]>
git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - net/netfilter/ipvs/ip_vs_sched.c
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 #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
);
36 static LIST_HEAD(ip_vs_schedulers
);
38 /* semaphore for schedulers */
39 static DEFINE_MUTEX(ip_vs_sched_mutex
);
43 * Bind a service with a scheduler
45 int ip_vs_bind_scheduler(struct ip_vs_service
*svc
,
46 struct ip_vs_scheduler
*scheduler
)
50 svc
->scheduler
= scheduler
;
52 if (scheduler
->init_service
) {
53 ret
= scheduler
->init_service(svc
);
55 pr_err("%s(): init error\n", __func__
);
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
;
74 if (sched
->done_service
) {
75 if (sched
->done_service(svc
) != 0) {
76 pr_err("%s(): done error\n", __func__
);
81 svc
->scheduler
= NULL
;
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 mutex_lock(&ip_vs_sched_mutex
);
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
107 if (strcmp(sched_name
, sched
->name
)==0) {
109 mutex_unlock(&ip_vs_sched_mutex
);
113 module_put(sched
->module
);
116 mutex_unlock(&ip_vs_sched_mutex
);
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
137 request_module("ip_vs_%s", sched_name
);
138 sched
= ip_vs_sched_getbyname(sched_name
);
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
)
157 IP_VS_ERR_RL("%s: FWM %u 0x%08X - %s\n",
158 svc
->scheduler
->name
, svc
->fwmark
,
160 #ifdef CONFIG_IP_VS_IPV6
161 } else if (svc
->af
== AF_INET6
) {
162 IP_VS_ERR_RL("%s: %s [%pI6c]:%d - %s\n",
163 svc
->scheduler
->name
,
164 ip_vs_proto_name(svc
->protocol
),
165 &svc
->addr
.in6
, ntohs(svc
->port
), msg
);
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
;
183 pr_err("%s(): NULL arg\n", __func__
);
187 if (!scheduler
->name
) {
188 pr_err("%s(): NULL scheduler_name\n", __func__
);
192 /* increase the module use count */
193 ip_vs_use_count_inc();
195 mutex_lock(&ip_vs_sched_mutex
);
197 if (!list_empty(&scheduler
->n_list
)) {
198 mutex_unlock(&ip_vs_sched_mutex
);
199 ip_vs_use_count_dec();
200 pr_err("%s(): [%s] scheduler already linked\n",
201 __func__
, scheduler
->name
);
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 mutex_unlock(&ip_vs_sched_mutex
);
212 ip_vs_use_count_dec();
213 pr_err("%s(): [%s] scheduler already existed "
214 "in the system\n", __func__
, scheduler
->name
);
219 * Add it into the d-linked scheduler list
221 list_add(&scheduler
->n_list
, &ip_vs_schedulers
);
222 mutex_unlock(&ip_vs_sched_mutex
);
224 pr_info("[%s] scheduler registered.\n", scheduler
->name
);
231 * Unregister a scheduler from the scheduler list
233 int unregister_ip_vs_scheduler(struct ip_vs_scheduler
*scheduler
)
236 pr_err("%s(): NULL arg\n", __func__
);
240 mutex_lock(&ip_vs_sched_mutex
);
241 if (list_empty(&scheduler
->n_list
)) {
242 mutex_unlock(&ip_vs_sched_mutex
);
243 pr_err("%s(): [%s] scheduler is not in the list. failed\n",
244 __func__
, scheduler
->name
);
249 * Remove it from the d-linked scheduler list
251 list_del(&scheduler
->n_list
);
252 mutex_unlock(&ip_vs_sched_mutex
);
254 /* decrease the module use count */
255 ip_vs_use_count_dec();
257 pr_info("[%s] scheduler unregistered.\n", scheduler
->name
);