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