nmt-page-bond.c 15.3 KB
Newer Older
Dan Winship's avatar
Dan Winship committed
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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
 * 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, see <http://www.gnu.org/licenses/>.
 *
 * Copyright 2013 Red Hat, Inc.
 */

/**
 * SECTION:nmt-page-bond
 * @short_description: The editor page for Bond connections
 *
 * Note that this is fairly different from most of the other editor
 * pages, because #NMSettingBond doesn't have properties, so we
 * can't just use #GBinding.
 */

#include "config.h"

#include <glib.h>
#include <glib/gi18n-lib.h>

#include "nmt-page-bond.h"

#include "nmt-address-list.h"
#include "nmt-slave-list.h"

G_DEFINE_TYPE (NmtPageBond, nmt_page_bond, NMT_TYPE_PAGE_DEVICE)

#define NMT_PAGE_BOND_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NMT_TYPE_PAGE_BOND, NmtPageBondPrivate))

typedef enum {
	NMT_PAGE_BOND_MONITORING_UNKNOWN = -1,
	NMT_PAGE_BOND_MONITORING_MII = 0,
	NMT_PAGE_BOND_MONITORING_ARP = 1,
} NmtPageBondMonitoringMode;

typedef struct {
	NmtSlaveList *slaves;

	NmtNewtPopup *mode;
	NmtNewtEntry *primary;
	NmtNewtPopup *monitoring;
	NmtNewtEntry *miimon;
	NmtNewtEntry *updelay;
	NmtNewtEntry *downdelay;
	NmtNewtEntry *arp_interval;
	NmtAddressList *arp_ip_target;

	NmtPageBondMonitoringMode monitoring_mode;

	NMSettingBond *s_bond;
	GType slave_type;
	gboolean updating;
} NmtPageBondPrivate;

NmtNewtWidget *
nmt_page_bond_new (NMConnection   *conn,
                   NmtDeviceEntry *deventry)
{
	return g_object_new (NMT_TYPE_PAGE_BOND,
	                     "connection", conn,
	                     "title", _("BOND"),
	                     "device-entry", deventry,
	                     NULL);
}

static void
nmt_page_bond_init (NmtPageBond *bond)
{
	NmtPageBondPrivate *priv = NMT_PAGE_BOND_GET_PRIVATE (bond);

	priv->monitoring_mode = NMT_PAGE_BOND_MONITORING_UNKNOWN;
	priv->slave_type = G_TYPE_NONE;
}

static NmtNewtPopupEntry bond_mode[] = {
	{ N_("Round-robin"),                            "balance-rr" },
	{ N_("Active Backup"),                          "active-backup" },
	{ N_("XOR"),                                    "balance-xor" },
	{ N_("Broadcast"),                              "broadcast" },
	{ N_("802.3ad"),                                "802.3ad" },
	{ N_("Adaptive Transmit Load Balancing (tlb)"), "balance-tlb" },
	{ N_("Adaptive Load Balancing (alb)"),          "balance-alb" },
	{ NULL, NULL }
};

/* NB: the ordering/numbering here corresponds to NmtPageBondMonitoringMode */
static NmtNewtPopupEntry bond_monitoring[] = {
	{ N_("MII (recommended)"), "mii" },
	{ N_("ARP"),               "arp" },
	{ NULL, NULL }
};

static void
bond_options_changed (GObject    *object,
                      GParamSpec *pspec,
                      gpointer    user_data)
{
	NMSettingBond *s_bond = NM_SETTING_BOND (object);
	NmtPageBond *bond = NMT_PAGE_BOND (user_data);
	NmtPageBondPrivate *priv = NMT_PAGE_BOND_GET_PRIVATE (bond);
	const char *val;
	char **ips;

	if (priv->updating)
		return;

	priv->updating = TRUE;

	val = nm_setting_bond_get_option_by_name (s_bond, NM_SETTING_BOND_OPTION_MODE);
	nmt_newt_popup_set_active_id (priv->mode, val);

	if (!strcmp (val, "active-backup")) {
		nmt_newt_widget_set_visible (NMT_NEWT_WIDGET (priv->primary), TRUE);
		val = nm_setting_bond_get_option_by_name (s_bond, NM_SETTING_BOND_OPTION_PRIMARY);
		nmt_newt_entry_set_text (priv->primary, val);
	} else
		nmt_newt_widget_set_visible (NMT_NEWT_WIDGET (priv->primary), FALSE);

	if (priv->monitoring_mode == NMT_PAGE_BOND_MONITORING_UNKNOWN) {
		val = nm_setting_bond_get_option_by_name (s_bond, NM_SETTING_BOND_OPTION_ARP_INTERVAL);
		if (val && strcmp (val, "0") != 0)
			priv->monitoring_mode = NMT_PAGE_BOND_MONITORING_ARP;
		else
			priv->monitoring_mode = NMT_PAGE_BOND_MONITORING_MII;
	}
	nmt_newt_popup_set_active (priv->monitoring, priv->monitoring_mode);

	if (priv->monitoring_mode == NMT_PAGE_BOND_MONITORING_MII) {
		nmt_newt_widget_set_visible (NMT_NEWT_WIDGET (priv->miimon), TRUE);
		val = nm_setting_bond_get_option_by_name (s_bond, NM_SETTING_BOND_OPTION_MIIMON);
		nmt_newt_entry_set_text (priv->miimon, val);

		nmt_newt_widget_set_visible (NMT_NEWT_WIDGET (priv->updelay), TRUE);
		val = nm_setting_bond_get_option_by_name (s_bond, NM_SETTING_BOND_OPTION_UPDELAY);
		nmt_newt_entry_set_text (priv->updelay, val);

		nmt_newt_widget_set_visible (NMT_NEWT_WIDGET (priv->downdelay), TRUE);
		val = nm_setting_bond_get_option_by_name (s_bond, NM_SETTING_BOND_OPTION_DOWNDELAY);
		nmt_newt_entry_set_text (priv->downdelay, val);

		nmt_newt_widget_set_visible (NMT_NEWT_WIDGET (priv->arp_interval), FALSE);
		nmt_newt_widget_set_visible (NMT_NEWT_WIDGET (priv->arp_ip_target), FALSE);
	} else {
		nmt_newt_widget_set_visible (NMT_NEWT_WIDGET (priv->arp_interval), TRUE);
		val = nm_setting_bond_get_option_by_name (s_bond, NM_SETTING_BOND_OPTION_ARP_INTERVAL);
		nmt_newt_entry_set_text (priv->arp_interval, val);

		nmt_newt_widget_set_visible (NMT_NEWT_WIDGET (priv->arp_ip_target), TRUE);
		val = nm_setting_bond_get_option_by_name (s_bond, NM_SETTING_BOND_OPTION_ARP_IP_TARGET);
		ips = g_strsplit (val, ",", -1);
		g_object_set (G_OBJECT (priv->arp_ip_target),
164
		              "strings", ips,
Dan Winship's avatar
Dan Winship committed
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
		              NULL);
		g_strfreev (ips);

		nmt_newt_widget_set_visible (NMT_NEWT_WIDGET (priv->miimon), FALSE);
		nmt_newt_widget_set_visible (NMT_NEWT_WIDGET (priv->updelay), FALSE);
		nmt_newt_widget_set_visible (NMT_NEWT_WIDGET (priv->downdelay), FALSE);
	}

	priv->updating = FALSE;
}

static void
slaves_changed (GObject    *object,
                GParamSpec *pspec,
                gpointer    user_data)
{
	NmtPageBond *bond = NMT_PAGE_BOND (user_data);
	NmtPageBondPrivate *priv = NMT_PAGE_BOND_GET_PRIVATE (bond);
	GPtrArray *slaves;

	g_object_get (object, "connections", &slaves, NULL);
	if (slaves->len == 0) {
		if (priv->slave_type == G_TYPE_NONE)
			return;
		priv->slave_type = G_TYPE_NONE;
	} else {
		NMConnection *slave = slaves->pdata[0];

		if (priv->slave_type != G_TYPE_NONE)
			return;

		if (nm_connection_is_type (slave, NM_SETTING_INFINIBAND_SETTING_NAME))
			priv->slave_type = NM_TYPE_SETTING_INFINIBAND;
		else
			priv->slave_type = NM_TYPE_SETTING_WIRED;
	}

	if (priv->slave_type == NM_TYPE_SETTING_INFINIBAND) {
		nmt_newt_popup_set_active_id (priv->mode, "active-backup");
		nmt_newt_component_set_sensitive (NMT_NEWT_COMPONENT (priv->mode), FALSE);
	} else
		nmt_newt_component_set_sensitive (NMT_NEWT_COMPONENT (priv->mode), TRUE);
}

#define WIDGET_CHANGED_FUNC(widget, func, option)							\
static void																	\
widget ## _widget_changed (GObject    *object,								\
                           GParamSpec *pspec,								\
                           gpointer    user_data)							\
{																			\
	NmtPageBond *bond = NMT_PAGE_BOND (user_data);							\
	NmtPageBondPrivate *priv = NMT_PAGE_BOND_GET_PRIVATE (bond);			\
																			\
	if (priv->updating)														\
		return;																\
																			\
	priv->updating = TRUE;													\
	nm_setting_bond_add_option (priv->s_bond, option, func (priv->widget));	\
	priv->updating = FALSE;													\
}

WIDGET_CHANGED_FUNC (primary, nmt_newt_entry_get_text, NM_SETTING_BOND_OPTION_PRIMARY)
WIDGET_CHANGED_FUNC (miimon, nmt_newt_entry_get_text, NM_SETTING_BOND_OPTION_MIIMON)
WIDGET_CHANGED_FUNC (updelay, nmt_newt_entry_get_text, NM_SETTING_BOND_OPTION_UPDELAY)
WIDGET_CHANGED_FUNC (downdelay, nmt_newt_entry_get_text, NM_SETTING_BOND_OPTION_DOWNDELAY)
WIDGET_CHANGED_FUNC (arp_interval, nmt_newt_entry_get_text, NM_SETTING_BOND_OPTION_ARP_INTERVAL)

static void
mode_widget_changed (GObject    *object,
                     GParamSpec *pspec,
                     gpointer    user_data)
{
	NmtPageBond *bond = NMT_PAGE_BOND (user_data);
	NmtPageBondPrivate *priv = NMT_PAGE_BOND_GET_PRIVATE (bond);
	const char *mode;

	if (priv->updating)
		return;

	mode = nmt_newt_popup_get_active_id (priv->mode);
	priv->updating = TRUE;
	nm_setting_bond_add_option (priv->s_bond, NM_SETTING_BOND_OPTION_MODE, mode);
	priv->updating = FALSE;

	if (!strcmp (mode, "balance-tlb") || !strcmp (mode, "balance-alb")) {
		nmt_newt_popup_set_active (priv->monitoring, NMT_PAGE_BOND_MONITORING_MII);
		nmt_newt_component_set_sensitive (NMT_NEWT_COMPONENT (priv->monitoring), FALSE);
	} else
		nmt_newt_component_set_sensitive (NMT_NEWT_COMPONENT (priv->monitoring), TRUE);

255
	if (!strcmp (mode, "active-backup")) {
Dan Winship's avatar
Dan Winship committed
256
		nmt_newt_widget_set_visible (NMT_NEWT_WIDGET (priv->primary), TRUE);
257
258
259
		nm_setting_bond_add_option (priv->s_bond, NM_SETTING_BOND_OPTION_PRIMARY,
		                            nmt_newt_entry_get_text (priv->primary));
	} else {
Dan Winship's avatar
Dan Winship committed
260
		nmt_newt_widget_set_visible (NMT_NEWT_WIDGET (priv->primary), FALSE);
261
262
		nm_setting_bond_remove_option (priv->s_bond, NM_SETTING_BOND_OPTION_PRIMARY);
	}
Dan Winship's avatar
Dan Winship committed
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
}

static void
monitoring_widget_changed (GObject    *object,
                           GParamSpec *pspec,
                           gpointer    user_data)
{
	NmtPageBond *bond = NMT_PAGE_BOND (user_data);
	NmtPageBondPrivate *priv = NMT_PAGE_BOND_GET_PRIVATE (bond);

	if (priv->updating)
		return;

	priv->monitoring_mode = nmt_newt_popup_get_active (priv->monitoring);
	if (priv->monitoring_mode == NMT_PAGE_BOND_MONITORING_MII) {
		nmt_newt_widget_set_visible (NMT_NEWT_WIDGET (priv->miimon), TRUE);
		nmt_newt_widget_set_visible (NMT_NEWT_WIDGET (priv->updelay), TRUE);
		nmt_newt_widget_set_visible (NMT_NEWT_WIDGET (priv->downdelay), TRUE);

		nmt_newt_widget_set_visible (NMT_NEWT_WIDGET (priv->arp_interval), FALSE);
		nmt_newt_widget_set_visible (NMT_NEWT_WIDGET (priv->arp_ip_target), FALSE);
	} else {
		nmt_newt_widget_set_visible (NMT_NEWT_WIDGET (priv->arp_interval), TRUE);
		nmt_newt_widget_set_visible (NMT_NEWT_WIDGET (priv->arp_ip_target), TRUE);

		nmt_newt_widget_set_visible (NMT_NEWT_WIDGET (priv->miimon), FALSE);
		nmt_newt_widget_set_visible (NMT_NEWT_WIDGET (priv->updelay), FALSE);
		nmt_newt_widget_set_visible (NMT_NEWT_WIDGET (priv->downdelay), FALSE);
	}
}

static void
arp_ip_target_widget_changed (GObject    *object,
                              GParamSpec *pspec,
                              gpointer    user_data)
{
	NmtPageBond *bond = NMT_PAGE_BOND (user_data);
	NmtPageBondPrivate *priv = NMT_PAGE_BOND_GET_PRIVATE (bond);
	char **ips, *target;

	if (priv->updating)
		return;

	g_object_get (G_OBJECT (priv->arp_ip_target),
	              "strings", &ips,
	              NULL);
	target = g_strjoinv (",", ips);

	priv->updating = TRUE;
	nm_setting_bond_add_option (priv->s_bond, NM_SETTING_BOND_OPTION_ARP_IP_TARGET, target);
	priv->updating = FALSE;

	g_free (target);
	g_strfreev (ips);
}

static gboolean
bond_connection_type_filter (GType    connection_type,
                             gpointer user_data)
{
	NmtPageBond *bond = user_data;
	NmtPageBondPrivate *priv = NMT_PAGE_BOND_GET_PRIVATE (bond);

	if (   priv->slave_type != NM_TYPE_SETTING_WIRED
	    && connection_type == NM_TYPE_SETTING_INFINIBAND)
		return TRUE;
	if (   priv->slave_type != NM_TYPE_SETTING_INFINIBAND
	    && connection_type == NM_TYPE_SETTING_WIRED)
		return TRUE;

	return FALSE;
}

static void
nmt_page_bond_constructed (GObject *object)
{
	NmtPageBond *bond = NMT_PAGE_BOND (object);
	NmtPageBondPrivate *priv = NMT_PAGE_BOND_GET_PRIVATE (bond);
	NmtPageGrid *grid;
	NMSettingBond *s_bond;
	NmtNewtWidget *widget, *label;
	NMConnection *conn;

	conn = nmt_editor_page_get_connection (NMT_EDITOR_PAGE (bond));
	s_bond = nm_connection_get_setting_bond (conn);
	if (!s_bond) {
		nm_connection_add_setting (conn, nm_setting_bond_new ());
		s_bond = nm_connection_get_setting_bond (conn);
	}
	priv->s_bond = s_bond;

	grid = NMT_PAGE_GRID (bond);

	widget = nmt_newt_separator_new ();
	nmt_page_grid_append (grid, _("Slaves"), widget, NULL);
	nmt_page_grid_set_row_flags (grid, widget, NMT_PAGE_GRID_ROW_LABEL_ALIGN_LEFT);

	widget = nmt_slave_list_new (conn, bond_connection_type_filter, bond);
	g_signal_connect (widget, "notify::connections",
	                  G_CALLBACK (slaves_changed), bond);
	nmt_page_grid_append (grid, NULL, widget, NULL);
	priv->slaves = NMT_SLAVE_LIST (widget);

	widget = nmt_newt_popup_new (bond_mode);
	g_signal_connect (widget, "notify::active-id",
	                  G_CALLBACK (mode_widget_changed), bond);
	nmt_page_grid_append (grid, _("Mode"), widget, NULL);
	priv->mode = NMT_NEWT_POPUP (widget);

372
	widget = nmt_newt_entry_new (40, NMT_NEWT_ENTRY_NONEMPTY);
Dan Winship's avatar
Dan Winship committed
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
	g_signal_connect (widget, "notify::text",
	                  G_CALLBACK (primary_widget_changed), bond);
	nmt_page_grid_append (grid, _("Primary"), widget, NULL);
	priv->primary = NMT_NEWT_ENTRY (widget);

	widget = nmt_newt_popup_new (bond_monitoring);
	g_signal_connect (widget, "notify::active",
	                  G_CALLBACK (monitoring_widget_changed), bond);
	nmt_page_grid_append (grid, _("Link monitoring"), widget, NULL);
	priv->monitoring = NMT_NEWT_POPUP (widget);

	widget = nmt_newt_entry_numeric_new (10, 0, G_MAXINT);
	g_signal_connect (widget, "notify::text",
	                  G_CALLBACK (miimon_widget_changed), bond);
	label = nmt_newt_label_new (C_("milliseconds", "ms"));
	nmt_page_grid_append (grid, _("Monitoring frequency"), widget, label);
	priv->miimon = NMT_NEWT_ENTRY (widget);

	widget = nmt_newt_entry_numeric_new (10, 0, G_MAXINT);
	g_signal_connect (widget, "notify::text",
	                  G_CALLBACK (updelay_widget_changed), bond);
	label = nmt_newt_label_new (C_("milliseconds", "ms"));
	nmt_page_grid_append (grid, _("Link up delay"), widget, label);
	priv->updelay = NMT_NEWT_ENTRY (widget);

	widget = nmt_newt_entry_numeric_new (10, 0, G_MAXINT);
	g_signal_connect (widget, "notify::text",
	                  G_CALLBACK (downdelay_widget_changed), bond);
	label = nmt_newt_label_new (C_("milliseconds", "ms"));
	nmt_page_grid_append (grid, _("Link down delay"), widget, label);
	priv->downdelay = NMT_NEWT_ENTRY (widget);

	widget = nmt_newt_entry_numeric_new (10, 0, G_MAXINT);
	g_signal_connect (widget, "notify::text",
	                  G_CALLBACK (arp_interval_widget_changed), bond);
	label = nmt_newt_label_new (C_("milliseconds", "ms"));
	nmt_page_grid_append (grid, _("Monitoring frequency"), widget, label);
	priv->arp_interval = NMT_NEWT_ENTRY (widget);

	widget = nmt_address_list_new (NMT_ADDRESS_LIST_IP4);
	g_signal_connect (widget, "notify::strings",
	                  G_CALLBACK (arp_ip_target_widget_changed), bond);
	nmt_page_grid_append (grid, _("ARP targets"), widget, NULL);
	priv->arp_ip_target = NMT_ADDRESS_LIST (widget);

	g_signal_connect (s_bond, "notify::" NM_SETTING_BOND_OPTIONS,
	                  G_CALLBACK (bond_options_changed), bond);
	bond_options_changed (G_OBJECT (s_bond), NULL, bond);
	slaves_changed (G_OBJECT (priv->slaves), NULL, bond);

	G_OBJECT_CLASS (nmt_page_bond_parent_class)->constructed (object);
}

static void
nmt_page_bond_class_init (NmtPageBondClass *bond_class)
{
	GObjectClass *object_class = G_OBJECT_CLASS (bond_class);

	g_type_class_add_private (bond_class, sizeof (NmtPageBondPrivate));

	object_class->constructed = nmt_page_bond_constructed;
}