From nobody Thu May 2 07:06:44 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1573798233; cv=none; d=zoho.com; s=zohoarc; b=DnwmcDhPRdx6M8/dC0942Q4bheLGi2jUvS/5roelTO69p+xhU0frmJee2Wg3hX/Mn+dp4OqE04icAvLZxPuOOIUHeKOz8Fyl1gyOJ5gvx8WBSHnBotGpIq0VR7Z0FKgnQvKJD+jqfbQJdXrSZmrDeI5zEkKNTTBNczaEsLHwTf8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1573798233; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=LVqzV8tLnk+0CWhsZn8dhcU+MYaathjQ0Eug+EgFAcQ=; b=QFx1dXWrm2y7MnxjHa3MhHkyyyiZHgjrNSFV0P63UWWaWwWHakCSLadduOCOCVcZ25kT/CowTsJ2D1B44lqAt47h9tyPjRAdBf9GVsvlx97bv5OBK9yaLFo0wTj8UxIPnRM53iR/HhqOaZQwLcpFvJYXNrhWtDsO3L0xhvob0cY= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1573798233729270.37133387650897; Thu, 14 Nov 2019 22:10:33 -0800 (PST) Received: from localhost ([::1]:36046 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iVUoN-0004yM-MO for importer@patchew.org; Fri, 15 Nov 2019 01:10:27 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:58397) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iVUnd-0004VJ-PA for qemu-devel@nongnu.org; Fri, 15 Nov 2019 01:09:42 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iVUnc-0005do-HZ for qemu-devel@nongnu.org; Fri, 15 Nov 2019 01:09:41 -0500 Received: from mail-pl1-x643.google.com ([2607:f8b0:4864:20::643]:33185) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iVUnc-0005dT-BU for qemu-devel@nongnu.org; Fri, 15 Nov 2019 01:09:40 -0500 Received: by mail-pl1-x643.google.com with SMTP id ay6so3911611plb.0 for ; Thu, 14 Nov 2019 22:09:39 -0800 (PST) Received: from localhost.localdomain.com ([209.132.188.80]) by smtp.googlemail.com with ESMTPSA id r24sm10644284pgu.36.2019.11.14.22.09.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Nov 2019 22:09:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=LVqzV8tLnk+0CWhsZn8dhcU+MYaathjQ0Eug+EgFAcQ=; b=Rg5bxtpIuWh0laJscM3MEGgO5D/mYa/5ssQTDfJo2wNaDth5lS5hra6gpzB80k5IgK 4/pKozrFqmwuJUrdHPxPmSPeHwQNHxCl5vE6KfK/gJ1uGZ3LEq7ayr/fxkI9iI/Hyazv Pz+2VvtTdWug0EcITPiOpsLV0OJt0GziqAxSaEv5B0ORtlNjrcV3djD5XnBoJZTQjeWp otx3m+OMpdNo58FoX5UZjdaRj0RdaVxJIwr0eskaSH37nLkZauC7qG1o2DkNjhsdbSJA FSoUpTez/d+oBc8UdtifYVaTPbRDCVo/8bye8OTXYWj33oKYhz9lUjB0+pJiLMBDbg7b R32A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=LVqzV8tLnk+0CWhsZn8dhcU+MYaathjQ0Eug+EgFAcQ=; b=IGBbl/zrJofqTC7aCu/S2ydArUDjTSf0kgFqz4EKP5XpgCP5KvV+SVBVjmkoc/SgI4 /QjwCCDLKt6xbTApP0IhEHqiCx1Fgi7sLwWk18l2iJ8AmhXQn5ICbg6cOWMvK0d6CkwX Nz0HEwopSSwwjqriiGhcsao6BvdyT3uGPC11E08aIY9mCXmAe03RvIBSvBbM9UwaOrZD Xjb7Ggqay3JjvPhi8qXLvajQ/NQzwM3ygDv+qWJcsdlyQu1aATdLFUteu6m9yOiJJIJo jWD55pX4wHIU6TDQZFa13HZylkV5lvfWIh6/VwXkcOSQTjqwlxKixmF00sWlq5qZd1S/ 7OaQ== X-Gm-Message-State: APjAAAXU21/VWZigf6QJehhMzpNwRHxJL8/3rt6v9QH9jD6mglSnQeoW S4zjL7wjHfcLVAmxaH953KZc0W/Ot2/xoA== X-Google-Smtp-Source: APXvYqzcZHnH4zaTiz88f4Fvl/L4Ps4Ag9Y2J9bpMbUYSHOfHdESSMOiuHQtvtCJFpyOZEIANH239w== X-Received: by 2002:a17:90a:ff02:: with SMTP id ce2mr17594828pjb.117.1573798178122; Thu, 14 Nov 2019 22:09:38 -0800 (PST) From: Micky Yun Chan X-Google-Original-From: Micky Yun Chan To: qemu-devel@nongnu.org Subject: [PATCH] Implement backend program convention command for vhost-user-blk Date: Fri, 15 Nov 2019 14:09:25 +0800 Message-Id: <20191115060925.12346-1-michan@redhat.com> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::643 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: michan , Micky Yun Chan , stefanha@redhat.com, "Michael S. Tsirkin" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: michan Signed-off-by: Micky Yun Chan (michiboo) --- contrib/vhost-user-blk/vhost-user-blk.c | 95 +++++++++++++++---------- 1 file changed, 57 insertions(+), 38 deletions(-) diff --git a/contrib/vhost-user-blk/vhost-user-blk.c b/contrib/vhost-user-b= lk/vhost-user-blk.c index ae61034656..435ddc46b2 100644 --- a/contrib/vhost-user-blk/vhost-user-blk.c +++ b/contrib/vhost-user-blk/vhost-user-blk.c @@ -576,70 +576,89 @@ vub_new(char *blk_file) return vdev_blk; } =20 +static int opt_fdnum =3D -1; +static char *opt_socket_path; +static char *opt_blk_file; +static gboolean opt_print_caps; +static gboolean opt_read_only; + + +static GOptionEntry entries[] =3D { + { "print-capabilities", 'c', 0, G_OPTION_ARG_NONE, &opt_print_caps, + "Print capabilities", NULL }, + { "fd", 'f', 0, G_OPTION_ARG_INT, &opt_fdnum, + "Use inherited fd socket", "FDNUM" }, + { "socket-path", 's', 0, G_OPTION_ARG_FILENAME, &opt_socket_path, + "Use UNIX socket path", "PATH" }, + {"blk-file", 'b', 0, G_OPTION_ARG_FILENAME, &opt_blk_file, + "block device or file path", "PATH"}, + { "read-only", 'r', 0, G_OPTION_ARG_NONE, &opt_read_only, + "Enable read-only", NULL } +}; + int main(int argc, char **argv) -{ - int opt; - char *unix_socket =3D NULL; - char *blk_file =3D NULL; - bool enable_ro =3D false; +{ =20 int lsock =3D -1, csock =3D -1; VubDev *vdev_blk =3D NULL; + GError *error =3D NULL; + GOptionContext *context; =20 - while ((opt =3D getopt(argc, argv, "b:rs:h")) !=3D -1) { - switch (opt) { - case 'b': - blk_file =3D g_strdup(optarg); - break; - case 's': - unix_socket =3D g_strdup(optarg); - break; - case 'r': - enable_ro =3D true; - break; - case 'h': - default: - printf("Usage: %s [ -b block device or file, -s UNIX domain so= cket" - " | -r Enable read-only ] | [ -h ]\n", argv[0]); - return 0; - } + context =3D g_option_context_new(NULL); + g_option_context_add_main_entries(context, entries, NULL); + if (!g_option_context_parse(context, &argc, &argv, &error)) { + g_printerr("Option parsing failed: %s\n", error->message); + exit(EXIT_FAILURE); + } + if (opt_print_caps) { + g_print("{\n"); + g_print(" \"type\": \"blk\",\n"); + g_print(" \"features\": [\n"); + g_print(" \"blk-file\",\n"); + g_print(" \"read-only\"\n"); + g_print(" ]\n"); + g_print("}\n"); + exit(EXIT_SUCCESS); } =20 - if (!unix_socket || !blk_file) { + if (!opt_blk_file) { printf("Usage: %s [ -b block device or file, -s UNIX domain socket" " | -r Enable read-only ] | [ -h ]\n", argv[0]); return -1; } =20 - lsock =3D unix_sock_new(unix_socket); - if (lsock < 0) { - goto err; + if (opt_socket_path) { + lsock =3D unix_sock_new(opt_socket_path); + if (lsock < 0) { + exit(EXIT_FAILURE); + } + } else { + lsock =3D opt_fdnum; } =20 - csock =3D accept(lsock, (void *)0, (void *)0); + csock =3D accept(lsock, NULL, NULL); if (csock < 0) { - fprintf(stderr, "Accept error %s\n", strerror(errno)); - goto err; + g_printerr("Accept error %s\n", strerror(errno)); + exit(EXIT_FAILURE); } =20 - vdev_blk =3D vub_new(blk_file); + vdev_blk =3D vub_new(opt_blk_file); if (!vdev_blk) { - goto err; + exit(EXIT_FAILURE); } - if (enable_ro) { + if (opt_read_only) { vdev_blk->enable_ro =3D true; } =20 if (!vug_init(&vdev_blk->parent, VHOST_USER_BLK_MAX_QUEUES, csock, vub_panic_cb, &vub_iface)) { - fprintf(stderr, "Failed to initialized libvhost-user-glib\n"); - goto err; + g_printerr("Failed to initialized libvhost-user-glib\n"); + exit(EXIT_FAILURE); } =20 g_main_loop_run(vdev_blk->loop); + g_main_loop_unref(vdev_blk->loop); =20 vug_deinit(&vdev_blk->parent); - -err: vub_free(vdev_blk); if (csock >=3D 0) { close(csock); @@ -647,8 +666,8 @@ err: if (lsock >=3D 0) { close(lsock); } - g_free(unix_socket); - g_free(blk_file); + g_free(opt_socket_path); + g_free(opt_blk_file); =20 return 0; } --=20 2.21.0