debuggers.hg

changeset 21938:9ced33b68bd8

libxl: Check whether a PCI device is assignable before assigning it do a domU

Implement a new libxl function libxl_device_pci_list_assignable. This is
used to implement the xl list-assignable-pci-devices command and part of
the implementation is used to make sure that PCI devices are not multiply
assigned to one or more domU's before doing the passthrough assignment.

The function libxl_device_pci_list changes to libxl_device_pci_list_assigned
due to a parameter change for consistency with pci_list_assignable.

Signed-off-by: Gianni Tedesco <gianni.tedesco@citrix.com>
Acked-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
Committed-by: Ian Jackson <ian.jackson@eu.citrix.com>
author Gianni Tedesco <gianni.tedesco@citrix.com>
date Thu Jul 29 18:52:33 2010 +0100 (2010-07-29)
parents 1a92c8881770
children 754877be695b
files tools/libxl/libxl.h tools/libxl/libxl_internal.h tools/libxl/libxl_pci.c tools/libxl/xl.h tools/libxl/xl_cmdimpl.c tools/libxl/xl_cmdtable.c
line diff
     1.1 --- a/tools/libxl/libxl.h	Thu Jul 29 18:49:47 2010 +0100
     1.2 +++ b/tools/libxl/libxl.h	Thu Jul 29 18:52:33 2010 +0100
     1.3 @@ -521,7 +521,8 @@ int libxl_device_vfb_hard_shutdown(libxl
     1.4  int libxl_device_pci_add(libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev);
     1.5  int libxl_device_pci_remove(libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev);
     1.6  int libxl_device_pci_shutdown(libxl_ctx *ctx, uint32_t domid);
     1.7 -libxl_device_pci *libxl_device_pci_list(libxl_ctx *ctx, uint32_t domid, int *num);
     1.8 +int libxl_device_pci_list_assigned(libxl_ctx *ctx, libxl_device_pci **list, uint32_t domid, int *num);
     1.9 +int libxl_device_pci_list_assignable(libxl_ctx *ctx, libxl_device_pci **list, int *num);
    1.10  int libxl_device_pci_init(libxl_device_pci *pcidev, unsigned int domain,
    1.11                            unsigned int bus, unsigned int dev,
    1.12                            unsigned int func, unsigned int vdevfn);
     2.1 --- a/tools/libxl/libxl_internal.h	Thu Jul 29 18:49:47 2010 +0100
     2.2 +++ b/tools/libxl/libxl_internal.h	Thu Jul 29 18:52:33 2010 +0100
     2.3 @@ -91,6 +91,8 @@ typedef struct {
     2.4  #define XC_PCI_BDF             "0x%x, 0x%x, 0x%x, 0x%x"
     2.5  #define AUTO_PHP_SLOT          0x100
     2.6  #define SYSFS_PCI_DEV          "/sys/bus/pci/devices"
     2.7 +#define SYSFS_PCIBACK_DRIVER   "/sys/bus/pci/drivers/pciback"
     2.8 +
     2.9  #define PROC_PCI_NUM_RESOURCES 7
    2.10  #define PCI_BAR_IO             0x01
    2.11  
     3.1 --- a/tools/libxl/libxl_pci.c	Thu Jul 29 18:49:47 2010 +0100
     3.2 +++ b/tools/libxl/libxl_pci.c	Thu Jul 29 18:52:33 2010 +0100
     3.3 @@ -28,6 +28,7 @@
     3.4  #include <unistd.h> /* for write, unlink and close */
     3.5  #include <stdint.h>
     3.6  #include <inttypes.h>
     3.7 +#include <dirent.h>
     3.8  #include <assert.h>
     3.9  
    3.10  #include "libxl.h"
    3.11 @@ -258,22 +259,77 @@ retry_transaction2:
    3.12      return 0;
    3.13  }
    3.14  
    3.15 -int libxl_device_pci_add(libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
    3.16 +static int get_all_assigned_devices(libxl_ctx *ctx, libxl_device_pci **list, int *num)
    3.17 +{
    3.18 +    libxl_device_pci *pcidevs = NULL;
    3.19 +    char **domlist;
    3.20 +    unsigned int nd = 0, i;
    3.21 +
    3.22 +    *list = NULL;
    3.23 +    *num = 0;
    3.24 +
    3.25 +    domlist = libxl_xs_directory(ctx, XBT_NULL, "/local/domain", &nd);
    3.26 +    for(i = 0; i < nd; i++) {
    3.27 +        char *path, *num_devs;
    3.28 +
    3.29 +        path = libxl_sprintf(ctx, "/local/domain/0/backend/pci/%s/0/num_devs", domlist[i]);
    3.30 +        num_devs = libxl_xs_read(ctx, XBT_NULL, path);
    3.31 +        if ( num_devs ) {
    3.32 +            int ndev = atoi(num_devs), j;
    3.33 +            char *devpath, *bdf;
    3.34 +
    3.35 +            pcidevs = calloc(sizeof(*pcidevs), ndev);
    3.36 +            for(j = (pcidevs) ? 0 : ndev; j < ndev; j++) {
    3.37 +                devpath = libxl_sprintf(ctx, "/local/domain/0/backend/pci/%s/0/dev-%u",
    3.38 +                                        domlist[i], j);
    3.39 +                bdf = libxl_xs_read(ctx, XBT_NULL, devpath);
    3.40 +                if ( bdf ) {
    3.41 +                    unsigned dom, bus, dev, func;
    3.42 +                    if ( sscanf(bdf, PCI_BDF, &dom, &bus, &dev, &func) != 4 )
    3.43 +                        continue;
    3.44 +
    3.45 +                    libxl_device_pci_init(pcidevs + *num, dom, bus, dev, func, 0);
    3.46 +                    (*num)++;
    3.47 +                }
    3.48 +            }
    3.49 +        }
    3.50 +    }
    3.51 +
    3.52 +    if ( 0 == *num ) {
    3.53 +        free(pcidevs);
    3.54 +        pcidevs = NULL;
    3.55 +    }else{
    3.56 +        *list = pcidevs;
    3.57 +    }
    3.58 +
    3.59 +    return 0;
    3.60 +}
    3.61 +
    3.62 +static int is_assigned(libxl_device_pci *assigned, int num_assigned,
    3.63 +                       int dom, int bus, int dev, int func)
    3.64 +{
    3.65 +    int i;
    3.66 +
    3.67 +    for(i = 0; i < num_assigned; i++) {
    3.68 +        if ( assigned[i].domain != dom )
    3.69 +            continue;
    3.70 +        if ( assigned[i].bus != bus )
    3.71 +            continue;
    3.72 +        if ( assigned[i].dev != dev )
    3.73 +            continue;
    3.74 +        if ( assigned[i].func != func )
    3.75 +            continue;
    3.76 +        return 1;
    3.77 +    }
    3.78 +
    3.79 +    return 0;
    3.80 +}
    3.81 +
    3.82 +static int do_pci_add(libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
    3.83  {
    3.84      char *path;
    3.85      char *state, *vdevfn;
    3.86      int rc, hvm;
    3.87 -    int stubdomid = 0;
    3.88 -
    3.89 -    /* TODO: check if the device can be assigned */
    3.90 -
    3.91 -    libxl_device_pci_reset(ctx, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
    3.92 -
    3.93 -    stubdomid = libxl_get_stubdom_id(ctx, domid);
    3.94 -    if (stubdomid != 0) {
    3.95 -        libxl_device_pci pcidev_s = *pcidev;
    3.96 -        libxl_device_pci_add(ctx, stubdomid, &pcidev_s);
    3.97 -    }
    3.98  
    3.99      hvm = is_hvm(ctx, domid);
   3.100      if (hvm) {
   3.101 @@ -370,6 +426,38 @@ out:
   3.102      return 0;
   3.103  }
   3.104  
   3.105 +int libxl_device_pci_add(libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
   3.106 +{
   3.107 +    libxl_device_pci *assigned;
   3.108 +    int num_assigned, rc;
   3.109 +    int stubdomid = 0;
   3.110 +
   3.111 +    rc = get_all_assigned_devices(ctx, &assigned, &num_assigned);
   3.112 +    if ( rc ) {
   3.113 +        XL_LOG(ctx, XL_LOG_ERROR, "cannot determine if device is assigned, refusing to continue");
   3.114 +        return ERROR_FAIL;
   3.115 +    }
   3.116 +    if ( is_assigned(assigned, num_assigned, pcidev->domain,
   3.117 +                     pcidev->bus, pcidev->dev, pcidev->func) ) {
   3.118 +        XL_LOG(ctx, XL_LOG_ERROR, "PCI device already attached to a domain");
   3.119 +        free(assigned);
   3.120 +        return ERROR_FAIL;
   3.121 +    }
   3.122 +    free(assigned);
   3.123 +
   3.124 +    libxl_device_pci_reset(ctx, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
   3.125 +
   3.126 +    stubdomid = libxl_get_stubdom_id(ctx, domid);
   3.127 +    if (stubdomid != 0) {
   3.128 +        libxl_device_pci pcidev_s = *pcidev;
   3.129 +        rc = do_pci_add(ctx, stubdomid, &pcidev_s);
   3.130 +        if ( rc )
   3.131 +            return rc;
   3.132 +    }
   3.133 +
   3.134 +    return do_pci_add(ctx, domid, pcidev);
   3.135 +}
   3.136 +
   3.137  int libxl_device_pci_remove(libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
   3.138  {
   3.139      char *path;
   3.140 @@ -466,7 +554,64 @@ out:
   3.141      return 0;
   3.142  }
   3.143  
   3.144 -libxl_device_pci *libxl_device_pci_list(libxl_ctx *ctx, uint32_t domid, int *num)
   3.145 +static libxl_device_pci *scan_sys_pcidir(libxl_device_pci *assigned,
   3.146 +                                         int num_assigned, const char *path, int *num)
   3.147 +{
   3.148 +    libxl_device_pci *pcidevs = NULL, *new;
   3.149 +    struct dirent *de;
   3.150 +    DIR *dir;
   3.151 +
   3.152 +    dir = opendir(path);
   3.153 +    if ( NULL == dir )
   3.154 +        return pcidevs;
   3.155 +
   3.156 +    while( (de = readdir(dir)) ) {
   3.157 +        unsigned dom, bus, dev, func;
   3.158 +        if ( sscanf(de->d_name, PCI_BDF, &dom, &bus, &dev, &func) != 4 )
   3.159 +            continue;
   3.160 +
   3.161 +        if ( is_assigned(assigned, num_assigned, dom, bus, dev, func) )
   3.162 +            continue;
   3.163 +
   3.164 +        new = realloc(pcidevs, ((*num) + 1) * sizeof(*new));
   3.165 +        if ( NULL == new )
   3.166 +            continue;
   3.167 +
   3.168 +        pcidevs = new;
   3.169 +        new = pcidevs + *num;
   3.170 +
   3.171 +        memset(new, 0, sizeof(*new));
   3.172 +        libxl_device_pci_init(new, dom, bus, dev, func, 0);
   3.173 +        (*num)++;
   3.174 +    }
   3.175 +
   3.176 +    closedir(dir);
   3.177 +    return pcidevs;
   3.178 +}
   3.179 +
   3.180 +int libxl_device_pci_list_assignable(libxl_ctx *ctx, libxl_device_pci **list, int *num)
   3.181 +{
   3.182 +    libxl_device_pci *pcidevs = NULL;
   3.183 +    libxl_device_pci *assigned;
   3.184 +    int num_assigned, rc;
   3.185 +
   3.186 +    *num = 0;
   3.187 +    *list = NULL;
   3.188 +
   3.189 +    rc = get_all_assigned_devices(ctx, &assigned, &num_assigned);
   3.190 +    if ( rc )
   3.191 +        return rc;
   3.192 +
   3.193 +    pcidevs = scan_sys_pcidir(assigned, num_assigned,
   3.194 +                              SYSFS_PCIBACK_DRIVER, num);
   3.195 +
   3.196 +    free(assigned);
   3.197 +    if ( *num )
   3.198 +        *list = pcidevs;
   3.199 +    return 0;
   3.200 +}
   3.201 +
   3.202 +int libxl_device_pci_list_assigned(libxl_ctx *ctx, libxl_device_pci **list, uint32_t domid, int *num)
   3.203  {
   3.204      char *be_path, *num_devs, *xsdev, *xsvdevfn, *xsopts;
   3.205      int n, i;
   3.206 @@ -477,7 +622,8 @@ libxl_device_pci *libxl_device_pci_list(
   3.207      num_devs = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/num_devs", be_path));
   3.208      if (!num_devs) {
   3.209          *num = 0;
   3.210 -        return NULL;
   3.211 +        *list = NULL;
   3.212 +        return ERROR_FAIL;
   3.213      }
   3.214      n = atoi(num_devs);
   3.215      pcidevs = calloc(n, sizeof(libxl_device_pci));
   3.216 @@ -507,15 +653,19 @@ libxl_device_pci *libxl_device_pci_list(
   3.217              } while ((p = strtok_r(NULL, ",=", &saveptr)) != NULL);
   3.218          }
   3.219      }
   3.220 -    return pcidevs;
   3.221 +    if ( *num )
   3.222 +        *list = pcidevs;
   3.223 +    return 0;
   3.224  }
   3.225  
   3.226  int libxl_device_pci_shutdown(libxl_ctx *ctx, uint32_t domid)
   3.227  {
   3.228      libxl_device_pci *pcidevs;
   3.229 -    int num, i;
   3.230 +    int num, i, rc;
   3.231  
   3.232 -    pcidevs = libxl_device_pci_list(ctx, domid, &num);
   3.233 +    rc = libxl_device_pci_list_assigned(ctx, &pcidevs, domid, &num);
   3.234 +    if ( rc )
   3.235 +        return rc;
   3.236      for (i = 0; i < num; i++) {
   3.237          if (libxl_device_pci_remove(ctx, domid, pcidevs + i) < 0)
   3.238              return ERROR_FAIL;
     4.1 --- a/tools/libxl/xl.h	Thu Jul 29 18:49:47 2010 +0100
     4.2 +++ b/tools/libxl/xl.h	Thu Jul 29 18:52:33 2010 +0100
     4.3 @@ -32,6 +32,7 @@ int main_cd_insert(int argc, char **argv
     4.4  int main_console(int argc, char **argv);
     4.5  int main_vncviewer(int argc, char **argv);
     4.6  int main_pcilist(int argc, char **argv);
     4.7 +int main_pcilist_assignable(int argc, char **argv);
     4.8  int main_pcidetach(int argc, char **argv);
     4.9  int main_pciattach(int argc, char **argv);
    4.10  int main_restore(int argc, char **argv);
     5.1 --- a/tools/libxl/xl_cmdimpl.c	Thu Jul 29 18:49:47 2010 +0100
     5.2 +++ b/tools/libxl/xl_cmdimpl.c	Thu Jul 29 18:52:33 2010 +0100
     5.3 @@ -1923,6 +1923,39 @@ int main_vncviewer(int argc, char **argv
     5.4      exit(0);
     5.5  }
     5.6  
     5.7 +void pcilist_assignable(void)
     5.8 +{
     5.9 +    libxl_device_pci *pcidevs;
    5.10 +    int num, i;
    5.11 +
    5.12 +    if ( libxl_device_pci_list_assignable(&ctx, &pcidevs, &num) )
    5.13 +        return;
    5.14 +    for (i = 0; i < num; i++) {
    5.15 +        printf("%04x:%02x:%02x:%01x\n",
    5.16 +                pcidevs[i].domain, pcidevs[i].bus, pcidevs[i].dev, pcidevs[i].func);
    5.17 +    }
    5.18 +    free(pcidevs);
    5.19 +}
    5.20 +
    5.21 +int main_pcilist_assignable(int argc, char **argv)
    5.22 +{
    5.23 +    int opt;
    5.24 +
    5.25 +    while ((opt = getopt(argc, argv, "h")) != -1) {
    5.26 +        switch (opt) {
    5.27 +        case 'h':
    5.28 +            help("pci-list-assignable-devices");
    5.29 +            exit(0);
    5.30 +        default:
    5.31 +            fprintf(stderr, "option not supported\n");
    5.32 +            break;
    5.33 +        }
    5.34 +    }
    5.35 +
    5.36 +    pcilist_assignable();
    5.37 +    exit(0);
    5.38 +}
    5.39 +
    5.40  void pcilist(char *dom)
    5.41  {
    5.42      libxl_device_pci *pcidevs;
    5.43 @@ -1930,8 +1963,7 @@ void pcilist(char *dom)
    5.44  
    5.45      find_domain(dom);
    5.46  
    5.47 -    pcidevs = libxl_device_pci_list(&ctx, domid, &num);
    5.48 -    if (!num)
    5.49 +    if (libxl_device_pci_list_assigned(&ctx, &pcidevs, domid, &num))
    5.50          return;
    5.51      printf("VFn  domain bus  slot func\n");
    5.52      for (i = 0; i < num; i++) {
     6.1 --- a/tools/libxl/xl_cmdtable.c	Thu Jul 29 18:49:47 2010 +0100
     6.2 +++ b/tools/libxl/xl_cmdtable.c	Thu Jul 29 18:52:33 2010 +0100
     6.3 @@ -69,6 +69,11 @@ struct cmd_spec cmd_table[] = {
     6.4        "List pass-through pci devices for a domain",
     6.5        "<Domain>",
     6.6      },
     6.7 +    { "pci-list-assignable-devices",
     6.8 +      &main_pcilist_assignable,
     6.9 +      "List all the assignable pci devices",
    6.10 +      "",
    6.11 +    },
    6.12      { "pause",
    6.13        &main_pause,
    6.14        "Pause execution of a domain",