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
->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
, void *arg
)
57 struct team_option_binary
*tbinary
= team_optarg_tbinary(arg
);
59 memset(tbinary
, 0, sizeof(*tbinary
));
60 if (!lb_priv(team
)->orig_fprog
)
63 tbinary
->data_len
= lb_priv(team
)->orig_fprog
->len
*
64 sizeof(struct sock_filter
);
65 tbinary
->data
= lb_priv(team
)->orig_fprog
->filter
;
69 static int __fprog_create(struct sock_fprog
**pfprog
, u32 data_len
,
72 struct sock_fprog
*fprog
;
73 struct sock_filter
*filter
= (struct sock_filter
*) data
;
75 if (data_len
% sizeof(struct sock_filter
))
77 fprog
= kmalloc(sizeof(struct sock_fprog
), GFP_KERNEL
);
80 fprog
->filter
= kmemdup(filter
, data_len
, GFP_KERNEL
);
85 fprog
->len
= data_len
/ sizeof(struct sock_filter
);
90 static void __fprog_destroy(struct sock_fprog
*fprog
)
96 static int lb_bpf_func_set(struct team
*team
, void *arg
)
98 struct team_option_binary
*tbinary
= team_optarg_tbinary(arg
);
99 struct sk_filter
*fp
= NULL
;
100 struct sock_fprog
*fprog
= NULL
;
103 if (tbinary
->data_len
) {
104 err
= __fprog_create(&fprog
, tbinary
->data_len
,
108 err
= sk_unattached_filter_create(&fp
, fprog
);
110 __fprog_destroy(fprog
);
115 if (lb_priv(team
)->orig_fprog
) {
116 /* Clear old filter data */
117 __fprog_destroy(lb_priv(team
)->orig_fprog
);
118 sk_unattached_filter_destroy(lb_priv(team
)->fp
);
121 rcu_assign_pointer(lb_priv(team
)->fp
, fp
);
122 lb_priv(team
)->orig_fprog
= fprog
;
126 static const struct team_option lb_options
[] = {
128 .name
= "bpf_hash_func",
129 .type
= TEAM_OPTION_TYPE_BINARY
,
130 .getter
= lb_bpf_func_get
,
131 .setter
= lb_bpf_func_set
,
135 int lb_init(struct team
*team
)
137 return team_options_register(team
, lb_options
,
138 ARRAY_SIZE(lb_options
));
141 void lb_exit(struct team
*team
)
143 team_options_unregister(team
, lb_options
,
144 ARRAY_SIZE(lb_options
));
147 static int lb_port_enter(struct team
*team
, struct team_port
*port
)
149 return team_port_set_team_mac(port
);
152 static void lb_port_change_mac(struct team
*team
, struct team_port
*port
)
154 team_port_set_team_mac(port
);
157 static const struct team_mode_ops lb_mode_ops
= {
160 .transmit
= lb_transmit
,
161 .port_enter
= lb_port_enter
,
162 .port_change_mac
= lb_port_change_mac
,
165 static struct team_mode lb_mode
= {
166 .kind
= "loadbalance",
167 .owner
= THIS_MODULE
,
168 .priv_size
= sizeof(struct lb_priv
),
172 static int __init
lb_init_module(void)
174 return team_mode_register(&lb_mode
);
177 static void __exit
lb_cleanup_module(void)
179 team_mode_unregister(&lb_mode
);
182 module_init(lb_init_module
);
183 module_exit(lb_cleanup_module
);
185 MODULE_LICENSE("GPL v2");
186 MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
187 MODULE_DESCRIPTION("Load-balancing mode for team");
188 MODULE_ALIAS("team-mode-loadbalance");