LCOV - code coverage report
Current view: top level - waltz/quic - fd_quic_ack_tx.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 82 85 96.5 %
Date: 2025-01-08 12:08:44 Functions: 5 5 100.0 %

          Line data    Source code
       1             : #include "fd_quic_ack_tx.h"
       2             : #include "fd_quic_private.h"
       3             : 
       4             : #include "fd_quic_proto.h"
       5             : #include "fd_quic_proto.c"
       6             : 
       7             : static inline int
       8             : fd_quic_range_can_extend( fd_quic_range_t const * range,
       9    93670571 :                           ulong                   idx ) {
      10    93670571 :   return idx+1UL >= range->offset_lo && idx <= range->offset_hi;
      11    93670571 : }
      12             : 
      13             : static inline int
      14             : fd_quic_range_extend( fd_quic_range_t * range,
      15    93346316 :                       ulong             idx ) {
      16    93346316 :   int lo_decreased = idx <  range->offset_lo;
      17    93346316 :   int hi_increased = idx >= range->offset_hi;
      18    93346316 :   range->offset_lo = fd_ulong_min( range->offset_lo, idx );
      19    93346316 :   range->offset_hi = fd_ulong_max( range->offset_hi, idx+1UL );
      20    93346316 :   return lo_decreased || hi_increased;
      21    93346316 : }
      22             : 
      23             : int
      24             : fd_quic_ack_pkt( fd_quic_ack_gen_t * gen,
      25             :                  ulong               pkt_number,
      26             :                  uint                enc_level,
      27    93670574 :                  ulong               now ) {
      28             : 
      29    93670574 :   if( pkt_number == FD_QUIC_PKT_NUM_UNUSED ) return FD_QUIC_ACK_TX_NOOP;
      30             : 
      31             :   /* Can we merge pkt_number into the most recent ACK? */
      32    93670571 :   uint            cached_seq = gen->head - 1U;
      33    93670571 :   fd_quic_ack_t * cached_ack = fd_quic_ack_queue_ele( gen, cached_seq );
      34    93670571 :   if( ( enc_level == cached_ack->enc_level ) &
      35    93670571 :       fd_quic_range_can_extend( &cached_ack->pkt_number, pkt_number ) ) {
      36             : 
      37             :     /* update timestamp */
      38    93346316 :     if( pkt_number >= cached_ack->pkt_number.offset_hi ) {
      39    93346304 :       cached_ack->ts = now;
      40    93346304 :     }
      41             : 
      42             :     /* add packet number to existing range */
      43    93346316 :     int changed = fd_quic_range_extend( &cached_ack->pkt_number, pkt_number );
      44             : 
      45             :     /* re-enqueue most recent ACK for sending */
      46    93346316 :     if( changed && gen->head==gen->tail ) {
      47     1593758 :       gen->tail = cached_seq;
      48     1593758 :     }
      49             : 
      50    93346316 :     FD_ACK_DEBUG( FD_LOG_DEBUG(( "gen=%p queue ACK for enc=%u pkt_num=%lu range=[%lu,%lu) seq=%u (merged)",
      51    93346316 :         (void *)gen, enc_level, pkt_number, cached_ack->pkt_number.offset_lo, cached_ack->pkt_number.offset_hi, cached_seq )); )
      52    93346316 :     return FD_QUIC_ACK_TX_MERGED;
      53             : 
      54    93346316 :   }
      55             : 
      56             :   /* Attempt to allocate another ACK queue entry */
      57      324255 :   if( gen->head - gen->tail >= FD_QUIC_ACK_QUEUE_CNT ) {
      58           0 :     FD_DEBUG( FD_LOG_DEBUG(( "ACK queue overflow! (excessive reordering)" )); )
      59           0 :     return FD_QUIC_ACK_TX_ENOSPC;
      60           0 :   }
      61             : 
      62             :   /* Start new pending ACK */
      63      324255 :   FD_ACK_DEBUG( FD_LOG_DEBUG(( "gen=%p queue ACK for enc=%u pkt_num=%lu seq=%u",
      64      324255 :     (void *)gen, enc_level, pkt_number, gen->head )); )
      65      324255 :   fd_quic_ack_t * next_ack = fd_quic_ack_queue_ele( gen, gen->head );
      66      324255 :   *next_ack = (fd_quic_ack_t) {
      67      324255 :     .pkt_number = { .offset_lo = pkt_number, .offset_hi = pkt_number+1UL },
      68      324255 :     .enc_level  = (uchar)enc_level,
      69      324255 :     .ts         = now
      70      324255 :   };
      71      324255 :   gen->head += 1U;
      72      324255 :   return FD_QUIC_ACK_TX_NEW;
      73      324255 : }
      74             : 
      75             : void
      76             : fd_quic_ack_gen_abandon_enc_level( fd_quic_ack_gen_t * gen,
      77       24060 :                                    uint                enc_level ) {
      78       36090 :   for( ; gen->tail != gen->head; gen->tail++ ) {
      79       12030 :     fd_quic_ack_t const * ack = fd_quic_ack_queue_ele( gen, gen->tail );
      80       12030 :     if( ack->enc_level > enc_level ) break;
      81       12030 :     FD_DEBUG( FD_LOG_DEBUG(( "gen=%p discard ACK for enc=%u range=[%lu,%lu) seq=%u",
      82       12030 :         (void *)gen, enc_level, ack->pkt_number.offset_lo, ack->pkt_number.offset_hi, gen->tail )); )
      83       12030 :   }
      84       24060 : }
      85             : 
      86             : extern ulong
      87             : fd_quic_encode_ack_frame( uchar *               buf,
      88             :                           ulong                 sz,
      89             :                           fd_quic_ack_frame_t * frame );
      90             : 
      91             : uchar *
      92             : fd_quic_gen_ack_frames( fd_quic_ack_gen_t * gen,
      93             :                         uchar *             payload_ptr,
      94             :                         uchar *             payload_end,
      95             :                         uint                enc_level,
      96             :                         ulong               now,
      97    14363129 :                         float               tick_per_us ) {
      98             : 
      99    14363129 :   FD_ACK_DEBUG( FD_LOG_DEBUG(( "[ACK gen] elicited=%d", gen->is_elicited )); )
     100             :   /* Never generate an ACK frame if no ACK-eliciting packet is pending.
     101             :      This prevents an infinite ACK loop. */
     102    14363129 :   if( !gen->is_elicited ) return payload_ptr;
     103             : 
     104             :   /* Attempt to send all ACK ranges */
     105     3217596 :   for( ; gen->tail != gen->head; gen->tail++ ) {
     106     1605807 :     fd_quic_ack_t * ack = fd_quic_ack_queue_ele( gen, gen->tail );
     107     1605807 :     if( ack->enc_level != enc_level ) {
     108           6 :       FD_ACK_DEBUG( FD_LOG_DEBUG(( "need encryption level %u for ACKs but have %u", ack->enc_level, enc_level )); )
     109           6 :       break;
     110           6 :     }
     111             : 
     112     1605801 :     ulong ack_delay_ticks = fd_ulong_sat_sub( now, ack->ts );
     113     1605801 :     ulong ack_delay_us    = (ulong)( (float)ack_delay_ticks / tick_per_us );
     114             : 
     115     1605801 :     if( FD_UNLIKELY( ack->pkt_number.offset_lo == ack->pkt_number.offset_hi ) ) continue;
     116     1605801 :     fd_quic_ack_frame_t ack_frame = {
     117     1605801 :       .type            = 0x02, /* type 0x02 is the base ack, 0x03 indicates ECN */
     118     1605801 :       .largest_ack     = ack->pkt_number.offset_hi - 1U,
     119     1605801 :       .ack_delay       = ack_delay_us,
     120     1605801 :       .ack_range_count = 0, /* no fragments */
     121     1605801 :       .first_ack_range = ack->pkt_number.offset_hi - ack->pkt_number.offset_lo - 1U,
     122     1605801 :     };
     123     1605801 :     ulong frame_sz = fd_quic_encode_ack_frame( payload_ptr, (ulong)( payload_end - payload_ptr ), &ack_frame );
     124     1605801 :     if( FD_UNLIKELY( frame_sz==FD_QUIC_ENCODE_FAIL ) ) {
     125          18 :       FD_DEBUG( FD_LOG_DEBUG(( "insufficient buffer space to send ACK" )); )
     126          18 :       break;
     127          18 :     }
     128     1605783 :     payload_ptr += frame_sz;
     129     1605783 :     FD_ACK_DEBUG( FD_LOG_DEBUG(( "gen=%p sending ACK enc=%u range=[%lu,%lu) seq=%u sz=%lu",
     130     1605783 :         (void *)gen, enc_level, ack->pkt_number.offset_lo, ack->pkt_number.offset_hi, gen->tail, frame_sz )); )
     131     1605783 :   }
     132             : 
     133             :   /* If all frames were flushed, reset status bits. */
     134     1611813 :   if( gen->head == gen->tail ) {
     135     1611789 :     gen->is_elicited = 0;
     136     1611789 :   } else {
     137          24 :     FD_ACK_DEBUG( FD_LOG_DEBUG(( "Not all ACK frames were flushed" )); )
     138          24 :   }
     139             : 
     140     1611813 :   return payload_ptr;
     141    14363129 : }

Generated by: LCOV version 1.14