1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * IPVS: Locality-Based Least-Connection with Replication scheduler 4 * 5 * Authors: Wensong Zhang <wensong@gnuchina.org> 6 * 7 * Changes: 8 * Julian Anastasov : Added the missing (dest->weight>0) 9 * condition in the ip_vs_dest_set_max. 10 */ 11 12 /* 13 * The lblc/r algorithm is as follows (pseudo code): 14 * 15 * if serverSet[dest_ip] is null then 16 * n, serverSet[dest_ip] <- {weighted least-conn node}; 17 * else 18 * n <- {least-conn (alive) node in serverSet[dest_ip]}; 19 * if (n is null) OR 20 * (n.conns>n.weight AND 21 * there is a node m with m.conns<m.weight/2) then 22 * n <- {weighted least-conn node}; 23 * add n to serverSet[dest_ip]; 24 * if |serverSet[dest_ip]| > 1 AND 25 * now - serverSet[dest_ip].lastMod > T then 26 * m <- {most conn node in serverSet[dest_ip]}; 27 * remove m from serverSet[dest_ip]; 28 * if serverSet[dest_ip] changed then 29 * serverSet[dest_ip].lastMod <- now; 30 * 31 * return n; 32 * 33 */ 34 35 #define pr_fmt(fmt) "IPVS: " fmt 36 37 #include <linux/ip.h> 38 #include <linux/module.h> 39 #include <linux/kernel.h> 40 #include <linux/skbuff.h> 41 #include <linux/jiffies.h> 42 #include <linux/list.h> 43 #include <linux/slab.h> 44 #include <linux/hash.h> 45 46 /* for sysctl */ 47 #include <linux/fs.h> 48 #include <linux/sysctl.h> 49 #include <net/net_namespace.h> 50 51 #include <net/ip_vs.h> 52 53 54 /* 55 * It is for garbage collection of stale IPVS lblcr entries, 56 * when the table is full. 57 */ 58 #define CHECK_EXPIRE_INTERVAL (60*HZ) 59 #define ENTRY_TIMEOUT (6*60*HZ) 60 61 #define DEFAULT_EXPIRATION (24*60*60*HZ) 62 63 /* 64 * It is for full expiration check. 65 * When there is no partial expiration check (garbage collection) 66 * in a half hour, do a full expiration check to collect stale 67 * entries that haven't been touched for a day. 68 */ 69 #define COUNT_FOR_FULL_EXPIRATION 30 70 71 /* 72 * for IPVS lblcr entry hash table 73 */ 74 #ifndef CONFIG_IP_VS_LBLCR_TAB_BITS 75 #define CONFIG_IP_VS_LBLCR_TAB_BITS 10 76 #endif 77 #define IP_VS_LBLCR_TAB_BITS CONFIG_IP_VS_LBLCR_TAB_BITS 78 #define IP_VS_LBLCR_TAB_SIZE (1 << IP_VS_LBLCR_TAB_BITS) 79 #define IP_VS_LBLCR_TAB_MASK (IP_VS_LBLCR_TAB_SIZE - 1) 80 81 82 /* 83 * IPVS destination set structure and operations 84 */ 85 struct ip_vs_dest_set_elem { 86 struct list_head list; /* list link */ 87 struct ip_vs_dest *dest; /* destination server */ 88 struct rcu_head rcu_head; 89 }; 90 91 struct ip_vs_dest_set { 92 atomic_t size; /* set size */ 93 unsigned long lastmod; /* last modified time */ 94 struct list_head list; /* destination list */ 95 }; 96 97 98 static void ip_vs_dest_set_insert(struct ip_vs_dest_set *set, 99 struct ip_vs_dest *dest, bool check) 100 { 101 struct ip_vs_dest_set_elem *e; 102 103 if (check) { 104 list_for_each_entry(e, &set->list, list) { 105 if (e->dest == dest) 106 return; 107 } 108 } 109 110 e = kmalloc_obj(*e, GFP_ATOMIC); 111 if (e == NULL) 112 return; 113 114 ip_vs_dest_hold(dest); 115 e->dest = dest; 116 117 list_add_rcu(&e->list, &set->list); 118 atomic_inc(&set->size); 119 120 set->lastmod = jiffies; 121 } 122 123 static void ip_vs_lblcr_elem_rcu_free(struct rcu_head *head) 124 { 125 struct ip_vs_dest_set_elem *e; 126 127 e = container_of(head, struct ip_vs_dest_set_elem, rcu_head); 128 ip_vs_dest_put_and_free(e->dest); 129 kfree(e); 130 } 131 132 static void 133 ip_vs_dest_set_erase(struct ip_vs_dest_set *set, struct ip_vs_dest *dest) 134 { 135 struct ip_vs_dest_set_elem *e; 136 137 list_for_each_entry(e, &set->list, list) { 138 if (e->dest == dest) { 139 /* HIT */ 140 atomic_dec(&set->size); 141 set->lastmod = jiffies; 142 list_del_rcu(&e->list); 143 call_rcu(&e->rcu_head, ip_vs_lblcr_elem_rcu_free); 144 break; 145 } 146 } 147 } 148 149 static void ip_vs_dest_set_eraseall(struct ip_vs_dest_set *set) 150 { 151 struct ip_vs_dest_set_elem *e, *ep; 152 153 list_for_each_entry_safe(e, ep, &set->list, list) { 154 list_del_rcu(&e->list); 155 call_rcu(&e->rcu_head, ip_vs_lblcr_elem_rcu_free); 156 } 157 } 158 159 /* get weighted least-connection node in the destination set */ 160 static inline struct ip_vs_dest *ip_vs_dest_set_min(struct ip_vs_dest_set *set) 161 { 162 struct ip_vs_dest_set_elem *e; 163 struct ip_vs_dest *dest, *least; 164 int loh, doh; 165 166 /* select the first destination server, whose weight > 0 */ 167 list_for_each_entry_rcu(e, &set->list, list) { 168 least = e->dest; 169 if (least->flags & IP_VS_DEST_F_OVERLOAD) 170 continue; 171 172 if ((atomic_read(&least->weight) > 0) && 173 (least->cflags & IP_VS_DEST_CF_AVAILABLE)) { 174 loh = ip_vs_dest_conn_overhead(least); 175 goto nextstage; 176 } 177 } 178 return NULL; 179 180 /* find the destination with the weighted least load */ 181 nextstage: 182 list_for_each_entry_continue_rcu(e, &set->list, list) { 183 dest = e->dest; 184 if (dest->flags & IP_VS_DEST_F_OVERLOAD) 185 continue; 186 187 doh = ip_vs_dest_conn_overhead(dest); 188 if (((__s64)loh * atomic_read(&dest->weight) > 189 (__s64)doh * atomic_read(&least->weight)) && 190 (dest->cflags & IP_VS_DEST_CF_AVAILABLE)) { 191 least = dest; 192 loh = doh; 193 } 194 } 195 196 IP_VS_DBG_BUF(6, "%s(): server %s:%d " 197 "activeconns %d refcnt %d weight %d overhead %d\n", 198 __func__, 199 IP_VS_DBG_ADDR(least->af, &least->addr), 200 ntohs(least->port), 201 atomic_read(&least->activeconns), 202 refcount_read(&least->refcnt), 203 atomic_read(&least->weight), loh); 204 return least; 205 } 206 207 208 /* get weighted most-connection node in the destination set */ 209 static inline struct ip_vs_dest *ip_vs_dest_set_max(struct ip_vs_dest_set *set) 210 { 211 struct ip_vs_dest_set_elem *e; 212 struct ip_vs_dest *dest, *most; 213 int moh, doh; 214 215 if (set == NULL) 216 return NULL; 217 218 /* select the first destination server, whose weight > 0 */ 219 list_for_each_entry(e, &set->list, list) { 220 most = e->dest; 221 if (atomic_read(&most->weight) > 0) { 222 moh = ip_vs_dest_conn_overhead(most); 223 goto nextstage; 224 } 225 } 226 return NULL; 227 228 /* find the destination with the weighted most load */ 229 nextstage: 230 list_for_each_entry_continue(e, &set->list, list) { 231 dest = e->dest; 232 doh = ip_vs_dest_conn_overhead(dest); 233 /* moh/mw < doh/dw ==> moh*dw < doh*mw, where mw,dw>0 */ 234 if (((__s64)moh * atomic_read(&dest->weight) < 235 (__s64)doh * atomic_read(&most->weight)) 236 && (atomic_read(&dest->weight) > 0)) { 237 most = dest; 238 moh = doh; 239 } 240 } 241 242 IP_VS_DBG_BUF(6, "%s(): server %s:%d " 243 "activeconns %d refcnt %d weight %d overhead %d\n", 244 __func__, 245 IP_VS_DBG_ADDR(most->af, &most->addr), ntohs(most->port), 246 atomic_read(&most->activeconns), 247 refcount_read(&most->refcnt), 248 atomic_read(&most->weight), moh); 249 return most; 250 } 251 252 253 /* 254 * IPVS lblcr entry represents an association between destination 255 * IP address and its destination server set 256 */ 257 struct ip_vs_lblcr_entry { 258 struct hlist_node list; 259 int af; /* address family */ 260 union nf_inet_addr addr; /* destination IP address */ 261 struct ip_vs_dest_set set; /* destination server set */ 262 unsigned long lastuse; /* last used time */ 263 struct rcu_head rcu_head; 264 }; 265 266 267 /* 268 * IPVS lblcr hash table 269 */ 270 struct ip_vs_lblcr_table { 271 struct rcu_head rcu_head; 272 struct hlist_head bucket[IP_VS_LBLCR_TAB_SIZE]; /* hash bucket */ 273 atomic_t entries; /* number of entries */ 274 int max_size; /* maximum size of entries */ 275 struct timer_list periodic_timer; /* collect stale entries */ 276 struct ip_vs_service *svc; /* pointer back to service */ 277 int rover; /* rover for expire check */ 278 int counter; /* counter for no expire */ 279 bool dead; 280 }; 281 282 283 #ifdef CONFIG_SYSCTL 284 /* 285 * IPVS LBLCR sysctl table 286 */ 287 288 static struct ctl_table vs_vars_table[] = { 289 { 290 .procname = "lblcr_expiration", 291 .data = NULL, 292 .maxlen = sizeof(int), 293 .mode = 0644, 294 .proc_handler = proc_dointvec_jiffies, 295 }, 296 }; 297 #endif 298 299 static inline void ip_vs_lblcr_free(struct ip_vs_lblcr_entry *en) 300 { 301 hlist_del_rcu(&en->list); 302 ip_vs_dest_set_eraseall(&en->set); 303 kfree_rcu(en, rcu_head); 304 } 305 306 307 /* 308 * Returns hash value for IPVS LBLCR entry 309 */ 310 static inline unsigned int 311 ip_vs_lblcr_hashkey(int af, const union nf_inet_addr *addr) 312 { 313 __be32 addr_fold = addr->ip; 314 315 #ifdef CONFIG_IP_VS_IPV6 316 if (af == AF_INET6) 317 addr_fold = addr->ip6[0]^addr->ip6[1]^ 318 addr->ip6[2]^addr->ip6[3]; 319 #endif 320 return hash_32(ntohl(addr_fold), IP_VS_LBLCR_TAB_BITS); 321 } 322 323 324 /* 325 * Hash an entry in the ip_vs_lblcr_table. 326 * returns bool success. 327 */ 328 static void 329 ip_vs_lblcr_hash(struct ip_vs_lblcr_table *tbl, struct ip_vs_lblcr_entry *en) 330 { 331 unsigned int hash = ip_vs_lblcr_hashkey(en->af, &en->addr); 332 333 hlist_add_head_rcu(&en->list, &tbl->bucket[hash]); 334 atomic_inc(&tbl->entries); 335 } 336 337 338 /* Get ip_vs_lblcr_entry associated with supplied parameters. */ 339 static inline struct ip_vs_lblcr_entry * 340 ip_vs_lblcr_get(int af, struct ip_vs_lblcr_table *tbl, 341 const union nf_inet_addr *addr) 342 { 343 unsigned int hash = ip_vs_lblcr_hashkey(af, addr); 344 struct ip_vs_lblcr_entry *en; 345 346 hlist_for_each_entry_rcu(en, &tbl->bucket[hash], list) 347 if (ip_vs_addr_equal(af, &en->addr, addr)) 348 return en; 349 350 return NULL; 351 } 352 353 354 /* 355 * Create or update an ip_vs_lblcr_entry, which is a mapping of a destination 356 * IP address to a server. Called under spin lock. 357 */ 358 static inline struct ip_vs_lblcr_entry * 359 ip_vs_lblcr_new(struct ip_vs_lblcr_table *tbl, const union nf_inet_addr *daddr, 360 u16 af, struct ip_vs_dest *dest) 361 { 362 struct ip_vs_lblcr_entry *en; 363 364 en = ip_vs_lblcr_get(af, tbl, daddr); 365 if (!en) { 366 if (atomic_read(&tbl->entries) >= tbl->max_size * 3 / 2) 367 return NULL; 368 369 en = kmalloc_obj(*en, GFP_ATOMIC); 370 if (!en) 371 return NULL; 372 373 en->af = af; 374 ip_vs_addr_copy(af, &en->addr, daddr); 375 en->lastuse = jiffies; 376 377 /* initialize its dest set */ 378 atomic_set(&(en->set.size), 0); 379 INIT_LIST_HEAD(&en->set.list); 380 381 ip_vs_dest_set_insert(&en->set, dest, false); 382 383 ip_vs_lblcr_hash(tbl, en); 384 return en; 385 } 386 387 ip_vs_dest_set_insert(&en->set, dest, true); 388 389 return en; 390 } 391 392 393 /* 394 * Flush all the entries of the specified table. 395 */ 396 static void ip_vs_lblcr_flush(struct ip_vs_service *svc) 397 { 398 struct ip_vs_lblcr_table *tbl = svc->sched_data; 399 int i; 400 struct ip_vs_lblcr_entry *en; 401 struct hlist_node *next; 402 403 spin_lock_bh(&svc->sched_lock); 404 tbl->dead = true; 405 for (i = 0; i < IP_VS_LBLCR_TAB_SIZE; i++) { 406 hlist_for_each_entry_safe(en, next, &tbl->bucket[i], list) { 407 ip_vs_lblcr_free(en); 408 } 409 } 410 spin_unlock_bh(&svc->sched_lock); 411 } 412 413 static int sysctl_lblcr_expiration(struct ip_vs_service *svc) 414 { 415 #ifdef CONFIG_SYSCTL 416 return svc->ipvs->sysctl_lblcr_expiration; 417 #else 418 return DEFAULT_EXPIRATION; 419 #endif 420 } 421 422 static inline void ip_vs_lblcr_full_check(struct ip_vs_service *svc) 423 { 424 struct ip_vs_lblcr_table *tbl = svc->sched_data; 425 unsigned long now = jiffies; 426 int i, j; 427 struct ip_vs_lblcr_entry *en; 428 struct hlist_node *next; 429 430 for (i = 0, j = tbl->rover; i < IP_VS_LBLCR_TAB_SIZE; i++) { 431 j = (j + 1) & IP_VS_LBLCR_TAB_MASK; 432 433 spin_lock(&svc->sched_lock); 434 hlist_for_each_entry_safe(en, next, &tbl->bucket[j], list) { 435 if (time_after(en->lastuse + 436 sysctl_lblcr_expiration(svc), now)) 437 continue; 438 439 ip_vs_lblcr_free(en); 440 atomic_dec(&tbl->entries); 441 } 442 spin_unlock(&svc->sched_lock); 443 } 444 tbl->rover = j; 445 } 446 447 448 /* 449 * Periodical timer handler for IPVS lblcr table 450 * It is used to collect stale entries when the number of entries 451 * exceeds the maximum size of the table. 452 * 453 * Fixme: we probably need more complicated algorithm to collect 454 * entries that have not been used for a long time even 455 * if the number of entries doesn't exceed the maximum size 456 * of the table. 457 * The full expiration check is for this purpose now. 458 */ 459 static void ip_vs_lblcr_check_expire(struct timer_list *t) 460 { 461 struct ip_vs_lblcr_table *tbl = timer_container_of(tbl, t, 462 periodic_timer); 463 struct ip_vs_service *svc = tbl->svc; 464 unsigned long now = jiffies; 465 int goal; 466 int i, j; 467 struct ip_vs_lblcr_entry *en; 468 struct hlist_node *next; 469 470 if ((tbl->counter % COUNT_FOR_FULL_EXPIRATION) == 0) { 471 /* do full expiration check */ 472 ip_vs_lblcr_full_check(svc); 473 tbl->counter = 1; 474 goto out; 475 } 476 477 if (atomic_read(&tbl->entries) <= tbl->max_size) { 478 tbl->counter++; 479 goto out; 480 } 481 482 goal = (atomic_read(&tbl->entries) - tbl->max_size)*4/3; 483 if (goal > tbl->max_size/2) 484 goal = tbl->max_size/2; 485 486 for (i = 0, j = tbl->rover; i < IP_VS_LBLCR_TAB_SIZE; i++) { 487 j = (j + 1) & IP_VS_LBLCR_TAB_MASK; 488 489 spin_lock(&svc->sched_lock); 490 hlist_for_each_entry_safe(en, next, &tbl->bucket[j], list) { 491 if (time_before(now, en->lastuse+ENTRY_TIMEOUT)) 492 continue; 493 494 ip_vs_lblcr_free(en); 495 atomic_dec(&tbl->entries); 496 goal--; 497 } 498 spin_unlock(&svc->sched_lock); 499 if (goal <= 0) 500 break; 501 } 502 tbl->rover = j; 503 504 out: 505 mod_timer(&tbl->periodic_timer, jiffies+CHECK_EXPIRE_INTERVAL); 506 } 507 508 static int ip_vs_lblcr_init_svc(struct ip_vs_service *svc) 509 { 510 int i; 511 struct ip_vs_lblcr_table *tbl; 512 513 /* 514 * Allocate the ip_vs_lblcr_table for this service 515 */ 516 tbl = kmalloc_obj(*tbl); 517 if (tbl == NULL) 518 return -ENOMEM; 519 520 svc->sched_data = tbl; 521 IP_VS_DBG(6, "LBLCR hash table (memory=%zdbytes) allocated for " 522 "current service\n", sizeof(*tbl)); 523 524 /* 525 * Initialize the hash buckets 526 */ 527 for (i = 0; i < IP_VS_LBLCR_TAB_SIZE; i++) { 528 INIT_HLIST_HEAD(&tbl->bucket[i]); 529 } 530 tbl->max_size = IP_VS_LBLCR_TAB_SIZE*16; 531 tbl->rover = 0; 532 tbl->counter = 1; 533 tbl->dead = false; 534 tbl->svc = svc; 535 atomic_set(&tbl->entries, 0); 536 537 /* 538 * Hook periodic timer for garbage collection 539 */ 540 timer_setup(&tbl->periodic_timer, ip_vs_lblcr_check_expire, 0); 541 mod_timer(&tbl->periodic_timer, jiffies + CHECK_EXPIRE_INTERVAL); 542 543 return 0; 544 } 545 546 547 static void ip_vs_lblcr_done_svc(struct ip_vs_service *svc) 548 { 549 struct ip_vs_lblcr_table *tbl = svc->sched_data; 550 551 /* remove periodic timer */ 552 timer_shutdown_sync(&tbl->periodic_timer); 553 554 /* got to clean up table entries here */ 555 ip_vs_lblcr_flush(svc); 556 557 /* release the table itself */ 558 kfree_rcu(tbl, rcu_head); 559 IP_VS_DBG(6, "LBLCR hash table (memory=%zdbytes) released\n", 560 sizeof(*tbl)); 561 } 562 563 564 static inline struct ip_vs_dest * 565 __ip_vs_lblcr_schedule(struct ip_vs_service *svc) 566 { 567 struct ip_vs_dest *dest, *least; 568 int loh, doh; 569 570 /* 571 * We use the following formula to estimate the load: 572 * (dest overhead) / dest->weight 573 * 574 * Remember -- no floats in kernel mode!!! 575 * The comparison of h1*w2 > h2*w1 is equivalent to that of 576 * h1/w1 > h2/w2 577 * if every weight is larger than zero. 578 * 579 * The server with weight=0 is quiesced and will not receive any 580 * new connection. 581 */ 582 list_for_each_entry_rcu(dest, &svc->destinations, n_list) { 583 if (dest->flags & IP_VS_DEST_F_OVERLOAD) 584 continue; 585 586 if (atomic_read(&dest->weight) > 0) { 587 least = dest; 588 loh = ip_vs_dest_conn_overhead(least); 589 goto nextstage; 590 } 591 } 592 return NULL; 593 594 /* 595 * Find the destination with the least load. 596 */ 597 nextstage: 598 list_for_each_entry_continue_rcu(dest, &svc->destinations, n_list) { 599 if (dest->flags & IP_VS_DEST_F_OVERLOAD) 600 continue; 601 602 doh = ip_vs_dest_conn_overhead(dest); 603 if ((__s64)loh * atomic_read(&dest->weight) > 604 (__s64)doh * atomic_read(&least->weight)) { 605 least = dest; 606 loh = doh; 607 } 608 } 609 610 IP_VS_DBG_BUF(6, "LBLCR: server %s:%d " 611 "activeconns %d refcnt %d weight %d overhead %d\n", 612 IP_VS_DBG_ADDR(least->af, &least->addr), 613 ntohs(least->port), 614 atomic_read(&least->activeconns), 615 refcount_read(&least->refcnt), 616 atomic_read(&least->weight), loh); 617 618 return least; 619 } 620 621 622 /* 623 * If this destination server is overloaded and there is a less loaded 624 * server, then return true. 625 */ 626 static inline int 627 is_overloaded(struct ip_vs_dest *dest, struct ip_vs_service *svc) 628 { 629 if (atomic_read(&dest->activeconns) > atomic_read(&dest->weight)) { 630 struct ip_vs_dest *d; 631 632 list_for_each_entry_rcu(d, &svc->destinations, n_list) { 633 if (atomic_read(&d->activeconns)*2 634 < atomic_read(&d->weight)) { 635 return 1; 636 } 637 } 638 } 639 return 0; 640 } 641 642 643 /* 644 * Locality-Based (weighted) Least-Connection scheduling 645 */ 646 static struct ip_vs_dest * 647 ip_vs_lblcr_schedule(struct ip_vs_service *svc, const struct sk_buff *skb, 648 struct ip_vs_iphdr *iph) 649 { 650 struct ip_vs_lblcr_table *tbl = svc->sched_data; 651 struct ip_vs_dest *dest; 652 struct ip_vs_lblcr_entry *en; 653 654 IP_VS_DBG(6, "%s(): Scheduling...\n", __func__); 655 656 /* First look in our cache */ 657 en = ip_vs_lblcr_get(svc->af, tbl, &iph->daddr); 658 if (en) { 659 en->lastuse = jiffies; 660 661 /* Get the least loaded destination */ 662 dest = ip_vs_dest_set_min(&en->set); 663 664 /* More than one destination + enough time passed by, cleanup */ 665 if (atomic_read(&en->set.size) > 1 && 666 time_after(jiffies, en->set.lastmod + 667 sysctl_lblcr_expiration(svc))) { 668 spin_lock_bh(&svc->sched_lock); 669 if (atomic_read(&en->set.size) > 1) { 670 struct ip_vs_dest *m; 671 672 m = ip_vs_dest_set_max(&en->set); 673 if (m) 674 ip_vs_dest_set_erase(&en->set, m); 675 } 676 spin_unlock_bh(&svc->sched_lock); 677 } 678 679 /* If the destination is not overloaded, use it */ 680 if (dest && !is_overloaded(dest, svc)) 681 goto out; 682 683 /* The cache entry is invalid, time to schedule */ 684 dest = __ip_vs_lblcr_schedule(svc); 685 if (!dest) { 686 ip_vs_scheduler_err(svc, "no destination available"); 687 return NULL; 688 } 689 690 /* Update our cache entry */ 691 spin_lock_bh(&svc->sched_lock); 692 if (!tbl->dead) 693 ip_vs_dest_set_insert(&en->set, dest, true); 694 spin_unlock_bh(&svc->sched_lock); 695 goto out; 696 } 697 698 /* No cache entry, time to schedule */ 699 dest = __ip_vs_lblcr_schedule(svc); 700 if (!dest) { 701 IP_VS_DBG(1, "no destination available\n"); 702 return NULL; 703 } 704 705 /* If we fail to create a cache entry, we'll just use the valid dest */ 706 spin_lock_bh(&svc->sched_lock); 707 if (!tbl->dead) 708 ip_vs_lblcr_new(tbl, &iph->daddr, svc->af, dest); 709 spin_unlock_bh(&svc->sched_lock); 710 711 out: 712 IP_VS_DBG_BUF(6, "LBLCR: destination IP address %s --> server %s:%d\n", 713 IP_VS_DBG_ADDR(svc->af, &iph->daddr), 714 IP_VS_DBG_ADDR(dest->af, &dest->addr), ntohs(dest->port)); 715 716 return dest; 717 } 718 719 720 /* 721 * IPVS LBLCR Scheduler structure 722 */ 723 static struct ip_vs_scheduler ip_vs_lblcr_scheduler = 724 { 725 .name = "lblcr", 726 .refcnt = ATOMIC_INIT(0), 727 .module = THIS_MODULE, 728 .n_list = LIST_HEAD_INIT(ip_vs_lblcr_scheduler.n_list), 729 .init_service = ip_vs_lblcr_init_svc, 730 .done_service = ip_vs_lblcr_done_svc, 731 .schedule = ip_vs_lblcr_schedule, 732 }; 733 734 /* 735 * per netns init. 736 */ 737 #ifdef CONFIG_SYSCTL 738 static int __net_init __ip_vs_lblcr_init(struct net *net) 739 { 740 struct netns_ipvs *ipvs = net_ipvs(net); 741 size_t vars_table_size = ARRAY_SIZE(vs_vars_table); 742 743 if (!ipvs) 744 return -ENOENT; 745 746 if (!net_eq(net, &init_net)) { 747 ipvs->lblcr_ctl_table = kmemdup(vs_vars_table, 748 sizeof(vs_vars_table), 749 GFP_KERNEL); 750 if (ipvs->lblcr_ctl_table == NULL) 751 return -ENOMEM; 752 753 /* Don't export sysctls to unprivileged users */ 754 if (net->user_ns != &init_user_ns) 755 vars_table_size = 0; 756 } else 757 ipvs->lblcr_ctl_table = vs_vars_table; 758 ipvs->sysctl_lblcr_expiration = DEFAULT_EXPIRATION; 759 ipvs->lblcr_ctl_table[0].data = &ipvs->sysctl_lblcr_expiration; 760 761 ipvs->lblcr_ctl_header = register_net_sysctl_sz(net, "net/ipv4/vs", 762 ipvs->lblcr_ctl_table, 763 vars_table_size); 764 if (!ipvs->lblcr_ctl_header) { 765 if (!net_eq(net, &init_net)) 766 kfree(ipvs->lblcr_ctl_table); 767 return -ENOMEM; 768 } 769 770 return 0; 771 } 772 773 static void __net_exit __ip_vs_lblcr_exit(struct net *net) 774 { 775 struct netns_ipvs *ipvs = net_ipvs(net); 776 777 unregister_net_sysctl_table(ipvs->lblcr_ctl_header); 778 779 if (!net_eq(net, &init_net)) 780 kfree(ipvs->lblcr_ctl_table); 781 } 782 783 #else 784 785 static int __net_init __ip_vs_lblcr_init(struct net *net) { return 0; } 786 static void __net_exit __ip_vs_lblcr_exit(struct net *net) { } 787 788 #endif 789 790 static struct pernet_operations ip_vs_lblcr_ops = { 791 .init = __ip_vs_lblcr_init, 792 .exit = __ip_vs_lblcr_exit, 793 }; 794 795 static int __init ip_vs_lblcr_init(void) 796 { 797 int ret; 798 799 ret = register_pernet_subsys(&ip_vs_lblcr_ops); 800 if (ret) 801 return ret; 802 803 ret = register_ip_vs_scheduler(&ip_vs_lblcr_scheduler); 804 if (ret) 805 unregister_pernet_subsys(&ip_vs_lblcr_ops); 806 return ret; 807 } 808 809 static void __exit ip_vs_lblcr_cleanup(void) 810 { 811 unregister_ip_vs_scheduler(&ip_vs_lblcr_scheduler); 812 unregister_pernet_subsys(&ip_vs_lblcr_ops); 813 rcu_barrier(); 814 } 815 816 817 module_init(ip_vs_lblcr_init); 818 module_exit(ip_vs_lblcr_cleanup); 819 MODULE_LICENSE("GPL"); 820 MODULE_DESCRIPTION("ipvs locality-based least-connection with replication scheduler"); 821