blob: 548bf37aa29e07ebd53eaa760dbcadf844edfa13 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPVS: Locality-Based Least-Connection with Replication scheduler
3 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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
Hannes Eder9aada7a2009-07-30 14:29:44 -070040#define KMSG_COMPONENT "IPVS"
41#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
42
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020043#include <linux/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/module.h>
45#include <linux/kernel.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020046#include <linux/skbuff.h>
Al Virod7fe0f22006-12-03 23:15:30 -050047#include <linux/jiffies.h>
Simon Horman51f0bc72010-02-26 17:45:14 +010048#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090049#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/* for sysctl */
52#include <linux/fs.h>
53#include <linux/sysctl.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020054#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
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
Simon Hormanb27d7772011-02-04 18:33:01 +090066#define DEFAULT_EXPIRATION (24*60*60*HZ)
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
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 */
Simon Horman51f0bc72010-02-26 17:45:14 +010090struct ip_vs_dest_set_elem {
91 struct list_head list; /* list link */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 struct ip_vs_dest *dest; /* destination server */
93};
94
95struct ip_vs_dest_set {
96 atomic_t size; /* set size */
97 unsigned long lastmod; /* last modified time */
Simon Horman51f0bc72010-02-26 17:45:14 +010098 struct list_head list; /* destination list */
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 rwlock_t lock; /* lock for this list */
100};
101
102
Simon Horman51f0bc72010-02-26 17:45:14 +0100103static struct ip_vs_dest_set_elem *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104ip_vs_dest_set_insert(struct ip_vs_dest_set *set, struct ip_vs_dest *dest)
105{
Simon Horman51f0bc72010-02-26 17:45:14 +0100106 struct ip_vs_dest_set_elem *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Simon Horman51f0bc72010-02-26 17:45:14 +0100108 list_for_each_entry(e, &set->list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 if (e->dest == dest)
110 /* already existed */
111 return NULL;
112 }
113
Sven Wegenerf728baf2008-08-19 08:16:19 +0200114 e = kmalloc(sizeof(*e), GFP_ATOMIC);
Joe Perches0a9ee812011-08-29 14:17:25 -0700115 if (e == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 atomic_inc(&dest->refcnt);
119 e->dest = dest;
120
Simon Horman51f0bc72010-02-26 17:45:14 +0100121 list_add(&e->list, &set->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 atomic_inc(&set->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 set->lastmod = jiffies;
125 return e;
126}
127
128static void
129ip_vs_dest_set_erase(struct ip_vs_dest_set *set, struct ip_vs_dest *dest)
130{
Simon Horman51f0bc72010-02-26 17:45:14 +0100131 struct ip_vs_dest_set_elem *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Simon Horman51f0bc72010-02-26 17:45:14 +0100133 list_for_each_entry(e, &set->list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (e->dest == dest) {
135 /* HIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 atomic_dec(&set->size);
137 set->lastmod = jiffies;
138 atomic_dec(&e->dest->refcnt);
Simon Horman51f0bc72010-02-26 17:45:14 +0100139 list_del(&e->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 kfree(e);
141 break;
142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
146static void ip_vs_dest_set_eraseall(struct ip_vs_dest_set *set)
147{
Simon Horman51f0bc72010-02-26 17:45:14 +0100148 struct ip_vs_dest_set_elem *e, *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 write_lock(&set->lock);
Simon Horman51f0bc72010-02-26 17:45:14 +0100151 list_for_each_entry_safe(e, ep, &set->list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300153 * We don't kfree dest because it is referred either
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 * by its service or by the trash dest list.
155 */
156 atomic_dec(&e->dest->refcnt);
Simon Horman51f0bc72010-02-26 17:45:14 +0100157 list_del(&e->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 kfree(e);
159 }
160 write_unlock(&set->lock);
161}
162
163/* get weighted least-connection node in the destination set */
164static inline struct ip_vs_dest *ip_vs_dest_set_min(struct ip_vs_dest_set *set)
165{
Simon Horman51f0bc72010-02-26 17:45:14 +0100166 register struct ip_vs_dest_set_elem *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 struct ip_vs_dest *dest, *least;
168 int loh, doh;
169
170 if (set == NULL)
171 return NULL;
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 /* select the first destination server, whose weight > 0 */
Simon Horman51f0bc72010-02-26 17:45:14 +0100174 list_for_each_entry(e, &set->list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 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)) {
Changli Gaob552f7e2011-02-19 17:32:28 +0800181 loh = ip_vs_dest_conn_overhead(least);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 goto nextstage;
183 }
184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return NULL;
186
187 /* find the destination with the weighted least load */
188 nextstage:
Simon Horman51f0bc72010-02-26 17:45:14 +0100189 list_for_each_entry(e, &set->list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 dest = e->dest;
191 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
192 continue;
193
Changli Gaob552f7e2011-02-19 17:32:28 +0800194 doh = ip_vs_dest_conn_overhead(dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Hannes Eder1e3e2382009-08-02 11:05:41 +0000203 IP_VS_DBG_BUF(6, "%s(): server %s:%d "
Julius Volz44548372008-11-03 17:08:28 -0800204 "activeconns %d refcnt %d weight %d overhead %d\n",
Hannes Eder1e3e2382009-08-02 11:05:41 +0000205 __func__,
Julius Volz44548372008-11-03 17:08:28 -0800206 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return least;
212}
213
214
215/* get weighted most-connection node in the destination set */
216static inline struct ip_vs_dest *ip_vs_dest_set_max(struct ip_vs_dest_set *set)
217{
Simon Horman51f0bc72010-02-26 17:45:14 +0100218 register struct ip_vs_dest_set_elem *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 struct ip_vs_dest *dest, *most;
220 int moh, doh;
221
222 if (set == NULL)
223 return NULL;
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 /* select the first destination server, whose weight > 0 */
Simon Horman51f0bc72010-02-26 17:45:14 +0100226 list_for_each_entry(e, &set->list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 most = e->dest;
228 if (atomic_read(&most->weight) > 0) {
Changli Gaob552f7e2011-02-19 17:32:28 +0800229 moh = ip_vs_dest_conn_overhead(most);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 goto nextstage;
231 }
232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return NULL;
234
235 /* find the destination with the weighted most load */
236 nextstage:
Simon Horman51f0bc72010-02-26 17:45:14 +0100237 list_for_each_entry(e, &set->list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 dest = e->dest;
Changli Gaob552f7e2011-02-19 17:32:28 +0800239 doh = ip_vs_dest_conn_overhead(dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 /* 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Hannes Eder1e3e2382009-08-02 11:05:41 +0000249 IP_VS_DBG_BUF(6, "%s(): server %s:%d "
Julius Volz44548372008-11-03 17:08:28 -0800250 "activeconns %d refcnt %d weight %d overhead %d\n",
Hannes Eder1e3e2382009-08-02 11:05:41 +0000251 __func__,
Julius Volz44548372008-11-03 17:08:28 -0800252 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return most;
257}
258
259
260/*
261 * IPVS lblcr entry represents an association between destination
262 * IP address and its destination server set
263 */
264struct ip_vs_lblcr_entry {
265 struct list_head list;
Julius Volz44548372008-11-03 17:08:28 -0800266 int af; /* address family */
267 union nf_inet_addr addr; /* destination IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 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 */
276struct ip_vs_lblcr_table {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 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
Simon Hormanfb1de432011-02-04 18:33:02 +0900286#ifdef CONFIG_SYSCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287/*
288 * IPVS LBLCR sysctl table
289 */
290
291static ctl_table vs_vars_table[] = {
292 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 .procname = "lblcr_expiration",
Hans Schillstromd0a1eef2011-01-03 14:44:44 +0100294 .data = NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 .maxlen = sizeof(int),
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900296 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800297 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800299 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300};
Simon Hormanfb1de432011-02-04 18:33:02 +0900301#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303static 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 */
Julius Volz44548372008-11-03 17:08:28 -0800314static inline unsigned
315ip_vs_lblcr_hashkey(int af, const union nf_inet_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Julius Volz44548372008-11-03 17:08:28 -0800317 __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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
327
328/*
329 * Hash an entry in the ip_vs_lblcr_table.
330 * returns bool success.
331 */
Sven Wegenerf728baf2008-08-19 08:16:19 +0200332static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333ip_vs_lblcr_hash(struct ip_vs_lblcr_table *tbl, struct ip_vs_lblcr_entry *en)
334{
Julius Volz44548372008-11-03 17:08:28 -0800335 unsigned hash = ip_vs_lblcr_hashkey(en->af, &en->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 list_add(&en->list, &tbl->bucket[hash]);
338 atomic_inc(&tbl->entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342/*
Sven Wegenerf728baf2008-08-19 08:16:19 +0200343 * Get ip_vs_lblcr_entry associated with supplied parameters. Called under
344 * read lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 */
346static inline struct ip_vs_lblcr_entry *
Julius Volz44548372008-11-03 17:08:28 -0800347ip_vs_lblcr_get(int af, struct ip_vs_lblcr_table *tbl,
348 const union nf_inet_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Julius Volz44548372008-11-03 17:08:28 -0800350 unsigned hash = ip_vs_lblcr_hashkey(af, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 struct ip_vs_lblcr_entry *en;
352
Sven Wegenerf728baf2008-08-19 08:16:19 +0200353 list_for_each_entry(en, &tbl->bucket[hash], list)
Julius Volz44548372008-11-03 17:08:28 -0800354 if (ip_vs_addr_equal(af, &en->addr, addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return en;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 return NULL;
358}
359
360
361/*
Sven Wegenerf728baf2008-08-19 08:16:19 +0200362 * 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 */
365static inline struct ip_vs_lblcr_entry *
Julius Volz44548372008-11-03 17:08:28 -0800366ip_vs_lblcr_new(struct ip_vs_lblcr_table *tbl, const union nf_inet_addr *daddr,
Sven Wegenerf728baf2008-08-19 08:16:19 +0200367 struct ip_vs_dest *dest)
368{
369 struct ip_vs_lblcr_entry *en;
370
Julius Volz44548372008-11-03 17:08:28 -0800371 en = ip_vs_lblcr_get(dest->af, tbl, daddr);
Sven Wegenerf728baf2008-08-19 08:16:19 +0200372 if (!en) {
373 en = kmalloc(sizeof(*en), GFP_ATOMIC);
Joe Perches0a9ee812011-08-29 14:17:25 -0700374 if (!en)
Sven Wegenerf728baf2008-08-19 08:16:19 +0200375 return NULL;
Sven Wegenerf728baf2008-08-19 08:16:19 +0200376
Julius Volz44548372008-11-03 17:08:28 -0800377 en->af = dest->af;
378 ip_vs_addr_copy(dest->af, &en->addr, daddr);
Sven Wegenerf728baf2008-08-19 08:16:19 +0200379 en->lastuse = jiffies;
380
Uwe Kleine-König421f91d2010-06-11 12:17:00 +0200381 /* initialize its dest set */
Sven Wegenerf728baf2008-08-19 08:16:19 +0200382 atomic_set(&(en->set.size), 0);
Simon Horman51f0bc72010-02-26 17:45:14 +0100383 INIT_LIST_HEAD(&en->set.list);
Sven Wegenerf728baf2008-08-19 08:16:19 +0200384 rwlock_init(&en->set.lock);
385
386 ip_vs_lblcr_hash(tbl, en);
387 }
388
389 write_lock(&en->set.lock);
390 ip_vs_dest_set_insert(&en->set, dest);
391 write_unlock(&en->set.lock);
392
393 return en;
394}
395
396
397/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 * Flush all the entries of the specified table.
399 */
400static void ip_vs_lblcr_flush(struct ip_vs_lblcr_table *tbl)
401{
402 int i;
403 struct ip_vs_lblcr_entry *en, *nxt;
404
Sven Wegenerf728baf2008-08-19 08:16:19 +0200405 /* No locking required, only called during cleanup. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 for (i=0; i<IP_VS_LBLCR_TAB_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 list_for_each_entry_safe(en, nxt, &tbl->bucket[i], list) {
408 ip_vs_lblcr_free(en);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
411}
412
Simon Hormanb27d7772011-02-04 18:33:01 +0900413static 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}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Sven Wegenerf728baf2008-08-19 08:16:19 +0200423static inline void ip_vs_lblcr_full_check(struct ip_vs_service *svc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Sven Wegenerf728baf2008-08-19 08:16:19 +0200425 struct ip_vs_lblcr_table *tbl = svc->sched_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 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
Sven Wegenerf728baf2008-08-19 08:16:19 +0200433 write_lock(&svc->sched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 list_for_each_entry_safe(en, nxt, &tbl->bucket[j], list) {
Simon Hormanb27d7772011-02-04 18:33:01 +0900435 if (time_after(en->lastuse +
436 sysctl_lblcr_expiration(svc), now))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 continue;
438
439 ip_vs_lblcr_free(en);
440 atomic_dec(&tbl->entries);
441 }
Sven Wegenerf728baf2008-08-19 08:16:19 +0200442 write_unlock(&svc->sched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
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 */
459static void ip_vs_lblcr_check_expire(unsigned long data)
460{
Sven Wegenerf728baf2008-08-19 08:16:19 +0200461 struct ip_vs_service *svc = (struct ip_vs_service *) data;
462 struct ip_vs_lblcr_table *tbl = svc->sched_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 unsigned long now = jiffies;
464 int goal;
465 int i, j;
466 struct ip_vs_lblcr_entry *en, *nxt;
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if ((tbl->counter % COUNT_FOR_FULL_EXPIRATION) == 0) {
469 /* do full expiration check */
Sven Wegenerf728baf2008-08-19 08:16:19 +0200470 ip_vs_lblcr_full_check(svc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 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
Sven Wegenerf728baf2008-08-19 08:16:19 +0200487 write_lock(&svc->sched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 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 }
Sven Wegenerf728baf2008-08-19 08:16:19 +0200496 write_unlock(&svc->sched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506static 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 */
Sven Wegenerf728baf2008-08-19 08:16:19 +0200514 tbl = kmalloc(sizeof(*tbl), GFP_ATOMIC);
Joe Perches0a9ee812011-08-29 14:17:25 -0700515 if (tbl == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return -ENOMEM;
Joe Perches0a9ee812011-08-29 14:17:25 -0700517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 svc->sched_data = tbl;
519 IP_VS_DBG(6, "LBLCR hash table (memory=%Zdbytes) allocated for "
Sven Wegenerf728baf2008-08-19 08:16:19 +0200520 "current service\n", sizeof(*tbl));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 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 */
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800535 setup_timer(&tbl->periodic_timer, ip_vs_lblcr_check_expire,
Sven Wegenerf728baf2008-08-19 08:16:19 +0200536 (unsigned long)svc);
537 mod_timer(&tbl->periodic_timer, jiffies + CHECK_EXPIRE_INTERVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return 0;
540}
541
542
543static 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 */
Sven Wegenerf728baf2008-08-19 08:16:19 +0200554 kfree(tbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 IP_VS_DBG(6, "LBLCR hash table (memory=%Zdbytes) released\n",
Sven Wegenerf728baf2008-08-19 08:16:19 +0200556 sizeof(*tbl));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 return 0;
559}
560
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562static inline struct ip_vs_dest *
Julius Volz44548372008-11-03 17:08:28 -0800563__ip_vs_lblcr_schedule(struct ip_vs_service *svc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
565 struct ip_vs_dest *dest, *least;
566 int loh, doh;
567
568 /*
Changli Gaob552f7e2011-02-19 17:32:28 +0800569 * We use the following formula to estimate the load:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 * (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;
Changli Gaob552f7e2011-02-19 17:32:28 +0800586 loh = ip_vs_dest_conn_overhead(least);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 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
Changli Gaob552f7e2011-02-19 17:32:28 +0800600 doh = ip_vs_dest_conn_overhead(dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (loh * atomic_read(&dest->weight) >
602 doh * atomic_read(&least->weight)) {
603 least = dest;
604 loh = doh;
605 }
606 }
607
Julius Volz44548372008-11-03 17:08:28 -0800608 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
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 */
624static inline int
625is_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 */
644static struct ip_vs_dest *
645ip_vs_lblcr_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
646{
Sven Wegenerf728baf2008-08-19 08:16:19 +0200647 struct ip_vs_lblcr_table *tbl = svc->sched_data;
Julius Volz44548372008-11-03 17:08:28 -0800648 struct ip_vs_iphdr iph;
Sven Wegenerf728baf2008-08-19 08:16:19 +0200649 struct ip_vs_dest *dest = NULL;
650 struct ip_vs_lblcr_entry *en;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Julius Volz44548372008-11-03 17:08:28 -0800652 ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
653
Hannes Eder1e3e2382009-08-02 11:05:41 +0000654 IP_VS_DBG(6, "%s(): Scheduling...\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Sven Wegenerf728baf2008-08-19 08:16:19 +0200656 /* First look in our cache */
657 read_lock(&svc->sched_lock);
Julius Volz44548372008-11-03 17:08:28 -0800658 en = ip_vs_lblcr_get(svc->af, tbl, &iph.daddr);
Sven Wegenerf728baf2008-08-19 08:16:19 +0200659 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 dest = ip_vs_dest_set_min(&en->set);
Sven Wegenerf728baf2008-08-19 08:16:19 +0200666 read_unlock(&en->set.lock);
667
668 /* More than one destination + enough time passed by, cleanup */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (atomic_read(&en->set.size) > 1 &&
Sven Wegenerf728baf2008-08-19 08:16:19 +0200670 time_after(jiffies, en->set.lastmod +
Simon Hormanb27d7772011-02-04 18:33:01 +0900671 sysctl_lblcr_expiration(svc))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 struct ip_vs_dest *m;
Sven Wegenerf728baf2008-08-19 08:16:19 +0200673
674 write_lock(&en->set.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 m = ip_vs_dest_set_max(&en->set);
676 if (m)
677 ip_vs_dest_set_erase(&en->set, m);
Sven Wegenerf728baf2008-08-19 08:16:19 +0200678 write_unlock(&en->set.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Sven Wegenerf728baf2008-08-19 08:16:19 +0200681 /* 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 */
Julius Volz44548372008-11-03 17:08:28 -0800688 dest = __ip_vs_lblcr_schedule(svc);
Sven Wegenerf728baf2008-08-19 08:16:19 +0200689 if (!dest) {
Patrick Schaaf41ac51e2011-02-11 14:01:12 +0100690 ip_vs_scheduler_err(svc, "no destination available");
Sven Wegenerf728baf2008-08-19 08:16:19 +0200691 read_unlock(&svc->sched_lock);
692 return NULL;
693 }
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 */
Julius Volz44548372008-11-03 17:08:28 -0800706 dest = __ip_vs_lblcr_schedule(svc);
Sven Wegenerf728baf2008-08-19 08:16:19 +0200707 if (!dest) {
708 IP_VS_DBG(1, "no destination available\n");
709 return NULL;
710 }
711
712 /* If we fail to create a cache entry, we'll just use the valid dest */
713 write_lock(&svc->sched_lock);
Julius Volz44548372008-11-03 17:08:28 -0800714 ip_vs_lblcr_new(tbl, &iph.daddr, dest);
Sven Wegenerf728baf2008-08-19 08:16:19 +0200715 write_unlock(&svc->sched_lock);
716
717out:
Julius Volz44548372008-11-03 17:08:28 -0800718 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 return dest;
723}
724
725
726/*
727 * IPVS LBLCR Scheduler structure
728 */
729static struct ip_vs_scheduler ip_vs_lblcr_scheduler =
730{
731 .name = "lblcr",
732 .refcnt = ATOMIC_INIT(0),
733 .module = THIS_MODULE,
Sven Wegenerd149ccc2008-08-10 09:18:02 +0000734 .n_list = LIST_HEAD_INIT(ip_vs_lblcr_scheduler.n_list),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 .init_service = ip_vs_lblcr_init_svc,
736 .done_service = ip_vs_lblcr_done_svc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 .schedule = ip_vs_lblcr_schedule,
738};
739
Hans Schillstrom61b1ab42011-01-03 14:44:42 +0100740/*
741 * per netns init.
742 */
Simon Hormanfb1de432011-02-04 18:33:02 +0900743#ifdef CONFIG_SYSCTL
Hans Schillstrom61b1ab42011-01-03 14:44:42 +0100744static int __net_init __ip_vs_lblcr_init(struct net *net)
745{
Hans Schillstromd0a1eef2011-01-03 14:44:44 +0100746 struct netns_ipvs *ipvs = net_ipvs(net);
Hans Schillstrom61b1ab42011-01-03 14:44:42 +0100747
Hans Schillstrom4b984cd2012-04-26 09:45:34 +0200748 if (!ipvs)
749 return -ENOENT;
750
Hans Schillstromd0a1eef2011-01-03 14:44:44 +0100751 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)
Simon Horman04439292011-02-01 18:29:04 +0100756 return -ENOMEM;
Hans Schillstromd0a1eef2011-01-03 14:44:44 +0100757 } else
758 ipvs->lblcr_ctl_table = vs_vars_table;
Simon Hormanb27d7772011-02-04 18:33:01 +0900759 ipvs->sysctl_lblcr_expiration = DEFAULT_EXPIRATION;
Hans Schillstromd0a1eef2011-01-03 14:44:44 +0100760 ipvs->lblcr_ctl_table[0].data = &ipvs->sysctl_lblcr_expiration;
761
762 ipvs->lblcr_ctl_header =
763 register_net_sysctl_table(net, net_vs_ctl_path,
764 ipvs->lblcr_ctl_table);
Simon Horman04439292011-02-01 18:29:04 +0100765 if (!ipvs->lblcr_ctl_header) {
766 if (!net_eq(net, &init_net))
767 kfree(ipvs->lblcr_ctl_table);
768 return -ENOMEM;
769 }
Hans Schillstrom61b1ab42011-01-03 14:44:42 +0100770
771 return 0;
772}
773
774static void __net_exit __ip_vs_lblcr_exit(struct net *net)
775{
Hans Schillstromd0a1eef2011-01-03 14:44:44 +0100776 struct netns_ipvs *ipvs = net_ipvs(net);
Hans Schillstrom61b1ab42011-01-03 14:44:42 +0100777
Hans Schillstromd0a1eef2011-01-03 14:44:44 +0100778 unregister_net_sysctl_table(ipvs->lblcr_ctl_header);
779
780 if (!net_eq(net, &init_net))
781 kfree(ipvs->lblcr_ctl_table);
Hans Schillstrom61b1ab42011-01-03 14:44:42 +0100782}
783
Simon Hormanfb1de432011-02-04 18:33:02 +0900784#else
785
786static int __net_init __ip_vs_lblcr_init(struct net *net) { return 0; }
787static void __net_exit __ip_vs_lblcr_exit(struct net *net) { }
788
789#endif
790
Hans Schillstrom61b1ab42011-01-03 14:44:42 +0100791static struct pernet_operations ip_vs_lblcr_ops = {
792 .init = __ip_vs_lblcr_init,
793 .exit = __ip_vs_lblcr_exit,
794};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796static int __init ip_vs_lblcr_init(void)
797{
Pavel Emelyanova014bc82007-12-04 00:43:24 -0800798 int ret;
799
Hans Schillstrom61b1ab42011-01-03 14:44:42 +0100800 ret = register_pernet_subsys(&ip_vs_lblcr_ops);
801 if (ret)
802 return ret;
803
Pavel Emelyanova014bc82007-12-04 00:43:24 -0800804 ret = register_ip_vs_scheduler(&ip_vs_lblcr_scheduler);
805 if (ret)
Hans Schillstrom61b1ab42011-01-03 14:44:42 +0100806 unregister_pernet_subsys(&ip_vs_lblcr_ops);
Pavel Emelyanova014bc82007-12-04 00:43:24 -0800807 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810static void __exit ip_vs_lblcr_cleanup(void)
811{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 unregister_ip_vs_scheduler(&ip_vs_lblcr_scheduler);
Hans Schillstrom61b1ab42011-01-03 14:44:42 +0100813 unregister_pernet_subsys(&ip_vs_lblcr_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
816
817module_init(ip_vs_lblcr_init);
818module_exit(ip_vs_lblcr_cleanup);
819MODULE_LICENSE("GPL");