1 /*
2 * IPVS An implementation of the IP virtual server support for the
3 * LINUX operating system. IPVS is now implemented as a module
4 * over the Netfilter framework. IPVS can be used to build a
5 * high-performance and highly available server based on a
6 * cluster of servers.
7 *
8 * Version: $Id: ip_vs_sched.c,v 1.11 2001/11/04 08:58:43 wensong Exp $
9 *
10 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
11 * Peter Kese <peter.kese@ijs.si>
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version
16 * 2 of the License, or (at your option) any later version.
17 *
18 * Changes:
19 *
20 */
21
22 #include <linux/module.h>
23 #include <linux/sched.h>
24 #include <linux/spinlock.h>
25 #include <asm/softirq.h> /* for local_bh_* */
26 #include <asm/string.h>
27 #include <linux/kmod.h>
28
29 #include <net/ip_vs.h>
30
31 /*
32 * IPVS scheduler list
33 */
34 static LIST_HEAD(ip_vs_schedulers);
35
36 /* lock for service table */
37 static rwlock_t __ip_vs_sched_lock = RW_LOCK_UNLOCKED;
38
39
40 /*
41 * Bind a service with a scheduler
42 */
ip_vs_bind_scheduler(struct ip_vs_service * svc,struct ip_vs_scheduler * scheduler)43 int ip_vs_bind_scheduler(struct ip_vs_service *svc,
44 struct ip_vs_scheduler *scheduler)
45 {
46 int ret;
47
48 if (svc == NULL) {
49 IP_VS_ERR("ip_vs_bind_scheduler(): svc arg NULL\n");
50 return -EINVAL;
51 }
52 if (scheduler == NULL) {
53 IP_VS_ERR("ip_vs_bind_scheduler(): scheduler arg NULL\n");
54 return -EINVAL;
55 }
56
57 svc->scheduler = scheduler;
58
59 if (scheduler->init_service) {
60 ret = scheduler->init_service(svc);
61 if (ret) {
62 IP_VS_ERR("ip_vs_bind_scheduler(): init error\n");
63 return ret;
64 }
65 }
66
67 return 0;
68 }
69
70
71 /*
72 * Unbind a service with its scheduler
73 */
ip_vs_unbind_scheduler(struct ip_vs_service * svc)74 int ip_vs_unbind_scheduler(struct ip_vs_service *svc)
75 {
76 struct ip_vs_scheduler *sched;
77
78 if (svc == NULL) {
79 IP_VS_ERR("ip_vs_unbind_scheduler(): svc arg NULL\n");
80 return -EINVAL;
81 }
82
83 sched = svc->scheduler;
84 if (sched == NULL) {
85 IP_VS_ERR("ip_vs_unbind_scheduler(): svc isn't bound\n");
86 return -EINVAL;
87 }
88
89 if (sched->done_service) {
90 if (sched->done_service(svc) != 0) {
91 IP_VS_ERR("ip_vs_unbind_scheduler(): done error\n");
92 return -EINVAL;
93 }
94 }
95
96 svc->scheduler = NULL;
97 return 0;
98 }
99
100
101 /*
102 * Get scheduler in the scheduler list by name
103 */
ip_vs_sched_getbyname(const char * sched_name)104 static struct ip_vs_scheduler *ip_vs_sched_getbyname(const char *sched_name)
105 {
106 struct ip_vs_scheduler *sched;
107 struct list_head *l, *e;
108
109 IP_VS_DBG(2, "ip_vs_sched_getbyname(): sched_name \"%s\"\n",
110 sched_name);
111
112 l = &ip_vs_schedulers;
113
114 read_lock_bh(&__ip_vs_sched_lock);
115
116 for (e=l->next; e!=l; e=e->next) {
117 sched = list_entry(e, struct ip_vs_scheduler, n_list);
118
119 /*
120 * Test and MOD_INC_USE_COUNT atomically
121 */
122 if (sched->module && !try_inc_mod_count(sched->module)) {
123 /*
124 * This scheduler is just deleted
125 */
126 continue;
127 }
128 if (strcmp(sched_name, sched->name)==0) {
129 /* HIT */
130 read_unlock_bh(&__ip_vs_sched_lock);
131 return sched;
132 }
133 if (sched->module)
134 __MOD_DEC_USE_COUNT(sched->module);
135 }
136
137 read_unlock_bh(&__ip_vs_sched_lock);
138 return NULL;
139 }
140
141
142 /*
143 * Lookup scheduler and try to load it if it doesn't exist
144 */
ip_vs_scheduler_get(const char * sched_name)145 struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name)
146 {
147 struct ip_vs_scheduler *sched;
148
149 /*
150 * Search for the scheduler by sched_name
151 */
152 sched = ip_vs_sched_getbyname(sched_name);
153
154 /*
155 * If scheduler not found, load the module and search again
156 */
157 if (sched == NULL) {
158 char module_name[IP_VS_SCHEDNAME_MAXLEN+8];
159 snprintf(module_name, sizeof(module_name), "ip_vs_%s", sched_name);
160 request_module(module_name);
161 sched = ip_vs_sched_getbyname(sched_name);
162 }
163
164 return sched;
165 }
166
ip_vs_scheduler_put(struct ip_vs_scheduler * scheduler)167 void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler)
168 {
169 if (scheduler->module)
170 __MOD_DEC_USE_COUNT(scheduler->module);
171 }
172
173
174 /*
175 * Register a scheduler in the scheduler list
176 */
register_ip_vs_scheduler(struct ip_vs_scheduler * scheduler)177 int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler)
178 {
179 struct ip_vs_scheduler *sched;
180
181 if (!scheduler) {
182 IP_VS_ERR("register_ip_vs_scheduler(): NULL arg\n");
183 return -EINVAL;
184 }
185
186 if (!scheduler->name) {
187 IP_VS_ERR("register_ip_vs_scheduler(): NULL scheduler_name\n");
188 return -EINVAL;
189 }
190
191 MOD_INC_USE_COUNT;
192
193 /*
194 * Make sure that the scheduler with this name doesn't exist
195 * in the scheduler list.
196 */
197 sched = ip_vs_sched_getbyname(scheduler->name);
198 if (sched) {
199 ip_vs_scheduler_put(sched);
200 MOD_DEC_USE_COUNT;
201 IP_VS_ERR("register_ip_vs_scheduler(): [%s] scheduler "
202 "already existed in the system\n", scheduler->name);
203 return -EINVAL;
204 }
205
206 write_lock_bh(&__ip_vs_sched_lock);
207
208 if (scheduler->n_list.next != &scheduler->n_list) {
209 write_unlock_bh(&__ip_vs_sched_lock);
210 MOD_DEC_USE_COUNT;
211 IP_VS_ERR("register_ip_vs_scheduler(): [%s] scheduler "
212 "already linked\n", scheduler->name);
213 return -EINVAL;
214 }
215
216 /*
217 * Add it into the d-linked scheduler list
218 */
219 list_add(&scheduler->n_list, &ip_vs_schedulers);
220 write_unlock_bh(&__ip_vs_sched_lock);
221
222 IP_VS_INFO("[%s] scheduler registered.\n", scheduler->name);
223
224 return 0;
225 }
226
227
228 /*
229 * Unregister a scheduler from the scheduler list
230 */
unregister_ip_vs_scheduler(struct ip_vs_scheduler * scheduler)231 int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler)
232 {
233 if (!scheduler) {
234 IP_VS_ERR( "unregister_ip_vs_scheduler(): NULL arg\n");
235 return -EINVAL;
236 }
237
238 write_lock_bh(&__ip_vs_sched_lock);
239 if (scheduler->n_list.next == &scheduler->n_list) {
240 write_unlock_bh(&__ip_vs_sched_lock);
241 IP_VS_ERR("unregister_ip_vs_scheduler(): [%s] scheduler "
242 "is not in the list. failed\n", scheduler->name);
243 return -EINVAL;
244 }
245
246 /*
247 * Remove it from the d-linked scheduler list
248 */
249 list_del(&scheduler->n_list);
250 write_unlock_bh(&__ip_vs_sched_lock);
251
252 MOD_DEC_USE_COUNT;
253
254 IP_VS_INFO("[%s] scheduler unregistered.\n", scheduler->name);
255
256 return 0;
257 }
258