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