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