1 /*
2 * drivers/net/team/team.c - Network team device driver
3 * Copyright (c) 2011 Jiri Pirko <jpirko@redhat.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 */
10
11 #include <linux/kernel.h>
12 #include <linux/types.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/rcupdate.h>
17 #include <linux/errno.h>
18 #include <linux/ctype.h>
19 #include <linux/notifier.h>
20 #include <linux/netdevice.h>
21 #include <linux/netpoll.h>
22 #include <linux/if_vlan.h>
23 #include <linux/if_arp.h>
24 #include <linux/socket.h>
25 #include <linux/etherdevice.h>
26 #include <linux/rtnetlink.h>
27 #include <net/rtnetlink.h>
28 #include <net/genetlink.h>
29 #include <net/netlink.h>
30 #include <net/sch_generic.h>
31 #include <net/switchdev.h>
32 #include <generated/utsrelease.h>
33 #include <linux/if_team.h>
34
35 #define DRV_NAME "team"
36
37
38 /**********
39 * Helpers
40 **********/
41
42 #define team_port_exists(dev) (dev->priv_flags & IFF_TEAM_PORT)
43
team_port_get_rcu(const struct net_device * dev)44 static struct team_port *team_port_get_rcu(const struct net_device *dev)
45 {
46 return rcu_dereference(dev->rx_handler_data);
47 }
48
team_port_get_rtnl(const struct net_device * dev)49 static struct team_port *team_port_get_rtnl(const struct net_device *dev)
50 {
51 struct team_port *port = rtnl_dereference(dev->rx_handler_data);
52
53 return team_port_exists(dev) ? port : NULL;
54 }
55
56 /*
57 * Since the ability to change device address for open port device is tested in
58 * team_port_add, this function can be called without control of return value
59 */
__set_port_dev_addr(struct net_device * port_dev,const unsigned char * dev_addr)60 static int __set_port_dev_addr(struct net_device *port_dev,
61 const unsigned char *dev_addr)
62 {
63 struct sockaddr addr;
64
65 memcpy(addr.sa_data, dev_addr, port_dev->addr_len);
66 addr.sa_family = port_dev->type;
67 return dev_set_mac_address(port_dev, &addr);
68 }
69
team_port_set_orig_dev_addr(struct team_port * port)70 static int team_port_set_orig_dev_addr(struct team_port *port)
71 {
72 return __set_port_dev_addr(port->dev, port->orig.dev_addr);
73 }
74
team_port_set_team_dev_addr(struct team * team,struct team_port * port)75 static int team_port_set_team_dev_addr(struct team *team,
76 struct team_port *port)
77 {
78 return __set_port_dev_addr(port->dev, team->dev->dev_addr);
79 }
80
team_modeop_port_enter(struct team * team,struct team_port * port)81 int team_modeop_port_enter(struct team *team, struct team_port *port)
82 {
83 return team_port_set_team_dev_addr(team, port);
84 }
85 EXPORT_SYMBOL(team_modeop_port_enter);
86
team_modeop_port_change_dev_addr(struct team * team,struct team_port * port)87 void team_modeop_port_change_dev_addr(struct team *team,
88 struct team_port *port)
89 {
90 team_port_set_team_dev_addr(team, port);
91 }
92 EXPORT_SYMBOL(team_modeop_port_change_dev_addr);
93
team_refresh_port_linkup(struct team_port * port)94 static void team_refresh_port_linkup(struct team_port *port)
95 {
96 port->linkup = port->user.linkup_enabled ? port->user.linkup :
97 port->state.linkup;
98 }
99
100
101 /*******************
102 * Options handling
103 *******************/
104
105 struct team_option_inst { /* One for each option instance */
106 struct list_head list;
107 struct list_head tmp_list;
108 struct team_option *option;
109 struct team_option_inst_info info;
110 bool changed;
111 bool removed;
112 };
113
__team_find_option(struct team * team,const char * opt_name)114 static struct team_option *__team_find_option(struct team *team,
115 const char *opt_name)
116 {
117 struct team_option *option;
118
119 list_for_each_entry(option, &team->option_list, list) {
120 if (strcmp(option->name, opt_name) == 0)
121 return option;
122 }
123 return NULL;
124 }
125
__team_option_inst_del(struct team_option_inst * opt_inst)126 static void __team_option_inst_del(struct team_option_inst *opt_inst)
127 {
128 list_del(&opt_inst->list);
129 kfree(opt_inst);
130 }
131
__team_option_inst_del_option(struct team * team,struct team_option * option)132 static void __team_option_inst_del_option(struct team *team,
133 struct team_option *option)
134 {
135 struct team_option_inst *opt_inst, *tmp;
136
137 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
138 if (opt_inst->option == option)
139 __team_option_inst_del(opt_inst);
140 }
141 }
142
__team_option_inst_add(struct team * team,struct team_option * option,struct team_port * port)143 static int __team_option_inst_add(struct team *team, struct team_option *option,
144 struct team_port *port)
145 {
146 struct team_option_inst *opt_inst;
147 unsigned int array_size;
148 unsigned int i;
149 int err;
150
151 array_size = option->array_size;
152 if (!array_size)
153 array_size = 1; /* No array but still need one instance */
154
155 for (i = 0; i < array_size; i++) {
156 opt_inst = kmalloc(sizeof(*opt_inst), GFP_KERNEL);
157 if (!opt_inst)
158 return -ENOMEM;
159 opt_inst->option = option;
160 opt_inst->info.port = port;
161 opt_inst->info.array_index = i;
162 opt_inst->changed = true;
163 opt_inst->removed = false;
164 list_add_tail(&opt_inst->list, &team->option_inst_list);
165 if (option->init) {
166 err = option->init(team, &opt_inst->info);
167 if (err)
168 return err;
169 }
170
171 }
172 return 0;
173 }
174
__team_option_inst_add_option(struct team * team,struct team_option * option)175 static int __team_option_inst_add_option(struct team *team,
176 struct team_option *option)
177 {
178 int err;
179
180 if (!option->per_port) {
181 err = __team_option_inst_add(team, option, NULL);
182 if (err)
183 goto inst_del_option;
184 }
185 return 0;
186
187 inst_del_option:
188 __team_option_inst_del_option(team, option);
189 return err;
190 }
191
__team_option_inst_mark_removed_option(struct team * team,struct team_option * option)192 static void __team_option_inst_mark_removed_option(struct team *team,
193 struct team_option *option)
194 {
195 struct team_option_inst *opt_inst;
196
197 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
198 if (opt_inst->option == option) {
199 opt_inst->changed = true;
200 opt_inst->removed = true;
201 }
202 }
203 }
204
__team_option_inst_del_port(struct team * team,struct team_port * port)205 static void __team_option_inst_del_port(struct team *team,
206 struct team_port *port)
207 {
208 struct team_option_inst *opt_inst, *tmp;
209
210 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
211 if (opt_inst->option->per_port &&
212 opt_inst->info.port == port)
213 __team_option_inst_del(opt_inst);
214 }
215 }
216
__team_option_inst_add_port(struct team * team,struct team_port * port)217 static int __team_option_inst_add_port(struct team *team,
218 struct team_port *port)
219 {
220 struct team_option *option;
221 int err;
222
223 list_for_each_entry(option, &team->option_list, list) {
224 if (!option->per_port)
225 continue;
226 err = __team_option_inst_add(team, option, port);
227 if (err)
228 goto inst_del_port;
229 }
230 return 0;
231
232 inst_del_port:
233 __team_option_inst_del_port(team, port);
234 return err;
235 }
236
__team_option_inst_mark_removed_port(struct team * team,struct team_port * port)237 static void __team_option_inst_mark_removed_port(struct team *team,
238 struct team_port *port)
239 {
240 struct team_option_inst *opt_inst;
241
242 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
243 if (opt_inst->info.port == port) {
244 opt_inst->changed = true;
245 opt_inst->removed = true;
246 }
247 }
248 }
249
__team_options_register(struct team * team,const struct team_option * option,size_t option_count)250 static int __team_options_register(struct team *team,
251 const struct team_option *option,
252 size_t option_count)
253 {
254 int i;
255 struct team_option **dst_opts;
256 int err;
257
258 dst_opts = kzalloc(sizeof(struct team_option *) * option_count,
259 GFP_KERNEL);
260 if (!dst_opts)
261 return -ENOMEM;
262 for (i = 0; i < option_count; i++, option++) {
263 if (__team_find_option(team, option->name)) {
264 err = -EEXIST;
265 goto alloc_rollback;
266 }
267 dst_opts[i] = kmemdup(option, sizeof(*option), GFP_KERNEL);
268 if (!dst_opts[i]) {
269 err = -ENOMEM;
270 goto alloc_rollback;
271 }
272 }
273
274 for (i = 0; i < option_count; i++) {
275 err = __team_option_inst_add_option(team, dst_opts[i]);
276 if (err)
277 goto inst_rollback;
278 list_add_tail(&dst_opts[i]->list, &team->option_list);
279 }
280
281 kfree(dst_opts);
282 return 0;
283
284 inst_rollback:
285 for (i--; i >= 0; i--)
286 __team_option_inst_del_option(team, dst_opts[i]);
287
288 i = option_count - 1;
289 alloc_rollback:
290 for (i--; i >= 0; i--)
291 kfree(dst_opts[i]);
292
293 kfree(dst_opts);
294 return err;
295 }
296
__team_options_mark_removed(struct team * team,const struct team_option * option,size_t option_count)297 static void __team_options_mark_removed(struct team *team,
298 const struct team_option *option,
299 size_t option_count)
300 {
301 int i;
302
303 for (i = 0; i < option_count; i++, option++) {
304 struct team_option *del_opt;
305
306 del_opt = __team_find_option(team, option->name);
307 if (del_opt)
308 __team_option_inst_mark_removed_option(team, del_opt);
309 }
310 }
311
__team_options_unregister(struct team * team,const struct team_option * option,size_t option_count)312 static void __team_options_unregister(struct team *team,
313 const struct team_option *option,
314 size_t option_count)
315 {
316 int i;
317
318 for (i = 0; i < option_count; i++, option++) {
319 struct team_option *del_opt;
320
321 del_opt = __team_find_option(team, option->name);
322 if (del_opt) {
323 __team_option_inst_del_option(team, del_opt);
324 list_del(&del_opt->list);
325 kfree(del_opt);
326 }
327 }
328 }
329
330 static void __team_options_change_check(struct team *team);
331
team_options_register(struct team * team,const struct team_option * option,size_t option_count)332 int team_options_register(struct team *team,
333 const struct team_option *option,
334 size_t option_count)
335 {
336 int err;
337
338 err = __team_options_register(team, option, option_count);
339 if (err)
340 return err;
341 __team_options_change_check(team);
342 return 0;
343 }
344 EXPORT_SYMBOL(team_options_register);
345
team_options_unregister(struct team * team,const struct team_option * option,size_t option_count)346 void team_options_unregister(struct team *team,
347 const struct team_option *option,
348 size_t option_count)
349 {
350 __team_options_mark_removed(team, option, option_count);
351 __team_options_change_check(team);
352 __team_options_unregister(team, option, option_count);
353 }
354 EXPORT_SYMBOL(team_options_unregister);
355
team_option_get(struct team * team,struct team_option_inst * opt_inst,struct team_gsetter_ctx * ctx)356 static int team_option_get(struct team *team,
357 struct team_option_inst *opt_inst,
358 struct team_gsetter_ctx *ctx)
359 {
360 if (!opt_inst->option->getter)
361 return -EOPNOTSUPP;
362 return opt_inst->option->getter(team, ctx);
363 }
364
team_option_set(struct team * team,struct team_option_inst * opt_inst,struct team_gsetter_ctx * ctx)365 static int team_option_set(struct team *team,
366 struct team_option_inst *opt_inst,
367 struct team_gsetter_ctx *ctx)
368 {
369 if (!opt_inst->option->setter)
370 return -EOPNOTSUPP;
371 return opt_inst->option->setter(team, ctx);
372 }
373
team_option_inst_set_change(struct team_option_inst_info * opt_inst_info)374 void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info)
375 {
376 struct team_option_inst *opt_inst;
377
378 opt_inst = container_of(opt_inst_info, struct team_option_inst, info);
379 opt_inst->changed = true;
380 }
381 EXPORT_SYMBOL(team_option_inst_set_change);
382
team_options_change_check(struct team * team)383 void team_options_change_check(struct team *team)
384 {
385 __team_options_change_check(team);
386 }
387 EXPORT_SYMBOL(team_options_change_check);
388
389
390 /****************
391 * Mode handling
392 ****************/
393
394 static LIST_HEAD(mode_list);
395 static DEFINE_SPINLOCK(mode_list_lock);
396
397 struct team_mode_item {
398 struct list_head list;
399 const struct team_mode *mode;
400 };
401
__find_mode(const char * kind)402 static struct team_mode_item *__find_mode(const char *kind)
403 {
404 struct team_mode_item *mitem;
405
406 list_for_each_entry(mitem, &mode_list, list) {
407 if (strcmp(mitem->mode->kind, kind) == 0)
408 return mitem;
409 }
410 return NULL;
411 }
412
is_good_mode_name(const char * name)413 static bool is_good_mode_name(const char *name)
414 {
415 while (*name != '\0') {
416 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
417 return false;
418 name++;
419 }
420 return true;
421 }
422
team_mode_register(const struct team_mode * mode)423 int team_mode_register(const struct team_mode *mode)
424 {
425 int err = 0;
426 struct team_mode_item *mitem;
427
428 if (!is_good_mode_name(mode->kind) ||
429 mode->priv_size > TEAM_MODE_PRIV_SIZE)
430 return -EINVAL;
431
432 mitem = kmalloc(sizeof(*mitem), GFP_KERNEL);
433 if (!mitem)
434 return -ENOMEM;
435
436 spin_lock(&mode_list_lock);
437 if (__find_mode(mode->kind)) {
438 err = -EEXIST;
439 kfree(mitem);
440 goto unlock;
441 }
442 mitem->mode = mode;
443 list_add_tail(&mitem->list, &mode_list);
444 unlock:
445 spin_unlock(&mode_list_lock);
446 return err;
447 }
448 EXPORT_SYMBOL(team_mode_register);
449
team_mode_unregister(const struct team_mode * mode)450 void team_mode_unregister(const struct team_mode *mode)
451 {
452 struct team_mode_item *mitem;
453
454 spin_lock(&mode_list_lock);
455 mitem = __find_mode(mode->kind);
456 if (mitem) {
457 list_del_init(&mitem->list);
458 kfree(mitem);
459 }
460 spin_unlock(&mode_list_lock);
461 }
462 EXPORT_SYMBOL(team_mode_unregister);
463
team_mode_get(const char * kind)464 static const struct team_mode *team_mode_get(const char *kind)
465 {
466 struct team_mode_item *mitem;
467 const struct team_mode *mode = NULL;
468
469 spin_lock(&mode_list_lock);
470 mitem = __find_mode(kind);
471 if (!mitem) {
472 spin_unlock(&mode_list_lock);
473 request_module("team-mode-%s", kind);
474 spin_lock(&mode_list_lock);
475 mitem = __find_mode(kind);
476 }
477 if (mitem) {
478 mode = mitem->mode;
479 if (!try_module_get(mode->owner))
480 mode = NULL;
481 }
482
483 spin_unlock(&mode_list_lock);
484 return mode;
485 }
486
team_mode_put(const struct team_mode * mode)487 static void team_mode_put(const struct team_mode *mode)
488 {
489 module_put(mode->owner);
490 }
491
team_dummy_transmit(struct team * team,struct sk_buff * skb)492 static bool team_dummy_transmit(struct team *team, struct sk_buff *skb)
493 {
494 dev_kfree_skb_any(skb);
495 return false;
496 }
497
team_dummy_receive(struct team * team,struct team_port * port,struct sk_buff * skb)498 static rx_handler_result_t team_dummy_receive(struct team *team,
499 struct team_port *port,
500 struct sk_buff *skb)
501 {
502 return RX_HANDLER_ANOTHER;
503 }
504
505 static const struct team_mode __team_no_mode = {
506 .kind = "*NOMODE*",
507 };
508
team_is_mode_set(struct team * team)509 static bool team_is_mode_set(struct team *team)
510 {
511 return team->mode != &__team_no_mode;
512 }
513
team_set_no_mode(struct team * team)514 static void team_set_no_mode(struct team *team)
515 {
516 team->user_carrier_enabled = false;
517 team->mode = &__team_no_mode;
518 }
519
team_adjust_ops(struct team * team)520 static void team_adjust_ops(struct team *team)
521 {
522 /*
523 * To avoid checks in rx/tx skb paths, ensure here that non-null and
524 * correct ops are always set.
525 */
526
527 if (!team->en_port_count || !team_is_mode_set(team) ||
528 !team->mode->ops->transmit)
529 team->ops.transmit = team_dummy_transmit;
530 else
531 team->ops.transmit = team->mode->ops->transmit;
532
533 if (!team->en_port_count || !team_is_mode_set(team) ||
534 !team->mode->ops->receive)
535 team->ops.receive = team_dummy_receive;
536 else
537 team->ops.receive = team->mode->ops->receive;
538 }
539
540 /*
541 * We can benefit from the fact that it's ensured no port is present
542 * at the time of mode change. Therefore no packets are in fly so there's no
543 * need to set mode operations in any special way.
544 */
__team_change_mode(struct team * team,const struct team_mode * new_mode)545 static int __team_change_mode(struct team *team,
546 const struct team_mode *new_mode)
547 {
548 /* Check if mode was previously set and do cleanup if so */
549 if (team_is_mode_set(team)) {
550 void (*exit_op)(struct team *team) = team->ops.exit;
551
552 /* Clear ops area so no callback is called any longer */
553 memset(&team->ops, 0, sizeof(struct team_mode_ops));
554 team_adjust_ops(team);
555
556 if (exit_op)
557 exit_op(team);
558 team_mode_put(team->mode);
559 team_set_no_mode(team);
560 /* zero private data area */
561 memset(&team->mode_priv, 0,
562 sizeof(struct team) - offsetof(struct team, mode_priv));
563 }
564
565 if (!new_mode)
566 return 0;
567
568 if (new_mode->ops->init) {
569 int err;
570
571 err = new_mode->ops->init(team);
572 if (err)
573 return err;
574 }
575
576 team->mode = new_mode;
577 memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops));
578 team_adjust_ops(team);
579
580 return 0;
581 }
582
team_change_mode(struct team * team,const char * kind)583 static int team_change_mode(struct team *team, const char *kind)
584 {
585 const struct team_mode *new_mode;
586 struct net_device *dev = team->dev;
587 int err;
588
589 if (!list_empty(&team->port_list)) {
590 netdev_err(dev, "No ports can be present during mode change\n");
591 return -EBUSY;
592 }
593
594 if (team_is_mode_set(team) && strcmp(team->mode->kind, kind) == 0) {
595 netdev_err(dev, "Unable to change to the same mode the team is in\n");
596 return -EINVAL;
597 }
598
599 new_mode = team_mode_get(kind);
600 if (!new_mode) {
601 netdev_err(dev, "Mode \"%s\" not found\n", kind);
602 return -EINVAL;
603 }
604
605 err = __team_change_mode(team, new_mode);
606 if (err) {
607 netdev_err(dev, "Failed to change to mode \"%s\"\n", kind);
608 team_mode_put(new_mode);
609 return err;
610 }
611
612 netdev_info(dev, "Mode changed to \"%s\"\n", kind);
613 return 0;
614 }
615
616
617 /*********************
618 * Peers notification
619 *********************/
620
team_notify_peers_work(struct work_struct * work)621 static void team_notify_peers_work(struct work_struct *work)
622 {
623 struct team *team;
624 int val;
625
626 team = container_of(work, struct team, notify_peers.dw.work);
627
628 if (!rtnl_trylock()) {
629 schedule_delayed_work(&team->notify_peers.dw, 0);
630 return;
631 }
632 val = atomic_dec_if_positive(&team->notify_peers.count_pending);
633 if (val < 0) {
634 rtnl_unlock();
635 return;
636 }
637 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, team->dev);
638 rtnl_unlock();
639 if (val)
640 schedule_delayed_work(&team->notify_peers.dw,
641 msecs_to_jiffies(team->notify_peers.interval));
642 }
643
team_notify_peers(struct team * team)644 static void team_notify_peers(struct team *team)
645 {
646 if (!team->notify_peers.count || !netif_running(team->dev))
647 return;
648 atomic_add(team->notify_peers.count, &team->notify_peers.count_pending);
649 schedule_delayed_work(&team->notify_peers.dw, 0);
650 }
651
team_notify_peers_init(struct team * team)652 static void team_notify_peers_init(struct team *team)
653 {
654 INIT_DELAYED_WORK(&team->notify_peers.dw, team_notify_peers_work);
655 }
656
team_notify_peers_fini(struct team * team)657 static void team_notify_peers_fini(struct team *team)
658 {
659 cancel_delayed_work_sync(&team->notify_peers.dw);
660 }
661
662
663 /*******************************
664 * Send multicast group rejoins
665 *******************************/
666
team_mcast_rejoin_work(struct work_struct * work)667 static void team_mcast_rejoin_work(struct work_struct *work)
668 {
669 struct team *team;
670 int val;
671
672 team = container_of(work, struct team, mcast_rejoin.dw.work);
673
674 if (!rtnl_trylock()) {
675 schedule_delayed_work(&team->mcast_rejoin.dw, 0);
676 return;
677 }
678 val = atomic_dec_if_positive(&team->mcast_rejoin.count_pending);
679 if (val < 0) {
680 rtnl_unlock();
681 return;
682 }
683 call_netdevice_notifiers(NETDEV_RESEND_IGMP, team->dev);
684 rtnl_unlock();
685 if (val)
686 schedule_delayed_work(&team->mcast_rejoin.dw,
687 msecs_to_jiffies(team->mcast_rejoin.interval));
688 }
689
team_mcast_rejoin(struct team * team)690 static void team_mcast_rejoin(struct team *team)
691 {
692 if (!team->mcast_rejoin.count || !netif_running(team->dev))
693 return;
694 atomic_add(team->mcast_rejoin.count, &team->mcast_rejoin.count_pending);
695 schedule_delayed_work(&team->mcast_rejoin.dw, 0);
696 }
697
team_mcast_rejoin_init(struct team * team)698 static void team_mcast_rejoin_init(struct team *team)
699 {
700 INIT_DELAYED_WORK(&team->mcast_rejoin.dw, team_mcast_rejoin_work);
701 }
702
team_mcast_rejoin_fini(struct team * team)703 static void team_mcast_rejoin_fini(struct team *team)
704 {
705 cancel_delayed_work_sync(&team->mcast_rejoin.dw);
706 }
707
708
709 /************************
710 * Rx path frame handler
711 ************************/
712
713 /* note: already called with rcu_read_lock */
team_handle_frame(struct sk_buff ** pskb)714 static rx_handler_result_t team_handle_frame(struct sk_buff **pskb)
715 {
716 struct sk_buff *skb = *pskb;
717 struct team_port *port;
718 struct team *team;
719 rx_handler_result_t res;
720
721 skb = skb_share_check(skb, GFP_ATOMIC);
722 if (!skb)
723 return RX_HANDLER_CONSUMED;
724
725 *pskb = skb;
726
727 port = team_port_get_rcu(skb->dev);
728 team = port->team;
729 if (!team_port_enabled(port)) {
730 /* allow exact match delivery for disabled ports */
731 res = RX_HANDLER_EXACT;
732 } else {
733 res = team->ops.receive(team, port, skb);
734 }
735 if (res == RX_HANDLER_ANOTHER) {
736 struct team_pcpu_stats *pcpu_stats;
737
738 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
739 u64_stats_update_begin(&pcpu_stats->syncp);
740 pcpu_stats->rx_packets++;
741 pcpu_stats->rx_bytes += skb->len;
742 if (skb->pkt_type == PACKET_MULTICAST)
743 pcpu_stats->rx_multicast++;
744 u64_stats_update_end(&pcpu_stats->syncp);
745
746 skb->dev = team->dev;
747 } else {
748 this_cpu_inc(team->pcpu_stats->rx_dropped);
749 }
750
751 return res;
752 }
753
754
755 /*************************************
756 * Multiqueue Tx port select override
757 *************************************/
758
team_queue_override_init(struct team * team)759 static int team_queue_override_init(struct team *team)
760 {
761 struct list_head *listarr;
762 unsigned int queue_cnt = team->dev->num_tx_queues - 1;
763 unsigned int i;
764
765 if (!queue_cnt)
766 return 0;
767 listarr = kmalloc(sizeof(struct list_head) * queue_cnt, GFP_KERNEL);
768 if (!listarr)
769 return -ENOMEM;
770 team->qom_lists = listarr;
771 for (i = 0; i < queue_cnt; i++)
772 INIT_LIST_HEAD(listarr++);
773 return 0;
774 }
775
team_queue_override_fini(struct team * team)776 static void team_queue_override_fini(struct team *team)
777 {
778 kfree(team->qom_lists);
779 }
780
__team_get_qom_list(struct team * team,u16 queue_id)781 static struct list_head *__team_get_qom_list(struct team *team, u16 queue_id)
782 {
783 return &team->qom_lists[queue_id - 1];
784 }
785
786 /*
787 * note: already called with rcu_read_lock
788 */
team_queue_override_transmit(struct team * team,struct sk_buff * skb)789 static bool team_queue_override_transmit(struct team *team, struct sk_buff *skb)
790 {
791 struct list_head *qom_list;
792 struct team_port *port;
793
794 if (!team->queue_override_enabled || !skb->queue_mapping)
795 return false;
796 qom_list = __team_get_qom_list(team, skb->queue_mapping);
797 list_for_each_entry_rcu(port, qom_list, qom_list) {
798 if (!team_dev_queue_xmit(team, port, skb))
799 return true;
800 }
801 return false;
802 }
803
__team_queue_override_port_del(struct team * team,struct team_port * port)804 static void __team_queue_override_port_del(struct team *team,
805 struct team_port *port)
806 {
807 if (!port->queue_id)
808 return;
809 list_del_rcu(&port->qom_list);
810 }
811
team_queue_override_port_has_gt_prio_than(struct team_port * port,struct team_port * cur)812 static bool team_queue_override_port_has_gt_prio_than(struct team_port *port,
813 struct team_port *cur)
814 {
815 if (port->priority < cur->priority)
816 return true;
817 if (port->priority > cur->priority)
818 return false;
819 if (port->index < cur->index)
820 return true;
821 return false;
822 }
823
__team_queue_override_port_add(struct team * team,struct team_port * port)824 static void __team_queue_override_port_add(struct team *team,
825 struct team_port *port)
826 {
827 struct team_port *cur;
828 struct list_head *qom_list;
829 struct list_head *node;
830
831 if (!port->queue_id)
832 return;
833 qom_list = __team_get_qom_list(team, port->queue_id);
834 node = qom_list;
835 list_for_each_entry(cur, qom_list, qom_list) {
836 if (team_queue_override_port_has_gt_prio_than(port, cur))
837 break;
838 node = &cur->qom_list;
839 }
840 list_add_tail_rcu(&port->qom_list, node);
841 }
842
__team_queue_override_enabled_check(struct team * team)843 static void __team_queue_override_enabled_check(struct team *team)
844 {
845 struct team_port *port;
846 bool enabled = false;
847
848 list_for_each_entry(port, &team->port_list, list) {
849 if (port->queue_id) {
850 enabled = true;
851 break;
852 }
853 }
854 if (enabled == team->queue_override_enabled)
855 return;
856 netdev_dbg(team->dev, "%s queue override\n",
857 enabled ? "Enabling" : "Disabling");
858 team->queue_override_enabled = enabled;
859 }
860
team_queue_override_port_prio_changed(struct team * team,struct team_port * port)861 static void team_queue_override_port_prio_changed(struct team *team,
862 struct team_port *port)
863 {
864 if (!port->queue_id || team_port_enabled(port))
865 return;
866 __team_queue_override_port_del(team, port);
867 __team_queue_override_port_add(team, port);
868 __team_queue_override_enabled_check(team);
869 }
870
team_queue_override_port_change_queue_id(struct team * team,struct team_port * port,u16 new_queue_id)871 static void team_queue_override_port_change_queue_id(struct team *team,
872 struct team_port *port,
873 u16 new_queue_id)
874 {
875 if (team_port_enabled(port)) {
876 __team_queue_override_port_del(team, port);
877 port->queue_id = new_queue_id;
878 __team_queue_override_port_add(team, port);
879 __team_queue_override_enabled_check(team);
880 } else {
881 port->queue_id = new_queue_id;
882 }
883 }
884
team_queue_override_port_add(struct team * team,struct team_port * port)885 static void team_queue_override_port_add(struct team *team,
886 struct team_port *port)
887 {
888 __team_queue_override_port_add(team, port);
889 __team_queue_override_enabled_check(team);
890 }
891
team_queue_override_port_del(struct team * team,struct team_port * port)892 static void team_queue_override_port_del(struct team *team,
893 struct team_port *port)
894 {
895 __team_queue_override_port_del(team, port);
896 __team_queue_override_enabled_check(team);
897 }
898
899
900 /****************
901 * Port handling
902 ****************/
903
team_port_find(const struct team * team,const struct team_port * port)904 static bool team_port_find(const struct team *team,
905 const struct team_port *port)
906 {
907 struct team_port *cur;
908
909 list_for_each_entry(cur, &team->port_list, list)
910 if (cur == port)
911 return true;
912 return false;
913 }
914
915 /*
916 * Enable/disable port by adding to enabled port hashlist and setting
917 * port->index (Might be racy so reader could see incorrect ifindex when
918 * processing a flying packet, but that is not a problem). Write guarded
919 * by team->lock.
920 */
team_port_enable(struct team * team,struct team_port * port)921 static void team_port_enable(struct team *team,
922 struct team_port *port)
923 {
924 if (team_port_enabled(port))
925 return;
926 port->index = team->en_port_count++;
927 hlist_add_head_rcu(&port->hlist,
928 team_port_index_hash(team, port->index));
929 team_adjust_ops(team);
930 team_queue_override_port_add(team, port);
931 if (team->ops.port_enabled)
932 team->ops.port_enabled(team, port);
933 team_notify_peers(team);
934 team_mcast_rejoin(team);
935 }
936
__reconstruct_port_hlist(struct team * team,int rm_index)937 static void __reconstruct_port_hlist(struct team *team, int rm_index)
938 {
939 int i;
940 struct team_port *port;
941
942 for (i = rm_index + 1; i < team->en_port_count; i++) {
943 port = team_get_port_by_index(team, i);
944 hlist_del_rcu(&port->hlist);
945 port->index--;
946 hlist_add_head_rcu(&port->hlist,
947 team_port_index_hash(team, port->index));
948 }
949 }
950
team_port_disable(struct team * team,struct team_port * port)951 static void team_port_disable(struct team *team,
952 struct team_port *port)
953 {
954 if (!team_port_enabled(port))
955 return;
956 if (team->ops.port_disabled)
957 team->ops.port_disabled(team, port);
958 hlist_del_rcu(&port->hlist);
959 __reconstruct_port_hlist(team, port->index);
960 port->index = -1;
961 team->en_port_count--;
962 team_queue_override_port_del(team, port);
963 team_adjust_ops(team);
964 team_notify_peers(team);
965 team_mcast_rejoin(team);
966 }
967
968 #define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \
969 NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
970 NETIF_F_HIGHDMA | NETIF_F_LRO)
971
___team_compute_features(struct team * team)972 static void ___team_compute_features(struct team *team)
973 {
974 struct team_port *port;
975 u32 vlan_features = TEAM_VLAN_FEATURES & NETIF_F_ALL_FOR_ALL;
976 unsigned short max_hard_header_len = ETH_HLEN;
977 unsigned int dst_release_flag = IFF_XMIT_DST_RELEASE |
978 IFF_XMIT_DST_RELEASE_PERM;
979
980 list_for_each_entry(port, &team->port_list, list) {
981 vlan_features = netdev_increment_features(vlan_features,
982 port->dev->vlan_features,
983 TEAM_VLAN_FEATURES);
984
985 dst_release_flag &= port->dev->priv_flags;
986 if (port->dev->hard_header_len > max_hard_header_len)
987 max_hard_header_len = port->dev->hard_header_len;
988 }
989
990 team->dev->vlan_features = vlan_features;
991 team->dev->hard_header_len = max_hard_header_len;
992
993 team->dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
994 if (dst_release_flag == (IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM))
995 team->dev->priv_flags |= IFF_XMIT_DST_RELEASE;
996 }
997
__team_compute_features(struct team * team)998 static void __team_compute_features(struct team *team)
999 {
1000 ___team_compute_features(team);
1001 netdev_change_features(team->dev);
1002 }
1003
team_compute_features(struct team * team)1004 static void team_compute_features(struct team *team)
1005 {
1006 mutex_lock(&team->lock);
1007 ___team_compute_features(team);
1008 mutex_unlock(&team->lock);
1009 netdev_change_features(team->dev);
1010 }
1011
team_port_enter(struct team * team,struct team_port * port)1012 static int team_port_enter(struct team *team, struct team_port *port)
1013 {
1014 int err = 0;
1015
1016 dev_hold(team->dev);
1017 if (team->ops.port_enter) {
1018 err = team->ops.port_enter(team, port);
1019 if (err) {
1020 netdev_err(team->dev, "Device %s failed to enter team mode\n",
1021 port->dev->name);
1022 goto err_port_enter;
1023 }
1024 }
1025
1026 return 0;
1027
1028 err_port_enter:
1029 dev_put(team->dev);
1030
1031 return err;
1032 }
1033
team_port_leave(struct team * team,struct team_port * port)1034 static void team_port_leave(struct team *team, struct team_port *port)
1035 {
1036 if (team->ops.port_leave)
1037 team->ops.port_leave(team, port);
1038 dev_put(team->dev);
1039 }
1040
1041 #ifdef CONFIG_NET_POLL_CONTROLLER
team_port_enable_netpoll(struct team * team,struct team_port * port)1042 static int team_port_enable_netpoll(struct team *team, struct team_port *port)
1043 {
1044 struct netpoll *np;
1045 int err;
1046
1047 if (!team->dev->npinfo)
1048 return 0;
1049
1050 np = kzalloc(sizeof(*np), GFP_KERNEL);
1051 if (!np)
1052 return -ENOMEM;
1053
1054 err = __netpoll_setup(np, port->dev);
1055 if (err) {
1056 kfree(np);
1057 return err;
1058 }
1059 port->np = np;
1060 return err;
1061 }
1062
team_port_disable_netpoll(struct team_port * port)1063 static void team_port_disable_netpoll(struct team_port *port)
1064 {
1065 struct netpoll *np = port->np;
1066
1067 if (!np)
1068 return;
1069 port->np = NULL;
1070
1071 /* Wait for transmitting packets to finish before freeing. */
1072 synchronize_rcu_bh();
1073 __netpoll_cleanup(np);
1074 kfree(np);
1075 }
1076 #else
team_port_enable_netpoll(struct team * team,struct team_port * port)1077 static int team_port_enable_netpoll(struct team *team, struct team_port *port)
1078 {
1079 return 0;
1080 }
team_port_disable_netpoll(struct team_port * port)1081 static void team_port_disable_netpoll(struct team_port *port)
1082 {
1083 }
1084 #endif
1085
team_upper_dev_link(struct net_device * dev,struct net_device * port_dev)1086 static int team_upper_dev_link(struct net_device *dev,
1087 struct net_device *port_dev)
1088 {
1089 int err;
1090
1091 err = netdev_master_upper_dev_link(port_dev, dev);
1092 if (err)
1093 return err;
1094 port_dev->priv_flags |= IFF_TEAM_PORT;
1095 return 0;
1096 }
1097
team_upper_dev_unlink(struct net_device * dev,struct net_device * port_dev)1098 static void team_upper_dev_unlink(struct net_device *dev,
1099 struct net_device *port_dev)
1100 {
1101 netdev_upper_dev_unlink(port_dev, dev);
1102 port_dev->priv_flags &= ~IFF_TEAM_PORT;
1103 }
1104
1105 static void __team_port_change_port_added(struct team_port *port, bool linkup);
1106 static int team_dev_type_check_change(struct net_device *dev,
1107 struct net_device *port_dev);
1108
team_port_add(struct team * team,struct net_device * port_dev)1109 static int team_port_add(struct team *team, struct net_device *port_dev)
1110 {
1111 struct net_device *dev = team->dev;
1112 struct team_port *port;
1113 char *portname = port_dev->name;
1114 int err;
1115
1116 if (port_dev->flags & IFF_LOOPBACK) {
1117 netdev_err(dev, "Device %s is loopback device. Loopback devices can't be added as a team port\n",
1118 portname);
1119 return -EINVAL;
1120 }
1121
1122 if (team_port_exists(port_dev)) {
1123 netdev_err(dev, "Device %s is already a port "
1124 "of a team device\n", portname);
1125 return -EBUSY;
1126 }
1127
1128 if (port_dev->features & NETIF_F_VLAN_CHALLENGED &&
1129 vlan_uses_dev(dev)) {
1130 netdev_err(dev, "Device %s is VLAN challenged and team device has VLAN set up\n",
1131 portname);
1132 return -EPERM;
1133 }
1134
1135 err = team_dev_type_check_change(dev, port_dev);
1136 if (err)
1137 return err;
1138
1139 if (port_dev->flags & IFF_UP) {
1140 netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
1141 portname);
1142 return -EBUSY;
1143 }
1144
1145 port = kzalloc(sizeof(struct team_port) + team->mode->port_priv_size,
1146 GFP_KERNEL);
1147 if (!port)
1148 return -ENOMEM;
1149
1150 port->dev = port_dev;
1151 port->team = team;
1152 INIT_LIST_HEAD(&port->qom_list);
1153
1154 port->orig.mtu = port_dev->mtu;
1155 err = dev_set_mtu(port_dev, dev->mtu);
1156 if (err) {
1157 netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
1158 goto err_set_mtu;
1159 }
1160
1161 memcpy(port->orig.dev_addr, port_dev->dev_addr, port_dev->addr_len);
1162
1163 err = team_port_enter(team, port);
1164 if (err) {
1165 netdev_err(dev, "Device %s failed to enter team mode\n",
1166 portname);
1167 goto err_port_enter;
1168 }
1169
1170 err = dev_open(port_dev);
1171 if (err) {
1172 netdev_dbg(dev, "Device %s opening failed\n",
1173 portname);
1174 goto err_dev_open;
1175 }
1176
1177 err = vlan_vids_add_by_dev(port_dev, dev);
1178 if (err) {
1179 netdev_err(dev, "Failed to add vlan ids to device %s\n",
1180 portname);
1181 goto err_vids_add;
1182 }
1183
1184 err = team_port_enable_netpoll(team, port);
1185 if (err) {
1186 netdev_err(dev, "Failed to enable netpoll on device %s\n",
1187 portname);
1188 goto err_enable_netpoll;
1189 }
1190
1191 if (!(dev->features & NETIF_F_LRO))
1192 dev_disable_lro(port_dev);
1193
1194 err = netdev_rx_handler_register(port_dev, team_handle_frame,
1195 port);
1196 if (err) {
1197 netdev_err(dev, "Device %s failed to register rx_handler\n",
1198 portname);
1199 goto err_handler_register;
1200 }
1201
1202 err = team_upper_dev_link(dev, port_dev);
1203 if (err) {
1204 netdev_err(dev, "Device %s failed to set upper link\n",
1205 portname);
1206 goto err_set_upper_link;
1207 }
1208
1209 err = __team_option_inst_add_port(team, port);
1210 if (err) {
1211 netdev_err(dev, "Device %s failed to add per-port options\n",
1212 portname);
1213 goto err_option_port_add;
1214 }
1215
1216 port->index = -1;
1217 list_add_tail_rcu(&port->list, &team->port_list);
1218 team_port_enable(team, port);
1219 __team_compute_features(team);
1220 __team_port_change_port_added(port, !!netif_carrier_ok(port_dev));
1221 __team_options_change_check(team);
1222
1223 netdev_info(dev, "Port device %s added\n", portname);
1224
1225 return 0;
1226
1227 err_option_port_add:
1228 team_upper_dev_unlink(dev, port_dev);
1229
1230 err_set_upper_link:
1231 netdev_rx_handler_unregister(port_dev);
1232
1233 err_handler_register:
1234 team_port_disable_netpoll(port);
1235
1236 err_enable_netpoll:
1237 vlan_vids_del_by_dev(port_dev, dev);
1238
1239 err_vids_add:
1240 dev_close(port_dev);
1241
1242 err_dev_open:
1243 team_port_leave(team, port);
1244 team_port_set_orig_dev_addr(port);
1245
1246 err_port_enter:
1247 dev_set_mtu(port_dev, port->orig.mtu);
1248
1249 err_set_mtu:
1250 kfree(port);
1251
1252 return err;
1253 }
1254
1255 static void __team_port_change_port_removed(struct team_port *port);
1256
team_port_del(struct team * team,struct net_device * port_dev)1257 static int team_port_del(struct team *team, struct net_device *port_dev)
1258 {
1259 struct net_device *dev = team->dev;
1260 struct team_port *port;
1261 char *portname = port_dev->name;
1262
1263 port = team_port_get_rtnl(port_dev);
1264 if (!port || !team_port_find(team, port)) {
1265 netdev_err(dev, "Device %s does not act as a port of this team\n",
1266 portname);
1267 return -ENOENT;
1268 }
1269
1270 team_port_disable(team, port);
1271 list_del_rcu(&port->list);
1272 team_upper_dev_unlink(dev, port_dev);
1273 netdev_rx_handler_unregister(port_dev);
1274 team_port_disable_netpoll(port);
1275 vlan_vids_del_by_dev(port_dev, dev);
1276 dev_uc_unsync(port_dev, dev);
1277 dev_mc_unsync(port_dev, dev);
1278 dev_close(port_dev);
1279 team_port_leave(team, port);
1280
1281 __team_option_inst_mark_removed_port(team, port);
1282 __team_options_change_check(team);
1283 __team_option_inst_del_port(team, port);
1284 __team_port_change_port_removed(port);
1285
1286 team_port_set_orig_dev_addr(port);
1287 dev_set_mtu(port_dev, port->orig.mtu);
1288 kfree_rcu(port, rcu);
1289 netdev_info(dev, "Port device %s removed\n", portname);
1290 __team_compute_features(team);
1291
1292 return 0;
1293 }
1294
1295
1296 /*****************
1297 * Net device ops
1298 *****************/
1299
team_mode_option_get(struct team * team,struct team_gsetter_ctx * ctx)1300 static int team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx)
1301 {
1302 ctx->data.str_val = team->mode->kind;
1303 return 0;
1304 }
1305
team_mode_option_set(struct team * team,struct team_gsetter_ctx * ctx)1306 static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx)
1307 {
1308 return team_change_mode(team, ctx->data.str_val);
1309 }
1310
team_notify_peers_count_get(struct team * team,struct team_gsetter_ctx * ctx)1311 static int team_notify_peers_count_get(struct team *team,
1312 struct team_gsetter_ctx *ctx)
1313 {
1314 ctx->data.u32_val = team->notify_peers.count;
1315 return 0;
1316 }
1317
team_notify_peers_count_set(struct team * team,struct team_gsetter_ctx * ctx)1318 static int team_notify_peers_count_set(struct team *team,
1319 struct team_gsetter_ctx *ctx)
1320 {
1321 team->notify_peers.count = ctx->data.u32_val;
1322 return 0;
1323 }
1324
team_notify_peers_interval_get(struct team * team,struct team_gsetter_ctx * ctx)1325 static int team_notify_peers_interval_get(struct team *team,
1326 struct team_gsetter_ctx *ctx)
1327 {
1328 ctx->data.u32_val = team->notify_peers.interval;
1329 return 0;
1330 }
1331
team_notify_peers_interval_set(struct team * team,struct team_gsetter_ctx * ctx)1332 static int team_notify_peers_interval_set(struct team *team,
1333 struct team_gsetter_ctx *ctx)
1334 {
1335 team->notify_peers.interval = ctx->data.u32_val;
1336 return 0;
1337 }
1338
team_mcast_rejoin_count_get(struct team * team,struct team_gsetter_ctx * ctx)1339 static int team_mcast_rejoin_count_get(struct team *team,
1340 struct team_gsetter_ctx *ctx)
1341 {
1342 ctx->data.u32_val = team->mcast_rejoin.count;
1343 return 0;
1344 }
1345
team_mcast_rejoin_count_set(struct team * team,struct team_gsetter_ctx * ctx)1346 static int team_mcast_rejoin_count_set(struct team *team,
1347 struct team_gsetter_ctx *ctx)
1348 {
1349 team->mcast_rejoin.count = ctx->data.u32_val;
1350 return 0;
1351 }
1352
team_mcast_rejoin_interval_get(struct team * team,struct team_gsetter_ctx * ctx)1353 static int team_mcast_rejoin_interval_get(struct team *team,
1354 struct team_gsetter_ctx *ctx)
1355 {
1356 ctx->data.u32_val = team->mcast_rejoin.interval;
1357 return 0;
1358 }
1359
team_mcast_rejoin_interval_set(struct team * team,struct team_gsetter_ctx * ctx)1360 static int team_mcast_rejoin_interval_set(struct team *team,
1361 struct team_gsetter_ctx *ctx)
1362 {
1363 team->mcast_rejoin.interval = ctx->data.u32_val;
1364 return 0;
1365 }
1366
team_port_en_option_get(struct team * team,struct team_gsetter_ctx * ctx)1367 static int team_port_en_option_get(struct team *team,
1368 struct team_gsetter_ctx *ctx)
1369 {
1370 struct team_port *port = ctx->info->port;
1371
1372 ctx->data.bool_val = team_port_enabled(port);
1373 return 0;
1374 }
1375
team_port_en_option_set(struct team * team,struct team_gsetter_ctx * ctx)1376 static int team_port_en_option_set(struct team *team,
1377 struct team_gsetter_ctx *ctx)
1378 {
1379 struct team_port *port = ctx->info->port;
1380
1381 if (ctx->data.bool_val)
1382 team_port_enable(team, port);
1383 else
1384 team_port_disable(team, port);
1385 return 0;
1386 }
1387
team_user_linkup_option_get(struct team * team,struct team_gsetter_ctx * ctx)1388 static int team_user_linkup_option_get(struct team *team,
1389 struct team_gsetter_ctx *ctx)
1390 {
1391 struct team_port *port = ctx->info->port;
1392
1393 ctx->data.bool_val = port->user.linkup;
1394 return 0;
1395 }
1396
1397 static void __team_carrier_check(struct team *team);
1398
team_user_linkup_option_set(struct team * team,struct team_gsetter_ctx * ctx)1399 static int team_user_linkup_option_set(struct team *team,
1400 struct team_gsetter_ctx *ctx)
1401 {
1402 struct team_port *port = ctx->info->port;
1403
1404 port->user.linkup = ctx->data.bool_val;
1405 team_refresh_port_linkup(port);
1406 __team_carrier_check(port->team);
1407 return 0;
1408 }
1409
team_user_linkup_en_option_get(struct team * team,struct team_gsetter_ctx * ctx)1410 static int team_user_linkup_en_option_get(struct team *team,
1411 struct team_gsetter_ctx *ctx)
1412 {
1413 struct team_port *port = ctx->info->port;
1414
1415 ctx->data.bool_val = port->user.linkup_enabled;
1416 return 0;
1417 }
1418
team_user_linkup_en_option_set(struct team * team,struct team_gsetter_ctx * ctx)1419 static int team_user_linkup_en_option_set(struct team *team,
1420 struct team_gsetter_ctx *ctx)
1421 {
1422 struct team_port *port = ctx->info->port;
1423
1424 port->user.linkup_enabled = ctx->data.bool_val;
1425 team_refresh_port_linkup(port);
1426 __team_carrier_check(port->team);
1427 return 0;
1428 }
1429
team_priority_option_get(struct team * team,struct team_gsetter_ctx * ctx)1430 static int team_priority_option_get(struct team *team,
1431 struct team_gsetter_ctx *ctx)
1432 {
1433 struct team_port *port = ctx->info->port;
1434
1435 ctx->data.s32_val = port->priority;
1436 return 0;
1437 }
1438
team_priority_option_set(struct team * team,struct team_gsetter_ctx * ctx)1439 static int team_priority_option_set(struct team *team,
1440 struct team_gsetter_ctx *ctx)
1441 {
1442 struct team_port *port = ctx->info->port;
1443 s32 priority = ctx->data.s32_val;
1444
1445 if (port->priority == priority)
1446 return 0;
1447 port->priority = priority;
1448 team_queue_override_port_prio_changed(team, port);
1449 return 0;
1450 }
1451
team_queue_id_option_get(struct team * team,struct team_gsetter_ctx * ctx)1452 static int team_queue_id_option_get(struct team *team,
1453 struct team_gsetter_ctx *ctx)
1454 {
1455 struct team_port *port = ctx->info->port;
1456
1457 ctx->data.u32_val = port->queue_id;
1458 return 0;
1459 }
1460
team_queue_id_option_set(struct team * team,struct team_gsetter_ctx * ctx)1461 static int team_queue_id_option_set(struct team *team,
1462 struct team_gsetter_ctx *ctx)
1463 {
1464 struct team_port *port = ctx->info->port;
1465 u16 new_queue_id = ctx->data.u32_val;
1466
1467 if (port->queue_id == new_queue_id)
1468 return 0;
1469 if (new_queue_id >= team->dev->real_num_tx_queues)
1470 return -EINVAL;
1471 team_queue_override_port_change_queue_id(team, port, new_queue_id);
1472 return 0;
1473 }
1474
1475 static const struct team_option team_options[] = {
1476 {
1477 .name = "mode",
1478 .type = TEAM_OPTION_TYPE_STRING,
1479 .getter = team_mode_option_get,
1480 .setter = team_mode_option_set,
1481 },
1482 {
1483 .name = "notify_peers_count",
1484 .type = TEAM_OPTION_TYPE_U32,
1485 .getter = team_notify_peers_count_get,
1486 .setter = team_notify_peers_count_set,
1487 },
1488 {
1489 .name = "notify_peers_interval",
1490 .type = TEAM_OPTION_TYPE_U32,
1491 .getter = team_notify_peers_interval_get,
1492 .setter = team_notify_peers_interval_set,
1493 },
1494 {
1495 .name = "mcast_rejoin_count",
1496 .type = TEAM_OPTION_TYPE_U32,
1497 .getter = team_mcast_rejoin_count_get,
1498 .setter = team_mcast_rejoin_count_set,
1499 },
1500 {
1501 .name = "mcast_rejoin_interval",
1502 .type = TEAM_OPTION_TYPE_U32,
1503 .getter = team_mcast_rejoin_interval_get,
1504 .setter = team_mcast_rejoin_interval_set,
1505 },
1506 {
1507 .name = "enabled",
1508 .type = TEAM_OPTION_TYPE_BOOL,
1509 .per_port = true,
1510 .getter = team_port_en_option_get,
1511 .setter = team_port_en_option_set,
1512 },
1513 {
1514 .name = "user_linkup",
1515 .type = TEAM_OPTION_TYPE_BOOL,
1516 .per_port = true,
1517 .getter = team_user_linkup_option_get,
1518 .setter = team_user_linkup_option_set,
1519 },
1520 {
1521 .name = "user_linkup_enabled",
1522 .type = TEAM_OPTION_TYPE_BOOL,
1523 .per_port = true,
1524 .getter = team_user_linkup_en_option_get,
1525 .setter = team_user_linkup_en_option_set,
1526 },
1527 {
1528 .name = "priority",
1529 .type = TEAM_OPTION_TYPE_S32,
1530 .per_port = true,
1531 .getter = team_priority_option_get,
1532 .setter = team_priority_option_set,
1533 },
1534 {
1535 .name = "queue_id",
1536 .type = TEAM_OPTION_TYPE_U32,
1537 .per_port = true,
1538 .getter = team_queue_id_option_get,
1539 .setter = team_queue_id_option_set,
1540 },
1541 };
1542
1543 static struct lock_class_key team_netdev_xmit_lock_key;
1544 static struct lock_class_key team_netdev_addr_lock_key;
1545 static struct lock_class_key team_tx_busylock_key;
1546
team_set_lockdep_class_one(struct net_device * dev,struct netdev_queue * txq,void * unused)1547 static void team_set_lockdep_class_one(struct net_device *dev,
1548 struct netdev_queue *txq,
1549 void *unused)
1550 {
1551 lockdep_set_class(&txq->_xmit_lock, &team_netdev_xmit_lock_key);
1552 }
1553
team_set_lockdep_class(struct net_device * dev)1554 static void team_set_lockdep_class(struct net_device *dev)
1555 {
1556 lockdep_set_class(&dev->addr_list_lock, &team_netdev_addr_lock_key);
1557 netdev_for_each_tx_queue(dev, team_set_lockdep_class_one, NULL);
1558 dev->qdisc_tx_busylock = &team_tx_busylock_key;
1559 }
1560
team_init(struct net_device * dev)1561 static int team_init(struct net_device *dev)
1562 {
1563 struct team *team = netdev_priv(dev);
1564 int i;
1565 int err;
1566
1567 team->dev = dev;
1568 mutex_init(&team->lock);
1569 team_set_no_mode(team);
1570
1571 team->pcpu_stats = netdev_alloc_pcpu_stats(struct team_pcpu_stats);
1572 if (!team->pcpu_stats)
1573 return -ENOMEM;
1574
1575 for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
1576 INIT_HLIST_HEAD(&team->en_port_hlist[i]);
1577 INIT_LIST_HEAD(&team->port_list);
1578 err = team_queue_override_init(team);
1579 if (err)
1580 goto err_team_queue_override_init;
1581
1582 team_adjust_ops(team);
1583
1584 INIT_LIST_HEAD(&team->option_list);
1585 INIT_LIST_HEAD(&team->option_inst_list);
1586
1587 team_notify_peers_init(team);
1588 team_mcast_rejoin_init(team);
1589
1590 err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
1591 if (err)
1592 goto err_options_register;
1593 netif_carrier_off(dev);
1594
1595 team_set_lockdep_class(dev);
1596
1597 return 0;
1598
1599 err_options_register:
1600 team_mcast_rejoin_fini(team);
1601 team_notify_peers_fini(team);
1602 team_queue_override_fini(team);
1603 err_team_queue_override_init:
1604 free_percpu(team->pcpu_stats);
1605
1606 return err;
1607 }
1608
team_uninit(struct net_device * dev)1609 static void team_uninit(struct net_device *dev)
1610 {
1611 struct team *team = netdev_priv(dev);
1612 struct team_port *port;
1613 struct team_port *tmp;
1614
1615 mutex_lock(&team->lock);
1616 list_for_each_entry_safe(port, tmp, &team->port_list, list)
1617 team_port_del(team, port->dev);
1618
1619 __team_change_mode(team, NULL); /* cleanup */
1620 __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
1621 team_mcast_rejoin_fini(team);
1622 team_notify_peers_fini(team);
1623 team_queue_override_fini(team);
1624 mutex_unlock(&team->lock);
1625 }
1626
team_destructor(struct net_device * dev)1627 static void team_destructor(struct net_device *dev)
1628 {
1629 struct team *team = netdev_priv(dev);
1630
1631 free_percpu(team->pcpu_stats);
1632 free_netdev(dev);
1633 }
1634
team_open(struct net_device * dev)1635 static int team_open(struct net_device *dev)
1636 {
1637 return 0;
1638 }
1639
team_close(struct net_device * dev)1640 static int team_close(struct net_device *dev)
1641 {
1642 return 0;
1643 }
1644
1645 /*
1646 * note: already called with rcu_read_lock
1647 */
team_xmit(struct sk_buff * skb,struct net_device * dev)1648 static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
1649 {
1650 struct team *team = netdev_priv(dev);
1651 bool tx_success;
1652 unsigned int len = skb->len;
1653
1654 tx_success = team_queue_override_transmit(team, skb);
1655 if (!tx_success)
1656 tx_success = team->ops.transmit(team, skb);
1657 if (tx_success) {
1658 struct team_pcpu_stats *pcpu_stats;
1659
1660 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
1661 u64_stats_update_begin(&pcpu_stats->syncp);
1662 pcpu_stats->tx_packets++;
1663 pcpu_stats->tx_bytes += len;
1664 u64_stats_update_end(&pcpu_stats->syncp);
1665 } else {
1666 this_cpu_inc(team->pcpu_stats->tx_dropped);
1667 }
1668
1669 return NETDEV_TX_OK;
1670 }
1671
team_select_queue(struct net_device * dev,struct sk_buff * skb,void * accel_priv,select_queue_fallback_t fallback)1672 static u16 team_select_queue(struct net_device *dev, struct sk_buff *skb,
1673 void *accel_priv, select_queue_fallback_t fallback)
1674 {
1675 /*
1676 * This helper function exists to help dev_pick_tx get the correct
1677 * destination queue. Using a helper function skips a call to
1678 * skb_tx_hash and will put the skbs in the queue we expect on their
1679 * way down to the team driver.
1680 */
1681 u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
1682
1683 /*
1684 * Save the original txq to restore before passing to the driver
1685 */
1686 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
1687
1688 if (unlikely(txq >= dev->real_num_tx_queues)) {
1689 do {
1690 txq -= dev->real_num_tx_queues;
1691 } while (txq >= dev->real_num_tx_queues);
1692 }
1693 return txq;
1694 }
1695
team_change_rx_flags(struct net_device * dev,int change)1696 static void team_change_rx_flags(struct net_device *dev, int change)
1697 {
1698 struct team *team = netdev_priv(dev);
1699 struct team_port *port;
1700 int inc;
1701
1702 rcu_read_lock();
1703 list_for_each_entry_rcu(port, &team->port_list, list) {
1704 if (change & IFF_PROMISC) {
1705 inc = dev->flags & IFF_PROMISC ? 1 : -1;
1706 dev_set_promiscuity(port->dev, inc);
1707 }
1708 if (change & IFF_ALLMULTI) {
1709 inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
1710 dev_set_allmulti(port->dev, inc);
1711 }
1712 }
1713 rcu_read_unlock();
1714 }
1715
team_set_rx_mode(struct net_device * dev)1716 static void team_set_rx_mode(struct net_device *dev)
1717 {
1718 struct team *team = netdev_priv(dev);
1719 struct team_port *port;
1720
1721 rcu_read_lock();
1722 list_for_each_entry_rcu(port, &team->port_list, list) {
1723 dev_uc_sync_multiple(port->dev, dev);
1724 dev_mc_sync_multiple(port->dev, dev);
1725 }
1726 rcu_read_unlock();
1727 }
1728
team_set_mac_address(struct net_device * dev,void * p)1729 static int team_set_mac_address(struct net_device *dev, void *p)
1730 {
1731 struct sockaddr *addr = p;
1732 struct team *team = netdev_priv(dev);
1733 struct team_port *port;
1734
1735 if (dev->type == ARPHRD_ETHER && !is_valid_ether_addr(addr->sa_data))
1736 return -EADDRNOTAVAIL;
1737 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1738 mutex_lock(&team->lock);
1739 list_for_each_entry(port, &team->port_list, list)
1740 if (team->ops.port_change_dev_addr)
1741 team->ops.port_change_dev_addr(team, port);
1742 mutex_unlock(&team->lock);
1743 return 0;
1744 }
1745
team_change_mtu(struct net_device * dev,int new_mtu)1746 static int team_change_mtu(struct net_device *dev, int new_mtu)
1747 {
1748 struct team *team = netdev_priv(dev);
1749 struct team_port *port;
1750 int err;
1751
1752 /*
1753 * Alhough this is reader, it's guarded by team lock. It's not possible
1754 * to traverse list in reverse under rcu_read_lock
1755 */
1756 mutex_lock(&team->lock);
1757 team->port_mtu_change_allowed = true;
1758 list_for_each_entry(port, &team->port_list, list) {
1759 err = dev_set_mtu(port->dev, new_mtu);
1760 if (err) {
1761 netdev_err(dev, "Device %s failed to change mtu",
1762 port->dev->name);
1763 goto unwind;
1764 }
1765 }
1766 team->port_mtu_change_allowed = false;
1767 mutex_unlock(&team->lock);
1768
1769 dev->mtu = new_mtu;
1770
1771 return 0;
1772
1773 unwind:
1774 list_for_each_entry_continue_reverse(port, &team->port_list, list)
1775 dev_set_mtu(port->dev, dev->mtu);
1776 team->port_mtu_change_allowed = false;
1777 mutex_unlock(&team->lock);
1778
1779 return err;
1780 }
1781
1782 static struct rtnl_link_stats64 *
team_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * stats)1783 team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1784 {
1785 struct team *team = netdev_priv(dev);
1786 struct team_pcpu_stats *p;
1787 u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
1788 u32 rx_dropped = 0, tx_dropped = 0;
1789 unsigned int start;
1790 int i;
1791
1792 for_each_possible_cpu(i) {
1793 p = per_cpu_ptr(team->pcpu_stats, i);
1794 do {
1795 start = u64_stats_fetch_begin_irq(&p->syncp);
1796 rx_packets = p->rx_packets;
1797 rx_bytes = p->rx_bytes;
1798 rx_multicast = p->rx_multicast;
1799 tx_packets = p->tx_packets;
1800 tx_bytes = p->tx_bytes;
1801 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
1802
1803 stats->rx_packets += rx_packets;
1804 stats->rx_bytes += rx_bytes;
1805 stats->multicast += rx_multicast;
1806 stats->tx_packets += tx_packets;
1807 stats->tx_bytes += tx_bytes;
1808 /*
1809 * rx_dropped & tx_dropped are u32, updated
1810 * without syncp protection.
1811 */
1812 rx_dropped += p->rx_dropped;
1813 tx_dropped += p->tx_dropped;
1814 }
1815 stats->rx_dropped = rx_dropped;
1816 stats->tx_dropped = tx_dropped;
1817 return stats;
1818 }
1819
team_vlan_rx_add_vid(struct net_device * dev,__be16 proto,u16 vid)1820 static int team_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
1821 {
1822 struct team *team = netdev_priv(dev);
1823 struct team_port *port;
1824 int err;
1825
1826 /*
1827 * Alhough this is reader, it's guarded by team lock. It's not possible
1828 * to traverse list in reverse under rcu_read_lock
1829 */
1830 mutex_lock(&team->lock);
1831 list_for_each_entry(port, &team->port_list, list) {
1832 err = vlan_vid_add(port->dev, proto, vid);
1833 if (err)
1834 goto unwind;
1835 }
1836 mutex_unlock(&team->lock);
1837
1838 return 0;
1839
1840 unwind:
1841 list_for_each_entry_continue_reverse(port, &team->port_list, list)
1842 vlan_vid_del(port->dev, proto, vid);
1843 mutex_unlock(&team->lock);
1844
1845 return err;
1846 }
1847
team_vlan_rx_kill_vid(struct net_device * dev,__be16 proto,u16 vid)1848 static int team_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
1849 {
1850 struct team *team = netdev_priv(dev);
1851 struct team_port *port;
1852
1853 mutex_lock(&team->lock);
1854 list_for_each_entry(port, &team->port_list, list)
1855 vlan_vid_del(port->dev, proto, vid);
1856 mutex_unlock(&team->lock);
1857
1858 return 0;
1859 }
1860
1861 #ifdef CONFIG_NET_POLL_CONTROLLER
team_poll_controller(struct net_device * dev)1862 static void team_poll_controller(struct net_device *dev)
1863 {
1864 }
1865
__team_netpoll_cleanup(struct team * team)1866 static void __team_netpoll_cleanup(struct team *team)
1867 {
1868 struct team_port *port;
1869
1870 list_for_each_entry(port, &team->port_list, list)
1871 team_port_disable_netpoll(port);
1872 }
1873
team_netpoll_cleanup(struct net_device * dev)1874 static void team_netpoll_cleanup(struct net_device *dev)
1875 {
1876 struct team *team = netdev_priv(dev);
1877
1878 mutex_lock(&team->lock);
1879 __team_netpoll_cleanup(team);
1880 mutex_unlock(&team->lock);
1881 }
1882
team_netpoll_setup(struct net_device * dev,struct netpoll_info * npifo)1883 static int team_netpoll_setup(struct net_device *dev,
1884 struct netpoll_info *npifo)
1885 {
1886 struct team *team = netdev_priv(dev);
1887 struct team_port *port;
1888 int err = 0;
1889
1890 mutex_lock(&team->lock);
1891 list_for_each_entry(port, &team->port_list, list) {
1892 err = team_port_enable_netpoll(team, port);
1893 if (err) {
1894 __team_netpoll_cleanup(team);
1895 break;
1896 }
1897 }
1898 mutex_unlock(&team->lock);
1899 return err;
1900 }
1901 #endif
1902
team_add_slave(struct net_device * dev,struct net_device * port_dev)1903 static int team_add_slave(struct net_device *dev, struct net_device *port_dev)
1904 {
1905 struct team *team = netdev_priv(dev);
1906 int err;
1907
1908 mutex_lock(&team->lock);
1909 err = team_port_add(team, port_dev);
1910 mutex_unlock(&team->lock);
1911 return err;
1912 }
1913
team_del_slave(struct net_device * dev,struct net_device * port_dev)1914 static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
1915 {
1916 struct team *team = netdev_priv(dev);
1917 int err;
1918
1919 mutex_lock(&team->lock);
1920 err = team_port_del(team, port_dev);
1921 mutex_unlock(&team->lock);
1922 return err;
1923 }
1924
team_fix_features(struct net_device * dev,netdev_features_t features)1925 static netdev_features_t team_fix_features(struct net_device *dev,
1926 netdev_features_t features)
1927 {
1928 struct team_port *port;
1929 struct team *team = netdev_priv(dev);
1930 netdev_features_t mask;
1931
1932 mask = features;
1933 features &= ~NETIF_F_ONE_FOR_ALL;
1934 features |= NETIF_F_ALL_FOR_ALL;
1935
1936 rcu_read_lock();
1937 list_for_each_entry_rcu(port, &team->port_list, list) {
1938 features = netdev_increment_features(features,
1939 port->dev->features,
1940 mask);
1941 }
1942 rcu_read_unlock();
1943
1944 features = netdev_add_tso_features(features, mask);
1945
1946 return features;
1947 }
1948
team_change_carrier(struct net_device * dev,bool new_carrier)1949 static int team_change_carrier(struct net_device *dev, bool new_carrier)
1950 {
1951 struct team *team = netdev_priv(dev);
1952
1953 team->user_carrier_enabled = true;
1954
1955 if (new_carrier)
1956 netif_carrier_on(dev);
1957 else
1958 netif_carrier_off(dev);
1959 return 0;
1960 }
1961
1962 static const struct net_device_ops team_netdev_ops = {
1963 .ndo_init = team_init,
1964 .ndo_uninit = team_uninit,
1965 .ndo_open = team_open,
1966 .ndo_stop = team_close,
1967 .ndo_start_xmit = team_xmit,
1968 .ndo_select_queue = team_select_queue,
1969 .ndo_change_rx_flags = team_change_rx_flags,
1970 .ndo_set_rx_mode = team_set_rx_mode,
1971 .ndo_set_mac_address = team_set_mac_address,
1972 .ndo_change_mtu = team_change_mtu,
1973 .ndo_get_stats64 = team_get_stats64,
1974 .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid,
1975 .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid,
1976 #ifdef CONFIG_NET_POLL_CONTROLLER
1977 .ndo_poll_controller = team_poll_controller,
1978 .ndo_netpoll_setup = team_netpoll_setup,
1979 .ndo_netpoll_cleanup = team_netpoll_cleanup,
1980 #endif
1981 .ndo_add_slave = team_add_slave,
1982 .ndo_del_slave = team_del_slave,
1983 .ndo_fix_features = team_fix_features,
1984 .ndo_change_carrier = team_change_carrier,
1985 .ndo_bridge_setlink = switchdev_port_bridge_setlink,
1986 .ndo_bridge_getlink = switchdev_port_bridge_getlink,
1987 .ndo_bridge_dellink = switchdev_port_bridge_dellink,
1988 .ndo_fdb_add = switchdev_port_fdb_add,
1989 .ndo_fdb_del = switchdev_port_fdb_del,
1990 .ndo_fdb_dump = switchdev_port_fdb_dump,
1991 .ndo_features_check = passthru_features_check,
1992 };
1993
1994 /***********************
1995 * ethtool interface
1996 ***********************/
1997
team_ethtool_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * drvinfo)1998 static void team_ethtool_get_drvinfo(struct net_device *dev,
1999 struct ethtool_drvinfo *drvinfo)
2000 {
2001 strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
2002 strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
2003 }
2004
2005 static const struct ethtool_ops team_ethtool_ops = {
2006 .get_drvinfo = team_ethtool_get_drvinfo,
2007 .get_link = ethtool_op_get_link,
2008 };
2009
2010 /***********************
2011 * rt netlink interface
2012 ***********************/
2013
team_setup_by_port(struct net_device * dev,struct net_device * port_dev)2014 static void team_setup_by_port(struct net_device *dev,
2015 struct net_device *port_dev)
2016 {
2017 dev->header_ops = port_dev->header_ops;
2018 dev->type = port_dev->type;
2019 dev->hard_header_len = port_dev->hard_header_len;
2020 dev->addr_len = port_dev->addr_len;
2021 dev->mtu = port_dev->mtu;
2022 memcpy(dev->broadcast, port_dev->broadcast, port_dev->addr_len);
2023 eth_hw_addr_inherit(dev, port_dev);
2024 }
2025
team_dev_type_check_change(struct net_device * dev,struct net_device * port_dev)2026 static int team_dev_type_check_change(struct net_device *dev,
2027 struct net_device *port_dev)
2028 {
2029 struct team *team = netdev_priv(dev);
2030 char *portname = port_dev->name;
2031 int err;
2032
2033 if (dev->type == port_dev->type)
2034 return 0;
2035 if (!list_empty(&team->port_list)) {
2036 netdev_err(dev, "Device %s is of different type\n", portname);
2037 return -EBUSY;
2038 }
2039 err = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE, dev);
2040 err = notifier_to_errno(err);
2041 if (err) {
2042 netdev_err(dev, "Refused to change device type\n");
2043 return err;
2044 }
2045 dev_uc_flush(dev);
2046 dev_mc_flush(dev);
2047 team_setup_by_port(dev, port_dev);
2048 call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
2049 return 0;
2050 }
2051
team_setup(struct net_device * dev)2052 static void team_setup(struct net_device *dev)
2053 {
2054 ether_setup(dev);
2055
2056 dev->netdev_ops = &team_netdev_ops;
2057 dev->ethtool_ops = &team_ethtool_ops;
2058 dev->destructor = team_destructor;
2059 dev->flags |= IFF_MULTICAST;
2060 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
2061 dev->priv_flags |= IFF_NO_QUEUE;
2062
2063 /*
2064 * Indicate we support unicast address filtering. That way core won't
2065 * bring us to promisc mode in case a unicast addr is added.
2066 * Let this up to underlay drivers.
2067 */
2068 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
2069
2070 dev->features |= NETIF_F_LLTX;
2071 dev->features |= NETIF_F_GRO;
2072
2073 /* Don't allow team devices to change network namespaces. */
2074 dev->features |= NETIF_F_NETNS_LOCAL;
2075
2076 dev->hw_features = TEAM_VLAN_FEATURES |
2077 NETIF_F_HW_VLAN_CTAG_TX |
2078 NETIF_F_HW_VLAN_CTAG_RX |
2079 NETIF_F_HW_VLAN_CTAG_FILTER;
2080
2081 dev->hw_features &= ~(NETIF_F_ALL_CSUM & ~NETIF_F_HW_CSUM);
2082 dev->features |= dev->hw_features;
2083 }
2084
team_newlink(struct net * src_net,struct net_device * dev,struct nlattr * tb[],struct nlattr * data[])2085 static int team_newlink(struct net *src_net, struct net_device *dev,
2086 struct nlattr *tb[], struct nlattr *data[])
2087 {
2088 if (tb[IFLA_ADDRESS] == NULL)
2089 eth_hw_addr_random(dev);
2090
2091 return register_netdevice(dev);
2092 }
2093
team_validate(struct nlattr * tb[],struct nlattr * data[])2094 static int team_validate(struct nlattr *tb[], struct nlattr *data[])
2095 {
2096 if (tb[IFLA_ADDRESS]) {
2097 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
2098 return -EINVAL;
2099 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
2100 return -EADDRNOTAVAIL;
2101 }
2102 return 0;
2103 }
2104
team_get_num_tx_queues(void)2105 static unsigned int team_get_num_tx_queues(void)
2106 {
2107 return TEAM_DEFAULT_NUM_TX_QUEUES;
2108 }
2109
team_get_num_rx_queues(void)2110 static unsigned int team_get_num_rx_queues(void)
2111 {
2112 return TEAM_DEFAULT_NUM_RX_QUEUES;
2113 }
2114
2115 static struct rtnl_link_ops team_link_ops __read_mostly = {
2116 .kind = DRV_NAME,
2117 .priv_size = sizeof(struct team),
2118 .setup = team_setup,
2119 .newlink = team_newlink,
2120 .validate = team_validate,
2121 .get_num_tx_queues = team_get_num_tx_queues,
2122 .get_num_rx_queues = team_get_num_rx_queues,
2123 };
2124
2125
2126 /***********************************
2127 * Generic netlink custom interface
2128 ***********************************/
2129
2130 static struct genl_family team_nl_family = {
2131 .id = GENL_ID_GENERATE,
2132 .name = TEAM_GENL_NAME,
2133 .version = TEAM_GENL_VERSION,
2134 .maxattr = TEAM_ATTR_MAX,
2135 .netnsok = true,
2136 };
2137
2138 static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
2139 [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, },
2140 [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 },
2141 [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED },
2142 [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED },
2143 };
2144
2145 static const struct nla_policy
2146 team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
2147 [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
2148 [TEAM_ATTR_OPTION_NAME] = {
2149 .type = NLA_STRING,
2150 .len = TEAM_STRING_MAX_LEN,
2151 },
2152 [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG },
2153 [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 },
2154 [TEAM_ATTR_OPTION_DATA] = { .type = NLA_BINARY },
2155 };
2156
team_nl_cmd_noop(struct sk_buff * skb,struct genl_info * info)2157 static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
2158 {
2159 struct sk_buff *msg;
2160 void *hdr;
2161 int err;
2162
2163 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2164 if (!msg)
2165 return -ENOMEM;
2166
2167 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
2168 &team_nl_family, 0, TEAM_CMD_NOOP);
2169 if (!hdr) {
2170 err = -EMSGSIZE;
2171 goto err_msg_put;
2172 }
2173
2174 genlmsg_end(msg, hdr);
2175
2176 return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
2177
2178 err_msg_put:
2179 nlmsg_free(msg);
2180
2181 return err;
2182 }
2183
2184 /*
2185 * Netlink cmd functions should be locked by following two functions.
2186 * Since dev gets held here, that ensures dev won't disappear in between.
2187 */
team_nl_team_get(struct genl_info * info)2188 static struct team *team_nl_team_get(struct genl_info *info)
2189 {
2190 struct net *net = genl_info_net(info);
2191 int ifindex;
2192 struct net_device *dev;
2193 struct team *team;
2194
2195 if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
2196 return NULL;
2197
2198 ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
2199 dev = dev_get_by_index(net, ifindex);
2200 if (!dev || dev->netdev_ops != &team_netdev_ops) {
2201 if (dev)
2202 dev_put(dev);
2203 return NULL;
2204 }
2205
2206 team = netdev_priv(dev);
2207 mutex_lock(&team->lock);
2208 return team;
2209 }
2210
team_nl_team_put(struct team * team)2211 static void team_nl_team_put(struct team *team)
2212 {
2213 mutex_unlock(&team->lock);
2214 dev_put(team->dev);
2215 }
2216
2217 typedef int team_nl_send_func_t(struct sk_buff *skb,
2218 struct team *team, u32 portid);
2219
team_nl_send_unicast(struct sk_buff * skb,struct team * team,u32 portid)2220 static int team_nl_send_unicast(struct sk_buff *skb, struct team *team, u32 portid)
2221 {
2222 return genlmsg_unicast(dev_net(team->dev), skb, portid);
2223 }
2224
team_nl_fill_one_option_get(struct sk_buff * skb,struct team * team,struct team_option_inst * opt_inst)2225 static int team_nl_fill_one_option_get(struct sk_buff *skb, struct team *team,
2226 struct team_option_inst *opt_inst)
2227 {
2228 struct nlattr *option_item;
2229 struct team_option *option = opt_inst->option;
2230 struct team_option_inst_info *opt_inst_info = &opt_inst->info;
2231 struct team_gsetter_ctx ctx;
2232 int err;
2233
2234 ctx.info = opt_inst_info;
2235 err = team_option_get(team, opt_inst, &ctx);
2236 if (err)
2237 return err;
2238
2239 option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
2240 if (!option_item)
2241 return -EMSGSIZE;
2242
2243 if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name))
2244 goto nest_cancel;
2245 if (opt_inst_info->port &&
2246 nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX,
2247 opt_inst_info->port->dev->ifindex))
2248 goto nest_cancel;
2249 if (opt_inst->option->array_size &&
2250 nla_put_u32(skb, TEAM_ATTR_OPTION_ARRAY_INDEX,
2251 opt_inst_info->array_index))
2252 goto nest_cancel;
2253
2254 switch (option->type) {
2255 case TEAM_OPTION_TYPE_U32:
2256 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32))
2257 goto nest_cancel;
2258 if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.u32_val))
2259 goto nest_cancel;
2260 break;
2261 case TEAM_OPTION_TYPE_STRING:
2262 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING))
2263 goto nest_cancel;
2264 if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA,
2265 ctx.data.str_val))
2266 goto nest_cancel;
2267 break;
2268 case TEAM_OPTION_TYPE_BINARY:
2269 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY))
2270 goto nest_cancel;
2271 if (nla_put(skb, TEAM_ATTR_OPTION_DATA, ctx.data.bin_val.len,
2272 ctx.data.bin_val.ptr))
2273 goto nest_cancel;
2274 break;
2275 case TEAM_OPTION_TYPE_BOOL:
2276 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG))
2277 goto nest_cancel;
2278 if (ctx.data.bool_val &&
2279 nla_put_flag(skb, TEAM_ATTR_OPTION_DATA))
2280 goto nest_cancel;
2281 break;
2282 case TEAM_OPTION_TYPE_S32:
2283 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_S32))
2284 goto nest_cancel;
2285 if (nla_put_s32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.s32_val))
2286 goto nest_cancel;
2287 break;
2288 default:
2289 BUG();
2290 }
2291 if (opt_inst->removed && nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED))
2292 goto nest_cancel;
2293 if (opt_inst->changed) {
2294 if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED))
2295 goto nest_cancel;
2296 opt_inst->changed = false;
2297 }
2298 nla_nest_end(skb, option_item);
2299 return 0;
2300
2301 nest_cancel:
2302 nla_nest_cancel(skb, option_item);
2303 return -EMSGSIZE;
2304 }
2305
__send_and_alloc_skb(struct sk_buff ** pskb,struct team * team,u32 portid,team_nl_send_func_t * send_func)2306 static int __send_and_alloc_skb(struct sk_buff **pskb,
2307 struct team *team, u32 portid,
2308 team_nl_send_func_t *send_func)
2309 {
2310 int err;
2311
2312 if (*pskb) {
2313 err = send_func(*pskb, team, portid);
2314 if (err)
2315 return err;
2316 }
2317 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2318 if (!*pskb)
2319 return -ENOMEM;
2320 return 0;
2321 }
2322
team_nl_send_options_get(struct team * team,u32 portid,u32 seq,int flags,team_nl_send_func_t * send_func,struct list_head * sel_opt_inst_list)2323 static int team_nl_send_options_get(struct team *team, u32 portid, u32 seq,
2324 int flags, team_nl_send_func_t *send_func,
2325 struct list_head *sel_opt_inst_list)
2326 {
2327 struct nlattr *option_list;
2328 struct nlmsghdr *nlh;
2329 void *hdr;
2330 struct team_option_inst *opt_inst;
2331 int err;
2332 struct sk_buff *skb = NULL;
2333 bool incomplete;
2334 int i;
2335
2336 opt_inst = list_first_entry(sel_opt_inst_list,
2337 struct team_option_inst, tmp_list);
2338
2339 start_again:
2340 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2341 if (err)
2342 return err;
2343
2344 hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
2345 TEAM_CMD_OPTIONS_GET);
2346 if (!hdr)
2347 return -EMSGSIZE;
2348
2349 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2350 goto nla_put_failure;
2351 option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
2352 if (!option_list)
2353 goto nla_put_failure;
2354
2355 i = 0;
2356 incomplete = false;
2357 list_for_each_entry_from(opt_inst, sel_opt_inst_list, tmp_list) {
2358 err = team_nl_fill_one_option_get(skb, team, opt_inst);
2359 if (err) {
2360 if (err == -EMSGSIZE) {
2361 if (!i)
2362 goto errout;
2363 incomplete = true;
2364 break;
2365 }
2366 goto errout;
2367 }
2368 i++;
2369 }
2370
2371 nla_nest_end(skb, option_list);
2372 genlmsg_end(skb, hdr);
2373 if (incomplete)
2374 goto start_again;
2375
2376 send_done:
2377 nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
2378 if (!nlh) {
2379 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2380 if (err)
2381 goto errout;
2382 goto send_done;
2383 }
2384
2385 return send_func(skb, team, portid);
2386
2387 nla_put_failure:
2388 err = -EMSGSIZE;
2389 errout:
2390 genlmsg_cancel(skb, hdr);
2391 nlmsg_free(skb);
2392 return err;
2393 }
2394
team_nl_cmd_options_get(struct sk_buff * skb,struct genl_info * info)2395 static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
2396 {
2397 struct team *team;
2398 struct team_option_inst *opt_inst;
2399 int err;
2400 LIST_HEAD(sel_opt_inst_list);
2401
2402 team = team_nl_team_get(info);
2403 if (!team)
2404 return -EINVAL;
2405
2406 list_for_each_entry(opt_inst, &team->option_inst_list, list)
2407 list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
2408 err = team_nl_send_options_get(team, info->snd_portid, info->snd_seq,
2409 NLM_F_ACK, team_nl_send_unicast,
2410 &sel_opt_inst_list);
2411
2412 team_nl_team_put(team);
2413
2414 return err;
2415 }
2416
2417 static int team_nl_send_event_options_get(struct team *team,
2418 struct list_head *sel_opt_inst_list);
2419
team_nl_cmd_options_set(struct sk_buff * skb,struct genl_info * info)2420 static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
2421 {
2422 struct team *team;
2423 int err = 0;
2424 int i;
2425 struct nlattr *nl_option;
2426 LIST_HEAD(opt_inst_list);
2427
2428 team = team_nl_team_get(info);
2429 if (!team)
2430 return -EINVAL;
2431
2432 err = -EINVAL;
2433 if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
2434 err = -EINVAL;
2435 goto team_put;
2436 }
2437
2438 nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
2439 struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1];
2440 struct nlattr *attr;
2441 struct nlattr *attr_data;
2442 enum team_option_type opt_type;
2443 int opt_port_ifindex = 0; /* != 0 for per-port options */
2444 u32 opt_array_index = 0;
2445 bool opt_is_array = false;
2446 struct team_option_inst *opt_inst;
2447 char *opt_name;
2448 bool opt_found = false;
2449
2450 if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
2451 err = -EINVAL;
2452 goto team_put;
2453 }
2454 err = nla_parse_nested(opt_attrs, TEAM_ATTR_OPTION_MAX,
2455 nl_option, team_nl_option_policy);
2456 if (err)
2457 goto team_put;
2458 if (!opt_attrs[TEAM_ATTR_OPTION_NAME] ||
2459 !opt_attrs[TEAM_ATTR_OPTION_TYPE]) {
2460 err = -EINVAL;
2461 goto team_put;
2462 }
2463 switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) {
2464 case NLA_U32:
2465 opt_type = TEAM_OPTION_TYPE_U32;
2466 break;
2467 case NLA_STRING:
2468 opt_type = TEAM_OPTION_TYPE_STRING;
2469 break;
2470 case NLA_BINARY:
2471 opt_type = TEAM_OPTION_TYPE_BINARY;
2472 break;
2473 case NLA_FLAG:
2474 opt_type = TEAM_OPTION_TYPE_BOOL;
2475 break;
2476 case NLA_S32:
2477 opt_type = TEAM_OPTION_TYPE_S32;
2478 break;
2479 default:
2480 goto team_put;
2481 }
2482
2483 attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA];
2484 if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) {
2485 err = -EINVAL;
2486 goto team_put;
2487 }
2488
2489 opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]);
2490 attr = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX];
2491 if (attr)
2492 opt_port_ifindex = nla_get_u32(attr);
2493
2494 attr = opt_attrs[TEAM_ATTR_OPTION_ARRAY_INDEX];
2495 if (attr) {
2496 opt_is_array = true;
2497 opt_array_index = nla_get_u32(attr);
2498 }
2499
2500 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2501 struct team_option *option = opt_inst->option;
2502 struct team_gsetter_ctx ctx;
2503 struct team_option_inst_info *opt_inst_info;
2504 int tmp_ifindex;
2505
2506 opt_inst_info = &opt_inst->info;
2507 tmp_ifindex = opt_inst_info->port ?
2508 opt_inst_info->port->dev->ifindex : 0;
2509 if (option->type != opt_type ||
2510 strcmp(option->name, opt_name) ||
2511 tmp_ifindex != opt_port_ifindex ||
2512 (option->array_size && !opt_is_array) ||
2513 opt_inst_info->array_index != opt_array_index)
2514 continue;
2515 opt_found = true;
2516 ctx.info = opt_inst_info;
2517 switch (opt_type) {
2518 case TEAM_OPTION_TYPE_U32:
2519 ctx.data.u32_val = nla_get_u32(attr_data);
2520 break;
2521 case TEAM_OPTION_TYPE_STRING:
2522 if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) {
2523 err = -EINVAL;
2524 goto team_put;
2525 }
2526 ctx.data.str_val = nla_data(attr_data);
2527 break;
2528 case TEAM_OPTION_TYPE_BINARY:
2529 ctx.data.bin_val.len = nla_len(attr_data);
2530 ctx.data.bin_val.ptr = nla_data(attr_data);
2531 break;
2532 case TEAM_OPTION_TYPE_BOOL:
2533 ctx.data.bool_val = attr_data ? true : false;
2534 break;
2535 case TEAM_OPTION_TYPE_S32:
2536 ctx.data.s32_val = nla_get_s32(attr_data);
2537 break;
2538 default:
2539 BUG();
2540 }
2541 err = team_option_set(team, opt_inst, &ctx);
2542 if (err)
2543 goto team_put;
2544 opt_inst->changed = true;
2545 list_add(&opt_inst->tmp_list, &opt_inst_list);
2546 }
2547 if (!opt_found) {
2548 err = -ENOENT;
2549 goto team_put;
2550 }
2551 }
2552
2553 err = team_nl_send_event_options_get(team, &opt_inst_list);
2554
2555 team_put:
2556 team_nl_team_put(team);
2557
2558 return err;
2559 }
2560
team_nl_fill_one_port_get(struct sk_buff * skb,struct team_port * port)2561 static int team_nl_fill_one_port_get(struct sk_buff *skb,
2562 struct team_port *port)
2563 {
2564 struct nlattr *port_item;
2565
2566 port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
2567 if (!port_item)
2568 goto nest_cancel;
2569 if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex))
2570 goto nest_cancel;
2571 if (port->changed) {
2572 if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED))
2573 goto nest_cancel;
2574 port->changed = false;
2575 }
2576 if ((port->removed &&
2577 nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) ||
2578 (port->state.linkup &&
2579 nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) ||
2580 nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) ||
2581 nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex))
2582 goto nest_cancel;
2583 nla_nest_end(skb, port_item);
2584 return 0;
2585
2586 nest_cancel:
2587 nla_nest_cancel(skb, port_item);
2588 return -EMSGSIZE;
2589 }
2590
team_nl_send_port_list_get(struct team * team,u32 portid,u32 seq,int flags,team_nl_send_func_t * send_func,struct team_port * one_port)2591 static int team_nl_send_port_list_get(struct team *team, u32 portid, u32 seq,
2592 int flags, team_nl_send_func_t *send_func,
2593 struct team_port *one_port)
2594 {
2595 struct nlattr *port_list;
2596 struct nlmsghdr *nlh;
2597 void *hdr;
2598 struct team_port *port;
2599 int err;
2600 struct sk_buff *skb = NULL;
2601 bool incomplete;
2602 int i;
2603
2604 port = list_first_entry_or_null(&team->port_list,
2605 struct team_port, list);
2606
2607 start_again:
2608 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2609 if (err)
2610 return err;
2611
2612 hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
2613 TEAM_CMD_PORT_LIST_GET);
2614 if (!hdr)
2615 return -EMSGSIZE;
2616
2617 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2618 goto nla_put_failure;
2619 port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
2620 if (!port_list)
2621 goto nla_put_failure;
2622
2623 i = 0;
2624 incomplete = false;
2625
2626 /* If one port is selected, called wants to send port list containing
2627 * only this port. Otherwise go through all listed ports and send all
2628 */
2629 if (one_port) {
2630 err = team_nl_fill_one_port_get(skb, one_port);
2631 if (err)
2632 goto errout;
2633 } else if (port) {
2634 list_for_each_entry_from(port, &team->port_list, list) {
2635 err = team_nl_fill_one_port_get(skb, port);
2636 if (err) {
2637 if (err == -EMSGSIZE) {
2638 if (!i)
2639 goto errout;
2640 incomplete = true;
2641 break;
2642 }
2643 goto errout;
2644 }
2645 i++;
2646 }
2647 }
2648
2649 nla_nest_end(skb, port_list);
2650 genlmsg_end(skb, hdr);
2651 if (incomplete)
2652 goto start_again;
2653
2654 send_done:
2655 nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
2656 if (!nlh) {
2657 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2658 if (err)
2659 goto errout;
2660 goto send_done;
2661 }
2662
2663 return send_func(skb, team, portid);
2664
2665 nla_put_failure:
2666 err = -EMSGSIZE;
2667 errout:
2668 genlmsg_cancel(skb, hdr);
2669 nlmsg_free(skb);
2670 return err;
2671 }
2672
team_nl_cmd_port_list_get(struct sk_buff * skb,struct genl_info * info)2673 static int team_nl_cmd_port_list_get(struct sk_buff *skb,
2674 struct genl_info *info)
2675 {
2676 struct team *team;
2677 int err;
2678
2679 team = team_nl_team_get(info);
2680 if (!team)
2681 return -EINVAL;
2682
2683 err = team_nl_send_port_list_get(team, info->snd_portid, info->snd_seq,
2684 NLM_F_ACK, team_nl_send_unicast, NULL);
2685
2686 team_nl_team_put(team);
2687
2688 return err;
2689 }
2690
2691 static const struct genl_ops team_nl_ops[] = {
2692 {
2693 .cmd = TEAM_CMD_NOOP,
2694 .doit = team_nl_cmd_noop,
2695 .policy = team_nl_policy,
2696 },
2697 {
2698 .cmd = TEAM_CMD_OPTIONS_SET,
2699 .doit = team_nl_cmd_options_set,
2700 .policy = team_nl_policy,
2701 .flags = GENL_ADMIN_PERM,
2702 },
2703 {
2704 .cmd = TEAM_CMD_OPTIONS_GET,
2705 .doit = team_nl_cmd_options_get,
2706 .policy = team_nl_policy,
2707 .flags = GENL_ADMIN_PERM,
2708 },
2709 {
2710 .cmd = TEAM_CMD_PORT_LIST_GET,
2711 .doit = team_nl_cmd_port_list_get,
2712 .policy = team_nl_policy,
2713 .flags = GENL_ADMIN_PERM,
2714 },
2715 };
2716
2717 static const struct genl_multicast_group team_nl_mcgrps[] = {
2718 { .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME, },
2719 };
2720
team_nl_send_multicast(struct sk_buff * skb,struct team * team,u32 portid)2721 static int team_nl_send_multicast(struct sk_buff *skb,
2722 struct team *team, u32 portid)
2723 {
2724 return genlmsg_multicast_netns(&team_nl_family, dev_net(team->dev),
2725 skb, 0, 0, GFP_KERNEL);
2726 }
2727
team_nl_send_event_options_get(struct team * team,struct list_head * sel_opt_inst_list)2728 static int team_nl_send_event_options_get(struct team *team,
2729 struct list_head *sel_opt_inst_list)
2730 {
2731 return team_nl_send_options_get(team, 0, 0, 0, team_nl_send_multicast,
2732 sel_opt_inst_list);
2733 }
2734
team_nl_send_event_port_get(struct team * team,struct team_port * port)2735 static int team_nl_send_event_port_get(struct team *team,
2736 struct team_port *port)
2737 {
2738 return team_nl_send_port_list_get(team, 0, 0, 0, team_nl_send_multicast,
2739 port);
2740 }
2741
team_nl_init(void)2742 static int team_nl_init(void)
2743 {
2744 return genl_register_family_with_ops_groups(&team_nl_family, team_nl_ops,
2745 team_nl_mcgrps);
2746 }
2747
team_nl_fini(void)2748 static void team_nl_fini(void)
2749 {
2750 genl_unregister_family(&team_nl_family);
2751 }
2752
2753
2754 /******************
2755 * Change checkers
2756 ******************/
2757
__team_options_change_check(struct team * team)2758 static void __team_options_change_check(struct team *team)
2759 {
2760 int err;
2761 struct team_option_inst *opt_inst;
2762 LIST_HEAD(sel_opt_inst_list);
2763
2764 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2765 if (opt_inst->changed)
2766 list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
2767 }
2768 err = team_nl_send_event_options_get(team, &sel_opt_inst_list);
2769 if (err && err != -ESRCH)
2770 netdev_warn(team->dev, "Failed to send options change via netlink (err %d)\n",
2771 err);
2772 }
2773
2774 /* rtnl lock is held */
2775
__team_port_change_send(struct team_port * port,bool linkup)2776 static void __team_port_change_send(struct team_port *port, bool linkup)
2777 {
2778 int err;
2779
2780 port->changed = true;
2781 port->state.linkup = linkup;
2782 team_refresh_port_linkup(port);
2783 if (linkup) {
2784 struct ethtool_cmd ecmd;
2785
2786 err = __ethtool_get_settings(port->dev, &ecmd);
2787 if (!err) {
2788 port->state.speed = ethtool_cmd_speed(&ecmd);
2789 port->state.duplex = ecmd.duplex;
2790 goto send_event;
2791 }
2792 }
2793 port->state.speed = 0;
2794 port->state.duplex = 0;
2795
2796 send_event:
2797 err = team_nl_send_event_port_get(port->team, port);
2798 if (err && err != -ESRCH)
2799 netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink (err %d)\n",
2800 port->dev->name, err);
2801
2802 }
2803
__team_carrier_check(struct team * team)2804 static void __team_carrier_check(struct team *team)
2805 {
2806 struct team_port *port;
2807 bool team_linkup;
2808
2809 if (team->user_carrier_enabled)
2810 return;
2811
2812 team_linkup = false;
2813 list_for_each_entry(port, &team->port_list, list) {
2814 if (port->linkup) {
2815 team_linkup = true;
2816 break;
2817 }
2818 }
2819
2820 if (team_linkup)
2821 netif_carrier_on(team->dev);
2822 else
2823 netif_carrier_off(team->dev);
2824 }
2825
__team_port_change_check(struct team_port * port,bool linkup)2826 static void __team_port_change_check(struct team_port *port, bool linkup)
2827 {
2828 if (port->state.linkup != linkup)
2829 __team_port_change_send(port, linkup);
2830 __team_carrier_check(port->team);
2831 }
2832
__team_port_change_port_added(struct team_port * port,bool linkup)2833 static void __team_port_change_port_added(struct team_port *port, bool linkup)
2834 {
2835 __team_port_change_send(port, linkup);
2836 __team_carrier_check(port->team);
2837 }
2838
__team_port_change_port_removed(struct team_port * port)2839 static void __team_port_change_port_removed(struct team_port *port)
2840 {
2841 port->removed = true;
2842 __team_port_change_send(port, false);
2843 __team_carrier_check(port->team);
2844 }
2845
team_port_change_check(struct team_port * port,bool linkup)2846 static void team_port_change_check(struct team_port *port, bool linkup)
2847 {
2848 struct team *team = port->team;
2849
2850 mutex_lock(&team->lock);
2851 __team_port_change_check(port, linkup);
2852 mutex_unlock(&team->lock);
2853 }
2854
2855
2856 /************************************
2857 * Net device notifier event handler
2858 ************************************/
2859
team_device_event(struct notifier_block * unused,unsigned long event,void * ptr)2860 static int team_device_event(struct notifier_block *unused,
2861 unsigned long event, void *ptr)
2862 {
2863 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2864 struct team_port *port;
2865
2866 port = team_port_get_rtnl(dev);
2867 if (!port)
2868 return NOTIFY_DONE;
2869
2870 switch (event) {
2871 case NETDEV_UP:
2872 if (netif_carrier_ok(dev))
2873 team_port_change_check(port, true);
2874 break;
2875 case NETDEV_DOWN:
2876 team_port_change_check(port, false);
2877 break;
2878 case NETDEV_CHANGE:
2879 if (netif_running(port->dev))
2880 team_port_change_check(port,
2881 !!netif_carrier_ok(port->dev));
2882 break;
2883 case NETDEV_UNREGISTER:
2884 team_del_slave(port->team->dev, dev);
2885 break;
2886 case NETDEV_FEAT_CHANGE:
2887 team_compute_features(port->team);
2888 break;
2889 case NETDEV_PRECHANGEMTU:
2890 /* Forbid to change mtu of underlaying device */
2891 if (!port->team->port_mtu_change_allowed)
2892 return NOTIFY_BAD;
2893 break;
2894 case NETDEV_PRE_TYPE_CHANGE:
2895 /* Forbid to change type of underlaying device */
2896 return NOTIFY_BAD;
2897 case NETDEV_RESEND_IGMP:
2898 /* Propagate to master device */
2899 call_netdevice_notifiers(event, port->team->dev);
2900 break;
2901 }
2902 return NOTIFY_DONE;
2903 }
2904
2905 static struct notifier_block team_notifier_block __read_mostly = {
2906 .notifier_call = team_device_event,
2907 };
2908
2909
2910 /***********************
2911 * Module init and exit
2912 ***********************/
2913
team_module_init(void)2914 static int __init team_module_init(void)
2915 {
2916 int err;
2917
2918 register_netdevice_notifier(&team_notifier_block);
2919
2920 err = rtnl_link_register(&team_link_ops);
2921 if (err)
2922 goto err_rtnl_reg;
2923
2924 err = team_nl_init();
2925 if (err)
2926 goto err_nl_init;
2927
2928 return 0;
2929
2930 err_nl_init:
2931 rtnl_link_unregister(&team_link_ops);
2932
2933 err_rtnl_reg:
2934 unregister_netdevice_notifier(&team_notifier_block);
2935
2936 return err;
2937 }
2938
team_module_exit(void)2939 static void __exit team_module_exit(void)
2940 {
2941 team_nl_fini();
2942 rtnl_link_unregister(&team_link_ops);
2943 unregister_netdevice_notifier(&team_notifier_block);
2944 }
2945
2946 module_init(team_module_init);
2947 module_exit(team_module_exit);
2948
2949 MODULE_LICENSE("GPL v2");
2950 MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
2951 MODULE_DESCRIPTION("Ethernet team device driver");
2952 MODULE_ALIAS_RTNL_LINK(DRV_NAME);
2953