提交 27908453 编写于 作者: T Tyler Coumbes 提交者: Eric Blake

bridge: modify for use when sVirt is enabled with qemu

This refactors the TAP creation code out of brAddTap into a new
function brCreateTap to allow it to be used on its own. I have also
changed ifSetInterfaceMac to brSetInterfaceMac and exported it since
it is will be needed by code outside of util/bridge.c in the next
patch.

 AUTHORS                 |    1 +
 src/libvirt_bridge.syms |    2 +
 src/util/bridge.c       |  116 +++++++++++++++++++++++++++++++----------------
 src/util/bridge.h       |    9 ++++
 4 files changed, 89 insertions(+), 39 deletions(-)
上级 c0d9dfe2
...@@ -199,6 +199,7 @@ Patches have also been contributed by: ...@@ -199,6 +199,7 @@ Patches have also been contributed by:
Dan Horák <dan@danny.cz> Dan Horák <dan@danny.cz>
Sage Weil <sage@newdream.net> Sage Weil <sage@newdream.net>
David L Stevens <dlstevens@us.ibm.com> David L Stevens <dlstevens@us.ibm.com>
Tyler Coumbes <coumbes@gmail.com>
[....send patches to get your name here....] [....send patches to get your name here....]
......
...@@ -9,13 +9,15 @@ brAddBridge; ...@@ -9,13 +9,15 @@ brAddBridge;
brAddInetAddress; brAddInetAddress;
brAddInterface; brAddInterface;
brAddTap; brAddTap;
brDeleteTap; brCreateTap;
brDeleteBridge;
brDelInetAddress; brDelInetAddress;
brDeleteBridge;
brDeleteTap;
brHasBridge; brHasBridge;
brInit; brInit;
brSetEnableSTP; brSetEnableSTP;
brSetForwardDelay; brSetForwardDelay;
brSetInetNetmask; brSetInetNetmask;
brSetInterfaceMac;
brSetInterfaceUp; brSetInterfaceUp;
brShutdown; brShutdown;
...@@ -278,7 +278,7 @@ brDeleteInterface(brControl *ctl ATTRIBUTE_UNUSED, ...@@ -278,7 +278,7 @@ brDeleteInterface(brControl *ctl ATTRIBUTE_UNUSED,
# endif # endif
/** /**
* ifSetInterfaceMac: * brSetInterfaceMac:
* @ctl: bridge control pointer * @ctl: bridge control pointer
* @ifname: interface name to set MTU for * @ifname: interface name to set MTU for
* @macaddr: MAC address (VIR_MAC_BUFLEN in size) * @macaddr: MAC address (VIR_MAC_BUFLEN in size)
...@@ -288,7 +288,8 @@ brDeleteInterface(brControl *ctl ATTRIBUTE_UNUSED, ...@@ -288,7 +288,8 @@ brDeleteInterface(brControl *ctl ATTRIBUTE_UNUSED,
* *
* Returns 0 in case of success or an errno code in case of failure. * Returns 0 in case of success or an errno code in case of failure.
*/ */
static int ifSetInterfaceMac(brControl *ctl, const char *ifname, int
brSetInterfaceMac(brControl *ctl, const char *ifname,
const unsigned char *macaddr) const unsigned char *macaddr)
{ {
struct ifreq ifr; struct ifreq ifr;
...@@ -478,32 +479,12 @@ brAddTap(brControl *ctl, ...@@ -478,32 +479,12 @@ brAddTap(brControl *ctl,
bool up, bool up,
int *tapfd) int *tapfd)
{ {
int fd;
struct ifreq ifr;
if (!ctl || !ctl->fd || !bridge || !ifname) if (!ctl || !ctl->fd || !bridge || !ifname)
return EINVAL; return EINVAL;
if ((fd = open("/dev/net/tun", O_RDWR)) < 0) errno = brCreateTap(ctl, ifname, vnet_hdr, tapfd);
return errno;
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TAP|IFF_NO_PI;
# ifdef IFF_VNET_HDR if (*tapfd < 0 || errno)
if (vnet_hdr && brProbeVnetHdr(fd))
ifr.ifr_flags |= IFF_VNET_HDR;
# else
(void) vnet_hdr;
# endif
if (virStrcpyStatic(ifr.ifr_name, *ifname) == NULL) {
errno = EINVAL;
goto error;
}
if (ioctl(fd, TUNSETIFF, &ifr) < 0)
goto error; goto error;
/* We need to set the interface MAC before adding it /* We need to set the interface MAC before adding it
...@@ -512,32 +493,22 @@ brAddTap(brControl *ctl, ...@@ -512,32 +493,22 @@ brAddTap(brControl *ctl,
* seeing the kernel allocate random MAC for the TAP * seeing the kernel allocate random MAC for the TAP
* device before we set our static MAC. * device before we set our static MAC.
*/ */
if ((errno = ifSetInterfaceMac(ctl, ifr.ifr_name, macaddr))) if ((errno = brSetInterfaceMac(ctl, *ifname, macaddr)))
goto error; goto error;
/* We need to set the interface MTU before adding it /* We need to set the interface MTU before adding it
* to the bridge, because the bridge will have its * to the bridge, because the bridge will have its
* MTU adjusted automatically when we add the new interface. * MTU adjusted automatically when we add the new interface.
*/ */
if ((errno = brSetInterfaceMtu(ctl, bridge, ifr.ifr_name))) if ((errno = brSetInterfaceMtu(ctl, bridge, *ifname)))
goto error;
if ((errno = brAddInterface(ctl, bridge, ifr.ifr_name)))
goto error; goto error;
if (up && ((errno = brSetInterfaceUp(ctl, ifr.ifr_name, 1)))) if ((errno = brAddInterface(ctl, bridge, *ifname)))
goto error; goto error;
if (!tapfd && if (up && ((errno = brSetInterfaceUp(ctl, *ifname, 1))))
(errno = ioctl(fd, TUNSETPERSIST, 1)))
goto error;
VIR_FREE(*ifname);
if (!(*ifname = strdup(ifr.ifr_name)))
goto error; goto error;
if (tapfd)
*tapfd = fd;
else
VIR_FORCE_CLOSE(fd);
return 0; return 0;
error: error:
VIR_FORCE_CLOSE(fd); VIR_FORCE_CLOSE(*tapfd);
return errno; return errno;
} }
...@@ -803,4 +774,70 @@ cleanup: ...@@ -803,4 +774,70 @@ cleanup:
return ret; return ret;
} }
/**
* brCreateTap:
* @ctl: bridge control pointer
* @ifname: the interface name
* @vnet_hr: whether to try enabling IFF_VNET_HDR
* @tapfd: file descriptor return value for the new tap device
*
* Creates a tap interface.
* If the @tapfd parameter is supplied, the open tap device file
* descriptor will be returned, otherwise the TAP device will be made
* persistent and closed. The caller must use brDeleteTap to remove
* a persistent TAP devices when it is no longer needed.
*
* Returns 0 in case of success or an errno code in case of failure.
*/
int
brCreateTap(brControl *ctl ATTRIBUTE_UNUSED,
char **ifname,
int vnet_hdr ATTRIBUTE_UNUSED,
int *tapfd)
{
int fd;
struct ifreq ifr;
if (!ifname)
return EINVAL;
if ((fd = open("/dev/net/tun", O_RDWR)) < 0)
return errno;
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TAP|IFF_NO_PI;
# ifdef IFF_VNET_HDR
if (vnet_hdr && brProbeVnetHdr(fd))
ifr.ifr_flags |= IFF_VNET_HDR;
# endif
if (virStrcpyStatic(ifr.ifr_name, *ifname) == NULL) {
errno = EINVAL;
goto error;
}
if (ioctl(fd, TUNSETIFF, &ifr) < 0)
goto error;
if (!tapfd &&
(errno = ioctl(fd, TUNSETPERSIST, 1)))
goto error;
VIR_FREE(*ifname);
if (!(*ifname = strdup(ifr.ifr_name)))
goto error;
if(tapfd)
*tapfd = fd;
else
VIR_FORCE_CLOSE(fd);
return 0;
error:
VIR_FORCE_CLOSE(fd);
return errno;
}
#endif /* WITH_BRIDGE */ #endif /* WITH_BRIDGE */
...@@ -106,6 +106,15 @@ int brGetEnableSTP (brControl *ctl, ...@@ -106,6 +106,15 @@ int brGetEnableSTP (brControl *ctl,
const char *bridge, const char *bridge,
int *enable); int *enable);
int brCreateTap (brControl *ctl,
char **ifname,
int vnet_hdr,
int *tapfd);
int brSetInterfaceMac (brControl *ctl,
const char *ifname,
const unsigned char *macaddr);
# endif /* WITH_BRIDGE */ # endif /* WITH_BRIDGE */
#endif /* __QEMUD_BRIDGE_H__ */ #endif /* __QEMUD_BRIDGE_H__ */
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册