[PATCH] Make the 'default' NetRASD allocation include a randomly-generated MAC

# HG changeset patch # User Dan Smith <danms@us.ibm.com> # Date 1208189730 25200 # Node ID abf9b4881cc061cca318fe39c3c849f01316cdc8 # Parent da267e967adbd316d30303cc064584ebd36d7123 Make the 'default' NetRASD allocation include a randomly-generated MAC No changes from last time, except I think this can be put in by itself, to reduce the size of my queue for the VSMS stuff. Signed-off-by: Dan Smith <danms@us.ibm.com> diff -r da267e967adb -r abf9b4881cc0 src/Virt_SettingsDefineCapabilities.c --- a/src/Virt_SettingsDefineCapabilities.c Mon Apr 14 09:13:15 2008 -0700 +++ b/src/Virt_SettingsDefineCapabilities.c Mon Apr 14 09:15:30 2008 -0700 @@ -25,6 +25,7 @@ #include <stdbool.h> #include <sys/vfs.h> #include <errno.h> +#include <time.h> #include <libvirt/libvirt.h> @@ -52,6 +53,8 @@ const static CMPIBroker *_BROKER; #define SDC_DISK_MIN 2000 #define SDC_DISK_DEF 5000 #define SDC_DISK_INC 250 + +#define DEFAULT_MAC_PREFIX "00:16:3e" static bool rasd_prop_copy_value(struct sdc_rasd_prop src, struct sdc_rasd_prop *dest) @@ -430,6 +433,45 @@ static struct sdc_rasd_prop *net_max(con return rasd; } +static const char *_net_rand_mac(void) +{ + int r; + int ret; + unsigned int s; + char *mac = NULL; + CMPIString *str = NULL; + CMPIStatus status; + + srand(time(NULL)); + r = rand_r(&s); + + ret = asprintf(&mac, + "%s:%02x:%02x:%02x", + DEFAULT_MAC_PREFIX, + r & 0xFF, + (r & 0xFF00) >> 8, + (r & 0xFF0000) >> 16); + + if (ret == -1) + goto out; + + str = CMNewString(_BROKER, mac, &status); + if ((str == NULL) || (status.rc != CMPI_RC_OK)) { + str = NULL; + CU_DEBUG("Failed to create string"); + goto out; + } + out: + free(mac); + + if (str != NULL) + mac = CMGetCharPtr(str); + else + mac = NULL; + + return mac; +} + static struct sdc_rasd_prop *net_def(const CMPIObjectPath *ref, CMPIStatus *s) { @@ -440,6 +482,7 @@ static struct sdc_rasd_prop *net_def(con struct sdc_rasd_prop tmp[] = { {"InstanceID", (CMPIValue *)"Default", CMPI_chars}, {"VirtualQuantity", (CMPIValue *)&num_nics, CMPI_uint16}, + {"Address", (CMPIValue *)_net_rand_mac(), CMPI_chars}, PROP_END };

Dan Smith wrote:
# HG changeset patch # User Dan Smith <danms@us.ibm.com> # Date 1208189730 25200 # Node ID abf9b4881cc061cca318fe39c3c849f01316cdc8 # Parent da267e967adbd316d30303cc064584ebd36d7123 Make the 'default' NetRASD allocation include a randomly-generated MAC
No changes from last time, except I think this can be put in by itself, to reduce the size of my queue for the VSMS stuff.
Signed-off-by: Dan Smith <danms@us.ibm.com>
static struct sdc_rasd_prop *net_def(const CMPIObjectPath *ref, CMPIStatus *s) { @@ -440,6 +482,7 @@ static struct sdc_rasd_prop *net_def(con struct sdc_rasd_prop tmp[] = { {"InstanceID", (CMPIValue *)"Default", CMPI_chars}, {"VirtualQuantity", (CMPIValue *)&num_nics, CMPI_uint16}, + {"Address", (CMPIValue *)_net_rand_mac(), CMPI_chars}, PROP_END };
Is this going to segfault if _net_rand_mac() hits an error and comes back NULL? Or is the code that actually turns the struct into an inst able to handle that gracefully? -- -Jay

JG> Is this going to segfault if _net_rand_mac() hits an error and JG> comes back NULL? Or is the code that actually turns the struct JG> into an inst able to handle that gracefully? Yep, sure is. Replacement soon :) -- Dan Smith IBM Linux Technology Center Open Hypervisor Team email: danms@us.ibm.com
participants (2)
-
Dan Smith
-
Jay Gagnon