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
32
33
34
35static LIST_HEAD(ip_vs_schedulers);
36
37
38static DEFINE_RWLOCK(__ip_vs_sched_lock);
39
40
41
42
43
44int ip_vs_bind_scheduler(struct ip_vs_service *svc,
45 struct ip_vs_scheduler *scheduler)
46{
47 int ret;
48
49 if (svc == NULL) {
50 pr_err("%s(): svc arg NULL\n", __func__);
51 return -EINVAL;
52 }
53 if (scheduler == NULL) {
54 pr_err("%s(): scheduler arg NULL\n", __func__);
55 return -EINVAL;
56 }
57
58 svc->scheduler = scheduler;
59
60 if (scheduler->init_service) {
61 ret = scheduler->init_service(svc);
62 if (ret) {
63 pr_err("%s(): init error\n", __func__);
64 return ret;
65 }
66 }
67
68 return 0;
69}
70
71
72
73
74
75int ip_vs_unbind_scheduler(struct ip_vs_service *svc)
76{
77 struct ip_vs_scheduler *sched;
78
79 if (svc == NULL) {
80 pr_err("%s(): svc arg NULL\n", __func__);
81 return -EINVAL;
82 }
83
84 sched = svc->scheduler;
85 if (sched == NULL) {
86 pr_err("%s(): svc isn't bound\n", __func__);
87 return -EINVAL;
88 }
89
90 if (sched->done_service) {
91 if (sched->done_service(svc) != 0) {
92 pr_err("%s(): done error\n", __func__);
93 return -EINVAL;
94 }
95 }
96
97 svc->scheduler = NULL;
98 return 0;
99}
100
101
102
103
104
105static struct ip_vs_scheduler *ip_vs_sched_getbyname(const char *sched_name)
106{
107 struct ip_vs_scheduler *sched;
108
109 IP_VS_DBG(2, "%s(): sched_name \"%s\"\n", __func__, sched_name);
110
111 read_lock_bh(&__ip_vs_sched_lock);
112
113 list_for_each_entry(sched, &ip_vs_schedulers, n_list) {
114
115
116
117 if (sched->module && !try_module_get(sched->module)) {
118
119
120
121 continue;
122 }
123 if (strcmp(sched_name, sched->name)==0) {
124
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
139
140struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name)
141{
142 struct ip_vs_scheduler *sched;
143
144
145
146
147 sched = ip_vs_sched_getbyname(sched_name);
148
149
150
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
160void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler)
161{
162 if (scheduler->module)
163 module_put(scheduler->module);
164}
165
166
167
168
169
170int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler)
171{
172 struct ip_vs_scheduler *sched;
173
174 if (!scheduler) {
175 pr_err("%s(): NULL arg\n", __func__);
176 return -EINVAL;
177 }
178
179 if (!scheduler->name) {
180 pr_err("%s(): NULL scheduler_name\n", __func__);
181 return -EINVAL;
182 }
183
184
185 ip_vs_use_count_inc();
186
187 write_lock_bh(&__ip_vs_sched_lock);
188
189 if (!list_empty(&scheduler->n_list)) {
190 write_unlock_bh(&__ip_vs_sched_lock);
191 ip_vs_use_count_dec();
192 pr_err("%s(): [%s] scheduler already linked\n",
193 __func__, scheduler->name);
194 return -EINVAL;
195 }
196
197
198
199
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();
205 pr_err("%s(): [%s] scheduler already existed "
206 "in the system\n", __func__, scheduler->name);
207 return -EINVAL;
208 }
209 }
210
211
212
213 list_add(&scheduler->n_list, &ip_vs_schedulers);
214 write_unlock_bh(&__ip_vs_sched_lock);
215
216 pr_info("[%s] scheduler registered.\n", scheduler->name);
217
218 return 0;
219}
220
221
222
223
224
225int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler)
226{
227 if (!scheduler) {
228 pr_err("%s(): NULL arg\n", __func__);
229 return -EINVAL;
230 }
231
232 write_lock_bh(&__ip_vs_sched_lock);
233 if (list_empty(&scheduler->n_list)) {
234 write_unlock_bh(&__ip_vs_sched_lock);
235 pr_err("%s(): [%s] scheduler is not in the list. failed\n",
236 __func__, scheduler->name);
237 return -EINVAL;
238 }
239
240
241
242
243 list_del(&scheduler->n_list);
244 write_unlock_bh(&__ip_vs_sched_lock);
245
246
247 ip_vs_use_count_dec();
248
249 pr_info("[%s] scheduler unregistered.\n", scheduler->name);
250
251 return 0;
252}
253