summaryrefslogtreecommitdiff
path: root/src/devices/contrail/nm-device-contrail-vrouter.c
blob: 773d6318555ef99d16c84fbb9676bc22abc221ed (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// SPDX-License-Identifier: LGPL-2.1+
/*
 * Copyright (C) 2007 - 2013 Red Hat, Inc.
 * Copyright (C) 2007 - 2008 Novell, Inc.
 */

#include <string.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <unistd.h>

#include "nm-default.h"

#include "nm-device-contrail-vrouter.h"

#include "devices/nm-device-private.h"
#include "nm-active-connection.h"
#include "nm-setting-connection.h"
#include "nm-setting-contrail-vrouter.h"

#include "devices/nm-device-logging.h"
_LOG_DECLARE_SELF(NMDeviceContrailVrouter);

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

typedef struct {
	bool waiting_for_interface:1;
} NMDeviceContrailVrouterPrivate;

struct _NMDeviceContrailVrouter {
	NMDevice parent;
	NMDeviceContrailVrouterPrivate _priv;
};

struct _NMDeviceContrailVrouterClass {
	NMDeviceClass parent;
};

G_DEFINE_TYPE (NMDeviceContrailVrouter, nm_device_contrail_vrouter, NM_TYPE_DEVICE)

#define NM_DEVICE_CONTRAIL_VROUTER_GET_PRIVATE(self) _NM_GET_PRIVATE (self, NMDeviceContrailVrouter, NM_IS_DEVICE_CONTRAIL_VROUTER, NMDevice)

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

void _get_command(const char *iface,
                  const char *physdev,
                  char *command,
                  const int command_size);

void
_get_mac (const char *physdev, char *mac_str);

void
_get_mac (const char *physdev, char *mac_str)
{
	struct ifreq ifr;
	int fd;
	unsigned char *mac;
	int i;
	char *pos;

	nm_log_dbg (LOGD_DEVICE, "CONTRAIL: %s %s", __FILE__ , __func__);

	memset(&ifr, 0, sizeof(ifr));
	fd = socket(AF_INET, SOCK_DGRAM, 0);
	ifr.ifr_addr.sa_family = AF_INET;
	strncpy(ifr.ifr_name , physdev , IFNAMSIZ-1);
	if (0 == ioctl(fd, SIOCGIFHWADDR, &ifr)) {
		mac = (unsigned char *)ifr.ifr_hwaddr.sa_data;
		i = 0;
		pos = mac_str;
		for ( ; i < 6; i++) {
			if (i) {
				pos += sprintf(pos, ":");
			}
			pos += sprintf(pos, "%02X", (unsigned char)mac[i]);
		}
	}
	close(fd);
}

void
_get_command (const char *iface,
              const char *physdev,
              char *command,
              const int command_size)
{
	char mac_str[19];

	nm_log_dbg (LOGD_DEVICE, "CONTRAIL: %s %s", __FILE__ , __func__);

	_get_mac(physdev, mac_str);
	nm_log_dbg (LOGD_DEVICE, "CONTRAIL: mac address: %s", mac_str);

	snprintf(command, command_size,
			"modprobe vrouter && "
			"vif --create %s --mac %s 2>&1 && "
			"vif --add %s --mac %s --vrf 0 --vhost-phys --type physical 2>&1 && "
			"vif --add %s --mac %s --vrf 0 --type vhost --xconnect %s 2>&1 && "
			"ip link set dev %s address %s 2>&1 && "
			"ip link set dev %s up 2>&1",
			iface, mac_str,
			physdev, mac_str,
			iface, mac_str, physdev,
			iface, mac_str,
			iface);
}

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

static const char *
get_type_description (NMDevice *device)
{
	nm_log_dbg (LOGD_DEVICE, "CONTRAIL: %s %s", __FILE__ , __func__);

	return "contrail-vrouter";
}

static gboolean
create_and_realize (NMDevice *device,
                    NMConnection *connection,
                    NMDevice *parent,
                    const NMPlatformLink **out_plink,
                    GError **error)
{
	nm_log_dbg (LOGD_DEVICE, "CONTRAIL: %s %s", __FILE__ , __func__);

	return TRUE;
}

static NMDeviceCapabilities
get_generic_capabilities (NMDevice *device)
{
	nm_log_dbg (LOGD_DEVICE, "CONTRAIL: %s %s", __FILE__ , __func__);

	return NM_DEVICE_CAP_IS_SOFTWARE;
}

static gboolean
is_available (NMDevice *device, NMDeviceCheckDevAvailableFlags flags)
{
	nm_log_dbg (LOGD_DEVICE, "CONTRAIL: %s %s", __FILE__ , __func__);

	return TRUE;
}

static gboolean
check_connection_compatible (NMDevice *device, NMConnection *connection, GError **error)
{
	nm_log_dbg (LOGD_DEVICE, "CONTRAIL: %s %s", __FILE__ , __func__);

	return TRUE;
}

static void
link_changed (NMDevice *device,
              const NMPlatformLink *pllink)
{
    NMDeviceContrailVrouterPrivate *priv;

    nm_log_dbg (LOGD_DEVICE, "CONTRAIL: %s %s", __FILE__ , __func__);
	priv = NM_DEVICE_CONTRAIL_VROUTER_GET_PRIVATE (device);

	if (pllink && priv->waiting_for_interface) {
		priv->waiting_for_interface = FALSE;
		nm_device_bring_up (device, TRUE, NULL);
		nm_device_activate_schedule_stage3_ip_config_start (device);
	}
}

static NMActStageReturn
act_stage1_prepare (NMDevice *device, NMDeviceStateReason *out_failure_reason)
{
	FILE *fp;
	int COMMAND_OUTPUT_SIZE = 512;
	char output[COMMAND_OUTPUT_SIZE];
	const char *iface;
	int COMMAND_SIZE = 512;
	char command[COMMAND_SIZE];
	const char *physdev;
    NMConnection *connection;

	nm_log_dbg (LOGD_DEVICE, "CONTRAIL: %s %s", __FILE__ , __func__);

	if (!device) {
		nm_log_err (LOGD_DEVICE, "CONTRAIL: device is null");
		return NM_ACT_STAGE_RETURN_FAILURE;
	}
	connection = nm_device_get_applied_connection (device);
	if (!connection) {
		nm_log_err (LOGD_DEVICE, "CONTRAIL: connection is null");
		return NM_ACT_STAGE_RETURN_FAILURE;
	}
	nm_connection_dump (connection);
	iface = nm_connection_get_interface_name (connection);
	nm_log_dbg (LOGD_DEVICE, "CONTRAIL: interface name: %s", iface);
	physdev = nm_setting_contrail_vrouter_get_physdev (nm_connection_get_setting_contrail_vrouter (connection));
	if (physdev) {
		nm_log_dbg (LOGD_DEVICE, "CONTRAIL: physical device name: %s", physdev);
		_get_command(iface, physdev, command, COMMAND_SIZE);
		nm_log_dbg (LOGD_DEVICE, "CONTRAIL: vrouter command: %s", command);
		fp = popen(command, "r");
		while (fgets(output, COMMAND_OUTPUT_SIZE, fp) != NULL){
			nm_log_err (LOGD_DEVICE, "CONTRAIL: %s", output);
		}
		pclose(fp);
	}
	else {
		nm_log_err (LOGD_DEVICE, "CONTRAIL: physical device name was not provided.");
		return NM_ACT_STAGE_RETURN_FAILURE;
	}
	return NM_ACT_STAGE_RETURN_SUCCESS;
}

static NMActStageReturn
act_stage3_ip_config_start (NMDevice *device,
                            int addr_family,
                            gpointer *out_config,
                            NMDeviceStateReason *out_failure_reason)
{
    NMDeviceContrailVrouterPrivate *priv;

	nm_log_dbg (LOGD_DEVICE, "CONTRAIL: %s %s", __FILE__ , __func__);
	priv = NM_DEVICE_CONTRAIL_VROUTER_GET_PRIVATE (device);

	if (!nm_device_get_ip_ifindex (device)) {
		priv->waiting_for_interface = TRUE;
		return NM_ACT_STAGE_RETURN_POSTPONE;
	}

	return NM_DEVICE_CLASS (nm_device_contrail_vrouter_parent_class)->act_stage3_ip_config_start (device, addr_family, out_config, out_failure_reason);
}

static gboolean
can_unmanaged_external_down (NMDevice *self)
{
	nm_log_dbg (LOGD_DEVICE, "CONTRAIL: %s %s", __FILE__ , __func__);

	return FALSE;
}

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

static void
nm_device_contrail_vrouter_init (NMDeviceContrailVrouter *self)
{
	nm_log_dbg (LOGD_DEVICE, "CONTRAIL: %s %s", __FILE__ , __func__);
}

static const NMDBusInterfaceInfoExtended interface_info_device_contrail_vrouter = {
	.parent = NM_DEFINE_GDBUS_INTERFACE_INFO_INIT (
		NM_DBUS_INTERFACE_DEVICE_CONTRAIL_VROUTER,
		.signals = NM_DEFINE_GDBUS_SIGNAL_INFOS (
			&nm_signal_info_property_changed_legacy,
		),
	),
	.legacy_property_changed = TRUE,
};

static void
nm_device_contrail_vrouter_class_init (NMDeviceContrailVrouterClass *klass)
{
	NMDBusObjectClass *dbus_object_class = NM_DBUS_OBJECT_CLASS (klass);
	NMDeviceClass *device_class = NM_DEVICE_CLASS (klass);

	dbus_object_class->interface_infos = NM_DBUS_INTERFACE_INFOS (&interface_info_device_contrail_vrouter);

	device_class->connection_type_supported = NM_SETTING_CONTRAIL_VROUTER_SETTING_NAME;
	device_class->connection_type_check_compatible = NM_SETTING_CONTRAIL_VROUTER_SETTING_NAME;
	device_class->link_types = NM_DEVICE_DEFINE_LINK_TYPES (NM_LINK_TYPE_CONTRAILVROUTER);

	device_class->get_type_description = get_type_description;
	device_class->create_and_realize = create_and_realize;
	device_class->get_generic_capabilities = get_generic_capabilities;
	device_class->is_available = is_available;
	device_class->check_connection_compatible = check_connection_compatible;
	device_class->link_changed = link_changed;
	device_class->act_stage1_prepare = act_stage1_prepare;
    device_class->act_stage3_ip_config_start = act_stage3_ip_config_start;
	device_class->can_unmanaged_external_down = can_unmanaged_external_down;
}