Skip to contentSkip to conversation

libxl: autogenerate type definitions and destructor functions

PATCH In review Ian Campbell 5 participants 38 messages Aug 16, 2010 – Aug 17, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libxl: autogenerate type definitions and destructor functions

The series introduces auto-generation of the type definitions used in the libxl interface followed by auto-generation of a destructor function for each type. In the future it may be possible to use the related data structures for other purposes, for example auto-generation of the functions to marshal between C and language binding data types.

tools/_libxl_types.h should be identical both before applying and after applying+building "libxl: autogenerate _libxl_types.h" apart from a "DO NOT EDIT" header.

Since last time:
* rebased
* corrected Makefile dependencies to include libxltypes.idl
* manually implemented libxl_file_reference_destroy since it is more complex than just freeing the contained types.
* Made libxl_file_reference_{map,unmap} into internal functions.
* Added typedefs for various types:
- libxl_cpumap
- libxl_hwcap
* Made libxl_xen_console_reader an opaque type and by making the definition internal.
* moved more types from libxl.h to _libxl_types.h. I think all those which it makes sense to generate are now accounted for.
* disabled destructor generation for types which have no interesting fields (i.e. had empty destructor functions). I have retained the empty destructors for types which belong to a set where some types do have a valid need for a destructor funntion (e.g. libxl_device_* or libxl_*info)
* Audit for usages of libxl_device_* and libxl_*info which can use the new destructors. I'm sure I haven't caught them all.

Depends (or at least was developed on top of) on Gianni patches:
* fix invalid free segfault and use-after-free in libxl_device_disk_list()
* v2: xl: make libxl_uuid2string internal to libxenlight

Tested with:
create
destroy (w/ valgrind)
block-list (w/ valgrind)
network-list (w/ valgrind)
network-attach (w/ valgrind)
network-detach (w/ valgrind)
vcpu-list (w/ valgrind)
cd-insert (w/ valgrind)

create, other than dry run, and valgrind don't currently play nice together. It's not clear if its all the foreign mappings or something else. I've tested creating both HVM and PV guests.

Ian.

#1 lore raw

PATCH 1/1 xl: use the regular implicit rules to build the xl .o files

# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1281969064 -3600
# Node ID a05ba7c6ed2b070d75c9ba9f69fb9ce30346250a # Parent 28a59726148cc062fe764a78b0effc1c6ac5a367
xl: use the regular implicit rules to build the xl .o files

Having $(XL_OBJS) makes part of a subsequent patch a little cleaner.

tools/libxl/Makefile +3 -10
diff -r 28a59726148c -r a05ba7c6ed2b tools/libxl/Makefile
@@ -25,6 +25,8 @@ LIBXLU_OBJS = libxlu_cfg_y.o libxlu_cfg_
 LIBXLU_OBJS = libxlu_cfg_y.o libxlu_cfg_l.o libxlu_cfg.o
 
 CLIENTS = xl
+
+XL_OBJS = xl.o xl_cmdimpl.o xl_cmdtable.o
 
 .PHONY: all
 all: $(CLIENTS) libxenlight.so libxenlight.a libxlutil.so libxlutil.a \
@@ -76,16 +78,7 @@ libxlutil.a: $(LIBXLU_OBJS)
 libxlutil.a: $(LIBXLU_OBJS)
 	$(AR) rcs libxlutil.a $^
 
-xl.o: xl.c
-	$(CC) $(CFLAGS) -c xl.c
-
-xl_cmdimpl.o: xl_cmdimpl.c
-	$(CC) $(CFLAGS) -c xl_cmdimpl.c
-
-xl_cmdtable.o: xl_cmdtable.c
-	$(CC) $(CFLAGS) -c xl_cmdtable.c
-
-$(CLIENTS): xl.o xl_cmdimpl.o xl_cmdtable.o libxlutil.so libxenlight.so
+$(CLIENTS): $(XL_OBJS) libxlutil.so libxenlight.so
 	$(CC) $(LDFLAGS) -o $@ $^ $(LIBS)
 
 .PHONY: install
#2 lore raw

PATCH 1/1 libxl: define specific types for string list and key, value list

In reply to Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1281969064 -3600
# Node ID f76fd27048c69a88c9d38c0d162ac0d92bfc629f
# Parent a05ba7c6ed2b070d75c9ba9f69fb9ce30346250a
libxl: define specific types for string list and key,value list

tools/libxl/libxl.h +7 -3
diff -r a05ba7c6ed2b -r f76fd27048c6 tools/libxl/libxl.h
@@ -141,6 +141,10 @@ typedef uint8_t libxl_uuid[16];
 
 typedef uint8_t libxl_mac[6];
 
+typedef char **libxl_string_list;
+
+typedef char **libxl_key_value_list;
+
 typedef struct {
     libxl_uuid uuid;
     uint32_t domid;
@@ -209,8 +213,8 @@ typedef struct {
     int ssidref;
     char *name;
     libxl_uuid uuid;
-    char **xsdata;
-    char **platformdata;
+    libxl_key_value_list xsdata;
+    libxl_key_value_list platformdata;
     uint32_t poolid;
     char *poolname;
 } libxl_domain_create_info;
@@ -313,7 +317,7 @@ typedef struct {
     int vcpus; /* max number of vcpus */
     int vcpu_avail; /* vcpus actually available */
     int xen_platform_pci; /* enable/disable the xen platform pci device */
-    char **extra; /* extra parameters pass directly to qemu, NULL terminated */
+    libxl_string_list extra; /* extra parameters pass directly to qemu, NULL terminated */
     /* Network is missing */
 } libxl_device_model_info;
#3 lore raw

PATCH 1/1 libxl: move various enum and #defines above datastructure definitions

In reply to Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1281969064 -3600
# Node ID cafbb3f960bab892721b90f6a0d6f24e85f8f494
# Parent f76fd27048c69a88c9d38c0d162ac0d92bfc629f
libxl: move various enum and #defines above datastructure definitions.

These are left behind when the datastructures move to _lixl_types.hg in a following patch and moving them first makes that pure code motion patch more obviously correct.

tools/libxl/libxl.h +31 -30
diff -r f76fd27048c6 -r cafbb3f960ba tools/libxl/libxl.h
@@ -144,6 +144,37 @@ typedef char **libxl_string_list;
 typedef char **libxl_string_list;
 
 typedef char **libxl_key_value_list;
+
+typedef enum {
+    XENFV = 1,
+    XENPV,
+} libxl_qemu_machine_type;
+
+typedef enum {
+    LIBXL_CONSTYPE_SERIAL,
+    LIBXL_CONSTYPE_PV,
+} libxl_console_constype;
+
+typedef enum {
+    LIBXL_CONSBACK_XENCONSOLED,
+    LIBXL_CONSBACK_IOEMU,
+} libxl_console_consback;
+
+typedef enum {
+    PHYSTYPE_QCOW = 1,
+    PHYSTYPE_QCOW2,
+    PHYSTYPE_VHD,
+    PHYSTYPE_AIO,
+    PHYSTYPE_FILE,
+    PHYSTYPE_PHY,
+} libxl_disk_phystype;
+
+typedef enum {
+    NICTYPE_IOEMU = 1,
+    NICTYPE_VIF,
+} libxl_nic_type;
+
+#define LIBXL_PCI_FUNC_ALL (~0U)
 
 typedef struct {
     libxl_uuid uuid;
@@ -285,11 +316,6 @@ typedef struct {
     int (*suspend_callback)(void *, int);
 } libxl_domain_suspend_info;
 
-typedef enum {
-    XENFV = 1,
-    XENPV,
-} libxl_qemu_machine_type;
-
 typedef struct {
     int domid;
     libxl_uuid uuid; /* this is use only with stubdom, and must be different from the domain uuid */
@@ -343,16 +369,6 @@ typedef struct {
     int devid;
 } libxl_device_vkb;
 
-typedef enum {
-    LIBXL_CONSTYPE_SERIAL,
-    LIBXL_CONSTYPE_PV,
-} libxl_console_constype;
-
-typedef enum {
-    LIBXL_CONSBACK_XENCONSOLED,
-    LIBXL_CONSBACK_IOEMU,
-} libxl_console_consback;
-
 typedef struct {
     uint32_t backend_domid;
     uint32_t domid;
@@ -361,15 +377,6 @@ typedef struct {
     libxl_domain_build_state *build_state;
     char *output;
 } libxl_device_console;
-
-typedef enum {
-    PHYSTYPE_QCOW = 1,
-    PHYSTYPE_QCOW2,
-    PHYSTYPE_VHD,
-    PHYSTYPE_AIO,
-    PHYSTYPE_FILE,
-    PHYSTYPE_PHY,
-} libxl_disk_phystype;
 
 typedef struct {
     uint32_t backend_domid;
@@ -381,11 +388,6 @@ typedef struct {
     int readwrite;
     int is_cdrom;
 } libxl_device_disk;
-
-typedef enum {
-    NICTYPE_IOEMU = 1,
-    NICTYPE_VIF,
-} libxl_nic_type;
 
 typedef struct {
     uint32_t backend_domid;
@@ -431,7 +433,6 @@ typedef struct {
     };
     unsigned int domain;
     unsigned int vdevfn;
-#define LIBXL_PCI_FUNC_ALL (~0U)
     unsigned int vfunc_mask;
     bool msitranslate;
     bool power_mgmt;
#4 lore raw

PATCH 1/1 libxl: add specific type for cpumap

In reply to Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]> # Date 1281969064 -3600
# Node ID 2ae6c13c2724ee5841d6f851f0bb100a7fef400c # Parent cafbb3f960bab892721b90f6a0d6f24e85f8f494
libxl: add specific type for cpumap.

tools/libxl/libxl.h +3 -1
diff -r cafbb3f960ba -r 2ae6c13c2724 tools/libxl/libxl.h
@@ -144,6 +144,8 @@ typedef char **libxl_string_list;
 typedef char **libxl_string_list;
 
 typedef char **libxl_key_value_list;
+
+typedef uint64_t *libxl_cpumap;
 
 typedef enum {
     XENFV = 1,
@@ -695,7 +697,7 @@ typedef struct {
     uint8_t blocked:1; /* blocked waiting for an event? */
     uint8_t running:1; /* currently scheduled on its CPU? */
     uint64_t vcpu_time; /* total vcpu time ran (ns) */
-    uint64_t *cpumap; /* current cpu's affinities */
+    libxl_cpumap cpumap; /* current cpu's affinities */
 } libxl_vcpuinfo;
 
 typedef struct {
#5 lore raw

PATCH 1/1 libxl: add specific type for hwcaps

In reply to Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]> # Date 1281969064 -3600
# Node ID 9c9b64a020535e99286bbb84502ec6cc30c8cc90 # Parent 2ae6c13c2724ee5841d6f851f0bb100a7fef400c
libxl: add specific type for hwcaps

tools/libxl/libxl.h +3 -1
diff -r 2ae6c13c2724 -r 9c9b64a02053 tools/libxl/libxl.h
@@ -146,6 +146,8 @@ typedef char **libxl_key_value_list;
 typedef char **libxl_key_value_list;
 
 typedef uint64_t *libxl_cpumap;
+
+typedef uint32_t libxl_hwcap[8];
 
 typedef enum {
     XENFV = 1,
@@ -713,7 +715,7 @@ typedef struct {
     uint64_t scrub_pages;
 
     uint32_t nr_nodes;
-    uint32_t hw_cap[8];
+    libxl_hwcap hw_cap;
     uint32_t phys_cap;
 }  libxl_physinfo;
#6 lore raw

PATCH 1/1 libxl: make libxl_console_reader type opaque to users of libxl

In reply to Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1281969065 -3600
# Node ID 075c01bbdf4db1bf2cf3b77cd82a59430afc894b
# Parent 9c9b64a020535e99286bbb84502ec6cc30c8cc90
libxl: make libxl_console_reader type opaque to users of libxl

tools/libxl/libxl.h +1 -8
diff -r 9c9b64a02053 -r 075c01bbdf4d tools/libxl/libxl.h
@@ -744,14 +744,7 @@ int libxl_send_sysrq(libxl_ctx *ctx, uin
 int libxl_send_sysrq(libxl_ctx *ctx, uint32_t domid, char sysrq);
 int libxl_send_debug_keys(libxl_ctx *ctx, char *keys);
 
-typedef struct {
-    char *buffer;
-    unsigned int size;
-    unsigned int count;
-    unsigned int clear;
-    unsigned int incremental;
-    unsigned int index;
-}  libxl_xen_console_reader;
+typedef struct libxl__xen_console_reader libxl_xen_console_reader;
 
 libxl_xen_console_reader *
     libxl_xen_console_read_start(libxl_ctx *ctx, int clear);
tools/libxl/libxl_internal.h +9 -0
diff -r 9c9b64a02053 -r 075c01bbdf4d tools/libxl/libxl_internal.h
@@ -244,4 +244,13 @@ _hidden char *_libxl_poolid_to_name(libx
 
 _hidden char *libxl_uuid2string(libxl_gc *gc, const libxl_uuid uuid);
 
+struct libxl__xen_console_reader {
+    char *buffer;
+    unsigned int size;
+    unsigned int count;
+    unsigned int clear;
+    unsigned int incremental;
+    unsigned int index;
+};
+
 #endif
#7 lore raw

PATCH 1/1 lbixl: make libxl_device_model_starting type opaque to users of libxl

In reply to Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1281969065 -3600
# Node ID 3b7826a70148e2bf1e2bc9eecd7623a1f23a95fc
# Parent 075c01bbdf4db1bf2cf3b77cd82a59430afc894b
lbixl: make libxl_device_model_starting type opaque to users of libxl

tools/libxl/libxl.h +1 -1
diff -r 075c01bbdf4d -r 3b7826a70148 tools/libxl/libxl.h
@@ -577,7 +577,7 @@ libxl_poolinfo * libxl_list_pool(libxl_c
 libxl_poolinfo * libxl_list_pool(libxl_ctx*, int *nb_pool);
 libxl_vminfo * libxl_list_vm(libxl_ctx *ctx, int *nb_vm);
 
-typedef struct libxl_device_model_starting libxl_device_model_starting;
+typedef struct libxl__device_model_starting libxl_device_model_starting;
 int libxl_create_device_model(libxl_ctx *ctx,
                               libxl_device_model_info *info,
                               libxl_device_disk *disk, int num_disks,
tools/libxl/libxl_internal.h +1 -1
diff -r 075c01bbdf4d -r 3b7826a70148 tools/libxl/libxl_internal.h
@@ -199,7 +199,7 @@ typedef struct {
     char *what; /* malloc'd in spawn_spawn */
 }  libxl_spawn_starting;
 
-struct libxl_device_model_starting {
+struct libxl__device_model_starting {
     libxl_spawn_starting *for_spawn; /* first! */
     char *dom_path; /* from libxl_malloc, only for dm_xenstore_record_pid */
     int domid;
#8 lore raw

PATCH 1/1 libxl: ensure result of libxl_poolid_to_name is always dynamically allocated

In reply to Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1281969065 -3600
# Node ID 9933762e16944f88d2e3a1637584d503d2b051ed
# Parent 3b7826a70148e2bf1e2bc9eecd7623a1f23a95fc
libxl: ensure result of libxl_poolid_to_name is always dynamically allocated

tools/libxl/libxl_utils.c +1 -1
diff -r 3b7826a70148 -r 9933762e1694 tools/libxl/libxl_utils.c
@@ -97,7 +97,7 @@ char *libxl_poolid_to_name(libxl_ctx *ct
     char *s;
 
     if (poolid == 0)
-        return "Pool-0";
+        return strdup("Pool-0");
     snprintf(path, sizeof(path), "/local/pool/%d/name", poolid);
     s = xs_read(ctx->xsh, XBT_NULL, path, &len);
     return s;
#9 lore raw

PATCH 1/1 libxl: move type definitions into _libxl_types.h

In reply to Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]> # Date 1281969065 -3600
# Node ID 9812d883f7085b9029982fa8e03f5cd5394c1b8b # Parent 9933762e16944f88d2e3a1637584d503d2b051ed
libxl: move type definitions into _libxl_types.h

The intention is to autogenerate this file in a future patch. This pure code motion patch allows for easier before and after comparison of that patch.

tools/libxl/Makefile +4 -1
diff -r 9933762e1694 -r 9812d883f708 tools/libxl/Makefile
@@ -96,12 +96,15 @@ install: all
 	ln -sf libxlutil.so.$(XLUMAJOR).$(XLUMINOR) $(DESTDIR)$(LIBDIR)/libxlutil.so.$(XLUMAJOR)
 	ln -sf libxlutil.so.$(XLUMAJOR) $(DESTDIR)$(LIBDIR)/libxlutil.so
 	$(INSTALL_DATA) libxlutil.a $(DESTDIR)$(LIBDIR)
-	$(INSTALL_DATA) libxl.h $(DESTDIR)$(INCLUDEDIR)
+	$(INSTALL_DATA) libxl.h _libxl_types.h $(DESTDIR)$(INCLUDEDIR)
 	$(INSTALL_DATA) bash-completion $(DESTDIR)$(BASH_COMPLETION_DIR)/xl.sh
 
 .PHONY: clean
 clean:
+	# XXX Preserve during transition to autogeneration
+	cp _libxl_types.h SAVED__libxl_types.h
 	$(RM) -f _*.h *.o *.so* *.a $(CLIENTS) $(DEPS)
+	mv SAVED__libxl_types.h _libxl_types.h 
 #	$(RM) -f $(AUTOSRCS) $(AUTOINCS)
 
 distclean: clean
tools/libxl/_libxl_types.h new file +315 -0

315 lines not on this page · raw message

tools/libxl/libxl.h +1 -310

358 lines not on this page · raw message

#10 lore raw

PATCH 1/1 libxl: tweak formatting/whitespace of _libxl_types.h

In reply to Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]> # Date 1281969065 -3600
# Node ID 01f64d048f0fef53e638169a204fa0649a9245d1 # Parent 9812d883f7085b9029982fa8e03f5cd5394c1b8b
libxl: tweak formatting/whitespace of _libxl_types.h to better suit autogeneration

This makes it easier to do a before/after comparison when the file becomes autogenerated.

tools/libxl/_libxl_types.h +194 -84
diff -r 9812d883f708 -r 01f64d048f0f tools/libxl/_libxl_types.h
@@ -9,7 +9,6 @@ typedef struct {
     uint8_t paused:1;
     uint8_t shutdown:1;
     uint8_t dying:1;
-
     /*
      * Valid SHUTDOWN_* value from xen/sched.h iff (shutdown||dying).
      *
@@ -17,7 +16,6 @@ typedef struct {
      * SHUTDOWN_* constant.
      */
     unsigned int shutdown_reason;
-
     uint64_t max_memkb;
     uint64_t cpu_time;
     uint32_t vcpu_max_id;
@@ -36,16 +34,16 @@ typedef struct {
 typedef struct {
     int xen_version_major;
     int xen_version_minor;
-    char *xen_version_extra;
-    char *compiler;
-    char *compile_by;
-    char *compile_domain;
-    char *compile_date;
-    char *capabilities;
-    char *changeset;
+    char * xen_version_extra;
+    char * compiler;
+    char * compile_by;
+    char * compile_domain;
+    char * compile_date;
+    char * capabilities;
+    char * changeset;
     unsigned long virt_start;
     unsigned long pagesize;
-    char *commandline;
+    char * commandline;
 } libxl_version_info;
 
 typedef struct {
@@ -53,22 +51,22 @@ typedef struct {
     bool hap;
     bool oos;
     int ssidref;
-    char *name;
+    char * name;
     libxl_uuid uuid;
     libxl_key_value_list xsdata;
     libxl_key_value_list platformdata;
     uint32_t poolid;
-    char *poolname;
+    char * poolname;
 } libxl_domain_create_info;
 
 typedef struct {
     /*
-     * Path is always set if the file refernece is valid. However if
+     * Path is always set if the file reference is valid. However if
      * mapped is true then the actual file may already be unlinked.
      */
-    char *path;
+    char * path;
     int mapped;
-    void *data;
+    void * data;
     size_t size;
 } libxl_file_reference;
 
@@ -97,18 +95,18 @@ typedef struct {
             bool acpi;
             bool nx;
             bool viridian;
-            char *timeoffset;
+            char * timeoffset;
             bool hpet;
             bool vpt_align;
             int timer_mode;
         } hvm;
         struct {
-            uint32_t   slack_memkb;
-            const char *bootloader;
-            const char *bootloader_args;
-            char *cmdline;
+            uint32_t slack_memkb;
+            const char * bootloader;
+            const char * bootloader_args;
+            char * cmdline;
             libxl_file_reference ramdisk;
-            const char *features;
+            const char * features;
         } pv;
     } u;
 } libxl_domain_build_info;
@@ -120,51 +118,145 @@ typedef struct {
     unsigned long console_mfn;
 } libxl_domain_build_state;
 
+/*
+ * Device Model information.
+ *
+ * Network is missing
+ */
 typedef struct {
     int domid;
-    libxl_uuid uuid; /* this is use only with stubdom, and must be different from the domain uuid */
-    char *dom_name;
-    char *device_model;
-    char *saved_state;
+    /*
+     * this is use only with stubdom, and must be different from the domain uuid
+     */
+    libxl_uuid uuid;
+    char * dom_name;
+    char * device_model;
+    char * saved_state;
     libxl_qemu_machine_type type;
-    int videoram; /* size of the videoram in MB */
-    bool stdvga; /* stdvga enabled or disabled */
-    bool vnc; /* vnc enabled or disabled */
-    char *vnclisten; /* address:port that should be listened on for the VNC server if vnc is set */
-    char *vncpasswd; /* the VNC password */
-    int vncdisplay; /* set VNC display number */
-    bool vncunused; /* try to find an unused port for the VNC server */
-    char *keymap; /* set keyboard layout, default is en-us keyboard */
-    bool sdl; /* sdl enabled or disabled */
-    bool opengl; /* opengl enabled or disabled (if enabled requires sdl enabled) */
-    bool nographic; /* no graphics, use serial port */
-    char *serial; /* serial port re-direct to pty deivce */
-    char *boot; /* boot order, for example dca */
-    bool usb; /* usb support enabled or disabled */
-    char *usbdevice; /* enable usb mouse: tablet for absolute mouse, mouse for PS/2 protocol relative mouse */
-    char *soundhw; /* enable sound hardware */
-    bool apic; /* apic enabled or disabled */
-    int vcpus; /* max number of vcpus */
-    int vcpu_avail; /* vcpus actually available */
-    int xen_platform_pci; /* enable/disable the xen platform pci device */
-    libxl_string_list extra; /* extra parameters pass directly to qemu, NULL terminated */
-    /* Network is missing */
+    /*
+     * size of the videoram in MB
+     */
+    int videoram;
+    /*
+     * stdvga enabled or disabled
+     */
+    bool stdvga;
+    /*
+     * vnc enabled or disabled
+     */
+    bool vnc;
+    /*
+     * address:port that should be listened on for the VNC server if vnc is set
+     */
+    char * vnclisten;
+    /*
+     * the VNC password
+     */
+    char * vncpasswd;
+    /*
+     * set VNC display number
+     */
+    int vncdisplay;
+    /*
+     * try to find an unused port for the VNC server
+     */
+    bool vncunused;
+    /*
+     * set keyboard layout, default is en-us keyboard
+     */
+    char * keymap;
+    /*
+     * sdl enabled or disabled
+     */
+    bool sdl;
+    /*
+     * opengl enabled or disabled (if enabled requires sdl enabled)
+     */
+    bool opengl;
+    /*
+     * no graphics, use serial port
+     */
+    bool nographic;
+    /*
+     * serial port re-direct to pty deivce
+     */
+    char * serial;
+    /*
+     * boot order, for example dca
+     */
+    char * boot;
+    /*
+     * usb support enabled or disabled
+     */
+    bool usb;
+    /*
+     * enable usb mouse: tablet for absolute mouse, mouse for PS/2 protocol relative mouse
+     */
+    char * usbdevice;
+    /*
+     * enable sound hardware
+     */
+    char * soundhw;
+    /*
+     * apic enabled or disabled
+     */
+    bool apic;
+    /*
+     * max number of vcpus
+     */
+    int vcpus;
+    /*
+     * vcpus actually available
+     */
+    int vcpu_avail;
+    /*
+     * enable/disable the xen platform pci device
+     */
+    int xen_platform_pci;
+    /*
+     * extra parameters pass directly to qemu, NULL terminated
+     */
+    libxl_string_list extra;
 } libxl_device_model_info;
 
 typedef struct {
     uint32_t backend_domid;
     uint32_t domid;
     int devid;
-    bool vnc; /* vnc enabled or disabled */
-    char *vnclisten; /* address:port that should be listened on for the VNC server if vnc is set */
-    char *vncpasswd; /* the VNC password */
-    int vncdisplay; /* set VNC display number */
-    bool vncunused; /* try to find an unused port for the VNC server */
-    char *keymap; /* set keyboard layout, default is en-us keyboard */
-    bool sdl; /* sdl enabled or disabled */
-    bool opengl; /* opengl enabled or disabled (if enabled requires sdl enabled) */
-    char *display;
-    char *xauthority;
+    /*
+     * vnc enabled or disabled
+     */
+    bool vnc;
+    /*
+     * address:port that should be listened on for the VNC server if vnc is set
+     */
+    char * vnclisten;
+    /*
+     * the VNC password
+     */
+    char * vncpasswd;
+    /*
+     * set VNC display number
+     */
+    int vncdisplay;
+    /*
+     * try to find an unused port for the VNC server
+     */
+    bool vncunused;
+    /*
+     * set keyboard layout, default is en-us keyboard
+     */
+    char * keymap;
+    /*
+     * sdl enabled or disabled
+     */
+    bool sdl;
+    /*
+     * opengl enabled or disabled (if enabled requires sdl enabled)
+     */
+    bool opengl;
+    char * display;
+    char * xauthority;
 } libxl_device_vfb;
 
 typedef struct {
@@ -178,16 +270,16 @@ typedef struct {
     uint32_t domid;
     int devid;
     libxl_console_consback consback;
-    libxl_domain_build_state *build_state;
-    char *output;
+    libxl_domain_build_state * build_state;
+    char * output;
 } libxl_device_console;
 
 typedef struct {
     uint32_t backend_domid;
     uint32_t domid;
-    char *physpath;
+    char * physpath;
     libxl_disk_phystype phystype;
-    char *virtpath;
+    char * virtpath;
     int unpluggable;
     int readwrite;
     int is_cdrom;
@@ -198,12 +290,12 @@ typedef struct {
     uint32_t domid;
     int devid;
     int mtu;
-    char *model;
+    char * model;
     libxl_mac mac;
     struct in_addr ip;
-    char *bridge;
-    char *ifname;
-    char *script;
+    char * bridge;
+    char * ifname;
+    char * script;
     libxl_nic_type nictype;
 } libxl_device_nic;
 
@@ -219,7 +311,7 @@ typedef struct {
     uint32_t front_filter_mac:1;
     uint32_t pdev;
     uint32_t max_bypasses;
-    char *bridge;
+    char * bridge;
 } libxl_device_net2;
 
 typedef struct {
@@ -243,9 +335,9 @@ typedef struct {
 } libxl_device_pci;
 
 typedef struct {
-    char *backend;
+    char * backend;
     uint32_t backend_id;
-    char *frontend;
+    char * frontend;
     uint32_t frontend_id;
     int devid;
     int state;
@@ -254,13 +346,13 @@ typedef struct {
 } libxl_diskinfo;
 
 typedef struct {
-    char *backend;
+    char * backend;
     uint32_t backend_id;
-    char *frontend;
+    char * frontend;
     uint32_t frontend_id;
     int devid;
     int state;
-    char *script;
+    char * script;
     libxl_mac mac;
     int evtch;
     int rref_tx;
@@ -268,31 +360,49 @@ typedef struct {
 } libxl_nicinfo;
 
 typedef struct {
-    uint32_t vcpuid; /* vcpu's id */
-    uint32_t cpu; /* current mapping */
-    uint8_t online:1; /* currently online (not hotplugged)? */
-    uint8_t blocked:1; /* blocked waiting for an event? */
-    uint8_t running:1; /* currently scheduled on its CPU? */
-    uint64_t vcpu_time; /* total vcpu time ran (ns) */
-    libxl_cpumap cpumap; /* current cpu's affinities */
+    /*
+     * vcpu's id
+     */
+    uint32_t vcpuid;
+    /*
+     * current mapping
+     */
+    uint32_t cpu;
+    /*
+     * currently online (not hotplugged)?
+     */
+    uint8_t online:1;
+    /*
+     * blocked waiting for an event?
+     */
+    uint8_t blocked:1;
+    /*
+     * currently scheduled on its CPU?
+     */
+    uint8_t running:1;
+    /*
+     * total vcpu time ran (ns)
+     */
+    uint64_t vcpu_time;
+    /*
+     * current cpu's affinities
+     */
+    libxl_cpumap cpumap;
 } libxl_vcpuinfo;
 
 typedef struct {
     uint32_t threads_per_core;
     uint32_t cores_per_socket;
-
     uint32_t max_cpu_id;
     uint32_t nr_cpus;
     uint32_t cpu_khz;
-
     uint64_t total_pages;
     uint64_t free_pages;
     uint64_t scrub_pages;
-
     uint32_t nr_nodes;
     libxl_hwcap hw_cap;
     uint32_t phys_cap;
-}  libxl_physinfo;
+} libxl_physinfo;
 
 typedef struct {
     int weight;
@@ -300,9 +410,9 @@ typedef struct {
 } libxl_sched_credit;
 
 typedef struct {
-    char *backend;
+    char * backend;
     uint32_t backend_id;
-    char *frontend;
+    char * frontend;
     uint32_t frontend_id;
     int devid;
     int state;
#11 lore raw

PATCH 1/1 libxl: autogenerate _libxl_types.h

In reply to Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]> # Date 1281969065 -3600
# Node ID d8761fd067bebad80872da3bb7b5c3d373ba63eb # Parent 01f64d048f0fef53e638169a204fa0649a9245d1
libxl: autogenerate _libxl_types.h

The libxl interface types are represented by a simple python data structure (which could be parsed from a bespoke language in the future).

This will allow the autogeneration of functions to free the component members of the libxl types. In the future it may also enable auto generation of type marshalling code for language bindings.

The generated file should be identical to before with the exception of the "DO NOT EDIT" header.

It was unfortunately necessary to add explcit an dependency on _libxl_types.h (indirectly via libxl.h) to all C files since the autogenerated dependencies are not available in time.

tools/libxl/Makefile +8 -3
diff -r 01f64d048f0f -r d8761fd067be tools/libxl/Makefile
@@ -54,6 +54,14 @@ _libxl_paths.h: genpath
 
 libxl_paths.c: _libxl_paths.h
 
+libxl.h: _libxl_types.h
+
+$(LIBXL_OBJS:%.o=%.c) $(LIBXLU_OBJS:%.o=%.c) $(XL_OBJS:%.o=%.c): libxl.h
+
+_libxl_types.h: libxltypes.idl gentypes.py libxltypes.py
+	python gentypes.py libxltypes.idl  __libxl_types.h
+	mv __libxl_types.h _libxl_types.h
+
 libxenlight.so: libxenlight.so.$(MAJOR)
 	ln -sf $< $@
 
@@ -101,10 +109,7 @@ install: all
 
 .PHONY: clean
 clean:
-	# XXX Preserve during transition to autogeneration
-	cp _libxl_types.h SAVED__libxl_types.h
 	$(RM) -f _*.h *.o *.so* *.a $(CLIENTS) $(DEPS)
-	mv SAVED__libxl_types.h _libxl_types.h 
 #	$(RM) -f $(AUTOSRCS) $(AUTOINCS)
 
 distclean: clean
tools/libxl/_libxl_types.h deleted +0 -425

425 lines not on this page · raw message

tools/libxl/gentypes.py new file +91 -0

91 lines not on this page · raw message

tools/libxl/libxltypes.idl new file +329 -0

329 lines not on this page · raw message

tools/libxl/libxltypes.py new file +168 -0

168 lines not on this page · raw message

#12 lore raw

Re: [PATCH 11 of 26] libxl: autogenerate _libxl_types.h

Quoted 15 lines · Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]> # Date 1281969065 -3600
# Node ID d8761fd067bebad80872da3bb7b5c3d373ba63eb # Parent 01f64d048f0fef53e638169a204fa0649a9245d1
libxl: autogenerate _libxl_types.h

The libxl interface types are represented by a simple python data structure (which could be parsed from a bespoke language in the future).

This will allow the autogeneration of functions to free the component members of the libxl types. In the future it may also enable auto generation of type marshalling code for language bindings.

Quoted 9 lines

The generated file should be identical to before with the exception of the "DO NOT EDIT" header.

It was unfortunately necessary to add explcit an dependency on _libxl_types.h (indirectly via libxl.h) to all C files since the autogenerated dependencies are not available in time.

I think we need a document or at least a link to a document that describes in details the idl language we are using.

#13 lore raw

Re: [PATCH 11 of 26] libxl: autogenerate _libxl_types.h

Quoted 23 lines · Stefano Stabellini
Quoted 22 lines · Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]> # Date 1281969065 -3600
# Node ID d8761fd067bebad80872da3bb7b5c3d373ba63eb # Parent 01f64d048f0fef53e638169a204fa0649a9245d1
libxl: autogenerate _libxl_types.h

The libxl interface types are represented by a simple python data structure (which could be parsed from a bespoke language in the future).

This will allow the autogeneration of functions to free the component members of the libxl types. In the future it may also enable auto generation of type marshalling code for language bindings.

The generated file should be identical to before with the exception of the "DO NOT EDIT" header.

It was unfortunately necessary to add explcit an dependency on _libxl_types.h (indirectly via libxl.h) to all C files since the autogenerated dependencies are not available in time.

Quoted 5 lines
Quoted 2 lines

I think we need a document or at least a link to a document that describes in details the idl language we are using.

I'll write something up representing the current state. Since it is really just python it's likely to be a bit fluid I suspect.

Ian.

#14 lore raw

PATCH 1/1 libxl: generate destructors for each libxl defined type

In reply to Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]> # Date 1281969065 -3600
# Node ID 5567b7c1b303d6f54d76a0efe63353701a12dad3 # Parent d8761fd067bebad80872da3bb7b5c3d373ba63eb
libxl: generate destructors for each libxl defined type

I chose the name "_destroy" rather than "_free" because the destructor functions will free only the members of a type recursively but will not free the actual type structure itself. The allocation of the type is typically done by the caller and may not be a single allocation, e.g. lists/arrays of types or embedded in other structures etc.

The exceptions to this rule are libxl_string_list_destroy and libxl_key_value_list_destroy but I'm not 100% convinced they are exceptions (since they are kind-of opaque) and I couldn't see a cleanerway to express this concept. I have made a best effort attempt to implement these functions sanely but since as far as I can tell nothing in the current code base ever sets libxl_domain_create_info.{xsdata,platformdata} I'm flying somewhat blind.

.hgignore +1 -0
diff -r d8761fd067be -r 5567b7c1b303 .hgignore
@@ -182,6 +182,7 @@
 ^tools/libxen/test/test_bindings$
 ^tools/libxen/test/test_event_handling$
 ^tools/libxl/_.*\.h$
+^tools/libxl/_.*\.c$
 ^tools/libxl/libxlu_cfg_y\.output$
 ^tools/libxl/xl$
 ^tools/libaio/src/.*\.ol$
tools/libxl/Makefile +5 -2
diff -r d8761fd067be -r 5567b7c1b303 tools/libxl/Makefile
@@ -19,6 +19,7 @@ LIBS = $(LDLIBS_libxenctrl) $(LDLIBS_lib
 
 LIBXL_OBJS-y = osdeps.o libxl_paths.o libxl_bootloader.o
 LIBXL_OBJS = flexarray.o libxl.o libxl_pci.o libxl_dom.o libxl_exec.o libxl_xshelp.o libxl_device.o libxl_internal.o xenguest.o libxl_utils.o $(LIBXL_OBJS-y)
+LIBXL_OBJS += _libxl_types.o
 
 AUTOINCS= libxlu_cfg_y.h libxlu_cfg_l.h
 AUTOSRCS= libxlu_cfg_y.c libxlu_cfg_l.c
@@ -58,9 +59,10 @@ libxl.h: _libxl_types.h
 
 $(LIBXL_OBJS:%.o=%.c) $(LIBXLU_OBJS:%.o=%.c) $(XL_OBJS:%.o=%.c): libxl.h
 
-_libxl_types.h: libxltypes.idl gentypes.py libxltypes.py
-	python gentypes.py libxltypes.idl  __libxl_types.h
+_libxl_types.h _libxl_types.c: libxltypes.idl gentypes.py libxltypes.py
+	python gentypes.py libxltypes.idl __libxl_types.h __libxl_types.c
 	mv __libxl_types.h _libxl_types.h
+	mv __libxl_types.c _libxl_types.c
 
 libxenlight.so: libxenlight.so.$(MAJOR)
 	ln -sf $< $@
@@ -110,6 +112,7 @@ install: all
 .PHONY: clean
 clean:
 	$(RM) -f _*.h *.o *.so* *.a $(CLIENTS) $(DEPS)
+	$(RM) -f _*.c
 #	$(RM) -f $(AUTOSRCS) $(AUTOINCS)
 
 distclean: clean
tools/libxl/gentypes.py +73 -5

97 lines not on this page · raw message

tools/libxl/libxl.c +27 -0
diff -r d8761fd067be -r 5567b7c1b303 tools/libxl/libxl.c
@@ -72,6 +72,33 @@ int libxl_ctx_free(libxl_ctx *ctx)
     do_free_version_info(&ctx->version_info);
     if (ctx->xsh) xs_daemon_close(ctx->xsh); 
     return 0;
+}
+
+void libxl_string_list_destroy(libxl_string_list sl)
+{
+    int i;
+
+    if (!sl)
+        return;
+
+    for (i = 0; sl[i] != NULL; i++)
+        free(sl[i]);
+    free(sl);
+}
+
+void libxl_key_value_list_destroy(libxl_key_value_list kvl)
+{
+    int i;
+
+    if (!kvl)
+        return;
+
+    for (i = 0; kvl[i] != NULL; i += 2) {
+        free(kvl[i]);
+        if (kvl[i + 1])
+            free(kvl[i + 1]);
+    }
+    free(kvl);
 }
 
 /******************************************************************************/
tools/libxl/libxl.h +2 -0
diff -r d8761fd067be -r 5567b7c1b303 tools/libxl/libxl.h
@@ -142,8 +142,10 @@ typedef uint8_t libxl_mac[6];
 typedef uint8_t libxl_mac[6];
 
 typedef char **libxl_string_list;
+void libxl_string_list_destroy(libxl_string_list sl);
 
 typedef char **libxl_key_value_list;
+void libxl_key_value_list_destroy(libxl_key_value_list kvl);
 
 typedef uint64_t *libxl_cpumap;
 
tools/libxl/libxltypes.idl +3 -3
diff -r d8761fd067be -r 5567b7c1b303 tools/libxl/libxltypes.idl
@@ -12,10 +12,10 @@ libxl_disk_phystype = Builtin("disk_phys
 libxl_disk_phystype = Builtin("disk_phystype")
 libxl_nic_type = Builtin("nic_type")
 
-libxl_string_list = Builtin("string_list")
-libxl_key_value_list = Builtin("key_value_list")
+libxl_string_list = Builtin("string_list", destructor_fn="libxl_string_list_destroy")
+libxl_key_value_list = Builtin("key_value_list", destructor_fn="libxl_key_value_list_destroy")
 
-libxl_cpumap = Builtin("cpumap")
+libxl_cpumap = Builtin("cpumap", destructor_fn="free")
 
 libxl_hwcap = Builtin("hwcap")
 
tools/libxl/libxltypes.py +31 -1
diff -r d8761fd067be -r 5567b7c1b303 tools/libxl/libxltypes.py
@@ -1,9 +1,16 @@ import sys
 import sys
+
+PASS_BY_VALUE = 1
+PASS_BY_REFERENCE = 2
 
 class Type(object):
     def __init__(self, typename, **kwargs):
         self.comment = kwargs.setdefault('comment', None)
         self.namespace = kwargs.setdefault('namespace', "libxl_")
+
+        self.passby = kwargs.setdefault('passby', PASS_BY_VALUE)
+        if self.passby not in [PASS_BY_VALUE, PASS_BY_REFERENCE]:
+            raise ValueError
 
         if typename is None: # Anonymous type
             self.typename = None
@@ -12,14 +19,23 @@ class Type(object):
         else:
             self.typename = self.namespace + typename
 
+        if self.typename is not None:
+            self.destructor_fn = kwargs.setdefault('destructor_fn', self.typename + "_destroy")
+        else:
+            self.destructor_fn = kwargs.setdefault('destructor_fn', None)
+
+        self.autogenerate_destructor = kwargs.setdefault('autogenerate_destructor', True)
+
 class Builtin(Type):
     """Builtin type"""
     def __init__(self, typename, **kwargs):
+        kwargs.setdefault('destructor_fn', None)
         Type.__init__(self, typename, **kwargs)
 
 class UInt(Type):
     def __init__(self, w, **kwargs):
         kwargs.setdefault('namespace', None)
+        kwargs.setdefault('destructor_fn', None)
         Type.__init__(self, "uint%d_t" % w, **kwargs)
 
         self.width = w
@@ -27,6 +43,7 @@ class BitField(Type):
 class BitField(Type):
     def __init__(self, ty, w, **kwargs):
         kwargs.setdefault('namespace', None)
+        kwargs.setdefault('destructor_fn', None)
         Type.__init__(self, ty.typename, **kwargs)
 
         self.width = w
@@ -63,10 +80,16 @@ class Aggregate(Type):
 
 class Struct(Aggregate):
     def __init__(self, name, fields, **kwargs):
+        kwargs.setdefault('passby', PASS_BY_REFERENCE)
         Aggregate.__init__(self, "struct", name, fields, **kwargs)
 
 class Union(Aggregate):
     def __init__(self, name, fields, **kwargs):
+        # Generally speaking some intelligence is required to free a
+        # union therefore any specific instance of this class will
+        # need to provide an explicit destructor function.
+        kwargs.setdefault('passby', PASS_BY_REFERENCE)
+        kwargs.setdefault('destructor_fn', None)
         Aggregate.__init__(self, "union", name, fields, **kwargs)
 
 class KeyedUnion(Aggregate):
@@ -87,7 +110,14 @@ class Reference(Type):
 class Reference(Type):
     """A reference to another type"""
     def __init__(self, ty, **kwargs):
+        self.ref_type = ty
+        
         # Ugh
+        
+        kwargs.setdefault('destructor_fn', "free")
+        kwargs.setdefault('autogenerate_destructor', False)
+        kwargs.setdefault('passby', PASS_BY_VALUE)
+        
         kwargs.setdefault('namespace', ty.namespace)
         typename = ty.typename[len(kwargs['namespace']):]
         Type.__init__(self, typename + " *", **kwargs)
@@ -112,7 +142,7 @@ uint64 = UInt(64)
 
 domid = UInt(32)
 
-string = Builtin("char *", namespace = None)
+string = Builtin("char *", namespace = None, destructor_fn = "free")
 
 inaddr_ip = Builtin("struct in_addr", namespace = None)
#15 lore raw

PATCH 1/1 libxl: libxl_device_console.build_state is const

In reply to Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]> # Date 1281969065 -3600
# Node ID f8c43de0c6d661c1662c8a34d5838b8755ae8fde # Parent 5567b7c1b303d6f54d76a0efe63353701a12dad3
libxl: libxl_device_console.build_state is const

This field always refers to a libxl_domain_build_state structure which is managed already by the caller, therefore it is incorrect to free it as part of libxl_device_console_destroy.

(arguably this field should be a copy of, not a reference too, the original)

tools/libxl/libxltypes.idl +1 -1
diff -r 5567b7c1b303 -r f8c43de0c6d6 tools/libxl/libxltypes.idl
@@ -195,7 +195,7 @@ libxl_device_console = Struct("device_co
     ("domid", domid),
     ("devid", integer),
     ("consback", libxl_console_consback),
-    ("build_state", Reference(libxl_domain_build_state)),
+    ("build_state", Reference(libxl_domain_build_state), True),
     ("output", string),
     ])
#16 lore raw

PATCH 1/1 libxl: build info bootloader{, _args} are not const

In reply to Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]> # Date 1281969065 -3600
# Node ID 7d3021e9dcfbaaa18481c93b854733726c4d6267 # Parent f8c43de0c6d661c1662c8a34d5838b8755ae8fde
libxl: build info bootloader{,_args} are not const.

tools/libxl/libxltypes.idl +2 -2
diff -r f8c43de0c6d6 -r 7d3021e9dcfb tools/libxl/libxltypes.idl
@@ -112,8 +112,8 @@ libxl_domain_build_info = Struct("domain
                                         ])),
                  ("pv", "!%s", Struct(None,
                                        [("slack_memkb", uint32),
-                                        ("bootloader", string, True),
-                                        ("bootloader_args", string, True),
+                                        ("bootloader", string),
+                                        ("bootloader_args", string),
                                         ("cmdline", string),
                                         ("ramdisk", libxl_file_reference),
                                         ("features", string, True),
#17 lore raw

PATCH 1/1 libxl: do not generate a destructor for data types which do not require one

In reply to Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1281969065 -3600
# Node ID 4c0e7313a88fc4219f37cf48177a1c807ca4d22b
# Parent 7d3021e9dcfbaaa18481c93b854733726c4d6267
libxl: do not generate a destructor for data types which do not require one

libxl_dominfo, libxl_poolinfo, libxl_vminfo, libxl_domain_build_state, libxl_physinfo and libxl_sched_credit contain no members which require destruction so omit the auto generated destructor.

As an exception continue to generate destructors for libxl_device_* and libxl_{devtype}* even if they have no interesting members so that all device types can be treated the same without special knowledge about the type contents being required.

tools/libxl/gentypes.py +1 -1
diff -r 7d3021e9dcfb -r 4c0e7313a88f tools/libxl/gentypes.py
@@ -150,7 +150,7 @@ if __name__ == '__main__':
 
 """ % " ".join(sys.argv))
 
-    for ty in [t for t in types if t.autogenerate_destructor]:
+    for ty in [t for t in types if t.destructor_fn is not None and t.autogenerate_destructor]:
         f.write("void %s(%s *p)\n" % (ty.destructor_fn, ty.typename))
         f.write("{\n")
         f.write(libxl_C_type_destroy(ty, "p", True))
tools/libxl/libxltypes.idl +6 -6
diff -r 7d3021e9dcfb -r 4c0e7313a88f tools/libxl/libxltypes.idl
@@ -40,16 +40,16 @@ SHUTDOWN_* constant."""),
     ("cpu_time",    uint64),
     ("vcpu_max_id", uint32),
     ("vcpu_online", uint32),
-    ])
+    ], destructor_fn=None)
 
 libxl_poolinfo = Struct("poolinfo", [
     ("poolid", uint32)
-    ])
+    ], destructor_fn=None)
 
 libxl_vminfo = Struct("vminfo", [
     ("uuid", libxl_uuid),
     ("domid", domid),
-    ])
+    ], destructor_fn=None)
 
 libxl_version_info = Struct("version_info", [
     ("xen_version_major", integer),
@@ -132,7 +132,7 @@ libxl_domain_build_state = Struct("domai
     ("store_mfn",    unsigned_long),
     ("console_port", uint32),
     ("console_mfn",  unsigned_long),
-    ])
+    ], destructor_fn=None)
 
 libxl_device_model_info = Struct("device_model_info",[
     ("domid",            integer),
@@ -308,12 +308,12 @@ libxl_physinfo = Struct("physinfo", [
     ("nr_nodes", uint32),
     ("hw_cap", libxl_hwcap),
     ("phys_cap", uint32),
-    ])
+    ], destructor_fn=None)
 
 libxl_sched_credit = Struct("sched_credit", [
     ("weight", integer),
     ("cap", integer),
-    ])
+    ], destructor_fn=None)
 
 libxl_net2info = Struct("net2info", [
     ("backend", string),
#18 lore raw

PATCH 1/1 libxl: implement destroy for libxl_file_reference builtin type

In reply to Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1281969065 -3600
# Node ID d91c2cef85ea93062cdbd23c81e4990567c0ba25
# Parent 4c0e7313a88fc4219f37cf48177a1c807ca4d22b
libxl: implement destroy for libxl_file_reference builtin type

As well as freeing data any file mappings need to be torn down so implement an explicit destroy function.

Also the map and unmap function are internal to libxl so make that so.

tools/libxl/libxl.c +7 -47
diff -r 4c0e7313a88f -r d91c2cef85ea tools/libxl/libxl.c
@@ -23,7 +23,6 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <sys/select.h>
-#include <sys/mman.h>
 #include <sys/wait.h>
 #include <sys/time.h>
 #include <signal.h>
@@ -341,9 +340,9 @@ int libxl_domain_build(libxl_ctx *ctx, l
     }
     ret = build_post(ctx, domid, info, state, vments, localents);
 out:
-    libxl_file_reference_unmap(ctx, &info->kernel);
+    libxl__file_reference_unmap(&info->kernel);
     if (!info->hvm)
-	    libxl_file_reference_unmap(ctx, &info->u.pv.ramdisk);
+	    libxl__file_reference_unmap(&info->u.pv.ramdisk);
 
     libxl_free_all(&gc);
     return ret;
@@ -406,9 +405,9 @@ int libxl_domain_restore(libxl_ctx *ctx,
     }
 
 out:
-    libxl_file_reference_unmap(ctx, &info->kernel);
+    libxl__file_reference_unmap(&info->kernel);
     if (!info->hvm)
-	    libxl_file_reference_unmap(ctx, &info->u.pv.ramdisk);
+	    libxl__file_reference_unmap(&info->u.pv.ramdisk);
 
     esave = errno;
 
@@ -3371,47 +3370,8 @@ int libxl_tmem_freeable(libxl_ctx *ctx)
     return rc;
 }
 
-int libxl_file_reference_map(libxl_ctx *ctx, libxl_file_reference *f)
+void libxl_file_reference_destroy(libxl_file_reference *f)
 {
-	struct stat st_buf;
-	int ret, fd;
-	void *data;
-
-	if (f->mapped)
-		return 0;
-
-	fd = open(f->path, O_RDONLY);
-	if (f < 0)
-		return ERROR_FAIL;
-
-	ret = fstat(fd, &st_buf);
-	if (ret < 0)
-		goto out;
-
-	ret = -1;
-	data = mmap(NULL, st_buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
-	if (data == NULL)
-		goto out;
-
-	f->mapped = 1;
-	f->data = data;
-	f->size = st_buf.st_size;
-
-	ret = 0;
-out:
-	close(fd);
-
-	return ret == 0 ? 0 : ERROR_FAIL;
+    libxl__file_reference_unmap(f);
+    free(f->path);
 }
-
-int libxl_file_reference_unmap(libxl_ctx *ctx, libxl_file_reference *f)
-{
-	int ret;
-
-	if (!f->mapped)
-		return 0;
-
-	ret = munmap(f->data, f->size);
-
-	return ret == 0 ? 0 : ERROR_FAIL;
-}
tools/libxl/libxl.h +4 -4
diff -r 4c0e7313a88f -r d91c2cef85ea tools/libxl/libxl.h
@@ -142,10 +142,8 @@ typedef uint8_t libxl_mac[6];
 typedef uint8_t libxl_mac[6];
 
 typedef char **libxl_string_list;
-void libxl_string_list_destroy(libxl_string_list sl);
 
 typedef char **libxl_key_value_list;
-void libxl_key_value_list_destroy(libxl_key_value_list kvl);
 
 typedef uint64_t *libxl_cpumap;
 
@@ -235,8 +233,10 @@ int libxl_domain_destroy(libxl_ctx *ctx,
 int libxl_domain_destroy(libxl_ctx *ctx, uint32_t domid, int force);
 int libxl_domain_preserve(libxl_ctx *ctx, uint32_t domid, libxl_domain_create_info *info, const char *name_suffix, libxl_uuid new_uuid);
 
-int libxl_file_reference_map(libxl_ctx *ctx, libxl_file_reference *f);
-int libxl_file_reference_unmap(libxl_ctx *ctx, libxl_file_reference *f);
+/* destructors for builtin data types */
+void libxl_string_list_destroy(libxl_string_list sl);
+void libxl_key_value_list_destroy(libxl_key_value_list kvl);
+void libxl_file_reference_destroy(libxl_file_reference *f);
 
 /*
  * Run the configured bootloader for a PV domain and update
tools/libxl/libxl_bootloader.c +2 -2
diff -r 4c0e7313a88f -r d91c2cef85ea tools/libxl/libxl_bootloader.c
@@ -279,12 +279,12 @@ static void parse_bootloader_result(libx
         if (strncmp("kernel ", o, strlen("kernel ")) == 0) {
             free(info->kernel.path);
             info->kernel.path = strdup(o + strlen("kernel "));
-            libxl_file_reference_map(ctx, &info->kernel);
+            libxl__file_reference_map(&info->kernel);
             unlink(info->kernel.path);
         } else if (strncmp("ramdisk ", o, strlen("ramdisk ")) == 0) {
             free(info->u.pv.ramdisk.path);
             info->u.pv.ramdisk.path = strdup(o + strlen("ramdisk "));
-            libxl_file_reference_map(ctx, &info->u.pv.ramdisk);
+            libxl__file_reference_map(&info->u.pv.ramdisk);
             unlink(info->u.pv.ramdisk.path);
         } else if (strncmp("args ", o, strlen("args ")) == 0) {
             free(info->u.pv.cmdline);
tools/libxl/libxl_internal.c +51 -0

60 lines not on this page · raw message

tools/libxl/libxl_internal.h +3 -0
diff -r 4c0e7313a88f -r d91c2cef85ea tools/libxl/libxl_internal.h
@@ -253,4 +253,7 @@ struct libxl__xen_console_reader {
     unsigned int index;
 };
 
+_hidden int libxl__file_reference_map(libxl_file_reference *f);
+_hidden int libxl__file_reference_unmap(libxl_file_reference *f);
+
 #endif
tools/libxl/libxltypes.idl +1 -1
diff -r 4c0e7313a88f -r d91c2cef85ea tools/libxl/libxltypes.idl
@@ -85,7 +85,7 @@ mapped is true then the actual file may 
 mapped is true then the actual file may already be unlinked."""),
     ("mapped", integer),
     ("data", void),
-    ("size", size_t)])
+    ("size", size_t)], autogenerate_destructor=False)
 
 libxl_domain_build_info = Struct("domain_build_info",[
     ("max_vcpus",       integer),
#19 lore raw

PATCH 1/1 xl: free the libxl types contained in struct domain_config

In reply to Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1281969065 -3600
# Node ID f00e5b9e555fee97d60a98fe11a6d856b27bb3b0 # Parent d91c2cef85ea93062cdbd23c81e4990567c0ba25
xl: free the libxl types contained in struct domain_config

Resolves the following leaks found using "valgrind xl create -n ..." (plus presumably others which didn't trigger because I have no devices of that type)

==21241== 5 bytes in 1 blocks are definitely lost in loss record 1 of 6

==21241==    at 0x4022F0A: malloc (vg_replace_malloc.c:236)
==21241==    by 0x411B22F: strdup (in /lib/i686/cmov/libc-2.7.so)
==21241==    by 0x804E56B: parse_config_data (xl_cmdimpl.c:760)
==21241==    by 0x8056B04: create_domain (xl_cmdimpl.c:1357)
==21241==    by 0x8057ECE: main_create (xl_cmdimpl.c:3154)
==21241==    by 0x804B2FB: main (xl.c:76)
==21241==
==21241== 6 bytes in 1 blocks are definitely lost in loss record 2 of 6
==21241==    at 0x4022F0A: malloc (vg_replace_malloc.c:236)
==21241==    by 0x411B22F: strdup (in /lib/i686/cmov/libc-2.7.so)
==21241==    by 0x804DAAA: parse_config_data (xl_cmdimpl.c:586)
==21241==    by 0x8056B04: create_domain (xl_cmdimpl.c:1357)
==21241==    by 0x8057ECE: main_create (xl_cmdimpl.c:3154)
==21241==    by 0x804B2FB: main (xl.c:76)
==21241==
==21241== 24 bytes in 1 blocks are definitely lost in loss record 3 of 6
==21241==    at 0x4022F0A: malloc (vg_replace_malloc.c:236)
==21241==    by 0x411B22F: strdup (in /lib/i686/cmov/libc-2.7.so)
==21241==    by 0x804E51F: parse_config_data (xl_cmdimpl.c:754)
==21241==    by 0x8056B04: create_domain (xl_cmdimpl.c:1357)
==21241==    by 0x8057ECE: main_create (xl_cmdimpl.c:3154)
==21241==    by 0x804B2FB: main (xl.c:76)
==21241==
==21241== 27 bytes in 1 blocks are definitely lost in loss record 4 of 6
==21241==    at 0x4022F0A: malloc (vg_replace_malloc.c:236)
==21241==    by 0x411B22F: strdup (in /lib/i686/cmov/libc-2.7.so)
==21241==    by 0x804DF4A: parse_config_data (xl_cmdimpl.c:664)
==21241==    by 0x8056B04: create_domain (xl_cmdimpl.c:1357)
==21241==    by 0x8057ECE: main_create (xl_cmdimpl.c:3154)
==21241==    by 0x804B2FB: main (xl.c:76)
==21241==
==21241== 28 bytes in 1 blocks are definitely lost in loss record 5 of 6
==21241==    at 0x4022F0A: malloc (vg_replace_malloc.c:236)
==21241==    by 0x410C1A7: vasprintf (in /lib/i686/cmov/libc-2.7.so)
==21241==    by 0x40EE59A: asprintf (in /lib/i686/cmov/libc-2.7.so)
==21241==    by 0x804CA8B: init_nic_info (xl_cmdimpl.c:322)
==21241==    by 0x804E6FF: parse_config_data (xl_cmdimpl.c:791)
==21241==    by 0x8056B04: create_domain (xl_cmdimpl.c:1357)
==21241==    by 0x8057ECE: main_create (xl_cmdimpl.c:3154)
==21241==    by 0x804B2FB: main (xl.c:76)
==21241==
==21241== 44 bytes in 1 blocks are definitely lost in loss record 6 of 6
==21241==    at 0x4022F0A: malloc (vg_replace_malloc.c:236)
==21241==    by 0x411B22F: strdup (in /lib/i686/cmov/libc-2.7.so)
==21241==    by 0x804E168: parse_config_data (xl_cmdimpl.c:694)
==21241==    by 0x8056B04: create_domain (xl_cmdimpl.c:1357)
==21241==    by 0x8057ECE: main_create (xl_cmdimpl.c:3154)
==21241==    by 0x804B2FB: main (xl.c:76)
tools/libxl/xl_cmdimpl.c +26 -2
diff -r d91c2cef85ea -r f00e5b9e555f tools/libxl/xl_cmdimpl.c
@@ -144,12 +144,34 @@ struct domain_config {
 
 static void free_domain_config(struct domain_config *d_config)
 {
+    int i;
+
+    for (i=0; i<d_config->num_disks; i++)
+        libxl_device_disk_destroy(&d_config->disks[i]);
     free(d_config->disks);
+
+    for (i=0; i<d_config->num_vifs; i++)
+        libxl_device_nic_destroy(&d_config->vifs[i]);
     free(d_config->vifs);
+
+    for (i=0; i<d_config->num_vif2s; i++)
+        libxl_device_net2_destroy(&d_config->vif2s[i]);
     free(d_config->vif2s);
+
+    for (i=0; i<d_config->num_pcidevs; i++)
+        libxl_device_pci_destroy(&d_config->pcidevs[i]);
     free(d_config->pcidevs);
+
+    for (i=0; i<d_config->num_vfbs; i++)
+        libxl_device_vfb_destroy(&d_config->vfbs[i]);
     free(d_config->vfbs);
+
+    for (i=0; i<d_config->num_vkbs; i++)
+        libxl_device_vkb_destroy(&d_config->vkbs[i]);
     free(d_config->vkbs);
+
+    libxl_domain_create_info_destroy(&d_config->c_info);
+    libxl_domain_build_info_destroy(&d_config->b_info);
 }
 
 /* Optional data, in order:
@@ -309,7 +331,7 @@ static void init_nic_info(libxl_device_n
     nic_info->domid = 0;
     nic_info->devid = devnum;
     nic_info->mtu = 1492;
-    nic_info->model = "e1000";
+    nic_info->model = strdup("e1000");
     nic_info->mac[0] = 0x00;
     nic_info->mac[1] = 0x16;
     nic_info->mac[2] = 0x3e;
@@ -317,7 +339,7 @@ static void init_nic_info(libxl_device_n
     nic_info->mac[4] = 1 + (int) (0xff * (rand() / (RAND_MAX + 1.0)));
     nic_info->mac[5] = 1 + (int) (0xff * (rand() / (RAND_MAX + 1.0)));
     nic_info->ifname = NULL;
-    nic_info->bridge = "xenbr0";
+    nic_info->bridge = strdup("xenbr0");
     CHK_ERRNO( asprintf(&nic_info->script, "%s/vif-bridge",
                libxl_xen_script_dir_path()) );
     nic_info->nictype = NICTYPE_IOEMU;
@@ -796,6 +818,7 @@ static void parse_config_data(const char
                     break;
                 *p2 = '\0';
                 if (!strcmp(p, "model")) {
+                    free(nic->model);
                     nic->model = strdup(p2 + 1);
                 } else if (!strcmp(p, "mac")) {
                     char *p3 = p2 + 1;
@@ -817,6 +840,7 @@ static void parse_config_data(const char
                     *(p3 + 2) = '\0';
                     nic->mac[5] = strtol(p3, NULL, 16);
                 } else if (!strcmp(p, "bridge")) {
+                    free(nic->bridge);
                     nic->bridge = strdup(p2 + 1);
                 } else if (!strcmp(p, "type")) {
                     if (!strcmp(p2 + 1, "ioemu"))
#20 lore raw

PATCH 1/1 libxl: use libxl_version_info_destroy instead of hand-coded do_free_version_info

In reply to Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1281969065 -3600
# Node ID b8069740f4aa7ab792497a9d4a65b614ee94aadd
# Parent f00e5b9e555fee97d60a98fe11a6d856b27bb3b0
libxl: use libxl_version_info_destroy instead of hand-coded do_free_version_info

tools/libxl/libxl.c +1 -14
diff -r f00e5b9e555f -r b8069740f4aa tools/libxl/libxl.c
@@ -64,11 +64,10 @@ int libxl_ctx_init(libxl_ctx *ctx, int v
     return 0;
 }
 
-static void do_free_version_info(libxl_version_info *info);
 int libxl_ctx_free(libxl_ctx *ctx)
 {
     xc_interface_close(ctx->xch);
-    do_free_version_info(&ctx->version_info);
+    libxl_version_info_destroy(&ctx->version_info);
     if (ctx->xsh) xs_daemon_close(ctx->xsh); 
     return 0;
 }
@@ -2872,18 +2871,6 @@ int libxl_get_physinfo(libxl_ctx *ctx, l
     return 0;
 }
 
-static void do_free_version_info(libxl_version_info *info)
-{
-    free(info->xen_version_extra);
-    free(info->compiler);
-    free(info->compile_by);
-    free(info->compile_domain);
-    free(info->compile_date);
-    free(info->capabilities);
-    free(info->changeset);
-    free(info->commandline);
-}
-
 const libxl_version_info* libxl_get_version_info(libxl_ctx *ctx)
 {
     union {
#21 lore raw

PATCH 1/1 xl: destroy device model info after creation

In reply to Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]> # Date 1281969065 -3600
# Node ID a33d1901115bfeceab9cad25954472acdd475744 # Parent b8069740f4aa7ab792497a9d4a65b614ee94aadd
xl: destroy device model info after creation

tools/libxl/xl_cmdimpl.c +2 -0
diff -r b8069740f4aa -r a33d1901115b tools/libxl/xl_cmdimpl.c
@@ -1621,6 +1621,8 @@ error_out:
         libxl_domain_destroy(&ctx, domid, 0);
 out:
 
+    libxl_device_model_info_destroy(&dm_info);
+
     free_domain_config(&d_config);
 
     free(config_data);
#22 lore raw

PATCH 1/1 xl: free all data on exit from the domain monitor daemon

In reply to Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1281969065 -3600
# Node ID dd25ac29c0f74a2d909df3e3d072ed5d59320312 # Parent a33d1901115bfeceab9cad25954472acdd475744
xl: free all data on exit from the domain monitor daemon.

tools/libxl/xl_cmdimpl.c +6 -5
diff -r a33d1901115b -r dd25ac29c0f7 tools/libxl/xl_cmdimpl.c
@@ -1581,9 +1581,11 @@ start:
                 if (ret) {
                     switch (handle_domain_death(&ctx, domid, &event, &d_config, &info)) {
                     case 2:
-                        if (!preserve_domain(&ctx, domid, &event, &d_config, &info))
+                        if (!preserve_domain(&ctx, domid, &event, &d_config, &info)) {
                             /* If we fail then exit leaving the old domain in place. */
-                            exit(-1);
+                            ret = -1;
+                            goto out;
+                        }
 
                         /* Otherwise fall through and restart. */
                     case 1:
@@ -1601,6 +1603,8 @@ start:
                         goto start;
                     case 0:
                         LOG("Done. Exiting now");
+                        ret = 0;
+                        goto out;
                         exit(0);
                     }
                 }
@@ -1612,9 +1616,6 @@ start:
         }
         libxl_free_event(&event);
     }
-
-    close(logfile);
-    exit(0);
 
 error_out:
     if (domid)
#23 lore raw

Re: [PATCH 20 of 26] xl: free all data on exit from the domain monitor daemon

Quoted 27 lines · Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1281969065 -3600
# Node ID dd25ac29c0f74a2d909df3e3d072ed5d59320312 # Parent a33d1901115bfeceab9cad25954472acdd475744
xl: free all data on exit from the domain monitor daemon.

diff -r a33d1901115b -r dd25ac29c0f7 tools/libxl/xl_cmdimpl.c

tools/libxl/xl_cmdimpl.c +4 -2
@@ -1581,9 +1581,11 @@ start:
                 if (ret) {
                     switch (handle_domain_death(&ctx, domid, &event, &d_config, &info)) {
                     case 2:
-                        if (!preserve_domain(&ctx, domid, &event, &d_config, &info))
+                        if (!preserve_domain(&ctx, domid, &event, &d_config, &info)) {
                             /* If we fail then exit leaving the old domain in place. */
-                            exit(-1);
+                            ret = -1;
+                            goto out;
+                        }
 
                         /* Otherwise fall through and restart. */
                     case 1:

tools/libxl/xl_cmdimpl.c start: · line 1610

Quoted 8 lines
tools/libxl/xl_cmdimpl.c +2 -0
@@ -1601,6 +1603,8 @@ start:
                         goto start;
                     case 0:
                         LOG("Done. Exiting now");
+                        ret = 0;
+                        goto out;
                         exit(0);
                     }
                 }

you can remove the exit(0) from there

tools/libxl/xl_cmdimpl.c start: · line 1619

Quoted 8 lines
tools/libxl/xl_cmdimpl.c +0 -3
@@ -1612,9 +1616,6 @@ start:
         }
         libxl_free_event(&event);
     }
-
-    close(logfile);
-    exit(0);
 

shouldn't the close(logfile) be moved to the out path?

#24 lore raw

Re: [PATCH 20 of 26] xl: free all data on exit from the domain monitor daemon

Quoted 25 lines · Stefano Stabellini
Quoted 24 lines · Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1281969065 -3600
# Node ID dd25ac29c0f74a2d909df3e3d072ed5d59320312 # Parent a33d1901115bfeceab9cad25954472acdd475744
xl: free all data on exit from the domain monitor daemon.

diff -r a33d1901115b -r dd25ac29c0f7 tools/libxl/xl_cmdimpl.c

tools/libxl/xl_cmdimpl.c +4 -2
@@ -1581,9 +1581,11 @@ start:
                 if (ret) {
                     switch (handle_domain_death(&ctx, domid, &event, &d_config, &info)) {
                     case 2:
-                        if (!preserve_domain(&ctx, domid, &event, &d_config, &info))
+                        if (!preserve_domain(&ctx, domid, &event, &d_config, &info)) {
                             /* If we fail then exit leaving the old domain in place. */
-                            exit(-1);
+                            ret = -1;
+                            goto out;
+                        }
 
Quoted 13 lines

tools/libxl/xl_cmdimpl.c start: · line 1610

Quoted 11 lines
tools/libxl/xl_cmdimpl.c +2 -0
                         /* Otherwise fall through and restart. */
                     case 1:
@@ -1601,6 +1603,8 @@ start:
                         goto start;
                     case 0:
                         LOG("Done. Exiting now");
+                        ret = 0;
+                        goto out;
                         exit(0);
                     }
                 }

you can remove the exit(0) from there

oops, yes indeed.

Quoted 12 lines

tools/libxl/xl_cmdimpl.c start: · line 1619

Quoted 10 lines
tools/libxl/xl_cmdimpl.c +0 -3
@@ -1612,9 +1616,6 @@ start:
         }
         libxl_free_event(&event);
     }
-
-    close(logfile);
-    exit(0);
 

shouldn't the close(logfile) be moved to the out path?

I guess it should be done somewhere but as far as I can tell the code in the current location was never reachable.

I think it needs an out_closelog label which does the closing since the regular out label is used from places which do not open the logfile. I'll make that change.

Ian.

#25 lore raw

Re: [PATCH 20 of 26] xl: free all data on exit from the domain monitor daemon

Ian Campbell writes ("Re: [Xen-devel] [PATCH 20 of 26] xl: free all data on exit from the domain monitor daemon"):

Quoted 3 lines

I think it needs an out_closelog label which does the closing since the regular out label is used from places which do not open the logfile. I'll make that change.

You could avoid messing about with different out labels (always error-prone) by closing logfile iff it has been reopened:
if (logfile != 2) close(logfile)

Ian.

#26 lore raw

Re: [PATCH 20 of 26] xl: free all data on exit from the domain monitor daemon

Quoted 8 lines · Ian Jackson

Ian Campbell writes ("Re: [Xen-devel] [PATCH 20 of 26] xl: free all data on exit from the domain monitor daemon"):

Quoted 3 lines

I think it needs an out_closelog label which does the closing since the regular out label is used from places which do not open the logfile. I'll make that change.

You could avoid messing about with different out labels (always error-prone) by closing logfile iff it has been reopened:
if (logfile != 2) close(logfile)

That's actually what I ended up doing. I'll repost tomorrow once I've had a chance to rebase on top ofthe applied subset.

(I'll pull this one to the front of the series as well since it's really a generic fix)

Ian.

#27 lore raw

PATCH 1/1 libxl/xl: use libxl_diskinfo_destroy and libxl_device_disk_destroy

In reply to Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1281969065 -3600
# Node ID b37efbc62265ee7e0967ae856bfff9e9953f62f4
# Parent dd25ac29c0f74a2d909df3e3d072ed5d59320312 libxl/xl: use libxl_diskinfo_destroy and libxl_device_disk_destroy

tools/libxl/libxl.c +10 -5
diff -r dd25ac29c0f7 -r b37efbc62265 tools/libxl/libxl.c
@@ -1348,8 +1348,8 @@ static char ** libxl_build_device_model_
             flexarray_set(dm_args, num++, libxl_sprintf(gc, "-%s", disks[i].virtpath));
             flexarray_set(dm_args, num++, disks[i].physpath);
         }
+        libxl_device_disk_destroy(&disks[i]);
     }
-    /* FIXME: leaks disk paths */
     free(disks);
     flexarray_set(dm_args, num++, NULL);
     return (char **) flexarray_contents(dm_args);
@@ -2578,6 +2578,7 @@ int libxl_cdrom_insert(libxl_ctx *ctx, u
     int num, i;
     uint32_t stubdomid;
     libxl_device_disk *disks;
+    int ret = ERROR_FAIL;
 
     if (!disk->physpath) {
         disk->physpath = "";
@@ -2591,9 +2592,11 @@ int libxl_cdrom_insert(libxl_ctx *ctx, u
     }
     if (i == num) {
         XL_LOG(ctx, XL_LOG_ERROR, "Virtual device not found");
-        free(disks);
-        return ERROR_FAIL;
+        goto out;
     }
+
+    ret = 0;
+
     libxl_device_disk_del(ctx, disks + i, 1);
     libxl_device_disk_add(ctx, domid, disk);
     stubdomid = libxl_get_stubdom_id(ctx, domid);
@@ -2604,9 +2607,11 @@ int libxl_cdrom_insert(libxl_ctx *ctx, u
         libxl_device_disk_add(ctx, stubdomid, disk);
         disk->domid = domid;
     }
-    /* FIXME: leaks disk paths */
+out:
+    for (i = 0; i < num; i++)
+        libxl_device_disk_destroy(&disks[i]);
     free(disks);
-    return 0;
+    return ret;
 }
 
 /******************************************************************************/
tools/libxl/xl_cmdimpl.c +8 -5
diff -r dd25ac29c0f7 -r b37efbc62265 tools/libxl/xl_cmdimpl.c
@@ -4259,7 +4259,7 @@ int main_blocklist(int argc, char **argv
 int main_blocklist(int argc, char **argv)
 {
     int opt;
-    int nb;
+    int i, nb;
     libxl_device_disk *disks;
     libxl_diskinfo diskinfo;
 
@@ -4289,14 +4289,17 @@ int main_blocklist(int argc, char **argv
         if (!disks) {
             continue;
         }
-        for (; nb > 0; --nb, ++disks) {
-            if (!libxl_device_disk_getinfo(&ctx, domid, disks, &diskinfo)) {
+        for (i=0; i<nb; i++) {
+            if (!libxl_device_disk_getinfo(&ctx, domid, &disks[i], &diskinfo)) {
                 /*      Vdev BE   hdl  st   evch rref BE-path*/
                 printf("%-5d %-3d %-6d %-5d %-6d %-8d %-30s\n",
                        diskinfo.devid, diskinfo.backend_id, diskinfo.frontend_id,
                        diskinfo.state, diskinfo.evtch, diskinfo.rref, diskinfo.backend);
-            }
-        }
+                libxl_diskinfo_destroy(&diskinfo);
+            }
+            libxl_device_disk_destroy(&disks[i]);
+        }
+        free(disks);
     }
     return 0;
 }
#28 lore raw

PATCH 1/1 libxl/xl: Use libxl_device_nic_destroy and libxl_nicinfo_destroy

In reply to Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1281969065 -3600
# Node ID 3b72249bb27c9bd73cf40a09d42ae8f8d663a8f5
# Parent b37efbc62265ee7e0967ae856bfff9e9953f62f4 libxl/xl: Use libxl_device_nic_destroy and libxl_nicinfo_destroy

Replaces libxl_free_nics_list

tools/libxl/libxl.c +0 -11
diff -r b37efbc62265 -r 3b72249bb27c tools/libxl/libxl.c
@@ -2063,17 +2063,6 @@ int libxl_device_nic_del(libxl_ctx *ctx,
     return libxl_device_del(ctx, &device, wait);
 }
 
-void libxl_free_nics_list(libxl_nicinfo *nics, unsigned int nb)
-{
-    unsigned int i;
-    for(i = 0; i < nb; i++) {
-        free(nics[i].backend);
-        free(nics[i].frontend);
-        free(nics[i].script);
-    }
-    free(nics);
-}
-
 libxl_nicinfo *libxl_list_nics(libxl_ctx *ctx, uint32_t domid, unsigned int *nb)
 {
     libxl_gc gc = LIBXL_INIT_GC(ctx);
tools/libxl/libxl.h +0 -1
diff -r b37efbc62265 -r 3b72249bb27c tools/libxl/libxl.h
@@ -373,7 +373,6 @@ int libxl_device_nic_add(libxl_ctx *ctx,
 int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic);
 int libxl_device_nic_del(libxl_ctx *ctx, libxl_device_nic *nic, int wait);
 libxl_nicinfo *libxl_list_nics(libxl_ctx *ctx, uint32_t domid, unsigned int *nb);
-void libxl_free_nics_list(libxl_nicinfo *nics, unsigned int nb);
 
 int libxl_device_console_add(libxl_ctx *ctx, uint32_t domid, libxl_device_console *console);
 
tools/libxl/libxl_utils.c +21 -20

71 lines not on this page · raw message

tools/libxl/xl_cmdimpl.c +15 -12
diff -r b37efbc62265 -r 3b72249bb27c tools/libxl/xl_cmdimpl.c
@@ -4089,13 +4089,14 @@ int main_networkattach(int argc, char **
         fprintf(stderr, "libxl_device_nic_add failed.\n");
         return 1;
     }
+    libxl_device_nic_destroy(&nic);
     return 0;
 }
 
 int main_networklist(int argc, char **argv)
 {
     int opt;
-    libxl_nicinfo *nics, *list;
+    libxl_nicinfo *nics;
     unsigned int nb, i;
 
     if (argc < 3) {
@@ -4121,22 +4122,23 @@ int main_networklist(int argc, char **ar
             fprintf(stderr, "%s is an invalid domain identifier\n", *argv);
             continue;
         }
-        if (!(list = nics = libxl_list_nics(&ctx, domid, &nb))) {
-            continue;
-        }
-        for (i = 0; i < nb; ++i, ++nics) {
+        if (!(nics = libxl_list_nics(&ctx, domid, &nb))) {
+            continue;
+        }
+        for (i = 0; i < nb; ++i) {
             /* Idx BE */
-            printf("%-3d %-2d ", nics->devid, nics->backend_id);
+            printf("%-3d %-2d ", nics[i].devid, nics[i].backend_id);
             /* MAC */
             printf("%02x:%02x:%02x:%02x:%02x:%02x ",
-                   nics->mac[0], nics->mac[1], nics->mac[2],
-                   nics->mac[3], nics->mac[4], nics->mac[5]);
+                   nics[i].mac[0], nics[i].mac[1], nics[i].mac[2],
+                   nics[i].mac[3], nics[i].mac[4], nics[i].mac[5]);
             /* Hdl  Sta  evch txr/rxr  BE-path */
             printf("%6d %5d %6d %5d/%-11d %-30s\n",
-                   nics->devid, nics->state, nics->evtch,
-                   nics->rref_tx, nics->rref_rx, nics->backend);
-        }
-        libxl_free_nics_list(list, nb);
+                   nics[i].devid, nics[i].state, nics[i].evtch,
+                   nics[i].rref_tx, nics[i].rref_rx, nics[i].backend);
+            libxl_nicinfo_destroy(&nics[i]);
+        }
+        free(nics);
     }
     return 0;
 }
@@ -4181,6 +4183,7 @@ int main_networkdetach(int argc, char **
         fprintf(stderr, "libxl_device_nic_del failed.\n");
         return 1;
     }
+    libxl_device_nic_destroy(&nic);
     return 0;
 }
#29 lore raw

PATCH 1/1 libxl/xl: Use libxl_vcpuinfo_destroy

In reply to Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]> # Date 1281969065 -3600
# Node ID 8c709ca39c91e53add430ada580e027af6100eef # Parent 3b72249bb27c9bd73cf40a09d42ae8f8d663a8f5 libxl/xl: Use libxl_vcpuinfo_destroy

Replaces libxl_free_vcpu_list.

The ->cpumap field is now always a unique allocation rather than each being an offset into the cpumap allocated in the first libxl_device_pci in the list.

Refactor vcpulist so that the two cases can share more code.

tools/libxl/libxl.c +1 -10
diff -r 3b72249bb27c -r 8c709ca39c91 tools/libxl/libxl.c
@@ -2918,7 +2918,6 @@ libxl_vcpuinfo *libxl_list_vcpu(libxl_ct
     xc_domaininfo_t domaininfo;
     xc_vcpuinfo_t vcpuinfo;
     xc_physinfo_t physinfo = { 0 };
-    uint64_t *cpumaps;
     unsigned num_cpuwords;
 
     if (xc_domain_getinfolist(ctx->xch, domid, 1, &domaininfo) != 1) {
@@ -2936,9 +2935,8 @@ libxl_vcpuinfo *libxl_list_vcpu(libxl_ct
     }
 
     num_cpuwords = ((physinfo.max_cpu_id + 64) / 64);
-    cpumaps = calloc(num_cpuwords * sizeof(*cpumaps), domaininfo.max_vcpu_id + 1);
     for (*nb_vcpu = 0; *nb_vcpu <= domaininfo.max_vcpu_id; ++*nb_vcpu, ++ptr) {
-        ptr->cpumap = cpumaps + (num_cpuwords * *nb_vcpu);
+        ptr->cpumap = malloc(num_cpuwords * sizeof(*ptr->cpumap));
         if (!ptr->cpumap) {
             return NULL;
         }
@@ -2959,13 +2957,6 @@ libxl_vcpuinfo *libxl_list_vcpu(libxl_ct
         ptr->vcpu_time = vcpuinfo.cpu_time;
     }
     return ret;
-}
-
-void libxl_free_vcpu_list(libxl_vcpuinfo *vcpu)
-{
-    if ( vcpu )
-        free(vcpu[0].cpumap);
-    free(vcpu);
 }
 
 int libxl_set_vcpuaffinity(libxl_ctx *ctx, uint32_t domid, uint32_t vcpuid,
tools/libxl/libxl.h +0 -1
diff -r 3b72249bb27c -r 8c709ca39c91 tools/libxl/libxl.h
@@ -432,7 +432,6 @@ int libxl_get_physinfo(libxl_ctx *ctx, l
 int libxl_get_physinfo(libxl_ctx *ctx, libxl_physinfo *physinfo);
 libxl_vcpuinfo *libxl_list_vcpu(libxl_ctx *ctx, uint32_t domid,
                                        int *nb_vcpu, int *nrcpus);
-void libxl_free_vcpu_list(libxl_vcpuinfo *vcpu);
 int libxl_set_vcpuaffinity(libxl_ctx *ctx, uint32_t domid, uint32_t vcpuid,
                            uint64_t *cpumap, int nrcpus);
 int libxl_set_vcpucount(libxl_ctx *ctx, uint32_t domid, uint32_t count);
tools/libxl/xl_cmdimpl.c +32 -23

81 lines not on this page · raw message

#30 lore raw

PATCH 1/1 xl: use libxl_device_pci_destroy

In reply to Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]> # Date 1281969065 -3600
# Node ID 9ff0821b2c7915051a64ca89ec9c64a4484c16ad # Parent 8c709ca39c91e53add430ada580e027af6100eef
xl: use libxl_device_pci_destroy

tools/libxl/xl_cmdimpl.c +3 -0
diff -r 8c709ca39c91 -r 9ff0821b2c79 tools/libxl/xl_cmdimpl.c
@@ -2008,6 +2008,7 @@ void pcilist(char *dom)
     printf("VFn  domain bus  slot func\n");
     for (i = 0; i < num; i++) {
         printf("0x%02x 0x%04x 0x%02x 0x%02x 0x%01x\n", pcidevs[i].vdevfn, pcidevs[i].domain, pcidevs[i].bus, pcidevs[i].dev, pcidevs[i].func);
+        libxl_device_pci_destroy(&pcidevs[i]);
     }
     free(pcidevs);
 }
@@ -2050,6 +2051,7 @@ void pcidetach(char *dom, char *bdf)
         exit(2);
     }
     libxl_device_pci_remove(&ctx, domid, &pcidev);
+    libxl_device_pci_destroy(&pcidev);
 }
 
 int main_pcidetach(int argc, char **argv)
@@ -2090,6 +2092,7 @@ void pciattach(char *dom, char *bdf, cha
         exit(2);
     }
     libxl_device_pci_add(&ctx, domid, &pcidev);
+    libxl_device_pci_destroy(&pcidev);
 }
 
 int main_pciattach(int argc, char **argv)
#31 lore raw

PATCH 1/1 libxl: do not GC data returned to the caller by libxl_device_disk_getinfo

In reply to Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1281969065 -3600
# Node ID 5f7df0947ce97ba5ec604bacd09694ed399c2729
# Parent 9ff0821b2c7915051a64ca89ec9c64a4484c16ad
libxl: do not GC data returned to the caller by libxl_device_disk_getinfo.

tools/libxl/libxl.c +4 -4
diff -r 9ff0821b2c79 -r 5f7df0947ce9 tools/libxl/libxl.c
@@ -2539,8 +2539,8 @@ int libxl_device_disk_getinfo(libxl_ctx 
 
     /* tap devices entries in xenstore are written as vbd devices. */
     diskpath = libxl_sprintf(&gc, "%s/device/vbd/%d", dompath, diskinfo->devid);
-    diskinfo->backend = libxl_xs_read(&gc, XBT_NULL,
-                                      libxl_sprintf(&gc, "%s/backend", diskpath));
+    diskinfo->backend = xs_read(ctx->xsh, XBT_NULL,
+                                libxl_sprintf(&gc, "%s/backend", diskpath), NULL);
     if (!diskinfo->backend) {
         libxl_free_all(&gc);
         return ERROR_FAIL;
@@ -2553,8 +2553,8 @@ int libxl_device_disk_getinfo(libxl_ctx 
     diskinfo->evtch = val ? strtoul(val, NULL, 10) : -1;
     val = libxl_xs_read(&gc, XBT_NULL, libxl_sprintf(&gc, "%s/ring-ref", diskpath));
     diskinfo->rref = val ? strtoul(val, NULL, 10) : -1;
-    diskinfo->frontend = libxl_xs_read(&gc, XBT_NULL,
-                                       libxl_sprintf(&gc, "%s/frontend", diskinfo->backend));
+    diskinfo->frontend = xs_read(ctx->xsh, XBT_NULL,
+                                 libxl_sprintf(&gc, "%s/frontend", diskinfo->backend), NULL);
     val = libxl_xs_read(&gc, XBT_NULL, libxl_sprintf(&gc, "%s/frontend-id", diskinfo->backend));
     diskinfo->frontend_id = val ? strtoul(val, NULL, 10) : -1;
#32 lore raw

PATCH 1/1 libxl: xs_read accepts NULL for *len parameter

In reply to Ian Campbell

# HG changeset patch
# User Ian Campbell <[email protected]> # Date 1281969065 -3600
# Node ID 3619c13659ae1fa8adb283db84d7f1bedb8b651f # Parent 5f7df0947ce97ba5ec604bacd09694ed399c2729
libxl: xs_read accepts NULL for *len parameter

tools/libxl/libxl_xshelp.c +1 -2
diff -r 5f7df0947ce9 -r 3619c13659ae tools/libxl/libxl_xshelp.c
@@ -110,10 +110,9 @@ char * libxl_xs_read(libxl_gc *gc, xs_tr
 char * libxl_xs_read(libxl_gc *gc, xs_transaction_t t, char *path)
 {
     libxl_ctx *ctx = libxl_gc_owner(gc);
-    unsigned int len;
     char *ptr;
 
-    ptr = xs_read(ctx->xsh, t, path, &len);
+    ptr = xs_read(ctx->xsh, t, path, NULL);
     if (ptr != NULL) {
         libxl_ptr_add(gc, ptr);
         return ptr;
#33 lore raw
In reply to Ian Campbell
Quoted 22 lines · Ian Campbell

The series introduces auto-generation of the type definitions used in the libxl interface followed by auto-generation of a destructor function for each type. In the future it may be possible to use the related data structures for other purposes, for example auto-generation of the functions to marshal between C and language binding data types.

tools/_libxl_types.h should be identical both before applying and after applying+building "libxl: autogenerate _libxl_types.h" apart from a "DO NOT EDIT" header.

Since last time:
* rebased
* corrected Makefile dependencies to include libxltypes.idl
* manually implemented libxl_file_reference_destroy since it is more complex than just freeing the contained types.
* Made libxl_file_reference_{map,unmap} into internal functions.
* Added typedefs for various types:
- libxl_cpumap
- libxl_hwcap
* Made libxl_xen_console_reader an opaque type and by making the definition internal.
* moved more types from libxl.h to _libxl_types.h. I think all those

Quoted 8 lines

which it makes sense to generate are now accounted for.
* disabled destructor generation for types which have no interesting fields (i.e. had empty destructor functions). I have retained the empty destructors for types which belong to a set where some types do have a valid need for a destructor funntion (e.g. libxl_device_* or libxl_*info)
* Audit for usages of libxl_device_* and libxl_*info which can use the new destructors. I'm sure I haven't caught them all.

Many of these patches are straight-up bug fixes that should be applied right away. Especially:
[PATCH 25 of 26] libxl: do not GC data returned to the caller by
libxl_device_disk_getinfo
[PATCH 08 of 26] libxl: ensure result of libxl_poolid_to_name is always
dynamically allocated

Some are just a good idea anyway: specific typedefs and making some structs opaque, 20/26 - free all data from domain monitor. I think these should be applied as-is too.

As for auto-generation stuff, I believe that since it's produced the right header file last few go's around then it will work in future. So can you submit that in one chunk next time without the backup/restore features, diffs for comparison purposes?

Good stuff

#34 lore raw
Quoted 2 lines · On Tue, 2010-08-17 at 13:14 +0100, Gianni Tedesco (3P) wrote:

So can you submit that in one chunk next time without the backup/restore features, diffs for comparison purposes?

backup/restore features? What do you mean?

If you are referring to the separation of movement, reformatting and finally autogeneration into separate patches then I think this is necessary to be able to simply (practically automatically) validate the series is correct. I'd rather not get rid of that since it makes rebasing much simpler.

Ian.

#35 lore raw
Quoted 5 lines · Ian Campbell
Quoted 2 lines · On Tue, 2010-08-17 at 13:14 +0100, Gianni Tedesco (3P) wrote:

So can you submit that in one chunk next time without the backup/restore features, diffs for comparison purposes?

backup/restore features? What do you mean?

I meant the cp _libxl_types.h back and forth in makefiles

Quoted 5 lines

If you are referring to the separation of movement, reformatting and finally autogeneration into separate patches then I think this is necessary to be able to simply (practically automatically) validate the series is correct. I'd rather not get rid of that since it makes rebasing much simpler.

OK fair enough, I am just a passive consumer who is always applying whole series anyway.

As long as the bug-fixes you posted don't get lost in the wind until the next time you re-submit I am happy.

#36 lore raw
In reply to Gianni Tedesco
Quoted 32 lines · On Tue, 17 Aug 2010, Gianni Tedesco (3P) wrote:
Quoted 22 lines · Ian Campbell

The series introduces auto-generation of the type definitions used in the libxl interface followed by auto-generation of a destructor function for each type. In the future it may be possible to use the related data structures for other purposes, for example auto-generation of the functions to marshal between C and language binding data types.

tools/_libxl_types.h should be identical both before applying and after applying+building "libxl: autogenerate _libxl_types.h" apart from a "DO NOT EDIT" header.

Since last time:
* rebased
* corrected Makefile dependencies to include libxltypes.idl
* manually implemented libxl_file_reference_destroy since it is more complex than just freeing the contained types.
* Made libxl_file_reference_{map,unmap} into internal functions.
* Added typedefs for various types:
- libxl_cpumap
- libxl_hwcap
* Made libxl_xen_console_reader an opaque type and by making the definition internal.
* moved more types from libxl.h to _libxl_types.h. I think all those

Quoted 8 lines

which it makes sense to generate are now accounted for.
* disabled destructor generation for types which have no interesting fields (i.e. had empty destructor functions). I have retained the empty destructors for types which belong to a set where some types do have a valid need for a destructor funntion (e.g. libxl_device_* or libxl_*info)
* Audit for usages of libxl_device_* and libxl_*info which can use the new destructors. I'm sure I haven't caught them all.

Quoted 7 lines

Many of these patches are straight-up bug fixes that should be applied right away. Especially:
[PATCH 25 of 26] libxl: do not GC data returned to the caller by
libxl_device_disk_getinfo
[PATCH 08 of 26] libxl: ensure result of libxl_poolid_to_name is always
dynamically allocated

I am thinking of applying patches 1 to 8 and patches 25 and 26, so that next iteration we are left with 3 conceptually similar groups of patches:

- introduction of _libxl_types.h

- autogeneration of _libxl_types and introduction of the idl

- destructors usage in xl

#37 lore raw
Quoted 9 lines · Stefano Stabellini

I am thinking of applying patches 1 to 8 and patches 25 and 26, so that next iteration we are left with 3 conceptually similar groups of patches:

- introduction of _libxl_types.h

- autogeneration of _libxl_types and introduction of the idl

- destructors usage in xl

Sounds ok to me. I'll rebase the remainder onto the result.

Ian.