#include "domain_audit.h"
#include "domain_nwfilter.h"
#include "network/bridge_driver.h"
+#include "virnetdev.h"
#define VIR_FROM_THIS VIR_FROM_LXC
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]);
}
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);
(*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,
}
for (i = 0 ; i < nveths ; i++) {
if (rc != 0)
- vethDelete(veths[i]);
+ ignore_value(virNetDevVethDelete(veths[i]));
VIR_FREE(veths[i]);
}
if (rc != 0) {
#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)
*
*
* 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;
}
/**
- * vethCreate:
+ * virNetDevVethCreate:
* @veth1: pointer to name for parent end of veth pair
* @veth2: pointer to return name for container end of veth pair
*
*
* 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[] = {
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;
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;
}
/**
- * vethDelete:
+ * virNetDevVethDelete:
* @veth: name for one end of veth pair
*
* This will delete both veth devices in a pair. Only one end needs to
*
* 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};
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
*
* 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) {
}
/**
- * 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;
}
# 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 */