From patchwork Tue Nov 19 02:15:48 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Micky Yun Chan(michiboo)" X-Patchwork-Id: 1197130 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="gmPHndJU"; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 47H8Zv5LzWz9s4Y for ; Tue, 19 Nov 2019 13:16:37 +1100 (AEDT) Received: from localhost ([::1]:41054 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iWt4E-0005VK-Jl for incoming@patchwork.ozlabs.org; Mon, 18 Nov 2019 21:16:34 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:38770) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iWt3r-0005VC-BL for qemu-devel@nongnu.org; Mon, 18 Nov 2019 21:16:12 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iWt3p-0000cm-IO for qemu-devel@nongnu.org; Mon, 18 Nov 2019 21:16:11 -0500 Received: from mail-pg1-x541.google.com ([2607:f8b0:4864:20::541]:38751) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iWt3n-0000c9-FF for qemu-devel@nongnu.org; Mon, 18 Nov 2019 21:16:08 -0500 Received: by mail-pg1-x541.google.com with SMTP id 15so10559194pgh.5 for ; Mon, 18 Nov 2019 18:16:06 -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=a6jCuCAelSuIndEzbgorEPQx5AkNha1h9EkMyOLHxpk=; b=gmPHndJUhWiSiqlDmkiNOVwMtMVAV/MIjHcbotE4GXOmzsqMikUkW89C4tc8MgLCaX AMjJcp+Wf1V64+pOpAj6UJpnkhhd55TWBGHYdPtI92Xeao346vTTP6QUKt8XvnKyo5gg zFhfP7YzrrsyGPKgXdeGpUg69N73u9nifjiDk5Zojfpf8Sdk24jUNqJjeyDJMydHpEp8 9JZ43PGXMN7nOGBlJZhUZRRDLnhZ4VrgCEWiG55qjyUbFgfQvPYo2wCLr/Wa1GAskAjp QhUjWU+vBs4Yh0NMUmJZZ0EStFr+tlVo/QGP5FM0Ox17H2iXfDqPuFCQDtfrojfJ1VVG zCAw== 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=a6jCuCAelSuIndEzbgorEPQx5AkNha1h9EkMyOLHxpk=; b=qnoBzkXA0KlGnkS8ZwcRogmD6jr6jTmiPZIcpFby1+r+Hhb0gqiWbbwl5049eIsjUh RzNk6KPToYplFiX2E/G5eaTBHmhkNNoUQTaxQzGWtaN49EkK49hxk+Vkvch3xuH8MDiJ zeb9MqSu7hv8AuVeyy5oBHOh4uUHkMpK5mHchRTTCIANUo9vPi2m9xWY6vbqxKxYXC1N vUPus+hdbpciX7DfSA/sq3UOw/47EWno+4E1w/rZi57KTfDXDXQnm4zPW39bJdohSfTS BBm/bI5XOjpr9+2YpOB0iIizaCQbgSaGZ0VV16XiQ0Y+7/PJNZJEjKkqV1C2M2xtr+qs +Q5w== X-Gm-Message-State: APjAAAVJdTb76XWjZuFDMeM5b9gMyoRFxFpw5+co0xAO15AmnBqqUsum AiGYHDiJe4VUCKv1InsCPWm1Ia6hfz/i6+cI X-Google-Smtp-Source: APXvYqxU3m8tVGSrv58DSkyeEr3Hl/TgDtLz2I/jHmpzpReBJ/Sx/y+j1Ic/sZD54Z6V+T4qMybhuA== X-Received: by 2002:aa7:8690:: with SMTP id d16mr2751619pfo.117.1574129765119; Mon, 18 Nov 2019 18:16:05 -0800 (PST) Received: from localhost.localdomain.com ([209.132.188.80]) by smtp.googlemail.com with ESMTPSA id h9sm7169247pgk.84.2019.11.18.18.16.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Nov 2019 18:16:04 -0800 (PST) From: Micky Yun Chan X-Google-Original-From: Micky Yun Chan To: qemu-devel@nongnu.org Subject: [PATCH v3] Implement backend program convention command for vhost-user-blk Date: Tue, 19 Nov 2019 10:15:48 +0800 Message-Id: <20191119021548.10101-1-michan@redhat.com> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::541 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" From: michan This patch is to add standard commands defined in docs/interop/vhost-user.rst For vhost-user-* program Signed-off-by: Micky Yun Chan (michiboo) --- contrib/vhost-user-blk/vhost-user-blk.c | 109 ++++++++++++++---------- 1 file changed, 65 insertions(+), 44 deletions(-) diff --git a/contrib/vhost-user-blk/vhost-user-blk.c b/contrib/vhost-user-blk/vhost-user-blk.c index ae61034656..c26a0214a5 100644 --- a/contrib/vhost-user-blk/vhost-user-blk.c +++ b/contrib/vhost-user-blk/vhost-user-blk.c @@ -576,70 +576,91 @@ vub_new(char *blk_file) return vdev_blk; } +static int opt_fdnum = -1; +static char *opt_socket_path; +static char *opt_blk_file; +static gboolean opt_print_caps; +static gboolean opt_read_only; + + +static GOptionEntry entries[] = { + { "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 = NULL; - char *blk_file = NULL; - bool enable_ro = false; int lsock = -1, csock = -1; VubDev *vdev_blk = NULL; + GError *error = NULL; + GOptionContext *context; - while ((opt = getopt(argc, argv, "b:rs:h")) != -1) { - switch (opt) { - case 'b': - blk_file = g_strdup(optarg); - break; - case 's': - unix_socket = g_strdup(optarg); - break; - case 'r': - enable_ro = true; - break; - case 'h': - default: - printf("Usage: %s [ -b block device or file, -s UNIX domain socket" - " | -r Enable read-only ] | [ -h ]\n", argv[0]); - return 0; + context = 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(" \"read-only\",\n"); + g_print(" \"blk-file\"\n"); + g_print(" ]\n"); + g_print("}\n"); + exit(EXIT_SUCCESS); + } + + if (!opt_blk_file) { + g_print("%s", g_option_context_get_help(context, true, NULL)); + exit(EXIT_FAILURE); + } + + if (opt_socket_path) { + lsock = unix_sock_new(opt_socket_path); + if (lsock < 0) { + exit(EXIT_FAILURE); } + } else if(opt_fdnum < 0){ + g_print("%s", g_option_context_get_help(context, true, NULL)); + exit(EXIT_FAILURE); + } else { + lsock = opt_fdnum; } - if (!unix_socket || !blk_file) { - printf("Usage: %s [ -b block device or file, -s UNIX domain socket" - " | -r Enable read-only ] | [ -h ]\n", argv[0]); - return -1; - } - - lsock = unix_sock_new(unix_socket); - if (lsock < 0) { - goto err; - } - - csock = accept(lsock, (void *)0, (void *)0); + csock = 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); } - vdev_blk = vub_new(blk_file); + vdev_blk = vub_new(opt_blk_file); if (!vdev_blk) { - goto err; + exit(EXIT_FAILURE); } - if (enable_ro) { + if (opt_read_only) { vdev_blk->enable_ro = true; } 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 initialize libvhost-user-glib\n"); + exit(EXIT_FAILURE); } g_main_loop_run(vdev_blk->loop); - + g_main_loop_unref(vdev_blk->loop); + g_option_context_free(context); vug_deinit(&vdev_blk->parent); - -err: vub_free(vdev_blk); if (csock >= 0) { close(csock); @@ -647,8 +668,8 @@ err: if (lsock >= 0) { close(lsock); } - g_free(unix_socket); - g_free(blk_file); + g_free(opt_socket_path); + g_free(opt_blk_file); return 0; }