blob: 1383b0eadc0e777d5ff6dbce2a2ded451a3ae296 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPVS: Locality-Based Least-Connection scheduling module
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 * Martin Hamilton : fixed the terrible locking bugs
13 * *lock(tbl->lock) ==> *lock(&tbl->lock)
Uwe Kleine-König421f91d2010-06-11 12:17:00 +020014 * Wensong Zhang : fixed the uninitialized tbl->lock bug
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Wensong Zhang : added doing full expiration check to
16 * collect stale entries of 24+ hours when
17 * no partial expire check in a half hour
18 * Julian Anastasov : replaced del_timer call with del_timer_sync
19 * to avoid the possible race between timer
20 * handler and del_timer thread in SMP
21 *
22 */
23
24/*
25 * The lblc algorithm is as follows (pseudo code):
26 *
27 * if cachenode[dest_ip] is null then
28 * n, cachenode[dest_ip] <- {weighted least-conn node};
29 * else
30 * n <- cachenode[dest_ip];
31 * if (n is dead) OR
32 * (n.conns>n.weight AND
33 * there is a node m with m.conns<m.weight/2) then
34 * n, cachenode[dest_ip] <- {weighted least-conn node};
35 *
36 * return n;
37 *
38 * Thanks must go to Wenzhuo Zhang for talking WCCP to me and pushing
39 * me to write this module.
40 */
41
Hannes Eder9aada7a2009-07-30 14:29:44 -070042#define KMSG_COMPONENT "IPVS"
43#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
44
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020045#include <linux/ip.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090046#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/module.h>
48#include <linux/kernel.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020049#include <linux/skbuff.h>
Al Virod7fe0f22006-12-03 23:15:30 -050050#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/* for sysctl */
53#include <linux/fs.h>
54#include <linux/sysctl.h>
55
56#include <net/ip_vs.h>
57
58
59/*
60 * It is for garbage collection of stale IPVS lblc 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/*
78 * for IPVS lblc entry hash table
79 */
80#ifndef CONFIG_IP_VS_LBLC_TAB_BITS
81#define CONFIG_IP_VS_LBLC_TAB_BITS 10
82#endif
83#define IP_VS_LBLC_TAB_BITS CONFIG_IP_VS_LBLC_TAB_BITS
84#define IP_VS_LBLC_TAB_SIZE (1 << IP_VS_LBLC_TAB_BITS)
85#define IP_VS_LBLC_TAB_MASK (IP_VS_LBLC_TAB_SIZE - 1)
86
87
88/*
89 * IPVS lblc entry represents an association between destination
90 * IP address and its destination server
91 */
92struct ip_vs_lblc_entry {
Julian Anastasovc2a4ffb2013-03-22 11:46:40 +020093 struct hlist_node list;
Julius Volz44548372008-11-03 17:08:28 -080094 int af; /* address family */
95 union nf_inet_addr addr; /* destination IP address */
Julian Anastasovc2a4ffb2013-03-22 11:46:40 +020096 struct ip_vs_dest __rcu *dest; /* real server (cache) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 unsigned long lastuse; /* last used time */
Julian Anastasovc2a4ffb2013-03-22 11:46:40 +020098 struct rcu_head rcu_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099};
100
101
102/*
103 * IPVS lblc hash table
104 */
105struct ip_vs_lblc_table {
Julian Anastasovc2a4ffb2013-03-22 11:46:40 +0200106 struct rcu_head rcu_head;
Julian Anastasovf33c8b92013-04-17 23:50:47 +0300107 struct hlist_head bucket[IP_VS_LBLC_TAB_SIZE]; /* hash bucket */
Julian Anastasovc2a4ffb2013-03-22 11:46:40 +0200108 struct timer_list periodic_timer; /* collect stale entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 atomic_t entries; /* number of entries */
110 int max_size; /* maximum size of entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 int rover; /* rover for expire check */
112 int counter; /* counter for no expire */
Julian Anastasovc2a4ffb2013-03-22 11:46:40 +0200113 bool dead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114};
115
116
117/*
118 * IPVS LBLC sysctl table
119 */
Simon Hormanfb1de432011-02-04 18:33:02 +0900120#ifdef CONFIG_SYSCTL
Joe Perchesfe2c6332013-06-11 23:04:25 -0700121static struct ctl_table vs_vars_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 .procname = "lblc_expiration",
Hans Schillstromb6e885d2011-01-03 14:44:45 +0100124 .data = NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 .maxlen = sizeof(int),
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900126 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800127 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800129 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130};
Simon Hormanfb1de432011-02-04 18:33:02 +0900131#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133static inline void ip_vs_lblc_free(struct ip_vs_lblc_entry *en)
134{
Julian Anastasovc2a4ffb2013-03-22 11:46:40 +0200135 struct ip_vs_dest *dest;
136
137 hlist_del_rcu(&en->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300139 * We don't kfree dest because it is referred either by its service
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 * or the trash dest list.
141 */
Julian Anastasovc2a4ffb2013-03-22 11:46:40 +0200142 dest = rcu_dereference_protected(en->dest, 1);
143 ip_vs_dest_put(dest);
144 kfree_rcu(en, rcu_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
147
148/*
149 * Returns hash value for IPVS LBLC entry
150 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000151static inline unsigned int
Julius Volz44548372008-11-03 17:08:28 -0800152ip_vs_lblc_hashkey(int af, const union nf_inet_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Julius Volz44548372008-11-03 17:08:28 -0800154 __be32 addr_fold = addr->ip;
155
156#ifdef CONFIG_IP_VS_IPV6
157 if (af == AF_INET6)
158 addr_fold = addr->ip6[0]^addr->ip6[1]^
159 addr->ip6[2]^addr->ip6[3];
160#endif
161 return (ntohl(addr_fold)*2654435761UL) & IP_VS_LBLC_TAB_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
164
165/*
166 * Hash an entry in the ip_vs_lblc_table.
167 * returns bool success.
168 */
Sven Wegener39ac50d2008-08-18 00:52:08 +0200169static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170ip_vs_lblc_hash(struct ip_vs_lblc_table *tbl, struct ip_vs_lblc_entry *en)
171{
Eric Dumazet95c96172012-04-15 05:58:06 +0000172 unsigned int hash = ip_vs_lblc_hashkey(en->af, &en->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Julian Anastasovc2a4ffb2013-03-22 11:46:40 +0200174 hlist_add_head_rcu(&en->list, &tbl->bucket[hash]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 atomic_inc(&tbl->entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
178
Julian Anastasovc2a4ffb2013-03-22 11:46:40 +0200179/* Get ip_vs_lblc_entry associated with supplied parameters. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180static inline struct ip_vs_lblc_entry *
Julius Volz44548372008-11-03 17:08:28 -0800181ip_vs_lblc_get(int af, struct ip_vs_lblc_table *tbl,
182 const union nf_inet_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Eric Dumazet95c96172012-04-15 05:58:06 +0000184 unsigned int hash = ip_vs_lblc_hashkey(af, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 struct ip_vs_lblc_entry *en;
186
Julian Anastasovc2a4ffb2013-03-22 11:46:40 +0200187 hlist_for_each_entry_rcu(en, &tbl->bucket[hash], list)
Julius Volz44548372008-11-03 17:08:28 -0800188 if (ip_vs_addr_equal(af, &en->addr, addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return en;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 return NULL;
192}
193
194
195/*
Sven Wegener39ac50d2008-08-18 00:52:08 +0200196 * Create or update an ip_vs_lblc_entry, which is a mapping of a destination IP
Julian Anastasovba3a3ce2013-03-22 11:46:51 +0200197 * address to a server. Called under spin lock.
Sven Wegener39ac50d2008-08-18 00:52:08 +0200198 */
199static inline struct ip_vs_lblc_entry *
Julius Volz44548372008-11-03 17:08:28 -0800200ip_vs_lblc_new(struct ip_vs_lblc_table *tbl, const union nf_inet_addr *daddr,
Sven Wegener39ac50d2008-08-18 00:52:08 +0200201 struct ip_vs_dest *dest)
202{
203 struct ip_vs_lblc_entry *en;
204
Julius Volz44548372008-11-03 17:08:28 -0800205 en = ip_vs_lblc_get(dest->af, tbl, daddr);
Sven Wegener39ac50d2008-08-18 00:52:08 +0200206 if (!en) {
207 en = kmalloc(sizeof(*en), GFP_ATOMIC);
Joe Perches0a9ee812011-08-29 14:17:25 -0700208 if (!en)
Sven Wegener39ac50d2008-08-18 00:52:08 +0200209 return NULL;
Sven Wegener39ac50d2008-08-18 00:52:08 +0200210
Julius Volz44548372008-11-03 17:08:28 -0800211 en->af = dest->af;
212 ip_vs_addr_copy(dest->af, &en->addr, daddr);
Sven Wegener39ac50d2008-08-18 00:52:08 +0200213 en->lastuse = jiffies;
214
Julian Anastasovc2a4ffb2013-03-22 11:46:40 +0200215 ip_vs_dest_hold(dest);
216 RCU_INIT_POINTER(en->dest, dest);
Sven Wegener39ac50d2008-08-18 00:52:08 +0200217
218 ip_vs_lblc_hash(tbl, en);
Julian Anastasovc2a4ffb2013-03-22 11:46:40 +0200219 } else {
220 struct ip_vs_dest *old_dest;
221
222 old_dest = rcu_dereference_protected(en->dest, 1);
223 if (old_dest != dest) {
224 ip_vs_dest_put(old_dest);
225 ip_vs_dest_hold(dest);
226 /* No ordering constraints for refcnt */
227 RCU_INIT_POINTER(en->dest, dest);
228 }
Sven Wegener39ac50d2008-08-18 00:52:08 +0200229 }
230
231 return en;
232}
233
234
235/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 * Flush all the entries of the specified table.
237 */
Julian Anastasovc2a4ffb2013-03-22 11:46:40 +0200238static void ip_vs_lblc_flush(struct ip_vs_service *svc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Julian Anastasovc2a4ffb2013-03-22 11:46:40 +0200240 struct ip_vs_lblc_table *tbl = svc->sched_data;
241 struct ip_vs_lblc_entry *en;
242 struct hlist_node *next;
Sven Wegener39ac50d2008-08-18 00:52:08 +0200243 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Julian Anastasovba3a3ce2013-03-22 11:46:51 +0200245 spin_lock_bh(&svc->sched_lock);
Julian Anastasovc2a4ffb2013-03-22 11:46:40 +0200246 tbl->dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 for (i=0; i<IP_VS_LBLC_TAB_SIZE; i++) {
Julian Anastasovc2a4ffb2013-03-22 11:46:40 +0200248 hlist_for_each_entry_safe(en, next, &tbl->bucket[i], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 ip_vs_lblc_free(en);
250 atomic_dec(&tbl->entries);
251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
Julian Anastasovba3a3ce2013-03-22 11:46:51 +0200253 spin_unlock_bh(&svc->sched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
Simon Hormanb27d7772011-02-04 18:33:01 +0900256static int sysctl_lblc_expiration(struct ip_vs_service *svc)
257{
258#ifdef CONFIG_SYSCTL
259 struct netns_ipvs *ipvs = net_ipvs(svc->net);
260 return ipvs->sysctl_lblc_expiration;
261#else
262 return DEFAULT_EXPIRATION;
263#endif
264}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Sven Wegener39ac50d2008-08-18 00:52:08 +0200266static inline void ip_vs_lblc_full_check(struct ip_vs_service *svc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Sven Wegener39ac50d2008-08-18 00:52:08 +0200268 struct ip_vs_lblc_table *tbl = svc->sched_data;
Julian Anastasovc2a4ffb2013-03-22 11:46:40 +0200269 struct ip_vs_lblc_entry *en;
270 struct hlist_node *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 unsigned long now = jiffies;
272 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 for (i=0, j=tbl->rover; i<IP_VS_LBLC_TAB_SIZE; i++) {
275 j = (j + 1) & IP_VS_LBLC_TAB_MASK;
276
Julian Anastasovba3a3ce2013-03-22 11:46:51 +0200277 spin_lock(&svc->sched_lock);
Julian Anastasovc2a4ffb2013-03-22 11:46:40 +0200278 hlist_for_each_entry_safe(en, next, &tbl->bucket[j], list) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900279 if (time_before(now,
Hans Schillstromb6e885d2011-01-03 14:44:45 +0100280 en->lastuse +
Simon Hormanb27d7772011-02-04 18:33:01 +0900281 sysctl_lblc_expiration(svc)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 continue;
283
284 ip_vs_lblc_free(en);
285 atomic_dec(&tbl->entries);
286 }
Julian Anastasovba3a3ce2013-03-22 11:46:51 +0200287 spin_unlock(&svc->sched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289 tbl->rover = j;
290}
291
292
293/*
294 * Periodical timer handler for IPVS lblc table
295 * It is used to collect stale entries when the number of entries
296 * exceeds the maximum size of the table.
297 *
298 * Fixme: we probably need more complicated algorithm to collect
299 * entries that have not been used for a long time even
300 * if the number of entries doesn't exceed the maximum size
301 * of the table.
302 * The full expiration check is for this purpose now.
303 */
304static void ip_vs_lblc_check_expire(unsigned long data)
305{
Sven Wegener39ac50d2008-08-18 00:52:08 +0200306 struct ip_vs_service *svc = (struct ip_vs_service *) data;
307 struct ip_vs_lblc_table *tbl = svc->sched_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 unsigned long now = jiffies;
309 int goal;
310 int i, j;
Julian Anastasovc2a4ffb2013-03-22 11:46:40 +0200311 struct ip_vs_lblc_entry *en;
312 struct hlist_node *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if ((tbl->counter % COUNT_FOR_FULL_EXPIRATION) == 0) {
315 /* do full expiration check */
Sven Wegener39ac50d2008-08-18 00:52:08 +0200316 ip_vs_lblc_full_check(svc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 tbl->counter = 1;
318 goto out;
319 }
320
321 if (atomic_read(&tbl->entries) <= tbl->max_size) {
322 tbl->counter++;
323 goto out;
324 }
325
326 goal = (atomic_read(&tbl->entries) - tbl->max_size)*4/3;
327 if (goal > tbl->max_size/2)
328 goal = tbl->max_size/2;
329
330 for (i=0, j=tbl->rover; i<IP_VS_LBLC_TAB_SIZE; i++) {
331 j = (j + 1) & IP_VS_LBLC_TAB_MASK;
332
Julian Anastasovba3a3ce2013-03-22 11:46:51 +0200333 spin_lock(&svc->sched_lock);
Julian Anastasovc2a4ffb2013-03-22 11:46:40 +0200334 hlist_for_each_entry_safe(en, next, &tbl->bucket[j], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (time_before(now, en->lastuse + ENTRY_TIMEOUT))
336 continue;
337
338 ip_vs_lblc_free(en);
339 atomic_dec(&tbl->entries);
340 goal--;
341 }
Julian Anastasovba3a3ce2013-03-22 11:46:51 +0200342 spin_unlock(&svc->sched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (goal <= 0)
344 break;
345 }
346 tbl->rover = j;
347
348 out:
349 mod_timer(&tbl->periodic_timer, jiffies+CHECK_EXPIRE_INTERVAL);
350}
351
352
353static int ip_vs_lblc_init_svc(struct ip_vs_service *svc)
354{
355 int i;
356 struct ip_vs_lblc_table *tbl;
357
358 /*
359 * Allocate the ip_vs_lblc_table for this service
360 */
Julian Anastasov748d8452012-04-13 16:49:40 +0300361 tbl = kmalloc(sizeof(*tbl), GFP_KERNEL);
Joe Perches0a9ee812011-08-29 14:17:25 -0700362 if (tbl == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return -ENOMEM;
Joe Perches0a9ee812011-08-29 14:17:25 -0700364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 svc->sched_data = tbl;
366 IP_VS_DBG(6, "LBLC hash table (memory=%Zdbytes) allocated for "
Sven Wegener39ac50d2008-08-18 00:52:08 +0200367 "current service\n", sizeof(*tbl));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 /*
370 * Initialize the hash buckets
371 */
372 for (i=0; i<IP_VS_LBLC_TAB_SIZE; i++) {
Julian Anastasovc2a4ffb2013-03-22 11:46:40 +0200373 INIT_HLIST_HEAD(&tbl->bucket[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 tbl->max_size = IP_VS_LBLC_TAB_SIZE*16;
376 tbl->rover = 0;
377 tbl->counter = 1;
Julian Anastasovc2a4ffb2013-03-22 11:46:40 +0200378 tbl->dead = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 /*
381 * Hook periodic timer for garbage collection
382 */
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800383 setup_timer(&tbl->periodic_timer, ip_vs_lblc_check_expire,
Sven Wegener39ac50d2008-08-18 00:52:08 +0200384 (unsigned long)svc);
385 mod_timer(&tbl->periodic_timer, jiffies + CHECK_EXPIRE_INTERVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 return 0;
388}
389
390
Julian Anastasoved3ffc42013-03-22 11:46:50 +0200391static void ip_vs_lblc_done_svc(struct ip_vs_service *svc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 struct ip_vs_lblc_table *tbl = svc->sched_data;
394
395 /* remove periodic timer */
396 del_timer_sync(&tbl->periodic_timer);
397
398 /* got to clean up table entries here */
Julian Anastasovc2a4ffb2013-03-22 11:46:40 +0200399 ip_vs_lblc_flush(svc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 /* release the table itself */
Julian Anastasovc2a4ffb2013-03-22 11:46:40 +0200402 kfree_rcu(tbl, rcu_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 IP_VS_DBG(6, "LBLC hash table (memory=%Zdbytes) released\n",
Sven Wegener39ac50d2008-08-18 00:52:08 +0200404 sizeof(*tbl));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408static inline struct ip_vs_dest *
Julius Volz44548372008-11-03 17:08:28 -0800409__ip_vs_lblc_schedule(struct ip_vs_service *svc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
411 struct ip_vs_dest *dest, *least;
412 int loh, doh;
413
414 /*
Changli Gaob552f7e2011-02-19 17:32:28 +0800415 * We use the following formula to estimate the load:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 * (dest overhead) / dest->weight
417 *
418 * Remember -- no floats in kernel mode!!!
419 * The comparison of h1*w2 > h2*w1 is equivalent to that of
420 * h1/w1 > h2/w2
421 * if every weight is larger than zero.
422 *
423 * The server with weight=0 is quiesced and will not receive any
424 * new connection.
425 */
Julian Anastasovc2a4ffb2013-03-22 11:46:40 +0200426 list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
428 continue;
429 if (atomic_read(&dest->weight) > 0) {
430 least = dest;
Changli Gaob552f7e2011-02-19 17:32:28 +0800431 loh = ip_vs_dest_conn_overhead(least);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 goto nextstage;
433 }
434 }
435 return NULL;
436
437 /*
438 * Find the destination with the least load.
439 */
440 nextstage:
Julian Anastasovc2a4ffb2013-03-22 11:46:40 +0200441 list_for_each_entry_continue_rcu(dest, &svc->destinations, n_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
443 continue;
444
Changli Gaob552f7e2011-02-19 17:32:28 +0800445 doh = ip_vs_dest_conn_overhead(dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (loh * atomic_read(&dest->weight) >
447 doh * atomic_read(&least->weight)) {
448 least = dest;
449 loh = doh;
450 }
451 }
452
Julius Volz44548372008-11-03 17:08:28 -0800453 IP_VS_DBG_BUF(6, "LBLC: server %s:%d "
454 "activeconns %d refcnt %d weight %d overhead %d\n",
455 IP_VS_DBG_ADDR(least->af, &least->addr),
456 ntohs(least->port),
457 atomic_read(&least->activeconns),
458 atomic_read(&least->refcnt),
459 atomic_read(&least->weight), loh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 return least;
462}
463
464
465/*
466 * If this destination server is overloaded and there is a less loaded
467 * server, then return true.
468 */
469static inline int
470is_overloaded(struct ip_vs_dest *dest, struct ip_vs_service *svc)
471{
472 if (atomic_read(&dest->activeconns) > atomic_read(&dest->weight)) {
473 struct ip_vs_dest *d;
474
Julian Anastasovc2a4ffb2013-03-22 11:46:40 +0200475 list_for_each_entry_rcu(d, &svc->destinations, n_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (atomic_read(&d->activeconns)*2
477 < atomic_read(&d->weight)) {
478 return 1;
479 }
480 }
481 }
482 return 0;
483}
484
485
486/*
487 * Locality-Based (weighted) Least-Connection scheduling
488 */
489static struct ip_vs_dest *
Julian Anastasovbba54de2013-06-16 09:09:36 +0300490ip_vs_lblc_schedule(struct ip_vs_service *svc, const struct sk_buff *skb,
491 struct ip_vs_iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Sven Wegener39ac50d2008-08-18 00:52:08 +0200493 struct ip_vs_lblc_table *tbl = svc->sched_data;
Sven Wegener39ac50d2008-08-18 00:52:08 +0200494 struct ip_vs_dest *dest = NULL;
495 struct ip_vs_lblc_entry *en;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Hannes Eder1e3e2382009-08-02 11:05:41 +0000497 IP_VS_DBG(6, "%s(): Scheduling...\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Sven Wegener39ac50d2008-08-18 00:52:08 +0200499 /* First look in our cache */
Julian Anastasovbba54de2013-06-16 09:09:36 +0300500 en = ip_vs_lblc_get(svc->af, tbl, &iph->daddr);
Sven Wegener39ac50d2008-08-18 00:52:08 +0200501 if (en) {
502 /* We only hold a read lock, but this is atomic */
503 en->lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Sven Wegener39ac50d2008-08-18 00:52:08 +0200505 /*
506 * If the destination is not available, i.e. it's in the trash,
507 * we must ignore it, as it may be removed from under our feet,
508 * if someone drops our reference count. Our caller only makes
509 * sure that destinations, that are not in the trash, are not
510 * moved to the trash, while we are scheduling. But anyone can
511 * free up entries from the trash at any time.
512 */
513
Julian Anastasovc2a4ffb2013-03-22 11:46:40 +0200514 dest = rcu_dereference(en->dest);
515 if ((dest->flags & IP_VS_DEST_F_AVAILABLE) &&
516 atomic_read(&dest->weight) > 0 && !is_overloaded(dest, svc))
517 goto out;
Sven Wegener39ac50d2008-08-18 00:52:08 +0200518 }
Sven Wegener39ac50d2008-08-18 00:52:08 +0200519
520 /* No cache entry or it is invalid, time to schedule */
Julius Volz44548372008-11-03 17:08:28 -0800521 dest = __ip_vs_lblc_schedule(svc);
Sven Wegener39ac50d2008-08-18 00:52:08 +0200522 if (!dest) {
Patrick Schaaf41ac51e2011-02-11 14:01:12 +0100523 ip_vs_scheduler_err(svc, "no destination available");
Sven Wegener39ac50d2008-08-18 00:52:08 +0200524 return NULL;
525 }
526
527 /* If we fail to create a cache entry, we'll just use the valid dest */
Julian Anastasovac692692013-03-22 11:46:54 +0200528 spin_lock_bh(&svc->sched_lock);
Julian Anastasovc2a4ffb2013-03-22 11:46:40 +0200529 if (!tbl->dead)
Julian Anastasovbba54de2013-06-16 09:09:36 +0300530 ip_vs_lblc_new(tbl, &iph->daddr, dest);
Julian Anastasovac692692013-03-22 11:46:54 +0200531 spin_unlock_bh(&svc->sched_lock);
Sven Wegener39ac50d2008-08-18 00:52:08 +0200532
533out:
Julius Volz44548372008-11-03 17:08:28 -0800534 IP_VS_DBG_BUF(6, "LBLC: destination IP address %s --> server %s:%d\n",
Julian Anastasovbba54de2013-06-16 09:09:36 +0300535 IP_VS_DBG_ADDR(svc->af, &iph->daddr),
Julius Volz44548372008-11-03 17:08:28 -0800536 IP_VS_DBG_ADDR(svc->af, &dest->addr), ntohs(dest->port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 return dest;
539}
540
541
542/*
543 * IPVS LBLC Scheduler structure
544 */
545static struct ip_vs_scheduler ip_vs_lblc_scheduler =
546{
547 .name = "lblc",
548 .refcnt = ATOMIC_INIT(0),
549 .module = THIS_MODULE,
Sven Wegenerd149ccc2008-08-10 09:18:02 +0000550 .n_list = LIST_HEAD_INIT(ip_vs_lblc_scheduler.n_list),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 .init_service = ip_vs_lblc_init_svc,
552 .done_service = ip_vs_lblc_done_svc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 .schedule = ip_vs_lblc_schedule,
554};
555
Hans Schillstrom61b1ab42011-01-03 14:44:42 +0100556/*
557 * per netns init.
558 */
Simon Hormanfb1de432011-02-04 18:33:02 +0900559#ifdef CONFIG_SYSCTL
Hans Schillstrom61b1ab42011-01-03 14:44:42 +0100560static int __net_init __ip_vs_lblc_init(struct net *net)
561{
Hans Schillstromb6e885d2011-01-03 14:44:45 +0100562 struct netns_ipvs *ipvs = net_ipvs(net);
Hans Schillstrom61b1ab42011-01-03 14:44:42 +0100563
Hans Schillstrom4b984cd2012-04-26 09:45:34 +0200564 if (!ipvs)
565 return -ENOENT;
566
Hans Schillstromb6e885d2011-01-03 14:44:45 +0100567 if (!net_eq(net, &init_net)) {
568 ipvs->lblc_ctl_table = kmemdup(vs_vars_table,
569 sizeof(vs_vars_table),
570 GFP_KERNEL);
571 if (ipvs->lblc_ctl_table == NULL)
Simon Horman04439292011-02-01 18:29:04 +0100572 return -ENOMEM;
Eric W. Biederman464dc802012-11-16 03:02:59 +0000573
574 /* Don't export sysctls to unprivileged users */
575 if (net->user_ns != &init_user_ns)
576 ipvs->lblc_ctl_table[0].procname = NULL;
577
Hans Schillstromb6e885d2011-01-03 14:44:45 +0100578 } else
579 ipvs->lblc_ctl_table = vs_vars_table;
Simon Hormanb27d7772011-02-04 18:33:01 +0900580 ipvs->sysctl_lblc_expiration = DEFAULT_EXPIRATION;
Hans Schillstromb6e885d2011-01-03 14:44:45 +0100581 ipvs->lblc_ctl_table[0].data = &ipvs->sysctl_lblc_expiration;
582
583 ipvs->lblc_ctl_header =
Eric W. Biedermanec8f23c2012-04-19 13:44:49 +0000584 register_net_sysctl(net, "net/ipv4/vs", ipvs->lblc_ctl_table);
Simon Horman04439292011-02-01 18:29:04 +0100585 if (!ipvs->lblc_ctl_header) {
586 if (!net_eq(net, &init_net))
Eric W. Biedermane5ef39e2012-11-19 05:26:30 +0000587 kfree(ipvs->lblc_ctl_table);
Simon Horman04439292011-02-01 18:29:04 +0100588 return -ENOMEM;
589 }
Hans Schillstrom61b1ab42011-01-03 14:44:42 +0100590
591 return 0;
592}
593
594static void __net_exit __ip_vs_lblc_exit(struct net *net)
595{
Hans Schillstromb6e885d2011-01-03 14:44:45 +0100596 struct netns_ipvs *ipvs = net_ipvs(net);
Hans Schillstrom61b1ab42011-01-03 14:44:42 +0100597
Hans Schillstromb6e885d2011-01-03 14:44:45 +0100598 unregister_net_sysctl_table(ipvs->lblc_ctl_header);
599
600 if (!net_eq(net, &init_net))
601 kfree(ipvs->lblc_ctl_table);
Hans Schillstrom61b1ab42011-01-03 14:44:42 +0100602}
603
Simon Hormanfb1de432011-02-04 18:33:02 +0900604#else
605
606static int __net_init __ip_vs_lblc_init(struct net *net) { return 0; }
607static void __net_exit __ip_vs_lblc_exit(struct net *net) { }
608
609#endif
610
Hans Schillstrom61b1ab42011-01-03 14:44:42 +0100611static struct pernet_operations ip_vs_lblc_ops = {
612 .init = __ip_vs_lblc_init,
613 .exit = __ip_vs_lblc_exit,
614};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616static int __init ip_vs_lblc_init(void)
617{
Pavel Emelyanova014bc82007-12-04 00:43:24 -0800618 int ret;
619
Hans Schillstrom61b1ab42011-01-03 14:44:42 +0100620 ret = register_pernet_subsys(&ip_vs_lblc_ops);
621 if (ret)
622 return ret;
623
Pavel Emelyanova014bc82007-12-04 00:43:24 -0800624 ret = register_ip_vs_scheduler(&ip_vs_lblc_scheduler);
625 if (ret)
Hans Schillstrom61b1ab42011-01-03 14:44:42 +0100626 unregister_pernet_subsys(&ip_vs_lblc_ops);
Pavel Emelyanova014bc82007-12-04 00:43:24 -0800627 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630static void __exit ip_vs_lblc_cleanup(void)
631{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 unregister_ip_vs_scheduler(&ip_vs_lblc_scheduler);
Hans Schillstrom61b1ab42011-01-03 14:44:42 +0100633 unregister_pernet_subsys(&ip_vs_lblc_ops);
Julian Anastasovceec4c32013-03-22 11:46:53 +0200634 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
637
638module_init(ip_vs_lblc_init);
639module_exit(ip_vs_lblc_cleanup);
640MODULE_LICENSE("GPL");