From: Gianni Tedesco Subject: [PATCH]: xl: use libuuid to generate random UUID's Date: Wed, 4 Aug 2010 18:49:22 +0100 Message-ID: <1280944162.18490.219.camel@qabil.uk.xensource.com> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: Xen Devel Cc: Ian Jackson , Stefano Stabellini List-Id: xen-devel@lists.xenproject.org Link to libuuid in xl et al and bring in the header file. Use libuuid to generate UUID's which are actually UU and not merely pseudo-random :) As a side-effect I have also used libuuid to generate more random MAC addresses rather than keeping the old code and simply xoring getpid() in to the PRNG seed. diff -r 668f71224241 tools/libxl/Makefile --- a/tools/libxl/Makefile Mon Aug 02 16:58:50 2010 +0100 +++ b/tools/libxl/Makefile Wed Aug 04 18:48:10 2010 +0100 @@ -15,7 +15,7 @@ CFLAGS += -Werror -Wno-format-zero-lengt CFLAGS += -I. -fPIC CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_libxenguest) $(CFLAGS_libxenstore) $(CFLAGS_libblktapctl) -LIBS = $(LDFLAGS_libxenctrl) $(LDFLAGS_libxenguest) $(LDFLAGS_libxenstore) $(LDFLAGS_libblktapctl) $(UTIL_LIBS) +LIBS = $(LDFLAGS_libxenctrl) $(LDFLAGS_libxenguest) $(LDFLAGS_libxenstore) $(LDFLAGS_libblktapctl) $(UTIL_LIBS) -luuid 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 668f71224241 tools/libxl/libxl.h --- a/tools/libxl/libxl.h Mon Aug 02 16:58:50 2010 +0100 +++ b/tools/libxl/libxl.h Wed Aug 04 18:48:10 2010 +0100 @@ -22,8 +22,10 @@ #include #include /* for pid_t */ +#include + typedef struct { - uint8_t uuid[16]; + uuid_t uuid; uint32_t domid; uint8_t running:1; uint8_t blocked:1; @@ -50,7 +52,7 @@ typedef struct { } libxl_poolinfo; typedef struct { - uint8_t uuid[16]; + uuid_t uuid; uint32_t domid; } libxl_vminfo; @@ -93,7 +95,7 @@ typedef struct { bool oos; int ssidref; char *name; - uint8_t uuid[16]; + uuid_t uuid; char **xsdata; char **platformdata; uint32_t poolid; @@ -173,7 +175,7 @@ typedef enum { typedef struct { int domid; - uint8_t uuid[16]; /* this is use only with stubdom, and must be different from the domain uuid */ + uuid_t uuid; /* this is use only with stubdom, and must be different from the domain uuid */ char *dom_name; char *device_model; char *saved_state; @@ -338,7 +340,7 @@ int libxl_domain_suspend(libxl_ctx *ctx, int libxl_domain_resume(libxl_ctx *ctx, uint32_t domid); int libxl_domain_shutdown(libxl_ctx *ctx, uint32_t domid, int req); int libxl_domain_destroy(libxl_ctx *ctx, uint32_t domid, int force); -int libxl_domain_preserve(libxl_ctx *ctx, uint32_t domid, libxl_domain_create_info *info, const char *name_suffix, uint8_t new_uuid[16]); +int libxl_domain_preserve(libxl_ctx *ctx, uint32_t domid, libxl_domain_create_info *info, const char *name_suffix, uuid_t new_uuid); int libxl_file_reference_map(libxl_ctx *ctx, libxl_file_reference *f); int libxl_file_reference_unmap(libxl_ctx *ctx, libxl_file_reference *f); @@ -360,7 +362,7 @@ int libxl_run_bootloader(libxl_ctx *ctx, libxl_device_disk *disk, uint32_t domid); -char *libxl_uuid2string(libxl_ctx *ctx, const uint8_t uuid[16]); +char *libxl_uuid2string(libxl_ctx *ctx, const uuid_t uuid); /* 0 means ERROR_ENOMEM, which we have logged */ /* events handling */ diff -r 668f71224241 tools/libxl/xl.c --- a/tools/libxl/xl.c Mon Aug 02 16:58:50 2010 +0100 +++ b/tools/libxl/xl.c Wed Aug 04 18:48:10 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 668f71224241 tools/libxl/xl_cmdimpl.c --- a/tools/libxl/xl_cmdimpl.c Mon Aug 02 16:58:50 2010 +0100 +++ b/tools/libxl/xl_cmdimpl.c Wed Aug 04 18:48:10 2010 +0100 @@ -255,19 +255,12 @@ static void init_build_info(libxl_domain } } -static void random_uuid(uint8_t *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[0]); + uuid_generate(dm_info->uuid); dm_info->dom_name = c_info->name; dm_info->device_model = "qemu-dm"; @@ -294,7 +287,10 @@ static void init_dm_info(libxl_device_mo static void init_nic_info(libxl_device_nic *nic_info, int devnum) { + uuid_t seed; + memset(nic_info, '\0', sizeof(*nic_info)); + uuid_generate(seed); nic_info->backend_domid = 0; nic_info->domid = 0; @@ -304,9 +300,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] = seed[0] & 0x7f; + nic_info->mac[4] = seed[1]; + nic_info->mac[5] = seed[2]; nic_info->ifname = NULL; nic_info->bridge = "xenbr0"; CHK_ERRNO( asprintf(&nic_info->script, "%s/vif-bridge", @@ -316,21 +312,24 @@ static void init_nic_info(libxl_device_n static void init_net2_info(libxl_device_net2 *net2_info, int devnum) { + uuid_t seed; + memset(net2_info, '\0', sizeof(*net2_info)); + uuid_generate(seed); 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] = seed[0] & 0x7f; + net2_info->front_mac[4] = seed[1]; + net2_info->front_mac[5] = seed[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] = seed[3] & 0x7f; + net2_info->back_mac[4] = seed[4]; + net2_info->back_mac[5] = seed[5]; net2_info->back_trusted = 1; net2_info->filter_mac = 1; net2_info->max_bypasses = 5; @@ -576,7 +575,7 @@ static void parse_config_data(const char c_info->name = strdup(buf); else c_info->name = "test"; - random_uuid(&c_info->uuid[0]); + uuid_generate(c_info->uuid); if (!xlu_cfg_get_long(config, "oos", &l)) c_info->oos = l; @@ -1192,7 +1191,7 @@ static int preserve_domain(libxl_ctx *ct return 0; } - random_uuid(&new_uuid[0]); + 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);