Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * IPVS: Weighted Round-Robin Scheduling module |
| 3 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * 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 : changed the ip_vs_wrr_schedule to return dest |
| 13 | * Wensong Zhang : changed some comestics things for debugging |
| 14 | * Wensong Zhang : changed for the d-linked destination list |
| 15 | * Wensong Zhang : added the ip_vs_wrr_update_svc |
| 16 | * Julian Anastasov : fixed the bug of returning destination |
| 17 | * with weight 0 when all weights are zero |
| 18 | * |
| 19 | */ |
| 20 | |
Hannes Eder | 9aada7a | 2009-07-30 14:29:44 -0700 | [diff] [blame] | 21 | #define KMSG_COMPONENT "IPVS" |
| 22 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
| 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/module.h> |
| 25 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> |
Sven Wegener | 9c1ca6e | 2008-02-05 20:00:10 -0800 | [diff] [blame] | 27 | #include <linux/net.h> |
Florian Fainelli | ae24e57 | 2009-12-22 09:42:06 +0100 | [diff] [blame] | 28 | #include <linux/gcd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | #include <net/ip_vs.h> |
| 31 | |
Julian Anastasov | 08cb2d0 | 2013-03-22 11:46:48 +0200 | [diff] [blame] | 32 | /* The WRR algorithm depends on some caclulations: |
| 33 | * - mw: maximum weight |
| 34 | * - di: weight step, greatest common divisor from all weights |
| 35 | * - cw: current required weight |
| 36 | * As result, all weights are in the [di..mw] range with a step=di. |
| 37 | * |
| 38 | * First, we start with cw = mw and select dests with weight >= cw. |
| 39 | * Then cw is reduced with di and all dests are checked again. |
| 40 | * Last pass should be with cw = di. We have mw/di passes in total: |
| 41 | * |
| 42 | * pass 1: cw = max weight |
| 43 | * pass 2: cw = max weight - di |
| 44 | * pass 3: cw = max weight - 2 * di |
| 45 | * ... |
| 46 | * last pass: cw = di |
| 47 | * |
| 48 | * Weights are supposed to be >= di but we run in parallel with |
| 49 | * weight changes, it is possible some dest weight to be reduced |
| 50 | * below di, bad if it is the only available dest. |
| 51 | * |
| 52 | * So, we modify how mw is calculated, now it is reduced with (di - 1), |
| 53 | * so that last cw is 1 to catch such dests with weight below di: |
| 54 | * pass 1: cw = max weight - (di - 1) |
| 55 | * pass 2: cw = max weight - di - (di - 1) |
| 56 | * pass 3: cw = max weight - 2 * di - (di - 1) |
| 57 | * ... |
| 58 | * last pass: cw = 1 |
| 59 | * |
| 60 | */ |
| 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | /* |
| 63 | * current destination pointer for weighted round-robin scheduling |
| 64 | */ |
| 65 | struct ip_vs_wrr_mark { |
Julian Anastasov | 08cb2d0 | 2013-03-22 11:46:48 +0200 | [diff] [blame] | 66 | struct ip_vs_dest *cl; /* current dest or head */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | int cw; /* current weight */ |
| 68 | int mw; /* maximum weight */ |
| 69 | int di; /* decreasing interval */ |
Julian Anastasov | 08cb2d0 | 2013-03-22 11:46:48 +0200 | [diff] [blame] | 70 | struct rcu_head rcu_head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | static int ip_vs_wrr_gcd_weight(struct ip_vs_service *svc) |
| 75 | { |
| 76 | struct ip_vs_dest *dest; |
| 77 | int weight; |
| 78 | int g = 0; |
| 79 | |
| 80 | list_for_each_entry(dest, &svc->destinations, n_list) { |
| 81 | weight = atomic_read(&dest->weight); |
| 82 | if (weight > 0) { |
| 83 | if (g > 0) |
| 84 | g = gcd(weight, g); |
| 85 | else |
| 86 | g = weight; |
| 87 | } |
| 88 | } |
| 89 | return g ? g : 1; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | /* |
| 94 | * Get the maximum weight of the service destinations. |
| 95 | */ |
| 96 | static int ip_vs_wrr_max_weight(struct ip_vs_service *svc) |
| 97 | { |
| 98 | struct ip_vs_dest *dest; |
Simon Horman | 1e66daf | 2009-08-31 14:18:48 +0200 | [diff] [blame] | 99 | int new_weight, weight = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
| 101 | list_for_each_entry(dest, &svc->destinations, n_list) { |
Simon Horman | 1e66daf | 2009-08-31 14:18:48 +0200 | [diff] [blame] | 102 | new_weight = atomic_read(&dest->weight); |
| 103 | if (new_weight > weight) |
| 104 | weight = new_weight; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | return weight; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | static int ip_vs_wrr_init_svc(struct ip_vs_service *svc) |
| 112 | { |
| 113 | struct ip_vs_wrr_mark *mark; |
| 114 | |
| 115 | /* |
| 116 | * Allocate the mark variable for WRR scheduling |
| 117 | */ |
Julian Anastasov | 4f2a94d | 2012-04-13 16:49:42 +0300 | [diff] [blame] | 118 | mark = kmalloc(sizeof(struct ip_vs_wrr_mark), GFP_KERNEL); |
Joe Perches | 0a9ee81 | 2011-08-29 14:17:25 -0700 | [diff] [blame] | 119 | if (mark == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | return -ENOMEM; |
Joe Perches | 0a9ee81 | 2011-08-29 14:17:25 -0700 | [diff] [blame] | 121 | |
Julian Anastasov | 08cb2d0 | 2013-03-22 11:46:48 +0200 | [diff] [blame] | 122 | mark->cl = list_entry(&svc->destinations, struct ip_vs_dest, n_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | mark->di = ip_vs_wrr_gcd_weight(svc); |
Julian Anastasov | 08cb2d0 | 2013-03-22 11:46:48 +0200 | [diff] [blame] | 124 | mark->mw = ip_vs_wrr_max_weight(svc) - (mark->di - 1); |
| 125 | mark->cw = mark->mw; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | svc->sched_data = mark; |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | |
Julian Anastasov | ed3ffc4 | 2013-03-22 11:46:50 +0200 | [diff] [blame] | 132 | static void ip_vs_wrr_done_svc(struct ip_vs_service *svc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | { |
Julian Anastasov | 08cb2d0 | 2013-03-22 11:46:48 +0200 | [diff] [blame] | 134 | struct ip_vs_wrr_mark *mark = svc->sched_data; |
| 135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | /* |
| 137 | * Release the mark variable |
| 138 | */ |
Julian Anastasov | 08cb2d0 | 2013-03-22 11:46:48 +0200 | [diff] [blame] | 139 | kfree_rcu(mark, rcu_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | |
Julian Anastasov | 08cb2d0 | 2013-03-22 11:46:48 +0200 | [diff] [blame] | 143 | static int ip_vs_wrr_dest_changed(struct ip_vs_service *svc, |
| 144 | struct ip_vs_dest *dest) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | { |
| 146 | struct ip_vs_wrr_mark *mark = svc->sched_data; |
| 147 | |
Julian Anastasov | ba3a3ce | 2013-03-22 11:46:51 +0200 | [diff] [blame] | 148 | spin_lock_bh(&svc->sched_lock); |
Julian Anastasov | 08cb2d0 | 2013-03-22 11:46:48 +0200 | [diff] [blame] | 149 | mark->cl = list_entry(&svc->destinations, struct ip_vs_dest, n_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | mark->di = ip_vs_wrr_gcd_weight(svc); |
Julian Anastasov | 08cb2d0 | 2013-03-22 11:46:48 +0200 | [diff] [blame] | 151 | mark->mw = ip_vs_wrr_max_weight(svc) - (mark->di - 1); |
| 152 | if (mark->cw > mark->mw || !mark->cw) |
| 153 | mark->cw = mark->mw; |
| 154 | else if (mark->di > 1) |
| 155 | mark->cw = (mark->cw / mark->di) * mark->di + 1; |
Julian Anastasov | ba3a3ce | 2013-03-22 11:46:51 +0200 | [diff] [blame] | 156 | spin_unlock_bh(&svc->sched_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | |
| 161 | /* |
| 162 | * Weighted Round-Robin Scheduling |
| 163 | */ |
| 164 | static struct ip_vs_dest * |
Julian Anastasov | bba54de | 2013-06-16 09:09:36 +0300 | [diff] [blame] | 165 | ip_vs_wrr_schedule(struct ip_vs_service *svc, const struct sk_buff *skb, |
| 166 | struct ip_vs_iphdr *iph) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | { |
Julian Anastasov | 08cb2d0 | 2013-03-22 11:46:48 +0200 | [diff] [blame] | 168 | struct ip_vs_dest *dest, *last, *stop = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | struct ip_vs_wrr_mark *mark = svc->sched_data; |
Julian Anastasov | 08cb2d0 | 2013-03-22 11:46:48 +0200 | [diff] [blame] | 170 | bool last_pass = false, restarted = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 172 | IP_VS_DBG(6, "%s(): Scheduling...\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
Julian Anastasov | ac69269 | 2013-03-22 11:46:54 +0200 | [diff] [blame] | 174 | spin_lock_bh(&svc->sched_lock); |
Julian Anastasov | 08cb2d0 | 2013-03-22 11:46:48 +0200 | [diff] [blame] | 175 | dest = mark->cl; |
| 176 | /* No available dests? */ |
| 177 | if (mark->mw == 0) |
| 178 | goto err_noavail; |
| 179 | last = dest; |
| 180 | /* Stop only after all dests were checked for weight >= 1 (last pass) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | while (1) { |
Julian Anastasov | 08cb2d0 | 2013-03-22 11:46:48 +0200 | [diff] [blame] | 182 | list_for_each_entry_continue_rcu(dest, |
| 183 | &svc->destinations, |
| 184 | n_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | if (!(dest->flags & IP_VS_DEST_F_OVERLOAD) && |
Julian Anastasov | 08cb2d0 | 2013-03-22 11:46:48 +0200 | [diff] [blame] | 186 | atomic_read(&dest->weight) >= mark->cw) |
| 187 | goto found; |
| 188 | if (dest == stop) |
| 189 | goto err_over; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | } |
Julian Anastasov | 08cb2d0 | 2013-03-22 11:46:48 +0200 | [diff] [blame] | 191 | mark->cw -= mark->di; |
| 192 | if (mark->cw <= 0) { |
| 193 | mark->cw = mark->mw; |
| 194 | /* Stop if we tried last pass from first dest: |
| 195 | * 1. last_pass: we started checks when cw > di but |
| 196 | * then all dests were checked for w >= 1 |
| 197 | * 2. last was head: the first and only traversal |
| 198 | * was for weight >= 1, for all dests. |
| 199 | */ |
| 200 | if (last_pass || |
| 201 | &last->n_list == &svc->destinations) |
| 202 | goto err_over; |
| 203 | restarted = true; |
| 204 | } |
| 205 | last_pass = mark->cw <= mark->di; |
| 206 | if (last_pass && restarted && |
| 207 | &last->n_list != &svc->destinations) { |
| 208 | /* First traversal was for w >= 1 but only |
| 209 | * for dests after 'last', now do the same |
| 210 | * for all dests up to 'last'. |
| 211 | */ |
| 212 | stop = last; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
Julian Anastasov | 08cb2d0 | 2013-03-22 11:46:48 +0200 | [diff] [blame] | 216 | found: |
Julius Volz | b14198f | 2008-09-02 15:55:39 +0200 | [diff] [blame] | 217 | IP_VS_DBG_BUF(6, "WRR: server %s:%u " |
| 218 | "activeconns %d refcnt %d weight %d\n", |
Julian Anastasov | 4d316f3 | 2014-09-17 00:09:00 +0300 | [diff] [blame] | 219 | IP_VS_DBG_ADDR(dest->af, &dest->addr), ntohs(dest->port), |
Julius Volz | b14198f | 2008-09-02 15:55:39 +0200 | [diff] [blame] | 220 | atomic_read(&dest->activeconns), |
| 221 | atomic_read(&dest->refcnt), |
| 222 | atomic_read(&dest->weight)); |
Julian Anastasov | 08cb2d0 | 2013-03-22 11:46:48 +0200 | [diff] [blame] | 223 | mark->cl = dest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
| 225 | out: |
Julian Anastasov | ac69269 | 2013-03-22 11:46:54 +0200 | [diff] [blame] | 226 | spin_unlock_bh(&svc->sched_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | return dest; |
Julian Anastasov | 08cb2d0 | 2013-03-22 11:46:48 +0200 | [diff] [blame] | 228 | |
| 229 | err_noavail: |
| 230 | mark->cl = dest; |
| 231 | dest = NULL; |
| 232 | ip_vs_scheduler_err(svc, "no destination available"); |
| 233 | goto out; |
| 234 | |
| 235 | err_over: |
| 236 | mark->cl = dest; |
| 237 | dest = NULL; |
| 238 | ip_vs_scheduler_err(svc, "no destination available: " |
| 239 | "all destinations are overloaded"); |
| 240 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | |
| 244 | static struct ip_vs_scheduler ip_vs_wrr_scheduler = { |
| 245 | .name = "wrr", |
| 246 | .refcnt = ATOMIC_INIT(0), |
| 247 | .module = THIS_MODULE, |
Sven Wegener | d149ccc | 2008-08-10 09:18:02 +0000 | [diff] [blame] | 248 | .n_list = LIST_HEAD_INIT(ip_vs_wrr_scheduler.n_list), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | .init_service = ip_vs_wrr_init_svc, |
| 250 | .done_service = ip_vs_wrr_done_svc, |
Julian Anastasov | 08cb2d0 | 2013-03-22 11:46:48 +0200 | [diff] [blame] | 251 | .add_dest = ip_vs_wrr_dest_changed, |
| 252 | .del_dest = ip_vs_wrr_dest_changed, |
| 253 | .upd_dest = ip_vs_wrr_dest_changed, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | .schedule = ip_vs_wrr_schedule, |
| 255 | }; |
| 256 | |
| 257 | static int __init ip_vs_wrr_init(void) |
| 258 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | return register_ip_vs_scheduler(&ip_vs_wrr_scheduler) ; |
| 260 | } |
| 261 | |
| 262 | static void __exit ip_vs_wrr_cleanup(void) |
| 263 | { |
| 264 | unregister_ip_vs_scheduler(&ip_vs_wrr_scheduler); |
Julian Anastasov | ceec4c3 | 2013-03-22 11:46:53 +0200 | [diff] [blame] | 265 | synchronize_rcu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | module_init(ip_vs_wrr_init); |
| 269 | module_exit(ip_vs_wrr_cleanup); |
| 270 | MODULE_LICENSE("GPL"); |