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/filter.h>
18 #include <linux/if_team.h>
22 typedef struct team_port *lb_select_tx_port_func_t(struct team *,
27 #define LB_TX_HASHTABLE_SIZE 256 /* hash is a char */
33 struct lb_pcpu_stats {
34 struct lb_stats hash_stats[LB_TX_HASHTABLE_SIZE];
35 struct u64_stats_sync syncp;
38 struct lb_stats_info {
39 struct lb_stats stats;
40 struct lb_stats last_stats;
41 struct team_option_inst_info *opt_inst_info;
44 struct lb_port_mapping {
45 struct team_port __rcu *port;
46 struct team_option_inst_info *opt_inst_info;
51 struct lb_port_mapping tx_hash_to_port_mapping[LB_TX_HASHTABLE_SIZE];
52 struct sock_fprog *orig_fprog;
54 unsigned int refresh_interval; /* in tenths of second */
55 struct delayed_work refresh_dw;
56 struct lb_stats_info info[LB_TX_HASHTABLE_SIZE];
61 struct sk_filter __rcu *fp;
62 lb_select_tx_port_func_t __rcu *select_tx_port_func;
63 struct lb_pcpu_stats __percpu *pcpu_stats;
64 struct lb_priv_ex *ex; /* priv extension */
67 static struct lb_priv *get_lb_priv(struct team *team)
69 return (struct lb_priv *) &team->mode_priv;
73 struct lb_stats __percpu *pcpu_stats;
74 struct lb_stats_info stats_info;
77 static struct lb_port_priv *get_lb_port_priv(struct team_port *port)
79 return (struct lb_port_priv *) &port->mode_priv;
82 #define LB_HTPM_PORT_BY_HASH(lp_priv, hash) \
83 (lb_priv)->ex->tx_hash_to_port_mapping[hash].port
85 #define LB_HTPM_OPT_INST_INFO_BY_HASH(lp_priv, hash) \
86 (lb_priv)->ex->tx_hash_to_port_mapping[hash].opt_inst_info
88 static void lb_tx_hash_to_port_mapping_null_port(struct team *team,
89 struct team_port *port)
91 struct lb_priv *lb_priv = get_lb_priv(team);
95 for (i = 0; i < LB_TX_HASHTABLE_SIZE; i++) {
96 struct lb_port_mapping *pm;
98 pm = &lb_priv->ex->tx_hash_to_port_mapping[i];
99 if (rcu_access_pointer(pm->port) == port) {
100 RCU_INIT_POINTER(pm->port, NULL);
101 team_option_inst_set_change(pm->opt_inst_info);
106 team_options_change_check(team);
109 /* Basic tx selection based solely by hash */
110 static struct team_port *lb_hash_select_tx_port(struct team *team,
111 struct lb_priv *lb_priv,
117 port_index = hash % team->en_port_count;
118 return team_get_port_by_index_rcu(team, port_index);
121 /* Hash to port mapping select tx port */
122 static struct team_port *lb_htpm_select_tx_port(struct team *team,
123 struct lb_priv *lb_priv,
127 return rcu_dereference_bh(LB_HTPM_PORT_BY_HASH(lb_priv, hash));
130 struct lb_select_tx_port {
132 lb_select_tx_port_func_t *func;
135 static const struct lb_select_tx_port lb_select_tx_port_list[] = {
138 .func = lb_hash_select_tx_port,
141 .name = "hash_to_port_mapping",
142 .func = lb_htpm_select_tx_port,
145 #define LB_SELECT_TX_PORT_LIST_COUNT ARRAY_SIZE(lb_select_tx_port_list)
147 static char *lb_select_tx_port_get_name(lb_select_tx_port_func_t *func)
151 for (i = 0; i < LB_SELECT_TX_PORT_LIST_COUNT; i++) {
152 const struct lb_select_tx_port *item;
154 item = &lb_select_tx_port_list[i];
155 if (item->func == func)
161 static lb_select_tx_port_func_t *lb_select_tx_port_get_func(const char *name)
165 for (i = 0; i < LB_SELECT_TX_PORT_LIST_COUNT; i++) {
166 const struct lb_select_tx_port *item;
168 item = &lb_select_tx_port_list[i];
169 if (!strcmp(item->name, name))
175 static unsigned int lb_get_skb_hash(struct lb_priv *lb_priv,
178 struct sk_filter *fp;
182 fp = rcu_dereference_bh(lb_priv->fp);
185 lhash = SK_RUN_FILTER(fp, skb);
187 return c[0] ^ c[1] ^ c[2] ^ c[3];
190 static void lb_update_tx_stats(unsigned int tx_bytes, struct lb_priv *lb_priv,
191 struct lb_port_priv *lb_port_priv,
194 struct lb_pcpu_stats *pcpu_stats;
195 struct lb_stats *port_stats;
196 struct lb_stats *hash_stats;
198 pcpu_stats = this_cpu_ptr(lb_priv->pcpu_stats);
199 port_stats = this_cpu_ptr(lb_port_priv->pcpu_stats);
200 hash_stats = &pcpu_stats->hash_stats[hash];
201 u64_stats_update_begin(&pcpu_stats->syncp);
202 port_stats->tx_bytes += tx_bytes;
203 hash_stats->tx_bytes += tx_bytes;
204 u64_stats_update_end(&pcpu_stats->syncp);
207 static bool lb_transmit(struct team *team, struct sk_buff *skb)
209 struct lb_priv *lb_priv = get_lb_priv(team);
210 lb_select_tx_port_func_t *select_tx_port_func;
211 struct team_port *port;
213 unsigned int tx_bytes = skb->len;
215 hash = lb_get_skb_hash(lb_priv, skb);
216 select_tx_port_func = rcu_dereference_bh(lb_priv->select_tx_port_func);
217 port = select_tx_port_func(team, lb_priv, skb, hash);
220 skb->dev = port->dev;
221 if (dev_queue_xmit(skb))
223 lb_update_tx_stats(tx_bytes, lb_priv, get_lb_port_priv(port), hash);
227 dev_kfree_skb_any(skb);
231 static int lb_bpf_func_get(struct team *team, struct team_gsetter_ctx *ctx)
233 struct lb_priv *lb_priv = get_lb_priv(team);
235 if (!lb_priv->ex->orig_fprog) {
236 ctx->data.bin_val.len = 0;
237 ctx->data.bin_val.ptr = NULL;
240 ctx->data.bin_val.len = lb_priv->ex->orig_fprog->len *
241 sizeof(struct sock_filter);
242 ctx->data.bin_val.ptr = lb_priv->ex->orig_fprog->filter;
246 static int __fprog_create(struct sock_fprog **pfprog, u32 data_len,
249 struct sock_fprog *fprog;
250 struct sock_filter *filter = (struct sock_filter *) data;
252 if (data_len % sizeof(struct sock_filter))
254 fprog = kmalloc(sizeof(struct sock_fprog), GFP_KERNEL);
257 fprog->filter = kmemdup(filter, data_len, GFP_KERNEL);
258 if (!fprog->filter) {
262 fprog->len = data_len / sizeof(struct sock_filter);
267 static void __fprog_destroy(struct sock_fprog *fprog)
269 kfree(fprog->filter);
273 static int lb_bpf_func_set(struct team *team, struct team_gsetter_ctx *ctx)
275 struct lb_priv *lb_priv = get_lb_priv(team);
276 struct sk_filter *fp = NULL;
277 struct sk_filter *orig_fp;
278 struct sock_fprog *fprog = NULL;
281 if (ctx->data.bin_val.len) {
282 err = __fprog_create(&fprog, ctx->data.bin_val.len,
283 ctx->data.bin_val.ptr);
286 err = sk_unattached_filter_create(&fp, fprog);
288 __fprog_destroy(fprog);
293 if (lb_priv->ex->orig_fprog) {
294 /* Clear old filter data */
295 __fprog_destroy(lb_priv->ex->orig_fprog);
296 orig_fp = rcu_dereference_protected(lb_priv->fp,
297 lockdep_is_held(&team->lock));
298 sk_unattached_filter_destroy(orig_fp);
301 rcu_assign_pointer(lb_priv->fp, fp);
302 lb_priv->ex->orig_fprog = fprog;
306 static int lb_tx_method_get(struct team *team, struct team_gsetter_ctx *ctx)
308 struct lb_priv *lb_priv = get_lb_priv(team);
309 lb_select_tx_port_func_t *func;
312 func = rcu_dereference_protected(lb_priv->select_tx_port_func,
313 lockdep_is_held(&team->lock));
314 name = lb_select_tx_port_get_name(func);
316 ctx->data.str_val = name;
320 static int lb_tx_method_set(struct team *team, struct team_gsetter_ctx *ctx)
322 struct lb_priv *lb_priv = get_lb_priv(team);
323 lb_select_tx_port_func_t *func;
325 func = lb_select_tx_port_get_func(ctx->data.str_val);
328 rcu_assign_pointer(lb_priv->select_tx_port_func, func);
332 static int lb_tx_hash_to_port_mapping_init(struct team *team,
333 struct team_option_inst_info *info)
335 struct lb_priv *lb_priv = get_lb_priv(team);
336 unsigned char hash = info->array_index;
338 LB_HTPM_OPT_INST_INFO_BY_HASH(lb_priv, hash) = info;
342 static int lb_tx_hash_to_port_mapping_get(struct team *team,
343 struct team_gsetter_ctx *ctx)
345 struct lb_priv *lb_priv = get_lb_priv(team);
346 struct team_port *port;
347 unsigned char hash = ctx->info->array_index;
349 port = LB_HTPM_PORT_BY_HASH(lb_priv, hash);
350 ctx->data.u32_val = port ? port->dev->ifindex : 0;
354 static int lb_tx_hash_to_port_mapping_set(struct team *team,
355 struct team_gsetter_ctx *ctx)
357 struct lb_priv *lb_priv = get_lb_priv(team);
358 struct team_port *port;
359 unsigned char hash = ctx->info->array_index;
361 list_for_each_entry(port, &team->port_list, list) {
362 if (ctx->data.u32_val == port->dev->ifindex &&
363 team_port_enabled(port)) {
364 rcu_assign_pointer(LB_HTPM_PORT_BY_HASH(lb_priv, hash),
372 static int lb_hash_stats_init(struct team *team,
373 struct team_option_inst_info *info)
375 struct lb_priv *lb_priv = get_lb_priv(team);
376 unsigned char hash = info->array_index;
378 lb_priv->ex->stats.info[hash].opt_inst_info = info;
382 static int lb_hash_stats_get(struct team *team, struct team_gsetter_ctx *ctx)
384 struct lb_priv *lb_priv = get_lb_priv(team);
385 unsigned char hash = ctx->info->array_index;
387 ctx->data.bin_val.ptr = &lb_priv->ex->stats.info[hash].stats;
388 ctx->data.bin_val.len = sizeof(struct lb_stats);
392 static int lb_port_stats_init(struct team *team,
393 struct team_option_inst_info *info)
395 struct team_port *port = info->port;
396 struct lb_port_priv *lb_port_priv = get_lb_port_priv(port);
398 lb_port_priv->stats_info.opt_inst_info = info;
402 static int lb_port_stats_get(struct team *team, struct team_gsetter_ctx *ctx)
404 struct team_port *port = ctx->info->port;
405 struct lb_port_priv *lb_port_priv = get_lb_port_priv(port);
407 ctx->data.bin_val.ptr = &lb_port_priv->stats_info.stats;
408 ctx->data.bin_val.len = sizeof(struct lb_stats);
412 static void __lb_stats_info_refresh_prepare(struct lb_stats_info *s_info)
414 memcpy(&s_info->last_stats, &s_info->stats, sizeof(struct lb_stats));
415 memset(&s_info->stats, 0, sizeof(struct lb_stats));
418 static bool __lb_stats_info_refresh_check(struct lb_stats_info *s_info,
421 if (memcmp(&s_info->last_stats, &s_info->stats,
422 sizeof(struct lb_stats))) {
423 team_option_inst_set_change(s_info->opt_inst_info);
429 static void __lb_one_cpu_stats_add(struct lb_stats *acc_stats,
430 struct lb_stats *cpu_stats,
431 struct u64_stats_sync *syncp)
437 start = u64_stats_fetch_begin_bh(syncp);
438 tmp.tx_bytes = cpu_stats->tx_bytes;
439 } while (u64_stats_fetch_retry_bh(syncp, start));
440 acc_stats->tx_bytes += tmp.tx_bytes;
443 static void lb_stats_refresh(struct work_struct *work)
446 struct lb_priv *lb_priv;
447 struct lb_priv_ex *lb_priv_ex;
448 struct lb_pcpu_stats *pcpu_stats;
449 struct lb_stats *stats;
450 struct lb_stats_info *s_info;
451 struct team_port *port;
452 bool changed = false;
456 lb_priv_ex = container_of(work, struct lb_priv_ex,
457 stats.refresh_dw.work);
459 team = lb_priv_ex->team;
460 lb_priv = get_lb_priv(team);
462 if (!mutex_trylock(&team->lock)) {
463 schedule_delayed_work(&lb_priv_ex->stats.refresh_dw, 0);
467 for (j = 0; j < LB_TX_HASHTABLE_SIZE; j++) {
468 s_info = &lb_priv->ex->stats.info[j];
469 __lb_stats_info_refresh_prepare(s_info);
470 for_each_possible_cpu(i) {
471 pcpu_stats = per_cpu_ptr(lb_priv->pcpu_stats, i);
472 stats = &pcpu_stats->hash_stats[j];
473 __lb_one_cpu_stats_add(&s_info->stats, stats,
476 changed |= __lb_stats_info_refresh_check(s_info, team);
479 list_for_each_entry(port, &team->port_list, list) {
480 struct lb_port_priv *lb_port_priv = get_lb_port_priv(port);
482 s_info = &lb_port_priv->stats_info;
483 __lb_stats_info_refresh_prepare(s_info);
484 for_each_possible_cpu(i) {
485 pcpu_stats = per_cpu_ptr(lb_priv->pcpu_stats, i);
486 stats = per_cpu_ptr(lb_port_priv->pcpu_stats, i);
487 __lb_one_cpu_stats_add(&s_info->stats, stats,
490 changed |= __lb_stats_info_refresh_check(s_info, team);
494 team_options_change_check(team);
496 schedule_delayed_work(&lb_priv_ex->stats.refresh_dw,
497 (lb_priv_ex->stats.refresh_interval * HZ) / 10);
499 mutex_unlock(&team->lock);
502 static int lb_stats_refresh_interval_get(struct team *team,
503 struct team_gsetter_ctx *ctx)
505 struct lb_priv *lb_priv = get_lb_priv(team);
507 ctx->data.u32_val = lb_priv->ex->stats.refresh_interval;
511 static int lb_stats_refresh_interval_set(struct team *team,
512 struct team_gsetter_ctx *ctx)
514 struct lb_priv *lb_priv = get_lb_priv(team);
515 unsigned int interval;
517 interval = ctx->data.u32_val;
518 if (lb_priv->ex->stats.refresh_interval == interval)
520 lb_priv->ex->stats.refresh_interval = interval;
522 schedule_delayed_work(&lb_priv->ex->stats.refresh_dw, 0);
524 cancel_delayed_work(&lb_priv->ex->stats.refresh_dw);
528 static const struct team_option lb_options[] = {
530 .name = "bpf_hash_func",
531 .type = TEAM_OPTION_TYPE_BINARY,
532 .getter = lb_bpf_func_get,
533 .setter = lb_bpf_func_set,
536 .name = "lb_tx_method",
537 .type = TEAM_OPTION_TYPE_STRING,
538 .getter = lb_tx_method_get,
539 .setter = lb_tx_method_set,
542 .name = "lb_tx_hash_to_port_mapping",
543 .array_size = LB_TX_HASHTABLE_SIZE,
544 .type = TEAM_OPTION_TYPE_U32,
545 .init = lb_tx_hash_to_port_mapping_init,
546 .getter = lb_tx_hash_to_port_mapping_get,
547 .setter = lb_tx_hash_to_port_mapping_set,
550 .name = "lb_hash_stats",
551 .array_size = LB_TX_HASHTABLE_SIZE,
552 .type = TEAM_OPTION_TYPE_BINARY,
553 .init = lb_hash_stats_init,
554 .getter = lb_hash_stats_get,
557 .name = "lb_port_stats",
559 .type = TEAM_OPTION_TYPE_BINARY,
560 .init = lb_port_stats_init,
561 .getter = lb_port_stats_get,
564 .name = "lb_stats_refresh_interval",
565 .type = TEAM_OPTION_TYPE_U32,
566 .getter = lb_stats_refresh_interval_get,
567 .setter = lb_stats_refresh_interval_set,
571 static int lb_init(struct team *team)
573 struct lb_priv *lb_priv = get_lb_priv(team);
574 lb_select_tx_port_func_t *func;
577 /* set default tx port selector */
578 func = lb_select_tx_port_get_func("hash");
580 rcu_assign_pointer(lb_priv->select_tx_port_func, func);
582 lb_priv->ex = kzalloc(sizeof(*lb_priv->ex), GFP_KERNEL);
585 lb_priv->ex->team = team;
587 lb_priv->pcpu_stats = alloc_percpu(struct lb_pcpu_stats);
588 if (!lb_priv->pcpu_stats) {
590 goto err_alloc_pcpu_stats;
593 INIT_DELAYED_WORK(&lb_priv->ex->stats.refresh_dw, lb_stats_refresh);
595 err = team_options_register(team, lb_options, ARRAY_SIZE(lb_options));
597 goto err_options_register;
600 err_options_register:
601 free_percpu(lb_priv->pcpu_stats);
602 err_alloc_pcpu_stats:
607 static void lb_exit(struct team *team)
609 struct lb_priv *lb_priv = get_lb_priv(team);
611 team_options_unregister(team, lb_options,
612 ARRAY_SIZE(lb_options));
613 cancel_delayed_work_sync(&lb_priv->ex->stats.refresh_dw);
614 free_percpu(lb_priv->pcpu_stats);
618 static int lb_port_enter(struct team *team, struct team_port *port)
620 struct lb_port_priv *lb_port_priv = get_lb_port_priv(port);
622 lb_port_priv->pcpu_stats = alloc_percpu(struct lb_stats);
623 if (!lb_port_priv->pcpu_stats)
628 static void lb_port_leave(struct team *team, struct team_port *port)
630 struct lb_port_priv *lb_port_priv = get_lb_port_priv(port);
632 free_percpu(lb_port_priv->pcpu_stats);
635 static void lb_port_disabled(struct team *team, struct team_port *port)
637 lb_tx_hash_to_port_mapping_null_port(team, port);
640 static const struct team_mode_ops lb_mode_ops = {
643 .port_enter = lb_port_enter,
644 .port_leave = lb_port_leave,
645 .port_disabled = lb_port_disabled,
646 .transmit = lb_transmit,
649 static const struct team_mode lb_mode = {
650 .kind = "loadbalance",
651 .owner = THIS_MODULE,
652 .priv_size = sizeof(struct lb_priv),
653 .port_priv_size = sizeof(struct lb_port_priv),
657 static int __init lb_init_module(void)
659 return team_mode_register(&lb_mode);
662 static void __exit lb_cleanup_module(void)
664 team_mode_unregister(&lb_mode);
667 module_init(lb_init_module);
668 module_exit(lb_cleanup_module);
670 MODULE_LICENSE("GPL v2");
671 MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
672 MODULE_DESCRIPTION("Load-balancing mode for team");
673 MODULE_ALIAS("team-mode-loadbalance");