Skip to contentSkip to conversation

libxl: made vm mac address assignment deterministic

PATCH In review Joshua Perrett 2 participants 2 messages Aug 29, 2018 – Aug 30, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libxl: made vm mac address assignment deterministic

Uses MD5 on the host mac address, vm name and vif index to generate the last three bytes of the vm mac address (for each vm).
It uses the vif index to account for multiple vifs.
MD5 code is originally from the public domain (written by Colin Plumb in 1993), files found in xen/tools/blktap2/drivers/.

---
 tools/libxl/Makefile    |   2 +-
 tools/libxl/libxl_nic.c |  59 +++++++++--
 tools/libxl/md5.c       | 266 ++++++++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/md5.h       |  26 +++++
 4 files changed, 346 insertions(+), 7 deletions(-)
 create mode 100644 tools/libxl/md5.c
 create mode 100644 tools/libxl/md5.h
tools/libxl/Makefile +1 -1
index 6da342ed61..6e7db11367 100644
@@ -142,7 +142,7 @@ LIBXL_OBJS = flexarray.o libxl.o libxl_create.o libxl_dm.o libxl_pci.o \
 			libxl_9pfs.o libxl_domain.o libxl_vdispl.o \
 			libxl_pvcalls.o libxl_vsnd.o libxl_vkb.o $(LIBXL_OBJS-y)
 LIBXL_OBJS += libxl_genid.o
-LIBXL_OBJS += _libxl_types.o libxl_flask.o _libxl_types_internal.o
+LIBXL_OBJS += _libxl_types.o libxl_flask.o _libxl_types_internal.o md5.o
 
 LIBXL_TESTS += timedereg
 LIBXL_TESTS_PROGS = $(LIBXL_TESTS) fdderegrace
tools/libxl/libxl_nic.c +53 -6

85 lines not on this page · raw message

tools/libxl/md5.c new file +266 -0

266 lines not on this page · raw message

tools/libxl/md5.h new file +26 -0
new file mode 100644
index 0000000000..09c27e2968
@@ -0,0 +1,26 @@
+#ifndef MD5_H
+#define MD5_H
+
+#include <stdint.h>
+#include <stddef.h>
+
+/**
+ * md5_sum - MD5 hash for a data block
+ * @addr: Pointers to the data area
+ * @len: Lengths of the data block
+ * @mac: Buffer for the hash
+ */
+void md5_sum(const uint8_t *addr, const size_t len, uint8_t *mac);
+
+
+struct MD5Context {
+        uint32_t buf[4];
+        uint32_t bits[2];
+        uint8_t in[64];
+};
+typedef struct MD5Context MD5_CTX;
+void MD5Init(struct MD5Context *context);
+void MD5Update(struct MD5Context *context, unsigned char const *buf, unsigned len);
+void MD5Final(unsigned char digest[16], struct MD5Context *context);
+
+#endif
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw

Also CC George

We had a race -- I commented on your patch on github. I will redo my review here.

Quoted 5 lines · Joshua Perrett

Uses MD5 on the host mac address, vm name and vif index to generate the last three bytes of the vm mac address (for each vm).
It uses the vif index to account for multiple vifs.
MD5 code is originally from the public domain (written by Colin Plumb in 1993), files found in xen/tools/blktap2/drivers/.

It would be better to reformat the commit message a bit.

Uses MD5 on the host mac address, vm name and vif index to generate the last three bytes of the vm mac address (for each vm).

It uses the vif index to account for multiple vifs.

MD5 code is originally from the public domain (written by Colin Plumb in 1993), files found in xen/tools/blktap2/drivers/.

Add a blank line between paragraphs.

Quoted 3 lines

Do you have a Citrix email address?

Quoted 30 lines
---
 tools/libxl/Makefile    |   2 +-
 tools/libxl/libxl_nic.c |  59 +++++++++--
 tools/libxl/md5.c       | 266 ++++++++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/md5.h       |  26 +++++
 4 files changed, 346 insertions(+), 7 deletions(-)
 create mode 100644 tools/libxl/md5.c
 create mode 100644 tools/libxl/md5.h
tools/libxl/Makefile +1 -1
index 6da342ed61..6e7db11367 100644
@@ -142,7 +142,7 @@ LIBXL_OBJS = flexarray.o libxl.o libxl_create.o libxl_dm.o libxl_pci.o \
 			libxl_9pfs.o libxl_domain.o libxl_vdispl.o \
 			libxl_pvcalls.o libxl_vsnd.o libxl_vkb.o $(LIBXL_OBJS-y)
 LIBXL_OBJS += libxl_genid.o
-LIBXL_OBJS += _libxl_types.o libxl_flask.o _libxl_types_internal.o
+LIBXL_OBJS += _libxl_types.o libxl_flask.o _libxl_types_internal.o md5.o
 
 LIBXL_TESTS += timedereg
 LIBXL_TESTS_PROGS = $(LIBXL_TESTS) fdderegrace
tools/libxl/libxl_nic.c +0 -0
index 01b711b84e..37de5ffb04 100644
@@ -17,6 +17,16 @@
 
 #include "libxl_internal.h"
 

tools/libxl/libxl_nic.c line 29

Quoted 10 lines
tools/libxl/libxl_nic.c +10 -0
+#include <string.h>
+
+#include "md5.h"
+
+#include <sys/types.h>
+#include <ifaddrs.h>
+#include <sys/socket.h>
+#include <linux/if_packet.h>
+#include <net/ethernet.h>
+

This will break BSDs. They will probably need to be enclosed in ifdef __linux__.

It isn't always clear at the beginning that Xen can use other Unix-like systems as Dom0.

tools/libxl/libxl_nic.c int libxl_mac_to_device_nic(libxl_ctx *ctx, uint32_t domid, · line 68

Quoted 10 lines
tools/libxl/libxl_nic.c +3 -0
 int libxl_mac_to_device_nic(libxl_ctx *ctx, uint32_t domid,
                             const char *mac, libxl_device_nic *nic)
 {
@@ -53,8 +63,36 @@ int libxl_mac_to_device_nic(libxl_ctx *ctx, uint32_t domid,
     return rc;
 }
 
+static int libxl__get_host_mac(unsigned char *buf)
+{
+    int success = -1;

Change this to rc and use it to store libxl error code. Please read CODING_STYLE in libxl.

tools/libxl/libxl_nic.c int libxl_mac_to_device_nic(libxl_ctx *ctx, uint32_t domid, · line 69

Quoted 1 line
tools/libxl/libxl_nic.c +1 -0
+    struct ifaddrs *iface_list;

Blank line here.

tools/libxl/libxl_nic.c int libxl_mac_to_device_nic(libxl_ctx *ctx, uint32_t domid, · line 70

Quoted 1 line
tools/libxl/libxl_nic.c +1 -0
+    if (getifaddrs(&iface_list) == 0) {

Is the list stable?

tools/libxl/libxl_nic.c int libxl_mac_to_device_nic(libxl_ctx *ctx, uint32_t domid, · line 74

Quoted 4 lines
tools/libxl/libxl_nic.c +4 -0
+        for (struct ifaddrs *iface = iface_list;
+            iface != NULL; iface = iface->ifa_next) {
+            if (iface->ifa_addr && iface->ifa_addr->sa_family == AF_PACKET) {
+                struct sockaddr_ll *s = (struct sockaddr_ll *)iface->ifa_addr;

So the first mac is used?

tools/libxl/libxl_nic.c int libxl_mac_to_device_nic(libxl_ctx *ctx, uint32_t domid, · line 78

Quoted 4 lines
tools/libxl/libxl_nic.c +4 -0
+                if (s->sll_halen == 6) {
+                    for (int i = 0; i < 6; i++) {
+                        buf[i] = s->sll_addr[i];
+                    }

This can probably be replaced by a call to memcpy.

tools/libxl/libxl_nic.c int libxl_mac_to_device_nic(libxl_ctx *ctx, uint32_t domid, · line 88

Quoted 10 lines
tools/libxl/libxl_nic.c +10 -0
+                    if(buf[0] || buf[1] || buf[2] || buf[3] || buf[4] || buf[5]) {
+                        success = 0;
+                        break;
+                    }
+                }
+            }
+        }
+        freeifaddrs(iface_list);
+    } else {
+        perror("ERROR: getifaddrs\n");

Use one of the LOG macros here. You will need to pass in a gc to this function.

tools/libxl/libxl_nic.c int libxl_mac_to_device_nic(libxl_ctx *ctx, uint32_t domid, · line 89

Quoted 1 line
tools/libxl/libxl_nic.c +1 -0
+    }

Blank line here.

Quoted 12 lines
tools/libxl/libxl_nic.c +5 -1
+    return success;
+}
+
 static int libxl__device_nic_setdefault(libxl__gc *gc, uint32_t domid,
-                                        libxl_device_nic *nic, bool hotplug)
+                                        libxl_device_nic *nic, const char *name,
+                                        const int nic_index, bool hotplug)
 {
     int rc;
 
@@ -65,11 +103,20 @@ static int libxl__device_nic_setdefault(libxl__gc *gc, uint32_t domid,
         if (!nic->model) return ERROR_NOMEM;

tools/libxl/libxl_nic.c static int libxl__device_nic_setdefault(libxl__gc *gc, uint32_t domid, · line 111

Quoted 10 lines
tools/libxl/libxl_nic.c +6 -2
     }
     if (libxl__mac_is_default(&nic->mac)) {
-        const uint8_t *r;
-        libxl_uuid uuid;
+        uint8_t r[16];
+
+        uint8_t hostmac[6] = {0};
+
+        if(libxl__get_host_mac(hostmac)) {
+            perror("WARNING: failed to get host mac address\n");

LOG here. And you should probably bail instead of continuing?

Again, you might want to make hostmac a Linux only thing.

tools/libxl/md5.c

Quoted 27 lines
tools/libxl/libxl_nic.c +8 -3
+        }
 
-        libxl_uuid_generate(&uuid);
-        r = libxl_uuid_bytearray(&uuid);
+        MD5_CTX ctx;
+        MD5Init(&ctx);
+        MD5Update(&ctx, hostmac, sizeof(hostmac));
+        MD5Update(&ctx, (uint8_t *) name, strlen(name));
+        MD5Update(&ctx, (uint8_t *) &nic_index, sizeof(nic_index));
+        MD5Final(r, &ctx);
 
         nic->mac[0] = 0x00;
         nic->mac[1] = 0x16;
@@ -478,7 +525,7 @@ int libxl__device_nic_set_devids(libxl__gc *gc, libxl_domain_config *d_config,
          * but qemu needs the nic information to be complete.
          */
         ret = libxl__device_nic_setdefault(gc, domid, &d_config->nics[i],
-                                           false);
+                                           d_config->c_info.name, i, false);
         if (ret) {
             LOGD(ERROR, domid, "Unable to set nic defaults for nic %d", i);
             goto out;
tools/libxl/md5.c new file +0 -0
new file mode 100644
index 0000000000..88ea13938a

I have skipped this file.

Wei.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel