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