kernel: add a RPS balancer
[openwrt/staging/wigyori.git] / target / linux / generic / hack-4.14 / 600-net-core-add-RPS-balancer.patch
1 From 3e969c9695b45e1a052d43b367096ec99f2f0aac Mon Sep 17 00:00:00 2001
2 From: John Crispin <john@phrozen.org>
3 Date: Thu, 10 Aug 2017 15:58:29 +0200
4 Subject: [PATCH] net: core: add RPS balancer
5
6 This patch adds a hash bucket based rps hash balancer.
7
8 Signed-off-by: John Crispin <john@phrozen.org>
9 ---
10 net/core/dev.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
11 1 file changed, 56 insertions(+), 1 deletion(-)
12
13 --- a/net/core/dev.c
14 +++ b/net/core/dev.c
15 @@ -3626,6 +3626,58 @@ set_rps_cpu(struct net_device *dev, stru
16 return rflow;
17 }
18
19 +#define RPS_TBL_SIZE_SHIFT 10
20 +#define RPS_TBL_SIZE (1 << RPS_TBL_SIZE_SHIFT)
21 +struct rps_table {
22 + int core;
23 + struct timer_list expire;
24 +};
25 +static struct rps_table rps_table[RPS_TBL_SIZE];
26 +static int rps_table_last_core;
27 +
28 +static void rps_table_expire(unsigned long data)
29 +{
30 + struct rps_table *entry = (struct rps_table *) data;
31 +
32 + entry->core = -1;
33 +}
34 +
35 +static int rps_table_core(struct rps_map *map)
36 +{
37 + int i;
38 +
39 + for (i = 0; i < map->len; i++) {
40 + int cpu = map->cpus[(rps_table_last_core + i + 1) % map->len];
41 + if (cpu_online(cpu)) {
42 + rps_table_last_core = cpu;
43 + return cpu;
44 + }
45 + }
46 + return map->cpus[0];
47 +}
48 +
49 +static int rps_table_lookup(struct rps_map *map, u32 hash)
50 +{
51 + int bucket = hash & 0x3ff;
52 +
53 + if (rps_table[bucket].core < 0)
54 + rps_table[bucket].core = rps_table_core(map);
55 + mod_timer(&rps_table[bucket].expire, jiffies + HZ);
56 +
57 + return rps_table[bucket].core;
58 +}
59 +
60 +static void rps_table_init(void)
61 +{
62 + int i;
63 +
64 + for (i = 0; i < RPS_TBL_SIZE; i++) {
65 + rps_table[i].core = -1;
66 + setup_timer(&rps_table[i].expire, rps_table_expire,
67 + (unsigned long) &rps_table[i]);
68 + }
69 +}
70 +
71 /*
72 * get_rps_cpu is called from netif_receive_skb and returns the target
73 * CPU from the RPS map of the receiving queue for a given skb.
74 @@ -3715,7 +3767,7 @@ static int get_rps_cpu(struct net_device
75 try_rps:
76
77 if (map) {
78 - tcpu = map->cpus[reciprocal_scale(hash, map->len)];
79 + tcpu = rps_table_lookup(map, hash);
80 if (cpu_online(tcpu)) {
81 cpu = tcpu;
82 goto done;
83 @@ -8800,6 +8852,10 @@ static int __init net_dev_init(void)
84 sd->backlog.weight = weight_p;
85 }
86
87 +#ifdef CONFIG_RPS
88 + rps_table_init();
89 +#endif
90 +
91 dev_boot_phase = 0;
92
93 /* The loopback device is special if any other network devices