[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH,v2] xl: randomly generate UUID's



This patch converts xl to randomly generate UUID's rather than using a
dodgy time-seeded PRNG. I have incorporated Cristoph Eggers suggestions
wrt. portability so I would be grateful for an ACK on the NetBSD side of
things. This also folds in the "v2: xl: make libxl_uuid2string internal
to libxenlight" patch since it couldn't be avoided in fixing up xl
printfs() which grok around in the uuid type directly.

I have ignored various suggestions so far on auto-generation of MAC
addresses and left it as a topic for a future patch to solve. In other
words the behaviour stays the same it's just using a true random source.

This will probably conflict horribly with "libxl: autogenerate type
definitions and destructor functions" so if it's acked in principle I
can re-base and re-send on top of that.

Signed-off-by: Gianni Tedesco <gianni.tedesco@xxxxxxxxxx>

diff -r 85bd0f6e8fed tools/libxl/Makefile
--- a/tools/libxl/Makefile      Fri Aug 13 13:55:51 2010 +0100
+++ b/tools/libxl/Makefile      Mon Aug 16 15:39:13 2010 +0100
@@ -16,6 +16,9 @@ CFLAGS += -I. -fPIC
 CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_libxenguest) $(CFLAGS_libxenstore) 
$(CFLAGS_libblktapctl)
 
 LIBS = $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxenstore) 
$(LDLIBS_libblktapctl) $(UTIL_LIBS)
+ifeq ($(CONFIG_Linux),y)
+LIBS += -luuid
+endif
 
 LIBXL_OBJS-y = osdeps.o libxl_paths.o libxl_bootloader.o
 LIBXL_OBJS = flexarray.o libxl.o libxl_pci.o libxl_dom.o libxl_exec.o 
libxl_xshelp.o libxl_device.o libxl_internal.o xenguest.o libxl_utils.o 
$(LIBXL_OBJS-y)
diff -r 85bd0f6e8fed tools/libxl/libxl.c
--- a/tools/libxl/libxl.c       Fri Aug 13 13:55:51 2010 +0100
+++ b/tools/libxl/libxl.c       Mon Aug 16 15:39:13 2010 +0100
@@ -90,7 +90,7 @@ int libxl_domain_make(libxl_ctx *ctx, li
     xs_transaction_t t;
     xen_domain_handle_t handle;
 
-    uuid_string = libxl_uuid2string(ctx, info->uuid);
+    uuid_string = libxl_uuid2string(&gc, info->uuid);
     if (!uuid_string) {
         libxl_free_all(&gc);
         return ERROR_NOMEM;
@@ -102,7 +102,7 @@ int libxl_domain_make(libxl_ctx *ctx, li
     *domid = -1;
 
     /* Ultimately, handle is an array of 16 uint8_t, same as uuid */
-    memcpy(handle, info->uuid, sizeof(xen_domain_handle_t));
+    libxl_uuid_copy((libxl_uuid *)handle, &info->uuid);
 
     ret = xc_domain_create(ctx->xch, info->ssidref, handle, flags, domid);
     if (ret < 0) {
@@ -453,7 +453,7 @@ int libxl_domain_preserve(libxl_ctx *ctx
         return ERROR_NOMEM;
     }
 
-    uuid_string = libxl_uuid2string(ctx, new_uuid);
+    uuid_string = libxl_uuid2string(&gc, new_uuid);
     if (!uuid_string) {
         libxl_free_all(&gc);
         return ERROR_NOMEM;
@@ -1458,8 +1458,8 @@ static int libxl_create_stubdom(libxl_ct
     memset(&c_info, 0x00, sizeof(libxl_domain_create_info));
     c_info.hvm = 0;
     c_info.name = libxl_sprintf(&gc, "%s-dm", _libxl_domid_to_name(&gc, 
info->domid));
-    for (i = 0; i < 16; i++)
-        c_info.uuid[i] = info->uuid[i];
+
+    libxl_uuid_copy(&c_info.uuid, &info->uuid);
 
     memset(&b_info, 0x00, sizeof(libxl_domain_build_info));
     b_info.max_vcpus = 1;
@@ -2785,7 +2785,7 @@ int libxl_set_memory_target(libxl_ctx *c
     if (rc != 1 || info.domain != domid)
         goto out;
     xcinfo2xlinfo(&info, &ptr);
-    uuid = libxl_uuid2string(ctx, ptr.uuid);
+    uuid = libxl_uuid2string(&gc, ptr.uuid);
     libxl_xs_write(&gc, XBT_NULL, libxl_sprintf(&gc, "/vm/%s/memory", uuid), 
"%"PRIu32, target_memkb / 1024);
 
     if (enforce || !domid)
diff -r 85bd0f6e8fed tools/libxl/libxl.h
--- a/tools/libxl/libxl.h       Fri Aug 13 13:55:51 2010 +0100
+++ b/tools/libxl/libxl.h       Mon Aug 16 15:39:13 2010 +0100
@@ -21,8 +21,7 @@
 #include <xenctrl.h>
 #include <xs.h>
 #include <sys/wait.h> /* for pid_t */
-
-typedef uint8_t libxl_uuid[16];
+#include "xl_uuid.h"
 
 typedef uint8_t libxl_mac[6];
 
@@ -362,7 +361,6 @@ int libxl_run_bootloader(libxl_ctx *ctx,
                          libxl_device_disk *disk,
                          uint32_t domid);
 
-char *libxl_uuid2string(libxl_ctx *ctx, const libxl_uuid uuid);
   /* 0 means ERROR_ENOMEM, which we have logged */
 
 /* events handling */
diff -r 85bd0f6e8fed tools/libxl/libxl_dom.c
--- a/tools/libxl/libxl_dom.c   Fri Aug 13 13:55:51 2010 +0100
+++ b/tools/libxl/libxl_dom.c   Mon Aug 16 15:39:13 2010 +0100
@@ -442,19 +442,12 @@ int save_device_model(libxl_ctx *ctx, ui
     return 0;
 }
 
-char *libxl_uuid2string(libxl_ctx *ctx, const libxl_uuid uuid)
+char *libxl_uuid2string(libxl_gc *gc, const libxl_uuid uuid)
 {
-    libxl_gc gc = LIBXL_INIT_GC(ctx);
-    char *s = string_of_uuid(&gc, uuid);
-    char *ret;
-    if (!s) {
-        XL_LOG(ctx, XL_LOG_ERROR, "cannot allocate for uuid");
-        ret = NULL;
-    }else{
-        ret = strdup(s);
-    }
-    libxl_free_all(&gc);
-    return ret;
+    char *s = libxl_sprintf(gc, LIBXL_UUID_FMT, LIBXL_UUID_BYTES(uuid));
+    if (!s)
+        XL_LOG(libxl_gc_owner(gc), XL_LOG_ERROR, "cannot allocate for uuid");
+    return s;
 }
 
 static const char *userdata_path(libxl_gc *gc, uint32_t domid,
@@ -472,7 +465,7 @@ static const char *userdata_path(libxl_g
                      " for domain %"PRIu32, domid);
         return NULL;
     }
-    uuid_string = string_of_uuid(gc, info.uuid);
+    uuid_string = libxl_sprintf(gc, LIBXL_UUID_FMT, 
LIBXL_UUID_BYTES(info.uuid));
 
     path = libxl_sprintf(gc, "/var/lib/xen/"
                          "userdata-%s.%s.%s",
diff -r 85bd0f6e8fed tools/libxl/libxl_internal.h
--- a/tools/libxl/libxl_internal.h      Fri Aug 13 13:55:51 2010 +0100
+++ b/tools/libxl/libxl_internal.h      Mon Aug 16 15:39:13 2010 +0100
@@ -106,12 +106,6 @@ typedef struct {
 
 #define PRINTF_ATTRIBUTE(x, y) __attribute__((format(printf, x, y)))
 
-#define UUID_FMT 
"%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
-#define string_of_uuid(ctx, u) \
-    libxl_sprintf(ctx, UUID_FMT, \
-                (u)[0], (u)[1], (u)[2], (u)[3], (u)[4], (u)[5], (u)[6], 
(u)[7], \
-                (u)[8], (u)[9], (u)[10], (u)[11], (u)[12], (u)[13], (u)[14], 
(u)[15])
-
 _hidden int xs_writev(struct xs_handle *xsh, xs_transaction_t t, char *dir, 
char *kvs[]);
 
 typedef struct {
@@ -249,4 +243,6 @@ _hidden char *libxl_abs_path(libxl_gc *g
 _hidden char *_libxl_domid_to_name(libxl_gc *gc, uint32_t domid);
 _hidden char *_libxl_poolid_to_name(libxl_gc *gc, uint32_t poolid);
 
+_hidden char *libxl_uuid2string(libxl_gc *gc, const libxl_uuid uuid);
+
 #endif
diff -r 85bd0f6e8fed tools/libxl/xl.c
--- a/tools/libxl/xl.c  Fri Aug 13 13:55:51 2010 +0100
+++ b/tools/libxl/xl.c  Mon Aug 16 15:39:13 2010 +0100
@@ -69,8 +69,6 @@ int main(int argc, char **argv)
         exit(1);
     }
 
-    srand(time(0));
-
     cspec = cmdtable_lookup(cmd);
     if (cspec)
         ret = cspec->cmd_impl(argc, argv);
diff -r 85bd0f6e8fed tools/libxl/xl_cmdimpl.c
--- a/tools/libxl/xl_cmdimpl.c  Fri Aug 13 13:55:51 2010 +0100
+++ b/tools/libxl/xl_cmdimpl.c  Mon Aug 16 15:39:13 2010 +0100
@@ -40,8 +40,6 @@
 #include "libxlutil.h"
 #include "xl.h"
 
-#define UUID_FMT 
"%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
-
 #define CHK_ERRNO( call ) ({                                            \
         int chk_errno = (call);                                         \
         if (chk_errno < 0) {                                                \
@@ -265,19 +263,12 @@ static void init_build_info(libxl_domain
     }
 }
 
-static void random_uuid(libxl_uuid *uuid)
-{
-    int i;
-    for (i = 0; i < 16; i++)
-        (*uuid)[i] = rand();
-}
-
 static void init_dm_info(libxl_device_model_info *dm_info,
         libxl_domain_create_info *c_info, libxl_domain_build_info *b_info)
 {
     memset(dm_info, '\0', sizeof(*dm_info));
 
-    random_uuid(&dm_info->uuid);
+    libxl_uuid_generate(&dm_info->uuid);
 
     dm_info->dom_name = c_info->name;
     dm_info->device_model = "qemu-dm";
@@ -304,6 +295,11 @@ static void init_dm_info(libxl_device_mo
 
 static void init_nic_info(libxl_device_nic *nic_info, int devnum)
 {
+    const uint8_t *r;
+    libxl_uuid uuid;
+
+    libxl_uuid_generate(&uuid);
+    r = libxl_uuid_bytearray(&uuid);
     memset(nic_info, '\0', sizeof(*nic_info));
 
     nic_info->backend_domid = 0;
@@ -314,9 +310,9 @@ static void init_nic_info(libxl_device_n
     nic_info->mac[0] = 0x00;
     nic_info->mac[1] = 0x16;
     nic_info->mac[2] = 0x3e;
-    nic_info->mac[3] = 1 + (int) (0x7f * (rand() / (RAND_MAX + 1.0)));
-    nic_info->mac[4] = 1 + (int) (0xff * (rand() / (RAND_MAX + 1.0)));
-    nic_info->mac[5] = 1 + (int) (0xff * (rand() / (RAND_MAX + 1.0)));
+    nic_info->mac[3] = r[0] & 0x7f;
+    nic_info->mac[4] = r[1];
+    nic_info->mac[5] = r[2];
     nic_info->ifname = NULL;
     nic_info->bridge = "xenbr0";
     CHK_ERRNO( asprintf(&nic_info->script, "%s/vif-bridge",
@@ -326,21 +322,26 @@ static void init_nic_info(libxl_device_n
 
 static void init_net2_info(libxl_device_net2 *net2_info, int devnum)
 {
+    const uint8_t *r;
+    libxl_uuid uuid;
+
+    libxl_uuid_generate(&uuid);
+    r = libxl_uuid_bytearray(&uuid);
     memset(net2_info, '\0', sizeof(*net2_info));
 
     net2_info->devid = devnum;
     net2_info->front_mac[0] = 0x00;
     net2_info->front_mac[1] = 0x16;
     net2_info->front_mac[2] = 0x3e;;
-    net2_info->front_mac[3] = 1 + (int) (0x7f * (rand() / (RAND_MAX + 1.0)));
-    net2_info->front_mac[4] = 1 + (int) (0xff * (rand() / (RAND_MAX + 1.0)));
-    net2_info->front_mac[5] = 1 + (int) (0xff * (rand() / (RAND_MAX + 1.0)));
+    net2_info->front_mac[3] = 0x7f & r[0];
+    net2_info->front_mac[4] = r[1];
+    net2_info->front_mac[5] = r[2];
     net2_info->back_mac[0] = 0x00;
     net2_info->back_mac[1] = 0x16;
     net2_info->back_mac[2] = 0x3e;
-    net2_info->back_mac[3] = 1 + (int) (0x7f * (rand() / (RAND_MAX + 1.0)));
-    net2_info->back_mac[4] = 1 + (int) (0xff * (rand() / (RAND_MAX + 1.0)));
-    net2_info->back_mac[5] = 1 + (int) (0xff * (rand() / (RAND_MAX + 1.0)));
+    net2_info->back_mac[3] = 0x7f & r[3];
+    net2_info->back_mac[4] = r[4];
+    net2_info->back_mac[5] = r[5];
     net2_info->back_trusted = 1;
     net2_info->filter_mac = 1;
     net2_info->max_bypasses = 5;
@@ -391,11 +392,7 @@ static void printf_info(int domid,
     printf("\t(oos %d)\n", c_info->oos);
     printf("\t(ssidref %d)\n", c_info->ssidref);
     printf("\t(name %s)\n", c_info->name);
-    printf("\t(uuid " UUID_FMT ")\n",
-           (c_info->uuid)[0], (c_info->uuid)[1], (c_info->uuid)[2], 
(c_info->uuid)[3],
-           (c_info->uuid)[4], (c_info->uuid)[5], (c_info->uuid)[6], 
(c_info->uuid)[7],
-           (c_info->uuid)[8], (c_info->uuid)[9], (c_info->uuid)[10], 
(c_info->uuid)[11],
-           (c_info->uuid)[12], (c_info->uuid)[13], (c_info->uuid)[14], 
(c_info->uuid)[15]);
+    printf("\t(uuid " LIBXL_UUID_FMT ")\n", LIBXL_UUID_BYTES(c_info->uuid));
     printf("\t(cpupool %s (%d))\n", c_info->poolname, c_info->poolid);
     if (c_info->xsdata)
         printf("\t(xsdata contains data)\n");
@@ -586,7 +583,7 @@ static void parse_config_data(const char
         c_info->name = strdup(buf);
     else
         c_info->name = "test";
-    random_uuid(&c_info->uuid);
+    libxl_uuid_generate(&c_info->uuid);
 
     if (!xlu_cfg_get_long(config, "oos", &l))
         c_info->oos = l;
@@ -1181,7 +1178,7 @@ static int preserve_domain(libxl_ctx *ct
         return 0;
     }
 
-    random_uuid(&new_uuid);
+    libxl_uuid_generate(&new_uuid);
 
     LOG("Preserving domain %d %s with suffix%s", domid, d_config->c_info.name, 
stime);
     rc = libxl_domain_preserve(ctx, domid, &d_config->c_info, stime, new_uuid);
@@ -2169,10 +2166,8 @@ void list_domains(int verbose, const lib
                 info[i].dying ? 'd' : '-',
                 ((float)info[i].cpu_time / 1e9));
         free(domname);
-        if (verbose) {
-            char *uuid = libxl_uuid2string(&ctx, info[i].uuid);
-            printf(" %s", uuid);
-        }
+        if (verbose)
+            printf(" " LIBXL_UUID_FMT, LIBXL_UUID_BYTES(info[i].uuid));
         putchar('\n');
     }
 }
@@ -2192,11 +2187,7 @@ void list_vm(void)
     printf("UUID                                  ID    name\n");
     for (i = 0; i < nb_vm; i++) {
         domname = libxl_domid_to_name(&ctx, info[i].domid);
-        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],
+        printf(LIBXL_UUID_FMT "  %d    %-30s\n", 
LIBXL_UUID_BYTES(info[i].uuid),
             info[i].domid, domname);
         free(domname);
     }
diff -r 85bd0f6e8fed tools/libxl/xl_uuid.h
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/libxl/xl_uuid.h     Mon Aug 16 15:39:13 2010 +0100
@@ -0,0 +1,151 @@
+/* Copyright (c) 2008, XenSource Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in the
+ *       documentation and/or other materials provided with the distribution.
+ *     * Neither the name of XenSource Inc. nor the names of its contributors
+ *       may be used to endorse or promote products derived from this software
+ *       without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+#ifndef __LIBXL_UUID_H__
+#define __LIBXL_UUID_H__
+
+#define LIBXL_UUID_FMT 
"%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
+
+#if defined(__linux__)
+
+#include <uuid/uuid.h>
+
+typedef struct {
+    uuid_t uuid;
+} libxl_uuid;
+
+#define LIBXL_UUID_BYTES(arg) arg.uuid[0], arg.uuid[1], arg.uuid[2], 
arg.uuid[3], \
+            arg.uuid[4], arg.uuid[5], arg.uuid[6], arg.uuid[7], \
+            arg.uuid[8], arg.uuid[9], arg.uuid[10], arg.uuid[11], \
+            arg.uuid[12], arg.uuid[13], arg.uuid[14], arg.uuid[15] \
+
+static inline int libxl_uuid_is_nil(libxl_uuid *uuid)
+{
+       return uuid_is_null(uuid->uuid);
+}
+
+static inline void libxl_uuid_generate(libxl_uuid *uuid)
+{
+       uuid_generate(uuid->uuid);
+}
+
+static inline void libxl_uuido_string(libxl_uuid *uuid, char *out, size_t size)
+{
+       uuid_unparse(uuid->uuid, out);
+}
+
+static inline void libxl_uuid_from_string(libxl_uuid *uuid, const char *in)
+{
+       uuid_parse(in, uuid->uuid);
+}
+
+static inline void libxl_uuid_copy(libxl_uuid *dst, libxl_uuid *src)
+{
+       uuid_copy(dst->uuid, src->uuid);
+}
+
+static inline void libxl_uuid_clear(libxl_uuid *uuid)
+{
+       uuid_clear(uuid->uuid);
+}
+
+static inline int libxl_uuid_compare(libxl_uuid *uuid1, libxl_uuid *uuid2)
+{
+       return uuid_compare(uuid1->uuid, uuid2->uuid);
+}
+
+static inline const uint8_t *libxl_uuid_bytearray(libxl_uuid *uuid)
+{
+    return uuid->uuid;
+}
+
+#elif defined(__NetBSD__)
+
+#include <uuid.h>
+#include <string.h>
+#include <stdlib.h>
+
+typedef uuid_t libxl_uuid;
+#define LIBXL_UUID_BYTES(uuid) uuid[0], uuid[1], uuid[2], uuid[3], \
+            uuid[4], uuid[5], uuid[6], uuid[7], \
+            uuid[8], uuid[9], uuid[10], uuid[11], \
+            uuid[12], uuid[13], uuid[14], uuid[15] \
+
+static inline int libxl_uuid_is_nil(libxl_uuid *uuid)
+{
+       uint32_t status;
+       return uuid_is_nil((uuid_t *)uuid, &status);
+}
+
+static inline void libxl_uuid_generate(libxl_uuid *uuid)
+{
+       uint32_t status;
+       uuid_create((uuid_t *)uuid, &status);
+}
+
+static inline void libxl_uuido_string(libxl_uuid *uuid, char *out, size_t size)
+{
+       uint32_t status;
+       char *_out = NULL;
+       uuid_to_string((uuid_t *)uuid, &_out, &status);
+       strlcpy(out, _out, size);
+       free(_out);
+}
+
+static inline void libxl_uuid_from_string(libxl_uuid *uuid, const char *in)
+{
+       uint32_t status;
+       uuid_from_string(in, (uuid_t *)uuid, &status);
+}
+
+static inline void libxl_uuid_copy(libxl_uuid *dst, libxl_uuid *src)
+{
+       memcpy((uuid_t *)dst, (uuid_t *)src, sizeof(uuid_t));
+}
+
+static inline void libxl_uuid_clear(libxl_uuid *uuid)
+{
+       memset((uuid_t *)uuid, 0, sizeof(uuid_t));
+}
+
+static inline int libxl_uuid_compare(libxl_uuid *uuid1, libxl_uuid *uuid2)
+{
+       uint32_t status;
+       return uuid_compare((uuid_t *)uuid1, (uuid_t *)uuid2, &status);
+}
+
+static inline const uint8_t *libxl_uuid_bytearray(libxl_uuid *uuid)
+{
+    return uuid;
+}
+
+#else
+
+#error "Please update libxl_uuid.h for your OS"
+
+#endif
+
+#endif /* __LIBXL_UUID_H__ */



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


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.