added 2.6.29.6 aldebaran kernel
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / net / netfilter / ipvs / ip_vs_sh.c
blob75709ebeb630384893fb56ef4c22d4df9b3a3eb0
1 /*
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.
11 * Changes:
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];
20 * if (n is dead) OR
21 * (n is overloaded) or (n.weight <= 0) then
22 * return NULL;
24 * return n;
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 #include <linux/ip.h>
36 #include <linux/module.h>
37 #include <linux/kernel.h>
38 #include <linux/skbuff.h>
40 #include <net/ip_vs.h>
44 * IPVS SH bucket
46 struct ip_vs_sh_bucket {
47 struct ip_vs_dest *dest; /* real server (cache) */
51 * for IPVS SH entry hash table
53 #ifndef CONFIG_IP_VS_SH_TAB_BITS
54 #define CONFIG_IP_VS_SH_TAB_BITS 8
55 #endif
56 #define IP_VS_SH_TAB_BITS CONFIG_IP_VS_SH_TAB_BITS
57 #define IP_VS_SH_TAB_SIZE (1 << IP_VS_SH_TAB_BITS)
58 #define IP_VS_SH_TAB_MASK (IP_VS_SH_TAB_SIZE - 1)
62 * Returns hash value for IPVS SH entry
64 static inline unsigned ip_vs_sh_hashkey(int af, const union nf_inet_addr *addr)
66 __be32 addr_fold = addr->ip;
68 #ifdef CONFIG_IP_VS_IPV6
69 if (af == AF_INET6)
70 addr_fold = addr->ip6[0]^addr->ip6[1]^
71 addr->ip6[2]^addr->ip6[3];
72 #endif
73 return (ntohl(addr_fold)*2654435761UL) & IP_VS_SH_TAB_MASK;
78 * Get ip_vs_dest associated with supplied parameters.
80 static inline struct ip_vs_dest *
81 ip_vs_sh_get(int af, struct ip_vs_sh_bucket *tbl,
82 const union nf_inet_addr *addr)
84 return (tbl[ip_vs_sh_hashkey(af, addr)]).dest;
89 * Assign all the hash buckets of the specified table with the service.
91 static int
92 ip_vs_sh_assign(struct ip_vs_sh_bucket *tbl, struct ip_vs_service *svc)
94 int i;
95 struct ip_vs_sh_bucket *b;
96 struct list_head *p;
97 struct ip_vs_dest *dest;
99 b = tbl;
100 p = &svc->destinations;
101 for (i=0; i<IP_VS_SH_TAB_SIZE; i++) {
102 if (list_empty(p)) {
103 b->dest = NULL;
104 } else {
105 if (p == &svc->destinations)
106 p = p->next;
108 dest = list_entry(p, struct ip_vs_dest, n_list);
109 atomic_inc(&dest->refcnt);
110 b->dest = dest;
112 p = p->next;
114 b++;
116 return 0;
121 * Flush all the hash buckets of the specified table.
123 static void ip_vs_sh_flush(struct ip_vs_sh_bucket *tbl)
125 int i;
126 struct ip_vs_sh_bucket *b;
128 b = tbl;
129 for (i=0; i<IP_VS_SH_TAB_SIZE; i++) {
130 if (b->dest) {
131 atomic_dec(&b->dest->refcnt);
132 b->dest = NULL;
134 b++;
139 static int ip_vs_sh_init_svc(struct ip_vs_service *svc)
141 struct ip_vs_sh_bucket *tbl;
143 /* allocate the SH table for this service */
144 tbl = kmalloc(sizeof(struct ip_vs_sh_bucket)*IP_VS_SH_TAB_SIZE,
145 GFP_ATOMIC);
146 if (tbl == NULL) {
147 IP_VS_ERR("ip_vs_sh_init_svc(): no memory\n");
148 return -ENOMEM;
150 svc->sched_data = tbl;
151 IP_VS_DBG(6, "SH hash table (memory=%Zdbytes) allocated for "
152 "current service\n",
153 sizeof(struct ip_vs_sh_bucket)*IP_VS_SH_TAB_SIZE);
155 /* assign the hash buckets with the updated service */
156 ip_vs_sh_assign(tbl, svc);
158 return 0;
162 static int ip_vs_sh_done_svc(struct ip_vs_service *svc)
164 struct ip_vs_sh_bucket *tbl = svc->sched_data;
166 /* got to clean up hash buckets here */
167 ip_vs_sh_flush(tbl);
169 /* release the table itself */
170 kfree(svc->sched_data);
171 IP_VS_DBG(6, "SH hash table (memory=%Zdbytes) released\n",
172 sizeof(struct ip_vs_sh_bucket)*IP_VS_SH_TAB_SIZE);
174 return 0;
178 static int ip_vs_sh_update_svc(struct ip_vs_service *svc)
180 struct ip_vs_sh_bucket *tbl = svc->sched_data;
182 /* got to clean up hash buckets here */
183 ip_vs_sh_flush(tbl);
185 /* assign the hash buckets with the updated service */
186 ip_vs_sh_assign(tbl, svc);
188 return 0;
193 * If the dest flags is set with IP_VS_DEST_F_OVERLOAD,
194 * consider that the server is overloaded here.
196 static inline int is_overloaded(struct ip_vs_dest *dest)
198 return dest->flags & IP_VS_DEST_F_OVERLOAD;
203 * Source Hashing scheduling
205 static struct ip_vs_dest *
206 ip_vs_sh_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
208 struct ip_vs_dest *dest;
209 struct ip_vs_sh_bucket *tbl;
210 struct ip_vs_iphdr iph;
212 ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
214 IP_VS_DBG(6, "ip_vs_sh_schedule(): Scheduling...\n");
216 tbl = (struct ip_vs_sh_bucket *)svc->sched_data;
217 dest = ip_vs_sh_get(svc->af, tbl, &iph.saddr);
218 if (!dest
219 || !(dest->flags & IP_VS_DEST_F_AVAILABLE)
220 || atomic_read(&dest->weight) <= 0
221 || is_overloaded(dest)) {
222 IP_VS_ERR_RL("SH: no destination available\n");
223 return NULL;
226 IP_VS_DBG_BUF(6, "SH: source IP address %s --> server %s:%d\n",
227 IP_VS_DBG_ADDR(svc->af, &iph.saddr),
228 IP_VS_DBG_ADDR(svc->af, &dest->addr),
229 ntohs(dest->port));
231 return dest;
236 * IPVS SH Scheduler structure
238 static struct ip_vs_scheduler ip_vs_sh_scheduler =
240 .name = "sh",
241 .refcnt = ATOMIC_INIT(0),
242 .module = THIS_MODULE,
243 .n_list = LIST_HEAD_INIT(ip_vs_sh_scheduler.n_list),
244 .init_service = ip_vs_sh_init_svc,
245 .done_service = ip_vs_sh_done_svc,
246 .update_service = ip_vs_sh_update_svc,
247 .schedule = ip_vs_sh_schedule,
251 static int __init ip_vs_sh_init(void)
253 return register_ip_vs_scheduler(&ip_vs_sh_scheduler);
257 static void __exit ip_vs_sh_cleanup(void)
259 unregister_ip_vs_scheduler(&ip_vs_sh_scheduler);
263 module_init(ip_vs_sh_init);
264 module_exit(ip_vs_sh_cleanup);
265 MODULE_LICENSE("GPL");