Skip to contentSkip to conversation

Allow to build libxl and other tools with json-c instead of yajl

PATCH Superseded Anthony PERARD 4 participants 34 messages Aug 8, 2025 – Sep 29, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Allow to build libxl and other tools with json-c instead of yajl

From: Anthony PERARD <[email protected]>

Patch series available in this git branch:
https://xenbits.xenproject.org/git-http/people/aperard/xen-unstable.git br.libxl-libjsonc-v1

Hi,

The library YAJL has been unmaintained for several years, without an obvious fork to pick.

On the other and the library json-c is been maintained and use by several other project, it's probably already installed on your machine. So this patch series intend to allow to build the Xen toolstack again json-c, and forgo yajl.

Just in case, YAJL is can still be used.

There's bit of libxl API that exposes YAJL, mainly so it can be used by xl to call libxl_domain_config_gen_json(). It was exposed via the "libxl_json.h" headers. This functions and others won't be available when libxl is build against json-c.

Cheers,

Anthony PERARD (11):
xl: move printf_info prototype to an header
libxl: Remove duplicate libxl_domain_config_gen_json prototype
libxl: remove duplicated libxl__yajl_gen_asciiz() prototype tools/configure: Introduce deps on json-c lib for libxl
libxl: Convert libxl__json_parse() to use json-c
libxl: convert libxl__json_object_to_yajl_gen to
libxl__json_object_to_libjsonc_object
libxl: libxl__object_to_json() to json-c
libxl: convert libxl__json_object_to_json() to json_object tools/libxenstat: Use json-c when available
configure: Use json-c by default, fallback to yajl Update CHANGELOG and README with dependency on json-c

 CHANGELOG.md                              |   2 +
 README                                    |   2 +-
 config/Tools.mk.in                        |   2 +
 tools/config.h.in                         |   3 +
 tools/configure                           | 136 +++++-
 tools/configure.ac                        |  10 +-
 tools/include/libxl_json.h                |  30 +-
 tools/libs/light/Makefile                 |   7 +-
 tools/libs/light/gentypes.py              | 160 ++++++-
 tools/libs/light/idl.py                   |   7 +-
 tools/libs/light/libxl_cpuid.c            | 119 +++++
 tools/libs/light/libxl_internal.h         |  25 +-
 tools/libs/light/libxl_json.c             | 557 +++++++++++++++++++++-
 tools/libs/light/libxl_qmp.c              |  53 ++
 tools/libs/light/libxl_types.idl          |   7 +-
 tools/libs/light/libxl_types_internal.idl |   3 +-
 tools/libs/stat/Makefile                  |   3 +-
 tools/libs/stat/xenstat_qmp.c             | 126 ++++-
 tools/xl/Makefile                         |   2 +-
 tools/xl/xl.h                             |   3 +
 tools/xl/xl_info.c                        | 105 +++-
 tools/xl/xl_misc.c                        |   3 -
 22 files changed, 1313 insertions(+), 52 deletions(-)
Signature · 1 line
Anthony PERARD
#1 lore raw

PATCH 1/11 xl: move printf_info prototype to an header

From: Anthony PERARD <[email protected]>

In a single place.

---
 tools/xl/xl.h      | 3 +++
 tools/xl/xl_info.c | 3 ---
 tools/xl/xl_misc.c | 3 ---
 3 files changed, 3 insertions(+), 6 deletions(-)
tools/xl/xl.h +3 -0
index 45745f0dbb..9000df00de 100644
@@ -310,6 +310,9 @@ extern void printf_info_sexp(int domid, libxl_domain_config *d_config, FILE *fh)
 extern void apply_global_affinity_masks(libxl_domain_type type,
                                         libxl_bitmap *vcpu_affinity_array,
                                         unsigned int size);
+void printf_info(enum output_format output_format,
+                 int domid,
+                 libxl_domain_config *d_config, FILE *fh);
 
 #define XL_GLOBAL_CONFIG XEN_CONFIG_DIR "/xl.conf"
 #define XL_LOCK_FILE XEN_LOCK_DIR "/xl"
tools/xl/xl_info.c +0 -3
index eb019e3ee9..3fbc0698b7 100644
@@ -96,9 +96,6 @@ static yajl_gen_status printf_info_one_json(yajl_gen hand, int domid,
     return s;
 }
 
-void printf_info(enum output_format output_format,
-                 int domid,
-                 libxl_domain_config *d_config, FILE *fh);
 void printf_info(enum output_format output_format,
                  int domid,
                  libxl_domain_config *d_config, FILE *fh)
tools/xl/xl_misc.c +0 -3
index 08f0fb6dc9..f0167e1603 100644
@@ -256,9 +256,6 @@ int main_dump_core(int argc, char **argv)
     return EXIT_SUCCESS;
 }
 
-extern void printf_info(enum output_format output_format,
-                        int domid,
-                        libxl_domain_config *d_config, FILE *fh);
 int main_config_update(int argc, char **argv)
 {
     uint32_t domid;
Signature · 1 line
Anthony PERARD
#3 lore raw

PATCH 2/11 libxl: Remove duplicate libxl_domain_config_gen_json prototype

In reply to Anthony PERARD

From: Anthony PERARD <[email protected]>

Since f9e681d3d1b8, the prototype of libxl_domain_config_gen_json is generated from IDL in _libxl_types_json.h.

Fixes f9e681d3d1b8 ("libxl: move definition of libxl_domain_config into the IDL")
---
 tools/include/libxl_json.h | 3 ---
 1 file changed, 3 deletions(-)
tools/include/libxl_json.h +0 -3
index 260783bfde..3f97267eae 100644
@@ -90,7 +90,4 @@ static inline yajl_gen libxl_yajl_gen_alloc(const yajl_alloc_funcs *allocFuncs)
 
 #endif /* !HAVE_YAJL_V2 */
 
-yajl_gen_status libxl_domain_config_gen_json(yajl_gen hand,
-                                             libxl_domain_config *p);
-
 #endif /* LIBXL_JSON_H */
Signature · 1 line
Anthony PERARD
Reviewed-by on v1 2/11
#4 lore raw

Re: [XEN PATCH 02/11] libxl: Remove duplicate libxl_domain_config_gen_json prototype

Quoted 7 lines · Anthony PERARD

From: Anthony PERARD <[email protected]>

Since f9e681d3d1b8, the prototype of libxl_domain_config_gen_json is generated from IDL in _libxl_types_json.h.

Fixes f9e681d3d1b8 ("libxl: move definition of libxl_domain_config into the IDL")
#5 lore raw

PATCH 3/11 libxl: remove duplicated libxl__yajl_gen_asciiz() prototype

In reply to Anthony PERARD

From: Anthony PERARD <[email protected]>

---
 tools/libs/light/libxl_internal.h | 2 --
 1 file changed, 2 deletions(-)
tools/libs/light/libxl_internal.h +0 -2
index 2a5b233caa..4b6587a27a 100644
@@ -1993,8 +1993,6 @@ _hidden char *libxl__cpupoolid_to_name(libxl__gc *gc, uint32_t poolid);
 _hidden int libxl__enum_from_string(const libxl_enum_string_table *t,
                                     const char *s, int *e) NN(2);
 
-_hidden yajl_gen_status libxl__yajl_gen_asciiz(yajl_gen hand, const char *str);
-
 _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 *);
Signature · 1 line
Anthony PERARD
#7 lore raw

PATCH 4/11 tools/configure: Introduce deps on json-c lib for libxl

In reply to Anthony PERARD

From: Anthony PERARD <[email protected]>

To replace yajl.

Introduce YAJL_LIBS variable, to be able to remove "-lyajl" later.

---
 config/Tools.mk.in        |   2 +
 tools/config.h.in         |   3 ++
 tools/configure           | 107 +++++++++++++++++++++++++++++++++++++-
 tools/configure.ac        |   6 ++-
 tools/libs/light/Makefile |   5 +-
 tools/xl/Makefile         |   2 +-
 6 files changed, 119 insertions(+), 6 deletions(-)
config/Tools.mk.in +2 -0
index e47ac23d11..c2201021d1 100644
@@ -65,6 +65,8 @@ EXTFS_LIBS          := @EXTFS_LIBS@
 CURSES_LIBS         := @CURSES_LIBS@
 TINFO_LIBS          := @TINFO_LIBS@
 ARGP_LDFLAGS        := @argp_ldflags@
+LIBJSONC_LIBS       := @libjsonc_LIBS@
+YAJL_LIBS           := @YAJL_LIBS@
 
 FILE_OFFSET_BITS    := @FILE_OFFSET_BITS@
 
tools/config.h.in +3 -0
index fe2a94cfc4..ed0042018d 100644
@@ -27,6 +27,9 @@
 /* Define to 1 if you have the `fdt' library (-lfdt). */
 #undef HAVE_LIBFDT
 
+/* Use library json-c */
+#undef HAVE_LIBJSONC
+
 /* Define to 1 if you have the `lzma' library (-llzma). */
 #undef HAVE_LIBLZMA
 
tools/configure +105 -2

138 lines not on this page · raw message

tools/configure.ac +5 -1
index dada1c3b15..bb40b5b3f0 100644
@@ -424,7 +424,11 @@ AC_SUBST([ZLIB_CFLAGS])
 AC_SUBST([ZLIB_LIBS])
 AX_CHECK_EXTFS
 AX_CHECK_PTHREAD
-AC_CHECK_LIB([yajl], [yajl_alloc], [],
+PKG_CHECK_MODULES([libjsonc], [json-c],
+    [AC_DEFINE([HAVE_LIBJSONC], [1], [Use library json-c])])
+AC_CHECK_LIB([yajl], [yajl_alloc],
+   [AC_SUBST([YAJL_LIBS],[-lyajl])
+    AC_DEFINE([HAVE_LIBYAJL],[1],[Define to 1 if you have the `yajl' library (-lyajl).])],
     [AC_MSG_ERROR([Could not find yajl])])
 AC_CHECK_LIB([z], [deflateCopy], [], [AC_MSG_ERROR([Could not find zlib])])
 AC_CHECK_HEADER([argp.h], [
tools/libs/light/Makefile +3 -2
index b690d92159..fa2a338c2b 100644
@@ -166,7 +166,8 @@ LDLIBS-$(CONFIG_Linux) += -luuid
 LDLIBS-$(CONFIG_Linux) += -lrt
 LDLIBS-$(CONFIG_ARM) += -lfdt
 LDLIBS-y += $(PTHREAD_LIBS)
-LDLIBS-y += -lyajl
+LDLIBS-y += $(YAJL_LIBS)
+LDLIBS-y += $(LIBJSONC_LIBS)
 LDLIBS += $(LDLIBS-y)
 
 $(OBJS-y) $(PIC_OBJS) $(LIBXL_TEST_OBJS): CFLAGS += $(CFLAGS_LIBXL) -include $(XEN_ROOT)/tools/config.h
@@ -246,7 +247,7 @@ libxenlight_test.so: $(PIC_OBJS) $(LIBXL_TEST_OBJS)
 	$(CC) $(LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenlight.so.$(MAJOR) $(SHLIB_LDFLAGS) -o $@ $^ $(LDLIBS) $(APPEND_LDFLAGS)
 
 test_%: test_%.o test_common.o libxenlight_test.so
-	$(CC) $(LDFLAGS) -o $@ $^ $(filter-out %libxenlight.so, $(LDLIBS_libxenlight)) $(LDLIBS_libxentoollog) $(LDLIBS_libxentoolcore) -lyajl $(APPEND_LDFLAGS)
+	$(CC) $(LDFLAGS) -o $@ $^ $(filter-out %libxenlight.so, $(LDLIBS_libxenlight)) $(LDLIBS_libxentoollog) $(LDLIBS_libxentoolcore) $(YAJL_LIBS) $(LIBJSONC_LIBS) $(APPEND_LDFLAGS)
 
 libxl-save-helper: $(SAVE_HELPER_OBJS) libxenlight.so
 	$(CC) $(LDFLAGS) -o $@ $(SAVE_HELPER_OBJS) $(LDLIBS_libxentoollog) $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxentoolcore) $(APPEND_LDFLAGS)
tools/xl/Makefile +1 -1
index ad577cdd70..ff7a5aee94 100644
@@ -33,7 +33,7 @@ $(XL_OBJS): CFLAGS += -include $(XEN_ROOT)/tools/config.h # libxl_json.h needs i
 all: xl
 
 xl: $(XL_OBJS)
-	$(CC) $(LDFLAGS) -o $@ $(XL_OBJS) $(LDLIBS_libxenutil) $(LDLIBS_libxenlight) $(LDLIBS_libxentoollog) $(LDLIBS_libxenstore) -lyajl $(APPEND_LDFLAGS)
+	$(CC) $(LDFLAGS) -o $@ $(XL_OBJS) $(LDLIBS_libxenutil) $(LDLIBS_libxenlight) $(LDLIBS_libxentoollog) $(LDLIBS_libxenstore) $(YAJL_LIBS) $(LIBJSONC_LIBS) $(APPEND_LDFLAGS)
 
 .PHONY: install
 install: all
Signature · 1 line
Anthony PERARD
#8 lore raw

Re: [XEN PATCH 04/11] tools/configure: Introduce deps on json-c lib for libxl

Quoted 28 lines · Anthony PERARD
tools/libs/light/Makefile +3 -2
index b690d92159..fa2a338c2b 100644
@@ -166,7 +166,8 @@ LDLIBS-$(CONFIG_Linux) += -luuid
 LDLIBS-$(CONFIG_Linux) += -lrt
 LDLIBS-$(CONFIG_ARM) += -lfdt
 LDLIBS-y += $(PTHREAD_LIBS)
-LDLIBS-y += -lyajl
+LDLIBS-y += $(YAJL_LIBS)
+LDLIBS-y += $(LIBJSONC_LIBS)
 LDLIBS += $(LDLIBS-y)
 
 $(OBJS-y) $(PIC_OBJS) $(LIBXL_TEST_OBJS): CFLAGS += $(CFLAGS_LIBXL) -include $(XEN_ROOT)/tools/config.h
@@ -246,7 +247,7 @@ libxenlight_test.so: $(PIC_OBJS) $(LIBXL_TEST_OBJS)
 	$(CC) $(LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenlight.so.$(MAJOR) $(SHLIB_LDFLAGS) -o $@ $^ $(LDLIBS) $(APPEND_LDFLAGS)
 
 test_%: test_%.o test_common.o libxenlight_test.so
-	$(CC) $(LDFLAGS) -o $@ $^ $(filter-out %libxenlight.so, $(LDLIBS_libxenlight)) $(LDLIBS_libxentoollog) $(LDLIBS_libxentoolcore) -lyajl $(APPEND_LDFLAGS)
+	$(CC) $(LDFLAGS) -o $@ $^ $(filter-out %libxenlight.so, $(LDLIBS_libxenlight)) $(LDLIBS_libxentoollog) $(LDLIBS_libxentoolcore) $(YAJL_LIBS) $(LIBJSONC_LIBS) $(APPEND_LDFLAGS)
 
 libxl-save-helper: $(SAVE_HELPER_OBJS) libxenlight.so
 	$(CC) $(LDFLAGS) -o $@ $(SAVE_HELPER_OBJS) $(LDLIBS_libxentoollog) $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxentoolcore) $(APPEND_LDFLAGS)
tools/xl/Makefile +0 -0
index ad577cdd70..ff7a5aee94 100644

tools/xl/Makefile $(XL_OBJS): CFLAGS += -include $(XEN_ROOT)/tools/config.h # libxl_json.h needs i · line 39

Quoted 8 lines
tools/xl/Makefile +1 -1
@@ -33,7 +33,7 @@ $(XL_OBJS): CFLAGS += -include $(XEN_ROOT)/tools/config.h # libxl_json.h needs i
 all: xl
 
 xl: $(XL_OBJS)
-	$(CC) $(LDFLAGS) -o $@ $(XL_OBJS) $(LDLIBS_libxenutil) $(LDLIBS_libxenlight) $(LDLIBS_libxentoollog) $(LDLIBS_libxenstore) -lyajl $(APPEND_LDFLAGS)
+	$(CC) $(LDFLAGS) -o $@ $(XL_OBJS) $(LDLIBS_libxenutil) $(LDLIBS_libxenlight) $(LDLIBS_libxentoollog) $(LDLIBS_libxenstore) $(YAJL_LIBS) $(LIBJSONC_LIBS) $(APPEND_LDFLAGS)
 
 .PHONY: install
 install: all

Specifying both isn't entirely great when one is supposed to be empty.

Could we not have XEN_JSON_LIB(s) which is set to either $YAJL_LIBS or $LIBJSONC_LIBS as appropriate?

~Andrew

#9 lore raw

Re: [XEN PATCH 04/11] tools/configure: Introduce deps on json-c lib for libxl

Quoted 5 lines · Andrew Cooper
Quoted 4 lines · Anthony PERARD

xl: $(XL_OBJS)

tools/xl/Makefile +1 -1
-	$(CC) $(LDFLAGS) -o $@ $(XL_OBJS) $(LDLIBS_libxenutil) $(LDLIBS_libxenlight) $(LDLIBS_libxentoollog) $(LDLIBS_libxenstore) -lyajl $(APPEND_LDFLAGS)
+	$(CC) $(LDFLAGS) -o $@ $(XL_OBJS) $(LDLIBS_libxenutil) $(LDLIBS_libxenlight) $(LDLIBS_libxentoollog) $(LDLIBS_libxenstore) $(YAJL_LIBS) $(LIBJSONC_LIBS) $(APPEND_LDFLAGS)
Quoted 7 lines
Quoted 2 lines

.PHONY: install
install: all

Specifying both isn't entirely great when one is supposed to be empty.

Could we not have XEN_JSON_LIB(s) which is set to either $YAJL_LIBS or $LIBJSONC_LIBS as appropriate?

Yes, that sounds good.

Thanks,

Signature · 1 line
Anthony PERARD
#10 lore raw

Re: [XEN PATCH 04/11] tools/configure: Introduce deps on json-c lib for libxl

In reply to Anthony PERARD
Quoted 7 lines · Anthony PERARD

From: Anthony PERARD <[email protected]>

To replace yajl.

Introduce YAJL_LIBS variable, to be able to remove "-lyajl" later.

Should the two configure changes be merged and placed at the end of the series? That avoids a half implemented conversion in between.

I think Andrew's XEN_JSON_LIBS suggestion is a good one.

Regards,
Jason

#11 lore raw

Re: [XEN PATCH 04/11] tools/configure: Introduce deps on json-c lib for libxl

Quoted 11 lines · Jason Andryuk
Quoted 7 lines · Anthony PERARD

From: Anthony PERARD <[email protected]>

To replace yajl.

Introduce YAJL_LIBS variable, to be able to remove "-lyajl" later.

Should the two configure changes be merged and placed at the end of the series? That avoids a half implemented conversion in between.

It might be half-converted, but it's fully working. Every single patch introduce changes that can be use right away. I've actually make use of this possibility. On every single patch, we can run ./testidl, which will exercise both lib against each other.

./testidl does LibxlObj-to-JSONstring and back and forth. So on the next commit, we would have JSON generated with json-c, which is then parsed with YAJL, to finally be generated to JSON with json-c again, then both JSON output are compared.

I've actually prepared the patch "libxl: Convert libxl__json_parse() to use json-c' last, then put it in front and check that ./testidl was still working.

This help me be confident enough that the conversion to json-c was correct.

So if we squash both config change, every patch until the config change would only introduce dead code.

Cheers,

Signature · 1 line
Anthony PERARD
#12 lore raw

Re: [XEN PATCH 04/11] tools/configure: Introduce deps on json-c lib for libxl

Quoted 27 lines · Anthony PERARD
Quoted 11 lines · Jason Andryuk
Quoted 7 lines · Anthony PERARD

From: Anthony PERARD <[email protected]>

To replace yajl.

Introduce YAJL_LIBS variable, to be able to remove "-lyajl" later.

Should the two configure changes be merged and placed at the end of the series? That avoids a half implemented conversion in between.

It might be half-converted, but it's fully working. Every single patch introduce changes that can be use right away. I've actually make use of this possibility. On every single patch, we can run ./testidl, which will exercise both lib against each other.

./testidl does LibxlObj-to-JSONstring and back and forth. So on the next commit, we would have JSON generated with json-c, which is then parsed with YAJL, to finally be generated to JSON with json-c again, then both JSON output are compared.

I've actually prepared the patch "libxl: Convert libxl__json_parse() to use json-c' last, then put it in front and check that ./testidl was still working.

Quoted 5 lines

This help me be confident enough that the conversion to json-c was correct.

So if we squash both config change, every patch until the config change would only introduce dead code.

Oh, I didn't realize that. Yes, your approach sounds great.

Thanks,
Jason

#13 lore raw

PATCH 5/11 libxl: Convert libxl__json_parse() to use json-c

In reply to Anthony PERARD

From: Anthony PERARD <[email protected]>

This reuse the "json_callback_*" implemented for the yajl parser as they don't really need to be changed. It's just awkward to have to cast between unsigned char and char.

Replace few strncpy() by memcpy() to let the compiler know we want to copy the string without the terminating nul, as we are adding it just after.

Also, it should be possible to keep using YAJL parser when json-c library isn't available.

---
 tools/include/libxl_json.h    |   4 ++
 tools/libs/light/libxl_json.c | 120 ++++++++++++++++++++++++++++++++--
 2 files changed, 120 insertions(+), 4 deletions(-)
tools/include/libxl_json.h +4 -0
index 3f97267eae..f0b4871e0e 100644
@@ -42,6 +42,7 @@ yajl_gen_status libxl_ms_vm_genid_gen_json(yajl_gen hand, libxl_ms_vm_genid *p);
 #  define HAVE_YAJL_V2 1
 #endif
 
+#ifdef HAVE_LIBYAJL
 #ifdef HAVE_YAJL_V2
 
 typedef size_t libxl_yajl_length;
@@ -89,5 +90,8 @@ static inline yajl_gen libxl_yajl_gen_alloc(const yajl_alloc_funcs *allocFuncs)
 }
 
 #endif /* !HAVE_YAJL_V2 */
+#else
+typedef size_t libxl_yajl_length;
+#endif /* !HAVE_LIBYAJL */
 
 #endif /* LIBXL_JSON_H */
tools/libs/light/libxl_json.c +116 -4

204 lines not on this page · raw message

Signature · 1 line
Anthony PERARD
Reviewed-by on v1 5/11
#14 lore raw

Re: [XEN PATCH 05/11] libxl: Convert libxl__json_parse() to use json-c

Quoted 6 lines · Anthony PERARD

From: Anthony PERARD <[email protected]>

This reuse the "json_callback_*" implemented for the yajl parser as they don't really need to be changed. It's just awkward to have to cast between unsigned char and char.

Quoted 8 lines

Replace few strncpy() by memcpy() to let the compiler know we want to copy the string without the terminating nul, as we are adding it just after.

Also, it should be possible to keep using YAJL parser when json-c library isn't available.

#15 lore raw

PATCH 6/11 libxl: convert libxl__json_object_to_yajl_gen to libxl__json_object_to_libjsonc_object

In reply to Anthony PERARD

From: Anthony PERARD <[email protected]>

Convert yajl_gen to json_object from lib json-c.

And make use of it in qmp_prepare_cmd(), which can be compiled with either lib.

---
 tools/include/libxl_json.h        |  6 ++
 tools/libs/light/libxl_internal.h |  7 +++
 tools/libs/light/libxl_json.c     | 95 +++++++++++++++++++++++++++++++
 tools/libs/light/libxl_qmp.c      | 53 +++++++++++++++++
 4 files changed, 161 insertions(+)
tools/include/libxl_json.h +6 -0
index f0b4871e0e..e2ef8151f0 100644
@@ -15,12 +15,18 @@
 #ifndef LIBXL_JSON_H
 #define LIBXL_JSON_H
 
+#ifdef HAVE_LIBJSONC
+#include <json-c/json.h>
+#endif
+
+#ifdef HAVE_LIBYAJL
 #include <yajl/yajl_gen.h>
 #include <yajl/yajl_parse.h>
 
 #ifdef HAVE_YAJL_YAJL_VERSION_H
 #  include <yajl/yajl_version.h>
 #endif
+#endif
 
 yajl_gen_status libxl__uint64_gen_json(yajl_gen hand, uint64_t val);
 yajl_gen_status libxl_defbool_gen_json(yajl_gen hand, libxl_defbool *p);
tools/libs/light/libxl_internal.h +7 -0
index 4b6587a27a..b66aaa779d 100644
@@ -2234,9 +2234,16 @@ _hidden const libxl__json_object *libxl__json_map_get(const char *key,
  */
 _hidden libxl__json_object *libxl__json_object_alloc(libxl__gc *gc_opt,
                                                      libxl__json_node_type type);
+#ifdef HAVE_LIBJSONC
+_hidden int libxl__json_object_to_json_object(libxl__gc *gc,
+                                              json_object **jso_out,
+                                              const libxl__json_object *obj);
+#endif
+#ifdef HAVE_LIBYAJL
 _hidden yajl_status libxl__json_object_to_yajl_gen(libxl__gc *gc_opt,
                                                    yajl_gen hand,
                                                    const libxl__json_object *param);
+#endif
 _hidden void libxl__json_object_free(libxl__gc *gc_opt,
                                      libxl__json_object *obj);
 
tools/libs/light/libxl_json.c +95 -0

107 lines not on this page · raw message

tools/libs/light/libxl_qmp.c +53 -0

82 lines not on this page · raw message

Signature · 1 line
Anthony PERARD
Reviewed-by on v1 6/11
#16 lore raw

Re: [XEN PATCH 06/11] libxl: convert libxl__json_object_to_yajl_gen to libxl__json_object_to_libjsonc_object

Quoted 37 lines · Anthony PERARD

From: Anthony PERARD <[email protected]>

Convert yajl_gen to json_object from lib json-c.

And make use of it in qmp_prepare_cmd(), which can be compiled with either lib.

---

tools/libs/light/libxl_json.c +19 -0
index 44ee6e213f..b26ac901d6 100644
@@ -631,6 +631,100 @@ const libxl__json_object *libxl__json_map_get(const char *key,
      return NULL;
  }
 
+#ifdef HAVE_LIBJSONC
+int libxl__json_object_to_json_object(libxl__gc *gc,
+                                      json_object **jso_out,
+                                      const libxl__json_object *obj)
+{
+    int idx = 0;
+    int rc, r;
+
+    switch (obj->type) {
+    case JSON_NULL:
+        *jso_out = json_object_new_null();
+        return 0;
+    case JSON_BOOL:
+        *jso_out = json_object_new_boolean(obj->u.b);
+        if (!*jso_out)
+            return ERROR_NOMEM;
+        return 0;
+    case JSON_INTEGER:
+        *jso_out = json_object_new_int64(obj->u.i);

tools/libs/light/libxl_json.c const libxl__json_object *libxl__json_map_get(const char *key, · line 662

Quoted 10 lines
tools/libs/light/libxl_json.c +10 -0
+        if (!*jso_out)
+            return ERROR_NOMEM;
+        return 0;
+    case JSON_DOUBLE:
+        *jso_out = json_object_new_double(obj->u.d);
+        if (!*jso_out)
+            return ERROR_NOMEM;
+        return 0;
+    case JSON_NUMBER:
+        *jso_out = json_object_new_string(obj->u.string);

Is JSON_NUMBER calling json_object_new_string() correct? It looks like the yajl code falls back to a string, so that is okay but surprising.

So I just want to double check. If so:

Thanks,
Jason

Quoted 3 lines
tools/libs/light/libxl_json.c +3 -0
+        if (!*jso_out)
+            return ERROR_NOMEM;
+        return 0;
#17 lore raw

Re: [XEN PATCH 06/11] libxl: convert libxl__json_object_to_yajl_gen to libxl__json_object_to_libjsonc_object

Quoted 6 lines · Jason Andryuk

Review

Quoted 2 lines · Anthony PERARD
(quoted diff) +2 -0
+    case JSON_NUMBER:
+        *jso_out = json_object_new_string(obj->u.string);

Is JSON_NUMBER calling json_object_new_string() correct? It looks like the yajl code falls back to a string, so that is okay but surprising.

Yeah, I think that's correct.
:-( maybe not. Even if we have these too comments:

    In libxl_internal.h, enum libxl__json_node_type:
        /* number is store in string, it's too big to be a long long or a double */
        JSON_NUMBER  = (1 << 4),

    In json_callback_number():
        /* If the conversion fail, we just store the original string. */

With yajl, we call yajl_gen_number(), which probably write 2^128 as:

    340282366920938463463374607431768211456

but this new json-c generator would write instead:

    "340282366920938463463374607431768211456"

I guess we might be able to replicate the same behavior by using json_object_set_serializer() or json_object_new_double_s() (which use the former). But I don't know if it is worth the effort. I hope we won't have int bigger than 64 bits.

And there's probably no tests for JSON_NUMBERs. So I guess first step would be to write a test that have numbers that can't be converted to long long and see what happens.

Thanks,

Signature · 1 line
Anthony PERARD
#18 lore raw

Re: [XEN PATCH 06/11] libxl: convert libxl__json_object_to_yajl_gen to libxl__json_object_to_libjsonc_object

Quoted 22 lines · Anthony PERARD
Quoted 6 lines · Jason Andryuk

Review

Quoted 2 lines · Anthony PERARD
(quoted diff) +2 -0
+    case JSON_NUMBER:
+        *jso_out = json_object_new_string(obj->u.string);

Is JSON_NUMBER calling json_object_new_string() correct? It looks like the yajl code falls back to a string, so that is okay but surprising.

Yeah, I think that's correct.
:-( maybe not. Even if we have these too comments:

     In libxl_internal.h, enum libxl__json_node_type:
         /* number is store in string, it's too big to be a long long or a double */
         JSON_NUMBER  = (1 << 4),

     In json_callback_number():
         /* If the conversion fail, we just store the original string. */

With yajl, we call yajl_gen_number(), which probably write 2^128 as:

     340282366920938463463374607431768211456
Quoted 8 lines

but this new json-c generator would write instead:

     "340282366920938463463374607431768211456"

I guess we might be able to replicate the same behavior by using json_object_set_serializer() or json_object_new_double_s() (which use the former). But I don't know if it is worth the effort. I hope we won't have int bigger than 64 bits.

I didn't check, but I thought uint64_t is the biggest size libxl uses.

Regards,
Jason

#19 lore raw

Re: [XEN PATCH 06/11] libxl: convert libxl__json_object_to_yajl_gen to libxl__json_object_to_libjsonc_object

Quoted 25 lines · Jason Andryuk
Quoted 24 lines · Anthony PERARD
Quoted 6 lines · Jason Andryuk

Review

Quoted 2 lines · Anthony PERARD
(quoted diff) +2 -0
+    case JSON_NUMBER:
+        *jso_out = json_object_new_string(obj->u.string);

Is JSON_NUMBER calling json_object_new_string() correct? It looks like the yajl code falls back to a string, so that is okay but surprising.

Yeah, I think that's correct.
:-( maybe not. Even if we have these too comments:

     In libxl_internal.h, enum libxl__json_node_type:
         /* number is store in string, it's too big to be a long long or a double */
         JSON_NUMBER  = (1 << 4),

     In json_callback_number():
         /* If the conversion fail, we just store the original string. */

With yajl, we call yajl_gen_number(), which probably write 2^128 as:

     340282366920938463463374607431768211456

but this new json-c generator would write instead:

Quoted 8 lines
Quoted 6 lines
     "340282366920938463463374607431768211456"

I guess we might be able to replicate the same behavior by using json_object_set_serializer() or json_object_new_double_s() (which use the former). But I don't know if it is worth the effort. I hope we won't have int bigger than 64 bits.

I didn't check, but I thought uint64_t is the biggest size libxl uses.

Yes, but we also parse json that are produce else where. (could be file saved by libxl, but also json produced by QEMU)

Anywhy, I investitaged this a bit, and it's very unlikely that a JSON_NUMBER would be created, so it's even less likely that this case would happen. But I've change the call to json_object_new_string by:

/*
* Use json_object_new_double_s() to rewrite the number exactly as
* we parsed it. When generating the JSON string the value 0 will
* be ignored and obj->u.string will be written instead. */
*jso_out = json_object_new_double_s(0, obj->u.string);

That way, we get the same json blob, with both library.

Cheers,

Signature · 1 line
Anthony PERARD
#20 lore raw

PATCH 7/11 libxl: libxl__object_to_json() to json-c

In reply to Anthony PERARD

From: Anthony PERARD <[email protected]>

- libxl changes:

While doing so, we rename all "*_gen_json" function to "*_gen_jso" as they have different prototype. All the function pointer are been cast to (libxl__gen_json_callback) by "gentypes.py" when generating "*_to_json()" functions.

We also introduce a few more "*_gen_jso" functions for "int" and "bool" because we can't use json_object_*() functions from json-c directly like it's done with yajl.

To make the generation of _libxl_types*json.[ch] with both YAJL and json-c we add "--libjsonc" to gentypes.py so it can generate functions/types for both.

Also introducing "jsonc_json_gen_fn" in the IDL, to be able to point to a different function when using json-c.

Also, don't export any of the new *_gen_jso() function, at the cost of having "_hidden" macro in semi-public headers.

- xl changes:

Also, rework the implementation of printf_info() in xl to avoid using libxl_domain_config_gen_json() which isn't available without YAJL. The implementation using "json_object" call libxl_domain_config_to_json() which generate a plain string of JSON, which we parse to add it to our own json; this avoid a dependency on the json library used by libxl.

---
 tools/include/libxl_json.h                |  17 ++
 tools/libs/light/Makefile                 |   2 +-
 tools/libs/light/gentypes.py              | 160 ++++++++++-
 tools/libs/light/idl.py                   |   7 +-
 tools/libs/light/libxl_cpuid.c            | 119 ++++++++
 tools/libs/light/libxl_internal.h         |  16 +-
 tools/libs/light/libxl_json.c             | 316 ++++++++++++++++++++++
 tools/libs/light/libxl_types.idl          |   7 +-
 tools/libs/light/libxl_types_internal.idl |   3 +-
 tools/xl/xl_info.c                        | 102 ++++++-
 10 files changed, 729 insertions(+), 20 deletions(-)
tools/include/libxl_json.h +17 -0
index e2ef8151f0..c130e88a5e 100644
@@ -28,6 +28,22 @@
 #endif
 #endif
 
+#ifdef HAVE_LIBJSONC
+#ifndef _hidden
+#define _hidden
+#endif
+_hidden int libxl__uint64_gen_jso(json_object **jso_r, uint64_t val);
+_hidden int libxl_defbool_gen_jso(json_object **jso_r, libxl_defbool *p);
+_hidden int libxl_uuid_gen_jso(json_object **jso_r, libxl_uuid *p);
+_hidden int libxl_mac_gen_jso(json_object **jso_r, libxl_mac *p);
+_hidden int libxl_bitmap_gen_jso(json_object **jso_r, libxl_bitmap *p);
+_hidden int libxl_cpuid_policy_list_gen_jso(json_object **jso_r,libxl_cpuid_policy_list *p);
+_hidden int libxl_string_list_gen_jso(json_object **jso_r,libxl_string_list *p);
+_hidden int libxl_key_value_list_gen_jso(json_object **jso_r, libxl_key_value_list *p);
+_hidden int libxl_hwcap_gen_jso(json_object **jso_r, libxl_hwcap *p);
+_hidden int libxl_ms_vm_genid_gen_jso(json_object **jso_r, libxl_ms_vm_genid *p);
+#endif
+#if defined(HAVE_LIBYAJL)
 yajl_gen_status libxl__uint64_gen_json(yajl_gen hand, uint64_t val);
 yajl_gen_status libxl_defbool_gen_json(yajl_gen hand, libxl_defbool *p);
 yajl_gen_status libxl_uuid_gen_json(yajl_gen hand, libxl_uuid *p);
@@ -40,6 +56,7 @@ yajl_gen_status libxl_key_value_list_gen_json(yajl_gen hand,
                                               libxl_key_value_list *p);
 yajl_gen_status libxl_hwcap_gen_json(yajl_gen hand, libxl_hwcap *p);
 yajl_gen_status libxl_ms_vm_genid_gen_json(yajl_gen hand, libxl_ms_vm_genid *p);
+#endif
 
 #include <_libxl_types_json.h>
 
tools/libs/light/Makefile +1 -1
index fa2a338c2b..c59c2cf198 100644
@@ -227,7 +227,7 @@ testidl.o: $(XEN_INCLUDE)/libxl.h
 # This exploits the 'multi-target pattern rule' trick.
 # gentypes.py should be executed only once to make all the targets.
 _libxl_type%.h _libxl_type%_json.h _libxl_type%_private.h _libxl_type%.c: libxl_type%.idl gentypes.py idl.py
-	$(PYTHON) gentypes.py libxl_type$(*F).idl __libxl_type$(*F).h __libxl_type$(*F)_private.h \
+	$(PYTHON) gentypes.py $(if $(LIBJSONC_LIBS),--libjsonc) libxl_type$(*F).idl __libxl_type$(*F).h __libxl_type$(*F)_private.h \
 		__libxl_type$(*F)_json.h  __libxl_type$(*F).c
 	$(call move-if-changed,__libxl_type$(*F).h,_libxl_type$(*F).h)
 	$(call move-if-changed,__libxl_type$(*F)_private.h,_libxl_type$(*F)_private.h)
tools/libs/light/gentypes.py +151 -9

208 lines not on this page · raw message

tools/libs/light/idl.py +6 -1
index d7367503b4..61c8e14004 100644
@@ -79,6 +79,7 @@ class Type(object):
 
         if self.typename is not None and not self.private:
             self.json_gen_fn = kwargs.setdefault('json_gen_fn', self.typename + "_gen_json")
+            self.jsonc_json_gen_fn = kwargs.setdefault('jsonc_json_gen_fn', self.typename + "_gen_jso")
             self.json_parse_type = kwargs.setdefault('json_parse_type', "JSON_ANY")
             if self.namespace is not None:
                 self.json_parse_fn = kwargs.setdefault('json_parse_fn',
@@ -88,6 +89,7 @@ class Type(object):
                                                        self.typename + "_parse_json")
         else:
             self.json_gen_fn = kwargs.setdefault('json_gen_fn', None)
+            self.jsonc_json_gen_fn = kwargs.setdefault('jsonc_json_gen_fn', None)
             self.json_parse_type = kwargs.setdefault('json_parse_type', None)
             self.json_parse_fn = kwargs.setdefault('json_parse_fn', None)
 
@@ -142,6 +144,7 @@ class Number(Builtin):
         kwargs.setdefault('copy_fn', None)
         kwargs.setdefault('signed', False)
         kwargs.setdefault('json_gen_fn', "yajl_gen_integer")
+        kwargs.setdefault('jsonc_json_gen_fn', "libxl__int_gen_jso")
         kwargs.setdefault('json_parse_type', "JSON_INTEGER")
         # json_parse_fn might be overriden on specific type
         kwargs.setdefault('json_parse_fn', "libxl__int_parse_json")
@@ -290,6 +293,7 @@ void = Builtin("void *", namespace = None)
 bool = Builtin("bool", namespace = None,
                copy_fn=None,
                json_gen_fn = "yajl_gen_bool",
+               jsonc_json_gen_fn = "libxl__boolean_gen_jso",
                json_parse_type = "JSON_BOOL",
                json_parse_fn = "libxl__bool_parse_json",
                autogenerate_json = False)
@@ -301,10 +305,11 @@ integer = Number("int", namespace = None, signed = True)
 uint8 = UInt(8)
 uint16 = UInt(16)
 uint32 = UInt(32)
-uint64 = UInt(64, json_gen_fn = "libxl__uint64_gen_json")
+uint64 = UInt(64, json_gen_fn = "libxl__uint64_gen_json", jsonc_json_gen_fn = "libxl__uint64_gen_jso")
 
 string = Builtin("char *", namespace = None, copy_fn = "libxl_string_copy", dispose_fn = "free",
                  json_gen_fn = "libxl__string_gen_json",
+                 jsonc_json_gen_fn = "libxl__string_gen_jso",
                  json_parse_type = "JSON_STRING | JSON_NULL",
                  json_parse_fn = "libxl__string_parse_json",
                  autogenerate_json = False,
tools/libs/light/libxl_cpuid.c +119 -0

131 lines not on this page · raw message

tools/libs/light/libxl_internal.h +14 -2
index b66aaa779d..0f707e4204 100644
@@ -1993,9 +1993,11 @@ _hidden char *libxl__cpupoolid_to_name(libxl__gc *gc, uint32_t poolid);
 _hidden int libxl__enum_from_string(const libxl_enum_string_table *t,
                                     const char *s, int *e) NN(2);
 
-_hidden yajl_gen_status libxl__string_gen_json(yajl_gen hand, const char *p);
-
+#ifdef HAVE_LIBJSONC
+typedef int (*libxl__gen_json_callback)(json_object **jso_r, void *);
+#elif defined(HAVE_LIBYAJL)
 typedef yajl_gen_status (*libxl__gen_json_callback)(yajl_gen hand, void *);
+#endif
 _hidden char *libxl__object_to_json(libxl_ctx *ctx, const char *type,
                                     libxl__gen_json_callback gen, void *p);
 
@@ -2084,11 +2086,21 @@ int libxl__recvmsg_fds(libxl__gc *gc, int carrier,
                        void *databuf, size_t datalen,
                        int nfds, int fds[], const char *what);
 
+#ifdef HAVE_LIBJSONC
+_hidden int libxl__enum_gen_jso(json_object **jso_r, const char *str);
+_hidden int libxl__int_gen_jso(json_object **jso_r, int i);
+_hidden int libxl__boolean_gen_jso(json_object **jso_r, bool b);
+_hidden int libxl__string_gen_jso(json_object **jso_r, const char *p);
+#endif
+
+#ifdef HAVE_LIBYAJL
 /* from libxl_json */
 #include <yajl/yajl_gen.h>
 
 _hidden yajl_gen_status libxl__yajl_gen_asciiz(yajl_gen hand, const char *str);
 _hidden yajl_gen_status libxl__yajl_gen_enum(yajl_gen hand, const char *str);
+_hidden yajl_gen_status libxl__string_gen_json(yajl_gen hand, const char *p);
+#endif
 
 typedef enum {
     JSON_NULL    = (1 << 0),
tools/libs/light/libxl_json.c +316 -0

495 lines not on this page · raw message

tools/libs/light/libxl_types.idl +4 -3
index a6030a2dbd..caeddbcdc0 100644
@@ -7,9 +7,9 @@ namespace("libxl_")
 
 libxl_defbool = Builtin("defbool", json_parse_type="JSON_STRING", passby=PASS_BY_REFERENCE, copy_fn=None,
                         check_default_fn="libxl__defbool_is_default")
-libxl_domid = Builtin("domid", json_gen_fn = "yajl_gen_integer", json_parse_fn = "libxl__uint32_parse_json",
+libxl_domid = Builtin("domid", json_gen_fn = "yajl_gen_integer", jsonc_json_gen_fn = "libxl__uint64_gen_jso", json_parse_fn = "libxl__uint32_parse_json",
                       json_parse_type = "JSON_INTEGER", autogenerate_json = False, copy_fn=None)
-libxl_devid = Builtin("devid", json_gen_fn = "yajl_gen_integer", json_parse_fn = "libxl__int_parse_json",
+libxl_devid = Builtin("devid", json_gen_fn = "yajl_gen_integer", jsonc_json_gen_fn = "libxl__int_gen_jso", json_parse_fn = "libxl__int_parse_json",
                       json_parse_type = "JSON_INTEGER", autogenerate_json = False, signed = True, init_val="-1",
                       copy_fn=None)
 libxl_uuid = Builtin("uuid", json_parse_type="JSON_STRING", passby=PASS_BY_REFERENCE, check_default_fn="libxl_uuid_is_nil",
@@ -37,7 +37,8 @@ libxl_ms_vm_genid = Builtin("ms_vm_genid", passby=PASS_BY_REFERENCE, check_defau
 # Specific integer types
 #
 
-MemKB = UInt(64, init_val = "LIBXL_MEMKB_DEFAULT", json_gen_fn = "libxl__uint64_gen_json")
+MemKB = UInt(64, init_val = "LIBXL_MEMKB_DEFAULT",
+             json_gen_fn = "libxl__uint64_gen_json", jsonc_json_gen_fn = "libxl__uint64_gen_jso")
 
 #
 # Constants / Enumerations
tools/libs/light/libxl_types_internal.idl +2 -1
index 0425e9b6b0..ab4ee92870 100644
@@ -1,7 +1,8 @@
 namespace("libxl__")
 hidden(True)
 
-libxl_domid = Builtin("domid", namespace="libxl_", json_gen_fn = "yajl_gen_integer",
+libxl_domid = Builtin("domid", namespace="libxl_",
+                      json_gen_fn = "yajl_gen_integer", jsonc_json_gen_fn = "libxl__uint64_gen_jso",
 		      json_parse_fn = "libxl__uint32_parse_json", json_parse_type = "JSON_INTEGER",
 		      autogenerate_json = False, copy_fn = None)
 
tools/xl/xl_info.c +99 -3

172 lines not on this page · raw message

Signature · 1 line
Anthony PERARD
Reviewed-by on v1 7/11
#21 lore raw

Re: [XEN PATCH 07/11] libxl: libxl__object_to_json() to json-c

Quoted 39 lines · Anthony PERARD

From: Anthony PERARD <[email protected]>

- libxl changes:

While doing so, we rename all "*_gen_json" function to "*_gen_jso" as they have different prototype. All the function pointer are been cast to (libxl__gen_json_callback) by "gentypes.py" when generating "*_to_json()" functions.

We also introduce a few more "*_gen_jso" functions for "int" and "bool" because we can't use json_object_*() functions from json-c directly like it's done with yajl.

To make the generation of _libxl_types*json.[ch] with both YAJL and json-c we add "--libjsonc" to gentypes.py so it can generate functions/types for both.

Also introducing "jsonc_json_gen_fn" in the IDL, to be able to point to a different function when using json-c.

Also, don't export any of the new *_gen_jso() function, at the cost of having "_hidden" macro in semi-public headers.

- xl changes:

Also, rework the implementation of printf_info() in xl to avoid using libxl_domain_config_gen_json() which isn't available without YAJL. The implementation using "json_object" call libxl_domain_config_to_json() which generate a plain string of JSON, which we parse to add it to our own json; this avoid a dependency on the json library used by libxl.

tools/libs/light/libxl_json.c +1 -0
@@ -358,6 +535,36 @@ int libxl__mac_parse_json(libxl__gc *gc, const libxl__json_object *o,
      return libxl__parse_mac(libxl__json_object_get_string(o), *p);
  }
 
+#ifdef HAVE_LIBJSONC

tools/libs/light/libxl_json.c int libxl__mac_parse_json(libxl__gc *gc, const libxl__json_object *o, · line 548

Quoted 10 lines
tools/libs/light/libxl_json.c +10 -0
+int libxl_hwcap_gen_jso(json_object **jso_r, libxl_hwcap *p)
+{
+    json_object *jso;
+    int i;
+    int rc = ERROR_FAIL;
+
+    jso = json_object_new_array();
+    if (!jso) goto out;
+
+    for(i=0; i<4; i++) {

typedef uint32_t libxl_hwcap[8];

I see this is the same as the yajl implementation, but should this be 8?

The remainder looks good:

Thanks,
Jason

Quoted 22 lines
tools/libs/light/libxl_json.c +19 -0
+        json_object *jso_value = json_object_new_int((*p)[i]);
+        if (!jso_value)
+            goto out;
+        int r = json_object_array_add(jso, jso_value);
+        if (r) {
+            json_object_put(jso_value);
+            goto out;
+        }
+    }
+    *jso_r = jso;
+    jso = NULL;
+    rc = 0;
+out:
+    json_object_put(jso);
+    return rc;
+}
+#endif
+
+#ifdef HAVE_LIBYAJL
  yajl_gen_status libxl_hwcap_gen_json(yajl_gen hand,
                                       libxl_hwcap *p)
  {
#22 lore raw

Re: [XEN PATCH 07/11] libxl: libxl__object_to_json() to json-c

Quoted 53 lines · Jason Andryuk
Quoted 39 lines · Anthony PERARD

From: Anthony PERARD <[email protected]>

- libxl changes:

While doing so, we rename all "*_gen_json" function to "*_gen_jso" as they have different prototype. All the function pointer are been cast to (libxl__gen_json_callback) by "gentypes.py" when generating "*_to_json()" functions.

We also introduce a few more "*_gen_jso" functions for "int" and "bool" because we can't use json_object_*() functions from json-c directly like it's done with yajl.

To make the generation of _libxl_types*json.[ch] with both YAJL and json-c we add "--libjsonc" to gentypes.py so it can generate functions/types for both.

Also introducing "jsonc_json_gen_fn" in the IDL, to be able to point to a different function when using json-c.

Also, don't export any of the new *_gen_jso() function, at the cost of having "_hidden" macro in semi-public headers.

- xl changes:

Also, rework the implementation of printf_info() in xl to avoid using libxl_domain_config_gen_json() which isn't available without YAJL. The implementation using "json_object" call libxl_domain_config_to_json() which generate a plain string of JSON, which we parse to add it to our own json; this avoid a dependency on the json library used by libxl.

tools/libs/light/libxl_json.c +0 -0
@@ -358,6 +535,36 @@ int libxl__mac_parse_json(libxl__gc *gc, const
 libxl__json_object *o,
       return libxl__parse_mac(libxl__json_object_get_string(o), *p);
   }

  +#ifdef HAVE_LIBJSONC

tools/libs/light/libxl_json.c int libxl__mac_parse_json(libxl__gc *gc, const libxl__json_object *o, · line 548

Quoted 10 lines
tools/libs/light/libxl_json.c +10 -0
+int libxl_hwcap_gen_jso(json_object **jso_r, libxl_hwcap *p)
+{
+    json_object *jso;
+    int i;
+    int rc = ERROR_FAIL;
+
+    jso = json_object_new_array();
+    if (!jso) goto out;
+
+    for(i=0; i<4; i++) {

typedef uint32_t libxl_hwcap[8];

Quoted 5 lines

I see this is the same as the yajl implementation, but should this be 8?

The remainder looks good:

A tangent, but physinfo.hw_cap (which libxl_hwcap wraps) is as good as stack rubble.  It's not exactly random data, but it is a view of an internal Xen structure which has been reformatted multiple times before we even realised it was exported, then continued being reformatting it because it's a gross laying violation that noone could possibly be using anyway.

It needs purging from libxl and the sysctl interface, and if that makes JSON easier, then lets get it done.

~Andrew

#23 lore raw

Re: [XEN PATCH 07/11] libxl: libxl__object_to_json() to json-c

Quoted 54 lines · Andrew Cooper
Quoted 53 lines · Jason Andryuk
Quoted 39 lines · Anthony PERARD

From: Anthony PERARD <[email protected]>

- libxl changes:

While doing so, we rename all "*_gen_json" function to "*_gen_jso" as they have different prototype. All the function pointer are been cast to (libxl__gen_json_callback) by "gentypes.py" when generating "*_to_json()" functions.

We also introduce a few more "*_gen_jso" functions for "int" and "bool" because we can't use json_object_*() functions from json-c directly like it's done with yajl.

To make the generation of _libxl_types*json.[ch] with both YAJL and json-c we add "--libjsonc" to gentypes.py so it can generate functions/types for both.

Also introducing "jsonc_json_gen_fn" in the IDL, to be able to point to a different function when using json-c.

Also, don't export any of the new *_gen_jso() function, at the cost of having "_hidden" macro in semi-public headers.

- xl changes:

Also, rework the implementation of printf_info() in xl to avoid using libxl_domain_config_gen_json() which isn't available without YAJL. The implementation using "json_object" call libxl_domain_config_to_json() which generate a plain string of JSON, which we parse to add it to our own json; this avoid a dependency on the json library used by libxl.

tools/libs/light/libxl_json.c +0 -0
@@ -358,6 +535,36 @@ int libxl__mac_parse_json(libxl__gc *gc, const
 libxl__json_object *o,
       return libxl__parse_mac(libxl__json_object_get_string(o), *p);
   }

  +#ifdef HAVE_LIBJSONC

tools/libs/light/libxl_json.c int libxl__mac_parse_json(libxl__gc *gc, const libxl__json_object *o, · line 548

Quoted 10 lines
tools/libs/light/libxl_json.c +10 -0
+int libxl_hwcap_gen_jso(json_object **jso_r, libxl_hwcap *p)
+{
+    json_object *jso;
+    int i;
+    int rc = ERROR_FAIL;
+
+    jso = json_object_new_array();
+    if (!jso) goto out;
+
+    for(i=0; i<4; i++) {

typedef uint32_t libxl_hwcap[8];

Quoted 1 line
Quoted 1 line

I see this is the same as the yajl implementation, but should this be 8?

Yeah, looks like it should be ARRAY_SIZE(p). But that would want fixing in a different patch. And since no one complained about missing bits in this json conversion, I guess it's not used.

We would need to also fix libxl__hwcap_parse_json(), to parse up to 8 items, and be able to deal with missing 4 of them.

Quoted 11 lines
Quoted 4 lines

The remainder looks good:

A tangent, but physinfo.hw_cap (which libxl_hwcap wraps) is as good as stack rubble.  It's not exactly random data, but it is a view of an internal Xen structure which has been reformatted multiple times before we even realised it was exported, then continued being reformatting it because it's a gross laying violation that noone could possibly be using anyway.

It's display in xl info, surely it's useful for someone :-), a long long time ago since it seems to be inherited from xm info.

Quoted 2 lines

It needs purging from libxl and the sysctl interface, and if that makes JSON easier, then lets get it done.

It would be more work, not less. Removing things from libxl's api is annoying. But I guess we can look at removing that later.

Cheers,

Signature · 1 line
Anthony PERARD
#24 lore raw

PATCH 8/11 libxl: convert libxl__json_object_to_json() to json_object

In reply to Anthony PERARD

From: Anthony PERARD <[email protected]>

But keep the implementation done for YAJL.

---
 tools/libs/light/libxl_json.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
tools/libs/light/libxl_json.c +26 -0
index 01944ef94d..cffda158d2 100644
@@ -104,10 +104,12 @@ typedef struct libxl__yajl_ctx {
  * YAJL Helper
  */
 
+#ifdef HAVE_LIBYAJL
 yajl_gen_status libxl__yajl_gen_asciiz(yajl_gen hand, const char *str)
 {
     return yajl_gen_string(hand, (const unsigned char *)str, strlen(str));
 }
+#endif
 
 #ifdef HAVE_LIBJSONC
 int libxl__enum_gen_jso(json_object **jso_r, const char *str)
@@ -1522,6 +1524,29 @@ char *libxl__object_to_json(libxl_ctx *ctx, const char *type,
 char *libxl__json_object_to_json(libxl__gc *gc,
                                  const libxl__json_object *args)
 {
+#ifdef HAVE_LIBJSONC
+    const char *buf;
+    json_object *root;
+    char *ret = NULL;
+    int rc;
+
+    if (!args)
+        return NULL;
+
+    rc = libxl__json_object_to_json_object(gc, &root, args);
+    if (rc)
+        goto out;
+
+    buf = json_object_to_json_string_ext(root, JSON_C_TO_STRING_PRETTY);
+    if (!buf)
+        goto out;
+
+    ret = libxl__strdup(gc, buf);
+
+out:
+    json_object_put(root);
+    return ret;
+#elif defined(HAVE_LIBYAJL)
     const unsigned char *buf;
     libxl_yajl_length len;
     yajl_gen_status s;
@@ -1549,6 +1574,7 @@ char *libxl__json_object_to_json(libxl__gc *gc,
 out:
     yajl_gen_free(hand);
     return ret;
+#endif
 }
 
 #ifdef HAVE_LIBJSONC
Signature · 1 line
Anthony PERARD
#26 lore raw

PATCH 9/11 tools/libxenstat: Use json-c when available

In reply to Anthony PERARD

From: Anthony PERARD <[email protected]>

This is mainly a copy of the existing code in yajl and use json-c instead.

---
 tools/libs/stat/Makefile      |   3 +-
 tools/libs/stat/xenstat_qmp.c | 126 ++++++++++++++++++++++++++++++++--
 2 files changed, 121 insertions(+), 8 deletions(-)
tools/libs/stat/Makefile +2 -1
index a968eaff48..3f13cf07be 100644
@@ -24,7 +24,8 @@ OBJS-$(CONFIG_SunOS) += xenstat_solaris.o
 OBJS-$(CONFIG_NetBSD) += xenstat_netbsd.o
 OBJS-$(CONFIG_FreeBSD) += xenstat_freebsd.o
 
-LDLIBS-y += -lyajl
+LDLIBS-y += $(YAJL_LIBS)
+LDLIBS-y += $(LIBJSONC_LIBS)
 LDLIBS-$(CONFIG_SunOS) += -lkstat
 LDLIBS += $(LDLIBS-y)
 
tools/libs/stat/xenstat_qmp.c +119 -7

195 lines not on this page · raw message

Signature · 1 line
Anthony PERARD
#28 lore raw

PATCH 10/11 configure: Use json-c by default, fallback to yajl

In reply to Anthony PERARD

From: Anthony PERARD <[email protected]>

---
 tools/configure    | 97 +++++++++++++++++++++++++++++-----------------
 tools/configure.ac | 12 +++---
 2 files changed, 69 insertions(+), 40 deletions(-)
tools/configure +62 -35
index edd1701b2d..0eb7a0ab6a 100755
@@ -9692,41 +9692,7 @@ fi
 	# Put the nasty error message in config.log where it belongs
 	echo "$libjsonc_PKG_ERRORS" >&5
 
-	as_fn_error $? "Package requirements (json-c) were not met:
-
-$libjsonc_PKG_ERRORS
-
-Consider adjusting the PKG_CONFIG_PATH environment variable if you
-installed software in a non-standard prefix.
-
-Alternatively, you may set the environment variables libjsonc_CFLAGS
-and libjsonc_LIBS to avoid the need to call pkg-config.
-See the pkg-config man page for more details." "$LINENO" 5
-elif test $pkg_failed = untried; then
-     	{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: no" >&5
-printf "%s\n" "no" >&6; }
-	{ { printf "%s\n" "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5
-printf "%s\n" "$as_me: error: in \`$ac_pwd':" >&2;}
-as_fn_error $? "The pkg-config script could not be found or is too old.  Make sure it
-is in your PATH or set the PKG_CONFIG environment variable to the full
-path to pkg-config.
-
-Alternatively, you may set the environment variables libjsonc_CFLAGS
-and libjsonc_LIBS to avoid the need to call pkg-config.
-See the pkg-config man page for more details.
-
-To get pkg-config, see <http://pkg-config.freedesktop.org/>.
-See \`config.log' for more details" "$LINENO" 5; }
-else
-	libjsonc_CFLAGS=$pkg_cv_libjsonc_CFLAGS
-	libjsonc_LIBS=$pkg_cv_libjsonc_LIBS
-        { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: yes" >&5
-printf "%s\n" "yes" >&6; }
-
-printf "%s\n" "#define HAVE_LIBJSONC 1" >>confdefs.h
-
-fi
-{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for yajl_alloc in -lyajl" >&5
+	{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for yajl_alloc in -lyajl" >&5
 printf %s "checking for yajl_alloc in -lyajl... " >&6; }
 if test ${ac_cv_lib_yajl_yajl_alloc+y}
 then :
@@ -9772,6 +9738,67 @@ else $as_nop
   as_fn_error $? "Could not find yajl" "$LINENO" 5
 fi
 
+
+elif test $pkg_failed = untried; then
+     	{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: no" >&5
+printf "%s\n" "no" >&6; }
+	{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for yajl_alloc in -lyajl" >&5
+printf %s "checking for yajl_alloc in -lyajl... " >&6; }
+if test ${ac_cv_lib_yajl_yajl_alloc+y}
+then :
+  printf %s "(cached) " >&6
+else $as_nop
+  ac_check_lib_save_LIBS=$LIBS
+LIBS="-lyajl  $LIBS"
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+/* Override any GCC internal prototype to avoid an error.
+   Use char because int might match the return type of a GCC
+   builtin and then its argument prototype would still apply.  */
+char yajl_alloc ();
+int
+main (void)
+{
+return yajl_alloc ();
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_link "$LINENO"
+then :
+  ac_cv_lib_yajl_yajl_alloc=yes
+else $as_nop
+  ac_cv_lib_yajl_yajl_alloc=no
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.beam \
+    conftest$ac_exeext conftest.$ac_ext
+LIBS=$ac_check_lib_save_LIBS
+fi
+{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_yajl_yajl_alloc" >&5
+printf "%s\n" "$ac_cv_lib_yajl_yajl_alloc" >&6; }
+if test "x$ac_cv_lib_yajl_yajl_alloc" = xyes
+then :
+  YAJL_LIBS=-lyajl
+
+
+printf "%s\n" "#define HAVE_LIBYAJL 1" >>confdefs.h
+
+else $as_nop
+  as_fn_error $? "Could not find yajl" "$LINENO" 5
+fi
+
+
+else
+	libjsonc_CFLAGS=$pkg_cv_libjsonc_CFLAGS
+	libjsonc_LIBS=$pkg_cv_libjsonc_LIBS
+        { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: yes" >&5
+printf "%s\n" "yes" >&6; }
+
+printf "%s\n" "#define HAVE_LIBJSONC 1" >>confdefs.h
+
+fi
+
 { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for deflateCopy in -lz" >&5
 printf %s "checking for deflateCopy in -lz... " >&6; }
 if test ${ac_cv_lib_z_deflateCopy+y}
tools/configure.ac +7 -5
index bb40b5b3f0..7267d02a04 100644
@@ -425,11 +425,13 @@ AC_SUBST([ZLIB_LIBS])
 AX_CHECK_EXTFS
 AX_CHECK_PTHREAD
 PKG_CHECK_MODULES([libjsonc], [json-c],
-    [AC_DEFINE([HAVE_LIBJSONC], [1], [Use library json-c])])
-AC_CHECK_LIB([yajl], [yajl_alloc],
-   [AC_SUBST([YAJL_LIBS],[-lyajl])
-    AC_DEFINE([HAVE_LIBYAJL],[1],[Define to 1 if you have the `yajl' library (-lyajl).])],
-    [AC_MSG_ERROR([Could not find yajl])])
+    [AC_DEFINE([HAVE_LIBJSONC], [1], [Use library json-c])],
+    [AC_CHECK_LIB([yajl], [yajl_alloc],
+        [AC_SUBST([YAJL_LIBS],[-lyajl])
+         AC_DEFINE([HAVE_LIBYAJL],[1],[Define to 1 if you have the `yajl' library (-lyajl).])],
+        [AC_MSG_ERROR([Could not find yajl])])
+])
+
 AC_CHECK_LIB([z], [deflateCopy], [], [AC_MSG_ERROR([Could not find zlib])])
 AC_CHECK_HEADER([argp.h], [
 AC_CHECK_LIB([argp], [argp_usage], [argp_ldflags="-largp"])
Signature · 1 line
Anthony PERARD
#29 lore raw

PATCH 11/11 Update CHANGELOG and README with dependency on json-c

In reply to Anthony PERARD

From: Anthony PERARD <[email protected]>

---
 CHANGELOG.md | 2 ++
 README       | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
CHANGELOG.md +2 -0
index 5f31ca08fe..83195e2dae 100644
@@ -11,6 +11,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
    - For x86, GCC 5.1 and Binutils 2.25, or Clang/LLVM 11
    - For ARM32 and ARM64, GCC 5.1 and Binutils 2.25
  - Linux based device model stubdomains are now fully supported.
+ - New dependency on library json-c, the toolstack will prefer it to `YAJL`
+   when available.
 
  - On x86:
    - Restrict the cache flushing done as a result of guest physical memory map
README +1 -1
index 6ee58f7b35..9329f30e13 100644
@@ -53,7 +53,7 @@ provided by your OS distributor:
     * Development install of Python 2.7 or later (e.g., python-dev)
     * Development install of curses (e.g., libncurses-dev)
     * Development install of uuid (e.g. uuid-dev)
-    * Development install of yajl (e.g. libyajl-dev)
+    * Development install of json-c (e.g. libjson-c-dev) or yajl (e.g. libyajl-dev)
     * Development install of libaio (e.g. libaio-dev) version 0.3.107 or
       greater.
     * Development install of GLib v2.0 (e.g. libglib2.0-dev)
Signature · 1 line
Anthony PERARD
#30 lore raw

Re: [XEN PATCH 11/11] Update CHANGELOG and README with dependency on json-c

Quoted 3 lines · Anthony PERARD

From: Anthony PERARD<[email protected]>

LGTM: Acked-by: Oleksii Kurochko <oleksii.kurochko@gmail> With one question ...

Quoted 23 lines
---
  CHANGELOG.md | 2 ++
  README       | 2 +-
  2 files changed, 3 insertions(+), 1 deletion(-)
CHANGELOG.md +2 -0
index 5f31ca08fe..83195e2dae 100644
@@ -11,6 +11,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
     - For x86, GCC 5.1 and Binutils 2.25, or Clang/LLVM 11
     - For ARM32 and ARM64, GCC 5.1 and Binutils 2.25
   - Linux based device model stubdomains are now fully supported.
+ - New dependency on library json-c, the toolstack will prefer it to `YAJL`
+   when available.
 
   - On x86:
     - Restrict the cache flushing done as a result of guest physical memory map
README +0 -0
index 6ee58f7b35..9329f30e13 100644

README provided by your OS distributor: · line 56

Quoted 5 lines
README +1 -1
@@ -53,7 +53,7 @@ provided by your OS distributor:
      * Development install of Python 2.7 or later (e.g., python-dev)
      * Development install of curses (e.g., libncurses-dev)
      * Development install of uuid (e.g. uuid-dev)
-    * Development install of yajl (e.g. libyajl-dev)
+    * Development install of json-c (e.g. libjson-c-dev) or yajl (e.g. libyajl-dev)

... as you mentioned in the cover letter libyajl-dev is unmaintained for several years. Do any plans exist to drop libyajl-dev at all? Can't it be dropped now?

~ Oleksii

Quoted 3 lines
README +0 -0
      * Development install of libaio (e.g. libaio-dev) version 0.3.107 or
        greater.
      * Development install of GLib v2.0 (e.g. libglib2.0-dev)
#31 lore raw

Re: [XEN PATCH 11/11] Update CHANGELOG and README with dependency on json-c

In reply to Anthony PERARD
Quoted 26 lines · Anthony PERARD

From: Anthony PERARD <[email protected]>

---
 CHANGELOG.md | 2 ++
 README       | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
CHANGELOG.md +2 -0
index 5f31ca08fe..83195e2dae 100644
@@ -11,6 +11,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
    - For x86, GCC 5.1 and Binutils 2.25, or Clang/LLVM 11
    - For ARM32 and ARM64, GCC 5.1 and Binutils 2.25
  - Linux based device model stubdomains are now fully supported.
+ - New dependency on library json-c, the toolstack will prefer it to `YAJL`
+   when available.
 
  - On x86:
    - Restrict the cache flushing done as a result of guest physical memory map
README +0 -0
index 6ee58f7b35..9329f30e13 100644

README provided by your OS distributor: · line 59

Quoted 8 lines
README +1 -1
@@ -53,7 +53,7 @@ provided by your OS distributor:
     * Development install of Python 2.7 or later (e.g., python-dev)
     * Development install of curses (e.g., libncurses-dev)
     * Development install of uuid (e.g. uuid-dev)
-    * Development install of yajl (e.g. libyajl-dev)
+    * Development install of json-c (e.g. libjson-c-dev) or yajl (e.g. libyajl-dev)
     * Development install of libaio (e.g. libaio-dev) version 0.3.107 or
       greater.
     * Development install of GLib v2.0 (e.g. libglib2.0-dev)

What are we going to do about testing this?

We should add libjson-c to some build containers.  We need to annotate # Xen < 4.21 in the existing containers so we can drop it in due course.

Also, for a container we don't intend to backport to stable trees, we should have libjson-c only and no yajl.  Probably best to do this in the Alpine update which is long overdue.

~Andrew

#32 lore raw

Re: [XEN PATCH 11/11] Update CHANGELOG and README with dependency on json-c

Quoted 38 lines · Andrew Cooper
Quoted 26 lines · Anthony PERARD

From: Anthony PERARD <[email protected]>

---
 CHANGELOG.md | 2 ++
 README       | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
CHANGELOG.md +2 -0
index 5f31ca08fe..83195e2dae 100644
@@ -11,6 +11,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
    - For x86, GCC 5.1 and Binutils 2.25, or Clang/LLVM 11
    - For ARM32 and ARM64, GCC 5.1 and Binutils 2.25
  - Linux based device model stubdomains are now fully supported.
+ - New dependency on library json-c, the toolstack will prefer it to `YAJL`
+   when available.
 
  - On x86:
    - Restrict the cache flushing done as a result of guest physical memory map
README +0 -0
index 6ee58f7b35..9329f30e13 100644

README provided by your OS distributor: · line 59

Quoted 8 lines
README +1 -1
@@ -53,7 +53,7 @@ provided by your OS distributor:
     * Development install of Python 2.7 or later (e.g., python-dev)
     * Development install of curses (e.g., libncurses-dev)
     * Development install of uuid (e.g. uuid-dev)
-    * Development install of yajl (e.g. libyajl-dev)
+    * Development install of json-c (e.g. libjson-c-dev) or yajl (e.g. libyajl-dev)
     * Development install of libaio (e.g. libaio-dev) version 0.3.107 or
       greater.
     * Development install of GLib v2.0 (e.g. libglib2.0-dev)

What are we going to do about testing this?

Quoted 2 lines

We should add libjson-c to some build containers.  We need to annotate # Xen < 4.21 in the existing containers so we can drop it in due course.

archlinux container already have json-c so the CI already do some build jobs with it. (no *-dev needed for this one container). It even notice an issue with debug=n builds, I had to replace strncpy by memcpy.

Quoted 3 lines

Also, for a container we don't intend to backport to stable trees, we should have libjson-c only and no yajl.  Probably best to do this in the Alpine update which is long overdue.

Doing this on alpine container would allow to actually do some runtime tests with json-c instead of yajl, so yes.

Signature · 1 line
Anthony PERARD
#33 lore raw
In reply to Anthony PERARD
Quoted 21 lines · Anthony PERARD

From: Anthony PERARD <[email protected]>

Patch series available in this git branch:
https://xenbits.xenproject.org/git-http/people/aperard/xen-unstable.git br.libxl-libjsonc-v1

Hi,

The library YAJL has been unmaintained for several years, without an obvious fork to pick.

On the other and the library json-c is been maintained and use by several other project, it's probably already installed on your machine. So this patch series intend to allow to build the Xen toolstack again json-c, and forgo yajl.

Just in case, YAJL is can still be used.

There's bit of libxl API that exposes YAJL, mainly so it can be used by xl to call libxl_domain_config_gen_json(). It was exposed via the "libxl_json.h" headers. This functions and others won't be available when libxl is build against json-c.

Quoted 6 lines

Cheers,

Anthony PERARD (11):
xl: move printf_info prototype to an header
libxl: Remove duplicate libxl_domain_config_gen_json prototype
libxl: remove duplicated libxl__yajl_gen_asciiz() prototype

First three, trivially Acked-by: Andrew Cooper <[email protected]>

I'll look at the rest when I've got a bit more time.