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