From: Roger Pau Monne <roger.pau@entel.upc.edu>
To: xen-devel@lists.xensource.com
Subject: [PATCH] libxl: update nic list API to use common device API style
Date: Wed, 05 Oct 2011 14:48:11 +0200 [thread overview]
Message-ID: <e94763a6bd3495cc95bd.1317818891@loki> (raw)
In-Reply-To: <cafd8b3f9e0c8bbf5c30.1317389599@localhost.localdomain>
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>
Signed-off-by: Roger Pau Monne <roger.pau@entel.upc.edu>
diff -r 25db0629e338 -r e94763a6bd34 tools/libxl/Makefile
--- a/tools/libxl/Makefile Fri Sep 30 14:27:27 2011 +0100
+++ b/tools/libxl/Makefile Fri Sep 30 14:27:27 2011 +0100
@@ -119,7 +119,7 @@ libxlutil.a: $(LIBXLU_OBJS)
$(AR) rcs libxlutil.a $^
xl: $(XL_OBJS) libxlutil.so libxenlight.so
- $(CC) $(LDFLAGS) -o $@ $(XL_OBJS) libxlutil.so $(LDLIBS_libxenlight) $(LDLIBS_libxenctrl)
+ $(CC) $(LDFLAGS) -o $@ $(XL_OBJS) libxlutil.so libxenlight.so $(LDLIBS_libxenlight) $(LDLIBS_libxenctrl)
testidl: testidl.o libxlutil.so libxenlight.so
$(CC) $(LDFLAGS) -o $@ testidl.o libxlutil.so $(LDLIBS_libxenlight) $(LDLIBS_libxenctrl)
diff -r 25db0629e338 -r e94763a6bd34 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
@@ -1285,60 +1285,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 25db0629e338 -r e94763a6bd34 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 25db0629e338 -r e94763a6bd34 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 25db0629e338 -r e94763a6bd34 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 25db0629e338 -r e94763a6bd34 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
@@ -4022,8 +4022,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;
@@ -4036,19 +4037,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);
}
next prev parent reply other threads:[~2011-10-05 12:48 UTC|newest]
Thread overview: 55+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-09-30 13:33 [PATCH 00 of 23] libxl: rationalise libxl_device_* APIs Ian Campbell
2011-09-30 13:33 ` [PATCH 01 of 23] libxl: remove hard tabs from non-generated *.[ch] Ian Campbell
2011-10-06 16:34 ` Ian Jackson
2011-10-07 8:37 ` Ian Campbell
2011-10-07 14:08 ` [PATCH 0 of 3] libxl: fixup hard tabs and indentation Ian Campbell
2011-10-07 14:08 ` [PATCH 1 of 3] libxl: expand hard tabs stops Ian Campbell
2011-10-07 14:08 ` [PATCH 2 of 3] libxl: fixup incorrect indentation Ian Campbell
2011-10-12 15:23 ` Ian Jackson
2011-10-07 14:08 ` [PATCH 3 of 3] libxl: add emacs local variables block to turn on four space indentation Ian Campbell
2011-10-10 11:09 ` Ian Jackson
2011-09-30 13:33 ` [PATCH 02 of 23] libxl: add a comment describing the device interfaces Ian Campbell
2011-10-04 8:53 ` Dave Scott
2011-10-04 9:38 ` Ian Campbell
2011-10-11 12:59 ` Roger Pau Monné
2011-10-12 15:27 ` Ian Campbell
2011-09-30 13:33 ` [PATCH 03 of 23] libxl: various fixes to libxl_device_disk_list (and internals) Ian Campbell
2011-09-30 13:33 ` [PATCH 04 of 23] libxl: refactor code to construct disk from xenstore backend Ian Campbell
2011-09-30 13:33 ` [PATCH 05 of 23] libxl: do not read f.e. xenstore dir in disk list function Ian Campbell
2011-09-30 13:33 ` [PATCH 06 of 23] libxl: update nic list API to use common device API style Ian Campbell
2011-10-05 12:48 ` Roger Pau Monne [this message]
2011-10-05 13:10 ` [PATCH] " Ian Campbell
2011-10-05 13:22 ` Roger Pau Monné
2011-10-05 13:42 ` Ian Campbell
2011-09-30 13:33 ` [PATCH 07 of 23] libxl: reimplement devid->nic in terms of from_xs_be function Ian Campbell
2011-09-30 13:33 ` [PATCH 08 of 23] libxl: reimplement devid->disk " Ian Campbell
2011-09-30 13:33 ` [PATCH 09 of 23] libxl: libxl_devid_to_* should take an integer device id Ian Campbell
2011-09-30 13:33 ` [PATCH 10 of 23] libxl: separate forced and non-forced device remove Ian Campbell
2011-10-06 16:38 ` Ian Jackson
2011-10-07 8:44 ` Ian Campbell
2011-10-07 13:08 ` Ian Jackson
2011-09-30 13:33 ` [PATCH 11 of 23] libxl: split forced and non-forced uses of libxl__device_del Ian Campbell
2011-09-30 13:33 ` [PATCH 12 of 23] libxl: use IDL to define device front- and back-end kinds Ian Campbell
2011-09-30 13:33 ` [PATCH 13 of 23] libxl: do not remove unidentified frontend paths in libxl__devices_destroy Ian Campbell
2011-09-30 13:33 ` [PATCH 14 of 23] libxl: use libxl__device in libxl_devices_destroy and libxl__device_pci_remove_xenstore Ian Campbell
2011-10-07 12:18 ` Roger Pau Monné
2011-10-07 12:24 ` Ian Campbell
2011-10-07 12:26 ` Ian Campbell
2011-09-30 13:33 ` [PATCH 15 of 23] libxl: merge libxl__device_del into libxl__device_remove Ian Campbell
2011-09-30 13:33 ` [PATCH 16 of 23] libxl: use more descriptive variable names in libxl__devices_destroy Ian Campbell
2011-09-30 13:33 ` [PATCH 17 of 23] libxl: convert disk handling to device API Ian Campbell
2011-09-30 13:33 ` [PATCH 18 of 23] libxl: convert NIC " Ian Campbell
2011-09-30 13:33 ` [PATCH 19 of 23] libxl: remove libxl_device_console_add Ian Campbell
2011-09-30 13:33 ` [PATCH 20 of 23] libxl: convert VKB handling to device API Ian Campbell
2011-09-30 13:33 ` [PATCH 21 " Ian Campbell
2011-09-30 13:33 ` [PATCH 22 of 23] libxl: reorder device functions to put functions for each device together Ian Campbell
2011-09-30 13:33 ` [PATCH 23 of 23] libxl: convert PCI device handling to device API Ian Campbell
2011-10-04 7:32 ` [PATCH 00 of 23] libxl: rationalise libxl_device_* APIs Roger Pau Monné
2011-10-04 8:12 ` Ian Campbell
2011-10-04 8:32 ` Ian Campbell
2011-10-05 9:04 ` Roger Pau Monné
2011-10-05 9:33 ` Ian Campbell
2011-10-05 11:59 ` Roger Pau Monné
2011-10-05 13:08 ` Ian Campbell
2011-10-05 13:15 ` Roger Pau Monné
2011-10-06 16:43 ` Ian Jackson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=e94763a6bd3495cc95bd.1317818891@loki \
--to=roger.pau@entel.upc.edu \
--cc=xen-devel@lists.xensource.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.