This patch adds network-detach command to xl.
Usage: xl network-detach <Domain> <DevId|mac>
diff --git a/tools/libxl/libxl_utils.c b/tools/libxl/libxl_utils.c
--- a/tools/libxl/libxl_utils.c
+++ b/tools/libxl/libxl_utils.c
@@ -356,3 +356,77 @@ int libxl_pipe(struct libxl_ctx *ctx, in
}
return 0;
}
+
+int libxl_mac_to_device_nic(struct libxl_ctx *ctx, uint32_t domid,
+ const char *mac, libxl_device_nic *nic)
+{
+ libxl_nicinfo *nics;
+ unsigned int nb, i;
+ uint8_t mac_n[6];
+ uint8_t *a, *b;
+ const char *tok;
+ char *endptr;
+
+ nics = libxl_list_nics(ctx, domid, &nb);
+ if (!nics) {
+ return ERROR_FAIL;
+ }
+
+ for (i = 0, tok = mac; *tok && (i < 6); ++i, tok += 3) {
+ mac_n[i] = strtol(tok, &endptr, 16);
+ if (endptr != (tok + 2)) {
+ return ERROR_INVAL;
+ }
+ }
+ memset(nic, 0, sizeof (libxl_device_nic));
+ for (; nb; --nb, ++nics) {
+ for (i = 0, a = nics->mac, b = mac_n;
+ (b < mac_n + 6) && (*a == *b); ++a, ++b)
+ ;
+ if ((b >= mac_n + 6) && (*a == *b)) {
+ nic->backend_domid = nics->backend_id;
+ nic->domid = nics->frontend_id;
+ nic->devid = nics->devid;
+ memcpy(nic->mac, nics->mac, sizeof (nic->mac));
+ nic->script = nics->script;
+ libxl_free(ctx, nics);
+ return 0;
+ }
+ }
+
+ libxl_free(ctx, nics);
+ return 0;
+}
+
+int libxl_devid_to_device_nic(struct libxl_ctx *ctx, uint32_t domid,
+ const char *devid, libxl_device_nic *nic)
+{
+ char *tok, *val;
+ char *dompath, *nic_path_fe, *nic_path_be;
+ unsigned int i;
+
+ memset(nic, 0, sizeof (libxl_device_nic));
+ dompath = libxl_xs_get_dompath(ctx, domid);
+ if (!dompath) {
+ return ERROR_FAIL;
+ }
+ nic_path_fe = libxl_sprintf(ctx, "%s/device/vif/%s", dompath, devid);
+ 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));
+ nic->backend_domid = strtoul(val, NULL, 10);
+ nic->devid = strtoul(devid, NULL, 10);
+ libxl_free(ctx, val);
+
+ val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/mac",
nic_path_fe));
+ for (i = 0, tok = strtok(val, ":"); tok && (i < 6);
+ ++i, tok = strtok(NULL, ":")) {
+ nic->mac[i] = strtoul(tok, NULL, 16);
+ }
+ libxl_free(ctx, val);
+ nic->script = libxl_xs_read(ctx, XBT_NULL,
+ libxl_sprintf(ctx, "%s/script", nic_path_be));
+ libxl_free(ctx, nic_path_fe);
+ libxl_free(ctx, nic_path_be);
+ return 0;
+}
diff --git a/tools/libxl/libxl_utils.h b/tools/libxl/libxl_utils.h
--- a/tools/libxl/libxl_utils.h
+++ b/tools/libxl/libxl_utils.h
@@ -55,6 +55,12 @@ void libxl_report_child_exitstatus(struc
/* treats all exit statuses as errors; if that's not what you want,
* check status yourself first */
+
+int libxl_mac_to_device_nic(struct libxl_ctx *ctx, uint32_t domid,
+ const char *mac, libxl_device_nic *nic);
+int libxl_devid_to_device_nic(struct libxl_ctx *ctx, uint32_t domid,
+ const char *devid, libxl_device_nic *nic);
+
/* log levels: */
#define XL_LOG_DEBUG 3
#define XL_LOG_INFO 2
diff --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c
--- a/tools/libxl/xl_cmdimpl.c
+++ b/tools/libxl/xl_cmdimpl.c
@@ -3189,3 +3189,45 @@ int main_networklist(int argc, char **ar
}
exit(0);
}
+
+int main_networkdetach(int argc, char **argv)
+{
+ int opt;
+ libxl_device_nic nic;
+
+ if (argc != 3) {
+ help("network-detach");
+ exit(0);
+ }
+ while ((opt = getopt(argc, argv, "hl")) != -1) {
+ switch (opt) {
+ case 'h':
+ help("network-detach");
+ exit(0);
+ default:
+ fprintf(stderr, "option `%c' not supported.\n", opt);
+ break;
+ }
+ }
+
+ if (domain_qualifier_to_domid(argv[1], &domid, 0) < 0) {
+ fprintf(stderr, "%s is an invalid domain identifier\n", argv[1]);
+ exit(1);
+ }
+
+ if (!strchr(argv[2], ':')) {
+ if (libxl_devid_to_device_nic(&ctx, domid, argv[2], &nic)) {
+ fprintf(stderr, "Unknown device %s.\n", argv[2]);
+ exit(1);
+ }
+ } else {
+ if (libxl_mac_to_device_nic(&ctx, domid, argv[2], &nic)) {
+ fprintf(stderr, "Unknown device %s.\n", argv[2]);
+ exit(1);
+ }
+ }
+ if (libxl_device_nic_del(&ctx, &nic, 1)) {
+ fprintf(stderr, "libxl_device_nic_del failed.\n");
+ }
+ exit(0);
+}
diff --git a/tools/libxl/xl_cmdimpl.h b/tools/libxl/xl_cmdimpl.h
--- a/tools/libxl/xl_cmdimpl.h
+++ b/tools/libxl/xl_cmdimpl.h
@@ -41,5 +41,6 @@ int main_rename(int argc, char **argv);
int main_trigger(int argc, char **argv);
int main_networkattach(int argc, char **argv);
int main_networklist(int argc, char **argv);
+int main_networkdetach(int argc, char **argv);
void help(char *command);
diff --git a/tools/libxl/xl_cmdtable.c b/tools/libxl/xl_cmdtable.c
--- a/tools/libxl/xl_cmdtable.c
+++ b/tools/libxl/xl_cmdtable.c
@@ -180,6 +180,10 @@ struct cmd_spec cmd_table[] = {
&main_networklist,
"List virtual network interfaces for a domain"
},
+ { "network-detach",
+ &main_networkdetach,
+ "Destroy a domain's virtual network device"
+ },
};
int cmdtable_len = sizeof(cmd_table)/sizeof(struct cmd_spec);
_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel
|