Use a separate mutex for cached routes in kernel-netlink plugin
[strongswan.git] / src / libhydra / plugins / kernel_netlink / kernel_netlink_net.c
index e4a880a..4b64a8d 100644 (file)
@@ -53,6 +53,7 @@
 #include <threading/thread.h>
 #include <threading/condvar.h>
 #include <threading/mutex.h>
+#include <threading/spinlock.h>
 #include <utils/hashtable.h>
 #include <utils/linked_list.h>
 #include <processing/jobs/callback_job.h>
@@ -373,9 +374,14 @@ struct private_kernel_netlink_net_t {
        int socket_events;
 
        /**
-        * time of the last roam event
+        * earliest time of the next roam event
         */
-       timeval_t last_roam;
+       timeval_t next_roam;
+
+       /**
+        * lock to check and update roam event time
+        */
+       spinlock_t *roam_lock;
 
        /**
         * routing table to install routes
@@ -393,6 +399,11 @@ struct private_kernel_netlink_net_t {
        hashtable_t *routes;
 
        /**
+        * mutex for routes
+        */
+       mutex_t *routes_lock;
+
+       /**
         * interface changes which may trigger route reinstallation
         */
        hashtable_t *net_changes;
@@ -467,7 +478,7 @@ static job_requeue_t reinstall_routes(private_kernel_netlink_net_t *this)
        route_entry_t *route;
 
        this->net_changes_lock->lock(this->net_changes_lock);
-       this->mutex->lock(this->mutex);
+       this->routes_lock->lock(this->routes_lock);
 
        enumerator = this->routes->create_enumerator(this->routes);
        while (enumerator->enumerate(enumerator, NULL, (void**)&route))
@@ -497,7 +508,7 @@ static job_requeue_t reinstall_routes(private_kernel_netlink_net_t *this)
                }
        }
        enumerator->destroy(enumerator);
-       this->mutex->unlock(this->mutex);
+       this->routes_lock->unlock(this->routes_lock);
 
        net_changes_clear(this);
        this->net_changes_lock->unlock(this->net_changes_lock);
@@ -690,21 +701,25 @@ static void fire_roam_event(private_kernel_netlink_net_t *this, bool address)
        job_t *job;
 
        time_monotonic(&now);
-       if (timercmp(&now, &this->last_roam, >))
+       this->roam_lock->lock(this->roam_lock);
+       if (!timercmp(&now, &this->next_roam, >))
        {
-               now.tv_usec += ROAM_DELAY * 1000;
-               while (now.tv_usec > 1000000)
-               {
-                       now.tv_sec++;
-                       now.tv_usec -= 1000000;
-               }
-               this->last_roam = now;
-
-               job = (job_t*)callback_job_create((callback_job_cb_t)roam_event,
-                                                                                 (void*)(uintptr_t)(address ? 1 : 0),
-                                                                                 NULL, NULL);
-               lib->scheduler->schedule_job_ms(lib->scheduler, job, ROAM_DELAY);
+               this->roam_lock->unlock(this->roam_lock);
+               return;
        }
+       now.tv_usec += ROAM_DELAY * 1000;
+       while (now.tv_usec > 1000000)
+       {
+               now.tv_sec++;
+               now.tv_usec -= 1000000;
+       }
+       this->next_roam = now;
+       this->roam_lock->unlock(this->roam_lock);
+
+       job = (job_t*)callback_job_create((callback_job_cb_t)roam_event,
+                                                                         (void*)(uintptr_t)(address ? 1 : 0),
+                                                                          NULL, NULL);
+       lib->scheduler->schedule_job_ms(lib->scheduler, job, ROAM_DELAY);
 }
 
 /**
@@ -1889,18 +1904,18 @@ METHOD(kernel_net_t, add_route, status_t,
                .if_name = if_name,
        };
 
-       this->mutex->lock(this->mutex);
+       this->routes_lock->lock(this->routes_lock);
        found = this->routes->get(this->routes, &route);
        if (found)
        {
-               this->mutex->unlock(this->mutex);
+               this->routes_lock->unlock(this->routes_lock);
                return ALREADY_DONE;
        }
        found = route_entry_clone(&route);
        this->routes->put(this->routes, found, found);
        status = manage_srcroute(this, RTM_NEWROUTE, NLM_F_CREATE | NLM_F_EXCL,
                                                         dst_net, prefixlen, gateway, src_ip, if_name);
-       this->mutex->unlock(this->mutex);
+       this->routes_lock->unlock(this->routes_lock);
        return status;
 }
 
@@ -1917,18 +1932,18 @@ METHOD(kernel_net_t, del_route, status_t,
                .if_name = if_name,
        };
 
-       this->mutex->lock(this->mutex);
+       this->routes_lock->lock(this->routes_lock);
        found = this->routes->get(this->routes, &route);
        if (!found)
        {
-               this->mutex->unlock(this->mutex);
+               this->routes_lock->unlock(this->routes_lock);
                return NOT_FOUND;
        }
        this->routes->remove(this->routes, found);
        route_entry_destroy(found);
        status = manage_srcroute(this, RTM_DELROUTE, 0, dst_net, prefixlen,
                                                         gateway, src_ip, if_name);
-       this->mutex->unlock(this->mutex);
+       this->routes_lock->unlock(this->routes_lock);
        return status;
 }
 
@@ -2130,6 +2145,7 @@ METHOD(kernel_net_t, destroy, void,
        }
        enumerator->destroy(enumerator);
        this->routes->destroy(this->routes);
+       this->routes_lock->destroy(this->routes_lock);
        DESTROY_IF(this->socket);
 
        net_changes_clear(this);
@@ -2141,6 +2157,7 @@ METHOD(kernel_net_t, destroy, void,
 
        this->ifaces->destroy_function(this->ifaces, (void*)iface_entry_destroy);
        this->rt_exclude->destroy(this->rt_exclude);
+       this->roam_lock->destroy(this->roam_lock);
        this->condvar->destroy(this->condvar);
        this->mutex->destroy(this->mutex);
        free(this);
@@ -2182,10 +2199,12 @@ kernel_netlink_net_t *kernel_netlink_net_create()
                                                                (hashtable_equals_t)addr_map_entry_equals, 16),
                .vips = hashtable_create((hashtable_hash_t)addr_map_entry_hash,
                                                                 (hashtable_equals_t)addr_map_entry_equals, 16),
+               .routes_lock = mutex_create(MUTEX_TYPE_DEFAULT),
                .net_changes_lock = mutex_create(MUTEX_TYPE_DEFAULT),
                .ifaces = linked_list_create(),
                .mutex = mutex_create(MUTEX_TYPE_RECURSIVE),
                .condvar = condvar_create(CONDVAR_TYPE_DEFAULT),
+               .roam_lock = spinlock_create(),
                .routing_table = lib->settings->get_int(lib->settings,
                                "%s.routing_table", ROUTING_TABLE, hydra->daemon),
                .routing_table_prio = lib->settings->get_int(lib->settings,
@@ -2198,7 +2217,7 @@ kernel_netlink_net_t *kernel_netlink_net_create()
                                "%s.install_virtual_ip_on", NULL, hydra->daemon),
        );
        timerclear(&this->last_route_reinstall);
-       timerclear(&this->last_roam);
+       timerclear(&this->next_roam);
 
        check_kernel_features(this);