1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * IPVS: Weighted Round-Robin Scheduling module
5 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
8 * Wensong Zhang : changed the ip_vs_wrr_schedule to return dest
9 * Wensong Zhang : changed some comestics things for debugging
10 * Wensong Zhang : changed for the d-linked destination list
11 * Wensong Zhang : added the ip_vs_wrr_update_svc
12 * Julian Anastasov : fixed the bug of returning destination
13 * with weight 0 when all weights are zero
16 #define KMSG_COMPONENT "IPVS"
17 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/slab.h>
22 #include <linux/net.h>
23 #include <linux/gcd.h>
25 #include <net/ip_vs.h>
27 /* The WRR algorithm depends on some caclulations:
28 * - mw: maximum weight
29 * - di: weight step, greatest common divisor from all weights
30 * - cw: current required weight
31 * As result, all weights are in the [di..mw] range with a step=di.
33 * First, we start with cw = mw and select dests with weight >= cw.
34 * Then cw is reduced with di and all dests are checked again.
35 * Last pass should be with cw = di. We have mw/di passes in total:
37 * pass 1: cw = max weight
38 * pass 2: cw = max weight - di
39 * pass 3: cw = max weight - 2 * di
43 * Weights are supposed to be >= di but we run in parallel with
44 * weight changes, it is possible some dest weight to be reduced
45 * below di, bad if it is the only available dest.
47 * So, we modify how mw is calculated, now it is reduced with (di - 1),
48 * so that last cw is 1 to catch such dests with weight below di:
49 * pass 1: cw = max weight - (di - 1)
50 * pass 2: cw = max weight - di - (di - 1)
51 * pass 3: cw = max weight - 2 * di - (di - 1)
58 * current destination pointer for weighted round-robin scheduling
60 struct ip_vs_wrr_mark
{
61 struct ip_vs_dest
*cl
; /* current dest or head */
62 int cw
; /* current weight */
63 int mw
; /* maximum weight */
64 int di
; /* decreasing interval */
65 struct rcu_head rcu_head
;
69 static int ip_vs_wrr_gcd_weight(struct ip_vs_service
*svc
)
71 struct ip_vs_dest
*dest
;
75 list_for_each_entry(dest
, &svc
->destinations
, n_list
) {
76 weight
= atomic_read(&dest
->weight
);
89 * Get the maximum weight of the service destinations.
91 static int ip_vs_wrr_max_weight(struct ip_vs_service
*svc
)
93 struct ip_vs_dest
*dest
;
94 int new_weight
, weight
= 0;
96 list_for_each_entry(dest
, &svc
->destinations
, n_list
) {
97 new_weight
= atomic_read(&dest
->weight
);
98 if (new_weight
> weight
)
106 static int ip_vs_wrr_init_svc(struct ip_vs_service
*svc
)
108 struct ip_vs_wrr_mark
*mark
;
111 * Allocate the mark variable for WRR scheduling
113 mark
= kmalloc(sizeof(struct ip_vs_wrr_mark
), GFP_KERNEL
);
117 mark
->cl
= list_entry(&svc
->destinations
, struct ip_vs_dest
, n_list
);
118 mark
->di
= ip_vs_wrr_gcd_weight(svc
);
119 mark
->mw
= ip_vs_wrr_max_weight(svc
) - (mark
->di
- 1);
121 svc
->sched_data
= mark
;
127 static void ip_vs_wrr_done_svc(struct ip_vs_service
*svc
)
129 struct ip_vs_wrr_mark
*mark
= svc
->sched_data
;
132 * Release the mark variable
134 kfree_rcu(mark
, rcu_head
);
138 static int ip_vs_wrr_dest_changed(struct ip_vs_service
*svc
,
139 struct ip_vs_dest
*dest
)
141 struct ip_vs_wrr_mark
*mark
= svc
->sched_data
;
143 spin_lock_bh(&svc
->sched_lock
);
144 mark
->cl
= list_entry(&svc
->destinations
, struct ip_vs_dest
, n_list
);
145 mark
->di
= ip_vs_wrr_gcd_weight(svc
);
146 mark
->mw
= ip_vs_wrr_max_weight(svc
) - (mark
->di
- 1);
147 if (mark
->cw
> mark
->mw
|| !mark
->cw
)
149 else if (mark
->di
> 1)
150 mark
->cw
= (mark
->cw
/ mark
->di
) * mark
->di
+ 1;
151 spin_unlock_bh(&svc
->sched_lock
);
157 * Weighted Round-Robin Scheduling
159 static struct ip_vs_dest
*
160 ip_vs_wrr_schedule(struct ip_vs_service
*svc
, const struct sk_buff
*skb
,
161 struct ip_vs_iphdr
*iph
)
163 struct ip_vs_dest
*dest
, *last
, *stop
= NULL
;
164 struct ip_vs_wrr_mark
*mark
= svc
->sched_data
;
165 bool last_pass
= false, restarted
= false;
167 IP_VS_DBG(6, "%s(): Scheduling...\n", __func__
);
169 spin_lock_bh(&svc
->sched_lock
);
171 /* No available dests? */
175 /* Stop only after all dests were checked for weight >= 1 (last pass) */
177 list_for_each_entry_continue_rcu(dest
,
180 if (!(dest
->flags
& IP_VS_DEST_F_OVERLOAD
) &&
181 atomic_read(&dest
->weight
) >= mark
->cw
)
186 mark
->cw
-= mark
->di
;
189 /* Stop if we tried last pass from first dest:
190 * 1. last_pass: we started checks when cw > di but
191 * then all dests were checked for w >= 1
192 * 2. last was head: the first and only traversal
193 * was for weight >= 1, for all dests.
196 &last
->n_list
== &svc
->destinations
)
200 last_pass
= mark
->cw
<= mark
->di
;
201 if (last_pass
&& restarted
&&
202 &last
->n_list
!= &svc
->destinations
) {
203 /* First traversal was for w >= 1 but only
204 * for dests after 'last', now do the same
205 * for all dests up to 'last'.
212 IP_VS_DBG_BUF(6, "WRR: server %s:%u "
213 "activeconns %d refcnt %d weight %d\n",
214 IP_VS_DBG_ADDR(dest
->af
, &dest
->addr
), ntohs(dest
->port
),
215 atomic_read(&dest
->activeconns
),
216 refcount_read(&dest
->refcnt
),
217 atomic_read(&dest
->weight
));
221 spin_unlock_bh(&svc
->sched_lock
);
227 ip_vs_scheduler_err(svc
, "no destination available");
233 ip_vs_scheduler_err(svc
, "no destination available: "
234 "all destinations are overloaded");
239 static struct ip_vs_scheduler ip_vs_wrr_scheduler
= {
241 .refcnt
= ATOMIC_INIT(0),
242 .module
= THIS_MODULE
,
243 .n_list
= LIST_HEAD_INIT(ip_vs_wrr_scheduler
.n_list
),
244 .init_service
= ip_vs_wrr_init_svc
,
245 .done_service
= ip_vs_wrr_done_svc
,
246 .add_dest
= ip_vs_wrr_dest_changed
,
247 .del_dest
= ip_vs_wrr_dest_changed
,
248 .upd_dest
= ip_vs_wrr_dest_changed
,
249 .schedule
= ip_vs_wrr_schedule
,
252 static int __init
ip_vs_wrr_init(void)
254 return register_ip_vs_scheduler(&ip_vs_wrr_scheduler
) ;
257 static void __exit
ip_vs_wrr_cleanup(void)
259 unregister_ip_vs_scheduler(&ip_vs_wrr_scheduler
);
263 module_init(ip_vs_wrr_init
);
264 module_exit(ip_vs_wrr_cleanup
);
265 MODULE_LICENSE("GPL");