mirror of https://gitee.com/openkylin/libvirt.git
Allow creation of plain macvlan devices
Update virNetDevMacVLanCreateWithVPortProfile to allow creation of plain macvlan devices, as well as macvtap devices. The former is useful for LXC containers * src/qemu/qemu_command.c: Explicitly request a macvtap device * src/util/virnetdevmacvlan.c, src/util/virnetdevmacvlan.h: Add new flag to allow switching between macvlan and macvtap creation
This commit is contained in:
parent
f3b1b9b184
commit
6ec8288a96
|
@ -153,7 +153,7 @@ qemuPhysIfaceConnect(virDomainDefPtr def,
|
||||||
net->ifname, net->mac,
|
net->ifname, net->mac,
|
||||||
virDomainNetGetActualDirectDev(net),
|
virDomainNetGetActualDirectDev(net),
|
||||||
virDomainNetGetActualDirectMode(net),
|
virDomainNetGetActualDirectMode(net),
|
||||||
vnet_hdr, def->uuid,
|
true, vnet_hdr, def->uuid,
|
||||||
virDomainNetGetActualDirectVirtPortProfile(net),
|
virDomainNetGetActualDirectVirtPortProfile(net),
|
||||||
&res_ifname,
|
&res_ifname,
|
||||||
vmop, driver->stateDir,
|
vmop, driver->stateDir,
|
||||||
|
|
|
@ -72,11 +72,14 @@ VIR_ENUM_IMPL(virNetDevMacVLanMode, VIR_NETDEV_MACVLAN_MODE_LAST,
|
||||||
# define MACVTAP_NAME_PREFIX "macvtap"
|
# define MACVTAP_NAME_PREFIX "macvtap"
|
||||||
# define MACVTAP_NAME_PATTERN "macvtap%d"
|
# define MACVTAP_NAME_PATTERN "macvtap%d"
|
||||||
|
|
||||||
|
# define MACVLAN_NAME_PREFIX "macvlan"
|
||||||
|
# define MACVLAN_NAME_PATTERN "macvlan%d"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* virNetDevMacVLanCreate:
|
* virNetDevMacVLanCreate:
|
||||||
*
|
*
|
||||||
* @ifname: The name the interface is supposed to have; optional parameter
|
* @ifname: The name the interface is supposed to have; optional parameter
|
||||||
* @type: The type of device, i.e., "macvtap"
|
* @type: The type of device, i.e., "macvtap", "macvlan"
|
||||||
* @macaddress: The MAC address of the device
|
* @macaddress: The MAC address of the device
|
||||||
* @srcdev: The name of the 'link' device
|
* @srcdev: The name of the 'link' device
|
||||||
* @macvlan_mode: The macvlan mode to use
|
* @macvlan_mode: The macvlan mode to use
|
||||||
|
@ -458,14 +461,14 @@ static const uint32_t modeMap[VIR_NETDEV_MACVLAN_MODE_LAST] = {
|
||||||
* interface will be stored into if everything succeeded. It is up
|
* interface will be stored into if everything succeeded. It is up
|
||||||
* to the caller to free the string.
|
* to the caller to free the string.
|
||||||
*
|
*
|
||||||
* Returns file descriptor of the tap device in case of success,
|
* Returns file descriptor of the tap device in case of success with @withTap,
|
||||||
* negative value otherwise with error reported.
|
* otherwise returns 0; returns -1 on error.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
int virNetDevMacVLanCreateWithVPortProfile(const char *tgifname,
|
int virNetDevMacVLanCreateWithVPortProfile(const char *tgifname,
|
||||||
const unsigned char *macaddress,
|
const unsigned char *macaddress,
|
||||||
const char *linkdev,
|
const char *linkdev,
|
||||||
enum virNetDevMacVLanMode mode,
|
enum virNetDevMacVLanMode mode,
|
||||||
|
bool withTap,
|
||||||
int vnet_hdr,
|
int vnet_hdr,
|
||||||
const unsigned char *vmuuid,
|
const unsigned char *vmuuid,
|
||||||
virNetDevVPortProfilePtr virtPortProfile,
|
virNetDevVPortProfilePtr virtPortProfile,
|
||||||
|
@ -474,7 +477,9 @@ int virNetDevMacVLanCreateWithVPortProfile(const char *tgifname,
|
||||||
char *stateDir,
|
char *stateDir,
|
||||||
virNetDevBandwidthPtr bandwidth)
|
virNetDevBandwidthPtr bandwidth)
|
||||||
{
|
{
|
||||||
const char *type = "macvtap";
|
const char *type = withTap ? "macvtap" : "macvlan";
|
||||||
|
const char *prefix = withTap ? MACVTAP_NAME_PREFIX : MACVLAN_NAME_PREFIX;
|
||||||
|
const char *pattern = withTap ? MACVTAP_NAME_PATTERN : MACVLAN_NAME_PATTERN;
|
||||||
int c, rc;
|
int c, rc;
|
||||||
char ifname[IFNAMSIZ];
|
char ifname[IFNAMSIZ];
|
||||||
int retries, do_retry = 0;
|
int retries, do_retry = 0;
|
||||||
|
@ -505,8 +510,7 @@ int virNetDevMacVLanCreateWithVPortProfile(const char *tgifname,
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if (ret) {
|
if (ret) {
|
||||||
if (STRPREFIX(tgifname,
|
if (STRPREFIX(tgifname, prefix)) {
|
||||||
MACVTAP_NAME_PREFIX)) {
|
|
||||||
goto create_name;
|
goto create_name;
|
||||||
}
|
}
|
||||||
virReportSystemError(EEXIST,
|
virReportSystemError(EEXIST,
|
||||||
|
@ -522,7 +526,7 @@ int virNetDevMacVLanCreateWithVPortProfile(const char *tgifname,
|
||||||
create_name:
|
create_name:
|
||||||
retries = 5;
|
retries = 5;
|
||||||
for (c = 0; c < 8192; c++) {
|
for (c = 0; c < 8192; c++) {
|
||||||
snprintf(ifname, sizeof(ifname), MACVTAP_NAME_PATTERN, c);
|
snprintf(ifname, sizeof(ifname), pattern, c);
|
||||||
if ((ret = virNetDevExists(ifname)) < 0)
|
if ((ret = virNetDevExists(ifname)) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
if (!ret) {
|
if (!ret) {
|
||||||
|
@ -553,15 +557,26 @@ create_name:
|
||||||
goto disassociate_exit;
|
goto disassociate_exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
rc = virNetDevMacVLanTapOpen(cr_ifname, 10);
|
if (withTap) {
|
||||||
if (rc >= 0) {
|
if ((rc = virNetDevMacVLanTapOpen(cr_ifname, 10)) < 0)
|
||||||
|
goto disassociate_exit;
|
||||||
|
|
||||||
if (virNetDevMacVLanTapSetup(rc, vnet_hdr) < 0) {
|
if (virNetDevMacVLanTapSetup(rc, vnet_hdr) < 0) {
|
||||||
VIR_FORCE_CLOSE(rc); /* sets rc to -1 */
|
VIR_FORCE_CLOSE(rc); /* sets rc to -1 */
|
||||||
goto disassociate_exit;
|
goto disassociate_exit;
|
||||||
}
|
}
|
||||||
*res_ifname = strdup(cr_ifname);
|
if (!(*res_ifname = strdup(cr_ifname))) {
|
||||||
} else
|
VIR_FORCE_CLOSE(rc); /* sets rc to -1 */
|
||||||
goto disassociate_exit;
|
virReportOOMError();
|
||||||
|
goto disassociate_exit;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!(*res_ifname = strdup(cr_ifname))) {
|
||||||
|
virReportOOMError();
|
||||||
|
goto disassociate_exit;
|
||||||
|
}
|
||||||
|
rc = 0;
|
||||||
|
}
|
||||||
|
|
||||||
if (virNetDevBandwidthSet(cr_ifname, bandwidth) < 0) {
|
if (virNetDevBandwidthSet(cr_ifname, bandwidth) < 0) {
|
||||||
virNetDevError(VIR_ERR_INTERNAL_ERROR,
|
virNetDevError(VIR_ERR_INTERNAL_ERROR,
|
||||||
|
|
|
@ -55,6 +55,7 @@ int virNetDevMacVLanCreateWithVPortProfile(const char *ifname,
|
||||||
const unsigned char *macaddress,
|
const unsigned char *macaddress,
|
||||||
const char *linkdev,
|
const char *linkdev,
|
||||||
enum virNetDevMacVLanMode mode,
|
enum virNetDevMacVLanMode mode,
|
||||||
|
bool withTap,
|
||||||
int vnet_hdr,
|
int vnet_hdr,
|
||||||
const unsigned char *vmuuid,
|
const unsigned char *vmuuid,
|
||||||
virNetDevVPortProfilePtr virtPortProfile,
|
virNetDevVPortProfilePtr virtPortProfile,
|
||||||
|
@ -62,8 +63,8 @@ int virNetDevMacVLanCreateWithVPortProfile(const char *ifname,
|
||||||
enum virNetDevVPortProfileOp vmop,
|
enum virNetDevVPortProfileOp vmop,
|
||||||
char *stateDir,
|
char *stateDir,
|
||||||
virNetDevBandwidthPtr bandwidth)
|
virNetDevBandwidthPtr bandwidth)
|
||||||
ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(6)
|
ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(7)
|
||||||
ATTRIBUTE_NONNULL(8) ATTRIBUTE_NONNULL(10) ATTRIBUTE_RETURN_CHECK;
|
ATTRIBUTE_NONNULL(9) ATTRIBUTE_NONNULL(11) ATTRIBUTE_RETURN_CHECK;
|
||||||
|
|
||||||
int virNetDevMacVLanDeleteWithVPortProfile(const char *ifname,
|
int virNetDevMacVLanDeleteWithVPortProfile(const char *ifname,
|
||||||
const unsigned char *macaddress,
|
const unsigned char *macaddress,
|
||||||
|
|
Loading…
Reference in New Issue