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 }, |
Jiri Pirko | 90af231 | 2013-10-18 17:43:38 +0200 | [diff] [blame] | 30 | }; |
| 31 | |
Jiri Pirko | 0a2a78c | 2013-10-18 17:43:33 +0200 | [diff] [blame] | 32 | static int bond_validate(struct nlattr *tb[], struct nlattr *data[]) |
| 33 | { |
| 34 | if (tb[IFLA_ADDRESS]) { |
| 35 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) |
| 36 | return -EINVAL; |
| 37 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) |
| 38 | return -EADDRNOTAVAIL; |
| 39 | } |
| 40 | return 0; |
| 41 | } |
| 42 | |
Jiri Pirko | 90af231 | 2013-10-18 17:43:38 +0200 | [diff] [blame] | 43 | static int bond_changelink(struct net_device *bond_dev, |
| 44 | struct nlattr *tb[], struct nlattr *data[]) |
| 45 | { |
| 46 | struct bonding *bond = netdev_priv(bond_dev); |
| 47 | int err; |
| 48 | |
sfeldma@cumulusnetworks.com | eecdaa6 | 2013-12-12 14:09:55 -0800 | [diff] [blame] | 49 | if (!data) |
| 50 | return 0; |
| 51 | |
| 52 | if (data[IFLA_BOND_MODE]) { |
Jiri Pirko | 90af231 | 2013-10-18 17:43:38 +0200 | [diff] [blame] | 53 | int mode = nla_get_u8(data[IFLA_BOND_MODE]); |
| 54 | |
| 55 | err = bond_option_mode_set(bond, mode); |
| 56 | if (err) |
| 57 | return err; |
| 58 | } |
sfeldma@cumulusnetworks.com | eecdaa6 | 2013-12-12 14:09:55 -0800 | [diff] [blame] | 59 | if (data[IFLA_BOND_ACTIVE_SLAVE]) { |
Jiri Pirko | ec76aa4 | 2013-10-18 17:43:39 +0200 | [diff] [blame] | 60 | int ifindex = nla_get_u32(data[IFLA_BOND_ACTIVE_SLAVE]); |
| 61 | struct net_device *slave_dev; |
| 62 | |
| 63 | if (ifindex == 0) { |
| 64 | slave_dev = NULL; |
| 65 | } else { |
| 66 | slave_dev = __dev_get_by_index(dev_net(bond_dev), |
| 67 | ifindex); |
| 68 | if (!slave_dev) |
| 69 | return -ENODEV; |
| 70 | } |
| 71 | err = bond_option_active_slave_set(bond, slave_dev); |
| 72 | if (err) |
| 73 | return err; |
| 74 | } |
sfeldma@cumulusnetworks.com | eecdaa6 | 2013-12-12 14:09:55 -0800 | [diff] [blame] | 75 | if (data[IFLA_BOND_MIIMON]) { |
| 76 | int miimon = nla_get_u32(data[IFLA_BOND_MIIMON]); |
| 77 | |
| 78 | err = bond_option_miimon_set(bond, miimon); |
| 79 | if (err) |
| 80 | return err; |
| 81 | } |
sfeldma@cumulusnetworks.com | 25852e2 | 2013-12-12 14:10:02 -0800 | [diff] [blame] | 82 | if (data[IFLA_BOND_UPDELAY]) { |
| 83 | int updelay = nla_get_u32(data[IFLA_BOND_UPDELAY]); |
| 84 | |
| 85 | err = bond_option_updelay_set(bond, updelay); |
| 86 | if (err) |
| 87 | return err; |
| 88 | } |
sfeldma@cumulusnetworks.com | c7461f9 | 2013-12-12 14:10:09 -0800 | [diff] [blame^] | 89 | if (data[IFLA_BOND_DOWNDELAY]) { |
| 90 | int downdelay = nla_get_u32(data[IFLA_BOND_DOWNDELAY]); |
| 91 | |
| 92 | err = bond_option_downdelay_set(bond, downdelay); |
| 93 | if (err) |
| 94 | return err; |
| 95 | } |
Jiri Pirko | 90af231 | 2013-10-18 17:43:38 +0200 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static int bond_newlink(struct net *src_net, struct net_device *bond_dev, |
| 100 | struct nlattr *tb[], struct nlattr *data[]) |
| 101 | { |
| 102 | int err; |
| 103 | |
| 104 | err = bond_changelink(bond_dev, tb, data); |
| 105 | if (err < 0) |
| 106 | return err; |
| 107 | |
| 108 | return register_netdevice(bond_dev); |
| 109 | } |
| 110 | |
| 111 | static size_t bond_get_size(const struct net_device *bond_dev) |
| 112 | { |
Dan Carpenter | e139862 | 2013-11-01 13:18:44 +0300 | [diff] [blame] | 113 | return nla_total_size(sizeof(u8)) + /* IFLA_BOND_MODE */ |
sfeldma@cumulusnetworks.com | eecdaa6 | 2013-12-12 14:09:55 -0800 | [diff] [blame] | 114 | nla_total_size(sizeof(u32)) + /* IFLA_BOND_ACTIVE_SLAVE */ |
| 115 | nla_total_size(sizeof(u32)) + /* IFLA_BOND_MIIMON */ |
sfeldma@cumulusnetworks.com | 25852e2 | 2013-12-12 14:10:02 -0800 | [diff] [blame] | 116 | nla_total_size(sizeof(u32)) + /* IFLA_BOND_UPDELAY */ |
sfeldma@cumulusnetworks.com | c7461f9 | 2013-12-12 14:10:09 -0800 | [diff] [blame^] | 117 | nla_total_size(sizeof(u32)) + /* IFLA_BOND_DOWNDELAY */ |
sfeldma@cumulusnetworks.com | eecdaa6 | 2013-12-12 14:09:55 -0800 | [diff] [blame] | 118 | 0; |
Jiri Pirko | 90af231 | 2013-10-18 17:43:38 +0200 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static int bond_fill_info(struct sk_buff *skb, |
| 122 | const struct net_device *bond_dev) |
| 123 | { |
| 124 | struct bonding *bond = netdev_priv(bond_dev); |
Jiri Pirko | ec76aa4 | 2013-10-18 17:43:39 +0200 | [diff] [blame] | 125 | struct net_device *slave_dev = bond_option_active_slave_get(bond); |
Jiri Pirko | 90af231 | 2013-10-18 17:43:38 +0200 | [diff] [blame] | 126 | |
sfeldma@cumulusnetworks.com | eecdaa6 | 2013-12-12 14:09:55 -0800 | [diff] [blame] | 127 | if (nla_put_u8(skb, IFLA_BOND_MODE, bond->params.mode)) |
Jiri Pirko | 90af231 | 2013-10-18 17:43:38 +0200 | [diff] [blame] | 128 | goto nla_put_failure; |
sfeldma@cumulusnetworks.com | eecdaa6 | 2013-12-12 14:09:55 -0800 | [diff] [blame] | 129 | |
| 130 | if (slave_dev && |
| 131 | nla_put_u32(skb, IFLA_BOND_ACTIVE_SLAVE, slave_dev->ifindex)) |
| 132 | goto nla_put_failure; |
| 133 | |
| 134 | if (nla_put_u32(skb, IFLA_BOND_MIIMON, bond->params.miimon)) |
| 135 | goto nla_put_failure; |
| 136 | |
sfeldma@cumulusnetworks.com | 25852e2 | 2013-12-12 14:10:02 -0800 | [diff] [blame] | 137 | if (nla_put_u32(skb, IFLA_BOND_UPDELAY, |
| 138 | bond->params.updelay * bond->params.miimon)) |
| 139 | goto nla_put_failure; |
| 140 | |
sfeldma@cumulusnetworks.com | c7461f9 | 2013-12-12 14:10:09 -0800 | [diff] [blame^] | 141 | if (nla_put_u32(skb, IFLA_BOND_DOWNDELAY, |
| 142 | bond->params.downdelay * bond->params.miimon)) |
| 143 | goto nla_put_failure; |
| 144 | |
Jiri Pirko | 90af231 | 2013-10-18 17:43:38 +0200 | [diff] [blame] | 145 | return 0; |
| 146 | |
| 147 | nla_put_failure: |
| 148 | return -EMSGSIZE; |
| 149 | } |
| 150 | |
Jiri Pirko | 0a2a78c | 2013-10-18 17:43:33 +0200 | [diff] [blame] | 151 | struct rtnl_link_ops bond_link_ops __read_mostly = { |
| 152 | .kind = "bond", |
| 153 | .priv_size = sizeof(struct bonding), |
| 154 | .setup = bond_setup, |
Jiri Pirko | 90af231 | 2013-10-18 17:43:38 +0200 | [diff] [blame] | 155 | .maxtype = IFLA_BOND_MAX, |
| 156 | .policy = bond_policy, |
Jiri Pirko | 0a2a78c | 2013-10-18 17:43:33 +0200 | [diff] [blame] | 157 | .validate = bond_validate, |
Jiri Pirko | 90af231 | 2013-10-18 17:43:38 +0200 | [diff] [blame] | 158 | .newlink = bond_newlink, |
| 159 | .changelink = bond_changelink, |
| 160 | .get_size = bond_get_size, |
| 161 | .fill_info = bond_fill_info, |
Jiri Pirko | 0a2a78c | 2013-10-18 17:43:33 +0200 | [diff] [blame] | 162 | .get_num_tx_queues = bond_get_num_tx_queues, |
| 163 | .get_num_rx_queues = bond_get_num_tx_queues, /* Use the same number |
| 164 | as for TX queues */ |
| 165 | }; |
| 166 | |
| 167 | int __init bond_netlink_init(void) |
| 168 | { |
| 169 | return rtnl_link_register(&bond_link_ops); |
| 170 | } |
| 171 | |
David S. Miller | a729e83 | 2013-10-19 19:09:18 -0400 | [diff] [blame] | 172 | void bond_netlink_fini(void) |
Jiri Pirko | 0a2a78c | 2013-10-18 17:43:33 +0200 | [diff] [blame] | 173 | { |
| 174 | rtnl_link_unregister(&bond_link_ops); |
| 175 | } |
| 176 | |
| 177 | MODULE_ALIAS_RTNL_LINK("bond"); |