[libvirt] [PATCH]: set and get the MAC of an interface

Gerhard Stenzel gstenzel at linux.vnet.ibm.com
Mon Jun 20 13:54:31 UTC 2011


The following patch renames the function to set the MAC of an interface
from  ifSetInterfaceMac() to brSetInterfaceMac() and makes it available
to other components.
It also adds brGetInterfaceMac() to retrieve the MAC.

Signed-off-by: Gerhard Stenzel <gerhard.stenzel at de.ibm.com>

Index: libvirt/src/util/bridge.c
===================================================================
--- libvirt.orig/src/util/bridge.c
+++ libvirt/src/util/bridge.c
@@ -286,8 +286,41 @@ brDeleteInterface(brControl *ctl ATTRIBU
 }
 # endif
 
+
+/**
+ * brGetInterfaceMac:
+ * @ctl: bridge control pointer
+ * @ifname: interface name to set MTU for
+ * @macaddr: MAC address (VIR_MAC_BUFLEN in size)
+ *
+ * This function gets the @macaddr for a given interface @ifname.
+ *
+ * Returns 0 in case of success or an errno code in case of failure.
+ */
+int brGetInterfaceMac(brControl *ctl, const char *ifname,
+                      unsigned char *macaddr)
+{
+    struct ifreq ifr;
+
+    if (!ctl || !ifname)
+        return EINVAL;
+
+    memset(&ifr, 0, sizeof(struct ifreq));
+    if (virStrcpyStatic(ifr.ifr_name, ifname) == NULL)
+        return EINVAL;
+
+    if(ioctl(ctl->fd, SIOCGIFHWADDR, (char *)&ifr) != 0){
+        return errno;
+    }
+
+    memcpy(macaddr, ifr.ifr_ifru.ifru_hwaddr.sa_data, VIR_MAC_BUFLEN);
+
+    return 0;
+}
+
+
 /**
- * ifSetInterfaceMac:
+ * brSetInterfaceMac:
  * @ctl: bridge control pointer
  * @ifname: interface name to set MTU for
  * @macaddr: MAC address (VIR_MAC_BUFLEN in size)
@@ -297,7 +330,7 @@ brDeleteInterface(brControl *ctl ATTRIBU
  *
  * Returns 0 in case of success or an errno code in case of failure.
  */
-static int ifSetInterfaceMac(brControl *ctl, const char *ifname,
+int brSetInterfaceMac(brControl *ctl, const char *ifname,
                              const unsigned char *macaddr)
 {
     struct ifreq ifr;
@@ -521,7 +554,7 @@ brAddTap(brControl *ctl,
      * seeing the kernel allocate random MAC for the TAP
      * device before we set our static MAC.
      */
-    if ((errno = ifSetInterfaceMac(ctl, ifr.ifr_name, macaddr)))
+    if ((errno = brSetInterfaceMac(ctl, ifr.ifr_name, macaddr)))
         goto error;
     /* We need to set the interface MTU before adding it
      * to the bridge, because the bridge will have its
Index: libvirt/src/util/bridge.h
===================================================================
--- libvirt.orig/src/util/bridge.h
+++ libvirt/src/util/bridge.h
@@ -106,6 +106,14 @@ int     brGetEnableSTP          (brContr
                                  const char *bridge,
                                  int *enable);
 
+int     brSetInterfaceMac         (brControl *ctl,
+                                   const char *ifname,
+                                   const unsigned char *macaddr);
+
+int     brGetInterfaceMac         (brControl *ctl,
+                                   const char *ifname,
+                                   unsigned char *macaddr);
+
 # endif /* WITH_BRIDGE */
 
 #endif /* __QEMUD_BRIDGE_H__ */

===================================================================

Best regards, 

Gerhard Stenzel
-------------------------------------------------------------------------------------
IBM Deutschland Research & Development GmbH
Vorsitzender des Aufsichtsrats: Martin Jetter
Geschaeftsfuehrung: Dirk Wittkopp
Sitz der Gesellschaft: Boeblingen
Registergericht: Amtsgericht Stuttgart, HRB 243294




More information about the libvir-list mailing list