blob: 1e373a5e44e34cf172f3843b1fcc700ed3a57cbc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPVS: Source Hashing scheduling module
3 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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 *
Michael Maxim76ad94f2011-12-08 10:55:09 -050033 * 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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 */
39
Hannes Eder9aada7a2009-07-30 14:29:44 -070040#define KMSG_COMPONENT "IPVS"
41#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
42
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020043#include <linux/ip.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/module.h>
46#include <linux/kernel.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020047#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#include <net/ip_vs.h>
50
Alexander Frolkineba3b5a2013-06-19 10:54:25 +010051#include <net/tcp.h>
52#include <linux/udp.h>
53#include <linux/sctp.h>
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/*
57 * IPVS SH bucket
58 */
59struct ip_vs_sh_bucket {
Julian Anastasov1acb7f62013-03-22 11:46:46 +020060 struct ip_vs_dest __rcu *dest; /* real server (cache) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061};
62
63/*
64 * for IPVS SH entry hash table
65 */
66#ifndef CONFIG_IP_VS_SH_TAB_BITS
67#define CONFIG_IP_VS_SH_TAB_BITS 8
68#endif
69#define IP_VS_SH_TAB_BITS CONFIG_IP_VS_SH_TAB_BITS
70#define IP_VS_SH_TAB_SIZE (1 << IP_VS_SH_TAB_BITS)
71#define IP_VS_SH_TAB_MASK (IP_VS_SH_TAB_SIZE - 1)
72
Julian Anastasov1acb7f62013-03-22 11:46:46 +020073struct ip_vs_sh_state {
Julian Anastasov1acb7f62013-03-22 11:46:46 +020074 struct rcu_head rcu_head;
Jan Beulicha70b9642013-05-29 13:33:51 +010075 struct ip_vs_sh_bucket buckets[IP_VS_SH_TAB_SIZE];
Julian Anastasov1acb7f62013-03-22 11:46:46 +020076};
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Alexander Frolkineba3b5a2013-06-19 10:54:25 +010078/* Helper function to determine if server is unavailable */
79static inline bool is_unavailable(struct ip_vs_dest *dest)
80{
81 return atomic_read(&dest->weight) <= 0 ||
82 dest->flags & IP_VS_DEST_F_OVERLOAD;
83}
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/*
86 * Returns hash value for IPVS SH entry
87 */
Alexander Frolkineba3b5a2013-06-19 10:54:25 +010088static inline unsigned int
89ip_vs_sh_hashkey(int af, const union nf_inet_addr *addr,
90 __be16 port, unsigned int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Julius Volz20971a02008-11-01 13:13:19 +000092 __be32 addr_fold = addr->ip;
93
94#ifdef CONFIG_IP_VS_IPV6
95 if (af == AF_INET6)
96 addr_fold = addr->ip6[0]^addr->ip6[1]^
97 addr->ip6[2]^addr->ip6[3];
98#endif
Alexander Frolkineba3b5a2013-06-19 10:54:25 +010099 return (offset + (ntohs(port) + ntohl(addr_fold))*2654435761UL) &
100 IP_VS_SH_TAB_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
103
104/*
105 * Get ip_vs_dest associated with supplied parameters.
106 */
107static inline struct ip_vs_dest *
Alexander Frolkineba3b5a2013-06-19 10:54:25 +0100108ip_vs_sh_get(struct ip_vs_service *svc, struct ip_vs_sh_state *s,
109 const union nf_inet_addr *addr, __be16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Alexander Frolkineba3b5a2013-06-19 10:54:25 +0100111 unsigned int hash = ip_vs_sh_hashkey(svc->af, addr, port, 0);
112 struct ip_vs_dest *dest = rcu_dereference(s->buckets[hash].dest);
113
114 return (!dest || is_unavailable(dest)) ? NULL : dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117
Alexander Frolkin1255ce52013-09-27 11:06:23 +0100118/* As ip_vs_sh_get, but with fallback if selected server is unavailable
119 *
120 * The fallback strategy loops around the table starting from a "random"
121 * point (in fact, it is chosen to be the original hash value to make the
122 * algorithm deterministic) to find a new server.
123 */
Alexander Frolkineba3b5a2013-06-19 10:54:25 +0100124static inline struct ip_vs_dest *
125ip_vs_sh_get_fallback(struct ip_vs_service *svc, struct ip_vs_sh_state *s,
126 const union nf_inet_addr *addr, __be16 port)
127{
Alexander Frolkin1255ce52013-09-27 11:06:23 +0100128 unsigned int offset, roffset;
129 unsigned int hash, ihash;
Alexander Frolkineba3b5a2013-06-19 10:54:25 +0100130 struct ip_vs_dest *dest;
131
Alexander Frolkin1255ce52013-09-27 11:06:23 +0100132 /* first try the dest it's supposed to go to */
133 ihash = ip_vs_sh_hashkey(svc->af, addr, port, 0);
134 dest = rcu_dereference(s->buckets[ihash].dest);
135 if (!dest)
136 return NULL;
137 if (!is_unavailable(dest))
138 return dest;
139
140 IP_VS_DBG_BUF(6, "SH: selected unavailable server %s:%d, reselecting",
Julian Anastasov4d316f32014-09-17 00:09:00 +0300141 IP_VS_DBG_ADDR(dest->af, &dest->addr), ntohs(dest->port));
Alexander Frolkin1255ce52013-09-27 11:06:23 +0100142
143 /* if the original dest is unavailable, loop around the table
144 * starting from ihash to find a new dest
145 */
Alexander Frolkineba3b5a2013-06-19 10:54:25 +0100146 for (offset = 0; offset < IP_VS_SH_TAB_SIZE; offset++) {
Alexander Frolkin1255ce52013-09-27 11:06:23 +0100147 roffset = (offset + ihash) % IP_VS_SH_TAB_SIZE;
148 hash = ip_vs_sh_hashkey(svc->af, addr, port, roffset);
Alexander Frolkineba3b5a2013-06-19 10:54:25 +0100149 dest = rcu_dereference(s->buckets[hash].dest);
150 if (!dest)
151 break;
Alexander Frolkin1255ce52013-09-27 11:06:23 +0100152 if (!is_unavailable(dest))
Alexander Frolkineba3b5a2013-06-19 10:54:25 +0100153 return dest;
Alexander Frolkin1255ce52013-09-27 11:06:23 +0100154 IP_VS_DBG_BUF(6, "SH: selected unavailable "
155 "server %s:%d (offset %d), reselecting",
Julian Anastasov4d316f32014-09-17 00:09:00 +0300156 IP_VS_DBG_ADDR(dest->af, &dest->addr),
Alexander Frolkin1255ce52013-09-27 11:06:23 +0100157 ntohs(dest->port), roffset);
Alexander Frolkineba3b5a2013-06-19 10:54:25 +0100158 }
159
160 return NULL;
161}
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/*
164 * Assign all the hash buckets of the specified table with the service.
165 */
166static int
Julian Anastasov1acb7f62013-03-22 11:46:46 +0200167ip_vs_sh_reassign(struct ip_vs_sh_state *s, struct ip_vs_service *svc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
169 int i;
170 struct ip_vs_sh_bucket *b;
171 struct list_head *p;
172 struct ip_vs_dest *dest;
Michael Maxim76ad94f2011-12-08 10:55:09 -0500173 int d_count;
Julian Anastasov1acb7f62013-03-22 11:46:46 +0200174 bool empty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Julian Anastasov1acb7f62013-03-22 11:46:46 +0200176 b = &s->buckets[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 p = &svc->destinations;
Julian Anastasov1acb7f62013-03-22 11:46:46 +0200178 empty = list_empty(p);
Michael Maxim76ad94f2011-12-08 10:55:09 -0500179 d_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 for (i=0; i<IP_VS_SH_TAB_SIZE; i++) {
Julian Anastasov1acb7f62013-03-22 11:46:46 +0200181 dest = rcu_dereference_protected(b->dest, 1);
182 if (dest)
183 ip_vs_dest_put(dest);
184 if (empty)
185 RCU_INIT_POINTER(b->dest, NULL);
186 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (p == &svc->destinations)
188 p = p->next;
189
190 dest = list_entry(p, struct ip_vs_dest, n_list);
Julian Anastasov1acb7f62013-03-22 11:46:46 +0200191 ip_vs_dest_hold(dest);
192 RCU_INIT_POINTER(b->dest, dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Michael Maxim76ad94f2011-12-08 10:55:09 -0500194 IP_VS_DBG_BUF(6, "assigned i: %d dest: %s weight: %d\n",
Julian Anastasov4d316f32014-09-17 00:09:00 +0300195 i, IP_VS_DBG_ADDR(dest->af, &dest->addr),
Michael Maxim76ad94f2011-12-08 10:55:09 -0500196 atomic_read(&dest->weight));
197
198 /* Don't move to next dest until filling weight */
199 if (++d_count >= atomic_read(&dest->weight)) {
200 p = p->next;
201 d_count = 0;
202 }
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205 b++;
206 }
207 return 0;
208}
209
210
211/*
212 * Flush all the hash buckets of the specified table.
213 */
Julian Anastasov1acb7f62013-03-22 11:46:46 +0200214static void ip_vs_sh_flush(struct ip_vs_sh_state *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 int i;
217 struct ip_vs_sh_bucket *b;
Julian Anastasov1acb7f62013-03-22 11:46:46 +0200218 struct ip_vs_dest *dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Julian Anastasov1acb7f62013-03-22 11:46:46 +0200220 b = &s->buckets[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 for (i=0; i<IP_VS_SH_TAB_SIZE; i++) {
Julian Anastasov1acb7f62013-03-22 11:46:46 +0200222 dest = rcu_dereference_protected(b->dest, 1);
223 if (dest) {
224 ip_vs_dest_put(dest);
225 RCU_INIT_POINTER(b->dest, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
227 b++;
228 }
229}
230
231
232static int ip_vs_sh_init_svc(struct ip_vs_service *svc)
233{
Julian Anastasov1acb7f62013-03-22 11:46:46 +0200234 struct ip_vs_sh_state *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 /* allocate the SH table for this service */
Julian Anastasov1acb7f62013-03-22 11:46:46 +0200237 s = kzalloc(sizeof(struct ip_vs_sh_state), GFP_KERNEL);
238 if (s == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return -ENOMEM;
Joe Perches0a9ee812011-08-29 14:17:25 -0700240
Julian Anastasov1acb7f62013-03-22 11:46:46 +0200241 svc->sched_data = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 IP_VS_DBG(6, "SH hash table (memory=%Zdbytes) allocated for "
243 "current service\n",
244 sizeof(struct ip_vs_sh_bucket)*IP_VS_SH_TAB_SIZE);
245
Julian Anastasov1acb7f62013-03-22 11:46:46 +0200246 /* assign the hash buckets with current dests */
247 ip_vs_sh_reassign(s, svc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 return 0;
250}
251
252
Julian Anastasoved3ffc42013-03-22 11:46:50 +0200253static void ip_vs_sh_done_svc(struct ip_vs_service *svc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Julian Anastasov1acb7f62013-03-22 11:46:46 +0200255 struct ip_vs_sh_state *s = svc->sched_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 /* got to clean up hash buckets here */
Julian Anastasov1acb7f62013-03-22 11:46:46 +0200258 ip_vs_sh_flush(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 /* release the table itself */
Julian Anastasov1acb7f62013-03-22 11:46:46 +0200261 kfree_rcu(s, rcu_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 IP_VS_DBG(6, "SH hash table (memory=%Zdbytes) released\n",
263 sizeof(struct ip_vs_sh_bucket)*IP_VS_SH_TAB_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
266
Julian Anastasov1acb7f62013-03-22 11:46:46 +0200267static int ip_vs_sh_dest_changed(struct ip_vs_service *svc,
268 struct ip_vs_dest *dest)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Julian Anastasov1acb7f62013-03-22 11:46:46 +0200270 struct ip_vs_sh_state *s = svc->sched_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 /* assign the hash buckets with the updated service */
Julian Anastasov1acb7f62013-03-22 11:46:46 +0200273 ip_vs_sh_reassign(s, svc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 return 0;
276}
277
278
Alexander Frolkineba3b5a2013-06-19 10:54:25 +0100279/* Helper function to get port number */
280static inline __be16
281ip_vs_sh_get_port(const struct sk_buff *skb, struct ip_vs_iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Alex Gartrell1471f352015-08-26 09:40:36 -0700283 __be16 _ports[2], *ports;
Alexander Frolkineba3b5a2013-06-19 10:54:25 +0100284
Alex Gartrell1471f352015-08-26 09:40:36 -0700285 /* At this point we know that we have a valid packet of some kind.
286 * Because ICMP packets are only guaranteed to have the first 8
287 * bytes, let's just grab the ports. Fortunately they're in the
288 * same position for all three of the protocols we care about.
289 */
Alexander Frolkineba3b5a2013-06-19 10:54:25 +0100290 switch (iph->protocol) {
291 case IPPROTO_TCP:
Alexander Frolkineba3b5a2013-06-19 10:54:25 +0100292 case IPPROTO_UDP:
Alexander Frolkineba3b5a2013-06-19 10:54:25 +0100293 case IPPROTO_SCTP:
Alex Gartrell1471f352015-08-26 09:40:36 -0700294 ports = skb_header_pointer(skb, iph->len, sizeof(_ports),
295 &_ports);
296 if (unlikely(!ports))
Daniel Borkmann54e35cc2013-08-06 11:20:23 +0200297 return 0;
Alexander Frolkineba3b5a2013-06-19 10:54:25 +0100298
Alex Gartrell1471f352015-08-26 09:40:36 -0700299 if (likely(!ip_vs_iph_inverse(iph)))
300 return ports[0];
301 else
302 return ports[1];
303 default:
304 return 0;
305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
308
309/*
310 * Source Hashing scheduling
311 */
312static struct ip_vs_dest *
Julian Anastasovbba54de2013-06-16 09:09:36 +0300313ip_vs_sh_schedule(struct ip_vs_service *svc, const struct sk_buff *skb,
314 struct ip_vs_iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 struct ip_vs_dest *dest;
Julian Anastasov1acb7f62013-03-22 11:46:46 +0200317 struct ip_vs_sh_state *s;
Alexander Frolkineba3b5a2013-06-19 10:54:25 +0100318 __be16 port = 0;
Alex Gartrell1471f352015-08-26 09:40:36 -0700319 const union nf_inet_addr *hash_addr;
320
321 hash_addr = ip_vs_iph_inverse(iph) ? &iph->daddr : &iph->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 IP_VS_DBG(6, "ip_vs_sh_schedule(): Scheduling...\n");
324
Alexander Frolkineba3b5a2013-06-19 10:54:25 +0100325 if (svc->flags & IP_VS_SVC_F_SCHED_SH_PORT)
326 port = ip_vs_sh_get_port(skb, iph);
327
Julian Anastasov1acb7f62013-03-22 11:46:46 +0200328 s = (struct ip_vs_sh_state *) svc->sched_data;
Alexander Frolkineba3b5a2013-06-19 10:54:25 +0100329
330 if (svc->flags & IP_VS_SVC_F_SCHED_SH_FALLBACK)
Alex Gartrell1471f352015-08-26 09:40:36 -0700331 dest = ip_vs_sh_get_fallback(svc, s, hash_addr, port);
Alexander Frolkineba3b5a2013-06-19 10:54:25 +0100332 else
Alex Gartrell1471f352015-08-26 09:40:36 -0700333 dest = ip_vs_sh_get(svc, s, hash_addr, port);
Alexander Frolkineba3b5a2013-06-19 10:54:25 +0100334
335 if (!dest) {
Patrick Schaaf41ac51e2011-02-11 14:01:12 +0100336 ip_vs_scheduler_err(svc, "no destination available");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return NULL;
338 }
339
Julius Volz20971a02008-11-01 13:13:19 +0000340 IP_VS_DBG_BUF(6, "SH: source IP address %s --> server %s:%d\n",
Alex Gartrell1471f352015-08-26 09:40:36 -0700341 IP_VS_DBG_ADDR(svc->af, hash_addr),
Julian Anastasov4d316f32014-09-17 00:09:00 +0300342 IP_VS_DBG_ADDR(dest->af, &dest->addr),
Julius Volz20971a02008-11-01 13:13:19 +0000343 ntohs(dest->port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 return dest;
346}
347
348
349/*
350 * IPVS SH Scheduler structure
351 */
352static struct ip_vs_scheduler ip_vs_sh_scheduler =
353{
354 .name = "sh",
355 .refcnt = ATOMIC_INIT(0),
356 .module = THIS_MODULE,
Sven Wegenerd149ccc2008-08-10 09:18:02 +0000357 .n_list = LIST_HEAD_INIT(ip_vs_sh_scheduler.n_list),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 .init_service = ip_vs_sh_init_svc,
359 .done_service = ip_vs_sh_done_svc,
Julian Anastasov1acb7f62013-03-22 11:46:46 +0200360 .add_dest = ip_vs_sh_dest_changed,
361 .del_dest = ip_vs_sh_dest_changed,
362 .upd_dest = ip_vs_sh_dest_changed,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 .schedule = ip_vs_sh_schedule,
364};
365
366
367static int __init ip_vs_sh_init(void)
368{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return register_ip_vs_scheduler(&ip_vs_sh_scheduler);
370}
371
372
373static void __exit ip_vs_sh_cleanup(void)
374{
375 unregister_ip_vs_scheduler(&ip_vs_sh_scheduler);
Julian Anastasovceec4c32013-03-22 11:46:53 +0200376 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
379
380module_init(ip_vs_sh_init);
381module_exit(ip_vs_sh_cleanup);
382MODULE_LICENSE("GPL");