bond_netlink.c 6.8 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 22 23
 *
 * 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>
#include "bonding.h"

24 25
static const struct nla_policy bond_policy[IFLA_BOND_MAX + 1] = {
	[IFLA_BOND_MODE]		= { .type = NLA_U8 },
26
	[IFLA_BOND_ACTIVE_SLAVE]	= { .type = NLA_U32 },
27
	[IFLA_BOND_MIIMON]		= { .type = NLA_U32 },
28
	[IFLA_BOND_UPDELAY]		= { .type = NLA_U32 },
29
	[IFLA_BOND_DOWNDELAY]		= { .type = NLA_U32 },
30
	[IFLA_BOND_USE_CARRIER]		= { .type = NLA_U8 },
31
	[IFLA_BOND_ARP_INTERVAL]	= { .type = NLA_U32 },
32
	[IFLA_BOND_ARP_IP_TARGET]	= { .type = NLA_NESTED },
33
	[IFLA_BOND_ARP_VALIDATE]	= { .type = NLA_U32 },
34 35
};

36 37 38 39 40 41 42 43 44 45 46
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;
}

47 48 49 50
static int bond_changelink(struct net_device *bond_dev,
			   struct nlattr *tb[], struct nlattr *data[])
{
	struct bonding *bond = netdev_priv(bond_dev);
51
	int miimon = 0;
52 53
	int err;

54 55 56 57
	if (!data)
		return 0;

	if (data[IFLA_BOND_MODE]) {
58 59 60 61 62 63
		int mode = nla_get_u8(data[IFLA_BOND_MODE]);

		err = bond_option_mode_set(bond, mode);
		if (err)
			return err;
	}
64
	if (data[IFLA_BOND_ACTIVE_SLAVE]) {
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
		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;
	}
80
	if (data[IFLA_BOND_MIIMON]) {
81
		miimon = nla_get_u32(data[IFLA_BOND_MIIMON]);
82 83 84 85 86

		err = bond_option_miimon_set(bond, miimon);
		if (err)
			return err;
	}
87 88 89 90 91 92 93
	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;
	}
94 95 96 97 98 99 100
	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;
	}
101 102 103 104 105 106 107
	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;
	}
108 109 110 111 112 113 114 115 116 117 118 119 120
	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;
	}
121 122 123 124 125 126 127 128 129 130 131 132 133 134
	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) {
			__be32 target = nla_get_u32(attr);
			targets[i++] = target;
		}

		err = bond_option_arp_ip_targets_set(bond, targets, i);
		if (err)
			return err;
	}
135 136 137 138 139 140 141 142 143 144 145 146 147
	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;
	}
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
	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)
{
165
	return nla_total_size(sizeof(u8)) +	/* IFLA_BOND_MODE */
166 167
		nla_total_size(sizeof(u32)) +	/* IFLA_BOND_ACTIVE_SLAVE */
		nla_total_size(sizeof(u32)) +	/* IFLA_BOND_MIIMON */
168
		nla_total_size(sizeof(u32)) +	/* IFLA_BOND_UPDELAY */
169
		nla_total_size(sizeof(u32)) +	/* IFLA_BOND_DOWNDELAY */
170
		nla_total_size(sizeof(u8)) +	/* IFLA_BOND_USE_CARRIER */
171
		nla_total_size(sizeof(u32)) +	/* IFLA_BOND_ARP_INTERVAL */
172 173
						/* IFLA_BOND_ARP_IP_TARGET */
		nla_total_size(sizeof(u32)) * BOND_MAX_ARP_TARGETS +
174
		nla_total_size(sizeof(u32)) +	/* IFLA_BOND_ARP_VALIDATE */
175
		0;
176 177 178 179 180 181
}

static int bond_fill_info(struct sk_buff *skb,
			  const struct net_device *bond_dev)
{
	struct bonding *bond = netdev_priv(bond_dev);
182
	struct net_device *slave_dev = bond_option_active_slave_get(bond);
183 184
	struct nlattr *targets;
	int i, targets_added;
185

186
	if (nla_put_u8(skb, IFLA_BOND_MODE, bond->params.mode))
187
		goto nla_put_failure;
188 189 190 191 192 193 194 195

	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;

196 197 198 199
	if (nla_put_u32(skb, IFLA_BOND_UPDELAY,
			bond->params.updelay * bond->params.miimon))
		goto nla_put_failure;

200 201 202 203
	if (nla_put_u32(skb, IFLA_BOND_DOWNDELAY,
			bond->params.downdelay * bond->params.miimon))
		goto nla_put_failure;

204 205 206
	if (nla_put_u8(skb, IFLA_BOND_USE_CARRIER, bond->params.use_carrier))
		goto nla_put_failure;

207 208 209
	if (nla_put_u32(skb, IFLA_BOND_ARP_INTERVAL, bond->params.arp_interval))
		goto nla_put_failure;

210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
	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]) {
			nla_put_u32(skb, i, bond->params.arp_targets[i]);
			targets_added = 1;
		}
	}

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

227 228 229
	if (nla_put_u32(skb, IFLA_BOND_ARP_VALIDATE, bond->params.arp_validate))
		goto nla_put_failure;

230 231 232 233 234 235
	return 0;

nla_put_failure:
	return -EMSGSIZE;
}

236 237 238 239
struct rtnl_link_ops bond_link_ops __read_mostly = {
	.kind			= "bond",
	.priv_size		= sizeof(struct bonding),
	.setup			= bond_setup,
240 241
	.maxtype		= IFLA_BOND_MAX,
	.policy			= bond_policy,
242
	.validate		= bond_validate,
243 244 245 246
	.newlink		= bond_newlink,
	.changelink		= bond_changelink,
	.get_size		= bond_get_size,
	.fill_info		= bond_fill_info,
247 248 249 250 251 252 253 254 255 256
	.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);
}

257
void bond_netlink_fini(void)
258 259 260 261 262
{
	rtnl_link_unregister(&bond_link_ops);
}

MODULE_ALIAS_RTNL_LINK("bond");