2 * IPVS: Source Hashing scheduling module
4 * Version: $Id: ip_vs_sh.c,v 1.5 2002/09/15 08:14:08 wensong Exp $
6 * Authors: Wensong Zhang <wensong@gnuchina.org>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
18 * The sh algorithm is to select server by the hash key of source IP
19 * address. The pseudo code is as follows:
21 * n <- servernode[src_ip];
23 * (n is overloaded) or (n.weight <= 0) then
28 * Notes that servernode is a 256-bucket hash table that maps the hash
29 * index derived from packet source IP address to the current server
30 * array. If the sh scheduler is used in cache cluster, it is good to
31 * combine it with cache_bypass feature. When the statically assigned
32 * server is dead or overloaded, the load balancer can bypass the cache
33 * server and send requests to the original server directly.
38 #include <linux/module.h>
39 #include <linux/kernel.h>
40 #include <linux/skbuff.h>
42 #include <net/ip_vs.h>
48 struct ip_vs_sh_bucket {
49 struct ip_vs_dest *dest; /* real server (cache) */
53 * for IPVS SH entry hash table
55 #ifndef CONFIG_IP_VS_SH_TAB_BITS
56 #define CONFIG_IP_VS_SH_TAB_BITS 8
58 #define IP_VS_SH_TAB_BITS CONFIG_IP_VS_SH_TAB_BITS
59 #define IP_VS_SH_TAB_SIZE (1 << IP_VS_SH_TAB_BITS)
60 #define IP_VS_SH_TAB_MASK (IP_VS_SH_TAB_SIZE - 1)
64 * Returns hash value for IPVS SH entry
66 static inline unsigned ip_vs_sh_hashkey(__be32 addr)
68 return (ntohl(addr)*2654435761UL) & IP_VS_SH_TAB_MASK;
73 * Get ip_vs_dest associated with supplied parameters.
75 static inline struct ip_vs_dest *
76 ip_vs_sh_get(struct ip_vs_sh_bucket *tbl, __be32 addr)
78 return (tbl[ip_vs_sh_hashkey(addr)]).dest;
83 * Assign all the hash buckets of the specified table with the service.
86 ip_vs_sh_assign(struct ip_vs_sh_bucket *tbl, struct ip_vs_service *svc)
89 struct ip_vs_sh_bucket *b;
91 struct ip_vs_dest *dest;
94 p = &svc->destinations;
95 for (i=0; i<IP_VS_SH_TAB_SIZE; i++) {
99 if (p == &svc->destinations)
102 dest = list_entry(p, struct ip_vs_dest, n_list);
103 atomic_inc(&dest->refcnt);
115 * Flush all the hash buckets of the specified table.
117 static void ip_vs_sh_flush(struct ip_vs_sh_bucket *tbl)
120 struct ip_vs_sh_bucket *b;
123 for (i=0; i<IP_VS_SH_TAB_SIZE; i++) {
125 atomic_dec(&b->dest->refcnt);
133 static int ip_vs_sh_init_svc(struct ip_vs_service *svc)
135 struct ip_vs_sh_bucket *tbl;
137 /* allocate the SH table for this service */
138 tbl = kmalloc(sizeof(struct ip_vs_sh_bucket)*IP_VS_SH_TAB_SIZE,
141 IP_VS_ERR("ip_vs_sh_init_svc(): no memory\n");
144 svc->sched_data = tbl;
145 IP_VS_DBG(6, "SH hash table (memory=%Zdbytes) allocated for "
147 sizeof(struct ip_vs_sh_bucket)*IP_VS_SH_TAB_SIZE);
149 /* assign the hash buckets with the updated service */
150 ip_vs_sh_assign(tbl, svc);
156 static int ip_vs_sh_done_svc(struct ip_vs_service *svc)
158 struct ip_vs_sh_bucket *tbl = svc->sched_data;
160 /* got to clean up hash buckets here */
163 /* release the table itself */
164 kfree(svc->sched_data);
165 IP_VS_DBG(6, "SH hash table (memory=%Zdbytes) released\n",
166 sizeof(struct ip_vs_sh_bucket)*IP_VS_SH_TAB_SIZE);
172 static int ip_vs_sh_update_svc(struct ip_vs_service *svc)
174 struct ip_vs_sh_bucket *tbl = svc->sched_data;
176 /* got to clean up hash buckets here */
179 /* assign the hash buckets with the updated service */
180 ip_vs_sh_assign(tbl, svc);
187 * If the dest flags is set with IP_VS_DEST_F_OVERLOAD,
188 * consider that the server is overloaded here.
190 static inline int is_overloaded(struct ip_vs_dest *dest)
192 return dest->flags & IP_VS_DEST_F_OVERLOAD;
197 * Source Hashing scheduling
199 static struct ip_vs_dest *
200 ip_vs_sh_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
202 struct ip_vs_dest *dest;
203 struct ip_vs_sh_bucket *tbl;
204 struct iphdr *iph = skb->nh.iph;
206 IP_VS_DBG(6, "ip_vs_sh_schedule(): Scheduling...\n");
208 tbl = (struct ip_vs_sh_bucket *)svc->sched_data;
209 dest = ip_vs_sh_get(tbl, iph->saddr);
211 || !(dest->flags & IP_VS_DEST_F_AVAILABLE)
212 || atomic_read(&dest->weight) <= 0
213 || is_overloaded(dest)) {
217 IP_VS_DBG(6, "SH: source IP address %u.%u.%u.%u "
218 "--> server %u.%u.%u.%u:%d\n",
228 * IPVS SH Scheduler structure
230 static struct ip_vs_scheduler ip_vs_sh_scheduler =
233 .refcnt = ATOMIC_INIT(0),
234 .module = THIS_MODULE,
235 .init_service = ip_vs_sh_init_svc,
236 .done_service = ip_vs_sh_done_svc,
237 .update_service = ip_vs_sh_update_svc,
238 .schedule = ip_vs_sh_schedule,
242 static int __init ip_vs_sh_init(void)
244 INIT_LIST_HEAD(&ip_vs_sh_scheduler.n_list);
245 return register_ip_vs_scheduler(&ip_vs_sh_scheduler);
249 static void __exit ip_vs_sh_cleanup(void)
251 unregister_ip_vs_scheduler(&ip_vs_sh_scheduler);
255 module_init(ip_vs_sh_init);
256 module_exit(ip_vs_sh_cleanup);
257 MODULE_LICENSE("GPL");