Skip to contentSkip to conversation

libxl: made vm mac address assignment deterministic

PATCH In review Joshua Perrett 2 participants 2 messages Aug 30, 2018 – Aug 31, 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 |  65 ++++++++++--
 tools/libxl/md5.c       | 266 ++++++++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/md5.h       |  26 +++++
 4 files changed, 352 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 +59 -6

91 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
Quoted 56 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/.

---
 tools/libxl/Makefile    |   2 +-
 tools/libxl/libxl_nic.c |  65 ++++++++++--
 tools/libxl/md5.c       | 266 ++++++++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/md5.h       |  26 +++++
 4 files changed, 352 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 +12 -0
index 01b711b84e..9a1c0fb16f 100644
@@ -17,6 +17,18 @@
 
 #include "libxl_internal.h"
 
+#include <string.h>
+
+#include "md5.h"
+
+#ifdef __linux__
+#include <sys/types.h>
+#include <ifaddrs.h>
+#include <sys/socket.h>
+#include <linux/if_packet.h>
+#include <net/ethernet.h>
+#endif
+
 int libxl_mac_to_device_nic(libxl_ctx *ctx, uint32_t domid,
                             const char *mac, libxl_device_nic *nic)
 {

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

Quoted 6 lines
tools/libxl/libxl_nic.c +3 -0
@@ -53,8 +65,38 @@ int libxl_mac_to_device_nic(libxl_ctx *ctx, uint32_t domid,
     return rc;
 }
 
+static int libxl__get_host_mac(libxl__gc *gc, unsigned char *buf)
+{
+    int rc = -1;

rc should be initialised to ERROR_FAIL;

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
+    #ifdef __linux__
+    struct ifaddrs *iface_list;
+
+    if (getifaddrs(&iface_list) == 0) {

You haven't answered my question regarding stability of this list.

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

Quoted 7 lines
tools/libxl/libxl_nic.c +7 -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;
+                if (s->sll_halen == 6) {
+                    memcpy(buf, s->sll_addr, 6);
+                    if(buf[0] || buf[1] || buf[2] || buf[3] || buf[4] || buf[5]) {

Missing space after if.

Also please wrap the line around 80 characters.

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

Quoted 9 lines
tools/libxl/libxl_nic.c +9 -0
+                        rc = 0;
+                        break;
+                    }
+                }
+            }
+        }
+        freeifaddrs(iface_list);
+    } else {
+        LOG(ERROR, "getifaddrs\n");

If this is not a fatal error you should use WARN here.

Quoted 20 lines
tools/libxl/libxl_nic.c +9 -3
+    }
+    #endif
+
+    return rc;
+}
+
 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 +107,22 @@ static int libxl__device_nic_setdefault(libxl__gc *gc, uint32_t domid,
         if (!nic->model) return ERROR_NOMEM;
     }
     if (libxl__mac_is_default(&nic->mac)) {
-        const uint8_t *r;
-        libxl_uuid uuid;
+        uint8_t r[16];

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

Quoted 10 lines
tools/libxl/libxl_nic.c +10 -0
+
+        MD5_CTX ctx;
+        MD5Init(&ctx);
+
+        uint8_t hostmac[6] = {0};
+
+        if(libxl__get_host_mac(gc, hostmac) == 0) {
+            MD5Update(&ctx, hostmac, sizeof(hostmac));
+        } else {
+            LOGD(WARN, domid, "failed to get host mac address, will generate vm mac address without\n");

Turn WARN into INFO.

Wei.

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