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