diff --git a/po/POTFILES.in b/po/POTFILES.in index 1665d2d39346ae6479c2d79675b1237e71bb29ca..810cf68fd0389b078d03a5fd8772734651b859fa 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -42,7 +42,6 @@ src/lxc/lxc_container.c src/lxc/lxc_conf.c src/lxc/lxc_controller.c src/lxc/lxc_driver.c -src/lxc/veth.c src/libxl/libxl_driver.c src/libxl/libxl_conf.c src/network/bridge_driver.c diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c index c2484a75745f7b6b10b2da8a624c8374c3367515..137a07ccb7d424202b3ad5eae232171590fd0f7f 100644 --- a/src/lxc/lxc_container.c +++ b/src/lxc/lxc_container.c @@ -59,6 +59,7 @@ #include "uuid.h" #include "virfile.h" #include "command.h" +#include "virnetdev.h" #define VIR_FROM_THIS VIR_FROM_LXC @@ -268,12 +269,12 @@ static int lxcContainerRenameAndEnableInterfaces(unsigned int nveths, } VIR_DEBUG("Renaming %s to %s", veths[i], newname); - rc = setInterfaceName(veths[i], newname); + rc = virNetDevSetName(veths[i], newname); if (rc < 0) goto error_out; VIR_DEBUG("Enabling %s", newname); - rc = vethInterfaceUpOrDown(newname, 1); + rc = virNetDevSetOnline(newname, true); if (rc < 0) goto error_out; @@ -282,7 +283,7 @@ static int lxcContainerRenameAndEnableInterfaces(unsigned int nveths, /* enable lo device only if there were other net devices */ if (veths) - rc = vethInterfaceUpOrDown("lo", 1); + rc = virNetDevSetOnline("lo", true); error_out: VIR_FREE(newname); diff --git a/src/lxc/lxc_controller.c b/src/lxc/lxc_controller.c index 137ef528609dc4ca9716dc7d5d9400f4ab03bda6..dcf78f9af696af9f3f72363e30d3634977595240 100644 --- a/src/lxc/lxc_controller.c +++ b/src/lxc/lxc_controller.c @@ -933,7 +933,7 @@ static int lxcControllerMoveInterfaces(unsigned int nveths, { unsigned int i; for (i = 0 ; i < nveths ; i++) - if (moveInterfaceToNetNs(veths[i], container) < 0) + if (virNetDevSetNamespace(veths[i], container) < 0) return -1; return 0; @@ -954,7 +954,7 @@ static int lxcControllerCleanupInterfaces(unsigned int nveths, { unsigned int i; for (i = 0 ; i < nveths ; i++) - vethDelete(veths[i]); + ignore_value(virNetDevVethDelete(veths[i])); return 0; } diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c index f945e2ae528aed9386a5788acb9f889f656be94d..dc20d6eff4e6566fac3b3144ddc7949ae309bb02 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -55,6 +55,7 @@ #include "domain_audit.h" #include "domain_nwfilter.h" #include "network/bridge_driver.h" +#include "virnetdev.h" #define VIR_FROM_THIS VIR_FROM_LXC @@ -1152,8 +1153,8 @@ static void lxcVmCleanup(lxc_driver_t *driver, priv->monitorWatch = -1; for (i = 0 ; i < vm->def->nnets ; i++) { - vethInterfaceUpOrDown(vm->def->nets[i]->ifname, 0); - vethDelete(vm->def->nets[i]->ifname); + ignore_value(virNetDevSetOnline(vm->def->nets[i]->ifname, false)); + ignore_value(virNetDevVethDelete(vm->def->nets[i]->ifname)); networkReleaseActualDevice(vm->def->nets[i]); } @@ -1246,7 +1247,7 @@ static int lxcSetupInterfaces(virConnectPtr conn, VIR_DEBUG("calling vethCreate()"); parentVeth = def->nets[i]->ifname; - if (vethCreate(&parentVeth, &containerVeth) < 0) + if (virNetDevVethCreate(&parentVeth, &containerVeth) < 0) goto error_exit; VIR_DEBUG("parentVeth: %s, containerVeth: %s", parentVeth, containerVeth); @@ -1262,17 +1263,13 @@ static int lxcSetupInterfaces(virConnectPtr conn, (*veths)[(*nveths)] = containerVeth; (*nveths)++; - { - char macaddr[VIR_MAC_STRING_BUFLEN]; - virFormatMacAddr(def->nets[i]->mac, macaddr); - if (setMacAddr(containerVeth, macaddr) < 0) - goto error_exit; - } + if (virNetDevSetMAC(containerVeth, def->nets[i]->mac) < 0) + goto error_exit; if (virNetDevBridgeAddPort(bridge, parentVeth) < 0) goto error_exit; - if (vethInterfaceUpOrDown(parentVeth, 1) < 0) + if (virNetDevSetOnline(parentVeth, true) < 0) goto error_exit; if (virNetDevBandwidthSet(def->nets[i]->ifname, @@ -1828,7 +1825,7 @@ cleanup: } for (i = 0 ; i < nveths ; i++) { if (rc != 0) - vethDelete(veths[i]); + ignore_value(virNetDevVethDelete(veths[i])); VIR_FREE(veths[i]); } if (rc != 0) { diff --git a/src/lxc/veth.c b/src/lxc/veth.c index e4db65c920f302c2ff55531c8c86005547809392..b31ce33edff7b0d784a65f79e1d4ca9e5e1bf456 100644 --- a/src/lxc/veth.c +++ b/src/lxc/veth.c @@ -28,15 +28,15 @@ #include "virterror_internal.h" #include "virfile.h" -#define VIR_FROM_THIS VIR_FROM_LXC +#define VIR_FROM_THIS VIR_FROM_NONE -#define vethError(code, ...) \ - virReportErrorHelper(VIR_FROM_LXC, code, __FILE__, \ +#define virNetDevvError(code, ...) \ + virReportErrorHelper(VIR_FROM_NONE, code, __FILE__, \ __FUNCTION__, __LINE__, __VA_ARGS__) /* Functions */ /** - * getFreeVethName: + * virNetDevVethGetFreeName: * @veth: pointer to store returned name for veth device * @startDev: device number to start at (x in vethx) * @@ -45,7 +45,7 @@ * * 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; char *path = NULL; @@ -71,7 +71,7 @@ static int getFreeVethName(char **veth, int startDev) } /** - * vethCreate: + * virNetDevVethCreate: * @veth1: pointer to name for parent 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) * * 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; const char *argv[] = { @@ -108,7 +108,7 @@ int vethCreate(char** veth1, char** veth2) VIR_DEBUG("Host: %s guest: %s", NULLSTR(*veth1), NULLSTR(*veth2)); if (*veth1 == NULL) { - if ((vethDev = getFreeVethName(veth1, vethDev)) < 0) + if ((vethDev = virNetDevVethGetFreeName(veth1, vethDev)) < 0) goto cleanup; VIR_DEBUG("Assigned host: %s", *veth1); veth1_alloc = true; @@ -117,7 +117,7 @@ int vethCreate(char** veth1, char** veth2) argv[3] = *veth1; while (*veth2 == NULL) { - if ((vethDev = getFreeVethName(veth2, vethDev)) < 0) { + if ((vethDev = virNetDevVethGetFreeName(veth2, vethDev)) < 0) { if (veth1_alloc) VIR_FREE(*veth1); goto cleanup; @@ -151,7 +151,7 @@ cleanup: } /** - * vethDelete: + * virNetDevVethDelete: * @veth: name for one end of veth pair * * This will delete both veth devices in a pair. Only one end needs to @@ -161,7 +161,7 @@ cleanup: * * Returns 0 on success or -1 in case of error */ -int vethDelete(const char *veth) +int virNetDevVethDelete(const char *veth) { int rc; const char *argv[] = {"ip", "link", "del", veth, NULL}; @@ -185,59 +185,10 @@ int vethDelete(const char *veth) 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: - * @iface: name of device + * virNetDevSetNamespace: + * @ifname: name of device * @pidInNs: PID of process in target net namespace * * Moves the given device into the target net namespace specified by the given @@ -246,12 +197,12 @@ int vethInterfaceUpOrDown(const char* veth, int upOrDown) * * 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; char *pid = NULL; const char *argv[] = { - "ip", "link", "set", iface, "netns", NULL, NULL + "ip", "link", "set", ifname, "netns", NULL, NULL }; if (virAsprintf(&pid, "%d", pidInNs) == -1) { @@ -267,42 +218,22 @@ int moveInterfaceToNetNs(const char* iface, int pidInNs) } /** - * setMacAddr - * @iface: name of device - * @macaddr: MAC address to be assigned - * - * 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 + * virNetDevSetName: + * @ifname: name of device + * @new: new name of @ifname * * Changes the name of the given device. * * 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; int fd = socket(PF_PACKET, SOCK_DGRAM, 0); memset(&ifr, 0, sizeof(struct ifreq)); - if (virStrcpyStatic(ifr.ifr_name, iface) == NULL) { + if (virStrcpyStatic(ifr.ifr_name, ifname) == NULL) { errno = EINVAL; return -1; } diff --git a/src/lxc/veth.h b/src/lxc/veth.h index f50a939f2b917dba5525c4e1c4fe2dd4d1c01617..4a66098731cc4c5fbef40ed29396c8541af63ce0 100644 --- a/src/lxc/veth.h +++ b/src/lxc/veth.h @@ -17,17 +17,13 @@ # include "internal.h" /* Function declarations */ -int vethCreate(char** veth1, char** veth2) - ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); -int vethDelete(const char* veth) - ATTRIBUTE_NONNULL(1); -int vethInterfaceUpOrDown(const char* veth, int upOrDown) - ATTRIBUTE_NONNULL(1); -int moveInterfaceToNetNs(const char *iface, int pidInNs) - ATTRIBUTE_NONNULL(1); -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); +int virNetDevVethCreate(char **veth1, char **veth2) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK; +int virNetDevVethDelete(const char *veth) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK; +int virNetDevSetNamespace(const char *ifname, int pidInNs) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK; +int virNetDevSetName(const char *ifname, const char *newifname) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK; #endif /* VETH_H */