]> Pileus Git - ~andy/linux/blob - drivers/net/team/team.c
694ccf6d71a3bca9d502ac670c543f2e70149d64
[~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_master_upper_dev_link(port_dev, dev);
1059         if (err) {
1060                 netdev_err(dev, "Device %s failed to set upper link\n",
1061                            portname);
1062                 goto err_set_upper_link;
1063         }
1064
1065         err = netdev_rx_handler_register(port_dev, team_handle_frame,
1066                                          port);
1067         if (err) {
1068                 netdev_err(dev, "Device %s failed to register rx_handler\n",
1069                            portname);
1070                 goto err_handler_register;
1071         }
1072
1073         err = __team_option_inst_add_port(team, port);
1074         if (err) {
1075                 netdev_err(dev, "Device %s failed to add per-port options\n",
1076                            portname);
1077                 goto err_option_port_add;
1078         }
1079
1080         port->index = -1;
1081         team_port_enable(team, port);
1082         list_add_tail_rcu(&port->list, &team->port_list);
1083         __team_compute_features(team);
1084         __team_port_change_port_added(port, !!netif_carrier_ok(port_dev));
1085         __team_options_change_check(team);
1086
1087         netdev_info(dev, "Port device %s added\n", portname);
1088
1089         return 0;
1090
1091 err_option_port_add:
1092         netdev_rx_handler_unregister(port_dev);
1093
1094 err_handler_register:
1095         netdev_upper_dev_unlink(port_dev, dev);
1096
1097 err_set_upper_link:
1098         team_port_disable_netpoll(port);
1099
1100 err_enable_netpoll:
1101         vlan_vids_del_by_dev(port_dev, dev);
1102
1103 err_vids_add:
1104         dev_close(port_dev);
1105
1106 err_dev_open:
1107         team_port_leave(team, port);
1108         team_port_set_orig_dev_addr(port);
1109
1110 err_port_enter:
1111         dev_set_mtu(port_dev, port->orig.mtu);
1112
1113 err_set_mtu:
1114         kfree(port);
1115
1116         return err;
1117 }
1118
1119 static void __team_port_change_port_removed(struct team_port *port);
1120
1121 static int team_port_del(struct team *team, struct net_device *port_dev)
1122 {
1123         struct net_device *dev = team->dev;
1124         struct team_port *port;
1125         char *portname = port_dev->name;
1126
1127         port = team_port_get_rtnl(port_dev);
1128         if (!port || !team_port_find(team, port)) {
1129                 netdev_err(dev, "Device %s does not act as a port of this team\n",
1130                            portname);
1131                 return -ENOENT;
1132         }
1133
1134         team_port_disable(team, port);
1135         list_del_rcu(&port->list);
1136         netdev_rx_handler_unregister(port_dev);
1137         netdev_upper_dev_unlink(port_dev, dev);
1138         team_port_disable_netpoll(port);
1139         vlan_vids_del_by_dev(port_dev, dev);
1140         dev_close(port_dev);
1141         team_port_leave(team, port);
1142
1143         __team_option_inst_mark_removed_port(team, port);
1144         __team_options_change_check(team);
1145         __team_option_inst_del_port(team, port);
1146         __team_port_change_port_removed(port);
1147
1148         team_port_set_orig_dev_addr(port);
1149         dev_set_mtu(port_dev, port->orig.mtu);
1150         synchronize_rcu();
1151         kfree(port);
1152         netdev_info(dev, "Port device %s removed\n", portname);
1153         __team_compute_features(team);
1154
1155         return 0;
1156 }
1157
1158
1159 /*****************
1160  * Net device ops
1161  *****************/
1162
1163 static int team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx)
1164 {
1165         ctx->data.str_val = team->mode->kind;
1166         return 0;
1167 }
1168
1169 static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx)
1170 {
1171         return team_change_mode(team, ctx->data.str_val);
1172 }
1173
1174 static int team_port_en_option_get(struct team *team,
1175                                    struct team_gsetter_ctx *ctx)
1176 {
1177         struct team_port *port = ctx->info->port;
1178
1179         ctx->data.bool_val = team_port_enabled(port);
1180         return 0;
1181 }
1182
1183 static int team_port_en_option_set(struct team *team,
1184                                    struct team_gsetter_ctx *ctx)
1185 {
1186         struct team_port *port = ctx->info->port;
1187
1188         if (ctx->data.bool_val)
1189                 team_port_enable(team, port);
1190         else
1191                 team_port_disable(team, port);
1192         return 0;
1193 }
1194
1195 static int team_user_linkup_option_get(struct team *team,
1196                                        struct team_gsetter_ctx *ctx)
1197 {
1198         struct team_port *port = ctx->info->port;
1199
1200         ctx->data.bool_val = port->user.linkup;
1201         return 0;
1202 }
1203
1204 static int team_user_linkup_option_set(struct team *team,
1205                                        struct team_gsetter_ctx *ctx)
1206 {
1207         struct team_port *port = ctx->info->port;
1208
1209         port->user.linkup = ctx->data.bool_val;
1210         team_refresh_port_linkup(port);
1211         return 0;
1212 }
1213
1214 static int team_user_linkup_en_option_get(struct team *team,
1215                                           struct team_gsetter_ctx *ctx)
1216 {
1217         struct team_port *port = ctx->info->port;
1218
1219         ctx->data.bool_val = port->user.linkup_enabled;
1220         return 0;
1221 }
1222
1223 static int team_user_linkup_en_option_set(struct team *team,
1224                                           struct team_gsetter_ctx *ctx)
1225 {
1226         struct team_port *port = ctx->info->port;
1227
1228         port->user.linkup_enabled = ctx->data.bool_val;
1229         team_refresh_port_linkup(port);
1230         return 0;
1231 }
1232
1233 static int team_priority_option_get(struct team *team,
1234                                     struct team_gsetter_ctx *ctx)
1235 {
1236         struct team_port *port = ctx->info->port;
1237
1238         ctx->data.s32_val = port->priority;
1239         return 0;
1240 }
1241
1242 static int team_priority_option_set(struct team *team,
1243                                     struct team_gsetter_ctx *ctx)
1244 {
1245         struct team_port *port = ctx->info->port;
1246
1247         port->priority = ctx->data.s32_val;
1248         team_queue_override_port_refresh(team, port);
1249         return 0;
1250 }
1251
1252 static int team_queue_id_option_get(struct team *team,
1253                                     struct team_gsetter_ctx *ctx)
1254 {
1255         struct team_port *port = ctx->info->port;
1256
1257         ctx->data.u32_val = port->queue_id;
1258         return 0;
1259 }
1260
1261 static int team_queue_id_option_set(struct team *team,
1262                                     struct team_gsetter_ctx *ctx)
1263 {
1264         struct team_port *port = ctx->info->port;
1265
1266         if (port->queue_id == ctx->data.u32_val)
1267                 return 0;
1268         if (ctx->data.u32_val >= team->dev->real_num_tx_queues)
1269                 return -EINVAL;
1270         port->queue_id = ctx->data.u32_val;
1271         team_queue_override_port_refresh(team, port);
1272         return 0;
1273 }
1274
1275
1276 static const struct team_option team_options[] = {
1277         {
1278                 .name = "mode",
1279                 .type = TEAM_OPTION_TYPE_STRING,
1280                 .getter = team_mode_option_get,
1281                 .setter = team_mode_option_set,
1282         },
1283         {
1284                 .name = "enabled",
1285                 .type = TEAM_OPTION_TYPE_BOOL,
1286                 .per_port = true,
1287                 .getter = team_port_en_option_get,
1288                 .setter = team_port_en_option_set,
1289         },
1290         {
1291                 .name = "user_linkup",
1292                 .type = TEAM_OPTION_TYPE_BOOL,
1293                 .per_port = true,
1294                 .getter = team_user_linkup_option_get,
1295                 .setter = team_user_linkup_option_set,
1296         },
1297         {
1298                 .name = "user_linkup_enabled",
1299                 .type = TEAM_OPTION_TYPE_BOOL,
1300                 .per_port = true,
1301                 .getter = team_user_linkup_en_option_get,
1302                 .setter = team_user_linkup_en_option_set,
1303         },
1304         {
1305                 .name = "priority",
1306                 .type = TEAM_OPTION_TYPE_S32,
1307                 .per_port = true,
1308                 .getter = team_priority_option_get,
1309                 .setter = team_priority_option_set,
1310         },
1311         {
1312                 .name = "queue_id",
1313                 .type = TEAM_OPTION_TYPE_U32,
1314                 .per_port = true,
1315                 .getter = team_queue_id_option_get,
1316                 .setter = team_queue_id_option_set,
1317         },
1318 };
1319
1320 static struct lock_class_key team_netdev_xmit_lock_key;
1321 static struct lock_class_key team_netdev_addr_lock_key;
1322 static struct lock_class_key team_tx_busylock_key;
1323
1324 static void team_set_lockdep_class_one(struct net_device *dev,
1325                                        struct netdev_queue *txq,
1326                                        void *unused)
1327 {
1328         lockdep_set_class(&txq->_xmit_lock, &team_netdev_xmit_lock_key);
1329 }
1330
1331 static void team_set_lockdep_class(struct net_device *dev)
1332 {
1333         lockdep_set_class(&dev->addr_list_lock, &team_netdev_addr_lock_key);
1334         netdev_for_each_tx_queue(dev, team_set_lockdep_class_one, NULL);
1335         dev->qdisc_tx_busylock = &team_tx_busylock_key;
1336 }
1337
1338 static int team_init(struct net_device *dev)
1339 {
1340         struct team *team = netdev_priv(dev);
1341         int i;
1342         int err;
1343
1344         team->dev = dev;
1345         mutex_init(&team->lock);
1346         team_set_no_mode(team);
1347
1348         team->pcpu_stats = alloc_percpu(struct team_pcpu_stats);
1349         if (!team->pcpu_stats)
1350                 return -ENOMEM;
1351
1352         for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
1353                 INIT_HLIST_HEAD(&team->en_port_hlist[i]);
1354         INIT_LIST_HEAD(&team->port_list);
1355         err = team_queue_override_init(team);
1356         if (err)
1357                 goto err_team_queue_override_init;
1358
1359         team_adjust_ops(team);
1360
1361         INIT_LIST_HEAD(&team->option_list);
1362         INIT_LIST_HEAD(&team->option_inst_list);
1363         err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
1364         if (err)
1365                 goto err_options_register;
1366         netif_carrier_off(dev);
1367
1368         team_set_lockdep_class(dev);
1369
1370         return 0;
1371
1372 err_options_register:
1373         team_queue_override_fini(team);
1374 err_team_queue_override_init:
1375         free_percpu(team->pcpu_stats);
1376
1377         return err;
1378 }
1379
1380 static void team_uninit(struct net_device *dev)
1381 {
1382         struct team *team = netdev_priv(dev);
1383         struct team_port *port;
1384         struct team_port *tmp;
1385
1386         mutex_lock(&team->lock);
1387         list_for_each_entry_safe(port, tmp, &team->port_list, list)
1388                 team_port_del(team, port->dev);
1389
1390         __team_change_mode(team, NULL); /* cleanup */
1391         __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
1392         team_queue_override_fini(team);
1393         mutex_unlock(&team->lock);
1394 }
1395
1396 static void team_destructor(struct net_device *dev)
1397 {
1398         struct team *team = netdev_priv(dev);
1399
1400         free_percpu(team->pcpu_stats);
1401         free_netdev(dev);
1402 }
1403
1404 static int team_open(struct net_device *dev)
1405 {
1406         return 0;
1407 }
1408
1409 static int team_close(struct net_device *dev)
1410 {
1411         return 0;
1412 }
1413
1414 /*
1415  * note: already called with rcu_read_lock
1416  */
1417 static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
1418 {
1419         struct team *team = netdev_priv(dev);
1420         bool tx_success;
1421         unsigned int len = skb->len;
1422
1423         tx_success = team_queue_override_transmit(team, skb);
1424         if (!tx_success)
1425                 tx_success = team->ops.transmit(team, skb);
1426         if (tx_success) {
1427                 struct team_pcpu_stats *pcpu_stats;
1428
1429                 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
1430                 u64_stats_update_begin(&pcpu_stats->syncp);
1431                 pcpu_stats->tx_packets++;
1432                 pcpu_stats->tx_bytes += len;
1433                 u64_stats_update_end(&pcpu_stats->syncp);
1434         } else {
1435                 this_cpu_inc(team->pcpu_stats->tx_dropped);
1436         }
1437
1438         return NETDEV_TX_OK;
1439 }
1440
1441 static u16 team_select_queue(struct net_device *dev, struct sk_buff *skb)
1442 {
1443         /*
1444          * This helper function exists to help dev_pick_tx get the correct
1445          * destination queue.  Using a helper function skips a call to
1446          * skb_tx_hash and will put the skbs in the queue we expect on their
1447          * way down to the team driver.
1448          */
1449         u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
1450
1451         /*
1452          * Save the original txq to restore before passing to the driver
1453          */
1454         qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
1455
1456         if (unlikely(txq >= dev->real_num_tx_queues)) {
1457                 do {
1458                         txq -= dev->real_num_tx_queues;
1459                 } while (txq >= dev->real_num_tx_queues);
1460         }
1461         return txq;
1462 }
1463
1464 static void team_change_rx_flags(struct net_device *dev, int change)
1465 {
1466         struct team *team = netdev_priv(dev);
1467         struct team_port *port;
1468         int inc;
1469
1470         rcu_read_lock();
1471         list_for_each_entry_rcu(port, &team->port_list, list) {
1472                 if (change & IFF_PROMISC) {
1473                         inc = dev->flags & IFF_PROMISC ? 1 : -1;
1474                         dev_set_promiscuity(port->dev, inc);
1475                 }
1476                 if (change & IFF_ALLMULTI) {
1477                         inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
1478                         dev_set_allmulti(port->dev, inc);
1479                 }
1480         }
1481         rcu_read_unlock();
1482 }
1483
1484 static void team_set_rx_mode(struct net_device *dev)
1485 {
1486         struct team *team = netdev_priv(dev);
1487         struct team_port *port;
1488
1489         rcu_read_lock();
1490         list_for_each_entry_rcu(port, &team->port_list, list) {
1491                 dev_uc_sync(port->dev, dev);
1492                 dev_mc_sync(port->dev, dev);
1493         }
1494         rcu_read_unlock();
1495 }
1496
1497 static int team_set_mac_address(struct net_device *dev, void *p)
1498 {
1499         struct sockaddr *addr = p;
1500         struct team *team = netdev_priv(dev);
1501         struct team_port *port;
1502
1503         if (dev->type == ARPHRD_ETHER && !is_valid_ether_addr(addr->sa_data))
1504                 return -EADDRNOTAVAIL;
1505         memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1506         rcu_read_lock();
1507         list_for_each_entry_rcu(port, &team->port_list, list)
1508                 if (team->ops.port_change_dev_addr)
1509                         team->ops.port_change_dev_addr(team, port);
1510         rcu_read_unlock();
1511         return 0;
1512 }
1513
1514 static int team_change_mtu(struct net_device *dev, int new_mtu)
1515 {
1516         struct team *team = netdev_priv(dev);
1517         struct team_port *port;
1518         int err;
1519
1520         /*
1521          * Alhough this is reader, it's guarded by team lock. It's not possible
1522          * to traverse list in reverse under rcu_read_lock
1523          */
1524         mutex_lock(&team->lock);
1525         list_for_each_entry(port, &team->port_list, list) {
1526                 err = dev_set_mtu(port->dev, new_mtu);
1527                 if (err) {
1528                         netdev_err(dev, "Device %s failed to change mtu",
1529                                    port->dev->name);
1530                         goto unwind;
1531                 }
1532         }
1533         mutex_unlock(&team->lock);
1534
1535         dev->mtu = new_mtu;
1536
1537         return 0;
1538
1539 unwind:
1540         list_for_each_entry_continue_reverse(port, &team->port_list, list)
1541                 dev_set_mtu(port->dev, dev->mtu);
1542         mutex_unlock(&team->lock);
1543
1544         return err;
1545 }
1546
1547 static struct rtnl_link_stats64 *
1548 team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1549 {
1550         struct team *team = netdev_priv(dev);
1551         struct team_pcpu_stats *p;
1552         u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
1553         u32 rx_dropped = 0, tx_dropped = 0;
1554         unsigned int start;
1555         int i;
1556
1557         for_each_possible_cpu(i) {
1558                 p = per_cpu_ptr(team->pcpu_stats, i);
1559                 do {
1560                         start = u64_stats_fetch_begin_bh(&p->syncp);
1561                         rx_packets      = p->rx_packets;
1562                         rx_bytes        = p->rx_bytes;
1563                         rx_multicast    = p->rx_multicast;
1564                         tx_packets      = p->tx_packets;
1565                         tx_bytes        = p->tx_bytes;
1566                 } while (u64_stats_fetch_retry_bh(&p->syncp, start));
1567
1568                 stats->rx_packets       += rx_packets;
1569                 stats->rx_bytes         += rx_bytes;
1570                 stats->multicast        += rx_multicast;
1571                 stats->tx_packets       += tx_packets;
1572                 stats->tx_bytes         += tx_bytes;
1573                 /*
1574                  * rx_dropped & tx_dropped are u32, updated
1575                  * without syncp protection.
1576                  */
1577                 rx_dropped      += p->rx_dropped;
1578                 tx_dropped      += p->tx_dropped;
1579         }
1580         stats->rx_dropped       = rx_dropped;
1581         stats->tx_dropped       = tx_dropped;
1582         return stats;
1583 }
1584
1585 static int team_vlan_rx_add_vid(struct net_device *dev, uint16_t vid)
1586 {
1587         struct team *team = netdev_priv(dev);
1588         struct team_port *port;
1589         int err;
1590
1591         /*
1592          * Alhough this is reader, it's guarded by team lock. It's not possible
1593          * to traverse list in reverse under rcu_read_lock
1594          */
1595         mutex_lock(&team->lock);
1596         list_for_each_entry(port, &team->port_list, list) {
1597                 err = vlan_vid_add(port->dev, vid);
1598                 if (err)
1599                         goto unwind;
1600         }
1601         mutex_unlock(&team->lock);
1602
1603         return 0;
1604
1605 unwind:
1606         list_for_each_entry_continue_reverse(port, &team->port_list, list)
1607                 vlan_vid_del(port->dev, vid);
1608         mutex_unlock(&team->lock);
1609
1610         return err;
1611 }
1612
1613 static int team_vlan_rx_kill_vid(struct net_device *dev, uint16_t vid)
1614 {
1615         struct team *team = netdev_priv(dev);
1616         struct team_port *port;
1617
1618         rcu_read_lock();
1619         list_for_each_entry_rcu(port, &team->port_list, list)
1620                 vlan_vid_del(port->dev, vid);
1621         rcu_read_unlock();
1622
1623         return 0;
1624 }
1625
1626 #ifdef CONFIG_NET_POLL_CONTROLLER
1627 static void team_poll_controller(struct net_device *dev)
1628 {
1629 }
1630
1631 static void __team_netpoll_cleanup(struct team *team)
1632 {
1633         struct team_port *port;
1634
1635         list_for_each_entry(port, &team->port_list, list)
1636                 team_port_disable_netpoll(port);
1637 }
1638
1639 static void team_netpoll_cleanup(struct net_device *dev)
1640 {
1641         struct team *team = netdev_priv(dev);
1642
1643         mutex_lock(&team->lock);
1644         __team_netpoll_cleanup(team);
1645         mutex_unlock(&team->lock);
1646 }
1647
1648 static int team_netpoll_setup(struct net_device *dev,
1649                               struct netpoll_info *npifo, gfp_t gfp)
1650 {
1651         struct team *team = netdev_priv(dev);
1652         struct team_port *port;
1653         int err = 0;
1654
1655         mutex_lock(&team->lock);
1656         list_for_each_entry(port, &team->port_list, list) {
1657                 err = team_port_enable_netpoll(team, port, gfp);
1658                 if (err) {
1659                         __team_netpoll_cleanup(team);
1660                         break;
1661                 }
1662         }
1663         mutex_unlock(&team->lock);
1664         return err;
1665 }
1666 #endif
1667
1668 static int team_add_slave(struct net_device *dev, struct net_device *port_dev)
1669 {
1670         struct team *team = netdev_priv(dev);
1671         int err;
1672
1673         mutex_lock(&team->lock);
1674         err = team_port_add(team, port_dev);
1675         mutex_unlock(&team->lock);
1676         return err;
1677 }
1678
1679 static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
1680 {
1681         struct team *team = netdev_priv(dev);
1682         int err;
1683
1684         mutex_lock(&team->lock);
1685         err = team_port_del(team, port_dev);
1686         mutex_unlock(&team->lock);
1687         return err;
1688 }
1689
1690 static netdev_features_t team_fix_features(struct net_device *dev,
1691                                            netdev_features_t features)
1692 {
1693         struct team_port *port;
1694         struct team *team = netdev_priv(dev);
1695         netdev_features_t mask;
1696
1697         mask = features;
1698         features &= ~NETIF_F_ONE_FOR_ALL;
1699         features |= NETIF_F_ALL_FOR_ALL;
1700
1701         rcu_read_lock();
1702         list_for_each_entry_rcu(port, &team->port_list, list) {
1703                 features = netdev_increment_features(features,
1704                                                      port->dev->features,
1705                                                      mask);
1706         }
1707         rcu_read_unlock();
1708         return features;
1709 }
1710
1711 static int team_change_carrier(struct net_device *dev, bool new_carrier)
1712 {
1713         if (new_carrier)
1714                 netif_carrier_on(dev);
1715         else
1716                 netif_carrier_off(dev);
1717         return 0;
1718 }
1719
1720 static const struct net_device_ops team_netdev_ops = {
1721         .ndo_init               = team_init,
1722         .ndo_uninit             = team_uninit,
1723         .ndo_open               = team_open,
1724         .ndo_stop               = team_close,
1725         .ndo_start_xmit         = team_xmit,
1726         .ndo_select_queue       = team_select_queue,
1727         .ndo_change_rx_flags    = team_change_rx_flags,
1728         .ndo_set_rx_mode        = team_set_rx_mode,
1729         .ndo_set_mac_address    = team_set_mac_address,
1730         .ndo_change_mtu         = team_change_mtu,
1731         .ndo_get_stats64        = team_get_stats64,
1732         .ndo_vlan_rx_add_vid    = team_vlan_rx_add_vid,
1733         .ndo_vlan_rx_kill_vid   = team_vlan_rx_kill_vid,
1734 #ifdef CONFIG_NET_POLL_CONTROLLER
1735         .ndo_poll_controller    = team_poll_controller,
1736         .ndo_netpoll_setup      = team_netpoll_setup,
1737         .ndo_netpoll_cleanup    = team_netpoll_cleanup,
1738 #endif
1739         .ndo_add_slave          = team_add_slave,
1740         .ndo_del_slave          = team_del_slave,
1741         .ndo_fix_features       = team_fix_features,
1742         .ndo_change_carrier     = team_change_carrier,
1743 };
1744
1745 /***********************
1746  * ethtool interface
1747  ***********************/
1748
1749 static void team_ethtool_get_drvinfo(struct net_device *dev,
1750                                      struct ethtool_drvinfo *drvinfo)
1751 {
1752         strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
1753         strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
1754 }
1755
1756 static const struct ethtool_ops team_ethtool_ops = {
1757         .get_drvinfo            = team_ethtool_get_drvinfo,
1758         .get_link               = ethtool_op_get_link,
1759 };
1760
1761 /***********************
1762  * rt netlink interface
1763  ***********************/
1764
1765 static void team_setup_by_port(struct net_device *dev,
1766                                struct net_device *port_dev)
1767 {
1768         dev->header_ops = port_dev->header_ops;
1769         dev->type = port_dev->type;
1770         dev->hard_header_len = port_dev->hard_header_len;
1771         dev->addr_len = port_dev->addr_len;
1772         dev->mtu = port_dev->mtu;
1773         memcpy(dev->broadcast, port_dev->broadcast, port_dev->addr_len);
1774         memcpy(dev->dev_addr, port_dev->dev_addr, port_dev->addr_len);
1775 }
1776
1777 static int team_dev_type_check_change(struct net_device *dev,
1778                                       struct net_device *port_dev)
1779 {
1780         struct team *team = netdev_priv(dev);
1781         char *portname = port_dev->name;
1782         int err;
1783
1784         if (dev->type == port_dev->type)
1785                 return 0;
1786         if (!list_empty(&team->port_list)) {
1787                 netdev_err(dev, "Device %s is of different type\n", portname);
1788                 return -EBUSY;
1789         }
1790         err = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE, dev);
1791         err = notifier_to_errno(err);
1792         if (err) {
1793                 netdev_err(dev, "Refused to change device type\n");
1794                 return err;
1795         }
1796         dev_uc_flush(dev);
1797         dev_mc_flush(dev);
1798         team_setup_by_port(dev, port_dev);
1799         call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
1800         return 0;
1801 }
1802
1803 static void team_setup(struct net_device *dev)
1804 {
1805         ether_setup(dev);
1806
1807         dev->netdev_ops = &team_netdev_ops;
1808         dev->ethtool_ops = &team_ethtool_ops;
1809         dev->destructor = team_destructor;
1810         dev->tx_queue_len = 0;
1811         dev->flags |= IFF_MULTICAST;
1812         dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
1813
1814         /*
1815          * Indicate we support unicast address filtering. That way core won't
1816          * bring us to promisc mode in case a unicast addr is added.
1817          * Let this up to underlay drivers.
1818          */
1819         dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
1820
1821         dev->features |= NETIF_F_LLTX;
1822         dev->features |= NETIF_F_GRO;
1823         dev->hw_features = TEAM_VLAN_FEATURES |
1824                            NETIF_F_HW_VLAN_TX |
1825                            NETIF_F_HW_VLAN_RX |
1826                            NETIF_F_HW_VLAN_FILTER;
1827
1828         dev->hw_features &= ~(NETIF_F_ALL_CSUM & ~NETIF_F_HW_CSUM);
1829         dev->features |= dev->hw_features;
1830 }
1831
1832 static int team_newlink(struct net *src_net, struct net_device *dev,
1833                         struct nlattr *tb[], struct nlattr *data[])
1834 {
1835         int err;
1836
1837         if (tb[IFLA_ADDRESS] == NULL)
1838                 eth_hw_addr_random(dev);
1839
1840         err = register_netdevice(dev);
1841         if (err)
1842                 return err;
1843
1844         return 0;
1845 }
1846
1847 static int team_validate(struct nlattr *tb[], struct nlattr *data[])
1848 {
1849         if (tb[IFLA_ADDRESS]) {
1850                 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1851                         return -EINVAL;
1852                 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1853                         return -EADDRNOTAVAIL;
1854         }
1855         return 0;
1856 }
1857
1858 static unsigned int team_get_num_tx_queues(void)
1859 {
1860         return TEAM_DEFAULT_NUM_TX_QUEUES;
1861 }
1862
1863 static unsigned int team_get_num_rx_queues(void)
1864 {
1865         return TEAM_DEFAULT_NUM_RX_QUEUES;
1866 }
1867
1868 static struct rtnl_link_ops team_link_ops __read_mostly = {
1869         .kind                   = DRV_NAME,
1870         .priv_size              = sizeof(struct team),
1871         .setup                  = team_setup,
1872         .newlink                = team_newlink,
1873         .validate               = team_validate,
1874         .get_num_tx_queues      = team_get_num_tx_queues,
1875         .get_num_rx_queues      = team_get_num_rx_queues,
1876 };
1877
1878
1879 /***********************************
1880  * Generic netlink custom interface
1881  ***********************************/
1882
1883 static struct genl_family team_nl_family = {
1884         .id             = GENL_ID_GENERATE,
1885         .name           = TEAM_GENL_NAME,
1886         .version        = TEAM_GENL_VERSION,
1887         .maxattr        = TEAM_ATTR_MAX,
1888         .netnsok        = true,
1889 };
1890
1891 static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
1892         [TEAM_ATTR_UNSPEC]                      = { .type = NLA_UNSPEC, },
1893         [TEAM_ATTR_TEAM_IFINDEX]                = { .type = NLA_U32 },
1894         [TEAM_ATTR_LIST_OPTION]                 = { .type = NLA_NESTED },
1895         [TEAM_ATTR_LIST_PORT]                   = { .type = NLA_NESTED },
1896 };
1897
1898 static const struct nla_policy
1899 team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
1900         [TEAM_ATTR_OPTION_UNSPEC]               = { .type = NLA_UNSPEC, },
1901         [TEAM_ATTR_OPTION_NAME] = {
1902                 .type = NLA_STRING,
1903                 .len = TEAM_STRING_MAX_LEN,
1904         },
1905         [TEAM_ATTR_OPTION_CHANGED]              = { .type = NLA_FLAG },
1906         [TEAM_ATTR_OPTION_TYPE]                 = { .type = NLA_U8 },
1907         [TEAM_ATTR_OPTION_DATA]                 = { .type = NLA_BINARY },
1908 };
1909
1910 static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
1911 {
1912         struct sk_buff *msg;
1913         void *hdr;
1914         int err;
1915
1916         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1917         if (!msg)
1918                 return -ENOMEM;
1919
1920         hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
1921                           &team_nl_family, 0, TEAM_CMD_NOOP);
1922         if (!hdr) {
1923                 err = -EMSGSIZE;
1924                 goto err_msg_put;
1925         }
1926
1927         genlmsg_end(msg, hdr);
1928
1929         return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
1930
1931 err_msg_put:
1932         nlmsg_free(msg);
1933
1934         return err;
1935 }
1936
1937 /*
1938  * Netlink cmd functions should be locked by following two functions.
1939  * Since dev gets held here, that ensures dev won't disappear in between.
1940  */
1941 static struct team *team_nl_team_get(struct genl_info *info)
1942 {
1943         struct net *net = genl_info_net(info);
1944         int ifindex;
1945         struct net_device *dev;
1946         struct team *team;
1947
1948         if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
1949                 return NULL;
1950
1951         ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
1952         dev = dev_get_by_index(net, ifindex);
1953         if (!dev || dev->netdev_ops != &team_netdev_ops) {
1954                 if (dev)
1955                         dev_put(dev);
1956                 return NULL;
1957         }
1958
1959         team = netdev_priv(dev);
1960         mutex_lock(&team->lock);
1961         return team;
1962 }
1963
1964 static void team_nl_team_put(struct team *team)
1965 {
1966         mutex_unlock(&team->lock);
1967         dev_put(team->dev);
1968 }
1969
1970 typedef int team_nl_send_func_t(struct sk_buff *skb,
1971                                 struct team *team, u32 portid);
1972
1973 static int team_nl_send_unicast(struct sk_buff *skb, struct team *team, u32 portid)
1974 {
1975         return genlmsg_unicast(dev_net(team->dev), skb, portid);
1976 }
1977
1978 static int team_nl_fill_one_option_get(struct sk_buff *skb, struct team *team,
1979                                        struct team_option_inst *opt_inst)
1980 {
1981         struct nlattr *option_item;
1982         struct team_option *option = opt_inst->option;
1983         struct team_option_inst_info *opt_inst_info = &opt_inst->info;
1984         struct team_gsetter_ctx ctx;
1985         int err;
1986
1987         ctx.info = opt_inst_info;
1988         err = team_option_get(team, opt_inst, &ctx);
1989         if (err)
1990                 return err;
1991
1992         option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
1993         if (!option_item)
1994                 return -EMSGSIZE;
1995
1996         if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name))
1997                 goto nest_cancel;
1998         if (opt_inst_info->port &&
1999             nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX,
2000                         opt_inst_info->port->dev->ifindex))
2001                 goto nest_cancel;
2002         if (opt_inst->option->array_size &&
2003             nla_put_u32(skb, TEAM_ATTR_OPTION_ARRAY_INDEX,
2004                         opt_inst_info->array_index))
2005                 goto nest_cancel;
2006
2007         switch (option->type) {
2008         case TEAM_OPTION_TYPE_U32:
2009                 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32))
2010                         goto nest_cancel;
2011                 if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.u32_val))
2012                         goto nest_cancel;
2013                 break;
2014         case TEAM_OPTION_TYPE_STRING:
2015                 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING))
2016                         goto nest_cancel;
2017                 if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA,
2018                                    ctx.data.str_val))
2019                         goto nest_cancel;
2020                 break;
2021         case TEAM_OPTION_TYPE_BINARY:
2022                 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY))
2023                         goto nest_cancel;
2024                 if (nla_put(skb, TEAM_ATTR_OPTION_DATA, ctx.data.bin_val.len,
2025                             ctx.data.bin_val.ptr))
2026                         goto nest_cancel;
2027                 break;
2028         case TEAM_OPTION_TYPE_BOOL:
2029                 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG))
2030                         goto nest_cancel;
2031                 if (ctx.data.bool_val &&
2032                     nla_put_flag(skb, TEAM_ATTR_OPTION_DATA))
2033                         goto nest_cancel;
2034                 break;
2035         case TEAM_OPTION_TYPE_S32:
2036                 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_S32))
2037                         goto nest_cancel;
2038                 if (nla_put_s32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.s32_val))
2039                         goto nest_cancel;
2040                 break;
2041         default:
2042                 BUG();
2043         }
2044         if (opt_inst->removed && nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED))
2045                 goto nest_cancel;
2046         if (opt_inst->changed) {
2047                 if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED))
2048                         goto nest_cancel;
2049                 opt_inst->changed = false;
2050         }
2051         nla_nest_end(skb, option_item);
2052         return 0;
2053
2054 nest_cancel:
2055         nla_nest_cancel(skb, option_item);
2056         return -EMSGSIZE;
2057 }
2058
2059 static int __send_and_alloc_skb(struct sk_buff **pskb,
2060                                 struct team *team, u32 portid,
2061                                 team_nl_send_func_t *send_func)
2062 {
2063         int err;
2064
2065         if (*pskb) {
2066                 err = send_func(*pskb, team, portid);
2067                 if (err)
2068                         return err;
2069         }
2070         *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2071         if (!*pskb)
2072                 return -ENOMEM;
2073         return 0;
2074 }
2075
2076 static int team_nl_send_options_get(struct team *team, u32 portid, u32 seq,
2077                                     int flags, team_nl_send_func_t *send_func,
2078                                     struct list_head *sel_opt_inst_list)
2079 {
2080         struct nlattr *option_list;
2081         struct nlmsghdr *nlh;
2082         void *hdr;
2083         struct team_option_inst *opt_inst;
2084         int err;
2085         struct sk_buff *skb = NULL;
2086         bool incomplete;
2087         int i;
2088
2089         opt_inst = list_first_entry(sel_opt_inst_list,
2090                                     struct team_option_inst, tmp_list);
2091
2092 start_again:
2093         err = __send_and_alloc_skb(&skb, team, portid, send_func);
2094         if (err)
2095                 return err;
2096
2097         hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
2098                           TEAM_CMD_OPTIONS_GET);
2099         if (!hdr)
2100                 return -EMSGSIZE;
2101
2102         if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2103                 goto nla_put_failure;
2104         option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
2105         if (!option_list)
2106                 goto nla_put_failure;
2107
2108         i = 0;
2109         incomplete = false;
2110         list_for_each_entry_from(opt_inst, sel_opt_inst_list, tmp_list) {
2111                 err = team_nl_fill_one_option_get(skb, team, opt_inst);
2112                 if (err) {
2113                         if (err == -EMSGSIZE) {
2114                                 if (!i)
2115                                         goto errout;
2116                                 incomplete = true;
2117                                 break;
2118                         }
2119                         goto errout;
2120                 }
2121                 i++;
2122         }
2123
2124         nla_nest_end(skb, option_list);
2125         genlmsg_end(skb, hdr);
2126         if (incomplete)
2127                 goto start_again;
2128
2129 send_done:
2130         nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
2131         if (!nlh) {
2132                 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2133                 if (err)
2134                         goto errout;
2135                 goto send_done;
2136         }
2137
2138         return send_func(skb, team, portid);
2139
2140 nla_put_failure:
2141         err = -EMSGSIZE;
2142 errout:
2143         genlmsg_cancel(skb, hdr);
2144         nlmsg_free(skb);
2145         return err;
2146 }
2147
2148 static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
2149 {
2150         struct team *team;
2151         struct team_option_inst *opt_inst;
2152         int err;
2153         LIST_HEAD(sel_opt_inst_list);
2154
2155         team = team_nl_team_get(info);
2156         if (!team)
2157                 return -EINVAL;
2158
2159         list_for_each_entry(opt_inst, &team->option_inst_list, list)
2160                 list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
2161         err = team_nl_send_options_get(team, info->snd_portid, info->snd_seq,
2162                                        NLM_F_ACK, team_nl_send_unicast,
2163                                        &sel_opt_inst_list);
2164
2165         team_nl_team_put(team);
2166
2167         return err;
2168 }
2169
2170 static int team_nl_send_event_options_get(struct team *team,
2171                                           struct list_head *sel_opt_inst_list);
2172
2173 static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
2174 {
2175         struct team *team;
2176         int err = 0;
2177         int i;
2178         struct nlattr *nl_option;
2179         LIST_HEAD(opt_inst_list);
2180
2181         team = team_nl_team_get(info);
2182         if (!team)
2183                 return -EINVAL;
2184
2185         err = -EINVAL;
2186         if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
2187                 err = -EINVAL;
2188                 goto team_put;
2189         }
2190
2191         nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
2192                 struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1];
2193                 struct nlattr *attr;
2194                 struct nlattr *attr_data;
2195                 enum team_option_type opt_type;
2196                 int opt_port_ifindex = 0; /* != 0 for per-port options */
2197                 u32 opt_array_index = 0;
2198                 bool opt_is_array = false;
2199                 struct team_option_inst *opt_inst;
2200                 char *opt_name;
2201                 bool opt_found = false;
2202
2203                 if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
2204                         err = -EINVAL;
2205                         goto team_put;
2206                 }
2207                 err = nla_parse_nested(opt_attrs, TEAM_ATTR_OPTION_MAX,
2208                                        nl_option, team_nl_option_policy);
2209                 if (err)
2210                         goto team_put;
2211                 if (!opt_attrs[TEAM_ATTR_OPTION_NAME] ||
2212                     !opt_attrs[TEAM_ATTR_OPTION_TYPE]) {
2213                         err = -EINVAL;
2214                         goto team_put;
2215                 }
2216                 switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) {
2217                 case NLA_U32:
2218                         opt_type = TEAM_OPTION_TYPE_U32;
2219                         break;
2220                 case NLA_STRING:
2221                         opt_type = TEAM_OPTION_TYPE_STRING;
2222                         break;
2223                 case NLA_BINARY:
2224                         opt_type = TEAM_OPTION_TYPE_BINARY;
2225                         break;
2226                 case NLA_FLAG:
2227                         opt_type = TEAM_OPTION_TYPE_BOOL;
2228                         break;
2229                 case NLA_S32:
2230                         opt_type = TEAM_OPTION_TYPE_S32;
2231                         break;
2232                 default:
2233                         goto team_put;
2234                 }
2235
2236                 attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA];
2237                 if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) {
2238                         err = -EINVAL;
2239                         goto team_put;
2240                 }
2241
2242                 opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]);
2243                 attr = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX];
2244                 if (attr)
2245                         opt_port_ifindex = nla_get_u32(attr);
2246
2247                 attr = opt_attrs[TEAM_ATTR_OPTION_ARRAY_INDEX];
2248                 if (attr) {
2249                         opt_is_array = true;
2250                         opt_array_index = nla_get_u32(attr);
2251                 }
2252
2253                 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2254                         struct team_option *option = opt_inst->option;
2255                         struct team_gsetter_ctx ctx;
2256                         struct team_option_inst_info *opt_inst_info;
2257                         int tmp_ifindex;
2258
2259                         opt_inst_info = &opt_inst->info;
2260                         tmp_ifindex = opt_inst_info->port ?
2261                                       opt_inst_info->port->dev->ifindex : 0;
2262                         if (option->type != opt_type ||
2263                             strcmp(option->name, opt_name) ||
2264                             tmp_ifindex != opt_port_ifindex ||
2265                             (option->array_size && !opt_is_array) ||
2266                             opt_inst_info->array_index != opt_array_index)
2267                                 continue;
2268                         opt_found = true;
2269                         ctx.info = opt_inst_info;
2270                         switch (opt_type) {
2271                         case TEAM_OPTION_TYPE_U32:
2272                                 ctx.data.u32_val = nla_get_u32(attr_data);
2273                                 break;
2274                         case TEAM_OPTION_TYPE_STRING:
2275                                 if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) {
2276                                         err = -EINVAL;
2277                                         goto team_put;
2278                                 }
2279                                 ctx.data.str_val = nla_data(attr_data);
2280                                 break;
2281                         case TEAM_OPTION_TYPE_BINARY:
2282                                 ctx.data.bin_val.len = nla_len(attr_data);
2283                                 ctx.data.bin_val.ptr = nla_data(attr_data);
2284                                 break;
2285                         case TEAM_OPTION_TYPE_BOOL:
2286                                 ctx.data.bool_val = attr_data ? true : false;
2287                                 break;
2288                         case TEAM_OPTION_TYPE_S32:
2289                                 ctx.data.s32_val = nla_get_s32(attr_data);
2290                                 break;
2291                         default:
2292                                 BUG();
2293                         }
2294                         err = team_option_set(team, opt_inst, &ctx);
2295                         if (err)
2296                                 goto team_put;
2297                         opt_inst->changed = true;
2298                         list_add(&opt_inst->tmp_list, &opt_inst_list);
2299                 }
2300                 if (!opt_found) {
2301                         err = -ENOENT;
2302                         goto team_put;
2303                 }
2304         }
2305
2306         err = team_nl_send_event_options_get(team, &opt_inst_list);
2307
2308 team_put:
2309         team_nl_team_put(team);
2310
2311         return err;
2312 }
2313
2314 static int team_nl_fill_one_port_get(struct sk_buff *skb,
2315                                      struct team_port *port)
2316 {
2317         struct nlattr *port_item;
2318
2319         port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
2320         if (!port_item)
2321                 goto nest_cancel;
2322         if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex))
2323                 goto nest_cancel;
2324         if (port->changed) {
2325                 if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED))
2326                         goto nest_cancel;
2327                 port->changed = false;
2328         }
2329         if ((port->removed &&
2330              nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) ||
2331             (port->state.linkup &&
2332              nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) ||
2333             nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) ||
2334             nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex))
2335                 goto nest_cancel;
2336         nla_nest_end(skb, port_item);
2337         return 0;
2338
2339 nest_cancel:
2340         nla_nest_cancel(skb, port_item);
2341         return -EMSGSIZE;
2342 }
2343
2344 static int team_nl_send_port_list_get(struct team *team, u32 portid, u32 seq,
2345                                       int flags, team_nl_send_func_t *send_func,
2346                                       struct team_port *one_port)
2347 {
2348         struct nlattr *port_list;
2349         struct nlmsghdr *nlh;
2350         void *hdr;
2351         struct team_port *port;
2352         int err;
2353         struct sk_buff *skb = NULL;
2354         bool incomplete;
2355         int i;
2356
2357         port = list_first_entry(&team->port_list, struct team_port, list);
2358
2359 start_again:
2360         err = __send_and_alloc_skb(&skb, team, portid, send_func);
2361         if (err)
2362                 return err;
2363
2364         hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
2365                           TEAM_CMD_PORT_LIST_GET);
2366         if (!hdr)
2367                 return -EMSGSIZE;
2368
2369         if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2370                 goto nla_put_failure;
2371         port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
2372         if (!port_list)
2373                 goto nla_put_failure;
2374
2375         i = 0;
2376         incomplete = false;
2377
2378         /* If one port is selected, called wants to send port list containing
2379          * only this port. Otherwise go through all listed ports and send all
2380          */
2381         if (one_port) {
2382                 err = team_nl_fill_one_port_get(skb, one_port);
2383                 if (err)
2384                         goto errout;
2385         } else {
2386                 list_for_each_entry(port, &team->port_list, list) {
2387                         err = team_nl_fill_one_port_get(skb, port);
2388                         if (err) {
2389                                 if (err == -EMSGSIZE) {
2390                                         if (!i)
2391                                                 goto errout;
2392                                         incomplete = true;
2393                                         break;
2394                                 }
2395                                 goto errout;
2396                         }
2397                         i++;
2398                 }
2399         }
2400
2401         nla_nest_end(skb, port_list);
2402         genlmsg_end(skb, hdr);
2403         if (incomplete)
2404                 goto start_again;
2405
2406 send_done:
2407         nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
2408         if (!nlh) {
2409                 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2410                 if (err)
2411                         goto errout;
2412                 goto send_done;
2413         }
2414
2415         return send_func(skb, team, portid);
2416
2417 nla_put_failure:
2418         err = -EMSGSIZE;
2419 errout:
2420         genlmsg_cancel(skb, hdr);
2421         nlmsg_free(skb);
2422         return err;
2423 }
2424
2425 static int team_nl_cmd_port_list_get(struct sk_buff *skb,
2426                                      struct genl_info *info)
2427 {
2428         struct team *team;
2429         int err;
2430
2431         team = team_nl_team_get(info);
2432         if (!team)
2433                 return -EINVAL;
2434
2435         err = team_nl_send_port_list_get(team, info->snd_portid, info->snd_seq,
2436                                          NLM_F_ACK, team_nl_send_unicast, NULL);
2437
2438         team_nl_team_put(team);
2439
2440         return err;
2441 }
2442
2443 static struct genl_ops team_nl_ops[] = {
2444         {
2445                 .cmd = TEAM_CMD_NOOP,
2446                 .doit = team_nl_cmd_noop,
2447                 .policy = team_nl_policy,
2448         },
2449         {
2450                 .cmd = TEAM_CMD_OPTIONS_SET,
2451                 .doit = team_nl_cmd_options_set,
2452                 .policy = team_nl_policy,
2453                 .flags = GENL_ADMIN_PERM,
2454         },
2455         {
2456                 .cmd = TEAM_CMD_OPTIONS_GET,
2457                 .doit = team_nl_cmd_options_get,
2458                 .policy = team_nl_policy,
2459                 .flags = GENL_ADMIN_PERM,
2460         },
2461         {
2462                 .cmd = TEAM_CMD_PORT_LIST_GET,
2463                 .doit = team_nl_cmd_port_list_get,
2464                 .policy = team_nl_policy,
2465                 .flags = GENL_ADMIN_PERM,
2466         },
2467 };
2468
2469 static struct genl_multicast_group team_change_event_mcgrp = {
2470         .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME,
2471 };
2472
2473 static int team_nl_send_multicast(struct sk_buff *skb,
2474                                   struct team *team, u32 portid)
2475 {
2476         return genlmsg_multicast_netns(dev_net(team->dev), skb, 0,
2477                                        team_change_event_mcgrp.id, GFP_KERNEL);
2478 }
2479
2480 static int team_nl_send_event_options_get(struct team *team,
2481                                           struct list_head *sel_opt_inst_list)
2482 {
2483         return team_nl_send_options_get(team, 0, 0, 0, team_nl_send_multicast,
2484                                         sel_opt_inst_list);
2485 }
2486
2487 static int team_nl_send_event_port_get(struct team *team,
2488                                        struct team_port *port)
2489 {
2490         return team_nl_send_port_list_get(team, 0, 0, 0, team_nl_send_multicast,
2491                                           port);
2492 }
2493
2494 static int team_nl_init(void)
2495 {
2496         int err;
2497
2498         err = genl_register_family_with_ops(&team_nl_family, team_nl_ops,
2499                                             ARRAY_SIZE(team_nl_ops));
2500         if (err)
2501                 return err;
2502
2503         err = genl_register_mc_group(&team_nl_family, &team_change_event_mcgrp);
2504         if (err)
2505                 goto err_change_event_grp_reg;
2506
2507         return 0;
2508
2509 err_change_event_grp_reg:
2510         genl_unregister_family(&team_nl_family);
2511
2512         return err;
2513 }
2514
2515 static void team_nl_fini(void)
2516 {
2517         genl_unregister_family(&team_nl_family);
2518 }
2519
2520
2521 /******************
2522  * Change checkers
2523  ******************/
2524
2525 static void __team_options_change_check(struct team *team)
2526 {
2527         int err;
2528         struct team_option_inst *opt_inst;
2529         LIST_HEAD(sel_opt_inst_list);
2530
2531         list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2532                 if (opt_inst->changed)
2533                         list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
2534         }
2535         err = team_nl_send_event_options_get(team, &sel_opt_inst_list);
2536         if (err && err != -ESRCH)
2537                 netdev_warn(team->dev, "Failed to send options change via netlink (err %d)\n",
2538                             err);
2539 }
2540
2541 /* rtnl lock is held */
2542
2543 static void __team_port_change_send(struct team_port *port, bool linkup)
2544 {
2545         int err;
2546
2547         port->changed = true;
2548         port->state.linkup = linkup;
2549         team_refresh_port_linkup(port);
2550         if (linkup) {
2551                 struct ethtool_cmd ecmd;
2552
2553                 err = __ethtool_get_settings(port->dev, &ecmd);
2554                 if (!err) {
2555                         port->state.speed = ethtool_cmd_speed(&ecmd);
2556                         port->state.duplex = ecmd.duplex;
2557                         goto send_event;
2558                 }
2559         }
2560         port->state.speed = 0;
2561         port->state.duplex = 0;
2562
2563 send_event:
2564         err = team_nl_send_event_port_get(port->team, port);
2565         if (err && err != -ESRCH)
2566                 netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink (err %d)\n",
2567                             port->dev->name, err);
2568
2569 }
2570
2571 static void __team_carrier_check(struct team *team)
2572 {
2573         struct team_port *port;
2574         bool team_linkup;
2575
2576         team_linkup = false;
2577         list_for_each_entry(port, &team->port_list, list) {
2578                 if (port->linkup) {
2579                         team_linkup = true;
2580                         break;
2581                 }
2582         }
2583
2584         if (team_linkup)
2585                 netif_carrier_on(team->dev);
2586         else
2587                 netif_carrier_off(team->dev);
2588 }
2589
2590 static void __team_port_change_check(struct team_port *port, bool linkup)
2591 {
2592         if (port->state.linkup != linkup)
2593                 __team_port_change_send(port, linkup);
2594         __team_carrier_check(port->team);
2595 }
2596
2597 static void __team_port_change_port_added(struct team_port *port, bool linkup)
2598 {
2599         __team_port_change_send(port, linkup);
2600         __team_carrier_check(port->team);
2601 }
2602
2603 static void __team_port_change_port_removed(struct team_port *port)
2604 {
2605         port->removed = true;
2606         __team_port_change_send(port, false);
2607         __team_carrier_check(port->team);
2608 }
2609
2610 static void team_port_change_check(struct team_port *port, bool linkup)
2611 {
2612         struct team *team = port->team;
2613
2614         mutex_lock(&team->lock);
2615         __team_port_change_check(port, linkup);
2616         mutex_unlock(&team->lock);
2617 }
2618
2619
2620 /************************************
2621  * Net device notifier event handler
2622  ************************************/
2623
2624 static int team_device_event(struct notifier_block *unused,
2625                              unsigned long event, void *ptr)
2626 {
2627         struct net_device *dev = (struct net_device *) ptr;
2628         struct team_port *port;
2629
2630         port = team_port_get_rtnl(dev);
2631         if (!port)
2632                 return NOTIFY_DONE;
2633
2634         switch (event) {
2635         case NETDEV_UP:
2636                 if (netif_carrier_ok(dev))
2637                         team_port_change_check(port, true);
2638         case NETDEV_DOWN:
2639                 team_port_change_check(port, false);
2640         case NETDEV_CHANGE:
2641                 if (netif_running(port->dev))
2642                         team_port_change_check(port,
2643                                                !!netif_carrier_ok(port->dev));
2644                 break;
2645         case NETDEV_UNREGISTER:
2646                 team_del_slave(port->team->dev, dev);
2647                 break;
2648         case NETDEV_FEAT_CHANGE:
2649                 team_compute_features(port->team);
2650                 break;
2651         case NETDEV_CHANGEMTU:
2652                 /* Forbid to change mtu of underlaying device */
2653                 return NOTIFY_BAD;
2654         case NETDEV_PRE_TYPE_CHANGE:
2655                 /* Forbid to change type of underlaying device */
2656                 return NOTIFY_BAD;
2657         }
2658         return NOTIFY_DONE;
2659 }
2660
2661 static struct notifier_block team_notifier_block __read_mostly = {
2662         .notifier_call = team_device_event,
2663 };
2664
2665
2666 /***********************
2667  * Module init and exit
2668  ***********************/
2669
2670 static int __init team_module_init(void)
2671 {
2672         int err;
2673
2674         register_netdevice_notifier(&team_notifier_block);
2675
2676         err = rtnl_link_register(&team_link_ops);
2677         if (err)
2678                 goto err_rtnl_reg;
2679
2680         err = team_nl_init();
2681         if (err)
2682                 goto err_nl_init;
2683
2684         return 0;
2685
2686 err_nl_init:
2687         rtnl_link_unregister(&team_link_ops);
2688
2689 err_rtnl_reg:
2690         unregister_netdevice_notifier(&team_notifier_block);
2691
2692         return err;
2693 }
2694
2695 static void __exit team_module_exit(void)
2696 {
2697         team_nl_fini();
2698         rtnl_link_unregister(&team_link_ops);
2699         unregister_netdevice_notifier(&team_notifier_block);
2700 }
2701
2702 module_init(team_module_init);
2703 module_exit(team_module_exit);
2704
2705 MODULE_LICENSE("GPL v2");
2706 MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
2707 MODULE_DESCRIPTION("Ethernet team device driver");
2708 MODULE_ALIAS_RTNL_LINK(DRV_NAME);