Give processor_t more control over the lifecycle of a job
[strongswan.git] / src / libcharon / processing / jobs / process_message_job.c
index a47d48e..71a2cb4 100644 (file)
@@ -35,19 +35,15 @@ struct private_process_message_job_t {
        message_t *message;
 };
 
-/**
- * Implements job_t.destroy.
- */
-static void destroy(private_process_message_job_t *this)
+METHOD(job_t, destroy, void,
+       private_process_message_job_t *this)
 {
        this->message->destroy(this->message);
        free(this);
 }
 
-/**
- * Implementation of job_t.execute.
- */
-static void execute(private_process_message_job_t *this)
+METHOD(job_t, execute, job_requeue_t,
+       private_process_message_job_t *this)
 {
        ike_sa_t *ike_sa;
 
@@ -63,8 +59,7 @@ static void execute(private_process_message_job_t *this)
                         this->message->get_source(this->message),
                         this->message->get_destination(this->message));
                charon->connect_manager->process_check(charon->connect_manager, this->message);
-               destroy(this);
-               return;
+               return JOB_REQUEUE_NONE;
        }
 #endif /* ME */
 
@@ -85,7 +80,28 @@ static void execute(private_process_message_job_t *this)
                        charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
                }
        }
-       destroy(this);
+       return JOB_REQUEUE_NONE;
+}
+
+METHOD(job_t, get_priority, job_priority_t,
+       private_process_message_job_t *this)
+{
+       switch (this->message->get_exchange_type(this->message))
+       {
+               case IKE_AUTH:
+                       /* IKE auth is rather expensive and often blocking, low priority */
+                       return JOB_PRIO_LOW;
+               case INFORMATIONAL:
+                       /* INFORMATIONALs are inexpensive, for DPD we should have low
+                        * reaction times */
+                       return JOB_PRIO_HIGH;
+               case IKE_SA_INIT:
+               case CREATE_CHILD_SA:
+               default:
+                       /* IKE_SA_INIT is expensive, but we will drop them in the receiver
+                        * if we are overloaded */
+                       return JOB_PRIO_MEDIUM;
+       }
 }
 
 /*
@@ -93,14 +109,18 @@ static void execute(private_process_message_job_t *this)
  */
 process_message_job_t *process_message_job_create(message_t *message)
 {
-       private_process_message_job_t *this = malloc_thing(private_process_message_job_t);
-
-       /* interface functions */
-       this->public.job_interface.execute = (void (*) (job_t *)) execute;
-       this->public.job_interface.destroy = (void(*)(job_t*))destroy;
+       private_process_message_job_t *this;
 
-       /* private variables */
-       this->message = message;
+       INIT(this,
+               .public = {
+                       .job_interface = {
+                               .execute = _execute,
+                               .get_priority = _get_priority,
+                               .destroy = _destroy,
+                       },
+               },
+               .message = message,
+       );
 
        return &(this->public);
 }