Skip to contentSkip to conversation

libxl: deterministic mac addresses

PATCH In review Joshua Perrett 2 participants 5 messages Sep 12, 2018 – Sep 20, 2018

lore
Share & tools
lore
#0 lore raw

v4 cover libxl: deterministic mac addresses

The patch has now been split into two parts. One adds the MD5 files to a shared directory in tools, so that it can potentially be reused. The second patch makes mac addresses deterministic. It still uses a host mac address, seeing as gethostid() won't work (as George explained). The code should now work the same on both little endian and big endian machines.

Joshua Perrett (2):
Created tools/shared directory containing MD5 files
libxl: made vm mac address assignment deterministic

 tools/Makefile               |   1 +
 tools/Rules.mk               |   1 +
 tools/libxl/Makefile         |   2 +-
 tools/libxl/libxl_freebsd.c  |   5 +
 tools/libxl/libxl_internal.h |   5 +
 tools/libxl/libxl_linux.c    |  31 ++++-
 tools/libxl/libxl_netbsd.c   |   5 +
 tools/libxl/libxl_nic.c      |  31 ++++-
 tools/shared/Makefile        |  21 ++++
 tools/shared/md5.c           | 266 +++++++++++++++++++++++++++++++++++++++++++
 tools/shared/md5.h           |  26 +++++
 11 files changed, 386 insertions(+), 8 deletions(-)
 create mode 100644 tools/shared/Makefile
 create mode 100644 tools/shared/md5.c
 create mode 100644 tools/shared/md5.h
Signature · 7 lines
2.11.0


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

PATCH v4 1/2 Created tools/shared directory containing MD5 files

MD5 code is originally from the public domain (written by Colin Plumb in 1993), files taken from xen/tools/blktap2/drivers/. They have been modified slightly (useful functions made public).

---
 tools/Makefile        |   1 +
 tools/Rules.mk        |   1 +
 tools/shared/Makefile |  21 ++++
 tools/shared/md5.c    | 266 ++++++++++++++++++++++++++++++++++++++++++++++++++
 tools/shared/md5.h    |  26 +++++
 5 files changed, 315 insertions(+)
 create mode 100644 tools/shared/Makefile
 create mode 100644 tools/shared/md5.c
 create mode 100644 tools/shared/md5.h
tools/Makefile +1 -0
index 67977ad850..34ec41790d 100644
@@ -24,6 +24,7 @@ SUBDIRS-$(CONFIG_BLKTAP2) += blktap2
 SUBDIRS-$(CONFIG_NetBSD) += xenbackendd
 SUBDIRS-y += libfsimage
 SUBDIRS-$(CONFIG_Linux) += libvchan
+SUBDIRS-y += shared
 
 # do not recurse in to a dir we are about to delete
 ifneq "$(MAKECMDGOALS)" "distclean"
tools/Rules.mk +1 -0
index 296b722372..40040c7947 100644
@@ -25,6 +25,7 @@ XEN_XENSTORE       = $(XEN_ROOT)/tools/xenstore
 XEN_LIBXENSTAT     = $(XEN_ROOT)/tools/xenstat/libxenstat/src
 XEN_BLKTAP2        = $(XEN_ROOT)/tools/blktap2
 XEN_LIBVCHAN       = $(XEN_ROOT)/tools/libvchan
+XEN_SHARED         = $(XEN_ROOT)/tools/shared
 
 CFLAGS_xeninclude = -I$(XEN_INCLUDE)
 
tools/shared/Makefile new file +21 -0
new file mode 100644
index 0000000000..8aa26863b9
@@ -0,0 +1,21 @@
+XEN_ROOT=$(CURDIR)/../..
+include $(XEN_ROOT)/tools/Rules.mk
+
+TARGETS=md5.o
+
+.PHONY: all
+all: build
+
+.PHONY: build
+build: $(TARGETS)
+
+.PHONY: clean
+clean:
+	$(RM) *.o $(TARGETS) *~ $(DEPS_RM)
+
+.PHONY: distclean
+distclean: clean
+
+install uninstall:
+
+-include $(DEPS_INCLUDE)
tools/shared/md5.c new file +266 -0

266 lines not on this page · raw message

tools/shared/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
#2 lore raw

Re: [PATCH v4 1/2] Created tools/shared directory containing MD5 files

Quoted 21 lines · Joshua Perrett

MD5 code is originally from the public domain (written by Colin Plumb in 1993), files taken from xen/tools/blktap2/drivers/. They have been modified slightly (useful functions made public).

---
 tools/Makefile        |   1 +
 tools/Rules.mk        |   1 +
 tools/shared/Makefile |  21 ++++
 tools/shared/md5.c    | 266 ++++++++++++++++++++++++++++++++++++++++++++++++++
 tools/shared/md5.h    |  26 +++++
 5 files changed, 315 insertions(+)
 create mode 100644 tools/shared/Makefile
 create mode 100644 tools/shared/md5.c
 create mode 100644 tools/shared/md5.h
tools/Makefile +0 -0
index 67977ad850..34ec41790d 100644

tools/Makefile SUBDIRS-$(CONFIG_BLKTAP2) += blktap2 · line 27

Quoted 4 lines
tools/Makefile +1 -0
@@ -24,6 +24,7 @@ SUBDIRS-$(CONFIG_BLKTAP2) += blktap2
 SUBDIRS-$(CONFIG_NetBSD) += xenbackendd
 SUBDIRS-y += libfsimage
 SUBDIRS-$(CONFIG_Linux) += libvchan
+SUBDIRS-y += shared

I'm afraid building this in the shared directory is not quite right.

You will need to use vpath in libxl/Makefile to add the shared/ directory to libxl.

See https://www.gnu.org/software/make/manual/html_node/General-Search.html
and libxc/Makefile for example.

Wei.

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

#3 lore raw

PATCH v4 2/2 libxl: made vm mac address assignment deterministic

In reply to Joshua Perrett

Uses MD5 on a host mac address, the 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.

---
 tools/libxl/Makefile         |  2 +-
 tools/libxl/libxl_freebsd.c  |  5 +++++
 tools/libxl/libxl_internal.h |  5 +++++
 tools/libxl/libxl_linux.c    | 31 ++++++++++++++++++++++++++++++-
 tools/libxl/libxl_netbsd.c   |  5 +++++
 tools/libxl/libxl_nic.c      | 31 +++++++++++++++++++++++++------
 6 files changed, 71 insertions(+), 8 deletions(-)
tools/libxl/Makefile +1 -1
index 6da342ed61..21a67bc2fc 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 $(XEN_SHARED)/md5.o
 
 LIBXL_TESTS += timedereg
 LIBXL_TESTS_PROGS = $(LIBXL_TESTS) fdderegrace
tools/libxl/libxl_freebsd.c +5 -0
index 6442ccec72..23d8155c75 100644
@@ -245,3 +245,8 @@ int libxl__pci_topology_init(libxl__gc *gc,
 {
     return ERROR_NI;
 }
+
+int libxl__get_host_mac(libxl__gc *gc, unsigned char *buf)
+{
+    return ERROR_NI;
+}
tools/libxl/libxl_internal.h +5 -0
index 802382c704..4a5154600d 100644
@@ -50,6 +50,9 @@
 #include <sys/un.h>
 #include <sys/file.h>
 #include <sys/ioctl.h>
+#include <ifaddrs.h>
+#include <linux/if_packet.h>
+#include <net/ethernet.h>
 
 #include <xenevtchn.h>
 #include <xenstore.h>
@@ -1413,6 +1416,8 @@ _hidden int libxl__pci_topology_init(libxl__gc *gc,
                                      physdev_pci_device_t *devs,
                                      int num_devs);
 
+_hidden int libxl__get_host_mac(libxl__gc *gc, unsigned char *buf);
+
 /* from libxl_pci */
 
 _hidden int libxl__device_pci_add(libxl__gc *gc, uint32_t domid, libxl_device_pci *pcidev, int starting);
tools/libxl/libxl_linux.c +30 -1
index 6ef0abc693..e528f7c085 100644
@@ -16,7 +16,7 @@
 #include "libxl_osdeps.h" /* must come before any other headers */
 
 #include "libxl_internal.h"
- 
+
 int libxl__try_phy_backend(mode_t st_mode)
 {
     if (S_ISBLK(st_mode) || S_ISREG(st_mode)) {
@@ -307,6 +307,35 @@ int libxl__pci_topology_init(libxl__gc *gc,
     return err;
 }
 
+int libxl__get_host_mac(libxl__gc *gc, unsigned char *buf)
+{
+    int rc = ERROR_FAIL;
+
+    struct ifaddrs *iface_list;
+    uint8_t largest[6] = {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;
+                if (s->sll_halen == 6) {
+                    if (memcmp(s->sll_addr, largest, s->sll_halen) > 0) {
+                        memcpy(buf, s->sll_addr, s->sll_halen);
+                        memcpy(largest, s->sll_addr, s->sll_halen);
+                        rc = 0;
+                    }
+                }
+            }
+        }
+        freeifaddrs(iface_list);
+    } else {
+        LOGE(WARN, "getifaddrs failed");
+    }
+
+    return rc;
+}
+
 /*
  * Local variables:
  * mode: C
tools/libxl/libxl_netbsd.c +5 -0
index 2edfb00641..7b001ec2b7 100644
@@ -124,3 +124,8 @@ int libxl__pci_topology_init(libxl__gc *gc,
 {
     return ERROR_NI;
 }
+
+int libxl__get_host_mac(libxl__gc *gc, unsigned char *buf)
+{
+    return ERROR_NI;
+}
tools/libxl/libxl_nic.c +25 -6
index 01b711b84e..1010627f5d 100644
@@ -17,6 +17,12 @@
 
 #include "libxl_internal.h"
 
+#include <string.h>
+
+#include <arpa/inet.h>
+
+#include "../shared/md5.h"
+
 int libxl_mac_to_device_nic(libxl_ctx *ctx, uint32_t domid,
                             const char *mac, libxl_device_nic *nic)
 {
@@ -54,7 +60,8 @@ int libxl_mac_to_device_nic(libxl_ctx *ctx, uint32_t domid,
 }
 
 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 +72,23 @@ 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];
+
+        MD5_CTX ctx;
+        MD5Init(&ctx);
+
+        uint8_t hostmac[6];
+
+        if (libxl__get_host_mac(gc, hostmac) == 0) {
+            MD5Update(&ctx, hostmac, sizeof(hostmac));
+        } else {
+            LOGD(INFO, domid, "failed to get host mac address, will generate vm mac address without");
+        }
 
-        libxl_uuid_generate(&uuid);
-        r = libxl_uuid_bytearray(&uuid);
+        MD5Update(&ctx, (uint8_t *) name, strlen(name));
+        uint32_t index = htonl(nic_index);
+        MD5Update(&ctx, (uint8_t *) &index, sizeof(nic_index));
+        MD5Final(r, &ctx);
 
         nic->mac[0] = 0x00;
         nic->mac[1] = 0x16;
@@ -478,7 +497,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;
Signature · 7 lines
2.11.0


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

Re: [PATCH v4 2/2] libxl: made vm mac address assignment deterministic

On Wed, Sep 12, 2018 at 05:40:37PM +0000, Joshua Perrett wrote:
[...]

Quoted 7 lines
tools/libxl/libxl_linux.c +7 -0
+int libxl__get_host_mac(libxl__gc *gc, unsigned char *buf)
+{
+    int rc = ERROR_FAIL;
+
+    struct ifaddrs *iface_list;
+    uint8_t largest[6] = {0};
+

tools/libxl/libxl_linux.c int libxl__pci_topology_init(libxl__gc *gc, · line 326

Quoted 10 lines
tools/libxl/libxl_linux.c +10 -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;
+                if (s->sll_halen == 6) {
+                    if (memcmp(s->sll_addr, largest, s->sll_halen) > 0) {
+                        memcpy(buf, s->sll_addr, s->sll_halen);
+                        memcpy(largest, s->sll_addr, s->sll_halen);
+                        rc = 0;

Sorry I didn't participate in the conversation IRL.

What is the resolution here?

Wei.

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