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