1 /*
2  * IPVS:        Round-Robin Scheduling module
3  *
4  * Version:     $Id: ip_vs_rr.c,v 1.8 2001/10/19 15:05:17 wensong Exp $
5  *
6  * Authors:     Wensong Zhang <wensong@linuxvirtualserver.org>
7  *              Peter Kese <peter.kese@ijs.si>
8  *
9  *              This program is free software; you can redistribute it and/or
10  *              modify it under the terms of the GNU General Public License
11  *              as published by the Free Software Foundation; either version
12  *              2 of the License, or (at your option) any later version.
13  *
14  * Fixes/Changes:
15  *     Wensong Zhang            :     changed the ip_vs_rr_schedule to return dest
16  *     Julian Anastasov         :     fixed the NULL pointer access bug in debugging
17  *     Wensong Zhang            :     changed some comestics things for debugging
18  *     Wensong Zhang            :     changed for the d-linked destination list
19  *     Wensong Zhang            :     added the ip_vs_rr_update_svc
20  *     Wensong Zhang            :     added any dest with weight=0 is quiesced
21  *
22  */
23 
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 
27 #include <net/ip_vs.h>
28 
29 
ip_vs_rr_init_svc(struct ip_vs_service * svc)30 static int ip_vs_rr_init_svc(struct ip_vs_service *svc)
31 {
32 	svc->sched_data = &svc->destinations;
33 	return 0;
34 }
35 
36 
ip_vs_rr_done_svc(struct ip_vs_service * svc)37 static int ip_vs_rr_done_svc(struct ip_vs_service *svc)
38 {
39 	return 0;
40 }
41 
42 
ip_vs_rr_update_svc(struct ip_vs_service * svc)43 static int ip_vs_rr_update_svc(struct ip_vs_service *svc)
44 {
45 	svc->sched_data = &svc->destinations;
46 	return 0;
47 }
48 
49 
50 /*
51  * Round-Robin Scheduling
52  */
53 static struct ip_vs_dest *
ip_vs_rr_schedule(struct ip_vs_service * svc,struct iphdr * iph)54 ip_vs_rr_schedule(struct ip_vs_service *svc, struct iphdr *iph)
55 {
56 	register struct list_head *p, *q;
57 	struct ip_vs_dest *dest;
58 
59 	IP_VS_DBG(6, "ip_vs_rr_schedule(): Scheduling...\n");
60 
61 	write_lock(&svc->sched_lock);
62 	p = (struct list_head *)svc->sched_data;
63 	p = p->next;
64 	q = p;
65 	do {
66 		if (q == &svc->destinations) {
67 			q = q->next;
68 			continue;
69 		}
70 		dest = list_entry(q, struct ip_vs_dest, n_list);
71 		if (atomic_read(&dest->weight) > 0)
72 			/* HIT */
73 			goto out;
74 		q = q->next;
75 	} while (q != p);
76 	write_unlock(&svc->sched_lock);
77 	return NULL;
78 
79   out:
80 	svc->sched_data = q;
81 	write_unlock(&svc->sched_lock);
82 	IP_VS_DBG(6, "RR: server %u.%u.%u.%u:%u "
83 		  "activeconns %d refcnt %d weight %d\n",
84 		  NIPQUAD(dest->addr), ntohs(dest->port),
85 		  atomic_read(&dest->activeconns),
86 		  atomic_read(&dest->refcnt), atomic_read(&dest->weight));
87 
88 	return dest;
89 }
90 
91 
92 static struct ip_vs_scheduler ip_vs_rr_scheduler = {
93 	{0},			/* n_list */
94 	"rr",			/* name */
95 	ATOMIC_INIT(0),		/* refcnt */
96 	THIS_MODULE,		/* this module */
97 	ip_vs_rr_init_svc,	/* service initializer */
98 	ip_vs_rr_done_svc,	/* service done */
99 	ip_vs_rr_update_svc,	/* service updater */
100 	ip_vs_rr_schedule,	/* select a server from the destination list */
101 };
102 
ip_vs_rr_init(void)103 static int __init ip_vs_rr_init(void)
104 {
105 	INIT_LIST_HEAD(&ip_vs_rr_scheduler.n_list);
106 	return register_ip_vs_scheduler(&ip_vs_rr_scheduler);
107 }
108 
ip_vs_rr_cleanup(void)109 static void __exit ip_vs_rr_cleanup(void)
110 {
111 	unregister_ip_vs_scheduler(&ip_vs_rr_scheduler);
112 }
113 
114 module_init(ip_vs_rr_init);
115 module_exit(ip_vs_rr_cleanup);
116 MODULE_LICENSE("GPL");
117