]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/team/team.c
cpuset, isolcpus: document relationship between cpusets & isolcpus
[karo-tx-linux.git] / drivers / net / team / team.c
1 /*
2  * drivers/net/team/team.c - Network team device driver
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>
21 #include <linux/netpoll.h>
22 #include <linux/if_vlan.h>
23 #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>
30 #include <net/sch_generic.h>
31 #include <net/switchdev.h>
32 #include <generated/utsrelease.h>
33 #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
44 static struct team_port *team_port_get_rcu(const struct net_device *dev)
45 {
46         struct team_port *port = rcu_dereference(dev->rx_handler_data);
47
48         return team_port_exists(dev) ? port : NULL;
49 }
50
51 static struct team_port *team_port_get_rtnl(const struct net_device *dev)
52 {
53         struct team_port *port = rtnl_dereference(dev->rx_handler_data);
54
55         return team_port_exists(dev) ? port : NULL;
56 }
57
58 /*
59  * Since the ability to change device address for open port device is tested in
60  * team_port_add, this function can be called without control of return value
61  */
62 static int __set_port_dev_addr(struct net_device *port_dev,
63                                const unsigned char *dev_addr)
64 {
65         struct sockaddr addr;
66
67         memcpy(addr.sa_data, dev_addr, port_dev->addr_len);
68         addr.sa_family = port_dev->type;
69         return dev_set_mac_address(port_dev, &addr);
70 }
71
72 static int team_port_set_orig_dev_addr(struct team_port *port)
73 {
74         return __set_port_dev_addr(port->dev, port->orig.dev_addr);
75 }
76
77 static int team_port_set_team_dev_addr(struct team *team,
78                                        struct team_port *port)
79 {
80         return __set_port_dev_addr(port->dev, team->dev->dev_addr);
81 }
82
83 int team_modeop_port_enter(struct team *team, struct team_port *port)
84 {
85         return team_port_set_team_dev_addr(team, port);
86 }
87 EXPORT_SYMBOL(team_modeop_port_enter);
88
89 void team_modeop_port_change_dev_addr(struct team *team,
90                                       struct team_port *port)
91 {
92         team_port_set_team_dev_addr(team, port);
93 }
94 EXPORT_SYMBOL(team_modeop_port_change_dev_addr);
95
96 static void team_refresh_port_linkup(struct team_port *port)
97 {
98         port->linkup = port->user.linkup_enabled ? port->user.linkup :
99                                                    port->state.linkup;
100 }
101
102
103 /*******************
104  * Options handling
105  *******************/
106
107 struct team_option_inst { /* One for each option instance */
108         struct list_head list;
109         struct list_head tmp_list;
110         struct team_option *option;
111         struct team_option_inst_info info;
112         bool changed;
113         bool removed;
114 };
115
116 static struct team_option *__team_find_option(struct team *team,
117                                               const char *opt_name)
118 {
119         struct team_option *option;
120
121         list_for_each_entry(option, &team->option_list, list) {
122                 if (strcmp(option->name, opt_name) == 0)
123                         return option;
124         }
125         return NULL;
126 }
127
128 static void __team_option_inst_del(struct team_option_inst *opt_inst)
129 {
130         list_del(&opt_inst->list);
131         kfree(opt_inst);
132 }
133
134 static void __team_option_inst_del_option(struct team *team,
135                                           struct team_option *option)
136 {
137         struct team_option_inst *opt_inst, *tmp;
138
139         list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
140                 if (opt_inst->option == option)
141                         __team_option_inst_del(opt_inst);
142         }
143 }
144
145 static int __team_option_inst_add(struct team *team, struct team_option *option,
146                                   struct team_port *port)
147 {
148         struct team_option_inst *opt_inst;
149         unsigned int array_size;
150         unsigned int i;
151         int err;
152
153         array_size = option->array_size;
154         if (!array_size)
155                 array_size = 1; /* No array but still need one instance */
156
157         for (i = 0; i < array_size; i++) {
158                 opt_inst = kmalloc(sizeof(*opt_inst), GFP_KERNEL);
159                 if (!opt_inst)
160                         return -ENOMEM;
161                 opt_inst->option = option;
162                 opt_inst->info.port = port;
163                 opt_inst->info.array_index = i;
164                 opt_inst->changed = true;
165                 opt_inst->removed = false;
166                 list_add_tail(&opt_inst->list, &team->option_inst_list);
167                 if (option->init) {
168                         err = option->init(team, &opt_inst->info);
169                         if (err)
170                                 return err;
171                 }
172
173         }
174         return 0;
175 }
176
177 static int __team_option_inst_add_option(struct team *team,
178                                          struct team_option *option)
179 {
180         int err;
181
182         if (!option->per_port) {
183                 err = __team_option_inst_add(team, option, NULL);
184                 if (err)
185                         goto inst_del_option;
186         }
187         return 0;
188
189 inst_del_option:
190         __team_option_inst_del_option(team, option);
191         return err;
192 }
193
194 static void __team_option_inst_mark_removed_option(struct team *team,
195                                                    struct team_option *option)
196 {
197         struct team_option_inst *opt_inst;
198
199         list_for_each_entry(opt_inst, &team->option_inst_list, list) {
200                 if (opt_inst->option == option) {
201                         opt_inst->changed = true;
202                         opt_inst->removed = true;
203                 }
204         }
205 }
206
207 static void __team_option_inst_del_port(struct team *team,
208                                         struct team_port *port)
209 {
210         struct team_option_inst *opt_inst, *tmp;
211
212         list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
213                 if (opt_inst->option->per_port &&
214                     opt_inst->info.port == port)
215                         __team_option_inst_del(opt_inst);
216         }
217 }
218
219 static int __team_option_inst_add_port(struct team *team,
220                                        struct team_port *port)
221 {
222         struct team_option *option;
223         int err;
224
225         list_for_each_entry(option, &team->option_list, list) {
226                 if (!option->per_port)
227                         continue;
228                 err = __team_option_inst_add(team, option, port);
229                 if (err)
230                         goto inst_del_port;
231         }
232         return 0;
233
234 inst_del_port:
235         __team_option_inst_del_port(team, port);
236         return err;
237 }
238
239 static void __team_option_inst_mark_removed_port(struct team *team,
240                                                  struct team_port *port)
241 {
242         struct team_option_inst *opt_inst;
243
244         list_for_each_entry(opt_inst, &team->option_inst_list, list) {
245                 if (opt_inst->info.port == port) {
246                         opt_inst->changed = true;
247                         opt_inst->removed = true;
248                 }
249         }
250 }
251
252 static int __team_options_register(struct team *team,
253                                    const struct team_option *option,
254                                    size_t option_count)
255 {
256         int i;
257         struct team_option **dst_opts;
258         int err;
259
260         dst_opts = kzalloc(sizeof(struct team_option *) * option_count,
261                            GFP_KERNEL);
262         if (!dst_opts)
263                 return -ENOMEM;
264         for (i = 0; i < option_count; i++, option++) {
265                 if (__team_find_option(team, option->name)) {
266                         err = -EEXIST;
267                         goto alloc_rollback;
268                 }
269                 dst_opts[i] = kmemdup(option, sizeof(*option), GFP_KERNEL);
270                 if (!dst_opts[i]) {
271                         err = -ENOMEM;
272                         goto alloc_rollback;
273                 }
274         }
275
276         for (i = 0; i < option_count; i++) {
277                 err = __team_option_inst_add_option(team, dst_opts[i]);
278                 if (err)
279                         goto inst_rollback;
280                 list_add_tail(&dst_opts[i]->list, &team->option_list);
281         }
282
283         kfree(dst_opts);
284         return 0;
285
286 inst_rollback:
287         for (i--; i >= 0; i--)
288                 __team_option_inst_del_option(team, dst_opts[i]);
289
290         i = option_count - 1;
291 alloc_rollback:
292         for (i--; i >= 0; i--)
293                 kfree(dst_opts[i]);
294
295         kfree(dst_opts);
296         return err;
297 }
298
299 static void __team_options_mark_removed(struct team *team,
300                                         const struct team_option *option,
301                                         size_t option_count)
302 {
303         int i;
304
305         for (i = 0; i < option_count; i++, option++) {
306                 struct team_option *del_opt;
307
308                 del_opt = __team_find_option(team, option->name);
309                 if (del_opt)
310                         __team_option_inst_mark_removed_option(team, del_opt);
311         }
312 }
313
314 static void __team_options_unregister(struct team *team,
315                                       const struct team_option *option,
316                                       size_t option_count)
317 {
318         int i;
319
320         for (i = 0; i < option_count; i++, option++) {
321                 struct team_option *del_opt;
322
323                 del_opt = __team_find_option(team, option->name);
324                 if (del_opt) {
325                         __team_option_inst_del_option(team, del_opt);
326                         list_del(&del_opt->list);
327                         kfree(del_opt);
328                 }
329         }
330 }
331
332 static void __team_options_change_check(struct team *team);
333
334 int team_options_register(struct team *team,
335                           const struct team_option *option,
336                           size_t option_count)
337 {
338         int err;
339
340         err = __team_options_register(team, option, option_count);
341         if (err)
342                 return err;
343         __team_options_change_check(team);
344         return 0;
345 }
346 EXPORT_SYMBOL(team_options_register);
347
348 void team_options_unregister(struct team *team,
349                              const struct team_option *option,
350                              size_t option_count)
351 {
352         __team_options_mark_removed(team, option, option_count);
353         __team_options_change_check(team);
354         __team_options_unregister(team, option, option_count);
355 }
356 EXPORT_SYMBOL(team_options_unregister);
357
358 static int team_option_get(struct team *team,
359                            struct team_option_inst *opt_inst,
360                            struct team_gsetter_ctx *ctx)
361 {
362         if (!opt_inst->option->getter)
363                 return -EOPNOTSUPP;
364         return opt_inst->option->getter(team, ctx);
365 }
366
367 static int team_option_set(struct team *team,
368                            struct team_option_inst *opt_inst,
369                            struct team_gsetter_ctx *ctx)
370 {
371         if (!opt_inst->option->setter)
372                 return -EOPNOTSUPP;
373         return opt_inst->option->setter(team, ctx);
374 }
375
376 void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info)
377 {
378         struct team_option_inst *opt_inst;
379
380         opt_inst = container_of(opt_inst_info, struct team_option_inst, info);
381         opt_inst->changed = true;
382 }
383 EXPORT_SYMBOL(team_option_inst_set_change);
384
385 void team_options_change_check(struct team *team)
386 {
387         __team_options_change_check(team);
388 }
389 EXPORT_SYMBOL(team_options_change_check);
390
391
392 /****************
393  * Mode handling
394  ****************/
395
396 static LIST_HEAD(mode_list);
397 static DEFINE_SPINLOCK(mode_list_lock);
398
399 struct team_mode_item {
400         struct list_head list;
401         const struct team_mode *mode;
402 };
403
404 static struct team_mode_item *__find_mode(const char *kind)
405 {
406         struct team_mode_item *mitem;
407
408         list_for_each_entry(mitem, &mode_list, list) {
409                 if (strcmp(mitem->mode->kind, kind) == 0)
410                         return mitem;
411         }
412         return NULL;
413 }
414
415 static bool is_good_mode_name(const char *name)
416 {
417         while (*name != '\0') {
418                 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
419                         return false;
420                 name++;
421         }
422         return true;
423 }
424
425 int team_mode_register(const struct team_mode *mode)
426 {
427         int err = 0;
428         struct team_mode_item *mitem;
429
430         if (!is_good_mode_name(mode->kind) ||
431             mode->priv_size > TEAM_MODE_PRIV_SIZE)
432                 return -EINVAL;
433
434         mitem = kmalloc(sizeof(*mitem), GFP_KERNEL);
435         if (!mitem)
436                 return -ENOMEM;
437
438         spin_lock(&mode_list_lock);
439         if (__find_mode(mode->kind)) {
440                 err = -EEXIST;
441                 kfree(mitem);
442                 goto unlock;
443         }
444         mitem->mode = mode;
445         list_add_tail(&mitem->list, &mode_list);
446 unlock:
447         spin_unlock(&mode_list_lock);
448         return err;
449 }
450 EXPORT_SYMBOL(team_mode_register);
451
452 void team_mode_unregister(const struct team_mode *mode)
453 {
454         struct team_mode_item *mitem;
455
456         spin_lock(&mode_list_lock);
457         mitem = __find_mode(mode->kind);
458         if (mitem) {
459                 list_del_init(&mitem->list);
460                 kfree(mitem);
461         }
462         spin_unlock(&mode_list_lock);
463 }
464 EXPORT_SYMBOL(team_mode_unregister);
465
466 static const struct team_mode *team_mode_get(const char *kind)
467 {
468         struct team_mode_item *mitem;
469         const struct team_mode *mode = NULL;
470
471         spin_lock(&mode_list_lock);
472         mitem = __find_mode(kind);
473         if (!mitem) {
474                 spin_unlock(&mode_list_lock);
475                 request_module("team-mode-%s", kind);
476                 spin_lock(&mode_list_lock);
477                 mitem = __find_mode(kind);
478         }
479         if (mitem) {
480                 mode = mitem->mode;
481                 if (!try_module_get(mode->owner))
482                         mode = NULL;
483         }
484
485         spin_unlock(&mode_list_lock);
486         return mode;
487 }
488
489 static void team_mode_put(const struct team_mode *mode)
490 {
491         module_put(mode->owner);
492 }
493
494 static bool team_dummy_transmit(struct team *team, struct sk_buff *skb)
495 {
496         dev_kfree_skb_any(skb);
497         return false;
498 }
499
500 static rx_handler_result_t team_dummy_receive(struct team *team,
501                                               struct team_port *port,
502                                               struct sk_buff *skb)
503 {
504         return RX_HANDLER_ANOTHER;
505 }
506
507 static const struct team_mode __team_no_mode = {
508         .kind           = "*NOMODE*",
509 };
510
511 static bool team_is_mode_set(struct team *team)
512 {
513         return team->mode != &__team_no_mode;
514 }
515
516 static void team_set_no_mode(struct team *team)
517 {
518         team->user_carrier_enabled = false;
519         team->mode = &__team_no_mode;
520 }
521
522 static void team_adjust_ops(struct team *team)
523 {
524         /*
525          * To avoid checks in rx/tx skb paths, ensure here that non-null and
526          * correct ops are always set.
527          */
528
529         if (!team->en_port_count || !team_is_mode_set(team) ||
530             !team->mode->ops->transmit)
531                 team->ops.transmit = team_dummy_transmit;
532         else
533                 team->ops.transmit = team->mode->ops->transmit;
534
535         if (!team->en_port_count || !team_is_mode_set(team) ||
536             !team->mode->ops->receive)
537                 team->ops.receive = team_dummy_receive;
538         else
539                 team->ops.receive = team->mode->ops->receive;
540 }
541
542 /*
543  * We can benefit from the fact that it's ensured no port is present
544  * at the time of mode change. Therefore no packets are in fly so there's no
545  * need to set mode operations in any special way.
546  */
547 static int __team_change_mode(struct team *team,
548                               const struct team_mode *new_mode)
549 {
550         /* Check if mode was previously set and do cleanup if so */
551         if (team_is_mode_set(team)) {
552                 void (*exit_op)(struct team *team) = team->ops.exit;
553
554                 /* Clear ops area so no callback is called any longer */
555                 memset(&team->ops, 0, sizeof(struct team_mode_ops));
556                 team_adjust_ops(team);
557
558                 if (exit_op)
559                         exit_op(team);
560                 team_mode_put(team->mode);
561                 team_set_no_mode(team);
562                 /* zero private data area */
563                 memset(&team->mode_priv, 0,
564                        sizeof(struct team) - offsetof(struct team, mode_priv));
565         }
566
567         if (!new_mode)
568                 return 0;
569
570         if (new_mode->ops->init) {
571                 int err;
572
573                 err = new_mode->ops->init(team);
574                 if (err)
575                         return err;
576         }
577
578         team->mode = new_mode;
579         memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops));
580         team_adjust_ops(team);
581
582         return 0;
583 }
584
585 static int team_change_mode(struct team *team, const char *kind)
586 {
587         const struct team_mode *new_mode;
588         struct net_device *dev = team->dev;
589         int err;
590
591         if (!list_empty(&team->port_list)) {
592                 netdev_err(dev, "No ports can be present during mode change\n");
593                 return -EBUSY;
594         }
595
596         if (team_is_mode_set(team) && strcmp(team->mode->kind, kind) == 0) {
597                 netdev_err(dev, "Unable to change to the same mode the team is in\n");
598                 return -EINVAL;
599         }
600
601         new_mode = team_mode_get(kind);
602         if (!new_mode) {
603                 netdev_err(dev, "Mode \"%s\" not found\n", kind);
604                 return -EINVAL;
605         }
606
607         err = __team_change_mode(team, new_mode);
608         if (err) {
609                 netdev_err(dev, "Failed to change to mode \"%s\"\n", kind);
610                 team_mode_put(new_mode);
611                 return err;
612         }
613
614         netdev_info(dev, "Mode changed to \"%s\"\n", kind);
615         return 0;
616 }
617
618
619 /*********************
620  * Peers notification
621  *********************/
622
623 static void team_notify_peers_work(struct work_struct *work)
624 {
625         struct team *team;
626         int val;
627
628         team = container_of(work, struct team, notify_peers.dw.work);
629
630         if (!rtnl_trylock()) {
631                 schedule_delayed_work(&team->notify_peers.dw, 0);
632                 return;
633         }
634         val = atomic_dec_if_positive(&team->notify_peers.count_pending);
635         if (val < 0) {
636                 rtnl_unlock();
637                 return;
638         }
639         call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, team->dev);
640         rtnl_unlock();
641         if (val)
642                 schedule_delayed_work(&team->notify_peers.dw,
643                                       msecs_to_jiffies(team->notify_peers.interval));
644 }
645
646 static void team_notify_peers(struct team *team)
647 {
648         if (!team->notify_peers.count || !netif_running(team->dev))
649                 return;
650         atomic_add(team->notify_peers.count, &team->notify_peers.count_pending);
651         schedule_delayed_work(&team->notify_peers.dw, 0);
652 }
653
654 static void team_notify_peers_init(struct team *team)
655 {
656         INIT_DELAYED_WORK(&team->notify_peers.dw, team_notify_peers_work);
657 }
658
659 static void team_notify_peers_fini(struct team *team)
660 {
661         cancel_delayed_work_sync(&team->notify_peers.dw);
662 }
663
664
665 /*******************************
666  * Send multicast group rejoins
667  *******************************/
668
669 static void team_mcast_rejoin_work(struct work_struct *work)
670 {
671         struct team *team;
672         int val;
673
674         team = container_of(work, struct team, mcast_rejoin.dw.work);
675
676         if (!rtnl_trylock()) {
677                 schedule_delayed_work(&team->mcast_rejoin.dw, 0);
678                 return;
679         }
680         val = atomic_dec_if_positive(&team->mcast_rejoin.count_pending);
681         if (val < 0) {
682                 rtnl_unlock();
683                 return;
684         }
685         call_netdevice_notifiers(NETDEV_RESEND_IGMP, team->dev);
686         rtnl_unlock();
687         if (val)
688                 schedule_delayed_work(&team->mcast_rejoin.dw,
689                                       msecs_to_jiffies(team->mcast_rejoin.interval));
690 }
691
692 static void team_mcast_rejoin(struct team *team)
693 {
694         if (!team->mcast_rejoin.count || !netif_running(team->dev))
695                 return;
696         atomic_add(team->mcast_rejoin.count, &team->mcast_rejoin.count_pending);
697         schedule_delayed_work(&team->mcast_rejoin.dw, 0);
698 }
699
700 static void team_mcast_rejoin_init(struct team *team)
701 {
702         INIT_DELAYED_WORK(&team->mcast_rejoin.dw, team_mcast_rejoin_work);
703 }
704
705 static void team_mcast_rejoin_fini(struct team *team)
706 {
707         cancel_delayed_work_sync(&team->mcast_rejoin.dw);
708 }
709
710
711 /************************
712  * Rx path frame handler
713  ************************/
714
715 /* note: already called with rcu_read_lock */
716 static rx_handler_result_t team_handle_frame(struct sk_buff **pskb)
717 {
718         struct sk_buff *skb = *pskb;
719         struct team_port *port;
720         struct team *team;
721         rx_handler_result_t res;
722
723         skb = skb_share_check(skb, GFP_ATOMIC);
724         if (!skb)
725                 return RX_HANDLER_CONSUMED;
726
727         *pskb = skb;
728
729         port = team_port_get_rcu(skb->dev);
730         team = port->team;
731         if (!team_port_enabled(port)) {
732                 /* allow exact match delivery for disabled ports */
733                 res = RX_HANDLER_EXACT;
734         } else {
735                 res = team->ops.receive(team, port, skb);
736         }
737         if (res == RX_HANDLER_ANOTHER) {
738                 struct team_pcpu_stats *pcpu_stats;
739
740                 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
741                 u64_stats_update_begin(&pcpu_stats->syncp);
742                 pcpu_stats->rx_packets++;
743                 pcpu_stats->rx_bytes += skb->len;
744                 if (skb->pkt_type == PACKET_MULTICAST)
745                         pcpu_stats->rx_multicast++;
746                 u64_stats_update_end(&pcpu_stats->syncp);
747
748                 skb->dev = team->dev;
749         } else {
750                 this_cpu_inc(team->pcpu_stats->rx_dropped);
751         }
752
753         return res;
754 }
755
756
757 /*************************************
758  * Multiqueue Tx port select override
759  *************************************/
760
761 static int team_queue_override_init(struct team *team)
762 {
763         struct list_head *listarr;
764         unsigned int queue_cnt = team->dev->num_tx_queues - 1;
765         unsigned int i;
766
767         if (!queue_cnt)
768                 return 0;
769         listarr = kmalloc(sizeof(struct list_head) * queue_cnt, GFP_KERNEL);
770         if (!listarr)
771                 return -ENOMEM;
772         team->qom_lists = listarr;
773         for (i = 0; i < queue_cnt; i++)
774                 INIT_LIST_HEAD(listarr++);
775         return 0;
776 }
777
778 static void team_queue_override_fini(struct team *team)
779 {
780         kfree(team->qom_lists);
781 }
782
783 static struct list_head *__team_get_qom_list(struct team *team, u16 queue_id)
784 {
785         return &team->qom_lists[queue_id - 1];
786 }
787
788 /*
789  * note: already called with rcu_read_lock
790  */
791 static bool team_queue_override_transmit(struct team *team, struct sk_buff *skb)
792 {
793         struct list_head *qom_list;
794         struct team_port *port;
795
796         if (!team->queue_override_enabled || !skb->queue_mapping)
797                 return false;
798         qom_list = __team_get_qom_list(team, skb->queue_mapping);
799         list_for_each_entry_rcu(port, qom_list, qom_list) {
800                 if (!team_dev_queue_xmit(team, port, skb))
801                         return true;
802         }
803         return false;
804 }
805
806 static void __team_queue_override_port_del(struct team *team,
807                                            struct team_port *port)
808 {
809         if (!port->queue_id)
810                 return;
811         list_del_rcu(&port->qom_list);
812 }
813
814 static bool team_queue_override_port_has_gt_prio_than(struct team_port *port,
815                                                       struct team_port *cur)
816 {
817         if (port->priority < cur->priority)
818                 return true;
819         if (port->priority > cur->priority)
820                 return false;
821         if (port->index < cur->index)
822                 return true;
823         return false;
824 }
825
826 static void __team_queue_override_port_add(struct team *team,
827                                            struct team_port *port)
828 {
829         struct team_port *cur;
830         struct list_head *qom_list;
831         struct list_head *node;
832
833         if (!port->queue_id)
834                 return;
835         qom_list = __team_get_qom_list(team, port->queue_id);
836         node = qom_list;
837         list_for_each_entry(cur, qom_list, qom_list) {
838                 if (team_queue_override_port_has_gt_prio_than(port, cur))
839                         break;
840                 node = &cur->qom_list;
841         }
842         list_add_tail_rcu(&port->qom_list, node);
843 }
844
845 static void __team_queue_override_enabled_check(struct team *team)
846 {
847         struct team_port *port;
848         bool enabled = false;
849
850         list_for_each_entry(port, &team->port_list, list) {
851                 if (port->queue_id) {
852                         enabled = true;
853                         break;
854                 }
855         }
856         if (enabled == team->queue_override_enabled)
857                 return;
858         netdev_dbg(team->dev, "%s queue override\n",
859                    enabled ? "Enabling" : "Disabling");
860         team->queue_override_enabled = enabled;
861 }
862
863 static void team_queue_override_port_prio_changed(struct team *team,
864                                                   struct team_port *port)
865 {
866         if (!port->queue_id || team_port_enabled(port))
867                 return;
868         __team_queue_override_port_del(team, port);
869         __team_queue_override_port_add(team, port);
870         __team_queue_override_enabled_check(team);
871 }
872
873 static void team_queue_override_port_change_queue_id(struct team *team,
874                                                      struct team_port *port,
875                                                      u16 new_queue_id)
876 {
877         if (team_port_enabled(port)) {
878                 __team_queue_override_port_del(team, port);
879                 port->queue_id = new_queue_id;
880                 __team_queue_override_port_add(team, port);
881                 __team_queue_override_enabled_check(team);
882         } else {
883                 port->queue_id = new_queue_id;
884         }
885 }
886
887 static void team_queue_override_port_add(struct team *team,
888                                          struct team_port *port)
889 {
890         __team_queue_override_port_add(team, port);
891         __team_queue_override_enabled_check(team);
892 }
893
894 static void team_queue_override_port_del(struct team *team,
895                                          struct team_port *port)
896 {
897         __team_queue_override_port_del(team, port);
898         __team_queue_override_enabled_check(team);
899 }
900
901
902 /****************
903  * Port handling
904  ****************/
905
906 static bool team_port_find(const struct team *team,
907                            const struct team_port *port)
908 {
909         struct team_port *cur;
910
911         list_for_each_entry(cur, &team->port_list, list)
912                 if (cur == port)
913                         return true;
914         return false;
915 }
916
917 /*
918  * Enable/disable port by adding to enabled port hashlist and setting
919  * port->index (Might be racy so reader could see incorrect ifindex when
920  * processing a flying packet, but that is not a problem). Write guarded
921  * by team->lock.
922  */
923 static void team_port_enable(struct team *team,
924                              struct team_port *port)
925 {
926         if (team_port_enabled(port))
927                 return;
928         port->index = team->en_port_count++;
929         hlist_add_head_rcu(&port->hlist,
930                            team_port_index_hash(team, port->index));
931         team_adjust_ops(team);
932         team_queue_override_port_add(team, port);
933         if (team->ops.port_enabled)
934                 team->ops.port_enabled(team, port);
935         team_notify_peers(team);
936         team_mcast_rejoin(team);
937 }
938
939 static void __reconstruct_port_hlist(struct team *team, int rm_index)
940 {
941         int i;
942         struct team_port *port;
943
944         for (i = rm_index + 1; i < team->en_port_count; i++) {
945                 port = team_get_port_by_index(team, i);
946                 hlist_del_rcu(&port->hlist);
947                 port->index--;
948                 hlist_add_head_rcu(&port->hlist,
949                                    team_port_index_hash(team, port->index));
950         }
951 }
952
953 static void team_port_disable(struct team *team,
954                               struct team_port *port)
955 {
956         if (!team_port_enabled(port))
957                 return;
958         if (team->ops.port_disabled)
959                 team->ops.port_disabled(team, port);
960         hlist_del_rcu(&port->hlist);
961         __reconstruct_port_hlist(team, port->index);
962         port->index = -1;
963         team->en_port_count--;
964         team_queue_override_port_del(team, port);
965         team_adjust_ops(team);
966         team_notify_peers(team);
967         team_mcast_rejoin(team);
968 }
969
970 #define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \
971                             NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
972                             NETIF_F_HIGHDMA | NETIF_F_LRO)
973
974 static void __team_compute_features(struct team *team)
975 {
976         struct team_port *port;
977         u32 vlan_features = TEAM_VLAN_FEATURES & NETIF_F_ALL_FOR_ALL;
978         unsigned short max_hard_header_len = ETH_HLEN;
979         unsigned int dst_release_flag = IFF_XMIT_DST_RELEASE |
980                                         IFF_XMIT_DST_RELEASE_PERM;
981
982         list_for_each_entry(port, &team->port_list, list) {
983                 vlan_features = netdev_increment_features(vlan_features,
984                                         port->dev->vlan_features,
985                                         TEAM_VLAN_FEATURES);
986
987                 dst_release_flag &= port->dev->priv_flags;
988                 if (port->dev->hard_header_len > max_hard_header_len)
989                         max_hard_header_len = port->dev->hard_header_len;
990         }
991
992         team->dev->vlan_features = vlan_features;
993         team->dev->hard_header_len = max_hard_header_len;
994
995         team->dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
996         if (dst_release_flag == (IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM))
997                 team->dev->priv_flags |= IFF_XMIT_DST_RELEASE;
998
999         netdev_change_features(team->dev);
1000 }
1001
1002 static void team_compute_features(struct team *team)
1003 {
1004         mutex_lock(&team->lock);
1005         __team_compute_features(team);
1006         mutex_unlock(&team->lock);
1007 }
1008
1009 static int team_port_enter(struct team *team, struct team_port *port)
1010 {
1011         int err = 0;
1012
1013         dev_hold(team->dev);
1014         if (team->ops.port_enter) {
1015                 err = team->ops.port_enter(team, port);
1016                 if (err) {
1017                         netdev_err(team->dev, "Device %s failed to enter team mode\n",
1018                                    port->dev->name);
1019                         goto err_port_enter;
1020                 }
1021         }
1022
1023         return 0;
1024
1025 err_port_enter:
1026         dev_put(team->dev);
1027
1028         return err;
1029 }
1030
1031 static void team_port_leave(struct team *team, struct team_port *port)
1032 {
1033         if (team->ops.port_leave)
1034                 team->ops.port_leave(team, port);
1035         dev_put(team->dev);
1036 }
1037
1038 #ifdef CONFIG_NET_POLL_CONTROLLER
1039 static int team_port_enable_netpoll(struct team *team, struct team_port *port)
1040 {
1041         struct netpoll *np;
1042         int err;
1043
1044         if (!team->dev->npinfo)
1045                 return 0;
1046
1047         np = kzalloc(sizeof(*np), GFP_KERNEL);
1048         if (!np)
1049                 return -ENOMEM;
1050
1051         err = __netpoll_setup(np, port->dev);
1052         if (err) {
1053                 kfree(np);
1054                 return err;
1055         }
1056         port->np = np;
1057         return err;
1058 }
1059
1060 static void team_port_disable_netpoll(struct team_port *port)
1061 {
1062         struct netpoll *np = port->np;
1063
1064         if (!np)
1065                 return;
1066         port->np = NULL;
1067
1068         /* Wait for transmitting packets to finish before freeing. */
1069         synchronize_rcu_bh();
1070         __netpoll_cleanup(np);
1071         kfree(np);
1072 }
1073 #else
1074 static int team_port_enable_netpoll(struct team *team, struct team_port *port)
1075 {
1076         return 0;
1077 }
1078 static void team_port_disable_netpoll(struct team_port *port)
1079 {
1080 }
1081 #endif
1082
1083 static int team_upper_dev_link(struct net_device *dev,
1084                                struct net_device *port_dev)
1085 {
1086         int err;
1087
1088         err = netdev_master_upper_dev_link(port_dev, dev);
1089         if (err)
1090                 return err;
1091         port_dev->priv_flags |= IFF_TEAM_PORT;
1092         return 0;
1093 }
1094
1095 static void team_upper_dev_unlink(struct net_device *dev,
1096                                   struct net_device *port_dev)
1097 {
1098         netdev_upper_dev_unlink(port_dev, dev);
1099         port_dev->priv_flags &= ~IFF_TEAM_PORT;
1100 }
1101
1102 static void __team_port_change_port_added(struct team_port *port, bool linkup);
1103 static int team_dev_type_check_change(struct net_device *dev,
1104                                       struct net_device *port_dev);
1105
1106 static int team_port_add(struct team *team, struct net_device *port_dev)
1107 {
1108         struct net_device *dev = team->dev;
1109         struct team_port *port;
1110         char *portname = port_dev->name;
1111         int err;
1112
1113         if (port_dev->flags & IFF_LOOPBACK) {
1114                 netdev_err(dev, "Device %s is loopback device. Loopback devices can't be added as a team port\n",
1115                            portname);
1116                 return -EINVAL;
1117         }
1118
1119         if (team_port_exists(port_dev)) {
1120                 netdev_err(dev, "Device %s is already a port "
1121                                 "of a team device\n", portname);
1122                 return -EBUSY;
1123         }
1124
1125         if (port_dev->features & NETIF_F_VLAN_CHALLENGED &&
1126             vlan_uses_dev(dev)) {
1127                 netdev_err(dev, "Device %s is VLAN challenged and team device has VLAN set up\n",
1128                            portname);
1129                 return -EPERM;
1130         }
1131
1132         err = team_dev_type_check_change(dev, port_dev);
1133         if (err)
1134                 return err;
1135
1136         if (port_dev->flags & IFF_UP) {
1137                 netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
1138                            portname);
1139                 return -EBUSY;
1140         }
1141
1142         port = kzalloc(sizeof(struct team_port) + team->mode->port_priv_size,
1143                        GFP_KERNEL);
1144         if (!port)
1145                 return -ENOMEM;
1146
1147         port->dev = port_dev;
1148         port->team = team;
1149         INIT_LIST_HEAD(&port->qom_list);
1150
1151         port->orig.mtu = port_dev->mtu;
1152         err = dev_set_mtu(port_dev, dev->mtu);
1153         if (err) {
1154                 netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
1155                 goto err_set_mtu;
1156         }
1157
1158         memcpy(port->orig.dev_addr, port_dev->dev_addr, port_dev->addr_len);
1159
1160         err = team_port_enter(team, port);
1161         if (err) {
1162                 netdev_err(dev, "Device %s failed to enter team mode\n",
1163                            portname);
1164                 goto err_port_enter;
1165         }
1166
1167         err = dev_open(port_dev);
1168         if (err) {
1169                 netdev_dbg(dev, "Device %s opening failed\n",
1170                            portname);
1171                 goto err_dev_open;
1172         }
1173
1174         err = vlan_vids_add_by_dev(port_dev, dev);
1175         if (err) {
1176                 netdev_err(dev, "Failed to add vlan ids to device %s\n",
1177                                 portname);
1178                 goto err_vids_add;
1179         }
1180
1181         err = team_port_enable_netpoll(team, port);
1182         if (err) {
1183                 netdev_err(dev, "Failed to enable netpoll on device %s\n",
1184                            portname);
1185                 goto err_enable_netpoll;
1186         }
1187
1188         if (!(dev->features & NETIF_F_LRO))
1189                 dev_disable_lro(port_dev);
1190
1191         err = netdev_rx_handler_register(port_dev, team_handle_frame,
1192                                          port);
1193         if (err) {
1194                 netdev_err(dev, "Device %s failed to register rx_handler\n",
1195                            portname);
1196                 goto err_handler_register;
1197         }
1198
1199         err = team_upper_dev_link(dev, port_dev);
1200         if (err) {
1201                 netdev_err(dev, "Device %s failed to set upper link\n",
1202                            portname);
1203                 goto err_set_upper_link;
1204         }
1205
1206         err = __team_option_inst_add_port(team, port);
1207         if (err) {
1208                 netdev_err(dev, "Device %s failed to add per-port options\n",
1209                            portname);
1210                 goto err_option_port_add;
1211         }
1212
1213         port->index = -1;
1214         list_add_tail_rcu(&port->list, &team->port_list);
1215         team_port_enable(team, port);
1216         __team_compute_features(team);
1217         __team_port_change_port_added(port, !!netif_carrier_ok(port_dev));
1218         __team_options_change_check(team);
1219
1220         netdev_info(dev, "Port device %s added\n", portname);
1221
1222         return 0;
1223
1224 err_option_port_add:
1225         team_upper_dev_unlink(dev, port_dev);
1226
1227 err_set_upper_link:
1228         netdev_rx_handler_unregister(port_dev);
1229
1230 err_handler_register:
1231         team_port_disable_netpoll(port);
1232
1233 err_enable_netpoll:
1234         vlan_vids_del_by_dev(port_dev, dev);
1235
1236 err_vids_add:
1237         dev_close(port_dev);
1238
1239 err_dev_open:
1240         team_port_leave(team, port);
1241         team_port_set_orig_dev_addr(port);
1242
1243 err_port_enter:
1244         dev_set_mtu(port_dev, port->orig.mtu);
1245
1246 err_set_mtu:
1247         kfree(port);
1248
1249         return err;
1250 }
1251
1252 static void __team_port_change_port_removed(struct team_port *port);
1253
1254 static int team_port_del(struct team *team, struct net_device *port_dev)
1255 {
1256         struct net_device *dev = team->dev;
1257         struct team_port *port;
1258         char *portname = port_dev->name;
1259
1260         port = team_port_get_rtnl(port_dev);
1261         if (!port || !team_port_find(team, port)) {
1262                 netdev_err(dev, "Device %s does not act as a port of this team\n",
1263                            portname);
1264                 return -ENOENT;
1265         }
1266
1267         team_port_disable(team, port);
1268         list_del_rcu(&port->list);
1269         team_upper_dev_unlink(dev, port_dev);
1270         netdev_rx_handler_unregister(port_dev);
1271         team_port_disable_netpoll(port);
1272         vlan_vids_del_by_dev(port_dev, dev);
1273         dev_uc_unsync(port_dev, dev);
1274         dev_mc_unsync(port_dev, dev);
1275         dev_close(port_dev);
1276         team_port_leave(team, port);
1277
1278         __team_option_inst_mark_removed_port(team, port);
1279         __team_options_change_check(team);
1280         __team_option_inst_del_port(team, port);
1281         __team_port_change_port_removed(port);
1282
1283         team_port_set_orig_dev_addr(port);
1284         dev_set_mtu(port_dev, port->orig.mtu);
1285         kfree_rcu(port, rcu);
1286         netdev_info(dev, "Port device %s removed\n", portname);
1287         __team_compute_features(team);
1288
1289         return 0;
1290 }
1291
1292
1293 /*****************
1294  * Net device ops
1295  *****************/
1296
1297 static int team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx)
1298 {
1299         ctx->data.str_val = team->mode->kind;
1300         return 0;
1301 }
1302
1303 static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx)
1304 {
1305         return team_change_mode(team, ctx->data.str_val);
1306 }
1307
1308 static int team_notify_peers_count_get(struct team *team,
1309                                        struct team_gsetter_ctx *ctx)
1310 {
1311         ctx->data.u32_val = team->notify_peers.count;
1312         return 0;
1313 }
1314
1315 static int team_notify_peers_count_set(struct team *team,
1316                                        struct team_gsetter_ctx *ctx)
1317 {
1318         team->notify_peers.count = ctx->data.u32_val;
1319         return 0;
1320 }
1321
1322 static int team_notify_peers_interval_get(struct team *team,
1323                                           struct team_gsetter_ctx *ctx)
1324 {
1325         ctx->data.u32_val = team->notify_peers.interval;
1326         return 0;
1327 }
1328
1329 static int team_notify_peers_interval_set(struct team *team,
1330                                           struct team_gsetter_ctx *ctx)
1331 {
1332         team->notify_peers.interval = ctx->data.u32_val;
1333         return 0;
1334 }
1335
1336 static int team_mcast_rejoin_count_get(struct team *team,
1337                                        struct team_gsetter_ctx *ctx)
1338 {
1339         ctx->data.u32_val = team->mcast_rejoin.count;
1340         return 0;
1341 }
1342
1343 static int team_mcast_rejoin_count_set(struct team *team,
1344                                        struct team_gsetter_ctx *ctx)
1345 {
1346         team->mcast_rejoin.count = ctx->data.u32_val;
1347         return 0;
1348 }
1349
1350 static int team_mcast_rejoin_interval_get(struct team *team,
1351                                           struct team_gsetter_ctx *ctx)
1352 {
1353         ctx->data.u32_val = team->mcast_rejoin.interval;
1354         return 0;
1355 }
1356
1357 static int team_mcast_rejoin_interval_set(struct team *team,
1358                                           struct team_gsetter_ctx *ctx)
1359 {
1360         team->mcast_rejoin.interval = ctx->data.u32_val;
1361         return 0;
1362 }
1363
1364 static int team_port_en_option_get(struct team *team,
1365                                    struct team_gsetter_ctx *ctx)
1366 {
1367         struct team_port *port = ctx->info->port;
1368
1369         ctx->data.bool_val = team_port_enabled(port);
1370         return 0;
1371 }
1372
1373 static int team_port_en_option_set(struct team *team,
1374                                    struct team_gsetter_ctx *ctx)
1375 {
1376         struct team_port *port = ctx->info->port;
1377
1378         if (ctx->data.bool_val)
1379                 team_port_enable(team, port);
1380         else
1381                 team_port_disable(team, port);
1382         return 0;
1383 }
1384
1385 static int team_user_linkup_option_get(struct team *team,
1386                                        struct team_gsetter_ctx *ctx)
1387 {
1388         struct team_port *port = ctx->info->port;
1389
1390         ctx->data.bool_val = port->user.linkup;
1391         return 0;
1392 }
1393
1394 static void __team_carrier_check(struct team *team);
1395
1396 static int team_user_linkup_option_set(struct team *team,
1397                                        struct team_gsetter_ctx *ctx)
1398 {
1399         struct team_port *port = ctx->info->port;
1400
1401         port->user.linkup = ctx->data.bool_val;
1402         team_refresh_port_linkup(port);
1403         __team_carrier_check(port->team);
1404         return 0;
1405 }
1406
1407 static int team_user_linkup_en_option_get(struct team *team,
1408                                           struct team_gsetter_ctx *ctx)
1409 {
1410         struct team_port *port = ctx->info->port;
1411
1412         ctx->data.bool_val = port->user.linkup_enabled;
1413         return 0;
1414 }
1415
1416 static int team_user_linkup_en_option_set(struct team *team,
1417                                           struct team_gsetter_ctx *ctx)
1418 {
1419         struct team_port *port = ctx->info->port;
1420
1421         port->user.linkup_enabled = ctx->data.bool_val;
1422         team_refresh_port_linkup(port);
1423         __team_carrier_check(port->team);
1424         return 0;
1425 }
1426
1427 static int team_priority_option_get(struct team *team,
1428                                     struct team_gsetter_ctx *ctx)
1429 {
1430         struct team_port *port = ctx->info->port;
1431
1432         ctx->data.s32_val = port->priority;
1433         return 0;
1434 }
1435
1436 static int team_priority_option_set(struct team *team,
1437                                     struct team_gsetter_ctx *ctx)
1438 {
1439         struct team_port *port = ctx->info->port;
1440         s32 priority = ctx->data.s32_val;
1441
1442         if (port->priority == priority)
1443                 return 0;
1444         port->priority = priority;
1445         team_queue_override_port_prio_changed(team, port);
1446         return 0;
1447 }
1448
1449 static int team_queue_id_option_get(struct team *team,
1450                                     struct team_gsetter_ctx *ctx)
1451 {
1452         struct team_port *port = ctx->info->port;
1453
1454         ctx->data.u32_val = port->queue_id;
1455         return 0;
1456 }
1457
1458 static int team_queue_id_option_set(struct team *team,
1459                                     struct team_gsetter_ctx *ctx)
1460 {
1461         struct team_port *port = ctx->info->port;
1462         u16 new_queue_id = ctx->data.u32_val;
1463
1464         if (port->queue_id == new_queue_id)
1465                 return 0;
1466         if (new_queue_id >= team->dev->real_num_tx_queues)
1467                 return -EINVAL;
1468         team_queue_override_port_change_queue_id(team, port, new_queue_id);
1469         return 0;
1470 }
1471
1472 static const struct team_option team_options[] = {
1473         {
1474                 .name = "mode",
1475                 .type = TEAM_OPTION_TYPE_STRING,
1476                 .getter = team_mode_option_get,
1477                 .setter = team_mode_option_set,
1478         },
1479         {
1480                 .name = "notify_peers_count",
1481                 .type = TEAM_OPTION_TYPE_U32,
1482                 .getter = team_notify_peers_count_get,
1483                 .setter = team_notify_peers_count_set,
1484         },
1485         {
1486                 .name = "notify_peers_interval",
1487                 .type = TEAM_OPTION_TYPE_U32,
1488                 .getter = team_notify_peers_interval_get,
1489                 .setter = team_notify_peers_interval_set,
1490         },
1491         {
1492                 .name = "mcast_rejoin_count",
1493                 .type = TEAM_OPTION_TYPE_U32,
1494                 .getter = team_mcast_rejoin_count_get,
1495                 .setter = team_mcast_rejoin_count_set,
1496         },
1497         {
1498                 .name = "mcast_rejoin_interval",
1499                 .type = TEAM_OPTION_TYPE_U32,
1500                 .getter = team_mcast_rejoin_interval_get,
1501                 .setter = team_mcast_rejoin_interval_set,
1502         },
1503         {
1504                 .name = "enabled",
1505                 .type = TEAM_OPTION_TYPE_BOOL,
1506                 .per_port = true,
1507                 .getter = team_port_en_option_get,
1508                 .setter = team_port_en_option_set,
1509         },
1510         {
1511                 .name = "user_linkup",
1512                 .type = TEAM_OPTION_TYPE_BOOL,
1513                 .per_port = true,
1514                 .getter = team_user_linkup_option_get,
1515                 .setter = team_user_linkup_option_set,
1516         },
1517         {
1518                 .name = "user_linkup_enabled",
1519                 .type = TEAM_OPTION_TYPE_BOOL,
1520                 .per_port = true,
1521                 .getter = team_user_linkup_en_option_get,
1522                 .setter = team_user_linkup_en_option_set,
1523         },
1524         {
1525                 .name = "priority",
1526                 .type = TEAM_OPTION_TYPE_S32,
1527                 .per_port = true,
1528                 .getter = team_priority_option_get,
1529                 .setter = team_priority_option_set,
1530         },
1531         {
1532                 .name = "queue_id",
1533                 .type = TEAM_OPTION_TYPE_U32,
1534                 .per_port = true,
1535                 .getter = team_queue_id_option_get,
1536                 .setter = team_queue_id_option_set,
1537         },
1538 };
1539
1540 static struct lock_class_key team_netdev_xmit_lock_key;
1541 static struct lock_class_key team_netdev_addr_lock_key;
1542 static struct lock_class_key team_tx_busylock_key;
1543
1544 static void team_set_lockdep_class_one(struct net_device *dev,
1545                                        struct netdev_queue *txq,
1546                                        void *unused)
1547 {
1548         lockdep_set_class(&txq->_xmit_lock, &team_netdev_xmit_lock_key);
1549 }
1550
1551 static void team_set_lockdep_class(struct net_device *dev)
1552 {
1553         lockdep_set_class(&dev->addr_list_lock, &team_netdev_addr_lock_key);
1554         netdev_for_each_tx_queue(dev, team_set_lockdep_class_one, NULL);
1555         dev->qdisc_tx_busylock = &team_tx_busylock_key;
1556 }
1557
1558 static int team_init(struct net_device *dev)
1559 {
1560         struct team *team = netdev_priv(dev);
1561         int i;
1562         int err;
1563
1564         team->dev = dev;
1565         mutex_init(&team->lock);
1566         team_set_no_mode(team);
1567
1568         team->pcpu_stats = netdev_alloc_pcpu_stats(struct team_pcpu_stats);
1569         if (!team->pcpu_stats)
1570                 return -ENOMEM;
1571
1572         for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
1573                 INIT_HLIST_HEAD(&team->en_port_hlist[i]);
1574         INIT_LIST_HEAD(&team->port_list);
1575         err = team_queue_override_init(team);
1576         if (err)
1577                 goto err_team_queue_override_init;
1578
1579         team_adjust_ops(team);
1580
1581         INIT_LIST_HEAD(&team->option_list);
1582         INIT_LIST_HEAD(&team->option_inst_list);
1583
1584         team_notify_peers_init(team);
1585         team_mcast_rejoin_init(team);
1586
1587         err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
1588         if (err)
1589                 goto err_options_register;
1590         netif_carrier_off(dev);
1591
1592         team_set_lockdep_class(dev);
1593
1594         return 0;
1595
1596 err_options_register:
1597         team_mcast_rejoin_fini(team);
1598         team_notify_peers_fini(team);
1599         team_queue_override_fini(team);
1600 err_team_queue_override_init:
1601         free_percpu(team->pcpu_stats);
1602
1603         return err;
1604 }
1605
1606 static void team_uninit(struct net_device *dev)
1607 {
1608         struct team *team = netdev_priv(dev);
1609         struct team_port *port;
1610         struct team_port *tmp;
1611
1612         mutex_lock(&team->lock);
1613         list_for_each_entry_safe(port, tmp, &team->port_list, list)
1614                 team_port_del(team, port->dev);
1615
1616         __team_change_mode(team, NULL); /* cleanup */
1617         __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
1618         team_mcast_rejoin_fini(team);
1619         team_notify_peers_fini(team);
1620         team_queue_override_fini(team);
1621         mutex_unlock(&team->lock);
1622 }
1623
1624 static void team_destructor(struct net_device *dev)
1625 {
1626         struct team *team = netdev_priv(dev);
1627
1628         free_percpu(team->pcpu_stats);
1629         free_netdev(dev);
1630 }
1631
1632 static int team_open(struct net_device *dev)
1633 {
1634         return 0;
1635 }
1636
1637 static int team_close(struct net_device *dev)
1638 {
1639         return 0;
1640 }
1641
1642 /*
1643  * note: already called with rcu_read_lock
1644  */
1645 static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
1646 {
1647         struct team *team = netdev_priv(dev);
1648         bool tx_success;
1649         unsigned int len = skb->len;
1650
1651         tx_success = team_queue_override_transmit(team, skb);
1652         if (!tx_success)
1653                 tx_success = team->ops.transmit(team, skb);
1654         if (tx_success) {
1655                 struct team_pcpu_stats *pcpu_stats;
1656
1657                 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
1658                 u64_stats_update_begin(&pcpu_stats->syncp);
1659                 pcpu_stats->tx_packets++;
1660                 pcpu_stats->tx_bytes += len;
1661                 u64_stats_update_end(&pcpu_stats->syncp);
1662         } else {
1663                 this_cpu_inc(team->pcpu_stats->tx_dropped);
1664         }
1665
1666         return NETDEV_TX_OK;
1667 }
1668
1669 static u16 team_select_queue(struct net_device *dev, struct sk_buff *skb,
1670                              void *accel_priv, select_queue_fallback_t fallback)
1671 {
1672         /*
1673          * This helper function exists to help dev_pick_tx get the correct
1674          * destination queue.  Using a helper function skips a call to
1675          * skb_tx_hash and will put the skbs in the queue we expect on their
1676          * way down to the team driver.
1677          */
1678         u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
1679
1680         /*
1681          * Save the original txq to restore before passing to the driver
1682          */
1683         qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
1684
1685         if (unlikely(txq >= dev->real_num_tx_queues)) {
1686                 do {
1687                         txq -= dev->real_num_tx_queues;
1688                 } while (txq >= dev->real_num_tx_queues);
1689         }
1690         return txq;
1691 }
1692
1693 static void team_change_rx_flags(struct net_device *dev, int change)
1694 {
1695         struct team *team = netdev_priv(dev);
1696         struct team_port *port;
1697         int inc;
1698
1699         rcu_read_lock();
1700         list_for_each_entry_rcu(port, &team->port_list, list) {
1701                 if (change & IFF_PROMISC) {
1702                         inc = dev->flags & IFF_PROMISC ? 1 : -1;
1703                         dev_set_promiscuity(port->dev, inc);
1704                 }
1705                 if (change & IFF_ALLMULTI) {
1706                         inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
1707                         dev_set_allmulti(port->dev, inc);
1708                 }
1709         }
1710         rcu_read_unlock();
1711 }
1712
1713 static void team_set_rx_mode(struct net_device *dev)
1714 {
1715         struct team *team = netdev_priv(dev);
1716         struct team_port *port;
1717
1718         rcu_read_lock();
1719         list_for_each_entry_rcu(port, &team->port_list, list) {
1720                 dev_uc_sync_multiple(port->dev, dev);
1721                 dev_mc_sync_multiple(port->dev, dev);
1722         }
1723         rcu_read_unlock();
1724 }
1725
1726 static int team_set_mac_address(struct net_device *dev, void *p)
1727 {
1728         struct sockaddr *addr = p;
1729         struct team *team = netdev_priv(dev);
1730         struct team_port *port;
1731
1732         if (dev->type == ARPHRD_ETHER && !is_valid_ether_addr(addr->sa_data))
1733                 return -EADDRNOTAVAIL;
1734         memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1735         rcu_read_lock();
1736         list_for_each_entry_rcu(port, &team->port_list, list)
1737                 if (team->ops.port_change_dev_addr)
1738                         team->ops.port_change_dev_addr(team, port);
1739         rcu_read_unlock();
1740         return 0;
1741 }
1742
1743 static int team_change_mtu(struct net_device *dev, int new_mtu)
1744 {
1745         struct team *team = netdev_priv(dev);
1746         struct team_port *port;
1747         int err;
1748
1749         /*
1750          * Alhough this is reader, it's guarded by team lock. It's not possible
1751          * to traverse list in reverse under rcu_read_lock
1752          */
1753         mutex_lock(&team->lock);
1754         team->port_mtu_change_allowed = true;
1755         list_for_each_entry(port, &team->port_list, list) {
1756                 err = dev_set_mtu(port->dev, new_mtu);
1757                 if (err) {
1758                         netdev_err(dev, "Device %s failed to change mtu",
1759                                    port->dev->name);
1760                         goto unwind;
1761                 }
1762         }
1763         team->port_mtu_change_allowed = false;
1764         mutex_unlock(&team->lock);
1765
1766         dev->mtu = new_mtu;
1767
1768         return 0;
1769
1770 unwind:
1771         list_for_each_entry_continue_reverse(port, &team->port_list, list)
1772                 dev_set_mtu(port->dev, dev->mtu);
1773         team->port_mtu_change_allowed = false;
1774         mutex_unlock(&team->lock);
1775
1776         return err;
1777 }
1778
1779 static struct rtnl_link_stats64 *
1780 team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1781 {
1782         struct team *team = netdev_priv(dev);
1783         struct team_pcpu_stats *p;
1784         u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
1785         u32 rx_dropped = 0, tx_dropped = 0;
1786         unsigned int start;
1787         int i;
1788
1789         for_each_possible_cpu(i) {
1790                 p = per_cpu_ptr(team->pcpu_stats, i);
1791                 do {
1792                         start = u64_stats_fetch_begin_irq(&p->syncp);
1793                         rx_packets      = p->rx_packets;
1794                         rx_bytes        = p->rx_bytes;
1795                         rx_multicast    = p->rx_multicast;
1796                         tx_packets      = p->tx_packets;
1797                         tx_bytes        = p->tx_bytes;
1798                 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
1799
1800                 stats->rx_packets       += rx_packets;
1801                 stats->rx_bytes         += rx_bytes;
1802                 stats->multicast        += rx_multicast;
1803                 stats->tx_packets       += tx_packets;
1804                 stats->tx_bytes         += tx_bytes;
1805                 /*
1806                  * rx_dropped & tx_dropped are u32, updated
1807                  * without syncp protection.
1808                  */
1809                 rx_dropped      += p->rx_dropped;
1810                 tx_dropped      += p->tx_dropped;
1811         }
1812         stats->rx_dropped       = rx_dropped;
1813         stats->tx_dropped       = tx_dropped;
1814         return stats;
1815 }
1816
1817 static int team_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
1818 {
1819         struct team *team = netdev_priv(dev);
1820         struct team_port *port;
1821         int err;
1822
1823         /*
1824          * Alhough this is reader, it's guarded by team lock. It's not possible
1825          * to traverse list in reverse under rcu_read_lock
1826          */
1827         mutex_lock(&team->lock);
1828         list_for_each_entry(port, &team->port_list, list) {
1829                 err = vlan_vid_add(port->dev, proto, vid);
1830                 if (err)
1831                         goto unwind;
1832         }
1833         mutex_unlock(&team->lock);
1834
1835         return 0;
1836
1837 unwind:
1838         list_for_each_entry_continue_reverse(port, &team->port_list, list)
1839                 vlan_vid_del(port->dev, proto, vid);
1840         mutex_unlock(&team->lock);
1841
1842         return err;
1843 }
1844
1845 static int team_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
1846 {
1847         struct team *team = netdev_priv(dev);
1848         struct team_port *port;
1849
1850         rcu_read_lock();
1851         list_for_each_entry_rcu(port, &team->port_list, list)
1852                 vlan_vid_del(port->dev, proto, vid);
1853         rcu_read_unlock();
1854
1855         return 0;
1856 }
1857
1858 #ifdef CONFIG_NET_POLL_CONTROLLER
1859 static void team_poll_controller(struct net_device *dev)
1860 {
1861 }
1862
1863 static void __team_netpoll_cleanup(struct team *team)
1864 {
1865         struct team_port *port;
1866
1867         list_for_each_entry(port, &team->port_list, list)
1868                 team_port_disable_netpoll(port);
1869 }
1870
1871 static void team_netpoll_cleanup(struct net_device *dev)
1872 {
1873         struct team *team = netdev_priv(dev);
1874
1875         mutex_lock(&team->lock);
1876         __team_netpoll_cleanup(team);
1877         mutex_unlock(&team->lock);
1878 }
1879
1880 static int team_netpoll_setup(struct net_device *dev,
1881                               struct netpoll_info *npifo)
1882 {
1883         struct team *team = netdev_priv(dev);
1884         struct team_port *port;
1885         int err = 0;
1886
1887         mutex_lock(&team->lock);
1888         list_for_each_entry(port, &team->port_list, list) {
1889                 err = team_port_enable_netpoll(team, port);
1890                 if (err) {
1891                         __team_netpoll_cleanup(team);
1892                         break;
1893                 }
1894         }
1895         mutex_unlock(&team->lock);
1896         return err;
1897 }
1898 #endif
1899
1900 static int team_add_slave(struct net_device *dev, struct net_device *port_dev)
1901 {
1902         struct team *team = netdev_priv(dev);
1903         int err;
1904
1905         mutex_lock(&team->lock);
1906         err = team_port_add(team, port_dev);
1907         mutex_unlock(&team->lock);
1908         return err;
1909 }
1910
1911 static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
1912 {
1913         struct team *team = netdev_priv(dev);
1914         int err;
1915
1916         mutex_lock(&team->lock);
1917         err = team_port_del(team, port_dev);
1918         mutex_unlock(&team->lock);
1919         return err;
1920 }
1921
1922 static netdev_features_t team_fix_features(struct net_device *dev,
1923                                            netdev_features_t features)
1924 {
1925         struct team_port *port;
1926         struct team *team = netdev_priv(dev);
1927         netdev_features_t mask;
1928
1929         mask = features | NETIF_F_HW_SWITCH_OFFLOAD;
1930         features &= ~NETIF_F_ONE_FOR_ALL;
1931         features |= NETIF_F_ALL_FOR_ALL;
1932
1933         rcu_read_lock();
1934         list_for_each_entry_rcu(port, &team->port_list, list) {
1935                 features = netdev_increment_features(features,
1936                                                      port->dev->features,
1937                                                      mask);
1938         }
1939         rcu_read_unlock();
1940         return features;
1941 }
1942
1943 static int team_change_carrier(struct net_device *dev, bool new_carrier)
1944 {
1945         struct team *team = netdev_priv(dev);
1946
1947         team->user_carrier_enabled = true;
1948
1949         if (new_carrier)
1950                 netif_carrier_on(dev);
1951         else
1952                 netif_carrier_off(dev);
1953         return 0;
1954 }
1955
1956 static const struct net_device_ops team_netdev_ops = {
1957         .ndo_init               = team_init,
1958         .ndo_uninit             = team_uninit,
1959         .ndo_open               = team_open,
1960         .ndo_stop               = team_close,
1961         .ndo_start_xmit         = team_xmit,
1962         .ndo_select_queue       = team_select_queue,
1963         .ndo_change_rx_flags    = team_change_rx_flags,
1964         .ndo_set_rx_mode        = team_set_rx_mode,
1965         .ndo_set_mac_address    = team_set_mac_address,
1966         .ndo_change_mtu         = team_change_mtu,
1967         .ndo_get_stats64        = team_get_stats64,
1968         .ndo_vlan_rx_add_vid    = team_vlan_rx_add_vid,
1969         .ndo_vlan_rx_kill_vid   = team_vlan_rx_kill_vid,
1970 #ifdef CONFIG_NET_POLL_CONTROLLER
1971         .ndo_poll_controller    = team_poll_controller,
1972         .ndo_netpoll_setup      = team_netpoll_setup,
1973         .ndo_netpoll_cleanup    = team_netpoll_cleanup,
1974 #endif
1975         .ndo_add_slave          = team_add_slave,
1976         .ndo_del_slave          = team_del_slave,
1977         .ndo_fix_features       = team_fix_features,
1978         .ndo_change_carrier     = team_change_carrier,
1979         .ndo_bridge_setlink     = ndo_dflt_netdev_switch_port_bridge_setlink,
1980         .ndo_bridge_dellink     = ndo_dflt_netdev_switch_port_bridge_dellink,
1981 };
1982
1983 /***********************
1984  * ethtool interface
1985  ***********************/
1986
1987 static void team_ethtool_get_drvinfo(struct net_device *dev,
1988                                      struct ethtool_drvinfo *drvinfo)
1989 {
1990         strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
1991         strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
1992 }
1993
1994 static const struct ethtool_ops team_ethtool_ops = {
1995         .get_drvinfo            = team_ethtool_get_drvinfo,
1996         .get_link               = ethtool_op_get_link,
1997 };
1998
1999 /***********************
2000  * rt netlink interface
2001  ***********************/
2002
2003 static void team_setup_by_port(struct net_device *dev,
2004                                struct net_device *port_dev)
2005 {
2006         dev->header_ops = port_dev->header_ops;
2007         dev->type = port_dev->type;
2008         dev->hard_header_len = port_dev->hard_header_len;
2009         dev->addr_len = port_dev->addr_len;
2010         dev->mtu = port_dev->mtu;
2011         memcpy(dev->broadcast, port_dev->broadcast, port_dev->addr_len);
2012         eth_hw_addr_inherit(dev, port_dev);
2013 }
2014
2015 static int team_dev_type_check_change(struct net_device *dev,
2016                                       struct net_device *port_dev)
2017 {
2018         struct team *team = netdev_priv(dev);
2019         char *portname = port_dev->name;
2020         int err;
2021
2022         if (dev->type == port_dev->type)
2023                 return 0;
2024         if (!list_empty(&team->port_list)) {
2025                 netdev_err(dev, "Device %s is of different type\n", portname);
2026                 return -EBUSY;
2027         }
2028         err = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE, dev);
2029         err = notifier_to_errno(err);
2030         if (err) {
2031                 netdev_err(dev, "Refused to change device type\n");
2032                 return err;
2033         }
2034         dev_uc_flush(dev);
2035         dev_mc_flush(dev);
2036         team_setup_by_port(dev, port_dev);
2037         call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
2038         return 0;
2039 }
2040
2041 static void team_setup(struct net_device *dev)
2042 {
2043         ether_setup(dev);
2044
2045         dev->netdev_ops = &team_netdev_ops;
2046         dev->ethtool_ops = &team_ethtool_ops;
2047         dev->destructor = team_destructor;
2048         dev->tx_queue_len = 0;
2049         dev->flags |= IFF_MULTICAST;
2050         dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
2051
2052         /*
2053          * Indicate we support unicast address filtering. That way core won't
2054          * bring us to promisc mode in case a unicast addr is added.
2055          * Let this up to underlay drivers.
2056          */
2057         dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
2058
2059         dev->features |= NETIF_F_LLTX;
2060         dev->features |= NETIF_F_GRO;
2061
2062         /* Don't allow team devices to change network namespaces. */
2063         dev->features |= NETIF_F_NETNS_LOCAL;
2064
2065         dev->hw_features = TEAM_VLAN_FEATURES |
2066                            NETIF_F_HW_VLAN_CTAG_TX |
2067                            NETIF_F_HW_VLAN_CTAG_RX |
2068                            NETIF_F_HW_VLAN_CTAG_FILTER;
2069
2070         dev->hw_features &= ~(NETIF_F_ALL_CSUM & ~NETIF_F_HW_CSUM);
2071         dev->features |= dev->hw_features;
2072 }
2073
2074 static int team_newlink(struct net *src_net, struct net_device *dev,
2075                         struct nlattr *tb[], struct nlattr *data[])
2076 {
2077         if (tb[IFLA_ADDRESS] == NULL)
2078                 eth_hw_addr_random(dev);
2079
2080         return register_netdevice(dev);
2081 }
2082
2083 static int team_validate(struct nlattr *tb[], struct nlattr *data[])
2084 {
2085         if (tb[IFLA_ADDRESS]) {
2086                 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
2087                         return -EINVAL;
2088                 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
2089                         return -EADDRNOTAVAIL;
2090         }
2091         return 0;
2092 }
2093
2094 static unsigned int team_get_num_tx_queues(void)
2095 {
2096         return TEAM_DEFAULT_NUM_TX_QUEUES;
2097 }
2098
2099 static unsigned int team_get_num_rx_queues(void)
2100 {
2101         return TEAM_DEFAULT_NUM_RX_QUEUES;
2102 }
2103
2104 static struct rtnl_link_ops team_link_ops __read_mostly = {
2105         .kind                   = DRV_NAME,
2106         .priv_size              = sizeof(struct team),
2107         .setup                  = team_setup,
2108         .newlink                = team_newlink,
2109         .validate               = team_validate,
2110         .get_num_tx_queues      = team_get_num_tx_queues,
2111         .get_num_rx_queues      = team_get_num_rx_queues,
2112 };
2113
2114
2115 /***********************************
2116  * Generic netlink custom interface
2117  ***********************************/
2118
2119 static struct genl_family team_nl_family = {
2120         .id             = GENL_ID_GENERATE,
2121         .name           = TEAM_GENL_NAME,
2122         .version        = TEAM_GENL_VERSION,
2123         .maxattr        = TEAM_ATTR_MAX,
2124         .netnsok        = true,
2125 };
2126
2127 static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
2128         [TEAM_ATTR_UNSPEC]                      = { .type = NLA_UNSPEC, },
2129         [TEAM_ATTR_TEAM_IFINDEX]                = { .type = NLA_U32 },
2130         [TEAM_ATTR_LIST_OPTION]                 = { .type = NLA_NESTED },
2131         [TEAM_ATTR_LIST_PORT]                   = { .type = NLA_NESTED },
2132 };
2133
2134 static const struct nla_policy
2135 team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
2136         [TEAM_ATTR_OPTION_UNSPEC]               = { .type = NLA_UNSPEC, },
2137         [TEAM_ATTR_OPTION_NAME] = {
2138                 .type = NLA_STRING,
2139                 .len = TEAM_STRING_MAX_LEN,
2140         },
2141         [TEAM_ATTR_OPTION_CHANGED]              = { .type = NLA_FLAG },
2142         [TEAM_ATTR_OPTION_TYPE]                 = { .type = NLA_U8 },
2143         [TEAM_ATTR_OPTION_DATA]                 = { .type = NLA_BINARY },
2144 };
2145
2146 static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
2147 {
2148         struct sk_buff *msg;
2149         void *hdr;
2150         int err;
2151
2152         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2153         if (!msg)
2154                 return -ENOMEM;
2155
2156         hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
2157                           &team_nl_family, 0, TEAM_CMD_NOOP);
2158         if (!hdr) {
2159                 err = -EMSGSIZE;
2160                 goto err_msg_put;
2161         }
2162
2163         genlmsg_end(msg, hdr);
2164
2165         return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
2166
2167 err_msg_put:
2168         nlmsg_free(msg);
2169
2170         return err;
2171 }
2172
2173 /*
2174  * Netlink cmd functions should be locked by following two functions.
2175  * Since dev gets held here, that ensures dev won't disappear in between.
2176  */
2177 static struct team *team_nl_team_get(struct genl_info *info)
2178 {
2179         struct net *net = genl_info_net(info);
2180         int ifindex;
2181         struct net_device *dev;
2182         struct team *team;
2183
2184         if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
2185                 return NULL;
2186
2187         ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
2188         dev = dev_get_by_index(net, ifindex);
2189         if (!dev || dev->netdev_ops != &team_netdev_ops) {
2190                 if (dev)
2191                         dev_put(dev);
2192                 return NULL;
2193         }
2194
2195         team = netdev_priv(dev);
2196         mutex_lock(&team->lock);
2197         return team;
2198 }
2199
2200 static void team_nl_team_put(struct team *team)
2201 {
2202         mutex_unlock(&team->lock);
2203         dev_put(team->dev);
2204 }
2205
2206 typedef int team_nl_send_func_t(struct sk_buff *skb,
2207                                 struct team *team, u32 portid);
2208
2209 static int team_nl_send_unicast(struct sk_buff *skb, struct team *team, u32 portid)
2210 {
2211         return genlmsg_unicast(dev_net(team->dev), skb, portid);
2212 }
2213
2214 static int team_nl_fill_one_option_get(struct sk_buff *skb, struct team *team,
2215                                        struct team_option_inst *opt_inst)
2216 {
2217         struct nlattr *option_item;
2218         struct team_option *option = opt_inst->option;
2219         struct team_option_inst_info *opt_inst_info = &opt_inst->info;
2220         struct team_gsetter_ctx ctx;
2221         int err;
2222
2223         ctx.info = opt_inst_info;
2224         err = team_option_get(team, opt_inst, &ctx);
2225         if (err)
2226                 return err;
2227
2228         option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
2229         if (!option_item)
2230                 return -EMSGSIZE;
2231
2232         if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name))
2233                 goto nest_cancel;
2234         if (opt_inst_info->port &&
2235             nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX,
2236                         opt_inst_info->port->dev->ifindex))
2237                 goto nest_cancel;
2238         if (opt_inst->option->array_size &&
2239             nla_put_u32(skb, TEAM_ATTR_OPTION_ARRAY_INDEX,
2240                         opt_inst_info->array_index))
2241                 goto nest_cancel;
2242
2243         switch (option->type) {
2244         case TEAM_OPTION_TYPE_U32:
2245                 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32))
2246                         goto nest_cancel;
2247                 if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.u32_val))
2248                         goto nest_cancel;
2249                 break;
2250         case TEAM_OPTION_TYPE_STRING:
2251                 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING))
2252                         goto nest_cancel;
2253                 if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA,
2254                                    ctx.data.str_val))
2255                         goto nest_cancel;
2256                 break;
2257         case TEAM_OPTION_TYPE_BINARY:
2258                 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY))
2259                         goto nest_cancel;
2260                 if (nla_put(skb, TEAM_ATTR_OPTION_DATA, ctx.data.bin_val.len,
2261                             ctx.data.bin_val.ptr))
2262                         goto nest_cancel;
2263                 break;
2264         case TEAM_OPTION_TYPE_BOOL:
2265                 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG))
2266                         goto nest_cancel;
2267                 if (ctx.data.bool_val &&
2268                     nla_put_flag(skb, TEAM_ATTR_OPTION_DATA))
2269                         goto nest_cancel;
2270                 break;
2271         case TEAM_OPTION_TYPE_S32:
2272                 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_S32))
2273                         goto nest_cancel;
2274                 if (nla_put_s32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.s32_val))
2275                         goto nest_cancel;
2276                 break;
2277         default:
2278                 BUG();
2279         }
2280         if (opt_inst->removed && nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED))
2281                 goto nest_cancel;
2282         if (opt_inst->changed) {
2283                 if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED))
2284                         goto nest_cancel;
2285                 opt_inst->changed = false;
2286         }
2287         nla_nest_end(skb, option_item);
2288         return 0;
2289
2290 nest_cancel:
2291         nla_nest_cancel(skb, option_item);
2292         return -EMSGSIZE;
2293 }
2294
2295 static int __send_and_alloc_skb(struct sk_buff **pskb,
2296                                 struct team *team, u32 portid,
2297                                 team_nl_send_func_t *send_func)
2298 {
2299         int err;
2300
2301         if (*pskb) {
2302                 err = send_func(*pskb, team, portid);
2303                 if (err)
2304                         return err;
2305         }
2306         *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2307         if (!*pskb)
2308                 return -ENOMEM;
2309         return 0;
2310 }
2311
2312 static int team_nl_send_options_get(struct team *team, u32 portid, u32 seq,
2313                                     int flags, team_nl_send_func_t *send_func,
2314                                     struct list_head *sel_opt_inst_list)
2315 {
2316         struct nlattr *option_list;
2317         struct nlmsghdr *nlh;
2318         void *hdr;
2319         struct team_option_inst *opt_inst;
2320         int err;
2321         struct sk_buff *skb = NULL;
2322         bool incomplete;
2323         int i;
2324
2325         opt_inst = list_first_entry(sel_opt_inst_list,
2326                                     struct team_option_inst, tmp_list);
2327
2328 start_again:
2329         err = __send_and_alloc_skb(&skb, team, portid, send_func);
2330         if (err)
2331                 return err;
2332
2333         hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
2334                           TEAM_CMD_OPTIONS_GET);
2335         if (!hdr)
2336                 return -EMSGSIZE;
2337
2338         if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2339                 goto nla_put_failure;
2340         option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
2341         if (!option_list)
2342                 goto nla_put_failure;
2343
2344         i = 0;
2345         incomplete = false;
2346         list_for_each_entry_from(opt_inst, sel_opt_inst_list, tmp_list) {
2347                 err = team_nl_fill_one_option_get(skb, team, opt_inst);
2348                 if (err) {
2349                         if (err == -EMSGSIZE) {
2350                                 if (!i)
2351                                         goto errout;
2352                                 incomplete = true;
2353                                 break;
2354                         }
2355                         goto errout;
2356                 }
2357                 i++;
2358         }
2359
2360         nla_nest_end(skb, option_list);
2361         genlmsg_end(skb, hdr);
2362         if (incomplete)
2363                 goto start_again;
2364
2365 send_done:
2366         nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
2367         if (!nlh) {
2368                 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2369                 if (err)
2370                         goto errout;
2371                 goto send_done;
2372         }
2373
2374         return send_func(skb, team, portid);
2375
2376 nla_put_failure:
2377         err = -EMSGSIZE;
2378 errout:
2379         genlmsg_cancel(skb, hdr);
2380         nlmsg_free(skb);
2381         return err;
2382 }
2383
2384 static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
2385 {
2386         struct team *team;
2387         struct team_option_inst *opt_inst;
2388         int err;
2389         LIST_HEAD(sel_opt_inst_list);
2390
2391         team = team_nl_team_get(info);
2392         if (!team)
2393                 return -EINVAL;
2394
2395         list_for_each_entry(opt_inst, &team->option_inst_list, list)
2396                 list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
2397         err = team_nl_send_options_get(team, info->snd_portid, info->snd_seq,
2398                                        NLM_F_ACK, team_nl_send_unicast,
2399                                        &sel_opt_inst_list);
2400
2401         team_nl_team_put(team);
2402
2403         return err;
2404 }
2405
2406 static int team_nl_send_event_options_get(struct team *team,
2407                                           struct list_head *sel_opt_inst_list);
2408
2409 static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
2410 {
2411         struct team *team;
2412         int err = 0;
2413         int i;
2414         struct nlattr *nl_option;
2415         LIST_HEAD(opt_inst_list);
2416
2417         team = team_nl_team_get(info);
2418         if (!team)
2419                 return -EINVAL;
2420
2421         err = -EINVAL;
2422         if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
2423                 err = -EINVAL;
2424                 goto team_put;
2425         }
2426
2427         nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
2428                 struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1];
2429                 struct nlattr *attr;
2430                 struct nlattr *attr_data;
2431                 enum team_option_type opt_type;
2432                 int opt_port_ifindex = 0; /* != 0 for per-port options */
2433                 u32 opt_array_index = 0;
2434                 bool opt_is_array = false;
2435                 struct team_option_inst *opt_inst;
2436                 char *opt_name;
2437                 bool opt_found = false;
2438
2439                 if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
2440                         err = -EINVAL;
2441                         goto team_put;
2442                 }
2443                 err = nla_parse_nested(opt_attrs, TEAM_ATTR_OPTION_MAX,
2444                                        nl_option, team_nl_option_policy);
2445                 if (err)
2446                         goto team_put;
2447                 if (!opt_attrs[TEAM_ATTR_OPTION_NAME] ||
2448                     !opt_attrs[TEAM_ATTR_OPTION_TYPE]) {
2449                         err = -EINVAL;
2450                         goto team_put;
2451                 }
2452                 switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) {
2453                 case NLA_U32:
2454                         opt_type = TEAM_OPTION_TYPE_U32;
2455                         break;
2456                 case NLA_STRING:
2457                         opt_type = TEAM_OPTION_TYPE_STRING;
2458                         break;
2459                 case NLA_BINARY:
2460                         opt_type = TEAM_OPTION_TYPE_BINARY;
2461                         break;
2462                 case NLA_FLAG:
2463                         opt_type = TEAM_OPTION_TYPE_BOOL;
2464                         break;
2465                 case NLA_S32:
2466                         opt_type = TEAM_OPTION_TYPE_S32;
2467                         break;
2468                 default:
2469                         goto team_put;
2470                 }
2471
2472                 attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA];
2473                 if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) {
2474                         err = -EINVAL;
2475                         goto team_put;
2476                 }
2477
2478                 opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]);
2479                 attr = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX];
2480                 if (attr)
2481                         opt_port_ifindex = nla_get_u32(attr);
2482
2483                 attr = opt_attrs[TEAM_ATTR_OPTION_ARRAY_INDEX];
2484                 if (attr) {
2485                         opt_is_array = true;
2486                         opt_array_index = nla_get_u32(attr);
2487                 }
2488
2489                 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2490                         struct team_option *option = opt_inst->option;
2491                         struct team_gsetter_ctx ctx;
2492                         struct team_option_inst_info *opt_inst_info;
2493                         int tmp_ifindex;
2494
2495                         opt_inst_info = &opt_inst->info;
2496                         tmp_ifindex = opt_inst_info->port ?
2497                                       opt_inst_info->port->dev->ifindex : 0;
2498                         if (option->type != opt_type ||
2499                             strcmp(option->name, opt_name) ||
2500                             tmp_ifindex != opt_port_ifindex ||
2501                             (option->array_size && !opt_is_array) ||
2502                             opt_inst_info->array_index != opt_array_index)
2503                                 continue;
2504                         opt_found = true;
2505                         ctx.info = opt_inst_info;
2506                         switch (opt_type) {
2507                         case TEAM_OPTION_TYPE_U32:
2508                                 ctx.data.u32_val = nla_get_u32(attr_data);
2509                                 break;
2510                         case TEAM_OPTION_TYPE_STRING:
2511                                 if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) {
2512                                         err = -EINVAL;
2513                                         goto team_put;
2514                                 }
2515                                 ctx.data.str_val = nla_data(attr_data);
2516                                 break;
2517                         case TEAM_OPTION_TYPE_BINARY:
2518                                 ctx.data.bin_val.len = nla_len(attr_data);
2519                                 ctx.data.bin_val.ptr = nla_data(attr_data);
2520                                 break;
2521                         case TEAM_OPTION_TYPE_BOOL:
2522                                 ctx.data.bool_val = attr_data ? true : false;
2523                                 break;
2524                         case TEAM_OPTION_TYPE_S32:
2525                                 ctx.data.s32_val = nla_get_s32(attr_data);
2526                                 break;
2527                         default:
2528                                 BUG();
2529                         }
2530                         err = team_option_set(team, opt_inst, &ctx);
2531                         if (err)
2532                                 goto team_put;
2533                         opt_inst->changed = true;
2534                         list_add(&opt_inst->tmp_list, &opt_inst_list);
2535                 }
2536                 if (!opt_found) {
2537                         err = -ENOENT;
2538                         goto team_put;
2539                 }
2540         }
2541
2542         err = team_nl_send_event_options_get(team, &opt_inst_list);
2543
2544 team_put:
2545         team_nl_team_put(team);
2546
2547         return err;
2548 }
2549
2550 static int team_nl_fill_one_port_get(struct sk_buff *skb,
2551                                      struct team_port *port)
2552 {
2553         struct nlattr *port_item;
2554
2555         port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
2556         if (!port_item)
2557                 goto nest_cancel;
2558         if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex))
2559                 goto nest_cancel;
2560         if (port->changed) {
2561                 if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED))
2562                         goto nest_cancel;
2563                 port->changed = false;
2564         }
2565         if ((port->removed &&
2566              nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) ||
2567             (port->state.linkup &&
2568              nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) ||
2569             nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) ||
2570             nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex))
2571                 goto nest_cancel;
2572         nla_nest_end(skb, port_item);
2573         return 0;
2574
2575 nest_cancel:
2576         nla_nest_cancel(skb, port_item);
2577         return -EMSGSIZE;
2578 }
2579
2580 static int team_nl_send_port_list_get(struct team *team, u32 portid, u32 seq,
2581                                       int flags, team_nl_send_func_t *send_func,
2582                                       struct team_port *one_port)
2583 {
2584         struct nlattr *port_list;
2585         struct nlmsghdr *nlh;
2586         void *hdr;
2587         struct team_port *port;
2588         int err;
2589         struct sk_buff *skb = NULL;
2590         bool incomplete;
2591         int i;
2592
2593         port = list_first_entry_or_null(&team->port_list,
2594                                         struct team_port, list);
2595
2596 start_again:
2597         err = __send_and_alloc_skb(&skb, team, portid, send_func);
2598         if (err)
2599                 return err;
2600
2601         hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
2602                           TEAM_CMD_PORT_LIST_GET);
2603         if (!hdr)
2604                 return -EMSGSIZE;
2605
2606         if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2607                 goto nla_put_failure;
2608         port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
2609         if (!port_list)
2610                 goto nla_put_failure;
2611
2612         i = 0;
2613         incomplete = false;
2614
2615         /* If one port is selected, called wants to send port list containing
2616          * only this port. Otherwise go through all listed ports and send all
2617          */
2618         if (one_port) {
2619                 err = team_nl_fill_one_port_get(skb, one_port);
2620                 if (err)
2621                         goto errout;
2622         } else if (port) {
2623                 list_for_each_entry_from(port, &team->port_list, list) {
2624                         err = team_nl_fill_one_port_get(skb, port);
2625                         if (err) {
2626                                 if (err == -EMSGSIZE) {
2627                                         if (!i)
2628                                                 goto errout;
2629                                         incomplete = true;
2630                                         break;
2631                                 }
2632                                 goto errout;
2633                         }
2634                         i++;
2635                 }
2636         }
2637
2638         nla_nest_end(skb, port_list);
2639         genlmsg_end(skb, hdr);
2640         if (incomplete)
2641                 goto start_again;
2642
2643 send_done:
2644         nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
2645         if (!nlh) {
2646                 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2647                 if (err)
2648                         goto errout;
2649                 goto send_done;
2650         }
2651
2652         return send_func(skb, team, portid);
2653
2654 nla_put_failure:
2655         err = -EMSGSIZE;
2656 errout:
2657         genlmsg_cancel(skb, hdr);
2658         nlmsg_free(skb);
2659         return err;
2660 }
2661
2662 static int team_nl_cmd_port_list_get(struct sk_buff *skb,
2663                                      struct genl_info *info)
2664 {
2665         struct team *team;
2666         int err;
2667
2668         team = team_nl_team_get(info);
2669         if (!team)
2670                 return -EINVAL;
2671
2672         err = team_nl_send_port_list_get(team, info->snd_portid, info->snd_seq,
2673                                          NLM_F_ACK, team_nl_send_unicast, NULL);
2674
2675         team_nl_team_put(team);
2676
2677         return err;
2678 }
2679
2680 static const struct genl_ops team_nl_ops[] = {
2681         {
2682                 .cmd = TEAM_CMD_NOOP,
2683                 .doit = team_nl_cmd_noop,
2684                 .policy = team_nl_policy,
2685         },
2686         {
2687                 .cmd = TEAM_CMD_OPTIONS_SET,
2688                 .doit = team_nl_cmd_options_set,
2689                 .policy = team_nl_policy,
2690                 .flags = GENL_ADMIN_PERM,
2691         },
2692         {
2693                 .cmd = TEAM_CMD_OPTIONS_GET,
2694                 .doit = team_nl_cmd_options_get,
2695                 .policy = team_nl_policy,
2696                 .flags = GENL_ADMIN_PERM,
2697         },
2698         {
2699                 .cmd = TEAM_CMD_PORT_LIST_GET,
2700                 .doit = team_nl_cmd_port_list_get,
2701                 .policy = team_nl_policy,
2702                 .flags = GENL_ADMIN_PERM,
2703         },
2704 };
2705
2706 static const struct genl_multicast_group team_nl_mcgrps[] = {
2707         { .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME, },
2708 };
2709
2710 static int team_nl_send_multicast(struct sk_buff *skb,
2711                                   struct team *team, u32 portid)
2712 {
2713         return genlmsg_multicast_netns(&team_nl_family, dev_net(team->dev),
2714                                        skb, 0, 0, GFP_KERNEL);
2715 }
2716
2717 static int team_nl_send_event_options_get(struct team *team,
2718                                           struct list_head *sel_opt_inst_list)
2719 {
2720         return team_nl_send_options_get(team, 0, 0, 0, team_nl_send_multicast,
2721                                         sel_opt_inst_list);
2722 }
2723
2724 static int team_nl_send_event_port_get(struct team *team,
2725                                        struct team_port *port)
2726 {
2727         return team_nl_send_port_list_get(team, 0, 0, 0, team_nl_send_multicast,
2728                                           port);
2729 }
2730
2731 static int team_nl_init(void)
2732 {
2733         return genl_register_family_with_ops_groups(&team_nl_family, team_nl_ops,
2734                                                     team_nl_mcgrps);
2735 }
2736
2737 static void team_nl_fini(void)
2738 {
2739         genl_unregister_family(&team_nl_family);
2740 }
2741
2742
2743 /******************
2744  * Change checkers
2745  ******************/
2746
2747 static void __team_options_change_check(struct team *team)
2748 {
2749         int err;
2750         struct team_option_inst *opt_inst;
2751         LIST_HEAD(sel_opt_inst_list);
2752
2753         list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2754                 if (opt_inst->changed)
2755                         list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
2756         }
2757         err = team_nl_send_event_options_get(team, &sel_opt_inst_list);
2758         if (err && err != -ESRCH)
2759                 netdev_warn(team->dev, "Failed to send options change via netlink (err %d)\n",
2760                             err);
2761 }
2762
2763 /* rtnl lock is held */
2764
2765 static void __team_port_change_send(struct team_port *port, bool linkup)
2766 {
2767         int err;
2768
2769         port->changed = true;
2770         port->state.linkup = linkup;
2771         team_refresh_port_linkup(port);
2772         if (linkup) {
2773                 struct ethtool_cmd ecmd;
2774
2775                 err = __ethtool_get_settings(port->dev, &ecmd);
2776                 if (!err) {
2777                         port->state.speed = ethtool_cmd_speed(&ecmd);
2778                         port->state.duplex = ecmd.duplex;
2779                         goto send_event;
2780                 }
2781         }
2782         port->state.speed = 0;
2783         port->state.duplex = 0;
2784
2785 send_event:
2786         err = team_nl_send_event_port_get(port->team, port);
2787         if (err && err != -ESRCH)
2788                 netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink (err %d)\n",
2789                             port->dev->name, err);
2790
2791 }
2792
2793 static void __team_carrier_check(struct team *team)
2794 {
2795         struct team_port *port;
2796         bool team_linkup;
2797
2798         if (team->user_carrier_enabled)
2799                 return;
2800
2801         team_linkup = false;
2802         list_for_each_entry(port, &team->port_list, list) {
2803                 if (port->linkup) {
2804                         team_linkup = true;
2805                         break;
2806                 }
2807         }
2808
2809         if (team_linkup)
2810                 netif_carrier_on(team->dev);
2811         else
2812                 netif_carrier_off(team->dev);
2813 }
2814
2815 static void __team_port_change_check(struct team_port *port, bool linkup)
2816 {
2817         if (port->state.linkup != linkup)
2818                 __team_port_change_send(port, linkup);
2819         __team_carrier_check(port->team);
2820 }
2821
2822 static void __team_port_change_port_added(struct team_port *port, bool linkup)
2823 {
2824         __team_port_change_send(port, linkup);
2825         __team_carrier_check(port->team);
2826 }
2827
2828 static void __team_port_change_port_removed(struct team_port *port)
2829 {
2830         port->removed = true;
2831         __team_port_change_send(port, false);
2832         __team_carrier_check(port->team);
2833 }
2834
2835 static void team_port_change_check(struct team_port *port, bool linkup)
2836 {
2837         struct team *team = port->team;
2838
2839         mutex_lock(&team->lock);
2840         __team_port_change_check(port, linkup);
2841         mutex_unlock(&team->lock);
2842 }
2843
2844
2845 /************************************
2846  * Net device notifier event handler
2847  ************************************/
2848
2849 static int team_device_event(struct notifier_block *unused,
2850                              unsigned long event, void *ptr)
2851 {
2852         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2853         struct team_port *port;
2854
2855         port = team_port_get_rtnl(dev);
2856         if (!port)
2857                 return NOTIFY_DONE;
2858
2859         switch (event) {
2860         case NETDEV_UP:
2861                 if (netif_carrier_ok(dev))
2862                         team_port_change_check(port, true);
2863                 break;
2864         case NETDEV_DOWN:
2865                 team_port_change_check(port, false);
2866                 break;
2867         case NETDEV_CHANGE:
2868                 if (netif_running(port->dev))
2869                         team_port_change_check(port,
2870                                                !!netif_carrier_ok(port->dev));
2871                 break;
2872         case NETDEV_UNREGISTER:
2873                 team_del_slave(port->team->dev, dev);
2874                 break;
2875         case NETDEV_FEAT_CHANGE:
2876                 team_compute_features(port->team);
2877                 break;
2878         case NETDEV_PRECHANGEMTU:
2879                 /* Forbid to change mtu of underlaying device */
2880                 if (!port->team->port_mtu_change_allowed)
2881                         return NOTIFY_BAD;
2882                 break;
2883         case NETDEV_PRE_TYPE_CHANGE:
2884                 /* Forbid to change type of underlaying device */
2885                 return NOTIFY_BAD;
2886         case NETDEV_RESEND_IGMP:
2887                 /* Propagate to master device */
2888                 call_netdevice_notifiers(event, port->team->dev);
2889                 break;
2890         }
2891         return NOTIFY_DONE;
2892 }
2893
2894 static struct notifier_block team_notifier_block __read_mostly = {
2895         .notifier_call = team_device_event,
2896 };
2897
2898
2899 /***********************
2900  * Module init and exit
2901  ***********************/
2902
2903 static int __init team_module_init(void)
2904 {
2905         int err;
2906
2907         register_netdevice_notifier(&team_notifier_block);
2908
2909         err = rtnl_link_register(&team_link_ops);
2910         if (err)
2911                 goto err_rtnl_reg;
2912
2913         err = team_nl_init();
2914         if (err)
2915                 goto err_nl_init;
2916
2917         return 0;
2918
2919 err_nl_init:
2920         rtnl_link_unregister(&team_link_ops);
2921
2922 err_rtnl_reg:
2923         unregister_netdevice_notifier(&team_notifier_block);
2924
2925         return err;
2926 }
2927
2928 static void __exit team_module_exit(void)
2929 {
2930         team_nl_fini();
2931         rtnl_link_unregister(&team_link_ops);
2932         unregister_netdevice_notifier(&team_notifier_block);
2933 }
2934
2935 module_init(team_module_init);
2936 module_exit(team_module_exit);
2937
2938 MODULE_LICENSE("GPL v2");
2939 MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
2940 MODULE_DESCRIPTION("Ethernet team device driver");
2941 MODULE_ALIAS_RTNL_LINK(DRV_NAME);