Merge tag 'dmaengine-5.4-rc1' of git://git.infradead.org/users/vkoul/slave-dma
[linux-2.6-block.git] / net / netfilter / ipvs / ip_vs_sed.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/*
3 * IPVS: Shortest Expected Delay scheduling module
4 *
1da177e4
LT
5 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
6 *
1da177e4 7 * Changes:
1da177e4
LT
8 */
9
10/*
11 * The SED algorithm attempts to minimize each job's expected delay until
12 * completion. The expected delay that the job will experience is
13 * (Ci + 1) / Ui if sent to the ith server, in which Ci is the number of
59c51591 14 * jobs on the ith server and Ui is the fixed service rate (weight) of
1da177e4
LT
15 * the ith server. The SED algorithm adopts a greedy policy that each does
16 * what is in its own best interest, i.e. to join the queue which would
17 * minimize its expected delay of completion.
18 *
19 * See the following paper for more information:
20 * A. Weinrib and S. Shenker, Greed is not enough: Adaptive load sharing
21 * in large heterogeneous systems. In Proceedings IEEE INFOCOM'88,
22 * pages 986-994, 1988.
23 *
24 * Thanks must go to Marko Buuri <marko@buuri.name> for talking SED to me.
25 *
26 * The difference between SED and WLC is that SED includes the incoming
27 * job in the cost function (the increment of 1). SED may outperform
28 * WLC, while scheduling big jobs under larger heterogeneous systems
29 * (the server weight varies a lot).
30 *
31 */
32
9aada7ac
HE
33#define KMSG_COMPONENT "IPVS"
34#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
35
1da177e4
LT
36#include <linux/module.h>
37#include <linux/kernel.h>
38
39#include <net/ip_vs.h>
40
41
c16526a7 42static inline int
1da177e4
LT
43ip_vs_sed_dest_overhead(struct ip_vs_dest *dest)
44{
45 /*
46 * We only use the active connection number in the cost
47 * calculation here.
48 */
49 return atomic_read(&dest->activeconns) + 1;
50}
51
52
53/*
54 * Weighted Least Connection scheduling
55 */
56static struct ip_vs_dest *
bba54de5
JA
57ip_vs_sed_schedule(struct ip_vs_service *svc, const struct sk_buff *skb,
58 struct ip_vs_iphdr *iph)
1da177e4
LT
59{
60 struct ip_vs_dest *dest, *least;
c16526a7 61 int loh, doh;
1da177e4 62
1e3e238e 63 IP_VS_DBG(6, "%s(): Scheduling...\n", __func__);
1da177e4
LT
64
65 /*
66 * We calculate the load of each dest server as follows:
67 * (server expected overhead) / dest->weight
68 *
69 * Remember -- no floats in kernel mode!!!
70 * The comparison of h1*w2 > h2*w1 is equivalent to that of
71 * h1/w1 > h2/w2
72 * if every weight is larger than zero.
73 *
74 * The server with weight=0 is quiesced and will not receive any
75 * new connections.
76 */
77
9be52aba 78 list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
1da177e4
LT
79 if (!(dest->flags & IP_VS_DEST_F_OVERLOAD) &&
80 atomic_read(&dest->weight) > 0) {
81 least = dest;
82 loh = ip_vs_sed_dest_overhead(least);
83 goto nextstage;
84 }
85 }
41ac51ee 86 ip_vs_scheduler_err(svc, "no destination available");
1da177e4
LT
87 return NULL;
88
89 /*
90 * Find the destination with the least load.
91 */
92 nextstage:
9be52aba 93 list_for_each_entry_continue_rcu(dest, &svc->destinations, n_list) {
1da177e4
LT
94 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
95 continue;
96 doh = ip_vs_sed_dest_overhead(dest);
c16526a7
SK
97 if ((__s64)loh * atomic_read(&dest->weight) >
98 (__s64)doh * atomic_read(&least->weight)) {
1da177e4
LT
99 least = dest;
100 loh = doh;
101 }
102 }
103
b14198f6
JV
104 IP_VS_DBG_BUF(6, "SED: server %s:%u "
105 "activeconns %d refcnt %d weight %d overhead %d\n",
4d316f3f
JA
106 IP_VS_DBG_ADDR(least->af, &least->addr),
107 ntohs(least->port),
b14198f6 108 atomic_read(&least->activeconns),
b54ab92b 109 refcount_read(&least->refcnt),
b14198f6 110 atomic_read(&least->weight), loh);
1da177e4
LT
111
112 return least;
113}
114
115
116static struct ip_vs_scheduler ip_vs_sed_scheduler =
117{
118 .name = "sed",
119 .refcnt = ATOMIC_INIT(0),
120 .module = THIS_MODULE,
d149ccc9 121 .n_list = LIST_HEAD_INIT(ip_vs_sed_scheduler.n_list),
1da177e4
LT
122 .schedule = ip_vs_sed_schedule,
123};
124
125
126static int __init ip_vs_sed_init(void)
127{
1da177e4
LT
128 return register_ip_vs_scheduler(&ip_vs_sed_scheduler);
129}
130
131static void __exit ip_vs_sed_cleanup(void)
132{
133 unregister_ip_vs_scheduler(&ip_vs_sed_scheduler);
ceec4c38 134 synchronize_rcu();
1da177e4
LT
135}
136
137module_init(ip_vs_sed_init);
138module_exit(ip_vs_sed_cleanup);
139MODULE_LICENSE("GPL");