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