2 * IPVS: Source Hashing scheduling module
4 * Authors: Wensong Zhang <wensong@gnuchina.org>
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.
16 * The sh algorithm is to select server by the hash key of source IP
17 * address. The pseudo code is as follows:
19 * n <- servernode[src_ip];
21 * (n is overloaded) or (n.weight <= 0) then
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.
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
40 #define KMSG_COMPONENT "IPVS"
41 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
44 #include <linux/slab.h>
45 #include <linux/module.h>
46 #include <linux/kernel.h>
47 #include <linux/skbuff.h>
49 #include <net/ip_vs.h>
52 #include <linux/udp.h>
53 #include <linux/sctp.h>
59 struct ip_vs_sh_bucket {
60 struct ip_vs_dest __rcu *dest; /* real server (cache) */
64 * for IPVS SH entry hash table
66 #ifndef CONFIG_IP_VS_SH_TAB_BITS
67 #define CONFIG_IP_VS_SH_TAB_BITS 8
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)
73 struct ip_vs_sh_state {
74 struct rcu_head rcu_head;
75 struct ip_vs_sh_bucket buckets[IP_VS_SH_TAB_SIZE];
78 /* Helper function to determine if server is unavailable */
79 static inline bool is_unavailable(struct ip_vs_dest *dest)
81 return atomic_read(&dest->weight) <= 0 ||
82 dest->flags & IP_VS_DEST_F_OVERLOAD;
86 * Returns hash value for IPVS SH entry
88 static inline unsigned int
89 ip_vs_sh_hashkey(int af, const union nf_inet_addr *addr,
90 __be16 port, unsigned int offset)
92 __be32 addr_fold = addr->ip;
94 #ifdef CONFIG_IP_VS_IPV6
96 addr_fold = addr->ip6[0]^addr->ip6[1]^
97 addr->ip6[2]^addr->ip6[3];
99 return (offset + (ntohs(port) + ntohl(addr_fold))*2654435761UL) &
105 * Get ip_vs_dest associated with supplied parameters.
107 static inline struct ip_vs_dest *
108 ip_vs_sh_get(struct ip_vs_service *svc, struct ip_vs_sh_state *s,
109 const union nf_inet_addr *addr, __be16 port)
111 unsigned int hash = ip_vs_sh_hashkey(svc->af, addr, port, 0);
112 struct ip_vs_dest *dest = rcu_dereference(s->buckets[hash].dest);
114 return (!dest || is_unavailable(dest)) ? NULL : dest;
118 /* As ip_vs_sh_get, but with fallback if selected server is unavailable */
119 static inline struct ip_vs_dest *
120 ip_vs_sh_get_fallback(struct ip_vs_service *svc, struct ip_vs_sh_state *s,
121 const union nf_inet_addr *addr, __be16 port)
125 struct ip_vs_dest *dest;
127 for (offset = 0; offset < IP_VS_SH_TAB_SIZE; offset++) {
128 hash = ip_vs_sh_hashkey(svc->af, addr, port, offset);
129 dest = rcu_dereference(s->buckets[hash].dest);
132 if (is_unavailable(dest))
133 IP_VS_DBG_BUF(6, "SH: selected unavailable server "
135 IP_VS_DBG_ADDR(svc->af, &dest->addr),
136 ntohs(dest->port), offset);
145 * Assign all the hash buckets of the specified table with the service.
148 ip_vs_sh_reassign(struct ip_vs_sh_state *s, struct ip_vs_service *svc)
151 struct ip_vs_sh_bucket *b;
153 struct ip_vs_dest *dest;
158 p = &svc->destinations;
159 empty = list_empty(p);
161 for (i=0; i<IP_VS_SH_TAB_SIZE; i++) {
162 dest = rcu_dereference_protected(b->dest, 1);
164 ip_vs_dest_put(dest);
166 RCU_INIT_POINTER(b->dest, NULL);
168 if (p == &svc->destinations)
171 dest = list_entry(p, struct ip_vs_dest, n_list);
172 ip_vs_dest_hold(dest);
173 RCU_INIT_POINTER(b->dest, dest);
175 IP_VS_DBG_BUF(6, "assigned i: %d dest: %s weight: %d\n",
176 i, IP_VS_DBG_ADDR(svc->af, &dest->addr),
177 atomic_read(&dest->weight));
179 /* Don't move to next dest until filling weight */
180 if (++d_count >= atomic_read(&dest->weight)) {
193 * Flush all the hash buckets of the specified table.
195 static void ip_vs_sh_flush(struct ip_vs_sh_state *s)
198 struct ip_vs_sh_bucket *b;
199 struct ip_vs_dest *dest;
202 for (i=0; i<IP_VS_SH_TAB_SIZE; i++) {
203 dest = rcu_dereference_protected(b->dest, 1);
205 ip_vs_dest_put(dest);
206 RCU_INIT_POINTER(b->dest, NULL);
213 static int ip_vs_sh_init_svc(struct ip_vs_service *svc)
215 struct ip_vs_sh_state *s;
217 /* allocate the SH table for this service */
218 s = kzalloc(sizeof(struct ip_vs_sh_state), GFP_KERNEL);
223 IP_VS_DBG(6, "SH hash table (memory=%Zdbytes) allocated for "
225 sizeof(struct ip_vs_sh_bucket)*IP_VS_SH_TAB_SIZE);
227 /* assign the hash buckets with current dests */
228 ip_vs_sh_reassign(s, svc);
234 static void ip_vs_sh_done_svc(struct ip_vs_service *svc)
236 struct ip_vs_sh_state *s = svc->sched_data;
238 /* got to clean up hash buckets here */
241 /* release the table itself */
242 kfree_rcu(s, rcu_head);
243 IP_VS_DBG(6, "SH hash table (memory=%Zdbytes) released\n",
244 sizeof(struct ip_vs_sh_bucket)*IP_VS_SH_TAB_SIZE);
248 static int ip_vs_sh_dest_changed(struct ip_vs_service *svc,
249 struct ip_vs_dest *dest)
251 struct ip_vs_sh_state *s = svc->sched_data;
253 /* assign the hash buckets with the updated service */
254 ip_vs_sh_reassign(s, svc);
260 /* Helper function to get port number */
262 ip_vs_sh_get_port(const struct sk_buff *skb, struct ip_vs_iphdr *iph)
265 struct tcphdr _tcph, *th;
266 struct udphdr _udph, *uh;
267 sctp_sctphdr_t _sctph, *sh;
269 switch (iph->protocol) {
271 th = skb_header_pointer(skb, iph->len, sizeof(_tcph), &_tcph);
272 if (unlikely(th == NULL))
277 uh = skb_header_pointer(skb, iph->len, sizeof(_udph), &_udph);
278 if (unlikely(uh == NULL))
283 sh = skb_header_pointer(skb, iph->len, sizeof(_sctph), &_sctph);
284 if (unlikely(sh == NULL))
297 * Source Hashing scheduling
299 static struct ip_vs_dest *
300 ip_vs_sh_schedule(struct ip_vs_service *svc, const struct sk_buff *skb,
301 struct ip_vs_iphdr *iph)
303 struct ip_vs_dest *dest;
304 struct ip_vs_sh_state *s;
307 IP_VS_DBG(6, "ip_vs_sh_schedule(): Scheduling...\n");
309 if (svc->flags & IP_VS_SVC_F_SCHED_SH_PORT)
310 port = ip_vs_sh_get_port(skb, iph);
312 s = (struct ip_vs_sh_state *) svc->sched_data;
314 if (svc->flags & IP_VS_SVC_F_SCHED_SH_FALLBACK)
315 dest = ip_vs_sh_get_fallback(svc, s, &iph->saddr, port);
317 dest = ip_vs_sh_get(svc, s, &iph->saddr, port);
320 ip_vs_scheduler_err(svc, "no destination available");
324 IP_VS_DBG_BUF(6, "SH: source IP address %s --> server %s:%d\n",
325 IP_VS_DBG_ADDR(svc->af, &iph->saddr),
326 IP_VS_DBG_ADDR(svc->af, &dest->addr),
334 * IPVS SH Scheduler structure
336 static struct ip_vs_scheduler ip_vs_sh_scheduler =
339 .refcnt = ATOMIC_INIT(0),
340 .module = THIS_MODULE,
341 .n_list = LIST_HEAD_INIT(ip_vs_sh_scheduler.n_list),
342 .init_service = ip_vs_sh_init_svc,
343 .done_service = ip_vs_sh_done_svc,
344 .add_dest = ip_vs_sh_dest_changed,
345 .del_dest = ip_vs_sh_dest_changed,
346 .upd_dest = ip_vs_sh_dest_changed,
347 .schedule = ip_vs_sh_schedule,
351 static int __init ip_vs_sh_init(void)
353 return register_ip_vs_scheduler(&ip_vs_sh_scheduler);
357 static void __exit ip_vs_sh_cleanup(void)
359 unregister_ip_vs_scheduler(&ip_vs_sh_scheduler);
364 module_init(ip_vs_sh_init);
365 module_exit(ip_vs_sh_cleanup);
366 MODULE_LICENSE("GPL");