blob: f50b8ca8dc949d63a53cabf6b872c3713314bff1 [file] [log] [blame]
Jiri Pirko3d249d42011-11-11 22:16:48 +00001/*
2 * net/drivers/team/team.c - Network team device driver
3 * Copyright (c) 2011 Jiri Pirko <jpirko@redhat.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 */
10
11#include <linux/kernel.h>
12#include <linux/types.h>
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/slab.h>
16#include <linux/rcupdate.h>
17#include <linux/errno.h>
18#include <linux/ctype.h>
19#include <linux/notifier.h>
20#include <linux/netdevice.h>
Jiri Pirko87002b02011-12-08 04:11:17 +000021#include <linux/if_vlan.h>
Jiri Pirko3d249d42011-11-11 22:16:48 +000022#include <linux/if_arp.h>
23#include <linux/socket.h>
24#include <linux/etherdevice.h>
25#include <linux/rtnetlink.h>
26#include <net/rtnetlink.h>
27#include <net/genetlink.h>
28#include <net/netlink.h>
29#include <linux/if_team.h>
30
31#define DRV_NAME "team"
32
33
34/**********
35 * Helpers
36 **********/
37
38#define team_port_exists(dev) (dev->priv_flags & IFF_TEAM_PORT)
39
40static struct team_port *team_port_get_rcu(const struct net_device *dev)
41{
42 struct team_port *port = rcu_dereference(dev->rx_handler_data);
43
44 return team_port_exists(dev) ? port : NULL;
45}
46
47static struct team_port *team_port_get_rtnl(const struct net_device *dev)
48{
49 struct team_port *port = rtnl_dereference(dev->rx_handler_data);
50
51 return team_port_exists(dev) ? port : NULL;
52}
53
54/*
55 * Since the ability to change mac address for open port device is tested in
56 * team_port_add, this function can be called without control of return value
57 */
58static int __set_port_mac(struct net_device *port_dev,
59 const unsigned char *dev_addr)
60{
61 struct sockaddr addr;
62
63 memcpy(addr.sa_data, dev_addr, ETH_ALEN);
64 addr.sa_family = ARPHRD_ETHER;
65 return dev_set_mac_address(port_dev, &addr);
66}
67
Jiri Pirkocade4552012-04-10 05:15:46 +000068static int team_port_set_orig_mac(struct team_port *port)
Jiri Pirko3d249d42011-11-11 22:16:48 +000069{
70 return __set_port_mac(port->dev, port->orig.dev_addr);
71}
72
73int team_port_set_team_mac(struct team_port *port)
74{
75 return __set_port_mac(port->dev, port->team->dev->dev_addr);
76}
77EXPORT_SYMBOL(team_port_set_team_mac);
78
Jiri Pirko71472ec2012-04-10 05:15:44 +000079static void team_refresh_port_linkup(struct team_port *port)
80{
81 port->linkup = port->user.linkup_enabled ? port->user.linkup :
82 port->state.linkup;
83}
Jiri Pirko3d249d42011-11-11 22:16:48 +000084
85/*******************
86 * Options handling
87 *******************/
88
Jiri Pirko80f7c662012-04-10 05:15:42 +000089struct team_option_inst { /* One for each option instance */
90 struct list_head list;
91 struct team_option *option;
92 struct team_port *port; /* != NULL if per-port */
93 bool changed;
94 bool removed;
95};
96
97static struct team_option *__team_find_option(struct team *team,
98 const char *opt_name)
Jiri Pirko358b8382011-11-16 11:09:09 +000099{
100 struct team_option *option;
101
102 list_for_each_entry(option, &team->option_list, list) {
103 if (strcmp(option->name, opt_name) == 0)
104 return option;
105 }
106 return NULL;
107}
108
Jiri Pirko80f7c662012-04-10 05:15:42 +0000109static int __team_option_inst_add(struct team *team, struct team_option *option,
110 struct team_port *port)
111{
112 struct team_option_inst *opt_inst;
113
114 opt_inst = kmalloc(sizeof(*opt_inst), GFP_KERNEL);
115 if (!opt_inst)
116 return -ENOMEM;
117 opt_inst->option = option;
118 opt_inst->port = port;
119 opt_inst->changed = true;
120 opt_inst->removed = false;
121 list_add_tail(&opt_inst->list, &team->option_inst_list);
122 return 0;
123}
124
125static void __team_option_inst_del(struct team_option_inst *opt_inst)
126{
127 list_del(&opt_inst->list);
128 kfree(opt_inst);
129}
130
131static void __team_option_inst_del_option(struct team *team,
132 struct team_option *option)
133{
134 struct team_option_inst *opt_inst, *tmp;
135
136 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
137 if (opt_inst->option == option)
138 __team_option_inst_del(opt_inst);
139 }
140}
141
142static int __team_option_inst_add_option(struct team *team,
143 struct team_option *option)
144{
145 struct team_port *port;
146 int err;
147
148 if (!option->per_port)
149 return __team_option_inst_add(team, option, 0);
150
151 list_for_each_entry(port, &team->port_list, list) {
152 err = __team_option_inst_add(team, option, port);
153 if (err)
154 goto inst_del_option;
155 }
156 return 0;
157
158inst_del_option:
159 __team_option_inst_del_option(team, option);
160 return err;
161}
162
163static void __team_option_inst_mark_removed_option(struct team *team,
164 struct team_option *option)
165{
166 struct team_option_inst *opt_inst;
167
168 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
169 if (opt_inst->option == option) {
170 opt_inst->changed = true;
171 opt_inst->removed = true;
172 }
173 }
174}
175
176static void __team_option_inst_del_port(struct team *team,
177 struct team_port *port)
178{
179 struct team_option_inst *opt_inst, *tmp;
180
181 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
182 if (opt_inst->option->per_port &&
183 opt_inst->port == port)
184 __team_option_inst_del(opt_inst);
185 }
186}
187
188static int __team_option_inst_add_port(struct team *team,
189 struct team_port *port)
190{
191 struct team_option *option;
192 int err;
193
194 list_for_each_entry(option, &team->option_list, list) {
195 if (!option->per_port)
196 continue;
197 err = __team_option_inst_add(team, option, port);
198 if (err)
199 goto inst_del_port;
200 }
201 return 0;
202
203inst_del_port:
204 __team_option_inst_del_port(team, port);
205 return err;
206}
207
208static void __team_option_inst_mark_removed_port(struct team *team,
209 struct team_port *port)
210{
211 struct team_option_inst *opt_inst;
212
213 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
214 if (opt_inst->port == port) {
215 opt_inst->changed = true;
216 opt_inst->removed = true;
217 }
218 }
219}
220
221static int __team_options_register(struct team *team,
222 const struct team_option *option,
223 size_t option_count)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000224{
225 int i;
Jiri Pirko2bba19f2011-11-17 04:16:05 +0000226 struct team_option **dst_opts;
Jiri Pirko358b8382011-11-16 11:09:09 +0000227 int err;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000228
Jiri Pirko2bba19f2011-11-17 04:16:05 +0000229 dst_opts = kzalloc(sizeof(struct team_option *) * option_count,
230 GFP_KERNEL);
231 if (!dst_opts)
232 return -ENOMEM;
Jiri Pirko358b8382011-11-16 11:09:09 +0000233 for (i = 0; i < option_count; i++, option++) {
Jiri Pirko358b8382011-11-16 11:09:09 +0000234 if (__team_find_option(team, option->name)) {
235 err = -EEXIST;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000236 goto alloc_rollback;
Jiri Pirko358b8382011-11-16 11:09:09 +0000237 }
Jiri Pirkof8a15af2011-11-17 06:32:37 +0000238 dst_opts[i] = kmemdup(option, sizeof(*option), GFP_KERNEL);
239 if (!dst_opts[i]) {
Jiri Pirko358b8382011-11-16 11:09:09 +0000240 err = -ENOMEM;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000241 goto alloc_rollback;
Jiri Pirko358b8382011-11-16 11:09:09 +0000242 }
Jiri Pirko358b8382011-11-16 11:09:09 +0000243 }
244
Jiri Pirkob82b9182012-01-24 05:16:00 +0000245 for (i = 0; i < option_count; i++) {
Jiri Pirko80f7c662012-04-10 05:15:42 +0000246 err = __team_option_inst_add_option(team, dst_opts[i]);
247 if (err)
248 goto inst_rollback;
Jiri Pirko358b8382011-11-16 11:09:09 +0000249 list_add_tail(&dst_opts[i]->list, &team->option_list);
Jiri Pirkob82b9182012-01-24 05:16:00 +0000250 }
Jiri Pirko358b8382011-11-16 11:09:09 +0000251
Jiri Pirko2bba19f2011-11-17 04:16:05 +0000252 kfree(dst_opts);
Jiri Pirko358b8382011-11-16 11:09:09 +0000253 return 0;
254
Jiri Pirko80f7c662012-04-10 05:15:42 +0000255inst_rollback:
256 for (i--; i >= 0; i--)
257 __team_option_inst_del_option(team, dst_opts[i]);
258
259 i = option_count - 1;
260alloc_rollback:
261 for (i--; i >= 0; i--)
Jiri Pirko358b8382011-11-16 11:09:09 +0000262 kfree(dst_opts[i]);
263
Jiri Pirko2bba19f2011-11-17 04:16:05 +0000264 kfree(dst_opts);
Jiri Pirko358b8382011-11-16 11:09:09 +0000265 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000266}
Jiri Pirko358b8382011-11-16 11:09:09 +0000267
Jiri Pirkob82b9182012-01-24 05:16:00 +0000268static void __team_options_mark_removed(struct team *team,
269 const struct team_option *option,
270 size_t option_count)
271{
272 int i;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000273
Jiri Pirkob82b9182012-01-24 05:16:00 +0000274 for (i = 0; i < option_count; i++, option++) {
275 struct team_option *del_opt;
276
277 del_opt = __team_find_option(team, option->name);
Jiri Pirko80f7c662012-04-10 05:15:42 +0000278 if (del_opt)
279 __team_option_inst_mark_removed_option(team, del_opt);
Jiri Pirkob82b9182012-01-24 05:16:00 +0000280 }
281}
Jiri Pirko3d249d42011-11-11 22:16:48 +0000282
283static void __team_options_unregister(struct team *team,
Jiri Pirko358b8382011-11-16 11:09:09 +0000284 const struct team_option *option,
Jiri Pirko3d249d42011-11-11 22:16:48 +0000285 size_t option_count)
286{
287 int i;
288
Jiri Pirko358b8382011-11-16 11:09:09 +0000289 for (i = 0; i < option_count; i++, option++) {
290 struct team_option *del_opt;
291
292 del_opt = __team_find_option(team, option->name);
293 if (del_opt) {
Jiri Pirko80f7c662012-04-10 05:15:42 +0000294 __team_option_inst_del_option(team, del_opt);
Jiri Pirko358b8382011-11-16 11:09:09 +0000295 list_del(&del_opt->list);
296 kfree(del_opt);
297 }
298 }
Jiri Pirko3d249d42011-11-11 22:16:48 +0000299}
300
Jiri Pirkob82b9182012-01-24 05:16:00 +0000301static void __team_options_change_check(struct team *team);
302
303int team_options_register(struct team *team,
304 const struct team_option *option,
305 size_t option_count)
306{
307 int err;
308
309 err = __team_options_register(team, option, option_count);
310 if (err)
311 return err;
312 __team_options_change_check(team);
313 return 0;
314}
315EXPORT_SYMBOL(team_options_register);
316
Jiri Pirko358b8382011-11-16 11:09:09 +0000317void team_options_unregister(struct team *team,
318 const struct team_option *option,
Jiri Pirko3d249d42011-11-11 22:16:48 +0000319 size_t option_count)
320{
Jiri Pirkob82b9182012-01-24 05:16:00 +0000321 __team_options_mark_removed(team, option, option_count);
322 __team_options_change_check(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000323 __team_options_unregister(team, option, option_count);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000324}
325EXPORT_SYMBOL(team_options_unregister);
326
Jiri Pirko80f7c662012-04-10 05:15:42 +0000327static int team_option_port_add(struct team *team, struct team_port *port)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000328{
329 int err;
330
Jiri Pirko80f7c662012-04-10 05:15:42 +0000331 err = __team_option_inst_add_port(team, port);
332 if (err)
333 return err;
334 __team_options_change_check(team);
335 return 0;
336}
337
338static void team_option_port_del(struct team *team, struct team_port *port)
339{
340 __team_option_inst_mark_removed_port(team, port);
341 __team_options_change_check(team);
342 __team_option_inst_del_port(team, port);
343}
344
345static int team_option_get(struct team *team,
346 struct team_option_inst *opt_inst,
347 struct team_gsetter_ctx *ctx)
348{
Jiri Pirkof82b9592012-06-19 05:54:07 +0000349 if (!opt_inst->option->getter)
350 return -EOPNOTSUPP;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000351 return opt_inst->option->getter(team, ctx);
352}
353
354static int team_option_set(struct team *team,
355 struct team_option_inst *opt_inst,
356 struct team_gsetter_ctx *ctx)
357{
358 int err;
359
Jiri Pirkof82b9592012-06-19 05:54:07 +0000360 if (!opt_inst->option->setter)
361 return -EOPNOTSUPP;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000362 err = opt_inst->option->setter(team, ctx);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000363 if (err)
364 return err;
365
Jiri Pirko80f7c662012-04-10 05:15:42 +0000366 opt_inst->changed = true;
Jiri Pirkob82b9182012-01-24 05:16:00 +0000367 __team_options_change_check(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000368 return err;
369}
370
371/****************
372 * Mode handling
373 ****************/
374
375static LIST_HEAD(mode_list);
376static DEFINE_SPINLOCK(mode_list_lock);
377
Jiri Pirko04027882012-06-19 05:54:03 +0000378struct team_mode_item {
379 struct list_head list;
380 const struct team_mode *mode;
381};
Jiri Pirko3d249d42011-11-11 22:16:48 +0000382
Jiri Pirko04027882012-06-19 05:54:03 +0000383static struct team_mode_item *__find_mode(const char *kind)
384{
385 struct team_mode_item *mitem;
386
387 list_for_each_entry(mitem, &mode_list, list) {
388 if (strcmp(mitem->mode->kind, kind) == 0)
389 return mitem;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000390 }
391 return NULL;
392}
393
394static bool is_good_mode_name(const char *name)
395{
396 while (*name != '\0') {
397 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
398 return false;
399 name++;
400 }
401 return true;
402}
403
Jiri Pirko04027882012-06-19 05:54:03 +0000404int team_mode_register(const struct team_mode *mode)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000405{
406 int err = 0;
Jiri Pirko04027882012-06-19 05:54:03 +0000407 struct team_mode_item *mitem;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000408
409 if (!is_good_mode_name(mode->kind) ||
410 mode->priv_size > TEAM_MODE_PRIV_SIZE)
411 return -EINVAL;
Jiri Pirko04027882012-06-19 05:54:03 +0000412
413 mitem = kmalloc(sizeof(*mitem), GFP_KERNEL);
414 if (!mitem)
415 return -ENOMEM;
416
Jiri Pirko3d249d42011-11-11 22:16:48 +0000417 spin_lock(&mode_list_lock);
418 if (__find_mode(mode->kind)) {
419 err = -EEXIST;
Jiri Pirko04027882012-06-19 05:54:03 +0000420 kfree(mitem);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000421 goto unlock;
422 }
Jiri Pirko04027882012-06-19 05:54:03 +0000423 mitem->mode = mode;
424 list_add_tail(&mitem->list, &mode_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000425unlock:
426 spin_unlock(&mode_list_lock);
427 return err;
428}
429EXPORT_SYMBOL(team_mode_register);
430
Jiri Pirko04027882012-06-19 05:54:03 +0000431void team_mode_unregister(const struct team_mode *mode)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000432{
Jiri Pirko04027882012-06-19 05:54:03 +0000433 struct team_mode_item *mitem;
434
Jiri Pirko3d249d42011-11-11 22:16:48 +0000435 spin_lock(&mode_list_lock);
Jiri Pirko04027882012-06-19 05:54:03 +0000436 mitem = __find_mode(mode->kind);
437 if (mitem) {
438 list_del_init(&mitem->list);
439 kfree(mitem);
440 }
Jiri Pirko3d249d42011-11-11 22:16:48 +0000441 spin_unlock(&mode_list_lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000442}
443EXPORT_SYMBOL(team_mode_unregister);
444
Jiri Pirko04027882012-06-19 05:54:03 +0000445static const struct team_mode *team_mode_get(const char *kind)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000446{
Jiri Pirko04027882012-06-19 05:54:03 +0000447 struct team_mode_item *mitem;
448 const struct team_mode *mode = NULL;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000449
450 spin_lock(&mode_list_lock);
Jiri Pirko04027882012-06-19 05:54:03 +0000451 mitem = __find_mode(kind);
452 if (!mitem) {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000453 spin_unlock(&mode_list_lock);
454 request_module("team-mode-%s", kind);
455 spin_lock(&mode_list_lock);
Jiri Pirko04027882012-06-19 05:54:03 +0000456 mitem = __find_mode(kind);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000457 }
Jiri Pirko04027882012-06-19 05:54:03 +0000458 if (mitem) {
459 mode = mitem->mode;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000460 if (!try_module_get(mode->owner))
461 mode = NULL;
Jiri Pirko04027882012-06-19 05:54:03 +0000462 }
Jiri Pirko3d249d42011-11-11 22:16:48 +0000463
464 spin_unlock(&mode_list_lock);
465 return mode;
466}
467
468static void team_mode_put(const struct team_mode *mode)
469{
470 module_put(mode->owner);
471}
472
473static bool team_dummy_transmit(struct team *team, struct sk_buff *skb)
474{
475 dev_kfree_skb_any(skb);
476 return false;
477}
478
479rx_handler_result_t team_dummy_receive(struct team *team,
480 struct team_port *port,
481 struct sk_buff *skb)
482{
483 return RX_HANDLER_ANOTHER;
484}
485
Jiri Pirkod299cd52012-06-19 05:54:04 +0000486static const struct team_mode __team_no_mode = {
487 .kind = "*NOMODE*",
488};
489
490static bool team_is_mode_set(struct team *team)
491{
492 return team->mode != &__team_no_mode;
493}
494
495static void team_set_no_mode(struct team *team)
496{
497 team->mode = &__team_no_mode;
498}
499
Jiri Pirko3d249d42011-11-11 22:16:48 +0000500static void team_adjust_ops(struct team *team)
501{
502 /*
503 * To avoid checks in rx/tx skb paths, ensure here that non-null and
504 * correct ops are always set.
505 */
506
507 if (list_empty(&team->port_list) ||
Jiri Pirkod299cd52012-06-19 05:54:04 +0000508 !team_is_mode_set(team) || !team->mode->ops->transmit)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000509 team->ops.transmit = team_dummy_transmit;
510 else
511 team->ops.transmit = team->mode->ops->transmit;
512
513 if (list_empty(&team->port_list) ||
Jiri Pirkod299cd52012-06-19 05:54:04 +0000514 !team_is_mode_set(team) || !team->mode->ops->receive)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000515 team->ops.receive = team_dummy_receive;
516 else
517 team->ops.receive = team->mode->ops->receive;
518}
519
520/*
521 * We can benefit from the fact that it's ensured no port is present
522 * at the time of mode change. Therefore no packets are in fly so there's no
523 * need to set mode operations in any special way.
524 */
525static int __team_change_mode(struct team *team,
526 const struct team_mode *new_mode)
527{
528 /* Check if mode was previously set and do cleanup if so */
Jiri Pirkod299cd52012-06-19 05:54:04 +0000529 if (team_is_mode_set(team)) {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000530 void (*exit_op)(struct team *team) = team->ops.exit;
531
532 /* Clear ops area so no callback is called any longer */
533 memset(&team->ops, 0, sizeof(struct team_mode_ops));
534 team_adjust_ops(team);
535
536 if (exit_op)
537 exit_op(team);
538 team_mode_put(team->mode);
Jiri Pirkod299cd52012-06-19 05:54:04 +0000539 team_set_no_mode(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000540 /* zero private data area */
541 memset(&team->mode_priv, 0,
542 sizeof(struct team) - offsetof(struct team, mode_priv));
543 }
544
545 if (!new_mode)
546 return 0;
547
548 if (new_mode->ops->init) {
549 int err;
550
551 err = new_mode->ops->init(team);
552 if (err)
553 return err;
554 }
555
556 team->mode = new_mode;
557 memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops));
558 team_adjust_ops(team);
559
560 return 0;
561}
562
563static int team_change_mode(struct team *team, const char *kind)
564{
Jiri Pirko04027882012-06-19 05:54:03 +0000565 const struct team_mode *new_mode;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000566 struct net_device *dev = team->dev;
567 int err;
568
569 if (!list_empty(&team->port_list)) {
570 netdev_err(dev, "No ports can be present during mode change\n");
571 return -EBUSY;
572 }
573
Jiri Pirkod299cd52012-06-19 05:54:04 +0000574 if (team_is_mode_set(team) && strcmp(team->mode->kind, kind) == 0) {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000575 netdev_err(dev, "Unable to change to the same mode the team is in\n");
576 return -EINVAL;
577 }
578
579 new_mode = team_mode_get(kind);
580 if (!new_mode) {
581 netdev_err(dev, "Mode \"%s\" not found\n", kind);
582 return -EINVAL;
583 }
584
585 err = __team_change_mode(team, new_mode);
586 if (err) {
587 netdev_err(dev, "Failed to change to mode \"%s\"\n", kind);
588 team_mode_put(new_mode);
589 return err;
590 }
591
592 netdev_info(dev, "Mode changed to \"%s\"\n", kind);
593 return 0;
594}
595
596
597/************************
598 * Rx path frame handler
599 ************************/
600
Jiri Pirko19a0b582012-04-20 04:42:05 +0000601static bool team_port_enabled(struct team_port *port);
602
Jiri Pirko3d249d42011-11-11 22:16:48 +0000603/* note: already called with rcu_read_lock */
604static rx_handler_result_t team_handle_frame(struct sk_buff **pskb)
605{
606 struct sk_buff *skb = *pskb;
607 struct team_port *port;
608 struct team *team;
609 rx_handler_result_t res;
610
611 skb = skb_share_check(skb, GFP_ATOMIC);
612 if (!skb)
613 return RX_HANDLER_CONSUMED;
614
615 *pskb = skb;
616
617 port = team_port_get_rcu(skb->dev);
618 team = port->team;
Jiri Pirko19a0b582012-04-20 04:42:05 +0000619 if (!team_port_enabled(port)) {
620 /* allow exact match delivery for disabled ports */
621 res = RX_HANDLER_EXACT;
622 } else {
623 res = team->ops.receive(team, port, skb);
624 }
Jiri Pirko3d249d42011-11-11 22:16:48 +0000625 if (res == RX_HANDLER_ANOTHER) {
626 struct team_pcpu_stats *pcpu_stats;
627
628 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
629 u64_stats_update_begin(&pcpu_stats->syncp);
630 pcpu_stats->rx_packets++;
631 pcpu_stats->rx_bytes += skb->len;
632 if (skb->pkt_type == PACKET_MULTICAST)
633 pcpu_stats->rx_multicast++;
634 u64_stats_update_end(&pcpu_stats->syncp);
635
636 skb->dev = team->dev;
637 } else {
638 this_cpu_inc(team->pcpu_stats->rx_dropped);
639 }
640
641 return res;
642}
643
644
645/****************
646 * Port handling
647 ****************/
648
649static bool team_port_find(const struct team *team,
650 const struct team_port *port)
651{
652 struct team_port *cur;
653
654 list_for_each_entry(cur, &team->port_list, list)
655 if (cur == port)
656 return true;
657 return false;
658}
659
Jiri Pirko19a0b582012-04-20 04:42:05 +0000660static bool team_port_enabled(struct team_port *port)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000661{
Jiri Pirko19a0b582012-04-20 04:42:05 +0000662 return port->index != -1;
663}
664
665/*
666 * Enable/disable port by adding to enabled port hashlist and setting
667 * port->index (Might be racy so reader could see incorrect ifindex when
668 * processing a flying packet, but that is not a problem). Write guarded
669 * by team->lock.
670 */
671static void team_port_enable(struct team *team,
672 struct team_port *port)
673{
674 if (team_port_enabled(port))
675 return;
676 port->index = team->en_port_count++;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000677 hlist_add_head_rcu(&port->hlist,
678 team_port_index_hash(team, port->index));
Jiri Pirko3d249d42011-11-11 22:16:48 +0000679}
680
681static void __reconstruct_port_hlist(struct team *team, int rm_index)
682{
683 int i;
684 struct team_port *port;
685
Jiri Pirko19a0b582012-04-20 04:42:05 +0000686 for (i = rm_index + 1; i < team->en_port_count; i++) {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000687 port = team_get_port_by_index(team, i);
688 hlist_del_rcu(&port->hlist);
689 port->index--;
690 hlist_add_head_rcu(&port->hlist,
691 team_port_index_hash(team, port->index));
692 }
693}
694
Jiri Pirko19a0b582012-04-20 04:42:05 +0000695static void team_port_disable(struct team *team,
696 struct team_port *port)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000697{
698 int rm_index = port->index;
699
Jiri Pirko19a0b582012-04-20 04:42:05 +0000700 if (!team_port_enabled(port))
701 return;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000702 hlist_del_rcu(&port->hlist);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000703 __reconstruct_port_hlist(team, rm_index);
Jiri Pirko19a0b582012-04-20 04:42:05 +0000704 team->en_port_count--;
705 port->index = -1;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000706}
707
708#define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \
709 NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
710 NETIF_F_HIGHDMA | NETIF_F_LRO)
711
712static void __team_compute_features(struct team *team)
713{
714 struct team_port *port;
715 u32 vlan_features = TEAM_VLAN_FEATURES;
716 unsigned short max_hard_header_len = ETH_HLEN;
717
718 list_for_each_entry(port, &team->port_list, list) {
719 vlan_features = netdev_increment_features(vlan_features,
720 port->dev->vlan_features,
721 TEAM_VLAN_FEATURES);
722
723 if (port->dev->hard_header_len > max_hard_header_len)
724 max_hard_header_len = port->dev->hard_header_len;
725 }
726
727 team->dev->vlan_features = vlan_features;
728 team->dev->hard_header_len = max_hard_header_len;
729
730 netdev_change_features(team->dev);
731}
732
733static void team_compute_features(struct team *team)
734{
Jiri Pirko61dc3462011-11-16 11:09:08 +0000735 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000736 __team_compute_features(team);
Jiri Pirko61dc3462011-11-16 11:09:08 +0000737 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000738}
739
740static int team_port_enter(struct team *team, struct team_port *port)
741{
742 int err = 0;
743
744 dev_hold(team->dev);
745 port->dev->priv_flags |= IFF_TEAM_PORT;
746 if (team->ops.port_enter) {
747 err = team->ops.port_enter(team, port);
748 if (err) {
749 netdev_err(team->dev, "Device %s failed to enter team mode\n",
750 port->dev->name);
751 goto err_port_enter;
752 }
753 }
754
755 return 0;
756
757err_port_enter:
758 port->dev->priv_flags &= ~IFF_TEAM_PORT;
759 dev_put(team->dev);
760
761 return err;
762}
763
764static void team_port_leave(struct team *team, struct team_port *port)
765{
766 if (team->ops.port_leave)
767 team->ops.port_leave(team, port);
768 port->dev->priv_flags &= ~IFF_TEAM_PORT;
769 dev_put(team->dev);
770}
771
772static void __team_port_change_check(struct team_port *port, bool linkup);
773
774static int team_port_add(struct team *team, struct net_device *port_dev)
775{
776 struct net_device *dev = team->dev;
777 struct team_port *port;
778 char *portname = port_dev->name;
779 int err;
780
781 if (port_dev->flags & IFF_LOOPBACK ||
782 port_dev->type != ARPHRD_ETHER) {
783 netdev_err(dev, "Device %s is of an unsupported type\n",
784 portname);
785 return -EINVAL;
786 }
787
788 if (team_port_exists(port_dev)) {
789 netdev_err(dev, "Device %s is already a port "
790 "of a team device\n", portname);
791 return -EBUSY;
792 }
793
794 if (port_dev->flags & IFF_UP) {
795 netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
796 portname);
797 return -EBUSY;
798 }
799
Jiri Pirko5149ee52012-06-19 05:54:05 +0000800 port = kzalloc(sizeof(struct team_port) + team->mode->port_priv_size,
801 GFP_KERNEL);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000802 if (!port)
803 return -ENOMEM;
804
805 port->dev = port_dev;
806 port->team = team;
807
808 port->orig.mtu = port_dev->mtu;
809 err = dev_set_mtu(port_dev, dev->mtu);
810 if (err) {
811 netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
812 goto err_set_mtu;
813 }
814
815 memcpy(port->orig.dev_addr, port_dev->dev_addr, ETH_ALEN);
816
817 err = team_port_enter(team, port);
818 if (err) {
819 netdev_err(dev, "Device %s failed to enter team mode\n",
820 portname);
821 goto err_port_enter;
822 }
823
824 err = dev_open(port_dev);
825 if (err) {
826 netdev_dbg(dev, "Device %s opening failed\n",
827 portname);
828 goto err_dev_open;
829 }
830
Jiri Pirko57459182011-12-08 04:11:20 +0000831 err = vlan_vids_add_by_dev(port_dev, dev);
832 if (err) {
833 netdev_err(dev, "Failed to add vlan ids to device %s\n",
834 portname);
835 goto err_vids_add;
836 }
837
Jiri Pirko3d249d42011-11-11 22:16:48 +0000838 err = netdev_set_master(port_dev, dev);
839 if (err) {
840 netdev_err(dev, "Device %s failed to set master\n", portname);
841 goto err_set_master;
842 }
843
844 err = netdev_rx_handler_register(port_dev, team_handle_frame,
845 port);
846 if (err) {
847 netdev_err(dev, "Device %s failed to register rx_handler\n",
848 portname);
849 goto err_handler_register;
850 }
851
Jiri Pirko80f7c662012-04-10 05:15:42 +0000852 err = team_option_port_add(team, port);
853 if (err) {
854 netdev_err(dev, "Device %s failed to add per-port options\n",
855 portname);
856 goto err_option_port_add;
857 }
858
Jiri Pirko19a0b582012-04-20 04:42:05 +0000859 port->index = -1;
860 team_port_enable(team, port);
861 list_add_tail_rcu(&port->list, &team->port_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000862 team_adjust_ops(team);
863 __team_compute_features(team);
864 __team_port_change_check(port, !!netif_carrier_ok(port_dev));
865
866 netdev_info(dev, "Port device %s added\n", portname);
867
868 return 0;
869
Jiri Pirko80f7c662012-04-10 05:15:42 +0000870err_option_port_add:
871 netdev_rx_handler_unregister(port_dev);
872
Jiri Pirko3d249d42011-11-11 22:16:48 +0000873err_handler_register:
874 netdev_set_master(port_dev, NULL);
875
876err_set_master:
Jiri Pirko57459182011-12-08 04:11:20 +0000877 vlan_vids_del_by_dev(port_dev, dev);
878
879err_vids_add:
Jiri Pirko3d249d42011-11-11 22:16:48 +0000880 dev_close(port_dev);
881
882err_dev_open:
883 team_port_leave(team, port);
884 team_port_set_orig_mac(port);
885
886err_port_enter:
887 dev_set_mtu(port_dev, port->orig.mtu);
888
889err_set_mtu:
890 kfree(port);
891
892 return err;
893}
894
895static int team_port_del(struct team *team, struct net_device *port_dev)
896{
897 struct net_device *dev = team->dev;
898 struct team_port *port;
899 char *portname = port_dev->name;
900
901 port = team_port_get_rtnl(port_dev);
902 if (!port || !team_port_find(team, port)) {
903 netdev_err(dev, "Device %s does not act as a port of this team\n",
904 portname);
905 return -ENOENT;
906 }
907
Jiri Pirkob82b9182012-01-24 05:16:00 +0000908 port->removed = true;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000909 __team_port_change_check(port, false);
Jiri Pirko19a0b582012-04-20 04:42:05 +0000910 team_port_disable(team, port);
911 list_del_rcu(&port->list);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000912 team_adjust_ops(team);
Jiri Pirko80f7c662012-04-10 05:15:42 +0000913 team_option_port_del(team, port);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000914 netdev_rx_handler_unregister(port_dev);
915 netdev_set_master(port_dev, NULL);
Jiri Pirko57459182011-12-08 04:11:20 +0000916 vlan_vids_del_by_dev(port_dev, dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000917 dev_close(port_dev);
918 team_port_leave(team, port);
919 team_port_set_orig_mac(port);
920 dev_set_mtu(port_dev, port->orig.mtu);
921 synchronize_rcu();
922 kfree(port);
923 netdev_info(dev, "Port device %s removed\n", portname);
924 __team_compute_features(team);
925
926 return 0;
927}
928
929
930/*****************
931 * Net device ops
932 *****************/
933
Jiri Pirko80f7c662012-04-10 05:15:42 +0000934static int team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000935{
Jiri Pirkod299cd52012-06-19 05:54:04 +0000936 ctx->data.str_val = team->mode->kind;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000937 return 0;
938}
939
Jiri Pirko80f7c662012-04-10 05:15:42 +0000940static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000941{
Jiri Pirko80f7c662012-04-10 05:15:42 +0000942 return team_change_mode(team, ctx->data.str_val);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000943}
944
Jiri Pirkoacd69962012-04-20 04:42:06 +0000945static int team_port_en_option_get(struct team *team,
946 struct team_gsetter_ctx *ctx)
947{
948 ctx->data.bool_val = team_port_enabled(ctx->port);
949 return 0;
950}
951
952static int team_port_en_option_set(struct team *team,
953 struct team_gsetter_ctx *ctx)
954{
955 if (ctx->data.bool_val)
956 team_port_enable(team, ctx->port);
957 else
958 team_port_disable(team, ctx->port);
959 return 0;
960}
961
Jiri Pirko71472ec2012-04-10 05:15:44 +0000962static int team_user_linkup_option_get(struct team *team,
963 struct team_gsetter_ctx *ctx)
964{
965 ctx->data.bool_val = ctx->port->user.linkup;
966 return 0;
967}
968
969static int team_user_linkup_option_set(struct team *team,
970 struct team_gsetter_ctx *ctx)
971{
972 ctx->port->user.linkup = ctx->data.bool_val;
973 team_refresh_port_linkup(ctx->port);
974 return 0;
975}
976
977static int team_user_linkup_en_option_get(struct team *team,
978 struct team_gsetter_ctx *ctx)
979{
980 struct team_port *port = ctx->port;
981
982 ctx->data.bool_val = port->user.linkup_enabled;
983 return 0;
984}
985
986static int team_user_linkup_en_option_set(struct team *team,
987 struct team_gsetter_ctx *ctx)
988{
989 struct team_port *port = ctx->port;
990
991 port->user.linkup_enabled = ctx->data.bool_val;
992 team_refresh_port_linkup(ctx->port);
993 return 0;
994}
995
Jiri Pirko358b8382011-11-16 11:09:09 +0000996static const struct team_option team_options[] = {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000997 {
998 .name = "mode",
999 .type = TEAM_OPTION_TYPE_STRING,
1000 .getter = team_mode_option_get,
1001 .setter = team_mode_option_set,
1002 },
Jiri Pirko71472ec2012-04-10 05:15:44 +00001003 {
Jiri Pirkoacd69962012-04-20 04:42:06 +00001004 .name = "enabled",
1005 .type = TEAM_OPTION_TYPE_BOOL,
1006 .per_port = true,
1007 .getter = team_port_en_option_get,
1008 .setter = team_port_en_option_set,
1009 },
1010 {
Jiri Pirko71472ec2012-04-10 05:15:44 +00001011 .name = "user_linkup",
1012 .type = TEAM_OPTION_TYPE_BOOL,
1013 .per_port = true,
1014 .getter = team_user_linkup_option_get,
1015 .setter = team_user_linkup_option_set,
1016 },
1017 {
1018 .name = "user_linkup_enabled",
1019 .type = TEAM_OPTION_TYPE_BOOL,
1020 .per_port = true,
1021 .getter = team_user_linkup_en_option_get,
1022 .setter = team_user_linkup_en_option_set,
1023 },
Jiri Pirko3d249d42011-11-11 22:16:48 +00001024};
1025
1026static int team_init(struct net_device *dev)
1027{
1028 struct team *team = netdev_priv(dev);
1029 int i;
Jiri Pirko358b8382011-11-16 11:09:09 +00001030 int err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001031
1032 team->dev = dev;
Jiri Pirko61dc3462011-11-16 11:09:08 +00001033 mutex_init(&team->lock);
Jiri Pirkod299cd52012-06-19 05:54:04 +00001034 team_set_no_mode(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001035
1036 team->pcpu_stats = alloc_percpu(struct team_pcpu_stats);
1037 if (!team->pcpu_stats)
1038 return -ENOMEM;
1039
1040 for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
Jiri Pirko19a0b582012-04-20 04:42:05 +00001041 INIT_HLIST_HEAD(&team->en_port_hlist[i]);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001042 INIT_LIST_HEAD(&team->port_list);
1043
1044 team_adjust_ops(team);
1045
1046 INIT_LIST_HEAD(&team->option_list);
Jiri Pirko80f7c662012-04-10 05:15:42 +00001047 INIT_LIST_HEAD(&team->option_inst_list);
Jiri Pirko358b8382011-11-16 11:09:09 +00001048 err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
1049 if (err)
1050 goto err_options_register;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001051 netif_carrier_off(dev);
1052
1053 return 0;
Jiri Pirko358b8382011-11-16 11:09:09 +00001054
1055err_options_register:
1056 free_percpu(team->pcpu_stats);
1057
1058 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001059}
1060
1061static void team_uninit(struct net_device *dev)
1062{
1063 struct team *team = netdev_priv(dev);
1064 struct team_port *port;
1065 struct team_port *tmp;
1066
Jiri Pirko61dc3462011-11-16 11:09:08 +00001067 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001068 list_for_each_entry_safe(port, tmp, &team->port_list, list)
1069 team_port_del(team, port->dev);
1070
1071 __team_change_mode(team, NULL); /* cleanup */
1072 __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
Jiri Pirko61dc3462011-11-16 11:09:08 +00001073 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001074}
1075
1076static void team_destructor(struct net_device *dev)
1077{
1078 struct team *team = netdev_priv(dev);
1079
1080 free_percpu(team->pcpu_stats);
1081 free_netdev(dev);
1082}
1083
1084static int team_open(struct net_device *dev)
1085{
1086 netif_carrier_on(dev);
1087 return 0;
1088}
1089
1090static int team_close(struct net_device *dev)
1091{
1092 netif_carrier_off(dev);
1093 return 0;
1094}
1095
1096/*
1097 * note: already called with rcu_read_lock
1098 */
1099static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
1100{
1101 struct team *team = netdev_priv(dev);
1102 bool tx_success = false;
1103 unsigned int len = skb->len;
1104
1105 tx_success = team->ops.transmit(team, skb);
1106 if (tx_success) {
1107 struct team_pcpu_stats *pcpu_stats;
1108
1109 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
1110 u64_stats_update_begin(&pcpu_stats->syncp);
1111 pcpu_stats->tx_packets++;
1112 pcpu_stats->tx_bytes += len;
1113 u64_stats_update_end(&pcpu_stats->syncp);
1114 } else {
1115 this_cpu_inc(team->pcpu_stats->tx_dropped);
1116 }
1117
1118 return NETDEV_TX_OK;
1119}
1120
1121static void team_change_rx_flags(struct net_device *dev, int change)
1122{
1123 struct team *team = netdev_priv(dev);
1124 struct team_port *port;
1125 int inc;
1126
1127 rcu_read_lock();
1128 list_for_each_entry_rcu(port, &team->port_list, list) {
1129 if (change & IFF_PROMISC) {
1130 inc = dev->flags & IFF_PROMISC ? 1 : -1;
1131 dev_set_promiscuity(port->dev, inc);
1132 }
1133 if (change & IFF_ALLMULTI) {
1134 inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
1135 dev_set_allmulti(port->dev, inc);
1136 }
1137 }
1138 rcu_read_unlock();
1139}
1140
1141static void team_set_rx_mode(struct net_device *dev)
1142{
1143 struct team *team = netdev_priv(dev);
1144 struct team_port *port;
1145
1146 rcu_read_lock();
1147 list_for_each_entry_rcu(port, &team->port_list, list) {
1148 dev_uc_sync(port->dev, dev);
1149 dev_mc_sync(port->dev, dev);
1150 }
1151 rcu_read_unlock();
1152}
1153
1154static int team_set_mac_address(struct net_device *dev, void *p)
1155{
1156 struct team *team = netdev_priv(dev);
1157 struct team_port *port;
1158 struct sockaddr *addr = p;
1159
Danny Kukawka7ce5d222012-02-15 06:45:40 +00001160 dev->addr_assign_type &= ~NET_ADDR_RANDOM;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001161 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
1162 rcu_read_lock();
1163 list_for_each_entry_rcu(port, &team->port_list, list)
1164 if (team->ops.port_change_mac)
1165 team->ops.port_change_mac(team, port);
1166 rcu_read_unlock();
1167 return 0;
1168}
1169
1170static int team_change_mtu(struct net_device *dev, int new_mtu)
1171{
1172 struct team *team = netdev_priv(dev);
1173 struct team_port *port;
1174 int err;
1175
1176 /*
1177 * Alhough this is reader, it's guarded by team lock. It's not possible
1178 * to traverse list in reverse under rcu_read_lock
1179 */
Jiri Pirko61dc3462011-11-16 11:09:08 +00001180 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001181 list_for_each_entry(port, &team->port_list, list) {
1182 err = dev_set_mtu(port->dev, new_mtu);
1183 if (err) {
1184 netdev_err(dev, "Device %s failed to change mtu",
1185 port->dev->name);
1186 goto unwind;
1187 }
1188 }
Jiri Pirko61dc3462011-11-16 11:09:08 +00001189 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001190
1191 dev->mtu = new_mtu;
1192
1193 return 0;
1194
1195unwind:
1196 list_for_each_entry_continue_reverse(port, &team->port_list, list)
1197 dev_set_mtu(port->dev, dev->mtu);
Jiri Pirko61dc3462011-11-16 11:09:08 +00001198 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001199
1200 return err;
1201}
1202
1203static struct rtnl_link_stats64 *
1204team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1205{
1206 struct team *team = netdev_priv(dev);
1207 struct team_pcpu_stats *p;
1208 u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
1209 u32 rx_dropped = 0, tx_dropped = 0;
1210 unsigned int start;
1211 int i;
1212
1213 for_each_possible_cpu(i) {
1214 p = per_cpu_ptr(team->pcpu_stats, i);
1215 do {
1216 start = u64_stats_fetch_begin_bh(&p->syncp);
1217 rx_packets = p->rx_packets;
1218 rx_bytes = p->rx_bytes;
1219 rx_multicast = p->rx_multicast;
1220 tx_packets = p->tx_packets;
1221 tx_bytes = p->tx_bytes;
1222 } while (u64_stats_fetch_retry_bh(&p->syncp, start));
1223
1224 stats->rx_packets += rx_packets;
1225 stats->rx_bytes += rx_bytes;
1226 stats->multicast += rx_multicast;
1227 stats->tx_packets += tx_packets;
1228 stats->tx_bytes += tx_bytes;
1229 /*
1230 * rx_dropped & tx_dropped are u32, updated
1231 * without syncp protection.
1232 */
1233 rx_dropped += p->rx_dropped;
1234 tx_dropped += p->tx_dropped;
1235 }
1236 stats->rx_dropped = rx_dropped;
1237 stats->tx_dropped = tx_dropped;
1238 return stats;
1239}
1240
Jiri Pirko8e586132011-12-08 19:52:37 -05001241static int team_vlan_rx_add_vid(struct net_device *dev, uint16_t vid)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001242{
1243 struct team *team = netdev_priv(dev);
1244 struct team_port *port;
Jiri Pirko87002b02011-12-08 04:11:17 +00001245 int err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001246
Jiri Pirko87002b02011-12-08 04:11:17 +00001247 /*
1248 * Alhough this is reader, it's guarded by team lock. It's not possible
1249 * to traverse list in reverse under rcu_read_lock
1250 */
1251 mutex_lock(&team->lock);
1252 list_for_each_entry(port, &team->port_list, list) {
1253 err = vlan_vid_add(port->dev, vid);
1254 if (err)
1255 goto unwind;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001256 }
Jiri Pirko87002b02011-12-08 04:11:17 +00001257 mutex_unlock(&team->lock);
Jiri Pirko8e586132011-12-08 19:52:37 -05001258
1259 return 0;
Jiri Pirko87002b02011-12-08 04:11:17 +00001260
1261unwind:
1262 list_for_each_entry_continue_reverse(port, &team->port_list, list)
1263 vlan_vid_del(port->dev, vid);
1264 mutex_unlock(&team->lock);
1265
1266 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001267}
1268
Jiri Pirko8e586132011-12-08 19:52:37 -05001269static int team_vlan_rx_kill_vid(struct net_device *dev, uint16_t vid)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001270{
1271 struct team *team = netdev_priv(dev);
1272 struct team_port *port;
1273
1274 rcu_read_lock();
Jiri Pirko87002b02011-12-08 04:11:17 +00001275 list_for_each_entry_rcu(port, &team->port_list, list)
1276 vlan_vid_del(port->dev, vid);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001277 rcu_read_unlock();
Jiri Pirko8e586132011-12-08 19:52:37 -05001278
1279 return 0;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001280}
1281
1282static int team_add_slave(struct net_device *dev, struct net_device *port_dev)
1283{
1284 struct team *team = netdev_priv(dev);
1285 int err;
1286
Jiri Pirko61dc3462011-11-16 11:09:08 +00001287 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001288 err = team_port_add(team, port_dev);
Jiri Pirko61dc3462011-11-16 11:09:08 +00001289 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001290 return err;
1291}
1292
1293static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
1294{
1295 struct team *team = netdev_priv(dev);
1296 int err;
1297
Jiri Pirko61dc3462011-11-16 11:09:08 +00001298 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001299 err = team_port_del(team, port_dev);
Jiri Pirko61dc3462011-11-16 11:09:08 +00001300 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001301 return err;
1302}
1303
Jiri Pirko234a8fd2011-11-17 04:16:04 +00001304static netdev_features_t team_fix_features(struct net_device *dev,
1305 netdev_features_t features)
1306{
1307 struct team_port *port;
1308 struct team *team = netdev_priv(dev);
1309 netdev_features_t mask;
1310
1311 mask = features;
1312 features &= ~NETIF_F_ONE_FOR_ALL;
1313 features |= NETIF_F_ALL_FOR_ALL;
1314
1315 rcu_read_lock();
1316 list_for_each_entry_rcu(port, &team->port_list, list) {
1317 features = netdev_increment_features(features,
1318 port->dev->features,
1319 mask);
1320 }
1321 rcu_read_unlock();
1322 return features;
1323}
1324
Jiri Pirko3d249d42011-11-11 22:16:48 +00001325static const struct net_device_ops team_netdev_ops = {
1326 .ndo_init = team_init,
1327 .ndo_uninit = team_uninit,
1328 .ndo_open = team_open,
1329 .ndo_stop = team_close,
1330 .ndo_start_xmit = team_xmit,
1331 .ndo_change_rx_flags = team_change_rx_flags,
1332 .ndo_set_rx_mode = team_set_rx_mode,
1333 .ndo_set_mac_address = team_set_mac_address,
1334 .ndo_change_mtu = team_change_mtu,
1335 .ndo_get_stats64 = team_get_stats64,
1336 .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid,
1337 .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid,
1338 .ndo_add_slave = team_add_slave,
1339 .ndo_del_slave = team_del_slave,
Jiri Pirko234a8fd2011-11-17 04:16:04 +00001340 .ndo_fix_features = team_fix_features,
Jiri Pirko3d249d42011-11-11 22:16:48 +00001341};
1342
1343
1344/***********************
1345 * rt netlink interface
1346 ***********************/
1347
1348static void team_setup(struct net_device *dev)
1349{
1350 ether_setup(dev);
1351
1352 dev->netdev_ops = &team_netdev_ops;
1353 dev->destructor = team_destructor;
1354 dev->tx_queue_len = 0;
1355 dev->flags |= IFF_MULTICAST;
1356 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
1357
1358 /*
1359 * Indicate we support unicast address filtering. That way core won't
1360 * bring us to promisc mode in case a unicast addr is added.
1361 * Let this up to underlay drivers.
1362 */
1363 dev->priv_flags |= IFF_UNICAST_FLT;
1364
1365 dev->features |= NETIF_F_LLTX;
1366 dev->features |= NETIF_F_GRO;
1367 dev->hw_features = NETIF_F_HW_VLAN_TX |
1368 NETIF_F_HW_VLAN_RX |
1369 NETIF_F_HW_VLAN_FILTER;
1370
1371 dev->features |= dev->hw_features;
1372}
1373
1374static int team_newlink(struct net *src_net, struct net_device *dev,
1375 struct nlattr *tb[], struct nlattr *data[])
1376{
1377 int err;
1378
1379 if (tb[IFLA_ADDRESS] == NULL)
Danny Kukawka7ce5d222012-02-15 06:45:40 +00001380 eth_hw_addr_random(dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001381
1382 err = register_netdevice(dev);
1383 if (err)
1384 return err;
1385
1386 return 0;
1387}
1388
1389static int team_validate(struct nlattr *tb[], struct nlattr *data[])
1390{
1391 if (tb[IFLA_ADDRESS]) {
1392 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1393 return -EINVAL;
1394 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1395 return -EADDRNOTAVAIL;
1396 }
1397 return 0;
1398}
1399
1400static struct rtnl_link_ops team_link_ops __read_mostly = {
1401 .kind = DRV_NAME,
1402 .priv_size = sizeof(struct team),
1403 .setup = team_setup,
1404 .newlink = team_newlink,
1405 .validate = team_validate,
1406};
1407
1408
1409/***********************************
1410 * Generic netlink custom interface
1411 ***********************************/
1412
1413static struct genl_family team_nl_family = {
1414 .id = GENL_ID_GENERATE,
1415 .name = TEAM_GENL_NAME,
1416 .version = TEAM_GENL_VERSION,
1417 .maxattr = TEAM_ATTR_MAX,
1418 .netnsok = true,
1419};
1420
1421static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
1422 [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, },
1423 [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 },
1424 [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED },
1425 [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED },
1426};
1427
1428static const struct nla_policy
1429team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
1430 [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
1431 [TEAM_ATTR_OPTION_NAME] = {
1432 .type = NLA_STRING,
1433 .len = TEAM_STRING_MAX_LEN,
1434 },
1435 [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG },
1436 [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 },
Jiri Pirko26155982012-04-04 12:16:26 +00001437 [TEAM_ATTR_OPTION_DATA] = { .type = NLA_BINARY },
Jiri Pirko3d249d42011-11-11 22:16:48 +00001438};
1439
1440static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
1441{
1442 struct sk_buff *msg;
1443 void *hdr;
1444 int err;
1445
1446 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1447 if (!msg)
1448 return -ENOMEM;
1449
1450 hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq,
1451 &team_nl_family, 0, TEAM_CMD_NOOP);
1452 if (IS_ERR(hdr)) {
1453 err = PTR_ERR(hdr);
1454 goto err_msg_put;
1455 }
1456
1457 genlmsg_end(msg, hdr);
1458
1459 return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid);
1460
1461err_msg_put:
1462 nlmsg_free(msg);
1463
1464 return err;
1465}
1466
1467/*
1468 * Netlink cmd functions should be locked by following two functions.
Jiri Pirko8c0713a2011-11-16 11:55:54 +00001469 * Since dev gets held here, that ensures dev won't disappear in between.
Jiri Pirko3d249d42011-11-11 22:16:48 +00001470 */
1471static struct team *team_nl_team_get(struct genl_info *info)
1472{
1473 struct net *net = genl_info_net(info);
1474 int ifindex;
1475 struct net_device *dev;
1476 struct team *team;
1477
1478 if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
1479 return NULL;
1480
1481 ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
Jiri Pirko8c0713a2011-11-16 11:55:54 +00001482 dev = dev_get_by_index(net, ifindex);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001483 if (!dev || dev->netdev_ops != &team_netdev_ops) {
Jiri Pirko8c0713a2011-11-16 11:55:54 +00001484 if (dev)
1485 dev_put(dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001486 return NULL;
1487 }
1488
1489 team = netdev_priv(dev);
Jiri Pirko61dc3462011-11-16 11:09:08 +00001490 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001491 return team;
1492}
1493
1494static void team_nl_team_put(struct team *team)
1495{
Jiri Pirko61dc3462011-11-16 11:09:08 +00001496 mutex_unlock(&team->lock);
Jiri Pirko8c0713a2011-11-16 11:55:54 +00001497 dev_put(team->dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001498}
1499
1500static int team_nl_send_generic(struct genl_info *info, struct team *team,
1501 int (*fill_func)(struct sk_buff *skb,
1502 struct genl_info *info,
1503 int flags, struct team *team))
1504{
1505 struct sk_buff *skb;
1506 int err;
1507
1508 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1509 if (!skb)
1510 return -ENOMEM;
1511
1512 err = fill_func(skb, info, NLM_F_ACK, team);
1513 if (err < 0)
1514 goto err_fill;
1515
1516 err = genlmsg_unicast(genl_info_net(info), skb, info->snd_pid);
1517 return err;
1518
1519err_fill:
1520 nlmsg_free(skb);
1521 return err;
1522}
1523
Jiri Pirkob82b9182012-01-24 05:16:00 +00001524static int team_nl_fill_options_get(struct sk_buff *skb,
1525 u32 pid, u32 seq, int flags,
1526 struct team *team, bool fillall)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001527{
1528 struct nlattr *option_list;
1529 void *hdr;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001530 struct team_option_inst *opt_inst;
1531 int err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001532
1533 hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags,
1534 TEAM_CMD_OPTIONS_GET);
1535 if (IS_ERR(hdr))
1536 return PTR_ERR(hdr);
1537
David S. Miller86ebb022012-04-01 20:25:18 -04001538 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
1539 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001540 option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
1541 if (!option_list)
1542 return -EMSGSIZE;
1543
Jiri Pirko80f7c662012-04-10 05:15:42 +00001544 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
Jiri Pirko3d249d42011-11-11 22:16:48 +00001545 struct nlattr *option_item;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001546 struct team_option *option = opt_inst->option;
1547 struct team_gsetter_ctx ctx;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001548
Jiri Pirkob82b9182012-01-24 05:16:00 +00001549 /* Include only changed options if fill all mode is not on */
Jiri Pirko80f7c662012-04-10 05:15:42 +00001550 if (!fillall && !opt_inst->changed)
Jiri Pirkob82b9182012-01-24 05:16:00 +00001551 continue;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001552 option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
1553 if (!option_item)
1554 goto nla_put_failure;
David S. Miller86ebb022012-04-01 20:25:18 -04001555 if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name))
1556 goto nla_put_failure;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001557 if (opt_inst->changed) {
David S. Miller86ebb022012-04-01 20:25:18 -04001558 if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED))
1559 goto nla_put_failure;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001560 opt_inst->changed = false;
Jiri Pirkob82b9182012-01-24 05:16:00 +00001561 }
Jiri Pirko80f7c662012-04-10 05:15:42 +00001562 if (opt_inst->removed &&
David S. Miller86ebb022012-04-01 20:25:18 -04001563 nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED))
1564 goto nla_put_failure;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001565 if (opt_inst->port &&
1566 nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX,
1567 opt_inst->port->dev->ifindex))
1568 goto nla_put_failure;
1569 ctx.port = opt_inst->port;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001570 switch (option->type) {
1571 case TEAM_OPTION_TYPE_U32:
David S. Miller86ebb022012-04-01 20:25:18 -04001572 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32))
1573 goto nla_put_failure;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001574 err = team_option_get(team, opt_inst, &ctx);
1575 if (err)
1576 goto errout;
1577 if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA,
1578 ctx.data.u32_val))
David S. Miller86ebb022012-04-01 20:25:18 -04001579 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001580 break;
1581 case TEAM_OPTION_TYPE_STRING:
David S. Miller86ebb022012-04-01 20:25:18 -04001582 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING))
1583 goto nla_put_failure;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001584 err = team_option_get(team, opt_inst, &ctx);
1585 if (err)
1586 goto errout;
David S. Miller86ebb022012-04-01 20:25:18 -04001587 if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA,
Jiri Pirko80f7c662012-04-10 05:15:42 +00001588 ctx.data.str_val))
David S. Miller86ebb022012-04-01 20:25:18 -04001589 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001590 break;
Jiri Pirko26155982012-04-04 12:16:26 +00001591 case TEAM_OPTION_TYPE_BINARY:
1592 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY))
1593 goto nla_put_failure;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001594 err = team_option_get(team, opt_inst, &ctx);
1595 if (err)
1596 goto errout;
Jiri Pirko26155982012-04-04 12:16:26 +00001597 if (nla_put(skb, TEAM_ATTR_OPTION_DATA,
Jiri Pirko80f7c662012-04-10 05:15:42 +00001598 ctx.data.bin_val.len, ctx.data.bin_val.ptr))
Jiri Pirko26155982012-04-04 12:16:26 +00001599 goto nla_put_failure;
1600 break;
Jiri Pirko14f066b2012-04-10 05:15:43 +00001601 case TEAM_OPTION_TYPE_BOOL:
1602 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG))
1603 goto nla_put_failure;
1604 err = team_option_get(team, opt_inst, &ctx);
1605 if (err)
1606 goto errout;
1607 if (ctx.data.bool_val &&
1608 nla_put_flag(skb, TEAM_ATTR_OPTION_DATA))
1609 goto nla_put_failure;
1610 break;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001611 default:
1612 BUG();
1613 }
1614 nla_nest_end(skb, option_item);
1615 }
1616
1617 nla_nest_end(skb, option_list);
1618 return genlmsg_end(skb, hdr);
1619
1620nla_put_failure:
Jiri Pirko80f7c662012-04-10 05:15:42 +00001621 err = -EMSGSIZE;
1622errout:
Jiri Pirko3d249d42011-11-11 22:16:48 +00001623 genlmsg_cancel(skb, hdr);
Jiri Pirko80f7c662012-04-10 05:15:42 +00001624 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001625}
1626
Jiri Pirkob82b9182012-01-24 05:16:00 +00001627static int team_nl_fill_options_get_all(struct sk_buff *skb,
1628 struct genl_info *info, int flags,
1629 struct team *team)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001630{
Jiri Pirkob82b9182012-01-24 05:16:00 +00001631 return team_nl_fill_options_get(skb, info->snd_pid,
1632 info->snd_seq, NLM_F_ACK,
1633 team, true);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001634}
1635
1636static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
1637{
1638 struct team *team;
1639 int err;
1640
1641 team = team_nl_team_get(info);
1642 if (!team)
1643 return -EINVAL;
1644
Jiri Pirkob82b9182012-01-24 05:16:00 +00001645 err = team_nl_send_generic(info, team, team_nl_fill_options_get_all);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001646
1647 team_nl_team_put(team);
1648
1649 return err;
1650}
1651
1652static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
1653{
1654 struct team *team;
1655 int err = 0;
1656 int i;
1657 struct nlattr *nl_option;
1658
1659 team = team_nl_team_get(info);
1660 if (!team)
1661 return -EINVAL;
1662
1663 err = -EINVAL;
1664 if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
1665 err = -EINVAL;
1666 goto team_put;
1667 }
1668
1669 nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
Jiri Pirko80f7c662012-04-10 05:15:42 +00001670 struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1];
1671 struct nlattr *attr_port_ifindex;
Jiri Pirko14f066b2012-04-10 05:15:43 +00001672 struct nlattr *attr_data;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001673 enum team_option_type opt_type;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001674 int opt_port_ifindex = 0; /* != 0 for per-port options */
1675 struct team_option_inst *opt_inst;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001676 char *opt_name;
1677 bool opt_found = false;
1678
1679 if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
1680 err = -EINVAL;
1681 goto team_put;
1682 }
Jiri Pirko80f7c662012-04-10 05:15:42 +00001683 err = nla_parse_nested(opt_attrs, TEAM_ATTR_OPTION_MAX,
Jiri Pirko3d249d42011-11-11 22:16:48 +00001684 nl_option, team_nl_option_policy);
1685 if (err)
1686 goto team_put;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001687 if (!opt_attrs[TEAM_ATTR_OPTION_NAME] ||
Jiri Pirko14f066b2012-04-10 05:15:43 +00001688 !opt_attrs[TEAM_ATTR_OPTION_TYPE]) {
Jiri Pirko3d249d42011-11-11 22:16:48 +00001689 err = -EINVAL;
1690 goto team_put;
1691 }
Jiri Pirko80f7c662012-04-10 05:15:42 +00001692 switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) {
Jiri Pirko3d249d42011-11-11 22:16:48 +00001693 case NLA_U32:
1694 opt_type = TEAM_OPTION_TYPE_U32;
1695 break;
1696 case NLA_STRING:
1697 opt_type = TEAM_OPTION_TYPE_STRING;
1698 break;
Jiri Pirko26155982012-04-04 12:16:26 +00001699 case NLA_BINARY:
1700 opt_type = TEAM_OPTION_TYPE_BINARY;
1701 break;
Jiri Pirko14f066b2012-04-10 05:15:43 +00001702 case NLA_FLAG:
1703 opt_type = TEAM_OPTION_TYPE_BOOL;
1704 break;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001705 default:
1706 goto team_put;
1707 }
1708
Jiri Pirko14f066b2012-04-10 05:15:43 +00001709 attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA];
1710 if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) {
1711 err = -EINVAL;
1712 goto team_put;
1713 }
1714
Jiri Pirko80f7c662012-04-10 05:15:42 +00001715 opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]);
1716 attr_port_ifindex = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX];
1717 if (attr_port_ifindex)
1718 opt_port_ifindex = nla_get_u32(attr_port_ifindex);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001719
Jiri Pirko80f7c662012-04-10 05:15:42 +00001720 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
1721 struct team_option *option = opt_inst->option;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001722 struct team_gsetter_ctx ctx;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001723 int tmp_ifindex;
1724
1725 tmp_ifindex = opt_inst->port ?
1726 opt_inst->port->dev->ifindex : 0;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001727 if (option->type != opt_type ||
Jiri Pirko80f7c662012-04-10 05:15:42 +00001728 strcmp(option->name, opt_name) ||
1729 tmp_ifindex != opt_port_ifindex)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001730 continue;
1731 opt_found = true;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001732 ctx.port = opt_inst->port;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001733 switch (opt_type) {
1734 case TEAM_OPTION_TYPE_U32:
Jiri Pirko14f066b2012-04-10 05:15:43 +00001735 ctx.data.u32_val = nla_get_u32(attr_data);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001736 break;
1737 case TEAM_OPTION_TYPE_STRING:
Jiri Pirko14f066b2012-04-10 05:15:43 +00001738 if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) {
Jiri Pirko26155982012-04-04 12:16:26 +00001739 err = -EINVAL;
1740 goto team_put;
1741 }
Jiri Pirko14f066b2012-04-10 05:15:43 +00001742 ctx.data.str_val = nla_data(attr_data);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001743 break;
Jiri Pirko26155982012-04-04 12:16:26 +00001744 case TEAM_OPTION_TYPE_BINARY:
Jiri Pirko14f066b2012-04-10 05:15:43 +00001745 ctx.data.bin_val.len = nla_len(attr_data);
1746 ctx.data.bin_val.ptr = nla_data(attr_data);
1747 break;
1748 case TEAM_OPTION_TYPE_BOOL:
1749 ctx.data.bool_val = attr_data ? true : false;
Jiri Pirko26155982012-04-04 12:16:26 +00001750 break;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001751 default:
1752 BUG();
1753 }
Jiri Pirko80f7c662012-04-10 05:15:42 +00001754 err = team_option_set(team, opt_inst, &ctx);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001755 if (err)
1756 goto team_put;
1757 }
1758 if (!opt_found) {
1759 err = -ENOENT;
1760 goto team_put;
1761 }
1762 }
1763
1764team_put:
1765 team_nl_team_put(team);
1766
1767 return err;
1768}
1769
Jiri Pirkob82b9182012-01-24 05:16:00 +00001770static int team_nl_fill_port_list_get(struct sk_buff *skb,
1771 u32 pid, u32 seq, int flags,
1772 struct team *team,
1773 bool fillall)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001774{
1775 struct nlattr *port_list;
1776 void *hdr;
1777 struct team_port *port;
1778
1779 hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags,
1780 TEAM_CMD_PORT_LIST_GET);
1781 if (IS_ERR(hdr))
1782 return PTR_ERR(hdr);
1783
David S. Miller86ebb022012-04-01 20:25:18 -04001784 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
1785 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001786 port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
1787 if (!port_list)
1788 return -EMSGSIZE;
1789
1790 list_for_each_entry(port, &team->port_list, list) {
1791 struct nlattr *port_item;
1792
Jiri Pirkob82b9182012-01-24 05:16:00 +00001793 /* Include only changed ports if fill all mode is not on */
1794 if (!fillall && !port->changed)
1795 continue;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001796 port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
1797 if (!port_item)
1798 goto nla_put_failure;
David S. Miller86ebb022012-04-01 20:25:18 -04001799 if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex))
1800 goto nla_put_failure;
Jiri Pirkob82b9182012-01-24 05:16:00 +00001801 if (port->changed) {
David S. Miller86ebb022012-04-01 20:25:18 -04001802 if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED))
1803 goto nla_put_failure;
Jiri Pirkob82b9182012-01-24 05:16:00 +00001804 port->changed = false;
1805 }
David S. Miller86ebb022012-04-01 20:25:18 -04001806 if ((port->removed &&
1807 nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) ||
Jiri Pirko71472ec2012-04-10 05:15:44 +00001808 (port->state.linkup &&
David S. Miller86ebb022012-04-01 20:25:18 -04001809 nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) ||
Jiri Pirko71472ec2012-04-10 05:15:44 +00001810 nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) ||
1811 nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex))
David S. Miller86ebb022012-04-01 20:25:18 -04001812 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001813 nla_nest_end(skb, port_item);
1814 }
1815
1816 nla_nest_end(skb, port_list);
1817 return genlmsg_end(skb, hdr);
1818
1819nla_put_failure:
1820 genlmsg_cancel(skb, hdr);
1821 return -EMSGSIZE;
1822}
1823
Jiri Pirkob82b9182012-01-24 05:16:00 +00001824static int team_nl_fill_port_list_get_all(struct sk_buff *skb,
1825 struct genl_info *info, int flags,
1826 struct team *team)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001827{
Jiri Pirkob82b9182012-01-24 05:16:00 +00001828 return team_nl_fill_port_list_get(skb, info->snd_pid,
1829 info->snd_seq, NLM_F_ACK,
1830 team, true);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001831}
1832
1833static int team_nl_cmd_port_list_get(struct sk_buff *skb,
1834 struct genl_info *info)
1835{
1836 struct team *team;
1837 int err;
1838
1839 team = team_nl_team_get(info);
1840 if (!team)
1841 return -EINVAL;
1842
Jiri Pirkob82b9182012-01-24 05:16:00 +00001843 err = team_nl_send_generic(info, team, team_nl_fill_port_list_get_all);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001844
1845 team_nl_team_put(team);
1846
1847 return err;
1848}
1849
1850static struct genl_ops team_nl_ops[] = {
1851 {
1852 .cmd = TEAM_CMD_NOOP,
1853 .doit = team_nl_cmd_noop,
1854 .policy = team_nl_policy,
1855 },
1856 {
1857 .cmd = TEAM_CMD_OPTIONS_SET,
1858 .doit = team_nl_cmd_options_set,
1859 .policy = team_nl_policy,
1860 .flags = GENL_ADMIN_PERM,
1861 },
1862 {
1863 .cmd = TEAM_CMD_OPTIONS_GET,
1864 .doit = team_nl_cmd_options_get,
1865 .policy = team_nl_policy,
1866 .flags = GENL_ADMIN_PERM,
1867 },
1868 {
1869 .cmd = TEAM_CMD_PORT_LIST_GET,
1870 .doit = team_nl_cmd_port_list_get,
1871 .policy = team_nl_policy,
1872 .flags = GENL_ADMIN_PERM,
1873 },
1874};
1875
1876static struct genl_multicast_group team_change_event_mcgrp = {
1877 .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME,
1878};
1879
Jiri Pirkob82b9182012-01-24 05:16:00 +00001880static int team_nl_send_event_options_get(struct team *team)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001881{
1882 struct sk_buff *skb;
1883 int err;
1884 struct net *net = dev_net(team->dev);
1885
1886 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1887 if (!skb)
1888 return -ENOMEM;
1889
Jiri Pirkob82b9182012-01-24 05:16:00 +00001890 err = team_nl_fill_options_get(skb, 0, 0, 0, team, false);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001891 if (err < 0)
1892 goto err_fill;
1893
1894 err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id,
1895 GFP_KERNEL);
1896 return err;
1897
1898err_fill:
1899 nlmsg_free(skb);
1900 return err;
1901}
1902
Jiri Pirkob82b9182012-01-24 05:16:00 +00001903static int team_nl_send_event_port_list_get(struct team *team)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001904{
1905 struct sk_buff *skb;
1906 int err;
Jiri Pirkob82b9182012-01-24 05:16:00 +00001907 struct net *net = dev_net(team->dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001908
1909 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1910 if (!skb)
1911 return -ENOMEM;
1912
Jiri Pirkob82b9182012-01-24 05:16:00 +00001913 err = team_nl_fill_port_list_get(skb, 0, 0, 0, team, false);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001914 if (err < 0)
1915 goto err_fill;
1916
1917 err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id,
1918 GFP_KERNEL);
1919 return err;
1920
1921err_fill:
1922 nlmsg_free(skb);
1923 return err;
1924}
1925
1926static int team_nl_init(void)
1927{
1928 int err;
1929
1930 err = genl_register_family_with_ops(&team_nl_family, team_nl_ops,
1931 ARRAY_SIZE(team_nl_ops));
1932 if (err)
1933 return err;
1934
1935 err = genl_register_mc_group(&team_nl_family, &team_change_event_mcgrp);
1936 if (err)
1937 goto err_change_event_grp_reg;
1938
1939 return 0;
1940
1941err_change_event_grp_reg:
1942 genl_unregister_family(&team_nl_family);
1943
1944 return err;
1945}
1946
1947static void team_nl_fini(void)
1948{
1949 genl_unregister_family(&team_nl_family);
1950}
1951
1952
1953/******************
1954 * Change checkers
1955 ******************/
1956
Jiri Pirkob82b9182012-01-24 05:16:00 +00001957static void __team_options_change_check(struct team *team)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001958{
1959 int err;
1960
Jiri Pirkob82b9182012-01-24 05:16:00 +00001961 err = team_nl_send_event_options_get(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001962 if (err)
1963 netdev_warn(team->dev, "Failed to send options change via netlink\n");
1964}
1965
1966/* rtnl lock is held */
1967static void __team_port_change_check(struct team_port *port, bool linkup)
1968{
1969 int err;
1970
Jiri Pirko71472ec2012-04-10 05:15:44 +00001971 if (!port->removed && port->state.linkup == linkup)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001972 return;
1973
Jiri Pirkob82b9182012-01-24 05:16:00 +00001974 port->changed = true;
Jiri Pirko71472ec2012-04-10 05:15:44 +00001975 port->state.linkup = linkup;
1976 team_refresh_port_linkup(port);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001977 if (linkup) {
1978 struct ethtool_cmd ecmd;
1979
1980 err = __ethtool_get_settings(port->dev, &ecmd);
1981 if (!err) {
Jiri Pirko71472ec2012-04-10 05:15:44 +00001982 port->state.speed = ethtool_cmd_speed(&ecmd);
1983 port->state.duplex = ecmd.duplex;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001984 goto send_event;
1985 }
1986 }
Jiri Pirko71472ec2012-04-10 05:15:44 +00001987 port->state.speed = 0;
1988 port->state.duplex = 0;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001989
1990send_event:
Jiri Pirkob82b9182012-01-24 05:16:00 +00001991 err = team_nl_send_event_port_list_get(port->team);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001992 if (err)
1993 netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink\n",
1994 port->dev->name);
1995
1996}
1997
1998static void team_port_change_check(struct team_port *port, bool linkup)
1999{
2000 struct team *team = port->team;
2001
Jiri Pirko61dc3462011-11-16 11:09:08 +00002002 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002003 __team_port_change_check(port, linkup);
Jiri Pirko61dc3462011-11-16 11:09:08 +00002004 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002005}
2006
2007/************************************
2008 * Net device notifier event handler
2009 ************************************/
2010
2011static int team_device_event(struct notifier_block *unused,
2012 unsigned long event, void *ptr)
2013{
2014 struct net_device *dev = (struct net_device *) ptr;
2015 struct team_port *port;
2016
2017 port = team_port_get_rtnl(dev);
2018 if (!port)
2019 return NOTIFY_DONE;
2020
2021 switch (event) {
2022 case NETDEV_UP:
2023 if (netif_carrier_ok(dev))
2024 team_port_change_check(port, true);
2025 case NETDEV_DOWN:
2026 team_port_change_check(port, false);
2027 case NETDEV_CHANGE:
2028 if (netif_running(port->dev))
2029 team_port_change_check(port,
2030 !!netif_carrier_ok(port->dev));
2031 break;
2032 case NETDEV_UNREGISTER:
2033 team_del_slave(port->team->dev, dev);
2034 break;
2035 case NETDEV_FEAT_CHANGE:
2036 team_compute_features(port->team);
2037 break;
2038 case NETDEV_CHANGEMTU:
2039 /* Forbid to change mtu of underlaying device */
2040 return NOTIFY_BAD;
2041 case NETDEV_PRE_TYPE_CHANGE:
2042 /* Forbid to change type of underlaying device */
2043 return NOTIFY_BAD;
2044 }
2045 return NOTIFY_DONE;
2046}
2047
2048static struct notifier_block team_notifier_block __read_mostly = {
2049 .notifier_call = team_device_event,
2050};
2051
2052
2053/***********************
2054 * Module init and exit
2055 ***********************/
2056
2057static int __init team_module_init(void)
2058{
2059 int err;
2060
2061 register_netdevice_notifier(&team_notifier_block);
2062
2063 err = rtnl_link_register(&team_link_ops);
2064 if (err)
2065 goto err_rtnl_reg;
2066
2067 err = team_nl_init();
2068 if (err)
2069 goto err_nl_init;
2070
2071 return 0;
2072
2073err_nl_init:
2074 rtnl_link_unregister(&team_link_ops);
2075
2076err_rtnl_reg:
2077 unregister_netdevice_notifier(&team_notifier_block);
2078
2079 return err;
2080}
2081
2082static void __exit team_module_exit(void)
2083{
2084 team_nl_fini();
2085 rtnl_link_unregister(&team_link_ops);
2086 unregister_netdevice_notifier(&team_notifier_block);
2087}
2088
2089module_init(team_module_init);
2090module_exit(team_module_exit);
2091
2092MODULE_LICENSE("GPL v2");
2093MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
2094MODULE_DESCRIPTION("Ethernet team device driver");
2095MODULE_ALIAS_RTNL_LINK(DRV_NAME);