USB: Add USB-ID for Multiplex RC serial adapter to cp210x.c
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / netfilter / ipvs / ip_vs_dh.c
blobfe3e18834b911c1b388d253d76eeaa68dfa464d7
1 /*
2 * IPVS: Destination Hashing scheduling module
4 * Authors: Wensong Zhang <wensong@gnuchina.org>
6 * Inspired by the consistent hashing scheduler patch from
7 * Thomas Proell <proellt@gmx.de>
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.
14 * Changes:
19 * The dh algorithm is to select server by the hash key of destination IP
20 * address. The pseudo code is as follows:
22 * n <- servernode[dest_ip];
23 * if (n is dead) OR
24 * (n is overloaded) OR (n.weight <= 0) then
25 * return NULL;
27 * return n;
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.
38 #define KMSG_COMPONENT "IPVS"
39 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
41 #include <linux/ip.h>
42 #include <linux/module.h>
43 #include <linux/kernel.h>
44 #include <linux/skbuff.h>
46 #include <net/ip_vs.h>
50 * IPVS DH bucket
52 struct ip_vs_dh_bucket {
53 struct ip_vs_dest *dest; /* real server (cache) */
57 * for IPVS DH entry hash table
59 #ifndef CONFIG_IP_VS_DH_TAB_BITS
60 #define CONFIG_IP_VS_DH_TAB_BITS 8
61 #endif
62 #define IP_VS_DH_TAB_BITS CONFIG_IP_VS_DH_TAB_BITS
63 #define IP_VS_DH_TAB_SIZE (1 << IP_VS_DH_TAB_BITS)
64 #define IP_VS_DH_TAB_MASK (IP_VS_DH_TAB_SIZE - 1)
68 * Returns hash value for IPVS DH entry
70 static inline unsigned ip_vs_dh_hashkey(int af, const union nf_inet_addr *addr)
72 __be32 addr_fold = addr->ip;
74 #ifdef CONFIG_IP_VS_IPV6
75 if (af == AF_INET6)
76 addr_fold = addr->ip6[0]^addr->ip6[1]^
77 addr->ip6[2]^addr->ip6[3];
78 #endif
79 return (ntohl(addr_fold)*2654435761UL) & IP_VS_DH_TAB_MASK;
84 * Get ip_vs_dest associated with supplied parameters.
86 static inline struct ip_vs_dest *
87 ip_vs_dh_get(int af, struct ip_vs_dh_bucket *tbl,
88 const union nf_inet_addr *addr)
90 return (tbl[ip_vs_dh_hashkey(af, addr)]).dest;
95 * Assign all the hash buckets of the specified table with the service.
97 static int
98 ip_vs_dh_assign(struct ip_vs_dh_bucket *tbl, struct ip_vs_service *svc)
100 int i;
101 struct ip_vs_dh_bucket *b;
102 struct list_head *p;
103 struct ip_vs_dest *dest;
105 b = tbl;
106 p = &svc->destinations;
107 for (i=0; i<IP_VS_DH_TAB_SIZE; i++) {
108 if (list_empty(p)) {
109 b->dest = NULL;
110 } else {
111 if (p == &svc->destinations)
112 p = p->next;
114 dest = list_entry(p, struct ip_vs_dest, n_list);
115 atomic_inc(&dest->refcnt);
116 b->dest = dest;
118 p = p->next;
120 b++;
122 return 0;
127 * Flush all the hash buckets of the specified table.
129 static void ip_vs_dh_flush(struct ip_vs_dh_bucket *tbl)
131 int i;
132 struct ip_vs_dh_bucket *b;
134 b = tbl;
135 for (i=0; i<IP_VS_DH_TAB_SIZE; i++) {
136 if (b->dest) {
137 atomic_dec(&b->dest->refcnt);
138 b->dest = NULL;
140 b++;
145 static int ip_vs_dh_init_svc(struct ip_vs_service *svc)
147 struct ip_vs_dh_bucket *tbl;
149 /* allocate the DH table for this service */
150 tbl = kmalloc(sizeof(struct ip_vs_dh_bucket)*IP_VS_DH_TAB_SIZE,
151 GFP_ATOMIC);
152 if (tbl == NULL) {
153 pr_err("%s(): no memory\n", __func__);
154 return -ENOMEM;
156 svc->sched_data = tbl;
157 IP_VS_DBG(6, "DH hash table (memory=%Zdbytes) allocated for "
158 "current service\n",
159 sizeof(struct ip_vs_dh_bucket)*IP_VS_DH_TAB_SIZE);
161 /* assign the hash buckets with the updated service */
162 ip_vs_dh_assign(tbl, svc);
164 return 0;
168 static int ip_vs_dh_done_svc(struct ip_vs_service *svc)
170 struct ip_vs_dh_bucket *tbl = svc->sched_data;
172 /* got to clean up hash buckets here */
173 ip_vs_dh_flush(tbl);
175 /* release the table itself */
176 kfree(svc->sched_data);
177 IP_VS_DBG(6, "DH hash table (memory=%Zdbytes) released\n",
178 sizeof(struct ip_vs_dh_bucket)*IP_VS_DH_TAB_SIZE);
180 return 0;
184 static int ip_vs_dh_update_svc(struct ip_vs_service *svc)
186 struct ip_vs_dh_bucket *tbl = svc->sched_data;
188 /* got to clean up hash buckets here */
189 ip_vs_dh_flush(tbl);
191 /* assign the hash buckets with the updated service */
192 ip_vs_dh_assign(tbl, svc);
194 return 0;
199 * If the dest flags is set with IP_VS_DEST_F_OVERLOAD,
200 * consider that the server is overloaded here.
202 static inline int is_overloaded(struct ip_vs_dest *dest)
204 return dest->flags & IP_VS_DEST_F_OVERLOAD;
209 * Destination hashing scheduling
211 static struct ip_vs_dest *
212 ip_vs_dh_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
214 struct ip_vs_dest *dest;
215 struct ip_vs_dh_bucket *tbl;
216 struct ip_vs_iphdr iph;
218 ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
220 IP_VS_DBG(6, "%s(): Scheduling...\n", __func__);
222 tbl = (struct ip_vs_dh_bucket *)svc->sched_data;
223 dest = ip_vs_dh_get(svc->af, tbl, &iph.daddr);
224 if (!dest
225 || !(dest->flags & IP_VS_DEST_F_AVAILABLE)
226 || atomic_read(&dest->weight) <= 0
227 || is_overloaded(dest)) {
228 return NULL;
231 IP_VS_DBG_BUF(6, "DH: destination IP address %s --> server %s:%d\n",
232 IP_VS_DBG_ADDR(svc->af, &iph.daddr),
233 IP_VS_DBG_ADDR(svc->af, &dest->addr),
234 ntohs(dest->port));
236 return dest;
241 * IPVS DH Scheduler structure
243 static struct ip_vs_scheduler ip_vs_dh_scheduler =
245 .name = "dh",
246 .refcnt = ATOMIC_INIT(0),
247 .module = THIS_MODULE,
248 .n_list = LIST_HEAD_INIT(ip_vs_dh_scheduler.n_list),
249 .init_service = ip_vs_dh_init_svc,
250 .done_service = ip_vs_dh_done_svc,
251 .update_service = ip_vs_dh_update_svc,
252 .schedule = ip_vs_dh_schedule,
256 static int __init ip_vs_dh_init(void)
258 return register_ip_vs_scheduler(&ip_vs_dh_scheduler);
262 static void __exit ip_vs_dh_cleanup(void)
264 unregister_ip_vs_scheduler(&ip_vs_dh_scheduler);
268 module_init(ip_vs_dh_init);
269 module_exit(ip_vs_dh_cleanup);
270 MODULE_LICENSE("GPL");