lsquic_send_ctl.c revision f4841319
1/* Copyright (c) 2017 - 2020 LiteSpeed Technologies Inc.  See LICENSE. */
2/*
3 * lsquic_send_ctl.c -- Logic for sending and sent packets
4 */
5
6#include <assert.h>
7#include <errno.h>
8#include <inttypes.h>
9#include <stdlib.h>
10#include <string.h>
11#include <sys/queue.h>
12
13#include <openssl/rand.h>
14
15#include "lsquic_types.h"
16#include "lsquic_int_types.h"
17#include "lsquic.h"
18#include "lsquic_mm.h"
19#include "lsquic_engine_public.h"
20#include "lsquic_packet_common.h"
21#include "lsquic_alarmset.h"
22#include "lsquic_parse.h"
23#include "lsquic_packet_out.h"
24#include "lsquic_senhist.h"
25#include "lsquic_rtt.h"
26#include "lsquic_cubic.h"
27#include "lsquic_pacer.h"
28#include "lsquic_bw_sampler.h"
29#include "lsquic_minmax.h"
30#include "lsquic_bbr.h"
31#include "lsquic_send_ctl.h"
32#include "lsquic_util.h"
33#include "lsquic_sfcw.h"
34#include "lsquic_varint.h"
35#include "lsquic_hq.h"
36#include "lsquic_hash.h"
37#include "lsquic_stream.h"
38#include "lsquic_ver_neg.h"
39#include "lsquic_ev_log.h"
40#include "lsquic_conn.h"
41#include "lsquic_conn_flow.h"
42#include "lsquic_conn_public.h"
43#include "lsquic_cong_ctl.h"
44#include "lsquic_enc_sess.h"
45#include "lsquic_malo.h"
46#include "lsquic_attq.h"
47#include "lsquic_http1x_if.h"
48#include "lsqpack.h"
49#include "lsquic_frab_list.h"
50#include "lsquic_qdec_hdl.h"
51#include "lsquic_crand.h"
52
53#define LSQUIC_LOGGER_MODULE LSQLM_SENDCTL
54#define LSQUIC_LOG_CONN_ID lsquic_conn_log_cid(ctl->sc_conn_pub->lconn)
55#include "lsquic_logger.h"
56
57#define MAX_RESUBMITTED_ON_RTO  2
58#define MAX_RTO_BACKOFFS        10
59#define DEFAULT_RETX_DELAY      500000      /* Microseconds */
60#define MAX_RTO_DELAY           60000000    /* Microseconds */
61#define MIN_RTO_DELAY           1000000      /* Microseconds */
62#define N_NACKS_BEFORE_RETX     3
63
64#define CGP(ctl) ((struct cong_ctl *) &(ctl)->sc_cong_u)
65
66#define packet_out_total_sz(p) \
67                lsquic_packet_out_total_sz(ctl->sc_conn_pub->lconn, p)
68#define packet_out_sent_sz(p) \
69                lsquic_packet_out_sent_sz(ctl->sc_conn_pub->lconn, p)
70
71enum retx_mode {
72    RETX_MODE_HANDSHAKE,
73    RETX_MODE_LOSS,
74    RETX_MODE_TLP,
75    RETX_MODE_RTO,
76};
77
78
79static const char *const retx2str[] = {
80    [RETX_MODE_HANDSHAKE] = "RETX_MODE_HANDSHAKE",
81    [RETX_MODE_LOSS]      = "RETX_MODE_LOSS",
82    [RETX_MODE_TLP]       = "RETX_MODE_TLP",
83    [RETX_MODE_RTO]       = "RETX_MODE_RTO",
84};
85
86#ifdef NDEBUG
87#define MAX_BPQ_COUNT 10
88#else
89static unsigned MAX_BPQ_COUNT = 10;
90void
91lsquic_send_ctl_set_max_bpq_count (unsigned count) { MAX_BPQ_COUNT = count; }
92#endif
93
94
95static void
96update_for_resending (lsquic_send_ctl_t *ctl, lsquic_packet_out_t *packet_out);
97
98
99enum expire_filter { EXFI_ALL, EXFI_HSK, EXFI_LAST, };
100
101
102static void
103send_ctl_expire (struct lsquic_send_ctl *, enum packnum_space,
104                                                        enum expire_filter);
105
106static void
107set_retx_alarm (struct lsquic_send_ctl *, enum packnum_space, lsquic_time_t);
108
109static void
110send_ctl_detect_losses (struct lsquic_send_ctl *, enum packnum_space,
111                                                        lsquic_time_t time);
112
113static unsigned
114send_ctl_retx_bytes_out (const struct lsquic_send_ctl *ctl);
115
116static unsigned
117send_ctl_all_bytes_out (const struct lsquic_send_ctl *ctl);
118
119static void
120send_ctl_reschedule_poison (struct lsquic_send_ctl *ctl);
121
122#ifdef NDEBUG
123static
124#elif __GNUC__
125__attribute__((weak))
126#endif
127int
128lsquic_send_ctl_schedule_stream_packets_immediately (lsquic_send_ctl_t *ctl)
129{
130    return !(ctl->sc_flags & SC_BUFFER_STREAM);
131}
132
133
134#ifdef NDEBUG
135static
136#elif __GNUC__
137__attribute__((weak))
138#endif
139enum packno_bits
140lsquic_send_ctl_guess_packno_bits (lsquic_send_ctl_t *ctl)
141{
142    return PACKNO_BITS_1;   /* This is 2 bytes in both GQUIC and IQUIC */
143}
144
145
146int
147lsquic_send_ctl_have_unacked_stream_frames (const lsquic_send_ctl_t *ctl)
148{
149    const lsquic_packet_out_t *packet_out;
150
151    TAILQ_FOREACH(packet_out, &ctl->sc_unacked_packets[PNS_APP], po_next)
152        if (0 == (packet_out->po_flags & (PO_LOSS_REC|PO_POISON))
153                && (packet_out->po_frame_types &
154                    ((1 << QUIC_FRAME_STREAM) | (1 << QUIC_FRAME_RST_STREAM))))
155            return 1;
156
157    return 0;
158}
159
160
161static lsquic_packet_out_t *
162send_ctl_first_unacked_retx_packet (const struct lsquic_send_ctl *ctl,
163                                                        enum packnum_space pns)
164{
165    lsquic_packet_out_t *packet_out;
166
167    TAILQ_FOREACH(packet_out, &ctl->sc_unacked_packets[pns], po_next)
168        if (0 == (packet_out->po_flags & (PO_LOSS_REC|PO_POISON))
169                && (packet_out->po_frame_types & ctl->sc_retx_frames))
170            return packet_out;
171
172    return NULL;
173}
174
175
176static lsquic_packet_out_t *
177send_ctl_last_unacked_retx_packet (const struct lsquic_send_ctl *ctl,
178                                                    enum packnum_space pns)
179{
180    lsquic_packet_out_t *packet_out;
181    TAILQ_FOREACH_REVERSE(packet_out, &ctl->sc_unacked_packets[pns],
182                                            lsquic_packets_tailq, po_next)
183        if (0 == (packet_out->po_flags & (PO_LOSS_REC|PO_POISON))
184                && (packet_out->po_frame_types & ctl->sc_retx_frames))
185            return packet_out;
186    return NULL;
187}
188
189
190static int
191have_unacked_handshake_packets (const lsquic_send_ctl_t *ctl)
192{
193    const lsquic_packet_out_t *packet_out;
194    enum packnum_space pns;
195
196    for (pns = ctl->sc_flags & SC_IETF ? PNS_INIT : PNS_APP; pns < N_PNS; ++pns)
197        TAILQ_FOREACH(packet_out, &ctl->sc_unacked_packets[pns], po_next)
198            if (packet_out->po_flags & PO_HELLO)
199                return 1;
200    return 0;
201}
202
203
204static enum retx_mode
205get_retx_mode (const lsquic_send_ctl_t *ctl)
206{
207    if (!(ctl->sc_conn_pub->lconn->cn_flags & LSCONN_HANDSHAKE_DONE)
208                                    && have_unacked_handshake_packets(ctl))
209        return RETX_MODE_HANDSHAKE;
210    if (ctl->sc_loss_to)
211        return RETX_MODE_LOSS;
212    if (ctl->sc_n_tlp < 2)
213        return RETX_MODE_TLP;
214    return RETX_MODE_RTO;
215}
216
217
218static lsquic_time_t
219get_retx_delay (const struct lsquic_rtt_stats *rtt_stats)
220{
221    lsquic_time_t srtt, delay;
222
223    srtt = lsquic_rtt_stats_get_srtt(rtt_stats);
224    if (srtt)
225    {
226        delay = srtt + 4 * lsquic_rtt_stats_get_rttvar(rtt_stats);
227        if (delay < MIN_RTO_DELAY)
228            delay = MIN_RTO_DELAY;
229    }
230    else
231        delay = DEFAULT_RETX_DELAY;
232
233    return delay;
234}
235
236
237static void
238retx_alarm_rings (enum alarm_id al_id, void *ctx, lsquic_time_t expiry, lsquic_time_t now)
239{
240    lsquic_send_ctl_t *ctl = ctx;
241    lsquic_packet_out_t *packet_out;
242    enum packnum_space pns;
243    enum retx_mode rm;
244
245    pns = al_id - AL_RETX_INIT;
246
247    /* This is a callback -- before it is called, the alarm is unset */
248    assert(!lsquic_alarmset_is_set(ctl->sc_alset, AL_RETX_INIT + pns));
249
250    rm = get_retx_mode(ctl);
251    LSQ_INFO("retx timeout, mode %s", retx2str[rm]);
252
253    switch (rm)
254    {
255    case RETX_MODE_HANDSHAKE:
256        send_ctl_expire(ctl, pns, EXFI_HSK);
257        /* Do not register cubic loss during handshake */
258        break;
259    case RETX_MODE_LOSS:
260        send_ctl_detect_losses(ctl, pns, now);
261        break;
262    case RETX_MODE_TLP:
263        ++ctl->sc_n_tlp;
264        send_ctl_expire(ctl, pns, EXFI_LAST);
265        break;
266    case RETX_MODE_RTO:
267        ctl->sc_last_rto_time = now;
268        ++ctl->sc_n_consec_rtos;
269        ctl->sc_next_limit = 2;
270        LSQ_DEBUG("packet RTO is %"PRIu64" usec", expiry);
271        send_ctl_expire(ctl, pns, EXFI_ALL);
272        ctl->sc_ci->cci_timeout(CGP(ctl));
273        break;
274    }
275
276    packet_out = send_ctl_first_unacked_retx_packet(ctl, pns);
277    if (packet_out)
278        set_retx_alarm(ctl, pns, now);
279    lsquic_send_ctl_sanity_check(ctl);
280}
281
282
283static lsquic_packno_t
284first_packno (const struct lsquic_send_ctl *ctl)
285{
286    if (ctl->sc_flags & SC_IETF)
287        return 0;
288    else
289        return 1;
290}
291
292
293/*
294 * [draft-ietf-quic-transport-12], Section 4.4.1:
295 *
296 * "   The first Initial packet that is sent by a client contains a packet
297 * "   number of 0.  All subsequent packets contain a packet number that is
298 * "   incremented by at least one, see (Section 4.8).
299 */
300static void
301send_ctl_pick_initial_packno (struct lsquic_send_ctl *ctl)
302{
303    ctl->sc_cur_packno = first_packno(ctl) - 1;
304}
305
306
307void
308lsquic_send_ctl_init (lsquic_send_ctl_t *ctl, struct lsquic_alarmset *alset,
309          struct lsquic_engine_public *enpub, const struct ver_neg *ver_neg,
310          struct lsquic_conn_public *conn_pub, enum send_ctl_flags flags)
311{
312    unsigned i, algo;
313    memset(ctl, 0, sizeof(*ctl));
314    TAILQ_INIT(&ctl->sc_scheduled_packets);
315    TAILQ_INIT(&ctl->sc_unacked_packets[PNS_INIT]);
316    TAILQ_INIT(&ctl->sc_unacked_packets[PNS_HSK]);
317    TAILQ_INIT(&ctl->sc_unacked_packets[PNS_APP]);
318    TAILQ_INIT(&ctl->sc_lost_packets);
319    ctl->sc_enpub = enpub;
320    ctl->sc_alset = alset;
321    ctl->sc_ver_neg = ver_neg;
322    ctl->sc_conn_pub = conn_pub;
323    assert(!(flags & ~(SC_IETF|SC_NSTP|SC_ECN)));
324    ctl->sc_flags = flags;
325    send_ctl_pick_initial_packno(ctl);
326    if (enpub->enp_settings.es_pace_packets)
327        ctl->sc_flags |= SC_PACE;
328    if (flags & SC_ECN)
329        ctl->sc_ecn = ECN_ECT0;
330    else
331        ctl->sc_ecn = ECN_NOT_ECT;
332    if (flags & SC_IETF)
333        ctl->sc_retx_frames = IQUIC_FRAME_RETX_MASK;
334    else
335        ctl->sc_retx_frames = GQUIC_FRAME_RETRANSMITTABLE_MASK;
336    lsquic_alarmset_init_alarm(alset, AL_RETX_INIT, retx_alarm_rings, ctl);
337    lsquic_alarmset_init_alarm(alset, AL_RETX_HSK, retx_alarm_rings, ctl);
338    lsquic_alarmset_init_alarm(alset, AL_RETX_APP, retx_alarm_rings, ctl);
339    lsquic_senhist_init(&ctl->sc_senhist, ctl->sc_flags & SC_IETF);
340    if (0 == enpub->enp_settings.es_cc_algo)
341        algo = LSQUIC_DF_CC_ALGO;
342    else
343        algo = enpub->enp_settings.es_cc_algo;
344    if (algo == 2)
345        ctl->sc_ci = &lsquic_cong_bbr_if;
346    else
347        ctl->sc_ci = &lsquic_cong_cubic_if;
348    ctl->sc_ci->cci_init(CGP(ctl), conn_pub, ctl->sc_retx_frames);
349    if (ctl->sc_flags & SC_PACE)
350        pacer_init(&ctl->sc_pacer, conn_pub->lconn,
351        /* TODO: conn_pub has a pointer to enpub: drop third argument */
352                                    enpub->enp_settings.es_clock_granularity);
353    for (i = 0; i < sizeof(ctl->sc_buffered_packets) /
354                                sizeof(ctl->sc_buffered_packets[0]); ++i)
355        TAILQ_INIT(&ctl->sc_buffered_packets[i].bpq_packets);
356    ctl->sc_max_packno_bits = PACKNO_BITS_2; /* Safe value before verneg */
357    ctl->sc_cached_bpt.stream_id = UINT64_MAX;
358#if LSQUIC_EXTRA_CHECKS
359    ctl->sc_flags |= SC_SANITY_CHECK;
360#else
361    if ((ctl->sc_conn_pub->lconn->cn_flags & (LSCONN_IETF|LSCONN_SERVER))
362                                                                == LSCONN_IETF)
363        ctl->sc_flags |= SC_SANITY_CHECK;
364#endif
365    ctl->sc_gap = UINT64_MAX - 1 /* Can't have +1 == 0 */;
366}
367
368
369static int
370send_ctl_ecn_on (const struct lsquic_send_ctl *ctl)
371{
372    return ctl->sc_ecn != ECN_NOT_ECT;
373}
374
375
376static lsquic_time_t
377calculate_packet_rto (lsquic_send_ctl_t *ctl)
378{
379    lsquic_time_t delay;
380
381    delay = get_retx_delay(&ctl->sc_conn_pub->rtt_stats);
382
383    unsigned exp = ctl->sc_n_consec_rtos;
384    if (exp > MAX_RTO_BACKOFFS)
385        exp = MAX_RTO_BACKOFFS;
386
387    delay = delay * (1 << exp);
388
389    return delay;
390}
391
392
393static lsquic_time_t
394calculate_tlp_delay (lsquic_send_ctl_t *ctl)
395{
396    lsquic_time_t srtt, delay;
397
398    srtt = lsquic_rtt_stats_get_srtt(&ctl->sc_conn_pub->rtt_stats);
399    if (ctl->sc_n_in_flight_all > 1)
400    {
401        delay = 10000;  /* 10 ms is the minimum tail loss probe delay */
402        if (delay < 2 * srtt)
403            delay = 2 * srtt;
404    }
405    else
406    {
407        delay = srtt + srtt / 2 + MIN_RTO_DELAY;
408        if (delay < 2 * srtt)
409            delay = 2 * srtt;
410    }
411
412    return delay;
413}
414
415
416static void
417set_retx_alarm (struct lsquic_send_ctl *ctl, enum packnum_space pns,
418                                                            lsquic_time_t now)
419{
420    enum retx_mode rm;
421    lsquic_time_t delay;
422
423    assert(!TAILQ_EMPTY(&ctl->sc_unacked_packets[pns]));
424
425    rm = get_retx_mode(ctl);
426    switch (rm)
427    {
428    case RETX_MODE_HANDSHAKE:
429    /* [draft-iyengar-quic-loss-recovery-01]:
430     *
431     *  if (handshake packets are outstanding):
432     *      alarm_duration = max(1.5 * smoothed_rtt, 10ms) << handshake_count;
433     *      handshake_count++;
434     */
435        delay = lsquic_rtt_stats_get_srtt(&ctl->sc_conn_pub->rtt_stats);
436        if (delay)
437        {
438            delay += delay / 2;
439            if (10000 > delay)
440                delay = 10000;
441        }
442        else
443            delay = 150000;
444        delay <<= ctl->sc_n_hsk;
445        ++ctl->sc_n_hsk;
446        break;
447    case RETX_MODE_LOSS:
448        delay = ctl->sc_loss_to;
449        break;
450    case RETX_MODE_TLP:
451        delay = calculate_tlp_delay(ctl);
452        break;
453    default:
454        assert(rm == RETX_MODE_RTO);
455        /* XXX the comment below as well as the name of the function
456         * that follows seem obsolete.
457         */
458        /* Base RTO on the first unacked packet, following reference
459         * implementation.
460         */
461        delay = calculate_packet_rto(ctl);
462        break;
463    }
464
465    if (delay > MAX_RTO_DELAY)
466        delay = MAX_RTO_DELAY;
467
468    LSQ_DEBUG("set retx alarm to %"PRIu64", which is %"PRIu64
469        " usec from now, mode %s", now + delay, delay, retx2str[rm]);
470    lsquic_alarmset_set(ctl->sc_alset, AL_RETX_INIT + pns, now + delay);
471}
472
473
474static int
475send_ctl_in_recovery (lsquic_send_ctl_t *ctl)
476{
477    return ctl->sc_largest_acked_packno
478        && ctl->sc_largest_acked_packno <= ctl->sc_largest_sent_at_cutback;
479}
480
481
482#define SC_PACK_SIZE(ctl_) (+(ctl_)->sc_conn_pub->path->np_pack_size)
483
484static lsquic_time_t
485send_ctl_transfer_time (void *ctx)
486{
487    lsquic_send_ctl_t *const ctl = ctx;
488    lsquic_time_t tx_time;
489    uint64_t pacing_rate;
490    int in_recovery;
491
492    in_recovery = send_ctl_in_recovery(ctl);
493    pacing_rate = ctl->sc_ci->cci_pacing_rate(CGP(ctl), in_recovery);
494    tx_time = (uint64_t) SC_PACK_SIZE(ctl) * 1000000 / pacing_rate;
495    return tx_time;
496}
497
498
499static void
500send_ctl_unacked_append (struct lsquic_send_ctl *ctl,
501                         struct lsquic_packet_out *packet_out)
502{
503    enum packnum_space pns;
504
505    pns = lsquic_packet_out_pns(packet_out);
506    assert(0 == (packet_out->po_flags & (PO_LOSS_REC|PO_POISON)));
507    TAILQ_INSERT_TAIL(&ctl->sc_unacked_packets[pns], packet_out, po_next);
508    packet_out->po_flags |= PO_UNACKED;
509    ctl->sc_bytes_unacked_all += packet_out_sent_sz(packet_out);
510    ctl->sc_n_in_flight_all  += 1;
511    if (packet_out->po_frame_types & ctl->sc_retx_frames)
512    {
513        ctl->sc_bytes_unacked_retx += packet_out_total_sz(packet_out);
514        ++ctl->sc_n_in_flight_retx;
515    }
516}
517
518
519static void
520send_ctl_unacked_remove (struct lsquic_send_ctl *ctl,
521                     struct lsquic_packet_out *packet_out, unsigned packet_sz)
522{
523    enum packnum_space pns;
524
525    pns = lsquic_packet_out_pns(packet_out);
526    TAILQ_REMOVE(&ctl->sc_unacked_packets[pns], packet_out, po_next);
527    packet_out->po_flags &= ~PO_UNACKED;
528    assert(ctl->sc_bytes_unacked_all >= packet_sz);
529    ctl->sc_bytes_unacked_all -= packet_sz;
530    ctl->sc_n_in_flight_all  -= 1;
531    if (packet_out->po_frame_types & ctl->sc_retx_frames)
532    {
533        ctl->sc_bytes_unacked_retx -= packet_sz;
534        --ctl->sc_n_in_flight_retx;
535    }
536}
537
538
539static void
540send_ctl_sched_Xpend_common (struct lsquic_send_ctl *ctl,
541                      struct lsquic_packet_out *packet_out)
542{
543    packet_out->po_flags |= PO_SCHED;
544    ++ctl->sc_n_scheduled;
545    ctl->sc_bytes_scheduled += packet_out_total_sz(packet_out);
546    lsquic_send_ctl_sanity_check(ctl);
547}
548
549
550static void
551send_ctl_sched_append (struct lsquic_send_ctl *ctl,
552                       struct lsquic_packet_out *packet_out)
553{
554    TAILQ_INSERT_TAIL(&ctl->sc_scheduled_packets, packet_out, po_next);
555    send_ctl_sched_Xpend_common(ctl, packet_out);
556}
557
558
559static void
560send_ctl_sched_prepend (struct lsquic_send_ctl *ctl,
561                       struct lsquic_packet_out *packet_out)
562{
563    TAILQ_INSERT_HEAD(&ctl->sc_scheduled_packets, packet_out, po_next);
564    send_ctl_sched_Xpend_common(ctl, packet_out);
565}
566
567
568static void
569send_ctl_sched_remove (struct lsquic_send_ctl *ctl,
570                       struct lsquic_packet_out *packet_out)
571{
572    TAILQ_REMOVE(&ctl->sc_scheduled_packets, packet_out, po_next);
573    packet_out->po_flags &= ~PO_SCHED;
574    assert(ctl->sc_n_scheduled);
575    --ctl->sc_n_scheduled;
576    ctl->sc_bytes_scheduled -= packet_out_total_sz(packet_out);
577    lsquic_send_ctl_sanity_check(ctl);
578}
579
580
581/* Poisoned packets are used to detect optimistic ACK attacks.  We only
582 * use a single poisoned packet at a time.
583 */
584static int
585send_ctl_add_poison (struct lsquic_send_ctl *ctl)
586{
587    struct lsquic_packet_out *poison;
588
589    poison = lsquic_malo_get(ctl->sc_conn_pub->packet_out_malo);
590    if (!poison)
591        return -1;
592
593    memset(poison, 0, sizeof(*poison));
594    poison->po_flags      = PO_UNACKED|PO_POISON;
595    poison->po_packno     = ctl->sc_gap;
596    poison->po_loss_chain = poison; /* Won't be used, but just in case */
597    TAILQ_INSERT_TAIL(&ctl->sc_unacked_packets[PNS_APP], poison, po_next);
598    LSQ_DEBUG("insert poisoned packet %"PRIu64, poison->po_packno);
599    ctl->sc_flags |= SC_POISON;
600    return 0;
601}
602
603
604static void
605send_ctl_reschedule_poison (struct lsquic_send_ctl *ctl)
606{
607    struct lsquic_packet_out *poison;
608    enum lsq_log_level log_level;
609    lsquic_time_t approx_now;
610
611    TAILQ_FOREACH(poison, &ctl->sc_unacked_packets[PNS_APP], po_next)
612        if (poison->po_flags & PO_POISON)
613        {
614            LSQ_DEBUG("remove poisoned packet %"PRIu64, poison->po_packno);
615            TAILQ_REMOVE(&ctl->sc_unacked_packets[PNS_APP], poison, po_next);
616            lsquic_malo_put(poison);
617            lsquic_send_ctl_begin_optack_detection(ctl);
618            ctl->sc_flags &= ~SC_POISON;
619            return;
620        }
621
622    approx_now = ctl->sc_last_sent_time;
623    if (0 == ctl->sc_enpub->enp_last_warning[WT_NO_POISON]
624                || ctl->sc_enpub->enp_last_warning[WT_NO_POISON]
625                                            + WARNING_INTERVAL < approx_now)
626    {
627        ctl->sc_enpub->enp_last_warning[WT_NO_POISON] = approx_now;
628        log_level = LSQ_LOG_WARN;
629    }
630    else
631        log_level = LSQ_LOG_DEBUG;
632    LSQ_LOG(log_level, "odd: poisoned packet %"PRIu64" not found during "
633        "reschedule, flag: %d", ctl->sc_gap, !!(ctl->sc_flags & SC_POISON));
634}
635
636
637int
638lsquic_send_ctl_sent_packet (lsquic_send_ctl_t *ctl,
639                             struct lsquic_packet_out *packet_out)
640{
641    enum packnum_space pns;
642    char frames[lsquic_frame_types_str_sz];
643
644    assert(!(packet_out->po_flags & PO_ENCRYPTED));
645    ctl->sc_last_sent_time = packet_out->po_sent;
646    pns = lsquic_packet_out_pns(packet_out);
647    if (packet_out->po_packno == ctl->sc_gap + 1 && pns == PNS_APP)
648    {
649        assert(!(ctl->sc_flags & SC_POISON));
650        lsquic_senhist_add(&ctl->sc_senhist, ctl->sc_gap);
651        if (0 != send_ctl_add_poison(ctl))
652            return -1;
653    }
654    LSQ_DEBUG("packet %"PRIu64" has been sent (frame types: %s)",
655        packet_out->po_packno, lsquic_frame_types_to_str(frames,
656            sizeof(frames), packet_out->po_frame_types));
657    lsquic_senhist_add(&ctl->sc_senhist, packet_out->po_packno);
658    send_ctl_unacked_append(ctl, packet_out);
659    if (packet_out->po_frame_types & ctl->sc_retx_frames)
660    {
661        if (!lsquic_alarmset_is_set(ctl->sc_alset, AL_RETX_INIT + pns))
662            set_retx_alarm(ctl, pns, packet_out->po_sent);
663        if (ctl->sc_n_in_flight_retx == 1)
664            ctl->sc_flags |= SC_WAS_QUIET;
665    }
666    /* TODO: Do we really want to use those for RTT info? Revisit this. */
667    /* Hold on to packets that are not retransmittable because we need them
668     * to sample RTT information.  They are released when ACK is received.
669     */
670#if LSQUIC_SEND_STATS
671    ++ctl->sc_stats.n_total_sent;
672#endif
673    if (ctl->sc_ci->cci_sent)
674        ctl->sc_ci->cci_sent(CGP(ctl), packet_out, ctl->sc_n_in_flight_all,
675                                            ctl->sc_flags & SC_APP_LIMITED);
676    lsquic_send_ctl_sanity_check(ctl);
677    return 0;
678}
679
680
681static void
682take_rtt_sample (lsquic_send_ctl_t *ctl,
683                 lsquic_time_t now, lsquic_time_t lack_delta)
684{
685    const lsquic_packno_t packno = ctl->sc_largest_acked_packno;
686    const lsquic_time_t sent = ctl->sc_largest_acked_sent_time;
687    const lsquic_time_t measured_rtt = now - sent;
688    if (packno > ctl->sc_max_rtt_packno && lack_delta < measured_rtt)
689    {
690        ctl->sc_max_rtt_packno = packno;
691        lsquic_rtt_stats_update(&ctl->sc_conn_pub->rtt_stats, measured_rtt, lack_delta);
692        LSQ_DEBUG("packno %"PRIu64"; rtt: %"PRIu64"; delta: %"PRIu64"; "
693            "new srtt: %"PRIu64, packno, measured_rtt, lack_delta,
694            lsquic_rtt_stats_get_srtt(&ctl->sc_conn_pub->rtt_stats));
695    }
696}
697
698
699static void
700send_ctl_return_enc_data (struct lsquic_send_ctl *ctl,
701                                        struct lsquic_packet_out *packet_out)
702{
703    ctl->sc_enpub->enp_pmi->pmi_return(ctl->sc_enpub->enp_pmi_ctx,
704        packet_out->po_path->np_peer_ctx,
705        packet_out->po_enc_data, lsquic_packet_out_ipv6(packet_out));
706    packet_out->po_flags &= ~PO_ENCRYPTED;
707    packet_out->po_enc_data = NULL;
708}
709
710
711static void
712send_ctl_destroy_packet (struct lsquic_send_ctl *ctl,
713                                        struct lsquic_packet_out *packet_out)
714{
715    if (0 == (packet_out->po_flags & (PO_LOSS_REC|PO_POISON)))
716        lsquic_packet_out_destroy(packet_out, ctl->sc_enpub,
717                                            packet_out->po_path->np_peer_ctx);
718    else
719        lsquic_malo_put(packet_out);
720}
721
722
723static void
724send_ctl_maybe_renumber_sched_to_right (struct lsquic_send_ctl *ctl,
725                                        const struct lsquic_packet_out *cur)
726{
727    struct lsquic_packet_out *packet_out;
728
729    /* If current packet has PO_REPACKNO set, it means that all those to the
730     * right of it have this flag set as well.
731     */
732    if (0 == (cur->po_flags & PO_REPACKNO))
733    {
734        ctl->sc_cur_packno = cur->po_packno - 1;
735        for (packet_out = TAILQ_NEXT(cur, po_next);
736                packet_out && 0 == (packet_out->po_flags & PO_REPACKNO);
737                    packet_out = TAILQ_NEXT(packet_out, po_next))
738        {
739            packet_out->po_flags |= PO_REPACKNO;
740        }
741    }
742}
743
744
745/* The third argument to advance `next' pointer when modifying the unacked
746 * queue.  This is because the unacked queue may contain several elements
747 * of the same chain.  This is not true of the lost and scheduled packet
748 * queue, as the loss records are only present on the unacked queue.
749 */
750static void
751send_ctl_destroy_chain (struct lsquic_send_ctl *ctl,
752                        struct lsquic_packet_out *const packet_out,
753                        struct lsquic_packet_out **next)
754{
755    struct lsquic_packet_out *chain_cur, *chain_next;
756    unsigned packet_sz, count;
757    enum packnum_space pns = lsquic_packet_out_pns(packet_out);
758
759    count = 0;
760    for (chain_cur = packet_out->po_loss_chain; chain_cur != packet_out;
761                                                    chain_cur = chain_next)
762    {
763        chain_next = chain_cur->po_loss_chain;
764        switch (chain_cur->po_flags & (PO_SCHED|PO_UNACKED|PO_LOST))
765        {
766        case PO_SCHED:
767            send_ctl_maybe_renumber_sched_to_right(ctl, chain_cur);
768            send_ctl_sched_remove(ctl, chain_cur);
769            break;
770        case PO_UNACKED:
771            if (chain_cur->po_flags & PO_LOSS_REC)
772                TAILQ_REMOVE(&ctl->sc_unacked_packets[pns], chain_cur, po_next);
773            else
774            {
775                packet_sz = packet_out_sent_sz(chain_cur);
776                send_ctl_unacked_remove(ctl, chain_cur, packet_sz);
777            }
778            break;
779        case PO_LOST:
780            TAILQ_REMOVE(&ctl->sc_lost_packets, chain_cur, po_next);
781            break;
782        case 0:
783            /* This is also weird, but let it pass */
784            break;
785        default:
786            assert(0);
787            break;
788        }
789        if (next && *next == chain_cur)
790            *next = TAILQ_NEXT(*next, po_next);
791        if (0 == (chain_cur->po_flags & PO_LOSS_REC))
792            lsquic_packet_out_ack_streams(chain_cur);
793        send_ctl_destroy_packet(ctl, chain_cur);
794        ++count;
795    }
796    packet_out->po_loss_chain = packet_out;
797
798    if (count)
799        LSQ_DEBUG("destroyed %u packet%.*s in chain of packet %"PRIu64,
800            count, count != 1, "s", packet_out->po_packno);
801}
802
803
804static void
805send_ctl_record_loss (struct lsquic_send_ctl *ctl,
806                                        struct lsquic_packet_out *packet_out)
807{
808    struct lsquic_packet_out *loss_record;
809
810    loss_record = lsquic_malo_get(ctl->sc_conn_pub->packet_out_malo);
811    if (loss_record)
812    {
813        memset(loss_record, 0, sizeof(*loss_record));
814        loss_record->po_flags = PO_UNACKED|PO_LOSS_REC|PO_SENT_SZ;
815        loss_record->po_flags |=
816            ((packet_out->po_flags >> POPNS_SHIFT) & 3) << POPNS_SHIFT;
817        /* Copy values used in ACK processing: */
818        loss_record->po_packno = packet_out->po_packno;
819        loss_record->po_sent = packet_out->po_sent;
820        loss_record->po_sent_sz = packet_out_sent_sz(packet_out);
821        loss_record->po_frame_types = packet_out->po_frame_types;
822        /* Insert the loss record into the chain: */
823        loss_record->po_loss_chain = packet_out->po_loss_chain;
824        packet_out->po_loss_chain = loss_record;
825        /* Place the loss record next to the lost packet we are about to
826         * remove from the list:
827         */
828        TAILQ_INSERT_BEFORE(packet_out, loss_record, po_next);
829    }
830    else
831        LSQ_INFO("cannot allocate memory for loss record");
832}
833
834
835/* Returns true if packet was rescheduled, false otherwise.  In the latter
836 * case, you should not dereference packet_out after the function returns.
837 */
838static int
839send_ctl_handle_lost_packet (lsquic_send_ctl_t *ctl,
840            lsquic_packet_out_t *packet_out, struct lsquic_packet_out **next)
841{
842    unsigned packet_sz;
843
844    assert(ctl->sc_n_in_flight_all);
845    packet_sz = packet_out_sent_sz(packet_out);
846
847    ++ctl->sc_loss_count;
848
849    if (packet_out->po_frame_types & (1 << QUIC_FRAME_ACK))
850    {
851        ctl->sc_flags |= SC_LOST_ACK_INIT << lsquic_packet_out_pns(packet_out);
852        LSQ_DEBUG("lost ACK in packet %"PRIu64, packet_out->po_packno);
853    }
854
855    if (ctl->sc_ci->cci_lost)
856        ctl->sc_ci->cci_lost(CGP(ctl), packet_out, packet_sz);
857
858    /* This is a client-only check, server check happens in mini conn */
859    if (send_ctl_ecn_on(ctl)
860            && 0 == ctl->sc_ecn_total_acked[PNS_INIT]
861                && HETY_INITIAL == packet_out->po_header_type
862                    && 3 == packet_out->po_packno)
863    {
864        LSQ_DEBUG("possible ECN black hole during handshake, disable ECN");
865        ctl->sc_ecn = ECN_NOT_ECT;
866    }
867
868    if (packet_out->po_frame_types & ctl->sc_retx_frames)
869    {
870        LSQ_DEBUG("lost retransmittable packet %"PRIu64,
871                                                    packet_out->po_packno);
872        send_ctl_record_loss(ctl, packet_out);
873        send_ctl_unacked_remove(ctl, packet_out, packet_sz);
874        TAILQ_INSERT_TAIL(&ctl->sc_lost_packets, packet_out, po_next);
875        packet_out->po_flags |= PO_LOST;
876        return 1;
877    }
878    else
879    {
880        LSQ_DEBUG("lost unretransmittable packet %"PRIu64,
881                                                    packet_out->po_packno);
882        send_ctl_unacked_remove(ctl, packet_out, packet_sz);
883        send_ctl_destroy_chain(ctl, packet_out, next);
884        send_ctl_destroy_packet(ctl, packet_out);
885        return 0;
886    }
887}
888
889
890static lsquic_packno_t
891largest_retx_packet_number (const struct lsquic_send_ctl *ctl,
892                                                    enum packnum_space pns)
893{
894    const lsquic_packet_out_t *packet_out;
895    TAILQ_FOREACH_REVERSE(packet_out, &ctl->sc_unacked_packets[pns],
896                                                lsquic_packets_tailq, po_next)
897    {
898        if (0 == (packet_out->po_flags & (PO_LOSS_REC|PO_POISON))
899                && (packet_out->po_frame_types & ctl->sc_retx_frames))
900            return packet_out->po_packno;
901    }
902    return 0;
903}
904
905
906static void
907send_ctl_detect_losses (struct lsquic_send_ctl *ctl, enum packnum_space pns,
908                                                            lsquic_time_t time)
909{
910    lsquic_packet_out_t *packet_out, *next;
911    lsquic_packno_t largest_retx_packno, largest_lost_packno;
912
913    largest_retx_packno = largest_retx_packet_number(ctl, pns);
914    largest_lost_packno = 0;
915    ctl->sc_loss_to = 0;
916
917    for (packet_out = TAILQ_FIRST(&ctl->sc_unacked_packets[pns]);
918            packet_out && packet_out->po_packno <= ctl->sc_largest_acked_packno;
919                packet_out = next)
920    {
921        next = TAILQ_NEXT(packet_out, po_next);
922
923        if (packet_out->po_flags & (PO_LOSS_REC|PO_POISON))
924            continue;
925
926        if (packet_out->po_packno + N_NACKS_BEFORE_RETX <
927                                                ctl->sc_largest_acked_packno)
928        {
929            LSQ_DEBUG("loss by FACK detected, packet %"PRIu64,
930                                                    packet_out->po_packno);
931            largest_lost_packno = packet_out->po_packno;
932            (void) send_ctl_handle_lost_packet(ctl, packet_out, &next);
933            continue;
934        }
935
936        if (largest_retx_packno
937            && (packet_out->po_frame_types & ctl->sc_retx_frames)
938            && largest_retx_packno <= ctl->sc_largest_acked_packno)
939        {
940            LSQ_DEBUG("loss by early retransmit detected, packet %"PRIu64,
941                                                    packet_out->po_packno);
942            largest_lost_packno = packet_out->po_packno;
943            ctl->sc_loss_to =
944                lsquic_rtt_stats_get_srtt(&ctl->sc_conn_pub->rtt_stats) / 4;
945            LSQ_DEBUG("set sc_loss_to to %"PRIu64", packet %"PRIu64,
946                                    ctl->sc_loss_to, packet_out->po_packno);
947            (void) send_ctl_handle_lost_packet(ctl, packet_out, &next);
948            continue;
949        }
950
951        if (ctl->sc_largest_acked_sent_time > packet_out->po_sent +
952                    lsquic_rtt_stats_get_srtt(&ctl->sc_conn_pub->rtt_stats))
953        {
954            LSQ_DEBUG("loss by sent time detected: packet %"PRIu64,
955                                                    packet_out->po_packno);
956            if (packet_out->po_frame_types & ctl->sc_retx_frames)
957                largest_lost_packno = packet_out->po_packno;
958            else { /* don't count it as a loss */; }
959            (void) send_ctl_handle_lost_packet(ctl, packet_out, &next);
960            continue;
961        }
962    }
963
964    if (largest_lost_packno > ctl->sc_largest_sent_at_cutback)
965    {
966        LSQ_DEBUG("detected new loss: packet %"PRIu64"; new lsac: "
967            "%"PRIu64, largest_lost_packno, ctl->sc_largest_sent_at_cutback);
968        ctl->sc_ci->cci_loss(CGP(ctl));
969        if (ctl->sc_flags & SC_PACE)
970            pacer_loss_event(&ctl->sc_pacer);
971        ctl->sc_largest_sent_at_cutback =
972                                lsquic_senhist_largest(&ctl->sc_senhist);
973    }
974    else if (largest_lost_packno)
975        /* Lost packets whose numbers are smaller than the largest packet
976         * number sent at the time of the last loss event indicate the same
977         * loss event.  This follows NewReno logic, see RFC 6582.
978         */
979        LSQ_DEBUG("ignore loss of packet %"PRIu64" smaller than lsac "
980            "%"PRIu64, largest_lost_packno, ctl->sc_largest_sent_at_cutback);
981}
982
983
984int
985lsquic_send_ctl_got_ack (lsquic_send_ctl_t *ctl,
986                         const struct ack_info *acki,
987                         lsquic_time_t ack_recv_time, lsquic_time_t now)
988{
989    const struct lsquic_packno_range *range =
990                                    &acki->ranges[ acki->n_ranges - 1 ];
991    lsquic_packet_out_t *packet_out, *next;
992    lsquic_packno_t smallest_unacked;
993    lsquic_packno_t ack2ed[2];
994    unsigned packet_sz;
995    int app_limited;
996    signed char do_rtt, skip_checks;
997    enum packnum_space pns;
998    unsigned ecn_total_acked, ecn_ce_cnt, one_rtt_cnt;
999
1000    pns = acki->pns;
1001    packet_out = TAILQ_FIRST(&ctl->sc_unacked_packets[pns]);
1002#if __GNUC__
1003    __builtin_prefetch(packet_out);
1004#endif
1005
1006#if __GNUC__
1007#   define UNLIKELY(cond) __builtin_expect(cond, 0)
1008#else
1009#   define UNLIKELY(cond) cond
1010#endif
1011
1012#if __GNUC__
1013    if (UNLIKELY(LSQ_LOG_ENABLED(LSQ_LOG_DEBUG)))
1014#endif
1015        LSQ_DEBUG("Got ACK frame, largest acked: %"PRIu64"; delta: %"PRIu64,
1016                            largest_acked(acki), acki->lack_delta);
1017
1018    /* Validate ACK first: */
1019    if (UNLIKELY(largest_acked(acki)
1020                                > lsquic_senhist_largest(&ctl->sc_senhist)))
1021    {
1022        LSQ_INFO("at least one packet in ACK range [%"PRIu64" - %"PRIu64"] "
1023            "was never sent", acki->ranges[0].low, acki->ranges[0].high);
1024        return -1;
1025    }
1026
1027    if (ctl->sc_ci->cci_begin_ack)
1028        ctl->sc_ci->cci_begin_ack(CGP(ctl), ack_recv_time,
1029                                                    ctl->sc_bytes_unacked_all);
1030
1031    ecn_total_acked = 0;
1032    ecn_ce_cnt = 0;
1033    one_rtt_cnt = 0;
1034
1035    if (UNLIKELY(ctl->sc_flags & SC_WAS_QUIET))
1036    {
1037        ctl->sc_flags &= ~SC_WAS_QUIET;
1038        LSQ_DEBUG("ACK comes after a period of quiescence");
1039        ctl->sc_ci->cci_was_quiet(CGP(ctl), now, ctl->sc_bytes_unacked_all);
1040    }
1041
1042    if (UNLIKELY(!packet_out))
1043        goto no_unacked_packets;
1044
1045    smallest_unacked = packet_out->po_packno;
1046    LSQ_DEBUG("Smallest unacked: %"PRIu64, smallest_unacked);
1047
1048    ack2ed[1] = 0;
1049
1050    if (packet_out->po_packno > largest_acked(acki))
1051        goto detect_losses;
1052
1053    if (largest_acked(acki) > ctl->sc_cur_rt_end)
1054    {
1055        ++ctl->sc_rt_count;
1056        ctl->sc_cur_rt_end = lsquic_senhist_largest(&ctl->sc_senhist);
1057    }
1058
1059    do_rtt = 0, skip_checks = 0;
1060    app_limited = -1;
1061    do
1062    {
1063        next = TAILQ_NEXT(packet_out, po_next);
1064#if __GNUC__
1065        __builtin_prefetch(next);
1066#endif
1067        if (skip_checks)
1068            goto after_checks;
1069        /* This is faster than binary search in the normal case when the number
1070         * of ranges is not much larger than the number of unacked packets.
1071         */
1072        while (UNLIKELY(range->high < packet_out->po_packno))
1073            --range;
1074        if (range->low <= packet_out->po_packno)
1075        {
1076            skip_checks = range == acki->ranges;
1077            if (app_limited < 0)
1078                app_limited = send_ctl_retx_bytes_out(ctl) + 3 * SC_PACK_SIZE(ctl) /* This
1079                    is the "maximum burst" parameter */
1080                    < ctl->sc_ci->cci_get_cwnd(CGP(ctl));
1081  after_checks:
1082            ctl->sc_largest_acked_packno    = packet_out->po_packno;
1083            ctl->sc_largest_acked_sent_time = packet_out->po_sent;
1084            ecn_total_acked += lsquic_packet_out_ecn(packet_out) != ECN_NOT_ECT;
1085            ecn_ce_cnt += lsquic_packet_out_ecn(packet_out) == ECN_CE;
1086            one_rtt_cnt += lsquic_packet_out_enc_level(packet_out) == ENC_LEV_FORW;
1087            if (0 == (packet_out->po_flags & (PO_LOSS_REC|PO_POISON)))
1088            {
1089                packet_sz = packet_out_sent_sz(packet_out);
1090                send_ctl_unacked_remove(ctl, packet_out, packet_sz);
1091                lsquic_packet_out_ack_streams(packet_out);
1092                LSQ_DEBUG("acking via regular record %"PRIu64,
1093                                                        packet_out->po_packno);
1094            }
1095            else if (packet_out->po_flags & PO_LOSS_REC)
1096            {
1097                packet_sz = packet_out->po_sent_sz;
1098                TAILQ_REMOVE(&ctl->sc_unacked_packets[pns], packet_out,
1099                                                                    po_next);
1100                LSQ_DEBUG("acking via loss record %"PRIu64,
1101                                                        packet_out->po_packno);
1102#if LSQUIC_CONN_STATS
1103                ++ctl->sc_conn_pub->conn_stats->out.acked_via_loss;
1104                LSQ_DEBUG("acking via loss record %"PRIu64,
1105                                                        packet_out->po_packno);
1106#endif
1107            }
1108            else
1109            {
1110                LSQ_WARN("poisoned packet %"PRIu64" acked",
1111                                                        packet_out->po_packno);
1112                return -1;
1113            }
1114            ack2ed[!!(packet_out->po_frame_types & (1 << QUIC_FRAME_ACK))]
1115                = packet_out->po_ack2ed;
1116            do_rtt |= packet_out->po_packno == largest_acked(acki);
1117            ctl->sc_ci->cci_ack(CGP(ctl), packet_out, packet_sz, now,
1118                                                             app_limited);
1119            send_ctl_destroy_chain(ctl, packet_out, &next);
1120            send_ctl_destroy_packet(ctl, packet_out);
1121        }
1122        packet_out = next;
1123    }
1124    while (packet_out && packet_out->po_packno <= largest_acked(acki));
1125
1126    if (do_rtt)
1127    {
1128        take_rtt_sample(ctl, ack_recv_time, acki->lack_delta);
1129        ctl->sc_n_consec_rtos = 0;
1130        ctl->sc_n_hsk = 0;
1131        ctl->sc_n_tlp = 0;
1132    }
1133
1134  detect_losses:
1135    send_ctl_detect_losses(ctl, pns, ack_recv_time);
1136    if (send_ctl_first_unacked_retx_packet(ctl, pns))
1137        set_retx_alarm(ctl, pns, now);
1138    else
1139    {
1140        LSQ_DEBUG("No retransmittable packets: clear alarm");
1141        lsquic_alarmset_unset(ctl->sc_alset, AL_RETX_INIT + pns);
1142    }
1143    lsquic_send_ctl_sanity_check(ctl);
1144
1145    if ((ctl->sc_flags & SC_NSTP) && ack2ed[1] > ctl->sc_largest_ack2ed[pns])
1146        ctl->sc_largest_ack2ed[pns] = ack2ed[1];
1147
1148    if (ctl->sc_n_in_flight_retx == 0)
1149        ctl->sc_flags |= SC_WAS_QUIET;
1150
1151    if (one_rtt_cnt)
1152        ctl->sc_flags |= SC_1RTT_ACKED;
1153
1154    if (send_ctl_ecn_on(ctl))
1155    {
1156        const uint64_t sum = acki->ecn_counts[ECN_ECT0]
1157                           + acki->ecn_counts[ECN_ECT1]
1158                           + acki->ecn_counts[ECN_CE];
1159        ctl->sc_ecn_total_acked[pns] += ecn_total_acked;
1160        ctl->sc_ecn_ce_cnt[pns] += ecn_ce_cnt;
1161        if (sum >= ctl->sc_ecn_total_acked[pns])
1162        {
1163            if (sum > ctl->sc_ecn_total_acked[pns])
1164                ctl->sc_ecn_total_acked[pns] = sum;
1165            if (acki->ecn_counts[ECN_CE] > ctl->sc_ecn_ce_cnt[pns])
1166            {
1167                ctl->sc_ecn_ce_cnt[pns] = acki->ecn_counts[ECN_CE];
1168                LSQ_WARN("TODO: handle ECN CE event");  /* XXX TODO */
1169            }
1170        }
1171        else
1172        {
1173            LSQ_INFO("ECN total ACKed (%"PRIu64") is greater than the sum "
1174                "of ECN counters (%"PRIu64"): disable ECN",
1175                ctl->sc_ecn_total_acked[pns], sum);
1176            ctl->sc_ecn = ECN_NOT_ECT;
1177        }
1178    }
1179
1180  update_n_stop_waiting:
1181    if (!(ctl->sc_flags & (SC_NSTP|SC_IETF)))
1182    {
1183        if (smallest_unacked > smallest_acked(acki))
1184            /* Peer is acking packets that have been acked already.  Schedule
1185             * ACK and STOP_WAITING frame to chop the range if we get two of
1186             * these in a row.
1187             */
1188            ++ctl->sc_n_stop_waiting;
1189        else
1190            ctl->sc_n_stop_waiting = 0;
1191    }
1192    lsquic_send_ctl_sanity_check(ctl);
1193    if (ctl->sc_ci->cci_end_ack)
1194        ctl->sc_ci->cci_end_ack(CGP(ctl), ctl->sc_bytes_unacked_all);
1195    if (ctl->sc_gap < smallest_acked(acki))
1196        send_ctl_reschedule_poison(ctl);
1197    return 0;
1198
1199  no_unacked_packets:
1200    smallest_unacked = lsquic_senhist_largest(&ctl->sc_senhist) + 1;
1201    ctl->sc_flags |= SC_WAS_QUIET;
1202    goto update_n_stop_waiting;
1203}
1204
1205
1206lsquic_packno_t
1207lsquic_send_ctl_smallest_unacked (lsquic_send_ctl_t *ctl)
1208{
1209    const lsquic_packet_out_t *packet_out;
1210    enum packnum_space pns;
1211
1212    /* Packets are always sent out in order (unless we are reordering them
1213     * on purpose).  Thus, the first packet on the unacked packets list has
1214     * the smallest packet number of all packets on that list.
1215     */
1216    for (pns = ctl->sc_flags & SC_IETF ? PNS_INIT : PNS_APP; pns < N_PNS; ++pns)
1217        if ((packet_out = TAILQ_FIRST(&ctl->sc_unacked_packets[pns])))
1218            /* We're OK with using a loss record */
1219            return packet_out->po_packno;
1220
1221    return lsquic_senhist_largest(&ctl->sc_senhist) + first_packno(ctl);
1222}
1223
1224
1225static struct lsquic_packet_out *
1226send_ctl_next_lost (lsquic_send_ctl_t *ctl)
1227{
1228    struct lsquic_packet_out *lost_packet;
1229
1230  get_next_lost:
1231    lost_packet = TAILQ_FIRST(&ctl->sc_lost_packets);
1232    if (lost_packet)
1233    {
1234        if (lost_packet->po_frame_types & (1 << QUIC_FRAME_STREAM))
1235        {
1236            if (0 == (lost_packet->po_flags & PO_MINI))
1237            {
1238                lsquic_packet_out_elide_reset_stream_frames(lost_packet, 0);
1239                if (lost_packet->po_regen_sz >= lost_packet->po_data_sz)
1240                {
1241                    LSQ_DEBUG("Dropping packet %"PRIu64" from lost queue",
1242                        lost_packet->po_packno);
1243                    TAILQ_REMOVE(&ctl->sc_lost_packets, lost_packet, po_next);
1244                    lost_packet->po_flags &= ~PO_LOST;
1245                    send_ctl_destroy_chain(ctl, lost_packet, NULL);
1246                    send_ctl_destroy_packet(ctl, lost_packet);
1247                    goto get_next_lost;
1248                }
1249            }
1250            else
1251            {
1252                /* Mini connection only ever sends data on stream 1.  There
1253                 * is nothing to elide: always resend it.
1254                 */
1255                ;
1256            }
1257        }
1258
1259        if (!lsquic_send_ctl_can_send(ctl))
1260            return NULL;
1261
1262        TAILQ_REMOVE(&ctl->sc_lost_packets, lost_packet, po_next);
1263        lost_packet->po_flags &= ~PO_LOST;
1264        lost_packet->po_flags |= PO_RETX;
1265    }
1266
1267    return lost_packet;
1268}
1269
1270
1271static lsquic_packno_t
1272send_ctl_next_packno (lsquic_send_ctl_t *ctl)
1273{
1274    lsquic_packno_t packno;
1275
1276    packno = ++ctl->sc_cur_packno;
1277    if (packno == ctl->sc_gap)
1278        packno = ++ctl->sc_cur_packno;
1279
1280    return packno;
1281}
1282
1283
1284void
1285lsquic_send_ctl_cleanup (lsquic_send_ctl_t *ctl)
1286{
1287    lsquic_packet_out_t *packet_out, *next;
1288    enum packnum_space pns;
1289    unsigned n;
1290
1291    lsquic_senhist_cleanup(&ctl->sc_senhist);
1292    while ((packet_out = TAILQ_FIRST(&ctl->sc_scheduled_packets)))
1293    {
1294        send_ctl_sched_remove(ctl, packet_out);
1295        send_ctl_destroy_packet(ctl, packet_out);
1296    }
1297    assert(0 == ctl->sc_n_scheduled);
1298    assert(0 == ctl->sc_bytes_scheduled);
1299    for (pns = PNS_INIT; pns < N_PNS; ++pns)
1300        while ((packet_out = TAILQ_FIRST(&ctl->sc_unacked_packets[pns])))
1301        {
1302            TAILQ_REMOVE(&ctl->sc_unacked_packets[pns], packet_out, po_next);
1303            packet_out->po_flags &= ~PO_UNACKED;
1304#ifndef NDEBUG
1305            if (0 == (packet_out->po_flags & (PO_LOSS_REC|PO_POISON)))
1306            {
1307                ctl->sc_bytes_unacked_all -= packet_out_sent_sz(packet_out);
1308                --ctl->sc_n_in_flight_all;
1309            }
1310#endif
1311            send_ctl_destroy_packet(ctl, packet_out);
1312        }
1313    assert(0 == ctl->sc_n_in_flight_all);
1314    assert(0 == ctl->sc_bytes_unacked_all);
1315    while ((packet_out = TAILQ_FIRST(&ctl->sc_lost_packets)))
1316    {
1317        TAILQ_REMOVE(&ctl->sc_lost_packets, packet_out, po_next);
1318        packet_out->po_flags &= ~PO_LOST;
1319        send_ctl_destroy_packet(ctl, packet_out);
1320    }
1321    for (n = 0; n < sizeof(ctl->sc_buffered_packets) /
1322                                sizeof(ctl->sc_buffered_packets[0]); ++n)
1323    {
1324        for (packet_out = TAILQ_FIRST(&ctl->sc_buffered_packets[n].bpq_packets);
1325                                                packet_out; packet_out = next)
1326        {
1327            next = TAILQ_NEXT(packet_out, po_next);
1328            send_ctl_destroy_packet(ctl, packet_out);
1329        }
1330    }
1331    if (ctl->sc_flags & SC_PACE)
1332        pacer_cleanup(&ctl->sc_pacer);
1333    ctl->sc_ci->cci_cleanup(CGP(ctl));
1334#if LSQUIC_SEND_STATS
1335    LSQ_NOTICE("stats: n_total_sent: %u; n_resent: %u; n_delayed: %u",
1336        ctl->sc_stats.n_total_sent, ctl->sc_stats.n_resent,
1337        ctl->sc_stats.n_delayed);
1338#endif
1339    free(ctl->sc_token);
1340}
1341
1342
1343static unsigned
1344send_ctl_retx_bytes_out (const struct lsquic_send_ctl *ctl)
1345{
1346    return ctl->sc_bytes_scheduled
1347         + ctl->sc_bytes_unacked_retx
1348         ;
1349}
1350
1351
1352static unsigned
1353send_ctl_all_bytes_out (const struct lsquic_send_ctl *ctl)
1354{
1355    return ctl->sc_bytes_scheduled
1356         + ctl->sc_bytes_unacked_all
1357         ;
1358}
1359
1360
1361int
1362lsquic_send_ctl_pacer_blocked (struct lsquic_send_ctl *ctl)
1363{
1364    return (ctl->sc_flags & SC_PACE)
1365        && !pacer_can_schedule(&ctl->sc_pacer,
1366                               ctl->sc_n_scheduled + ctl->sc_n_in_flight_all);
1367}
1368
1369
1370#ifndef NDEBUG
1371#if __GNUC__
1372__attribute__((weak))
1373#endif
1374#endif
1375int
1376lsquic_send_ctl_can_send (lsquic_send_ctl_t *ctl)
1377{
1378    const unsigned n_out = send_ctl_all_bytes_out(ctl);
1379    LSQ_DEBUG("%s: n_out: %u (unacked_all: %u); cwnd: %"PRIu64, __func__,
1380        n_out, ctl->sc_bytes_unacked_all,
1381        ctl->sc_ci->cci_get_cwnd(CGP(ctl)));
1382    if (ctl->sc_flags & SC_PACE)
1383    {
1384        if (n_out >= ctl->sc_ci->cci_get_cwnd(CGP(ctl)))
1385            return 0;
1386        if (pacer_can_schedule(&ctl->sc_pacer,
1387                               ctl->sc_n_scheduled + ctl->sc_n_in_flight_all))
1388            return 1;
1389        if (ctl->sc_flags & SC_SCHED_TICK)
1390        {
1391            ctl->sc_flags &= ~SC_SCHED_TICK;
1392            lsquic_engine_add_conn_to_attq(ctl->sc_enpub,
1393                    ctl->sc_conn_pub->lconn, pacer_next_sched(&ctl->sc_pacer),
1394                    AEW_PACER);
1395        }
1396        return 0;
1397    }
1398    else
1399        return n_out < ctl->sc_ci->cci_get_cwnd(CGP(ctl));
1400}
1401
1402
1403/* Like lsquic_send_ctl_can_send(), but no mods */
1404static int
1405send_ctl_could_send (const struct lsquic_send_ctl *ctl)
1406{
1407    uint64_t cwnd;
1408    unsigned n_out;
1409
1410    if ((ctl->sc_flags & SC_PACE) && pacer_delayed(&ctl->sc_pacer))
1411        return 0;
1412
1413    cwnd = ctl->sc_ci->cci_get_cwnd(CGP(ctl));
1414    n_out = send_ctl_all_bytes_out(ctl);
1415    return n_out < cwnd;
1416}
1417
1418
1419void
1420lsquic_send_ctl_maybe_app_limited (struct lsquic_send_ctl *ctl,
1421                                            const struct network_path *path)
1422{
1423    const struct lsquic_packet_out *packet_out;
1424
1425    packet_out = lsquic_send_ctl_last_scheduled(ctl, PNS_APP, path, 0);
1426    if ((packet_out && lsquic_packet_out_avail(packet_out) > 10)
1427                                                || send_ctl_could_send(ctl))
1428    {
1429        LSQ_DEBUG("app-limited");
1430        ctl->sc_flags |= SC_APP_LIMITED;
1431    }
1432}
1433
1434
1435static void
1436send_ctl_expire (struct lsquic_send_ctl *ctl, enum packnum_space pns,
1437                                                    enum expire_filter filter)
1438{
1439    lsquic_packet_out_t *packet_out, *next;
1440    int n_resubmitted;
1441    static const char *const filter_type2str[] = {
1442        [EXFI_ALL] = "all",
1443        [EXFI_HSK] = "handshake",
1444        [EXFI_LAST] = "last",
1445    };
1446
1447    switch (filter)
1448    {
1449    case EXFI_ALL:
1450        n_resubmitted = 0;
1451        for (packet_out = TAILQ_FIRST(&ctl->sc_unacked_packets[pns]);
1452                                                packet_out; packet_out = next)
1453        {
1454            next = TAILQ_NEXT(packet_out, po_next);
1455            if (0 == (packet_out->po_flags & (PO_LOSS_REC|PO_POISON)))
1456                n_resubmitted += send_ctl_handle_lost_packet(ctl, packet_out,
1457                                                                        &next);
1458        }
1459        break;
1460    case EXFI_HSK:
1461        n_resubmitted = 0;
1462        for (packet_out = TAILQ_FIRST(&ctl->sc_unacked_packets[pns]); packet_out;
1463                                                            packet_out = next)
1464        {
1465            next = TAILQ_NEXT(packet_out, po_next);
1466            if (packet_out->po_flags & PO_HELLO)
1467                n_resubmitted += send_ctl_handle_lost_packet(ctl, packet_out,
1468                                                                        &next);
1469        }
1470        break;
1471    default:
1472        assert(filter == EXFI_LAST);
1473        packet_out = send_ctl_last_unacked_retx_packet(ctl, pns);
1474        if (packet_out)
1475            n_resubmitted = send_ctl_handle_lost_packet(ctl, packet_out, NULL);
1476        else
1477            n_resubmitted = 0;
1478        break;
1479    }
1480
1481    LSQ_DEBUG("consider %s packets lost: %d resubmitted",
1482                                    filter_type2str[filter], n_resubmitted);
1483}
1484
1485
1486void
1487lsquic_send_ctl_expire_all (lsquic_send_ctl_t *ctl)
1488{
1489    enum packnum_space pns;
1490
1491    for (pns = ctl->sc_flags & SC_IETF ? PNS_INIT : PNS_APP; pns < N_PNS; ++pns)
1492    {
1493        lsquic_alarmset_unset(ctl->sc_alset, AL_RETX_INIT + pns);
1494        send_ctl_expire(ctl, pns, EXFI_ALL);
1495    }
1496    lsquic_send_ctl_sanity_check(ctl);
1497}
1498
1499
1500#ifndef NDEBUG
1501void
1502lsquic_send_ctl_do_sanity_check (const struct lsquic_send_ctl *ctl)
1503{
1504    const struct lsquic_packet_out *packet_out;
1505    lsquic_packno_t prev_packno;
1506    int prev_packno_set;
1507    unsigned count, bytes;
1508    enum packnum_space pns;
1509
1510    count = 0, bytes = 0;
1511    for (pns = PNS_INIT; pns <= PNS_APP; ++pns)
1512    {
1513        prev_packno_set = 0;
1514        TAILQ_FOREACH(packet_out, &ctl->sc_unacked_packets[pns], po_next)
1515        {
1516            if (prev_packno_set)
1517                assert(packet_out->po_packno > prev_packno);
1518            else
1519            {
1520                prev_packno = packet_out->po_packno;
1521                prev_packno_set = 1;
1522            }
1523            if (0 == (packet_out->po_flags & (PO_LOSS_REC|PO_POISON)))
1524            {
1525                bytes += packet_out_sent_sz(packet_out);
1526                ++count;
1527            }
1528        }
1529    }
1530    assert(count == ctl->sc_n_in_flight_all);
1531    assert(bytes == ctl->sc_bytes_unacked_all);
1532
1533    count = 0, bytes = 0;
1534    TAILQ_FOREACH(packet_out, &ctl->sc_scheduled_packets, po_next)
1535    {
1536        assert(packet_out->po_flags & PO_SCHED);
1537        bytes += packet_out_total_sz(packet_out);
1538        ++count;
1539    }
1540    assert(count == ctl->sc_n_scheduled);
1541    assert(bytes == ctl->sc_bytes_scheduled);
1542}
1543#endif
1544
1545
1546void
1547lsquic_send_ctl_scheduled_one (lsquic_send_ctl_t *ctl,
1548                                            lsquic_packet_out_t *packet_out)
1549{
1550#ifndef NDEBUG
1551    const lsquic_packet_out_t *last;
1552    last = TAILQ_LAST(&ctl->sc_scheduled_packets, lsquic_packets_tailq);
1553    if (last)
1554        assert((last->po_flags & PO_REPACKNO) ||
1555                last->po_packno < packet_out->po_packno);
1556#endif
1557    if (ctl->sc_flags & SC_PACE)
1558    {
1559        unsigned n_out = ctl->sc_n_in_flight_retx + ctl->sc_n_scheduled;
1560        pacer_packet_scheduled(&ctl->sc_pacer, n_out,
1561            send_ctl_in_recovery(ctl), send_ctl_transfer_time, ctl);
1562    }
1563    send_ctl_sched_append(ctl, packet_out);
1564}
1565
1566
1567/* Wrapper is used to reset the counter when it's been too long */
1568static unsigned
1569send_ctl_get_n_consec_rtos (struct lsquic_send_ctl *ctl)
1570{
1571    lsquic_time_t timeout;
1572
1573    if (ctl->sc_n_consec_rtos)
1574    {
1575        timeout = calculate_packet_rto(ctl);
1576        if (ctl->sc_last_rto_time + timeout < ctl->sc_last_sent_time)
1577        {
1578            ctl->sc_n_consec_rtos = 0;
1579            LSQ_DEBUG("reset RTO counter after %"PRIu64" usec",
1580                ctl->sc_last_sent_time - ctl->sc_last_rto_time);
1581        }
1582    }
1583
1584    return ctl->sc_n_consec_rtos;
1585}
1586
1587
1588/* This mimics the logic in lsquic_send_ctl_next_packet_to_send(): we want
1589 * to check whether the first scheduled packet cannot be sent.
1590 */
1591int
1592lsquic_send_ctl_sched_is_blocked (struct lsquic_send_ctl *ctl)
1593{
1594    const lsquic_packet_out_t *packet_out
1595                            = TAILQ_FIRST(&ctl->sc_scheduled_packets);
1596    return send_ctl_get_n_consec_rtos(ctl)
1597        && 0 == ctl->sc_next_limit
1598        && packet_out
1599        && !(packet_out->po_frame_types & (1 << QUIC_FRAME_ACK));
1600}
1601
1602
1603static void
1604send_ctl_maybe_zero_pad (struct lsquic_send_ctl *ctl,
1605                        struct lsquic_packet_out *initial_packet, size_t limit)
1606{
1607    struct lsquic_packet_out *packet_out;
1608    size_t cum_size, size;
1609
1610    cum_size = packet_out_total_sz(initial_packet);
1611    if (cum_size >= limit)
1612    {
1613        LSQ_DEBUG("packet size %zu larger than %zu-byte limit: not "
1614            "zero-padding", cum_size, limit);
1615        return;
1616    }
1617
1618    TAILQ_FOREACH(packet_out, &ctl->sc_scheduled_packets, po_next)
1619    {
1620        size = packet_out_total_sz(packet_out);
1621        if (cum_size + size > limit)
1622            break;
1623        cum_size += size;
1624    }
1625
1626    LSQ_DEBUG("cum_size: %zu; limit: %zu", cum_size, limit);
1627    assert(cum_size < limit);
1628    size = limit - cum_size;
1629    if (size > lsquic_packet_out_avail(initial_packet))
1630        size = lsquic_packet_out_avail(initial_packet);
1631    memset(initial_packet->po_data + initial_packet->po_data_sz, 0, size);
1632    initial_packet->po_data_sz += size;
1633    initial_packet->po_frame_types |= QUIC_FTBIT_PADDING;
1634    LSQ_DEBUG("Added %zu bytes of PADDING to packet %"PRIu64, size,
1635                                                initial_packet->po_packno);
1636}
1637
1638
1639lsquic_packet_out_t *
1640lsquic_send_ctl_next_packet_to_send (struct lsquic_send_ctl *ctl, size_t size)
1641{
1642    lsquic_packet_out_t *packet_out;
1643    int dec_limit;
1644
1645  get_packet:
1646    packet_out = TAILQ_FIRST(&ctl->sc_scheduled_packets);
1647    if (!packet_out)
1648        return NULL;
1649
1650    if (!(packet_out->po_frame_types & (1 << QUIC_FRAME_ACK))
1651                                        && send_ctl_get_n_consec_rtos(ctl))
1652    {
1653        if (ctl->sc_next_limit)
1654            dec_limit = 1;
1655        else
1656            return NULL;
1657    }
1658    else
1659        dec_limit = 0;
1660
1661    if (packet_out->po_flags & PO_REPACKNO)
1662    {
1663        if (packet_out->po_regen_sz < packet_out->po_data_sz)
1664        {
1665            update_for_resending(ctl, packet_out);
1666            packet_out->po_flags &= ~PO_REPACKNO;
1667        }
1668        else
1669        {
1670            LSQ_DEBUG("Dropping packet %"PRIu64" from scheduled queue",
1671                packet_out->po_packno);
1672            send_ctl_sched_remove(ctl, packet_out);
1673            send_ctl_destroy_chain(ctl, packet_out, NULL);
1674            send_ctl_destroy_packet(ctl, packet_out);
1675            goto get_packet;
1676        }
1677    }
1678
1679    if (UNLIKELY(size))
1680    {
1681        if (packet_out_total_sz(packet_out) + size > SC_PACK_SIZE(ctl))
1682            return NULL;
1683        LSQ_DEBUG("packet %"PRIu64" (%zu bytes) will be tacked on to "
1684            "previous packet(s) (%zu bytes) (coalescing)",
1685            packet_out->po_packno, packet_out_total_sz(packet_out), size);
1686    }
1687    send_ctl_sched_remove(ctl, packet_out);
1688
1689    if (dec_limit)
1690    {
1691        --ctl->sc_next_limit;
1692        packet_out->po_flags |= PO_LIMITED;
1693    }
1694    else
1695        packet_out->po_flags &= ~PO_LIMITED;
1696
1697    if (UNLIKELY(packet_out->po_header_type == HETY_INITIAL)
1698                    && !(ctl->sc_conn_pub->lconn->cn_flags & LSCONN_SERVER)
1699                    && size < 1200)
1700    {
1701        send_ctl_maybe_zero_pad(ctl, packet_out, 1200 - size);
1702    }
1703
1704    if (ctl->sc_flags & SC_QL_BITS)
1705    {
1706        packet_out->po_lflags |= POL_LOG_QL_BITS;
1707        if (ctl->sc_loss_count)
1708        {
1709            --ctl->sc_loss_count;
1710            packet_out->po_lflags |= POL_LOSS_BIT;
1711        }
1712        else
1713            packet_out->po_lflags &= ~POL_LOSS_BIT;
1714        if (packet_out->po_header_type == HETY_NOT_SET)
1715        {
1716            if (ctl->sc_gap + 1 == packet_out->po_packno)
1717                ++ctl->sc_square_count;
1718            if (ctl->sc_square_count++ & 64)
1719                packet_out->po_lflags |= POL_SQUARE_BIT;
1720            else
1721                packet_out->po_lflags &= ~POL_SQUARE_BIT;
1722        }
1723    }
1724
1725    return packet_out;
1726}
1727
1728
1729void
1730lsquic_send_ctl_delayed_one (lsquic_send_ctl_t *ctl,
1731                                            lsquic_packet_out_t *packet_out)
1732{
1733    send_ctl_sched_prepend(ctl, packet_out);
1734    if (packet_out->po_flags & PO_LIMITED)
1735        ++ctl->sc_next_limit;
1736    LSQ_DEBUG("packet %"PRIu64" has been delayed", packet_out->po_packno);
1737#if LSQUIC_SEND_STATS
1738    ++ctl->sc_stats.n_delayed;
1739#endif
1740    if (packet_out->po_lflags & POL_LOSS_BIT)
1741        ++ctl->sc_loss_count;
1742    if ((ctl->sc_flags & SC_QL_BITS)
1743                            && packet_out->po_header_type == HETY_NOT_SET)
1744        ctl->sc_square_count -= 1 + (ctl->sc_gap + 1 == packet_out->po_packno);
1745}
1746
1747
1748int
1749lsquic_send_ctl_have_outgoing_stream_frames (const lsquic_send_ctl_t *ctl)
1750{
1751    const lsquic_packet_out_t *packet_out;
1752    TAILQ_FOREACH(packet_out, &ctl->sc_scheduled_packets, po_next)
1753        if (packet_out->po_frame_types &
1754                    ((1 << QUIC_FRAME_STREAM) | (1 << QUIC_FRAME_RST_STREAM)))
1755            return 1;
1756    return 0;
1757}
1758
1759
1760int
1761lsquic_send_ctl_have_outgoing_retx_frames (const lsquic_send_ctl_t *ctl)
1762{
1763    const lsquic_packet_out_t *packet_out;
1764    TAILQ_FOREACH(packet_out, &ctl->sc_scheduled_packets, po_next)
1765        if (packet_out->po_frame_types & ctl->sc_retx_frames)
1766            return 1;
1767    return 0;
1768}
1769
1770
1771static int
1772send_ctl_set_packet_out_token (const struct lsquic_send_ctl *ctl,
1773                                        struct lsquic_packet_out *packet_out)
1774{
1775    unsigned char *token;
1776
1777    token = malloc(ctl->sc_token_sz);
1778    if (!token)
1779    {
1780        LSQ_WARN("malloc failed: cannot set initial token");
1781        return -1;
1782    }
1783
1784    memcpy(token, ctl->sc_token, ctl->sc_token_sz);
1785    packet_out->po_token = token;
1786    packet_out->po_token_len = ctl->sc_token_sz;
1787    packet_out->po_flags |= PO_NONCE;
1788    LSQ_DEBUG("set initial token on packet");
1789    return 0;
1790}
1791
1792
1793static lsquic_packet_out_t *
1794send_ctl_allocate_packet (struct lsquic_send_ctl *ctl, enum packno_bits bits,
1795                            unsigned need_at_least, enum packnum_space pns,
1796                            const struct network_path *path)
1797{
1798    lsquic_packet_out_t *packet_out;
1799
1800    packet_out = lsquic_packet_out_new(&ctl->sc_enpub->enp_mm,
1801                    ctl->sc_conn_pub->packet_out_malo,
1802                    !(ctl->sc_flags & SC_TCID0), ctl->sc_conn_pub->lconn, bits,
1803                    ctl->sc_ver_neg->vn_tag, NULL, path);
1804    if (!packet_out)
1805        return NULL;
1806
1807    if (need_at_least && lsquic_packet_out_avail(packet_out) < need_at_least)
1808    {   /* This should never happen, this is why this check is performed at
1809         * this level and not lower, before the packet is actually allocated.
1810         */
1811        LSQ_ERROR("wanted to allocate packet with at least %u bytes of "
1812            "payload, but only got %u bytes (mtu: %u bytes)", need_at_least,
1813            lsquic_packet_out_avail(packet_out), SC_PACK_SIZE(ctl));
1814        send_ctl_destroy_packet(ctl, packet_out);
1815        return NULL;
1816    }
1817
1818    if (UNLIKELY(pns != PNS_APP))
1819    {
1820        if (pns == PNS_INIT)
1821        {
1822            packet_out->po_header_type = HETY_INITIAL;
1823            if (ctl->sc_token)
1824                (void) send_ctl_set_packet_out_token(ctl, packet_out);
1825        }
1826        else
1827            packet_out->po_header_type = HETY_HANDSHAKE;
1828    }
1829
1830    lsquic_packet_out_set_pns(packet_out, pns);
1831    packet_out->po_lflags |= ctl->sc_ecn << POECN_SHIFT;
1832    packet_out->po_loss_chain = packet_out;
1833    return packet_out;
1834}
1835
1836
1837lsquic_packet_out_t *
1838lsquic_send_ctl_new_packet_out (lsquic_send_ctl_t *ctl, unsigned need_at_least,
1839                        enum packnum_space pns, const struct network_path *path)
1840{
1841    lsquic_packet_out_t *packet_out;
1842    enum packno_bits bits;
1843
1844    bits = lsquic_send_ctl_packno_bits(ctl);
1845    packet_out = send_ctl_allocate_packet(ctl, bits, need_at_least, pns, path);
1846    if (!packet_out)
1847        return NULL;
1848
1849    packet_out->po_packno = send_ctl_next_packno(ctl);
1850    LSQ_DEBUG("created packet %"PRIu64, packet_out->po_packno);
1851    EV_LOG_PACKET_CREATED(LSQUIC_LOG_CONN_ID, packet_out);
1852    return packet_out;
1853}
1854
1855
1856struct lsquic_packet_out *
1857lsquic_send_ctl_last_scheduled (struct lsquic_send_ctl *ctl,
1858                    enum packnum_space pns, const struct network_path *path,
1859                    int regen_match)
1860{
1861    struct lsquic_packet_out *packet_out;
1862
1863    if (0 == regen_match)
1864    {
1865        TAILQ_FOREACH_REVERSE(packet_out, &ctl->sc_scheduled_packets,
1866                                                lsquic_packets_tailq, po_next)
1867            if (pns == lsquic_packet_out_pns(packet_out)
1868                                                && path == packet_out->po_path)
1869                return packet_out;
1870    }
1871    else
1872    {
1873        TAILQ_FOREACH_REVERSE(packet_out, &ctl->sc_scheduled_packets,
1874                                                lsquic_packets_tailq, po_next)
1875            if (pns == lsquic_packet_out_pns(packet_out)
1876                    && packet_out->po_regen_sz == packet_out->po_data_sz
1877                                                && path == packet_out->po_path)
1878                return packet_out;
1879    }
1880
1881    return NULL;
1882}
1883
1884
1885/* Do not use for STREAM frames
1886 */
1887lsquic_packet_out_t *
1888lsquic_send_ctl_get_writeable_packet (lsquic_send_ctl_t *ctl,
1889                enum packnum_space pns, unsigned need_at_least,
1890                const struct network_path *path, int regen_match, int *is_err)
1891{
1892    lsquic_packet_out_t *packet_out;
1893
1894    assert(need_at_least > 0);
1895
1896    packet_out = lsquic_send_ctl_last_scheduled(ctl, pns, path, regen_match);
1897    if (packet_out
1898        && !(packet_out->po_flags & (PO_MINI|PO_STREAM_END|PO_RETX))
1899        && lsquic_packet_out_avail(packet_out) >= need_at_least)
1900    {
1901        return packet_out;
1902    }
1903
1904    if (!lsquic_send_ctl_can_send(ctl))
1905    {
1906        if (is_err)
1907            *is_err = 0;
1908        return NULL;
1909    }
1910
1911    packet_out = lsquic_send_ctl_new_packet_out(ctl, need_at_least, pns, path);
1912    if (packet_out)
1913    {
1914        lsquic_packet_out_set_pns(packet_out, pns);
1915        lsquic_send_ctl_scheduled_one(ctl, packet_out);
1916    }
1917    else if (is_err)
1918        *is_err = 1;
1919    return packet_out;
1920}
1921
1922
1923struct lsquic_packet_out *
1924lsquic_send_ctl_get_packet_for_crypto (struct lsquic_send_ctl *ctl,
1925                          unsigned need_at_least, enum packnum_space pns,
1926                          const struct network_path *path)
1927{
1928    struct lsquic_packet_out *packet_out;
1929
1930    assert(lsquic_send_ctl_schedule_stream_packets_immediately(ctl));
1931    assert(need_at_least > 0);
1932
1933    packet_out = lsquic_send_ctl_last_scheduled(ctl, pns, path, 0);
1934    if (packet_out
1935        && !(packet_out->po_flags & (PO_STREAM_END|PO_RETX))
1936        && lsquic_packet_out_avail(packet_out) >= need_at_least)
1937    {
1938        return packet_out;
1939    }
1940
1941    if (!lsquic_send_ctl_can_send(ctl))
1942        return NULL;
1943
1944    packet_out = lsquic_send_ctl_new_packet_out(ctl, need_at_least, pns, path);
1945    if (!packet_out)
1946        return NULL;
1947
1948    lsquic_send_ctl_scheduled_one(ctl, packet_out);
1949    return packet_out;
1950}
1951
1952
1953static void
1954update_for_resending (lsquic_send_ctl_t *ctl, lsquic_packet_out_t *packet_out)
1955{
1956
1957    lsquic_packno_t oldno, packno;
1958
1959    /* When the packet is resent, it uses the same number of bytes to encode
1960     * the packet number as the original packet.  This follows the reference
1961     * implementation.
1962     */
1963    oldno = packet_out->po_packno;
1964    packno = send_ctl_next_packno(ctl);
1965
1966    packet_out->po_flags &= ~PO_SENT_SZ;
1967    packet_out->po_frame_types &= ~GQUIC_FRAME_REGEN_MASK;
1968    assert(packet_out->po_frame_types);
1969    packet_out->po_packno = packno;
1970    lsquic_packet_out_set_ecn(packet_out, ctl->sc_ecn);
1971
1972    if (ctl->sc_ver_neg->vn_tag)
1973    {
1974        assert(packet_out->po_flags & PO_VERSION);  /* It can only disappear */
1975        packet_out->po_ver_tag = *ctl->sc_ver_neg->vn_tag;
1976    }
1977
1978    assert(packet_out->po_regen_sz < packet_out->po_data_sz);
1979    if (packet_out->po_regen_sz)
1980    {
1981        if (packet_out->po_flags & PO_SCHED)
1982            ctl->sc_bytes_scheduled -= packet_out->po_regen_sz;
1983        lsquic_packet_out_chop_regen(packet_out);
1984    }
1985    LSQ_DEBUG("Packet %"PRIu64" repackaged for resending as packet %"PRIu64,
1986                                                            oldno, packno);
1987    EV_LOG_CONN_EVENT(LSQUIC_LOG_CONN_ID, "packet %"PRIu64" repackaged for "
1988        "resending as packet %"PRIu64, oldno, packno);
1989}
1990
1991
1992unsigned
1993lsquic_send_ctl_reschedule_packets (lsquic_send_ctl_t *ctl)
1994{
1995    lsquic_packet_out_t *packet_out;
1996    unsigned n = 0;
1997
1998    while ((packet_out = send_ctl_next_lost(ctl)))
1999    {
2000        assert(packet_out->po_regen_sz < packet_out->po_data_sz);
2001        ++n;
2002#if LSQUIC_CONN_STATS
2003        ++ctl->sc_conn_pub->conn_stats->out.retx_packets;
2004#endif
2005        update_for_resending(ctl, packet_out);
2006        lsquic_send_ctl_scheduled_one(ctl, packet_out);
2007    }
2008
2009    if (n)
2010        LSQ_DEBUG("rescheduled %u packets", n);
2011
2012    return n;
2013}
2014
2015
2016void
2017lsquic_send_ctl_set_tcid0 (lsquic_send_ctl_t *ctl, int tcid0)
2018{
2019    if (tcid0)
2020    {
2021        LSQ_INFO("set TCID flag");
2022        ctl->sc_flags |=  SC_TCID0;
2023    }
2024    else
2025    {
2026        LSQ_INFO("unset TCID flag");
2027        ctl->sc_flags &= ~SC_TCID0;
2028    }
2029}
2030
2031
2032/* The controller elides this STREAM frames of stream `stream_id' from
2033 * scheduled and buffered packets.  If a packet becomes empty as a result,
2034 * it is dropped.
2035 *
2036 * Packets on other queues do not need to be processed: unacked packets
2037 * have already been sent, and lost packets' reset stream frames will be
2038 * elided in due time.
2039 */
2040void
2041lsquic_send_ctl_elide_stream_frames (lsquic_send_ctl_t *ctl,
2042                                                lsquic_stream_id_t stream_id)
2043{
2044    struct lsquic_packet_out *packet_out, *next;
2045    unsigned n, adj;
2046    int dropped;
2047
2048    dropped = 0;
2049#ifdef WIN32
2050    next = NULL;
2051#endif
2052    for (packet_out = TAILQ_FIRST(&ctl->sc_scheduled_packets); packet_out;
2053                                                            packet_out = next)
2054    {
2055        next = TAILQ_NEXT(packet_out, po_next);
2056
2057        if ((packet_out->po_frame_types & (1 << QUIC_FRAME_STREAM))
2058                                    && 0 == (packet_out->po_flags & PO_MINI))
2059        {
2060            adj = lsquic_packet_out_elide_reset_stream_frames(packet_out,
2061                                                              stream_id);
2062            ctl->sc_bytes_scheduled -= adj;
2063            if (0 == packet_out->po_frame_types)
2064            {
2065                LSQ_DEBUG("cancel packet %"PRIu64" after eliding frames for "
2066                    "stream %"PRIu64, packet_out->po_packno, stream_id);
2067                send_ctl_sched_remove(ctl, packet_out);
2068                send_ctl_destroy_chain(ctl, packet_out, NULL);
2069                send_ctl_destroy_packet(ctl, packet_out);
2070                ++dropped;
2071            }
2072        }
2073    }
2074
2075    if (dropped)
2076        lsquic_send_ctl_reset_packnos(ctl);
2077
2078    for (n = 0; n < sizeof(ctl->sc_buffered_packets) /
2079                                sizeof(ctl->sc_buffered_packets[0]); ++n)
2080    {
2081        for (packet_out = TAILQ_FIRST(&ctl->sc_buffered_packets[n].bpq_packets);
2082                                                packet_out; packet_out = next)
2083        {
2084            next = TAILQ_NEXT(packet_out, po_next);
2085            if (packet_out->po_frame_types & (1 << QUIC_FRAME_STREAM))
2086            {
2087                lsquic_packet_out_elide_reset_stream_frames(packet_out, stream_id);
2088                if (0 == packet_out->po_frame_types)
2089                {
2090                    LSQ_DEBUG("cancel buffered packet in queue #%u after eliding "
2091                        "frames for stream %"PRIu64, n, stream_id);
2092                    TAILQ_REMOVE(&ctl->sc_buffered_packets[n].bpq_packets,
2093                                 packet_out, po_next);
2094                    --ctl->sc_buffered_packets[n].bpq_count;
2095                    send_ctl_destroy_packet(ctl, packet_out);
2096                    LSQ_DEBUG("Elide packet from buffered queue #%u; count: %u",
2097                              n, ctl->sc_buffered_packets[n].bpq_count);
2098                }
2099            }
2100        }
2101    }
2102}
2103
2104
2105/* Count how many packets will remain after the squeezing performed by
2106 * lsquic_send_ctl_squeeze_sched().  This is the number of delayed data
2107 * packets.
2108 */
2109#ifndef NDEBUG
2110#if __GNUC__
2111__attribute__((weak))
2112#endif
2113#endif
2114int
2115lsquic_send_ctl_have_delayed_packets (const lsquic_send_ctl_t *ctl)
2116{
2117    const struct lsquic_packet_out *packet_out;
2118    TAILQ_FOREACH(packet_out, &ctl->sc_scheduled_packets, po_next)
2119        if (packet_out->po_regen_sz < packet_out->po_data_sz)
2120            return 1;
2121    return 0;
2122}
2123
2124
2125#ifndef NDEBUG
2126static void
2127send_ctl_log_packet_q (const lsquic_send_ctl_t *ctl, const char *prefix,
2128                                const struct lsquic_packets_tailq *tailq)
2129{
2130    const lsquic_packet_out_t *packet_out;
2131    unsigned n_packets;
2132    char *buf;
2133    size_t bufsz;
2134    int off;
2135
2136    n_packets = 0;
2137    TAILQ_FOREACH(packet_out, tailq, po_next)
2138        ++n_packets;
2139
2140    if (n_packets == 0)
2141    {
2142        LSQ_DEBUG("%s: [<empty set>]", prefix);
2143        return;
2144    }
2145
2146    bufsz = n_packets * sizeof("18446744073709551615" /* UINT64_MAX */);
2147    buf = malloc(bufsz);
2148    if (!buf)
2149    {
2150        LSQ_ERROR("%s: malloc: %s", __func__, strerror(errno));
2151        return;
2152    }
2153
2154    off = 0;
2155    TAILQ_FOREACH(packet_out, tailq, po_next)
2156    {
2157        if (off)
2158            buf[off++] = ' ';
2159        off += sprintf(buf + off, "%"PRIu64, packet_out->po_packno);
2160    }
2161
2162    LSQ_DEBUG("%s: [%s]", prefix, buf);
2163    free(buf);
2164}
2165
2166#define LOG_PACKET_Q(prefix, queue) do {                                    \
2167    if (LSQ_LOG_ENABLED(LSQ_LOG_DEBUG))                                     \
2168        send_ctl_log_packet_q(ctl, queue, prefix);                          \
2169} while (0)
2170#else
2171#define LOG_PACKET_Q(p, q)
2172#endif
2173
2174
2175int
2176lsquic_send_ctl_squeeze_sched (lsquic_send_ctl_t *ctl)
2177{
2178    struct lsquic_packet_out *packet_out, *next;
2179    int dropped;
2180#ifndef NDEBUG
2181    int pre_squeeze_logged = 0;
2182#endif
2183
2184    dropped = 0;
2185    for (packet_out = TAILQ_FIRST(&ctl->sc_scheduled_packets); packet_out;
2186                                                            packet_out = next)
2187    {
2188        next = TAILQ_NEXT(packet_out, po_next);
2189        if (packet_out->po_regen_sz < packet_out->po_data_sz)
2190        {
2191            if (packet_out->po_flags & PO_ENCRYPTED)
2192                send_ctl_return_enc_data(ctl, packet_out);
2193        }
2194        else
2195        {
2196#ifndef NDEBUG
2197            /* Log the whole list before we squeeze for the first time */
2198            if (!pre_squeeze_logged++)
2199                LOG_PACKET_Q(&ctl->sc_scheduled_packets,
2200                                        "unacked packets before squeezing");
2201#endif
2202            send_ctl_sched_remove(ctl, packet_out);
2203            LSQ_DEBUG("Dropping packet %"PRIu64" from scheduled queue",
2204                packet_out->po_packno);
2205            send_ctl_destroy_chain(ctl, packet_out, NULL);
2206            send_ctl_destroy_packet(ctl, packet_out);
2207            ++dropped;
2208        }
2209    }
2210
2211    if (dropped)
2212        lsquic_send_ctl_reset_packnos(ctl);
2213
2214#ifndef NDEBUG
2215    if (pre_squeeze_logged)
2216        LOG_PACKET_Q(&ctl->sc_scheduled_packets,
2217                                        "unacked packets after squeezing");
2218    else if (ctl->sc_n_scheduled > 0)
2219        LOG_PACKET_Q(&ctl->sc_scheduled_packets, "delayed packets");
2220#endif
2221
2222    return ctl->sc_n_scheduled > 0;
2223}
2224
2225
2226void
2227lsquic_send_ctl_reset_packnos (lsquic_send_ctl_t *ctl)
2228{
2229    struct lsquic_packet_out *packet_out;
2230
2231    ctl->sc_cur_packno = lsquic_senhist_largest(&ctl->sc_senhist);
2232    TAILQ_FOREACH(packet_out, &ctl->sc_scheduled_packets, po_next)
2233        packet_out->po_flags |= PO_REPACKNO;
2234}
2235
2236
2237void
2238lsquic_send_ctl_ack_to_front (struct lsquic_send_ctl *ctl, unsigned n_acks)
2239{
2240    struct lsquic_packet_out *ack_packet;
2241
2242    assert(n_acks > 0);
2243    assert(ctl->sc_n_scheduled > n_acks);   /* Otherwise, why is this called? */
2244    for ( ; n_acks > 0; --n_acks)
2245    {
2246        ack_packet = TAILQ_LAST(&ctl->sc_scheduled_packets, lsquic_packets_tailq);
2247        assert(ack_packet->po_frame_types & (1 << QUIC_FRAME_ACK));
2248        TAILQ_REMOVE(&ctl->sc_scheduled_packets, ack_packet, po_next);
2249        TAILQ_INSERT_HEAD(&ctl->sc_scheduled_packets, ack_packet, po_next);
2250    }
2251}
2252
2253
2254void
2255lsquic_send_ctl_drop_scheduled (lsquic_send_ctl_t *ctl)
2256{
2257    struct lsquic_packet_out *packet_out, *next;
2258    unsigned n;
2259
2260    n = 0;
2261    for (packet_out = TAILQ_FIRST(&ctl->sc_scheduled_packets); packet_out;
2262                                                            packet_out = next)
2263    {
2264        next = TAILQ_NEXT(packet_out, po_next);
2265        if (0 == (packet_out->po_flags & PO_HELLO))
2266        {
2267            send_ctl_sched_remove(ctl, packet_out);
2268            send_ctl_destroy_chain(ctl, packet_out, NULL);
2269            send_ctl_destroy_packet(ctl, packet_out);
2270            ++n;
2271        }
2272    }
2273
2274    ctl->sc_senhist.sh_flags |= SH_GAP_OK;
2275
2276    LSQ_DEBUG("dropped %u scheduled packet%s (%u left)", n, n != 1 ? "s" : "",
2277        ctl->sc_n_scheduled);
2278}
2279
2280
2281#ifdef NDEBUG
2282static
2283#elif __GNUC__
2284__attribute__((weak))
2285#endif
2286enum buf_packet_type
2287lsquic_send_ctl_determine_bpt (lsquic_send_ctl_t *ctl,
2288                                            const lsquic_stream_t *stream)
2289{
2290    const lsquic_stream_t *other_stream;
2291    struct lsquic_hash_elem *el;
2292    struct lsquic_hash *all_streams;
2293
2294    all_streams = ctl->sc_conn_pub->all_streams;
2295    for (el = lsquic_hash_first(all_streams); el;
2296                                     el = lsquic_hash_next(all_streams))
2297    {
2298        other_stream = lsquic_hashelem_getdata(el);
2299        if (other_stream != stream
2300              && (!(other_stream->stream_flags & STREAM_U_WRITE_DONE))
2301                && !lsquic_stream_is_critical(other_stream)
2302                  && other_stream->sm_priority < stream->sm_priority)
2303            return BPT_OTHER_PRIO;
2304    }
2305    return BPT_HIGHEST_PRIO;
2306}
2307
2308
2309static enum buf_packet_type
2310send_ctl_lookup_bpt (lsquic_send_ctl_t *ctl,
2311                                        const struct lsquic_stream *stream)
2312{
2313    if (ctl->sc_cached_bpt.stream_id != stream->id)
2314    {
2315        ctl->sc_cached_bpt.stream_id = stream->id;
2316        ctl->sc_cached_bpt.packet_type =
2317                                lsquic_send_ctl_determine_bpt(ctl, stream);
2318    }
2319    return ctl->sc_cached_bpt.packet_type;
2320}
2321
2322
2323static unsigned
2324send_ctl_max_bpq_count (const lsquic_send_ctl_t *ctl,
2325                                        enum buf_packet_type packet_type)
2326{
2327    unsigned long cwnd;
2328    unsigned count;
2329
2330    switch (packet_type)
2331    {
2332    case BPT_OTHER_PRIO:
2333        return MAX_BPQ_COUNT;
2334    case BPT_HIGHEST_PRIO:
2335    default: /* clang does not complain about absence of `default'... */
2336        count = ctl->sc_n_scheduled + ctl->sc_n_in_flight_retx;
2337        cwnd = ctl->sc_ci->cci_get_cwnd(CGP(ctl));
2338        if (count < cwnd / SC_PACK_SIZE(ctl))
2339        {
2340            count = cwnd / SC_PACK_SIZE(ctl) - count;
2341            if (count > MAX_BPQ_COUNT)
2342                return count;
2343        }
2344        return MAX_BPQ_COUNT;
2345    }
2346}
2347
2348
2349static void
2350send_ctl_move_ack (struct lsquic_send_ctl *ctl, struct lsquic_packet_out *dst,
2351                    struct lsquic_packet_out *src)
2352{
2353    assert(dst->po_data_sz == 0);
2354
2355    if (lsquic_packet_out_avail(dst) >= src->po_regen_sz)
2356    {
2357        memcpy(dst->po_data, src->po_data, src->po_regen_sz);
2358        dst->po_data_sz = src->po_regen_sz;
2359        dst->po_regen_sz = src->po_regen_sz;
2360        dst->po_frame_types |= (GQUIC_FRAME_REGEN_MASK & src->po_frame_types);
2361        src->po_frame_types &= ~GQUIC_FRAME_REGEN_MASK;
2362        lsquic_packet_out_chop_regen(src);
2363    }
2364}
2365
2366
2367static lsquic_packet_out_t *
2368send_ctl_get_buffered_packet (lsquic_send_ctl_t *ctl,
2369            enum buf_packet_type packet_type, unsigned need_at_least,
2370            const struct network_path *path, const struct lsquic_stream *stream)
2371{
2372    struct buf_packet_q *const packet_q =
2373                                    &ctl->sc_buffered_packets[packet_type];
2374    struct lsquic_conn *const lconn = ctl->sc_conn_pub->lconn;
2375    lsquic_packet_out_t *packet_out;
2376    enum packno_bits bits;
2377    enum { AA_STEAL, AA_GENERATE, AA_NONE, } ack_action;
2378
2379    packet_out = TAILQ_LAST(&packet_q->bpq_packets, lsquic_packets_tailq);
2380    if (packet_out
2381        && !(packet_out->po_flags & PO_STREAM_END)
2382        && lsquic_packet_out_avail(packet_out) >= need_at_least)
2383    {
2384        return packet_out;
2385    }
2386
2387    if (packet_q->bpq_count >= send_ctl_max_bpq_count(ctl, packet_type))
2388        return NULL;
2389
2390    if (packet_q->bpq_count == 0)
2391    {
2392        /* If ACK was written to the low-priority queue first, steal it */
2393        if (packet_q == &ctl->sc_buffered_packets[BPT_HIGHEST_PRIO]
2394            && !TAILQ_EMPTY(&ctl->sc_buffered_packets[BPT_OTHER_PRIO].bpq_packets)
2395            && (TAILQ_FIRST(&ctl->sc_buffered_packets[BPT_OTHER_PRIO].bpq_packets)
2396                                        ->po_frame_types & QUIC_FTBIT_ACK))
2397        {
2398            LSQ_DEBUG("steal ACK frame from low-priority buffered queue");
2399            ack_action = AA_STEAL;
2400            bits = ctl->sc_max_packno_bits;
2401        }
2402        /* If ACK can be generated, write it to the first buffered packet. */
2403        else if (lconn->cn_if->ci_can_write_ack(lconn))
2404        {
2405            LSQ_DEBUG("generate ACK frame for first buffered packet in "
2406                                                    "queue #%u", packet_type);
2407            ack_action = AA_GENERATE;
2408            /* Packet length is set to the largest possible size to guarantee
2409             * that buffered packet with the ACK will not need to be split.
2410             */
2411            bits = ctl->sc_max_packno_bits;
2412        }
2413        else
2414            goto no_ack_action;
2415    }
2416    else
2417    {
2418  no_ack_action:
2419        ack_action = AA_NONE;
2420        bits = lsquic_send_ctl_guess_packno_bits(ctl);
2421    }
2422
2423    packet_out = send_ctl_allocate_packet(ctl, bits, need_at_least, PNS_APP,
2424                                                                        path);
2425    if (!packet_out)
2426        return NULL;
2427
2428    switch (ack_action)
2429    {
2430    case AA_STEAL:
2431        send_ctl_move_ack(ctl, packet_out,
2432            TAILQ_FIRST(&ctl->sc_buffered_packets[BPT_OTHER_PRIO].bpq_packets));
2433        break;
2434    case AA_GENERATE:
2435        lconn->cn_if->ci_write_ack(lconn, packet_out);
2436        break;
2437    case AA_NONE:
2438        break;
2439    }
2440
2441    TAILQ_INSERT_TAIL(&packet_q->bpq_packets, packet_out, po_next);
2442    ++packet_q->bpq_count;
2443    LSQ_DEBUG("Add new packet to buffered queue #%u; count: %u",
2444              packet_type, packet_q->bpq_count);
2445    return packet_out;
2446}
2447
2448
2449static void
2450send_ctl_maybe_flush_decoder (struct lsquic_send_ctl *ctl,
2451                                        const struct lsquic_stream *caller)
2452{
2453    struct lsquic_stream *decoder;
2454
2455    if ((ctl->sc_flags & SC_IETF) && ctl->sc_conn_pub->u.ietf.qdh)
2456    {
2457        decoder = ctl->sc_conn_pub->u.ietf.qdh->qdh_dec_sm_out;
2458        if (decoder && decoder != caller
2459                                && lsquic_stream_has_data_to_flush(decoder))
2460        {
2461            LSQ_DEBUG("flushing decoder stream");
2462            lsquic_stream_flush(decoder);
2463        }
2464    }
2465}
2466
2467
2468lsquic_packet_out_t *
2469lsquic_send_ctl_get_packet_for_stream (lsquic_send_ctl_t *ctl,
2470                unsigned need_at_least, const struct network_path *path,
2471                const struct lsquic_stream *stream)
2472{
2473    enum buf_packet_type packet_type;
2474
2475    if (lsquic_send_ctl_schedule_stream_packets_immediately(ctl))
2476        return lsquic_send_ctl_get_writeable_packet(ctl, PNS_APP,
2477                                                need_at_least, path, 0, NULL);
2478    else
2479    {
2480        if (!lsquic_send_ctl_has_buffered(ctl))
2481            send_ctl_maybe_flush_decoder(ctl, stream);
2482        packet_type = send_ctl_lookup_bpt(ctl, stream);
2483        return send_ctl_get_buffered_packet(ctl, packet_type, need_at_least,
2484                                            path, stream);
2485    }
2486}
2487
2488
2489#ifdef NDEBUG
2490static
2491#elif __GNUC__
2492__attribute__((weak))
2493#endif
2494enum packno_bits
2495lsquic_send_ctl_calc_packno_bits (lsquic_send_ctl_t *ctl)
2496{
2497    lsquic_packno_t smallest_unacked;
2498    enum packno_bits bits;
2499    unsigned n_in_flight;
2500    unsigned long cwnd;
2501    const struct parse_funcs *pf;
2502
2503    pf = ctl->sc_conn_pub->lconn->cn_pf;
2504
2505    smallest_unacked = lsquic_send_ctl_smallest_unacked(ctl);
2506    cwnd = ctl->sc_ci->cci_get_cwnd(CGP(ctl));
2507    n_in_flight = cwnd / SC_PACK_SIZE(ctl);
2508    bits = pf->pf_calc_packno_bits(ctl->sc_cur_packno + 1, smallest_unacked,
2509                                                            n_in_flight);
2510    if (bits <= ctl->sc_max_packno_bits)
2511        return bits;
2512    else
2513        return ctl->sc_max_packno_bits;
2514}
2515
2516
2517enum packno_bits
2518lsquic_send_ctl_packno_bits (lsquic_send_ctl_t *ctl)
2519{
2520
2521    if (lsquic_send_ctl_schedule_stream_packets_immediately(ctl))
2522        return lsquic_send_ctl_calc_packno_bits(ctl);
2523    else
2524        return lsquic_send_ctl_guess_packno_bits(ctl);
2525}
2526
2527
2528static int
2529split_buffered_packet (lsquic_send_ctl_t *ctl,
2530        enum buf_packet_type packet_type, lsquic_packet_out_t *packet_out,
2531        enum packno_bits bits, unsigned excess_bytes)
2532{
2533    struct buf_packet_q *const packet_q =
2534                                    &ctl->sc_buffered_packets[packet_type];
2535    lsquic_packet_out_t *new_packet_out;
2536
2537    assert(TAILQ_FIRST(&packet_q->bpq_packets) == packet_out);
2538
2539    new_packet_out = send_ctl_allocate_packet(ctl, bits, 0,
2540                        lsquic_packet_out_pns(packet_out), packet_out->po_path);
2541    if (!new_packet_out)
2542        return -1;
2543
2544    if (0 == lsquic_packet_out_split_in_two(&ctl->sc_enpub->enp_mm, packet_out,
2545                  new_packet_out, ctl->sc_conn_pub->lconn->cn_pf, excess_bytes))
2546    {
2547        lsquic_packet_out_set_packno_bits(packet_out, bits);
2548        TAILQ_INSERT_AFTER(&packet_q->bpq_packets, packet_out, new_packet_out,
2549                           po_next);
2550        ++packet_q->bpq_count;
2551        LSQ_DEBUG("Add split packet to buffered queue #%u; count: %u",
2552                  packet_type, packet_q->bpq_count);
2553        return 0;
2554    }
2555    else
2556    {
2557        send_ctl_destroy_packet(ctl, new_packet_out);
2558        return -1;
2559    }
2560}
2561
2562
2563int
2564lsquic_send_ctl_schedule_buffered (lsquic_send_ctl_t *ctl,
2565                                            enum buf_packet_type packet_type)
2566{
2567    struct buf_packet_q *const packet_q =
2568                                    &ctl->sc_buffered_packets[packet_type];
2569    const struct parse_funcs *const pf = ctl->sc_conn_pub->lconn->cn_pf;
2570    lsquic_packet_out_t *packet_out;
2571    unsigned used, excess;
2572
2573    assert(lsquic_send_ctl_schedule_stream_packets_immediately(ctl));
2574    const enum packno_bits bits = lsquic_send_ctl_calc_packno_bits(ctl);
2575    const unsigned need = pf->pf_packno_bits2len(bits);
2576
2577    while ((packet_out = TAILQ_FIRST(&packet_q->bpq_packets)) &&
2578                                            lsquic_send_ctl_can_send(ctl))
2579    {
2580        if ((packet_out->po_frame_types & QUIC_FTBIT_ACK)
2581                            && packet_out->po_ack2ed < ctl->sc_largest_acked)
2582        {
2583            /* Chrome watches for a decrease in the value of the Largest
2584             * Observed field of the ACK frame and marks it as an error:
2585             * this is why we have to send out ACK in the order they were
2586             * generated.
2587             */
2588            LSQ_DEBUG("Remove out-of-order ACK from buffered packet");
2589            lsquic_packet_out_chop_regen(packet_out);
2590            if (packet_out->po_data_sz == 0)
2591            {
2592                LSQ_DEBUG("Dropping now-empty buffered packet");
2593                TAILQ_REMOVE(&packet_q->bpq_packets, packet_out, po_next);
2594                --packet_q->bpq_count;
2595                send_ctl_destroy_packet(ctl, packet_out);
2596                continue;
2597            }
2598        }
2599        if (bits != lsquic_packet_out_packno_bits(packet_out))
2600        {
2601            used = pf->pf_packno_bits2len(
2602                                lsquic_packet_out_packno_bits(packet_out));
2603            if (need > used
2604                && need - used > lsquic_packet_out_avail(packet_out))
2605            {
2606                excess = need - used - lsquic_packet_out_avail(packet_out);
2607                if (0 != split_buffered_packet(ctl, packet_type,
2608                                               packet_out, bits, excess))
2609                {
2610                    return -1;
2611                }
2612            }
2613        }
2614        TAILQ_REMOVE(&packet_q->bpq_packets, packet_out, po_next);
2615        --packet_q->bpq_count;
2616        packet_out->po_packno = send_ctl_next_packno(ctl);
2617        LSQ_DEBUG("Remove packet from buffered queue #%u; count: %u.  "
2618            "It becomes packet %"PRIu64, packet_type, packet_q->bpq_count,
2619            packet_out->po_packno);
2620        lsquic_send_ctl_scheduled_one(ctl, packet_out);
2621    }
2622
2623    return 0;
2624}
2625
2626
2627int
2628lsquic_send_ctl_turn_on_fin (struct lsquic_send_ctl *ctl,
2629                             const struct lsquic_stream *stream)
2630{
2631    enum buf_packet_type packet_type;
2632    struct buf_packet_q *packet_q;
2633    lsquic_packet_out_t *packet_out;
2634    const struct parse_funcs *pf;
2635
2636    pf = ctl->sc_conn_pub->lconn->cn_pf;
2637    packet_type = send_ctl_lookup_bpt(ctl, stream);
2638    packet_q = &ctl->sc_buffered_packets[packet_type];
2639
2640    TAILQ_FOREACH_REVERSE(packet_out, &packet_q->bpq_packets,
2641                          lsquic_packets_tailq, po_next)
2642        if (0 == lsquic_packet_out_turn_on_fin(packet_out, pf, stream))
2643            return 0;
2644
2645    TAILQ_FOREACH(packet_out, &ctl->sc_scheduled_packets, po_next)
2646        if (0 == packet_out->po_sent
2647            && 0 == lsquic_packet_out_turn_on_fin(packet_out, pf, stream))
2648        {
2649            return 0;
2650        }
2651
2652    return -1;
2653}
2654
2655
2656size_t
2657lsquic_send_ctl_mem_used (const struct lsquic_send_ctl *ctl)
2658{
2659    const lsquic_packet_out_t *packet_out;
2660    unsigned n;
2661    size_t size;
2662    const struct lsquic_packets_tailq queues[] = {
2663        ctl->sc_scheduled_packets,
2664        ctl->sc_unacked_packets[PNS_INIT],
2665        ctl->sc_unacked_packets[PNS_HSK],
2666        ctl->sc_unacked_packets[PNS_APP],
2667        ctl->sc_lost_packets,
2668        ctl->sc_buffered_packets[0].bpq_packets,
2669        ctl->sc_buffered_packets[1].bpq_packets,
2670    };
2671
2672    size = sizeof(*ctl);
2673
2674    for (n = 0; n < sizeof(queues) / sizeof(queues[0]); ++n)
2675        TAILQ_FOREACH(packet_out, &queues[n], po_next)
2676            size += lsquic_packet_out_mem_used(packet_out);
2677
2678    return size;
2679}
2680
2681
2682void
2683lsquic_send_ctl_verneg_done (struct lsquic_send_ctl *ctl)
2684{
2685    ctl->sc_max_packno_bits = PACKNO_BITS_3;
2686    LSQ_DEBUG("version negotiation done (%s): max packno bits: %u",
2687        lsquic_ver2str[ ctl->sc_conn_pub->lconn->cn_version ],
2688        ctl->sc_max_packno_bits);
2689}
2690
2691
2692static void
2693strip_trailing_padding (struct lsquic_packet_out *packet_out)
2694{
2695    struct packet_out_srec_iter posi;
2696    const struct stream_rec *srec;
2697    unsigned off;
2698
2699    off = 0;
2700    for (srec = posi_first(&posi, packet_out); srec; srec = posi_next(&posi))
2701        off = srec->sr_off + srec->sr_len;
2702
2703    assert(off);
2704
2705    packet_out->po_data_sz = off;
2706    packet_out->po_frame_types &= ~QUIC_FTBIT_PADDING;
2707}
2708
2709
2710int
2711lsquic_send_ctl_retry (struct lsquic_send_ctl *ctl,
2712                                const unsigned char *token, size_t token_sz)
2713{
2714    struct lsquic_packet_out *packet_out, *next, *new_packet_out;
2715    struct lsquic_conn *const lconn = ctl->sc_conn_pub->lconn;
2716    size_t sz;
2717
2718    if (token_sz >= 1ull << (sizeof(packet_out->po_token_len) * 8))
2719    {
2720        LSQ_WARN("token size %zu is too long", token_sz);
2721        return -1;
2722    }
2723
2724    ++ctl->sc_retry_count;
2725    if (ctl->sc_retry_count > 3)
2726    {
2727        LSQ_INFO("failing connection after %u retries", ctl->sc_retry_count);
2728        return -1;
2729    }
2730
2731    send_ctl_expire(ctl, PNS_INIT, EXFI_ALL);
2732
2733    if (0 != lsquic_send_ctl_set_token(ctl, token, token_sz))
2734        return -1;
2735
2736    for (packet_out = TAILQ_FIRST(&ctl->sc_lost_packets); packet_out; packet_out = next)
2737    {
2738        next = TAILQ_NEXT(packet_out, po_next);
2739        if (HETY_INITIAL != packet_out->po_header_type)
2740            continue;
2741
2742        if (packet_out->po_nonce)
2743        {
2744            free(packet_out->po_nonce);
2745            packet_out->po_nonce = NULL;
2746            packet_out->po_flags &= ~PO_NONCE;
2747        }
2748
2749        if (0 != send_ctl_set_packet_out_token(ctl, packet_out))
2750        {
2751            LSQ_INFO("cannot set out token on packet");
2752            return -1;
2753        }
2754
2755        if (packet_out->po_frame_types & QUIC_FTBIT_PADDING)
2756            strip_trailing_padding(packet_out);
2757
2758        sz = lconn->cn_pf->pf_packout_size(lconn, packet_out);
2759        if (sz > 1200)
2760        {
2761            const enum packno_bits bits = lsquic_send_ctl_calc_packno_bits(ctl);
2762            new_packet_out = send_ctl_allocate_packet(ctl, bits, 0, PNS_INIT,
2763                                                        packet_out->po_path);
2764            if (!new_packet_out)
2765                return -1;
2766            if (0 != send_ctl_set_packet_out_token(ctl, new_packet_out))
2767            {
2768                send_ctl_destroy_packet(ctl, new_packet_out);
2769                LSQ_INFO("cannot set out token on packet");
2770                return -1;
2771            }
2772            if (0 == lsquic_packet_out_split_in_two(&ctl->sc_enpub->enp_mm,
2773                            packet_out, new_packet_out,
2774                            ctl->sc_conn_pub->lconn->cn_pf, sz - 1200))
2775            {
2776                LSQ_DEBUG("split lost packet %"PRIu64" into two",
2777                                                        packet_out->po_packno);
2778                lsquic_packet_out_set_packno_bits(packet_out, bits);
2779                TAILQ_INSERT_AFTER(&ctl->sc_lost_packets, packet_out,
2780                                    new_packet_out, po_next);
2781                new_packet_out->po_flags |= PO_LOST;
2782                packet_out->po_flags &= ~PO_SENT_SZ;
2783            }
2784            else
2785            {
2786                LSQ_DEBUG("could not split lost packet into two");
2787                send_ctl_destroy_packet(ctl, new_packet_out);
2788                return -1;
2789            }
2790        }
2791    }
2792
2793    return 0;
2794}
2795
2796
2797int
2798lsquic_send_ctl_set_token (struct lsquic_send_ctl *ctl,
2799                const unsigned char *token, size_t token_sz)
2800{
2801    unsigned char *copy;
2802
2803    if (token_sz > 1 <<
2804                (sizeof(((struct lsquic_packet_out *)0)->po_token_len) * 8))
2805    {
2806        errno = EINVAL;
2807        return -1;
2808    }
2809
2810    copy = malloc(token_sz);
2811    if (!copy)
2812        return -1;
2813    memcpy(copy, token, token_sz);
2814    free(ctl->sc_token);
2815    ctl->sc_token = copy;
2816    ctl->sc_token_sz = token_sz;
2817    LSQ_DEBUG("set token");
2818    return 0;
2819}
2820
2821
2822void
2823lsquic_send_ctl_empty_pns (struct lsquic_send_ctl *ctl, enum packnum_space pns)
2824{
2825    lsquic_packet_out_t *packet_out, *next;
2826    unsigned count, packet_sz;
2827    struct lsquic_packets_tailq *const *q;
2828    struct lsquic_packets_tailq *const queues[] = {
2829        &ctl->sc_lost_packets,
2830        &ctl->sc_buffered_packets[0].bpq_packets,
2831        &ctl->sc_buffered_packets[1].bpq_packets,
2832    };
2833
2834    /* Don't bother with chain destruction, as all chains members are always
2835     * within the same packet number space
2836     */
2837
2838    count = 0;
2839    for (packet_out = TAILQ_FIRST(&ctl->sc_scheduled_packets); packet_out;
2840                                                            packet_out = next)
2841    {
2842        next = TAILQ_NEXT(packet_out, po_next);
2843        if (pns == lsquic_packet_out_pns(packet_out))
2844        {
2845            send_ctl_maybe_renumber_sched_to_right(ctl, packet_out);
2846            send_ctl_sched_remove(ctl, packet_out);
2847            send_ctl_destroy_packet(ctl, packet_out);
2848            ++count;
2849        }
2850    }
2851
2852    for (packet_out = TAILQ_FIRST(&ctl->sc_unacked_packets[pns]); packet_out;
2853                                                            packet_out = next)
2854    {
2855        next = TAILQ_NEXT(packet_out, po_next);
2856        if (packet_out->po_flags & (PO_LOSS_REC|PO_POISON))
2857            TAILQ_REMOVE(&ctl->sc_unacked_packets[pns], packet_out, po_next);
2858        else
2859        {
2860            packet_sz = packet_out_sent_sz(packet_out);
2861            send_ctl_unacked_remove(ctl, packet_out, packet_sz);
2862            lsquic_packet_out_ack_streams(packet_out);
2863        }
2864        send_ctl_destroy_packet(ctl, packet_out);
2865        ++count;
2866    }
2867
2868    for (q = queues; q < queues + sizeof(queues) / sizeof(queues[0]); ++q)
2869        for (packet_out = TAILQ_FIRST(*q); packet_out; packet_out = next)
2870            {
2871                next = TAILQ_NEXT(packet_out, po_next);
2872                if (pns == lsquic_packet_out_pns(packet_out))
2873                {
2874                    TAILQ_REMOVE(*q, packet_out, po_next);
2875                    send_ctl_destroy_packet(ctl, packet_out);
2876                    ++count;
2877                }
2878            }
2879
2880    lsquic_alarmset_unset(ctl->sc_alset, AL_RETX_INIT + pns);
2881
2882    LSQ_DEBUG("emptied %s, destroyed %u packet%.*s", lsquic_pns2str[pns],
2883        count, count != 1, "s");
2884}
2885
2886
2887void
2888lsquic_send_ctl_repath (struct lsquic_send_ctl *ctl, struct network_path *old,
2889                                                    struct network_path *new)
2890{
2891    struct lsquic_packet_out *packet_out;
2892    unsigned count;
2893    struct lsquic_packets_tailq *const *q;
2894    struct lsquic_packets_tailq *const queues[] = {
2895        &ctl->sc_scheduled_packets,
2896        &ctl->sc_unacked_packets[PNS_INIT],
2897        &ctl->sc_unacked_packets[PNS_HSK],
2898        &ctl->sc_unacked_packets[PNS_APP],
2899        &ctl->sc_lost_packets,
2900        &ctl->sc_buffered_packets[0].bpq_packets,
2901        &ctl->sc_buffered_packets[1].bpq_packets,
2902    };
2903
2904    assert(ctl->sc_flags & SC_IETF);
2905
2906    count = 0;
2907    for (q = queues; q < queues + sizeof(queues) / sizeof(queues[0]); ++q)
2908        TAILQ_FOREACH(packet_out, *q, po_next)
2909            if (packet_out->po_path == old)
2910            {
2911                ++count;
2912                packet_out->po_path = new;
2913                if (packet_out->po_flags & PO_ENCRYPTED)
2914                    send_ctl_return_enc_data(ctl, packet_out);
2915            }
2916
2917    LSQ_DEBUG("repathed %u packet%.*s", count, count != 1, "s");
2918
2919    memset(&ctl->sc_conn_pub->rtt_stats, 0,
2920                                    sizeof(ctl->sc_conn_pub->rtt_stats));
2921    ctl->sc_ci->cci_reinit(CGP(ctl));
2922}
2923
2924
2925void
2926lsquic_send_ctl_return_enc_data (struct lsquic_send_ctl *ctl)
2927{
2928    struct lsquic_packet_out *packet_out;
2929
2930    assert(!(ctl->sc_flags & SC_IETF));
2931
2932    TAILQ_FOREACH(packet_out, &ctl->sc_scheduled_packets, po_next)
2933        if (packet_out->po_flags & PO_ENCRYPTED)
2934            send_ctl_return_enc_data(ctl, packet_out);
2935}
2936
2937
2938/* When client updated DCID based on the first packet returned by the server,
2939 * we must update the number of bytes scheduled if the DCID length changed
2940 * because this length is used to calculate packet size.
2941 */
2942void
2943lsquic_send_ctl_cidlen_change (struct lsquic_send_ctl *ctl,
2944                                unsigned orig_cid_len, unsigned new_cid_len)
2945{
2946    unsigned diff;
2947
2948    assert(!(ctl->sc_conn_pub->lconn->cn_flags & LSCONN_SERVER));
2949    if (ctl->sc_n_scheduled)
2950    {
2951        ctl->sc_flags |= SC_CIDLEN;
2952        ctl->sc_cidlen = (signed char) new_cid_len - (signed char) orig_cid_len;
2953        if (new_cid_len > orig_cid_len)
2954        {
2955            diff = new_cid_len - orig_cid_len;
2956            diff *= ctl->sc_n_scheduled;
2957            ctl->sc_bytes_scheduled += diff;
2958            LSQ_DEBUG("increased bytes scheduled by %u bytes to %u",
2959                diff, ctl->sc_bytes_scheduled);
2960        }
2961        else if (new_cid_len < orig_cid_len)
2962        {
2963            diff = orig_cid_len - new_cid_len;
2964            diff *= ctl->sc_n_scheduled;
2965            ctl->sc_bytes_scheduled -= diff;
2966            LSQ_DEBUG("decreased bytes scheduled by %u bytes to %u",
2967                diff, ctl->sc_bytes_scheduled);
2968        }
2969        else
2970            LSQ_DEBUG("DCID length did not change");
2971    }
2972    else
2973        LSQ_DEBUG("no scheduled packets at the time of DCID change");
2974}
2975
2976
2977void
2978lsquic_send_ctl_begin_optack_detection (struct lsquic_send_ctl *ctl)
2979{
2980    uint8_t rand;
2981
2982    rand = lsquic_crand_get_byte(ctl->sc_enpub->enp_crand);
2983    ctl->sc_gap = ctl->sc_cur_packno + 1 + rand;
2984}
2985