nm-setting-bond.c 34.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */

/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA.
 *
 * Copyright 2011 - 2013 Red Hat, Inc.
 */

22
#include "nm-default.h"
23

24 25 26 27 28 29 30 31 32
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "nm-setting-bond.h"
#include "nm-utils.h"
#include "nm-utils-private.h"
33
#include "nm-connection-private.h"
34
#include "nm-setting-infiniband.h"
35
#include "nm-core-internal.h"
36

37 38
/*****************************************************************************/

39 40 41 42 43 44 45 46
/**
 * SECTION:nm-setting-bond
 * @short_description: Describes connection properties for bonds
 *
 * The #NMSettingBond object is a #NMSetting subclass that describes properties
 * necessary for bond connections.
 **/

47
/*****************************************************************************/
48 49 50 51 52 53 54

enum {
	PROP_0,
	PROP_OPTIONS,
	LAST_PROP
};

55 56
typedef struct {
	GHashTable *options;
57
	NMUtilsNamedValue *options_idx_cache;
58 59
} NMSettingBondPrivate;

60
G_DEFINE_TYPE (NMSettingBond, nm_setting_bond, NM_TYPE_SETTING)
61 62 63 64 65

#define NM_SETTING_BOND_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_BOND, NMSettingBondPrivate))

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

66 67 68 69 70 71 72 73 74 75
typedef struct {
	const char *opt;
	const char *val;
	guint opt_type;
	guint min;
	guint max;
	char *list[10];
} BondDefault;

static const BondDefault defaults[] = {
76
	{ NM_SETTING_BOND_OPTION_MODE,             "balance-rr", NM_BOND_OPTION_TYPE_BOTH, 0, 6,
77
	  { "balance-rr", "active-backup", "balance-xor", "broadcast", "802.3ad", "balance-tlb", "balance-alb", NULL } },
78 79 80 81 82 83
	{ NM_SETTING_BOND_OPTION_MIIMON,           "100",        NM_BOND_OPTION_TYPE_INT, 0, G_MAXINT },
	{ NM_SETTING_BOND_OPTION_DOWNDELAY,        "0",          NM_BOND_OPTION_TYPE_INT, 0, G_MAXINT },
	{ NM_SETTING_BOND_OPTION_UPDELAY,          "0",          NM_BOND_OPTION_TYPE_INT, 0, G_MAXINT },
	{ NM_SETTING_BOND_OPTION_ARP_INTERVAL,     "0",          NM_BOND_OPTION_TYPE_INT, 0, G_MAXINT },
	{ NM_SETTING_BOND_OPTION_ARP_IP_TARGET,    "",           NM_BOND_OPTION_TYPE_IP },
	{ NM_SETTING_BOND_OPTION_ARP_VALIDATE,     "none",       NM_BOND_OPTION_TYPE_BOTH, 0, 3,
84
	  { "none", "active", "backup", "all", NULL } },
85 86
	{ NM_SETTING_BOND_OPTION_PRIMARY,          "",           NM_BOND_OPTION_TYPE_IFNAME },
	{ NM_SETTING_BOND_OPTION_PRIMARY_RESELECT, "always",     NM_BOND_OPTION_TYPE_BOTH, 0, 2,
87
	  { "always", "better", "failure", NULL } },
88
	{ NM_SETTING_BOND_OPTION_FAIL_OVER_MAC,    "none",       NM_BOND_OPTION_TYPE_BOTH, 0, 2,
89
	  { "none", "active", "follow", NULL } },
90 91
	{ NM_SETTING_BOND_OPTION_USE_CARRIER,      "1",          NM_BOND_OPTION_TYPE_INT, 0, 1 },
	{ NM_SETTING_BOND_OPTION_AD_SELECT,        "stable",     NM_BOND_OPTION_TYPE_BOTH, 0, 2,
92
	  { "stable", "bandwidth", "count", NULL } },
93 94
	{ NM_SETTING_BOND_OPTION_XMIT_HASH_POLICY, "layer2",     NM_BOND_OPTION_TYPE_BOTH, 0, 4,
	  { "layer2", "layer3+4", "layer2+3", "encap2+3", "encap3+4", NULL } },
95 96
	{ NM_SETTING_BOND_OPTION_RESEND_IGMP,      "1",          NM_BOND_OPTION_TYPE_INT, 0, 255 },
	{ NM_SETTING_BOND_OPTION_LACP_RATE,        "slow",       NM_BOND_OPTION_TYPE_BOTH, 0, 1,
97
	  { "slow", "fast", NULL } },
98 99
	{ NM_SETTING_BOND_OPTION_ACTIVE_SLAVE,     "",           NM_BOND_OPTION_TYPE_IFNAME },
	{ NM_SETTING_BOND_OPTION_AD_ACTOR_SYS_PRIO,"65535",      NM_BOND_OPTION_TYPE_INT, 1, 65535 },
100
	{ NM_SETTING_BOND_OPTION_AD_ACTOR_SYSTEM,  NULL,         NM_BOND_OPTION_TYPE_MAC },
101 102 103 104 105 106 107 108 109
	{ NM_SETTING_BOND_OPTION_AD_USER_PORT_KEY, "0",          NM_BOND_OPTION_TYPE_INT, 0, 1023},
	{ NM_SETTING_BOND_OPTION_ALL_SLAVES_ACTIVE,"0",          NM_BOND_OPTION_TYPE_INT, 0, 1},
	{ NM_SETTING_BOND_OPTION_ARP_ALL_TARGETS,  "any",        NM_BOND_OPTION_TYPE_BOTH, 0, 1, {"any", "all"}},
	{ NM_SETTING_BOND_OPTION_MIN_LINKS,        "0",          NM_BOND_OPTION_TYPE_INT, 0, G_MAXINT },
	{ NM_SETTING_BOND_OPTION_NUM_GRAT_ARP,     "1",          NM_BOND_OPTION_TYPE_INT, 0, 255 },
	{ NM_SETTING_BOND_OPTION_NUM_UNSOL_NA,     "1",          NM_BOND_OPTION_TYPE_INT, 0, 255 },
	{ NM_SETTING_BOND_OPTION_PACKETS_PER_SLAVE,"1",          NM_BOND_OPTION_TYPE_INT, 0, 65535 },
	{ NM_SETTING_BOND_OPTION_TLB_DYNAMIC_LB,   "1",          NM_BOND_OPTION_TYPE_INT, 0, 1 },
	{ NM_SETTING_BOND_OPTION_LP_INTERVAL,      "1",          NM_BOND_OPTION_TYPE_INT, 1, G_MAXINT },
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

/**
 * nm_setting_bond_get_num_options:
 * @setting: the #NMSettingBond
 *
 * Returns the number of options that should be set for this bond when it
 * is activated. This can be used to retrieve each option individually
 * using nm_setting_bond_get_option().
 *
 * Returns: the number of bonding options
 **/
guint32
nm_setting_bond_get_num_options (NMSettingBond *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_BOND (setting), 0);

	return g_hash_table_size (NM_SETTING_BOND_GET_PRIVATE (setting)->options);
}

/**
 * nm_setting_bond_get_option:
 * @setting: the #NMSettingBond
 * @idx: index of the desired option, from 0 to
 * nm_setting_bond_get_num_options() - 1
137 138 139 140 141
 * @out_name: (out) (transfer none): on return, the name of the bonding option;
 *   this value is owned by the setting and should not be modified
 * @out_value: (out) (transfer none): on return, the value of the name of the
 *   bonding option; this value is owned by the setting and should not be
 *   modified
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
 *
 * Given an index, return the value of the bonding option at that index.  Indexes
 * are *not* guaranteed to be static across modifications to options done by
 * nm_setting_bond_add_option() and nm_setting_bond_remove_option(),
 * and should not be used to refer to options except for short periods of time
 * such as during option iteration.
 *
 * Returns: %TRUE on success if the index was valid and an option was found,
 * %FALSE if the index was invalid (ie, greater than the number of options
 * currently held by the setting)
 **/
gboolean
nm_setting_bond_get_option (NMSettingBond *setting,
                            guint32 idx,
                            const char **out_name,
                            const char **out_value)
{
	NMSettingBondPrivate *priv;
160
	guint len;
161 162 163 164 165

	g_return_val_if_fail (NM_IS_SETTING_BOND (setting), FALSE);

	priv = NM_SETTING_BOND_GET_PRIVATE (setting);

166 167
	len = g_hash_table_size (priv->options);
	if (idx >= len)
168 169
		return FALSE;

170 171
	if (!G_UNLIKELY (priv->options_idx_cache))
		priv->options_idx_cache = nm_utils_named_values_from_str_dict (priv->options, NULL);
172 173 174

	NM_SET_OUT (out_name, priv->options_idx_cache[idx].name);
	NM_SET_OUT (out_value, priv->options_idx_cache[idx].value_str);
175 176 177 178 179 180
	return TRUE;
}

static gboolean
validate_int (const char *name, const char *value, const BondDefault *def)
{
181
	long num;
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
	guint i;

	for (i = 0; i < strlen (value); i++) {
		if (!g_ascii_isdigit (value[i]) && value[i] != '-')
			return FALSE;
	}

	errno = 0;
	num = strtol (value, NULL, 10);
	if (errno)
		return FALSE;
	if (num < def->min || num > def->max)
		return FALSE;

	return TRUE;
}

static gboolean
validate_list (const char *name, const char *value, const BondDefault *def)
{
	guint i;

	for (i = 0; i < G_N_ELEMENTS (def->list) && def->list[i]; i++) {
		if (g_strcmp0 (def->list[i], value) == 0)
			return TRUE;
	}

	/* empty validation list means all values pass */
	return def->list[0] == NULL ? TRUE : FALSE;
}

static gboolean
validate_ip (const char *name, const char *value)
{
216
	gs_free char *value_clone = NULL;
217 218 219 220 221
	struct in_addr addr;

	if (!value || !value[0])
		return FALSE;

222 223 224 225
	value_clone = g_strdup (value);
	value = value_clone;
	for (;;) {
		char *eow;
226

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
		/* we do not skip over empty words. E.g
		 * "192.168.1.1," is an error.
		 *
		 * ... for no particular reason. */

		eow = strchr (value, ',');
		if (eow)
			*eow = '\0';

		if (inet_pton (AF_INET, value, &addr) != 1)
			return FALSE;

		if (!eow)
			break;
		value = eow + 1;
	}
	return TRUE;
244 245 246 247 248 249 250 251
}

static gboolean
validate_ifname (const char *name, const char *value)
{
	if (!value || !value[0])
		return FALSE;

252
	return nm_utils_is_valid_iface_name (value, NULL);
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
}

/**
 * nm_setting_bond_validate_option:
 * @name: the name of the option to validate
 * @value: the value of the option to validate
 *
 * Checks whether @name is a valid bond option and @value is a valid value for
 * the @name. If @value is %NULL, the function only validates the option name.
 *
 * Returns: %TRUE, if the @value is valid for the given name.
 * If the @name is not a valid option, %FALSE will be returned.
 **/
gboolean
nm_setting_bond_validate_option (const char *name,
                                 const char *value)
{
	guint i;

	if (!name || !name[0])
		return FALSE;

	for (i = 0; i < G_N_ELEMENTS (defaults); i++) {
		if (g_strcmp0 (defaults[i].opt, name) == 0) {
			if (value == NULL)
				return TRUE;
			switch (defaults[i].opt_type) {
280
			case NM_BOND_OPTION_TYPE_INT:
281
				return validate_int (name, value, &defaults[i]);
282
			case NM_BOND_OPTION_TYPE_STRING:
283
				return validate_list (name, value, &defaults[i]);
284
			case NM_BOND_OPTION_TYPE_BOTH:
285 286
				return (   validate_int (name, value, &defaults[i])
				        || validate_list (name, value, &defaults[i]));
287
			case NM_BOND_OPTION_TYPE_IP:
288
				return validate_ip (name, value);
289 290
			case NM_BOND_OPTION_TYPE_MAC:
				return nm_utils_hwaddr_valid (value, ETH_ALEN);
291
			case NM_BOND_OPTION_TYPE_IFNAME:
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
				return validate_ifname (name, value);
			}
			return FALSE;
		}
	}
	return FALSE;
}

/**
 * nm_setting_bond_get_option_by_name:
 * @setting: the #NMSettingBond
 * @name: the option name for which to retrieve the value
 *
 * Returns the value associated with the bonding option specified by
 * @name, if it exists.
 *
 * Returns: the value, or %NULL if the key/value pair was never added to the
 * setting; the value is owned by the setting and must not be modified
 **/
const char *
nm_setting_bond_get_option_by_name (NMSettingBond *setting,
                                    const char *name)
{
	g_return_val_if_fail (NM_IS_SETTING_BOND (setting), NULL);

	if (!nm_setting_bond_validate_option (name, NULL))
		return NULL;

	return g_hash_table_lookup (NM_SETTING_BOND_GET_PRIVATE (setting)->options, name);
}

/**
 * nm_setting_bond_add_option:
 * @setting: the #NMSettingBond
 * @name: name for the option
 * @value: value for the option
 *
 * Add an option to the table.  The option is compared to an internal list
 * of allowed options.  Option names may contain only alphanumeric characters
 * (ie [a-zA-Z0-9]).  Adding a new name replaces any existing name/value pair
 * that may already exist.
 *
 * The order of how to set several options is relevant because there are options
 * that conflict with each other.
 *
 * Returns: %TRUE if the option was valid and was added to the internal option
 * list, %FALSE if it was not.
 **/
gboolean
nm_setting_bond_add_option (NMSettingBond *setting,
                            const char *name,
                            const char *value)
{
	NMSettingBondPrivate *priv;

	g_return_val_if_fail (NM_IS_SETTING_BOND (setting), FALSE);

	if (!value || !nm_setting_bond_validate_option (name, value))
		return FALSE;

	priv = NM_SETTING_BOND_GET_PRIVATE (setting);

354
	nm_clear_g_free (&priv->options_idx_cache);
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
	g_hash_table_insert (priv->options, g_strdup (name), g_strdup (value));

	if (   !strcmp (name, NM_SETTING_BOND_OPTION_MIIMON)
	    && strcmp (value, "0") != 0) {
		g_hash_table_remove (priv->options, NM_SETTING_BOND_OPTION_ARP_INTERVAL);
		g_hash_table_remove (priv->options, NM_SETTING_BOND_OPTION_ARP_IP_TARGET);
	} else if (   !strcmp (name, NM_SETTING_BOND_OPTION_ARP_INTERVAL)
	           && strcmp (value, "0") != 0) {
		g_hash_table_remove (priv->options, NM_SETTING_BOND_OPTION_MIIMON);
		g_hash_table_remove (priv->options, NM_SETTING_BOND_OPTION_DOWNDELAY);
		g_hash_table_remove (priv->options, NM_SETTING_BOND_OPTION_UPDELAY);
	}

	g_object_notify (G_OBJECT (setting), NM_SETTING_BOND_OPTIONS);

	return TRUE;
}

/**
 * nm_setting_bond_remove_option:
 * @setting: the #NMSettingBond
 * @name: name of the option to remove
 *
 * Remove the bonding option referenced by @name from the internal option
 * list.
 *
 * Returns: %TRUE if the option was found and removed from the internal option
 * list, %FALSE if it was not.
 **/
gboolean
nm_setting_bond_remove_option (NMSettingBond *setting,
                               const char *name)
{
388
	NMSettingBondPrivate *priv;
389 390 391 392 393 394 395
	gboolean found;

	g_return_val_if_fail (NM_IS_SETTING_BOND (setting), FALSE);

	if (!nm_setting_bond_validate_option (name, NULL))
		return FALSE;

396 397 398 399
	priv = NM_SETTING_BOND_GET_PRIVATE (setting);

	nm_clear_g_free (&priv->options_idx_cache);
	found = g_hash_table_remove (priv->options, name);
400 401 402 403 404 405 406
	if (found)
		g_object_notify (G_OBJECT (setting), NM_SETTING_BOND_OPTIONS);
	return found;
}

/**
 * nm_setting_bond_get_valid_options:
407
 * @setting: (allow-none): the #NMSettingBond
408 409 410
 *
 * Returns a list of valid bond options.
 *
411 412
 * The @setting argument is unused and may be passed as %NULL.
 *
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
 * Returns: (transfer none): a %NULL-terminated array of strings of valid bond options.
 **/
const char **
nm_setting_bond_get_valid_options  (NMSettingBond *setting)
{
	static const char *array[G_N_ELEMENTS (defaults) + 1] = { NULL };
	int i;

	/* initialize the array once */
	if (G_UNLIKELY (array[0] == NULL)) {
		for (i = 0; i < G_N_ELEMENTS (defaults); i++)
			array[i] = defaults[i].opt;
		array[i] = NULL;
	}
	return array;
}

/**
 * nm_setting_bond_get_option_default:
 * @setting: the #NMSettingBond
 * @name: the name of the option
 *
 * Returns: the value of the bond option if not overridden by an entry in
 *   the #NMSettingBond:options property.
 **/
const char *
nm_setting_bond_get_option_default (NMSettingBond *setting, const char *name)
{
441
	const char *mode;
442 443 444 445 446
	guint i;

	g_return_val_if_fail (NM_IS_SETTING_BOND (setting), NULL);
	g_return_val_if_fail (nm_setting_bond_validate_option (name, NULL), NULL);

447 448 449 450 451 452 453 454 455 456
	if (nm_streq (name, NM_SETTING_BOND_OPTION_AD_ACTOR_SYSTEM)) {
		/* The default value depends on the current mode */
		mode = nm_setting_bond_get_option_by_name (setting, NM_SETTING_BOND_OPTION_MODE);
		if (   nm_streq0 (mode, "4")
		    || nm_streq0 (mode, "802.3ad"))
			return "00:00:00:00:00:00";
		else
			return "";
	}

457 458 459 460 461 462 463 464
	for (i = 0; i < G_N_ELEMENTS (defaults); i++) {
		if (g_strcmp0 (defaults[i].opt, name) == 0)
			return defaults[i].val;
	}
	/* Any option that passes nm_setting_bond_validate_option() should also be found in defaults */
	g_assert_not_reached ();
}

465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
/**
 * nm_setting_bond_get_option_type:
 * @setting: the #NMSettingBond
 * @name: the name of the option
 *
 * Returns: the type of the bond option.
 **/
NMBondOptionType
_nm_setting_bond_get_option_type (NMSettingBond *setting, const char *name)
{
	guint i;

	g_return_val_if_fail (NM_IS_SETTING_BOND (setting), NM_BOND_OPTION_TYPE_INT);
	g_return_val_if_fail (nm_setting_bond_validate_option (name, NULL), NM_BOND_OPTION_TYPE_INT);

	for (i = 0; i < G_N_ELEMENTS (defaults); i++) {
		if (nm_streq0 (defaults[i].opt, name))
			return defaults[i].opt_type;
	}
	/* Any option that passes nm_setting_bond_validate_option() should also be found in defaults */
	g_assert_not_reached ();
}

488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
NMBondMode
_nm_setting_bond_mode_from_string (const char *str)
{
	g_return_val_if_fail (str, NM_BOND_MODE_UNKNOWN);

	if (nm_streq (str, "balance-rr"))
		return NM_BOND_MODE_ROUNDROBIN;
	if (nm_streq (str, "active-backup"))
		return NM_BOND_MODE_ACTIVEBACKUP;
	if (nm_streq (str, "balance-xor"))
		return NM_BOND_MODE_XOR;
	if (nm_streq (str, "broadcast"))
		return NM_BOND_MODE_BROADCAST;
	if (nm_streq (str, "802.3ad"))
		return NM_BOND_MODE_8023AD;
	if (nm_streq (str, "balance-tlb"))
		return NM_BOND_MODE_TLB;
	if (nm_streq (str, "balance-alb"))
		return NM_BOND_MODE_ALB;

	return NM_BOND_MODE_UNKNOWN;
}

511 512
/*****************************************************************************/

513 514
#define BIT(x) (1 << (x))

515
static const struct {
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
	const char *option;
	NMBondMode unsupp_modes;
} bond_unsupp_modes[] = {
	{ NM_SETTING_BOND_OPTION_PACKETS_PER_SLAVE, ~(BIT (NM_BOND_MODE_ROUNDROBIN)) },
	{ NM_SETTING_BOND_OPTION_ARP_VALIDATE,      BIT (NM_BOND_MODE_8023AD) | BIT (NM_BOND_MODE_TLB) | BIT (NM_BOND_MODE_ALB) },
	{ NM_SETTING_BOND_OPTION_ARP_INTERVAL,      BIT (NM_BOND_MODE_8023AD) | BIT (NM_BOND_MODE_TLB) | BIT (NM_BOND_MODE_ALB) },
	{ NM_SETTING_BOND_OPTION_LACP_RATE,         ~(BIT (NM_BOND_MODE_8023AD)) },
	{ NM_SETTING_BOND_OPTION_PRIMARY,           ~(BIT (NM_BOND_MODE_ACTIVEBACKUP) | BIT (NM_BOND_MODE_TLB) | BIT (NM_BOND_MODE_ALB)) },
	{ NM_SETTING_BOND_OPTION_ACTIVE_SLAVE,      ~(BIT (NM_BOND_MODE_ACTIVEBACKUP) | BIT (NM_BOND_MODE_TLB) | BIT (NM_BOND_MODE_ALB)) },
	{ NM_SETTING_BOND_OPTION_TLB_DYNAMIC_LB,    ~(BIT (NM_BOND_MODE_TLB)) },
	{ NM_SETTING_BOND_OPTION_AD_ACTOR_SYS_PRIO, ~(BIT (NM_BOND_MODE_8023AD)) },
	{ NM_SETTING_BOND_OPTION_AD_ACTOR_SYSTEM,   ~(BIT (NM_BOND_MODE_8023AD)) },
	{ NM_SETTING_BOND_OPTION_AD_USER_PORT_KEY,  ~(BIT (NM_BOND_MODE_8023AD)) },
};

gboolean
_nm_setting_bond_option_supported (const char *option, NMBondMode mode)
{
	guint i;

	for (i = 0; i < G_N_ELEMENTS (bond_unsupp_modes); i++) {
		if (nm_streq (option, bond_unsupp_modes[i].option))
538
			return !NM_FLAGS_ANY (bond_unsupp_modes[i].unsupp_modes, BIT (mode));
539 540 541 542 543
	}

	return TRUE;
}

544
static gboolean
545
verify (NMSetting *setting, NMConnection *connection, GError **error)
546 547 548 549
{
	NMSettingBondPrivate *priv = NM_SETTING_BOND_GET_PRIVATE (setting);
	GHashTableIter iter;
	const char *key, *value;
550
	int mode, miimon = 0, arp_interval = 0;
551
	int num_grat_arp = -1, num_unsol_na = -1;
552
	const char *mode_orig, *mode_new;
553
	const char *arp_ip_target = NULL;
554
	const char *lacp_rate;
555
	const char *primary;
556
	NMBondMode bond_mode;
557 558 559 560 561

	g_hash_table_iter_init (&iter, priv->options);
	while (g_hash_table_iter_next (&iter, (gpointer) &key, (gpointer) &value)) {
		if (!value[0] || !nm_setting_bond_validate_option (key, value)) {
			g_set_error (error,
562 563
			             NM_CONNECTION_ERROR,
			             NM_CONNECTION_ERROR_INVALID_PROPERTY,
564 565 566 567 568 569 570 571 572 573 574 575 576
			             _("invalid option '%s' or its value '%s'"),
			             key, value);
			g_prefix_error (error, "%s.%s: ", NM_SETTING_BOND_SETTING_NAME, NM_SETTING_BOND_OPTIONS);
			return FALSE;
		}
	}

	value = g_hash_table_lookup (priv->options, NM_SETTING_BOND_OPTION_MIIMON);
	if (value)
		miimon = atoi (value);
	value = g_hash_table_lookup (priv->options, NM_SETTING_BOND_OPTION_ARP_INTERVAL);
	if (value)
		arp_interval = atoi (value);
577 578 579 580 581 582
	value = g_hash_table_lookup (priv->options, NM_SETTING_BOND_OPTION_NUM_GRAT_ARP);
	if (value)
		num_grat_arp = atoi (value);
	value = g_hash_table_lookup (priv->options, NM_SETTING_BOND_OPTION_NUM_UNSOL_NA);
	if (value)
		num_unsol_na = atoi (value);
583 584 585 586

	/* Can only set one of miimon and arp_interval */
	if (miimon > 0 && arp_interval > 0) {
		g_set_error (error,
587 588
		             NM_CONNECTION_ERROR,
		             NM_CONNECTION_ERROR_INVALID_PROPERTY,
589 590 591 592
		             _("only one of '%s' and '%s' can be set"),
		             NM_SETTING_BOND_OPTION_MIIMON,
		             NM_SETTING_BOND_OPTION_ARP_INTERVAL);
		g_prefix_error (error, "%s.%s: ", NM_SETTING_BOND_SETTING_NAME, NM_SETTING_BOND_OPTIONS);
593
		return FALSE;
594 595
	}

596
	/* Verify bond mode */
597 598
	mode_orig = g_hash_table_lookup (priv->options, NM_SETTING_BOND_OPTION_MODE);
	if (!mode_orig) {
599
		g_set_error (error,
600 601
		             NM_CONNECTION_ERROR,
		             NM_CONNECTION_ERROR_INVALID_PROPERTY,
602 603 604 605 606
		             _("mandatory option '%s' is missing"),
		             NM_SETTING_BOND_OPTION_MODE);
		g_prefix_error (error, "%s.%s: ", NM_SETTING_BOND_SETTING_NAME, NM_SETTING_BOND_OPTIONS);
		return FALSE;
	}
607
	mode = nm_utils_bond_mode_string_to_int (mode_orig);
608
	if (mode == -1) {
609
		g_set_error (error,
610 611
		             NM_CONNECTION_ERROR,
		             NM_CONNECTION_ERROR_INVALID_PROPERTY,
612 613 614 615 616
		             _("'%s' is not a valid value for '%s'"),
		             value, NM_SETTING_BOND_OPTION_MODE);
		g_prefix_error (error, "%s.%s: ", NM_SETTING_BOND_SETTING_NAME, NM_SETTING_BOND_OPTIONS);
		return FALSE;
	}
617
	mode_new = nm_utils_bond_mode_int_to_string (mode);
618 619

	/* Make sure mode is compatible with other settings */
620 621
	if (   strcmp (mode_new, "balance-alb") == 0
	    || strcmp (mode_new, "balance-tlb") == 0) {
622 623
		if (arp_interval > 0) {
			g_set_error (error,
624 625
			             NM_CONNECTION_ERROR,
			             NM_CONNECTION_ERROR_INVALID_PROPERTY,
626
			             _("'%s=%s' is incompatible with '%s > 0'"),
627
			             NM_SETTING_BOND_OPTION_MODE, mode_new, NM_SETTING_BOND_OPTION_ARP_INTERVAL);
628 629 630 631 632 633
			g_prefix_error (error, "%s.%s: ", NM_SETTING_BOND_SETTING_NAME, NM_SETTING_BOND_OPTIONS);
			return FALSE;
		}
	}

	primary = g_hash_table_lookup (priv->options, NM_SETTING_BOND_OPTION_PRIMARY);
634
	if (strcmp (mode_new, "active-backup") == 0) {
635 636 637
		GError *tmp_error = NULL;

		if (primary && !nm_utils_is_valid_iface_name (primary, &tmp_error)) {
638
			g_set_error (error,
639 640
			             NM_CONNECTION_ERROR,
			             NM_CONNECTION_ERROR_INVALID_PROPERTY,
641
			             _("'%s' is not valid for the '%s' option: %s"),
642
			             primary, NM_SETTING_BOND_OPTION_PRIMARY, tmp_error->message);
643
			g_prefix_error (error, "%s.%s: ", NM_SETTING_BOND_SETTING_NAME, NM_SETTING_BOND_OPTIONS);
644
			g_error_free (tmp_error);
645 646 647 648 649
			return FALSE;
		}
	} else {
		if (primary) {
			g_set_error (error,
650 651
			             NM_CONNECTION_ERROR,
			             NM_CONNECTION_ERROR_INVALID_PROPERTY,
652 653 654 655 656 657 658 659
			             _("'%s' option is only valid for '%s=%s'"),
			             NM_SETTING_BOND_OPTION_PRIMARY,
			             NM_SETTING_BOND_OPTION_MODE, "active-backup");
			g_prefix_error (error, "%s.%s: ", NM_SETTING_BOND_SETTING_NAME, NM_SETTING_BOND_OPTIONS);
			return FALSE;
		}
	}

660
	if (connection && nm_connection_get_setting_infiniband (connection)) {
661
		if (strcmp (mode_new, "active-backup") != 0) {
662
			g_set_error (error,
663 664
			             NM_CONNECTION_ERROR,
			             NM_CONNECTION_ERROR_INVALID_PROPERTY,
665
			             _("'%s=%s' is not a valid configuration for '%s'"),
666
			             NM_SETTING_BOND_OPTION_MODE, mode_new, NM_SETTING_INFINIBAND_SETTING_NAME);
667 668 669 670 671 672 673 674 675
			g_prefix_error (error, "%s.%s: ", NM_SETTING_BOND_SETTING_NAME, NM_SETTING_BOND_OPTIONS);
			return FALSE;
		}
	}

	if (miimon == 0) {
		/* updelay and downdelay can only be used with miimon */
		if (g_hash_table_lookup (priv->options, NM_SETTING_BOND_OPTION_UPDELAY)) {
			g_set_error (error,
676 677
			             NM_CONNECTION_ERROR,
			             NM_CONNECTION_ERROR_INVALID_PROPERTY,
678 679 680 681 682 683 684
			             _("'%s' option requires '%s' option to be set"),
			             NM_SETTING_BOND_OPTION_UPDELAY, NM_SETTING_BOND_OPTION_MIIMON);
			g_prefix_error (error, "%s.%s: ", NM_SETTING_BOND_SETTING_NAME, NM_SETTING_BOND_OPTIONS);
			return FALSE;
		}
		if (g_hash_table_lookup (priv->options, NM_SETTING_BOND_OPTION_DOWNDELAY)) {
			g_set_error (error,
685 686
			             NM_CONNECTION_ERROR,
			             NM_CONNECTION_ERROR_INVALID_PROPERTY,
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
			             _("'%s' option requires '%s' option to be set"),
			             NM_SETTING_BOND_OPTION_DOWNDELAY, NM_SETTING_BOND_OPTION_MIIMON);
			g_prefix_error (error, "%s.%s: ", NM_SETTING_BOND_SETTING_NAME, NM_SETTING_BOND_OPTIONS);
			return FALSE;
		}
	}

	/* arp_ip_target can only be used with arp_interval, and must
	 * contain a comma-separated list of IPv4 addresses.
	 */
	arp_ip_target = g_hash_table_lookup (priv->options, NM_SETTING_BOND_OPTION_ARP_IP_TARGET);
	if (arp_interval > 0) {
		char **addrs;
		guint32 addr;
		int i;

		if (!arp_ip_target) {
			g_set_error (error,
705 706
			             NM_CONNECTION_ERROR,
			             NM_CONNECTION_ERROR_INVALID_PROPERTY,
707 708 709 710 711 712 713 714 715
			             _("'%s' option requires '%s' option to be set"),
			             NM_SETTING_BOND_OPTION_ARP_INTERVAL, NM_SETTING_BOND_OPTION_ARP_IP_TARGET);
			g_prefix_error (error, "%s.%s: ", NM_SETTING_BOND_SETTING_NAME, NM_SETTING_BOND_OPTIONS);
			return FALSE;
		}

		addrs = g_strsplit (arp_ip_target, ",", -1);
		if (!addrs[0]) {
			g_set_error (error,
716 717
			             NM_CONNECTION_ERROR,
			             NM_CONNECTION_ERROR_INVALID_PROPERTY,
718 719 720 721 722 723 724 725 726 727
			             _("'%s' option is empty"),
			             NM_SETTING_BOND_OPTION_ARP_IP_TARGET);
			g_prefix_error (error, "%s.%s: ", NM_SETTING_BOND_SETTING_NAME, NM_SETTING_BOND_OPTIONS);
			g_strfreev (addrs);
			return FALSE;
		}

		for (i = 0; addrs[i]; i++) {
			if (!inet_pton (AF_INET, addrs[i], &addr)) {
				g_set_error (error,
728 729
				             NM_CONNECTION_ERROR,
				             NM_CONNECTION_ERROR_INVALID_PROPERTY,
730 731 732 733 734 735 736 737 738 739 740
				             _("'%s' is not a valid IPv4 address for '%s' option"),
				             NM_SETTING_BOND_OPTION_ARP_IP_TARGET, addrs[i]);
				g_prefix_error (error, "%s.%s: ", NM_SETTING_BOND_SETTING_NAME, NM_SETTING_BOND_OPTIONS);
				g_strfreev (addrs);
				return FALSE;
			}
		}
		g_strfreev (addrs);
	} else {
		if (arp_ip_target) {
			g_set_error (error,
741 742
			             NM_CONNECTION_ERROR,
			             NM_CONNECTION_ERROR_INVALID_PROPERTY,
743 744 745 746 747 748 749
			             _("'%s' option requires '%s' option to be set"),
			             NM_SETTING_BOND_OPTION_ARP_IP_TARGET, NM_SETTING_BOND_OPTION_ARP_INTERVAL);
			g_prefix_error (error, "%s.%s: ", NM_SETTING_BOND_SETTING_NAME, NM_SETTING_BOND_OPTIONS);
			return FALSE;
		}
	}

750 751
	lacp_rate = g_hash_table_lookup (priv->options, NM_SETTING_BOND_OPTION_LACP_RATE);
	if (   lacp_rate
752 753 754
	    && g_strcmp0 (mode_new, "802.3ad")
	    && strcmp (lacp_rate, "slow") != 0
	    && strcmp (lacp_rate, "0") != 0) {
755
		g_set_error (error,
756 757
		             NM_CONNECTION_ERROR,
		             NM_CONNECTION_ERROR_INVALID_PROPERTY,
758 759 760
		             _("'%s' option is only valid with mode '%s'"),
		             NM_SETTING_BOND_OPTION_LACP_RATE, "802.3ad");
		g_prefix_error (error, "%s.%s: ", NM_SETTING_BOND_SETTING_NAME, NM_SETTING_BOND_OPTIONS);
761 762 763 764 765 766 767 768 769 770 771 772
		return FALSE;
	}

	if (   (num_grat_arp != -1 && num_unsol_na != -1)
	    && (num_grat_arp != num_unsol_na)) {
		g_set_error (error,
		             NM_CONNECTION_ERROR,
		             NM_CONNECTION_ERROR_INVALID_PROPERTY,
		             _("'%s' and '%s' cannot have different values"),
		             NM_SETTING_BOND_OPTION_NUM_GRAT_ARP,
		             NM_SETTING_BOND_OPTION_NUM_UNSOL_NA);
		g_prefix_error (error, "%s.%s: ", NM_SETTING_BOND_SETTING_NAME, NM_SETTING_BOND_OPTIONS);
773 774 775
		return FALSE;
	}

776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
	if (!_nm_connection_verify_required_interface_name (connection, error))
		return FALSE;

	/* *** errors above here should be always fatal, below NORMALIZABLE_ERROR *** */

	if (g_strcmp0 (mode_orig, mode_new) != 0) {
		g_set_error (error,
		             NM_CONNECTION_ERROR,
		             NM_CONNECTION_ERROR_INVALID_PROPERTY,
		             _("'%s' option should be string"),
		             NM_SETTING_BOND_OPTION_MODE);
		g_prefix_error (error, "%s.%s: ", NM_SETTING_BOND_SETTING_NAME, NM_SETTING_BOND_OPTIONS);
		return NM_SETTING_VERIFY_NORMALIZABLE;
	}

791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
	/* normalize unsupported options for the current mode */
	bond_mode = _nm_setting_bond_mode_from_string (mode_new);
	g_hash_table_iter_init (&iter, priv->options);
	while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL)) {
		if (nm_streq (key, "mode"))
			continue;
		if (!_nm_setting_bond_option_supported (key, bond_mode)) {
			g_set_error (error,
			             NM_CONNECTION_ERROR,
			             NM_CONNECTION_ERROR_INVALID_PROPERTY,
			             _("'%s' option is not valid with mode '%s'"),
			             key, mode_new);
			g_prefix_error (error, "%s.%s: ", NM_SETTING_BOND_SETTING_NAME, NM_SETTING_BOND_OPTIONS);
			return NM_SETTING_VERIFY_NORMALIZABLE;
		}
	}

808
	return TRUE;
809 810
}

811 812
/*****************************************************************************/

813
static gboolean
814 815 816 817
options_hash_match (NMSettingBond *s_bond,
                    GHashTable *options1,
                    GHashTable *options2,
                    NMSettingCompareFlags flags)
818 819 820 821 822 823
{
	GHashTableIter iter;
	const char *key, *value, *value2;

	g_hash_table_iter_init (&iter, options1);
	while (g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *) &value)) {
824 825 826 827 828 829 830 831 832 833 834

		if (NM_FLAGS_HAS (flags, NM_SETTING_COMPARE_FLAG_INFERRABLE)) {
			/* when doing an inferrable match, the active-slave should be ignored
			 * as it might be differ from the setting in the connection.
			 *
			 * Also, the fail_over_mac setting can change, see for example
			 * https://bugzilla.redhat.com/show_bug.cgi?id=1375558#c8 */
			if (NM_IN_STRSET (key, "fail_over_mac", "active_slave"))
				continue;
		}

835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
		value2 = g_hash_table_lookup (options2, key);

		if (!value2) {
			if (nm_streq (key, "num_grat_arp"))
				value2 = g_hash_table_lookup (options2, "num_unsol_na");
			else if (nm_streq (key, "num_unsol_na"))
				value2 = g_hash_table_lookup (options2, "num_grat_arp");
		}

		if (value2) {
			if (nm_streq (value, value2))
				continue;
		} else {
			if (nm_streq (value, nm_setting_bond_get_option_default (s_bond, key)))
				continue;
		}

		return FALSE;
	}

	return TRUE;
}

static gboolean
859 860 861 862
options_equal (NMSettingBond *s_bond,
               GHashTable *options1,
               GHashTable *options2,
               NMSettingCompareFlags flags)
863
{
864 865
	return    options_hash_match (s_bond, options1, options2, flags)
	       && options_hash_match (s_bond, options2, options1, flags);
866 867 868 869 870 871 872 873
}

static gboolean
compare_property (NMSetting *setting,
                  NMSetting *other,
                  const GParamSpec *prop_spec,
                  NMSettingCompareFlags flags)
{
874
	NMSettingClass *setting_class;
875 876 877 878

	if (nm_streq0 (prop_spec->name, NM_SETTING_BOND_OPTIONS)) {
		return options_equal (NM_SETTING_BOND (setting),
		                      NM_SETTING_BOND_GET_PRIVATE (setting)->options,
879 880
		                      NM_SETTING_BOND_GET_PRIVATE (other)->options,
		                      flags);
881 882
	}

883 884
	setting_class = NM_SETTING_CLASS (nm_setting_bond_parent_class);
	return setting_class->compare_property (setting, other, prop_spec, flags);
885 886
}

887
/*****************************************************************************/
888 889

static void
890 891
get_property (GObject *object, guint prop_id,
              GValue *value, GParamSpec *pspec)
892 893 894
{
	NMSettingBondPrivate *priv = NM_SETTING_BOND_GET_PRIVATE (object);

895 896 897 898 899 900 901 902
	switch (prop_id) {
	case PROP_OPTIONS:
		g_value_take_boxed (value, _nm_utils_copy_strdict (priv->options));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
903 904 905 906 907 908 909 910 911 912
}

static void
set_property (GObject *object, guint prop_id,
              const GValue *value, GParamSpec *pspec)
{
	NMSettingBondPrivate *priv = NM_SETTING_BOND_GET_PRIVATE (object);

	switch (prop_id) {
	case PROP_OPTIONS:
913
		nm_clear_g_free (&priv->options_idx_cache);
914 915
		g_hash_table_unref (priv->options);
		priv->options = _nm_utils_copy_strdict (g_value_get_boxed (value));
916 917 918 919 920 921 922
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

923 924
/*****************************************************************************/

925
static void
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
nm_setting_bond_init (NMSettingBond *setting)
{
	NMSettingBondPrivate *priv = NM_SETTING_BOND_GET_PRIVATE (setting);

	priv->options = g_hash_table_new_full (nm_str_hash, g_str_equal, g_free, g_free);

	/* Default values: */
	nm_setting_bond_add_option (setting, NM_SETTING_BOND_OPTION_MODE, "balance-rr");
}

/**
 * nm_setting_bond_new:
 *
 * Creates a new #NMSettingBond object with default values.
 *
 * Returns: (transfer full): the new empty #NMSettingBond object
 **/
NMSetting *
nm_setting_bond_new (void)
{
	return (NMSetting *) g_object_new (NM_TYPE_SETTING_BOND, NULL);
}

static void
finalize (GObject *object)
951 952 953
{
	NMSettingBondPrivate *priv = NM_SETTING_BOND_GET_PRIVATE (object);

954
	nm_clear_g_free (&priv->options_idx_cache);
955 956 957
	g_hash_table_destroy (priv->options);

	G_OBJECT_CLASS (nm_setting_bond_parent_class)->finalize (object);
958 959 960
}

static void
961
nm_setting_bond_class_init (NMSettingBondClass *klass)
962
{
963 964
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	NMSettingClass *setting_class = NM_SETTING_CLASS (klass);
965
	GArray *properties_override = _nm_sett_info_property_override_create_array ();
966

967
	g_type_class_add_private (klass, sizeof (NMSettingBondPrivate));
968

969 970 971
	object_class->set_property     = set_property;
	object_class->get_property     = get_property;
	object_class->finalize         = finalize;
972

973 974 975
	setting_class->verify           = verify;
	setting_class->compare_property = compare_property;

976
	/**
977
	 * NMSettingBond:options: (type GHashTable(utf8,utf8)):
978 979 980 981 982
	 *
	 * Dictionary of key/value pairs of bonding options.  Both keys and values
	 * must be strings. Option names must contain only alphanumeric characters
	 * (ie, [a-zA-Z0-9]).
	 **/
983 984 985 986 987 988 989
	/* ---ifcfg-rh---
	 * property: options
	 * variable: BONDING_OPTS
	 * description: Bonding options.
	 * example: BONDING_OPTS="miimon=100 mode=broadcast"
	 * ---end---
	 */
990 991
	 g_object_class_install_property
		 (object_class, PROP_OPTIONS,
992 993 994 995 996
		 g_param_spec_boxed (NM_SETTING_BOND_OPTIONS, "", "",
		                     G_TYPE_HASH_TABLE,
		                     G_PARAM_READWRITE |
		                     NM_SETTING_PARAM_INFERRABLE |
		                     G_PARAM_STATIC_STRINGS));
997 998 999 1000 1001 1002 1003

	_properties_override_add_transform (properties_override,
	                                    g_object_class_find_property (G_OBJECT_CLASS (setting_class),
	                                                                  NM_SETTING_BOND_OPTIONS),
	                                    G_VARIANT_TYPE ("a{ss}"),
	                                    _nm_utils_strdict_to_dbus,
	                                    _nm_utils_strdict_from_dbus);
1004

1005 1006 1007 1008 1009 1010 1011 1012
	 /* ---dbus---
	  * property: interface-name
	  * format: string
	  * description: Deprecated in favor of connection.interface-name, but can
	  *   be used for backward-compatibility with older daemons, to set the
	  *   bond's interface name.
	  * ---end---
	  */
1013 1014 1015 1016 1017 1018 1019 1020
	_properties_override_add_dbus_only (properties_override,
	                                    "interface-name",
	                                    G_VARIANT_TYPE_STRING,
	                                    _nm_setting_get_deprecated_virtual_interface_name,
	                                    NULL);

	_nm_setting_class_commit_full (setting_class, NM_META_SETTING_TYPE_BOND,
	                               NULL, properties_override);
1021
}