2 * IPVS: Destination Hashing scheduling module
4 * Authors: Wensong Zhang <wensong@gnuchina.org>
6 * Inspired by the consistent hashing scheduler patch from
7 * Thomas Proell <proellt@gmx.de>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
19 * The dh algorithm is to select server by the hash key of destination IP
20 * address. The pseudo code is as follows:
22 * n <- servernode[dest_ip];
24 * (n is overloaded) OR (n.weight <= 0) then
29 * Notes that servernode is a 256-bucket hash table that maps the hash
30 * index derived from packet destination IP address to the current server
31 * array. If the dh scheduler is used in cache cluster, it is good to
32 * combine it with cache_bypass feature. When the statically assigned
33 * server is dead or overloaded, the load balancer can bypass the cache
34 * server and send requests to the original server directly.
39 #include <linux/module.h>
40 #include <linux/kernel.h>
41 #include <linux/skbuff.h>
43 #include <net/ip_vs.h>
49 struct ip_vs_dh_bucket {
50 struct ip_vs_dest *dest; /* real server (cache) */
54 * for IPVS DH entry hash table
56 #ifndef CONFIG_IP_VS_DH_TAB_BITS
57 #define CONFIG_IP_VS_DH_TAB_BITS 8
59 #define IP_VS_DH_TAB_BITS CONFIG_IP_VS_DH_TAB_BITS
60 #define IP_VS_DH_TAB_SIZE (1 << IP_VS_DH_TAB_BITS)
61 #define IP_VS_DH_TAB_MASK (IP_VS_DH_TAB_SIZE - 1)
65 * Returns hash value for IPVS DH entry
67 static inline unsigned ip_vs_dh_hashkey(__be32 addr)
69 return (ntohl(addr)*2654435761UL) & IP_VS_DH_TAB_MASK;
74 * Get ip_vs_dest associated with supplied parameters.
76 static inline struct ip_vs_dest *
77 ip_vs_dh_get(struct ip_vs_dh_bucket *tbl, __be32 addr)
79 return (tbl[ip_vs_dh_hashkey(addr)]).dest;
84 * Assign all the hash buckets of the specified table with the service.
87 ip_vs_dh_assign(struct ip_vs_dh_bucket *tbl, struct ip_vs_service *svc)
90 struct ip_vs_dh_bucket *b;
92 struct ip_vs_dest *dest;
95 p = &svc->destinations;
96 for (i=0; i<IP_VS_DH_TAB_SIZE; i++) {
100 if (p == &svc->destinations)
103 dest = list_entry(p, struct ip_vs_dest, n_list);
104 atomic_inc(&dest->refcnt);
116 * Flush all the hash buckets of the specified table.
118 static void ip_vs_dh_flush(struct ip_vs_dh_bucket *tbl)
121 struct ip_vs_dh_bucket *b;
124 for (i=0; i<IP_VS_DH_TAB_SIZE; i++) {
126 atomic_dec(&b->dest->refcnt);
134 static int ip_vs_dh_init_svc(struct ip_vs_service *svc)
136 struct ip_vs_dh_bucket *tbl;
138 /* allocate the DH table for this service */
139 tbl = kmalloc(sizeof(struct ip_vs_dh_bucket)*IP_VS_DH_TAB_SIZE,
142 IP_VS_ERR("ip_vs_dh_init_svc(): no memory\n");
145 svc->sched_data = tbl;
146 IP_VS_DBG(6, "DH hash table (memory=%Zdbytes) allocated for "
148 sizeof(struct ip_vs_dh_bucket)*IP_VS_DH_TAB_SIZE);
150 /* assign the hash buckets with the updated service */
151 ip_vs_dh_assign(tbl, svc);
157 static int ip_vs_dh_done_svc(struct ip_vs_service *svc)
159 struct ip_vs_dh_bucket *tbl = svc->sched_data;
161 /* got to clean up hash buckets here */
164 /* release the table itself */
165 kfree(svc->sched_data);
166 IP_VS_DBG(6, "DH hash table (memory=%Zdbytes) released\n",
167 sizeof(struct ip_vs_dh_bucket)*IP_VS_DH_TAB_SIZE);
173 static int ip_vs_dh_update_svc(struct ip_vs_service *svc)
175 struct ip_vs_dh_bucket *tbl = svc->sched_data;
177 /* got to clean up hash buckets here */
180 /* assign the hash buckets with the updated service */
181 ip_vs_dh_assign(tbl, svc);
188 * If the dest flags is set with IP_VS_DEST_F_OVERLOAD,
189 * consider that the server is overloaded here.
191 static inline int is_overloaded(struct ip_vs_dest *dest)
193 return dest->flags & IP_VS_DEST_F_OVERLOAD;
198 * Destination hashing scheduling
200 static struct ip_vs_dest *
201 ip_vs_dh_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
203 struct ip_vs_dest *dest;
204 struct ip_vs_dh_bucket *tbl;
205 struct iphdr *iph = ip_hdr(skb);
207 IP_VS_DBG(6, "ip_vs_dh_schedule(): Scheduling...\n");
209 tbl = (struct ip_vs_dh_bucket *)svc->sched_data;
210 dest = ip_vs_dh_get(tbl, iph->daddr);
212 || !(dest->flags & IP_VS_DEST_F_AVAILABLE)
213 || atomic_read(&dest->weight) <= 0
214 || is_overloaded(dest)) {
218 IP_VS_DBG(6, "DH: destination IP address %u.%u.%u.%u "
219 "--> server %u.%u.%u.%u:%d\n",
229 * IPVS DH Scheduler structure
231 static struct ip_vs_scheduler ip_vs_dh_scheduler =
234 .refcnt = ATOMIC_INIT(0),
235 .module = THIS_MODULE,
236 .init_service = ip_vs_dh_init_svc,
237 .done_service = ip_vs_dh_done_svc,
238 .update_service = ip_vs_dh_update_svc,
239 .schedule = ip_vs_dh_schedule,
243 static int __init ip_vs_dh_init(void)
245 INIT_LIST_HEAD(&ip_vs_dh_scheduler.n_list);
246 return register_ip_vs_scheduler(&ip_vs_dh_scheduler);
250 static void __exit ip_vs_dh_cleanup(void)
252 unregister_ip_vs_scheduler(&ip_vs_dh_scheduler);
256 module_init(ip_vs_dh_init);
257 module_exit(ip_vs_dh_cleanup);
258 MODULE_LICENSE("GPL");