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