提交 d1c31023 编写于 作者: L Laine Stump

util: combine bools in virNetDevTapCreateInBridgePort into flags

With an additional new bool added to determine whether or not to
discourage the use of the supplied MAC address by the bridge itself,
virNetDevTapCreateInBridgePort had three booleans (well, 2 bools and
an int used as a bool) in the arg list, which made it increasingly
difficult to follow what was going on. This patch combines those three
into a single flags arg, which not only shortens the arg list, but
makes it more self-documenting.
上级 c1b164d7
......@@ -1766,7 +1766,8 @@ networkStartNetworkVirtual(struct network_driver *driver,
}
if (virNetDevTapCreateInBridgePort(network->def->bridge,
&macTapIfName, network->def->mac,
false, 0, false, NULL, NULL) < 0) {
NULL, NULL,
VIR_NETDEV_TAP_CREATE_USE_MAC_FOR_BRIDGE) < 0) {
VIR_FREE(macTapIfName);
goto err0;
}
......
......@@ -178,7 +178,7 @@ qemuNetworkIfaceConnect(virDomainDefPtr def,
char *brname = NULL;
int err;
int tapfd = -1;
int vnet_hdr = 0;
unsigned int tap_create_flags = VIR_NETDEV_TAP_CREATE_IFUP;
bool template_ifname = false;
int actualType = virDomainNetGetActualType(net);
......@@ -240,12 +240,13 @@ qemuNetworkIfaceConnect(virDomainDefPtr def,
}
if (qemuCapsGet(qemuCaps, QEMU_CAPS_VNET_HDR) &&
net->model && STREQ(net->model, "virtio"))
vnet_hdr = 1;
net->model && STREQ(net->model, "virtio")) {
tap_create_flags |= VIR_NETDEV_TAP_CREATE_VNET_HDR;
}
err = virNetDevTapCreateInBridgePort(brname, &net->ifname, net->mac, true,
vnet_hdr, true, &tapfd,
virDomainNetGetActualVirtPortProfile(net));
err = virNetDevTapCreateInBridgePort(brname, &net->ifname, net->mac, &tapfd,
virDomainNetGetActualVirtPortProfile(net),
tap_create_flags);
virDomainAuditNetDevice(def, net, "/dev/net/tun", tapfd >= 0);
if (err < 0) {
if (template_ifname)
......
/*
* uml_conf.c: UML driver configuration
*
* Copyright (C) 2006-2011 Red Hat, Inc.
* Copyright (C) 2006-2012 Red Hat, Inc.
* Copyright (C) 2006 Daniel P. Berrange
*
* This library is free software; you can redistribute it and/or
......@@ -138,9 +138,9 @@ umlConnectTapDevice(virConnectPtr conn,
template_ifname = true;
}
if (virNetDevTapCreateInBridgePort(bridge, &net->ifname, net->mac, true,
0, true, NULL,
virDomainNetGetActualVirtPortProfile(net)) < 0) {
if (virNetDevTapCreateInBridgePort(bridge, &net->ifname, net->mac, NULL,
virDomainNetGetActualVirtPortProfile(net),
VIR_NETDEV_TAP_CREATE_IFUP) < 0) {
if (template_ifname)
VIR_FREE(net->ifname);
goto error;
......
......@@ -107,22 +107,25 @@ virNetDevProbeVnetHdr(int tapfd)
#ifdef TUNSETIFF
/**
* brCreateTap:
* virNetDevTapCreate:
* @ifname: the interface name
* @vnet_hr: whether to try enabling IFF_VNET_HDR
* @tapfd: file descriptor return value for the new tap device
* @flags: OR of virNetDevTapCreateFlags. Only one flag is recognized:
*
* VIR_NETDEV_TAP_CREATE_VNET_HDR
* - Enable IFF_VNET_HDR on the 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.
* persistent and closed. The caller must use virNetDevTapDelete 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 virNetDevTapCreate(char **ifname,
int vnet_hdr ATTRIBUTE_UNUSED,
int *tapfd)
int *tapfd,
unsigned int flags ATTRIBUTE_UNUSED)
{
int fd;
struct ifreq ifr;
......@@ -139,7 +142,8 @@ int virNetDevTapCreate(char **ifname,
ifr.ifr_flags = IFF_TAP|IFF_NO_PI;
# ifdef IFF_VNET_HDR
if (vnet_hdr && virNetDevProbeVnetHdr(fd))
if ((flags & VIR_NETDEV_TAP_CREATE_VNET_HDR) &&
virNetDevProbeVnetHdr(fd))
ifr.ifr_flags |= IFF_VNET_HDR;
# endif
......@@ -228,8 +232,8 @@ cleanup:
}
#else /* ! TUNSETIFF */
int virNetDevTapCreate(char **ifname ATTRIBUTE_UNUSED,
int vnet_hdr ATTRIBUTE_UNUSED,
int *tapfd ATTRIBUTE_UNUSED)
int *tapfd ATTRIBUTE_UNUSED,
unsigned int flags ATTRIBUTE_UNUSED)
{
virReportSystemError(ENOSYS, "%s",
_("Unable to create TAP devices on this platform"));
......@@ -249,17 +253,23 @@ int virNetDevTapDelete(const char *ifname ATTRIBUTE_UNUSED)
* @brname: the bridge name
* @ifname: the interface name (or name template)
* @macaddr: desired MAC address (VIR_MAC_BUFLEN long)
* @discourage: whether bridge should be discouraged from using macaddr
* @vnet_hdr: whether to try enabling IFF_VNET_HDR
* @tapfd: file descriptor return value for the new tap device
* @virtPortProfile: bridge/port specific configuration
* @flags: OR of virNetDevTapCreateFlags:
* VIR_NETDEV_TAP_CREATE_IFUP
* - Bring the interface up
* VIR_NETDEV_TAP_CREATE_VNET_HDR
* - Enable IFF_VNET_HDR on the tap device
* VIR_NETDEV_TAP_CREATE_USE_MAC_FOR_BRIDGE
* - Set this interface's MAC as the bridge's MAC address
*
* This function creates a new tap device on a bridge. @ifname can be either
* a fixed name or a name template with '%d' for dynamic name allocation.
* in either case the final name for the bridge will be stored in @ifname.
* 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
* persistent and closed. The caller must use virNetDevTapDelete to remove
* a persistent TAP devices when it is no longer needed.
*
* Returns 0 in case of success or -1 on failure
......@@ -267,15 +277,13 @@ int virNetDevTapDelete(const char *ifname ATTRIBUTE_UNUSED)
int virNetDevTapCreateInBridgePort(const char *brname,
char **ifname,
const unsigned char *macaddr,
bool discourage,
int vnet_hdr,
bool up,
int *tapfd,
virNetDevVPortProfilePtr virtPortProfile)
virNetDevVPortProfilePtr virtPortProfile,
unsigned int flags)
{
unsigned char tapmac[VIR_MAC_BUFLEN];
if (virNetDevTapCreate(ifname, vnet_hdr, tapfd) < 0)
if (virNetDevTapCreate(ifname, tapfd, flags) < 0)
return -1;
/* We need to set the interface MAC before adding it
......@@ -285,7 +293,7 @@ int virNetDevTapCreateInBridgePort(const char *brname,
* device before we set our static MAC.
*/
memcpy(tapmac, macaddr, VIR_MAC_BUFLEN);
if (discourage)
if (!(flags & VIR_NETDEV_TAP_CREATE_USE_MAC_FOR_BRIDGE))
tapmac[0] = 0xFE; /* Discourage bridge from using TAP dev MAC */
if (virNetDevSetMAC(*ifname, tapmac) < 0)
......@@ -308,7 +316,7 @@ int virNetDevTapCreateInBridgePort(const char *brname,
goto error;
}
if (virNetDevSetOnline(*ifname, up) < 0)
if (virNetDevSetOnline(*ifname, !!(flags & VIR_NETDEV_TAP_CREATE_IFUP)) < 0)
goto error;
return 0;
......
......@@ -27,21 +27,29 @@
# include "virnetdevvportprofile.h"
int virNetDevTapCreate(char **ifname,
int vnet_hdr,
int *tapfd)
int *tapfd,
unsigned int flags)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
int virNetDevTapDelete(const char *ifname)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
typedef enum {
VIR_NETDEV_TAP_CREATE_NONE = 0,
/* Bring the interface up */
VIR_NETDEV_TAP_CREATE_IFUP = 1 << 0,
/* Enable IFF_VNET_HDR on the tap device */
VIR_NETDEV_TAP_CREATE_VNET_HDR = 1 << 1,
/* Set this interface's MAC as the bridge's MAC address */
VIR_NETDEV_TAP_CREATE_USE_MAC_FOR_BRIDGE = 1 << 2,
} virNetDevTapCreateFlags;
int virNetDevTapCreateInBridgePort(const char *brname,
char **ifname,
const unsigned char *macaddr,
bool discourage,
int vnet_hdr,
bool up,
int *tapfd,
virNetDevVPortProfilePtr virtPortProfile)
virNetDevVPortProfilePtr virtPortProfile,
unsigned int flags)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3)
ATTRIBUTE_RETURN_CHECK;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册