util: new function virNetDevTapAttachBridge()

This patch splits out the part of virNetDevTapCreateInBridgePort()
that would need to be re-done if an existing tap device had to be
re-attached to a bridge, and puts it into a separate function. This
can be used both when an existing domain interface config is updated
to change its connection, and also to re-attach to the "same" bridge
when a network has been stopped and restarted. So far it is used for
nothing.
This commit is contained in:
Laine Stump 2017-03-18 14:03:20 -04:00
parent e75f5bfbe8
commit a454134951
3 changed files with 87 additions and 37 deletions

View File

@ -2107,6 +2107,7 @@ virNetDevOpenvswitchSetTimeout;
# util/virnetdevtap.h
virNetDevTapAttachBridge;
virNetDevTapCreate;
virNetDevTapCreateInBridgePort;
virNetDevTapDelete;

View File

@ -504,6 +504,77 @@ int virNetDevTapDelete(const char *ifname ATTRIBUTE_UNUSED,
#endif
/**
* virNetDevTapAttachBridge:
* @tapname: the tap interface name (or name template)
* @brname: the bridge name
* @macaddr: desired MAC address
* @virtPortProfile: bridge/port specific configuration
* @virtVlan: vlan tag info
* @mtu: requested MTU for port (or 0 for "default")
* @actualMTU: MTU actually set for port (after accounting for bridge's MTU)
*
* This attaches an existing tap device (@tapname) to a bridge
* (@brname).
*
* Returns 0 in case of success or -1 on failure
*/
int
virNetDevTapAttachBridge(const char *tapname,
const char *brname,
const virMacAddr *macaddr,
const unsigned char *vmuuid,
virNetDevVPortProfilePtr virtPortProfile,
virNetDevVlanPtr virtVlan,
unsigned int mtu,
unsigned int *actualMTU)
{
/* If an MTU is specified for the new device, set it before
* attaching the device to the bridge, as it may affect the MTU of
* the bridge (in particular if it is the first device attached to
* the bridge, or if it is smaller than the current MTU of the
* bridge). If MTU isn't specified for the new device (i.e. 0),
* we need to set the interface MTU to the current MTU of the
* bridge (to avoid inadvertantly changing the bridge's MTU).
*/
if (mtu > 0) {
if (virNetDevSetMTU(tapname, mtu) < 0)
goto error;
} else {
if (virNetDevSetMTUFromDevice(tapname, brname) < 0)
goto error;
}
if (actualMTU) {
int retMTU = virNetDevGetMTU(tapname);
if (retMTU < 0)
goto error;
*actualMTU = retMTU;
}
if (virtPortProfile) {
if (virtPortProfile->virtPortType == VIR_NETDEV_VPORT_PROFILE_MIDONET) {
if (virNetDevMidonetBindPort(tapname, virtPortProfile) < 0)
goto error;
} else if (virtPortProfile->virtPortType == VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH) {
if (virNetDevOpenvswitchAddPort(brname, tapname, macaddr, vmuuid,
virtPortProfile, virtVlan) < 0)
goto error;
}
} else {
if (virNetDevBridgeAddPort(brname, tapname) < 0)
goto error;
}
return 0;
error:
return -1;
}
/**
* virNetDevTapCreateInBridgePort:
* @brname: the bridge name
@ -582,43 +653,9 @@ int virNetDevTapCreateInBridgePort(const char *brname,
if (virNetDevSetMAC(*ifname, &tapmac) < 0)
goto error;
/* If an MTU is specified for the new device, set it before
* attaching the device to the bridge, as it may affect the MTU of
* the bridge (in particular if it is the first device attached to
* the bridge, or if it is smaller than the current MTU of the
* bridge). If MTU isn't specified for the new device (i.e. 0),
* we need to set the interface MTU to the current MTU of the
* bridge (to avoid inadvertantly changing the bridge's MTU).
*/
if (mtu > 0) {
if (virNetDevSetMTU(*ifname, mtu) < 0)
goto error;
} else {
if (virNetDevSetMTUFromDevice(*ifname, brname) < 0)
goto error;
}
if (actualMTU) {
int retMTU = virNetDevGetMTU(*ifname);
if (retMTU < 0)
goto error;
*actualMTU = retMTU;
}
if (virtPortProfile) {
if (virtPortProfile->virtPortType == VIR_NETDEV_VPORT_PROFILE_MIDONET) {
if (virNetDevMidonetBindPort(*ifname, virtPortProfile) < 0)
goto error;
} else if (virtPortProfile->virtPortType == VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH) {
if (virNetDevOpenvswitchAddPort(brname, *ifname, macaddr, vmuuid,
virtPortProfile, virtVlan) < 0)
goto error;
}
} else {
if (virNetDevBridgeAddPort(brname, *ifname) < 0)
goto error;
if (virNetDevTapAttachBridge(*ifname, brname, macaddr, vmuuid,
virtPortProfile, virtVlan, mtu, actualMTU) < 0) {
goto error;
}
if (virNetDevSetOnline(*ifname, !!(flags & VIR_NETDEV_TAP_CREATE_IFUP)) < 0)

View File

@ -62,6 +62,18 @@ typedef enum {
VIR_NETDEV_TAP_CREATE_PERSIST = 1 << 3,
} virNetDevTapCreateFlags;
int
virNetDevTapAttachBridge(const char *tapname,
const char *brname,
const virMacAddr *macaddr,
const unsigned char *vmuuid,
virNetDevVPortProfilePtr virtPortProfile,
virNetDevVlanPtr virtVlan,
unsigned int mtu,
unsigned int *actualMTU)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3)
ATTRIBUTE_RETURN_CHECK;
int virNetDevTapCreateInBridgePort(const char *brname,
char **ifname,
const virMacAddr *macaddr,