All of lore.kernel.org
 help / color / mirror / Atom feed
From: Paul Durrant <paul@xen.org>
To: xen-devel@lists.xenproject.org
Cc: Paul Durrant <pdurrant@amazon.com>,
	Ian Jackson <iwj@xenproject.org>, Wei Liu <wl@xen.org>,
	Anthony PERARD <anthony.perard@citrix.com>
Subject: [PATCH v6 23/25] xl: support naming of assignable devices
Date: Tue,  8 Dec 2020 19:30:31 +0000	[thread overview]
Message-ID: <20201208193033.11306-24-paul@xen.org> (raw)
In-Reply-To: <20201208193033.11306-1-paul@xen.org>

From: Paul Durrant <pdurrant@amazon.com>

This patch converts libxl to use libxl_pci_bdf_assignable_add/remove/list/
list_free() rather than libxl_device_pci_assignable_add/remove/list/
list_free(), which then allows naming of assignable devices to be supported.

With this patch applied 'xl pci-assignable-add' will take an optional '--name'
parameter, 'xl pci-assignable-remove' can be passed either a BDF or a name and
'xl pci-assignable-list' will take a optional '--show-names' flag which
determines whether names are displayed in its output.

Signed-off-by: Paul Durrant <pdurrant@amazon.com>
---
Cc: Ian Jackson <iwj@xenproject.org>
Cc: Wei Liu <wl@xen.org>
Cc: Anthony PERARD <anthony.perard@citrix.com>

v6:
 - New in v6 (split out from "xl / libxl: support naming of assignable
   devices")
---
 tools/xl/xl_cmdtable.c |  12 +++--
 tools/xl/xl_pci.c      | 100 +++++++++++++++++++++++++++--------------
 2 files changed, 74 insertions(+), 38 deletions(-)

diff --git a/tools/xl/xl_cmdtable.c b/tools/xl/xl_cmdtable.c
index 30e17a2848cd..bd8af12ff36e 100644
--- a/tools/xl/xl_cmdtable.c
+++ b/tools/xl/xl_cmdtable.c
@@ -105,21 +105,25 @@ struct cmd_spec cmd_table[] = {
     { "pci-assignable-add",
       &main_pciassignable_add, 0, 1,
       "Make a device assignable for pci-passthru",
-      "<BDF>",
+      "[options] <BDF>",
+      "-n NAME, --name=NAME    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] <BDF>",
+      "[options] <BDF>|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 9c24496cb2dd..eb29b4e08d8b 100644
--- a/tools/xl/xl_pci.c
+++ b/tools/xl/xl_pci.c
@@ -152,55 +152,68 @@ int main_pciattach(int argc, char **argv)
     return EXIT_SUCCESS;
 }
 
-static void pciassignable_list(void)
+static void pciassignable_list(bool show_names)
 {
-    libxl_device_pci *pcis;
+    libxl_pci_bdf *pcibdfs;
     int num, i;
 
-    pcis = libxl_device_pci_assignable_list(ctx, &num);
+    pcibdfs = libxl_pci_bdf_assignable_list(ctx, &num);
 
-    if ( pcis == NULL )
+    if ( pcibdfs == NULL )
         return;
     for (i = 0; i < num; i++) {
-        printf("%04x:%02x:%02x.%01x\n",
-               pcis[i].bdf.domain, pcis[i].bdf.bus, pcis[i].bdf.dev,
-               pcis[i].bdf.func);
+        libxl_pci_bdf *pcibdf = &pcibdfs[i];
+        char *name = show_names ?
+            libxl_pci_bdf_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(pcis, num);
+    libxl_pci_bdf_assignable_list_free(pcibdfs, num);
 }
 
 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[] = {
+        {"show-names", 0, 0, 'n'},
+        COMMON_LONG_OPTS
+    };
+    bool show_names = false;
+
+    SWITCH_FOREACH_OPT(opt, "n", opts, "pci-assignable-list", 0) {
+    case 'n':
+        show_names = true;
+        break;
     }
 
-    pciassignable_list();
+    pciassignable_list(show_names);
     return 0;
 }
 
-static int pciassignable_add(const char *bdf, int rebind)
+static int pciassignable_add(const char *bdf, const char *name, int rebind)
 {
-    libxl_device_pci pci;
+    libxl_pci_bdf pcibdf;
     XLU_Config *config;
     int r = 0;
 
-    libxl_device_pci_init(&pci);
+    libxl_pci_bdf_init(&pcibdf);
 
     config = xlu_cfg_init(stderr, "command line");
     if (!config) { perror("xlu_cfg_init"); exit(-1); }
 
-    if (xlu_pci_parse_bdf(config, &pci.bdf, bdf)) {
+    if (xlu_pci_parse_bdf(config, &pcibdf, bdf)) {
         fprintf(stderr, "pci-assignable-add: malformed BDF \"%s\"\n", bdf);
         exit(2);
     }
 
-    if (libxl_device_pci_assignable_add(ctx, &pci, rebind))
+    if (libxl_pci_bdf_assignable_add(ctx, &pcibdf, name, rebind))
         r = 1;
 
-    libxl_device_pci_dispose(&pci);
+    libxl_pci_bdf_dispose(&pcibdf);
     xlu_cfg_destroy(config);
 
     return r;
@@ -210,39 +223,58 @@ int main_pciassignable_add(int argc, char **argv)
 {
     int opt;
     const char *bdf = NULL;
-
-    SWITCH_FOREACH_OPT(opt, "", NULL, "pci-assignable-add", 1) {
-        /* No options */
+    static struct option opts[] = {
+        {"name", 1, 0, 'n'},
+        COMMON_LONG_OPTS
+    };
+    const char *name = NULL;
+
+    SWITCH_FOREACH_OPT(opt, "n:", opts, "pci-assignable-add", 0) {
+    case 'n':
+        name = optarg;
+        break;
     }
 
     bdf = argv[optind];
 
-    if (pciassignable_add(bdf, 1))
+    if (pciassignable_add(bdf, name, 1))
         return EXIT_FAILURE;
 
     return EXIT_SUCCESS;
 }
 
-static int pciassignable_remove(const char *bdf, int rebind)
+static int pciassignable_remove(const char *ident, int rebind)
 {
-    libxl_device_pci pci;
+    libxl_pci_bdf *pcibdf;
     XLU_Config *config;
     int r = 0;
 
-    libxl_device_pci_init(&pci);
-
     config = xlu_cfg_init(stderr, "command line");
     if (!config) { perror("xlu_cfg_init"); exit(-1); }
 
-    if (xlu_pci_parse_bdf(config, &pci.bdf, bdf)) {
-        fprintf(stderr, "pci-assignable-remove: malformed BDF \"%s\"\n", bdf);
-        exit(2);
+    pcibdf = libxl_pci_bdf_assignable_name2bdf(ctx, ident);
+    if (!pcibdf) {
+        pcibdf = 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);
+        }
     }
 
-    if (libxl_device_pci_assignable_remove(ctx, &pci, rebind))
+    if (libxl_pci_bdf_assignable_remove(ctx, pcibdf, rebind))
         r = 1;
 
-    libxl_device_pci_dispose(&pci);
+    libxl_pci_bdf_dispose(pcibdf);
+    free(pcibdf);
     xlu_cfg_destroy(config);
 
     return r;
@@ -251,7 +283,7 @@ static int pciassignable_remove(const char *bdf, int rebind)
 int main_pciassignable_remove(int argc, char **argv)
 {
     int opt;
-    const char *bdf = NULL;
+    const char *ident = NULL;
     int rebind = 0;
 
     SWITCH_FOREACH_OPT(opt, "r", NULL, "pci-assignable-remove", 1) {
@@ -260,9 +292,9 @@ int main_pciassignable_remove(int argc, char **argv)
         break;
     }
 
-    bdf = argv[optind];
+    ident = argv[optind];
 
-    if (pciassignable_remove(bdf, rebind))
+    if (pciassignable_remove(ident, rebind))
         return EXIT_FAILURE;
 
     return EXIT_SUCCESS;
-- 
2.20.1



  parent reply	other threads:[~2020-12-08 20:01 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-08 19:30 [PATCH v6 00/25] xl / libxl: named PCI pass-through devices Paul Durrant
2020-12-08 19:30 ` [PATCH v6 01/25] libxl: s/pcidev/pci and remove DEFINE_DEVICE_TYPE_STRUCT_X Paul Durrant
2020-12-15 15:59   ` Wei Liu
2020-12-08 19:30 ` [PATCH v6 02/25] xl: s/pcidev/pci where possible Paul Durrant
2020-12-15 16:00   ` Wei Liu
2020-12-08 19:30 ` [PATCH v6 03/25] libxl: make libxl__device_list() work correctly for LIBXL__DEVICE_KIND_PCI Paul Durrant
2020-12-08 19:30 ` [PATCH v6 04/25] libxl: Make sure devices added by pci-attach are reflected in the config Paul Durrant
2020-12-08 19:30 ` [PATCH v6 05/25] libxl: add/recover 'rdm_policy' to/from PCI backend in xenstore Paul Durrant
2020-12-08 19:30 ` [PATCH v6 06/25] libxl: s/detatched/detached in libxl_pci.c Paul Durrant
2020-12-08 19:30 ` [PATCH v6 07/25] libxl: remove extraneous arguments to do_pci_remove() " Paul Durrant
2020-12-08 19:30 ` [PATCH v6 08/25] libxl: stop using aodev->device_config in libxl__device_pci_add() Paul Durrant
2020-12-08 19:30 ` [PATCH v6 09/25] libxl: generalise 'driver_path' xenstore access functions in libxl_pci.c Paul Durrant
2020-12-08 19:30 ` [PATCH v6 10/25] libxl: remove unnecessary check from libxl__device_pci_add() Paul Durrant
2020-12-08 19:30 ` [PATCH v6 11/25] libxl: remove get_all_assigned_devices() from libxl_pci.c Paul Durrant
2020-12-08 19:30 ` [PATCH v6 12/25] libxl: make sure callers of libxl_device_pci_list() free the list after use Paul Durrant
2020-12-08 19:30 ` [PATCH v6 13/25] libxl: add libxl_device_pci_assignable_list_free() Paul Durrant
2020-12-08 19:30 ` [PATCH v6 14/25] libxl: use COMPARE_PCI() macro is_pci_in_array() Paul Durrant
2020-12-08 19:30 ` [PATCH v6 15/25] docs/man: extract documentation of PCI_SPEC_STRING from the xl.cfg manpage Paul Durrant
2020-12-08 19:30 ` [PATCH v6 16/25] docs/man: improve documentation of PCI_SPEC_STRING Paul Durrant
2020-12-08 19:30 ` [PATCH v6 17/25] docs/man: fix xl(1) documentation for 'pci' operations Paul Durrant
2020-12-08 19:30 ` [PATCH v6 18/25] libxl: introduce 'libxl_pci_bdf' in the idl Paul Durrant
2020-12-08 19:30 ` [PATCH v6 19/25] libxlu: introduce xlu_pci_parse_spec_string() Paul Durrant
2020-12-08 19:30 ` [PATCH v6 20/25] docs/man: modify xl(1) in preparation for naming of assignable devices Paul Durrant
2020-12-08 19:30 ` [PATCH v6 21/25] libxl: convert internal functions in libxl_pci.c Paul Durrant
2020-12-15 16:02   ` Wei Liu
2020-12-08 19:30 ` [PATCH v6 22/25] libxl: introduce libxl_pci_bdf_assignable_add/remove/list/list_free(), Paul Durrant
2020-12-15 16:03   ` Wei Liu
2020-12-08 19:30 ` Paul Durrant [this message]
2020-12-15 16:03   ` [PATCH v6 23/25] xl: support naming of assignable devices Wei Liu
2020-12-08 19:30 ` [PATCH v6 24/25] docs/man: modify xl-pci-configuration(5) to add 'name' field to PCI_SPEC_STRING Paul Durrant
2020-12-08 19:30 ` [PATCH v6 25/25] libxl / libxlu: support 'xl pci-attach/detach' by name Paul Durrant

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201208193033.11306-24-paul@xen.org \
    --to=paul@xen.org \
    --cc=anthony.perard@citrix.com \
    --cc=iwj@xenproject.org \
    --cc=pdurrant@amazon.com \
    --cc=wl@xen.org \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.