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.
35 #define KMSG_COMPONENT "IPVS"
36 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
39 #include <linux/slab.h>
40 #include <linux/module.h>
41 #include <linux/kernel.h>
42 #include <linux/skbuff.h>
44 #include <net/ip_vs.h>
50 struct ip_vs_sh_bucket
{
51 struct ip_vs_dest
*dest
; /* real server (cache) */
55 * for IPVS SH entry hash table
57 #ifndef CONFIG_IP_VS_SH_TAB_BITS
58 #define CONFIG_IP_VS_SH_TAB_BITS 8
60 #define IP_VS_SH_TAB_BITS CONFIG_IP_VS_SH_TAB_BITS
61 #define IP_VS_SH_TAB_SIZE (1 << IP_VS_SH_TAB_BITS)
62 #define IP_VS_SH_TAB_MASK (IP_VS_SH_TAB_SIZE - 1)
66 * Returns hash value for IPVS SH entry
68 static inline unsigned ip_vs_sh_hashkey(int af
, const union nf_inet_addr
*addr
)
70 __be32 addr_fold
= addr
->ip
;
72 #ifdef CONFIG_IP_VS_IPV6
74 addr_fold
= addr
->ip6
[0]^addr
->ip6
[1]^
75 addr
->ip6
[2]^addr
->ip6
[3];
77 return (ntohl(addr_fold
)*2654435761UL) & IP_VS_SH_TAB_MASK
;
82 * Get ip_vs_dest associated with supplied parameters.
84 static inline struct ip_vs_dest
*
85 ip_vs_sh_get(int af
, struct ip_vs_sh_bucket
*tbl
,
86 const union nf_inet_addr
*addr
)
88 return (tbl
[ip_vs_sh_hashkey(af
, addr
)]).dest
;
93 * Assign all the hash buckets of the specified table with the service.
96 ip_vs_sh_assign(struct ip_vs_sh_bucket
*tbl
, struct ip_vs_service
*svc
)
99 struct ip_vs_sh_bucket
*b
;
101 struct ip_vs_dest
*dest
;
104 p
= &svc
->destinations
;
105 for (i
=0; i
<IP_VS_SH_TAB_SIZE
; i
++) {
109 if (p
== &svc
->destinations
)
112 dest
= list_entry(p
, struct ip_vs_dest
, n_list
);
113 atomic_inc(&dest
->refcnt
);
125 * Flush all the hash buckets of the specified table.
127 static void ip_vs_sh_flush(struct ip_vs_sh_bucket
*tbl
)
130 struct ip_vs_sh_bucket
*b
;
133 for (i
=0; i
<IP_VS_SH_TAB_SIZE
; i
++) {
135 atomic_dec(&b
->dest
->refcnt
);
143 static int ip_vs_sh_init_svc(struct ip_vs_service
*svc
)
145 struct ip_vs_sh_bucket
*tbl
;
147 /* allocate the SH table for this service */
148 tbl
= kmalloc(sizeof(struct ip_vs_sh_bucket
)*IP_VS_SH_TAB_SIZE
,
151 pr_err("%s(): no memory\n", __func__
);
154 svc
->sched_data
= tbl
;
155 IP_VS_DBG(6, "SH hash table (memory=%Zdbytes) allocated for "
157 sizeof(struct ip_vs_sh_bucket
)*IP_VS_SH_TAB_SIZE
);
159 /* assign the hash buckets with the updated service */
160 ip_vs_sh_assign(tbl
, svc
);
166 static int ip_vs_sh_done_svc(struct ip_vs_service
*svc
)
168 struct ip_vs_sh_bucket
*tbl
= svc
->sched_data
;
170 /* got to clean up hash buckets here */
173 /* release the table itself */
174 kfree(svc
->sched_data
);
175 IP_VS_DBG(6, "SH hash table (memory=%Zdbytes) released\n",
176 sizeof(struct ip_vs_sh_bucket
)*IP_VS_SH_TAB_SIZE
);
182 static int ip_vs_sh_update_svc(struct ip_vs_service
*svc
)
184 struct ip_vs_sh_bucket
*tbl
= svc
->sched_data
;
186 /* got to clean up hash buckets here */
189 /* assign the hash buckets with the updated service */
190 ip_vs_sh_assign(tbl
, svc
);
197 * If the dest flags is set with IP_VS_DEST_F_OVERLOAD,
198 * consider that the server is overloaded here.
200 static inline int is_overloaded(struct ip_vs_dest
*dest
)
202 return dest
->flags
& IP_VS_DEST_F_OVERLOAD
;
207 * Source Hashing scheduling
209 static struct ip_vs_dest
*
210 ip_vs_sh_schedule(struct ip_vs_service
*svc
, const struct sk_buff
*skb
)
212 struct ip_vs_dest
*dest
;
213 struct ip_vs_sh_bucket
*tbl
;
214 struct ip_vs_iphdr iph
;
216 ip_vs_fill_iphdr(svc
->af
, skb_network_header(skb
), &iph
);
218 IP_VS_DBG(6, "ip_vs_sh_schedule(): Scheduling...\n");
220 tbl
= (struct ip_vs_sh_bucket
*)svc
->sched_data
;
221 dest
= ip_vs_sh_get(svc
->af
, tbl
, &iph
.saddr
);
223 || !(dest
->flags
& IP_VS_DEST_F_AVAILABLE
)
224 || atomic_read(&dest
->weight
) <= 0
225 || is_overloaded(dest
)) {
226 ip_vs_scheduler_err(svc
, "no destination available");
230 IP_VS_DBG_BUF(6, "SH: source IP address %s --> server %s:%d\n",
231 IP_VS_DBG_ADDR(svc
->af
, &iph
.saddr
),
232 IP_VS_DBG_ADDR(svc
->af
, &dest
->addr
),
240 * IPVS SH Scheduler structure
242 static struct ip_vs_scheduler ip_vs_sh_scheduler
=
245 .refcnt
= ATOMIC_INIT(0),
246 .module
= THIS_MODULE
,
247 .n_list
= LIST_HEAD_INIT(ip_vs_sh_scheduler
.n_list
),
248 .init_service
= ip_vs_sh_init_svc
,
249 .done_service
= ip_vs_sh_done_svc
,
250 .update_service
= ip_vs_sh_update_svc
,
251 .schedule
= ip_vs_sh_schedule
,
255 static int __init
ip_vs_sh_init(void)
257 return register_ip_vs_scheduler(&ip_vs_sh_scheduler
);
261 static void __exit
ip_vs_sh_cleanup(void)
263 unregister_ip_vs_scheduler(&ip_vs_sh_scheduler
);
267 module_init(ip_vs_sh_init
);
268 module_exit(ip_vs_sh_cleanup
);
269 MODULE_LICENSE("GPL");