2012-10-06 19:20:25 +00:00
|
|
|
/*
|
|
|
|
* interface_backend_udev.c: udev backend for virInterface
|
|
|
|
*
|
2014-03-18 08:19:33 +00:00
|
|
|
* Copyright (C) 2014 Red Hat, Inc.
|
2012-10-06 19:20:25 +00:00
|
|
|
* Copyright (C) 2012 Doug Goldstein <cardoe@cardoe.com>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* License along with this library; If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <config.h>
|
|
|
|
|
2012-10-14 22:02:33 +00:00
|
|
|
#include <dirent.h>
|
2012-10-06 19:20:25 +00:00
|
|
|
#include <libudev.h>
|
|
|
|
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2014-04-24 12:58:47 +00:00
|
|
|
#include "virfile.h"
|
2012-10-06 19:20:25 +00:00
|
|
|
#include "datatypes.h"
|
2013-02-25 02:31:03 +00:00
|
|
|
#include "domain_conf.h"
|
2012-10-06 19:20:25 +00:00
|
|
|
#include "interface_driver.h"
|
|
|
|
#include "interface_conf.h"
|
2012-12-12 18:06:53 +00:00
|
|
|
#include "viralloc.h"
|
2013-04-03 10:36:23 +00:00
|
|
|
#include "virstring.h"
|
2019-05-23 10:34:08 +00:00
|
|
|
#include "virpidfile.h"
|
2013-04-23 10:56:22 +00:00
|
|
|
#include "viraccessapicheck.h"
|
2017-02-28 21:20:44 +00:00
|
|
|
#include "virinterfaceobj.h"
|
2014-06-05 14:39:18 +00:00
|
|
|
#include "virnetdev.h"
|
2020-02-16 21:59:28 +00:00
|
|
|
#include "virutil.h"
|
2012-10-06 19:20:25 +00:00
|
|
|
|
2019-05-23 10:34:08 +00:00
|
|
|
#include "configmake.h"
|
|
|
|
|
2012-10-06 19:20:25 +00:00
|
|
|
#define VIR_FROM_THIS VIR_FROM_INTERFACE
|
|
|
|
|
|
|
|
struct udev_iface_driver {
|
|
|
|
struct udev *udev;
|
2019-05-23 10:34:08 +00:00
|
|
|
/* pid file FD, ensures two copies of the driver can't use the same root */
|
|
|
|
int lockFD;
|
|
|
|
|
|
|
|
char *stateDir;
|
2018-01-26 11:16:00 +00:00
|
|
|
bool privileged;
|
2012-10-06 19:20:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
VIR_UDEV_IFACE_ACTIVE,
|
|
|
|
VIR_UDEV_IFACE_INACTIVE,
|
|
|
|
VIR_UDEV_IFACE_ALL
|
2013-05-21 07:21:20 +00:00
|
|
|
} virUdevStatus;
|
2012-10-06 19:20:25 +00:00
|
|
|
|
2014-11-17 17:09:00 +00:00
|
|
|
static struct udev_iface_driver *driver;
|
|
|
|
|
2013-04-23 12:50:18 +00:00
|
|
|
static virInterfaceDef *udevGetIfaceDef(struct udev *udev, const char *name);
|
2013-02-15 16:49:08 +00:00
|
|
|
|
2012-10-06 19:20:25 +00:00
|
|
|
static const char *
|
|
|
|
virUdevStatusString(virUdevStatus status)
|
|
|
|
{
|
|
|
|
switch (status) {
|
|
|
|
case VIR_UDEV_IFACE_ACTIVE:
|
|
|
|
return "active";
|
|
|
|
case VIR_UDEV_IFACE_INACTIVE:
|
|
|
|
return "inactive";
|
|
|
|
case VIR_UDEV_IFACE_ALL:
|
|
|
|
return "all";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2013-04-23 10:56:22 +00:00
|
|
|
/*
|
|
|
|
* Get a minimal virInterfaceDef containing enough metadata
|
|
|
|
* for access control checks to be performed. Currently
|
2014-04-20 20:07:46 +00:00
|
|
|
* this implies existence of name and mac address attributes
|
2013-04-23 10:56:22 +00:00
|
|
|
*/
|
|
|
|
static virInterfaceDef * ATTRIBUTE_NONNULL(1)
|
|
|
|
udevGetMinimalDefForDevice(struct udev_device *dev)
|
|
|
|
{
|
|
|
|
virInterfaceDef *def;
|
|
|
|
|
|
|
|
/* Allocate our interface definition structure */
|
2013-07-04 10:10:05 +00:00
|
|
|
if (VIR_ALLOC(def) < 0)
|
2013-04-23 10:56:22 +00:00
|
|
|
return NULL;
|
|
|
|
|
2019-10-20 11:49:46 +00:00
|
|
|
def->name = g_strdup(udev_device_get_sysname(dev));
|
|
|
|
def->mac = g_strdup(udev_device_get_sysattr_value(dev, "address"));
|
2013-04-23 10:56:22 +00:00
|
|
|
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-06 19:20:25 +00:00
|
|
|
static struct udev_enumerate * ATTRIBUTE_NONNULL(1)
|
2013-04-23 12:50:18 +00:00
|
|
|
udevGetDevices(struct udev *udev, virUdevStatus status)
|
2012-10-06 19:20:25 +00:00
|
|
|
{
|
|
|
|
struct udev_enumerate *enumerate;
|
|
|
|
|
|
|
|
/* Create a new enumeration to create a list */
|
|
|
|
enumerate = udev_enumerate_new(udev);
|
|
|
|
|
|
|
|
if (!enumerate)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Enumerate all network subsystem devices */
|
|
|
|
udev_enumerate_add_match_subsystem(enumerate, "net");
|
|
|
|
|
|
|
|
/* Ignore devices that are part of a bridge */
|
|
|
|
udev_enumerate_add_nomatch_sysattr(enumerate, "brport/state", NULL);
|
|
|
|
|
|
|
|
/* State of the device */
|
|
|
|
switch (status) {
|
|
|
|
case VIR_UDEV_IFACE_ACTIVE:
|
|
|
|
udev_enumerate_add_match_sysattr(enumerate, "operstate", "up");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VIR_UDEV_IFACE_INACTIVE:
|
|
|
|
udev_enumerate_add_match_sysattr(enumerate, "operstate", "down");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VIR_UDEV_IFACE_ALL:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We don't want to see the TUN devices that QEMU creates for other guests
|
|
|
|
* running on this machine. By saying nomatch NULL, we just are getting
|
|
|
|
* devices without the tun_flags sysattr.
|
|
|
|
*/
|
|
|
|
udev_enumerate_add_nomatch_sysattr(enumerate, "tun_flags", NULL);
|
|
|
|
|
|
|
|
return enumerate;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2013-06-27 16:07:22 +00:00
|
|
|
udevNumOfInterfacesByStatus(virConnectPtr conn, virUdevStatus status,
|
|
|
|
virInterfaceObjListFilter filter)
|
2012-10-06 19:20:25 +00:00
|
|
|
{
|
2014-11-17 17:09:00 +00:00
|
|
|
struct udev *udev = udev_ref(driver->udev);
|
2012-10-06 19:20:25 +00:00
|
|
|
struct udev_enumerate *enumerate = NULL;
|
|
|
|
struct udev_list_entry *devices;
|
|
|
|
struct udev_list_entry *dev_entry;
|
|
|
|
int count = 0;
|
|
|
|
|
2013-04-23 12:50:18 +00:00
|
|
|
enumerate = udevGetDevices(udev, status);
|
2012-10-06 19:20:25 +00:00
|
|
|
|
|
|
|
if (!enumerate) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("failed to get number of %s interfaces on host"),
|
|
|
|
virUdevStatusString(status));
|
|
|
|
count = -1;
|
2013-04-29 17:37:48 +00:00
|
|
|
goto cleanup;
|
2012-10-06 19:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Do the scan to load up the enumeration */
|
|
|
|
udev_enumerate_scan_devices(enumerate);
|
|
|
|
|
|
|
|
/* Get a list we can walk */
|
|
|
|
devices = udev_enumerate_get_list_entry(enumerate);
|
|
|
|
|
|
|
|
/* For each item so we can count */
|
|
|
|
udev_list_entry_foreach(dev_entry, devices) {
|
2013-06-27 16:07:22 +00:00
|
|
|
struct udev_device *dev;
|
|
|
|
const char *path;
|
|
|
|
virInterfaceDefPtr def;
|
|
|
|
|
|
|
|
path = udev_list_entry_get_name(dev_entry);
|
|
|
|
dev = udev_device_new_from_syspath(udev, path);
|
|
|
|
|
|
|
|
def = udevGetMinimalDefForDevice(dev);
|
|
|
|
if (filter(conn, def))
|
|
|
|
count++;
|
|
|
|
udev_device_unref(dev);
|
|
|
|
virInterfaceDefFree(def);
|
2012-10-06 19:20:25 +00:00
|
|
|
}
|
|
|
|
|
2014-03-25 06:57:22 +00:00
|
|
|
cleanup:
|
2012-10-06 19:20:25 +00:00
|
|
|
if (enumerate)
|
|
|
|
udev_enumerate_unref(enumerate);
|
|
|
|
udev_unref(udev);
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2013-04-23 12:50:18 +00:00
|
|
|
udevListInterfacesByStatus(virConnectPtr conn,
|
|
|
|
char **const names,
|
|
|
|
int names_len,
|
2013-06-27 16:07:22 +00:00
|
|
|
virUdevStatus status,
|
|
|
|
virInterfaceObjListFilter filter)
|
2012-10-06 19:20:25 +00:00
|
|
|
{
|
2014-11-17 17:09:00 +00:00
|
|
|
struct udev *udev = udev_ref(driver->udev);
|
2012-10-06 19:20:25 +00:00
|
|
|
struct udev_enumerate *enumerate = NULL;
|
|
|
|
struct udev_list_entry *devices;
|
|
|
|
struct udev_list_entry *dev_entry;
|
|
|
|
int count = 0;
|
|
|
|
|
2013-04-23 12:50:18 +00:00
|
|
|
enumerate = udevGetDevices(udev, status);
|
2012-10-06 19:20:25 +00:00
|
|
|
|
|
|
|
if (!enumerate) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("failed to get list of %s interfaces on host"),
|
|
|
|
virUdevStatusString(status));
|
2013-04-29 17:37:48 +00:00
|
|
|
goto error;
|
2012-10-06 19:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Do the scan to load up the enumeration */
|
|
|
|
udev_enumerate_scan_devices(enumerate);
|
|
|
|
|
|
|
|
/* Get a list we can walk */
|
|
|
|
devices = udev_enumerate_get_list_entry(enumerate);
|
|
|
|
|
|
|
|
/* For each item so we can count */
|
|
|
|
udev_list_entry_foreach(dev_entry, devices) {
|
|
|
|
struct udev_device *dev;
|
|
|
|
const char *path;
|
2013-06-27 16:07:22 +00:00
|
|
|
virInterfaceDefPtr def;
|
2012-10-06 19:20:25 +00:00
|
|
|
|
|
|
|
/* Ensure we won't exceed the size of our array */
|
|
|
|
if (count > names_len)
|
|
|
|
break;
|
|
|
|
|
|
|
|
path = udev_list_entry_get_name(dev_entry);
|
|
|
|
dev = udev_device_new_from_syspath(udev, path);
|
2013-06-27 16:07:22 +00:00
|
|
|
|
|
|
|
def = udevGetMinimalDefForDevice(dev);
|
|
|
|
if (filter(conn, def)) {
|
2019-10-20 11:49:46 +00:00
|
|
|
names[count] = g_strdup(udev_device_get_sysname(dev));
|
2013-06-27 16:07:22 +00:00
|
|
|
count++;
|
2012-10-06 19:20:25 +00:00
|
|
|
}
|
2013-05-03 12:44:36 +00:00
|
|
|
udev_device_unref(dev);
|
2013-06-27 16:07:22 +00:00
|
|
|
virInterfaceDefFree(def);
|
2012-10-06 19:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
udev_enumerate_unref(enumerate);
|
|
|
|
udev_unref(udev);
|
|
|
|
|
|
|
|
return count;
|
|
|
|
|
2014-03-25 06:57:22 +00:00
|
|
|
error:
|
2012-10-06 19:20:25 +00:00
|
|
|
if (enumerate)
|
|
|
|
udev_enumerate_unref(enumerate);
|
|
|
|
udev_unref(udev);
|
|
|
|
|
|
|
|
for (names_len = 0; names_len < count; names_len++)
|
|
|
|
VIR_FREE(names[names_len]);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2013-04-23 12:50:18 +00:00
|
|
|
udevConnectNumOfInterfaces(virConnectPtr conn)
|
2012-10-06 19:20:25 +00:00
|
|
|
{
|
2013-04-23 10:56:22 +00:00
|
|
|
if (virConnectNumOfInterfacesEnsureACL(conn) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2013-06-27 16:07:22 +00:00
|
|
|
return udevNumOfInterfacesByStatus(conn, VIR_UDEV_IFACE_ACTIVE,
|
|
|
|
virConnectNumOfInterfacesCheckACL);
|
2012-10-06 19:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2013-04-23 12:50:18 +00:00
|
|
|
udevConnectListInterfaces(virConnectPtr conn,
|
|
|
|
char **const names,
|
|
|
|
int names_len)
|
2012-10-06 19:20:25 +00:00
|
|
|
{
|
2013-04-23 10:56:22 +00:00
|
|
|
if (virConnectListInterfacesEnsureACL(conn) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2013-04-23 12:50:18 +00:00
|
|
|
return udevListInterfacesByStatus(conn, names, names_len,
|
2013-06-27 16:07:22 +00:00
|
|
|
VIR_UDEV_IFACE_ACTIVE,
|
|
|
|
virConnectListInterfacesCheckACL);
|
2012-10-06 19:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2013-04-23 12:50:18 +00:00
|
|
|
udevConnectNumOfDefinedInterfaces(virConnectPtr conn)
|
2012-10-06 19:20:25 +00:00
|
|
|
{
|
2013-04-23 10:56:22 +00:00
|
|
|
if (virConnectNumOfDefinedInterfacesEnsureACL(conn) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2013-06-27 16:07:22 +00:00
|
|
|
return udevNumOfInterfacesByStatus(conn, VIR_UDEV_IFACE_INACTIVE,
|
|
|
|
virConnectNumOfDefinedInterfacesCheckACL);
|
2012-10-06 19:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2013-04-23 12:50:18 +00:00
|
|
|
udevConnectListDefinedInterfaces(virConnectPtr conn,
|
|
|
|
char **const names,
|
|
|
|
int names_len)
|
2012-10-06 19:20:25 +00:00
|
|
|
{
|
2013-04-23 10:56:22 +00:00
|
|
|
if (virConnectListDefinedInterfacesEnsureACL(conn) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2013-04-23 12:50:18 +00:00
|
|
|
return udevListInterfacesByStatus(conn, names, names_len,
|
2013-06-27 16:07:22 +00:00
|
|
|
VIR_UDEV_IFACE_INACTIVE,
|
|
|
|
virConnectListDefinedInterfacesCheckACL);
|
2012-10-06 19:20:25 +00:00
|
|
|
}
|
|
|
|
|
2013-05-21 13:29:38 +00:00
|
|
|
#define MATCH(FLAG) (flags & (FLAG))
|
2012-10-06 19:20:25 +00:00
|
|
|
static int
|
2013-04-23 12:50:18 +00:00
|
|
|
udevConnectListAllInterfaces(virConnectPtr conn,
|
|
|
|
virInterfacePtr **ifaces,
|
|
|
|
unsigned int flags)
|
2012-10-06 19:20:25 +00:00
|
|
|
{
|
|
|
|
struct udev *udev;
|
|
|
|
struct udev_enumerate *enumerate = NULL;
|
|
|
|
struct udev_list_entry *devices;
|
|
|
|
struct udev_list_entry *dev_entry;
|
2013-01-22 14:15:47 +00:00
|
|
|
virInterfacePtr *ifaces_list = NULL;
|
2012-10-06 19:20:25 +00:00
|
|
|
virInterfacePtr iface_obj;
|
|
|
|
int tmp_count;
|
|
|
|
int count = 0;
|
|
|
|
int status = 0;
|
|
|
|
int ret;
|
|
|
|
|
2013-05-21 13:29:38 +00:00
|
|
|
virCheckFlags(VIR_CONNECT_LIST_INTERFACES_FILTERS_ACTIVE, -1);
|
2012-10-06 19:20:25 +00:00
|
|
|
|
2013-04-23 10:56:22 +00:00
|
|
|
if (virConnectListAllInterfacesEnsureACL(conn) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2012-10-06 19:20:25 +00:00
|
|
|
/* Grab a udev reference */
|
2014-11-17 17:09:00 +00:00
|
|
|
udev = udev_ref(driver->udev);
|
2012-10-06 19:20:25 +00:00
|
|
|
|
|
|
|
/* List all interfaces in case we support more filter flags in the future */
|
2013-04-23 12:50:18 +00:00
|
|
|
enumerate = udevGetDevices(udev, VIR_UDEV_IFACE_ALL);
|
2012-10-06 19:20:25 +00:00
|
|
|
|
|
|
|
if (!enumerate) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("failed to get list of %s interfaces on host"),
|
|
|
|
virUdevStatusString(status));
|
|
|
|
ret = -1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do the scan to load up the enumeration */
|
|
|
|
udev_enumerate_scan_devices(enumerate);
|
|
|
|
|
|
|
|
/* Get a list we can walk */
|
|
|
|
devices = udev_enumerate_get_list_entry(enumerate);
|
|
|
|
|
|
|
|
/* For each item so we can count */
|
|
|
|
udev_list_entry_foreach(dev_entry, devices) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we've got nothing, exit out */
|
|
|
|
if (count == 0) {
|
|
|
|
ret = 0;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we're asked for the ifaces then alloc up memory */
|
2013-07-04 10:10:05 +00:00
|
|
|
if (ifaces && VIR_ALLOC_N(ifaces_list, count + 1) < 0) {
|
|
|
|
ret = -1;
|
|
|
|
goto cleanup;
|
2012-10-06 19:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a list we can walk */
|
|
|
|
devices = udev_enumerate_get_list_entry(enumerate);
|
|
|
|
|
|
|
|
/* reset our iterator */
|
|
|
|
count = 0;
|
|
|
|
|
|
|
|
/* Walk through each device */
|
|
|
|
udev_list_entry_foreach(dev_entry, devices) {
|
|
|
|
struct udev_device *dev;
|
|
|
|
const char *path;
|
|
|
|
const char *name;
|
|
|
|
const char *macaddr;
|
2013-06-27 16:07:22 +00:00
|
|
|
virInterfaceDefPtr def;
|
2012-10-06 19:20:25 +00:00
|
|
|
|
|
|
|
path = udev_list_entry_get_name(dev_entry);
|
|
|
|
dev = udev_device_new_from_syspath(udev, path);
|
|
|
|
name = udev_device_get_sysname(dev);
|
|
|
|
macaddr = udev_device_get_sysattr_value(dev, "address");
|
|
|
|
status = STREQ(udev_device_get_sysattr_value(dev, "operstate"), "up");
|
|
|
|
|
2013-06-27 16:07:22 +00:00
|
|
|
def = udevGetMinimalDefForDevice(dev);
|
|
|
|
if (!virConnectListAllInterfacesCheckACL(conn, def)) {
|
|
|
|
udev_device_unref(dev);
|
|
|
|
virInterfaceDefFree(def);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
virInterfaceDefFree(def);
|
|
|
|
|
2012-10-06 19:20:25 +00:00
|
|
|
/* Filter the results */
|
2013-05-21 13:29:38 +00:00
|
|
|
if (MATCH(VIR_CONNECT_LIST_INTERFACES_FILTERS_ACTIVE) &&
|
|
|
|
!((MATCH(VIR_CONNECT_LIST_INTERFACES_ACTIVE) && status) ||
|
|
|
|
(MATCH(VIR_CONNECT_LIST_INTERFACES_INACTIVE) && !status))) {
|
|
|
|
udev_device_unref(dev);
|
|
|
|
continue;
|
|
|
|
}
|
2012-10-06 19:20:25 +00:00
|
|
|
|
|
|
|
/* If we matched a filter, then add it */
|
2013-05-21 13:29:38 +00:00
|
|
|
if (ifaces) {
|
|
|
|
iface_obj = virGetInterface(conn, name, macaddr);
|
|
|
|
ifaces_list[count++] = iface_obj;
|
2012-10-06 19:20:25 +00:00
|
|
|
}
|
2013-02-18 04:26:38 +00:00
|
|
|
udev_device_unref(dev);
|
2012-10-06 19:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Drop our refcounts */
|
|
|
|
udev_enumerate_unref(enumerate);
|
|
|
|
udev_unref(udev);
|
|
|
|
|
|
|
|
/* Trim the array to its final size */
|
|
|
|
if (ifaces) {
|
|
|
|
ignore_value(VIR_REALLOC_N(ifaces_list, count + 1));
|
|
|
|
*ifaces = ifaces_list;
|
2013-05-21 13:29:38 +00:00
|
|
|
ifaces_list = NULL;
|
2012-10-06 19:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
|
2014-03-25 06:57:22 +00:00
|
|
|
cleanup:
|
2012-10-06 19:20:25 +00:00
|
|
|
if (enumerate)
|
|
|
|
udev_enumerate_unref(enumerate);
|
|
|
|
udev_unref(udev);
|
|
|
|
|
|
|
|
if (ifaces) {
|
|
|
|
for (tmp_count = 0; tmp_count < count; tmp_count++)
|
2014-11-30 17:07:19 +00:00
|
|
|
virObjectUnref(ifaces_list[tmp_count]);
|
2012-10-06 19:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VIR_FREE(ifaces_list);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static virInterfacePtr
|
2013-04-23 12:50:18 +00:00
|
|
|
udevInterfaceLookupByName(virConnectPtr conn, const char *name)
|
2012-10-06 19:20:25 +00:00
|
|
|
{
|
2014-11-17 17:09:00 +00:00
|
|
|
struct udev *udev = udev_ref(driver->udev);
|
2012-10-06 19:20:25 +00:00
|
|
|
struct udev_device *dev;
|
|
|
|
virInterfacePtr ret = NULL;
|
2013-04-23 10:56:22 +00:00
|
|
|
virInterfaceDefPtr def = NULL;
|
2012-10-06 19:20:25 +00:00
|
|
|
|
|
|
|
/* get a device reference based on the device name */
|
|
|
|
dev = udev_device_new_from_subsystem_sysname(udev, "net", name);
|
|
|
|
if (!dev) {
|
|
|
|
virReportError(VIR_ERR_NO_INTERFACE,
|
|
|
|
_("couldn't find interface named '%s'"),
|
|
|
|
name);
|
2013-04-29 17:37:48 +00:00
|
|
|
goto cleanup;
|
2012-10-06 19:20:25 +00:00
|
|
|
}
|
|
|
|
|
2013-04-23 10:56:22 +00:00
|
|
|
if (!(def = udevGetMinimalDefForDevice(dev)))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virInterfaceLookupByNameEnsureACL(conn, def) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = virGetInterface(conn, def->name, def->mac);
|
2012-10-06 19:20:25 +00:00
|
|
|
udev_device_unref(dev);
|
|
|
|
|
2014-03-25 06:57:22 +00:00
|
|
|
cleanup:
|
2012-10-06 19:20:25 +00:00
|
|
|
udev_unref(udev);
|
2013-04-23 10:56:22 +00:00
|
|
|
virInterfaceDefFree(def);
|
2012-10-06 19:20:25 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static virInterfacePtr
|
2013-04-23 12:50:18 +00:00
|
|
|
udevInterfaceLookupByMACString(virConnectPtr conn, const char *macstr)
|
2012-10-06 19:20:25 +00:00
|
|
|
{
|
2014-11-17 17:09:00 +00:00
|
|
|
struct udev *udev = udev_ref(driver->udev);
|
2012-10-06 19:20:25 +00:00
|
|
|
struct udev_enumerate *enumerate = NULL;
|
|
|
|
struct udev_list_entry *dev_entry;
|
|
|
|
struct udev_device *dev;
|
2013-04-23 10:56:22 +00:00
|
|
|
virInterfaceDefPtr def = NULL;
|
2012-10-06 19:20:25 +00:00
|
|
|
virInterfacePtr ret = NULL;
|
|
|
|
|
2013-04-23 12:50:18 +00:00
|
|
|
enumerate = udevGetDevices(udev, VIR_UDEV_IFACE_ALL);
|
2012-10-06 19:20:25 +00:00
|
|
|
|
|
|
|
if (!enumerate) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("failed to lookup interface with MAC address '%s'"),
|
|
|
|
macstr);
|
2013-04-29 17:37:48 +00:00
|
|
|
goto cleanup;
|
2012-10-06 19:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Match on MAC */
|
|
|
|
udev_enumerate_add_match_sysattr(enumerate, "address", macstr);
|
|
|
|
|
|
|
|
/* Do the scan to load up the enumeration */
|
|
|
|
udev_enumerate_scan_devices(enumerate);
|
|
|
|
|
|
|
|
/* Get a list we can walk */
|
|
|
|
dev_entry = udev_enumerate_get_list_entry(enumerate);
|
|
|
|
|
|
|
|
/* Check that we got something back */
|
|
|
|
if (!dev_entry) {
|
|
|
|
virReportError(VIR_ERR_NO_INTERFACE,
|
|
|
|
_("couldn't find interface with MAC address '%s'"),
|
|
|
|
macstr);
|
2013-04-29 17:37:48 +00:00
|
|
|
goto cleanup;
|
2012-10-06 19:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that we didn't get multiple items back */
|
|
|
|
if (udev_list_entry_get_next(dev_entry)) {
|
|
|
|
virReportError(VIR_ERR_MULTIPLE_INTERFACES,
|
|
|
|
_("the MAC address '%s' matches multiple interfaces"),
|
|
|
|
macstr);
|
2013-04-29 17:37:48 +00:00
|
|
|
goto cleanup;
|
2012-10-06 19:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dev = udev_device_new_from_syspath(udev, udev_list_entry_get_name(dev_entry));
|
2013-04-23 10:56:22 +00:00
|
|
|
|
|
|
|
if (!(def = udevGetMinimalDefForDevice(dev)))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virInterfaceLookupByMACStringEnsureACL(conn, def) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = virGetInterface(conn, def->name, def->mac);
|
2012-10-06 19:20:25 +00:00
|
|
|
udev_device_unref(dev);
|
|
|
|
|
2014-03-25 06:57:22 +00:00
|
|
|
cleanup:
|
2012-10-06 19:20:25 +00:00
|
|
|
if (enumerate)
|
|
|
|
udev_enumerate_unref(enumerate);
|
|
|
|
udev_unref(udev);
|
2013-04-23 10:56:22 +00:00
|
|
|
virInterfaceDefFree(def);
|
2012-10-06 19:20:25 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-02-17 21:34:17 +00:00
|
|
|
/**
|
|
|
|
* Helper function for finding bond slaves using scandir()
|
|
|
|
*
|
|
|
|
* @param entry - directory entry passed by scandir()
|
|
|
|
*
|
|
|
|
* @return 1 if we want to add it to scandir's list, 0 if not.
|
|
|
|
*/
|
|
|
|
static int
|
2013-04-23 12:50:18 +00:00
|
|
|
udevBondScanDirFilter(const struct dirent *entry)
|
2013-02-17 21:34:17 +00:00
|
|
|
{
|
|
|
|
/* This is ugly so if anyone has a better suggestion, please improve
|
|
|
|
* this. Unfortunately the kernel stores everything in the top level
|
|
|
|
* interface sysfs entry and references the slaves as slave_eth0 for
|
|
|
|
* example.
|
|
|
|
*/
|
|
|
|
if (STRPREFIX(entry->d_name, "slave_"))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-14 22:02:33 +00:00
|
|
|
/**
|
2013-02-15 16:49:08 +00:00
|
|
|
* Helper function for finding bridge members using scandir()
|
2012-10-14 22:02:33 +00:00
|
|
|
*
|
|
|
|
* @param entry - directory entry passed by scandir()
|
|
|
|
*
|
|
|
|
* @return 1 if we want to add it to scandir's list, 0 if not.
|
|
|
|
*/
|
|
|
|
static int
|
2013-04-23 12:50:18 +00:00
|
|
|
udevBridgeScanDirFilter(const struct dirent *entry)
|
2012-10-14 22:02:33 +00:00
|
|
|
{
|
|
|
|
if (STREQ(entry->d_name, ".") || STREQ(entry->d_name, ".."))
|
|
|
|
return 0;
|
|
|
|
|
2013-02-25 02:31:03 +00:00
|
|
|
/* Omit the domain interfaces from the list of bridge attached
|
|
|
|
* devices. All we can do is check for the device name matching
|
|
|
|
* vnet%d. Improvements to this check are welcome.
|
|
|
|
*/
|
|
|
|
if (strlen(entry->d_name) >= 5) {
|
2017-04-25 18:43:24 +00:00
|
|
|
if (STRPREFIX(entry->d_name, VIR_NET_GENERATED_TAP_PREFIX) &&
|
2019-11-18 14:13:11 +00:00
|
|
|
g_ascii_isdigit(entry->d_name[4]))
|
2013-02-25 02:31:03 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-14 22:02:33 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-17 21:34:17 +00:00
|
|
|
static int
|
|
|
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3)
|
2019-10-14 12:25:14 +00:00
|
|
|
ATTRIBUTE_NONNULL(4) G_GNUC_WARN_UNUSED_RESULT
|
2013-04-23 12:50:18 +00:00
|
|
|
udevGetIfaceDefBond(struct udev *udev,
|
|
|
|
struct udev_device *dev,
|
|
|
|
const char *name,
|
|
|
|
virInterfaceDef *ifacedef)
|
2013-02-17 21:34:17 +00:00
|
|
|
{
|
|
|
|
struct dirent **slave_list = NULL;
|
|
|
|
int slave_count = 0;
|
Convert 'int i' to 'size_t i' in src/interface/ 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;
|
2013-02-17 21:34:17 +00:00
|
|
|
const char *tmp_str;
|
|
|
|
int tmp_int;
|
|
|
|
|
|
|
|
/* Initial defaults */
|
|
|
|
ifacedef->data.bond.target = NULL;
|
|
|
|
ifacedef->data.bond.nbItf = 0;
|
|
|
|
ifacedef->data.bond.itf = NULL;
|
|
|
|
|
|
|
|
/* Set the bond specifics */
|
|
|
|
tmp_str = udev_device_get_sysattr_value(dev, "bonding/downdelay");
|
|
|
|
if (!tmp_str) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Could not retrieve 'bonding/downdelay' for '%s'"), name);
|
2013-05-09 16:09:12 +00:00
|
|
|
goto error;
|
2013-02-17 21:34:17 +00:00
|
|
|
}
|
|
|
|
if (virStrToLong_i(tmp_str, NULL, 10, &tmp_int) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Could not parse 'bonding/downdelay' '%s' for '%s'"),
|
|
|
|
tmp_str, name);
|
2013-05-09 16:09:12 +00:00
|
|
|
goto error;
|
2013-02-17 21:34:17 +00:00
|
|
|
}
|
|
|
|
ifacedef->data.bond.downdelay = tmp_int;
|
|
|
|
|
|
|
|
tmp_str = udev_device_get_sysattr_value(dev, "bonding/updelay");
|
|
|
|
if (!tmp_str) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Could not retrieve 'bonding/updelay' for '%s'"), name);
|
2013-05-09 16:09:12 +00:00
|
|
|
goto error;
|
2013-02-17 21:34:17 +00:00
|
|
|
}
|
|
|
|
if (virStrToLong_i(tmp_str, NULL, 10, &tmp_int) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Could not parse 'bonding/updelay' '%s' for '%s'"),
|
|
|
|
tmp_str, name);
|
2013-05-09 16:09:12 +00:00
|
|
|
goto error;
|
2013-02-17 21:34:17 +00:00
|
|
|
}
|
|
|
|
ifacedef->data.bond.updelay = tmp_int;
|
|
|
|
|
|
|
|
tmp_str = udev_device_get_sysattr_value(dev, "bonding/miimon");
|
|
|
|
if (!tmp_str) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Could not retrieve 'bonding/miimon' for '%s'"), name);
|
2013-05-09 16:09:12 +00:00
|
|
|
goto error;
|
2013-02-17 21:34:17 +00:00
|
|
|
}
|
|
|
|
if (virStrToLong_i(tmp_str, NULL, 10, &tmp_int) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Could not parse 'bonding/miimon' '%s' for '%s'"),
|
|
|
|
tmp_str, name);
|
2013-05-09 16:09:12 +00:00
|
|
|
goto error;
|
2013-02-17 21:34:17 +00:00
|
|
|
}
|
|
|
|
ifacedef->data.bond.frequency = tmp_int;
|
|
|
|
|
|
|
|
tmp_str = udev_device_get_sysattr_value(dev, "bonding/arp_interval");
|
|
|
|
if (!tmp_str) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Could not retrieve 'bonding/arp_interval' for '%s'"), name);
|
2013-05-09 16:09:12 +00:00
|
|
|
goto error;
|
2013-02-17 21:34:17 +00:00
|
|
|
}
|
|
|
|
if (virStrToLong_i(tmp_str, NULL, 10, &tmp_int) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Could not parse 'bonding/arp_interval' '%s' for '%s'"),
|
|
|
|
tmp_str, name);
|
2013-05-09 16:09:12 +00:00
|
|
|
goto error;
|
2013-02-17 21:34:17 +00:00
|
|
|
}
|
|
|
|
ifacedef->data.bond.interval = tmp_int;
|
|
|
|
|
|
|
|
/* bonding/mode is in the format: "balance-rr 0" so we find the
|
|
|
|
* space and increment the pointer to get the number and convert
|
|
|
|
* it to an interger. libvirt uses 1 through 7 while the raw
|
|
|
|
* number is 0 through 6 so increment it by 1.
|
|
|
|
*/
|
|
|
|
tmp_str = udev_device_get_sysattr_value(dev, "bonding/mode");
|
|
|
|
if (!tmp_str) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Could not retrieve 'bonding/mode' for '%s'"), name);
|
2013-05-09 16:09:12 +00:00
|
|
|
goto error;
|
2013-02-17 21:34:17 +00:00
|
|
|
}
|
|
|
|
tmp_str = strchr(tmp_str, ' ');
|
|
|
|
if (!tmp_str) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Invalid format for 'bonding/mode' for '%s'"), name);
|
2013-05-09 16:09:12 +00:00
|
|
|
goto error;
|
2013-02-17 21:34:17 +00:00
|
|
|
}
|
|
|
|
if (strlen(tmp_str) < 2) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to find correct value in 'bonding/mode' for '%s'"),
|
|
|
|
name);
|
2013-05-09 16:09:12 +00:00
|
|
|
goto error;
|
2013-02-17 21:34:17 +00:00
|
|
|
}
|
|
|
|
if (virStrToLong_i(tmp_str + 1, NULL, 10, &tmp_int) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Could not parse 'bonding/mode' '%s' for '%s'"),
|
|
|
|
tmp_str, name);
|
2013-05-09 16:09:12 +00:00
|
|
|
goto error;
|
2013-02-17 21:34:17 +00:00
|
|
|
}
|
|
|
|
ifacedef->data.bond.mode = tmp_int + 1;
|
|
|
|
|
|
|
|
/* bonding/arp_validate is in the format: "none 0" so we find the
|
|
|
|
* space and increment the pointer to get the number and convert
|
|
|
|
* it to an interger.
|
|
|
|
*/
|
|
|
|
tmp_str = udev_device_get_sysattr_value(dev, "bonding/arp_validate");
|
|
|
|
if (!tmp_str) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Could not retrieve 'bonding/arp_validate' for '%s'"), name);
|
2013-05-09 16:09:12 +00:00
|
|
|
goto error;
|
2013-02-17 21:34:17 +00:00
|
|
|
}
|
|
|
|
tmp_str = strchr(tmp_str, ' ');
|
|
|
|
if (!tmp_str) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Invalid format for 'bonding/arp_validate' for '%s'"), name);
|
2013-05-09 16:09:12 +00:00
|
|
|
goto error;
|
2013-02-17 21:34:17 +00:00
|
|
|
}
|
|
|
|
if (strlen(tmp_str) < 2) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to find correct value in 'bonding/arp_validate' "
|
|
|
|
"for '%s'"), name);
|
2013-05-09 16:09:12 +00:00
|
|
|
goto error;
|
2013-02-17 21:34:17 +00:00
|
|
|
}
|
|
|
|
if (virStrToLong_i(tmp_str + 1, NULL, 10, &tmp_int) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Could not parse 'bonding/arp_validate' '%s' for '%s'"),
|
|
|
|
tmp_str, name);
|
2013-05-09 16:09:12 +00:00
|
|
|
goto error;
|
2013-02-17 21:34:17 +00:00
|
|
|
}
|
|
|
|
ifacedef->data.bond.validate = tmp_int;
|
|
|
|
|
|
|
|
/* bonding/use_carrier is 0 or 1 and libvirt stores it as 1 or 2. */
|
|
|
|
tmp_str = udev_device_get_sysattr_value(dev, "bonding/use_carrier");
|
|
|
|
if (!tmp_str) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Could not retrieve 'bonding/use_carrier' for '%s'"), name);
|
2013-05-09 16:09:12 +00:00
|
|
|
goto error;
|
2013-02-17 21:34:17 +00:00
|
|
|
}
|
|
|
|
if (virStrToLong_i(tmp_str, NULL, 10, &tmp_int) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Could not parse 'bonding/use_carrier' '%s' for '%s'"),
|
|
|
|
tmp_str, name);
|
2013-05-09 16:09:12 +00:00
|
|
|
goto error;
|
2013-02-17 21:34:17 +00:00
|
|
|
}
|
|
|
|
ifacedef->data.bond.carrier = tmp_int + 1;
|
|
|
|
|
|
|
|
/* MII or ARP Monitoring is based on arp_interval and miimon.
|
|
|
|
* if arp_interval > 0 then ARP monitoring is in play, if
|
|
|
|
* miimon > 0 then MII monitoring is in play.
|
|
|
|
*/
|
|
|
|
if (ifacedef->data.bond.interval > 0)
|
|
|
|
ifacedef->data.bond.monit = VIR_INTERFACE_BOND_MONIT_ARP;
|
|
|
|
else if (ifacedef->data.bond.frequency > 0)
|
|
|
|
ifacedef->data.bond.monit = VIR_INTERFACE_BOND_MONIT_MII;
|
|
|
|
else
|
|
|
|
ifacedef->data.bond.monit = VIR_INTERFACE_BOND_MONIT_NONE;
|
|
|
|
|
|
|
|
tmp_str = udev_device_get_sysattr_value(dev, "bonding/arp_ip_target");
|
|
|
|
if (!tmp_str) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Could not retrieve 'bonding/arp_ip_target' for '%s'"), name);
|
2013-05-09 16:09:12 +00:00
|
|
|
goto error;
|
2013-02-17 21:34:17 +00:00
|
|
|
}
|
2019-10-20 11:49:46 +00:00
|
|
|
ifacedef->data.bond.target = g_strdup(tmp_str);
|
2013-02-17 21:34:17 +00:00
|
|
|
|
|
|
|
/* Slaves of the bond */
|
|
|
|
/* Get each slave in the bond */
|
|
|
|
slave_count = scandir(udev_device_get_syspath(dev), &slave_list,
|
2013-04-23 12:50:18 +00:00
|
|
|
udevBondScanDirFilter, alphasort);
|
2013-02-17 21:34:17 +00:00
|
|
|
|
|
|
|
if (slave_count < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Could not get slaves of bond '%s'"), name);
|
2013-05-09 16:09:12 +00:00
|
|
|
goto error;
|
2013-02-17 21:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate our list of slave devices */
|
2013-07-04 10:10:05 +00:00
|
|
|
if (VIR_ALLOC_N(ifacedef->data.bond.itf, slave_count) < 0)
|
2013-05-09 16:09:12 +00:00
|
|
|
goto error;
|
2013-02-17 21:34:17 +00:00
|
|
|
ifacedef->data.bond.nbItf = slave_count;
|
|
|
|
|
|
|
|
for (i = 0; i < slave_count; i++) {
|
|
|
|
/* Names are slave_interface. e.g. slave_eth0
|
|
|
|
* so we use the part after the _
|
|
|
|
*/
|
|
|
|
tmp_str = strchr(slave_list[i]->d_name, '_');
|
2013-02-26 06:27:19 +00:00
|
|
|
if (!tmp_str || strlen(tmp_str) < 2) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Invalid enslaved interface name '%s' seen for "
|
|
|
|
"bond '%s'"), slave_list[i]->d_name, name);
|
2013-05-09 16:09:12 +00:00
|
|
|
goto error;
|
2013-02-26 06:27:19 +00:00
|
|
|
}
|
|
|
|
/* go past the _ */
|
2013-02-17 21:34:17 +00:00
|
|
|
tmp_str++;
|
|
|
|
|
|
|
|
ifacedef->data.bond.itf[i] =
|
2013-04-23 12:50:18 +00:00
|
|
|
udevGetIfaceDef(udev, tmp_str);
|
2013-02-17 21:34:17 +00:00
|
|
|
if (!ifacedef->data.bond.itf[i]) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Could not get interface information for '%s', which is "
|
|
|
|
"a enslaved in bond '%s'"), slave_list[i]->d_name, name);
|
2013-05-09 16:09:12 +00:00
|
|
|
goto error;
|
2013-02-17 21:34:17 +00:00
|
|
|
}
|
|
|
|
VIR_FREE(slave_list[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_FREE(slave_list);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2014-03-25 06:57:22 +00:00
|
|
|
error:
|
2014-11-13 14:27:11 +00:00
|
|
|
for (i = 0; slave_count != -1 && i < slave_count; i++)
|
2013-02-17 21:34:17 +00:00
|
|
|
VIR_FREE(slave_list[i]);
|
|
|
|
VIR_FREE(slave_list);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-02-15 16:49:08 +00:00
|
|
|
static int
|
|
|
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3)
|
2019-10-14 12:25:14 +00:00
|
|
|
ATTRIBUTE_NONNULL(4) G_GNUC_WARN_UNUSED_RESULT
|
2013-04-23 12:50:18 +00:00
|
|
|
udevGetIfaceDefBridge(struct udev *udev,
|
|
|
|
struct udev_device *dev,
|
|
|
|
const char *name,
|
|
|
|
virInterfaceDef *ifacedef)
|
2013-02-15 16:49:08 +00:00
|
|
|
{
|
|
|
|
struct dirent **member_list = NULL;
|
|
|
|
int member_count = 0;
|
|
|
|
char *member_path;
|
2013-02-20 06:39:37 +00:00
|
|
|
const char *tmp_str;
|
2013-02-15 16:49:08 +00:00
|
|
|
int stp;
|
Convert 'int i' to 'size_t i' in src/interface/ 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;
|
2013-02-15 16:49:08 +00:00
|
|
|
|
|
|
|
/* Set our type to Bridge */
|
|
|
|
ifacedef->type = VIR_INTERFACE_TYPE_BRIDGE;
|
|
|
|
|
2013-02-20 06:39:37 +00:00
|
|
|
/* Retrieve the forward delay */
|
|
|
|
tmp_str = udev_device_get_sysattr_value(dev, "bridge/forward_delay");
|
|
|
|
if (!tmp_str) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Could not retrieve 'bridge/forward_delay' for '%s'"), name);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2019-10-20 11:49:46 +00:00
|
|
|
ifacedef->data.bridge.delay = g_strdup(tmp_str);
|
2013-02-15 16:49:08 +00:00
|
|
|
|
2013-02-20 06:39:37 +00:00
|
|
|
/* Retrieve Spanning Tree State. Valid values = -1, 0, 1 */
|
|
|
|
tmp_str = udev_device_get_sysattr_value(dev, "bridge/stp_state");
|
|
|
|
if (!tmp_str) {
|
2013-02-15 16:49:08 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2013-02-20 06:39:37 +00:00
|
|
|
_("Could not retrieve 'bridge/stp_state' for '%s'"), name);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virStrToLong_i(tmp_str, NULL, 10, &stp) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Could not parse 'bridge/stp_state' '%s' for '%s'"),
|
|
|
|
tmp_str, name);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (stp) {
|
|
|
|
case -1:
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
ifacedef->data.bridge.stp = stp;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Invalid STP state value %d received for '%s'. Must be "
|
|
|
|
"-1, 0, or 1."), stp, name);
|
|
|
|
goto error;
|
2013-02-15 16:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Members of the bridge */
|
2019-10-22 13:26:14 +00:00
|
|
|
member_path = g_strdup_printf("%s/%s", udev_device_get_syspath(dev), "brif");
|
2013-02-15 16:49:08 +00:00
|
|
|
|
|
|
|
/* Get each member of the bridge */
|
|
|
|
member_count = scandir(member_path, &member_list,
|
2013-04-23 12:50:18 +00:00
|
|
|
udevBridgeScanDirFilter, alphasort);
|
2013-02-15 16:49:08 +00:00
|
|
|
|
|
|
|
/* Don't need the path anymore */
|
|
|
|
VIR_FREE(member_path);
|
|
|
|
|
|
|
|
if (member_count < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Could not get members of bridge '%s'"),
|
|
|
|
name);
|
2013-02-20 06:39:37 +00:00
|
|
|
goto error;
|
2013-02-15 16:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate our list of member devices */
|
2013-07-04 10:10:05 +00:00
|
|
|
if (VIR_ALLOC_N(ifacedef->data.bridge.itf, member_count) < 0)
|
2013-02-20 06:39:37 +00:00
|
|
|
goto error;
|
2013-02-15 16:49:08 +00:00
|
|
|
ifacedef->data.bridge.nbItf = member_count;
|
|
|
|
|
2016-09-22 10:33:24 +00:00
|
|
|
/* Get the interface definitions for each member of the bridge */
|
2013-02-15 16:49:08 +00:00
|
|
|
for (i = 0; i < member_count; i++) {
|
|
|
|
ifacedef->data.bridge.itf[i] =
|
2013-04-23 12:50:18 +00:00
|
|
|
udevGetIfaceDef(udev, member_list[i]->d_name);
|
2013-02-20 06:39:37 +00:00
|
|
|
if (!ifacedef->data.bridge.itf[i]) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Could not get interface information for '%s', which is "
|
|
|
|
"a member of bridge '%s'"), member_list[i]->d_name, name);
|
|
|
|
goto error;
|
|
|
|
}
|
2013-02-15 16:49:08 +00:00
|
|
|
VIR_FREE(member_list[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_FREE(member_list);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2014-03-25 06:57:22 +00:00
|
|
|
error:
|
2014-11-13 14:27:11 +00:00
|
|
|
for (i = 0; member_count != -1 && i < member_count; i++)
|
2013-02-15 16:49:08 +00:00
|
|
|
VIR_FREE(member_list[i]);
|
|
|
|
VIR_FREE(member_list);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2012-10-14 22:02:33 +00:00
|
|
|
|
2013-02-15 22:32:34 +00:00
|
|
|
static int
|
|
|
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3)
|
2019-10-14 12:25:14 +00:00
|
|
|
ATTRIBUTE_NONNULL(4) G_GNUC_WARN_UNUSED_RESULT
|
2019-10-14 12:45:33 +00:00
|
|
|
udevGetIfaceDefVlan(struct udev *udev G_GNUC_UNUSED,
|
|
|
|
struct udev_device *dev G_GNUC_UNUSED,
|
2013-04-23 12:50:18 +00:00
|
|
|
const char *name,
|
|
|
|
virInterfaceDef *ifacedef)
|
2013-02-15 22:32:34 +00:00
|
|
|
{
|
2014-04-24 12:58:47 +00:00
|
|
|
char *procpath = NULL;
|
|
|
|
char *buf = NULL;
|
|
|
|
char *vid_pos, *dev_pos;
|
|
|
|
size_t vid_len, dev_len;
|
|
|
|
const char *vid_prefix = "VID: ";
|
|
|
|
const char *dev_prefix = "\nDevice: ";
|
|
|
|
int ret = -1;
|
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
procpath = g_strdup_printf("/proc/net/vlan/%s", name);
|
2014-04-24 12:58:47 +00:00
|
|
|
|
|
|
|
if (virFileReadAll(procpath, BUFSIZ, &buf) < 0)
|
|
|
|
goto cleanup;
|
2013-02-15 22:32:34 +00:00
|
|
|
|
2014-04-24 12:58:47 +00:00
|
|
|
if ((vid_pos = strstr(buf, vid_prefix)) == NULL) {
|
2013-02-15 22:32:34 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2013-04-29 17:10:57 +00:00
|
|
|
_("failed to find the VID for the VLAN device '%s'"),
|
|
|
|
name);
|
2014-04-24 12:58:47 +00:00
|
|
|
goto cleanup;
|
2013-04-29 17:10:57 +00:00
|
|
|
}
|
2014-04-24 12:58:47 +00:00
|
|
|
vid_pos += strlen(vid_prefix);
|
2013-02-15 22:32:34 +00:00
|
|
|
|
2014-04-24 12:58:47 +00:00
|
|
|
if ((vid_len = strspn(vid_pos, "0123456789")) == 0 ||
|
2019-11-18 14:10:02 +00:00
|
|
|
!g_ascii_isspace(vid_pos[vid_len])) {
|
2014-04-24 12:58:47 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("failed to find the VID for the VLAN device '%s'"),
|
|
|
|
name);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2013-02-15 22:32:34 +00:00
|
|
|
|
2014-04-24 12:58:47 +00:00
|
|
|
if ((dev_pos = strstr(vid_pos + vid_len, dev_prefix)) == NULL) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("failed to find the real device for the VLAN device '%s'"),
|
|
|
|
name);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
dev_pos += strlen(dev_prefix);
|
2013-02-15 22:32:34 +00:00
|
|
|
|
2014-04-24 12:58:47 +00:00
|
|
|
if ((dev_len = strcspn(dev_pos, "\n")) == 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("failed to find the real device for the VLAN device '%s'"),
|
|
|
|
name);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2013-02-15 22:32:34 +00:00
|
|
|
|
2019-10-24 21:34:40 +00:00
|
|
|
ifacedef->data.vlan.tag = g_strndup(vid_pos, vid_len);
|
|
|
|
ifacedef->data.vlan.dev_name = g_strndup(dev_pos, dev_len);
|
2014-04-24 12:58:47 +00:00
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
VIR_FREE(procpath);
|
|
|
|
VIR_FREE(buf);
|
|
|
|
return ret;
|
2013-02-15 22:32:34 +00:00
|
|
|
}
|
|
|
|
|
2012-10-14 22:02:33 +00:00
|
|
|
static virInterfaceDef * ATTRIBUTE_NONNULL(1)
|
2013-04-23 12:50:18 +00:00
|
|
|
udevGetIfaceDef(struct udev *udev, const char *name)
|
2012-10-14 22:02:33 +00:00
|
|
|
{
|
|
|
|
struct udev_device *dev = NULL;
|
|
|
|
virInterfaceDef *ifacedef;
|
|
|
|
unsigned int mtu;
|
|
|
|
const char *mtu_str;
|
|
|
|
char *vlan_parent_dev = NULL;
|
2013-02-15 22:48:07 +00:00
|
|
|
const char *devtype;
|
2012-10-14 22:02:33 +00:00
|
|
|
|
|
|
|
/* Allocate our interface definition structure */
|
2013-07-04 10:10:05 +00:00
|
|
|
if (VIR_ALLOC(ifacedef) < 0)
|
2012-10-14 22:02:33 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Clear our structure and set safe defaults */
|
|
|
|
ifacedef->startmode = VIR_INTERFACE_START_UNSPECIFIED;
|
2019-10-20 11:49:46 +00:00
|
|
|
ifacedef->name = g_strdup(name);
|
2012-10-14 22:02:33 +00:00
|
|
|
|
|
|
|
/* Lookup the device we've been asked about */
|
|
|
|
dev = udev_device_new_from_subsystem_sysname(udev, "net", name);
|
|
|
|
if (!dev) {
|
|
|
|
virReportError(VIR_ERR_NO_INTERFACE,
|
|
|
|
_("couldn't find interface named '%s'"), name);
|
2013-05-09 16:09:12 +00:00
|
|
|
goto error;
|
2012-10-14 22:02:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* MAC address */
|
2019-10-20 11:49:46 +00:00
|
|
|
ifacedef->mac = g_strdup(udev_device_get_sysattr_value(dev, "address"));
|
2012-10-14 22:02:33 +00:00
|
|
|
|
2014-06-05 14:39:18 +00:00
|
|
|
/* Link state and speed */
|
|
|
|
if (virNetDevGetLinkInfo(ifacedef->name, &ifacedef->lnk) < 0)
|
|
|
|
goto error;
|
|
|
|
|
2012-10-14 22:02:33 +00:00
|
|
|
/* MTU */
|
|
|
|
mtu_str = udev_device_get_sysattr_value(dev, "mtu");
|
|
|
|
if (virStrToLong_ui(mtu_str, NULL, 10, &mtu) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Could not parse MTU value '%s'"), mtu_str);
|
2013-05-09 16:09:12 +00:00
|
|
|
goto error;
|
2012-10-14 22:02:33 +00:00
|
|
|
}
|
|
|
|
ifacedef->mtu = mtu;
|
|
|
|
|
|
|
|
/* Number of IP protocols this interface has assigned */
|
|
|
|
/* XXX: Do we want a netlink query or a call out to ip or leave it? */
|
|
|
|
ifacedef->nprotos = 0;
|
|
|
|
ifacedef->protos = NULL;
|
|
|
|
|
2013-02-15 22:48:07 +00:00
|
|
|
/* Check the type of device we are working with based on the devtype */
|
|
|
|
devtype = udev_device_get_devtype(dev);
|
|
|
|
|
|
|
|
/* Set our type to ethernet as the default case */
|
|
|
|
ifacedef->type = VIR_INTERFACE_TYPE_ETHERNET;
|
|
|
|
|
|
|
|
if (STREQ_NULLABLE(devtype, "vlan")) {
|
|
|
|
/* This only works on modern kernels (3.7 and newer)
|
|
|
|
* e949b09b71d975a82f13ac88ce4ad338fed213da
|
|
|
|
*/
|
|
|
|
ifacedef->type = VIR_INTERFACE_TYPE_VLAN;
|
|
|
|
} else if (STREQ_NULLABLE(devtype, "bridge")) {
|
|
|
|
ifacedef->type = VIR_INTERFACE_TYPE_BRIDGE;
|
2013-02-20 06:48:56 +00:00
|
|
|
} else if (STREQ_NULLABLE(devtype, "bond")) {
|
|
|
|
/* This only works on modern kernels (3.9 and newer) */
|
|
|
|
ifacedef->type = VIR_INTERFACE_TYPE_BOND;
|
2013-02-15 22:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Fallback checks if the devtype check didn't work. */
|
|
|
|
if (ifacedef->type == VIR_INTERFACE_TYPE_ETHERNET) {
|
|
|
|
/* First check if its a VLAN based on the name containing a dot,
|
|
|
|
* to prevent false positives
|
|
|
|
*/
|
|
|
|
vlan_parent_dev = strrchr(name, '.');
|
2014-11-13 14:27:11 +00:00
|
|
|
if (vlan_parent_dev)
|
2013-02-15 22:48:07 +00:00
|
|
|
ifacedef->type = VIR_INTERFACE_TYPE_VLAN;
|
2013-02-17 21:34:17 +00:00
|
|
|
|
|
|
|
/* Fallback check to see if this is a bond device */
|
2014-11-13 14:27:11 +00:00
|
|
|
if (udev_device_get_sysattr_value(dev, "bonding/mode"))
|
2013-02-17 21:34:17 +00:00
|
|
|
ifacedef->type = VIR_INTERFACE_TYPE_BOND;
|
2013-02-15 22:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (ifacedef->type) {
|
|
|
|
case VIR_INTERFACE_TYPE_VLAN:
|
2013-04-23 12:50:18 +00:00
|
|
|
if (udevGetIfaceDefVlan(udev, dev, name, ifacedef) < 0)
|
2013-05-09 16:09:12 +00:00
|
|
|
goto error;
|
2013-02-15 22:48:07 +00:00
|
|
|
break;
|
|
|
|
case VIR_INTERFACE_TYPE_BRIDGE:
|
2013-04-23 12:50:18 +00:00
|
|
|
if (udevGetIfaceDefBridge(udev, dev, name, ifacedef) < 0)
|
2013-05-09 16:09:12 +00:00
|
|
|
goto error;
|
2013-02-15 22:48:07 +00:00
|
|
|
break;
|
2013-02-17 21:34:17 +00:00
|
|
|
case VIR_INTERFACE_TYPE_BOND:
|
2013-04-23 12:50:18 +00:00
|
|
|
if (udevGetIfaceDefBond(udev, dev, name, ifacedef) < 0)
|
2013-05-09 16:09:12 +00:00
|
|
|
goto error;
|
2013-02-17 21:34:17 +00:00
|
|
|
break;
|
2013-02-15 22:48:07 +00:00
|
|
|
case VIR_INTERFACE_TYPE_ETHERNET:
|
|
|
|
break;
|
2012-10-14 22:02:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
udev_device_unref(dev);
|
|
|
|
|
|
|
|
return ifacedef;
|
|
|
|
|
2014-03-25 06:57:22 +00:00
|
|
|
error:
|
2012-10-14 22:02:33 +00:00
|
|
|
udev_device_unref(dev);
|
|
|
|
|
2013-04-29 17:10:57 +00:00
|
|
|
virInterfaceDefFree(ifacedef);
|
2012-10-14 22:02:33 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2013-04-23 12:50:18 +00:00
|
|
|
udevInterfaceGetXMLDesc(virInterfacePtr ifinfo,
|
|
|
|
unsigned int flags)
|
2012-10-14 22:02:33 +00:00
|
|
|
{
|
2014-11-17 17:09:00 +00:00
|
|
|
struct udev *udev = udev_ref(driver->udev);
|
2012-10-14 22:02:33 +00:00
|
|
|
virInterfaceDef *ifacedef;
|
|
|
|
char *xmlstr = NULL;
|
|
|
|
|
|
|
|
virCheckFlags(VIR_INTERFACE_XML_INACTIVE, NULL);
|
|
|
|
|
|
|
|
/* Recursively build up the interface XML based on the requested
|
|
|
|
* interface name
|
|
|
|
*/
|
2013-04-23 12:50:18 +00:00
|
|
|
ifacedef = udevGetIfaceDef(udev, ifinfo->name);
|
2012-10-14 22:02:33 +00:00
|
|
|
|
|
|
|
if (!ifacedef)
|
2013-04-29 17:37:48 +00:00
|
|
|
goto cleanup;
|
2012-10-14 22:02:33 +00:00
|
|
|
|
2013-04-23 10:56:22 +00:00
|
|
|
if (virInterfaceGetXMLDescEnsureACL(ifinfo->conn, ifacedef) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2012-10-14 22:02:33 +00:00
|
|
|
xmlstr = virInterfaceDefFormat(ifacedef);
|
|
|
|
|
2013-04-29 17:10:57 +00:00
|
|
|
virInterfaceDefFree(ifacedef);
|
2012-10-14 22:02:33 +00:00
|
|
|
|
2014-03-25 06:57:22 +00:00
|
|
|
cleanup:
|
2012-10-14 22:02:33 +00:00
|
|
|
/* decrement our udev ptr */
|
|
|
|
udev_unref(udev);
|
|
|
|
|
|
|
|
return xmlstr;
|
|
|
|
}
|
|
|
|
|
2012-10-06 19:20:29 +00:00
|
|
|
static int
|
2013-04-23 12:50:18 +00:00
|
|
|
udevInterfaceIsActive(virInterfacePtr ifinfo)
|
2012-10-06 19:20:29 +00:00
|
|
|
{
|
2014-11-17 17:09:00 +00:00
|
|
|
struct udev *udev = udev_ref(driver->udev);
|
2012-10-06 19:20:29 +00:00
|
|
|
struct udev_device *dev;
|
2013-04-23 10:56:22 +00:00
|
|
|
virInterfaceDefPtr def = NULL;
|
|
|
|
int status = -1;
|
2012-10-06 19:20:29 +00:00
|
|
|
|
|
|
|
dev = udev_device_new_from_subsystem_sysname(udev, "net",
|
|
|
|
ifinfo->name);
|
|
|
|
if (!dev) {
|
|
|
|
virReportError(VIR_ERR_NO_INTERFACE,
|
|
|
|
_("couldn't find interface named '%s'"),
|
|
|
|
ifinfo->name);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2013-04-23 10:56:22 +00:00
|
|
|
if (!(def = udevGetMinimalDefForDevice(dev)))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virInterfaceIsActiveEnsureACL(ifinfo->conn, def) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2012-10-06 19:20:29 +00:00
|
|
|
/* Check if it's active or not */
|
|
|
|
status = STREQ(udev_device_get_sysattr_value(dev, "operstate"), "up");
|
|
|
|
|
|
|
|
udev_device_unref(dev);
|
|
|
|
|
2014-03-25 06:57:22 +00:00
|
|
|
cleanup:
|
2012-10-06 19:20:29 +00:00
|
|
|
udev_unref(udev);
|
2013-04-23 10:56:22 +00:00
|
|
|
virInterfaceDefFree(def);
|
2012-10-06 19:20:29 +00:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2014-11-17 17:09:00 +00:00
|
|
|
|
2019-05-23 10:34:08 +00:00
|
|
|
static int
|
|
|
|
udevStateCleanup(void);
|
|
|
|
|
2014-11-17 17:09:00 +00:00
|
|
|
static int
|
2018-01-26 11:16:00 +00:00
|
|
|
udevStateInitialize(bool privileged,
|
2019-05-17 11:30:45 +00:00
|
|
|
const char *root,
|
2019-10-14 12:45:33 +00:00
|
|
|
virStateInhibitCallback callback G_GNUC_UNUSED,
|
|
|
|
void *opaque G_GNUC_UNUSED)
|
2014-11-17 17:09:00 +00:00
|
|
|
{
|
2019-07-23 12:05:18 +00:00
|
|
|
int ret = VIR_DRV_STATE_INIT_ERROR;
|
2014-11-17 17:09:00 +00:00
|
|
|
|
2019-05-17 11:30:45 +00:00
|
|
|
if (root != NULL) {
|
|
|
|
virReportError(VIR_ERR_INVALID_ARG, "%s",
|
|
|
|
_("Driver does not support embedded mode"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-17 17:09:00 +00:00
|
|
|
if (VIR_ALLOC(driver) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2019-05-23 10:34:08 +00:00
|
|
|
driver->lockFD = -1;
|
|
|
|
|
|
|
|
if (privileged) {
|
2019-10-22 13:26:14 +00:00
|
|
|
driver->stateDir = g_strdup_printf("%s/libvirt/interface", RUNSTATEDIR);
|
2019-05-23 10:34:08 +00:00
|
|
|
} else {
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *rundir = NULL;
|
2019-05-23 10:34:08 +00:00
|
|
|
|
2019-12-19 09:39:32 +00:00
|
|
|
rundir = virGetUserRuntimeDirectory();
|
2019-10-22 13:26:14 +00:00
|
|
|
driver->stateDir = g_strdup_printf("%s/interface/run", rundir);
|
2019-05-23 10:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (virFileMakePathWithMode(driver->stateDir, S_IRWXU) < 0) {
|
|
|
|
virReportSystemError(errno, _("cannot create state directory '%s'"),
|
|
|
|
driver->stateDir);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((driver->lockFD =
|
2019-07-11 14:57:34 +00:00
|
|
|
virPidFileAcquire(driver->stateDir, "driver", false, getpid())) < 0)
|
2019-05-23 10:34:08 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2014-11-17 17:09:00 +00:00
|
|
|
driver->udev = udev_new();
|
|
|
|
if (!driver->udev) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("failed to create udev context"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2018-01-26 11:16:00 +00:00
|
|
|
driver->privileged = privileged;
|
2014-11-17 17:09:00 +00:00
|
|
|
|
2019-07-23 12:05:18 +00:00
|
|
|
ret = VIR_DRV_STATE_INIT_COMPLETE;
|
2014-11-17 17:09:00 +00:00
|
|
|
|
|
|
|
cleanup:
|
2019-05-23 10:34:08 +00:00
|
|
|
if (ret < 0)
|
|
|
|
udevStateCleanup();
|
2014-11-17 17:09:00 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
udevStateCleanup(void)
|
|
|
|
{
|
|
|
|
if (!driver)
|
|
|
|
return -1;
|
|
|
|
|
2019-05-23 10:34:08 +00:00
|
|
|
if (driver->udev)
|
|
|
|
udev_unref(driver->udev);
|
|
|
|
|
|
|
|
if (driver->lockFD != -1)
|
|
|
|
virPidFileRelease(driver->stateDir, "driver", driver->lockFD);
|
2014-11-17 17:09:00 +00:00
|
|
|
|
2019-05-23 10:34:08 +00:00
|
|
|
VIR_FREE(driver->stateDir);
|
2014-11-17 17:09:00 +00:00
|
|
|
VIR_FREE(driver);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-26 11:16:00 +00:00
|
|
|
static virDrvOpenStatus
|
|
|
|
udevConnectOpen(virConnectPtr conn,
|
2019-10-14 12:45:33 +00:00
|
|
|
virConnectAuthPtr auth G_GNUC_UNUSED,
|
|
|
|
virConfPtr conf G_GNUC_UNUSED,
|
2018-01-26 11:16:00 +00:00
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);
|
|
|
|
|
2018-03-28 11:49:29 +00:00
|
|
|
if (driver == NULL) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("interface state driver is not active"));
|
|
|
|
return VIR_DRV_OPEN_ERROR;
|
|
|
|
}
|
|
|
|
|
2019-09-26 14:56:38 +00:00
|
|
|
if (!virConnectValidateURIPath(conn->uri->path,
|
|
|
|
"interface",
|
|
|
|
driver->privileged))
|
|
|
|
return VIR_DRV_OPEN_ERROR;
|
2018-01-26 11:16:00 +00:00
|
|
|
|
|
|
|
if (virConnectOpenEnsureACL(conn) < 0)
|
|
|
|
return VIR_DRV_OPEN_ERROR;
|
|
|
|
|
|
|
|
return VIR_DRV_OPEN_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2019-10-14 12:45:33 +00:00
|
|
|
static int udevConnectClose(virConnectPtr conn G_GNUC_UNUSED)
|
2018-01-26 11:16:00 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-14 12:45:33 +00:00
|
|
|
static int udevConnectIsSecure(virConnectPtr conn G_GNUC_UNUSED)
|
2018-01-26 11:16:00 +00:00
|
|
|
{
|
|
|
|
/* Trivially secure, since always inside the daemon */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-14 12:45:33 +00:00
|
|
|
static int udevConnectIsEncrypted(virConnectPtr conn G_GNUC_UNUSED)
|
2018-01-26 11:16:00 +00:00
|
|
|
{
|
|
|
|
/* Not encrypted, but remote driver takes care of that */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-14 12:45:33 +00:00
|
|
|
static int udevConnectIsAlive(virConnectPtr conn G_GNUC_UNUSED)
|
2018-01-26 11:16:00 +00:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-06 19:20:25 +00:00
|
|
|
static virInterfaceDriver udevIfaceDriver = {
|
2015-01-20 16:16:26 +00:00
|
|
|
.name = "udev",
|
2013-04-23 12:50:18 +00:00
|
|
|
.connectNumOfInterfaces = udevConnectNumOfInterfaces, /* 1.0.0 */
|
|
|
|
.connectListInterfaces = udevConnectListInterfaces, /* 1.0.0 */
|
|
|
|
.connectNumOfDefinedInterfaces = udevConnectNumOfDefinedInterfaces, /* 1.0.0 */
|
|
|
|
.connectListDefinedInterfaces = udevConnectListDefinedInterfaces, /* 1.0.0 */
|
|
|
|
.connectListAllInterfaces = udevConnectListAllInterfaces, /* 1.0.0 */
|
|
|
|
.interfaceLookupByName = udevInterfaceLookupByName, /* 1.0.0 */
|
|
|
|
.interfaceLookupByMACString = udevInterfaceLookupByMACString, /* 1.0.0 */
|
|
|
|
.interfaceIsActive = udevInterfaceIsActive, /* 1.0.0 */
|
|
|
|
.interfaceGetXMLDesc = udevInterfaceGetXMLDesc, /* 1.0.0 */
|
2012-10-06 19:20:25 +00:00
|
|
|
};
|
|
|
|
|
2018-01-26 11:16:00 +00:00
|
|
|
|
|
|
|
static virHypervisorDriver udevHypervisorDriver = {
|
|
|
|
.name = "interface",
|
|
|
|
.connectOpen = udevConnectOpen, /* 4.1.0 */
|
|
|
|
.connectClose = udevConnectClose, /* 4.1.0 */
|
|
|
|
.connectIsEncrypted = udevConnectIsEncrypted, /* 4.1.0 */
|
|
|
|
.connectIsSecure = udevConnectIsSecure, /* 4.1.0 */
|
|
|
|
.connectIsAlive = udevConnectIsAlive, /* 4.1.0 */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static virConnectDriver udevConnectDriver = {
|
2018-03-28 09:53:31 +00:00
|
|
|
.localOnly = true,
|
2018-03-27 14:51:45 +00:00
|
|
|
.uriSchemes = (const char *[]){ "interface", NULL },
|
2018-01-26 11:16:00 +00:00
|
|
|
.hypervisorDriver = &udevHypervisorDriver,
|
|
|
|
.interfaceDriver = &udevIfaceDriver,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-11-17 17:09:00 +00:00
|
|
|
static virStateDriver interfaceStateDriver = {
|
|
|
|
.name = "udev",
|
|
|
|
.stateInitialize = udevStateInitialize,
|
|
|
|
.stateCleanup = udevStateCleanup,
|
|
|
|
};
|
|
|
|
|
2012-10-06 19:20:25 +00:00
|
|
|
int
|
2014-03-18 08:19:33 +00:00
|
|
|
udevIfaceRegister(void)
|
|
|
|
{
|
2018-01-26 11:16:00 +00:00
|
|
|
if (virRegisterConnectDriver(&udevConnectDriver, false) < 0)
|
|
|
|
return -1;
|
2015-01-20 16:16:26 +00:00
|
|
|
if (virSetSharedInterfaceDriver(&udevIfaceDriver) < 0) {
|
2012-10-06 19:20:25 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("failed to register udev interface driver"));
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-17 17:09:00 +00:00
|
|
|
if (virRegisterStateDriver(&interfaceStateDriver) < 0)
|
|
|
|
return -1;
|
2012-10-06 19:20:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|