summaryrefslogtreecommitdiff
path: root/src/devices/nm-device-veth.c
blob: d143ccf7b900a0e02129d65549ef12fbdc4d189f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* -*- 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"
#include "nm-device-private.h"
#include "nm-default.h"
#include "nm-manager.h"
#include "nm-platform.h"
#include "nm-device-factory.h"

#include "nmdbus-device-veth.h"

#include "nm-device-logging.h"
_LOG_DECLARE_SELF(NMDeviceVeth);

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;

	if (!nm_platform_veth_get_properties (NM_PLATFORM_GET, nm_device_get_ifindex (device), &props)) {
		_LOGW (LOGD_HW, "could not read veth properties");
		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;
}

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;
}

/**************************************************************/

static void
nm_device_veth_init (NMDeviceVeth *self)
{
}

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);
}

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);
		nm_utils_g_value_set_object_path (value, peer);
		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);
	NMDeviceClass *device_class = NM_DEVICE_CLASS (klass);

	g_type_class_add_private (klass, sizeof (NMDeviceVethPrivate));

	NM_DEVICE_CLASS_DECLARE_TYPES(klass, NULL, NM_LINK_TYPE_VETH)

	object_class->get_property = get_property;
	object_class->dispose = dispose;

	device_class->can_unmanaged_external_down = can_unmanaged_external_down;

	/* properties */
	g_object_class_install_property
		(object_class, PROP_PEER,
		 g_param_spec_string (NM_DEVICE_VETH_PEER, "", "",
		                      NULL,
		                      G_PARAM_READABLE |
		                      G_PARAM_STATIC_STRINGS));

	nm_exported_object_class_add_interface (NM_EXPORTED_OBJECT_CLASS (klass),
	                                        NMDBUS_TYPE_DEVICE_VETH_SKELETON,
	                                        NULL);
}

/*************************************************************/

#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 *
create_device (NMDeviceFactory *factory,
               const char *iface,
               NMPlatformLink *plink,
               NMConnection *connection,
               gboolean *out_ignore)
{
	return (NMDevice *) g_object_new (NM_TYPE_DEVICE_VETH,
	                                  NM_DEVICE_IFACE, iface,
	                                  NM_DEVICE_TYPE_DESC, "Veth",
	                                  NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_ETHERNET,
	                                  NULL);
}

NM_DEVICE_FACTORY_DEFINE_INTERNAL (VETH, Veth, veth,
	NM_DEVICE_FACTORY_DECLARE_LINK_TYPES (NM_LINK_TYPE_VETH),
	factory_iface->create_device = create_device;
	)