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

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@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

On Mon, Jun 20, 2011 at 03:54:31PM +0200, Gerhard Stenzel wrote:
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@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__ */
ACK, Daniel -- Daniel Veillard | libxml Gnome XML XSLT toolkit http://xmlsoft.org/ daniel@veillard.com | Rpmfind RPM search engine http://rpmfind.net/ http://veillard.com/ | virtualization library http://libvirt.org/

On Wed, 2011-06-22 at 16:30 +0800, Daniel Veillard wrote:
ACK,
Actually, this is not needed anymore, now that the functions are in interface.c -- Best regards, Gerhard Stenzel, ----------------------------------------------------------------------------------------------------------------------------------- IBM Deutschland Research & Development GmbH Vorsitzender des Aufsichtsrats: Martin Jetter Geschäftsführung: Dirk Wittkopp Sitz der Gesellschaft: Böblingen Registergericht: Amtsgericht Stuttgart, HRB 243294
participants (2)
-
Daniel Veillard
-
Gerhard Stenzel