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> |
Jiri Pirko | c15e07b | 2016-08-25 18:30:52 +0200 | [diff] [blame] | 17 | #include <linux/etherdevice.h> |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 18 | #include <linux/filter.h> |
| 19 | #include <linux/if_team.h> |
| 20 | |
Jiri Pirko | c15e07b | 2016-08-25 18:30:52 +0200 | [diff] [blame] | 21 | static rx_handler_result_t lb_receive(struct team *team, struct team_port *port, |
| 22 | struct sk_buff *skb) |
| 23 | { |
| 24 | if (unlikely(skb->protocol == htons(ETH_P_SLOW))) { |
| 25 | /* LACPDU packets should go to exact delivery */ |
| 26 | const unsigned char *dest = eth_hdr(skb)->h_dest; |
| 27 | |
| 28 | if (is_link_local_ether_addr(dest) && dest[5] == 0x02) |
| 29 | return RX_HANDLER_EXACT; |
| 30 | } |
| 31 | return RX_HANDLER_ANOTHER; |
| 32 | } |
| 33 | |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 34 | struct lb_priv; |
| 35 | |
| 36 | typedef struct team_port *lb_select_tx_port_func_t(struct team *, |
| 37 | struct lb_priv *, |
| 38 | struct sk_buff *, |
| 39 | unsigned char); |
| 40 | |
| 41 | #define LB_TX_HASHTABLE_SIZE 256 /* hash is a char */ |
| 42 | |
| 43 | struct lb_stats { |
| 44 | u64 tx_bytes; |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 47 | struct lb_pcpu_stats { |
| 48 | struct lb_stats hash_stats[LB_TX_HASHTABLE_SIZE]; |
| 49 | struct u64_stats_sync syncp; |
| 50 | }; |
| 51 | |
| 52 | struct lb_stats_info { |
| 53 | struct lb_stats stats; |
| 54 | struct lb_stats last_stats; |
| 55 | struct team_option_inst_info *opt_inst_info; |
| 56 | }; |
| 57 | |
| 58 | struct lb_port_mapping { |
| 59 | struct team_port __rcu *port; |
| 60 | struct team_option_inst_info *opt_inst_info; |
| 61 | }; |
| 62 | |
| 63 | struct lb_priv_ex { |
| 64 | struct team *team; |
| 65 | struct lb_port_mapping tx_hash_to_port_mapping[LB_TX_HASHTABLE_SIZE]; |
Daniel Borkmann | b1fcd35 | 2014-05-23 18:43:58 +0200 | [diff] [blame] | 66 | struct sock_fprog_kern *orig_fprog; |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 67 | struct { |
| 68 | unsigned int refresh_interval; /* in tenths of second */ |
| 69 | struct delayed_work refresh_dw; |
| 70 | struct lb_stats_info info[LB_TX_HASHTABLE_SIZE]; |
| 71 | } stats; |
| 72 | }; |
| 73 | |
| 74 | struct lb_priv { |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 75 | struct bpf_prog __rcu *fp; |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 76 | lb_select_tx_port_func_t __rcu *select_tx_port_func; |
| 77 | struct lb_pcpu_stats __percpu *pcpu_stats; |
| 78 | struct lb_priv_ex *ex; /* priv extension */ |
| 79 | }; |
| 80 | |
| 81 | static struct lb_priv *get_lb_priv(struct team *team) |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 82 | { |
| 83 | return (struct lb_priv *) &team->mode_priv; |
| 84 | } |
| 85 | |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 86 | struct lb_port_priv { |
| 87 | struct lb_stats __percpu *pcpu_stats; |
| 88 | struct lb_stats_info stats_info; |
| 89 | }; |
| 90 | |
| 91 | static struct lb_port_priv *get_lb_port_priv(struct team_port *port) |
| 92 | { |
| 93 | return (struct lb_port_priv *) &port->mode_priv; |
| 94 | } |
| 95 | |
| 96 | #define LB_HTPM_PORT_BY_HASH(lp_priv, hash) \ |
| 97 | (lb_priv)->ex->tx_hash_to_port_mapping[hash].port |
| 98 | |
| 99 | #define LB_HTPM_OPT_INST_INFO_BY_HASH(lp_priv, hash) \ |
| 100 | (lb_priv)->ex->tx_hash_to_port_mapping[hash].opt_inst_info |
| 101 | |
| 102 | static void lb_tx_hash_to_port_mapping_null_port(struct team *team, |
| 103 | struct team_port *port) |
| 104 | { |
| 105 | struct lb_priv *lb_priv = get_lb_priv(team); |
| 106 | bool changed = false; |
| 107 | int i; |
| 108 | |
| 109 | for (i = 0; i < LB_TX_HASHTABLE_SIZE; i++) { |
| 110 | struct lb_port_mapping *pm; |
| 111 | |
| 112 | pm = &lb_priv->ex->tx_hash_to_port_mapping[i]; |
Jiri Pirko | 6dab015 | 2012-06-20 08:39:39 +0000 | [diff] [blame] | 113 | if (rcu_access_pointer(pm->port) == port) { |
| 114 | RCU_INIT_POINTER(pm->port, NULL); |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 115 | team_option_inst_set_change(pm->opt_inst_info); |
| 116 | changed = true; |
| 117 | } |
| 118 | } |
| 119 | if (changed) |
| 120 | team_options_change_check(team); |
| 121 | } |
| 122 | |
| 123 | /* Basic tx selection based solely by hash */ |
| 124 | static struct team_port *lb_hash_select_tx_port(struct team *team, |
| 125 | struct lb_priv *lb_priv, |
| 126 | struct sk_buff *skb, |
| 127 | unsigned char hash) |
| 128 | { |
Jiri Pirko | 735d381 | 2013-06-10 17:42:25 +0200 | [diff] [blame] | 129 | int port_index = team_num_to_port_index(team, hash); |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 130 | |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 131 | return team_get_port_by_index_rcu(team, port_index); |
| 132 | } |
| 133 | |
| 134 | /* Hash to port mapping select tx port */ |
| 135 | static struct team_port *lb_htpm_select_tx_port(struct team *team, |
| 136 | struct lb_priv *lb_priv, |
| 137 | struct sk_buff *skb, |
| 138 | unsigned char hash) |
| 139 | { |
Jiri Pirko | d1904fb | 2012-06-19 05:54:21 +0000 | [diff] [blame] | 140 | return rcu_dereference_bh(LB_HTPM_PORT_BY_HASH(lb_priv, hash)); |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | struct lb_select_tx_port { |
| 144 | char *name; |
| 145 | lb_select_tx_port_func_t *func; |
| 146 | }; |
| 147 | |
| 148 | static const struct lb_select_tx_port lb_select_tx_port_list[] = { |
| 149 | { |
| 150 | .name = "hash", |
| 151 | .func = lb_hash_select_tx_port, |
| 152 | }, |
| 153 | { |
| 154 | .name = "hash_to_port_mapping", |
| 155 | .func = lb_htpm_select_tx_port, |
| 156 | }, |
| 157 | }; |
| 158 | #define LB_SELECT_TX_PORT_LIST_COUNT ARRAY_SIZE(lb_select_tx_port_list) |
| 159 | |
| 160 | static char *lb_select_tx_port_get_name(lb_select_tx_port_func_t *func) |
| 161 | { |
| 162 | int i; |
| 163 | |
| 164 | for (i = 0; i < LB_SELECT_TX_PORT_LIST_COUNT; i++) { |
| 165 | const struct lb_select_tx_port *item; |
| 166 | |
| 167 | item = &lb_select_tx_port_list[i]; |
| 168 | if (item->func == func) |
| 169 | return item->name; |
| 170 | } |
| 171 | return NULL; |
| 172 | } |
| 173 | |
| 174 | static lb_select_tx_port_func_t *lb_select_tx_port_get_func(const char *name) |
| 175 | { |
| 176 | int i; |
| 177 | |
| 178 | for (i = 0; i < LB_SELECT_TX_PORT_LIST_COUNT; i++) { |
| 179 | const struct lb_select_tx_port *item; |
| 180 | |
| 181 | item = &lb_select_tx_port_list[i]; |
| 182 | if (!strcmp(item->name, name)) |
| 183 | return item->func; |
| 184 | } |
| 185 | return NULL; |
| 186 | } |
| 187 | |
| 188 | static unsigned int lb_get_skb_hash(struct lb_priv *lb_priv, |
| 189 | struct sk_buff *skb) |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 190 | { |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 191 | struct bpf_prog *fp; |
Jiri Pirko | 596e202 | 2012-06-19 05:54:06 +0000 | [diff] [blame] | 192 | uint32_t lhash; |
| 193 | unsigned char *c; |
| 194 | |
Jiri Pirko | d1904fb | 2012-06-19 05:54:21 +0000 | [diff] [blame] | 195 | fp = rcu_dereference_bh(lb_priv->fp); |
Jiri Pirko | 596e202 | 2012-06-19 05:54:06 +0000 | [diff] [blame] | 196 | if (unlikely(!fp)) |
| 197 | return 0; |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 198 | lhash = BPF_PROG_RUN(fp, skb); |
Jiri Pirko | 596e202 | 2012-06-19 05:54:06 +0000 | [diff] [blame] | 199 | c = (char *) &lhash; |
| 200 | return c[0] ^ c[1] ^ c[2] ^ c[3]; |
| 201 | } |
| 202 | |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 203 | static void lb_update_tx_stats(unsigned int tx_bytes, struct lb_priv *lb_priv, |
| 204 | struct lb_port_priv *lb_port_priv, |
| 205 | unsigned char hash) |
| 206 | { |
| 207 | struct lb_pcpu_stats *pcpu_stats; |
| 208 | struct lb_stats *port_stats; |
| 209 | struct lb_stats *hash_stats; |
| 210 | |
| 211 | pcpu_stats = this_cpu_ptr(lb_priv->pcpu_stats); |
| 212 | port_stats = this_cpu_ptr(lb_port_priv->pcpu_stats); |
| 213 | hash_stats = &pcpu_stats->hash_stats[hash]; |
| 214 | u64_stats_update_begin(&pcpu_stats->syncp); |
| 215 | port_stats->tx_bytes += tx_bytes; |
| 216 | hash_stats->tx_bytes += tx_bytes; |
| 217 | u64_stats_update_end(&pcpu_stats->syncp); |
| 218 | } |
| 219 | |
Jiri Pirko | 596e202 | 2012-06-19 05:54:06 +0000 | [diff] [blame] | 220 | static bool lb_transmit(struct team *team, struct sk_buff *skb) |
| 221 | { |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 222 | struct lb_priv *lb_priv = get_lb_priv(team); |
| 223 | lb_select_tx_port_func_t *select_tx_port_func; |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 224 | struct team_port *port; |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 225 | unsigned char hash; |
| 226 | unsigned int tx_bytes = skb->len; |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 227 | |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 228 | hash = lb_get_skb_hash(lb_priv, skb); |
Jiri Pirko | d1904fb | 2012-06-19 05:54:21 +0000 | [diff] [blame] | 229 | select_tx_port_func = rcu_dereference_bh(lb_priv->select_tx_port_func); |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 230 | port = select_tx_port_func(team, lb_priv, skb, hash); |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 231 | if (unlikely(!port)) |
| 232 | goto drop; |
Jiri Pirko | bd2d083 | 2012-07-17 05:22:36 +0000 | [diff] [blame] | 233 | if (team_dev_queue_xmit(team, port, skb)) |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 234 | return false; |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 235 | lb_update_tx_stats(tx_bytes, lb_priv, get_lb_port_priv(port), hash); |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 236 | return true; |
| 237 | |
| 238 | drop: |
| 239 | dev_kfree_skb_any(skb); |
| 240 | return false; |
| 241 | } |
| 242 | |
Jiri Pirko | 80f7c66 | 2012-04-10 05:15:42 +0000 | [diff] [blame] | 243 | 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] | 244 | { |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 245 | struct lb_priv *lb_priv = get_lb_priv(team); |
| 246 | |
| 247 | if (!lb_priv->ex->orig_fprog) { |
Jiri Pirko | 80f7c66 | 2012-04-10 05:15:42 +0000 | [diff] [blame] | 248 | ctx->data.bin_val.len = 0; |
| 249 | ctx->data.bin_val.ptr = NULL; |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 250 | return 0; |
Jiri Pirko | 80f7c66 | 2012-04-10 05:15:42 +0000 | [diff] [blame] | 251 | } |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 252 | ctx->data.bin_val.len = lb_priv->ex->orig_fprog->len * |
Jiri Pirko | 80f7c66 | 2012-04-10 05:15:42 +0000 | [diff] [blame] | 253 | sizeof(struct sock_filter); |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 254 | ctx->data.bin_val.ptr = lb_priv->ex->orig_fprog->filter; |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 255 | return 0; |
| 256 | } |
| 257 | |
Daniel Borkmann | b1fcd35 | 2014-05-23 18:43:58 +0200 | [diff] [blame] | 258 | static int __fprog_create(struct sock_fprog_kern **pfprog, u32 data_len, |
Jiri Pirko | 80f7c66 | 2012-04-10 05:15:42 +0000 | [diff] [blame] | 259 | const void *data) |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 260 | { |
Daniel Borkmann | b1fcd35 | 2014-05-23 18:43:58 +0200 | [diff] [blame] | 261 | struct sock_fprog_kern *fprog; |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 262 | struct sock_filter *filter = (struct sock_filter *) data; |
| 263 | |
| 264 | if (data_len % sizeof(struct sock_filter)) |
| 265 | return -EINVAL; |
Daniel Borkmann | ea5930f | 2014-05-24 21:47:46 +0200 | [diff] [blame] | 266 | fprog = kmalloc(sizeof(*fprog), GFP_KERNEL); |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 267 | if (!fprog) |
| 268 | return -ENOMEM; |
| 269 | fprog->filter = kmemdup(filter, data_len, GFP_KERNEL); |
| 270 | if (!fprog->filter) { |
| 271 | kfree(fprog); |
| 272 | return -ENOMEM; |
| 273 | } |
| 274 | fprog->len = data_len / sizeof(struct sock_filter); |
| 275 | *pfprog = fprog; |
| 276 | return 0; |
| 277 | } |
| 278 | |
Daniel Borkmann | b1fcd35 | 2014-05-23 18:43:58 +0200 | [diff] [blame] | 279 | static void __fprog_destroy(struct sock_fprog_kern *fprog) |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 280 | { |
| 281 | kfree(fprog->filter); |
| 282 | kfree(fprog); |
| 283 | } |
| 284 | |
Jiri Pirko | 80f7c66 | 2012-04-10 05:15:42 +0000 | [diff] [blame] | 285 | 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] | 286 | { |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 287 | struct lb_priv *lb_priv = get_lb_priv(team); |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 288 | struct bpf_prog *fp = NULL; |
| 289 | struct bpf_prog *orig_fp = NULL; |
Daniel Borkmann | b1fcd35 | 2014-05-23 18:43:58 +0200 | [diff] [blame] | 290 | struct sock_fprog_kern *fprog = NULL; |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 291 | int err; |
| 292 | |
Jiri Pirko | 80f7c66 | 2012-04-10 05:15:42 +0000 | [diff] [blame] | 293 | if (ctx->data.bin_val.len) { |
| 294 | err = __fprog_create(&fprog, ctx->data.bin_val.len, |
| 295 | ctx->data.bin_val.ptr); |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 296 | if (err) |
| 297 | return err; |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 298 | err = bpf_prog_create(&fp, fprog); |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 299 | if (err) { |
| 300 | __fprog_destroy(fprog); |
| 301 | return err; |
| 302 | } |
| 303 | } |
| 304 | |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 305 | if (lb_priv->ex->orig_fprog) { |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 306 | /* Clear old filter data */ |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 307 | __fprog_destroy(lb_priv->ex->orig_fprog); |
Jiri Pirko | 6dab015 | 2012-06-20 08:39:39 +0000 | [diff] [blame] | 308 | orig_fp = rcu_dereference_protected(lb_priv->fp, |
| 309 | lockdep_is_held(&team->lock)); |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 312 | rcu_assign_pointer(lb_priv->fp, fp); |
| 313 | lb_priv->ex->orig_fprog = fprog; |
Pablo Neira | 34c5bd6 | 2014-07-29 17:36:28 +0200 | [diff] [blame] | 314 | |
| 315 | if (orig_fp) { |
| 316 | synchronize_rcu(); |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 317 | bpf_prog_destroy(orig_fp); |
Pablo Neira | 34c5bd6 | 2014-07-29 17:36:28 +0200 | [diff] [blame] | 318 | } |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | static int lb_tx_method_get(struct team *team, struct team_gsetter_ctx *ctx) |
| 323 | { |
| 324 | struct lb_priv *lb_priv = get_lb_priv(team); |
Jiri Pirko | 6dab015 | 2012-06-20 08:39:39 +0000 | [diff] [blame] | 325 | lb_select_tx_port_func_t *func; |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 326 | char *name; |
| 327 | |
Jiri Pirko | 6dab015 | 2012-06-20 08:39:39 +0000 | [diff] [blame] | 328 | func = rcu_dereference_protected(lb_priv->select_tx_port_func, |
| 329 | lockdep_is_held(&team->lock)); |
| 330 | name = lb_select_tx_port_get_name(func); |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 331 | BUG_ON(!name); |
| 332 | ctx->data.str_val = name; |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | static int lb_tx_method_set(struct team *team, struct team_gsetter_ctx *ctx) |
| 337 | { |
| 338 | struct lb_priv *lb_priv = get_lb_priv(team); |
| 339 | lb_select_tx_port_func_t *func; |
| 340 | |
| 341 | func = lb_select_tx_port_get_func(ctx->data.str_val); |
| 342 | if (!func) |
| 343 | return -EINVAL; |
| 344 | rcu_assign_pointer(lb_priv->select_tx_port_func, func); |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | static int lb_tx_hash_to_port_mapping_init(struct team *team, |
| 349 | struct team_option_inst_info *info) |
| 350 | { |
| 351 | struct lb_priv *lb_priv = get_lb_priv(team); |
| 352 | unsigned char hash = info->array_index; |
| 353 | |
| 354 | LB_HTPM_OPT_INST_INFO_BY_HASH(lb_priv, hash) = info; |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | static int lb_tx_hash_to_port_mapping_get(struct team *team, |
| 359 | struct team_gsetter_ctx *ctx) |
| 360 | { |
| 361 | struct lb_priv *lb_priv = get_lb_priv(team); |
| 362 | struct team_port *port; |
| 363 | unsigned char hash = ctx->info->array_index; |
| 364 | |
| 365 | port = LB_HTPM_PORT_BY_HASH(lb_priv, hash); |
| 366 | ctx->data.u32_val = port ? port->dev->ifindex : 0; |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | static int lb_tx_hash_to_port_mapping_set(struct team *team, |
| 371 | struct team_gsetter_ctx *ctx) |
| 372 | { |
| 373 | struct lb_priv *lb_priv = get_lb_priv(team); |
| 374 | struct team_port *port; |
| 375 | unsigned char hash = ctx->info->array_index; |
| 376 | |
| 377 | list_for_each_entry(port, &team->port_list, list) { |
Jiri Pirko | 52a4fd7 | 2012-06-26 06:52:46 +0000 | [diff] [blame] | 378 | if (ctx->data.u32_val == port->dev->ifindex && |
| 379 | team_port_enabled(port)) { |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 380 | rcu_assign_pointer(LB_HTPM_PORT_BY_HASH(lb_priv, hash), |
| 381 | port); |
| 382 | return 0; |
| 383 | } |
| 384 | } |
| 385 | return -ENODEV; |
| 386 | } |
| 387 | |
| 388 | static int lb_hash_stats_init(struct team *team, |
| 389 | struct team_option_inst_info *info) |
| 390 | { |
| 391 | struct lb_priv *lb_priv = get_lb_priv(team); |
| 392 | unsigned char hash = info->array_index; |
| 393 | |
| 394 | lb_priv->ex->stats.info[hash].opt_inst_info = info; |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | static int lb_hash_stats_get(struct team *team, struct team_gsetter_ctx *ctx) |
| 399 | { |
| 400 | struct lb_priv *lb_priv = get_lb_priv(team); |
| 401 | unsigned char hash = ctx->info->array_index; |
| 402 | |
| 403 | ctx->data.bin_val.ptr = &lb_priv->ex->stats.info[hash].stats; |
| 404 | ctx->data.bin_val.len = sizeof(struct lb_stats); |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | static int lb_port_stats_init(struct team *team, |
| 409 | struct team_option_inst_info *info) |
| 410 | { |
| 411 | struct team_port *port = info->port; |
| 412 | struct lb_port_priv *lb_port_priv = get_lb_port_priv(port); |
| 413 | |
| 414 | lb_port_priv->stats_info.opt_inst_info = info; |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | static int lb_port_stats_get(struct team *team, struct team_gsetter_ctx *ctx) |
| 419 | { |
| 420 | struct team_port *port = ctx->info->port; |
| 421 | struct lb_port_priv *lb_port_priv = get_lb_port_priv(port); |
| 422 | |
| 423 | ctx->data.bin_val.ptr = &lb_port_priv->stats_info.stats; |
| 424 | ctx->data.bin_val.len = sizeof(struct lb_stats); |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | static void __lb_stats_info_refresh_prepare(struct lb_stats_info *s_info) |
| 429 | { |
| 430 | memcpy(&s_info->last_stats, &s_info->stats, sizeof(struct lb_stats)); |
| 431 | memset(&s_info->stats, 0, sizeof(struct lb_stats)); |
| 432 | } |
| 433 | |
| 434 | static bool __lb_stats_info_refresh_check(struct lb_stats_info *s_info, |
| 435 | struct team *team) |
| 436 | { |
| 437 | if (memcmp(&s_info->last_stats, &s_info->stats, |
| 438 | sizeof(struct lb_stats))) { |
| 439 | team_option_inst_set_change(s_info->opt_inst_info); |
| 440 | return true; |
| 441 | } |
| 442 | return false; |
| 443 | } |
| 444 | |
| 445 | static void __lb_one_cpu_stats_add(struct lb_stats *acc_stats, |
| 446 | struct lb_stats *cpu_stats, |
| 447 | struct u64_stats_sync *syncp) |
| 448 | { |
| 449 | unsigned int start; |
| 450 | struct lb_stats tmp; |
| 451 | |
| 452 | do { |
Eric W. Biederman | 57a7744 | 2014-03-13 21:26:42 -0700 | [diff] [blame] | 453 | start = u64_stats_fetch_begin_irq(syncp); |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 454 | tmp.tx_bytes = cpu_stats->tx_bytes; |
Eric W. Biederman | 57a7744 | 2014-03-13 21:26:42 -0700 | [diff] [blame] | 455 | } while (u64_stats_fetch_retry_irq(syncp, start)); |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 456 | acc_stats->tx_bytes += tmp.tx_bytes; |
| 457 | } |
| 458 | |
| 459 | static void lb_stats_refresh(struct work_struct *work) |
| 460 | { |
| 461 | struct team *team; |
| 462 | struct lb_priv *lb_priv; |
| 463 | struct lb_priv_ex *lb_priv_ex; |
| 464 | struct lb_pcpu_stats *pcpu_stats; |
| 465 | struct lb_stats *stats; |
| 466 | struct lb_stats_info *s_info; |
| 467 | struct team_port *port; |
| 468 | bool changed = false; |
| 469 | int i; |
| 470 | int j; |
| 471 | |
| 472 | lb_priv_ex = container_of(work, struct lb_priv_ex, |
| 473 | stats.refresh_dw.work); |
| 474 | |
| 475 | team = lb_priv_ex->team; |
| 476 | lb_priv = get_lb_priv(team); |
| 477 | |
| 478 | if (!mutex_trylock(&team->lock)) { |
| 479 | schedule_delayed_work(&lb_priv_ex->stats.refresh_dw, 0); |
| 480 | return; |
| 481 | } |
| 482 | |
| 483 | for (j = 0; j < LB_TX_HASHTABLE_SIZE; j++) { |
| 484 | s_info = &lb_priv->ex->stats.info[j]; |
| 485 | __lb_stats_info_refresh_prepare(s_info); |
| 486 | for_each_possible_cpu(i) { |
| 487 | pcpu_stats = per_cpu_ptr(lb_priv->pcpu_stats, i); |
| 488 | stats = &pcpu_stats->hash_stats[j]; |
| 489 | __lb_one_cpu_stats_add(&s_info->stats, stats, |
| 490 | &pcpu_stats->syncp); |
| 491 | } |
| 492 | changed |= __lb_stats_info_refresh_check(s_info, team); |
| 493 | } |
| 494 | |
| 495 | list_for_each_entry(port, &team->port_list, list) { |
| 496 | struct lb_port_priv *lb_port_priv = get_lb_port_priv(port); |
| 497 | |
| 498 | s_info = &lb_port_priv->stats_info; |
| 499 | __lb_stats_info_refresh_prepare(s_info); |
| 500 | for_each_possible_cpu(i) { |
| 501 | pcpu_stats = per_cpu_ptr(lb_priv->pcpu_stats, i); |
| 502 | stats = per_cpu_ptr(lb_port_priv->pcpu_stats, i); |
| 503 | __lb_one_cpu_stats_add(&s_info->stats, stats, |
| 504 | &pcpu_stats->syncp); |
| 505 | } |
| 506 | changed |= __lb_stats_info_refresh_check(s_info, team); |
| 507 | } |
| 508 | |
| 509 | if (changed) |
| 510 | team_options_change_check(team); |
| 511 | |
| 512 | schedule_delayed_work(&lb_priv_ex->stats.refresh_dw, |
| 513 | (lb_priv_ex->stats.refresh_interval * HZ) / 10); |
| 514 | |
| 515 | mutex_unlock(&team->lock); |
| 516 | } |
| 517 | |
| 518 | static int lb_stats_refresh_interval_get(struct team *team, |
| 519 | struct team_gsetter_ctx *ctx) |
| 520 | { |
| 521 | struct lb_priv *lb_priv = get_lb_priv(team); |
| 522 | |
| 523 | ctx->data.u32_val = lb_priv->ex->stats.refresh_interval; |
| 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | static int lb_stats_refresh_interval_set(struct team *team, |
| 528 | struct team_gsetter_ctx *ctx) |
| 529 | { |
| 530 | struct lb_priv *lb_priv = get_lb_priv(team); |
| 531 | unsigned int interval; |
| 532 | |
| 533 | interval = ctx->data.u32_val; |
| 534 | if (lb_priv->ex->stats.refresh_interval == interval) |
| 535 | return 0; |
| 536 | lb_priv->ex->stats.refresh_interval = interval; |
| 537 | if (interval) |
| 538 | schedule_delayed_work(&lb_priv->ex->stats.refresh_dw, 0); |
| 539 | else |
| 540 | cancel_delayed_work(&lb_priv->ex->stats.refresh_dw); |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 541 | return 0; |
| 542 | } |
| 543 | |
| 544 | static const struct team_option lb_options[] = { |
| 545 | { |
| 546 | .name = "bpf_hash_func", |
| 547 | .type = TEAM_OPTION_TYPE_BINARY, |
| 548 | .getter = lb_bpf_func_get, |
| 549 | .setter = lb_bpf_func_set, |
| 550 | }, |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 551 | { |
| 552 | .name = "lb_tx_method", |
| 553 | .type = TEAM_OPTION_TYPE_STRING, |
| 554 | .getter = lb_tx_method_get, |
| 555 | .setter = lb_tx_method_set, |
| 556 | }, |
| 557 | { |
| 558 | .name = "lb_tx_hash_to_port_mapping", |
| 559 | .array_size = LB_TX_HASHTABLE_SIZE, |
| 560 | .type = TEAM_OPTION_TYPE_U32, |
| 561 | .init = lb_tx_hash_to_port_mapping_init, |
| 562 | .getter = lb_tx_hash_to_port_mapping_get, |
| 563 | .setter = lb_tx_hash_to_port_mapping_set, |
| 564 | }, |
| 565 | { |
| 566 | .name = "lb_hash_stats", |
| 567 | .array_size = LB_TX_HASHTABLE_SIZE, |
| 568 | .type = TEAM_OPTION_TYPE_BINARY, |
| 569 | .init = lb_hash_stats_init, |
| 570 | .getter = lb_hash_stats_get, |
| 571 | }, |
| 572 | { |
| 573 | .name = "lb_port_stats", |
| 574 | .per_port = true, |
| 575 | .type = TEAM_OPTION_TYPE_BINARY, |
| 576 | .init = lb_port_stats_init, |
| 577 | .getter = lb_port_stats_get, |
| 578 | }, |
| 579 | { |
| 580 | .name = "lb_stats_refresh_interval", |
| 581 | .type = TEAM_OPTION_TYPE_U32, |
| 582 | .getter = lb_stats_refresh_interval_get, |
| 583 | .setter = lb_stats_refresh_interval_set, |
| 584 | }, |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 585 | }; |
| 586 | |
Jiri Pirko | cade455 | 2012-04-10 05:15:46 +0000 | [diff] [blame] | 587 | static int lb_init(struct team *team) |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 588 | { |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 589 | struct lb_priv *lb_priv = get_lb_priv(team); |
| 590 | lb_select_tx_port_func_t *func; |
John Stultz | 827da44 | 2013-10-07 15:51:58 -0700 | [diff] [blame] | 591 | int i, err; |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 592 | |
| 593 | /* set default tx port selector */ |
| 594 | func = lb_select_tx_port_get_func("hash"); |
| 595 | BUG_ON(!func); |
| 596 | rcu_assign_pointer(lb_priv->select_tx_port_func, func); |
| 597 | |
| 598 | lb_priv->ex = kzalloc(sizeof(*lb_priv->ex), GFP_KERNEL); |
| 599 | if (!lb_priv->ex) |
| 600 | return -ENOMEM; |
| 601 | lb_priv->ex->team = team; |
| 602 | |
| 603 | lb_priv->pcpu_stats = alloc_percpu(struct lb_pcpu_stats); |
| 604 | if (!lb_priv->pcpu_stats) { |
| 605 | err = -ENOMEM; |
| 606 | goto err_alloc_pcpu_stats; |
| 607 | } |
| 608 | |
John Stultz | 827da44 | 2013-10-07 15:51:58 -0700 | [diff] [blame] | 609 | for_each_possible_cpu(i) { |
| 610 | struct lb_pcpu_stats *team_lb_stats; |
| 611 | team_lb_stats = per_cpu_ptr(lb_priv->pcpu_stats, i); |
| 612 | u64_stats_init(&team_lb_stats->syncp); |
| 613 | } |
| 614 | |
| 615 | |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 616 | INIT_DELAYED_WORK(&lb_priv->ex->stats.refresh_dw, lb_stats_refresh); |
| 617 | |
| 618 | err = team_options_register(team, lb_options, ARRAY_SIZE(lb_options)); |
| 619 | if (err) |
| 620 | goto err_options_register; |
| 621 | return 0; |
| 622 | |
| 623 | err_options_register: |
| 624 | free_percpu(lb_priv->pcpu_stats); |
| 625 | err_alloc_pcpu_stats: |
| 626 | kfree(lb_priv->ex); |
| 627 | return err; |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Jiri Pirko | cade455 | 2012-04-10 05:15:46 +0000 | [diff] [blame] | 630 | static void lb_exit(struct team *team) |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 631 | { |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 632 | struct lb_priv *lb_priv = get_lb_priv(team); |
| 633 | |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 634 | team_options_unregister(team, lb_options, |
| 635 | ARRAY_SIZE(lb_options)); |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 636 | cancel_delayed_work_sync(&lb_priv->ex->stats.refresh_dw); |
| 637 | free_percpu(lb_priv->pcpu_stats); |
| 638 | kfree(lb_priv->ex); |
| 639 | } |
| 640 | |
| 641 | static int lb_port_enter(struct team *team, struct team_port *port) |
| 642 | { |
| 643 | struct lb_port_priv *lb_port_priv = get_lb_port_priv(port); |
| 644 | |
| 645 | lb_port_priv->pcpu_stats = alloc_percpu(struct lb_stats); |
| 646 | if (!lb_port_priv->pcpu_stats) |
| 647 | return -ENOMEM; |
| 648 | return 0; |
| 649 | } |
| 650 | |
| 651 | static void lb_port_leave(struct team *team, struct team_port *port) |
| 652 | { |
| 653 | struct lb_port_priv *lb_port_priv = get_lb_port_priv(port); |
| 654 | |
| 655 | free_percpu(lb_port_priv->pcpu_stats); |
| 656 | } |
| 657 | |
| 658 | static void lb_port_disabled(struct team *team, struct team_port *port) |
| 659 | { |
| 660 | lb_tx_hash_to_port_mapping_null_port(team, port); |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 663 | static const struct team_mode_ops lb_mode_ops = { |
| 664 | .init = lb_init, |
| 665 | .exit = lb_exit, |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 666 | .port_enter = lb_port_enter, |
| 667 | .port_leave = lb_port_leave, |
| 668 | .port_disabled = lb_port_disabled, |
Jiri Pirko | c15e07b | 2016-08-25 18:30:52 +0200 | [diff] [blame] | 669 | .receive = lb_receive, |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 670 | .transmit = lb_transmit, |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 671 | }; |
| 672 | |
Jiri Pirko | 0402788 | 2012-06-19 05:54:03 +0000 | [diff] [blame] | 673 | static const struct team_mode lb_mode = { |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 674 | .kind = "loadbalance", |
| 675 | .owner = THIS_MODULE, |
| 676 | .priv_size = sizeof(struct lb_priv), |
Jiri Pirko | ab8250d7 | 2012-06-19 05:54:17 +0000 | [diff] [blame] | 677 | .port_priv_size = sizeof(struct lb_port_priv), |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 678 | .ops = &lb_mode_ops, |
Jiri Pirko | 8fd7285 | 2015-12-03 12:12:13 +0100 | [diff] [blame] | 679 | .lag_tx_type = NETDEV_LAG_TX_TYPE_HASH, |
Jiri Pirko | 01d7f30 | 2012-04-04 12:16:27 +0000 | [diff] [blame] | 680 | }; |
| 681 | |
| 682 | static int __init lb_init_module(void) |
| 683 | { |
| 684 | return team_mode_register(&lb_mode); |
| 685 | } |
| 686 | |
| 687 | static void __exit lb_cleanup_module(void) |
| 688 | { |
| 689 | team_mode_unregister(&lb_mode); |
| 690 | } |
| 691 | |
| 692 | module_init(lb_init_module); |
| 693 | module_exit(lb_cleanup_module); |
| 694 | |
| 695 | MODULE_LICENSE("GPL v2"); |
| 696 | MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>"); |
| 697 | MODULE_DESCRIPTION("Load-balancing mode for team"); |
| 698 | MODULE_ALIAS("team-mode-loadbalance"); |