Move MAC address replacement functions to virnetdev.c

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
This commit is contained in:
Daniel P. Berrange 2011-11-02 17:40:50 +00:00
parent 10462d5c78
commit 255917f516
4 changed files with 103 additions and 94 deletions

View File

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

View File

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

View File

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

View File

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