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 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
9 * Peter Kese <peter.kese@ijs.si>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
20 #include <linux/module.h>
21 #include <linux/spinlock.h>
22 #include <linux/interrupt.h>
23 #include <asm/string.h>
24 #include <linux/kmod.h>
25 #include <linux/sysctl.h>
27 #include <net/ip_vs.h>
32 static LIST_HEAD(ip_vs_schedulers);
34 /* lock for service table */
35 static DEFINE_RWLOCK(__ip_vs_sched_lock);
39 * Bind a service with a scheduler
41 int ip_vs_bind_scheduler(struct ip_vs_service *svc,
42 struct ip_vs_scheduler *scheduler)
47 IP_VS_ERR("ip_vs_bind_scheduler(): svc arg NULL\n");
50 if (scheduler == NULL) {
51 IP_VS_ERR("ip_vs_bind_scheduler(): scheduler arg NULL\n");
55 svc->scheduler = scheduler;
57 if (scheduler->init_service) {
58 ret = scheduler->init_service(svc);
60 IP_VS_ERR("ip_vs_bind_scheduler(): init error\n");
70 * Unbind a service with its scheduler
72 int ip_vs_unbind_scheduler(struct ip_vs_service *svc)
74 struct ip_vs_scheduler *sched;
77 IP_VS_ERR("ip_vs_unbind_scheduler(): svc arg NULL\n");
81 sched = svc->scheduler;
83 IP_VS_ERR("ip_vs_unbind_scheduler(): svc isn't bound\n");
87 if (sched->done_service) {
88 if (sched->done_service(svc) != 0) {
89 IP_VS_ERR("ip_vs_unbind_scheduler(): done error\n");
94 svc->scheduler = NULL;
100 * Get scheduler in the scheduler list by name
102 static struct ip_vs_scheduler *ip_vs_sched_getbyname(const char *sched_name)
104 struct ip_vs_scheduler *sched;
106 IP_VS_DBG(2, "ip_vs_sched_getbyname(): sched_name \"%s\"\n",
109 read_lock_bh(&__ip_vs_sched_lock);
111 list_for_each_entry(sched, &ip_vs_schedulers, n_list) {
113 * Test and get the modules atomically
115 if (sched->module && !try_module_get(sched->module)) {
117 * This scheduler is just deleted
121 if (strcmp(sched_name, sched->name)==0) {
123 read_unlock_bh(&__ip_vs_sched_lock);
127 module_put(sched->module);
130 read_unlock_bh(&__ip_vs_sched_lock);
136 * Lookup scheduler and try to load it if it doesn't exist
138 struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name)
140 struct ip_vs_scheduler *sched;
143 * Search for the scheduler by sched_name
145 sched = ip_vs_sched_getbyname(sched_name);
148 * If scheduler not found, load the module and search again
151 request_module("ip_vs_%s", sched_name);
152 sched = ip_vs_sched_getbyname(sched_name);
158 void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler)
160 if (scheduler->module)
161 module_put(scheduler->module);
166 * Register a scheduler in the scheduler list
168 int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler)
170 struct ip_vs_scheduler *sched;
173 IP_VS_ERR("register_ip_vs_scheduler(): NULL arg\n");
177 if (!scheduler->name) {
178 IP_VS_ERR("register_ip_vs_scheduler(): NULL scheduler_name\n");
182 /* increase the module use count */
183 ip_vs_use_count_inc();
185 write_lock_bh(&__ip_vs_sched_lock);
187 if (scheduler->n_list.next != &scheduler->n_list) {
188 write_unlock_bh(&__ip_vs_sched_lock);
189 ip_vs_use_count_dec();
190 IP_VS_ERR("register_ip_vs_scheduler(): [%s] scheduler "
191 "already linked\n", scheduler->name);
196 * Make sure that the scheduler with this name doesn't exist
197 * in the scheduler list.
199 list_for_each_entry(sched, &ip_vs_schedulers, n_list) {
200 if (strcmp(scheduler->name, sched->name) == 0) {
201 write_unlock_bh(&__ip_vs_sched_lock);
202 ip_vs_use_count_dec();
203 IP_VS_ERR("register_ip_vs_scheduler(): [%s] scheduler "
204 "already existed in the system\n",
210 * Add it into the d-linked scheduler list
212 list_add(&scheduler->n_list, &ip_vs_schedulers);
213 write_unlock_bh(&__ip_vs_sched_lock);
215 IP_VS_INFO("[%s] scheduler registered.\n", scheduler->name);
222 * Unregister a scheduler from the scheduler list
224 int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler)
227 IP_VS_ERR( "unregister_ip_vs_scheduler(): NULL arg\n");
231 write_lock_bh(&__ip_vs_sched_lock);
232 if (scheduler->n_list.next == &scheduler->n_list) {
233 write_unlock_bh(&__ip_vs_sched_lock);
234 IP_VS_ERR("unregister_ip_vs_scheduler(): [%s] scheduler "
235 "is not in the list. failed\n", scheduler->name);
240 * Remove it from the d-linked scheduler list
242 list_del(&scheduler->n_list);
243 write_unlock_bh(&__ip_vs_sched_lock);
245 /* decrease the module use count */
246 ip_vs_use_count_dec();
248 IP_VS_INFO("[%s] scheduler unregistered.\n", scheduler->name);