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