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