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