Commit | Line | Data |
---|---|---|
3d249d4c JP |
1 | /* |
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> | |
21 | #include <linux/if_arp.h> | |
22 | #include <linux/socket.h> | |
23 | #include <linux/etherdevice.h> | |
24 | #include <linux/rtnetlink.h> | |
25 | #include <net/rtnetlink.h> | |
26 | #include <net/genetlink.h> | |
27 | #include <net/netlink.h> | |
28 | #include <linux/if_team.h> | |
29 | ||
30 | #define DRV_NAME "team" | |
31 | ||
32 | ||
33 | /********** | |
34 | * Helpers | |
35 | **********/ | |
36 | ||
37 | #define team_port_exists(dev) (dev->priv_flags & IFF_TEAM_PORT) | |
38 | ||
39 | static struct team_port *team_port_get_rcu(const struct net_device *dev) | |
40 | { | |
41 | struct team_port *port = rcu_dereference(dev->rx_handler_data); | |
42 | ||
43 | return team_port_exists(dev) ? port : NULL; | |
44 | } | |
45 | ||
46 | static struct team_port *team_port_get_rtnl(const struct net_device *dev) | |
47 | { | |
48 | struct team_port *port = rtnl_dereference(dev->rx_handler_data); | |
49 | ||
50 | return team_port_exists(dev) ? port : NULL; | |
51 | } | |
52 | ||
53 | /* | |
54 | * Since the ability to change mac address for open port device is tested in | |
55 | * team_port_add, this function can be called without control of return value | |
56 | */ | |
57 | static int __set_port_mac(struct net_device *port_dev, | |
58 | const unsigned char *dev_addr) | |
59 | { | |
60 | struct sockaddr addr; | |
61 | ||
62 | memcpy(addr.sa_data, dev_addr, ETH_ALEN); | |
63 | addr.sa_family = ARPHRD_ETHER; | |
64 | return dev_set_mac_address(port_dev, &addr); | |
65 | } | |
66 | ||
67 | int team_port_set_orig_mac(struct team_port *port) | |
68 | { | |
69 | return __set_port_mac(port->dev, port->orig.dev_addr); | |
70 | } | |
71 | ||
72 | int team_port_set_team_mac(struct team_port *port) | |
73 | { | |
74 | return __set_port_mac(port->dev, port->team->dev->dev_addr); | |
75 | } | |
76 | EXPORT_SYMBOL(team_port_set_team_mac); | |
77 | ||
78 | ||
79 | /******************* | |
80 | * Options handling | |
81 | *******************/ | |
82 | ||
358b8382 JP |
83 | struct team_option *__team_find_option(struct team *team, const char *opt_name) |
84 | { | |
85 | struct team_option *option; | |
86 | ||
87 | list_for_each_entry(option, &team->option_list, list) { | |
88 | if (strcmp(option->name, opt_name) == 0) | |
89 | return option; | |
90 | } | |
91 | return NULL; | |
92 | } | |
93 | ||
94 | int team_options_register(struct team *team, | |
95 | const struct team_option *option, | |
96 | size_t option_count) | |
3d249d4c JP |
97 | { |
98 | int i; | |
358b8382 JP |
99 | struct team_option *dst_opts[option_count]; |
100 | int err; | |
3d249d4c | 101 | |
358b8382 JP |
102 | memset(dst_opts, 0, sizeof(dst_opts)); |
103 | for (i = 0; i < option_count; i++, option++) { | |
104 | struct team_option *dst_opt; | |
105 | ||
106 | if (__team_find_option(team, option->name)) { | |
107 | err = -EEXIST; | |
108 | goto rollback; | |
109 | } | |
110 | dst_opt = kmalloc(sizeof(*option), GFP_KERNEL); | |
111 | if (!dst_opt) { | |
112 | err = -ENOMEM; | |
113 | goto rollback; | |
114 | } | |
115 | memcpy(dst_opt, option, sizeof(*option)); | |
116 | dst_opts[i] = dst_opt; | |
117 | } | |
118 | ||
119 | for (i = 0; i < option_count; i++) | |
120 | list_add_tail(&dst_opts[i]->list, &team->option_list); | |
121 | ||
122 | return 0; | |
123 | ||
124 | rollback: | |
125 | for (i = 0; i < option_count; i++) | |
126 | kfree(dst_opts[i]); | |
127 | ||
128 | return err; | |
3d249d4c | 129 | } |
358b8382 | 130 | |
3d249d4c JP |
131 | EXPORT_SYMBOL(team_options_register); |
132 | ||
133 | static void __team_options_change_check(struct team *team, | |
134 | struct team_option *changed_option); | |
135 | ||
136 | static void __team_options_unregister(struct team *team, | |
358b8382 | 137 | const struct team_option *option, |
3d249d4c JP |
138 | size_t option_count) |
139 | { | |
140 | int i; | |
141 | ||
358b8382 JP |
142 | for (i = 0; i < option_count; i++, option++) { |
143 | struct team_option *del_opt; | |
144 | ||
145 | del_opt = __team_find_option(team, option->name); | |
146 | if (del_opt) { | |
147 | list_del(&del_opt->list); | |
148 | kfree(del_opt); | |
149 | } | |
150 | } | |
3d249d4c JP |
151 | } |
152 | ||
358b8382 JP |
153 | void team_options_unregister(struct team *team, |
154 | const struct team_option *option, | |
3d249d4c JP |
155 | size_t option_count) |
156 | { | |
157 | __team_options_unregister(team, option, option_count); | |
158 | __team_options_change_check(team, NULL); | |
159 | } | |
160 | EXPORT_SYMBOL(team_options_unregister); | |
161 | ||
162 | static int team_option_get(struct team *team, struct team_option *option, | |
163 | void *arg) | |
164 | { | |
165 | return option->getter(team, arg); | |
166 | } | |
167 | ||
168 | static int team_option_set(struct team *team, struct team_option *option, | |
169 | void *arg) | |
170 | { | |
171 | int err; | |
172 | ||
173 | err = option->setter(team, arg); | |
174 | if (err) | |
175 | return err; | |
176 | ||
177 | __team_options_change_check(team, option); | |
178 | return err; | |
179 | } | |
180 | ||
181 | /**************** | |
182 | * Mode handling | |
183 | ****************/ | |
184 | ||
185 | static LIST_HEAD(mode_list); | |
186 | static DEFINE_SPINLOCK(mode_list_lock); | |
187 | ||
188 | static struct team_mode *__find_mode(const char *kind) | |
189 | { | |
190 | struct team_mode *mode; | |
191 | ||
192 | list_for_each_entry(mode, &mode_list, list) { | |
193 | if (strcmp(mode->kind, kind) == 0) | |
194 | return mode; | |
195 | } | |
196 | return NULL; | |
197 | } | |
198 | ||
199 | static bool is_good_mode_name(const char *name) | |
200 | { | |
201 | while (*name != '\0') { | |
202 | if (!isalpha(*name) && !isdigit(*name) && *name != '_') | |
203 | return false; | |
204 | name++; | |
205 | } | |
206 | return true; | |
207 | } | |
208 | ||
209 | int team_mode_register(struct team_mode *mode) | |
210 | { | |
211 | int err = 0; | |
212 | ||
213 | if (!is_good_mode_name(mode->kind) || | |
214 | mode->priv_size > TEAM_MODE_PRIV_SIZE) | |
215 | return -EINVAL; | |
216 | spin_lock(&mode_list_lock); | |
217 | if (__find_mode(mode->kind)) { | |
218 | err = -EEXIST; | |
219 | goto unlock; | |
220 | } | |
221 | list_add_tail(&mode->list, &mode_list); | |
222 | unlock: | |
223 | spin_unlock(&mode_list_lock); | |
224 | return err; | |
225 | } | |
226 | EXPORT_SYMBOL(team_mode_register); | |
227 | ||
228 | int team_mode_unregister(struct team_mode *mode) | |
229 | { | |
230 | spin_lock(&mode_list_lock); | |
231 | list_del_init(&mode->list); | |
232 | spin_unlock(&mode_list_lock); | |
233 | return 0; | |
234 | } | |
235 | EXPORT_SYMBOL(team_mode_unregister); | |
236 | ||
237 | static struct team_mode *team_mode_get(const char *kind) | |
238 | { | |
239 | struct team_mode *mode; | |
240 | ||
241 | spin_lock(&mode_list_lock); | |
242 | mode = __find_mode(kind); | |
243 | if (!mode) { | |
244 | spin_unlock(&mode_list_lock); | |
245 | request_module("team-mode-%s", kind); | |
246 | spin_lock(&mode_list_lock); | |
247 | mode = __find_mode(kind); | |
248 | } | |
249 | if (mode) | |
250 | if (!try_module_get(mode->owner)) | |
251 | mode = NULL; | |
252 | ||
253 | spin_unlock(&mode_list_lock); | |
254 | return mode; | |
255 | } | |
256 | ||
257 | static void team_mode_put(const struct team_mode *mode) | |
258 | { | |
259 | module_put(mode->owner); | |
260 | } | |
261 | ||
262 | static bool team_dummy_transmit(struct team *team, struct sk_buff *skb) | |
263 | { | |
264 | dev_kfree_skb_any(skb); | |
265 | return false; | |
266 | } | |
267 | ||
268 | rx_handler_result_t team_dummy_receive(struct team *team, | |
269 | struct team_port *port, | |
270 | struct sk_buff *skb) | |
271 | { | |
272 | return RX_HANDLER_ANOTHER; | |
273 | } | |
274 | ||
275 | static void team_adjust_ops(struct team *team) | |
276 | { | |
277 | /* | |
278 | * To avoid checks in rx/tx skb paths, ensure here that non-null and | |
279 | * correct ops are always set. | |
280 | */ | |
281 | ||
282 | if (list_empty(&team->port_list) || | |
283 | !team->mode || !team->mode->ops->transmit) | |
284 | team->ops.transmit = team_dummy_transmit; | |
285 | else | |
286 | team->ops.transmit = team->mode->ops->transmit; | |
287 | ||
288 | if (list_empty(&team->port_list) || | |
289 | !team->mode || !team->mode->ops->receive) | |
290 | team->ops.receive = team_dummy_receive; | |
291 | else | |
292 | team->ops.receive = team->mode->ops->receive; | |
293 | } | |
294 | ||
295 | /* | |
296 | * We can benefit from the fact that it's ensured no port is present | |
297 | * at the time of mode change. Therefore no packets are in fly so there's no | |
298 | * need to set mode operations in any special way. | |
299 | */ | |
300 | static int __team_change_mode(struct team *team, | |
301 | const struct team_mode *new_mode) | |
302 | { | |
303 | /* Check if mode was previously set and do cleanup if so */ | |
304 | if (team->mode) { | |
305 | void (*exit_op)(struct team *team) = team->ops.exit; | |
306 | ||
307 | /* Clear ops area so no callback is called any longer */ | |
308 | memset(&team->ops, 0, sizeof(struct team_mode_ops)); | |
309 | team_adjust_ops(team); | |
310 | ||
311 | if (exit_op) | |
312 | exit_op(team); | |
313 | team_mode_put(team->mode); | |
314 | team->mode = NULL; | |
315 | /* zero private data area */ | |
316 | memset(&team->mode_priv, 0, | |
317 | sizeof(struct team) - offsetof(struct team, mode_priv)); | |
318 | } | |
319 | ||
320 | if (!new_mode) | |
321 | return 0; | |
322 | ||
323 | if (new_mode->ops->init) { | |
324 | int err; | |
325 | ||
326 | err = new_mode->ops->init(team); | |
327 | if (err) | |
328 | return err; | |
329 | } | |
330 | ||
331 | team->mode = new_mode; | |
332 | memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops)); | |
333 | team_adjust_ops(team); | |
334 | ||
335 | return 0; | |
336 | } | |
337 | ||
338 | static int team_change_mode(struct team *team, const char *kind) | |
339 | { | |
340 | struct team_mode *new_mode; | |
341 | struct net_device *dev = team->dev; | |
342 | int err; | |
343 | ||
344 | if (!list_empty(&team->port_list)) { | |
345 | netdev_err(dev, "No ports can be present during mode change\n"); | |
346 | return -EBUSY; | |
347 | } | |
348 | ||
349 | if (team->mode && strcmp(team->mode->kind, kind) == 0) { | |
350 | netdev_err(dev, "Unable to change to the same mode the team is in\n"); | |
351 | return -EINVAL; | |
352 | } | |
353 | ||
354 | new_mode = team_mode_get(kind); | |
355 | if (!new_mode) { | |
356 | netdev_err(dev, "Mode \"%s\" not found\n", kind); | |
357 | return -EINVAL; | |
358 | } | |
359 | ||
360 | err = __team_change_mode(team, new_mode); | |
361 | if (err) { | |
362 | netdev_err(dev, "Failed to change to mode \"%s\"\n", kind); | |
363 | team_mode_put(new_mode); | |
364 | return err; | |
365 | } | |
366 | ||
367 | netdev_info(dev, "Mode changed to \"%s\"\n", kind); | |
368 | return 0; | |
369 | } | |
370 | ||
371 | ||
372 | /************************ | |
373 | * Rx path frame handler | |
374 | ************************/ | |
375 | ||
376 | /* note: already called with rcu_read_lock */ | |
377 | static rx_handler_result_t team_handle_frame(struct sk_buff **pskb) | |
378 | { | |
379 | struct sk_buff *skb = *pskb; | |
380 | struct team_port *port; | |
381 | struct team *team; | |
382 | rx_handler_result_t res; | |
383 | ||
384 | skb = skb_share_check(skb, GFP_ATOMIC); | |
385 | if (!skb) | |
386 | return RX_HANDLER_CONSUMED; | |
387 | ||
388 | *pskb = skb; | |
389 | ||
390 | port = team_port_get_rcu(skb->dev); | |
391 | team = port->team; | |
392 | ||
393 | res = team->ops.receive(team, port, skb); | |
394 | if (res == RX_HANDLER_ANOTHER) { | |
395 | struct team_pcpu_stats *pcpu_stats; | |
396 | ||
397 | pcpu_stats = this_cpu_ptr(team->pcpu_stats); | |
398 | u64_stats_update_begin(&pcpu_stats->syncp); | |
399 | pcpu_stats->rx_packets++; | |
400 | pcpu_stats->rx_bytes += skb->len; | |
401 | if (skb->pkt_type == PACKET_MULTICAST) | |
402 | pcpu_stats->rx_multicast++; | |
403 | u64_stats_update_end(&pcpu_stats->syncp); | |
404 | ||
405 | skb->dev = team->dev; | |
406 | } else { | |
407 | this_cpu_inc(team->pcpu_stats->rx_dropped); | |
408 | } | |
409 | ||
410 | return res; | |
411 | } | |
412 | ||
413 | ||
414 | /**************** | |
415 | * Port handling | |
416 | ****************/ | |
417 | ||
418 | static bool team_port_find(const struct team *team, | |
419 | const struct team_port *port) | |
420 | { | |
421 | struct team_port *cur; | |
422 | ||
423 | list_for_each_entry(cur, &team->port_list, list) | |
424 | if (cur == port) | |
425 | return true; | |
426 | return false; | |
427 | } | |
428 | ||
429 | /* | |
430 | * Add/delete port to the team port list. Write guarded by rtnl_lock. | |
431 | * Takes care of correct port->index setup (might be racy). | |
432 | */ | |
433 | static void team_port_list_add_port(struct team *team, | |
434 | struct team_port *port) | |
435 | { | |
436 | port->index = team->port_count++; | |
437 | hlist_add_head_rcu(&port->hlist, | |
438 | team_port_index_hash(team, port->index)); | |
439 | list_add_tail_rcu(&port->list, &team->port_list); | |
440 | } | |
441 | ||
442 | static void __reconstruct_port_hlist(struct team *team, int rm_index) | |
443 | { | |
444 | int i; | |
445 | struct team_port *port; | |
446 | ||
447 | for (i = rm_index + 1; i < team->port_count; i++) { | |
448 | port = team_get_port_by_index(team, i); | |
449 | hlist_del_rcu(&port->hlist); | |
450 | port->index--; | |
451 | hlist_add_head_rcu(&port->hlist, | |
452 | team_port_index_hash(team, port->index)); | |
453 | } | |
454 | } | |
455 | ||
456 | static void team_port_list_del_port(struct team *team, | |
457 | struct team_port *port) | |
458 | { | |
459 | int rm_index = port->index; | |
460 | ||
461 | hlist_del_rcu(&port->hlist); | |
462 | list_del_rcu(&port->list); | |
463 | __reconstruct_port_hlist(team, rm_index); | |
464 | team->port_count--; | |
465 | } | |
466 | ||
467 | #define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \ | |
468 | NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \ | |
469 | NETIF_F_HIGHDMA | NETIF_F_LRO) | |
470 | ||
471 | static void __team_compute_features(struct team *team) | |
472 | { | |
473 | struct team_port *port; | |
474 | u32 vlan_features = TEAM_VLAN_FEATURES; | |
475 | unsigned short max_hard_header_len = ETH_HLEN; | |
476 | ||
477 | list_for_each_entry(port, &team->port_list, list) { | |
478 | vlan_features = netdev_increment_features(vlan_features, | |
479 | port->dev->vlan_features, | |
480 | TEAM_VLAN_FEATURES); | |
481 | ||
482 | if (port->dev->hard_header_len > max_hard_header_len) | |
483 | max_hard_header_len = port->dev->hard_header_len; | |
484 | } | |
485 | ||
486 | team->dev->vlan_features = vlan_features; | |
487 | team->dev->hard_header_len = max_hard_header_len; | |
488 | ||
489 | netdev_change_features(team->dev); | |
490 | } | |
491 | ||
492 | static void team_compute_features(struct team *team) | |
493 | { | |
61dc3461 | 494 | mutex_lock(&team->lock); |
3d249d4c | 495 | __team_compute_features(team); |
61dc3461 | 496 | mutex_unlock(&team->lock); |
3d249d4c JP |
497 | } |
498 | ||
499 | static int team_port_enter(struct team *team, struct team_port *port) | |
500 | { | |
501 | int err = 0; | |
502 | ||
503 | dev_hold(team->dev); | |
504 | port->dev->priv_flags |= IFF_TEAM_PORT; | |
505 | if (team->ops.port_enter) { | |
506 | err = team->ops.port_enter(team, port); | |
507 | if (err) { | |
508 | netdev_err(team->dev, "Device %s failed to enter team mode\n", | |
509 | port->dev->name); | |
510 | goto err_port_enter; | |
511 | } | |
512 | } | |
513 | ||
514 | return 0; | |
515 | ||
516 | err_port_enter: | |
517 | port->dev->priv_flags &= ~IFF_TEAM_PORT; | |
518 | dev_put(team->dev); | |
519 | ||
520 | return err; | |
521 | } | |
522 | ||
523 | static void team_port_leave(struct team *team, struct team_port *port) | |
524 | { | |
525 | if (team->ops.port_leave) | |
526 | team->ops.port_leave(team, port); | |
527 | port->dev->priv_flags &= ~IFF_TEAM_PORT; | |
528 | dev_put(team->dev); | |
529 | } | |
530 | ||
531 | static void __team_port_change_check(struct team_port *port, bool linkup); | |
532 | ||
533 | static int team_port_add(struct team *team, struct net_device *port_dev) | |
534 | { | |
535 | struct net_device *dev = team->dev; | |
536 | struct team_port *port; | |
537 | char *portname = port_dev->name; | |
538 | int err; | |
539 | ||
540 | if (port_dev->flags & IFF_LOOPBACK || | |
541 | port_dev->type != ARPHRD_ETHER) { | |
542 | netdev_err(dev, "Device %s is of an unsupported type\n", | |
543 | portname); | |
544 | return -EINVAL; | |
545 | } | |
546 | ||
547 | if (team_port_exists(port_dev)) { | |
548 | netdev_err(dev, "Device %s is already a port " | |
549 | "of a team device\n", portname); | |
550 | return -EBUSY; | |
551 | } | |
552 | ||
553 | if (port_dev->flags & IFF_UP) { | |
554 | netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n", | |
555 | portname); | |
556 | return -EBUSY; | |
557 | } | |
558 | ||
559 | port = kzalloc(sizeof(struct team_port), GFP_KERNEL); | |
560 | if (!port) | |
561 | return -ENOMEM; | |
562 | ||
563 | port->dev = port_dev; | |
564 | port->team = team; | |
565 | ||
566 | port->orig.mtu = port_dev->mtu; | |
567 | err = dev_set_mtu(port_dev, dev->mtu); | |
568 | if (err) { | |
569 | netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err); | |
570 | goto err_set_mtu; | |
571 | } | |
572 | ||
573 | memcpy(port->orig.dev_addr, port_dev->dev_addr, ETH_ALEN); | |
574 | ||
575 | err = team_port_enter(team, port); | |
576 | if (err) { | |
577 | netdev_err(dev, "Device %s failed to enter team mode\n", | |
578 | portname); | |
579 | goto err_port_enter; | |
580 | } | |
581 | ||
582 | err = dev_open(port_dev); | |
583 | if (err) { | |
584 | netdev_dbg(dev, "Device %s opening failed\n", | |
585 | portname); | |
586 | goto err_dev_open; | |
587 | } | |
588 | ||
589 | err = netdev_set_master(port_dev, dev); | |
590 | if (err) { | |
591 | netdev_err(dev, "Device %s failed to set master\n", portname); | |
592 | goto err_set_master; | |
593 | } | |
594 | ||
595 | err = netdev_rx_handler_register(port_dev, team_handle_frame, | |
596 | port); | |
597 | if (err) { | |
598 | netdev_err(dev, "Device %s failed to register rx_handler\n", | |
599 | portname); | |
600 | goto err_handler_register; | |
601 | } | |
602 | ||
603 | team_port_list_add_port(team, port); | |
604 | team_adjust_ops(team); | |
605 | __team_compute_features(team); | |
606 | __team_port_change_check(port, !!netif_carrier_ok(port_dev)); | |
607 | ||
608 | netdev_info(dev, "Port device %s added\n", portname); | |
609 | ||
610 | return 0; | |
611 | ||
612 | err_handler_register: | |
613 | netdev_set_master(port_dev, NULL); | |
614 | ||
615 | err_set_master: | |
616 | dev_close(port_dev); | |
617 | ||
618 | err_dev_open: | |
619 | team_port_leave(team, port); | |
620 | team_port_set_orig_mac(port); | |
621 | ||
622 | err_port_enter: | |
623 | dev_set_mtu(port_dev, port->orig.mtu); | |
624 | ||
625 | err_set_mtu: | |
626 | kfree(port); | |
627 | ||
628 | return err; | |
629 | } | |
630 | ||
631 | static int team_port_del(struct team *team, struct net_device *port_dev) | |
632 | { | |
633 | struct net_device *dev = team->dev; | |
634 | struct team_port *port; | |
635 | char *portname = port_dev->name; | |
636 | ||
637 | port = team_port_get_rtnl(port_dev); | |
638 | if (!port || !team_port_find(team, port)) { | |
639 | netdev_err(dev, "Device %s does not act as a port of this team\n", | |
640 | portname); | |
641 | return -ENOENT; | |
642 | } | |
643 | ||
644 | __team_port_change_check(port, false); | |
645 | team_port_list_del_port(team, port); | |
646 | team_adjust_ops(team); | |
647 | netdev_rx_handler_unregister(port_dev); | |
648 | netdev_set_master(port_dev, NULL); | |
649 | dev_close(port_dev); | |
650 | team_port_leave(team, port); | |
651 | team_port_set_orig_mac(port); | |
652 | dev_set_mtu(port_dev, port->orig.mtu); | |
653 | synchronize_rcu(); | |
654 | kfree(port); | |
655 | netdev_info(dev, "Port device %s removed\n", portname); | |
656 | __team_compute_features(team); | |
657 | ||
658 | return 0; | |
659 | } | |
660 | ||
661 | ||
662 | /***************** | |
663 | * Net device ops | |
664 | *****************/ | |
665 | ||
666 | static const char team_no_mode_kind[] = "*NOMODE*"; | |
667 | ||
668 | static int team_mode_option_get(struct team *team, void *arg) | |
669 | { | |
670 | const char **str = arg; | |
671 | ||
672 | *str = team->mode ? team->mode->kind : team_no_mode_kind; | |
673 | return 0; | |
674 | } | |
675 | ||
676 | static int team_mode_option_set(struct team *team, void *arg) | |
677 | { | |
678 | const char **str = arg; | |
679 | ||
680 | return team_change_mode(team, *str); | |
681 | } | |
682 | ||
358b8382 | 683 | static const struct team_option team_options[] = { |
3d249d4c JP |
684 | { |
685 | .name = "mode", | |
686 | .type = TEAM_OPTION_TYPE_STRING, | |
687 | .getter = team_mode_option_get, | |
688 | .setter = team_mode_option_set, | |
689 | }, | |
690 | }; | |
691 | ||
692 | static int team_init(struct net_device *dev) | |
693 | { | |
694 | struct team *team = netdev_priv(dev); | |
695 | int i; | |
358b8382 | 696 | int err; |
3d249d4c JP |
697 | |
698 | team->dev = dev; | |
61dc3461 | 699 | mutex_init(&team->lock); |
3d249d4c JP |
700 | |
701 | team->pcpu_stats = alloc_percpu(struct team_pcpu_stats); | |
702 | if (!team->pcpu_stats) | |
703 | return -ENOMEM; | |
704 | ||
705 | for (i = 0; i < TEAM_PORT_HASHENTRIES; i++) | |
706 | INIT_HLIST_HEAD(&team->port_hlist[i]); | |
707 | INIT_LIST_HEAD(&team->port_list); | |
708 | ||
709 | team_adjust_ops(team); | |
710 | ||
711 | INIT_LIST_HEAD(&team->option_list); | |
358b8382 JP |
712 | err = team_options_register(team, team_options, ARRAY_SIZE(team_options)); |
713 | if (err) | |
714 | goto err_options_register; | |
3d249d4c JP |
715 | netif_carrier_off(dev); |
716 | ||
717 | return 0; | |
358b8382 JP |
718 | |
719 | err_options_register: | |
720 | free_percpu(team->pcpu_stats); | |
721 | ||
722 | return err; | |
3d249d4c JP |
723 | } |
724 | ||
725 | static void team_uninit(struct net_device *dev) | |
726 | { | |
727 | struct team *team = netdev_priv(dev); | |
728 | struct team_port *port; | |
729 | struct team_port *tmp; | |
730 | ||
61dc3461 | 731 | mutex_lock(&team->lock); |
3d249d4c JP |
732 | list_for_each_entry_safe(port, tmp, &team->port_list, list) |
733 | team_port_del(team, port->dev); | |
734 | ||
735 | __team_change_mode(team, NULL); /* cleanup */ | |
736 | __team_options_unregister(team, team_options, ARRAY_SIZE(team_options)); | |
61dc3461 | 737 | mutex_unlock(&team->lock); |
3d249d4c JP |
738 | } |
739 | ||
740 | static void team_destructor(struct net_device *dev) | |
741 | { | |
742 | struct team *team = netdev_priv(dev); | |
743 | ||
744 | free_percpu(team->pcpu_stats); | |
745 | free_netdev(dev); | |
746 | } | |
747 | ||
748 | static int team_open(struct net_device *dev) | |
749 | { | |
750 | netif_carrier_on(dev); | |
751 | return 0; | |
752 | } | |
753 | ||
754 | static int team_close(struct net_device *dev) | |
755 | { | |
756 | netif_carrier_off(dev); | |
757 | return 0; | |
758 | } | |
759 | ||
760 | /* | |
761 | * note: already called with rcu_read_lock | |
762 | */ | |
763 | static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev) | |
764 | { | |
765 | struct team *team = netdev_priv(dev); | |
766 | bool tx_success = false; | |
767 | unsigned int len = skb->len; | |
768 | ||
769 | tx_success = team->ops.transmit(team, skb); | |
770 | if (tx_success) { | |
771 | struct team_pcpu_stats *pcpu_stats; | |
772 | ||
773 | pcpu_stats = this_cpu_ptr(team->pcpu_stats); | |
774 | u64_stats_update_begin(&pcpu_stats->syncp); | |
775 | pcpu_stats->tx_packets++; | |
776 | pcpu_stats->tx_bytes += len; | |
777 | u64_stats_update_end(&pcpu_stats->syncp); | |
778 | } else { | |
779 | this_cpu_inc(team->pcpu_stats->tx_dropped); | |
780 | } | |
781 | ||
782 | return NETDEV_TX_OK; | |
783 | } | |
784 | ||
785 | static void team_change_rx_flags(struct net_device *dev, int change) | |
786 | { | |
787 | struct team *team = netdev_priv(dev); | |
788 | struct team_port *port; | |
789 | int inc; | |
790 | ||
791 | rcu_read_lock(); | |
792 | list_for_each_entry_rcu(port, &team->port_list, list) { | |
793 | if (change & IFF_PROMISC) { | |
794 | inc = dev->flags & IFF_PROMISC ? 1 : -1; | |
795 | dev_set_promiscuity(port->dev, inc); | |
796 | } | |
797 | if (change & IFF_ALLMULTI) { | |
798 | inc = dev->flags & IFF_ALLMULTI ? 1 : -1; | |
799 | dev_set_allmulti(port->dev, inc); | |
800 | } | |
801 | } | |
802 | rcu_read_unlock(); | |
803 | } | |
804 | ||
805 | static void team_set_rx_mode(struct net_device *dev) | |
806 | { | |
807 | struct team *team = netdev_priv(dev); | |
808 | struct team_port *port; | |
809 | ||
810 | rcu_read_lock(); | |
811 | list_for_each_entry_rcu(port, &team->port_list, list) { | |
812 | dev_uc_sync(port->dev, dev); | |
813 | dev_mc_sync(port->dev, dev); | |
814 | } | |
815 | rcu_read_unlock(); | |
816 | } | |
817 | ||
818 | static int team_set_mac_address(struct net_device *dev, void *p) | |
819 | { | |
820 | struct team *team = netdev_priv(dev); | |
821 | struct team_port *port; | |
822 | struct sockaddr *addr = p; | |
823 | ||
824 | memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN); | |
825 | rcu_read_lock(); | |
826 | list_for_each_entry_rcu(port, &team->port_list, list) | |
827 | if (team->ops.port_change_mac) | |
828 | team->ops.port_change_mac(team, port); | |
829 | rcu_read_unlock(); | |
830 | return 0; | |
831 | } | |
832 | ||
833 | static int team_change_mtu(struct net_device *dev, int new_mtu) | |
834 | { | |
835 | struct team *team = netdev_priv(dev); | |
836 | struct team_port *port; | |
837 | int err; | |
838 | ||
839 | /* | |
840 | * Alhough this is reader, it's guarded by team lock. It's not possible | |
841 | * to traverse list in reverse under rcu_read_lock | |
842 | */ | |
61dc3461 | 843 | mutex_lock(&team->lock); |
3d249d4c JP |
844 | list_for_each_entry(port, &team->port_list, list) { |
845 | err = dev_set_mtu(port->dev, new_mtu); | |
846 | if (err) { | |
847 | netdev_err(dev, "Device %s failed to change mtu", | |
848 | port->dev->name); | |
849 | goto unwind; | |
850 | } | |
851 | } | |
61dc3461 | 852 | mutex_unlock(&team->lock); |
3d249d4c JP |
853 | |
854 | dev->mtu = new_mtu; | |
855 | ||
856 | return 0; | |
857 | ||
858 | unwind: | |
859 | list_for_each_entry_continue_reverse(port, &team->port_list, list) | |
860 | dev_set_mtu(port->dev, dev->mtu); | |
61dc3461 | 861 | mutex_unlock(&team->lock); |
3d249d4c JP |
862 | |
863 | return err; | |
864 | } | |
865 | ||
866 | static struct rtnl_link_stats64 * | |
867 | team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) | |
868 | { | |
869 | struct team *team = netdev_priv(dev); | |
870 | struct team_pcpu_stats *p; | |
871 | u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes; | |
872 | u32 rx_dropped = 0, tx_dropped = 0; | |
873 | unsigned int start; | |
874 | int i; | |
875 | ||
876 | for_each_possible_cpu(i) { | |
877 | p = per_cpu_ptr(team->pcpu_stats, i); | |
878 | do { | |
879 | start = u64_stats_fetch_begin_bh(&p->syncp); | |
880 | rx_packets = p->rx_packets; | |
881 | rx_bytes = p->rx_bytes; | |
882 | rx_multicast = p->rx_multicast; | |
883 | tx_packets = p->tx_packets; | |
884 | tx_bytes = p->tx_bytes; | |
885 | } while (u64_stats_fetch_retry_bh(&p->syncp, start)); | |
886 | ||
887 | stats->rx_packets += rx_packets; | |
888 | stats->rx_bytes += rx_bytes; | |
889 | stats->multicast += rx_multicast; | |
890 | stats->tx_packets += tx_packets; | |
891 | stats->tx_bytes += tx_bytes; | |
892 | /* | |
893 | * rx_dropped & tx_dropped are u32, updated | |
894 | * without syncp protection. | |
895 | */ | |
896 | rx_dropped += p->rx_dropped; | |
897 | tx_dropped += p->tx_dropped; | |
898 | } | |
899 | stats->rx_dropped = rx_dropped; | |
900 | stats->tx_dropped = tx_dropped; | |
901 | return stats; | |
902 | } | |
903 | ||
904 | static void team_vlan_rx_add_vid(struct net_device *dev, uint16_t vid) | |
905 | { | |
906 | struct team *team = netdev_priv(dev); | |
907 | struct team_port *port; | |
908 | ||
909 | rcu_read_lock(); | |
910 | list_for_each_entry_rcu(port, &team->port_list, list) { | |
911 | const struct net_device_ops *ops = port->dev->netdev_ops; | |
912 | ||
913 | if (ops->ndo_vlan_rx_add_vid) | |
914 | ops->ndo_vlan_rx_add_vid(port->dev, vid); | |
915 | } | |
916 | rcu_read_unlock(); | |
917 | } | |
918 | ||
919 | static void team_vlan_rx_kill_vid(struct net_device *dev, uint16_t vid) | |
920 | { | |
921 | struct team *team = netdev_priv(dev); | |
922 | struct team_port *port; | |
923 | ||
924 | rcu_read_lock(); | |
925 | list_for_each_entry_rcu(port, &team->port_list, list) { | |
926 | const struct net_device_ops *ops = port->dev->netdev_ops; | |
927 | ||
928 | if (ops->ndo_vlan_rx_kill_vid) | |
929 | ops->ndo_vlan_rx_kill_vid(port->dev, vid); | |
930 | } | |
931 | rcu_read_unlock(); | |
932 | } | |
933 | ||
934 | static int team_add_slave(struct net_device *dev, struct net_device *port_dev) | |
935 | { | |
936 | struct team *team = netdev_priv(dev); | |
937 | int err; | |
938 | ||
61dc3461 | 939 | mutex_lock(&team->lock); |
3d249d4c | 940 | err = team_port_add(team, port_dev); |
61dc3461 | 941 | mutex_unlock(&team->lock); |
3d249d4c JP |
942 | return err; |
943 | } | |
944 | ||
945 | static int team_del_slave(struct net_device *dev, struct net_device *port_dev) | |
946 | { | |
947 | struct team *team = netdev_priv(dev); | |
948 | int err; | |
949 | ||
61dc3461 | 950 | mutex_lock(&team->lock); |
3d249d4c | 951 | err = team_port_del(team, port_dev); |
61dc3461 | 952 | mutex_unlock(&team->lock); |
3d249d4c JP |
953 | return err; |
954 | } | |
955 | ||
956 | static const struct net_device_ops team_netdev_ops = { | |
957 | .ndo_init = team_init, | |
958 | .ndo_uninit = team_uninit, | |
959 | .ndo_open = team_open, | |
960 | .ndo_stop = team_close, | |
961 | .ndo_start_xmit = team_xmit, | |
962 | .ndo_change_rx_flags = team_change_rx_flags, | |
963 | .ndo_set_rx_mode = team_set_rx_mode, | |
964 | .ndo_set_mac_address = team_set_mac_address, | |
965 | .ndo_change_mtu = team_change_mtu, | |
966 | .ndo_get_stats64 = team_get_stats64, | |
967 | .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid, | |
968 | .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid, | |
969 | .ndo_add_slave = team_add_slave, | |
970 | .ndo_del_slave = team_del_slave, | |
971 | }; | |
972 | ||
973 | ||
974 | /*********************** | |
975 | * rt netlink interface | |
976 | ***********************/ | |
977 | ||
978 | static void team_setup(struct net_device *dev) | |
979 | { | |
980 | ether_setup(dev); | |
981 | ||
982 | dev->netdev_ops = &team_netdev_ops; | |
983 | dev->destructor = team_destructor; | |
984 | dev->tx_queue_len = 0; | |
985 | dev->flags |= IFF_MULTICAST; | |
986 | dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING); | |
987 | ||
988 | /* | |
989 | * Indicate we support unicast address filtering. That way core won't | |
990 | * bring us to promisc mode in case a unicast addr is added. | |
991 | * Let this up to underlay drivers. | |
992 | */ | |
993 | dev->priv_flags |= IFF_UNICAST_FLT; | |
994 | ||
995 | dev->features |= NETIF_F_LLTX; | |
996 | dev->features |= NETIF_F_GRO; | |
997 | dev->hw_features = NETIF_F_HW_VLAN_TX | | |
998 | NETIF_F_HW_VLAN_RX | | |
999 | NETIF_F_HW_VLAN_FILTER; | |
1000 | ||
1001 | dev->features |= dev->hw_features; | |
1002 | } | |
1003 | ||
1004 | static int team_newlink(struct net *src_net, struct net_device *dev, | |
1005 | struct nlattr *tb[], struct nlattr *data[]) | |
1006 | { | |
1007 | int err; | |
1008 | ||
1009 | if (tb[IFLA_ADDRESS] == NULL) | |
1010 | random_ether_addr(dev->dev_addr); | |
1011 | ||
1012 | err = register_netdevice(dev); | |
1013 | if (err) | |
1014 | return err; | |
1015 | ||
1016 | return 0; | |
1017 | } | |
1018 | ||
1019 | static int team_validate(struct nlattr *tb[], struct nlattr *data[]) | |
1020 | { | |
1021 | if (tb[IFLA_ADDRESS]) { | |
1022 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) | |
1023 | return -EINVAL; | |
1024 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) | |
1025 | return -EADDRNOTAVAIL; | |
1026 | } | |
1027 | return 0; | |
1028 | } | |
1029 | ||
1030 | static struct rtnl_link_ops team_link_ops __read_mostly = { | |
1031 | .kind = DRV_NAME, | |
1032 | .priv_size = sizeof(struct team), | |
1033 | .setup = team_setup, | |
1034 | .newlink = team_newlink, | |
1035 | .validate = team_validate, | |
1036 | }; | |
1037 | ||
1038 | ||
1039 | /*********************************** | |
1040 | * Generic netlink custom interface | |
1041 | ***********************************/ | |
1042 | ||
1043 | static struct genl_family team_nl_family = { | |
1044 | .id = GENL_ID_GENERATE, | |
1045 | .name = TEAM_GENL_NAME, | |
1046 | .version = TEAM_GENL_VERSION, | |
1047 | .maxattr = TEAM_ATTR_MAX, | |
1048 | .netnsok = true, | |
1049 | }; | |
1050 | ||
1051 | static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = { | |
1052 | [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, }, | |
1053 | [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 }, | |
1054 | [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED }, | |
1055 | [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED }, | |
1056 | }; | |
1057 | ||
1058 | static const struct nla_policy | |
1059 | team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = { | |
1060 | [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, }, | |
1061 | [TEAM_ATTR_OPTION_NAME] = { | |
1062 | .type = NLA_STRING, | |
1063 | .len = TEAM_STRING_MAX_LEN, | |
1064 | }, | |
1065 | [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG }, | |
1066 | [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 }, | |
1067 | [TEAM_ATTR_OPTION_DATA] = { | |
1068 | .type = NLA_BINARY, | |
1069 | .len = TEAM_STRING_MAX_LEN, | |
1070 | }, | |
1071 | }; | |
1072 | ||
1073 | static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info) | |
1074 | { | |
1075 | struct sk_buff *msg; | |
1076 | void *hdr; | |
1077 | int err; | |
1078 | ||
1079 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
1080 | if (!msg) | |
1081 | return -ENOMEM; | |
1082 | ||
1083 | hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq, | |
1084 | &team_nl_family, 0, TEAM_CMD_NOOP); | |
1085 | if (IS_ERR(hdr)) { | |
1086 | err = PTR_ERR(hdr); | |
1087 | goto err_msg_put; | |
1088 | } | |
1089 | ||
1090 | genlmsg_end(msg, hdr); | |
1091 | ||
1092 | return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid); | |
1093 | ||
1094 | err_msg_put: | |
1095 | nlmsg_free(msg); | |
1096 | ||
1097 | return err; | |
1098 | } | |
1099 | ||
1100 | /* | |
1101 | * Netlink cmd functions should be locked by following two functions. | |
8c0713a5 | 1102 | * Since dev gets held here, that ensures dev won't disappear in between. |
3d249d4c JP |
1103 | */ |
1104 | static struct team *team_nl_team_get(struct genl_info *info) | |
1105 | { | |
1106 | struct net *net = genl_info_net(info); | |
1107 | int ifindex; | |
1108 | struct net_device *dev; | |
1109 | struct team *team; | |
1110 | ||
1111 | if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX]) | |
1112 | return NULL; | |
1113 | ||
1114 | ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]); | |
8c0713a5 | 1115 | dev = dev_get_by_index(net, ifindex); |
3d249d4c | 1116 | if (!dev || dev->netdev_ops != &team_netdev_ops) { |
8c0713a5 JP |
1117 | if (dev) |
1118 | dev_put(dev); | |
3d249d4c JP |
1119 | return NULL; |
1120 | } | |
1121 | ||
1122 | team = netdev_priv(dev); | |
61dc3461 | 1123 | mutex_lock(&team->lock); |
3d249d4c JP |
1124 | return team; |
1125 | } | |
1126 | ||
1127 | static void team_nl_team_put(struct team *team) | |
1128 | { | |
61dc3461 | 1129 | mutex_unlock(&team->lock); |
8c0713a5 | 1130 | dev_put(team->dev); |
3d249d4c JP |
1131 | } |
1132 | ||
1133 | static int team_nl_send_generic(struct genl_info *info, struct team *team, | |
1134 | int (*fill_func)(struct sk_buff *skb, | |
1135 | struct genl_info *info, | |
1136 | int flags, struct team *team)) | |
1137 | { | |
1138 | struct sk_buff *skb; | |
1139 | int err; | |
1140 | ||
1141 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
1142 | if (!skb) | |
1143 | return -ENOMEM; | |
1144 | ||
1145 | err = fill_func(skb, info, NLM_F_ACK, team); | |
1146 | if (err < 0) | |
1147 | goto err_fill; | |
1148 | ||
1149 | err = genlmsg_unicast(genl_info_net(info), skb, info->snd_pid); | |
1150 | return err; | |
1151 | ||
1152 | err_fill: | |
1153 | nlmsg_free(skb); | |
1154 | return err; | |
1155 | } | |
1156 | ||
1157 | static int team_nl_fill_options_get_changed(struct sk_buff *skb, | |
1158 | u32 pid, u32 seq, int flags, | |
1159 | struct team *team, | |
1160 | struct team_option *changed_option) | |
1161 | { | |
1162 | struct nlattr *option_list; | |
1163 | void *hdr; | |
1164 | struct team_option *option; | |
1165 | ||
1166 | hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags, | |
1167 | TEAM_CMD_OPTIONS_GET); | |
1168 | if (IS_ERR(hdr)) | |
1169 | return PTR_ERR(hdr); | |
1170 | ||
1171 | NLA_PUT_U32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex); | |
1172 | option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION); | |
1173 | if (!option_list) | |
1174 | return -EMSGSIZE; | |
1175 | ||
1176 | list_for_each_entry(option, &team->option_list, list) { | |
1177 | struct nlattr *option_item; | |
1178 | long arg; | |
1179 | ||
1180 | option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION); | |
1181 | if (!option_item) | |
1182 | goto nla_put_failure; | |
1183 | NLA_PUT_STRING(skb, TEAM_ATTR_OPTION_NAME, option->name); | |
1184 | if (option == changed_option) | |
1185 | NLA_PUT_FLAG(skb, TEAM_ATTR_OPTION_CHANGED); | |
1186 | switch (option->type) { | |
1187 | case TEAM_OPTION_TYPE_U32: | |
1188 | NLA_PUT_U8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32); | |
1189 | team_option_get(team, option, &arg); | |
1190 | NLA_PUT_U32(skb, TEAM_ATTR_OPTION_DATA, arg); | |
1191 | break; | |
1192 | case TEAM_OPTION_TYPE_STRING: | |
1193 | NLA_PUT_U8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING); | |
1194 | team_option_get(team, option, &arg); | |
1195 | NLA_PUT_STRING(skb, TEAM_ATTR_OPTION_DATA, | |
1196 | (char *) arg); | |
1197 | break; | |
1198 | default: | |
1199 | BUG(); | |
1200 | } | |
1201 | nla_nest_end(skb, option_item); | |
1202 | } | |
1203 | ||
1204 | nla_nest_end(skb, option_list); | |
1205 | return genlmsg_end(skb, hdr); | |
1206 | ||
1207 | nla_put_failure: | |
1208 | genlmsg_cancel(skb, hdr); | |
1209 | return -EMSGSIZE; | |
1210 | } | |
1211 | ||
1212 | static int team_nl_fill_options_get(struct sk_buff *skb, | |
1213 | struct genl_info *info, int flags, | |
1214 | struct team *team) | |
1215 | { | |
1216 | return team_nl_fill_options_get_changed(skb, info->snd_pid, | |
1217 | info->snd_seq, NLM_F_ACK, | |
1218 | team, NULL); | |
1219 | } | |
1220 | ||
1221 | static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info) | |
1222 | { | |
1223 | struct team *team; | |
1224 | int err; | |
1225 | ||
1226 | team = team_nl_team_get(info); | |
1227 | if (!team) | |
1228 | return -EINVAL; | |
1229 | ||
1230 | err = team_nl_send_generic(info, team, team_nl_fill_options_get); | |
1231 | ||
1232 | team_nl_team_put(team); | |
1233 | ||
1234 | return err; | |
1235 | } | |
1236 | ||
1237 | static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info) | |
1238 | { | |
1239 | struct team *team; | |
1240 | int err = 0; | |
1241 | int i; | |
1242 | struct nlattr *nl_option; | |
1243 | ||
1244 | team = team_nl_team_get(info); | |
1245 | if (!team) | |
1246 | return -EINVAL; | |
1247 | ||
1248 | err = -EINVAL; | |
1249 | if (!info->attrs[TEAM_ATTR_LIST_OPTION]) { | |
1250 | err = -EINVAL; | |
1251 | goto team_put; | |
1252 | } | |
1253 | ||
1254 | nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) { | |
1255 | struct nlattr *mode_attrs[TEAM_ATTR_OPTION_MAX + 1]; | |
1256 | enum team_option_type opt_type; | |
1257 | struct team_option *option; | |
1258 | char *opt_name; | |
1259 | bool opt_found = false; | |
1260 | ||
1261 | if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) { | |
1262 | err = -EINVAL; | |
1263 | goto team_put; | |
1264 | } | |
1265 | err = nla_parse_nested(mode_attrs, TEAM_ATTR_OPTION_MAX, | |
1266 | nl_option, team_nl_option_policy); | |
1267 | if (err) | |
1268 | goto team_put; | |
1269 | if (!mode_attrs[TEAM_ATTR_OPTION_NAME] || | |
1270 | !mode_attrs[TEAM_ATTR_OPTION_TYPE] || | |
1271 | !mode_attrs[TEAM_ATTR_OPTION_DATA]) { | |
1272 | err = -EINVAL; | |
1273 | goto team_put; | |
1274 | } | |
1275 | switch (nla_get_u8(mode_attrs[TEAM_ATTR_OPTION_TYPE])) { | |
1276 | case NLA_U32: | |
1277 | opt_type = TEAM_OPTION_TYPE_U32; | |
1278 | break; | |
1279 | case NLA_STRING: | |
1280 | opt_type = TEAM_OPTION_TYPE_STRING; | |
1281 | break; | |
1282 | default: | |
1283 | goto team_put; | |
1284 | } | |
1285 | ||
1286 | opt_name = nla_data(mode_attrs[TEAM_ATTR_OPTION_NAME]); | |
1287 | list_for_each_entry(option, &team->option_list, list) { | |
1288 | long arg; | |
1289 | struct nlattr *opt_data_attr; | |
1290 | ||
1291 | if (option->type != opt_type || | |
1292 | strcmp(option->name, opt_name)) | |
1293 | continue; | |
1294 | opt_found = true; | |
1295 | opt_data_attr = mode_attrs[TEAM_ATTR_OPTION_DATA]; | |
1296 | switch (opt_type) { | |
1297 | case TEAM_OPTION_TYPE_U32: | |
1298 | arg = nla_get_u32(opt_data_attr); | |
1299 | break; | |
1300 | case TEAM_OPTION_TYPE_STRING: | |
1301 | arg = (long) nla_data(opt_data_attr); | |
1302 | break; | |
1303 | default: | |
1304 | BUG(); | |
1305 | } | |
1306 | err = team_option_set(team, option, &arg); | |
1307 | if (err) | |
1308 | goto team_put; | |
1309 | } | |
1310 | if (!opt_found) { | |
1311 | err = -ENOENT; | |
1312 | goto team_put; | |
1313 | } | |
1314 | } | |
1315 | ||
1316 | team_put: | |
1317 | team_nl_team_put(team); | |
1318 | ||
1319 | return err; | |
1320 | } | |
1321 | ||
1322 | static int team_nl_fill_port_list_get_changed(struct sk_buff *skb, | |
1323 | u32 pid, u32 seq, int flags, | |
1324 | struct team *team, | |
1325 | struct team_port *changed_port) | |
1326 | { | |
1327 | struct nlattr *port_list; | |
1328 | void *hdr; | |
1329 | struct team_port *port; | |
1330 | ||
1331 | hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags, | |
1332 | TEAM_CMD_PORT_LIST_GET); | |
1333 | if (IS_ERR(hdr)) | |
1334 | return PTR_ERR(hdr); | |
1335 | ||
1336 | NLA_PUT_U32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex); | |
1337 | port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT); | |
1338 | if (!port_list) | |
1339 | return -EMSGSIZE; | |
1340 | ||
1341 | list_for_each_entry(port, &team->port_list, list) { | |
1342 | struct nlattr *port_item; | |
1343 | ||
1344 | port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT); | |
1345 | if (!port_item) | |
1346 | goto nla_put_failure; | |
1347 | NLA_PUT_U32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex); | |
1348 | if (port == changed_port) | |
1349 | NLA_PUT_FLAG(skb, TEAM_ATTR_PORT_CHANGED); | |
1350 | if (port->linkup) | |
1351 | NLA_PUT_FLAG(skb, TEAM_ATTR_PORT_LINKUP); | |
1352 | NLA_PUT_U32(skb, TEAM_ATTR_PORT_SPEED, port->speed); | |
1353 | NLA_PUT_U8(skb, TEAM_ATTR_PORT_DUPLEX, port->duplex); | |
1354 | nla_nest_end(skb, port_item); | |
1355 | } | |
1356 | ||
1357 | nla_nest_end(skb, port_list); | |
1358 | return genlmsg_end(skb, hdr); | |
1359 | ||
1360 | nla_put_failure: | |
1361 | genlmsg_cancel(skb, hdr); | |
1362 | return -EMSGSIZE; | |
1363 | } | |
1364 | ||
1365 | static int team_nl_fill_port_list_get(struct sk_buff *skb, | |
1366 | struct genl_info *info, int flags, | |
1367 | struct team *team) | |
1368 | { | |
1369 | return team_nl_fill_port_list_get_changed(skb, info->snd_pid, | |
1370 | info->snd_seq, NLM_F_ACK, | |
1371 | team, NULL); | |
1372 | } | |
1373 | ||
1374 | static int team_nl_cmd_port_list_get(struct sk_buff *skb, | |
1375 | struct genl_info *info) | |
1376 | { | |
1377 | struct team *team; | |
1378 | int err; | |
1379 | ||
1380 | team = team_nl_team_get(info); | |
1381 | if (!team) | |
1382 | return -EINVAL; | |
1383 | ||
1384 | err = team_nl_send_generic(info, team, team_nl_fill_port_list_get); | |
1385 | ||
1386 | team_nl_team_put(team); | |
1387 | ||
1388 | return err; | |
1389 | } | |
1390 | ||
1391 | static struct genl_ops team_nl_ops[] = { | |
1392 | { | |
1393 | .cmd = TEAM_CMD_NOOP, | |
1394 | .doit = team_nl_cmd_noop, | |
1395 | .policy = team_nl_policy, | |
1396 | }, | |
1397 | { | |
1398 | .cmd = TEAM_CMD_OPTIONS_SET, | |
1399 | .doit = team_nl_cmd_options_set, | |
1400 | .policy = team_nl_policy, | |
1401 | .flags = GENL_ADMIN_PERM, | |
1402 | }, | |
1403 | { | |
1404 | .cmd = TEAM_CMD_OPTIONS_GET, | |
1405 | .doit = team_nl_cmd_options_get, | |
1406 | .policy = team_nl_policy, | |
1407 | .flags = GENL_ADMIN_PERM, | |
1408 | }, | |
1409 | { | |
1410 | .cmd = TEAM_CMD_PORT_LIST_GET, | |
1411 | .doit = team_nl_cmd_port_list_get, | |
1412 | .policy = team_nl_policy, | |
1413 | .flags = GENL_ADMIN_PERM, | |
1414 | }, | |
1415 | }; | |
1416 | ||
1417 | static struct genl_multicast_group team_change_event_mcgrp = { | |
1418 | .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME, | |
1419 | }; | |
1420 | ||
1421 | static int team_nl_send_event_options_get(struct team *team, | |
1422 | struct team_option *changed_option) | |
1423 | { | |
1424 | struct sk_buff *skb; | |
1425 | int err; | |
1426 | struct net *net = dev_net(team->dev); | |
1427 | ||
1428 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
1429 | if (!skb) | |
1430 | return -ENOMEM; | |
1431 | ||
1432 | err = team_nl_fill_options_get_changed(skb, 0, 0, 0, team, | |
1433 | changed_option); | |
1434 | if (err < 0) | |
1435 | goto err_fill; | |
1436 | ||
1437 | err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id, | |
1438 | GFP_KERNEL); | |
1439 | return err; | |
1440 | ||
1441 | err_fill: | |
1442 | nlmsg_free(skb); | |
1443 | return err; | |
1444 | } | |
1445 | ||
1446 | static int team_nl_send_event_port_list_get(struct team_port *port) | |
1447 | { | |
1448 | struct sk_buff *skb; | |
1449 | int err; | |
1450 | struct net *net = dev_net(port->team->dev); | |
1451 | ||
1452 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
1453 | if (!skb) | |
1454 | return -ENOMEM; | |
1455 | ||
1456 | err = team_nl_fill_port_list_get_changed(skb, 0, 0, 0, | |
1457 | port->team, port); | |
1458 | if (err < 0) | |
1459 | goto err_fill; | |
1460 | ||
1461 | err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id, | |
1462 | GFP_KERNEL); | |
1463 | return err; | |
1464 | ||
1465 | err_fill: | |
1466 | nlmsg_free(skb); | |
1467 | return err; | |
1468 | } | |
1469 | ||
1470 | static int team_nl_init(void) | |
1471 | { | |
1472 | int err; | |
1473 | ||
1474 | err = genl_register_family_with_ops(&team_nl_family, team_nl_ops, | |
1475 | ARRAY_SIZE(team_nl_ops)); | |
1476 | if (err) | |
1477 | return err; | |
1478 | ||
1479 | err = genl_register_mc_group(&team_nl_family, &team_change_event_mcgrp); | |
1480 | if (err) | |
1481 | goto err_change_event_grp_reg; | |
1482 | ||
1483 | return 0; | |
1484 | ||
1485 | err_change_event_grp_reg: | |
1486 | genl_unregister_family(&team_nl_family); | |
1487 | ||
1488 | return err; | |
1489 | } | |
1490 | ||
1491 | static void team_nl_fini(void) | |
1492 | { | |
1493 | genl_unregister_family(&team_nl_family); | |
1494 | } | |
1495 | ||
1496 | ||
1497 | /****************** | |
1498 | * Change checkers | |
1499 | ******************/ | |
1500 | ||
1501 | static void __team_options_change_check(struct team *team, | |
1502 | struct team_option *changed_option) | |
1503 | { | |
1504 | int err; | |
1505 | ||
1506 | err = team_nl_send_event_options_get(team, changed_option); | |
1507 | if (err) | |
1508 | netdev_warn(team->dev, "Failed to send options change via netlink\n"); | |
1509 | } | |
1510 | ||
1511 | /* rtnl lock is held */ | |
1512 | static void __team_port_change_check(struct team_port *port, bool linkup) | |
1513 | { | |
1514 | int err; | |
1515 | ||
1516 | if (port->linkup == linkup) | |
1517 | return; | |
1518 | ||
1519 | port->linkup = linkup; | |
1520 | if (linkup) { | |
1521 | struct ethtool_cmd ecmd; | |
1522 | ||
1523 | err = __ethtool_get_settings(port->dev, &ecmd); | |
1524 | if (!err) { | |
1525 | port->speed = ethtool_cmd_speed(&ecmd); | |
1526 | port->duplex = ecmd.duplex; | |
1527 | goto send_event; | |
1528 | } | |
1529 | } | |
1530 | port->speed = 0; | |
1531 | port->duplex = 0; | |
1532 | ||
1533 | send_event: | |
1534 | err = team_nl_send_event_port_list_get(port); | |
1535 | if (err) | |
1536 | netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink\n", | |
1537 | port->dev->name); | |
1538 | ||
1539 | } | |
1540 | ||
1541 | static void team_port_change_check(struct team_port *port, bool linkup) | |
1542 | { | |
1543 | struct team *team = port->team; | |
1544 | ||
61dc3461 | 1545 | mutex_lock(&team->lock); |
3d249d4c | 1546 | __team_port_change_check(port, linkup); |
61dc3461 | 1547 | mutex_unlock(&team->lock); |
3d249d4c JP |
1548 | } |
1549 | ||
1550 | /************************************ | |
1551 | * Net device notifier event handler | |
1552 | ************************************/ | |
1553 | ||
1554 | static int team_device_event(struct notifier_block *unused, | |
1555 | unsigned long event, void *ptr) | |
1556 | { | |
1557 | struct net_device *dev = (struct net_device *) ptr; | |
1558 | struct team_port *port; | |
1559 | ||
1560 | port = team_port_get_rtnl(dev); | |
1561 | if (!port) | |
1562 | return NOTIFY_DONE; | |
1563 | ||
1564 | switch (event) { | |
1565 | case NETDEV_UP: | |
1566 | if (netif_carrier_ok(dev)) | |
1567 | team_port_change_check(port, true); | |
1568 | case NETDEV_DOWN: | |
1569 | team_port_change_check(port, false); | |
1570 | case NETDEV_CHANGE: | |
1571 | if (netif_running(port->dev)) | |
1572 | team_port_change_check(port, | |
1573 | !!netif_carrier_ok(port->dev)); | |
1574 | break; | |
1575 | case NETDEV_UNREGISTER: | |
1576 | team_del_slave(port->team->dev, dev); | |
1577 | break; | |
1578 | case NETDEV_FEAT_CHANGE: | |
1579 | team_compute_features(port->team); | |
1580 | break; | |
1581 | case NETDEV_CHANGEMTU: | |
1582 | /* Forbid to change mtu of underlaying device */ | |
1583 | return NOTIFY_BAD; | |
1584 | case NETDEV_PRE_TYPE_CHANGE: | |
1585 | /* Forbid to change type of underlaying device */ | |
1586 | return NOTIFY_BAD; | |
1587 | } | |
1588 | return NOTIFY_DONE; | |
1589 | } | |
1590 | ||
1591 | static struct notifier_block team_notifier_block __read_mostly = { | |
1592 | .notifier_call = team_device_event, | |
1593 | }; | |
1594 | ||
1595 | ||
1596 | /*********************** | |
1597 | * Module init and exit | |
1598 | ***********************/ | |
1599 | ||
1600 | static int __init team_module_init(void) | |
1601 | { | |
1602 | int err; | |
1603 | ||
1604 | register_netdevice_notifier(&team_notifier_block); | |
1605 | ||
1606 | err = rtnl_link_register(&team_link_ops); | |
1607 | if (err) | |
1608 | goto err_rtnl_reg; | |
1609 | ||
1610 | err = team_nl_init(); | |
1611 | if (err) | |
1612 | goto err_nl_init; | |
1613 | ||
1614 | return 0; | |
1615 | ||
1616 | err_nl_init: | |
1617 | rtnl_link_unregister(&team_link_ops); | |
1618 | ||
1619 | err_rtnl_reg: | |
1620 | unregister_netdevice_notifier(&team_notifier_block); | |
1621 | ||
1622 | return err; | |
1623 | } | |
1624 | ||
1625 | static void __exit team_module_exit(void) | |
1626 | { | |
1627 | team_nl_fini(); | |
1628 | rtnl_link_unregister(&team_link_ops); | |
1629 | unregister_netdevice_notifier(&team_notifier_block); | |
1630 | } | |
1631 | ||
1632 | module_init(team_module_init); | |
1633 | module_exit(team_module_exit); | |
1634 | ||
1635 | MODULE_LICENSE("GPL v2"); | |
1636 | MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>"); | |
1637 | MODULE_DESCRIPTION("Ethernet team device driver"); | |
1638 | MODULE_ALIAS_RTNL_LINK(DRV_NAME); |