1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * IPVS: Weighted Least-Connection Scheduling module
5 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
6 * Peter Kese <peter.kese@ijs.si>
9 * Wensong Zhang : changed the ip_vs_wlc_schedule to return dest
10 * Wensong Zhang : changed to use the inactconns in scheduling
11 * Wensong Zhang : changed some comestics things for debugging
12 * Wensong Zhang : changed for the d-linked destination list
13 * Wensong Zhang : added the ip_vs_wlc_update_svc
14 * Wensong Zhang : added any dest with weight=0 is quiesced
17 #define KMSG_COMPONENT "IPVS"
18 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
20 #include <linux/module.h>
21 #include <linux/kernel.h>
23 #include <net/ip_vs.h>
26 * Weighted Least Connection scheduling
28 static struct ip_vs_dest *
29 ip_vs_wlc_schedule(struct ip_vs_service *svc, const struct sk_buff *skb,
30 struct ip_vs_iphdr *iph)
32 struct ip_vs_dest *dest, *least;
35 IP_VS_DBG(6, "ip_vs_wlc_schedule(): Scheduling...\n");
38 * We calculate the load of each dest server as follows:
39 * (dest overhead) / dest->weight
41 * Remember -- no floats in kernel mode!!!
42 * The comparison of h1*w2 > h2*w1 is equivalent to that of
44 * if every weight is larger than zero.
46 * The server with weight=0 is quiesced and will not receive any
50 list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
51 if (!(dest->flags & IP_VS_DEST_F_OVERLOAD) &&
52 atomic_read(&dest->weight) > 0) {
54 loh = ip_vs_dest_conn_overhead(least);
58 ip_vs_scheduler_err(svc, "no destination available");
62 * Find the destination with the least load.
65 list_for_each_entry_continue_rcu(dest, &svc->destinations, n_list) {
66 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
68 doh = ip_vs_dest_conn_overhead(dest);
69 if ((__s64)loh * atomic_read(&dest->weight) >
70 (__s64)doh * atomic_read(&least->weight)) {
76 IP_VS_DBG_BUF(6, "WLC: server %s:%u "
77 "activeconns %d refcnt %d weight %d overhead %d\n",
78 IP_VS_DBG_ADDR(least->af, &least->addr),
80 atomic_read(&least->activeconns),
81 refcount_read(&least->refcnt),
82 atomic_read(&least->weight), loh);
88 static struct ip_vs_scheduler ip_vs_wlc_scheduler =
91 .refcnt = ATOMIC_INIT(0),
92 .module = THIS_MODULE,
93 .n_list = LIST_HEAD_INIT(ip_vs_wlc_scheduler.n_list),
94 .schedule = ip_vs_wlc_schedule,
98 static int __init ip_vs_wlc_init(void)
100 return register_ip_vs_scheduler(&ip_vs_wlc_scheduler);
103 static void __exit ip_vs_wlc_cleanup(void)
105 unregister_ip_vs_scheduler(&ip_vs_wlc_scheduler);
109 module_init(ip_vs_wlc_init);
110 module_exit(ip_vs_wlc_cleanup);
111 MODULE_LICENSE("GPL");
112 MODULE_DESCRIPTION("ipvs weighted least connection scheduler");