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