diff --git a/include/internal/time.h b/include/internal/time.h index 5cc55b3f51..f5c56b6c7b 100644 --- a/include/internal/time.h +++ b/include/internal/time.h @@ -98,18 +98,18 @@ int ossl_time_compare(OSSL_TIME a, OSSL_TIME b) return 0; } -/* Returns true if an OSSL_TIME is OSSL_TIME_ZERO. */ +/* Returns true if an OSSL_TIME is ossl_time_zero(). */ static ossl_unused ossl_inline int ossl_time_is_zero(OSSL_TIME t) { - return t == OSSL_TIME_ZERO; + return ossl_time_compare(t, ossl_time_zero()) == 0; } -/* Returns true if an OSSL_TIME is OSSL_TIME_INFINITY. */ +/* Returns true if an OSSL_TIME is ossl_time_infinite(). */ static ossl_unused ossl_inline -int ossl_time_is_infinity(OSSL_TIME t) +int ossl_time_is_infinite(OSSL_TIME t) { - return t == OSSL_TIME_INFINITY; + return ossl_time_compare(t, ossl_time_infinite()) == 0; } /* diff --git a/ssl/quic/quic_ackm.c b/ssl/quic/quic_ackm.c index 670b2d316e..dde882c649 100644 --- a/ssl/quic/quic_ackm.c +++ b/ssl/quic/quic_ackm.c @@ -1124,15 +1124,15 @@ static OSSL_ACKM_TX_PKT *ackm_detect_and_remove_lost_pkts(OSSL_ACKM *ackm, ossl_statm_get_rtt_info(ackm->statm, &rtt); - ackm->loss_time[pkt_space] = 0; + ackm->loss_time[pkt_space] = ossl_time_zero(); - loss_delay = ossl_time_multiply(K_TIME_THRESHOLD_NUM, - ossl_time_max(rtt.latest_rtt, - rtt.smoothed_rtt)); + loss_delay = ossl_time_multiply(ossl_time_max(rtt.latest_rtt, + rtt.smoothed_rtt), + K_TIME_THRESHOLD_NUM); loss_delay = ossl_time_divide(loss_delay, K_TIME_THRESHOLD_DEN); /* Minimum time of K_GRANULARITY before packets are deemed lost. */ - loss_delay = ossl_time_max(loss_delay, K_GRANULARITY); + loss_delay = ossl_time_max(loss_delay, ossl_ticks2time(K_GRANULARITY)); /* Packets sent before this time are deemed lost. */ now = ackm->now(ackm->now_arg); @@ -1198,15 +1198,15 @@ static OSSL_TIME ackm_get_pto_time_and_space(OSSL_ACKM *ackm, int *space) { OSSL_RTT_INFO rtt; OSSL_TIME duration; - OSSL_TIME pto_timeout = OSSL_TIME_INFINITY, t; + OSSL_TIME pto_timeout = ossl_time_infinite(), t; int pto_space = QUIC_PN_SPACE_INITIAL, i; ossl_statm_get_rtt_info(ackm->statm, &rtt); duration = ossl_time_add(rtt.smoothed_rtt, - ossl_time_max(ossl_time_multiply(4, rtt.rtt_variance), - K_GRANULARITY)); + ossl_time_max(ossl_time_multiply(rtt.rtt_variance, 4), + ossl_ticks2time(K_GRANULARITY))); duration = ossl_time_multiply(duration, 1U << min_u32(ackm->pto_count, @@ -1232,7 +1232,7 @@ static OSSL_TIME ackm_get_pto_time_and_space(OSSL_ACKM *ackm, int *space) break; /* Include max_ack_delay and backoff for app data. */ - if (!ossl_time_is_infinity(rtt.max_ack_delay)) + if (!ossl_time_is_infinite(rtt.max_ack_delay)) duration = ossl_time_add(duration, ossl_time_multiply(rtt.max_ack_delay, @@ -1241,7 +1241,7 @@ static OSSL_TIME ackm_get_pto_time_and_space(OSSL_ACKM *ackm, int *space) } t = ossl_time_add(ackm->time_of_last_ack_eliciting_pkt[i], duration); - if (t < pto_timeout) { + if (ossl_time_compare(t, pto_timeout) < 0) { pto_timeout = t; pto_space = i; } @@ -1280,7 +1280,7 @@ static int ackm_set_loss_detection_timer(OSSL_ACKM *ackm) * needs to arm the timer if the server might be blocked by the * anti-amplification limit. */ - ackm_set_loss_detection_timer_actual(ackm, OSSL_TIME_ZERO); + ackm_set_loss_detection_timer_actual(ackm, ossl_time_zero()); return 1; } @@ -1328,7 +1328,7 @@ static void ackm_on_pkts_lost(OSSL_ACKM *ackm, int pkt_space, */ ossl_statm_get_rtt_info(ackm->statm, &rtt); - if (ackm->first_rtt_sample == 0) + if (ossl_time_is_zero(ackm->first_rtt_sample)) return; ackm->cc_method->on_data_lost(ackm->cc_data, @@ -1391,7 +1391,7 @@ OSSL_ACKM *ossl_ackm_new(OSSL_TIME (*now)(void *arg), for (i = 0; i < (int)OSSL_NELEM(ackm->tx_history); ++i) { ackm->largest_acked_pkt[i] = QUIC_PN_INVALID; - ackm->rx_ack_flush_deadline[i] = OSSL_TIME_INFINITY; + ackm->rx_ack_flush_deadline[i] = ossl_time_infinite(); if (tx_pkt_history_init(&ackm->tx_history[i]) < 1) goto err; } @@ -1607,8 +1607,8 @@ int ossl_ackm_on_pkt_space_discarded(OSSL_ACKM *ackm, int pkt_space) ackm->cc_method->on_data_invalidated(ackm->cc_data, num_bytes_invalidated); - ackm->time_of_last_ack_eliciting_pkt[pkt_space] = OSSL_TIME_ZERO; - ackm->loss_time[pkt_space] = OSSL_TIME_ZERO; + ackm->time_of_last_ack_eliciting_pkt[pkt_space] = ossl_time_zero(); + ackm->loss_time[pkt_space] = ossl_time_zero(); ackm->pto_count = 0; ackm->discarded[pkt_space] = 1; ackm->ack_eliciting_bytes_in_flight[pkt_space] = 0; @@ -1713,7 +1713,7 @@ int ossl_ackm_get_largest_unacked(OSSL_ACKM *ackm, int pkt_space, QUIC_PN *pn) /* Number of ACK-eliciting packets RX'd before we always emit an ACK. */ #define PKTS_BEFORE_ACK 2 /* Maximum amount of time to leave an ACK-eliciting packet un-ACK'd. */ -#define MAX_ACK_DELAY (ossl_time_multiply(OSSL_TIME_MS, 25)) +#define MAX_ACK_DELAY (OSSL_TIME_MS * 25) /* * Return 1 if emission of an ACK frame is currently desired. @@ -1733,7 +1733,7 @@ int ossl_ackm_get_largest_unacked(OSSL_ACKM *ackm, int pkt_space, QUIC_PN *pn) int ossl_ackm_is_ack_desired(OSSL_ACKM *ackm, int pkt_space) { return ackm->rx_ack_desired[pkt_space] - || (!ossl_time_is_infinity(ackm->rx_ack_flush_deadline[pkt_space]) + || (!ossl_time_is_infinite(ackm->rx_ack_flush_deadline[pkt_space]) && ossl_time_compare(ackm->now(ackm->now_arg), ackm->rx_ack_flush_deadline[pkt_space]) >= 0); } @@ -1814,7 +1814,7 @@ static void ackm_queue_ack(OSSL_ACKM *ackm, int pkt_space) ackm->rx_ack_desired[pkt_space] = 1; /* Cancel deadline. */ - ackm_set_flush_deadline(ackm, pkt_space, OSSL_TIME_INFINITY); + ackm_set_flush_deadline(ackm, pkt_space, ossl_time_infinite()); } static void ackm_on_rx_ack_eliciting(OSSL_ACKM *ackm, @@ -1863,14 +1863,15 @@ static void ackm_on_rx_ack_eliciting(OSSL_ACKM *ackm, * * Update the ACK flush deadline. */ - if (ossl_time_is_infinity(ackm->rx_ack_flush_deadline[pkt_space])) + if (ossl_time_is_infinite(ackm->rx_ack_flush_deadline[pkt_space])) ackm_set_flush_deadline(ackm, pkt_space, - ossl_time_add(rx_time, MAX_ACK_DELAY)); + ossl_time_add(rx_time, + ossl_ticks2time(MAX_ACK_DELAY))); else ackm_set_flush_deadline(ackm, pkt_space, ossl_time_min(ackm->rx_ack_flush_deadline[pkt_space], ossl_time_add(rx_time, - MAX_ACK_DELAY))); + ossl_ticks2time(MAX_ACK_DELAY)))); } int ossl_ackm_on_rx_packet(OSSL_ACKM *ackm, const OSSL_ACKM_RX_PKT *pkt) @@ -1966,7 +1967,7 @@ const OSSL_QUIC_FRAME_ACK *ossl_ackm_get_ack_frame(OSSL_ACKM *ackm, ack->delay_time = ossl_time_subtract(now, ackm->rx_largest_time[pkt_space]); else - ack->delay_time = OSSL_TIME_ZERO; + ack->delay_time = ossl_time_zero(); ack->ect0 = ackm->rx_ect0[pkt_space]; ack->ect1 = ackm->rx_ect1[pkt_space]; @@ -1977,7 +1978,7 @@ const OSSL_QUIC_FRAME_ACK *ossl_ackm_get_ack_frame(OSSL_ACKM *ackm, ackm->rx_ack_generated[pkt_space] = 1; ackm->rx_ack_desired[pkt_space] = 0; - ackm_set_flush_deadline(ackm, pkt_space, OSSL_TIME_INFINITY); + ackm_set_flush_deadline(ackm, pkt_space, ossl_time_infinite()); return ack; } @@ -1986,7 +1987,7 @@ OSSL_TIME ossl_ackm_get_ack_deadline(OSSL_ACKM *ackm, int pkt_space) { if (ackm->rx_ack_desired[pkt_space]) /* Already desired, deadline is now. */ - return OSSL_TIME_ZERO; + return ossl_time_zero(); return ackm->rx_ack_flush_deadline[pkt_space]; } diff --git a/ssl/quic/quic_statm.c b/ssl/quic/quic_statm.c index 35f5722735..cf402036c5 100644 --- a/ssl/quic/quic_statm.c +++ b/ssl/quic/quic_statm.c @@ -42,24 +42,24 @@ void ossl_statm_update_rtt(OSSL_STATM *statm, if (ossl_time_compare(latest_rtt, ossl_time_add(statm->min_rtt, ack_delay)) >= 0) adjusted_rtt = ossl_time_subtract(latest_rtt, ack_delay); - statm->rtt_variance = ossl_time_divide(ossl_time_add(ossl_time_multiply(3, statm->rtt_variance), + statm->rtt_variance = ossl_time_divide(ossl_time_add(ossl_time_multiply(statm->rtt_variance, 3), ossl_time_abs_difference(statm->smoothed_rtt, adjusted_rtt)), 4); - statm->smoothed_rtt = ossl_time_divide(ossl_time_add(ossl_time_multiply(7, statm->smoothed_rtt), + statm->smoothed_rtt = ossl_time_divide(ossl_time_add(ossl_time_multiply(statm->smoothed_rtt, 7), adjusted_rtt), 8); } /* RFC 9002 kInitialRtt value. RFC recommended value. */ -#define K_INITIAL_RTT (ossl_time_multiply(OSSL_TIME_MS, 333)) +#define K_INITIAL_RTT (ossl_ticks2time(333 * OSSL_TIME_MS)) int ossl_statm_init(OSSL_STATM *statm) { statm->smoothed_rtt = K_INITIAL_RTT; - statm->latest_rtt = OSSL_TIME_ZERO; - statm->min_rtt = OSSL_TIME_INFINITY; + statm->latest_rtt = ossl_time_zero(); + statm->min_rtt = ossl_time_infinite(); statm->rtt_variance = ossl_time_divide(K_INITIAL_RTT, 2); statm->have_first_sample = 0; - statm->max_ack_delay = OSSL_TIME_INFINITY; + statm->max_ack_delay = ossl_time_infinite(); return 1; } diff --git a/test/quic_ackm_test.c b/test/quic_ackm_test.c index cea7c6fed9..83da7098bf 100644 --- a/test/quic_ackm_test.c +++ b/test/quic_ackm_test.c @@ -12,9 +12,9 @@ #include "internal/quic_ackm.h" #include "internal/quic_cc.h" -static OSSL_TIME fake_time = 0; +static OSSL_TIME fake_time = {0}; -#define TIME_BASE (123 * OSSL_TIME_SECOND) +#define TIME_BASE (ossl_ticks2time(123 * OSSL_TIME_SECOND)) static OSSL_TIME fake_now(void *arg) { @@ -341,7 +341,7 @@ static int test_tx_ack_case_actual(int tidx, int space, int mode) OSSL_ACKM_TX_PKT *tx; const struct tx_ack_test_case *c = tx_ack_cases[tidx]; OSSL_QUIC_FRAME_ACK ack = {0}; - OSSL_TIME loss_detection_deadline = OSSL_TIME_ZERO; + OSSL_TIME loss_detection_deadline = ossl_time_zero(); /* Cannot discard app space, so skip this */ if (mode == MODE_DISCARD && space == QUIC_PN_SPACE_APP) { @@ -417,11 +417,11 @@ static int test_tx_ack_case_actual(int tidx, int space, int mode) OSSL_TIME deadline = ossl_ackm_get_loss_detection_deadline(h.ackm); OSSL_ACKM_PROBE_INFO probe = {0}; - if (!TEST_true(deadline == loss_detection_deadline)) + if (!TEST_int_eq(ossl_time_compare(deadline, loss_detection_deadline), 0)) goto err; /* We should have a PTO deadline. */ - if (!TEST_true(deadline > fake_time)) + if (!TEST_int_gt(ossl_time_compare(deadline, fake_time), 0)) goto err; /* Should not have any probe requests yet. */ @@ -440,7 +440,7 @@ static int test_tx_ack_case_actual(int tidx, int space, int mode) goto err; /* Advance to the PTO deadline. */ - fake_time = deadline + 1; + fake_time = ossl_time_add(deadline, ossl_ticks2time(1)); if (!TEST_int_eq(ossl_ackm_on_timeout(h.ackm), 1)) goto err; @@ -484,7 +484,7 @@ enum { struct tx_ack_time_op { int kind; - OSSL_TIME time_advance; /* all ops */ + uint64_t time_advance; /* all ops */ QUIC_PN pn; /* PKT, ACK */ size_t num_pn; /* PKT, ACK */ const char *expect; /* 1=ack, 2=lost, 4=discarded */ @@ -557,7 +557,8 @@ static int test_tx_ack_time_script(int tidx) tx->on_discarded = on_discarded; tx->cb_arg = &h.pkts[pkt_idx + i]; - fake_time += s->time_advance; + fake_time = ossl_time_add(fake_time, + ossl_ticks2time(s->time_advance)); tx->time = fake_time; if (!TEST_int_eq(ossl_ackm_on_tx_packet(h.ackm, tx), 1)) @@ -574,7 +575,8 @@ static int test_tx_ack_time_script(int tidx) ack_range.start = s->pn; ack_range.end = s->pn + s->num_pn; - fake_time += s->time_advance; + fake_time = ossl_time_add(fake_time, + ossl_ticks2time(s->time_advance)); if (!TEST_int_eq(ossl_ackm_on_rx_ack_frame(h.ackm, &ack, QUIC_PN_SPACE_INITIAL, @@ -622,7 +624,7 @@ enum { struct rx_test_op { int kind; - OSSL_TIME time_advance; + uint64_t time_advance; QUIC_PN pn; /* PKT, CHECK_(UN)PROC, TX, RX_ACK */ size_t num_pn; /* PKT, CHECK_(UN)PROC, TX, RX_ACK */ @@ -848,9 +850,10 @@ static int test_rx_ack_actual(int tidx, int space) OSSL_QUIC_ACK_RANGE rx_ack_range = {0}; struct pkt_info *pkts = NULL; OSSL_ACKM_TX_PKT *txs = NULL, *tx; - OSSL_TIME ack_deadline[QUIC_PN_SPACE_NUM] = { - OSSL_TIME_INFINITY, OSSL_TIME_INFINITY, OSSL_TIME_INFINITY - }; + OSSL_TIME ack_deadline[QUIC_PN_SPACE_NUM]; + + for (i = 0; i < QUIC_PN_SPACE_NUM; ++i) + ack_deadline[i] = ossl_time_infinite(); /* Initialise ACK manager. */ if (!TEST_int_eq(helper_init(&h, 0), 1)) @@ -879,7 +882,8 @@ static int test_rx_ack_actual(int tidx, int space) /* Run script. */ for (s = script; s->kind != RX_OPK_END; ++s) { - fake_time += s->time_advance; + fake_time = ossl_time_add(fake_time, + ossl_ticks2time(s->time_advance)); switch (s->kind) { case RX_OPK_PKT: for (i = 0; i < s->num_pn; ++i) { @@ -917,19 +921,18 @@ static int test_rx_ack_actual(int tidx, int space) s->expect_desired)) goto err; - if (!TEST_int_eq(!ossl_time_is_infinity(ossl_ackm_get_ack_deadline(h.ackm, space)) + if (!TEST_int_eq(!ossl_time_is_infinite(ossl_ackm_get_ack_deadline(h.ackm, space)) && !ossl_time_is_zero(ossl_ackm_get_ack_deadline(h.ackm, space)), s->expect_deadline)) goto err; for (i = 0; i < QUIC_PN_SPACE_NUM; ++i) { if (i != (size_t)space - && !TEST_true(ossl_ackm_get_ack_deadline(h.ackm, i) - == OSSL_TIME_INFINITY)) + && !TEST_true(ossl_time_is_infinite(ossl_ackm_get_ack_deadline(h.ackm, i)))) goto err; - if (!TEST_true(ossl_ackm_get_ack_deadline(h.ackm, i) - == ack_deadline[i])) + if (!TEST_int_eq(ossl_time_compare(ossl_ackm_get_ack_deadline(h.ackm, i), + ack_deadline[i]), 0)) goto err; }