Refer to scheduler and processor via lib and not hydra.
[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 * currenly running initiators
70 */
71 int running;
72
73 /**
74 * delay between initiations, in ms
75 */
76 int delay;
77
78 /**
79 * mutex to lock running field
80 */
81 mutex_t *mutex;
82
83 /**
84 * condvar to wait for initiators
85 */
86 condvar_t *condvar;
87 };
88
89 /**
90 * Begin the load test
91 */
92 static job_requeue_t do_load_test(private_load_tester_plugin_t *this)
93 {
94 int i, s = 0, ms = 0;
95
96 this->mutex->lock(this->mutex);
97 if (!this->running)
98 {
99 this->running = this->initiators;
100 }
101 this->mutex->unlock(this->mutex);
102 if (this->delay)
103 {
104 s = this->delay / 1000;
105 ms = this->delay % 1000;
106 }
107
108 for (i = 0; this->iterations == 0 || i < this->iterations; i++)
109 {
110 peer_cfg_t *peer_cfg;
111 child_cfg_t *child_cfg = NULL;
112 enumerator_t *enumerator;
113
114 peer_cfg = charon->backends->get_peer_cfg_by_name(charon->backends,
115 "load-test");
116 if (!peer_cfg)
117 {
118 break;
119 }
120 enumerator = peer_cfg->create_child_cfg_enumerator(peer_cfg);
121 if (!enumerator->enumerate(enumerator, &child_cfg))
122 {
123 enumerator->destroy(enumerator);
124 break;
125 }
126 enumerator->destroy(enumerator);
127
128 charon->controller->initiate(charon->controller,
129 peer_cfg, child_cfg->get_ref(child_cfg),
130 NULL, NULL);
131 if (s)
132 {
133 sleep(s);
134 }
135 if (ms)
136 {
137 usleep(ms * 1000);
138 }
139 }
140 this->mutex->lock(this->mutex);
141 this->running--;
142 this->mutex->unlock(this->mutex);
143 this->condvar->signal(this->condvar);
144 return JOB_REQUEUE_NONE;
145 }
146
147 /**
148 * Implementation of plugin_t.destroy
149 */
150 static void destroy(private_load_tester_plugin_t *this)
151 {
152 this->iterations = -1;
153 this->mutex->lock(this->mutex);
154 while (this->running)
155 {
156 this->condvar->wait(this->condvar, this->mutex);
157 }
158 this->mutex->unlock(this->mutex);
159 hydra->kernel_interface->remove_ipsec_interface(hydra->kernel_interface,
160 (kernel_ipsec_constructor_t)load_tester_ipsec_create);
161 charon->backends->remove_backend(charon->backends, &this->config->backend);
162 lib->credmgr->remove_set(lib->credmgr, &this->creds->credential_set);
163 charon->bus->remove_listener(charon->bus, &this->listener->listener);
164 this->config->destroy(this->config);
165 this->creds->destroy(this->creds);
166 this->listener->destroy(this->listener);
167 lib->crypto->remove_dh(lib->crypto,
168 (dh_constructor_t)load_tester_diffie_hellman_create);
169 this->mutex->destroy(this->mutex);
170 this->condvar->destroy(this->condvar);
171 free(this);
172 }
173
174 /*
175 * see header file
176 */
177 plugin_t *load_tester_plugin_create()
178 {
179 private_load_tester_plugin_t *this;
180 u_int i, shutdown_on = 0;
181
182 if (!lib->settings->get_bool(lib->settings,
183 "charon.plugins.load-tester.enable", FALSE))
184 {
185 DBG1(DBG_CFG, "disabling load-tester plugin, not configured");
186 return NULL;
187 }
188
189 this = malloc_thing(private_load_tester_plugin_t);
190 this->public.plugin.destroy = (void(*)(plugin_t*))destroy;
191
192 lib->crypto->add_dh(lib->crypto, MODP_NULL,
193 (dh_constructor_t)load_tester_diffie_hellman_create);
194
195 this->delay = lib->settings->get_int(lib->settings,
196 "charon.plugins.load-tester.delay", 0);
197 this->iterations = lib->settings->get_int(lib->settings,
198 "charon.plugins.load-tester.iterations", 1);
199 this->initiators = lib->settings->get_int(lib->settings,
200 "charon.plugins.load-tester.initiators", 0);
201 if (lib->settings->get_bool(lib->settings,
202 "charon.plugins.load-tester.shutdown_when_complete", 0))
203 {
204 shutdown_on = this->iterations * this->initiators;
205 }
206
207 this->mutex = mutex_create(MUTEX_TYPE_DEFAULT);
208 this->condvar = condvar_create(CONDVAR_TYPE_DEFAULT);
209 this->config = load_tester_config_create();
210 this->creds = load_tester_creds_create();
211 this->listener = load_tester_listener_create(shutdown_on);
212 charon->backends->add_backend(charon->backends, &this->config->backend);
213 lib->credmgr->add_set(lib->credmgr, &this->creds->credential_set);
214 charon->bus->add_listener(charon->bus, &this->listener->listener);
215
216 if (lib->settings->get_bool(lib->settings,
217 "charon.plugins.load-tester.fake_kernel", FALSE))
218 {
219 hydra->kernel_interface->add_ipsec_interface(hydra->kernel_interface,
220 (kernel_ipsec_constructor_t)load_tester_ipsec_create);
221 }
222 this->running = 0;
223 for (i = 0; i < this->initiators; i++)
224 {
225 lib->processor->queue_job(lib->processor,
226 (job_t*)callback_job_create((callback_job_cb_t)do_load_test,
227 this, NULL, NULL));
228 }
229 return &this->public.plugin;
230 }
231