haproxy/src/quic_loss.c

211 lines
6.1 KiB
C
Raw Normal View History

#include <import/eb64tree.h>
#include <haproxy/quic_conn-t.h>
#include <haproxy/quic_loss.h>
#include <haproxy/quic_tls.h>
#include <haproxy/atomic.h>
#include <haproxy/list.h>
#include <haproxy/ticks.h>
#include <haproxy/trace.h>
#define TRACE_SOURCE &trace_quic
/* Update <ql> QUIC loss information with new <rtt> measurement and <ack_delay>
* on ACK frame receipt which MUST be min(ack->ack_delay, max_ack_delay)
* before the handshake is confirmed.
*/
void quic_loss_srtt_update(struct quic_loss *ql,
unsigned int rtt, unsigned int ack_delay,
struct quic_conn *qc)
{
TRACE_ENTER(QUIC_EV_CONN_RTTUPDT, qc);
TRACE_PROTO("TX loss srtt update", QUIC_EV_CONN_RTTUPDT, qc, &rtt, &ack_delay, ql);
ql->latest_rtt = rtt;
if (!ql->rtt_min) {
/* No previous measurement. */
ql->srtt = rtt << 3;
/* rttval <- rtt / 2 or 4*rttval <- 2*rtt. */
ql->rtt_var = rtt << 1;
ql->rtt_min = rtt;
}
else {
int diff;
ql->rtt_min = QUIC_MIN(rtt, ql->rtt_min);
/* Specific to QUIC (RTT adjustment). */
if (ack_delay && rtt > ql->rtt_min + ack_delay)
rtt -= ack_delay;
diff = (ql->srtt >> 3) - rtt;
if (diff < 0)
diff = -diff;
/* 4*rttvar = 3*rttvar + |diff| */
ql->rtt_var += diff - (ql->rtt_var >> 2);
/* 8*srtt = 7*srtt + rtt */
ql->srtt += rtt - (ql->srtt >> 3);
}
TRACE_PROTO("TX loss srtt update", QUIC_EV_CONN_RTTUPDT, qc,,, ql);
TRACE_LEAVE(QUIC_EV_CONN_RTTUPDT, qc);
}
/* Returns for <qc> QUIC connection the first packet number space which
* experienced packet loss, if any or a packet number space with
* TICK_ETERNITY as packet loss time if not.
*/
struct quic_pktns *quic_loss_pktns(struct quic_conn *qc)
{
MEDIUM: quic: Dynamic allocations of packet number spaces Add a pool to dynamically handle the memory used for the QUIC TLS packet number spaces. Remove the static array of packet number spaces at QUIC connection level (struct quic_conn) and add three new members to quic_conn struc as pointers to quic_pktns struct, one by packet number space as follows: ->ipktns for Initial packet number space, ->hpktns for Handshake packet number space and ->apktns for Application packet number space. Also add a ->pktns_list new member (struct list) to quic_conn struct to attach the list of the packet number spaces allocated for the QUIC connection. Implement ssl_to_quic_pktns() to map and retrieve the addresses of these pointers from TLS stack encryption levels. Modify quic_pktns_init() to initialize these members. Modify ha_quic_set_encryption_secrets() and ha_quic_add_handshake_data() to allocate the packet numbers and initialize the encryption level. Implement quic_pktns_release() which takes pointers to pointers to packet number space objects to release the memory allocated for a packet number space attached to a QUIC connection and reset their address values. Modify qc_new_conn() to allocation only the Initial packet number space and Initial encryption level. Modify QUIC loss detection API (quic_loss.c) to use the new ->pktns_list list attached to a QUIC connection in place of a static array of packet number spaces. Replace at several locations the use of elements of an array of packet number spaces by one of the three pointers to packet number spaces
2023-06-20 09:21:43 +00:00
struct quic_pktns *pktns, *p;
TRACE_ENTER(QUIC_EV_CONN_SPTO, qc);
MEDIUM: quic: Dynamic allocations of packet number spaces Add a pool to dynamically handle the memory used for the QUIC TLS packet number spaces. Remove the static array of packet number spaces at QUIC connection level (struct quic_conn) and add three new members to quic_conn struc as pointers to quic_pktns struct, one by packet number space as follows: ->ipktns for Initial packet number space, ->hpktns for Handshake packet number space and ->apktns for Application packet number space. Also add a ->pktns_list new member (struct list) to quic_conn struct to attach the list of the packet number spaces allocated for the QUIC connection. Implement ssl_to_quic_pktns() to map and retrieve the addresses of these pointers from TLS stack encryption levels. Modify quic_pktns_init() to initialize these members. Modify ha_quic_set_encryption_secrets() and ha_quic_add_handshake_data() to allocate the packet numbers and initialize the encryption level. Implement quic_pktns_release() which takes pointers to pointers to packet number space objects to release the memory allocated for a packet number space attached to a QUIC connection and reset their address values. Modify qc_new_conn() to allocation only the Initial packet number space and Initial encryption level. Modify QUIC loss detection API (quic_loss.c) to use the new ->pktns_list list attached to a QUIC connection in place of a static array of packet number spaces. Replace at several locations the use of elements of an array of packet number spaces by one of the three pointers to packet number spaces
2023-06-20 09:21:43 +00:00
BUG_ON(LIST_ISEMPTY(&qc->pktns_list));
pktns = p = LIST_NEXT(&qc->pktns_list, struct quic_pktns *, list);
do {
TRACE_PROTO("TX loss pktns", QUIC_EV_CONN_SPTO, qc, p);
if (!tick_isset(pktns->tx.loss_time) ||
MEDIUM: quic: Dynamic allocations of packet number spaces Add a pool to dynamically handle the memory used for the QUIC TLS packet number spaces. Remove the static array of packet number spaces at QUIC connection level (struct quic_conn) and add three new members to quic_conn struc as pointers to quic_pktns struct, one by packet number space as follows: ->ipktns for Initial packet number space, ->hpktns for Handshake packet number space and ->apktns for Application packet number space. Also add a ->pktns_list new member (struct list) to quic_conn struct to attach the list of the packet number spaces allocated for the QUIC connection. Implement ssl_to_quic_pktns() to map and retrieve the addresses of these pointers from TLS stack encryption levels. Modify quic_pktns_init() to initialize these members. Modify ha_quic_set_encryption_secrets() and ha_quic_add_handshake_data() to allocate the packet numbers and initialize the encryption level. Implement quic_pktns_release() which takes pointers to pointers to packet number space objects to release the memory allocated for a packet number space attached to a QUIC connection and reset their address values. Modify qc_new_conn() to allocation only the Initial packet number space and Initial encryption level. Modify QUIC loss detection API (quic_loss.c) to use the new ->pktns_list list attached to a QUIC connection in place of a static array of packet number spaces. Replace at several locations the use of elements of an array of packet number spaces by one of the three pointers to packet number spaces
2023-06-20 09:21:43 +00:00
tick_is_lt(p->tx.loss_time, pktns->tx.loss_time)) {
pktns = p;
}
p = LIST_NEXT(&p->list, struct quic_pktns *, list);
} while (&p->list != &qc->pktns_list);
TRACE_LEAVE(QUIC_EV_CONN_SPTO, qc);
return pktns;
}
/* Returns for <qc> QUIC connection the first packet number space to
* arm the PTO for if any or a packet number space with TICK_ETERNITY
* as PTO value if not.
*/
struct quic_pktns *quic_pto_pktns(struct quic_conn *qc,
int handshake_confirmed,
unsigned int *pto)
{
unsigned int duration, lpto;
struct quic_loss *ql = &qc->path->loss;
struct quic_pktns *pktns, *p;
TRACE_ENTER(QUIC_EV_CONN_SPTO, qc);
MEDIUM: quic: Dynamic allocations of packet number spaces Add a pool to dynamically handle the memory used for the QUIC TLS packet number spaces. Remove the static array of packet number spaces at QUIC connection level (struct quic_conn) and add three new members to quic_conn struc as pointers to quic_pktns struct, one by packet number space as follows: ->ipktns for Initial packet number space, ->hpktns for Handshake packet number space and ->apktns for Application packet number space. Also add a ->pktns_list new member (struct list) to quic_conn struct to attach the list of the packet number spaces allocated for the QUIC connection. Implement ssl_to_quic_pktns() to map and retrieve the addresses of these pointers from TLS stack encryption levels. Modify quic_pktns_init() to initialize these members. Modify ha_quic_set_encryption_secrets() and ha_quic_add_handshake_data() to allocate the packet numbers and initialize the encryption level. Implement quic_pktns_release() which takes pointers to pointers to packet number space objects to release the memory allocated for a packet number space attached to a QUIC connection and reset their address values. Modify qc_new_conn() to allocation only the Initial packet number space and Initial encryption level. Modify QUIC loss detection API (quic_loss.c) to use the new ->pktns_list list attached to a QUIC connection in place of a static array of packet number spaces. Replace at several locations the use of elements of an array of packet number spaces by one of the three pointers to packet number spaces
2023-06-20 09:21:43 +00:00
BUG_ON(LIST_ISEMPTY(&qc->pktns_list));
duration =
(ql->srtt >> 3) +
(QUIC_MAX(ql->rtt_var, QUIC_TIMER_GRANULARITY) << ql->pto_count);
/* RFC 9002 6.2.2.1. Before Address Validation
*
* the client MUST set the PTO timer if the client has not received an
* acknowledgment for any of its Handshake packets and the handshake is
* not confirmed (see Section 4.1.2 of [QUIC-TLS]), even if there are no
* packets in flight.
*
* TODO implement the above paragraph for QUIC on backend side. Note
* that if now_ms is used this function is not reentrant anymore and can
* not be used anytime without side-effect (for example after QUIC
* connection migration).
*/
lpto = TICK_ETERNITY;
MEDIUM: quic: Dynamic allocations of packet number spaces Add a pool to dynamically handle the memory used for the QUIC TLS packet number spaces. Remove the static array of packet number spaces at QUIC connection level (struct quic_conn) and add three new members to quic_conn struc as pointers to quic_pktns struct, one by packet number space as follows: ->ipktns for Initial packet number space, ->hpktns for Handshake packet number space and ->apktns for Application packet number space. Also add a ->pktns_list new member (struct list) to quic_conn struct to attach the list of the packet number spaces allocated for the QUIC connection. Implement ssl_to_quic_pktns() to map and retrieve the addresses of these pointers from TLS stack encryption levels. Modify quic_pktns_init() to initialize these members. Modify ha_quic_set_encryption_secrets() and ha_quic_add_handshake_data() to allocate the packet numbers and initialize the encryption level. Implement quic_pktns_release() which takes pointers to pointers to packet number space objects to release the memory allocated for a packet number space attached to a QUIC connection and reset their address values. Modify qc_new_conn() to allocation only the Initial packet number space and Initial encryption level. Modify QUIC loss detection API (quic_loss.c) to use the new ->pktns_list list attached to a QUIC connection in place of a static array of packet number spaces. Replace at several locations the use of elements of an array of packet number spaces by one of the three pointers to packet number spaces
2023-06-20 09:21:43 +00:00
pktns = p = LIST_NEXT(&qc->pktns_list, struct quic_pktns *, list);
MEDIUM: quic: Dynamic allocations of packet number spaces Add a pool to dynamically handle the memory used for the QUIC TLS packet number spaces. Remove the static array of packet number spaces at QUIC connection level (struct quic_conn) and add three new members to quic_conn struc as pointers to quic_pktns struct, one by packet number space as follows: ->ipktns for Initial packet number space, ->hpktns for Handshake packet number space and ->apktns for Application packet number space. Also add a ->pktns_list new member (struct list) to quic_conn struct to attach the list of the packet number spaces allocated for the QUIC connection. Implement ssl_to_quic_pktns() to map and retrieve the addresses of these pointers from TLS stack encryption levels. Modify quic_pktns_init() to initialize these members. Modify ha_quic_set_encryption_secrets() and ha_quic_add_handshake_data() to allocate the packet numbers and initialize the encryption level. Implement quic_pktns_release() which takes pointers to pointers to packet number space objects to release the memory allocated for a packet number space attached to a QUIC connection and reset their address values. Modify qc_new_conn() to allocation only the Initial packet number space and Initial encryption level. Modify QUIC loss detection API (quic_loss.c) to use the new ->pktns_list list attached to a QUIC connection in place of a static array of packet number spaces. Replace at several locations the use of elements of an array of packet number spaces by one of the three pointers to packet number spaces
2023-06-20 09:21:43 +00:00
do {
unsigned int tmp_pto;
MEDIUM: quic: Dynamic allocations of packet number spaces Add a pool to dynamically handle the memory used for the QUIC TLS packet number spaces. Remove the static array of packet number spaces at QUIC connection level (struct quic_conn) and add three new members to quic_conn struc as pointers to quic_pktns struct, one by packet number space as follows: ->ipktns for Initial packet number space, ->hpktns for Handshake packet number space and ->apktns for Application packet number space. Also add a ->pktns_list new member (struct list) to quic_conn struct to attach the list of the packet number spaces allocated for the QUIC connection. Implement ssl_to_quic_pktns() to map and retrieve the addresses of these pointers from TLS stack encryption levels. Modify quic_pktns_init() to initialize these members. Modify ha_quic_set_encryption_secrets() and ha_quic_add_handshake_data() to allocate the packet numbers and initialize the encryption level. Implement quic_pktns_release() which takes pointers to pointers to packet number space objects to release the memory allocated for a packet number space attached to a QUIC connection and reset their address values. Modify qc_new_conn() to allocation only the Initial packet number space and Initial encryption level. Modify QUIC loss detection API (quic_loss.c) to use the new ->pktns_list list attached to a QUIC connection in place of a static array of packet number spaces. Replace at several locations the use of elements of an array of packet number spaces by one of the three pointers to packet number spaces
2023-06-20 09:21:43 +00:00
if (p->tx.in_flight) {
if (p == qc->apktns) {
if (!handshake_confirmed) {
TRACE_STATE("TX PTO handshake not already confirmed", QUIC_EV_CONN_SPTO, qc);
goto out;
}
MEDIUM: quic: Dynamic allocations of packet number spaces Add a pool to dynamically handle the memory used for the QUIC TLS packet number spaces. Remove the static array of packet number spaces at QUIC connection level (struct quic_conn) and add three new members to quic_conn struc as pointers to quic_pktns struct, one by packet number space as follows: ->ipktns for Initial packet number space, ->hpktns for Handshake packet number space and ->apktns for Application packet number space. Also add a ->pktns_list new member (struct list) to quic_conn struct to attach the list of the packet number spaces allocated for the QUIC connection. Implement ssl_to_quic_pktns() to map and retrieve the addresses of these pointers from TLS stack encryption levels. Modify quic_pktns_init() to initialize these members. Modify ha_quic_set_encryption_secrets() and ha_quic_add_handshake_data() to allocate the packet numbers and initialize the encryption level. Implement quic_pktns_release() which takes pointers to pointers to packet number space objects to release the memory allocated for a packet number space attached to a QUIC connection and reset their address values. Modify qc_new_conn() to allocation only the Initial packet number space and Initial encryption level. Modify QUIC loss detection API (quic_loss.c) to use the new ->pktns_list list attached to a QUIC connection in place of a static array of packet number spaces. Replace at several locations the use of elements of an array of packet number spaces by one of the three pointers to packet number spaces
2023-06-20 09:21:43 +00:00
duration += qc->max_ack_delay << ql->pto_count;
}
tmp_pto = tick_add(p->tx.time_of_last_eliciting, duration);
if (!tick_isset(lpto) || tick_is_lt(tmp_pto, lpto)) {
lpto = tmp_pto;
pktns = p;
}
MEDIUM: quic: Dynamic allocations of packet number spaces Add a pool to dynamically handle the memory used for the QUIC TLS packet number spaces. Remove the static array of packet number spaces at QUIC connection level (struct quic_conn) and add three new members to quic_conn struc as pointers to quic_pktns struct, one by packet number space as follows: ->ipktns for Initial packet number space, ->hpktns for Handshake packet number space and ->apktns for Application packet number space. Also add a ->pktns_list new member (struct list) to quic_conn struct to attach the list of the packet number spaces allocated for the QUIC connection. Implement ssl_to_quic_pktns() to map and retrieve the addresses of these pointers from TLS stack encryption levels. Modify quic_pktns_init() to initialize these members. Modify ha_quic_set_encryption_secrets() and ha_quic_add_handshake_data() to allocate the packet numbers and initialize the encryption level. Implement quic_pktns_release() which takes pointers to pointers to packet number space objects to release the memory allocated for a packet number space attached to a QUIC connection and reset their address values. Modify qc_new_conn() to allocation only the Initial packet number space and Initial encryption level. Modify QUIC loss detection API (quic_loss.c) to use the new ->pktns_list list attached to a QUIC connection in place of a static array of packet number spaces. Replace at several locations the use of elements of an array of packet number spaces by one of the three pointers to packet number spaces
2023-06-20 09:21:43 +00:00
TRACE_PROTO("TX PTO", QUIC_EV_CONN_SPTO, qc, p);
}
MEDIUM: quic: Dynamic allocations of packet number spaces Add a pool to dynamically handle the memory used for the QUIC TLS packet number spaces. Remove the static array of packet number spaces at QUIC connection level (struct quic_conn) and add three new members to quic_conn struc as pointers to quic_pktns struct, one by packet number space as follows: ->ipktns for Initial packet number space, ->hpktns for Handshake packet number space and ->apktns for Application packet number space. Also add a ->pktns_list new member (struct list) to quic_conn struct to attach the list of the packet number spaces allocated for the QUIC connection. Implement ssl_to_quic_pktns() to map and retrieve the addresses of these pointers from TLS stack encryption levels. Modify quic_pktns_init() to initialize these members. Modify ha_quic_set_encryption_secrets() and ha_quic_add_handshake_data() to allocate the packet numbers and initialize the encryption level. Implement quic_pktns_release() which takes pointers to pointers to packet number space objects to release the memory allocated for a packet number space attached to a QUIC connection and reset their address values. Modify qc_new_conn() to allocation only the Initial packet number space and Initial encryption level. Modify QUIC loss detection API (quic_loss.c) to use the new ->pktns_list list attached to a QUIC connection in place of a static array of packet number spaces. Replace at several locations the use of elements of an array of packet number spaces by one of the three pointers to packet number spaces
2023-06-20 09:21:43 +00:00
p = LIST_NEXT(&p->list, struct quic_pktns *, list);
} while (&p->list != &qc->pktns_list);
out:
if (pto)
*pto = lpto;
TRACE_PROTO("TX PTO", QUIC_EV_CONN_SPTO, qc, pktns, &duration);
TRACE_LEAVE(QUIC_EV_CONN_SPTO, qc);
return pktns;
}
/* Look for packet loss from sent packets for <qel> encryption level of a
* connection with <ctx> as I/O handler context. If remove is true, remove them from
* their tree if deemed as lost or set the <loss_time> value the packet number
* space if any not deemed lost.
* Should be called after having received an ACK frame with newly acknowledged
* packets or when the the loss detection timer has expired.
* Always succeeds.
*/
void qc_packet_loss_lookup(struct quic_pktns *pktns, struct quic_conn *qc,
struct list *lost_pkts)
{
struct eb_root *pkts;
struct eb64_node *node;
struct quic_loss *ql;
unsigned int loss_delay;
TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc);
TRACE_PROTO("TX loss", QUIC_EV_CONN_PKTLOSS, qc, pktns);
pkts = &pktns->tx.pkts;
pktns->tx.loss_time = TICK_ETERNITY;
if (eb_is_empty(pkts))
goto out;
ql = &qc->path->loss;
loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY)) *
QUIC_LOSS_TIME_THRESHOLD_MULTIPLICAND / QUIC_LOSS_TIME_THRESHOLD_DIVISOR;
node = eb64_first(pkts);
while (node) {
struct quic_tx_packet *pkt;
int64_t largest_acked_pn;
unsigned int loss_time_limit, time_sent;
pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
largest_acked_pn = pktns->rx.largest_acked_pn;
node = eb64_next(node);
if ((int64_t)pkt->pn_node.key > largest_acked_pn)
break;
time_sent = pkt->time_sent;
loss_time_limit = tick_add(time_sent, loss_delay);
if (tick_is_le(loss_time_limit, now_ms) ||
(int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
eb64_delete(&pkt->pn_node);
LIST_APPEND(lost_pkts, &pkt->list);
ql->nb_lost_pkt++;
}
else {
if (tick_isset(pktns->tx.loss_time))
pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
else
pktns->tx.loss_time = loss_time_limit;
break;
}
}
out:
TRACE_PROTO("TX loss", QUIC_EV_CONN_PKTLOSS, qc, pktns, lost_pkts);
TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc);
}