2011-11-02 17:11:02 +00:00
|
|
|
/*
|
2014-09-03 22:24:43 +00:00
|
|
|
* Copyright (C) 2010-2014 Red Hat, Inc.
|
2012-02-22 13:17:14 +00:00
|
|
|
* Copyright (C) 2010-2012 IBM Corporation
|
2011-11-02 17:11:02 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-07-21 10:06:23 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
2011-11-02 17:11:02 +00:00
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Stefan Berger <stefanb@us.ibm.com>
|
|
|
|
*
|
|
|
|
* Notes:
|
|
|
|
* netlink: http://lovezutto.googlepages.com/netlink.pdf
|
|
|
|
* iproute2 package
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "virnetdevmacvlan.h"
|
2012-01-27 17:23:05 +00:00
|
|
|
#include "virmacaddr.h"
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2013-02-28 14:59:01 +00:00
|
|
|
#include "virthread.h"
|
2013-05-24 07:19:51 +00:00
|
|
|
#include "virstring.h"
|
2011-11-02 17:11:02 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_NET
|
|
|
|
|
|
|
|
VIR_ENUM_IMPL(virNetDevMacVLanMode, VIR_NETDEV_MACVLAN_MODE_LAST,
|
|
|
|
"vepa",
|
|
|
|
"private",
|
|
|
|
"bridge",
|
|
|
|
"passthrough")
|
|
|
|
|
|
|
|
#if WITH_MACVTAP
|
|
|
|
# include <stdint.h>
|
|
|
|
# include <stdio.h>
|
|
|
|
# include <errno.h>
|
|
|
|
# include <fcntl.h>
|
|
|
|
# include <sys/socket.h>
|
|
|
|
# include <sys/ioctl.h>
|
|
|
|
|
2013-06-13 06:26:22 +00:00
|
|
|
# include <net/if.h>
|
2011-11-02 17:11:02 +00:00
|
|
|
# include <linux/if_tun.h>
|
|
|
|
|
|
|
|
/* Older kernels lacked this enum value. */
|
|
|
|
# if !HAVE_DECL_MACVLAN_MODE_PASSTHRU
|
|
|
|
# define MACVLAN_MODE_PASSTHRU 8
|
|
|
|
# endif
|
|
|
|
|
2012-12-12 18:06:53 +00:00
|
|
|
# include "viralloc.h"
|
2012-12-12 17:59:27 +00:00
|
|
|
# include "virlog.h"
|
2012-12-13 18:01:25 +00:00
|
|
|
# include "viruuid.h"
|
2011-11-02 17:11:02 +00:00
|
|
|
# include "virfile.h"
|
2012-02-03 14:13:19 +00:00
|
|
|
# include "virnetlink.h"
|
2011-11-02 17:11:02 +00:00
|
|
|
# include "virnetdev.h"
|
2012-02-22 13:17:14 +00:00
|
|
|
# include "virpidfile.h"
|
|
|
|
|
2014-02-28 12:16:17 +00:00
|
|
|
VIR_LOG_INIT("util.netdevmacvlan");
|
2011-11-02 17:11:02 +00:00
|
|
|
|
|
|
|
# define MACVTAP_NAME_PREFIX "macvtap"
|
|
|
|
# define MACVTAP_NAME_PATTERN "macvtap%d"
|
|
|
|
|
2011-11-10 10:29:09 +00:00
|
|
|
# define MACVLAN_NAME_PREFIX "macvlan"
|
|
|
|
# define MACVLAN_NAME_PATTERN "macvlan%d"
|
|
|
|
|
2014-03-25 14:54:44 +00:00
|
|
|
virMutex virNetDevMacVLanCreateMutex = VIR_MUTEX_INITIALIZER;
|
2013-02-28 14:59:01 +00:00
|
|
|
|
2011-11-02 17:34:41 +00:00
|
|
|
/**
|
|
|
|
* virNetDevMacVLanCreate:
|
|
|
|
*
|
|
|
|
* @ifname: The name the interface is supposed to have; optional parameter
|
2011-11-10 10:29:09 +00:00
|
|
|
* @type: The type of device, i.e., "macvtap", "macvlan"
|
2011-11-02 17:34:41 +00:00
|
|
|
* @macaddress: The MAC address of the device
|
|
|
|
* @srcdev: The name of the 'link' device
|
|
|
|
* @macvlan_mode: The macvlan mode to use
|
|
|
|
* @retry: Pointer to integer that will be '1' upon return if an interface
|
|
|
|
* with the same name already exists and it is worth to try
|
|
|
|
* again with a different name
|
|
|
|
*
|
|
|
|
* Create a macvtap device with the given properties.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on fatal error.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virNetDevMacVLanCreate(const char *ifname,
|
|
|
|
const char *type,
|
2013-10-05 19:41:44 +00:00
|
|
|
const virMacAddr *macaddress,
|
2011-11-02 17:34:41 +00:00
|
|
|
const char *srcdev,
|
|
|
|
uint32_t macvlan_mode,
|
|
|
|
int *retry)
|
|
|
|
{
|
util: standardize return from functions calling virNetlinkCommand
There are several functions that call virNetlinkCommand, and they all
follow a common pattern, with three exit labels: err_exit (or
cleanup), malformed_resp, and buffer_too_small. All three of these
labels do their own cleanup and have their own return. However, the
malformed_resp label usually frees the same items as the
cleanup/err_exit label, and the buffer_too_small label just doesn't
free recvbuf (because it's known to always be NULL at the time we goto
buffer_too_small.
In order to simplify and standardize the code, I've made the following
changes to all of these functions:
1) err_exit is replaced with the more libvirt-ish "cleanup", which
makes sense because in all cases this code is also executed in the
case of success, so labelling it err_exit may be confusing.
2) rc is initialized to -1, and set to 0 just before the cleanup
label. Any code that currently sets rc = -1 is made to instead goto
cleanup.
3) malformed_resp and buffer_too_small just log their error and goto
cleanup. This gives us a single return path, and a single place to
free up resources.
4) In one instance, rather then logging an error immediately, a char*
msg was pointed to an error string, then goto cleanup (and cleanup
would log an error if msg != NULL). It takes no more lines of code
to just log the message as we encounter it.
This patch should have 0 functional effects.
2012-03-07 17:44:56 +00:00
|
|
|
int rc = -1;
|
2013-04-03 13:09:19 +00:00
|
|
|
struct nlmsghdr *resp = NULL;
|
2011-11-02 17:34:41 +00:00
|
|
|
struct nlmsgerr *err;
|
|
|
|
struct ifinfomsg ifinfo = { .ifi_family = AF_UNSPEC };
|
|
|
|
int ifindex;
|
|
|
|
unsigned int recvbuflen;
|
|
|
|
struct nl_msg *nl_msg;
|
|
|
|
struct nlattr *linkinfo, *info_data;
|
|
|
|
|
2011-11-03 09:21:35 +00:00
|
|
|
if (virNetDevGetIndex(srcdev, &ifindex) < 0)
|
2011-11-02 17:34:41 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
*retry = 0;
|
|
|
|
|
|
|
|
nl_msg = nlmsg_alloc_simple(RTM_NEWLINK,
|
|
|
|
NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL);
|
|
|
|
if (!nl_msg) {
|
|
|
|
virReportOOMError();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nlmsg_append(nl_msg, &ifinfo, sizeof(ifinfo), NLMSG_ALIGNTO) < 0)
|
|
|
|
goto buffer_too_small;
|
|
|
|
|
|
|
|
if (nla_put_u32(nl_msg, IFLA_LINK, ifindex) < 0)
|
|
|
|
goto buffer_too_small;
|
|
|
|
|
|
|
|
if (nla_put(nl_msg, IFLA_ADDRESS, VIR_MAC_BUFLEN, macaddress) < 0)
|
|
|
|
goto buffer_too_small;
|
|
|
|
|
|
|
|
if (ifname &&
|
|
|
|
nla_put(nl_msg, IFLA_IFNAME, strlen(ifname)+1, ifname) < 0)
|
|
|
|
goto buffer_too_small;
|
|
|
|
|
|
|
|
if (!(linkinfo = nla_nest_start(nl_msg, IFLA_LINKINFO)))
|
|
|
|
goto buffer_too_small;
|
|
|
|
|
|
|
|
if (nla_put(nl_msg, IFLA_INFO_KIND, strlen(type), type) < 0)
|
|
|
|
goto buffer_too_small;
|
|
|
|
|
|
|
|
if (macvlan_mode > 0) {
|
|
|
|
if (!(info_data = nla_nest_start(nl_msg, IFLA_INFO_DATA)))
|
|
|
|
goto buffer_too_small;
|
|
|
|
|
|
|
|
if (nla_put(nl_msg, IFLA_MACVLAN_MODE, sizeof(macvlan_mode),
|
|
|
|
&macvlan_mode) < 0)
|
|
|
|
goto buffer_too_small;
|
|
|
|
|
|
|
|
nla_nest_end(nl_msg, info_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
nla_nest_end(nl_msg, linkinfo);
|
|
|
|
|
2013-04-03 13:09:19 +00:00
|
|
|
if (virNetlinkCommand(nl_msg, &resp, &recvbuflen, 0, 0,
|
2012-08-22 04:10:23 +00:00
|
|
|
NETLINK_ROUTE, 0) < 0) {
|
2011-11-02 17:34:41 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2013-04-03 13:09:19 +00:00
|
|
|
if (recvbuflen < NLMSG_LENGTH(0) || resp == NULL)
|
2011-11-02 17:34:41 +00:00
|
|
|
goto malformed_resp;
|
|
|
|
|
|
|
|
switch (resp->nlmsg_type) {
|
|
|
|
case NLMSG_ERROR:
|
|
|
|
err = (struct nlmsgerr *)NLMSG_DATA(resp);
|
|
|
|
if (resp->nlmsg_len < NLMSG_LENGTH(sizeof(*err)))
|
|
|
|
goto malformed_resp;
|
|
|
|
|
|
|
|
switch (err->error) {
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case -EEXIST:
|
|
|
|
*retry = 1;
|
util: standardize return from functions calling virNetlinkCommand
There are several functions that call virNetlinkCommand, and they all
follow a common pattern, with three exit labels: err_exit (or
cleanup), malformed_resp, and buffer_too_small. All three of these
labels do their own cleanup and have their own return. However, the
malformed_resp label usually frees the same items as the
cleanup/err_exit label, and the buffer_too_small label just doesn't
free recvbuf (because it's known to always be NULL at the time we goto
buffer_too_small.
In order to simplify and standardize the code, I've made the following
changes to all of these functions:
1) err_exit is replaced with the more libvirt-ish "cleanup", which
makes sense because in all cases this code is also executed in the
case of success, so labelling it err_exit may be confusing.
2) rc is initialized to -1, and set to 0 just before the cleanup
label. Any code that currently sets rc = -1 is made to instead goto
cleanup.
3) malformed_resp and buffer_too_small just log their error and goto
cleanup. This gives us a single return path, and a single place to
free up resources.
4) In one instance, rather then logging an error immediately, a char*
msg was pointed to an error string, then goto cleanup (and cleanup
would log an error if msg != NULL). It takes no more lines of code
to just log the message as we encounter it.
This patch should have 0 functional effects.
2012-03-07 17:44:56 +00:00
|
|
|
goto cleanup;
|
2011-11-02 17:34:41 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
virReportSystemError(-err->error,
|
2012-11-23 16:34:10 +00:00
|
|
|
_("error creating %s type of interface attach to %s"),
|
|
|
|
type, srcdev);
|
util: standardize return from functions calling virNetlinkCommand
There are several functions that call virNetlinkCommand, and they all
follow a common pattern, with three exit labels: err_exit (or
cleanup), malformed_resp, and buffer_too_small. All three of these
labels do their own cleanup and have their own return. However, the
malformed_resp label usually frees the same items as the
cleanup/err_exit label, and the buffer_too_small label just doesn't
free recvbuf (because it's known to always be NULL at the time we goto
buffer_too_small.
In order to simplify and standardize the code, I've made the following
changes to all of these functions:
1) err_exit is replaced with the more libvirt-ish "cleanup", which
makes sense because in all cases this code is also executed in the
case of success, so labelling it err_exit may be confusing.
2) rc is initialized to -1, and set to 0 just before the cleanup
label. Any code that currently sets rc = -1 is made to instead goto
cleanup.
3) malformed_resp and buffer_too_small just log their error and goto
cleanup. This gives us a single return path, and a single place to
free up resources.
4) In one instance, rather then logging an error immediately, a char*
msg was pointed to an error string, then goto cleanup (and cleanup
would log an error if msg != NULL). It takes no more lines of code
to just log the message as we encounter it.
This patch should have 0 functional effects.
2012-03-07 17:44:56 +00:00
|
|
|
goto cleanup;
|
2011-11-02 17:34:41 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NLMSG_DONE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
goto malformed_resp;
|
|
|
|
}
|
|
|
|
|
util: standardize return from functions calling virNetlinkCommand
There are several functions that call virNetlinkCommand, and they all
follow a common pattern, with three exit labels: err_exit (or
cleanup), malformed_resp, and buffer_too_small. All three of these
labels do their own cleanup and have their own return. However, the
malformed_resp label usually frees the same items as the
cleanup/err_exit label, and the buffer_too_small label just doesn't
free recvbuf (because it's known to always be NULL at the time we goto
buffer_too_small.
In order to simplify and standardize the code, I've made the following
changes to all of these functions:
1) err_exit is replaced with the more libvirt-ish "cleanup", which
makes sense because in all cases this code is also executed in the
case of success, so labelling it err_exit may be confusing.
2) rc is initialized to -1, and set to 0 just before the cleanup
label. Any code that currently sets rc = -1 is made to instead goto
cleanup.
3) malformed_resp and buffer_too_small just log their error and goto
cleanup. This gives us a single return path, and a single place to
free up resources.
4) In one instance, rather then logging an error immediately, a char*
msg was pointed to an error string, then goto cleanup (and cleanup
would log an error if msg != NULL). It takes no more lines of code
to just log the message as we encounter it.
This patch should have 0 functional effects.
2012-03-07 17:44:56 +00:00
|
|
|
rc = 0;
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2011-11-02 17:34:41 +00:00
|
|
|
nlmsg_free(nl_msg);
|
2013-04-03 13:09:19 +00:00
|
|
|
VIR_FREE(resp);
|
2011-11-02 17:34:41 +00:00
|
|
|
return rc;
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
malformed_resp:
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
2011-11-02 17:34:41 +00:00
|
|
|
_("malformed netlink response message"));
|
util: standardize return from functions calling virNetlinkCommand
There are several functions that call virNetlinkCommand, and they all
follow a common pattern, with three exit labels: err_exit (or
cleanup), malformed_resp, and buffer_too_small. All three of these
labels do their own cleanup and have their own return. However, the
malformed_resp label usually frees the same items as the
cleanup/err_exit label, and the buffer_too_small label just doesn't
free recvbuf (because it's known to always be NULL at the time we goto
buffer_too_small.
In order to simplify and standardize the code, I've made the following
changes to all of these functions:
1) err_exit is replaced with the more libvirt-ish "cleanup", which
makes sense because in all cases this code is also executed in the
case of success, so labelling it err_exit may be confusing.
2) rc is initialized to -1, and set to 0 just before the cleanup
label. Any code that currently sets rc = -1 is made to instead goto
cleanup.
3) malformed_resp and buffer_too_small just log their error and goto
cleanup. This gives us a single return path, and a single place to
free up resources.
4) In one instance, rather then logging an error immediately, a char*
msg was pointed to an error string, then goto cleanup (and cleanup
would log an error if msg != NULL). It takes no more lines of code
to just log the message as we encounter it.
This patch should have 0 functional effects.
2012-03-07 17:44:56 +00:00
|
|
|
goto cleanup;
|
2011-11-02 17:34:41 +00:00
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
buffer_too_small:
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
2011-11-02 17:34:41 +00:00
|
|
|
_("allocated netlink buffer is too small"));
|
util: standardize return from functions calling virNetlinkCommand
There are several functions that call virNetlinkCommand, and they all
follow a common pattern, with three exit labels: err_exit (or
cleanup), malformed_resp, and buffer_too_small. All three of these
labels do their own cleanup and have their own return. However, the
malformed_resp label usually frees the same items as the
cleanup/err_exit label, and the buffer_too_small label just doesn't
free recvbuf (because it's known to always be NULL at the time we goto
buffer_too_small.
In order to simplify and standardize the code, I've made the following
changes to all of these functions:
1) err_exit is replaced with the more libvirt-ish "cleanup", which
makes sense because in all cases this code is also executed in the
case of success, so labelling it err_exit may be confusing.
2) rc is initialized to -1, and set to 0 just before the cleanup
label. Any code that currently sets rc = -1 is made to instead goto
cleanup.
3) malformed_resp and buffer_too_small just log their error and goto
cleanup. This gives us a single return path, and a single place to
free up resources.
4) In one instance, rather then logging an error immediately, a char*
msg was pointed to an error string, then goto cleanup (and cleanup
would log an error if msg != NULL). It takes no more lines of code
to just log the message as we encounter it.
This patch should have 0 functional effects.
2012-03-07 17:44:56 +00:00
|
|
|
goto cleanup;
|
2011-11-02 17:34:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virNetDevMacVLanDelete:
|
|
|
|
*
|
|
|
|
* @ifname: Name of the interface
|
|
|
|
*
|
|
|
|
* Tear down an interface with the given name.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on fatal error.
|
|
|
|
*/
|
|
|
|
int virNetDevMacVLanDelete(const char *ifname)
|
|
|
|
{
|
util: standardize return from functions calling virNetlinkCommand
There are several functions that call virNetlinkCommand, and they all
follow a common pattern, with three exit labels: err_exit (or
cleanup), malformed_resp, and buffer_too_small. All three of these
labels do their own cleanup and have their own return. However, the
malformed_resp label usually frees the same items as the
cleanup/err_exit label, and the buffer_too_small label just doesn't
free recvbuf (because it's known to always be NULL at the time we goto
buffer_too_small.
In order to simplify and standardize the code, I've made the following
changes to all of these functions:
1) err_exit is replaced with the more libvirt-ish "cleanup", which
makes sense because in all cases this code is also executed in the
case of success, so labelling it err_exit may be confusing.
2) rc is initialized to -1, and set to 0 just before the cleanup
label. Any code that currently sets rc = -1 is made to instead goto
cleanup.
3) malformed_resp and buffer_too_small just log their error and goto
cleanup. This gives us a single return path, and a single place to
free up resources.
4) In one instance, rather then logging an error immediately, a char*
msg was pointed to an error string, then goto cleanup (and cleanup
would log an error if msg != NULL). It takes no more lines of code
to just log the message as we encounter it.
This patch should have 0 functional effects.
2012-03-07 17:44:56 +00:00
|
|
|
int rc = -1;
|
2013-04-03 13:09:19 +00:00
|
|
|
struct nlmsghdr *resp = NULL;
|
2011-11-02 17:34:41 +00:00
|
|
|
struct nlmsgerr *err;
|
|
|
|
struct ifinfomsg ifinfo = { .ifi_family = AF_UNSPEC };
|
|
|
|
unsigned int recvbuflen;
|
|
|
|
struct nl_msg *nl_msg;
|
|
|
|
|
|
|
|
nl_msg = nlmsg_alloc_simple(RTM_DELLINK,
|
|
|
|
NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL);
|
|
|
|
if (!nl_msg) {
|
|
|
|
virReportOOMError();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nlmsg_append(nl_msg, &ifinfo, sizeof(ifinfo), NLMSG_ALIGNTO) < 0)
|
|
|
|
goto buffer_too_small;
|
|
|
|
|
|
|
|
if (nla_put(nl_msg, IFLA_IFNAME, strlen(ifname)+1, ifname) < 0)
|
|
|
|
goto buffer_too_small;
|
|
|
|
|
2013-04-03 13:09:19 +00:00
|
|
|
if (virNetlinkCommand(nl_msg, &resp, &recvbuflen, 0, 0,
|
2012-08-22 04:10:23 +00:00
|
|
|
NETLINK_ROUTE, 0) < 0) {
|
2011-11-02 17:34:41 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2013-04-03 13:09:19 +00:00
|
|
|
if (recvbuflen < NLMSG_LENGTH(0) || resp == NULL)
|
2011-11-02 17:34:41 +00:00
|
|
|
goto malformed_resp;
|
|
|
|
|
|
|
|
switch (resp->nlmsg_type) {
|
|
|
|
case NLMSG_ERROR:
|
|
|
|
err = (struct nlmsgerr *)NLMSG_DATA(resp);
|
|
|
|
if (resp->nlmsg_len < NLMSG_LENGTH(sizeof(*err)))
|
|
|
|
goto malformed_resp;
|
|
|
|
|
|
|
|
if (err->error) {
|
|
|
|
virReportSystemError(-err->error,
|
|
|
|
_("error destroying %s interface"),
|
|
|
|
ifname);
|
util: standardize return from functions calling virNetlinkCommand
There are several functions that call virNetlinkCommand, and they all
follow a common pattern, with three exit labels: err_exit (or
cleanup), malformed_resp, and buffer_too_small. All three of these
labels do their own cleanup and have their own return. However, the
malformed_resp label usually frees the same items as the
cleanup/err_exit label, and the buffer_too_small label just doesn't
free recvbuf (because it's known to always be NULL at the time we goto
buffer_too_small.
In order to simplify and standardize the code, I've made the following
changes to all of these functions:
1) err_exit is replaced with the more libvirt-ish "cleanup", which
makes sense because in all cases this code is also executed in the
case of success, so labelling it err_exit may be confusing.
2) rc is initialized to -1, and set to 0 just before the cleanup
label. Any code that currently sets rc = -1 is made to instead goto
cleanup.
3) malformed_resp and buffer_too_small just log their error and goto
cleanup. This gives us a single return path, and a single place to
free up resources.
4) In one instance, rather then logging an error immediately, a char*
msg was pointed to an error string, then goto cleanup (and cleanup
would log an error if msg != NULL). It takes no more lines of code
to just log the message as we encounter it.
This patch should have 0 functional effects.
2012-03-07 17:44:56 +00:00
|
|
|
goto cleanup;
|
2011-11-02 17:34:41 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NLMSG_DONE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
goto malformed_resp;
|
|
|
|
}
|
|
|
|
|
util: standardize return from functions calling virNetlinkCommand
There are several functions that call virNetlinkCommand, and they all
follow a common pattern, with three exit labels: err_exit (or
cleanup), malformed_resp, and buffer_too_small. All three of these
labels do their own cleanup and have their own return. However, the
malformed_resp label usually frees the same items as the
cleanup/err_exit label, and the buffer_too_small label just doesn't
free recvbuf (because it's known to always be NULL at the time we goto
buffer_too_small.
In order to simplify and standardize the code, I've made the following
changes to all of these functions:
1) err_exit is replaced with the more libvirt-ish "cleanup", which
makes sense because in all cases this code is also executed in the
case of success, so labelling it err_exit may be confusing.
2) rc is initialized to -1, and set to 0 just before the cleanup
label. Any code that currently sets rc = -1 is made to instead goto
cleanup.
3) malformed_resp and buffer_too_small just log their error and goto
cleanup. This gives us a single return path, and a single place to
free up resources.
4) In one instance, rather then logging an error immediately, a char*
msg was pointed to an error string, then goto cleanup (and cleanup
would log an error if msg != NULL). It takes no more lines of code
to just log the message as we encounter it.
This patch should have 0 functional effects.
2012-03-07 17:44:56 +00:00
|
|
|
rc = 0;
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2011-11-02 17:34:41 +00:00
|
|
|
nlmsg_free(nl_msg);
|
2013-04-03 13:09:19 +00:00
|
|
|
VIR_FREE(resp);
|
2011-11-02 17:34:41 +00:00
|
|
|
return rc;
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
malformed_resp:
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
2011-11-02 17:34:41 +00:00
|
|
|
_("malformed netlink response message"));
|
util: standardize return from functions calling virNetlinkCommand
There are several functions that call virNetlinkCommand, and they all
follow a common pattern, with three exit labels: err_exit (or
cleanup), malformed_resp, and buffer_too_small. All three of these
labels do their own cleanup and have their own return. However, the
malformed_resp label usually frees the same items as the
cleanup/err_exit label, and the buffer_too_small label just doesn't
free recvbuf (because it's known to always be NULL at the time we goto
buffer_too_small.
In order to simplify and standardize the code, I've made the following
changes to all of these functions:
1) err_exit is replaced with the more libvirt-ish "cleanup", which
makes sense because in all cases this code is also executed in the
case of success, so labelling it err_exit may be confusing.
2) rc is initialized to -1, and set to 0 just before the cleanup
label. Any code that currently sets rc = -1 is made to instead goto
cleanup.
3) malformed_resp and buffer_too_small just log their error and goto
cleanup. This gives us a single return path, and a single place to
free up resources.
4) In one instance, rather then logging an error immediately, a char*
msg was pointed to an error string, then goto cleanup (and cleanup
would log an error if msg != NULL). It takes no more lines of code
to just log the message as we encounter it.
This patch should have 0 functional effects.
2012-03-07 17:44:56 +00:00
|
|
|
goto cleanup;
|
2011-11-02 17:34:41 +00:00
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
buffer_too_small:
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
2011-11-02 17:34:41 +00:00
|
|
|
_("allocated netlink buffer is too small"));
|
util: standardize return from functions calling virNetlinkCommand
There are several functions that call virNetlinkCommand, and they all
follow a common pattern, with three exit labels: err_exit (or
cleanup), malformed_resp, and buffer_too_small. All three of these
labels do their own cleanup and have their own return. However, the
malformed_resp label usually frees the same items as the
cleanup/err_exit label, and the buffer_too_small label just doesn't
free recvbuf (because it's known to always be NULL at the time we goto
buffer_too_small.
In order to simplify and standardize the code, I've made the following
changes to all of these functions:
1) err_exit is replaced with the more libvirt-ish "cleanup", which
makes sense because in all cases this code is also executed in the
case of success, so labelling it err_exit may be confusing.
2) rc is initialized to -1, and set to 0 just before the cleanup
label. Any code that currently sets rc = -1 is made to instead goto
cleanup.
3) malformed_resp and buffer_too_small just log their error and goto
cleanup. This gives us a single return path, and a single place to
free up resources.
4) In one instance, rather then logging an error immediately, a char*
msg was pointed to an error string, then goto cleanup (and cleanup
would log an error if msg != NULL). It takes no more lines of code
to just log the message as we encounter it.
This patch should have 0 functional effects.
2012-03-07 17:44:56 +00:00
|
|
|
goto cleanup;
|
2011-11-02 17:34:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-02 17:11:02 +00:00
|
|
|
/**
|
|
|
|
* virNetDevMacVLanTapOpen:
|
|
|
|
* Open the macvtap's tap device.
|
|
|
|
* @ifname: Name of the macvtap interface
|
|
|
|
* @retries : Number of retries in case udev for example may need to be
|
|
|
|
* waited for to create the tap chardev
|
|
|
|
* Returns negative value in case of error, the file descriptor otherwise.
|
|
|
|
*/
|
|
|
|
static
|
|
|
|
int virNetDevMacVLanTapOpen(const char *ifname,
|
|
|
|
int retries)
|
|
|
|
{
|
|
|
|
FILE *file;
|
|
|
|
char path[64];
|
|
|
|
int ifindex;
|
|
|
|
char tapname[50];
|
|
|
|
int tapfd;
|
|
|
|
|
|
|
|
if (snprintf(path, sizeof(path),
|
|
|
|
"/sys/class/net/%s/ifindex", ifname) >= sizeof(path)) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
"%s",
|
|
|
|
_("buffer for ifindex path is too small"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
file = fopen(path, "r");
|
|
|
|
|
|
|
|
if (!file) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("cannot open macvtap file %s to determine "
|
|
|
|
"interface index"), path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fscanf(file, "%d", &ifindex) != 1) {
|
|
|
|
virReportSystemError(errno,
|
2013-11-19 23:00:32 +00:00
|
|
|
"%s", _("cannot determine macvtap's tap device "
|
2011-11-02 17:11:02 +00:00
|
|
|
"interface index"));
|
|
|
|
VIR_FORCE_FCLOSE(file);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_FORCE_FCLOSE(file);
|
|
|
|
|
|
|
|
if (snprintf(tapname, sizeof(tapname),
|
|
|
|
"/dev/tap%d", ifindex) >= sizeof(tapname)) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
"%s",
|
|
|
|
_("internal buffer for tap device is too small"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
/* may need to wait for udev to be done */
|
|
|
|
tapfd = open(tapname, O_RDWR);
|
|
|
|
if (tapfd < 0 && retries > 0) {
|
|
|
|
retries--;
|
|
|
|
usleep(20000);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tapfd < 0)
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("cannot open macvtap tap device %s"),
|
|
|
|
tapname);
|
|
|
|
|
|
|
|
return tapfd;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virNetDevMacVLanTapSetup:
|
|
|
|
* @tapfd: file descriptor of the macvtap tap
|
|
|
|
* @vnet_hdr: 1 to enable IFF_VNET_HDR, 0 to disable it
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 in case of fatal error, error code otherwise.
|
|
|
|
*
|
|
|
|
* Turn the IFF_VNET_HDR flag, if requested and available, make sure
|
|
|
|
* it's off in the other cases.
|
|
|
|
* A fatal error is defined as the VNET_HDR flag being set but it cannot
|
|
|
|
* be turned off for some reason. This is reported with -1. Other fatal
|
|
|
|
* error is not being able to read the interface flags. In that case the
|
|
|
|
* macvtap device should not be used.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
virNetDevMacVLanTapSetup(int tapfd, int vnet_hdr)
|
|
|
|
{
|
|
|
|
unsigned int features;
|
|
|
|
struct ifreq ifreq;
|
|
|
|
short new_flags = 0;
|
|
|
|
int rc_on_fail = 0;
|
|
|
|
const char *errmsg = NULL;
|
|
|
|
|
|
|
|
memset(&ifreq, 0, sizeof(ifreq));
|
|
|
|
|
|
|
|
if (ioctl(tapfd, TUNGETIFF, &ifreq) < 0) {
|
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("cannot get interface flags on macvtap tap"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_flags = ifreq.ifr_flags;
|
|
|
|
|
|
|
|
if ((ifreq.ifr_flags & IFF_VNET_HDR) && !vnet_hdr) {
|
|
|
|
new_flags = ifreq.ifr_flags & ~IFF_VNET_HDR;
|
|
|
|
rc_on_fail = -1;
|
|
|
|
errmsg = _("cannot clean IFF_VNET_HDR flag on macvtap tap");
|
|
|
|
} else if ((ifreq.ifr_flags & IFF_VNET_HDR) == 0 && vnet_hdr) {
|
|
|
|
if (ioctl(tapfd, TUNGETFEATURES, &features) < 0) {
|
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("cannot get feature flags on macvtap tap"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ((features & IFF_VNET_HDR)) {
|
|
|
|
new_flags = ifreq.ifr_flags | IFF_VNET_HDR;
|
|
|
|
errmsg = _("cannot set IFF_VNET_HDR flag on macvtap tap");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_flags != ifreq.ifr_flags) {
|
|
|
|
ifreq.ifr_flags = new_flags;
|
|
|
|
if (ioctl(tapfd, TUNSETIFF, &ifreq) < 0) {
|
|
|
|
virReportSystemError(errno, "%s", errmsg);
|
|
|
|
return rc_on_fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const uint32_t modeMap[VIR_NETDEV_MACVLAN_MODE_LAST] = {
|
|
|
|
[VIR_NETDEV_MACVLAN_MODE_VEPA] = MACVLAN_MODE_VEPA,
|
|
|
|
[VIR_NETDEV_MACVLAN_MODE_PRIVATE] = MACVLAN_MODE_PRIVATE,
|
|
|
|
[VIR_NETDEV_MACVLAN_MODE_BRIDGE] = MACVLAN_MODE_BRIDGE,
|
|
|
|
[VIR_NETDEV_MACVLAN_MODE_PASSTHRU] = MACVLAN_MODE_PASSTHRU,
|
|
|
|
};
|
|
|
|
|
2012-02-22 13:17:14 +00:00
|
|
|
/* Struct to hold the state and configuration of a 802.1qbg port */
|
|
|
|
struct virNetlinkCallbackData {
|
|
|
|
char *cr_ifname;
|
|
|
|
virNetDevVPortProfilePtr virtPortProfile;
|
2012-07-17 12:07:59 +00:00
|
|
|
virMacAddr macaddress;
|
2012-02-22 13:17:14 +00:00
|
|
|
char *linkdev;
|
2012-03-06 01:12:39 +00:00
|
|
|
int vf;
|
2012-04-25 11:55:07 +00:00
|
|
|
unsigned char vmuuid[VIR_UUID_BUFLEN];
|
2014-04-27 00:15:22 +00:00
|
|
|
virNetDevVPortProfileOp vmOp;
|
2012-02-22 13:17:14 +00:00
|
|
|
unsigned int linkState;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct virNetlinkCallbackData *virNetlinkCallbackDataPtr;
|
|
|
|
|
|
|
|
# define INSTANCE_STRLEN 36
|
|
|
|
|
|
|
|
static int instance2str(const unsigned char *p, char *dst, size_t size)
|
|
|
|
{
|
|
|
|
if (dst && size > INSTANCE_STRLEN) {
|
|
|
|
snprintf(dst, size, "%02x%02x%02x%02x-%02x%02x-%02x%02x-"
|
|
|
|
"%02x%02x-%02x%02x%02x%02x%02x%02x",
|
|
|
|
p[0], p[1], p[2], p[3],
|
|
|
|
p[4], p[5], p[6], p[7],
|
|
|
|
p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
# define LLDPAD_PID_FILE "/var/run/lldpad.pid"
|
|
|
|
# define VIRIP_PID_FILE "/var/run/virip.pid"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virNetDevMacVLanVPortProfileCallback:
|
|
|
|
*
|
2013-04-03 13:09:19 +00:00
|
|
|
* @hdr: The buffer containing the received netlink header + payload
|
2012-02-22 13:17:14 +00:00
|
|
|
* @length: The length of the received netlink message.
|
|
|
|
* @peer: The netling sockaddr containing the peer information
|
|
|
|
* @handled: Contains information if the message has been replied to yet
|
|
|
|
* @opaque: Contains vital information regarding the associated vm an interface
|
|
|
|
*
|
|
|
|
* This function is called when a netlink message is received. The function
|
|
|
|
* reads the message and responds if it is pertinent to the running VMs
|
|
|
|
* network interface.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
2013-04-03 13:09:19 +00:00
|
|
|
virNetDevMacVLanVPortProfileCallback(struct nlmsghdr *hdr,
|
|
|
|
unsigned int length,
|
2012-02-22 13:17:14 +00:00
|
|
|
struct sockaddr_nl *peer,
|
|
|
|
bool *handled,
|
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
struct nla_policy ifla_vf_policy[IFLA_VF_MAX + 1] = {
|
|
|
|
[IFLA_VF_MAC] = {.minlen = sizeof(struct ifla_vf_mac),
|
|
|
|
.maxlen = sizeof(struct ifla_vf_mac)},
|
|
|
|
[IFLA_VF_VLAN] = {.minlen = sizeof(struct ifla_vf_vlan),
|
|
|
|
.maxlen = sizeof(struct ifla_vf_vlan)},
|
|
|
|
};
|
|
|
|
|
|
|
|
struct nla_policy ifla_port_policy[IFLA_PORT_MAX + 1] = {
|
|
|
|
[IFLA_PORT_RESPONSE] = {.type = NLA_U16},
|
|
|
|
};
|
|
|
|
|
|
|
|
struct nlattr *tb[IFLA_MAX + 1], *tb3[IFLA_PORT_MAX + 1],
|
|
|
|
*tb_vfinfo[IFLA_VF_MAX + 1], *tb_vfinfo_list;
|
|
|
|
|
|
|
|
struct ifinfomsg ifinfo;
|
|
|
|
void *data;
|
|
|
|
int rem;
|
|
|
|
char *ifname;
|
|
|
|
bool indicate = false;
|
|
|
|
virNetlinkCallbackDataPtr calld = opaque;
|
|
|
|
pid_t lldpad_pid = 0;
|
|
|
|
pid_t virip_pid = 0;
|
2013-03-26 11:21:33 +00:00
|
|
|
char macaddr[VIR_MAC_STRING_BUFLEN];
|
2012-02-22 13:17:14 +00:00
|
|
|
|
|
|
|
data = nlmsg_data(hdr);
|
|
|
|
|
|
|
|
/* Quickly decide if we want this or not */
|
|
|
|
|
|
|
|
if (virPidFileReadPath(LLDPAD_PID_FILE, &lldpad_pid) < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ignore_value(virPidFileReadPath(VIRIP_PID_FILE, &virip_pid));
|
|
|
|
|
|
|
|
if (hdr->nlmsg_pid != lldpad_pid && hdr->nlmsg_pid != virip_pid)
|
|
|
|
return; /* we only care for lldpad and virip messages */
|
|
|
|
if (hdr->nlmsg_type != RTM_SETLINK)
|
|
|
|
return; /* we only care for RTM_SETLINK */
|
|
|
|
if (*handled)
|
|
|
|
return; /* if it has been handled - dont handle again */
|
|
|
|
|
|
|
|
/* DEBUG start */
|
|
|
|
VIR_INFO("netlink message nl_sockaddr: %p len: %d", peer, length);
|
|
|
|
VIR_DEBUG("nlmsg_type = 0x%02x", hdr->nlmsg_type);
|
|
|
|
VIR_DEBUG("nlmsg_len = 0x%04x", hdr->nlmsg_len);
|
|
|
|
VIR_DEBUG("nlmsg_pid = %d", hdr->nlmsg_pid);
|
|
|
|
VIR_DEBUG("nlmsg_seq = 0x%08x", hdr->nlmsg_seq);
|
|
|
|
VIR_DEBUG("nlmsg_flags = 0x%04x", hdr->nlmsg_flags);
|
|
|
|
|
|
|
|
VIR_DEBUG("lldpad pid = %d", lldpad_pid);
|
|
|
|
|
|
|
|
switch (hdr->nlmsg_type) {
|
|
|
|
case RTM_NEWLINK:
|
|
|
|
case RTM_DELLINK:
|
|
|
|
case RTM_SETLINK:
|
|
|
|
case RTM_GETLINK:
|
|
|
|
VIR_DEBUG(" IFINFOMSG\n");
|
|
|
|
VIR_DEBUG(" ifi_family = 0x%02x\n",
|
|
|
|
((struct ifinfomsg *)data)->ifi_family);
|
|
|
|
VIR_DEBUG(" ifi_type = 0x%x\n",
|
|
|
|
((struct ifinfomsg *)data)->ifi_type);
|
|
|
|
VIR_DEBUG(" ifi_index = %i\n",
|
|
|
|
((struct ifinfomsg *)data)->ifi_index);
|
|
|
|
VIR_DEBUG(" ifi_flags = 0x%04x\n",
|
|
|
|
((struct ifinfomsg *)data)->ifi_flags);
|
|
|
|
VIR_DEBUG(" ifi_change = 0x%04x\n",
|
|
|
|
((struct ifinfomsg *)data)->ifi_change);
|
|
|
|
}
|
|
|
|
/* DEBUG end */
|
|
|
|
|
|
|
|
/* Parse netlink message assume a setlink with vfports */
|
2012-03-29 09:52:04 +00:00
|
|
|
memcpy(&ifinfo, NLMSG_DATA(hdr), sizeof(ifinfo));
|
2012-02-22 13:17:14 +00:00
|
|
|
VIR_DEBUG("family:%#x type:%#x index:%d flags:%#x change:%#x",
|
|
|
|
ifinfo.ifi_family, ifinfo.ifi_type, ifinfo.ifi_index,
|
|
|
|
ifinfo.ifi_flags, ifinfo.ifi_change);
|
2012-03-29 09:52:04 +00:00
|
|
|
if (nlmsg_parse(hdr, sizeof(ifinfo),
|
2012-02-22 13:17:14 +00:00
|
|
|
(struct nlattr **)&tb, IFLA_MAX, NULL)) {
|
|
|
|
VIR_DEBUG("error parsing request...");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tb[IFLA_VFINFO_LIST]) {
|
|
|
|
VIR_DEBUG("FOUND IFLA_VFINFO_LIST!");
|
|
|
|
|
|
|
|
nla_for_each_nested(tb_vfinfo_list, tb[IFLA_VFINFO_LIST], rem) {
|
|
|
|
if (nla_type(tb_vfinfo_list) != IFLA_VF_INFO) {
|
|
|
|
VIR_DEBUG("nested parsing of"
|
|
|
|
"IFLA_VFINFO_LIST failed.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (nla_parse_nested(tb_vfinfo, IFLA_VF_MAX,
|
|
|
|
tb_vfinfo_list, ifla_vf_policy)) {
|
|
|
|
VIR_DEBUG("nested parsing of "
|
|
|
|
"IFLA_VF_INFO failed.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tb_vfinfo[IFLA_VF_MAC]) {
|
|
|
|
struct ifla_vf_mac *mac = RTA_DATA(tb_vfinfo[IFLA_VF_MAC]);
|
|
|
|
unsigned char *m = mac->mac;
|
|
|
|
|
|
|
|
VIR_DEBUG("IFLA_VF_MAC = %2x:%2x:%2x:%2x:%2x:%2x",
|
|
|
|
m[0], m[1], m[2], m[3], m[4], m[5]);
|
|
|
|
|
2014-09-03 22:24:43 +00:00
|
|
|
if (virMacAddrCmpRaw(&calld->macaddress, mac->mac)) {
|
2012-02-22 13:17:14 +00:00
|
|
|
/* Repeat the same check for a broadcast mac */
|
Convert 'int i' to 'size_t i' in src/util/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2012-02-22 13:17:14 +00:00
|
|
|
|
2012-07-17 12:07:59 +00:00
|
|
|
for (i = 0; i < VIR_MAC_BUFLEN; i++) {
|
|
|
|
if (calld->macaddress.addr[i] != 0xff) {
|
2012-02-22 13:17:14 +00:00
|
|
|
VIR_DEBUG("MAC address match failed (wasn't broadcast)");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tb_vfinfo[IFLA_VF_VLAN]) {
|
|
|
|
struct ifla_vf_vlan *vlan = RTA_DATA(tb_vfinfo[IFLA_VF_VLAN]);
|
|
|
|
|
|
|
|
VIR_DEBUG("IFLA_VF_VLAN = %d", vlan->vlan);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tb[IFLA_IFNAME]) {
|
|
|
|
ifname = (char *)RTA_DATA(tb[IFLA_IFNAME]);
|
|
|
|
VIR_DEBUG("IFLA_IFNAME = %s\n", ifname);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tb[IFLA_OPERSTATE]) {
|
|
|
|
rem = *(unsigned short *)RTA_DATA(tb[IFLA_OPERSTATE]);
|
|
|
|
VIR_DEBUG("IFLA_OPERSTATE = %d\n", rem);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tb[IFLA_VF_PORTS]) {
|
|
|
|
struct nlattr *tb_vf_ports;
|
|
|
|
|
|
|
|
VIR_DEBUG("found IFLA_VF_PORTS\n");
|
|
|
|
nla_for_each_nested(tb_vf_ports, tb[IFLA_VF_PORTS], rem) {
|
|
|
|
|
|
|
|
VIR_DEBUG("iterating\n");
|
|
|
|
if (nla_type(tb_vf_ports) != IFLA_VF_PORT) {
|
|
|
|
VIR_DEBUG("not a IFLA_VF_PORT. skipping\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (nla_parse_nested(tb3, IFLA_PORT_MAX, tb_vf_ports,
|
|
|
|
ifla_port_policy)) {
|
|
|
|
VIR_DEBUG("nested parsing on level 2"
|
|
|
|
" failed.");
|
|
|
|
}
|
|
|
|
if (tb3[IFLA_PORT_VF]) {
|
|
|
|
VIR_DEBUG("IFLA_PORT_VF = %d",
|
|
|
|
*(uint32_t *) (RTA_DATA(tb3[IFLA_PORT_VF])));
|
|
|
|
}
|
|
|
|
if (tb3[IFLA_PORT_PROFILE]) {
|
|
|
|
VIR_DEBUG("IFLA_PORT_PROFILE = %s",
|
|
|
|
(char *) RTA_DATA(tb3[IFLA_PORT_PROFILE]));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tb3[IFLA_PORT_VSI_TYPE]) {
|
|
|
|
struct ifla_port_vsi *pvsi;
|
|
|
|
int tid = 0;
|
|
|
|
|
|
|
|
pvsi = (struct ifla_port_vsi *)
|
|
|
|
RTA_DATA(tb3[IFLA_PORT_VSI_TYPE]);
|
|
|
|
tid = ((pvsi->vsi_type_id[2] << 16) |
|
|
|
|
(pvsi->vsi_type_id[1] << 8) |
|
|
|
|
pvsi->vsi_type_id[0]);
|
|
|
|
|
|
|
|
VIR_DEBUG("mgr_id: %d", pvsi->vsi_mgr_id);
|
|
|
|
VIR_DEBUG("type_id: %d", tid);
|
|
|
|
VIR_DEBUG("type_version: %d",
|
|
|
|
pvsi->vsi_type_version);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tb3[IFLA_PORT_INSTANCE_UUID]) {
|
|
|
|
char instance[INSTANCE_STRLEN + 2];
|
|
|
|
unsigned char *uuid;
|
|
|
|
|
|
|
|
uuid = (unsigned char *)
|
|
|
|
RTA_DATA(tb3[IFLA_PORT_INSTANCE_UUID]);
|
|
|
|
instance2str(uuid, instance, sizeof(instance));
|
|
|
|
VIR_DEBUG("IFLA_PORT_INSTANCE_UUID = %s\n",
|
|
|
|
instance);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tb3[IFLA_PORT_REQUEST]) {
|
|
|
|
uint8_t req = *(uint8_t *) RTA_DATA(tb3[IFLA_PORT_REQUEST]);
|
|
|
|
VIR_DEBUG("IFLA_PORT_REQUEST = %d", req);
|
|
|
|
|
|
|
|
if (req == PORT_REQUEST_DISASSOCIATE) {
|
|
|
|
VIR_DEBUG("Set dissaccociated.");
|
|
|
|
indicate = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tb3[IFLA_PORT_RESPONSE]) {
|
|
|
|
VIR_DEBUG("IFLA_PORT_RESPONSE = %d\n", *(uint16_t *)
|
|
|
|
RTA_DATA(tb3[IFLA_PORT_RESPONSE]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-13 14:28:18 +00:00
|
|
|
if (!indicate)
|
2012-02-22 13:17:14 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
VIR_INFO("Re-send 802.1qbg associate request:");
|
|
|
|
VIR_INFO(" if: %s", calld->cr_ifname);
|
|
|
|
VIR_INFO(" lf: %s", calld->linkdev);
|
2013-03-26 11:21:33 +00:00
|
|
|
VIR_INFO(" mac: %s", virMacAddrFormat(&calld->macaddress, macaddr));
|
2012-02-22 13:17:14 +00:00
|
|
|
ignore_value(virNetDevVPortProfileAssociate(calld->cr_ifname,
|
|
|
|
calld->virtPortProfile,
|
2012-07-17 12:07:59 +00:00
|
|
|
&calld->macaddress,
|
2012-02-22 13:17:14 +00:00
|
|
|
calld->linkdev,
|
2012-03-06 01:12:39 +00:00
|
|
|
calld->vf,
|
2012-02-22 13:17:14 +00:00
|
|
|
calld->vmuuid,
|
|
|
|
calld->vmOp, true));
|
|
|
|
*handled = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virNetlinkCallbackDataFree
|
|
|
|
*
|
|
|
|
* @calld: pointer to a virNetlinkCallbackData object to free
|
|
|
|
*
|
|
|
|
* This function frees all the data associated with a virNetlinkCallbackData object
|
|
|
|
* as well as the object itself. If called with NULL, it does nothing.
|
|
|
|
*
|
|
|
|
* Returns nothing.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
virNetlinkCallbackDataFree(virNetlinkCallbackDataPtr calld)
|
|
|
|
{
|
|
|
|
if (calld) {
|
|
|
|
VIR_FREE(calld->cr_ifname);
|
|
|
|
VIR_FREE(calld->virtPortProfile);
|
|
|
|
VIR_FREE(calld->linkdev);
|
|
|
|
}
|
|
|
|
VIR_FREE(calld);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virNetDevMacVLanVPortProfileDestroyCallback:
|
|
|
|
*
|
|
|
|
* @watch: watch whose handle to remove
|
|
|
|
* @macaddr: macaddr whose handle to remove
|
|
|
|
* @opaque: Contains vital information regarding the associated vm
|
|
|
|
*
|
|
|
|
* This function is called when a netlink message handler is terminated.
|
|
|
|
* The function frees locally allocated data referenced in the opaque
|
|
|
|
* data, and the opaque object itself.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
virNetDevMacVLanVPortProfileDestroyCallback(int watch ATTRIBUTE_UNUSED,
|
2013-10-05 19:41:44 +00:00
|
|
|
const virMacAddr *macaddr ATTRIBUTE_UNUSED,
|
2012-02-22 13:17:14 +00:00
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
virNetlinkCallbackDataFree((virNetlinkCallbackDataPtr)opaque);
|
|
|
|
}
|
|
|
|
|
2012-03-29 11:15:00 +00:00
|
|
|
int
|
2012-03-27 12:38:33 +00:00
|
|
|
virNetDevMacVLanVPortProfileRegisterCallback(const char *ifname,
|
2013-10-05 19:41:44 +00:00
|
|
|
const virMacAddr *macaddress,
|
2012-03-27 12:38:33 +00:00
|
|
|
const char *linkdev,
|
|
|
|
const unsigned char *vmuuid,
|
|
|
|
virNetDevVPortProfilePtr virtPortProfile,
|
2014-04-27 00:15:22 +00:00
|
|
|
virNetDevVPortProfileOp vmOp)
|
2012-03-27 12:38:33 +00:00
|
|
|
{
|
|
|
|
virNetlinkCallbackDataPtr calld = NULL;
|
|
|
|
|
2012-08-22 04:10:23 +00:00
|
|
|
if (virtPortProfile && virNetlinkEventServiceIsRunning(NETLINK_ROUTE)) {
|
2012-03-27 12:38:33 +00:00
|
|
|
if (VIR_ALLOC(calld) < 0)
|
2013-07-04 10:17:18 +00:00
|
|
|
goto error;
|
2013-05-24 07:19:51 +00:00
|
|
|
if (VIR_STRDUP(calld->cr_ifname, ifname) < 0)
|
|
|
|
goto error;
|
2012-03-27 12:38:33 +00:00
|
|
|
if (VIR_ALLOC(calld->virtPortProfile) < 0)
|
2013-07-04 10:17:18 +00:00
|
|
|
goto error;
|
2012-03-27 12:38:33 +00:00
|
|
|
memcpy(calld->virtPortProfile, virtPortProfile, sizeof(*virtPortProfile));
|
2012-07-17 12:07:59 +00:00
|
|
|
virMacAddrSet(&calld->macaddress, macaddress);
|
2013-05-24 07:19:51 +00:00
|
|
|
if (VIR_STRDUP(calld->linkdev, linkdev) < 0)
|
|
|
|
goto error;
|
2012-04-25 11:55:07 +00:00
|
|
|
memcpy(calld->vmuuid, vmuuid, sizeof(calld->vmuuid));
|
2012-03-27 12:38:33 +00:00
|
|
|
|
|
|
|
calld->vmOp = vmOp;
|
|
|
|
|
|
|
|
if (virNetlinkEventAddClient(virNetDevMacVLanVPortProfileCallback,
|
|
|
|
virNetDevMacVLanVPortProfileDestroyCallback,
|
2012-08-22 04:10:23 +00:00
|
|
|
calld, macaddress, NETLINK_ROUTE) < 0)
|
2012-03-27 12:38:33 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
error:
|
2012-03-27 12:38:33 +00:00
|
|
|
virNetlinkCallbackDataFree(calld);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-02-22 13:17:14 +00:00
|
|
|
|
2011-11-02 17:11:02 +00:00
|
|
|
/**
|
2011-11-02 17:19:48 +00:00
|
|
|
* virNetDevMacVLanCreateWithVPortProfile:
|
2011-11-02 17:11:02 +00:00
|
|
|
* Create an instance of a macvtap device and open its tap character
|
|
|
|
* device.
|
|
|
|
* @tgifname: Interface name that the macvtap is supposed to have. May
|
|
|
|
* be NULL if this function is supposed to choose a name
|
|
|
|
* @macaddress: The MAC address for the macvtap device
|
|
|
|
* @linkdev: The interface name of the NIC to connect to the external bridge
|
|
|
|
* @mode: int describing the mode for 'bridge', 'vepa', 'private' or 'passthru'.
|
|
|
|
* @vnet_hdr: 1 to enable IFF_VNET_HDR, 0 to disable it
|
|
|
|
* @vmuuid: The UUID of the VM the macvtap belongs to
|
|
|
|
* @virtPortProfile: pointer to object holding the virtual port profile data
|
|
|
|
* @res_ifname: Pointer to a string pointer where the actual name of the
|
|
|
|
* interface will be stored into if everything succeeded. It is up
|
|
|
|
* to the caller to free the string.
|
2014-08-27 14:34:13 +00:00
|
|
|
* @flags: OR of virNetDevMacVLanCreateFlags.
|
2011-11-02 17:11:02 +00:00
|
|
|
*
|
2014-08-27 14:34:13 +00:00
|
|
|
* Returns file descriptor of the tap device in case of success with
|
|
|
|
* @flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP, otherwise returns 0; returns
|
|
|
|
* -1 on error.
|
2011-11-02 17:11:02 +00:00
|
|
|
*/
|
2011-11-02 17:19:48 +00:00
|
|
|
int virNetDevMacVLanCreateWithVPortProfile(const char *tgifname,
|
2013-10-05 19:41:44 +00:00
|
|
|
const virMacAddr *macaddress,
|
2011-11-02 17:19:48 +00:00
|
|
|
const char *linkdev,
|
2014-04-27 00:15:22 +00:00
|
|
|
virNetDevMacVLanMode mode,
|
2011-11-02 17:19:48 +00:00
|
|
|
int vnet_hdr,
|
|
|
|
const unsigned char *vmuuid,
|
|
|
|
virNetDevVPortProfilePtr virtPortProfile,
|
|
|
|
char **res_ifname,
|
2014-04-27 00:15:22 +00:00
|
|
|
virNetDevVPortProfileOp vmOp,
|
2011-11-02 17:19:48 +00:00
|
|
|
char *stateDir,
|
2014-08-27 14:34:13 +00:00
|
|
|
unsigned int flags)
|
2011-11-02 17:11:02 +00:00
|
|
|
{
|
2014-08-27 14:34:13 +00:00
|
|
|
const char *type = (flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) ?
|
|
|
|
"macvtap" : "macvlan";
|
|
|
|
const char *prefix = (flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) ?
|
|
|
|
MACVTAP_NAME_PREFIX : MACVLAN_NAME_PREFIX;
|
|
|
|
const char *pattern = (flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) ?
|
|
|
|
MACVTAP_NAME_PATTERN : MACVLAN_NAME_PATTERN;
|
2011-11-02 17:11:02 +00:00
|
|
|
int c, rc;
|
|
|
|
char ifname[IFNAMSIZ];
|
|
|
|
int retries, do_retry = 0;
|
|
|
|
uint32_t macvtapMode;
|
2013-02-28 14:59:01 +00:00
|
|
|
const char *cr_ifname = NULL;
|
2011-11-03 09:21:35 +00:00
|
|
|
int ret;
|
2012-03-06 01:12:39 +00:00
|
|
|
int vf = -1;
|
2011-11-02 17:11:02 +00:00
|
|
|
|
|
|
|
macvtapMode = modeMap[mode];
|
|
|
|
|
|
|
|
*res_ifname = NULL;
|
|
|
|
|
|
|
|
VIR_DEBUG("%s: VM OPERATION: %s", __FUNCTION__, virNetDevVPortProfileOpTypeToString(vmOp));
|
|
|
|
|
|
|
|
/** Note: When using PASSTHROUGH mode with MACVTAP devices the link
|
|
|
|
* device's MAC address must be set to the VMs MAC address. In
|
|
|
|
* order to not confuse the first switch or bridge in line this MAC
|
|
|
|
* address must be reset when the VM is shut down.
|
|
|
|
* This is especially important when using SRIOV capable cards that
|
|
|
|
* emulate their switch in firmware.
|
|
|
|
*/
|
|
|
|
if (mode == VIR_NETDEV_MACVLAN_MODE_PASSTHRU) {
|
2011-11-02 17:38:26 +00:00
|
|
|
if (virNetDevReplaceMacAddress(linkdev, macaddress, stateDir) < 0)
|
2011-11-02 17:11:02 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tgifname) {
|
2011-11-03 09:21:35 +00:00
|
|
|
if ((ret = virNetDevExists(tgifname)) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (ret) {
|
2014-11-13 14:28:18 +00:00
|
|
|
if (STRPREFIX(tgifname, prefix))
|
2011-11-02 17:11:02 +00:00
|
|
|
goto create_name;
|
2011-11-03 09:21:35 +00:00
|
|
|
virReportSystemError(EEXIST,
|
|
|
|
_("Unable to create macvlan device %s"), tgifname);
|
2011-11-02 17:11:02 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cr_ifname = tgifname;
|
2011-11-02 17:26:11 +00:00
|
|
|
rc = virNetDevMacVLanCreate(tgifname, type, macaddress, linkdev,
|
|
|
|
macvtapMode, &do_retry);
|
2011-11-02 17:11:02 +00:00
|
|
|
if (rc < 0)
|
|
|
|
return -1;
|
|
|
|
} else {
|
2014-03-25 06:53:22 +00:00
|
|
|
create_name:
|
2011-11-02 17:11:02 +00:00
|
|
|
retries = 5;
|
2013-02-28 14:59:01 +00:00
|
|
|
virMutexLock(&virNetDevMacVLanCreateMutex);
|
2011-11-02 17:11:02 +00:00
|
|
|
for (c = 0; c < 8192; c++) {
|
2011-11-10 10:29:09 +00:00
|
|
|
snprintf(ifname, sizeof(ifname), pattern, c);
|
2013-02-28 14:59:01 +00:00
|
|
|
if ((ret = virNetDevExists(ifname)) < 0) {
|
|
|
|
virMutexUnlock(&virNetDevMacVLanCreateMutex);
|
2011-11-03 09:21:35 +00:00
|
|
|
return -1;
|
2013-02-28 14:59:01 +00:00
|
|
|
}
|
2011-11-03 09:21:35 +00:00
|
|
|
if (!ret) {
|
2011-11-02 17:26:11 +00:00
|
|
|
rc = virNetDevMacVLanCreate(ifname, type, macaddress, linkdev,
|
|
|
|
macvtapMode, &do_retry);
|
2013-02-28 14:59:01 +00:00
|
|
|
if (rc == 0) {
|
|
|
|
cr_ifname = ifname;
|
2011-11-02 17:11:02 +00:00
|
|
|
break;
|
2013-02-28 14:59:01 +00:00
|
|
|
}
|
2011-11-02 17:11:02 +00:00
|
|
|
|
|
|
|
if (do_retry && --retries)
|
|
|
|
continue;
|
2013-02-28 14:59:01 +00:00
|
|
|
break;
|
2011-11-02 17:11:02 +00:00
|
|
|
}
|
|
|
|
}
|
2013-02-28 14:59:01 +00:00
|
|
|
|
|
|
|
virMutexUnlock(&virNetDevMacVLanCreateMutex);
|
2013-05-13 16:32:55 +00:00
|
|
|
if (!cr_ifname)
|
2013-02-28 14:59:01 +00:00
|
|
|
return -1;
|
2011-11-02 17:11:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (virNetDevVPortProfileAssociate(cr_ifname,
|
|
|
|
virtPortProfile,
|
|
|
|
macaddress,
|
|
|
|
linkdev,
|
2012-03-06 01:12:39 +00:00
|
|
|
vf,
|
2012-02-22 13:17:14 +00:00
|
|
|
vmuuid, vmOp, false) < 0) {
|
2011-11-02 17:11:02 +00:00
|
|
|
rc = -1;
|
|
|
|
goto link_del_exit;
|
|
|
|
}
|
|
|
|
|
2014-09-16 20:50:53 +00:00
|
|
|
if (flags & VIR_NETDEV_MACVLAN_CREATE_IFUP) {
|
|
|
|
if (virNetDevSetOnline(cr_ifname, true) < 0) {
|
|
|
|
rc = -1;
|
|
|
|
goto disassociate_exit;
|
|
|
|
}
|
2011-11-02 17:11:02 +00:00
|
|
|
}
|
|
|
|
|
2014-08-27 14:34:13 +00:00
|
|
|
if (flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) {
|
2011-11-10 10:29:09 +00:00
|
|
|
if ((rc = virNetDevMacVLanTapOpen(cr_ifname, 10)) < 0)
|
|
|
|
goto disassociate_exit;
|
|
|
|
|
2011-11-02 17:11:02 +00:00
|
|
|
if (virNetDevMacVLanTapSetup(rc, vnet_hdr) < 0) {
|
|
|
|
VIR_FORCE_CLOSE(rc); /* sets rc to -1 */
|
|
|
|
goto disassociate_exit;
|
|
|
|
}
|
2013-05-24 07:19:51 +00:00
|
|
|
if (VIR_STRDUP(*res_ifname, cr_ifname) < 0) {
|
2011-11-10 10:29:09 +00:00
|
|
|
VIR_FORCE_CLOSE(rc); /* sets rc to -1 */
|
|
|
|
goto disassociate_exit;
|
|
|
|
}
|
|
|
|
} else {
|
2013-05-24 07:19:51 +00:00
|
|
|
if (VIR_STRDUP(*res_ifname, cr_ifname) < 0)
|
2011-11-10 10:29:09 +00:00
|
|
|
goto disassociate_exit;
|
|
|
|
rc = 0;
|
|
|
|
}
|
2011-11-02 17:11:02 +00:00
|
|
|
|
2012-04-13 12:41:16 +00:00
|
|
|
if (vmOp == VIR_NETDEV_VPORT_PROFILE_OP_CREATE ||
|
|
|
|
vmOp == VIR_NETDEV_VPORT_PROFILE_OP_RESTORE) {
|
|
|
|
/* Only directly register upon a create or restore (restarting
|
|
|
|
* a saved image) - migration and libvirtd restart are handled
|
|
|
|
* elsewhere.
|
|
|
|
*/
|
|
|
|
if (virNetDevMacVLanVPortProfileRegisterCallback(cr_ifname, macaddress,
|
|
|
|
linkdev, vmuuid,
|
|
|
|
virtPortProfile,
|
2012-10-17 09:23:12 +00:00
|
|
|
vmOp) < 0)
|
2012-03-27 12:38:33 +00:00
|
|
|
goto disassociate_exit;
|
2012-04-13 12:41:16 +00:00
|
|
|
}
|
2011-11-02 17:11:02 +00:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
disassociate_exit:
|
2011-11-02 17:11:02 +00:00
|
|
|
ignore_value(virNetDevVPortProfileDisassociate(cr_ifname,
|
|
|
|
virtPortProfile,
|
|
|
|
macaddress,
|
|
|
|
linkdev,
|
2012-03-06 01:12:39 +00:00
|
|
|
vf,
|
2011-11-02 17:11:02 +00:00
|
|
|
vmOp));
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
link_del_exit:
|
2011-11-02 17:26:11 +00:00
|
|
|
ignore_value(virNetDevMacVLanDelete(cr_ifname));
|
2011-11-02 17:11:02 +00:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2011-11-02 17:19:48 +00:00
|
|
|
* virNetDevMacVLanDeleteWithVPortProfile:
|
2011-11-02 17:11:02 +00:00
|
|
|
* @ifname : The name of the macvtap interface
|
|
|
|
* @linkdev: The interface name of the NIC to connect to the external bridge
|
|
|
|
* @virtPortProfile: pointer to object holding the virtual port profile data
|
|
|
|
*
|
|
|
|
* Delete an interface given its name. Disassociate
|
|
|
|
* it with the switch if port profile parameters
|
|
|
|
* were provided.
|
|
|
|
*/
|
2011-11-02 17:19:48 +00:00
|
|
|
int virNetDevMacVLanDeleteWithVPortProfile(const char *ifname,
|
2013-10-05 19:41:44 +00:00
|
|
|
const virMacAddr *macaddr,
|
2011-11-02 17:19:48 +00:00
|
|
|
const char *linkdev,
|
|
|
|
int mode,
|
|
|
|
virNetDevVPortProfilePtr virtPortProfile,
|
|
|
|
char *stateDir)
|
2011-11-02 17:11:02 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2012-03-06 01:12:39 +00:00
|
|
|
int vf = -1;
|
|
|
|
|
2014-11-13 14:28:18 +00:00
|
|
|
if (mode == VIR_NETDEV_MACVLAN_MODE_PASSTHRU)
|
2011-11-02 17:38:26 +00:00
|
|
|
ignore_value(virNetDevRestoreMacAddress(linkdev, stateDir));
|
2011-11-02 17:11:02 +00:00
|
|
|
|
|
|
|
if (ifname) {
|
|
|
|
if (virNetDevVPortProfileDisassociate(ifname,
|
|
|
|
virtPortProfile,
|
|
|
|
macaddr,
|
|
|
|
linkdev,
|
2012-03-06 01:12:39 +00:00
|
|
|
vf,
|
2011-11-02 17:11:02 +00:00
|
|
|
VIR_NETDEV_VPORT_PROFILE_OP_DESTROY) < 0)
|
|
|
|
ret = -1;
|
2011-11-02 17:26:11 +00:00
|
|
|
if (virNetDevMacVLanDelete(ifname) < 0)
|
2011-11-02 17:11:02 +00:00
|
|
|
ret = -1;
|
|
|
|
}
|
2012-02-22 13:17:14 +00:00
|
|
|
|
2012-08-22 04:10:23 +00:00
|
|
|
virNetlinkEventRemoveClient(0, macaddr, NETLINK_ROUTE);
|
2012-02-22 13:17:14 +00:00
|
|
|
|
2011-11-02 17:11:02 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-03-27 12:38:33 +00:00
|
|
|
/**
|
|
|
|
* virNetDevMacVLanRestartWithVPortProfile:
|
|
|
|
* Register a port profile callback handler for a VM that
|
|
|
|
* is already running
|
|
|
|
* .
|
|
|
|
* @cr_ifname: Interface name that the macvtap has.
|
|
|
|
* @macaddress: The MAC address for the macvtap device
|
|
|
|
* @linkdev: The interface name of the NIC to connect to the external bridge
|
|
|
|
* @vmuuid: The UUID of the VM the macvtap belongs to
|
|
|
|
* @virtPortProfile: pointer to object holding the virtual port profile data
|
|
|
|
* @vmOp: Operation to use during setup of the association
|
|
|
|
*
|
|
|
|
* Returns 0; returns -1 on error.
|
|
|
|
*/
|
|
|
|
int virNetDevMacVLanRestartWithVPortProfile(const char *cr_ifname,
|
2013-10-05 19:41:44 +00:00
|
|
|
const virMacAddr *macaddress,
|
2012-03-27 12:38:33 +00:00
|
|
|
const char *linkdev,
|
|
|
|
const unsigned char *vmuuid,
|
|
|
|
virNetDevVPortProfilePtr virtPortProfile,
|
2014-04-27 00:15:22 +00:00
|
|
|
virNetDevVPortProfileOp vmOp)
|
2012-03-27 12:38:33 +00:00
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
rc = virNetDevMacVLanVPortProfileRegisterCallback(cr_ifname, macaddress,
|
|
|
|
linkdev, vmuuid,
|
|
|
|
virtPortProfile, vmOp);
|
|
|
|
if (rc < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
ignore_value(virNetDevVPortProfileAssociate(cr_ifname,
|
|
|
|
virtPortProfile,
|
|
|
|
macaddress,
|
|
|
|
linkdev,
|
|
|
|
-1,
|
|
|
|
vmuuid,
|
|
|
|
vmOp, true));
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
error:
|
2012-03-27 12:38:33 +00:00
|
|
|
return rc;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-11-02 17:11:02 +00:00
|
|
|
#else /* ! WITH_MACVTAP */
|
2011-11-02 17:34:41 +00:00
|
|
|
int virNetDevMacVLanCreate(const char *ifname ATTRIBUTE_UNUSED,
|
|
|
|
const char *type ATTRIBUTE_UNUSED,
|
2013-10-05 19:41:44 +00:00
|
|
|
const virMacAddr *macaddress ATTRIBUTE_UNUSED,
|
2011-11-02 17:34:41 +00:00
|
|
|
const char *srcdev ATTRIBUTE_UNUSED,
|
|
|
|
uint32_t macvlan_mode ATTRIBUTE_UNUSED,
|
|
|
|
int *retry ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
virReportSystemError(ENOSYS, "%s",
|
|
|
|
_("Cannot create macvlan devices on this platform"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int virNetDevMacVLanDelete(const char *ifname ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
virReportSystemError(ENOSYS, "%s",
|
|
|
|
_("Cannot create macvlan devices on this platform"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-11-02 17:19:48 +00:00
|
|
|
int virNetDevMacVLanCreateWithVPortProfile(const char *ifname ATTRIBUTE_UNUSED,
|
2013-10-05 19:41:44 +00:00
|
|
|
const virMacAddr *macaddress ATTRIBUTE_UNUSED,
|
2011-11-02 17:19:48 +00:00
|
|
|
const char *linkdev ATTRIBUTE_UNUSED,
|
2014-05-06 07:14:05 +00:00
|
|
|
virNetDevMacVLanMode mode ATTRIBUTE_UNUSED,
|
2011-11-02 17:19:48 +00:00
|
|
|
int vnet_hdr ATTRIBUTE_UNUSED,
|
|
|
|
const unsigned char *vmuuid ATTRIBUTE_UNUSED,
|
|
|
|
virNetDevVPortProfilePtr virtPortProfile ATTRIBUTE_UNUSED,
|
|
|
|
char **res_ifname ATTRIBUTE_UNUSED,
|
2014-05-06 07:14:05 +00:00
|
|
|
virNetDevVPortProfileOp vmop ATTRIBUTE_UNUSED,
|
2011-11-02 17:19:48 +00:00
|
|
|
char *stateDir ATTRIBUTE_UNUSED,
|
2014-08-27 14:34:13 +00:00
|
|
|
unsigned int flags)
|
2011-11-02 17:11:02 +00:00
|
|
|
{
|
2014-08-27 14:34:13 +00:00
|
|
|
virCheckFlags(0, -1);
|
2011-11-02 17:11:02 +00:00
|
|
|
virReportSystemError(ENOSYS, "%s",
|
|
|
|
_("Cannot create macvlan devices on this platform"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-11-02 17:19:48 +00:00
|
|
|
int virNetDevMacVLanDeleteWithVPortProfile(const char *ifname ATTRIBUTE_UNUSED,
|
2013-10-05 19:41:44 +00:00
|
|
|
const virMacAddr *macaddress ATTRIBUTE_UNUSED,
|
2011-11-02 17:19:48 +00:00
|
|
|
const char *linkdev ATTRIBUTE_UNUSED,
|
|
|
|
int mode ATTRIBUTE_UNUSED,
|
|
|
|
virNetDevVPortProfilePtr virtPortProfile ATTRIBUTE_UNUSED,
|
|
|
|
char *stateDir ATTRIBUTE_UNUSED)
|
2011-11-02 17:11:02 +00:00
|
|
|
{
|
|
|
|
virReportSystemError(ENOSYS, "%s",
|
|
|
|
_("Cannot create macvlan devices on this platform"));
|
|
|
|
return -1;
|
|
|
|
}
|
2012-03-27 12:38:33 +00:00
|
|
|
|
|
|
|
int virNetDevMacVLanRestartWithVPortProfile(const char *cr_ifname ATTRIBUTE_UNUSED,
|
2013-10-05 19:41:44 +00:00
|
|
|
const virMacAddr *macaddress ATTRIBUTE_UNUSED,
|
2012-03-27 12:38:33 +00:00
|
|
|
const char *linkdev ATTRIBUTE_UNUSED,
|
|
|
|
const unsigned char *vmuuid ATTRIBUTE_UNUSED,
|
|
|
|
virNetDevVPortProfilePtr virtPortProfile ATTRIBUTE_UNUSED,
|
2014-05-06 07:14:05 +00:00
|
|
|
virNetDevVPortProfileOp vmOp ATTRIBUTE_UNUSED)
|
2012-03-27 12:38:33 +00:00
|
|
|
{
|
|
|
|
virReportSystemError(ENOSYS, "%s",
|
|
|
|
_("Cannot create macvlan devices on this platform"));
|
|
|
|
return -1;
|
|
|
|
}
|
2012-03-29 11:15:00 +00:00
|
|
|
|
|
|
|
int virNetDevMacVLanVPortProfileRegisterCallback(const char *ifname ATTRIBUTE_UNUSED,
|
2013-10-05 19:41:44 +00:00
|
|
|
const virMacAddr *macaddress ATTRIBUTE_UNUSED,
|
2012-03-29 11:15:00 +00:00
|
|
|
const char *linkdev ATTRIBUTE_UNUSED,
|
|
|
|
const unsigned char *vmuuid ATTRIBUTE_UNUSED,
|
|
|
|
virNetDevVPortProfilePtr virtPortProfile ATTRIBUTE_UNUSED,
|
2014-05-06 07:14:05 +00:00
|
|
|
virNetDevVPortProfileOp vmOp ATTRIBUTE_UNUSED)
|
2012-03-29 11:15:00 +00:00
|
|
|
{
|
|
|
|
virReportSystemError(ENOSYS, "%s",
|
|
|
|
_("Cannot create macvlan devices on this platform"));
|
|
|
|
return -1;
|
|
|
|
}
|
2011-11-02 17:11:02 +00:00
|
|
|
#endif /* ! WITH_MACVTAP */
|