[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH 4 of 7] xl: don't use libxl allocator for nic_list



 tools/libxl/libxl.c       |  33 ++++++++++++++++++++-------------
 tools/libxl/libxl.h       |   1 +
 tools/libxl/libxl_utils.c |  15 +++++++++------
 tools/libxl/xl_cmdimpl.c  |   9 +++++----
 4 files changed, 35 insertions(+), 23 deletions(-)


This also fixes a bug with an erroneous call to libxl_free(). A destructor for
the nic list is also implemented which is called from xl.

Signed-off-by: Gianni Tedesco <gianni.tedesco@xxxxxxxxxx>

diff -r 4a877c3d722f -r 0c8c78fea130 tools/libxl/libxl.c
--- a/tools/libxl/libxl.c       Tue Aug 10 15:47:00 2010 +0100
+++ b/tools/libxl/libxl.c       Tue Aug 10 15:47:56 2010 +0100
@@ -1729,10 +1729,21 @@ 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)
 {
     char *dompath, *nic_path_fe;
-    char **l;
+    char **l, **list;
     char *val, *tok;
     unsigned int nb_nics, i;
     libxl_nicinfo *res, *nics;
@@ -1741,22 +1752,21 @@ libxl_nicinfo *libxl_list_nics(libxl_ctx
     if (!dompath) {
         return NULL;
     }
-    l = libxl_xs_directory(ctx, XBT_NULL,
+    list = l = libxl_xs_directory(ctx, XBT_NULL,
                            libxl_sprintf(ctx, "%s/device/vif", dompath), 
&nb_nics);
     if (!l) {
         return NULL;
     }
-    res = libxl_calloc(ctx, nb_nics, sizeof (libxl_device_nic));
+    nics = res = calloc(nb_nics, sizeof (libxl_device_nic));
     if (!res) {
         libxl_free(ctx, l);
         return NULL;
     }
-    nics = res;
     for (*nb = nb_nics; nb_nics > 0; --nb_nics, ++l, ++nics) {
         nic_path_fe = libxl_sprintf(ctx, "%s/device/vif/%s", dompath, *l);
 
-        nics->backend = libxl_xs_read(ctx, XBT_NULL,
-                                      libxl_sprintf(ctx, "%s/backend", 
nic_path_fe));
+        nics->backend = xs_read(ctx->xsh, XBT_NULL,
+                                libxl_sprintf(ctx, "%s/backend", nic_path_fe), 
NULL);
         val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/backend-id", 
nic_path_fe));
         nics->backend_id = val ? strtoul(val, NULL, 10) : -1;
 
@@ -1774,17 +1784,14 @@ libxl_nicinfo *libxl_list_nics(libxl_ctx
         nics->rref_tx = val ? strtol(val, NULL, 10) : -1;
         val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, 
"%s/rx-ring-ref", nic_path_fe));
         nics->rref_rx = val ? strtol(val, NULL, 10) : -1;
-        nics->frontend = libxl_xs_read(ctx, XBT_NULL,
-                                       libxl_sprintf(ctx, "%s/frontend", 
nics->backend));
+        nics->frontend = xs_read(ctx->xsh, XBT_NULL,
+                                 libxl_sprintf(ctx, "%s/frontend", 
nics->backend), NULL);
         val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, 
"%s/frontend-id", nics->backend));
         nics->frontend_id = val ? strtoul(val, NULL, 10) : -1;
-        nics->script = libxl_xs_read(ctx, XBT_NULL,
-                                     libxl_sprintf(ctx, "%s/script", 
nics->backend));
-
-        libxl_free(ctx, nic_path_fe);
+        nics->script = xs_read(ctx->xsh, XBT_NULL,
+                               libxl_sprintf(ctx, "%s/script", nics->backend), 
NULL);
     }
 
-    libxl_free(ctx, l);
     return res;
 }
 
diff -r 4a877c3d722f -r 0c8c78fea130 tools/libxl/libxl.h
--- a/tools/libxl/libxl.h       Tue Aug 10 15:47:00 2010 +0100
+++ b/tools/libxl/libxl.h       Tue Aug 10 15:47:56 2010 +0100
@@ -509,6 +509,7 @@ typedef struct {
 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);
 
diff -r 4a877c3d722f -r 0c8c78fea130 tools/libxl/libxl_utils.c
--- a/tools/libxl/libxl_utils.c Tue Aug 10 15:47:00 2010 +0100
+++ b/tools/libxl/libxl_utils.c Tue Aug 10 15:47:56 2010 +0100
@@ -365,14 +365,14 @@ int libxl_pipe(libxl_ctx *ctx, int pipes
 int libxl_mac_to_device_nic(libxl_ctx *ctx, uint32_t domid,
                             const char *mac, libxl_device_nic *nic)
 {
-    libxl_nicinfo *nics;
-    unsigned int nb, i;
+    libxl_nicinfo *nics, *list;
+    unsigned int nb, i, j;
     uint8_t mac_n[6];
     uint8_t *a, *b;
     const char *tok;
     char *endptr;
 
-    nics = libxl_list_nics(ctx, domid, &nb);
+    list = nics = libxl_list_nics(ctx, domid, &nb);
     if (!nics) {
         return ERROR_FAIL;
     }
@@ -384,7 +384,7 @@ int libxl_mac_to_device_nic(libxl_ctx *c
         }
     }
     memset(nic, 0, sizeof (libxl_device_nic));
-    for (; nb; --nb, ++nics) {
+    for (j = 0; j < nb; ++j, ++nics) {
         for (i = 0, a = nics->mac, b = mac_n;
              (b < mac_n + 6) && (*a == *b); ++a, ++b)
             ;
@@ -394,12 +394,12 @@ int libxl_mac_to_device_nic(libxl_ctx *c
             nic->devid = nics->devid;
             memcpy(nic->mac, nics->mac, sizeof (nic->mac));
             nic->script = nics->script;
-            libxl_free(ctx, nics);
+            libxl_free_nics_list(list, nb);
             return 0;
         }
     }
 
-    libxl_free(ctx, nics);
+    libxl_free_nics_list(list, nb);
     return 0;
 }
 
@@ -419,6 +419,9 @@ int libxl_devid_to_device_nic(libxl_ctx 
     nic_path_be = libxl_xs_read(ctx, XBT_NULL,
                                 libxl_sprintf(ctx, "%s/backend", nic_path_fe));
     val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/backend-id", 
nic_path_fe));
+    if ( NULL == val ) {
+        return ERROR_FAIL;
+    }
     nic->backend_domid = strtoul(val, NULL, 10);
     nic->devid = strtoul(devid, NULL, 10);
     libxl_free(ctx, val);
diff -r 4a877c3d722f -r 0c8c78fea130 tools/libxl/xl_cmdimpl.c
--- a/tools/libxl/xl_cmdimpl.c  Tue Aug 10 15:47:00 2010 +0100
+++ b/tools/libxl/xl_cmdimpl.c  Tue Aug 10 15:47:56 2010 +0100
@@ -4031,8 +4031,8 @@ int main_networkattach(int argc, char **
 int main_networklist(int argc, char **argv)
 {
     int opt;
-    libxl_nicinfo *nics;
-    unsigned int nb;
+    libxl_nicinfo *nics, *list;
+    unsigned int nb, i;
 
     if (argc < 3) {
         help("network-list");
@@ -4057,10 +4057,10 @@ int main_networklist(int argc, char **ar
             fprintf(stderr, "%s is an invalid domain identifier\n", *argv);
             continue;
         }
-        if (!(nics = libxl_list_nics(&ctx, domid, &nb))) {
+        if (!(list = nics = libxl_list_nics(&ctx, domid, &nb))) {
             continue;
         }
-        for (; nb > 0; --nb, ++nics) {
+        for (i = 0; i < nb; ++i, ++nics) {
             /* Idx BE */
             printf("%-3d %-2d ", nics->devid, nics->backend_id);
             /* MAC */
@@ -4072,6 +4072,7 @@ int main_networklist(int argc, char **ar
                    nics->devid, nics->state, nics->evtch,
                    nics->rref_tx, nics->rref_rx, nics->backend);
         }
+        libxl_free_nics_list(list, nb);
     }
     return 0;
 }

_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.