Register load-tester faked kernel interface before other kernel interfaces
[strongswan.git] / src / libcharon / plugins / load_tester / load_tester_plugin.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "load_tester_plugin.h"
17 #include "load_tester_config.h"
18 #include "load_tester_creds.h"
19 #include "load_tester_ipsec.h"
20 #include "load_tester_listener.h"
21 #include "load_tester_diffie_hellman.h"
22
23 #include <unistd.h>
24
25 #include <hydra.h>
26 #include <daemon.h>
27 #include <processing/jobs/callback_job.h>
28 #include <threading/condvar.h>
29 #include <threading/mutex.h>
30
31 typedef struct private_load_tester_plugin_t private_load_tester_plugin_t;
32
33 /**
34 * private data of load_tester plugin
35 */
36 struct private_load_tester_plugin_t {
37
38 /**
39 * implements plugin interface
40 */
41 load_tester_plugin_t public;
42
43 /**
44 * load_tester configuration backend
45 */
46 load_tester_config_t *config;
47
48 /**
49 * load_tester credential set implementation
50 */
51 load_tester_creds_t *creds;
52
53 /**
54 * event handler, listens on bus
55 */
56 load_tester_listener_t *listener;
57
58 /**
59 * number of iterations per thread
60 */
61 int iterations;
62
63 /**
64 * number desired initiator threads
65 */
66 int initiators;
67
68 /**
69 * currently running initiators
70 */
71 int running;
72
73 /**
74 * delay between initiations, in ms
75 */
76 int delay;
77
78 /**
79 * Throttle initiation if half-open IKE_SA count reached
80 */
81 int init_limit;
82
83 /**
84 * mutex to lock running field
85 */
86 mutex_t *mutex;
87
88 /**
89 * condvar to wait for initiators
90 */
91 condvar_t *condvar;
92 };
93
94 /**
95 * Begin the load test
96 */
97 static job_requeue_t do_load_test(private_load_tester_plugin_t *this)
98 {
99 int i, s = 0, ms = 0;
100
101 this->mutex->lock(this->mutex);
102 this->running++;
103 this->mutex->unlock(this->mutex);
104 if (this->delay)
105 {
106 s = this->delay / 1000;
107 ms = this->delay % 1000;
108 }
109
110 for (i = 0; this->iterations == 0 || i < this->iterations; i++)
111 {
112 peer_cfg_t *peer_cfg;
113 child_cfg_t *child_cfg = NULL;
114 enumerator_t *enumerator;
115
116 if (this->init_limit)
117 {
118 while ((charon->ike_sa_manager->get_count(charon->ike_sa_manager) -
119 this->listener->get_established(this->listener)) >
120 this->init_limit)
121 {
122 if (s)
123 {
124 sleep(s);
125 }
126 if (ms)
127 {
128 usleep(ms * 1000);
129 }
130 }
131 }
132
133 peer_cfg = charon->backends->get_peer_cfg_by_name(charon->backends,
134 "load-test");
135 if (!peer_cfg)
136 {
137 break;
138 }
139 enumerator = peer_cfg->create_child_cfg_enumerator(peer_cfg);
140 if (!enumerator->enumerate(enumerator, &child_cfg))
141 {
142 enumerator->destroy(enumerator);
143 break;
144 }
145 enumerator->destroy(enumerator);
146
147 charon->controller->initiate(charon->controller,
148 peer_cfg, child_cfg->get_ref(child_cfg),
149 NULL, NULL, 0);
150 if (s)
151 {
152 sleep(s);
153 }
154 if (ms)
155 {
156 usleep(ms * 1000);
157 }
158 }
159 this->mutex->lock(this->mutex);
160 this->running--;
161 this->condvar->signal(this->condvar);
162 this->mutex->unlock(this->mutex);
163 return JOB_REQUEUE_NONE;
164 }
165
166 METHOD(plugin_t, get_name, char*,
167 private_load_tester_plugin_t *this)
168 {
169 return "load-tester";
170 }
171
172 /**
173 * Register load_tester plugin features
174 */
175 static bool register_load_tester(private_load_tester_plugin_t *this,
176 plugin_feature_t *feature, bool reg, void *data)
177 {
178 if (reg)
179 {
180 u_int i, shutdown_on = 0;
181
182 this->config = load_tester_config_create();
183 this->creds = load_tester_creds_create();
184
185 charon->backends->add_backend(charon->backends, &this->config->backend);
186 lib->credmgr->add_set(lib->credmgr, &this->creds->credential_set);
187
188 if (lib->settings->get_bool(lib->settings,
189 "%s.plugins.load-tester.shutdown_when_complete", 0, charon->name))
190 {
191 shutdown_on = this->iterations * this->initiators;
192 }
193 this->listener = load_tester_listener_create(shutdown_on);
194 charon->bus->add_listener(charon->bus, &this->listener->listener);
195
196 for (i = 0; i < this->initiators; i++)
197 {
198 lib->processor->queue_job(lib->processor, (job_t*)
199 callback_job_create_with_prio((callback_job_cb_t)do_load_test,
200 this, NULL, NULL, JOB_PRIO_CRITICAL));
201 }
202 }
203 else
204 {
205 this->iterations = -1;
206 this->mutex->lock(this->mutex);
207 while (this->running)
208 {
209 this->condvar->wait(this->condvar, this->mutex);
210 }
211 this->mutex->unlock(this->mutex);
212 charon->backends->remove_backend(charon->backends, &this->config->backend);
213 lib->credmgr->remove_set(lib->credmgr, &this->creds->credential_set);
214 charon->bus->remove_listener(charon->bus, &this->listener->listener);
215 this->config->destroy(this->config);
216 this->creds->destroy(this->creds);
217 this->listener->destroy(this->listener);
218 }
219 return TRUE;
220 }
221
222 METHOD(plugin_t, get_features, int,
223 private_load_tester_plugin_t *this, plugin_feature_t *features[])
224 {
225 static plugin_feature_t f[] = {
226 PLUGIN_REGISTER(DH, load_tester_diffie_hellman_create),
227 PLUGIN_PROVIDE(DH, MODP_NULL),
228 PLUGIN_DEPENDS(CUSTOM, "load-tester"),
229 PLUGIN_CALLBACK((plugin_feature_callback_t)register_load_tester, NULL),
230 PLUGIN_PROVIDE(CUSTOM, "load-tester"),
231 PLUGIN_SDEPEND(PRIVKEY, KEY_RSA),
232 PLUGIN_SDEPEND(CERT_DECODE, CERT_ANY),
233 PLUGIN_SDEPEND(CERT_DECODE, CERT_X509),
234 };
235 *features = f;
236 return countof(f);
237 }
238
239 METHOD(plugin_t, destroy, void,
240 private_load_tester_plugin_t *this)
241 {
242 hydra->kernel_interface->remove_ipsec_interface(hydra->kernel_interface,
243 (kernel_ipsec_constructor_t)load_tester_ipsec_create);
244 this->mutex->destroy(this->mutex);
245 this->condvar->destroy(this->condvar);
246 free(this);
247 }
248
249 /*
250 * see header file
251 */
252 plugin_t *load_tester_plugin_create()
253 {
254 private_load_tester_plugin_t *this;
255
256 if (!lib->settings->get_bool(lib->settings,
257 "%s.plugins.load-tester.enable", FALSE, charon->name))
258 {
259 DBG1(DBG_CFG, "disabling load-tester plugin, not configured");
260 return NULL;
261 }
262
263 INIT(this,
264 .public = {
265 .plugin = {
266 .get_name = _get_name,
267 .get_features = _get_features,
268 .reload = (void*)return_false,
269 .destroy = _destroy,
270 },
271 },
272 .delay = lib->settings->get_int(lib->settings,
273 "%s.plugins.load-tester.delay", 0, charon->name),
274 .iterations = lib->settings->get_int(lib->settings,
275 "%s.plugins.load-tester.iterations", 1, charon->name),
276 .initiators = lib->settings->get_int(lib->settings,
277 "%s.plugins.load-tester.initiators", 0, charon->name),
278 .init_limit = lib->settings->get_int(lib->settings,
279 "%s.plugins.load-tester.init_limit", 0, charon->name),
280 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
281 .condvar = condvar_create(CONDVAR_TYPE_DEFAULT),
282 );
283
284 if (lib->settings->get_bool(lib->settings,
285 "%s.plugins.load-tester.fake_kernel", FALSE, charon->name))
286 {
287 hydra->kernel_interface->add_ipsec_interface(hydra->kernel_interface,
288 (kernel_ipsec_constructor_t)load_tester_ipsec_create);
289 }
290 return &this->public.plugin;
291 }
292