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