blob: 4dbcda6258bc2c2c20ecc71e130d25a462a90be4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
9 * Peter Kese <peter.kese@ijs.si>
10 *
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.
15 *
16 * Changes:
17 *
18 */
19
Hannes Eder9aada7a2009-07-30 14:29:44 -070020#define KMSG_COMPONENT "IPVS"
21#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/spinlock.h>
Adrian Bunk4ffd2e42006-01-05 12:14:43 -080025#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/string.h>
27#include <linux/kmod.h>
Pavel Emelyanov90754f82008-01-12 02:33:50 -080028#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <net/ip_vs.h>
31
Patrick Schaaf41ac51e2011-02-11 14:01:12 +010032EXPORT_SYMBOL(ip_vs_scheduler_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/*
34 * IPVS scheduler list
35 */
36static LIST_HEAD(ip_vs_schedulers);
37
Julian Anastasov71dfa982013-03-22 11:46:36 +020038/* semaphore for schedulers */
39static DEFINE_MUTEX(ip_vs_sched_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41
42/*
43 * Bind a service with a scheduler
44 */
45int ip_vs_bind_scheduler(struct ip_vs_service *svc,
46 struct ip_vs_scheduler *scheduler)
47{
48 int ret;
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 if (scheduler->init_service) {
51 ret = scheduler->init_service(svc);
52 if (ret) {
Hannes Eder1e3e2382009-08-02 11:05:41 +000053 pr_err("%s(): init error\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 return ret;
55 }
56 }
Julian Anastasovceec4c32013-03-22 11:46:53 +020057 rcu_assign_pointer(svc->scheduler, scheduler);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 return 0;
59}
60
61
62/*
63 * Unbind a service with its scheduler
64 */
Julian Anastasovceec4c32013-03-22 11:46:53 +020065void ip_vs_unbind_scheduler(struct ip_vs_service *svc,
66 struct ip_vs_scheduler *sched)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
Julian Anastasovceec4c32013-03-22 11:46:53 +020068 struct ip_vs_scheduler *cur_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Julian Anastasovceec4c32013-03-22 11:46:53 +020070 cur_sched = rcu_dereference_protected(svc->scheduler, 1);
71 /* This check proves that old 'sched' was installed */
72 if (!cur_sched)
Julian Anastasoved3ffc42013-03-22 11:46:50 +020073 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Julian Anastasoved3ffc42013-03-22 11:46:50 +020075 if (sched->done_service)
76 sched->done_service(svc);
Julian Anastasovceec4c32013-03-22 11:46:53 +020077 /* svc->scheduler can not be set to NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
80
81/*
82 * Get scheduler in the scheduler list by name
83 */
84static struct ip_vs_scheduler *ip_vs_sched_getbyname(const char *sched_name)
85{
86 struct ip_vs_scheduler *sched;
87
Hannes Eder1e3e2382009-08-02 11:05:41 +000088 IP_VS_DBG(2, "%s(): sched_name \"%s\"\n", __func__, sched_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Julian Anastasov71dfa982013-03-22 11:46:36 +020090 mutex_lock(&ip_vs_sched_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 list_for_each_entry(sched, &ip_vs_schedulers, n_list) {
93 /*
94 * Test and get the modules atomically
95 */
96 if (sched->module && !try_module_get(sched->module)) {
97 /*
98 * This scheduler is just deleted
99 */
100 continue;
101 }
102 if (strcmp(sched_name, sched->name)==0) {
103 /* HIT */
Julian Anastasov71dfa982013-03-22 11:46:36 +0200104 mutex_unlock(&ip_vs_sched_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return sched;
106 }
107 if (sched->module)
108 module_put(sched->module);
109 }
110
Julian Anastasov71dfa982013-03-22 11:46:36 +0200111 mutex_unlock(&ip_vs_sched_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return NULL;
113}
114
115
116/*
117 * Lookup scheduler and try to load it if it doesn't exist
118 */
119struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name)
120{
121 struct ip_vs_scheduler *sched;
122
123 /*
124 * Search for the scheduler by sched_name
125 */
126 sched = ip_vs_sched_getbyname(sched_name);
127
128 /*
129 * If scheduler not found, load the module and search again
130 */
131 if (sched == NULL) {
132 request_module("ip_vs_%s", sched_name);
133 sched = ip_vs_sched_getbyname(sched_name);
134 }
135
136 return sched;
137}
138
139void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler)
140{
Simon Horman6e08bfb2010-08-22 21:37:52 +0900141 if (scheduler && scheduler->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 module_put(scheduler->module);
143}
144
Patrick Schaaf41ac51e2011-02-11 14:01:12 +0100145/*
146 * Common error output helper for schedulers
147 */
148
149void ip_vs_scheduler_err(struct ip_vs_service *svc, const char *msg)
150{
Julian Anastasovceec4c32013-03-22 11:46:53 +0200151 struct ip_vs_scheduler *sched;
152
153 sched = rcu_dereference(svc->scheduler);
Patrick Schaaf41ac51e2011-02-11 14:01:12 +0100154 if (svc->fwmark) {
155 IP_VS_ERR_RL("%s: FWM %u 0x%08X - %s\n",
Julian Anastasovceec4c32013-03-22 11:46:53 +0200156 sched->name, svc->fwmark, svc->fwmark, msg);
Patrick Schaaf41ac51e2011-02-11 14:01:12 +0100157#ifdef CONFIG_IP_VS_IPV6
158 } else if (svc->af == AF_INET6) {
Jesper Dangaard Brouer120b9c12012-09-26 14:05:53 +0200159 IP_VS_ERR_RL("%s: %s [%pI6c]:%d - %s\n",
Julian Anastasovceec4c32013-03-22 11:46:53 +0200160 sched->name, ip_vs_proto_name(svc->protocol),
Patrick Schaaf41ac51e2011-02-11 14:01:12 +0100161 &svc->addr.in6, ntohs(svc->port), msg);
162#endif
163 } else {
164 IP_VS_ERR_RL("%s: %s %pI4:%d - %s\n",
Julian Anastasovceec4c32013-03-22 11:46:53 +0200165 sched->name, ip_vs_proto_name(svc->protocol),
Patrick Schaaf41ac51e2011-02-11 14:01:12 +0100166 &svc->addr.ip, ntohs(svc->port), msg);
167 }
168}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170/*
171 * Register a scheduler in the scheduler list
172 */
173int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler)
174{
175 struct ip_vs_scheduler *sched;
176
177 if (!scheduler) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000178 pr_err("%s(): NULL arg\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return -EINVAL;
180 }
181
182 if (!scheduler->name) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000183 pr_err("%s(): NULL scheduler_name\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return -EINVAL;
185 }
186
187 /* increase the module use count */
188 ip_vs_use_count_inc();
189
Julian Anastasov71dfa982013-03-22 11:46:36 +0200190 mutex_lock(&ip_vs_sched_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Sven Wegener66a0be42008-08-10 09:18:02 +0000192 if (!list_empty(&scheduler->n_list)) {
Julian Anastasov71dfa982013-03-22 11:46:36 +0200193 mutex_unlock(&ip_vs_sched_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 ip_vs_use_count_dec();
Hannes Eder1e3e2382009-08-02 11:05:41 +0000195 pr_err("%s(): [%s] scheduler already linked\n",
196 __func__, scheduler->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return -EINVAL;
198 }
199
200 /*
Pavel Emelyanov4ac63ad62007-12-04 00:45:06 -0800201 * Make sure that the scheduler with this name doesn't exist
202 * in the scheduler list.
203 */
204 list_for_each_entry(sched, &ip_vs_schedulers, n_list) {
205 if (strcmp(scheduler->name, sched->name) == 0) {
Julian Anastasov71dfa982013-03-22 11:46:36 +0200206 mutex_unlock(&ip_vs_sched_mutex);
Pavel Emelyanov4ac63ad62007-12-04 00:45:06 -0800207 ip_vs_use_count_dec();
Hannes Eder1e3e2382009-08-02 11:05:41 +0000208 pr_err("%s(): [%s] scheduler already existed "
209 "in the system\n", __func__, scheduler->name);
Pavel Emelyanov4ac63ad62007-12-04 00:45:06 -0800210 return -EINVAL;
211 }
212 }
213 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 * Add it into the d-linked scheduler list
215 */
216 list_add(&scheduler->n_list, &ip_vs_schedulers);
Julian Anastasov71dfa982013-03-22 11:46:36 +0200217 mutex_unlock(&ip_vs_sched_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Hannes Eder1e3e2382009-08-02 11:05:41 +0000219 pr_info("[%s] scheduler registered.\n", scheduler->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 return 0;
222}
223
224
225/*
226 * Unregister a scheduler from the scheduler list
227 */
228int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler)
229{
230 if (!scheduler) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000231 pr_err("%s(): NULL arg\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return -EINVAL;
233 }
234
Julian Anastasov71dfa982013-03-22 11:46:36 +0200235 mutex_lock(&ip_vs_sched_mutex);
Sven Wegener66a0be42008-08-10 09:18:02 +0000236 if (list_empty(&scheduler->n_list)) {
Julian Anastasov71dfa982013-03-22 11:46:36 +0200237 mutex_unlock(&ip_vs_sched_mutex);
Hannes Eder1e3e2382009-08-02 11:05:41 +0000238 pr_err("%s(): [%s] scheduler is not in the list. failed\n",
239 __func__, scheduler->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return -EINVAL;
241 }
242
243 /*
244 * Remove it from the d-linked scheduler list
245 */
246 list_del(&scheduler->n_list);
Julian Anastasov71dfa982013-03-22 11:46:36 +0200247 mutex_unlock(&ip_vs_sched_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 /* decrease the module use count */
250 ip_vs_use_count_dec();
251
Hannes Eder1e3e2382009-08-02 11:05:41 +0000252 pr_info("[%s] scheduler unregistered.\n", scheduler->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 return 0;
255}