]>
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 | ||
32 | /* | |
33 | * IPVS scheduler list | |
34 | */ | |
35 | static LIST_HEAD(ip_vs_schedulers); | |
36 | ||
37 | /* lock for service table */ | |
38 | static DEFINE_RWLOCK(__ip_vs_sched_lock); | |
39 | ||
40 | ||
41 | /* | |
42 | * Bind a service with a scheduler | |
43 | */ | |
44 | int ip_vs_bind_scheduler(struct ip_vs_service *svc, | |
45 | struct ip_vs_scheduler *scheduler) | |
46 | { | |
47 | int ret; | |
48 | ||
49 | if (svc == NULL) { | |
1e3e238e | 50 | pr_err("%s(): svc arg NULL\n", __func__); |
1da177e4 LT |
51 | return -EINVAL; |
52 | } | |
53 | if (scheduler == NULL) { | |
1e3e238e | 54 | pr_err("%s(): scheduler arg NULL\n", __func__); |
1da177e4 LT |
55 | return -EINVAL; |
56 | } | |
57 | ||
58 | svc->scheduler = scheduler; | |
59 | ||
60 | if (scheduler->init_service) { | |
61 | ret = scheduler->init_service(svc); | |
62 | if (ret) { | |
1e3e238e | 63 | pr_err("%s(): init error\n", __func__); |
1da177e4 LT |
64 | return ret; |
65 | } | |
66 | } | |
67 | ||
68 | return 0; | |
69 | } | |
70 | ||
71 | ||
72 | /* | |
73 | * Unbind a service with its scheduler | |
74 | */ | |
75 | int ip_vs_unbind_scheduler(struct ip_vs_service *svc) | |
76 | { | |
77 | struct ip_vs_scheduler *sched; | |
78 | ||
79 | if (svc == NULL) { | |
1e3e238e | 80 | pr_err("%s(): svc arg NULL\n", __func__); |
1da177e4 LT |
81 | return -EINVAL; |
82 | } | |
83 | ||
84 | sched = svc->scheduler; | |
85 | if (sched == NULL) { | |
1e3e238e | 86 | pr_err("%s(): svc isn't bound\n", __func__); |
1da177e4 LT |
87 | return -EINVAL; |
88 | } | |
89 | ||
90 | if (sched->done_service) { | |
91 | if (sched->done_service(svc) != 0) { | |
1e3e238e | 92 | pr_err("%s(): done error\n", __func__); |
1da177e4 LT |
93 | return -EINVAL; |
94 | } | |
95 | } | |
96 | ||
97 | svc->scheduler = NULL; | |
98 | return 0; | |
99 | } | |
100 | ||
101 | ||
102 | /* | |
103 | * Get scheduler in the scheduler list by name | |
104 | */ | |
105 | static struct ip_vs_scheduler *ip_vs_sched_getbyname(const char *sched_name) | |
106 | { | |
107 | struct ip_vs_scheduler *sched; | |
108 | ||
1e3e238e | 109 | IP_VS_DBG(2, "%s(): sched_name \"%s\"\n", __func__, sched_name); |
1da177e4 LT |
110 | |
111 | read_lock_bh(&__ip_vs_sched_lock); | |
112 | ||
113 | list_for_each_entry(sched, &ip_vs_schedulers, n_list) { | |
114 | /* | |
115 | * Test and get the modules atomically | |
116 | */ | |
117 | if (sched->module && !try_module_get(sched->module)) { | |
118 | /* | |
119 | * This scheduler is just deleted | |
120 | */ | |
121 | continue; | |
122 | } | |
123 | if (strcmp(sched_name, sched->name)==0) { | |
124 | /* HIT */ | |
125 | read_unlock_bh(&__ip_vs_sched_lock); | |
126 | return sched; | |
127 | } | |
128 | if (sched->module) | |
129 | module_put(sched->module); | |
130 | } | |
131 | ||
132 | read_unlock_bh(&__ip_vs_sched_lock); | |
133 | return NULL; | |
134 | } | |
135 | ||
136 | ||
137 | /* | |
138 | * Lookup scheduler and try to load it if it doesn't exist | |
139 | */ | |
140 | struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name) | |
141 | { | |
142 | struct ip_vs_scheduler *sched; | |
143 | ||
144 | /* | |
145 | * Search for the scheduler by sched_name | |
146 | */ | |
147 | sched = ip_vs_sched_getbyname(sched_name); | |
148 | ||
149 | /* | |
150 | * If scheduler not found, load the module and search again | |
151 | */ | |
152 | if (sched == NULL) { | |
153 | request_module("ip_vs_%s", sched_name); | |
154 | sched = ip_vs_sched_getbyname(sched_name); | |
155 | } | |
156 | ||
157 | return sched; | |
158 | } | |
159 | ||
160 | void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler) | |
161 | { | |
162 | if (scheduler->module) | |
163 | module_put(scheduler->module); | |
164 | } | |
165 | ||
166 | ||
167 | /* | |
168 | * Register a scheduler in the scheduler list | |
169 | */ | |
170 | int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler) | |
171 | { | |
172 | struct ip_vs_scheduler *sched; | |
173 | ||
174 | if (!scheduler) { | |
1e3e238e | 175 | pr_err("%s(): NULL arg\n", __func__); |
1da177e4 LT |
176 | return -EINVAL; |
177 | } | |
178 | ||
179 | if (!scheduler->name) { | |
1e3e238e | 180 | pr_err("%s(): NULL scheduler_name\n", __func__); |
1da177e4 LT |
181 | return -EINVAL; |
182 | } | |
183 | ||
184 | /* increase the module use count */ | |
185 | ip_vs_use_count_inc(); | |
186 | ||
1da177e4 LT |
187 | write_lock_bh(&__ip_vs_sched_lock); |
188 | ||
66a0be47 | 189 | if (!list_empty(&scheduler->n_list)) { |
1da177e4 LT |
190 | write_unlock_bh(&__ip_vs_sched_lock); |
191 | ip_vs_use_count_dec(); | |
1e3e238e HE |
192 | pr_err("%s(): [%s] scheduler already linked\n", |
193 | __func__, scheduler->name); | |
1da177e4 LT |
194 | return -EINVAL; |
195 | } | |
196 | ||
4ac63ad6 PE |
197 | /* |
198 | * Make sure that the scheduler with this name doesn't exist | |
199 | * in the scheduler list. | |
200 | */ | |
201 | list_for_each_entry(sched, &ip_vs_schedulers, n_list) { | |
202 | if (strcmp(scheduler->name, sched->name) == 0) { | |
203 | write_unlock_bh(&__ip_vs_sched_lock); | |
204 | ip_vs_use_count_dec(); | |
1e3e238e HE |
205 | pr_err("%s(): [%s] scheduler already existed " |
206 | "in the system\n", __func__, scheduler->name); | |
4ac63ad6 PE |
207 | return -EINVAL; |
208 | } | |
209 | } | |
1da177e4 LT |
210 | /* |
211 | * Add it into the d-linked scheduler list | |
212 | */ | |
213 | list_add(&scheduler->n_list, &ip_vs_schedulers); | |
214 | write_unlock_bh(&__ip_vs_sched_lock); | |
215 | ||
1e3e238e | 216 | pr_info("[%s] scheduler registered.\n", scheduler->name); |
1da177e4 LT |
217 | |
218 | return 0; | |
219 | } | |
220 | ||
221 | ||
222 | /* | |
223 | * Unregister a scheduler from the scheduler list | |
224 | */ | |
225 | int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler) | |
226 | { | |
227 | if (!scheduler) { | |
1e3e238e | 228 | pr_err("%s(): NULL arg\n", __func__); |
1da177e4 LT |
229 | return -EINVAL; |
230 | } | |
231 | ||
232 | write_lock_bh(&__ip_vs_sched_lock); | |
66a0be47 | 233 | if (list_empty(&scheduler->n_list)) { |
1da177e4 | 234 | write_unlock_bh(&__ip_vs_sched_lock); |
1e3e238e HE |
235 | pr_err("%s(): [%s] scheduler is not in the list. failed\n", |
236 | __func__, scheduler->name); | |
1da177e4 LT |
237 | return -EINVAL; |
238 | } | |
239 | ||
240 | /* | |
241 | * Remove it from the d-linked scheduler list | |
242 | */ | |
243 | list_del(&scheduler->n_list); | |
244 | write_unlock_bh(&__ip_vs_sched_lock); | |
245 | ||
246 | /* decrease the module use count */ | |
247 | ip_vs_use_count_dec(); | |
248 | ||
1e3e238e | 249 | pr_info("[%s] scheduler unregistered.\n", scheduler->name); |
1da177e4 LT |
250 | |
251 | return 0; | |
252 | } |