From: "Daniel P. Berrange" <berrange@xxxxxxxxxx> Move virNetDevReplaceMacAddress and virNetDevRestoreMacAddress to the virnetdev.c file where they naturally belong * util/interface.c, util/interface.h: Remove virNetDevReplaceMacAddress and virNetDevRestoreMacAddress * util/virnetdev.c, util/virnetdev.h: Add virNetDevReplaceMacAddress and virNetDevRestoreMacAddress --- src/util/interface.c | 84 ---------------------------------------------- src/util/interface.h | 10 ----- src/util/virnetdev.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/util/virnetdev.h | 12 ++++++ 4 files changed, 103 insertions(+), 94 deletions(-) diff --git a/src/util/interface.c b/src/util/interface.c index df2aa88..ebe537e 100644 --- a/src/util/interface.c +++ b/src/util/interface.c @@ -553,90 +553,6 @@ ifaceGetNthParent(int ifindex ATTRIBUTE_UNUSED, #endif -/** - * virNetDevReplaceMacAddress: - * @macaddress: new MAC address for interface - * @linkdev: name of interface - * @stateDir: directory to store old MAC address - * - * Returns 0 on success, -1 on failure - * - */ -int -virNetDevReplaceMacAddress(const char *linkdev, - const unsigned char *macaddress, - const char *stateDir) -{ - unsigned char oldmac[6]; - char *path = NULL; - char macstr[VIR_MAC_STRING_BUFLEN]; - - if (virNetDevGetMAC(linkdev, oldmac) < 0) - return -1; - - - if (virAsprintf(&path, "%s/%s", - stateDir, - linkdev) < 0) { - virReportOOMError(); - return -1; - } - virFormatMacAddr(oldmac, macstr); - if (virFileWriteStr(path, macstr, O_CREAT|O_TRUNC|O_WRONLY) < 0) { - virReportSystemError(errno, _("Unable to preserve mac for %s"), - linkdev); - return -1; - } - - if (virNetDevSetMAC(linkdev, macaddress) < 0) - return -1; - - return 0; -} - -/** - * virNetDevRestoreMacAddress: - * @linkdev: name of interface - * @stateDir: directory containing old MAC address - * - * Returns 0 on success, -errno on failure. - * - */ -int -virNetDevRestoreMacAddress(const char *linkdev, - const char *stateDir) -{ - int rc; - char *oldmacname = NULL; - char *macstr = NULL; - char *path = NULL; - unsigned char oldmac[6]; - - if (virAsprintf(&path, "%s/%s", - stateDir, - linkdev) < 0) { - virReportOOMError(); - return -1; - } - - if (virFileReadAll(path, VIR_MAC_STRING_BUFLEN, &macstr) < 0) - return -1; - - if (virParseMacAddr(macstr, &oldmac[0]) != 0) { - ifaceError(VIR_ERR_INTERNAL_ERROR, - _("Cannot parse MAC address from '%s'"), - oldmacname); - VIR_FREE(macstr); - return -1; - } - - /*reset mac and remove file-ignore results*/ - rc = virNetDevSetMAC(linkdev, oldmac); - ignore_value(unlink(path)); - VIR_FREE(macstr); - - return rc; -} #ifdef __linux__ static int diff --git a/src/util/interface.h b/src/util/interface.h index 6a4631c..e322a21 100644 --- a/src/util/interface.h +++ b/src/util/interface.h @@ -49,16 +49,6 @@ int ifaceGetNthParent(int ifindex, const char *ifname, unsigned int nthParent, ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(4) ATTRIBUTE_NONNULL(5) ATTRIBUTE_NONNULL(6); -int virNetDevReplaceMacAddress(const char *linkdev, - const unsigned char *macaddress, - const char *stateDir) - ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3) - ATTRIBUTE_RETURN_CHECK; - -int virNetDevRestoreMacAddress(const char *linkdev, - const char *stateDir) - ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK; - int ifaceIsVirtualFunction(const char *ifname); int ifaceGetVirtualFunctionIndex(const char *pfname, const char *vfname, diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c index 28aa1fb..230dbeb 100644 --- a/src/util/virnetdev.c +++ b/src/util/virnetdev.c @@ -32,8 +32,12 @@ #ifdef HAVE_NET_IF_H # include <net/if.h> #endif +#include <fcntl.h> #define VIR_FROM_THIS VIR_FROM_NONE +#define virNetDevError(code, ...) \ + virReportErrorHelper(VIR_FROM_THIS, code, __FILE__, \ + __FUNCTION__, __LINE__, __VA_ARGS__) #ifdef HAVE_NET_IF_H static int virNetDevSetupControlFull(const char *ifname, @@ -223,6 +227,93 @@ int virNetDevGetMAC(const char *ifname, #endif + +/** + * virNetDevReplaceMacAddress: + * @macaddress: new MAC address for interface + * @linkdev: name of interface + * @stateDir: directory to store old MAC address + * + * Returns 0 on success, -1 on failure + * + */ +int +virNetDevReplaceMacAddress(const char *linkdev, + const unsigned char *macaddress, + const char *stateDir) +{ + unsigned char oldmac[6]; + char *path = NULL; + char macstr[VIR_MAC_STRING_BUFLEN]; + + if (virNetDevGetMAC(linkdev, oldmac) < 0) + return -1; + + + if (virAsprintf(&path, "%s/%s", + stateDir, + linkdev) < 0) { + virReportOOMError(); + return -1; + } + virFormatMacAddr(oldmac, macstr); + if (virFileWriteStr(path, macstr, O_CREAT|O_TRUNC|O_WRONLY) < 0) { + virReportSystemError(errno, _("Unable to preserve mac for %s"), + linkdev); + return -1; + } + + if (virNetDevSetMAC(linkdev, macaddress) < 0) + return -1; + + return 0; +} + +/** + * virNetDevRestoreMacAddress: + * @linkdev: name of interface + * @stateDir: directory containing old MAC address + * + * Returns 0 on success, -errno on failure. + * + */ +int +virNetDevRestoreMacAddress(const char *linkdev, + const char *stateDir) +{ + int rc; + char *oldmacname = NULL; + char *macstr = NULL; + char *path = NULL; + unsigned char oldmac[6]; + + if (virAsprintf(&path, "%s/%s", + stateDir, + linkdev) < 0) { + virReportOOMError(); + return -1; + } + + if (virFileReadAll(path, VIR_MAC_STRING_BUFLEN, &macstr) < 0) + return -1; + + if (virParseMacAddr(macstr, &oldmac[0]) != 0) { + virNetDevError(VIR_ERR_INTERNAL_ERROR, + _("Cannot parse MAC address from '%s'"), + oldmacname); + VIR_FREE(macstr); + return -1; + } + + /*reset mac and remove file-ignore results*/ + rc = virNetDevSetMAC(linkdev, oldmac); + ignore_value(unlink(path)); + VIR_FREE(macstr); + + return rc; +} + + #ifdef SIOCGIFMTU /** * virNetDevGetMTU: diff --git a/src/util/virnetdev.h b/src/util/virnetdev.h index 0b0db84..84966d7 100644 --- a/src/util/virnetdev.h +++ b/src/util/virnetdev.h @@ -51,6 +51,18 @@ int virNetDevSetMAC(const char *ifname, int virNetDevGetMAC(const char *ifname, unsigned char *macaddr) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK; + +int virNetDevReplaceMacAddress(const char *linkdev, + const unsigned char *macaddress, + const char *stateDir) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3) + ATTRIBUTE_RETURN_CHECK; + +int virNetDevRestoreMacAddress(const char *linkdev, + const char *stateDir) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK; + + int virNetDevSetMTU(const char *ifname, int mtu) ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK; -- 1.7.6.4 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list