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