bond_netlink.c 13.7 KB
Newer Older
1 2 3
/*
 * drivers/net/bond/bond_netlink.c - Netlink interface for bonding
 * Copyright (c) 2013 Jiri Pirko <jiri@resnulli.us>
4
 * Copyright (c) 2013 Scott Feldman <sfeldma@cumulusnetworks.com>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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.
 */

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/module.h>
#include <linux/errno.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/if_link.h>
#include <linux/if_ether.h>
#include <net/netlink.h>
#include <net/rtnetlink.h>
22
#include <linux/reciprocal_div.h>
23 24
#include "bonding.h"

25 26
static const struct nla_policy bond_policy[IFLA_BOND_MAX + 1] = {
	[IFLA_BOND_MODE]		= { .type = NLA_U8 },
27
	[IFLA_BOND_ACTIVE_SLAVE]	= { .type = NLA_U32 },
28
	[IFLA_BOND_MIIMON]		= { .type = NLA_U32 },
29
	[IFLA_BOND_UPDELAY]		= { .type = NLA_U32 },
30
	[IFLA_BOND_DOWNDELAY]		= { .type = NLA_U32 },
31
	[IFLA_BOND_USE_CARRIER]		= { .type = NLA_U8 },
32
	[IFLA_BOND_ARP_INTERVAL]	= { .type = NLA_U32 },
33
	[IFLA_BOND_ARP_IP_TARGET]	= { .type = NLA_NESTED },
34
	[IFLA_BOND_ARP_VALIDATE]	= { .type = NLA_U32 },
35
	[IFLA_BOND_ARP_ALL_TARGETS]	= { .type = NLA_U32 },
36
	[IFLA_BOND_PRIMARY]		= { .type = NLA_U32 },
37
	[IFLA_BOND_PRIMARY_RESELECT]	= { .type = NLA_U8 },
38
	[IFLA_BOND_FAIL_OVER_MAC]	= { .type = NLA_U8 },
39
	[IFLA_BOND_XMIT_HASH_POLICY]	= { .type = NLA_U8 },
40
	[IFLA_BOND_RESEND_IGMP]		= { .type = NLA_U32 },
41
	[IFLA_BOND_NUM_PEER_NOTIF]	= { .type = NLA_U8 },
42
	[IFLA_BOND_ALL_SLAVES_ACTIVE]	= { .type = NLA_U8 },
43
	[IFLA_BOND_MIN_LINKS]		= { .type = NLA_U32 },
44
	[IFLA_BOND_LP_INTERVAL]		= { .type = NLA_U32 },
45
	[IFLA_BOND_PACKETS_PER_SLAVE]	= { .type = NLA_U32 },
46
	[IFLA_BOND_AD_LACP_RATE]	= { .type = NLA_U8 },
47
	[IFLA_BOND_AD_SELECT]		= { .type = NLA_U8 },
48
	[IFLA_BOND_AD_INFO]		= { .type = NLA_NESTED },
49 50
};

51 52 53 54 55 56 57 58 59 60 61
static int bond_validate(struct nlattr *tb[], struct nlattr *data[])
{
	if (tb[IFLA_ADDRESS]) {
		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
			return -EINVAL;
		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
			return -EADDRNOTAVAIL;
	}
	return 0;
}

62 63 64 65
static int bond_changelink(struct net_device *bond_dev,
			   struct nlattr *tb[], struct nlattr *data[])
{
	struct bonding *bond = netdev_priv(bond_dev);
66
	int miimon = 0;
67 68
	int err;

69 70 71 72
	if (!data)
		return 0;

	if (data[IFLA_BOND_MODE]) {
73 74 75 76 77 78
		int mode = nla_get_u8(data[IFLA_BOND_MODE]);

		err = bond_option_mode_set(bond, mode);
		if (err)
			return err;
	}
79
	if (data[IFLA_BOND_ACTIVE_SLAVE]) {
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
		int ifindex = nla_get_u32(data[IFLA_BOND_ACTIVE_SLAVE]);
		struct net_device *slave_dev;

		if (ifindex == 0) {
			slave_dev = NULL;
		} else {
			slave_dev = __dev_get_by_index(dev_net(bond_dev),
						       ifindex);
			if (!slave_dev)
				return -ENODEV;
		}
		err = bond_option_active_slave_set(bond, slave_dev);
		if (err)
			return err;
	}
95
	if (data[IFLA_BOND_MIIMON]) {
96
		miimon = nla_get_u32(data[IFLA_BOND_MIIMON]);
97 98 99 100 101

		err = bond_option_miimon_set(bond, miimon);
		if (err)
			return err;
	}
102 103 104 105 106 107 108
	if (data[IFLA_BOND_UPDELAY]) {
		int updelay = nla_get_u32(data[IFLA_BOND_UPDELAY]);

		err = bond_option_updelay_set(bond, updelay);
		if (err)
			return err;
	}
109 110 111 112 113 114 115
	if (data[IFLA_BOND_DOWNDELAY]) {
		int downdelay = nla_get_u32(data[IFLA_BOND_DOWNDELAY]);

		err = bond_option_downdelay_set(bond, downdelay);
		if (err)
			return err;
	}
116 117 118 119 120 121 122
	if (data[IFLA_BOND_USE_CARRIER]) {
		int use_carrier = nla_get_u8(data[IFLA_BOND_USE_CARRIER]);

		err = bond_option_use_carrier_set(bond, use_carrier);
		if (err)
			return err;
	}
123 124 125 126 127 128 129 130 131 132 133 134 135
	if (data[IFLA_BOND_ARP_INTERVAL]) {
		int arp_interval = nla_get_u32(data[IFLA_BOND_ARP_INTERVAL]);

		if (arp_interval && miimon) {
			pr_err("%s: ARP monitoring cannot be used with MII monitoring.\n",
			       bond->dev->name);
			return -EINVAL;
		}

		err = bond_option_arp_interval_set(bond, arp_interval);
		if (err)
			return err;
	}
136 137 138 139 140 141
	if (data[IFLA_BOND_ARP_IP_TARGET]) {
		__be32 targets[BOND_MAX_ARP_TARGETS] = { 0, };
		struct nlattr *attr;
		int i = 0, rem;

		nla_for_each_nested(attr, data[IFLA_BOND_ARP_IP_TARGET], rem) {
142
			__be32 target = nla_get_be32(attr);
143 144 145 146 147 148 149
			targets[i++] = target;
		}

		err = bond_option_arp_ip_targets_set(bond, targets, i);
		if (err)
			return err;
	}
150 151 152 153 154 155 156 157 158 159 160 161 162
	if (data[IFLA_BOND_ARP_VALIDATE]) {
		int arp_validate = nla_get_u32(data[IFLA_BOND_ARP_VALIDATE]);

		if (arp_validate && miimon) {
			pr_err("%s: ARP validating cannot be used with MII monitoring.\n",
			       bond->dev->name);
			return -EINVAL;
		}

		err = bond_option_arp_validate_set(bond, arp_validate);
		if (err)
			return err;
	}
163 164 165 166 167 168 169 170
	if (data[IFLA_BOND_ARP_ALL_TARGETS]) {
		int arp_all_targets =
			nla_get_u32(data[IFLA_BOND_ARP_ALL_TARGETS]);

		err = bond_option_arp_all_targets_set(bond, arp_all_targets);
		if (err)
			return err;
	}
171 172 173 174 175 176 177 178 179 180 181 182 183
	if (data[IFLA_BOND_PRIMARY]) {
		int ifindex = nla_get_u32(data[IFLA_BOND_PRIMARY]);
		struct net_device *dev;
		char *primary = "";

		dev = __dev_get_by_index(dev_net(bond_dev), ifindex);
		if (dev)
			primary = dev->name;

		err = bond_option_primary_set(bond, primary);
		if (err)
			return err;
	}
184 185 186 187 188 189 190 191
	if (data[IFLA_BOND_PRIMARY_RESELECT]) {
		int primary_reselect =
			nla_get_u8(data[IFLA_BOND_PRIMARY_RESELECT]);

		err = bond_option_primary_reselect_set(bond, primary_reselect);
		if (err)
			return err;
	}
192 193 194 195 196 197 198 199
	if (data[IFLA_BOND_FAIL_OVER_MAC]) {
		int fail_over_mac =
			nla_get_u8(data[IFLA_BOND_FAIL_OVER_MAC]);

		err = bond_option_fail_over_mac_set(bond, fail_over_mac);
		if (err)
			return err;
	}
200 201 202 203 204 205 206 207
	if (data[IFLA_BOND_XMIT_HASH_POLICY]) {
		int xmit_hash_policy =
			nla_get_u8(data[IFLA_BOND_XMIT_HASH_POLICY]);

		err = bond_option_xmit_hash_policy_set(bond, xmit_hash_policy);
		if (err)
			return err;
	}
208 209 210 211 212 213 214 215
	if (data[IFLA_BOND_RESEND_IGMP]) {
		int resend_igmp =
			nla_get_u32(data[IFLA_BOND_RESEND_IGMP]);

		err = bond_option_resend_igmp_set(bond, resend_igmp);
		if (err)
			return err;
	}
216 217 218 219 220 221 222 223
	if (data[IFLA_BOND_NUM_PEER_NOTIF]) {
		int num_peer_notif =
			nla_get_u8(data[IFLA_BOND_NUM_PEER_NOTIF]);

		err = bond_option_num_peer_notif_set(bond, num_peer_notif);
		if (err)
			return err;
	}
224 225 226 227 228 229 230 231 232
	if (data[IFLA_BOND_ALL_SLAVES_ACTIVE]) {
		int all_slaves_active =
			nla_get_u8(data[IFLA_BOND_ALL_SLAVES_ACTIVE]);

		err = bond_option_all_slaves_active_set(bond,
							all_slaves_active);
		if (err)
			return err;
	}
233 234 235 236 237 238 239 240
	if (data[IFLA_BOND_MIN_LINKS]) {
		int min_links =
			nla_get_u32(data[IFLA_BOND_MIN_LINKS]);

		err = bond_option_min_links_set(bond, min_links);
		if (err)
			return err;
	}
241 242 243 244 245 246 247 248
	if (data[IFLA_BOND_LP_INTERVAL]) {
		int lp_interval =
			nla_get_u32(data[IFLA_BOND_LP_INTERVAL]);

		err = bond_option_lp_interval_set(bond, lp_interval);
		if (err)
			return err;
	}
249 250 251 252 253 254 255 256 257
	if (data[IFLA_BOND_PACKETS_PER_SLAVE]) {
		int packets_per_slave =
			nla_get_u32(data[IFLA_BOND_PACKETS_PER_SLAVE]);

		err = bond_option_packets_per_slave_set(bond,
							packets_per_slave);
		if (err)
			return err;
	}
258 259 260 261 262 263 264 265
	if (data[IFLA_BOND_AD_LACP_RATE]) {
		int lacp_rate =
			nla_get_u8(data[IFLA_BOND_AD_LACP_RATE]);

		err = bond_option_lacp_rate_set(bond, lacp_rate);
		if (err)
			return err;
	}
266 267 268 269 270 271 272 273
	if (data[IFLA_BOND_AD_SELECT]) {
		int ad_select =
			nla_get_u8(data[IFLA_BOND_AD_SELECT]);

		err = bond_option_ad_select_set(bond, ad_select);
		if (err)
			return err;
	}
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
	return 0;
}

static int bond_newlink(struct net *src_net, struct net_device *bond_dev,
			struct nlattr *tb[], struct nlattr *data[])
{
	int err;

	err = bond_changelink(bond_dev, tb, data);
	if (err < 0)
		return err;

	return register_netdevice(bond_dev);
}

static size_t bond_get_size(const struct net_device *bond_dev)
{
291
	return nla_total_size(sizeof(u8)) +	/* IFLA_BOND_MODE */
292 293
		nla_total_size(sizeof(u32)) +	/* IFLA_BOND_ACTIVE_SLAVE */
		nla_total_size(sizeof(u32)) +	/* IFLA_BOND_MIIMON */
294
		nla_total_size(sizeof(u32)) +	/* IFLA_BOND_UPDELAY */
295
		nla_total_size(sizeof(u32)) +	/* IFLA_BOND_DOWNDELAY */
296
		nla_total_size(sizeof(u8)) +	/* IFLA_BOND_USE_CARRIER */
297
		nla_total_size(sizeof(u32)) +	/* IFLA_BOND_ARP_INTERVAL */
298 299
						/* IFLA_BOND_ARP_IP_TARGET */
		nla_total_size(sizeof(u32)) * BOND_MAX_ARP_TARGETS +
300
		nla_total_size(sizeof(u32)) +	/* IFLA_BOND_ARP_VALIDATE */
301
		nla_total_size(sizeof(u32)) +	/* IFLA_BOND_ARP_ALL_TARGETS */
302
		nla_total_size(sizeof(u32)) +	/* IFLA_BOND_PRIMARY */
303
		nla_total_size(sizeof(u8)) +	/* IFLA_BOND_PRIMARY_RESELECT */
304
		nla_total_size(sizeof(u8)) +	/* IFLA_BOND_FAIL_OVER_MAC */
305
		nla_total_size(sizeof(u8)) +	/* IFLA_BOND_XMIT_HASH_POLICY */
306
		nla_total_size(sizeof(u32)) +	/* IFLA_BOND_RESEND_IGMP */
307
		nla_total_size(sizeof(u8)) +	/* IFLA_BOND_NUM_PEER_NOTIF */
308
		nla_total_size(sizeof(u8)) +   /* IFLA_BOND_ALL_SLAVES_ACTIVE */
309
		nla_total_size(sizeof(u32)) +	/* IFLA_BOND_MIN_LINKS */
310
		nla_total_size(sizeof(u32)) +	/* IFLA_BOND_LP_INTERVAL */
311
		nla_total_size(sizeof(u32)) +  /* IFLA_BOND_PACKETS_PER_SLAVE */
312
		nla_total_size(sizeof(u8)) +	/* IFLA_BOND_AD_LACP_RATE */
313
		nla_total_size(sizeof(u8)) +	/* IFLA_BOND_AD_SELECT */
314 315 316 317 318 319
		nla_total_size(sizeof(struct nlattr)) + /* IFLA_BOND_AD_INFO */
		nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_AGGREGATOR */
		nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_NUM_PORTS */
		nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_ACTOR_KEY */
		nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_PARTNER_KEY*/
		nla_total_size(ETH_ALEN) +    /* IFLA_BOND_AD_INFO_PARTNER_MAC*/
320
		0;
321 322 323 324 325 326
}

static int bond_fill_info(struct sk_buff *skb,
			  const struct net_device *bond_dev)
{
	struct bonding *bond = netdev_priv(bond_dev);
327
	struct net_device *slave_dev = bond_option_active_slave_get(bond);
328
	struct nlattr *targets;
329
	unsigned int packets_per_slave;
330
	int i, targets_added;
331

332
	if (nla_put_u8(skb, IFLA_BOND_MODE, bond->params.mode))
333
		goto nla_put_failure;
334 335 336 337 338 339 340 341

	if (slave_dev &&
	    nla_put_u32(skb, IFLA_BOND_ACTIVE_SLAVE, slave_dev->ifindex))
		goto nla_put_failure;

	if (nla_put_u32(skb, IFLA_BOND_MIIMON, bond->params.miimon))
		goto nla_put_failure;

342 343 344 345
	if (nla_put_u32(skb, IFLA_BOND_UPDELAY,
			bond->params.updelay * bond->params.miimon))
		goto nla_put_failure;

346 347 348 349
	if (nla_put_u32(skb, IFLA_BOND_DOWNDELAY,
			bond->params.downdelay * bond->params.miimon))
		goto nla_put_failure;

350 351 352
	if (nla_put_u8(skb, IFLA_BOND_USE_CARRIER, bond->params.use_carrier))
		goto nla_put_failure;

353 354 355
	if (nla_put_u32(skb, IFLA_BOND_ARP_INTERVAL, bond->params.arp_interval))
		goto nla_put_failure;

356 357 358 359 360 361 362
	targets = nla_nest_start(skb, IFLA_BOND_ARP_IP_TARGET);
	if (!targets)
		goto nla_put_failure;

	targets_added = 0;
	for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
		if (bond->params.arp_targets[i]) {
363
			nla_put_be32(skb, i, bond->params.arp_targets[i]);
364 365 366 367 368 369 370 371 372
			targets_added = 1;
		}
	}

	if (targets_added)
		nla_nest_end(skb, targets);
	else
		nla_nest_cancel(skb, targets);

373 374 375
	if (nla_put_u32(skb, IFLA_BOND_ARP_VALIDATE, bond->params.arp_validate))
		goto nla_put_failure;

376 377 378 379
	if (nla_put_u32(skb, IFLA_BOND_ARP_ALL_TARGETS,
			bond->params.arp_all_targets))
		goto nla_put_failure;

380 381 382 383 384
	if (bond->primary_slave &&
	    nla_put_u32(skb, IFLA_BOND_PRIMARY,
			bond->primary_slave->dev->ifindex))
		goto nla_put_failure;

385 386 387 388
	if (nla_put_u8(skb, IFLA_BOND_PRIMARY_RESELECT,
		       bond->params.primary_reselect))
		goto nla_put_failure;

389 390 391 392
	if (nla_put_u8(skb, IFLA_BOND_FAIL_OVER_MAC,
		       bond->params.fail_over_mac))
		goto nla_put_failure;

393 394 395 396
	if (nla_put_u8(skb, IFLA_BOND_XMIT_HASH_POLICY,
		       bond->params.xmit_policy))
		goto nla_put_failure;

397 398 399 400
	if (nla_put_u32(skb, IFLA_BOND_RESEND_IGMP,
		        bond->params.resend_igmp))
		goto nla_put_failure;

401 402 403 404
	if (nla_put_u8(skb, IFLA_BOND_NUM_PEER_NOTIF,
		       bond->params.num_peer_notif))
		goto nla_put_failure;

405 406 407 408
	if (nla_put_u8(skb, IFLA_BOND_ALL_SLAVES_ACTIVE,
		       bond->params.all_slaves_active))
		goto nla_put_failure;

409 410 411 412
	if (nla_put_u32(skb, IFLA_BOND_MIN_LINKS,
			bond->params.min_links))
		goto nla_put_failure;

413 414 415 416
	if (nla_put_u32(skb, IFLA_BOND_LP_INTERVAL,
			bond->params.lp_interval))
		goto nla_put_failure;

417 418 419 420 421 422 423 424
	packets_per_slave = bond->params.packets_per_slave;
	if (packets_per_slave > 1)
		packets_per_slave = reciprocal_value(packets_per_slave);

	if (nla_put_u32(skb, IFLA_BOND_PACKETS_PER_SLAVE,
			packets_per_slave))
		goto nla_put_failure;

425 426 427 428
	if (nla_put_u8(skb, IFLA_BOND_AD_LACP_RATE,
		       bond->params.lacp_fast))
		goto nla_put_failure;

429 430 431 432
	if (nla_put_u8(skb, IFLA_BOND_AD_SELECT,
		       bond->params.ad_select))
		goto nla_put_failure;

433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
	if (bond->params.mode == BOND_MODE_8023AD) {
		struct ad_info info;

		if (!bond_3ad_get_active_agg_info(bond, &info)) {
			struct nlattr *nest;

			nest = nla_nest_start(skb, IFLA_BOND_AD_INFO);
			if (!nest)
				goto nla_put_failure;

			if (nla_put_u16(skb, IFLA_BOND_AD_INFO_AGGREGATOR,
					info.aggregator_id))
				goto nla_put_failure;
			if (nla_put_u16(skb, IFLA_BOND_AD_INFO_NUM_PORTS,
					info.ports))
				goto nla_put_failure;
			if (nla_put_u16(skb, IFLA_BOND_AD_INFO_ACTOR_KEY,
					info.actor_key))
				goto nla_put_failure;
			if (nla_put_u16(skb, IFLA_BOND_AD_INFO_PARTNER_KEY,
					info.partner_key))
				goto nla_put_failure;
			if (nla_put(skb, IFLA_BOND_AD_INFO_PARTNER_MAC,
				    sizeof(info.partner_system),
				    &info.partner_system))
				goto nla_put_failure;

			nla_nest_end(skb, nest);
		}
	}

464 465 466 467 468 469
	return 0;

nla_put_failure:
	return -EMSGSIZE;
}

470 471 472 473
struct rtnl_link_ops bond_link_ops __read_mostly = {
	.kind			= "bond",
	.priv_size		= sizeof(struct bonding),
	.setup			= bond_setup,
474 475
	.maxtype		= IFLA_BOND_MAX,
	.policy			= bond_policy,
476
	.validate		= bond_validate,
477 478 479 480
	.newlink		= bond_newlink,
	.changelink		= bond_changelink,
	.get_size		= bond_get_size,
	.fill_info		= bond_fill_info,
481 482 483 484 485 486 487 488 489 490
	.get_num_tx_queues	= bond_get_num_tx_queues,
	.get_num_rx_queues	= bond_get_num_tx_queues, /* Use the same number
							     as for TX queues */
};

int __init bond_netlink_init(void)
{
	return rtnl_link_register(&bond_link_ops);
}

491
void bond_netlink_fini(void)
492 493 494 495 496
{
	rtnl_link_unregister(&bond_link_ops);
}

MODULE_ALIAS_RTNL_LINK("bond");