From nobody Tue Feb 10 09:57:35 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1606206673; cv=none; d=zohomail.com; s=zohoarc; b=DaSjaRb3Yw14ZvFJ8QOMoorN7EVIzGJlu+0cOiJq+78f9tPUw/dntkNvhA82a41EzQSj6UdfsaZDLnXszlBbfhje9Y6p37KlTZcB6KxcEOYzsCtf2Hkyuc/8H0dBSdb1ykSHp3YKsFuqQbVhN/TKg7+Rxo7uxeEGPgBqoHLfEfE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1606206673; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=4uWNFHLUmZLexL69CUdt3mhLOWrq8aO99ml+uC2JZLc=; b=nM6Rpjq7MWrxa+hBOk4yBLxyU74afqzjYnbvR4FcY4Z6f+uvUiI0J8khJGy9imcjFCK5sGbHNvJ7Yv1c4ARIn5Ts+kew9petoAuVaLWin0UXONddOlgJ50c7IyP8MYazRzLGWLDlrMG1GghfV/8KQtDusgHW8oUsFPufBb2f9Ho= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1606206673400724.8409915797811; Tue, 24 Nov 2020 00:31:13 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.35515.67192 (Exim 4.92) (envelope-from ) id 1khTiv-0003iy-Dk; Tue, 24 Nov 2020 08:30:53 +0000 Received: by outflank-mailman (output) from mailman id 35515.67192; Tue, 24 Nov 2020 08:30:53 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1khTiu-0003ho-No; Tue, 24 Nov 2020 08:30:52 +0000 Received: by outflank-mailman (input) for mailman id 35515; Tue, 24 Nov 2020 08:30:45 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1khTin-0003QD-IE for xen-devel@lists.xenproject.org; Tue, 24 Nov 2020 08:30:45 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1khTim-0006bN-PH; Tue, 24 Nov 2020 08:30:44 +0000 Received: from ec2-54-145-241-208.compute-1.amazonaws.com ([54.145.241.208] helo=ip-10-0-29-170.ec2.internal) by xenbits.xenproject.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1khTH5-0001hp-9V; Tue, 24 Nov 2020 08:02:07 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1khTin-0003QD-IE for xen-devel@lists.xenproject.org; Tue, 24 Nov 2020 08:30:45 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1khTim-0006bN-PH; Tue, 24 Nov 2020 08:30:44 +0000 Received: from ec2-54-145-241-208.compute-1.amazonaws.com ([54.145.241.208] helo=ip-10-0-29-170.ec2.internal) by xenbits.xenproject.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1khTH5-0001hp-9V; Tue, 24 Nov 2020 08:02:07 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xen.org; s=20200302mail; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=4uWNFHLUmZLexL69CUdt3mhLOWrq8aO99ml+uC2JZLc=; b=VbSOpiTVNdY2BNAwshKashhMc G/WA39HxAw2hZbB5TMirCRuo/4dHujZhd9YtzKPUn740wEreahZTqOlc9k1iDzP3MeUyiHU0xRvkr 4YWXQvk+Kw0Vhu7PYfNCRlKQu8tiSdC1hkv6OkXLsdD8Z/R6mu5SUoJ5zqeiDZmNyJuac=; From: Paul Durrant To: xen-devel@lists.xenproject.org Cc: Paul Durrant , Ian Jackson , Wei Liu , Christian Lindig , David Scott , Anthony PERARD Subject: [PATCH v4 21/23] xl / libxl: support naming of assignable devices Date: Tue, 24 Nov 2020 08:01:57 +0000 Message-Id: <20201124080159.11912-22-paul@xen.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20201124080159.11912-1-paul@xen.org> References: <20201124080159.11912-1-paul@xen.org> X-ZohoMail-DKIM: pass (identity @xen.org) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Paul Durrant This patch modifies libxl_device_pci_assignable_add() to take an optional 'name' argument, which (if supplied) is saved into xenstore and can hence be used to refer to the now-assignable BDF in subsequent operations. To facilitate this, a new libxl_device_pci_assignable_name2bdf() function is added. The xl code is modified to allow a name to be specified in the 'pci-assignable-add' operation and also allow an option to be specified to 'pci-assignable-list' requesting that names be displayed. The latter is facilitated by a new libxl_device_pci_assignable_bdf2name() function. Final= ly xl 'pci-assignable-remove' is modified to that either a name or BDF can be supplied. The supplied 'identifier' is first assumed to be a name, but if libxl_device_pci_assignable_name2bdf() fails to find a matching BDF the identifier itself will be parsed as a BDF. Names my only include printable characters and may not include whitespace. Signed-off-by: Paul Durrant --- Cc: Ian Jackson Cc: Wei Liu Cc: Christian Lindig Cc: David Scott Cc: Anthony PERARD v4: - Fix unitialized return value in libxl_device_pci_assignable_name2bdf() that was discovered in CI --- tools/include/libxl.h | 19 +++++++- tools/libs/light/libxl_pci.c | 86 ++++++++++++++++++++++++++++++++= +--- tools/ocaml/libs/xl/xenlight_stubs.c | 3 +- tools/xl/xl_cmdtable.c | 12 +++-- tools/xl/xl_pci.c | 84 ++++++++++++++++++++++++--------= --- 5 files changed, 166 insertions(+), 38 deletions(-) diff --git a/tools/include/libxl.h b/tools/include/libxl.h index 5703fdf367..4025d3a3d4 100644 --- a/tools/include/libxl.h +++ b/tools/include/libxl.h @@ -477,6 +477,14 @@ #define LIBXL_HAVE_PCI_ASSIGNABLE_BDF 1 =20 /* + * LIBXL_HAVE_PCI_ASSIGNABLE_NAME indicates that the + * libxl_device_pci_assignable_add() function takes a 'name' argument + * and that the libxl_device_pci_assignable_name2bdf() and + * libxl_device_pci_assignable_bdf2name() functions are defined. + */ +#define LIBXL_HAVE_PCI_ASSIGNABLE_NAME 1 + +/* * libxl ABI compatibility * * The only guarantee which libxl makes regarding ABI compatibility @@ -2385,11 +2393,18 @@ int libxl_device_events_handler(libxl_ctx *ctx, * added or is not bound, the functions will emit a warning but return * SUCCESS. */ -int libxl_device_pci_assignable_add(libxl_ctx *ctx, libxl_pci_bdf *pcibdf,= int rebind); -int libxl_device_pci_assignable_remove(libxl_ctx *ctx, libxl_pci_bdf *pcib= df, int rebind); +int libxl_device_pci_assignable_add(libxl_ctx *ctx, libxl_pci_bdf *pcibdf, + const char *name, int rebind); +int libxl_device_pci_assignable_remove(libxl_ctx *ctx, libxl_pci_bdf *pcib= df, + int rebind); libxl_pci_bdf *libxl_device_pci_assignable_list(libxl_ctx *ctx, int *num); void libxl_device_pci_assignable_list_free(libxl_pci_bdf *list, int num); =20 +libxl_pci_bdf *libxl_device_pci_assignable_name2bdf(libxl_ctx *ctx, + const char *name); +char *libxl_device_pci_assignable_bdf2name(libxl_ctx *ctx, + libxl_pci_bdf *pcibdf); + /* CPUID handling */ int libxl_cpuid_parse_config(libxl_cpuid_policy_list *cpuid, const char* s= tr); int libxl_cpuid_parse_config_xend(libxl_cpuid_policy_list *cpuid, diff --git a/tools/libs/light/libxl_pci.c b/tools/libs/light/libxl_pci.c index f9ace1faec..1da7fd5508 100644 --- a/tools/libs/light/libxl_pci.c +++ b/tools/libs/light/libxl_pci.c @@ -745,6 +745,7 @@ static int pciback_dev_unassign(libxl__gc *gc, libxl_pc= i_bdf *pcibdf) =20 static int libxl__device_pci_assignable_add(libxl__gc *gc, libxl_pci_bdf *pcibdf, + const char *name, int rebind) { libxl_ctx *ctx =3D libxl__gc_owner(gc); @@ -753,6 +754,23 @@ static int libxl__device_pci_assignable_add(libxl__gc = *gc, int rc; struct stat st; =20 + /* Sanitise any name that was passed */ + if (name) { + unsigned int i, n =3D strlen(name); + + if (n > 64) { /* Reasonable upper bound on name length */ + LOG(ERROR, "Name too long"); + return ERROR_FAIL; + } + + for (i =3D 0; i < n; i++) { + if (!isgraph(name[i])) { + LOG(ERROR, "Names may only include printable characters"); + return ERROR_FAIL; + } + } + } + /* Local copy for convenience */ dom =3D pcibdf->domain; bus =3D pcibdf->bus; @@ -773,7 +791,7 @@ static int libxl__device_pci_assignable_add(libxl__gc *= gc, } if ( rc ) { LOG(WARN, PCI_BDF" already assigned to pciback", dom, bus, dev, fu= nc); - goto quarantine; + goto name; } =20 /* Check to see if there's already a driver that we need to unbind fro= m */ @@ -804,7 +822,12 @@ static int libxl__device_pci_assignable_add(libxl__gc = *gc, return ERROR_FAIL; } =20 -quarantine: +name: + if (name) + pci_info_xs_write(gc, pcibdf, "name", name); + else + pci_info_xs_remove(gc, pcibdf, "name"); + /* * DOMID_IO is just a sentinel domain, without any actual mappings, * so always pass XEN_DOMCTL_DEV_RDM_RELAXED to avoid assignment being @@ -868,16 +891,18 @@ static int libxl__device_pci_assignable_remove(libxl_= _gc *gc, } } =20 + pci_info_xs_remove(gc, pcibdf, "name"); + return 0; } =20 int libxl_device_pci_assignable_add(libxl_ctx *ctx, libxl_pci_bdf *pcibdf, - int rebind) + const char *name, int rebind) { GC_INIT(ctx); int rc; =20 - rc =3D libxl__device_pci_assignable_add(gc, pcibdf, rebind); + rc =3D libxl__device_pci_assignable_add(gc, pcibdf, name, rebind); =20 GC_FREE; return rc; @@ -896,6 +921,57 @@ int libxl_device_pci_assignable_remove(libxl_ctx *ctx,= libxl_pci_bdf *pcibdf, return rc; } =20 +libxl_pci_bdf *libxl_device_pci_assignable_name2bdf(libxl_ctx *ctx, + const char *name) +{ + GC_INIT(ctx); + char **bdfs; + libxl_pci_bdf *pcibdf =3D NULL; + unsigned int i, n; + + bdfs =3D libxl__xs_directory(gc, XBT_NULL, PCI_INFO_PATH, &n); + if (!n) + goto out; + + pcibdf =3D calloc(1, sizeof(*pcibdf)); + if (!pcibdf) + goto out; + + for (i =3D 0; i < n; i++) { + unsigned dom, bus, dev, func; + const char *tmp; + + if (sscanf(bdfs[i], PCI_BDF_XSPATH, &dom, &bus, &dev, &func) !=3D = 4) + continue; + + pcibdf_struct_fill(pcibdf, dom, bus, dev, func); + + tmp =3D pci_info_xs_read(gc, pcibdf, "name"); + if (tmp && !strcmp(tmp, name)) + goto out; + } + + free(pcibdf); + pcibdf =3D NULL; + +out: + GC_FREE; + return pcibdf; +} + +char *libxl_device_pci_assignable_bdf2name(libxl_ctx *ctx, + libxl_pci_bdf *pcibdf) +{ + GC_INIT(ctx); + char *name =3D NULL, *tmp =3D pci_info_xs_read(gc, pcibdf, "name"); + + if (tmp) + name =3D strdup(tmp); + + GC_FREE; + return name; +} + /* * This function checks that all functions of a device are bound to pciback * driver. It also initialises a bit-mask of which function numbers are pr= esent @@ -1560,7 +1636,7 @@ void libxl__device_pci_add(libxl__egc *egc, uint32_t = domid, if (rc) goto out; =20 if (pci->seize && !pciback_dev_is_assigned(gc, &pci->bdf)) { - rc =3D libxl__device_pci_assignable_add(gc, &pci->bdf, 1); + rc =3D libxl__device_pci_assignable_add(gc, &pci->bdf, NULL, 1); if ( rc ) goto out; } diff --git a/tools/ocaml/libs/xl/xenlight_stubs.c b/tools/ocaml/libs/xl/xen= light_stubs.c index 2388f23869..96bb4655e0 100644 --- a/tools/ocaml/libs/xl/xenlight_stubs.c +++ b/tools/ocaml/libs/xl/xenlight_stubs.c @@ -840,7 +840,8 @@ value stub_xl_device_pci_assignable_add(value ctx, valu= e info, value rebind) device_pci_val(CTX, &c_info, info); =20 caml_enter_blocking_section(); - ret =3D libxl_device_pci_assignable_add(CTX, &c_info.bdf, c_rebind); + ret =3D libxl_device_pci_assignable_add(CTX, &c_info.bdf, NULL, + c_rebind); caml_leave_blocking_section(); =20 libxl_device_pci_dispose(&c_info); diff --git a/tools/xl/xl_cmdtable.c b/tools/xl/xl_cmdtable.c index 2ee0c49673..9e9aa448e2 100644 --- a/tools/xl/xl_cmdtable.c +++ b/tools/xl/xl_cmdtable.c @@ -105,21 +105,25 @@ struct cmd_spec cmd_table[] =3D { { "pci-assignable-add", &main_pciassignable_add, 0, 1, "Make a device assignable for pci-passthru", - "", + "[options] ", + "-n NAME, --name=3DNAME Name the assignable device.\n" "-h Print this help.\n" }, { "pci-assignable-remove", &main_pciassignable_remove, 0, 1, "Remove a device from being assignable", - "[options] ", + "[options] |NAME", "-h Print this help.\n" "-r Attempt to re-assign the device to the\n" - " original driver" + " original driver." }, { "pci-assignable-list", &main_pciassignable_list, 0, 0, "List all the assignable pci devices", - "", + "[options]", + "-h Print this help.\n" + "-n, --show-names Display assignable device names where\n" + " supplied.\n" }, { "pause", &main_pause, 0, 1, diff --git a/tools/xl/xl_pci.c b/tools/xl/xl_pci.c index 37708b4eb1..f1b58b3976 100644 --- a/tools/xl/xl_pci.c +++ b/tools/xl/xl_pci.c @@ -152,7 +152,7 @@ int main_pciattach(int argc, char **argv) return EXIT_SUCCESS; } =20 -static void pciassignable_list(void) +static void pciassignable_list(bool show_names) { libxl_pci_bdf *pcibdfs; int num, i; @@ -162,9 +162,15 @@ static void pciassignable_list(void) if ( pcibdfs =3D=3D NULL ) return; for (i =3D 0; i < num; i++) { - printf("%04x:%02x:%02x.%01x\n", - pcibdfs[i].domain, pcibdfs[i].bus, pcibdfs[i].dev, - pcibdfs[i].func); + libxl_pci_bdf *pcibdf =3D &pcibdfs[i]; + char *name =3D show_names ? + libxl_device_pci_assignable_bdf2name(ctx, pcibdf) : NULL; + + printf("%04x:%02x:%02x.%01x %s\n", + pcibdf->domain, pcibdf->bus, pcibdf->dev, pcibdf->func, + name ?: ""); + + free(name); } libxl_device_pci_assignable_list_free(pcibdfs, num); } @@ -172,16 +178,23 @@ static void pciassignable_list(void) int main_pciassignable_list(int argc, char **argv) { int opt; - - SWITCH_FOREACH_OPT(opt, "", NULL, "pci-assignable-list", 0) { - /* No options */ + static struct option opts[] =3D { + {"show-names", 0, 0, 'n'}, + COMMON_LONG_OPTS + }; + bool show_names =3D false; + + SWITCH_FOREACH_OPT(opt, "n", opts, "pci-assignable-list", 0) { + case 'n': + show_names =3D true; + break; } =20 - pciassignable_list(); + pciassignable_list(show_names); return 0; } =20 -static int pciassignable_add(const char *bdf, int rebind) +static int pciassignable_add(const char *bdf, const char *name, int rebind) { libxl_pci_bdf pcibdf; XLU_Config *config; @@ -197,7 +210,7 @@ static int pciassignable_add(const char *bdf, int rebin= d) exit(2); } =20 - if (libxl_device_pci_assignable_add(ctx, &pcibdf, rebind)) + if (libxl_device_pci_assignable_add(ctx, &pcibdf, name, rebind)) r =3D 1; =20 libxl_pci_bdf_dispose(&pcibdf); @@ -210,39 +223,58 @@ int main_pciassignable_add(int argc, char **argv) { int opt; const char *bdf =3D NULL; - - SWITCH_FOREACH_OPT(opt, "", NULL, "pci-assignable-add", 1) { - /* No options */ + static struct option opts[] =3D { + {"name", 1, 0, 'n'}, + COMMON_LONG_OPTS + }; + const char *name =3D NULL; + + SWITCH_FOREACH_OPT(opt, "n:", opts, "pci-assignable-add", 0) { + case 'n': + name =3D optarg; + break; } =20 bdf =3D argv[optind]; =20 - if (pciassignable_add(bdf, 1)) + if (pciassignable_add(bdf, name, 1)) return EXIT_FAILURE; =20 return EXIT_SUCCESS; } =20 -static int pciassignable_remove(const char *bdf, int rebind) +static int pciassignable_remove(const char *ident, int rebind) { - libxl_pci_bdf pcibdf; + libxl_pci_bdf *pcibdf; XLU_Config *config; int r =3D 0; =20 - libxl_pci_bdf_init(&pcibdf); - config =3D xlu_cfg_init(stderr, "command line"); if (!config) { perror("xlu_cfg_init"); exit(-1); } =20 - if (xlu_pci_parse_bdf(config, &pcibdf, bdf)) { - fprintf(stderr, "pci-assignable-remove: malformed BDF \"%s\"\n", b= df); - exit(2); + pcibdf =3D libxl_device_pci_assignable_name2bdf(ctx, ident); + if (!pcibdf) { + pcibdf =3D calloc(1, sizeof(*pcibdf)); + + if (!pcibdf) { + fprintf(stderr, + "pci-assignable-remove: failed to allocate memory\n"); + exit(2); + } + + libxl_pci_bdf_init(pcibdf); + if (xlu_pci_parse_bdf(config, pcibdf, ident)) { + fprintf(stderr, + "pci-assignable-remove: malformed BDF '%s'\n", ident); + exit(2); + } } =20 - if (libxl_device_pci_assignable_remove(ctx, &pcibdf, rebind)) + if (libxl_device_pci_assignable_remove(ctx, pcibdf, rebind)) r =3D 1; =20 - libxl_pci_bdf_dispose(&pcibdf); + libxl_pci_bdf_dispose(pcibdf); + free(pcibdf); xlu_cfg_destroy(config); =20 return r; @@ -251,7 +283,7 @@ static int pciassignable_remove(const char *bdf, int re= bind) int main_pciassignable_remove(int argc, char **argv) { int opt; - const char *bdf =3D NULL; + const char *ident =3D NULL; int rebind =3D 0; =20 SWITCH_FOREACH_OPT(opt, "r", NULL, "pci-assignable-remove", 1) { @@ -260,9 +292,9 @@ int main_pciassignable_remove(int argc, char **argv) break; } =20 - bdf =3D argv[optind]; + ident =3D argv[optind]; =20 - if (pciassignable_remove(bdf, rebind)) + if (pciassignable_remove(ident, rebind)) return EXIT_FAILURE; =20 return EXIT_SUCCESS; --=20 2.11.0