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 }, |
Jiri Pirko | 90af231 | 2013-10-18 17:43:38 +0200 | [diff] [blame] | 32 | }; |
| 33 | |
Jiri Pirko | 0a2a78c | 2013-10-18 17:43:33 +0200 | [diff] [blame] | 34 | static 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 Pirko | 90af231 | 2013-10-18 17:43:38 +0200 | [diff] [blame] | 45 | static 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.com | 06151db | 2013-12-12 14:10:24 -0800 | [diff] [blame^] | 49 | int miimon = 0; |
Jiri Pirko | 90af231 | 2013-10-18 17:43:38 +0200 | [diff] [blame] | 50 | int err; |
| 51 | |
sfeldma@cumulusnetworks.com | eecdaa6 | 2013-12-12 14:09:55 -0800 | [diff] [blame] | 52 | if (!data) |
| 53 | return 0; |
| 54 | |
| 55 | if (data[IFLA_BOND_MODE]) { |
Jiri Pirko | 90af231 | 2013-10-18 17:43:38 +0200 | [diff] [blame] | 56 | 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.com | eecdaa6 | 2013-12-12 14:09:55 -0800 | [diff] [blame] | 62 | if (data[IFLA_BOND_ACTIVE_SLAVE]) { |
Jiri Pirko | ec76aa4 | 2013-10-18 17:43:39 +0200 | [diff] [blame] | 63 | 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.com | eecdaa6 | 2013-12-12 14:09:55 -0800 | [diff] [blame] | 78 | if (data[IFLA_BOND_MIIMON]) { |
sfeldma@cumulusnetworks.com | 06151db | 2013-12-12 14:10:24 -0800 | [diff] [blame^] | 79 | miimon = nla_get_u32(data[IFLA_BOND_MIIMON]); |
sfeldma@cumulusnetworks.com | eecdaa6 | 2013-12-12 14:09:55 -0800 | [diff] [blame] | 80 | |
| 81 | err = bond_option_miimon_set(bond, miimon); |
| 82 | if (err) |
| 83 | return err; |
| 84 | } |
sfeldma@cumulusnetworks.com | 25852e2 | 2013-12-12 14:10:02 -0800 | [diff] [blame] | 85 | 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.com | c7461f9 | 2013-12-12 14:10:09 -0800 | [diff] [blame] | 92 | 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.com | 9f53e14 | 2013-12-12 14:10:16 -0800 | [diff] [blame] | 99 | 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.com | 06151db | 2013-12-12 14:10:24 -0800 | [diff] [blame^] | 106 | 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 Pirko | 90af231 | 2013-10-18 17:43:38 +0200 | [diff] [blame] | 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | static 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 | |
| 134 | static size_t bond_get_size(const struct net_device *bond_dev) |
| 135 | { |
Dan Carpenter | e139862 | 2013-11-01 13:18:44 +0300 | [diff] [blame] | 136 | return nla_total_size(sizeof(u8)) + /* IFLA_BOND_MODE */ |
sfeldma@cumulusnetworks.com | eecdaa6 | 2013-12-12 14:09:55 -0800 | [diff] [blame] | 137 | nla_total_size(sizeof(u32)) + /* IFLA_BOND_ACTIVE_SLAVE */ |
| 138 | nla_total_size(sizeof(u32)) + /* IFLA_BOND_MIIMON */ |
sfeldma@cumulusnetworks.com | 25852e2 | 2013-12-12 14:10:02 -0800 | [diff] [blame] | 139 | nla_total_size(sizeof(u32)) + /* IFLA_BOND_UPDELAY */ |
sfeldma@cumulusnetworks.com | c7461f9 | 2013-12-12 14:10:09 -0800 | [diff] [blame] | 140 | nla_total_size(sizeof(u32)) + /* IFLA_BOND_DOWNDELAY */ |
sfeldma@cumulusnetworks.com | 9f53e14 | 2013-12-12 14:10:16 -0800 | [diff] [blame] | 141 | nla_total_size(sizeof(u8)) + /* IFLA_BOND_USE_CARRIER */ |
sfeldma@cumulusnetworks.com | 06151db | 2013-12-12 14:10:24 -0800 | [diff] [blame^] | 142 | nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_INTERVAL */ |
sfeldma@cumulusnetworks.com | eecdaa6 | 2013-12-12 14:09:55 -0800 | [diff] [blame] | 143 | 0; |
Jiri Pirko | 90af231 | 2013-10-18 17:43:38 +0200 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | static 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 Pirko | ec76aa4 | 2013-10-18 17:43:39 +0200 | [diff] [blame] | 150 | struct net_device *slave_dev = bond_option_active_slave_get(bond); |
Jiri Pirko | 90af231 | 2013-10-18 17:43:38 +0200 | [diff] [blame] | 151 | |
sfeldma@cumulusnetworks.com | eecdaa6 | 2013-12-12 14:09:55 -0800 | [diff] [blame] | 152 | if (nla_put_u8(skb, IFLA_BOND_MODE, bond->params.mode)) |
Jiri Pirko | 90af231 | 2013-10-18 17:43:38 +0200 | [diff] [blame] | 153 | goto nla_put_failure; |
sfeldma@cumulusnetworks.com | eecdaa6 | 2013-12-12 14:09:55 -0800 | [diff] [blame] | 154 | |
| 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.com | 25852e2 | 2013-12-12 14:10:02 -0800 | [diff] [blame] | 162 | if (nla_put_u32(skb, IFLA_BOND_UPDELAY, |
| 163 | bond->params.updelay * bond->params.miimon)) |
| 164 | goto nla_put_failure; |
| 165 | |
sfeldma@cumulusnetworks.com | c7461f9 | 2013-12-12 14:10:09 -0800 | [diff] [blame] | 166 | if (nla_put_u32(skb, IFLA_BOND_DOWNDELAY, |
| 167 | bond->params.downdelay * bond->params.miimon)) |
| 168 | goto nla_put_failure; |
| 169 | |
sfeldma@cumulusnetworks.com | 9f53e14 | 2013-12-12 14:10:16 -0800 | [diff] [blame] | 170 | if (nla_put_u8(skb, IFLA_BOND_USE_CARRIER, bond->params.use_carrier)) |
| 171 | goto nla_put_failure; |
| 172 | |
sfeldma@cumulusnetworks.com | 06151db | 2013-12-12 14:10:24 -0800 | [diff] [blame^] | 173 | if (nla_put_u32(skb, IFLA_BOND_ARP_INTERVAL, bond->params.arp_interval)) |
| 174 | goto nla_put_failure; |
| 175 | |
Jiri Pirko | 90af231 | 2013-10-18 17:43:38 +0200 | [diff] [blame] | 176 | return 0; |
| 177 | |
| 178 | nla_put_failure: |
| 179 | return -EMSGSIZE; |
| 180 | } |
| 181 | |
Jiri Pirko | 0a2a78c | 2013-10-18 17:43:33 +0200 | [diff] [blame] | 182 | struct rtnl_link_ops bond_link_ops __read_mostly = { |
| 183 | .kind = "bond", |
| 184 | .priv_size = sizeof(struct bonding), |
| 185 | .setup = bond_setup, |
Jiri Pirko | 90af231 | 2013-10-18 17:43:38 +0200 | [diff] [blame] | 186 | .maxtype = IFLA_BOND_MAX, |
| 187 | .policy = bond_policy, |
Jiri Pirko | 0a2a78c | 2013-10-18 17:43:33 +0200 | [diff] [blame] | 188 | .validate = bond_validate, |
Jiri Pirko | 90af231 | 2013-10-18 17:43:38 +0200 | [diff] [blame] | 189 | .newlink = bond_newlink, |
| 190 | .changelink = bond_changelink, |
| 191 | .get_size = bond_get_size, |
| 192 | .fill_info = bond_fill_info, |
Jiri Pirko | 0a2a78c | 2013-10-18 17:43:33 +0200 | [diff] [blame] | 193 | .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 | |
| 198 | int __init bond_netlink_init(void) |
| 199 | { |
| 200 | return rtnl_link_register(&bond_link_ops); |
| 201 | } |
| 202 | |
David S. Miller | a729e83 | 2013-10-19 19:09:18 -0400 | [diff] [blame] | 203 | void bond_netlink_fini(void) |
Jiri Pirko | 0a2a78c | 2013-10-18 17:43:33 +0200 | [diff] [blame] | 204 | { |
| 205 | rtnl_link_unregister(&bond_link_ops); |
| 206 | } |
| 207 | |
| 208 | MODULE_ALIAS_RTNL_LINK("bond"); |