2005-11-03 13:34:07 +00:00
|
|
|
/**
|
2005-11-04 09:37:03 +00:00
|
|
|
* @file thread_pool.c
|
2005-11-03 13:34:07 +00:00
|
|
|
*
|
2005-11-25 13:42:58 +00:00
|
|
|
* @brief Implementation of thread_pool_t.
|
2005-11-03 13:34:07 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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>
|
2005-11-10 09:17:23 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2005-11-11 13:31:52 +00:00
|
|
|
|
|
|
|
#include "thread_pool.h"
|
2005-11-07 13:12:26 +00:00
|
|
|
|
2005-11-29 10:25:07 +00:00
|
|
|
#include <daemon.h>
|
2005-11-23 09:24:35 +00:00
|
|
|
#include <queues/job_queue.h>
|
2005-11-23 09:43:14 +00:00
|
|
|
#include <queues/jobs/delete_ike_sa_job.h>
|
|
|
|
#include <queues/jobs/incoming_packet_job.h>
|
|
|
|
#include <queues/jobs/initiate_ike_sa_job.h>
|
2005-12-02 13:20:20 +00:00
|
|
|
#include <queues/jobs/retransmit_request_job.h>
|
2005-12-05 12:21:38 +00:00
|
|
|
#include <encoding/payloads/notify_payload.h>
|
2005-11-23 09:24:35 +00:00
|
|
|
#include <utils/allocator.h>
|
|
|
|
#include <utils/logger.h>
|
2005-11-03 17:26:30 +00:00
|
|
|
|
2005-11-24 11:30:19 +00:00
|
|
|
typedef struct private_thread_pool_t private_thread_pool_t;
|
|
|
|
|
2005-11-03 13:34:07 +00:00
|
|
|
/**
|
2005-11-25 13:42:58 +00:00
|
|
|
* @brief Structure with private members for thread_pool_t.
|
2005-11-03 13:34:07 +00:00
|
|
|
*/
|
2005-11-24 11:30:19 +00:00
|
|
|
struct private_thread_pool_t {
|
2005-11-03 13:34:07 +00:00
|
|
|
/**
|
|
|
|
* inclusion of public members
|
|
|
|
*/
|
|
|
|
thread_pool_t public;
|
2005-11-25 13:42:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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);
|
|
|
|
|
|
|
|
/**
|
2005-12-02 13:20:20 +00:00
|
|
|
* @brief Process a INCOMING_PACKET job.
|
2005-11-25 13:42:58 +00:00
|
|
|
*
|
2005-12-02 13:20:20 +00:00
|
|
|
* @param this private_thread_pool_t object
|
|
|
|
* @param job incoming_packet_job_t object
|
2005-11-25 13:42:58 +00:00
|
|
|
*/
|
|
|
|
void (*process_incoming_packet_job) (private_thread_pool_t *this, incoming_packet_job_t *job);
|
|
|
|
|
|
|
|
/**
|
2005-12-02 13:20:20 +00:00
|
|
|
* @brief Process a INITIATE_IKE_SA job.
|
2005-11-25 13:42:58 +00:00
|
|
|
*
|
2005-12-02 13:20:20 +00:00
|
|
|
* @param this private_thread_pool_t object
|
|
|
|
* @param job initiate_ike_sa_job_t object
|
2005-11-25 13:42:58 +00:00
|
|
|
*/
|
|
|
|
void (*process_initiate_ike_sa_job) (private_thread_pool_t *this, initiate_ike_sa_job_t *job);
|
|
|
|
|
2005-11-10 15:06:52 +00:00
|
|
|
/**
|
2005-12-02 13:20:20 +00:00
|
|
|
* @brief Process a DELETE_IKE_SA job.
|
2005-11-10 15:06:52 +00:00
|
|
|
*
|
2005-12-02 13:20:20 +00:00
|
|
|
* @param this private_thread_pool_t object
|
|
|
|
* @param job delete_ike_sa_job_t object
|
2005-11-10 15:06:52 +00:00
|
|
|
*/
|
2005-11-25 13:42:58 +00:00
|
|
|
void (*process_delete_ike_sa_job) (private_thread_pool_t *this, delete_ike_sa_job_t *job);
|
|
|
|
|
2005-12-02 13:20:20 +00:00
|
|
|
/**
|
|
|
|
* @brief Process a RETRANSMIT_REQUEST job.
|
|
|
|
*
|
|
|
|
* @param this private_thread_pool_t object
|
|
|
|
* @param job retransmit_request_job_t object
|
|
|
|
*/
|
|
|
|
void (*process_retransmit_request_job) (private_thread_pool_t *this, retransmit_request_job_t *job);
|
|
|
|
|
2005-11-03 13:34:07 +00:00
|
|
|
/**
|
|
|
|
* number of running threads
|
|
|
|
*/
|
2005-11-28 20:29:47 +00:00
|
|
|
size_t pool_size;
|
|
|
|
|
2005-11-03 13:34:07 +00:00
|
|
|
/**
|
|
|
|
* array of thread ids
|
|
|
|
*/
|
|
|
|
pthread_t *threads;
|
2005-11-28 20:29:47 +00:00
|
|
|
|
2005-11-10 09:17:23 +00:00
|
|
|
/**
|
|
|
|
* logger of the threadpool
|
|
|
|
*/
|
2005-11-21 11:48:04 +00:00
|
|
|
logger_t *pool_logger;
|
2005-11-28 20:29:47 +00:00
|
|
|
|
2005-11-21 11:48:04 +00:00
|
|
|
/**
|
2005-11-25 13:42:58 +00:00
|
|
|
* logger of the worker threads
|
2005-11-21 11:48:04 +00:00
|
|
|
*/
|
|
|
|
logger_t *worker_logger;
|
2005-11-10 15:06:52 +00:00
|
|
|
} ;
|
2005-11-03 13:34:07 +00:00
|
|
|
|
2005-11-10 15:06:52 +00:00
|
|
|
/**
|
2005-12-02 13:20:20 +00:00
|
|
|
* Implementation of private_thread_pool_t.process_jobs.
|
2005-11-10 15:06:52 +00:00
|
|
|
*/
|
2005-11-25 13:42:58 +00:00
|
|
|
static void process_jobs(private_thread_pool_t *this)
|
2005-11-03 13:34:07 +00:00
|
|
|
{
|
2005-12-05 12:21:38 +00:00
|
|
|
job_t *job;
|
|
|
|
job_type_t job_type;
|
|
|
|
timeval_t start_time;
|
|
|
|
timeval_t end_time;
|
|
|
|
|
2005-11-04 13:20:11 +00:00
|
|
|
/* cancellation disabled by default */
|
|
|
|
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
|
2005-11-26 15:44:40 +00:00
|
|
|
|
2005-12-04 11:46:58 +00:00
|
|
|
this->worker_logger->log(this->worker_logger, CONTROL, "worker thread running, thread_id: %u", (int)pthread_self());
|
2005-11-09 09:07:53 +00:00
|
|
|
|
2005-11-03 13:34:07 +00:00
|
|
|
for (;;) {
|
2005-11-16 16:50:13 +00:00
|
|
|
|
2005-11-29 10:25:07 +00:00
|
|
|
job = charon->job_queue->get(charon->job_queue);
|
2005-11-16 16:50:13 +00:00
|
|
|
job_type = job->get_type(job);
|
2005-12-02 13:20:20 +00:00
|
|
|
this->worker_logger->log(this->worker_logger, CONTROL|MORE, "Process job of type %s",
|
2005-11-25 13:42:58 +00:00
|
|
|
mapping_find(job_type_m,job_type));
|
2005-12-05 12:21:38 +00:00
|
|
|
gettimeofday(&start_time,NULL);
|
2005-11-16 16:50:13 +00:00
|
|
|
switch (job_type)
|
|
|
|
{
|
|
|
|
case INCOMING_PACKET:
|
|
|
|
{
|
2005-11-25 13:42:58 +00:00
|
|
|
this->process_incoming_packet_job(this, (incoming_packet_job_t*)job);
|
2005-12-02 13:20:20 +00:00
|
|
|
job->destroy(job);
|
2005-11-25 13:42:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case INITIATE_IKE_SA:
|
|
|
|
{
|
|
|
|
this->process_initiate_ike_sa_job(this, (initiate_ike_sa_job_t*)job);
|
2005-12-02 13:20:20 +00:00
|
|
|
job->destroy(job);
|
2005-11-25 13:42:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DELETE_IKE_SA:
|
|
|
|
{
|
|
|
|
this->process_delete_ike_sa_job(this, (delete_ike_sa_job_t*)job);
|
2005-12-02 13:20:20 +00:00
|
|
|
job->destroy(job);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RETRANSMIT_REQUEST:
|
|
|
|
{
|
|
|
|
this->process_retransmit_request_job(this, (retransmit_request_job_t*)job);
|
2005-11-25 13:42:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
this->worker_logger->log(this->worker_logger, ERROR, "job of type %s not supported!",
|
|
|
|
mapping_find(job_type_m,job_type));
|
2005-12-02 13:20:20 +00:00
|
|
|
job->destroy(job);
|
2005-11-25 13:42:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-12-05 12:21:38 +00:00
|
|
|
gettimeofday(&end_time,NULL);
|
|
|
|
|
|
|
|
this->worker_logger->log(this->worker_logger, CONTROL, "Processed job of type %s in %d us",
|
|
|
|
mapping_find(job_type_m,job_type),
|
|
|
|
(((end_time.tv_sec - start_time.tv_sec) * 1000000) + (end_time.tv_usec - start_time.tv_usec)));
|
2005-12-02 13:20:20 +00:00
|
|
|
|
|
|
|
|
2005-11-25 13:42:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-12-02 13:20:20 +00:00
|
|
|
* Implementation of private_thread_pool_t.process_incoming_packet_job.
|
2005-11-25 13:42:58 +00:00
|
|
|
*/
|
2005-11-28 20:29:47 +00:00
|
|
|
static void process_incoming_packet_job(private_thread_pool_t *this, incoming_packet_job_t *job)
|
2005-11-25 13:42:58 +00:00
|
|
|
{
|
|
|
|
packet_t *packet;
|
|
|
|
message_t *message;
|
|
|
|
ike_sa_t *ike_sa;
|
|
|
|
ike_sa_id_t *ike_sa_id;
|
|
|
|
status_t status;
|
2005-11-28 20:29:47 +00:00
|
|
|
|
2005-11-29 08:08:03 +00:00
|
|
|
|
|
|
|
packet = job->get_packet(job);
|
2005-11-21 16:41:24 +00:00
|
|
|
|
2005-11-25 13:42:58 +00:00
|
|
|
message = message_create_from_packet(packet);
|
2005-11-17 11:21:01 +00:00
|
|
|
|
2005-11-25 13:42:58 +00:00
|
|
|
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;
|
|
|
|
}
|
2005-11-17 08:48:01 +00:00
|
|
|
|
2005-11-25 13:42:58 +00:00
|
|
|
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");
|
2005-11-21 11:48:04 +00:00
|
|
|
|
2005-11-25 13:42:58 +00:00
|
|
|
if ((message->get_major_version(message) != IKE_MAJOR_VERSION) ||
|
|
|
|
(message->get_minor_version(message) != IKE_MINOR_VERSION))
|
2005-12-05 12:21:38 +00:00
|
|
|
|
2005-11-25 13:42:58 +00:00
|
|
|
{
|
|
|
|
this->worker_logger->log(this->worker_logger, ERROR, "IKE version %d.%d not supported",
|
|
|
|
message->get_major_version(message),
|
|
|
|
message->get_minor_version(message));
|
2005-12-02 11:38:56 +00:00
|
|
|
/*
|
2005-12-05 12:21:38 +00:00
|
|
|
* TODO send notify reply of type INVALID_MAJOR_VERSION for requests of type IKE_SA_INIT.
|
|
|
|
*
|
|
|
|
* This check is not handled in state_t object of IKE_SA to increase speed.
|
2005-12-02 11:38:56 +00:00
|
|
|
*/
|
2005-12-05 12:21:38 +00:00
|
|
|
if ((message->get_exchange_type(message) == IKE_SA_INIT) && (message->get_request(message)))
|
|
|
|
{
|
|
|
|
message_t *response;
|
|
|
|
message->get_ike_sa_id(message, &ike_sa_id);
|
|
|
|
ike_sa_id->switch_initiator(ike_sa_id);
|
|
|
|
response = message_create_notify_reply(message->get_destination(message),
|
|
|
|
message->get_source(message),
|
|
|
|
IKE_SA_INIT,
|
|
|
|
FALSE,ike_sa_id,INVALID_MAJOR_VERSION);
|
|
|
|
|
|
|
|
message->destroy(message);
|
|
|
|
ike_sa_id->destroy(ike_sa_id);
|
|
|
|
status = response->generate(response, NULL, NULL, &packet);
|
|
|
|
if (status != SUCCESS)
|
|
|
|
{
|
|
|
|
this->worker_logger->log(this->worker_logger, ERROR, "Could not generate packet from message");
|
|
|
|
response->destroy(response);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this->worker_logger->log(this->worker_logger, ERROR, "Send notify reply of type INVALID_MAJOR_VERSION");
|
|
|
|
charon->send_queue->add(charon->send_queue, packet);
|
|
|
|
response->destroy(response);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
message->destroy(message);
|
|
|
|
|
|
|
|
return;
|
2005-11-25 13:42:58 +00:00
|
|
|
}
|
2005-11-17 08:48:01 +00:00
|
|
|
|
2005-11-29 08:08:03 +00:00
|
|
|
message->get_ike_sa_id(message, &ike_sa_id);
|
2005-11-21 17:50:56 +00:00
|
|
|
|
2005-11-25 13:42:58 +00:00
|
|
|
ike_sa_id->switch_initiator(ike_sa_id);
|
2005-11-16 17:20:03 +00:00
|
|
|
|
2005-11-25 13:42:58 +00:00
|
|
|
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");
|
2005-11-21 11:48:04 +00:00
|
|
|
|
2005-11-29 10:25:07 +00:00
|
|
|
status = charon->ike_sa_manager->checkout(charon->ike_sa_manager,ike_sa_id, &ike_sa);
|
2005-11-25 13:42:58 +00:00
|
|
|
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);
|
2005-12-02 11:38:56 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO send notify reply of type INVALID_IKE_SPI if SPI could not be found
|
|
|
|
*/
|
|
|
|
|
2005-11-25 13:42:58 +00:00
|
|
|
return;
|
|
|
|
}
|
2005-11-17 11:21:01 +00:00
|
|
|
|
2005-11-25 13:42:58 +00:00
|
|
|
status = ike_sa->process_message(ike_sa, message);
|
2005-12-02 11:38:56 +00:00
|
|
|
if ((status != SUCCESS) && (status != DELETE_ME))
|
2005-11-25 13:42:58 +00:00
|
|
|
{
|
|
|
|
this->worker_logger->log(this->worker_logger, ERROR, "message could not be processed by IKE SA");
|
|
|
|
}
|
2005-11-16 17:20:03 +00:00
|
|
|
|
2005-12-02 11:38:56 +00:00
|
|
|
this->worker_logger->log(this->worker_logger, CONTROL|MOST, "%s IKE SA %lld:%lld, role %s",
|
|
|
|
(status == DELETE_ME) ? "Checkin and delete" : "Checkin",
|
2005-11-25 13:42:58 +00:00
|
|
|
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);
|
2005-12-02 07:43:05 +00:00
|
|
|
|
|
|
|
if (status == DELETE_ME)
|
|
|
|
{
|
|
|
|
status = charon->ike_sa_manager->checkin_and_delete(charon->ike_sa_manager, ike_sa);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
|
|
|
|
}
|
|
|
|
|
2005-11-25 13:42:58 +00:00
|
|
|
if (status != SUCCESS)
|
|
|
|
{
|
2005-12-02 07:43:05 +00:00
|
|
|
this->worker_logger->log(this->worker_logger, ERROR, "checkin of IKE SA failed!");
|
2005-11-25 13:42:58 +00:00
|
|
|
}
|
|
|
|
message->destroy(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-12-02 13:20:20 +00:00
|
|
|
* Implementation of private_thread_pool_t.process_initiate_ike_sa_job.
|
2005-11-25 13:42:58 +00:00
|
|
|
*/
|
2005-11-28 20:29:47 +00:00
|
|
|
static void process_initiate_ike_sa_job(private_thread_pool_t *this, initiate_ike_sa_job_t *job)
|
2005-11-25 13:42:58 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* 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;
|
2005-11-28 20:29:47 +00:00
|
|
|
|
|
|
|
|
2005-11-25 13:42:58 +00:00
|
|
|
this->worker_logger->log(this->worker_logger, CONTROL|MOST, "create and checking out IKE SA");
|
2005-11-28 20:29:47 +00:00
|
|
|
|
2005-11-29 10:25:07 +00:00
|
|
|
charon->ike_sa_manager->create_and_checkout(charon->ike_sa_manager, &ike_sa);
|
2005-11-28 20:29:47 +00:00
|
|
|
|
2005-11-25 13:42:58 +00:00
|
|
|
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)
|
|
|
|
{
|
2005-12-02 07:43:05 +00:00
|
|
|
this->worker_logger->log(this->worker_logger, ERROR, "%s by initialize_conection, going to delete IKE_SA.",
|
2005-11-25 13:42:58 +00:00
|
|
|
mapping_find(status_m, status));
|
2005-11-29 10:25:07 +00:00
|
|
|
charon->ike_sa_manager->checkin_and_delete(charon->ike_sa_manager, ike_sa);
|
2005-11-25 13:42:58 +00:00
|
|
|
return;
|
|
|
|
}
|
2005-11-28 20:29:47 +00:00
|
|
|
|
2005-11-25 13:42:58 +00:00
|
|
|
this->worker_logger->log(this->worker_logger, CONTROL|MOST, "checking in IKE SA");
|
2005-11-29 10:25:07 +00:00
|
|
|
status = charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
|
2005-11-25 13:42:58 +00:00
|
|
|
if (status != SUCCESS)
|
|
|
|
{
|
|
|
|
this->worker_logger->log(this->worker_logger, ERROR, "%s could not checkin IKE_SA.",
|
|
|
|
mapping_find(status_m, status));
|
2005-11-03 13:34:07 +00:00
|
|
|
}
|
2005-11-25 13:42:58 +00:00
|
|
|
}
|
2005-11-09 09:07:53 +00:00
|
|
|
|
2005-11-25 13:42:58 +00:00
|
|
|
/**
|
2005-12-02 13:20:20 +00:00
|
|
|
* Implementation of private_thread_pool_t.process_delete_ike_sa_job.
|
2005-11-25 13:42:58 +00:00
|
|
|
*/
|
2005-11-28 20:29:47 +00:00
|
|
|
static void process_delete_ike_sa_job(private_thread_pool_t *this, delete_ike_sa_job_t *job)
|
2005-11-25 13:42:58 +00:00
|
|
|
{
|
|
|
|
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");
|
2005-11-28 20:29:47 +00:00
|
|
|
|
2005-11-29 10:25:07 +00:00
|
|
|
status = charon->ike_sa_manager->delete(charon->ike_sa_manager, ike_sa_id);
|
2005-11-25 13:42:58 +00:00
|
|
|
if (status != SUCCESS)
|
|
|
|
{
|
|
|
|
this->worker_logger->log(this->worker_logger, ERROR, "could not delete IKE_SA (%s)",
|
|
|
|
mapping_find(status_m, status));
|
|
|
|
}
|
2005-11-03 13:34:07 +00:00
|
|
|
}
|
|
|
|
|
2005-12-02 13:20:20 +00:00
|
|
|
/**
|
|
|
|
* Implementation of private_thread_pool_t.process_retransmit_request_job.
|
|
|
|
*/
|
|
|
|
static void process_retransmit_request_job(private_thread_pool_t *this, retransmit_request_job_t *job)
|
|
|
|
{
|
2005-12-02 14:07:36 +00:00
|
|
|
|
2005-12-02 13:20:20 +00:00
|
|
|
ike_sa_id_t *ike_sa_id = job->get_ike_sa_id(job);
|
|
|
|
u_int32_t message_id = job->get_message_id(job);
|
2005-12-02 14:07:36 +00:00
|
|
|
bool stop_retransmitting = FALSE;
|
|
|
|
u_int32_t timeout;
|
2005-12-02 13:20:20 +00:00
|
|
|
ike_sa_t *ike_sa;
|
2005-12-02 14:07:36 +00:00
|
|
|
status_t status;
|
2005-12-02 13:20:20 +00:00
|
|
|
|
|
|
|
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 = charon->ike_sa_manager->checkout(charon->ike_sa_manager,ike_sa_id, &ike_sa);
|
|
|
|
if (status != SUCCESS)
|
|
|
|
{
|
2005-12-02 15:40:04 +00:00
|
|
|
job->destroy(job);
|
2005-12-02 13:20:20 +00:00
|
|
|
this->worker_logger->log(this->worker_logger, ERROR, "IKE SA could not be checked out. Allready deleted?");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = ike_sa->retransmit_request(ike_sa, message_id);
|
|
|
|
|
|
|
|
if (status != SUCCESS)
|
|
|
|
{
|
2005-12-02 14:07:36 +00:00
|
|
|
this->worker_logger->log(this->worker_logger, CONTROL | MOST, "Message doesn't have to be retransmitted");
|
|
|
|
stop_retransmitting = TRUE;
|
2005-12-02 13:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this->worker_logger->log(this->worker_logger, CONTROL|MOST, "Checkin 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 = charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
|
|
|
|
if (status != SUCCESS)
|
|
|
|
{
|
|
|
|
this->worker_logger->log(this->worker_logger, ERROR, "Checkin of IKE SA failed!");
|
|
|
|
}
|
|
|
|
|
2005-12-02 14:07:36 +00:00
|
|
|
if (stop_retransmitting)
|
|
|
|
{
|
|
|
|
job->destroy(job);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
job->increase_retransmit_count(job);
|
|
|
|
status = charon->configuration_manager->get_retransmit_timeout (charon->configuration_manager,job->get_retransmit_count(job),&timeout);
|
|
|
|
if (status != SUCCESS)
|
|
|
|
{
|
|
|
|
this->worker_logger->log(this->worker_logger, CONTROL | MOST, "Message will not be anymore retransmitted");
|
|
|
|
job->destroy(job);
|
|
|
|
/*
|
|
|
|
* TODO delete IKE_SA ?
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
charon->event_queue->add_relative(charon->event_queue,(job_t *) job,timeout);
|
2005-12-02 13:20:20 +00:00
|
|
|
}
|
2005-11-25 13:42:58 +00:00
|
|
|
|
2005-11-03 13:34:07 +00:00
|
|
|
/**
|
2005-12-02 13:20:20 +00:00
|
|
|
* Implementation of thread_pool_t.get_pool_size.
|
2005-11-03 13:34:07 +00:00
|
|
|
*/
|
2005-11-10 15:06:52 +00:00
|
|
|
static size_t get_pool_size(private_thread_pool_t *this)
|
2005-11-03 13:34:07 +00:00
|
|
|
{
|
2005-11-10 15:06:52 +00:00
|
|
|
return this->pool_size;
|
2005-11-03 13:34:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-12-02 13:20:20 +00:00
|
|
|
* Implementation of thread_pool_t.destroy.
|
2005-11-03 13:34:07 +00:00
|
|
|
*/
|
2005-11-28 20:29:47 +00:00
|
|
|
static void destroy(private_thread_pool_t *this)
|
2005-11-03 13:34:07 +00:00
|
|
|
{
|
|
|
|
int current;
|
|
|
|
/* flag thread for termination */
|
2005-11-10 09:17:23 +00:00
|
|
|
for (current = 0; current < this->pool_size; current++) {
|
2005-11-26 15:44:40 +00:00
|
|
|
this->pool_logger->log(this->pool_logger, CONTROL, "cancelling worker a thread #%d", current+1);
|
2005-11-03 13:34:07 +00:00
|
|
|
pthread_cancel(this->threads[current]);
|
2005-11-04 09:37:03 +00:00
|
|
|
}
|
2005-11-03 13:34:07 +00:00
|
|
|
|
|
|
|
/* wait for all threads */
|
|
|
|
for (current = 0; current < this->pool_size; current++) {
|
|
|
|
pthread_join(this->threads[current], NULL);
|
2005-11-26 15:44:40 +00:00
|
|
|
this->pool_logger->log(this->pool_logger, CONTROL, "worker thread #%d terminated", current+1);
|
2005-11-03 13:34:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* free mem */
|
2005-11-29 10:25:07 +00:00
|
|
|
charon->logger_manager->destroy_logger(charon->logger_manager, this->pool_logger);
|
|
|
|
charon->logger_manager->destroy_logger(charon->logger_manager, this->worker_logger);
|
2005-11-09 09:07:53 +00:00
|
|
|
allocator_free(this->threads);
|
|
|
|
allocator_free(this);
|
2005-11-03 13:34:07 +00:00
|
|
|
}
|
|
|
|
|
2005-11-10 15:06:52 +00:00
|
|
|
/*
|
2005-12-02 13:20:20 +00:00
|
|
|
* Described in header.
|
2005-11-03 13:34:07 +00:00
|
|
|
*/
|
|
|
|
thread_pool_t *thread_pool_create(size_t pool_size)
|
|
|
|
{
|
|
|
|
int current;
|
|
|
|
|
2005-11-09 09:07:53 +00:00
|
|
|
private_thread_pool_t *this = allocator_alloc_thing(private_thread_pool_t);
|
2005-11-03 13:34:07 +00:00
|
|
|
|
|
|
|
/* fill in public fields */
|
2005-11-28 20:29:47 +00:00
|
|
|
this->public.destroy = (void(*)(thread_pool_t*))destroy;
|
2005-11-10 15:06:52 +00:00
|
|
|
this->public.get_pool_size = (size_t(*)(thread_pool_t*))get_pool_size;
|
2005-11-03 13:34:07 +00:00
|
|
|
|
2005-11-25 13:42:58 +00:00
|
|
|
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;
|
2005-12-02 13:20:20 +00:00
|
|
|
this->process_retransmit_request_job = process_retransmit_request_job;
|
2005-11-03 13:34:07 +00:00
|
|
|
this->pool_size = pool_size;
|
2005-11-10 15:06:52 +00:00
|
|
|
|
2005-11-09 09:07:53 +00:00
|
|
|
this->threads = allocator_alloc(sizeof(pthread_t) * pool_size);
|
2005-11-28 20:29:47 +00:00
|
|
|
|
2005-11-29 10:25:07 +00:00
|
|
|
this->pool_logger = charon->logger_manager->create_logger(charon->logger_manager,THREAD_POOL,NULL);
|
2005-11-28 20:29:47 +00:00
|
|
|
|
2005-11-29 10:25:07 +00:00
|
|
|
this->worker_logger = charon->logger_manager->create_logger(charon->logger_manager,WORKER,NULL);
|
2005-11-04 13:20:11 +00:00
|
|
|
|
2005-11-03 13:34:07 +00:00
|
|
|
/* try to create as many threads as possible, up tu pool_size */
|
2005-11-10 15:06:52 +00:00
|
|
|
for (current = 0; current < pool_size; current++)
|
|
|
|
{
|
2005-11-25 13:42:58 +00:00
|
|
|
if (pthread_create(&(this->threads[current]), NULL, (void*(*)(void*))this->process_jobs, this) == 0)
|
2005-11-10 15:06:52 +00:00
|
|
|
{
|
2005-11-26 15:44:40 +00:00
|
|
|
this->pool_logger->log(this->pool_logger, CONTROL, "created worker thread #%d", current+1);
|
2005-11-10 15:06:52 +00:00
|
|
|
}
|
2005-11-26 15:44:40 +00:00
|
|
|
else
|
2005-11-10 09:17:23 +00:00
|
|
|
{
|
2005-11-10 15:06:52 +00:00
|
|
|
/* creation failed, is it the first one? */
|
2005-11-10 09:17:23 +00:00
|
|
|
if (current == 0)
|
|
|
|
{
|
2005-11-26 15:44:40 +00:00
|
|
|
this->pool_logger->log(this->pool_logger, ERROR, "could not create any thread");
|
2005-11-29 10:25:07 +00:00
|
|
|
charon->logger_manager->destroy_logger(charon->logger_manager, this->pool_logger);
|
|
|
|
charon->logger_manager->destroy_logger(charon->logger_manager, this->worker_logger);
|
2005-11-09 09:07:53 +00:00
|
|
|
allocator_free(this->threads);
|
|
|
|
allocator_free(this);
|
2005-11-03 13:34:07 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* not all threads could be created, but at least one :-/ */
|
2005-11-26 15:44:40 +00:00
|
|
|
this->pool_logger->log(this->pool_logger, ERROR, "could only create %d from requested %d threads!", current, pool_size);
|
2005-11-10 09:17:23 +00:00
|
|
|
|
2005-11-03 13:34:07 +00:00
|
|
|
this->pool_size = current;
|
|
|
|
return (thread_pool_t*)this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (thread_pool_t*)this;
|
|
|
|
}
|