1 /* 2 * drivers/net/team/team.c - Network team device driver 3 * Copyright (c) 2011 Jiri Pirko <jpirko@redhat.com> 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 2 of the License, or 8 * (at your option) any later version. 9 */ 10 11 #include <linux/kernel.h> 12 #include <linux/types.h> 13 #include <linux/module.h> 14 #include <linux/init.h> 15 #include <linux/slab.h> 16 #include <linux/rcupdate.h> 17 #include <linux/errno.h> 18 #include <linux/ctype.h> 19 #include <linux/notifier.h> 20 #include <linux/netdevice.h> 21 #include <linux/if_vlan.h> 22 #include <linux/if_arp.h> 23 #include <linux/socket.h> 24 #include <linux/etherdevice.h> 25 #include <linux/rtnetlink.h> 26 #include <net/rtnetlink.h> 27 #include <net/genetlink.h> 28 #include <net/netlink.h> 29 #include <linux/if_team.h> 30 31 #define DRV_NAME "team" 32 33 34 /********** 35 * Helpers 36 **********/ 37 38 #define team_port_exists(dev) (dev->priv_flags & IFF_TEAM_PORT) 39 40 static struct team_port *team_port_get_rcu(const struct net_device *dev) 41 { 42 struct team_port *port = rcu_dereference(dev->rx_handler_data); 43 44 return team_port_exists(dev) ? port : NULL; 45 } 46 47 static struct team_port *team_port_get_rtnl(const struct net_device *dev) 48 { 49 struct team_port *port = rtnl_dereference(dev->rx_handler_data); 50 51 return team_port_exists(dev) ? port : NULL; 52 } 53 54 /* 55 * Since the ability to change mac address for open port device is tested in 56 * team_port_add, this function can be called without control of return value 57 */ 58 static int __set_port_mac(struct net_device *port_dev, 59 const unsigned char *dev_addr) 60 { 61 struct sockaddr addr; 62 63 memcpy(addr.sa_data, dev_addr, ETH_ALEN); 64 addr.sa_family = ARPHRD_ETHER; 65 return dev_set_mac_address(port_dev, &addr); 66 } 67 68 static int team_port_set_orig_mac(struct team_port *port) 69 { 70 return __set_port_mac(port->dev, port->orig.dev_addr); 71 } 72 73 int team_port_set_team_mac(struct team_port *port) 74 { 75 return __set_port_mac(port->dev, port->team->dev->dev_addr); 76 } 77 EXPORT_SYMBOL(team_port_set_team_mac); 78 79 static void team_refresh_port_linkup(struct team_port *port) 80 { 81 port->linkup = port->user.linkup_enabled ? port->user.linkup : 82 port->state.linkup; 83 } 84 85 86 /******************* 87 * Options handling 88 *******************/ 89 90 struct team_option_inst { /* One for each option instance */ 91 struct list_head list; 92 struct list_head tmp_list; 93 struct team_option *option; 94 struct team_option_inst_info info; 95 bool changed; 96 bool removed; 97 }; 98 99 static struct team_option *__team_find_option(struct team *team, 100 const char *opt_name) 101 { 102 struct team_option *option; 103 104 list_for_each_entry(option, &team->option_list, list) { 105 if (strcmp(option->name, opt_name) == 0) 106 return option; 107 } 108 return NULL; 109 } 110 111 static void __team_option_inst_del(struct team_option_inst *opt_inst) 112 { 113 list_del(&opt_inst->list); 114 kfree(opt_inst); 115 } 116 117 static void __team_option_inst_del_option(struct team *team, 118 struct team_option *option) 119 { 120 struct team_option_inst *opt_inst, *tmp; 121 122 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) { 123 if (opt_inst->option == option) 124 __team_option_inst_del(opt_inst); 125 } 126 } 127 128 static int __team_option_inst_add(struct team *team, struct team_option *option, 129 struct team_port *port) 130 { 131 struct team_option_inst *opt_inst; 132 unsigned int array_size; 133 unsigned int i; 134 int err; 135 136 array_size = option->array_size; 137 if (!array_size) 138 array_size = 1; /* No array but still need one instance */ 139 140 for (i = 0; i < array_size; i++) { 141 opt_inst = kmalloc(sizeof(*opt_inst), GFP_KERNEL); 142 if (!opt_inst) 143 return -ENOMEM; 144 opt_inst->option = option; 145 opt_inst->info.port = port; 146 opt_inst->info.array_index = i; 147 opt_inst->changed = true; 148 opt_inst->removed = false; 149 list_add_tail(&opt_inst->list, &team->option_inst_list); 150 if (option->init) { 151 err = option->init(team, &opt_inst->info); 152 if (err) 153 return err; 154 } 155 156 } 157 return 0; 158 } 159 160 static int __team_option_inst_add_option(struct team *team, 161 struct team_option *option) 162 { 163 struct team_port *port; 164 int err; 165 166 if (!option->per_port) { 167 err = __team_option_inst_add(team, option, NULL); 168 if (err) 169 goto inst_del_option; 170 } 171 172 list_for_each_entry(port, &team->port_list, list) { 173 err = __team_option_inst_add(team, option, port); 174 if (err) 175 goto inst_del_option; 176 } 177 return 0; 178 179 inst_del_option: 180 __team_option_inst_del_option(team, option); 181 return err; 182 } 183 184 static void __team_option_inst_mark_removed_option(struct team *team, 185 struct team_option *option) 186 { 187 struct team_option_inst *opt_inst; 188 189 list_for_each_entry(opt_inst, &team->option_inst_list, list) { 190 if (opt_inst->option == option) { 191 opt_inst->changed = true; 192 opt_inst->removed = true; 193 } 194 } 195 } 196 197 static void __team_option_inst_del_port(struct team *team, 198 struct team_port *port) 199 { 200 struct team_option_inst *opt_inst, *tmp; 201 202 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) { 203 if (opt_inst->option->per_port && 204 opt_inst->info.port == port) 205 __team_option_inst_del(opt_inst); 206 } 207 } 208 209 static int __team_option_inst_add_port(struct team *team, 210 struct team_port *port) 211 { 212 struct team_option *option; 213 int err; 214 215 list_for_each_entry(option, &team->option_list, list) { 216 if (!option->per_port) 217 continue; 218 err = __team_option_inst_add(team, option, port); 219 if (err) 220 goto inst_del_port; 221 } 222 return 0; 223 224 inst_del_port: 225 __team_option_inst_del_port(team, port); 226 return err; 227 } 228 229 static void __team_option_inst_mark_removed_port(struct team *team, 230 struct team_port *port) 231 { 232 struct team_option_inst *opt_inst; 233 234 list_for_each_entry(opt_inst, &team->option_inst_list, list) { 235 if (opt_inst->info.port == port) { 236 opt_inst->changed = true; 237 opt_inst->removed = true; 238 } 239 } 240 } 241 242 static int __team_options_register(struct team *team, 243 const struct team_option *option, 244 size_t option_count) 245 { 246 int i; 247 struct team_option **dst_opts; 248 int err; 249 250 dst_opts = kzalloc(sizeof(struct team_option *) * option_count, 251 GFP_KERNEL); 252 if (!dst_opts) 253 return -ENOMEM; 254 for (i = 0; i < option_count; i++, option++) { 255 if (__team_find_option(team, option->name)) { 256 err = -EEXIST; 257 goto alloc_rollback; 258 } 259 dst_opts[i] = kmemdup(option, sizeof(*option), GFP_KERNEL); 260 if (!dst_opts[i]) { 261 err = -ENOMEM; 262 goto alloc_rollback; 263 } 264 } 265 266 for (i = 0; i < option_count; i++) { 267 err = __team_option_inst_add_option(team, dst_opts[i]); 268 if (err) 269 goto inst_rollback; 270 list_add_tail(&dst_opts[i]->list, &team->option_list); 271 } 272 273 kfree(dst_opts); 274 return 0; 275 276 inst_rollback: 277 for (i--; i >= 0; i--) 278 __team_option_inst_del_option(team, dst_opts[i]); 279 280 i = option_count - 1; 281 alloc_rollback: 282 for (i--; i >= 0; i--) 283 kfree(dst_opts[i]); 284 285 kfree(dst_opts); 286 return err; 287 } 288 289 static void __team_options_mark_removed(struct team *team, 290 const struct team_option *option, 291 size_t option_count) 292 { 293 int i; 294 295 for (i = 0; i < option_count; i++, option++) { 296 struct team_option *del_opt; 297 298 del_opt = __team_find_option(team, option->name); 299 if (del_opt) 300 __team_option_inst_mark_removed_option(team, del_opt); 301 } 302 } 303 304 static void __team_options_unregister(struct team *team, 305 const struct team_option *option, 306 size_t option_count) 307 { 308 int i; 309 310 for (i = 0; i < option_count; i++, option++) { 311 struct team_option *del_opt; 312 313 del_opt = __team_find_option(team, option->name); 314 if (del_opt) { 315 __team_option_inst_del_option(team, del_opt); 316 list_del(&del_opt->list); 317 kfree(del_opt); 318 } 319 } 320 } 321 322 static void __team_options_change_check(struct team *team); 323 static void __team_option_inst_change(struct team *team, 324 struct team_option_inst *opt_inst); 325 326 int team_options_register(struct team *team, 327 const struct team_option *option, 328 size_t option_count) 329 { 330 int err; 331 332 err = __team_options_register(team, option, option_count); 333 if (err) 334 return err; 335 __team_options_change_check(team); 336 return 0; 337 } 338 EXPORT_SYMBOL(team_options_register); 339 340 void team_options_unregister(struct team *team, 341 const struct team_option *option, 342 size_t option_count) 343 { 344 __team_options_mark_removed(team, option, option_count); 345 __team_options_change_check(team); 346 __team_options_unregister(team, option, option_count); 347 } 348 EXPORT_SYMBOL(team_options_unregister); 349 350 static int team_option_get(struct team *team, 351 struct team_option_inst *opt_inst, 352 struct team_gsetter_ctx *ctx) 353 { 354 if (!opt_inst->option->getter) 355 return -EOPNOTSUPP; 356 return opt_inst->option->getter(team, ctx); 357 } 358 359 static int team_option_set(struct team *team, 360 struct team_option_inst *opt_inst, 361 struct team_gsetter_ctx *ctx) 362 { 363 int err; 364 365 if (!opt_inst->option->setter) 366 return -EOPNOTSUPP; 367 err = opt_inst->option->setter(team, ctx); 368 if (err) 369 return err; 370 371 __team_option_inst_change(team, opt_inst); 372 return err; 373 } 374 375 void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info) 376 { 377 struct team_option_inst *opt_inst; 378 379 opt_inst = container_of(opt_inst_info, struct team_option_inst, info); 380 opt_inst->changed = true; 381 } 382 EXPORT_SYMBOL(team_option_inst_set_change); 383 384 void team_options_change_check(struct team *team) 385 { 386 __team_options_change_check(team); 387 } 388 EXPORT_SYMBOL(team_options_change_check); 389 390 391 /**************** 392 * Mode handling 393 ****************/ 394 395 static LIST_HEAD(mode_list); 396 static DEFINE_SPINLOCK(mode_list_lock); 397 398 struct team_mode_item { 399 struct list_head list; 400 const struct team_mode *mode; 401 }; 402 403 static struct team_mode_item *__find_mode(const char *kind) 404 { 405 struct team_mode_item *mitem; 406 407 list_for_each_entry(mitem, &mode_list, list) { 408 if (strcmp(mitem->mode->kind, kind) == 0) 409 return mitem; 410 } 411 return NULL; 412 } 413 414 static bool is_good_mode_name(const char *name) 415 { 416 while (*name != '\0') { 417 if (!isalpha(*name) && !isdigit(*name) && *name != '_') 418 return false; 419 name++; 420 } 421 return true; 422 } 423 424 int team_mode_register(const struct team_mode *mode) 425 { 426 int err = 0; 427 struct team_mode_item *mitem; 428 429 if (!is_good_mode_name(mode->kind) || 430 mode->priv_size > TEAM_MODE_PRIV_SIZE) 431 return -EINVAL; 432 433 mitem = kmalloc(sizeof(*mitem), GFP_KERNEL); 434 if (!mitem) 435 return -ENOMEM; 436 437 spin_lock(&mode_list_lock); 438 if (__find_mode(mode->kind)) { 439 err = -EEXIST; 440 kfree(mitem); 441 goto unlock; 442 } 443 mitem->mode = mode; 444 list_add_tail(&mitem->list, &mode_list); 445 unlock: 446 spin_unlock(&mode_list_lock); 447 return err; 448 } 449 EXPORT_SYMBOL(team_mode_register); 450 451 void team_mode_unregister(const struct team_mode *mode) 452 { 453 struct team_mode_item *mitem; 454 455 spin_lock(&mode_list_lock); 456 mitem = __find_mode(mode->kind); 457 if (mitem) { 458 list_del_init(&mitem->list); 459 kfree(mitem); 460 } 461 spin_unlock(&mode_list_lock); 462 } 463 EXPORT_SYMBOL(team_mode_unregister); 464 465 static const struct team_mode *team_mode_get(const char *kind) 466 { 467 struct team_mode_item *mitem; 468 const struct team_mode *mode = NULL; 469 470 spin_lock(&mode_list_lock); 471 mitem = __find_mode(kind); 472 if (!mitem) { 473 spin_unlock(&mode_list_lock); 474 request_module("team-mode-%s", kind); 475 spin_lock(&mode_list_lock); 476 mitem = __find_mode(kind); 477 } 478 if (mitem) { 479 mode = mitem->mode; 480 if (!try_module_get(mode->owner)) 481 mode = NULL; 482 } 483 484 spin_unlock(&mode_list_lock); 485 return mode; 486 } 487 488 static void team_mode_put(const struct team_mode *mode) 489 { 490 module_put(mode->owner); 491 } 492 493 static bool team_dummy_transmit(struct team *team, struct sk_buff *skb) 494 { 495 dev_kfree_skb_any(skb); 496 return false; 497 } 498 499 rx_handler_result_t team_dummy_receive(struct team *team, 500 struct team_port *port, 501 struct sk_buff *skb) 502 { 503 return RX_HANDLER_ANOTHER; 504 } 505 506 static const struct team_mode __team_no_mode = { 507 .kind = "*NOMODE*", 508 }; 509 510 static bool team_is_mode_set(struct team *team) 511 { 512 return team->mode != &__team_no_mode; 513 } 514 515 static void team_set_no_mode(struct team *team) 516 { 517 team->mode = &__team_no_mode; 518 } 519 520 static void team_adjust_ops(struct team *team) 521 { 522 /* 523 * To avoid checks in rx/tx skb paths, ensure here that non-null and 524 * correct ops are always set. 525 */ 526 527 if (list_empty(&team->port_list) || 528 !team_is_mode_set(team) || !team->mode->ops->transmit) 529 team->ops.transmit = team_dummy_transmit; 530 else 531 team->ops.transmit = team->mode->ops->transmit; 532 533 if (list_empty(&team->port_list) || 534 !team_is_mode_set(team) || !team->mode->ops->receive) 535 team->ops.receive = team_dummy_receive; 536 else 537 team->ops.receive = team->mode->ops->receive; 538 } 539 540 /* 541 * We can benefit from the fact that it's ensured no port is present 542 * at the time of mode change. Therefore no packets are in fly so there's no 543 * need to set mode operations in any special way. 544 */ 545 static int __team_change_mode(struct team *team, 546 const struct team_mode *new_mode) 547 { 548 /* Check if mode was previously set and do cleanup if so */ 549 if (team_is_mode_set(team)) { 550 void (*exit_op)(struct team *team) = team->ops.exit; 551 552 /* Clear ops area so no callback is called any longer */ 553 memset(&team->ops, 0, sizeof(struct team_mode_ops)); 554 team_adjust_ops(team); 555 556 if (exit_op) 557 exit_op(team); 558 team_mode_put(team->mode); 559 team_set_no_mode(team); 560 /* zero private data area */ 561 memset(&team->mode_priv, 0, 562 sizeof(struct team) - offsetof(struct team, mode_priv)); 563 } 564 565 if (!new_mode) 566 return 0; 567 568 if (new_mode->ops->init) { 569 int err; 570 571 err = new_mode->ops->init(team); 572 if (err) 573 return err; 574 } 575 576 team->mode = new_mode; 577 memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops)); 578 team_adjust_ops(team); 579 580 return 0; 581 } 582 583 static int team_change_mode(struct team *team, const char *kind) 584 { 585 const struct team_mode *new_mode; 586 struct net_device *dev = team->dev; 587 int err; 588 589 if (!list_empty(&team->port_list)) { 590 netdev_err(dev, "No ports can be present during mode change\n"); 591 return -EBUSY; 592 } 593 594 if (team_is_mode_set(team) && strcmp(team->mode->kind, kind) == 0) { 595 netdev_err(dev, "Unable to change to the same mode the team is in\n"); 596 return -EINVAL; 597 } 598 599 new_mode = team_mode_get(kind); 600 if (!new_mode) { 601 netdev_err(dev, "Mode \"%s\" not found\n", kind); 602 return -EINVAL; 603 } 604 605 err = __team_change_mode(team, new_mode); 606 if (err) { 607 netdev_err(dev, "Failed to change to mode \"%s\"\n", kind); 608 team_mode_put(new_mode); 609 return err; 610 } 611 612 netdev_info(dev, "Mode changed to \"%s\"\n", kind); 613 return 0; 614 } 615 616 617 /************************ 618 * Rx path frame handler 619 ************************/ 620 621 static bool team_port_enabled(struct team_port *port); 622 623 /* note: already called with rcu_read_lock */ 624 static rx_handler_result_t team_handle_frame(struct sk_buff **pskb) 625 { 626 struct sk_buff *skb = *pskb; 627 struct team_port *port; 628 struct team *team; 629 rx_handler_result_t res; 630 631 skb = skb_share_check(skb, GFP_ATOMIC); 632 if (!skb) 633 return RX_HANDLER_CONSUMED; 634 635 *pskb = skb; 636 637 port = team_port_get_rcu(skb->dev); 638 team = port->team; 639 if (!team_port_enabled(port)) { 640 /* allow exact match delivery for disabled ports */ 641 res = RX_HANDLER_EXACT; 642 } else { 643 res = team->ops.receive(team, port, skb); 644 } 645 if (res == RX_HANDLER_ANOTHER) { 646 struct team_pcpu_stats *pcpu_stats; 647 648 pcpu_stats = this_cpu_ptr(team->pcpu_stats); 649 u64_stats_update_begin(&pcpu_stats->syncp); 650 pcpu_stats->rx_packets++; 651 pcpu_stats->rx_bytes += skb->len; 652 if (skb->pkt_type == PACKET_MULTICAST) 653 pcpu_stats->rx_multicast++; 654 u64_stats_update_end(&pcpu_stats->syncp); 655 656 skb->dev = team->dev; 657 } else { 658 this_cpu_inc(team->pcpu_stats->rx_dropped); 659 } 660 661 return res; 662 } 663 664 665 /**************** 666 * Port handling 667 ****************/ 668 669 static bool team_port_find(const struct team *team, 670 const struct team_port *port) 671 { 672 struct team_port *cur; 673 674 list_for_each_entry(cur, &team->port_list, list) 675 if (cur == port) 676 return true; 677 return false; 678 } 679 680 static bool team_port_enabled(struct team_port *port) 681 { 682 return port->index != -1; 683 } 684 685 /* 686 * Enable/disable port by adding to enabled port hashlist and setting 687 * port->index (Might be racy so reader could see incorrect ifindex when 688 * processing a flying packet, but that is not a problem). Write guarded 689 * by team->lock. 690 */ 691 static void team_port_enable(struct team *team, 692 struct team_port *port) 693 { 694 if (team_port_enabled(port)) 695 return; 696 port->index = team->en_port_count++; 697 hlist_add_head_rcu(&port->hlist, 698 team_port_index_hash(team, port->index)); 699 if (team->ops.port_enabled) 700 team->ops.port_enabled(team, port); 701 } 702 703 static void __reconstruct_port_hlist(struct team *team, int rm_index) 704 { 705 int i; 706 struct team_port *port; 707 708 for (i = rm_index + 1; i < team->en_port_count; i++) { 709 port = team_get_port_by_index(team, i); 710 hlist_del_rcu(&port->hlist); 711 port->index--; 712 hlist_add_head_rcu(&port->hlist, 713 team_port_index_hash(team, port->index)); 714 } 715 } 716 717 static void team_port_disable(struct team *team, 718 struct team_port *port) 719 { 720 int rm_index = port->index; 721 722 if (!team_port_enabled(port)) 723 return; 724 if (team->ops.port_disabled) 725 team->ops.port_disabled(team, port); 726 hlist_del_rcu(&port->hlist); 727 __reconstruct_port_hlist(team, rm_index); 728 team->en_port_count--; 729 port->index = -1; 730 } 731 732 #define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \ 733 NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \ 734 NETIF_F_HIGHDMA | NETIF_F_LRO) 735 736 static void __team_compute_features(struct team *team) 737 { 738 struct team_port *port; 739 u32 vlan_features = TEAM_VLAN_FEATURES; 740 unsigned short max_hard_header_len = ETH_HLEN; 741 742 list_for_each_entry(port, &team->port_list, list) { 743 vlan_features = netdev_increment_features(vlan_features, 744 port->dev->vlan_features, 745 TEAM_VLAN_FEATURES); 746 747 if (port->dev->hard_header_len > max_hard_header_len) 748 max_hard_header_len = port->dev->hard_header_len; 749 } 750 751 team->dev->vlan_features = vlan_features; 752 team->dev->hard_header_len = max_hard_header_len; 753 754 netdev_change_features(team->dev); 755 } 756 757 static void team_compute_features(struct team *team) 758 { 759 mutex_lock(&team->lock); 760 __team_compute_features(team); 761 mutex_unlock(&team->lock); 762 } 763 764 static int team_port_enter(struct team *team, struct team_port *port) 765 { 766 int err = 0; 767 768 dev_hold(team->dev); 769 port->dev->priv_flags |= IFF_TEAM_PORT; 770 if (team->ops.port_enter) { 771 err = team->ops.port_enter(team, port); 772 if (err) { 773 netdev_err(team->dev, "Device %s failed to enter team mode\n", 774 port->dev->name); 775 goto err_port_enter; 776 } 777 } 778 779 return 0; 780 781 err_port_enter: 782 port->dev->priv_flags &= ~IFF_TEAM_PORT; 783 dev_put(team->dev); 784 785 return err; 786 } 787 788 static void team_port_leave(struct team *team, struct team_port *port) 789 { 790 if (team->ops.port_leave) 791 team->ops.port_leave(team, port); 792 port->dev->priv_flags &= ~IFF_TEAM_PORT; 793 dev_put(team->dev); 794 } 795 796 static void __team_port_change_check(struct team_port *port, bool linkup); 797 798 static int team_port_add(struct team *team, struct net_device *port_dev) 799 { 800 struct net_device *dev = team->dev; 801 struct team_port *port; 802 char *portname = port_dev->name; 803 int err; 804 805 if (port_dev->flags & IFF_LOOPBACK || 806 port_dev->type != ARPHRD_ETHER) { 807 netdev_err(dev, "Device %s is of an unsupported type\n", 808 portname); 809 return -EINVAL; 810 } 811 812 if (team_port_exists(port_dev)) { 813 netdev_err(dev, "Device %s is already a port " 814 "of a team device\n", portname); 815 return -EBUSY; 816 } 817 818 if (port_dev->flags & IFF_UP) { 819 netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n", 820 portname); 821 return -EBUSY; 822 } 823 824 port = kzalloc(sizeof(struct team_port) + team->mode->port_priv_size, 825 GFP_KERNEL); 826 if (!port) 827 return -ENOMEM; 828 829 port->dev = port_dev; 830 port->team = team; 831 832 port->orig.mtu = port_dev->mtu; 833 err = dev_set_mtu(port_dev, dev->mtu); 834 if (err) { 835 netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err); 836 goto err_set_mtu; 837 } 838 839 memcpy(port->orig.dev_addr, port_dev->dev_addr, ETH_ALEN); 840 841 err = team_port_enter(team, port); 842 if (err) { 843 netdev_err(dev, "Device %s failed to enter team mode\n", 844 portname); 845 goto err_port_enter; 846 } 847 848 err = dev_open(port_dev); 849 if (err) { 850 netdev_dbg(dev, "Device %s opening failed\n", 851 portname); 852 goto err_dev_open; 853 } 854 855 err = vlan_vids_add_by_dev(port_dev, dev); 856 if (err) { 857 netdev_err(dev, "Failed to add vlan ids to device %s\n", 858 portname); 859 goto err_vids_add; 860 } 861 862 err = netdev_set_master(port_dev, dev); 863 if (err) { 864 netdev_err(dev, "Device %s failed to set master\n", portname); 865 goto err_set_master; 866 } 867 868 err = netdev_rx_handler_register(port_dev, team_handle_frame, 869 port); 870 if (err) { 871 netdev_err(dev, "Device %s failed to register rx_handler\n", 872 portname); 873 goto err_handler_register; 874 } 875 876 err = __team_option_inst_add_port(team, port); 877 if (err) { 878 netdev_err(dev, "Device %s failed to add per-port options\n", 879 portname); 880 goto err_option_port_add; 881 } 882 883 port->index = -1; 884 team_port_enable(team, port); 885 list_add_tail_rcu(&port->list, &team->port_list); 886 team_adjust_ops(team); 887 __team_compute_features(team); 888 __team_port_change_check(port, !!netif_carrier_ok(port_dev)); 889 __team_options_change_check(team); 890 891 netdev_info(dev, "Port device %s added\n", portname); 892 893 return 0; 894 895 err_option_port_add: 896 netdev_rx_handler_unregister(port_dev); 897 898 err_handler_register: 899 netdev_set_master(port_dev, NULL); 900 901 err_set_master: 902 vlan_vids_del_by_dev(port_dev, dev); 903 904 err_vids_add: 905 dev_close(port_dev); 906 907 err_dev_open: 908 team_port_leave(team, port); 909 team_port_set_orig_mac(port); 910 911 err_port_enter: 912 dev_set_mtu(port_dev, port->orig.mtu); 913 914 err_set_mtu: 915 kfree(port); 916 917 return err; 918 } 919 920 static int team_port_del(struct team *team, struct net_device *port_dev) 921 { 922 struct net_device *dev = team->dev; 923 struct team_port *port; 924 char *portname = port_dev->name; 925 926 port = team_port_get_rtnl(port_dev); 927 if (!port || !team_port_find(team, port)) { 928 netdev_err(dev, "Device %s does not act as a port of this team\n", 929 portname); 930 return -ENOENT; 931 } 932 933 __team_option_inst_mark_removed_port(team, port); 934 __team_options_change_check(team); 935 __team_option_inst_del_port(team, port); 936 port->removed = true; 937 __team_port_change_check(port, false); 938 team_port_disable(team, port); 939 list_del_rcu(&port->list); 940 team_adjust_ops(team); 941 netdev_rx_handler_unregister(port_dev); 942 netdev_set_master(port_dev, NULL); 943 vlan_vids_del_by_dev(port_dev, dev); 944 dev_close(port_dev); 945 team_port_leave(team, port); 946 team_port_set_orig_mac(port); 947 dev_set_mtu(port_dev, port->orig.mtu); 948 synchronize_rcu(); 949 kfree(port); 950 netdev_info(dev, "Port device %s removed\n", portname); 951 __team_compute_features(team); 952 953 return 0; 954 } 955 956 957 /***************** 958 * Net device ops 959 *****************/ 960 961 static int team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx) 962 { 963 ctx->data.str_val = team->mode->kind; 964 return 0; 965 } 966 967 static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx) 968 { 969 return team_change_mode(team, ctx->data.str_val); 970 } 971 972 static int team_port_en_option_get(struct team *team, 973 struct team_gsetter_ctx *ctx) 974 { 975 struct team_port *port = ctx->info->port; 976 977 ctx->data.bool_val = team_port_enabled(port); 978 return 0; 979 } 980 981 static int team_port_en_option_set(struct team *team, 982 struct team_gsetter_ctx *ctx) 983 { 984 struct team_port *port = ctx->info->port; 985 986 if (ctx->data.bool_val) 987 team_port_enable(team, port); 988 else 989 team_port_disable(team, port); 990 return 0; 991 } 992 993 static int team_user_linkup_option_get(struct team *team, 994 struct team_gsetter_ctx *ctx) 995 { 996 struct team_port *port = ctx->info->port; 997 998 ctx->data.bool_val = port->user.linkup; 999 return 0; 1000 } 1001 1002 static int team_user_linkup_option_set(struct team *team, 1003 struct team_gsetter_ctx *ctx) 1004 { 1005 struct team_port *port = ctx->info->port; 1006 1007 port->user.linkup = ctx->data.bool_val; 1008 team_refresh_port_linkup(port); 1009 return 0; 1010 } 1011 1012 static int team_user_linkup_en_option_get(struct team *team, 1013 struct team_gsetter_ctx *ctx) 1014 { 1015 struct team_port *port = ctx->info->port; 1016 1017 ctx->data.bool_val = port->user.linkup_enabled; 1018 return 0; 1019 } 1020 1021 static int team_user_linkup_en_option_set(struct team *team, 1022 struct team_gsetter_ctx *ctx) 1023 { 1024 struct team_port *port = ctx->info->port; 1025 1026 port->user.linkup_enabled = ctx->data.bool_val; 1027 team_refresh_port_linkup(port); 1028 return 0; 1029 } 1030 1031 static const struct team_option team_options[] = { 1032 { 1033 .name = "mode", 1034 .type = TEAM_OPTION_TYPE_STRING, 1035 .getter = team_mode_option_get, 1036 .setter = team_mode_option_set, 1037 }, 1038 { 1039 .name = "enabled", 1040 .type = TEAM_OPTION_TYPE_BOOL, 1041 .per_port = true, 1042 .getter = team_port_en_option_get, 1043 .setter = team_port_en_option_set, 1044 }, 1045 { 1046 .name = "user_linkup", 1047 .type = TEAM_OPTION_TYPE_BOOL, 1048 .per_port = true, 1049 .getter = team_user_linkup_option_get, 1050 .setter = team_user_linkup_option_set, 1051 }, 1052 { 1053 .name = "user_linkup_enabled", 1054 .type = TEAM_OPTION_TYPE_BOOL, 1055 .per_port = true, 1056 .getter = team_user_linkup_en_option_get, 1057 .setter = team_user_linkup_en_option_set, 1058 }, 1059 }; 1060 1061 static int team_init(struct net_device *dev) 1062 { 1063 struct team *team = netdev_priv(dev); 1064 int i; 1065 int err; 1066 1067 team->dev = dev; 1068 mutex_init(&team->lock); 1069 team_set_no_mode(team); 1070 1071 team->pcpu_stats = alloc_percpu(struct team_pcpu_stats); 1072 if (!team->pcpu_stats) 1073 return -ENOMEM; 1074 1075 for (i = 0; i < TEAM_PORT_HASHENTRIES; i++) 1076 INIT_HLIST_HEAD(&team->en_port_hlist[i]); 1077 INIT_LIST_HEAD(&team->port_list); 1078 1079 team_adjust_ops(team); 1080 1081 INIT_LIST_HEAD(&team->option_list); 1082 INIT_LIST_HEAD(&team->option_inst_list); 1083 err = team_options_register(team, team_options, ARRAY_SIZE(team_options)); 1084 if (err) 1085 goto err_options_register; 1086 netif_carrier_off(dev); 1087 1088 return 0; 1089 1090 err_options_register: 1091 free_percpu(team->pcpu_stats); 1092 1093 return err; 1094 } 1095 1096 static void team_uninit(struct net_device *dev) 1097 { 1098 struct team *team = netdev_priv(dev); 1099 struct team_port *port; 1100 struct team_port *tmp; 1101 1102 mutex_lock(&team->lock); 1103 list_for_each_entry_safe(port, tmp, &team->port_list, list) 1104 team_port_del(team, port->dev); 1105 1106 __team_change_mode(team, NULL); /* cleanup */ 1107 __team_options_unregister(team, team_options, ARRAY_SIZE(team_options)); 1108 mutex_unlock(&team->lock); 1109 } 1110 1111 static void team_destructor(struct net_device *dev) 1112 { 1113 struct team *team = netdev_priv(dev); 1114 1115 free_percpu(team->pcpu_stats); 1116 free_netdev(dev); 1117 } 1118 1119 static int team_open(struct net_device *dev) 1120 { 1121 netif_carrier_on(dev); 1122 return 0; 1123 } 1124 1125 static int team_close(struct net_device *dev) 1126 { 1127 netif_carrier_off(dev); 1128 return 0; 1129 } 1130 1131 /* 1132 * note: already called with rcu_read_lock 1133 */ 1134 static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev) 1135 { 1136 struct team *team = netdev_priv(dev); 1137 bool tx_success = false; 1138 unsigned int len = skb->len; 1139 1140 tx_success = team->ops.transmit(team, skb); 1141 if (tx_success) { 1142 struct team_pcpu_stats *pcpu_stats; 1143 1144 pcpu_stats = this_cpu_ptr(team->pcpu_stats); 1145 u64_stats_update_begin(&pcpu_stats->syncp); 1146 pcpu_stats->tx_packets++; 1147 pcpu_stats->tx_bytes += len; 1148 u64_stats_update_end(&pcpu_stats->syncp); 1149 } else { 1150 this_cpu_inc(team->pcpu_stats->tx_dropped); 1151 } 1152 1153 return NETDEV_TX_OK; 1154 } 1155 1156 static void team_change_rx_flags(struct net_device *dev, int change) 1157 { 1158 struct team *team = netdev_priv(dev); 1159 struct team_port *port; 1160 int inc; 1161 1162 rcu_read_lock(); 1163 list_for_each_entry_rcu(port, &team->port_list, list) { 1164 if (change & IFF_PROMISC) { 1165 inc = dev->flags & IFF_PROMISC ? 1 : -1; 1166 dev_set_promiscuity(port->dev, inc); 1167 } 1168 if (change & IFF_ALLMULTI) { 1169 inc = dev->flags & IFF_ALLMULTI ? 1 : -1; 1170 dev_set_allmulti(port->dev, inc); 1171 } 1172 } 1173 rcu_read_unlock(); 1174 } 1175 1176 static void team_set_rx_mode(struct net_device *dev) 1177 { 1178 struct team *team = netdev_priv(dev); 1179 struct team_port *port; 1180 1181 rcu_read_lock(); 1182 list_for_each_entry_rcu(port, &team->port_list, list) { 1183 dev_uc_sync(port->dev, dev); 1184 dev_mc_sync(port->dev, dev); 1185 } 1186 rcu_read_unlock(); 1187 } 1188 1189 static int team_set_mac_address(struct net_device *dev, void *p) 1190 { 1191 struct team *team = netdev_priv(dev); 1192 struct team_port *port; 1193 struct sockaddr *addr = p; 1194 1195 dev->addr_assign_type &= ~NET_ADDR_RANDOM; 1196 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN); 1197 rcu_read_lock(); 1198 list_for_each_entry_rcu(port, &team->port_list, list) 1199 if (team->ops.port_change_mac) 1200 team->ops.port_change_mac(team, port); 1201 rcu_read_unlock(); 1202 return 0; 1203 } 1204 1205 static int team_change_mtu(struct net_device *dev, int new_mtu) 1206 { 1207 struct team *team = netdev_priv(dev); 1208 struct team_port *port; 1209 int err; 1210 1211 /* 1212 * Alhough this is reader, it's guarded by team lock. It's not possible 1213 * to traverse list in reverse under rcu_read_lock 1214 */ 1215 mutex_lock(&team->lock); 1216 list_for_each_entry(port, &team->port_list, list) { 1217 err = dev_set_mtu(port->dev, new_mtu); 1218 if (err) { 1219 netdev_err(dev, "Device %s failed to change mtu", 1220 port->dev->name); 1221 goto unwind; 1222 } 1223 } 1224 mutex_unlock(&team->lock); 1225 1226 dev->mtu = new_mtu; 1227 1228 return 0; 1229 1230 unwind: 1231 list_for_each_entry_continue_reverse(port, &team->port_list, list) 1232 dev_set_mtu(port->dev, dev->mtu); 1233 mutex_unlock(&team->lock); 1234 1235 return err; 1236 } 1237 1238 static struct rtnl_link_stats64 * 1239 team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) 1240 { 1241 struct team *team = netdev_priv(dev); 1242 struct team_pcpu_stats *p; 1243 u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes; 1244 u32 rx_dropped = 0, tx_dropped = 0; 1245 unsigned int start; 1246 int i; 1247 1248 for_each_possible_cpu(i) { 1249 p = per_cpu_ptr(team->pcpu_stats, i); 1250 do { 1251 start = u64_stats_fetch_begin_bh(&p->syncp); 1252 rx_packets = p->rx_packets; 1253 rx_bytes = p->rx_bytes; 1254 rx_multicast = p->rx_multicast; 1255 tx_packets = p->tx_packets; 1256 tx_bytes = p->tx_bytes; 1257 } while (u64_stats_fetch_retry_bh(&p->syncp, start)); 1258 1259 stats->rx_packets += rx_packets; 1260 stats->rx_bytes += rx_bytes; 1261 stats->multicast += rx_multicast; 1262 stats->tx_packets += tx_packets; 1263 stats->tx_bytes += tx_bytes; 1264 /* 1265 * rx_dropped & tx_dropped are u32, updated 1266 * without syncp protection. 1267 */ 1268 rx_dropped += p->rx_dropped; 1269 tx_dropped += p->tx_dropped; 1270 } 1271 stats->rx_dropped = rx_dropped; 1272 stats->tx_dropped = tx_dropped; 1273 return stats; 1274 } 1275 1276 static int team_vlan_rx_add_vid(struct net_device *dev, uint16_t vid) 1277 { 1278 struct team *team = netdev_priv(dev); 1279 struct team_port *port; 1280 int err; 1281 1282 /* 1283 * Alhough this is reader, it's guarded by team lock. It's not possible 1284 * to traverse list in reverse under rcu_read_lock 1285 */ 1286 mutex_lock(&team->lock); 1287 list_for_each_entry(port, &team->port_list, list) { 1288 err = vlan_vid_add(port->dev, vid); 1289 if (err) 1290 goto unwind; 1291 } 1292 mutex_unlock(&team->lock); 1293 1294 return 0; 1295 1296 unwind: 1297 list_for_each_entry_continue_reverse(port, &team->port_list, list) 1298 vlan_vid_del(port->dev, vid); 1299 mutex_unlock(&team->lock); 1300 1301 return err; 1302 } 1303 1304 static int team_vlan_rx_kill_vid(struct net_device *dev, uint16_t vid) 1305 { 1306 struct team *team = netdev_priv(dev); 1307 struct team_port *port; 1308 1309 rcu_read_lock(); 1310 list_for_each_entry_rcu(port, &team->port_list, list) 1311 vlan_vid_del(port->dev, vid); 1312 rcu_read_unlock(); 1313 1314 return 0; 1315 } 1316 1317 static int team_add_slave(struct net_device *dev, struct net_device *port_dev) 1318 { 1319 struct team *team = netdev_priv(dev); 1320 int err; 1321 1322 mutex_lock(&team->lock); 1323 err = team_port_add(team, port_dev); 1324 mutex_unlock(&team->lock); 1325 return err; 1326 } 1327 1328 static int team_del_slave(struct net_device *dev, struct net_device *port_dev) 1329 { 1330 struct team *team = netdev_priv(dev); 1331 int err; 1332 1333 mutex_lock(&team->lock); 1334 err = team_port_del(team, port_dev); 1335 mutex_unlock(&team->lock); 1336 return err; 1337 } 1338 1339 static netdev_features_t team_fix_features(struct net_device *dev, 1340 netdev_features_t features) 1341 { 1342 struct team_port *port; 1343 struct team *team = netdev_priv(dev); 1344 netdev_features_t mask; 1345 1346 mask = features; 1347 features &= ~NETIF_F_ONE_FOR_ALL; 1348 features |= NETIF_F_ALL_FOR_ALL; 1349 1350 rcu_read_lock(); 1351 list_for_each_entry_rcu(port, &team->port_list, list) { 1352 features = netdev_increment_features(features, 1353 port->dev->features, 1354 mask); 1355 } 1356 rcu_read_unlock(); 1357 return features; 1358 } 1359 1360 static const struct net_device_ops team_netdev_ops = { 1361 .ndo_init = team_init, 1362 .ndo_uninit = team_uninit, 1363 .ndo_open = team_open, 1364 .ndo_stop = team_close, 1365 .ndo_start_xmit = team_xmit, 1366 .ndo_change_rx_flags = team_change_rx_flags, 1367 .ndo_set_rx_mode = team_set_rx_mode, 1368 .ndo_set_mac_address = team_set_mac_address, 1369 .ndo_change_mtu = team_change_mtu, 1370 .ndo_get_stats64 = team_get_stats64, 1371 .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid, 1372 .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid, 1373 .ndo_add_slave = team_add_slave, 1374 .ndo_del_slave = team_del_slave, 1375 .ndo_fix_features = team_fix_features, 1376 }; 1377 1378 1379 /*********************** 1380 * rt netlink interface 1381 ***********************/ 1382 1383 static void team_setup(struct net_device *dev) 1384 { 1385 ether_setup(dev); 1386 1387 dev->netdev_ops = &team_netdev_ops; 1388 dev->destructor = team_destructor; 1389 dev->tx_queue_len = 0; 1390 dev->flags |= IFF_MULTICAST; 1391 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING); 1392 1393 /* 1394 * Indicate we support unicast address filtering. That way core won't 1395 * bring us to promisc mode in case a unicast addr is added. 1396 * Let this up to underlay drivers. 1397 */ 1398 dev->priv_flags |= IFF_UNICAST_FLT; 1399 1400 dev->features |= NETIF_F_LLTX; 1401 dev->features |= NETIF_F_GRO; 1402 dev->hw_features = NETIF_F_HW_VLAN_TX | 1403 NETIF_F_HW_VLAN_RX | 1404 NETIF_F_HW_VLAN_FILTER; 1405 1406 dev->features |= dev->hw_features; 1407 } 1408 1409 static int team_newlink(struct net *src_net, struct net_device *dev, 1410 struct nlattr *tb[], struct nlattr *data[]) 1411 { 1412 int err; 1413 1414 if (tb[IFLA_ADDRESS] == NULL) 1415 eth_hw_addr_random(dev); 1416 1417 err = register_netdevice(dev); 1418 if (err) 1419 return err; 1420 1421 return 0; 1422 } 1423 1424 static int team_validate(struct nlattr *tb[], struct nlattr *data[]) 1425 { 1426 if (tb[IFLA_ADDRESS]) { 1427 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) 1428 return -EINVAL; 1429 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) 1430 return -EADDRNOTAVAIL; 1431 } 1432 return 0; 1433 } 1434 1435 static struct rtnl_link_ops team_link_ops __read_mostly = { 1436 .kind = DRV_NAME, 1437 .priv_size = sizeof(struct team), 1438 .setup = team_setup, 1439 .newlink = team_newlink, 1440 .validate = team_validate, 1441 }; 1442 1443 1444 /*********************************** 1445 * Generic netlink custom interface 1446 ***********************************/ 1447 1448 static struct genl_family team_nl_family = { 1449 .id = GENL_ID_GENERATE, 1450 .name = TEAM_GENL_NAME, 1451 .version = TEAM_GENL_VERSION, 1452 .maxattr = TEAM_ATTR_MAX, 1453 .netnsok = true, 1454 }; 1455 1456 static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = { 1457 [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, }, 1458 [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 }, 1459 [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED }, 1460 [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED }, 1461 }; 1462 1463 static const struct nla_policy 1464 team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = { 1465 [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, }, 1466 [TEAM_ATTR_OPTION_NAME] = { 1467 .type = NLA_STRING, 1468 .len = TEAM_STRING_MAX_LEN, 1469 }, 1470 [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG }, 1471 [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 }, 1472 [TEAM_ATTR_OPTION_DATA] = { .type = NLA_BINARY }, 1473 }; 1474 1475 static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info) 1476 { 1477 struct sk_buff *msg; 1478 void *hdr; 1479 int err; 1480 1481 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); 1482 if (!msg) 1483 return -ENOMEM; 1484 1485 hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq, 1486 &team_nl_family, 0, TEAM_CMD_NOOP); 1487 if (IS_ERR(hdr)) { 1488 err = PTR_ERR(hdr); 1489 goto err_msg_put; 1490 } 1491 1492 genlmsg_end(msg, hdr); 1493 1494 return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid); 1495 1496 err_msg_put: 1497 nlmsg_free(msg); 1498 1499 return err; 1500 } 1501 1502 /* 1503 * Netlink cmd functions should be locked by following two functions. 1504 * Since dev gets held here, that ensures dev won't disappear in between. 1505 */ 1506 static struct team *team_nl_team_get(struct genl_info *info) 1507 { 1508 struct net *net = genl_info_net(info); 1509 int ifindex; 1510 struct net_device *dev; 1511 struct team *team; 1512 1513 if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX]) 1514 return NULL; 1515 1516 ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]); 1517 dev = dev_get_by_index(net, ifindex); 1518 if (!dev || dev->netdev_ops != &team_netdev_ops) { 1519 if (dev) 1520 dev_put(dev); 1521 return NULL; 1522 } 1523 1524 team = netdev_priv(dev); 1525 mutex_lock(&team->lock); 1526 return team; 1527 } 1528 1529 static void team_nl_team_put(struct team *team) 1530 { 1531 mutex_unlock(&team->lock); 1532 dev_put(team->dev); 1533 } 1534 1535 static int team_nl_send_generic(struct genl_info *info, struct team *team, 1536 int (*fill_func)(struct sk_buff *skb, 1537 struct genl_info *info, 1538 int flags, struct team *team)) 1539 { 1540 struct sk_buff *skb; 1541 int err; 1542 1543 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); 1544 if (!skb) 1545 return -ENOMEM; 1546 1547 err = fill_func(skb, info, NLM_F_ACK, team); 1548 if (err < 0) 1549 goto err_fill; 1550 1551 err = genlmsg_unicast(genl_info_net(info), skb, info->snd_pid); 1552 return err; 1553 1554 err_fill: 1555 nlmsg_free(skb); 1556 return err; 1557 } 1558 1559 typedef int team_nl_send_func_t(struct sk_buff *skb, 1560 struct team *team, u32 pid); 1561 1562 static int team_nl_send_unicast(struct sk_buff *skb, struct team *team, u32 pid) 1563 { 1564 return genlmsg_unicast(dev_net(team->dev), skb, pid); 1565 } 1566 1567 static int team_nl_fill_one_option_get(struct sk_buff *skb, struct team *team, 1568 struct team_option_inst *opt_inst) 1569 { 1570 struct nlattr *option_item; 1571 struct team_option *option = opt_inst->option; 1572 struct team_option_inst_info *opt_inst_info = &opt_inst->info; 1573 struct team_gsetter_ctx ctx; 1574 int err; 1575 1576 ctx.info = opt_inst_info; 1577 err = team_option_get(team, opt_inst, &ctx); 1578 if (err) 1579 return err; 1580 1581 option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION); 1582 if (!option_item) 1583 return -EMSGSIZE; 1584 1585 if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name)) 1586 goto nest_cancel; 1587 if (opt_inst_info->port && 1588 nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX, 1589 opt_inst_info->port->dev->ifindex)) 1590 goto nest_cancel; 1591 if (opt_inst->option->array_size && 1592 nla_put_u32(skb, TEAM_ATTR_OPTION_ARRAY_INDEX, 1593 opt_inst_info->array_index)) 1594 goto nest_cancel; 1595 1596 switch (option->type) { 1597 case TEAM_OPTION_TYPE_U32: 1598 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32)) 1599 goto nest_cancel; 1600 if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.u32_val)) 1601 goto nest_cancel; 1602 break; 1603 case TEAM_OPTION_TYPE_STRING: 1604 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING)) 1605 goto nest_cancel; 1606 if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA, 1607 ctx.data.str_val)) 1608 goto nest_cancel; 1609 break; 1610 case TEAM_OPTION_TYPE_BINARY: 1611 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY)) 1612 goto nest_cancel; 1613 if (nla_put(skb, TEAM_ATTR_OPTION_DATA, ctx.data.bin_val.len, 1614 ctx.data.bin_val.ptr)) 1615 goto nest_cancel; 1616 break; 1617 case TEAM_OPTION_TYPE_BOOL: 1618 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG)) 1619 goto nest_cancel; 1620 if (ctx.data.bool_val && 1621 nla_put_flag(skb, TEAM_ATTR_OPTION_DATA)) 1622 goto nest_cancel; 1623 break; 1624 default: 1625 BUG(); 1626 } 1627 if (opt_inst->removed && nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED)) 1628 goto nest_cancel; 1629 if (opt_inst->changed) { 1630 if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED)) 1631 goto nest_cancel; 1632 opt_inst->changed = false; 1633 } 1634 nla_nest_end(skb, option_item); 1635 return 0; 1636 1637 nest_cancel: 1638 nla_nest_cancel(skb, option_item); 1639 return -EMSGSIZE; 1640 } 1641 1642 static int __send_and_alloc_skb(struct sk_buff **pskb, 1643 struct team *team, u32 pid, 1644 team_nl_send_func_t *send_func) 1645 { 1646 int err; 1647 1648 if (*pskb) { 1649 err = send_func(*pskb, team, pid); 1650 if (err) 1651 return err; 1652 } 1653 *pskb = genlmsg_new(NLMSG_DEFAULT_SIZE - GENL_HDRLEN, GFP_KERNEL); 1654 if (!*pskb) 1655 return -ENOMEM; 1656 return 0; 1657 } 1658 1659 static int team_nl_send_options_get(struct team *team, u32 pid, u32 seq, 1660 int flags, team_nl_send_func_t *send_func, 1661 struct list_head *sel_opt_inst_list) 1662 { 1663 struct nlattr *option_list; 1664 struct nlmsghdr *nlh; 1665 void *hdr; 1666 struct team_option_inst *opt_inst; 1667 int err; 1668 struct sk_buff *skb = NULL; 1669 bool incomplete; 1670 int i; 1671 1672 opt_inst = list_first_entry(sel_opt_inst_list, 1673 struct team_option_inst, tmp_list); 1674 1675 start_again: 1676 err = __send_and_alloc_skb(&skb, team, pid, send_func); 1677 if (err) 1678 return err; 1679 1680 hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags | NLM_F_MULTI, 1681 TEAM_CMD_OPTIONS_GET); 1682 if (IS_ERR(hdr)) 1683 return PTR_ERR(hdr); 1684 1685 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex)) 1686 goto nla_put_failure; 1687 option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION); 1688 if (!option_list) 1689 goto nla_put_failure; 1690 1691 i = 0; 1692 incomplete = false; 1693 list_for_each_entry_from(opt_inst, sel_opt_inst_list, tmp_list) { 1694 err = team_nl_fill_one_option_get(skb, team, opt_inst); 1695 if (err) { 1696 if (err == -EMSGSIZE) { 1697 if (!i) 1698 goto errout; 1699 incomplete = true; 1700 break; 1701 } 1702 goto errout; 1703 } 1704 i++; 1705 } 1706 1707 nla_nest_end(skb, option_list); 1708 genlmsg_end(skb, hdr); 1709 if (incomplete) 1710 goto start_again; 1711 1712 send_done: 1713 nlh = nlmsg_put(skb, pid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI); 1714 if (!nlh) { 1715 err = __send_and_alloc_skb(&skb, team, pid, send_func); 1716 if (err) 1717 goto errout; 1718 goto send_done; 1719 } 1720 1721 return send_func(skb, team, pid); 1722 1723 nla_put_failure: 1724 err = -EMSGSIZE; 1725 errout: 1726 genlmsg_cancel(skb, hdr); 1727 nlmsg_free(skb); 1728 return err; 1729 } 1730 1731 static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info) 1732 { 1733 struct team *team; 1734 struct team_option_inst *opt_inst; 1735 int err; 1736 LIST_HEAD(sel_opt_inst_list); 1737 1738 team = team_nl_team_get(info); 1739 if (!team) 1740 return -EINVAL; 1741 1742 list_for_each_entry(opt_inst, &team->option_inst_list, list) 1743 list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list); 1744 err = team_nl_send_options_get(team, info->snd_pid, info->snd_seq, 1745 NLM_F_ACK, team_nl_send_unicast, 1746 &sel_opt_inst_list); 1747 1748 team_nl_team_put(team); 1749 1750 return err; 1751 } 1752 1753 static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info) 1754 { 1755 struct team *team; 1756 int err = 0; 1757 int i; 1758 struct nlattr *nl_option; 1759 1760 team = team_nl_team_get(info); 1761 if (!team) 1762 return -EINVAL; 1763 1764 err = -EINVAL; 1765 if (!info->attrs[TEAM_ATTR_LIST_OPTION]) { 1766 err = -EINVAL; 1767 goto team_put; 1768 } 1769 1770 nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) { 1771 struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1]; 1772 struct nlattr *attr; 1773 struct nlattr *attr_data; 1774 enum team_option_type opt_type; 1775 int opt_port_ifindex = 0; /* != 0 for per-port options */ 1776 u32 opt_array_index = 0; 1777 bool opt_is_array = false; 1778 struct team_option_inst *opt_inst; 1779 char *opt_name; 1780 bool opt_found = false; 1781 1782 if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) { 1783 err = -EINVAL; 1784 goto team_put; 1785 } 1786 err = nla_parse_nested(opt_attrs, TEAM_ATTR_OPTION_MAX, 1787 nl_option, team_nl_option_policy); 1788 if (err) 1789 goto team_put; 1790 if (!opt_attrs[TEAM_ATTR_OPTION_NAME] || 1791 !opt_attrs[TEAM_ATTR_OPTION_TYPE]) { 1792 err = -EINVAL; 1793 goto team_put; 1794 } 1795 switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) { 1796 case NLA_U32: 1797 opt_type = TEAM_OPTION_TYPE_U32; 1798 break; 1799 case NLA_STRING: 1800 opt_type = TEAM_OPTION_TYPE_STRING; 1801 break; 1802 case NLA_BINARY: 1803 opt_type = TEAM_OPTION_TYPE_BINARY; 1804 break; 1805 case NLA_FLAG: 1806 opt_type = TEAM_OPTION_TYPE_BOOL; 1807 break; 1808 default: 1809 goto team_put; 1810 } 1811 1812 attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA]; 1813 if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) { 1814 err = -EINVAL; 1815 goto team_put; 1816 } 1817 1818 opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]); 1819 attr = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX]; 1820 if (attr) 1821 opt_port_ifindex = nla_get_u32(attr); 1822 1823 attr = opt_attrs[TEAM_ATTR_OPTION_ARRAY_INDEX]; 1824 if (attr) { 1825 opt_is_array = true; 1826 opt_array_index = nla_get_u32(attr); 1827 } 1828 1829 list_for_each_entry(opt_inst, &team->option_inst_list, list) { 1830 struct team_option *option = opt_inst->option; 1831 struct team_gsetter_ctx ctx; 1832 struct team_option_inst_info *opt_inst_info; 1833 int tmp_ifindex; 1834 1835 opt_inst_info = &opt_inst->info; 1836 tmp_ifindex = opt_inst_info->port ? 1837 opt_inst_info->port->dev->ifindex : 0; 1838 if (option->type != opt_type || 1839 strcmp(option->name, opt_name) || 1840 tmp_ifindex != opt_port_ifindex || 1841 (option->array_size && !opt_is_array) || 1842 opt_inst_info->array_index != opt_array_index) 1843 continue; 1844 opt_found = true; 1845 ctx.info = opt_inst_info; 1846 switch (opt_type) { 1847 case TEAM_OPTION_TYPE_U32: 1848 ctx.data.u32_val = nla_get_u32(attr_data); 1849 break; 1850 case TEAM_OPTION_TYPE_STRING: 1851 if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) { 1852 err = -EINVAL; 1853 goto team_put; 1854 } 1855 ctx.data.str_val = nla_data(attr_data); 1856 break; 1857 case TEAM_OPTION_TYPE_BINARY: 1858 ctx.data.bin_val.len = nla_len(attr_data); 1859 ctx.data.bin_val.ptr = nla_data(attr_data); 1860 break; 1861 case TEAM_OPTION_TYPE_BOOL: 1862 ctx.data.bool_val = attr_data ? true : false; 1863 break; 1864 default: 1865 BUG(); 1866 } 1867 err = team_option_set(team, opt_inst, &ctx); 1868 if (err) 1869 goto team_put; 1870 } 1871 if (!opt_found) { 1872 err = -ENOENT; 1873 goto team_put; 1874 } 1875 } 1876 1877 team_put: 1878 team_nl_team_put(team); 1879 1880 return err; 1881 } 1882 1883 static int team_nl_fill_port_list_get(struct sk_buff *skb, 1884 u32 pid, u32 seq, int flags, 1885 struct team *team, 1886 bool fillall) 1887 { 1888 struct nlattr *port_list; 1889 void *hdr; 1890 struct team_port *port; 1891 1892 hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags, 1893 TEAM_CMD_PORT_LIST_GET); 1894 if (IS_ERR(hdr)) 1895 return PTR_ERR(hdr); 1896 1897 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex)) 1898 goto nla_put_failure; 1899 port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT); 1900 if (!port_list) 1901 goto nla_put_failure; 1902 1903 list_for_each_entry(port, &team->port_list, list) { 1904 struct nlattr *port_item; 1905 1906 /* Include only changed ports if fill all mode is not on */ 1907 if (!fillall && !port->changed) 1908 continue; 1909 port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT); 1910 if (!port_item) 1911 goto nla_put_failure; 1912 if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex)) 1913 goto nla_put_failure; 1914 if (port->changed) { 1915 if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED)) 1916 goto nla_put_failure; 1917 port->changed = false; 1918 } 1919 if ((port->removed && 1920 nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) || 1921 (port->state.linkup && 1922 nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) || 1923 nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) || 1924 nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex)) 1925 goto nla_put_failure; 1926 nla_nest_end(skb, port_item); 1927 } 1928 1929 nla_nest_end(skb, port_list); 1930 return genlmsg_end(skb, hdr); 1931 1932 nla_put_failure: 1933 genlmsg_cancel(skb, hdr); 1934 return -EMSGSIZE; 1935 } 1936 1937 static int team_nl_fill_port_list_get_all(struct sk_buff *skb, 1938 struct genl_info *info, int flags, 1939 struct team *team) 1940 { 1941 return team_nl_fill_port_list_get(skb, info->snd_pid, 1942 info->snd_seq, NLM_F_ACK, 1943 team, true); 1944 } 1945 1946 static int team_nl_cmd_port_list_get(struct sk_buff *skb, 1947 struct genl_info *info) 1948 { 1949 struct team *team; 1950 int err; 1951 1952 team = team_nl_team_get(info); 1953 if (!team) 1954 return -EINVAL; 1955 1956 err = team_nl_send_generic(info, team, team_nl_fill_port_list_get_all); 1957 1958 team_nl_team_put(team); 1959 1960 return err; 1961 } 1962 1963 static struct genl_ops team_nl_ops[] = { 1964 { 1965 .cmd = TEAM_CMD_NOOP, 1966 .doit = team_nl_cmd_noop, 1967 .policy = team_nl_policy, 1968 }, 1969 { 1970 .cmd = TEAM_CMD_OPTIONS_SET, 1971 .doit = team_nl_cmd_options_set, 1972 .policy = team_nl_policy, 1973 .flags = GENL_ADMIN_PERM, 1974 }, 1975 { 1976 .cmd = TEAM_CMD_OPTIONS_GET, 1977 .doit = team_nl_cmd_options_get, 1978 .policy = team_nl_policy, 1979 .flags = GENL_ADMIN_PERM, 1980 }, 1981 { 1982 .cmd = TEAM_CMD_PORT_LIST_GET, 1983 .doit = team_nl_cmd_port_list_get, 1984 .policy = team_nl_policy, 1985 .flags = GENL_ADMIN_PERM, 1986 }, 1987 }; 1988 1989 static struct genl_multicast_group team_change_event_mcgrp = { 1990 .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME, 1991 }; 1992 1993 static int team_nl_send_multicast(struct sk_buff *skb, 1994 struct team *team, u32 pid) 1995 { 1996 return genlmsg_multicast_netns(dev_net(team->dev), skb, 0, 1997 team_change_event_mcgrp.id, GFP_KERNEL); 1998 } 1999 2000 static int team_nl_send_event_options_get(struct team *team, 2001 struct list_head *sel_opt_inst_list) 2002 { 2003 return team_nl_send_options_get(team, 0, 0, 0, team_nl_send_multicast, 2004 sel_opt_inst_list); 2005 } 2006 2007 static int team_nl_send_event_port_list_get(struct team *team) 2008 { 2009 struct sk_buff *skb; 2010 int err; 2011 struct net *net = dev_net(team->dev); 2012 2013 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); 2014 if (!skb) 2015 return -ENOMEM; 2016 2017 err = team_nl_fill_port_list_get(skb, 0, 0, 0, team, false); 2018 if (err < 0) 2019 goto err_fill; 2020 2021 err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id, 2022 GFP_KERNEL); 2023 return err; 2024 2025 err_fill: 2026 nlmsg_free(skb); 2027 return err; 2028 } 2029 2030 static int team_nl_init(void) 2031 { 2032 int err; 2033 2034 err = genl_register_family_with_ops(&team_nl_family, team_nl_ops, 2035 ARRAY_SIZE(team_nl_ops)); 2036 if (err) 2037 return err; 2038 2039 err = genl_register_mc_group(&team_nl_family, &team_change_event_mcgrp); 2040 if (err) 2041 goto err_change_event_grp_reg; 2042 2043 return 0; 2044 2045 err_change_event_grp_reg: 2046 genl_unregister_family(&team_nl_family); 2047 2048 return err; 2049 } 2050 2051 static void team_nl_fini(void) 2052 { 2053 genl_unregister_family(&team_nl_family); 2054 } 2055 2056 2057 /****************** 2058 * Change checkers 2059 ******************/ 2060 2061 static void __team_options_change_check(struct team *team) 2062 { 2063 int err; 2064 struct team_option_inst *opt_inst; 2065 LIST_HEAD(sel_opt_inst_list); 2066 2067 list_for_each_entry(opt_inst, &team->option_inst_list, list) { 2068 if (opt_inst->changed) 2069 list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list); 2070 } 2071 err = team_nl_send_event_options_get(team, &sel_opt_inst_list); 2072 if (err) 2073 netdev_warn(team->dev, "Failed to send options change via netlink (err %d)\n", 2074 err); 2075 } 2076 2077 static void __team_option_inst_change(struct team *team, 2078 struct team_option_inst *sel_opt_inst) 2079 { 2080 int err; 2081 LIST_HEAD(sel_opt_inst_list); 2082 2083 sel_opt_inst->changed = true; 2084 list_add(&sel_opt_inst->tmp_list, &sel_opt_inst_list); 2085 err = team_nl_send_event_options_get(team, &sel_opt_inst_list); 2086 if (err) 2087 netdev_warn(team->dev, "Failed to send option change via netlink (err %d)\n", 2088 err); 2089 } 2090 2091 /* rtnl lock is held */ 2092 static void __team_port_change_check(struct team_port *port, bool linkup) 2093 { 2094 int err; 2095 2096 if (!port->removed && port->state.linkup == linkup) 2097 return; 2098 2099 port->changed = true; 2100 port->state.linkup = linkup; 2101 team_refresh_port_linkup(port); 2102 if (linkup) { 2103 struct ethtool_cmd ecmd; 2104 2105 err = __ethtool_get_settings(port->dev, &ecmd); 2106 if (!err) { 2107 port->state.speed = ethtool_cmd_speed(&ecmd); 2108 port->state.duplex = ecmd.duplex; 2109 goto send_event; 2110 } 2111 } 2112 port->state.speed = 0; 2113 port->state.duplex = 0; 2114 2115 send_event: 2116 err = team_nl_send_event_port_list_get(port->team); 2117 if (err) 2118 netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink\n", 2119 port->dev->name); 2120 2121 } 2122 2123 static void team_port_change_check(struct team_port *port, bool linkup) 2124 { 2125 struct team *team = port->team; 2126 2127 mutex_lock(&team->lock); 2128 __team_port_change_check(port, linkup); 2129 mutex_unlock(&team->lock); 2130 } 2131 2132 2133 /************************************ 2134 * Net device notifier event handler 2135 ************************************/ 2136 2137 static int team_device_event(struct notifier_block *unused, 2138 unsigned long event, void *ptr) 2139 { 2140 struct net_device *dev = (struct net_device *) ptr; 2141 struct team_port *port; 2142 2143 port = team_port_get_rtnl(dev); 2144 if (!port) 2145 return NOTIFY_DONE; 2146 2147 switch (event) { 2148 case NETDEV_UP: 2149 if (netif_carrier_ok(dev)) 2150 team_port_change_check(port, true); 2151 case NETDEV_DOWN: 2152 team_port_change_check(port, false); 2153 case NETDEV_CHANGE: 2154 if (netif_running(port->dev)) 2155 team_port_change_check(port, 2156 !!netif_carrier_ok(port->dev)); 2157 break; 2158 case NETDEV_UNREGISTER: 2159 team_del_slave(port->team->dev, dev); 2160 break; 2161 case NETDEV_FEAT_CHANGE: 2162 team_compute_features(port->team); 2163 break; 2164 case NETDEV_CHANGEMTU: 2165 /* Forbid to change mtu of underlaying device */ 2166 return NOTIFY_BAD; 2167 case NETDEV_PRE_TYPE_CHANGE: 2168 /* Forbid to change type of underlaying device */ 2169 return NOTIFY_BAD; 2170 } 2171 return NOTIFY_DONE; 2172 } 2173 2174 static struct notifier_block team_notifier_block __read_mostly = { 2175 .notifier_call = team_device_event, 2176 }; 2177 2178 2179 /*********************** 2180 * Module init and exit 2181 ***********************/ 2182 2183 static int __init team_module_init(void) 2184 { 2185 int err; 2186 2187 register_netdevice_notifier(&team_notifier_block); 2188 2189 err = rtnl_link_register(&team_link_ops); 2190 if (err) 2191 goto err_rtnl_reg; 2192 2193 err = team_nl_init(); 2194 if (err) 2195 goto err_nl_init; 2196 2197 return 0; 2198 2199 err_nl_init: 2200 rtnl_link_unregister(&team_link_ops); 2201 2202 err_rtnl_reg: 2203 unregister_netdevice_notifier(&team_notifier_block); 2204 2205 return err; 2206 } 2207 2208 static void __exit team_module_exit(void) 2209 { 2210 team_nl_fini(); 2211 rtnl_link_unregister(&team_link_ops); 2212 unregister_netdevice_notifier(&team_notifier_block); 2213 } 2214 2215 module_init(team_module_init); 2216 module_exit(team_module_exit); 2217 2218 MODULE_LICENSE("GPL v2"); 2219 MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>"); 2220 MODULE_DESCRIPTION("Ethernet team device driver"); 2221 MODULE_ALIAS_RTNL_LINK(DRV_NAME); 2222