Skip to contentSkip to conversation

domain/vm list API outlining

PATCH New Vincent Hanquez 1 participant 3 messages Jan 17, 2010

lore
Share & tools
lore
#0 lore raw

cover domain/vm list API outlining

The API currently available in xl for listing, is not very pratical and some person have express interest having more access to the lowlevel.

Hence separating the middle level call into one low level call that lists domains, and one high level call that lists vms. This small patchset does that and add a list-vm options to xl to be able to list only VMs compared to domains.

also a couple of renames around lists, and is_stubdom is more resiliant to possible unexpected value.

Vincent Hanquez (2):
separate logically list_vm and list_domain
add a list-vm options to xl that only list vms uuid,domid,name

 tools/libxl/libxl.c       |   53 ++++++++++++++++++++++++++++++++++----------
 tools/libxl/libxl.h       |   12 +++++++++-
 tools/libxl/libxl_utils.c |   19 +++++++++++-----
 tools/libxl/libxl_utils.h |    2 +-
 tools/libxl/xl.c          |   53 ++++++++++++++++++++++++++++++++++++++++++++-
 5 files changed, 118 insertions(+), 21 deletions(-)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#1 lore raw

PATCH 1/2 separate logically list_vm and list_domain

previously list_domain was something between listing VM and domains. provide 2 separates API calls to list domains and list vms. the list vms API filters utility domains like stubdomains, and domain 0

change is_stubdom to properly check the integer and also return a boolean value.

---
 tools/libxl/libxl.c       |   53 ++++++++++++++++++++++++++++++++++----------
 tools/libxl/libxl.h       |   12 +++++++++-
 tools/libxl/libxl_utils.c |   19 +++++++++++-----
 tools/libxl/libxl_utils.h |    2 +-
 tools/libxl/xl.c          |    2 +-
 5 files changed, 67 insertions(+), 21 deletions(-)
tools/libxl/libxl.c +41 -12
index 909bd17..ca92400 100644
@@ -34,6 +34,8 @@
 #include "libxl_internal.h"
 #include "flexarray.h"
 
+#define PAGE_TO_MEMKB(pages) ((pages) * 4)
+
 int libxl_ctx_init(struct libxl_ctx *ctx, int version)
 {
     if (version != LIBXL_VERSION)
@@ -282,21 +284,19 @@ int libxl_domain_resume(struct libxl_ctx *ctx, uint32_t domid)
     return 0;
 }
 
-struct libxl_dominfo * libxl_domain_list(struct libxl_ctx *ctx, int *nb_domain)
+struct libxl_dominfo * libxl_list_domain(struct libxl_ctx *ctx, int *nb_domain)
 {
     struct libxl_dominfo *ptr;
-    int index, i, ret, first_domain;
+    int index, i, ret;
     xc_domaininfo_t info[1024];
     int size = 1024;
 
-    first_domain = 1;
-    index = 0;
     ptr = calloc(size, sizeof(struct libxl_dominfo));
     if (!ptr)
         return NULL;
 
-    ret = xc_domain_getinfolist(ctx->xch, first_domain, 1024, info);
-    for (i = 0; i < ret; i++) {
+    ret = xc_domain_getinfolist(ctx->xch, 0, 1024, info);
+    for (index = i = 0; i < ret; i++) {
         memcpy(&(ptr[index].uuid), info[i].handle, sizeof(xen_domain_handle_t));
         ptr[index].domid = info[i].domain;
 
@@ -306,14 +306,42 @@ struct libxl_dominfo * libxl_domain_list(struct libxl_ctx *ctx, int *nb_domain)
             ptr[index].paused = 1;
         else if (info[i].flags & XEN_DOMINF_blocked || info[i].flags & XEN_DOMINF_running)
             ptr[index].running = 1;
+        ptr[index].max_memkb = PAGE_TO_MEMKB(info[i].max_pages);
+        ptr[index].cpu_time = info[i].cpu_time;
+        ptr[index].vcpu_max_id = info[i].max_vcpu_id;
+        ptr[index].vcpu_online = info[i].nr_online_vcpus;
 
-        first_domain = info[i].domain + 1;
         index++;
     }
     *nb_domain = index;
     return ptr;
 }
 
+/* this API call only list VM running on this host. a VM can be an aggregate of multiple domains. */
+struct libxl_vminfo * libxl_list_vm(struct libxl_ctx *ctx, int *nb_vm)
+{
+    struct libxl_vminfo *ptr;
+    int index, i, ret;
+    xc_domaininfo_t info[1024];
+    int size = 1024;
+
+    ptr = calloc(size, sizeof(struct libxl_dominfo));
+    if (!ptr)
+        return NULL;
+
+    ret = xc_domain_getinfolist(ctx->xch, 1, 1024, info);
+    for (index = i = 0; i < ret; i++) {
+        if (libxl_is_stubdom(ctx, info[i].domain, NULL))
+            continue;
+        memcpy(&(ptr[index].uuid), info[i].handle, sizeof(xen_domain_handle_t));
+        ptr[index].domid = info[i].domain;
+
+        index++;
+    }
+    *nb_vm = index;
+    return ptr;
+}
+
 int libxl_domain_suspend(struct libxl_ctx *ctx, libxl_domain_suspend_info *info,
                          uint32_t domid, int fd)
 {
@@ -1530,7 +1558,8 @@ static int libxl_build_xenpv_qemu_args(struct libxl_ctx *ctx,
                                        libxl_device_vfb *vfb,
                                        int num_console,
                                        libxl_device_console *console,
-                                       libxl_device_model_info *info) {
+                                       libxl_device_model_info *info)
+{
     int i = 0, j = 0, num = 0;
     memset(info, 0x00, sizeof(libxl_device_model_info));
 
@@ -1548,8 +1577,8 @@ static int libxl_build_xenpv_qemu_args(struct libxl_ctx *ctx,
             num++;
     }
     if (num > 0) {
-        uint32_t guest_domid = libxl_is_stubdom(ctx, vfb->domid);
-        if (guest_domid) {
+        uint32_t guest_domid;
+        if (libxl_is_stubdom(ctx, vfb->domid, &guest_domid)) {
             char *filename;
             char *name = libxl_sprintf(ctx, "qemu-dm-%s", libxl_domid_to_name(ctx, guest_domid));
             libxl_create_logfile(ctx, name, &filename);
@@ -1988,7 +2017,7 @@ int libxl_device_pci_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci
         fclose(f);
     }
 out:
-    if (!libxl_is_stubdom(ctx, domid)) {
+    if (!libxl_is_stubdom(ctx, domid, NULL)) {
         rc = xc_assign_device(ctx->xch, domid, pcidev->value);
         if (rc < 0)
             XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_assign_device failed");
@@ -2079,7 +2108,7 @@ skip1:
 out:
     libxl_device_pci_flr(ctx, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
 
-    if (!libxl_is_stubdom(ctx, domid)) {
+    if (!libxl_is_stubdom(ctx, domid, NULL)) {
         rc = xc_deassign_device(ctx->xch, domid, pcidev->value);
         if (rc < 0)
             XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_deassign_device failed");
tools/libxl/libxl.h +11 -1
index 25a66d9..9b69772 100644
@@ -30,6 +30,15 @@ struct libxl_dominfo {
     uint8_t dying:1;
     uint8_t paused:1;
     uint8_t running:1;
+    uint64_t max_memkb;
+    uint64_t cpu_time;
+    uint32_t vcpu_max_id;
+    uint32_t vcpu_online;
+};
+
+struct libxl_vminfo {
+    uint8_t uuid[16];
+    uint32_t domid;
 };
 
 struct libxl_ctx {
@@ -295,7 +304,8 @@ int libxl_set_memory_target(struct libxl_ctx *ctx, uint32_t domid, uint32_t targ
 
 int libxl_console_attach(struct libxl_ctx *ctx, uint32_t domid, int cons_num);
 
-struct libxl_dominfo * libxl_domain_list(struct libxl_ctx *ctx, int *nb_domain);
+struct libxl_dominfo * libxl_list_domain(struct libxl_ctx *ctx, int *nb_domain);
+struct libxl_vminfo * libxl_list_vm(struct libxl_ctx *ctx, int *nb_vm);
 
 typedef struct libxl_device_model_starting libxl_device_model_starting;
 int libxl_create_device_model(struct libxl_ctx *ctx,
tools/libxl/libxl_utils.c +13 -6
index eca4347..1ba9431 100644
@@ -60,7 +60,7 @@ int libxl_name_to_domid(struct libxl_ctx *ctx, char *name, uint32_t *domid)
     char *domname;
     struct libxl_dominfo *dominfo;
 
-    dominfo = libxl_domain_list(ctx, &nb_domains);
+    dominfo = libxl_list_domain(ctx, &nb_domains);
     if (!dominfo)
         return ERROR_NOMEM;
 
@@ -85,13 +85,20 @@ int libxl_get_stubdom_id(struct libxl_ctx *ctx, int guest_domid)
         return 0;
 }
 
-int libxl_is_stubdom(struct libxl_ctx *ctx, int domid)
+int libxl_is_stubdom(struct libxl_ctx *ctx, uint32_t domid, uint32_t *target_domid)
 {
-    char *target = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/target", libxl_xs_get_dompath(ctx, domid)));
-    if (target)
-        return atoi(target);
-    else
+    char *target, *endptr;
+    uint32_t value;
+
+    target = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/target", libxl_xs_get_dompath(ctx, domid)));
+    if (!target)
+        return 0;
+    value = strtol(target, &endptr, 10);
+    if (*endptr != '\0')
         return 0;
+    if (target_domid)
+        *target_domid = value;
+    return 1;
 }
 
 int libxl_create_logfile(struct libxl_ctx *ctx, char *name, char **full_name)
tools/libxl/libxl_utils.h +1 -1
index 85bd257..05fa4e3 100644
@@ -22,7 +22,7 @@ unsigned long libxl_get_required_shadow_memory(unsigned long maxmem_kb, unsigned
 int libxl_name_to_domid(struct libxl_ctx *ctx, char *name, uint32_t *domid);
 char *libxl_domid_to_name(struct libxl_ctx *ctx, uint32_t domid);
 int libxl_get_stubdom_id(struct libxl_ctx *ctx, int guest_domid);
-int libxl_is_stubdom(struct libxl_ctx *ctx, int domid);
+int libxl_is_stubdom(struct libxl_ctx *ctx, uint32_t domid, uint32_t *target_domid);
 int libxl_create_logfile(struct libxl_ctx *ctx, char *name, char **full_name);
 int libxl_string_to_phystype(struct libxl_ctx *ctx, char *s, libxl_disk_phystype *phystype);
 
tools/libxl/xl.c +1 -1
index 1e95ac5..030d999 100644
@@ -1415,7 +1415,7 @@ void list_domains(void)
     }
     libxl_ctx_set_log(&ctx, log_callback, NULL);
 
-    info = libxl_domain_list(&ctx, &nb_domain);
+    info = libxl_list_domain(&ctx, &nb_domain);
 
     if (info < 0) {
         fprintf(stderr, "libxl_domain_infolist failed.\n");

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#2 lore raw

PATCH 2/2 add a list-vm options to xl that only list vms uuid, domid, name

In reply to Vincent Hanquez
---
 tools/libxl/xl.c |   51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 51 insertions(+), 0 deletions(-)
tools/libxl/xl.c +51 -0
index 030d999..0117e1d 100644
@@ -1433,6 +1433,36 @@ void list_domains(void)
     free(info);
 }
 
+void list_vm(void)
+{
+    struct libxl_ctx ctx;
+    struct libxl_vminfo *info;
+    int nb_vm, i;
+
+    if (libxl_ctx_init(&ctx, LIBXL_VERSION)) {
+        fprintf(stderr, "cannot init xl context\n");
+        return;
+    }
+    libxl_ctx_set_log(&ctx, log_callback, NULL);
+
+    info = libxl_list_vm(&ctx, &nb_vm);
+
+    if (info < 0) {
+        fprintf(stderr, "libxl_domain_infolist failed.\n");
+        exit(1);
+    }
+    printf("UUID                                  ID    name\n");
+    for (i = 0; i < nb_vm; i++) {
+        printf(UUID_FMT "  %d    %-30s\n",
+            info[i].uuid[0], info[i].uuid[1], info[i].uuid[2], info[i].uuid[3],
+            info[i].uuid[4], info[i].uuid[5], info[i].uuid[6], info[i].uuid[7],
+            info[i].uuid[8], info[i].uuid[9], info[i].uuid[10], info[i].uuid[11],
+            info[i].uuid[12], info[i].uuid[13], info[i].uuid[14], info[i].uuid[15],
+            info[i].domid, libxl_domid_to_name(&ctx, info[i].domid));
+    }
+    free(info);
+}
+
 int save_domain(char *p, char *filename, int checkpoint)
 {
     struct libxl_ctx ctx;
@@ -1633,6 +1663,25 @@ int main_list(int argc, char **argv)
     exit(0);
 }
 
+int main_list_vm(int argc, char **argv)
+{
+    int opt;
+
+    while ((opt = getopt(argc, argv, "h")) != -1) {
+        switch (opt) {
+        case 'h':
+            help("list-vm");
+            exit(0);
+        default:
+            fprintf(stderr, "option not supported\n");
+            break;
+        }
+    }
+
+    list_vm();
+    exit(0);
+}
+
 int main_create(int argc, char **argv)
 {
     char *filename = NULL;
@@ -1679,6 +1728,8 @@ int main(int argc, char **argv)
         main_create(argc - 1, argv + 1);
     } else if (!strcmp(argv[1], "list")) {
         main_list(argc - 1, argv + 1);
+    } else if (!strcmp(argv[1], "list-vm")) {
+        main_list_vm(argc - 1, argv + 1);
     } else if (!strcmp(argv[1], "destroy")) {
         main_destroy(argc - 1, argv + 1);
     } else if (!strcmp(argv[1], "pci-attach")) {

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel