2013-05-03 13:55:51 -04:00
|
|
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
|
|
|
|
|
/* NetworkManager -- Network link manager
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
*
|
|
|
|
|
* Copyright 2013 Red Hat, Inc.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <linux/sockios.h>
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
|
|
#include "nm-device-veth.h"
|
2013-05-29 12:57:13 -03:00
|
|
|
#include "nm-device-private.h"
|
2013-05-03 13:55:51 -04:00
|
|
|
#include "nm-logging.h"
|
|
|
|
|
#include "nm-manager.h"
|
|
|
|
|
#include "nm-platform.h"
|
2014-09-08 10:57:31 -05:00
|
|
|
#include "nm-device-factory.h"
|
2013-05-03 13:55:51 -04:00
|
|
|
|
|
|
|
|
#include "nm-device-veth-glue.h"
|
|
|
|
|
|
2014-08-02 15:14:26 +02:00
|
|
|
#include "nm-device-logging.h"
|
|
|
|
|
_LOG_DECLARE_SELF(NMDeviceVeth);
|
|
|
|
|
|
2013-05-03 13:55:51 -04:00
|
|
|
G_DEFINE_TYPE (NMDeviceVeth, nm_device_veth, NM_TYPE_DEVICE_ETHERNET)
|
|
|
|
|
|
|
|
|
|
#define NM_DEVICE_VETH_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_VETH, NMDeviceVethPrivate))
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
NMDevice *peer;
|
|
|
|
|
gboolean ever_had_peer;
|
|
|
|
|
} NMDeviceVethPrivate;
|
|
|
|
|
|
|
|
|
|
enum {
|
|
|
|
|
PROP_0,
|
|
|
|
|
PROP_PEER,
|
|
|
|
|
|
|
|
|
|
LAST_PROP
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**************************************************************/
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
set_peer (NMDeviceVeth *self, NMDevice *peer)
|
|
|
|
|
{
|
|
|
|
|
NMDeviceVethPrivate *priv = NM_DEVICE_VETH_GET_PRIVATE (self);
|
|
|
|
|
|
|
|
|
|
if (!priv->peer) {
|
|
|
|
|
priv->ever_had_peer = TRUE;
|
|
|
|
|
priv->peer = peer;
|
|
|
|
|
g_object_add_weak_pointer (G_OBJECT (peer), (gpointer *) &priv->peer);
|
|
|
|
|
|
|
|
|
|
g_object_notify (G_OBJECT (self), NM_DEVICE_VETH_PEER);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static NMDevice *
|
|
|
|
|
get_peer (NMDeviceVeth *self)
|
|
|
|
|
{
|
|
|
|
|
NMDeviceVethPrivate *priv = NM_DEVICE_VETH_GET_PRIVATE (self);
|
|
|
|
|
NMDevice *device = NM_DEVICE (self), *peer = NULL;
|
|
|
|
|
NMPlatformVethProperties props;
|
|
|
|
|
|
|
|
|
|
if (priv->ever_had_peer)
|
|
|
|
|
return priv->peer;
|
|
|
|
|
|
platform: add self argument to platform functions
Most nm_platform_*() functions operate on the platform
singleton nm_platform_get(). That made sense because the
NMPlatform instance was mainly to hook fake platform for
testing.
While the implicit argument saved some typing, I think explicit is
better. Especially, because NMPlatform could become a more usable
object then just a hook for testing.
With this change, NMPlatform instances can be used individually, not
only as a singleton instance.
Before this change, the constructor of NMLinuxPlatform could not
call any nm_platform_*() functions because the singleton was not
yet initialized. We could only instantiate an incomplete instance,
register it via nm_platform_setup(), and then complete initialization
via singleton->setup().
With this change, we can create and fully initialize NMPlatform instances
before/without setting them up them as singleton.
Also, currently there is no clear distinction between functions
that operate on the NMPlatform instance, and functions that can
be used stand-alone (e.g. nm_platform_ip4_address_to_string()).
The latter can not be mocked for testing. With this change, the
distinction becomes obvious. That is also useful because it becomes
clearer which functions make use of the platform cache and which not.
Inside nm-linux-platform.c, continue the pattern that the
self instance is named @platform. That makes sense because
its type is NMPlatform, and not NMLinuxPlatform what we
would expect from a paramter named @self.
This is a major diff that causes some pain when rebasing. Try
to rebase to the parent commit of this commit as a first step.
Then rebase on top of this commit using merge-strategy "ours".
2015-04-18 12:36:09 +02:00
|
|
|
if (!nm_platform_veth_get_properties (NM_PLATFORM_GET, nm_device_get_ifindex (device), &props)) {
|
2014-08-02 15:14:26 +02:00
|
|
|
_LOGW (LOGD_HW, "could not read veth properties");
|
2013-05-03 13:55:51 -04:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
peer = nm_manager_get_device_by_ifindex (nm_manager_get (), props.peer);
|
|
|
|
|
if (peer && NM_IS_DEVICE_VETH (peer)) {
|
|
|
|
|
set_peer (self, peer);
|
|
|
|
|
set_peer (NM_DEVICE_VETH (peer), device);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return priv->peer;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-07 14:24:12 +02:00
|
|
|
static gboolean
|
|
|
|
|
can_unmanaged_external_down (NMDevice *self)
|
|
|
|
|
{
|
|
|
|
|
/* Unless running in a container, an udev rule causes these to be
|
|
|
|
|
* unmanaged. If there's no udev then we're probably in a container
|
|
|
|
|
* and should IFF_UP and configure the veth ourselves even if we
|
|
|
|
|
* didn't create it. */
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
2013-05-03 13:55:51 -04:00
|
|
|
|
|
|
|
|
/**************************************************************/
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
nm_device_veth_init (NMDeviceVeth *self)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-26 13:32:30 -05:00
|
|
|
static void
|
|
|
|
|
dispose (GObject *object)
|
|
|
|
|
{
|
|
|
|
|
NMDeviceVeth *self = NM_DEVICE_VETH (object);
|
|
|
|
|
NMDeviceVethPrivate *priv = NM_DEVICE_VETH_GET_PRIVATE (self);
|
|
|
|
|
|
|
|
|
|
if (priv->peer) {
|
|
|
|
|
g_object_remove_weak_pointer (G_OBJECT (priv->peer), (gpointer *) &priv->peer);
|
|
|
|
|
priv->peer = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (nm_device_veth_parent_class)->dispose (object);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-03 13:55:51 -04:00
|
|
|
static void
|
|
|
|
|
get_property (GObject *object, guint prop_id,
|
|
|
|
|
GValue *value, GParamSpec *pspec)
|
|
|
|
|
{
|
|
|
|
|
NMDeviceVeth *self = NM_DEVICE_VETH (object);
|
|
|
|
|
NMDevice *peer;
|
|
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
|
case PROP_PEER:
|
|
|
|
|
peer = get_peer (self);
|
2015-04-03 10:08:52 -04:00
|
|
|
nm_utils_g_value_set_object_path (value, peer);
|
2013-05-03 13:55:51 -04:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
nm_device_veth_class_init (NMDeviceVethClass *klass)
|
|
|
|
|
{
|
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
2015-04-07 14:24:12 +02:00
|
|
|
NMDeviceClass *device_class = NM_DEVICE_CLASS (klass);
|
2013-05-03 13:55:51 -04:00
|
|
|
|
|
|
|
|
g_type_class_add_private (klass, sizeof (NMDeviceVethPrivate));
|
|
|
|
|
|
|
|
|
|
object_class->get_property = get_property;
|
2014-02-26 13:32:30 -05:00
|
|
|
object_class->dispose = dispose;
|
2013-05-03 13:55:51 -04:00
|
|
|
|
2015-04-07 14:24:12 +02:00
|
|
|
device_class->can_unmanaged_external_down = can_unmanaged_external_down;
|
|
|
|
|
|
2013-05-03 13:55:51 -04:00
|
|
|
/* properties */
|
|
|
|
|
g_object_class_install_property
|
|
|
|
|
(object_class, PROP_PEER,
|
2014-06-09 16:17:37 -04:00
|
|
|
g_param_spec_boxed (NM_DEVICE_VETH_PEER, "", "",
|
2013-05-03 13:55:51 -04:00
|
|
|
DBUS_TYPE_G_OBJECT_PATH,
|
2014-06-09 16:17:37 -04:00
|
|
|
G_PARAM_READABLE |
|
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
2013-05-03 13:55:51 -04:00
|
|
|
|
2015-04-13 13:31:42 -04:00
|
|
|
nm_exported_object_class_add_interface (NM_EXPORTED_OBJECT_CLASS (klass),
|
2013-05-03 13:55:51 -04:00
|
|
|
&dbus_glib_nm_device_veth_object_info);
|
|
|
|
|
}
|
2014-09-08 10:57:31 -05:00
|
|
|
|
|
|
|
|
/*************************************************************/
|
|
|
|
|
|
|
|
|
|
#define NM_TYPE_VETH_FACTORY (nm_veth_factory_get_type ())
|
|
|
|
|
#define NM_VETH_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_VETH_FACTORY, NMVethFactory))
|
|
|
|
|
|
|
|
|
|
static NMDevice *
|
2015-05-06 09:53:44 -05:00
|
|
|
new_link (NMDeviceFactory *factory, NMPlatformLink *plink, gboolean *out_ignore, GError **error)
|
2014-09-08 10:57:31 -05:00
|
|
|
{
|
2014-09-17 14:17:30 -05:00
|
|
|
return (NMDevice *) g_object_new (NM_TYPE_DEVICE_VETH,
|
|
|
|
|
NM_DEVICE_PLATFORM_DEVICE, plink,
|
|
|
|
|
NM_DEVICE_TYPE_DESC, "Veth",
|
|
|
|
|
NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_ETHERNET,
|
|
|
|
|
NULL);
|
2014-09-08 10:57:31 -05:00
|
|
|
}
|
|
|
|
|
|
2014-09-17 14:17:30 -05:00
|
|
|
NM_DEVICE_FACTORY_DEFINE_INTERNAL (VETH, Veth, veth,
|
|
|
|
|
NM_DEVICE_FACTORY_DECLARE_LINK_TYPES (NM_LINK_TYPE_VETH),
|
|
|
|
|
factory_iface->new_link = new_link;
|
2014-09-08 10:57:31 -05:00
|
|
|
)
|
|
|
|
|
|