1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40#include <linux/ip.h>
41#include <linux/module.h>
42#include <linux/kernel.h>
43#include <linux/skbuff.h>
44
45#include <net/ip_vs.h>
46
47
48
49
50
51struct ip_vs_dh_bucket {
52 struct ip_vs_dest *dest;
53};
54
55
56
57
58#ifndef CONFIG_IP_VS_DH_TAB_BITS
59#define CONFIG_IP_VS_DH_TAB_BITS 8
60#endif
61#define IP_VS_DH_TAB_BITS CONFIG_IP_VS_DH_TAB_BITS
62#define IP_VS_DH_TAB_SIZE (1 << IP_VS_DH_TAB_BITS)
63#define IP_VS_DH_TAB_MASK (IP_VS_DH_TAB_SIZE - 1)
64
65
66
67
68
69static inline unsigned ip_vs_dh_hashkey(__be32 addr)
70{
71 return (ntohl(addr)*2654435761UL) & IP_VS_DH_TAB_MASK;
72}
73
74
75
76
77
78static inline struct ip_vs_dest *
79ip_vs_dh_get(struct ip_vs_dh_bucket *tbl, __be32 addr)
80{
81 return (tbl[ip_vs_dh_hashkey(addr)]).dest;
82}
83
84
85
86
87
88static int
89ip_vs_dh_assign(struct ip_vs_dh_bucket *tbl, struct ip_vs_service *svc)
90{
91 int i;
92 struct ip_vs_dh_bucket *b;
93 struct list_head *p;
94 struct ip_vs_dest *dest;
95
96 b = tbl;
97 p = &svc->destinations;
98 for (i=0; i<IP_VS_DH_TAB_SIZE; i++) {
99 if (list_empty(p)) {
100 b->dest = NULL;
101 } else {
102 if (p == &svc->destinations)
103 p = p->next;
104
105 dest = list_entry(p, struct ip_vs_dest, n_list);
106 atomic_inc(&dest->refcnt);
107 b->dest = dest;
108
109 p = p->next;
110 }
111 b++;
112 }
113 return 0;
114}
115
116
117
118
119
120static void ip_vs_dh_flush(struct ip_vs_dh_bucket *tbl)
121{
122 int i;
123 struct ip_vs_dh_bucket *b;
124
125 b = tbl;
126 for (i=0; i<IP_VS_DH_TAB_SIZE; i++) {
127 if (b->dest) {
128 atomic_dec(&b->dest->refcnt);
129 b->dest = NULL;
130 }
131 b++;
132 }
133}
134
135
136static int ip_vs_dh_init_svc(struct ip_vs_service *svc)
137{
138 struct ip_vs_dh_bucket *tbl;
139
140
141 tbl = kmalloc(sizeof(struct ip_vs_dh_bucket)*IP_VS_DH_TAB_SIZE,
142 GFP_ATOMIC);
143 if (tbl == NULL) {
144 IP_VS_ERR("ip_vs_dh_init_svc(): no memory\n");
145 return -ENOMEM;
146 }
147 svc->sched_data = tbl;
148 IP_VS_DBG(6, "DH hash table (memory=%Zdbytes) allocated for "
149 "current service\n",
150 sizeof(struct ip_vs_dh_bucket)*IP_VS_DH_TAB_SIZE);
151
152
153 ip_vs_dh_assign(tbl, svc);
154
155 return 0;
156}
157
158
159static int ip_vs_dh_done_svc(struct ip_vs_service *svc)
160{
161 struct ip_vs_dh_bucket *tbl = svc->sched_data;
162
163
164 ip_vs_dh_flush(tbl);
165
166
167 kfree(svc->sched_data);
168 IP_VS_DBG(6, "DH hash table (memory=%Zdbytes) released\n",
169 sizeof(struct ip_vs_dh_bucket)*IP_VS_DH_TAB_SIZE);
170
171 return 0;
172}
173
174
175static int ip_vs_dh_update_svc(struct ip_vs_service *svc)
176{
177 struct ip_vs_dh_bucket *tbl = svc->sched_data;
178
179
180 ip_vs_dh_flush(tbl);
181
182
183 ip_vs_dh_assign(tbl, svc);
184
185 return 0;
186}
187
188
189
190
191
192
193static inline int is_overloaded(struct ip_vs_dest *dest)
194{
195 return dest->flags & IP_VS_DEST_F_OVERLOAD;
196}
197
198
199
200
201
202static struct ip_vs_dest *
203ip_vs_dh_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
204{
205 struct ip_vs_dest *dest;
206 struct ip_vs_dh_bucket *tbl;
207 struct iphdr *iph = ip_hdr(skb);
208
209 IP_VS_DBG(6, "ip_vs_dh_schedule(): Scheduling...\n");
210
211 tbl = (struct ip_vs_dh_bucket *)svc->sched_data;
212 dest = ip_vs_dh_get(tbl, iph->daddr);
213 if (!dest
214 || !(dest->flags & IP_VS_DEST_F_AVAILABLE)
215 || atomic_read(&dest->weight) <= 0
216 || is_overloaded(dest)) {
217 return NULL;
218 }
219
220 IP_VS_DBG(6, "DH: destination IP address %u.%u.%u.%u "
221 "--> server %u.%u.%u.%u:%d\n",
222 NIPQUAD(iph->daddr),
223 NIPQUAD(dest->addr),
224 ntohs(dest->port));
225
226 return dest;
227}
228
229
230
231
232
233static struct ip_vs_scheduler ip_vs_dh_scheduler =
234{
235 .name = "dh",
236 .refcnt = ATOMIC_INIT(0),
237 .module = THIS_MODULE,
238 .init_service = ip_vs_dh_init_svc,
239 .done_service = ip_vs_dh_done_svc,
240 .update_service = ip_vs_dh_update_svc,
241 .schedule = ip_vs_dh_schedule,
242};
243
244
245static int __init ip_vs_dh_init(void)
246{
247 INIT_LIST_HEAD(&ip_vs_dh_scheduler.n_list);
248 return register_ip_vs_scheduler(&ip_vs_dh_scheduler);
249}
250
251
252static void __exit ip_vs_dh_cleanup(void)
253{
254 unregister_ip_vs_scheduler(&ip_vs_dh_scheduler);
255}
256
257
258module_init(ip_vs_dh_init);
259module_exit(ip_vs_dh_cleanup);
260MODULE_LICENSE("GPL");
261