2 * IPVS: Destination Hashing scheduling module
4 * Version: $Id: ip_vs_dh.c,v 1.5 2002/09/15 08:14:08 wensong Exp $
6 * Authors: Wensong Zhang <wensong@gnuchina.org>
8 * Inspired by the consistent hashing scheduler patch from
9 * Thomas Proell <proellt@gmx.de>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
21 * The dh algorithm is to select server by the hash key of destination IP
22 * address. The pseudo code is as follows:
24 * n <- servernode[dest_ip];
26 * (n is overloaded) OR (n.weight <= 0) then
31 * Notes that servernode is a 256-bucket hash table that maps the hash
32 * index derived from packet destination IP address to the current server
33 * array. If the dh scheduler is used in cache cluster, it is good to
34 * combine it with cache_bypass feature. When the statically assigned
35 * server is dead or overloaded, the load balancer can bypass the cache
36 * server and send requests to the original server directly.
41 #include <linux/module.h>
42 #include <linux/kernel.h>
43 #include <linux/skbuff.h>
45 #include <net/ip_vs.h>
51 struct ip_vs_dh_bucket
{
52 struct ip_vs_dest
*dest
; /* real server (cache) */
56 * for IPVS DH entry hash table
58 #ifndef CONFIG_IP_VS_DH_TAB_BITS
59 #define CONFIG_IP_VS_DH_TAB_BITS 8
61 #define IP_VS_DH_TAB_BITS CONFIG_IP_VS_DH_TAB_BITS
62 #define IP_VS_DH_TAB_SIZE (1 << IP_VS_DH_TAB_BITS)
63 #define IP_VS_DH_TAB_MASK (IP_VS_DH_TAB_SIZE - 1)
67 * Returns hash value for IPVS DH entry
69 static inline unsigned ip_vs_dh_hashkey(__be32 addr
)
71 return (ntohl(addr
)*2654435761UL) & IP_VS_DH_TAB_MASK
;
76 * Get ip_vs_dest associated with supplied parameters.
78 static inline struct ip_vs_dest
*
79 ip_vs_dh_get(struct ip_vs_dh_bucket
*tbl
, __be32 addr
)
81 return (tbl
[ip_vs_dh_hashkey(addr
)]).dest
;
86 * Assign all the hash buckets of the specified table with the service.
89 ip_vs_dh_assign(struct ip_vs_dh_bucket
*tbl
, struct ip_vs_service
*svc
)
92 struct ip_vs_dh_bucket
*b
;
94 struct ip_vs_dest
*dest
;
97 p
= &svc
->destinations
;
98 for (i
=0; i
<IP_VS_DH_TAB_SIZE
; i
++) {
102 if (p
== &svc
->destinations
)
105 dest
= list_entry(p
, struct ip_vs_dest
, n_list
);
106 atomic_inc(&dest
->refcnt
);
118 * Flush all the hash buckets of the specified table.
120 static void ip_vs_dh_flush(struct ip_vs_dh_bucket
*tbl
)
123 struct ip_vs_dh_bucket
*b
;
126 for (i
=0; i
<IP_VS_DH_TAB_SIZE
; i
++) {
128 atomic_dec(&b
->dest
->refcnt
);
136 static int ip_vs_dh_init_svc(struct ip_vs_service
*svc
)
138 struct ip_vs_dh_bucket
*tbl
;
140 /* allocate the DH table for this service */
141 tbl
= kmalloc(sizeof(struct ip_vs_dh_bucket
)*IP_VS_DH_TAB_SIZE
,
144 IP_VS_ERR("ip_vs_dh_init_svc(): no memory\n");
147 svc
->sched_data
= tbl
;
148 IP_VS_DBG(6, "DH hash table (memory=%Zdbytes) allocated for "
150 sizeof(struct ip_vs_dh_bucket
)*IP_VS_DH_TAB_SIZE
);
152 /* assign the hash buckets with the updated service */
153 ip_vs_dh_assign(tbl
, svc
);
159 static int ip_vs_dh_done_svc(struct ip_vs_service
*svc
)
161 struct ip_vs_dh_bucket
*tbl
= svc
->sched_data
;
163 /* got to clean up hash buckets here */
166 /* release the table itself */
167 kfree(svc
->sched_data
);
168 IP_VS_DBG(6, "DH hash table (memory=%Zdbytes) released\n",
169 sizeof(struct ip_vs_dh_bucket
)*IP_VS_DH_TAB_SIZE
);
175 static int ip_vs_dh_update_svc(struct ip_vs_service
*svc
)
177 struct ip_vs_dh_bucket
*tbl
= svc
->sched_data
;
179 /* got to clean up hash buckets here */
182 /* assign the hash buckets with the updated service */
183 ip_vs_dh_assign(tbl
, svc
);
190 * If the dest flags is set with IP_VS_DEST_F_OVERLOAD,
191 * consider that the server is overloaded here.
193 static inline int is_overloaded(struct ip_vs_dest
*dest
)
195 return dest
->flags
& IP_VS_DEST_F_OVERLOAD
;
200 * Destination hashing scheduling
202 static struct ip_vs_dest
*
203 ip_vs_dh_schedule(struct ip_vs_service
*svc
, const struct sk_buff
*skb
)
205 struct ip_vs_dest
*dest
;
206 struct ip_vs_dh_bucket
*tbl
;
207 struct iphdr
*iph
= ip_hdr(skb
);
209 IP_VS_DBG(6, "ip_vs_dh_schedule(): Scheduling...\n");
211 tbl
= (struct ip_vs_dh_bucket
*)svc
->sched_data
;
212 dest
= ip_vs_dh_get(tbl
, iph
->daddr
);
214 || !(dest
->flags
& IP_VS_DEST_F_AVAILABLE
)
215 || atomic_read(&dest
->weight
) <= 0
216 || is_overloaded(dest
)) {
220 IP_VS_DBG(6, "DH: destination IP address %u.%u.%u.%u "
221 "--> server %u.%u.%u.%u:%d\n",
231 * IPVS DH Scheduler structure
233 static struct ip_vs_scheduler ip_vs_dh_scheduler
=
236 .refcnt
= ATOMIC_INIT(0),
237 .module
= THIS_MODULE
,
238 .init_service
= ip_vs_dh_init_svc
,
239 .done_service
= ip_vs_dh_done_svc
,
240 .update_service
= ip_vs_dh_update_svc
,
241 .schedule
= ip_vs_dh_schedule
,
245 static int __init
ip_vs_dh_init(void)
247 INIT_LIST_HEAD(&ip_vs_dh_scheduler
.n_list
);
248 return register_ip_vs_scheduler(&ip_vs_dh_scheduler
);
252 static void __exit
ip_vs_dh_cleanup(void)
254 unregister_ip_vs_scheduler(&ip_vs_dh_scheduler
);
258 module_init(ip_vs_dh_init
);
259 module_exit(ip_vs_dh_cleanup
);
260 MODULE_LICENSE("GPL");