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/module.h>
40 #include <linux/kernel.h>
41 #include <linux/skbuff.h>
43 #include <net/ip_vs.h>
49 struct ip_vs_sh_bucket {
50 struct ip_vs_dest *dest; /* real server (cache) */
54 * for IPVS SH entry hash table
56 #ifndef CONFIG_IP_VS_SH_TAB_BITS
57 #define CONFIG_IP_VS_SH_TAB_BITS 8
59 #define IP_VS_SH_TAB_BITS CONFIG_IP_VS_SH_TAB_BITS
60 #define IP_VS_SH_TAB_SIZE (1 << IP_VS_SH_TAB_BITS)
61 #define IP_VS_SH_TAB_MASK (IP_VS_SH_TAB_SIZE - 1)
65 * Returns hash value for IPVS SH entry
67 static inline unsigned ip_vs_sh_hashkey(int af, const union nf_inet_addr *addr)
69 __be32 addr_fold = addr->ip;
71 #ifdef CONFIG_IP_VS_IPV6
73 addr_fold = addr->ip6[0]^addr->ip6[1]^
74 addr->ip6[2]^addr->ip6[3];
76 return (ntohl(addr_fold)*2654435761UL) & IP_VS_SH_TAB_MASK;
81 * Get ip_vs_dest associated with supplied parameters.
83 static inline struct ip_vs_dest *
84 ip_vs_sh_get(int af, struct ip_vs_sh_bucket *tbl,
85 const union nf_inet_addr *addr)
87 return (tbl[ip_vs_sh_hashkey(af, addr)]).dest;
92 * Assign all the hash buckets of the specified table with the service.
95 ip_vs_sh_assign(struct ip_vs_sh_bucket *tbl, struct ip_vs_service *svc)
98 struct ip_vs_sh_bucket *b;
100 struct ip_vs_dest *dest;
103 p = &svc->destinations;
104 for (i=0; i<IP_VS_SH_TAB_SIZE; i++) {
108 if (p == &svc->destinations)
111 dest = list_entry(p, struct ip_vs_dest, n_list);
112 atomic_inc(&dest->refcnt);
124 * Flush all the hash buckets of the specified table.
126 static void ip_vs_sh_flush(struct ip_vs_sh_bucket *tbl)
129 struct ip_vs_sh_bucket *b;
132 for (i=0; i<IP_VS_SH_TAB_SIZE; i++) {
134 atomic_dec(&b->dest->refcnt);
142 static int ip_vs_sh_init_svc(struct ip_vs_service *svc)
144 struct ip_vs_sh_bucket *tbl;
146 /* allocate the SH table for this service */
147 tbl = kmalloc(sizeof(struct ip_vs_sh_bucket)*IP_VS_SH_TAB_SIZE,
150 pr_err("%s(): no memory\n", __func__);
153 svc->sched_data = tbl;
154 IP_VS_DBG(6, "SH hash table (memory=%Zdbytes) allocated for "
156 sizeof(struct ip_vs_sh_bucket)*IP_VS_SH_TAB_SIZE);
158 /* assign the hash buckets with the updated service */
159 ip_vs_sh_assign(tbl, svc);
165 static int ip_vs_sh_done_svc(struct ip_vs_service *svc)
167 struct ip_vs_sh_bucket *tbl = svc->sched_data;
169 /* got to clean up hash buckets here */
172 /* release the table itself */
173 kfree(svc->sched_data);
174 IP_VS_DBG(6, "SH hash table (memory=%Zdbytes) released\n",
175 sizeof(struct ip_vs_sh_bucket)*IP_VS_SH_TAB_SIZE);
181 static int ip_vs_sh_update_svc(struct ip_vs_service *svc)
183 struct ip_vs_sh_bucket *tbl = svc->sched_data;
185 /* got to clean up hash buckets here */
188 /* assign the hash buckets with the updated service */
189 ip_vs_sh_assign(tbl, svc);
196 * If the dest flags is set with IP_VS_DEST_F_OVERLOAD,
197 * consider that the server is overloaded here.
199 static inline int is_overloaded(struct ip_vs_dest *dest)
201 return dest->flags & IP_VS_DEST_F_OVERLOAD;
206 * Source Hashing scheduling
208 static struct ip_vs_dest *
209 ip_vs_sh_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
211 struct ip_vs_dest *dest;
212 struct ip_vs_sh_bucket *tbl;
213 struct ip_vs_iphdr iph;
215 ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
217 IP_VS_DBG(6, "ip_vs_sh_schedule(): Scheduling...\n");
219 tbl = (struct ip_vs_sh_bucket *)svc->sched_data;
220 dest = ip_vs_sh_get(svc->af, tbl, &iph.saddr);
222 || !(dest->flags & IP_VS_DEST_F_AVAILABLE)
223 || atomic_read(&dest->weight) <= 0
224 || is_overloaded(dest)) {
225 IP_VS_ERR_RL("SH: no destination available\n");
229 IP_VS_DBG_BUF(6, "SH: source IP address %s --> server %s:%d\n",
230 IP_VS_DBG_ADDR(svc->af, &iph.saddr),
231 IP_VS_DBG_ADDR(svc->af, &dest->addr),
239 * IPVS SH Scheduler structure
241 static struct ip_vs_scheduler ip_vs_sh_scheduler =
244 .refcnt = ATOMIC_INIT(0),
245 .module = THIS_MODULE,
246 .n_list = LIST_HEAD_INIT(ip_vs_sh_scheduler.n_list),
247 .init_service = ip_vs_sh_init_svc,
248 .done_service = ip_vs_sh_done_svc,
249 .update_service = ip_vs_sh_update_svc,
250 .schedule = ip_vs_sh_schedule,
254 static int __init ip_vs_sh_init(void)
256 return register_ip_vs_scheduler(&ip_vs_sh_scheduler);
260 static void __exit ip_vs_sh_cleanup(void)
262 unregister_ip_vs_scheduler(&ip_vs_sh_scheduler);
266 module_init(ip_vs_sh_init);
267 module_exit(ip_vs_sh_cleanup);
268 MODULE_LICENSE("GPL");