All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/6] libxl: extend device type framework
@ 2016-07-12 15:30 Juergen Gross
  2016-07-12 15:30 ` [PATCH 1/6] libxl: add "merge" function to generic device type support Juergen Gross
                   ` (7 more replies)
  0 siblings, 8 replies; 18+ messages in thread
From: Juergen Gross @ 2016-07-12 15:30 UTC (permalink / raw)
  To: xen-devel; +Cc: Juergen Gross, wei.liu2, ian.jackson

Extend the device type framework in libxl to cover more functions in a
generic way. This allows to have all functionality of a specific device
type in just one source file instead of spreading it via multiple files
and have functions to deal with multiple device types in a similar way.

Some patches of this series can be regarded as RFC, as having e.g. all
nic related functions in one file is a matter of taste. I haven't
marked the patches as RFC as they are completely tested and I believe
the result is an improvement over current status.

Juergen Gross (6):
  libxl: add "merge" function to generic device type support
  libxl: add "pv device mode needed" support to device type framework
  libxl: move library pvusb specific code into libxl_pvusb.c
  libxl: split libxl vtpm code into one source
  libxl: add config update callback to device type framework
  libxl: move common nic stuff into one source

 tools/libxl/Makefile         |   1 +
 tools/libxl/libxl.c          | 930 ++++++-------------------------------------
 tools/libxl/libxl_create.c   |  35 +-
 tools/libxl/libxl_dm.c       |  31 +-
 tools/libxl/libxl_internal.c |  21 +-
 tools/libxl/libxl_internal.h |  77 ++--
 tools/libxl/libxl_nic.c      | 554 ++++++++++++++++++++++++++
 tools/libxl/libxl_pci.c      |   8 +-
 tools/libxl/libxl_pvusb.c    |  42 +-
 tools/libxl/libxl_utils.c    |  96 -----
 tools/libxl/libxl_vtpm.c     | 380 ++++++++++++++++++
 11 files changed, 1192 insertions(+), 983 deletions(-)
 create mode 100644 tools/libxl/libxl_nic.c
 create mode 100644 tools/libxl/libxl_vtpm.c

-- 
2.6.6


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [PATCH 1/6] libxl: add "merge" function to generic device type support
  2016-07-12 15:30 [PATCH 0/6] libxl: extend device type framework Juergen Gross
@ 2016-07-12 15:30 ` Juergen Gross
  2016-07-25 10:45   ` Wei Liu
  2017-01-19 16:14   ` Olaf Hering
  2016-07-12 15:30 ` [PATCH 2/6] libxl: add "pv device mode needed" support to device type framework Juergen Gross
                   ` (6 subsequent siblings)
  7 siblings, 2 replies; 18+ messages in thread
From: Juergen Gross @ 2016-07-12 15:30 UTC (permalink / raw)
  To: xen-devel; +Cc: Juergen Gross, wei.liu2, ian.jackson

Instead of using a macro generating the code to merge xenstore and
json configuration data, use the generic device type support for
this purpose.

This requires to add some accessor functions to the framework and
a structure for disks (as disks are added separately they didn't need
such a structure up to now).

Signed-off-by: Juergen Gross <jgross@suse.com>
---
 tools/libxl/libxl.c          | 181 ++++++++++++++++++++++++-------------------
 tools/libxl/libxl_create.c   |  18 +++--
 tools/libxl/libxl_internal.h |  52 +++++++++++--
 tools/libxl/libxl_pci.c      |   8 +-
 tools/libxl/libxl_pvusb.c    |  12 +++
 5 files changed, 181 insertions(+), 90 deletions(-)

diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c
index 2cf7451..07b96c7 100644
--- a/tools/libxl/libxl.c
+++ b/tools/libxl/libxl.c
@@ -7371,93 +7371,68 @@ int libxl_retrieve_domain_configuration(libxl_ctx *ctx, uint32_t domid,
      *    entry retrieved from xenstore while "dst" points to the entry
      *    retrieve from JSON.
      */
-#define MERGE(type, ptr, compare, merge)                                \
-    do {                                                                \
-        libxl_device_##type *p = NULL;                                  \
-        int i, j, num;                                                  \
-                                                                        \
-        p = libxl_device_##type##_list(CTX, domid, &num);               \
-        if (p == NULL) {                                                \
-            LOG(DEBUG,                                                  \
-                "no %s from xenstore for domain %d",                    \
-                #type, domid);                                          \
-        }                                                               \
-                                                                        \
-        for (i = 0; i < d_config->num_##ptr; i++) {                     \
-            libxl_device_##type *q = &d_config->ptr[i];                 \
-            for (j = 0; j < num; j++) {                                 \
-                if (compare(&p[j], q))                                  \
-                    break;                                              \
-            }                                                           \
-                                                                        \
-            if (j < num) {         /* found in xenstore */              \
-                libxl_device_##type *dst, *src;                         \
-                dst = q;                                                \
-                src = &p[j];                                            \
-                merge;                                                  \
-            } else {                /* not found in xenstore */         \
-                LOG(WARN,                                               \
-                "Device present in JSON but not in xenstore, ignored"); \
-                                                                        \
-                libxl_device_##type##_dispose(q);                       \
-                                                                        \
-                for (j = i; j < d_config->num_##ptr - 1; j++)           \
-                    memcpy(&d_config->ptr[j], &d_config->ptr[j+1],      \
-                           sizeof(libxl_device_##type));                \
-                                                                        \
-                d_config->ptr =                                         \
-                    libxl__realloc(NOGC, d_config->ptr,                 \
-                                   sizeof(libxl_device_##type) *        \
-                                   (d_config->num_##ptr - 1));          \
-                                                                        \
-                /* rewind counters */                                   \
-                d_config->num_##ptr--;                                  \
-                i--;                                                    \
-            }                                                           \
-        }                                                               \
-                                                                        \
-        for (i = 0; i < num; i++)                                       \
-            libxl_device_##type##_dispose(&p[i]);                       \
-        free(p);                                                        \
-    } while (0);
+    {
+        const struct libxl_device_type *dt;
+        int idx;
 
-    MERGE(nic, nics, COMPARE_DEVID, {});
+        for (idx = 0;; idx++) {
+            void *p = NULL;
+            void **devs;
+            int i, j, num;
+            int *num_dev;
 
-    MERGE(vtpm, vtpms, COMPARE_DEVID, {});
+            dt = device_type_tbl[idx];
+            if (!dt)
+                break;
 
-    MERGE(pci, pcidevs, COMPARE_PCI, {});
+            if (!dt->list || !dt->compare)
+                continue;
 
-    MERGE(usbctrl, usbctrls, COMPARE_USBCTRL, {});
+            num_dev = libxl__device_type_get_num(dt, d_config);
+            p = dt->list(CTX, domid, &num);
+            if (p == NULL) {
+                LOG(DEBUG, "no %s from xenstore for domain %d",
+                    dt->type, domid);
+            }
+            devs = libxl__device_type_get_ptr(dt, d_config);
+
+            for (i = 0; i < *num_dev; i++) {
+                void *q;
+
+                q = libxl__device_type_get_elem(dt, d_config, i);
+                for (j = 0; j < num; j++) {
+                    if (dt->compare(p + dt->dev_elem_size * j, q))
+                        break;
+                }
+
+                if (j < num) {         /* found in xenstore */
+                    if (dt->merge)
+                        dt->merge(ctx, p + dt->dev_elem_size * j, q);
+                } else {                /* not found in xenstore */
+                    LOG(WARN,
+                        "Device present in JSON but not in xenstore, ignored");
+
+                    dt->dispose(q);
+
+                    for (j = i; j < *num_dev - 1; j++)
+                        memcpy(libxl__device_type_get_elem(dt, d_config, j),
+                               libxl__device_type_get_elem(dt, d_config, j+1),
+                               dt->dev_elem_size);
 
-    MERGE(usbdev, usbdevs, COMPARE_USB, {});
+                    /* rewind counters */
+                    (*num_dev)--;
+                    i--;
 
-    /* Take care of removable device. We maintain invariant in the
-     * insert / remove operation so that:
-     * 1. if xenstore is "empty" while JSON is not, the result
-     *    is "empty"
-     * 2. if xenstore has a different media than JSON, use the
-     *    one in JSON
-     * 3. if xenstore and JSON have the same media, well, you
-     *    know the answer :-)
-     *
-     * Currently there is only one removable device -- CDROM.
-     * Look for libxl_cdrom_insert for reference.
-     */
-    MERGE(disk, disks, COMPARE_DISK, {
-            if (src->removable) {
-                if (!src->pdev_path || *src->pdev_path == '\0') {
-                    /* 1, no media in drive */
-                    free(dst->pdev_path);
-                    dst->pdev_path = libxl__strdup(NOGC, "");
-                    dst->format = LIBXL_DISK_FORMAT_EMPTY;
-                } else {
-                    /* 2 and 3, use JSON, no need to touch anything */
-                    ;
+                    *devs = libxl__realloc(NOGC, *devs,
+                                           dt->dev_elem_size * *num_dev);
                 }
             }
-        });
 
-#undef MERGE
+            for (i = 0; i < num; i++)
+                dt->dispose(p + dt->dev_elem_size * i);
+            free(p);
+        }
+    }
 
 out:
     if (lock) libxl__unlock_domain_userdata(lock);
@@ -7466,6 +7441,56 @@ out:
     return rc;
 }
 
+static int libxl_device_disk_compare(libxl_device_disk *d1,
+                                     libxl_device_disk *d2)
+{
+    return COMPARE_DISK(d1, d2);
+}
+
+/* Take care of removable device. We maintain invariant in the
+ * insert / remove operation so that:
+ * 1. if xenstore is "empty" while JSON is not, the result
+ *    is "empty"
+ * 2. if xenstore has a different media than JSON, use the
+ *    one in JSON
+ * 3. if xenstore and JSON have the same media, well, you
+ *    know the answer :-)
+ *
+ * Currently there is only one removable device -- CDROM.
+ * Look for libxl_cdrom_insert for reference.
+ */
+static void libxl_device_disk_merge(libxl_ctx *ctx, void *d1, void *d2)
+{
+    GC_INIT(ctx);
+    libxl_device_disk *src = d1;
+    libxl_device_disk *dst = d2;
+
+    if (src->removable) {
+        if (!src->pdev_path || *src->pdev_path == '\0') {
+            /* 1, no media in drive */
+            free(dst->pdev_path);
+            dst->pdev_path = libxl__strdup(NOGC, "");
+            dst->format = LIBXL_DISK_FORMAT_EMPTY;
+        } else {
+            /* 2 and 3, use JSON, no need to touch anything */
+            ;
+        }
+    }
+}
+
+static int libxl_device_nic_compare(libxl_device_nic *d1,
+                                    libxl_device_nic *d2)
+{
+    return COMPARE_DEVID(d1, d2);
+}
+
+static int libxl_device_vtpm_compare(libxl_device_vtpm *d1,
+                                     libxl_device_vtpm *d2)
+{
+    return COMPARE_DEVID(d1, d2);
+}
+
+DEFINE_DEVICE_TYPE_STRUCT(disk, .merge = libxl_device_disk_merge);
 DEFINE_DEVICE_TYPE_STRUCT(nic);
 DEFINE_DEVICE_TYPE_STRUCT(vtpm);
 
diff --git a/tools/libxl/libxl_create.c b/tools/libxl/libxl_create.c
index 828f254..40dac1a 100644
--- a/tools/libxl/libxl_create.c
+++ b/tools/libxl/libxl_create.c
@@ -1420,15 +1420,19 @@ out:
     aodev->callback(egc, aodev);
 }
 
+#define libxl_device_dtdev_list NULL
+#define libxl_device_dtdev_compare NULL
 static DEFINE_DEVICE_TYPE_STRUCT(dtdev);
 
-static const struct libxl_device_type *device_type_tbl[] = {
+const struct libxl_device_type *device_type_tbl[] = {
+    &libxl__disk_devtype,
     &libxl__nic_devtype,
     &libxl__vtpm_devtype,
     &libxl__usbctrl_devtype,
     &libxl__usbdev_devtype,
     &libxl__pcidev_devtype,
     &libxl__dtdev_devtype,
+    NULL
 };
 
 static void domcreate_attach_devices(libxl__egc *egc,
@@ -1448,9 +1452,9 @@ static void domcreate_attach_devices(libxl__egc *egc,
     }
 
     dcs->device_type_idx++;
-    if (dcs->device_type_idx < ARRAY_SIZE(device_type_tbl)) {
-        dt = device_type_tbl[dcs->device_type_idx];
-        if (*(int *)((void *)d_config + dt->num_offset) > 0) {
+    dt = device_type_tbl[dcs->device_type_idx];
+    if (dt) {
+        if (*libxl__device_type_get_num(dt, d_config) > 0) {
             /* Attach devices */
             libxl__multidev_begin(ao, &dcs->multidev);
             dcs->multidev.callback = domcreate_attach_devices;
@@ -1497,7 +1501,11 @@ static void domcreate_devmodel_started(libxl__egc *egc,
         }
     }
 
-    dcs->device_type_idx = -1;
+    /*
+     * Setting dcs->device_type_idx to 0 will skip disks, those have been
+     * already added.
+     */
+    dcs->device_type_idx = 0;
     domcreate_attach_devices(egc, &dcs->multidev, 0);
     return;
 
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index 5347b69..1a62d6f 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -3441,23 +3441,63 @@ _hidden void libxl__bootloader_run(libxl__egc*, libxl__bootloader_state *st);
 
 struct libxl_device_type {
     char *type;
-    int num_offset;   /* Offset of # of devices in libxl_domain_config */
+    int ptr_offset;    /* Offset of device array ptr in libxl_domain_config */
+    int num_offset;    /* Offset of # of devices in libxl_domain_config */
+    int dev_elem_size; /* Size of one device element in array */
     void (*add)(libxl__egc *, libxl__ao *, uint32_t, libxl_domain_config *,
                 libxl__multidev *);
+    void *(*list)(libxl_ctx *, uint32_t, int *);
+    void (*dispose)(void *);
+    int (*compare)(void *, void *);
+    void (*merge)(libxl_ctx *, void *, void *);
 };
 
-#define DEFINE_DEVICE_TYPE_STRUCT(name)                                 \
-    const struct libxl_device_type libxl__ ## name ## _devtype = {      \
-        .type       = #name,                                            \
-        .num_offset = offsetof(libxl_domain_config, num_ ## name ## s), \
-        .add        = libxl__add_ ## name ## s,                         \
+#define DEFINE_DEVICE_TYPE_STRUCT_X(name, sname, ...)                          \
+    const struct libxl_device_type libxl__ ## name ## _devtype = {             \
+        .type          = #sname,                                               \
+        .ptr_offset    = offsetof(libxl_domain_config, name ## s),             \
+        .num_offset    = offsetof(libxl_domain_config, num_ ## name ## s),     \
+        .dev_elem_size = sizeof(libxl_device_ ## sname),                       \
+        .add           = libxl__add_ ## name ## s,                             \
+        .list          = (void *(*)(libxl_ctx *, uint32_t, int *))             \
+                         libxl_device_ ## sname ## _list,                      \
+        .dispose       = (void (*)(void *))libxl_device_ ## sname ## _dispose, \
+        .compare       = (int (*)(void *, void *))                             \
+                         libxl_device_ ## sname ## _compare,                   \
+        __VA_ARGS__                                                            \
     }
 
+#define DEFINE_DEVICE_TYPE_STRUCT(name, ...)                                   \
+    DEFINE_DEVICE_TYPE_STRUCT_X(name, name, __VA_ARGS__)
+
+static inline void **libxl__device_type_get_ptr(
+    const struct libxl_device_type *dt, const libxl_domain_config *d_config)
+{
+    return (void **)((void *)d_config + dt->ptr_offset);
+}
+
+static inline void *libxl__device_type_get_elem(
+    const struct libxl_device_type *dt, const libxl_domain_config *d_config,
+    int e)
+{
+    return *libxl__device_type_get_ptr(dt, d_config) + dt->dev_elem_size * e;
+}
+
+static inline int *libxl__device_type_get_num(
+    const struct libxl_device_type *dt, const libxl_domain_config *d_config)
+{
+    return (int *)((void *)d_config + dt->num_offset);
+}
+
+extern const struct libxl_device_type libxl__disk_devtype;
 extern const struct libxl_device_type libxl__nic_devtype;
 extern const struct libxl_device_type libxl__vtpm_devtype;
 extern const struct libxl_device_type libxl__usbctrl_devtype;
 extern const struct libxl_device_type libxl__usbdev_devtype;
 extern const struct libxl_device_type libxl__pcidev_devtype;
+
+extern const struct libxl_device_type *device_type_tbl[];
+
 /*----- Domain destruction -----*/
 
 /* Domain destruction has been split into two functions:
diff --git a/tools/libxl/libxl_pci.c b/tools/libxl/libxl_pci.c
index 9676687..22398a4 100644
--- a/tools/libxl/libxl_pci.c
+++ b/tools/libxl/libxl_pci.c
@@ -1698,7 +1698,13 @@ int libxl__grant_vga_iomem_permission(libxl__gc *gc, const uint32_t domid,
     return 0;
 }
 
-DEFINE_DEVICE_TYPE_STRUCT(pcidev);
+static int libxl_device_pci_compare(libxl_device_pci *d1,
+                                    libxl_device_pci *d2)
+{
+    return COMPARE_PCI(d1, d2);
+}
+
+DEFINE_DEVICE_TYPE_STRUCT_X(pcidev, pci);
 
 /*
  * Local variables:
diff --git a/tools/libxl/libxl_pvusb.c b/tools/libxl/libxl_pvusb.c
index 41ea6bc..48a4cec 100644
--- a/tools/libxl/libxl_pvusb.c
+++ b/tools/libxl/libxl_pvusb.c
@@ -1675,6 +1675,18 @@ out:
     return rc;
 }
 
+static int libxl_device_usbctrl_compare(libxl_device_usbctrl *d1,
+                                        libxl_device_usbctrl *d2)
+{
+    return COMPARE_USBCTRL(d1, d2);
+}
+
+static int libxl_device_usbdev_compare(libxl_device_usbdev *d1,
+                                       libxl_device_usbdev *d2)
+{
+    return COMPARE_USB(d1, d2);
+}
+
 DEFINE_DEVICE_TYPE_STRUCT(usbctrl);
 DEFINE_DEVICE_TYPE_STRUCT(usbdev);
 
-- 
2.6.6


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH 2/6] libxl: add "pv device mode needed" support to device type framework
  2016-07-12 15:30 [PATCH 0/6] libxl: extend device type framework Juergen Gross
  2016-07-12 15:30 ` [PATCH 1/6] libxl: add "merge" function to generic device type support Juergen Gross
@ 2016-07-12 15:30 ` Juergen Gross
  2016-07-25 10:46   ` Wei Liu
  2016-07-12 15:30 ` [PATCH 3/6] libxl: move library pvusb specific code into libxl_pvusb.c Juergen Gross
                   ` (5 subsequent siblings)
  7 siblings, 1 reply; 18+ messages in thread
From: Juergen Gross @ 2016-07-12 15:30 UTC (permalink / raw)
  To: xen-devel; +Cc: Juergen Gross, wei.liu2, ian.jackson

Add another callback to the device type framework in order to aid
decision whether a pv domain needs a device model.

Signed-off-by: Juergen Gross <jgross@suse.com>
---
 tools/libxl/libxl.c          | 14 +++++++++++++-
 tools/libxl/libxl_create.c   |  8 ++------
 tools/libxl/libxl_dm.c       | 31 +++++++++++++++++--------------
 tools/libxl/libxl_internal.h |  2 ++
 tools/libxl/libxl_pvusb.c    | 12 +++++++++++-
 5 files changed, 45 insertions(+), 22 deletions(-)

diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c
index 07b96c7..12bc0e1 100644
--- a/tools/libxl/libxl.c
+++ b/tools/libxl/libxl.c
@@ -7478,6 +7478,14 @@ static void libxl_device_disk_merge(libxl_ctx *ctx, void *d1, void *d2)
     }
 }
 
+static int libxl_device_disk_dm_needed(void *e, unsigned domid)
+{
+    libxl_device_disk *elem = e;
+
+    return elem->backend == LIBXL_DISK_BACKEND_QDISK &&
+           elem->backend_domid == domid;
+}
+
 static int libxl_device_nic_compare(libxl_device_nic *d1,
                                     libxl_device_nic *d2)
 {
@@ -7490,7 +7498,11 @@ static int libxl_device_vtpm_compare(libxl_device_vtpm *d1,
     return COMPARE_DEVID(d1, d2);
 }
 
-DEFINE_DEVICE_TYPE_STRUCT(disk, .merge = libxl_device_disk_merge);
+DEFINE_DEVICE_TYPE_STRUCT(disk,
+    .merge       = libxl_device_disk_merge,
+    .dm_needed   = libxl_device_disk_dm_needed,
+    .skip_attach = 1
+);
 DEFINE_DEVICE_TYPE_STRUCT(nic);
 DEFINE_DEVICE_TYPE_STRUCT(vtpm);
 
diff --git a/tools/libxl/libxl_create.c b/tools/libxl/libxl_create.c
index 40dac1a..23b82e9 100644
--- a/tools/libxl/libxl_create.c
+++ b/tools/libxl/libxl_create.c
@@ -1454,7 +1454,7 @@ static void domcreate_attach_devices(libxl__egc *egc,
     dcs->device_type_idx++;
     dt = device_type_tbl[dcs->device_type_idx];
     if (dt) {
-        if (*libxl__device_type_get_num(dt, d_config) > 0) {
+        if (*libxl__device_type_get_num(dt, d_config) > 0 && !dt->skip_attach) {
             /* Attach devices */
             libxl__multidev_begin(ao, &dcs->multidev);
             dcs->multidev.callback = domcreate_attach_devices;
@@ -1501,11 +1501,7 @@ static void domcreate_devmodel_started(libxl__egc *egc,
         }
     }
 
-    /*
-     * Setting dcs->device_type_idx to 0 will skip disks, those have been
-     * already added.
-     */
-    dcs->device_type_idx = 0;
+    dcs->device_type_idx = -1;
     domcreate_attach_devices(egc, &dcs->multidev, 0);
     return;
 
diff --git a/tools/libxl/libxl_dm.c b/tools/libxl/libxl_dm.c
index de16a59..e3bf28f 100644
--- a/tools/libxl/libxl_dm.c
+++ b/tools/libxl/libxl_dm.c
@@ -2348,8 +2348,9 @@ int libxl__destroy_device_model(libxl__gc *gc, uint32_t domid)
 /* Return 0 if no dm needed, 1 if needed and <0 if error. */
 int libxl__need_xenpv_qemu(libxl__gc *gc, libxl_domain_config *d_config)
 {
-    int i, ret;
+    int idx, i, ret, num;
     uint32_t domid;
+    const struct libxl_device_type *dt;
 
     ret = libxl__get_domid(gc, &domid);
     if (ret) {
@@ -2362,11 +2363,21 @@ int libxl__need_xenpv_qemu(libxl__gc *gc, libxl_domain_config *d_config)
         goto out;
     }
 
-    for (i = 0; i < d_config->num_disks; i++) {
-        if (d_config->disks[i].backend == LIBXL_DISK_BACKEND_QDISK &&
-            d_config->disks[i].backend_domid == domid) {
-            ret = 1;
-            goto out;
+    for (idx = 0;; idx++) {
+        dt = device_type_tbl[idx];
+        if (!dt)
+            break;
+
+        num = *libxl__device_type_get_num(dt, d_config);
+        if (!dt->dm_needed || !num)
+            continue;
+
+        for (i = 0; i < num; i++) {
+            if (dt->dm_needed(libxl__device_type_get_elem(dt, d_config, i),
+                              domid)) {
+                ret = 1;
+                goto out;
+            }
         }
     }
 
@@ -2380,14 +2391,6 @@ int libxl__need_xenpv_qemu(libxl__gc *gc, libxl_domain_config *d_config)
         }
     }
 
-    for (i = 0; i < d_config->num_usbctrls; i++) {
-       if (d_config->usbctrls[i].type == LIBXL_USBCTRL_TYPE_QUSB &&
-           d_config->usbctrls[i].backend_domid == domid) {
-            ret = 1;
-            goto out;
-        }
-    }
-
 out:
     return ret;
 }
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index 1a62d6f..aea14ea 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -3441,6 +3441,7 @@ _hidden void libxl__bootloader_run(libxl__egc*, libxl__bootloader_state *st);
 
 struct libxl_device_type {
     char *type;
+    int skip_attach;   /* Skip entry in domcreate_attach_devices() if 1 */
     int ptr_offset;    /* Offset of device array ptr in libxl_domain_config */
     int num_offset;    /* Offset of # of devices in libxl_domain_config */
     int dev_elem_size; /* Size of one device element in array */
@@ -3450,6 +3451,7 @@ struct libxl_device_type {
     void (*dispose)(void *);
     int (*compare)(void *, void *);
     void (*merge)(libxl_ctx *, void *, void *);
+    int (*dm_needed)(void *, unsigned);
 };
 
 #define DEFINE_DEVICE_TYPE_STRUCT_X(name, sname, ...)                          \
diff --git a/tools/libxl/libxl_pvusb.c b/tools/libxl/libxl_pvusb.c
index 48a4cec..c86d0b6 100644
--- a/tools/libxl/libxl_pvusb.c
+++ b/tools/libxl/libxl_pvusb.c
@@ -1681,13 +1681,23 @@ static int libxl_device_usbctrl_compare(libxl_device_usbctrl *d1,
     return COMPARE_USBCTRL(d1, d2);
 }
 
+static int libxl_device_usbctrl_dm_needed(void *e, unsigned domid)
+{
+    libxl_device_usbctrl *elem = e;
+
+    return elem->type == LIBXL_USBCTRL_TYPE_QUSB &&
+           elem->backend_domid == domid;
+}
+
 static int libxl_device_usbdev_compare(libxl_device_usbdev *d1,
                                        libxl_device_usbdev *d2)
 {
     return COMPARE_USB(d1, d2);
 }
 
-DEFINE_DEVICE_TYPE_STRUCT(usbctrl);
+DEFINE_DEVICE_TYPE_STRUCT(usbctrl,
+    .dm_needed = libxl_device_usbctrl_dm_needed
+);
 DEFINE_DEVICE_TYPE_STRUCT(usbdev);
 
 /*
-- 
2.6.6


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH 3/6] libxl: move library pvusb specific code into libxl_pvusb.c
  2016-07-12 15:30 [PATCH 0/6] libxl: extend device type framework Juergen Gross
  2016-07-12 15:30 ` [PATCH 1/6] libxl: add "merge" function to generic device type support Juergen Gross
  2016-07-12 15:30 ` [PATCH 2/6] libxl: add "pv device mode needed" support to device type framework Juergen Gross
@ 2016-07-12 15:30 ` Juergen Gross
  2016-07-25 10:46   ` Wei Liu
  2016-07-12 15:30 ` [PATCH 4/6] libxl: split libxl vtpm code into one source Juergen Gross
                   ` (4 subsequent siblings)
  7 siblings, 1 reply; 18+ messages in thread
From: Juergen Gross @ 2016-07-12 15:30 UTC (permalink / raw)
  To: xen-devel; +Cc: Juergen Gross, wei.liu2, ian.jackson

Outside libxl_pvusb.c only libxl_util.c still contains some pvusb code.

Move it to libxl_pvusb.c.

Signed-off-by: Juergen Gross <jgross@suse.com>
---
 tools/libxl/libxl_pvusb.c | 18 ++++++++++++++++++
 tools/libxl/libxl_utils.c | 18 ------------------
 2 files changed, 18 insertions(+), 18 deletions(-)

diff --git a/tools/libxl/libxl_pvusb.c b/tools/libxl/libxl_pvusb.c
index c86d0b6..88e9ed4 100644
--- a/tools/libxl/libxl_pvusb.c
+++ b/tools/libxl/libxl_pvusb.c
@@ -1695,6 +1695,24 @@ static int libxl_device_usbdev_compare(libxl_device_usbdev *d1,
     return COMPARE_USB(d1, d2);
 }
 
+void libxl_device_usbctrl_list_free(libxl_device_usbctrl *list, int nr)
+{
+   int i;
+
+   for (i = 0; i < nr; i++)
+       libxl_device_usbctrl_dispose(&list[i]);
+   free(list);
+}
+
+void libxl_device_usbdev_list_free(libxl_device_usbdev *list, int nr)
+{
+   int i;
+
+   for (i = 0; i < nr; i++)
+       libxl_device_usbdev_dispose(&list[i]);
+   free(list);
+}
+
 DEFINE_DEVICE_TYPE_STRUCT(usbctrl,
     .dm_needed = libxl_device_usbctrl_dm_needed
 );
diff --git a/tools/libxl/libxl_utils.c b/tools/libxl/libxl_utils.c
index 1607d4f..c6933d6 100644
--- a/tools/libxl/libxl_utils.c
+++ b/tools/libxl/libxl_utils.c
@@ -1310,24 +1310,6 @@ int libxl__random_bytes(libxl__gc *gc, uint8_t *buf, size_t len)
     return ret;
 }
 
-void libxl_device_usbctrl_list_free(libxl_device_usbctrl *list, int nr)
-{
-   int i;
-
-   for (i = 0; i < nr; i++)
-       libxl_device_usbctrl_dispose(&list[i]);
-   free(list);
-}
-
-void libxl_device_usbdev_list_free(libxl_device_usbdev *list, int nr)
-{
-   int i;
-
-   for (i = 0; i < nr; i++)
-       libxl_device_usbdev_dispose(&list[i]);
-   free(list);
-}
-
 /*
  * Local variables:
  * mode: C
-- 
2.6.6


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH 4/6] libxl: split libxl vtpm code into one source
  2016-07-12 15:30 [PATCH 0/6] libxl: extend device type framework Juergen Gross
                   ` (2 preceding siblings ...)
  2016-07-12 15:30 ` [PATCH 3/6] libxl: move library pvusb specific code into libxl_pvusb.c Juergen Gross
@ 2016-07-12 15:30 ` Juergen Gross
  2016-07-25 10:46   ` Wei Liu
  2016-07-12 15:30 ` [PATCH 5/6] libxl: add config update callback to device type framework Juergen Gross
                   ` (3 subsequent siblings)
  7 siblings, 1 reply; 18+ messages in thread
From: Juergen Gross @ 2016-07-12 15:30 UTC (permalink / raw)
  To: xen-devel; +Cc: Juergen Gross, wei.liu2, ian.jackson

Put all vtpm related stuff of libxl into a dedicated source file.

Signed-off-by: Juergen Gross <jgross@suse.com>
---
 tools/libxl/Makefile         |   1 +
 tools/libxl/libxl.c          | 302 -----------------------------------
 tools/libxl/libxl_internal.h |   1 -
 tools/libxl/libxl_utils.c    |  43 -----
 tools/libxl/libxl_vtpm.c     | 366 +++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 367 insertions(+), 346 deletions(-)
 create mode 100644 tools/libxl/libxl_vtpm.c

diff --git a/tools/libxl/Makefile b/tools/libxl/Makefile
index 9fee752..86c11bf 100644
--- a/tools/libxl/Makefile
+++ b/tools/libxl/Makefile
@@ -115,6 +115,7 @@ LIBXL_OBJS = flexarray.o libxl.o libxl_create.o libxl_dm.o libxl_pci.o \
 			libxl_save_callout.o _libxl_save_msgs_callout.o \
 			libxl_qmp.o libxl_event.o libxl_fork.o \
 			libxl_dom_suspend.o libxl_dom_save.o libxl_pvusb.o \
+			libxl_vtpm.o \
                         $(LIBXL_OBJS-y)
 LIBXL_OBJS += libxl_genid.o
 LIBXL_OBJS += _libxl_types.o libxl_flask.o _libxl_types_internal.o
diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c
index 12bc0e1..7ada4a7 100644
--- a/tools/libxl/libxl.c
+++ b/tools/libxl/libxl.c
@@ -2031,292 +2031,6 @@ int libxl__resolve_domid(libxl__gc *gc, const char *name, uint32_t *domid)
 }
 
 /******************************************************************************/
-int libxl__device_vtpm_setdefault(libxl__gc *gc, libxl_device_vtpm *vtpm)
-{
-    int rc;
-    if (libxl_uuid_is_nil(&vtpm->uuid)) {
-        libxl_uuid_generate(&vtpm->uuid);
-    }
-    rc = libxl__resolve_domid(gc, vtpm->backend_domname, &vtpm->backend_domid);
-    return rc;
-}
-
-static int libxl__device_from_vtpm(libxl__gc *gc, uint32_t domid,
-                                   libxl_device_vtpm *vtpm,
-                                   libxl__device *device)
-{
-   device->backend_devid   = vtpm->devid;
-   device->backend_domid   = vtpm->backend_domid;
-   device->backend_kind    = LIBXL__DEVICE_KIND_VTPM;
-   device->devid           = vtpm->devid;
-   device->domid           = domid;
-   device->kind            = LIBXL__DEVICE_KIND_VTPM;
-
-   return 0;
-}
-
-static void libxl__device_vtpm_add(libxl__egc *egc, uint32_t domid,
-                                   libxl_device_vtpm *vtpm,
-                                   libxl__ao_device *aodev)
-{
-    STATE_AO_GC(aodev->ao);
-    flexarray_t *front;
-    flexarray_t *back;
-    libxl__device *device;
-    int rc;
-    xs_transaction_t t = XBT_NULL;
-    libxl_domain_config d_config;
-    libxl_device_vtpm vtpm_saved;
-    libxl__domain_userdata_lock *lock = NULL;
-
-    libxl_domain_config_init(&d_config);
-    libxl_device_vtpm_init(&vtpm_saved);
-    libxl_device_vtpm_copy(CTX, &vtpm_saved, vtpm);
-
-    rc = libxl__device_vtpm_setdefault(gc, vtpm);
-    if (rc) goto out;
-
-    front = flexarray_make(gc, 16, 1);
-    back = flexarray_make(gc, 16, 1);
-
-    if (vtpm->devid == -1) {
-        if ((vtpm->devid = libxl__device_nextid(gc, domid, "vtpm")) < 0) {
-            rc = ERROR_FAIL;
-            goto out;
-        }
-    }
-
-    libxl__update_config_vtpm(gc, &vtpm_saved, vtpm);
-
-    GCNEW(device);
-    rc = libxl__device_from_vtpm(gc, domid, vtpm, device);
-    if ( rc != 0 ) goto out;
-
-    flexarray_append(back, "frontend-id");
-    flexarray_append(back, GCSPRINTF("%d", domid));
-    flexarray_append(back, "online");
-    flexarray_append(back, "1");
-    flexarray_append(back, "state");
-    flexarray_append(back, GCSPRINTF("%d", XenbusStateInitialising));
-    flexarray_append(back, "handle");
-    flexarray_append(back, GCSPRINTF("%d", vtpm->devid));
-
-    flexarray_append(back, "uuid");
-    flexarray_append(back, GCSPRINTF(LIBXL_UUID_FMT, LIBXL_UUID_BYTES(vtpm->uuid)));
-    flexarray_append(back, "resume");
-    flexarray_append(back, "False");
-
-    flexarray_append(front, "backend-id");
-    flexarray_append(front, GCSPRINTF("%d", vtpm->backend_domid));
-    flexarray_append(front, "state");
-    flexarray_append(front, GCSPRINTF("%d", XenbusStateInitialising));
-    flexarray_append(front, "handle");
-    flexarray_append(front, GCSPRINTF("%d", vtpm->devid));
-
-    if (aodev->update_json) {
-        lock = libxl__lock_domain_userdata(gc, domid);
-        if (!lock) {
-            rc = ERROR_LOCK_FAIL;
-            goto out;
-        }
-
-        rc = libxl__get_domain_configuration(gc, domid, &d_config);
-        if (rc) goto out;
-
-        DEVICE_ADD(vtpm, vtpms, domid, &vtpm_saved, COMPARE_DEVID, &d_config);
-
-        rc = libxl__dm_check_start(gc, &d_config, domid);
-        if (rc) goto out;
-    }
-
-    for (;;) {
-        rc = libxl__xs_transaction_start(gc, &t);
-        if (rc) goto out;
-
-        rc = libxl__device_exists(gc, t, device);
-        if (rc < 0) goto out;
-        if (rc == 1) {              /* already exists in xenstore */
-            LOG(ERROR, "device already exists in xenstore");
-            aodev->action = LIBXL__DEVICE_ACTION_ADD; /* for error message */
-            rc = ERROR_DEVICE_EXISTS;
-            goto out;
-        }
-
-        if (aodev->update_json) {
-            rc = libxl__set_domain_configuration(gc, domid, &d_config);
-            if (rc) goto out;
-        }
-
-        libxl__device_generic_add(gc, t, device,
-                                  libxl__xs_kvs_of_flexarray(gc, back,
-                                                             back->count),
-                                  libxl__xs_kvs_of_flexarray(gc, front,
-                                                             front->count),
-                                  NULL);
-
-        rc = libxl__xs_transaction_commit(gc, &t);
-        if (!rc) break;
-        if (rc < 0) goto out;
-    }
-
-    aodev->dev = device;
-    aodev->action = LIBXL__DEVICE_ACTION_ADD;
-    libxl__wait_device_connection(egc, aodev);
-
-    rc = 0;
-out:
-    libxl__xs_transaction_abort(gc, &t);
-    if (lock) libxl__unlock_domain_userdata(lock);
-    libxl_device_vtpm_dispose(&vtpm_saved);
-    libxl_domain_config_dispose(&d_config);
-    aodev->rc = rc;
-    if(rc) aodev->callback(egc, aodev);
-    return;
-}
-
-libxl_device_vtpm *libxl_device_vtpm_list(libxl_ctx *ctx, uint32_t domid, int *num)
-{
-    GC_INIT(ctx);
-
-    libxl_device_vtpm* vtpms = NULL;
-    char *libxl_path;
-    char** dir = NULL;
-    unsigned int ndirs = 0;
-    int rc;
-
-    *num = 0;
-
-    libxl_path = GCSPRINTF("%s/device/vtpm", libxl__xs_libxl_path(gc, domid));
-    dir = libxl__xs_directory(gc, XBT_NULL, libxl_path, &ndirs);
-    if (dir && ndirs) {
-       vtpms = malloc(sizeof(*vtpms) * ndirs);
-       libxl_device_vtpm* vtpm;
-       libxl_device_vtpm* end = vtpms + ndirs;
-       for(vtpm = vtpms; vtpm < end; ++vtpm, ++dir) {
-          char* tmp;
-          const char* be_path = libxl__xs_read(gc, XBT_NULL,
-                GCSPRINTF("%s/%s/backend",
-                   libxl_path, *dir));
-
-          libxl_device_vtpm_init(vtpm);
-
-          vtpm->devid = atoi(*dir);
-
-          rc = libxl__backendpath_parse_domid(gc, be_path,
-                                              &vtpm->backend_domid);
-          if (rc) return NULL;
-
-          tmp = libxl__xs_read(gc, XBT_NULL, GCSPRINTF("%s/uuid", libxl_path));
-          if (tmp) {
-              if(libxl_uuid_from_string(&(vtpm->uuid), tmp)) {
-                  LOG(ERROR, "%s/uuid is a malformed uuid?? (%s) Probably a bug!!\n", be_path, tmp);
-                  free(vtpms);
-                  return NULL;
-              }
-          }
-       }
-    }
-    *num = ndirs;
-
-    GC_FREE;
-    return vtpms;
-}
-
-int libxl_device_vtpm_getinfo(libxl_ctx *ctx,
-                              uint32_t domid,
-                              libxl_device_vtpm *vtpm,
-                              libxl_vtpminfo *vtpminfo)
-{
-    GC_INIT(ctx);
-    char *libxl_path, *dompath, *vtpmpath;
-    char *val;
-    int rc = 0;
-
-    libxl_vtpminfo_init(vtpminfo);
-    dompath = libxl__xs_get_dompath(gc, domid);
-    vtpminfo->devid = vtpm->devid;
-
-    vtpmpath = GCSPRINTF("%s/device/vtpm/%d", dompath, vtpminfo->devid);
-    libxl_path = GCSPRINTF("%s/device/vtpm/%d",
-                           libxl__xs_libxl_path(gc, domid), vtpminfo->devid);
-    vtpminfo->backend = xs_read(ctx->xsh, XBT_NULL,
-          GCSPRINTF("%s/backend", libxl_path), NULL);
-    if (!vtpminfo->backend) {
-        goto err;
-    }
-
-    rc = libxl__backendpath_parse_domid(gc, vtpminfo->backend,
-                                        &vtpminfo->backend_id);
-    if (rc) goto exit;
-
-    val = libxl__xs_read(gc, XBT_NULL,
-          GCSPRINTF("%s/state", vtpmpath));
-    vtpminfo->state = val ? strtoul(val, NULL, 10) : -1;
-
-    val = libxl__xs_read(gc, XBT_NULL,
-          GCSPRINTF("%s/event-channel", vtpmpath));
-    vtpminfo->evtch = val ? strtoul(val, NULL, 10) : -1;
-
-    val = libxl__xs_read(gc, XBT_NULL,
-          GCSPRINTF("%s/ring-ref", vtpmpath));
-    vtpminfo->rref = val ? strtoul(val, NULL, 10) : -1;
-
-    vtpminfo->frontend = xs_read(ctx->xsh, XBT_NULL,
-          GCSPRINTF("%s/frontend", libxl_path), NULL);
-    vtpminfo->frontend_id = domid;
-
-    val = libxl__xs_read(gc, XBT_NULL,
-          GCSPRINTF("%s/uuid", libxl_path));
-    if(val == NULL) {
-       LOG(ERROR, "%s/uuid does not exist!", vtpminfo->backend);
-       goto err;
-    }
-    if(libxl_uuid_from_string(&(vtpminfo->uuid), val)) {
-       LOG(ERROR,
-             "%s/uuid is a malformed uuid?? (%s) Probably a bug!\n",
-             vtpminfo->backend, val);
-       goto err;
-    }
-
-    goto exit;
-err:
-    rc = ERROR_FAIL;
-exit:
-    GC_FREE;
-    return rc;
-}
-
-int libxl_devid_to_device_vtpm(libxl_ctx *ctx,
-                               uint32_t domid,
-                               int devid,
-                               libxl_device_vtpm *vtpm)
-{
-    libxl_device_vtpm *vtpms;
-    int nb, i;
-    int rc;
-
-    vtpms = libxl_device_vtpm_list(ctx, domid, &nb);
-    if (!vtpms)
-        return ERROR_FAIL;
-
-    libxl_device_vtpm_init(vtpm);
-    rc = 1;
-    for (i = 0; i < nb; ++i) {
-        if(devid == vtpms[i].devid) {
-            vtpm->backend_domid = vtpms[i].backend_domid;
-            vtpm->devid = vtpms[i].devid;
-            libxl_uuid_copy(ctx, &vtpm->uuid, &vtpms[i].uuid);
-            rc = 0;
-            break;
-        }
-    }
-
-    libxl_device_vtpm_list_free(vtpms, nb);
-    return rc;
-}
-
-
-/******************************************************************************/
 
 int libxl__device_disk_setdefault(libxl__gc *gc, libxl_device_disk *disk,
                                   uint32_t domid)
@@ -4309,10 +4023,6 @@ out:
  * libxl__add_nics
  * libxl_device_nic_remove
  * libxl_device_nic_destroy
- * libxl_device_vtpm_add
- * libxl__add_vtpms
- * libxl_device_vtpm_remove
- * libxl_device_vtpm_destroy
  * libxl_device_vkb_remove
  * libxl_device_vkb_destroy
  * libxl_device_vfb_remove
@@ -4333,11 +4043,6 @@ LIBXL_DEFINE_DEVICE_ADD(nic)
 LIBXL_DEFINE_DEVICES_ADD(nic)
 LIBXL_DEFINE_DEVICE_REMOVE(nic)
 
-/* vtpm */
-LIBXL_DEFINE_DEVICE_ADD(vtpm)
-static LIBXL_DEFINE_DEVICES_ADD(vtpm)
-LIBXL_DEFINE_DEVICE_REMOVE(vtpm)
-
 /* vkb */
 LIBXL_DEFINE_DEVICE_REMOVE(vkb)
 
@@ -7492,19 +7197,12 @@ static int libxl_device_nic_compare(libxl_device_nic *d1,
     return COMPARE_DEVID(d1, d2);
 }
 
-static int libxl_device_vtpm_compare(libxl_device_vtpm *d1,
-                                     libxl_device_vtpm *d2)
-{
-    return COMPARE_DEVID(d1, d2);
-}
-
 DEFINE_DEVICE_TYPE_STRUCT(disk,
     .merge       = libxl_device_disk_merge,
     .dm_needed   = libxl_device_disk_dm_needed,
     .skip_attach = 1
 );
 DEFINE_DEVICE_TYPE_STRUCT(nic);
-DEFINE_DEVICE_TYPE_STRUCT(vtpm);
 
 /*
  * Local variables:
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index aea14ea..30b6e1e 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -1221,7 +1221,6 @@ _hidden int libxl__device_disk_setdefault(libxl__gc *gc,
                                           uint32_t domid);
 _hidden int libxl__device_nic_setdefault(libxl__gc *gc, libxl_device_nic *nic,
                                          uint32_t domid, bool hotplug);
-_hidden int libxl__device_vtpm_setdefault(libxl__gc *gc, libxl_device_vtpm *vtpm);
 _hidden int libxl__device_vfb_setdefault(libxl__gc *gc, libxl_device_vfb *vfb);
 _hidden int libxl__device_vkb_setdefault(libxl__gc *gc, libxl_device_vkb *vkb);
 _hidden int libxl__device_pci_setdefault(libxl__gc *gc, libxl_device_pci *pci);
diff --git a/tools/libxl/libxl_utils.c b/tools/libxl/libxl_utils.c
index c6933d6..b748555 100644
--- a/tools/libxl/libxl_utils.c
+++ b/tools/libxl/libxl_utils.c
@@ -598,33 +598,6 @@ int libxl_pipe(libxl_ctx *ctx, int pipes[2])
     return ret;
 }
 
-int libxl_uuid_to_device_vtpm(libxl_ctx *ctx, uint32_t domid,
-                            libxl_uuid* uuid, libxl_device_vtpm *vtpm)
-{
-    libxl_device_vtpm *vtpms;
-    int nb, i;
-    int rc;
-
-    vtpms = libxl_device_vtpm_list(ctx, domid, &nb);
-    if (!vtpms)
-        return ERROR_FAIL;
-
-    memset(vtpm, 0, sizeof (libxl_device_vtpm));
-    rc = 1;
-    for (i = 0; i < nb; ++i) {
-        if(!libxl_uuid_compare(uuid, &vtpms[i].uuid)) {
-            vtpm->backend_domid = vtpms[i].backend_domid;
-            vtpm->devid = vtpms[i].devid;
-            libxl_uuid_copy(ctx, &vtpm->uuid, &vtpms[i].uuid);
-            rc = 0;
-            break;
-        }
-    }
-
-    libxl_device_vtpm_list_free(vtpms, nb);
-    return rc;
-}
-
 int libxl_mac_to_device_nic(libxl_ctx *ctx, uint32_t domid,
                             const char *mac, libxl_device_nic *nic)
 {
@@ -1248,22 +1221,6 @@ void libxl_cpupoolinfo_list_free(libxl_cpupoolinfo *list, int nr)
     free(list);
 }
 
-void libxl_vtpminfo_list_free(libxl_vtpminfo* list, int nr)
-{
-   int i;
-   for (i = 0; i < nr; i++)
-      libxl_vtpminfo_dispose(&list[i]);
-   free(list);
-}
-
-void libxl_device_vtpm_list_free(libxl_device_vtpm* list, int nr)
-{
-   int i;
-   for (i = 0; i < nr; i++)
-      libxl_device_vtpm_dispose(&list[i]);
-   free(list);
-}
-
 int libxl_domid_valid_guest(uint32_t domid)
 {
     /* returns 1 if the value _could_ be a valid guest domid, 0 otherwise
diff --git a/tools/libxl/libxl_vtpm.c b/tools/libxl/libxl_vtpm.c
new file mode 100644
index 0000000..cbbeea7
--- /dev/null
+++ b/tools/libxl/libxl_vtpm.c
@@ -0,0 +1,366 @@
+/*
+ * Copyright (C) 2016      SUSE Linux GmbH
+ * Author Juergen Gross <jgross@suse.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; version 2.1 only. with the special
+ * exception on linking described in file LICENSE.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ */
+
+#include "libxl_osdeps.h"
+
+#include "libxl_internal.h"
+
+static int libxl__device_vtpm_setdefault(libxl__gc *gc, libxl_device_vtpm *vtpm)
+{
+    int rc;
+    if (libxl_uuid_is_nil(&vtpm->uuid)) {
+        libxl_uuid_generate(&vtpm->uuid);
+    }
+    rc = libxl__resolve_domid(gc, vtpm->backend_domname, &vtpm->backend_domid);
+    return rc;
+}
+
+static int libxl__device_from_vtpm(libxl__gc *gc, uint32_t domid,
+                                   libxl_device_vtpm *vtpm,
+                                   libxl__device *device)
+{
+   device->backend_devid   = vtpm->devid;
+   device->backend_domid   = vtpm->backend_domid;
+   device->backend_kind    = LIBXL__DEVICE_KIND_VTPM;
+   device->devid           = vtpm->devid;
+   device->domid           = domid;
+   device->kind            = LIBXL__DEVICE_KIND_VTPM;
+
+   return 0;
+}
+
+static void libxl__device_vtpm_add(libxl__egc *egc, uint32_t domid,
+                                   libxl_device_vtpm *vtpm,
+                                   libxl__ao_device *aodev)
+{
+    STATE_AO_GC(aodev->ao);
+    flexarray_t *front;
+    flexarray_t *back;
+    libxl__device *device;
+    int rc;
+    xs_transaction_t t = XBT_NULL;
+    libxl_domain_config d_config;
+    libxl_device_vtpm vtpm_saved;
+    libxl__domain_userdata_lock *lock = NULL;
+
+    libxl_domain_config_init(&d_config);
+    libxl_device_vtpm_init(&vtpm_saved);
+    libxl_device_vtpm_copy(CTX, &vtpm_saved, vtpm);
+
+    rc = libxl__device_vtpm_setdefault(gc, vtpm);
+    if (rc) goto out;
+
+    front = flexarray_make(gc, 16, 1);
+    back = flexarray_make(gc, 16, 1);
+
+    if (vtpm->devid == -1) {
+        if ((vtpm->devid = libxl__device_nextid(gc, domid, "vtpm")) < 0) {
+            rc = ERROR_FAIL;
+            goto out;
+        }
+    }
+
+    libxl__update_config_vtpm(gc, &vtpm_saved, vtpm);
+
+    GCNEW(device);
+    rc = libxl__device_from_vtpm(gc, domid, vtpm, device);
+    if ( rc != 0 ) goto out;
+
+    flexarray_append(back, "frontend-id");
+    flexarray_append(back, GCSPRINTF("%d", domid));
+    flexarray_append(back, "online");
+    flexarray_append(back, "1");
+    flexarray_append(back, "state");
+    flexarray_append(back, GCSPRINTF("%d", XenbusStateInitialising));
+    flexarray_append(back, "handle");
+    flexarray_append(back, GCSPRINTF("%d", vtpm->devid));
+
+    flexarray_append(back, "uuid");
+    flexarray_append(back, GCSPRINTF(LIBXL_UUID_FMT, LIBXL_UUID_BYTES(vtpm->uuid)));
+    flexarray_append(back, "resume");
+    flexarray_append(back, "False");
+
+    flexarray_append(front, "backend-id");
+    flexarray_append(front, GCSPRINTF("%d", vtpm->backend_domid));
+    flexarray_append(front, "state");
+    flexarray_append(front, GCSPRINTF("%d", XenbusStateInitialising));
+    flexarray_append(front, "handle");
+    flexarray_append(front, GCSPRINTF("%d", vtpm->devid));
+
+    if (aodev->update_json) {
+        lock = libxl__lock_domain_userdata(gc, domid);
+        if (!lock) {
+            rc = ERROR_LOCK_FAIL;
+            goto out;
+        }
+
+        rc = libxl__get_domain_configuration(gc, domid, &d_config);
+        if (rc) goto out;
+
+        DEVICE_ADD(vtpm, vtpms, domid, &vtpm_saved, COMPARE_DEVID, &d_config);
+
+        rc = libxl__dm_check_start(gc, &d_config, domid);
+        if (rc) goto out;
+    }
+
+    for (;;) {
+        rc = libxl__xs_transaction_start(gc, &t);
+        if (rc) goto out;
+
+        rc = libxl__device_exists(gc, t, device);
+        if (rc < 0) goto out;
+        if (rc == 1) {              /* already exists in xenstore */
+            LOG(ERROR, "device already exists in xenstore");
+            aodev->action = LIBXL__DEVICE_ACTION_ADD; /* for error message */
+            rc = ERROR_DEVICE_EXISTS;
+            goto out;
+        }
+
+        if (aodev->update_json) {
+            rc = libxl__set_domain_configuration(gc, domid, &d_config);
+            if (rc) goto out;
+        }
+
+        libxl__device_generic_add(gc, t, device,
+                                  libxl__xs_kvs_of_flexarray(gc, back,
+                                                             back->count),
+                                  libxl__xs_kvs_of_flexarray(gc, front,
+                                                             front->count),
+                                  NULL);
+
+        rc = libxl__xs_transaction_commit(gc, &t);
+        if (!rc) break;
+        if (rc < 0) goto out;
+    }
+
+    aodev->dev = device;
+    aodev->action = LIBXL__DEVICE_ACTION_ADD;
+    libxl__wait_device_connection(egc, aodev);
+
+    rc = 0;
+out:
+    libxl__xs_transaction_abort(gc, &t);
+    if (lock) libxl__unlock_domain_userdata(lock);
+    libxl_device_vtpm_dispose(&vtpm_saved);
+    libxl_domain_config_dispose(&d_config);
+    aodev->rc = rc;
+    if(rc) aodev->callback(egc, aodev);
+    return;
+}
+
+libxl_device_vtpm *libxl_device_vtpm_list(libxl_ctx *ctx, uint32_t domid, int *num)
+{
+    GC_INIT(ctx);
+
+    libxl_device_vtpm* vtpms = NULL;
+    char *libxl_path;
+    char** dir = NULL;
+    unsigned int ndirs = 0;
+    int rc;
+
+    *num = 0;
+
+    libxl_path = GCSPRINTF("%s/device/vtpm", libxl__xs_libxl_path(gc, domid));
+    dir = libxl__xs_directory(gc, XBT_NULL, libxl_path, &ndirs);
+    if (dir && ndirs) {
+       vtpms = malloc(sizeof(*vtpms) * ndirs);
+       libxl_device_vtpm* vtpm;
+       libxl_device_vtpm* end = vtpms + ndirs;
+       for(vtpm = vtpms; vtpm < end; ++vtpm, ++dir) {
+          char* tmp;
+          const char* be_path = libxl__xs_read(gc, XBT_NULL,
+                GCSPRINTF("%s/%s/backend",
+                   libxl_path, *dir));
+
+          libxl_device_vtpm_init(vtpm);
+
+          vtpm->devid = atoi(*dir);
+
+          rc = libxl__backendpath_parse_domid(gc, be_path,
+                                              &vtpm->backend_domid);
+          if (rc) return NULL;
+
+          tmp = libxl__xs_read(gc, XBT_NULL, GCSPRINTF("%s/uuid", libxl_path));
+          if (tmp) {
+              if(libxl_uuid_from_string(&(vtpm->uuid), tmp)) {
+                  LOG(ERROR, "%s/uuid is a malformed uuid?? (%s) Probably a bug!!\n", be_path, tmp);
+                  free(vtpms);
+                  return NULL;
+              }
+          }
+       }
+    }
+    *num = ndirs;
+
+    GC_FREE;
+    return vtpms;
+}
+
+int libxl_device_vtpm_getinfo(libxl_ctx *ctx,
+                              uint32_t domid,
+                              libxl_device_vtpm *vtpm,
+                              libxl_vtpminfo *vtpminfo)
+{
+    GC_INIT(ctx);
+    char *libxl_path, *dompath, *vtpmpath;
+    char *val;
+    int rc = 0;
+
+    libxl_vtpminfo_init(vtpminfo);
+    dompath = libxl__xs_get_dompath(gc, domid);
+    vtpminfo->devid = vtpm->devid;
+
+    vtpmpath = GCSPRINTF("%s/device/vtpm/%d", dompath, vtpminfo->devid);
+    libxl_path = GCSPRINTF("%s/device/vtpm/%d",
+                           libxl__xs_libxl_path(gc, domid), vtpminfo->devid);
+    vtpminfo->backend = xs_read(ctx->xsh, XBT_NULL,
+          GCSPRINTF("%s/backend", libxl_path), NULL);
+    if (!vtpminfo->backend) {
+        goto err;
+    }
+
+    rc = libxl__backendpath_parse_domid(gc, vtpminfo->backend,
+                                        &vtpminfo->backend_id);
+    if (rc) goto exit;
+
+    val = libxl__xs_read(gc, XBT_NULL,
+          GCSPRINTF("%s/state", vtpmpath));
+    vtpminfo->state = val ? strtoul(val, NULL, 10) : -1;
+
+    val = libxl__xs_read(gc, XBT_NULL,
+          GCSPRINTF("%s/event-channel", vtpmpath));
+    vtpminfo->evtch = val ? strtoul(val, NULL, 10) : -1;
+
+    val = libxl__xs_read(gc, XBT_NULL,
+          GCSPRINTF("%s/ring-ref", vtpmpath));
+    vtpminfo->rref = val ? strtoul(val, NULL, 10) : -1;
+
+    vtpminfo->frontend = xs_read(ctx->xsh, XBT_NULL,
+          GCSPRINTF("%s/frontend", libxl_path), NULL);
+    vtpminfo->frontend_id = domid;
+
+    val = libxl__xs_read(gc, XBT_NULL,
+          GCSPRINTF("%s/uuid", libxl_path));
+    if(val == NULL) {
+       LOG(ERROR, "%s/uuid does not exist!", vtpminfo->backend);
+       goto err;
+    }
+    if(libxl_uuid_from_string(&(vtpminfo->uuid), val)) {
+       LOG(ERROR,
+             "%s/uuid is a malformed uuid?? (%s) Probably a bug!\n",
+             vtpminfo->backend, val);
+       goto err;
+    }
+
+    goto exit;
+err:
+    rc = ERROR_FAIL;
+exit:
+    GC_FREE;
+    return rc;
+}
+
+int libxl_devid_to_device_vtpm(libxl_ctx *ctx,
+                               uint32_t domid,
+                               int devid,
+                               libxl_device_vtpm *vtpm)
+{
+    libxl_device_vtpm *vtpms;
+    int nb, i;
+    int rc;
+
+    vtpms = libxl_device_vtpm_list(ctx, domid, &nb);
+    if (!vtpms)
+        return ERROR_FAIL;
+
+    libxl_device_vtpm_init(vtpm);
+    rc = 1;
+    for (i = 0; i < nb; ++i) {
+        if(devid == vtpms[i].devid) {
+            vtpm->backend_domid = vtpms[i].backend_domid;
+            vtpm->devid = vtpms[i].devid;
+            libxl_uuid_copy(ctx, &vtpm->uuid, &vtpms[i].uuid);
+            rc = 0;
+            break;
+        }
+    }
+
+    libxl_device_vtpm_list_free(vtpms, nb);
+    return rc;
+}
+
+static int libxl_device_vtpm_compare(libxl_device_vtpm *d1,
+                                     libxl_device_vtpm *d2)
+{
+    return COMPARE_DEVID(d1, d2);
+}
+
+int libxl_uuid_to_device_vtpm(libxl_ctx *ctx, uint32_t domid,
+                            libxl_uuid* uuid, libxl_device_vtpm *vtpm)
+{
+    libxl_device_vtpm *vtpms;
+    int nb, i;
+    int rc;
+
+    vtpms = libxl_device_vtpm_list(ctx, domid, &nb);
+    if (!vtpms)
+        return ERROR_FAIL;
+
+    memset(vtpm, 0, sizeof (libxl_device_vtpm));
+    rc = 1;
+    for (i = 0; i < nb; ++i) {
+        if(!libxl_uuid_compare(uuid, &vtpms[i].uuid)) {
+            vtpm->backend_domid = vtpms[i].backend_domid;
+            vtpm->devid = vtpms[i].devid;
+            libxl_uuid_copy(ctx, &vtpm->uuid, &vtpms[i].uuid);
+            rc = 0;
+            break;
+        }
+    }
+
+    libxl_device_vtpm_list_free(vtpms, nb);
+    return rc;
+}
+
+void libxl_vtpminfo_list_free(libxl_vtpminfo* list, int nr)
+{
+   int i;
+   for (i = 0; i < nr; i++)
+      libxl_vtpminfo_dispose(&list[i]);
+   free(list);
+}
+
+void libxl_device_vtpm_list_free(libxl_device_vtpm* list, int nr)
+{
+   int i;
+   for (i = 0; i < nr; i++)
+      libxl_device_vtpm_dispose(&list[i]);
+   free(list);
+}
+
+LIBXL_DEFINE_DEVICE_ADD(vtpm)
+static LIBXL_DEFINE_DEVICES_ADD(vtpm)
+LIBXL_DEFINE_DEVICE_REMOVE(vtpm)
+
+DEFINE_DEVICE_TYPE_STRUCT(vtpm);
+
+/*
+ * Local variables:
+ * mode: C
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
+
-- 
2.6.6


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH 5/6] libxl: add config update callback to device type framework
  2016-07-12 15:30 [PATCH 0/6] libxl: extend device type framework Juergen Gross
                   ` (3 preceding siblings ...)
  2016-07-12 15:30 ` [PATCH 4/6] libxl: split libxl vtpm code into one source Juergen Gross
@ 2016-07-12 15:30 ` Juergen Gross
  2016-07-25 10:46   ` Wei Liu
  2016-07-12 15:30 ` [PATCH 6/6] libxl: move common nic stuff into one source Juergen Gross
                   ` (2 subsequent siblings)
  7 siblings, 1 reply; 18+ messages in thread
From: Juergen Gross @ 2016-07-12 15:30 UTC (permalink / raw)
  To: xen-devel; +Cc: Juergen Gross, wei.liu2, ian.jackson

Some device types require a configuration update after resume of
domain. Add a callback for this purpose.

Signed-off-by: Juergen Gross <jgross@suse.com>
---
 tools/libxl/libxl.c          | 17 ++++++++++++++++-
 tools/libxl/libxl_internal.c | 21 ++++++++++++++-------
 tools/libxl/libxl_internal.h | 17 +----------------
 tools/libxl/libxl_vtpm.c     | 16 +++++++++++++++-
 4 files changed, 46 insertions(+), 25 deletions(-)

diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c
index 7ada4a7..03e5a58 100644
--- a/tools/libxl/libxl.c
+++ b/tools/libxl/libxl.c
@@ -3147,6 +3147,14 @@ static int libxl__device_from_nic(libxl__gc *gc, uint32_t domid,
     return 0;
 }
 
+static void libxl__update_config_nic(libxl__gc *gc, libxl_device_nic *dst,
+                                     const libxl_device_nic *src)
+{
+    dst->devid = src->devid;
+    dst->nictype = src->nictype;
+    libxl_mac_copy(CTX, &dst->mac, &src->mac);
+}
+
 static void libxl__device_nic_add(libxl__egc *egc, uint32_t domid,
                                   libxl_device_nic *nic,
                                   libxl__ao_device *aodev)
@@ -7197,12 +7205,19 @@ static int libxl_device_nic_compare(libxl_device_nic *d1,
     return COMPARE_DEVID(d1, d2);
 }
 
+static void libxl_device_nic_update_config(libxl__gc *gc, void *d, void *s)
+{
+    libxl__update_config_nic(gc, d, s);
+}
+
 DEFINE_DEVICE_TYPE_STRUCT(disk,
     .merge       = libxl_device_disk_merge,
     .dm_needed   = libxl_device_disk_dm_needed,
     .skip_attach = 1
 );
-DEFINE_DEVICE_TYPE_STRUCT(nic);
+DEFINE_DEVICE_TYPE_STRUCT(nic,
+    .update_config = libxl_device_nic_update_config
+);
 
 /*
  * Local variables:
diff --git a/tools/libxl/libxl_internal.c b/tools/libxl/libxl_internal.c
index 3b30f8a..448dd61 100644
--- a/tools/libxl/libxl_internal.c
+++ b/tools/libxl/libxl_internal.c
@@ -537,15 +537,22 @@ void libxl__update_domain_configuration(libxl__gc *gc,
                                         libxl_domain_config *dst,
                                         const libxl_domain_config *src)
 {
-    int i;
+    int i, idx, num;
+    const struct libxl_device_type *dt;
 
-    /* update network interface information */
-    for (i = 0; i < src->num_nics; i++)
-        libxl__update_config_nic(gc, &dst->nics[i], &src->nics[i]);
+    for (idx = 0;; idx++) {
+        dt = device_type_tbl[idx];
+        if (!dt)
+            break;
 
-    /* update vtpm information */
-    for (i = 0; i < src->num_vtpms; i++)
-        libxl__update_config_vtpm(gc, &dst->vtpms[i], &src->vtpms[i]);
+        num = *libxl__device_type_get_num(dt, src);
+        if (!dt->update_config || !num)
+            continue;
+
+        for (i = 0; i < num; i++)
+            dt->update_config(gc, libxl__device_type_get_elem(dt, dst, i),
+                                  libxl__device_type_get_elem(dt, src, i));
+    }
 
     /* update guest UUID */
     libxl_uuid_copy(CTX, &dst->c_info.uuid, &src->c_info.uuid);
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index 30b6e1e..cdc23e2 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -3451,6 +3451,7 @@ struct libxl_device_type {
     int (*compare)(void *, void *);
     void (*merge)(libxl_ctx *, void *, void *);
     int (*dm_needed)(void *, unsigned);
+    void (*update_config)(libxl__gc *, void *, void *);
 };
 
 #define DEFINE_DEVICE_TYPE_STRUCT_X(name, sname, ...)                          \
@@ -4192,22 +4193,6 @@ int libxl__set_domain_configuration(libxl__gc *gc, uint32_t domid,
 void libxl__update_domain_configuration(libxl__gc *gc,
                                         libxl_domain_config *dst,
                                         const libxl_domain_config *src);
-static inline void libxl__update_config_nic(libxl__gc *gc,
-                                            libxl_device_nic *dst,
-                                            const libxl_device_nic *src)
-{
-    dst->devid = src->devid;
-    dst->nictype = src->nictype;
-    libxl_mac_copy(CTX, &dst->mac, &src->mac);
-}
-
-static inline void libxl__update_config_vtpm(libxl__gc *gc,
-                                             libxl_device_vtpm *dst,
-                                             libxl_device_vtpm *src)
-{
-    dst->devid = src->devid;
-    libxl_uuid_copy(CTX, &dst->uuid, &src->uuid);
-}
 
 /* Target memory in xenstore is different from what user has
  * asked for. The difference is video_memkb + (possible) fudge.
diff --git a/tools/libxl/libxl_vtpm.c b/tools/libxl/libxl_vtpm.c
index cbbeea7..29a0817 100644
--- a/tools/libxl/libxl_vtpm.c
+++ b/tools/libxl/libxl_vtpm.c
@@ -41,6 +41,13 @@ static int libxl__device_from_vtpm(libxl__gc *gc, uint32_t domid,
    return 0;
 }
 
+static void libxl__update_config_vtpm(libxl__gc *gc, libxl_device_vtpm *dst,
+                                      libxl_device_vtpm *src)
+{
+    dst->devid = src->devid;
+    libxl_uuid_copy(CTX, &dst->uuid, &src->uuid);
+}
+
 static void libxl__device_vtpm_add(libxl__egc *egc, uint32_t domid,
                                    libxl_device_vtpm *vtpm,
                                    libxl__ao_device *aodev)
@@ -350,11 +357,18 @@ void libxl_device_vtpm_list_free(libxl_device_vtpm* list, int nr)
    free(list);
 }
 
+static void libxl_device_vtpm_update_config(libxl__gc *gc, void *d, void *s)
+{
+    libxl__update_config_vtpm(gc, d, s);
+}
+
 LIBXL_DEFINE_DEVICE_ADD(vtpm)
 static LIBXL_DEFINE_DEVICES_ADD(vtpm)
 LIBXL_DEFINE_DEVICE_REMOVE(vtpm)
 
-DEFINE_DEVICE_TYPE_STRUCT(vtpm);
+DEFINE_DEVICE_TYPE_STRUCT(vtpm,
+    .update_config = libxl_device_vtpm_update_config
+);
 
 /*
  * Local variables:
-- 
2.6.6


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH 6/6] libxl: move common nic stuff into one source
  2016-07-12 15:30 [PATCH 0/6] libxl: extend device type framework Juergen Gross
                   ` (4 preceding siblings ...)
  2016-07-12 15:30 ` [PATCH 5/6] libxl: add config update callback to device type framework Juergen Gross
@ 2016-07-12 15:30 ` Juergen Gross
  2016-07-25 10:46   ` Wei Liu
  2016-07-21 13:12 ` [PATCH 0/6] libxl: extend device type framework Juergen Gross
  2016-07-27 11:45 ` Wei Liu
  7 siblings, 1 reply; 18+ messages in thread
From: Juergen Gross @ 2016-07-12 15:30 UTC (permalink / raw)
  To: xen-devel; +Cc: Juergen Gross, wei.liu2, ian.jackson

Put all nic related stuff of libxl form common files into a dedicated
source file.

Signed-off-by: Juergen Gross <jgross@suse.com>
---
 tools/libxl/Makefile         |   2 +-
 tools/libxl/libxl.c          | 474 +-----------------------------------
 tools/libxl/libxl_create.c   |  23 +-
 tools/libxl/libxl_internal.h |   5 +-
 tools/libxl/libxl_nic.c      | 554 +++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_utils.c    |  35 ---
 6 files changed, 573 insertions(+), 520 deletions(-)
 create mode 100644 tools/libxl/libxl_nic.c

diff --git a/tools/libxl/Makefile b/tools/libxl/Makefile
index 86c11bf..14a1a8e 100644
--- a/tools/libxl/Makefile
+++ b/tools/libxl/Makefile
@@ -115,7 +115,7 @@ LIBXL_OBJS = flexarray.o libxl.o libxl_create.o libxl_dm.o libxl_pci.o \
 			libxl_save_callout.o _libxl_save_msgs_callout.o \
 			libxl_qmp.o libxl_event.o libxl_fork.o \
 			libxl_dom_suspend.o libxl_dom_save.o libxl_pvusb.o \
-			libxl_vtpm.o \
+			libxl_vtpm.o libxl_nic.o \
                         $(LIBXL_OBJS-y)
 LIBXL_OBJS += libxl_genid.o
 LIBXL_OBJS += _libxl_types.o libxl_flask.o _libxl_types_internal.o
diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c
index 03e5a58..58d80b7 100644
--- a/tools/libxl/libxl.c
+++ b/tools/libxl/libxl.c
@@ -21,15 +21,6 @@
 #define PAGE_TO_MEMKB(pages) ((pages) * 4)
 #define BACKEND_STRING_SIZE 5
 
-/* Utility to read /libxl xenstore keys, from libxl_path */
-#define READ_LIBXLDEV(tgc, subpath) ({                                  \
-        rc = libxl__xs_read_checked(tgc, XBT_NULL,                      \
-                                    GCSPRINTF("%s/" subpath, libxl_path),  \
-                                    &tmp);                              \
-        if (rc) goto out;                                               \
-        (char*)tmp;                                                     \
-    });
-
 int libxl_ctx_alloc(libxl_ctx **pctx, int version,
                     unsigned flags, xentoollog_logger * lg)
 {
@@ -3069,433 +3060,6 @@ out:
 }
 
 /******************************************************************************/
-
-int libxl__device_nic_setdefault(libxl__gc *gc, libxl_device_nic *nic,
-                                 uint32_t domid, bool hotplug)
-{
-    int rc;
-
-    if (!nic->mtu)
-        nic->mtu = 1492;
-    if (!nic->model) {
-        nic->model = strdup("rtl8139");
-        if (!nic->model) return ERROR_NOMEM;
-    }
-    if (libxl__mac_is_default(&nic->mac)) {
-        const uint8_t *r;
-        libxl_uuid uuid;
-
-        libxl_uuid_generate(&uuid);
-        r = libxl_uuid_bytearray(&uuid);
-
-        nic->mac[0] = 0x00;
-        nic->mac[1] = 0x16;
-        nic->mac[2] = 0x3e;
-        nic->mac[3] = r[0] & 0x7f;
-        nic->mac[4] = r[1];
-        nic->mac[5] = r[2];
-    }
-    if (!nic->bridge) {
-        nic->bridge = strdup("xenbr0");
-        if (!nic->bridge) return ERROR_NOMEM;
-    }
-    if ( !nic->script && asprintf(&nic->script, "%s/vif-bridge",
-                                  libxl__xen_script_dir_path()) < 0 )
-        return ERROR_FAIL;
-
-    rc = libxl__resolve_domid(gc, nic->backend_domname, &nic->backend_domid);
-    if (rc < 0) return rc;
-
-    switch (libxl__domain_type(gc, domid)) {
-    case LIBXL_DOMAIN_TYPE_HVM:
-        if (!nic->nictype) {
-            if (hotplug ||
-                (libxl__device_model_version_running(gc, domid) ==
-                 LIBXL_DEVICE_MODEL_VERSION_NONE))
-                nic->nictype = LIBXL_NIC_TYPE_VIF;
-            else
-                nic->nictype = LIBXL_NIC_TYPE_VIF_IOEMU;
-        }
-        break;
-    case LIBXL_DOMAIN_TYPE_PV:
-        if (nic->nictype == LIBXL_NIC_TYPE_VIF_IOEMU) {
-            LOG(ERROR, "trying to create PV guest with an emulated interface");
-            return ERROR_INVAL;
-        }
-        nic->nictype = LIBXL_NIC_TYPE_VIF;
-        break;
-    case LIBXL_DOMAIN_TYPE_INVALID:
-        return ERROR_FAIL;
-    default:
-        abort();
-    }
-
-    return rc;
-}
-
-static int libxl__device_from_nic(libxl__gc *gc, uint32_t domid,
-                                  libxl_device_nic *nic,
-                                  libxl__device *device)
-{
-    device->backend_devid    = nic->devid;
-    device->backend_domid    = nic->backend_domid;
-    device->backend_kind     = LIBXL__DEVICE_KIND_VIF;
-    device->devid            = nic->devid;
-    device->domid            = domid;
-    device->kind             = LIBXL__DEVICE_KIND_VIF;
-
-    return 0;
-}
-
-static void libxl__update_config_nic(libxl__gc *gc, libxl_device_nic *dst,
-                                     const libxl_device_nic *src)
-{
-    dst->devid = src->devid;
-    dst->nictype = src->nictype;
-    libxl_mac_copy(CTX, &dst->mac, &src->mac);
-}
-
-static void libxl__device_nic_add(libxl__egc *egc, uint32_t domid,
-                                  libxl_device_nic *nic,
-                                  libxl__ao_device *aodev)
-{
-    STATE_AO_GC(aodev->ao);
-    flexarray_t *front;
-    flexarray_t *back;
-    libxl__device *device;
-    int rc;
-    xs_transaction_t t = XBT_NULL;
-    libxl_domain_config d_config;
-    libxl_device_nic nic_saved;
-    libxl__domain_userdata_lock *lock = NULL;
-
-    libxl_domain_config_init(&d_config);
-    libxl_device_nic_init(&nic_saved);
-    libxl_device_nic_copy(CTX, &nic_saved, nic);
-
-    rc = libxl__device_nic_setdefault(gc, nic, domid, aodev->update_json);
-    if (rc) goto out;
-
-    front = flexarray_make(gc, 16, 1);
-    back = flexarray_make(gc, 18, 1);
-
-    if (nic->devid == -1) {
-        if ((nic->devid = libxl__device_nextid(gc, domid, "vif")) < 0) {
-            rc = ERROR_FAIL;
-            goto out;
-        }
-    }
-
-    libxl__update_config_nic(gc, &nic_saved, nic);
-
-    GCNEW(device);
-    rc = libxl__device_from_nic(gc, domid, nic, device);
-    if ( rc != 0 ) goto out;
-
-    flexarray_append(back, "frontend-id");
-    flexarray_append(back, GCSPRINTF("%d", domid));
-    flexarray_append(back, "online");
-    flexarray_append(back, "1");
-    flexarray_append(back, "state");
-    flexarray_append(back, GCSPRINTF("%d", XenbusStateInitialising));
-    if (nic->script)
-        flexarray_append_pair(back, "script",
-                              libxl__abs_path(gc, nic->script,
-                                              libxl__xen_script_dir_path()));
-
-    if (nic->ifname) {
-        flexarray_append(back, "vifname");
-        flexarray_append(back, nic->ifname);
-    }
-
-    if (nic->coloft_forwarddev) {
-        flexarray_append(back, "forwarddev");
-        flexarray_append(back, nic->coloft_forwarddev);
-    }
-
-    flexarray_append(back, "mac");
-    flexarray_append(back,GCSPRINTF(LIBXL_MAC_FMT, LIBXL_MAC_BYTES(nic->mac)));
-    if (nic->ip) {
-        flexarray_append(back, "ip");
-        flexarray_append(back, libxl__strdup(gc, nic->ip));
-    }
-    if (nic->gatewaydev) {
-        flexarray_append(back, "gatewaydev");
-        flexarray_append(back, libxl__strdup(gc, nic->gatewaydev));
-    }
-
-    if (nic->rate_interval_usecs > 0) {
-        flexarray_append(back, "rate");
-        flexarray_append(back, GCSPRINTF("%"PRIu64",%"PRIu32"",
-                            nic->rate_bytes_per_interval,
-                            nic->rate_interval_usecs));
-    }
-
-    flexarray_append(back, "bridge");
-    flexarray_append(back, libxl__strdup(gc, nic->bridge));
-    flexarray_append(back, "handle");
-    flexarray_append(back, GCSPRINTF("%d", nic->devid));
-    flexarray_append(back, "type");
-    flexarray_append(back, libxl__strdup(gc,
-                                     libxl_nic_type_to_string(nic->nictype)));
-
-    flexarray_append(front, "backend-id");
-    flexarray_append(front, GCSPRINTF("%d", nic->backend_domid));
-    flexarray_append(front, "state");
-    flexarray_append(front, GCSPRINTF("%d", XenbusStateInitialising));
-    flexarray_append(front, "handle");
-    flexarray_append(front, GCSPRINTF("%d", nic->devid));
-    flexarray_append(front, "mac");
-    flexarray_append(front, GCSPRINTF(
-                                    LIBXL_MAC_FMT, LIBXL_MAC_BYTES(nic->mac)));
-
-    if (aodev->update_json) {
-        lock = libxl__lock_domain_userdata(gc, domid);
-        if (!lock) {
-            rc = ERROR_LOCK_FAIL;
-            goto out;
-        }
-
-        rc = libxl__get_domain_configuration(gc, domid, &d_config);
-        if (rc) goto out;
-
-        DEVICE_ADD(nic, nics, domid, &nic_saved, COMPARE_DEVID, &d_config);
-
-        rc = libxl__dm_check_start(gc, &d_config, domid);
-        if (rc) goto out;
-    }
-
-    for (;;) {
-        rc = libxl__xs_transaction_start(gc, &t);
-        if (rc) goto out;
-
-        rc = libxl__device_exists(gc, t, device);
-        if (rc < 0) goto out;
-        if (rc == 1) {              /* already exists in xenstore */
-            LOG(ERROR, "device already exists in xenstore");
-            aodev->action = LIBXL__DEVICE_ACTION_ADD; /* for error message */
-            rc = ERROR_DEVICE_EXISTS;
-            goto out;
-        }
-
-        if (aodev->update_json) {
-            rc = libxl__set_domain_configuration(gc, domid, &d_config);
-            if (rc) goto out;
-        }
-
-        libxl__device_generic_add(gc, t, device,
-                                  libxl__xs_kvs_of_flexarray(gc, back,
-                                                             back->count),
-                                  libxl__xs_kvs_of_flexarray(gc, front,
-                                                             front->count),
-                                  NULL);
-
-        rc = libxl__xs_transaction_commit(gc, &t);
-        if (!rc) break;
-        if (rc < 0) goto out;
-    }
-
-    aodev->dev = device;
-    aodev->action = LIBXL__DEVICE_ACTION_ADD;
-    libxl__wait_device_connection(egc, aodev);
-
-    rc = 0;
-out:
-    libxl__xs_transaction_abort(gc, &t);
-    if (lock) libxl__unlock_domain_userdata(lock);
-    libxl_device_nic_dispose(&nic_saved);
-    libxl_domain_config_dispose(&d_config);
-    aodev->rc = rc;
-    if (rc) aodev->callback(egc, aodev);
-    return;
-}
-
-static int libxl__device_nic_from_xenstore(libxl__gc *gc,
-                                        const char *libxl_path,
-                                        libxl_device_nic *nic)
-{
-    const char *tmp;
-    int rc;
-
-    libxl_device_nic_init(nic);
-
-    tmp = READ_LIBXLDEV(gc, "handle");
-    if (tmp)
-        nic->devid = atoi(tmp);
-    else
-        nic->devid = 0;
-
-    /* nic->mtu = */
-
-    tmp = READ_LIBXLDEV(gc, "mac");
-    if (tmp) {
-        rc = libxl__parse_mac(tmp, nic->mac);
-        if (rc) goto out;
-    } else {
-        memset(nic->mac, 0, sizeof(nic->mac));
-    }
-
-    nic->ip = READ_LIBXLDEV(NOGC, "ip");
-    nic->bridge = READ_LIBXLDEV(NOGC, "bridge");
-    nic->script = READ_LIBXLDEV(NOGC, "script");
-    nic->coloft_forwarddev = READ_LIBXLDEV(NOGC, "forwarddev");
-
-    /* vif_ioemu nics use the same xenstore entries as vif interfaces */
-    tmp = READ_LIBXLDEV(gc, "type");
-    if (tmp) {
-        rc = libxl_nic_type_from_string(tmp, &nic->nictype);
-        if (rc) goto out;
-    } else {
-        nic->nictype = LIBXL_NIC_TYPE_VIF;
-    }
-    nic->model = NULL; /* XXX Only for TYPE_IOEMU */
-    nic->ifname = NULL; /* XXX Only for TYPE_IOEMU */
-
-    rc = 0;
- out:
-    return rc;
-}
-
-int libxl_devid_to_device_nic(libxl_ctx *ctx, uint32_t domid,
-                              int devid, libxl_device_nic *nic)
-{
-    GC_INIT(ctx);
-    char *libxl_dom_path, *libxl_path;
-    int rc = ERROR_FAIL;
-
-    libxl_device_nic_init(nic);
-    libxl_dom_path = libxl__xs_libxl_path(gc, domid);
-    if (!libxl_dom_path)
-        goto out;
-
-    libxl_path = GCSPRINTF("%s/device/vif/%d", libxl_dom_path, devid);
-
-    rc = libxl__device_nic_from_xenstore(gc, libxl_path, nic);
-    if (rc) goto out;
-
-    rc = 0;
-out:
-    GC_FREE;
-    return rc;
-}
-
-static int libxl__append_nic_list(libxl__gc *gc,
-                                           uint32_t domid,
-                                           libxl_device_nic **nics,
-                                           int *nnics)
-{
-    char *libxl_dir_path = NULL;
-    char **dir = NULL;
-    unsigned int n = 0;
-    libxl_device_nic *pnic = NULL, *pnic_end = NULL;
-    int rc;
-
-    libxl_dir_path = GCSPRINTF("%s/device/vif",
-                               libxl__xs_libxl_path(gc, domid));
-    dir = libxl__xs_directory(gc, XBT_NULL, libxl_dir_path, &n);
-    if (dir && n) {
-        libxl_device_nic *tmp;
-        tmp = realloc(*nics, sizeof (libxl_device_nic) * (*nnics + n));
-        if (tmp == NULL)
-            return ERROR_NOMEM;
-        *nics = tmp;
-        pnic = *nics + *nnics;
-        pnic_end = *nics + *nnics + n;
-        for (; pnic < pnic_end; pnic++, dir++) {
-            const char *p;
-            p = GCSPRINTF("%s/%s", libxl_dir_path, *dir);
-            rc = libxl__device_nic_from_xenstore(gc, p, pnic);
-            if (rc) goto out;
-        }
-        *nnics += n;
-    }
-    return 0;
-
- out:
-    return rc;
-}
-
-libxl_device_nic *libxl_device_nic_list(libxl_ctx *ctx, uint32_t domid, int *num)
-{
-    GC_INIT(ctx);
-    libxl_device_nic *nics = NULL;
-    int rc;
-
-    *num = 0;
-
-    rc = libxl__append_nic_list(gc, domid, &nics, num);
-    if (rc) goto out_err;
-
-    GC_FREE;
-    return nics;
-
-out_err:
-    LOG(ERROR, "Unable to list nics");
-    while (*num) {
-        (*num)--;
-        libxl_device_nic_dispose(&nics[*num]);
-    }
-    free(nics);
-    return NULL;
-}
-
-int libxl_device_nic_getinfo(libxl_ctx *ctx, uint32_t domid,
-                              libxl_device_nic *nic, libxl_nicinfo *nicinfo)
-{
-    GC_INIT(ctx);
-    char *dompath, *nicpath, *libxl_path;
-    char *val;
-    int rc;
-
-    dompath = libxl__xs_get_dompath(gc, domid);
-    nicinfo->devid = nic->devid;
-
-    nicpath = GCSPRINTF("%s/device/vif/%d", dompath, nicinfo->devid);
-    libxl_path = GCSPRINTF("%s/device/vif/%d",
-                           libxl__xs_libxl_path(gc, domid), nicinfo->devid);
-    nicinfo->backend = xs_read(ctx->xsh, XBT_NULL,
-                                GCSPRINTF("%s/backend", libxl_path), NULL);
-    if (!nicinfo->backend) {
-        GC_FREE;
-        return ERROR_FAIL;
-    }
-    rc = libxl__backendpath_parse_domid(gc, nicinfo->backend,
-                                        &nicinfo->backend_id);
-    if (rc) goto out;
-
-    val = libxl__xs_read(gc, XBT_NULL, GCSPRINTF("%s/state", nicpath));
-    nicinfo->state = val ? strtoul(val, NULL, 10) : -1;
-    val = libxl__xs_read(gc, XBT_NULL, GCSPRINTF("%s/event-channel", nicpath));
-    nicinfo->evtch = val ? strtoul(val, NULL, 10) : -1;
-    val = libxl__xs_read(gc, XBT_NULL, GCSPRINTF("%s/tx-ring-ref", nicpath));
-    nicinfo->rref_tx = val ? strtoul(val, NULL, 10) : -1;
-    val = libxl__xs_read(gc, XBT_NULL, GCSPRINTF("%s/rx-ring-ref", nicpath));
-    nicinfo->rref_rx = val ? strtoul(val, NULL, 10) : -1;
-    nicinfo->frontend = libxl__strdup(NOGC, nicpath);
-    nicinfo->frontend_id = domid;
-
-    rc = 0;
- out:
-    GC_FREE;
-    return rc;
-}
-
-const char *libxl__device_nic_devname(libxl__gc *gc,
-                                      uint32_t domid,
-                                      uint32_t devid,
-                                      libxl_nic_type type)
-{
-    switch (type) {
-    case LIBXL_NIC_TYPE_VIF:
-        return GCSPRINTF(NETBACK_NIC_NAME, domid, devid);
-    case LIBXL_NIC_TYPE_VIF_IOEMU:
-        return GCSPRINTF(NETBACK_NIC_NAME TAP_DEVICE_SUFFIX, domid, devid);
-    default:
-        abort();
-    }
-}
-
-/******************************************************************************/
 int libxl__device_console_add(libxl__gc *gc, uint32_t domid,
                               libxl__device_console *console,
                               libxl__domain_build_state *state,
@@ -3660,14 +3224,21 @@ static int libxl__device_channel_from_xenstore(libxl__gc *gc,
 
     libxl_device_channel_init(channel);
 
-    /* READ_BACKEND is from libxl__device_nic_from_xenstore above */
-    channel->name = READ_LIBXLDEV(NOGC, "name");
-    tmp = READ_LIBXLDEV(gc, "connection");
+    rc = libxl__xs_read_checked(NOGC, XBT_NULL,
+                                GCSPRINTF("%s/name", libxl_path),
+                                (const char **)(&channel->name));
+    if (rc) goto out;
+    rc = libxl__xs_read_checked(NOGC, XBT_NULL,
+                                GCSPRINTF("%s/connection", libxl_path), &tmp);
+    if (rc) goto out;
     if (!strcmp(tmp, "pty")) {
         channel->connection = LIBXL_CHANNEL_CONNECTION_PTY;
     } else if (!strcmp(tmp, "socket")) {
         channel->connection = LIBXL_CHANNEL_CONNECTION_SOCKET;
-        channel->u.socket.path = READ_LIBXLDEV(NOGC, "path");
+        rc = libxl__xs_read_checked(NOGC, XBT_NULL,
+                                    GCSPRINTF("%s/path", libxl_path),
+                                    (const char **)(&channel->u.socket.path));
+        if (rc) goto out;
     } else {
         rc = ERROR_INVAL;
         goto out;
@@ -4027,10 +3598,6 @@ out:
  * libxl__add_disks
  * libxl_device_disk_remove
  * libxl_device_disk_destroy
- * libxl_device_nic_add
- * libxl__add_nics
- * libxl_device_nic_remove
- * libxl_device_nic_destroy
  * libxl_device_vkb_remove
  * libxl_device_vkb_destroy
  * libxl_device_vfb_remove
@@ -4046,11 +3613,6 @@ LIBXL_DEFINE_DEVICE_ADD(disk)
 LIBXL_DEFINE_DEVICES_ADD(disk)
 LIBXL_DEFINE_DEVICE_REMOVE(disk)
 
-/* nic */
-LIBXL_DEFINE_DEVICE_ADD(nic)
-LIBXL_DEFINE_DEVICES_ADD(nic)
-LIBXL_DEFINE_DEVICE_REMOVE(nic)
-
 /* vkb */
 LIBXL_DEFINE_DEVICE_REMOVE(vkb)
 
@@ -7199,25 +6761,11 @@ static int libxl_device_disk_dm_needed(void *e, unsigned domid)
            elem->backend_domid == domid;
 }
 
-static int libxl_device_nic_compare(libxl_device_nic *d1,
-                                    libxl_device_nic *d2)
-{
-    return COMPARE_DEVID(d1, d2);
-}
-
-static void libxl_device_nic_update_config(libxl__gc *gc, void *d, void *s)
-{
-    libxl__update_config_nic(gc, d, s);
-}
-
 DEFINE_DEVICE_TYPE_STRUCT(disk,
     .merge       = libxl_device_disk_merge,
     .dm_needed   = libxl_device_disk_dm_needed,
     .skip_attach = 1
 );
-DEFINE_DEVICE_TYPE_STRUCT(nic,
-    .update_config = libxl_device_nic_update_config
-);
 
 /*
  * Local variables:
diff --git a/tools/libxl/libxl_create.c b/tools/libxl/libxl_create.c
index 23b82e9..d7db9e9 100644
--- a/tools/libxl/libxl_create.c
+++ b/tools/libxl/libxl_create.c
@@ -772,7 +772,6 @@ static void initiate_domain_create(libxl__egc *egc,
     libxl_ctx *ctx = libxl__gc_owner(gc);
     uint32_t domid;
     int i, ret;
-    size_t last_devid = -1;
     bool pod_enabled = false;
 
     /* convenience aliases */
@@ -932,25 +931,9 @@ static void initiate_domain_create(libxl__egc *egc,
      * Make two runs over configured NICs in order to avoid duplicate IDs
      * in case the caller partially assigned IDs.
      */
-    for (i = 0; i < d_config->num_nics; i++) {
-        /* We have to init the nic here, because we still haven't
-         * called libxl_device_nic_add when domcreate_launch_dm gets called,
-         * but qemu needs the nic information to be complete.
-         */
-        ret = libxl__device_nic_setdefault(gc, &d_config->nics[i], domid,
-                                           false);
-        if (ret) {
-            LOG(ERROR, "Unable to set nic defaults for nic %d", i);
-            goto error_out;
-        }
-
-        if (d_config->nics[i].devid > last_devid)
-            last_devid = d_config->nics[i].devid;
-    }
-    for (i = 0; i < d_config->num_nics; i++) {
-        if (d_config->nics[i].devid < 0)
-            d_config->nics[i].devid = ++last_devid;
-    }
+    ret = libxl__device_nic_set_devids(gc, d_config, domid);
+    if (ret)
+        goto error_out;
 
     if (restore_fd >= 0 || dcs->domid_soft_reset != INVALID_DOMID) {
         LOG(DEBUG, "restoring, not running bootloader");
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index cdc23e2..0e78c12 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -1288,7 +1288,6 @@ bool libxl__xswait_inuse(const libxl__xswait_state *ss);
 
 int libxl__xswait_start(libxl__gc*, libxl__xswait_state*);
 
-
 /*
  * libxl__ev_devstate - waits a given time for a device to
  * reach a given state.  Follows the libxl_ev_* conventions.
@@ -3679,6 +3678,10 @@ struct libxl__domain_create_state {
     libxl__multidev multidev;
 };
 
+_hidden int libxl__device_nic_set_devids(libxl__gc *gc,
+                                         libxl_domain_config *d_config,
+                                         uint32_t domid);
+
 /*----- Domain suspend (save) functions -----*/
 
 /* calls dss->callback when done */
diff --git a/tools/libxl/libxl_nic.c b/tools/libxl/libxl_nic.c
new file mode 100644
index 0000000..ff1834f
--- /dev/null
+++ b/tools/libxl/libxl_nic.c
@@ -0,0 +1,554 @@
+/*
+ * Copyright (C) 2016      SUSE Linux GmbH
+ * Author Juergen Gross <jgross@suse.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; version 2.1 only. with the special
+ * exception on linking described in file LICENSE.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ */
+
+#include "libxl_osdeps.h"
+
+#include "libxl_internal.h"
+
+int libxl_mac_to_device_nic(libxl_ctx *ctx, uint32_t domid,
+                            const char *mac, libxl_device_nic *nic)
+{
+    libxl_device_nic *nics;
+    int nb, rc, i;
+    libxl_mac mac_n;
+
+    rc = libxl__parse_mac(mac, mac_n);
+    if (rc)
+        return rc;
+
+    nics = libxl_device_nic_list(ctx, domid, &nb);
+    if (!nics)
+        return ERROR_FAIL;
+
+    memset(nic, 0, sizeof (libxl_device_nic));
+
+    rc = ERROR_INVAL;
+    for (i = 0; i < nb; ++i) {
+        if (!libxl__compare_macs(&mac_n, &nics[i].mac)) {
+            *nic = nics[i];
+            rc = 0;
+            i++; /* Do not dispose this NIC on exit path */
+            break;
+        }
+        libxl_device_nic_dispose(&nics[i]);
+    }
+
+    for (; i<nb; i++)
+        libxl_device_nic_dispose(&nics[i]);
+
+    free(nics);
+    return rc;
+}
+
+int libxl__device_nic_setdefault(libxl__gc *gc, libxl_device_nic *nic,
+                                 uint32_t domid, bool hotplug)
+{
+    int rc;
+
+    if (!nic->mtu)
+        nic->mtu = 1492;
+    if (!nic->model) {
+        nic->model = strdup("rtl8139");
+        if (!nic->model) return ERROR_NOMEM;
+    }
+    if (libxl__mac_is_default(&nic->mac)) {
+        const uint8_t *r;
+        libxl_uuid uuid;
+
+        libxl_uuid_generate(&uuid);
+        r = libxl_uuid_bytearray(&uuid);
+
+        nic->mac[0] = 0x00;
+        nic->mac[1] = 0x16;
+        nic->mac[2] = 0x3e;
+        nic->mac[3] = r[0] & 0x7f;
+        nic->mac[4] = r[1];
+        nic->mac[5] = r[2];
+    }
+    if (!nic->bridge) {
+        nic->bridge = strdup("xenbr0");
+        if (!nic->bridge) return ERROR_NOMEM;
+    }
+    if ( !nic->script && asprintf(&nic->script, "%s/vif-bridge",
+                                  libxl__xen_script_dir_path()) < 0 )
+        return ERROR_FAIL;
+
+    rc = libxl__resolve_domid(gc, nic->backend_domname, &nic->backend_domid);
+    if (rc < 0) return rc;
+
+    switch (libxl__domain_type(gc, domid)) {
+    case LIBXL_DOMAIN_TYPE_HVM:
+        if (!nic->nictype) {
+            if (hotplug ||
+                (libxl__device_model_version_running(gc, domid) ==
+                 LIBXL_DEVICE_MODEL_VERSION_NONE))
+                nic->nictype = LIBXL_NIC_TYPE_VIF;
+            else
+                nic->nictype = LIBXL_NIC_TYPE_VIF_IOEMU;
+        }
+        break;
+    case LIBXL_DOMAIN_TYPE_PV:
+        if (nic->nictype == LIBXL_NIC_TYPE_VIF_IOEMU) {
+            LOG(ERROR, "trying to create PV guest with an emulated interface");
+            return ERROR_INVAL;
+        }
+        nic->nictype = LIBXL_NIC_TYPE_VIF;
+        break;
+    case LIBXL_DOMAIN_TYPE_INVALID:
+        return ERROR_FAIL;
+    default:
+        abort();
+    }
+
+    return rc;
+}
+
+static int libxl__device_from_nic(libxl__gc *gc, uint32_t domid,
+                                  libxl_device_nic *nic,
+                                  libxl__device *device)
+{
+    device->backend_devid    = nic->devid;
+    device->backend_domid    = nic->backend_domid;
+    device->backend_kind     = LIBXL__DEVICE_KIND_VIF;
+    device->devid            = nic->devid;
+    device->domid            = domid;
+    device->kind             = LIBXL__DEVICE_KIND_VIF;
+
+    return 0;
+}
+
+static void libxl__update_config_nic(libxl__gc *gc, libxl_device_nic *dst,
+                                     const libxl_device_nic *src)
+{
+    dst->devid = src->devid;
+    dst->nictype = src->nictype;
+    libxl_mac_copy(CTX, &dst->mac, &src->mac);
+}
+
+static void libxl__device_nic_add(libxl__egc *egc, uint32_t domid,
+                                  libxl_device_nic *nic,
+                                  libxl__ao_device *aodev)
+{
+    STATE_AO_GC(aodev->ao);
+    flexarray_t *front;
+    flexarray_t *back;
+    libxl__device *device;
+    int rc;
+    xs_transaction_t t = XBT_NULL;
+    libxl_domain_config d_config;
+    libxl_device_nic nic_saved;
+    libxl__domain_userdata_lock *lock = NULL;
+
+    libxl_domain_config_init(&d_config);
+    libxl_device_nic_init(&nic_saved);
+    libxl_device_nic_copy(CTX, &nic_saved, nic);
+
+    rc = libxl__device_nic_setdefault(gc, nic, domid, aodev->update_json);
+    if (rc) goto out;
+
+    front = flexarray_make(gc, 16, 1);
+    back = flexarray_make(gc, 18, 1);
+
+    if (nic->devid == -1) {
+        if ((nic->devid = libxl__device_nextid(gc, domid, "vif")) < 0) {
+            rc = ERROR_FAIL;
+            goto out;
+        }
+    }
+
+    libxl__update_config_nic(gc, &nic_saved, nic);
+
+    GCNEW(device);
+    rc = libxl__device_from_nic(gc, domid, nic, device);
+    if ( rc != 0 ) goto out;
+
+    flexarray_append(back, "frontend-id");
+    flexarray_append(back, GCSPRINTF("%d", domid));
+    flexarray_append(back, "online");
+    flexarray_append(back, "1");
+    flexarray_append(back, "state");
+    flexarray_append(back, GCSPRINTF("%d", XenbusStateInitialising));
+    if (nic->script)
+        flexarray_append_pair(back, "script",
+                              libxl__abs_path(gc, nic->script,
+                                              libxl__xen_script_dir_path()));
+
+    if (nic->ifname) {
+        flexarray_append(back, "vifname");
+        flexarray_append(back, nic->ifname);
+    }
+
+    if (nic->coloft_forwarddev) {
+        flexarray_append(back, "forwarddev");
+        flexarray_append(back, nic->coloft_forwarddev);
+    }
+
+    flexarray_append(back, "mac");
+    flexarray_append(back,GCSPRINTF(LIBXL_MAC_FMT, LIBXL_MAC_BYTES(nic->mac)));
+    if (nic->ip) {
+        flexarray_append(back, "ip");
+        flexarray_append(back, libxl__strdup(gc, nic->ip));
+    }
+    if (nic->gatewaydev) {
+        flexarray_append(back, "gatewaydev");
+        flexarray_append(back, libxl__strdup(gc, nic->gatewaydev));
+    }
+
+    if (nic->rate_interval_usecs > 0) {
+        flexarray_append(back, "rate");
+        flexarray_append(back, GCSPRINTF("%"PRIu64",%"PRIu32"",
+                            nic->rate_bytes_per_interval,
+                            nic->rate_interval_usecs));
+    }
+
+    flexarray_append(back, "bridge");
+    flexarray_append(back, libxl__strdup(gc, nic->bridge));
+    flexarray_append(back, "handle");
+    flexarray_append(back, GCSPRINTF("%d", nic->devid));
+    flexarray_append(back, "type");
+    flexarray_append(back, libxl__strdup(gc,
+                                     libxl_nic_type_to_string(nic->nictype)));
+
+    flexarray_append(front, "backend-id");
+    flexarray_append(front, GCSPRINTF("%d", nic->backend_domid));
+    flexarray_append(front, "state");
+    flexarray_append(front, GCSPRINTF("%d", XenbusStateInitialising));
+    flexarray_append(front, "handle");
+    flexarray_append(front, GCSPRINTF("%d", nic->devid));
+    flexarray_append(front, "mac");
+    flexarray_append(front, GCSPRINTF(
+                                    LIBXL_MAC_FMT, LIBXL_MAC_BYTES(nic->mac)));
+
+    if (aodev->update_json) {
+        lock = libxl__lock_domain_userdata(gc, domid);
+        if (!lock) {
+            rc = ERROR_LOCK_FAIL;
+            goto out;
+        }
+
+        rc = libxl__get_domain_configuration(gc, domid, &d_config);
+        if (rc) goto out;
+
+        DEVICE_ADD(nic, nics, domid, &nic_saved, COMPARE_DEVID, &d_config);
+
+        rc = libxl__dm_check_start(gc, &d_config, domid);
+        if (rc) goto out;
+    }
+
+    for (;;) {
+        rc = libxl__xs_transaction_start(gc, &t);
+        if (rc) goto out;
+
+        rc = libxl__device_exists(gc, t, device);
+        if (rc < 0) goto out;
+        if (rc == 1) {              /* already exists in xenstore */
+            LOG(ERROR, "device already exists in xenstore");
+            aodev->action = LIBXL__DEVICE_ACTION_ADD; /* for error message */
+            rc = ERROR_DEVICE_EXISTS;
+            goto out;
+        }
+
+        if (aodev->update_json) {
+            rc = libxl__set_domain_configuration(gc, domid, &d_config);
+            if (rc) goto out;
+        }
+
+        libxl__device_generic_add(gc, t, device,
+                                  libxl__xs_kvs_of_flexarray(gc, back,
+                                                             back->count),
+                                  libxl__xs_kvs_of_flexarray(gc, front,
+                                                             front->count),
+                                  NULL);
+
+        rc = libxl__xs_transaction_commit(gc, &t);
+        if (!rc) break;
+        if (rc < 0) goto out;
+    }
+
+    aodev->dev = device;
+    aodev->action = LIBXL__DEVICE_ACTION_ADD;
+    libxl__wait_device_connection(egc, aodev);
+
+    rc = 0;
+out:
+    libxl__xs_transaction_abort(gc, &t);
+    if (lock) libxl__unlock_domain_userdata(lock);
+    libxl_device_nic_dispose(&nic_saved);
+    libxl_domain_config_dispose(&d_config);
+    aodev->rc = rc;
+    if (rc) aodev->callback(egc, aodev);
+    return;
+}
+
+static int libxl__device_nic_from_xenstore(libxl__gc *gc,
+                                           const char *libxl_path,
+                                           libxl_device_nic *nic)
+{
+    const char *tmp;
+    int rc;
+
+    libxl_device_nic_init(nic);
+
+    rc = libxl__xs_read_checked(gc, XBT_NULL,
+                                GCSPRINTF("%s/handle", libxl_path), &tmp);
+    if (rc) goto out;
+    if (tmp)
+        nic->devid = atoi(tmp);
+    else
+        nic->devid = 0;
+
+    /* nic->mtu = */
+
+    rc = libxl__xs_read_checked(gc, XBT_NULL,
+                                GCSPRINTF("%s/mac", libxl_path), &tmp);
+    if (rc) goto out;
+    if (tmp) {
+        rc = libxl__parse_mac(tmp, nic->mac);
+        if (rc) goto out;
+    } else {
+        memset(nic->mac, 0, sizeof(nic->mac));
+    }
+
+    rc = libxl__xs_read_checked(gc, XBT_NULL,
+                                GCSPRINTF("%s/ip", libxl_path),
+                                (const char **)(&nic->ip));
+    if (rc) goto out;
+    rc = libxl__xs_read_checked(gc, XBT_NULL,
+                                GCSPRINTF("%s/bridge", libxl_path),
+                                (const char **)(&nic->bridge));
+    if (rc) goto out;
+    rc = libxl__xs_read_checked(gc, XBT_NULL,
+                                GCSPRINTF("%s/script", libxl_path),
+                                (const char **)(&nic->script));
+    if (rc) goto out;
+    rc = libxl__xs_read_checked(gc, XBT_NULL,
+                                GCSPRINTF("%s/forwarddev", libxl_path),
+                                (const char **)(&nic->coloft_forwarddev));
+    if (rc) goto out;
+
+    /* vif_ioemu nics use the same xenstore entries as vif interfaces */
+    rc = libxl__xs_read_checked(gc, XBT_NULL,
+                                GCSPRINTF("%s/type", libxl_path), &tmp);
+    if (rc) goto out;
+    if (tmp) {
+        rc = libxl_nic_type_from_string(tmp, &nic->nictype);
+        if (rc) goto out;
+    } else {
+        nic->nictype = LIBXL_NIC_TYPE_VIF;
+    }
+    nic->model = NULL; /* XXX Only for TYPE_IOEMU */
+    nic->ifname = NULL; /* XXX Only for TYPE_IOEMU */
+
+    rc = 0;
+ out:
+    return rc;
+}
+
+int libxl_devid_to_device_nic(libxl_ctx *ctx, uint32_t domid,
+                              int devid, libxl_device_nic *nic)
+{
+    GC_INIT(ctx);
+    char *libxl_dom_path, *libxl_path;
+    int rc = ERROR_FAIL;
+
+    libxl_device_nic_init(nic);
+    libxl_dom_path = libxl__xs_libxl_path(gc, domid);
+    if (!libxl_dom_path)
+        goto out;
+
+    libxl_path = GCSPRINTF("%s/device/vif/%d", libxl_dom_path, devid);
+
+    rc = libxl__device_nic_from_xenstore(gc, libxl_path, nic);
+    if (rc) goto out;
+
+    rc = 0;
+out:
+    GC_FREE;
+    return rc;
+}
+
+static int libxl__append_nic_list(libxl__gc *gc,
+                                           uint32_t domid,
+                                           libxl_device_nic **nics,
+                                           int *nnics)
+{
+    char *libxl_dir_path = NULL;
+    char **dir = NULL;
+    unsigned int n = 0;
+    libxl_device_nic *pnic = NULL, *pnic_end = NULL;
+    int rc;
+
+    libxl_dir_path = GCSPRINTF("%s/device/vif",
+                               libxl__xs_libxl_path(gc, domid));
+    dir = libxl__xs_directory(gc, XBT_NULL, libxl_dir_path, &n);
+    if (dir && n) {
+        libxl_device_nic *tmp;
+        tmp = realloc(*nics, sizeof (libxl_device_nic) * (*nnics + n));
+        if (tmp == NULL)
+            return ERROR_NOMEM;
+        *nics = tmp;
+        pnic = *nics + *nnics;
+        pnic_end = *nics + *nnics + n;
+        for (; pnic < pnic_end; pnic++, dir++) {
+            const char *p;
+            p = GCSPRINTF("%s/%s", libxl_dir_path, *dir);
+            rc = libxl__device_nic_from_xenstore(gc, p, pnic);
+            if (rc) goto out;
+        }
+        *nnics += n;
+    }
+    return 0;
+
+ out:
+    return rc;
+}
+
+libxl_device_nic *libxl_device_nic_list(libxl_ctx *ctx, uint32_t domid, int *num)
+{
+    GC_INIT(ctx);
+    libxl_device_nic *nics = NULL;
+    int rc;
+
+    *num = 0;
+
+    rc = libxl__append_nic_list(gc, domid, &nics, num);
+    if (rc) goto out_err;
+
+    GC_FREE;
+    return nics;
+
+out_err:
+    LOG(ERROR, "Unable to list nics");
+    while (*num) {
+        (*num)--;
+        libxl_device_nic_dispose(&nics[*num]);
+    }
+    free(nics);
+    return NULL;
+}
+
+int libxl_device_nic_getinfo(libxl_ctx *ctx, uint32_t domid,
+                              libxl_device_nic *nic, libxl_nicinfo *nicinfo)
+{
+    GC_INIT(ctx);
+    char *dompath, *nicpath, *libxl_path;
+    char *val;
+    int rc;
+
+    dompath = libxl__xs_get_dompath(gc, domid);
+    nicinfo->devid = nic->devid;
+
+    nicpath = GCSPRINTF("%s/device/vif/%d", dompath, nicinfo->devid);
+    libxl_path = GCSPRINTF("%s/device/vif/%d",
+                           libxl__xs_libxl_path(gc, domid), nicinfo->devid);
+    nicinfo->backend = xs_read(ctx->xsh, XBT_NULL,
+                                GCSPRINTF("%s/backend", libxl_path), NULL);
+    if (!nicinfo->backend) {
+        GC_FREE;
+        return ERROR_FAIL;
+    }
+    rc = libxl__backendpath_parse_domid(gc, nicinfo->backend,
+                                        &nicinfo->backend_id);
+    if (rc) goto out;
+
+    val = libxl__xs_read(gc, XBT_NULL, GCSPRINTF("%s/state", nicpath));
+    nicinfo->state = val ? strtoul(val, NULL, 10) : -1;
+    val = libxl__xs_read(gc, XBT_NULL, GCSPRINTF("%s/event-channel", nicpath));
+    nicinfo->evtch = val ? strtoul(val, NULL, 10) : -1;
+    val = libxl__xs_read(gc, XBT_NULL, GCSPRINTF("%s/tx-ring-ref", nicpath));
+    nicinfo->rref_tx = val ? strtoul(val, NULL, 10) : -1;
+    val = libxl__xs_read(gc, XBT_NULL, GCSPRINTF("%s/rx-ring-ref", nicpath));
+    nicinfo->rref_rx = val ? strtoul(val, NULL, 10) : -1;
+    nicinfo->frontend = libxl__strdup(NOGC, nicpath);
+    nicinfo->frontend_id = domid;
+
+    rc = 0;
+ out:
+    GC_FREE;
+    return rc;
+}
+
+const char *libxl__device_nic_devname(libxl__gc *gc,
+                                      uint32_t domid,
+                                      uint32_t devid,
+                                      libxl_nic_type type)
+{
+    switch (type) {
+    case LIBXL_NIC_TYPE_VIF:
+        return GCSPRINTF(NETBACK_NIC_NAME, domid, devid);
+    case LIBXL_NIC_TYPE_VIF_IOEMU:
+        return GCSPRINTF(NETBACK_NIC_NAME TAP_DEVICE_SUFFIX, domid, devid);
+    default:
+        abort();
+    }
+}
+
+static int libxl_device_nic_compare(libxl_device_nic *d1,
+                                    libxl_device_nic *d2)
+{
+    return COMPARE_DEVID(d1, d2);
+}
+
+static void libxl_device_nic_update_config(libxl__gc *gc, void *d, void *s)
+{
+    libxl__update_config_nic(gc, d, s);
+}
+
+int libxl__device_nic_set_devids(libxl__gc *gc, libxl_domain_config *d_config,
+                                 uint32_t domid)
+{
+    int ret = 0;
+    int i;
+    size_t last_devid = -1;
+
+    for (i = 0; i < d_config->num_nics; i++) {
+        /* We have to init the nic here, because we still haven't
+         * called libxl_device_nic_add when domcreate_launch_dm gets called,
+         * but qemu needs the nic information to be complete.
+         */
+        ret = libxl__device_nic_setdefault(gc, &d_config->nics[i], domid,
+                                           false);
+        if (ret) {
+            LOG(ERROR, "Unable to set nic defaults for nic %d", i);
+            goto out;
+        }
+
+        if (d_config->nics[i].devid > last_devid)
+            last_devid = d_config->nics[i].devid;
+    }
+    for (i = 0; i < d_config->num_nics; i++) {
+        if (d_config->nics[i].devid < 0)
+            d_config->nics[i].devid = ++last_devid;
+    }
+
+out:
+    return ret;
+}
+
+LIBXL_DEFINE_DEVICE_ADD(nic)
+LIBXL_DEFINE_DEVICES_ADD(nic)
+LIBXL_DEFINE_DEVICE_REMOVE(nic)
+
+DEFINE_DEVICE_TYPE_STRUCT(nic,
+    .update_config = libxl_device_nic_update_config
+);
+
+/*
+ * Local variables:
+ * mode: C
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/tools/libxl/libxl_utils.c b/tools/libxl/libxl_utils.c
index b748555..49cbaa5 100644
--- a/tools/libxl/libxl_utils.c
+++ b/tools/libxl/libxl_utils.c
@@ -598,41 +598,6 @@ int libxl_pipe(libxl_ctx *ctx, int pipes[2])
     return ret;
 }
 
-int libxl_mac_to_device_nic(libxl_ctx *ctx, uint32_t domid,
-                            const char *mac, libxl_device_nic *nic)
-{
-    libxl_device_nic *nics;
-    int nb, rc, i;
-    libxl_mac mac_n;
-
-    rc = libxl__parse_mac(mac, mac_n);
-    if (rc)
-        return rc;
-
-    nics = libxl_device_nic_list(ctx, domid, &nb);
-    if (!nics)
-        return ERROR_FAIL;
-
-    memset(nic, 0, sizeof (libxl_device_nic));
-
-    rc = ERROR_INVAL;
-    for (i = 0; i < nb; ++i) {
-        if (!libxl__compare_macs(&mac_n, &nics[i].mac)) {
-            *nic = nics[i];
-            rc = 0;
-            i++; /* Do not dispose this NIC on exit path */
-            break;
-        }
-        libxl_device_nic_dispose(&nics[i]);
-    }
-
-    for (; i<nb; i++)
-        libxl_device_nic_dispose(&nics[i]);
-
-    free(nics);
-    return rc;
-}
-
 int libxl_bitmap_alloc(libxl_ctx *ctx, libxl_bitmap *bitmap, int n_bits)
 {
     GC_INIT(ctx);
-- 
2.6.6


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH 0/6] libxl: extend device type framework
  2016-07-12 15:30 [PATCH 0/6] libxl: extend device type framework Juergen Gross
                   ` (5 preceding siblings ...)
  2016-07-12 15:30 ` [PATCH 6/6] libxl: move common nic stuff into one source Juergen Gross
@ 2016-07-21 13:12 ` Juergen Gross
  2016-07-27 11:45 ` Wei Liu
  7 siblings, 0 replies; 18+ messages in thread
From: Juergen Gross @ 2016-07-21 13:12 UTC (permalink / raw)
  To: xen-devel; +Cc: ian.jackson, wei.liu2

On 12/07/16 17:30, Juergen Gross wrote:
> Extend the device type framework in libxl to cover more functions in a
> generic way. This allows to have all functionality of a specific device
> type in just one source file instead of spreading it via multiple files
> and have functions to deal with multiple device types in a similar way.
> 
> Some patches of this series can be regarded as RFC, as having e.g. all
> nic related functions in one file is a matter of taste. I haven't
> marked the patches as RFC as they are completely tested and I believe
> the result is an improvement over current status.
> 
> Juergen Gross (6):
>   libxl: add "merge" function to generic device type support
>   libxl: add "pv device mode needed" support to device type framework
>   libxl: move library pvusb specific code into libxl_pvusb.c
>   libxl: split libxl vtpm code into one source
>   libxl: add config update callback to device type framework
>   libxl: move common nic stuff into one source
> 
>  tools/libxl/Makefile         |   1 +
>  tools/libxl/libxl.c          | 930 ++++++-------------------------------------
>  tools/libxl/libxl_create.c   |  35 +-
>  tools/libxl/libxl_dm.c       |  31 +-
>  tools/libxl/libxl_internal.c |  21 +-
>  tools/libxl/libxl_internal.h |  77 ++--
>  tools/libxl/libxl_nic.c      | 554 ++++++++++++++++++++++++++
>  tools/libxl/libxl_pci.c      |   8 +-
>  tools/libxl/libxl_pvusb.c    |  42 +-
>  tools/libxl/libxl_utils.c    |  96 -----
>  tools/libxl/libxl_vtpm.c     | 380 ++++++++++++++++++
>  11 files changed, 1192 insertions(+), 983 deletions(-)
>  create mode 100644 tools/libxl/libxl_nic.c
>  create mode 100644 tools/libxl/libxl_vtpm.c
> 

Any comments on this series?


Juergen

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 1/6] libxl: add "merge" function to generic device type support
  2016-07-12 15:30 ` [PATCH 1/6] libxl: add "merge" function to generic device type support Juergen Gross
@ 2016-07-25 10:45   ` Wei Liu
  2017-01-19 16:14   ` Olaf Hering
  1 sibling, 0 replies; 18+ messages in thread
From: Wei Liu @ 2016-07-25 10:45 UTC (permalink / raw)
  To: Juergen Gross; +Cc: wei.liu2, ian.jackson, xen-devel

On Tue, Jul 12, 2016 at 05:30:39PM +0200, Juergen Gross wrote:
> Instead of using a macro generating the code to merge xenstore and
> json configuration data, use the generic device type support for
> this purpose.
> 
> This requires to add some accessor functions to the framework and
> a structure for disks (as disks are added separately they didn't need
> such a structure up to now).
> 
> Signed-off-by: Juergen Gross <jgross@suse.com>

I definitely think this is a good idea.

The code looks good to me:

Acked-by: Wei Liu <wei.liu2@citrix.com>

I didn't do a line-by-line review though because most code looks very
mechanical. I'm confident that we can sort out issues should they arise.

Wei.

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 2/6] libxl: add "pv device mode needed" support to device type framework
  2016-07-12 15:30 ` [PATCH 2/6] libxl: add "pv device mode needed" support to device type framework Juergen Gross
@ 2016-07-25 10:46   ` Wei Liu
  0 siblings, 0 replies; 18+ messages in thread
From: Wei Liu @ 2016-07-25 10:46 UTC (permalink / raw)
  To: Juergen Gross; +Cc: wei.liu2, ian.jackson, xen-devel

On Tue, Jul 12, 2016 at 05:30:40PM +0200, Juergen Gross wrote:
> Add another callback to the device type framework in order to aid
> decision whether a pv domain needs a device model.
> 
> Signed-off-by: Juergen Gross <jgross@suse.com>

Acked-by: Wei Liu <wei.liu2@citrix.com>

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 3/6] libxl: move library pvusb specific code into libxl_pvusb.c
  2016-07-12 15:30 ` [PATCH 3/6] libxl: move library pvusb specific code into libxl_pvusb.c Juergen Gross
@ 2016-07-25 10:46   ` Wei Liu
  0 siblings, 0 replies; 18+ messages in thread
From: Wei Liu @ 2016-07-25 10:46 UTC (permalink / raw)
  To: Juergen Gross; +Cc: wei.liu2, ian.jackson, xen-devel

On Tue, Jul 12, 2016 at 05:30:41PM +0200, Juergen Gross wrote:
> Outside libxl_pvusb.c only libxl_util.c still contains some pvusb code.
> 
> Move it to libxl_pvusb.c.
> 
> Signed-off-by: Juergen Gross <jgross@suse.com>

Acked-by: Wei Liu <wei.liu2@citrix.com>

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 4/6] libxl: split libxl vtpm code into one source
  2016-07-12 15:30 ` [PATCH 4/6] libxl: split libxl vtpm code into one source Juergen Gross
@ 2016-07-25 10:46   ` Wei Liu
  0 siblings, 0 replies; 18+ messages in thread
From: Wei Liu @ 2016-07-25 10:46 UTC (permalink / raw)
  To: Juergen Gross; +Cc: ian.jackson, wei.liu2, xen-devel

On Tue, Jul 12, 2016 at 05:30:42PM +0200, Juergen Gross wrote:
> Put all vtpm related stuff of libxl into a dedicated source file.
> 
> Signed-off-by: Juergen Gross <jgross@suse.com>

Acked-by: Wei Liu <wei.liu2@citrix.com>

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 6/6] libxl: move common nic stuff into one source
  2016-07-12 15:30 ` [PATCH 6/6] libxl: move common nic stuff into one source Juergen Gross
@ 2016-07-25 10:46   ` Wei Liu
  0 siblings, 0 replies; 18+ messages in thread
From: Wei Liu @ 2016-07-25 10:46 UTC (permalink / raw)
  To: Juergen Gross; +Cc: wei.liu2, ian.jackson, xen-devel

On Tue, Jul 12, 2016 at 05:30:44PM +0200, Juergen Gross wrote:
> Put all nic related stuff of libxl form common files into a dedicated
> source file.
> 
> Signed-off-by: Juergen Gross <jgross@suse.com>

Assuming this is pure code motion:

Acked-by: Wei Liu <wei.liu2@citrix.com>

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 5/6] libxl: add config update callback to device type framework
  2016-07-12 15:30 ` [PATCH 5/6] libxl: add config update callback to device type framework Juergen Gross
@ 2016-07-25 10:46   ` Wei Liu
  0 siblings, 0 replies; 18+ messages in thread
From: Wei Liu @ 2016-07-25 10:46 UTC (permalink / raw)
  To: Juergen Gross; +Cc: wei.liu2, ian.jackson, xen-devel

On Tue, Jul 12, 2016 at 05:30:43PM +0200, Juergen Gross wrote:
> Some device types require a configuration update after resume of
> domain. Add a callback for this purpose.
> 
> Signed-off-by: Juergen Gross <jgross@suse.com>

Acked-by: Wei Liu <wei.liu2@citrix.com>

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 0/6] libxl: extend device type framework
  2016-07-12 15:30 [PATCH 0/6] libxl: extend device type framework Juergen Gross
                   ` (6 preceding siblings ...)
  2016-07-21 13:12 ` [PATCH 0/6] libxl: extend device type framework Juergen Gross
@ 2016-07-27 11:45 ` Wei Liu
  7 siblings, 0 replies; 18+ messages in thread
From: Wei Liu @ 2016-07-27 11:45 UTC (permalink / raw)
  To: Juergen Gross; +Cc: ian.jackson, wei.liu2, xen-devel

Series pushed to staging.

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 1/6] libxl: add "merge" function to generic device type support
  2016-07-12 15:30 ` [PATCH 1/6] libxl: add "merge" function to generic device type support Juergen Gross
  2016-07-25 10:45   ` Wei Liu
@ 2017-01-19 16:14   ` Olaf Hering
  2017-01-19 16:19     ` Wei Liu
  1 sibling, 1 reply; 18+ messages in thread
From: Olaf Hering @ 2017-01-19 16:14 UTC (permalink / raw)
  To: Juergen Gross; +Cc: ian.jackson, wei.liu2, xen-devel


[-- Attachment #1.1: Type: text/plain, Size: 1715 bytes --]

On Tue, Jul 12, Juergen Gross wrote:

> Instead of using a macro generating the code to merge xenstore and
> json configuration data, use the generic device type support for
> this purpose.
> This requires to add some accessor functions to the framework and
> a structure for disks (as disks are added separately they didn't need
> such a structure up to now).

> +++ b/tools/libxl/libxl.c
> @@ -7371,93 +7371,68 @@ int libxl_retrieve_domain_configuration(libxl_ctx *ctx, uint32_t domid,

> +            if (!dt->list || !dt->compare)
> +                continue;


This makes libxl_device_<type>_compare optional ...

> +#define DEFINE_DEVICE_TYPE_STRUCT_X(name, sname, ...)                          \
> +    const struct libxl_device_type libxl__ ## name ## _devtype = {             \
> +        .type          = #sname,                                               \
> +        .ptr_offset    = offsetof(libxl_domain_config, name ## s),             \
> +        .num_offset    = offsetof(libxl_domain_config, num_ ## name ## s),     \
> +        .dev_elem_size = sizeof(libxl_device_ ## sname),                       \
> +        .add           = libxl__add_ ## name ## s,                             \
> +        .list          = (void *(*)(libxl_ctx *, uint32_t, int *))             \
> +                         libxl_device_ ## sname ## _list,                      \
> +        .dispose       = (void (*)(void *))libxl_device_ ## sname ## _dispose, \
> +        .compare       = (int (*)(void *, void *))                             \
> +                         libxl_device_ ## sname ## _compare,                   \

... and this makes libxl_device_<type>_compare mandatory.

Which one is correct?

Olaf

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

[-- Attachment #2: Type: text/plain, Size: 127 bytes --]

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 1/6] libxl: add "merge" function to generic device type support
  2017-01-19 16:14   ` Olaf Hering
@ 2017-01-19 16:19     ` Wei Liu
  2017-01-19 16:51       ` Juergen Gross
  0 siblings, 1 reply; 18+ messages in thread
From: Wei Liu @ 2017-01-19 16:19 UTC (permalink / raw)
  To: Olaf Hering; +Cc: Juergen Gross, ian.jackson, wei.liu2, xen-devel

On Thu, Jan 19, 2017 at 05:14:35PM +0100, Olaf Hering wrote:
> On Tue, Jul 12, Juergen Gross wrote:
> 
> > Instead of using a macro generating the code to merge xenstore and
> > json configuration data, use the generic device type support for
> > this purpose.
> > This requires to add some accessor functions to the framework and
> > a structure for disks (as disks are added separately they didn't need
> > such a structure up to now).
> 
> > +++ b/tools/libxl/libxl.c
> > @@ -7371,93 +7371,68 @@ int libxl_retrieve_domain_configuration(libxl_ctx *ctx, uint32_t domid,
> 
> > +            if (!dt->list || !dt->compare)
> > +                continue;
> 
> 
> This makes libxl_device_<type>_compare optional ...

Actually this makes both list and compare optional.

I would say both should be mandatory -- why would you have a device type
that can't be listed or compared?

Juergen?

Wei.

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 1/6] libxl: add "merge" function to generic device type support
  2017-01-19 16:19     ` Wei Liu
@ 2017-01-19 16:51       ` Juergen Gross
  0 siblings, 0 replies; 18+ messages in thread
From: Juergen Gross @ 2017-01-19 16:51 UTC (permalink / raw)
  To: Wei Liu, Olaf Hering; +Cc: ian.jackson, xen-devel

On 19/01/17 17:19, Wei Liu wrote:
> On Thu, Jan 19, 2017 at 05:14:35PM +0100, Olaf Hering wrote:
>> On Tue, Jul 12, Juergen Gross wrote:
>>
>>> Instead of using a macro generating the code to merge xenstore and
>>> json configuration data, use the generic device type support for
>>> this purpose.
>>> This requires to add some accessor functions to the framework and
>>> a structure for disks (as disks are added separately they didn't need
>>> such a structure up to now).
>>
>>> +++ b/tools/libxl/libxl.c
>>> @@ -7371,93 +7371,68 @@ int libxl_retrieve_domain_configuration(libxl_ctx *ctx, uint32_t domid,
>>
>>> +            if (!dt->list || !dt->compare)
>>> +                continue;
>>
>>
>> This makes libxl_device_<type>_compare optional ...
> 
> Actually this makes both list and compare optional.
> 
> I would say both should be mandatory -- why would you have a device type
> that can't be listed or compared?
> 
> Juergen?

I think above lines predate the introduction of
DEFINE_DEVICE_TYPE_STRUCT_X().

They can probably be removed.


Juergen


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2017-01-19 16:51 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-07-12 15:30 [PATCH 0/6] libxl: extend device type framework Juergen Gross
2016-07-12 15:30 ` [PATCH 1/6] libxl: add "merge" function to generic device type support Juergen Gross
2016-07-25 10:45   ` Wei Liu
2017-01-19 16:14   ` Olaf Hering
2017-01-19 16:19     ` Wei Liu
2017-01-19 16:51       ` Juergen Gross
2016-07-12 15:30 ` [PATCH 2/6] libxl: add "pv device mode needed" support to device type framework Juergen Gross
2016-07-25 10:46   ` Wei Liu
2016-07-12 15:30 ` [PATCH 3/6] libxl: move library pvusb specific code into libxl_pvusb.c Juergen Gross
2016-07-25 10:46   ` Wei Liu
2016-07-12 15:30 ` [PATCH 4/6] libxl: split libxl vtpm code into one source Juergen Gross
2016-07-25 10:46   ` Wei Liu
2016-07-12 15:30 ` [PATCH 5/6] libxl: add config update callback to device type framework Juergen Gross
2016-07-25 10:46   ` Wei Liu
2016-07-12 15:30 ` [PATCH 6/6] libxl: move common nic stuff into one source Juergen Gross
2016-07-25 10:46   ` Wei Liu
2016-07-21 13:12 ` [PATCH 0/6] libxl: extend device type framework Juergen Gross
2016-07-27 11:45 ` Wei Liu

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.