Jiri Pirko | 0a2a78c | 2013-10-18 17:43:33 +0200 | [diff] [blame] | 1 | /* |
| 2 | * drivers/net/bond/bond_netlink.c - Netlink interface for bonding |
| 3 | * Copyright (c) 2013 Jiri Pirko <jiri@resnulli.us> |
sfeldma@cumulusnetworks.com | eecdaa6 | 2013-12-12 14:09:55 -0800 | [diff] [blame] | 4 | * Copyright (c) 2013 Scott Feldman <sfeldma@cumulusnetworks.com> |
Jiri Pirko | 0a2a78c | 2013-10-18 17:43:33 +0200 | [diff] [blame] | 5 | * |
| 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 Pirko | 90af231 | 2013-10-18 17:43:38 +0200 | [diff] [blame] | 24 | static const struct nla_policy bond_policy[IFLA_BOND_MAX + 1] = { |
| 25 | [IFLA_BOND_MODE] = { .type = NLA_U8 }, |
Jiri Pirko | ec76aa4 | 2013-10-18 17:43:39 +0200 | [diff] [blame] | 26 | [IFLA_BOND_ACTIVE_SLAVE] = { .type = NLA_U32 }, |
sfeldma@cumulusnetworks.com | eecdaa6 | 2013-12-12 14:09:55 -0800 | [diff] [blame] | 27 | [IFLA_BOND_MIIMON] = { .type = NLA_U32 }, |
sfeldma@cumulusnetworks.com | 25852e2 | 2013-12-12 14:10:02 -0800 | [diff] [blame] | 28 | [IFLA_BOND_UPDELAY] = { .type = NLA_U32 }, |
sfeldma@cumulusnetworks.com | c7461f9 | 2013-12-12 14:10:09 -0800 | [diff] [blame] | 29 | [IFLA_BOND_DOWNDELAY] = { .type = NLA_U32 }, |
sfeldma@cumulusnetworks.com | 9f53e14 | 2013-12-12 14:10:16 -0800 | [diff] [blame] | 30 | [IFLA_BOND_USE_CARRIER] = { .type = NLA_U8 }, |
sfeldma@cumulusnetworks.com | 06151db | 2013-12-12 14:10:24 -0800 | [diff] [blame] | 31 | [IFLA_BOND_ARP_INTERVAL] = { .type = NLA_U32 }, |
sfeldma@cumulusnetworks.com | 7f28fa1 | 2013-12-12 14:10:31 -0800 | [diff] [blame] | 32 | [IFLA_BOND_ARP_IP_TARGET] = { .type = NLA_NESTED }, |
sfeldma@cumulusnetworks.com | 29c4948 | 2013-12-12 14:10:38 -0800 | [diff] [blame] | 33 | [IFLA_BOND_ARP_VALIDATE] = { .type = NLA_U32 }, |
sfeldma@cumulusnetworks.com | d5c8425 | 2013-12-12 14:10:45 -0800 | [diff] [blame] | 34 | [IFLA_BOND_ARP_ALL_TARGETS] = { .type = NLA_U32 }, |
sfeldma@cumulusnetworks.com | 0a98a0d | 2013-12-15 16:41:51 -0800 | [diff] [blame^] | 35 | [IFLA_BOND_PRIMARY] = { .type = NLA_U32 }, |
Jiri Pirko | 90af231 | 2013-10-18 17:43:38 +0200 | [diff] [blame] | 36 | }; |
| 37 | |
Jiri Pirko | 0a2a78c | 2013-10-18 17:43:33 +0200 | [diff] [blame] | 38 | static int bond_validate(struct nlattr *tb[], struct nlattr *data[]) |
| 39 | { |
| 40 | if (tb[IFLA_ADDRESS]) { |
| 41 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) |
| 42 | return -EINVAL; |
| 43 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) |
| 44 | return -EADDRNOTAVAIL; |
| 45 | } |
| 46 | return 0; |
| 47 | } |
| 48 | |
Jiri Pirko | 90af231 | 2013-10-18 17:43:38 +0200 | [diff] [blame] | 49 | static int bond_changelink(struct net_device *bond_dev, |
| 50 | struct nlattr *tb[], struct nlattr *data[]) |
| 51 | { |
| 52 | struct bonding *bond = netdev_priv(bond_dev); |
sfeldma@cumulusnetworks.com | 06151db | 2013-12-12 14:10:24 -0800 | [diff] [blame] | 53 | int miimon = 0; |
Jiri Pirko | 90af231 | 2013-10-18 17:43:38 +0200 | [diff] [blame] | 54 | int err; |
| 55 | |
sfeldma@cumulusnetworks.com | eecdaa6 | 2013-12-12 14:09:55 -0800 | [diff] [blame] | 56 | if (!data) |
| 57 | return 0; |
| 58 | |
| 59 | if (data[IFLA_BOND_MODE]) { |
Jiri Pirko | 90af231 | 2013-10-18 17:43:38 +0200 | [diff] [blame] | 60 | int mode = nla_get_u8(data[IFLA_BOND_MODE]); |
| 61 | |
| 62 | err = bond_option_mode_set(bond, mode); |
| 63 | if (err) |
| 64 | return err; |
| 65 | } |
sfeldma@cumulusnetworks.com | eecdaa6 | 2013-12-12 14:09:55 -0800 | [diff] [blame] | 66 | if (data[IFLA_BOND_ACTIVE_SLAVE]) { |
Jiri Pirko | ec76aa4 | 2013-10-18 17:43:39 +0200 | [diff] [blame] | 67 | int ifindex = nla_get_u32(data[IFLA_BOND_ACTIVE_SLAVE]); |
| 68 | struct net_device *slave_dev; |
| 69 | |
| 70 | if (ifindex == 0) { |
| 71 | slave_dev = NULL; |
| 72 | } else { |
| 73 | slave_dev = __dev_get_by_index(dev_net(bond_dev), |
| 74 | ifindex); |
| 75 | if (!slave_dev) |
| 76 | return -ENODEV; |
| 77 | } |
| 78 | err = bond_option_active_slave_set(bond, slave_dev); |
| 79 | if (err) |
| 80 | return err; |
| 81 | } |
sfeldma@cumulusnetworks.com | eecdaa6 | 2013-12-12 14:09:55 -0800 | [diff] [blame] | 82 | if (data[IFLA_BOND_MIIMON]) { |
sfeldma@cumulusnetworks.com | 06151db | 2013-12-12 14:10:24 -0800 | [diff] [blame] | 83 | miimon = nla_get_u32(data[IFLA_BOND_MIIMON]); |
sfeldma@cumulusnetworks.com | eecdaa6 | 2013-12-12 14:09:55 -0800 | [diff] [blame] | 84 | |
| 85 | err = bond_option_miimon_set(bond, miimon); |
| 86 | if (err) |
| 87 | return err; |
| 88 | } |
sfeldma@cumulusnetworks.com | 25852e2 | 2013-12-12 14:10:02 -0800 | [diff] [blame] | 89 | if (data[IFLA_BOND_UPDELAY]) { |
| 90 | int updelay = nla_get_u32(data[IFLA_BOND_UPDELAY]); |
| 91 | |
| 92 | err = bond_option_updelay_set(bond, updelay); |
| 93 | if (err) |
| 94 | return err; |
| 95 | } |
sfeldma@cumulusnetworks.com | c7461f9 | 2013-12-12 14:10:09 -0800 | [diff] [blame] | 96 | if (data[IFLA_BOND_DOWNDELAY]) { |
| 97 | int downdelay = nla_get_u32(data[IFLA_BOND_DOWNDELAY]); |
| 98 | |
| 99 | err = bond_option_downdelay_set(bond, downdelay); |
| 100 | if (err) |
| 101 | return err; |
| 102 | } |
sfeldma@cumulusnetworks.com | 9f53e14 | 2013-12-12 14:10:16 -0800 | [diff] [blame] | 103 | if (data[IFLA_BOND_USE_CARRIER]) { |
| 104 | int use_carrier = nla_get_u8(data[IFLA_BOND_USE_CARRIER]); |
| 105 | |
| 106 | err = bond_option_use_carrier_set(bond, use_carrier); |
| 107 | if (err) |
| 108 | return err; |
| 109 | } |
sfeldma@cumulusnetworks.com | 06151db | 2013-12-12 14:10:24 -0800 | [diff] [blame] | 110 | if (data[IFLA_BOND_ARP_INTERVAL]) { |
| 111 | int arp_interval = nla_get_u32(data[IFLA_BOND_ARP_INTERVAL]); |
| 112 | |
| 113 | if (arp_interval && miimon) { |
| 114 | pr_err("%s: ARP monitoring cannot be used with MII monitoring.\n", |
| 115 | bond->dev->name); |
| 116 | return -EINVAL; |
| 117 | } |
| 118 | |
| 119 | err = bond_option_arp_interval_set(bond, arp_interval); |
| 120 | if (err) |
| 121 | return err; |
| 122 | } |
sfeldma@cumulusnetworks.com | 7f28fa1 | 2013-12-12 14:10:31 -0800 | [diff] [blame] | 123 | if (data[IFLA_BOND_ARP_IP_TARGET]) { |
| 124 | __be32 targets[BOND_MAX_ARP_TARGETS] = { 0, }; |
| 125 | struct nlattr *attr; |
| 126 | int i = 0, rem; |
| 127 | |
| 128 | nla_for_each_nested(attr, data[IFLA_BOND_ARP_IP_TARGET], rem) { |
Jiri Pirko | e7ef941 | 2013-12-14 12:32:10 +0100 | [diff] [blame] | 129 | __be32 target = nla_get_be32(attr); |
sfeldma@cumulusnetworks.com | 7f28fa1 | 2013-12-12 14:10:31 -0800 | [diff] [blame] | 130 | targets[i++] = target; |
| 131 | } |
| 132 | |
| 133 | err = bond_option_arp_ip_targets_set(bond, targets, i); |
| 134 | if (err) |
| 135 | return err; |
| 136 | } |
sfeldma@cumulusnetworks.com | 29c4948 | 2013-12-12 14:10:38 -0800 | [diff] [blame] | 137 | if (data[IFLA_BOND_ARP_VALIDATE]) { |
| 138 | int arp_validate = nla_get_u32(data[IFLA_BOND_ARP_VALIDATE]); |
| 139 | |
| 140 | if (arp_validate && miimon) { |
| 141 | pr_err("%s: ARP validating cannot be used with MII monitoring.\n", |
| 142 | bond->dev->name); |
| 143 | return -EINVAL; |
| 144 | } |
| 145 | |
| 146 | err = bond_option_arp_validate_set(bond, arp_validate); |
| 147 | if (err) |
| 148 | return err; |
| 149 | } |
sfeldma@cumulusnetworks.com | d5c8425 | 2013-12-12 14:10:45 -0800 | [diff] [blame] | 150 | if (data[IFLA_BOND_ARP_ALL_TARGETS]) { |
| 151 | int arp_all_targets = |
| 152 | nla_get_u32(data[IFLA_BOND_ARP_ALL_TARGETS]); |
| 153 | |
| 154 | err = bond_option_arp_all_targets_set(bond, arp_all_targets); |
| 155 | if (err) |
| 156 | return err; |
| 157 | } |
sfeldma@cumulusnetworks.com | 0a98a0d | 2013-12-15 16:41:51 -0800 | [diff] [blame^] | 158 | if (data[IFLA_BOND_PRIMARY]) { |
| 159 | int ifindex = nla_get_u32(data[IFLA_BOND_PRIMARY]); |
| 160 | struct net_device *dev; |
| 161 | char *primary = ""; |
| 162 | |
| 163 | dev = __dev_get_by_index(dev_net(bond_dev), ifindex); |
| 164 | if (dev) |
| 165 | primary = dev->name; |
| 166 | |
| 167 | err = bond_option_primary_set(bond, primary); |
| 168 | if (err) |
| 169 | return err; |
| 170 | } |
Jiri Pirko | 90af231 | 2013-10-18 17:43:38 +0200 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | static int bond_newlink(struct net *src_net, struct net_device *bond_dev, |
| 175 | struct nlattr *tb[], struct nlattr *data[]) |
| 176 | { |
| 177 | int err; |
| 178 | |
| 179 | err = bond_changelink(bond_dev, tb, data); |
| 180 | if (err < 0) |
| 181 | return err; |
| 182 | |
| 183 | return register_netdevice(bond_dev); |
| 184 | } |
| 185 | |
| 186 | static size_t bond_get_size(const struct net_device *bond_dev) |
| 187 | { |
Dan Carpenter | e139862 | 2013-11-01 13:18:44 +0300 | [diff] [blame] | 188 | return nla_total_size(sizeof(u8)) + /* IFLA_BOND_MODE */ |
sfeldma@cumulusnetworks.com | eecdaa6 | 2013-12-12 14:09:55 -0800 | [diff] [blame] | 189 | nla_total_size(sizeof(u32)) + /* IFLA_BOND_ACTIVE_SLAVE */ |
| 190 | nla_total_size(sizeof(u32)) + /* IFLA_BOND_MIIMON */ |
sfeldma@cumulusnetworks.com | 25852e2 | 2013-12-12 14:10:02 -0800 | [diff] [blame] | 191 | nla_total_size(sizeof(u32)) + /* IFLA_BOND_UPDELAY */ |
sfeldma@cumulusnetworks.com | c7461f9 | 2013-12-12 14:10:09 -0800 | [diff] [blame] | 192 | nla_total_size(sizeof(u32)) + /* IFLA_BOND_DOWNDELAY */ |
sfeldma@cumulusnetworks.com | 9f53e14 | 2013-12-12 14:10:16 -0800 | [diff] [blame] | 193 | nla_total_size(sizeof(u8)) + /* IFLA_BOND_USE_CARRIER */ |
sfeldma@cumulusnetworks.com | 06151db | 2013-12-12 14:10:24 -0800 | [diff] [blame] | 194 | nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_INTERVAL */ |
sfeldma@cumulusnetworks.com | 7f28fa1 | 2013-12-12 14:10:31 -0800 | [diff] [blame] | 195 | /* IFLA_BOND_ARP_IP_TARGET */ |
| 196 | nla_total_size(sizeof(u32)) * BOND_MAX_ARP_TARGETS + |
sfeldma@cumulusnetworks.com | 29c4948 | 2013-12-12 14:10:38 -0800 | [diff] [blame] | 197 | nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_VALIDATE */ |
sfeldma@cumulusnetworks.com | d5c8425 | 2013-12-12 14:10:45 -0800 | [diff] [blame] | 198 | nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_ALL_TARGETS */ |
sfeldma@cumulusnetworks.com | 0a98a0d | 2013-12-15 16:41:51 -0800 | [diff] [blame^] | 199 | nla_total_size(sizeof(u32)) + /* IFLA_BOND_PRIMARY */ |
sfeldma@cumulusnetworks.com | eecdaa6 | 2013-12-12 14:09:55 -0800 | [diff] [blame] | 200 | 0; |
Jiri Pirko | 90af231 | 2013-10-18 17:43:38 +0200 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | static int bond_fill_info(struct sk_buff *skb, |
| 204 | const struct net_device *bond_dev) |
| 205 | { |
| 206 | struct bonding *bond = netdev_priv(bond_dev); |
Jiri Pirko | ec76aa4 | 2013-10-18 17:43:39 +0200 | [diff] [blame] | 207 | struct net_device *slave_dev = bond_option_active_slave_get(bond); |
sfeldma@cumulusnetworks.com | 7f28fa1 | 2013-12-12 14:10:31 -0800 | [diff] [blame] | 208 | struct nlattr *targets; |
| 209 | int i, targets_added; |
Jiri Pirko | 90af231 | 2013-10-18 17:43:38 +0200 | [diff] [blame] | 210 | |
sfeldma@cumulusnetworks.com | eecdaa6 | 2013-12-12 14:09:55 -0800 | [diff] [blame] | 211 | if (nla_put_u8(skb, IFLA_BOND_MODE, bond->params.mode)) |
Jiri Pirko | 90af231 | 2013-10-18 17:43:38 +0200 | [diff] [blame] | 212 | goto nla_put_failure; |
sfeldma@cumulusnetworks.com | eecdaa6 | 2013-12-12 14:09:55 -0800 | [diff] [blame] | 213 | |
| 214 | if (slave_dev && |
| 215 | nla_put_u32(skb, IFLA_BOND_ACTIVE_SLAVE, slave_dev->ifindex)) |
| 216 | goto nla_put_failure; |
| 217 | |
| 218 | if (nla_put_u32(skb, IFLA_BOND_MIIMON, bond->params.miimon)) |
| 219 | goto nla_put_failure; |
| 220 | |
sfeldma@cumulusnetworks.com | 25852e2 | 2013-12-12 14:10:02 -0800 | [diff] [blame] | 221 | if (nla_put_u32(skb, IFLA_BOND_UPDELAY, |
| 222 | bond->params.updelay * bond->params.miimon)) |
| 223 | goto nla_put_failure; |
| 224 | |
sfeldma@cumulusnetworks.com | c7461f9 | 2013-12-12 14:10:09 -0800 | [diff] [blame] | 225 | if (nla_put_u32(skb, IFLA_BOND_DOWNDELAY, |
| 226 | bond->params.downdelay * bond->params.miimon)) |
| 227 | goto nla_put_failure; |
| 228 | |
sfeldma@cumulusnetworks.com | 9f53e14 | 2013-12-12 14:10:16 -0800 | [diff] [blame] | 229 | if (nla_put_u8(skb, IFLA_BOND_USE_CARRIER, bond->params.use_carrier)) |
| 230 | goto nla_put_failure; |
| 231 | |
sfeldma@cumulusnetworks.com | 06151db | 2013-12-12 14:10:24 -0800 | [diff] [blame] | 232 | if (nla_put_u32(skb, IFLA_BOND_ARP_INTERVAL, bond->params.arp_interval)) |
| 233 | goto nla_put_failure; |
| 234 | |
sfeldma@cumulusnetworks.com | 7f28fa1 | 2013-12-12 14:10:31 -0800 | [diff] [blame] | 235 | targets = nla_nest_start(skb, IFLA_BOND_ARP_IP_TARGET); |
| 236 | if (!targets) |
| 237 | goto nla_put_failure; |
| 238 | |
| 239 | targets_added = 0; |
| 240 | for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) { |
| 241 | if (bond->params.arp_targets[i]) { |
Jiri Pirko | e7ef941 | 2013-12-14 12:32:10 +0100 | [diff] [blame] | 242 | nla_put_be32(skb, i, bond->params.arp_targets[i]); |
sfeldma@cumulusnetworks.com | 7f28fa1 | 2013-12-12 14:10:31 -0800 | [diff] [blame] | 243 | targets_added = 1; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | if (targets_added) |
| 248 | nla_nest_end(skb, targets); |
| 249 | else |
| 250 | nla_nest_cancel(skb, targets); |
| 251 | |
sfeldma@cumulusnetworks.com | 29c4948 | 2013-12-12 14:10:38 -0800 | [diff] [blame] | 252 | if (nla_put_u32(skb, IFLA_BOND_ARP_VALIDATE, bond->params.arp_validate)) |
| 253 | goto nla_put_failure; |
| 254 | |
sfeldma@cumulusnetworks.com | d5c8425 | 2013-12-12 14:10:45 -0800 | [diff] [blame] | 255 | if (nla_put_u32(skb, IFLA_BOND_ARP_ALL_TARGETS, |
| 256 | bond->params.arp_all_targets)) |
| 257 | goto nla_put_failure; |
| 258 | |
sfeldma@cumulusnetworks.com | 0a98a0d | 2013-12-15 16:41:51 -0800 | [diff] [blame^] | 259 | if (bond->primary_slave && |
| 260 | nla_put_u32(skb, IFLA_BOND_PRIMARY, |
| 261 | bond->primary_slave->dev->ifindex)) |
| 262 | goto nla_put_failure; |
| 263 | |
Jiri Pirko | 90af231 | 2013-10-18 17:43:38 +0200 | [diff] [blame] | 264 | return 0; |
| 265 | |
| 266 | nla_put_failure: |
| 267 | return -EMSGSIZE; |
| 268 | } |
| 269 | |
Jiri Pirko | 0a2a78c | 2013-10-18 17:43:33 +0200 | [diff] [blame] | 270 | struct rtnl_link_ops bond_link_ops __read_mostly = { |
| 271 | .kind = "bond", |
| 272 | .priv_size = sizeof(struct bonding), |
| 273 | .setup = bond_setup, |
Jiri Pirko | 90af231 | 2013-10-18 17:43:38 +0200 | [diff] [blame] | 274 | .maxtype = IFLA_BOND_MAX, |
| 275 | .policy = bond_policy, |
Jiri Pirko | 0a2a78c | 2013-10-18 17:43:33 +0200 | [diff] [blame] | 276 | .validate = bond_validate, |
Jiri Pirko | 90af231 | 2013-10-18 17:43:38 +0200 | [diff] [blame] | 277 | .newlink = bond_newlink, |
| 278 | .changelink = bond_changelink, |
| 279 | .get_size = bond_get_size, |
| 280 | .fill_info = bond_fill_info, |
Jiri Pirko | 0a2a78c | 2013-10-18 17:43:33 +0200 | [diff] [blame] | 281 | .get_num_tx_queues = bond_get_num_tx_queues, |
| 282 | .get_num_rx_queues = bond_get_num_tx_queues, /* Use the same number |
| 283 | as for TX queues */ |
| 284 | }; |
| 285 | |
| 286 | int __init bond_netlink_init(void) |
| 287 | { |
| 288 | return rtnl_link_register(&bond_link_ops); |
| 289 | } |
| 290 | |
David S. Miller | a729e83 | 2013-10-19 19:09:18 -0400 | [diff] [blame] | 291 | void bond_netlink_fini(void) |
Jiri Pirko | 0a2a78c | 2013-10-18 17:43:33 +0200 | [diff] [blame] | 292 | { |
| 293 | rtnl_link_unregister(&bond_link_ops); |
| 294 | } |
| 295 | |
| 296 | MODULE_ALIAS_RTNL_LINK("bond"); |