提交 29b242ad 编写于 作者: D Daniel P. Berrange

Rename the LXC veth management APIs and delete duplicated APIs

The src/lxc/veth.c file contains APIs for managing veth devices,
but some of the APIs duplicate stuff from src/util/virnetdev.h.
Delete thed duplicate APIs and rename the remaining ones to
follow virNetDevVethXXXX

* src/lxc/veth.c, src/lxc/veth.h: Rename APIs & delete duplicates
* src/lxc/lxc_container.c, src/lxc/lxc_controller.c,
  src/lxc/lxc_driver.c: Update for API renaming
上级 d3406045
...@@ -42,7 +42,6 @@ src/lxc/lxc_container.c ...@@ -42,7 +42,6 @@ src/lxc/lxc_container.c
src/lxc/lxc_conf.c src/lxc/lxc_conf.c
src/lxc/lxc_controller.c src/lxc/lxc_controller.c
src/lxc/lxc_driver.c src/lxc/lxc_driver.c
src/lxc/veth.c
src/libxl/libxl_driver.c src/libxl/libxl_driver.c
src/libxl/libxl_conf.c src/libxl/libxl_conf.c
src/network/bridge_driver.c src/network/bridge_driver.c
......
...@@ -59,6 +59,7 @@ ...@@ -59,6 +59,7 @@
#include "uuid.h" #include "uuid.h"
#include "virfile.h" #include "virfile.h"
#include "command.h" #include "command.h"
#include "virnetdev.h"
#define VIR_FROM_THIS VIR_FROM_LXC #define VIR_FROM_THIS VIR_FROM_LXC
...@@ -268,12 +269,12 @@ static int lxcContainerRenameAndEnableInterfaces(unsigned int nveths, ...@@ -268,12 +269,12 @@ static int lxcContainerRenameAndEnableInterfaces(unsigned int nveths,
} }
VIR_DEBUG("Renaming %s to %s", veths[i], newname); VIR_DEBUG("Renaming %s to %s", veths[i], newname);
rc = setInterfaceName(veths[i], newname); rc = virNetDevSetName(veths[i], newname);
if (rc < 0) if (rc < 0)
goto error_out; goto error_out;
VIR_DEBUG("Enabling %s", newname); VIR_DEBUG("Enabling %s", newname);
rc = vethInterfaceUpOrDown(newname, 1); rc = virNetDevSetOnline(newname, true);
if (rc < 0) if (rc < 0)
goto error_out; goto error_out;
...@@ -282,7 +283,7 @@ static int lxcContainerRenameAndEnableInterfaces(unsigned int nveths, ...@@ -282,7 +283,7 @@ static int lxcContainerRenameAndEnableInterfaces(unsigned int nveths,
/* enable lo device only if there were other net devices */ /* enable lo device only if there were other net devices */
if (veths) if (veths)
rc = vethInterfaceUpOrDown("lo", 1); rc = virNetDevSetOnline("lo", true);
error_out: error_out:
VIR_FREE(newname); VIR_FREE(newname);
......
...@@ -933,7 +933,7 @@ static int lxcControllerMoveInterfaces(unsigned int nveths, ...@@ -933,7 +933,7 @@ static int lxcControllerMoveInterfaces(unsigned int nveths,
{ {
unsigned int i; unsigned int i;
for (i = 0 ; i < nveths ; i++) for (i = 0 ; i < nveths ; i++)
if (moveInterfaceToNetNs(veths[i], container) < 0) if (virNetDevSetNamespace(veths[i], container) < 0)
return -1; return -1;
return 0; return 0;
...@@ -954,7 +954,7 @@ static int lxcControllerCleanupInterfaces(unsigned int nveths, ...@@ -954,7 +954,7 @@ static int lxcControllerCleanupInterfaces(unsigned int nveths,
{ {
unsigned int i; unsigned int i;
for (i = 0 ; i < nveths ; i++) for (i = 0 ; i < nveths ; i++)
vethDelete(veths[i]); ignore_value(virNetDevVethDelete(veths[i]));
return 0; return 0;
} }
......
...@@ -55,6 +55,7 @@ ...@@ -55,6 +55,7 @@
#include "domain_audit.h" #include "domain_audit.h"
#include "domain_nwfilter.h" #include "domain_nwfilter.h"
#include "network/bridge_driver.h" #include "network/bridge_driver.h"
#include "virnetdev.h"
#define VIR_FROM_THIS VIR_FROM_LXC #define VIR_FROM_THIS VIR_FROM_LXC
...@@ -1152,8 +1153,8 @@ static void lxcVmCleanup(lxc_driver_t *driver, ...@@ -1152,8 +1153,8 @@ static void lxcVmCleanup(lxc_driver_t *driver,
priv->monitorWatch = -1; priv->monitorWatch = -1;
for (i = 0 ; i < vm->def->nnets ; i++) { for (i = 0 ; i < vm->def->nnets ; i++) {
vethInterfaceUpOrDown(vm->def->nets[i]->ifname, 0); ignore_value(virNetDevSetOnline(vm->def->nets[i]->ifname, false));
vethDelete(vm->def->nets[i]->ifname); ignore_value(virNetDevVethDelete(vm->def->nets[i]->ifname));
networkReleaseActualDevice(vm->def->nets[i]); networkReleaseActualDevice(vm->def->nets[i]);
} }
...@@ -1246,7 +1247,7 @@ static int lxcSetupInterfaces(virConnectPtr conn, ...@@ -1246,7 +1247,7 @@ static int lxcSetupInterfaces(virConnectPtr conn,
VIR_DEBUG("calling vethCreate()"); VIR_DEBUG("calling vethCreate()");
parentVeth = def->nets[i]->ifname; parentVeth = def->nets[i]->ifname;
if (vethCreate(&parentVeth, &containerVeth) < 0) if (virNetDevVethCreate(&parentVeth, &containerVeth) < 0)
goto error_exit; goto error_exit;
VIR_DEBUG("parentVeth: %s, containerVeth: %s", parentVeth, containerVeth); VIR_DEBUG("parentVeth: %s, containerVeth: %s", parentVeth, containerVeth);
...@@ -1262,17 +1263,13 @@ static int lxcSetupInterfaces(virConnectPtr conn, ...@@ -1262,17 +1263,13 @@ static int lxcSetupInterfaces(virConnectPtr conn,
(*veths)[(*nveths)] = containerVeth; (*veths)[(*nveths)] = containerVeth;
(*nveths)++; (*nveths)++;
{ if (virNetDevSetMAC(containerVeth, def->nets[i]->mac) < 0)
char macaddr[VIR_MAC_STRING_BUFLEN]; goto error_exit;
virFormatMacAddr(def->nets[i]->mac, macaddr);
if (setMacAddr(containerVeth, macaddr) < 0)
goto error_exit;
}
if (virNetDevBridgeAddPort(bridge, parentVeth) < 0) if (virNetDevBridgeAddPort(bridge, parentVeth) < 0)
goto error_exit; goto error_exit;
if (vethInterfaceUpOrDown(parentVeth, 1) < 0) if (virNetDevSetOnline(parentVeth, true) < 0)
goto error_exit; goto error_exit;
if (virNetDevBandwidthSet(def->nets[i]->ifname, if (virNetDevBandwidthSet(def->nets[i]->ifname,
...@@ -1828,7 +1825,7 @@ cleanup: ...@@ -1828,7 +1825,7 @@ cleanup:
} }
for (i = 0 ; i < nveths ; i++) { for (i = 0 ; i < nveths ; i++) {
if (rc != 0) if (rc != 0)
vethDelete(veths[i]); ignore_value(virNetDevVethDelete(veths[i]));
VIR_FREE(veths[i]); VIR_FREE(veths[i]);
} }
if (rc != 0) { if (rc != 0) {
......
...@@ -28,15 +28,15 @@ ...@@ -28,15 +28,15 @@
#include "virterror_internal.h" #include "virterror_internal.h"
#include "virfile.h" #include "virfile.h"
#define VIR_FROM_THIS VIR_FROM_LXC #define VIR_FROM_THIS VIR_FROM_NONE
#define vethError(code, ...) \ #define virNetDevvError(code, ...) \
virReportErrorHelper(VIR_FROM_LXC, code, __FILE__, \ virReportErrorHelper(VIR_FROM_NONE, code, __FILE__, \
__FUNCTION__, __LINE__, __VA_ARGS__) __FUNCTION__, __LINE__, __VA_ARGS__)
/* Functions */ /* Functions */
/** /**
* getFreeVethName: * virNetDevVethGetFreeName:
* @veth: pointer to store returned name for veth device * @veth: pointer to store returned name for veth device
* @startDev: device number to start at (x in vethx) * @startDev: device number to start at (x in vethx)
* *
...@@ -45,7 +45,7 @@ ...@@ -45,7 +45,7 @@
* *
* Returns non-negative device number on success or -1 in case of error * Returns non-negative device number on success or -1 in case of error
*/ */
static int getFreeVethName(char **veth, int startDev) static int virNetDevVethGetFreeName(char **veth, int startDev)
{ {
int devNum = startDev-1; int devNum = startDev-1;
char *path = NULL; char *path = NULL;
...@@ -71,7 +71,7 @@ static int getFreeVethName(char **veth, int startDev) ...@@ -71,7 +71,7 @@ static int getFreeVethName(char **veth, int startDev)
} }
/** /**
* vethCreate: * virNetDevVethCreate:
* @veth1: pointer to name for parent end of veth pair * @veth1: pointer to name for parent end of veth pair
* @veth2: pointer to return name for container end of veth pair * @veth2: pointer to return name for container end of veth pair
* *
...@@ -95,7 +95,7 @@ static int getFreeVethName(char **veth, int startDev) ...@@ -95,7 +95,7 @@ static int getFreeVethName(char **veth, int startDev)
* *
* Returns 0 on success or -1 in case of error * Returns 0 on success or -1 in case of error
*/ */
int vethCreate(char** veth1, char** veth2) int virNetDevVethCreate(char** veth1, char** veth2)
{ {
int rc = -1; int rc = -1;
const char *argv[] = { const char *argv[] = {
...@@ -108,7 +108,7 @@ int vethCreate(char** veth1, char** veth2) ...@@ -108,7 +108,7 @@ int vethCreate(char** veth1, char** veth2)
VIR_DEBUG("Host: %s guest: %s", NULLSTR(*veth1), NULLSTR(*veth2)); VIR_DEBUG("Host: %s guest: %s", NULLSTR(*veth1), NULLSTR(*veth2));
if (*veth1 == NULL) { if (*veth1 == NULL) {
if ((vethDev = getFreeVethName(veth1, vethDev)) < 0) if ((vethDev = virNetDevVethGetFreeName(veth1, vethDev)) < 0)
goto cleanup; goto cleanup;
VIR_DEBUG("Assigned host: %s", *veth1); VIR_DEBUG("Assigned host: %s", *veth1);
veth1_alloc = true; veth1_alloc = true;
...@@ -117,7 +117,7 @@ int vethCreate(char** veth1, char** veth2) ...@@ -117,7 +117,7 @@ int vethCreate(char** veth1, char** veth2)
argv[3] = *veth1; argv[3] = *veth1;
while (*veth2 == NULL) { while (*veth2 == NULL) {
if ((vethDev = getFreeVethName(veth2, vethDev)) < 0) { if ((vethDev = virNetDevVethGetFreeName(veth2, vethDev)) < 0) {
if (veth1_alloc) if (veth1_alloc)
VIR_FREE(*veth1); VIR_FREE(*veth1);
goto cleanup; goto cleanup;
...@@ -151,7 +151,7 @@ cleanup: ...@@ -151,7 +151,7 @@ cleanup:
} }
/** /**
* vethDelete: * virNetDevVethDelete:
* @veth: name for one end of veth pair * @veth: name for one end of veth pair
* *
* This will delete both veth devices in a pair. Only one end needs to * This will delete both veth devices in a pair. Only one end needs to
...@@ -161,7 +161,7 @@ cleanup: ...@@ -161,7 +161,7 @@ cleanup:
* *
* Returns 0 on success or -1 in case of error * Returns 0 on success or -1 in case of error
*/ */
int vethDelete(const char *veth) int virNetDevVethDelete(const char *veth)
{ {
int rc; int rc;
const char *argv[] = {"ip", "link", "del", veth, NULL}; const char *argv[] = {"ip", "link", "del", veth, NULL};
...@@ -185,59 +185,10 @@ int vethDelete(const char *veth) ...@@ -185,59 +185,10 @@ int vethDelete(const char *veth)
return rc; return rc;
} }
/**
* vethInterfaceUpOrDown:
* @veth: name of veth device
* @upOrDown: 0 => down, 1 => up
*
* Enables a veth device using SIOCSIFFLAGS
*
* Returns 0 on success, -1 on failure, with errno set
*/
int vethInterfaceUpOrDown(const char* veth, int upOrDown)
{
struct ifreq ifr;
int fd, ret;
if ((fd = socket(PF_PACKET, SOCK_DGRAM, 0)) == -1)
return(-1);
memset(&ifr, 0, sizeof(struct ifreq));
if (virStrcpyStatic(ifr.ifr_name, veth) == NULL) {
errno = EINVAL;
return -1;
}
if ((ret = ioctl(fd, SIOCGIFFLAGS, &ifr)) == 0) {
if (upOrDown)
ifr.ifr_flags |= IFF_UP;
else
ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
ret = ioctl(fd, SIOCSIFFLAGS, &ifr);
}
VIR_FORCE_CLOSE(fd);
if (ret == -1)
if (upOrDown == 0)
/*
* Prevent overwriting an error log which may be set
* where an actual failure occurs.
*/
VIR_DEBUG("Failed to disable '%s'", veth);
else
vethError(VIR_ERR_INTERNAL_ERROR,
_("Failed to enable '%s'"), veth);
else
ret = 0;
return(ret);
}
/** /**
* moveInterfaceToNetNs: * virNetDevSetNamespace:
* @iface: name of device * @ifname: name of device
* @pidInNs: PID of process in target net namespace * @pidInNs: PID of process in target net namespace
* *
* Moves the given device into the target net namespace specified by the given * Moves the given device into the target net namespace specified by the given
...@@ -246,12 +197,12 @@ int vethInterfaceUpOrDown(const char* veth, int upOrDown) ...@@ -246,12 +197,12 @@ int vethInterfaceUpOrDown(const char* veth, int upOrDown)
* *
* Returns 0 on success or -1 in case of error * Returns 0 on success or -1 in case of error
*/ */
int moveInterfaceToNetNs(const char* iface, int pidInNs) int virNetDevSetNamespace(const char* ifname, int pidInNs)
{ {
int rc; int rc;
char *pid = NULL; char *pid = NULL;
const char *argv[] = { const char *argv[] = {
"ip", "link", "set", iface, "netns", NULL, NULL "ip", "link", "set", ifname, "netns", NULL, NULL
}; };
if (virAsprintf(&pid, "%d", pidInNs) == -1) { if (virAsprintf(&pid, "%d", pidInNs) == -1) {
...@@ -267,42 +218,22 @@ int moveInterfaceToNetNs(const char* iface, int pidInNs) ...@@ -267,42 +218,22 @@ int moveInterfaceToNetNs(const char* iface, int pidInNs)
} }
/** /**
* setMacAddr * virNetDevSetName:
* @iface: name of device * @ifname: name of device
* @macaddr: MAC address to be assigned * @new: new name of @ifname
*
* Changes the MAC address of the given device with the
* given address using this command:
* ip link set @iface address @macaddr
*
* Returns 0 on success or -1 in case of error
*/
int setMacAddr(const char* iface, const char* macaddr)
{
const char *argv[] = {
"ip", "link", "set", iface, "address", macaddr, NULL
};
return virRun(argv, NULL);
}
/**
* setInterfaceName
* @iface: name of device
* @new: new name of @iface
* *
* Changes the name of the given device. * Changes the name of the given device.
* *
* Returns 0 on success, -1 on failure with errno set. * Returns 0 on success, -1 on failure with errno set.
*/ */
int setInterfaceName(const char* iface, const char* new) int virNetDevSetName(const char* ifname, const char* new)
{ {
struct ifreq ifr; struct ifreq ifr;
int fd = socket(PF_PACKET, SOCK_DGRAM, 0); int fd = socket(PF_PACKET, SOCK_DGRAM, 0);
memset(&ifr, 0, sizeof(struct ifreq)); memset(&ifr, 0, sizeof(struct ifreq));
if (virStrcpyStatic(ifr.ifr_name, iface) == NULL) { if (virStrcpyStatic(ifr.ifr_name, ifname) == NULL) {
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
......
...@@ -17,17 +17,13 @@ ...@@ -17,17 +17,13 @@
# include "internal.h" # include "internal.h"
/* Function declarations */ /* Function declarations */
int vethCreate(char** veth1, char** veth2) int virNetDevVethCreate(char **veth1, char **veth2)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
int vethDelete(const char* veth) int virNetDevVethDelete(const char *veth)
ATTRIBUTE_NONNULL(1); ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
int vethInterfaceUpOrDown(const char* veth, int upOrDown) int virNetDevSetNamespace(const char *ifname, int pidInNs)
ATTRIBUTE_NONNULL(1); ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
int moveInterfaceToNetNs(const char *iface, int pidInNs) int virNetDevSetName(const char *ifname, const char *newifname)
ATTRIBUTE_NONNULL(1); ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
int setMacAddr(const char* iface, const char* macaddr)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
int setInterfaceName(const char* iface, const char* new)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
#endif /* VETH_H */ #endif /* VETH_H */
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册