1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * IPVS:        Overflow-Connection Scheduling module
4  *
5  * Authors:     Raducu Deaconu <rhadoo_io@yahoo.com>
6  *
7  * Scheduler implements "overflow" loadbalancing according to number of active
8  * connections , will keep all connections to the node with the highest weight
9  * and overflow to the next node if the number of connections exceeds the node's
10  * weight.
11  * Note that this scheduler might not be suitable for UDP because it only uses
12  * active connections
13  */
14 
15 #define KMSG_COMPONENT "IPVS"
16 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
17 
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 
21 #include <net/ip_vs.h>
22 
23 /* OVF Connection scheduling  */
24 static struct ip_vs_dest *
ip_vs_ovf_schedule(struct ip_vs_service * svc,const struct sk_buff * skb,struct ip_vs_iphdr * iph)25 ip_vs_ovf_schedule(struct ip_vs_service *svc, const struct sk_buff *skb,
26 		   struct ip_vs_iphdr *iph)
27 {
28 	struct ip_vs_dest *dest, *h = NULL;
29 	int hw = 0, w;
30 
31 	IP_VS_DBG(6, "ip_vs_ovf_schedule(): Scheduling...\n");
32 	/* select the node with highest weight, go to next in line if active
33 	* connections exceed weight
34 	*/
35 	list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
36 		w = atomic_read(&dest->weight);
37 		if ((dest->flags & IP_VS_DEST_F_OVERLOAD) ||
38 		    atomic_read(&dest->activeconns) > w ||
39 		    w == 0)
40 			continue;
41 		if (!h || w > hw) {
42 			h = dest;
43 			hw = w;
44 		}
45 	}
46 
47 	if (h) {
48 		IP_VS_DBG_BUF(6, "OVF: server %s:%u active %d w %d\n",
49 			      IP_VS_DBG_ADDR(h->af, &h->addr),
50 			      ntohs(h->port),
51 			      atomic_read(&h->activeconns),
52 			      atomic_read(&h->weight));
53 		return h;
54 	}
55 
56 	ip_vs_scheduler_err(svc, "no destination available");
57 	return NULL;
58 }
59 
60 static struct ip_vs_scheduler ip_vs_ovf_scheduler = {
61 	.name =			"ovf",
62 	.refcnt =		ATOMIC_INIT(0),
63 	.module =		THIS_MODULE,
64 	.n_list =		LIST_HEAD_INIT(ip_vs_ovf_scheduler.n_list),
65 	.schedule =		ip_vs_ovf_schedule,
66 };
67 
ip_vs_ovf_init(void)68 static int __init ip_vs_ovf_init(void)
69 {
70 	return register_ip_vs_scheduler(&ip_vs_ovf_scheduler);
71 }
72 
ip_vs_ovf_cleanup(void)73 static void __exit ip_vs_ovf_cleanup(void)
74 {
75 	unregister_ip_vs_scheduler(&ip_vs_ovf_scheduler);
76 	synchronize_rcu();
77 }
78 
79 module_init(ip_vs_ovf_init);
80 module_exit(ip_vs_ovf_cleanup);
81 MODULE_LICENSE("GPL");
82