| 1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
/* |
| 3 |
* IPVS: Least-Connection Scheduling module |
| 4 |
* |
| 5 |
* Authors: Wensong Zhang <[email protected]> |
| 6 |
* |
| 7 |
* Changes: |
| 8 |
* Wensong Zhang : added the ip_vs_lc_update_svc |
| 9 |
* Wensong Zhang : added any dest with weight=0 is quiesced |
| 10 |
*/ |
| 11 |
|
| 12 |
#define pr_fmt(fmt) "IPVS: " fmt |
| 13 |
|
| 14 |
#include <linux/module.h> |
| 15 |
#include <linux/kernel.h> |
| 16 |
|
| 17 |
#include <net/ip_vs.h> |
| 18 |
|
| 19 |
/* |
| 20 |
* Least Connection scheduling |
| 21 |
*/ |
| 22 |
static struct ip_vs_dest * |
| 23 |
ip_vs_lc_schedule(struct ip_vs_service *svc, const struct sk_buff *skb, |
| 24 |
struct ip_vs_iphdr *iph) |
| 25 |
{ |
| 26 |
struct ip_vs_dest *dest, *least = NULL; |
| 27 |
unsigned int loh = 0, doh; |
| 28 |
|
| 29 |
IP_VS_DBG(6, "%s(): Scheduling...\n", __func__); |
| 30 |
|
| 31 |
/* |
| 32 |
* Simply select the server with the least number of |
| 33 |
* (activeconns<<5) + inactconns |
| 34 |
* Except whose weight is equal to zero. |
| 35 |
* If the weight is equal to zero, it means that the server is |
| 36 |
* quiesced, the existing connections to the server still get |
| 37 |
* served, but no new connection is assigned to the server. |
| 38 |
*/ |
| 39 |
|
| 40 |
list_for_each_entry_rcu(dest, &svc->destinations, n_list) { |
| 41 |
if ((dest->flags & IP_VS_DEST_F_OVERLOAD) || |
| 42 |
atomic_read(&dest->weight) == 0) |
| 43 |
continue; |
| 44 |
doh = ip_vs_dest_conn_overhead(dest); |
| 45 |
if (!least || doh < loh) { |
| 46 |
least = dest; |
| 47 |
loh = doh; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
if (!least) |
| 52 |
ip_vs_scheduler_err(svc, "no destination available"); |
| 53 |
else |
| 54 |
IP_VS_DBG_BUF(6, "LC: server %s:%u activeconns %d " |
| 55 |
"inactconns %d\n", |
| 56 |
IP_VS_DBG_ADDR(least->af, &least->addr), |
| 57 |
ntohs(least->port), |
| 58 |
atomic_read(&least->activeconns), |
| 59 |
atomic_read(&least->inactconns)); |
| 60 |
|
| 61 |
return least; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
static struct ip_vs_scheduler ip_vs_lc_scheduler = { |
| 66 |
.name = "lc", |
| 67 |
.refcnt = ATOMIC_INIT(0), |
| 68 |
.module = THIS_MODULE, |
| 69 |
.n_list = LIST_HEAD_INIT(ip_vs_lc_scheduler.n_list), |
| 70 |
.schedule = ip_vs_lc_schedule, |
| 71 |
}; |
| 72 |
|
| 73 |
|
| 74 |
static int __init ip_vs_lc_init(void) |
| 75 |
{ |
| 76 |
return register_ip_vs_scheduler(&ip_vs_lc_scheduler) ; |
| 77 |
} |
| 78 |
|
| 79 |
static void __exit ip_vs_lc_cleanup(void) |
| 80 |
{ |
| 81 |
unregister_ip_vs_scheduler(&ip_vs_lc_scheduler); |
| 82 |
synchronize_rcu(); |
| 83 |
} |
| 84 |
|
| 85 |
module_init(ip_vs_lc_init); |
| 86 |
module_exit(ip_vs_lc_cleanup); |
| 87 |
MODULE_LICENSE("GPL"); |
| 88 |
MODULE_DESCRIPTION("ipvs least connection scheduler"); |