team: comments: s/net\/drivers\/team/drivers\/net\/team/
[linux-2.6-block.git] / drivers / net / team / team.c
CommitLineData
3d249d4c 1/*
0d572e45 2 * drivers/net/team/team.c - Network team device driver
3d249d4c
JP
3 * 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>
87002b03 21#include <linux/if_vlan.h>
3d249d4c
JP
22#include <linux/if_arp.h>
23#include <linux/socket.h>
24#include <linux/etherdevice.h>
25#include <linux/rtnetlink.h>
26#include <net/rtnetlink.h>
27#include <net/genetlink.h>
28#include <net/netlink.h>
29#include <linux/if_team.h>
30
31#define DRV_NAME "team"
32
33
34/**********
35 * Helpers
36 **********/
37
38#define team_port_exists(dev) (dev->priv_flags & IFF_TEAM_PORT)
39
40static struct team_port *team_port_get_rcu(const struct net_device *dev)
41{
42 struct team_port *port = rcu_dereference(dev->rx_handler_data);
43
44 return team_port_exists(dev) ? port : NULL;
45}
46
47static struct team_port *team_port_get_rtnl(const struct net_device *dev)
48{
49 struct team_port *port = rtnl_dereference(dev->rx_handler_data);
50
51 return team_port_exists(dev) ? port : NULL;
52}
53
54/*
55 * Since the ability to change mac address for open port device is tested in
56 * team_port_add, this function can be called without control of return value
57 */
58static int __set_port_mac(struct net_device *port_dev,
59 const unsigned char *dev_addr)
60{
61 struct sockaddr addr;
62
63 memcpy(addr.sa_data, dev_addr, ETH_ALEN);
64 addr.sa_family = ARPHRD_ETHER;
65 return dev_set_mac_address(port_dev, &addr);
66}
67
cade4555 68static int team_port_set_orig_mac(struct team_port *port)
3d249d4c
JP
69{
70 return __set_port_mac(port->dev, port->orig.dev_addr);
71}
72
73int team_port_set_team_mac(struct team_port *port)
74{
75 return __set_port_mac(port->dev, port->team->dev->dev_addr);
76}
77EXPORT_SYMBOL(team_port_set_team_mac);
78
71472ec1
JP
79static void team_refresh_port_linkup(struct team_port *port)
80{
81 port->linkup = port->user.linkup_enabled ? port->user.linkup :
82 port->state.linkup;
83}
3d249d4c
JP
84
85/*******************
86 * Options handling
87 *******************/
88
80f7c668
JP
89struct team_option_inst { /* One for each option instance */
90 struct list_head list;
91 struct team_option *option;
92 struct team_port *port; /* != NULL if per-port */
b1303326 93 u32 array_index;
80f7c668
JP
94 bool changed;
95 bool removed;
96};
97
98static struct team_option *__team_find_option(struct team *team,
99 const char *opt_name)
358b8382
JP
100{
101 struct team_option *option;
102
103 list_for_each_entry(option, &team->option_list, list) {
104 if (strcmp(option->name, opt_name) == 0)
105 return option;
106 }
107 return NULL;
108}
109
80f7c668
JP
110static void __team_option_inst_del(struct team_option_inst *opt_inst)
111{
112 list_del(&opt_inst->list);
113 kfree(opt_inst);
114}
115
116static void __team_option_inst_del_option(struct team *team,
117 struct team_option *option)
118{
119 struct team_option_inst *opt_inst, *tmp;
120
121 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
122 if (opt_inst->option == option)
123 __team_option_inst_del(opt_inst);
124 }
125}
126
b1303326
JP
127static int __team_option_inst_add(struct team *team, struct team_option *option,
128 struct team_port *port)
129{
130 struct team_option_inst *opt_inst;
131 unsigned int array_size;
132 unsigned int i;
133
134 array_size = option->array_size;
135 if (!array_size)
136 array_size = 1; /* No array but still need one instance */
137
138 for (i = 0; i < array_size; i++) {
139 opt_inst = kmalloc(sizeof(*opt_inst), GFP_KERNEL);
140 if (!opt_inst)
141 return -ENOMEM;
142 opt_inst->option = option;
143 opt_inst->port = port;
144 opt_inst->array_index = i;
145 opt_inst->changed = true;
146 opt_inst->removed = false;
147 list_add_tail(&opt_inst->list, &team->option_inst_list);
148 }
149 return 0;
150}
151
80f7c668
JP
152static int __team_option_inst_add_option(struct team *team,
153 struct team_option *option)
154{
155 struct team_port *port;
156 int err;
157
b1303326
JP
158 if (!option->per_port) {
159 err = __team_option_inst_add(team, option, 0);
160 if (err)
161 goto inst_del_option;
162 }
80f7c668
JP
163
164 list_for_each_entry(port, &team->port_list, list) {
165 err = __team_option_inst_add(team, option, port);
166 if (err)
167 goto inst_del_option;
168 }
169 return 0;
170
171inst_del_option:
172 __team_option_inst_del_option(team, option);
173 return err;
174}
175
176static void __team_option_inst_mark_removed_option(struct team *team,
177 struct team_option *option)
178{
179 struct team_option_inst *opt_inst;
180
181 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
182 if (opt_inst->option == option) {
183 opt_inst->changed = true;
184 opt_inst->removed = true;
185 }
186 }
187}
188
189static void __team_option_inst_del_port(struct team *team,
190 struct team_port *port)
191{
192 struct team_option_inst *opt_inst, *tmp;
193
194 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
195 if (opt_inst->option->per_port &&
196 opt_inst->port == port)
197 __team_option_inst_del(opt_inst);
198 }
199}
200
201static int __team_option_inst_add_port(struct team *team,
202 struct team_port *port)
203{
204 struct team_option *option;
205 int err;
206
207 list_for_each_entry(option, &team->option_list, list) {
208 if (!option->per_port)
209 continue;
210 err = __team_option_inst_add(team, option, port);
211 if (err)
212 goto inst_del_port;
213 }
214 return 0;
215
216inst_del_port:
217 __team_option_inst_del_port(team, port);
218 return err;
219}
220
221static void __team_option_inst_mark_removed_port(struct team *team,
222 struct team_port *port)
223{
224 struct team_option_inst *opt_inst;
225
226 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
227 if (opt_inst->port == port) {
228 opt_inst->changed = true;
229 opt_inst->removed = true;
230 }
231 }
232}
233
234static int __team_options_register(struct team *team,
235 const struct team_option *option,
236 size_t option_count)
3d249d4c
JP
237{
238 int i;
2bba19ff 239 struct team_option **dst_opts;
358b8382 240 int err;
3d249d4c 241
2bba19ff
JP
242 dst_opts = kzalloc(sizeof(struct team_option *) * option_count,
243 GFP_KERNEL);
244 if (!dst_opts)
245 return -ENOMEM;
358b8382 246 for (i = 0; i < option_count; i++, option++) {
358b8382
JP
247 if (__team_find_option(team, option->name)) {
248 err = -EEXIST;
80f7c668 249 goto alloc_rollback;
358b8382 250 }
f8a15af0
JP
251 dst_opts[i] = kmemdup(option, sizeof(*option), GFP_KERNEL);
252 if (!dst_opts[i]) {
358b8382 253 err = -ENOMEM;
80f7c668 254 goto alloc_rollback;
358b8382 255 }
358b8382
JP
256 }
257
b82b9183 258 for (i = 0; i < option_count; i++) {
80f7c668
JP
259 err = __team_option_inst_add_option(team, dst_opts[i]);
260 if (err)
261 goto inst_rollback;
358b8382 262 list_add_tail(&dst_opts[i]->list, &team->option_list);
b82b9183 263 }
358b8382 264
2bba19ff 265 kfree(dst_opts);
358b8382
JP
266 return 0;
267
80f7c668
JP
268inst_rollback:
269 for (i--; i >= 0; i--)
270 __team_option_inst_del_option(team, dst_opts[i]);
271
272 i = option_count - 1;
273alloc_rollback:
274 for (i--; i >= 0; i--)
358b8382
JP
275 kfree(dst_opts[i]);
276
2bba19ff 277 kfree(dst_opts);
358b8382 278 return err;
3d249d4c 279}
358b8382 280
b82b9183
JP
281static void __team_options_mark_removed(struct team *team,
282 const struct team_option *option,
283 size_t option_count)
284{
285 int i;
286
287 for (i = 0; i < option_count; i++, option++) {
288 struct team_option *del_opt;
3d249d4c 289
b82b9183 290 del_opt = __team_find_option(team, option->name);
80f7c668
JP
291 if (del_opt)
292 __team_option_inst_mark_removed_option(team, del_opt);
b82b9183
JP
293 }
294}
3d249d4c
JP
295
296static void __team_options_unregister(struct team *team,
358b8382 297 const struct team_option *option,
3d249d4c
JP
298 size_t option_count)
299{
300 int i;
301
358b8382
JP
302 for (i = 0; i < option_count; i++, option++) {
303 struct team_option *del_opt;
304
305 del_opt = __team_find_option(team, option->name);
306 if (del_opt) {
80f7c668 307 __team_option_inst_del_option(team, del_opt);
358b8382
JP
308 list_del(&del_opt->list);
309 kfree(del_opt);
310 }
311 }
3d249d4c
JP
312}
313
b82b9183
JP
314static void __team_options_change_check(struct team *team);
315
316int team_options_register(struct team *team,
317 const struct team_option *option,
318 size_t option_count)
319{
320 int err;
321
322 err = __team_options_register(team, option, option_count);
323 if (err)
324 return err;
325 __team_options_change_check(team);
326 return 0;
327}
328EXPORT_SYMBOL(team_options_register);
329
358b8382
JP
330void team_options_unregister(struct team *team,
331 const struct team_option *option,
3d249d4c
JP
332 size_t option_count)
333{
b82b9183
JP
334 __team_options_mark_removed(team, option, option_count);
335 __team_options_change_check(team);
3d249d4c 336 __team_options_unregister(team, option, option_count);
3d249d4c
JP
337}
338EXPORT_SYMBOL(team_options_unregister);
339
80f7c668 340static int team_option_port_add(struct team *team, struct team_port *port)
3d249d4c 341{
80f7c668
JP
342 int err;
343
344 err = __team_option_inst_add_port(team, port);
345 if (err)
346 return err;
347 __team_options_change_check(team);
348 return 0;
3d249d4c
JP
349}
350
80f7c668
JP
351static void team_option_port_del(struct team *team, struct team_port *port)
352{
353 __team_option_inst_mark_removed_port(team, port);
354 __team_options_change_check(team);
355 __team_option_inst_del_port(team, port);
356}
357
358static int team_option_get(struct team *team,
359 struct team_option_inst *opt_inst,
360 struct team_gsetter_ctx *ctx)
361{
f82b959d
JP
362 if (!opt_inst->option->getter)
363 return -EOPNOTSUPP;
80f7c668
JP
364 return opt_inst->option->getter(team, ctx);
365}
366
367static int team_option_set(struct team *team,
368 struct team_option_inst *opt_inst,
369 struct team_gsetter_ctx *ctx)
3d249d4c
JP
370{
371 int err;
372
f82b959d
JP
373 if (!opt_inst->option->setter)
374 return -EOPNOTSUPP;
80f7c668 375 err = opt_inst->option->setter(team, ctx);
3d249d4c
JP
376 if (err)
377 return err;
378
80f7c668 379 opt_inst->changed = true;
b82b9183 380 __team_options_change_check(team);
3d249d4c
JP
381 return err;
382}
383
384/****************
385 * Mode handling
386 ****************/
387
388static LIST_HEAD(mode_list);
389static DEFINE_SPINLOCK(mode_list_lock);
390
0402788a
JP
391struct team_mode_item {
392 struct list_head list;
393 const struct team_mode *mode;
394};
395
396static struct team_mode_item *__find_mode(const char *kind)
3d249d4c 397{
0402788a 398 struct team_mode_item *mitem;
3d249d4c 399
0402788a
JP
400 list_for_each_entry(mitem, &mode_list, list) {
401 if (strcmp(mitem->mode->kind, kind) == 0)
402 return mitem;
3d249d4c
JP
403 }
404 return NULL;
405}
406
407static bool is_good_mode_name(const char *name)
408{
409 while (*name != '\0') {
410 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
411 return false;
412 name++;
413 }
414 return true;
415}
416
0402788a 417int team_mode_register(const struct team_mode *mode)
3d249d4c
JP
418{
419 int err = 0;
0402788a 420 struct team_mode_item *mitem;
3d249d4c
JP
421
422 if (!is_good_mode_name(mode->kind) ||
423 mode->priv_size > TEAM_MODE_PRIV_SIZE)
424 return -EINVAL;
0402788a
JP
425
426 mitem = kmalloc(sizeof(*mitem), GFP_KERNEL);
427 if (!mitem)
428 return -ENOMEM;
429
3d249d4c
JP
430 spin_lock(&mode_list_lock);
431 if (__find_mode(mode->kind)) {
432 err = -EEXIST;
0402788a 433 kfree(mitem);
3d249d4c
JP
434 goto unlock;
435 }
0402788a
JP
436 mitem->mode = mode;
437 list_add_tail(&mitem->list, &mode_list);
3d249d4c
JP
438unlock:
439 spin_unlock(&mode_list_lock);
440 return err;
441}
442EXPORT_SYMBOL(team_mode_register);
443
0402788a 444void team_mode_unregister(const struct team_mode *mode)
3d249d4c 445{
0402788a
JP
446 struct team_mode_item *mitem;
447
3d249d4c 448 spin_lock(&mode_list_lock);
0402788a
JP
449 mitem = __find_mode(mode->kind);
450 if (mitem) {
451 list_del_init(&mitem->list);
452 kfree(mitem);
453 }
3d249d4c 454 spin_unlock(&mode_list_lock);
3d249d4c
JP
455}
456EXPORT_SYMBOL(team_mode_unregister);
457
0402788a 458static const struct team_mode *team_mode_get(const char *kind)
3d249d4c 459{
0402788a
JP
460 struct team_mode_item *mitem;
461 const struct team_mode *mode = NULL;
3d249d4c
JP
462
463 spin_lock(&mode_list_lock);
0402788a
JP
464 mitem = __find_mode(kind);
465 if (!mitem) {
3d249d4c
JP
466 spin_unlock(&mode_list_lock);
467 request_module("team-mode-%s", kind);
468 spin_lock(&mode_list_lock);
0402788a 469 mitem = __find_mode(kind);
3d249d4c 470 }
0402788a
JP
471 if (mitem) {
472 mode = mitem->mode;
3d249d4c
JP
473 if (!try_module_get(mode->owner))
474 mode = NULL;
0402788a 475 }
3d249d4c
JP
476
477 spin_unlock(&mode_list_lock);
478 return mode;
479}
480
481static void team_mode_put(const struct team_mode *mode)
482{
483 module_put(mode->owner);
484}
485
486static bool team_dummy_transmit(struct team *team, struct sk_buff *skb)
487{
488 dev_kfree_skb_any(skb);
489 return false;
490}
491
492rx_handler_result_t team_dummy_receive(struct team *team,
493 struct team_port *port,
494 struct sk_buff *skb)
495{
496 return RX_HANDLER_ANOTHER;
497}
498
d299cd51
JP
499static const struct team_mode __team_no_mode = {
500 .kind = "*NOMODE*",
501};
502
503static bool team_is_mode_set(struct team *team)
504{
505 return team->mode != &__team_no_mode;
506}
507
508static void team_set_no_mode(struct team *team)
509{
510 team->mode = &__team_no_mode;
511}
512
3d249d4c
JP
513static void team_adjust_ops(struct team *team)
514{
515 /*
516 * To avoid checks in rx/tx skb paths, ensure here that non-null and
517 * correct ops are always set.
518 */
519
520 if (list_empty(&team->port_list) ||
d299cd51 521 !team_is_mode_set(team) || !team->mode->ops->transmit)
3d249d4c
JP
522 team->ops.transmit = team_dummy_transmit;
523 else
524 team->ops.transmit = team->mode->ops->transmit;
525
526 if (list_empty(&team->port_list) ||
d299cd51 527 !team_is_mode_set(team) || !team->mode->ops->receive)
3d249d4c
JP
528 team->ops.receive = team_dummy_receive;
529 else
530 team->ops.receive = team->mode->ops->receive;
531}
532
533/*
534 * We can benefit from the fact that it's ensured no port is present
535 * at the time of mode change. Therefore no packets are in fly so there's no
536 * need to set mode operations in any special way.
537 */
538static int __team_change_mode(struct team *team,
539 const struct team_mode *new_mode)
540{
541 /* Check if mode was previously set and do cleanup if so */
d299cd51 542 if (team_is_mode_set(team)) {
3d249d4c
JP
543 void (*exit_op)(struct team *team) = team->ops.exit;
544
545 /* Clear ops area so no callback is called any longer */
546 memset(&team->ops, 0, sizeof(struct team_mode_ops));
547 team_adjust_ops(team);
548
549 if (exit_op)
550 exit_op(team);
551 team_mode_put(team->mode);
d299cd51 552 team_set_no_mode(team);
3d249d4c
JP
553 /* zero private data area */
554 memset(&team->mode_priv, 0,
555 sizeof(struct team) - offsetof(struct team, mode_priv));
556 }
557
558 if (!new_mode)
559 return 0;
560
561 if (new_mode->ops->init) {
562 int err;
563
564 err = new_mode->ops->init(team);
565 if (err)
566 return err;
567 }
568
569 team->mode = new_mode;
570 memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops));
571 team_adjust_ops(team);
572
573 return 0;
574}
575
576static int team_change_mode(struct team *team, const char *kind)
577{
0402788a 578 const struct team_mode *new_mode;
3d249d4c
JP
579 struct net_device *dev = team->dev;
580 int err;
581
582 if (!list_empty(&team->port_list)) {
583 netdev_err(dev, "No ports can be present during mode change\n");
584 return -EBUSY;
585 }
586
d299cd51 587 if (team_is_mode_set(team) && strcmp(team->mode->kind, kind) == 0) {
3d249d4c
JP
588 netdev_err(dev, "Unable to change to the same mode the team is in\n");
589 return -EINVAL;
590 }
591
592 new_mode = team_mode_get(kind);
593 if (!new_mode) {
594 netdev_err(dev, "Mode \"%s\" not found\n", kind);
595 return -EINVAL;
596 }
597
598 err = __team_change_mode(team, new_mode);
599 if (err) {
600 netdev_err(dev, "Failed to change to mode \"%s\"\n", kind);
601 team_mode_put(new_mode);
602 return err;
603 }
604
605 netdev_info(dev, "Mode changed to \"%s\"\n", kind);
606 return 0;
607}
608
609
610/************************
611 * Rx path frame handler
612 ************************/
613
19a0b58e
JP
614static bool team_port_enabled(struct team_port *port);
615
3d249d4c
JP
616/* note: already called with rcu_read_lock */
617static rx_handler_result_t team_handle_frame(struct sk_buff **pskb)
618{
619 struct sk_buff *skb = *pskb;
620 struct team_port *port;
621 struct team *team;
622 rx_handler_result_t res;
623
624 skb = skb_share_check(skb, GFP_ATOMIC);
625 if (!skb)
626 return RX_HANDLER_CONSUMED;
627
628 *pskb = skb;
629
630 port = team_port_get_rcu(skb->dev);
631 team = port->team;
19a0b58e
JP
632 if (!team_port_enabled(port)) {
633 /* allow exact match delivery for disabled ports */
634 res = RX_HANDLER_EXACT;
635 } else {
636 res = team->ops.receive(team, port, skb);
637 }
3d249d4c
JP
638 if (res == RX_HANDLER_ANOTHER) {
639 struct team_pcpu_stats *pcpu_stats;
640
641 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
642 u64_stats_update_begin(&pcpu_stats->syncp);
643 pcpu_stats->rx_packets++;
644 pcpu_stats->rx_bytes += skb->len;
645 if (skb->pkt_type == PACKET_MULTICAST)
646 pcpu_stats->rx_multicast++;
647 u64_stats_update_end(&pcpu_stats->syncp);
648
649 skb->dev = team->dev;
650 } else {
651 this_cpu_inc(team->pcpu_stats->rx_dropped);
652 }
653
654 return res;
655}
656
657
658/****************
659 * Port handling
660 ****************/
661
662static bool team_port_find(const struct team *team,
663 const struct team_port *port)
664{
665 struct team_port *cur;
666
667 list_for_each_entry(cur, &team->port_list, list)
668 if (cur == port)
669 return true;
670 return false;
671}
672
19a0b58e
JP
673static bool team_port_enabled(struct team_port *port)
674{
675 return port->index != -1;
676}
677
3d249d4c 678/*
19a0b58e
JP
679 * Enable/disable port by adding to enabled port hashlist and setting
680 * port->index (Might be racy so reader could see incorrect ifindex when
681 * processing a flying packet, but that is not a problem). Write guarded
682 * by team->lock.
3d249d4c 683 */
19a0b58e
JP
684static void team_port_enable(struct team *team,
685 struct team_port *port)
3d249d4c 686{
19a0b58e
JP
687 if (team_port_enabled(port))
688 return;
689 port->index = team->en_port_count++;
3d249d4c
JP
690 hlist_add_head_rcu(&port->hlist,
691 team_port_index_hash(team, port->index));
3d249d4c
JP
692}
693
694static void __reconstruct_port_hlist(struct team *team, int rm_index)
695{
696 int i;
697 struct team_port *port;
698
19a0b58e 699 for (i = rm_index + 1; i < team->en_port_count; i++) {
3d249d4c
JP
700 port = team_get_port_by_index(team, i);
701 hlist_del_rcu(&port->hlist);
702 port->index--;
703 hlist_add_head_rcu(&port->hlist,
704 team_port_index_hash(team, port->index));
705 }
706}
707
19a0b58e
JP
708static void team_port_disable(struct team *team,
709 struct team_port *port)
3d249d4c
JP
710{
711 int rm_index = port->index;
712
19a0b58e
JP
713 if (!team_port_enabled(port))
714 return;
3d249d4c 715 hlist_del_rcu(&port->hlist);
3d249d4c 716 __reconstruct_port_hlist(team, rm_index);
19a0b58e
JP
717 team->en_port_count--;
718 port->index = -1;
3d249d4c
JP
719}
720
721#define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \
722 NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
723 NETIF_F_HIGHDMA | NETIF_F_LRO)
724
725static void __team_compute_features(struct team *team)
726{
727 struct team_port *port;
728 u32 vlan_features = TEAM_VLAN_FEATURES;
729 unsigned short max_hard_header_len = ETH_HLEN;
730
731 list_for_each_entry(port, &team->port_list, list) {
732 vlan_features = netdev_increment_features(vlan_features,
733 port->dev->vlan_features,
734 TEAM_VLAN_FEATURES);
735
736 if (port->dev->hard_header_len > max_hard_header_len)
737 max_hard_header_len = port->dev->hard_header_len;
738 }
739
740 team->dev->vlan_features = vlan_features;
741 team->dev->hard_header_len = max_hard_header_len;
742
743 netdev_change_features(team->dev);
744}
745
746static void team_compute_features(struct team *team)
747{
61dc3461 748 mutex_lock(&team->lock);
3d249d4c 749 __team_compute_features(team);
61dc3461 750 mutex_unlock(&team->lock);
3d249d4c
JP
751}
752
753static int team_port_enter(struct team *team, struct team_port *port)
754{
755 int err = 0;
756
757 dev_hold(team->dev);
758 port->dev->priv_flags |= IFF_TEAM_PORT;
759 if (team->ops.port_enter) {
760 err = team->ops.port_enter(team, port);
761 if (err) {
762 netdev_err(team->dev, "Device %s failed to enter team mode\n",
763 port->dev->name);
764 goto err_port_enter;
765 }
766 }
767
768 return 0;
769
770err_port_enter:
771 port->dev->priv_flags &= ~IFF_TEAM_PORT;
772 dev_put(team->dev);
773
774 return err;
775}
776
777static void team_port_leave(struct team *team, struct team_port *port)
778{
779 if (team->ops.port_leave)
780 team->ops.port_leave(team, port);
781 port->dev->priv_flags &= ~IFF_TEAM_PORT;
782 dev_put(team->dev);
783}
784
785static void __team_port_change_check(struct team_port *port, bool linkup);
786
787static int team_port_add(struct team *team, struct net_device *port_dev)
788{
789 struct net_device *dev = team->dev;
790 struct team_port *port;
791 char *portname = port_dev->name;
792 int err;
793
794 if (port_dev->flags & IFF_LOOPBACK ||
795 port_dev->type != ARPHRD_ETHER) {
796 netdev_err(dev, "Device %s is of an unsupported type\n",
797 portname);
798 return -EINVAL;
799 }
800
801 if (team_port_exists(port_dev)) {
802 netdev_err(dev, "Device %s is already a port "
803 "of a team device\n", portname);
804 return -EBUSY;
805 }
806
807 if (port_dev->flags & IFF_UP) {
808 netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
809 portname);
810 return -EBUSY;
811 }
812
5149ee58
JP
813 port = kzalloc(sizeof(struct team_port) + team->mode->port_priv_size,
814 GFP_KERNEL);
3d249d4c
JP
815 if (!port)
816 return -ENOMEM;
817
818 port->dev = port_dev;
819 port->team = team;
820
821 port->orig.mtu = port_dev->mtu;
822 err = dev_set_mtu(port_dev, dev->mtu);
823 if (err) {
824 netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
825 goto err_set_mtu;
826 }
827
828 memcpy(port->orig.dev_addr, port_dev->dev_addr, ETH_ALEN);
829
830 err = team_port_enter(team, port);
831 if (err) {
832 netdev_err(dev, "Device %s failed to enter team mode\n",
833 portname);
834 goto err_port_enter;
835 }
836
837 err = dev_open(port_dev);
838 if (err) {
839 netdev_dbg(dev, "Device %s opening failed\n",
840 portname);
841 goto err_dev_open;
842 }
843
57459185
JP
844 err = vlan_vids_add_by_dev(port_dev, dev);
845 if (err) {
846 netdev_err(dev, "Failed to add vlan ids to device %s\n",
847 portname);
848 goto err_vids_add;
849 }
850
3d249d4c
JP
851 err = netdev_set_master(port_dev, dev);
852 if (err) {
853 netdev_err(dev, "Device %s failed to set master\n", portname);
854 goto err_set_master;
855 }
856
857 err = netdev_rx_handler_register(port_dev, team_handle_frame,
858 port);
859 if (err) {
860 netdev_err(dev, "Device %s failed to register rx_handler\n",
861 portname);
862 goto err_handler_register;
863 }
864
80f7c668
JP
865 err = team_option_port_add(team, port);
866 if (err) {
867 netdev_err(dev, "Device %s failed to add per-port options\n",
868 portname);
869 goto err_option_port_add;
870 }
871
19a0b58e
JP
872 port->index = -1;
873 team_port_enable(team, port);
874 list_add_tail_rcu(&port->list, &team->port_list);
3d249d4c
JP
875 team_adjust_ops(team);
876 __team_compute_features(team);
877 __team_port_change_check(port, !!netif_carrier_ok(port_dev));
878
879 netdev_info(dev, "Port device %s added\n", portname);
880
881 return 0;
882
80f7c668
JP
883err_option_port_add:
884 netdev_rx_handler_unregister(port_dev);
885
3d249d4c
JP
886err_handler_register:
887 netdev_set_master(port_dev, NULL);
888
889err_set_master:
57459185
JP
890 vlan_vids_del_by_dev(port_dev, dev);
891
892err_vids_add:
3d249d4c
JP
893 dev_close(port_dev);
894
895err_dev_open:
896 team_port_leave(team, port);
897 team_port_set_orig_mac(port);
898
899err_port_enter:
900 dev_set_mtu(port_dev, port->orig.mtu);
901
902err_set_mtu:
903 kfree(port);
904
905 return err;
906}
907
908static int team_port_del(struct team *team, struct net_device *port_dev)
909{
910 struct net_device *dev = team->dev;
911 struct team_port *port;
912 char *portname = port_dev->name;
913
914 port = team_port_get_rtnl(port_dev);
915 if (!port || !team_port_find(team, port)) {
916 netdev_err(dev, "Device %s does not act as a port of this team\n",
917 portname);
918 return -ENOENT;
919 }
920
b82b9183 921 port->removed = true;
3d249d4c 922 __team_port_change_check(port, false);
19a0b58e
JP
923 team_port_disable(team, port);
924 list_del_rcu(&port->list);
3d249d4c 925 team_adjust_ops(team);
80f7c668 926 team_option_port_del(team, port);
3d249d4c
JP
927 netdev_rx_handler_unregister(port_dev);
928 netdev_set_master(port_dev, NULL);
57459185 929 vlan_vids_del_by_dev(port_dev, dev);
3d249d4c
JP
930 dev_close(port_dev);
931 team_port_leave(team, port);
932 team_port_set_orig_mac(port);
933 dev_set_mtu(port_dev, port->orig.mtu);
934 synchronize_rcu();
935 kfree(port);
936 netdev_info(dev, "Port device %s removed\n", portname);
937 __team_compute_features(team);
938
939 return 0;
940}
941
942
943/*****************
944 * Net device ops
945 *****************/
946
80f7c668 947static int team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx)
3d249d4c 948{
d299cd51 949 ctx->data.str_val = team->mode->kind;
3d249d4c
JP
950 return 0;
951}
952
80f7c668 953static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx)
3d249d4c 954{
80f7c668 955 return team_change_mode(team, ctx->data.str_val);
3d249d4c
JP
956}
957
acd69962
JP
958static int team_port_en_option_get(struct team *team,
959 struct team_gsetter_ctx *ctx)
960{
961 ctx->data.bool_val = team_port_enabled(ctx->port);
962 return 0;
963}
964
965static int team_port_en_option_set(struct team *team,
966 struct team_gsetter_ctx *ctx)
967{
968 if (ctx->data.bool_val)
969 team_port_enable(team, ctx->port);
970 else
971 team_port_disable(team, ctx->port);
972 return 0;
973}
974
71472ec1
JP
975static int team_user_linkup_option_get(struct team *team,
976 struct team_gsetter_ctx *ctx)
977{
978 ctx->data.bool_val = ctx->port->user.linkup;
979 return 0;
980}
981
982static int team_user_linkup_option_set(struct team *team,
983 struct team_gsetter_ctx *ctx)
984{
985 ctx->port->user.linkup = ctx->data.bool_val;
986 team_refresh_port_linkup(ctx->port);
987 return 0;
988}
989
990static int team_user_linkup_en_option_get(struct team *team,
991 struct team_gsetter_ctx *ctx)
992{
993 struct team_port *port = ctx->port;
994
995 ctx->data.bool_val = port->user.linkup_enabled;
996 return 0;
997}
998
999static int team_user_linkup_en_option_set(struct team *team,
1000 struct team_gsetter_ctx *ctx)
1001{
1002 struct team_port *port = ctx->port;
1003
1004 port->user.linkup_enabled = ctx->data.bool_val;
1005 team_refresh_port_linkup(ctx->port);
1006 return 0;
1007}
1008
358b8382 1009static const struct team_option team_options[] = {
3d249d4c
JP
1010 {
1011 .name = "mode",
1012 .type = TEAM_OPTION_TYPE_STRING,
1013 .getter = team_mode_option_get,
1014 .setter = team_mode_option_set,
1015 },
acd69962
JP
1016 {
1017 .name = "enabled",
1018 .type = TEAM_OPTION_TYPE_BOOL,
1019 .per_port = true,
1020 .getter = team_port_en_option_get,
1021 .setter = team_port_en_option_set,
1022 },
71472ec1
JP
1023 {
1024 .name = "user_linkup",
1025 .type = TEAM_OPTION_TYPE_BOOL,
1026 .per_port = true,
1027 .getter = team_user_linkup_option_get,
1028 .setter = team_user_linkup_option_set,
1029 },
1030 {
1031 .name = "user_linkup_enabled",
1032 .type = TEAM_OPTION_TYPE_BOOL,
1033 .per_port = true,
1034 .getter = team_user_linkup_en_option_get,
1035 .setter = team_user_linkup_en_option_set,
1036 },
3d249d4c
JP
1037};
1038
1039static int team_init(struct net_device *dev)
1040{
1041 struct team *team = netdev_priv(dev);
1042 int i;
358b8382 1043 int err;
3d249d4c
JP
1044
1045 team->dev = dev;
61dc3461 1046 mutex_init(&team->lock);
d299cd51 1047 team_set_no_mode(team);
3d249d4c
JP
1048
1049 team->pcpu_stats = alloc_percpu(struct team_pcpu_stats);
1050 if (!team->pcpu_stats)
1051 return -ENOMEM;
1052
1053 for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
19a0b58e 1054 INIT_HLIST_HEAD(&team->en_port_hlist[i]);
3d249d4c
JP
1055 INIT_LIST_HEAD(&team->port_list);
1056
1057 team_adjust_ops(team);
1058
1059 INIT_LIST_HEAD(&team->option_list);
80f7c668 1060 INIT_LIST_HEAD(&team->option_inst_list);
358b8382
JP
1061 err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
1062 if (err)
1063 goto err_options_register;
3d249d4c
JP
1064 netif_carrier_off(dev);
1065
1066 return 0;
358b8382
JP
1067
1068err_options_register:
1069 free_percpu(team->pcpu_stats);
1070
1071 return err;
3d249d4c
JP
1072}
1073
1074static void team_uninit(struct net_device *dev)
1075{
1076 struct team *team = netdev_priv(dev);
1077 struct team_port *port;
1078 struct team_port *tmp;
1079
61dc3461 1080 mutex_lock(&team->lock);
3d249d4c
JP
1081 list_for_each_entry_safe(port, tmp, &team->port_list, list)
1082 team_port_del(team, port->dev);
1083
1084 __team_change_mode(team, NULL); /* cleanup */
1085 __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
61dc3461 1086 mutex_unlock(&team->lock);
3d249d4c
JP
1087}
1088
1089static void team_destructor(struct net_device *dev)
1090{
1091 struct team *team = netdev_priv(dev);
1092
1093 free_percpu(team->pcpu_stats);
1094 free_netdev(dev);
1095}
1096
1097static int team_open(struct net_device *dev)
1098{
1099 netif_carrier_on(dev);
1100 return 0;
1101}
1102
1103static int team_close(struct net_device *dev)
1104{
1105 netif_carrier_off(dev);
1106 return 0;
1107}
1108
1109/*
1110 * note: already called with rcu_read_lock
1111 */
1112static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
1113{
1114 struct team *team = netdev_priv(dev);
1115 bool tx_success = false;
1116 unsigned int len = skb->len;
1117
1118 tx_success = team->ops.transmit(team, skb);
1119 if (tx_success) {
1120 struct team_pcpu_stats *pcpu_stats;
1121
1122 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
1123 u64_stats_update_begin(&pcpu_stats->syncp);
1124 pcpu_stats->tx_packets++;
1125 pcpu_stats->tx_bytes += len;
1126 u64_stats_update_end(&pcpu_stats->syncp);
1127 } else {
1128 this_cpu_inc(team->pcpu_stats->tx_dropped);
1129 }
1130
1131 return NETDEV_TX_OK;
1132}
1133
1134static void team_change_rx_flags(struct net_device *dev, int change)
1135{
1136 struct team *team = netdev_priv(dev);
1137 struct team_port *port;
1138 int inc;
1139
1140 rcu_read_lock();
1141 list_for_each_entry_rcu(port, &team->port_list, list) {
1142 if (change & IFF_PROMISC) {
1143 inc = dev->flags & IFF_PROMISC ? 1 : -1;
1144 dev_set_promiscuity(port->dev, inc);
1145 }
1146 if (change & IFF_ALLMULTI) {
1147 inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
1148 dev_set_allmulti(port->dev, inc);
1149 }
1150 }
1151 rcu_read_unlock();
1152}
1153
1154static void team_set_rx_mode(struct net_device *dev)
1155{
1156 struct team *team = netdev_priv(dev);
1157 struct team_port *port;
1158
1159 rcu_read_lock();
1160 list_for_each_entry_rcu(port, &team->port_list, list) {
1161 dev_uc_sync(port->dev, dev);
1162 dev_mc_sync(port->dev, dev);
1163 }
1164 rcu_read_unlock();
1165}
1166
1167static int team_set_mac_address(struct net_device *dev, void *p)
1168{
1169 struct team *team = netdev_priv(dev);
1170 struct team_port *port;
1171 struct sockaddr *addr = p;
1172
7ce5d222 1173 dev->addr_assign_type &= ~NET_ADDR_RANDOM;
3d249d4c
JP
1174 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
1175 rcu_read_lock();
1176 list_for_each_entry_rcu(port, &team->port_list, list)
1177 if (team->ops.port_change_mac)
1178 team->ops.port_change_mac(team, port);
1179 rcu_read_unlock();
1180 return 0;
1181}
1182
1183static int team_change_mtu(struct net_device *dev, int new_mtu)
1184{
1185 struct team *team = netdev_priv(dev);
1186 struct team_port *port;
1187 int err;
1188
1189 /*
1190 * Alhough this is reader, it's guarded by team lock. It's not possible
1191 * to traverse list in reverse under rcu_read_lock
1192 */
61dc3461 1193 mutex_lock(&team->lock);
3d249d4c
JP
1194 list_for_each_entry(port, &team->port_list, list) {
1195 err = dev_set_mtu(port->dev, new_mtu);
1196 if (err) {
1197 netdev_err(dev, "Device %s failed to change mtu",
1198 port->dev->name);
1199 goto unwind;
1200 }
1201 }
61dc3461 1202 mutex_unlock(&team->lock);
3d249d4c
JP
1203
1204 dev->mtu = new_mtu;
1205
1206 return 0;
1207
1208unwind:
1209 list_for_each_entry_continue_reverse(port, &team->port_list, list)
1210 dev_set_mtu(port->dev, dev->mtu);
61dc3461 1211 mutex_unlock(&team->lock);
3d249d4c
JP
1212
1213 return err;
1214}
1215
1216static struct rtnl_link_stats64 *
1217team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1218{
1219 struct team *team = netdev_priv(dev);
1220 struct team_pcpu_stats *p;
1221 u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
1222 u32 rx_dropped = 0, tx_dropped = 0;
1223 unsigned int start;
1224 int i;
1225
1226 for_each_possible_cpu(i) {
1227 p = per_cpu_ptr(team->pcpu_stats, i);
1228 do {
1229 start = u64_stats_fetch_begin_bh(&p->syncp);
1230 rx_packets = p->rx_packets;
1231 rx_bytes = p->rx_bytes;
1232 rx_multicast = p->rx_multicast;
1233 tx_packets = p->tx_packets;
1234 tx_bytes = p->tx_bytes;
1235 } while (u64_stats_fetch_retry_bh(&p->syncp, start));
1236
1237 stats->rx_packets += rx_packets;
1238 stats->rx_bytes += rx_bytes;
1239 stats->multicast += rx_multicast;
1240 stats->tx_packets += tx_packets;
1241 stats->tx_bytes += tx_bytes;
1242 /*
1243 * rx_dropped & tx_dropped are u32, updated
1244 * without syncp protection.
1245 */
1246 rx_dropped += p->rx_dropped;
1247 tx_dropped += p->tx_dropped;
1248 }
1249 stats->rx_dropped = rx_dropped;
1250 stats->tx_dropped = tx_dropped;
1251 return stats;
1252}
1253
8e586137 1254static int team_vlan_rx_add_vid(struct net_device *dev, uint16_t vid)
3d249d4c
JP
1255{
1256 struct team *team = netdev_priv(dev);
1257 struct team_port *port;
87002b03 1258 int err;
3d249d4c 1259
87002b03
JP
1260 /*
1261 * Alhough this is reader, it's guarded by team lock. It's not possible
1262 * to traverse list in reverse under rcu_read_lock
1263 */
1264 mutex_lock(&team->lock);
1265 list_for_each_entry(port, &team->port_list, list) {
1266 err = vlan_vid_add(port->dev, vid);
1267 if (err)
1268 goto unwind;
3d249d4c 1269 }
87002b03 1270 mutex_unlock(&team->lock);
8e586137
JP
1271
1272 return 0;
87002b03
JP
1273
1274unwind:
1275 list_for_each_entry_continue_reverse(port, &team->port_list, list)
1276 vlan_vid_del(port->dev, vid);
1277 mutex_unlock(&team->lock);
1278
1279 return err;
3d249d4c
JP
1280}
1281
8e586137 1282static int team_vlan_rx_kill_vid(struct net_device *dev, uint16_t vid)
3d249d4c
JP
1283{
1284 struct team *team = netdev_priv(dev);
1285 struct team_port *port;
1286
1287 rcu_read_lock();
87002b03
JP
1288 list_for_each_entry_rcu(port, &team->port_list, list)
1289 vlan_vid_del(port->dev, vid);
3d249d4c 1290 rcu_read_unlock();
8e586137
JP
1291
1292 return 0;
3d249d4c
JP
1293}
1294
1295static int team_add_slave(struct net_device *dev, struct net_device *port_dev)
1296{
1297 struct team *team = netdev_priv(dev);
1298 int err;
1299
61dc3461 1300 mutex_lock(&team->lock);
3d249d4c 1301 err = team_port_add(team, port_dev);
61dc3461 1302 mutex_unlock(&team->lock);
3d249d4c
JP
1303 return err;
1304}
1305
1306static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
1307{
1308 struct team *team = netdev_priv(dev);
1309 int err;
1310
61dc3461 1311 mutex_lock(&team->lock);
3d249d4c 1312 err = team_port_del(team, port_dev);
61dc3461 1313 mutex_unlock(&team->lock);
3d249d4c
JP
1314 return err;
1315}
1316
234a8fd4
JP
1317static netdev_features_t team_fix_features(struct net_device *dev,
1318 netdev_features_t features)
1319{
1320 struct team_port *port;
1321 struct team *team = netdev_priv(dev);
1322 netdev_features_t mask;
1323
1324 mask = features;
1325 features &= ~NETIF_F_ONE_FOR_ALL;
1326 features |= NETIF_F_ALL_FOR_ALL;
1327
1328 rcu_read_lock();
1329 list_for_each_entry_rcu(port, &team->port_list, list) {
1330 features = netdev_increment_features(features,
1331 port->dev->features,
1332 mask);
1333 }
1334 rcu_read_unlock();
1335 return features;
1336}
1337
3d249d4c
JP
1338static const struct net_device_ops team_netdev_ops = {
1339 .ndo_init = team_init,
1340 .ndo_uninit = team_uninit,
1341 .ndo_open = team_open,
1342 .ndo_stop = team_close,
1343 .ndo_start_xmit = team_xmit,
1344 .ndo_change_rx_flags = team_change_rx_flags,
1345 .ndo_set_rx_mode = team_set_rx_mode,
1346 .ndo_set_mac_address = team_set_mac_address,
1347 .ndo_change_mtu = team_change_mtu,
1348 .ndo_get_stats64 = team_get_stats64,
1349 .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid,
1350 .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid,
1351 .ndo_add_slave = team_add_slave,
1352 .ndo_del_slave = team_del_slave,
234a8fd4 1353 .ndo_fix_features = team_fix_features,
3d249d4c
JP
1354};
1355
1356
1357/***********************
1358 * rt netlink interface
1359 ***********************/
1360
1361static void team_setup(struct net_device *dev)
1362{
1363 ether_setup(dev);
1364
1365 dev->netdev_ops = &team_netdev_ops;
1366 dev->destructor = team_destructor;
1367 dev->tx_queue_len = 0;
1368 dev->flags |= IFF_MULTICAST;
1369 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
1370
1371 /*
1372 * Indicate we support unicast address filtering. That way core won't
1373 * bring us to promisc mode in case a unicast addr is added.
1374 * Let this up to underlay drivers.
1375 */
1376 dev->priv_flags |= IFF_UNICAST_FLT;
1377
1378 dev->features |= NETIF_F_LLTX;
1379 dev->features |= NETIF_F_GRO;
1380 dev->hw_features = NETIF_F_HW_VLAN_TX |
1381 NETIF_F_HW_VLAN_RX |
1382 NETIF_F_HW_VLAN_FILTER;
1383
1384 dev->features |= dev->hw_features;
1385}
1386
1387static int team_newlink(struct net *src_net, struct net_device *dev,
1388 struct nlattr *tb[], struct nlattr *data[])
1389{
1390 int err;
1391
1392 if (tb[IFLA_ADDRESS] == NULL)
7ce5d222 1393 eth_hw_addr_random(dev);
3d249d4c
JP
1394
1395 err = register_netdevice(dev);
1396 if (err)
1397 return err;
1398
1399 return 0;
1400}
1401
1402static int team_validate(struct nlattr *tb[], struct nlattr *data[])
1403{
1404 if (tb[IFLA_ADDRESS]) {
1405 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1406 return -EINVAL;
1407 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1408 return -EADDRNOTAVAIL;
1409 }
1410 return 0;
1411}
1412
1413static struct rtnl_link_ops team_link_ops __read_mostly = {
1414 .kind = DRV_NAME,
1415 .priv_size = sizeof(struct team),
1416 .setup = team_setup,
1417 .newlink = team_newlink,
1418 .validate = team_validate,
1419};
1420
1421
1422/***********************************
1423 * Generic netlink custom interface
1424 ***********************************/
1425
1426static struct genl_family team_nl_family = {
1427 .id = GENL_ID_GENERATE,
1428 .name = TEAM_GENL_NAME,
1429 .version = TEAM_GENL_VERSION,
1430 .maxattr = TEAM_ATTR_MAX,
1431 .netnsok = true,
1432};
1433
1434static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
1435 [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, },
1436 [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 },
1437 [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED },
1438 [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED },
1439};
1440
1441static const struct nla_policy
1442team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
1443 [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
1444 [TEAM_ATTR_OPTION_NAME] = {
1445 .type = NLA_STRING,
1446 .len = TEAM_STRING_MAX_LEN,
1447 },
1448 [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG },
1449 [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 },
2615598f 1450 [TEAM_ATTR_OPTION_DATA] = { .type = NLA_BINARY },
3d249d4c
JP
1451};
1452
1453static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
1454{
1455 struct sk_buff *msg;
1456 void *hdr;
1457 int err;
1458
1459 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1460 if (!msg)
1461 return -ENOMEM;
1462
1463 hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq,
1464 &team_nl_family, 0, TEAM_CMD_NOOP);
1465 if (IS_ERR(hdr)) {
1466 err = PTR_ERR(hdr);
1467 goto err_msg_put;
1468 }
1469
1470 genlmsg_end(msg, hdr);
1471
1472 return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid);
1473
1474err_msg_put:
1475 nlmsg_free(msg);
1476
1477 return err;
1478}
1479
1480/*
1481 * Netlink cmd functions should be locked by following two functions.
8c0713a5 1482 * Since dev gets held here, that ensures dev won't disappear in between.
3d249d4c
JP
1483 */
1484static struct team *team_nl_team_get(struct genl_info *info)
1485{
1486 struct net *net = genl_info_net(info);
1487 int ifindex;
1488 struct net_device *dev;
1489 struct team *team;
1490
1491 if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
1492 return NULL;
1493
1494 ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
8c0713a5 1495 dev = dev_get_by_index(net, ifindex);
3d249d4c 1496 if (!dev || dev->netdev_ops != &team_netdev_ops) {
8c0713a5
JP
1497 if (dev)
1498 dev_put(dev);
3d249d4c
JP
1499 return NULL;
1500 }
1501
1502 team = netdev_priv(dev);
61dc3461 1503 mutex_lock(&team->lock);
3d249d4c
JP
1504 return team;
1505}
1506
1507static void team_nl_team_put(struct team *team)
1508{
61dc3461 1509 mutex_unlock(&team->lock);
8c0713a5 1510 dev_put(team->dev);
3d249d4c
JP
1511}
1512
1513static int team_nl_send_generic(struct genl_info *info, struct team *team,
1514 int (*fill_func)(struct sk_buff *skb,
1515 struct genl_info *info,
1516 int flags, struct team *team))
1517{
1518 struct sk_buff *skb;
1519 int err;
1520
1521 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1522 if (!skb)
1523 return -ENOMEM;
1524
1525 err = fill_func(skb, info, NLM_F_ACK, team);
1526 if (err < 0)
1527 goto err_fill;
1528
1529 err = genlmsg_unicast(genl_info_net(info), skb, info->snd_pid);
1530 return err;
1531
1532err_fill:
1533 nlmsg_free(skb);
1534 return err;
1535}
1536
b82b9183
JP
1537static int team_nl_fill_options_get(struct sk_buff *skb,
1538 u32 pid, u32 seq, int flags,
1539 struct team *team, bool fillall)
3d249d4c
JP
1540{
1541 struct nlattr *option_list;
1542 void *hdr;
80f7c668
JP
1543 struct team_option_inst *opt_inst;
1544 int err;
3d249d4c
JP
1545
1546 hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags,
1547 TEAM_CMD_OPTIONS_GET);
1548 if (IS_ERR(hdr))
1549 return PTR_ERR(hdr);
1550
86ebb02d
DM
1551 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
1552 goto nla_put_failure;
3d249d4c
JP
1553 option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
1554 if (!option_list)
1555 return -EMSGSIZE;
1556
80f7c668 1557 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
3d249d4c 1558 struct nlattr *option_item;
80f7c668
JP
1559 struct team_option *option = opt_inst->option;
1560 struct team_gsetter_ctx ctx;
3d249d4c 1561
b82b9183 1562 /* Include only changed options if fill all mode is not on */
80f7c668 1563 if (!fillall && !opt_inst->changed)
b82b9183 1564 continue;
3d249d4c
JP
1565 option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
1566 if (!option_item)
1567 goto nla_put_failure;
86ebb02d
DM
1568 if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name))
1569 goto nla_put_failure;
80f7c668 1570 if (opt_inst->changed) {
86ebb02d
DM
1571 if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED))
1572 goto nla_put_failure;
80f7c668 1573 opt_inst->changed = false;
b82b9183 1574 }
80f7c668 1575 if (opt_inst->removed &&
86ebb02d
DM
1576 nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED))
1577 goto nla_put_failure;
80f7c668
JP
1578 if (opt_inst->port &&
1579 nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX,
1580 opt_inst->port->dev->ifindex))
1581 goto nla_put_failure;
1582 ctx.port = opt_inst->port;
b1303326
JP
1583 if (opt_inst->option->array_size &&
1584 nla_put_u32(skb, TEAM_ATTR_OPTION_ARRAY_INDEX,
1585 opt_inst->array_index))
1586 goto nla_put_failure;
1587 ctx.array_index = opt_inst->array_index;
3d249d4c
JP
1588 switch (option->type) {
1589 case TEAM_OPTION_TYPE_U32:
86ebb02d
DM
1590 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32))
1591 goto nla_put_failure;
80f7c668
JP
1592 err = team_option_get(team, opt_inst, &ctx);
1593 if (err)
1594 goto errout;
1595 if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA,
1596 ctx.data.u32_val))
86ebb02d 1597 goto nla_put_failure;
3d249d4c
JP
1598 break;
1599 case TEAM_OPTION_TYPE_STRING:
86ebb02d
DM
1600 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING))
1601 goto nla_put_failure;
80f7c668
JP
1602 err = team_option_get(team, opt_inst, &ctx);
1603 if (err)
1604 goto errout;
86ebb02d 1605 if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA,
80f7c668 1606 ctx.data.str_val))
86ebb02d 1607 goto nla_put_failure;
3d249d4c 1608 break;
2615598f
JP
1609 case TEAM_OPTION_TYPE_BINARY:
1610 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY))
1611 goto nla_put_failure;
80f7c668
JP
1612 err = team_option_get(team, opt_inst, &ctx);
1613 if (err)
1614 goto errout;
2615598f 1615 if (nla_put(skb, TEAM_ATTR_OPTION_DATA,
80f7c668 1616 ctx.data.bin_val.len, ctx.data.bin_val.ptr))
2615598f
JP
1617 goto nla_put_failure;
1618 break;
14f066ba
JP
1619 case TEAM_OPTION_TYPE_BOOL:
1620 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG))
1621 goto nla_put_failure;
1622 err = team_option_get(team, opt_inst, &ctx);
1623 if (err)
1624 goto errout;
1625 if (ctx.data.bool_val &&
1626 nla_put_flag(skb, TEAM_ATTR_OPTION_DATA))
1627 goto nla_put_failure;
1628 break;
3d249d4c
JP
1629 default:
1630 BUG();
1631 }
1632 nla_nest_end(skb, option_item);
1633 }
1634
1635 nla_nest_end(skb, option_list);
1636 return genlmsg_end(skb, hdr);
1637
1638nla_put_failure:
80f7c668
JP
1639 err = -EMSGSIZE;
1640errout:
3d249d4c 1641 genlmsg_cancel(skb, hdr);
80f7c668 1642 return err;
3d249d4c
JP
1643}
1644
b82b9183
JP
1645static int team_nl_fill_options_get_all(struct sk_buff *skb,
1646 struct genl_info *info, int flags,
1647 struct team *team)
3d249d4c 1648{
b82b9183
JP
1649 return team_nl_fill_options_get(skb, info->snd_pid,
1650 info->snd_seq, NLM_F_ACK,
1651 team, true);
3d249d4c
JP
1652}
1653
1654static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
1655{
1656 struct team *team;
1657 int err;
1658
1659 team = team_nl_team_get(info);
1660 if (!team)
1661 return -EINVAL;
1662
b82b9183 1663 err = team_nl_send_generic(info, team, team_nl_fill_options_get_all);
3d249d4c
JP
1664
1665 team_nl_team_put(team);
1666
1667 return err;
1668}
1669
1670static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
1671{
1672 struct team *team;
1673 int err = 0;
1674 int i;
1675 struct nlattr *nl_option;
1676
1677 team = team_nl_team_get(info);
1678 if (!team)
1679 return -EINVAL;
1680
1681 err = -EINVAL;
1682 if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
1683 err = -EINVAL;
1684 goto team_put;
1685 }
1686
1687 nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
80f7c668 1688 struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1];
b1303326 1689 struct nlattr *attr;
14f066ba 1690 struct nlattr *attr_data;
3d249d4c 1691 enum team_option_type opt_type;
80f7c668 1692 int opt_port_ifindex = 0; /* != 0 for per-port options */
b1303326
JP
1693 u32 opt_array_index = 0;
1694 bool opt_is_array = false;
80f7c668 1695 struct team_option_inst *opt_inst;
3d249d4c
JP
1696 char *opt_name;
1697 bool opt_found = false;
1698
1699 if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
1700 err = -EINVAL;
1701 goto team_put;
1702 }
80f7c668 1703 err = nla_parse_nested(opt_attrs, TEAM_ATTR_OPTION_MAX,
3d249d4c
JP
1704 nl_option, team_nl_option_policy);
1705 if (err)
1706 goto team_put;
80f7c668 1707 if (!opt_attrs[TEAM_ATTR_OPTION_NAME] ||
14f066ba 1708 !opt_attrs[TEAM_ATTR_OPTION_TYPE]) {
3d249d4c
JP
1709 err = -EINVAL;
1710 goto team_put;
1711 }
80f7c668 1712 switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) {
3d249d4c
JP
1713 case NLA_U32:
1714 opt_type = TEAM_OPTION_TYPE_U32;
1715 break;
1716 case NLA_STRING:
1717 opt_type = TEAM_OPTION_TYPE_STRING;
1718 break;
2615598f
JP
1719 case NLA_BINARY:
1720 opt_type = TEAM_OPTION_TYPE_BINARY;
1721 break;
14f066ba
JP
1722 case NLA_FLAG:
1723 opt_type = TEAM_OPTION_TYPE_BOOL;
1724 break;
3d249d4c
JP
1725 default:
1726 goto team_put;
1727 }
1728
14f066ba
JP
1729 attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA];
1730 if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) {
1731 err = -EINVAL;
1732 goto team_put;
1733 }
1734
80f7c668 1735 opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]);
b1303326
JP
1736 attr = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX];
1737 if (attr)
1738 opt_port_ifindex = nla_get_u32(attr);
1739
1740 attr = opt_attrs[TEAM_ATTR_OPTION_ARRAY_INDEX];
1741 if (attr) {
1742 opt_is_array = true;
1743 opt_array_index = nla_get_u32(attr);
1744 }
80f7c668
JP
1745
1746 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
1747 struct team_option *option = opt_inst->option;
80f7c668 1748 struct team_gsetter_ctx ctx;
80f7c668 1749 int tmp_ifindex;
3d249d4c 1750
80f7c668
JP
1751 tmp_ifindex = opt_inst->port ?
1752 opt_inst->port->dev->ifindex : 0;
3d249d4c 1753 if (option->type != opt_type ||
80f7c668 1754 strcmp(option->name, opt_name) ||
b1303326
JP
1755 tmp_ifindex != opt_port_ifindex ||
1756 (option->array_size && !opt_is_array) ||
1757 opt_inst->array_index != opt_array_index)
3d249d4c
JP
1758 continue;
1759 opt_found = true;
80f7c668 1760 ctx.port = opt_inst->port;
b1303326 1761 ctx.array_index = opt_inst->array_index;
3d249d4c
JP
1762 switch (opt_type) {
1763 case TEAM_OPTION_TYPE_U32:
14f066ba 1764 ctx.data.u32_val = nla_get_u32(attr_data);
3d249d4c
JP
1765 break;
1766 case TEAM_OPTION_TYPE_STRING:
14f066ba 1767 if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) {
2615598f
JP
1768 err = -EINVAL;
1769 goto team_put;
1770 }
14f066ba 1771 ctx.data.str_val = nla_data(attr_data);
3d249d4c 1772 break;
2615598f 1773 case TEAM_OPTION_TYPE_BINARY:
14f066ba
JP
1774 ctx.data.bin_val.len = nla_len(attr_data);
1775 ctx.data.bin_val.ptr = nla_data(attr_data);
1776 break;
1777 case TEAM_OPTION_TYPE_BOOL:
1778 ctx.data.bool_val = attr_data ? true : false;
2615598f 1779 break;
3d249d4c
JP
1780 default:
1781 BUG();
1782 }
80f7c668 1783 err = team_option_set(team, opt_inst, &ctx);
3d249d4c
JP
1784 if (err)
1785 goto team_put;
1786 }
1787 if (!opt_found) {
1788 err = -ENOENT;
1789 goto team_put;
1790 }
1791 }
1792
1793team_put:
1794 team_nl_team_put(team);
1795
1796 return err;
1797}
1798
b82b9183
JP
1799static int team_nl_fill_port_list_get(struct sk_buff *skb,
1800 u32 pid, u32 seq, int flags,
1801 struct team *team,
1802 bool fillall)
3d249d4c
JP
1803{
1804 struct nlattr *port_list;
1805 void *hdr;
1806 struct team_port *port;
1807
1808 hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags,
1809 TEAM_CMD_PORT_LIST_GET);
1810 if (IS_ERR(hdr))
1811 return PTR_ERR(hdr);
1812
86ebb02d
DM
1813 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
1814 goto nla_put_failure;
3d249d4c
JP
1815 port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
1816 if (!port_list)
1817 return -EMSGSIZE;
1818
1819 list_for_each_entry(port, &team->port_list, list) {
1820 struct nlattr *port_item;
1821
b82b9183
JP
1822 /* Include only changed ports if fill all mode is not on */
1823 if (!fillall && !port->changed)
1824 continue;
3d249d4c
JP
1825 port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
1826 if (!port_item)
1827 goto nla_put_failure;
86ebb02d
DM
1828 if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex))
1829 goto nla_put_failure;
b82b9183 1830 if (port->changed) {
86ebb02d
DM
1831 if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED))
1832 goto nla_put_failure;
b82b9183
JP
1833 port->changed = false;
1834 }
86ebb02d
DM
1835 if ((port->removed &&
1836 nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) ||
71472ec1 1837 (port->state.linkup &&
86ebb02d 1838 nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) ||
71472ec1
JP
1839 nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) ||
1840 nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex))
86ebb02d 1841 goto nla_put_failure;
3d249d4c
JP
1842 nla_nest_end(skb, port_item);
1843 }
1844
1845 nla_nest_end(skb, port_list);
1846 return genlmsg_end(skb, hdr);
1847
1848nla_put_failure:
1849 genlmsg_cancel(skb, hdr);
1850 return -EMSGSIZE;
1851}
1852
b82b9183
JP
1853static int team_nl_fill_port_list_get_all(struct sk_buff *skb,
1854 struct genl_info *info, int flags,
1855 struct team *team)
3d249d4c 1856{
b82b9183
JP
1857 return team_nl_fill_port_list_get(skb, info->snd_pid,
1858 info->snd_seq, NLM_F_ACK,
1859 team, true);
3d249d4c
JP
1860}
1861
1862static int team_nl_cmd_port_list_get(struct sk_buff *skb,
1863 struct genl_info *info)
1864{
1865 struct team *team;
1866 int err;
1867
1868 team = team_nl_team_get(info);
1869 if (!team)
1870 return -EINVAL;
1871
b82b9183 1872 err = team_nl_send_generic(info, team, team_nl_fill_port_list_get_all);
3d249d4c
JP
1873
1874 team_nl_team_put(team);
1875
1876 return err;
1877}
1878
1879static struct genl_ops team_nl_ops[] = {
1880 {
1881 .cmd = TEAM_CMD_NOOP,
1882 .doit = team_nl_cmd_noop,
1883 .policy = team_nl_policy,
1884 },
1885 {
1886 .cmd = TEAM_CMD_OPTIONS_SET,
1887 .doit = team_nl_cmd_options_set,
1888 .policy = team_nl_policy,
1889 .flags = GENL_ADMIN_PERM,
1890 },
1891 {
1892 .cmd = TEAM_CMD_OPTIONS_GET,
1893 .doit = team_nl_cmd_options_get,
1894 .policy = team_nl_policy,
1895 .flags = GENL_ADMIN_PERM,
1896 },
1897 {
1898 .cmd = TEAM_CMD_PORT_LIST_GET,
1899 .doit = team_nl_cmd_port_list_get,
1900 .policy = team_nl_policy,
1901 .flags = GENL_ADMIN_PERM,
1902 },
1903};
1904
1905static struct genl_multicast_group team_change_event_mcgrp = {
1906 .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME,
1907};
1908
b82b9183 1909static int team_nl_send_event_options_get(struct team *team)
3d249d4c
JP
1910{
1911 struct sk_buff *skb;
1912 int err;
1913 struct net *net = dev_net(team->dev);
1914
1915 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1916 if (!skb)
1917 return -ENOMEM;
1918
b82b9183 1919 err = team_nl_fill_options_get(skb, 0, 0, 0, team, false);
3d249d4c
JP
1920 if (err < 0)
1921 goto err_fill;
1922
1923 err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id,
1924 GFP_KERNEL);
1925 return err;
1926
1927err_fill:
1928 nlmsg_free(skb);
1929 return err;
1930}
1931
b82b9183 1932static int team_nl_send_event_port_list_get(struct team *team)
3d249d4c
JP
1933{
1934 struct sk_buff *skb;
1935 int err;
b82b9183 1936 struct net *net = dev_net(team->dev);
3d249d4c
JP
1937
1938 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1939 if (!skb)
1940 return -ENOMEM;
1941
b82b9183 1942 err = team_nl_fill_port_list_get(skb, 0, 0, 0, team, false);
3d249d4c
JP
1943 if (err < 0)
1944 goto err_fill;
1945
1946 err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id,
1947 GFP_KERNEL);
1948 return err;
1949
1950err_fill:
1951 nlmsg_free(skb);
1952 return err;
1953}
1954
1955static int team_nl_init(void)
1956{
1957 int err;
1958
1959 err = genl_register_family_with_ops(&team_nl_family, team_nl_ops,
1960 ARRAY_SIZE(team_nl_ops));
1961 if (err)
1962 return err;
1963
1964 err = genl_register_mc_group(&team_nl_family, &team_change_event_mcgrp);
1965 if (err)
1966 goto err_change_event_grp_reg;
1967
1968 return 0;
1969
1970err_change_event_grp_reg:
1971 genl_unregister_family(&team_nl_family);
1972
1973 return err;
1974}
1975
1976static void team_nl_fini(void)
1977{
1978 genl_unregister_family(&team_nl_family);
1979}
1980
1981
1982/******************
1983 * Change checkers
1984 ******************/
1985
b82b9183 1986static void __team_options_change_check(struct team *team)
3d249d4c
JP
1987{
1988 int err;
1989
b82b9183 1990 err = team_nl_send_event_options_get(team);
3d249d4c
JP
1991 if (err)
1992 netdev_warn(team->dev, "Failed to send options change via netlink\n");
1993}
1994
1995/* rtnl lock is held */
1996static void __team_port_change_check(struct team_port *port, bool linkup)
1997{
1998 int err;
1999
71472ec1 2000 if (!port->removed && port->state.linkup == linkup)
3d249d4c
JP
2001 return;
2002
b82b9183 2003 port->changed = true;
71472ec1
JP
2004 port->state.linkup = linkup;
2005 team_refresh_port_linkup(port);
3d249d4c
JP
2006 if (linkup) {
2007 struct ethtool_cmd ecmd;
2008
2009 err = __ethtool_get_settings(port->dev, &ecmd);
2010 if (!err) {
71472ec1
JP
2011 port->state.speed = ethtool_cmd_speed(&ecmd);
2012 port->state.duplex = ecmd.duplex;
3d249d4c
JP
2013 goto send_event;
2014 }
2015 }
71472ec1
JP
2016 port->state.speed = 0;
2017 port->state.duplex = 0;
3d249d4c
JP
2018
2019send_event:
b82b9183 2020 err = team_nl_send_event_port_list_get(port->team);
3d249d4c
JP
2021 if (err)
2022 netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink\n",
2023 port->dev->name);
2024
2025}
2026
2027static void team_port_change_check(struct team_port *port, bool linkup)
2028{
2029 struct team *team = port->team;
2030
61dc3461 2031 mutex_lock(&team->lock);
3d249d4c 2032 __team_port_change_check(port, linkup);
61dc3461 2033 mutex_unlock(&team->lock);
3d249d4c
JP
2034}
2035
2036/************************************
2037 * Net device notifier event handler
2038 ************************************/
2039
2040static int team_device_event(struct notifier_block *unused,
2041 unsigned long event, void *ptr)
2042{
2043 struct net_device *dev = (struct net_device *) ptr;
2044 struct team_port *port;
2045
2046 port = team_port_get_rtnl(dev);
2047 if (!port)
2048 return NOTIFY_DONE;
2049
2050 switch (event) {
2051 case NETDEV_UP:
2052 if (netif_carrier_ok(dev))
2053 team_port_change_check(port, true);
2054 case NETDEV_DOWN:
2055 team_port_change_check(port, false);
2056 case NETDEV_CHANGE:
2057 if (netif_running(port->dev))
2058 team_port_change_check(port,
2059 !!netif_carrier_ok(port->dev));
2060 break;
2061 case NETDEV_UNREGISTER:
2062 team_del_slave(port->team->dev, dev);
2063 break;
2064 case NETDEV_FEAT_CHANGE:
2065 team_compute_features(port->team);
2066 break;
2067 case NETDEV_CHANGEMTU:
2068 /* Forbid to change mtu of underlaying device */
2069 return NOTIFY_BAD;
2070 case NETDEV_PRE_TYPE_CHANGE:
2071 /* Forbid to change type of underlaying device */
2072 return NOTIFY_BAD;
2073 }
2074 return NOTIFY_DONE;
2075}
2076
2077static struct notifier_block team_notifier_block __read_mostly = {
2078 .notifier_call = team_device_event,
2079};
2080
2081
2082/***********************
2083 * Module init and exit
2084 ***********************/
2085
2086static int __init team_module_init(void)
2087{
2088 int err;
2089
2090 register_netdevice_notifier(&team_notifier_block);
2091
2092 err = rtnl_link_register(&team_link_ops);
2093 if (err)
2094 goto err_rtnl_reg;
2095
2096 err = team_nl_init();
2097 if (err)
2098 goto err_nl_init;
2099
2100 return 0;
2101
2102err_nl_init:
2103 rtnl_link_unregister(&team_link_ops);
2104
2105err_rtnl_reg:
2106 unregister_netdevice_notifier(&team_notifier_block);
2107
2108 return err;
2109}
2110
2111static void __exit team_module_exit(void)
2112{
2113 team_nl_fini();
2114 rtnl_link_unregister(&team_link_ops);
2115 unregister_netdevice_notifier(&team_notifier_block);
2116}
2117
2118module_init(team_module_init);
2119module_exit(team_module_exit);
2120
2121MODULE_LICENSE("GPL v2");
2122MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
2123MODULE_DESCRIPTION("Ethernet team device driver");
2124MODULE_ALIAS_RTNL_LINK(DRV_NAME);