xref: /linux/drivers/net/team/team_mode_loadbalance.c (revision a508da6cc0093171833efb8376b00473f24221b9)
1 /*
2  * drivers/net/team/team_mode_loadbalance.c - Load-balancing mode for team
3  * Copyright (c) 2012 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/errno.h>
16 #include <linux/netdevice.h>
17 #include <linux/filter.h>
18 #include <linux/if_team.h>
19 
20 struct lb_priv {
21 	struct sk_filter __rcu *fp;
22 	struct sock_fprog *orig_fprog;
23 };
24 
25 static struct lb_priv *lb_priv(struct team *team)
26 {
27 	return (struct lb_priv *) &team->mode_priv;
28 }
29 
30 static bool lb_transmit(struct team *team, struct sk_buff *skb)
31 {
32 	struct sk_filter *fp;
33 	struct team_port *port;
34 	unsigned int hash;
35 	int port_index;
36 
37 	fp = rcu_dereference(lb_priv(team)->fp);
38 	if (unlikely(!fp))
39 		goto drop;
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);
43 	if (unlikely(!port))
44 		goto drop;
45 	skb->dev = port->dev;
46 	if (dev_queue_xmit(skb))
47 		return false;
48 	return true;
49 
50 drop:
51 	dev_kfree_skb_any(skb);
52 	return false;
53 }
54 
55 static int lb_bpf_func_get(struct team *team, struct team_gsetter_ctx *ctx)
56 {
57 	if (!lb_priv(team)->orig_fprog) {
58 		ctx->data.bin_val.len = 0;
59 		ctx->data.bin_val.ptr = NULL;
60 		return 0;
61 	}
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;
65 	return 0;
66 }
67 
68 static int __fprog_create(struct sock_fprog **pfprog, u32 data_len,
69 			  const void *data)
70 {
71 	struct sock_fprog *fprog;
72 	struct sock_filter *filter = (struct sock_filter *) data;
73 
74 	if (data_len % sizeof(struct sock_filter))
75 		return -EINVAL;
76 	fprog = kmalloc(sizeof(struct sock_fprog), GFP_KERNEL);
77 	if (!fprog)
78 		return -ENOMEM;
79 	fprog->filter = kmemdup(filter, data_len, GFP_KERNEL);
80 	if (!fprog->filter) {
81 		kfree(fprog);
82 		return -ENOMEM;
83 	}
84 	fprog->len = data_len / sizeof(struct sock_filter);
85 	*pfprog = fprog;
86 	return 0;
87 }
88 
89 static void __fprog_destroy(struct sock_fprog *fprog)
90 {
91 	kfree(fprog->filter);
92 	kfree(fprog);
93 }
94 
95 static int lb_bpf_func_set(struct team *team, struct team_gsetter_ctx *ctx)
96 {
97 	struct sk_filter *fp = NULL;
98 	struct sock_fprog *fprog = NULL;
99 	int err;
100 
101 	if (ctx->data.bin_val.len) {
102 		err = __fprog_create(&fprog, ctx->data.bin_val.len,
103 				     ctx->data.bin_val.ptr);
104 		if (err)
105 			return err;
106 		err = sk_unattached_filter_create(&fp, fprog);
107 		if (err) {
108 			__fprog_destroy(fprog);
109 			return err;
110 		}
111 	}
112 
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);
117 	}
118 
119 	rcu_assign_pointer(lb_priv(team)->fp, fp);
120 	lb_priv(team)->orig_fprog = fprog;
121 	return 0;
122 }
123 
124 static const struct team_option lb_options[] = {
125 	{
126 		.name = "bpf_hash_func",
127 		.type = TEAM_OPTION_TYPE_BINARY,
128 		.getter = lb_bpf_func_get,
129 		.setter = lb_bpf_func_set,
130 	},
131 };
132 
133 static int lb_init(struct team *team)
134 {
135 	return team_options_register(team, lb_options,
136 				     ARRAY_SIZE(lb_options));
137 }
138 
139 static void lb_exit(struct team *team)
140 {
141 	team_options_unregister(team, lb_options,
142 				ARRAY_SIZE(lb_options));
143 }
144 
145 static const struct team_mode_ops lb_mode_ops = {
146 	.init			= lb_init,
147 	.exit			= lb_exit,
148 	.transmit		= lb_transmit,
149 };
150 
151 static struct team_mode lb_mode = {
152 	.kind		= "loadbalance",
153 	.owner		= THIS_MODULE,
154 	.priv_size	= sizeof(struct lb_priv),
155 	.ops		= &lb_mode_ops,
156 };
157 
158 static int __init lb_init_module(void)
159 {
160 	return team_mode_register(&lb_mode);
161 }
162 
163 static void __exit lb_cleanup_module(void)
164 {
165 	team_mode_unregister(&lb_mode);
166 }
167 
168 module_init(lb_init_module);
169 module_exit(lb_cleanup_module);
170 
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");
175