+
+ return clib_min (pacer->bucket, pacer->max_burst_size);
+}
+
+static inline void
+spacer_update_bucket (spacer_t * pacer, u32 bytes)
+{
+ ASSERT (pacer->bucket >= bytes);
+ pacer->bucket -= bytes;
+}
+
+static inline void
+spacer_update_max_burst_size (spacer_t * pacer, u32 max_burst_bytes)
+{
+ pacer->max_burst_size = clib_max (max_burst_bytes,
+ TRANSPORT_PACER_MIN_BURST);
+}
+
+static inline void
+spacer_set_pace_rate (spacer_t * pacer, u64 rate_bytes_per_sec)
+{
+ ASSERT (rate_bytes_per_sec != 0);
+ pacer->tokens_per_period = rate_bytes_per_sec / transport_pacer_period;
+}
+
+void
+transport_connection_tx_pacer_reset (transport_connection_t * tc,
+ u32 rate_bytes_per_sec,
+ u32 start_bucket, u64 time_now)
+{
+ spacer_t *pacer = &tc->pacer;
+ f64 dispatch_period;
+ u32 burst_size;
+
+ dispatch_period = transport_dispatch_period (tc->thread_index);
+ burst_size = rate_bytes_per_sec * dispatch_period;
+ spacer_update_max_burst_size (&tc->pacer, burst_size);
+ spacer_set_pace_rate (&tc->pacer, rate_bytes_per_sec);
+ pacer->last_update = time_now >> SPACER_CPU_TICKS_PER_PERIOD_SHIFT;
+ pacer->bucket = start_bucket;
+}
+
+void
+transport_connection_tx_pacer_init (transport_connection_t * tc,
+ u32 rate_bytes_per_sec,
+ u32 initial_bucket)
+{
+ vlib_main_t *vm = vlib_get_main ();
+ tc->flags |= TRANSPORT_CONNECTION_F_IS_TX_PACED;
+ transport_connection_tx_pacer_reset (tc, rate_bytes_per_sec,
+ initial_bucket,
+ vm->clib_time.last_cpu_time);
+}
+
+void
+transport_connection_tx_pacer_update (transport_connection_t * tc,
+ u64 bytes_per_sec)
+{
+ f64 dispatch_period = transport_dispatch_period (tc->thread_index);
+ u32 burst_size = 1.1 * bytes_per_sec * dispatch_period;
+ spacer_set_pace_rate (&tc->pacer, bytes_per_sec);
+ spacer_update_max_burst_size (&tc->pacer, burst_size);
+}
+
+u32
+transport_connection_tx_pacer_burst (transport_connection_t * tc,
+ u64 time_now)
+{
+ time_now >>= SPACER_CPU_TICKS_PER_PERIOD_SHIFT;
+ return spacer_max_burst (&tc->pacer, time_now);
+}
+
+u32
+transport_connection_snd_space (transport_connection_t * tc, u64 time_now,
+ u16 mss)
+{
+ u32 snd_space, max_paced_burst;
+
+ snd_space = tp_vfts[tc->proto].send_space (tc);
+ if (transport_connection_is_tx_paced (tc))
+ {
+ time_now >>= SPACER_CPU_TICKS_PER_PERIOD_SHIFT;
+ max_paced_burst = spacer_max_burst (&tc->pacer, time_now);
+ max_paced_burst = (max_paced_burst < mss) ? 0 : max_paced_burst;
+ snd_space = clib_min (snd_space, max_paced_burst);
+ snd_space = snd_space - snd_space % mss;
+ }
+ return snd_space;
+}
+
+void
+transport_connection_update_tx_stats (transport_connection_t * tc, u32 bytes)
+{
+ tc->stats.tx_bytes += 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_init_tx_pacers_period (void)
+{
+ f64 cpu_freq = os_cpu_clock_frequency ();
+ transport_pacer_period = cpu_freq / SPACER_CPU_TICKS_PER_PERIOD;