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