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