WARNING - OLD ARCHIVES

This is an archived copy of the Xen.org mailing list, which we have preserved to ensure that existing links to archives are not broken. The live archive, which contains the latest emails, can be found at http://lists.xen.org/
   
 
 
Xen 
 
Home Products Support Community News
 
   
 

xen-devel

[Xen-devel] [PATCH 06 of 23] libxl: update nic list API to use common de

To: xen-devel@xxxxxxxxxxxxxxxxxxx
Subject: [Xen-devel] [PATCH 06 of 23] libxl: update nic list API to use common device API style
From: Ian Campbell <ian.campbell@xxxxxxxxxx>
Date: Fri, 30 Sep 2011 14:33:19 +0100
Cc: Jim Fehlig <jfehlig@xxxxxxxxxx>, Mike McClurg <mike.mcclurg@xxxxxxxxxx>, Dave Scott <Dave.Scott@xxxxxxxxxxxxx>, Jonathan Ludlam <Jonathan.Ludlam@xxxxxxxxxxxxx>
Delivery-date: Fri, 30 Sep 2011 06:38:40 -0700
Envelope-to: www-data@xxxxxxxxxxxxxxxxxxx
In-reply-to: <patchbomb.1317389593@xxxxxxxxxxxxxxxxxxxxx>
List-help: <mailto:xen-devel-request@lists.xensource.com?subject=help>
List-id: Xen developer discussion <xen-devel.lists.xensource.com>
List-post: <mailto:xen-devel@lists.xensource.com>
List-subscribe: <http://lists.xensource.com/mailman/listinfo/xen-devel>, <mailto:xen-devel-request@lists.xensource.com?subject=subscribe>
List-unsubscribe: <http://lists.xensource.com/mailman/listinfo/xen-devel>, <mailto:xen-devel-request@lists.xensource.com?subject=unsubscribe>
References: <patchbomb.1317389593@xxxxxxxxxxxxxxxxxxxxx>
Sender: xen-devel-bounces@xxxxxxxxxxxxxxxxxxx
User-agent: Mercurial-patchbomb/1.6.4
# HG changeset patch
# User Ian Campbell <ian.campbell@xxxxxxxxxx>
# Date 1317389247 -3600
# Node ID cafd8b3f9e0c8bbf5c30cfcd512c1486661ca4d9
# Parent  4e640cbed20e8ef533f8eb27a82dcdac2be2e8ab
libxl: update nic list API to use common device API style

libxl_device_nic_list returns an array of libxl_device_nic and
libxl_device_nic_getinfo retrieves further information.

Signed-off-by: Ian Campbell <ian.campbell@xxxxxxxxxx>

diff -r 4e640cbed20e -r cafd8b3f9e0c tools/libxl/libxl.c
--- a/tools/libxl/libxl.c       Fri Sep 30 14:27:27 2011 +0100
+++ b/tools/libxl/libxl.c       Fri Sep 30 14:27:27 2011 +0100
@@ -1288,60 +1288,138 @@ int libxl_device_nic_del(libxl_ctx *ctx,
     return rc;
 }
 
-libxl_nicinfo *libxl_list_nics(libxl_ctx *ctx, uint32_t domid, unsigned int 
*nb)
+static void libxl__device_nic_from_xs_be(libxl__gc *gc,
+                                         const char *be_path,
+                                         libxl_device_nic *nic)
+{
+    libxl_ctx *ctx = libxl__gc_owner(gc);
+    unsigned int len;
+    char *tmp;
+    int rc;
+
+    memset(nic, 0, sizeof(*nic));
+
+    tmp = xs_read(ctx->xsh, XBT_NULL,
+                  libxl__sprintf(gc, "%s/handle", be_path), &len);
+    if ( tmp )
+        nic->devid = atoi(tmp);
+    else
+        nic->devid = 0;
+
+    /* nic->mtu = */
+
+    tmp = xs_read(ctx->xsh, XBT_NULL,
+                  libxl__sprintf(gc, "%s/mac", be_path), &len);
+    rc = libxl__parse_mac(tmp, nic->mac);
+    if (rc)
+            memset(nic->mac, 0, sizeof(nic->mac));
+
+    nic->ip = xs_read(ctx->xsh, XBT_NULL,
+                      libxl__sprintf(gc, "%s/ip", be_path), &len);
+
+    nic->bridge = xs_read(ctx->xsh, XBT_NULL,
+                      libxl__sprintf(gc, "%s/bridge", be_path), &len);
+
+    nic->script = xs_read(ctx->xsh, XBT_NULL,
+                      libxl__sprintf(gc, "%s/script", be_path), &len);
+
+    /* XXX ioemu nics are not in xenstore at all? */
+    nic->nictype = LIBXL_NIC_TYPE_VIF;
+    nic->model = NULL; /* XXX Only for TYPE_IOEMU */
+    nic->ifname = NULL; /* XXX Only for TYPE_IOEMU */
+}
+
+static int libxl__append_nic_list_of_type(libxl__gc *gc,
+                                           uint32_t domid,
+                                           const char *type,
+                                           libxl_device_nic **nics,
+                                           int *nnics)
+{
+    char *be_path = NULL;
+    char **dir = NULL;
+    unsigned int n = 0;
+    libxl_device_nic *pnic = NULL, *pnic_end = NULL;
+
+    be_path = libxl__sprintf(gc, "%s/backend/%s/%d",
+                             libxl__xs_get_dompath(gc, 0), type, domid);
+    dir = libxl__xs_directory(gc, XBT_NULL, be_path, &n);
+    if (dir) {
+        libxl_device_nic *tmp;
+        tmp = realloc(*nics, sizeof (libxl_device_nic) * (*nnics + n));
+        if (tmp == NULL)
+            return ERROR_NOMEM;
+        *nics = tmp;
+        pnic = *nics + *nnics;
+        *nnics += n;
+        pnic_end = *nics + *nnics;
+        for (; pnic < pnic_end; pnic++, dir++) {
+            const char *p;
+            p = libxl__sprintf(gc, "%s/%s", be_path, *dir);
+            libxl__device_nic_from_xs_be(gc, p, pnic);
+            pnic->backend_domid = 0;
+        }
+    }
+    return 0;
+}
+
+libxl_device_nic *libxl_device_nic_list(libxl_ctx *ctx, uint32_t domid, int 
*num)
 {
     libxl__gc gc = LIBXL_INIT_GC(ctx);
-    char *dompath, *nic_path_fe;
-    char **l, **list;
-    char *val, *tok;
-    unsigned int nb_nics, i;
-    libxl_nicinfo *res, *nics;
+    libxl_device_nic *nics = NULL;
+    int rc;
+
+    *num = 0;
+
+    rc = libxl__append_nic_list_of_type(&gc, domid, "vif", &nics, num);
+    if (rc) goto out_err;
+
+    libxl__free_all(&gc);
+    return nics;
+
+out_err:
+    LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Unable to list nics");
+    while (*num) {
+        (*num)--;
+        libxl_device_nic_destroy(&nics[*num]);
+    }
+    free(nics);
+    return NULL;
+}
+
+int libxl_device_nic_getinfo(libxl_ctx *ctx, uint32_t domid,
+                              libxl_device_nic *nic, libxl_nicinfo *nicinfo)
+{
+    libxl__gc gc = LIBXL_INIT_GC(ctx);
+    char *dompath, *nicpath;
+    char *val;
 
     dompath = libxl__xs_get_dompath(&gc, domid);
-    if (!dompath)
-        goto err;
-    list = l = libxl__xs_directory(&gc, XBT_NULL,
-                           libxl__sprintf(&gc, "%s/device/vif", dompath), 
&nb_nics);
-    if (!l)
-        goto err;
-    nics = res = calloc(nb_nics, sizeof (libxl_nicinfo));
-    if (!res)
-        goto err;
-    for (*nb = nb_nics; nb_nics > 0; --nb_nics, ++l, ++nics) {
-        nic_path_fe = libxl__sprintf(&gc, "%s/device/vif/%s", dompath, *l);
-
-        nics->backend = xs_read(ctx->xsh, XBT_NULL,
-                                libxl__sprintf(&gc, "%s/backend", 
nic_path_fe), NULL);
-        val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, 
"%s/backend-id", nic_path_fe));
-        nics->backend_id = val ? strtoul(val, NULL, 10) : -1;
-
-        nics->devid = strtoul(*l, NULL, 10);
-        val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/state", 
nic_path_fe));
-        nics->state = val ? strtoul(val, NULL, 10) : -1;
-        val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/mac", 
nic_path_fe));
-        for (i = 0, tok = strtok(val, ":"); tok && (i < 6);
-             ++i, tok = strtok(NULL, ":")) {
-            nics->mac[i] = strtoul(tok, NULL, 16);
-        }
-        val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, 
"%s/event-channel", nic_path_fe));
-        nics->evtch = val ? strtol(val, NULL, 10) : -1;
-        val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, 
"%s/tx-ring-ref", nic_path_fe));
-        nics->rref_tx = val ? strtol(val, NULL, 10) : -1;
-        val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, 
"%s/rx-ring-ref", nic_path_fe));
-        nics->rref_rx = val ? strtol(val, NULL, 10) : -1;
-        nics->frontend = xs_read(ctx->xsh, XBT_NULL,
-                                 libxl__sprintf(&gc, "%s/frontend", 
nics->backend), NULL);
-        val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, 
"%s/frontend-id", nics->backend));
-        nics->frontend_id = val ? strtoul(val, NULL, 10) : -1;
-        nics->script = xs_read(ctx->xsh, XBT_NULL,
-                               libxl__sprintf(&gc, "%s/script", 
nics->backend), NULL);
+    nicinfo->devid = nic->devid;
+
+    nicpath = libxl__sprintf(&gc, "%s/device/vif/%d", dompath, nicinfo->devid);
+    nicinfo->backend = xs_read(ctx->xsh, XBT_NULL,
+                                libxl__sprintf(&gc, "%s/backend", nicpath), 
NULL);
+    if (!nicinfo->backend) {
+        libxl__free_all(&gc);
+        return ERROR_FAIL;
     }
+    val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/backend-id", 
nicpath));
+    nicinfo->backend_id = val ? strtoul(val, NULL, 10) : -1;
+    val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/state", 
nicpath));
+    nicinfo->state = val ? strtoul(val, NULL, 10) : -1;
+    val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, 
"%s/event-channel", nicpath));
+    nicinfo->evtch = val ? strtoul(val, NULL, 10) : -1;
+    val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/tx-ring-ref", 
nicpath));
+    nicinfo->rref_tx = val ? strtoul(val, NULL, 10) : -1;
+    val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/rx-ring-ref", 
nicpath));
+    nicinfo->rref_rx = val ? strtoul(val, NULL, 10) : -1;
+    nicinfo->frontend = xs_read(ctx->xsh, XBT_NULL,
+                                 libxl__sprintf(&gc, "%s/frontend", 
nicinfo->backend), NULL);
+    val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/frontend-id", 
nicinfo->backend));
+    nicinfo->frontend_id = val ? strtoul(val, NULL, 10) : -1;
 
     libxl__free_all(&gc);
-    return res;
-err:
-    libxl__free_all(&gc);
-    return NULL;
+    return 0;
 }
 
 
/******************************************************************************/
diff -r 4e640cbed20e -r cafd8b3f9e0c tools/libxl/libxl.h
--- a/tools/libxl/libxl.h       Fri Sep 30 14:27:27 2011 +0100
+++ b/tools/libxl/libxl.h       Fri Sep 30 14:27:27 2011 +0100
@@ -467,7 +467,9 @@ int libxl_device_disk_local_detach(libxl
 int libxl_device_nic_init(libxl_device_nic *nic, int dev_num);
 int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic 
*nic);
 int libxl_device_nic_del(libxl_ctx *ctx, uint32_t domid, libxl_device_nic 
*nic, int wait);
-libxl_nicinfo *libxl_list_nics(libxl_ctx *ctx, uint32_t domid, unsigned int 
*nb);
+libxl_device_nic *libxl_device_nic_list(libxl_ctx *ctx, uint32_t domid, int 
*num);
+int libxl_device_nic_getinfo(libxl_ctx *ctx, uint32_t domid,
+                              libxl_device_nic *nic, libxl_nicinfo *nicinfo);
 
 int libxl_device_console_add(libxl_ctx *ctx, uint32_t domid, 
libxl_device_console *console);
 
diff -r 4e640cbed20e -r cafd8b3f9e0c tools/libxl/libxl_types.idl
--- a/tools/libxl/libxl_types.idl       Fri Sep 30 14:27:27 2011 +0100
+++ b/tools/libxl/libxl_types.idl       Fri Sep 30 14:27:27 2011 +0100
@@ -330,8 +330,6 @@ libxl_nicinfo = Struct("nicinfo", [
     ("frontend_id", uint32),
     ("devid", integer),
     ("state", integer),
-    ("script", string),
-    ("mac", libxl_mac),
     ("evtch", integer),
     ("rref_tx", integer),
     ("rref_rx", integer),
diff -r 4e640cbed20e -r cafd8b3f9e0c tools/libxl/libxl_utils.c
--- a/tools/libxl/libxl_utils.c Fri Sep 30 14:27:27 2011 +0100
+++ b/tools/libxl/libxl_utils.c Fri Sep 30 14:27:27 2011 +0100
@@ -451,15 +451,15 @@ 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, rc, i;
+    libxl_device_nic *nics;
+    int nb, rc, i;
     libxl_mac mac_n;
 
     rc = libxl__parse_mac(mac, mac_n);
     if (rc)
         return rc;
 
-    nics = libxl_list_nics(ctx, domid, &nb);
+    nics = libxl_device_nic_list(ctx, domid, &nb);
     if (!nics)
         return ERROR_FAIL;
 
@@ -468,17 +468,17 @@ int libxl_mac_to_device_nic(libxl_ctx *c
     rc = ERROR_INVAL;
     for (i = 0; i < nb; ++i) {
         if (!libxl__compare_macs(&mac_n, &nics[i].mac)) {
-            nic->backend_domid = nics[i].backend_id;
-            nic->devid = nics[i].devid;
-            memcpy(nic->mac, nics[i].mac, sizeof (nic->mac));
-            nic->script = strdup(nics[i].script);
+            *nic = nics[i];
             rc = 0;
+            i++; /* Do not destroy this NIC on exit path */
             break;
         }
+        libxl_device_nic_destroy(&nics[i]);
     }
 
-    for (i=0; i<nb; i++)
-        libxl_nicinfo_destroy(&nics[i]);
+    for (; i<nb; i++)
+        libxl_device_nic_destroy(&nics[i]);
+
     free(nics);
     return rc;
 }
diff -r 4e640cbed20e -r cafd8b3f9e0c tools/libxl/xl_cmdimpl.c
--- a/tools/libxl/xl_cmdimpl.c  Fri Sep 30 14:27:27 2011 +0100
+++ b/tools/libxl/xl_cmdimpl.c  Fri Sep 30 14:27:27 2011 +0100
@@ -4085,8 +4085,9 @@ int main_networkattach(int argc, char **
 int main_networklist(int argc, char **argv)
 {
     int opt;
-    libxl_nicinfo *nics;
-    unsigned int nb, i;
+    libxl_device_nic *nics;
+    libxl_nicinfo nicinfo;
+    int nb, i;
 
     if ((opt = def_getopt(argc, argv, "", "network-list", 1)) != -1)
         return opt;
@@ -4099,19 +4100,23 @@ 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))) {
+        nics = libxl_device_nic_list(ctx, domid, &nb);
+        if (!nics) {
             continue;
         }
         for (i = 0; i < nb; ++i) {
-            /* Idx BE */
-            printf("%-3d %-2d ", nics[i].devid, nics[i].backend_id);
-            /* MAC */
-            printf(LIBXL_MAC_FMT, LIBXL_MAC_BYTES(nics[i].mac));
-            /* Hdl  Sta  evch txr/rxr  BE-path */
-            printf("%6d %5d %6d %5d/%-11d %-30s\n",
-                   nics[i].devid, nics[i].state, nics[i].evtch,
-                   nics[i].rref_tx, nics[i].rref_rx, nics[i].backend);
-            libxl_nicinfo_destroy(&nics[i]);
+            if (!libxl_device_nic_getinfo(ctx, domid, &nics[i], &nicinfo)) {
+                /* Idx BE */
+                printf("%-3d %-2d ", nicinfo.devid, nicinfo.backend_id);
+                /* MAC */
+                printf(LIBXL_MAC_FMT, LIBXL_MAC_BYTES(nics[i].mac));
+                /* Hdl  Sta  evch txr/rxr  BE-path */
+                printf("%6d %5d %6d %5d/%-11d %-30s\n",
+                       nicinfo.devid, nicinfo.state, nicinfo.evtch,
+                       nicinfo.rref_tx, nicinfo.rref_rx, nicinfo.backend);
+                libxl_nicinfo_destroy(&nicinfo);
+            }
+            libxl_device_nic_destroy(&nics[i]);
         }
         free(nics);
     }

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