LCOV - code coverage report
Current view: top level - disco - fd_disco_base.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 97 0.0 %
Date: 2025-07-09 04:58:18 Functions: 0 7807 0.0 %

          Line data    Source code
       1             : #ifndef HEADER_fd_src_disco_fd_disco_base_h
       2             : #define HEADER_fd_src_disco_fd_disco_base_h
       3             : 
       4             : #include "../tango/fd_tango.h"
       5             : #include "../ballet/shred/fd_shred.h"
       6             : #include "../ballet/txn/fd_txn.h"
       7             : 
       8             : #include "../util/wksp/fd_wksp_private.h"
       9             : 
      10           0 : #define DST_PROTO_OUTGOING (0UL)
      11           0 : #define DST_PROTO_TPU_UDP  (1UL)
      12           0 : #define DST_PROTO_TPU_QUIC (2UL)
      13           0 : #define DST_PROTO_SHRED    (3UL)
      14           0 : #define DST_PROTO_REPAIR   (4UL)
      15           0 : #define DST_PROTO_GOSSIP   (5UL)
      16           0 : #define DST_PROTO_SEND     (6UL)
      17             : 
      18           0 : #define POH_PKT_TYPE_MICROBLOCK    (0UL)
      19           0 : #define POH_PKT_TYPE_BECAME_LEADER (1UL)
      20           0 : #define POH_PKT_TYPE_FEAT_ACT_SLOT (2UL)
      21             : 
      22             : #define REPLAY_FLAG_FINISHED_BLOCK      (0x01UL)
      23           0 : #define REPLAY_FLAG_PACKED_MICROBLOCK   (0x02UL)
      24             : #define REPLAY_FLAG_MICROBLOCK          (0x04UL)
      25             : #define REPLAY_FLAG_CATCHING_UP         (0x08UL)
      26           0 : #define REPLAY_FLAG_INIT                (0x10UL)
      27             : 
      28           0 : #define EXEC_FLAG_READY_NEW             (0x20UL)
      29           0 : #define EXEC_FLAG_EXECUTING_SLICE       (0x40UL)
      30           0 : #define EXEC_FLAG_FINISHED_SLOT         (0x80UL)
      31             : 
      32             : /* FD_NET_MTU is the max full packet size, with ethernet, IP, and UDP
      33             :    headers that can go in or out of the net tile.  2048 is the maximum
      34             :    XSK entry size, so this value follows naturally. */
      35             : 
      36           0 : #define FD_NET_MTU (2048UL)
      37             : 
      38             : /* FD_TPU_MTU is the max serialized byte size of a txn sent over TPU.
      39             : 
      40             :    This is minimum MTU of IPv6 packet - IPv6 header - UDP header
      41             :                                  1280 -          40 -          8 */
      42             : 
      43           0 : #define FD_TPU_MTU (1232UL)
      44             : 
      45             : /* FD_GOSSIP_MTU is the max sz of a gossip packet which is the same as
      46             :    above. */
      47             : 
      48             : #define FD_GOSSIP_MTU (FD_TPU_MTU)
      49             : 
      50             : /* FD_SHRED_STORE_MTU is the size of an fd_shred34_t (statically
      51             :    asserted in fd_shred_tile.c). */
      52             : 
      53           0 : #define FD_SHRED_STORE_MTU (41792UL)
      54             : 
      55             : /* FD_SHRED_REPAIR_MTU is the maximum size of a frag on the shred_repair
      56             :    link.  This is the size of a data shred header + merkle root. */
      57             : 
      58           0 : #define FD_SHRED_REPAIR_MTU (FD_SHRED_DATA_HEADER_SZ + FD_SHRED_MERKLE_ROOT_SZ)
      59             : FD_STATIC_ASSERT( FD_SHRED_REPAIR_MTU == 120 , update FD_SHRED_REPAIR_MTU );
      60             : 
      61             : /* Maximum size of frags going into the writer tile. */
      62             : #define FD_REPLAY_WRITER_MTU (128UL)
      63           0 : #define FD_EXEC_WRITER_MTU   (128UL)
      64             : 
      65             : 
      66           0 : #define FD_NETMUX_SIG_MIN_HDR_SZ    ( 42UL) /* The default header size, which means no vlan tags and no IP options. */
      67             : #define FD_NETMUX_SIG_IGNORE_HDR_SZ (102UL) /* Outside the allowable range, but still fits in 4 bits when compressed */
      68             : 
      69             : FD_PROTOTYPES_BEGIN
      70             : 
      71             :  /* hdr_sz is the total size of network headers, including eth, ip, udp.
      72             :     Ignored for outgoing packets.
      73             :     For incoming packets, hash_{ip_addr,port} are the source IP and port,
      74             :     for outgoing packets, they are the destination IP and port. */
      75             : FD_FN_CONST static inline ulong
      76             : fd_disco_netmux_sig( uint   hash_ip_addr,
      77             :                      ushort hash_port,
      78             :                      uint   dst_ip_addr,
      79             :                      ulong  proto,
      80           0 :                      ulong  hdr_sz ) {
      81             :   /* The size of an Ethernet header is 14+4k bytes, where 0<=k<=3 (?) is
      82             :      the number of vlan tags.  The size of an IP header is 4j, where
      83             :      5<=j<=15 is the size given in the header.  The size of a UDP header
      84             :      is 8B.  Thus, the total sum of these is 42+4i, where i=k+j-5,
      85             :      0<=i<=13.  Since bits are at a premium here, we compress the header
      86             :      size by just storing i. */
      87           0 :   ulong hdr_sz_i = ((hdr_sz - 42UL)>>2)&0xFUL;
      88           0 :   ulong hash     = 0xfffffUL & fd_ulong_hash( (ulong)hash_ip_addr | ((ulong)hash_port<<32) );
      89           0 :   return (hash<<44) | ((hdr_sz_i&0xFUL)<<40UL) | ((proto&0xFFUL)<<32UL) | ((ulong)dst_ip_addr);
      90           0 : }
      91             : 
      92           0 : FD_FN_CONST static inline ulong fd_disco_netmux_sig_hash  ( ulong sig ) { return (sig>>44UL); }
      93           0 : FD_FN_CONST static inline ulong fd_disco_netmux_sig_proto ( ulong sig ) { return (sig>>32UL) & 0xFFUL; }
      94           0 : FD_FN_CONST static inline uint  fd_disco_netmux_sig_dst_ip( ulong sig ) { return (uint)(sig & 0xFFFFFFFFUL); }
      95             : 
      96             : /* fd_disco_netmux_sig_hdr_sz extracts the total size of the Ethernet,
      97             :    IP, and UDP headers from the netmux signature field.  The UDP payload
      98             :    of the packet stored in the corresponding frag begins at the returned
      99             :    offset. */
     100           0 : FD_FN_CONST static inline ulong  fd_disco_netmux_sig_hdr_sz( ulong sig ) { return 4UL*((sig>>40UL) & 0xFUL) + 42UL; }
     101             : 
     102             : FD_FN_CONST static inline ulong
     103             : fd_disco_poh_sig( ulong slot,
     104             :                   ulong pkt_type,
     105           0 :                   ulong bank_tile ) {
     106             :    /* The high 6 bits of the low byte of the signature field is the bank
     107             :       idx.  Banks will filter to only handle frags with their own idx.
     108             :       The higher 7 bytes are the slot number.  Technically, the slot
     109             :       number is a ulong, but it won't hit 256^7 for about 10^9 years at
     110             :       the current rate.  The lowest bits of the low byte is the packet
     111             :       type. */
     112           0 :   return (slot << 8) | ((bank_tile & 0x3FUL) << 2) | (pkt_type & 0x3UL);
     113           0 : }
     114             : 
     115           0 : FD_FN_CONST static inline ulong fd_disco_poh_sig_pkt_type( ulong sig ) { return (sig & 0x3UL); }
     116           0 : FD_FN_CONST static inline ulong fd_disco_poh_sig_slot( ulong sig ) { return (sig >> 8); }
     117           0 : FD_FN_CONST static inline ulong fd_disco_poh_sig_bank_tile( ulong sig ) { return (sig >> 2) & 0x3FUL; }
     118             : 
     119             : FD_FN_CONST static inline ulong
     120             : fd_disco_bank_sig( ulong slot,
     121           0 :                    ulong pack_idx ) {
     122           0 :   return (slot << 32) | pack_idx;
     123           0 : }
     124             : 
     125           0 : FD_FN_CONST static inline ulong fd_disco_bank_sig_slot( ulong sig ) { return (sig >> 32); }
     126           0 : FD_FN_CONST static inline ulong fd_disco_bank_sig_pack_idx( ulong sig ) { return sig & 0xFFFFFFFFUL; }
     127             : 
     128             : /* TODO remove with store_int */
     129             : 
     130             : FD_FN_CONST static inline ulong
     131             : fd_disco_replay_old_sig( ulong slot,
     132           0 :                      ulong flags ) {
     133             :    /* The low byte of the signature field is the flags for replay message.
     134             :       The higher 7 bytes are the slot number.  These flags indicate the status
     135             :       of a microblock as it transits through the replay system.  Technically,
     136             :       the slot number is a ulong, but it won't hit 256^7 for about 10^9 years
     137             :       at the current rate.  The lowest bit of the low byte is the packet
     138             :       type. */
     139           0 :   return (slot << 8) | (flags & 0xFFUL);
     140           0 : }
     141             : 
     142           0 : FD_FN_CONST static inline ulong fd_disco_replay_old_sig_flags( ulong sig ) { return (sig & 0xFFUL); }
     143           0 : FD_FN_CONST static inline ulong fd_disco_replay_old_sig_slot( ulong sig ) { return (sig >> 8); }
     144             : 
     145             : /* fd_disco_shred_repair_shred_sig constructs a sig for the shred_repair link.
     146             :    The encoded fields vary depending on the type of the sig.  The
     147             :    diagram below describes the encoding.
     148             : 
     149             :    completes (1) | slot (32) | fec_set_idx (15) | is_code (1) | shred_idx or data_cnt (15)
     150             :    [63]          | [31, 62]  | [16, 30]         | [15]        | [0, 14]
     151             : 
     152             :    There are two types of messages on the shred_repair link.  The first
     153             :    type is a generic shred message. The second is a FEC set completion
     154             :    message. Since we have run out of bits, the reciever must look at the
     155             :    sz of the dcache entry to determine which type of message it is.
     156             : 
     157             :    For the first message type (SHRED):
     158             : 
     159             :    The first bit [63] describes whether this shred marks the end of a
     160             :    batch and/or a slot, i.e. shred.flags & (DATA_COMPLETE | SLOT_COMPLETE)
     161             : 
     162             :    The next 32 bits [31, 62] describe the slot number. Note: if the slot
     163             :    number is >= UINT_MAX, the sender will store the value UINT_MAX in
     164             :    this field. If the receiver sees a value of UINT_MAX in the field, it
     165             :    must read the actual slot number from the dcache entry.
     166             : 
     167             :    The following 15 bits [16, 30] describe the fec_set_idx.  This is a
     168             :    15-bit value because shreds are bounded to 2^15 per slot, so in the
     169             :    worst case there is an independent FEC set for every shred, which
     170             :    results in at most 2^15 FEC sets per slot.
     171             : 
     172             :    The next bit [15] describes whether it is a coding shred (is_code).
     173             :    If is_code = 0, the sig describes a data shred, and the last 15 bits
     174             :    [0, 14] encode the shred_idx.  If is_code = 1, the sig describes a
     175             :    coding shred, and the last 15 bits encode the data_cnt.
     176             : 
     177             :    For the second message type (FEC):
     178             : 
     179             :    Only the slot and fec_set_idx bits are populated. The data in the
     180             :    frag is the full shred header of the last data shred in the FEC set,
     181             :    the merkle root of the FEC set, and the chained merkle root of the
     182             :    FEC. Each field immediately follows the other field. */
     183             : 
     184             : /* TODO this shred_repair_sig can be greatly simplified when FEC sets
     185             :    are uniformly coding shreds and fixed size. */
     186             : 
     187             : FD_FN_CONST static inline ulong
     188             : fd_disco_shred_repair_shred_sig( int   completes,
     189             :                                  ulong slot,
     190             :                                  uint  fec_set_idx,
     191             :                                  int   is_code,
     192           0 :                                  uint  shred_idx_or_data_cnt ) {
     193           0 :    ulong slot_ul                  = fd_ulong_min( slot, (ulong)UINT_MAX );
     194           0 :    ulong shred_idx_or_data_cnt_ul = fd_ulong_min( (ulong)shred_idx_or_data_cnt, (ulong)FD_SHRED_BLK_MAX );
     195           0 :    ulong fec_set_idx_ul           = fd_ulong_min( (ulong)fec_set_idx, (ulong)FD_SHRED_BLK_MAX );
     196           0 :    ulong completes_ul             = !!completes;
     197           0 :    ulong is_code_ul               = !!is_code;
     198             : 
     199           0 :   return completes_ul << 63 | slot_ul << 31 | fec_set_idx_ul << 16 | is_code_ul << 15 | shred_idx_or_data_cnt_ul;
     200           0 : }
     201             : 
     202             : /* fd_disco_shred_repair_shred_sig_{...} are accessors for the fields encoded
     203             :    in the sig described above. */
     204             : 
     205           0 : FD_FN_CONST static inline int   fd_disco_shred_repair_shred_sig_completes  ( ulong sig ) { return       fd_ulong_extract_bit( sig, 63     ); }
     206           0 : FD_FN_CONST static inline ulong fd_disco_shred_repair_shred_sig_slot       ( ulong sig ) { return       fd_ulong_extract    ( sig, 31, 62 ); }
     207           0 : FD_FN_CONST static inline uint  fd_disco_shred_repair_shred_sig_fec_set_idx( ulong sig ) { return (uint)fd_ulong_extract    ( sig, 16, 30 ); }
     208           0 : FD_FN_CONST static inline int   fd_disco_shred_repair_shred_sig_is_code    ( ulong sig ) { return       fd_ulong_extract_bit( sig, 15     ); }
     209           0 : FD_FN_CONST static inline uint  fd_disco_shred_repair_shred_sig_shred_idx  ( ulong sig ) { return (uint)fd_ulong_extract_lsb( sig, 15     ); } /* only when is_code = 0 */
     210           0 : FD_FN_CONST static inline uint  fd_disco_shred_repair_shred_sig_data_cnt   ( ulong sig ) { return (uint)fd_ulong_extract_lsb( sig, 15     ); } /* only when is_code = 1 */
     211             : 
     212             : /*
     213             :    | slot (32) | fec_set_idx (15) | data_cnt (15) | is_data_complete (1) | is_batch_complete (1) |
     214             :    | [32, 63]  | [17, 31]         | [2, 16]       | [1]                  | [0]                   |
     215             : 
     216             : */
     217             : FD_FN_CONST static inline ulong
     218           0 : fd_disco_shred_repair_fec_sig( ulong slot, uint fec_set_idx, uint data_cnt, int is_slot_complete, int is_batch_complete ) {
     219           0 :   ulong slot_ul          = fd_ulong_min( slot, (ulong)UINT_MAX );
     220           0 :   ulong fec_set_idx_ul   = fd_ulong_min( (ulong)fec_set_idx, (ulong)FD_SHRED_BLK_MAX );
     221           0 :   ulong data_cnt_ul      = fd_ulong_min( (ulong)data_cnt, (ulong)FD_SHRED_BLK_MAX );
     222           0 :   ulong is_slot_complete_ul = !!is_slot_complete;
     223           0 :   ulong is_batch_complete_ul = !!is_batch_complete;
     224           0 :   return slot_ul << 32 | fec_set_idx_ul << 17 | data_cnt_ul << 2 | is_slot_complete_ul << 1 | is_batch_complete_ul;
     225           0 : }
     226             : 
     227           0 : FD_FN_CONST static inline ulong fd_disco_shred_repair_fec_sig_slot             ( ulong sig ) { return         fd_ulong_extract    ( sig, 32, 63 ); }
     228           0 : FD_FN_CONST static inline uint  fd_disco_shred_repair_fec_sig_fec_set_idx      ( ulong sig ) { return (uint)  fd_ulong_extract    ( sig, 17, 31 ); }
     229           0 : FD_FN_CONST static inline uint  fd_disco_shred_repair_fec_sig_data_cnt         ( ulong sig ) { return (uint)  fd_ulong_extract    ( sig, 2, 16  ); }
     230           0 : FD_FN_CONST static inline int   fd_disco_shred_repair_fec_sig_is_slot_complete ( ulong sig ) { return         fd_ulong_extract_bit( sig, 1     ); }
     231           0 : FD_FN_CONST static inline int   fd_disco_shred_repair_fec_sig_is_batch_complete( ulong sig ) { return         fd_ulong_extract_bit( sig, 0     ); }
     232             : 
     233             : /* Exclusively used for force completion messages */
     234             : 
     235             : FD_FN_CONST static inline ulong
     236           0 : fd_disco_repair_shred_sig( uint last_shred_idx ){
     237           0 :    return (ulong) last_shred_idx;
     238           0 : }
     239             : 
     240           0 : FD_FN_CONST static inline uint fd_disco_repair_shred_sig_last_shred_idx( ulong sig ) { return (uint) sig; }
     241             : 
     242             : 
     243             : FD_FN_CONST static inline ulong
     244           0 : fd_disco_repair_replay_sig( ulong slot, ushort parent_off, uint data_cnt, int slot_complete ) {
     245             :   /*
     246             :    | slot (32) | parent_off (16) | data_cnt (15) | slot_complete(1)
     247             :    | [32, 63]  | [16, 31]        | [1, 15]       | [0]
     248             :   */
     249           0 :   ulong slot_ul          = fd_ulong_min( slot, (ulong)UINT_MAX );
     250           0 :   ulong parent_off_ul    = (ulong)parent_off;
     251           0 :   ulong data_cnt_ul      = fd_ulong_min( (ulong)data_cnt, (ulong)FD_SHRED_BLK_MAX );
     252           0 :   ulong slot_complete_ul = !!slot_complete;
     253           0 :   return slot_ul << 32 | parent_off_ul << 16 | data_cnt_ul << 1 | slot_complete_ul;
     254           0 : }
     255             : 
     256           0 : FD_FN_CONST static inline ulong  fd_disco_repair_replay_sig_slot         ( ulong sig ) { return         fd_ulong_extract    ( sig, 32, 63 ); }
     257           0 : FD_FN_CONST static inline ushort fd_disco_repair_replay_sig_parent_off   ( ulong sig ) { return (ushort)fd_ulong_extract    ( sig, 16, 31 ); }
     258           0 : FD_FN_CONST static inline uint   fd_disco_repair_replay_sig_data_cnt     ( ulong sig ) { return (uint)  fd_ulong_extract    ( sig, 1,  15 ); }
     259           0 : FD_FN_CONST static inline int    fd_disco_repair_replay_sig_slot_complete( ulong sig ) { return         fd_ulong_extract_bit( sig, 0      ); }
     260             : 
     261           0 : #define FD_DISCO_REPAIR_REPLAY_MTU (sizeof(ulong) + sizeof(ushort) + sizeof(uint) + sizeof(int))
     262             : 
     263             : FD_FN_PURE static inline ulong
     264           0 : fd_disco_compact_chunk0( void * wksp ) {
     265           0 :   return (((struct fd_wksp_private *)wksp)->gaddr_lo) >> FD_CHUNK_LG_SZ;
     266           0 : }
     267             : 
     268             : FD_FN_PURE static inline ulong
     269           0 : fd_disco_compact_wmark( void * wksp, ulong mtu ) {
     270           0 :   ulong chunk_mtu  = ((mtu + 2UL*FD_CHUNK_SZ-1UL) >> (1+FD_CHUNK_LG_SZ)) << 1;
     271           0 :   ulong wksp_hi = ((struct fd_wksp_private *)wksp)->gaddr_hi;
     272           0 :   return (wksp_hi >> FD_CHUNK_LG_SZ) - chunk_mtu;
     273           0 : }
     274             : 
     275             : FD_PROTOTYPES_END
     276             : 
     277             : #endif /* HEADER_fd_src_disco_fd_disco_base_h */

Generated by: LCOV version 1.14