Commit | Line | Data |
---|---|---|
1da177e4 LT |
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. | |
7 | * | |
1da177e4 LT |
8 | * 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 | ||
9aada7ac HE |
20 | #define KMSG_COMPONENT "IPVS" |
21 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt | |
22 | ||
1da177e4 | 23 | #include <linux/module.h> |
1da177e4 | 24 | #include <linux/spinlock.h> |
4ffd2e49 | 25 | #include <linux/interrupt.h> |
1da177e4 LT |
26 | #include <asm/string.h> |
27 | #include <linux/kmod.h> | |
90754f8e | 28 | #include <linux/sysctl.h> |
1da177e4 LT |
29 | |
30 | #include <net/ip_vs.h> | |
31 | ||
41ac51ee | 32 | EXPORT_SYMBOL(ip_vs_scheduler_err); |
1da177e4 LT |
33 | /* |
34 | * IPVS scheduler list | |
35 | */ | |
36 | static LIST_HEAD(ip_vs_schedulers); | |
37 | ||
71dfa982 JA |
38 | /* semaphore for schedulers */ |
39 | static DEFINE_MUTEX(ip_vs_sched_mutex); | |
1da177e4 LT |
40 | |
41 | ||
42 | /* | |
43 | * Bind a service with a scheduler | |
44 | */ | |
45 | int ip_vs_bind_scheduler(struct ip_vs_service *svc, | |
46 | struct ip_vs_scheduler *scheduler) | |
47 | { | |
48 | int ret; | |
49 | ||
1da177e4 LT |
50 | if (scheduler->init_service) { |
51 | ret = scheduler->init_service(svc); | |
52 | if (ret) { | |
1e3e238e | 53 | pr_err("%s(): init error\n", __func__); |
1da177e4 LT |
54 | return ret; |
55 | } | |
56 | } | |
ceec4c38 | 57 | rcu_assign_pointer(svc->scheduler, scheduler); |
1da177e4 LT |
58 | return 0; |
59 | } | |
60 | ||
61 | ||
62 | /* | |
63 | * Unbind a service with its scheduler | |
64 | */ | |
ceec4c38 JA |
65 | void ip_vs_unbind_scheduler(struct ip_vs_service *svc, |
66 | struct ip_vs_scheduler *sched) | |
1da177e4 | 67 | { |
ceec4c38 | 68 | struct ip_vs_scheduler *cur_sched; |
1da177e4 | 69 | |
ceec4c38 JA |
70 | cur_sched = rcu_dereference_protected(svc->scheduler, 1); |
71 | /* This check proves that old 'sched' was installed */ | |
72 | if (!cur_sched) | |
ed3ffc4e | 73 | return; |
1da177e4 | 74 | |
ed3ffc4e JA |
75 | if (sched->done_service) |
76 | sched->done_service(svc); | |
ceec4c38 | 77 | /* svc->scheduler can not be set to NULL */ |
1da177e4 LT |
78 | } |
79 | ||
80 | ||
81 | /* | |
82 | * Get scheduler in the scheduler list by name | |
83 | */ | |
84 | static struct ip_vs_scheduler *ip_vs_sched_getbyname(const char *sched_name) | |
85 | { | |
86 | struct ip_vs_scheduler *sched; | |
87 | ||
1e3e238e | 88 | IP_VS_DBG(2, "%s(): sched_name \"%s\"\n", __func__, sched_name); |
1da177e4 | 89 | |
71dfa982 | 90 | mutex_lock(&ip_vs_sched_mutex); |
1da177e4 LT |
91 | |
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 */ | |
71dfa982 | 104 | mutex_unlock(&ip_vs_sched_mutex); |
1da177e4 LT |
105 | return sched; |
106 | } | |
982f4051 | 107 | module_put(sched->module); |
1da177e4 LT |
108 | } |
109 | ||
71dfa982 | 110 | mutex_unlock(&ip_vs_sched_mutex); |
1da177e4 LT |
111 | return NULL; |
112 | } | |
113 | ||
114 | ||
115 | /* | |
116 | * Lookup scheduler and try to load it if it doesn't exist | |
117 | */ | |
118 | struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name) | |
119 | { | |
120 | struct ip_vs_scheduler *sched; | |
121 | ||
122 | /* | |
123 | * Search for the scheduler by sched_name | |
124 | */ | |
125 | sched = ip_vs_sched_getbyname(sched_name); | |
126 | ||
127 | /* | |
128 | * If scheduler not found, load the module and search again | |
129 | */ | |
130 | if (sched == NULL) { | |
131 | request_module("ip_vs_%s", sched_name); | |
132 | sched = ip_vs_sched_getbyname(sched_name); | |
133 | } | |
134 | ||
135 | return sched; | |
136 | } | |
137 | ||
138 | void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler) | |
139 | { | |
6e08bfb8 | 140 | if (scheduler && scheduler->module) |
1da177e4 LT |
141 | module_put(scheduler->module); |
142 | } | |
143 | ||
41ac51ee PS |
144 | /* |
145 | * Common error output helper for schedulers | |
146 | */ | |
147 | ||
148 | void ip_vs_scheduler_err(struct ip_vs_service *svc, const char *msg) | |
149 | { | |
ceec4c38 JA |
150 | struct ip_vs_scheduler *sched; |
151 | ||
152 | sched = rcu_dereference(svc->scheduler); | |
41ac51ee PS |
153 | if (svc->fwmark) { |
154 | IP_VS_ERR_RL("%s: FWM %u 0x%08X - %s\n", | |
ceec4c38 | 155 | sched->name, svc->fwmark, svc->fwmark, msg); |
41ac51ee PS |
156 | #ifdef CONFIG_IP_VS_IPV6 |
157 | } else if (svc->af == AF_INET6) { | |
120b9c14 | 158 | IP_VS_ERR_RL("%s: %s [%pI6c]:%d - %s\n", |
ceec4c38 | 159 | sched->name, ip_vs_proto_name(svc->protocol), |
41ac51ee PS |
160 | &svc->addr.in6, ntohs(svc->port), msg); |
161 | #endif | |
162 | } else { | |
163 | IP_VS_ERR_RL("%s: %s %pI4:%d - %s\n", | |
ceec4c38 | 164 | sched->name, ip_vs_proto_name(svc->protocol), |
41ac51ee PS |
165 | &svc->addr.ip, ntohs(svc->port), msg); |
166 | } | |
167 | } | |
1da177e4 LT |
168 | |
169 | /* | |
170 | * Register a scheduler in the scheduler list | |
171 | */ | |
172 | int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler) | |
173 | { | |
174 | struct ip_vs_scheduler *sched; | |
175 | ||
176 | if (!scheduler) { | |
1e3e238e | 177 | pr_err("%s(): NULL arg\n", __func__); |
1da177e4 LT |
178 | return -EINVAL; |
179 | } | |
180 | ||
181 | if (!scheduler->name) { | |
1e3e238e | 182 | pr_err("%s(): NULL scheduler_name\n", __func__); |
1da177e4 LT |
183 | return -EINVAL; |
184 | } | |
185 | ||
186 | /* increase the module use count */ | |
187 | ip_vs_use_count_inc(); | |
188 | ||
71dfa982 | 189 | mutex_lock(&ip_vs_sched_mutex); |
1da177e4 | 190 | |
66a0be47 | 191 | if (!list_empty(&scheduler->n_list)) { |
71dfa982 | 192 | mutex_unlock(&ip_vs_sched_mutex); |
1da177e4 | 193 | ip_vs_use_count_dec(); |
1e3e238e HE |
194 | pr_err("%s(): [%s] scheduler already linked\n", |
195 | __func__, scheduler->name); | |
1da177e4 LT |
196 | return -EINVAL; |
197 | } | |
198 | ||
4ac63ad6 PE |
199 | /* |
200 | * Make sure that the scheduler with this name doesn't exist | |
201 | * in the scheduler list. | |
202 | */ | |
203 | list_for_each_entry(sched, &ip_vs_schedulers, n_list) { | |
204 | if (strcmp(scheduler->name, sched->name) == 0) { | |
71dfa982 | 205 | mutex_unlock(&ip_vs_sched_mutex); |
4ac63ad6 | 206 | ip_vs_use_count_dec(); |
1e3e238e HE |
207 | pr_err("%s(): [%s] scheduler already existed " |
208 | "in the system\n", __func__, scheduler->name); | |
4ac63ad6 PE |
209 | return -EINVAL; |
210 | } | |
211 | } | |
1da177e4 LT |
212 | /* |
213 | * Add it into the d-linked scheduler list | |
214 | */ | |
215 | list_add(&scheduler->n_list, &ip_vs_schedulers); | |
71dfa982 | 216 | mutex_unlock(&ip_vs_sched_mutex); |
1da177e4 | 217 | |
1e3e238e | 218 | pr_info("[%s] scheduler registered.\n", scheduler->name); |
1da177e4 LT |
219 | |
220 | return 0; | |
221 | } | |
222 | ||
223 | ||
224 | /* | |
225 | * Unregister a scheduler from the scheduler list | |
226 | */ | |
227 | int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler) | |
228 | { | |
229 | if (!scheduler) { | |
1e3e238e | 230 | pr_err("%s(): NULL arg\n", __func__); |
1da177e4 LT |
231 | return -EINVAL; |
232 | } | |
233 | ||
71dfa982 | 234 | mutex_lock(&ip_vs_sched_mutex); |
66a0be47 | 235 | if (list_empty(&scheduler->n_list)) { |
71dfa982 | 236 | mutex_unlock(&ip_vs_sched_mutex); |
1e3e238e HE |
237 | pr_err("%s(): [%s] scheduler is not in the list. failed\n", |
238 | __func__, scheduler->name); | |
1da177e4 LT |
239 | return -EINVAL; |
240 | } | |
241 | ||
242 | /* | |
243 | * Remove it from the d-linked scheduler list | |
244 | */ | |
245 | list_del(&scheduler->n_list); | |
71dfa982 | 246 | mutex_unlock(&ip_vs_sched_mutex); |
1da177e4 LT |
247 | |
248 | /* decrease the module use count */ | |
249 | ip_vs_use_count_dec(); | |
250 | ||
1e3e238e | 251 | pr_info("[%s] scheduler unregistered.\n", scheduler->name); |
1da177e4 LT |
252 | |
253 | return 0; | |
254 | } |