LCOV - code coverage report
Current view: top level - disco/topo - fd_topo.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 25 76 32.9 %
Date: 2024-11-13 11:58:15 Functions: 5 715 0.7 %

          Line data    Source code
       1             : #ifndef HEADER_fd_src_disco_topo_fd_topo_h
       2             : #define HEADER_fd_src_disco_topo_fd_topo_h
       3             : 
       4             : #include "../stem/fd_stem.h"
       5             : #include "../quic/fd_tpu.h"
       6             : #include "../../tango/fd_tango.h"
       7             : #include "../../waltz/xdp/fd_xdp1.h"
       8             : 
       9             : /* Maximum number of workspaces that may be present in a topology. */
      10             : #define FD_TOPO_MAX_WKSPS         (256UL)
      11             : /* Maximum number of links that may be present in a topology. */
      12             : #define FD_TOPO_MAX_LINKS         (256UL)
      13             : /* Maximum number of tiles that may be present in a topology. */
      14             : #define FD_TOPO_MAX_TILES         (256UL)
      15             : /* Maximum number of objects that may be present in a topology. */
      16             : #define FD_TOPO_MAX_OBJS          (4096UL)
      17             : /* Maximum number of links that may go into any one tile in the
      18             :    topology. */
      19             : #define FD_TOPO_MAX_TILE_IN_LINKS  ( 128UL)
      20             : /* Maximum number of links that a tile may write to. */
      21             : #define FD_TOPO_MAX_TILE_OUT_LINKS ( 32UL)
      22             : /* Maximum number of objects that a tile can use. */
      23             : #define FD_TOPO_MAX_TILE_OBJS      ( 256UL)
      24             : 
      25             : /* Maximum number of additional ip addresses */
      26             : #define FD_NET_MAX_SRC_ADDR 4
      27             : 
      28             : /* A workspace is a Firedance specific memory management structure that
      29             :    sits on top of 1 or more memory mapped gigantic or huge pages mounted
      30             :    to the hugetlbfs. */
      31             : typedef struct {
      32             :   ulong id;           /* The ID of this workspace.  Indexed from [0, wksp_cnt).  When placed in a topology, the ID must be the index of the workspace in the workspaces list. */
      33             :   char  name[ 13UL ]; /* The name of this workspace, like "pack".  There can be at most one of each workspace name in a topology. */
      34             : 
      35             :   ulong numa_idx;     /* The index of the NUMA node on the system that this workspace should be allocated from. */
      36             : 
      37             :   /* Computed fields.  These are not supplied as configuration but calculated as needed. */
      38             :   struct {
      39             :     ulong page_sz;  /* The size of the pages that this workspace is backed by.  One of FD_PAGE_SIZE_*. */
      40             :     ulong page_cnt; /* The number of pages that must be mapped to this workspace to store all the data needed by consumers. */
      41             :     ulong part_max; /* The maximum number of partitions in the underlying workspace.  There can only be this many allocations made at any one time. */
      42             : 
      43             :     fd_wksp_t * wksp;            /* The workspace memory in the local process. */
      44             :     ulong       known_footprint; /* Total size in bytes of all data in Firedancer that will be stored in this workspace at startup. */
      45             :     ulong       total_footprint; /* Total size in bytes of all data in Firedancer that could be stored in this workspace, includes known data and loose data. */
      46             :   };
      47             : } fd_topo_wksp_t;
      48             : 
      49             : /* A link is an mcache in a workspace that has one producer and one or
      50             :    more consumers. A link may optionally also have a dcache, that holds
      51             :    fragments referred to by the mcache entries.
      52             : 
      53             :    A link belongs to exactly one workspace.  A link has exactly one
      54             :    producer, and 1 or more consumers.  Each consumer is either reliable
      55             :    or not reliable.  A link has a depth and a MTU, which correspond to
      56             :    the depth and MTU of the mcache and dcache respectively.  A MTU of
      57             :    zero means no dcache is needed, as there is no data. */
      58             : typedef struct {
      59             :   ulong id;           /* The ID of this link.  Indexed from [0, link_cnt).  When placed in a topology, the ID must be the index of the link in the links list. */
      60             :   char  name[ 13UL ]; /* The name of this link, like "pack_bank". There can be multiple of each link name in a topology. */
      61             :   ulong kind_id;      /* The ID of this link within its name.  If there are N links of a particular name, they have IDs [0, N).  The pair (name, kind_id) uniquely identifies a link, as does "id" on its own. */
      62             : 
      63             :   int   is_reasm; /* If the link is a reassembly buffer. */
      64             :   ulong depth;    /* The depth of the mcache representing the link. */
      65             :   ulong mtu;      /* The MTU of data fragments in the mcache.  A value of 0 means there is no dcache. */
      66             :   ulong burst;    /* The max amount of MTU sized data fragments that might be bursted to the dcache. */
      67             : 
      68             :   ulong mcache_obj_id;
      69             :   ulong dcache_obj_id;
      70             :   ulong reasm_obj_id;
      71             : 
      72             :   /* Computed fields.  These are not supplied as configuration but calculated as needed. */
      73             :   struct {
      74             :     fd_frag_meta_t * mcache; /* The mcache of this link. */
      75             :     void *           dcache; /* The dcache of this link, if it has one. */
      76             :     fd_tpu_reasm_t * reasm;  /* The reassembly buffer of this link, if it has one. */
      77             :   };
      78             : } fd_topo_link_t;
      79             : 
      80             : /* A tile is a unique process that is spawned by Firedancer to represent
      81             :    one thread of execution.  Firedancer sandboxes all tiles to their own
      82             :    process for security reasons.
      83             : 
      84             :    A tile belongs to exactly one workspace.  A tile is a consumer of 0
      85             :    or more links, it's inputs.  A tile is a producer of 0 or more output
      86             :    links.
      87             : 
      88             :    All input links will be automatically polled by the tile
      89             :    infrastructure, and output links will automatically source and manage
      90             :    credits from consumers. */
      91             : typedef struct {
      92             :   ulong id;                     /* The ID of this tile.  Indexed from [0, tile_cnt).  When placed in a topology, the ID must be the index of the tile in the tiles list. */
      93             :   char  name[ 7UL ];            /* The name of this tile.  There can be multiple of each tile name in a topology. */
      94             :   ulong kind_id;                /* The ID of this tile within its name.  If there are n tile of a particular name, they have IDs [0, N).  The pair (name, kind_id) uniquely identifies a tile, as does "id" on its own. */
      95             :   int   is_agave;               /* If the tile needs to run in the Agave (Anza) address space or not. */
      96             : 
      97             :   ulong cpu_idx;                /* The CPU index to pin the tile on.  A value of ULONG_MAX or more indicates the tile should be floating and not pinned to a core. */
      98             : 
      99             :   ulong in_cnt;                 /* The number of links that this tile reads from. */
     100             :   ulong in_link_id[ FD_TOPO_MAX_TILE_IN_LINKS ];       /* The link_id of each link that this tile reads from, indexed in [0, in_cnt). */
     101             :   int   in_link_reliable[ FD_TOPO_MAX_TILE_IN_LINKS ]; /* If each link that this tile reads from is a reliable or unreliable consumer, indexed in [0, in_cnt). */
     102             :   int   in_link_poll[ FD_TOPO_MAX_TILE_IN_LINKS ];     /* If each link that this tile reads from should be polled by the tile infrastructure, indexed in [0, in_cnt).
     103             :                                                           If the link is not polled, the tile will not receive frags for it and the tile writer is responsible for
     104             :                                                           reading from the link.  The link must be marked as unreliable as it is not flow controlled. */
     105             : 
     106             :   ulong out_cnt;                                   /* The number of links that this tile writes to. */
     107             :   ulong out_link_id[ FD_TOPO_MAX_TILE_OUT_LINKS ]; /* The link_id of each link that this tile writes to, indexed in [0, link_cnt). */
     108             : 
     109             :   ulong tile_obj_id;
     110             :   ulong metrics_obj_id;
     111             :   ulong in_link_fseq_obj_id[ FD_TOPO_MAX_TILE_IN_LINKS ];
     112             : 
     113             :   ulong uses_obj_cnt;
     114             :   ulong uses_obj_id[ FD_TOPO_MAX_TILE_OBJS ];
     115             :   int   uses_obj_mode[ FD_TOPO_MAX_TILE_OBJS ];
     116             : 
     117             :   /* Computed fields.  These are not supplied as configuration but calculated as needed. */
     118             :   struct {
     119             :     ulong *    metrics; /* The shared memory for metrics that this tile should write.  Consumer by monitoring and metrics writing tiles. */
     120             : 
     121             :     /* The fseq of each link that this tile reads from.  Multiple fseqs
     122             :        may point to the link, if there are multiple consumers.  An fseq
     123             :        can be uniquely identified via (link_id, tile_id), or (link_kind,
     124             :        link_kind_id, tile_kind, tile_kind_id) */
     125             :     ulong *    in_link_fseq[ FD_TOPO_MAX_TILE_IN_LINKS ];
     126             :   };
     127             : 
     128             :   /* Configuration fields.  These are required to be known by the topology so it can determine the
     129             :      total size of Firedancer in memory. */
     130             :   union {
     131             :     struct {
     132             :       char   interface[ 16 ];
     133             :       ulong  xdp_rx_queue_size;
     134             :       ulong  xdp_tx_queue_size;
     135             :       ulong  xdp_aio_depth;
     136             :       char   xdp_mode[4];
     137             :       int    zero_copy;
     138             :       uint   src_ip_addr;
     139             :       uchar  src_mac_addr[6];
     140             : 
     141             :       ushort shred_listen_port;
     142             :       ushort quic_transaction_listen_port;
     143             :       ushort legacy_transaction_listen_port;
     144             :       ushort gossip_listen_port;
     145             :       ushort repair_intake_listen_port;
     146             :       ushort repair_serve_listen_port;
     147             : 
     148             :       /* multihoming support */
     149             :       ulong multihome_ip_addrs_cnt;
     150             :       uint  multihome_ip_addrs[FD_NET_MAX_SRC_ADDR];
     151             :     } net;
     152             : 
     153             :     struct {
     154             :       ulong  depth;
     155             :       uint   reasm_cnt;
     156             :       ulong  max_concurrent_connections;
     157             :       ulong  max_concurrent_handshakes;
     158             :       ulong  max_inflight_quic_packets;
     159             :       ulong  max_concurrent_streams_per_connection;
     160             :       uint   ip_addr;
     161             :       uchar  src_mac_addr[ 6 ];
     162             :       ushort quic_transaction_listen_port;
     163             :       ulong  idle_timeout_millis;
     164             :       uint   ack_delay_millis;
     165             :       char   identity_key_path[ PATH_MAX ];
     166             :       int    retry;
     167             :     } quic;
     168             : 
     169             :     struct {
     170             :       ulong tcache_depth;
     171             :     } dedup;
     172             : 
     173             :     struct {
     174             :       ulong max_pending_transactions;
     175             :       ulong bank_tile_count;
     176             :       int   larger_max_cost_per_block;
     177             :       int   larger_shred_limits_per_block;
     178             :       int   use_consumed_cus;
     179             :       char  identity_key_path[ PATH_MAX ];
     180             :     } pack;
     181             : 
     182             :     struct {
     183             :       int   plugins_enabled;
     184             :       ulong bank_cnt;
     185             :       char  identity_key_path[ PATH_MAX ];
     186             :     } poh;
     187             : 
     188             :     struct {
     189             :       ulong  depth;
     190             :       uint   ip_addr;
     191             :       uchar  src_mac_addr[ 6 ];
     192             :       ulong  fec_resolver_depth;
     193             :       char   identity_key_path[ PATH_MAX ];
     194             :       ushort shred_listen_port;
     195             :       int    larger_shred_limits_per_block;
     196             :       ulong  expected_shred_version;
     197             :     } shred;
     198             : 
     199             :     struct {
     200             :       ulong disable_blockstore_from_slot;
     201             :     } store;
     202             : 
     203             :     struct {
     204             :       char   identity_key_path[ PATH_MAX ];
     205             :     } sign;
     206             : 
     207             :     struct {
     208             :       uint   listen_addr;
     209             :       ushort listen_port;
     210             : 
     211             :       int    is_voting;
     212             : 
     213             :       char   cluster[ 32 ];
     214             :       char   identity_key_path[ PATH_MAX ];
     215             :     } gui;
     216             : 
     217             :     struct {
     218             :       uint   prometheus_listen_addr;
     219             :       ushort prometheus_listen_port;
     220             :     } metric;
     221             : 
     222             :     struct {
     223             : 
     224             :       /* specified by [tiles.replay] */
     225             : 
     226             :       char  blockstore_checkpt[ PATH_MAX ];
     227             :       int   blockstore_publish;
     228             :       int   tx_metadata_storage;
     229             :       char  capture[ PATH_MAX ];
     230             :       char  funk_checkpt[ PATH_MAX ];
     231             :       ulong funk_rec_max;
     232             :       ulong funk_sz_gb;
     233             :       ulong funk_txn_max;
     234             :       char  funk_file[ PATH_MAX ];
     235             :       char  genesis[ PATH_MAX ];
     236             :       char  incremental[ PATH_MAX ];
     237             :       char  slots_replayed[ PATH_MAX ];
     238             :       char  snapshot[ PATH_MAX ];
     239             :       char  status_cache[ PATH_MAX ];
     240             :       ulong tpool_thread_count;
     241             :       char  cluster_version[ 32 ];
     242             : 
     243             :       /* not specified by [tiles.replay] */
     244             : 
     245             :       char  identity_key_path[ PATH_MAX ];
     246             :       uint  ip_addr;
     247             :       uchar src_mac_addr[ 6 ];
     248             :       int   vote;
     249             :       char  vote_account_path[ PATH_MAX ];
     250             :       ulong bank_tile_count;
     251             :     } replay;
     252             : 
     253             :     struct {
     254             :       ushort send_to_port;
     255             :       uint   send_to_ip_addr;
     256             :       ulong  conn_cnt;
     257             :       int    no_quic;
     258             :     } benchs;
     259             : 
     260             :     struct {
     261             :       ushort rpc_port;
     262             :       uint   rpc_ip_addr;
     263             :     } bencho;
     264             : 
     265             :     struct {
     266             :       ulong accounts_cnt;
     267             :       int   mode;
     268             :       float contending_fraction;
     269             :       float cu_price_spread;
     270             :     } benchg;
     271             : 
     272             :     /* Firedancer-only tile configs */
     273             : 
     274             :     struct {
     275             :       ushort  gossip_listen_port;
     276             :       ulong   entrypoints_cnt;
     277             :       uint    entrypoints[16];
     278             :       ulong   peer_ports_cnt;
     279             :       ushort  peer_ports[16];
     280             : 
     281             :       uint    ip_addr;
     282             :       uchar   src_mac_addr[ 6 ];
     283             :       char    identity_key_path[ PATH_MAX ];
     284             :       ushort  tvu_port;
     285             :       ushort  tvu_fwd_port;
     286             :       ushort  tpu_port;
     287             :       ushort  tpu_vote_port;
     288             :       ushort  repair_serve_port;
     289             :       ulong   expected_shred_version;
     290             :     } gossip;
     291             : 
     292             :     struct {
     293             :       ushort  repair_intake_listen_port;
     294             :       ushort  repair_serve_listen_port;
     295             : 
     296             :       /* non-config */
     297             : 
     298             :       uint    ip_addr;
     299             :       uchar   src_mac_addr[ 6 ];
     300             :       char    identity_key_path[ PATH_MAX ];
     301             :     } repair;
     302             : 
     303             :     struct {
     304             :       char  blockstore_restore[ PATH_MAX ];
     305             :       char  slots_pending[PATH_MAX];
     306             : 
     307             :       ulong expected_shred_version;
     308             : 
     309             :       /* non-config */
     310             : 
     311             :       char  identity_key_path[ PATH_MAX ];
     312             :       char  shred_cap_archive[ PATH_MAX ];
     313             :       char  shred_cap_replay[ PATH_MAX ];
     314             :     } store_int;
     315             : 
     316             :     struct {
     317             :       ushort  tpu_listen_port;
     318             : 
     319             :       /* non-config */
     320             : 
     321             :       uint    ip_addr;
     322             :       uchar   src_mac_addr[ 6 ];
     323             :       char  identity_key_path[ PATH_MAX ];
     324             :     } sender;
     325             : 
     326             :     struct {
     327             :       char  identity_key_path[ PATH_MAX ];
     328             :     } eqvoc;
     329             : 
     330             :     struct {
     331             :       ushort  rpc_port;
     332             :       ushort  tpu_port;
     333             :       uint    tpu_ip_addr;
     334             :       char    identity_key_path[ PATH_MAX ];
     335             :     } rpcserv;
     336             :   };
     337             : } fd_topo_tile_t;
     338             : 
     339             : typedef struct {
     340             :   ulong id;
     341             :   char  name[ 13UL ];
     342             :   ulong wksp_id;
     343             : 
     344             :   ulong offset;
     345             :   ulong footprint;
     346             : } fd_topo_obj_t;
     347             : 
     348             : /* An fd_topo_t represents the overall structure of a Firedancer
     349             :    configuration, describing all the workspaces, tiles, and links
     350             :    between them. */
     351             : typedef struct fd_topo_t {
     352             :   char           app_name[ 256UL ];
     353             :   uchar          props[ 16384UL ];
     354             : 
     355             :   ulong          wksp_cnt;
     356             :   ulong          link_cnt;
     357             :   ulong          tile_cnt;
     358             :   ulong          obj_cnt;
     359             : 
     360             :   fd_topo_wksp_t workspaces[ FD_TOPO_MAX_WKSPS ];
     361             :   fd_topo_link_t links[ FD_TOPO_MAX_LINKS ];
     362             :   fd_topo_tile_t tiles[ FD_TOPO_MAX_TILES ];
     363             :   fd_topo_obj_t  objs[ FD_TOPO_MAX_OBJS ];
     364             : 
     365             :   ulong          agave_affinity_cnt;
     366             :   ulong          agave_affinity_cpu_idx[ FD_TILE_MAX ];
     367             : } fd_topo_t;
     368             : 
     369             : typedef struct {
     370             :   char const * name;
     371             : 
     372             :   int          keep_host_networking;
     373             :   ulong        rlimit_file_cnt;
     374             :   int          for_tpool;
     375             : 
     376             :   ulong (*populate_allowed_seccomp)( fd_topo_t const * topo, fd_topo_tile_t const * tile, ulong out_cnt, struct sock_filter * out );
     377             :   ulong (*populate_allowed_fds    )( fd_topo_t const * topo, fd_topo_tile_t const * tile, ulong out_fds_sz, int * out_fds );
     378             :   ulong (*scratch_align           )( void );
     379             :   ulong (*scratch_footprint       )( fd_topo_tile_t const * tile );
     380             :   ulong (*loose_footprint         )( fd_topo_tile_t const * tile );
     381             :   void  (*privileged_init         )( fd_topo_t * topo, fd_topo_tile_t * tile );
     382             :   void  (*unprivileged_init       )( fd_topo_t * topo, fd_topo_tile_t * tile );
     383             :   void  (*run                     )( fd_topo_t * topo, fd_topo_tile_t * tile );
     384             : } fd_topo_run_tile_t;
     385             : 
     386             : FD_PROTOTYPES_BEGIN
     387             : 
     388             : FD_FN_CONST static inline ulong
     389           0 : fd_topo_workspace_align( void ) {
     390             :   /* This needs to be the max( align ) of all the child members that
     391             :      could be aligned into this workspace, otherwise our footprint
     392             :      calculation will not be correct.  For now just set to 4096 but this
     393             :      should probably be calculated dynamically, or we should reduce
     394             :      those child aligns if we can. */
     395           0 :   return 4096UL;
     396           0 : }
     397             : 
     398             : FD_FN_PURE static inline void *
     399             : fd_topo_obj_laddr( fd_topo_t const * topo,
     400           0 :                    ulong             obj_id ) {
     401           0 :   fd_topo_obj_t const * obj = &topo->objs[ obj_id ];
     402           0 :   return (void *)((ulong)topo->workspaces[ obj->wksp_id ].wksp + obj->offset);
     403           0 : }
     404             : 
     405             : FD_FN_PURE static inline ulong
     406             : fd_topo_tile_name_cnt( fd_topo_t const * topo,
     407           0 :                        char const *      name ) {
     408           0 :   ulong cnt = 0;
     409           0 :   for( ulong i=0; i<topo->tile_cnt; i++ ) {
     410           0 :     if( FD_UNLIKELY( !strcmp( topo->tiles[ i ].name, name ) ) ) cnt++;
     411           0 :   }
     412           0 :   return cnt;
     413           0 : }
     414             : 
     415             : /* Finds the workspace of a given name in the topology.  Returns
     416             :    ULONG_MAX if there is no such workspace.  There can be at most one
     417             :    workspace of a given name. */
     418             : 
     419             : FD_FN_PURE static inline ulong
     420             : fd_topo_find_wksp( fd_topo_t const * topo,
     421         342 :                    char const *      name ) {
     422        4209 :   for( ulong i=0; i<topo->wksp_cnt; i++ ) {
     423        4209 :     if( FD_UNLIKELY( !strcmp( topo->workspaces[ i ].name, name ) ) ) return i;
     424        4209 :   }
     425           0 :   return ULONG_MAX;
     426         342 : }
     427             : 
     428             : /* Find the tile of a given name and kind_id in the topology, there will
     429             :    be at most one such tile, since kind_id is unique among the name.
     430             :    Returns ULONG_MAX if there is no such tile. */
     431             : 
     432             : FD_FN_PURE static inline ulong
     433             : fd_topo_find_tile( fd_topo_t const * topo,
     434             :                    char const *      name,
     435         180 :                    ulong             kind_id ) {
     436        1602 :   for( ulong i=0; i<topo->tile_cnt; i++ ) {
     437        1602 :     if( FD_UNLIKELY( !strcmp( topo->tiles[ i ].name, name ) ) && topo->tiles[ i ].kind_id == kind_id ) return i;
     438        1602 :   }
     439           0 :   return ULONG_MAX;
     440         180 : }
     441             : 
     442             : /* Find the link of a given name and kind_id in the topology, there will
     443             :    be at most one such link, since kind_id is unique among the name.
     444             :    Returns ULONG_MAX if there is no such link. */
     445             : 
     446             : FD_FN_PURE static inline ulong
     447             : fd_topo_find_link( fd_topo_t const * topo,
     448             :                    char const *      name,
     449         162 :                    ulong             kind_id ) {
     450        1917 :   for( ulong i=0; i<topo->link_cnt; i++ ) {
     451        1917 :     if( FD_UNLIKELY( !strcmp( topo->links[ i ].name, name ) ) && topo->links[ i ].kind_id == kind_id ) return i;
     452        1917 :   }
     453           0 :   return ULONG_MAX;
     454         162 : }
     455             : 
     456             : FD_FN_PURE static inline ulong
     457             : fd_topo_find_tile_in_link( fd_topo_t const *      topo,
     458             :                            fd_topo_tile_t const * tile,
     459             :                            char const *           name,
     460           0 :                            ulong                  kind_id ) {
     461           0 :   for( ulong i=0; i<tile->in_cnt; i++ ) {
     462           0 :     if( FD_UNLIKELY( !strcmp( topo->links[ tile->in_link_id[ i ] ].name, name ) )
     463           0 :         && topo->links[ tile->in_link_id[ i ] ].kind_id == kind_id ) return i;
     464           0 :   }
     465           0 :   return ULONG_MAX;
     466           0 : }
     467             : 
     468             : FD_FN_PURE static inline ulong
     469             : fd_topo_find_tile_out_link( fd_topo_t const *      topo,
     470             :                            fd_topo_tile_t const * tile,
     471             :                            char const *           name,
     472           0 :                            ulong                  kind_id ) {
     473           0 :   for( ulong i=0; i<tile->out_cnt; i++ ) {
     474           0 :     if( FD_UNLIKELY( !strcmp( topo->links[ tile->out_link_id[ i ] ].name, name ) )
     475           0 :         && topo->links[ tile->out_link_id[ i ] ].kind_id == kind_id ) return i;
     476           0 :   }
     477           0 :   return ULONG_MAX;
     478           0 : }
     479             : 
     480             : /* Find the id of the tile which is a producer for the given link.  If
     481             :    no tile is a producer for the link, returns ULONG_MAX.  This should
     482             :    not be possible for a well formed and validated topology.  */
     483             : FD_FN_PURE static inline ulong
     484             : fd_topo_find_link_producer( fd_topo_t const *      topo,
     485           0 :                             fd_topo_link_t const * link ) {
     486           0 :   for( ulong i=0; i<topo->tile_cnt; i++ ) {
     487           0 :     fd_topo_tile_t const * tile = &topo->tiles[ i ];
     488             : 
     489           0 :     for( ulong j=0; j<tile->out_cnt; j++ ) {
     490           0 :       if( FD_UNLIKELY( tile->out_link_id[ j ] == link->id ) ) return i;
     491           0 :     }
     492           0 :   }
     493           0 :   return ULONG_MAX;
     494           0 : }
     495             : 
     496             : /* Given a link, count the number of consumers of that link among all
     497             :    the tiles in the topology. */
     498             : FD_FN_PURE static inline ulong
     499             : fd_topo_link_consumer_cnt( fd_topo_t const *      topo,
     500          69 :                            fd_topo_link_t const * link ) {
     501          69 :   ulong cnt = 0;
     502        1242 :   for( ulong i=0; i<topo->tile_cnt; i++ ) {
     503        1173 :     fd_topo_tile_t const * tile = &topo->tiles[ i ];
     504        3312 :     for( ulong j=0; j<tile->in_cnt; j++ ) {
     505        2139 :       if( FD_UNLIKELY( tile->in_link_id[ j ] == link->id ) ) cnt++;
     506        2139 :     }
     507        1173 :   }
     508             : 
     509          69 :   return cnt;
     510          69 : }
     511             : 
     512             : /* Given a link, count the number of reliable consumers of that link
     513             :    among all the tiles in the topology. */
     514             : FD_FN_PURE static inline ulong
     515             : fd_topo_link_reliable_consumer_cnt( fd_topo_t const *      topo,
     516           0 :                                     fd_topo_link_t const * link ) {
     517           0 :   ulong cnt = 0;
     518           0 :   for( ulong i=0; i<topo->tile_cnt; i++ ) {
     519           0 :     fd_topo_tile_t const * tile = &topo->tiles[ i ];
     520           0 :     for( ulong j=0; j<tile->in_cnt; j++ ) {
     521           0 :       if( FD_UNLIKELY( tile->in_link_id[ j ] == link->id && tile->in_link_reliable[ j ] ) ) cnt++;
     522           0 :     }
     523           0 :   }
     524           0 : 
     525           0 :   return cnt;
     526           0 : }
     527             : 
     528             : /* Join (map into the process) all shared memory (huge/gigantic pages)
     529             :    needed by the tile, in the given topology.  All memory associated
     530             :    with the tile (aka. used by links that the tile either produces to or
     531             :    consumes from, or used by the tile itself for its cnc) will be
     532             :    attached (mapped into the process).
     533             : 
     534             :    This is needed to play nicely with the sandbox.  Once a process is
     535             :    sandboxed we can no longer map any memory. */
     536             : void
     537             : fd_topo_join_tile_workspaces( fd_topo_t *      topo,
     538             :                               fd_topo_tile_t * tile );
     539             : 
     540             : /* Join (map into the process) the shared memory (huge/gigantic pages)
     541             :    for the given workspace.  Mode is one of
     542             :    FD_SHMEM_JOIN_MODE_READ_WRITE or FD_SHMEM_JOIN_MODE_READ_ONLY and
     543             :    determines the prot argument that will be passed to mmap when mapping
     544             :    the pages in (PROT_WRITE or PROT_READ respectively). */
     545             : void
     546             : fd_topo_join_workspace( fd_topo_t *      topo,
     547             :                         fd_topo_wksp_t * wksp,
     548             :                         int              mode );
     549             : 
     550             : /* Join (map into the process) all shared memory (huge/gigantic pages)
     551             :    needed by all tiles in the topology.  Mode is one of
     552             :    FD_SHMEM_JOIN_MODE_READ_WRITE or FD_SHMEM_JOIN_MODE_READ_ONLY and
     553             :    determines the prot argument that will be passed to mmap when
     554             :    mapping the pages in (PROT_WRITE or PROT_READ respectively). */
     555             : void
     556             : fd_topo_join_workspaces( fd_topo_t *  topo,
     557             :                          int          mode );
     558             : 
     559             : /* Leave (unmap from the process) the shared memory needed for the
     560             :    given workspace in the topology, if it was previously mapped.
     561             : 
     562             :    topo and wksp are assumed non-NULL.  It is OK if the workspace
     563             :    has not been previously joined, in which case this is a no-op. */
     564             : 
     565             : void
     566             : fd_topo_leave_workspace( fd_topo_t *      topo,
     567             :                          fd_topo_wksp_t * wksp );
     568             : 
     569             : /* Leave (unmap from the process) all shared memory needed by all
     570             :    tiles in the topology, if each of them was mapped.
     571             : 
     572             :    topo is assumed non-NULL.  Only workspaces which were previously
     573             :    joined are unmapped. */
     574             : 
     575             : void
     576             : fd_topo_leave_workspaces( fd_topo_t * topo );
     577             : 
     578             : /* Create the given workspace needed by the topology on the system.
     579             :    This does not "join" the workspaces (map their memory into the
     580             :    process), but only creates the .wksp file and formats it correctly
     581             :    as a workspace.
     582             : 
     583             :    Returns 0 on success and -1 on failure, with errno set to the error.
     584             :    The only reason for failure currently that will be returned is
     585             :    ENOMEM, as other unexpected errors will cause the program to exit.
     586             : 
     587             :    If update_existing is 1, the workspace will not be created from
     588             :    scratch but it will be assumed that it already exists from a prior
     589             :    run and needs to be maybe resized and then have the header
     590             :    structures reinitialized.  This can save a very expensive operation
     591             :    of zeroing all of the workspace pages.  This is dangerous in
     592             :    production because it can leave stray memory from prior runs around,
     593             :    and should only be used in development environments. */
     594             : 
     595             : int
     596             : fd_topo_create_workspace( fd_topo_t *      topo,
     597             :                           fd_topo_wksp_t * wksp,
     598             :                           int              update_existing );
     599             : 
     600             : /* Join the standard IPC objects needed by the topology of this particular
     601             :    tile */
     602             : 
     603             : void
     604             : fd_topo_fill_tile( fd_topo_t *      topo,
     605             :                    fd_topo_tile_t * tile );
     606             : 
     607             : /* Same as fd_topo_fill_tile but fills in all the objects for a
     608             :    particular workspace with the given mode. */
     609             : void
     610             : fd_topo_workspace_fill( fd_topo_t *      topo,
     611             :                         fd_topo_wksp_t * wksp );
     612             : 
     613             : /* Apply a function to every object that is resident in the given
     614             :    workspace in the topology. */
     615             : 
     616             : void
     617             : fd_topo_wksp_apply( fd_topo_t *      topo,
     618             :                     fd_topo_wksp_t * wksp,
     619             :                     void (* fn )( fd_topo_t const * topo, fd_topo_obj_t const * obj ) );
     620             : 
     621             : /* Same as fd_topo_fill_tile but fills in all tiles in the topology. */
     622             : 
     623             : void
     624             : fd_topo_fill( fd_topo_t * topo );
     625             : 
     626             : /* fd_topo_tile_stack_join joins a huge page optimized stack for the
     627             :    provided tile.  The stack is assumed to already exist at a known
     628             :    path in the hugetlbfs mount. */
     629             : 
     630             : void *
     631             : fd_topo_tile_stack_join( char const * app_name,
     632             :                          char const * tile_name,
     633             :                          ulong        tile_kind_id );
     634             : 
     635             : /* Install the XDP program needed by the net tiles into the local device
     636             :    and return the xsk_map_fd. */
     637             : 
     638             : fd_xdp_fds_t
     639             : fd_topo_install_xdp( fd_topo_t * topo );
     640             : 
     641             : /* fd_topo_run_single_process runs all the tiles in a single process
     642             :    (the calling process).  This spawns a thread for each tile, switches
     643             :    that thread to the given UID and GID and then runs the tile in it.
     644             :    Each thread will never exit, as tiles are expected to run forever.
     645             :    An error is logged and the application will exit if a tile exits.
     646             :    The function itself does return after spawning all the threads.
     647             : 
     648             :    The threads will not be sandboxed in any way, except switching to the
     649             :    provided UID and GID, so they will share the same address space, and
     650             :    not have any seccomp restrictions or use any Linux namespaces.  The
     651             :    calling thread will also switch to the provided UID and GID before
     652             :    it returns.
     653             : 
     654             :    In production, when running with an Agave child process this is
     655             :    used for spawning certain tiles inside the Agave address space.
     656             :    It's also useful for tooling and debugging, but is not how the main
     657             :    production Firedancer process runs.  For production, each tile is run
     658             :    in its own address space with a separate process and full security
     659             :    sandbox.
     660             : 
     661             :    The agave argument determines which tiles are started.  If the
     662             :    argument is 0 or 1, only non-agave (or only agave) tiles are started.
     663             :    If the argument is any other value, all tiles in the topology are
     664             :    started regardless of if they are Agave tiles or not. */
     665             : 
     666             : void
     667             : fd_topo_run_single_process( fd_topo_t * topo,
     668             :                             int         agave,
     669             :                             uint        uid,
     670             :                             uint        gid,
     671             :                             fd_topo_run_tile_t (* tile_run )( fd_topo_tile_t * tile ),
     672             :                             int *       done_futex );
     673             : 
     674             : /* fd_topo_run_tile runs the given tile directly within the current
     675             :    process (and thread).  The function will never return, as tiles are
     676             :    expected to run forever.  An error is logged and the application will
     677             :    exit if the tile exits.
     678             : 
     679             :    The sandbox argument determines if the current process will be
     680             :    sandboxed fully before starting the tile.  The thread will switch to
     681             :    the UID and GID provided before starting the tile, even if the thread
     682             :    is not being sandboxed.  Although POSIX specifies that all threads in
     683             :    a process must share a UID and GID, this is not the case on Linux.
     684             :    The thread will switch to the provided UID and GID without switching
     685             :    the other threads in the process.
     686             : 
     687             :    If keep_controlling_terminal is set to 0, and the sandbox is enabled
     688             :    the controlling terminal will be detached as an additional sandbox
     689             :    measure, but you will not be able to send Ctrl+C or other signals
     690             :    from the terminal.  See fd_sandbox.h for more information.
     691             : 
     692             :    The allow_fd argument is only used if sandbox is true, and is a file
     693             :    descriptor which will be allowed to exist in the process.  Normally
     694             :    the sandbox code rejects and aborts if there is an unexpected file
     695             :    descriptor present on boot.  This is helpful to allow a parent
     696             :    process to be notified on termination of the tile by waiting for a
     697             :    pipe file descriptor to get closed.
     698             : 
     699             :    wait and debugger are both used in debugging.  If wait is non-NULL,
     700             :    the runner will wait until the value pointed to by wait is non-zero
     701             :    before launching the tile.  Likewise, if debugger is non-NULL, the
     702             :    runner will wait until a debugger is attached before setting the
     703             :    value pointed to by debugger to non-zero.  These are intended to be
     704             :    used as a pair, where many tiles share a waiting reference, and then
     705             :    one of the tiles (a tile you want to attach the debugger to) has the
     706             :    same reference provided as the debugger, so all tiles will stop and
     707             :    wait for the debugger to attach to it before proceeding. */
     708             : 
     709             : void
     710             : fd_topo_run_tile( fd_topo_t *          topo,
     711             :                   fd_topo_tile_t *     tile,
     712             :                   int                  sandbox,
     713             :                   int                  keep_controlling_terminal,
     714             :                   uint                 uid,
     715             :                   uint                 gid,
     716             :                   int                  allow_fd,
     717             :                   volatile int *       wait,
     718             :                   volatile int *       debugger,
     719             :                   fd_topo_run_tile_t * tile_run );
     720             : 
     721             : /* This is for determining the value of RLIMIT_MLOCK that we need to
     722             :    successfully run all tiles in separate processes.  The value returned
     723             :    is the maximum amount of memory that will be locked with mlock() by
     724             :    any individual process in the tree.  Specifically, if we have three
     725             :    tile processes, and they each need to lock 5, 9, and 2 MiB of memory
     726             :    respectively, RLIMIT_MLOCK needs to be 9 MiB to allow all three
     727             :    process mlock() calls to succeed.
     728             : 
     729             :    Tiles lock memory in three ways.  Any workspace they are using, they
     730             :    lock the entire workspace.  Then each tile uses huge pages for the
     731             :    stack which are also locked, and finally some tiles use private
     732             :    locked mmaps outside the workspace for storing key material.  The
     733             :    results here include all of this memory together.
     734             : 
     735             :    The result is not necessarily the amount of memory used by the tile
     736             :    process, although it will be quite close.  Tiles could potentially
     737             :    allocate memory (eg, with brk) without needing to lock it, which
     738             :    would not need to included, and some kernel memory that tiles cause
     739             :    to be allocated (for example XSK buffers) is also not included.  The
     740             :    actual amount of memory used will not be less than this value. */
     741             : FD_FN_PURE ulong
     742             : fd_topo_mlock_max_tile( fd_topo_t * topo );
     743             : 
     744             : /* Same as fd_topo_mlock_max_tile, but for loading the entire topology
     745             :    topology into one process, rather than a separate process per tile.
     746             :    This is used, for example, by the configuration code when it creates
     747             :    all the workspaces, or the monitor that maps the entire system into
     748             :    one address space. */
     749             : FD_FN_PURE ulong
     750             : fd_topo_mlock( fd_topo_t * topo );
     751             : 
     752             : /* This returns the number of gigantic pages needed by the topology on
     753             :    the provided numa node.  It includes pages needed by the workspaces,
     754             :    as well as additional allocations like huge pages for process stacks
     755             :    and private key storage. */
     756             : 
     757             : FD_FN_PURE ulong
     758             : fd_topo_gigantic_page_cnt( fd_topo_t * topo,
     759             :                            ulong       numa_idx );
     760             : 
     761             : /* This returns the number of huge pages in the application needed by
     762             :    the topology on the provided numa node.  It includes pages needed by
     763             :    things placed in the hugetlbfs (workspaces, process stacks).  If
     764             :    include_anonymous is true, it also includes anonymous hugepages which
     765             :    are needed but are not placed in the hugetlbfs. */
     766             : 
     767             : FD_FN_PURE ulong
     768             : fd_topo_huge_page_cnt( fd_topo_t * topo,
     769             :                        ulong       numa_idx,
     770             :                        int         include_anonymous );
     771             : 
     772             : /* Check all invariants of the given topology to make sure it is valid.
     773             :    An invalid topology will cause the program to abort with an error
     774             :    message. */
     775             : void
     776             : fd_topo_validate( fd_topo_t const * topo );
     777             : 
     778             : /* Prints a message describing the topology to an output stream.  If
     779             :    stdout is true, will be written to stdout, otherwise will be written
     780             :    as a NOTICE log message to the log file. */
     781             : void
     782             : fd_topo_print_log( int         stdout,
     783             :                    fd_topo_t * topo );
     784             : 
     785             : FD_PROTOTYPES_END
     786             : 
     787             : #endif /* HEADER_fd_src_disco_topo_fd_topo_h */

Generated by: LCOV version 1.14