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