char *libxl_path;
char **dir = NULL;
unsigned int ndirs = 0;
+ unsigned int ndevs = 0;
int rc;
*num = 0;
dir = libxl__xs_directory(gc, XBT_NULL, libxl_path, &ndirs);
if (dir && ndirs) {
- list = libxl__malloc(NOGC, dt->dev_elem_size * ndirs);
+ if (dt->get_num) {
+ if (ndirs != 1) {
+ LOGD(ERROR, domid, "multiple entries in %s\n", libxl_path);
+ rc = ERROR_FAIL;
+ goto out;
+ }
+ rc = dt->get_num(gc, GCSPRINTF("%s/%s", libxl_path, *dir), &ndevs);
+ if (rc) goto out;
+ } else {
+ ndevs = ndirs;
+ }
+ list = libxl__malloc(NOGC, dt->dev_elem_size * ndevs);
item = list;
- while (*num < ndirs) {
+ while (*num < ndevs) {
dt->init(item);
- ++(*num);
if (dt->from_xenstore) {
+ int nr = dt->get_num ? *num : atoi(*dir);
char *device_libxl_path = GCSPRINTF("%s/%s", libxl_path, *dir);
- rc = dt->from_xenstore(gc, device_libxl_path, atoi(*dir), item);
+ rc = dt->from_xenstore(gc, device_libxl_path, nr, item);
if (rc) goto out;
}
item = (uint8_t *)item + dt->dev_elem_size;
- ++dir;
+ ++(*num);
+ if (!dt->get_num)
+ ++dir;
}
}
typedef int (*device_dm_needed_fn_t)(void *, unsigned);
typedef void (*device_update_config_fn_t)(libxl__gc *, void *, void *);
typedef int (*device_update_devid_fn_t)(libxl__gc *, uint32_t, void *);
+typedef int (*device_get_num_fn_t)(libxl__gc *, const char *, unsigned int *);
typedef int (*device_from_xenstore_fn_t)(libxl__gc *, const char *,
libxl_devid, void *);
typedef int (*device_set_xenstore_config_fn_t)(libxl__gc *, uint32_t, void *,
device_dm_needed_fn_t dm_needed;
device_update_config_fn_t update_config;
device_update_devid_fn_t update_devid;
+ device_get_num_fn_t get_num;
device_from_xenstore_fn_t from_xenstore;
device_set_xenstore_config_fn_t set_xenstore_config;
};
return AO_INPROGRESS;
}
-static void libxl__device_pci_from_xs_be(libxl__gc *gc,
- const char *be_path,
- int nr, libxl_device_pci *pci)
+static int libxl__device_pci_from_xs_be(libxl__gc *gc,
+ const char *be_path,
+ libxl_devid nr, void *data)
{
char *s;
unsigned int domain = 0, bus = 0, dev = 0, func = 0, vdevfn = 0;
+ libxl_device_pci *pci = data;
s = libxl__xs_read(gc, XBT_NULL, GCSPRINTF("%s/dev-%d", be_path, nr));
sscanf(s, PCI_BDF, &domain, &bus, &dev, &func);
}
} while ((p = strtok_r(NULL, ",=", &saveptr)) != NULL);
}
+
+ return 0;
+}
+
+static int libxl__device_pci_get_num(libxl__gc *gc, const char *be_path,
+ unsigned int *num)
+{
+ char *num_devs;
+ int rc = 0;
+
+ num_devs = libxl__xs_read(gc, XBT_NULL, GCSPRINTF("%s/num_devs", be_path));
+ if (!num_devs)
+ rc = ERROR_FAIL;
+ else
+ *num = atoi(num_devs);
+
+ return rc;
}
libxl_device_pci *libxl_device_pci_list(libxl_ctx *ctx, uint32_t domid, int *num)
{
GC_INIT(ctx);
- char *be_path, *num_devs;
- int n, i;
+ char *be_path;
+ unsigned int n, i;
libxl_device_pci *pcidevs = NULL;
*num = 0;
be_path = libxl__domain_device_backend_path(gc, 0, domid, 0,
LIBXL__DEVICE_KIND_PCI);
- num_devs = libxl__xs_read(gc, XBT_NULL, GCSPRINTF("%s/num_devs", be_path));
- if (!num_devs)
+ if (libxl__device_pci_get_num(gc, be_path, &n))
goto out;
- n = atoi(num_devs);
pcidevs = calloc(n, sizeof(libxl_device_pci));
for (i = 0; i < n; i++)
#define libxl__device_pci_update_devid NULL
DEFINE_DEVICE_TYPE_STRUCT_X(pcidev, pci, PCI,
- .from_xenstore = (device_from_xenstore_fn_t)libxl__device_pci_from_xs_be,
+ .get_num = libxl__device_pci_get_num,
+ .from_xenstore = libxl__device_pci_from_xs_be,
);
/*