1 /* 2 * net/drivers/team/team.c - Network team device driver 3 * Copyright (c) 2011 Jiri Pirko <jpirko@redhat.com> 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 2 of the License, or 8 * (at your option) any later version. 9 */ 10 11 #include <linux/kernel.h> 12 #include <linux/types.h> 13 #include <linux/module.h> 14 #include <linux/init.h> 15 #include <linux/slab.h> 16 #include <linux/rcupdate.h> 17 #include <linux/errno.h> 18 #include <linux/ctype.h> 19 #include <linux/notifier.h> 20 #include <linux/netdevice.h> 21 #include <linux/if_arp.h> 22 #include <linux/socket.h> 23 #include <linux/etherdevice.h> 24 #include <linux/rtnetlink.h> 25 #include <net/rtnetlink.h> 26 #include <net/genetlink.h> 27 #include <net/netlink.h> 28 #include <linux/if_team.h> 29 30 #define DRV_NAME "team" 31 32 33 /********** 34 * Helpers 35 **********/ 36 37 #define team_port_exists(dev) (dev->priv_flags & IFF_TEAM_PORT) 38 39 static struct team_port *team_port_get_rcu(const struct net_device *dev) 40 { 41 struct team_port *port = rcu_dereference(dev->rx_handler_data); 42 43 return team_port_exists(dev) ? port : NULL; 44 } 45 46 static struct team_port *team_port_get_rtnl(const struct net_device *dev) 47 { 48 struct team_port *port = rtnl_dereference(dev->rx_handler_data); 49 50 return team_port_exists(dev) ? port : NULL; 51 } 52 53 /* 54 * Since the ability to change mac address for open port device is tested in 55 * team_port_add, this function can be called without control of return value 56 */ 57 static int __set_port_mac(struct net_device *port_dev, 58 const unsigned char *dev_addr) 59 { 60 struct sockaddr addr; 61 62 memcpy(addr.sa_data, dev_addr, ETH_ALEN); 63 addr.sa_family = ARPHRD_ETHER; 64 return dev_set_mac_address(port_dev, &addr); 65 } 66 67 int team_port_set_orig_mac(struct team_port *port) 68 { 69 return __set_port_mac(port->dev, port->orig.dev_addr); 70 } 71 72 int team_port_set_team_mac(struct team_port *port) 73 { 74 return __set_port_mac(port->dev, port->team->dev->dev_addr); 75 } 76 EXPORT_SYMBOL(team_port_set_team_mac); 77 78 79 /******************* 80 * Options handling 81 *******************/ 82 83 struct team_option *__team_find_option(struct team *team, const char *opt_name) 84 { 85 struct team_option *option; 86 87 list_for_each_entry(option, &team->option_list, list) { 88 if (strcmp(option->name, opt_name) == 0) 89 return option; 90 } 91 return NULL; 92 } 93 94 int team_options_register(struct team *team, 95 const struct team_option *option, 96 size_t option_count) 97 { 98 int i; 99 struct team_option **dst_opts; 100 int err; 101 102 dst_opts = kzalloc(sizeof(struct team_option *) * option_count, 103 GFP_KERNEL); 104 if (!dst_opts) 105 return -ENOMEM; 106 for (i = 0; i < option_count; i++, option++) { 107 if (__team_find_option(team, option->name)) { 108 err = -EEXIST; 109 goto rollback; 110 } 111 dst_opts[i] = kmemdup(option, sizeof(*option), GFP_KERNEL); 112 if (!dst_opts[i]) { 113 err = -ENOMEM; 114 goto rollback; 115 } 116 } 117 118 for (i = 0; i < option_count; i++) 119 list_add_tail(&dst_opts[i]->list, &team->option_list); 120 121 kfree(dst_opts); 122 return 0; 123 124 rollback: 125 for (i = 0; i < option_count; i++) 126 kfree(dst_opts[i]); 127 128 kfree(dst_opts); 129 return err; 130 } 131 132 EXPORT_SYMBOL(team_options_register); 133 134 static void __team_options_change_check(struct team *team, 135 struct team_option *changed_option); 136 137 static void __team_options_unregister(struct team *team, 138 const struct team_option *option, 139 size_t option_count) 140 { 141 int i; 142 143 for (i = 0; i < option_count; i++, option++) { 144 struct team_option *del_opt; 145 146 del_opt = __team_find_option(team, option->name); 147 if (del_opt) { 148 list_del(&del_opt->list); 149 kfree(del_opt); 150 } 151 } 152 } 153 154 void team_options_unregister(struct team *team, 155 const struct team_option *option, 156 size_t option_count) 157 { 158 __team_options_unregister(team, option, option_count); 159 __team_options_change_check(team, NULL); 160 } 161 EXPORT_SYMBOL(team_options_unregister); 162 163 static int team_option_get(struct team *team, struct team_option *option, 164 void *arg) 165 { 166 return option->getter(team, arg); 167 } 168 169 static int team_option_set(struct team *team, struct team_option *option, 170 void *arg) 171 { 172 int err; 173 174 err = option->setter(team, arg); 175 if (err) 176 return err; 177 178 __team_options_change_check(team, option); 179 return err; 180 } 181 182 /**************** 183 * Mode handling 184 ****************/ 185 186 static LIST_HEAD(mode_list); 187 static DEFINE_SPINLOCK(mode_list_lock); 188 189 static struct team_mode *__find_mode(const char *kind) 190 { 191 struct team_mode *mode; 192 193 list_for_each_entry(mode, &mode_list, list) { 194 if (strcmp(mode->kind, kind) == 0) 195 return mode; 196 } 197 return NULL; 198 } 199 200 static bool is_good_mode_name(const char *name) 201 { 202 while (*name != '\0') { 203 if (!isalpha(*name) && !isdigit(*name) && *name != '_') 204 return false; 205 name++; 206 } 207 return true; 208 } 209 210 int team_mode_register(struct team_mode *mode) 211 { 212 int err = 0; 213 214 if (!is_good_mode_name(mode->kind) || 215 mode->priv_size > TEAM_MODE_PRIV_SIZE) 216 return -EINVAL; 217 spin_lock(&mode_list_lock); 218 if (__find_mode(mode->kind)) { 219 err = -EEXIST; 220 goto unlock; 221 } 222 list_add_tail(&mode->list, &mode_list); 223 unlock: 224 spin_unlock(&mode_list_lock); 225 return err; 226 } 227 EXPORT_SYMBOL(team_mode_register); 228 229 int team_mode_unregister(struct team_mode *mode) 230 { 231 spin_lock(&mode_list_lock); 232 list_del_init(&mode->list); 233 spin_unlock(&mode_list_lock); 234 return 0; 235 } 236 EXPORT_SYMBOL(team_mode_unregister); 237 238 static struct team_mode *team_mode_get(const char *kind) 239 { 240 struct team_mode *mode; 241 242 spin_lock(&mode_list_lock); 243 mode = __find_mode(kind); 244 if (!mode) { 245 spin_unlock(&mode_list_lock); 246 request_module("team-mode-%s", kind); 247 spin_lock(&mode_list_lock); 248 mode = __find_mode(kind); 249 } 250 if (mode) 251 if (!try_module_get(mode->owner)) 252 mode = NULL; 253 254 spin_unlock(&mode_list_lock); 255 return mode; 256 } 257 258 static void team_mode_put(const struct team_mode *mode) 259 { 260 module_put(mode->owner); 261 } 262 263 static bool team_dummy_transmit(struct team *team, struct sk_buff *skb) 264 { 265 dev_kfree_skb_any(skb); 266 return false; 267 } 268 269 rx_handler_result_t team_dummy_receive(struct team *team, 270 struct team_port *port, 271 struct sk_buff *skb) 272 { 273 return RX_HANDLER_ANOTHER; 274 } 275 276 static void team_adjust_ops(struct team *team) 277 { 278 /* 279 * To avoid checks in rx/tx skb paths, ensure here that non-null and 280 * correct ops are always set. 281 */ 282 283 if (list_empty(&team->port_list) || 284 !team->mode || !team->mode->ops->transmit) 285 team->ops.transmit = team_dummy_transmit; 286 else 287 team->ops.transmit = team->mode->ops->transmit; 288 289 if (list_empty(&team->port_list) || 290 !team->mode || !team->mode->ops->receive) 291 team->ops.receive = team_dummy_receive; 292 else 293 team->ops.receive = team->mode->ops->receive; 294 } 295 296 /* 297 * We can benefit from the fact that it's ensured no port is present 298 * at the time of mode change. Therefore no packets are in fly so there's no 299 * need to set mode operations in any special way. 300 */ 301 static int __team_change_mode(struct team *team, 302 const struct team_mode *new_mode) 303 { 304 /* Check if mode was previously set and do cleanup if so */ 305 if (team->mode) { 306 void (*exit_op)(struct team *team) = team->ops.exit; 307 308 /* Clear ops area so no callback is called any longer */ 309 memset(&team->ops, 0, sizeof(struct team_mode_ops)); 310 team_adjust_ops(team); 311 312 if (exit_op) 313 exit_op(team); 314 team_mode_put(team->mode); 315 team->mode = NULL; 316 /* zero private data area */ 317 memset(&team->mode_priv, 0, 318 sizeof(struct team) - offsetof(struct team, mode_priv)); 319 } 320 321 if (!new_mode) 322 return 0; 323 324 if (new_mode->ops->init) { 325 int err; 326 327 err = new_mode->ops->init(team); 328 if (err) 329 return err; 330 } 331 332 team->mode = new_mode; 333 memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops)); 334 team_adjust_ops(team); 335 336 return 0; 337 } 338 339 static int team_change_mode(struct team *team, const char *kind) 340 { 341 struct team_mode *new_mode; 342 struct net_device *dev = team->dev; 343 int err; 344 345 if (!list_empty(&team->port_list)) { 346 netdev_err(dev, "No ports can be present during mode change\n"); 347 return -EBUSY; 348 } 349 350 if (team->mode && strcmp(team->mode->kind, kind) == 0) { 351 netdev_err(dev, "Unable to change to the same mode the team is in\n"); 352 return -EINVAL; 353 } 354 355 new_mode = team_mode_get(kind); 356 if (!new_mode) { 357 netdev_err(dev, "Mode \"%s\" not found\n", kind); 358 return -EINVAL; 359 } 360 361 err = __team_change_mode(team, new_mode); 362 if (err) { 363 netdev_err(dev, "Failed to change to mode \"%s\"\n", kind); 364 team_mode_put(new_mode); 365 return err; 366 } 367 368 netdev_info(dev, "Mode changed to \"%s\"\n", kind); 369 return 0; 370 } 371 372 373 /************************ 374 * Rx path frame handler 375 ************************/ 376 377 /* note: already called with rcu_read_lock */ 378 static rx_handler_result_t team_handle_frame(struct sk_buff **pskb) 379 { 380 struct sk_buff *skb = *pskb; 381 struct team_port *port; 382 struct team *team; 383 rx_handler_result_t res; 384 385 skb = skb_share_check(skb, GFP_ATOMIC); 386 if (!skb) 387 return RX_HANDLER_CONSUMED; 388 389 *pskb = skb; 390 391 port = team_port_get_rcu(skb->dev); 392 team = port->team; 393 394 res = team->ops.receive(team, port, skb); 395 if (res == RX_HANDLER_ANOTHER) { 396 struct team_pcpu_stats *pcpu_stats; 397 398 pcpu_stats = this_cpu_ptr(team->pcpu_stats); 399 u64_stats_update_begin(&pcpu_stats->syncp); 400 pcpu_stats->rx_packets++; 401 pcpu_stats->rx_bytes += skb->len; 402 if (skb->pkt_type == PACKET_MULTICAST) 403 pcpu_stats->rx_multicast++; 404 u64_stats_update_end(&pcpu_stats->syncp); 405 406 skb->dev = team->dev; 407 } else { 408 this_cpu_inc(team->pcpu_stats->rx_dropped); 409 } 410 411 return res; 412 } 413 414 415 /**************** 416 * Port handling 417 ****************/ 418 419 static bool team_port_find(const struct team *team, 420 const struct team_port *port) 421 { 422 struct team_port *cur; 423 424 list_for_each_entry(cur, &team->port_list, list) 425 if (cur == port) 426 return true; 427 return false; 428 } 429 430 /* 431 * Add/delete port to the team port list. Write guarded by rtnl_lock. 432 * Takes care of correct port->index setup (might be racy). 433 */ 434 static void team_port_list_add_port(struct team *team, 435 struct team_port *port) 436 { 437 port->index = team->port_count++; 438 hlist_add_head_rcu(&port->hlist, 439 team_port_index_hash(team, port->index)); 440 list_add_tail_rcu(&port->list, &team->port_list); 441 } 442 443 static void __reconstruct_port_hlist(struct team *team, int rm_index) 444 { 445 int i; 446 struct team_port *port; 447 448 for (i = rm_index + 1; i < team->port_count; i++) { 449 port = team_get_port_by_index(team, i); 450 hlist_del_rcu(&port->hlist); 451 port->index--; 452 hlist_add_head_rcu(&port->hlist, 453 team_port_index_hash(team, port->index)); 454 } 455 } 456 457 static void team_port_list_del_port(struct team *team, 458 struct team_port *port) 459 { 460 int rm_index = port->index; 461 462 hlist_del_rcu(&port->hlist); 463 list_del_rcu(&port->list); 464 __reconstruct_port_hlist(team, rm_index); 465 team->port_count--; 466 } 467 468 #define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \ 469 NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \ 470 NETIF_F_HIGHDMA | NETIF_F_LRO) 471 472 static void __team_compute_features(struct team *team) 473 { 474 struct team_port *port; 475 u32 vlan_features = TEAM_VLAN_FEATURES; 476 unsigned short max_hard_header_len = ETH_HLEN; 477 478 list_for_each_entry(port, &team->port_list, list) { 479 vlan_features = netdev_increment_features(vlan_features, 480 port->dev->vlan_features, 481 TEAM_VLAN_FEATURES); 482 483 if (port->dev->hard_header_len > max_hard_header_len) 484 max_hard_header_len = port->dev->hard_header_len; 485 } 486 487 team->dev->vlan_features = vlan_features; 488 team->dev->hard_header_len = max_hard_header_len; 489 490 netdev_change_features(team->dev); 491 } 492 493 static void team_compute_features(struct team *team) 494 { 495 mutex_lock(&team->lock); 496 __team_compute_features(team); 497 mutex_unlock(&team->lock); 498 } 499 500 static int team_port_enter(struct team *team, struct team_port *port) 501 { 502 int err = 0; 503 504 dev_hold(team->dev); 505 port->dev->priv_flags |= IFF_TEAM_PORT; 506 if (team->ops.port_enter) { 507 err = team->ops.port_enter(team, port); 508 if (err) { 509 netdev_err(team->dev, "Device %s failed to enter team mode\n", 510 port->dev->name); 511 goto err_port_enter; 512 } 513 } 514 515 return 0; 516 517 err_port_enter: 518 port->dev->priv_flags &= ~IFF_TEAM_PORT; 519 dev_put(team->dev); 520 521 return err; 522 } 523 524 static void team_port_leave(struct team *team, struct team_port *port) 525 { 526 if (team->ops.port_leave) 527 team->ops.port_leave(team, port); 528 port->dev->priv_flags &= ~IFF_TEAM_PORT; 529 dev_put(team->dev); 530 } 531 532 static void __team_port_change_check(struct team_port *port, bool linkup); 533 534 static int team_port_add(struct team *team, struct net_device *port_dev) 535 { 536 struct net_device *dev = team->dev; 537 struct team_port *port; 538 char *portname = port_dev->name; 539 int err; 540 541 if (port_dev->flags & IFF_LOOPBACK || 542 port_dev->type != ARPHRD_ETHER) { 543 netdev_err(dev, "Device %s is of an unsupported type\n", 544 portname); 545 return -EINVAL; 546 } 547 548 if (team_port_exists(port_dev)) { 549 netdev_err(dev, "Device %s is already a port " 550 "of a team device\n", portname); 551 return -EBUSY; 552 } 553 554 if (port_dev->flags & IFF_UP) { 555 netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n", 556 portname); 557 return -EBUSY; 558 } 559 560 port = kzalloc(sizeof(struct team_port), GFP_KERNEL); 561 if (!port) 562 return -ENOMEM; 563 564 port->dev = port_dev; 565 port->team = team; 566 567 port->orig.mtu = port_dev->mtu; 568 err = dev_set_mtu(port_dev, dev->mtu); 569 if (err) { 570 netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err); 571 goto err_set_mtu; 572 } 573 574 memcpy(port->orig.dev_addr, port_dev->dev_addr, ETH_ALEN); 575 576 err = team_port_enter(team, port); 577 if (err) { 578 netdev_err(dev, "Device %s failed to enter team mode\n", 579 portname); 580 goto err_port_enter; 581 } 582 583 err = dev_open(port_dev); 584 if (err) { 585 netdev_dbg(dev, "Device %s opening failed\n", 586 portname); 587 goto err_dev_open; 588 } 589 590 err = netdev_set_master(port_dev, dev); 591 if (err) { 592 netdev_err(dev, "Device %s failed to set master\n", portname); 593 goto err_set_master; 594 } 595 596 err = netdev_rx_handler_register(port_dev, team_handle_frame, 597 port); 598 if (err) { 599 netdev_err(dev, "Device %s failed to register rx_handler\n", 600 portname); 601 goto err_handler_register; 602 } 603 604 team_port_list_add_port(team, port); 605 team_adjust_ops(team); 606 __team_compute_features(team); 607 __team_port_change_check(port, !!netif_carrier_ok(port_dev)); 608 609 netdev_info(dev, "Port device %s added\n", portname); 610 611 return 0; 612 613 err_handler_register: 614 netdev_set_master(port_dev, NULL); 615 616 err_set_master: 617 dev_close(port_dev); 618 619 err_dev_open: 620 team_port_leave(team, port); 621 team_port_set_orig_mac(port); 622 623 err_port_enter: 624 dev_set_mtu(port_dev, port->orig.mtu); 625 626 err_set_mtu: 627 kfree(port); 628 629 return err; 630 } 631 632 static int team_port_del(struct team *team, struct net_device *port_dev) 633 { 634 struct net_device *dev = team->dev; 635 struct team_port *port; 636 char *portname = port_dev->name; 637 638 port = team_port_get_rtnl(port_dev); 639 if (!port || !team_port_find(team, port)) { 640 netdev_err(dev, "Device %s does not act as a port of this team\n", 641 portname); 642 return -ENOENT; 643 } 644 645 __team_port_change_check(port, false); 646 team_port_list_del_port(team, port); 647 team_adjust_ops(team); 648 netdev_rx_handler_unregister(port_dev); 649 netdev_set_master(port_dev, NULL); 650 dev_close(port_dev); 651 team_port_leave(team, port); 652 team_port_set_orig_mac(port); 653 dev_set_mtu(port_dev, port->orig.mtu); 654 synchronize_rcu(); 655 kfree(port); 656 netdev_info(dev, "Port device %s removed\n", portname); 657 __team_compute_features(team); 658 659 return 0; 660 } 661 662 663 /***************** 664 * Net device ops 665 *****************/ 666 667 static const char team_no_mode_kind[] = "*NOMODE*"; 668 669 static int team_mode_option_get(struct team *team, void *arg) 670 { 671 const char **str = arg; 672 673 *str = team->mode ? team->mode->kind : team_no_mode_kind; 674 return 0; 675 } 676 677 static int team_mode_option_set(struct team *team, void *arg) 678 { 679 const char **str = arg; 680 681 return team_change_mode(team, *str); 682 } 683 684 static const struct team_option team_options[] = { 685 { 686 .name = "mode", 687 .type = TEAM_OPTION_TYPE_STRING, 688 .getter = team_mode_option_get, 689 .setter = team_mode_option_set, 690 }, 691 }; 692 693 static int team_init(struct net_device *dev) 694 { 695 struct team *team = netdev_priv(dev); 696 int i; 697 int err; 698 699 team->dev = dev; 700 mutex_init(&team->lock); 701 702 team->pcpu_stats = alloc_percpu(struct team_pcpu_stats); 703 if (!team->pcpu_stats) 704 return -ENOMEM; 705 706 for (i = 0; i < TEAM_PORT_HASHENTRIES; i++) 707 INIT_HLIST_HEAD(&team->port_hlist[i]); 708 INIT_LIST_HEAD(&team->port_list); 709 710 team_adjust_ops(team); 711 712 INIT_LIST_HEAD(&team->option_list); 713 err = team_options_register(team, team_options, ARRAY_SIZE(team_options)); 714 if (err) 715 goto err_options_register; 716 netif_carrier_off(dev); 717 718 return 0; 719 720 err_options_register: 721 free_percpu(team->pcpu_stats); 722 723 return err; 724 } 725 726 static void team_uninit(struct net_device *dev) 727 { 728 struct team *team = netdev_priv(dev); 729 struct team_port *port; 730 struct team_port *tmp; 731 732 mutex_lock(&team->lock); 733 list_for_each_entry_safe(port, tmp, &team->port_list, list) 734 team_port_del(team, port->dev); 735 736 __team_change_mode(team, NULL); /* cleanup */ 737 __team_options_unregister(team, team_options, ARRAY_SIZE(team_options)); 738 mutex_unlock(&team->lock); 739 } 740 741 static void team_destructor(struct net_device *dev) 742 { 743 struct team *team = netdev_priv(dev); 744 745 free_percpu(team->pcpu_stats); 746 free_netdev(dev); 747 } 748 749 static int team_open(struct net_device *dev) 750 { 751 netif_carrier_on(dev); 752 return 0; 753 } 754 755 static int team_close(struct net_device *dev) 756 { 757 netif_carrier_off(dev); 758 return 0; 759 } 760 761 /* 762 * note: already called with rcu_read_lock 763 */ 764 static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev) 765 { 766 struct team *team = netdev_priv(dev); 767 bool tx_success = false; 768 unsigned int len = skb->len; 769 770 tx_success = team->ops.transmit(team, skb); 771 if (tx_success) { 772 struct team_pcpu_stats *pcpu_stats; 773 774 pcpu_stats = this_cpu_ptr(team->pcpu_stats); 775 u64_stats_update_begin(&pcpu_stats->syncp); 776 pcpu_stats->tx_packets++; 777 pcpu_stats->tx_bytes += len; 778 u64_stats_update_end(&pcpu_stats->syncp); 779 } else { 780 this_cpu_inc(team->pcpu_stats->tx_dropped); 781 } 782 783 return NETDEV_TX_OK; 784 } 785 786 static void team_change_rx_flags(struct net_device *dev, int change) 787 { 788 struct team *team = netdev_priv(dev); 789 struct team_port *port; 790 int inc; 791 792 rcu_read_lock(); 793 list_for_each_entry_rcu(port, &team->port_list, list) { 794 if (change & IFF_PROMISC) { 795 inc = dev->flags & IFF_PROMISC ? 1 : -1; 796 dev_set_promiscuity(port->dev, inc); 797 } 798 if (change & IFF_ALLMULTI) { 799 inc = dev->flags & IFF_ALLMULTI ? 1 : -1; 800 dev_set_allmulti(port->dev, inc); 801 } 802 } 803 rcu_read_unlock(); 804 } 805 806 static void team_set_rx_mode(struct net_device *dev) 807 { 808 struct team *team = netdev_priv(dev); 809 struct team_port *port; 810 811 rcu_read_lock(); 812 list_for_each_entry_rcu(port, &team->port_list, list) { 813 dev_uc_sync(port->dev, dev); 814 dev_mc_sync(port->dev, dev); 815 } 816 rcu_read_unlock(); 817 } 818 819 static int team_set_mac_address(struct net_device *dev, void *p) 820 { 821 struct team *team = netdev_priv(dev); 822 struct team_port *port; 823 struct sockaddr *addr = p; 824 825 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN); 826 rcu_read_lock(); 827 list_for_each_entry_rcu(port, &team->port_list, list) 828 if (team->ops.port_change_mac) 829 team->ops.port_change_mac(team, port); 830 rcu_read_unlock(); 831 return 0; 832 } 833 834 static int team_change_mtu(struct net_device *dev, int new_mtu) 835 { 836 struct team *team = netdev_priv(dev); 837 struct team_port *port; 838 int err; 839 840 /* 841 * Alhough this is reader, it's guarded by team lock. It's not possible 842 * to traverse list in reverse under rcu_read_lock 843 */ 844 mutex_lock(&team->lock); 845 list_for_each_entry(port, &team->port_list, list) { 846 err = dev_set_mtu(port->dev, new_mtu); 847 if (err) { 848 netdev_err(dev, "Device %s failed to change mtu", 849 port->dev->name); 850 goto unwind; 851 } 852 } 853 mutex_unlock(&team->lock); 854 855 dev->mtu = new_mtu; 856 857 return 0; 858 859 unwind: 860 list_for_each_entry_continue_reverse(port, &team->port_list, list) 861 dev_set_mtu(port->dev, dev->mtu); 862 mutex_unlock(&team->lock); 863 864 return err; 865 } 866 867 static struct rtnl_link_stats64 * 868 team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) 869 { 870 struct team *team = netdev_priv(dev); 871 struct team_pcpu_stats *p; 872 u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes; 873 u32 rx_dropped = 0, tx_dropped = 0; 874 unsigned int start; 875 int i; 876 877 for_each_possible_cpu(i) { 878 p = per_cpu_ptr(team->pcpu_stats, i); 879 do { 880 start = u64_stats_fetch_begin_bh(&p->syncp); 881 rx_packets = p->rx_packets; 882 rx_bytes = p->rx_bytes; 883 rx_multicast = p->rx_multicast; 884 tx_packets = p->tx_packets; 885 tx_bytes = p->tx_bytes; 886 } while (u64_stats_fetch_retry_bh(&p->syncp, start)); 887 888 stats->rx_packets += rx_packets; 889 stats->rx_bytes += rx_bytes; 890 stats->multicast += rx_multicast; 891 stats->tx_packets += tx_packets; 892 stats->tx_bytes += tx_bytes; 893 /* 894 * rx_dropped & tx_dropped are u32, updated 895 * without syncp protection. 896 */ 897 rx_dropped += p->rx_dropped; 898 tx_dropped += p->tx_dropped; 899 } 900 stats->rx_dropped = rx_dropped; 901 stats->tx_dropped = tx_dropped; 902 return stats; 903 } 904 905 static int team_vlan_rx_add_vid(struct net_device *dev, uint16_t vid) 906 { 907 struct team *team = netdev_priv(dev); 908 struct team_port *port; 909 910 rcu_read_lock(); 911 list_for_each_entry_rcu(port, &team->port_list, list) { 912 const struct net_device_ops *ops = port->dev->netdev_ops; 913 914 if (ops->ndo_vlan_rx_add_vid) 915 ops->ndo_vlan_rx_add_vid(port->dev, vid); 916 } 917 rcu_read_unlock(); 918 919 return 0; 920 } 921 922 static int team_vlan_rx_kill_vid(struct net_device *dev, uint16_t vid) 923 { 924 struct team *team = netdev_priv(dev); 925 struct team_port *port; 926 927 rcu_read_lock(); 928 list_for_each_entry_rcu(port, &team->port_list, list) { 929 const struct net_device_ops *ops = port->dev->netdev_ops; 930 931 if (ops->ndo_vlan_rx_kill_vid) 932 ops->ndo_vlan_rx_kill_vid(port->dev, vid); 933 } 934 rcu_read_unlock(); 935 936 return 0; 937 } 938 939 static int team_add_slave(struct net_device *dev, struct net_device *port_dev) 940 { 941 struct team *team = netdev_priv(dev); 942 int err; 943 944 mutex_lock(&team->lock); 945 err = team_port_add(team, port_dev); 946 mutex_unlock(&team->lock); 947 return err; 948 } 949 950 static int team_del_slave(struct net_device *dev, struct net_device *port_dev) 951 { 952 struct team *team = netdev_priv(dev); 953 int err; 954 955 mutex_lock(&team->lock); 956 err = team_port_del(team, port_dev); 957 mutex_unlock(&team->lock); 958 return err; 959 } 960 961 static netdev_features_t team_fix_features(struct net_device *dev, 962 netdev_features_t features) 963 { 964 struct team_port *port; 965 struct team *team = netdev_priv(dev); 966 netdev_features_t mask; 967 968 mask = features; 969 features &= ~NETIF_F_ONE_FOR_ALL; 970 features |= NETIF_F_ALL_FOR_ALL; 971 972 rcu_read_lock(); 973 list_for_each_entry_rcu(port, &team->port_list, list) { 974 features = netdev_increment_features(features, 975 port->dev->features, 976 mask); 977 } 978 rcu_read_unlock(); 979 return features; 980 } 981 982 static const struct net_device_ops team_netdev_ops = { 983 .ndo_init = team_init, 984 .ndo_uninit = team_uninit, 985 .ndo_open = team_open, 986 .ndo_stop = team_close, 987 .ndo_start_xmit = team_xmit, 988 .ndo_change_rx_flags = team_change_rx_flags, 989 .ndo_set_rx_mode = team_set_rx_mode, 990 .ndo_set_mac_address = team_set_mac_address, 991 .ndo_change_mtu = team_change_mtu, 992 .ndo_get_stats64 = team_get_stats64, 993 .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid, 994 .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid, 995 .ndo_add_slave = team_add_slave, 996 .ndo_del_slave = team_del_slave, 997 .ndo_fix_features = team_fix_features, 998 }; 999 1000 1001 /*********************** 1002 * rt netlink interface 1003 ***********************/ 1004 1005 static void team_setup(struct net_device *dev) 1006 { 1007 ether_setup(dev); 1008 1009 dev->netdev_ops = &team_netdev_ops; 1010 dev->destructor = team_destructor; 1011 dev->tx_queue_len = 0; 1012 dev->flags |= IFF_MULTICAST; 1013 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING); 1014 1015 /* 1016 * Indicate we support unicast address filtering. That way core won't 1017 * bring us to promisc mode in case a unicast addr is added. 1018 * Let this up to underlay drivers. 1019 */ 1020 dev->priv_flags |= IFF_UNICAST_FLT; 1021 1022 dev->features |= NETIF_F_LLTX; 1023 dev->features |= NETIF_F_GRO; 1024 dev->hw_features = NETIF_F_HW_VLAN_TX | 1025 NETIF_F_HW_VLAN_RX | 1026 NETIF_F_HW_VLAN_FILTER; 1027 1028 dev->features |= dev->hw_features; 1029 } 1030 1031 static int team_newlink(struct net *src_net, struct net_device *dev, 1032 struct nlattr *tb[], struct nlattr *data[]) 1033 { 1034 int err; 1035 1036 if (tb[IFLA_ADDRESS] == NULL) 1037 random_ether_addr(dev->dev_addr); 1038 1039 err = register_netdevice(dev); 1040 if (err) 1041 return err; 1042 1043 return 0; 1044 } 1045 1046 static int team_validate(struct nlattr *tb[], struct nlattr *data[]) 1047 { 1048 if (tb[IFLA_ADDRESS]) { 1049 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) 1050 return -EINVAL; 1051 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) 1052 return -EADDRNOTAVAIL; 1053 } 1054 return 0; 1055 } 1056 1057 static struct rtnl_link_ops team_link_ops __read_mostly = { 1058 .kind = DRV_NAME, 1059 .priv_size = sizeof(struct team), 1060 .setup = team_setup, 1061 .newlink = team_newlink, 1062 .validate = team_validate, 1063 }; 1064 1065 1066 /*********************************** 1067 * Generic netlink custom interface 1068 ***********************************/ 1069 1070 static struct genl_family team_nl_family = { 1071 .id = GENL_ID_GENERATE, 1072 .name = TEAM_GENL_NAME, 1073 .version = TEAM_GENL_VERSION, 1074 .maxattr = TEAM_ATTR_MAX, 1075 .netnsok = true, 1076 }; 1077 1078 static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = { 1079 [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, }, 1080 [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 }, 1081 [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED }, 1082 [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED }, 1083 }; 1084 1085 static const struct nla_policy 1086 team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = { 1087 [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, }, 1088 [TEAM_ATTR_OPTION_NAME] = { 1089 .type = NLA_STRING, 1090 .len = TEAM_STRING_MAX_LEN, 1091 }, 1092 [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG }, 1093 [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 }, 1094 [TEAM_ATTR_OPTION_DATA] = { 1095 .type = NLA_BINARY, 1096 .len = TEAM_STRING_MAX_LEN, 1097 }, 1098 }; 1099 1100 static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info) 1101 { 1102 struct sk_buff *msg; 1103 void *hdr; 1104 int err; 1105 1106 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); 1107 if (!msg) 1108 return -ENOMEM; 1109 1110 hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq, 1111 &team_nl_family, 0, TEAM_CMD_NOOP); 1112 if (IS_ERR(hdr)) { 1113 err = PTR_ERR(hdr); 1114 goto err_msg_put; 1115 } 1116 1117 genlmsg_end(msg, hdr); 1118 1119 return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid); 1120 1121 err_msg_put: 1122 nlmsg_free(msg); 1123 1124 return err; 1125 } 1126 1127 /* 1128 * Netlink cmd functions should be locked by following two functions. 1129 * Since dev gets held here, that ensures dev won't disappear in between. 1130 */ 1131 static struct team *team_nl_team_get(struct genl_info *info) 1132 { 1133 struct net *net = genl_info_net(info); 1134 int ifindex; 1135 struct net_device *dev; 1136 struct team *team; 1137 1138 if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX]) 1139 return NULL; 1140 1141 ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]); 1142 dev = dev_get_by_index(net, ifindex); 1143 if (!dev || dev->netdev_ops != &team_netdev_ops) { 1144 if (dev) 1145 dev_put(dev); 1146 return NULL; 1147 } 1148 1149 team = netdev_priv(dev); 1150 mutex_lock(&team->lock); 1151 return team; 1152 } 1153 1154 static void team_nl_team_put(struct team *team) 1155 { 1156 mutex_unlock(&team->lock); 1157 dev_put(team->dev); 1158 } 1159 1160 static int team_nl_send_generic(struct genl_info *info, struct team *team, 1161 int (*fill_func)(struct sk_buff *skb, 1162 struct genl_info *info, 1163 int flags, struct team *team)) 1164 { 1165 struct sk_buff *skb; 1166 int err; 1167 1168 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); 1169 if (!skb) 1170 return -ENOMEM; 1171 1172 err = fill_func(skb, info, NLM_F_ACK, team); 1173 if (err < 0) 1174 goto err_fill; 1175 1176 err = genlmsg_unicast(genl_info_net(info), skb, info->snd_pid); 1177 return err; 1178 1179 err_fill: 1180 nlmsg_free(skb); 1181 return err; 1182 } 1183 1184 static int team_nl_fill_options_get_changed(struct sk_buff *skb, 1185 u32 pid, u32 seq, int flags, 1186 struct team *team, 1187 struct team_option *changed_option) 1188 { 1189 struct nlattr *option_list; 1190 void *hdr; 1191 struct team_option *option; 1192 1193 hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags, 1194 TEAM_CMD_OPTIONS_GET); 1195 if (IS_ERR(hdr)) 1196 return PTR_ERR(hdr); 1197 1198 NLA_PUT_U32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex); 1199 option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION); 1200 if (!option_list) 1201 return -EMSGSIZE; 1202 1203 list_for_each_entry(option, &team->option_list, list) { 1204 struct nlattr *option_item; 1205 long arg; 1206 1207 option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION); 1208 if (!option_item) 1209 goto nla_put_failure; 1210 NLA_PUT_STRING(skb, TEAM_ATTR_OPTION_NAME, option->name); 1211 if (option == changed_option) 1212 NLA_PUT_FLAG(skb, TEAM_ATTR_OPTION_CHANGED); 1213 switch (option->type) { 1214 case TEAM_OPTION_TYPE_U32: 1215 NLA_PUT_U8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32); 1216 team_option_get(team, option, &arg); 1217 NLA_PUT_U32(skb, TEAM_ATTR_OPTION_DATA, arg); 1218 break; 1219 case TEAM_OPTION_TYPE_STRING: 1220 NLA_PUT_U8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING); 1221 team_option_get(team, option, &arg); 1222 NLA_PUT_STRING(skb, TEAM_ATTR_OPTION_DATA, 1223 (char *) arg); 1224 break; 1225 default: 1226 BUG(); 1227 } 1228 nla_nest_end(skb, option_item); 1229 } 1230 1231 nla_nest_end(skb, option_list); 1232 return genlmsg_end(skb, hdr); 1233 1234 nla_put_failure: 1235 genlmsg_cancel(skb, hdr); 1236 return -EMSGSIZE; 1237 } 1238 1239 static int team_nl_fill_options_get(struct sk_buff *skb, 1240 struct genl_info *info, int flags, 1241 struct team *team) 1242 { 1243 return team_nl_fill_options_get_changed(skb, info->snd_pid, 1244 info->snd_seq, NLM_F_ACK, 1245 team, NULL); 1246 } 1247 1248 static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info) 1249 { 1250 struct team *team; 1251 int err; 1252 1253 team = team_nl_team_get(info); 1254 if (!team) 1255 return -EINVAL; 1256 1257 err = team_nl_send_generic(info, team, team_nl_fill_options_get); 1258 1259 team_nl_team_put(team); 1260 1261 return err; 1262 } 1263 1264 static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info) 1265 { 1266 struct team *team; 1267 int err = 0; 1268 int i; 1269 struct nlattr *nl_option; 1270 1271 team = team_nl_team_get(info); 1272 if (!team) 1273 return -EINVAL; 1274 1275 err = -EINVAL; 1276 if (!info->attrs[TEAM_ATTR_LIST_OPTION]) { 1277 err = -EINVAL; 1278 goto team_put; 1279 } 1280 1281 nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) { 1282 struct nlattr *mode_attrs[TEAM_ATTR_OPTION_MAX + 1]; 1283 enum team_option_type opt_type; 1284 struct team_option *option; 1285 char *opt_name; 1286 bool opt_found = false; 1287 1288 if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) { 1289 err = -EINVAL; 1290 goto team_put; 1291 } 1292 err = nla_parse_nested(mode_attrs, TEAM_ATTR_OPTION_MAX, 1293 nl_option, team_nl_option_policy); 1294 if (err) 1295 goto team_put; 1296 if (!mode_attrs[TEAM_ATTR_OPTION_NAME] || 1297 !mode_attrs[TEAM_ATTR_OPTION_TYPE] || 1298 !mode_attrs[TEAM_ATTR_OPTION_DATA]) { 1299 err = -EINVAL; 1300 goto team_put; 1301 } 1302 switch (nla_get_u8(mode_attrs[TEAM_ATTR_OPTION_TYPE])) { 1303 case NLA_U32: 1304 opt_type = TEAM_OPTION_TYPE_U32; 1305 break; 1306 case NLA_STRING: 1307 opt_type = TEAM_OPTION_TYPE_STRING; 1308 break; 1309 default: 1310 goto team_put; 1311 } 1312 1313 opt_name = nla_data(mode_attrs[TEAM_ATTR_OPTION_NAME]); 1314 list_for_each_entry(option, &team->option_list, list) { 1315 long arg; 1316 struct nlattr *opt_data_attr; 1317 1318 if (option->type != opt_type || 1319 strcmp(option->name, opt_name)) 1320 continue; 1321 opt_found = true; 1322 opt_data_attr = mode_attrs[TEAM_ATTR_OPTION_DATA]; 1323 switch (opt_type) { 1324 case TEAM_OPTION_TYPE_U32: 1325 arg = nla_get_u32(opt_data_attr); 1326 break; 1327 case TEAM_OPTION_TYPE_STRING: 1328 arg = (long) nla_data(opt_data_attr); 1329 break; 1330 default: 1331 BUG(); 1332 } 1333 err = team_option_set(team, option, &arg); 1334 if (err) 1335 goto team_put; 1336 } 1337 if (!opt_found) { 1338 err = -ENOENT; 1339 goto team_put; 1340 } 1341 } 1342 1343 team_put: 1344 team_nl_team_put(team); 1345 1346 return err; 1347 } 1348 1349 static int team_nl_fill_port_list_get_changed(struct sk_buff *skb, 1350 u32 pid, u32 seq, int flags, 1351 struct team *team, 1352 struct team_port *changed_port) 1353 { 1354 struct nlattr *port_list; 1355 void *hdr; 1356 struct team_port *port; 1357 1358 hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags, 1359 TEAM_CMD_PORT_LIST_GET); 1360 if (IS_ERR(hdr)) 1361 return PTR_ERR(hdr); 1362 1363 NLA_PUT_U32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex); 1364 port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT); 1365 if (!port_list) 1366 return -EMSGSIZE; 1367 1368 list_for_each_entry(port, &team->port_list, list) { 1369 struct nlattr *port_item; 1370 1371 port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT); 1372 if (!port_item) 1373 goto nla_put_failure; 1374 NLA_PUT_U32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex); 1375 if (port == changed_port) 1376 NLA_PUT_FLAG(skb, TEAM_ATTR_PORT_CHANGED); 1377 if (port->linkup) 1378 NLA_PUT_FLAG(skb, TEAM_ATTR_PORT_LINKUP); 1379 NLA_PUT_U32(skb, TEAM_ATTR_PORT_SPEED, port->speed); 1380 NLA_PUT_U8(skb, TEAM_ATTR_PORT_DUPLEX, port->duplex); 1381 nla_nest_end(skb, port_item); 1382 } 1383 1384 nla_nest_end(skb, port_list); 1385 return genlmsg_end(skb, hdr); 1386 1387 nla_put_failure: 1388 genlmsg_cancel(skb, hdr); 1389 return -EMSGSIZE; 1390 } 1391 1392 static int team_nl_fill_port_list_get(struct sk_buff *skb, 1393 struct genl_info *info, int flags, 1394 struct team *team) 1395 { 1396 return team_nl_fill_port_list_get_changed(skb, info->snd_pid, 1397 info->snd_seq, NLM_F_ACK, 1398 team, NULL); 1399 } 1400 1401 static int team_nl_cmd_port_list_get(struct sk_buff *skb, 1402 struct genl_info *info) 1403 { 1404 struct team *team; 1405 int err; 1406 1407 team = team_nl_team_get(info); 1408 if (!team) 1409 return -EINVAL; 1410 1411 err = team_nl_send_generic(info, team, team_nl_fill_port_list_get); 1412 1413 team_nl_team_put(team); 1414 1415 return err; 1416 } 1417 1418 static struct genl_ops team_nl_ops[] = { 1419 { 1420 .cmd = TEAM_CMD_NOOP, 1421 .doit = team_nl_cmd_noop, 1422 .policy = team_nl_policy, 1423 }, 1424 { 1425 .cmd = TEAM_CMD_OPTIONS_SET, 1426 .doit = team_nl_cmd_options_set, 1427 .policy = team_nl_policy, 1428 .flags = GENL_ADMIN_PERM, 1429 }, 1430 { 1431 .cmd = TEAM_CMD_OPTIONS_GET, 1432 .doit = team_nl_cmd_options_get, 1433 .policy = team_nl_policy, 1434 .flags = GENL_ADMIN_PERM, 1435 }, 1436 { 1437 .cmd = TEAM_CMD_PORT_LIST_GET, 1438 .doit = team_nl_cmd_port_list_get, 1439 .policy = team_nl_policy, 1440 .flags = GENL_ADMIN_PERM, 1441 }, 1442 }; 1443 1444 static struct genl_multicast_group team_change_event_mcgrp = { 1445 .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME, 1446 }; 1447 1448 static int team_nl_send_event_options_get(struct team *team, 1449 struct team_option *changed_option) 1450 { 1451 struct sk_buff *skb; 1452 int err; 1453 struct net *net = dev_net(team->dev); 1454 1455 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); 1456 if (!skb) 1457 return -ENOMEM; 1458 1459 err = team_nl_fill_options_get_changed(skb, 0, 0, 0, team, 1460 changed_option); 1461 if (err < 0) 1462 goto err_fill; 1463 1464 err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id, 1465 GFP_KERNEL); 1466 return err; 1467 1468 err_fill: 1469 nlmsg_free(skb); 1470 return err; 1471 } 1472 1473 static int team_nl_send_event_port_list_get(struct team_port *port) 1474 { 1475 struct sk_buff *skb; 1476 int err; 1477 struct net *net = dev_net(port->team->dev); 1478 1479 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); 1480 if (!skb) 1481 return -ENOMEM; 1482 1483 err = team_nl_fill_port_list_get_changed(skb, 0, 0, 0, 1484 port->team, port); 1485 if (err < 0) 1486 goto err_fill; 1487 1488 err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id, 1489 GFP_KERNEL); 1490 return err; 1491 1492 err_fill: 1493 nlmsg_free(skb); 1494 return err; 1495 } 1496 1497 static int team_nl_init(void) 1498 { 1499 int err; 1500 1501 err = genl_register_family_with_ops(&team_nl_family, team_nl_ops, 1502 ARRAY_SIZE(team_nl_ops)); 1503 if (err) 1504 return err; 1505 1506 err = genl_register_mc_group(&team_nl_family, &team_change_event_mcgrp); 1507 if (err) 1508 goto err_change_event_grp_reg; 1509 1510 return 0; 1511 1512 err_change_event_grp_reg: 1513 genl_unregister_family(&team_nl_family); 1514 1515 return err; 1516 } 1517 1518 static void team_nl_fini(void) 1519 { 1520 genl_unregister_family(&team_nl_family); 1521 } 1522 1523 1524 /****************** 1525 * Change checkers 1526 ******************/ 1527 1528 static void __team_options_change_check(struct team *team, 1529 struct team_option *changed_option) 1530 { 1531 int err; 1532 1533 err = team_nl_send_event_options_get(team, changed_option); 1534 if (err) 1535 netdev_warn(team->dev, "Failed to send options change via netlink\n"); 1536 } 1537 1538 /* rtnl lock is held */ 1539 static void __team_port_change_check(struct team_port *port, bool linkup) 1540 { 1541 int err; 1542 1543 if (port->linkup == linkup) 1544 return; 1545 1546 port->linkup = linkup; 1547 if (linkup) { 1548 struct ethtool_cmd ecmd; 1549 1550 err = __ethtool_get_settings(port->dev, &ecmd); 1551 if (!err) { 1552 port->speed = ethtool_cmd_speed(&ecmd); 1553 port->duplex = ecmd.duplex; 1554 goto send_event; 1555 } 1556 } 1557 port->speed = 0; 1558 port->duplex = 0; 1559 1560 send_event: 1561 err = team_nl_send_event_port_list_get(port); 1562 if (err) 1563 netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink\n", 1564 port->dev->name); 1565 1566 } 1567 1568 static void team_port_change_check(struct team_port *port, bool linkup) 1569 { 1570 struct team *team = port->team; 1571 1572 mutex_lock(&team->lock); 1573 __team_port_change_check(port, linkup); 1574 mutex_unlock(&team->lock); 1575 } 1576 1577 /************************************ 1578 * Net device notifier event handler 1579 ************************************/ 1580 1581 static int team_device_event(struct notifier_block *unused, 1582 unsigned long event, void *ptr) 1583 { 1584 struct net_device *dev = (struct net_device *) ptr; 1585 struct team_port *port; 1586 1587 port = team_port_get_rtnl(dev); 1588 if (!port) 1589 return NOTIFY_DONE; 1590 1591 switch (event) { 1592 case NETDEV_UP: 1593 if (netif_carrier_ok(dev)) 1594 team_port_change_check(port, true); 1595 case NETDEV_DOWN: 1596 team_port_change_check(port, false); 1597 case NETDEV_CHANGE: 1598 if (netif_running(port->dev)) 1599 team_port_change_check(port, 1600 !!netif_carrier_ok(port->dev)); 1601 break; 1602 case NETDEV_UNREGISTER: 1603 team_del_slave(port->team->dev, dev); 1604 break; 1605 case NETDEV_FEAT_CHANGE: 1606 team_compute_features(port->team); 1607 break; 1608 case NETDEV_CHANGEMTU: 1609 /* Forbid to change mtu of underlaying device */ 1610 return NOTIFY_BAD; 1611 case NETDEV_PRE_TYPE_CHANGE: 1612 /* Forbid to change type of underlaying device */ 1613 return NOTIFY_BAD; 1614 } 1615 return NOTIFY_DONE; 1616 } 1617 1618 static struct notifier_block team_notifier_block __read_mostly = { 1619 .notifier_call = team_device_event, 1620 }; 1621 1622 1623 /*********************** 1624 * Module init and exit 1625 ***********************/ 1626 1627 static int __init team_module_init(void) 1628 { 1629 int err; 1630 1631 register_netdevice_notifier(&team_notifier_block); 1632 1633 err = rtnl_link_register(&team_link_ops); 1634 if (err) 1635 goto err_rtnl_reg; 1636 1637 err = team_nl_init(); 1638 if (err) 1639 goto err_nl_init; 1640 1641 return 0; 1642 1643 err_nl_init: 1644 rtnl_link_unregister(&team_link_ops); 1645 1646 err_rtnl_reg: 1647 unregister_netdevice_notifier(&team_notifier_block); 1648 1649 return err; 1650 } 1651 1652 static void __exit team_module_exit(void) 1653 { 1654 team_nl_fini(); 1655 rtnl_link_unregister(&team_link_ops); 1656 unregister_netdevice_notifier(&team_notifier_block); 1657 } 1658 1659 module_init(team_module_init); 1660 module_exit(team_module_exit); 1661 1662 MODULE_LICENSE("GPL v2"); 1663 MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>"); 1664 MODULE_DESCRIPTION("Ethernet team device driver"); 1665 MODULE_ALIAS_RTNL_LINK(DRV_NAME); 1666