a22445feb4664ee3dc73d131b46c9192134ec7d3
[strongswan.git] / src / charon / 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 * $Id$
16 */
17
18 #include "load_tester_plugin.h"
19 #include "load_tester_config.h"
20 #include "load_tester_creds.h"
21 #include "load_tester_ipsec.h"
22 #include "load_tester_listener.h"
23 #include "load_tester_diffie_hellman.h"
24
25 #include <unistd.h>
26
27 #include <daemon.h>
28 #include <processing/jobs/callback_job.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 of threads
64 */
65 int initiators;
66
67 /**
68 * delay between initiations, in ms
69 */
70 int delay;
71 };
72
73 /**
74 * Begin the load test
75 */
76 static job_requeue_t do_load_test(private_load_tester_plugin_t *this)
77 {
78 peer_cfg_t *peer_cfg;
79 child_cfg_t *child_cfg = NULL;;
80 enumerator_t *enumerator;
81 int i, s = 0, ms = 0;
82
83 if (this->delay)
84 {
85 s = this->delay / 1000;
86 ms = this->delay % 1000;
87 }
88 peer_cfg = charon->backends->get_peer_cfg_by_name(charon->backends,
89 "load-test");
90 if (peer_cfg)
91 {
92 enumerator = peer_cfg->create_child_cfg_enumerator(peer_cfg);
93 if (enumerator->enumerate(enumerator, &child_cfg))
94 {
95 child_cfg->get_ref(child_cfg);
96 }
97 enumerator->destroy(enumerator);
98
99 if (child_cfg)
100 {
101 for (i = 0; this->iterations == 0 || i < this->iterations; i++)
102 {
103 charon->controller->initiate(charon->controller,
104 peer_cfg->get_ref(peer_cfg), child_cfg->get_ref(child_cfg),
105 NULL, NULL);
106
107 if (s)
108 {
109 sleep(s);
110 }
111 if (ms)
112 {
113 usleep(ms * 1000);
114 }
115 }
116 child_cfg->destroy(child_cfg);
117 }
118 peer_cfg->destroy(peer_cfg);
119 }
120 return JOB_REQUEUE_NONE;
121 }
122
123 /**
124 * Implementation of plugin_t.destroy
125 */
126 static void destroy(private_load_tester_plugin_t *this)
127 {
128 charon->kernel_interface->remove_ipsec_interface(charon->kernel_interface,
129 (kernel_ipsec_constructor_t)load_tester_ipsec_create);
130 charon->backends->remove_backend(charon->backends, &this->config->backend);
131 charon->credentials->remove_set(charon->credentials, &this->creds->credential_set);
132 charon->bus->remove_listener(charon->bus, &this->listener->listener);
133 this->config->destroy(this->config);
134 this->creds->destroy(this->creds);
135 this->listener->destroy(this->listener);
136 lib->crypto->remove_dh(lib->crypto,
137 (dh_constructor_t)load_tester_diffie_hellman_create);
138 free(this);
139 }
140
141 /*
142 * see header file
143 */
144 plugin_t *plugin_create()
145 {
146 private_load_tester_plugin_t *this = malloc_thing(private_load_tester_plugin_t);
147 int i;
148
149 this->public.plugin.destroy = (void(*)(plugin_t*))destroy;
150
151 lib->crypto->add_dh(lib->crypto, MODP_NULL,
152 (dh_constructor_t)load_tester_diffie_hellman_create);
153
154 this->config = load_tester_config_create();
155 this->creds = load_tester_creds_create();
156 this->listener = load_tester_listener_create();
157 charon->backends->add_backend(charon->backends, &this->config->backend);
158 charon->credentials->add_set(charon->credentials, &this->creds->credential_set);
159 charon->bus->add_listener(charon->bus, &this->listener->listener);
160
161 if (lib->settings->get_bool(lib->settings,
162 "charon.plugins.load_tester.fake_kernel", FALSE))
163 {
164 charon->kernel_interface->add_ipsec_interface(charon->kernel_interface,
165 (kernel_ipsec_constructor_t)load_tester_ipsec_create);
166 }
167 this->delay = lib->settings->get_int(lib->settings,
168 "charon.plugins.load_tester.delay", 0);
169 this->iterations = lib->settings->get_int(lib->settings,
170 "charon.plugins.load_tester.iterations", 1);
171 this->initiators = lib->settings->get_int(lib->settings,
172 "charon.plugins.load_tester.initiators", 0);
173 for (i = 0; i < this->initiators; i++)
174 {
175 charon->processor->queue_job(charon->processor,
176 (job_t*)callback_job_create((callback_job_cb_t)do_load_test,
177 this, NULL, NULL));
178 }
179 return &this->public.plugin;
180 }
181