team: use function team_port_txable() for determing enabled and up port
[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
52a4fd77 674bool team_port_enabled(struct team_port *port)
19a0b58e
JP
675{
676 return port->index != -1;
677}
52a4fd77 678EXPORT_SYMBOL(team_port_enabled);
19a0b58e 679
6e88e135
JP
680bool team_port_txable(struct team_port *port)
681{
682 return port->linkup && team_port_enabled(port);
683}
684EXPORT_SYMBOL(team_port_txable);
685
3d249d4c 686/*
19a0b58e
JP
687 * Enable/disable port by adding to enabled port hashlist and setting
688 * port->index (Might be racy so reader could see incorrect ifindex when
689 * processing a flying packet, but that is not a problem). Write guarded
690 * by team->lock.
3d249d4c 691 */
19a0b58e
JP
692static void team_port_enable(struct team *team,
693 struct team_port *port)
3d249d4c 694{
19a0b58e
JP
695 if (team_port_enabled(port))
696 return;
697 port->index = team->en_port_count++;
3d249d4c
JP
698 hlist_add_head_rcu(&port->hlist,
699 team_port_index_hash(team, port->index));
122bb046 700 team_adjust_ops(team);
4bccfd17
JP
701 if (team->ops.port_enabled)
702 team->ops.port_enabled(team, port);
3d249d4c
JP
703}
704
705static void __reconstruct_port_hlist(struct team *team, int rm_index)
706{
707 int i;
708 struct team_port *port;
709
19a0b58e 710 for (i = rm_index + 1; i < team->en_port_count; i++) {
3d249d4c
JP
711 port = team_get_port_by_index(team, i);
712 hlist_del_rcu(&port->hlist);
713 port->index--;
714 hlist_add_head_rcu(&port->hlist,
715 team_port_index_hash(team, port->index));
716 }
717}
718
19a0b58e
JP
719static void team_port_disable(struct team *team,
720 struct team_port *port)
3d249d4c 721{
19a0b58e
JP
722 if (!team_port_enabled(port))
723 return;
4bccfd17
JP
724 if (team->ops.port_disabled)
725 team->ops.port_disabled(team, port);
3d249d4c 726 hlist_del_rcu(&port->hlist);
122bb046 727 __reconstruct_port_hlist(team, port->index);
19a0b58e 728 port->index = -1;
122bb046
JP
729 __team_adjust_ops(team, team->en_port_count - 1);
730 /*
731 * Wait until readers see adjusted ops. This ensures that
732 * readers never see team->en_port_count == 0
733 */
734 synchronize_rcu();
735 team->en_port_count--;
3d249d4c
JP
736}
737
738#define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \
739 NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
740 NETIF_F_HIGHDMA | NETIF_F_LRO)
741
742static void __team_compute_features(struct team *team)
743{
744 struct team_port *port;
745 u32 vlan_features = TEAM_VLAN_FEATURES;
746 unsigned short max_hard_header_len = ETH_HLEN;
747
748 list_for_each_entry(port, &team->port_list, list) {
749 vlan_features = netdev_increment_features(vlan_features,
750 port->dev->vlan_features,
751 TEAM_VLAN_FEATURES);
752
753 if (port->dev->hard_header_len > max_hard_header_len)
754 max_hard_header_len = port->dev->hard_header_len;
755 }
756
757 team->dev->vlan_features = vlan_features;
758 team->dev->hard_header_len = max_hard_header_len;
759
760 netdev_change_features(team->dev);
761}
762
763static void team_compute_features(struct team *team)
764{
61dc3461 765 mutex_lock(&team->lock);
3d249d4c 766 __team_compute_features(team);
61dc3461 767 mutex_unlock(&team->lock);
3d249d4c
JP
768}
769
770static int team_port_enter(struct team *team, struct team_port *port)
771{
772 int err = 0;
773
774 dev_hold(team->dev);
775 port->dev->priv_flags |= IFF_TEAM_PORT;
776 if (team->ops.port_enter) {
777 err = team->ops.port_enter(team, port);
778 if (err) {
779 netdev_err(team->dev, "Device %s failed to enter team mode\n",
780 port->dev->name);
781 goto err_port_enter;
782 }
783 }
784
785 return 0;
786
787err_port_enter:
788 port->dev->priv_flags &= ~IFF_TEAM_PORT;
789 dev_put(team->dev);
790
791 return err;
792}
793
794static void team_port_leave(struct team *team, struct team_port *port)
795{
796 if (team->ops.port_leave)
797 team->ops.port_leave(team, port);
798 port->dev->priv_flags &= ~IFF_TEAM_PORT;
799 dev_put(team->dev);
800}
801
802static void __team_port_change_check(struct team_port *port, bool linkup);
803
804static int team_port_add(struct team *team, struct net_device *port_dev)
805{
806 struct net_device *dev = team->dev;
807 struct team_port *port;
808 char *portname = port_dev->name;
809 int err;
810
811 if (port_dev->flags & IFF_LOOPBACK ||
812 port_dev->type != ARPHRD_ETHER) {
813 netdev_err(dev, "Device %s is of an unsupported type\n",
814 portname);
815 return -EINVAL;
816 }
817
818 if (team_port_exists(port_dev)) {
819 netdev_err(dev, "Device %s is already a port "
820 "of a team device\n", portname);
821 return -EBUSY;
822 }
823
824 if (port_dev->flags & IFF_UP) {
825 netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
826 portname);
827 return -EBUSY;
828 }
829
5149ee58
JP
830 port = kzalloc(sizeof(struct team_port) + team->mode->port_priv_size,
831 GFP_KERNEL);
3d249d4c
JP
832 if (!port)
833 return -ENOMEM;
834
835 port->dev = port_dev;
836 port->team = team;
837
838 port->orig.mtu = port_dev->mtu;
839 err = dev_set_mtu(port_dev, dev->mtu);
840 if (err) {
841 netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
842 goto err_set_mtu;
843 }
844
845 memcpy(port->orig.dev_addr, port_dev->dev_addr, ETH_ALEN);
846
847 err = team_port_enter(team, port);
848 if (err) {
849 netdev_err(dev, "Device %s failed to enter team mode\n",
850 portname);
851 goto err_port_enter;
852 }
853
854 err = dev_open(port_dev);
855 if (err) {
856 netdev_dbg(dev, "Device %s opening failed\n",
857 portname);
858 goto err_dev_open;
859 }
860
57459185
JP
861 err = vlan_vids_add_by_dev(port_dev, dev);
862 if (err) {
863 netdev_err(dev, "Failed to add vlan ids to device %s\n",
864 portname);
865 goto err_vids_add;
866 }
867
3d249d4c
JP
868 err = netdev_set_master(port_dev, dev);
869 if (err) {
870 netdev_err(dev, "Device %s failed to set master\n", portname);
871 goto err_set_master;
872 }
873
874 err = netdev_rx_handler_register(port_dev, team_handle_frame,
875 port);
876 if (err) {
877 netdev_err(dev, "Device %s failed to register rx_handler\n",
878 portname);
879 goto err_handler_register;
880 }
881
35b384bd 882 err = __team_option_inst_add_port(team, port);
80f7c668
JP
883 if (err) {
884 netdev_err(dev, "Device %s failed to add per-port options\n",
885 portname);
886 goto err_option_port_add;
887 }
888
19a0b58e
JP
889 port->index = -1;
890 team_port_enable(team, port);
891 list_add_tail_rcu(&port->list, &team->port_list);
3d249d4c
JP
892 __team_compute_features(team);
893 __team_port_change_check(port, !!netif_carrier_ok(port_dev));
35b384bd 894 __team_options_change_check(team);
3d249d4c
JP
895
896 netdev_info(dev, "Port device %s added\n", portname);
897
898 return 0;
899
80f7c668
JP
900err_option_port_add:
901 netdev_rx_handler_unregister(port_dev);
902
3d249d4c
JP
903err_handler_register:
904 netdev_set_master(port_dev, NULL);
905
906err_set_master:
57459185
JP
907 vlan_vids_del_by_dev(port_dev, dev);
908
909err_vids_add:
3d249d4c
JP
910 dev_close(port_dev);
911
912err_dev_open:
913 team_port_leave(team, port);
914 team_port_set_orig_mac(port);
915
916err_port_enter:
917 dev_set_mtu(port_dev, port->orig.mtu);
918
919err_set_mtu:
920 kfree(port);
921
922 return err;
923}
924
925static int team_port_del(struct team *team, struct net_device *port_dev)
926{
927 struct net_device *dev = team->dev;
928 struct team_port *port;
929 char *portname = port_dev->name;
930
931 port = team_port_get_rtnl(port_dev);
932 if (!port || !team_port_find(team, port)) {
933 netdev_err(dev, "Device %s does not act as a port of this team\n",
934 portname);
935 return -ENOENT;
936 }
937
35b384bd
JP
938 __team_option_inst_mark_removed_port(team, port);
939 __team_options_change_check(team);
940 __team_option_inst_del_port(team, port);
b82b9183 941 port->removed = true;
3d249d4c 942 __team_port_change_check(port, false);
19a0b58e
JP
943 team_port_disable(team, port);
944 list_del_rcu(&port->list);
3d249d4c
JP
945 netdev_rx_handler_unregister(port_dev);
946 netdev_set_master(port_dev, NULL);
57459185 947 vlan_vids_del_by_dev(port_dev, dev);
3d249d4c
JP
948 dev_close(port_dev);
949 team_port_leave(team, port);
950 team_port_set_orig_mac(port);
951 dev_set_mtu(port_dev, port->orig.mtu);
952 synchronize_rcu();
953 kfree(port);
954 netdev_info(dev, "Port device %s removed\n", portname);
955 __team_compute_features(team);
956
957 return 0;
958}
959
960
961/*****************
962 * Net device ops
963 *****************/
964
80f7c668 965static int team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx)
3d249d4c 966{
d299cd51 967 ctx->data.str_val = team->mode->kind;
3d249d4c
JP
968 return 0;
969}
970
80f7c668 971static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx)
3d249d4c 972{
80f7c668 973 return team_change_mode(team, ctx->data.str_val);
3d249d4c
JP
974}
975
acd69962
JP
976static int team_port_en_option_get(struct team *team,
977 struct team_gsetter_ctx *ctx)
978{
85d59a87
JP
979 struct team_port *port = ctx->info->port;
980
981 ctx->data.bool_val = team_port_enabled(port);
acd69962
JP
982 return 0;
983}
984
985static int team_port_en_option_set(struct team *team,
986 struct team_gsetter_ctx *ctx)
987{
85d59a87
JP
988 struct team_port *port = ctx->info->port;
989
acd69962 990 if (ctx->data.bool_val)
85d59a87 991 team_port_enable(team, port);
acd69962 992 else
85d59a87 993 team_port_disable(team, port);
acd69962
JP
994 return 0;
995}
996
71472ec1
JP
997static int team_user_linkup_option_get(struct team *team,
998 struct team_gsetter_ctx *ctx)
999{
85d59a87
JP
1000 struct team_port *port = ctx->info->port;
1001
1002 ctx->data.bool_val = port->user.linkup;
71472ec1
JP
1003 return 0;
1004}
1005
1006static int team_user_linkup_option_set(struct team *team,
1007 struct team_gsetter_ctx *ctx)
1008{
85d59a87
JP
1009 struct team_port *port = ctx->info->port;
1010
1011 port->user.linkup = ctx->data.bool_val;
1012 team_refresh_port_linkup(port);
71472ec1
JP
1013 return 0;
1014}
1015
1016static int team_user_linkup_en_option_get(struct team *team,
1017 struct team_gsetter_ctx *ctx)
1018{
85d59a87 1019 struct team_port *port = ctx->info->port;
71472ec1
JP
1020
1021 ctx->data.bool_val = port->user.linkup_enabled;
1022 return 0;
1023}
1024
1025static int team_user_linkup_en_option_set(struct team *team,
1026 struct team_gsetter_ctx *ctx)
1027{
85d59a87 1028 struct team_port *port = ctx->info->port;
71472ec1
JP
1029
1030 port->user.linkup_enabled = ctx->data.bool_val;
85d59a87 1031 team_refresh_port_linkup(port);
71472ec1
JP
1032 return 0;
1033}
1034
358b8382 1035static const struct team_option team_options[] = {
3d249d4c
JP
1036 {
1037 .name = "mode",
1038 .type = TEAM_OPTION_TYPE_STRING,
1039 .getter = team_mode_option_get,
1040 .setter = team_mode_option_set,
1041 },
acd69962
JP
1042 {
1043 .name = "enabled",
1044 .type = TEAM_OPTION_TYPE_BOOL,
1045 .per_port = true,
1046 .getter = team_port_en_option_get,
1047 .setter = team_port_en_option_set,
1048 },
71472ec1
JP
1049 {
1050 .name = "user_linkup",
1051 .type = TEAM_OPTION_TYPE_BOOL,
1052 .per_port = true,
1053 .getter = team_user_linkup_option_get,
1054 .setter = team_user_linkup_option_set,
1055 },
1056 {
1057 .name = "user_linkup_enabled",
1058 .type = TEAM_OPTION_TYPE_BOOL,
1059 .per_port = true,
1060 .getter = team_user_linkup_en_option_get,
1061 .setter = team_user_linkup_en_option_set,
1062 },
3d249d4c
JP
1063};
1064
1065static int team_init(struct net_device *dev)
1066{
1067 struct team *team = netdev_priv(dev);
1068 int i;
358b8382 1069 int err;
3d249d4c
JP
1070
1071 team->dev = dev;
61dc3461 1072 mutex_init(&team->lock);
d299cd51 1073 team_set_no_mode(team);
3d249d4c
JP
1074
1075 team->pcpu_stats = alloc_percpu(struct team_pcpu_stats);
1076 if (!team->pcpu_stats)
1077 return -ENOMEM;
1078
1079 for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
19a0b58e 1080 INIT_HLIST_HEAD(&team->en_port_hlist[i]);
3d249d4c
JP
1081 INIT_LIST_HEAD(&team->port_list);
1082
1083 team_adjust_ops(team);
1084
1085 INIT_LIST_HEAD(&team->option_list);
80f7c668 1086 INIT_LIST_HEAD(&team->option_inst_list);
358b8382
JP
1087 err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
1088 if (err)
1089 goto err_options_register;
3d249d4c
JP
1090 netif_carrier_off(dev);
1091
1092 return 0;
358b8382
JP
1093
1094err_options_register:
1095 free_percpu(team->pcpu_stats);
1096
1097 return err;
3d249d4c
JP
1098}
1099
1100static void team_uninit(struct net_device *dev)
1101{
1102 struct team *team = netdev_priv(dev);
1103 struct team_port *port;
1104 struct team_port *tmp;
1105
61dc3461 1106 mutex_lock(&team->lock);
3d249d4c
JP
1107 list_for_each_entry_safe(port, tmp, &team->port_list, list)
1108 team_port_del(team, port->dev);
1109
1110 __team_change_mode(team, NULL); /* cleanup */
1111 __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
61dc3461 1112 mutex_unlock(&team->lock);
3d249d4c
JP
1113}
1114
1115static void team_destructor(struct net_device *dev)
1116{
1117 struct team *team = netdev_priv(dev);
1118
1119 free_percpu(team->pcpu_stats);
1120 free_netdev(dev);
1121}
1122
1123static int team_open(struct net_device *dev)
1124{
1125 netif_carrier_on(dev);
1126 return 0;
1127}
1128
1129static int team_close(struct net_device *dev)
1130{
1131 netif_carrier_off(dev);
1132 return 0;
1133}
1134
1135/*
1136 * note: already called with rcu_read_lock
1137 */
1138static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
1139{
1140 struct team *team = netdev_priv(dev);
1141 bool tx_success = false;
1142 unsigned int len = skb->len;
1143
1144 tx_success = team->ops.transmit(team, skb);
1145 if (tx_success) {
1146 struct team_pcpu_stats *pcpu_stats;
1147
1148 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
1149 u64_stats_update_begin(&pcpu_stats->syncp);
1150 pcpu_stats->tx_packets++;
1151 pcpu_stats->tx_bytes += len;
1152 u64_stats_update_end(&pcpu_stats->syncp);
1153 } else {
1154 this_cpu_inc(team->pcpu_stats->tx_dropped);
1155 }
1156
1157 return NETDEV_TX_OK;
1158}
1159
1160static void team_change_rx_flags(struct net_device *dev, int change)
1161{
1162 struct team *team = netdev_priv(dev);
1163 struct team_port *port;
1164 int inc;
1165
1166 rcu_read_lock();
1167 list_for_each_entry_rcu(port, &team->port_list, list) {
1168 if (change & IFF_PROMISC) {
1169 inc = dev->flags & IFF_PROMISC ? 1 : -1;
1170 dev_set_promiscuity(port->dev, inc);
1171 }
1172 if (change & IFF_ALLMULTI) {
1173 inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
1174 dev_set_allmulti(port->dev, inc);
1175 }
1176 }
1177 rcu_read_unlock();
1178}
1179
1180static void team_set_rx_mode(struct net_device *dev)
1181{
1182 struct team *team = netdev_priv(dev);
1183 struct team_port *port;
1184
1185 rcu_read_lock();
1186 list_for_each_entry_rcu(port, &team->port_list, list) {
1187 dev_uc_sync(port->dev, dev);
1188 dev_mc_sync(port->dev, dev);
1189 }
1190 rcu_read_unlock();
1191}
1192
1193static int team_set_mac_address(struct net_device *dev, void *p)
1194{
1195 struct team *team = netdev_priv(dev);
1196 struct team_port *port;
5a1d1c8c 1197 int err;
3d249d4c 1198
5a1d1c8c
JP
1199 err = eth_mac_addr(dev, p);
1200 if (err)
1201 return err;
3d249d4c
JP
1202 rcu_read_lock();
1203 list_for_each_entry_rcu(port, &team->port_list, list)
1204 if (team->ops.port_change_mac)
1205 team->ops.port_change_mac(team, port);
1206 rcu_read_unlock();
1207 return 0;
1208}
1209
1210static int team_change_mtu(struct net_device *dev, int new_mtu)
1211{
1212 struct team *team = netdev_priv(dev);
1213 struct team_port *port;
1214 int err;
1215
1216 /*
1217 * Alhough this is reader, it's guarded by team lock. It's not possible
1218 * to traverse list in reverse under rcu_read_lock
1219 */
61dc3461 1220 mutex_lock(&team->lock);
3d249d4c
JP
1221 list_for_each_entry(port, &team->port_list, list) {
1222 err = dev_set_mtu(port->dev, new_mtu);
1223 if (err) {
1224 netdev_err(dev, "Device %s failed to change mtu",
1225 port->dev->name);
1226 goto unwind;
1227 }
1228 }
61dc3461 1229 mutex_unlock(&team->lock);
3d249d4c
JP
1230
1231 dev->mtu = new_mtu;
1232
1233 return 0;
1234
1235unwind:
1236 list_for_each_entry_continue_reverse(port, &team->port_list, list)
1237 dev_set_mtu(port->dev, dev->mtu);
61dc3461 1238 mutex_unlock(&team->lock);
3d249d4c
JP
1239
1240 return err;
1241}
1242
1243static struct rtnl_link_stats64 *
1244team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1245{
1246 struct team *team = netdev_priv(dev);
1247 struct team_pcpu_stats *p;
1248 u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
1249 u32 rx_dropped = 0, tx_dropped = 0;
1250 unsigned int start;
1251 int i;
1252
1253 for_each_possible_cpu(i) {
1254 p = per_cpu_ptr(team->pcpu_stats, i);
1255 do {
1256 start = u64_stats_fetch_begin_bh(&p->syncp);
1257 rx_packets = p->rx_packets;
1258 rx_bytes = p->rx_bytes;
1259 rx_multicast = p->rx_multicast;
1260 tx_packets = p->tx_packets;
1261 tx_bytes = p->tx_bytes;
1262 } while (u64_stats_fetch_retry_bh(&p->syncp, start));
1263
1264 stats->rx_packets += rx_packets;
1265 stats->rx_bytes += rx_bytes;
1266 stats->multicast += rx_multicast;
1267 stats->tx_packets += tx_packets;
1268 stats->tx_bytes += tx_bytes;
1269 /*
1270 * rx_dropped & tx_dropped are u32, updated
1271 * without syncp protection.
1272 */
1273 rx_dropped += p->rx_dropped;
1274 tx_dropped += p->tx_dropped;
1275 }
1276 stats->rx_dropped = rx_dropped;
1277 stats->tx_dropped = tx_dropped;
1278 return stats;
1279}
1280
8e586137 1281static int team_vlan_rx_add_vid(struct net_device *dev, uint16_t vid)
3d249d4c
JP
1282{
1283 struct team *team = netdev_priv(dev);
1284 struct team_port *port;
87002b03 1285 int err;
3d249d4c 1286
87002b03
JP
1287 /*
1288 * Alhough this is reader, it's guarded by team lock. It's not possible
1289 * to traverse list in reverse under rcu_read_lock
1290 */
1291 mutex_lock(&team->lock);
1292 list_for_each_entry(port, &team->port_list, list) {
1293 err = vlan_vid_add(port->dev, vid);
1294 if (err)
1295 goto unwind;
3d249d4c 1296 }
87002b03 1297 mutex_unlock(&team->lock);
8e586137
JP
1298
1299 return 0;
87002b03
JP
1300
1301unwind:
1302 list_for_each_entry_continue_reverse(port, &team->port_list, list)
1303 vlan_vid_del(port->dev, vid);
1304 mutex_unlock(&team->lock);
1305
1306 return err;
3d249d4c
JP
1307}
1308
8e586137 1309static int team_vlan_rx_kill_vid(struct net_device *dev, uint16_t vid)
3d249d4c
JP
1310{
1311 struct team *team = netdev_priv(dev);
1312 struct team_port *port;
1313
1314 rcu_read_lock();
87002b03
JP
1315 list_for_each_entry_rcu(port, &team->port_list, list)
1316 vlan_vid_del(port->dev, vid);
3d249d4c 1317 rcu_read_unlock();
8e586137
JP
1318
1319 return 0;
3d249d4c
JP
1320}
1321
1322static int team_add_slave(struct net_device *dev, struct net_device *port_dev)
1323{
1324 struct team *team = netdev_priv(dev);
1325 int err;
1326
61dc3461 1327 mutex_lock(&team->lock);
3d249d4c 1328 err = team_port_add(team, port_dev);
61dc3461 1329 mutex_unlock(&team->lock);
3d249d4c
JP
1330 return err;
1331}
1332
1333static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
1334{
1335 struct team *team = netdev_priv(dev);
1336 int err;
1337
61dc3461 1338 mutex_lock(&team->lock);
3d249d4c 1339 err = team_port_del(team, port_dev);
61dc3461 1340 mutex_unlock(&team->lock);
3d249d4c
JP
1341 return err;
1342}
1343
234a8fd4
JP
1344static netdev_features_t team_fix_features(struct net_device *dev,
1345 netdev_features_t features)
1346{
1347 struct team_port *port;
1348 struct team *team = netdev_priv(dev);
1349 netdev_features_t mask;
1350
1351 mask = features;
1352 features &= ~NETIF_F_ONE_FOR_ALL;
1353 features |= NETIF_F_ALL_FOR_ALL;
1354
1355 rcu_read_lock();
1356 list_for_each_entry_rcu(port, &team->port_list, list) {
1357 features = netdev_increment_features(features,
1358 port->dev->features,
1359 mask);
1360 }
1361 rcu_read_unlock();
1362 return features;
1363}
1364
3d249d4c
JP
1365static const struct net_device_ops team_netdev_ops = {
1366 .ndo_init = team_init,
1367 .ndo_uninit = team_uninit,
1368 .ndo_open = team_open,
1369 .ndo_stop = team_close,
1370 .ndo_start_xmit = team_xmit,
1371 .ndo_change_rx_flags = team_change_rx_flags,
1372 .ndo_set_rx_mode = team_set_rx_mode,
1373 .ndo_set_mac_address = team_set_mac_address,
1374 .ndo_change_mtu = team_change_mtu,
1375 .ndo_get_stats64 = team_get_stats64,
1376 .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid,
1377 .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid,
1378 .ndo_add_slave = team_add_slave,
1379 .ndo_del_slave = team_del_slave,
234a8fd4 1380 .ndo_fix_features = team_fix_features,
3d249d4c
JP
1381};
1382
1383
1384/***********************
1385 * rt netlink interface
1386 ***********************/
1387
1388static void team_setup(struct net_device *dev)
1389{
1390 ether_setup(dev);
1391
1392 dev->netdev_ops = &team_netdev_ops;
1393 dev->destructor = team_destructor;
1394 dev->tx_queue_len = 0;
1395 dev->flags |= IFF_MULTICAST;
1396 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
1397
1398 /*
1399 * Indicate we support unicast address filtering. That way core won't
1400 * bring us to promisc mode in case a unicast addr is added.
1401 * Let this up to underlay drivers.
1402 */
5a1d1c8c 1403 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
3d249d4c
JP
1404
1405 dev->features |= NETIF_F_LLTX;
1406 dev->features |= NETIF_F_GRO;
1407 dev->hw_features = NETIF_F_HW_VLAN_TX |
1408 NETIF_F_HW_VLAN_RX |
1409 NETIF_F_HW_VLAN_FILTER;
1410
1411 dev->features |= dev->hw_features;
1412}
1413
1414static int team_newlink(struct net *src_net, struct net_device *dev,
1415 struct nlattr *tb[], struct nlattr *data[])
1416{
1417 int err;
1418
1419 if (tb[IFLA_ADDRESS] == NULL)
7ce5d222 1420 eth_hw_addr_random(dev);
3d249d4c
JP
1421
1422 err = register_netdevice(dev);
1423 if (err)
1424 return err;
1425
1426 return 0;
1427}
1428
1429static int team_validate(struct nlattr *tb[], struct nlattr *data[])
1430{
1431 if (tb[IFLA_ADDRESS]) {
1432 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1433 return -EINVAL;
1434 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1435 return -EADDRNOTAVAIL;
1436 }
1437 return 0;
1438}
1439
1440static struct rtnl_link_ops team_link_ops __read_mostly = {
1441 .kind = DRV_NAME,
1442 .priv_size = sizeof(struct team),
1443 .setup = team_setup,
1444 .newlink = team_newlink,
1445 .validate = team_validate,
1446};
1447
1448
1449/***********************************
1450 * Generic netlink custom interface
1451 ***********************************/
1452
1453static struct genl_family team_nl_family = {
1454 .id = GENL_ID_GENERATE,
1455 .name = TEAM_GENL_NAME,
1456 .version = TEAM_GENL_VERSION,
1457 .maxattr = TEAM_ATTR_MAX,
1458 .netnsok = true,
1459};
1460
1461static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
1462 [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, },
1463 [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 },
1464 [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED },
1465 [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED },
1466};
1467
1468static const struct nla_policy
1469team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
1470 [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
1471 [TEAM_ATTR_OPTION_NAME] = {
1472 .type = NLA_STRING,
1473 .len = TEAM_STRING_MAX_LEN,
1474 },
1475 [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG },
1476 [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 },
2615598f 1477 [TEAM_ATTR_OPTION_DATA] = { .type = NLA_BINARY },
3d249d4c
JP
1478};
1479
1480static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
1481{
1482 struct sk_buff *msg;
1483 void *hdr;
1484 int err;
1485
58050fce 1486 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3d249d4c
JP
1487 if (!msg)
1488 return -ENOMEM;
1489
1490 hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq,
1491 &team_nl_family, 0, TEAM_CMD_NOOP);
1492 if (IS_ERR(hdr)) {
1493 err = PTR_ERR(hdr);
1494 goto err_msg_put;
1495 }
1496
1497 genlmsg_end(msg, hdr);
1498
1499 return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid);
1500
1501err_msg_put:
1502 nlmsg_free(msg);
1503
1504 return err;
1505}
1506
1507/*
1508 * Netlink cmd functions should be locked by following two functions.
8c0713a5 1509 * Since dev gets held here, that ensures dev won't disappear in between.
3d249d4c
JP
1510 */
1511static struct team *team_nl_team_get(struct genl_info *info)
1512{
1513 struct net *net = genl_info_net(info);
1514 int ifindex;
1515 struct net_device *dev;
1516 struct team *team;
1517
1518 if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
1519 return NULL;
1520
1521 ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
8c0713a5 1522 dev = dev_get_by_index(net, ifindex);
3d249d4c 1523 if (!dev || dev->netdev_ops != &team_netdev_ops) {
8c0713a5
JP
1524 if (dev)
1525 dev_put(dev);
3d249d4c
JP
1526 return NULL;
1527 }
1528
1529 team = netdev_priv(dev);
61dc3461 1530 mutex_lock(&team->lock);
3d249d4c
JP
1531 return team;
1532}
1533
1534static void team_nl_team_put(struct team *team)
1535{
61dc3461 1536 mutex_unlock(&team->lock);
8c0713a5 1537 dev_put(team->dev);
3d249d4c
JP
1538}
1539
1540static int team_nl_send_generic(struct genl_info *info, struct team *team,
1541 int (*fill_func)(struct sk_buff *skb,
1542 struct genl_info *info,
1543 int flags, struct team *team))
1544{
1545 struct sk_buff *skb;
1546 int err;
1547
58050fce 1548 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3d249d4c
JP
1549 if (!skb)
1550 return -ENOMEM;
1551
1552 err = fill_func(skb, info, NLM_F_ACK, team);
1553 if (err < 0)
1554 goto err_fill;
1555
1556 err = genlmsg_unicast(genl_info_net(info), skb, info->snd_pid);
1557 return err;
1558
1559err_fill:
1560 nlmsg_free(skb);
1561 return err;
1562}
1563
9b00cf2d
JP
1564typedef int team_nl_send_func_t(struct sk_buff *skb,
1565 struct team *team, u32 pid);
1566
1567static int team_nl_send_unicast(struct sk_buff *skb, struct team *team, u32 pid)
1568{
1569 return genlmsg_unicast(dev_net(team->dev), skb, pid);
1570}
1571
01048d9a
JP
1572static int team_nl_fill_one_option_get(struct sk_buff *skb, struct team *team,
1573 struct team_option_inst *opt_inst)
1574{
1575 struct nlattr *option_item;
1576 struct team_option *option = opt_inst->option;
9b00cf2d 1577 struct team_option_inst_info *opt_inst_info = &opt_inst->info;
01048d9a
JP
1578 struct team_gsetter_ctx ctx;
1579 int err;
1580
9b00cf2d
JP
1581 ctx.info = opt_inst_info;
1582 err = team_option_get(team, opt_inst, &ctx);
1583 if (err)
1584 return err;
1585
01048d9a
JP
1586 option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
1587 if (!option_item)
9b00cf2d 1588 return -EMSGSIZE;
01048d9a 1589
9b00cf2d
JP
1590 if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name))
1591 goto nest_cancel;
01048d9a
JP
1592 if (opt_inst_info->port &&
1593 nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX,
1594 opt_inst_info->port->dev->ifindex))
9b00cf2d 1595 goto nest_cancel;
01048d9a
JP
1596 if (opt_inst->option->array_size &&
1597 nla_put_u32(skb, TEAM_ATTR_OPTION_ARRAY_INDEX,
1598 opt_inst_info->array_index))
9b00cf2d 1599 goto nest_cancel;
01048d9a
JP
1600
1601 switch (option->type) {
1602 case TEAM_OPTION_TYPE_U32:
1603 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32))
9b00cf2d 1604 goto nest_cancel;
01048d9a 1605 if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.u32_val))
9b00cf2d 1606 goto nest_cancel;
01048d9a
JP
1607 break;
1608 case TEAM_OPTION_TYPE_STRING:
1609 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING))
9b00cf2d 1610 goto nest_cancel;
01048d9a
JP
1611 if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA,
1612 ctx.data.str_val))
9b00cf2d 1613 goto nest_cancel;
01048d9a
JP
1614 break;
1615 case TEAM_OPTION_TYPE_BINARY:
1616 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY))
9b00cf2d 1617 goto nest_cancel;
01048d9a
JP
1618 if (nla_put(skb, TEAM_ATTR_OPTION_DATA, ctx.data.bin_val.len,
1619 ctx.data.bin_val.ptr))
9b00cf2d 1620 goto nest_cancel;
01048d9a
JP
1621 break;
1622 case TEAM_OPTION_TYPE_BOOL:
1623 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG))
9b00cf2d 1624 goto nest_cancel;
01048d9a
JP
1625 if (ctx.data.bool_val &&
1626 nla_put_flag(skb, TEAM_ATTR_OPTION_DATA))
9b00cf2d 1627 goto nest_cancel;
01048d9a
JP
1628 break;
1629 default:
1630 BUG();
1631 }
9b00cf2d
JP
1632 if (opt_inst->removed && nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED))
1633 goto nest_cancel;
1634 if (opt_inst->changed) {
1635 if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED))
1636 goto nest_cancel;
1637 opt_inst->changed = false;
1638 }
01048d9a
JP
1639 nla_nest_end(skb, option_item);
1640 return 0;
1641
9b00cf2d
JP
1642nest_cancel:
1643 nla_nest_cancel(skb, option_item);
1644 return -EMSGSIZE;
1645}
1646
1647static int __send_and_alloc_skb(struct sk_buff **pskb,
1648 struct team *team, u32 pid,
1649 team_nl_send_func_t *send_func)
1650{
1651 int err;
1652
1653 if (*pskb) {
1654 err = send_func(*pskb, team, pid);
1655 if (err)
1656 return err;
1657 }
58050fce 1658 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
9b00cf2d
JP
1659 if (!*pskb)
1660 return -ENOMEM;
1661 return 0;
01048d9a
JP
1662}
1663
9b00cf2d
JP
1664static int team_nl_send_options_get(struct team *team, u32 pid, u32 seq,
1665 int flags, team_nl_send_func_t *send_func,
01048d9a 1666 struct list_head *sel_opt_inst_list)
3d249d4c
JP
1667{
1668 struct nlattr *option_list;
9b00cf2d 1669 struct nlmsghdr *nlh;
3d249d4c 1670 void *hdr;
80f7c668
JP
1671 struct team_option_inst *opt_inst;
1672 int err;
9b00cf2d
JP
1673 struct sk_buff *skb = NULL;
1674 bool incomplete;
1675 int i;
3d249d4c 1676
9b00cf2d
JP
1677 opt_inst = list_first_entry(sel_opt_inst_list,
1678 struct team_option_inst, tmp_list);
1679
1680start_again:
1681 err = __send_and_alloc_skb(&skb, team, pid, send_func);
1682 if (err)
1683 return err;
1684
1685 hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags | NLM_F_MULTI,
3d249d4c
JP
1686 TEAM_CMD_OPTIONS_GET);
1687 if (IS_ERR(hdr))
1688 return PTR_ERR(hdr);
1689
86ebb02d
DM
1690 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
1691 goto nla_put_failure;
3d249d4c
JP
1692 option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
1693 if (!option_list)
75db986a 1694 goto nla_put_failure;
3d249d4c 1695
9b00cf2d
JP
1696 i = 0;
1697 incomplete = false;
1698 list_for_each_entry_from(opt_inst, sel_opt_inst_list, tmp_list) {
01048d9a 1699 err = team_nl_fill_one_option_get(skb, team, opt_inst);
9b00cf2d
JP
1700 if (err) {
1701 if (err == -EMSGSIZE) {
1702 if (!i)
1703 goto errout;
1704 incomplete = true;
1705 break;
1706 }
01048d9a 1707 goto errout;
9b00cf2d
JP
1708 }
1709 i++;
3d249d4c
JP
1710 }
1711
1712 nla_nest_end(skb, option_list);
9b00cf2d
JP
1713 genlmsg_end(skb, hdr);
1714 if (incomplete)
1715 goto start_again;
1716
1717send_done:
1718 nlh = nlmsg_put(skb, pid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
1719 if (!nlh) {
1720 err = __send_and_alloc_skb(&skb, team, pid, send_func);
1721 if (err)
1722 goto errout;
1723 goto send_done;
1724 }
1725
1726 return send_func(skb, team, pid);
3d249d4c
JP
1727
1728nla_put_failure:
80f7c668
JP
1729 err = -EMSGSIZE;
1730errout:
3d249d4c 1731 genlmsg_cancel(skb, hdr);
9b00cf2d 1732 nlmsg_free(skb);
80f7c668 1733 return err;
3d249d4c
JP
1734}
1735
3d249d4c
JP
1736static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
1737{
1738 struct team *team;
9b00cf2d 1739 struct team_option_inst *opt_inst;
3d249d4c 1740 int err;
9b00cf2d 1741 LIST_HEAD(sel_opt_inst_list);
3d249d4c
JP
1742
1743 team = team_nl_team_get(info);
1744 if (!team)
1745 return -EINVAL;
1746
9b00cf2d
JP
1747 list_for_each_entry(opt_inst, &team->option_inst_list, list)
1748 list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
1749 err = team_nl_send_options_get(team, info->snd_pid, info->snd_seq,
1750 NLM_F_ACK, team_nl_send_unicast,
1751 &sel_opt_inst_list);
3d249d4c
JP
1752
1753 team_nl_team_put(team);
1754
1755 return err;
1756}
1757
2fcdb2c9
JP
1758static int team_nl_send_event_options_get(struct team *team,
1759 struct list_head *sel_opt_inst_list);
1760
3d249d4c
JP
1761static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
1762{
1763 struct team *team;
1764 int err = 0;
1765 int i;
1766 struct nlattr *nl_option;
2fcdb2c9 1767 LIST_HEAD(opt_inst_list);
3d249d4c
JP
1768
1769 team = team_nl_team_get(info);
1770 if (!team)
1771 return -EINVAL;
1772
1773 err = -EINVAL;
1774 if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
1775 err = -EINVAL;
1776 goto team_put;
1777 }
1778
1779 nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
80f7c668 1780 struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1];
b1303326 1781 struct nlattr *attr;
14f066ba 1782 struct nlattr *attr_data;
3d249d4c 1783 enum team_option_type opt_type;
80f7c668 1784 int opt_port_ifindex = 0; /* != 0 for per-port options */
b1303326
JP
1785 u32 opt_array_index = 0;
1786 bool opt_is_array = false;
80f7c668 1787 struct team_option_inst *opt_inst;
3d249d4c
JP
1788 char *opt_name;
1789 bool opt_found = false;
1790
1791 if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
1792 err = -EINVAL;
1793 goto team_put;
1794 }
80f7c668 1795 err = nla_parse_nested(opt_attrs, TEAM_ATTR_OPTION_MAX,
3d249d4c
JP
1796 nl_option, team_nl_option_policy);
1797 if (err)
1798 goto team_put;
80f7c668 1799 if (!opt_attrs[TEAM_ATTR_OPTION_NAME] ||
14f066ba 1800 !opt_attrs[TEAM_ATTR_OPTION_TYPE]) {
3d249d4c
JP
1801 err = -EINVAL;
1802 goto team_put;
1803 }
80f7c668 1804 switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) {
3d249d4c
JP
1805 case NLA_U32:
1806 opt_type = TEAM_OPTION_TYPE_U32;
1807 break;
1808 case NLA_STRING:
1809 opt_type = TEAM_OPTION_TYPE_STRING;
1810 break;
2615598f
JP
1811 case NLA_BINARY:
1812 opt_type = TEAM_OPTION_TYPE_BINARY;
1813 break;
14f066ba
JP
1814 case NLA_FLAG:
1815 opt_type = TEAM_OPTION_TYPE_BOOL;
1816 break;
3d249d4c
JP
1817 default:
1818 goto team_put;
1819 }
1820
14f066ba
JP
1821 attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA];
1822 if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) {
1823 err = -EINVAL;
1824 goto team_put;
1825 }
1826
80f7c668 1827 opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]);
b1303326
JP
1828 attr = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX];
1829 if (attr)
1830 opt_port_ifindex = nla_get_u32(attr);
1831
1832 attr = opt_attrs[TEAM_ATTR_OPTION_ARRAY_INDEX];
1833 if (attr) {
1834 opt_is_array = true;
1835 opt_array_index = nla_get_u32(attr);
1836 }
80f7c668
JP
1837
1838 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
1839 struct team_option *option = opt_inst->option;
80f7c668 1840 struct team_gsetter_ctx ctx;
85d59a87 1841 struct team_option_inst_info *opt_inst_info;
80f7c668 1842 int tmp_ifindex;
3d249d4c 1843
85d59a87
JP
1844 opt_inst_info = &opt_inst->info;
1845 tmp_ifindex = opt_inst_info->port ?
1846 opt_inst_info->port->dev->ifindex : 0;
3d249d4c 1847 if (option->type != opt_type ||
80f7c668 1848 strcmp(option->name, opt_name) ||
b1303326
JP
1849 tmp_ifindex != opt_port_ifindex ||
1850 (option->array_size && !opt_is_array) ||
85d59a87 1851 opt_inst_info->array_index != opt_array_index)
3d249d4c
JP
1852 continue;
1853 opt_found = true;
85d59a87 1854 ctx.info = opt_inst_info;
3d249d4c
JP
1855 switch (opt_type) {
1856 case TEAM_OPTION_TYPE_U32:
14f066ba 1857 ctx.data.u32_val = nla_get_u32(attr_data);
3d249d4c
JP
1858 break;
1859 case TEAM_OPTION_TYPE_STRING:
14f066ba 1860 if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) {
2615598f
JP
1861 err = -EINVAL;
1862 goto team_put;
1863 }
14f066ba 1864 ctx.data.str_val = nla_data(attr_data);
3d249d4c 1865 break;
2615598f 1866 case TEAM_OPTION_TYPE_BINARY:
14f066ba
JP
1867 ctx.data.bin_val.len = nla_len(attr_data);
1868 ctx.data.bin_val.ptr = nla_data(attr_data);
1869 break;
1870 case TEAM_OPTION_TYPE_BOOL:
1871 ctx.data.bool_val = attr_data ? true : false;
2615598f 1872 break;
3d249d4c
JP
1873 default:
1874 BUG();
1875 }
80f7c668 1876 err = team_option_set(team, opt_inst, &ctx);
3d249d4c
JP
1877 if (err)
1878 goto team_put;
2fcdb2c9
JP
1879 opt_inst->changed = true;
1880 list_add(&opt_inst->tmp_list, &opt_inst_list);
3d249d4c
JP
1881 }
1882 if (!opt_found) {
1883 err = -ENOENT;
1884 goto team_put;
1885 }
1886 }
1887
2fcdb2c9
JP
1888 err = team_nl_send_event_options_get(team, &opt_inst_list);
1889
3d249d4c
JP
1890team_put:
1891 team_nl_team_put(team);
1892
1893 return err;
1894}
1895
b82b9183
JP
1896static int team_nl_fill_port_list_get(struct sk_buff *skb,
1897 u32 pid, u32 seq, int flags,
1898 struct team *team,
1899 bool fillall)
3d249d4c
JP
1900{
1901 struct nlattr *port_list;
1902 void *hdr;
1903 struct team_port *port;
1904
1905 hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags,
1906 TEAM_CMD_PORT_LIST_GET);
1907 if (IS_ERR(hdr))
1908 return PTR_ERR(hdr);
1909
86ebb02d
DM
1910 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
1911 goto nla_put_failure;
3d249d4c
JP
1912 port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
1913 if (!port_list)
3221c646 1914 goto nla_put_failure;
3d249d4c
JP
1915
1916 list_for_each_entry(port, &team->port_list, list) {
1917 struct nlattr *port_item;
1918
b82b9183
JP
1919 /* Include only changed ports if fill all mode is not on */
1920 if (!fillall && !port->changed)
1921 continue;
3d249d4c
JP
1922 port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
1923 if (!port_item)
1924 goto nla_put_failure;
86ebb02d
DM
1925 if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex))
1926 goto nla_put_failure;
b82b9183 1927 if (port->changed) {
86ebb02d
DM
1928 if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED))
1929 goto nla_put_failure;
b82b9183
JP
1930 port->changed = false;
1931 }
86ebb02d
DM
1932 if ((port->removed &&
1933 nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) ||
71472ec1 1934 (port->state.linkup &&
86ebb02d 1935 nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) ||
71472ec1
JP
1936 nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) ||
1937 nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex))
86ebb02d 1938 goto nla_put_failure;
3d249d4c
JP
1939 nla_nest_end(skb, port_item);
1940 }
1941
1942 nla_nest_end(skb, port_list);
1943 return genlmsg_end(skb, hdr);
1944
1945nla_put_failure:
1946 genlmsg_cancel(skb, hdr);
1947 return -EMSGSIZE;
1948}
1949
b82b9183
JP
1950static int team_nl_fill_port_list_get_all(struct sk_buff *skb,
1951 struct genl_info *info, int flags,
1952 struct team *team)
3d249d4c 1953{
b82b9183
JP
1954 return team_nl_fill_port_list_get(skb, info->snd_pid,
1955 info->snd_seq, NLM_F_ACK,
1956 team, true);
3d249d4c
JP
1957}
1958
1959static int team_nl_cmd_port_list_get(struct sk_buff *skb,
1960 struct genl_info *info)
1961{
1962 struct team *team;
1963 int err;
1964
1965 team = team_nl_team_get(info);
1966 if (!team)
1967 return -EINVAL;
1968
b82b9183 1969 err = team_nl_send_generic(info, team, team_nl_fill_port_list_get_all);
3d249d4c
JP
1970
1971 team_nl_team_put(team);
1972
1973 return err;
1974}
1975
1976static struct genl_ops team_nl_ops[] = {
1977 {
1978 .cmd = TEAM_CMD_NOOP,
1979 .doit = team_nl_cmd_noop,
1980 .policy = team_nl_policy,
1981 },
1982 {
1983 .cmd = TEAM_CMD_OPTIONS_SET,
1984 .doit = team_nl_cmd_options_set,
1985 .policy = team_nl_policy,
1986 .flags = GENL_ADMIN_PERM,
1987 },
1988 {
1989 .cmd = TEAM_CMD_OPTIONS_GET,
1990 .doit = team_nl_cmd_options_get,
1991 .policy = team_nl_policy,
1992 .flags = GENL_ADMIN_PERM,
1993 },
1994 {
1995 .cmd = TEAM_CMD_PORT_LIST_GET,
1996 .doit = team_nl_cmd_port_list_get,
1997 .policy = team_nl_policy,
1998 .flags = GENL_ADMIN_PERM,
1999 },
2000};
2001
2002static struct genl_multicast_group team_change_event_mcgrp = {
2003 .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME,
2004};
2005
9b00cf2d
JP
2006static int team_nl_send_multicast(struct sk_buff *skb,
2007 struct team *team, u32 pid)
2008{
2009 return genlmsg_multicast_netns(dev_net(team->dev), skb, 0,
2010 team_change_event_mcgrp.id, GFP_KERNEL);
2011}
2012
01048d9a
JP
2013static int team_nl_send_event_options_get(struct team *team,
2014 struct list_head *sel_opt_inst_list)
3d249d4c 2015{
9b00cf2d
JP
2016 return team_nl_send_options_get(team, 0, 0, 0, team_nl_send_multicast,
2017 sel_opt_inst_list);
3d249d4c
JP
2018}
2019
b82b9183 2020static int team_nl_send_event_port_list_get(struct team *team)
3d249d4c
JP
2021{
2022 struct sk_buff *skb;
2023 int err;
b82b9183 2024 struct net *net = dev_net(team->dev);
3d249d4c 2025
58050fce 2026 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3d249d4c
JP
2027 if (!skb)
2028 return -ENOMEM;
2029
b82b9183 2030 err = team_nl_fill_port_list_get(skb, 0, 0, 0, team, false);
3d249d4c
JP
2031 if (err < 0)
2032 goto err_fill;
2033
2034 err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id,
2035 GFP_KERNEL);
2036 return err;
2037
2038err_fill:
2039 nlmsg_free(skb);
2040 return err;
2041}
2042
2043static int team_nl_init(void)
2044{
2045 int err;
2046
2047 err = genl_register_family_with_ops(&team_nl_family, team_nl_ops,
2048 ARRAY_SIZE(team_nl_ops));
2049 if (err)
2050 return err;
2051
2052 err = genl_register_mc_group(&team_nl_family, &team_change_event_mcgrp);
2053 if (err)
2054 goto err_change_event_grp_reg;
2055
2056 return 0;
2057
2058err_change_event_grp_reg:
2059 genl_unregister_family(&team_nl_family);
2060
2061 return err;
2062}
2063
2064static void team_nl_fini(void)
2065{
2066 genl_unregister_family(&team_nl_family);
2067}
2068
2069
2070/******************
2071 * Change checkers
2072 ******************/
2073
b82b9183 2074static void __team_options_change_check(struct team *team)
3d249d4c
JP
2075{
2076 int err;
01048d9a
JP
2077 struct team_option_inst *opt_inst;
2078 LIST_HEAD(sel_opt_inst_list);
3d249d4c 2079
01048d9a
JP
2080 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2081 if (opt_inst->changed)
2082 list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
2083 }
2084 err = team_nl_send_event_options_get(team, &sel_opt_inst_list);
3d249d4c 2085 if (err)
9b00cf2d
JP
2086 netdev_warn(team->dev, "Failed to send options change via netlink (err %d)\n",
2087 err);
3d249d4c
JP
2088}
2089
2090/* rtnl lock is held */
2091static void __team_port_change_check(struct team_port *port, bool linkup)
2092{
2093 int err;
2094
71472ec1 2095 if (!port->removed && port->state.linkup == linkup)
3d249d4c
JP
2096 return;
2097
b82b9183 2098 port->changed = true;
71472ec1
JP
2099 port->state.linkup = linkup;
2100 team_refresh_port_linkup(port);
3d249d4c
JP
2101 if (linkup) {
2102 struct ethtool_cmd ecmd;
2103
2104 err = __ethtool_get_settings(port->dev, &ecmd);
2105 if (!err) {
71472ec1
JP
2106 port->state.speed = ethtool_cmd_speed(&ecmd);
2107 port->state.duplex = ecmd.duplex;
3d249d4c
JP
2108 goto send_event;
2109 }
2110 }
71472ec1
JP
2111 port->state.speed = 0;
2112 port->state.duplex = 0;
3d249d4c
JP
2113
2114send_event:
b82b9183 2115 err = team_nl_send_event_port_list_get(port->team);
3d249d4c
JP
2116 if (err)
2117 netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink\n",
2118 port->dev->name);
2119
2120}
2121
2122static void team_port_change_check(struct team_port *port, bool linkup)
2123{
2124 struct team *team = port->team;
2125
61dc3461 2126 mutex_lock(&team->lock);
3d249d4c 2127 __team_port_change_check(port, linkup);
61dc3461 2128 mutex_unlock(&team->lock);
3d249d4c
JP
2129}
2130
0f1aad2b 2131
3d249d4c
JP
2132/************************************
2133 * Net device notifier event handler
2134 ************************************/
2135
2136static int team_device_event(struct notifier_block *unused,
2137 unsigned long event, void *ptr)
2138{
2139 struct net_device *dev = (struct net_device *) ptr;
2140 struct team_port *port;
2141
2142 port = team_port_get_rtnl(dev);
2143 if (!port)
2144 return NOTIFY_DONE;
2145
2146 switch (event) {
2147 case NETDEV_UP:
2148 if (netif_carrier_ok(dev))
2149 team_port_change_check(port, true);
2150 case NETDEV_DOWN:
2151 team_port_change_check(port, false);
2152 case NETDEV_CHANGE:
2153 if (netif_running(port->dev))
2154 team_port_change_check(port,
2155 !!netif_carrier_ok(port->dev));
2156 break;
2157 case NETDEV_UNREGISTER:
2158 team_del_slave(port->team->dev, dev);
2159 break;
2160 case NETDEV_FEAT_CHANGE:
2161 team_compute_features(port->team);
2162 break;
2163 case NETDEV_CHANGEMTU:
2164 /* Forbid to change mtu of underlaying device */
2165 return NOTIFY_BAD;
2166 case NETDEV_PRE_TYPE_CHANGE:
2167 /* Forbid to change type of underlaying device */
2168 return NOTIFY_BAD;
2169 }
2170 return NOTIFY_DONE;
2171}
2172
2173static struct notifier_block team_notifier_block __read_mostly = {
2174 .notifier_call = team_device_event,
2175};
2176
2177
2178/***********************
2179 * Module init and exit
2180 ***********************/
2181
2182static int __init team_module_init(void)
2183{
2184 int err;
2185
2186 register_netdevice_notifier(&team_notifier_block);
2187
2188 err = rtnl_link_register(&team_link_ops);
2189 if (err)
2190 goto err_rtnl_reg;
2191
2192 err = team_nl_init();
2193 if (err)
2194 goto err_nl_init;
2195
2196 return 0;
2197
2198err_nl_init:
2199 rtnl_link_unregister(&team_link_ops);
2200
2201err_rtnl_reg:
2202 unregister_netdevice_notifier(&team_notifier_block);
2203
2204 return err;
2205}
2206
2207static void __exit team_module_exit(void)
2208{
2209 team_nl_fini();
2210 rtnl_link_unregister(&team_link_ops);
2211 unregister_netdevice_notifier(&team_notifier_block);
2212}
2213
2214module_init(team_module_init);
2215module_exit(team_module_exit);
2216
2217MODULE_LICENSE("GPL v2");
2218MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
2219MODULE_DESCRIPTION("Ethernet team device driver");
2220MODULE_ALIAS_RTNL_LINK(DRV_NAME);