From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on atuin.qyliss.net X-Spam-Level: X-Spam-Status: No, score=-4.6 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS autolearn=unavailable autolearn_force=no version=3.4.6 Received: by atuin.qyliss.net (Postfix, from userid 496) id C80D181E43; Wed, 13 Oct 2021 13:54:12 +0000 (UTC) Received: from atuin.qyliss.net (localhost [IPv6:::1]) by atuin.qyliss.net (Postfix) with ESMTP id 43CD581CE4; Wed, 13 Oct 2021 13:53:36 +0000 (UTC) Received: by atuin.qyliss.net (Postfix, from userid 496) id 2D70381CB0; Wed, 13 Oct 2021 13:53:32 +0000 (UTC) Received: from out5-smtp.messagingengine.com (out5-smtp.messagingengine.com [66.111.4.29]) by atuin.qyliss.net (Postfix) with ESMTPS id 3F87F81CAD for ; Wed, 13 Oct 2021 13:53:27 +0000 (UTC) Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 47D1A5C00F2 for ; Wed, 13 Oct 2021 09:53:25 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Wed, 13 Oct 2021 09:53:25 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alyssa.is; h= from:to:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; s=fm1; bh=jIWgElGgGBmT7 bJ9jCQvgTUbSLDBnTlJjunAh8JjXbI=; b=P2aQa6vcW5hsBYOnBjLETBoue8h/M 3+PN3gcpwLdI3pZ/Xs5r4NZkjohA1nzhaSCA0MSKeNDDbT7T8p0Q2GCsvNqIwTrf nzWeDDhw+/DQxW7yMq/3QH3c2B5YgqN7tIUjMpgSbk5DoCy7CSTrwz6Vx4DYhrlR yU2xJ7XWjbYjUNxRCPyjLA2351boge3/NPPJaA+GOMQJhoVUXD2PaycYzmpNVCYj yBtxrWzcYFMZNT1OM+zhBiXDfwWuou8HURq9KGmXMUCxtQ1VdhM5ErvxYbZE/qqa ava7Q4akHLxqy5l+38chauzq3IZZnl9a8Lh/QcflWkOmLPg24OEgNY0BQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; bh=jIWgElGgGBmT7bJ9jCQvgTUbSLDBnTlJjunAh8JjXbI=; b=kkfucYki FwQxJuSTleNnej3SJ9jxhhOLyRXkF4SY/z2nyytTHEQyKhLEaforSnQt36kjcXlT /d95DoFJ4FrNbfW3O9frt43CEknUhDX0D4jNtPJCcIEIosZ0qhNVZL8fsh+Rh+vR 1vUDiiRHf3lttTyDpklHh4z8Ce9YayG3JT4n8McFAvopnNYVc6Yz25MvVgu0NJzT 0I48TAFiU6ikX8iu/tH4Mb4KvnRahTRQZ7Heh0TnTqU2B/EgHG+CDaXt0nVvACxP ejEcsRuGkAmUGOy4TXYlmryq3+irvL3D8xJwCC+cwclusGm00u3kGRlsRjHTVghe 3fbJ4N1ylIlmpQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvtddrvddutddgieekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefhvffufffkofgjfhgggfestdekre dtredttdenucfhrhhomheptehlhihsshgrucftohhsshcuoehhihesrghlhihsshgrrdhi sheqnecuggftrfgrthhtvghrnhepgfefudekvdelieelledufeevheeglefggedvudejvd dtffeuueevffehleejkedvnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehm rghilhhfrhhomhepqhihlhhishhsseigvddvtddrqhihlhhishhsrdhnvght X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Wed, 13 Oct 2021 09:53:24 -0400 (EDT) Received: by x220.qyliss.net (Postfix, from userid 1000) id 358EC17BD; Wed, 13 Oct 2021 13:53:23 +0000 (UTC) From: Alyssa Ross To: devel@spectrum-os.org Subject: [DEMO PATCH linux 3/3] usbip: tools: usbip: support vsock(7) connections Date: Wed, 13 Oct 2021 13:53:13 +0000 Message-Id: <20211013135313.2494599-4-hi@alyssa.is> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211013135313.2494599-1-hi@alyssa.is> References: <20211013135313.2494599-1-hi@alyssa.is> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: O7UX3NECTJDZI5P6FIYN42JLJ6DBDPWU X-Message-ID-Hash: O7UX3NECTJDZI5P6FIYN42JLJ6DBDPWU X-MailFrom: qyliss@x220.qyliss.net X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.4 Precedence: list List-Id: Patches and low-level development discussion Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: "usbip list" and "usbip attach" can now both be given a "-P vsock" to tell them to use vsock sockets instead of ip sockets. Signed-off-by: Alyssa Ross --- tools/usb/usbip/src/usbip_attach.c | 25 +++++++++----- tools/usb/usbip/src/usbip_list.c | 43 +++++++++++++++++------- tools/usb/usbip/src/usbip_network.c | 51 ++++++++++++++++++++++++++++- tools/usb/usbip/src/usbip_network.h | 4 ++- 4 files changed, 102 insertions(+), 21 deletions(-) diff --git a/tools/usb/usbip/src/usbip_attach.c b/tools/usb/usbip/src/usbip_attach.c index b202c7513bc3..74adf9f09d01 100644 --- a/tools/usb/usbip/src/usbip_attach.c +++ b/tools/usb/usbip/src/usbip_attach.c @@ -26,10 +26,11 @@ #include "usbip.h" static const char usbip_attach_usage_string[] = - "usbip attach \n" - " -r, --remote= The machine with exported USB devices\n" + "usbip attach [-P|--proto] \n" + " -P, --proto Protocol family (ip or vsock)\n" + " -r, --remote= The machine with exported USB devices\n" " -b, --busid= Busid of the device on \n" - " -d, --device= Id of the virtual UDC on \n"; + " -d, --device= Id of the virtual UDC on \n"; void usbip_attach_usage(void) { @@ -184,20 +185,23 @@ static int get_protocol_family(int fd, char **proto) case AF_INET6: *proto = "ip"; return 0; + case AF_VSOCK: + *proto = "vsock"; + return 0; } return -1; } -static int attach_device(char *host, char *busid) +static int attach_device(char *proto, char *host, char *busid) { int sockfd; int rc; int rhport; char *pf; - sockfd = usbip_net_tcp_connect(host, usbip_port_string); + sockfd = usbip_net_connect(proto, host, usbip_port_string); if (sockfd < 0) { - err("tcp connect"); + err("connect"); return -1; } @@ -222,23 +226,28 @@ static int attach_device(char *host, char *busid) int usbip_attach(int argc, char *argv[]) { static const struct option opts[] = { + { "proto", required_argument, NULL, 'P' }, { "remote", required_argument, NULL, 'r' }, { "busid", required_argument, NULL, 'b' }, { "device", required_argument, NULL, 'd' }, { NULL, 0, NULL, 0 } }; + char *proto = NULL; char *host = NULL; char *busid = NULL; int opt; int ret = -1; for (;;) { - opt = getopt_long(argc, argv, "d:r:b:", opts, NULL); + opt = getopt_long(argc, argv, "P:d:r:b:", opts, NULL); if (opt == -1) break; switch (opt) { + case 'P': + proto = optarg; + break; case 'r': host = optarg; break; @@ -254,7 +263,7 @@ int usbip_attach(int argc, char *argv[]) if (!host || !busid) goto err_out; - ret = attach_device(host, busid); + ret = attach_device(proto, host, busid); goto out; err_out: diff --git a/tools/usb/usbip/src/usbip_list.c b/tools/usb/usbip/src/usbip_list.c index 3d810bcca02f..f479e15f9ae7 100644 --- a/tools/usb/usbip/src/usbip_list.c +++ b/tools/usb/usbip/src/usbip_list.c @@ -30,7 +30,8 @@ #include "usbip.h" static const char usbip_list_usage_string[] = - "usbip list [-p|--parsable] \n" + "usbip list [-P|--proto] [-p|--parsable] \n" + " -P, --proto Protocol family (ip or vsock)\n" " -p, --parsable Parsable list format\n" " -r, --remote= List the exportable USB devices on \n" " -l, --local List the local USB devices\n" @@ -125,12 +126,12 @@ static int get_exported_devices(char *host, int sockfd) return 0; } -static int list_exported_devices(char *host) +static int list_exported_devices(char *proto, char *host) { int rc; int sockfd; - sockfd = usbip_net_tcp_connect(host, usbip_port_string); + sockfd = usbip_net_connect(proto, host, usbip_port_string); if (sockfd < 0) { err("could not connect to %s:%s: %s", host, usbip_port_string, gai_strerror(sockfd)); @@ -326,44 +327,64 @@ static int list_gadget_devices(bool parsable) int usbip_list(int argc, char *argv[]) { static const struct option opts[] = { + { "proto", required_argument, NULL, 'P' }, { "parsable", no_argument, NULL, 'p' }, { "remote", required_argument, NULL, 'r' }, { "local", no_argument, NULL, 'l' }, - { "device", no_argument, NULL, 'd' }, + { "device", no_argument, NULL, 'd' }, { NULL, 0, NULL, 0 } }; bool parsable = false; + char *proto = NULL, *host = NULL; int opt; + int mode = -1; int ret = -1; if (usbip_names_init(USBIDS_FILE)) err("failed to open %s", USBIDS_FILE); for (;;) { - opt = getopt_long(argc, argv, "pr:ld", opts, NULL); + opt = getopt_long(argc, argv, "P:pr:ld", opts, NULL); if (opt == -1) break; switch (opt) { + case 'P': + proto = optarg; + break; case 'p': parsable = true; break; case 'r': - ret = list_exported_devices(optarg); - goto out; + host = optarg; + /* fall through */ case 'l': - ret = list_devices(parsable); - goto out; case 'd': - ret = list_gadget_devices(parsable); - goto out; + if (mode != -1) { + err("-%c and -%c are mutually exclusive", mode, opt); + goto out; + } + mode = opt; + break; default: goto err_out; } } + switch (mode) { + case 'r': + ret = list_exported_devices(proto, host); + goto out; + case 'l': + ret = list_devices(parsable); + goto out; + case 'd': + ret = list_gadget_devices(parsable); + goto out; + } + err_out: usbip_list_usage(); out: diff --git a/tools/usb/usbip/src/usbip_network.c b/tools/usb/usbip/src/usbip_network.c index ed4dc8c14269..c096dbb6afb3 100644 --- a/tools/usb/usbip/src/usbip_network.c +++ b/tools/usb/usbip/src/usbip_network.c @@ -9,10 +9,14 @@ #include #include +#include +#include #include #include #include +#include + #ifdef HAVE_LIBWRAP #include #endif @@ -258,7 +262,7 @@ int usbip_net_set_v6only(int sockfd) /* * IPv6 Ready */ -int usbip_net_tcp_connect(char *hostname, char *service) +int usbip_net_ip_connect(char *hostname, char *service) { struct addrinfo hints, *res, *rp; int sockfd; @@ -301,3 +305,48 @@ int usbip_net_tcp_connect(char *hostname, char *service) return sockfd; } + +int usbip_net_vsock_connect(char *cid_s, char *port_s) +{ + char *cid_end, *port_end; + int sockfd; + unsigned long cid, port; + struct sockaddr_vm addr = { 0 }; + socklen_t addrlen = sizeof addr; + + errno = 0; + cid = strtoul(cid_s, &cid_end, 0); + port = strtoul(port_s, &port_end, 0); + if (*cid_end || *port_end) { + errno = EINVAL; + return EAI_SYSTEM; + } + if (errno) + return EAI_SYSTEM; + if (cid > UINT_MAX || port > UINT_MAX) { + errno = ERANGE; + return EAI_SYSTEM; + } + + addr.svm_family = AF_VSOCK; + addr.svm_port = port; + addr.svm_cid = cid; + + sockfd = socket(AF_VSOCK, SOCK_STREAM, 0); + if (sockfd == -1) + return EAI_SYSTEM; + if (connect(sockfd, (struct sockaddr *)&addr, addrlen) == -1) + return EAI_SYSTEM; + return sockfd; +} + +int usbip_net_connect(char *proto, char *host, char *service) +{ + if (!proto || !strcmp(proto, "ip")) + return usbip_net_ip_connect(host, service); + if (!strcmp(proto, "vsock")) + return usbip_net_vsock_connect(host, service); + + errno = EPROTONOSUPPORT; + return EAI_SYSTEM; +} diff --git a/tools/usb/usbip/src/usbip_network.h b/tools/usb/usbip/src/usbip_network.h index 83b4c5344f72..fc2579525bc3 100644 --- a/tools/usb/usbip/src/usbip_network.h +++ b/tools/usb/usbip/src/usbip_network.h @@ -173,6 +173,8 @@ int usbip_net_set_reuseaddr(int sockfd); int usbip_net_set_nodelay(int sockfd); int usbip_net_set_keepalive(int sockfd); int usbip_net_set_v6only(int sockfd); -int usbip_net_tcp_connect(char *hostname, char *port); +int usbip_net_ip_connect(char *hostname, char *port); +int usbip_net_vsock_connect(char *cid, char *port); +int usbip_net_connect(char *proto, char *host, char *port); #endif /* __USBIP_NETWORK_H */ -- 2.32.0