ipvs: convert sh scheduler to rcu
[firefly-linux-kernel-4.4.55.git] / net / netfilter / ipvs / ip_vs_sh.c
1 /*
2  * IPVS:        Source Hashing scheduling module
3  *
4  * Authors:     Wensong Zhang <wensong@gnuchina.org>
5  *
6  *              This program is free software; you can redistribute it and/or
7  *              modify it under the terms of the GNU General Public License
8  *              as published by the Free Software Foundation; either version
9  *              2 of the License, or (at your option) any later version.
10  *
11  * Changes:
12  *
13  */
14
15 /*
16  * The sh algorithm is to select server by the hash key of source IP
17  * address. The pseudo code is as follows:
18  *
19  *       n <- servernode[src_ip];
20  *       if (n is dead) OR
21  *          (n is overloaded) or (n.weight <= 0) then
22  *                 return NULL;
23  *
24  *       return n;
25  *
26  * Notes that servernode is a 256-bucket hash table that maps the hash
27  * index derived from packet source IP address to the current server
28  * array. If the sh scheduler is used in cache cluster, it is good to
29  * combine it with cache_bypass feature. When the statically assigned
30  * server is dead or overloaded, the load balancer can bypass the cache
31  * server and send requests to the original server directly.
32  *
33  * The weight destination attribute can be used to control the
34  * distribution of connections to the destinations in servernode. The
35  * greater the weight, the more connections the destination
36  * will receive.
37  *
38  */
39
40 #define KMSG_COMPONENT "IPVS"
41 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
42
43 #include <linux/ip.h>
44 #include <linux/slab.h>
45 #include <linux/module.h>
46 #include <linux/kernel.h>
47 #include <linux/skbuff.h>
48
49 #include <net/ip_vs.h>
50
51
52 /*
53  *      IPVS SH bucket
54  */
55 struct ip_vs_sh_bucket {
56         struct ip_vs_dest __rcu *dest;  /* real server (cache) */
57 };
58
59 /*
60  *     for IPVS SH entry hash table
61  */
62 #ifndef CONFIG_IP_VS_SH_TAB_BITS
63 #define CONFIG_IP_VS_SH_TAB_BITS        8
64 #endif
65 #define IP_VS_SH_TAB_BITS               CONFIG_IP_VS_SH_TAB_BITS
66 #define IP_VS_SH_TAB_SIZE               (1 << IP_VS_SH_TAB_BITS)
67 #define IP_VS_SH_TAB_MASK               (IP_VS_SH_TAB_SIZE - 1)
68
69 struct ip_vs_sh_state {
70         struct ip_vs_sh_bucket          buckets[IP_VS_SH_TAB_SIZE];
71         struct rcu_head                 rcu_head;
72 };
73
74 /*
75  *      Returns hash value for IPVS SH entry
76  */
77 static inline unsigned int ip_vs_sh_hashkey(int af, const union nf_inet_addr *addr)
78 {
79         __be32 addr_fold = addr->ip;
80
81 #ifdef CONFIG_IP_VS_IPV6
82         if (af == AF_INET6)
83                 addr_fold = addr->ip6[0]^addr->ip6[1]^
84                             addr->ip6[2]^addr->ip6[3];
85 #endif
86         return (ntohl(addr_fold)*2654435761UL) & IP_VS_SH_TAB_MASK;
87 }
88
89
90 /*
91  *      Get ip_vs_dest associated with supplied parameters.
92  */
93 static inline struct ip_vs_dest *
94 ip_vs_sh_get(int af, struct ip_vs_sh_state *s, const union nf_inet_addr *addr)
95 {
96         return rcu_dereference(s->buckets[ip_vs_sh_hashkey(af, addr)].dest);
97 }
98
99
100 /*
101  *      Assign all the hash buckets of the specified table with the service.
102  */
103 static int
104 ip_vs_sh_reassign(struct ip_vs_sh_state *s, struct ip_vs_service *svc)
105 {
106         int i;
107         struct ip_vs_sh_bucket *b;
108         struct list_head *p;
109         struct ip_vs_dest *dest;
110         int d_count;
111         bool empty;
112
113         b = &s->buckets[0];
114         p = &svc->destinations;
115         empty = list_empty(p);
116         d_count = 0;
117         for (i=0; i<IP_VS_SH_TAB_SIZE; i++) {
118                 dest = rcu_dereference_protected(b->dest, 1);
119                 if (dest)
120                         ip_vs_dest_put(dest);
121                 if (empty)
122                         RCU_INIT_POINTER(b->dest, NULL);
123                 else {
124                         if (p == &svc->destinations)
125                                 p = p->next;
126
127                         dest = list_entry(p, struct ip_vs_dest, n_list);
128                         ip_vs_dest_hold(dest);
129                         RCU_INIT_POINTER(b->dest, dest);
130
131                         IP_VS_DBG_BUF(6, "assigned i: %d dest: %s weight: %d\n",
132                                       i, IP_VS_DBG_ADDR(svc->af, &dest->addr),
133                                       atomic_read(&dest->weight));
134
135                         /* Don't move to next dest until filling weight */
136                         if (++d_count >= atomic_read(&dest->weight)) {
137                                 p = p->next;
138                                 d_count = 0;
139                         }
140
141                 }
142                 b++;
143         }
144         return 0;
145 }
146
147
148 /*
149  *      Flush all the hash buckets of the specified table.
150  */
151 static void ip_vs_sh_flush(struct ip_vs_sh_state *s)
152 {
153         int i;
154         struct ip_vs_sh_bucket *b;
155         struct ip_vs_dest *dest;
156
157         b = &s->buckets[0];
158         for (i=0; i<IP_VS_SH_TAB_SIZE; i++) {
159                 dest = rcu_dereference_protected(b->dest, 1);
160                 if (dest) {
161                         ip_vs_dest_put(dest);
162                         RCU_INIT_POINTER(b->dest, NULL);
163                 }
164                 b++;
165         }
166 }
167
168
169 static int ip_vs_sh_init_svc(struct ip_vs_service *svc)
170 {
171         struct ip_vs_sh_state *s;
172
173         /* allocate the SH table for this service */
174         s = kzalloc(sizeof(struct ip_vs_sh_state), GFP_KERNEL);
175         if (s == NULL)
176                 return -ENOMEM;
177
178         svc->sched_data = s;
179         IP_VS_DBG(6, "SH hash table (memory=%Zdbytes) allocated for "
180                   "current service\n",
181                   sizeof(struct ip_vs_sh_bucket)*IP_VS_SH_TAB_SIZE);
182
183         /* assign the hash buckets with current dests */
184         ip_vs_sh_reassign(s, svc);
185
186         return 0;
187 }
188
189
190 static int ip_vs_sh_done_svc(struct ip_vs_service *svc)
191 {
192         struct ip_vs_sh_state *s = svc->sched_data;
193
194         /* got to clean up hash buckets here */
195         ip_vs_sh_flush(s);
196
197         /* release the table itself */
198         kfree_rcu(s, rcu_head);
199         IP_VS_DBG(6, "SH hash table (memory=%Zdbytes) released\n",
200                   sizeof(struct ip_vs_sh_bucket)*IP_VS_SH_TAB_SIZE);
201
202         return 0;
203 }
204
205
206 static int ip_vs_sh_dest_changed(struct ip_vs_service *svc,
207                                  struct ip_vs_dest *dest)
208 {
209         struct ip_vs_sh_state *s = svc->sched_data;
210
211         /* assign the hash buckets with the updated service */
212         ip_vs_sh_reassign(s, svc);
213
214         return 0;
215 }
216
217
218 /*
219  *      If the dest flags is set with IP_VS_DEST_F_OVERLOAD,
220  *      consider that the server is overloaded here.
221  */
222 static inline int is_overloaded(struct ip_vs_dest *dest)
223 {
224         return dest->flags & IP_VS_DEST_F_OVERLOAD;
225 }
226
227
228 /*
229  *      Source Hashing scheduling
230  */
231 static struct ip_vs_dest *
232 ip_vs_sh_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
233 {
234         struct ip_vs_dest *dest;
235         struct ip_vs_sh_state *s;
236         struct ip_vs_iphdr iph;
237
238         ip_vs_fill_iph_addr_only(svc->af, skb, &iph);
239
240         IP_VS_DBG(6, "ip_vs_sh_schedule(): Scheduling...\n");
241
242         s = (struct ip_vs_sh_state *) svc->sched_data;
243         dest = ip_vs_sh_get(svc->af, s, &iph.saddr);
244         if (!dest
245             || !(dest->flags & IP_VS_DEST_F_AVAILABLE)
246             || atomic_read(&dest->weight) <= 0
247             || is_overloaded(dest)) {
248                 ip_vs_scheduler_err(svc, "no destination available");
249                 return NULL;
250         }
251
252         IP_VS_DBG_BUF(6, "SH: source IP address %s --> server %s:%d\n",
253                       IP_VS_DBG_ADDR(svc->af, &iph.saddr),
254                       IP_VS_DBG_ADDR(svc->af, &dest->addr),
255                       ntohs(dest->port));
256
257         return dest;
258 }
259
260
261 /*
262  *      IPVS SH Scheduler structure
263  */
264 static struct ip_vs_scheduler ip_vs_sh_scheduler =
265 {
266         .name =                 "sh",
267         .refcnt =               ATOMIC_INIT(0),
268         .module =               THIS_MODULE,
269         .n_list  =              LIST_HEAD_INIT(ip_vs_sh_scheduler.n_list),
270         .init_service =         ip_vs_sh_init_svc,
271         .done_service =         ip_vs_sh_done_svc,
272         .add_dest =             ip_vs_sh_dest_changed,
273         .del_dest =             ip_vs_sh_dest_changed,
274         .upd_dest =             ip_vs_sh_dest_changed,
275         .schedule =             ip_vs_sh_schedule,
276 };
277
278
279 static int __init ip_vs_sh_init(void)
280 {
281         return register_ip_vs_scheduler(&ip_vs_sh_scheduler);
282 }
283
284
285 static void __exit ip_vs_sh_cleanup(void)
286 {
287         unregister_ip_vs_scheduler(&ip_vs_sh_scheduler);
288 }
289
290
291 module_init(ip_vs_sh_init);
292 module_exit(ip_vs_sh_cleanup);
293 MODULE_LICENSE("GPL");