+u8 *
+format_clib_us_time (u8 * s, va_list * args)
+{
+ clib_us_time_t t = va_arg (*args, clib_us_time_t);
+ if (t < 1e3)
+ s = format (s, "%u us", t);
+ else
+ s = format (s, "%.3f s", (f64) t * CLIB_US_TIME_PERIOD);
+ return s;
+}
+
+u8 *
+format_transport_pacer (u8 * s, va_list * args)
+{
+ spacer_t *pacer = va_arg (*args, spacer_t *);
+ u32 thread_index = va_arg (*args, int);
+ clib_us_time_t now, diff;
+
+ now = transport_us_time_now (thread_index);
+ diff = now - pacer->last_update;
+ s = format (s, "rate %lu bucket %lu t/p %.3f last_update %U idle %u",
+ pacer->bytes_per_sec, pacer->bucket, pacer->tokens_per_period,
+ format_clib_us_time, diff, pacer->idle_timeout_us);
+ return s;
+}
+
+static inline u32
+spacer_max_burst (spacer_t * pacer, clib_us_time_t time_now)
+{
+ u64 n_periods = (time_now - pacer->last_update);
+ u64 inc;
+
+ if (PREDICT_FALSE (n_periods > pacer->idle_timeout_us))
+ {
+ pacer->last_update = time_now;
+ pacer->bucket = TRANSPORT_PACER_MIN_BURST;
+ return TRANSPORT_PACER_MIN_BURST;
+ }
+
+ if ((inc = (f32) n_periods * pacer->tokens_per_period) > 10)
+ {
+ pacer->last_update = time_now;
+ pacer->bucket = clib_min (pacer->bucket + inc, pacer->bytes_per_sec);
+ }
+
+ return clib_min (pacer->bucket, TRANSPORT_PACER_MAX_BURST);
+}
+
+static inline void
+spacer_update_bucket (spacer_t * pacer, u32 bytes)
+{
+ ASSERT (pacer->bucket >= bytes);
+ pacer->bucket -= bytes;
+}
+
+static inline void
+spacer_set_pace_rate (spacer_t * pacer, u64 rate_bytes_per_sec,
+ clib_us_time_t rtt)
+{
+ ASSERT (rate_bytes_per_sec != 0);
+ pacer->bytes_per_sec = rate_bytes_per_sec;
+ pacer->tokens_per_period = rate_bytes_per_sec * CLIB_US_TIME_PERIOD;
+ pacer->idle_timeout_us = clib_max (rtt * TRANSPORT_PACER_IDLE_FACTOR,
+ TRANSPORT_PACER_MIN_IDLE);
+}
+
+static inline u64
+spacer_pace_rate (spacer_t * pacer)
+{
+ return pacer->bytes_per_sec;
+}
+
+static inline void
+spacer_reset (spacer_t * pacer, clib_us_time_t time_now, u64 bucket)
+{
+ pacer->last_update = time_now;
+ pacer->bucket = bucket;
+}
+
+void
+transport_connection_tx_pacer_reset (transport_connection_t * tc,
+ u64 rate_bytes_per_sec, u32 start_bucket,
+ clib_us_time_t rtt)
+{
+ spacer_set_pace_rate (&tc->pacer, rate_bytes_per_sec, rtt);
+ spacer_reset (&tc->pacer, transport_us_time_now (tc->thread_index),
+ start_bucket);
+}
+
+void
+transport_connection_tx_pacer_reset_bucket (transport_connection_t * tc,
+ u32 bucket)
+{
+ spacer_reset (&tc->pacer, transport_us_time_now (tc->thread_index), bucket);
+}
+
+void
+transport_connection_tx_pacer_init (transport_connection_t * tc,
+ u64 rate_bytes_per_sec,
+ u32 initial_bucket)
+{
+ tc->flags |= TRANSPORT_CONNECTION_F_IS_TX_PACED;
+ transport_connection_tx_pacer_reset (tc, rate_bytes_per_sec,
+ initial_bucket, 1e6);
+}
+
+void
+transport_connection_tx_pacer_update (transport_connection_t * tc,
+ u64 bytes_per_sec, clib_us_time_t rtt)
+{
+ spacer_set_pace_rate (&tc->pacer, bytes_per_sec, rtt);
+}
+
+u32
+transport_connection_tx_pacer_burst (transport_connection_t * tc)
+{
+ return spacer_max_burst (&tc->pacer,
+ transport_us_time_now (tc->thread_index));
+}
+
+u64
+transport_connection_tx_pacer_rate (transport_connection_t * tc)
+{
+ return spacer_pace_rate (&tc->pacer);
+}
+
+void
+transport_connection_update_tx_bytes (transport_connection_t * tc, u32 bytes)
+{
+ if (transport_connection_is_tx_paced (tc))
+ spacer_update_bucket (&tc->pacer, bytes);
+}
+
+void
+transport_connection_tx_pacer_update_bytes (transport_connection_t * tc,
+ u32 bytes)
+{
+ spacer_update_bucket (&tc->pacer, bytes);
+}
+
+void
+transport_connection_reschedule (transport_connection_t * tc)
+{
+ tc->flags &= ~TRANSPORT_CONNECTION_F_DESCHED;
+ transport_connection_tx_pacer_reset_bucket (tc, TRANSPORT_PACER_MIN_BURST);
+ if (transport_max_tx_dequeue (tc))
+ sesssion_reschedule_tx (tc);
+ else
+ {
+ session_t *s = session_get (tc->s_index, tc->thread_index);
+ svm_fifo_unset_event (s->tx_fifo);
+ if (svm_fifo_max_dequeue_cons (s->tx_fifo))
+ if (svm_fifo_set_event (s->tx_fifo))
+ sesssion_reschedule_tx (tc);
+ }
+}
+
+void
+transport_update_time (clib_time_type_t time_now, u8 thread_index)
+{
+ transport_proto_vft_t *vft;
+ vec_foreach (vft, tp_vfts)
+ {
+ if (vft->update_time)
+ (vft->update_time) (time_now, thread_index);
+ }
+}
+
+void
+transport_enable_disable (vlib_main_t * vm, u8 is_en)
+{
+ transport_proto_vft_t *vft;
+ vec_foreach (vft, tp_vfts)
+ {
+ if (vft->enable)
+ (vft->enable) (vm, is_en);
+ }
+}
+