blob: 0cabf78fbc314da7d798ef1ead79bdd50df41e9a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPVS: Least-Connection Scheduling module
3 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Authors: Wensong Zhang <wensong@linuxvirtualserver.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 * Wensong Zhang : added the ip_vs_lc_update_svc
13 * Wensong Zhang : added any dest with weight=0 is quiesced
14 *
15 */
16
Hannes Eder9aada7a2009-07-30 14:29:44 -070017#define KMSG_COMPONENT "IPVS"
18#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/module.h>
21#include <linux/kernel.h>
22
23#include <net/ip_vs.h>
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025/*
26 * Least Connection scheduling
27 */
28static struct ip_vs_dest *
29ip_vs_lc_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
30{
31 struct ip_vs_dest *dest, *least = NULL;
32 unsigned int loh = 0, doh;
33
Hannes Eder1e3e2382009-08-02 11:05:41 +000034 IP_VS_DBG(6, "%s(): Scheduling...\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36 /*
37 * Simply select the server with the least number of
38 * (activeconns<<5) + inactconns
39 * Except whose weight is equal to zero.
40 * If the weight is equal to zero, it means that the server is
41 * quiesced, the existing connections to the server still get
42 * served, but no new connection is assigned to the server.
43 */
44
Julian Anastasov4ebd2882013-03-22 11:46:42 +020045 list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 if ((dest->flags & IP_VS_DEST_F_OVERLOAD) ||
47 atomic_read(&dest->weight) == 0)
48 continue;
Changli Gaob552f7e2011-02-19 17:32:28 +080049 doh = ip_vs_dest_conn_overhead(dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 if (!least || doh < loh) {
51 least = dest;
52 loh = doh;
53 }
54 }
55
Simon Horman68888d12008-12-29 18:37:36 -080056 if (!least)
Patrick Schaaf41ac51e2011-02-11 14:01:12 +010057 ip_vs_scheduler_err(svc, "no destination available");
Simon Horman68888d12008-12-29 18:37:36 -080058 else
59 IP_VS_DBG_BUF(6, "LC: server %s:%u activeconns %d "
60 "inactconns %d\n",
61 IP_VS_DBG_ADDR(svc->af, &least->addr),
62 ntohs(least->port),
63 atomic_read(&least->activeconns),
64 atomic_read(&least->inactconns));
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 return least;
67}
68
69
70static struct ip_vs_scheduler ip_vs_lc_scheduler = {
71 .name = "lc",
72 .refcnt = ATOMIC_INIT(0),
73 .module = THIS_MODULE,
Sven Wegenerd149ccc2008-08-10 09:18:02 +000074 .n_list = LIST_HEAD_INIT(ip_vs_lc_scheduler.n_list),
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 .schedule = ip_vs_lc_schedule,
76};
77
78
79static int __init ip_vs_lc_init(void)
80{
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return register_ip_vs_scheduler(&ip_vs_lc_scheduler) ;
82}
83
84static void __exit ip_vs_lc_cleanup(void)
85{
86 unregister_ip_vs_scheduler(&ip_vs_lc_scheduler);
87}
88
89module_init(ip_vs_lc_init);
90module_exit(ip_vs_lc_cleanup);
91MODULE_LICENSE("GPL");