ASoC: Blackfin: fix typo in MUTE definition
[linux-2.6] / net / netfilter / ipvs / ip_vs_dh.c
1 /*
2  * IPVS:        Destination Hashing scheduling module
3  *
4  * Authors:     Wensong Zhang <wensong@gnuchina.org>
5  *
6  *              Inspired by the consistent hashing scheduler patch from
7  *              Thomas Proell <proellt@gmx.de>
8  *
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.
13  *
14  * Changes:
15  *
16  */
17
18 /*
19  * The dh algorithm is to select server by the hash key of destination IP
20  * address. The pseudo code is as follows:
21  *
22  *       n <- servernode[dest_ip];
23  *       if (n is dead) OR
24  *          (n is overloaded) OR (n.weight <= 0) then
25  *                 return NULL;
26  *
27  *       return n;
28  *
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.
35  *
36  */
37
38 #include <linux/ip.h>
39 #include <linux/module.h>
40 #include <linux/kernel.h>
41 #include <linux/skbuff.h>
42
43 #include <net/ip_vs.h>
44
45
46 /*
47  *      IPVS DH bucket
48  */
49 struct ip_vs_dh_bucket {
50         struct ip_vs_dest       *dest;          /* real server (cache) */
51 };
52
53 /*
54  *     for IPVS DH entry hash table
55  */
56 #ifndef CONFIG_IP_VS_DH_TAB_BITS
57 #define CONFIG_IP_VS_DH_TAB_BITS        8
58 #endif
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)
62
63
64 /*
65  *      Returns hash value for IPVS DH entry
66  */
67 static inline unsigned ip_vs_dh_hashkey(int af, const union nf_inet_addr *addr)
68 {
69         __be32 addr_fold = addr->ip;
70
71 #ifdef CONFIG_IP_VS_IPV6
72         if (af == AF_INET6)
73                 addr_fold = addr->ip6[0]^addr->ip6[1]^
74                             addr->ip6[2]^addr->ip6[3];
75 #endif
76         return (ntohl(addr_fold)*2654435761UL) & IP_VS_DH_TAB_MASK;
77 }
78
79
80 /*
81  *      Get ip_vs_dest associated with supplied parameters.
82  */
83 static inline struct ip_vs_dest *
84 ip_vs_dh_get(int af, struct ip_vs_dh_bucket *tbl,
85              const union nf_inet_addr *addr)
86 {
87         return (tbl[ip_vs_dh_hashkey(af, addr)]).dest;
88 }
89
90
91 /*
92  *      Assign all the hash buckets of the specified table with the service.
93  */
94 static int
95 ip_vs_dh_assign(struct ip_vs_dh_bucket *tbl, struct ip_vs_service *svc)
96 {
97         int i;
98         struct ip_vs_dh_bucket *b;
99         struct list_head *p;
100         struct ip_vs_dest *dest;
101
102         b = tbl;
103         p = &svc->destinations;
104         for (i=0; i<IP_VS_DH_TAB_SIZE; i++) {
105                 if (list_empty(p)) {
106                         b->dest = NULL;
107                 } else {
108                         if (p == &svc->destinations)
109                                 p = p->next;
110
111                         dest = list_entry(p, struct ip_vs_dest, n_list);
112                         atomic_inc(&dest->refcnt);
113                         b->dest = dest;
114
115                         p = p->next;
116                 }
117                 b++;
118         }
119         return 0;
120 }
121
122
123 /*
124  *      Flush all the hash buckets of the specified table.
125  */
126 static void ip_vs_dh_flush(struct ip_vs_dh_bucket *tbl)
127 {
128         int i;
129         struct ip_vs_dh_bucket *b;
130
131         b = tbl;
132         for (i=0; i<IP_VS_DH_TAB_SIZE; i++) {
133                 if (b->dest) {
134                         atomic_dec(&b->dest->refcnt);
135                         b->dest = NULL;
136                 }
137                 b++;
138         }
139 }
140
141
142 static int ip_vs_dh_init_svc(struct ip_vs_service *svc)
143 {
144         struct ip_vs_dh_bucket *tbl;
145
146         /* allocate the DH table for this service */
147         tbl = kmalloc(sizeof(struct ip_vs_dh_bucket)*IP_VS_DH_TAB_SIZE,
148                       GFP_ATOMIC);
149         if (tbl == NULL) {
150                 IP_VS_ERR("ip_vs_dh_init_svc(): no memory\n");
151                 return -ENOMEM;
152         }
153         svc->sched_data = tbl;
154         IP_VS_DBG(6, "DH hash table (memory=%Zdbytes) allocated for "
155                   "current service\n",
156                   sizeof(struct ip_vs_dh_bucket)*IP_VS_DH_TAB_SIZE);
157
158         /* assign the hash buckets with the updated service */
159         ip_vs_dh_assign(tbl, svc);
160
161         return 0;
162 }
163
164
165 static int ip_vs_dh_done_svc(struct ip_vs_service *svc)
166 {
167         struct ip_vs_dh_bucket *tbl = svc->sched_data;
168
169         /* got to clean up hash buckets here */
170         ip_vs_dh_flush(tbl);
171
172         /* release the table itself */
173         kfree(svc->sched_data);
174         IP_VS_DBG(6, "DH hash table (memory=%Zdbytes) released\n",
175                   sizeof(struct ip_vs_dh_bucket)*IP_VS_DH_TAB_SIZE);
176
177         return 0;
178 }
179
180
181 static int ip_vs_dh_update_svc(struct ip_vs_service *svc)
182 {
183         struct ip_vs_dh_bucket *tbl = svc->sched_data;
184
185         /* got to clean up hash buckets here */
186         ip_vs_dh_flush(tbl);
187
188         /* assign the hash buckets with the updated service */
189         ip_vs_dh_assign(tbl, svc);
190
191         return 0;
192 }
193
194
195 /*
196  *      If the dest flags is set with IP_VS_DEST_F_OVERLOAD,
197  *      consider that the server is overloaded here.
198  */
199 static inline int is_overloaded(struct ip_vs_dest *dest)
200 {
201         return dest->flags & IP_VS_DEST_F_OVERLOAD;
202 }
203
204
205 /*
206  *      Destination hashing scheduling
207  */
208 static struct ip_vs_dest *
209 ip_vs_dh_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
210 {
211         struct ip_vs_dest *dest;
212         struct ip_vs_dh_bucket *tbl;
213         struct ip_vs_iphdr iph;
214
215         ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
216
217         IP_VS_DBG(6, "ip_vs_dh_schedule(): Scheduling...\n");
218
219         tbl = (struct ip_vs_dh_bucket *)svc->sched_data;
220         dest = ip_vs_dh_get(svc->af, tbl, &iph.daddr);
221         if (!dest
222             || !(dest->flags & IP_VS_DEST_F_AVAILABLE)
223             || atomic_read(&dest->weight) <= 0
224             || is_overloaded(dest)) {
225                 return NULL;
226         }
227
228         IP_VS_DBG_BUF(6, "DH: destination IP address %s --> server %s:%d\n",
229                       IP_VS_DBG_ADDR(svc->af, &iph.daddr),
230                       IP_VS_DBG_ADDR(svc->af, &dest->addr),
231                       ntohs(dest->port));
232
233         return dest;
234 }
235
236
237 /*
238  *      IPVS DH Scheduler structure
239  */
240 static struct ip_vs_scheduler ip_vs_dh_scheduler =
241 {
242         .name =                 "dh",
243         .refcnt =               ATOMIC_INIT(0),
244         .module =               THIS_MODULE,
245         .n_list =               LIST_HEAD_INIT(ip_vs_dh_scheduler.n_list),
246         .init_service =         ip_vs_dh_init_svc,
247         .done_service =         ip_vs_dh_done_svc,
248         .update_service =       ip_vs_dh_update_svc,
249         .schedule =             ip_vs_dh_schedule,
250 };
251
252
253 static int __init ip_vs_dh_init(void)
254 {
255         return register_ip_vs_scheduler(&ip_vs_dh_scheduler);
256 }
257
258
259 static void __exit ip_vs_dh_cleanup(void)
260 {
261         unregister_ip_vs_scheduler(&ip_vs_dh_scheduler);
262 }
263
264
265 module_init(ip_vs_dh_init);
266 module_exit(ip_vs_dh_cleanup);
267 MODULE_LICENSE("GPL");