1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#define KMSG_COMPONENT "IPVS"
21#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
22
23#include <linux/module.h>
24#include <linux/spinlock.h>
25#include <linux/interrupt.h>
26#include <asm/string.h>
27#include <linux/kmod.h>
28#include <linux/sysctl.h>
29
30#include <net/ip_vs.h>
31
32EXPORT_SYMBOL(ip_vs_scheduler_err);
33
34
35
36static LIST_HEAD(ip_vs_schedulers);
37
38
39static DEFINE_MUTEX(ip_vs_sched_mutex);
40
41
42
43
44
45int ip_vs_bind_scheduler(struct ip_vs_service *svc,
46 struct ip_vs_scheduler *scheduler)
47{
48 int ret;
49
50 if (scheduler->init_service) {
51 ret = scheduler->init_service(svc);
52 if (ret) {
53 pr_err("%s(): init error\n", __func__);
54 return ret;
55 }
56 }
57 rcu_assign_pointer(svc->scheduler, scheduler);
58 return 0;
59}
60
61
62
63
64
65void ip_vs_unbind_scheduler(struct ip_vs_service *svc,
66 struct ip_vs_scheduler *sched)
67{
68 struct ip_vs_scheduler *cur_sched;
69
70 cur_sched = rcu_dereference_protected(svc->scheduler, 1);
71
72 if (!cur_sched)
73 return;
74
75 if (sched->done_service)
76 sched->done_service(svc);
77
78}
79
80
81
82
83
84static struct ip_vs_scheduler *ip_vs_sched_getbyname(const char *sched_name)
85{
86 struct ip_vs_scheduler *sched;
87
88 IP_VS_DBG(2, "%s(): sched_name \"%s\"\n", __func__, sched_name);
89
90 mutex_lock(&ip_vs_sched_mutex);
91
92 list_for_each_entry(sched, &ip_vs_schedulers, n_list) {
93
94
95
96 if (sched->module && !try_module_get(sched->module)) {
97
98
99
100 continue;
101 }
102 if (strcmp(sched_name, sched->name)==0) {
103
104 mutex_unlock(&ip_vs_sched_mutex);
105 return sched;
106 }
107 module_put(sched->module);
108 }
109
110 mutex_unlock(&ip_vs_sched_mutex);
111 return NULL;
112}
113
114
115
116
117
118struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name)
119{
120 struct ip_vs_scheduler *sched;
121
122
123
124
125 sched = ip_vs_sched_getbyname(sched_name);
126
127
128
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
138void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler)
139{
140 if (scheduler)
141 module_put(scheduler->module);
142}
143
144
145
146
147
148void ip_vs_scheduler_err(struct ip_vs_service *svc, const char *msg)
149{
150 struct ip_vs_scheduler *sched = rcu_dereference(svc->scheduler);
151 char *sched_name = sched ? sched->name : "none";
152
153 if (svc->fwmark) {
154 IP_VS_ERR_RL("%s: FWM %u 0x%08X - %s\n",
155 sched_name, svc->fwmark, svc->fwmark, msg);
156#ifdef CONFIG_IP_VS_IPV6
157 } else if (svc->af == AF_INET6) {
158 IP_VS_ERR_RL("%s: %s [%pI6c]:%d - %s\n",
159 sched_name, ip_vs_proto_name(svc->protocol),
160 &svc->addr.in6, ntohs(svc->port), msg);
161#endif
162 } else {
163 IP_VS_ERR_RL("%s: %s %pI4:%d - %s\n",
164 sched_name, ip_vs_proto_name(svc->protocol),
165 &svc->addr.ip, ntohs(svc->port), msg);
166 }
167}
168
169
170
171
172int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler)
173{
174 struct ip_vs_scheduler *sched;
175
176 if (!scheduler) {
177 pr_err("%s(): NULL arg\n", __func__);
178 return -EINVAL;
179 }
180
181 if (!scheduler->name) {
182 pr_err("%s(): NULL scheduler_name\n", __func__);
183 return -EINVAL;
184 }
185
186
187 if (!ip_vs_use_count_inc())
188 return -ENOENT;
189
190 mutex_lock(&ip_vs_sched_mutex);
191
192 if (!list_empty(&scheduler->n_list)) {
193 mutex_unlock(&ip_vs_sched_mutex);
194 ip_vs_use_count_dec();
195 pr_err("%s(): [%s] scheduler already linked\n",
196 __func__, scheduler->name);
197 return -EINVAL;
198 }
199
200
201
202
203
204 list_for_each_entry(sched, &ip_vs_schedulers, n_list) {
205 if (strcmp(scheduler->name, sched->name) == 0) {
206 mutex_unlock(&ip_vs_sched_mutex);
207 ip_vs_use_count_dec();
208 pr_err("%s(): [%s] scheduler already existed "
209 "in the system\n", __func__, scheduler->name);
210 return -EINVAL;
211 }
212 }
213
214
215
216 list_add(&scheduler->n_list, &ip_vs_schedulers);
217 mutex_unlock(&ip_vs_sched_mutex);
218
219 pr_info("[%s] scheduler registered.\n", scheduler->name);
220
221 return 0;
222}
223
224
225
226
227
228int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler)
229{
230 if (!scheduler) {
231 pr_err("%s(): NULL arg\n", __func__);
232 return -EINVAL;
233 }
234
235 mutex_lock(&ip_vs_sched_mutex);
236 if (list_empty(&scheduler->n_list)) {
237 mutex_unlock(&ip_vs_sched_mutex);
238 pr_err("%s(): [%s] scheduler is not in the list. failed\n",
239 __func__, scheduler->name);
240 return -EINVAL;
241 }
242
243
244
245
246 list_del(&scheduler->n_list);
247 mutex_unlock(&ip_vs_sched_mutex);
248
249
250 ip_vs_use_count_dec();
251
252 pr_info("[%s] scheduler unregistered.\n", scheduler->name);
253
254 return 0;
255}
256