blob: 08dbdd5bc18fc5dc9f23562ac86a66e20dafb897 [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
38/* lock for service table */
Simon Hormanbd144552010-08-26 02:54:29 +000039static DEFINE_SPINLOCK(ip_vs_sched_lock);
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 svc->scheduler = scheduler;
51
52 if (scheduler->init_service) {
53 ret = scheduler->init_service(svc);
54 if (ret) {
Hannes Eder1e3e2382009-08-02 11:05:41 +000055 pr_err("%s(): init error\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 return ret;
57 }
58 }
59
60 return 0;
61}
62
63
64/*
65 * Unbind a service with its scheduler
66 */
67int ip_vs_unbind_scheduler(struct ip_vs_service *svc)
68{
Simon Horman2fabf352010-08-22 21:37:52 +090069 struct ip_vs_scheduler *sched = svc->scheduler;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Simon Horman2fabf352010-08-22 21:37:52 +090071 if (!sched)
72 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 if (sched->done_service) {
75 if (sched->done_service(svc) != 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +000076 pr_err("%s(): done error\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return -EINVAL;
78 }
79 }
80
81 svc->scheduler = NULL;
82 return 0;
83}
84
85
86/*
87 * Get scheduler in the scheduler list by name
88 */
89static struct ip_vs_scheduler *ip_vs_sched_getbyname(const char *sched_name)
90{
91 struct ip_vs_scheduler *sched;
92
Hannes Eder1e3e2382009-08-02 11:05:41 +000093 IP_VS_DBG(2, "%s(): sched_name \"%s\"\n", __func__, sched_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Simon Hormanbd144552010-08-26 02:54:29 +000095 spin_lock_bh(&ip_vs_sched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 list_for_each_entry(sched, &ip_vs_schedulers, n_list) {
98 /*
99 * Test and get the modules atomically
100 */
101 if (sched->module && !try_module_get(sched->module)) {
102 /*
103 * This scheduler is just deleted
104 */
105 continue;
106 }
107 if (strcmp(sched_name, sched->name)==0) {
108 /* HIT */
Simon Hormanbd144552010-08-26 02:54:29 +0000109 spin_unlock_bh(&ip_vs_sched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return sched;
111 }
112 if (sched->module)
113 module_put(sched->module);
114 }
115
Simon Hormanbd144552010-08-26 02:54:29 +0000116 spin_unlock_bh(&ip_vs_sched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return NULL;
118}
119
120
121/*
122 * Lookup scheduler and try to load it if it doesn't exist
123 */
124struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name)
125{
126 struct ip_vs_scheduler *sched;
127
128 /*
129 * Search for the scheduler by sched_name
130 */
131 sched = ip_vs_sched_getbyname(sched_name);
132
133 /*
134 * If scheduler not found, load the module and search again
135 */
136 if (sched == NULL) {
137 request_module("ip_vs_%s", sched_name);
138 sched = ip_vs_sched_getbyname(sched_name);
139 }
140
141 return sched;
142}
143
144void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler)
145{
Simon Horman6e08bfb2010-08-22 21:37:52 +0900146 if (scheduler && scheduler->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 module_put(scheduler->module);
148}
149
Patrick Schaaf41ac51e2011-02-11 14:01:12 +0100150/*
151 * Common error output helper for schedulers
152 */
153
154void ip_vs_scheduler_err(struct ip_vs_service *svc, const char *msg)
155{
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);
172 }
173}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175/*
176 * Register a scheduler in the scheduler list
177 */
178int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler)
179{
180 struct ip_vs_scheduler *sched;
181
182 if (!scheduler) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000183 pr_err("%s(): NULL arg\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return -EINVAL;
185 }
186
187 if (!scheduler->name) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000188 pr_err("%s(): NULL scheduler_name\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return -EINVAL;
190 }
191
192 /* increase the module use count */
193 ip_vs_use_count_inc();
194
Simon Hormanbd144552010-08-26 02:54:29 +0000195 spin_lock_bh(&ip_vs_sched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Sven Wegener66a0be42008-08-10 09:18:02 +0000197 if (!list_empty(&scheduler->n_list)) {
Simon Hormanbd144552010-08-26 02:54:29 +0000198 spin_unlock_bh(&ip_vs_sched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 ip_vs_use_count_dec();
Hannes Eder1e3e2382009-08-02 11:05:41 +0000200 pr_err("%s(): [%s] scheduler already linked\n",
201 __func__, scheduler->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return -EINVAL;
203 }
204
205 /*
Pavel Emelyanov4ac63ad2007-12-04 00:45:06 -0800206 * Make sure that the scheduler with this name doesn't exist
207 * in the scheduler list.
208 */
209 list_for_each_entry(sched, &ip_vs_schedulers, n_list) {
210 if (strcmp(scheduler->name, sched->name) == 0) {
Simon Hormanbd144552010-08-26 02:54:29 +0000211 spin_unlock_bh(&ip_vs_sched_lock);
Pavel Emelyanov4ac63ad2007-12-04 00:45:06 -0800212 ip_vs_use_count_dec();
Hannes Eder1e3e2382009-08-02 11:05:41 +0000213 pr_err("%s(): [%s] scheduler already existed "
214 "in the system\n", __func__, scheduler->name);
Pavel Emelyanov4ac63ad2007-12-04 00:45:06 -0800215 return -EINVAL;
216 }
217 }
218 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 * Add it into the d-linked scheduler list
220 */
221 list_add(&scheduler->n_list, &ip_vs_schedulers);
Simon Hormanbd144552010-08-26 02:54:29 +0000222 spin_unlock_bh(&ip_vs_sched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Hannes Eder1e3e2382009-08-02 11:05:41 +0000224 pr_info("[%s] scheduler registered.\n", scheduler->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 return 0;
227}
228
229
230/*
231 * Unregister a scheduler from the scheduler list
232 */
233int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler)
234{
235 if (!scheduler) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000236 pr_err("%s(): NULL arg\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return -EINVAL;
238 }
239
Simon Hormanbd144552010-08-26 02:54:29 +0000240 spin_lock_bh(&ip_vs_sched_lock);
Sven Wegener66a0be42008-08-10 09:18:02 +0000241 if (list_empty(&scheduler->n_list)) {
Simon Hormanbd144552010-08-26 02:54:29 +0000242 spin_unlock_bh(&ip_vs_sched_lock);
Hannes Eder1e3e2382009-08-02 11:05:41 +0000243 pr_err("%s(): [%s] scheduler is not in the list. failed\n",
244 __func__, scheduler->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return -EINVAL;
246 }
247
248 /*
249 * Remove it from the d-linked scheduler list
250 */
251 list_del(&scheduler->n_list);
Simon Hormanbd144552010-08-26 02:54:29 +0000252 spin_unlock_bh(&ip_vs_sched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 /* decrease the module use count */
255 ip_vs_use_count_dec();
256
Hannes Eder1e3e2382009-08-02 11:05:41 +0000257 pr_info("[%s] scheduler unregistered.\n", scheduler->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 return 0;
260}