Skip to contentSkip to conversation

Introduce a QMP client

PATCH Superseded Anthony PERARD 3 participants 25 messages Jul 20, 2011 – Jul 25, 2011

v7 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v7 cover Introduce a QMP client

New:
- New patch that add set_default_namespaces to libxltypes.py, so we can set a namespace to use for a specific .idl.
- New function: libxl__realloc and libxl__strndup.
- The data is now pulled until it's completed (with a CRLF). Now libxl__json_parse expect only a whole json object.
- Use of libxl__gc in sevreal place.

Anthony PERARD (7):
libxl: Rename libxl.idl to libxl_types.idl.
libxl: Add get/set_default_namespace in libxltypes.py.
libxl: Introduce libxl_internal_types.idl. libxl, Introduce libxl__realloc.
libxl, Intruduce libxl__strndup.
libxl: Introduce JSON parsing stuff.
libxl, Introduce a QMP client

 tools/libxl/Makefile                       |   21 +-
 tools/libxl/gentypes.py                    |   15 +-
 tools/libxl/libxl.c                        |    2 +
 tools/libxl/libxl_create.c                 |    4 +
 tools/libxl/libxl_dm.c                     |   10 +
 tools/libxl/libxl_internal.c               |   30 ++
 tools/libxl/libxl_internal.h               |  117 ++++++
 tools/libxl/libxl_json.c                   |  505 +++++++++++++++++++++++
 tools/libxl/libxl_qmp.c                    |  597 ++++++++++++++++++++++++++++
 tools/libxl/{libxl.idl => libxl_types.idl} |    2 +
 tools/libxl/libxl_types_internal.idl       |    9 +
 tools/libxl/libxltypes.py                  |   16 +-
 tools/ocaml/libs/xl/Makefile               |    4 +-
 tools/python/Makefile                      |    4 +-
 14 files changed, 1315 insertions(+), 21 deletions(-)
 create mode 100644 tools/libxl/libxl_json.c
 create mode 100644 tools/libxl/libxl_qmp.c
 rename tools/libxl/{libxl.idl => libxl_types.idl} (99%)
 create mode 100644 tools/libxl/libxl_types_internal.idl
Signature · 1 line
Anthony PERARD
#1 lore raw

PATCH v7 1/7 libxl: Rename libxl.idl to libxl_types.idl.

---
 tools/libxl/Makefile                       |   12 ++++++------
 tools/libxl/{libxl.idl => libxl_types.idl} |    0
 tools/ocaml/libs/xl/Makefile               |    4 ++--
 tools/python/Makefile                      |    4 ++--
 4 files changed, 10 insertions(+), 10 deletions(-)
 rename tools/libxl/{libxl.idl => libxl_types.idl} (100%)
tools/libxl/Makefile +6 -6
index 3eeb26f..330ee6e 100644
@@ -52,8 +52,8 @@ $(XL_OBJS): CFLAGS += $(CFLAGS_libxenctrl) # For xentoollog.h
 $(XL_OBJS): CFLAGS += $(CFLAGS_libxenlight)
 
 testidl.o: CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_libxenlight)
-testidl.c: libxl.idl gentest.py libxl.h
-	$(PYTHON) gentest.py libxl.idl testidl.c.new
+testidl.c: libxl_types.idl gentest.py libxl.h
+	$(PYTHON) gentest.py libxl_types.idl testidl.c.new
 	mv testidl.c.new testidl.c
 
 .PHONY: all
@@ -84,10 +84,10 @@ libxl.h: _libxl_types.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
-	$(call move-if-changed,__libxl_$*.h,_libxl_$*.h)
-	$(call move-if-changed,__libxl_$*.c,_libxl_$*.c)
+_libxl_type%.h _libxl_type%.c: libxl_type%.idl gentypes.py libxltypes.py
+	$(PYTHON) gentypes.py libxl_type$*.idl __libxl_type$*.h __libxl_type$*.c
+	$(call move-if-changed,__libxl_type$*.h,_libxl_type$*.h)
+	$(call move-if-changed,__libxl_type$*.c,_libxl_type$*.c)
 
 libxenlight.so: libxenlight.so.$(MAJOR)
 	ln -sf $< $@
tools/libxl/libxl_types.idl renamed from tools/libxl/libxl.idl +0 -0
similarity index 100%
rename from tools/libxl/libxl.idl
rename to tools/libxl/libxl_types.idl
tools/ocaml/libs/xl/Makefile +2 -2
index 342dc35..a1e79a5 100644
@@ -45,10 +45,10 @@ xl.mli: xl.mli.in _libxl_types.mli.in
 	  < xl.mli.in > xl.mli.tmp
 	$(Q)mv xl.mli.tmp xl.mli
 
-_libxl_types.ml.in _libxl_types.mli.in _libxl_types.inc: genwrap.py $(XEN_ROOT)/tools/libxl/libxl.idl \
+_libxl_types.ml.in _libxl_types.mli.in _libxl_types.inc: genwrap.py $(XEN_ROOT)/tools/libxl/libxl_types.idl \
                 $(XEN_ROOT)/tools/libxl/libxltypes.py
 	PYTHONPATH=$(XEN_ROOT)/tools/libxl $(PYTHON) genwrap.py \
-		$(XEN_ROOT)/tools/libxl/libxl.idl \
+		$(XEN_ROOT)/tools/libxl/libxl_types.idl \
 		_libxl_types.mli.in _libxl_types.ml.in _libxl_types.inc
 
 libs: $(LIBS)
tools/python/Makefile +2 -2
index de44178..b7bc501 100644
@@ -10,10 +10,10 @@ genpath-target = $(call buildmakevars2file,$(XENPATH))
 $(eval $(genpath-target))
 
 .PHONY: build
-build: genpath genwrap.py $(XEN_ROOT)/tools/libxl/libxl.idl \
+build: genpath genwrap.py $(XEN_ROOT)/tools/libxl/libxl_types.idl \
 		$(XEN_ROOT)/tools/libxl/libxltypes.py
 	PYTHONPATH=$(XEN_ROOT)/tools/libxl $(PYTHON) genwrap.py \
-		$(XEN_ROOT)/tools/libxl/libxl.idl \
+		$(XEN_ROOT)/tools/libxl/libxl_types.idl \
 		xen/lowlevel/xl/_pyxl_types.h \
 		xen/lowlevel/xl/_pyxl_types.c
 	CC="$(CC)" CFLAGS="$(CFLAGS)" $(PYTHON) setup.py build
Signature · 1 line
Anthony PERARD
Acked-by on v7 1/7
#2 lore raw

Re: [PATCH V7 1/7] libxl: Rename libxl.idl to libxl_types.idl.

Quoted 1 line · Anthony PERARD
Quoted 76 lines
---
 tools/libxl/Makefile                       |   12 ++++++------
 tools/libxl/{libxl.idl => libxl_types.idl} |    0
 tools/ocaml/libs/xl/Makefile               |    4 ++--
 tools/python/Makefile                      |    4 ++--
 4 files changed, 10 insertions(+), 10 deletions(-)
 rename tools/libxl/{libxl.idl => libxl_types.idl} (100%)
tools/libxl/Makefile +6 -6
index 3eeb26f..330ee6e 100644
@@ -52,8 +52,8 @@ $(XL_OBJS): CFLAGS += $(CFLAGS_libxenctrl) # For xentoollog.h
 $(XL_OBJS): CFLAGS += $(CFLAGS_libxenlight)
 
 testidl.o: CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_libxenlight)
-testidl.c: libxl.idl gentest.py libxl.h
-	$(PYTHON) gentest.py libxl.idl testidl.c.new
+testidl.c: libxl_types.idl gentest.py libxl.h
+	$(PYTHON) gentest.py libxl_types.idl testidl.c.new
 	mv testidl.c.new testidl.c
 
 .PHONY: all
@@ -84,10 +84,10 @@ libxl.h: _libxl_types.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
-	$(call move-if-changed,__libxl_$*.h,_libxl_$*.h)
-	$(call move-if-changed,__libxl_$*.c,_libxl_$*.c)
+_libxl_type%.h _libxl_type%.c: libxl_type%.idl gentypes.py libxltypes.py
+	$(PYTHON) gentypes.py libxl_type$*.idl __libxl_type$*.h __libxl_type$*.c
+	$(call move-if-changed,__libxl_type$*.h,_libxl_type$*.h)
+	$(call move-if-changed,__libxl_type$*.c,_libxl_type$*.c)
 
 libxenlight.so: libxenlight.so.$(MAJOR)
 	ln -sf $< $@
tools/libxl/libxl_types.idl renamed from tools/libxl/libxl.idl +0 -0
similarity index 100%
rename from tools/libxl/libxl.idl
rename to tools/libxl/libxl_types.idl
tools/ocaml/libs/xl/Makefile +2 -2
index 342dc35..a1e79a5 100644
@@ -45,10 +45,10 @@ xl.mli: xl.mli.in _libxl_types.mli.in
 	  < xl.mli.in > xl.mli.tmp
 	$(Q)mv xl.mli.tmp xl.mli
 
-_libxl_types.ml.in _libxl_types.mli.in _libxl_types.inc: genwrap.py $(XEN_ROOT)/tools/libxl/libxl.idl \
+_libxl_types.ml.in _libxl_types.mli.in _libxl_types.inc: genwrap.py $(XEN_ROOT)/tools/libxl/libxl_types.idl \
                 $(XEN_ROOT)/tools/libxl/libxltypes.py
 	PYTHONPATH=$(XEN_ROOT)/tools/libxl $(PYTHON) genwrap.py \
-		$(XEN_ROOT)/tools/libxl/libxl.idl \
+		$(XEN_ROOT)/tools/libxl/libxl_types.idl \
 		_libxl_types.mli.in _libxl_types.ml.in _libxl_types.inc
 
 libs: $(LIBS)
tools/python/Makefile +2 -2
index de44178..b7bc501 100644
@@ -10,10 +10,10 @@ genpath-target = $(call buildmakevars2file,$(XENPATH))
 $(eval $(genpath-target))
 
 .PHONY: build
-build: genpath genwrap.py $(XEN_ROOT)/tools/libxl/libxl.idl \
+build: genpath genwrap.py $(XEN_ROOT)/tools/libxl/libxl_types.idl \
 		$(XEN_ROOT)/tools/libxl/libxltypes.py
 	PYTHONPATH=$(XEN_ROOT)/tools/libxl $(PYTHON) genwrap.py \
-		$(XEN_ROOT)/tools/libxl/libxl.idl \
+		$(XEN_ROOT)/tools/libxl/libxl_types.idl \
 		xen/lowlevel/xl/_pyxl_types.h \
 		xen/lowlevel/xl/_pyxl_types.c
 	CC="$(CC)" CFLAGS="$(CFLAGS)" $(PYTHON) setup.py build
#3 lore raw

PATCH v7 2/7 libxl: Add get/set_default_namespace in libxltypes.py.

In reply to Anthony PERARD
---
 tools/libxl/libxl_types.idl |    2 ++
 tools/libxl/libxltypes.py   |   16 ++++++++++++++--
 2 files changed, 16 insertions(+), 2 deletions(-)
tools/libxl/libxl_types.idl +2 -0
index f7249b1..718688d 100644
@@ -3,6 +3,8 @@
 # Builtin libxl types
 #
 
+set_default_namespace("libxl_")
+
 libxl_domid = Builtin("domid")
 libxl_uuid = Builtin("uuid", passby=PASS_BY_REFERENCE)
 libxl_mac = Builtin("mac", passby=PASS_BY_REFERENCE)
tools/libxl/libxltypes.py +14 -2
index b7b4669..64d2f50 100644
@@ -8,10 +8,21 @@ DIR_IN   = 1
 DIR_OUT  = 2
 DIR_BOTH = 3
 
+default_namespace = ""
+def set_default_namespace(s):
+  if type(s) != str:
+    raise TypeError, "Require a string for the default namespace."
+  global default_namespace
+  default_namespace = s
+
+def get_default_namespace():
+  global default_namespace
+  return default_namespace
+
 class Type(object):
     def __init__(self, typename, **kwargs):
         self.comment = kwargs.setdefault('comment', None)
-        self.namespace = kwargs.setdefault('namespace', "libxl_")
+        self.namespace = kwargs.setdefault('namespace', get_default_namespace())
         self.dir = kwargs.setdefault('dir', DIR_BOTH)
         if self.dir not in [DIR_NONE, DIR_IN, DIR_OUT, DIR_BOTH]:
             raise ValueError
@@ -248,7 +259,8 @@ def parse(f):
         elif isinstance(t,type(object)) and issubclass(t, Type):
             globs[n] = t
         elif n in ['PASS_BY_REFERENCE', 'PASS_BY_VALUE',
-                   'DIR_NONE', 'DIR_IN', 'DIR_OUT', 'DIR_BOTH']:
+                   'DIR_NONE', 'DIR_IN', 'DIR_OUT', 'DIR_BOTH',
+                   'set_default_namespace']:
             globs[n] = t
 
     try:
Signature · 1 line
Anthony PERARD
Acked-by on v7 2/7
#4 lore raw

Re: [PATCH V7 2/7] libxl: Add get/set_default_namespace in libxltypes.py.

Quoted 1 line · Anthony PERARD

I'm not sure if it matters but default_namespace should possibly be private to the module (__ at the start)? Perhaps get_default_namesspace too, probably neither .idl files nor gen*.py should ever need to query this, they should use the info in specific types.

If I were prone to bikeshedding I'd suggest that "set_default_namespace" could be just "namespace" since it would read nicer in the .idl files.

In any case that's all nit picking so:

Quoted 55 lines
---
 tools/libxl/libxl_types.idl |    2 ++
 tools/libxl/libxltypes.py   |   16 ++++++++++++++--
 2 files changed, 16 insertions(+), 2 deletions(-)
tools/libxl/libxl_types.idl +2 -0
index f7249b1..718688d 100644
@@ -3,6 +3,8 @@
 # Builtin libxl types
 #
 
+set_default_namespace("libxl_")
+
 libxl_domid = Builtin("domid")
 libxl_uuid = Builtin("uuid", passby=PASS_BY_REFERENCE)
 libxl_mac = Builtin("mac", passby=PASS_BY_REFERENCE)
tools/libxl/libxltypes.py +14 -2
index b7b4669..64d2f50 100644
@@ -8,10 +8,21 @@ DIR_IN   = 1
 DIR_OUT  = 2
 DIR_BOTH = 3
 
+default_namespace = ""
+def set_default_namespace(s):
+  if type(s) != str:
+    raise TypeError, "Require a string for the default namespace."
+  global default_namespace
+  default_namespace = s
+
+def get_default_namespace():
+  global default_namespace
+  return default_namespace
+
 class Type(object):
     def __init__(self, typename, **kwargs):
         self.comment = kwargs.setdefault('comment', None)
-        self.namespace = kwargs.setdefault('namespace', "libxl_")
+        self.namespace = kwargs.setdefault('namespace', get_default_namespace())
         self.dir = kwargs.setdefault('dir', DIR_BOTH)
         if self.dir not in [DIR_NONE, DIR_IN, DIR_OUT, DIR_BOTH]:
             raise ValueError
@@ -248,7 +259,8 @@ def parse(f):
         elif isinstance(t,type(object)) and issubclass(t, Type):
             globs[n] = t
         elif n in ['PASS_BY_REFERENCE', 'PASS_BY_VALUE',
-                   'DIR_NONE', 'DIR_IN', 'DIR_OUT', 'DIR_BOTH']:
+                   'DIR_NONE', 'DIR_IN', 'DIR_OUT', 'DIR_BOTH',
+                   'set_default_namespace']:
             globs[n] = t
 
     try:
#5 lore raw

Re: [PATCH V7 2/7] libxl: Add get/set_default_namespace in libxltypes.py.

Quoted 7 lines · Ian Campbell
Quoted 1 line · Anthony PERARD

I'm not sure if it matters but default_namespace should possibly be private to the module (__ at the start)? Perhaps get_default_namesspace too, probably neither .idl files nor gen*.py should ever need to query this, they should use the info in specific types.

Ok, I will change names but use only one '_' for "private" function/variable.

Quoted 2 lines

If I were prone to bikeshedding I'd suggest that "set_default_namespace" could be just "namespace" since it would read nicer in the .idl files.

:), I'll change that too.

Quoted 3 lines

In any case that's all nit picking so:

Thanks,

Signature · 1 line
Anthony PERARD
#6 lore raw

Re: [PATCH V7 2/7] libxl: Add get/set_default_namespace in libxltypes.py.

In reply to Anthony PERARD
Quoted 27 lines · Anthony PERARD
---
 tools/libxl/libxl_types.idl |    2 ++
 tools/libxl/libxltypes.py   |   16 ++++++++++++++--
 2 files changed, 16 insertions(+), 2 deletions(-)
tools/libxl/libxl_types.idl +2 -0
index f7249b1..718688d 100644
@@ -3,6 +3,8 @@
 # Builtin libxl types
 #
 
+set_default_namespace("libxl_")
+
 libxl_domid = Builtin("domid")
 libxl_uuid = Builtin("uuid", passby=PASS_BY_REFERENCE)
 libxl_mac = Builtin("mac", passby=PASS_BY_REFERENCE)
tools/libxl/libxltypes.py +0 -0
index b7b4669..64d2f50 100644
@@ -8,10 +8,21 @@ DIR_IN   = 1
 DIR_OUT  = 2
 DIR_BOTH = 3
 

tools/libxl/libxltypes.py DIR_IN = 1 · line 20

Quoted 10 lines
tools/libxl/libxltypes.py +10 -0
+default_namespace = ""
+def set_default_namespace(s):
+  if type(s) != str:
+    raise TypeError, "Require a string for the default namespace."
+  global default_namespace
+  default_namespace = s
+
+def get_default_namespace():
+  global default_namespace
+  return default_namespace

Can you make this 4-space indent please. The rest of this file is indented that way but this comes first so emacs' python-guess-indent guesses 2 and that confuses the autoindent of the following code.

Ian.

Quoted 19 lines
tools/libxl/libxltypes.py +4 -2
+
 class Type(object):
     def __init__(self, typename, **kwargs):
         self.comment = kwargs.setdefault('comment', None)
-        self.namespace = kwargs.setdefault('namespace', "libxl_")
+        self.namespace = kwargs.setdefault('namespace', get_default_namespace())
         self.dir = kwargs.setdefault('dir', DIR_BOTH)
         if self.dir not in [DIR_NONE, DIR_IN, DIR_OUT, DIR_BOTH]:
             raise ValueError
@@ -248,7 +259,8 @@ def parse(f):
         elif isinstance(t,type(object)) and issubclass(t, Type):
             globs[n] = t
         elif n in ['PASS_BY_REFERENCE', 'PASS_BY_VALUE',
-                   'DIR_NONE', 'DIR_IN', 'DIR_OUT', 'DIR_BOTH']:
+                   'DIR_NONE', 'DIR_IN', 'DIR_OUT', 'DIR_BOTH',
+                   'set_default_namespace']:
             globs[n] = t
 
     try:
#7 lore raw

PATCH v7 3/7 libxl: Introduce libxl_internal_types.idl.

In reply to Anthony PERARD
---
 tools/libxl/Makefile                 |    4 +++-
 tools/libxl/gentypes.py              |   15 ++++++++-------
 tools/libxl/libxl_internal.h         |    1 +
 tools/libxl/libxl_types_internal.idl |    9 +++++++++
 4 files changed, 21 insertions(+), 8 deletions(-)
 create mode 100644 tools/libxl/libxl_types_internal.idl
tools/libxl/Makefile +3 -1
index 330ee6e..1f6b418 100644
@@ -35,7 +35,7 @@ LIBXL_OBJS-$(CONFIG_IA64) += libxl_nocpuid.o
 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_OBJS += _libxl_types.o libxl_flask.o
+LIBXL_OBJS += _libxl_types.o libxl_flask.o _libxl_types_internal.o
 
 $(LIBXL_OBJS): CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_libxenguest) $(CFLAGS_libxenstore) $(CFLAGS_libblktapctl)
 
@@ -81,8 +81,10 @@ _libxl_paths.h: genpath
 libxl_paths.c: _libxl_paths.h
 
 libxl.h: _libxl_types.h
+libxl_internal.h: _libxl_types_internal.h
 
 $(LIBXL_OBJS) $(LIBXLU_OBJS) $(XL_OBJS): libxl.h
+$(LIBXL_OBJS): libxl_internal.h
 
 _libxl_type%.h _libxl_type%.c: libxl_type%.idl gentypes.py libxltypes.py
 	$(PYTHON) gentypes.py libxl_type$*.idl __libxl_type$*.h __libxl_type$*.c
tools/libxl/gentypes.py +8 -7
index 120c6d3..e807f94 100644
@@ -148,9 +148,10 @@ if __name__ == '__main__':
     print "outputting libxl type definitions to %s" % header
 
     f = open(header, "w")
-    
-    f.write("""#ifndef __LIBXL_TYPES_H
-#define __LIBXL_TYPES_H
+
+    header_define = header.upper().replace('.','_')
+    f.write("""#ifndef %s
+#define %s
 
 /*
  * DO NOT EDIT.
@@ -158,9 +159,9 @@ if __name__ == '__main__':
  * This file is autogenerated by
  * "%s"
  */
- 
-""" % " ".join(sys.argv))
-        
+
+""" % (header_define, header_define, " ".join(sys.argv)))
+
     for ty in types:
         f.write(libxl_C_type_define(ty) + ";\n")
         if ty.destructor_fn is not None:
@@ -171,7 +172,7 @@ if __name__ == '__main__':
             f.write("extern libxl_enum_string_table %s_string_table[];\n" % (ty.typename))
         f.write("\n")
 
-    f.write("""#endif /* __LIBXL_TYPES_H */\n""")
+    f.write("""#endif /* %s */\n""" % (header_define))
     f.close()
     
     print "outputting libxl type implementations to %s" % impl
tools/libxl/libxl_internal.h +1 -0
index 579188e..4580be9 100644
@@ -35,6 +35,7 @@
 
 #include "flexarray.h"
 #include "libxl_utils.h"
+#include "_libxl_types_internal.h"
 
 #define LIBXL_DESTROY_TIMEOUT 10
 #define LIBXL_DEVICE_MODEL_START_TIMEOUT 10
tools/libxl/libxl_types_internal.idl new file +9 -0
new file mode 100644
index 0000000..0086bf0
@@ -0,0 +1,9 @@
+set_default_namespace("libxl__")
+
+libxl__qmp_message_type  = Enumeration("qmp_message_type", [
+    (1, "QMP"),
+    (2, "return"),
+    (3, "error"),
+    (4, "event"),
+    (5, "invalid"),
+    ])
Signature · 1 line
Anthony PERARD
Acked-by on v7 3/7
#8 lore raw

Re: [PATCH V7 3/7] libxl: Introduce libxl_internal_types.idl.

Quoted 1 line · Anthony PERARD

This needs rebasing over 23728:548b2826293e "libxl: clean up trailing whitespaces in code" otherwise:

tools/libxl/gentypes.py if __name__ == '__main__': · line 152

Quoted 5 lines
tools/libxl/gentypes.py +2 -3
-    
-    f.write("""#ifndef __LIBXL_TYPES_H
-#define __LIBXL_TYPES_H
+
+    header_define = header.upper().replace('.','_')

It took me ages to figure out that this doesn't change the define because we generate to a temporary file with a __ prefix...

#9 lore raw

PATCH v7 4/7 libxl, Introduce libxl__realloc.

In reply to Anthony PERARD
---
 tools/libxl/libxl_internal.c |   20 ++++++++++++++++++++
 tools/libxl/libxl_internal.h |    1 +
 2 files changed, 21 insertions(+), 0 deletions(-)
tools/libxl/libxl_internal.c +20 -0
index e259278..527ebc5 100644
@@ -102,6 +102,26 @@ void *libxl__calloc(libxl__gc *gc, size_t nmemb, size_t size)
     return ptr;
 }
 
+void *libxl__realloc(libxl__gc *gc, void *ptr, size_t new_size)
+{
+    void *new_ptr = realloc(ptr, new_size);
+    int i = 0;
+
+    if (ptr == NULL) {
+        libxl__ptr_add(gc, new_ptr);
+    } else if (new_ptr != ptr) {
+        for (i = 0; i < gc->alloc_maxsize; i++) {
+            if (gc->alloc_ptrs[i] == ptr) {
+                gc->alloc_ptrs[i] = new_ptr;
+                break;
+            }
+        }
+    }
+
+
+    return new_ptr;
+}
+
 char *libxl__sprintf(libxl__gc *gc, const char *fmt, ...)
 {
     char *s;
tools/libxl/libxl_internal.h +1 -0
index 4580be9..64942e6 100644
@@ -145,6 +145,7 @@ _hidden int libxl__ptr_add(libxl__gc *gc, void *ptr);
 _hidden void libxl__free_all(libxl__gc *gc);
 _hidden void *libxl__zalloc(libxl__gc *gc, int bytes);
 _hidden void *libxl__calloc(libxl__gc *gc, size_t nmemb, size_t size);
+_hidden void *libxl__realloc(libxl__gc *gc, void *ptr, size_t new_size);
 _hidden char *libxl__sprintf(libxl__gc *gc, const char *fmt, ...) PRINTF_ATTRIBUTE(2, 3);
 _hidden char *libxl__strdup(libxl__gc *gc, const char *c);
 _hidden char *libxl__dirname(libxl__gc *gc, const char *s);
Signature · 1 line
Anthony PERARD
#10 lore raw

Re: [PATCH V7 4/7] libxl, Introduce libxl__realloc.

Quoted 11 lines · Anthony PERARD
---
 tools/libxl/libxl_internal.c |   20 ++++++++++++++++++++
 tools/libxl/libxl_internal.h |    1 +
 2 files changed, 21 insertions(+), 0 deletions(-)
tools/libxl/libxl_internal.c +0 -0
index e259278..527ebc5 100644

tools/libxl/libxl_internal.c void *libxl__calloc(libxl__gc *gc, size_t nmemb, size_t size) · line 107

Quoted 6 lines
tools/libxl/libxl_internal.c +3 -0
@@ -102,6 +102,26 @@ void *libxl__calloc(libxl__gc *gc, size_t nmemb, size_t size)
     return ptr;
 }
 
+void *libxl__realloc(libxl__gc *gc, void *ptr, size_t new_size)
+{
+    void *new_ptr = realloc(ptr, new_size);

On failure realloc will return NULL but not free the old pointer, so I think in that case you will set alloc_ptrs[i] to NULL but not actually free the old pointer, hence leaking it.

I think you can just check for new_ptr == NULL and return NULL up front. Normally that would leak the old ptr but by leaving it in the gc array we avoid that pitfall.

If new_size==0 then realloc behaves as free. I reckon you can just return NULL then too and allow the gc to clean up. Or you could outlaw such uses in this interface and abort(), that seems harsh however.

BTW libxl__free_all does cope correctly with NULLs mid-way through alloc_ptrs[] which did concern me initially.

Ian.

Quoted 34 lines
tools/libxl/libxl_internal.c +17 -0
+    int i = 0;
+
+    if (ptr == NULL) {
+        libxl__ptr_add(gc, new_ptr);
+    } else if (new_ptr != ptr) {
+        for (i = 0; i < gc->alloc_maxsize; i++) {
+            if (gc->alloc_ptrs[i] == ptr) {
+                gc->alloc_ptrs[i] = new_ptr;
+                break;
+            }
+        }
+    }
 
 
+
+
+    return new_ptr;
+}
+
 char *libxl__sprintf(libxl__gc *gc, const char *fmt, ...)
 {
     char *s;
tools/libxl/libxl_internal.h +1 -0
index 4580be9..64942e6 100644
@@ -145,6 +145,7 @@ _hidden int libxl__ptr_add(libxl__gc *gc, void *ptr);
 _hidden void libxl__free_all(libxl__gc *gc);
 _hidden void *libxl__zalloc(libxl__gc *gc, int bytes);
 _hidden void *libxl__calloc(libxl__gc *gc, size_t nmemb, size_t size);
+_hidden void *libxl__realloc(libxl__gc *gc, void *ptr, size_t new_size);
 _hidden char *libxl__sprintf(libxl__gc *gc, const char *fmt, ...) PRINTF_ATTRIBUTE(2, 3);
 _hidden char *libxl__strdup(libxl__gc *gc, const char *c);
 _hidden char *libxl__dirname(libxl__gc *gc, const char *s);
#11 lore raw

Re: [PATCH V7 4/7] libxl, Introduce libxl__realloc.

Quoted 15 lines · Ian Campbell
Quoted 14 lines · Anthony PERARD
---
 tools/libxl/libxl_internal.c |   20 ++++++++++++++++++++
 tools/libxl/libxl_internal.h |    1 +
 2 files changed, 21 insertions(+), 0 deletions(-)
tools/libxl/libxl_internal.c +0 -0
index e259278..527ebc5 100644
@@ -102,6 +102,26 @@ void *libxl__calloc(libxl__gc *gc, size_t nmemb, size_t size)
     return ptr;
 }
 
Quoted 7 lines

tools/libxl/libxl_internal.c void *libxl__calloc(libxl__gc *gc, size_t nmemb, size_t size) · line 107

Quoted 3 lines
tools/libxl/libxl_internal.c +3 -0
+void *libxl__realloc(libxl__gc *gc, void *ptr, size_t new_size)
+{
+    void *new_ptr = realloc(ptr, new_size);

On failure realloc will return NULL but not free the old pointer, so I think in that case you will set alloc_ptrs[i] to NULL but not actually free the old pointer, hence leaking it.

Oops, I'll fix that, and just return new_ptr (without removing the pointer from gc).

Quoted 7 lines

I think you can just check for new_ptr == NULL and return NULL up front. Normally that would leak the old ptr but by leaving it in the gc array we avoid that pitfall.

If new_size==0 then realloc behaves as free. I reckon you can just return NULL then too and allow the gc to clean up. Or you could outlaw such uses in this interface and abort(), that seems harsh however.

I think I will remove the pointer from gc, so libxl__realloc will behave as expected and free stuff if asked.

Quoted 2 lines

BTW libxl__free_all does cope correctly with NULLs mid-way through alloc_ptrs[] which did concern me initially.

Thanks,

Signature · 1 line
Anthony PERARD
#12 lore raw

PATCH v7 5/7 libxl, Intruduce libxl__strndup.

In reply to Anthony PERARD
---
 tools/libxl/libxl_internal.c |   10 ++++++++++
 tools/libxl/libxl_internal.h |    1 +
 2 files changed, 11 insertions(+), 0 deletions(-)
tools/libxl/libxl_internal.c +10 -0
index 527ebc5..66f9c16 100644
@@ -155,6 +155,16 @@ char *libxl__strdup(libxl__gc *gc, const char *c)
     return s;
 }
 
+char *libxl__strndup(libxl__gc *gc, const char *c, size_t n)
+{
+    char *s = strndup(c, n);
+
+    if (s)
+        libxl__ptr_add(gc, s);
+
+    return s;
+}
+
 char *libxl__dirname(libxl__gc *gc, const char *s)
 {
     char *c;
tools/libxl/libxl_internal.h +1 -0
index 64942e6..839df14 100644
@@ -148,6 +148,7 @@ _hidden void *libxl__calloc(libxl__gc *gc, size_t nmemb, size_t size);
 _hidden void *libxl__realloc(libxl__gc *gc, void *ptr, size_t new_size);
 _hidden char *libxl__sprintf(libxl__gc *gc, const char *fmt, ...) PRINTF_ATTRIBUTE(2, 3);
 _hidden char *libxl__strdup(libxl__gc *gc, const char *c);
+_hidden char *libxl__strndup(libxl__gc *gc, const char *c, size_t n);
 _hidden char *libxl__dirname(libxl__gc *gc, const char *s);
 
 _hidden char **libxl__xs_kvs_of_flexarray(libxl__gc *gc, flexarray_t *array, int length);
Signature · 1 line
Anthony PERARD
Acked-by on v7 5/7
#13 lore raw

Re: [PATCH V7 5/7] libxl, Intruduce libxl__strndup.

Quoted 1 line · Anthony PERARD
Quoted 38 lines
---
 tools/libxl/libxl_internal.c |   10 ++++++++++
 tools/libxl/libxl_internal.h |    1 +
 2 files changed, 11 insertions(+), 0 deletions(-)
tools/libxl/libxl_internal.c +10 -0
index 527ebc5..66f9c16 100644
@@ -155,6 +155,16 @@ char *libxl__strdup(libxl__gc *gc, const char *c)
     return s;
 }
 
+char *libxl__strndup(libxl__gc *gc, const char *c, size_t n)
+{
+    char *s = strndup(c, n);
+
+    if (s)
+        libxl__ptr_add(gc, s);
+
+    return s;
+}
+
 char *libxl__dirname(libxl__gc *gc, const char *s)
 {
     char *c;
tools/libxl/libxl_internal.h +1 -0
index 64942e6..839df14 100644
@@ -148,6 +148,7 @@ _hidden void *libxl__calloc(libxl__gc *gc, size_t nmemb, size_t size);
 _hidden void *libxl__realloc(libxl__gc *gc, void *ptr, size_t new_size);
 _hidden char *libxl__sprintf(libxl__gc *gc, const char *fmt, ...) PRINTF_ATTRIBUTE(2, 3);
 _hidden char *libxl__strdup(libxl__gc *gc, const char *c);
+_hidden char *libxl__strndup(libxl__gc *gc, const char *c, size_t n);
 _hidden char *libxl__dirname(libxl__gc *gc, const char *s);
 
 _hidden char **libxl__xs_kvs_of_flexarray(libxl__gc *gc, flexarray_t *array, int length);
#14 lore raw

PATCH v7 6/7 libxl: Introduce JSON parsing stuff.

In reply to Anthony PERARD

We use the yajl parser, but we need to make a tree from the parse result to use it outside the parser.

So this patch include json_object struct that is used to hold the JSON data.

---
 tools/libxl/Makefile         |    5 +-
 tools/libxl/libxl_internal.h |   95 ++++++++
 tools/libxl/libxl_json.c     |  505 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 604 insertions(+), 1 deletions(-)
 create mode 100644 tools/libxl/libxl_json.c
tools/libxl/Makefile +4 -1
index 1f6b418..e50874e 100644
@@ -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_types_internal.o
 
 $(LIBXL_OBJS): CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_libxenguest) $(CFLAGS_libxenstore) $(CFLAGS_libblktapctl)
tools/libxl/libxl_internal.h +95 -0

99 lines not on this page · raw message

tools/libxl/libxl_json.c new file +505 -0

505 lines not on this page · raw message

Signature · 1 line
Anthony PERARD
#15 lore raw

Re: [PATCH V7 6/7] libxl: Introduce JSON parsing stuff.

Quoted 19 lines · Anthony PERARD

We use the yajl parser, but we need to make a tree from the parse result to use it outside the parser.

So this patch include json_object struct that is used to hold the JSON data.

---
 tools/libxl/Makefile         |    5 +-
 tools/libxl/libxl_internal.h |   95 ++++++++
 tools/libxl/libxl_json.c     |  505 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 604 insertions(+), 1 deletions(-)
 create mode 100644 tools/libxl/libxl_json.c
tools/libxl/libxl_internal.h +0 -0
index 839df14..a8aa6d5 100644
@@ -382,4 +382,99 @@ _hidden int libxl__e820_alloc(libxl_ctx *ctx, uint32_t domid, libxl_domain_confi

[...]

Quoted 10 lines
tools/libxl/libxl_internal.h +5 -0
+static inline bool json_object_is_string(const libxl__json_object *o)
[...]
+static inline bool json_object_is_integer(const libxl__json_object *o)
[...]
+static inline bool json_object_is_map(const libxl__json_object *o)
[...]
+static inline bool json_object_is_array(const libxl__json_object *o)
[...]
+static inline const char *json_object_get_string(const libxl__json_object *o)
[...]

tools/libxl/libxl_internal.h _hidden int libxl__e820_alloc(libxl_ctx *ctx, uint32_t domid, libxl_domain_confi · line 469

Quoted 10 lines
tools/libxl/libxl_internal.h +7 -0
+static inline flexarray_t *json_object_get_map(const libxl__json_object *o)
[...]
+static inline flexarray_t *json_object_get_array(const libxl__json_object *o)
[...]
+static inline long json_object_get_integer(const libxl__json_object *o)
[...]
+_hidden const libxl__json_object *json_object_get(const char *key,
+                                          const libxl__json_object *o,
+                                          libxl__json_node_type expected_type);
+_hidden void json_object_free(libxl_ctx *ctx, libxl__json_object *obj);

These should all be libxl__json_object_FOO. (just json is ok for static functions in .c files if you prefer but for functions in headers I think correct namespacing makes sense).

Internal functions (such as [libxl__]json_object_free) should always take a libxl__gc and not a libxl_ctx.

Quoted 2 lines

+/* s: is the buffer to parse, libxl__json_parse will advance the pointer the
+ * part that has not been parsed

It can't advance a "const char *s" (at least not in a way which the caller can see). Is the comment or the implementation wrong?

tools/libxl/libxl_json.c

Quoted 13 lines

+ * *yajl_ctx: is set if the buffer have been whole consume, but the JSON
+ * structure is not complete.
+ * return NULL in case of error or when the JSON structure is not complete.
+ */

tools/libxl/libxl_internal.h +3 -0
+_hidden libxl__json_object *libxl__json_parse(libxl__gc *gc,
+                                              const char *s);
+
 #endif
tools/libxl/libxl_json.c new file +0 -0
new file mode 100644
index 0000000..d0fa5be

I think I reviewed this before so I just did a quick skim.

tools/libxl/libxl_json.c line 185

Quoted 8 lines
tools/libxl/libxl_json.c new file +8 -0
+const libxl__json_object *json_object_get(const char *key,
+                                          const libxl__json_object *o,
+                                          libxl__json_node_type expected_type)
+{
+    flexarray_t *maps = NULL;
+    int index = 0;
+
+    if (json_object_is_map(o)) {

This function only operates on map types? Should it therefore be names [libxl__]json_map_get?

Is passing a non-map to this function a hard error (e.g. abort())?

I suppose I'll find out in the next patch ;-)

Quoted 1 line

[...]

Ian.

#16 lore raw

Re: [PATCH V7 6/7] libxl: Introduce JSON parsing stuff.

Quoted 46 lines · Ian Campbell
Quoted 19 lines · Anthony PERARD

We use the yajl parser, but we need to make a tree from the parse result to use it outside the parser.

So this patch include json_object struct that is used to hold the JSON data.

---
 tools/libxl/Makefile         |    5 +-
 tools/libxl/libxl_internal.h |   95 ++++++++
 tools/libxl/libxl_json.c     |  505 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 604 insertions(+), 1 deletions(-)
 create mode 100644 tools/libxl/libxl_json.c
tools/libxl/libxl_internal.h +0 -0
index 839df14..a8aa6d5 100644
@@ -382,4 +382,99 @@ _hidden int libxl__e820_alloc(libxl_ctx *ctx, uint32_t domid, libxl_domain_confi

[...]

Quoted 10 lines
tools/libxl/libxl_internal.h +5 -0
+static inline bool json_object_is_string(const libxl__json_object *o)
[...]
+static inline bool json_object_is_integer(const libxl__json_object *o)
[...]
+static inline bool json_object_is_map(const libxl__json_object *o)
[...]
+static inline bool json_object_is_array(const libxl__json_object *o)
[...]
+static inline const char *json_object_get_string(const libxl__json_object *o)
[...]

tools/libxl/libxl_internal.h _hidden int libxl__e820_alloc(libxl_ctx *ctx, uint32_t domid, libxl_domain_confi · line 469

Quoted 10 lines
tools/libxl/libxl_internal.h +7 -0
+static inline flexarray_t *json_object_get_map(const libxl__json_object *o)
[...]
+static inline flexarray_t *json_object_get_array(const libxl__json_object *o)
[...]
+static inline long json_object_get_integer(const libxl__json_object *o)
[...]
+_hidden const libxl__json_object *json_object_get(const char *key,
+                                          const libxl__json_object *o,
+                                          libxl__json_node_type expected_type);
+_hidden void json_object_free(libxl_ctx *ctx, libxl__json_object *obj);

These should all be libxl__json_object_FOO. (just json is ok for static functions in .c files if you prefer but for functions in headers I think correct namespacing makes sense).

Quoted 2 lines

Internal functions (such as [libxl__]json_object_free) should always take a libxl__gc and not a libxl_ctx.

Ok, I will change that.

Quoted 5 lines
Quoted 2 lines

+/* s: is the buffer to parse, libxl__json_parse will advance the pointer the
+ * part that has not been parsed

It can't advance a "const char *s" (at least not in a way which the caller can see). Is the comment or the implementation wrong?

The comment is wrong, not updated. I'll just remove it as I think the prototype is explicit enough. (Parse *s, return a json_object OR NULL in case of error)

Quoted 16 lines

tools/libxl/libxl_json.c

Quoted 13 lines

+ * *yajl_ctx: is set if the buffer have been whole consume, but the JSON
+ * structure is not complete.
+ * return NULL in case of error or when the JSON structure is not complete.
+ */

tools/libxl/libxl_internal.h +3 -0
+_hidden libxl__json_object *libxl__json_parse(libxl__gc *gc,
+                                              const char *s);
+
 #endif
tools/libxl/libxl_json.c new file +0 -0
new file mode 100644
index 0000000..d0fa5be

I think I reviewed this before so I just did a quick skim.

Quoted 11 lines

tools/libxl/libxl_json.c line 185

Quoted 8 lines
tools/libxl/libxl_json.c new file +8 -0
+const libxl__json_object *json_object_get(const char *key,
+                                          const libxl__json_object *o,
+                                          libxl__json_node_type expected_type)
+{
+    flexarray_t *maps = NULL;
+    int index = 0;
+
+    if (json_object_is_map(o)) {

This function only operates on map types? Should it therefore be names [libxl__]json_map_get?

Yes, the only type to use keys. So, I'll rename it.

Quoted 3 lines

Is passing a non-map to this function a hard error (e.g. abort())?

I suppose I'll find out in the next patch ;-)

It is the same error as if the map does not contain the key or the type of the object found is not the right one. So not a hard error here.

Thanks,

Signature · 1 line
Anthony PERARD
#17 lore raw

Re: [PATCH V7 6/7] libxl: Introduce JSON parsing stuff.

In reply to Anthony PERARD

tools/libxl/Makefile endif · line 35

Quoted 9 lines · Anthony PERARD
tools/libxl/Makefile +1 -0
index 1f6b418..e50874e 100644
@@ -32,9 +32,12 @@ endif
 LIBXL_OBJS-$(CONFIG_X86) += libxl_cpuid.o
 LIBXL_OBJS-$(CONFIG_IA64) += libxl_nocpuid.o
 
+LIBXL_LIBS += -lyajl

I'm not convinced it is up to date and/or canonical but it would be worth adding yajl to the exteral requirements list in the top-level README too.

Ian.

#18 lore raw

Re: [PATCH V7 6/7] libxl: Introduce JSON parsing stuff.

In reply to Anthony PERARD

tools/libxl/libxl_json.c line 480

Quoted 4 lines · Anthony PERARD
tools/libxl/libxl_json.c new file +4 -0
+    /* parse the input */
+    if (yajl_ctx.hand == NULL) {
+        /* allow comments */
+        yajl_parser_config cfg = { 1, 1 }; 

Looks like YALJ v2 has enums for these config values but v1 doesn't. However

            yajl_parse_config cfg = {
                .allowComments = 1,
                .checkUTF8 = 1,
            };

is a bit more self documenting.

(supporting yajl 1 and 2 is going to be a pain. but lets cross that bridge when we come to it...)

Ian.

#19 lore raw

Re: Re: [PATCH V7 6/7] libxl: Introduce JSON parsing stuff.

Quoted 14 lines · Ian Campbell
Quoted 4 lines · Anthony PERARD

+    /* parse the input */
+    if (yajl_ctx.hand == NULL) {
+        /* allow comments */
+        yajl_parser_config cfg = { 1, 1 };

Looks like YALJ v2 has enums for these config values but v1 doesn't. However
           yajl_parse_config cfg = {
               .allowComments = 1,
               .checkUTF8 = 1,
           };
is a bit more self documenting.

Quoted 2 lines

(supporting yajl 1 and 2 is going to be a pain. but lets cross that bridge when we come to it...)

With the V2, there is "yajl_tree_parse" that basicly do the same as libxl__json_parse. I try to be closer to the tree implementation of the V2, but yes, it will sill be painfull to support both. If we can only support the v1, and change to the v2 later (when widly availlable in distribution), it should be OK.

Signature · 1 line
Anthony PERARD
#20 lore raw

PATCH v7 7/7 libxl, Introduce a QMP client

In reply to Anthony PERARD

QMP stands for QEMU Monitor Protocol and it is used to query information from QEMU or to control QEMU.

This implementation will ask QEMU the list of chardevice and store the path to serial0 in xenstored. So we will be able to use xl console with QEMU upstream.

In order to connect to the QMP server, a socket file is created in /var/run/xen/qmp-$(domid).

---
 tools/libxl/Makefile         |    2 +-
 tools/libxl/libxl.c          |    2 +
 tools/libxl/libxl_create.c   |    4 +
 tools/libxl/libxl_dm.c       |   10 +
 tools/libxl/libxl_internal.h |   19 ++
 tools/libxl/libxl_qmp.c      |  597 ++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 633 insertions(+), 1 deletions(-)
 create mode 100644 tools/libxl/libxl_qmp.c
tools/libxl/Makefile +1 -1
index e50874e..f10c7e8 100644
@@ -37,7 +37,7 @@ 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_json.o \
-			$(LIBXL_OBJS-y)
+			libxl_qmp.o $(LIBXL_OBJS-y)
 LIBXL_OBJS += _libxl_types.o libxl_flask.o _libxl_types_internal.o
 
 $(LIBXL_OBJS): CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_libxenguest) $(CFLAGS_libxenstore) $(CFLAGS_libblktapctl)
tools/libxl/libxl.c +2 -0
index 83d604c..79f9992 100644
@@ -757,6 +757,8 @@ int libxl_domain_destroy(libxl_ctx *ctx, uint32_t domid, int force)
     if (dm_present) {
         if (libxl__destroy_device_model(&gc, domid) < 0)
             LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "libxl__destroy_device_model failed for %d", domid);
+
+        libxl__qmp_cleanup(&gc, domid);
     }
     if (libxl__devices_destroy(&gc, domid, force) < 0)
         LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "libxl_destroy_devices failed for %d", domid);
tools/libxl/libxl_create.c +4 -0
index b74b66f..dd746ff 100644
@@ -520,6 +520,10 @@ static int do_domain_create(libxl__gc *gc, libxl_domain_config *d_config,
     }
 
     if (dm_starting) {
+        if (dm_info->device_model_version
+            == LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN) {
+            libxl__qmp_initializations(ctx, domid);
+        }
         ret = libxl__confirm_device_model_startup(gc, dm_starting);
         if (ret < 0) {
             LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
tools/libxl/libxl_dm.c +10 -0
index b9bf4b0..1a80e7e 100644
@@ -244,6 +244,16 @@ static char ** libxl__build_device_model_args_new(libxl__gc *gc,
     flexarray_vappend(dm_args, dm,
                       "-xen-domid", libxl__sprintf(gc, "%d", info->domid), NULL);
 
+    flexarray_append(dm_args, "-chardev");
+    flexarray_append(dm_args,
+                     libxl__sprintf(gc, "socket,id=libxl-cmd,"
+                                    "path=%s/qmp-libxl-%d,server,nowait",
+                                    libxl_run_dir_path(),
+                                    info->domid));
+
+    flexarray_append(dm_args, "-mon");
+    flexarray_append(dm_args, "chardev=libxl-cmd,mode=control");
+
     if (info->type == LIBXL_DOMAIN_TYPE_PV) {
         flexarray_append(dm_args, "-xen-attach");
     }
tools/libxl/libxl_internal.h +19 -0
index a8aa6d5..413bb36 100644
@@ -382,6 +382,25 @@ _hidden int libxl__e820_alloc(libxl_ctx *ctx, uint32_t domid, libxl_domain_confi
 #define STRINGIFY(x) #x
 #define TOSTRING(x) STRINGIFY(x)
 
+/* from libxl_qmp */
+typedef struct libxl__qmp_handler libxl__qmp_handler;
+
+/* Initialise and connect to the QMP socket.
+ *   Return an handler or NULL if there is an error
+ */
+_hidden libxl__qmp_handler *libxl__qmp_initialize(libxl_ctx *ctx,
+                                                  uint32_t domid);
+/* ask to QEMU the serial port information and store it in xenstore. */
+_hidden int libxl__qmp_query_serial(libxl__qmp_handler *qmp);
+/* close and free the QMP handler */
+_hidden void libxl__qmp_close(libxl__qmp_handler *qmp);
+/* remove the socket file, if the file has already been removed,
+ * nothing happen */
+_hidden void libxl__qmp_cleanup(libxl__gc *gc, uint32_t domid);
+
+/* this helper calls qmp_initialize, query_serial and qmp_close */
+_hidden int libxl__qmp_initializations(libxl_ctx *ctx, uint32_t domid);
+
 /* from libxl_json */
 typedef enum {
     JSON_ERROR,
tools/libxl/libxl_qmp.c new file +597 -0

597 lines not on this page · raw message

Signature · 1 line
Anthony PERARD
#21 lore raw

Re: [PATCH V7 7/7] libxl, Introduce a QMP client

Quoted 9 lines · Anthony PERARD

QMP stands for QEMU Monitor Protocol and it is used to query information from QEMU or to control QEMU.

This implementation will ask QEMU the list of chardevice and store the path to serial0 in xenstored. So we will be able to use xl console with QEMU upstream.

In order to connect to the QMP server, a socket file is created in /var/run/xen/qmp-$(domid).

That path doesn't match the implementation now.

Again I think I've reviewed much of this before so I only skimmed it, although I paid attention to the new stuff relating to pulling through to crlf etc.

Quoted 5 lines
tools/libxl/libxl_qmp.c new file +0 -0
new file mode 100644
index 0000000..90dba72

[...]

Quoted 8 lines
tools/libxl/libxl_qmp.c new file +8 -0
+static int register_serials_chardev_callback(libxl__qmp_handler *qmp,
+                                             const libxl__json_object *o)
+{
+    const libxl__json_object *obj = NULL;
+    const libxl__json_object *label = NULL;
+    const char *s = NULL;
+    flexarray_t *array = NULL;
+    int index = 0;

tools/libxl/libxl_qmp.c line 118

Quoted 10 lines
tools/libxl/libxl_qmp.c new file +10 -0
+    const char *chardev = NULL;
+    int ret = 0;
+
+    if ((array = json_object_get_array(o)) == NULL) {
+        return -1;
+    }
+
+    for (index = 0; index < array->count; index++) {
+        if (flexarray_get(array, index, (void**)&obj) != 0)
+            break;

I think a helper function to retrieve an index into a json_array would be helpful. I think in general exposing the internal use of flexarrays in this interface to callers can be avoided.

tools/libxl/libxl_qmp.c line 120

Quoted 2 lines
tools/libxl/libxl_qmp.c new file +2 -0
+        label = json_object_get("label", obj, JSON_STRING);
+        s = json_object_get_string(label);

If obj is not a map label will be null but json_object_get_string will DTRT and return NULL so this works. But perhaps an explicit type check would be correct here?

Quoted 7 lines
tools/libxl/libxl_qmp.c new file +7 -0
+/*
+ * Helpers
+ */
+
+static libxl__qmp_message_type qmp_response_type(libxl__qmp_handler *qmp,
+                                                 const libxl__json_object *o)
+{

tools/libxl/libxl_qmp.c line 183

Quoted 10 lines
tools/libxl/libxl_qmp.c new file +10 -0
+    flexarray_t *maps = json_object_get_map(o);
+    libxl__qmp_message_type type;
+
+    if (maps) {
+        libxl__json_map_node *node = NULL;
+        int index = 0;
+
+        for (index = 0; index < maps->count; index++) {
+            if (flexarray_get(maps, index, (void**)&node) != 0)
+                break;

Another helper function to get the a node from a map? Or even a json_map_for_each_node type construct?

Quoted 8 lines
tools/libxl/libxl_qmp.c new file +8 -0
+            if (libxl__qmp_message_type_from_string(node->map_key, &type) == 0)
+                return type;
+        }
+    }
+
+    return LIBXL__QMP_MESSAGE_TYPE_INVALID;
+}
+

tools/libxl/libxl_qmp.c line 201

Quoted 10 lines
tools/libxl/libxl_qmp.c new file +10 -0
+static callback_id_pair *qmp_get_callback_from_id(libxl__qmp_handler *qmp,
+                                                  const libxl__json_object *o)
+{
+    const libxl__json_object *id_object = json_object_get("id", o,
+                                                          JSON_INTEGER);
+    int id = -1;
+    callback_id_pair *pp = NULL;
+
+    if (id_object) {
+        id = json_object_get_integer(id_object);

Check that it is an integer? Presumably the -1 error return is never a valid id but it'd save a useless list walk.

Quoted 9 lines
tools/libxl/libxl_qmp.c new file +9 -0
+        SIMPLEQ_FOREACH(pp, &qmp->callback_list, next) {
+            if (pp->id == id) {
+                return pp;
+            }
+        }
+    }
+    return NULL;
+}
+

[...]

tools/libxl/libxl_qmp.c line 278

Quoted 5 lines
tools/libxl/libxl_qmp.c new file +5 -0
+static inline yajl_gen_status libxl__yajl_gen_asciiz(yajl_gen hand,
+                                                     const char *str)
+{
+    return yajl_gen_string(hand, (const unsigned char *)str, strlen(str));
+}

Belongs in libxl_json I think.

Quoted 13 lines
tools/libxl/libxl_qmp.c new file +13 -0
+static int qmp_next(libxl__gc *gc, libxl__qmp_handler *qmp)
+{
+    ssize_t rd;
+    char *s = NULL;
+    char *s_end = NULL;
+
+    char *incomplete = NULL;
+    size_t incomplete_size = 0;
+
+    do {
+        fd_set rfds;
+        int ret = 0;
+        struct timeval timeout = {

tools/libxl/libxl_qmp.c line 372

Quoted 10 lines
tools/libxl/libxl_qmp.c new file +10 -0
+            .tv_sec = qmp->timeout,
+            .tv_usec = 0,
+        };
+
+        FD_ZERO(&rfds);
+        FD_SET(qmp->qmp_fd, &rfds);
+
+        ret = select(qmp->qmp_fd + 1, &rfds, NULL, NULL, &timeout);
+        if (ret > 0) {
+            rd = read(qmp->qmp_fd, qmp->buffer, QMP_RECEIVE_BUFFER_SIZE);

If you do "if (rd == 0) continue" and the rd < 0 error handling+return here the error handling is nearer the error-site and hence easier to grok. (also you save a level of indentation, although the inner loop isn't wrapping in this particular case)

Actually the error handling for ret = select is much the same, if you pull the ret==0 and ret<0 cases up after the select it easier to follow.

Quoted 10 lines
tools/libxl/libxl_qmp.c new file +10 -0
+            if (rd > 0) {
+                DEBUG_REPORT_RECEIVED(qmp->buffer, rd);
+
+                do {
+                    char *end = NULL;
+                    if (incomplete) {
+                        size_t current_pos = s - incomplete;
+                        incomplete_size += rd;
+                        incomplete = libxl__realloc(gc, incomplete, incomplete_size + 1);
+                        incomplete = strncat(incomplete, qmp->buffer, rd);

tools/libxl/libxl_qmp.c line 392

Quoted 10 lines
tools/libxl/libxl_qmp.c new file +10 -0
+                        s = incomplete + current_pos;
+                        s_end = incomplete + incomplete_size;
+                    } else {
+                        incomplete = libxl__strndup(gc, qmp->buffer, rd);
+                        incomplete_size = rd;
+                        s = incomplete;
+                        s_end = s + rd;
+                    }
+
+                    end = strstr(s, "\r\n");

Is s definitely NULL terminated here? (yes, according to strncat(3), good!)

tools/libxl/libxl_qmp.c line 404

Quoted 12 lines
tools/libxl/libxl_qmp.c new file +12 -0
+                    if (end) {
+                        libxl__json_object *o = NULL;
+
+                        *end = '\0';
+
+                        o = libxl__json_parse(gc, s);
+                        s = end + 2;
+
+                        if (o) {
+                            qmp_handle_response(qmp, o);
+                            json_object_free(qmp->ctx, o);
+                        }

"if (!o)" is now an error since you pulled up to a "\r\n"?

Quoted 11 lines
tools/libxl/libxl_qmp.c new file +11 -0
+                    } else {
+                        break;
+                    }
+                } while (s < s_end);
+            } else if (rd < 0) {
+                LIBXL__LOG_ERRNO(qmp->ctx, LIBXL__LOG_ERROR,
+                                 "Socket read error");
+                return rd;
+            }
+        } else if (ret == 0) {
+            LIBXL__LOG(qmp->ctx, LIBXL__LOG_ERROR, "timeout");

tools/libxl/libxl_qmp.c line 425

Quoted 10 lines
tools/libxl/libxl_qmp.c new file +10 -0
+            return -1;
+        } else if (ret < 0) {
+            LIBXL__LOG_ERRNO(qmp->ctx, LIBXL__LOG_ERROR, "Select error");
+            return -1;
+        }
+    } while (s < s_end);
+
+    return 1;
+}
+

[...]

Ian.

#22 lore raw

Re: [PATCH V7 7/7] libxl, Introduce a QMP client

Quoted 37 lines · Ian Campbell
Quoted 9 lines · Anthony PERARD

QMP stands for QEMU Monitor Protocol and it is used to query information from QEMU or to control QEMU.

This implementation will ask QEMU the list of chardevice and store the path to serial0 in xenstored. So we will be able to use xl console with QEMU upstream.

In order to connect to the QMP server, a socket file is created in /var/run/xen/qmp-$(domid).

That path doesn't match the implementation now.

Again I think I've reviewed much of this before so I only skimmed it, although I paid attention to the new stuff relating to pulling through to crlf etc.

Quoted 5 lines
tools/libxl/libxl_qmp.c new file +0 -0
new file mode 100644
index 0000000..90dba72

[...]

Quoted 14 lines
tools/libxl/libxl_qmp.c new file +14 -0
+static int register_serials_chardev_callback(libxl__qmp_handler *qmp,
+                                             const libxl__json_object *o)
+{
+    const libxl__json_object *obj = NULL;
+    const libxl__json_object *label = NULL;
+    const char *s = NULL;
+    flexarray_t *array = NULL;
+    int index = 0;
+    const char *chardev = NULL;
+    int ret = 0;
+
+    if ((array = json_object_get_array(o)) == NULL) {
+        return -1;
+    }
Quoted 8 lines

tools/libxl/libxl_qmp.c line 118

Quoted 4 lines
tools/libxl/libxl_qmp.c new file +4 -0
+
+    for (index = 0; index < array->count; index++) {
+        if (flexarray_get(array, index, (void**)&obj) != 0)
+            break;

I think a helper function to retrieve an index into a json_array would be helpful. I think in general exposing the internal use of flexarrays in this interface to callers can be avoided.

Ok, I will do this helper.

Quoted 6 lines

tools/libxl/libxl_qmp.c line 120

Quoted 2 lines
tools/libxl/libxl_qmp.c new file +2 -0
+        label = json_object_get("label", obj, JSON_STRING);
+        s = json_object_get_string(label);

If obj is not a map label will be null but json_object_get_string will DTRT and return NULL so this works. But perhaps an explicit type check would be correct here?

Ok, I will add this explicit check.

Quoted 34 lines
Quoted 7 lines
tools/libxl/libxl_qmp.c new file +7 -0
+/*
+ * Helpers
+ */
+
+static libxl__qmp_message_type qmp_response_type(libxl__qmp_handler *qmp,
+                                                 const libxl__json_object *o)
+{

tools/libxl/libxl_qmp.c line 183

Quoted 10 lines
tools/libxl/libxl_qmp.c new file +10 -0
+    flexarray_t *maps = json_object_get_map(o);
+    libxl__qmp_message_type type;
+
+    if (maps) {
+        libxl__json_map_node *node = NULL;
+        int index = 0;
+
+        for (index = 0; index < maps->count; index++) {
+            if (flexarray_get(maps, index, (void**)&node) != 0)
+                break;

Another helper function to get the a node from a map? Or even a json_map_for_each_node type construct?

Quoted 13 lines
tools/libxl/libxl_qmp.c new file +13 -0
+            if (libxl__qmp_message_type_from_string(node->map_key, &type) == 0)
+                return type;
+        }
+    }
+
+    return LIBXL__QMP_MESSAGE_TYPE_INVALID;
+}
+
+static callback_id_pair *qmp_get_callback_from_id(libxl__qmp_handler *qmp,
+                                                  const libxl__json_object *o)
+{
+    const libxl__json_object *id_object = json_object_get("id", o,
+                                                          JSON_INTEGER);
Quoted 8 lines

tools/libxl/libxl_qmp.c line 201

Quoted 5 lines
tools/libxl/libxl_qmp.c new file +5 -0
+    int id = -1;
+    callback_id_pair *pp = NULL;
+
+    if (id_object) {
+        id = json_object_get_integer(id_object);

Check that it is an integer? Presumably the -1 error return is never a valid id but it'd save a useless list walk.

Actually, the parametter JSON_INTEGER given to json_object_get ask to explicitly return a json_object with an integer, otherwise, the function return null.

So the get_integer will not return an error.

Quoted 10 lines
Quoted 9 lines
tools/libxl/libxl_qmp.c new file +9 -0
+        SIMPLEQ_FOREACH(pp, &qmp->callback_list, next) {
+            if (pp->id == id) {
+                return pp;
+            }
+        }
+    }
+    return NULL;
+}
+

[...]

Quoted 7 lines

tools/libxl/libxl_qmp.c line 278

Quoted 5 lines
tools/libxl/libxl_qmp.c new file +5 -0
+static inline yajl_gen_status libxl__yajl_gen_asciiz(yajl_gen hand,
+                                                     const char *str)
+{
+    return yajl_gen_string(hand, (const unsigned char *)str, strlen(str));
+}

Belongs in libxl_json I think.

Actually, I have put it here to not expose the yajl_gen.h to all user of libxl_internal.h.

Otherwise, yes, it does not really belong to libxl_qmp.

Quoted 29 lines
Quoted 13 lines
tools/libxl/libxl_qmp.c new file +13 -0
+static int qmp_next(libxl__gc *gc, libxl__qmp_handler *qmp)
+{
+    ssize_t rd;
+    char *s = NULL;
+    char *s_end = NULL;
+
+    char *incomplete = NULL;
+    size_t incomplete_size = 0;
+
+    do {
+        fd_set rfds;
+        int ret = 0;
+        struct timeval timeout = {

tools/libxl/libxl_qmp.c line 372

Quoted 10 lines
tools/libxl/libxl_qmp.c new file +10 -0
+            .tv_sec = qmp->timeout,
+            .tv_usec = 0,
+        };
+
+        FD_ZERO(&rfds);
+        FD_SET(qmp->qmp_fd, &rfds);
+
+        ret = select(qmp->qmp_fd + 1, &rfds, NULL, NULL, &timeout);
+        if (ret > 0) {
+            rd = read(qmp->qmp_fd, qmp->buffer, QMP_RECEIVE_BUFFER_SIZE);

If you do "if (rd == 0) continue" and the rd < 0 error handling+return here the error handling is nearer the error-site and hence easier to grok. (also you save a level of indentation, although the inner loop isn't wrapping in this particular case)

Quoted 2 lines

Actually the error handling for ret = select is much the same, if you pull the ret==0 and ret<0 cases up after the select it easier to follow.

OK.

Quoted 15 lines
Quoted 15 lines
tools/libxl/libxl_qmp.c new file +15 -0
+            if (rd > 0) {
+                DEBUG_REPORT_RECEIVED(qmp->buffer, rd);
+
+                do {
+                    char *end = NULL;
+                    if (incomplete) {
+                        size_t current_pos = s - incomplete;
+                        incomplete_size += rd;
+                        incomplete = libxl__realloc(gc, incomplete, incomplete_size + 1);
+                        incomplete = strncat(incomplete, qmp->buffer, rd);
+                        s = incomplete + current_pos;
+                        s_end = incomplete + incomplete_size;
+                    } else {
+                        incomplete = libxl__strndup(gc, qmp->buffer, rd);
+                        incomplete_size = rd;
Quoted 8 lines

tools/libxl/libxl_qmp.c line 392

Quoted 5 lines
tools/libxl/libxl_qmp.c new file +5 -0
+                        s = incomplete;
+                        s_end = s + rd;
+                    }
+
+                    end = strstr(s, "\r\n");

Is s definitely NULL terminated here? (yes, according to strncat(3), good!)

Exactly :), otherwise a strnstr would be helpfull. (I actually write one before to use strncat.)

Quoted 6 lines
Quoted 6 lines
tools/libxl/libxl_qmp.c new file +6 -0
+                    if (end) {
+                        libxl__json_object *o = NULL;
+
+                        *end = '\0';
+
+                        o = libxl__json_parse(gc, s);
Quoted 8 lines

tools/libxl/libxl_qmp.c line 404

Quoted 6 lines
tools/libxl/libxl_qmp.c new file +6 -0
+                        s = end + 2;
+
+                        if (o) {
+                            qmp_handle_response(qmp, o);
+                            json_object_free(qmp->ctx, o);
+                        }

"if (!o)" is now an error since you pulled up to a "\r\n"?

Indeed, I should add a log_error, even if libxl__json_parse already print something. But I'm not sure if return -1 immediatly is a good idee, maybe the next json object is good.

Quoted 26 lines
Quoted 11 lines
tools/libxl/libxl_qmp.c new file +11 -0
+                    } else {
+                        break;
+                    }
+                } while (s < s_end);
+            } else if (rd < 0) {
+                LIBXL__LOG_ERRNO(qmp->ctx, LIBXL__LOG_ERROR,
+                                 "Socket read error");
+                return rd;
+            }
+        } else if (ret == 0) {
+            LIBXL__LOG(qmp->ctx, LIBXL__LOG_ERROR, "timeout");

tools/libxl/libxl_qmp.c line 425

Quoted 10 lines
tools/libxl/libxl_qmp.c new file +10 -0
+            return -1;
+        } else if (ret < 0) {
+            LIBXL__LOG_ERRNO(qmp->ctx, LIBXL__LOG_ERROR, "Select error");
+            return -1;
+        }
+    } while (s < s_end);
+
+    return 1;
+}
+

[...]

Ian.

Signature · 1 line
Anthony PERARD
#23 lore raw

Re: [PATCH V7 7/7] libxl, Introduce a QMP client

Quoted 17 lines · Anthony PERARD
Quoted 14 lines · Ian Campbell

tools/libxl/libxl_qmp.c line 201

Quoted 10 lines · Anthony PERARD
tools/libxl/libxl_qmp.c new file +10 -0
+static callback_id_pair *qmp_get_callback_from_id(libxl__qmp_handler *qmp,
+                                                  const libxl__json_object *o)
+{
+    const libxl__json_object *id_object = json_object_get("id", o,
+                                                          JSON_INTEGER);
+    int id = -1;
+    callback_id_pair *pp = NULL;
+
+    if (id_object) {
+        id = json_object_get_integer(id_object);

Check that it is an integer? Presumably the -1 error return is never a valid id but it'd save a useless list walk.

Quoted 5 lines

Actually, the parametter JSON_INTEGER given to json_object_get ask to explicitly return a json_object with an integer, otherwise, the function return null.

So the get_integer will not return an error.

Oh right, makes sense.

Quoted 18 lines
Quoted 10 lines
Quoted 9 lines
tools/libxl/libxl_qmp.c new file +9 -0
+        SIMPLEQ_FOREACH(pp, &qmp->callback_list, next) {
+            if (pp->id == id) {
+                return pp;
+            }
+        }
+    }
+    return NULL;
+}
+

[...]

Quoted 7 lines

tools/libxl/libxl_qmp.c line 278

Quoted 5 lines
tools/libxl/libxl_qmp.c new file +5 -0
+static inline yajl_gen_status libxl__yajl_gen_asciiz(yajl_gen hand,
+                                                     const char *str)
+{
+    return yajl_gen_string(hand, (const unsigned char *)str, strlen(str));
+}

Belongs in libxl_json I think.

Quoted 4 lines

Actually, I have put it here to not expose the yajl_gen.h to all user of libxl_internal.h.

Otherwise, yes, it does not really belong to libxl_qmp.

I wanted it there because my json generating patch also needs it ;-)

I think exposing yajl_gen.h to libxl_internal is ok, it's libxl.h where we should avoid it.

I can patch this up in my series which uses JSON though so no worries.

Ian.

#24 lore raw

v7 cover Introduce a QMP client

In reply to Anthony PERARD

New:
- New patch that add set_default_namespaces to libxltypes.py, so we can set a namespace to use for a specific .idl.
- New function: libxl__realloc and libxl__strndup.

Anthony PERARD (7):
libxl: Rename libxl.idl to libxl_types.idl.
libxl: Add get/set_default_namespace in libxltypes.py.
libxl: Introduce libxl_internal_types.idl. libxl, Introduce libxl__realloc.
libxl, Intruduce libxl__strndup.
libxl: Introduce JSON parsing stuff.
libxl, Introduce a QMP client

 tools/libxl/Makefile                       |   21 +-
 tools/libxl/gentypes.py                    |   15 +-
 tools/libxl/libxl.c                        |    2 +
 tools/libxl/libxl_create.c                 |    4 +
 tools/libxl/libxl_dm.c                     |   10 +
 tools/libxl/libxl_internal.c               |   30 ++
 tools/libxl/libxl_internal.h               |  117 ++++++
 tools/libxl/libxl_json.c                   |  505 +++++++++++++++++++++++
 tools/libxl/libxl_qmp.c                    |  597 ++++++++++++++++++++++++++++
 tools/libxl/{libxl.idl => libxl_types.idl} |    2 +
 tools/libxl/libxl_types_internal.idl       |    9 +
 tools/libxl/libxltypes.py                  |   16 +-
 tools/ocaml/libs/xl/Makefile               |    4 +-
 tools/python/Makefile                      |    4 +-
 14 files changed, 1315 insertions(+), 21 deletions(-)
 create mode 100644 tools/libxl/libxl_json.c
 create mode 100644 tools/libxl/libxl_qmp.c
 rename tools/libxl/{libxl.idl => libxl_types.idl} (99%)
 create mode 100644 tools/libxl/libxl_types_internal.idl
Signature · 1 line
Anthony PERARD