debuggers.hg

changeset 22143:22366e13f76d

xl: randomly generate UUIDs

This patch converts xl to randomly generate UUID's rather than using a
dodgy time-seeded PRNG. 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 patch also implements the "uuid" config
file parameter in xl.

Signed-off-by: Gianni Tedesco <gianni.tedesco@citrix.com>
Acked-By: Christoph Egger <Christoph.Egger@amd.com>
Signed-off-by: Ian Jackson <ian.jackson@eu.citrix.com>
author Gianni Tedesco <gianni.tedesco@citrix.com>
date Thu Sep 02 18:12:14 2010 +0100 (2010-09-02)
parents d37dc6401a1f
children 758154658bf8
files tools/libxl/Makefile tools/libxl/libxl.c tools/libxl/libxl.h tools/libxl/libxl_uuid.h tools/libxl/xl.c tools/libxl/xl_cmdimpl.c tools/ocaml/libs/xl/xl_stubs.c
line diff
     1.1 --- a/tools/libxl/Makefile	Thu Sep 02 17:44:46 2010 +0100
     1.2 +++ b/tools/libxl/Makefile	Thu Sep 02 18:12:14 2010 +0100
     1.3 @@ -16,6 +16,9 @@ CFLAGS += -I. -fPIC
     1.4  CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_libxenguest) $(CFLAGS_libxenstore) $(CFLAGS_libblktapctl)
     1.5  
     1.6  LIBS = $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxenstore) $(LDLIBS_libblktapctl) $(UTIL_LIBS)
     1.7 +ifeq ($(CONFIG_Linux),y)
     1.8 +LIBS += -luuid
     1.9 +endif
    1.10  
    1.11  LIBXL_OBJS-y = osdeps.o libxl_paths.o libxl_bootloader.o
    1.12  ifeq ($(LIBXL_BLKTAP),y)
     2.1 --- a/tools/libxl/libxl.c	Thu Sep 02 17:44:46 2010 +0100
     2.2 +++ b/tools/libxl/libxl.c	Thu Sep 02 18:12:14 2010 +0100
     2.3 @@ -131,7 +131,7 @@ int libxl_domain_make(libxl_ctx *ctx, li
     2.4      *domid = -1;
     2.5  
     2.6      /* Ultimately, handle is an array of 16 uint8_t, same as uuid */
     2.7 -    memcpy(handle, info->uuid, sizeof(xen_domain_handle_t));
     2.8 +    libxl_uuid_copy((libxl_uuid *)handle, &info->uuid);
     2.9  
    2.10      ret = xc_domain_create(ctx->xch, info->ssidref, handle, flags, domid);
    2.11      if (ret < 0) {
    2.12 @@ -1506,8 +1506,8 @@ static int libxl_create_stubdom(libxl_ct
    2.13      memset(&c_info, 0x00, sizeof(libxl_domain_create_info));
    2.14      c_info.hvm = 0;
    2.15      c_info.name = libxl_sprintf(&gc, "%s-dm", _libxl_domid_to_name(&gc, info->domid));
    2.16 -    for (i = 0; i < 16; i++)
    2.17 -        c_info.uuid[i] = info->uuid[i];
    2.18 +
    2.19 +    libxl_uuid_copy(&c_info.uuid, &info->uuid);
    2.20  
    2.21      memset(&b_info, 0x00, sizeof(libxl_domain_build_info));
    2.22      b_info.max_vcpus = 1;
     3.1 --- a/tools/libxl/libxl.h	Thu Sep 02 17:44:46 2010 +0100
     3.2 +++ b/tools/libxl/libxl.h	Thu Sep 02 18:12:14 2010 +0100
     3.3 @@ -131,13 +131,7 @@
     3.4  #include <xs.h>
     3.5  #include <sys/wait.h> /* for pid_t */
     3.6  
     3.7 -typedef uint8_t libxl_uuid[16];
     3.8 -#define LIBXL_UUID_FMT "%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
     3.9 -#define LIBXL_UUID_BYTES(uuid) uuid[0], uuid[1], uuid[2], uuid[3], \
    3.10 -            uuid[4], uuid[5], uuid[6], uuid[7], \
    3.11 -            uuid[8], uuid[9], uuid[10], uuid[11], \
    3.12 -            uuid[12], uuid[13], uuid[14], uuid[15] \
    3.13 -
    3.14 +#include "libxl_uuid.h"
    3.15  
    3.16  typedef uint8_t libxl_mac[6];
    3.17  
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/tools/libxl/libxl_uuid.h	Thu Sep 02 18:12:14 2010 +0100
     4.3 @@ -0,0 +1,134 @@
     4.4 +/*
     4.5 + * Copyright (C) 2008,2010 Citrix Ltd.
     4.6 + *
     4.7 + * This program is free software; you can redistribute it and/or modify
     4.8 + * it under the terms of the GNU Lesser General Public License as published
     4.9 + * by the Free Software Foundation; version 2.1 only. with the special
    4.10 + * exception on linking described in file LICENSE.
    4.11 + *
    4.12 + * This program is distributed in the hope that it will be useful,
    4.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    4.15 + * GNU Lesser General Public License for more details.
    4.16 + */
    4.17 +
    4.18 +#ifndef __LIBXL_UUID_H__
    4.19 +#define __LIBXL_UUID_H__
    4.20 +
    4.21 +#define LIBXL_UUID_FMT "%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
    4.22 +#define LIBXL__UUID_BYTES(uuid) uuid[0], uuid[1], uuid[2], uuid[3], \
    4.23 +                                uuid[4], uuid[5], uuid[6], uuid[7], \
    4.24 +                                uuid[8], uuid[9], uuid[10], uuid[11], \
    4.25 +                                uuid[12], uuid[13], uuid[14], uuid[15]
    4.26 +
    4.27 +#if defined(__linux__)
    4.28 +
    4.29 +#include <uuid/uuid.h>
    4.30 +
    4.31 +typedef struct {
    4.32 +    uuid_t uuid;
    4.33 +} libxl_uuid;
    4.34 +
    4.35 +#define LIBXL_UUID_BYTES(arg) LIBXL__UUID_BYTES(((uint8_t *)arg.uuid))
    4.36 +
    4.37 +static inline int libxl_uuid_is_nil(libxl_uuid *uuid)
    4.38 +{
    4.39 +     return uuid_is_null(uuid->uuid);
    4.40 +}
    4.41 +
    4.42 +static inline void libxl_uuid_generate(libxl_uuid *uuid)
    4.43 +{
    4.44 +     uuid_generate(uuid->uuid);
    4.45 +}
    4.46 +
    4.47 +static inline int libxl_uuid_from_string(libxl_uuid *uuid, const char *in)
    4.48 +{
    4.49 +     return uuid_parse(in, uuid->uuid);
    4.50 +}
    4.51 +
    4.52 +static inline void libxl_uuid_copy(libxl_uuid *dst, libxl_uuid *src)
    4.53 +{
    4.54 +     uuid_copy(dst->uuid, src->uuid);
    4.55 +}
    4.56 +
    4.57 +static inline void libxl_uuid_clear(libxl_uuid *uuid)
    4.58 +{
    4.59 +     uuid_clear(uuid->uuid);
    4.60 +}
    4.61 +
    4.62 +static inline int libxl_uuid_compare(libxl_uuid *uuid1, libxl_uuid *uuid2)
    4.63 +{
    4.64 +     return uuid_compare(uuid1->uuid, uuid2->uuid);
    4.65 +}
    4.66 +
    4.67 +static inline uint8_t *libxl_uuid_bytearray(libxl_uuid *uuid)
    4.68 +{
    4.69 +    return uuid->uuid;
    4.70 +}
    4.71 +
    4.72 +#elif defined(__NetBSD__)
    4.73 +
    4.74 +#include <uuid.h>
    4.75 +#include <string.h>
    4.76 +#include <stdlib.h>
    4.77 +#include <stdio.h>
    4.78 +#include <assert.h>
    4.79 +
    4.80 +#define LIBXL_UUID_BYTES(arg) LIBXL__UUID_BYTES(arg.uuid)
    4.81 +
    4.82 +typedef struct {
    4.83 +    uint8_t uuid[16];
    4.84 +} libxl_uuid;
    4.85 +
    4.86 +static inline int libxl_uuid_is_nil(libxl_uuid *uuid)
    4.87 +{
    4.88 +    uint32_t status;
    4.89 +    return uuid_is_nil((uuid_t *)uuid->uuid, &status);
    4.90 +}
    4.91 +
    4.92 +static inline void libxl_uuid_generate(libxl_uuid *uuid)
    4.93 +{
    4.94 +    uint32_t status;
    4.95 +    uuid_create((uuid_t *)uuid->uuid, &status);
    4.96 +    assert(status == uuid_s_ok);
    4.97 +}
    4.98 +
    4.99 +#define LIBXL__UUID_PTRS(uuid) &uuid[0], &uuid[1], &uuid[2], &uuid[3], \
   4.100 +                               &uuid[4], &uuid[5], &uuid[6], &uuid[7], \
   4.101 +                               &uuid[8], &uuid[9], &uuid[10],&uuid[11], \
   4.102 +                               &uuid[12],&uuid[13],&uuid[14],&uuid[15]
   4.103 +static inline int libxl_uuid_from_string(libxl_uuid *uuid, const char *in)
   4.104 +{
   4.105 +    if ( sscanf(in, LIBXL_UUID_FMT, LIBXL__UUID_PTRS(uuid->uuid)) != sizeof(uuid->uuid) )
   4.106 +        return -1;
   4.107 +    return 0;
   4.108 +}
   4.109 +#undef LIBXL__UUID_PTRS
   4.110 +
   4.111 +static inline void libxl_uuid_copy(libxl_uuid *dst, libxl_uuid *src)
   4.112 +{
   4.113 +     memcpy(dst->uuid, src->uuid, sizeof(dst->uuid));
   4.114 +}
   4.115 +
   4.116 +static inline void libxl_uuid_clear(libxl_uuid *uuid)
   4.117 +{
   4.118 +     memset(uuid->uuid, 0, sizeof(uuid->uuid));
   4.119 +}
   4.120 +
   4.121 +static inline int libxl_uuid_compare(libxl_uuid *uuid1, libxl_uuid *uuid2)
   4.122 +{
   4.123 +     return memcmp(uuid1->uuid, uuid2->uuid, sizeof(uuid1->uuid));
   4.124 +}
   4.125 +
   4.126 +static inline uint8_t *libxl_uuid_bytearray(libxl_uuid *uuid)
   4.127 +{
   4.128 +    return uuid->uuid;
   4.129 +}
   4.130 +
   4.131 +#else
   4.132 +
   4.133 +#error "Please update libxl_uuid.h for your OS"
   4.134 +
   4.135 +#endif
   4.136 +
   4.137 +#endif /* __LIBXL_UUID_H__ */
     5.1 --- a/tools/libxl/xl.c	Thu Sep 02 17:44:46 2010 +0100
     5.2 +++ b/tools/libxl/xl.c	Thu Sep 02 18:12:14 2010 +0100
     5.3 @@ -74,8 +74,6 @@ int main(int argc, char **argv)
     5.4      argc -= optind;
     5.5      optind = 1;
     5.6  
     5.7 -    srand(time(0));
     5.8 -
     5.9      cspec = cmdtable_lookup(cmd);
    5.10      if (cspec)
    5.11          ret = cspec->cmd_impl(argc, argv);
     6.1 --- a/tools/libxl/xl_cmdimpl.c	Thu Sep 02 17:44:46 2010 +0100
     6.2 +++ b/tools/libxl/xl_cmdimpl.c	Thu Sep 02 18:12:14 2010 +0100
     6.3 @@ -286,19 +286,12 @@ static void init_build_info(libxl_domain
     6.4      }
     6.5  }
     6.6  
     6.7 -static void random_uuid(libxl_uuid *uuid)
     6.8 -{
     6.9 -    int i;
    6.10 -    for (i = 0; i < 16; i++)
    6.11 -        (*uuid)[i] = rand();
    6.12 -}
    6.13 -
    6.14  static void init_dm_info(libxl_device_model_info *dm_info,
    6.15          libxl_domain_create_info *c_info, libxl_domain_build_info *b_info)
    6.16  {
    6.17      memset(dm_info, '\0', sizeof(*dm_info));
    6.18  
    6.19 -    random_uuid(&dm_info->uuid);
    6.20 +    libxl_uuid_generate(&dm_info->uuid);
    6.21  
    6.22      dm_info->dom_name = c_info->name;
    6.23      dm_info->device_model = "qemu-dm";
    6.24 @@ -325,6 +318,11 @@ static void init_dm_info(libxl_device_mo
    6.25  
    6.26  static void init_nic_info(libxl_device_nic *nic_info, int devnum)
    6.27  {
    6.28 +    const uint8_t *r;
    6.29 +    libxl_uuid uuid;
    6.30 +
    6.31 +    libxl_uuid_generate(&uuid);
    6.32 +    r = libxl_uuid_bytearray(&uuid);
    6.33      memset(nic_info, '\0', sizeof(*nic_info));
    6.34  
    6.35      nic_info->backend_domid = 0;
    6.36 @@ -335,9 +333,9 @@ static void init_nic_info(libxl_device_n
    6.37      nic_info->mac[0] = 0x00;
    6.38      nic_info->mac[1] = 0x16;
    6.39      nic_info->mac[2] = 0x3e;
    6.40 -    nic_info->mac[3] = 1 + (int) (0x7f * (rand() / (RAND_MAX + 1.0)));
    6.41 -    nic_info->mac[4] = 1 + (int) (0xff * (rand() / (RAND_MAX + 1.0)));
    6.42 -    nic_info->mac[5] = 1 + (int) (0xff * (rand() / (RAND_MAX + 1.0)));
    6.43 +    nic_info->mac[3] = r[0] & 0x7f;
    6.44 +    nic_info->mac[4] = r[1];
    6.45 +    nic_info->mac[5] = r[2];
    6.46      nic_info->ifname = NULL;
    6.47      nic_info->bridge = strdup("xenbr0");
    6.48      CHK_ERRNO( asprintf(&nic_info->script, "%s/vif-bridge",
    6.49 @@ -347,21 +345,26 @@ static void init_nic_info(libxl_device_n
    6.50  
    6.51  static void init_net2_info(libxl_device_net2 *net2_info, int devnum)
    6.52  {
    6.53 +    const uint8_t *r;
    6.54 +    libxl_uuid uuid;
    6.55 +
    6.56 +    libxl_uuid_generate(&uuid);
    6.57 +    r = libxl_uuid_bytearray(&uuid);
    6.58      memset(net2_info, '\0', sizeof(*net2_info));
    6.59  
    6.60      net2_info->devid = devnum;
    6.61      net2_info->front_mac[0] = 0x00;
    6.62      net2_info->front_mac[1] = 0x16;
    6.63      net2_info->front_mac[2] = 0x3e;;
    6.64 -    net2_info->front_mac[3] = 1 + (int) (0x7f * (rand() / (RAND_MAX + 1.0)));
    6.65 -    net2_info->front_mac[4] = 1 + (int) (0xff * (rand() / (RAND_MAX + 1.0)));
    6.66 -    net2_info->front_mac[5] = 1 + (int) (0xff * (rand() / (RAND_MAX + 1.0)));
    6.67 +    net2_info->front_mac[3] = 0x7f & r[0];
    6.68 +    net2_info->front_mac[4] = r[1];
    6.69 +    net2_info->front_mac[5] = r[2];
    6.70      net2_info->back_mac[0] = 0x00;
    6.71      net2_info->back_mac[1] = 0x16;
    6.72      net2_info->back_mac[2] = 0x3e;
    6.73 -    net2_info->back_mac[3] = 1 + (int) (0x7f * (rand() / (RAND_MAX + 1.0)));
    6.74 -    net2_info->back_mac[4] = 1 + (int) (0xff * (rand() / (RAND_MAX + 1.0)));
    6.75 -    net2_info->back_mac[5] = 1 + (int) (0xff * (rand() / (RAND_MAX + 1.0)));
    6.76 +    net2_info->back_mac[3] = 0x7f & r[3];
    6.77 +    net2_info->back_mac[4] = r[4];
    6.78 +    net2_info->back_mac[5] = r[5];
    6.79      net2_info->back_trusted = 1;
    6.80      net2_info->filter_mac = 1;
    6.81      net2_info->max_bypasses = 5;
    6.82 @@ -604,8 +607,16 @@ static void parse_config_data(const char
    6.83          c_info->name = strdup(buf);
    6.84      else
    6.85          c_info->name = "test";
    6.86 -    random_uuid(&c_info->uuid);
    6.87 -
    6.88 +
    6.89 +    if (!xlu_cfg_get_string (config, "uuid", &buf) ) {
    6.90 +        if ( libxl_uuid_from_string(&c_info->uuid, buf) ) {
    6.91 +            fprintf(stderr, "Failed to parse UUID: %s\n", buf);
    6.92 +            exit(1);
    6.93 +        }
    6.94 +    }else{
    6.95 +        libxl_uuid_generate(&c_info->uuid);
    6.96 +    }
    6.97 + 
    6.98      if (!xlu_cfg_get_long(config, "oos", &l))
    6.99          c_info->oos = l;
   6.100  
   6.101 @@ -1209,7 +1220,7 @@ static int preserve_domain(libxl_ctx *ct
   6.102          return 0;
   6.103      }
   6.104  
   6.105 -    random_uuid(&new_uuid);
   6.106 +    libxl_uuid_generate(&new_uuid);
   6.107  
   6.108      LOG("Preserving domain %d %s with suffix%s", domid, d_config->c_info.name, stime);
   6.109      rc = libxl_domain_preserve(ctx, domid, &d_config->c_info, stime, new_uuid);
     7.1 --- a/tools/ocaml/libs/xl/xl_stubs.c	Thu Sep 02 17:44:46 2010 +0100
     7.2 +++ b/tools/ocaml/libs/xl/xl_stubs.c	Thu Sep 02 18:12:14 2010 +0100
     7.3 @@ -131,6 +131,7 @@ static int domain_create_info_val (caml_
     7.4  {
     7.5  	CAMLparam1(v);
     7.6  	CAMLlocal1(a);
     7.7 +	uint8_t *uuid = libxl_uuid_bytearray(&c_val->uuid);
     7.8  	int i;
     7.9  
    7.10  	c_val->hvm = Bool_val(Field(v, 0));
    7.11 @@ -140,7 +141,7 @@ static int domain_create_info_val (caml_
    7.12  	c_val->name = dup_String_val(gc, Field(v, 4));
    7.13  	a = Field(v, 5);
    7.14  	for (i = 0; i < 16; i++)
    7.15 -		c_val->uuid[i] = Int_val(Field(a, i));
    7.16 +		uuid[i] = Int_val(Field(a, i));
    7.17  	string_string_tuple_array_val(gc, &(c_val->xsdata), Field(v, 6));
    7.18  	string_string_tuple_array_val(gc, &(c_val->platformdata), Field(v, 7));
    7.19