aboutsummaryrefslogtreecommitdiffstats
path: root/Source/charon/threads/thread_pool.c
blob: 58310ebc5de6229c0f4f423928444aec08bce323 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/**
 * @file thread_pool.c
 * 
 * @brief Implementation of thread_pool_t.
 * 
 */

/*
 * Copyright (C) 2005 Jan Hutter, Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */
 
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

#include "thread_pool.h"
 
#include <globals.h>
#include <queues/job_queue.h>
#include <queues/jobs/delete_ike_sa_job.h>
#include <queues/jobs/incoming_packet_job.h>
#include <queues/jobs/initiate_ike_sa_job.h>
#include <utils/allocator.h>
#include <utils/logger.h>

typedef struct private_thread_pool_t private_thread_pool_t;

/**
 * @brief Structure with private members for thread_pool_t.
 */
struct private_thread_pool_t {
	/**
	 * inclusion of public members
	 */
	thread_pool_t public;
	
	/**
	 * @brief Main processing functino for worker threads.
	 *
	 * Gets a job from the job queue and calls corresponding
	 * function for processing.
	 * 
	 * @param this	private_thread_pool_t-Object
	 */
	void (*process_jobs) (private_thread_pool_t *this);

	/**
	 * @brief Process a INCOMING_PACKET_JOB.
	 * 
	 * @param this	private_thread_pool_t-Object
	 */
	void (*process_incoming_packet_job) (private_thread_pool_t *this, incoming_packet_job_t *job);

	/**
	 * @brief Process a INITIATE_IKE_SA_JOB.
	 * 
	 * @param this	private_thread_pool_t-Object
	 */
	void (*process_initiate_ike_sa_job) (private_thread_pool_t *this, initiate_ike_sa_job_t *job);

	/**
	 * @brief Process a DELETE_IKE_SA_JOB.
	 * 
	 * @param this	private_thread_pool_t-Object
	 */
	void (*process_delete_ike_sa_job) (private_thread_pool_t *this, delete_ike_sa_job_t *job);
	
	/**
	 * number of running threads
	 */
	size_t pool_size;
	
	/**
	 * array of thread ids
	 */
	pthread_t *threads;
	
	/**
	 * logger of the threadpool
	 */
	logger_t *pool_logger;
	
	/**
	 * logger of the worker threads
	 */
	logger_t *worker_logger;
} ;



/**
 * implements private_thread_pool_t.function
 */
static void process_jobs(private_thread_pool_t *this)
{
	/* cancellation disabled by default */
	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
	
	this->worker_logger->log(this->worker_logger, CONTROL, "worker thread running, pid: %d", getpid());

	for (;;) {
		job_t *job;
		job_type_t job_type;
		
		job = global_job_queue->get(global_job_queue);
		job_type = job->get_type(job);
		this->worker_logger->log(this->worker_logger, CONTROL|MORE, "got a job of type %s", 
								 mapping_find(job_type_m,job_type));
		
		switch (job_type)
		{
			case INCOMING_PACKET:
			{
				this->process_incoming_packet_job(this, (incoming_packet_job_t*)job);
				break;
			}
			case INITIATE_IKE_SA:
			{
				this->process_initiate_ike_sa_job(this, (initiate_ike_sa_job_t*)job);
				break;
			}
			case DELETE_IKE_SA:
			{
				this->process_delete_ike_sa_job(this, (delete_ike_sa_job_t*)job);
				break;
			}
			default:
			{
				this->worker_logger->log(this->worker_logger, ERROR, "job of type %s not supported!", 
										 mapping_find(job_type_m,job_type));				
				break;
			}
		}
		job->destroy(job);
	}
}

/**
 * implementation of private_thread_pool_t.process_incoming_packet_job
 */
static void process_incoming_packet_job(private_thread_pool_t *this, incoming_packet_job_t *job)
{
	packet_t 	*packet;
	message_t 	*message;
	ike_sa_t 	*ike_sa;
	ike_sa_id_t *ike_sa_id;
	status_t 	status;
	
	
	packet = job->get_packet(job);
				
	message = message_create_from_packet(packet);

	status = message->parse_header(message);
	if (status != SUCCESS)
	{
		this->worker_logger->log(this->worker_logger, ERROR, "message header could not be verified!");				
		message->destroy(message);
		return;										
	}
				
	this->worker_logger->log(this->worker_logger, CONTROL|MOST, "message is a %s %s", 
							 mapping_find(exchange_type_m, message->get_exchange_type(message)),
							 message->get_request(message) ? "request" : "reply");
				
	if ((message->get_major_version(message) != IKE_MAJOR_VERSION) || 
			(message->get_minor_version(message) != IKE_MINOR_VERSION))
	{
		this->worker_logger->log(this->worker_logger, ERROR, "IKE version %d.%d not supported", 
								 message->get_major_version(message),
								 message->get_minor_version(message));	
		/* Todo send notify */
	}
				
	message->get_ike_sa_id(message, &ike_sa_id);
			
	ike_sa_id->switch_initiator(ike_sa_id);
				
	this->worker_logger->log(this->worker_logger, CONTROL|MOST, "checking out IKE SA %lld:%lld, role %s", 
							 ike_sa_id->get_initiator_spi(ike_sa_id),
							 ike_sa_id->get_responder_spi(ike_sa_id),
							 ike_sa_id->is_initiator(ike_sa_id) ? "initiator" : "responder");
				
	status = global_ike_sa_manager->checkout(global_ike_sa_manager,ike_sa_id, &ike_sa);
	if (status != SUCCESS)
	{
		this->worker_logger->log(this->worker_logger, ERROR, "IKE SA could not be checked out");
		ike_sa_id->destroy(ike_sa_id);	
		message->destroy(message);
		return;
	}
				
	status = ike_sa->process_message(ike_sa, message);				
	if (status != SUCCESS)
	{
		this->worker_logger->log(this->worker_logger, ERROR, "message could not be processed by IKE SA");
	}
				
	this->worker_logger->log(this->worker_logger, CONTROL|MOST, "checking in IKE SA %lld:%lld, role %s", 
							 ike_sa_id->get_initiator_spi(ike_sa_id),
							 ike_sa_id->get_responder_spi(ike_sa_id),
							 ike_sa_id->is_initiator(ike_sa_id) ? "initiator" : "responder");
	ike_sa_id->destroy(ike_sa_id);
									
	status = global_ike_sa_manager->checkin(global_ike_sa_manager, ike_sa);
	if (status != SUCCESS)
	{
		this->worker_logger->log(this->worker_logger, ERROR, "checkin of IKE SA failed");
	}
	message->destroy(message);
}

/**
 * implementation of private_thread_pool_t.process_initiate_ike_sa_job
 */
static void process_initiate_ike_sa_job(private_thread_pool_t *this, initiate_ike_sa_job_t *job)
{
	/*
	 * Initiatie an IKE_SA:
	 * - is defined by a name of a configuration
	 * - create an empty IKE_SA via manager
	 * - call initiate_connection on this sa
	 */
	ike_sa_t *ike_sa;
	status_t status;
	
	
	this->worker_logger->log(this->worker_logger, CONTROL|MOST, "create and checking out IKE SA");
	
	global_ike_sa_manager->create_and_checkout(global_ike_sa_manager, &ike_sa);
	
	this->worker_logger->log(this->worker_logger, CONTROL|MOST, "initializing connection \"%s\"", 
							 job->get_configuration_name(job));
	status = ike_sa->initialize_connection(ike_sa, job->get_configuration_name(job));
	if (status != SUCCESS)
	{
		this->worker_logger->log(this->worker_logger, ERROR, "%s by initialize_conection, job and rejected, IKE_SA deleted.", 
								 mapping_find(status_m, status));
		global_ike_sa_manager->checkin_and_delete(global_ike_sa_manager, ike_sa);
		return;
	}
	
	this->worker_logger->log(this->worker_logger, CONTROL|MOST, "checking in IKE SA");
	status = global_ike_sa_manager->checkin(global_ike_sa_manager, ike_sa);
	if (status != SUCCESS)
	{
		this->worker_logger->log(this->worker_logger, ERROR, "%s could not checkin IKE_SA.", 
								 mapping_find(status_m, status));
	}
}

/**
 * implementation of private_thread_pool_t.process_delete_ike_sa_job
 */
static void process_delete_ike_sa_job(private_thread_pool_t *this, delete_ike_sa_job_t *job)
{
	status_t status;
	ike_sa_id_t *ike_sa_id = job->get_ike_sa_id(job);
										
	this->worker_logger->log(this->worker_logger, CONTROL|MOST, "deleting IKE SA %lld:%lld, role %s", 
							 ike_sa_id->get_initiator_spi(ike_sa_id),
							 ike_sa_id->get_responder_spi(ike_sa_id),
							 ike_sa_id->is_initiator(ike_sa_id) ? "initiator" : "responder");
	
	status = global_ike_sa_manager->delete(global_ike_sa_manager, ike_sa_id);
	if (status != SUCCESS)
	{
		this->worker_logger->log(this->worker_logger, ERROR, "could not delete IKE_SA (%s)", 
								 mapping_find(status_m, status));
	}	
}


/**
 * implementation of thread_pool_t.get_pool_size
 */
static size_t get_pool_size(private_thread_pool_t *this)
{
	return this->pool_size;
}

/**
 * Implementation of thread_pool_t.destroy
 */
static void destroy(private_thread_pool_t *this)
{	
	int current;
	/* flag thread for termination */
	for (current = 0; current < this->pool_size; current++) {
		this->pool_logger->log(this->pool_logger, CONTROL, "cancelling worker a thread #%d", current+1);
		pthread_cancel(this->threads[current]);
	}
	
	/* wait for all threads */
	for (current = 0; current < this->pool_size; current++) {
		pthread_join(this->threads[current], NULL);
		this->pool_logger->log(this->pool_logger, CONTROL, "worker thread #%d terminated", current+1);
	}	

	/* free mem */
	global_logger_manager->destroy_logger(global_logger_manager, this->pool_logger);
	global_logger_manager->destroy_logger(global_logger_manager, this->worker_logger);
	allocator_free(this->threads);
	allocator_free(this);
}

/*
 * see header
 */
thread_pool_t *thread_pool_create(size_t pool_size)
{
	int current;
	
	private_thread_pool_t *this = allocator_alloc_thing(private_thread_pool_t);
	
	/* fill in public fields */
	this->public.destroy = (void(*)(thread_pool_t*))destroy;
	this->public.get_pool_size = (size_t(*)(thread_pool_t*))get_pool_size;
	
	this->process_jobs = process_jobs;
	this->process_initiate_ike_sa_job = process_initiate_ike_sa_job;
	this->process_delete_ike_sa_job = process_delete_ike_sa_job;
	this->process_incoming_packet_job = process_incoming_packet_job;
	this->pool_size = pool_size;
	
	this->threads = allocator_alloc(sizeof(pthread_t) * pool_size);

	this->pool_logger = global_logger_manager->create_logger(global_logger_manager,THREAD_POOL,NULL);

	this->worker_logger = global_logger_manager->create_logger(global_logger_manager,WORKER,NULL);
	
	/* try to create as many threads as possible, up tu pool_size */
	for (current = 0; current < pool_size; current++) 
	{
		if (pthread_create(&(this->threads[current]), NULL, (void*(*)(void*))this->process_jobs, this) == 0) 
		{
			this->pool_logger->log(this->pool_logger, CONTROL, "created worker thread #%d", current+1);
		}
		else
		{
			/* creation failed, is it the first one? */	
			if (current == 0) 
			{
				this->pool_logger->log(this->pool_logger, ERROR, "could not create any thread");
				global_logger_manager->destroy_logger(global_logger_manager, this->pool_logger);
				global_logger_manager->destroy_logger(global_logger_manager, this->worker_logger);
				allocator_free(this->threads);
				allocator_free(this);
				return NULL;
			}
			/* not all threads could be created, but at least one :-/ */
			this->pool_logger->log(this->pool_logger, ERROR, "could only create %d from requested %d threads!", current, pool_size);
				
			this->pool_size = current;
			return (thread_pool_t*)this;
		}
	}	
	return (thread_pool_t*)this;
}