1 // SPDX-License-Identifier: GPL-2.0
2 /* IPVS: Maglev Hashing scheduling module
3 *
4 * Authors: Inju Song <inju.song@navercorp.com>
5 *
6 */
7
8 /* The mh algorithm is to assign a preference list of all the lookup
9 * table positions to each destination and populate the table with
10 * the most-preferred position of destinations. Then it is to select
11 * destination with the hash key of source IP address through looking
12 * up a the lookup table.
13 *
14 * The algorithm is detailed in:
15 * [3.4 Consistent Hasing]
16 https://www.usenix.org/system/files/conference/nsdi16/nsdi16-paper-eisenbud.pdf
17 *
18 */
19
20 #define pr_fmt(fmt) "IPVS: " fmt
21
22 #include <linux/ip.h>
23 #include <linux/slab.h>
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/skbuff.h>
27
28 #include <net/ip_vs.h>
29
30 #include <linux/siphash.h>
31 #include <linux/bitops.h>
32 #include <linux/gcd.h>
33
34 #define IP_VS_SVC_F_SCHED_MH_FALLBACK IP_VS_SVC_F_SCHED1 /* MH fallback */
35 #define IP_VS_SVC_F_SCHED_MH_PORT IP_VS_SVC_F_SCHED2 /* MH use port */
36
37 struct ip_vs_mh_lookup {
38 struct ip_vs_dest __rcu *dest; /* real server (cache) */
39 };
40
41 struct ip_vs_mh_dest_setup {
42 unsigned int offset; /* starting offset */
43 unsigned int skip; /* skip */
44 unsigned int perm; /* next_offset */
45 int turns; /* weight / gcd() and rshift */
46 };
47
48 /* Available prime numbers for MH table */
49 static int primes[] = {251, 509, 1021, 2039, 4093,
50 8191, 16381, 32749, 65521, 131071};
51
52 /* For IPVS MH entry hash table */
53 #ifndef CONFIG_IP_VS_MH_TAB_INDEX
54 #define CONFIG_IP_VS_MH_TAB_INDEX 12
55 #endif
56 #define IP_VS_MH_TAB_BITS (CONFIG_IP_VS_MH_TAB_INDEX / 2)
57 #define IP_VS_MH_TAB_INDEX (CONFIG_IP_VS_MH_TAB_INDEX - 8)
58 #define IP_VS_MH_TAB_SIZE primes[IP_VS_MH_TAB_INDEX]
59
60 struct ip_vs_mh_state {
61 struct rcu_head rcu_head;
62 struct ip_vs_mh_lookup *lookup;
63 struct ip_vs_mh_dest_setup *dest_setup;
64 hsiphash_key_t hash1, hash2;
65 int gcd;
66 int rshift;
67 };
68
generate_hash_secret(hsiphash_key_t * hash1,hsiphash_key_t * hash2)69 static inline void generate_hash_secret(hsiphash_key_t *hash1,
70 hsiphash_key_t *hash2)
71 {
72 hash1->key[0] = 2654435761UL;
73 hash1->key[1] = 2654435761UL;
74
75 hash2->key[0] = 2654446892UL;
76 hash2->key[1] = 2654446892UL;
77 }
78
79 /* Helper function to determine if server is unavailable */
is_unavailable(struct ip_vs_dest * dest)80 static inline bool is_unavailable(struct ip_vs_dest *dest)
81 {
82 return atomic_read(&dest->weight) <= 0 ||
83 dest->flags & IP_VS_DEST_F_OVERLOAD;
84 }
85
86 /* Returns hash value for IPVS MH entry */
87 static inline unsigned int
ip_vs_mh_hashkey(int af,const union nf_inet_addr * addr,__be16 port,hsiphash_key_t * key,unsigned int offset)88 ip_vs_mh_hashkey(int af, const union nf_inet_addr *addr,
89 __be16 port, hsiphash_key_t *key, unsigned int offset)
90 {
91 unsigned int v;
92 __be32 addr_fold = addr->ip;
93
94 #ifdef CONFIG_IP_VS_IPV6
95 if (af == AF_INET6)
96 addr_fold = addr->ip6[0] ^ addr->ip6[1] ^
97 addr->ip6[2] ^ addr->ip6[3];
98 #endif
99 v = (offset + ntohs(port) + ntohl(addr_fold));
100 return hsiphash(&v, sizeof(v), key);
101 }
102
103 /* Reset all the hash buckets of the specified table. */
ip_vs_mh_reset(struct ip_vs_mh_state * s)104 static void ip_vs_mh_reset(struct ip_vs_mh_state *s)
105 {
106 int i;
107 struct ip_vs_mh_lookup *l;
108 struct ip_vs_dest *dest;
109
110 l = &s->lookup[0];
111 for (i = 0; i < IP_VS_MH_TAB_SIZE; i++) {
112 dest = rcu_dereference_protected(l->dest, 1);
113 if (dest) {
114 ip_vs_dest_put(dest);
115 RCU_INIT_POINTER(l->dest, NULL);
116 }
117 l++;
118 }
119 }
120
ip_vs_mh_permutate(struct ip_vs_mh_state * s,struct ip_vs_service * svc)121 static int ip_vs_mh_permutate(struct ip_vs_mh_state *s,
122 struct ip_vs_service *svc)
123 {
124 struct list_head *p;
125 struct ip_vs_mh_dest_setup *ds;
126 struct ip_vs_dest *dest;
127 int lw;
128
129 /* If gcd is smaller then 1, number of dests or
130 * all last_weight of dests are zero. So, skip
131 * permutation for the dests.
132 */
133 if (s->gcd < 1)
134 return 0;
135
136 /* Set dest_setup for the dests permutation */
137 p = &svc->destinations;
138 ds = &s->dest_setup[0];
139 while ((p = p->next) != &svc->destinations) {
140 dest = list_entry(p, struct ip_vs_dest, n_list);
141
142 ds->offset = ip_vs_mh_hashkey(svc->af, &dest->addr,
143 dest->port, &s->hash1, 0) %
144 IP_VS_MH_TAB_SIZE;
145 ds->skip = ip_vs_mh_hashkey(svc->af, &dest->addr,
146 dest->port, &s->hash2, 0) %
147 (IP_VS_MH_TAB_SIZE - 1) + 1;
148 ds->perm = ds->offset;
149
150 lw = atomic_read(&dest->last_weight);
151 ds->turns = ((lw / s->gcd) >> s->rshift) ? : (lw != 0);
152 ds++;
153 }
154
155 return 0;
156 }
157
ip_vs_mh_populate(struct ip_vs_mh_state * s,struct ip_vs_service * svc)158 static int ip_vs_mh_populate(struct ip_vs_mh_state *s,
159 struct ip_vs_service *svc)
160 {
161 int n, c, dt_count;
162 unsigned long *table;
163 struct list_head *p;
164 struct ip_vs_mh_dest_setup *ds;
165 struct ip_vs_dest *dest, *new_dest;
166
167 /* If gcd is smaller then 1, number of dests or
168 * all last_weight of dests are zero. So, skip
169 * the population for the dests and reset lookup table.
170 */
171 if (s->gcd < 1) {
172 ip_vs_mh_reset(s);
173 return 0;
174 }
175
176 table = bitmap_zalloc(IP_VS_MH_TAB_SIZE, GFP_KERNEL);
177 if (!table)
178 return -ENOMEM;
179
180 p = &svc->destinations;
181 n = 0;
182 dt_count = 0;
183 while (n < IP_VS_MH_TAB_SIZE) {
184 if (p == &svc->destinations)
185 p = p->next;
186
187 ds = &s->dest_setup[0];
188 while (p != &svc->destinations) {
189 /* Ignore added server with zero weight */
190 if (ds->turns < 1) {
191 p = p->next;
192 ds++;
193 continue;
194 }
195
196 c = ds->perm;
197 while (test_bit(c, table)) {
198 /* Add skip, mod IP_VS_MH_TAB_SIZE */
199 ds->perm += ds->skip;
200 if (ds->perm >= IP_VS_MH_TAB_SIZE)
201 ds->perm -= IP_VS_MH_TAB_SIZE;
202 c = ds->perm;
203 }
204
205 __set_bit(c, table);
206
207 dest = rcu_dereference_protected(s->lookup[c].dest, 1);
208 new_dest = list_entry(p, struct ip_vs_dest, n_list);
209 if (dest != new_dest) {
210 if (dest)
211 ip_vs_dest_put(dest);
212 ip_vs_dest_hold(new_dest);
213 RCU_INIT_POINTER(s->lookup[c].dest, new_dest);
214 }
215
216 if (++n == IP_VS_MH_TAB_SIZE)
217 goto out;
218
219 if (++dt_count >= ds->turns) {
220 dt_count = 0;
221 p = p->next;
222 ds++;
223 }
224 }
225 }
226
227 out:
228 bitmap_free(table);
229 return 0;
230 }
231
232 /* Get ip_vs_dest associated with supplied parameters. */
233 static inline struct ip_vs_dest *
ip_vs_mh_get(struct ip_vs_service * svc,struct ip_vs_mh_state * s,const union nf_inet_addr * addr,__be16 port)234 ip_vs_mh_get(struct ip_vs_service *svc, struct ip_vs_mh_state *s,
235 const union nf_inet_addr *addr, __be16 port)
236 {
237 unsigned int hash = ip_vs_mh_hashkey(svc->af, addr, port, &s->hash1, 0)
238 % IP_VS_MH_TAB_SIZE;
239 struct ip_vs_dest *dest = rcu_dereference(s->lookup[hash].dest);
240
241 return (!dest || is_unavailable(dest)) ? NULL : dest;
242 }
243
244 /* As ip_vs_mh_get, but with fallback if selected server is unavailable */
245 static inline struct ip_vs_dest *
ip_vs_mh_get_fallback(struct ip_vs_service * svc,struct ip_vs_mh_state * s,const union nf_inet_addr * addr,__be16 port)246 ip_vs_mh_get_fallback(struct ip_vs_service *svc, struct ip_vs_mh_state *s,
247 const union nf_inet_addr *addr, __be16 port)
248 {
249 unsigned int offset, roffset;
250 unsigned int hash, ihash;
251 struct ip_vs_dest *dest;
252
253 /* First try the dest it's supposed to go to */
254 ihash = ip_vs_mh_hashkey(svc->af, addr, port,
255 &s->hash1, 0) % IP_VS_MH_TAB_SIZE;
256 dest = rcu_dereference(s->lookup[ihash].dest);
257 if (!dest)
258 return NULL;
259 if (!is_unavailable(dest))
260 return dest;
261
262 IP_VS_DBG_BUF(6, "MH: selected unavailable server %s:%u, reselecting",
263 IP_VS_DBG_ADDR(dest->af, &dest->addr), ntohs(dest->port));
264
265 /* If the original dest is unavailable, loop around the table
266 * starting from ihash to find a new dest
267 */
268 for (offset = 0; offset < IP_VS_MH_TAB_SIZE; offset++) {
269 roffset = (offset + ihash) % IP_VS_MH_TAB_SIZE;
270 hash = ip_vs_mh_hashkey(svc->af, addr, port, &s->hash1,
271 roffset) % IP_VS_MH_TAB_SIZE;
272 dest = rcu_dereference(s->lookup[hash].dest);
273 if (!dest)
274 break;
275 if (!is_unavailable(dest))
276 return dest;
277 IP_VS_DBG_BUF(6,
278 "MH: selected unavailable server %s:%u (offset %u), reselecting",
279 IP_VS_DBG_ADDR(dest->af, &dest->addr),
280 ntohs(dest->port), roffset);
281 }
282
283 return NULL;
284 }
285
286 /* Assign all the hash buckets of the specified table with the service. */
ip_vs_mh_reassign(struct ip_vs_mh_state * s,struct ip_vs_service * svc)287 static int ip_vs_mh_reassign(struct ip_vs_mh_state *s,
288 struct ip_vs_service *svc)
289 {
290 int ret;
291
292 if (svc->num_dests > IP_VS_MH_TAB_SIZE)
293 return -EINVAL;
294
295 if (svc->num_dests >= 1) {
296 s->dest_setup = kzalloc_objs(struct ip_vs_mh_dest_setup,
297 svc->num_dests);
298 if (!s->dest_setup)
299 return -ENOMEM;
300 }
301
302 ip_vs_mh_permutate(s, svc);
303
304 ret = ip_vs_mh_populate(s, svc);
305 if (ret < 0)
306 goto out;
307
308 IP_VS_DBG_BUF(6, "MH: reassign lookup table of %s:%u\n",
309 IP_VS_DBG_ADDR(svc->af, &svc->addr),
310 ntohs(svc->port));
311
312 out:
313 if (svc->num_dests >= 1) {
314 kfree(s->dest_setup);
315 s->dest_setup = NULL;
316 }
317 return ret;
318 }
319
ip_vs_mh_gcd_weight(struct ip_vs_service * svc)320 static int ip_vs_mh_gcd_weight(struct ip_vs_service *svc)
321 {
322 struct ip_vs_dest *dest;
323 int weight;
324 int g = 0;
325
326 list_for_each_entry(dest, &svc->destinations, n_list) {
327 weight = atomic_read(&dest->last_weight);
328 if (weight > 0) {
329 if (g > 0)
330 g = gcd(weight, g);
331 else
332 g = weight;
333 }
334 }
335 return g;
336 }
337
338 /* To avoid assigning huge weight for the MH table,
339 * calculate shift value with gcd.
340 */
ip_vs_mh_shift_weight(struct ip_vs_service * svc,int gcd)341 static int ip_vs_mh_shift_weight(struct ip_vs_service *svc, int gcd)
342 {
343 struct ip_vs_dest *dest;
344 int new_weight, weight = 0;
345 int mw, shift;
346
347 /* If gcd is smaller then 1, number of dests or
348 * all last_weight of dests are zero. So, return
349 * shift value as zero.
350 */
351 if (gcd < 1)
352 return 0;
353
354 list_for_each_entry(dest, &svc->destinations, n_list) {
355 new_weight = atomic_read(&dest->last_weight);
356 if (new_weight > weight)
357 weight = new_weight;
358 }
359
360 /* Because gcd is greater than zero,
361 * the maximum weight and gcd are always greater than zero
362 */
363 mw = weight / gcd;
364
365 /* shift = occupied bits of weight/gcd - MH highest bits */
366 shift = fls(mw) - IP_VS_MH_TAB_BITS;
367 return (shift >= 0) ? shift : 0;
368 }
369
ip_vs_mh_state_free(struct rcu_head * head)370 static void ip_vs_mh_state_free(struct rcu_head *head)
371 {
372 struct ip_vs_mh_state *s;
373
374 s = container_of(head, struct ip_vs_mh_state, rcu_head);
375 kfree(s->lookup);
376 kfree(s);
377 }
378
ip_vs_mh_init_svc(struct ip_vs_service * svc)379 static int ip_vs_mh_init_svc(struct ip_vs_service *svc)
380 {
381 int ret;
382 struct ip_vs_mh_state *s;
383
384 /* Allocate the MH table for this service */
385 s = kzalloc_obj(*s);
386 if (!s)
387 return -ENOMEM;
388
389 s->lookup = kzalloc_objs(struct ip_vs_mh_lookup, IP_VS_MH_TAB_SIZE);
390 if (!s->lookup) {
391 kfree(s);
392 return -ENOMEM;
393 }
394
395 generate_hash_secret(&s->hash1, &s->hash2);
396 s->gcd = ip_vs_mh_gcd_weight(svc);
397 s->rshift = ip_vs_mh_shift_weight(svc, s->gcd);
398
399 IP_VS_DBG(6,
400 "MH lookup table (memory=%zdbytes) allocated for current service\n",
401 sizeof(struct ip_vs_mh_lookup) * IP_VS_MH_TAB_SIZE);
402
403 /* Assign the lookup table with current dests */
404 ret = ip_vs_mh_reassign(s, svc);
405 if (ret < 0) {
406 ip_vs_mh_reset(s);
407 ip_vs_mh_state_free(&s->rcu_head);
408 return ret;
409 }
410
411 /* No more failures, attach state */
412 svc->sched_data = s;
413 return 0;
414 }
415
ip_vs_mh_done_svc(struct ip_vs_service * svc)416 static void ip_vs_mh_done_svc(struct ip_vs_service *svc)
417 {
418 struct ip_vs_mh_state *s = svc->sched_data;
419
420 /* Got to clean up lookup entry here */
421 ip_vs_mh_reset(s);
422
423 call_rcu(&s->rcu_head, ip_vs_mh_state_free);
424 IP_VS_DBG(6, "MH lookup table (memory=%zdbytes) released\n",
425 sizeof(struct ip_vs_mh_lookup) * IP_VS_MH_TAB_SIZE);
426 }
427
ip_vs_mh_dest_changed(struct ip_vs_service * svc,struct ip_vs_dest * dest)428 static int ip_vs_mh_dest_changed(struct ip_vs_service *svc,
429 struct ip_vs_dest *dest)
430 {
431 struct ip_vs_mh_state *s = svc->sched_data;
432
433 s->gcd = ip_vs_mh_gcd_weight(svc);
434 s->rshift = ip_vs_mh_shift_weight(svc, s->gcd);
435
436 /* Assign the lookup table with the updated service */
437 return ip_vs_mh_reassign(s, svc);
438 }
439
440 /* Helper function to get port number */
441 static inline __be16
ip_vs_mh_get_port(const struct sk_buff * skb,struct ip_vs_iphdr * iph)442 ip_vs_mh_get_port(const struct sk_buff *skb, struct ip_vs_iphdr *iph)
443 {
444 __be16 _ports[2], *ports;
445
446 /* At this point we know that we have a valid packet of some kind.
447 * Because ICMP packets are only guaranteed to have the first 8
448 * bytes, let's just grab the ports. Fortunately they're in the
449 * same position for all three of the protocols we care about.
450 */
451 switch (iph->protocol) {
452 case IPPROTO_TCP:
453 case IPPROTO_UDP:
454 case IPPROTO_SCTP:
455 ports = skb_header_pointer(skb, iph->len, sizeof(_ports),
456 &_ports);
457 if (unlikely(!ports))
458 return 0;
459
460 if (likely(!ip_vs_iph_inverse(iph)))
461 return ports[0];
462 else
463 return ports[1];
464 default:
465 return 0;
466 }
467 }
468
469 /* Maglev Hashing scheduling */
470 static struct ip_vs_dest *
ip_vs_mh_schedule(struct ip_vs_service * svc,const struct sk_buff * skb,struct ip_vs_iphdr * iph)471 ip_vs_mh_schedule(struct ip_vs_service *svc, const struct sk_buff *skb,
472 struct ip_vs_iphdr *iph)
473 {
474 struct ip_vs_dest *dest;
475 struct ip_vs_mh_state *s;
476 __be16 port = 0;
477 const union nf_inet_addr *hash_addr;
478
479 hash_addr = ip_vs_iph_inverse(iph) ? &iph->daddr : &iph->saddr;
480
481 IP_VS_DBG(6, "%s : Scheduling...\n", __func__);
482
483 if (svc->flags & IP_VS_SVC_F_SCHED_MH_PORT)
484 port = ip_vs_mh_get_port(skb, iph);
485
486 s = (struct ip_vs_mh_state *)svc->sched_data;
487
488 if (svc->flags & IP_VS_SVC_F_SCHED_MH_FALLBACK)
489 dest = ip_vs_mh_get_fallback(svc, s, hash_addr, port);
490 else
491 dest = ip_vs_mh_get(svc, s, hash_addr, port);
492
493 if (!dest) {
494 ip_vs_scheduler_err(svc, "no destination available");
495 return NULL;
496 }
497
498 IP_VS_DBG_BUF(6, "MH: source IP address %s:%u --> server %s:%u\n",
499 IP_VS_DBG_ADDR(svc->af, hash_addr),
500 ntohs(port),
501 IP_VS_DBG_ADDR(dest->af, &dest->addr),
502 ntohs(dest->port));
503
504 return dest;
505 }
506
507 /* IPVS MH Scheduler structure */
508 static struct ip_vs_scheduler ip_vs_mh_scheduler = {
509 .name = "mh",
510 .refcnt = ATOMIC_INIT(0),
511 .module = THIS_MODULE,
512 .n_list = LIST_HEAD_INIT(ip_vs_mh_scheduler.n_list),
513 .init_service = ip_vs_mh_init_svc,
514 .done_service = ip_vs_mh_done_svc,
515 .add_dest = ip_vs_mh_dest_changed,
516 .del_dest = ip_vs_mh_dest_changed,
517 .upd_dest = ip_vs_mh_dest_changed,
518 .schedule = ip_vs_mh_schedule,
519 };
520
ip_vs_mh_init(void)521 static int __init ip_vs_mh_init(void)
522 {
523 return register_ip_vs_scheduler(&ip_vs_mh_scheduler);
524 }
525
ip_vs_mh_cleanup(void)526 static void __exit ip_vs_mh_cleanup(void)
527 {
528 unregister_ip_vs_scheduler(&ip_vs_mh_scheduler);
529 rcu_barrier();
530 }
531
532 module_init(ip_vs_mh_init);
533 module_exit(ip_vs_mh_cleanup);
534 MODULE_DESCRIPTION("Maglev hashing ipvs scheduler");
535 MODULE_LICENSE("GPL v2");
536 MODULE_AUTHOR("Inju Song <inju.song@navercorp.com>");
537