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