[libvirt] [PATCH v2 4/5] FreeBSD: implement virNetDevSetMAC().

Roman Bogorodskiy bogorodskiy at gmail.com
Sun Jan 20 16:22:26 UTC 2013


---
 src/util/virnetdev.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)

diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c
index 5100467..cd2b773 100644
--- a/src/util/virnetdev.c
+++ b/src/util/virnetdev.c
@@ -42,6 +42,11 @@
 # undef HAVE_STRUCT_IFREQ
 #endif
 
+#if defined(__FreeBSD__)
+# include <sys/sockio.h>
+# include <net/if_dl.h>
+#endif
+
 #define VIR_FROM_THIS VIR_FROM_NONE
 
 #if (defined(HAVE_STRUCT_IFREQ) || defined(__FreeBSD__))
@@ -182,6 +187,54 @@ cleanup:
     VIR_FORCE_CLOSE(fd);
     return ret;
 }
+#elif defined(__FreeBSD__)
+int virNetDevSetMAC(const char *ifname,
+                    const virMacAddrPtr macaddr)
+{
+        struct ifreq ifr;
+        struct sockaddr_dl sdl;
+        uint8_t mac[VIR_MAC_STRING_BUFLEN + 1];
+        char *macstr;
+        int s;
+        int ret = -1;
+
+        if ((s = virNetDevSetupControl(ifname, &ifr)) < 0)
+            return -1;
+
+        if (VIR_ALLOC_N(macstr, VIR_MAC_STRING_BUFLEN) < 0) {
+            virReportOOMError();
+            goto cleanup;
+        }
+        virMacAddrFormat(&macaddr, macstr);
+        memset(mac, 0, sizeof(mac));
+        mac[0] = ':';
+        if (virStrncpy(mac + 1, macstr, strlen(macstr),
+                       sizeof(mac)) == NULL) {
+            virReportSystemError(ERANGE,
+                                 _("invalid MAC %s"),
+                                 macstr);
+            goto cleanup;
+        }
+        sdl.sdl_len = sizeof(sdl);
+        link_addr(mac, &sdl);
+
+        bcopy(sdl.sdl_data, ifr.ifr_addr.sa_data, 6);
+        ifr.ifr_addr.sa_len = 6;
+
+        if (ioctl(s, SIOCSIFLLADDR, &ifr) < 0) {
+            virReportSystemError(errno,
+                                 _("Cannot set interface MAC on '%s'"),
+                                ifname);
+            goto cleanup;
+        }
+
+        ret = 0;
+cleanup:
+        VIR_FREE(macstr);
+        VIR_FORCE_CLOSE(s);
+
+        return ret;
+}
 #else
 int virNetDevSetMAC(const char *ifname,
                     const virMacAddrPtr macaddr ATTRIBUTE_UNUSED)
-- 
1.8.0




More information about the libvir-list mailing list