Skip to contentSkip to conversation

libxl: made vm mac address assignment deterministic

PATCH In review Joshua Perrett 3 participants 3 messages Sep 5, 2018 – Sep 6, 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 |  68 +++++++++++--
 tools/libxl/md5.c       | 266 ++++++++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/md5.h       |  26 +++++
 4 files changed, 355 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 +62 -6

94 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 10 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/libxl_nic.c int libxl_mac_to_device_nic(libxl_ctx *ctx, uint32_t domid, · line 70

Quoted 10 lines

int libxl_mac_to_device_nic(libxl_ctx *ctx, uint32_t domid,
const char *mac, libxl_device_nic *nic)
{

tools/libxl/libxl_nic.c +3 -0
@@ -53,8 +65,41 @@ 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 = ERROR_FAIL;

Having a blank line makes things a bit clearer.

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

Quoted 9 lines
tools/libxl/libxl_nic.c +9 -0
+    #ifdef __linux__
+    struct ifaddrs *iface_list;
+    uint64_t largest = 0;
+
+    if (getifaddrs(&iface_list) == 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;

Blank line here.

Generally please add a blank line between variable declarations and statements.

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

Quoted 2 lines
tools/libxl/libxl_nic.c +2 -0
+                if (s->sll_halen == 6) {
+                    uint64_t value = 0;

Blank line here.

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

Quoted 1 line
tools/libxl/libxl_nic.c +1 -0
+                    memcpy(&value, s->sll_addr, 6);

Please use sizeof(s->sll_addr) instead of 6.

Also please add an assert before the memcpy

   assert(sizeof(value) >= sizeof(s->sll-addr));

This will help catch potential buffer overflow issue. Not that there is one in your code, it is just good habit to have.

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

Quoted 4 lines
tools/libxl/libxl_nic.c +4 -0
+                    if (value > largest) {
+                        memcpy(buf, s->sll_addr, 6);
+                        largest = value;
+                        rc = 0;

Interesting algorithm, but I don't know anything better at the moment. :)

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

Quoted 7 lines
tools/libxl/libxl_nic.c +7 -0
+                    }
+                }
+            }
+        }
+        freeifaddrs(iface_list);
+    } else {
+        LOG(WARN, "getifaddrs\n");

Use LOGE here to log errno too. And please say

getifaddrs failed

And there is no need to have '\n' at the end of the line. LOG* macros add that themselves.

Quoted 17 lines
tools/libxl/libxl_nic.c +8 -1
+    }
+    #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 +110,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)) {

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 +8 -2
-        const uint8_t *r;
-        libxl_uuid uuid;
+        uint8_t r[16];
+
+        MD5_CTX ctx;
+        MD5Init(&ctx);
+
+        uint8_t hostmac[6];
+
+        if(libxl__get_host_mac(gc, hostmac) == 0) {

Missing a space after `if' here.

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

Quoted 3 lines
tools/libxl/libxl_nic.c +3 -0
+            MD5Update(&ctx, hostmac, sizeof(hostmac));
+        } else {
+            LOGD(INFO, domid, "failed to get host mac address, will generate vm mac address without\n");

Remove '\n'.

Wei.

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

#2 lore raw

Wei Liu writes ("Re: [PATCH] libxl: made vm mac address assignment deterministic"):

Quoted 3 lines · Joshua Perrett
Quoted 2 lines

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).

There is no such thing as "the" host mac address. The host might have several. However, generally there is a specific interface that will be used for this guest, depending on the vif connection mode. In bridge mode, for example, there is the mac address of the bridge. I think you should make sure to use the right interface.

I think you need to add something to the documentation. You should mention that this approach is only deterministic *on the same host* (so in setups where the guest might be started on multiple hosts, with networked storage, it won't work) and only *with the same physical nic* (so swapping out the physical nic will change all the guests' addresses).

Quoted 2 lines
Quoted 2 lines

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

You are duplicating these files within the Xen tree. That's pretty ugly. Can we not do this a better way ?

Quoted 8 lines

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

Quoted 3 lines
tools/libxl/libxl_nic.c +3 -0
+static int libxl__get_host_mac(libxl__gc *gc, unsigned char *buf)
+{
+    int rc = ERROR_FAIL;

Having a blank line makes things a bit clearer.

Quoted 2 lines
tools/libxl/libxl_nic.c +2 -0
+    #ifdef __linux__
+    struct ifaddrs *iface_list;

Can we not have #ifdefs here please ? The Linux-specific part should go in libxl_linux.c.

Quoted 3 lines
Quoted 1 line

+ memcpy(&value, s->sll_addr, 6);

Please use sizeof(s->sll_addr) instead of 6.

Err, are you sure ? Let me quote the proposed code:

Quoted 3 lines

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

Quoted 3 lines
tools/libxl/libxl_nic.c +3 -0
+                if (s->sll_halen == 6) {
+                    uint64_t value = 0;
+                    memcpy(&value, s->sll_addr, 6);

This is very strange. How can it possibly be right to overwrite the first 6 bytes of value ? That does a different thing on big-endian and little-endian machines. (

Also why are we not checking sll_hatype ?

Quoted 7 lines

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

Quoted 4 lines
tools/libxl/libxl_nic.c +4 -0
+                    if (value > largest) {
+                        memcpy(buf, s->sll_addr, 6);
+                        largest = value;
+                        rc = 0;

Interesting algorithm, but I don't know anything better at the moment. :)

There is some reason for choosing the lexically largest address, IIRC. But the reason should be in a comment in the code. (ISTR reading something about this in the Debian bridge-interfaces(5) manpage.)

Thanks,
Ian.

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