blob: 2bdcb1cf21279db80ebb0af0b091f600e8b4921b [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 *
Julian Anastasovbba54de2013-06-16 09:09:36 +030029ip_vs_lc_schedule(struct ip_vs_service *svc, const struct sk_buff *skb,
30 struct ip_vs_iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
32 struct ip_vs_dest *dest, *least = NULL;
33 unsigned int loh = 0, doh;
34
Hannes Eder1e3e2382009-08-02 11:05:41 +000035 IP_VS_DBG(6, "%s(): Scheduling...\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37 /*
38 * Simply select the server with the least number of
39 * (activeconns<<5) + inactconns
40 * Except whose weight is equal to zero.
41 * If the weight is equal to zero, it means that the server is
42 * quiesced, the existing connections to the server still get
43 * served, but no new connection is assigned to the server.
44 */
45
Julian Anastasov4ebd2882013-03-22 11:46:42 +020046 list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 if ((dest->flags & IP_VS_DEST_F_OVERLOAD) ||
48 atomic_read(&dest->weight) == 0)
49 continue;
Changli Gaob552f7e2011-02-19 17:32:28 +080050 doh = ip_vs_dest_conn_overhead(dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 if (!least || doh < loh) {
52 least = dest;
53 loh = doh;
54 }
55 }
56
Simon Horman68888d12008-12-29 18:37:36 -080057 if (!least)
Patrick Schaaf41ac51e2011-02-11 14:01:12 +010058 ip_vs_scheduler_err(svc, "no destination available");
Simon Horman68888d12008-12-29 18:37:36 -080059 else
60 IP_VS_DBG_BUF(6, "LC: server %s:%u activeconns %d "
61 "inactconns %d\n",
62 IP_VS_DBG_ADDR(svc->af, &least->addr),
63 ntohs(least->port),
64 atomic_read(&least->activeconns),
65 atomic_read(&least->inactconns));
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 return least;
68}
69
70
71static struct ip_vs_scheduler ip_vs_lc_scheduler = {
72 .name = "lc",
73 .refcnt = ATOMIC_INIT(0),
74 .module = THIS_MODULE,
Sven Wegenerd149ccc2008-08-10 09:18:02 +000075 .n_list = LIST_HEAD_INIT(ip_vs_lc_scheduler.n_list),
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 .schedule = ip_vs_lc_schedule,
77};
78
79
80static int __init ip_vs_lc_init(void)
81{
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return register_ip_vs_scheduler(&ip_vs_lc_scheduler) ;
83}
84
85static void __exit ip_vs_lc_cleanup(void)
86{
87 unregister_ip_vs_scheduler(&ip_vs_lc_scheduler);
Julian Anastasovceec4c32013-03-22 11:46:53 +020088 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
91module_init(ip_vs_lc_init);
92module_exit(ip_vs_lc_cleanup);
93MODULE_LICENSE("GPL");