[pisa-src] r1306 - in trunk/pisacd: cdderegister.c cdheartbeat.c cdmain.c cdpending.c cdpending.h cdregister.c

  • From: Thomas Jansen <mithi@xxxxxxxxx>
  • To: pisa-src@xxxxxxxxxxxxx
  • Date: Tue, 27 Oct 2009 13:06:08 +0100

Author: tjansen
Date: Tue Oct 27 13:06:08 2009
New Revision: 1306

Log:
cdpending.c uses the scheduler now rather than maintaining it's own polling
function on top of the scheduler. This removes the useless periodic wakeups
and also reduces the latency before the first try is made to 0.

Modified:
   trunk/pisacd/cdderegister.c
   trunk/pisacd/cdheartbeat.c
   trunk/pisacd/cdmain.c
   trunk/pisacd/cdpending.c
   trunk/pisacd/cdpending.h
   trunk/pisacd/cdregister.c

Modified: trunk/pisacd/cdderegister.c
==============================================================================
--- trunk/pisacd/cdderegister.c Tue Oct 27 11:45:39 2009        (r1305)
+++ trunk/pisacd/cdderegister.c Tue Oct 27 13:06:08 2009        (r1306)
@@ -92,10 +92,7 @@
 
        entry->status = PISA_CON_DEREGISTERING;
 
-       pend = pisa_pending_add(entry, PISA_PKTTYPE_TUN_DEREGISTER);
-       pend->func = pisa_send_deregister_pend;
-       pend->fail = pisa_send_deregister_fail;
-       pisa_pending_reschedule(pend, 0);
+       pend = pisa_pending_add(entry, PISA_PKTTYPE_TUN_DEREGISTER, 
pisa_send_deregister_pend, pisa_send_deregister_fail);
 }
 
 /**

Modified: trunk/pisacd/cdheartbeat.c
==============================================================================
--- trunk/pisacd/cdheartbeat.c  Tue Oct 27 11:45:39 2009        (r1305)
+++ trunk/pisacd/cdheartbeat.c  Tue Oct 27 13:06:08 2009        (r1306)
@@ -89,10 +89,7 @@
         * the server. */
        if (e->heartbeat_flag == 0) {
                pisa_pending *pend;
-               pend = pisa_pending_add(e, PISA_PKTTYPE_TUN_HEARTBEAT);
-               pend->func = pisa_send_heartbeat_pend;
-               pend->fail = pisa_send_heartbeat_fail;
-               pisa_pending_reschedule(pend, 0);
+               pend = pisa_pending_add(e, PISA_PKTTYPE_TUN_HEARTBEAT, 
pisa_send_heartbeat_pend, pisa_send_heartbeat_fail);
        }
 
        e->heartbeat_flag = 0;

Modified: trunk/pisacd/cdmain.c
==============================================================================
--- trunk/pisacd/cdmain.c       Tue Oct 27 11:45:39 2009        (r1305)
+++ trunk/pisacd/cdmain.c       Tue Oct 27 13:06:08 2009        (r1306)
@@ -189,9 +189,8 @@
 
        pisa_tunnel_configure_main(cd_ctx.ifname_tunnel, &cd_cfg.local_ipv4, 
&cd_cfg.local_netmask, MTU_TUN);
 
-       /* Initialize the scheduler and start some maintenance tasks */
+       /* Initialize the scheduler */
        pisa_sched_init(&cd_ctx.scheduler);
-       pisa_sched_add_now(&cd_ctx.scheduler, pisa_task_pending, NULL);
 }
 
 static void cd_deinit(void)

Modified: trunk/pisacd/cdpending.c
==============================================================================
--- trunk/pisacd/cdpending.c    Tue Oct 27 11:45:39 2009        (r1305)
+++ trunk/pisacd/cdpending.c    Tue Oct 27 13:06:08 2009        (r1306)
@@ -18,25 +18,6 @@
 #include "util.h"
 #include "scheduler.h"
 
-/* TODO: rewrite pending, only need the data structure, scheduling can be done
- *       scheduler, no need to maintain a duplicate infrastructure. */
-
-/**
- * Adds a number of microseconds to a timeval.
- * @param t original time, later contains the result
- * @param usec number of microseconds that should be added
- */
-static void pisa_time_add(struct timeval *t, int sec, int usec)
-{
-       t->tv_sec += sec;
-       t->tv_usec += usec;
-
-       while (t->tv_usec > 1000000) {
-               t->tv_usec -= 1000000;
-               t->tv_sec++;
-       }
-}
-
 static pisa_pending *hash_pending = NULL;
 
 /**
@@ -80,9 +61,11 @@
  * Create or find a pending request for a given HIT/type combination.
  * @param addr destination of the pending request
  * @param type pending packet type
+ * @param send callback function to send the pending request
+ * @param fail callback function called after reaching the maximum retries
  * @return pointer to the pending entry
  */
-pisa_pending *pisa_pending_add(pisa_conmgr_entry *entry, int type)
+pisa_pending *pisa_pending_add(pisa_conmgr_entry *entry, int type, 
pisa_pending_func send, pisa_pending_func fail)
 {
        pisa_pending *pend = pisa_pending_find(entry, type);
 
@@ -90,28 +73,21 @@
                pend = pisa_pending_new(entry, type);
 
        pend->count = 0;
-       pend->max_count = 3; /* @todo: make configurable (or based on type?) */
-       memset(&pend->next, 0, sizeof(struct timeval));
-       pend->delay_ms = 2000; /* @todo: make configurable (or based on type?) 
*/
+       pend->max_count = 3;
+       pend->delay.tv_sec = 2;
+       pend->delay.tv_usec = 0;
        if (pend->data)
                free(pend->data);
        pend->data = NULL;
+       pend->send = send;
+       pend->fail = fail;
 
-       return pend;
-}
+       /* Schedule the first try immediately */
+       if (pend->task)
+               pisa_sched_remove(&cd_ctx.scheduler, pend->task);
+       pend->task = pisa_sched_add_now(&cd_ctx.scheduler, pisa_task_pending, 
pend);
 
-/**
- * Reschedule a resend for the pending request.
- * @param pend pending request
- * @param millisec delay before resend in milliseconds
- */
-void pisa_pending_reschedule(pisa_pending *pend, int millisec)
-{
-       if (!pend)
-               return;
-
-       gettimeofday(&pend->next, NULL);
-       pisa_time_add(&pend->next, 0, 1000 * millisec);
+       return pend;
 }
 
 /**
@@ -121,6 +97,8 @@
 void pisa_pending_remove(pisa_pending *pend)
 {
        HASH_DELETE(hh, hash_pending, pend);
+       if (pend->task)
+               pisa_sched_remove(&cd_ctx.scheduler, pend->task);
        if (pend->data != NULL)
                free(pend->data);
        free(pend);
@@ -162,36 +140,22 @@
 }
 
 /**
- * Walk through the hash of pending requests and see if they need to be
- * processed. If they are due, check that we don't exceed the maximal number
- * of resends.
- * @return 1 if requests are pending, 0 if no requests are pending
+ * A pending resend is due. Check if the maximum number of retries has been
+ * exceeded and call the send or the fail function accordingly.
+ * @param data the pending event
  */
 void pisa_task_pending(void *data)
 {
-       pisa_pending *p = hash_pending;
-       struct timeval t, delay = {0, 10000};
-       gettimeofday(&t, NULL);
+       pisa_pending *pend = (pisa_pending *)data;
 
-       while (p) {
-               if (pisa_time_before(&p->next, &t)) {
-                       if (++(p->count) > p->max_count) {
-                               pisa_pending *p_old = p;
-                               PISA_DEBUG(PL_TIMEOUT, "removed pending request 
because of retry timeout.\n");
-                               p->fail(p);
-                               p = p->hh.next;
-                               pisa_pending_remove(p_old);
-                       } else {
-                               p->func(p);
-                               pisa_pending_reschedule(p, p->delay_ms);
-                               p = p->hh.next;
-                       }
-               } else {
-                       p = p->hh.next;
-               }
-       }
+       pend->task = NULL;
 
-       pisa_sched_add(&cd_ctx.scheduler, pisa_task_pending, &delay, NULL);
-
-       /* return hash_pending ? 1 : 0; */
+       if (++(pend->count) > pend->max_count) {
+               PISA_DEBUG(PL_TIMEOUT, "removed pending request because of 
retry timeout.\n");
+               pend->fail(pend);
+               pisa_pending_remove(pend);
+       } else {
+               pend->send(pend);
+               pend->task = pisa_sched_add(&cd_ctx.scheduler, 
pisa_task_pending, &pend->delay, pend);
+       }
 }

Modified: trunk/pisacd/cdpending.h
==============================================================================
--- trunk/pisacd/cdpending.h    Tue Oct 27 11:45:39 2009        (r1305)
+++ trunk/pisacd/cdpending.h    Tue Oct 27 13:06:08 2009        (r1306)
@@ -17,8 +17,12 @@
 #include <netinet/in.h>
 
 #include "conmgr.h"
+#include "scheduler.h"
 #include "uthash.h"
 
+struct pisa_pending;
+typedef void (*pisa_pending_func)(struct pisa_pending *p);
+
 typedef struct pisa_pending {
        struct {
                struct sockaddr_in6 addr;
@@ -27,19 +31,18 @@
        pisa_conmgr_entry *entry;
        int count;
        int max_count;
-       void (*func)(struct pisa_pending *p);
-       void (*fail)(struct pisa_pending *p);
-       struct timeval next; /* next time to run the function */
-       int delay_ms; /* time to wait between two retries in milliseconds */
+       pisa_pending_func send;
+       pisa_pending_func fail;
+       struct timeval delay; /* delay between two retries */
        void *data;
+       pisa_sched_task *task;
 
        UT_hash_handle hh;
 } pisa_pending;
 
 pisa_pending *pisa_pending_find(pisa_conmgr_entry *entry, int type);
 pisa_pending *pisa_pending_new(pisa_conmgr_entry *entry, int type);
-pisa_pending *pisa_pending_add(pisa_conmgr_entry *entry, int type);
-void pisa_pending_reschedule(pisa_pending *pend, int millisec);
+pisa_pending *pisa_pending_add(pisa_conmgr_entry *entry, int type, 
pisa_pending_func send, pisa_pending_func fail);
 void pisa_pending_remove(pisa_pending *pend);
 void pisa_pending_remove_all(void);
 void pisa_pending_remove_by_entry(pisa_conmgr_entry *entry);

Modified: trunk/pisacd/cdregister.c
==============================================================================
--- trunk/pisacd/cdregister.c   Tue Oct 27 11:45:39 2009        (r1305)
+++ trunk/pisacd/cdregister.c   Tue Oct 27 13:06:08 2009        (r1306)
@@ -85,10 +85,7 @@
        if (entry->status != PISA_CON_DISCONNECTED)
                return;
 
-       pend = pisa_pending_add(entry, PISA_PKTTYPE_TUN_REGISTER);
-       pend->func = pisa_send_register_pend;
-       pend->fail = pisa_send_register_fail;
-       pisa_pending_reschedule(pend, 0);
+       pend = pisa_pending_add(entry, PISA_PKTTYPE_TUN_REGISTER, 
pisa_send_register_pend, pisa_send_register_fail);
 
        reg = malloc(sizeof(pisa_payload_register));
        memset(reg, 0, sizeof(pisa_payload_register));

Other related posts:

  • » [pisa-src] r1306 - in trunk/pisacd: cdderegister.c cdheartbeat.c cdmain.c cdpending.c cdpending.h cdregister.c - Thomas Jansen