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