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