blob: 58e71235b75f4b8ce26992e16bd1af864edfcca0 [file] [log] [blame]
Jiri Pirko0a2a78c2013-10-18 17:43:33 +02001/*
2 * drivers/net/bond/bond_netlink.c - Netlink interface for bonding
3 * Copyright (c) 2013 Jiri Pirko <jiri@resnulli.us>
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -08004 * Copyright (c) 2013 Scott Feldman <sfeldma@cumulusnetworks.com>
Jiri Pirko0a2a78c2013-10-18 17:43:33 +02005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14#include <linux/module.h>
15#include <linux/errno.h>
16#include <linux/netdevice.h>
17#include <linux/etherdevice.h>
18#include <linux/if_link.h>
19#include <linux/if_ether.h>
20#include <net/netlink.h>
21#include <net/rtnetlink.h>
22#include "bonding.h"
23
Jiri Pirko90af2312013-10-18 17:43:38 +020024static const struct nla_policy bond_policy[IFLA_BOND_MAX + 1] = {
25 [IFLA_BOND_MODE] = { .type = NLA_U8 },
Jiri Pirkoec76aa42013-10-18 17:43:39 +020026 [IFLA_BOND_ACTIVE_SLAVE] = { .type = NLA_U32 },
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -080027 [IFLA_BOND_MIIMON] = { .type = NLA_U32 },
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -080028 [IFLA_BOND_UPDELAY] = { .type = NLA_U32 },
sfeldma@cumulusnetworks.comc7461f92013-12-12 14:10:09 -080029 [IFLA_BOND_DOWNDELAY] = { .type = NLA_U32 },
sfeldma@cumulusnetworks.com9f53e142013-12-12 14:10:16 -080030 [IFLA_BOND_USE_CARRIER] = { .type = NLA_U8 },
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -080031 [IFLA_BOND_ARP_INTERVAL] = { .type = NLA_U32 },
Jiri Pirko90af2312013-10-18 17:43:38 +020032};
33
Jiri Pirko0a2a78c2013-10-18 17:43:33 +020034static int bond_validate(struct nlattr *tb[], struct nlattr *data[])
35{
36 if (tb[IFLA_ADDRESS]) {
37 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
38 return -EINVAL;
39 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
40 return -EADDRNOTAVAIL;
41 }
42 return 0;
43}
44
Jiri Pirko90af2312013-10-18 17:43:38 +020045static int bond_changelink(struct net_device *bond_dev,
46 struct nlattr *tb[], struct nlattr *data[])
47{
48 struct bonding *bond = netdev_priv(bond_dev);
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -080049 int miimon = 0;
Jiri Pirko90af2312013-10-18 17:43:38 +020050 int err;
51
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -080052 if (!data)
53 return 0;
54
55 if (data[IFLA_BOND_MODE]) {
Jiri Pirko90af2312013-10-18 17:43:38 +020056 int mode = nla_get_u8(data[IFLA_BOND_MODE]);
57
58 err = bond_option_mode_set(bond, mode);
59 if (err)
60 return err;
61 }
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -080062 if (data[IFLA_BOND_ACTIVE_SLAVE]) {
Jiri Pirkoec76aa42013-10-18 17:43:39 +020063 int ifindex = nla_get_u32(data[IFLA_BOND_ACTIVE_SLAVE]);
64 struct net_device *slave_dev;
65
66 if (ifindex == 0) {
67 slave_dev = NULL;
68 } else {
69 slave_dev = __dev_get_by_index(dev_net(bond_dev),
70 ifindex);
71 if (!slave_dev)
72 return -ENODEV;
73 }
74 err = bond_option_active_slave_set(bond, slave_dev);
75 if (err)
76 return err;
77 }
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -080078 if (data[IFLA_BOND_MIIMON]) {
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -080079 miimon = nla_get_u32(data[IFLA_BOND_MIIMON]);
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -080080
81 err = bond_option_miimon_set(bond, miimon);
82 if (err)
83 return err;
84 }
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -080085 if (data[IFLA_BOND_UPDELAY]) {
86 int updelay = nla_get_u32(data[IFLA_BOND_UPDELAY]);
87
88 err = bond_option_updelay_set(bond, updelay);
89 if (err)
90 return err;
91 }
sfeldma@cumulusnetworks.comc7461f92013-12-12 14:10:09 -080092 if (data[IFLA_BOND_DOWNDELAY]) {
93 int downdelay = nla_get_u32(data[IFLA_BOND_DOWNDELAY]);
94
95 err = bond_option_downdelay_set(bond, downdelay);
96 if (err)
97 return err;
98 }
sfeldma@cumulusnetworks.com9f53e142013-12-12 14:10:16 -080099 if (data[IFLA_BOND_USE_CARRIER]) {
100 int use_carrier = nla_get_u8(data[IFLA_BOND_USE_CARRIER]);
101
102 err = bond_option_use_carrier_set(bond, use_carrier);
103 if (err)
104 return err;
105 }
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800106 if (data[IFLA_BOND_ARP_INTERVAL]) {
107 int arp_interval = nla_get_u32(data[IFLA_BOND_ARP_INTERVAL]);
108
109 if (arp_interval && miimon) {
110 pr_err("%s: ARP monitoring cannot be used with MII monitoring.\n",
111 bond->dev->name);
112 return -EINVAL;
113 }
114
115 err = bond_option_arp_interval_set(bond, arp_interval);
116 if (err)
117 return err;
118 }
Jiri Pirko90af2312013-10-18 17:43:38 +0200119 return 0;
120}
121
122static int bond_newlink(struct net *src_net, struct net_device *bond_dev,
123 struct nlattr *tb[], struct nlattr *data[])
124{
125 int err;
126
127 err = bond_changelink(bond_dev, tb, data);
128 if (err < 0)
129 return err;
130
131 return register_netdevice(bond_dev);
132}
133
134static size_t bond_get_size(const struct net_device *bond_dev)
135{
Dan Carpentere1398622013-11-01 13:18:44 +0300136 return nla_total_size(sizeof(u8)) + /* IFLA_BOND_MODE */
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800137 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ACTIVE_SLAVE */
138 nla_total_size(sizeof(u32)) + /* IFLA_BOND_MIIMON */
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -0800139 nla_total_size(sizeof(u32)) + /* IFLA_BOND_UPDELAY */
sfeldma@cumulusnetworks.comc7461f92013-12-12 14:10:09 -0800140 nla_total_size(sizeof(u32)) + /* IFLA_BOND_DOWNDELAY */
sfeldma@cumulusnetworks.com9f53e142013-12-12 14:10:16 -0800141 nla_total_size(sizeof(u8)) + /* IFLA_BOND_USE_CARRIER */
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800142 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_INTERVAL */
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800143 0;
Jiri Pirko90af2312013-10-18 17:43:38 +0200144}
145
146static int bond_fill_info(struct sk_buff *skb,
147 const struct net_device *bond_dev)
148{
149 struct bonding *bond = netdev_priv(bond_dev);
Jiri Pirkoec76aa42013-10-18 17:43:39 +0200150 struct net_device *slave_dev = bond_option_active_slave_get(bond);
Jiri Pirko90af2312013-10-18 17:43:38 +0200151
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800152 if (nla_put_u8(skb, IFLA_BOND_MODE, bond->params.mode))
Jiri Pirko90af2312013-10-18 17:43:38 +0200153 goto nla_put_failure;
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800154
155 if (slave_dev &&
156 nla_put_u32(skb, IFLA_BOND_ACTIVE_SLAVE, slave_dev->ifindex))
157 goto nla_put_failure;
158
159 if (nla_put_u32(skb, IFLA_BOND_MIIMON, bond->params.miimon))
160 goto nla_put_failure;
161
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -0800162 if (nla_put_u32(skb, IFLA_BOND_UPDELAY,
163 bond->params.updelay * bond->params.miimon))
164 goto nla_put_failure;
165
sfeldma@cumulusnetworks.comc7461f92013-12-12 14:10:09 -0800166 if (nla_put_u32(skb, IFLA_BOND_DOWNDELAY,
167 bond->params.downdelay * bond->params.miimon))
168 goto nla_put_failure;
169
sfeldma@cumulusnetworks.com9f53e142013-12-12 14:10:16 -0800170 if (nla_put_u8(skb, IFLA_BOND_USE_CARRIER, bond->params.use_carrier))
171 goto nla_put_failure;
172
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800173 if (nla_put_u32(skb, IFLA_BOND_ARP_INTERVAL, bond->params.arp_interval))
174 goto nla_put_failure;
175
Jiri Pirko90af2312013-10-18 17:43:38 +0200176 return 0;
177
178nla_put_failure:
179 return -EMSGSIZE;
180}
181
Jiri Pirko0a2a78c2013-10-18 17:43:33 +0200182struct rtnl_link_ops bond_link_ops __read_mostly = {
183 .kind = "bond",
184 .priv_size = sizeof(struct bonding),
185 .setup = bond_setup,
Jiri Pirko90af2312013-10-18 17:43:38 +0200186 .maxtype = IFLA_BOND_MAX,
187 .policy = bond_policy,
Jiri Pirko0a2a78c2013-10-18 17:43:33 +0200188 .validate = bond_validate,
Jiri Pirko90af2312013-10-18 17:43:38 +0200189 .newlink = bond_newlink,
190 .changelink = bond_changelink,
191 .get_size = bond_get_size,
192 .fill_info = bond_fill_info,
Jiri Pirko0a2a78c2013-10-18 17:43:33 +0200193 .get_num_tx_queues = bond_get_num_tx_queues,
194 .get_num_rx_queues = bond_get_num_tx_queues, /* Use the same number
195 as for TX queues */
196};
197
198int __init bond_netlink_init(void)
199{
200 return rtnl_link_register(&bond_link_ops);
201}
202
David S. Millera729e832013-10-19 19:09:18 -0400203void bond_netlink_fini(void)
Jiri Pirko0a2a78c2013-10-18 17:43:33 +0200204{
205 rtnl_link_unregister(&bond_link_ops);
206}
207
208MODULE_ALIAS_RTNL_LINK("bond");