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>
21 struct sk_filter __rcu *fp;
22 struct sock_fprog *orig_fprog;
25 static struct lb_priv *lb_priv(struct team *team)
27 return (struct lb_priv *) &team->mode_priv;
30 static bool lb_transmit(struct team *team, struct sk_buff *skb)
33 struct team_port *port;
37 fp = rcu_dereference(lb_priv(team)->fp);
40 hash = SK_RUN_FILTER(fp, skb);
41 port_index = hash % team->en_port_count;
42 port = team_get_port_by_index_rcu(team, port_index);
46 if (dev_queue_xmit(skb))
51 dev_kfree_skb_any(skb);
55 static int lb_bpf_func_get(struct team *team, struct team_gsetter_ctx *ctx)
57 if (!lb_priv(team)->orig_fprog) {
58 ctx->data.bin_val.len = 0;
59 ctx->data.bin_val.ptr = NULL;
62 ctx->data.bin_val.len = lb_priv(team)->orig_fprog->len *
63 sizeof(struct sock_filter);
64 ctx->data.bin_val.ptr = lb_priv(team)->orig_fprog->filter;
68 static int __fprog_create(struct sock_fprog **pfprog, u32 data_len,
71 struct sock_fprog *fprog;
72 struct sock_filter *filter = (struct sock_filter *) data;
74 if (data_len % sizeof(struct sock_filter))
76 fprog = kmalloc(sizeof(struct sock_fprog), GFP_KERNEL);
79 fprog->filter = kmemdup(filter, data_len, GFP_KERNEL);
84 fprog->len = data_len / sizeof(struct sock_filter);
89 static void __fprog_destroy(struct sock_fprog *fprog)
95 static int lb_bpf_func_set(struct team *team, struct team_gsetter_ctx *ctx)
97 struct sk_filter *fp = NULL;
98 struct sock_fprog *fprog = NULL;
101 if (ctx->data.bin_val.len) {
102 err = __fprog_create(&fprog, ctx->data.bin_val.len,
103 ctx->data.bin_val.ptr);
106 err = sk_unattached_filter_create(&fp, fprog);
108 __fprog_destroy(fprog);
113 if (lb_priv(team)->orig_fprog) {
114 /* Clear old filter data */
115 __fprog_destroy(lb_priv(team)->orig_fprog);
116 sk_unattached_filter_destroy(lb_priv(team)->fp);
119 rcu_assign_pointer(lb_priv(team)->fp, fp);
120 lb_priv(team)->orig_fprog = fprog;
124 static const struct team_option lb_options[] = {
126 .name = "bpf_hash_func",
127 .type = TEAM_OPTION_TYPE_BINARY,
128 .getter = lb_bpf_func_get,
129 .setter = lb_bpf_func_set,
133 static int lb_init(struct team *team)
135 return team_options_register(team, lb_options,
136 ARRAY_SIZE(lb_options));
139 static void lb_exit(struct team *team)
141 team_options_unregister(team, lb_options,
142 ARRAY_SIZE(lb_options));
145 static const struct team_mode_ops lb_mode_ops = {
148 .transmit = lb_transmit,
151 static struct team_mode lb_mode = {
152 .kind = "loadbalance",
153 .owner = THIS_MODULE,
154 .priv_size = sizeof(struct lb_priv),
158 static int __init lb_init_module(void)
160 return team_mode_register(&lb_mode);
163 static void __exit lb_cleanup_module(void)
165 team_mode_unregister(&lb_mode);
168 module_init(lb_init_module);
169 module_exit(lb_cleanup_module);
171 MODULE_LICENSE("GPL v2");
172 MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
173 MODULE_DESCRIPTION("Load-balancing mode for team");
174 MODULE_ALIAS("team-mode-loadbalance");