ikev2: support responder hostname
[vpp.git] / src / vppinfra / tw_timer_template.c
index a6d26d7..97c70b2 100644 (file)
@@ -142,9 +142,8 @@ timer_addhead (TWT (tw_timer) * pool, u32 head_index, u32 new_index)
 }
 
 static inline void
-timer_remove (TWT (tw_timer) * pool, u32 index)
+timer_remove (TWT (tw_timer) * pool, TWT (tw_timer) * elt)
 {
-  TWT (tw_timer) * elt = pool_elt_at_index (pool, index);
   TWT (tw_timer) * next_elt, *prev_elt;
 
   ASSERT (elt->user_handle != ~0);
@@ -158,17 +157,8 @@ timer_remove (TWT (tw_timer) * pool, u32 index)
   elt->prev = elt->next = ~0;
 }
 
-/**
- * @brief Start a Tw Timer
- * @param tw_timer_wheel_t * tw timer wheel object pointer
- * @param u32 pool_index user pool index, presumably for a tw session
- * @param u32 timer_id app-specific timer ID. 4 bits.
- * @param u64 interval timer interval in ticks
- * @returns handle needed to cancel the timer
- */
-u32
-TW (tw_timer_start) (TWT (tw_timer_wheel) * tw, u32 pool_index, u32 timer_id,
-                    u64 interval)
+static inline void
+timer_add (TWT (tw_timer_wheel) * tw, TWT (tw_timer) * t, u64 interval)
 {
 #if TW_TIMER_WHEELS > 1
   u16 slow_ring_offset;
@@ -182,14 +172,6 @@ TW (tw_timer_start) (TWT (tw_timer_wheel) * tw, u32 pool_index, u32 timer_id,
 #endif
   u16 fast_ring_offset;
   tw_timer_wheel_slot_t *ts;
-  TWT (tw_timer) * t;
-
-  ASSERT (interval);
-
-  pool_get (tw->timers, t);
-  memset (t, 0xff, sizeof (*t));
-
-  t->user_handle = TW (make_internal_timer_handle) (pool_index, timer_id);
 
   /* Factor interval into 1..3 wheel offsets */
 #if TW_TIMER_WHEELS > 2
@@ -209,9 +191,9 @@ TW (tw_timer_start) (TWT (tw_timer_wheel) * tw, u32 pool_index, u32 timer_id,
       ts = &tw->overflow;
       timer_addhead (tw->timers, ts->head_index, t - tw->timers);
 #if TW_START_STOP_TRACE_SIZE > 0
-      TW (tw_timer_trace) (tw, timer_id, pool_index, t - tw->timers);
+      TW (tw_timer_trace) (tw, timer_id, user_id, t - tw->timers);
 #endif
-      return t - tw->timers;
+      return;
     }
 #endif
 
@@ -262,9 +244,9 @@ TW (tw_timer_start) (TWT (tw_timer_wheel) * tw, u32 pool_index, u32 timer_id,
 
       timer_addhead (tw->timers, ts->head_index, t - tw->timers);
 #if TW_START_STOP_TRACE_SIZE > 0
-      TW (tw_timer_trace) (tw, timer_id, pool_index, t - tw->timers);
+      TW (tw_timer_trace) (tw, timer_id, user_id, t - tw->timers);
 #endif
-      return t - tw->timers;
+      return;
     }
 #endif
 
@@ -280,9 +262,9 @@ TW (tw_timer_start) (TWT (tw_timer_wheel) * tw, u32 pool_index, u32 timer_id,
 
       timer_addhead (tw->timers, ts->head_index, t - tw->timers);
 #if TW_START_STOP_TRACE_SIZE > 0
-      TW (tw_timer_trace) (tw, timer_id, pool_index, t - tw->timers);
+      TW (tw_timer_trace) (tw, timer_id, user_id, t - tw->timers);
 #endif
-      return t - tw->timers;
+      return;
     }
 #else
   fast_ring_offset %= TW_SLOTS_PER_RING;
@@ -298,8 +280,32 @@ TW (tw_timer_start) (TWT (tw_timer_wheel) * tw, u32 pool_index, u32 timer_id,
                                          fast_ring_offset, 1);
 #endif
 #if TW_START_STOP_TRACE_SIZE > 0
-  TW (tw_timer_trace) (tw, timer_id, pool_index, t - tw->timers);
+  TW (tw_timer_trace) (tw, timer_id, user_id, t - tw->timers);
 #endif
+}
+
+/**
+ * @brief Start a Tw Timer
+ * @param tw_timer_wheel_t * tw timer wheel object pointer
+ * @param u32 user_id user defined timer id, presumably for a tw session
+ * @param u32 timer_id app-specific timer ID. 4 bits.
+ * @param u64 interval timer interval in ticks
+ * @returns handle needed to cancel the timer
+ */
+__clib_export u32
+TW (tw_timer_start) (TWT (tw_timer_wheel) * tw, u32 user_id, u32 timer_id,
+                    u64 interval)
+{
+  TWT (tw_timer) * t;
+
+  ASSERT (interval);
+
+  pool_get (tw->timers, t);
+  clib_memset (t, 0xff, sizeof (*t));
+
+  t->user_handle = TW (make_internal_timer_handle) (user_id, timer_id);
+
+  timer_add (tw, t, interval);
   return t - tw->timers;
 }
 
@@ -343,7 +349,7 @@ int TW (scan_for_handle) (TWT (tw_timer_wheel) * tw, u32 handle)
  * @param tw_timer_wheel_t * tw timer wheel object pointer
  * @param u32 handle timer cancellation returned by tw_timer_start
  */
-void TW (tw_timer_stop) (TWT (tw_timer_wheel) * tw, u32 handle)
+__clib_export void TW (tw_timer_stop) (TWT (tw_timer_wheel) * tw, u32 handle)
 {
   TWT (tw_timer) * t;
 
@@ -365,11 +371,32 @@ void TW (tw_timer_stop) (TWT (tw_timer_wheel) * tw, u32 handle)
   /* in case of idiotic handle (e.g. passing a listhead index) */
   ASSERT (t->user_handle != ~0);
 
-  timer_remove (tw->timers, handle);
+  timer_remove (tw->timers, t);
 
   pool_put_index (tw->timers, handle);
 }
 
+__clib_export int
+TW (tw_timer_handle_is_free) (TWT (tw_timer_wheel) * tw, u32 handle)
+{
+  return pool_is_free_index (tw->timers, handle);
+}
+
+/**
+ * @brief Update a tw timer
+ * @param tw_timer_wheel_t * tw timer wheel object pointer
+ * @param u32 handle timer returned by tw_timer_start
+ * @param u32 interval timer interval in ticks
+ */
+__clib_export void
+TW (tw_timer_update) (TWT (tw_timer_wheel) * tw, u32 handle, u64 interval)
+{
+  TWT (tw_timer) * t;
+  t = pool_elt_at_index (tw->timers, handle);
+  timer_remove (tw->timers, t);
+  timer_add (tw, t, interval);
+}
+
 /**
  * @brief Initialize a tw timer wheel template instance
  * @param tw_timer_wheel_t * tw timer wheel object pointer
@@ -377,7 +404,7 @@ void TW (tw_timer_stop) (TWT (tw_timer_wheel) * tw, u32 handle)
  *   expired timer handles. The callback is optional.
  * @param f64 timer_interval_in_seconds
  */
-void
+__clib_export void
 TW (tw_timer_wheel_init) (TWT (tw_timer_wheel) * tw,
                          void *expired_timer_callback,
                          f64 timer_interval_in_seconds, u32 max_expirations)
@@ -385,7 +412,7 @@ TW (tw_timer_wheel_init) (TWT (tw_timer_wheel) * tw,
   int ring, slot;
   tw_timer_wheel_slot_t *ts;
   TWT (tw_timer) * t;
-  memset (tw, 0, sizeof (*tw));
+  clib_memset (tw, 0, sizeof (*tw));
   tw->expired_timer_callback = expired_timer_callback;
   tw->max_expirations = max_expirations;
   if (timer_interval_in_seconds == 0.0)
@@ -395,7 +422,6 @@ TW (tw_timer_wheel_init) (TWT (tw_timer_wheel) * tw,
     }
   tw->timer_interval = timer_interval_in_seconds;
   tw->ticks_per_second = 1.0 / timer_interval_in_seconds;
-  tw->first_expires_tick = ~0ULL;
 
   vec_validate (tw->expired_timer_handles, 0);
   _vec_len (tw->expired_timer_handles) = 0;
@@ -406,7 +432,7 @@ TW (tw_timer_wheel_init) (TWT (tw_timer_wheel) * tw,
        {
          ts = &tw->w[ring][slot];
          pool_get (tw->timers, t);
-         memset (t, 0xff, sizeof (*t));
+         clib_memset (t, 0xff, sizeof (*t));
          t->next = t->prev = t - tw->timers;
          ts->head_index = t - tw->timers;
        }
@@ -415,7 +441,7 @@ TW (tw_timer_wheel_init) (TWT (tw_timer_wheel) * tw,
 #if TW_OVERFLOW_VECTOR > 0
   ts = &tw->overflow;
   pool_get (tw->timers, t);
-  memset (t, 0xff, sizeof (*t));
+  clib_memset (t, 0xff, sizeof (*t));
   t->next = t->prev = t - tw->timers;
   ts->head_index = t - tw->timers;
 #endif
@@ -425,7 +451,7 @@ TW (tw_timer_wheel_init) (TWT (tw_timer_wheel) * tw,
  * @brief Free a tw timer wheel template instance
  * @param tw_timer_wheel_t * tw timer wheel object pointer
  */
-void TW (tw_timer_wheel_free) (TWT (tw_timer_wheel) * tw)
+__clib_export void TW (tw_timer_wheel_free) (TWT (tw_timer_wheel) * tw)
 {
   int i, j;
   tw_timer_wheel_slot_t *ts;
@@ -450,7 +476,7 @@ void TW (tw_timer_wheel_free) (TWT (tw_timer_wheel) * tw)
        }
     }
 
-#if TW_OVERFLOW_VECVOR > 0
+#if TW_OVERFLOW_VECTOR > 0
   ts = &tw->overflow;
   head = pool_elt_at_index (tw->timers, ts->head_index);
   next_index = head->next;
@@ -464,7 +490,7 @@ void TW (tw_timer_wheel_free) (TWT (tw_timer_wheel) * tw)
   pool_put (tw->timers, head);
 #endif
 
-  memset (tw, 0, sizeof (*tw));
+  clib_memset (tw, 0, sizeof (*tw));
 }
 
 /**
@@ -488,7 +514,7 @@ static inline
   u32 slow_wheel_index __attribute__ ((unused));
   u32 glacier_wheel_index __attribute__ ((unused));
 
-  /* Shouldn't happen */
+  /* Called too soon to process new timer expirations? */
   if (PREDICT_FALSE (now < tw->next_run_time))
     return callback_vector_arg;
 
@@ -500,6 +526,14 @@ static inline
   /* Remember when we ran, compute next runtime */
   tw->next_run_time = (now + tw->timer_interval);
 
+  /* First call, or time jumped backwards? */
+  if (PREDICT_FALSE
+      ((tw->last_run_time == 0.0) || (now <= tw->last_run_time)))
+    {
+      tw->last_run_time = now;
+      return callback_vector_arg;
+    }
+
   if (callback_vector_arg == 0)
     {
       _vec_len (tw->expired_timer_handles) = 0;
@@ -777,13 +811,14 @@ static inline
   return callback_vector;
 }
 
-u32 *TW (tw_timer_expire_timers) (TWT (tw_timer_wheel) * tw, f64 now)
+__clib_export u32 *TW (tw_timer_expire_timers) (TWT (tw_timer_wheel) * tw,
+                                               f64 now)
 {
   return TW (tw_timer_expire_timers_internal) (tw, now, 0 /* no vector */ );
 }
 
-u32 *TW (tw_timer_expire_timers_vec) (TWT (tw_timer_wheel) * tw, f64 now,
-                                     u32 * vec)
+__clib_export u32 *TW (tw_timer_expire_timers_vec) (TWT (tw_timer_wheel) * tw,
+                                                   f64 now, u32 * vec)
 {
   return TW (tw_timer_expire_timers_internal) (tw, now, vec);
 }
@@ -796,11 +831,24 @@ u32 *TW (tw_timer_expire_timers_vec) (TWT (tw_timer_wheel) * tw, f64 now,
  * when timers are removed from fast wheel slots.
  */
 
-u32 TW (tw_timer_first_expires_in_ticks) (TWT (tw_timer_wheel) * tw)
+__clib_export u32
+TW (tw_timer_first_expires_in_ticks) (TWT (tw_timer_wheel) * tw)
 {
   u32 first_expiring_index, fast_ring_index;
   i32 delta;
 
+#if TW_TIMER_WHEELS > 1
+  fast_ring_index = tw->current_index[TW_TIMER_RING_FAST];
+  if (fast_ring_index == TW_SLOTS_PER_RING)
+    return 1;
+
+  first_expiring_index = clib_bitmap_next_set (tw->fast_slot_bitmap,
+                                              fast_ring_index);
+  if (first_expiring_index == ~0)
+    first_expiring_index = TW_SLOTS_PER_RING;
+
+#else
+
   if (clib_bitmap_is_zero (tw->fast_slot_bitmap))
     return TW_SLOTS_PER_RING;
 
@@ -812,6 +860,7 @@ u32 TW (tw_timer_first_expires_in_ticks) (TWT (tw_timer_wheel) * tw)
                                               fast_ring_index);
   if (first_expiring_index == ~0 && fast_ring_index != 0)
     first_expiring_index = clib_bitmap_first_set (tw->fast_slot_bitmap);
+#endif
 
   ASSERT (first_expiring_index != ~0);