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