blob: 98141338d0ce3931d194bb88173c10b658fdd734 [file] [log] [blame]
Jiri Pirko3d249d42011-11-11 22:16:48 +00001/*
Jiri Pirko0d572e42012-06-19 05:54:09 +00002 * drivers/net/team/team.c - Network team device driver
Jiri Pirko3d249d42011-11-11 22:16:48 +00003 * Copyright (c) 2011 Jiri Pirko <jpirko@redhat.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 */
10
11#include <linux/kernel.h>
12#include <linux/types.h>
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/slab.h>
16#include <linux/rcupdate.h>
17#include <linux/errno.h>
18#include <linux/ctype.h>
19#include <linux/notifier.h>
20#include <linux/netdevice.h>
Jiri Pirkobd2d0832012-07-17 05:22:36 +000021#include <linux/netpoll.h>
Jiri Pirko87002b02011-12-08 04:11:17 +000022#include <linux/if_vlan.h>
Jiri Pirko3d249d42011-11-11 22:16:48 +000023#include <linux/if_arp.h>
24#include <linux/socket.h>
25#include <linux/etherdevice.h>
26#include <linux/rtnetlink.h>
27#include <net/rtnetlink.h>
28#include <net/genetlink.h>
29#include <net/netlink.h>
Jiri Pirko6c85f2b2012-07-20 02:28:51 +000030#include <net/sch_generic.h>
Roopa Prabhua16a8ee72015-01-29 22:40:17 -080031#include <net/switchdev.h>
Flavio Leitner7f51c582012-12-29 16:37:33 +000032#include <generated/utsrelease.h>
Jiri Pirko3d249d42011-11-11 22:16:48 +000033#include <linux/if_team.h>
34
35#define DRV_NAME "team"
36
37
38/**********
39 * Helpers
40 **********/
41
42#define team_port_exists(dev) (dev->priv_flags & IFF_TEAM_PORT)
43
44static struct team_port *team_port_get_rcu(const struct net_device *dev)
45{
Jiri Pirko57e59562015-02-23 14:02:54 +010046 return rcu_dereference(dev->rx_handler_data);
Jiri Pirko3d249d42011-11-11 22:16:48 +000047}
48
49static struct team_port *team_port_get_rtnl(const struct net_device *dev)
50{
51 struct team_port *port = rtnl_dereference(dev->rx_handler_data);
52
53 return team_port_exists(dev) ? port : NULL;
54}
55
56/*
Jiri Pirko1d76efe2012-08-17 04:00:48 +000057 * Since the ability to change device address for open port device is tested in
Jiri Pirko3d249d42011-11-11 22:16:48 +000058 * team_port_add, this function can be called without control of return value
59 */
Jiri Pirko1d76efe2012-08-17 04:00:48 +000060static int __set_port_dev_addr(struct net_device *port_dev,
61 const unsigned char *dev_addr)
Jiri Pirko3d249d42011-11-11 22:16:48 +000062{
63 struct sockaddr addr;
64
Jiri Pirko1d76efe2012-08-17 04:00:48 +000065 memcpy(addr.sa_data, dev_addr, port_dev->addr_len);
66 addr.sa_family = port_dev->type;
Jiri Pirko3d249d42011-11-11 22:16:48 +000067 return dev_set_mac_address(port_dev, &addr);
68}
69
Jiri Pirko1d76efe2012-08-17 04:00:48 +000070static int team_port_set_orig_dev_addr(struct team_port *port)
Jiri Pirko3d249d42011-11-11 22:16:48 +000071{
Jiri Pirko1d76efe2012-08-17 04:00:48 +000072 return __set_port_dev_addr(port->dev, port->orig.dev_addr);
Jiri Pirko3d249d42011-11-11 22:16:48 +000073}
74
Jiri Pirkoacbba0d2013-03-06 01:31:12 +000075static int team_port_set_team_dev_addr(struct team *team,
76 struct team_port *port)
Jiri Pirko3d249d42011-11-11 22:16:48 +000077{
Jiri Pirkoacbba0d2013-03-06 01:31:12 +000078 return __set_port_dev_addr(port->dev, team->dev->dev_addr);
Jiri Pirko3d249d42011-11-11 22:16:48 +000079}
Jiri Pirkoacbba0d2013-03-06 01:31:12 +000080
81int team_modeop_port_enter(struct team *team, struct team_port *port)
82{
83 return team_port_set_team_dev_addr(team, port);
84}
85EXPORT_SYMBOL(team_modeop_port_enter);
86
87void team_modeop_port_change_dev_addr(struct team *team,
88 struct team_port *port)
89{
90 team_port_set_team_dev_addr(team, port);
91}
92EXPORT_SYMBOL(team_modeop_port_change_dev_addr);
Jiri Pirko3d249d42011-11-11 22:16:48 +000093
Jiri Pirko71472ec2012-04-10 05:15:44 +000094static void team_refresh_port_linkup(struct team_port *port)
95{
96 port->linkup = port->user.linkup_enabled ? port->user.linkup :
97 port->state.linkup;
98}
Jiri Pirko3d249d42011-11-11 22:16:48 +000099
Jiri Pirko0f1aad22012-06-19 05:54:11 +0000100
Jiri Pirko3d249d42011-11-11 22:16:48 +0000101/*******************
102 * Options handling
103 *******************/
104
Jiri Pirko80f7c662012-04-10 05:15:42 +0000105struct team_option_inst { /* One for each option instance */
106 struct list_head list;
Jiri Pirko01048d92012-06-19 05:54:14 +0000107 struct list_head tmp_list;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000108 struct team_option *option;
Jiri Pirko85d59a82012-06-19 05:54:10 +0000109 struct team_option_inst_info info;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000110 bool changed;
111 bool removed;
112};
113
114static struct team_option *__team_find_option(struct team *team,
115 const char *opt_name)
Jiri Pirko358b8382011-11-16 11:09:09 +0000116{
117 struct team_option *option;
118
119 list_for_each_entry(option, &team->option_list, list) {
120 if (strcmp(option->name, opt_name) == 0)
121 return option;
122 }
123 return NULL;
124}
125
Jiri Pirko80f7c662012-04-10 05:15:42 +0000126static void __team_option_inst_del(struct team_option_inst *opt_inst)
127{
128 list_del(&opt_inst->list);
129 kfree(opt_inst);
130}
131
132static void __team_option_inst_del_option(struct team *team,
133 struct team_option *option)
134{
135 struct team_option_inst *opt_inst, *tmp;
136
137 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
138 if (opt_inst->option == option)
139 __team_option_inst_del(opt_inst);
140 }
141}
142
Jiri Pirkob1303322012-06-19 05:54:08 +0000143static int __team_option_inst_add(struct team *team, struct team_option *option,
144 struct team_port *port)
145{
146 struct team_option_inst *opt_inst;
147 unsigned int array_size;
148 unsigned int i;
Jiri Pirko85d59a82012-06-19 05:54:10 +0000149 int err;
Jiri Pirkob1303322012-06-19 05:54:08 +0000150
151 array_size = option->array_size;
152 if (!array_size)
153 array_size = 1; /* No array but still need one instance */
154
155 for (i = 0; i < array_size; i++) {
156 opt_inst = kmalloc(sizeof(*opt_inst), GFP_KERNEL);
157 if (!opt_inst)
158 return -ENOMEM;
159 opt_inst->option = option;
Jiri Pirko85d59a82012-06-19 05:54:10 +0000160 opt_inst->info.port = port;
161 opt_inst->info.array_index = i;
Jiri Pirkob1303322012-06-19 05:54:08 +0000162 opt_inst->changed = true;
163 opt_inst->removed = false;
164 list_add_tail(&opt_inst->list, &team->option_inst_list);
Jiri Pirko85d59a82012-06-19 05:54:10 +0000165 if (option->init) {
166 err = option->init(team, &opt_inst->info);
167 if (err)
168 return err;
169 }
170
Jiri Pirkob1303322012-06-19 05:54:08 +0000171 }
172 return 0;
173}
174
Jiri Pirko80f7c662012-04-10 05:15:42 +0000175static int __team_option_inst_add_option(struct team *team,
176 struct team_option *option)
177{
Jiri Pirko80f7c662012-04-10 05:15:42 +0000178 int err;
179
Jiri Pirkob1303322012-06-19 05:54:08 +0000180 if (!option->per_port) {
Jiri Pirkof88725f2012-06-19 05:54:15 +0000181 err = __team_option_inst_add(team, option, NULL);
Jiri Pirkob1303322012-06-19 05:54:08 +0000182 if (err)
183 goto inst_del_option;
184 }
Jiri Pirko80f7c662012-04-10 05:15:42 +0000185 return 0;
186
187inst_del_option:
188 __team_option_inst_del_option(team, option);
189 return err;
190}
191
192static void __team_option_inst_mark_removed_option(struct team *team,
193 struct team_option *option)
194{
195 struct team_option_inst *opt_inst;
196
197 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
198 if (opt_inst->option == option) {
199 opt_inst->changed = true;
200 opt_inst->removed = true;
201 }
202 }
203}
204
205static void __team_option_inst_del_port(struct team *team,
206 struct team_port *port)
207{
208 struct team_option_inst *opt_inst, *tmp;
209
210 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
211 if (opt_inst->option->per_port &&
Jiri Pirko85d59a82012-06-19 05:54:10 +0000212 opt_inst->info.port == port)
Jiri Pirko80f7c662012-04-10 05:15:42 +0000213 __team_option_inst_del(opt_inst);
214 }
215}
216
217static int __team_option_inst_add_port(struct team *team,
218 struct team_port *port)
219{
220 struct team_option *option;
221 int err;
222
223 list_for_each_entry(option, &team->option_list, list) {
224 if (!option->per_port)
225 continue;
226 err = __team_option_inst_add(team, option, port);
227 if (err)
228 goto inst_del_port;
229 }
230 return 0;
231
232inst_del_port:
233 __team_option_inst_del_port(team, port);
234 return err;
235}
236
237static void __team_option_inst_mark_removed_port(struct team *team,
238 struct team_port *port)
239{
240 struct team_option_inst *opt_inst;
241
242 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
Jiri Pirko85d59a82012-06-19 05:54:10 +0000243 if (opt_inst->info.port == port) {
Jiri Pirko80f7c662012-04-10 05:15:42 +0000244 opt_inst->changed = true;
245 opt_inst->removed = true;
246 }
247 }
248}
249
250static int __team_options_register(struct team *team,
251 const struct team_option *option,
252 size_t option_count)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000253{
254 int i;
Jiri Pirko2bba19f2011-11-17 04:16:05 +0000255 struct team_option **dst_opts;
Jiri Pirko358b8382011-11-16 11:09:09 +0000256 int err;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000257
Jiri Pirko2bba19f2011-11-17 04:16:05 +0000258 dst_opts = kzalloc(sizeof(struct team_option *) * option_count,
259 GFP_KERNEL);
260 if (!dst_opts)
261 return -ENOMEM;
Jiri Pirko358b8382011-11-16 11:09:09 +0000262 for (i = 0; i < option_count; i++, option++) {
Jiri Pirko358b8382011-11-16 11:09:09 +0000263 if (__team_find_option(team, option->name)) {
264 err = -EEXIST;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000265 goto alloc_rollback;
Jiri Pirko358b8382011-11-16 11:09:09 +0000266 }
Jiri Pirkof8a15af2011-11-17 06:32:37 +0000267 dst_opts[i] = kmemdup(option, sizeof(*option), GFP_KERNEL);
268 if (!dst_opts[i]) {
Jiri Pirko358b8382011-11-16 11:09:09 +0000269 err = -ENOMEM;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000270 goto alloc_rollback;
Jiri Pirko358b8382011-11-16 11:09:09 +0000271 }
Jiri Pirko358b8382011-11-16 11:09:09 +0000272 }
273
Jiri Pirkob82b9182012-01-24 05:16:00 +0000274 for (i = 0; i < option_count; i++) {
Jiri Pirko80f7c662012-04-10 05:15:42 +0000275 err = __team_option_inst_add_option(team, dst_opts[i]);
276 if (err)
277 goto inst_rollback;
Jiri Pirko358b8382011-11-16 11:09:09 +0000278 list_add_tail(&dst_opts[i]->list, &team->option_list);
Jiri Pirkob82b9182012-01-24 05:16:00 +0000279 }
Jiri Pirko358b8382011-11-16 11:09:09 +0000280
Jiri Pirko2bba19f2011-11-17 04:16:05 +0000281 kfree(dst_opts);
Jiri Pirko358b8382011-11-16 11:09:09 +0000282 return 0;
283
Jiri Pirko80f7c662012-04-10 05:15:42 +0000284inst_rollback:
285 for (i--; i >= 0; i--)
286 __team_option_inst_del_option(team, dst_opts[i]);
287
288 i = option_count - 1;
289alloc_rollback:
290 for (i--; i >= 0; i--)
Jiri Pirko358b8382011-11-16 11:09:09 +0000291 kfree(dst_opts[i]);
292
Jiri Pirko2bba19f2011-11-17 04:16:05 +0000293 kfree(dst_opts);
Jiri Pirko358b8382011-11-16 11:09:09 +0000294 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000295}
Jiri Pirko358b8382011-11-16 11:09:09 +0000296
Jiri Pirkob82b9182012-01-24 05:16:00 +0000297static void __team_options_mark_removed(struct team *team,
298 const struct team_option *option,
299 size_t option_count)
300{
301 int i;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000302
Jiri Pirkob82b9182012-01-24 05:16:00 +0000303 for (i = 0; i < option_count; i++, option++) {
304 struct team_option *del_opt;
305
306 del_opt = __team_find_option(team, option->name);
Jiri Pirko80f7c662012-04-10 05:15:42 +0000307 if (del_opt)
308 __team_option_inst_mark_removed_option(team, del_opt);
Jiri Pirkob82b9182012-01-24 05:16:00 +0000309 }
310}
Jiri Pirko3d249d42011-11-11 22:16:48 +0000311
312static void __team_options_unregister(struct team *team,
Jiri Pirko358b8382011-11-16 11:09:09 +0000313 const struct team_option *option,
Jiri Pirko3d249d42011-11-11 22:16:48 +0000314 size_t option_count)
315{
316 int i;
317
Jiri Pirko358b8382011-11-16 11:09:09 +0000318 for (i = 0; i < option_count; i++, option++) {
319 struct team_option *del_opt;
320
321 del_opt = __team_find_option(team, option->name);
322 if (del_opt) {
Jiri Pirko80f7c662012-04-10 05:15:42 +0000323 __team_option_inst_del_option(team, del_opt);
Jiri Pirko358b8382011-11-16 11:09:09 +0000324 list_del(&del_opt->list);
325 kfree(del_opt);
326 }
327 }
Jiri Pirko3d249d42011-11-11 22:16:48 +0000328}
329
Jiri Pirkob82b9182012-01-24 05:16:00 +0000330static void __team_options_change_check(struct team *team);
331
332int team_options_register(struct team *team,
333 const struct team_option *option,
334 size_t option_count)
335{
336 int err;
337
338 err = __team_options_register(team, option, option_count);
339 if (err)
340 return err;
341 __team_options_change_check(team);
342 return 0;
343}
344EXPORT_SYMBOL(team_options_register);
345
Jiri Pirko358b8382011-11-16 11:09:09 +0000346void team_options_unregister(struct team *team,
347 const struct team_option *option,
Jiri Pirko3d249d42011-11-11 22:16:48 +0000348 size_t option_count)
349{
Jiri Pirkob82b9182012-01-24 05:16:00 +0000350 __team_options_mark_removed(team, option, option_count);
351 __team_options_change_check(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000352 __team_options_unregister(team, option, option_count);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000353}
354EXPORT_SYMBOL(team_options_unregister);
355
Jiri Pirko80f7c662012-04-10 05:15:42 +0000356static int team_option_get(struct team *team,
357 struct team_option_inst *opt_inst,
358 struct team_gsetter_ctx *ctx)
359{
Jiri Pirkof82b9592012-06-19 05:54:07 +0000360 if (!opt_inst->option->getter)
361 return -EOPNOTSUPP;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000362 return opt_inst->option->getter(team, ctx);
363}
364
365static int team_option_set(struct team *team,
366 struct team_option_inst *opt_inst,
367 struct team_gsetter_ctx *ctx)
368{
Jiri Pirkof82b9592012-06-19 05:54:07 +0000369 if (!opt_inst->option->setter)
370 return -EOPNOTSUPP;
Jiri Pirko2fcdb2c2012-06-19 05:54:20 +0000371 return opt_inst->option->setter(team, ctx);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000372}
373
Jiri Pirko0f1aad22012-06-19 05:54:11 +0000374void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info)
375{
376 struct team_option_inst *opt_inst;
377
378 opt_inst = container_of(opt_inst_info, struct team_option_inst, info);
379 opt_inst->changed = true;
380}
381EXPORT_SYMBOL(team_option_inst_set_change);
382
383void team_options_change_check(struct team *team)
384{
385 __team_options_change_check(team);
386}
387EXPORT_SYMBOL(team_options_change_check);
388
389
Jiri Pirko3d249d42011-11-11 22:16:48 +0000390/****************
391 * Mode handling
392 ****************/
393
394static LIST_HEAD(mode_list);
395static DEFINE_SPINLOCK(mode_list_lock);
396
Jiri Pirko04027882012-06-19 05:54:03 +0000397struct team_mode_item {
398 struct list_head list;
399 const struct team_mode *mode;
400};
Jiri Pirko3d249d42011-11-11 22:16:48 +0000401
Jiri Pirko04027882012-06-19 05:54:03 +0000402static struct team_mode_item *__find_mode(const char *kind)
403{
404 struct team_mode_item *mitem;
405
406 list_for_each_entry(mitem, &mode_list, list) {
407 if (strcmp(mitem->mode->kind, kind) == 0)
408 return mitem;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000409 }
410 return NULL;
411}
412
413static bool is_good_mode_name(const char *name)
414{
415 while (*name != '\0') {
416 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
417 return false;
418 name++;
419 }
420 return true;
421}
422
Jiri Pirko04027882012-06-19 05:54:03 +0000423int team_mode_register(const struct team_mode *mode)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000424{
425 int err = 0;
Jiri Pirko04027882012-06-19 05:54:03 +0000426 struct team_mode_item *mitem;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000427
428 if (!is_good_mode_name(mode->kind) ||
429 mode->priv_size > TEAM_MODE_PRIV_SIZE)
430 return -EINVAL;
Jiri Pirko04027882012-06-19 05:54:03 +0000431
432 mitem = kmalloc(sizeof(*mitem), GFP_KERNEL);
433 if (!mitem)
434 return -ENOMEM;
435
Jiri Pirko3d249d42011-11-11 22:16:48 +0000436 spin_lock(&mode_list_lock);
437 if (__find_mode(mode->kind)) {
438 err = -EEXIST;
Jiri Pirko04027882012-06-19 05:54:03 +0000439 kfree(mitem);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000440 goto unlock;
441 }
Jiri Pirko04027882012-06-19 05:54:03 +0000442 mitem->mode = mode;
443 list_add_tail(&mitem->list, &mode_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000444unlock:
445 spin_unlock(&mode_list_lock);
446 return err;
447}
448EXPORT_SYMBOL(team_mode_register);
449
Jiri Pirko04027882012-06-19 05:54:03 +0000450void team_mode_unregister(const struct team_mode *mode)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000451{
Jiri Pirko04027882012-06-19 05:54:03 +0000452 struct team_mode_item *mitem;
453
Jiri Pirko3d249d42011-11-11 22:16:48 +0000454 spin_lock(&mode_list_lock);
Jiri Pirko04027882012-06-19 05:54:03 +0000455 mitem = __find_mode(mode->kind);
456 if (mitem) {
457 list_del_init(&mitem->list);
458 kfree(mitem);
459 }
Jiri Pirko3d249d42011-11-11 22:16:48 +0000460 spin_unlock(&mode_list_lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000461}
462EXPORT_SYMBOL(team_mode_unregister);
463
Jiri Pirko04027882012-06-19 05:54:03 +0000464static const struct team_mode *team_mode_get(const char *kind)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000465{
Jiri Pirko04027882012-06-19 05:54:03 +0000466 struct team_mode_item *mitem;
467 const struct team_mode *mode = NULL;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000468
469 spin_lock(&mode_list_lock);
Jiri Pirko04027882012-06-19 05:54:03 +0000470 mitem = __find_mode(kind);
471 if (!mitem) {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000472 spin_unlock(&mode_list_lock);
473 request_module("team-mode-%s", kind);
474 spin_lock(&mode_list_lock);
Jiri Pirko04027882012-06-19 05:54:03 +0000475 mitem = __find_mode(kind);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000476 }
Jiri Pirko04027882012-06-19 05:54:03 +0000477 if (mitem) {
478 mode = mitem->mode;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000479 if (!try_module_get(mode->owner))
480 mode = NULL;
Jiri Pirko04027882012-06-19 05:54:03 +0000481 }
Jiri Pirko3d249d42011-11-11 22:16:48 +0000482
483 spin_unlock(&mode_list_lock);
484 return mode;
485}
486
487static void team_mode_put(const struct team_mode *mode)
488{
489 module_put(mode->owner);
490}
491
492static bool team_dummy_transmit(struct team *team, struct sk_buff *skb)
493{
494 dev_kfree_skb_any(skb);
495 return false;
496}
497
stephen hemminger2ea46592013-03-08 09:07:43 +0000498static rx_handler_result_t team_dummy_receive(struct team *team,
499 struct team_port *port,
500 struct sk_buff *skb)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000501{
502 return RX_HANDLER_ANOTHER;
503}
504
Jiri Pirkod299cd52012-06-19 05:54:04 +0000505static const struct team_mode __team_no_mode = {
506 .kind = "*NOMODE*",
507};
508
509static bool team_is_mode_set(struct team *team)
510{
511 return team->mode != &__team_no_mode;
512}
513
514static void team_set_no_mode(struct team *team)
515{
Flavio Leitnere1854832013-02-05 09:30:55 +0000516 team->user_carrier_enabled = false;
Jiri Pirkod299cd52012-06-19 05:54:04 +0000517 team->mode = &__team_no_mode;
518}
519
Jiri Pirko735d3812013-06-10 17:42:25 +0200520static void team_adjust_ops(struct team *team)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000521{
522 /*
523 * To avoid checks in rx/tx skb paths, ensure here that non-null and
524 * correct ops are always set.
525 */
526
Jiri Pirko735d3812013-06-10 17:42:25 +0200527 if (!team->en_port_count || !team_is_mode_set(team) ||
Jiri Pirko122bb042012-06-26 06:52:45 +0000528 !team->mode->ops->transmit)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000529 team->ops.transmit = team_dummy_transmit;
530 else
531 team->ops.transmit = team->mode->ops->transmit;
532
Jiri Pirko735d3812013-06-10 17:42:25 +0200533 if (!team->en_port_count || !team_is_mode_set(team) ||
Jiri Pirko122bb042012-06-26 06:52:45 +0000534 !team->mode->ops->receive)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000535 team->ops.receive = team_dummy_receive;
536 else
537 team->ops.receive = team->mode->ops->receive;
538}
539
540/*
541 * We can benefit from the fact that it's ensured no port is present
542 * at the time of mode change. Therefore no packets are in fly so there's no
543 * need to set mode operations in any special way.
544 */
545static int __team_change_mode(struct team *team,
546 const struct team_mode *new_mode)
547{
548 /* Check if mode was previously set and do cleanup if so */
Jiri Pirkod299cd52012-06-19 05:54:04 +0000549 if (team_is_mode_set(team)) {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000550 void (*exit_op)(struct team *team) = team->ops.exit;
551
552 /* Clear ops area so no callback is called any longer */
553 memset(&team->ops, 0, sizeof(struct team_mode_ops));
554 team_adjust_ops(team);
555
556 if (exit_op)
557 exit_op(team);
558 team_mode_put(team->mode);
Jiri Pirkod299cd52012-06-19 05:54:04 +0000559 team_set_no_mode(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000560 /* zero private data area */
561 memset(&team->mode_priv, 0,
562 sizeof(struct team) - offsetof(struct team, mode_priv));
563 }
564
565 if (!new_mode)
566 return 0;
567
568 if (new_mode->ops->init) {
569 int err;
570
571 err = new_mode->ops->init(team);
572 if (err)
573 return err;
574 }
575
576 team->mode = new_mode;
577 memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops));
578 team_adjust_ops(team);
579
580 return 0;
581}
582
583static int team_change_mode(struct team *team, const char *kind)
584{
Jiri Pirko04027882012-06-19 05:54:03 +0000585 const struct team_mode *new_mode;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000586 struct net_device *dev = team->dev;
587 int err;
588
589 if (!list_empty(&team->port_list)) {
590 netdev_err(dev, "No ports can be present during mode change\n");
591 return -EBUSY;
592 }
593
Jiri Pirkod299cd52012-06-19 05:54:04 +0000594 if (team_is_mode_set(team) && strcmp(team->mode->kind, kind) == 0) {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000595 netdev_err(dev, "Unable to change to the same mode the team is in\n");
596 return -EINVAL;
597 }
598
599 new_mode = team_mode_get(kind);
600 if (!new_mode) {
601 netdev_err(dev, "Mode \"%s\" not found\n", kind);
602 return -EINVAL;
603 }
604
605 err = __team_change_mode(team, new_mode);
606 if (err) {
607 netdev_err(dev, "Failed to change to mode \"%s\"\n", kind);
608 team_mode_put(new_mode);
609 return err;
610 }
611
612 netdev_info(dev, "Mode changed to \"%s\"\n", kind);
613 return 0;
614}
615
616
Jiri Pirkofc423ff2013-07-20 12:13:52 +0200617/*********************
618 * Peers notification
619 *********************/
620
621static void team_notify_peers_work(struct work_struct *work)
622{
623 struct team *team;
Jiri Pirkob0d11b42015-01-14 18:15:30 +0100624 int val;
Jiri Pirkofc423ff2013-07-20 12:13:52 +0200625
626 team = container_of(work, struct team, notify_peers.dw.work);
627
628 if (!rtnl_trylock()) {
629 schedule_delayed_work(&team->notify_peers.dw, 0);
630 return;
631 }
Jiri Pirkob0d11b42015-01-14 18:15:30 +0100632 val = atomic_dec_if_positive(&team->notify_peers.count_pending);
633 if (val < 0) {
634 rtnl_unlock();
635 return;
636 }
Jiri Pirkofc423ff2013-07-20 12:13:52 +0200637 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, team->dev);
638 rtnl_unlock();
Jiri Pirkob0d11b42015-01-14 18:15:30 +0100639 if (val)
Jiri Pirkofc423ff2013-07-20 12:13:52 +0200640 schedule_delayed_work(&team->notify_peers.dw,
641 msecs_to_jiffies(team->notify_peers.interval));
642}
643
644static void team_notify_peers(struct team *team)
645{
646 if (!team->notify_peers.count || !netif_running(team->dev))
647 return;
Joe Lawrence47549652014-10-03 09:58:34 -0400648 atomic_add(team->notify_peers.count, &team->notify_peers.count_pending);
Jiri Pirkofc423ff2013-07-20 12:13:52 +0200649 schedule_delayed_work(&team->notify_peers.dw, 0);
650}
651
652static void team_notify_peers_init(struct team *team)
653{
654 INIT_DELAYED_WORK(&team->notify_peers.dw, team_notify_peers_work);
655}
656
657static void team_notify_peers_fini(struct team *team)
658{
659 cancel_delayed_work_sync(&team->notify_peers.dw);
660}
661
662
Jiri Pirko492b2002013-07-20 12:13:54 +0200663/*******************************
664 * Send multicast group rejoins
665 *******************************/
666
667static void team_mcast_rejoin_work(struct work_struct *work)
668{
669 struct team *team;
Jiri Pirkob0d11b42015-01-14 18:15:30 +0100670 int val;
Jiri Pirko492b2002013-07-20 12:13:54 +0200671
672 team = container_of(work, struct team, mcast_rejoin.dw.work);
673
674 if (!rtnl_trylock()) {
675 schedule_delayed_work(&team->mcast_rejoin.dw, 0);
676 return;
677 }
Jiri Pirkob0d11b42015-01-14 18:15:30 +0100678 val = atomic_dec_if_positive(&team->mcast_rejoin.count_pending);
679 if (val < 0) {
680 rtnl_unlock();
681 return;
682 }
Jiri Pirko492b2002013-07-20 12:13:54 +0200683 call_netdevice_notifiers(NETDEV_RESEND_IGMP, team->dev);
684 rtnl_unlock();
Jiri Pirkob0d11b42015-01-14 18:15:30 +0100685 if (val)
Jiri Pirko492b2002013-07-20 12:13:54 +0200686 schedule_delayed_work(&team->mcast_rejoin.dw,
687 msecs_to_jiffies(team->mcast_rejoin.interval));
688}
689
690static void team_mcast_rejoin(struct team *team)
691{
692 if (!team->mcast_rejoin.count || !netif_running(team->dev))
693 return;
Joe Lawrence47549652014-10-03 09:58:34 -0400694 atomic_add(team->mcast_rejoin.count, &team->mcast_rejoin.count_pending);
Jiri Pirko492b2002013-07-20 12:13:54 +0200695 schedule_delayed_work(&team->mcast_rejoin.dw, 0);
696}
697
698static void team_mcast_rejoin_init(struct team *team)
699{
700 INIT_DELAYED_WORK(&team->mcast_rejoin.dw, team_mcast_rejoin_work);
701}
702
703static void team_mcast_rejoin_fini(struct team *team)
704{
705 cancel_delayed_work_sync(&team->mcast_rejoin.dw);
706}
707
708
Jiri Pirko3d249d42011-11-11 22:16:48 +0000709/************************
710 * Rx path frame handler
711 ************************/
712
713/* note: already called with rcu_read_lock */
714static rx_handler_result_t team_handle_frame(struct sk_buff **pskb)
715{
716 struct sk_buff *skb = *pskb;
717 struct team_port *port;
718 struct team *team;
719 rx_handler_result_t res;
720
721 skb = skb_share_check(skb, GFP_ATOMIC);
722 if (!skb)
723 return RX_HANDLER_CONSUMED;
724
725 *pskb = skb;
726
727 port = team_port_get_rcu(skb->dev);
728 team = port->team;
Jiri Pirko19a0b582012-04-20 04:42:05 +0000729 if (!team_port_enabled(port)) {
730 /* allow exact match delivery for disabled ports */
731 res = RX_HANDLER_EXACT;
732 } else {
733 res = team->ops.receive(team, port, skb);
734 }
Jiri Pirko3d249d42011-11-11 22:16:48 +0000735 if (res == RX_HANDLER_ANOTHER) {
736 struct team_pcpu_stats *pcpu_stats;
737
738 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
739 u64_stats_update_begin(&pcpu_stats->syncp);
740 pcpu_stats->rx_packets++;
741 pcpu_stats->rx_bytes += skb->len;
742 if (skb->pkt_type == PACKET_MULTICAST)
743 pcpu_stats->rx_multicast++;
744 u64_stats_update_end(&pcpu_stats->syncp);
745
746 skb->dev = team->dev;
747 } else {
748 this_cpu_inc(team->pcpu_stats->rx_dropped);
749 }
750
751 return res;
752}
753
754
Jiri Pirko8ff51052012-07-27 06:28:55 +0000755/*************************************
756 * Multiqueue Tx port select override
757 *************************************/
758
759static int team_queue_override_init(struct team *team)
760{
761 struct list_head *listarr;
762 unsigned int queue_cnt = team->dev->num_tx_queues - 1;
763 unsigned int i;
764
765 if (!queue_cnt)
766 return 0;
767 listarr = kmalloc(sizeof(struct list_head) * queue_cnt, GFP_KERNEL);
768 if (!listarr)
769 return -ENOMEM;
770 team->qom_lists = listarr;
771 for (i = 0; i < queue_cnt; i++)
772 INIT_LIST_HEAD(listarr++);
773 return 0;
774}
775
776static void team_queue_override_fini(struct team *team)
777{
778 kfree(team->qom_lists);
779}
780
781static struct list_head *__team_get_qom_list(struct team *team, u16 queue_id)
782{
783 return &team->qom_lists[queue_id - 1];
784}
785
786/*
787 * note: already called with rcu_read_lock
788 */
789static bool team_queue_override_transmit(struct team *team, struct sk_buff *skb)
790{
791 struct list_head *qom_list;
792 struct team_port *port;
793
794 if (!team->queue_override_enabled || !skb->queue_mapping)
795 return false;
796 qom_list = __team_get_qom_list(team, skb->queue_mapping);
797 list_for_each_entry_rcu(port, qom_list, qom_list) {
798 if (!team_dev_queue_xmit(team, port, skb))
799 return true;
800 }
801 return false;
802}
803
804static void __team_queue_override_port_del(struct team *team,
805 struct team_port *port)
806{
Jiri Pirko6c31ff32013-06-10 17:42:23 +0200807 if (!port->queue_id)
808 return;
Jiri Pirko8ff51052012-07-27 06:28:55 +0000809 list_del_rcu(&port->qom_list);
Jiri Pirko8ff51052012-07-27 06:28:55 +0000810}
811
812static bool team_queue_override_port_has_gt_prio_than(struct team_port *port,
813 struct team_port *cur)
814{
815 if (port->priority < cur->priority)
816 return true;
817 if (port->priority > cur->priority)
818 return false;
819 if (port->index < cur->index)
820 return true;
821 return false;
822}
823
824static void __team_queue_override_port_add(struct team *team,
825 struct team_port *port)
826{
827 struct team_port *cur;
828 struct list_head *qom_list;
829 struct list_head *node;
830
Jiri Pirko6c31ff32013-06-10 17:42:23 +0200831 if (!port->queue_id)
Jiri Pirko8ff51052012-07-27 06:28:55 +0000832 return;
Jiri Pirko8ff51052012-07-27 06:28:55 +0000833 qom_list = __team_get_qom_list(team, port->queue_id);
834 node = qom_list;
835 list_for_each_entry(cur, qom_list, qom_list) {
836 if (team_queue_override_port_has_gt_prio_than(port, cur))
837 break;
838 node = &cur->qom_list;
839 }
840 list_add_tail_rcu(&port->qom_list, node);
841}
842
843static void __team_queue_override_enabled_check(struct team *team)
844{
845 struct team_port *port;
846 bool enabled = false;
847
848 list_for_each_entry(port, &team->port_list, list) {
Jiri Pirko6c31ff32013-06-10 17:42:23 +0200849 if (port->queue_id) {
Jiri Pirko8ff51052012-07-27 06:28:55 +0000850 enabled = true;
851 break;
852 }
853 }
854 if (enabled == team->queue_override_enabled)
855 return;
856 netdev_dbg(team->dev, "%s queue override\n",
857 enabled ? "Enabling" : "Disabling");
858 team->queue_override_enabled = enabled;
859}
860
Jiri Pirko6c31ff32013-06-10 17:42:23 +0200861static void team_queue_override_port_prio_changed(struct team *team,
862 struct team_port *port)
Jiri Pirko8ff51052012-07-27 06:28:55 +0000863{
Jiri Pirko6c31ff32013-06-10 17:42:23 +0200864 if (!port->queue_id || team_port_enabled(port))
865 return;
Jiri Pirko8ff51052012-07-27 06:28:55 +0000866 __team_queue_override_port_del(team, port);
867 __team_queue_override_port_add(team, port);
868 __team_queue_override_enabled_check(team);
869}
870
Jiri Pirko6c31ff32013-06-10 17:42:23 +0200871static void team_queue_override_port_change_queue_id(struct team *team,
872 struct team_port *port,
873 u16 new_queue_id)
874{
875 if (team_port_enabled(port)) {
876 __team_queue_override_port_del(team, port);
877 port->queue_id = new_queue_id;
878 __team_queue_override_port_add(team, port);
879 __team_queue_override_enabled_check(team);
880 } else {
881 port->queue_id = new_queue_id;
882 }
883}
884
885static void team_queue_override_port_add(struct team *team,
886 struct team_port *port)
887{
888 __team_queue_override_port_add(team, port);
889 __team_queue_override_enabled_check(team);
890}
891
892static void team_queue_override_port_del(struct team *team,
893 struct team_port *port)
894{
895 __team_queue_override_port_del(team, port);
896 __team_queue_override_enabled_check(team);
897}
898
Jiri Pirko8ff51052012-07-27 06:28:55 +0000899
Jiri Pirko3d249d42011-11-11 22:16:48 +0000900/****************
901 * Port handling
902 ****************/
903
904static bool team_port_find(const struct team *team,
905 const struct team_port *port)
906{
907 struct team_port *cur;
908
909 list_for_each_entry(cur, &team->port_list, list)
910 if (cur == port)
911 return true;
912 return false;
913}
914
Jiri Pirko19a0b582012-04-20 04:42:05 +0000915/*
916 * Enable/disable port by adding to enabled port hashlist and setting
917 * port->index (Might be racy so reader could see incorrect ifindex when
918 * processing a flying packet, but that is not a problem). Write guarded
919 * by team->lock.
920 */
921static void team_port_enable(struct team *team,
922 struct team_port *port)
923{
924 if (team_port_enabled(port))
925 return;
926 port->index = team->en_port_count++;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000927 hlist_add_head_rcu(&port->hlist,
928 team_port_index_hash(team, port->index));
Jiri Pirko122bb042012-06-26 06:52:45 +0000929 team_adjust_ops(team);
Jiri Pirko6c31ff32013-06-10 17:42:23 +0200930 team_queue_override_port_add(team, port);
Jiri Pirko4bccfd12012-06-19 05:54:16 +0000931 if (team->ops.port_enabled)
932 team->ops.port_enabled(team, port);
Jiri Pirkofc423ff2013-07-20 12:13:52 +0200933 team_notify_peers(team);
Jiri Pirko492b2002013-07-20 12:13:54 +0200934 team_mcast_rejoin(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000935}
936
937static void __reconstruct_port_hlist(struct team *team, int rm_index)
938{
939 int i;
940 struct team_port *port;
941
Jiri Pirko19a0b582012-04-20 04:42:05 +0000942 for (i = rm_index + 1; i < team->en_port_count; i++) {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000943 port = team_get_port_by_index(team, i);
944 hlist_del_rcu(&port->hlist);
945 port->index--;
946 hlist_add_head_rcu(&port->hlist,
947 team_port_index_hash(team, port->index));
948 }
949}
950
Jiri Pirko19a0b582012-04-20 04:42:05 +0000951static void team_port_disable(struct team *team,
952 struct team_port *port)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000953{
Jiri Pirko19a0b582012-04-20 04:42:05 +0000954 if (!team_port_enabled(port))
955 return;
Jiri Pirko4bccfd12012-06-19 05:54:16 +0000956 if (team->ops.port_disabled)
957 team->ops.port_disabled(team, port);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000958 hlist_del_rcu(&port->hlist);
Jiri Pirko122bb042012-06-26 06:52:45 +0000959 __reconstruct_port_hlist(team, port->index);
Jiri Pirko19a0b582012-04-20 04:42:05 +0000960 port->index = -1;
Jiri Pirko122bb042012-06-26 06:52:45 +0000961 team->en_port_count--;
Jiri Pirko735d3812013-06-10 17:42:25 +0200962 team_queue_override_port_del(team, port);
963 team_adjust_ops(team);
Jiri Pirkofc423ff2013-07-20 12:13:52 +0200964 team_notify_peers(team);
Jiri Pirko492b2002013-07-20 12:13:54 +0200965 team_mcast_rejoin(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000966}
967
968#define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \
969 NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
970 NETIF_F_HIGHDMA | NETIF_F_LRO)
971
972static void __team_compute_features(struct team *team)
973{
974 struct team_port *port;
Michal Kubeček36259202014-05-20 08:29:45 +0200975 u32 vlan_features = TEAM_VLAN_FEATURES & NETIF_F_ALL_FOR_ALL;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000976 unsigned short max_hard_header_len = ETH_HLEN;
Eric Dumazet02875872014-10-05 18:38:35 -0700977 unsigned int dst_release_flag = IFF_XMIT_DST_RELEASE |
978 IFF_XMIT_DST_RELEASE_PERM;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000979
980 list_for_each_entry(port, &team->port_list, list) {
981 vlan_features = netdev_increment_features(vlan_features,
982 port->dev->vlan_features,
983 TEAM_VLAN_FEATURES);
984
Jiri Pirkodc905952012-07-18 07:39:38 +0000985 dst_release_flag &= port->dev->priv_flags;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000986 if (port->dev->hard_header_len > max_hard_header_len)
987 max_hard_header_len = port->dev->hard_header_len;
988 }
989
990 team->dev->vlan_features = vlan_features;
991 team->dev->hard_header_len = max_hard_header_len;
992
Eric Dumazet02875872014-10-05 18:38:35 -0700993 team->dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
994 if (dst_release_flag == (IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM))
995 team->dev->priv_flags |= IFF_XMIT_DST_RELEASE;
Jiri Pirkodc905952012-07-18 07:39:38 +0000996
Jiri Pirko3d249d42011-11-11 22:16:48 +0000997 netdev_change_features(team->dev);
998}
999
1000static void team_compute_features(struct team *team)
1001{
Jiri Pirko61dc3462011-11-16 11:09:08 +00001002 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001003 __team_compute_features(team);
Jiri Pirko61dc3462011-11-16 11:09:08 +00001004 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001005}
1006
1007static int team_port_enter(struct team *team, struct team_port *port)
1008{
1009 int err = 0;
1010
1011 dev_hold(team->dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001012 if (team->ops.port_enter) {
1013 err = team->ops.port_enter(team, port);
1014 if (err) {
1015 netdev_err(team->dev, "Device %s failed to enter team mode\n",
1016 port->dev->name);
1017 goto err_port_enter;
1018 }
1019 }
1020
1021 return 0;
1022
1023err_port_enter:
Jiri Pirko3d249d42011-11-11 22:16:48 +00001024 dev_put(team->dev);
1025
1026 return err;
1027}
1028
1029static void team_port_leave(struct team *team, struct team_port *port)
1030{
1031 if (team->ops.port_leave)
1032 team->ops.port_leave(team, port);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001033 dev_put(team->dev);
1034}
1035
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001036#ifdef CONFIG_NET_POLL_CONTROLLER
Eric W. Biedermana8779ec2014-03-27 15:36:38 -07001037static int team_port_enable_netpoll(struct team *team, struct team_port *port)
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001038{
1039 struct netpoll *np;
1040 int err;
1041
stephen hemminger0fb52a22013-07-24 11:52:44 -07001042 if (!team->dev->npinfo)
1043 return 0;
1044
Eric W. Biedermana8779ec2014-03-27 15:36:38 -07001045 np = kzalloc(sizeof(*np), GFP_KERNEL);
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001046 if (!np)
1047 return -ENOMEM;
1048
Eric W. Biedermana8779ec2014-03-27 15:36:38 -07001049 err = __netpoll_setup(np, port->dev);
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001050 if (err) {
1051 kfree(np);
1052 return err;
1053 }
1054 port->np = np;
1055 return err;
1056}
1057
1058static void team_port_disable_netpoll(struct team_port *port)
1059{
1060 struct netpoll *np = port->np;
1061
1062 if (!np)
1063 return;
1064 port->np = NULL;
1065
1066 /* Wait for transmitting packets to finish before freeing. */
1067 synchronize_rcu_bh();
1068 __netpoll_cleanup(np);
1069 kfree(np);
1070}
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001071#else
Eric W. Biedermana8779ec2014-03-27 15:36:38 -07001072static int team_port_enable_netpoll(struct team *team, struct team_port *port)
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001073{
1074 return 0;
1075}
1076static void team_port_disable_netpoll(struct team_port *port)
1077{
1078}
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001079#endif
1080
Jiri Pirko8fd72852015-12-03 12:12:13 +01001081static int team_upper_dev_link(struct team *team, struct team_port *port)
Jiri Pirkod7d3c052014-08-25 21:38:27 +02001082{
Jiri Pirko8fd72852015-12-03 12:12:13 +01001083 struct netdev_lag_upper_info lag_upper_info;
Jiri Pirkod7d3c052014-08-25 21:38:27 +02001084 int err;
1085
Jiri Pirko8fd72852015-12-03 12:12:13 +01001086 lag_upper_info.tx_type = team->mode->lag_tx_type;
1087 err = netdev_master_upper_dev_link(port->dev, team->dev, NULL,
1088 &lag_upper_info);
Jiri Pirkod7d3c052014-08-25 21:38:27 +02001089 if (err)
1090 return err;
Jiri Pirko8fd72852015-12-03 12:12:13 +01001091 port->dev->priv_flags |= IFF_TEAM_PORT;
Jiri Pirkod7d3c052014-08-25 21:38:27 +02001092 return 0;
1093}
1094
Jiri Pirko8fd72852015-12-03 12:12:13 +01001095static void team_upper_dev_unlink(struct team *team, struct team_port *port)
Jiri Pirkod7d3c052014-08-25 21:38:27 +02001096{
Jiri Pirko8fd72852015-12-03 12:12:13 +01001097 netdev_upper_dev_unlink(port->dev, team->dev);
1098 port->dev->priv_flags &= ~IFF_TEAM_PORT;
Jiri Pirkod7d3c052014-08-25 21:38:27 +02001099}
1100
Jiri Pirko10f3f512012-09-21 01:51:59 +00001101static void __team_port_change_port_added(struct team_port *port, bool linkup);
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001102static int team_dev_type_check_change(struct net_device *dev,
1103 struct net_device *port_dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001104
1105static int team_port_add(struct team *team, struct net_device *port_dev)
1106{
1107 struct net_device *dev = team->dev;
1108 struct team_port *port;
1109 char *portname = port_dev->name;
1110 int err;
1111
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001112 if (port_dev->flags & IFF_LOOPBACK) {
1113 netdev_err(dev, "Device %s is loopback device. Loopback devices can't be added as a team port\n",
Jiri Pirko3d249d42011-11-11 22:16:48 +00001114 portname);
1115 return -EINVAL;
1116 }
1117
1118 if (team_port_exists(port_dev)) {
1119 netdev_err(dev, "Device %s is already a port "
1120 "of a team device\n", portname);
1121 return -EBUSY;
1122 }
1123
Jiri Pirko2c33bb32012-08-23 03:26:53 +00001124 if (port_dev->features & NETIF_F_VLAN_CHALLENGED &&
1125 vlan_uses_dev(dev)) {
1126 netdev_err(dev, "Device %s is VLAN challenged and team device has VLAN set up\n",
1127 portname);
1128 return -EPERM;
1129 }
1130
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001131 err = team_dev_type_check_change(dev, port_dev);
1132 if (err)
1133 return err;
1134
Jiri Pirko3d249d42011-11-11 22:16:48 +00001135 if (port_dev->flags & IFF_UP) {
1136 netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
1137 portname);
1138 return -EBUSY;
1139 }
1140
Jiri Pirko5149ee52012-06-19 05:54:05 +00001141 port = kzalloc(sizeof(struct team_port) + team->mode->port_priv_size,
1142 GFP_KERNEL);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001143 if (!port)
1144 return -ENOMEM;
1145
1146 port->dev = port_dev;
1147 port->team = team;
Jiri Pirko8ff51052012-07-27 06:28:55 +00001148 INIT_LIST_HEAD(&port->qom_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001149
1150 port->orig.mtu = port_dev->mtu;
1151 err = dev_set_mtu(port_dev, dev->mtu);
1152 if (err) {
1153 netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
1154 goto err_set_mtu;
1155 }
1156
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001157 memcpy(port->orig.dev_addr, port_dev->dev_addr, port_dev->addr_len);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001158
1159 err = team_port_enter(team, port);
1160 if (err) {
1161 netdev_err(dev, "Device %s failed to enter team mode\n",
1162 portname);
1163 goto err_port_enter;
1164 }
1165
1166 err = dev_open(port_dev);
1167 if (err) {
1168 netdev_dbg(dev, "Device %s opening failed\n",
1169 portname);
1170 goto err_dev_open;
1171 }
1172
Jiri Pirko57459182011-12-08 04:11:20 +00001173 err = vlan_vids_add_by_dev(port_dev, dev);
1174 if (err) {
1175 netdev_err(dev, "Failed to add vlan ids to device %s\n",
1176 portname);
1177 goto err_vids_add;
1178 }
1179
Eric W. Biedermana8779ec2014-03-27 15:36:38 -07001180 err = team_port_enable_netpoll(team, port);
stephen hemminger0fb52a22013-07-24 11:52:44 -07001181 if (err) {
1182 netdev_err(dev, "Failed to enable netpoll on device %s\n",
1183 portname);
1184 goto err_enable_netpoll;
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001185 }
1186
Michal Kubečekfbe168b2014-11-13 07:54:50 +01001187 if (!(dev->features & NETIF_F_LRO))
1188 dev_disable_lro(port_dev);
1189
Jiri Pirko3d249d42011-11-11 22:16:48 +00001190 err = netdev_rx_handler_register(port_dev, team_handle_frame,
1191 port);
1192 if (err) {
1193 netdev_err(dev, "Device %s failed to register rx_handler\n",
1194 portname);
1195 goto err_handler_register;
1196 }
1197
Jiri Pirko8fd72852015-12-03 12:12:13 +01001198 err = team_upper_dev_link(team, port);
Jiri Pirkod7d3c052014-08-25 21:38:27 +02001199 if (err) {
1200 netdev_err(dev, "Device %s failed to set upper link\n",
1201 portname);
1202 goto err_set_upper_link;
1203 }
1204
Jiri Pirko35b384b2012-06-19 05:54:19 +00001205 err = __team_option_inst_add_port(team, port);
Jiri Pirko80f7c662012-04-10 05:15:42 +00001206 if (err) {
1207 netdev_err(dev, "Device %s failed to add per-port options\n",
1208 portname);
1209 goto err_option_port_add;
1210 }
1211
Jiri Pirko19a0b582012-04-20 04:42:05 +00001212 port->index = -1;
Jiri Pirko19a0b582012-04-20 04:42:05 +00001213 list_add_tail_rcu(&port->list, &team->port_list);
Jiri Pirko72df9352013-06-08 15:00:54 +02001214 team_port_enable(team, port);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001215 __team_compute_features(team);
Jiri Pirko10f3f512012-09-21 01:51:59 +00001216 __team_port_change_port_added(port, !!netif_carrier_ok(port_dev));
Jiri Pirko35b384b2012-06-19 05:54:19 +00001217 __team_options_change_check(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001218
1219 netdev_info(dev, "Port device %s added\n", portname);
1220
1221 return 0;
1222
Jiri Pirko80f7c662012-04-10 05:15:42 +00001223err_option_port_add:
Jiri Pirko8fd72852015-12-03 12:12:13 +01001224 team_upper_dev_unlink(team, port);
Jiri Pirkod7d3c052014-08-25 21:38:27 +02001225
1226err_set_upper_link:
Jiri Pirko80f7c662012-04-10 05:15:42 +00001227 netdev_rx_handler_unregister(port_dev);
1228
Jiri Pirko3d249d42011-11-11 22:16:48 +00001229err_handler_register:
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001230 team_port_disable_netpoll(port);
1231
1232err_enable_netpoll:
Jiri Pirko57459182011-12-08 04:11:20 +00001233 vlan_vids_del_by_dev(port_dev, dev);
1234
1235err_vids_add:
Jiri Pirko3d249d42011-11-11 22:16:48 +00001236 dev_close(port_dev);
1237
1238err_dev_open:
1239 team_port_leave(team, port);
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001240 team_port_set_orig_dev_addr(port);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001241
1242err_port_enter:
1243 dev_set_mtu(port_dev, port->orig.mtu);
1244
1245err_set_mtu:
1246 kfree(port);
1247
1248 return err;
1249}
1250
Jiri Pirko10f3f512012-09-21 01:51:59 +00001251static void __team_port_change_port_removed(struct team_port *port);
1252
Jiri Pirko3d249d42011-11-11 22:16:48 +00001253static int team_port_del(struct team *team, struct net_device *port_dev)
1254{
1255 struct net_device *dev = team->dev;
1256 struct team_port *port;
1257 char *portname = port_dev->name;
1258
1259 port = team_port_get_rtnl(port_dev);
1260 if (!port || !team_port_find(team, port)) {
1261 netdev_err(dev, "Device %s does not act as a port of this team\n",
1262 portname);
1263 return -ENOENT;
1264 }
1265
Jiri Pirko19a0b582012-04-20 04:42:05 +00001266 team_port_disable(team, port);
1267 list_del_rcu(&port->list);
Jiri Pirko8fd72852015-12-03 12:12:13 +01001268 team_upper_dev_unlink(team, port);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001269 netdev_rx_handler_unregister(port_dev);
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001270 team_port_disable_netpoll(port);
Jiri Pirko57459182011-12-08 04:11:20 +00001271 vlan_vids_del_by_dev(port_dev, dev);
Vlad Yasevichba812762013-03-07 07:59:25 +00001272 dev_uc_unsync(port_dev, dev);
1273 dev_mc_unsync(port_dev, dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001274 dev_close(port_dev);
1275 team_port_leave(team, port);
Jiri Pirkoc3969d802013-02-01 08:17:25 +00001276
1277 __team_option_inst_mark_removed_port(team, port);
1278 __team_options_change_check(team);
1279 __team_option_inst_del_port(team, port);
1280 __team_port_change_port_removed(port);
1281
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001282 team_port_set_orig_dev_addr(port);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001283 dev_set_mtu(port_dev, port->orig.mtu);
Jiri Pirkod80b35b2013-06-10 17:42:24 +02001284 kfree_rcu(port, rcu);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001285 netdev_info(dev, "Port device %s removed\n", portname);
1286 __team_compute_features(team);
1287
1288 return 0;
1289}
1290
1291
1292/*****************
1293 * Net device ops
1294 *****************/
1295
Jiri Pirko80f7c662012-04-10 05:15:42 +00001296static int team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001297{
Jiri Pirkod299cd52012-06-19 05:54:04 +00001298 ctx->data.str_val = team->mode->kind;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001299 return 0;
1300}
1301
Jiri Pirko80f7c662012-04-10 05:15:42 +00001302static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001303{
Jiri Pirko80f7c662012-04-10 05:15:42 +00001304 return team_change_mode(team, ctx->data.str_val);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001305}
1306
Jiri Pirkofc423ff2013-07-20 12:13:52 +02001307static int team_notify_peers_count_get(struct team *team,
1308 struct team_gsetter_ctx *ctx)
1309{
1310 ctx->data.u32_val = team->notify_peers.count;
1311 return 0;
1312}
1313
1314static int team_notify_peers_count_set(struct team *team,
1315 struct team_gsetter_ctx *ctx)
1316{
1317 team->notify_peers.count = ctx->data.u32_val;
1318 return 0;
1319}
1320
1321static int team_notify_peers_interval_get(struct team *team,
1322 struct team_gsetter_ctx *ctx)
1323{
1324 ctx->data.u32_val = team->notify_peers.interval;
1325 return 0;
1326}
1327
1328static int team_notify_peers_interval_set(struct team *team,
1329 struct team_gsetter_ctx *ctx)
1330{
1331 team->notify_peers.interval = ctx->data.u32_val;
1332 return 0;
1333}
1334
Jiri Pirko492b2002013-07-20 12:13:54 +02001335static int team_mcast_rejoin_count_get(struct team *team,
1336 struct team_gsetter_ctx *ctx)
1337{
1338 ctx->data.u32_val = team->mcast_rejoin.count;
1339 return 0;
1340}
1341
1342static int team_mcast_rejoin_count_set(struct team *team,
1343 struct team_gsetter_ctx *ctx)
1344{
1345 team->mcast_rejoin.count = ctx->data.u32_val;
1346 return 0;
1347}
1348
1349static int team_mcast_rejoin_interval_get(struct team *team,
1350 struct team_gsetter_ctx *ctx)
1351{
1352 ctx->data.u32_val = team->mcast_rejoin.interval;
1353 return 0;
1354}
1355
1356static int team_mcast_rejoin_interval_set(struct team *team,
1357 struct team_gsetter_ctx *ctx)
1358{
1359 team->mcast_rejoin.interval = ctx->data.u32_val;
1360 return 0;
1361}
1362
Jiri Pirkoacd69962012-04-20 04:42:06 +00001363static int team_port_en_option_get(struct team *team,
1364 struct team_gsetter_ctx *ctx)
1365{
Jiri Pirko85d59a82012-06-19 05:54:10 +00001366 struct team_port *port = ctx->info->port;
1367
1368 ctx->data.bool_val = team_port_enabled(port);
Jiri Pirkoacd69962012-04-20 04:42:06 +00001369 return 0;
1370}
1371
1372static int team_port_en_option_set(struct team *team,
1373 struct team_gsetter_ctx *ctx)
1374{
Jiri Pirko85d59a82012-06-19 05:54:10 +00001375 struct team_port *port = ctx->info->port;
1376
Jiri Pirkoacd69962012-04-20 04:42:06 +00001377 if (ctx->data.bool_val)
Jiri Pirko85d59a82012-06-19 05:54:10 +00001378 team_port_enable(team, port);
Jiri Pirkoacd69962012-04-20 04:42:06 +00001379 else
Jiri Pirko85d59a82012-06-19 05:54:10 +00001380 team_port_disable(team, port);
Jiri Pirkoacd69962012-04-20 04:42:06 +00001381 return 0;
1382}
1383
Jiri Pirko71472ec2012-04-10 05:15:44 +00001384static int team_user_linkup_option_get(struct team *team,
1385 struct team_gsetter_ctx *ctx)
1386{
Jiri Pirko85d59a82012-06-19 05:54:10 +00001387 struct team_port *port = ctx->info->port;
1388
1389 ctx->data.bool_val = port->user.linkup;
Jiri Pirko71472ec2012-04-10 05:15:44 +00001390 return 0;
1391}
1392
Jiri Pirkof5e0d342013-11-28 18:01:38 +01001393static void __team_carrier_check(struct team *team);
1394
Jiri Pirko71472ec2012-04-10 05:15:44 +00001395static int team_user_linkup_option_set(struct team *team,
1396 struct team_gsetter_ctx *ctx)
1397{
Jiri Pirko85d59a82012-06-19 05:54:10 +00001398 struct team_port *port = ctx->info->port;
1399
1400 port->user.linkup = ctx->data.bool_val;
1401 team_refresh_port_linkup(port);
Jiri Pirkof5e0d342013-11-28 18:01:38 +01001402 __team_carrier_check(port->team);
Jiri Pirko71472ec2012-04-10 05:15:44 +00001403 return 0;
1404}
1405
1406static int team_user_linkup_en_option_get(struct team *team,
1407 struct team_gsetter_ctx *ctx)
1408{
Jiri Pirko85d59a82012-06-19 05:54:10 +00001409 struct team_port *port = ctx->info->port;
Jiri Pirko71472ec2012-04-10 05:15:44 +00001410
1411 ctx->data.bool_val = port->user.linkup_enabled;
1412 return 0;
1413}
1414
1415static int team_user_linkup_en_option_set(struct team *team,
1416 struct team_gsetter_ctx *ctx)
1417{
Jiri Pirko85d59a82012-06-19 05:54:10 +00001418 struct team_port *port = ctx->info->port;
Jiri Pirko71472ec2012-04-10 05:15:44 +00001419
1420 port->user.linkup_enabled = ctx->data.bool_val;
Jiri Pirko85d59a82012-06-19 05:54:10 +00001421 team_refresh_port_linkup(port);
Jiri Pirkof5e0d342013-11-28 18:01:38 +01001422 __team_carrier_check(port->team);
Jiri Pirko71472ec2012-04-10 05:15:44 +00001423 return 0;
1424}
1425
Jiri Pirkoa86fc6b2012-07-27 06:28:54 +00001426static int team_priority_option_get(struct team *team,
1427 struct team_gsetter_ctx *ctx)
1428{
1429 struct team_port *port = ctx->info->port;
1430
1431 ctx->data.s32_val = port->priority;
1432 return 0;
1433}
1434
1435static int team_priority_option_set(struct team *team,
1436 struct team_gsetter_ctx *ctx)
1437{
1438 struct team_port *port = ctx->info->port;
Jiri Pirko6c31ff32013-06-10 17:42:23 +02001439 s32 priority = ctx->data.s32_val;
Jiri Pirkoa86fc6b2012-07-27 06:28:54 +00001440
Jiri Pirko6c31ff32013-06-10 17:42:23 +02001441 if (port->priority == priority)
1442 return 0;
1443 port->priority = priority;
1444 team_queue_override_port_prio_changed(team, port);
Jiri Pirkoa86fc6b2012-07-27 06:28:54 +00001445 return 0;
1446}
1447
Jiri Pirko8ff51052012-07-27 06:28:55 +00001448static int team_queue_id_option_get(struct team *team,
1449 struct team_gsetter_ctx *ctx)
1450{
1451 struct team_port *port = ctx->info->port;
1452
1453 ctx->data.u32_val = port->queue_id;
1454 return 0;
1455}
1456
1457static int team_queue_id_option_set(struct team *team,
1458 struct team_gsetter_ctx *ctx)
1459{
1460 struct team_port *port = ctx->info->port;
Jiri Pirko6c31ff32013-06-10 17:42:23 +02001461 u16 new_queue_id = ctx->data.u32_val;
Jiri Pirko8ff51052012-07-27 06:28:55 +00001462
Jiri Pirko6c31ff32013-06-10 17:42:23 +02001463 if (port->queue_id == new_queue_id)
Jiri Pirko8ff51052012-07-27 06:28:55 +00001464 return 0;
Jiri Pirko6c31ff32013-06-10 17:42:23 +02001465 if (new_queue_id >= team->dev->real_num_tx_queues)
Jiri Pirko8ff51052012-07-27 06:28:55 +00001466 return -EINVAL;
Jiri Pirko6c31ff32013-06-10 17:42:23 +02001467 team_queue_override_port_change_queue_id(team, port, new_queue_id);
Jiri Pirko8ff51052012-07-27 06:28:55 +00001468 return 0;
1469}
1470
Jiri Pirko358b8382011-11-16 11:09:09 +00001471static const struct team_option team_options[] = {
Jiri Pirko3d249d42011-11-11 22:16:48 +00001472 {
1473 .name = "mode",
1474 .type = TEAM_OPTION_TYPE_STRING,
1475 .getter = team_mode_option_get,
1476 .setter = team_mode_option_set,
1477 },
Jiri Pirko71472ec2012-04-10 05:15:44 +00001478 {
Jiri Pirkofc423ff2013-07-20 12:13:52 +02001479 .name = "notify_peers_count",
1480 .type = TEAM_OPTION_TYPE_U32,
1481 .getter = team_notify_peers_count_get,
1482 .setter = team_notify_peers_count_set,
1483 },
1484 {
1485 .name = "notify_peers_interval",
1486 .type = TEAM_OPTION_TYPE_U32,
1487 .getter = team_notify_peers_interval_get,
1488 .setter = team_notify_peers_interval_set,
1489 },
1490 {
Jiri Pirko492b2002013-07-20 12:13:54 +02001491 .name = "mcast_rejoin_count",
1492 .type = TEAM_OPTION_TYPE_U32,
1493 .getter = team_mcast_rejoin_count_get,
1494 .setter = team_mcast_rejoin_count_set,
1495 },
1496 {
1497 .name = "mcast_rejoin_interval",
1498 .type = TEAM_OPTION_TYPE_U32,
1499 .getter = team_mcast_rejoin_interval_get,
1500 .setter = team_mcast_rejoin_interval_set,
1501 },
1502 {
Jiri Pirkoacd69962012-04-20 04:42:06 +00001503 .name = "enabled",
1504 .type = TEAM_OPTION_TYPE_BOOL,
1505 .per_port = true,
1506 .getter = team_port_en_option_get,
1507 .setter = team_port_en_option_set,
1508 },
1509 {
Jiri Pirko71472ec2012-04-10 05:15:44 +00001510 .name = "user_linkup",
1511 .type = TEAM_OPTION_TYPE_BOOL,
1512 .per_port = true,
1513 .getter = team_user_linkup_option_get,
1514 .setter = team_user_linkup_option_set,
1515 },
1516 {
1517 .name = "user_linkup_enabled",
1518 .type = TEAM_OPTION_TYPE_BOOL,
1519 .per_port = true,
1520 .getter = team_user_linkup_en_option_get,
1521 .setter = team_user_linkup_en_option_set,
1522 },
Jiri Pirkoa86fc6b2012-07-27 06:28:54 +00001523 {
1524 .name = "priority",
1525 .type = TEAM_OPTION_TYPE_S32,
1526 .per_port = true,
1527 .getter = team_priority_option_get,
1528 .setter = team_priority_option_set,
1529 },
Jiri Pirko8ff51052012-07-27 06:28:55 +00001530 {
1531 .name = "queue_id",
1532 .type = TEAM_OPTION_TYPE_U32,
1533 .per_port = true,
1534 .getter = team_queue_id_option_get,
1535 .setter = team_queue_id_option_set,
1536 },
Jiri Pirko3d249d42011-11-11 22:16:48 +00001537};
1538
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00001539static struct lock_class_key team_netdev_xmit_lock_key;
1540static struct lock_class_key team_netdev_addr_lock_key;
Eric Dumazetb3c581d2012-10-03 23:18:39 +00001541static struct lock_class_key team_tx_busylock_key;
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00001542
1543static void team_set_lockdep_class_one(struct net_device *dev,
1544 struct netdev_queue *txq,
1545 void *unused)
1546{
1547 lockdep_set_class(&txq->_xmit_lock, &team_netdev_xmit_lock_key);
1548}
1549
1550static void team_set_lockdep_class(struct net_device *dev)
1551{
1552 lockdep_set_class(&dev->addr_list_lock, &team_netdev_addr_lock_key);
1553 netdev_for_each_tx_queue(dev, team_set_lockdep_class_one, NULL);
Eric Dumazetb3c581d2012-10-03 23:18:39 +00001554 dev->qdisc_tx_busylock = &team_tx_busylock_key;
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00001555}
1556
Jiri Pirko3d249d42011-11-11 22:16:48 +00001557static int team_init(struct net_device *dev)
1558{
1559 struct team *team = netdev_priv(dev);
1560 int i;
Jiri Pirko358b8382011-11-16 11:09:09 +00001561 int err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001562
1563 team->dev = dev;
Jiri Pirko61dc3462011-11-16 11:09:08 +00001564 mutex_init(&team->lock);
Jiri Pirkod299cd52012-06-19 05:54:04 +00001565 team_set_no_mode(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001566
WANG Cong1c213bd2014-02-13 11:46:28 -08001567 team->pcpu_stats = netdev_alloc_pcpu_stats(struct team_pcpu_stats);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001568 if (!team->pcpu_stats)
1569 return -ENOMEM;
1570
1571 for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
Jiri Pirko19a0b582012-04-20 04:42:05 +00001572 INIT_HLIST_HEAD(&team->en_port_hlist[i]);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001573 INIT_LIST_HEAD(&team->port_list);
Jiri Pirko8ff51052012-07-27 06:28:55 +00001574 err = team_queue_override_init(team);
1575 if (err)
1576 goto err_team_queue_override_init;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001577
1578 team_adjust_ops(team);
1579
1580 INIT_LIST_HEAD(&team->option_list);
Jiri Pirko80f7c662012-04-10 05:15:42 +00001581 INIT_LIST_HEAD(&team->option_inst_list);
Jiri Pirkofc423ff2013-07-20 12:13:52 +02001582
1583 team_notify_peers_init(team);
Jiri Pirko492b2002013-07-20 12:13:54 +02001584 team_mcast_rejoin_init(team);
Jiri Pirkofc423ff2013-07-20 12:13:52 +02001585
Jiri Pirko358b8382011-11-16 11:09:09 +00001586 err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
1587 if (err)
1588 goto err_options_register;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001589 netif_carrier_off(dev);
1590
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00001591 team_set_lockdep_class(dev);
1592
Jiri Pirko3d249d42011-11-11 22:16:48 +00001593 return 0;
Jiri Pirko358b8382011-11-16 11:09:09 +00001594
1595err_options_register:
Jiri Pirko492b2002013-07-20 12:13:54 +02001596 team_mcast_rejoin_fini(team);
Jiri Pirkofc423ff2013-07-20 12:13:52 +02001597 team_notify_peers_fini(team);
Jiri Pirko8ff51052012-07-27 06:28:55 +00001598 team_queue_override_fini(team);
1599err_team_queue_override_init:
Jiri Pirko358b8382011-11-16 11:09:09 +00001600 free_percpu(team->pcpu_stats);
1601
1602 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001603}
1604
1605static void team_uninit(struct net_device *dev)
1606{
1607 struct team *team = netdev_priv(dev);
1608 struct team_port *port;
1609 struct team_port *tmp;
1610
Jiri Pirko61dc3462011-11-16 11:09:08 +00001611 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001612 list_for_each_entry_safe(port, tmp, &team->port_list, list)
1613 team_port_del(team, port->dev);
1614
1615 __team_change_mode(team, NULL); /* cleanup */
1616 __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
Jiri Pirko492b2002013-07-20 12:13:54 +02001617 team_mcast_rejoin_fini(team);
Jiri Pirkofc423ff2013-07-20 12:13:52 +02001618 team_notify_peers_fini(team);
Jiri Pirko8ff51052012-07-27 06:28:55 +00001619 team_queue_override_fini(team);
Jiri Pirko61dc3462011-11-16 11:09:08 +00001620 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001621}
1622
1623static void team_destructor(struct net_device *dev)
1624{
1625 struct team *team = netdev_priv(dev);
1626
1627 free_percpu(team->pcpu_stats);
1628 free_netdev(dev);
1629}
1630
1631static int team_open(struct net_device *dev)
1632{
Jiri Pirko3d249d42011-11-11 22:16:48 +00001633 return 0;
1634}
1635
1636static int team_close(struct net_device *dev)
1637{
Jiri Pirko3d249d42011-11-11 22:16:48 +00001638 return 0;
1639}
1640
1641/*
1642 * note: already called with rcu_read_lock
1643 */
1644static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
1645{
1646 struct team *team = netdev_priv(dev);
Jiri Pirko8ff51052012-07-27 06:28:55 +00001647 bool tx_success;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001648 unsigned int len = skb->len;
1649
Jiri Pirko8ff51052012-07-27 06:28:55 +00001650 tx_success = team_queue_override_transmit(team, skb);
1651 if (!tx_success)
1652 tx_success = team->ops.transmit(team, skb);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001653 if (tx_success) {
1654 struct team_pcpu_stats *pcpu_stats;
1655
1656 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
1657 u64_stats_update_begin(&pcpu_stats->syncp);
1658 pcpu_stats->tx_packets++;
1659 pcpu_stats->tx_bytes += len;
1660 u64_stats_update_end(&pcpu_stats->syncp);
1661 } else {
1662 this_cpu_inc(team->pcpu_stats->tx_dropped);
1663 }
1664
1665 return NETDEV_TX_OK;
1666}
1667
Jason Wangf663dd92014-01-10 16:18:26 +08001668static u16 team_select_queue(struct net_device *dev, struct sk_buff *skb,
Daniel Borkmann99932d42014-02-16 15:55:20 +01001669 void *accel_priv, select_queue_fallback_t fallback)
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00001670{
1671 /*
1672 * This helper function exists to help dev_pick_tx get the correct
1673 * destination queue. Using a helper function skips a call to
1674 * skb_tx_hash and will put the skbs in the queue we expect on their
1675 * way down to the team driver.
1676 */
1677 u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
1678
1679 /*
1680 * Save the original txq to restore before passing to the driver
1681 */
1682 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
1683
1684 if (unlikely(txq >= dev->real_num_tx_queues)) {
1685 do {
1686 txq -= dev->real_num_tx_queues;
1687 } while (txq >= dev->real_num_tx_queues);
1688 }
1689 return txq;
1690}
1691
Jiri Pirko3d249d42011-11-11 22:16:48 +00001692static void team_change_rx_flags(struct net_device *dev, int change)
1693{
1694 struct team *team = netdev_priv(dev);
1695 struct team_port *port;
1696 int inc;
1697
1698 rcu_read_lock();
1699 list_for_each_entry_rcu(port, &team->port_list, list) {
1700 if (change & IFF_PROMISC) {
1701 inc = dev->flags & IFF_PROMISC ? 1 : -1;
1702 dev_set_promiscuity(port->dev, inc);
1703 }
1704 if (change & IFF_ALLMULTI) {
1705 inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
1706 dev_set_allmulti(port->dev, inc);
1707 }
1708 }
1709 rcu_read_unlock();
1710}
1711
1712static void team_set_rx_mode(struct net_device *dev)
1713{
1714 struct team *team = netdev_priv(dev);
1715 struct team_port *port;
1716
1717 rcu_read_lock();
1718 list_for_each_entry_rcu(port, &team->port_list, list) {
Vlad Yasevich72b27032013-04-15 09:54:26 +00001719 dev_uc_sync_multiple(port->dev, dev);
1720 dev_mc_sync_multiple(port->dev, dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001721 }
1722 rcu_read_unlock();
1723}
1724
1725static int team_set_mac_address(struct net_device *dev, void *p)
1726{
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001727 struct sockaddr *addr = p;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001728 struct team *team = netdev_priv(dev);
1729 struct team_port *port;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001730
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001731 if (dev->type == ARPHRD_ETHER && !is_valid_ether_addr(addr->sa_data))
1732 return -EADDRNOTAVAIL;
1733 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
Jiri Pirko9215f432015-03-04 08:36:31 +01001734 mutex_lock(&team->lock);
1735 list_for_each_entry(port, &team->port_list, list)
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001736 if (team->ops.port_change_dev_addr)
1737 team->ops.port_change_dev_addr(team, port);
Jiri Pirko9215f432015-03-04 08:36:31 +01001738 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001739 return 0;
1740}
1741
1742static int team_change_mtu(struct net_device *dev, int new_mtu)
1743{
1744 struct team *team = netdev_priv(dev);
1745 struct team_port *port;
1746 int err;
1747
1748 /*
1749 * Alhough this is reader, it's guarded by team lock. It's not possible
1750 * to traverse list in reverse under rcu_read_lock
1751 */
Jiri Pirko61dc3462011-11-16 11:09:08 +00001752 mutex_lock(&team->lock);
Jiri Pirko9d0d68f2014-05-29 20:46:17 +02001753 team->port_mtu_change_allowed = true;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001754 list_for_each_entry(port, &team->port_list, list) {
1755 err = dev_set_mtu(port->dev, new_mtu);
1756 if (err) {
1757 netdev_err(dev, "Device %s failed to change mtu",
1758 port->dev->name);
1759 goto unwind;
1760 }
1761 }
Jiri Pirko9d0d68f2014-05-29 20:46:17 +02001762 team->port_mtu_change_allowed = false;
Jiri Pirko61dc3462011-11-16 11:09:08 +00001763 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001764
1765 dev->mtu = new_mtu;
1766
1767 return 0;
1768
1769unwind:
1770 list_for_each_entry_continue_reverse(port, &team->port_list, list)
1771 dev_set_mtu(port->dev, dev->mtu);
Jiri Pirko9d0d68f2014-05-29 20:46:17 +02001772 team->port_mtu_change_allowed = false;
Jiri Pirko61dc3462011-11-16 11:09:08 +00001773 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001774
1775 return err;
1776}
1777
1778static struct rtnl_link_stats64 *
1779team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1780{
1781 struct team *team = netdev_priv(dev);
1782 struct team_pcpu_stats *p;
1783 u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
1784 u32 rx_dropped = 0, tx_dropped = 0;
1785 unsigned int start;
1786 int i;
1787
1788 for_each_possible_cpu(i) {
1789 p = per_cpu_ptr(team->pcpu_stats, i);
1790 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07001791 start = u64_stats_fetch_begin_irq(&p->syncp);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001792 rx_packets = p->rx_packets;
1793 rx_bytes = p->rx_bytes;
1794 rx_multicast = p->rx_multicast;
1795 tx_packets = p->tx_packets;
1796 tx_bytes = p->tx_bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07001797 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Jiri Pirko3d249d42011-11-11 22:16:48 +00001798
1799 stats->rx_packets += rx_packets;
1800 stats->rx_bytes += rx_bytes;
1801 stats->multicast += rx_multicast;
1802 stats->tx_packets += tx_packets;
1803 stats->tx_bytes += tx_bytes;
1804 /*
1805 * rx_dropped & tx_dropped are u32, updated
1806 * without syncp protection.
1807 */
1808 rx_dropped += p->rx_dropped;
1809 tx_dropped += p->tx_dropped;
1810 }
1811 stats->rx_dropped = rx_dropped;
1812 stats->tx_dropped = tx_dropped;
1813 return stats;
1814}
1815
Patrick McHardy80d5c362013-04-19 02:04:28 +00001816static int team_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001817{
1818 struct team *team = netdev_priv(dev);
1819 struct team_port *port;
Jiri Pirko87002b02011-12-08 04:11:17 +00001820 int err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001821
Jiri Pirko87002b02011-12-08 04:11:17 +00001822 /*
1823 * Alhough this is reader, it's guarded by team lock. It's not possible
1824 * to traverse list in reverse under rcu_read_lock
1825 */
1826 mutex_lock(&team->lock);
1827 list_for_each_entry(port, &team->port_list, list) {
Patrick McHardy80d5c362013-04-19 02:04:28 +00001828 err = vlan_vid_add(port->dev, proto, vid);
Jiri Pirko87002b02011-12-08 04:11:17 +00001829 if (err)
1830 goto unwind;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001831 }
Jiri Pirko87002b02011-12-08 04:11:17 +00001832 mutex_unlock(&team->lock);
Jiri Pirko8e586132011-12-08 19:52:37 -05001833
1834 return 0;
Jiri Pirko87002b02011-12-08 04:11:17 +00001835
1836unwind:
1837 list_for_each_entry_continue_reverse(port, &team->port_list, list)
Patrick McHardy80d5c362013-04-19 02:04:28 +00001838 vlan_vid_del(port->dev, proto, vid);
Jiri Pirko87002b02011-12-08 04:11:17 +00001839 mutex_unlock(&team->lock);
1840
1841 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001842}
1843
Patrick McHardy80d5c362013-04-19 02:04:28 +00001844static int team_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001845{
1846 struct team *team = netdev_priv(dev);
1847 struct team_port *port;
1848
1849 rcu_read_lock();
Jiri Pirko87002b02011-12-08 04:11:17 +00001850 list_for_each_entry_rcu(port, &team->port_list, list)
Patrick McHardy80d5c362013-04-19 02:04:28 +00001851 vlan_vid_del(port->dev, proto, vid);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001852 rcu_read_unlock();
Jiri Pirko8e586132011-12-08 19:52:37 -05001853
1854 return 0;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001855}
1856
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001857#ifdef CONFIG_NET_POLL_CONTROLLER
1858static void team_poll_controller(struct net_device *dev)
1859{
1860}
1861
1862static void __team_netpoll_cleanup(struct team *team)
1863{
1864 struct team_port *port;
1865
1866 list_for_each_entry(port, &team->port_list, list)
1867 team_port_disable_netpoll(port);
1868}
1869
1870static void team_netpoll_cleanup(struct net_device *dev)
1871{
1872 struct team *team = netdev_priv(dev);
1873
1874 mutex_lock(&team->lock);
1875 __team_netpoll_cleanup(team);
1876 mutex_unlock(&team->lock);
1877}
1878
1879static int team_netpoll_setup(struct net_device *dev,
Eric W. Biedermana8779ec2014-03-27 15:36:38 -07001880 struct netpoll_info *npifo)
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001881{
1882 struct team *team = netdev_priv(dev);
1883 struct team_port *port;
Jiri Pirko3324d022012-07-24 01:20:48 +00001884 int err = 0;
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001885
1886 mutex_lock(&team->lock);
1887 list_for_each_entry(port, &team->port_list, list) {
Eric W. Biedermana8779ec2014-03-27 15:36:38 -07001888 err = team_port_enable_netpoll(team, port);
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001889 if (err) {
1890 __team_netpoll_cleanup(team);
1891 break;
1892 }
1893 }
1894 mutex_unlock(&team->lock);
1895 return err;
1896}
1897#endif
1898
Jiri Pirko3d249d42011-11-11 22:16:48 +00001899static int team_add_slave(struct net_device *dev, struct net_device *port_dev)
1900{
1901 struct team *team = netdev_priv(dev);
1902 int err;
1903
Jiri Pirko61dc3462011-11-16 11:09:08 +00001904 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001905 err = team_port_add(team, port_dev);
Jiri Pirko61dc3462011-11-16 11:09:08 +00001906 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001907 return err;
1908}
1909
1910static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
1911{
1912 struct team *team = netdev_priv(dev);
1913 int err;
1914
Jiri Pirko61dc3462011-11-16 11:09:08 +00001915 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001916 err = team_port_del(team, port_dev);
Jiri Pirko61dc3462011-11-16 11:09:08 +00001917 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001918 return err;
1919}
1920
Jiri Pirko234a8fd2011-11-17 04:16:04 +00001921static netdev_features_t team_fix_features(struct net_device *dev,
1922 netdev_features_t features)
1923{
1924 struct team_port *port;
1925 struct team *team = netdev_priv(dev);
1926 netdev_features_t mask;
1927
Scott Feldman7889cbe2015-05-10 09:48:07 -07001928 mask = features;
Jiri Pirko234a8fd2011-11-17 04:16:04 +00001929 features &= ~NETIF_F_ONE_FOR_ALL;
1930 features |= NETIF_F_ALL_FOR_ALL;
1931
1932 rcu_read_lock();
1933 list_for_each_entry_rcu(port, &team->port_list, list) {
1934 features = netdev_increment_features(features,
1935 port->dev->features,
1936 mask);
1937 }
1938 rcu_read_unlock();
Jiri Pirko247f6d02015-02-25 19:52:11 +01001939
1940 features = netdev_add_tso_features(features, mask);
1941
Jiri Pirko234a8fd2011-11-17 04:16:04 +00001942 return features;
1943}
1944
Flavio Leitner4cafe372012-12-29 15:31:01 +00001945static int team_change_carrier(struct net_device *dev, bool new_carrier)
1946{
Flavio Leitnere1854832013-02-05 09:30:55 +00001947 struct team *team = netdev_priv(dev);
1948
1949 team->user_carrier_enabled = true;
1950
Flavio Leitner4cafe372012-12-29 15:31:01 +00001951 if (new_carrier)
1952 netif_carrier_on(dev);
1953 else
1954 netif_carrier_off(dev);
1955 return 0;
1956}
1957
Jiri Pirko3d249d42011-11-11 22:16:48 +00001958static const struct net_device_ops team_netdev_ops = {
1959 .ndo_init = team_init,
1960 .ndo_uninit = team_uninit,
1961 .ndo_open = team_open,
1962 .ndo_stop = team_close,
1963 .ndo_start_xmit = team_xmit,
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00001964 .ndo_select_queue = team_select_queue,
Jiri Pirko3d249d42011-11-11 22:16:48 +00001965 .ndo_change_rx_flags = team_change_rx_flags,
1966 .ndo_set_rx_mode = team_set_rx_mode,
1967 .ndo_set_mac_address = team_set_mac_address,
1968 .ndo_change_mtu = team_change_mtu,
1969 .ndo_get_stats64 = team_get_stats64,
1970 .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid,
1971 .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid,
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001972#ifdef CONFIG_NET_POLL_CONTROLLER
1973 .ndo_poll_controller = team_poll_controller,
1974 .ndo_netpoll_setup = team_netpoll_setup,
1975 .ndo_netpoll_cleanup = team_netpoll_cleanup,
1976#endif
Jiri Pirko3d249d42011-11-11 22:16:48 +00001977 .ndo_add_slave = team_add_slave,
1978 .ndo_del_slave = team_del_slave,
Jiri Pirko234a8fd2011-11-17 04:16:04 +00001979 .ndo_fix_features = team_fix_features,
Flavio Leitner4cafe372012-12-29 15:31:01 +00001980 .ndo_change_carrier = team_change_carrier,
Scott Feldmanfc8f40d2015-05-10 09:47:57 -07001981 .ndo_bridge_setlink = switchdev_port_bridge_setlink,
Scott Feldman85fdb952015-05-10 09:48:05 -07001982 .ndo_bridge_getlink = switchdev_port_bridge_getlink,
Scott Feldman54ba5a02015-05-10 09:48:01 -07001983 .ndo_bridge_dellink = switchdev_port_bridge_dellink,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001984 .ndo_fdb_add = switchdev_port_fdb_add,
1985 .ndo_fdb_del = switchdev_port_fdb_del,
1986 .ndo_fdb_dump = switchdev_port_fdb_dump,
Toshiaki Makitab9f4cf752015-03-27 14:31:15 +09001987 .ndo_features_check = passthru_features_check,
Jiri Pirko3d249d42011-11-11 22:16:48 +00001988};
1989
Flavio Leitner7f51c582012-12-29 16:37:33 +00001990/***********************
1991 * ethtool interface
1992 ***********************/
1993
1994static void team_ethtool_get_drvinfo(struct net_device *dev,
1995 struct ethtool_drvinfo *drvinfo)
1996{
Flavio Leitner3066af52013-01-05 02:53:10 +00001997 strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
1998 strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
Flavio Leitner7f51c582012-12-29 16:37:33 +00001999}
2000
2001static const struct ethtool_ops team_ethtool_ops = {
2002 .get_drvinfo = team_ethtool_get_drvinfo,
2003 .get_link = ethtool_op_get_link,
2004};
Jiri Pirko3d249d42011-11-11 22:16:48 +00002005
2006/***********************
2007 * rt netlink interface
2008 ***********************/
2009
Jiri Pirko1d76efe2012-08-17 04:00:48 +00002010static void team_setup_by_port(struct net_device *dev,
2011 struct net_device *port_dev)
2012{
2013 dev->header_ops = port_dev->header_ops;
2014 dev->type = port_dev->type;
2015 dev->hard_header_len = port_dev->hard_header_len;
2016 dev->addr_len = port_dev->addr_len;
2017 dev->mtu = port_dev->mtu;
2018 memcpy(dev->broadcast, port_dev->broadcast, port_dev->addr_len);
Bjørn Mork93af7352013-08-30 18:08:48 +02002019 eth_hw_addr_inherit(dev, port_dev);
Jiri Pirko1d76efe2012-08-17 04:00:48 +00002020}
2021
2022static int team_dev_type_check_change(struct net_device *dev,
2023 struct net_device *port_dev)
2024{
2025 struct team *team = netdev_priv(dev);
2026 char *portname = port_dev->name;
2027 int err;
2028
2029 if (dev->type == port_dev->type)
2030 return 0;
2031 if (!list_empty(&team->port_list)) {
2032 netdev_err(dev, "Device %s is of different type\n", portname);
2033 return -EBUSY;
2034 }
2035 err = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE, dev);
2036 err = notifier_to_errno(err);
2037 if (err) {
2038 netdev_err(dev, "Refused to change device type\n");
2039 return err;
2040 }
2041 dev_uc_flush(dev);
2042 dev_mc_flush(dev);
2043 team_setup_by_port(dev, port_dev);
2044 call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
2045 return 0;
2046}
2047
Jiri Pirko3d249d42011-11-11 22:16:48 +00002048static void team_setup(struct net_device *dev)
2049{
2050 ether_setup(dev);
2051
2052 dev->netdev_ops = &team_netdev_ops;
Flavio Leitner7f51c582012-12-29 16:37:33 +00002053 dev->ethtool_ops = &team_ethtool_ops;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002054 dev->destructor = team_destructor;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002055 dev->flags |= IFF_MULTICAST;
2056 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
Phil Sutter22e380a2015-08-18 10:30:34 +02002057 dev->priv_flags |= IFF_NO_QUEUE;
Jiri Pirkoc981e422015-12-03 12:12:06 +01002058 dev->priv_flags |= IFF_TEAM;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002059
2060 /*
2061 * Indicate we support unicast address filtering. That way core won't
2062 * bring us to promisc mode in case a unicast addr is added.
2063 * Let this up to underlay drivers.
2064 */
Jiri Pirko5a1d1c82012-06-29 05:10:07 +00002065 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002066
2067 dev->features |= NETIF_F_LLTX;
2068 dev->features |= NETIF_F_GRO;
Weilong Chen99301ba2014-01-23 11:07:22 +08002069
2070 /* Don't allow team devices to change network namespaces. */
2071 dev->features |= NETIF_F_NETNS_LOCAL;
2072
Jiri Pirko3ed71472012-11-28 06:13:10 +00002073 dev->hw_features = TEAM_VLAN_FEATURES |
Patrick McHardyf6469682013-04-19 02:04:27 +00002074 NETIF_F_HW_VLAN_CTAG_TX |
2075 NETIF_F_HW_VLAN_CTAG_RX |
2076 NETIF_F_HW_VLAN_CTAG_FILTER;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002077
Jiri Pirko3ed71472012-11-28 06:13:10 +00002078 dev->hw_features &= ~(NETIF_F_ALL_CSUM & ~NETIF_F_HW_CSUM);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002079 dev->features |= dev->hw_features;
2080}
2081
2082static int team_newlink(struct net *src_net, struct net_device *dev,
2083 struct nlattr *tb[], struct nlattr *data[])
2084{
Jiri Pirko3d249d42011-11-11 22:16:48 +00002085 if (tb[IFLA_ADDRESS] == NULL)
Danny Kukawka7ce5d222012-02-15 06:45:40 +00002086 eth_hw_addr_random(dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002087
Toshiaki Makitaff204cc2014-08-05 15:58:50 +09002088 return register_netdevice(dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002089}
2090
2091static int team_validate(struct nlattr *tb[], struct nlattr *data[])
2092{
2093 if (tb[IFLA_ADDRESS]) {
2094 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
2095 return -EINVAL;
2096 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
2097 return -EADDRNOTAVAIL;
2098 }
2099 return 0;
2100}
2101
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00002102static unsigned int team_get_num_tx_queues(void)
2103{
2104 return TEAM_DEFAULT_NUM_TX_QUEUES;
2105}
2106
2107static unsigned int team_get_num_rx_queues(void)
2108{
2109 return TEAM_DEFAULT_NUM_RX_QUEUES;
2110}
2111
Jiri Pirko3d249d42011-11-11 22:16:48 +00002112static struct rtnl_link_ops team_link_ops __read_mostly = {
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00002113 .kind = DRV_NAME,
2114 .priv_size = sizeof(struct team),
2115 .setup = team_setup,
2116 .newlink = team_newlink,
2117 .validate = team_validate,
2118 .get_num_tx_queues = team_get_num_tx_queues,
2119 .get_num_rx_queues = team_get_num_rx_queues,
Jiri Pirko3d249d42011-11-11 22:16:48 +00002120};
2121
2122
2123/***********************************
2124 * Generic netlink custom interface
2125 ***********************************/
2126
2127static struct genl_family team_nl_family = {
2128 .id = GENL_ID_GENERATE,
2129 .name = TEAM_GENL_NAME,
2130 .version = TEAM_GENL_VERSION,
2131 .maxattr = TEAM_ATTR_MAX,
2132 .netnsok = true,
2133};
2134
2135static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
2136 [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, },
2137 [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 },
2138 [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED },
2139 [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED },
2140};
2141
2142static const struct nla_policy
2143team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
2144 [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
2145 [TEAM_ATTR_OPTION_NAME] = {
2146 .type = NLA_STRING,
2147 .len = TEAM_STRING_MAX_LEN,
2148 },
2149 [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG },
2150 [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 },
Jiri Pirko26155982012-04-04 12:16:26 +00002151 [TEAM_ATTR_OPTION_DATA] = { .type = NLA_BINARY },
Jiri Pirko3d249d42011-11-11 22:16:48 +00002152};
2153
2154static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
2155{
2156 struct sk_buff *msg;
2157 void *hdr;
2158 int err;
2159
Thomas Graf58050fc2012-06-28 03:57:45 +00002160 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002161 if (!msg)
2162 return -ENOMEM;
2163
Eric W. Biederman15e47302012-09-07 20:12:54 +00002164 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
Jiri Pirko3d249d42011-11-11 22:16:48 +00002165 &team_nl_family, 0, TEAM_CMD_NOOP);
Wei Yongjuna326e6d2012-09-24 18:29:35 +00002166 if (!hdr) {
2167 err = -EMSGSIZE;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002168 goto err_msg_put;
2169 }
2170
2171 genlmsg_end(msg, hdr);
2172
Eric W. Biederman15e47302012-09-07 20:12:54 +00002173 return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002174
2175err_msg_put:
2176 nlmsg_free(msg);
2177
2178 return err;
2179}
2180
2181/*
2182 * Netlink cmd functions should be locked by following two functions.
Jiri Pirko8c0713a2011-11-16 11:55:54 +00002183 * Since dev gets held here, that ensures dev won't disappear in between.
Jiri Pirko3d249d42011-11-11 22:16:48 +00002184 */
2185static struct team *team_nl_team_get(struct genl_info *info)
2186{
2187 struct net *net = genl_info_net(info);
2188 int ifindex;
2189 struct net_device *dev;
2190 struct team *team;
2191
2192 if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
2193 return NULL;
2194
2195 ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
Jiri Pirko8c0713a2011-11-16 11:55:54 +00002196 dev = dev_get_by_index(net, ifindex);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002197 if (!dev || dev->netdev_ops != &team_netdev_ops) {
Jiri Pirko8c0713a2011-11-16 11:55:54 +00002198 if (dev)
2199 dev_put(dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002200 return NULL;
2201 }
2202
2203 team = netdev_priv(dev);
Jiri Pirko61dc3462011-11-16 11:09:08 +00002204 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002205 return team;
2206}
2207
2208static void team_nl_team_put(struct team *team)
2209{
Jiri Pirko61dc3462011-11-16 11:09:08 +00002210 mutex_unlock(&team->lock);
Jiri Pirko8c0713a2011-11-16 11:55:54 +00002211 dev_put(team->dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002212}
2213
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002214typedef int team_nl_send_func_t(struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002215 struct team *team, u32 portid);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002216
Eric W. Biederman15e47302012-09-07 20:12:54 +00002217static int team_nl_send_unicast(struct sk_buff *skb, struct team *team, u32 portid)
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002218{
Eric W. Biederman15e47302012-09-07 20:12:54 +00002219 return genlmsg_unicast(dev_net(team->dev), skb, portid);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002220}
2221
Jiri Pirko01048d92012-06-19 05:54:14 +00002222static int team_nl_fill_one_option_get(struct sk_buff *skb, struct team *team,
2223 struct team_option_inst *opt_inst)
2224{
2225 struct nlattr *option_item;
2226 struct team_option *option = opt_inst->option;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002227 struct team_option_inst_info *opt_inst_info = &opt_inst->info;
Jiri Pirko01048d92012-06-19 05:54:14 +00002228 struct team_gsetter_ctx ctx;
2229 int err;
2230
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002231 ctx.info = opt_inst_info;
2232 err = team_option_get(team, opt_inst, &ctx);
2233 if (err)
2234 return err;
2235
Jiri Pirko01048d92012-06-19 05:54:14 +00002236 option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
2237 if (!option_item)
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002238 return -EMSGSIZE;
Jiri Pirko01048d92012-06-19 05:54:14 +00002239
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002240 if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name))
2241 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002242 if (opt_inst_info->port &&
2243 nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX,
2244 opt_inst_info->port->dev->ifindex))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002245 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002246 if (opt_inst->option->array_size &&
2247 nla_put_u32(skb, TEAM_ATTR_OPTION_ARRAY_INDEX,
2248 opt_inst_info->array_index))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002249 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002250
2251 switch (option->type) {
2252 case TEAM_OPTION_TYPE_U32:
2253 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002254 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002255 if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.u32_val))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002256 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002257 break;
2258 case TEAM_OPTION_TYPE_STRING:
2259 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002260 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002261 if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA,
2262 ctx.data.str_val))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002263 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002264 break;
2265 case TEAM_OPTION_TYPE_BINARY:
2266 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002267 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002268 if (nla_put(skb, TEAM_ATTR_OPTION_DATA, ctx.data.bin_val.len,
2269 ctx.data.bin_val.ptr))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002270 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002271 break;
2272 case TEAM_OPTION_TYPE_BOOL:
2273 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002274 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002275 if (ctx.data.bool_val &&
2276 nla_put_flag(skb, TEAM_ATTR_OPTION_DATA))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002277 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002278 break;
Jiri Pirko69821632012-07-27 06:28:53 +00002279 case TEAM_OPTION_TYPE_S32:
2280 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_S32))
2281 goto nest_cancel;
2282 if (nla_put_s32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.s32_val))
2283 goto nest_cancel;
2284 break;
Jiri Pirko01048d92012-06-19 05:54:14 +00002285 default:
2286 BUG();
2287 }
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002288 if (opt_inst->removed && nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED))
2289 goto nest_cancel;
2290 if (opt_inst->changed) {
2291 if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED))
2292 goto nest_cancel;
2293 opt_inst->changed = false;
2294 }
Jiri Pirko01048d92012-06-19 05:54:14 +00002295 nla_nest_end(skb, option_item);
2296 return 0;
2297
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002298nest_cancel:
2299 nla_nest_cancel(skb, option_item);
2300 return -EMSGSIZE;
Jiri Pirko01048d92012-06-19 05:54:14 +00002301}
2302
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002303static int __send_and_alloc_skb(struct sk_buff **pskb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002304 struct team *team, u32 portid,
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002305 team_nl_send_func_t *send_func)
2306{
2307 int err;
2308
2309 if (*pskb) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002310 err = send_func(*pskb, team, portid);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002311 if (err)
2312 return err;
2313 }
Thomas Graf58050fc2012-06-28 03:57:45 +00002314 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002315 if (!*pskb)
2316 return -ENOMEM;
2317 return 0;
2318}
2319
Eric W. Biederman15e47302012-09-07 20:12:54 +00002320static int team_nl_send_options_get(struct team *team, u32 portid, u32 seq,
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002321 int flags, team_nl_send_func_t *send_func,
Jiri Pirko01048d92012-06-19 05:54:14 +00002322 struct list_head *sel_opt_inst_list)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002323{
2324 struct nlattr *option_list;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002325 struct nlmsghdr *nlh;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002326 void *hdr;
Jiri Pirko80f7c662012-04-10 05:15:42 +00002327 struct team_option_inst *opt_inst;
2328 int err;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002329 struct sk_buff *skb = NULL;
2330 bool incomplete;
2331 int i;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002332
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002333 opt_inst = list_first_entry(sel_opt_inst_list,
2334 struct team_option_inst, tmp_list);
2335
2336start_again:
Eric W. Biederman15e47302012-09-07 20:12:54 +00002337 err = __send_and_alloc_skb(&skb, team, portid, send_func);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002338 if (err)
2339 return err;
2340
Eric W. Biederman15e47302012-09-07 20:12:54 +00002341 hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
Jiri Pirko3d249d42011-11-11 22:16:48 +00002342 TEAM_CMD_OPTIONS_GET);
Wei Yongjuna326e6d2012-09-24 18:29:35 +00002343 if (!hdr)
2344 return -EMSGSIZE;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002345
David S. Miller86ebb022012-04-01 20:25:18 -04002346 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2347 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002348 option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
2349 if (!option_list)
Jiri Pirko75db9862012-06-19 05:54:12 +00002350 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002351
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002352 i = 0;
2353 incomplete = false;
2354 list_for_each_entry_from(opt_inst, sel_opt_inst_list, tmp_list) {
Jiri Pirko01048d92012-06-19 05:54:14 +00002355 err = team_nl_fill_one_option_get(skb, team, opt_inst);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002356 if (err) {
2357 if (err == -EMSGSIZE) {
2358 if (!i)
2359 goto errout;
2360 incomplete = true;
2361 break;
2362 }
Jiri Pirko01048d92012-06-19 05:54:14 +00002363 goto errout;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002364 }
2365 i++;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002366 }
2367
2368 nla_nest_end(skb, option_list);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002369 genlmsg_end(skb, hdr);
2370 if (incomplete)
2371 goto start_again;
2372
2373send_done:
Eric W. Biederman15e47302012-09-07 20:12:54 +00002374 nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002375 if (!nlh) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002376 err = __send_and_alloc_skb(&skb, team, portid, send_func);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002377 if (err)
2378 goto errout;
2379 goto send_done;
2380 }
2381
Eric W. Biederman15e47302012-09-07 20:12:54 +00002382 return send_func(skb, team, portid);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002383
2384nla_put_failure:
Jiri Pirko80f7c662012-04-10 05:15:42 +00002385 err = -EMSGSIZE;
2386errout:
Jiri Pirko3d249d42011-11-11 22:16:48 +00002387 genlmsg_cancel(skb, hdr);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002388 nlmsg_free(skb);
Jiri Pirko80f7c662012-04-10 05:15:42 +00002389 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002390}
2391
Jiri Pirko3d249d42011-11-11 22:16:48 +00002392static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
2393{
2394 struct team *team;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002395 struct team_option_inst *opt_inst;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002396 int err;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002397 LIST_HEAD(sel_opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002398
2399 team = team_nl_team_get(info);
2400 if (!team)
2401 return -EINVAL;
2402
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002403 list_for_each_entry(opt_inst, &team->option_inst_list, list)
2404 list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
Eric W. Biederman15e47302012-09-07 20:12:54 +00002405 err = team_nl_send_options_get(team, info->snd_portid, info->snd_seq,
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002406 NLM_F_ACK, team_nl_send_unicast,
2407 &sel_opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002408
2409 team_nl_team_put(team);
2410
2411 return err;
2412}
2413
Jiri Pirko2fcdb2c2012-06-19 05:54:20 +00002414static int team_nl_send_event_options_get(struct team *team,
2415 struct list_head *sel_opt_inst_list);
2416
Jiri Pirko3d249d42011-11-11 22:16:48 +00002417static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
2418{
2419 struct team *team;
2420 int err = 0;
2421 int i;
2422 struct nlattr *nl_option;
Jiri Pirko2fcdb2c2012-06-19 05:54:20 +00002423 LIST_HEAD(opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002424
Jiri Pirko7d259502015-12-03 12:12:17 +01002425 rtnl_lock();
2426
Jiri Pirko3d249d42011-11-11 22:16:48 +00002427 team = team_nl_team_get(info);
Jiri Pirko7d259502015-12-03 12:12:17 +01002428 if (!team) {
2429 err = -EINVAL;
2430 goto rtnl_unlock;
2431 }
Jiri Pirko3d249d42011-11-11 22:16:48 +00002432
2433 err = -EINVAL;
2434 if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
2435 err = -EINVAL;
2436 goto team_put;
2437 }
2438
2439 nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
Jiri Pirko80f7c662012-04-10 05:15:42 +00002440 struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1];
Jiri Pirkob1303322012-06-19 05:54:08 +00002441 struct nlattr *attr;
Jiri Pirko14f066b2012-04-10 05:15:43 +00002442 struct nlattr *attr_data;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002443 enum team_option_type opt_type;
Jiri Pirko80f7c662012-04-10 05:15:42 +00002444 int opt_port_ifindex = 0; /* != 0 for per-port options */
Jiri Pirkob1303322012-06-19 05:54:08 +00002445 u32 opt_array_index = 0;
2446 bool opt_is_array = false;
Jiri Pirko80f7c662012-04-10 05:15:42 +00002447 struct team_option_inst *opt_inst;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002448 char *opt_name;
2449 bool opt_found = false;
2450
2451 if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
2452 err = -EINVAL;
2453 goto team_put;
2454 }
Jiri Pirko80f7c662012-04-10 05:15:42 +00002455 err = nla_parse_nested(opt_attrs, TEAM_ATTR_OPTION_MAX,
Jiri Pirko3d249d42011-11-11 22:16:48 +00002456 nl_option, team_nl_option_policy);
2457 if (err)
2458 goto team_put;
Jiri Pirko80f7c662012-04-10 05:15:42 +00002459 if (!opt_attrs[TEAM_ATTR_OPTION_NAME] ||
Jiri Pirko14f066b2012-04-10 05:15:43 +00002460 !opt_attrs[TEAM_ATTR_OPTION_TYPE]) {
Jiri Pirko3d249d42011-11-11 22:16:48 +00002461 err = -EINVAL;
2462 goto team_put;
2463 }
Jiri Pirko80f7c662012-04-10 05:15:42 +00002464 switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) {
Jiri Pirko3d249d42011-11-11 22:16:48 +00002465 case NLA_U32:
2466 opt_type = TEAM_OPTION_TYPE_U32;
2467 break;
2468 case NLA_STRING:
2469 opt_type = TEAM_OPTION_TYPE_STRING;
2470 break;
Jiri Pirko26155982012-04-04 12:16:26 +00002471 case NLA_BINARY:
2472 opt_type = TEAM_OPTION_TYPE_BINARY;
2473 break;
Jiri Pirko14f066b2012-04-10 05:15:43 +00002474 case NLA_FLAG:
2475 opt_type = TEAM_OPTION_TYPE_BOOL;
2476 break;
Jiri Pirko69821632012-07-27 06:28:53 +00002477 case NLA_S32:
2478 opt_type = TEAM_OPTION_TYPE_S32;
2479 break;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002480 default:
2481 goto team_put;
2482 }
2483
Jiri Pirko14f066b2012-04-10 05:15:43 +00002484 attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA];
2485 if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) {
2486 err = -EINVAL;
2487 goto team_put;
2488 }
2489
Jiri Pirko80f7c662012-04-10 05:15:42 +00002490 opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]);
Jiri Pirkob1303322012-06-19 05:54:08 +00002491 attr = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX];
2492 if (attr)
2493 opt_port_ifindex = nla_get_u32(attr);
2494
2495 attr = opt_attrs[TEAM_ATTR_OPTION_ARRAY_INDEX];
2496 if (attr) {
2497 opt_is_array = true;
2498 opt_array_index = nla_get_u32(attr);
2499 }
Jiri Pirko3d249d42011-11-11 22:16:48 +00002500
Jiri Pirko80f7c662012-04-10 05:15:42 +00002501 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2502 struct team_option *option = opt_inst->option;
Jiri Pirko80f7c662012-04-10 05:15:42 +00002503 struct team_gsetter_ctx ctx;
Jiri Pirko85d59a82012-06-19 05:54:10 +00002504 struct team_option_inst_info *opt_inst_info;
Jiri Pirko80f7c662012-04-10 05:15:42 +00002505 int tmp_ifindex;
2506
Jiri Pirko85d59a82012-06-19 05:54:10 +00002507 opt_inst_info = &opt_inst->info;
2508 tmp_ifindex = opt_inst_info->port ?
2509 opt_inst_info->port->dev->ifindex : 0;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002510 if (option->type != opt_type ||
Jiri Pirko80f7c662012-04-10 05:15:42 +00002511 strcmp(option->name, opt_name) ||
Jiri Pirkob1303322012-06-19 05:54:08 +00002512 tmp_ifindex != opt_port_ifindex ||
2513 (option->array_size && !opt_is_array) ||
Jiri Pirko85d59a82012-06-19 05:54:10 +00002514 opt_inst_info->array_index != opt_array_index)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002515 continue;
2516 opt_found = true;
Jiri Pirko85d59a82012-06-19 05:54:10 +00002517 ctx.info = opt_inst_info;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002518 switch (opt_type) {
2519 case TEAM_OPTION_TYPE_U32:
Jiri Pirko14f066b2012-04-10 05:15:43 +00002520 ctx.data.u32_val = nla_get_u32(attr_data);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002521 break;
2522 case TEAM_OPTION_TYPE_STRING:
Jiri Pirko14f066b2012-04-10 05:15:43 +00002523 if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) {
Jiri Pirko26155982012-04-04 12:16:26 +00002524 err = -EINVAL;
2525 goto team_put;
2526 }
Jiri Pirko14f066b2012-04-10 05:15:43 +00002527 ctx.data.str_val = nla_data(attr_data);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002528 break;
Jiri Pirko26155982012-04-04 12:16:26 +00002529 case TEAM_OPTION_TYPE_BINARY:
Jiri Pirko14f066b2012-04-10 05:15:43 +00002530 ctx.data.bin_val.len = nla_len(attr_data);
2531 ctx.data.bin_val.ptr = nla_data(attr_data);
2532 break;
2533 case TEAM_OPTION_TYPE_BOOL:
2534 ctx.data.bool_val = attr_data ? true : false;
Jiri Pirko26155982012-04-04 12:16:26 +00002535 break;
Jiri Pirko69821632012-07-27 06:28:53 +00002536 case TEAM_OPTION_TYPE_S32:
2537 ctx.data.s32_val = nla_get_s32(attr_data);
2538 break;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002539 default:
2540 BUG();
2541 }
Jiri Pirko80f7c662012-04-10 05:15:42 +00002542 err = team_option_set(team, opt_inst, &ctx);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002543 if (err)
2544 goto team_put;
Jiri Pirko2fcdb2c2012-06-19 05:54:20 +00002545 opt_inst->changed = true;
2546 list_add(&opt_inst->tmp_list, &opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002547 }
2548 if (!opt_found) {
2549 err = -ENOENT;
2550 goto team_put;
2551 }
2552 }
2553
Jiri Pirko2fcdb2c2012-06-19 05:54:20 +00002554 err = team_nl_send_event_options_get(team, &opt_inst_list);
2555
Jiri Pirko3d249d42011-11-11 22:16:48 +00002556team_put:
2557 team_nl_team_put(team);
Jiri Pirko7d259502015-12-03 12:12:17 +01002558rtnl_unlock:
2559 rtnl_unlock();
Jiri Pirko3d249d42011-11-11 22:16:48 +00002560 return err;
2561}
2562
Jiri Pirkod90f8892013-02-01 08:17:24 +00002563static int team_nl_fill_one_port_get(struct sk_buff *skb,
2564 struct team_port *port)
2565{
2566 struct nlattr *port_item;
2567
2568 port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
2569 if (!port_item)
2570 goto nest_cancel;
2571 if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex))
2572 goto nest_cancel;
2573 if (port->changed) {
2574 if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED))
2575 goto nest_cancel;
2576 port->changed = false;
2577 }
2578 if ((port->removed &&
2579 nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) ||
2580 (port->state.linkup &&
2581 nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) ||
2582 nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) ||
2583 nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex))
2584 goto nest_cancel;
2585 nla_nest_end(skb, port_item);
2586 return 0;
2587
2588nest_cancel:
2589 nla_nest_cancel(skb, port_item);
2590 return -EMSGSIZE;
2591}
2592
2593static int team_nl_send_port_list_get(struct team *team, u32 portid, u32 seq,
2594 int flags, team_nl_send_func_t *send_func,
2595 struct team_port *one_port)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002596{
2597 struct nlattr *port_list;
Jiri Pirkod90f8892013-02-01 08:17:24 +00002598 struct nlmsghdr *nlh;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002599 void *hdr;
2600 struct team_port *port;
Jiri Pirkod90f8892013-02-01 08:17:24 +00002601 int err;
2602 struct sk_buff *skb = NULL;
2603 bool incomplete;
2604 int i;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002605
Jiri Pirko3f3e7ce2013-05-29 05:02:57 +00002606 port = list_first_entry_or_null(&team->port_list,
2607 struct team_port, list);
Jiri Pirkod90f8892013-02-01 08:17:24 +00002608
2609start_again:
2610 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2611 if (err)
2612 return err;
2613
2614 hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
Jiri Pirko3d249d42011-11-11 22:16:48 +00002615 TEAM_CMD_PORT_LIST_GET);
Wei Yongjuna326e6d2012-09-24 18:29:35 +00002616 if (!hdr)
2617 return -EMSGSIZE;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002618
David S. Miller86ebb022012-04-01 20:25:18 -04002619 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2620 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002621 port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
2622 if (!port_list)
Jiri Pirko3221c642012-06-19 05:54:13 +00002623 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002624
Jiri Pirkod90f8892013-02-01 08:17:24 +00002625 i = 0;
2626 incomplete = false;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002627
Jiri Pirkod90f8892013-02-01 08:17:24 +00002628 /* If one port is selected, called wants to send port list containing
2629 * only this port. Otherwise go through all listed ports and send all
2630 */
2631 if (one_port) {
2632 err = team_nl_fill_one_port_get(skb, one_port);
2633 if (err)
2634 goto errout;
Jiri Pirko3f3e7ce2013-05-29 05:02:57 +00002635 } else if (port) {
2636 list_for_each_entry_from(port, &team->port_list, list) {
Jiri Pirkod90f8892013-02-01 08:17:24 +00002637 err = team_nl_fill_one_port_get(skb, port);
2638 if (err) {
2639 if (err == -EMSGSIZE) {
2640 if (!i)
2641 goto errout;
2642 incomplete = true;
2643 break;
2644 }
2645 goto errout;
2646 }
2647 i++;
Jiri Pirkob82b9182012-01-24 05:16:00 +00002648 }
Jiri Pirko3d249d42011-11-11 22:16:48 +00002649 }
2650
2651 nla_nest_end(skb, port_list);
Jiri Pirkod90f8892013-02-01 08:17:24 +00002652 genlmsg_end(skb, hdr);
2653 if (incomplete)
2654 goto start_again;
2655
2656send_done:
2657 nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
2658 if (!nlh) {
2659 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2660 if (err)
2661 goto errout;
2662 goto send_done;
2663 }
2664
2665 return send_func(skb, team, portid);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002666
2667nla_put_failure:
Jiri Pirkod90f8892013-02-01 08:17:24 +00002668 err = -EMSGSIZE;
2669errout:
Jiri Pirko3d249d42011-11-11 22:16:48 +00002670 genlmsg_cancel(skb, hdr);
Jiri Pirkod90f8892013-02-01 08:17:24 +00002671 nlmsg_free(skb);
2672 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002673}
2674
2675static int team_nl_cmd_port_list_get(struct sk_buff *skb,
2676 struct genl_info *info)
2677{
2678 struct team *team;
2679 int err;
2680
2681 team = team_nl_team_get(info);
2682 if (!team)
2683 return -EINVAL;
2684
Jiri Pirkod90f8892013-02-01 08:17:24 +00002685 err = team_nl_send_port_list_get(team, info->snd_portid, info->snd_seq,
2686 NLM_F_ACK, team_nl_send_unicast, NULL);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002687
2688 team_nl_team_put(team);
2689
2690 return err;
2691}
2692
Johannes Berg4534de82013-11-14 17:14:46 +01002693static const struct genl_ops team_nl_ops[] = {
Jiri Pirko3d249d42011-11-11 22:16:48 +00002694 {
2695 .cmd = TEAM_CMD_NOOP,
2696 .doit = team_nl_cmd_noop,
2697 .policy = team_nl_policy,
2698 },
2699 {
2700 .cmd = TEAM_CMD_OPTIONS_SET,
2701 .doit = team_nl_cmd_options_set,
2702 .policy = team_nl_policy,
2703 .flags = GENL_ADMIN_PERM,
2704 },
2705 {
2706 .cmd = TEAM_CMD_OPTIONS_GET,
2707 .doit = team_nl_cmd_options_get,
2708 .policy = team_nl_policy,
2709 .flags = GENL_ADMIN_PERM,
2710 },
2711 {
2712 .cmd = TEAM_CMD_PORT_LIST_GET,
2713 .doit = team_nl_cmd_port_list_get,
2714 .policy = team_nl_policy,
2715 .flags = GENL_ADMIN_PERM,
2716 },
2717};
2718
Johannes Berg2a94fe42013-11-19 15:19:39 +01002719static const struct genl_multicast_group team_nl_mcgrps[] = {
2720 { .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME, },
Jiri Pirko3d249d42011-11-11 22:16:48 +00002721};
2722
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002723static int team_nl_send_multicast(struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002724 struct team *team, u32 portid)
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002725{
Johannes Berg68eb5502013-11-19 15:19:38 +01002726 return genlmsg_multicast_netns(&team_nl_family, dev_net(team->dev),
Johannes Berg2a94fe42013-11-19 15:19:39 +01002727 skb, 0, 0, GFP_KERNEL);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002728}
2729
Jiri Pirko01048d92012-06-19 05:54:14 +00002730static int team_nl_send_event_options_get(struct team *team,
2731 struct list_head *sel_opt_inst_list)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002732{
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002733 return team_nl_send_options_get(team, 0, 0, 0, team_nl_send_multicast,
2734 sel_opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002735}
2736
Jiri Pirkod90f8892013-02-01 08:17:24 +00002737static int team_nl_send_event_port_get(struct team *team,
2738 struct team_port *port)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002739{
Jiri Pirkod90f8892013-02-01 08:17:24 +00002740 return team_nl_send_port_list_get(team, 0, 0, 0, team_nl_send_multicast,
2741 port);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002742}
2743
2744static int team_nl_init(void)
2745{
Johannes Berg2a94fe42013-11-19 15:19:39 +01002746 return genl_register_family_with_ops_groups(&team_nl_family, team_nl_ops,
2747 team_nl_mcgrps);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002748}
2749
2750static void team_nl_fini(void)
2751{
2752 genl_unregister_family(&team_nl_family);
2753}
2754
2755
2756/******************
2757 * Change checkers
2758 ******************/
2759
Jiri Pirkob82b9182012-01-24 05:16:00 +00002760static void __team_options_change_check(struct team *team)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002761{
2762 int err;
Jiri Pirko01048d92012-06-19 05:54:14 +00002763 struct team_option_inst *opt_inst;
2764 LIST_HEAD(sel_opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002765
Jiri Pirko01048d92012-06-19 05:54:14 +00002766 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2767 if (opt_inst->changed)
2768 list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
2769 }
2770 err = team_nl_send_event_options_get(team, &sel_opt_inst_list);
Jiri Pirko0c7517e2012-08-23 03:26:51 +00002771 if (err && err != -ESRCH)
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002772 netdev_warn(team->dev, "Failed to send options change via netlink (err %d)\n",
2773 err);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002774}
2775
2776/* rtnl lock is held */
Jiri Pirko10f3f512012-09-21 01:51:59 +00002777
2778static void __team_port_change_send(struct team_port *port, bool linkup)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002779{
2780 int err;
2781
Jiri Pirkob82b9182012-01-24 05:16:00 +00002782 port->changed = true;
Jiri Pirko71472ec2012-04-10 05:15:44 +00002783 port->state.linkup = linkup;
2784 team_refresh_port_linkup(port);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002785 if (linkup) {
2786 struct ethtool_cmd ecmd;
2787
2788 err = __ethtool_get_settings(port->dev, &ecmd);
2789 if (!err) {
Jiri Pirko71472ec2012-04-10 05:15:44 +00002790 port->state.speed = ethtool_cmd_speed(&ecmd);
2791 port->state.duplex = ecmd.duplex;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002792 goto send_event;
2793 }
2794 }
Jiri Pirko71472ec2012-04-10 05:15:44 +00002795 port->state.speed = 0;
2796 port->state.duplex = 0;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002797
2798send_event:
Jiri Pirkod90f8892013-02-01 08:17:24 +00002799 err = team_nl_send_event_port_get(port->team, port);
Jiri Pirko0c7517e2012-08-23 03:26:51 +00002800 if (err && err != -ESRCH)
2801 netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink (err %d)\n",
2802 port->dev->name, err);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002803
2804}
2805
Flavio Leitner06a7fc42012-12-30 08:27:29 +00002806static void __team_carrier_check(struct team *team)
2807{
2808 struct team_port *port;
2809 bool team_linkup;
2810
Flavio Leitnere1854832013-02-05 09:30:55 +00002811 if (team->user_carrier_enabled)
2812 return;
2813
Flavio Leitner06a7fc42012-12-30 08:27:29 +00002814 team_linkup = false;
2815 list_for_each_entry(port, &team->port_list, list) {
2816 if (port->linkup) {
2817 team_linkup = true;
2818 break;
2819 }
2820 }
2821
2822 if (team_linkup)
2823 netif_carrier_on(team->dev);
2824 else
2825 netif_carrier_off(team->dev);
2826}
2827
Jiri Pirko10f3f512012-09-21 01:51:59 +00002828static void __team_port_change_check(struct team_port *port, bool linkup)
2829{
2830 if (port->state.linkup != linkup)
2831 __team_port_change_send(port, linkup);
Flavio Leitner06a7fc42012-12-30 08:27:29 +00002832 __team_carrier_check(port->team);
Jiri Pirko10f3f512012-09-21 01:51:59 +00002833}
2834
2835static void __team_port_change_port_added(struct team_port *port, bool linkup)
2836{
2837 __team_port_change_send(port, linkup);
Flavio Leitner06a7fc42012-12-30 08:27:29 +00002838 __team_carrier_check(port->team);
Jiri Pirko10f3f512012-09-21 01:51:59 +00002839}
2840
2841static void __team_port_change_port_removed(struct team_port *port)
2842{
2843 port->removed = true;
2844 __team_port_change_send(port, false);
Flavio Leitner06a7fc42012-12-30 08:27:29 +00002845 __team_carrier_check(port->team);
Jiri Pirko10f3f512012-09-21 01:51:59 +00002846}
2847
Jiri Pirko3d249d42011-11-11 22:16:48 +00002848static void team_port_change_check(struct team_port *port, bool linkup)
2849{
2850 struct team *team = port->team;
2851
Jiri Pirko61dc3462011-11-16 11:09:08 +00002852 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002853 __team_port_change_check(port, linkup);
Jiri Pirko61dc3462011-11-16 11:09:08 +00002854 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002855}
2856
Jiri Pirko0f1aad22012-06-19 05:54:11 +00002857
Jiri Pirko3d249d42011-11-11 22:16:48 +00002858/************************************
2859 * Net device notifier event handler
2860 ************************************/
2861
2862static int team_device_event(struct notifier_block *unused,
2863 unsigned long event, void *ptr)
2864{
Jiri Pirko351638e2013-05-28 01:30:21 +00002865 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002866 struct team_port *port;
2867
2868 port = team_port_get_rtnl(dev);
2869 if (!port)
2870 return NOTIFY_DONE;
2871
2872 switch (event) {
2873 case NETDEV_UP:
2874 if (netif_carrier_ok(dev))
2875 team_port_change_check(port, true);
Jiri Pirkoed2da032014-04-23 14:17:55 +02002876 break;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002877 case NETDEV_DOWN:
2878 team_port_change_check(port, false);
Jiri Pirkoed2da032014-04-23 14:17:55 +02002879 break;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002880 case NETDEV_CHANGE:
2881 if (netif_running(port->dev))
2882 team_port_change_check(port,
2883 !!netif_carrier_ok(port->dev));
2884 break;
2885 case NETDEV_UNREGISTER:
2886 team_del_slave(port->team->dev, dev);
2887 break;
2888 case NETDEV_FEAT_CHANGE:
2889 team_compute_features(port->team);
2890 break;
Veaceslav Falicob01f2362014-01-16 00:02:19 +01002891 case NETDEV_PRECHANGEMTU:
Jiri Pirko3d249d42011-11-11 22:16:48 +00002892 /* Forbid to change mtu of underlaying device */
Jiri Pirko9d0d68f2014-05-29 20:46:17 +02002893 if (!port->team->port_mtu_change_allowed)
2894 return NOTIFY_BAD;
2895 break;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002896 case NETDEV_PRE_TYPE_CHANGE:
2897 /* Forbid to change type of underlaying device */
2898 return NOTIFY_BAD;
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02002899 case NETDEV_RESEND_IGMP:
2900 /* Propagate to master device */
2901 call_netdevice_notifiers(event, port->team->dev);
2902 break;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002903 }
2904 return NOTIFY_DONE;
2905}
2906
2907static struct notifier_block team_notifier_block __read_mostly = {
2908 .notifier_call = team_device_event,
2909};
2910
2911
2912/***********************
2913 * Module init and exit
2914 ***********************/
2915
2916static int __init team_module_init(void)
2917{
2918 int err;
2919
2920 register_netdevice_notifier(&team_notifier_block);
2921
2922 err = rtnl_link_register(&team_link_ops);
2923 if (err)
2924 goto err_rtnl_reg;
2925
2926 err = team_nl_init();
2927 if (err)
2928 goto err_nl_init;
2929
2930 return 0;
2931
2932err_nl_init:
2933 rtnl_link_unregister(&team_link_ops);
2934
2935err_rtnl_reg:
2936 unregister_netdevice_notifier(&team_notifier_block);
2937
2938 return err;
2939}
2940
2941static void __exit team_module_exit(void)
2942{
2943 team_nl_fini();
2944 rtnl_link_unregister(&team_link_ops);
2945 unregister_netdevice_notifier(&team_notifier_block);
2946}
2947
2948module_init(team_module_init);
2949module_exit(team_module_exit);
2950
2951MODULE_LICENSE("GPL v2");
2952MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
2953MODULE_DESCRIPTION("Ethernet team device driver");
2954MODULE_ALIAS_RTNL_LINK(DRV_NAME);