aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorIan Campbell <ian.campbell@citrix.com>2011-10-18 13:36:42 +0100
committerIan Campbell <ian.campbell@citrix.com>2011-10-18 13:36:42 +0100
commitef2d784f102635b276e4062701b2f8f84205ecc6 (patch)
tree8903f8bdfbca7f88c4c014bd964cae236e0fcca6 /tools
parentbcea3858dd4ec793dd2c30fe60571402a4b1d669 (diff)
downloadxen-ef2d784f102635b276e4062701b2f8f84205ecc6.tar.gz
xen-ef2d784f102635b276e4062701b2f8f84205ecc6.tar.bz2
xen-ef2d784f102635b276e4062701b2f8f84205ecc6.zip
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@citrix.com> Acked-by: Ian Jackson <ian.jackson.citrix.com> Committed-by: Ian Jackson <ian.jackson.citrix.com>
Diffstat (limited to 'tools')
-rw-r--r--tools/libxl/libxl.c172
-rw-r--r--tools/libxl/libxl.h4
-rw-r--r--tools/libxl/libxl_types.idl2
-rw-r--r--tools/libxl/libxl_utils.c18
-rw-r--r--tools/libxl/xl_cmdimpl.c29
5 files changed, 154 insertions, 71 deletions
diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c
index 3d74344131..07a50fa6d5 100644
--- a/tools/libxl/libxl.c
+++ b/tools/libxl/libxl.c
@@ -1291,62 +1291,140 @@ int libxl_device_nic_del(libxl_ctx *ctx, uint32_t domid,
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__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_ctx *ctx = libxl__gc_owner(gc);
+ unsigned int len;
+ char *tmp;
+ int rc;
- 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);
+ 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;
}
- 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);
}
+ return 0;
+}
+
+libxl_device_nic *libxl_device_nic_list(libxl_ctx *ctx, uint32_t domid, int *num)
+{
+ libxl__gc gc = LIBXL_INIT_GC(ctx);
+ 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 res;
-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_dispose(&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);
+ 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 0;
+}
+
/******************************************************************************/
int libxl__device_console_add(libxl__gc *gc, uint32_t domid,
libxl_device_console *console,
diff --git a/tools/libxl/libxl.h b/tools/libxl/libxl.h
index bb02c9905b..43c992921b 100644
--- a/tools/libxl/libxl.h
+++ b/tools/libxl/libxl.h
@@ -454,7 +454,9 @@ int libxl_device_disk_local_detach(libxl_ctx *ctx, libxl_device_disk *disk);
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 --git a/tools/libxl/libxl_types.idl b/tools/libxl/libxl_types.idl
index 65591de027..5a3accad40 100644
--- a/tools/libxl/libxl_types.idl
+++ b/tools/libxl/libxl_types.idl
@@ -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 --git a/tools/libxl/libxl_utils.c b/tools/libxl/libxl_utils.c
index d810f305dc..f53902a1fd 100644
--- a/tools/libxl/libxl_utils.c
+++ b/tools/libxl/libxl_utils.c
@@ -451,15 +451,15 @@ int libxl_pipe(libxl_ctx *ctx, int pipes[2])
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 *ctx, uint32_t domid,
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 dispose this NIC on exit path */
break;
}
+ libxl_device_nic_dispose(&nics[i]);
}
- for (i=0; i<nb; i++)
- libxl_nicinfo_dispose(&nics[i]);
+ for (; i<nb; i++)
+ libxl_device_nic_dispose(&nics[i]);
+
free(nics);
return rc;
}
diff --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c
index 5856303f7a..8424b66fc6 100644
--- a/tools/libxl/xl_cmdimpl.c
+++ b/tools/libxl/xl_cmdimpl.c
@@ -4025,8 +4025,9 @@ int main_networkattach(int argc, char **argv)
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;
@@ -4039,19 +4040,23 @@ int main_networklist(int argc, char **argv)
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_dispose(&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_dispose(&nicinfo);
+ }
+ libxl_device_nic_dispose(&nics[i]);
}
free(nics);
}