Commit | Line | Data |
---|---|---|
0a2a78c4 JP |
1 | /* |
2 | * drivers/net/bond/bond_netlink.c - Netlink interface for bonding | |
3 | * Copyright (c) 2013 Jiri Pirko <jiri@resnulli.us> | |
eecdaa6e | 4 | * Copyright (c) 2013 Scott Feldman <sfeldma@cumulusnetworks.com> |
0a2a78c4 JP |
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 | ||
90af2311 JP |
24 | static const struct nla_policy bond_policy[IFLA_BOND_MAX + 1] = { |
25 | [IFLA_BOND_MODE] = { .type = NLA_U8 }, | |
ec76aa49 | 26 | [IFLA_BOND_ACTIVE_SLAVE] = { .type = NLA_U32 }, |
eecdaa6e | 27 | [IFLA_BOND_MIIMON] = { .type = NLA_U32 }, |
25852e29 | 28 | [IFLA_BOND_UPDELAY] = { .type = NLA_U32 }, |
c7461f9b | 29 | [IFLA_BOND_DOWNDELAY] = { .type = NLA_U32 }, |
9f53e14e | 30 | [IFLA_BOND_USE_CARRIER] = { .type = NLA_U8 }, |
90af2311 JP |
31 | }; |
32 | ||
0a2a78c4 JP |
33 | static int bond_validate(struct nlattr *tb[], struct nlattr *data[]) |
34 | { | |
35 | if (tb[IFLA_ADDRESS]) { | |
36 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) | |
37 | return -EINVAL; | |
38 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) | |
39 | return -EADDRNOTAVAIL; | |
40 | } | |
41 | return 0; | |
42 | } | |
43 | ||
90af2311 JP |
44 | static int bond_changelink(struct net_device *bond_dev, |
45 | struct nlattr *tb[], struct nlattr *data[]) | |
46 | { | |
47 | struct bonding *bond = netdev_priv(bond_dev); | |
48 | int err; | |
49 | ||
eecdaa6e | 50 | if (!data) |
51 | return 0; | |
52 | ||
53 | if (data[IFLA_BOND_MODE]) { | |
90af2311 JP |
54 | int mode = nla_get_u8(data[IFLA_BOND_MODE]); |
55 | ||
56 | err = bond_option_mode_set(bond, mode); | |
57 | if (err) | |
58 | return err; | |
59 | } | |
eecdaa6e | 60 | if (data[IFLA_BOND_ACTIVE_SLAVE]) { |
ec76aa49 JP |
61 | int ifindex = nla_get_u32(data[IFLA_BOND_ACTIVE_SLAVE]); |
62 | struct net_device *slave_dev; | |
63 | ||
64 | if (ifindex == 0) { | |
65 | slave_dev = NULL; | |
66 | } else { | |
67 | slave_dev = __dev_get_by_index(dev_net(bond_dev), | |
68 | ifindex); | |
69 | if (!slave_dev) | |
70 | return -ENODEV; | |
71 | } | |
72 | err = bond_option_active_slave_set(bond, slave_dev); | |
73 | if (err) | |
74 | return err; | |
75 | } | |
eecdaa6e | 76 | if (data[IFLA_BOND_MIIMON]) { |
77 | int miimon = nla_get_u32(data[IFLA_BOND_MIIMON]); | |
78 | ||
79 | err = bond_option_miimon_set(bond, miimon); | |
80 | if (err) | |
81 | return err; | |
82 | } | |
25852e29 | 83 | if (data[IFLA_BOND_UPDELAY]) { |
84 | int updelay = nla_get_u32(data[IFLA_BOND_UPDELAY]); | |
85 | ||
86 | err = bond_option_updelay_set(bond, updelay); | |
87 | if (err) | |
88 | return err; | |
89 | } | |
c7461f9b | 90 | if (data[IFLA_BOND_DOWNDELAY]) { |
91 | int downdelay = nla_get_u32(data[IFLA_BOND_DOWNDELAY]); | |
92 | ||
93 | err = bond_option_downdelay_set(bond, downdelay); | |
94 | if (err) | |
95 | return err; | |
96 | } | |
9f53e14e | 97 | if (data[IFLA_BOND_USE_CARRIER]) { |
98 | int use_carrier = nla_get_u8(data[IFLA_BOND_USE_CARRIER]); | |
99 | ||
100 | err = bond_option_use_carrier_set(bond, use_carrier); | |
101 | if (err) | |
102 | return err; | |
103 | } | |
90af2311 JP |
104 | return 0; |
105 | } | |
106 | ||
107 | static int bond_newlink(struct net *src_net, struct net_device *bond_dev, | |
108 | struct nlattr *tb[], struct nlattr *data[]) | |
109 | { | |
110 | int err; | |
111 | ||
112 | err = bond_changelink(bond_dev, tb, data); | |
113 | if (err < 0) | |
114 | return err; | |
115 | ||
116 | return register_netdevice(bond_dev); | |
117 | } | |
118 | ||
119 | static size_t bond_get_size(const struct net_device *bond_dev) | |
120 | { | |
e139862e | 121 | return nla_total_size(sizeof(u8)) + /* IFLA_BOND_MODE */ |
eecdaa6e | 122 | nla_total_size(sizeof(u32)) + /* IFLA_BOND_ACTIVE_SLAVE */ |
123 | nla_total_size(sizeof(u32)) + /* IFLA_BOND_MIIMON */ | |
25852e29 | 124 | nla_total_size(sizeof(u32)) + /* IFLA_BOND_UPDELAY */ |
c7461f9b | 125 | nla_total_size(sizeof(u32)) + /* IFLA_BOND_DOWNDELAY */ |
9f53e14e | 126 | nla_total_size(sizeof(u8)) + /* IFLA_BOND_USE_CARRIER */ |
eecdaa6e | 127 | 0; |
90af2311 JP |
128 | } |
129 | ||
130 | static int bond_fill_info(struct sk_buff *skb, | |
131 | const struct net_device *bond_dev) | |
132 | { | |
133 | struct bonding *bond = netdev_priv(bond_dev); | |
ec76aa49 | 134 | struct net_device *slave_dev = bond_option_active_slave_get(bond); |
90af2311 | 135 | |
eecdaa6e | 136 | if (nla_put_u8(skb, IFLA_BOND_MODE, bond->params.mode)) |
90af2311 | 137 | goto nla_put_failure; |
eecdaa6e | 138 | |
139 | if (slave_dev && | |
140 | nla_put_u32(skb, IFLA_BOND_ACTIVE_SLAVE, slave_dev->ifindex)) | |
141 | goto nla_put_failure; | |
142 | ||
143 | if (nla_put_u32(skb, IFLA_BOND_MIIMON, bond->params.miimon)) | |
144 | goto nla_put_failure; | |
145 | ||
25852e29 | 146 | if (nla_put_u32(skb, IFLA_BOND_UPDELAY, |
147 | bond->params.updelay * bond->params.miimon)) | |
148 | goto nla_put_failure; | |
149 | ||
c7461f9b | 150 | if (nla_put_u32(skb, IFLA_BOND_DOWNDELAY, |
151 | bond->params.downdelay * bond->params.miimon)) | |
152 | goto nla_put_failure; | |
153 | ||
9f53e14e | 154 | if (nla_put_u8(skb, IFLA_BOND_USE_CARRIER, bond->params.use_carrier)) |
155 | goto nla_put_failure; | |
156 | ||
90af2311 JP |
157 | return 0; |
158 | ||
159 | nla_put_failure: | |
160 | return -EMSGSIZE; | |
161 | } | |
162 | ||
0a2a78c4 JP |
163 | struct rtnl_link_ops bond_link_ops __read_mostly = { |
164 | .kind = "bond", | |
165 | .priv_size = sizeof(struct bonding), | |
166 | .setup = bond_setup, | |
90af2311 JP |
167 | .maxtype = IFLA_BOND_MAX, |
168 | .policy = bond_policy, | |
0a2a78c4 | 169 | .validate = bond_validate, |
90af2311 JP |
170 | .newlink = bond_newlink, |
171 | .changelink = bond_changelink, | |
172 | .get_size = bond_get_size, | |
173 | .fill_info = bond_fill_info, | |
0a2a78c4 JP |
174 | .get_num_tx_queues = bond_get_num_tx_queues, |
175 | .get_num_rx_queues = bond_get_num_tx_queues, /* Use the same number | |
176 | as for TX queues */ | |
177 | }; | |
178 | ||
179 | int __init bond_netlink_init(void) | |
180 | { | |
181 | return rtnl_link_register(&bond_link_ops); | |
182 | } | |
183 | ||
a729e83a | 184 | void bond_netlink_fini(void) |
0a2a78c4 JP |
185 | { |
186 | rtnl_link_unregister(&bond_link_ops); | |
187 | } | |
188 | ||
189 | MODULE_ALIAS_RTNL_LINK("bond"); |