Skip to contentSkip to conversation

Add per-device and global permissive config options for pci passthrough

PATCH Applied George Dunlap 2 participants 7 messages Apr 3, 2012 – Apr 4, 2012 Arm/actions semi architecture

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Add per-device and global permissive config options for pci passthrough

This patch series adds the "permissive" option for passed-through devices which access config space out of the "known good" PCI config space.

v2:
- Added patch to move bdf parsing to libxlu
- Addressed some formatting comments

v3:
- Don't make the struct initialization utility function public
- Return error on failure
- Wrap a long line
- Clarify security warning / recommendation

#1 lore raw

PATCH 1/1 libxl: Move bdf parsing into libxlu

Config parsing functions do not properly belong in libxl. Move them into libxlu so that others can use them or not as they see fit.

No functional changes. One side-effect was making public a private libxl utility function which just set the elements of a structure from the function arguments passed in.

v2:
- Didn't make libxl_pci_dev_init() a public function; renamed to be more descriptive

tools/libxl/Makefile +1 -1
diff -r f744e82ea740 -r 74bb52e4f6a6 tools/libxl/Makefile
@@ -57,7 +57,7 @@ LIBXL_OBJS += _libxl_types.o libxl_flask
 AUTOINCS= libxlu_cfg_y.h libxlu_cfg_l.h _libxl_list.h
 AUTOSRCS= libxlu_cfg_y.c libxlu_cfg_l.c
 LIBXLU_OBJS = libxlu_cfg_y.o libxlu_cfg_l.o libxlu_cfg.o \
-	libxlu_disk_l.o libxlu_disk.o
+	libxlu_disk_l.o libxlu_disk.o libxlu_pci.o
 $(LIBXLU_OBJS): CFLAGS += $(CFLAGS_libxenctrl) # For xentoollog.h
 
 CLIENTS = xl testidl
tools/libxl/libxl.h +0 -7
diff -r f744e82ea740 -r 74bb52e4f6a6 tools/libxl/libxl.h
@@ -575,13 +575,6 @@ int libxl_device_pci_destroy(libxl_ctx *
 libxl_device_pci *libxl_device_pci_list(libxl_ctx *ctx, uint32_t domid, int *num);
 
 /*
- * Parse a PCI BDF into a PCI device structure.
- */
-int libxl_device_pci_parse_bdf(libxl_ctx *ctx,
-                               libxl_device_pci *pcidev,
-                               const char *str);
-
-/*
  * Similar to libxl_device_pci_list but returns all devices which
  * could be assigned to a domain (i.e. are bound to the backend
  * driver) but are not currently.
tools/libxl/libxl_pci.c +6 -149

185 lines not on this page · raw message

tools/libxl/libxlu_pci.c new file +172 -0

172 lines not on this page · raw message

tools/libxl/libxlutil.h +5 -0
diff -r f744e82ea740 -r 74bb52e4f6a6 tools/libxl/libxlutil.h
@@ -88,6 +88,11 @@ int xlu_disk_parse(XLU_Config *cfg, int 
    * resulting disk struct is used with libxl.
    */
 
+/*
+ * PCI specification parsing
+ */
+int xlu_pci_parse_bdf(XLU_Config *cfg, libxl_device_pci *pcidev, const char *str);
+
 
 #endif /* LIBXLUTIL_H */
 
tools/libxl/xl_cmdimpl.c +13 -3
diff -r f744e82ea740 -r 74bb52e4f6a6 tools/libxl/xl_cmdimpl.c
@@ -1005,7 +1005,7 @@ skip_vfb:
 
             pcidev->msitranslate = pci_msitranslate;
             pcidev->power_mgmt = pci_power_mgmt;
-            if (!libxl_device_pci_parse_bdf(ctx, pcidev, buf))
+            if (!xlu_pci_parse_bdf(config, pcidev, buf))
                 d_config->num_pcidevs++;
         }
         if (d_config->num_pcidevs && c_info->type == LIBXL_DOMAIN_TYPE_PV)
@@ -2217,11 +2217,16 @@ int main_pcilist(int argc, char **argv)
 static void pcidetach(const char *dom, const char *bdf, int force)
 {
     libxl_device_pci pcidev;
+    XLU_Config *config;
 
     find_domain(dom);
 
     memset(&pcidev, 0x00, sizeof(pcidev));
-    if (libxl_device_pci_parse_bdf(ctx, &pcidev, bdf)) {
+    
+    config = xlu_cfg_init(stderr, "command line");
+    if (!config) { perror("xlu_cfg_inig"); exit(-1); }
+
+    if (xlu_pci_parse_bdf(config, &pcidev, bdf)) {
         fprintf(stderr, "pci-detach: malformed BDF specification \"%s\"\n", bdf);
         exit(2);
     }
@@ -2257,11 +2262,16 @@ int main_pcidetach(int argc, char **argv
 static void pciattach(const char *dom, const char *bdf, const char *vs)
 {
     libxl_device_pci pcidev;
+    XLU_Config *config;
 
     find_domain(dom);
 
     memset(&pcidev, 0x00, sizeof(pcidev));
-    if (libxl_device_pci_parse_bdf(ctx, &pcidev, bdf)) {
+
+    config = xlu_cfg_init(stderr, "command line");
+    if (!config) { perror("xlu_cfg_inig"); exit(-1); }
+
+    if (xlu_pci_parse_bdf(config, &pcidev, bdf)) {
         fprintf(stderr, "pci-attach: malformed BDF specification \"%s\"\n", bdf);
         exit(2);
     }
tools/python/xen/lowlevel/xl/xl.c +2 -1
diff -r f744e82ea740 -r 74bb52e4f6a6 tools/python/xen/lowlevel/xl/xl.c
@@ -40,6 +40,7 @@
 
 #include <libxl.h>
 #include <libxl_utils.h>
+#include <libxlutil.h>
 
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
 
@@ -556,7 +557,7 @@ static PyObject *pyxl_pci_parse(XlObject
         return NULL;
     }
 
-    if ( libxl_device_pci_parse_bdf(self->ctx, &pci->obj, str) ) {
+    if ( xlu_pci_parse_bdf(NULL, &pci->obj, str) ) {
         PyErr_SetString(xl_error_obj, "cannot parse pci device spec (BDF)");
         Py_DECREF(pci);
         return NULL;
Acked-by on v1 1/1
#2 lore raw

Re: [PATCH 1 of 2] libxl: Move bdf parsing into libxlu

George Dunlap writes ("[Xen-devel] [PATCH 1 of 2] libxl: Move bdf parsing into libxlu"):

Quoted 2 lines

Config parsing functions do not properly belong in libxl. Move them into libxlu so that others can use them or not as they see fit.

#3 lore raw

PATCH 1/1 xl, libxl: Add per-device and global permissive config options for pci passthrough

In reply to George Dunlap

By default pciback only allows PV guests to write "known safe" values into PCI config space. But many devices require writes to other areas of config space in order to operate properly. One way to do that is with the "quirks" interface, which specifies areas known safe to a particular device; the other way is to mark a device as "permissive", which tells pciback to allow all config space writes for that domain and device.

This adds a "permissive" flag to the libxl_pci struct and teaches libxl how to write the appropriate value into sysfs to enable the permissive feature for devices being passed through. It also adds the permissive config options either on a per-device basis, or as a global option in the xl command-line.

Because of the potential stability and security implications of enabling permissive, the flag is left off by default.

v3:
- Return error on failure
- Wrap a long line
- Clarify security warning / recommendation

docs/man/xl.cfg.pod.5 +21 -0
diff -r 74bb52e4f6a6 -r 0625fe50a2ee docs/man/xl.cfg.pod.5
@@ -294,10 +294,31 @@ XXX
 
 XXX
 
+=item B<permissive=BOOLEAN>
+
+(PV only) By default pciback only allows PV guests to write "known
+safe" values into PCI config space.  But many devices require writes
+to other areas of config space in order to operate properly.  This
+tells the pciback driver to allow all writes to PCI config space of
+this device by this domain.  This option should be enabled with
+caution: it gives the guest much more control over the device, which
+may have security or stability implications.  It is recommended to
+enable this option only for trusted VMs under administrator control.
+
 =back
 
 =back
 
+=item B<pci_permissive=BOOLEAN>
+
+(PV only) Changes the default value of 'permissive' for all PCI
+devices for this VM.  This can still be overriden on a per-device
+basis. This option should be enabled with caution: it gives the guest
+much more control over the device, which may have security or
+stability implications.  It is recommended to enable this option only
+for trusted VMs under administrator control.  See the "pci=" section
+for more information on the "permissive" flag.
+
 =back
 
 =head2 Paravirtualised (PV) Guest Specific Options
tools/libxl/libxl_pci.c +32 -1
diff -r 74bb52e4f6a6 -r 0625fe50a2ee tools/libxl/libxl_pci.c
@@ -55,7 +55,10 @@ static void libxl_create_pci_backend_dev
     if (pcidev->vdevfn)
         flexarray_append_pair(back, libxl__sprintf(gc, "vdevfn-%d", num), libxl__sprintf(gc, "%x", pcidev->vdevfn));
     flexarray_append(back, libxl__sprintf(gc, "opts-%d", num));
-    flexarray_append(back, libxl__sprintf(gc, "msitranslate=%d,power_mgmt=%d", pcidev->msitranslate, pcidev->power_mgmt));
+    flexarray_append(back,
+              libxl__sprintf(gc, "msitranslate=%d,power_mgmt=%d,permissive=%d",
+                             pcidev->msitranslate, pcidev->power_mgmt,
+                             pcidev->permissive));
     flexarray_append_pair(back, libxl__sprintf(gc, "state-%d", num), libxl__sprintf(gc, "%d", 1));
 }
 
@@ -565,6 +568,31 @@ static int do_pci_add(libxl__gc *gc, uin
             }
         }
         fclose(f);
+
+        /* Don't restrict writes to the PCI config space from this VM */
+        if (pcidev->permissive) {
+            int fd;
+            char *buf;
+            
+            sysfs_path = libxl__sprintf(gc, SYSFS_PCIBACK_DRIVER"/permissive");
+            fd = open(sysfs_path, O_WRONLY);
+            if (fd < 0) {
+                LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "Couldn't open %s",
+                                 sysfs_path);
+                return ERROR_FAIL;
+            }
+ 
+            buf = libxl__sprintf(gc, PCI_BDF, pcidev->domain, pcidev->bus,
+                                 pcidev->dev, pcidev->func);
+            rc = write(fd, buf, strlen(buf));
+            /* Annoying to have two if's, but we need the errno */
+            if (rc < 0)
+                LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
+                                 "write to %s returned %d", sysfs_path, rc);
+            close(fd);
+            if (rc < 0)
+                return ERROR_FAIL;
+        }
         break;
     }
     default:
@@ -958,6 +986,9 @@ static void libxl__device_pci_from_xs_be
             } else if (!strcmp(p, "power_mgmt")) {
                 p = strtok_r(NULL, ",=", &saveptr);
                 pci->power_mgmt = atoi(p);
+            } else if (!strcmp(p, "permissive")) {
+                p = strtok_r(NULL, ",=", &saveptr);
+                pci->permissive = atoi(p);
             }
         } while ((p = strtok_r(NULL, ",=", &saveptr)) != NULL);
     }
tools/libxl/libxl_types.idl +1 -0
diff -r 74bb52e4f6a6 -r 0625fe50a2ee tools/libxl/libxl_types.idl
@@ -352,6 +352,7 @@ libxl_device_pci = Struct("device_pci", 
     ("vfunc_mask", uint32),
     ("msitranslate", bool),
     ("power_mgmt", bool),
+    ("permissive", bool),
     ])
 
 libxl_diskinfo = Struct("diskinfo", [
tools/libxl/libxlu_pci.c +2 -0
diff -r 74bb52e4f6a6 -r 0625fe50a2ee tools/libxl/libxlu_pci.c
@@ -139,6 +139,8 @@ int xlu_pci_parse_bdf(XLU_Config *cfg, l
                     pcidev->msitranslate = atoi(tok);
                 }else if ( !strcmp(optkey, "power_mgmt") ) {
                     pcidev->power_mgmt = atoi(tok);
+                }else if ( !strcmp(optkey, "permissive") ) {
+                    pcidev->permissive = atoi(tok);
                 }else{
                     XLU__PCI_ERR(cfg, "Unknown PCI BDF option: %s", optkey);
                 }
tools/libxl/xl_cmdimpl.c +5 -0
diff -r 74bb52e4f6a6 -r 0625fe50a2ee tools/libxl/xl_cmdimpl.c
@@ -518,6 +518,7 @@ static void parse_config_data(const char
     XLU_ConfigList *cpus, *vbds, *nics, *pcis, *cvfbs, *cpuids;
     int pci_power_mgmt = 0;
     int pci_msitranslate = 1;
+    int pci_permissive = 0;
     int e;
 
     libxl_domain_create_info *c_info = &d_config->c_info;
@@ -986,6 +987,9 @@ skip_vfb:
     if (!xlu_cfg_get_long (config, "pci_power_mgmt", &l, 0))
         pci_power_mgmt = l;
 
+    if (!xlu_cfg_get_long (config, "pci_permissive", &l, 0))
+        pci_permissive = l;
+
     /* To be reworked (automatically enabled) once the auto ballooning
      * after guest starts is done (with PCI devices passed in). */
     if (c_info->type == LIBXL_DOMAIN_TYPE_PV) {
@@ -1005,6 +1009,7 @@ skip_vfb:
 
             pcidev->msitranslate = pci_msitranslate;
             pcidev->power_mgmt = pci_power_mgmt;
+            pcidev->permissive = pci_permissive;
             if (!xlu_pci_parse_bdf(config, pcidev, buf))
                 d_config->num_pcidevs++;
         }
Acked-by on v1 1/1
#4 lore raw

Re: [PATCH 2 of 2] xl, libxl: Add per-device and global permissive config options for pci passthrough

George Dunlap writes ("[Xen-devel] [PATCH 2 of 2] xl, libxl: Add per-device and global permissive config options for pci passthrough"):

Quoted 6 lines

By default pciback only allows PV guests to write "known safe" values into PCI config space. But many devices require writes to other areas of config space in order to operate properly. One way to do that is with the "quirks" interface, which specifies areas known safe to a particular device; the other way is to mark a device as "permissive", which tells pciback to allow all config space writes for that domain and device.

Ian.

#5 lore raw
In reply to George Dunlap

BTW, that should be "[v3]"...

Quoted 17 lines · George Dunlap

This patch series adds the "permissive" option for passed-through devices which access config space out of the "known good" PCI config space.

v2:
- Added patch to move bdf parsing to libxlu
- Addressed some formatting comments

v3:
- Don't make the struct initialization utility function public
- Return error on failure
- Wrap a long line
- Clarify security warning / recommendation

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

Applied
#6 lore raw
In reply to George Dunlap

George Dunlap writes ("[Xen-devel] [PATCH 0 of 2] [v2] Add per-device and global permissive config options for pci passthrough"):

Quoted 2 lines

This patch series adds the "permissive" option for passed-through devices which access config space out of the "known good" PCI config space.

Applied both, thanks.

Ian.