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