Skip to contentSkip to conversation

libxl: IDL improvements and JSON generation

PATCH In review Ian Campbell 3 participants 13 messages Jun 23, 2011 – Jun 24, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libxl: IDL improvements and JSON generation

This patch series makes some improvements to the IDL language and generator and then goes on to implement JSON generation for IDL defined datatypes.

#1 lore raw

PATCH 1/1 build: define "move-if-changed" make macro

# HG changeset patch
# User Ian Campbell <[email protected]> # Date 1308843343 -3600
# Node ID edb8e868a964365a3ffe5c7d17467f9f08bf2803 # Parent 4fa92ff6c6156c2723d716a48c041dcb2c92938b
build: define "move-if-changed" make macro.

Use it to replace various places which (should) use the

	if ! cmp -s ...; then mv ....; fi

pattern.

Also add an else clause to cleanup the unchanged temporary file.

Config.mk +11 -1
diff -r 4fa92ff6c615 -r edb8e868a964 Config.mk
@@ -118,6 +118,16 @@ define buildmakevars2shellvars
     export XEN_ROOT="$(XEN_ROOT)"
 endef
 
+#
+# Compare $(1) and $(2) and replace $(2) with $(1) if they differ
+#
+# Typically $(1) is a newly generated file and $(2) is the target file
+# being regenerated. This prevents changing the timestamp of $(2) only
+# due to being auto regenereated with the same contents.
+define move-if-changed
+	if ! cmp -s $(1) $(2); then mv -f $(1) $(2); else rm -f $(1); fi
+endef
+
 buildmakevars2file = $(eval $(call buildmakevars2file-closure,$(1)))
 define buildmakevars2file-closure
     .PHONY: genpath
@@ -133,7 +143,7 @@ define buildmakevars2file-closure
 	echo "XEN_CONFIG_DIR=\"$(XEN_CONFIG_DIR)\"" >> $(1).tmp;           \
 	echo "XEN_SCRIPT_DIR=\"$(XEN_SCRIPT_DIR)\"" >> $(1).tmp;           \
 	echo "XEN_LOCK_DIR=\"$(XEN_LOCK_DIR)\"" >> $(1).tmp;               \
-	if ! cmp $(1).tmp $(1); then mv -f $(1).tmp $(1); fi
+	$(call move-if-changed,$(1).tmp,$(1))
 endef
 
 ifeq ($(debug),y)
tools/libxl/Makefile +4 -3
diff -r 4fa92ff6c615 -r edb8e868a964 tools/libxl/Makefile
@@ -74,7 +74,8 @@ genpath-target = $(call buildmakevars2fi
 
 _libxl_paths.h: genpath
 	sed -e "s/\([^=]*\)=\(.*\)/#define \1 \2/g" [email protected] >[email protected]
-	if ! cmp -s [email protected] $@; then mv -f [email protected] $@; fi
+	rm -f [email protected]
+	$(call move-if-changed,[email protected],$@)
 
 libxl_paths.c: _libxl_paths.h
 
@@ -84,8 +85,8 @@ libxl.h: _libxl_types.h
 
 _libxl_%.h _libxl_%.c: libxl.idl gen%.py libxl%.py
 	$(PYTHON) gen$*.py libxl.idl __libxl_$*.h __libxl_$*.c
-	mv __libxl_$*.h _libxl_$*.h
-	mv __libxl_$*.c _libxl_$*.c
+	$(call move-if-changed,__libxl_$*.h,_libxl_$*.h)
+	$(call move-if-changed,__libxl_$*.c,_libxl_$*.c)
 
 libxenlight.so: libxenlight.so.$(MAJOR)
 	ln -sf $< $@
#2 lore raw

PATCH 1/1 libxl: IDL: s/anonynous/anonymous/

In reply to Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]> # Date 1308845904 -3600
# Node ID ba383ce75f8bba3e08a84c660c8383c26d23aaa8 # Parent edb8e868a964365a3ffe5c7d17467f9f08bf2803
libxl: IDL: s/anonynous/anonymous/

tools/libxl/gentypes.py +1 -1
diff -r edb8e868a964 -r ba383ce75f8b tools/libxl/gentypes.py
@@ -101,7 +101,7 @@ def libxl_C_type_destroy(ty, v, indent =
     elif isinstance(ty, libxltypes.Struct) and (parent is None or ty.destructor_fn is None):
         for f in [f for f in ty.fields if not f.const]:
 
-            if f.name is None: # Anonynous struct
+            if f.name is None: # Anonymous struct
                 s += libxl_C_type_destroy(f.type, deref, "", deref)
             else:
                 s += libxl_C_type_destroy(f.type, deref + f.name, "", deref)
#3 lore raw

PATCH 1/1 libxl: IDL: handle generation of pass-by-reference arguments

In reply to Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1308845917 -3600
# Node ID 4186a6ca6799797e24fafa5fe39e55b474b86bf0 # Parent ba383ce75f8bba3e08a84c660c8383c26d23aaa8
libxl: IDL: handle generation of pass-by-reference arguments.

Up until now everything with a destructor function happened to be pass-by-reference so the current code worked but this will not be the case for *_to_string and *_to_json. Put some infrastructure in place and use it.

tools/libxl/gentypes.py +4 -4
diff -r ba383ce75f8b -r 4186a6ca6799 tools/libxl/gentypes.py
@@ -178,10 +178,10 @@ if __name__ == '__main__':
     for ty in types:
         f.write(libxl_C_type_define(ty) + ";\n")
         if ty.destructor_fn is not None:
-            f.write("void %s(%s *p);\n" % (ty.destructor_fn, ty.typename))
+            f.write("void %s(%s);\n" % (ty.destructor_fn, ty.make_arg("p")))
         if isinstance(ty, libxltypes.Enumeration):
-            f.write("const char *%s_to_string(%s e);\n" % (ty.typename, ty.typename))
-            f.write("int %s_from_string(const char *s, %s *e);\n" % (ty.typename, ty.typename))
+            f.write("const char *%s_to_string(%s);\n" % (ty.typename, ty.make_arg("p")))
+            f.write("int %s_from_string(const char *s, %s);\n" % (ty.typename, ty.make_arg("e", passby=libxltypes.PASS_BY_REFERENCE)))
             f.write("extern libxl_enum_string_table %s_string_table[];\n" % (ty.typename))
         f.write("\n")
 
@@ -213,7 +213,7 @@ if __name__ == '__main__':
 """ % " ".join(sys.argv))
 
     for ty in [t for t in types if t.destructor_fn is not None and t.autogenerate_destructor]:
-        f.write("void %s(%s *p)\n" % (ty.destructor_fn, ty.typename))
+        f.write("void %s(%s)\n" % (ty.destructor_fn, ty.make_arg("p")))
         f.write("{\n")
         f.write(libxl_C_type_destroy(ty, "p"))
         f.write("    memset(p, LIBXL_DTOR_POISON, sizeof(*p));\n")
tools/libxl/libxl.idl +2 -2
diff -r ba383ce75f8b -r 4186a6ca6799 tools/libxl/libxl.idl
@@ -4,8 +4,8 @@
 #
 
 libxl_domid = Builtin("domid")
-libxl_uuid = Builtin("uuid")
-libxl_mac = Builtin("mac")
+libxl_uuid = Builtin("uuid", passby=PASS_BY_REFERENCE)
+libxl_mac = Builtin("mac", passby=PASS_BY_REFERENCE)
 libxl_cpumap = Builtin("cpumap", destructor_fn="libxl_cpumap_destroy", passby=PASS_BY_REFERENCE)
 libxl_cpuarray = Builtin("cpuarray", destructor_fn="libxl_cpuarray_destroy", passby=PASS_BY_REFERENCE)
 libxl_cpuid_policy_list = Builtin("cpuid_policy_list", destructor_fn="libxl_cpuid_destroy", passby=PASS_BY_REFERENCE)
tools/libxl/libxltypes.py +8 -0
diff -r ba383ce75f8b -r 4186a6ca6799 tools/libxl/libxltypes.py
@@ -42,6 +42,14 @@ class Type(object):
     def marshal_out(self):
         return self.dir in [DIR_OUT, DIR_BOTH]
 
+    def make_arg(self, n, passby=None):
+        if passby is None: passby = self.passby
+        
+        if passby == PASS_BY_REFERENCE:
+            return "%s *%s" % (self.typename, n)
+        else:
+            return "%s %s" % (self.typename, n)
+        
 class Builtin(Type):
     """Builtin type"""
     def __init__(self, typename, **kwargs):
#4 lore raw

PATCH 1/1 libxl: IDL: refactor code to massage a type into a function argument

In reply to Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1308845927 -3600
# Node ID 34279f2359cbaaef7c16287e023cae1b023f02d3
# Parent 4186a6ca6799797e24fafa5fe39e55b474b86bf0
libxl: IDL: refactor code to massage a type into a function argument

tools/libxl/gentypes.py +1 -6
diff -r 4186a6ca6799 -r 34279f2359cb tools/libxl/gentypes.py
@@ -84,11 +84,6 @@ def libxl_C_type_destroy(ty, v, indent =
     else:
         deref = v + "."
         
-    if ty.passby == libxltypes.PASS_BY_REFERENCE and parent is not None:
-        makeref = "&"
-    else:
-        makeref = ""
-
     s = ""
     if isinstance(ty, libxltypes.KeyedUnion):
         if parent is None:
@@ -107,7 +102,7 @@ def libxl_C_type_destroy(ty, v, indent =
                 s += libxl_C_type_destroy(f.type, deref + f.name, "", deref)
     else:
         if ty.destructor_fn is not None:
-            s += "%s(%s);\n" % (ty.destructor_fn, makeref + v)
+            s += "%s(%s);\n" % (ty.destructor_fn, ty.pass_arg(v, parent is None))
             
     if s != "":
         s = indent + s
tools/libxl/libxltypes.py +15 -0
diff -r 4186a6ca6799 -r 34279f2359cb tools/libxl/libxltypes.py
@@ -50,6 +50,21 @@ class Type(object):
         else:
             return "%s %s" % (self.typename, n)
         
+    def pass_arg(self, n, isref=None, passby=None):
+        if passby is None: passby = self.passby
+        if isref is None: isref = self.passby == PASS_BY_REFERENCE
+
+        if passby == PASS_BY_REFERENCE:
+            if isref:
+                return "%s" % (n)
+            else:
+                return "&%s" % (n)
+        else:
+            if isref:
+                return "*%s" % (n)
+            else:
+                return "%s" % (n)
+
 class Builtin(Type):
     """Builtin type"""
     def __init__(self, typename, **kwargs):
#5 lore raw

PATCH 1/1 libxl: IDL: remove libxl_C_type_of

In reply to Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]> # Date 1308845928 -3600
# Node ID 2b91546fb417458d3fa29da85aed44bddb91854f # Parent 34279f2359cbaaef7c16287e023cae1b023f02d3
libxl: IDL: remove libxl_C_type_of

It's not really adding much...

tools/libxl/gentypes.py +2 -5
diff -r 34279f2359cb -r 2b91546fb417 tools/libxl/gentypes.py
@@ -17,10 +17,7 @@ def format_comment(level, comment):
     s += "%s */" % indent
     s += "\n"
     return s
-
-def libxl_C_type_of(ty):
-    return ty.typename
-
+    
 def libxl_C_instance_of(ty, instancename):
     if isinstance(ty, libxltypes.Aggregate) and ty.typename is None:
         if instancename is None:
@@ -28,7 +25,7 @@ def libxl_C_instance_of(ty, instancename
         else:
             return libxl_C_type_define(ty) + " " + instancename
     else:
-        return libxl_C_type_of(ty) + " " + instancename
+        return ty.typename + " " + instancename
 
 def libxl_C_type_define(ty, indent = ""):
     s = ""
#6 lore raw

PATCH 1/1 libxl: IDL: add helper to generate references to Aggregate type members

In reply to Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1308845933 -3600
# Node ID 273be439b67afbeefdf00e68efb5b09412a25c82
# Parent 2b91546fb417458d3fa29da85aed44bddb91854f
libxl: IDL: add helper to generate references to Aggregate type members.

tools/libxl/gentypes.py +4 -10
diff -r 2b91546fb417 -r 273be439b67a tools/libxl/gentypes.py
@@ -76,27 +76,21 @@ def libxl_C_type_define(ty, indent = "")
     return s.replace("\n", "\n%s" % indent)
 
 def libxl_C_type_destroy(ty, v, indent = "    ", parent = None):
-    if parent is None:
-        deref = v + "->"
-    else:
-        deref = v + "."
         
     s = ""
     if isinstance(ty, libxltypes.KeyedUnion):
         if parent is None:
             raise Exception("KeyedUnion type must have a parent")
         for f in ty.fields:
+            (nparent,fexpr) = ty.member(v, f, parent is None)
             keyvar_expr = f.keyvar_expr % (parent + ty.keyvar_name)
             s += "if (" + keyvar_expr + ") {\n"
-            s += libxl_C_type_destroy(f.type, deref + f.name, indent + "    ", deref)
+            s += libxl_C_type_destroy(f.type, fexpr, indent + "    ", nparent)
             s += "}\n"
     elif isinstance(ty, libxltypes.Struct) and (parent is None or ty.destructor_fn is None):
         for f in [f for f in ty.fields if not f.const]:
-
-            if f.name is None: # Anonymous struct
-                s += libxl_C_type_destroy(f.type, deref, "", deref)
-            else:
-                s += libxl_C_type_destroy(f.type, deref + f.name, "", deref)
+            (nparent,fexpr) = ty.member(v, f, parent is None)
+            s += libxl_C_type_destroy(f.type, fexpr, "", nparent)
     else:
         if ty.destructor_fn is not None:
             s += "%s(%s);\n" % (ty.destructor_fn, ty.pass_arg(v, parent is None))
tools/libxl/libxltypes.py +18 -0
diff -r 2b91546fb417 -r 273be439b67a tools/libxl/libxltypes.py
@@ -147,6 +147,24 @@ class Aggregate(Type):
                 n,t,const,comment = f
             self.fields.append(Field(t,n,const=const,comment=comment))
 
+    # Returns a tuple (stem, field-expr)
+    #
+    # field-expr is a C expression for a field "f" within the struct
+    # "v".
+    #
+    # stem is the stem common to both "f" and any other sibbling field
+    # within the "v".
+    def member(self, v, f, isref):
+        if isref:
+            deref = v + "->"
+        else:
+            deref = v + "."
+        
+        if f.name is None: # Anonymous
+            return (deref, deref)
+        else:
+            return (deref, deref + f.name)
+
 class Struct(Aggregate):
     def __init__(self, name, fields, **kwargs):
         kwargs.setdefault('passby', PASS_BY_REFERENCE)
#7 lore raw

PATCH 1/1 libxl: IDL: bring command line handling in genwrap.py into one place

In reply to Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1308845948 -3600
# Node ID 956eef6b25e72a6fb76c43813c4367c29a9860cb
# Parent 273be439b67afbeefdf00e68efb5b09412a25c82
libxl: IDL: bring command line handling in genwrap.py into one place.

tools/libxl/gentypes.py +3 -4
diff -r 273be439b67a -r 956eef6b25e7 tools/libxl/gentypes.py
@@ -137,14 +137,14 @@ def libxl_C_enum_from_string(ty, str, e,
     
 
 if __name__ == '__main__':
-    if len(sys.argv) < 4:
+    if len(sys.argv) != 4:
         print >>sys.stderr, "Usage: gentypes.py <idl> <header> <implementation>"
         sys.exit(1)
 
-    idl = sys.argv[1]
+    (_, idl, header, impl) = sys.argv
+
     (_,types) = libxltypes.parse(idl)
                     
-    header = sys.argv[2]
     print "outputting libxl type definitions to %s" % header
 
     f = open(header, "w")
@@ -174,7 +174,6 @@ if __name__ == '__main__':
     f.write("""#endif /* __LIBXL_TYPES_H */\n""")
     f.close()
     
-    impl = sys.argv[3]
     print "outputting libxl type implementations to %s" % impl
 
     f = open(impl, "w")
#8 lore raw

PATCH 1/1 libxl: rename testenum->testidl

In reply to Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]> # Date 1308845949 -3600
# Node ID dea9208b98db2b1b301a17b13d21558fa0a0edc7 # Parent 956eef6b25e72a6fb76c43813c4367c29a9860cb
libxl: rename testenum->testidl

I plan to add some non-Enumeration tests.

.hgignore +2 -2
diff -r 956eef6b25e7 -r dea9208b98db .hgignore
@@ -187,8 +187,8 @@
 ^tools/libxl/_.*\.c$
 ^tools/libxl/libxlu_cfg_y\.output$
 ^tools/libxl/xl$
-^tools/libxl/testenum$
-^tools/libxl/testenum\.c$
+^tools/libxl/testidl$
+^tools/libxl/testidl\.c$
 ^tools/libaio/src/.*\.ol$
 ^tools/libaio/src/.*\.os$
 ^tools/misc/cpuperf/cpuperf-perfcntr$
tools/libxl/Makefile +7 -7
diff -r 956eef6b25e7 -r dea9208b98db tools/libxl/Makefile
@@ -44,16 +44,16 @@ AUTOSRCS= libxlu_cfg_y.c libxlu_cfg_l.c
 LIBXLU_OBJS = libxlu_cfg_y.o libxlu_cfg_l.o libxlu_cfg.o
 $(LIBXLU_OBJS): CFLAGS += $(CFLAGS_libxenctrl) # For xentoollog.h
 
-CLIENTS = xl testenum
+CLIENTS = xl testidl
 
 XL_OBJS = xl.o xl_cmdimpl.o xl_cmdtable.o
 $(XL_OBJS): CFLAGS += $(CFLAGS_libxenctrl) # For xentoollog.h
 $(XL_OBJS): CFLAGS += $(CFLAGS_libxenlight)
 
-testenum.o: CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_libxenlight)
-testenum.c: libxl.idl gentest.py
-	$(PYTHON) gentest.py libxl.idl testenum.c.new
-	mv testenum.c.new testenum.c
+testidl.o: CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_libxenlight)
+testidl.c: libxl.idl gentest.py
+	$(PYTHON) gentest.py libxl.idl testidl.c.new
+	mv testidl.c.new testidl.c
 
 .PHONY: all
 all: $(CLIENTS) libxenlight.so libxenlight.a libxlutil.so libxlutil.a \
@@ -115,8 +115,8 @@ libxlutil.a: $(LIBXLU_OBJS)
 xl: $(XL_OBJS) libxlutil.so libxenlight.so
 	$(CC) $(LDFLAGS) -o $@ $(XL_OBJS) libxlutil.so $(LDLIBS_libxenlight) $(LDLIBS_libxenctrl)
 
-testenum: testenum.o libxlutil.so libxenlight.so
-	$(CC) $(LDFLAGS) -o $@ testenum.o libxlutil.so $(LDLIBS_libxenlight) $(LDLIBS_libxenctrl)
+testidl: testidl.o libxlutil.so libxenlight.so
+	$(CC) $(LDFLAGS) -o $@ testidl.o libxlutil.so $(LDLIBS_libxenlight) $(LDLIBS_libxenctrl)
 
 .PHONY: install
 install: all
#9 lore raw

PATCH 1/1 libxl: add LIBXL_MAC_{FMT, FMTLEN, BYTES}

In reply to Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]> # Date 1308845956 -3600
# Node ID 4e751a05df3c323aea8fb291961a21d13de4afda # Parent dea9208b98db2b1b301a17b13d21558fa0a0edc7
libxl: add LIBXL_MAC_{FMT,FMTLEN,BYTES}

Modelled after LIBXL_UUID_... (where I also add FMTLEN).

tools/libxl/libxl.c +4 -6
diff -r dea9208b98db -r 4e751a05df3c tools/libxl/libxl.c
@@ -1284,9 +1284,8 @@ int libxl_device_nic_add(libxl_ctx *ctx,
                                           nic->script));
     }
     flexarray_append(back, "mac");
-    flexarray_append(back, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
-                                                 nic->mac[0], nic->mac[1], nic->mac[2],
-                                                 nic->mac[3], nic->mac[4], nic->mac[5]));
+    flexarray_append(back,libxl__sprintf(&gc,
+                                    LIBXL_MAC_FMT, LIBXL_MAC_BYTES(nic->mac)));
     if (nic->ip) {
         flexarray_append(back, "ip");
         flexarray_append(back, libxl__strdup(&gc, nic->ip));
@@ -1304,9 +1303,8 @@ int libxl_device_nic_add(libxl_ctx *ctx,
     flexarray_append(front, "handle");
     flexarray_append(front, libxl__sprintf(&gc, "%d", nic->devid));
     flexarray_append(front, "mac");
-    flexarray_append(front, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
-                                                  nic->mac[0], nic->mac[1], nic->mac[2],
-                                                  nic->mac[3], nic->mac[4], nic->mac[5]));
+    flexarray_append(front, libxl__sprintf(&gc,
+                                    LIBXL_MAC_FMT, LIBXL_MAC_BYTES(nic->mac)));
     libxl__device_generic_add(&gc, &device,
                              libxl__xs_kvs_of_flexarray(&gc, back, back->count),
                              libxl__xs_kvs_of_flexarray(&gc, front, front->count));
tools/libxl/libxl.h +3 -0
diff -r dea9208b98db -r 4e751a05df3c tools/libxl/libxl.h
@@ -139,6 +139,9 @@
 #include <libxl_uuid.h>
 
 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]
 
 typedef char **libxl_string_list;
 void libxl_string_list_destroy(libxl_string_list *sl);
tools/libxl/libxl_dm.c +4 -6
diff -r dea9208b98db -r 4e751a05df3c tools/libxl/libxl_dm.c
@@ -177,9 +177,8 @@ static char ** libxl__build_device_model
         }
         for (i = 0; i < num_vifs; i++) {
             if (vifs[i].nictype == LIBXL_NIC_TYPE_IOEMU) {
-                char *smac = libxl__sprintf(gc, "%02x:%02x:%02x:%02x:%02x:%02x",
-                                           vifs[i].mac[0], vifs[i].mac[1], vifs[i].mac[2],
-                                           vifs[i].mac[3], vifs[i].mac[4], vifs[i].mac[5]);
+                char *smac = libxl__sprintf(gc,
+                                   LIBXL_MAC_FMT, LIBXL_MAC_BYTES(vifs[i].mac));
                 char *ifname;
                 if (!vifs[i].ifname)
                     ifname = libxl__sprintf(gc, "tap%d.%d", info->domid, vifs[i].devid);
@@ -368,9 +367,8 @@ static char ** libxl__build_device_model
         }
         for (i = 0; i < num_vifs; i++) {
             if (vifs[i].nictype == LIBXL_NIC_TYPE_IOEMU) {
-                char *smac = libxl__sprintf(gc, "%02x:%02x:%02x:%02x:%02x:%02x",
-                                           vifs[i].mac[0], vifs[i].mac[1], vifs[i].mac[2],
-                                           vifs[i].mac[3], vifs[i].mac[4], vifs[i].mac[5]);
+                char *smac = libxl__sprintf(gc,
+                                LIBXL_MAC_FMT, LIBXL_MAC_BYTES(vifs[i].mac));
                 char *ifname;
                 if (!vifs[i].ifname) {
                     ifname = libxl__sprintf(gc, "tap%d.%d", info->domid, vifs[i].devid);
tools/libxl/libxl_uuid.h +1 -0
diff -r dea9208b98db -r 4e751a05df3c tools/libxl/libxl_uuid.h
@@ -16,6 +16,7 @@
 #define __LIBXL_UUID_H__
 
 #define LIBXL_UUID_FMT "%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
+#define LIBXL_UUID_FMTLEN ((2*16)+4) /* 16 hex bytes plus 4 hypens */
 #define LIBXL__UUID_BYTES(uuid) uuid[0], uuid[1], uuid[2], uuid[3], \
                                 uuid[4], uuid[5], uuid[6], uuid[7], \
                                 uuid[8], uuid[9], uuid[10], uuid[11], \
tools/libxl/xl_cmdimpl.c +1 -3
diff -r dea9208b98db -r 4e751a05df3c tools/libxl/xl_cmdimpl.c
@@ -4094,9 +4094,7 @@ int main_networklist(int argc, char **ar
             /* Idx BE */
             printf("%-3d %-2d ", nics[i].devid, nics[i].backend_id);
             /* MAC */
-            printf("%02x:%02x:%02x:%02x:%02x:%02x ",
-                   nics[i].mac[0], nics[i].mac[1], nics[i].mac[2],
-                   nics[i].mac[3], nics[i].mac[4], nics[i].mac[5]);
+            printf(LIBXL_MAC_FMT, LIBXL_MAC_BYTES(nics[i].mac));
             /* Hdl  Sta  evch txr/rxr  BE-path */
             printf("%6d %5d %6d %5d/%-11d %-30s\n",
                    nics[i].devid, nics[i].state, nics[i].evtch,
#10 lore raw

PATCH 1/1 libxl: IDL: autogenerate functions to produce JSON from libxl data structures

In reply to Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1308845967 -3600
# Node ID f004439ac18259226136d68d533407661dd55232
# Parent 4e751a05df3c323aea8fb291961a21d13de4afda
libxl: IDL: autogenerate functions to produce JSON from libxl data structures.

Two functions are provided. TYPE_gen_json exposes an interface which is compatible with the YAGL generator infrastructure. TYPE_to_string uses this to produce a pretty printed string.

The TYPE_gen_json functions are defined in a new header libxl_json.h which is not exposed via libxl.h due to the use of YAGL datatypes to avoid poluting the namespace us libxl users which don't use the library themselves. If a libxl user is interested in integrating at the YAGL level then it should #include this file itself.

Also update testidl to generate a random version of each IDL datastructure and convert it to JSON. Unfortunately this requires a libxl_ctx and therefore the test must be run on a Xen system now.

tools/libxl/Makefile +10 -4
diff -r 4e751a05df3c -r f004439ac182 tools/libxl/Makefile
@@ -32,9 +32,12 @@ endif
 LIBXL_OBJS-$(CONFIG_X86) += libxl_cpuid.o
 LIBXL_OBJS-$(CONFIG_IA64) += libxl_nocpuid.o
 
+LIBXL_LIBS += -lyajl
+
 LIBXL_OBJS = flexarray.o libxl.o libxl_create.o libxl_dm.o libxl_pci.o \
 			libxl_dom.o libxl_exec.o libxl_xshelp.o libxl_device.o \
-			libxl_internal.o libxl_utils.o libxl_uuid.o $(LIBXL_OBJS-y)
+			libxl_internal.o libxl_utils.o libxl_uuid.o libxl_json.o \
+			$(LIBXL_OBJS-y)
 LIBXL_OBJS += _libxl_types.o libxl_flask.o
 
 $(LIBXL_OBJS): CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_libxenguest) $(CFLAGS_libxenstore) $(CFLAGS_libblktapctl)
@@ -81,11 +84,14 @@ libxl_paths.c: _libxl_paths.h
 
 libxl.h: _libxl_types.h
 
+libxl_json.h: _libxl_types_json.h
+
 $(LIBXL_OBJS) $(LIBXLU_OBJS) $(XL_OBJS): libxl.h
 
-_libxl_%.h _libxl_%.c: libxl.idl gen%.py libxl%.py
-	$(PYTHON) gen$*.py libxl.idl __libxl_$*.h __libxl_$*.c
+_libxl_%.h _libxl_%_json.h _libxl_%.c: libxl.idl gen%.py libxltypes.py
+	$(PYTHON) gen$*.py libxl.idl __libxl_$*.h __libxl_$*_json.h __libxl_$*.c
 	$(call move-if-changed,__libxl_$*.h,_libxl_$*.h)
+	$(call move-if-changed,__libxl_$*_json.h,_libxl_$*_json.h)
 	$(call move-if-changed,__libxl_$*.c,_libxl_$*.c)
 
 libxenlight.so: libxenlight.so.$(MAJOR)
@@ -133,7 +139,7 @@ install: all
 	ln -sf libxlutil.so.$(XLUMAJOR).$(XLUMINOR) $(DESTDIR)$(LIBDIR)/libxlutil.so.$(XLUMAJOR)
 	ln -sf libxlutil.so.$(XLUMAJOR) $(DESTDIR)$(LIBDIR)/libxlutil.so
 	$(INSTALL_DATA) libxlutil.a $(DESTDIR)$(LIBDIR)
-	$(INSTALL_DATA) libxl.h _libxl_types.h libxl_utils.h libxl_uuid.h $(DESTDIR)$(INCLUDEDIR)
+	$(INSTALL_DATA) libxl.h libxl_json.h _libxl_types.h _libxl_types_json.h libxl_utils.h libxl_uuid.h $(DESTDIR)$(INCLUDEDIR)
 	$(INSTALL_DATA) bash-completion $(DESTDIR)$(BASH_COMPLETION_DIR)/xl.sh
 
 .PHONY: clean
tools/libxl/gentest.py +229 -6

276 lines not on this page · raw message

tools/libxl/gentypes.py +99 -5

147 lines not on this page · raw message

tools/libxl/idl.txt +9 -0
diff -r 4e751a05df3c -r f004439ac182 tools/libxl/idl.txt
@@ -49,6 +49,15 @@ Type.autogenerate_destructor: (default: 
  Indicates if the above named Type.destructor_fn should be
  autogenerated.
 
+Type.json_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.autogenerate_json: (default: True)
+
+ Indicates if the above named Type.json_fn should be autogenerated.
+
 Other simple type-Classes
 -------------------------
 
tools/libxl/libxl.h +2 -2
diff -r 4e751a05df3c -r f004439ac182 tools/libxl/libxl.h
@@ -199,10 +199,10 @@ typedef struct {
     int v;
 } libxl_enum_string_table;
 
+typedef struct libxl__ctx libxl_ctx;
+
 #include "_libxl_types.h"
 
-typedef struct libxl__ctx libxl_ctx;
-
 const libxl_version_info* libxl_get_version_info(libxl_ctx *ctx);
 
 typedef struct {
tools/libxl/libxl.idl +2 -1
diff -r 4e751a05df3c -r f004439ac182 tools/libxl/libxl.idl
@@ -3,7 +3,8 @@
 # Builtin libxl types
 #
 
-libxl_domid = Builtin("domid")
+libxl_domid = Builtin("domid", json_fn = "yajl_gen_integer", autogenerate_json = False)
+
 libxl_uuid = Builtin("uuid", passby=PASS_BY_REFERENCE)
 libxl_mac = Builtin("mac", passby=PASS_BY_REFERENCE)
 libxl_cpumap = Builtin("cpumap", destructor_fn="libxl_cpumap_destroy", passby=PASS_BY_REFERENCE)
tools/libxl/libxl_internal.h +7 -0
diff -r 4e751a05df3c -r f004439ac182 tools/libxl/libxl_internal.h
@@ -35,6 +35,7 @@
 
 #include "flexarray.h"
 #include "libxl_utils.h"
+#include "libxl_json.h"
 
 #define LIBXL_DESTROY_TIMEOUT 10
 #define LIBXL_DEVICE_MODEL_START_TIMEOUT 10
@@ -328,6 +329,12 @@ _hidden char *libxl__cpupoolid_to_name(l
 _hidden int libxl__enum_from_string(const libxl_enum_string_table *t,
                                     const char *s, int *e);
 
+_hidden yajl_gen_status libxl__string_gen_json(yajl_gen hand, const char *p);
+
+typedef yajl_gen_status (*libxl__gen_json_callback)(yajl_gen hand, void *);
+_hidden char *libxl__object_to_json(libxl_ctx *ctx, const char *type,
+                                    libxl__gen_json_callback gen, void *p);
+
   /* holds the CPUID response for a single CPUID leaf
    * input contains the value of the EAX and ECX register,
    * and each policy string contains a filter to apply to
tools/libxl/libxl_json.c new file +287 -0

287 lines not on this page · raw message

tools/libxl/libxl_json.h new file +41 -0

41 lines not on this page · raw message

tools/libxl/libxltypes.py +18 -2
diff -r 4e751a05df3c -r f004439ac182 tools/libxl/libxltypes.py
@@ -37,6 +37,13 @@ class Type(object):
 
         self.autogenerate_destructor = kwargs.setdefault('autogenerate_destructor', True)
 
+        if self.typename is not None:
+            self.json_fn = kwargs.setdefault('json_fn', self.typename + "_gen_json")
+        else:
+            self.json_fn = kwargs.setdefault('json_fn', None)
+
+        self.autogenerate_json = kwargs.setdefault('autogenerate_json', True)
+
     def marshal_in(self):
         return self.dir in [DIR_IN, DIR_BOTH]
     def marshal_out(self):
@@ -70,6 +77,7 @@ class Builtin(Type):
     def __init__(self, typename, **kwargs):
         kwargs.setdefault('destructor_fn', None)
         kwargs.setdefault('autogenerate_destructor', False)
+        kwargs.setdefault('autogenerate_json', False)
         Type.__init__(self, typename, **kwargs)
 
 class Number(Builtin):
@@ -77,6 +85,7 @@ class Number(Builtin):
         kwargs.setdefault('namespace', None)
         kwargs.setdefault('destructor_fn', None)
         kwargs.setdefault('signed', False)
+        kwargs.setdefault('json_fn', "yajl_gen_integer")
         self.signed = kwargs['signed']
         Builtin.__init__(self, ctype, **kwargs)
 
@@ -145,6 +154,8 @@ class Aggregate(Type):
                 comment = None
             else:
                 n,t,const,comment = f
+            if n is None:
+                raise ValueError
             self.fields.append(Field(t,n,const=const,comment=comment))
 
     # Returns a tuple (stem, field-expr)
@@ -199,7 +210,10 @@ class KeyedUnion(Aggregate):
 #
 
 void = Builtin("void *", namespace = None)
-bool = Builtin("bool", namespace = None)
+bool = Builtin("bool", namespace = None,
+               json_fn = "yajl_gen_bool",
+               autogenerate_json = False)
+
 size_t = Number("size_t", namespace = None)
 
 integer = Number("int", namespace = None, signed = True)
@@ -209,7 +223,9 @@ uint16 = UInt(16)
 uint32 = UInt(32)
 uint64 = UInt(64)
 
-string = Builtin("char *", namespace = None, destructor_fn = "free")
+string = Builtin("char *", namespace = None, destructor_fn = "free",
+                 json_fn = "libxl__string_gen_json",
+                 autogenerate_json = False)
 
 class OrderedDict(dict):
     """A dictionary which remembers insertion order.
#11 lore raw

Re: [PATCH 10 of 10] libxl: IDL: autogenerate functions to produce JSON from libxl data structures

Minor update (missing "static") because I forgot to qrefresh before sending.

I also forgot to mention that this uses the same library (YAJL) as Anthony's QMP patch, I made the Makefile bit look the same to keep any conflict reslution minimal.

# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1308846102 -3600
# Node ID fc8319c378641194e028a9ae3353b95b1c05539a
# Parent 4e751a05df3c323aea8fb291961a21d13de4afda
libxl: IDL: autogenerate functions to produce JSON from libxl data structures.

Two functions are provided. TYPE_gen_json exposes an interface which is compatible with the YAGL generator infrastructure. TYPE_to_string uses this to produce a pretty printed string.

The TYPE_gen_json functions are defined in a new header libxl_json.h which is not exposed via libxl.h due to the use of YAGL datatypes to avoid poluting the namespace us libxl users which don't use the library themselves. If a libxl user is interested in integrating at the YAGL level then it should #include this file itself.

Also update testidl to generate a random version of each IDL datastructure and convert it to JSON. Unfortunately this requires a libxl_ctx and therefore the test must be run on a Xen system now.

tools/libxl/Makefile +10 -4
diff -r 4e751a05df3c -r fc8319c37864 tools/libxl/Makefile
@@ -32,9 +32,12 @@ endif
 LIBXL_OBJS-$(CONFIG_X86) += libxl_cpuid.o
 LIBXL_OBJS-$(CONFIG_IA64) += libxl_nocpuid.o
 
+LIBXL_LIBS += -lyajl
+
 LIBXL_OBJS = flexarray.o libxl.o libxl_create.o libxl_dm.o libxl_pci.o \
 			libxl_dom.o libxl_exec.o libxl_xshelp.o libxl_device.o \
-			libxl_internal.o libxl_utils.o libxl_uuid.o $(LIBXL_OBJS-y)
+			libxl_internal.o libxl_utils.o libxl_uuid.o libxl_json.o \
+			$(LIBXL_OBJS-y)
 LIBXL_OBJS += _libxl_types.o libxl_flask.o
 
 $(LIBXL_OBJS): CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_libxenguest) $(CFLAGS_libxenstore) $(CFLAGS_libblktapctl)
@@ -81,11 +84,14 @@ libxl_paths.c: _libxl_paths.h
 
 libxl.h: _libxl_types.h
 
+libxl_json.h: _libxl_types_json.h
+
 $(LIBXL_OBJS) $(LIBXLU_OBJS) $(XL_OBJS): libxl.h
 
-_libxl_%.h _libxl_%.c: libxl.idl gen%.py libxl%.py
-	$(PYTHON) gen$*.py libxl.idl __libxl_$*.h __libxl_$*.c
+_libxl_%.h _libxl_%_json.h _libxl_%.c: libxl.idl gen%.py libxltypes.py
+	$(PYTHON) gen$*.py libxl.idl __libxl_$*.h __libxl_$*_json.h __libxl_$*.c
 	$(call move-if-changed,__libxl_$*.h,_libxl_$*.h)
+	$(call move-if-changed,__libxl_$*_json.h,_libxl_$*_json.h)
 	$(call move-if-changed,__libxl_$*.c,_libxl_$*.c)
 
 libxenlight.so: libxenlight.so.$(MAJOR)
@@ -133,7 +139,7 @@ install: all
 	ln -sf libxlutil.so.$(XLUMAJOR).$(XLUMINOR) $(DESTDIR)$(LIBDIR)/libxlutil.so.$(XLUMAJOR)
 	ln -sf libxlutil.so.$(XLUMAJOR) $(DESTDIR)$(LIBDIR)/libxlutil.so
 	$(INSTALL_DATA) libxlutil.a $(DESTDIR)$(LIBDIR)
-	$(INSTALL_DATA) libxl.h _libxl_types.h libxl_utils.h libxl_uuid.h $(DESTDIR)$(INCLUDEDIR)
+	$(INSTALL_DATA) libxl.h libxl_json.h _libxl_types.h _libxl_types_json.h libxl_utils.h libxl_uuid.h $(DESTDIR)$(INCLUDEDIR)
 	$(INSTALL_DATA) bash-completion $(DESTDIR)$(BASH_COMPLETION_DIR)/xl.sh
 
 .PHONY: clean
tools/libxl/gentest.py +229 -6

276 lines not on this page · raw message

tools/libxl/gentypes.py +99 -5

147 lines not on this page · raw message

tools/libxl/idl.txt +9 -0
diff -r 4e751a05df3c -r fc8319c37864 tools/libxl/idl.txt
@@ -49,6 +49,15 @@ Type.autogenerate_destructor: (default: 
  Indicates if the above named Type.destructor_fn should be
  autogenerated.
 
+Type.json_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.autogenerate_json: (default: True)
+
+ Indicates if the above named Type.json_fn should be autogenerated.
+
 Other simple type-Classes
 -------------------------
 
tools/libxl/libxl.h +2 -2
diff -r 4e751a05df3c -r fc8319c37864 tools/libxl/libxl.h
@@ -199,10 +199,10 @@ typedef struct {
     int v;
 } libxl_enum_string_table;
 
+typedef struct libxl__ctx libxl_ctx;
+
 #include "_libxl_types.h"
 
-typedef struct libxl__ctx libxl_ctx;
-
 const libxl_version_info* libxl_get_version_info(libxl_ctx *ctx);
 
 typedef struct {
tools/libxl/libxl.idl +2 -1
diff -r 4e751a05df3c -r fc8319c37864 tools/libxl/libxl.idl
@@ -3,7 +3,8 @@
 # Builtin libxl types
 #
 
-libxl_domid = Builtin("domid")
+libxl_domid = Builtin("domid", json_fn = "yajl_gen_integer", autogenerate_json = False)
+
 libxl_uuid = Builtin("uuid", passby=PASS_BY_REFERENCE)
 libxl_mac = Builtin("mac", passby=PASS_BY_REFERENCE)
 libxl_cpumap = Builtin("cpumap", destructor_fn="libxl_cpumap_destroy", passby=PASS_BY_REFERENCE)
tools/libxl/libxl_internal.h +7 -0
diff -r 4e751a05df3c -r fc8319c37864 tools/libxl/libxl_internal.h
@@ -35,6 +35,7 @@
 
 #include "flexarray.h"
 #include "libxl_utils.h"
+#include "libxl_json.h"
 
 #define LIBXL_DESTROY_TIMEOUT 10
 #define LIBXL_DEVICE_MODEL_START_TIMEOUT 10
@@ -328,6 +329,12 @@ _hidden char *libxl__cpupoolid_to_name(l
 _hidden int libxl__enum_from_string(const libxl_enum_string_table *t,
                                     const char *s, int *e);
 
+_hidden yajl_gen_status libxl__string_gen_json(yajl_gen hand, const char *p);
+
+typedef yajl_gen_status (*libxl__gen_json_callback)(yajl_gen hand, void *);
+_hidden char *libxl__object_to_json(libxl_ctx *ctx, const char *type,
+                                    libxl__gen_json_callback gen, void *p);
+
   /* holds the CPUID response for a single CPUID leaf
    * input contains the value of the EAX and ECX register,
    * and each policy string contains a filter to apply to
tools/libxl/libxl_json.c new file +287 -0

287 lines not on this page · raw message

tools/libxl/libxl_json.h new file +41 -0

41 lines not on this page · raw message

tools/libxl/libxltypes.py +18 -2
diff -r 4e751a05df3c -r fc8319c37864 tools/libxl/libxltypes.py
@@ -37,6 +37,13 @@ class Type(object):
 
         self.autogenerate_destructor = kwargs.setdefault('autogenerate_destructor', True)
 
+        if self.typename is not None:
+            self.json_fn = kwargs.setdefault('json_fn', self.typename + "_gen_json")
+        else:
+            self.json_fn = kwargs.setdefault('json_fn', None)
+
+        self.autogenerate_json = kwargs.setdefault('autogenerate_json', True)
+
     def marshal_in(self):
         return self.dir in [DIR_IN, DIR_BOTH]
     def marshal_out(self):
@@ -70,6 +77,7 @@ class Builtin(Type):
     def __init__(self, typename, **kwargs):
         kwargs.setdefault('destructor_fn', None)
         kwargs.setdefault('autogenerate_destructor', False)
+        kwargs.setdefault('autogenerate_json', False)
         Type.__init__(self, typename, **kwargs)
 
 class Number(Builtin):
@@ -77,6 +85,7 @@ class Number(Builtin):
         kwargs.setdefault('namespace', None)
         kwargs.setdefault('destructor_fn', None)
         kwargs.setdefault('signed', False)
+        kwargs.setdefault('json_fn', "yajl_gen_integer")
         self.signed = kwargs['signed']
         Builtin.__init__(self, ctype, **kwargs)
 
@@ -145,6 +154,8 @@ class Aggregate(Type):
                 comment = None
             else:
                 n,t,const,comment = f
+            if n is None:
+                raise ValueError
             self.fields.append(Field(t,n,const=const,comment=comment))
 
     # Returns a tuple (stem, field-expr)
@@ -199,7 +210,10 @@ class KeyedUnion(Aggregate):
 #
 
 void = Builtin("void *", namespace = None)
-bool = Builtin("bool", namespace = None)
+bool = Builtin("bool", namespace = None,
+               json_fn = "yajl_gen_bool",
+               autogenerate_json = False)
+
 size_t = Number("size_t", namespace = None)
 
 integer = Number("int", namespace = None, signed = True)
@@ -209,7 +223,9 @@ uint16 = UInt(16)
 uint32 = UInt(32)
 uint64 = UInt(64)
 
-string = Builtin("char *", namespace = None, destructor_fn = "free")
+string = Builtin("char *", namespace = None, destructor_fn = "free",
+                 json_fn = "libxl__string_gen_json",
+                 autogenerate_json = False)
 
 class OrderedDict(dict):
     """A dictionary which remembers insertion order.
Acked-by on v1 1/1
#12 lore raw
In reply to Ian Campbell

Ian Campbell writes ("[Xen-devel] [PATCH 00 of 10] libxl: IDL improvements and JSON generation"):

Quoted 3 lines

This patch series makes some improvements to the IDL language and generator and then goes on to implement JSON generation for IDL defined datatypes.

Nice work.

If there are no adverse comments or last-minute changes :-) I will apply this early next week.

Ian.