blob: 9e9d3e57a2f64c7b05682a7f6eef28c2788566b4 [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 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
Jiri Pirko0f1aad22012-06-19 05:54:11 +000085
Jiri Pirko3d249d42011-11-11 22:16:48 +000086/*******************
87 * Options handling
88 *******************/
89
Jiri Pirko80f7c662012-04-10 05:15:42 +000090struct team_option_inst { /* One for each option instance */
91 struct list_head list;
Jiri Pirko01048d92012-06-19 05:54:14 +000092 struct list_head tmp_list;
Jiri Pirko80f7c662012-04-10 05:15:42 +000093 struct team_option *option;
Jiri Pirko85d59a82012-06-19 05:54:10 +000094 struct team_option_inst_info info;
Jiri Pirko80f7c662012-04-10 05:15:42 +000095 bool changed;
96 bool removed;
97};
98
99static struct team_option *__team_find_option(struct team *team,
100 const char *opt_name)
Jiri Pirko358b8382011-11-16 11:09:09 +0000101{
102 struct team_option *option;
103
104 list_for_each_entry(option, &team->option_list, list) {
105 if (strcmp(option->name, opt_name) == 0)
106 return option;
107 }
108 return NULL;
109}
110
Jiri Pirko80f7c662012-04-10 05:15:42 +0000111static void __team_option_inst_del(struct team_option_inst *opt_inst)
112{
113 list_del(&opt_inst->list);
114 kfree(opt_inst);
115}
116
117static void __team_option_inst_del_option(struct team *team,
118 struct team_option *option)
119{
120 struct team_option_inst *opt_inst, *tmp;
121
122 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
123 if (opt_inst->option == option)
124 __team_option_inst_del(opt_inst);
125 }
126}
127
Jiri Pirkob1303322012-06-19 05:54:08 +0000128static int __team_option_inst_add(struct team *team, struct team_option *option,
129 struct team_port *port)
130{
131 struct team_option_inst *opt_inst;
132 unsigned int array_size;
133 unsigned int i;
Jiri Pirko85d59a82012-06-19 05:54:10 +0000134 int err;
Jiri Pirkob1303322012-06-19 05:54:08 +0000135
136 array_size = option->array_size;
137 if (!array_size)
138 array_size = 1; /* No array but still need one instance */
139
140 for (i = 0; i < array_size; i++) {
141 opt_inst = kmalloc(sizeof(*opt_inst), GFP_KERNEL);
142 if (!opt_inst)
143 return -ENOMEM;
144 opt_inst->option = option;
Jiri Pirko85d59a82012-06-19 05:54:10 +0000145 opt_inst->info.port = port;
146 opt_inst->info.array_index = i;
Jiri Pirkob1303322012-06-19 05:54:08 +0000147 opt_inst->changed = true;
148 opt_inst->removed = false;
149 list_add_tail(&opt_inst->list, &team->option_inst_list);
Jiri Pirko85d59a82012-06-19 05:54:10 +0000150 if (option->init) {
151 err = option->init(team, &opt_inst->info);
152 if (err)
153 return err;
154 }
155
Jiri Pirkob1303322012-06-19 05:54:08 +0000156 }
157 return 0;
158}
159
Jiri Pirko80f7c662012-04-10 05:15:42 +0000160static int __team_option_inst_add_option(struct team *team,
161 struct team_option *option)
162{
163 struct team_port *port;
164 int err;
165
Jiri Pirkob1303322012-06-19 05:54:08 +0000166 if (!option->per_port) {
Jiri Pirkof88725f2012-06-19 05:54:15 +0000167 err = __team_option_inst_add(team, option, NULL);
Jiri Pirkob1303322012-06-19 05:54:08 +0000168 if (err)
169 goto inst_del_option;
170 }
Jiri Pirko80f7c662012-04-10 05:15:42 +0000171
172 list_for_each_entry(port, &team->port_list, list) {
173 err = __team_option_inst_add(team, option, port);
174 if (err)
175 goto inst_del_option;
176 }
177 return 0;
178
179inst_del_option:
180 __team_option_inst_del_option(team, option);
181 return err;
182}
183
184static void __team_option_inst_mark_removed_option(struct team *team,
185 struct team_option *option)
186{
187 struct team_option_inst *opt_inst;
188
189 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
190 if (opt_inst->option == option) {
191 opt_inst->changed = true;
192 opt_inst->removed = true;
193 }
194 }
195}
196
197static void __team_option_inst_del_port(struct team *team,
198 struct team_port *port)
199{
200 struct team_option_inst *opt_inst, *tmp;
201
202 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
203 if (opt_inst->option->per_port &&
Jiri Pirko85d59a82012-06-19 05:54:10 +0000204 opt_inst->info.port == port)
Jiri Pirko80f7c662012-04-10 05:15:42 +0000205 __team_option_inst_del(opt_inst);
206 }
207}
208
209static int __team_option_inst_add_port(struct team *team,
210 struct team_port *port)
211{
212 struct team_option *option;
213 int err;
214
215 list_for_each_entry(option, &team->option_list, list) {
216 if (!option->per_port)
217 continue;
218 err = __team_option_inst_add(team, option, port);
219 if (err)
220 goto inst_del_port;
221 }
222 return 0;
223
224inst_del_port:
225 __team_option_inst_del_port(team, port);
226 return err;
227}
228
229static void __team_option_inst_mark_removed_port(struct team *team,
230 struct team_port *port)
231{
232 struct team_option_inst *opt_inst;
233
234 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
Jiri Pirko85d59a82012-06-19 05:54:10 +0000235 if (opt_inst->info.port == port) {
Jiri Pirko80f7c662012-04-10 05:15:42 +0000236 opt_inst->changed = true;
237 opt_inst->removed = true;
238 }
239 }
240}
241
242static int __team_options_register(struct team *team,
243 const struct team_option *option,
244 size_t option_count)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000245{
246 int i;
Jiri Pirko2bba19f2011-11-17 04:16:05 +0000247 struct team_option **dst_opts;
Jiri Pirko358b8382011-11-16 11:09:09 +0000248 int err;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000249
Jiri Pirko2bba19f2011-11-17 04:16:05 +0000250 dst_opts = kzalloc(sizeof(struct team_option *) * option_count,
251 GFP_KERNEL);
252 if (!dst_opts)
253 return -ENOMEM;
Jiri Pirko358b8382011-11-16 11:09:09 +0000254 for (i = 0; i < option_count; i++, option++) {
Jiri Pirko358b8382011-11-16 11:09:09 +0000255 if (__team_find_option(team, option->name)) {
256 err = -EEXIST;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000257 goto alloc_rollback;
Jiri Pirko358b8382011-11-16 11:09:09 +0000258 }
Jiri Pirkof8a15af2011-11-17 06:32:37 +0000259 dst_opts[i] = kmemdup(option, sizeof(*option), GFP_KERNEL);
260 if (!dst_opts[i]) {
Jiri Pirko358b8382011-11-16 11:09:09 +0000261 err = -ENOMEM;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000262 goto alloc_rollback;
Jiri Pirko358b8382011-11-16 11:09:09 +0000263 }
Jiri Pirko358b8382011-11-16 11:09:09 +0000264 }
265
Jiri Pirkob82b9182012-01-24 05:16:00 +0000266 for (i = 0; i < option_count; i++) {
Jiri Pirko80f7c662012-04-10 05:15:42 +0000267 err = __team_option_inst_add_option(team, dst_opts[i]);
268 if (err)
269 goto inst_rollback;
Jiri Pirko358b8382011-11-16 11:09:09 +0000270 list_add_tail(&dst_opts[i]->list, &team->option_list);
Jiri Pirkob82b9182012-01-24 05:16:00 +0000271 }
Jiri Pirko358b8382011-11-16 11:09:09 +0000272
Jiri Pirko2bba19f2011-11-17 04:16:05 +0000273 kfree(dst_opts);
Jiri Pirko358b8382011-11-16 11:09:09 +0000274 return 0;
275
Jiri Pirko80f7c662012-04-10 05:15:42 +0000276inst_rollback:
277 for (i--; i >= 0; i--)
278 __team_option_inst_del_option(team, dst_opts[i]);
279
280 i = option_count - 1;
281alloc_rollback:
282 for (i--; i >= 0; i--)
Jiri Pirko358b8382011-11-16 11:09:09 +0000283 kfree(dst_opts[i]);
284
Jiri Pirko2bba19f2011-11-17 04:16:05 +0000285 kfree(dst_opts);
Jiri Pirko358b8382011-11-16 11:09:09 +0000286 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000287}
Jiri Pirko358b8382011-11-16 11:09:09 +0000288
Jiri Pirkob82b9182012-01-24 05:16:00 +0000289static void __team_options_mark_removed(struct team *team,
290 const struct team_option *option,
291 size_t option_count)
292{
293 int i;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000294
Jiri Pirkob82b9182012-01-24 05:16:00 +0000295 for (i = 0; i < option_count; i++, option++) {
296 struct team_option *del_opt;
297
298 del_opt = __team_find_option(team, option->name);
Jiri Pirko80f7c662012-04-10 05:15:42 +0000299 if (del_opt)
300 __team_option_inst_mark_removed_option(team, del_opt);
Jiri Pirkob82b9182012-01-24 05:16:00 +0000301 }
302}
Jiri Pirko3d249d42011-11-11 22:16:48 +0000303
304static void __team_options_unregister(struct team *team,
Jiri Pirko358b8382011-11-16 11:09:09 +0000305 const struct team_option *option,
Jiri Pirko3d249d42011-11-11 22:16:48 +0000306 size_t option_count)
307{
308 int i;
309
Jiri Pirko358b8382011-11-16 11:09:09 +0000310 for (i = 0; i < option_count; i++, option++) {
311 struct team_option *del_opt;
312
313 del_opt = __team_find_option(team, option->name);
314 if (del_opt) {
Jiri Pirko80f7c662012-04-10 05:15:42 +0000315 __team_option_inst_del_option(team, del_opt);
Jiri Pirko358b8382011-11-16 11:09:09 +0000316 list_del(&del_opt->list);
317 kfree(del_opt);
318 }
319 }
Jiri Pirko3d249d42011-11-11 22:16:48 +0000320}
321
Jiri Pirkob82b9182012-01-24 05:16:00 +0000322static void __team_options_change_check(struct team *team);
Jiri Pirko01048d92012-06-19 05:54:14 +0000323static void __team_option_inst_change(struct team *team,
324 struct team_option_inst *opt_inst);
Jiri Pirkob82b9182012-01-24 05:16:00 +0000325
326int team_options_register(struct team *team,
327 const struct team_option *option,
328 size_t option_count)
329{
330 int err;
331
332 err = __team_options_register(team, option, option_count);
333 if (err)
334 return err;
335 __team_options_change_check(team);
336 return 0;
337}
338EXPORT_SYMBOL(team_options_register);
339
Jiri Pirko358b8382011-11-16 11:09:09 +0000340void team_options_unregister(struct team *team,
341 const struct team_option *option,
Jiri Pirko3d249d42011-11-11 22:16:48 +0000342 size_t option_count)
343{
Jiri Pirkob82b9182012-01-24 05:16:00 +0000344 __team_options_mark_removed(team, option, option_count);
345 __team_options_change_check(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000346 __team_options_unregister(team, option, option_count);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000347}
348EXPORT_SYMBOL(team_options_unregister);
349
Jiri Pirko80f7c662012-04-10 05:15:42 +0000350static int team_option_port_add(struct team *team, struct team_port *port)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000351{
352 int err;
353
Jiri Pirko80f7c662012-04-10 05:15:42 +0000354 err = __team_option_inst_add_port(team, port);
355 if (err)
356 return err;
357 __team_options_change_check(team);
358 return 0;
359}
360
361static void team_option_port_del(struct team *team, struct team_port *port)
362{
363 __team_option_inst_mark_removed_port(team, port);
364 __team_options_change_check(team);
365 __team_option_inst_del_port(team, port);
366}
367
368static int team_option_get(struct team *team,
369 struct team_option_inst *opt_inst,
370 struct team_gsetter_ctx *ctx)
371{
Jiri Pirkof82b9592012-06-19 05:54:07 +0000372 if (!opt_inst->option->getter)
373 return -EOPNOTSUPP;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000374 return opt_inst->option->getter(team, ctx);
375}
376
377static int team_option_set(struct team *team,
378 struct team_option_inst *opt_inst,
379 struct team_gsetter_ctx *ctx)
380{
381 int err;
382
Jiri Pirkof82b9592012-06-19 05:54:07 +0000383 if (!opt_inst->option->setter)
384 return -EOPNOTSUPP;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000385 err = opt_inst->option->setter(team, ctx);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000386 if (err)
387 return err;
388
Jiri Pirko01048d92012-06-19 05:54:14 +0000389 __team_option_inst_change(team, opt_inst);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000390 return err;
391}
392
Jiri Pirko0f1aad22012-06-19 05:54:11 +0000393void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info)
394{
395 struct team_option_inst *opt_inst;
396
397 opt_inst = container_of(opt_inst_info, struct team_option_inst, info);
398 opt_inst->changed = true;
399}
400EXPORT_SYMBOL(team_option_inst_set_change);
401
402void team_options_change_check(struct team *team)
403{
404 __team_options_change_check(team);
405}
406EXPORT_SYMBOL(team_options_change_check);
407
408
Jiri Pirko3d249d42011-11-11 22:16:48 +0000409/****************
410 * Mode handling
411 ****************/
412
413static LIST_HEAD(mode_list);
414static DEFINE_SPINLOCK(mode_list_lock);
415
Jiri Pirko04027882012-06-19 05:54:03 +0000416struct team_mode_item {
417 struct list_head list;
418 const struct team_mode *mode;
419};
Jiri Pirko3d249d42011-11-11 22:16:48 +0000420
Jiri Pirko04027882012-06-19 05:54:03 +0000421static struct team_mode_item *__find_mode(const char *kind)
422{
423 struct team_mode_item *mitem;
424
425 list_for_each_entry(mitem, &mode_list, list) {
426 if (strcmp(mitem->mode->kind, kind) == 0)
427 return mitem;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000428 }
429 return NULL;
430}
431
432static bool is_good_mode_name(const char *name)
433{
434 while (*name != '\0') {
435 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
436 return false;
437 name++;
438 }
439 return true;
440}
441
Jiri Pirko04027882012-06-19 05:54:03 +0000442int team_mode_register(const struct team_mode *mode)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000443{
444 int err = 0;
Jiri Pirko04027882012-06-19 05:54:03 +0000445 struct team_mode_item *mitem;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000446
447 if (!is_good_mode_name(mode->kind) ||
448 mode->priv_size > TEAM_MODE_PRIV_SIZE)
449 return -EINVAL;
Jiri Pirko04027882012-06-19 05:54:03 +0000450
451 mitem = kmalloc(sizeof(*mitem), GFP_KERNEL);
452 if (!mitem)
453 return -ENOMEM;
454
Jiri Pirko3d249d42011-11-11 22:16:48 +0000455 spin_lock(&mode_list_lock);
456 if (__find_mode(mode->kind)) {
457 err = -EEXIST;
Jiri Pirko04027882012-06-19 05:54:03 +0000458 kfree(mitem);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000459 goto unlock;
460 }
Jiri Pirko04027882012-06-19 05:54:03 +0000461 mitem->mode = mode;
462 list_add_tail(&mitem->list, &mode_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000463unlock:
464 spin_unlock(&mode_list_lock);
465 return err;
466}
467EXPORT_SYMBOL(team_mode_register);
468
Jiri Pirko04027882012-06-19 05:54:03 +0000469void team_mode_unregister(const struct team_mode *mode)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000470{
Jiri Pirko04027882012-06-19 05:54:03 +0000471 struct team_mode_item *mitem;
472
Jiri Pirko3d249d42011-11-11 22:16:48 +0000473 spin_lock(&mode_list_lock);
Jiri Pirko04027882012-06-19 05:54:03 +0000474 mitem = __find_mode(mode->kind);
475 if (mitem) {
476 list_del_init(&mitem->list);
477 kfree(mitem);
478 }
Jiri Pirko3d249d42011-11-11 22:16:48 +0000479 spin_unlock(&mode_list_lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000480}
481EXPORT_SYMBOL(team_mode_unregister);
482
Jiri Pirko04027882012-06-19 05:54:03 +0000483static const struct team_mode *team_mode_get(const char *kind)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000484{
Jiri Pirko04027882012-06-19 05:54:03 +0000485 struct team_mode_item *mitem;
486 const struct team_mode *mode = NULL;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000487
488 spin_lock(&mode_list_lock);
Jiri Pirko04027882012-06-19 05:54:03 +0000489 mitem = __find_mode(kind);
490 if (!mitem) {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000491 spin_unlock(&mode_list_lock);
492 request_module("team-mode-%s", kind);
493 spin_lock(&mode_list_lock);
Jiri Pirko04027882012-06-19 05:54:03 +0000494 mitem = __find_mode(kind);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000495 }
Jiri Pirko04027882012-06-19 05:54:03 +0000496 if (mitem) {
497 mode = mitem->mode;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000498 if (!try_module_get(mode->owner))
499 mode = NULL;
Jiri Pirko04027882012-06-19 05:54:03 +0000500 }
Jiri Pirko3d249d42011-11-11 22:16:48 +0000501
502 spin_unlock(&mode_list_lock);
503 return mode;
504}
505
506static void team_mode_put(const struct team_mode *mode)
507{
508 module_put(mode->owner);
509}
510
511static bool team_dummy_transmit(struct team *team, struct sk_buff *skb)
512{
513 dev_kfree_skb_any(skb);
514 return false;
515}
516
517rx_handler_result_t team_dummy_receive(struct team *team,
518 struct team_port *port,
519 struct sk_buff *skb)
520{
521 return RX_HANDLER_ANOTHER;
522}
523
Jiri Pirkod299cd52012-06-19 05:54:04 +0000524static const struct team_mode __team_no_mode = {
525 .kind = "*NOMODE*",
526};
527
528static bool team_is_mode_set(struct team *team)
529{
530 return team->mode != &__team_no_mode;
531}
532
533static void team_set_no_mode(struct team *team)
534{
535 team->mode = &__team_no_mode;
536}
537
Jiri Pirko3d249d42011-11-11 22:16:48 +0000538static void team_adjust_ops(struct team *team)
539{
540 /*
541 * To avoid checks in rx/tx skb paths, ensure here that non-null and
542 * correct ops are always set.
543 */
544
545 if (list_empty(&team->port_list) ||
Jiri Pirkod299cd52012-06-19 05:54:04 +0000546 !team_is_mode_set(team) || !team->mode->ops->transmit)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000547 team->ops.transmit = team_dummy_transmit;
548 else
549 team->ops.transmit = team->mode->ops->transmit;
550
551 if (list_empty(&team->port_list) ||
Jiri Pirkod299cd52012-06-19 05:54:04 +0000552 !team_is_mode_set(team) || !team->mode->ops->receive)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000553 team->ops.receive = team_dummy_receive;
554 else
555 team->ops.receive = team->mode->ops->receive;
556}
557
558/*
559 * We can benefit from the fact that it's ensured no port is present
560 * at the time of mode change. Therefore no packets are in fly so there's no
561 * need to set mode operations in any special way.
562 */
563static int __team_change_mode(struct team *team,
564 const struct team_mode *new_mode)
565{
566 /* Check if mode was previously set and do cleanup if so */
Jiri Pirkod299cd52012-06-19 05:54:04 +0000567 if (team_is_mode_set(team)) {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000568 void (*exit_op)(struct team *team) = team->ops.exit;
569
570 /* Clear ops area so no callback is called any longer */
571 memset(&team->ops, 0, sizeof(struct team_mode_ops));
572 team_adjust_ops(team);
573
574 if (exit_op)
575 exit_op(team);
576 team_mode_put(team->mode);
Jiri Pirkod299cd52012-06-19 05:54:04 +0000577 team_set_no_mode(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000578 /* zero private data area */
579 memset(&team->mode_priv, 0,
580 sizeof(struct team) - offsetof(struct team, mode_priv));
581 }
582
583 if (!new_mode)
584 return 0;
585
586 if (new_mode->ops->init) {
587 int err;
588
589 err = new_mode->ops->init(team);
590 if (err)
591 return err;
592 }
593
594 team->mode = new_mode;
595 memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops));
596 team_adjust_ops(team);
597
598 return 0;
599}
600
601static int team_change_mode(struct team *team, const char *kind)
602{
Jiri Pirko04027882012-06-19 05:54:03 +0000603 const struct team_mode *new_mode;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000604 struct net_device *dev = team->dev;
605 int err;
606
607 if (!list_empty(&team->port_list)) {
608 netdev_err(dev, "No ports can be present during mode change\n");
609 return -EBUSY;
610 }
611
Jiri Pirkod299cd52012-06-19 05:54:04 +0000612 if (team_is_mode_set(team) && strcmp(team->mode->kind, kind) == 0) {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000613 netdev_err(dev, "Unable to change to the same mode the team is in\n");
614 return -EINVAL;
615 }
616
617 new_mode = team_mode_get(kind);
618 if (!new_mode) {
619 netdev_err(dev, "Mode \"%s\" not found\n", kind);
620 return -EINVAL;
621 }
622
623 err = __team_change_mode(team, new_mode);
624 if (err) {
625 netdev_err(dev, "Failed to change to mode \"%s\"\n", kind);
626 team_mode_put(new_mode);
627 return err;
628 }
629
630 netdev_info(dev, "Mode changed to \"%s\"\n", kind);
631 return 0;
632}
633
634
635/************************
636 * Rx path frame handler
637 ************************/
638
Jiri Pirko19a0b582012-04-20 04:42:05 +0000639static bool team_port_enabled(struct team_port *port);
640
Jiri Pirko3d249d42011-11-11 22:16:48 +0000641/* note: already called with rcu_read_lock */
642static rx_handler_result_t team_handle_frame(struct sk_buff **pskb)
643{
644 struct sk_buff *skb = *pskb;
645 struct team_port *port;
646 struct team *team;
647 rx_handler_result_t res;
648
649 skb = skb_share_check(skb, GFP_ATOMIC);
650 if (!skb)
651 return RX_HANDLER_CONSUMED;
652
653 *pskb = skb;
654
655 port = team_port_get_rcu(skb->dev);
656 team = port->team;
Jiri Pirko19a0b582012-04-20 04:42:05 +0000657 if (!team_port_enabled(port)) {
658 /* allow exact match delivery for disabled ports */
659 res = RX_HANDLER_EXACT;
660 } else {
661 res = team->ops.receive(team, port, skb);
662 }
Jiri Pirko3d249d42011-11-11 22:16:48 +0000663 if (res == RX_HANDLER_ANOTHER) {
664 struct team_pcpu_stats *pcpu_stats;
665
666 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
667 u64_stats_update_begin(&pcpu_stats->syncp);
668 pcpu_stats->rx_packets++;
669 pcpu_stats->rx_bytes += skb->len;
670 if (skb->pkt_type == PACKET_MULTICAST)
671 pcpu_stats->rx_multicast++;
672 u64_stats_update_end(&pcpu_stats->syncp);
673
674 skb->dev = team->dev;
675 } else {
676 this_cpu_inc(team->pcpu_stats->rx_dropped);
677 }
678
679 return res;
680}
681
682
683/****************
684 * Port handling
685 ****************/
686
687static bool team_port_find(const struct team *team,
688 const struct team_port *port)
689{
690 struct team_port *cur;
691
692 list_for_each_entry(cur, &team->port_list, list)
693 if (cur == port)
694 return true;
695 return false;
696}
697
Jiri Pirko19a0b582012-04-20 04:42:05 +0000698static bool team_port_enabled(struct team_port *port)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000699{
Jiri Pirko19a0b582012-04-20 04:42:05 +0000700 return port->index != -1;
701}
702
703/*
704 * Enable/disable port by adding to enabled port hashlist and setting
705 * port->index (Might be racy so reader could see incorrect ifindex when
706 * processing a flying packet, but that is not a problem). Write guarded
707 * by team->lock.
708 */
709static void team_port_enable(struct team *team,
710 struct team_port *port)
711{
712 if (team_port_enabled(port))
713 return;
714 port->index = team->en_port_count++;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000715 hlist_add_head_rcu(&port->hlist,
716 team_port_index_hash(team, port->index));
Jiri Pirko4bccfd12012-06-19 05:54:16 +0000717 if (team->ops.port_enabled)
718 team->ops.port_enabled(team, port);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000719}
720
721static void __reconstruct_port_hlist(struct team *team, int rm_index)
722{
723 int i;
724 struct team_port *port;
725
Jiri Pirko19a0b582012-04-20 04:42:05 +0000726 for (i = rm_index + 1; i < team->en_port_count; i++) {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000727 port = team_get_port_by_index(team, i);
728 hlist_del_rcu(&port->hlist);
729 port->index--;
730 hlist_add_head_rcu(&port->hlist,
731 team_port_index_hash(team, port->index));
732 }
733}
734
Jiri Pirko19a0b582012-04-20 04:42:05 +0000735static void team_port_disable(struct team *team,
736 struct team_port *port)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000737{
738 int rm_index = port->index;
739
Jiri Pirko19a0b582012-04-20 04:42:05 +0000740 if (!team_port_enabled(port))
741 return;
Jiri Pirko4bccfd12012-06-19 05:54:16 +0000742 if (team->ops.port_disabled)
743 team->ops.port_disabled(team, port);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000744 hlist_del_rcu(&port->hlist);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000745 __reconstruct_port_hlist(team, rm_index);
Jiri Pirko19a0b582012-04-20 04:42:05 +0000746 team->en_port_count--;
747 port->index = -1;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000748}
749
750#define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \
751 NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
752 NETIF_F_HIGHDMA | NETIF_F_LRO)
753
754static void __team_compute_features(struct team *team)
755{
756 struct team_port *port;
757 u32 vlan_features = TEAM_VLAN_FEATURES;
758 unsigned short max_hard_header_len = ETH_HLEN;
759
760 list_for_each_entry(port, &team->port_list, list) {
761 vlan_features = netdev_increment_features(vlan_features,
762 port->dev->vlan_features,
763 TEAM_VLAN_FEATURES);
764
765 if (port->dev->hard_header_len > max_hard_header_len)
766 max_hard_header_len = port->dev->hard_header_len;
767 }
768
769 team->dev->vlan_features = vlan_features;
770 team->dev->hard_header_len = max_hard_header_len;
771
772 netdev_change_features(team->dev);
773}
774
775static void team_compute_features(struct team *team)
776{
Jiri Pirko61dc3462011-11-16 11:09:08 +0000777 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000778 __team_compute_features(team);
Jiri Pirko61dc3462011-11-16 11:09:08 +0000779 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000780}
781
782static int team_port_enter(struct team *team, struct team_port *port)
783{
784 int err = 0;
785
786 dev_hold(team->dev);
787 port->dev->priv_flags |= IFF_TEAM_PORT;
788 if (team->ops.port_enter) {
789 err = team->ops.port_enter(team, port);
790 if (err) {
791 netdev_err(team->dev, "Device %s failed to enter team mode\n",
792 port->dev->name);
793 goto err_port_enter;
794 }
795 }
796
797 return 0;
798
799err_port_enter:
800 port->dev->priv_flags &= ~IFF_TEAM_PORT;
801 dev_put(team->dev);
802
803 return err;
804}
805
806static void team_port_leave(struct team *team, struct team_port *port)
807{
808 if (team->ops.port_leave)
809 team->ops.port_leave(team, port);
810 port->dev->priv_flags &= ~IFF_TEAM_PORT;
811 dev_put(team->dev);
812}
813
814static void __team_port_change_check(struct team_port *port, bool linkup);
815
816static int team_port_add(struct team *team, struct net_device *port_dev)
817{
818 struct net_device *dev = team->dev;
819 struct team_port *port;
820 char *portname = port_dev->name;
821 int err;
822
823 if (port_dev->flags & IFF_LOOPBACK ||
824 port_dev->type != ARPHRD_ETHER) {
825 netdev_err(dev, "Device %s is of an unsupported type\n",
826 portname);
827 return -EINVAL;
828 }
829
830 if (team_port_exists(port_dev)) {
831 netdev_err(dev, "Device %s is already a port "
832 "of a team device\n", portname);
833 return -EBUSY;
834 }
835
836 if (port_dev->flags & IFF_UP) {
837 netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
838 portname);
839 return -EBUSY;
840 }
841
Jiri Pirko5149ee52012-06-19 05:54:05 +0000842 port = kzalloc(sizeof(struct team_port) + team->mode->port_priv_size,
843 GFP_KERNEL);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000844 if (!port)
845 return -ENOMEM;
846
847 port->dev = port_dev;
848 port->team = team;
849
850 port->orig.mtu = port_dev->mtu;
851 err = dev_set_mtu(port_dev, dev->mtu);
852 if (err) {
853 netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
854 goto err_set_mtu;
855 }
856
857 memcpy(port->orig.dev_addr, port_dev->dev_addr, ETH_ALEN);
858
859 err = team_port_enter(team, port);
860 if (err) {
861 netdev_err(dev, "Device %s failed to enter team mode\n",
862 portname);
863 goto err_port_enter;
864 }
865
866 err = dev_open(port_dev);
867 if (err) {
868 netdev_dbg(dev, "Device %s opening failed\n",
869 portname);
870 goto err_dev_open;
871 }
872
Jiri Pirko57459182011-12-08 04:11:20 +0000873 err = vlan_vids_add_by_dev(port_dev, dev);
874 if (err) {
875 netdev_err(dev, "Failed to add vlan ids to device %s\n",
876 portname);
877 goto err_vids_add;
878 }
879
Jiri Pirko3d249d42011-11-11 22:16:48 +0000880 err = netdev_set_master(port_dev, dev);
881 if (err) {
882 netdev_err(dev, "Device %s failed to set master\n", portname);
883 goto err_set_master;
884 }
885
886 err = netdev_rx_handler_register(port_dev, team_handle_frame,
887 port);
888 if (err) {
889 netdev_err(dev, "Device %s failed to register rx_handler\n",
890 portname);
891 goto err_handler_register;
892 }
893
Jiri Pirko80f7c662012-04-10 05:15:42 +0000894 err = team_option_port_add(team, port);
895 if (err) {
896 netdev_err(dev, "Device %s failed to add per-port options\n",
897 portname);
898 goto err_option_port_add;
899 }
900
Jiri Pirko19a0b582012-04-20 04:42:05 +0000901 port->index = -1;
902 team_port_enable(team, port);
903 list_add_tail_rcu(&port->list, &team->port_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000904 team_adjust_ops(team);
905 __team_compute_features(team);
906 __team_port_change_check(port, !!netif_carrier_ok(port_dev));
907
908 netdev_info(dev, "Port device %s added\n", portname);
909
910 return 0;
911
Jiri Pirko80f7c662012-04-10 05:15:42 +0000912err_option_port_add:
913 netdev_rx_handler_unregister(port_dev);
914
Jiri Pirko3d249d42011-11-11 22:16:48 +0000915err_handler_register:
916 netdev_set_master(port_dev, NULL);
917
918err_set_master:
Jiri Pirko57459182011-12-08 04:11:20 +0000919 vlan_vids_del_by_dev(port_dev, dev);
920
921err_vids_add:
Jiri Pirko3d249d42011-11-11 22:16:48 +0000922 dev_close(port_dev);
923
924err_dev_open:
925 team_port_leave(team, port);
926 team_port_set_orig_mac(port);
927
928err_port_enter:
929 dev_set_mtu(port_dev, port->orig.mtu);
930
931err_set_mtu:
932 kfree(port);
933
934 return err;
935}
936
937static int team_port_del(struct team *team, struct net_device *port_dev)
938{
939 struct net_device *dev = team->dev;
940 struct team_port *port;
941 char *portname = port_dev->name;
942
943 port = team_port_get_rtnl(port_dev);
944 if (!port || !team_port_find(team, port)) {
945 netdev_err(dev, "Device %s does not act as a port of this team\n",
946 portname);
947 return -ENOENT;
948 }
949
Jiri Pirkob82b9182012-01-24 05:16:00 +0000950 port->removed = true;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000951 __team_port_change_check(port, false);
Jiri Pirko19a0b582012-04-20 04:42:05 +0000952 team_port_disable(team, port);
953 list_del_rcu(&port->list);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000954 team_adjust_ops(team);
Jiri Pirko80f7c662012-04-10 05:15:42 +0000955 team_option_port_del(team, port);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000956 netdev_rx_handler_unregister(port_dev);
957 netdev_set_master(port_dev, NULL);
Jiri Pirko57459182011-12-08 04:11:20 +0000958 vlan_vids_del_by_dev(port_dev, dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000959 dev_close(port_dev);
960 team_port_leave(team, port);
961 team_port_set_orig_mac(port);
962 dev_set_mtu(port_dev, port->orig.mtu);
963 synchronize_rcu();
964 kfree(port);
965 netdev_info(dev, "Port device %s removed\n", portname);
966 __team_compute_features(team);
967
968 return 0;
969}
970
971
972/*****************
973 * Net device ops
974 *****************/
975
Jiri Pirko80f7c662012-04-10 05:15:42 +0000976static int team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000977{
Jiri Pirkod299cd52012-06-19 05:54:04 +0000978 ctx->data.str_val = team->mode->kind;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000979 return 0;
980}
981
Jiri Pirko80f7c662012-04-10 05:15:42 +0000982static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000983{
Jiri Pirko80f7c662012-04-10 05:15:42 +0000984 return team_change_mode(team, ctx->data.str_val);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000985}
986
Jiri Pirkoacd69962012-04-20 04:42:06 +0000987static int team_port_en_option_get(struct team *team,
988 struct team_gsetter_ctx *ctx)
989{
Jiri Pirko85d59a82012-06-19 05:54:10 +0000990 struct team_port *port = ctx->info->port;
991
992 ctx->data.bool_val = team_port_enabled(port);
Jiri Pirkoacd69962012-04-20 04:42:06 +0000993 return 0;
994}
995
996static int team_port_en_option_set(struct team *team,
997 struct team_gsetter_ctx *ctx)
998{
Jiri Pirko85d59a82012-06-19 05:54:10 +0000999 struct team_port *port = ctx->info->port;
1000
Jiri Pirkoacd69962012-04-20 04:42:06 +00001001 if (ctx->data.bool_val)
Jiri Pirko85d59a82012-06-19 05:54:10 +00001002 team_port_enable(team, port);
Jiri Pirkoacd69962012-04-20 04:42:06 +00001003 else
Jiri Pirko85d59a82012-06-19 05:54:10 +00001004 team_port_disable(team, port);
Jiri Pirkoacd69962012-04-20 04:42:06 +00001005 return 0;
1006}
1007
Jiri Pirko71472ec2012-04-10 05:15:44 +00001008static int team_user_linkup_option_get(struct team *team,
1009 struct team_gsetter_ctx *ctx)
1010{
Jiri Pirko85d59a82012-06-19 05:54:10 +00001011 struct team_port *port = ctx->info->port;
1012
1013 ctx->data.bool_val = port->user.linkup;
Jiri Pirko71472ec2012-04-10 05:15:44 +00001014 return 0;
1015}
1016
1017static int team_user_linkup_option_set(struct team *team,
1018 struct team_gsetter_ctx *ctx)
1019{
Jiri Pirko85d59a82012-06-19 05:54:10 +00001020 struct team_port *port = ctx->info->port;
1021
1022 port->user.linkup = ctx->data.bool_val;
1023 team_refresh_port_linkup(port);
Jiri Pirko71472ec2012-04-10 05:15:44 +00001024 return 0;
1025}
1026
1027static int team_user_linkup_en_option_get(struct team *team,
1028 struct team_gsetter_ctx *ctx)
1029{
Jiri Pirko85d59a82012-06-19 05:54:10 +00001030 struct team_port *port = ctx->info->port;
Jiri Pirko71472ec2012-04-10 05:15:44 +00001031
1032 ctx->data.bool_val = port->user.linkup_enabled;
1033 return 0;
1034}
1035
1036static int team_user_linkup_en_option_set(struct team *team,
1037 struct team_gsetter_ctx *ctx)
1038{
Jiri Pirko85d59a82012-06-19 05:54:10 +00001039 struct team_port *port = ctx->info->port;
Jiri Pirko71472ec2012-04-10 05:15:44 +00001040
1041 port->user.linkup_enabled = ctx->data.bool_val;
Jiri Pirko85d59a82012-06-19 05:54:10 +00001042 team_refresh_port_linkup(port);
Jiri Pirko71472ec2012-04-10 05:15:44 +00001043 return 0;
1044}
1045
Jiri Pirko358b8382011-11-16 11:09:09 +00001046static const struct team_option team_options[] = {
Jiri Pirko3d249d42011-11-11 22:16:48 +00001047 {
1048 .name = "mode",
1049 .type = TEAM_OPTION_TYPE_STRING,
1050 .getter = team_mode_option_get,
1051 .setter = team_mode_option_set,
1052 },
Jiri Pirko71472ec2012-04-10 05:15:44 +00001053 {
Jiri Pirkoacd69962012-04-20 04:42:06 +00001054 .name = "enabled",
1055 .type = TEAM_OPTION_TYPE_BOOL,
1056 .per_port = true,
1057 .getter = team_port_en_option_get,
1058 .setter = team_port_en_option_set,
1059 },
1060 {
Jiri Pirko71472ec2012-04-10 05:15:44 +00001061 .name = "user_linkup",
1062 .type = TEAM_OPTION_TYPE_BOOL,
1063 .per_port = true,
1064 .getter = team_user_linkup_option_get,
1065 .setter = team_user_linkup_option_set,
1066 },
1067 {
1068 .name = "user_linkup_enabled",
1069 .type = TEAM_OPTION_TYPE_BOOL,
1070 .per_port = true,
1071 .getter = team_user_linkup_en_option_get,
1072 .setter = team_user_linkup_en_option_set,
1073 },
Jiri Pirko3d249d42011-11-11 22:16:48 +00001074};
1075
1076static int team_init(struct net_device *dev)
1077{
1078 struct team *team = netdev_priv(dev);
1079 int i;
Jiri Pirko358b8382011-11-16 11:09:09 +00001080 int err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001081
1082 team->dev = dev;
Jiri Pirko61dc3462011-11-16 11:09:08 +00001083 mutex_init(&team->lock);
Jiri Pirkod299cd52012-06-19 05:54:04 +00001084 team_set_no_mode(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001085
1086 team->pcpu_stats = alloc_percpu(struct team_pcpu_stats);
1087 if (!team->pcpu_stats)
1088 return -ENOMEM;
1089
1090 for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
Jiri Pirko19a0b582012-04-20 04:42:05 +00001091 INIT_HLIST_HEAD(&team->en_port_hlist[i]);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001092 INIT_LIST_HEAD(&team->port_list);
1093
1094 team_adjust_ops(team);
1095
1096 INIT_LIST_HEAD(&team->option_list);
Jiri Pirko80f7c662012-04-10 05:15:42 +00001097 INIT_LIST_HEAD(&team->option_inst_list);
Jiri Pirko358b8382011-11-16 11:09:09 +00001098 err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
1099 if (err)
1100 goto err_options_register;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001101 netif_carrier_off(dev);
1102
1103 return 0;
Jiri Pirko358b8382011-11-16 11:09:09 +00001104
1105err_options_register:
1106 free_percpu(team->pcpu_stats);
1107
1108 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001109}
1110
1111static void team_uninit(struct net_device *dev)
1112{
1113 struct team *team = netdev_priv(dev);
1114 struct team_port *port;
1115 struct team_port *tmp;
1116
Jiri Pirko61dc3462011-11-16 11:09:08 +00001117 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001118 list_for_each_entry_safe(port, tmp, &team->port_list, list)
1119 team_port_del(team, port->dev);
1120
1121 __team_change_mode(team, NULL); /* cleanup */
1122 __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
Jiri Pirko61dc3462011-11-16 11:09:08 +00001123 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001124}
1125
1126static void team_destructor(struct net_device *dev)
1127{
1128 struct team *team = netdev_priv(dev);
1129
1130 free_percpu(team->pcpu_stats);
1131 free_netdev(dev);
1132}
1133
1134static int team_open(struct net_device *dev)
1135{
1136 netif_carrier_on(dev);
1137 return 0;
1138}
1139
1140static int team_close(struct net_device *dev)
1141{
1142 netif_carrier_off(dev);
1143 return 0;
1144}
1145
1146/*
1147 * note: already called with rcu_read_lock
1148 */
1149static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
1150{
1151 struct team *team = netdev_priv(dev);
1152 bool tx_success = false;
1153 unsigned int len = skb->len;
1154
1155 tx_success = team->ops.transmit(team, skb);
1156 if (tx_success) {
1157 struct team_pcpu_stats *pcpu_stats;
1158
1159 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
1160 u64_stats_update_begin(&pcpu_stats->syncp);
1161 pcpu_stats->tx_packets++;
1162 pcpu_stats->tx_bytes += len;
1163 u64_stats_update_end(&pcpu_stats->syncp);
1164 } else {
1165 this_cpu_inc(team->pcpu_stats->tx_dropped);
1166 }
1167
1168 return NETDEV_TX_OK;
1169}
1170
1171static void team_change_rx_flags(struct net_device *dev, int change)
1172{
1173 struct team *team = netdev_priv(dev);
1174 struct team_port *port;
1175 int inc;
1176
1177 rcu_read_lock();
1178 list_for_each_entry_rcu(port, &team->port_list, list) {
1179 if (change & IFF_PROMISC) {
1180 inc = dev->flags & IFF_PROMISC ? 1 : -1;
1181 dev_set_promiscuity(port->dev, inc);
1182 }
1183 if (change & IFF_ALLMULTI) {
1184 inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
1185 dev_set_allmulti(port->dev, inc);
1186 }
1187 }
1188 rcu_read_unlock();
1189}
1190
1191static void team_set_rx_mode(struct net_device *dev)
1192{
1193 struct team *team = netdev_priv(dev);
1194 struct team_port *port;
1195
1196 rcu_read_lock();
1197 list_for_each_entry_rcu(port, &team->port_list, list) {
1198 dev_uc_sync(port->dev, dev);
1199 dev_mc_sync(port->dev, dev);
1200 }
1201 rcu_read_unlock();
1202}
1203
1204static int team_set_mac_address(struct net_device *dev, void *p)
1205{
1206 struct team *team = netdev_priv(dev);
1207 struct team_port *port;
1208 struct sockaddr *addr = p;
1209
Danny Kukawka7ce5d222012-02-15 06:45:40 +00001210 dev->addr_assign_type &= ~NET_ADDR_RANDOM;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001211 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
1212 rcu_read_lock();
1213 list_for_each_entry_rcu(port, &team->port_list, list)
1214 if (team->ops.port_change_mac)
1215 team->ops.port_change_mac(team, port);
1216 rcu_read_unlock();
1217 return 0;
1218}
1219
1220static int team_change_mtu(struct net_device *dev, int new_mtu)
1221{
1222 struct team *team = netdev_priv(dev);
1223 struct team_port *port;
1224 int err;
1225
1226 /*
1227 * Alhough this is reader, it's guarded by team lock. It's not possible
1228 * to traverse list in reverse under rcu_read_lock
1229 */
Jiri Pirko61dc3462011-11-16 11:09:08 +00001230 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001231 list_for_each_entry(port, &team->port_list, list) {
1232 err = dev_set_mtu(port->dev, new_mtu);
1233 if (err) {
1234 netdev_err(dev, "Device %s failed to change mtu",
1235 port->dev->name);
1236 goto unwind;
1237 }
1238 }
Jiri Pirko61dc3462011-11-16 11:09:08 +00001239 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001240
1241 dev->mtu = new_mtu;
1242
1243 return 0;
1244
1245unwind:
1246 list_for_each_entry_continue_reverse(port, &team->port_list, list)
1247 dev_set_mtu(port->dev, dev->mtu);
Jiri Pirko61dc3462011-11-16 11:09:08 +00001248 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001249
1250 return err;
1251}
1252
1253static struct rtnl_link_stats64 *
1254team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1255{
1256 struct team *team = netdev_priv(dev);
1257 struct team_pcpu_stats *p;
1258 u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
1259 u32 rx_dropped = 0, tx_dropped = 0;
1260 unsigned int start;
1261 int i;
1262
1263 for_each_possible_cpu(i) {
1264 p = per_cpu_ptr(team->pcpu_stats, i);
1265 do {
1266 start = u64_stats_fetch_begin_bh(&p->syncp);
1267 rx_packets = p->rx_packets;
1268 rx_bytes = p->rx_bytes;
1269 rx_multicast = p->rx_multicast;
1270 tx_packets = p->tx_packets;
1271 tx_bytes = p->tx_bytes;
1272 } while (u64_stats_fetch_retry_bh(&p->syncp, start));
1273
1274 stats->rx_packets += rx_packets;
1275 stats->rx_bytes += rx_bytes;
1276 stats->multicast += rx_multicast;
1277 stats->tx_packets += tx_packets;
1278 stats->tx_bytes += tx_bytes;
1279 /*
1280 * rx_dropped & tx_dropped are u32, updated
1281 * without syncp protection.
1282 */
1283 rx_dropped += p->rx_dropped;
1284 tx_dropped += p->tx_dropped;
1285 }
1286 stats->rx_dropped = rx_dropped;
1287 stats->tx_dropped = tx_dropped;
1288 return stats;
1289}
1290
Jiri Pirko8e586132011-12-08 19:52:37 -05001291static int team_vlan_rx_add_vid(struct net_device *dev, uint16_t vid)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001292{
1293 struct team *team = netdev_priv(dev);
1294 struct team_port *port;
Jiri Pirko87002b02011-12-08 04:11:17 +00001295 int err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001296
Jiri Pirko87002b02011-12-08 04:11:17 +00001297 /*
1298 * Alhough this is reader, it's guarded by team lock. It's not possible
1299 * to traverse list in reverse under rcu_read_lock
1300 */
1301 mutex_lock(&team->lock);
1302 list_for_each_entry(port, &team->port_list, list) {
1303 err = vlan_vid_add(port->dev, vid);
1304 if (err)
1305 goto unwind;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001306 }
Jiri Pirko87002b02011-12-08 04:11:17 +00001307 mutex_unlock(&team->lock);
Jiri Pirko8e586132011-12-08 19:52:37 -05001308
1309 return 0;
Jiri Pirko87002b02011-12-08 04:11:17 +00001310
1311unwind:
1312 list_for_each_entry_continue_reverse(port, &team->port_list, list)
1313 vlan_vid_del(port->dev, vid);
1314 mutex_unlock(&team->lock);
1315
1316 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001317}
1318
Jiri Pirko8e586132011-12-08 19:52:37 -05001319static int team_vlan_rx_kill_vid(struct net_device *dev, uint16_t vid)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001320{
1321 struct team *team = netdev_priv(dev);
1322 struct team_port *port;
1323
1324 rcu_read_lock();
Jiri Pirko87002b02011-12-08 04:11:17 +00001325 list_for_each_entry_rcu(port, &team->port_list, list)
1326 vlan_vid_del(port->dev, vid);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001327 rcu_read_unlock();
Jiri Pirko8e586132011-12-08 19:52:37 -05001328
1329 return 0;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001330}
1331
1332static int team_add_slave(struct net_device *dev, struct net_device *port_dev)
1333{
1334 struct team *team = netdev_priv(dev);
1335 int err;
1336
Jiri Pirko61dc3462011-11-16 11:09:08 +00001337 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001338 err = team_port_add(team, port_dev);
Jiri Pirko61dc3462011-11-16 11:09:08 +00001339 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001340 return err;
1341}
1342
1343static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
1344{
1345 struct team *team = netdev_priv(dev);
1346 int err;
1347
Jiri Pirko61dc3462011-11-16 11:09:08 +00001348 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001349 err = team_port_del(team, port_dev);
Jiri Pirko61dc3462011-11-16 11:09:08 +00001350 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001351 return err;
1352}
1353
Jiri Pirko234a8fd2011-11-17 04:16:04 +00001354static netdev_features_t team_fix_features(struct net_device *dev,
1355 netdev_features_t features)
1356{
1357 struct team_port *port;
1358 struct team *team = netdev_priv(dev);
1359 netdev_features_t mask;
1360
1361 mask = features;
1362 features &= ~NETIF_F_ONE_FOR_ALL;
1363 features |= NETIF_F_ALL_FOR_ALL;
1364
1365 rcu_read_lock();
1366 list_for_each_entry_rcu(port, &team->port_list, list) {
1367 features = netdev_increment_features(features,
1368 port->dev->features,
1369 mask);
1370 }
1371 rcu_read_unlock();
1372 return features;
1373}
1374
Jiri Pirko3d249d42011-11-11 22:16:48 +00001375static const struct net_device_ops team_netdev_ops = {
1376 .ndo_init = team_init,
1377 .ndo_uninit = team_uninit,
1378 .ndo_open = team_open,
1379 .ndo_stop = team_close,
1380 .ndo_start_xmit = team_xmit,
1381 .ndo_change_rx_flags = team_change_rx_flags,
1382 .ndo_set_rx_mode = team_set_rx_mode,
1383 .ndo_set_mac_address = team_set_mac_address,
1384 .ndo_change_mtu = team_change_mtu,
1385 .ndo_get_stats64 = team_get_stats64,
1386 .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid,
1387 .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid,
1388 .ndo_add_slave = team_add_slave,
1389 .ndo_del_slave = team_del_slave,
Jiri Pirko234a8fd2011-11-17 04:16:04 +00001390 .ndo_fix_features = team_fix_features,
Jiri Pirko3d249d42011-11-11 22:16:48 +00001391};
1392
1393
1394/***********************
1395 * rt netlink interface
1396 ***********************/
1397
1398static void team_setup(struct net_device *dev)
1399{
1400 ether_setup(dev);
1401
1402 dev->netdev_ops = &team_netdev_ops;
1403 dev->destructor = team_destructor;
1404 dev->tx_queue_len = 0;
1405 dev->flags |= IFF_MULTICAST;
1406 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
1407
1408 /*
1409 * Indicate we support unicast address filtering. That way core won't
1410 * bring us to promisc mode in case a unicast addr is added.
1411 * Let this up to underlay drivers.
1412 */
1413 dev->priv_flags |= IFF_UNICAST_FLT;
1414
1415 dev->features |= NETIF_F_LLTX;
1416 dev->features |= NETIF_F_GRO;
1417 dev->hw_features = NETIF_F_HW_VLAN_TX |
1418 NETIF_F_HW_VLAN_RX |
1419 NETIF_F_HW_VLAN_FILTER;
1420
1421 dev->features |= dev->hw_features;
1422}
1423
1424static int team_newlink(struct net *src_net, struct net_device *dev,
1425 struct nlattr *tb[], struct nlattr *data[])
1426{
1427 int err;
1428
1429 if (tb[IFLA_ADDRESS] == NULL)
Danny Kukawka7ce5d222012-02-15 06:45:40 +00001430 eth_hw_addr_random(dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001431
1432 err = register_netdevice(dev);
1433 if (err)
1434 return err;
1435
1436 return 0;
1437}
1438
1439static int team_validate(struct nlattr *tb[], struct nlattr *data[])
1440{
1441 if (tb[IFLA_ADDRESS]) {
1442 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1443 return -EINVAL;
1444 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1445 return -EADDRNOTAVAIL;
1446 }
1447 return 0;
1448}
1449
1450static struct rtnl_link_ops team_link_ops __read_mostly = {
1451 .kind = DRV_NAME,
1452 .priv_size = sizeof(struct team),
1453 .setup = team_setup,
1454 .newlink = team_newlink,
1455 .validate = team_validate,
1456};
1457
1458
1459/***********************************
1460 * Generic netlink custom interface
1461 ***********************************/
1462
1463static struct genl_family team_nl_family = {
1464 .id = GENL_ID_GENERATE,
1465 .name = TEAM_GENL_NAME,
1466 .version = TEAM_GENL_VERSION,
1467 .maxattr = TEAM_ATTR_MAX,
1468 .netnsok = true,
1469};
1470
1471static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
1472 [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, },
1473 [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 },
1474 [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED },
1475 [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED },
1476};
1477
1478static const struct nla_policy
1479team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
1480 [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
1481 [TEAM_ATTR_OPTION_NAME] = {
1482 .type = NLA_STRING,
1483 .len = TEAM_STRING_MAX_LEN,
1484 },
1485 [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG },
1486 [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 },
Jiri Pirko26155982012-04-04 12:16:26 +00001487 [TEAM_ATTR_OPTION_DATA] = { .type = NLA_BINARY },
Jiri Pirko3d249d42011-11-11 22:16:48 +00001488};
1489
1490static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
1491{
1492 struct sk_buff *msg;
1493 void *hdr;
1494 int err;
1495
1496 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1497 if (!msg)
1498 return -ENOMEM;
1499
1500 hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq,
1501 &team_nl_family, 0, TEAM_CMD_NOOP);
1502 if (IS_ERR(hdr)) {
1503 err = PTR_ERR(hdr);
1504 goto err_msg_put;
1505 }
1506
1507 genlmsg_end(msg, hdr);
1508
1509 return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid);
1510
1511err_msg_put:
1512 nlmsg_free(msg);
1513
1514 return err;
1515}
1516
1517/*
1518 * Netlink cmd functions should be locked by following two functions.
Jiri Pirko8c0713a2011-11-16 11:55:54 +00001519 * Since dev gets held here, that ensures dev won't disappear in between.
Jiri Pirko3d249d42011-11-11 22:16:48 +00001520 */
1521static struct team *team_nl_team_get(struct genl_info *info)
1522{
1523 struct net *net = genl_info_net(info);
1524 int ifindex;
1525 struct net_device *dev;
1526 struct team *team;
1527
1528 if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
1529 return NULL;
1530
1531 ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
Jiri Pirko8c0713a2011-11-16 11:55:54 +00001532 dev = dev_get_by_index(net, ifindex);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001533 if (!dev || dev->netdev_ops != &team_netdev_ops) {
Jiri Pirko8c0713a2011-11-16 11:55:54 +00001534 if (dev)
1535 dev_put(dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001536 return NULL;
1537 }
1538
1539 team = netdev_priv(dev);
Jiri Pirko61dc3462011-11-16 11:09:08 +00001540 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001541 return team;
1542}
1543
1544static void team_nl_team_put(struct team *team)
1545{
Jiri Pirko61dc3462011-11-16 11:09:08 +00001546 mutex_unlock(&team->lock);
Jiri Pirko8c0713a2011-11-16 11:55:54 +00001547 dev_put(team->dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001548}
1549
1550static int team_nl_send_generic(struct genl_info *info, struct team *team,
1551 int (*fill_func)(struct sk_buff *skb,
1552 struct genl_info *info,
1553 int flags, struct team *team))
1554{
1555 struct sk_buff *skb;
1556 int err;
1557
1558 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1559 if (!skb)
1560 return -ENOMEM;
1561
1562 err = fill_func(skb, info, NLM_F_ACK, team);
1563 if (err < 0)
1564 goto err_fill;
1565
1566 err = genlmsg_unicast(genl_info_net(info), skb, info->snd_pid);
1567 return err;
1568
1569err_fill:
1570 nlmsg_free(skb);
1571 return err;
1572}
1573
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001574typedef int team_nl_send_func_t(struct sk_buff *skb,
1575 struct team *team, u32 pid);
1576
1577static int team_nl_send_unicast(struct sk_buff *skb, struct team *team, u32 pid)
1578{
1579 return genlmsg_unicast(dev_net(team->dev), skb, pid);
1580}
1581
Jiri Pirko01048d92012-06-19 05:54:14 +00001582static int team_nl_fill_one_option_get(struct sk_buff *skb, struct team *team,
1583 struct team_option_inst *opt_inst)
1584{
1585 struct nlattr *option_item;
1586 struct team_option *option = opt_inst->option;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001587 struct team_option_inst_info *opt_inst_info = &opt_inst->info;
Jiri Pirko01048d92012-06-19 05:54:14 +00001588 struct team_gsetter_ctx ctx;
1589 int err;
1590
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001591 ctx.info = opt_inst_info;
1592 err = team_option_get(team, opt_inst, &ctx);
1593 if (err)
1594 return err;
1595
Jiri Pirko01048d92012-06-19 05:54:14 +00001596 option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
1597 if (!option_item)
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001598 return -EMSGSIZE;
Jiri Pirko01048d92012-06-19 05:54:14 +00001599
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001600 if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name))
1601 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00001602 if (opt_inst_info->port &&
1603 nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX,
1604 opt_inst_info->port->dev->ifindex))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001605 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00001606 if (opt_inst->option->array_size &&
1607 nla_put_u32(skb, TEAM_ATTR_OPTION_ARRAY_INDEX,
1608 opt_inst_info->array_index))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001609 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00001610
1611 switch (option->type) {
1612 case TEAM_OPTION_TYPE_U32:
1613 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001614 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00001615 if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.u32_val))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001616 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00001617 break;
1618 case TEAM_OPTION_TYPE_STRING:
1619 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001620 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00001621 if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA,
1622 ctx.data.str_val))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001623 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00001624 break;
1625 case TEAM_OPTION_TYPE_BINARY:
1626 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001627 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00001628 if (nla_put(skb, TEAM_ATTR_OPTION_DATA, ctx.data.bin_val.len,
1629 ctx.data.bin_val.ptr))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001630 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00001631 break;
1632 case TEAM_OPTION_TYPE_BOOL:
1633 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001634 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00001635 if (ctx.data.bool_val &&
1636 nla_put_flag(skb, TEAM_ATTR_OPTION_DATA))
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001637 goto nest_cancel;
Jiri Pirko01048d92012-06-19 05:54:14 +00001638 break;
1639 default:
1640 BUG();
1641 }
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001642 if (opt_inst->removed && nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED))
1643 goto nest_cancel;
1644 if (opt_inst->changed) {
1645 if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED))
1646 goto nest_cancel;
1647 opt_inst->changed = false;
1648 }
Jiri Pirko01048d92012-06-19 05:54:14 +00001649 nla_nest_end(skb, option_item);
1650 return 0;
1651
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001652nest_cancel:
1653 nla_nest_cancel(skb, option_item);
1654 return -EMSGSIZE;
Jiri Pirko01048d92012-06-19 05:54:14 +00001655}
1656
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001657static int __send_and_alloc_skb(struct sk_buff **pskb,
1658 struct team *team, u32 pid,
1659 team_nl_send_func_t *send_func)
1660{
1661 int err;
1662
1663 if (*pskb) {
1664 err = send_func(*pskb, team, pid);
1665 if (err)
1666 return err;
1667 }
1668 *pskb = genlmsg_new(NLMSG_DEFAULT_SIZE - GENL_HDRLEN, GFP_KERNEL);
1669 if (!*pskb)
1670 return -ENOMEM;
1671 return 0;
1672}
1673
1674static int team_nl_send_options_get(struct team *team, u32 pid, u32 seq,
1675 int flags, team_nl_send_func_t *send_func,
Jiri Pirko01048d92012-06-19 05:54:14 +00001676 struct list_head *sel_opt_inst_list)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001677{
1678 struct nlattr *option_list;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001679 struct nlmsghdr *nlh;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001680 void *hdr;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001681 struct team_option_inst *opt_inst;
1682 int err;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001683 struct sk_buff *skb = NULL;
1684 bool incomplete;
1685 int i;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001686
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001687 opt_inst = list_first_entry(sel_opt_inst_list,
1688 struct team_option_inst, tmp_list);
1689
1690start_again:
1691 err = __send_and_alloc_skb(&skb, team, pid, send_func);
1692 if (err)
1693 return err;
1694
1695 hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags | NLM_F_MULTI,
Jiri Pirko3d249d42011-11-11 22:16:48 +00001696 TEAM_CMD_OPTIONS_GET);
1697 if (IS_ERR(hdr))
1698 return PTR_ERR(hdr);
1699
David S. Miller86ebb022012-04-01 20:25:18 -04001700 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
1701 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001702 option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
1703 if (!option_list)
Jiri Pirko75db9862012-06-19 05:54:12 +00001704 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001705
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001706 i = 0;
1707 incomplete = false;
1708 list_for_each_entry_from(opt_inst, sel_opt_inst_list, tmp_list) {
Jiri Pirko01048d92012-06-19 05:54:14 +00001709 err = team_nl_fill_one_option_get(skb, team, opt_inst);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001710 if (err) {
1711 if (err == -EMSGSIZE) {
1712 if (!i)
1713 goto errout;
1714 incomplete = true;
1715 break;
1716 }
Jiri Pirko01048d92012-06-19 05:54:14 +00001717 goto errout;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001718 }
1719 i++;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001720 }
1721
1722 nla_nest_end(skb, option_list);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001723 genlmsg_end(skb, hdr);
1724 if (incomplete)
1725 goto start_again;
1726
1727send_done:
1728 nlh = nlmsg_put(skb, pid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
1729 if (!nlh) {
1730 err = __send_and_alloc_skb(&skb, team, pid, send_func);
1731 if (err)
1732 goto errout;
1733 goto send_done;
1734 }
1735
1736 return send_func(skb, team, pid);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001737
1738nla_put_failure:
Jiri Pirko80f7c662012-04-10 05:15:42 +00001739 err = -EMSGSIZE;
1740errout:
Jiri Pirko3d249d42011-11-11 22:16:48 +00001741 genlmsg_cancel(skb, hdr);
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001742 nlmsg_free(skb);
Jiri Pirko80f7c662012-04-10 05:15:42 +00001743 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001744}
1745
Jiri Pirko3d249d42011-11-11 22:16:48 +00001746static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
1747{
1748 struct team *team;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001749 struct team_option_inst *opt_inst;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001750 int err;
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001751 LIST_HEAD(sel_opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001752
1753 team = team_nl_team_get(info);
1754 if (!team)
1755 return -EINVAL;
1756
Jiri Pirko9b00cf22012-06-19 05:54:18 +00001757 list_for_each_entry(opt_inst, &team->option_inst_list, list)
1758 list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
1759 err = team_nl_send_options_get(team, info->snd_pid, info->snd_seq,
1760 NLM_F_ACK, team_nl_send_unicast,
1761 &sel_opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001762
1763 team_nl_team_put(team);
1764
1765 return err;
1766}
1767
1768static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
1769{
1770 struct team *team;
1771 int err = 0;
1772 int i;
1773 struct nlattr *nl_option;
1774
1775 team = team_nl_team_get(info);
1776 if (!team)
1777 return -EINVAL;
1778
1779 err = -EINVAL;
1780 if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
1781 err = -EINVAL;
1782 goto team_put;
1783 }
1784
1785 nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
Jiri Pirko80f7c662012-04-10 05:15:42 +00001786 struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1];
Jiri Pirkob1303322012-06-19 05:54:08 +00001787 struct nlattr *attr;
Jiri Pirko14f066b2012-04-10 05:15:43 +00001788 struct nlattr *attr_data;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001789 enum team_option_type opt_type;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001790 int opt_port_ifindex = 0; /* != 0 for per-port options */
Jiri Pirkob1303322012-06-19 05:54:08 +00001791 u32 opt_array_index = 0;
1792 bool opt_is_array = false;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001793 struct team_option_inst *opt_inst;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001794 char *opt_name;
1795 bool opt_found = false;
1796
1797 if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
1798 err = -EINVAL;
1799 goto team_put;
1800 }
Jiri Pirko80f7c662012-04-10 05:15:42 +00001801 err = nla_parse_nested(opt_attrs, TEAM_ATTR_OPTION_MAX,
Jiri Pirko3d249d42011-11-11 22:16:48 +00001802 nl_option, team_nl_option_policy);
1803 if (err)
1804 goto team_put;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001805 if (!opt_attrs[TEAM_ATTR_OPTION_NAME] ||
Jiri Pirko14f066b2012-04-10 05:15:43 +00001806 !opt_attrs[TEAM_ATTR_OPTION_TYPE]) {
Jiri Pirko3d249d42011-11-11 22:16:48 +00001807 err = -EINVAL;
1808 goto team_put;
1809 }
Jiri Pirko80f7c662012-04-10 05:15:42 +00001810 switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) {
Jiri Pirko3d249d42011-11-11 22:16:48 +00001811 case NLA_U32:
1812 opt_type = TEAM_OPTION_TYPE_U32;
1813 break;
1814 case NLA_STRING:
1815 opt_type = TEAM_OPTION_TYPE_STRING;
1816 break;
Jiri Pirko26155982012-04-04 12:16:26 +00001817 case NLA_BINARY:
1818 opt_type = TEAM_OPTION_TYPE_BINARY;
1819 break;
Jiri Pirko14f066b2012-04-10 05:15:43 +00001820 case NLA_FLAG:
1821 opt_type = TEAM_OPTION_TYPE_BOOL;
1822 break;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001823 default:
1824 goto team_put;
1825 }
1826
Jiri Pirko14f066b2012-04-10 05:15:43 +00001827 attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA];
1828 if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) {
1829 err = -EINVAL;
1830 goto team_put;
1831 }
1832
Jiri Pirko80f7c662012-04-10 05:15:42 +00001833 opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]);
Jiri Pirkob1303322012-06-19 05:54:08 +00001834 attr = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX];
1835 if (attr)
1836 opt_port_ifindex = nla_get_u32(attr);
1837
1838 attr = opt_attrs[TEAM_ATTR_OPTION_ARRAY_INDEX];
1839 if (attr) {
1840 opt_is_array = true;
1841 opt_array_index = nla_get_u32(attr);
1842 }
Jiri Pirko3d249d42011-11-11 22:16:48 +00001843
Jiri Pirko80f7c662012-04-10 05:15:42 +00001844 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
1845 struct team_option *option = opt_inst->option;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001846 struct team_gsetter_ctx ctx;
Jiri Pirko85d59a82012-06-19 05:54:10 +00001847 struct team_option_inst_info *opt_inst_info;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001848 int tmp_ifindex;
1849
Jiri Pirko85d59a82012-06-19 05:54:10 +00001850 opt_inst_info = &opt_inst->info;
1851 tmp_ifindex = opt_inst_info->port ?
1852 opt_inst_info->port->dev->ifindex : 0;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001853 if (option->type != opt_type ||
Jiri Pirko80f7c662012-04-10 05:15:42 +00001854 strcmp(option->name, opt_name) ||
Jiri Pirkob1303322012-06-19 05:54:08 +00001855 tmp_ifindex != opt_port_ifindex ||
1856 (option->array_size && !opt_is_array) ||
Jiri Pirko85d59a82012-06-19 05:54:10 +00001857 opt_inst_info->array_index != opt_array_index)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001858 continue;
1859 opt_found = true;
Jiri Pirko85d59a82012-06-19 05:54:10 +00001860 ctx.info = opt_inst_info;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001861 switch (opt_type) {
1862 case TEAM_OPTION_TYPE_U32:
Jiri Pirko14f066b2012-04-10 05:15:43 +00001863 ctx.data.u32_val = nla_get_u32(attr_data);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001864 break;
1865 case TEAM_OPTION_TYPE_STRING:
Jiri Pirko14f066b2012-04-10 05:15:43 +00001866 if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) {
Jiri Pirko26155982012-04-04 12:16:26 +00001867 err = -EINVAL;
1868 goto team_put;
1869 }
Jiri Pirko14f066b2012-04-10 05:15:43 +00001870 ctx.data.str_val = nla_data(attr_data);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001871 break;
Jiri Pirko26155982012-04-04 12:16:26 +00001872 case TEAM_OPTION_TYPE_BINARY:
Jiri Pirko14f066b2012-04-10 05:15:43 +00001873 ctx.data.bin_val.len = nla_len(attr_data);
1874 ctx.data.bin_val.ptr = nla_data(attr_data);
1875 break;
1876 case TEAM_OPTION_TYPE_BOOL:
1877 ctx.data.bool_val = attr_data ? true : false;
Jiri Pirko26155982012-04-04 12:16:26 +00001878 break;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001879 default:
1880 BUG();
1881 }
Jiri Pirko80f7c662012-04-10 05:15:42 +00001882 err = team_option_set(team, opt_inst, &ctx);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001883 if (err)
1884 goto team_put;
1885 }
1886 if (!opt_found) {
1887 err = -ENOENT;
1888 goto team_put;
1889 }
1890 }
1891
1892team_put:
1893 team_nl_team_put(team);
1894
1895 return err;
1896}
1897
Jiri Pirkob82b9182012-01-24 05:16:00 +00001898static int team_nl_fill_port_list_get(struct sk_buff *skb,
1899 u32 pid, u32 seq, int flags,
1900 struct team *team,
1901 bool fillall)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001902{
1903 struct nlattr *port_list;
1904 void *hdr;
1905 struct team_port *port;
1906
1907 hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags,
1908 TEAM_CMD_PORT_LIST_GET);
1909 if (IS_ERR(hdr))
1910 return PTR_ERR(hdr);
1911
David S. Miller86ebb022012-04-01 20:25:18 -04001912 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
1913 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001914 port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
1915 if (!port_list)
Jiri Pirko3221c642012-06-19 05:54:13 +00001916 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001917
1918 list_for_each_entry(port, &team->port_list, list) {
1919 struct nlattr *port_item;
1920
Jiri Pirkob82b9182012-01-24 05:16:00 +00001921 /* Include only changed ports if fill all mode is not on */
1922 if (!fillall && !port->changed)
1923 continue;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001924 port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
1925 if (!port_item)
1926 goto nla_put_failure;
David S. Miller86ebb022012-04-01 20:25:18 -04001927 if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex))
1928 goto nla_put_failure;
Jiri Pirkob82b9182012-01-24 05:16:00 +00001929 if (port->changed) {
David S. Miller86ebb022012-04-01 20:25:18 -04001930 if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED))
1931 goto nla_put_failure;
Jiri Pirkob82b9182012-01-24 05:16:00 +00001932 port->changed = false;
1933 }
David S. Miller86ebb022012-04-01 20:25:18 -04001934 if ((port->removed &&
1935 nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) ||
Jiri Pirko71472ec2012-04-10 05:15:44 +00001936 (port->state.linkup &&
David S. Miller86ebb022012-04-01 20:25:18 -04001937 nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) ||
Jiri Pirko71472ec2012-04-10 05:15:44 +00001938 nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) ||
1939 nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex))
David S. Miller86ebb022012-04-01 20:25:18 -04001940 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001941 nla_nest_end(skb, port_item);
1942 }
1943
1944 nla_nest_end(skb, port_list);
1945 return genlmsg_end(skb, hdr);
1946
1947nla_put_failure:
1948 genlmsg_cancel(skb, hdr);
1949 return -EMSGSIZE;
1950}
1951
Jiri Pirkob82b9182012-01-24 05:16:00 +00001952static int team_nl_fill_port_list_get_all(struct sk_buff *skb,
1953 struct genl_info *info, int flags,
1954 struct team *team)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001955{
Jiri Pirkob82b9182012-01-24 05:16:00 +00001956 return team_nl_fill_port_list_get(skb, info->snd_pid,
1957 info->snd_seq, NLM_F_ACK,
1958 team, true);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001959}
1960
1961static int team_nl_cmd_port_list_get(struct sk_buff *skb,
1962 struct genl_info *info)
1963{
1964 struct team *team;
1965 int err;
1966
1967 team = team_nl_team_get(info);
1968 if (!team)
1969 return -EINVAL;
1970
Jiri Pirkob82b9182012-01-24 05:16:00 +00001971 err = team_nl_send_generic(info, team, team_nl_fill_port_list_get_all);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001972
1973 team_nl_team_put(team);
1974
1975 return err;
1976}
1977
1978static struct genl_ops team_nl_ops[] = {
1979 {
1980 .cmd = TEAM_CMD_NOOP,
1981 .doit = team_nl_cmd_noop,
1982 .policy = team_nl_policy,
1983 },
1984 {
1985 .cmd = TEAM_CMD_OPTIONS_SET,
1986 .doit = team_nl_cmd_options_set,
1987 .policy = team_nl_policy,
1988 .flags = GENL_ADMIN_PERM,
1989 },
1990 {
1991 .cmd = TEAM_CMD_OPTIONS_GET,
1992 .doit = team_nl_cmd_options_get,
1993 .policy = team_nl_policy,
1994 .flags = GENL_ADMIN_PERM,
1995 },
1996 {
1997 .cmd = TEAM_CMD_PORT_LIST_GET,
1998 .doit = team_nl_cmd_port_list_get,
1999 .policy = team_nl_policy,
2000 .flags = GENL_ADMIN_PERM,
2001 },
2002};
2003
2004static struct genl_multicast_group team_change_event_mcgrp = {
2005 .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME,
2006};
2007
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002008static int team_nl_send_multicast(struct sk_buff *skb,
2009 struct team *team, u32 pid)
2010{
2011 return genlmsg_multicast_netns(dev_net(team->dev), skb, 0,
2012 team_change_event_mcgrp.id, GFP_KERNEL);
2013}
2014
Jiri Pirko01048d92012-06-19 05:54:14 +00002015static int team_nl_send_event_options_get(struct team *team,
2016 struct list_head *sel_opt_inst_list)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002017{
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002018 return team_nl_send_options_get(team, 0, 0, 0, team_nl_send_multicast,
2019 sel_opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002020}
2021
Jiri Pirkob82b9182012-01-24 05:16:00 +00002022static int team_nl_send_event_port_list_get(struct team *team)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002023{
2024 struct sk_buff *skb;
2025 int err;
Jiri Pirkob82b9182012-01-24 05:16:00 +00002026 struct net *net = dev_net(team->dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002027
2028 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2029 if (!skb)
2030 return -ENOMEM;
2031
Jiri Pirkob82b9182012-01-24 05:16:00 +00002032 err = team_nl_fill_port_list_get(skb, 0, 0, 0, team, false);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002033 if (err < 0)
2034 goto err_fill;
2035
2036 err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id,
2037 GFP_KERNEL);
2038 return err;
2039
2040err_fill:
2041 nlmsg_free(skb);
2042 return err;
2043}
2044
2045static int team_nl_init(void)
2046{
2047 int err;
2048
2049 err = genl_register_family_with_ops(&team_nl_family, team_nl_ops,
2050 ARRAY_SIZE(team_nl_ops));
2051 if (err)
2052 return err;
2053
2054 err = genl_register_mc_group(&team_nl_family, &team_change_event_mcgrp);
2055 if (err)
2056 goto err_change_event_grp_reg;
2057
2058 return 0;
2059
2060err_change_event_grp_reg:
2061 genl_unregister_family(&team_nl_family);
2062
2063 return err;
2064}
2065
2066static void team_nl_fini(void)
2067{
2068 genl_unregister_family(&team_nl_family);
2069}
2070
2071
2072/******************
2073 * Change checkers
2074 ******************/
2075
Jiri Pirkob82b9182012-01-24 05:16:00 +00002076static void __team_options_change_check(struct team *team)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002077{
2078 int err;
Jiri Pirko01048d92012-06-19 05:54:14 +00002079 struct team_option_inst *opt_inst;
2080 LIST_HEAD(sel_opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002081
Jiri Pirko01048d92012-06-19 05:54:14 +00002082 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2083 if (opt_inst->changed)
2084 list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
2085 }
2086 err = team_nl_send_event_options_get(team, &sel_opt_inst_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002087 if (err)
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002088 netdev_warn(team->dev, "Failed to send options change via netlink (err %d)\n",
2089 err);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002090}
2091
Jiri Pirko01048d92012-06-19 05:54:14 +00002092static void __team_option_inst_change(struct team *team,
2093 struct team_option_inst *sel_opt_inst)
2094{
2095 int err;
2096 LIST_HEAD(sel_opt_inst_list);
2097
2098 sel_opt_inst->changed = true;
2099 list_add(&sel_opt_inst->tmp_list, &sel_opt_inst_list);
2100 err = team_nl_send_event_options_get(team, &sel_opt_inst_list);
2101 if (err)
Jiri Pirko9b00cf22012-06-19 05:54:18 +00002102 netdev_warn(team->dev, "Failed to send option change via netlink (err %d)\n",
2103 err);
Jiri Pirko01048d92012-06-19 05:54:14 +00002104}
2105
Jiri Pirko3d249d42011-11-11 22:16:48 +00002106/* rtnl lock is held */
2107static void __team_port_change_check(struct team_port *port, bool linkup)
2108{
2109 int err;
2110
Jiri Pirko71472ec2012-04-10 05:15:44 +00002111 if (!port->removed && port->state.linkup == linkup)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002112 return;
2113
Jiri Pirkob82b9182012-01-24 05:16:00 +00002114 port->changed = true;
Jiri Pirko71472ec2012-04-10 05:15:44 +00002115 port->state.linkup = linkup;
2116 team_refresh_port_linkup(port);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002117 if (linkup) {
2118 struct ethtool_cmd ecmd;
2119
2120 err = __ethtool_get_settings(port->dev, &ecmd);
2121 if (!err) {
Jiri Pirko71472ec2012-04-10 05:15:44 +00002122 port->state.speed = ethtool_cmd_speed(&ecmd);
2123 port->state.duplex = ecmd.duplex;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002124 goto send_event;
2125 }
2126 }
Jiri Pirko71472ec2012-04-10 05:15:44 +00002127 port->state.speed = 0;
2128 port->state.duplex = 0;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002129
2130send_event:
Jiri Pirkob82b9182012-01-24 05:16:00 +00002131 err = team_nl_send_event_port_list_get(port->team);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002132 if (err)
2133 netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink\n",
2134 port->dev->name);
2135
2136}
2137
2138static void team_port_change_check(struct team_port *port, bool linkup)
2139{
2140 struct team *team = port->team;
2141
Jiri Pirko61dc3462011-11-16 11:09:08 +00002142 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002143 __team_port_change_check(port, linkup);
Jiri Pirko61dc3462011-11-16 11:09:08 +00002144 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002145}
2146
Jiri Pirko0f1aad22012-06-19 05:54:11 +00002147
Jiri Pirko3d249d42011-11-11 22:16:48 +00002148/************************************
2149 * Net device notifier event handler
2150 ************************************/
2151
2152static int team_device_event(struct notifier_block *unused,
2153 unsigned long event, void *ptr)
2154{
2155 struct net_device *dev = (struct net_device *) ptr;
2156 struct team_port *port;
2157
2158 port = team_port_get_rtnl(dev);
2159 if (!port)
2160 return NOTIFY_DONE;
2161
2162 switch (event) {
2163 case NETDEV_UP:
2164 if (netif_carrier_ok(dev))
2165 team_port_change_check(port, true);
2166 case NETDEV_DOWN:
2167 team_port_change_check(port, false);
2168 case NETDEV_CHANGE:
2169 if (netif_running(port->dev))
2170 team_port_change_check(port,
2171 !!netif_carrier_ok(port->dev));
2172 break;
2173 case NETDEV_UNREGISTER:
2174 team_del_slave(port->team->dev, dev);
2175 break;
2176 case NETDEV_FEAT_CHANGE:
2177 team_compute_features(port->team);
2178 break;
2179 case NETDEV_CHANGEMTU:
2180 /* Forbid to change mtu of underlaying device */
2181 return NOTIFY_BAD;
2182 case NETDEV_PRE_TYPE_CHANGE:
2183 /* Forbid to change type of underlaying device */
2184 return NOTIFY_BAD;
2185 }
2186 return NOTIFY_DONE;
2187}
2188
2189static struct notifier_block team_notifier_block __read_mostly = {
2190 .notifier_call = team_device_event,
2191};
2192
2193
2194/***********************
2195 * Module init and exit
2196 ***********************/
2197
2198static int __init team_module_init(void)
2199{
2200 int err;
2201
2202 register_netdevice_notifier(&team_notifier_block);
2203
2204 err = rtnl_link_register(&team_link_ops);
2205 if (err)
2206 goto err_rtnl_reg;
2207
2208 err = team_nl_init();
2209 if (err)
2210 goto err_nl_init;
2211
2212 return 0;
2213
2214err_nl_init:
2215 rtnl_link_unregister(&team_link_ops);
2216
2217err_rtnl_reg:
2218 unregister_netdevice_notifier(&team_notifier_block);
2219
2220 return err;
2221}
2222
2223static void __exit team_module_exit(void)
2224{
2225 team_nl_fini();
2226 rtnl_link_unregister(&team_link_ops);
2227 unregister_netdevice_notifier(&team_notifier_block);
2228}
2229
2230module_init(team_module_init);
2231module_exit(team_module_exit);
2232
2233MODULE_LICENSE("GPL v2");
2234MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
2235MODULE_DESCRIPTION("Ethernet team device driver");
2236MODULE_ALIAS_RTNL_LINK(DRV_NAME);