LCOV - code coverage report
Current view: top level - app/fdctl/run/tiles - fd_gui.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 180 0.0 %
Date: 2024-11-13 11:58:15 Functions: 0 12 0.0 %

          Line data    Source code
       1             : #include "../../../../disco/tiles.h"
       2             : 
       3             : /* The frontend assets are pre-built and statically compiled into the
       4             :    binary here.  To regenerate them, run
       5             : 
       6             :     $ git clone https://github.com/firedancer-io/firedancer-frontend.git frontend
       7             :     $ make frontend
       8             : 
       9             :    from the repository root. */
      10             : 
      11             : #include "generated/http_import_dist.h"
      12             : 
      13             : #include <sys/socket.h> /* SOCK_CLOEXEC, SOCK_NONBLOCK needed for seccomp filter */
      14             : #if defined(__aarch64__)
      15             : #include "generated/gui.arm64_seccomp.h"
      16             : #else
      17             : #include "generated/gui_seccomp.h"
      18             : #endif
      19             : 
      20             : #include "../../version.h"
      21             : 
      22             : #include "../../../../disco/keyguard/fd_keyload.h"
      23             : #include "../../../../disco/shred/fd_stake_ci.h"
      24             : #include "../../../../disco/gui/fd_gui.h"
      25             : #include "../../../../disco/plugin/fd_plugin.h"
      26             : #include "../../../../ballet/base58/fd_base58.h"
      27             : #include "../../../../ballet/http/fd_http_server.h"
      28             : 
      29             : #include <errno.h>
      30             : #include <sys/types.h>
      31             : #include <unistd.h>
      32             : #include <string.h>
      33             : #include <poll.h>
      34             : #include <stdio.h>
      35             : 
      36             : FD_IMPORT_BINARY( firedancer_svg, "book/public/fire.svg" );
      37             : 
      38             : #define FD_HTTP_SERVER_GUI_MAX_CONNS             1024
      39             : #define FD_HTTP_SERVER_GUI_MAX_REQUEST_LEN       1024
      40             : #define FD_HTTP_SERVER_GUI_MAX_WS_CONNS          1024
      41             : #define FD_HTTP_SERVER_GUI_MAX_WS_RECV_FRAME_LEN 1024
      42             : #define FD_HTTP_SERVER_GUI_MAX_WS_SEND_FRAME_CNT 8192
      43             : #define FD_HTTP_SERVER_GUI_OUTGOING_BUFFER_SZ    (5UL<<30UL) /* 5GiB reserved for buffering GUI websockets */
      44             : 
      45             : const fd_http_server_params_t GUI_PARAMS = {
      46             :   .max_connection_cnt    = FD_HTTP_SERVER_GUI_MAX_CONNS,
      47             :   .max_ws_connection_cnt = FD_HTTP_SERVER_GUI_MAX_WS_CONNS,
      48             :   .max_request_len       = FD_HTTP_SERVER_GUI_MAX_REQUEST_LEN,
      49             :   .max_ws_recv_frame_len = FD_HTTP_SERVER_GUI_MAX_WS_RECV_FRAME_LEN,
      50             :   .max_ws_send_frame_cnt = FD_HTTP_SERVER_GUI_MAX_WS_SEND_FRAME_CNT,
      51             :   .outgoing_buffer_sz    = FD_HTTP_SERVER_GUI_OUTGOING_BUFFER_SZ,
      52             : };
      53             : 
      54             : typedef struct {
      55             :   fd_topo_t * topo;
      56             : 
      57             :   fd_gui_t * gui;
      58             : 
      59             :   /* This needs to be max(plugin_msg) across all kinds of messages.
      60             :      Currently this is just figured out manually, it's a gossip update
      61             :      message assuming the table is completely full (40200) of peers. */
      62             :   uchar      buf[ 8UL+40200UL*(58UL+12UL*34UL) ] __attribute__((aligned(8)));
      63             : 
      64             :   fd_http_server_t * gui_server;
      65             : 
      66             :   char          version_string[ 16UL ];
      67             :   uchar const * identity_key;
      68             : 
      69             :   fd_wksp_t * in_mem;
      70             :   ulong       in_chunk0;
      71             :   ulong       in_wmark;
      72             : } fd_gui_ctx_t;
      73             : 
      74             : FD_FN_CONST static inline ulong
      75           0 : scratch_align( void ) {
      76           0 :   return 128UL;
      77           0 : }
      78             : 
      79             : FD_FN_PURE static inline ulong
      80           0 : scratch_footprint( fd_topo_tile_t const * tile ) {
      81           0 :   (void)tile;
      82             : 
      83           0 :   ulong l = FD_LAYOUT_INIT;
      84           0 :   l = FD_LAYOUT_APPEND( l, alignof( fd_gui_ctx_t ), sizeof( fd_gui_ctx_t ) );
      85           0 :   l = FD_LAYOUT_APPEND( l, fd_http_server_align(),  fd_http_server_footprint( GUI_PARAMS ) );
      86           0 :   l = FD_LAYOUT_APPEND( l, fd_gui_align(),          fd_gui_footprint() );
      87           0 :   return FD_LAYOUT_FINI( l, scratch_align() );
      88           0 : }
      89             : 
      90             : static int
      91             : before_credit( fd_gui_ctx_t *      ctx,
      92             :                fd_stem_context_t * stem,
      93           0 :                int *               charge_busy ) {
      94           0 :   (void)stem;
      95             : 
      96           0 :   int charge_busy_server = fd_http_server_poll( ctx->gui_server );
      97           0 :   int charge_poll        = fd_gui_poll( ctx->gui );
      98             : 
      99           0 :   *charge_busy = charge_busy_server | charge_poll;
     100             : 
     101           0 :   return 0;
     102           0 : }
     103             : 
     104             : static inline void
     105             : during_frag( fd_gui_ctx_t * ctx,
     106             :              ulong          in_idx,
     107             :              ulong          seq,
     108             :              ulong          sig,
     109             :              ulong          chunk,
     110           0 :              ulong          sz ) {
     111           0 :   (void)in_idx;
     112           0 :   (void)seq;
     113           0 :   (void)sig;
     114             : 
     115           0 :   uchar * src = (uchar *)fd_chunk_to_laddr( ctx->in_mem, chunk );
     116             : 
     117             :    /* ... todo... sigh, sz is not correct since it's too big */
     118           0 :   if( sig==FD_PLUGIN_MSG_GOSSIP_UPDATE || sig==FD_PLUGIN_MSG_VOTE_ACCOUNT_UPDATE || sig==FD_PLUGIN_MSG_VALIDATOR_INFO ) {
     119           0 :     ulong peer_cnt = ((ulong *)src)[ 0 ];
     120           0 :     FD_TEST( peer_cnt<=40200 );
     121           0 :     sz = 8UL + peer_cnt*(58UL+12UL*34UL);
     122           0 :   } else if( sig==FD_PLUGIN_MSG_LEADER_SCHEDULE ) {
     123           0 :     ulong leader_cnt = ((ulong *)src)[ 1 ];
     124           0 :     FD_TEST( leader_cnt<=40200 );
     125           0 :     sz = 40UL + leader_cnt*40UL;
     126           0 :   }
     127             : 
     128           0 :   if( FD_UNLIKELY( chunk<ctx->in_chunk0 || chunk>ctx->in_wmark || sz>sizeof( ctx->buf ) ) )
     129           0 :     FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, ctx->in_chunk0, ctx->in_wmark ));
     130             : 
     131           0 :   fd_memcpy( ctx->buf, src, sz );
     132           0 : }
     133             : 
     134             : static inline void
     135             : after_frag( fd_gui_ctx_t *      ctx,
     136             :             ulong               in_idx,
     137             :             ulong               seq,
     138             :             ulong               sig,
     139             :             ulong               chunk,
     140             :             ulong               sz,
     141             :             ulong               tsorig,
     142           0 :             fd_stem_context_t * stem ) {
     143           0 :   (void)in_idx;
     144           0 :   (void)seq;
     145           0 :   (void)chunk;
     146           0 :   (void)sz;
     147           0 :   (void)tsorig;
     148           0 :   (void)stem;
     149             : 
     150           0 :   fd_gui_plugin_message( ctx->gui, sig, ctx->buf );
     151           0 : }
     152             : 
     153             : static fd_http_server_response_t
     154           0 : gui_http_request( fd_http_server_request_t const * request ) {
     155           0 :   if( FD_UNLIKELY( request->method!=FD_HTTP_SERVER_METHOD_GET ) ) {
     156           0 :     return (fd_http_server_response_t){
     157           0 :       .status = 405,
     158           0 :     };
     159           0 :   }
     160             : 
     161           0 :   if( FD_LIKELY( !strcmp( request->path, "/websocket" ) ) ) {
     162           0 :     return (fd_http_server_response_t){
     163           0 :       .status            = 200,
     164           0 :       .upgrade_websocket = 1,
     165           0 :     };
     166           0 :   } else if( FD_LIKELY( !strcmp( request->path, "/favicon.svg" ) ) ) {
     167           0 :     return (fd_http_server_response_t){
     168           0 :       .status            = 200,
     169           0 :       .static_body       = firedancer_svg,
     170           0 :       .static_body_len   = firedancer_svg_sz,
     171           0 :       .content_type      = "image/svg+xml",
     172           0 :       .upgrade_websocket = 0,
     173           0 :     };
     174           0 :   }
     175             : 
     176           0 :   int is_vite_page = !strcmp( request->path, "/" ) ||
     177           0 :                      !strcmp( request->path, "/leaderSchedule" ) ||
     178           0 :                      !strcmp( request->path, "/gossip" );
     179             : 
     180           0 :   for( ulong i=0UL; i<sizeof(STATIC_FILES)/sizeof(STATIC_FILES[0]); i++ ) {
     181           0 :     if( !strcmp( request->path, STATIC_FILES[ i ].name ) ||
     182           0 :         (!strcmp( STATIC_FILES[ i ].name, "/index.html" ) && is_vite_page) ) {
     183           0 :       char const * content_type = NULL;
     184             : 
     185           0 :       char const * ext = strrchr( STATIC_FILES[ i ].name, '.' );
     186           0 :       if( FD_LIKELY( ext ) ) {
     187           0 :         if( !strcmp( ext, ".html" ) ) content_type = "text/html; charset=utf-8";
     188           0 :         else if( !strcmp( ext, ".css" ) ) content_type = "text/css";
     189           0 :         else if( !strcmp( ext, ".js" ) ) content_type = "application/javascript";
     190           0 :         else if( !strcmp( ext, ".svg" ) ) content_type = "image/svg+xml";
     191           0 :         else if( !strcmp( ext, ".woff" ) ) content_type = "font/woff";
     192           0 :         else if( !strcmp( ext, ".woff2" ) ) content_type = "font/woff2";
     193           0 :       }
     194             : 
     195           0 :       char const * cache_control = NULL;
     196           0 :       if( FD_LIKELY( !strncmp( request->path, "/assets", 7 ) ) ) cache_control = "public, max-age=31536000, immutable";
     197             : 
     198           0 :       const uchar * data = STATIC_FILES[ i ].data;
     199           0 :       ulong data_len = *(STATIC_FILES[ i ].data_len);
     200             : 
     201           0 :       int accepts_zstd = 0;
     202           0 :       if( FD_LIKELY( request->headers.accept_encoding ) ) {
     203           0 :         accepts_zstd = !!strstr( request->headers.accept_encoding, "zstd" );
     204           0 :       }
     205             : 
     206           0 :       char const * content_encoding = NULL;
     207           0 :       if( FD_LIKELY( accepts_zstd && STATIC_FILES[ i ].zstd_data ) ) {
     208           0 :         content_encoding = "zstd";
     209           0 :         data = STATIC_FILES[ i ].zstd_data;
     210           0 :         data_len = *(STATIC_FILES[ i ].zstd_data_len);
     211           0 :       }
     212             : 
     213           0 :       return (fd_http_server_response_t){
     214           0 :         .status            = 200,
     215           0 :         .static_body       = data,
     216           0 :         .static_body_len   = data_len,
     217           0 :         .content_type      = content_type,
     218           0 :         .cache_control     = cache_control,
     219           0 :         .content_encoding  = content_encoding,
     220           0 :         .upgrade_websocket = 0,
     221           0 :       };
     222           0 :     }
     223           0 :   }
     224             : 
     225           0 :   return (fd_http_server_response_t){
     226           0 :     .status            = 404,
     227           0 :   };
     228           0 : }
     229             : 
     230             : static void
     231             : gui_ws_open( ulong  conn_id,
     232           0 :              void * _ctx ) {
     233           0 :   fd_gui_ctx_t * ctx = (fd_gui_ctx_t *)_ctx;
     234             : 
     235           0 :   fd_gui_ws_open( ctx->gui, conn_id );
     236           0 : }
     237             : 
     238             : static void
     239             : gui_ws_message( ulong         ws_conn_id,
     240             :                 uchar const * data,
     241             :                 ulong         data_len,
     242           0 :                 void *        _ctx ) {
     243           0 :   fd_gui_ctx_t * ctx = (fd_gui_ctx_t *)_ctx;
     244             : 
     245           0 :   int close = fd_gui_ws_message( ctx->gui, ws_conn_id, data, data_len );
     246           0 :   if( FD_UNLIKELY( close<0 ) ) fd_http_server_ws_close( ctx->gui_server, ws_conn_id, close );
     247           0 : }
     248             : 
     249             : static void
     250             : privileged_init( fd_topo_t *      topo,
     251           0 :                  fd_topo_tile_t * tile ) {
     252           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     253             : 
     254           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     255           0 :   fd_gui_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_gui_ctx_t ), sizeof( fd_gui_ctx_t ) );
     256             : 
     257           0 :   fd_http_server_t * _gui = FD_SCRATCH_ALLOC_APPEND( l, fd_http_server_align(), fd_http_server_footprint( GUI_PARAMS ) );
     258             : 
     259           0 :   fd_http_server_callbacks_t gui_callbacks = {
     260           0 :     .request    = gui_http_request,
     261           0 :     .ws_open    = gui_ws_open,
     262           0 :     .ws_message = gui_ws_message,
     263           0 :   };
     264           0 :   ctx->gui_server = fd_http_server_join( fd_http_server_new( _gui, GUI_PARAMS, gui_callbacks, ctx ) );
     265           0 :   fd_http_server_listen( ctx->gui_server, tile->gui.listen_addr, tile->gui.listen_port );
     266             : 
     267           0 :   if( FD_UNLIKELY( !strcmp( tile->gui.identity_key_path, "" ) ) )
     268           0 :     FD_LOG_ERR(( "identity_key_path not set" ));
     269             : 
     270           0 :   ctx->identity_key = fd_keyload_load( tile->gui.identity_key_path, /* pubkey only: */ 1 );
     271           0 : }
     272             : 
     273             : static void
     274             : unprivileged_init( fd_topo_t *      topo,
     275           0 :                    fd_topo_tile_t * tile ) {
     276           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     277             : 
     278           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     279           0 :   fd_gui_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_gui_ctx_t ), sizeof( fd_gui_ctx_t ) );
     280           0 :                        FD_SCRATCH_ALLOC_APPEND( l, fd_http_server_align(), fd_http_server_footprint( GUI_PARAMS ) );
     281           0 :   void * _gui        = FD_SCRATCH_ALLOC_APPEND( l, fd_gui_align(),         fd_gui_footprint() );
     282             : 
     283           0 :   FD_TEST( fd_cstr_printf_check( ctx->version_string, sizeof( ctx->version_string ), NULL, "%lu.%lu.%lu", FDCTL_MAJOR_VERSION, FDCTL_MINOR_VERSION, FDCTL_PATCH_VERSION ) );
     284             : 
     285           0 :   ctx->topo = topo;
     286           0 :   ctx->gui  = fd_gui_join( fd_gui_new( _gui, ctx->gui_server, ctx->version_string, tile->gui.cluster, ctx->identity_key, tile->gui.is_voting, ctx->topo ) );
     287           0 :   FD_TEST( ctx->gui );
     288             : 
     289           0 :   fd_topo_link_t * link = &topo->links[ tile->in_link_id[ 0 ] ];
     290           0 :   fd_topo_wksp_t * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
     291             : 
     292           0 :   ctx->in_mem    = link_wksp->wksp;
     293           0 :   ctx->in_chunk0 = fd_dcache_compact_chunk0( ctx->in_mem, link->dcache );
     294           0 :   ctx->in_wmark  = fd_dcache_compact_wmark ( ctx->in_mem, link->dcache, link->mtu );
     295             : 
     296           0 :   ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
     297           0 :   if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
     298           0 :     FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
     299             : 
     300           0 :   FD_LOG_WARNING(( "GUI server listening at http://" FD_IP4_ADDR_FMT ":%u", FD_IP4_ADDR_FMT_ARGS( tile->gui.listen_addr ), tile->gui.listen_port ));
     301           0 : }
     302             : 
     303             : static ulong
     304             : populate_allowed_seccomp( fd_topo_t const *      topo,
     305             :                           fd_topo_tile_t const * tile,
     306             :                           ulong                  out_cnt,
     307           0 :                           struct sock_filter *   out ) {
     308           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     309           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     310           0 :   fd_gui_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_gui_ctx_t ), sizeof( fd_gui_ctx_t ) );
     311             : 
     312           0 :   populate_sock_filter_policy_gui( out_cnt, out, (uint)fd_log_private_logfile_fd(), (uint)fd_http_server_fd( ctx->gui_server ) );
     313           0 :   return sock_filter_policy_gui_instr_cnt;
     314           0 : }
     315             : 
     316             : static ulong
     317             : populate_allowed_fds( fd_topo_t const *      topo,
     318             :                       fd_topo_tile_t const * tile,
     319             :                       ulong                  out_fds_cnt,
     320           0 :                       int *                  out_fds ) {
     321           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     322           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     323           0 :   fd_gui_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_gui_ctx_t ), sizeof( fd_gui_ctx_t ) );
     324             : 
     325           0 :   if( FD_UNLIKELY( out_fds_cnt<3UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
     326             : 
     327           0 :   ulong out_cnt = 0UL;
     328           0 :   out_fds[ out_cnt++ ] = 2; /* stderr */
     329           0 :   if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
     330           0 :     out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
     331           0 :   out_fds[ out_cnt++ ] = fd_http_server_fd( ctx->gui_server ); /* gui listen socket */
     332           0 :   return out_cnt;
     333           0 : }
     334             : 
     335           0 : #define STEM_BURST (1UL)
     336             : 
     337           0 : #define STEM_CALLBACK_CONTEXT_TYPE  fd_gui_ctx_t
     338           0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_gui_ctx_t)
     339             : 
     340           0 : #define STEM_CALLBACK_BEFORE_CREDIT before_credit
     341           0 : #define STEM_CALLBACK_DURING_FRAG   during_frag
     342           0 : #define STEM_CALLBACK_AFTER_FRAG    after_frag
     343             : 
     344             : #include "../../../../disco/stem/fd_stem.c"
     345             : 
     346             : fd_topo_run_tile_t fd_tile_gui = {
     347             :   .name                     = "gui",
     348             :   .rlimit_file_cnt          = FD_HTTP_SERVER_GUI_MAX_CONNS+FD_HTTP_SERVER_GUI_MAX_WS_CONNS+5UL, /* pipefd, socket, stderr, logfile, and one spare for new accept() connections */
     349             :   .populate_allowed_seccomp = populate_allowed_seccomp,
     350             :   .populate_allowed_fds     = populate_allowed_fds,
     351             :   .scratch_align            = scratch_align,
     352             :   .scratch_footprint        = scratch_footprint,
     353             :   .privileged_init          = privileged_init,
     354             :   .unprivileged_init        = unprivileged_init,
     355             :   .run                      = stem_run,
     356             : };

Generated by: LCOV version 1.14