batman-adv: add bridge loop avoidance compile option
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / net / team / team.c
CommitLineData
3d249d4c
JP
1/*
2 * net/drivers/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>
87002b03 21#include <linux/if_vlan.h>
3d249d4c
JP
22#include <linux/if_arp.h>
23#include <linux/socket.h>
24#include <linux/etherdevice.h>
25#include <linux/rtnetlink.h>
26#include <net/rtnetlink.h>
27#include <net/genetlink.h>
28#include <net/netlink.h>
29#include <linux/if_team.h>
30
31#define DRV_NAME "team"
32
33
34/**********
35 * Helpers
36 **********/
37
38#define team_port_exists(dev) (dev->priv_flags & IFF_TEAM_PORT)
39
40static struct team_port *team_port_get_rcu(const struct net_device *dev)
41{
42 struct team_port *port = rcu_dereference(dev->rx_handler_data);
43
44 return team_port_exists(dev) ? port : NULL;
45}
46
47static struct team_port *team_port_get_rtnl(const struct net_device *dev)
48{
49 struct team_port *port = rtnl_dereference(dev->rx_handler_data);
50
51 return team_port_exists(dev) ? port : NULL;
52}
53
54/*
55 * Since the ability to change mac address for open port device is tested in
56 * team_port_add, this function can be called without control of return value
57 */
58static int __set_port_mac(struct net_device *port_dev,
59 const unsigned char *dev_addr)
60{
61 struct sockaddr addr;
62
63 memcpy(addr.sa_data, dev_addr, ETH_ALEN);
64 addr.sa_family = ARPHRD_ETHER;
65 return dev_set_mac_address(port_dev, &addr);
66}
67
68int team_port_set_orig_mac(struct team_port *port)
69{
70 return __set_port_mac(port->dev, port->orig.dev_addr);
71}
72
73int team_port_set_team_mac(struct team_port *port)
74{
75 return __set_port_mac(port->dev, port->team->dev->dev_addr);
76}
77EXPORT_SYMBOL(team_port_set_team_mac);
78
79
80/*******************
81 * Options handling
82 *******************/
83
358b8382
JP
84struct team_option *__team_find_option(struct team *team, const char *opt_name)
85{
86 struct team_option *option;
87
88 list_for_each_entry(option, &team->option_list, list) {
89 if (strcmp(option->name, opt_name) == 0)
90 return option;
91 }
92 return NULL;
93}
94
b82b9183
JP
95int __team_options_register(struct team *team,
96 const struct team_option *option,
97 size_t option_count)
3d249d4c
JP
98{
99 int i;
2bba19ff 100 struct team_option **dst_opts;
358b8382 101 int err;
3d249d4c 102
2bba19ff
JP
103 dst_opts = kzalloc(sizeof(struct team_option *) * option_count,
104 GFP_KERNEL);
105 if (!dst_opts)
106 return -ENOMEM;
358b8382 107 for (i = 0; i < option_count; i++, option++) {
358b8382
JP
108 if (__team_find_option(team, option->name)) {
109 err = -EEXIST;
110 goto rollback;
111 }
f8a15af0
JP
112 dst_opts[i] = kmemdup(option, sizeof(*option), GFP_KERNEL);
113 if (!dst_opts[i]) {
358b8382
JP
114 err = -ENOMEM;
115 goto rollback;
116 }
358b8382
JP
117 }
118
b82b9183
JP
119 for (i = 0; i < option_count; i++) {
120 dst_opts[i]->changed = true;
121 dst_opts[i]->removed = false;
358b8382 122 list_add_tail(&dst_opts[i]->list, &team->option_list);
b82b9183 123 }
358b8382 124
2bba19ff 125 kfree(dst_opts);
358b8382
JP
126 return 0;
127
128rollback:
129 for (i = 0; i < option_count; i++)
130 kfree(dst_opts[i]);
131
2bba19ff 132 kfree(dst_opts);
358b8382 133 return err;
3d249d4c 134}
358b8382 135
b82b9183
JP
136static void __team_options_mark_removed(struct team *team,
137 const struct team_option *option,
138 size_t option_count)
139{
140 int i;
141
142 for (i = 0; i < option_count; i++, option++) {
143 struct team_option *del_opt;
3d249d4c 144
b82b9183
JP
145 del_opt = __team_find_option(team, option->name);
146 if (del_opt) {
147 del_opt->changed = true;
148 del_opt->removed = true;
149 }
150 }
151}
3d249d4c
JP
152
153static void __team_options_unregister(struct team *team,
358b8382 154 const struct team_option *option,
3d249d4c
JP
155 size_t option_count)
156{
157 int i;
158
358b8382
JP
159 for (i = 0; i < option_count; i++, option++) {
160 struct team_option *del_opt;
161
162 del_opt = __team_find_option(team, option->name);
163 if (del_opt) {
164 list_del(&del_opt->list);
165 kfree(del_opt);
166 }
167 }
3d249d4c
JP
168}
169
b82b9183
JP
170static void __team_options_change_check(struct team *team);
171
172int team_options_register(struct team *team,
173 const struct team_option *option,
174 size_t option_count)
175{
176 int err;
177
178 err = __team_options_register(team, option, option_count);
179 if (err)
180 return err;
181 __team_options_change_check(team);
182 return 0;
183}
184EXPORT_SYMBOL(team_options_register);
185
358b8382
JP
186void team_options_unregister(struct team *team,
187 const struct team_option *option,
3d249d4c
JP
188 size_t option_count)
189{
b82b9183
JP
190 __team_options_mark_removed(team, option, option_count);
191 __team_options_change_check(team);
3d249d4c 192 __team_options_unregister(team, option, option_count);
3d249d4c
JP
193}
194EXPORT_SYMBOL(team_options_unregister);
195
196static int team_option_get(struct team *team, struct team_option *option,
197 void *arg)
198{
199 return option->getter(team, arg);
200}
201
202static int team_option_set(struct team *team, struct team_option *option,
203 void *arg)
204{
205 int err;
206
207 err = option->setter(team, arg);
208 if (err)
209 return err;
210
b82b9183
JP
211 option->changed = true;
212 __team_options_change_check(team);
3d249d4c
JP
213 return err;
214}
215
216/****************
217 * Mode handling
218 ****************/
219
220static LIST_HEAD(mode_list);
221static DEFINE_SPINLOCK(mode_list_lock);
222
223static struct team_mode *__find_mode(const char *kind)
224{
225 struct team_mode *mode;
226
227 list_for_each_entry(mode, &mode_list, list) {
228 if (strcmp(mode->kind, kind) == 0)
229 return mode;
230 }
231 return NULL;
232}
233
234static bool is_good_mode_name(const char *name)
235{
236 while (*name != '\0') {
237 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
238 return false;
239 name++;
240 }
241 return true;
242}
243
244int team_mode_register(struct team_mode *mode)
245{
246 int err = 0;
247
248 if (!is_good_mode_name(mode->kind) ||
249 mode->priv_size > TEAM_MODE_PRIV_SIZE)
250 return -EINVAL;
251 spin_lock(&mode_list_lock);
252 if (__find_mode(mode->kind)) {
253 err = -EEXIST;
254 goto unlock;
255 }
256 list_add_tail(&mode->list, &mode_list);
257unlock:
258 spin_unlock(&mode_list_lock);
259 return err;
260}
261EXPORT_SYMBOL(team_mode_register);
262
263int team_mode_unregister(struct team_mode *mode)
264{
265 spin_lock(&mode_list_lock);
266 list_del_init(&mode->list);
267 spin_unlock(&mode_list_lock);
268 return 0;
269}
270EXPORT_SYMBOL(team_mode_unregister);
271
272static struct team_mode *team_mode_get(const char *kind)
273{
274 struct team_mode *mode;
275
276 spin_lock(&mode_list_lock);
277 mode = __find_mode(kind);
278 if (!mode) {
279 spin_unlock(&mode_list_lock);
280 request_module("team-mode-%s", kind);
281 spin_lock(&mode_list_lock);
282 mode = __find_mode(kind);
283 }
284 if (mode)
285 if (!try_module_get(mode->owner))
286 mode = NULL;
287
288 spin_unlock(&mode_list_lock);
289 return mode;
290}
291
292static void team_mode_put(const struct team_mode *mode)
293{
294 module_put(mode->owner);
295}
296
297static bool team_dummy_transmit(struct team *team, struct sk_buff *skb)
298{
299 dev_kfree_skb_any(skb);
300 return false;
301}
302
303rx_handler_result_t team_dummy_receive(struct team *team,
304 struct team_port *port,
305 struct sk_buff *skb)
306{
307 return RX_HANDLER_ANOTHER;
308}
309
310static void team_adjust_ops(struct team *team)
311{
312 /*
313 * To avoid checks in rx/tx skb paths, ensure here that non-null and
314 * correct ops are always set.
315 */
316
317 if (list_empty(&team->port_list) ||
318 !team->mode || !team->mode->ops->transmit)
319 team->ops.transmit = team_dummy_transmit;
320 else
321 team->ops.transmit = team->mode->ops->transmit;
322
323 if (list_empty(&team->port_list) ||
324 !team->mode || !team->mode->ops->receive)
325 team->ops.receive = team_dummy_receive;
326 else
327 team->ops.receive = team->mode->ops->receive;
328}
329
330/*
331 * We can benefit from the fact that it's ensured no port is present
332 * at the time of mode change. Therefore no packets are in fly so there's no
333 * need to set mode operations in any special way.
334 */
335static int __team_change_mode(struct team *team,
336 const struct team_mode *new_mode)
337{
338 /* Check if mode was previously set and do cleanup if so */
339 if (team->mode) {
340 void (*exit_op)(struct team *team) = team->ops.exit;
341
342 /* Clear ops area so no callback is called any longer */
343 memset(&team->ops, 0, sizeof(struct team_mode_ops));
344 team_adjust_ops(team);
345
346 if (exit_op)
347 exit_op(team);
348 team_mode_put(team->mode);
349 team->mode = NULL;
350 /* zero private data area */
351 memset(&team->mode_priv, 0,
352 sizeof(struct team) - offsetof(struct team, mode_priv));
353 }
354
355 if (!new_mode)
356 return 0;
357
358 if (new_mode->ops->init) {
359 int err;
360
361 err = new_mode->ops->init(team);
362 if (err)
363 return err;
364 }
365
366 team->mode = new_mode;
367 memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops));
368 team_adjust_ops(team);
369
370 return 0;
371}
372
373static int team_change_mode(struct team *team, const char *kind)
374{
375 struct team_mode *new_mode;
376 struct net_device *dev = team->dev;
377 int err;
378
379 if (!list_empty(&team->port_list)) {
380 netdev_err(dev, "No ports can be present during mode change\n");
381 return -EBUSY;
382 }
383
384 if (team->mode && strcmp(team->mode->kind, kind) == 0) {
385 netdev_err(dev, "Unable to change to the same mode the team is in\n");
386 return -EINVAL;
387 }
388
389 new_mode = team_mode_get(kind);
390 if (!new_mode) {
391 netdev_err(dev, "Mode \"%s\" not found\n", kind);
392 return -EINVAL;
393 }
394
395 err = __team_change_mode(team, new_mode);
396 if (err) {
397 netdev_err(dev, "Failed to change to mode \"%s\"\n", kind);
398 team_mode_put(new_mode);
399 return err;
400 }
401
402 netdev_info(dev, "Mode changed to \"%s\"\n", kind);
403 return 0;
404}
405
406
407/************************
408 * Rx path frame handler
409 ************************/
410
411/* note: already called with rcu_read_lock */
412static rx_handler_result_t team_handle_frame(struct sk_buff **pskb)
413{
414 struct sk_buff *skb = *pskb;
415 struct team_port *port;
416 struct team *team;
417 rx_handler_result_t res;
418
419 skb = skb_share_check(skb, GFP_ATOMIC);
420 if (!skb)
421 return RX_HANDLER_CONSUMED;
422
423 *pskb = skb;
424
425 port = team_port_get_rcu(skb->dev);
426 team = port->team;
427
428 res = team->ops.receive(team, port, skb);
429 if (res == RX_HANDLER_ANOTHER) {
430 struct team_pcpu_stats *pcpu_stats;
431
432 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
433 u64_stats_update_begin(&pcpu_stats->syncp);
434 pcpu_stats->rx_packets++;
435 pcpu_stats->rx_bytes += skb->len;
436 if (skb->pkt_type == PACKET_MULTICAST)
437 pcpu_stats->rx_multicast++;
438 u64_stats_update_end(&pcpu_stats->syncp);
439
440 skb->dev = team->dev;
441 } else {
442 this_cpu_inc(team->pcpu_stats->rx_dropped);
443 }
444
445 return res;
446}
447
448
449/****************
450 * Port handling
451 ****************/
452
453static bool team_port_find(const struct team *team,
454 const struct team_port *port)
455{
456 struct team_port *cur;
457
458 list_for_each_entry(cur, &team->port_list, list)
459 if (cur == port)
460 return true;
461 return false;
462}
463
464/*
465 * Add/delete port to the team port list. Write guarded by rtnl_lock.
466 * Takes care of correct port->index setup (might be racy).
467 */
468static void team_port_list_add_port(struct team *team,
469 struct team_port *port)
470{
471 port->index = team->port_count++;
472 hlist_add_head_rcu(&port->hlist,
473 team_port_index_hash(team, port->index));
474 list_add_tail_rcu(&port->list, &team->port_list);
475}
476
477static void __reconstruct_port_hlist(struct team *team, int rm_index)
478{
479 int i;
480 struct team_port *port;
481
482 for (i = rm_index + 1; i < team->port_count; i++) {
483 port = team_get_port_by_index(team, i);
484 hlist_del_rcu(&port->hlist);
485 port->index--;
486 hlist_add_head_rcu(&port->hlist,
487 team_port_index_hash(team, port->index));
488 }
489}
490
491static void team_port_list_del_port(struct team *team,
492 struct team_port *port)
493{
494 int rm_index = port->index;
495
496 hlist_del_rcu(&port->hlist);
497 list_del_rcu(&port->list);
498 __reconstruct_port_hlist(team, rm_index);
499 team->port_count--;
500}
501
502#define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \
503 NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
504 NETIF_F_HIGHDMA | NETIF_F_LRO)
505
506static void __team_compute_features(struct team *team)
507{
508 struct team_port *port;
509 u32 vlan_features = TEAM_VLAN_FEATURES;
510 unsigned short max_hard_header_len = ETH_HLEN;
511
512 list_for_each_entry(port, &team->port_list, list) {
513 vlan_features = netdev_increment_features(vlan_features,
514 port->dev->vlan_features,
515 TEAM_VLAN_FEATURES);
516
517 if (port->dev->hard_header_len > max_hard_header_len)
518 max_hard_header_len = port->dev->hard_header_len;
519 }
520
521 team->dev->vlan_features = vlan_features;
522 team->dev->hard_header_len = max_hard_header_len;
523
524 netdev_change_features(team->dev);
525}
526
527static void team_compute_features(struct team *team)
528{
61dc3461 529 mutex_lock(&team->lock);
3d249d4c 530 __team_compute_features(team);
61dc3461 531 mutex_unlock(&team->lock);
3d249d4c
JP
532}
533
534static int team_port_enter(struct team *team, struct team_port *port)
535{
536 int err = 0;
537
538 dev_hold(team->dev);
539 port->dev->priv_flags |= IFF_TEAM_PORT;
540 if (team->ops.port_enter) {
541 err = team->ops.port_enter(team, port);
542 if (err) {
543 netdev_err(team->dev, "Device %s failed to enter team mode\n",
544 port->dev->name);
545 goto err_port_enter;
546 }
547 }
548
549 return 0;
550
551err_port_enter:
552 port->dev->priv_flags &= ~IFF_TEAM_PORT;
553 dev_put(team->dev);
554
555 return err;
556}
557
558static void team_port_leave(struct team *team, struct team_port *port)
559{
560 if (team->ops.port_leave)
561 team->ops.port_leave(team, port);
562 port->dev->priv_flags &= ~IFF_TEAM_PORT;
563 dev_put(team->dev);
564}
565
566static void __team_port_change_check(struct team_port *port, bool linkup);
567
568static int team_port_add(struct team *team, struct net_device *port_dev)
569{
570 struct net_device *dev = team->dev;
571 struct team_port *port;
572 char *portname = port_dev->name;
573 int err;
574
575 if (port_dev->flags & IFF_LOOPBACK ||
576 port_dev->type != ARPHRD_ETHER) {
577 netdev_err(dev, "Device %s is of an unsupported type\n",
578 portname);
579 return -EINVAL;
580 }
581
582 if (team_port_exists(port_dev)) {
583 netdev_err(dev, "Device %s is already a port "
584 "of a team device\n", portname);
585 return -EBUSY;
586 }
587
588 if (port_dev->flags & IFF_UP) {
589 netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
590 portname);
591 return -EBUSY;
592 }
593
594 port = kzalloc(sizeof(struct team_port), GFP_KERNEL);
595 if (!port)
596 return -ENOMEM;
597
598 port->dev = port_dev;
599 port->team = team;
600
601 port->orig.mtu = port_dev->mtu;
602 err = dev_set_mtu(port_dev, dev->mtu);
603 if (err) {
604 netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
605 goto err_set_mtu;
606 }
607
608 memcpy(port->orig.dev_addr, port_dev->dev_addr, ETH_ALEN);
609
610 err = team_port_enter(team, port);
611 if (err) {
612 netdev_err(dev, "Device %s failed to enter team mode\n",
613 portname);
614 goto err_port_enter;
615 }
616
617 err = dev_open(port_dev);
618 if (err) {
619 netdev_dbg(dev, "Device %s opening failed\n",
620 portname);
621 goto err_dev_open;
622 }
623
57459185
JP
624 err = vlan_vids_add_by_dev(port_dev, dev);
625 if (err) {
626 netdev_err(dev, "Failed to add vlan ids to device %s\n",
627 portname);
628 goto err_vids_add;
629 }
630
3d249d4c
JP
631 err = netdev_set_master(port_dev, dev);
632 if (err) {
633 netdev_err(dev, "Device %s failed to set master\n", portname);
634 goto err_set_master;
635 }
636
637 err = netdev_rx_handler_register(port_dev, team_handle_frame,
638 port);
639 if (err) {
640 netdev_err(dev, "Device %s failed to register rx_handler\n",
641 portname);
642 goto err_handler_register;
643 }
644
645 team_port_list_add_port(team, port);
646 team_adjust_ops(team);
647 __team_compute_features(team);
648 __team_port_change_check(port, !!netif_carrier_ok(port_dev));
649
650 netdev_info(dev, "Port device %s added\n", portname);
651
652 return 0;
653
654err_handler_register:
655 netdev_set_master(port_dev, NULL);
656
657err_set_master:
57459185
JP
658 vlan_vids_del_by_dev(port_dev, dev);
659
660err_vids_add:
3d249d4c
JP
661 dev_close(port_dev);
662
663err_dev_open:
664 team_port_leave(team, port);
665 team_port_set_orig_mac(port);
666
667err_port_enter:
668 dev_set_mtu(port_dev, port->orig.mtu);
669
670err_set_mtu:
671 kfree(port);
672
673 return err;
674}
675
676static int team_port_del(struct team *team, struct net_device *port_dev)
677{
678 struct net_device *dev = team->dev;
679 struct team_port *port;
680 char *portname = port_dev->name;
681
682 port = team_port_get_rtnl(port_dev);
683 if (!port || !team_port_find(team, port)) {
684 netdev_err(dev, "Device %s does not act as a port of this team\n",
685 portname);
686 return -ENOENT;
687 }
688
b82b9183 689 port->removed = true;
3d249d4c
JP
690 __team_port_change_check(port, false);
691 team_port_list_del_port(team, port);
692 team_adjust_ops(team);
693 netdev_rx_handler_unregister(port_dev);
694 netdev_set_master(port_dev, NULL);
57459185 695 vlan_vids_del_by_dev(port_dev, dev);
3d249d4c
JP
696 dev_close(port_dev);
697 team_port_leave(team, port);
698 team_port_set_orig_mac(port);
699 dev_set_mtu(port_dev, port->orig.mtu);
700 synchronize_rcu();
701 kfree(port);
702 netdev_info(dev, "Port device %s removed\n", portname);
703 __team_compute_features(team);
704
705 return 0;
706}
707
708
709/*****************
710 * Net device ops
711 *****************/
712
713static const char team_no_mode_kind[] = "*NOMODE*";
714
715static int team_mode_option_get(struct team *team, void *arg)
716{
717 const char **str = arg;
718
719 *str = team->mode ? team->mode->kind : team_no_mode_kind;
720 return 0;
721}
722
723static int team_mode_option_set(struct team *team, void *arg)
724{
725 const char **str = arg;
726
727 return team_change_mode(team, *str);
728}
729
358b8382 730static const struct team_option team_options[] = {
3d249d4c
JP
731 {
732 .name = "mode",
733 .type = TEAM_OPTION_TYPE_STRING,
734 .getter = team_mode_option_get,
735 .setter = team_mode_option_set,
736 },
737};
738
739static int team_init(struct net_device *dev)
740{
741 struct team *team = netdev_priv(dev);
742 int i;
358b8382 743 int err;
3d249d4c
JP
744
745 team->dev = dev;
61dc3461 746 mutex_init(&team->lock);
3d249d4c
JP
747
748 team->pcpu_stats = alloc_percpu(struct team_pcpu_stats);
749 if (!team->pcpu_stats)
750 return -ENOMEM;
751
752 for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
753 INIT_HLIST_HEAD(&team->port_hlist[i]);
754 INIT_LIST_HEAD(&team->port_list);
755
756 team_adjust_ops(team);
757
758 INIT_LIST_HEAD(&team->option_list);
358b8382
JP
759 err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
760 if (err)
761 goto err_options_register;
3d249d4c
JP
762 netif_carrier_off(dev);
763
764 return 0;
358b8382
JP
765
766err_options_register:
767 free_percpu(team->pcpu_stats);
768
769 return err;
3d249d4c
JP
770}
771
772static void team_uninit(struct net_device *dev)
773{
774 struct team *team = netdev_priv(dev);
775 struct team_port *port;
776 struct team_port *tmp;
777
61dc3461 778 mutex_lock(&team->lock);
3d249d4c
JP
779 list_for_each_entry_safe(port, tmp, &team->port_list, list)
780 team_port_del(team, port->dev);
781
782 __team_change_mode(team, NULL); /* cleanup */
783 __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
61dc3461 784 mutex_unlock(&team->lock);
3d249d4c
JP
785}
786
787static void team_destructor(struct net_device *dev)
788{
789 struct team *team = netdev_priv(dev);
790
791 free_percpu(team->pcpu_stats);
792 free_netdev(dev);
793}
794
795static int team_open(struct net_device *dev)
796{
797 netif_carrier_on(dev);
798 return 0;
799}
800
801static int team_close(struct net_device *dev)
802{
803 netif_carrier_off(dev);
804 return 0;
805}
806
807/*
808 * note: already called with rcu_read_lock
809 */
810static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
811{
812 struct team *team = netdev_priv(dev);
813 bool tx_success = false;
814 unsigned int len = skb->len;
815
816 tx_success = team->ops.transmit(team, skb);
817 if (tx_success) {
818 struct team_pcpu_stats *pcpu_stats;
819
820 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
821 u64_stats_update_begin(&pcpu_stats->syncp);
822 pcpu_stats->tx_packets++;
823 pcpu_stats->tx_bytes += len;
824 u64_stats_update_end(&pcpu_stats->syncp);
825 } else {
826 this_cpu_inc(team->pcpu_stats->tx_dropped);
827 }
828
829 return NETDEV_TX_OK;
830}
831
832static void team_change_rx_flags(struct net_device *dev, int change)
833{
834 struct team *team = netdev_priv(dev);
835 struct team_port *port;
836 int inc;
837
838 rcu_read_lock();
839 list_for_each_entry_rcu(port, &team->port_list, list) {
840 if (change & IFF_PROMISC) {
841 inc = dev->flags & IFF_PROMISC ? 1 : -1;
842 dev_set_promiscuity(port->dev, inc);
843 }
844 if (change & IFF_ALLMULTI) {
845 inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
846 dev_set_allmulti(port->dev, inc);
847 }
848 }
849 rcu_read_unlock();
850}
851
852static void team_set_rx_mode(struct net_device *dev)
853{
854 struct team *team = netdev_priv(dev);
855 struct team_port *port;
856
857 rcu_read_lock();
858 list_for_each_entry_rcu(port, &team->port_list, list) {
859 dev_uc_sync(port->dev, dev);
860 dev_mc_sync(port->dev, dev);
861 }
862 rcu_read_unlock();
863}
864
865static int team_set_mac_address(struct net_device *dev, void *p)
866{
867 struct team *team = netdev_priv(dev);
868 struct team_port *port;
869 struct sockaddr *addr = p;
870
7ce5d222 871 dev->addr_assign_type &= ~NET_ADDR_RANDOM;
3d249d4c
JP
872 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
873 rcu_read_lock();
874 list_for_each_entry_rcu(port, &team->port_list, list)
875 if (team->ops.port_change_mac)
876 team->ops.port_change_mac(team, port);
877 rcu_read_unlock();
878 return 0;
879}
880
881static int team_change_mtu(struct net_device *dev, int new_mtu)
882{
883 struct team *team = netdev_priv(dev);
884 struct team_port *port;
885 int err;
886
887 /*
888 * Alhough this is reader, it's guarded by team lock. It's not possible
889 * to traverse list in reverse under rcu_read_lock
890 */
61dc3461 891 mutex_lock(&team->lock);
3d249d4c
JP
892 list_for_each_entry(port, &team->port_list, list) {
893 err = dev_set_mtu(port->dev, new_mtu);
894 if (err) {
895 netdev_err(dev, "Device %s failed to change mtu",
896 port->dev->name);
897 goto unwind;
898 }
899 }
61dc3461 900 mutex_unlock(&team->lock);
3d249d4c
JP
901
902 dev->mtu = new_mtu;
903
904 return 0;
905
906unwind:
907 list_for_each_entry_continue_reverse(port, &team->port_list, list)
908 dev_set_mtu(port->dev, dev->mtu);
61dc3461 909 mutex_unlock(&team->lock);
3d249d4c
JP
910
911 return err;
912}
913
914static struct rtnl_link_stats64 *
915team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
916{
917 struct team *team = netdev_priv(dev);
918 struct team_pcpu_stats *p;
919 u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
920 u32 rx_dropped = 0, tx_dropped = 0;
921 unsigned int start;
922 int i;
923
924 for_each_possible_cpu(i) {
925 p = per_cpu_ptr(team->pcpu_stats, i);
926 do {
927 start = u64_stats_fetch_begin_bh(&p->syncp);
928 rx_packets = p->rx_packets;
929 rx_bytes = p->rx_bytes;
930 rx_multicast = p->rx_multicast;
931 tx_packets = p->tx_packets;
932 tx_bytes = p->tx_bytes;
933 } while (u64_stats_fetch_retry_bh(&p->syncp, start));
934
935 stats->rx_packets += rx_packets;
936 stats->rx_bytes += rx_bytes;
937 stats->multicast += rx_multicast;
938 stats->tx_packets += tx_packets;
939 stats->tx_bytes += tx_bytes;
940 /*
941 * rx_dropped & tx_dropped are u32, updated
942 * without syncp protection.
943 */
944 rx_dropped += p->rx_dropped;
945 tx_dropped += p->tx_dropped;
946 }
947 stats->rx_dropped = rx_dropped;
948 stats->tx_dropped = tx_dropped;
949 return stats;
950}
951
8e586137 952static int team_vlan_rx_add_vid(struct net_device *dev, uint16_t vid)
3d249d4c
JP
953{
954 struct team *team = netdev_priv(dev);
955 struct team_port *port;
87002b03 956 int err;
3d249d4c 957
87002b03
JP
958 /*
959 * Alhough this is reader, it's guarded by team lock. It's not possible
960 * to traverse list in reverse under rcu_read_lock
961 */
962 mutex_lock(&team->lock);
963 list_for_each_entry(port, &team->port_list, list) {
964 err = vlan_vid_add(port->dev, vid);
965 if (err)
966 goto unwind;
3d249d4c 967 }
87002b03 968 mutex_unlock(&team->lock);
8e586137
JP
969
970 return 0;
87002b03
JP
971
972unwind:
973 list_for_each_entry_continue_reverse(port, &team->port_list, list)
974 vlan_vid_del(port->dev, vid);
975 mutex_unlock(&team->lock);
976
977 return err;
3d249d4c
JP
978}
979
8e586137 980static int team_vlan_rx_kill_vid(struct net_device *dev, uint16_t vid)
3d249d4c
JP
981{
982 struct team *team = netdev_priv(dev);
983 struct team_port *port;
984
985 rcu_read_lock();
87002b03
JP
986 list_for_each_entry_rcu(port, &team->port_list, list)
987 vlan_vid_del(port->dev, vid);
3d249d4c 988 rcu_read_unlock();
8e586137
JP
989
990 return 0;
3d249d4c
JP
991}
992
993static int team_add_slave(struct net_device *dev, struct net_device *port_dev)
994{
995 struct team *team = netdev_priv(dev);
996 int err;
997
61dc3461 998 mutex_lock(&team->lock);
3d249d4c 999 err = team_port_add(team, port_dev);
61dc3461 1000 mutex_unlock(&team->lock);
3d249d4c
JP
1001 return err;
1002}
1003
1004static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
1005{
1006 struct team *team = netdev_priv(dev);
1007 int err;
1008
61dc3461 1009 mutex_lock(&team->lock);
3d249d4c 1010 err = team_port_del(team, port_dev);
61dc3461 1011 mutex_unlock(&team->lock);
3d249d4c
JP
1012 return err;
1013}
1014
234a8fd4
JP
1015static netdev_features_t team_fix_features(struct net_device *dev,
1016 netdev_features_t features)
1017{
1018 struct team_port *port;
1019 struct team *team = netdev_priv(dev);
1020 netdev_features_t mask;
1021
1022 mask = features;
1023 features &= ~NETIF_F_ONE_FOR_ALL;
1024 features |= NETIF_F_ALL_FOR_ALL;
1025
1026 rcu_read_lock();
1027 list_for_each_entry_rcu(port, &team->port_list, list) {
1028 features = netdev_increment_features(features,
1029 port->dev->features,
1030 mask);
1031 }
1032 rcu_read_unlock();
1033 return features;
1034}
1035
3d249d4c
JP
1036static const struct net_device_ops team_netdev_ops = {
1037 .ndo_init = team_init,
1038 .ndo_uninit = team_uninit,
1039 .ndo_open = team_open,
1040 .ndo_stop = team_close,
1041 .ndo_start_xmit = team_xmit,
1042 .ndo_change_rx_flags = team_change_rx_flags,
1043 .ndo_set_rx_mode = team_set_rx_mode,
1044 .ndo_set_mac_address = team_set_mac_address,
1045 .ndo_change_mtu = team_change_mtu,
1046 .ndo_get_stats64 = team_get_stats64,
1047 .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid,
1048 .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid,
1049 .ndo_add_slave = team_add_slave,
1050 .ndo_del_slave = team_del_slave,
234a8fd4 1051 .ndo_fix_features = team_fix_features,
3d249d4c
JP
1052};
1053
1054
1055/***********************
1056 * rt netlink interface
1057 ***********************/
1058
1059static void team_setup(struct net_device *dev)
1060{
1061 ether_setup(dev);
1062
1063 dev->netdev_ops = &team_netdev_ops;
1064 dev->destructor = team_destructor;
1065 dev->tx_queue_len = 0;
1066 dev->flags |= IFF_MULTICAST;
1067 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
1068
1069 /*
1070 * Indicate we support unicast address filtering. That way core won't
1071 * bring us to promisc mode in case a unicast addr is added.
1072 * Let this up to underlay drivers.
1073 */
1074 dev->priv_flags |= IFF_UNICAST_FLT;
1075
1076 dev->features |= NETIF_F_LLTX;
1077 dev->features |= NETIF_F_GRO;
1078 dev->hw_features = NETIF_F_HW_VLAN_TX |
1079 NETIF_F_HW_VLAN_RX |
1080 NETIF_F_HW_VLAN_FILTER;
1081
1082 dev->features |= dev->hw_features;
1083}
1084
1085static int team_newlink(struct net *src_net, struct net_device *dev,
1086 struct nlattr *tb[], struct nlattr *data[])
1087{
1088 int err;
1089
1090 if (tb[IFLA_ADDRESS] == NULL)
7ce5d222 1091 eth_hw_addr_random(dev);
3d249d4c
JP
1092
1093 err = register_netdevice(dev);
1094 if (err)
1095 return err;
1096
1097 return 0;
1098}
1099
1100static int team_validate(struct nlattr *tb[], struct nlattr *data[])
1101{
1102 if (tb[IFLA_ADDRESS]) {
1103 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1104 return -EINVAL;
1105 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1106 return -EADDRNOTAVAIL;
1107 }
1108 return 0;
1109}
1110
1111static struct rtnl_link_ops team_link_ops __read_mostly = {
1112 .kind = DRV_NAME,
1113 .priv_size = sizeof(struct team),
1114 .setup = team_setup,
1115 .newlink = team_newlink,
1116 .validate = team_validate,
1117};
1118
1119
1120/***********************************
1121 * Generic netlink custom interface
1122 ***********************************/
1123
1124static struct genl_family team_nl_family = {
1125 .id = GENL_ID_GENERATE,
1126 .name = TEAM_GENL_NAME,
1127 .version = TEAM_GENL_VERSION,
1128 .maxattr = TEAM_ATTR_MAX,
1129 .netnsok = true,
1130};
1131
1132static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
1133 [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, },
1134 [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 },
1135 [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED },
1136 [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED },
1137};
1138
1139static const struct nla_policy
1140team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
1141 [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
1142 [TEAM_ATTR_OPTION_NAME] = {
1143 .type = NLA_STRING,
1144 .len = TEAM_STRING_MAX_LEN,
1145 },
1146 [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG },
1147 [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 },
2615598f 1148 [TEAM_ATTR_OPTION_DATA] = { .type = NLA_BINARY },
3d249d4c
JP
1149};
1150
1151static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
1152{
1153 struct sk_buff *msg;
1154 void *hdr;
1155 int err;
1156
1157 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1158 if (!msg)
1159 return -ENOMEM;
1160
1161 hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq,
1162 &team_nl_family, 0, TEAM_CMD_NOOP);
1163 if (IS_ERR(hdr)) {
1164 err = PTR_ERR(hdr);
1165 goto err_msg_put;
1166 }
1167
1168 genlmsg_end(msg, hdr);
1169
1170 return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid);
1171
1172err_msg_put:
1173 nlmsg_free(msg);
1174
1175 return err;
1176}
1177
1178/*
1179 * Netlink cmd functions should be locked by following two functions.
8c0713a5 1180 * Since dev gets held here, that ensures dev won't disappear in between.
3d249d4c
JP
1181 */
1182static struct team *team_nl_team_get(struct genl_info *info)
1183{
1184 struct net *net = genl_info_net(info);
1185 int ifindex;
1186 struct net_device *dev;
1187 struct team *team;
1188
1189 if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
1190 return NULL;
1191
1192 ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
8c0713a5 1193 dev = dev_get_by_index(net, ifindex);
3d249d4c 1194 if (!dev || dev->netdev_ops != &team_netdev_ops) {
8c0713a5
JP
1195 if (dev)
1196 dev_put(dev);
3d249d4c
JP
1197 return NULL;
1198 }
1199
1200 team = netdev_priv(dev);
61dc3461 1201 mutex_lock(&team->lock);
3d249d4c
JP
1202 return team;
1203}
1204
1205static void team_nl_team_put(struct team *team)
1206{
61dc3461 1207 mutex_unlock(&team->lock);
8c0713a5 1208 dev_put(team->dev);
3d249d4c
JP
1209}
1210
1211static int team_nl_send_generic(struct genl_info *info, struct team *team,
1212 int (*fill_func)(struct sk_buff *skb,
1213 struct genl_info *info,
1214 int flags, struct team *team))
1215{
1216 struct sk_buff *skb;
1217 int err;
1218
1219 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1220 if (!skb)
1221 return -ENOMEM;
1222
1223 err = fill_func(skb, info, NLM_F_ACK, team);
1224 if (err < 0)
1225 goto err_fill;
1226
1227 err = genlmsg_unicast(genl_info_net(info), skb, info->snd_pid);
1228 return err;
1229
1230err_fill:
1231 nlmsg_free(skb);
1232 return err;
1233}
1234
b82b9183
JP
1235static int team_nl_fill_options_get(struct sk_buff *skb,
1236 u32 pid, u32 seq, int flags,
1237 struct team *team, bool fillall)
3d249d4c
JP
1238{
1239 struct nlattr *option_list;
1240 void *hdr;
1241 struct team_option *option;
1242
1243 hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags,
1244 TEAM_CMD_OPTIONS_GET);
1245 if (IS_ERR(hdr))
1246 return PTR_ERR(hdr);
1247
86ebb02d
DM
1248 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
1249 goto nla_put_failure;
3d249d4c
JP
1250 option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
1251 if (!option_list)
1252 return -EMSGSIZE;
1253
1254 list_for_each_entry(option, &team->option_list, list) {
1255 struct nlattr *option_item;
1256 long arg;
2615598f 1257 struct team_option_binary tbinary;
3d249d4c 1258
b82b9183
JP
1259 /* Include only changed options if fill all mode is not on */
1260 if (!fillall && !option->changed)
1261 continue;
3d249d4c
JP
1262 option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
1263 if (!option_item)
1264 goto nla_put_failure;
86ebb02d
DM
1265 if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name))
1266 goto nla_put_failure;
b82b9183 1267 if (option->changed) {
86ebb02d
DM
1268 if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED))
1269 goto nla_put_failure;
b82b9183
JP
1270 option->changed = false;
1271 }
86ebb02d
DM
1272 if (option->removed &&
1273 nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED))
1274 goto nla_put_failure;
3d249d4c
JP
1275 switch (option->type) {
1276 case TEAM_OPTION_TYPE_U32:
86ebb02d
DM
1277 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32))
1278 goto nla_put_failure;
3d249d4c 1279 team_option_get(team, option, &arg);
86ebb02d
DM
1280 if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA, arg))
1281 goto nla_put_failure;
3d249d4c
JP
1282 break;
1283 case TEAM_OPTION_TYPE_STRING:
86ebb02d
DM
1284 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING))
1285 goto nla_put_failure;
3d249d4c 1286 team_option_get(team, option, &arg);
86ebb02d
DM
1287 if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA,
1288 (char *) arg))
1289 goto nla_put_failure;
3d249d4c 1290 break;
2615598f
JP
1291 case TEAM_OPTION_TYPE_BINARY:
1292 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY))
1293 goto nla_put_failure;
1294 arg = (long) &tbinary;
1295 team_option_get(team, option, &arg);
1296 if (nla_put(skb, TEAM_ATTR_OPTION_DATA,
1297 tbinary.data_len, tbinary.data))
1298 goto nla_put_failure;
1299 break;
3d249d4c
JP
1300 default:
1301 BUG();
1302 }
1303 nla_nest_end(skb, option_item);
1304 }
1305
1306 nla_nest_end(skb, option_list);
1307 return genlmsg_end(skb, hdr);
1308
1309nla_put_failure:
1310 genlmsg_cancel(skb, hdr);
1311 return -EMSGSIZE;
1312}
1313
b82b9183
JP
1314static int team_nl_fill_options_get_all(struct sk_buff *skb,
1315 struct genl_info *info, int flags,
1316 struct team *team)
3d249d4c 1317{
b82b9183
JP
1318 return team_nl_fill_options_get(skb, info->snd_pid,
1319 info->snd_seq, NLM_F_ACK,
1320 team, true);
3d249d4c
JP
1321}
1322
1323static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
1324{
1325 struct team *team;
1326 int err;
1327
1328 team = team_nl_team_get(info);
1329 if (!team)
1330 return -EINVAL;
1331
b82b9183 1332 err = team_nl_send_generic(info, team, team_nl_fill_options_get_all);
3d249d4c
JP
1333
1334 team_nl_team_put(team);
1335
1336 return err;
1337}
1338
1339static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
1340{
1341 struct team *team;
1342 int err = 0;
1343 int i;
1344 struct nlattr *nl_option;
1345
1346 team = team_nl_team_get(info);
1347 if (!team)
1348 return -EINVAL;
1349
1350 err = -EINVAL;
1351 if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
1352 err = -EINVAL;
1353 goto team_put;
1354 }
1355
1356 nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
1357 struct nlattr *mode_attrs[TEAM_ATTR_OPTION_MAX + 1];
1358 enum team_option_type opt_type;
1359 struct team_option *option;
1360 char *opt_name;
1361 bool opt_found = false;
1362
1363 if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
1364 err = -EINVAL;
1365 goto team_put;
1366 }
1367 err = nla_parse_nested(mode_attrs, TEAM_ATTR_OPTION_MAX,
1368 nl_option, team_nl_option_policy);
1369 if (err)
1370 goto team_put;
1371 if (!mode_attrs[TEAM_ATTR_OPTION_NAME] ||
1372 !mode_attrs[TEAM_ATTR_OPTION_TYPE] ||
1373 !mode_attrs[TEAM_ATTR_OPTION_DATA]) {
1374 err = -EINVAL;
1375 goto team_put;
1376 }
1377 switch (nla_get_u8(mode_attrs[TEAM_ATTR_OPTION_TYPE])) {
1378 case NLA_U32:
1379 opt_type = TEAM_OPTION_TYPE_U32;
1380 break;
1381 case NLA_STRING:
1382 opt_type = TEAM_OPTION_TYPE_STRING;
1383 break;
2615598f
JP
1384 case NLA_BINARY:
1385 opt_type = TEAM_OPTION_TYPE_BINARY;
1386 break;
3d249d4c
JP
1387 default:
1388 goto team_put;
1389 }
1390
1391 opt_name = nla_data(mode_attrs[TEAM_ATTR_OPTION_NAME]);
1392 list_for_each_entry(option, &team->option_list, list) {
1393 long arg;
1394 struct nlattr *opt_data_attr;
2615598f
JP
1395 struct team_option_binary tbinary;
1396 int data_len;
3d249d4c
JP
1397
1398 if (option->type != opt_type ||
1399 strcmp(option->name, opt_name))
1400 continue;
1401 opt_found = true;
1402 opt_data_attr = mode_attrs[TEAM_ATTR_OPTION_DATA];
2615598f 1403 data_len = nla_len(opt_data_attr);
3d249d4c
JP
1404 switch (opt_type) {
1405 case TEAM_OPTION_TYPE_U32:
1406 arg = nla_get_u32(opt_data_attr);
1407 break;
1408 case TEAM_OPTION_TYPE_STRING:
2615598f
JP
1409 if (data_len > TEAM_STRING_MAX_LEN) {
1410 err = -EINVAL;
1411 goto team_put;
1412 }
3d249d4c
JP
1413 arg = (long) nla_data(opt_data_attr);
1414 break;
2615598f
JP
1415 case TEAM_OPTION_TYPE_BINARY:
1416 tbinary.data_len = data_len;
1417 tbinary.data = nla_data(opt_data_attr);
1418 arg = (long) &tbinary;
1419 break;
3d249d4c
JP
1420 default:
1421 BUG();
1422 }
1423 err = team_option_set(team, option, &arg);
1424 if (err)
1425 goto team_put;
1426 }
1427 if (!opt_found) {
1428 err = -ENOENT;
1429 goto team_put;
1430 }
1431 }
1432
1433team_put:
1434 team_nl_team_put(team);
1435
1436 return err;
1437}
1438
b82b9183
JP
1439static int team_nl_fill_port_list_get(struct sk_buff *skb,
1440 u32 pid, u32 seq, int flags,
1441 struct team *team,
1442 bool fillall)
3d249d4c
JP
1443{
1444 struct nlattr *port_list;
1445 void *hdr;
1446 struct team_port *port;
1447
1448 hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags,
1449 TEAM_CMD_PORT_LIST_GET);
1450 if (IS_ERR(hdr))
1451 return PTR_ERR(hdr);
1452
86ebb02d
DM
1453 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
1454 goto nla_put_failure;
3d249d4c
JP
1455 port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
1456 if (!port_list)
1457 return -EMSGSIZE;
1458
1459 list_for_each_entry(port, &team->port_list, list) {
1460 struct nlattr *port_item;
1461
b82b9183
JP
1462 /* Include only changed ports if fill all mode is not on */
1463 if (!fillall && !port->changed)
1464 continue;
3d249d4c
JP
1465 port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
1466 if (!port_item)
1467 goto nla_put_failure;
86ebb02d
DM
1468 if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex))
1469 goto nla_put_failure;
b82b9183 1470 if (port->changed) {
86ebb02d
DM
1471 if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED))
1472 goto nla_put_failure;
b82b9183
JP
1473 port->changed = false;
1474 }
86ebb02d
DM
1475 if ((port->removed &&
1476 nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) ||
1477 (port->linkup &&
1478 nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) ||
1479 nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->speed) ||
1480 nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->duplex))
1481 goto nla_put_failure;
3d249d4c
JP
1482 nla_nest_end(skb, port_item);
1483 }
1484
1485 nla_nest_end(skb, port_list);
1486 return genlmsg_end(skb, hdr);
1487
1488nla_put_failure:
1489 genlmsg_cancel(skb, hdr);
1490 return -EMSGSIZE;
1491}
1492
b82b9183
JP
1493static int team_nl_fill_port_list_get_all(struct sk_buff *skb,
1494 struct genl_info *info, int flags,
1495 struct team *team)
3d249d4c 1496{
b82b9183
JP
1497 return team_nl_fill_port_list_get(skb, info->snd_pid,
1498 info->snd_seq, NLM_F_ACK,
1499 team, true);
3d249d4c
JP
1500}
1501
1502static int team_nl_cmd_port_list_get(struct sk_buff *skb,
1503 struct genl_info *info)
1504{
1505 struct team *team;
1506 int err;
1507
1508 team = team_nl_team_get(info);
1509 if (!team)
1510 return -EINVAL;
1511
b82b9183 1512 err = team_nl_send_generic(info, team, team_nl_fill_port_list_get_all);
3d249d4c
JP
1513
1514 team_nl_team_put(team);
1515
1516 return err;
1517}
1518
1519static struct genl_ops team_nl_ops[] = {
1520 {
1521 .cmd = TEAM_CMD_NOOP,
1522 .doit = team_nl_cmd_noop,
1523 .policy = team_nl_policy,
1524 },
1525 {
1526 .cmd = TEAM_CMD_OPTIONS_SET,
1527 .doit = team_nl_cmd_options_set,
1528 .policy = team_nl_policy,
1529 .flags = GENL_ADMIN_PERM,
1530 },
1531 {
1532 .cmd = TEAM_CMD_OPTIONS_GET,
1533 .doit = team_nl_cmd_options_get,
1534 .policy = team_nl_policy,
1535 .flags = GENL_ADMIN_PERM,
1536 },
1537 {
1538 .cmd = TEAM_CMD_PORT_LIST_GET,
1539 .doit = team_nl_cmd_port_list_get,
1540 .policy = team_nl_policy,
1541 .flags = GENL_ADMIN_PERM,
1542 },
1543};
1544
1545static struct genl_multicast_group team_change_event_mcgrp = {
1546 .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME,
1547};
1548
b82b9183 1549static int team_nl_send_event_options_get(struct team *team)
3d249d4c
JP
1550{
1551 struct sk_buff *skb;
1552 int err;
1553 struct net *net = dev_net(team->dev);
1554
1555 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1556 if (!skb)
1557 return -ENOMEM;
1558
b82b9183 1559 err = team_nl_fill_options_get(skb, 0, 0, 0, team, false);
3d249d4c
JP
1560 if (err < 0)
1561 goto err_fill;
1562
1563 err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id,
1564 GFP_KERNEL);
1565 return err;
1566
1567err_fill:
1568 nlmsg_free(skb);
1569 return err;
1570}
1571
b82b9183 1572static int team_nl_send_event_port_list_get(struct team *team)
3d249d4c
JP
1573{
1574 struct sk_buff *skb;
1575 int err;
b82b9183 1576 struct net *net = dev_net(team->dev);
3d249d4c
JP
1577
1578 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1579 if (!skb)
1580 return -ENOMEM;
1581
b82b9183 1582 err = team_nl_fill_port_list_get(skb, 0, 0, 0, team, false);
3d249d4c
JP
1583 if (err < 0)
1584 goto err_fill;
1585
1586 err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id,
1587 GFP_KERNEL);
1588 return err;
1589
1590err_fill:
1591 nlmsg_free(skb);
1592 return err;
1593}
1594
1595static int team_nl_init(void)
1596{
1597 int err;
1598
1599 err = genl_register_family_with_ops(&team_nl_family, team_nl_ops,
1600 ARRAY_SIZE(team_nl_ops));
1601 if (err)
1602 return err;
1603
1604 err = genl_register_mc_group(&team_nl_family, &team_change_event_mcgrp);
1605 if (err)
1606 goto err_change_event_grp_reg;
1607
1608 return 0;
1609
1610err_change_event_grp_reg:
1611 genl_unregister_family(&team_nl_family);
1612
1613 return err;
1614}
1615
1616static void team_nl_fini(void)
1617{
1618 genl_unregister_family(&team_nl_family);
1619}
1620
1621
1622/******************
1623 * Change checkers
1624 ******************/
1625
b82b9183 1626static void __team_options_change_check(struct team *team)
3d249d4c
JP
1627{
1628 int err;
1629
b82b9183 1630 err = team_nl_send_event_options_get(team);
3d249d4c
JP
1631 if (err)
1632 netdev_warn(team->dev, "Failed to send options change via netlink\n");
1633}
1634
1635/* rtnl lock is held */
1636static void __team_port_change_check(struct team_port *port, bool linkup)
1637{
1638 int err;
1639
b82b9183 1640 if (!port->removed && port->linkup == linkup)
3d249d4c
JP
1641 return;
1642
b82b9183 1643 port->changed = true;
3d249d4c
JP
1644 port->linkup = linkup;
1645 if (linkup) {
1646 struct ethtool_cmd ecmd;
1647
1648 err = __ethtool_get_settings(port->dev, &ecmd);
1649 if (!err) {
1650 port->speed = ethtool_cmd_speed(&ecmd);
1651 port->duplex = ecmd.duplex;
1652 goto send_event;
1653 }
1654 }
1655 port->speed = 0;
1656 port->duplex = 0;
1657
1658send_event:
b82b9183 1659 err = team_nl_send_event_port_list_get(port->team);
3d249d4c
JP
1660 if (err)
1661 netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink\n",
1662 port->dev->name);
1663
1664}
1665
1666static void team_port_change_check(struct team_port *port, bool linkup)
1667{
1668 struct team *team = port->team;
1669
61dc3461 1670 mutex_lock(&team->lock);
3d249d4c 1671 __team_port_change_check(port, linkup);
61dc3461 1672 mutex_unlock(&team->lock);
3d249d4c
JP
1673}
1674
1675/************************************
1676 * Net device notifier event handler
1677 ************************************/
1678
1679static int team_device_event(struct notifier_block *unused,
1680 unsigned long event, void *ptr)
1681{
1682 struct net_device *dev = (struct net_device *) ptr;
1683 struct team_port *port;
1684
1685 port = team_port_get_rtnl(dev);
1686 if (!port)
1687 return NOTIFY_DONE;
1688
1689 switch (event) {
1690 case NETDEV_UP:
1691 if (netif_carrier_ok(dev))
1692 team_port_change_check(port, true);
1693 case NETDEV_DOWN:
1694 team_port_change_check(port, false);
1695 case NETDEV_CHANGE:
1696 if (netif_running(port->dev))
1697 team_port_change_check(port,
1698 !!netif_carrier_ok(port->dev));
1699 break;
1700 case NETDEV_UNREGISTER:
1701 team_del_slave(port->team->dev, dev);
1702 break;
1703 case NETDEV_FEAT_CHANGE:
1704 team_compute_features(port->team);
1705 break;
1706 case NETDEV_CHANGEMTU:
1707 /* Forbid to change mtu of underlaying device */
1708 return NOTIFY_BAD;
1709 case NETDEV_PRE_TYPE_CHANGE:
1710 /* Forbid to change type of underlaying device */
1711 return NOTIFY_BAD;
1712 }
1713 return NOTIFY_DONE;
1714}
1715
1716static struct notifier_block team_notifier_block __read_mostly = {
1717 .notifier_call = team_device_event,
1718};
1719
1720
1721/***********************
1722 * Module init and exit
1723 ***********************/
1724
1725static int __init team_module_init(void)
1726{
1727 int err;
1728
1729 register_netdevice_notifier(&team_notifier_block);
1730
1731 err = rtnl_link_register(&team_link_ops);
1732 if (err)
1733 goto err_rtnl_reg;
1734
1735 err = team_nl_init();
1736 if (err)
1737 goto err_nl_init;
1738
1739 return 0;
1740
1741err_nl_init:
1742 rtnl_link_unregister(&team_link_ops);
1743
1744err_rtnl_reg:
1745 unregister_netdevice_notifier(&team_notifier_block);
1746
1747 return err;
1748}
1749
1750static void __exit team_module_exit(void)
1751{
1752 team_nl_fini();
1753 rtnl_link_unregister(&team_link_ops);
1754 unregister_netdevice_notifier(&team_notifier_block);
1755}
1756
1757module_init(team_module_init);
1758module_exit(team_module_exit);
1759
1760MODULE_LICENSE("GPL v2");
1761MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
1762MODULE_DESCRIPTION("Ethernet team device driver");
1763MODULE_ALIAS_RTNL_LINK(DRV_NAME);