Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 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 | |
Jiri Pirko | 596e202 | 2012-06-19 05:54:06 +0000 | [diff] [blame^] | 30 | static unsigned char lb_get_skb_hash(struct lb_priv *lb_priv, |
| 31 | struct sk_buff *skb) |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 32 | { |
| 33 | struct sk_filter *fp; |
Jiri Pirko | 596e202 | 2012-06-19 05:54:06 +0000 | [diff] [blame^] | 34 | uint32_t lhash; |
| 35 | unsigned char *c; |
| 36 | |
| 37 | fp = rcu_dereference(lb_priv->fp); |
| 38 | if (unlikely(!fp)) |
| 39 | return 0; |
| 40 | lhash = SK_RUN_FILTER(fp, skb); |
| 41 | c = (char *) &lhash; |
| 42 | return c[0] ^ c[1] ^ c[2] ^ c[3]; |
| 43 | } |
| 44 | |
| 45 | static bool lb_transmit(struct team *team, struct sk_buff *skb) |
| 46 | { |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 47 | struct team_port *port; |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 48 | int port_index; |
| 49 | |
Jiri Pirko | 596e202 | 2012-06-19 05:54:06 +0000 | [diff] [blame^] | 50 | port_index = lb_get_skb_hash(lb_priv(team), skb) % team->en_port_count; |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 51 | port = team_get_port_by_index_rcu(team, port_index); |
| 52 | if (unlikely(!port)) |
| 53 | goto drop; |
| 54 | skb->dev = port->dev; |
| 55 | if (dev_queue_xmit(skb)) |
| 56 | return false; |
| 57 | return true; |
| 58 | |
| 59 | drop: |
| 60 | dev_kfree_skb_any(skb); |
| 61 | return false; |
| 62 | } |
| 63 | |
Jiri Pirko | 80f7c66 | 2012-04-10 05:15:42 +0000 | [diff] [blame] | 64 | static int lb_bpf_func_get(struct team *team, struct team_gsetter_ctx *ctx) |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 65 | { |
Jiri Pirko | 80f7c66 | 2012-04-10 05:15:42 +0000 | [diff] [blame] | 66 | if (!lb_priv(team)->orig_fprog) { |
| 67 | ctx->data.bin_val.len = 0; |
| 68 | ctx->data.bin_val.ptr = NULL; |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 69 | return 0; |
Jiri Pirko | 80f7c66 | 2012-04-10 05:15:42 +0000 | [diff] [blame] | 70 | } |
| 71 | ctx->data.bin_val.len = lb_priv(team)->orig_fprog->len * |
| 72 | sizeof(struct sock_filter); |
| 73 | ctx->data.bin_val.ptr = lb_priv(team)->orig_fprog->filter; |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | static int __fprog_create(struct sock_fprog **pfprog, u32 data_len, |
Jiri Pirko | 80f7c66 | 2012-04-10 05:15:42 +0000 | [diff] [blame] | 78 | const void *data) |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 79 | { |
| 80 | struct sock_fprog *fprog; |
| 81 | struct sock_filter *filter = (struct sock_filter *) data; |
| 82 | |
| 83 | if (data_len % sizeof(struct sock_filter)) |
| 84 | return -EINVAL; |
| 85 | fprog = kmalloc(sizeof(struct sock_fprog), GFP_KERNEL); |
| 86 | if (!fprog) |
| 87 | return -ENOMEM; |
| 88 | fprog->filter = kmemdup(filter, data_len, GFP_KERNEL); |
| 89 | if (!fprog->filter) { |
| 90 | kfree(fprog); |
| 91 | return -ENOMEM; |
| 92 | } |
| 93 | fprog->len = data_len / sizeof(struct sock_filter); |
| 94 | *pfprog = fprog; |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | static void __fprog_destroy(struct sock_fprog *fprog) |
| 99 | { |
| 100 | kfree(fprog->filter); |
| 101 | kfree(fprog); |
| 102 | } |
| 103 | |
Jiri Pirko | 80f7c66 | 2012-04-10 05:15:42 +0000 | [diff] [blame] | 104 | static int lb_bpf_func_set(struct team *team, struct team_gsetter_ctx *ctx) |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 105 | { |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 106 | struct sk_filter *fp = NULL; |
| 107 | struct sock_fprog *fprog = NULL; |
| 108 | int err; |
| 109 | |
Jiri Pirko | 80f7c66 | 2012-04-10 05:15:42 +0000 | [diff] [blame] | 110 | if (ctx->data.bin_val.len) { |
| 111 | err = __fprog_create(&fprog, ctx->data.bin_val.len, |
| 112 | ctx->data.bin_val.ptr); |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 113 | if (err) |
| 114 | return err; |
| 115 | err = sk_unattached_filter_create(&fp, fprog); |
| 116 | if (err) { |
| 117 | __fprog_destroy(fprog); |
| 118 | return err; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | if (lb_priv(team)->orig_fprog) { |
| 123 | /* Clear old filter data */ |
| 124 | __fprog_destroy(lb_priv(team)->orig_fprog); |
| 125 | sk_unattached_filter_destroy(lb_priv(team)->fp); |
| 126 | } |
| 127 | |
| 128 | rcu_assign_pointer(lb_priv(team)->fp, fp); |
| 129 | lb_priv(team)->orig_fprog = fprog; |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | static const struct team_option lb_options[] = { |
| 134 | { |
| 135 | .name = "bpf_hash_func", |
| 136 | .type = TEAM_OPTION_TYPE_BINARY, |
| 137 | .getter = lb_bpf_func_get, |
| 138 | .setter = lb_bpf_func_set, |
| 139 | }, |
| 140 | }; |
| 141 | |
Jiri Pirko | cade455 | 2012-04-10 05:15:46 +0000 | [diff] [blame] | 142 | static int lb_init(struct team *team) |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 143 | { |
| 144 | return team_options_register(team, lb_options, |
| 145 | ARRAY_SIZE(lb_options)); |
| 146 | } |
| 147 | |
Jiri Pirko | cade455 | 2012-04-10 05:15:46 +0000 | [diff] [blame] | 148 | static void lb_exit(struct team *team) |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 149 | { |
| 150 | team_options_unregister(team, lb_options, |
| 151 | ARRAY_SIZE(lb_options)); |
| 152 | } |
| 153 | |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 154 | static const struct team_mode_ops lb_mode_ops = { |
| 155 | .init = lb_init, |
| 156 | .exit = lb_exit, |
| 157 | .transmit = lb_transmit, |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 158 | }; |
| 159 | |
Jiri Pirko | 0402788 | 2012-06-19 05:54:03 +0000 | [diff] [blame] | 160 | static const struct team_mode lb_mode = { |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 161 | .kind = "loadbalance", |
| 162 | .owner = THIS_MODULE, |
| 163 | .priv_size = sizeof(struct lb_priv), |
| 164 | .ops = &lb_mode_ops, |
| 165 | }; |
| 166 | |
| 167 | static int __init lb_init_module(void) |
| 168 | { |
| 169 | return team_mode_register(&lb_mode); |
| 170 | } |
| 171 | |
| 172 | static void __exit lb_cleanup_module(void) |
| 173 | { |
| 174 | team_mode_unregister(&lb_mode); |
| 175 | } |
| 176 | |
| 177 | module_init(lb_init_module); |
| 178 | module_exit(lb_cleanup_module); |
| 179 | |
| 180 | MODULE_LICENSE("GPL v2"); |
| 181 | MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>"); |
| 182 | MODULE_DESCRIPTION("Load-balancing mode for team"); |
| 183 | MODULE_ALIAS("team-mode-loadbalance"); |