Fix listener registration in load-tester plugin.
[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 static const char *plugin_name = "load_tester";
32
33 typedef struct private_load_tester_plugin_t private_load_tester_plugin_t;
34
35 /**
36 * private data of load_tester plugin
37 */
38 struct private_load_tester_plugin_t {
39
40 /**
41 * implements plugin interface
42 */
43 load_tester_plugin_t public;
44
45 /**
46 * load_tester configuration backend
47 */
48 load_tester_config_t *config;
49
50 /**
51 * load_tester credential set implementation
52 */
53 load_tester_creds_t *creds;
54
55 /**
56 * event handler, listens on bus
57 */
58 load_tester_listener_t *listener;
59
60 /**
61 * number of iterations per thread
62 */
63 int iterations;
64
65 /**
66 * number desired initiator threads
67 */
68 int initiators;
69
70 /**
71 * currenly running initiators
72 */
73 int running;
74
75 /**
76 * delay between initiations, in ms
77 */
78 int delay;
79
80 /**
81 * Throttle initiation if half-open IKE_SA count reached
82 */
83 int init_limit;
84
85 /**
86 * mutex to lock running field
87 */
88 mutex_t *mutex;
89
90 /**
91 * condvar to wait for initiators
92 */
93 condvar_t *condvar;
94 };
95
96 /**
97 * Begin the load test
98 */
99 static job_requeue_t do_load_test(private_load_tester_plugin_t *this)
100 {
101 int i, s = 0, ms = 0;
102
103 this->mutex->lock(this->mutex);
104 if (!this->running)
105 {
106 this->running = this->initiators;
107 }
108 this->mutex->unlock(this->mutex);
109 if (this->delay)
110 {
111 s = this->delay / 1000;
112 ms = this->delay % 1000;
113 }
114
115 for (i = 0; this->iterations == 0 || i < this->iterations; i++)
116 {
117 peer_cfg_t *peer_cfg;
118 child_cfg_t *child_cfg = NULL;
119 enumerator_t *enumerator;
120
121 if (this->init_limit)
122 {
123 while ((charon->ike_sa_manager->get_count(charon->ike_sa_manager) -
124 this->listener->get_established(this->listener)) >
125 this->init_limit)
126 {
127 if (s)
128 {
129 sleep(s);
130 }
131 if (ms)
132 {
133 usleep(ms * 1000);
134 }
135 }
136 }
137
138 peer_cfg = charon->backends->get_peer_cfg_by_name(charon->backends,
139 "load-test");
140 if (!peer_cfg)
141 {
142 break;
143 }
144 enumerator = peer_cfg->create_child_cfg_enumerator(peer_cfg);
145 if (!enumerator->enumerate(enumerator, &child_cfg))
146 {
147 enumerator->destroy(enumerator);
148 break;
149 }
150 enumerator->destroy(enumerator);
151
152 charon->controller->initiate(charon->controller,
153 peer_cfg, child_cfg->get_ref(child_cfg),
154 NULL, NULL);
155 if (s)
156 {
157 sleep(s);
158 }
159 if (ms)
160 {
161 usleep(ms * 1000);
162 }
163 }
164 this->mutex->lock(this->mutex);
165 this->running--;
166 this->mutex->unlock(this->mutex);
167 this->condvar->signal(this->condvar);
168 return JOB_REQUEUE_NONE;
169 }
170
171 METHOD(plugin_t, get_name, char*,
172 private_load_tester_plugin_t *this)
173 {
174 return "load-tester";
175 }
176
177 METHOD(plugin_t, destroy, void,
178 private_load_tester_plugin_t *this)
179 {
180 this->iterations = -1;
181 this->mutex->lock(this->mutex);
182 while (this->running)
183 {
184 this->condvar->wait(this->condvar, this->mutex);
185 }
186 this->mutex->unlock(this->mutex);
187 hydra->kernel_interface->remove_ipsec_interface(hydra->kernel_interface,
188 (kernel_ipsec_constructor_t)load_tester_ipsec_create);
189 charon->backends->remove_backend(charon->backends, &this->config->backend);
190 lib->credmgr->remove_set(lib->credmgr, &this->creds->credential_set);
191 charon->bus->remove_listener(charon->bus, &this->listener->listener);
192 this->config->destroy(this->config);
193 this->creds->destroy(this->creds);
194 this->listener->destroy(this->listener);
195 lib->crypto->remove_dh(lib->crypto,
196 (dh_constructor_t)load_tester_diffie_hellman_create);
197 this->mutex->destroy(this->mutex);
198 this->condvar->destroy(this->condvar);
199 free(this);
200 }
201
202 /*
203 * see header file
204 */
205 plugin_t *load_tester_plugin_create()
206 {
207 private_load_tester_plugin_t *this;
208 u_int i, shutdown_on = 0;
209
210 if (!lib->settings->get_bool(lib->settings,
211 "charon.plugins.load-tester.enable", FALSE))
212 {
213 DBG1(DBG_CFG, "disabling load-tester plugin, not configured");
214 return NULL;
215 }
216
217 INIT(this,
218 .public = {
219 .plugin = {
220 .get_name = _get_name,
221 .reload = (void*)return_false,
222 .destroy = _destroy,
223 },
224 },
225 .delay = lib->settings->get_int(lib->settings,
226 "charon.plugins.load-tester.delay", 0),
227 .iterations = lib->settings->get_int(lib->settings,
228 "charon.plugins.load-tester.iterations", 1),
229 .initiators = lib->settings->get_int(lib->settings,
230 "charon.plugins.load-tester.initiators", 0),
231 .init_limit = lib->settings->get_int(lib->settings,
232 "charon.plugins.load-tester.init_limit", 0),
233 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
234 .condvar = condvar_create(CONDVAR_TYPE_DEFAULT),
235 .config = load_tester_config_create(),
236 .creds = load_tester_creds_create(),
237 );
238
239 lib->crypto->add_dh(lib->crypto, MODP_NULL, plugin_name,
240 (dh_constructor_t)load_tester_diffie_hellman_create);
241 charon->backends->add_backend(charon->backends, &this->config->backend);
242 lib->credmgr->add_set(lib->credmgr, &this->creds->credential_set);
243
244 if (lib->settings->get_bool(lib->settings,
245 "charon.plugins.load-tester.shutdown_when_complete", 0))
246 {
247 shutdown_on = this->iterations * this->initiators;
248 }
249 this->listener = load_tester_listener_create(shutdown_on);
250 charon->bus->add_listener(charon->bus, &this->listener->listener);
251
252 if (lib->settings->get_bool(lib->settings,
253 "charon.plugins.load-tester.fake_kernel", FALSE))
254 {
255 hydra->kernel_interface->add_ipsec_interface(hydra->kernel_interface,
256 (kernel_ipsec_constructor_t)load_tester_ipsec_create);
257 }
258 this->running = 0;
259 for (i = 0; i < this->initiators; i++)
260 {
261 lib->processor->queue_job(lib->processor, (job_t*)
262 callback_job_create_with_prio((callback_job_cb_t)do_load_test,
263 this, NULL, NULL, JOB_PRIO_CRITICAL));
264 }
265 return &this->public.plugin;
266 }
267