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