nm-dcb.c 10.8 KB
Newer Older
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
/* -*- 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 (C) 2013 Red Hat, Inc.
 */

#include <config.h>
#include <sys/wait.h>
#include <string.h>

#include <glib.h>
#include "nm-dcb.h"
#include "nm-platform.h"
#include "NetworkManagerUtils.h"
#include "nm-posix-signals.h"
#include "nm-logging.h"

GQuark
nm_dcb_error_quark (void)
{
	static GQuark ret = 0;

	if (ret == 0)
		ret = g_quark_from_static_string ("nm-dcb-error");
	return ret;
}

static const char *helper_names[] = { "dcbtool", "fcoeadm" };

gboolean
do_helper (const char *iface,
           guint which,
           DcbFunc run_func,
           gpointer user_data,
           GError **error,
           const char *fmt,
           ...)
{
	char **argv = NULL, **split = NULL, *cmdline, *errmsg = NULL;
	gboolean success = FALSE;
	guint i, u;
	va_list args;

	g_return_val_if_fail (fmt != NULL, FALSE);

	va_start (args, fmt);
	cmdline = g_strdup_vprintf (fmt, args);
	va_end (args);

	split = g_strsplit_set (cmdline, " ", 0);
	if (!split) {
		g_set_error (error, NM_DCB_ERROR, NM_DCB_ERROR_INTERNAL,
		             "failure parsing %s command line", helper_names[which]);
		goto out;
	}

	/* Allocate space for path, custom arg, interface name, arguments, and NULL */
	i = u = 0;
	argv = g_new0 (char *, g_strv_length (split) + 4);
	argv[i++] = NULL;  /* Placeholder for dcbtool path */
	if (which == DCBTOOL) {
		argv[i++] = "sc";
		argv[i++] = (char *) iface;
	}
	while (u < g_strv_length (split))
		argv[i++] = split[u++];
	argv[i++] = NULL;
	success = run_func (argv, which, user_data, error);
	if (!success && error)
		g_assert (*error);

out:
	if (split)
		g_strfreev (split);
	g_free (argv);
	g_free (cmdline);
	g_free (errmsg);
	return success;
}

#define SET_FLAGS(f, tag) \
G_STMT_START { \
	if (!do_helper (iface, DCBTOOL, run_func, user_data, error, tag " e:%c a:%c w:%c", \
	                 f & NM_SETTING_DCB_FLAG_ENABLE ? '1' : '0', \
	                 f & NM_SETTING_DCB_FLAG_ADVERTISE ? '1' : '0', \
	                 f & NM_SETTING_DCB_FLAG_WILLING ? '1' : '0')) \
		return FALSE; \
} G_STMT_END

#define SET_APP(f, s, tag) \
G_STMT_START { \
	gint prio = nm_setting_dcb_get_app_##tag##_priority (s); \
 \
	SET_FLAGS (f, "app:" #tag); \
	if ((f & NM_SETTING_DCB_FLAG_ENABLE) && (prio >= 0)) { \
		if (!do_helper (iface, DCBTOOL, run_func, user_data, error, "app:" #tag " appcfg:%02x", (1 << prio))) \
			return FALSE; \
	} \
} G_STMT_END

gboolean
_dcb_setup (const char *iface,
            NMSettingDcb *s_dcb,
            DcbFunc run_func,
            gpointer user_data,
            GError **error)
{
	NMSettingDcbFlags flags;
	guint i;

	g_assert (s_dcb);

	if (!do_helper (iface, DCBTOOL, run_func, user_data, error, "dcb on"))
		return FALSE;

	/* FCoE */
	flags = nm_setting_dcb_get_app_fcoe_flags (s_dcb);
	SET_APP (flags, s_dcb, fcoe);

	/* iSCSI */
	flags = nm_setting_dcb_get_app_iscsi_flags (s_dcb);
	SET_APP (flags, s_dcb, iscsi);

	/* FIP */
	flags = nm_setting_dcb_get_app_fip_flags (s_dcb);
	SET_APP (flags, s_dcb, fip);

	/* Priority Flow Control */
	flags = nm_setting_dcb_get_priority_flow_control_flags (s_dcb);
	SET_FLAGS (flags, "pfc");
	if (flags & NM_SETTING_DCB_FLAG_ENABLE) {
		char buf[10];

		for (i = 0; i < 8; i++)
			buf[i] = nm_setting_dcb_get_priority_flow_control (s_dcb, i) ? '1' : '0';
		buf[i] = 0;
		if (!do_helper (iface, DCBTOOL, run_func, user_data, error, "pfc pfcup:%s", buf))
			return FALSE;
	}

	/* Priority Groups */
	flags = nm_setting_dcb_get_priority_group_flags (s_dcb);
	if (flags & NM_SETTING_DCB_FLAG_ENABLE) {
158
159
		GString *s;
		gboolean success;
160
161
		guint id;

162
163
164
165
166
167
		s = g_string_sized_new (150);

		g_string_append_printf (s, "pg e:1 a:%c w:%c",
		                        flags & NM_SETTING_DCB_FLAG_ADVERTISE ? '1' : '0',
		                        flags & NM_SETTING_DCB_FLAG_WILLING ? '1' : '0');

168
		/* Priority Groups */
169
		g_string_append (s, " pgid:");
170
171
172
		for (i = 0; i < 8; i++) {
			id = nm_setting_dcb_get_priority_group_id (s_dcb, i);
			g_assert (id < 8 || id == 15);
173
			g_string_append_c (s, (id < 8) ? ('0' + id) : 'f');
174
175
176
		}

		/* Priority Group Bandwidth */
177
178
179
180
181
182
183
184
185
		g_string_append_printf (s, " pgpct:%u,%u,%u,%u,%u,%u,%u,%u",
		                        nm_setting_dcb_get_priority_group_bandwidth (s_dcb, 0),
		                        nm_setting_dcb_get_priority_group_bandwidth (s_dcb, 1),
		                        nm_setting_dcb_get_priority_group_bandwidth (s_dcb, 2),
		                        nm_setting_dcb_get_priority_group_bandwidth (s_dcb, 3),
		                        nm_setting_dcb_get_priority_group_bandwidth (s_dcb, 4),
		                        nm_setting_dcb_get_priority_group_bandwidth (s_dcb, 5),
		                        nm_setting_dcb_get_priority_group_bandwidth (s_dcb, 6),
		                        nm_setting_dcb_get_priority_group_bandwidth (s_dcb, 7));
186
187

		/* Priority Bandwidth */
188
189
190
191
192
193
194
195
196
		g_string_append_printf (s, " uppct:%u,%u,%u,%u,%u,%u,%u,%u",
		                        nm_setting_dcb_get_priority_bandwidth (s_dcb, 0),
		                        nm_setting_dcb_get_priority_bandwidth (s_dcb, 1),
		                        nm_setting_dcb_get_priority_bandwidth (s_dcb, 2),
		                        nm_setting_dcb_get_priority_bandwidth (s_dcb, 3),
		                        nm_setting_dcb_get_priority_bandwidth (s_dcb, 4),
		                        nm_setting_dcb_get_priority_bandwidth (s_dcb, 5),
		                        nm_setting_dcb_get_priority_bandwidth (s_dcb, 6),
		                        nm_setting_dcb_get_priority_bandwidth (s_dcb, 7));
197
198

		/* Strict Bandwidth */
199
		g_string_append (s, " strict:");
200
		for (i = 0; i < 8; i++)
201
			g_string_append_c (s, nm_setting_dcb_get_priority_strict_bandwidth (s_dcb, i) ? '1' : '0');
202
203

		/* Priority Traffic Class */
204
		g_string_append (s, " up2tc:");
205
206
207
		for (i = 0; i < 8; i++) {
			id = nm_setting_dcb_get_priority_traffic_class (s_dcb, i);
			g_assert (id < 8);
208
			g_string_append_c (s, '0' + id);
209
		}
210

Dan Winship's avatar
Dan Winship committed
211
		success = do_helper (iface, DCBTOOL, run_func, user_data, error, "%s", s->str);
212
213
		g_string_free (s, TRUE);
		if (!success)
214
			return FALSE;
215
216
217
218
	} else {
		/* Ignore disable failure since lldpad <= 0.9.46 does not support disabling
		 * priority groups without specifying an entire PG config.
		 */
219
		(void) do_helper (iface, DCBTOOL, run_func, user_data, error, "pg e:0");
220
221
222
223
224
225
226
227
228
229
230
	}

	return TRUE;
}

gboolean
_dcb_cleanup (const char *iface,
              DcbFunc run_func,
              gpointer user_data,
              GError **error)
{
231
232
233
234
235
236
237
238
239
240
241
242
243
244
	const char *cmds[] = {
		"dcb off",
		"app:fcoe e:0",
		"app:iscsi e:0",
		"app:fip e:0",
		"pfc e:0",
		"pg e:0",
		NULL
	};
	const char **iter = cmds;
	gboolean success = TRUE;

	/* Turn everything off and return first error we get (if any) */
	while (iter && *iter) {
Dan Winship's avatar
Dan Winship committed
245
		if (!do_helper (iface, DCBTOOL, run_func, user_data, success ? error : NULL, "%s", *iter))
246
247
248
249
250
			success = FALSE;
		iter++;
	}

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

gboolean
_fcoe_setup (const char *iface,
             NMSettingDcb *s_dcb,
             DcbFunc run_func,
             gpointer user_data,
             GError **error)
{
	NMSettingDcbFlags flags;

	g_assert (s_dcb);

	flags = nm_setting_dcb_get_app_fcoe_flags (s_dcb);
	if (flags & NM_SETTING_DCB_FLAG_ENABLE) {
		const char *mode = nm_setting_dcb_get_app_fcoe_mode (s_dcb);

		if (!do_helper (NULL, FCOEADM, run_func, user_data, error, "-m %s -c %s", mode, iface))
			return FALSE;
	} else {
		if (!do_helper (NULL, FCOEADM, run_func, user_data, error, "-d %s", iface))
			return FALSE;
	}

	return TRUE;
}

gboolean
_fcoe_cleanup (const char *iface,
               DcbFunc run_func,
               gpointer user_data,
               GError **error)
{
	return do_helper (NULL, FCOEADM, run_func, user_data, error, "-d %s", iface);
}


static const char *dcbpaths[] = {
	"/sbin/dcbtool",
	"/usr/sbin/dcbtool",
	"/usr/local/sbin/dcbtool",
	NULL
};
static const char *fcoepaths[] = {
	"/sbin/fcoeadm",
	"/usr/sbin/fcoeadm",
	"/usr/local/sbin/fcoeadm",
	NULL
};


static gboolean
run_helper (char **argv, guint which, gpointer user_data, GError **error)
{
	static const char *helper_path[2] = { NULL, NULL };
	int exit_status = 0;
	gboolean success;
	char *errmsg = NULL, *outmsg = NULL;
	const char **iter;
	char *cmdline;

	if (G_UNLIKELY (helper_path[which] == NULL)) {
		iter = (which == DCBTOOL) ? dcbpaths : fcoepaths;
		while (*iter) {
			if (g_file_test (*iter, G_FILE_TEST_EXISTS))
				helper_path[which] = *iter;
			iter++;
		}
		if (!helper_path[which]) {
			g_set_error (error, NM_DCB_ERROR, NM_DCB_ERROR_HELPER_NOT_FOUND,
			             "%s not found",
			             which == DCBTOOL ? "dcbtool" : "fcoadm");
			return FALSE;
		}
	}

	argv[0] = (char *) helper_path[which];
	cmdline = g_strjoinv (" ", argv);
	nm_log_dbg (LOGD_DCB, "%s", cmdline);

	success = g_spawn_sync ("/", argv, NULL, 0 /*G_SPAWN_DEFAULT*/,
	                        nm_unblock_posix_signals, NULL,
	                        &outmsg, &errmsg, &exit_status, error);
	/* Log any stderr output */
	if (success && WIFEXITED (exit_status) && WEXITSTATUS (exit_status) && (errmsg || outmsg)) {
336
337
338
339
340
341
342
343
344
345
346
347
348
		gboolean ignore_error = FALSE;

		/* Ignore fcoeadm "success" errors like when FCoE is already set up */
		if (errmsg && strstr (errmsg, "Connection already created"))
			ignore_error = TRUE;

		if (ignore_error == FALSE) {
			nm_log_warn (LOGD_DCB, "'%s' failed: '%s'",
			             cmdline, (errmsg && strlen (errmsg)) ? errmsg : outmsg);
			g_set_error (error, NM_DCB_ERROR, NM_DCB_ERROR_HELPER_FAILED,
			             "Failed to run '%s'", cmdline);
			success = FALSE;
		}
349
	}
Dan Williams's avatar
Dan Williams committed
350
	g_free (outmsg);
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
	g_free (errmsg);

	g_free (cmdline);
	return success;
}

gboolean
nm_dcb_setup (const char *iface, NMSettingDcb *s_dcb, GError **error)
{
	gboolean success;

	success = _dcb_setup (iface, s_dcb, run_helper, GUINT_TO_POINTER (DCBTOOL), error);
	if (success)
		success = _fcoe_setup (iface, s_dcb, run_helper, GUINT_TO_POINTER (FCOEADM), error);

	return success;
}

gboolean
nm_dcb_cleanup (const char *iface, GError **error)
{
Dan Williams's avatar
Dan Williams committed
372
373
374
375
376
377
378
379
380
	gboolean success;

	success = _dcb_cleanup (iface, run_helper, GUINT_TO_POINTER (DCBTOOL), error);
	if (success) {
		/* Only report FCoE errors if DCB cleanup was successful */
		success = _fcoe_cleanup (iface, run_helper, GUINT_TO_POINTER (FCOEADM), success ? error : NULL);
	}

	return success;
381
382
}