KVM: introduce KVM_PFN_ERR_RO_FAULT
[linux-2.6.git] / net / netfilter / ipvs / ip_vs_sched.c
blob08dbdd5bc18fc5dc9f23562ac86a66e20dafb897
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.
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.
16 * Changes:
20 #define KMSG_COMPONENT "IPVS"
21 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
23 #include <linux/module.h>
24 #include <linux/spinlock.h>
25 #include <linux/interrupt.h>
26 #include <asm/string.h>
27 #include <linux/kmod.h>
28 #include <linux/sysctl.h>
30 #include <net/ip_vs.h>
32 EXPORT_SYMBOL(ip_vs_scheduler_err);
34 * IPVS scheduler list
36 static LIST_HEAD(ip_vs_schedulers);
38 /* lock for service table */
39 static DEFINE_SPINLOCK(ip_vs_sched_lock);
43 * Bind a service with a scheduler
45 int ip_vs_bind_scheduler(struct ip_vs_service *svc,
46 struct ip_vs_scheduler *scheduler)
48 int ret;
50 svc->scheduler = scheduler;
52 if (scheduler->init_service) {
53 ret = scheduler->init_service(svc);
54 if (ret) {
55 pr_err("%s(): init error\n", __func__);
56 return ret;
60 return 0;
65 * Unbind a service with its scheduler
67 int ip_vs_unbind_scheduler(struct ip_vs_service *svc)
69 struct ip_vs_scheduler *sched = svc->scheduler;
71 if (!sched)
72 return 0;
74 if (sched->done_service) {
75 if (sched->done_service(svc) != 0) {
76 pr_err("%s(): done error\n", __func__);
77 return -EINVAL;
81 svc->scheduler = NULL;
82 return 0;
87 * Get scheduler in the scheduler list by name
89 static struct ip_vs_scheduler *ip_vs_sched_getbyname(const char *sched_name)
91 struct ip_vs_scheduler *sched;
93 IP_VS_DBG(2, "%s(): sched_name \"%s\"\n", __func__, sched_name);
95 spin_lock_bh(&ip_vs_sched_lock);
97 list_for_each_entry(sched, &ip_vs_schedulers, n_list) {
99 * Test and get the modules atomically
101 if (sched->module && !try_module_get(sched->module)) {
103 * This scheduler is just deleted
105 continue;
107 if (strcmp(sched_name, sched->name)==0) {
108 /* HIT */
109 spin_unlock_bh(&ip_vs_sched_lock);
110 return sched;
112 if (sched->module)
113 module_put(sched->module);
116 spin_unlock_bh(&ip_vs_sched_lock);
117 return NULL;
122 * Lookup scheduler and try to load it if it doesn't exist
124 struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name)
126 struct ip_vs_scheduler *sched;
129 * Search for the scheduler by sched_name
131 sched = ip_vs_sched_getbyname(sched_name);
134 * If scheduler not found, load the module and search again
136 if (sched == NULL) {
137 request_module("ip_vs_%s", sched_name);
138 sched = ip_vs_sched_getbyname(sched_name);
141 return sched;
144 void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler)
146 if (scheduler && scheduler->module)
147 module_put(scheduler->module);
151 * Common error output helper for schedulers
154 void ip_vs_scheduler_err(struct ip_vs_service *svc, const char *msg)
156 if (svc->fwmark) {
157 IP_VS_ERR_RL("%s: FWM %u 0x%08X - %s\n",
158 svc->scheduler->name, svc->fwmark,
159 svc->fwmark, msg);
160 #ifdef CONFIG_IP_VS_IPV6
161 } else if (svc->af == AF_INET6) {
162 IP_VS_ERR_RL("%s: %s [%pI6]:%d - %s\n",
163 svc->scheduler->name,
164 ip_vs_proto_name(svc->protocol),
165 &svc->addr.in6, ntohs(svc->port), msg);
166 #endif
167 } else {
168 IP_VS_ERR_RL("%s: %s %pI4:%d - %s\n",
169 svc->scheduler->name,
170 ip_vs_proto_name(svc->protocol),
171 &svc->addr.ip, ntohs(svc->port), msg);
176 * Register a scheduler in the scheduler list
178 int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler)
180 struct ip_vs_scheduler *sched;
182 if (!scheduler) {
183 pr_err("%s(): NULL arg\n", __func__);
184 return -EINVAL;
187 if (!scheduler->name) {
188 pr_err("%s(): NULL scheduler_name\n", __func__);
189 return -EINVAL;
192 /* increase the module use count */
193 ip_vs_use_count_inc();
195 spin_lock_bh(&ip_vs_sched_lock);
197 if (!list_empty(&scheduler->n_list)) {
198 spin_unlock_bh(&ip_vs_sched_lock);
199 ip_vs_use_count_dec();
200 pr_err("%s(): [%s] scheduler already linked\n",
201 __func__, scheduler->name);
202 return -EINVAL;
206 * Make sure that the scheduler with this name doesn't exist
207 * in the scheduler list.
209 list_for_each_entry(sched, &ip_vs_schedulers, n_list) {
210 if (strcmp(scheduler->name, sched->name) == 0) {
211 spin_unlock_bh(&ip_vs_sched_lock);
212 ip_vs_use_count_dec();
213 pr_err("%s(): [%s] scheduler already existed "
214 "in the system\n", __func__, scheduler->name);
215 return -EINVAL;
219 * Add it into the d-linked scheduler list
221 list_add(&scheduler->n_list, &ip_vs_schedulers);
222 spin_unlock_bh(&ip_vs_sched_lock);
224 pr_info("[%s] scheduler registered.\n", scheduler->name);
226 return 0;
231 * Unregister a scheduler from the scheduler list
233 int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler)
235 if (!scheduler) {
236 pr_err("%s(): NULL arg\n", __func__);
237 return -EINVAL;
240 spin_lock_bh(&ip_vs_sched_lock);
241 if (list_empty(&scheduler->n_list)) {
242 spin_unlock_bh(&ip_vs_sched_lock);
243 pr_err("%s(): [%s] scheduler is not in the list. failed\n",
244 __func__, scheduler->name);
245 return -EINVAL;
249 * Remove it from the d-linked scheduler list
251 list_del(&scheduler->n_list);
252 spin_unlock_bh(&ip_vs_sched_lock);
254 /* decrease the module use count */
255 ip_vs_use_count_dec();
257 pr_info("[%s] scheduler unregistered.\n", scheduler->name);
259 return 0;