Skip to contentSkip to conversation

libxl: JSON infrastructure, fixes and prerequisite patches for new API

PATCH Applied Wei Liu 3 participants 26 messages Jun 17, 2014 – Jun 24, 2014

lore
Share & tools
lore

Wei Liu

author
#0 lore raw

v7 cover libxl: JSON infrastructure, fixes and prerequisite patches for new API

V7 of the series. A patch to push CPU pinning is taken out as Dario has generously offered to take up that patch.

The JSON infrastructure was tested with running testidl over and over again. A new testidl executable was generated every run.

Rebased on top of staging.

Legend:
A - acked
C - changed since last version
D - previous acked, but new change introduced so acked-by dropped No marker - new patch

Wei.

Wei Liu (9):
A xl / libxl: push parsing of SSID and CPU pool ID down to libxl A libxl: libxl_uuid_copy now takes a ctx argument
libxl_internal: functions to check default values for builtin types
C libxl/gentypes.py: don't generate default values
D libxl IDL: generate code to parse libxl__json_object to libxl_FOO
struct

 A  libxl/gentest.py: test JSON parser
 A  libxl: copy function for builtin types
 A  libxl IDL: generate deep copy functions
 A  libxl/gentest.py: test deep copy functions
 tools/libxl/Makefile                 |    4 +-
 tools/libxl/gentest.py               |   60 +++++++-
 tools/libxl/gentypes.py              |  258 +++++++++++++++++++++++++++++++++-
 tools/libxl/idl.py                   |   30 +++-
 tools/libxl/idl.txt                  |   28 +++-
 tools/libxl/libxl.c                  |   94 ++++++++++++-
 tools/libxl/libxl.h                  |   60 +++++++-
 tools/libxl/libxl_cpuid.c            |   33 +++++
 tools/libxl/libxl_create.c           |   59 +++++++-
 tools/libxl/libxl_dm.c               |    4 +
 tools/libxl/libxl_internal.c         |    6 +
 tools/libxl/libxl_internal.h         |   30 ++++
 tools/libxl/libxl_nocpuid.c          |    6 +
 tools/libxl/libxl_types.idl          |   51 ++++---
 tools/libxl/libxl_types_internal.idl |    4 +-
 tools/libxl/libxl_utils.c            |   27 +++-
 tools/libxl/libxl_utils.h            |    4 +
 tools/libxl/libxl_uuid.c             |    8 +-
 tools/libxl/libxl_uuid.h             |    7 +-
 tools/libxl/xl_cmdimpl.c             |  107 ++++----------
 tools/libxl/xl_sxp.c                 |    7 +-
 21 files changed, 760 insertions(+), 127 deletions(-)
Signature · 1 line
1.7.10.4
author
#1 lore raw

PATCH v7 1/9 xl / libxl: push parsing of SSID and CPU pool ID down to libxl

This patch pushes parsing of "init_seclabel", "seclabel", "device_model_stubdomain_seclabel" and "pool" down to libxl level.

Originally the parsing is done in xl level, which is not ideal because libxl won't have the truely relevant information. With this patch libxl holds important information by itself.

The libxl IDL is extended to hold the string of labels and pool name. And if there those strings are present they take precedence over the numeric representations.

As all relevant structures (libxl_dominfo etc) have a field called X_name / X_label now, a string is also copied there so that callers won't have to do ID to name / label translation.

In order to be compatible with users of older versions of libxl, this patch also defines LIBXL_HAVE_SSID_LABEL and LIBXL_HAVE_CPUPOOL_NAME. If they are defined, the respective strings are available. And if those strings are not NULL, libxl will do the parsing and ignore the numeric values.

Cc Dario Faggioli <[email protected]>
Cc Juergen Gross <[email protected]>
Cc Daniel De Graaf <[email protected]>
---
 tools/libxl/libxl.c         |   22 +++++++--
 tools/libxl/libxl.h         |   20 ++++++++
 tools/libxl/libxl_create.c  |   57 +++++++++++++++++++++++
 tools/libxl/libxl_dm.c      |    4 ++
 tools/libxl/libxl_types.idl |    6 +++
 tools/libxl/xl_cmdimpl.c    |  107 ++++++++++++-------------------------------
 tools/libxl/xl_sxp.c        |    7 +--
 7 files changed, 137 insertions(+), 86 deletions(-)
tools/libxl/libxl.c +18 -4
index 4b66afc..08f1d4d 100644
@@ -529,12 +529,18 @@ int libxl_domain_preserve(libxl_ctx *ctx, uint32_t domid,
     return 0;
 }
 
-static void xcinfo2xlinfo(const xc_domaininfo_t *xcinfo,
+static void xcinfo2xlinfo(libxl_ctx *ctx,
+                          const xc_domaininfo_t *xcinfo,
                           libxl_dominfo *xlinfo)
 {
+    size_t size;
+
     memcpy(&(xlinfo->uuid), xcinfo->handle, sizeof(xen_domain_handle_t));
     xlinfo->domid = xcinfo->domain;
     xlinfo->ssidref = xcinfo->ssidref;
+    if (libxl_flask_sid_to_context(ctx, xlinfo->ssidref,
+                                   &xlinfo->ssid_label, &size) < 0)
+        xlinfo->ssid_label = NULL;
 
     xlinfo->dying    = !!(xcinfo->flags&XEN_DOMINF_dying);
     xlinfo->shutdown = !!(xcinfo->flags&XEN_DOMINF_shutdown);
@@ -581,7 +587,7 @@ libxl_dominfo * libxl_list_domain(libxl_ctx *ctx, int *nb_domain_out)
     }
 
     for (i = 0; i < ret; i++) {
-        xcinfo2xlinfo(&info[i], &ptr[i]);
+        xcinfo2xlinfo(ctx, &info[i], &ptr[i]);
     }
     *nb_domain_out = ret;
     return ptr;
@@ -600,7 +606,7 @@ int libxl_domain_info(libxl_ctx *ctx, libxl_dominfo *info_r,
     if (ret==0 || xcinfo.domain != domid) return ERROR_INVAL;
 
     if (info_r)
-        xcinfo2xlinfo(&xcinfo, info_r);
+        xcinfo2xlinfo(ctx, &xcinfo, info_r);
     return 0;
 }
 
@@ -628,6 +634,11 @@ static int cpupool_info(libxl__gc *gc,
     }
 
     info->poolid = xcinfo->cpupool_id;
+    info->pool_name = libxl_cpupoolid_to_name(CTX, info->poolid);
+    if (!info->pool_name) {
+        rc = ERROR_FAIL;
+        goto out;
+    }
     info->sched = xcinfo->sched_id;
     info->n_dom = xcinfo->n_dom;
     rc = libxl_cpu_bitmap_alloc(CTX, &info->cpumap, 0);
@@ -4172,10 +4183,13 @@ retry_transaction:
         abort_transaction = 1;
         goto out;
     }
-    xcinfo2xlinfo(&info, &ptr);
+
+    libxl_dominfo_init(&ptr);
+    xcinfo2xlinfo(ctx, &info, &ptr);
     uuid = libxl__uuid2string(gc, ptr.uuid);
     libxl__xs_write(gc, t, libxl__sprintf(gc, "/vm/%s/memory", uuid),
             "%"PRIu32, new_target_memkb / 1024);
+    libxl_dominfo_dispose(&ptr);
 
 out:
     if (!xs_transaction_end(ctx->xsh, t, abort_transaction)
tools/libxl/libxl.h +20 -0
index 17b8a7b..8fbfa2d 100644
@@ -498,6 +498,26 @@
 #define LIBXL_EXTERNAL_CALLERS_ONLY /* disappears for callers outside libxl */
 #endif
 
+/*
+ * LIBXL_HAVE_SSID_LABEL
+ *
+ * If this is defined, then libxl IDL contains string of XSM security
+ * label in all XSM related structures.
+ *
+ * If set this string takes precedence over the numeric field.
+ */
+#define LIBXL_HAVE_SSID_LABEL 1
+
+/*
+ * LIBXL_HAVE_CPUPOOL_NAME
+ *
+ * If this is defined, then libxl IDL contains string of CPU pool
+ * name in all CPU pool related structures.
+ *
+ * If set this string takes precedence over the numeric field.
+ */
+#define LIBXL_HAVE_CPUPOOL_NAME 1
+
 typedef uint8_t libxl_mac[6];
 #define LIBXL_MAC_FMT "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx"
 #define LIBXL_MAC_FMTLEN ((2*6)+5) /* 6 hex bytes plus 5 colons */
tools/libxl/libxl_create.c +57 -0

63 lines not on this page · raw message

tools/libxl/libxl_dm.c +4 -0
index 51ab2bf..addacdb 100644
@@ -910,7 +910,11 @@ void libxl__spawn_stub_dm(libxl__egc *egc, libxl__stub_dm_spawn_state *sdss)
     dm_config->c_info.type = LIBXL_DOMAIN_TYPE_PV;
     dm_config->c_info.name = libxl__stub_dm_name(gc,
                                     libxl__domid_to_name(gc, guest_domid));
+    /* When we are here to launch stubdom, ssidref is a valid value
+     * already, no need to parse it again.
+     */
     dm_config->c_info.ssidref = guest_config->b_info.device_model_ssidref;
+    dm_config->c_info.ssid_label = NULL;
 
     libxl_uuid_generate(&dm_config->c_info.uuid);
 
tools/libxl/libxl_types.idl +6 -0
index f0f6e34..1018142 100644
@@ -215,6 +215,7 @@ libxl_dominfo = Struct("dominfo",[
     ("uuid",        libxl_uuid),
     ("domid",       libxl_domid),
     ("ssidref",     uint32),
+    ("ssid_label",  string),
     ("running",     bool),
     ("blocked",     bool),
     ("paused",      bool),
@@ -240,6 +241,7 @@ libxl_dominfo = Struct("dominfo",[
 
 libxl_cpupoolinfo = Struct("cpupoolinfo", [
     ("poolid",      uint32),
+    ("pool_name",   string),
     ("sched",       libxl_scheduler),
     ("n_dom",       uint32),
     ("cpumap",      libxl_bitmap)
@@ -270,11 +272,13 @@ libxl_domain_create_info = Struct("domain_create_info",[
     ("hap",          libxl_defbool),
     ("oos",          libxl_defbool),
     ("ssidref",      uint32),
+    ("ssid_label",   string),
     ("name",         string),
     ("uuid",         libxl_uuid),
     ("xsdata",       libxl_key_value_list),
     ("platformdata", libxl_key_value_list),
     ("poolid",       uint32),
+    ("pool_name",    string),
     ("run_hotplug_scripts",libxl_defbool),
     ("pvh",          libxl_defbool),
     ("driver_domain",libxl_defbool),
@@ -307,6 +311,7 @@ libxl_domain_build_info = Struct("domain_build_info",[
     ("shadow_memkb",    MemKB),
     ("rtc_timeoffset",  uint32),
     ("exec_ssidref",    uint32),
+    ("exec_ssid_label", string),
     ("localtime",       libxl_defbool),
     ("disable_migrate", libxl_defbool),
     ("cpuid",           libxl_cpuid_policy_list),
@@ -317,6 +322,7 @@ libxl_domain_build_info = Struct("domain_build_info",[
     # if you set device_model you must set device_model_version too
     ("device_model",     string),
     ("device_model_ssidref", uint32),
+    ("device_model_ssid_label", string),
 
     # extra parameters pass directly to qemu, NULL terminated
     ("extra",            libxl_string_list),
tools/libxl/xl_cmdimpl.c +30 -77

141 lines not on this page · raw message

tools/libxl/xl_sxp.c +2 -5
index a16a025..48eb608 100644
@@ -37,7 +37,6 @@ void printf_info_sexp(int domid, libxl_domain_config *d_config)
 
     libxl_domain_create_info *c_info = &d_config->c_info;
     libxl_domain_build_info *b_info = &d_config->b_info;
-    char *pool;
 
     printf("(domain\n\t(domid %d)\n", domid);
     printf("\t(create_info)\n");
@@ -55,10 +54,8 @@ void printf_info_sexp(int domid, libxl_domain_config *d_config)
     } else {
         printf("\t(uuid <unknown>)\n");
     }
-    pool = libxl_cpupoolid_to_name(ctx, c_info->poolid);
-    if (pool)
-        printf("\t(cpupool %s)\n", pool);
-    free(pool);
+    if (c_info->pool_name)
+        printf("\t(cpupool %s)\n", c_info->pool_name);
     if (c_info->xsdata)
         printf("\t(xsdata contains data)\n");
     else
Signature · 1 line
1.7.10.4
#2 lore raw

Re: [PATCH v7 1/9] xl / libxl: push parsing of SSID and CPU pool ID down to libxl

Quoted 21 lines · Wei Liu

This patch pushes parsing of "init_seclabel", "seclabel", "device_model_stubdomain_seclabel" and "pool" down to libxl level.

Originally the parsing is done in xl level, which is not ideal because libxl won't have the truely relevant information. With this patch libxl holds important information by itself.

The libxl IDL is extended to hold the string of labels and pool name. And if there those strings are present they take precedence over the numeric representations.

As all relevant structures (libxl_dominfo etc) have a field called X_name / X_label now, a string is also copied there so that callers won't have to do ID to name / label translation.

In order to be compatible with users of older versions of libxl, this patch also defines LIBXL_HAVE_SSID_LABEL and LIBXL_HAVE_CPUPOOL_NAME. If they are defined, the respective strings are available. And if those strings are not NULL, libxl will do the parsing and ignore the numeric values.

Quoted 4 lines
Cc Dario Faggioli <[email protected]>
Cc Juergen Gross <[email protected]>
Cc Daniel De Graaf <[email protected]>

This change looks good to me (untested), although I don't believe I am listed as a maintainer for any of it.

Quoted 11 lines
---
  tools/libxl/libxl.c         |   22 +++++++--
  tools/libxl/libxl.h         |   20 ++++++++
  tools/libxl/libxl_create.c  |   57 +++++++++++++++++++++++
  tools/libxl/libxl_dm.c      |    4 ++
  tools/libxl/libxl_types.idl |    6 +++
  tools/libxl/xl_cmdimpl.c    |  107 ++++++++++++-------------------------------
  tools/libxl/xl_sxp.c        |    7 +--
  7 files changed, 137 insertions(+), 86 deletions(-)
Signature · 2 lines
Daniel De Graaf
National Security Agency
author
#3 lore raw

Re: [PATCH v7 1/9] xl / libxl: push parsing of SSID and CPU pool ID down to libxl

Quoted 22 lines · Daniel De Graaf
Quoted 21 lines · Wei Liu

This patch pushes parsing of "init_seclabel", "seclabel", "device_model_stubdomain_seclabel" and "pool" down to libxl level.

Originally the parsing is done in xl level, which is not ideal because libxl won't have the truely relevant information. With this patch libxl holds important information by itself.

The libxl IDL is extended to hold the string of labels and pool name. And if there those strings are present they take precedence over the numeric representations.

As all relevant structures (libxl_dominfo etc) have a field called X_name / X_label now, a string is also copied there so that callers won't have to do ID to name / label translation.

In order to be compatible with users of older versions of libxl, this patch also defines LIBXL_HAVE_SSID_LABEL and LIBXL_HAVE_CPUPOOL_NAME. If they are defined, the respective strings are available. And if those strings are not NULL, libxl will do the parsing and ignore the numeric values.

Quoted 8 lines
Quoted 4 lines
Cc Dario Faggioli <[email protected]>
Cc Juergen Gross <[email protected]>
Cc Daniel De Graaf <[email protected]>

This change looks good to me (untested), although I don't believe I am listed as a maintainer for any of it.

I'm not at all familiar with these features so I CC'ed experts. Thanks for confirming.

Wei.

author
#4 lore raw

PATCH v7 2/9 libxl: libxl_uuid_copy now takes a ctx argument

In reply to Wei Liu

Make it consistent with existing libxl functions like libxl_bitmap_copy etc.

---
 tools/libxl/libxl.c        |    2 +-
 tools/libxl/libxl.h        |   14 +++++++++++---
 tools/libxl/libxl_create.c |    2 +-
 tools/libxl/libxl_utils.c  |    2 +-
 tools/libxl/libxl_uuid.c   |    8 ++++----
 tools/libxl/libxl_uuid.h   |    7 ++++++-
 6 files changed, 24 insertions(+), 11 deletions(-)
tools/libxl/libxl.c +1 -1
index 08f1d4d..38d4b2a 100644
@@ -2019,7 +2019,7 @@ int libxl_devid_to_device_vtpm(libxl_ctx *ctx,
         if(devid == vtpms[i].devid) {
             vtpm->backend_domid = vtpms[i].backend_domid;
             vtpm->devid = vtpms[i].devid;
-            libxl_uuid_copy(&vtpm->uuid, &vtpms[i].uuid);
+            libxl_uuid_copy(ctx, &vtpm->uuid, &vtpms[i].uuid);
             rc = 0;
             break;
         }
tools/libxl/libxl.h +11 -3
index 8fbfa2d..69ceac8 100644
@@ -319,13 +319,15 @@
 
 #include <xentoollog.h>
 
+typedef struct libxl__ctx libxl_ctx;
+
 #include <libxl_uuid.h>
 #include <_libxl_list.h>
 
 /* API compatibility. */
 #ifdef LIBXL_API_VERSION
 #if LIBXL_API_VERSION != 0x040200 && LIBXL_API_VERSION != 0x040300 && \
-    LIBXL_API_VERSION != 0x040400
+    LIBXL_API_VERSION != 0x040400 && LIBXL_API_VERSION != 0x040500
 #error Unknown LIBXL_API_VERSION
 #endif
 #endif
@@ -499,6 +501,14 @@
 #endif
 
 /*
+ *  LIBXL_HAVE_UUID_COPY_CTX_PARAM
+ *
+ * If this is defined, libxl_uuid_copy has changed to take a libxl_ctx
+ * structure.
+ */
+#define LIBXL_HAVE_UUID_COPY_CTX_PARAM 1
+
+/*
  * LIBXL_HAVE_SSID_LABEL
  *
  * If this is defined, then libxl IDL contains string of XSM security
@@ -594,8 +604,6 @@ bool libxl_defbool_val(libxl_defbool db);
 
 const char *libxl_defbool_to_string(libxl_defbool b);
 
-typedef struct libxl__ctx libxl_ctx;
-
 #define LIBXL_TIMER_MODE_DEFAULT -1
 #define LIBXL_MEMKB_DEFAULT ~0ULL
 
tools/libxl/libxl_create.c +1 -1
index fe3bdd2..da1517c 100644
@@ -479,7 +479,7 @@ int libxl__domain_make(libxl__gc *gc, libxl_domain_create_info *info,
     *domid = -1;
 
     /* Ultimately, handle is an array of 16 uint8_t, same as uuid */
-    libxl_uuid_copy((libxl_uuid *)handle, &info->uuid);
+    libxl_uuid_copy(ctx, (libxl_uuid *)handle, &info->uuid);
 
     ret = xc_domain_create(ctx->xch, info->ssidref, handle, flags, domid);
     if (ret < 0) {
tools/libxl/libxl_utils.c +1 -1
index 476921e..16b734e 100644
@@ -536,7 +536,7 @@ int libxl_uuid_to_device_vtpm(libxl_ctx *ctx, uint32_t domid,
         if(!libxl_uuid_compare(uuid, &vtpms[i].uuid)) {
             vtpm->backend_domid = vtpms[i].backend_domid;
             vtpm->devid = vtpms[i].devid;
-            libxl_uuid_copy(&vtpm->uuid, &vtpms[i].uuid);
+            libxl_uuid_copy(ctx, &vtpm->uuid, &vtpms[i].uuid);
             rc = 0;
             break;
         }
tools/libxl/libxl_uuid.c +4 -4
index 6591cb7..172b7d1 100644
@@ -14,8 +14,6 @@
 
 #include "libxl_osdeps.h" /* must come before any other headers */
 
-#include <libxl_uuid.h>
-
 #include "libxl_internal.h"
 
 #if defined(__linux__)
@@ -35,7 +33,8 @@ int libxl_uuid_from_string(libxl_uuid *uuid, const char *in)
      return uuid_parse(in, uuid->uuid);
 }
 
-void libxl_uuid_copy(libxl_uuid *dst, const libxl_uuid *src)
+void libxl_uuid_copy(libxl_ctx *ctx_opt, libxl_uuid *dst,
+                     const libxl_uuid *src)
 {
      uuid_copy(dst->uuid, src->uuid);
 }
@@ -87,7 +86,8 @@ int libxl_uuid_from_string(libxl_uuid *uuid, const char *in)
 }
 #undef LIBXL__UUID_PTRS
 
-void libxl_uuid_copy(libxl_uuid *dst, const libxl_uuid *src)
+void libxl_uuid_copy(libxl_ctx *ctx_opt, libxl_uuid *dst,
+                     const libxl_uuid *src)
 {
      memcpy(dst->uuid, src->uuid, sizeof(dst->uuid));
 }
tools/libxl/libxl_uuid.h +6 -1
index fbde7b6..0c2a1e7 100644
@@ -56,7 +56,12 @@ typedef struct {
 int libxl_uuid_is_nil(const libxl_uuid *uuid);
 void libxl_uuid_generate(libxl_uuid *uuid);
 int libxl_uuid_from_string(libxl_uuid *uuid, const char *in);
-void libxl_uuid_copy(libxl_uuid *dst, const libxl_uuid *src);
+void libxl_uuid_copy(libxl_ctx *ctx_opt, libxl_uuid *dst,
+                     const libxl_uuid *src);
+#if defined(LIBXL_API_VERSION) && LIBXL_API_VERSION < 0x040500
+void libxl_uuid_copy(dst, src) libxl_uuid_copy(NULL, dst, src)
+#endif
+
 void libxl_uuid_clear(libxl_uuid *uuid);
 int libxl_uuid_compare(const libxl_uuid *uuid1, const libxl_uuid *uuid2);
 const uint8_t *libxl_uuid_bytearray_const(const libxl_uuid *uuid);
Signature · 1 line
1.7.10.4
author
#5 lore raw

PATCH v7 3/9 libxl_internal: functions to check default values for builtin types

In reply to Wei Liu

They will be used in later patch to determine whether we should generate JSON output for a type. If that type has default value we just skip generating JSON output.

---
 tools/libxl/libxl.c          |    3 +--
 tools/libxl/libxl_internal.c |    6 ++++++
 tools/libxl/libxl_internal.h |   27 +++++++++++++++++++++++++++
 3 files changed, 34 insertions(+), 2 deletions(-)
tools/libxl/libxl.c +1 -2
index 38d4b2a..1b0b08f 100644
@@ -2858,8 +2858,7 @@ int libxl__device_nic_setdefault(libxl__gc *gc, libxl_device_nic *nic,
         nic->model = strdup("rtl8139");
         if (!nic->model) return ERROR_NOMEM;
     }
-    if (!nic->mac[0] && !nic->mac[1] && !nic->mac[2] &&
-        !nic->mac[3] && !nic->mac[4] && !nic->mac[5]) {
+    if (libxl__mac_is_default(&nic->mac)) {
         const uint8_t *r;
         libxl_uuid uuid;
 
tools/libxl/libxl_internal.c +6 -0
index 6c94d3e..81f8985 100644
@@ -307,6 +307,12 @@ _hidden int libxl__compare_macs(libxl_mac *a, libxl_mac *b)
     return 0;
 }
 
+_hidden int libxl__mac_is_default(libxl_mac *mac)
+{
+    return (!(*mac)[0] && !(*mac)[1] && !(*mac)[2] &&
+            !(*mac)[3] && !(*mac)[4] && !(*mac)[5]);
+}
+
 _hidden int libxl__init_recursive_mutex(libxl_ctx *ctx, pthread_mutex_t *lock)
 {
     pthread_mutexattr_t attr;
tools/libxl/libxl_internal.h +27 -0
index a0d4f24..2a8f552 100644
@@ -1551,6 +1551,8 @@ struct libxl__xen_console_reader {
 _hidden int libxl__parse_mac(const char *s, libxl_mac mac);
 /* compare mac address @a and @b. 0 if the same, -ve if a<b and +ve if a>b */
 _hidden int libxl__compare_macs(libxl_mac *a, libxl_mac *b);
+/* return true if mac address is all zero (the default value) */
+_hidden int libxl__mac_is_default(libxl_mac *mac);
 /* init a recursive mutex */
 _hidden int libxl__init_recursive_mutex(libxl_ctx *ctx, pthread_mutex_t *lock);
 
@@ -3097,6 +3099,10 @@ void libxl__numa_candidate_put_nodemap(libxl__gc *gc,
 #define LIBXL__DEFBOOL_STR_DEFAULT "<default>"
 #define LIBXL__DEFBOOL_STR_FALSE   "False"
 #define LIBXL__DEFBOOL_STR_TRUE    "True"
+static inline int libxl__defbool_is_default(libxl_defbool *db)
+{
+    return !db->val;
+}
 
 /*
  * Inserts "elm_new" into the sorted list "head".
@@ -3180,6 +3186,27 @@ int libxl__uint64_parse_json(libxl__gc *gc, const libxl__json_object *o,
 int libxl__string_parse_json(libxl__gc *gc, const libxl__json_object *o,
                              char **p);
 
+/* This always return false, there's no "default value" for hw cap */
+static inline int libxl__hwcap_is_default(libxl_hwcap *hwcap)
+{
+    return 0;
+}
+
+static inline int libxl__string_list_is_empty(libxl_string_list *psl)
+{
+    return !libxl_string_list_length(psl);
+}
+
+static inline int libxl__key_value_list_is_empty(libxl_key_value_list *pkvl)
+{
+    return !libxl_key_value_list_length(pkvl);
+}
+
+static inline int libxl__cpuid_policy_is_empty(libxl_cpuid_policy_list *pl)
+{
+    return !libxl_cpuid_policy_list_length(pl);
+}
+
 #endif
 
 /*
Signature · 1 line
1.7.10.4
#6 lore raw

Re: [PATCH v7 3/9] libxl_internal: functions to check default values for builtin types

Quoted 1 line · Wei Liu

+static inline int libxl__defbool_is_default(libxl_defbool *db)

This already exists, but with a libxl_ prefix only.

author
#7 lore raw

Re: [PATCH v7 3/9] libxl_internal: functions to check default values for builtin types

Quoted 5 lines · Ian Campbell
Quoted 1 line · Wei Liu

+static inline int libxl__defbool_is_default(libxl_defbool *db)

This already exists, but with a libxl_ prefix only.

Ah, somehow I missed that one.

Any comments on other functions?

Wei.

#8 lore raw

Re: [PATCH v7 3/9] libxl_internal: functions to check default values for builtin types

Quoted 10 lines · Wei Liu
Quoted 5 lines · Ian Campbell
Quoted 1 line · Wei Liu

+static inline int libxl__defbool_is_default(libxl_defbool *db)

This already exists, but with a libxl_ prefix only.

Ah, somehow I missed that one.

Any comments on other functions?

I didn't explicitly check for similar external functions, but the ones you've got look ok if you've done that check already.

Ian.

author
#9 lore raw

Re: [PATCH v7 3/9] libxl_internal: functions to check default values for builtin types

In reply to Ian Campbell
Quoted 5 lines · Ian Campbell
Quoted 1 line · Wei Liu

+static inline int libxl__defbool_is_default(libxl_defbool *db)

This already exists, but with a libxl_ prefix only.

Now I remeber why I introduced a new function - libx_defbool_is_default doesn't take a pointer and I didn't want to special-case defbool in python generator.

What do you think?

Wei.

#10 lore raw

Re: [PATCH v7 3/9] libxl_internal: functions to check default values for builtin types

Quoted 4 lines · Wei Liu
Quoted 3 lines · Ian Campbell
Quoted 1 line · Wei Liu

+static inline int libxl__defbool_is_default(libxl_defbool *db)

Quoted 8 lines
Quoted 2 lines

This already exists, but with a libxl_ prefix only.

Now I remeber why I introduced a new function - libx_defbool_is_default doesn't take a pointer and I didn't want to special-case defbool in python generator.

What do you think?

OK, I guess.

Ian.

author
#11 lore raw

PATCH v7 4/9 libxl/gentypes.py: don't generate default values

In reply to Wei Liu

If a field:
0. is not of aggregate type
and
1. is of array type and the array is not empty

or  2. is of a type which has init_val and has been set to init_val,
or  3. is of builtin type and has been set to internal default value,
or  4. has been set to 0

then there's no need to generate output for that field in JSON output.

Note that 0 can result in output like

  {
    ...
    FOO : { }
    ...

}
where FOO is aggregate type but all its fields are set to default, hence no JSON output in {} at all. This is not pretty, but it's still valid JSON. And the parser should be able to skip touching those fields in the resulting C structures. When the parser consumes that generated JSON object, all default values should be automatically filled in.

Also change some non-zero init_vals to LIBXL_* for better readability in generated C code.

---
 tools/libxl/gentypes.py     |   33 +++++++++++++++++++++++++++++++--
 tools/libxl/idl.py          |    2 ++
 tools/libxl/idl.txt         |    9 +++++++++
 tools/libxl/libxl_types.idl |   29 +++++++++++++++++------------
 4 files changed, 59 insertions(+), 14 deletions(-)
tools/libxl/gentypes.py +31 -2
index 01416e7..f06afd9 100644
@@ -206,6 +206,29 @@ def libxl_C_type_gen_map_key(f, parent, indent = ""):
         s = indent + s
     return s.replace("\n", "\n%s" % indent).rstrip(indent)
 
+def get_init_val(f):
+    if f.init_val is not None:
+        return f.init_val
+    elif f.type.init_val is not None:
+        return f.type.init_val
+    return None
+
+def get_default_expr(f, nparent, fexpr):
+    if isinstance(f.type, idl.Aggregate):
+        return "1 /* always generate JSON output for aggregate type */"
+
+    if isinstance(f.type, idl.Array):
+        return "%s && %s" % (fexpr, nparent + f.type.lenvar.name)
+
+    init_val = get_init_val(f)
+    if init_val is not None:
+        return "%s != %s" % (fexpr, init_val)
+
+    if f.type.check_default_fn:
+        return "!%s(&%s)" % (f.type.check_default_fn, fexpr)
+
+    return "%s" % fexpr
+
 def libxl_C_type_gen_json(ty, v, indent = "    ", parent = None):
     s = ""
     if parent is None:
@@ -255,8 +278,14 @@ def libxl_C_type_gen_json(ty, v, indent = "    ", parent = None):
         s += "    goto out;\n"
         for f in [f for f in ty.fields if not f.const and not f.type.private]:
             (nparent,fexpr) = ty.member(v, f, parent is None)
-            s += libxl_C_type_gen_map_key(f, nparent)
-            s += libxl_C_type_gen_json(f.type, fexpr, "", nparent)
+            default_expr = get_default_expr(f, nparent, fexpr)
+            s += "if (%s) {\n" % default_expr
+
+            s += libxl_C_type_gen_map_key(f, nparent, "    ")
+            s += libxl_C_type_gen_json(f.type, fexpr, "    ", nparent)
+
+            s += "}\n"
+
         s += "s = yajl_gen_map_close(hand);\n"
         s += "if (s != yajl_gen_status_ok)\n"
         s += "    goto out;\n"
tools/libxl/idl.py +2 -0
index 8b118dd..ada801a 100644
@@ -64,6 +64,8 @@ class Type(object):
         self.init_val = kwargs.setdefault('init_val', None)
         self.autogenerate_init_fn = kwargs.setdefault('autogenerate_init_fn', False)
 
+        self.check_default_fn = kwargs.setdefault('check_default_fn', None)
+
         if self.typename is not None and not self.private:
             self.json_gen_fn = kwargs.setdefault('json_gen_fn', self.typename + "_gen_json")
         else:
tools/libxl/idl.txt +9 -0
index 6a53dd8..d47eba8 100644
@@ -69,6 +69,15 @@ Type.autogenerate_json: (default: True)
 
  Indicates if the above named Type.json_gen_fn should be autogenerated.
 
+Type.check_default_fn:
+
+ If it's set then calling this function shall return true if this type
+ has been set to default value (internal libxl implementation).
+
+ If this is not set, that means we can check the type against init_val
+ (if it has one) or zero to determine whether the value is default
+ value.
+
 Other simple type-Classes
 -------------------------
 
tools/libxl/libxl_types.idl +17 -12
index 1018142..7234519 100644
@@ -5,18 +5,23 @@
 
 namespace("libxl_")
 
-libxl_defbool = Builtin("defbool", passby=PASS_BY_REFERENCE)
+libxl_defbool = Builtin("defbool", passby=PASS_BY_REFERENCE, check_default_fn="libxl__defbool_is_default")
 
 libxl_domid = Builtin("domid", json_gen_fn = "yajl_gen_integer", autogenerate_json = False)
 libxl_devid = Builtin("devid", json_gen_fn = "yajl_gen_integer", autogenerate_json = False, signed = True, init_val="-1")
-libxl_uuid = Builtin("uuid", passby=PASS_BY_REFERENCE)
-libxl_mac = Builtin("mac", passby=PASS_BY_REFERENCE)
-libxl_bitmap = Builtin("bitmap", dispose_fn="libxl_bitmap_dispose", passby=PASS_BY_REFERENCE)
-libxl_cpuid_policy_list = Builtin("cpuid_policy_list", dispose_fn="libxl_cpuid_dispose", passby=PASS_BY_REFERENCE)
-
-libxl_string_list = Builtin("string_list", dispose_fn="libxl_string_list_dispose", passby=PASS_BY_REFERENCE)
-libxl_key_value_list = Builtin("key_value_list", dispose_fn="libxl_key_value_list_dispose", passby=PASS_BY_REFERENCE)
-libxl_hwcap = Builtin("hwcap", passby=PASS_BY_REFERENCE)
+libxl_uuid = Builtin("uuid", passby=PASS_BY_REFERENCE, check_default_fn="libxl_uuid_is_nil")
+libxl_mac = Builtin("mac", passby=PASS_BY_REFERENCE, check_default_fn="libxl__mac_is_default")
+libxl_bitmap = Builtin("bitmap", dispose_fn="libxl_bitmap_dispose", passby=PASS_BY_REFERENCE,
+                       check_default_fn="libxl_bitmap_is_empty")
+libxl_cpuid_policy_list = Builtin("cpuid_policy_list", dispose_fn="libxl_cpuid_dispose", passby=PASS_BY_REFERENCE,
+                                  check_default_fn="libxl__cpuid_policy_is_empty")
+
+libxl_string_list = Builtin("string_list", dispose_fn="libxl_string_list_dispose", passby=PASS_BY_REFERENCE,
+                            check_default_fn="libxl__string_list_is_empty")
+libxl_key_value_list = Builtin("key_value_list", dispose_fn="libxl_key_value_list_dispose", passby=PASS_BY_REFERENCE,
+                               check_default_fn="libxl__key_value_list_is_empty")
+libxl_hwcap = Builtin("hwcap", passby=PASS_BY_REFERENCE,
+                      check_default_fn="libxl__hwcap_is_default")
 
 #
 # Specific integer types
@@ -49,7 +54,7 @@ libxl_domain_type = Enumeration("domain_type", [
     (-1, "INVALID"),
     (1, "HVM"),
     (2, "PV"),
-    ], init_val = -1)
+    ], init_val = "LIBXL_DOMAIN_TYPE_INVALID")
 
 libxl_device_model_version = Enumeration("device_model_version", [
     (0, "UNKNOWN"),
@@ -95,7 +100,7 @@ libxl_action_on_shutdown = Enumeration("action_on_shutdown", [
 
     (5, "COREDUMP_DESTROY"),
     (6, "COREDUMP_RESTART"),
-    ], init_val = 1)
+    ], init_val = "LIBXL_ACTION_ON_SHUTDOWN_DESTROY")
 
 libxl_trigger = Enumeration("trigger", [
     (0, "UNKNOWN"),
@@ -154,7 +159,7 @@ libxl_vga_interface_type = Enumeration("vga_interface_type", [
     (1, "CIRRUS"),
     (2, "STD"),
     (3, "NONE"),
-    ], init_val = 1)
+    ], init_val = "LIBXL_VGA_INTERFACE_TYPE_CIRRUS")
 
 libxl_vendor_device = Enumeration("vendor_device", [
     (0, "NONE"),
Signature · 1 line
1.7.10.4
#12 lore raw

Re: [PATCH v7 4/9] libxl/gentypes.py: don't generate default values

Quoted 7 lines · Wei Liu

If a field:
0. is not of aggregate type
and
1. is of array type and the array is not empty

or  2. is of a type which has init_val and has been set to init_val,
or  3. is of builtin type and has been set to internal default value,
or  4. has been set to 0

This isn't quite true, since #4 depends on their being no init_val. Prepend "is of a type which has no init_val and "?

Quoted 1 line

Other than that: Acked-by: Ian Campbell <[email protected]>

author
#13 lore raw

Re: [PATCH v7 4/9] libxl/gentypes.py: don't generate default values

Quoted 9 lines · Ian Campbell
Quoted 7 lines · Wei Liu

If a field:
0. is not of aggregate type
and
1. is of array type and the array is not empty

or  2. is of a type which has init_val and has been set to init_val,
or  3. is of builtin type and has been set to internal default value,
or  4. has been set to 0
Quoted 3 lines

This isn't quite true, since #4 depends on their being no init_val. Prepend "is of a type which has no init_val and "?

You're right. I will prepend that sentence to #4.

Wei.

Quoted 4 lines
Quoted 1 line

Other than that: Acked-by: Ian Campbell <[email protected]>

author
#14 lore raw

PATCH v7 5/9 libxl IDL: generate code to parse libxl__json_object to libxl_FOO struct

In reply to Wei Liu

libxl_FOO_parse_json functions are generated.

Note that these functions are used to parse libxl__json_object to libxl__FOO struct. They don't consume JSON string.

The new function definitions are generated to new header files called __libxl_types_*_json_internal.h so that they don't contaiminate public header.

---
Change since last version: output parser function definitions to dedicated header file.

---
 tools/libxl/Makefile                 |    4 +-
 tools/libxl/gentypes.py              |  158 +++++++++++++++++++++++++++++++++-
 tools/libxl/idl.py                   |   15 ++++
 tools/libxl/idl.txt                  |    7 +-
 tools/libxl/libxl.h                  |   14 +++
 tools/libxl/libxl_internal.h         |    3 +
 tools/libxl/libxl_types.idl          |   27 +++---
 tools/libxl/libxl_types_internal.idl |    4 +-
 8 files changed, 214 insertions(+), 18 deletions(-)
tools/libxl/Makefile +3 -1
index 4cfa275..bac6287 100644
@@ -180,9 +180,11 @@ $(LIBXL_OBJS) $(LIBXL_TEST_OBJS) $(LIBXLU_OBJS) \
 $(LIBXL_OBJS) $(LIBXL_TEST_OBJS): libxl_internal.h
 
 _libxl_type%.h _libxl_type%_json.h _libxl_type%.c: libxl_type%.idl gentypes.py idl.py
-	$(PYTHON) gentypes.py libxl_type$*.idl __libxl_type$*.h __libxl_type$*_json.h __libxl_type$*.c
+	$(PYTHON) gentypes.py libxl_type$*.idl __libxl_type$*.h __libxl_type$*_json.h \
+		__libxl_type$*_json_internal.h __libxl_type$*.c
 	$(call move-if-changed,__libxl_type$*.h,_libxl_type$*.h)
 	$(call move-if-changed,__libxl_type$*_json.h,_libxl_type$*_json.h)
+	$(call move-if-changed,__libxl_type$*_json_internal.h,_libxl_type$*_json_internal.h)
 	$(call move-if-changed,__libxl_type$*.c,_libxl_type$*.c)
 
 libxenlight.so: libxenlight.so.$(MAJOR)
tools/libxl/gentypes.py +155 -3

188 lines not on this page · raw message

tools/libxl/idl.py +15 -0
index ada801a..381f11b 100644
@@ -68,8 +68,12 @@ class Type(object):
 
         if self.typename is not None and not self.private:
             self.json_gen_fn = kwargs.setdefault('json_gen_fn', self.typename + "_gen_json")
+            self.json_parse_type = kwargs.setdefault('json_parse_type', "JSON_ANY")
+            self.json_parse_fn = kwargs.setdefault('json_parse_fn', self.typename + "_parse_json")
         else:
             self.json_gen_fn = kwargs.setdefault('json_gen_fn', None)
+            self.json_parse_type = kwargs.setdefault('json_parse_type', None)
+            self.json_parse_fn = kwargs.setdefault('json_parse_fn', None)
 
         self.autogenerate_json = kwargs.setdefault('autogenerate_json', True)
 
@@ -121,6 +125,9 @@ class Number(Builtin):
         kwargs.setdefault('dispose_fn', None)
         kwargs.setdefault('signed', False)
         kwargs.setdefault('json_gen_fn', "yajl_gen_integer")
+        kwargs.setdefault('json_parse_type', "JSON_INTEGER")
+        # json_parse_fn might be overriden on specific type
+        kwargs.setdefault('json_parse_fn', "libxl__int_parse_json")
         self.signed = kwargs['signed']
         Builtin.__init__(self, ctype, **kwargs)
 
@@ -128,6 +135,7 @@ class UInt(Number):
     def __init__(self, w, **kwargs):
         kwargs.setdefault('namespace', None)
         kwargs.setdefault('dispose_fn', None)
+        kwargs.setdefault('json_parse_fn', "libxl__uint%d_parse_json" % w)
         Number.__init__(self, "uint%d_t" % w, **kwargs)
 
         self.width = w
@@ -144,6 +152,7 @@ class EnumerationValue(object):
 class Enumeration(Type):
     def __init__(self, typename, values, **kwargs):
         kwargs.setdefault('dispose_fn', None)
+        kwargs.setdefault('json_parse_type', "JSON_STRING")
         Type.__init__(self, typename, **kwargs)
 
         self.value_namespace = kwargs.setdefault('value_namespace',
@@ -173,6 +182,7 @@ class Field(object):
 class Aggregate(Type):
     """A type containing a collection of other types"""
     def __init__(self, kind, typename, fields, **kwargs):
+        kwargs.setdefault('json_parse_type', "JSON_MAP")
         Type.__init__(self, typename, **kwargs)
 
         if self.typename is not None:
@@ -259,6 +269,8 @@ class KeyedUnion(Aggregate):
 void = Builtin("void *", namespace = None)
 bool = Builtin("bool", namespace = None,
                json_gen_fn = "yajl_gen_bool",
+               json_parse_type = "JSON_BOOL",
+               json_parse_fn = "libxl__bool_parse_json",
                autogenerate_json = False)
 
 size_t = Number("size_t", namespace = None)
@@ -272,12 +284,15 @@ uint64 = UInt(64, json_gen_fn = "libxl__uint64_gen_json")
 
 string = Builtin("char *", namespace = None, dispose_fn = "free",
                  json_gen_fn = "libxl__string_gen_json",
+                 json_parse_type = "JSON_STRING | JSON_NULL",
+                 json_parse_fn = "libxl__string_parse_json",
                  autogenerate_json = False)
 
 class Array(Type):
     """An array of the same type"""
     def __init__(self, elem_type, lenvar_name, **kwargs):
         kwargs.setdefault('dispose_fn', 'free')
+        kwargs.setdefault('json_parse_type', 'JSON_ARRAY')
         Type.__init__(self, namespace=elem_type.namespace, typename=elem_type.rawname + " *", **kwargs)
 
         lv_kwargs = dict([(x.lstrip('lenvar_'),y) for (x,y) in kwargs.items() if x.startswith('lenvar_')])
tools/libxl/idl.txt +6 -1
index d47eba8..87c4952 100644
@@ -65,9 +65,14 @@ Type.json_gen_fn: (default: typename + "_gen_json" or None if type == None)
  The name of the C function which will generate a YAJL data structure
  representing this type.
 
+Type.json_parse_fn: (default: typename + "_parse_json" or None if type == None)
+
+ The name of the C function which will parse a libxl JSON structure
+ representing this type to C type.
+
 Type.autogenerate_json: (default: True)
 
- Indicates if the above named Type.json_gen_fn should be autogenerated.
+ Indicates if the above named Type.json_*_fn should be autogenerated.
 
 Type.check_default_fn:
 
tools/libxl/libxl.h +14 -0
index 69ceac8..e52a43f 100644
@@ -305,6 +305,20 @@
  *
  *    Generates a JSON object from "p" in the form of a NULL terminated
  *    string.
+ *
+ * <type *> libxl_<type>_from_json(const char *json)
+ * int      libxl_<type>_from_json(const char *json)
+ *
+ *    Parses "json" and returns:
+ *
+ *    an int value, if <type> is enumeration type. The value is the enum value
+ *    representing the respective string in "json".
+ *
+ *    an instance of <type>, if <type> is aggregate type. The returned
+ *    instance has its fields filled in by the parser according to "json".
+ *
+ *    If the parsing fails, caller cannot rely on the value / instance
+ *    returned.
  */
 #ifndef LIBXL_H
 #define LIBXL_H
tools/libxl/libxl_internal.h +3 -0
index 2a8f552..37fd77f 100644
@@ -3186,6 +3186,9 @@ int libxl__uint64_parse_json(libxl__gc *gc, const libxl__json_object *o,
 int libxl__string_parse_json(libxl__gc *gc, const libxl__json_object *o,
                              char **p);
 
+#include "_libxl_types_json_internal.h"
+#include "_libxl_types_internal_json_internal.h"
+
 /* This always return false, there's no "default value" for hw cap */
 static inline int libxl__hwcap_is_default(libxl_hwcap *hwcap)
 {
tools/libxl/libxl_types.idl +15 -12
index 7234519..2cb856c 100644
@@ -5,22 +5,25 @@
 
 namespace("libxl_")
 
-libxl_defbool = Builtin("defbool", passby=PASS_BY_REFERENCE, check_default_fn="libxl__defbool_is_default")
-
-libxl_domid = Builtin("domid", json_gen_fn = "yajl_gen_integer", autogenerate_json = False)
-libxl_devid = Builtin("devid", json_gen_fn = "yajl_gen_integer", autogenerate_json = False, signed = True, init_val="-1")
-libxl_uuid = Builtin("uuid", passby=PASS_BY_REFERENCE, check_default_fn="libxl_uuid_is_nil")
-libxl_mac = Builtin("mac", passby=PASS_BY_REFERENCE, check_default_fn="libxl__mac_is_default")
-libxl_bitmap = Builtin("bitmap", dispose_fn="libxl_bitmap_dispose", passby=PASS_BY_REFERENCE,
+libxl_defbool = Builtin("defbool", json_parse_type="JSON_STRING", passby=PASS_BY_REFERENCE,
+                        check_default_fn="libxl__defbool_is_default")
+
+libxl_domid = Builtin("domid", json_gen_fn = "yajl_gen_integer", json_parse_fn = "libxl__uint32_parse_json",
+                      json_parse_type = "JSON_INTEGER", autogenerate_json = False)
+libxl_devid = Builtin("devid", json_gen_fn = "yajl_gen_integer", json_parse_fn = "libxl__int_parse_json",
+                      json_parse_type = "JSON_INTEGER", autogenerate_json = False, signed = True, init_val="-1")
+libxl_uuid = Builtin("uuid", json_parse_type="JSON_STRING", passby=PASS_BY_REFERENCE, check_default_fn="libxl_uuid_is_nil")
+libxl_mac = Builtin("mac", json_parse_type="JSON_STRING", passby=PASS_BY_REFERENCE, check_default_fn="libxl__mac_is_default")
+libxl_bitmap = Builtin("bitmap", json_parse_type="JSON_ARRAY", dispose_fn="libxl_bitmap_dispose", passby=PASS_BY_REFERENCE,
                        check_default_fn="libxl_bitmap_is_empty")
 libxl_cpuid_policy_list = Builtin("cpuid_policy_list", dispose_fn="libxl_cpuid_dispose", passby=PASS_BY_REFERENCE,
-                                  check_default_fn="libxl__cpuid_policy_is_empty")
+                                  json_parse_type="JSON_ARRAY", check_default_fn="libxl__cpuid_policy_is_empty")
 
 libxl_string_list = Builtin("string_list", dispose_fn="libxl_string_list_dispose", passby=PASS_BY_REFERENCE,
-                            check_default_fn="libxl__string_list_is_empty")
+                            json_parse_type="JSON_ARRAY", check_default_fn="libxl__string_list_is_empty")
 libxl_key_value_list = Builtin("key_value_list", dispose_fn="libxl_key_value_list_dispose", passby=PASS_BY_REFERENCE,
-                               check_default_fn="libxl__key_value_list_is_empty")
-libxl_hwcap = Builtin("hwcap", passby=PASS_BY_REFERENCE,
+                               json_parse_type="JSON_MAP", check_default_fn="libxl__key_value_list_is_empty")
+libxl_hwcap = Builtin("hwcap", passby=PASS_BY_REFERENCE, json_parse_type="JSON_ARRAY",
                       check_default_fn="libxl__hwcap_is_default")
 
 #
@@ -586,7 +589,7 @@ libxl_event_type = Enumeration("event_type", [
 
 libxl_ev_user = UInt(64)
 
-libxl_ev_link = Builtin("ev_link", passby=PASS_BY_REFERENCE, private=True)
+libxl_ev_link = Builtin("ev_link", json_parse_type="JSON_STRING", passby=PASS_BY_REFERENCE, private=True)
 
 libxl_event = Struct("event",[
     ("link",     libxl_ev_link),
tools/libxl/libxl_types_internal.idl +3 -1
index a964851..17533f1 100644
@@ -1,7 +1,9 @@
 namespace("libxl__")
 hidden(True)
 
-libxl_domid = Builtin("domid", namespace="libxl_", json_gen_fn = "yajl_gen_integer")
+libxl_domid = Builtin("domid", namespace="libxl_", json_gen_fn = "yajl_gen_integer",
+		      json_parse_fn = "libxl__uint32_parse_json", json_parse_type = "JSON_INTEGER",
+		      autogenerate_json = False)
 
 libxl__qmp_message_type = Enumeration("qmp_message_type", [
     (1, "QMP"),
Signature · 1 line
1.7.10.4
#15 lore raw

Re: [PATCH v7 5/9] libxl IDL: generate code to parse libxl__json_object to libxl_FOO struct

Quoted 8 lines · Wei Liu

libxl_FOO_parse_json functions are generated.

Note that these functions are used to parse libxl__json_object to libxl__FOO struct. They don't consume JSON string.

The new function definitions are generated to new header files called __libxl_types_*_json_internal.h so that they don't contaiminate public header.

"contaminate".

I'm a bit confused what is going into this file. It seems to be these two:

    f.write("int %s_parse_json(libxl__gc *gc, const libxl__json_object *%s, %s)\n" % (ty.typename,"o",ty.make_arg("p", passby=idl.PASS_BY_REFERENCE)))
    f.write("int %s_from_json(libxl_ctx *ctx, %s, const char *s)\n" % (ty.typename, ty.make_arg("p", passby=idl.PASS_BY_REFERENCE)))

The first looks internal (it takes const libxl__json_object which is internal) but it is in the libxl_ namespace.

The second looks like it should be public in _libxl_json.h, shouldn't it?

I think you need to put the first function into a new _libxl_types_*_internal.h (no need to make it specific to internal json functions), and the second into just regular _libxl_types.h (it doesn't require the application to opt into using yajl so it doesn't have to go into _libxl_types_json.h)

Ian.

author
#16 lore raw

Re: [PATCH v7 5/9] libxl IDL: generate code to parse libxl__json_object to libxl_FOO struct

Quoted 12 lines · Ian Campbell
Quoted 8 lines · Wei Liu

libxl_FOO_parse_json functions are generated.

Note that these functions are used to parse libxl__json_object to libxl__FOO struct. They don't consume JSON string.

The new function definitions are generated to new header files called __libxl_types_*_json_internal.h so that they don't contaiminate public header.

"contaminate".

Fixed. Thanks.

Quoted 10 lines

I'm a bit confused what is going into this file. It seems to be these two:

    f.write("int %s_parse_json(libxl__gc *gc, const libxl__json_object *%s, %s)\n" % (ty.typename,"o",ty.make_arg("p", passby=idl.PASS_BY_REFERENCE)))
    f.write("int %s_from_json(libxl_ctx *ctx, %s, const char *s)\n" % (ty.typename, ty.make_arg("p", passby=idl.PASS_BY_REFERENCE)))

The first looks internal (it takes const libxl__json_object which is internal) but it is in the libxl_ namespace.

The first one should be internal. I should use libxl__ namespace in my next version. This is loose end I missed.

Quoted 3 lines

The second looks like it should be public in _libxl_json.h, shouldn't it?

Yes, you're right.

Quoted 3 lines

I think you need to put the first function into a new _libxl_types_*_internal.h (no need to make it specific to internal json functions),

This one sounds good.

Quoted 4 lines

and the second into just regular _libxl_types.h (it doesn't require the application to opt into using yajl so it doesn't have to go into _libxl_types_json.h)

I don't think it can work without YAJL.

Wei.

Quoted 2 lines

Ian.

#17 lore raw

Re: [PATCH v7 5/9] libxl IDL: generate code to parse libxl__json_object to libxl_FOO struct

Quoted 6 lines · Wei Liu
Quoted 4 lines

and the second into just regular _libxl_types.h (it doesn't require the application to opt into using yajl so it doesn't have to go into _libxl_types_json.h)

I don't think it can work without YAJL.

It takes a const char * and returns a libxl device. The *application* has no need to see any yajl, only libxl does, which is the distinction here.

IOW from_json can be used by an application without ever #including <yajl.h>

Contrast the libxl_foo_gen_json functions which take a "yajl_gen hand" and therefore require that the application be using yajl itself

Ian.

author
#18 lore raw

Re: [PATCH v7 5/9] libxl IDL: generate code to parse libxl__json_object to libxl_FOO struct

Quoted 12 lines · Ian Campbell
Quoted 6 lines · Wei Liu
Quoted 4 lines

and the second into just regular _libxl_types.h (it doesn't require the application to opt into using yajl so it doesn't have to go into _libxl_types_json.h)

I don't think it can work without YAJL.

It takes a const char * and returns a libxl device. The *application* has no need to see any yajl, only libxl does, which is the distinction here.

Quoted 6 lines

IOW from_json can be used by an application without ever #including <yajl.h>

Contrast the libxl_foo_gen_json functions which take a "yajl_gen hand" and therefore require that the application be using yajl itself

I see. I misunderstood. Thanks for clarification.

Wei.

Quoted 1 line

Ian.

author
#19 lore raw

Re: [PATCH v7 5/9] libxl IDL: generate code to parse libxl__json_object to libxl_FOO struct

In reply to Ian Campbell
Quoted 10 lines · Ian Campbell
Quoted 8 lines · Wei Liu

libxl_FOO_parse_json functions are generated.

Note that these functions are used to parse libxl__json_object to libxl__FOO struct. They don't consume JSON string.

The new function definitions are generated to new header files called __libxl_types_*_json_internal.h so that they don't contaiminate public header.

Quoted 8 lines

"contaminate".

I'm a bit confused what is going into this file. It seems to be these two:

    f.write("int %s_parse_json(libxl__gc *gc, const libxl__json_object *%s, %s)\n" % (ty.typename,"o",ty.make_arg("p", passby=idl.PASS_BY_REFERENCE)))
    f.write("int %s_from_json(libxl_ctx *ctx, %s, const char *s)\n" % (ty.typename, ty.make_arg("p", passby=idl.PASS_BY_REFERENCE)))

This hunk is in fact generating the implementation. I don't see the need to separate them to different implementation files.

Quoted 6 lines

The first looks internal (it takes const libxl__json_object which is internal) but it is in the libxl_ namespace.

The second looks like it should be public in _libxl_json.h, shouldn't it?

I agree with you that the public functions should to go _libxl_types.h -- it's done like that in this version already. So actually nothing to worry about with regard to header files.

Wei.

#20 lore raw

Re: [PATCH v7 5/9] libxl IDL: generate code to parse libxl__json_object to libxl_FOO struct

Quoted 13 lines · Wei Liu
Quoted 12 lines · Ian Campbell
Quoted 8 lines · Wei Liu

libxl_FOO_parse_json functions are generated.

Note that these functions are used to parse libxl__json_object to libxl__FOO struct. They don't consume JSON string.

The new function definitions are generated to new header files called __libxl_types_*_json_internal.h so that they don't contaiminate public header.

"contaminate".

Quoted 8 lines
Quoted 6 lines

I'm a bit confused what is going into this file. It seems to be these two:

    f.write("int %s_parse_json(libxl__gc *gc, const libxl__json_object *%s, %s)\n" % (ty.typename,"o",ty.make_arg("p", passby=idl.PASS_BY_REFERENCE)))
    f.write("int %s_from_json(libxl_ctx *ctx, %s, const char *s)\n" % (ty.typename, ty.make_arg("p", passby=idl.PASS_BY_REFERENCE)))

This hunk is in fact generating the implementation.

Sorry, somehow I thought this was the prototype.

Quoted 2 lines

I don't see the need
to separate them to different implementation files.

Indeed, there is no reason to do so. My confusion.

Quoted 10 lines
Quoted 6 lines

The first looks internal (it takes const libxl__json_object which is internal) but it is in the libxl_ namespace.

The second looks like it should be public in _libxl_json.h, shouldn't it?

I agree with you that the public functions should to go _libxl_types.h -- it's done like that in this version already. So actually nothing to worry about with regard to header files.

Smashing. I'll have another look at this patch with that in mind once I've dealt with my backlog from being away.

Ian.

author
#21 lore raw

PATCH v7 6/9 libxl/gentest.py: test JSON parser

In reply to Wei Liu

The test is done in following steps:

1. initialise libxl_FOO struct
2. generate JSON string A for libxl_FOO struct FOO1
3. convert JSON string A to libxl_FOO struct FOO2
4. generate JSON string B for libxl_FOO struct FOO2
5. compare A and B

If A and B are identical then we are good.

---
 tools/libxl/gentest.py |   23 ++++++++++++++++++++---
 1 file changed, 20 insertions(+), 3 deletions(-)
tools/libxl/gentest.py +20 -3
index eb9a21b..b92d092 100644
@@ -225,10 +225,11 @@ int main(int argc, char **argv)
 """)
 
     for ty in types:
-        f.write("    %s %s_val;\n" % (ty.typename, ty.typename))
+        f.write("    %s %s_val, %s_val_new;\n" % \
+                (ty.typename, ty.typename, ty.typename))
     f.write("""
     int rc;
-    char *s;
+    char *s, *new_s;
     xentoollog_logger_stdiostream *logger;
     libxl_ctx *ctx;
 
@@ -240,20 +241,36 @@ int main(int argc, char **argv)
         exit(1);
     }
 """)
-    f.write("    printf(\"Testing TYPE_to_json()\\n\");\n")
+    f.write("    printf(\"Testing TYPE_to/from_json()\\n\");\n")
     f.write("    printf(\"----------------------\\n\");\n")
     f.write("    printf(\"\\n\");\n")
     for ty in [t for t in types if t.json_gen_fn is not None]:
         arg = ty.typename + "_val"
         f.write("    %s_rand_init(%s);\n" % (ty.typename, \
             ty.pass_arg(arg, isref=False, passby=idl.PASS_BY_REFERENCE)))
+        if not isinstance(ty, idl.Enumeration):
+            f.write("    %s_init(%s_new);\n" % (ty.typename, \
+                ty.pass_arg(arg, isref=False, passby=idl.PASS_BY_REFERENCE)))
         f.write("    s = %s_to_json(ctx, %s);\n" % \
                 (ty.typename, ty.pass_arg(arg, isref=False)))
         f.write("    printf(\"%%s: %%s\\n\", \"%s\", s);\n" % ty.typename)
         f.write("    if (s == NULL) abort();\n")
+        f.write("    rc = %s_from_json(ctx, &%s_val_new, s);\n" % \
+                (ty.typename, ty.typename))
+        f.write("    if (rc) abort();\n")
+        f.write("    new_s = %s_to_json(ctx, %s_new);\n" % \
+                (ty.typename, ty.pass_arg(arg, isref=False)))
+        f.write("    if (new_s == NULL) abort();\n")
+        f.write("    if (strcmp(s, new_s)) {\n")
+        f.write("        printf(\"Huh? Regenerated string different from original string.\\n\");\n")
+        f.write("        printf(\"Regenerated string: %s\\n\", new_s);\n")
+        f.write("        abort();\n")
+        f.write("    }\n")
         f.write("    free(s);\n")
+        f.write("    free(new_s);\n")
         if ty.dispose_fn is not None:
             f.write("    %s(&%s_val);\n" % (ty.dispose_fn, ty.typename))
+            f.write("    %s(&%s_val_new);\n" % (ty.dispose_fn, ty.typename))
         f.write("\n")
 
     f.write("    printf(\"Testing Enumerations\\n\");\n")
Signature · 1 line
1.7.10.4
author
#22 lore raw

PATCH v7 7/9 libxl: copy function for builtin types

In reply to Wei Liu

These functions will be used in later patch to deep-copy a structure.

Functions introduced:
* libxl_string_list_copy
* libxl_key_value_list_copy
* libxl_hwcap_copy
* libxl_mac_copy
* libxl_cpuid_policy_list_copy
* libxl_string_copy
* libxl_bitmap_copy_alloc

---
 tools/libxl/libxl.c         |   67 +++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl.h         |   12 ++++++++
 tools/libxl/libxl_cpuid.c   |   33 +++++++++++++++++++++
 tools/libxl/libxl_nocpuid.c |    6 ++++
 tools/libxl/libxl_utils.c   |   25 ++++++++++++++++
 tools/libxl/libxl_utils.h   |    4 +++
 6 files changed, 147 insertions(+)
tools/libxl/libxl.c +67 -0
index 1b0b08f..e355e20 100644
@@ -205,6 +205,29 @@ void libxl_string_list_dispose(libxl_string_list *psl)
     free(sl);
 }
 
+void libxl_string_list_copy(libxl_ctx *ctx,
+                            libxl_string_list *dst,
+                            libxl_string_list *src)
+{
+    GC_INIT(ctx);
+    int i, len;
+
+    if (!*src) {
+        *dst = NULL;
+        goto out;
+    }
+
+    len = libxl_string_list_length(src);
+    /* one extra slot for sentinel */
+    *dst = libxl__calloc(NOGC, len + 1, sizeof(char *));
+
+    for (i = 0; i < len; i++)
+        (*dst)[i] = libxl__strdup(NOGC, (*src)[i]);
+
+out:
+    GC_FREE;
+}
+
 int libxl_string_list_length(const libxl_string_list *psl)
 {
     int i = 0;
@@ -245,6 +268,34 @@ void libxl_key_value_list_dispose(libxl_key_value_list *pkvl)
     free(kvl);
 }
 
+void libxl_key_value_list_copy(libxl_ctx *ctx,
+                               libxl_key_value_list *dst,
+                               libxl_key_value_list *src)
+{
+    GC_INIT(ctx);
+    int i, len;
+
+    if (*src == NULL) {
+        *dst = NULL;
+        goto out;
+    }
+
+    len = libxl_key_value_list_length(src);
+    /* one extra slot for sentinel */
+    *dst = libxl__calloc(NOGC, len * 2 + 1, sizeof(char *));
+
+    for (i = 0; i < len * 2; i += 2) {
+        (*dst)[i] = libxl__strdup(NOGC, (*src)[i]);
+        if ((*src)[i+1])
+            (*dst)[i+1] = libxl__strdup(NOGC, (*src)[i+1]);
+        else
+            (*dst)[i+1] = NULL;
+    }
+
+out:
+    GC_FREE;
+}
+
 void libxl_defbool_set(libxl_defbool *db, bool b)
 {
     db->val = b ? LIBXL__DEFBOOL_TRUE : LIBXL__DEFBOOL_FALSE;
@@ -5690,6 +5741,22 @@ int libxl_fd_set_cloexec(libxl_ctx *ctx, int fd, int cloexec)
 int libxl_fd_set_nonblock(libxl_ctx *ctx, int fd, int nonblock)
   { return fd_set_flags(ctx,fd, F_GETFL,F_SETFL,"FL", O_NONBLOCK, nonblock); }
 
+
+void libxl_hwcap_copy(libxl_ctx *ctx,libxl_hwcap *dst, libxl_hwcap *src)
+{
+    int i;
+
+    for (i = 0; i < 8; i++)
+        (*dst)[i] = (*src)[i];
+}
+
+void libxl_mac_copy(libxl_ctx *ctx, libxl_mac *dst, libxl_mac *src)
+{
+    int i;
+
+    for (i = 0; i < 6; i++)
+        (*dst)[i] = (*src)[i];
+}
 /*
  * Local variables:
  * mode: C
tools/libxl/libxl.h +12 -0
index e52a43f..1fb365d 100644
@@ -542,20 +542,29 @@ typedef struct libxl__ctx libxl_ctx;
  */
 #define LIBXL_HAVE_CPUPOOL_NAME 1
 
+typedef struct libxl__ctx libxl_ctx;
+
 typedef uint8_t libxl_mac[6];
 #define LIBXL_MAC_FMT "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx"
 #define LIBXL_MAC_FMTLEN ((2*6)+5) /* 6 hex bytes plus 5 colons */
 #define LIBXL_MAC_BYTES(mac) mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]
+void libxl_mac_copy(libxl_ctx *ctx, libxl_mac *dst, libxl_mac *src);
 
 typedef char **libxl_string_list;
 void libxl_string_list_dispose(libxl_string_list *sl);
 int libxl_string_list_length(const libxl_string_list *sl);
+void libxl_string_list_copy(libxl_ctx *ctx, libxl_string_list *dst,
+                            libxl_string_list *src);
 
 typedef char **libxl_key_value_list;
 void libxl_key_value_list_dispose(libxl_key_value_list *kvl);
 int libxl_key_value_list_length(libxl_key_value_list *kvl);
+void libxl_key_value_list_copy(libxl_ctx *ctx,
+                               libxl_key_value_list *dst,
+                               libxl_key_value_list *src);
 
 typedef uint32_t libxl_hwcap[8];
+void libxl_hwcap_copy(libxl_ctx *ctx, libxl_hwcap *dst, libxl_hwcap *src);
 
 typedef uint64_t libxl_ev_user;
 
@@ -574,6 +583,9 @@ typedef struct libxl__cpuid_policy libxl_cpuid_policy;
 typedef libxl_cpuid_policy * libxl_cpuid_policy_list;
 void libxl_cpuid_dispose(libxl_cpuid_policy_list *cpuid_list);
 int libxl_cpuid_policy_list_length(libxl_cpuid_policy_list *l);
+void libxl_cpuid_policy_list_copy(libxl_ctx *ctx,
+                                  libxl_cpuid_policy_list *dst,
+                                  libxl_cpuid_policy_list *src);
 
 #define LIBXL_PCI_FUNC_ALL (~0U)
 
tools/libxl/libxl_cpuid.c +33 -0
index 1f2bcd5..f3e67ab 100644
@@ -468,6 +468,39 @@ int libxl_cpuid_policy_list_length(libxl_cpuid_policy_list *pl)
     return i;
 }
 
+void libxl_cpuid_policy_list_copy(libxl_ctx *ctx,
+                                  libxl_cpuid_policy_list *dst,
+                                  libxl_cpuid_policy_list *src)
+{
+    GC_INIT(ctx);
+    int i, j, len;
+
+    if (*src == NULL) {
+        *dst = NULL;
+        goto out;
+    }
+
+    len = libxl_cpuid_policy_list_length(src);
+    /* one extra slot for sentinel */
+    *dst = libxl__calloc(NOGC, len + 1, sizeof(libxl_cpuid_policy));
+    (*dst)[len].input[0] = XEN_CPUID_INPUT_UNUSED;
+    (*dst)[len].input[1] = XEN_CPUID_INPUT_UNUSED;
+
+    for (i = 0; i < len; i++) {
+        for (j = 0; j < 2; j++)
+            (*dst)[i].input[j] = (*src)[i].input[j];
+        for (j = 0; j < 4; j++)
+            if ((*src)[i].policy[j])
+                (*dst)[i].policy[j] =
+                    libxl__strdup(NOGC, (*src)[i].policy[j]);
+            else
+                (*dst)[i].policy[j] = NULL;
+    }
+
+out:
+    GC_FREE;
+}
+
 /*
  * Local variables:
  * mode: C
tools/libxl/libxl_nocpuid.c +6 -0
index eb525fc..698d252 100644
@@ -51,6 +51,12 @@ int libxl_cpuid_policy_list_parse_json(libxl__gc *gc,
     return 0;
 }
 
+void libxl_cpuid_policy_list_copy(libxl_ctx *ctx,
+                                  libxl_cpuid_policy_list *dst,
+                                  libxl_cpuid_policy_list *src)
+{
+}
+
 /*
  * Local variables:
  * mode: C
tools/libxl/libxl_utils.c +25 -0
index 16b734e..6053017 100644
@@ -614,6 +614,19 @@ void libxl_bitmap_copy(libxl_ctx *ctx, libxl_bitmap *dptr,
     memcpy(dptr->map, sptr->map, sz * sizeof(*dptr->map));
 }
 
+void libxl_bitmap_copy_alloc(libxl_ctx *ctx,
+                             libxl_bitmap *dptr,
+                             const libxl_bitmap *sptr)
+{
+    GC_INIT(ctx);
+
+    dptr->map = libxl__calloc(NOGC, sptr->size, sizeof(*sptr->map));
+    dptr->size = sptr->size;
+    memcpy(dptr->map, sptr->map, sptr->size * sizeof(*sptr->map));
+
+    GC_FREE;
+}
+
 int libxl_bitmap_is_full(const libxl_bitmap *bitmap)
 {
     int i;
@@ -1013,6 +1026,18 @@ int libxl_domid_valid_guest(uint32_t domid)
     return domid > 0 && domid < DOMID_FIRST_RESERVED;
 }
 
+void libxl_string_copy(libxl_ctx *ctx, char **dst, char **src)
+{
+    GC_INIT(ctx);
+
+    if (*src)
+        *dst = libxl__strdup(NOGC, *src);
+    else
+        *dst = NULL;
+
+    GC_FREE;
+}
+
 /*
  * Local variables:
  * mode: C
tools/libxl/libxl_utils.h +4 -0
index 8bfb81b..cc528d2 100644
@@ -76,6 +76,8 @@ int libxl_devid_to_device_vtpm(libxl_ctx *ctx, uint32_t domid,
 int libxl_bitmap_alloc(libxl_ctx *ctx, libxl_bitmap *bitmap, int n_bits);
     /* Allocated bimap is from malloc, libxl_bitmap_dispose() to be
      * called by the application when done. */
+void libxl_bitmap_copy_alloc(libxl_ctx *ctx, libxl_bitmap *dptr,
+                             const libxl_bitmap *sptr);
 void libxl_bitmap_copy(libxl_ctx *ctx, libxl_bitmap *dptr,
                        const libxl_bitmap *sptr);
 int libxl_bitmap_is_full(const libxl_bitmap *bitmap);
@@ -121,6 +123,8 @@ int libxl_cpumap_to_nodemap(libxl_ctx *ctx,
     return (s + 1023) / 1024;
 }
 
+void libxl_string_copy(libxl_ctx *ctx, char **dst, char **src);
+
 #endif
 
 /*
Signature · 1 line
1.7.10.4
author
#23 lore raw

PATCH v7 8/9 libxl IDL: generate deep copy functions

In reply to Wei Liu

Introduces copy_fn for a type.

For most builtin types we can use direct assignment. For those builtin types which cannot use direct assignment we use the copy functions in previous patch.

---
 tools/libxl/gentypes.py              |   67 ++++++++++++++++++++++++++++++++++
 tools/libxl/idl.py                   |   13 ++++++-
 tools/libxl/idl.txt                  |   12 ++++++
 tools/libxl/libxl_types.idl          |   27 ++++++++------
 tools/libxl/libxl_types_internal.idl |    2 +-
 5 files changed, 108 insertions(+), 13 deletions(-)
tools/libxl/gentypes.py +67 -0
index 605e26e..3690cf3 100644
@@ -100,6 +100,61 @@ def libxl_C_type_dispose(ty, v, indent = "    ", parent = None):
         s = indent + s
     return s.replace("\n", "\n%s" % indent).rstrip(indent)
 
+def libxl_C_type_copy(ty, v, w, indent = "    ", vparent = None, wparent = None):
+    s = ""
+
+    if vparent is None:
+        s += "GC_INIT(ctx);\n";
+
+    if isinstance(ty, idl.KeyedUnion):
+        if vparent is None or wparent is None:
+            raise Exception("KeyedUnion type must have a parent")
+        s += "%s = %s;\n" % ((vparent + ty.keyvar.name), (wparent + ty.keyvar.name))
+        s += "switch (%s) {\n" % (wparent + ty.keyvar.name)
+        for f in ty.fields:
+            (vnparent,vfexpr) = ty.member(v, f, vparent is None)
+            (wnparent,wfexpr) = ty.member(w, f, wparent is None)
+            s += "case %s:\n" % f.enumname
+            if f.type is not None:
+                s += libxl_C_type_copy(f.type, vfexpr, wfexpr, indent + "    ",
+                                       vnparent, wnparent)
+            s += "    break;\n"
+        s += "}\n"
+    elif isinstance(ty, idl.Array):
+        if vparent is None or wparent is None:
+            raise Exception("Array type must have a parent")
+        s += "%s = libxl__calloc(NOGC, %s, sizeof(*%s));\n" % (ty.pass_arg(v, vparent is None),
+                                                               (wparent + ty.lenvar.name),
+                                                               ty.pass_arg(w, wparent is None))
+        s += "%s = %s;\n" % ((vparent + ty.lenvar.name), (wparent + ty.lenvar.name))
+        s += "{\n"
+        s += "    int i;\n"
+        s += "    for (i=0; i<%s; i++)\n" % (wparent + ty.lenvar.name)
+        s += libxl_C_type_copy(ty.elem_type, v+"[i]", w+"[i]",
+                               indent + "        ", vparent, wparent)
+        s += "}\n"
+    elif isinstance(ty, idl.Struct) and ((vparent is None and wparent is None) or ty.copy_fn is None):
+        for f in [f for f in ty.fields if not f.const and not f.type.private]:
+            (vnparent,vfexpr) = ty.member(v, f, vparent is None)
+            (wnparent,wfexpr) = ty.member(w, f, wparent is None)
+            s += libxl_C_type_copy(f.type, vfexpr, wfexpr, "", vnparent, wnparent)
+    else:
+        if ty.copy_fn is not None:
+            s += "%s(ctx, %s, %s);\n" % (ty.copy_fn,
+                                         ty.pass_arg(v, vparent is None, passby=idl.PASS_BY_REFERENCE),
+                                         ty.pass_arg(w, wparent is None, passby=idl.PASS_BY_REFERENCE))
+
+        else:
+            s += "%s = %s;\n" % (ty.pass_arg(v, vparent is None, passby=idl.PASS_BY_VALUE),
+                                 ty.pass_arg(w, wparent is None, passby=idl.PASS_BY_VALUE))
+
+    if vparent is None:
+        s += "GC_FREE;\n"
+
+    if s != "":
+        s = indent + s
+    return s.replace("\n", "\n%s" % indent).rstrip(indent)
+
 def libxl_init_members(ty, nesting = 0):
     """Returns a list of members of ty which require a separate init"""
 
@@ -489,6 +544,9 @@ if __name__ == '__main__':
         f.write(libxl_C_type_define(ty) + ";\n")
         if ty.dispose_fn is not None:
             f.write("%svoid %s(%s);\n" % (ty.hidden(), ty.dispose_fn, ty.make_arg("p")))
+        if ty.copy_fn is not None:
+            f.write("%svoid %s(libxl_ctx *ctx, %s, %s);\n" % (ty.hidden(), ty.copy_fn,
+                                              ty.make_arg("dst"), ty.make_arg("src")))
         if ty.init_fn is not None:
             f.write("%svoid %s(%s);\n" % (ty.hidden(), ty.init_fn, ty.make_arg("p")))
             for field in libxl_init_members(ty):
@@ -589,6 +647,15 @@ if __name__ == '__main__':
         f.write("    memset(p, LIBXL_DTOR_POISON, sizeof(*p));\n")
         f.write("}\n")
         f.write("\n")
+
+    for ty in [t for t in types if t.copy_fn and t.autogenerate_copy_fn]:
+        f.write("void %s(libxl_ctx *ctx, %s, %s)\n" % (ty.copy_fn,
+                                       ty.make_arg("dst", passby=idl.PASS_BY_REFERENCE),
+                                       ty.make_arg("src", passby=idl.PASS_BY_REFERENCE)))
+        f.write("{\n")
+        f.write(libxl_C_type_copy(ty, "dst", "src"))
+        f.write("}\n")
+        f.write("\n")
         
     for ty in [t for t in types if t.init_fn is not None and t.autogenerate_init_fn]:
         f.write(libxl_C_type_init(ty))
tools/libxl/idl.py +12 -1
index 381f11b..56f9e6c 100644
@@ -60,6 +60,13 @@ class Type(object):
 
         self.autogenerate_dispose_fn = kwargs.setdefault('autogenerate_dispose_fn', True)
 
+        if self.typename is not None:
+            self.copy_fn = kwargs.setdefault('copy_fn', self.typename + "_copy")
+        else:
+            self.copy_fn = kwargs.setdefault('copy_fn', None)
+
+        self.autogenerate_copy_fn = kwargs.setdefault('autogenerate_copy_fn', True)
+
         self.init_fn = kwargs.setdefault('init_fn', None)
         self.init_val = kwargs.setdefault('init_val', None)
         self.autogenerate_init_fn = kwargs.setdefault('autogenerate_init_fn', False)
@@ -123,6 +130,7 @@ class Number(Builtin):
     def __init__(self, ctype, **kwargs):
         kwargs.setdefault('namespace', None)
         kwargs.setdefault('dispose_fn', None)
+        kwargs.setdefault('copy_fn', None)
         kwargs.setdefault('signed', False)
         kwargs.setdefault('json_gen_fn', "yajl_gen_integer")
         kwargs.setdefault('json_parse_type', "JSON_INTEGER")
@@ -136,6 +144,7 @@ class UInt(Number):
         kwargs.setdefault('namespace', None)
         kwargs.setdefault('dispose_fn', None)
         kwargs.setdefault('json_parse_fn', "libxl__uint%d_parse_json" % w)
+        kwargs.setdefault('copy_fn', None)
         Number.__init__(self, "uint%d_t" % w, **kwargs)
 
         self.width = w
@@ -152,6 +161,7 @@ class EnumerationValue(object):
 class Enumeration(Type):
     def __init__(self, typename, values, **kwargs):
         kwargs.setdefault('dispose_fn', None)
+        kwargs.setdefault('copy_fn', None)
         kwargs.setdefault('json_parse_type', "JSON_STRING")
         Type.__init__(self, typename, **kwargs)
 
@@ -268,6 +278,7 @@ class KeyedUnion(Aggregate):
 
 void = Builtin("void *", namespace = None)
 bool = Builtin("bool", namespace = None,
+               copy_fn=None,
                json_gen_fn = "yajl_gen_bool",
                json_parse_type = "JSON_BOOL",
                json_parse_fn = "libxl__bool_parse_json",
@@ -282,7 +293,7 @@ uint16 = UInt(16)
 uint32 = UInt(32)
 uint64 = UInt(64, json_gen_fn = "libxl__uint64_gen_json")
 
-string = Builtin("char *", namespace = None, dispose_fn = "free",
+string = Builtin("char *", namespace = None, copy_fn = "libxl_string_copy", dispose_fn = "free",
                  json_gen_fn = "libxl__string_gen_json",
                  json_parse_type = "JSON_STRING | JSON_NULL",
                  json_parse_fn = "libxl__string_parse_json",
tools/libxl/idl.txt +12 -0
index 87c4952..7440fb3 100644
@@ -44,6 +44,18 @@ Type.autogenerate_dispose_fn: (default: True)
  Indicates if the above named Type.dispose_fn should be
  autogenerated.
 
+Type.copy_fn: (default: typename + "_copy" or None if type == None)
+
+ The name of the C function which will deep copy all fields within
+ this type.
+
+Type.autogenerate_copy_fn: (default: True)
+
+ Indicates if the above named Type.copy_fn should be
+ autogenerated.
+
+Type.autogenerate_copy_fn
+
 Type.init_val: (default: None)
 
  C expression for the value to initialise instances of this type to.
tools/libxl/libxl_types.idl +16 -11
index 2cb856c..d40b5c7 100644
@@ -5,26 +5,31 @@
 
 namespace("libxl_")
 
-libxl_defbool = Builtin("defbool", json_parse_type="JSON_STRING", passby=PASS_BY_REFERENCE,
+libxl_defbool = Builtin("defbool", json_parse_type="JSON_STRING", passby=PASS_BY_REFERENCE, copy_fn=None,
                         check_default_fn="libxl__defbool_is_default")
-
 libxl_domid = Builtin("domid", json_gen_fn = "yajl_gen_integer", json_parse_fn = "libxl__uint32_parse_json",
-                      json_parse_type = "JSON_INTEGER", autogenerate_json = False)
+                      json_parse_type = "JSON_INTEGER", autogenerate_json = False, copy_fn=None)
 libxl_devid = Builtin("devid", json_gen_fn = "yajl_gen_integer", json_parse_fn = "libxl__int_parse_json",
-                      json_parse_type = "JSON_INTEGER", autogenerate_json = False, signed = True, init_val="-1")
-libxl_uuid = Builtin("uuid", json_parse_type="JSON_STRING", passby=PASS_BY_REFERENCE, check_default_fn="libxl_uuid_is_nil")
-libxl_mac = Builtin("mac", json_parse_type="JSON_STRING", passby=PASS_BY_REFERENCE, check_default_fn="libxl__mac_is_default")
+                      json_parse_type = "JSON_INTEGER", autogenerate_json = False, signed = True, init_val="-1",
+                      copy_fn=None)
+libxl_uuid = Builtin("uuid", json_parse_type="JSON_STRING", passby=PASS_BY_REFERENCE, check_default_fn="libxl_uuid_is_nil",
+                     copy_fn="libxl_uuid_copy")
+libxl_mac = Builtin("mac", json_parse_type="JSON_STRING", passby=PASS_BY_REFERENCE, check_default_fn="libxl__mac_is_default",
+                    copy_fn="libxl_mac_copy")
 libxl_bitmap = Builtin("bitmap", json_parse_type="JSON_ARRAY", dispose_fn="libxl_bitmap_dispose", passby=PASS_BY_REFERENCE,
-                       check_default_fn="libxl_bitmap_is_empty")
+                       check_default_fn="libxl_bitmap_is_empty", copy_fn="libxl_bitmap_copy_alloc")
 libxl_cpuid_policy_list = Builtin("cpuid_policy_list", dispose_fn="libxl_cpuid_dispose", passby=PASS_BY_REFERENCE,
-                                  json_parse_type="JSON_ARRAY", check_default_fn="libxl__cpuid_policy_is_empty")
+                                  json_parse_type="JSON_ARRAY", check_default_fn="libxl__cpuid_policy_is_empty",
+                                  copy_fn="libxl_cpuid_policy_list_copy")
 
 libxl_string_list = Builtin("string_list", dispose_fn="libxl_string_list_dispose", passby=PASS_BY_REFERENCE,
-                            json_parse_type="JSON_ARRAY", check_default_fn="libxl__string_list_is_empty")
+                            json_parse_type="JSON_ARRAY", check_default_fn="libxl__string_list_is_empty",
+                            copy_fn="libxl_string_list_copy")
 libxl_key_value_list = Builtin("key_value_list", dispose_fn="libxl_key_value_list_dispose", passby=PASS_BY_REFERENCE,
-                               json_parse_type="JSON_MAP", check_default_fn="libxl__key_value_list_is_empty")
+                               json_parse_type="JSON_MAP", check_default_fn="libxl__key_value_list_is_empty",
+                               copy_fn="libxl_key_value_list_copy")
 libxl_hwcap = Builtin("hwcap", passby=PASS_BY_REFERENCE, json_parse_type="JSON_ARRAY",
-                      check_default_fn="libxl__hwcap_is_default")
+                      check_default_fn="libxl__hwcap_is_default", copy_fn="libxl_hwcap_copy")
 
 #
 # Specific integer types
tools/libxl/libxl_types_internal.idl +1 -1
index 17533f1..800361b 100644
@@ -3,7 +3,7 @@ hidden(True)
 
 libxl_domid = Builtin("domid", namespace="libxl_", json_gen_fn = "yajl_gen_integer",
 		      json_parse_fn = "libxl__uint32_parse_json", json_parse_type = "JSON_INTEGER",
-		      autogenerate_json = False)
+		      autogenerate_json = False, copy_fn = None)
 
 libxl__qmp_message_type = Enumeration("qmp_message_type", [
     (1, "QMP"),
Signature · 1 line
1.7.10.4
author
#24 lore raw

PATCH v7 9/9 libxl/gentest.py: test deep copy functions

In reply to Wei Liu

The test is done as followed:
1. initialise libxl_FOO struct A
2. deep-copy A to B
3. generate JSON string for A and B
4. compare two JSON strings

If two strings are identical then we're good.

---
 tools/libxl/gentest.py |   37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
tools/libxl/gentest.py +37 -0
index b92d092..07ffd31 100644
@@ -273,6 +273,43 @@ int main(int argc, char **argv)
             f.write("    %s(&%s_val_new);\n" % (ty.dispose_fn, ty.typename))
         f.write("\n")
 
+    f.write("    printf(\"Testing TYPE_copy()\\n\");\n")
+    f.write("    printf(\"----------------------\\n\");\n")
+    f.write("    printf(\"\\n\");\n")
+    for ty in [t for t in types if t.copy_fn is not None]:
+        f.write("    printf(\"Testing %s_copy, \");\n" % ty.typename)
+        arg = ty.typename + "_val"
+        f.write("    %s_init(%s);\n" % (ty.typename, \
+            ty.pass_arg(arg, isref=False, passby=idl.PASS_BY_REFERENCE)))
+        f.write("    %s_rand_init(%s);\n" % (ty.typename, \
+            ty.pass_arg(arg, isref=False, passby=idl.PASS_BY_REFERENCE)))
+        f.write("    %s_init(%s_new);\n" % (ty.typename, \
+            ty.pass_arg(arg, isref=False, passby=idl.PASS_BY_REFERENCE)))
+        f.write("    %s_copy(ctx, %s_new, %s);\n" % (ty.typename, \
+            ty.pass_arg(arg, isref=False, passby=idl.PASS_BY_REFERENCE), \
+            ty.pass_arg(arg, isref=False, passby=idl.PASS_BY_REFERENCE)))
+        f.write("    s = %s_to_json(ctx, %s);\n" % \
+                (ty.typename, ty.pass_arg(arg, isref=False)))
+        f.write("    if (s == NULL) abort();\n")
+        f.write("    new_s = %s_to_json(ctx, %s_new);\n" % \
+                (ty.typename, ty.pass_arg(arg, isref=False)))
+        f.write("    if (new_s == NULL) abort();\n")
+        f.write("    if (strcmp(s, new_s)) {\n")
+        f.write("        printf(\"Huh? Deep copy for %s failed. Regenerated string different from original string.\\n\");\n" \
+                % ty.typename)
+        f.write("        printf(\"Original string: %s\\n\", s);\n")
+        f.write("        printf(\"Regenerated string: %s\\n\", new_s);\n")
+        f.write("        abort();\n")
+        f.write("    }\n")
+        f.write("    free(s);\n")
+        f.write("    free(new_s);\n")
+        if ty.dispose_fn is not None:
+            f.write("    %s(&%s_val);\n" % (ty.dispose_fn, ty.typename))
+            f.write("    %s(&%s_val_new);\n" % (ty.dispose_fn, ty.typename))
+        f.write("    printf(\"done\\n\");\n")
+        f.write("\n")
+
+    f.write("    printf(\"\\n\");\n")
     f.write("    printf(\"Testing Enumerations\\n\");\n")
     f.write("    printf(\"--------------------\\n\");\n")
     f.write("    printf(\"\\n\");\n")
Signature · 1 line
1.7.10.4
Applied
#25 lore raw
In reply to Wei Liu
Quoted 3 lines · Wei Liu

Wei Liu (9):
A xl / libxl: push parsing of SSID and CPU pool ID down to libxl A libxl: libxl_uuid_copy now takes a ctx argument

Applied.