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