2 * drivers/net/team/team_mode_loadbalance.c - Load-balancing mode for team
3 * Copyright (c) 2012 Jiri Pirko <jpirko@redhat.com>
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.
11 #include <linux/kernel.h>
12 #include <linux/types.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/errno.h>
16 #include <linux/netdevice.h>
17 #include <linux/etherdevice.h>
18 #include <linux/filter.h>
19 #include <linux/if_team.h>
21 static rx_handler_result_t lb_receive(struct team *team, struct team_port *port,
24 if (unlikely(skb->protocol == htons(ETH_P_SLOW))) {
25 /* LACPDU packets should go to exact delivery */
26 const unsigned char *dest = eth_hdr(skb)->h_dest;
28 if (is_link_local_ether_addr(dest) && dest[5] == 0x02)
29 return RX_HANDLER_EXACT;
31 return RX_HANDLER_ANOTHER;
36 typedef struct team_port *lb_select_tx_port_func_t(struct team *,
41 #define LB_TX_HASHTABLE_SIZE 256 /* hash is a char */
47 struct lb_pcpu_stats {
48 struct lb_stats hash_stats[LB_TX_HASHTABLE_SIZE];
49 struct u64_stats_sync syncp;
52 struct lb_stats_info {
53 struct lb_stats stats;
54 struct lb_stats last_stats;
55 struct team_option_inst_info *opt_inst_info;
58 struct lb_port_mapping {
59 struct team_port __rcu *port;
60 struct team_option_inst_info *opt_inst_info;
65 struct lb_port_mapping tx_hash_to_port_mapping[LB_TX_HASHTABLE_SIZE];
66 struct sock_fprog_kern *orig_fprog;
68 unsigned int refresh_interval; /* in tenths of second */
69 struct delayed_work refresh_dw;
70 struct lb_stats_info info[LB_TX_HASHTABLE_SIZE];
75 struct bpf_prog __rcu *fp;
76 lb_select_tx_port_func_t __rcu *select_tx_port_func;
77 struct lb_pcpu_stats __percpu *pcpu_stats;
78 struct lb_priv_ex *ex; /* priv extension */
81 static struct lb_priv *get_lb_priv(struct team *team)
83 return (struct lb_priv *) &team->mode_priv;
87 struct lb_stats __percpu *pcpu_stats;
88 struct lb_stats_info stats_info;
91 static struct lb_port_priv *get_lb_port_priv(struct team_port *port)
93 return (struct lb_port_priv *) &port->mode_priv;
96 #define LB_HTPM_PORT_BY_HASH(lp_priv, hash) \
97 (lb_priv)->ex->tx_hash_to_port_mapping[hash].port
99 #define LB_HTPM_OPT_INST_INFO_BY_HASH(lp_priv, hash) \
100 (lb_priv)->ex->tx_hash_to_port_mapping[hash].opt_inst_info
102 static void lb_tx_hash_to_port_mapping_null_port(struct team *team,
103 struct team_port *port)
105 struct lb_priv *lb_priv = get_lb_priv(team);
106 bool changed = false;
109 for (i = 0; i < LB_TX_HASHTABLE_SIZE; i++) {
110 struct lb_port_mapping *pm;
112 pm = &lb_priv->ex->tx_hash_to_port_mapping[i];
113 if (rcu_access_pointer(pm->port) == port) {
114 RCU_INIT_POINTER(pm->port, NULL);
115 team_option_inst_set_change(pm->opt_inst_info);
120 team_options_change_check(team);
123 /* Basic tx selection based solely by hash */
124 static struct team_port *lb_hash_select_tx_port(struct team *team,
125 struct lb_priv *lb_priv,
129 int port_index = team_num_to_port_index(team, hash);
131 return team_get_port_by_index_rcu(team, port_index);
134 /* Hash to port mapping select tx port */
135 static struct team_port *lb_htpm_select_tx_port(struct team *team,
136 struct lb_priv *lb_priv,
140 return rcu_dereference_bh(LB_HTPM_PORT_BY_HASH(lb_priv, hash));
143 struct lb_select_tx_port {
145 lb_select_tx_port_func_t *func;
148 static const struct lb_select_tx_port lb_select_tx_port_list[] = {
151 .func = lb_hash_select_tx_port,
154 .name = "hash_to_port_mapping",
155 .func = lb_htpm_select_tx_port,
158 #define LB_SELECT_TX_PORT_LIST_COUNT ARRAY_SIZE(lb_select_tx_port_list)
160 static char *lb_select_tx_port_get_name(lb_select_tx_port_func_t *func)
164 for (i = 0; i < LB_SELECT_TX_PORT_LIST_COUNT; i++) {
165 const struct lb_select_tx_port *item;
167 item = &lb_select_tx_port_list[i];
168 if (item->func == func)
174 static lb_select_tx_port_func_t *lb_select_tx_port_get_func(const char *name)
178 for (i = 0; i < LB_SELECT_TX_PORT_LIST_COUNT; i++) {
179 const struct lb_select_tx_port *item;
181 item = &lb_select_tx_port_list[i];
182 if (!strcmp(item->name, name))
188 static unsigned int lb_get_skb_hash(struct lb_priv *lb_priv,
195 fp = rcu_dereference_bh(lb_priv->fp);
198 lhash = BPF_PROG_RUN(fp, skb);
200 return c[0] ^ c[1] ^ c[2] ^ c[3];
203 static void lb_update_tx_stats(unsigned int tx_bytes, struct lb_priv *lb_priv,
204 struct lb_port_priv *lb_port_priv,
207 struct lb_pcpu_stats *pcpu_stats;
208 struct lb_stats *port_stats;
209 struct lb_stats *hash_stats;
211 pcpu_stats = this_cpu_ptr(lb_priv->pcpu_stats);
212 port_stats = this_cpu_ptr(lb_port_priv->pcpu_stats);
213 hash_stats = &pcpu_stats->hash_stats[hash];
214 u64_stats_update_begin(&pcpu_stats->syncp);
215 port_stats->tx_bytes += tx_bytes;
216 hash_stats->tx_bytes += tx_bytes;
217 u64_stats_update_end(&pcpu_stats->syncp);
220 static bool lb_transmit(struct team *team, struct sk_buff *skb)
222 struct lb_priv *lb_priv = get_lb_priv(team);
223 lb_select_tx_port_func_t *select_tx_port_func;
224 struct team_port *port;
226 unsigned int tx_bytes = skb->len;
228 hash = lb_get_skb_hash(lb_priv, skb);
229 select_tx_port_func = rcu_dereference_bh(lb_priv->select_tx_port_func);
230 port = select_tx_port_func(team, lb_priv, skb, hash);
233 if (team_dev_queue_xmit(team, port, skb))
235 lb_update_tx_stats(tx_bytes, lb_priv, get_lb_port_priv(port), hash);
239 dev_kfree_skb_any(skb);
243 static int lb_bpf_func_get(struct team *team, struct team_gsetter_ctx *ctx)
245 struct lb_priv *lb_priv = get_lb_priv(team);
247 if (!lb_priv->ex->orig_fprog) {
248 ctx->data.bin_val.len = 0;
249 ctx->data.bin_val.ptr = NULL;
252 ctx->data.bin_val.len = lb_priv->ex->orig_fprog->len *
253 sizeof(struct sock_filter);
254 ctx->data.bin_val.ptr = lb_priv->ex->orig_fprog->filter;
258 static int __fprog_create(struct sock_fprog_kern **pfprog, u32 data_len,
261 struct sock_fprog_kern *fprog;
262 struct sock_filter *filter = (struct sock_filter *) data;
264 if (data_len % sizeof(struct sock_filter))
266 fprog = kmalloc(sizeof(*fprog), GFP_KERNEL);
269 fprog->filter = kmemdup(filter, data_len, GFP_KERNEL);
270 if (!fprog->filter) {
274 fprog->len = data_len / sizeof(struct sock_filter);
279 static void __fprog_destroy(struct sock_fprog_kern *fprog)
281 kfree(fprog->filter);
285 static int lb_bpf_func_set(struct team *team, struct team_gsetter_ctx *ctx)
287 struct lb_priv *lb_priv = get_lb_priv(team);
288 struct bpf_prog *fp = NULL;
289 struct bpf_prog *orig_fp = NULL;
290 struct sock_fprog_kern *fprog = NULL;
293 if (ctx->data.bin_val.len) {
294 err = __fprog_create(&fprog, ctx->data.bin_val.len,
295 ctx->data.bin_val.ptr);
298 err = bpf_prog_create(&fp, fprog);
300 __fprog_destroy(fprog);
305 if (lb_priv->ex->orig_fprog) {
306 /* Clear old filter data */
307 __fprog_destroy(lb_priv->ex->orig_fprog);
308 orig_fp = rcu_dereference_protected(lb_priv->fp,
309 lockdep_is_held(&team->lock));
312 rcu_assign_pointer(lb_priv->fp, fp);
313 lb_priv->ex->orig_fprog = fprog;
317 bpf_prog_destroy(orig_fp);
322 static void lb_bpf_func_free(struct team *team)
324 struct lb_priv *lb_priv = get_lb_priv(team);
327 if (!lb_priv->ex->orig_fprog)
330 __fprog_destroy(lb_priv->ex->orig_fprog);
331 fp = rcu_dereference_protected(lb_priv->fp,
332 lockdep_is_held(&team->lock));
333 bpf_prog_destroy(fp);
336 static int lb_tx_method_get(struct team *team, struct team_gsetter_ctx *ctx)
338 struct lb_priv *lb_priv = get_lb_priv(team);
339 lb_select_tx_port_func_t *func;
342 func = rcu_dereference_protected(lb_priv->select_tx_port_func,
343 lockdep_is_held(&team->lock));
344 name = lb_select_tx_port_get_name(func);
346 ctx->data.str_val = name;
350 static int lb_tx_method_set(struct team *team, struct team_gsetter_ctx *ctx)
352 struct lb_priv *lb_priv = get_lb_priv(team);
353 lb_select_tx_port_func_t *func;
355 func = lb_select_tx_port_get_func(ctx->data.str_val);
358 rcu_assign_pointer(lb_priv->select_tx_port_func, func);
362 static int lb_tx_hash_to_port_mapping_init(struct team *team,
363 struct team_option_inst_info *info)
365 struct lb_priv *lb_priv = get_lb_priv(team);
366 unsigned char hash = info->array_index;
368 LB_HTPM_OPT_INST_INFO_BY_HASH(lb_priv, hash) = info;
372 static int lb_tx_hash_to_port_mapping_get(struct team *team,
373 struct team_gsetter_ctx *ctx)
375 struct lb_priv *lb_priv = get_lb_priv(team);
376 struct team_port *port;
377 unsigned char hash = ctx->info->array_index;
379 port = LB_HTPM_PORT_BY_HASH(lb_priv, hash);
380 ctx->data.u32_val = port ? port->dev->ifindex : 0;
384 static int lb_tx_hash_to_port_mapping_set(struct team *team,
385 struct team_gsetter_ctx *ctx)
387 struct lb_priv *lb_priv = get_lb_priv(team);
388 struct team_port *port;
389 unsigned char hash = ctx->info->array_index;
391 list_for_each_entry(port, &team->port_list, list) {
392 if (ctx->data.u32_val == port->dev->ifindex &&
393 team_port_enabled(port)) {
394 rcu_assign_pointer(LB_HTPM_PORT_BY_HASH(lb_priv, hash),
402 static int lb_hash_stats_init(struct team *team,
403 struct team_option_inst_info *info)
405 struct lb_priv *lb_priv = get_lb_priv(team);
406 unsigned char hash = info->array_index;
408 lb_priv->ex->stats.info[hash].opt_inst_info = info;
412 static int lb_hash_stats_get(struct team *team, struct team_gsetter_ctx *ctx)
414 struct lb_priv *lb_priv = get_lb_priv(team);
415 unsigned char hash = ctx->info->array_index;
417 ctx->data.bin_val.ptr = &lb_priv->ex->stats.info[hash].stats;
418 ctx->data.bin_val.len = sizeof(struct lb_stats);
422 static int lb_port_stats_init(struct team *team,
423 struct team_option_inst_info *info)
425 struct team_port *port = info->port;
426 struct lb_port_priv *lb_port_priv = get_lb_port_priv(port);
428 lb_port_priv->stats_info.opt_inst_info = info;
432 static int lb_port_stats_get(struct team *team, struct team_gsetter_ctx *ctx)
434 struct team_port *port = ctx->info->port;
435 struct lb_port_priv *lb_port_priv = get_lb_port_priv(port);
437 ctx->data.bin_val.ptr = &lb_port_priv->stats_info.stats;
438 ctx->data.bin_val.len = sizeof(struct lb_stats);
442 static void __lb_stats_info_refresh_prepare(struct lb_stats_info *s_info)
444 memcpy(&s_info->last_stats, &s_info->stats, sizeof(struct lb_stats));
445 memset(&s_info->stats, 0, sizeof(struct lb_stats));
448 static bool __lb_stats_info_refresh_check(struct lb_stats_info *s_info,
451 if (memcmp(&s_info->last_stats, &s_info->stats,
452 sizeof(struct lb_stats))) {
453 team_option_inst_set_change(s_info->opt_inst_info);
459 static void __lb_one_cpu_stats_add(struct lb_stats *acc_stats,
460 struct lb_stats *cpu_stats,
461 struct u64_stats_sync *syncp)
467 start = u64_stats_fetch_begin_irq(syncp);
468 tmp.tx_bytes = cpu_stats->tx_bytes;
469 } while (u64_stats_fetch_retry_irq(syncp, start));
470 acc_stats->tx_bytes += tmp.tx_bytes;
473 static void lb_stats_refresh(struct work_struct *work)
476 struct lb_priv *lb_priv;
477 struct lb_priv_ex *lb_priv_ex;
478 struct lb_pcpu_stats *pcpu_stats;
479 struct lb_stats *stats;
480 struct lb_stats_info *s_info;
481 struct team_port *port;
482 bool changed = false;
486 lb_priv_ex = container_of(work, struct lb_priv_ex,
487 stats.refresh_dw.work);
489 team = lb_priv_ex->team;
490 lb_priv = get_lb_priv(team);
492 if (!mutex_trylock(&team->lock)) {
493 schedule_delayed_work(&lb_priv_ex->stats.refresh_dw, 0);
497 for (j = 0; j < LB_TX_HASHTABLE_SIZE; j++) {
498 s_info = &lb_priv->ex->stats.info[j];
499 __lb_stats_info_refresh_prepare(s_info);
500 for_each_possible_cpu(i) {
501 pcpu_stats = per_cpu_ptr(lb_priv->pcpu_stats, i);
502 stats = &pcpu_stats->hash_stats[j];
503 __lb_one_cpu_stats_add(&s_info->stats, stats,
506 changed |= __lb_stats_info_refresh_check(s_info, team);
509 list_for_each_entry(port, &team->port_list, list) {
510 struct lb_port_priv *lb_port_priv = get_lb_port_priv(port);
512 s_info = &lb_port_priv->stats_info;
513 __lb_stats_info_refresh_prepare(s_info);
514 for_each_possible_cpu(i) {
515 pcpu_stats = per_cpu_ptr(lb_priv->pcpu_stats, i);
516 stats = per_cpu_ptr(lb_port_priv->pcpu_stats, i);
517 __lb_one_cpu_stats_add(&s_info->stats, stats,
520 changed |= __lb_stats_info_refresh_check(s_info, team);
524 team_options_change_check(team);
526 schedule_delayed_work(&lb_priv_ex->stats.refresh_dw,
527 (lb_priv_ex->stats.refresh_interval * HZ) / 10);
529 mutex_unlock(&team->lock);
532 static int lb_stats_refresh_interval_get(struct team *team,
533 struct team_gsetter_ctx *ctx)
535 struct lb_priv *lb_priv = get_lb_priv(team);
537 ctx->data.u32_val = lb_priv->ex->stats.refresh_interval;
541 static int lb_stats_refresh_interval_set(struct team *team,
542 struct team_gsetter_ctx *ctx)
544 struct lb_priv *lb_priv = get_lb_priv(team);
545 unsigned int interval;
547 interval = ctx->data.u32_val;
548 if (lb_priv->ex->stats.refresh_interval == interval)
550 lb_priv->ex->stats.refresh_interval = interval;
552 schedule_delayed_work(&lb_priv->ex->stats.refresh_dw, 0);
554 cancel_delayed_work(&lb_priv->ex->stats.refresh_dw);
558 static const struct team_option lb_options[] = {
560 .name = "bpf_hash_func",
561 .type = TEAM_OPTION_TYPE_BINARY,
562 .getter = lb_bpf_func_get,
563 .setter = lb_bpf_func_set,
566 .name = "lb_tx_method",
567 .type = TEAM_OPTION_TYPE_STRING,
568 .getter = lb_tx_method_get,
569 .setter = lb_tx_method_set,
572 .name = "lb_tx_hash_to_port_mapping",
573 .array_size = LB_TX_HASHTABLE_SIZE,
574 .type = TEAM_OPTION_TYPE_U32,
575 .init = lb_tx_hash_to_port_mapping_init,
576 .getter = lb_tx_hash_to_port_mapping_get,
577 .setter = lb_tx_hash_to_port_mapping_set,
580 .name = "lb_hash_stats",
581 .array_size = LB_TX_HASHTABLE_SIZE,
582 .type = TEAM_OPTION_TYPE_BINARY,
583 .init = lb_hash_stats_init,
584 .getter = lb_hash_stats_get,
587 .name = "lb_port_stats",
589 .type = TEAM_OPTION_TYPE_BINARY,
590 .init = lb_port_stats_init,
591 .getter = lb_port_stats_get,
594 .name = "lb_stats_refresh_interval",
595 .type = TEAM_OPTION_TYPE_U32,
596 .getter = lb_stats_refresh_interval_get,
597 .setter = lb_stats_refresh_interval_set,
601 static int lb_init(struct team *team)
603 struct lb_priv *lb_priv = get_lb_priv(team);
604 lb_select_tx_port_func_t *func;
607 /* set default tx port selector */
608 func = lb_select_tx_port_get_func("hash");
610 rcu_assign_pointer(lb_priv->select_tx_port_func, func);
612 lb_priv->ex = kzalloc(sizeof(*lb_priv->ex), GFP_KERNEL);
615 lb_priv->ex->team = team;
617 lb_priv->pcpu_stats = alloc_percpu(struct lb_pcpu_stats);
618 if (!lb_priv->pcpu_stats) {
620 goto err_alloc_pcpu_stats;
623 for_each_possible_cpu(i) {
624 struct lb_pcpu_stats *team_lb_stats;
625 team_lb_stats = per_cpu_ptr(lb_priv->pcpu_stats, i);
626 u64_stats_init(&team_lb_stats->syncp);
630 INIT_DELAYED_WORK(&lb_priv->ex->stats.refresh_dw, lb_stats_refresh);
632 err = team_options_register(team, lb_options, ARRAY_SIZE(lb_options));
634 goto err_options_register;
637 err_options_register:
638 free_percpu(lb_priv->pcpu_stats);
639 err_alloc_pcpu_stats:
644 static void lb_exit(struct team *team)
646 struct lb_priv *lb_priv = get_lb_priv(team);
648 team_options_unregister(team, lb_options,
649 ARRAY_SIZE(lb_options));
650 lb_bpf_func_free(team);
651 cancel_delayed_work_sync(&lb_priv->ex->stats.refresh_dw);
652 free_percpu(lb_priv->pcpu_stats);
656 static int lb_port_enter(struct team *team, struct team_port *port)
658 struct lb_port_priv *lb_port_priv = get_lb_port_priv(port);
660 lb_port_priv->pcpu_stats = alloc_percpu(struct lb_stats);
661 if (!lb_port_priv->pcpu_stats)
666 static void lb_port_leave(struct team *team, struct team_port *port)
668 struct lb_port_priv *lb_port_priv = get_lb_port_priv(port);
670 free_percpu(lb_port_priv->pcpu_stats);
673 static void lb_port_disabled(struct team *team, struct team_port *port)
675 lb_tx_hash_to_port_mapping_null_port(team, port);
678 static const struct team_mode_ops lb_mode_ops = {
681 .port_enter = lb_port_enter,
682 .port_leave = lb_port_leave,
683 .port_disabled = lb_port_disabled,
684 .receive = lb_receive,
685 .transmit = lb_transmit,
688 static const struct team_mode lb_mode = {
689 .kind = "loadbalance",
690 .owner = THIS_MODULE,
691 .priv_size = sizeof(struct lb_priv),
692 .port_priv_size = sizeof(struct lb_port_priv),
694 .lag_tx_type = NETDEV_LAG_TX_TYPE_HASH,
697 static int __init lb_init_module(void)
699 return team_mode_register(&lb_mode);
702 static void __exit lb_cleanup_module(void)
704 team_mode_unregister(&lb_mode);
707 module_init(lb_init_module);
708 module_exit(lb_cleanup_module);
710 MODULE_LICENSE("GPL v2");
711 MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
712 MODULE_DESCRIPTION("Load-balancing mode for team");
713 MODULE_ALIAS_TEAM_MODE("loadbalance");