LCOV - code coverage report
Current view: top level - waltz/quic - fd_quic_ack_tx.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 79 82 96.3 %
Date: 2024-11-13 11:58:15 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    43362394 :                           ulong                   idx ) {
      10    43362394 :   return idx+1UL >= range->offset_lo && idx <= range->offset_hi;
      11    43362394 : }
      12             : 
      13             : static inline int
      14             : fd_quic_range_extend( fd_quic_range_t * range,
      15    43044296 :                       ulong             idx ) {
      16    43044296 :   int lo_decreased = idx <  range->offset_lo;
      17    43044296 :   int hi_increased = idx >= range->offset_hi;
      18    43044296 :   range->offset_lo = fd_ulong_min( range->offset_lo, idx );
      19    43044296 :   range->offset_hi = fd_ulong_max( range->offset_hi, idx+1UL );
      20    43044296 :   return lo_decreased || hi_increased;
      21    43044296 : }
      22             : 
      23             : void
      24             : fd_quic_ack_pkt( fd_quic_ack_gen_t * gen,
      25             :                  ulong               pkt_number,
      26             :                  uint                enc_level,
      27    43362397 :                  ulong               now ) {
      28             : 
      29    43362397 :   if( pkt_number == FD_QUIC_PKT_NUM_UNUSED ) return;
      30             : 
      31             :   /* Can we merge pkt_number into the most recent ACK? */
      32    43362394 :   uint            cached_seq = gen->head - 1U;
      33    43362394 :   fd_quic_ack_t * cached_ack = fd_quic_ack_queue_ele( gen, cached_seq );
      34    43362394 :   if( ( enc_level == cached_ack->enc_level ) &
      35    43362394 :       fd_quic_range_can_extend( &cached_ack->pkt_number, pkt_number ) ) {
      36             : 
      37             :     /* update timestamp */
      38    43044296 :     if( pkt_number >= cached_ack->pkt_number.offset_hi ) {
      39    43043498 :       cached_ack->ts = now;
      40    43043498 :     }
      41             : 
      42             :     /* add packet number to existing range */
      43    43044296 :     int changed = fd_quic_range_extend( &cached_ack->pkt_number, pkt_number );
      44             : 
      45             :     /* re-enqueue most recent ACK for sending */
      46    43044296 :     if( changed && gen->head==gen->tail ) {
      47     3095056 :       gen->tail = cached_seq;
      48     3095056 :     }
      49             : 
      50    43044296 :     FD_ACK_DEBUG( FD_LOG_DEBUG(( "gen=%p queue ACK for enc=%u pkt_num=%lu range=[%lu,%lu) seq=%u (merged)",
      51    43044296 :         (void *)gen, enc_level, pkt_number, cached_ack->pkt_number.offset_lo, cached_ack->pkt_number.offset_hi, cached_seq )); )
      52    43044296 :     return;
      53             : 
      54    43044296 :   }
      55             : 
      56             :   /* Attempt to allocate another ACK queue entry */
      57      318098 :   if( gen->head - gen->tail >= FD_QUIC_ACK_QUEUE_CNT ) {
      58           0 :     FD_DEBUG( FD_LOG_DEBUG(( "ACK queue overflow! (excessive reordering)" )); )
      59             :     /* FIXME count to metrics */
      60           0 :     return;
      61           0 :   }
      62             : 
      63             :   /* Start new pending ACK */
      64      318098 :   FD_ACK_DEBUG( FD_LOG_DEBUG(( "gen=%p queue ACK for enc=%u pkt_num=%lu seq=%u",
      65      318098 :     (void *)gen, enc_level, pkt_number, gen->head )); )
      66      318098 :   fd_quic_ack_t * next_ack = fd_quic_ack_queue_ele( gen, gen->head );
      67      318098 :   *next_ack = (fd_quic_ack_t) {
      68      318098 :     .pkt_number = { .offset_lo = pkt_number, .offset_hi = pkt_number+1UL },
      69      318098 :     .enc_level  = (uchar)enc_level,
      70      318098 :     .ts         = now
      71      318098 :   };
      72      318098 :   gen->head += 1U;
      73      318098 : }
      74             : 
      75             : void
      76             : fd_quic_ack_gen_abandon_enc_level( fd_quic_ack_gen_t * gen,
      77       24114 :                                    uint                enc_level ) {
      78       36150 :   for( ; gen->tail != gen->head; gen->tail++ ) {
      79       12036 :     fd_quic_ack_t const * ack = fd_quic_ack_queue_ele( gen, gen->tail );
      80       12036 :     if( ack->enc_level > enc_level ) break;
      81       12036 :     FD_DEBUG( FD_LOG_DEBUG(( "gen=%p discard ACK for enc=%u range=[%lu,%lu) seq=%u",
      82       12036 :         (void *)gen, enc_level, ack->pkt_number.offset_lo, ack->pkt_number.offset_hi, gen->tail )); )
      83       12036 :   }
      84       24114 : }
      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    19418939 :                         ulong               now ) {
      97             : 
      98    19418939 :   FD_ACK_DEBUG( FD_LOG_DEBUG(( "[ACK gen] elicited=%d", gen->is_elicited )); )
      99             :   /* Never generate an ACK frame if no ACK-eliciting packet is pending.
     100             :      This prevents an infinite ACK loop. */
     101    19418939 :   if( !gen->is_elicited ) return payload_ptr;
     102             : 
     103             :   /* Attempt to send all ACK ranges */
     104     6672383 :   for( ; gen->tail != gen->head; gen->tail++ ) {
     105     3333202 :     fd_quic_ack_t * ack = fd_quic_ack_queue_ele( gen, gen->tail );
     106     3333202 :     if( ack->enc_level != enc_level ) {
     107           6 :       FD_ACK_DEBUG( FD_LOG_DEBUG(( "need encryption level %u for ACKs but have %u", ack->enc_level, enc_level )); )
     108           6 :       break;
     109           6 :     }
     110             : 
     111     3333196 :     if( FD_UNLIKELY( ack->pkt_number.offset_lo == ack->pkt_number.offset_hi ) ) continue;
     112     3333196 :     fd_quic_ack_frame_t ack_frame = {
     113     3333196 :       .type            = 0x02, /* type 0x02 is the base ack, 0x03 indicates ECN */
     114     3333196 :       .largest_ack     = ack->pkt_number.offset_hi - 1U,
     115     3333196 :       .ack_delay       = fd_ulong_sat_sub( now, ack->ts ) / 1000, /* microseconds */
     116     3333196 :       .ack_range_count = 0, /* no fragments */
     117     3333196 :       .first_ack_range = ack->pkt_number.offset_hi - ack->pkt_number.offset_lo - 1U,
     118     3333196 :     };
     119     3333196 :     ulong frame_sz = fd_quic_encode_ack_frame( payload_ptr, (ulong)( payload_end - payload_ptr ), &ack_frame );
     120     3333196 :     if( FD_UNLIKELY( frame_sz==FD_QUIC_ENCODE_FAIL ) ) {
     121          18 :       FD_DEBUG( FD_LOG_DEBUG(( "insufficient buffer space to send ACK" )); )
     122          18 :       break;
     123          18 :     }
     124     3333178 :     payload_ptr += frame_sz;
     125     3333178 :     FD_ACK_DEBUG( FD_LOG_DEBUG(( "gen=%p sending ACK enc=%u range=[%lu,%lu) seq=%u sz=%lu",
     126     3333178 :         (void *)gen, enc_level, ack->pkt_number.offset_lo, ack->pkt_number.offset_hi, gen->tail, frame_sz )); )
     127     3333178 :   }
     128             : 
     129             :   /* If all frames were flushed, reset status bits. */
     130     3339205 :   if( gen->head == gen->tail ) {
     131     3339181 :     gen->is_elicited = 0;
     132     3339181 :   } else {
     133          24 :     FD_ACK_DEBUG( FD_LOG_DEBUG(( "Not all ACK frames were flushed" )); )
     134          24 :   }
     135             : 
     136     3339205 :   return payload_ptr;
     137    19418939 : }

Generated by: LCOV version 1.14