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