]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * IPVS: Locality-Based Least-Connection with Replication scheduler | |
3 | * | |
1da177e4 LT |
4 | * Authors: Wensong Zhang <wensong@gnuchina.org> |
5 | * | |
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. | |
10 | * | |
11 | * Changes: | |
12 | * Julian Anastasov : Added the missing (dest->weight>0) | |
13 | * condition in the ip_vs_dest_set_max. | |
14 | * | |
15 | */ | |
16 | ||
17 | /* | |
18 | * The lblc/r algorithm is as follows (pseudo code): | |
19 | * | |
20 | * if serverSet[dest_ip] is null then | |
21 | * n, serverSet[dest_ip] <- {weighted least-conn node}; | |
22 | * else | |
23 | * n <- {least-conn (alive) node in serverSet[dest_ip]}; | |
24 | * if (n is null) OR | |
25 | * (n.conns>n.weight AND | |
26 | * there is a node m with m.conns<m.weight/2) then | |
27 | * n <- {weighted least-conn node}; | |
28 | * add n to serverSet[dest_ip]; | |
29 | * if |serverSet[dest_ip]| > 1 AND | |
30 | * now - serverSet[dest_ip].lastMod > T then | |
31 | * m <- {most conn node in serverSet[dest_ip]}; | |
32 | * remove m from serverSet[dest_ip]; | |
33 | * if serverSet[dest_ip] changed then | |
34 | * serverSet[dest_ip].lastMod <- now; | |
35 | * | |
36 | * return n; | |
37 | * | |
38 | */ | |
39 | ||
9aada7ac HE |
40 | #define KMSG_COMPONENT "IPVS" |
41 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt | |
42 | ||
14c85021 | 43 | #include <linux/ip.h> |
1da177e4 LT |
44 | #include <linux/module.h> |
45 | #include <linux/kernel.h> | |
14c85021 | 46 | #include <linux/skbuff.h> |
d7fe0f24 | 47 | #include <linux/jiffies.h> |
51f0bc78 | 48 | #include <linux/list.h> |
5a0e3ad6 | 49 | #include <linux/slab.h> |
1da177e4 LT |
50 | |
51 | /* for sysctl */ | |
52 | #include <linux/fs.h> | |
53 | #include <linux/sysctl.h> | |
457c4cbc | 54 | #include <net/net_namespace.h> |
1da177e4 LT |
55 | |
56 | #include <net/ip_vs.h> | |
57 | ||
58 | ||
59 | /* | |
60 | * It is for garbage collection of stale IPVS lblcr entries, | |
61 | * when the table is full. | |
62 | */ | |
63 | #define CHECK_EXPIRE_INTERVAL (60*HZ) | |
64 | #define ENTRY_TIMEOUT (6*60*HZ) | |
65 | ||
b27d777e SH |
66 | #define DEFAULT_EXPIRATION (24*60*60*HZ) |
67 | ||
1da177e4 LT |
68 | /* |
69 | * It is for full expiration check. | |
70 | * When there is no partial expiration check (garbage collection) | |
71 | * in a half hour, do a full expiration check to collect stale | |
72 | * entries that haven't been touched for a day. | |
73 | */ | |
74 | #define COUNT_FOR_FULL_EXPIRATION 30 | |
1da177e4 LT |
75 | |
76 | /* | |
77 | * for IPVS lblcr entry hash table | |
78 | */ | |
79 | #ifndef CONFIG_IP_VS_LBLCR_TAB_BITS | |
80 | #define CONFIG_IP_VS_LBLCR_TAB_BITS 10 | |
81 | #endif | |
82 | #define IP_VS_LBLCR_TAB_BITS CONFIG_IP_VS_LBLCR_TAB_BITS | |
83 | #define IP_VS_LBLCR_TAB_SIZE (1 << IP_VS_LBLCR_TAB_BITS) | |
84 | #define IP_VS_LBLCR_TAB_MASK (IP_VS_LBLCR_TAB_SIZE - 1) | |
85 | ||
86 | ||
87 | /* | |
88 | * IPVS destination set structure and operations | |
89 | */ | |
51f0bc78 SH |
90 | struct ip_vs_dest_set_elem { |
91 | struct list_head list; /* list link */ | |
1da177e4 LT |
92 | struct ip_vs_dest *dest; /* destination server */ |
93 | }; | |
94 | ||
95 | struct ip_vs_dest_set { | |
96 | atomic_t size; /* set size */ | |
97 | unsigned long lastmod; /* last modified time */ | |
51f0bc78 | 98 | struct list_head list; /* destination list */ |
1da177e4 LT |
99 | rwlock_t lock; /* lock for this list */ |
100 | }; | |
101 | ||
102 | ||
51f0bc78 | 103 | static struct ip_vs_dest_set_elem * |
1da177e4 LT |
104 | ip_vs_dest_set_insert(struct ip_vs_dest_set *set, struct ip_vs_dest *dest) |
105 | { | |
51f0bc78 | 106 | struct ip_vs_dest_set_elem *e; |
1da177e4 | 107 | |
51f0bc78 | 108 | list_for_each_entry(e, &set->list, list) { |
1da177e4 LT |
109 | if (e->dest == dest) |
110 | /* already existed */ | |
111 | return NULL; | |
112 | } | |
113 | ||
f728bafb | 114 | e = kmalloc(sizeof(*e), GFP_ATOMIC); |
0a9ee813 | 115 | if (e == NULL) |
1da177e4 | 116 | return NULL; |
1da177e4 LT |
117 | |
118 | atomic_inc(&dest->refcnt); | |
119 | e->dest = dest; | |
120 | ||
51f0bc78 | 121 | list_add(&e->list, &set->list); |
1da177e4 | 122 | atomic_inc(&set->size); |
1da177e4 LT |
123 | |
124 | set->lastmod = jiffies; | |
125 | return e; | |
126 | } | |
127 | ||
128 | static void | |
129 | ip_vs_dest_set_erase(struct ip_vs_dest_set *set, struct ip_vs_dest *dest) | |
130 | { | |
51f0bc78 | 131 | struct ip_vs_dest_set_elem *e; |
1da177e4 | 132 | |
51f0bc78 | 133 | list_for_each_entry(e, &set->list, list) { |
1da177e4 LT |
134 | if (e->dest == dest) { |
135 | /* HIT */ | |
1da177e4 LT |
136 | atomic_dec(&set->size); |
137 | set->lastmod = jiffies; | |
138 | atomic_dec(&e->dest->refcnt); | |
51f0bc78 | 139 | list_del(&e->list); |
1da177e4 LT |
140 | kfree(e); |
141 | break; | |
142 | } | |
1da177e4 | 143 | } |
1da177e4 LT |
144 | } |
145 | ||
146 | static void ip_vs_dest_set_eraseall(struct ip_vs_dest_set *set) | |
147 | { | |
51f0bc78 | 148 | struct ip_vs_dest_set_elem *e, *ep; |
1da177e4 LT |
149 | |
150 | write_lock(&set->lock); | |
51f0bc78 | 151 | list_for_each_entry_safe(e, ep, &set->list, list) { |
1da177e4 | 152 | /* |
25985edc | 153 | * We don't kfree dest because it is referred either |
1da177e4 LT |
154 | * by its service or by the trash dest list. |
155 | */ | |
156 | atomic_dec(&e->dest->refcnt); | |
51f0bc78 | 157 | list_del(&e->list); |
1da177e4 LT |
158 | kfree(e); |
159 | } | |
160 | write_unlock(&set->lock); | |
161 | } | |
162 | ||
163 | /* get weighted least-connection node in the destination set */ | |
164 | static inline struct ip_vs_dest *ip_vs_dest_set_min(struct ip_vs_dest_set *set) | |
165 | { | |
51f0bc78 | 166 | register struct ip_vs_dest_set_elem *e; |
1da177e4 LT |
167 | struct ip_vs_dest *dest, *least; |
168 | int loh, doh; | |
169 | ||
170 | if (set == NULL) | |
171 | return NULL; | |
172 | ||
1da177e4 | 173 | /* select the first destination server, whose weight > 0 */ |
51f0bc78 | 174 | list_for_each_entry(e, &set->list, list) { |
1da177e4 LT |
175 | least = e->dest; |
176 | if (least->flags & IP_VS_DEST_F_OVERLOAD) | |
177 | continue; | |
178 | ||
179 | if ((atomic_read(&least->weight) > 0) | |
180 | && (least->flags & IP_VS_DEST_F_AVAILABLE)) { | |
b552f7e3 | 181 | loh = ip_vs_dest_conn_overhead(least); |
1da177e4 LT |
182 | goto nextstage; |
183 | } | |
184 | } | |
1da177e4 LT |
185 | return NULL; |
186 | ||
187 | /* find the destination with the weighted least load */ | |
188 | nextstage: | |
51f0bc78 | 189 | list_for_each_entry(e, &set->list, list) { |
1da177e4 LT |
190 | dest = e->dest; |
191 | if (dest->flags & IP_VS_DEST_F_OVERLOAD) | |
192 | continue; | |
193 | ||
b552f7e3 | 194 | doh = ip_vs_dest_conn_overhead(dest); |
1da177e4 LT |
195 | if ((loh * atomic_read(&dest->weight) > |
196 | doh * atomic_read(&least->weight)) | |
197 | && (dest->flags & IP_VS_DEST_F_AVAILABLE)) { | |
198 | least = dest; | |
199 | loh = doh; | |
200 | } | |
201 | } | |
1da177e4 | 202 | |
1e3e238e | 203 | IP_VS_DBG_BUF(6, "%s(): server %s:%d " |
44548375 | 204 | "activeconns %d refcnt %d weight %d overhead %d\n", |
1e3e238e | 205 | __func__, |
44548375 JV |
206 | IP_VS_DBG_ADDR(least->af, &least->addr), |
207 | ntohs(least->port), | |
208 | atomic_read(&least->activeconns), | |
209 | atomic_read(&least->refcnt), | |
210 | atomic_read(&least->weight), loh); | |
1da177e4 LT |
211 | return least; |
212 | } | |
213 | ||
214 | ||
215 | /* get weighted most-connection node in the destination set */ | |
216 | static inline struct ip_vs_dest *ip_vs_dest_set_max(struct ip_vs_dest_set *set) | |
217 | { | |
51f0bc78 | 218 | register struct ip_vs_dest_set_elem *e; |
1da177e4 LT |
219 | struct ip_vs_dest *dest, *most; |
220 | int moh, doh; | |
221 | ||
222 | if (set == NULL) | |
223 | return NULL; | |
224 | ||
1da177e4 | 225 | /* select the first destination server, whose weight > 0 */ |
51f0bc78 | 226 | list_for_each_entry(e, &set->list, list) { |
1da177e4 LT |
227 | most = e->dest; |
228 | if (atomic_read(&most->weight) > 0) { | |
b552f7e3 | 229 | moh = ip_vs_dest_conn_overhead(most); |
1da177e4 LT |
230 | goto nextstage; |
231 | } | |
232 | } | |
1da177e4 LT |
233 | return NULL; |
234 | ||
235 | /* find the destination with the weighted most load */ | |
236 | nextstage: | |
51f0bc78 | 237 | list_for_each_entry(e, &set->list, list) { |
1da177e4 | 238 | dest = e->dest; |
b552f7e3 | 239 | doh = ip_vs_dest_conn_overhead(dest); |
1da177e4 LT |
240 | /* moh/mw < doh/dw ==> moh*dw < doh*mw, where mw,dw>0 */ |
241 | if ((moh * atomic_read(&dest->weight) < | |
242 | doh * atomic_read(&most->weight)) | |
243 | && (atomic_read(&dest->weight) > 0)) { | |
244 | most = dest; | |
245 | moh = doh; | |
246 | } | |
247 | } | |
1da177e4 | 248 | |
1e3e238e | 249 | IP_VS_DBG_BUF(6, "%s(): server %s:%d " |
44548375 | 250 | "activeconns %d refcnt %d weight %d overhead %d\n", |
1e3e238e | 251 | __func__, |
44548375 JV |
252 | IP_VS_DBG_ADDR(most->af, &most->addr), ntohs(most->port), |
253 | atomic_read(&most->activeconns), | |
254 | atomic_read(&most->refcnt), | |
255 | atomic_read(&most->weight), moh); | |
1da177e4 LT |
256 | return most; |
257 | } | |
258 | ||
259 | ||
260 | /* | |
261 | * IPVS lblcr entry represents an association between destination | |
262 | * IP address and its destination server set | |
263 | */ | |
264 | struct ip_vs_lblcr_entry { | |
265 | struct list_head list; | |
44548375 JV |
266 | int af; /* address family */ |
267 | union nf_inet_addr addr; /* destination IP address */ | |
1da177e4 LT |
268 | struct ip_vs_dest_set set; /* destination server set */ |
269 | unsigned long lastuse; /* last used time */ | |
270 | }; | |
271 | ||
272 | ||
273 | /* | |
274 | * IPVS lblcr hash table | |
275 | */ | |
276 | struct ip_vs_lblcr_table { | |
1da177e4 LT |
277 | struct list_head bucket[IP_VS_LBLCR_TAB_SIZE]; /* hash bucket */ |
278 | atomic_t entries; /* number of entries */ | |
279 | int max_size; /* maximum size of entries */ | |
280 | struct timer_list periodic_timer; /* collect stale entries */ | |
281 | int rover; /* rover for expire check */ | |
282 | int counter; /* counter for no expire */ | |
283 | }; | |
284 | ||
285 | ||
fb1de432 | 286 | #ifdef CONFIG_SYSCTL |
1da177e4 LT |
287 | /* |
288 | * IPVS LBLCR sysctl table | |
289 | */ | |
290 | ||
291 | static ctl_table vs_vars_table[] = { | |
292 | { | |
1da177e4 | 293 | .procname = "lblcr_expiration", |
d0a1eef9 | 294 | .data = NULL, |
1da177e4 | 295 | .maxlen = sizeof(int), |
e905a9ed | 296 | .mode = 0644, |
6d9f239a | 297 | .proc_handler = proc_dointvec_jiffies, |
1da177e4 | 298 | }, |
f8572d8f | 299 | { } |
1da177e4 | 300 | }; |
fb1de432 | 301 | #endif |
1da177e4 | 302 | |
1da177e4 LT |
303 | static inline void ip_vs_lblcr_free(struct ip_vs_lblcr_entry *en) |
304 | { | |
305 | list_del(&en->list); | |
306 | ip_vs_dest_set_eraseall(&en->set); | |
307 | kfree(en); | |
308 | } | |
309 | ||
310 | ||
311 | /* | |
312 | * Returns hash value for IPVS LBLCR entry | |
313 | */ | |
95c96174 | 314 | static inline unsigned int |
44548375 | 315 | ip_vs_lblcr_hashkey(int af, const union nf_inet_addr *addr) |
1da177e4 | 316 | { |
44548375 JV |
317 | __be32 addr_fold = addr->ip; |
318 | ||
319 | #ifdef CONFIG_IP_VS_IPV6 | |
320 | if (af == AF_INET6) | |
321 | addr_fold = addr->ip6[0]^addr->ip6[1]^ | |
322 | addr->ip6[2]^addr->ip6[3]; | |
323 | #endif | |
324 | return (ntohl(addr_fold)*2654435761UL) & IP_VS_LBLCR_TAB_MASK; | |
1da177e4 LT |
325 | } |
326 | ||
327 | ||
328 | /* | |
329 | * Hash an entry in the ip_vs_lblcr_table. | |
330 | * returns bool success. | |
331 | */ | |
f728bafb | 332 | static void |
1da177e4 LT |
333 | ip_vs_lblcr_hash(struct ip_vs_lblcr_table *tbl, struct ip_vs_lblcr_entry *en) |
334 | { | |
95c96174 | 335 | unsigned int hash = ip_vs_lblcr_hashkey(en->af, &en->addr); |
1da177e4 | 336 | |
1da177e4 LT |
337 | list_add(&en->list, &tbl->bucket[hash]); |
338 | atomic_inc(&tbl->entries); | |
1da177e4 LT |
339 | } |
340 | ||
341 | ||
1da177e4 | 342 | /* |
f728bafb SW |
343 | * Get ip_vs_lblcr_entry associated with supplied parameters. Called under |
344 | * read lock. | |
1da177e4 LT |
345 | */ |
346 | static inline struct ip_vs_lblcr_entry * | |
44548375 JV |
347 | ip_vs_lblcr_get(int af, struct ip_vs_lblcr_table *tbl, |
348 | const union nf_inet_addr *addr) | |
1da177e4 | 349 | { |
95c96174 | 350 | unsigned int hash = ip_vs_lblcr_hashkey(af, addr); |
1da177e4 LT |
351 | struct ip_vs_lblcr_entry *en; |
352 | ||
f728bafb | 353 | list_for_each_entry(en, &tbl->bucket[hash], list) |
44548375 | 354 | if (ip_vs_addr_equal(af, &en->addr, addr)) |
f728bafb SW |
355 | return en; |
356 | ||
357 | return NULL; | |
358 | } | |
1da177e4 | 359 | |
1da177e4 | 360 | |
f728bafb SW |
361 | /* |
362 | * Create or update an ip_vs_lblcr_entry, which is a mapping of a destination | |
363 | * IP address to a server. Called under write lock. | |
364 | */ | |
365 | static inline struct ip_vs_lblcr_entry * | |
44548375 | 366 | ip_vs_lblcr_new(struct ip_vs_lblcr_table *tbl, const union nf_inet_addr *daddr, |
f728bafb SW |
367 | struct ip_vs_dest *dest) |
368 | { | |
369 | struct ip_vs_lblcr_entry *en; | |
370 | ||
44548375 | 371 | en = ip_vs_lblcr_get(dest->af, tbl, daddr); |
f728bafb SW |
372 | if (!en) { |
373 | en = kmalloc(sizeof(*en), GFP_ATOMIC); | |
0a9ee813 | 374 | if (!en) |
f728bafb | 375 | return NULL; |
f728bafb | 376 | |
44548375 JV |
377 | en->af = dest->af; |
378 | ip_vs_addr_copy(dest->af, &en->addr, daddr); | |
f728bafb SW |
379 | en->lastuse = jiffies; |
380 | ||
421f91d2 | 381 | /* initialize its dest set */ |
f728bafb | 382 | atomic_set(&(en->set.size), 0); |
51f0bc78 | 383 | INIT_LIST_HEAD(&en->set.list); |
f728bafb SW |
384 | rwlock_init(&en->set.lock); |
385 | ||
386 | ip_vs_lblcr_hash(tbl, en); | |
1da177e4 LT |
387 | } |
388 | ||
f728bafb SW |
389 | write_lock(&en->set.lock); |
390 | ip_vs_dest_set_insert(&en->set, dest); | |
391 | write_unlock(&en->set.lock); | |
1da177e4 | 392 | |
f728bafb | 393 | return en; |
1da177e4 LT |
394 | } |
395 | ||
396 | ||
397 | /* | |
398 | * Flush all the entries of the specified table. | |
399 | */ | |
400 | static void ip_vs_lblcr_flush(struct ip_vs_lblcr_table *tbl) | |
401 | { | |
402 | int i; | |
403 | struct ip_vs_lblcr_entry *en, *nxt; | |
404 | ||
f728bafb | 405 | /* No locking required, only called during cleanup. */ |
1da177e4 | 406 | for (i=0; i<IP_VS_LBLCR_TAB_SIZE; i++) { |
1da177e4 LT |
407 | list_for_each_entry_safe(en, nxt, &tbl->bucket[i], list) { |
408 | ip_vs_lblcr_free(en); | |
1da177e4 | 409 | } |
1da177e4 LT |
410 | } |
411 | } | |
412 | ||
b27d777e SH |
413 | static int sysctl_lblcr_expiration(struct ip_vs_service *svc) |
414 | { | |
415 | #ifdef CONFIG_SYSCTL | |
416 | struct netns_ipvs *ipvs = net_ipvs(svc->net); | |
417 | return ipvs->sysctl_lblcr_expiration; | |
418 | #else | |
419 | return DEFAULT_EXPIRATION; | |
420 | #endif | |
421 | } | |
1da177e4 | 422 | |
f728bafb | 423 | static inline void ip_vs_lblcr_full_check(struct ip_vs_service *svc) |
1da177e4 | 424 | { |
f728bafb | 425 | struct ip_vs_lblcr_table *tbl = svc->sched_data; |
1da177e4 LT |
426 | unsigned long now = jiffies; |
427 | int i, j; | |
428 | struct ip_vs_lblcr_entry *en, *nxt; | |
429 | ||
430 | for (i=0, j=tbl->rover; i<IP_VS_LBLCR_TAB_SIZE; i++) { | |
431 | j = (j + 1) & IP_VS_LBLCR_TAB_MASK; | |
432 | ||
f728bafb | 433 | write_lock(&svc->sched_lock); |
1da177e4 | 434 | list_for_each_entry_safe(en, nxt, &tbl->bucket[j], list) { |
b27d777e SH |
435 | if (time_after(en->lastuse + |
436 | sysctl_lblcr_expiration(svc), now)) | |
1da177e4 LT |
437 | continue; |
438 | ||
439 | ip_vs_lblcr_free(en); | |
440 | atomic_dec(&tbl->entries); | |
441 | } | |
f728bafb | 442 | write_unlock(&svc->sched_lock); |
1da177e4 LT |
443 | } |
444 | tbl->rover = j; | |
445 | } | |
446 | ||
447 | ||
448 | /* | |
449 | * Periodical timer handler for IPVS lblcr table | |
450 | * It is used to collect stale entries when the number of entries | |
451 | * exceeds the maximum size of the table. | |
452 | * | |
453 | * Fixme: we probably need more complicated algorithm to collect | |
454 | * entries that have not been used for a long time even | |
455 | * if the number of entries doesn't exceed the maximum size | |
456 | * of the table. | |
457 | * The full expiration check is for this purpose now. | |
458 | */ | |
459 | static void ip_vs_lblcr_check_expire(unsigned long data) | |
460 | { | |
f728bafb SW |
461 | struct ip_vs_service *svc = (struct ip_vs_service *) data; |
462 | struct ip_vs_lblcr_table *tbl = svc->sched_data; | |
1da177e4 LT |
463 | unsigned long now = jiffies; |
464 | int goal; | |
465 | int i, j; | |
466 | struct ip_vs_lblcr_entry *en, *nxt; | |
467 | ||
1da177e4 LT |
468 | if ((tbl->counter % COUNT_FOR_FULL_EXPIRATION) == 0) { |
469 | /* do full expiration check */ | |
f728bafb | 470 | ip_vs_lblcr_full_check(svc); |
1da177e4 LT |
471 | tbl->counter = 1; |
472 | goto out; | |
473 | } | |
474 | ||
475 | if (atomic_read(&tbl->entries) <= tbl->max_size) { | |
476 | tbl->counter++; | |
477 | goto out; | |
478 | } | |
479 | ||
480 | goal = (atomic_read(&tbl->entries) - tbl->max_size)*4/3; | |
481 | if (goal > tbl->max_size/2) | |
482 | goal = tbl->max_size/2; | |
483 | ||
484 | for (i=0, j=tbl->rover; i<IP_VS_LBLCR_TAB_SIZE; i++) { | |
485 | j = (j + 1) & IP_VS_LBLCR_TAB_MASK; | |
486 | ||
f728bafb | 487 | write_lock(&svc->sched_lock); |
1da177e4 LT |
488 | list_for_each_entry_safe(en, nxt, &tbl->bucket[j], list) { |
489 | if (time_before(now, en->lastuse+ENTRY_TIMEOUT)) | |
490 | continue; | |
491 | ||
492 | ip_vs_lblcr_free(en); | |
493 | atomic_dec(&tbl->entries); | |
494 | goal--; | |
495 | } | |
f728bafb | 496 | write_unlock(&svc->sched_lock); |
1da177e4 LT |
497 | if (goal <= 0) |
498 | break; | |
499 | } | |
500 | tbl->rover = j; | |
501 | ||
502 | out: | |
503 | mod_timer(&tbl->periodic_timer, jiffies+CHECK_EXPIRE_INTERVAL); | |
504 | } | |
505 | ||
1da177e4 LT |
506 | static int ip_vs_lblcr_init_svc(struct ip_vs_service *svc) |
507 | { | |
508 | int i; | |
509 | struct ip_vs_lblcr_table *tbl; | |
510 | ||
511 | /* | |
512 | * Allocate the ip_vs_lblcr_table for this service | |
513 | */ | |
45d4e71a | 514 | tbl = kmalloc(sizeof(*tbl), GFP_KERNEL); |
0a9ee813 | 515 | if (tbl == NULL) |
1da177e4 | 516 | return -ENOMEM; |
0a9ee813 | 517 | |
1da177e4 LT |
518 | svc->sched_data = tbl; |
519 | IP_VS_DBG(6, "LBLCR hash table (memory=%Zdbytes) allocated for " | |
f728bafb | 520 | "current service\n", sizeof(*tbl)); |
1da177e4 LT |
521 | |
522 | /* | |
523 | * Initialize the hash buckets | |
524 | */ | |
525 | for (i=0; i<IP_VS_LBLCR_TAB_SIZE; i++) { | |
526 | INIT_LIST_HEAD(&tbl->bucket[i]); | |
527 | } | |
1da177e4 LT |
528 | tbl->max_size = IP_VS_LBLCR_TAB_SIZE*16; |
529 | tbl->rover = 0; | |
530 | tbl->counter = 1; | |
531 | ||
532 | /* | |
533 | * Hook periodic timer for garbage collection | |
534 | */ | |
b24b8a24 | 535 | setup_timer(&tbl->periodic_timer, ip_vs_lblcr_check_expire, |
f728bafb SW |
536 | (unsigned long)svc); |
537 | mod_timer(&tbl->periodic_timer, jiffies + CHECK_EXPIRE_INTERVAL); | |
1da177e4 | 538 | |
1da177e4 LT |
539 | return 0; |
540 | } | |
541 | ||
542 | ||
543 | static int ip_vs_lblcr_done_svc(struct ip_vs_service *svc) | |
544 | { | |
545 | struct ip_vs_lblcr_table *tbl = svc->sched_data; | |
546 | ||
547 | /* remove periodic timer */ | |
548 | del_timer_sync(&tbl->periodic_timer); | |
549 | ||
550 | /* got to clean up table entries here */ | |
551 | ip_vs_lblcr_flush(tbl); | |
552 | ||
553 | /* release the table itself */ | |
f728bafb | 554 | kfree(tbl); |
1da177e4 | 555 | IP_VS_DBG(6, "LBLCR hash table (memory=%Zdbytes) released\n", |
f728bafb | 556 | sizeof(*tbl)); |
1da177e4 LT |
557 | |
558 | return 0; | |
559 | } | |
560 | ||
561 | ||
1da177e4 | 562 | static inline struct ip_vs_dest * |
44548375 | 563 | __ip_vs_lblcr_schedule(struct ip_vs_service *svc) |
1da177e4 LT |
564 | { |
565 | struct ip_vs_dest *dest, *least; | |
566 | int loh, doh; | |
567 | ||
568 | /* | |
b552f7e3 | 569 | * We use the following formula to estimate the load: |
1da177e4 LT |
570 | * (dest overhead) / dest->weight |
571 | * | |
572 | * Remember -- no floats in kernel mode!!! | |
573 | * The comparison of h1*w2 > h2*w1 is equivalent to that of | |
574 | * h1/w1 > h2/w2 | |
575 | * if every weight is larger than zero. | |
576 | * | |
577 | * The server with weight=0 is quiesced and will not receive any | |
578 | * new connection. | |
579 | */ | |
580 | list_for_each_entry(dest, &svc->destinations, n_list) { | |
581 | if (dest->flags & IP_VS_DEST_F_OVERLOAD) | |
582 | continue; | |
583 | ||
584 | if (atomic_read(&dest->weight) > 0) { | |
585 | least = dest; | |
b552f7e3 | 586 | loh = ip_vs_dest_conn_overhead(least); |
1da177e4 LT |
587 | goto nextstage; |
588 | } | |
589 | } | |
590 | return NULL; | |
591 | ||
592 | /* | |
593 | * Find the destination with the least load. | |
594 | */ | |
595 | nextstage: | |
596 | list_for_each_entry_continue(dest, &svc->destinations, n_list) { | |
597 | if (dest->flags & IP_VS_DEST_F_OVERLOAD) | |
598 | continue; | |
599 | ||
b552f7e3 | 600 | doh = ip_vs_dest_conn_overhead(dest); |
1da177e4 LT |
601 | if (loh * atomic_read(&dest->weight) > |
602 | doh * atomic_read(&least->weight)) { | |
603 | least = dest; | |
604 | loh = doh; | |
605 | } | |
606 | } | |
607 | ||
44548375 JV |
608 | IP_VS_DBG_BUF(6, "LBLCR: server %s:%d " |
609 | "activeconns %d refcnt %d weight %d overhead %d\n", | |
610 | IP_VS_DBG_ADDR(least->af, &least->addr), | |
611 | ntohs(least->port), | |
612 | atomic_read(&least->activeconns), | |
613 | atomic_read(&least->refcnt), | |
614 | atomic_read(&least->weight), loh); | |
1da177e4 LT |
615 | |
616 | return least; | |
617 | } | |
618 | ||
619 | ||
620 | /* | |
621 | * If this destination server is overloaded and there is a less loaded | |
622 | * server, then return true. | |
623 | */ | |
624 | static inline int | |
625 | is_overloaded(struct ip_vs_dest *dest, struct ip_vs_service *svc) | |
626 | { | |
627 | if (atomic_read(&dest->activeconns) > atomic_read(&dest->weight)) { | |
628 | struct ip_vs_dest *d; | |
629 | ||
630 | list_for_each_entry(d, &svc->destinations, n_list) { | |
631 | if (atomic_read(&d->activeconns)*2 | |
632 | < atomic_read(&d->weight)) { | |
633 | return 1; | |
634 | } | |
635 | } | |
636 | } | |
637 | return 0; | |
638 | } | |
639 | ||
640 | ||
641 | /* | |
642 | * Locality-Based (weighted) Least-Connection scheduling | |
643 | */ | |
644 | static struct ip_vs_dest * | |
645 | ip_vs_lblcr_schedule(struct ip_vs_service *svc, const struct sk_buff *skb) | |
646 | { | |
f728bafb | 647 | struct ip_vs_lblcr_table *tbl = svc->sched_data; |
44548375 | 648 | struct ip_vs_iphdr iph; |
f728bafb SW |
649 | struct ip_vs_dest *dest = NULL; |
650 | struct ip_vs_lblcr_entry *en; | |
1da177e4 | 651 | |
44548375 JV |
652 | ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph); |
653 | ||
1e3e238e | 654 | IP_VS_DBG(6, "%s(): Scheduling...\n", __func__); |
1da177e4 | 655 | |
f728bafb SW |
656 | /* First look in our cache */ |
657 | read_lock(&svc->sched_lock); | |
44548375 | 658 | en = ip_vs_lblcr_get(svc->af, tbl, &iph.daddr); |
f728bafb SW |
659 | if (en) { |
660 | /* We only hold a read lock, but this is atomic */ | |
661 | en->lastuse = jiffies; | |
662 | ||
663 | /* Get the least loaded destination */ | |
664 | read_lock(&en->set.lock); | |
1da177e4 | 665 | dest = ip_vs_dest_set_min(&en->set); |
f728bafb SW |
666 | read_unlock(&en->set.lock); |
667 | ||
668 | /* More than one destination + enough time passed by, cleanup */ | |
1da177e4 | 669 | if (atomic_read(&en->set.size) > 1 && |
f728bafb | 670 | time_after(jiffies, en->set.lastmod + |
b27d777e | 671 | sysctl_lblcr_expiration(svc))) { |
1da177e4 | 672 | struct ip_vs_dest *m; |
f728bafb SW |
673 | |
674 | write_lock(&en->set.lock); | |
1da177e4 LT |
675 | m = ip_vs_dest_set_max(&en->set); |
676 | if (m) | |
677 | ip_vs_dest_set_erase(&en->set, m); | |
f728bafb SW |
678 | write_unlock(&en->set.lock); |
679 | } | |
680 | ||
681 | /* If the destination is not overloaded, use it */ | |
682 | if (dest && !is_overloaded(dest, svc)) { | |
683 | read_unlock(&svc->sched_lock); | |
684 | goto out; | |
685 | } | |
686 | ||
687 | /* The cache entry is invalid, time to schedule */ | |
44548375 | 688 | dest = __ip_vs_lblcr_schedule(svc); |
f728bafb | 689 | if (!dest) { |
41ac51ee | 690 | ip_vs_scheduler_err(svc, "no destination available"); |
f728bafb SW |
691 | read_unlock(&svc->sched_lock); |
692 | return NULL; | |
1da177e4 | 693 | } |
f728bafb SW |
694 | |
695 | /* Update our cache entry */ | |
696 | write_lock(&en->set.lock); | |
697 | ip_vs_dest_set_insert(&en->set, dest); | |
698 | write_unlock(&en->set.lock); | |
699 | } | |
700 | read_unlock(&svc->sched_lock); | |
701 | ||
702 | if (dest) | |
703 | goto out; | |
704 | ||
705 | /* No cache entry, time to schedule */ | |
44548375 | 706 | dest = __ip_vs_lblcr_schedule(svc); |
f728bafb SW |
707 | if (!dest) { |
708 | IP_VS_DBG(1, "no destination available\n"); | |
709 | return NULL; | |
1da177e4 | 710 | } |
1da177e4 | 711 | |
f728bafb SW |
712 | /* If we fail to create a cache entry, we'll just use the valid dest */ |
713 | write_lock(&svc->sched_lock); | |
44548375 | 714 | ip_vs_lblcr_new(tbl, &iph.daddr, dest); |
f728bafb SW |
715 | write_unlock(&svc->sched_lock); |
716 | ||
717 | out: | |
44548375 JV |
718 | IP_VS_DBG_BUF(6, "LBLCR: destination IP address %s --> server %s:%d\n", |
719 | IP_VS_DBG_ADDR(svc->af, &iph.daddr), | |
720 | IP_VS_DBG_ADDR(svc->af, &dest->addr), ntohs(dest->port)); | |
1da177e4 LT |
721 | |
722 | return dest; | |
723 | } | |
724 | ||
725 | ||
726 | /* | |
727 | * IPVS LBLCR Scheduler structure | |
728 | */ | |
729 | static struct ip_vs_scheduler ip_vs_lblcr_scheduler = | |
730 | { | |
731 | .name = "lblcr", | |
732 | .refcnt = ATOMIC_INIT(0), | |
733 | .module = THIS_MODULE, | |
d149ccc9 | 734 | .n_list = LIST_HEAD_INIT(ip_vs_lblcr_scheduler.n_list), |
1da177e4 LT |
735 | .init_service = ip_vs_lblcr_init_svc, |
736 | .done_service = ip_vs_lblcr_done_svc, | |
1da177e4 LT |
737 | .schedule = ip_vs_lblcr_schedule, |
738 | }; | |
739 | ||
61b1ab45 HS |
740 | /* |
741 | * per netns init. | |
742 | */ | |
fb1de432 | 743 | #ifdef CONFIG_SYSCTL |
61b1ab45 HS |
744 | static int __net_init __ip_vs_lblcr_init(struct net *net) |
745 | { | |
d0a1eef9 HS |
746 | struct netns_ipvs *ipvs = net_ipvs(net); |
747 | ||
4b984cd5 HS |
748 | if (!ipvs) |
749 | return -ENOENT; | |
750 | ||
d0a1eef9 HS |
751 | if (!net_eq(net, &init_net)) { |
752 | ipvs->lblcr_ctl_table = kmemdup(vs_vars_table, | |
753 | sizeof(vs_vars_table), | |
754 | GFP_KERNEL); | |
755 | if (ipvs->lblcr_ctl_table == NULL) | |
0443929f | 756 | return -ENOMEM; |
d0a1eef9 HS |
757 | } else |
758 | ipvs->lblcr_ctl_table = vs_vars_table; | |
b27d777e | 759 | ipvs->sysctl_lblcr_expiration = DEFAULT_EXPIRATION; |
d0a1eef9 HS |
760 | ipvs->lblcr_ctl_table[0].data = &ipvs->sysctl_lblcr_expiration; |
761 | ||
762 | ipvs->lblcr_ctl_header = | |
ec8f23ce | 763 | register_net_sysctl(net, "net/ipv4/vs", ipvs->lblcr_ctl_table); |
0443929f SH |
764 | if (!ipvs->lblcr_ctl_header) { |
765 | if (!net_eq(net, &init_net)) | |
766 | kfree(ipvs->lblcr_ctl_table); | |
767 | return -ENOMEM; | |
768 | } | |
61b1ab45 HS |
769 | |
770 | return 0; | |
771 | } | |
772 | ||
773 | static void __net_exit __ip_vs_lblcr_exit(struct net *net) | |
774 | { | |
d0a1eef9 HS |
775 | struct netns_ipvs *ipvs = net_ipvs(net); |
776 | ||
777 | unregister_net_sysctl_table(ipvs->lblcr_ctl_header); | |
61b1ab45 | 778 | |
d0a1eef9 HS |
779 | if (!net_eq(net, &init_net)) |
780 | kfree(ipvs->lblcr_ctl_table); | |
61b1ab45 HS |
781 | } |
782 | ||
fb1de432 SH |
783 | #else |
784 | ||
785 | static int __net_init __ip_vs_lblcr_init(struct net *net) { return 0; } | |
786 | static void __net_exit __ip_vs_lblcr_exit(struct net *net) { } | |
787 | ||
788 | #endif | |
789 | ||
61b1ab45 HS |
790 | static struct pernet_operations ip_vs_lblcr_ops = { |
791 | .init = __ip_vs_lblcr_init, | |
792 | .exit = __ip_vs_lblcr_exit, | |
793 | }; | |
1da177e4 LT |
794 | |
795 | static int __init ip_vs_lblcr_init(void) | |
796 | { | |
a014bc8f PE |
797 | int ret; |
798 | ||
61b1ab45 HS |
799 | ret = register_pernet_subsys(&ip_vs_lblcr_ops); |
800 | if (ret) | |
801 | return ret; | |
802 | ||
a014bc8f PE |
803 | ret = register_ip_vs_scheduler(&ip_vs_lblcr_scheduler); |
804 | if (ret) | |
61b1ab45 | 805 | unregister_pernet_subsys(&ip_vs_lblcr_ops); |
a014bc8f | 806 | return ret; |
1da177e4 LT |
807 | } |
808 | ||
1da177e4 LT |
809 | static void __exit ip_vs_lblcr_cleanup(void) |
810 | { | |
1da177e4 | 811 | unregister_ip_vs_scheduler(&ip_vs_lblcr_scheduler); |
61b1ab45 | 812 | unregister_pernet_subsys(&ip_vs_lblcr_ops); |
1da177e4 LT |
813 | } |
814 | ||
815 | ||
816 | module_init(ip_vs_lblcr_init); | |
817 | module_exit(ip_vs_lblcr_cleanup); | |
818 | MODULE_LICENSE("GPL"); |