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