[Babel-users] [PATCH 3/3] utilize taskqueue for check_interfaces()

Christof Schulze christof.schulze at gmx.net
Wed Dec 5 23:11:39 GMT 2018


this illustrates how the taskqueue can be used and removes a little bit of
code from the main loop
---
  babeld.c         | 40 +++++++++++++++++-----------------------
  babeld.h         |  8 +++++++-
  interface.c      |  6 ++++--
  interface.h      |  2 +-
  kernel_netlink.c |  2 +-
  5 files changed, 30 insertions(+), 28 deletions(-)

diff --git a/babeld.c b/babeld.c
index b7d078f..9a1777c 100644
--- a/babeld.c
+++ b/babeld.c
@@ -94,8 +94,9 @@ static int kernel_link_changed = 0;
  static int kernel_addr_changed = 0;
  
  taskqueue_ctx queue_ctx = {};
+static struct tasks currenttasks = {};
  
-struct timeval check_neighbours_timeout, check_interfaces_timeout;
+struct timeval check_neighbours_timeout;
  
  static volatile sig_atomic_t exiting = 0, dumping = 0, reopening = 0;
  
@@ -535,7 +536,9 @@ main(int argc, char **argv)
      if(receive_buffer == NULL)
          goto fail;
  
-    check_interfaces();
+    taskqueue_init(&queue_ctx);
+
+    schedule_interfaces_check(0);
  
      rc = check_xroutes(0);
      if(rc < 0)
@@ -550,7 +553,7 @@ main(int argc, char **argv)
      kernel_addr_changed = 0;
      kernel_dump_time = now.tv_sec + roughly(30);
      schedule_neighbours_check(5000, 1);
-    schedule_interfaces_check(30000, 1);
+    schedule_interfaces_check(30000);
      expiry_time = now.tv_sec + roughly(30);
      source_expiry_time = now.tv_sec + roughly(300);
  
@@ -579,11 +582,9 @@ main(int argc, char **argv)
          flushbuf(&ifp->buf);
      }
  
-    taskqueue_init(&queue_ctx);
-
      debugf("Entering main loop.\n");
  
-    while(1) {
+    while( ! exiting ) {
          struct timeval tv;
          fd_set readfds;
          struct neighbour *neigh;
@@ -591,7 +592,6 @@ main(int argc, char **argv)
          gettime(&now);
  
          tv = check_neighbours_timeout;
-        timeval_min(&tv, &check_interfaces_timeout);
          timeval_min_sec(&tv, expiry_time);
          timeval_min_sec(&tv, source_expiry_time);
          timeval_min_sec(&tv, kernel_dump_time);
@@ -644,9 +644,6 @@ main(int argc, char **argv)
  
          gettime(&now);
  
-        if(exiting)
-            break;
-
          if(kernel_socket >= 0 && FD_ISSET(kernel_socket, &readfds)) {
              struct kernel_filter filter = {0};
              filter.route = kernel_route_notify;
@@ -715,7 +712,7 @@ main(int argc, char **argv)
          }
  
          if(kernel_link_changed || kernel_addr_changed) {
-            check_interfaces();
+	    schedule_interfaces_check(0);
              kernel_link_changed = 0;
          }
  
@@ -743,11 +740,6 @@ main(int argc, char **argv)
              schedule_neighbours_check(msecs, 1);
          }
  
-        if(timeval_compare(&check_interfaces_timeout, &now) < 0) {
-            check_interfaces();
-            schedule_interfaces_check(30000, 1);
-        }
-
          if(now.tv_sec >= expiry_time) {
              expire_routes();
              expire_resend();
@@ -958,15 +950,17 @@ schedule_neighbours_check(int msecs, int override)
  }
  
  void
-schedule_interfaces_check(int msecs, int override)
+schedule_interfaces_check(int msecs)
  {
-    struct timeval timeout;
-
-    timeval_add_msec(&timeout, &now, roughly(msecs));
-    if(override)
-        check_interfaces_timeout = timeout;
+    if ( (!! currenttasks.check_interfaces) && 
+	 taskqueue_linked(currenttasks.check_interfaces))
+        reschedule_task(&queue_ctx, currenttasks.check_interfaces,  
+		        msecs / 1000, msecs%1000 );
      else
-        timeval_min(&check_interfaces_timeout, &timeout);
+        currenttasks.check_interfaces = post_task(&queue_ctx, msecs / 1000, 
+		                                  msecs % 1000, 
+						  check_interfaces, 
+						  NULL, NULL);
  }
  
  int
diff --git a/babeld.h b/babeld.h
index 53ae464..0722eab 100644
--- a/babeld.h
+++ b/babeld.h
@@ -20,6 +20,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
  */
  #pragma once
+#include "taskqueue.h"
+
  #define INFINITY ((unsigned short)(~0))
  
  #ifndef RTPROT_BABEL
@@ -106,7 +108,11 @@ extern int protocol_socket;
  extern int kernel_socket;
  extern int max_request_hopcount;
  
+struct tasks {
+    taskqueue_t *check_interfaces;
+};
+
  void schedule_neighbours_check(int msecs, int override);
-void schedule_interfaces_check(int msecs, int override);
+void schedule_interfaces_check(int msecs);
  int resize_receive_buffer(int size);
  int reopen_logfile(void);
diff --git a/interface.c b/interface.c
index 123455d..d0d1c41 100644
--- a/interface.c
+++ b/interface.c
@@ -236,7 +236,7 @@ check_link_local_addresses(struct interface *ifp)
          local_notify_interface(ifp, LOCAL_CHANGE);
          /* Most probably DAD hasn't finished yet.  Reschedule us
             real soon. */
-        schedule_interfaces_check(2000, 0);
+        schedule_interfaces_check(2000);
          return -1;
      } else {
          int changed;
@@ -533,7 +533,7 @@ interface_ll_address(struct interface *ifp, const unsigned char *address)
  }
  
  void
-check_interfaces(void)
+check_interfaces(void *d)
  {
      struct interface *ifp;
      int rc, ifindex_changed = 0;
@@ -573,4 +573,6 @@ check_interfaces(void)
  
      if(ifindex_changed)
          renumber_filters();
+
+    schedule_interfaces_check(30000);
  }
diff --git a/interface.h b/interface.h
index 782979a..a458bba 100644
--- a/interface.h
+++ b/interface.h
@@ -149,4 +149,4 @@ unsigned update_jitter(struct interface *ifp, int urgent);
  void set_timeout(struct timeval *timeout, int msecs);
  int interface_up(struct interface *ifp, int up);
  int interface_ll_address(struct interface *ifp, const unsigned char *address);
-void check_interfaces(void);
+void check_interfaces(void *d);
diff --git a/kernel_netlink.c b/kernel_netlink.c
index 462bc4b..02a4680 100644
--- a/kernel_netlink.c
+++ b/kernel_netlink.c
@@ -392,7 +392,7 @@ netlink_read(struct netlink *nl, struct netlink *nl_ignore, int answer,
                  }
              } else if(nh->nlmsg_type == RTM_NEWLINK || nh->nlmsg_type == RTM_DELLINK ) {
                  kdebugf("detected an interface change via netlink - triggering babeld interface check\n");
-                check_interfaces();
+                schedule_interfaces_check(0);
              } else if(skip) {
                  kdebugf("(skip)");
              } if(filter) {
-- 
2.11.0




More information about the Babel-users mailing list