Frédéric Lécaille | 1601395 | 2022-04-29 15:07:48 +0200 | [diff] [blame] | 1 | #include <import/eb64tree.h> |
| 2 | |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 3 | #include <haproxy/quic_conn-t.h> |
Amaury Denoyelle | 0c2d964 | 2022-03-24 16:08:05 +0100 | [diff] [blame] | 4 | #include <haproxy/quic_loss.h> |
Frédéric Lécaille | e1a49cf | 2022-09-16 16:24:47 +0200 | [diff] [blame] | 5 | #include <haproxy/quic_tls.h> |
Amaury Denoyelle | 0c2d964 | 2022-03-24 16:08:05 +0100 | [diff] [blame] | 6 | |
Frédéric Lécaille | eb79145 | 2022-05-24 16:01:39 +0200 | [diff] [blame] | 7 | #include <haproxy/atomic.h> |
Amaury Denoyelle | 5c25dc5 | 2022-09-30 17:44:15 +0200 | [diff] [blame] | 8 | #include <haproxy/list.h> |
Amaury Denoyelle | 0c2d964 | 2022-03-24 16:08:05 +0100 | [diff] [blame] | 9 | #include <haproxy/ticks.h> |
| 10 | #include <haproxy/trace.h> |
| 11 | |
| 12 | #define TRACE_SOURCE &trace_quic |
| 13 | |
| 14 | /* Update <ql> QUIC loss information with new <rtt> measurement and <ack_delay> |
| 15 | * on ACK frame receipt which MUST be min(ack->ack_delay, max_ack_delay) |
| 16 | * before the handshake is confirmed. |
| 17 | */ |
| 18 | void quic_loss_srtt_update(struct quic_loss *ql, |
| 19 | unsigned int rtt, unsigned int ack_delay, |
| 20 | struct quic_conn *qc) |
| 21 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 22 | TRACE_ENTER(QUIC_EV_CONN_RTTUPDT, qc); |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 23 | TRACE_PROTO("TX loss srtt update", QUIC_EV_CONN_RTTUPDT, qc, &rtt, &ack_delay, ql); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 24 | |
Amaury Denoyelle | 0c2d964 | 2022-03-24 16:08:05 +0100 | [diff] [blame] | 25 | ql->latest_rtt = rtt; |
| 26 | if (!ql->rtt_min) { |
| 27 | /* No previous measurement. */ |
| 28 | ql->srtt = rtt << 3; |
| 29 | /* rttval <- rtt / 2 or 4*rttval <- 2*rtt. */ |
| 30 | ql->rtt_var = rtt << 1; |
| 31 | ql->rtt_min = rtt; |
| 32 | } |
| 33 | else { |
| 34 | int diff; |
| 35 | |
| 36 | ql->rtt_min = QUIC_MIN(rtt, ql->rtt_min); |
| 37 | /* Specific to QUIC (RTT adjustment). */ |
| 38 | if (ack_delay && rtt > ql->rtt_min + ack_delay) |
| 39 | rtt -= ack_delay; |
Frédéric Lécaille | 5d5afe7 | 2023-03-30 15:31:06 +0200 | [diff] [blame] | 40 | diff = (ql->srtt >> 3) - rtt; |
Amaury Denoyelle | 0c2d964 | 2022-03-24 16:08:05 +0100 | [diff] [blame] | 41 | if (diff < 0) |
| 42 | diff = -diff; |
| 43 | /* 4*rttvar = 3*rttvar + |diff| */ |
| 44 | ql->rtt_var += diff - (ql->rtt_var >> 2); |
| 45 | /* 8*srtt = 7*srtt + rtt */ |
| 46 | ql->srtt += rtt - (ql->srtt >> 3); |
| 47 | } |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 48 | |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 49 | TRACE_PROTO("TX loss srtt update", QUIC_EV_CONN_RTTUPDT, qc,,, ql); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 50 | TRACE_LEAVE(QUIC_EV_CONN_RTTUPDT, qc); |
Amaury Denoyelle | 0c2d964 | 2022-03-24 16:08:05 +0100 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /* Returns for <qc> QUIC connection the first packet number space which |
| 54 | * experienced packet loss, if any or a packet number space with |
| 55 | * TICK_ETERNITY as packet loss time if not. |
| 56 | */ |
| 57 | struct quic_pktns *quic_loss_pktns(struct quic_conn *qc) |
| 58 | { |
| 59 | enum quic_tls_pktns i; |
| 60 | struct quic_pktns *pktns; |
| 61 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 62 | TRACE_ENTER(QUIC_EV_CONN_SPTO, qc); |
| 63 | |
Amaury Denoyelle | 0c2d964 | 2022-03-24 16:08:05 +0100 | [diff] [blame] | 64 | pktns = &qc->pktns[QUIC_TLS_PKTNS_INITIAL]; |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 65 | TRACE_PROTO("TX loss pktns", QUIC_EV_CONN_SPTO, qc, pktns); |
Amaury Denoyelle | 0c2d964 | 2022-03-24 16:08:05 +0100 | [diff] [blame] | 66 | for (i = QUIC_TLS_PKTNS_HANDSHAKE; i < QUIC_TLS_PKTNS_MAX; i++) { |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 67 | TRACE_PROTO("TX loss pktns", QUIC_EV_CONN_SPTO, qc, &qc->pktns[i]); |
Amaury Denoyelle | 0c2d964 | 2022-03-24 16:08:05 +0100 | [diff] [blame] | 68 | if (!tick_isset(pktns->tx.loss_time) || |
| 69 | qc->pktns[i].tx.loss_time < pktns->tx.loss_time) |
| 70 | pktns = &qc->pktns[i]; |
| 71 | } |
| 72 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 73 | TRACE_LEAVE(QUIC_EV_CONN_SPTO, qc); |
| 74 | |
Amaury Denoyelle | 0c2d964 | 2022-03-24 16:08:05 +0100 | [diff] [blame] | 75 | return pktns; |
| 76 | } |
| 77 | |
| 78 | /* Returns for <qc> QUIC connection the first packet number space to |
| 79 | * arm the PTO for if any or a packet number space with TICK_ETERNITY |
| 80 | * as PTO value if not. |
| 81 | */ |
| 82 | struct quic_pktns *quic_pto_pktns(struct quic_conn *qc, |
Frédéric Lécaille | b75eecc | 2023-01-26 15:18:17 +0100 | [diff] [blame] | 83 | int handshake_confirmed, |
Amaury Denoyelle | 0c2d964 | 2022-03-24 16:08:05 +0100 | [diff] [blame] | 84 | unsigned int *pto) |
| 85 | { |
| 86 | int i; |
| 87 | unsigned int duration, lpto; |
| 88 | struct quic_loss *ql = &qc->path->loss; |
| 89 | struct quic_pktns *pktns, *p; |
| 90 | |
| 91 | TRACE_ENTER(QUIC_EV_CONN_SPTO, qc); |
| 92 | duration = |
| 93 | (ql->srtt >> 3) + |
| 94 | (QUIC_MAX(ql->rtt_var, QUIC_TIMER_GRANULARITY) << ql->pto_count); |
| 95 | |
Amaury Denoyelle | 5f87041 | 2023-04-11 14:43:42 +0200 | [diff] [blame] | 96 | /* RFC 9002 6.2.2.1. Before Address Validation |
| 97 | * |
| 98 | * the client MUST set the PTO timer if the client has not received an |
| 99 | * acknowledgment for any of its Handshake packets and the handshake is |
| 100 | * not confirmed (see Section 4.1.2 of [QUIC-TLS]), even if there are no |
| 101 | * packets in flight. |
| 102 | * |
| 103 | * TODO implement the above paragraph for QUIC on backend side. Note |
| 104 | * that if now_ms is used this function is not reentrant anymore and can |
| 105 | * not be used anytime without side-effect (for example after QUIC |
| 106 | * connection migration). |
| 107 | */ |
Amaury Denoyelle | 0c2d964 | 2022-03-24 16:08:05 +0100 | [diff] [blame] | 108 | |
| 109 | lpto = TICK_ETERNITY; |
| 110 | pktns = p = &qc->pktns[QUIC_TLS_PKTNS_INITIAL]; |
| 111 | |
| 112 | for (i = QUIC_TLS_PKTNS_INITIAL; i < QUIC_TLS_PKTNS_MAX; i++) { |
| 113 | unsigned int tmp_pto; |
| 114 | |
| 115 | if (!qc->pktns[i].tx.in_flight) |
| 116 | continue; |
| 117 | |
| 118 | if (i == QUIC_TLS_PKTNS_01RTT) { |
Frédéric Lécaille | b75eecc | 2023-01-26 15:18:17 +0100 | [diff] [blame] | 119 | if (!handshake_confirmed) { |
Frédéric Lécaille | 2513b1d | 2023-04-07 15:39:17 +0200 | [diff] [blame] | 120 | TRACE_STATE("TX PTO handshake not already confirmed", QUIC_EV_CONN_SPTO, qc); |
Amaury Denoyelle | 0c2d964 | 2022-03-24 16:08:05 +0100 | [diff] [blame] | 121 | pktns = p; |
| 122 | goto out; |
| 123 | } |
| 124 | |
| 125 | duration += qc->max_ack_delay << ql->pto_count; |
| 126 | } |
| 127 | |
| 128 | p = &qc->pktns[i]; |
| 129 | tmp_pto = tick_add(p->tx.time_of_last_eliciting, duration); |
Frédéric Lécaille | 0222cc6 | 2023-04-04 15:47:16 +0200 | [diff] [blame] | 130 | if (!tick_isset(lpto) || tick_is_lt(tmp_pto, lpto)) { |
Amaury Denoyelle | 0c2d964 | 2022-03-24 16:08:05 +0100 | [diff] [blame] | 131 | lpto = tmp_pto; |
| 132 | pktns = p; |
| 133 | } |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 134 | TRACE_PROTO("TX PTO", QUIC_EV_CONN_SPTO, qc, p); |
Amaury Denoyelle | 0c2d964 | 2022-03-24 16:08:05 +0100 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | out: |
| 138 | if (pto) |
| 139 | *pto = lpto; |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 140 | TRACE_PROTO("TX PTO", QUIC_EV_CONN_SPTO, qc, pktns, &duration); |
| 141 | TRACE_LEAVE(QUIC_EV_CONN_SPTO, qc); |
Amaury Denoyelle | 0c2d964 | 2022-03-24 16:08:05 +0100 | [diff] [blame] | 142 | |
| 143 | return pktns; |
| 144 | } |
Frédéric Lécaille | 1601395 | 2022-04-29 15:07:48 +0200 | [diff] [blame] | 145 | |
| 146 | /* Look for packet loss from sent packets for <qel> encryption level of a |
| 147 | * connection with <ctx> as I/O handler context. If remove is true, remove them from |
| 148 | * their tree if deemed as lost or set the <loss_time> value the packet number |
| 149 | * space if any not deemed lost. |
| 150 | * Should be called after having received an ACK frame with newly acknowledged |
| 151 | * packets or when the the loss detection timer has expired. |
| 152 | * Always succeeds. |
| 153 | */ |
| 154 | void qc_packet_loss_lookup(struct quic_pktns *pktns, struct quic_conn *qc, |
| 155 | struct list *lost_pkts) |
| 156 | { |
| 157 | struct eb_root *pkts; |
| 158 | struct eb64_node *node; |
| 159 | struct quic_loss *ql; |
| 160 | unsigned int loss_delay; |
| 161 | |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 162 | TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc); |
| 163 | TRACE_PROTO("TX loss", QUIC_EV_CONN_PKTLOSS, qc, pktns); |
Frédéric Lécaille | 1601395 | 2022-04-29 15:07:48 +0200 | [diff] [blame] | 164 | pkts = &pktns->tx.pkts; |
| 165 | pktns->tx.loss_time = TICK_ETERNITY; |
| 166 | if (eb_is_empty(pkts)) |
| 167 | goto out; |
| 168 | |
| 169 | ql = &qc->path->loss; |
| 170 | loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3); |
Frédéric Lécaille | c40e19d | 2022-04-29 16:00:17 +0200 | [diff] [blame] | 171 | loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY)) * |
| 172 | QUIC_LOSS_TIME_THRESHOLD_MULTIPLICAND / QUIC_LOSS_TIME_THRESHOLD_DIVISOR; |
Frédéric Lécaille | 1601395 | 2022-04-29 15:07:48 +0200 | [diff] [blame] | 173 | |
| 174 | node = eb64_first(pkts); |
| 175 | while (node) { |
| 176 | struct quic_tx_packet *pkt; |
| 177 | int64_t largest_acked_pn; |
| 178 | unsigned int loss_time_limit, time_sent; |
| 179 | |
| 180 | pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node); |
| 181 | largest_acked_pn = pktns->rx.largest_acked_pn; |
| 182 | node = eb64_next(node); |
| 183 | if ((int64_t)pkt->pn_node.key > largest_acked_pn) |
| 184 | break; |
| 185 | |
| 186 | time_sent = pkt->time_sent; |
| 187 | loss_time_limit = tick_add(time_sent, loss_delay); |
Frédéric Lécaille | d2e104f | 2022-07-21 09:55:15 +0200 | [diff] [blame] | 188 | if (tick_is_le(loss_time_limit, now_ms) || |
Frédéric Lécaille | 1601395 | 2022-04-29 15:07:48 +0200 | [diff] [blame] | 189 | (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) { |
| 190 | eb64_delete(&pkt->pn_node); |
| 191 | LIST_APPEND(lost_pkts, &pkt->list); |
Frédéric Lécaille | fad0e6c | 2023-04-06 10:19:17 +0200 | [diff] [blame] | 192 | ql->nb_lost_pkt++; |
Frédéric Lécaille | eb79145 | 2022-05-24 16:01:39 +0200 | [diff] [blame] | 193 | HA_ATOMIC_INC(&qc->prx_counters->lost_pkt); |
Frédéric Lécaille | 1601395 | 2022-04-29 15:07:48 +0200 | [diff] [blame] | 194 | } |
| 195 | else { |
| 196 | if (tick_isset(pktns->tx.loss_time)) |
| 197 | pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit); |
| 198 | else |
| 199 | pktns->tx.loss_time = loss_time_limit; |
Frédéric Lécaille | dc591cd | 2022-07-21 14:24:41 +0200 | [diff] [blame] | 200 | break; |
Frédéric Lécaille | 1601395 | 2022-04-29 15:07:48 +0200 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
| 204 | out: |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 205 | TRACE_PROTO("TX loss", QUIC_EV_CONN_PKTLOSS, qc, pktns, lost_pkts); |
| 206 | TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc); |
Frédéric Lécaille | 1601395 | 2022-04-29 15:07:48 +0200 | [diff] [blame] | 207 | } |
| 208 | |