+ tcp_bt_check_app_limited (tc);
+ tcp_bt_track_tx (tc, n_bytes);
+ }
+
+ tcp_push_hdr_i (tc, b, tc->snd_nxt, /* compute opts */ 0,
+ /* burst */ 0, /* update_snd_nxt */ 1);
+ tc->snd_una_max = seq_max (tc->snd_nxt, tc->snd_una_max);
+ tcp_validate_txf_size (tc, tc->snd_una_max - tc->snd_una);
+ tcp_enqueue_to_output (wrk, b, bi, tc->c_is_ip4);
+
+ /* Just sent new data, enable retransmit */
+ tcp_retransmit_timer_update (tc);
+}
+
+/**
+ * Retransmit first unacked segment
+ */
+int
+tcp_retransmit_first_unacked (tcp_worker_ctx_t * wrk, tcp_connection_t * tc)
+{
+ vlib_main_t *vm = wrk->vm;
+ vlib_buffer_t *b;
+ u32 bi, n_bytes;
+
+ TCP_EVT (TCP_EVT_CC_EVT, tc, 1);
+
+ n_bytes = tcp_prepare_retransmit_segment (wrk, tc, 0, tc->snd_mss, &b);
+ if (!n_bytes)
+ return -1;
+
+ bi = vlib_get_buffer_index (vm, b);
+ tcp_enqueue_to_output (wrk, b, bi, tc->c_is_ip4);
+
+ return 0;
+}
+
+static int
+tcp_transmit_unsent (tcp_worker_ctx_t * wrk, tcp_connection_t * tc,
+ u32 burst_size)
+{
+ u32 offset, n_segs = 0, n_written, bi, available_wnd;
+ vlib_main_t *vm = wrk->vm;
+ vlib_buffer_t *b = 0;
+
+ offset = tc->snd_nxt - tc->snd_una;
+ available_wnd = tc->snd_wnd - offset;
+ burst_size = clib_min (burst_size, available_wnd / tc->snd_mss);
+
+ if (tc->cfg_flags & TCP_CFG_F_RATE_SAMPLE)
+ tcp_bt_check_app_limited (tc);
+
+ while (n_segs < burst_size)
+ {
+ n_written = tcp_prepare_segment (wrk, tc, offset, tc->snd_mss, &b);
+ if (!n_written)
+ goto done;
+
+ bi = vlib_get_buffer_index (vm, b);
+ tcp_enqueue_to_output (wrk, b, bi, tc->c_is_ip4);
+ offset += n_written;
+ n_segs += 1;
+
+ if (tc->cfg_flags & TCP_CFG_F_RATE_SAMPLE)
+ tcp_bt_track_tx (tc, n_written);
+
+ tc->snd_nxt += n_written;
+ tc->snd_una_max = seq_max (tc->snd_nxt, tc->snd_una_max);
+ }
+
+done:
+ return n_segs;
+}
+
+/**
+ * Estimate send space using proportional rate reduction (RFC6937)
+ */
+int
+tcp_fastrecovery_prr_snd_space (tcp_connection_t * tc)
+{
+ u32 pipe, prr_out;
+ int space;
+
+ pipe = tcp_flight_size (tc);
+ prr_out = tc->snd_rxt_bytes + (tc->snd_nxt - tc->snd_congestion);
+
+ if (pipe > tc->ssthresh)
+ {
+ space = ((int) tc->prr_delivered * ((f64) tc->ssthresh / tc->prev_cwnd))
+ - prr_out;
+ }
+ else
+ {
+ int limit;
+ limit = clib_max ((int) (tc->prr_delivered - prr_out), 0) + tc->snd_mss;
+ space = clib_min (tc->ssthresh - pipe, limit);
+ }
+ space = clib_max (space, prr_out ? 0 : tc->snd_mss);
+ return space;
+}
+
+static inline u8
+tcp_retransmit_should_retry_head (tcp_connection_t * tc,
+ sack_scoreboard_t * sb)
+{
+ u32 tx_adv_sack = sb->high_sacked - tc->snd_congestion;
+ f64 rr = (f64) tc->ssthresh / tc->prev_cwnd;
+
+ if (tcp_fastrecovery_first (tc))
+ return 1;
+
+ return (tx_adv_sack > (tc->snd_una - tc->prr_start) * rr);
+}
+
+static inline u8
+tcp_max_tx_deq (tcp_connection_t * tc)
+{
+ return (transport_max_tx_dequeue (&tc->connection)
+ - (tc->snd_nxt - tc->snd_una));
+}
+
+#define scoreboard_rescue_rxt_valid(_sb, _tc) \
+ (seq_geq (_sb->rescue_rxt, _tc->snd_una) \
+ && seq_leq (_sb->rescue_rxt, _tc->snd_congestion))
+
+/**
+ * Do retransmit with SACKs
+ */
+static int
+tcp_retransmit_sack (tcp_worker_ctx_t * wrk, tcp_connection_t * tc,
+ u32 burst_size)
+{
+ u32 n_written = 0, offset, max_bytes, n_segs = 0;
+ u8 snd_limited = 0, can_rescue = 0;
+ u32 bi, max_deq, burst_bytes;
+ sack_scoreboard_hole_t *hole;
+ vlib_main_t *vm = wrk->vm;
+ vlib_buffer_t *b = 0;
+ sack_scoreboard_t *sb;
+ int snd_space;
+
+ ASSERT (tcp_in_cong_recovery (tc));
+
+ burst_bytes = transport_connection_tx_pacer_burst (&tc->connection);
+ burst_size = clib_min (burst_size, burst_bytes / tc->snd_mss);
+ if (!burst_size)
+ {
+ tcp_program_retransmit (tc);
+ return 0;
+ }
+
+ if (tcp_in_recovery (tc))
+ snd_space = tcp_available_cc_snd_space (tc);
+ else
+ snd_space = tcp_fastrecovery_prr_snd_space (tc);
+
+ if (snd_space < tc->snd_mss)
+ goto done;
+
+ sb = &tc->sack_sb;
+
+ /* Check if snd_una is a lost retransmit */
+ if (pool_elts (sb->holes)
+ && seq_gt (sb->high_sacked, tc->snd_congestion)
+ && tc->rxt_head != tc->snd_una
+ && tcp_retransmit_should_retry_head (tc, sb))
+ {
+ max_bytes = clib_min (tc->snd_mss, tc->snd_congestion - tc->snd_una);
+ n_written = tcp_prepare_retransmit_segment (wrk, tc, 0, max_bytes, &b);
+ if (!n_written)