From nobody Thu Jan 26 18:54:20 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4P2qcs2gbZz3bPS2; Thu, 26 Jan 2023 18:54:21 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4P2qcs18ZNz45t9; Thu, 26 Jan 2023 18:54:21 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1674759261; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=rqSJsbd/kjjg1MrwLurta53HUMnSjX7e1KP4jUnVm/4=; b=ZZn1tsywqYBMvXO3TjOaCmXpZ/kdXtATjrX9k6OrkKb0H1AR1frputo+6lkRPlU/BEv0sN yakHNtXunjyxz3EjtQ3t0G2DUgO9TPcz7fhp0WQ6cJqgKTJ2WRFzvkAvDP2RuKzHjysh/e CqohBPCDiX7EFdz0iKhYZgbacMGibxaPaHDEtSRuGAhHuQJPbuPbMz9dZz3dGR5rM0V05N jjQUaRihLOGZ7FF4MFw0xp2yZzx88r6OwSNkvkyYxr56eoJaOeK8DG8SJif+gkrklj0RUd of/vwwWBgT1Rcfs6Iw9oCje96Li2MEV3DokyHgvpTxm3IZV7PS6QwetpEIHakQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1674759261; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=rqSJsbd/kjjg1MrwLurta53HUMnSjX7e1KP4jUnVm/4=; b=pHqyqMtmquMCZNw28UyoTR5oUZpjR7fZ6dL3i3HWqSJQI+UERX1LJ+/86ErYQnX1AatEfy tbu/EfGVa/o3ubVbfoWfhtPaMjJsUhZMKJLB/056SYK7XH8wto8oJcLsZ+Nl0sTtbH/JHT i14rTkdO2CPGOwmCtizYib6818ueuwPwnPiTeta7Ngxt6J00uQ95F2E4s4HvNs+z2/9mPJ EyxZd/wrwj3rV4Ov10ETOm2rtNm/6my1t0bYrmKn0XUYtG2DBcOARJ8rDgi19q4Fp6Cwdf 0olKorSjXq44eIc6mSHccAOI/oYFFc/CmqMXY34aPvhX8mUYWK2Yz9KmIHIyZw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1674759261; a=rsa-sha256; cv=none; b=Ysbein7RvT7Uu7rsci0dOsQ/5iS/j8TPQZMcA1sPiU8fL0wkIvomqTwHzpToEH8WAqE8XN AZABuhMGR69T8wMeQy85WMw83AEvaMeIv232gWR6cMnq7rX/Os8zx5D1M75S2dSVvIbTmT VmFixEV8hrZfNWEbNpvx98dfvSlt7Nl5+01DrywrGsuwz6s+H4IrSok+219/SOFNR/BJqR zyM7F80kAV8kzCsNaHbkCyB93icqIz4LDtf8yMoJoNLAFH3CJqFo4AcCjBwuikxXNXUBGC hdda0V6Yz/F0zwlBptgu1t6xw9SeoAiBL7fam29rpzfrIv/n4Zs/x3B5ixibZw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4P2qcs0GF5zg38; Thu, 26 Jan 2023 18:54:21 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 30QIsKql034085; Thu, 26 Jan 2023 18:54:20 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 30QIsKkH034084; Thu, 26 Jan 2023 18:54:20 GMT (envelope-from git) Date: Thu, 26 Jan 2023 18:54:20 GMT Message-Id: <202301261854.30QIsKkH034084@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: John Baldwin Subject: git: e96f9fa135a2 - stable/13 - bhyve/snapshot: use SOCK_DGRAM instead of SOCK_STREAM List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jhb X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: e96f9fa135a24befbc7ea611f38848d83bff9a4d Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=e96f9fa135a24befbc7ea611f38848d83bff9a4d commit e96f9fa135a24befbc7ea611f38848d83bff9a4d Author: Robert Wing AuthorDate: 2021-03-08 00:23:29 +0000 Commit: John Baldwin CommitDate: 2023-01-26 18:46:01 +0000 bhyve/snapshot: use SOCK_DGRAM instead of SOCK_STREAM The save/restore feature uses a unix domain socket to send messages from bhyvectl(8) to a bhyve(8) process. A datagram socket will suffice for this. An added benefit of using a datagram socket is simplified code. For bhyve, the listen/accept calls are dropped; and for bhyvectl, the connect() call is dropped. EPRINTLN handles raw mode for bhyve(8), use it to print error messages. Reviewed by: jhb Differential Revision: https://reviews.freebsd.org/D28983 (cherry picked from commit 38dfb0626fd35c64b2e2d5faae2c90e7981a3307) --- usr.sbin/bhyve/snapshot.c | 65 +++++++++++++++++--------------------------- usr.sbin/bhyvectl/bhyvectl.c | 22 ++++----------- 2 files changed, 31 insertions(+), 56 deletions(-) diff --git a/usr.sbin/bhyve/snapshot.c b/usr.sbin/bhyve/snapshot.c index 2938a0e84c88..061ba11096e9 100644 --- a/usr.sbin/bhyve/snapshot.c +++ b/usr.sbin/bhyve/snapshot.c @@ -79,6 +79,7 @@ __FBSDID("$FreeBSD$"); #include "bhyverun.h" #include "acpi.h" #include "atkbdc.h" +#include "debug.h" #include "inout.h" #include "fwctl.h" #include "ioapic.h" @@ -117,8 +118,6 @@ static sig_t old_winch_handler; #define MAX_VMNAME 100 -#define MAX_MSG_SIZE 1024 - #define SNAPSHOT_BUFFER_SIZE (20 * MB) #define JSON_STRUCT_ARR_KEY "structs" @@ -1442,24 +1441,10 @@ done: } int -get_checkpoint_msg(int conn_fd, struct vmctx *ctx) +handle_message(struct checkpoint_op *checkpoint_op, struct vmctx *ctx) { - unsigned char buf[MAX_MSG_SIZE]; - struct checkpoint_op *checkpoint_op; - int len, recv_len, total_recv = 0; - int err = 0; - - len = sizeof(struct checkpoint_op); /* expected length */ - while ((recv_len = recv(conn_fd, buf + total_recv, len - total_recv, 0)) > 0) { - total_recv += recv_len; - } - if (recv_len < 0) { - perror("Error while receiving data from bhyvectl"); - err = -1; - goto done; - } + int err; - checkpoint_op = (struct checkpoint_op *)buf; switch (checkpoint_op->op) { case START_CHECKPOINT: err = vm_checkpoint(ctx, checkpoint_op->snapshot_filename, false); @@ -1468,12 +1453,13 @@ get_checkpoint_msg(int conn_fd, struct vmctx *ctx) err = vm_checkpoint(ctx, checkpoint_op->snapshot_filename, true); break; default: - fprintf(stderr, "Unrecognized checkpoint operation.\n"); + EPRINTLN("Unrecognized checkpoint operation\n"); err = -1; } -done: - close(conn_fd); + if (err != 0) + EPRINTLN("Unable to perform the requested operation\n"); + return (err); } @@ -1483,21 +1469,25 @@ done: void * checkpoint_thread(void *param) { + struct checkpoint_op op; struct checkpoint_thread_info *thread_info; - int conn_fd, ret; + ssize_t n; pthread_set_name_np(pthread_self(), "checkpoint thread"); thread_info = (struct checkpoint_thread_info *)param; - while ((conn_fd = accept(thread_info->socket_fd, NULL, NULL)) > -1) { - ret = get_checkpoint_msg(conn_fd, thread_info->ctx); - if (ret != 0) { - fprintf(stderr, "Failed to read message on checkpoint " - "socket. Retrying.\n"); - } - } - if (conn_fd < -1) { - perror("Failed to accept connection"); + for (;;) { + n = recvfrom(thread_info->socket_fd, &op, sizeof(op), 0, NULL, 0); + + /* + * slight sanity check: see if there's enough data to at + * least determine the type of message. + */ + if (n >= sizeof(op.op)) + handle_message(&op, thread_info->ctx); + else + EPRINTLN("Failed to receive message: %s\n", + n == -1 ? strerror(errno) : "unknown error"); } return (NULL); @@ -1527,9 +1517,9 @@ init_checkpoint_thread(struct vmctx *ctx) if (err != 0) errc(1, err, "checkpoint cv init"); - socket_fd = socket(PF_UNIX, SOCK_STREAM, 0); + socket_fd = socket(PF_UNIX, SOCK_DGRAM, 0); if (socket_fd < 0) { - perror("Socket creation failed (IPC with bhyvectl"); + EPRINTLN("Socket creation failed: %s", strerror(errno)); err = -1; goto fail; } @@ -1548,13 +1538,8 @@ init_checkpoint_thread(struct vmctx *ctx) unlink(addr.sun_path); if (bind(socket_fd, (struct sockaddr *)&addr, addr.sun_len) != 0) { - perror("Failed to bind socket (IPC with bhyvectl)"); - err = -1; - goto fail; - } - - if (listen(socket_fd, 10) < 0) { - perror("Failed to listen on socket (IPC with bhyvectl)"); + EPRINTLN("Failed to bind socket \"%s\": %s\n", + addr.sun_path, strerror(errno)); err = -1; goto fail; } diff --git a/usr.sbin/bhyvectl/bhyvectl.c b/usr.sbin/bhyvectl/bhyvectl.c index 0f7b9533fe4b..df02f7caf345 100644 --- a/usr.sbin/bhyvectl/bhyvectl.c +++ b/usr.sbin/bhyvectl/bhyvectl.c @@ -1687,11 +1687,11 @@ static int send_checkpoint_op_req(struct vmctx *ctx, struct checkpoint_op *op) { struct sockaddr_un addr; - int socket_fd, len, len_sent, total_sent; - int err = 0; + ssize_t len_sent; + int err, socket_fd; char vmname_buf[MAX_VMNAME]; - socket_fd = socket(PF_UNIX, SOCK_STREAM, 0); + socket_fd = socket(PF_UNIX, SOCK_DGRAM, 0); if (socket_fd < 0) { perror("Error creating bhyvectl socket"); err = -1; @@ -1709,21 +1709,11 @@ send_checkpoint_op_req(struct vmctx *ctx, struct checkpoint_op *op) snprintf(addr.sun_path, sizeof(addr.sun_path), "%s%s", BHYVE_RUN_DIR, vmname_buf); - if (connect(socket_fd, (struct sockaddr *)&addr, - sizeof(struct sockaddr_un)) != 0) { - perror("Connect to VM socket failed"); - err = -1; - goto done; - } - - len = sizeof(*op); - total_sent = 0; - while ((len_sent = send(socket_fd, (char *)op + total_sent, len - total_sent, 0)) > 0) { - total_sent += len_sent; - } + len_sent = sendto(socket_fd, op, sizeof(*op), 0, + (struct sockaddr *)&addr, sizeof(struct sockaddr_un)); if (len_sent < 0) { - perror("Failed to send checkpoint operation request"); + perror("Failed to send message to bhyve vm"); err = -1; }