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