blob: 0e62274e884a89de170d668795b40c3620d41046 [file] [log] [blame]
Jiri Pirko3d249d42011-11-11 22:16:48 +00001/*
Jiri Pirko0d572e42012-06-19 05:54:09 +00002 * drivers/net/team/team.c - Network team device driver
Jiri Pirko3d249d42011-11-11 22:16:48 +00003 * 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 Pirkobd2d0832012-07-17 05:22:36 +000021#include <linux/netpoll.h>
Jiri Pirko87002b02011-12-08 04:11:17 +000022#include <linux/if_vlan.h>
Jiri Pirko3d249d42011-11-11 22:16:48 +000023#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 Pirko6c85f2b2012-07-20 02:28:51 +000030#include <net/sch_generic.h>
Roopa Prabhua16a8ee72015-01-29 22:40:17 -080031#include <net/switchdev.h>
Flavio Leitner7f51c582012-12-29 16:37:33 +000032#include <generated/utsrelease.h>
Jiri Pirko3d249d42011-11-11 22:16:48 +000033#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
44static struct team_port *team_port_get_rcu(const struct net_device *dev)
45{
46 struct team_port *port = rcu_dereference(dev->rx_handler_data);
47
48 return team_port_exists(dev) ? port : NULL;
49}
50
51static struct team_port *team_port_get_rtnl(const struct net_device *dev)
52{
53 struct team_port *port = rtnl_dereference(dev->rx_handler_data);
54
55 return team_port_exists(dev) ? port : NULL;
56}
57
58/*
Jiri Pirko1d76efe2012-08-17 04:00:48 +000059 * Since the ability to change device address for open port device is tested in
Jiri Pirko3d249d42011-11-11 22:16:48 +000060 * team_port_add, this function can be called without control of return value
61 */
Jiri Pirko1d76efe2012-08-17 04:00:48 +000062static int __set_port_dev_addr(struct net_device *port_dev,
63 const unsigned char *dev_addr)
Jiri Pirko3d249d42011-11-11 22:16:48 +000064{
65 struct sockaddr addr;
66
Jiri Pirko1d76efe2012-08-17 04:00:48 +000067 memcpy(addr.sa_data, dev_addr, port_dev->addr_len);
68 addr.sa_family = port_dev->type;
Jiri Pirko3d249d42011-11-11 22:16:48 +000069 return dev_set_mac_address(port_dev, &addr);
70}
71
Jiri Pirko1d76efe2012-08-17 04:00:48 +000072static int team_port_set_orig_dev_addr(struct team_port *port)
Jiri Pirko3d249d42011-11-11 22:16:48 +000073{
Jiri Pirko1d76efe2012-08-17 04:00:48 +000074 return __set_port_dev_addr(port->dev, port->orig.dev_addr);
Jiri Pirko3d249d42011-11-11 22:16:48 +000075}
76
Jiri Pirkoacbba0d2013-03-06 01:31:12 +000077static int team_port_set_team_dev_addr(struct team *team,
78 struct team_port *port)
Jiri Pirko3d249d42011-11-11 22:16:48 +000079{
Jiri Pirkoacbba0d2013-03-06 01:31:12 +000080 return __set_port_dev_addr(port->dev, team->dev->dev_addr);
Jiri Pirko3d249d42011-11-11 22:16:48 +000081}
Jiri Pirkoacbba0d2013-03-06 01:31:12 +000082
83int team_modeop_port_enter(struct team *team, struct team_port *port)
84{
85 return team_port_set_team_dev_addr(team, port);
86}
87EXPORT_SYMBOL(team_modeop_port_enter);
88
89void team_modeop_port_change_dev_addr(struct team *team,
90 struct team_port *port)
91{
92 team_port_set_team_dev_addr(team, port);
93}
94EXPORT_SYMBOL(team_modeop_port_change_dev_addr);
Jiri Pirko3d249d42011-11-11 22:16:48 +000095
Jiri Pirko71472ec2012-04-10 05:15:44 +000096static void team_refresh_port_linkup(struct team_port *port)
97{
98 port->linkup = port->user.linkup_enabled ? port->user.linkup :
99 port->state.linkup;
100}
Jiri Pirko3d249d42011-11-11 22:16:48 +0000101
Jiri Pirko0f1aad22012-06-19 05:54:11 +0000102
Jiri Pirko3d249d42011-11-11 22:16:48 +0000103/*******************
104 * Options handling
105 *******************/
106
Jiri Pirko80f7c662012-04-10 05:15:42 +0000107struct team_option_inst { /* One for each option instance */
108 struct list_head list;
Jiri Pirko01048d92012-06-19 05:54:14 +0000109 struct list_head tmp_list;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000110 struct team_option *option;
Jiri Pirko85d59a82012-06-19 05:54:10 +0000111 struct team_option_inst_info info;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000112 bool changed;
113 bool removed;
114};
115
116static struct team_option *__team_find_option(struct team *team,
117 const char *opt_name)
Jiri Pirko358b8382011-11-16 11:09:09 +0000118{
119 struct team_option *option;
120
121 list_for_each_entry(option, &team->option_list, list) {
122 if (strcmp(option->name, opt_name) == 0)
123 return option;
124 }
125 return NULL;
126}
127
Jiri Pirko80f7c662012-04-10 05:15:42 +0000128static void __team_option_inst_del(struct team_option_inst *opt_inst)
129{
130 list_del(&opt_inst->list);
131 kfree(opt_inst);
132}
133
134static void __team_option_inst_del_option(struct team *team,
135 struct team_option *option)
136{
137 struct team_option_inst *opt_inst, *tmp;
138
139 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
140 if (opt_inst->option == option)
141 __team_option_inst_del(opt_inst);
142 }
143}
144
Jiri Pirkob1303322012-06-19 05:54:08 +0000145static int __team_option_inst_add(struct team *team, struct team_option *option,
146 struct team_port *port)
147{
148 struct team_option_inst *opt_inst;
149 unsigned int array_size;
150 unsigned int i;
Jiri Pirko85d59a82012-06-19 05:54:10 +0000151 int err;
Jiri Pirkob1303322012-06-19 05:54:08 +0000152
153 array_size = option->array_size;
154 if (!array_size)
155 array_size = 1; /* No array but still need one instance */
156
157 for (i = 0; i < array_size; i++) {
158 opt_inst = kmalloc(sizeof(*opt_inst), GFP_KERNEL);
159 if (!opt_inst)
160 return -ENOMEM;
161 opt_inst->option = option;
Jiri Pirko85d59a82012-06-19 05:54:10 +0000162 opt_inst->info.port = port;
163 opt_inst->info.array_index = i;
Jiri Pirkob1303322012-06-19 05:54:08 +0000164 opt_inst->changed = true;
165 opt_inst->removed = false;
166 list_add_tail(&opt_inst->list, &team->option_inst_list);
Jiri Pirko85d59a82012-06-19 05:54:10 +0000167 if (option->init) {
168 err = option->init(team, &opt_inst->info);
169 if (err)
170 return err;
171 }
172
Jiri Pirkob1303322012-06-19 05:54:08 +0000173 }
174 return 0;
175}
176
Jiri Pirko80f7c662012-04-10 05:15:42 +0000177static int __team_option_inst_add_option(struct team *team,
178 struct team_option *option)
179{
Jiri Pirko80f7c662012-04-10 05:15:42 +0000180 int err;
181
Jiri Pirkob1303322012-06-19 05:54:08 +0000182 if (!option->per_port) {
Jiri Pirkof88725f2012-06-19 05:54:15 +0000183 err = __team_option_inst_add(team, option, NULL);
Jiri Pirkob1303322012-06-19 05:54:08 +0000184 if (err)
185 goto inst_del_option;
186 }
Jiri Pirko80f7c662012-04-10 05:15:42 +0000187 return 0;
188
189inst_del_option:
190 __team_option_inst_del_option(team, option);
191 return err;
192}
193
194static void __team_option_inst_mark_removed_option(struct team *team,
195 struct team_option *option)
196{
197 struct team_option_inst *opt_inst;
198
199 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
200 if (opt_inst->option == option) {
201 opt_inst->changed = true;
202 opt_inst->removed = true;
203 }
204 }
205}
206
207static void __team_option_inst_del_port(struct team *team,
208 struct team_port *port)
209{
210 struct team_option_inst *opt_inst, *tmp;
211
212 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
213 if (opt_inst->option->per_port &&
Jiri Pirko85d59a82012-06-19 05:54:10 +0000214 opt_inst->info.port == port)
Jiri Pirko80f7c662012-04-10 05:15:42 +0000215 __team_option_inst_del(opt_inst);
216 }
217}
218
219static int __team_option_inst_add_port(struct team *team,
220 struct team_port *port)
221{
222 struct team_option *option;
223 int err;
224
225 list_for_each_entry(option, &team->option_list, list) {
226 if (!option->per_port)
227 continue;
228 err = __team_option_inst_add(team, option, port);
229 if (err)
230 goto inst_del_port;
231 }
232 return 0;
233
234inst_del_port:
235 __team_option_inst_del_port(team, port);
236 return err;
237}
238
239static void __team_option_inst_mark_removed_port(struct team *team,
240 struct team_port *port)
241{
242 struct team_option_inst *opt_inst;
243
244 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
Jiri Pirko85d59a82012-06-19 05:54:10 +0000245 if (opt_inst->info.port == port) {
Jiri Pirko80f7c662012-04-10 05:15:42 +0000246 opt_inst->changed = true;
247 opt_inst->removed = true;
248 }
249 }
250}
251
252static int __team_options_register(struct team *team,
253 const struct team_option *option,
254 size_t option_count)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000255{
256 int i;
Jiri Pirko2bba19f2011-11-17 04:16:05 +0000257 struct team_option **dst_opts;
Jiri Pirko358b8382011-11-16 11:09:09 +0000258 int err;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000259
Jiri Pirko2bba19f2011-11-17 04:16:05 +0000260 dst_opts = kzalloc(sizeof(struct team_option *) * option_count,
261 GFP_KERNEL);
262 if (!dst_opts)
263 return -ENOMEM;
Jiri Pirko358b8382011-11-16 11:09:09 +0000264 for (i = 0; i < option_count; i++, option++) {
Jiri Pirko358b8382011-11-16 11:09:09 +0000265 if (__team_find_option(team, option->name)) {
266 err = -EEXIST;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000267 goto alloc_rollback;
Jiri Pirko358b8382011-11-16 11:09:09 +0000268 }
Jiri Pirkof8a15af2011-11-17 06:32:37 +0000269 dst_opts[i] = kmemdup(option, sizeof(*option), GFP_KERNEL);
270 if (!dst_opts[i]) {
Jiri Pirko358b8382011-11-16 11:09:09 +0000271 err = -ENOMEM;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000272 goto alloc_rollback;
Jiri Pirko358b8382011-11-16 11:09:09 +0000273 }
Jiri Pirko358b8382011-11-16 11:09:09 +0000274 }
275
Jiri Pirkob82b9182012-01-24 05:16:00 +0000276 for (i = 0; i < option_count; i++) {
Jiri Pirko80f7c662012-04-10 05:15:42 +0000277 err = __team_option_inst_add_option(team, dst_opts[i]);
278 if (err)
279 goto inst_rollback;
Jiri Pirko358b8382011-11-16 11:09:09 +0000280 list_add_tail(&dst_opts[i]->list, &team->option_list);
Jiri Pirkob82b9182012-01-24 05:16:00 +0000281 }
Jiri Pirko358b8382011-11-16 11:09:09 +0000282
Jiri Pirko2bba19f2011-11-17 04:16:05 +0000283 kfree(dst_opts);
Jiri Pirko358b8382011-11-16 11:09:09 +0000284 return 0;
285
Jiri Pirko80f7c662012-04-10 05:15:42 +0000286inst_rollback:
287 for (i--; i >= 0; i--)
288 __team_option_inst_del_option(team, dst_opts[i]);
289
290 i = option_count - 1;
291alloc_rollback:
292 for (i--; i >= 0; i--)
Jiri Pirko358b8382011-11-16 11:09:09 +0000293 kfree(dst_opts[i]);
294
Jiri Pirko2bba19f2011-11-17 04:16:05 +0000295 kfree(dst_opts);
Jiri Pirko358b8382011-11-16 11:09:09 +0000296 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000297}
Jiri Pirko358b8382011-11-16 11:09:09 +0000298
Jiri Pirkob82b9182012-01-24 05:16:00 +0000299static void __team_options_mark_removed(struct team *team,
300 const struct team_option *option,
301 size_t option_count)
302{
303 int i;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000304
Jiri Pirkob82b9182012-01-24 05:16:00 +0000305 for (i = 0; i < option_count; i++, option++) {
306 struct team_option *del_opt;
307
308 del_opt = __team_find_option(team, option->name);
Jiri Pirko80f7c662012-04-10 05:15:42 +0000309 if (del_opt)
310 __team_option_inst_mark_removed_option(team, del_opt);
Jiri Pirkob82b9182012-01-24 05:16:00 +0000311 }
312}
Jiri Pirko3d249d42011-11-11 22:16:48 +0000313
314static void __team_options_unregister(struct team *team,
Jiri Pirko358b8382011-11-16 11:09:09 +0000315 const struct team_option *option,
Jiri Pirko3d249d42011-11-11 22:16:48 +0000316 size_t option_count)
317{
318 int i;
319
Jiri Pirko358b8382011-11-16 11:09:09 +0000320 for (i = 0; i < option_count; i++, option++) {
321 struct team_option *del_opt;
322
323 del_opt = __team_find_option(team, option->name);
324 if (del_opt) {
Jiri Pirko80f7c662012-04-10 05:15:42 +0000325 __team_option_inst_del_option(team, del_opt);
Jiri Pirko358b8382011-11-16 11:09:09 +0000326 list_del(&del_opt->list);
327 kfree(del_opt);
328 }
329 }
Jiri Pirko3d249d42011-11-11 22:16:48 +0000330}
331
Jiri Pirkob82b9182012-01-24 05:16:00 +0000332static void __team_options_change_check(struct team *team);
333
334int team_options_register(struct team *team,
335 const struct team_option *option,
336 size_t option_count)
337{
338 int err;
339
340 err = __team_options_register(team, option, option_count);
341 if (err)
342 return err;
343 __team_options_change_check(team);
344 return 0;
345}
346EXPORT_SYMBOL(team_options_register);
347
Jiri Pirko358b8382011-11-16 11:09:09 +0000348void team_options_unregister(struct team *team,
349 const struct team_option *option,
Jiri Pirko3d249d42011-11-11 22:16:48 +0000350 size_t option_count)
351{
Jiri Pirkob82b9182012-01-24 05:16:00 +0000352 __team_options_mark_removed(team, option, option_count);
353 __team_options_change_check(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000354 __team_options_unregister(team, option, option_count);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000355}
356EXPORT_SYMBOL(team_options_unregister);
357
Jiri Pirko80f7c662012-04-10 05:15:42 +0000358static int team_option_get(struct team *team,
359 struct team_option_inst *opt_inst,
360 struct team_gsetter_ctx *ctx)
361{
Jiri Pirkof82b9592012-06-19 05:54:07 +0000362 if (!opt_inst->option->getter)
363 return -EOPNOTSUPP;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000364 return opt_inst->option->getter(team, ctx);
365}
366
367static int team_option_set(struct team *team,
368 struct team_option_inst *opt_inst,
369 struct team_gsetter_ctx *ctx)
370{
Jiri Pirkof82b9592012-06-19 05:54:07 +0000371 if (!opt_inst->option->setter)
372 return -EOPNOTSUPP;
Jiri Pirko2fcdb2c2012-06-19 05:54:20 +0000373 return opt_inst->option->setter(team, ctx);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000374}
375
Jiri Pirko0f1aad22012-06-19 05:54:11 +0000376void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info)
377{
378 struct team_option_inst *opt_inst;
379
380 opt_inst = container_of(opt_inst_info, struct team_option_inst, info);
381 opt_inst->changed = true;
382}
383EXPORT_SYMBOL(team_option_inst_set_change);
384
385void team_options_change_check(struct team *team)
386{
387 __team_options_change_check(team);
388}
389EXPORT_SYMBOL(team_options_change_check);
390
391
Jiri Pirko3d249d42011-11-11 22:16:48 +0000392/****************
393 * Mode handling
394 ****************/
395
396static LIST_HEAD(mode_list);
397static DEFINE_SPINLOCK(mode_list_lock);
398
Jiri Pirko04027882012-06-19 05:54:03 +0000399struct team_mode_item {
400 struct list_head list;
401 const struct team_mode *mode;
402};
Jiri Pirko3d249d42011-11-11 22:16:48 +0000403
Jiri Pirko04027882012-06-19 05:54:03 +0000404static struct team_mode_item *__find_mode(const char *kind)
405{
406 struct team_mode_item *mitem;
407
408 list_for_each_entry(mitem, &mode_list, list) {
409 if (strcmp(mitem->mode->kind, kind) == 0)
410 return mitem;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000411 }
412 return NULL;
413}
414
415static bool is_good_mode_name(const char *name)
416{
417 while (*name != '\0') {
418 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
419 return false;
420 name++;
421 }
422 return true;
423}
424
Jiri Pirko04027882012-06-19 05:54:03 +0000425int team_mode_register(const struct team_mode *mode)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000426{
427 int err = 0;
Jiri Pirko04027882012-06-19 05:54:03 +0000428 struct team_mode_item *mitem;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000429
430 if (!is_good_mode_name(mode->kind) ||
431 mode->priv_size > TEAM_MODE_PRIV_SIZE)
432 return -EINVAL;
Jiri Pirko04027882012-06-19 05:54:03 +0000433
434 mitem = kmalloc(sizeof(*mitem), GFP_KERNEL);
435 if (!mitem)
436 return -ENOMEM;
437
Jiri Pirko3d249d42011-11-11 22:16:48 +0000438 spin_lock(&mode_list_lock);
439 if (__find_mode(mode->kind)) {
440 err = -EEXIST;
Jiri Pirko04027882012-06-19 05:54:03 +0000441 kfree(mitem);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000442 goto unlock;
443 }
Jiri Pirko04027882012-06-19 05:54:03 +0000444 mitem->mode = mode;
445 list_add_tail(&mitem->list, &mode_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000446unlock:
447 spin_unlock(&mode_list_lock);
448 return err;
449}
450EXPORT_SYMBOL(team_mode_register);
451
Jiri Pirko04027882012-06-19 05:54:03 +0000452void team_mode_unregister(const struct team_mode *mode)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000453{
Jiri Pirko04027882012-06-19 05:54:03 +0000454 struct team_mode_item *mitem;
455
Jiri Pirko3d249d42011-11-11 22:16:48 +0000456 spin_lock(&mode_list_lock);
Jiri Pirko04027882012-06-19 05:54:03 +0000457 mitem = __find_mode(mode->kind);
458 if (mitem) {
459 list_del_init(&mitem->list);
460 kfree(mitem);
461 }
Jiri Pirko3d249d42011-11-11 22:16:48 +0000462 spin_unlock(&mode_list_lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000463}
464EXPORT_SYMBOL(team_mode_unregister);
465
Jiri Pirko04027882012-06-19 05:54:03 +0000466static const struct team_mode *team_mode_get(const char *kind)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000467{
Jiri Pirko04027882012-06-19 05:54:03 +0000468 struct team_mode_item *mitem;
469 const struct team_mode *mode = NULL;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000470
471 spin_lock(&mode_list_lock);
Jiri Pirko04027882012-06-19 05:54:03 +0000472 mitem = __find_mode(kind);
473 if (!mitem) {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000474 spin_unlock(&mode_list_lock);
475 request_module("team-mode-%s", kind);
476 spin_lock(&mode_list_lock);
Jiri Pirko04027882012-06-19 05:54:03 +0000477 mitem = __find_mode(kind);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000478 }
Jiri Pirko04027882012-06-19 05:54:03 +0000479 if (mitem) {
480 mode = mitem->mode;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000481 if (!try_module_get(mode->owner))
482 mode = NULL;
Jiri Pirko04027882012-06-19 05:54:03 +0000483 }
Jiri Pirko3d249d42011-11-11 22:16:48 +0000484
485 spin_unlock(&mode_list_lock);
486 return mode;
487}
488
489static void team_mode_put(const struct team_mode *mode)
490{
491 module_put(mode->owner);
492}
493
494static bool team_dummy_transmit(struct team *team, struct sk_buff *skb)
495{
496 dev_kfree_skb_any(skb);
497 return false;
498}
499
stephen hemminger2ea46592013-03-08 09:07:43 +0000500static rx_handler_result_t team_dummy_receive(struct team *team,
501 struct team_port *port,
502 struct sk_buff *skb)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000503{
504 return RX_HANDLER_ANOTHER;
505}
506
Jiri Pirkod299cd52012-06-19 05:54:04 +0000507static const struct team_mode __team_no_mode = {
508 .kind = "*NOMODE*",
509};
510
511static bool team_is_mode_set(struct team *team)
512{
513 return team->mode != &__team_no_mode;
514}
515
516static void team_set_no_mode(struct team *team)
517{
Flavio Leitnere1854832013-02-05 09:30:55 +0000518 team->user_carrier_enabled = false;
Jiri Pirkod299cd52012-06-19 05:54:04 +0000519 team->mode = &__team_no_mode;
520}
521
Jiri Pirko735d3812013-06-10 17:42:25 +0200522static void team_adjust_ops(struct team *team)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000523{
524 /*
525 * To avoid checks in rx/tx skb paths, ensure here that non-null and
526 * correct ops are always set.
527 */
528
Jiri Pirko735d3812013-06-10 17:42:25 +0200529 if (!team->en_port_count || !team_is_mode_set(team) ||
Jiri Pirko122bb042012-06-26 06:52:45 +0000530 !team->mode->ops->transmit)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000531 team->ops.transmit = team_dummy_transmit;
532 else
533 team->ops.transmit = team->mode->ops->transmit;
534
Jiri Pirko735d3812013-06-10 17:42:25 +0200535 if (!team->en_port_count || !team_is_mode_set(team) ||
Jiri Pirko122bb042012-06-26 06:52:45 +0000536 !team->mode->ops->receive)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000537 team->ops.receive = team_dummy_receive;
538 else
539 team->ops.receive = team->mode->ops->receive;
540}
541
542/*
543 * We can benefit from the fact that it's ensured no port is present
544 * at the time of mode change. Therefore no packets are in fly so there's no
545 * need to set mode operations in any special way.
546 */
547static int __team_change_mode(struct team *team,
548 const struct team_mode *new_mode)
549{
550 /* Check if mode was previously set and do cleanup if so */
Jiri Pirkod299cd52012-06-19 05:54:04 +0000551 if (team_is_mode_set(team)) {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000552 void (*exit_op)(struct team *team) = team->ops.exit;
553
554 /* Clear ops area so no callback is called any longer */
555 memset(&team->ops, 0, sizeof(struct team_mode_ops));
556 team_adjust_ops(team);
557
558 if (exit_op)
559 exit_op(team);
560 team_mode_put(team->mode);
Jiri Pirkod299cd52012-06-19 05:54:04 +0000561 team_set_no_mode(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000562 /* zero private data area */
563 memset(&team->mode_priv, 0,
564 sizeof(struct team) - offsetof(struct team, mode_priv));
565 }
566
567 if (!new_mode)
568 return 0;
569
570 if (new_mode->ops->init) {
571 int err;
572
573 err = new_mode->ops->init(team);
574 if (err)
575 return err;
576 }
577
578 team->mode = new_mode;
579 memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops));
580 team_adjust_ops(team);
581
582 return 0;
583}
584
585static int team_change_mode(struct team *team, const char *kind)
586{
Jiri Pirko04027882012-06-19 05:54:03 +0000587 const struct team_mode *new_mode;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000588 struct net_device *dev = team->dev;
589 int err;
590
591 if (!list_empty(&team->port_list)) {
592 netdev_err(dev, "No ports can be present during mode change\n");
593 return -EBUSY;
594 }
595
Jiri Pirkod299cd52012-06-19 05:54:04 +0000596 if (team_is_mode_set(team) && strcmp(team->mode->kind, kind) == 0) {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000597 netdev_err(dev, "Unable to change to the same mode the team is in\n");
598 return -EINVAL;
599 }
600
601 new_mode = team_mode_get(kind);
602 if (!new_mode) {
603 netdev_err(dev, "Mode \"%s\" not found\n", kind);
604 return -EINVAL;
605 }
606
607 err = __team_change_mode(team, new_mode);
608 if (err) {
609 netdev_err(dev, "Failed to change to mode \"%s\"\n", kind);
610 team_mode_put(new_mode);
611 return err;
612 }
613
614 netdev_info(dev, "Mode changed to \"%s\"\n", kind);
615 return 0;
616}
617
618
Jiri Pirkofc423ff2013-07-20 12:13:52 +0200619/*********************
620 * Peers notification
621 *********************/
622
623static void team_notify_peers_work(struct work_struct *work)
624{
625 struct team *team;
Jiri Pirkob0d11b42015-01-14 18:15:30 +0100626 int val;
Jiri Pirkofc423ff2013-07-20 12:13:52 +0200627
628 team = container_of(work, struct team, notify_peers.dw.work);
629
630 if (!rtnl_trylock()) {
631 schedule_delayed_work(&team->notify_peers.dw, 0);
632 return;
633 }
Jiri Pirkob0d11b42015-01-14 18:15:30 +0100634 val = atomic_dec_if_positive(&team->notify_peers.count_pending);
635 if (val < 0) {
636 rtnl_unlock();
637 return;
638 }
Jiri Pirkofc423ff2013-07-20 12:13:52 +0200639 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, team->dev);
640 rtnl_unlock();
Jiri Pirkob0d11b42015-01-14 18:15:30 +0100641 if (val)
Jiri Pirkofc423ff2013-07-20 12:13:52 +0200642 schedule_delayed_work(&team->notify_peers.dw,
643 msecs_to_jiffies(team->notify_peers.interval));
644}
645
646static void team_notify_peers(struct team *team)
647{
648 if (!team->notify_peers.count || !netif_running(team->dev))
649 return;
Joe Lawrence47549652014-10-03 09:58:34 -0400650 atomic_add(team->notify_peers.count, &team->notify_peers.count_pending);
Jiri Pirkofc423ff2013-07-20 12:13:52 +0200651 schedule_delayed_work(&team->notify_peers.dw, 0);
652}
653
654static void team_notify_peers_init(struct team *team)
655{
656 INIT_DELAYED_WORK(&team->notify_peers.dw, team_notify_peers_work);
657}
658
659static void team_notify_peers_fini(struct team *team)
660{
661 cancel_delayed_work_sync(&team->notify_peers.dw);
662}
663
664
Jiri Pirko492b2002013-07-20 12:13:54 +0200665/*******************************
666 * Send multicast group rejoins
667 *******************************/
668
669static void team_mcast_rejoin_work(struct work_struct *work)
670{
671 struct team *team;
Jiri Pirkob0d11b42015-01-14 18:15:30 +0100672 int val;
Jiri Pirko492b2002013-07-20 12:13:54 +0200673
674 team = container_of(work, struct team, mcast_rejoin.dw.work);
675
676 if (!rtnl_trylock()) {
677 schedule_delayed_work(&team->mcast_rejoin.dw, 0);
678 return;
679 }
Jiri Pirkob0d11b42015-01-14 18:15:30 +0100680 val = atomic_dec_if_positive(&team->mcast_rejoin.count_pending);
681 if (val < 0) {
682 rtnl_unlock();
683 return;
684 }
Jiri Pirko492b2002013-07-20 12:13:54 +0200685 call_netdevice_notifiers(NETDEV_RESEND_IGMP, team->dev);
686 rtnl_unlock();
Jiri Pirkob0d11b42015-01-14 18:15:30 +0100687 if (val)
Jiri Pirko492b2002013-07-20 12:13:54 +0200688 schedule_delayed_work(&team->mcast_rejoin.dw,
689 msecs_to_jiffies(team->mcast_rejoin.interval));
690}
691
692static void team_mcast_rejoin(struct team *team)
693{
694 if (!team->mcast_rejoin.count || !netif_running(team->dev))
695 return;
Joe Lawrence47549652014-10-03 09:58:34 -0400696 atomic_add(team->mcast_rejoin.count, &team->mcast_rejoin.count_pending);
Jiri Pirko492b2002013-07-20 12:13:54 +0200697 schedule_delayed_work(&team->mcast_rejoin.dw, 0);
698}
699
700static void team_mcast_rejoin_init(struct team *team)
701{
702 INIT_DELAYED_WORK(&team->mcast_rejoin.dw, team_mcast_rejoin_work);
703}
704
705static void team_mcast_rejoin_fini(struct team *team)
706{
707 cancel_delayed_work_sync(&team->mcast_rejoin.dw);
708}
709
710
Jiri Pirko3d249d42011-11-11 22:16:48 +0000711/************************
712 * Rx path frame handler
713 ************************/
714
715/* note: already called with rcu_read_lock */
716static rx_handler_result_t team_handle_frame(struct sk_buff **pskb)
717{
718 struct sk_buff *skb = *pskb;
719 struct team_port *port;
720 struct team *team;
721 rx_handler_result_t res;
722
723 skb = skb_share_check(skb, GFP_ATOMIC);
724 if (!skb)
725 return RX_HANDLER_CONSUMED;
726
727 *pskb = skb;
728
729 port = team_port_get_rcu(skb->dev);
730 team = port->team;
Jiri Pirko19a0b582012-04-20 04:42:05 +0000731 if (!team_port_enabled(port)) {
732 /* allow exact match delivery for disabled ports */
733 res = RX_HANDLER_EXACT;
734 } else {
735 res = team->ops.receive(team, port, skb);
736 }
Jiri Pirko3d249d42011-11-11 22:16:48 +0000737 if (res == RX_HANDLER_ANOTHER) {
738 struct team_pcpu_stats *pcpu_stats;
739
740 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
741 u64_stats_update_begin(&pcpu_stats->syncp);
742 pcpu_stats->rx_packets++;
743 pcpu_stats->rx_bytes += skb->len;
744 if (skb->pkt_type == PACKET_MULTICAST)
745 pcpu_stats->rx_multicast++;
746 u64_stats_update_end(&pcpu_stats->syncp);
747
748 skb->dev = team->dev;
749 } else {
750 this_cpu_inc(team->pcpu_stats->rx_dropped);
751 }
752
753 return res;
754}
755
756
Jiri Pirko8ff51052012-07-27 06:28:55 +0000757/*************************************
758 * Multiqueue Tx port select override
759 *************************************/
760
761static int team_queue_override_init(struct team *team)
762{
763 struct list_head *listarr;
764 unsigned int queue_cnt = team->dev->num_tx_queues - 1;
765 unsigned int i;
766
767 if (!queue_cnt)
768 return 0;
769 listarr = kmalloc(sizeof(struct list_head) * queue_cnt, GFP_KERNEL);
770 if (!listarr)
771 return -ENOMEM;
772 team->qom_lists = listarr;
773 for (i = 0; i < queue_cnt; i++)
774 INIT_LIST_HEAD(listarr++);
775 return 0;
776}
777
778static void team_queue_override_fini(struct team *team)
779{
780 kfree(team->qom_lists);
781}
782
783static struct list_head *__team_get_qom_list(struct team *team, u16 queue_id)
784{
785 return &team->qom_lists[queue_id - 1];
786}
787
788/*
789 * note: already called with rcu_read_lock
790 */
791static bool team_queue_override_transmit(struct team *team, struct sk_buff *skb)
792{
793 struct list_head *qom_list;
794 struct team_port *port;
795
796 if (!team->queue_override_enabled || !skb->queue_mapping)
797 return false;
798 qom_list = __team_get_qom_list(team, skb->queue_mapping);
799 list_for_each_entry_rcu(port, qom_list, qom_list) {
800 if (!team_dev_queue_xmit(team, port, skb))
801 return true;
802 }
803 return false;
804}
805
806static void __team_queue_override_port_del(struct team *team,
807 struct team_port *port)
808{
Jiri Pirko6c31ff32013-06-10 17:42:23 +0200809 if (!port->queue_id)
810 return;
Jiri Pirko8ff51052012-07-27 06:28:55 +0000811 list_del_rcu(&port->qom_list);
Jiri Pirko8ff51052012-07-27 06:28:55 +0000812}
813
814static bool team_queue_override_port_has_gt_prio_than(struct team_port *port,
815 struct team_port *cur)
816{
817 if (port->priority < cur->priority)
818 return true;
819 if (port->priority > cur->priority)
820 return false;
821 if (port->index < cur->index)
822 return true;
823 return false;
824}
825
826static void __team_queue_override_port_add(struct team *team,
827 struct team_port *port)
828{
829 struct team_port *cur;
830 struct list_head *qom_list;
831 struct list_head *node;
832
Jiri Pirko6c31ff32013-06-10 17:42:23 +0200833 if (!port->queue_id)
Jiri Pirko8ff51052012-07-27 06:28:55 +0000834 return;
Jiri Pirko8ff51052012-07-27 06:28:55 +0000835 qom_list = __team_get_qom_list(team, port->queue_id);
836 node = qom_list;
837 list_for_each_entry(cur, qom_list, qom_list) {
838 if (team_queue_override_port_has_gt_prio_than(port, cur))
839 break;
840 node = &cur->qom_list;
841 }
842 list_add_tail_rcu(&port->qom_list, node);
843}
844
845static void __team_queue_override_enabled_check(struct team *team)
846{
847 struct team_port *port;
848 bool enabled = false;
849
850 list_for_each_entry(port, &team->port_list, list) {
Jiri Pirko6c31ff32013-06-10 17:42:23 +0200851 if (port->queue_id) {
Jiri Pirko8ff51052012-07-27 06:28:55 +0000852 enabled = true;
853 break;
854 }
855 }
856 if (enabled == team->queue_override_enabled)
857 return;
858 netdev_dbg(team->dev, "%s queue override\n",
859 enabled ? "Enabling" : "Disabling");
860 team->queue_override_enabled = enabled;
861}
862
Jiri Pirko6c31ff32013-06-10 17:42:23 +0200863static void team_queue_override_port_prio_changed(struct team *team,
864 struct team_port *port)
Jiri Pirko8ff51052012-07-27 06:28:55 +0000865{
Jiri Pirko6c31ff32013-06-10 17:42:23 +0200866 if (!port->queue_id || team_port_enabled(port))
867 return;
Jiri Pirko8ff51052012-07-27 06:28:55 +0000868 __team_queue_override_port_del(team, port);
869 __team_queue_override_port_add(team, port);
870 __team_queue_override_enabled_check(team);
871}
872
Jiri Pirko6c31ff32013-06-10 17:42:23 +0200873static void team_queue_override_port_change_queue_id(struct team *team,
874 struct team_port *port,
875 u16 new_queue_id)
876{
877 if (team_port_enabled(port)) {
878 __team_queue_override_port_del(team, port);
879 port->queue_id = new_queue_id;
880 __team_queue_override_port_add(team, port);
881 __team_queue_override_enabled_check(team);
882 } else {
883 port->queue_id = new_queue_id;
884 }
885}
886
887static void team_queue_override_port_add(struct team *team,
888 struct team_port *port)
889{
890 __team_queue_override_port_add(team, port);
891 __team_queue_override_enabled_check(team);
892}
893
894static void team_queue_override_port_del(struct team *team,
895 struct team_port *port)
896{
897 __team_queue_override_port_del(team, port);
898 __team_queue_override_enabled_check(team);
899}
900
Jiri Pirko8ff51052012-07-27 06:28:55 +0000901
Jiri Pirko3d249d42011-11-11 22:16:48 +0000902/****************
903 * Port handling
904 ****************/
905
906static bool team_port_find(const struct team *team,
907 const struct team_port *port)
908{
909 struct team_port *cur;
910
911 list_for_each_entry(cur, &team->port_list, list)
912 if (cur == port)
913 return true;
914 return false;
915}
916
Jiri Pirko19a0b582012-04-20 04:42:05 +0000917/*
918 * Enable/disable port by adding to enabled port hashlist and setting
919 * port->index (Might be racy so reader could see incorrect ifindex when
920 * processing a flying packet, but that is not a problem). Write guarded
921 * by team->lock.
922 */
923static void team_port_enable(struct team *team,
924 struct team_port *port)
925{
926 if (team_port_enabled(port))
927 return;
928 port->index = team->en_port_count++;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000929 hlist_add_head_rcu(&port->hlist,
930 team_port_index_hash(team, port->index));
Jiri Pirko122bb042012-06-26 06:52:45 +0000931 team_adjust_ops(team);
Jiri Pirko6c31ff32013-06-10 17:42:23 +0200932 team_queue_override_port_add(team, port);
Jiri Pirko4bccfd12012-06-19 05:54:16 +0000933 if (team->ops.port_enabled)
934 team->ops.port_enabled(team, port);
Jiri Pirkofc423ff2013-07-20 12:13:52 +0200935 team_notify_peers(team);
Jiri Pirko492b2002013-07-20 12:13:54 +0200936 team_mcast_rejoin(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000937}
938
939static void __reconstruct_port_hlist(struct team *team, int rm_index)
940{
941 int i;
942 struct team_port *port;
943
Jiri Pirko19a0b582012-04-20 04:42:05 +0000944 for (i = rm_index + 1; i < team->en_port_count; i++) {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000945 port = team_get_port_by_index(team, i);
946 hlist_del_rcu(&port->hlist);
947 port->index--;
948 hlist_add_head_rcu(&port->hlist,
949 team_port_index_hash(team, port->index));
950 }
951}
952
Jiri Pirko19a0b582012-04-20 04:42:05 +0000953static void team_port_disable(struct team *team,
954 struct team_port *port)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000955{
Jiri Pirko19a0b582012-04-20 04:42:05 +0000956 if (!team_port_enabled(port))
957 return;
Jiri Pirko4bccfd12012-06-19 05:54:16 +0000958 if (team->ops.port_disabled)
959 team->ops.port_disabled(team, port);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000960 hlist_del_rcu(&port->hlist);
Jiri Pirko122bb042012-06-26 06:52:45 +0000961 __reconstruct_port_hlist(team, port->index);
Jiri Pirko19a0b582012-04-20 04:42:05 +0000962 port->index = -1;
Jiri Pirko122bb042012-06-26 06:52:45 +0000963 team->en_port_count--;
Jiri Pirko735d3812013-06-10 17:42:25 +0200964 team_queue_override_port_del(team, port);
965 team_adjust_ops(team);
Jiri Pirkofc423ff2013-07-20 12:13:52 +0200966 team_notify_peers(team);
Jiri Pirko492b2002013-07-20 12:13:54 +0200967 team_mcast_rejoin(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000968}
969
970#define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \
971 NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
972 NETIF_F_HIGHDMA | NETIF_F_LRO)
973
974static void __team_compute_features(struct team *team)
975{
976 struct team_port *port;
Michal Kubeček36259202014-05-20 08:29:45 +0200977 u32 vlan_features = TEAM_VLAN_FEATURES & NETIF_F_ALL_FOR_ALL;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000978 unsigned short max_hard_header_len = ETH_HLEN;
Eric Dumazet02875872014-10-05 18:38:35 -0700979 unsigned int dst_release_flag = IFF_XMIT_DST_RELEASE |
980 IFF_XMIT_DST_RELEASE_PERM;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000981
982 list_for_each_entry(port, &team->port_list, list) {
983 vlan_features = netdev_increment_features(vlan_features,
984 port->dev->vlan_features,
985 TEAM_VLAN_FEATURES);
986
Jiri Pirkodc905952012-07-18 07:39:38 +0000987 dst_release_flag &= port->dev->priv_flags;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000988 if (port->dev->hard_header_len > max_hard_header_len)
989 max_hard_header_len = port->dev->hard_header_len;
990 }
991
992 team->dev->vlan_features = vlan_features;
993 team->dev->hard_header_len = max_hard_header_len;
994
Eric Dumazet02875872014-10-05 18:38:35 -0700995 team->dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
996 if (dst_release_flag == (IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM))
997 team->dev->priv_flags |= IFF_XMIT_DST_RELEASE;
Jiri Pirkodc905952012-07-18 07:39:38 +0000998
Jiri Pirko3d249d42011-11-11 22:16:48 +0000999 netdev_change_features(team->dev);
1000}
1001
1002static void team_compute_features(struct team *team)
1003{
Jiri Pirko61dc3462011-11-16 11:09:08 +00001004 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001005 __team_compute_features(team);
Jiri Pirko61dc3462011-11-16 11:09:08 +00001006 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001007}
1008
1009static int team_port_enter(struct team *team, struct team_port *port)
1010{
1011 int err = 0;
1012
1013 dev_hold(team->dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001014 if (team->ops.port_enter) {
1015 err = team->ops.port_enter(team, port);
1016 if (err) {
1017 netdev_err(team->dev, "Device %s failed to enter team mode\n",
1018 port->dev->name);
1019 goto err_port_enter;
1020 }
1021 }
1022
1023 return 0;
1024
1025err_port_enter:
Jiri Pirko3d249d42011-11-11 22:16:48 +00001026 dev_put(team->dev);
1027
1028 return err;
1029}
1030
1031static void team_port_leave(struct team *team, struct team_port *port)
1032{
1033 if (team->ops.port_leave)
1034 team->ops.port_leave(team, port);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001035 dev_put(team->dev);
1036}
1037
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001038#ifdef CONFIG_NET_POLL_CONTROLLER
Eric W. Biedermana8779ec2014-03-27 15:36:38 -07001039static int team_port_enable_netpoll(struct team *team, struct team_port *port)
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001040{
1041 struct netpoll *np;
1042 int err;
1043
stephen hemminger0fb52a22013-07-24 11:52:44 -07001044 if (!team->dev->npinfo)
1045 return 0;
1046
Eric W. Biedermana8779ec2014-03-27 15:36:38 -07001047 np = kzalloc(sizeof(*np), GFP_KERNEL);
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001048 if (!np)
1049 return -ENOMEM;
1050
Eric W. Biedermana8779ec2014-03-27 15:36:38 -07001051 err = __netpoll_setup(np, port->dev);
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001052 if (err) {
1053 kfree(np);
1054 return err;
1055 }
1056 port->np = np;
1057 return err;
1058}
1059
1060static void team_port_disable_netpoll(struct team_port *port)
1061{
1062 struct netpoll *np = port->np;
1063
1064 if (!np)
1065 return;
1066 port->np = NULL;
1067
1068 /* Wait for transmitting packets to finish before freeing. */
1069 synchronize_rcu_bh();
1070 __netpoll_cleanup(np);
1071 kfree(np);
1072}
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001073#else
Eric W. Biedermana8779ec2014-03-27 15:36:38 -07001074static int team_port_enable_netpoll(struct team *team, struct team_port *port)
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001075{
1076 return 0;
1077}
1078static void team_port_disable_netpoll(struct team_port *port)
1079{
1080}
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001081#endif
1082
Jiri Pirkod7d3c052014-08-25 21:38:27 +02001083static int team_upper_dev_link(struct net_device *dev,
1084 struct net_device *port_dev)
1085{
1086 int err;
1087
1088 err = netdev_master_upper_dev_link(port_dev, dev);
1089 if (err)
1090 return err;
1091 port_dev->priv_flags |= IFF_TEAM_PORT;
1092 return 0;
1093}
1094
1095static void team_upper_dev_unlink(struct net_device *dev,
1096 struct net_device *port_dev)
1097{
1098 netdev_upper_dev_unlink(port_dev, dev);
1099 port_dev->priv_flags &= ~IFF_TEAM_PORT;
1100}
1101
Jiri Pirko10f3f512012-09-21 01:51:59 +00001102static void __team_port_change_port_added(struct team_port *port, bool linkup);
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001103static int team_dev_type_check_change(struct net_device *dev,
1104 struct net_device *port_dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001105
1106static int team_port_add(struct team *team, struct net_device *port_dev)
1107{
1108 struct net_device *dev = team->dev;
1109 struct team_port *port;
1110 char *portname = port_dev->name;
1111 int err;
1112
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001113 if (port_dev->flags & IFF_LOOPBACK) {
1114 netdev_err(dev, "Device %s is loopback device. Loopback devices can't be added as a team port\n",
Jiri Pirko3d249d42011-11-11 22:16:48 +00001115 portname);
1116 return -EINVAL;
1117 }
1118
1119 if (team_port_exists(port_dev)) {
1120 netdev_err(dev, "Device %s is already a port "
1121 "of a team device\n", portname);
1122 return -EBUSY;
1123 }
1124
Jiri Pirko2c33bb32012-08-23 03:26:53 +00001125 if (port_dev->features & NETIF_F_VLAN_CHALLENGED &&
1126 vlan_uses_dev(dev)) {
1127 netdev_err(dev, "Device %s is VLAN challenged and team device has VLAN set up\n",
1128 portname);
1129 return -EPERM;
1130 }
1131
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001132 err = team_dev_type_check_change(dev, port_dev);
1133 if (err)
1134 return err;
1135
Jiri Pirko3d249d42011-11-11 22:16:48 +00001136 if (port_dev->flags & IFF_UP) {
1137 netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
1138 portname);
1139 return -EBUSY;
1140 }
1141
Jiri Pirko5149ee52012-06-19 05:54:05 +00001142 port = kzalloc(sizeof(struct team_port) + team->mode->port_priv_size,
1143 GFP_KERNEL);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001144 if (!port)
1145 return -ENOMEM;
1146
1147 port->dev = port_dev;
1148 port->team = team;
Jiri Pirko8ff51052012-07-27 06:28:55 +00001149 INIT_LIST_HEAD(&port->qom_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001150
1151 port->orig.mtu = port_dev->mtu;
1152 err = dev_set_mtu(port_dev, dev->mtu);
1153 if (err) {
1154 netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
1155 goto err_set_mtu;
1156 }
1157
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001158 memcpy(port->orig.dev_addr, port_dev->dev_addr, port_dev->addr_len);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001159
1160 err = team_port_enter(team, port);
1161 if (err) {
1162 netdev_err(dev, "Device %s failed to enter team mode\n",
1163 portname);
1164 goto err_port_enter;
1165 }
1166
1167 err = dev_open(port_dev);
1168 if (err) {
1169 netdev_dbg(dev, "Device %s opening failed\n",
1170 portname);
1171 goto err_dev_open;
1172 }
1173
Jiri Pirko57459182011-12-08 04:11:20 +00001174 err = vlan_vids_add_by_dev(port_dev, dev);
1175 if (err) {
1176 netdev_err(dev, "Failed to add vlan ids to device %s\n",
1177 portname);
1178 goto err_vids_add;
1179 }
1180
Eric W. Biedermana8779ec2014-03-27 15:36:38 -07001181 err = team_port_enable_netpoll(team, port);
stephen hemminger0fb52a22013-07-24 11:52:44 -07001182 if (err) {
1183 netdev_err(dev, "Failed to enable netpoll on device %s\n",
1184 portname);
1185 goto err_enable_netpoll;
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001186 }
1187
Michal Kubečekfbe168b2014-11-13 07:54:50 +01001188 if (!(dev->features & NETIF_F_LRO))
1189 dev_disable_lro(port_dev);
1190
Jiri Pirko3d249d42011-11-11 22:16:48 +00001191 err = netdev_rx_handler_register(port_dev, team_handle_frame,
1192 port);
1193 if (err) {
1194 netdev_err(dev, "Device %s failed to register rx_handler\n",
1195 portname);
1196 goto err_handler_register;
1197 }
1198
Jiri Pirkod7d3c052014-08-25 21:38:27 +02001199 err = team_upper_dev_link(dev, port_dev);
1200 if (err) {
1201 netdev_err(dev, "Device %s failed to set upper link\n",
1202 portname);
1203 goto err_set_upper_link;
1204 }
1205
Jiri Pirko35b384b2012-06-19 05:54:19 +00001206 err = __team_option_inst_add_port(team, port);
Jiri Pirko80f7c662012-04-10 05:15:42 +00001207 if (err) {
1208 netdev_err(dev, "Device %s failed to add per-port options\n",
1209 portname);
1210 goto err_option_port_add;
1211 }
1212
Jiri Pirko19a0b582012-04-20 04:42:05 +00001213 port->index = -1;
Jiri Pirko19a0b582012-04-20 04:42:05 +00001214 list_add_tail_rcu(&port->list, &team->port_list);
Jiri Pirko72df9352013-06-08 15:00:54 +02001215 team_port_enable(team, port);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001216 __team_compute_features(team);
Jiri Pirko10f3f512012-09-21 01:51:59 +00001217 __team_port_change_port_added(port, !!netif_carrier_ok(port_dev));
Jiri Pirko35b384b2012-06-19 05:54:19 +00001218 __team_options_change_check(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001219
1220 netdev_info(dev, "Port device %s added\n", portname);
1221
1222 return 0;
1223
Jiri Pirko80f7c662012-04-10 05:15:42 +00001224err_option_port_add:
Jiri Pirkod7d3c052014-08-25 21:38:27 +02001225 team_upper_dev_unlink(dev, port_dev);
1226
1227err_set_upper_link:
Jiri Pirko80f7c662012-04-10 05:15:42 +00001228 netdev_rx_handler_unregister(port_dev);
1229
Jiri Pirko3d249d42011-11-11 22:16:48 +00001230err_handler_register:
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001231 team_port_disable_netpoll(port);
1232
1233err_enable_netpoll:
Jiri Pirko57459182011-12-08 04:11:20 +00001234 vlan_vids_del_by_dev(port_dev, dev);
1235
1236err_vids_add:
Jiri Pirko3d249d42011-11-11 22:16:48 +00001237 dev_close(port_dev);
1238
1239err_dev_open:
1240 team_port_leave(team, port);
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001241 team_port_set_orig_dev_addr(port);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001242
1243err_port_enter:
1244 dev_set_mtu(port_dev, port->orig.mtu);
1245
1246err_set_mtu:
1247 kfree(port);
1248
1249 return err;
1250}
1251
Jiri Pirko10f3f512012-09-21 01:51:59 +00001252static void __team_port_change_port_removed(struct team_port *port);
1253
Jiri Pirko3d249d42011-11-11 22:16:48 +00001254static int team_port_del(struct team *team, struct net_device *port_dev)
1255{
1256 struct net_device *dev = team->dev;
1257 struct team_port *port;
1258 char *portname = port_dev->name;
1259
1260 port = team_port_get_rtnl(port_dev);
1261 if (!port || !team_port_find(team, port)) {
1262 netdev_err(dev, "Device %s does not act as a port of this team\n",
1263 portname);
1264 return -ENOENT;
1265 }
1266
Jiri Pirko19a0b582012-04-20 04:42:05 +00001267 team_port_disable(team, port);
1268 list_del_rcu(&port->list);
Jiri Pirkod7d3c052014-08-25 21:38:27 +02001269 team_upper_dev_unlink(dev, port_dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001270 netdev_rx_handler_unregister(port_dev);
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001271 team_port_disable_netpoll(port);
Jiri Pirko57459182011-12-08 04:11:20 +00001272 vlan_vids_del_by_dev(port_dev, dev);
Vlad Yasevichba812762013-03-07 07:59:25 +00001273 dev_uc_unsync(port_dev, dev);
1274 dev_mc_unsync(port_dev, dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001275 dev_close(port_dev);
1276 team_port_leave(team, port);
Jiri Pirkoc3969d802013-02-01 08:17:25 +00001277
1278 __team_option_inst_mark_removed_port(team, port);
1279 __team_options_change_check(team);
1280 __team_option_inst_del_port(team, port);
1281 __team_port_change_port_removed(port);
1282
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001283 team_port_set_orig_dev_addr(port);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001284 dev_set_mtu(port_dev, port->orig.mtu);
Jiri Pirkod80b35b2013-06-10 17:42:24 +02001285 kfree_rcu(port, rcu);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001286 netdev_info(dev, "Port device %s removed\n", portname);
1287 __team_compute_features(team);
1288
1289 return 0;
1290}
1291
1292
1293/*****************
1294 * Net device ops
1295 *****************/
1296
Jiri Pirko80f7c662012-04-10 05:15:42 +00001297static int team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001298{
Jiri Pirkod299cd52012-06-19 05:54:04 +00001299 ctx->data.str_val = team->mode->kind;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001300 return 0;
1301}
1302
Jiri Pirko80f7c662012-04-10 05:15:42 +00001303static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001304{
Jiri Pirko80f7c662012-04-10 05:15:42 +00001305 return team_change_mode(team, ctx->data.str_val);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001306}
1307
Jiri Pirkofc423ff2013-07-20 12:13:52 +02001308static int team_notify_peers_count_get(struct team *team,
1309 struct team_gsetter_ctx *ctx)
1310{
1311 ctx->data.u32_val = team->notify_peers.count;
1312 return 0;
1313}
1314
1315static int team_notify_peers_count_set(struct team *team,
1316 struct team_gsetter_ctx *ctx)
1317{
1318 team->notify_peers.count = ctx->data.u32_val;
1319 return 0;
1320}
1321
1322static int team_notify_peers_interval_get(struct team *team,
1323 struct team_gsetter_ctx *ctx)
1324{
1325 ctx->data.u32_val = team->notify_peers.interval;
1326 return 0;
1327}
1328
1329static int team_notify_peers_interval_set(struct team *team,
1330 struct team_gsetter_ctx *ctx)
1331{
1332 team->notify_peers.interval = ctx->data.u32_val;
1333 return 0;
1334}
1335
Jiri Pirko492b2002013-07-20 12:13:54 +02001336static int team_mcast_rejoin_count_get(struct team *team,
1337 struct team_gsetter_ctx *ctx)
1338{
1339 ctx->data.u32_val = team->mcast_rejoin.count;
1340 return 0;
1341}
1342
1343static int team_mcast_rejoin_count_set(struct team *team,
1344 struct team_gsetter_ctx *ctx)
1345{
1346 team->mcast_rejoin.count = ctx->data.u32_val;
1347 return 0;
1348}
1349
1350static int team_mcast_rejoin_interval_get(struct team *team,
1351 struct team_gsetter_ctx *ctx)
1352{
1353 ctx->data.u32_val = team->mcast_rejoin.interval;
1354 return 0;
1355}
1356
1357static int team_mcast_rejoin_interval_set(struct team *team,
1358 struct team_gsetter_ctx *ctx)
1359{
1360 team->mcast_rejoin.interval = ctx->data.u32_val;
1361 return 0;
1362}
1363
Jiri Pirkoacd69962012-04-20 04:42:06 +00001364static int team_port_en_option_get(struct team *team,
1365 struct team_gsetter_ctx *ctx)
1366{
Jiri Pirko85d59a82012-06-19 05:54:10 +00001367 struct team_port *port = ctx->info->port;
1368
1369 ctx->data.bool_val = team_port_enabled(port);
Jiri Pirkoacd69962012-04-20 04:42:06 +00001370 return 0;
1371}
1372
1373static int team_port_en_option_set(struct team *team,
1374 struct team_gsetter_ctx *ctx)
1375{
Jiri Pirko85d59a82012-06-19 05:54:10 +00001376 struct team_port *port = ctx->info->port;
1377
Jiri Pirkoacd69962012-04-20 04:42:06 +00001378 if (ctx->data.bool_val)
Jiri Pirko85d59a82012-06-19 05:54:10 +00001379 team_port_enable(team, port);
Jiri Pirkoacd69962012-04-20 04:42:06 +00001380 else
Jiri Pirko85d59a82012-06-19 05:54:10 +00001381 team_port_disable(team, port);
Jiri Pirkoacd69962012-04-20 04:42:06 +00001382 return 0;
1383}
1384
Jiri Pirko71472ec2012-04-10 05:15:44 +00001385static int team_user_linkup_option_get(struct team *team,
1386 struct team_gsetter_ctx *ctx)
1387{
Jiri Pirko85d59a82012-06-19 05:54:10 +00001388 struct team_port *port = ctx->info->port;
1389
1390 ctx->data.bool_val = port->user.linkup;
Jiri Pirko71472ec2012-04-10 05:15:44 +00001391 return 0;
1392}
1393
Jiri Pirkof5e0d342013-11-28 18:01:38 +01001394static void __team_carrier_check(struct team *team);
1395
Jiri Pirko71472ec2012-04-10 05:15:44 +00001396static int team_user_linkup_option_set(struct team *team,
1397 struct team_gsetter_ctx *ctx)
1398{
Jiri Pirko85d59a82012-06-19 05:54:10 +00001399 struct team_port *port = ctx->info->port;
1400
1401 port->user.linkup = ctx->data.bool_val;
1402 team_refresh_port_linkup(port);
Jiri Pirkof5e0d342013-11-28 18:01:38 +01001403 __team_carrier_check(port->team);
Jiri Pirko71472ec2012-04-10 05:15:44 +00001404 return 0;
1405}
1406
1407static int team_user_linkup_en_option_get(struct team *team,
1408 struct team_gsetter_ctx *ctx)
1409{
Jiri Pirko85d59a82012-06-19 05:54:10 +00001410 struct team_port *port = ctx->info->port;
Jiri Pirko71472ec2012-04-10 05:15:44 +00001411
1412 ctx->data.bool_val = port->user.linkup_enabled;
1413 return 0;
1414}
1415
1416static int team_user_linkup_en_option_set(struct team *team,
1417 struct team_gsetter_ctx *ctx)
1418{
Jiri Pirko85d59a82012-06-19 05:54:10 +00001419 struct team_port *port = ctx->info->port;
Jiri Pirko71472ec2012-04-10 05:15:44 +00001420
1421 port->user.linkup_enabled = ctx->data.bool_val;
Jiri Pirko85d59a82012-06-19 05:54:10 +00001422 team_refresh_port_linkup(port);
Jiri Pirkof5e0d342013-11-28 18:01:38 +01001423 __team_carrier_check(port->team);
Jiri Pirko71472ec2012-04-10 05:15:44 +00001424 return 0;
1425}
1426
Jiri Pirkoa86fc6b2012-07-27 06:28:54 +00001427static int team_priority_option_get(struct team *team,
1428 struct team_gsetter_ctx *ctx)
1429{
1430 struct team_port *port = ctx->info->port;
1431
1432 ctx->data.s32_val = port->priority;
1433 return 0;
1434}
1435
1436static int team_priority_option_set(struct team *team,
1437 struct team_gsetter_ctx *ctx)
1438{
1439 struct team_port *port = ctx->info->port;
Jiri Pirko6c31ff32013-06-10 17:42:23 +02001440 s32 priority = ctx->data.s32_val;
Jiri Pirkoa86fc6b2012-07-27 06:28:54 +00001441
Jiri Pirko6c31ff32013-06-10 17:42:23 +02001442 if (port->priority == priority)
1443 return 0;
1444 port->priority = priority;
1445 team_queue_override_port_prio_changed(team, port);
Jiri Pirkoa86fc6b2012-07-27 06:28:54 +00001446 return 0;
1447}
1448
Jiri Pirko8ff51052012-07-27 06:28:55 +00001449static int team_queue_id_option_get(struct team *team,
1450 struct team_gsetter_ctx *ctx)
1451{
1452 struct team_port *port = ctx->info->port;
1453
1454 ctx->data.u32_val = port->queue_id;
1455 return 0;
1456}
1457
1458static int team_queue_id_option_set(struct team *team,
1459 struct team_gsetter_ctx *ctx)
1460{
1461 struct team_port *port = ctx->info->port;
Jiri Pirko6c31ff32013-06-10 17:42:23 +02001462 u16 new_queue_id = ctx->data.u32_val;
Jiri Pirko8ff51052012-07-27 06:28:55 +00001463
Jiri Pirko6c31ff32013-06-10 17:42:23 +02001464 if (port->queue_id == new_queue_id)
Jiri Pirko8ff51052012-07-27 06:28:55 +00001465 return 0;
Jiri Pirko6c31ff32013-06-10 17:42:23 +02001466 if (new_queue_id >= team->dev->real_num_tx_queues)
Jiri Pirko8ff51052012-07-27 06:28:55 +00001467 return -EINVAL;
Jiri Pirko6c31ff32013-06-10 17:42:23 +02001468 team_queue_override_port_change_queue_id(team, port, new_queue_id);
Jiri Pirko8ff51052012-07-27 06:28:55 +00001469 return 0;
1470}
1471
Jiri Pirko358b8382011-11-16 11:09:09 +00001472static const struct team_option team_options[] = {
Jiri Pirko3d249d42011-11-11 22:16:48 +00001473 {
1474 .name = "mode",
1475 .type = TEAM_OPTION_TYPE_STRING,
1476 .getter = team_mode_option_get,
1477 .setter = team_mode_option_set,
1478 },
Jiri Pirko71472ec2012-04-10 05:15:44 +00001479 {
Jiri Pirkofc423ff2013-07-20 12:13:52 +02001480 .name = "notify_peers_count",
1481 .type = TEAM_OPTION_TYPE_U32,
1482 .getter = team_notify_peers_count_get,
1483 .setter = team_notify_peers_count_set,
1484 },
1485 {
1486 .name = "notify_peers_interval",
1487 .type = TEAM_OPTION_TYPE_U32,
1488 .getter = team_notify_peers_interval_get,
1489 .setter = team_notify_peers_interval_set,
1490 },
1491 {
Jiri Pirko492b2002013-07-20 12:13:54 +02001492 .name = "mcast_rejoin_count",
1493 .type = TEAM_OPTION_TYPE_U32,
1494 .getter = team_mcast_rejoin_count_get,
1495 .setter = team_mcast_rejoin_count_set,
1496 },
1497 {
1498 .name = "mcast_rejoin_interval",
1499 .type = TEAM_OPTION_TYPE_U32,
1500 .getter = team_mcast_rejoin_interval_get,
1501 .setter = team_mcast_rejoin_interval_set,
1502 },
1503 {
Jiri Pirkoacd69962012-04-20 04:42:06 +00001504 .name = "enabled",
1505 .type = TEAM_OPTION_TYPE_BOOL,
1506 .per_port = true,
1507 .getter = team_port_en_option_get,
1508 .setter = team_port_en_option_set,
1509 },
1510 {
Jiri Pirko71472ec2012-04-10 05:15:44 +00001511 .name = "user_linkup",
1512 .type = TEAM_OPTION_TYPE_BOOL,
1513 .per_port = true,
1514 .getter = team_user_linkup_option_get,
1515 .setter = team_user_linkup_option_set,
1516 },
1517 {
1518 .name = "user_linkup_enabled",
1519 .type = TEAM_OPTION_TYPE_BOOL,
1520 .per_port = true,
1521 .getter = team_user_linkup_en_option_get,
1522 .setter = team_user_linkup_en_option_set,
1523 },
Jiri Pirkoa86fc6b2012-07-27 06:28:54 +00001524 {
1525 .name = "priority",
1526 .type = TEAM_OPTION_TYPE_S32,
1527 .per_port = true,
1528 .getter = team_priority_option_get,
1529 .setter = team_priority_option_set,
1530 },
Jiri Pirko8ff51052012-07-27 06:28:55 +00001531 {
1532 .name = "queue_id",
1533 .type = TEAM_OPTION_TYPE_U32,
1534 .per_port = true,
1535 .getter = team_queue_id_option_get,
1536 .setter = team_queue_id_option_set,
1537 },
Jiri Pirko3d249d42011-11-11 22:16:48 +00001538};
1539
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00001540static struct lock_class_key team_netdev_xmit_lock_key;
1541static struct lock_class_key team_netdev_addr_lock_key;
Eric Dumazetb3c581d2012-10-03 23:18:39 +00001542static struct lock_class_key team_tx_busylock_key;
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00001543
1544static void team_set_lockdep_class_one(struct net_device *dev,
1545 struct netdev_queue *txq,
1546 void *unused)
1547{
1548 lockdep_set_class(&txq->_xmit_lock, &team_netdev_xmit_lock_key);
1549}
1550
1551static void team_set_lockdep_class(struct net_device *dev)
1552{
1553 lockdep_set_class(&dev->addr_list_lock, &team_netdev_addr_lock_key);
1554 netdev_for_each_tx_queue(dev, team_set_lockdep_class_one, NULL);
Eric Dumazetb3c581d2012-10-03 23:18:39 +00001555 dev->qdisc_tx_busylock = &team_tx_busylock_key;
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00001556}
1557
Jiri Pirko3d249d42011-11-11 22:16:48 +00001558static int team_init(struct net_device *dev)
1559{
1560 struct team *team = netdev_priv(dev);
1561 int i;
Jiri Pirko358b8382011-11-16 11:09:09 +00001562 int err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001563
1564 team->dev = dev;
Jiri Pirko61dc3462011-11-16 11:09:08 +00001565 mutex_init(&team->lock);
Jiri Pirkod299cd52012-06-19 05:54:04 +00001566 team_set_no_mode(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001567
WANG Cong1c213bd2014-02-13 11:46:28 -08001568 team->pcpu_stats = netdev_alloc_pcpu_stats(struct team_pcpu_stats);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001569 if (!team->pcpu_stats)
1570 return -ENOMEM;
1571
1572 for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
Jiri Pirko19a0b582012-04-20 04:42:05 +00001573 INIT_HLIST_HEAD(&team->en_port_hlist[i]);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001574 INIT_LIST_HEAD(&team->port_list);
Jiri Pirko8ff51052012-07-27 06:28:55 +00001575 err = team_queue_override_init(team);
1576 if (err)
1577 goto err_team_queue_override_init;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001578
1579 team_adjust_ops(team);
1580
1581 INIT_LIST_HEAD(&team->option_list);
Jiri Pirko80f7c662012-04-10 05:15:42 +00001582 INIT_LIST_HEAD(&team->option_inst_list);
Jiri Pirkofc423ff2013-07-20 12:13:52 +02001583
1584 team_notify_peers_init(team);
Jiri Pirko492b2002013-07-20 12:13:54 +02001585 team_mcast_rejoin_init(team);
Jiri Pirkofc423ff2013-07-20 12:13:52 +02001586
Jiri Pirko358b8382011-11-16 11:09:09 +00001587 err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
1588 if (err)
1589 goto err_options_register;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001590 netif_carrier_off(dev);
1591
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00001592 team_set_lockdep_class(dev);
1593
Jiri Pirko3d249d42011-11-11 22:16:48 +00001594 return 0;
Jiri Pirko358b8382011-11-16 11:09:09 +00001595
1596err_options_register:
Jiri Pirko492b2002013-07-20 12:13:54 +02001597 team_mcast_rejoin_fini(team);
Jiri Pirkofc423ff2013-07-20 12:13:52 +02001598 team_notify_peers_fini(team);
Jiri Pirko8ff51052012-07-27 06:28:55 +00001599 team_queue_override_fini(team);
1600err_team_queue_override_init:
Jiri Pirko358b8382011-11-16 11:09:09 +00001601 free_percpu(team->pcpu_stats);
1602
1603 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001604}
1605
1606static void team_uninit(struct net_device *dev)
1607{
1608 struct team *team = netdev_priv(dev);
1609 struct team_port *port;
1610 struct team_port *tmp;
1611
Jiri Pirko61dc3462011-11-16 11:09:08 +00001612 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001613 list_for_each_entry_safe(port, tmp, &team->port_list, list)
1614 team_port_del(team, port->dev);
1615
1616 __team_change_mode(team, NULL); /* cleanup */
1617 __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
Jiri Pirko492b2002013-07-20 12:13:54 +02001618 team_mcast_rejoin_fini(team);
Jiri Pirkofc423ff2013-07-20 12:13:52 +02001619 team_notify_peers_fini(team);
Jiri Pirko8ff51052012-07-27 06:28:55 +00001620 team_queue_override_fini(team);
Jiri Pirko61dc3462011-11-16 11:09:08 +00001621 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001622}
1623
1624static void team_destructor(struct net_device *dev)
1625{
1626 struct team *team = netdev_priv(dev);
1627
1628 free_percpu(team->pcpu_stats);
1629 free_netdev(dev);
1630}
1631
1632static int team_open(struct net_device *dev)
1633{
Jiri Pirko3d249d42011-11-11 22:16:48 +00001634 return 0;
1635}
1636
1637static int team_close(struct net_device *dev)
1638{
Jiri Pirko3d249d42011-11-11 22:16:48 +00001639 return 0;
1640}
1641
1642/*
1643 * note: already called with rcu_read_lock
1644 */
1645static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
1646{
1647 struct team *team = netdev_priv(dev);
Jiri Pirko8ff51052012-07-27 06:28:55 +00001648 bool tx_success;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001649 unsigned int len = skb->len;
1650
Jiri Pirko8ff51052012-07-27 06:28:55 +00001651 tx_success = team_queue_override_transmit(team, skb);
1652 if (!tx_success)
1653 tx_success = team->ops.transmit(team, skb);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001654 if (tx_success) {
1655 struct team_pcpu_stats *pcpu_stats;
1656
1657 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
1658 u64_stats_update_begin(&pcpu_stats->syncp);
1659 pcpu_stats->tx_packets++;
1660 pcpu_stats->tx_bytes += len;
1661 u64_stats_update_end(&pcpu_stats->syncp);
1662 } else {
1663 this_cpu_inc(team->pcpu_stats->tx_dropped);
1664 }
1665
1666 return NETDEV_TX_OK;
1667}
1668
Jason Wangf663dd92014-01-10 16:18:26 +08001669static u16 team_select_queue(struct net_device *dev, struct sk_buff *skb,
Daniel Borkmann99932d42014-02-16 15:55:20 +01001670 void *accel_priv, select_queue_fallback_t fallback)
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00001671{
1672 /*
1673 * This helper function exists to help dev_pick_tx get the correct
1674 * destination queue. Using a helper function skips a call to
1675 * skb_tx_hash and will put the skbs in the queue we expect on their
1676 * way down to the team driver.
1677 */
1678 u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
1679
1680 /*
1681 * Save the original txq to restore before passing to the driver
1682 */
1683 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
1684
1685 if (unlikely(txq >= dev->real_num_tx_queues)) {
1686 do {
1687 txq -= dev->real_num_tx_queues;
1688 } while (txq >= dev->real_num_tx_queues);
1689 }
1690 return txq;
1691}
1692
Jiri Pirko3d249d42011-11-11 22:16:48 +00001693static void team_change_rx_flags(struct net_device *dev, int change)
1694{
1695 struct team *team = netdev_priv(dev);
1696 struct team_port *port;
1697 int inc;
1698
1699 rcu_read_lock();
1700 list_for_each_entry_rcu(port, &team->port_list, list) {
1701 if (change & IFF_PROMISC) {
1702 inc = dev->flags & IFF_PROMISC ? 1 : -1;
1703 dev_set_promiscuity(port->dev, inc);
1704 }
1705 if (change & IFF_ALLMULTI) {
1706 inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
1707 dev_set_allmulti(port->dev, inc);
1708 }
1709 }
1710 rcu_read_unlock();
1711}
1712
1713static void team_set_rx_mode(struct net_device *dev)
1714{
1715 struct team *team = netdev_priv(dev);
1716 struct team_port *port;
1717
1718 rcu_read_lock();
1719 list_for_each_entry_rcu(port, &team->port_list, list) {
Vlad Yasevich72b27032013-04-15 09:54:26 +00001720 dev_uc_sync_multiple(port->dev, dev);
1721 dev_mc_sync_multiple(port->dev, dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001722 }
1723 rcu_read_unlock();
1724}
1725
1726static int team_set_mac_address(struct net_device *dev, void *p)
1727{
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001728 struct sockaddr *addr = p;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001729 struct team *team = netdev_priv(dev);
1730 struct team_port *port;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001731
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001732 if (dev->type == ARPHRD_ETHER && !is_valid_ether_addr(addr->sa_data))
1733 return -EADDRNOTAVAIL;
1734 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001735 rcu_read_lock();
1736 list_for_each_entry_rcu(port, &team->port_list, list)
Jiri Pirko1d76efe2012-08-17 04:00:48 +00001737 if (team->ops.port_change_dev_addr)
1738 team->ops.port_change_dev_addr(team, port);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001739 rcu_read_unlock();
1740 return 0;
1741}
1742
1743static int team_change_mtu(struct net_device *dev, int new_mtu)
1744{
1745 struct team *team = netdev_priv(dev);
1746 struct team_port *port;
1747 int err;
1748
1749 /*
1750 * Alhough this is reader, it's guarded by team lock. It's not possible
1751 * to traverse list in reverse under rcu_read_lock
1752 */
Jiri Pirko61dc3462011-11-16 11:09:08 +00001753 mutex_lock(&team->lock);
Jiri Pirko9d0d68f2014-05-29 20:46:17 +02001754 team->port_mtu_change_allowed = true;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001755 list_for_each_entry(port, &team->port_list, list) {
1756 err = dev_set_mtu(port->dev, new_mtu);
1757 if (err) {
1758 netdev_err(dev, "Device %s failed to change mtu",
1759 port->dev->name);
1760 goto unwind;
1761 }
1762 }
Jiri Pirko9d0d68f2014-05-29 20:46:17 +02001763 team->port_mtu_change_allowed = false;
Jiri Pirko61dc3462011-11-16 11:09:08 +00001764 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001765
1766 dev->mtu = new_mtu;
1767
1768 return 0;
1769
1770unwind:
1771 list_for_each_entry_continue_reverse(port, &team->port_list, list)
1772 dev_set_mtu(port->dev, dev->mtu);
Jiri Pirko9d0d68f2014-05-29 20:46:17 +02001773 team->port_mtu_change_allowed = false;
Jiri Pirko61dc3462011-11-16 11:09:08 +00001774 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001775
1776 return err;
1777}
1778
1779static struct rtnl_link_stats64 *
1780team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1781{
1782 struct team *team = netdev_priv(dev);
1783 struct team_pcpu_stats *p;
1784 u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
1785 u32 rx_dropped = 0, tx_dropped = 0;
1786 unsigned int start;
1787 int i;
1788
1789 for_each_possible_cpu(i) {
1790 p = per_cpu_ptr(team->pcpu_stats, i);
1791 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07001792 start = u64_stats_fetch_begin_irq(&p->syncp);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001793 rx_packets = p->rx_packets;
1794 rx_bytes = p->rx_bytes;
1795 rx_multicast = p->rx_multicast;
1796 tx_packets = p->tx_packets;
1797 tx_bytes = p->tx_bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07001798 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Jiri Pirko3d249d42011-11-11 22:16:48 +00001799
1800 stats->rx_packets += rx_packets;
1801 stats->rx_bytes += rx_bytes;
1802 stats->multicast += rx_multicast;
1803 stats->tx_packets += tx_packets;
1804 stats->tx_bytes += tx_bytes;
1805 /*
1806 * rx_dropped & tx_dropped are u32, updated
1807 * without syncp protection.
1808 */
1809 rx_dropped += p->rx_dropped;
1810 tx_dropped += p->tx_dropped;
1811 }
1812 stats->rx_dropped = rx_dropped;
1813 stats->tx_dropped = tx_dropped;
1814 return stats;
1815}
1816
Patrick McHardy80d5c362013-04-19 02:04:28 +00001817static int team_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001818{
1819 struct team *team = netdev_priv(dev);
1820 struct team_port *port;
Jiri Pirko87002b02011-12-08 04:11:17 +00001821 int err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001822
Jiri Pirko87002b02011-12-08 04:11:17 +00001823 /*
1824 * Alhough this is reader, it's guarded by team lock. It's not possible
1825 * to traverse list in reverse under rcu_read_lock
1826 */
1827 mutex_lock(&team->lock);
1828 list_for_each_entry(port, &team->port_list, list) {
Patrick McHardy80d5c362013-04-19 02:04:28 +00001829 err = vlan_vid_add(port->dev, proto, vid);
Jiri Pirko87002b02011-12-08 04:11:17 +00001830 if (err)
1831 goto unwind;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001832 }
Jiri Pirko87002b02011-12-08 04:11:17 +00001833 mutex_unlock(&team->lock);
Jiri Pirko8e586132011-12-08 19:52:37 -05001834
1835 return 0;
Jiri Pirko87002b02011-12-08 04:11:17 +00001836
1837unwind:
1838 list_for_each_entry_continue_reverse(port, &team->port_list, list)
Patrick McHardy80d5c362013-04-19 02:04:28 +00001839 vlan_vid_del(port->dev, proto, vid);
Jiri Pirko87002b02011-12-08 04:11:17 +00001840 mutex_unlock(&team->lock);
1841
1842 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001843}
1844
Patrick McHardy80d5c362013-04-19 02:04:28 +00001845static int team_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001846{
1847 struct team *team = netdev_priv(dev);
1848 struct team_port *port;
1849
1850 rcu_read_lock();
Jiri Pirko87002b02011-12-08 04:11:17 +00001851 list_for_each_entry_rcu(port, &team->port_list, list)
Patrick McHardy80d5c362013-04-19 02:04:28 +00001852 vlan_vid_del(port->dev, proto, vid);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001853 rcu_read_unlock();
Jiri Pirko8e586132011-12-08 19:52:37 -05001854
1855 return 0;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001856}
1857
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001858#ifdef CONFIG_NET_POLL_CONTROLLER
1859static void team_poll_controller(struct net_device *dev)
1860{
1861}
1862
1863static void __team_netpoll_cleanup(struct team *team)
1864{
1865 struct team_port *port;
1866
1867 list_for_each_entry(port, &team->port_list, list)
1868 team_port_disable_netpoll(port);
1869}
1870
1871static void team_netpoll_cleanup(struct net_device *dev)
1872{
1873 struct team *team = netdev_priv(dev);
1874
1875 mutex_lock(&team->lock);
1876 __team_netpoll_cleanup(team);
1877 mutex_unlock(&team->lock);
1878}
1879
1880static int team_netpoll_setup(struct net_device *dev,
Eric W. Biedermana8779ec2014-03-27 15:36:38 -07001881 struct netpoll_info *npifo)
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001882{
1883 struct team *team = netdev_priv(dev);
1884 struct team_port *port;
Jiri Pirko3324d022012-07-24 01:20:48 +00001885 int err = 0;
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001886
1887 mutex_lock(&team->lock);
1888 list_for_each_entry(port, &team->port_list, list) {
Eric W. Biedermana8779ec2014-03-27 15:36:38 -07001889 err = team_port_enable_netpoll(team, port);
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001890 if (err) {
1891 __team_netpoll_cleanup(team);
1892 break;
1893 }
1894 }
1895 mutex_unlock(&team->lock);
1896 return err;
1897}
1898#endif
1899
Jiri Pirko3d249d42011-11-11 22:16:48 +00001900static int team_add_slave(struct net_device *dev, struct net_device *port_dev)
1901{
1902 struct team *team = netdev_priv(dev);
1903 int err;
1904
Jiri Pirko61dc3462011-11-16 11:09:08 +00001905 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001906 err = team_port_add(team, port_dev);
Jiri Pirko61dc3462011-11-16 11:09:08 +00001907 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001908 return err;
1909}
1910
1911static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
1912{
1913 struct team *team = netdev_priv(dev);
1914 int err;
1915
Jiri Pirko61dc3462011-11-16 11:09:08 +00001916 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001917 err = team_port_del(team, port_dev);
Jiri Pirko61dc3462011-11-16 11:09:08 +00001918 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001919 return err;
1920}
1921
Jiri Pirko234a8fd2011-11-17 04:16:04 +00001922static netdev_features_t team_fix_features(struct net_device *dev,
1923 netdev_features_t features)
1924{
1925 struct team_port *port;
1926 struct team *team = netdev_priv(dev);
1927 netdev_features_t mask;
1928
Roopa Prabhua16a8ee72015-01-29 22:40:17 -08001929 mask = features | NETIF_F_HW_SWITCH_OFFLOAD;
Jiri Pirko234a8fd2011-11-17 04:16:04 +00001930 features &= ~NETIF_F_ONE_FOR_ALL;
1931 features |= NETIF_F_ALL_FOR_ALL;
1932
1933 rcu_read_lock();
1934 list_for_each_entry_rcu(port, &team->port_list, list) {
1935 features = netdev_increment_features(features,
1936 port->dev->features,
1937 mask);
1938 }
1939 rcu_read_unlock();
1940 return features;
1941}
1942
Flavio Leitner4cafe372012-12-29 15:31:01 +00001943static int team_change_carrier(struct net_device *dev, bool new_carrier)
1944{
Flavio Leitnere1854832013-02-05 09:30:55 +00001945 struct team *team = netdev_priv(dev);
1946
1947 team->user_carrier_enabled = true;
1948
Flavio Leitner4cafe372012-12-29 15:31:01 +00001949 if (new_carrier)
1950 netif_carrier_on(dev);
1951 else
1952 netif_carrier_off(dev);
1953 return 0;
1954}
1955
Jiri Pirko3d249d42011-11-11 22:16:48 +00001956static const struct net_device_ops team_netdev_ops = {
1957 .ndo_init = team_init,
1958 .ndo_uninit = team_uninit,
1959 .ndo_open = team_open,
1960 .ndo_stop = team_close,
1961 .ndo_start_xmit = team_xmit,
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00001962 .ndo_select_queue = team_select_queue,
Jiri Pirko3d249d42011-11-11 22:16:48 +00001963 .ndo_change_rx_flags = team_change_rx_flags,
1964 .ndo_set_rx_mode = team_set_rx_mode,
1965 .ndo_set_mac_address = team_set_mac_address,
1966 .ndo_change_mtu = team_change_mtu,
1967 .ndo_get_stats64 = team_get_stats64,
1968 .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid,
1969 .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid,
Jiri Pirkobd2d0832012-07-17 05:22:36 +00001970#ifdef CONFIG_NET_POLL_CONTROLLER
1971 .ndo_poll_controller = team_poll_controller,
1972 .ndo_netpoll_setup = team_netpoll_setup,
1973 .ndo_netpoll_cleanup = team_netpoll_cleanup,
1974#endif
Jiri Pirko3d249d42011-11-11 22:16:48 +00001975 .ndo_add_slave = team_add_slave,
1976 .ndo_del_slave = team_del_slave,
Jiri Pirko234a8fd2011-11-17 04:16:04 +00001977 .ndo_fix_features = team_fix_features,
Flavio Leitner4cafe372012-12-29 15:31:01 +00001978 .ndo_change_carrier = team_change_carrier,
Roopa Prabhua16a8ee72015-01-29 22:40:17 -08001979 .ndo_bridge_setlink = ndo_dflt_netdev_switch_port_bridge_setlink,
1980 .ndo_bridge_dellink = ndo_dflt_netdev_switch_port_bridge_dellink,
Jiri Pirko3d249d42011-11-11 22:16:48 +00001981};
1982
Flavio Leitner7f51c582012-12-29 16:37:33 +00001983/***********************
1984 * ethtool interface
1985 ***********************/
1986
1987static void team_ethtool_get_drvinfo(struct net_device *dev,
1988 struct ethtool_drvinfo *drvinfo)
1989{
Flavio Leitner3066af52013-01-05 02:53:10 +00001990 strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
1991 strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
Flavio Leitner7f51c582012-12-29 16:37:33 +00001992}
1993
1994static const struct ethtool_ops team_ethtool_ops = {
1995 .get_drvinfo = team_ethtool_get_drvinfo,
1996 .get_link = ethtool_op_get_link,
1997};
Jiri Pirko3d249d42011-11-11 22:16:48 +00001998
1999/***********************
2000 * rt netlink interface
2001 ***********************/
2002
Jiri Pirko1d76efe2012-08-17 04:00:48 +00002003static void team_setup_by_port(struct net_device *dev,
2004 struct net_device *port_dev)
2005{
2006 dev->header_ops = port_dev->header_ops;
2007 dev->type = port_dev->type;
2008 dev->hard_header_len = port_dev->hard_header_len;
2009 dev->addr_len = port_dev->addr_len;
2010 dev->mtu = port_dev->mtu;
2011 memcpy(dev->broadcast, port_dev->broadcast, port_dev->addr_len);
Bjørn Mork93af7352013-08-30 18:08:48 +02002012 eth_hw_addr_inherit(dev, port_dev);
Jiri Pirko1d76efe2012-08-17 04:00:48 +00002013}
2014
2015static int team_dev_type_check_change(struct net_device *dev,
2016 struct net_device *port_dev)
2017{
2018 struct team *team = netdev_priv(dev);
2019 char *portname = port_dev->name;
2020 int err;
2021
2022 if (dev->type == port_dev->type)
2023 return 0;
2024 if (!list_empty(&team->port_list)) {
2025 netdev_err(dev, "Device %s is of different type\n", portname);
2026 return -EBUSY;
2027 }
2028 err = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE, dev);
2029 err = notifier_to_errno(err);
2030 if (err) {
2031 netdev_err(dev, "Refused to change device type\n");
2032 return err;
2033 }
2034 dev_uc_flush(dev);
2035 dev_mc_flush(dev);
2036 team_setup_by_port(dev, port_dev);
2037 call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
2038 return 0;
2039}
2040
Jiri Pirko3d249d42011-11-11 22:16:48 +00002041static void team_setup(struct net_device *dev)
2042{
2043 ether_setup(dev);
2044
2045 dev->netdev_ops = &team_netdev_ops;
Flavio Leitner7f51c582012-12-29 16:37:33 +00002046 dev->ethtool_ops = &team_ethtool_ops;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002047 dev->destructor = team_destructor;
2048 dev->tx_queue_len = 0;
2049 dev->flags |= IFF_MULTICAST;
2050 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
2051
2052 /*
2053 * Indicate we support unicast address filtering. That way core won't
2054 * bring us to promisc mode in case a unicast addr is added.
2055 * Let this up to underlay drivers.
2056 */
Jiri Pirko5a1d1c82012-06-29 05:10:07 +00002057 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002058
2059 dev->features |= NETIF_F_LLTX;
2060 dev->features |= NETIF_F_GRO;
Weilong Chen99301ba2014-01-23 11:07:22 +08002061
2062 /* Don't allow team devices to change network namespaces. */
2063 dev->features |= NETIF_F_NETNS_LOCAL;
2064
Jiri Pirko3ed71472012-11-28 06:13:10 +00002065 dev->hw_features = TEAM_VLAN_FEATURES |
Patrick McHardyf6469682013-04-19 02:04:27 +00002066 NETIF_F_HW_VLAN_CTAG_TX |
2067 NETIF_F_HW_VLAN_CTAG_RX |
2068 NETIF_F_HW_VLAN_CTAG_FILTER;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002069
Jiri Pirko3ed71472012-11-28 06:13:10 +00002070 dev->hw_features &= ~(NETIF_F_ALL_CSUM & ~NETIF_F_HW_CSUM);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002071 dev->features |= dev->hw_features;
2072}
2073
2074static int team_newlink(struct net *src_net, struct net_device *dev,
2075 struct nlattr *tb[], struct nlattr *data[])
2076{
Jiri Pirko3d249d42011-11-11 22:16:48 +00002077 if (tb[IFLA_ADDRESS] == NULL)
Danny Kukawka7ce5d222012-02-15 06:45:40 +00002078 eth_hw_addr_random(dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002079
Toshiaki Makitaff204cc2014-08-05 15:58:50 +09002080 return register_netdevice(dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002081}
2082
2083static int team_validate(struct nlattr *tb[], struct nlattr *data[])
2084{
2085 if (tb[IFLA_ADDRESS]) {
2086 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
2087 return -EINVAL;
2088 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
2089 return -EADDRNOTAVAIL;
2090 }
2091 return 0;
2092}
2093
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00002094static unsigned int team_get_num_tx_queues(void)
2095{
2096 return TEAM_DEFAULT_NUM_TX_QUEUES;
2097}
2098
2099static unsigned int team_get_num_rx_queues(void)
2100{
2101 return TEAM_DEFAULT_NUM_RX_QUEUES;
2102}
2103
Jiri Pirko3d249d42011-11-11 22:16:48 +00002104static struct rtnl_link_ops team_link_ops __read_mostly = {
Jiri Pirko6c85f2b2012-07-20 02:28:51 +00002105 .kind = DRV_NAME,
2106 .priv_size = sizeof(struct team),
2107 .setup = team_setup,
2108 .newlink = team_newlink,
2109 .validate = team_validate,
2110 .get_num_tx_queues = team_get_num_tx_queues,
2111 .get_num_rx_queues = team_get_num_rx_queues,
Jiri Pirko3d249d42011-11-11 22:16:48 +00002112};
2113
2114
2115/***********************************
2116 * Generic netlink custom interface
2117 ***********************************/
2118
2119static struct genl_family team_nl_family = {
2120 .id = GENL_ID_GENERATE,
2121 .name = TEAM_GENL_NAME,
2122 .version = TEAM_GENL_VERSION,
2123 .maxattr = TEAM_ATTR_MAX,
2124 .netnsok = true,
2125};
2126
2127static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
2128 [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, },
2129 [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 },
2130 [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED },
2131 [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED },
2132};
2133
2134static const struct nla_policy
2135team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
2136 [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
2137 [TEAM_ATTR_OPTION_NAME] = {
2138 .type = NLA_STRING,
2139 .len = TEAM_STRING_MAX_LEN,
2140 },
2141 [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG },
2142 [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 },
Jiri Pirko26155982012-04-04 12:16:26 +00002143 [TEAM_ATTR_OPTION_DATA] = { .type = NLA_BINARY },
Jiri Pirko3d249d42011-11-11 22:16:48 +00002144};
2145
2146static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
2147{
2148 struct sk_buff *msg;
2149 void *hdr;
2150 int err;
2151
Thomas Graf58050fc2012-06-28 03:57:45 +00002152 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002153 if (!msg)
2154 return -ENOMEM;
2155
Eric W. Biederman15e47302012-09-07 20:12:54 +00002156 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
Jiri Pirko3d249d42011-11-11 22:16:48 +00002157 &team_nl_family, 0, TEAM_CMD_NOOP);
Wei Yongjuna326e6d2012-09-24 18:29:35 +00002158 if (!hdr) {
2159 err = -EMSGSIZE;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002160 goto err_msg_put;
2161 }
2162
2163 genlmsg_end(msg, hdr);
2164
Eric W. Biederman15e47302012-09-07 20:12:54 +00002165 return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002166
2167err_msg_put:
2168 nlmsg_free(msg);
2169
2170 return err;
2171}
2172
2173/*
2174 * Netlink cmd functions should be locked by following two functions.
Jiri Pirko8c0713a2011-11-16 11:55:54 +00002175 * Since dev gets held here, that ensures dev won't disappear in between.
Jiri Pirko3d249d42011-11-11 22:16:48 +00002176 */
2177static struct team *team_nl_team_get(struct genl_info *info)
2178{
2179 struct net *net = genl_info_net(info);
2180 int ifindex;
2181 struct net_device *dev;
2182 struct team *team;
2183
2184 if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
2185 return NULL;
2186
2187 ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
Jiri Pirko8c0713a2011-11-16 11:55:54 +00002188 dev = dev_get_by_index(net, ifindex);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002189 if (!dev || dev->netdev_ops != &team_netdev_ops) {
Jiri Pirko8c0713a2011-11-16 11:55:54 +00002190 if (dev)
2191 dev_put(dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002192 return NULL;
2193 }
2194
2195 team = netdev_priv(dev);
Jiri Pirko61dc3462011-11-16 11:09:08 +00002196 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002197 return team;
2198}
2199
2200static void team_nl_team_put(struct team *team)
2201{
Jiri Pirko61dc3462011-11-16 11:09:08 +00002202 mutex_unlock(&team->lock);
Jiri Pirko8c0713a2011-11-16 11:55:54 +00002203 dev_put(team->dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002204}
2205
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002206typedef int team_nl_send_func_t(struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002207 struct team *team, u32 portid);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002208
Eric W. Biederman15e47302012-09-07 20:12:54 +00002209static int team_nl_send_unicast(struct sk_buff *skb, struct team *team, u32 portid)
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002210{
Eric W. Biederman15e47302012-09-07 20:12:54 +00002211 return genlmsg_unicast(dev_net(team->dev), skb, portid);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002212}
2213
Jiri Pirko01048d92012-06-19 05:54:14 +00002214static int team_nl_fill_one_option_get(struct sk_buff *skb, struct team *team,
2215 struct team_option_inst *opt_inst)
2216{
2217 struct nlattr *option_item;
2218 struct team_option *option = opt_inst->option;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002219 struct team_option_inst_info *opt_inst_info = &opt_inst->info;
Jiri Pirko01048d92012-06-19 05:54:14 +00002220 struct team_gsetter_ctx ctx;
2221 int err;
2222
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002223 ctx.info = opt_inst_info;
2224 err = team_option_get(team, opt_inst, &ctx);
2225 if (err)
2226 return err;
2227
Jiri Pirko01048d92012-06-19 05:54:14 +00002228 option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
2229 if (!option_item)
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002230 return -EMSGSIZE;
Jiri Pirko01048d92012-06-19 05:54:14 +00002231
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002232 if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name))
2233 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002234 if (opt_inst_info->port &&
2235 nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX,
2236 opt_inst_info->port->dev->ifindex))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002237 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002238 if (opt_inst->option->array_size &&
2239 nla_put_u32(skb, TEAM_ATTR_OPTION_ARRAY_INDEX,
2240 opt_inst_info->array_index))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002241 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002242
2243 switch (option->type) {
2244 case TEAM_OPTION_TYPE_U32:
2245 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002246 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002247 if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.u32_val))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002248 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002249 break;
2250 case TEAM_OPTION_TYPE_STRING:
2251 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002252 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002253 if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA,
2254 ctx.data.str_val))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002255 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002256 break;
2257 case TEAM_OPTION_TYPE_BINARY:
2258 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002259 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002260 if (nla_put(skb, TEAM_ATTR_OPTION_DATA, ctx.data.bin_val.len,
2261 ctx.data.bin_val.ptr))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002262 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002263 break;
2264 case TEAM_OPTION_TYPE_BOOL:
2265 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002266 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002267 if (ctx.data.bool_val &&
2268 nla_put_flag(skb, TEAM_ATTR_OPTION_DATA))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002269 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00002270 break;
Jiri Pirko69821632012-07-27 06:28:53 +00002271 case TEAM_OPTION_TYPE_S32:
2272 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_S32))
2273 goto nest_cancel;
2274 if (nla_put_s32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.s32_val))
2275 goto nest_cancel;
2276 break;
Jiri Pirko01048d92012-06-19 05:54:14 +00002277 default:
2278 BUG();
2279 }
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002280 if (opt_inst->removed && nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED))
2281 goto nest_cancel;
2282 if (opt_inst->changed) {
2283 if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED))
2284 goto nest_cancel;
2285 opt_inst->changed = false;
2286 }
Jiri Pirko01048d92012-06-19 05:54:14 +00002287 nla_nest_end(skb, option_item);
2288 return 0;
2289
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002290nest_cancel:
2291 nla_nest_cancel(skb, option_item);
2292 return -EMSGSIZE;
Jiri Pirko01048d92012-06-19 05:54:14 +00002293}
2294
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002295static int __send_and_alloc_skb(struct sk_buff **pskb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002296 struct team *team, u32 portid,
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002297 team_nl_send_func_t *send_func)
2298{
2299 int err;
2300
2301 if (*pskb) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002302 err = send_func(*pskb, team, portid);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002303 if (err)
2304 return err;
2305 }
Thomas Graf58050fc2012-06-28 03:57:45 +00002306 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002307 if (!*pskb)
2308 return -ENOMEM;
2309 return 0;
2310}
2311
Eric W. Biederman15e47302012-09-07 20:12:54 +00002312static int team_nl_send_options_get(struct team *team, u32 portid, u32 seq,
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002313 int flags, team_nl_send_func_t *send_func,
Jiri Pirko01048d92012-06-19 05:54:14 +00002314 struct list_head *sel_opt_inst_list)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002315{
2316 struct nlattr *option_list;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002317 struct nlmsghdr *nlh;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002318 void *hdr;
Jiri Pirko80f7c662012-04-10 05:15:42 +00002319 struct team_option_inst *opt_inst;
2320 int err;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002321 struct sk_buff *skb = NULL;
2322 bool incomplete;
2323 int i;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002324
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002325 opt_inst = list_first_entry(sel_opt_inst_list,
2326 struct team_option_inst, tmp_list);
2327
2328start_again:
Eric W. Biederman15e47302012-09-07 20:12:54 +00002329 err = __send_and_alloc_skb(&skb, team, portid, send_func);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002330 if (err)
2331 return err;
2332
Eric W. Biederman15e47302012-09-07 20:12:54 +00002333 hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
Jiri Pirko3d249d42011-11-11 22:16:48 +00002334 TEAM_CMD_OPTIONS_GET);
Wei Yongjuna326e6d2012-09-24 18:29:35 +00002335 if (!hdr)
2336 return -EMSGSIZE;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002337
David S. Miller86ebb022012-04-01 20:25:18 -04002338 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2339 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002340 option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
2341 if (!option_list)
Jiri Pirko75db9862012-06-19 05:54:12 +00002342 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002343
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002344 i = 0;
2345 incomplete = false;
2346 list_for_each_entry_from(opt_inst, sel_opt_inst_list, tmp_list) {
Jiri Pirko01048d92012-06-19 05:54:14 +00002347 err = team_nl_fill_one_option_get(skb, team, opt_inst);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002348 if (err) {
2349 if (err == -EMSGSIZE) {
2350 if (!i)
2351 goto errout;
2352 incomplete = true;
2353 break;
2354 }
Jiri Pirko01048d92012-06-19 05:54:14 +00002355 goto errout;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002356 }
2357 i++;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002358 }
2359
2360 nla_nest_end(skb, option_list);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002361 genlmsg_end(skb, hdr);
2362 if (incomplete)
2363 goto start_again;
2364
2365send_done:
Eric W. Biederman15e47302012-09-07 20:12:54 +00002366 nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002367 if (!nlh) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002368 err = __send_and_alloc_skb(&skb, team, portid, send_func);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002369 if (err)
2370 goto errout;
2371 goto send_done;
2372 }
2373
Eric W. Biederman15e47302012-09-07 20:12:54 +00002374 return send_func(skb, team, portid);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002375
2376nla_put_failure:
Jiri Pirko80f7c662012-04-10 05:15:42 +00002377 err = -EMSGSIZE;
2378errout:
Jiri Pirko3d249d42011-11-11 22:16:48 +00002379 genlmsg_cancel(skb, hdr);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002380 nlmsg_free(skb);
Jiri Pirko80f7c662012-04-10 05:15:42 +00002381 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002382}
2383
Jiri Pirko3d249d42011-11-11 22:16:48 +00002384static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
2385{
2386 struct team *team;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002387 struct team_option_inst *opt_inst;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002388 int err;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002389 LIST_HEAD(sel_opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002390
2391 team = team_nl_team_get(info);
2392 if (!team)
2393 return -EINVAL;
2394
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002395 list_for_each_entry(opt_inst, &team->option_inst_list, list)
2396 list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
Eric W. Biederman15e47302012-09-07 20:12:54 +00002397 err = team_nl_send_options_get(team, info->snd_portid, info->snd_seq,
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002398 NLM_F_ACK, team_nl_send_unicast,
2399 &sel_opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002400
2401 team_nl_team_put(team);
2402
2403 return err;
2404}
2405
Jiri Pirko2fcdb2c2012-06-19 05:54:20 +00002406static int team_nl_send_event_options_get(struct team *team,
2407 struct list_head *sel_opt_inst_list);
2408
Jiri Pirko3d249d42011-11-11 22:16:48 +00002409static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
2410{
2411 struct team *team;
2412 int err = 0;
2413 int i;
2414 struct nlattr *nl_option;
Jiri Pirko2fcdb2c2012-06-19 05:54:20 +00002415 LIST_HEAD(opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002416
2417 team = team_nl_team_get(info);
2418 if (!team)
2419 return -EINVAL;
2420
2421 err = -EINVAL;
2422 if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
2423 err = -EINVAL;
2424 goto team_put;
2425 }
2426
2427 nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
Jiri Pirko80f7c662012-04-10 05:15:42 +00002428 struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1];
Jiri Pirkob1303322012-06-19 05:54:08 +00002429 struct nlattr *attr;
Jiri Pirko14f066b2012-04-10 05:15:43 +00002430 struct nlattr *attr_data;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002431 enum team_option_type opt_type;
Jiri Pirko80f7c662012-04-10 05:15:42 +00002432 int opt_port_ifindex = 0; /* != 0 for per-port options */
Jiri Pirkob1303322012-06-19 05:54:08 +00002433 u32 opt_array_index = 0;
2434 bool opt_is_array = false;
Jiri Pirko80f7c662012-04-10 05:15:42 +00002435 struct team_option_inst *opt_inst;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002436 char *opt_name;
2437 bool opt_found = false;
2438
2439 if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
2440 err = -EINVAL;
2441 goto team_put;
2442 }
Jiri Pirko80f7c662012-04-10 05:15:42 +00002443 err = nla_parse_nested(opt_attrs, TEAM_ATTR_OPTION_MAX,
Jiri Pirko3d249d42011-11-11 22:16:48 +00002444 nl_option, team_nl_option_policy);
2445 if (err)
2446 goto team_put;
Jiri Pirko80f7c662012-04-10 05:15:42 +00002447 if (!opt_attrs[TEAM_ATTR_OPTION_NAME] ||
Jiri Pirko14f066b2012-04-10 05:15:43 +00002448 !opt_attrs[TEAM_ATTR_OPTION_TYPE]) {
Jiri Pirko3d249d42011-11-11 22:16:48 +00002449 err = -EINVAL;
2450 goto team_put;
2451 }
Jiri Pirko80f7c662012-04-10 05:15:42 +00002452 switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) {
Jiri Pirko3d249d42011-11-11 22:16:48 +00002453 case NLA_U32:
2454 opt_type = TEAM_OPTION_TYPE_U32;
2455 break;
2456 case NLA_STRING:
2457 opt_type = TEAM_OPTION_TYPE_STRING;
2458 break;
Jiri Pirko26155982012-04-04 12:16:26 +00002459 case NLA_BINARY:
2460 opt_type = TEAM_OPTION_TYPE_BINARY;
2461 break;
Jiri Pirko14f066b2012-04-10 05:15:43 +00002462 case NLA_FLAG:
2463 opt_type = TEAM_OPTION_TYPE_BOOL;
2464 break;
Jiri Pirko69821632012-07-27 06:28:53 +00002465 case NLA_S32:
2466 opt_type = TEAM_OPTION_TYPE_S32;
2467 break;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002468 default:
2469 goto team_put;
2470 }
2471
Jiri Pirko14f066b2012-04-10 05:15:43 +00002472 attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA];
2473 if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) {
2474 err = -EINVAL;
2475 goto team_put;
2476 }
2477
Jiri Pirko80f7c662012-04-10 05:15:42 +00002478 opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]);
Jiri Pirkob1303322012-06-19 05:54:08 +00002479 attr = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX];
2480 if (attr)
2481 opt_port_ifindex = nla_get_u32(attr);
2482
2483 attr = opt_attrs[TEAM_ATTR_OPTION_ARRAY_INDEX];
2484 if (attr) {
2485 opt_is_array = true;
2486 opt_array_index = nla_get_u32(attr);
2487 }
Jiri Pirko3d249d42011-11-11 22:16:48 +00002488
Jiri Pirko80f7c662012-04-10 05:15:42 +00002489 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2490 struct team_option *option = opt_inst->option;
Jiri Pirko80f7c662012-04-10 05:15:42 +00002491 struct team_gsetter_ctx ctx;
Jiri Pirko85d59a82012-06-19 05:54:10 +00002492 struct team_option_inst_info *opt_inst_info;
Jiri Pirko80f7c662012-04-10 05:15:42 +00002493 int tmp_ifindex;
2494
Jiri Pirko85d59a82012-06-19 05:54:10 +00002495 opt_inst_info = &opt_inst->info;
2496 tmp_ifindex = opt_inst_info->port ?
2497 opt_inst_info->port->dev->ifindex : 0;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002498 if (option->type != opt_type ||
Jiri Pirko80f7c662012-04-10 05:15:42 +00002499 strcmp(option->name, opt_name) ||
Jiri Pirkob1303322012-06-19 05:54:08 +00002500 tmp_ifindex != opt_port_ifindex ||
2501 (option->array_size && !opt_is_array) ||
Jiri Pirko85d59a82012-06-19 05:54:10 +00002502 opt_inst_info->array_index != opt_array_index)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002503 continue;
2504 opt_found = true;
Jiri Pirko85d59a82012-06-19 05:54:10 +00002505 ctx.info = opt_inst_info;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002506 switch (opt_type) {
2507 case TEAM_OPTION_TYPE_U32:
Jiri Pirko14f066b2012-04-10 05:15:43 +00002508 ctx.data.u32_val = nla_get_u32(attr_data);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002509 break;
2510 case TEAM_OPTION_TYPE_STRING:
Jiri Pirko14f066b2012-04-10 05:15:43 +00002511 if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) {
Jiri Pirko26155982012-04-04 12:16:26 +00002512 err = -EINVAL;
2513 goto team_put;
2514 }
Jiri Pirko14f066b2012-04-10 05:15:43 +00002515 ctx.data.str_val = nla_data(attr_data);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002516 break;
Jiri Pirko26155982012-04-04 12:16:26 +00002517 case TEAM_OPTION_TYPE_BINARY:
Jiri Pirko14f066b2012-04-10 05:15:43 +00002518 ctx.data.bin_val.len = nla_len(attr_data);
2519 ctx.data.bin_val.ptr = nla_data(attr_data);
2520 break;
2521 case TEAM_OPTION_TYPE_BOOL:
2522 ctx.data.bool_val = attr_data ? true : false;
Jiri Pirko26155982012-04-04 12:16:26 +00002523 break;
Jiri Pirko69821632012-07-27 06:28:53 +00002524 case TEAM_OPTION_TYPE_S32:
2525 ctx.data.s32_val = nla_get_s32(attr_data);
2526 break;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002527 default:
2528 BUG();
2529 }
Jiri Pirko80f7c662012-04-10 05:15:42 +00002530 err = team_option_set(team, opt_inst, &ctx);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002531 if (err)
2532 goto team_put;
Jiri Pirko2fcdb2c2012-06-19 05:54:20 +00002533 opt_inst->changed = true;
2534 list_add(&opt_inst->tmp_list, &opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002535 }
2536 if (!opt_found) {
2537 err = -ENOENT;
2538 goto team_put;
2539 }
2540 }
2541
Jiri Pirko2fcdb2c2012-06-19 05:54:20 +00002542 err = team_nl_send_event_options_get(team, &opt_inst_list);
2543
Jiri Pirko3d249d42011-11-11 22:16:48 +00002544team_put:
2545 team_nl_team_put(team);
2546
2547 return err;
2548}
2549
Jiri Pirkod90f8892013-02-01 08:17:24 +00002550static int team_nl_fill_one_port_get(struct sk_buff *skb,
2551 struct team_port *port)
2552{
2553 struct nlattr *port_item;
2554
2555 port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
2556 if (!port_item)
2557 goto nest_cancel;
2558 if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex))
2559 goto nest_cancel;
2560 if (port->changed) {
2561 if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED))
2562 goto nest_cancel;
2563 port->changed = false;
2564 }
2565 if ((port->removed &&
2566 nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) ||
2567 (port->state.linkup &&
2568 nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) ||
2569 nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) ||
2570 nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex))
2571 goto nest_cancel;
2572 nla_nest_end(skb, port_item);
2573 return 0;
2574
2575nest_cancel:
2576 nla_nest_cancel(skb, port_item);
2577 return -EMSGSIZE;
2578}
2579
2580static int team_nl_send_port_list_get(struct team *team, u32 portid, u32 seq,
2581 int flags, team_nl_send_func_t *send_func,
2582 struct team_port *one_port)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002583{
2584 struct nlattr *port_list;
Jiri Pirkod90f8892013-02-01 08:17:24 +00002585 struct nlmsghdr *nlh;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002586 void *hdr;
2587 struct team_port *port;
Jiri Pirkod90f8892013-02-01 08:17:24 +00002588 int err;
2589 struct sk_buff *skb = NULL;
2590 bool incomplete;
2591 int i;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002592
Jiri Pirko3f3e7ce2013-05-29 05:02:57 +00002593 port = list_first_entry_or_null(&team->port_list,
2594 struct team_port, list);
Jiri Pirkod90f8892013-02-01 08:17:24 +00002595
2596start_again:
2597 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2598 if (err)
2599 return err;
2600
2601 hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
Jiri Pirko3d249d42011-11-11 22:16:48 +00002602 TEAM_CMD_PORT_LIST_GET);
Wei Yongjuna326e6d2012-09-24 18:29:35 +00002603 if (!hdr)
2604 return -EMSGSIZE;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002605
David S. Miller86ebb022012-04-01 20:25:18 -04002606 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2607 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002608 port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
2609 if (!port_list)
Jiri Pirko3221c642012-06-19 05:54:13 +00002610 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002611
Jiri Pirkod90f8892013-02-01 08:17:24 +00002612 i = 0;
2613 incomplete = false;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002614
Jiri Pirkod90f8892013-02-01 08:17:24 +00002615 /* If one port is selected, called wants to send port list containing
2616 * only this port. Otherwise go through all listed ports and send all
2617 */
2618 if (one_port) {
2619 err = team_nl_fill_one_port_get(skb, one_port);
2620 if (err)
2621 goto errout;
Jiri Pirko3f3e7ce2013-05-29 05:02:57 +00002622 } else if (port) {
2623 list_for_each_entry_from(port, &team->port_list, list) {
Jiri Pirkod90f8892013-02-01 08:17:24 +00002624 err = team_nl_fill_one_port_get(skb, port);
2625 if (err) {
2626 if (err == -EMSGSIZE) {
2627 if (!i)
2628 goto errout;
2629 incomplete = true;
2630 break;
2631 }
2632 goto errout;
2633 }
2634 i++;
Jiri Pirkob82b9182012-01-24 05:16:00 +00002635 }
Jiri Pirko3d249d42011-11-11 22:16:48 +00002636 }
2637
2638 nla_nest_end(skb, port_list);
Jiri Pirkod90f8892013-02-01 08:17:24 +00002639 genlmsg_end(skb, hdr);
2640 if (incomplete)
2641 goto start_again;
2642
2643send_done:
2644 nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
2645 if (!nlh) {
2646 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2647 if (err)
2648 goto errout;
2649 goto send_done;
2650 }
2651
2652 return send_func(skb, team, portid);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002653
2654nla_put_failure:
Jiri Pirkod90f8892013-02-01 08:17:24 +00002655 err = -EMSGSIZE;
2656errout:
Jiri Pirko3d249d42011-11-11 22:16:48 +00002657 genlmsg_cancel(skb, hdr);
Jiri Pirkod90f8892013-02-01 08:17:24 +00002658 nlmsg_free(skb);
2659 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002660}
2661
2662static int team_nl_cmd_port_list_get(struct sk_buff *skb,
2663 struct genl_info *info)
2664{
2665 struct team *team;
2666 int err;
2667
2668 team = team_nl_team_get(info);
2669 if (!team)
2670 return -EINVAL;
2671
Jiri Pirkod90f8892013-02-01 08:17:24 +00002672 err = team_nl_send_port_list_get(team, info->snd_portid, info->snd_seq,
2673 NLM_F_ACK, team_nl_send_unicast, NULL);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002674
2675 team_nl_team_put(team);
2676
2677 return err;
2678}
2679
Johannes Berg4534de82013-11-14 17:14:46 +01002680static const struct genl_ops team_nl_ops[] = {
Jiri Pirko3d249d42011-11-11 22:16:48 +00002681 {
2682 .cmd = TEAM_CMD_NOOP,
2683 .doit = team_nl_cmd_noop,
2684 .policy = team_nl_policy,
2685 },
2686 {
2687 .cmd = TEAM_CMD_OPTIONS_SET,
2688 .doit = team_nl_cmd_options_set,
2689 .policy = team_nl_policy,
2690 .flags = GENL_ADMIN_PERM,
2691 },
2692 {
2693 .cmd = TEAM_CMD_OPTIONS_GET,
2694 .doit = team_nl_cmd_options_get,
2695 .policy = team_nl_policy,
2696 .flags = GENL_ADMIN_PERM,
2697 },
2698 {
2699 .cmd = TEAM_CMD_PORT_LIST_GET,
2700 .doit = team_nl_cmd_port_list_get,
2701 .policy = team_nl_policy,
2702 .flags = GENL_ADMIN_PERM,
2703 },
2704};
2705
Johannes Berg2a94fe42013-11-19 15:19:39 +01002706static const struct genl_multicast_group team_nl_mcgrps[] = {
2707 { .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME, },
Jiri Pirko3d249d42011-11-11 22:16:48 +00002708};
2709
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002710static int team_nl_send_multicast(struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002711 struct team *team, u32 portid)
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002712{
Johannes Berg68eb5502013-11-19 15:19:38 +01002713 return genlmsg_multicast_netns(&team_nl_family, dev_net(team->dev),
Johannes Berg2a94fe42013-11-19 15:19:39 +01002714 skb, 0, 0, GFP_KERNEL);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002715}
2716
Jiri Pirko01048d92012-06-19 05:54:14 +00002717static int team_nl_send_event_options_get(struct team *team,
2718 struct list_head *sel_opt_inst_list)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002719{
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002720 return team_nl_send_options_get(team, 0, 0, 0, team_nl_send_multicast,
2721 sel_opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002722}
2723
Jiri Pirkod90f8892013-02-01 08:17:24 +00002724static int team_nl_send_event_port_get(struct team *team,
2725 struct team_port *port)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002726{
Jiri Pirkod90f8892013-02-01 08:17:24 +00002727 return team_nl_send_port_list_get(team, 0, 0, 0, team_nl_send_multicast,
2728 port);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002729}
2730
2731static int team_nl_init(void)
2732{
Johannes Berg2a94fe42013-11-19 15:19:39 +01002733 return genl_register_family_with_ops_groups(&team_nl_family, team_nl_ops,
2734 team_nl_mcgrps);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002735}
2736
2737static void team_nl_fini(void)
2738{
2739 genl_unregister_family(&team_nl_family);
2740}
2741
2742
2743/******************
2744 * Change checkers
2745 ******************/
2746
Jiri Pirkob82b9182012-01-24 05:16:00 +00002747static void __team_options_change_check(struct team *team)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002748{
2749 int err;
Jiri Pirko01048d92012-06-19 05:54:14 +00002750 struct team_option_inst *opt_inst;
2751 LIST_HEAD(sel_opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002752
Jiri Pirko01048d92012-06-19 05:54:14 +00002753 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2754 if (opt_inst->changed)
2755 list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
2756 }
2757 err = team_nl_send_event_options_get(team, &sel_opt_inst_list);
Jiri Pirko0c7517e2012-08-23 03:26:51 +00002758 if (err && err != -ESRCH)
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002759 netdev_warn(team->dev, "Failed to send options change via netlink (err %d)\n",
2760 err);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002761}
2762
2763/* rtnl lock is held */
Jiri Pirko10f3f512012-09-21 01:51:59 +00002764
2765static void __team_port_change_send(struct team_port *port, bool linkup)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002766{
2767 int err;
2768
Jiri Pirkob82b9182012-01-24 05:16:00 +00002769 port->changed = true;
Jiri Pirko71472ec2012-04-10 05:15:44 +00002770 port->state.linkup = linkup;
2771 team_refresh_port_linkup(port);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002772 if (linkup) {
2773 struct ethtool_cmd ecmd;
2774
2775 err = __ethtool_get_settings(port->dev, &ecmd);
2776 if (!err) {
Jiri Pirko71472ec2012-04-10 05:15:44 +00002777 port->state.speed = ethtool_cmd_speed(&ecmd);
2778 port->state.duplex = ecmd.duplex;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002779 goto send_event;
2780 }
2781 }
Jiri Pirko71472ec2012-04-10 05:15:44 +00002782 port->state.speed = 0;
2783 port->state.duplex = 0;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002784
2785send_event:
Jiri Pirkod90f8892013-02-01 08:17:24 +00002786 err = team_nl_send_event_port_get(port->team, port);
Jiri Pirko0c7517e2012-08-23 03:26:51 +00002787 if (err && err != -ESRCH)
2788 netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink (err %d)\n",
2789 port->dev->name, err);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002790
2791}
2792
Flavio Leitner06a7fc42012-12-30 08:27:29 +00002793static void __team_carrier_check(struct team *team)
2794{
2795 struct team_port *port;
2796 bool team_linkup;
2797
Flavio Leitnere1854832013-02-05 09:30:55 +00002798 if (team->user_carrier_enabled)
2799 return;
2800
Flavio Leitner06a7fc42012-12-30 08:27:29 +00002801 team_linkup = false;
2802 list_for_each_entry(port, &team->port_list, list) {
2803 if (port->linkup) {
2804 team_linkup = true;
2805 break;
2806 }
2807 }
2808
2809 if (team_linkup)
2810 netif_carrier_on(team->dev);
2811 else
2812 netif_carrier_off(team->dev);
2813}
2814
Jiri Pirko10f3f512012-09-21 01:51:59 +00002815static void __team_port_change_check(struct team_port *port, bool linkup)
2816{
2817 if (port->state.linkup != linkup)
2818 __team_port_change_send(port, linkup);
Flavio Leitner06a7fc42012-12-30 08:27:29 +00002819 __team_carrier_check(port->team);
Jiri Pirko10f3f512012-09-21 01:51:59 +00002820}
2821
2822static void __team_port_change_port_added(struct team_port *port, bool linkup)
2823{
2824 __team_port_change_send(port, linkup);
Flavio Leitner06a7fc42012-12-30 08:27:29 +00002825 __team_carrier_check(port->team);
Jiri Pirko10f3f512012-09-21 01:51:59 +00002826}
2827
2828static void __team_port_change_port_removed(struct team_port *port)
2829{
2830 port->removed = true;
2831 __team_port_change_send(port, false);
Flavio Leitner06a7fc42012-12-30 08:27:29 +00002832 __team_carrier_check(port->team);
Jiri Pirko10f3f512012-09-21 01:51:59 +00002833}
2834
Jiri Pirko3d249d42011-11-11 22:16:48 +00002835static void team_port_change_check(struct team_port *port, bool linkup)
2836{
2837 struct team *team = port->team;
2838
Jiri Pirko61dc3462011-11-16 11:09:08 +00002839 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002840 __team_port_change_check(port, linkup);
Jiri Pirko61dc3462011-11-16 11:09:08 +00002841 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002842}
2843
Jiri Pirko0f1aad22012-06-19 05:54:11 +00002844
Jiri Pirko3d249d42011-11-11 22:16:48 +00002845/************************************
2846 * Net device notifier event handler
2847 ************************************/
2848
2849static int team_device_event(struct notifier_block *unused,
2850 unsigned long event, void *ptr)
2851{
Jiri Pirko351638e2013-05-28 01:30:21 +00002852 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002853 struct team_port *port;
2854
2855 port = team_port_get_rtnl(dev);
2856 if (!port)
2857 return NOTIFY_DONE;
2858
2859 switch (event) {
2860 case NETDEV_UP:
2861 if (netif_carrier_ok(dev))
2862 team_port_change_check(port, true);
Jiri Pirkoed2da032014-04-23 14:17:55 +02002863 break;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002864 case NETDEV_DOWN:
2865 team_port_change_check(port, false);
Jiri Pirkoed2da032014-04-23 14:17:55 +02002866 break;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002867 case NETDEV_CHANGE:
2868 if (netif_running(port->dev))
2869 team_port_change_check(port,
2870 !!netif_carrier_ok(port->dev));
2871 break;
2872 case NETDEV_UNREGISTER:
2873 team_del_slave(port->team->dev, dev);
2874 break;
2875 case NETDEV_FEAT_CHANGE:
2876 team_compute_features(port->team);
2877 break;
Veaceslav Falicob01f2362014-01-16 00:02:19 +01002878 case NETDEV_PRECHANGEMTU:
Jiri Pirko3d249d42011-11-11 22:16:48 +00002879 /* Forbid to change mtu of underlaying device */
Jiri Pirko9d0d68f2014-05-29 20:46:17 +02002880 if (!port->team->port_mtu_change_allowed)
2881 return NOTIFY_BAD;
2882 break;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002883 case NETDEV_PRE_TYPE_CHANGE:
2884 /* Forbid to change type of underlaying device */
2885 return NOTIFY_BAD;
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02002886 case NETDEV_RESEND_IGMP:
2887 /* Propagate to master device */
2888 call_netdevice_notifiers(event, port->team->dev);
2889 break;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002890 }
2891 return NOTIFY_DONE;
2892}
2893
2894static struct notifier_block team_notifier_block __read_mostly = {
2895 .notifier_call = team_device_event,
2896};
2897
2898
2899/***********************
2900 * Module init and exit
2901 ***********************/
2902
2903static int __init team_module_init(void)
2904{
2905 int err;
2906
2907 register_netdevice_notifier(&team_notifier_block);
2908
2909 err = rtnl_link_register(&team_link_ops);
2910 if (err)
2911 goto err_rtnl_reg;
2912
2913 err = team_nl_init();
2914 if (err)
2915 goto err_nl_init;
2916
2917 return 0;
2918
2919err_nl_init:
2920 rtnl_link_unregister(&team_link_ops);
2921
2922err_rtnl_reg:
2923 unregister_netdevice_notifier(&team_notifier_block);
2924
2925 return err;
2926}
2927
2928static void __exit team_module_exit(void)
2929{
2930 team_nl_fini();
2931 rtnl_link_unregister(&team_link_ops);
2932 unregister_netdevice_notifier(&team_notifier_block);
2933}
2934
2935module_init(team_module_init);
2936module_exit(team_module_exit);
2937
2938MODULE_LICENSE("GPL v2");
2939MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
2940MODULE_DESCRIPTION("Ethernet team device driver");
2941MODULE_ALIAS_RTNL_LINK(DRV_NAME);