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
8 * Version: $Id: ip_vs_sched.c,v 1.13 2003/05/10 03:05:23 wensong Exp $
10 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
11 * Peter Kese <peter.kese@ijs.si>
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.
22 #include <linux/module.h>
23 #include <linux/spinlock.h>
24 #include <linux/interrupt.h>
25 #include <asm/string.h>
26 #include <linux/kmod.h>
27 #include <linux/sysctl.h>
29 #include <net/ip_vs.h>
34 static LIST_HEAD(ip_vs_schedulers);
36 /* lock for service table */
37 static DEFINE_RWLOCK(__ip_vs_sched_lock);
41 * Bind a service with a scheduler
43 int ip_vs_bind_scheduler(struct ip_vs_service *svc,
44 struct ip_vs_scheduler *scheduler)
49 IP_VS_ERR("ip_vs_bind_scheduler(): svc arg NULL\n");
52 if (scheduler == NULL) {
53 IP_VS_ERR("ip_vs_bind_scheduler(): scheduler arg NULL\n");
57 svc->scheduler = scheduler;
59 if (scheduler->init_service) {
60 ret = scheduler->init_service(svc);
62 IP_VS_ERR("ip_vs_bind_scheduler(): init error\n");
72 * Unbind a service with its scheduler
74 int ip_vs_unbind_scheduler(struct ip_vs_service *svc)
76 struct ip_vs_scheduler *sched;
79 IP_VS_ERR("ip_vs_unbind_scheduler(): svc arg NULL\n");
83 sched = svc->scheduler;
85 IP_VS_ERR("ip_vs_unbind_scheduler(): svc isn't bound\n");
89 if (sched->done_service) {
90 if (sched->done_service(svc) != 0) {
91 IP_VS_ERR("ip_vs_unbind_scheduler(): done error\n");
96 svc->scheduler = NULL;
102 * Get scheduler in the scheduler list by name
104 static struct ip_vs_scheduler *ip_vs_sched_getbyname(const char *sched_name)
106 struct ip_vs_scheduler *sched;
108 IP_VS_DBG(2, "ip_vs_sched_getbyname(): sched_name \"%s\"\n",
111 read_lock_bh(&__ip_vs_sched_lock);
113 list_for_each_entry(sched, &ip_vs_schedulers, n_list) {
115 * Test and get the modules atomically
117 if (sched->module && !try_module_get(sched->module)) {
119 * This scheduler is just deleted
123 if (strcmp(sched_name, sched->name)==0) {
125 read_unlock_bh(&__ip_vs_sched_lock);
129 module_put(sched->module);
132 read_unlock_bh(&__ip_vs_sched_lock);
138 * Lookup scheduler and try to load it if it doesn't exist
140 struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name)
142 struct ip_vs_scheduler *sched;
145 * Search for the scheduler by sched_name
147 sched = ip_vs_sched_getbyname(sched_name);
150 * If scheduler not found, load the module and search again
153 request_module("ip_vs_%s", sched_name);
154 sched = ip_vs_sched_getbyname(sched_name);
160 void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler)
162 if (scheduler->module)
163 module_put(scheduler->module);
168 * Register a scheduler in the scheduler list
170 int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler)
172 struct ip_vs_scheduler *sched;
175 IP_VS_ERR("register_ip_vs_scheduler(): NULL arg\n");
179 if (!scheduler->name) {
180 IP_VS_ERR("register_ip_vs_scheduler(): NULL scheduler_name\n");
184 /* increase the module use count */
185 ip_vs_use_count_inc();
187 write_lock_bh(&__ip_vs_sched_lock);
189 if (scheduler->n_list.next != &scheduler->n_list) {
190 write_unlock_bh(&__ip_vs_sched_lock);
191 ip_vs_use_count_dec();
192 IP_VS_ERR("register_ip_vs_scheduler(): [%s] scheduler "
193 "already linked\n", scheduler->name);
198 * Make sure that the scheduler with this name doesn't exist
199 * in the scheduler list.
201 list_for_each_entry(sched, &ip_vs_schedulers, n_list) {
202 if (strcmp(scheduler->name, sched->name) == 0) {
203 write_unlock_bh(&__ip_vs_sched_lock);
204 ip_vs_use_count_dec();
205 IP_VS_ERR("register_ip_vs_scheduler(): [%s] scheduler "
206 "already existed in the system\n",
212 * Add it into the d-linked scheduler list
214 list_add(&scheduler->n_list, &ip_vs_schedulers);
215 write_unlock_bh(&__ip_vs_sched_lock);
217 IP_VS_INFO("[%s] scheduler registered.\n", scheduler->name);
224 * Unregister a scheduler from the scheduler list
226 int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler)
229 IP_VS_ERR( "unregister_ip_vs_scheduler(): NULL arg\n");
233 write_lock_bh(&__ip_vs_sched_lock);
234 if (scheduler->n_list.next == &scheduler->n_list) {
235 write_unlock_bh(&__ip_vs_sched_lock);
236 IP_VS_ERR("unregister_ip_vs_scheduler(): [%s] scheduler "
237 "is not in the list. failed\n", scheduler->name);
242 * Remove it from the d-linked scheduler list
244 list_del(&scheduler->n_list);
245 write_unlock_bh(&__ip_vs_sched_lock);
247 /* decrease the module use count */
248 ip_vs_use_count_dec();
250 IP_VS_INFO("[%s] scheduler unregistered.\n", scheduler->name);