Line data Source code
1 : #include "../../shared/fd_config.h"
2 : #include "../../shared/fd_action.h"
3 :
4 : #include "../../platform/fd_sys_util.h"
5 : #include "../../platform/fd_net_util.h"
6 : #include "../../shared/commands/ready.h"
7 : #include "../../../ballet/base64/fd_base64.h"
8 : #include "../../../waltz/quic/fd_quic.h"
9 : #include "../../../waltz/quic/tests/fd_quic_test_helpers.h"
10 : #include "../../../waltz/tls/test_tls_helper.h"
11 : #include "../../../util/net/fd_ip4.h"
12 :
13 : #include <errno.h>
14 : #include <sys/random.h>
15 : #include <linux/capability.h>
16 :
17 : FD_IMPORT_BINARY(sample_transaction, "src/waltz/quic/tests/quic_txn.bin");
18 :
19 : static int g_conn_hs_complete = 0;
20 : static int g_conn_final = 0;
21 : static ulong g_stream_notify = 0UL;
22 :
23 : #define MAX_TXN_COUNT 128
24 :
25 : void
26 : txn_cmd_args( int * pargc,
27 : char *** pargv,
28 0 : args_t * args ) {
29 0 : args->txn.payload_base64 = fd_env_strip_cmdline_cstr( pargc, pargv, "--payload-base64-encoded", NULL, NULL );
30 0 : args->txn.count = fd_env_strip_cmdline_ulong( pargc, pargv, "--count", NULL, 1 );
31 0 : if( FD_UNLIKELY( !args->txn.count || args->txn.count > MAX_TXN_COUNT ) )
32 0 : FD_LOG_ERR(( "count must be between 1 and %d", MAX_TXN_COUNT ));
33 :
34 0 : args->txn.dst_ip = fd_env_strip_cmdline_cstr( pargc, pargv, "--dst-ip", NULL, 0 );
35 0 : args->txn.dst_port = fd_env_strip_cmdline_ushort( pargc, pargv, "--dst-port", NULL, 0 );
36 0 : }
37 :
38 : static void
39 : cb_conn_hs_complete( fd_quic_conn_t * conn,
40 0 : void * quic_ctx ) {
41 0 : (void)conn;
42 0 : (void)quic_ctx;
43 0 : g_conn_hs_complete = 1;
44 0 : }
45 :
46 : static void
47 : cb_conn_final( fd_quic_conn_t * conn,
48 0 : void * quic_ctx ) {
49 0 : (void)conn;
50 0 : (void)quic_ctx;
51 0 : g_conn_final = 1;
52 0 : }
53 :
54 : static void
55 : cb_stream_notify( fd_quic_stream_t * stream,
56 : void * stream_ctx,
57 0 : int notify_type ) {
58 0 : (void)stream;
59 0 : (void)stream_ctx;
60 0 : (void)notify_type;
61 0 : g_stream_notify += 1;
62 0 : }
63 :
64 : static void
65 : send_quic_transactions( fd_quic_t * quic,
66 : fd_quic_udpsock_t * udpsock,
67 : ulong count,
68 : uint dst_ip,
69 : ushort dst_port,
70 0 : fd_aio_pkt_info_t * pkt ) {
71 0 : fd_quic_set_aio_net_tx( quic, udpsock->aio );
72 0 : FD_TEST( fd_quic_init( quic ) );
73 :
74 0 : quic->cb.conn_final = cb_conn_final;
75 0 : quic->cb.conn_hs_complete = cb_conn_hs_complete;
76 0 : quic->cb.stream_notify = cb_stream_notify;
77 :
78 0 : fd_quic_conn_t * conn = fd_quic_connect( quic, dst_ip, dst_port, 0U, (ushort)udpsock->listen_port, fd_log_wallclock() );
79 0 : while( FD_LIKELY( !( g_conn_hs_complete || g_conn_final ) ) ) {
80 0 : fd_quic_service( quic, fd_log_wallclock() );
81 0 : fd_quic_udpsock_service( udpsock );
82 0 : }
83 0 : FD_TEST( conn );
84 0 : if( FD_UNLIKELY( conn->state != FD_QUIC_CONN_STATE_ACTIVE ) )
85 0 : FD_LOG_ERR(( "unable to connect to QUIC endpoint at "FD_IP4_ADDR_FMT":%hu, is it running? state is %u", FD_IP4_ADDR_FMT_ARGS(dst_ip), dst_port, conn->state ));
86 :
87 0 : ulong sent = 0;
88 0 : while( sent < count && !g_conn_final ) {
89 0 : long now = fd_log_wallclock();
90 0 : fd_quic_stream_t * stream = fd_quic_conn_new_stream( conn );
91 0 : if( FD_UNLIKELY( !stream ) ) {
92 0 : fd_quic_service( quic, now );
93 0 : fd_quic_udpsock_service( udpsock );
94 0 : continue;
95 0 : }
96 :
97 0 : fd_aio_pkt_info_t * chunk = pkt + sent;
98 0 : int res = fd_quic_stream_send( stream, chunk->buf, chunk->buf_sz, 1 );
99 0 : if( FD_UNLIKELY( res != FD_QUIC_SUCCESS ) ) FD_LOG_ERR(( "fd_quic_stream_send failed (%d)", res ));
100 0 : sent += 1UL;
101 :
102 0 : fd_quic_service( quic, now );
103 0 : fd_quic_udpsock_service( udpsock );
104 0 : }
105 :
106 0 : while( FD_LIKELY( g_stream_notify!=count && !g_conn_final ) ) {
107 0 : fd_quic_service( quic, fd_log_wallclock() );
108 0 : fd_quic_udpsock_service( udpsock );
109 0 : }
110 :
111 : /* close and wait for connection to complete */
112 0 : if( !g_conn_final ) {
113 0 : fd_quic_conn_close( conn, 0 );
114 0 : while( !g_conn_final ) {
115 0 : fd_quic_service( quic, fd_log_wallclock() );
116 0 : fd_quic_udpsock_service( udpsock );
117 0 : }
118 0 : }
119 :
120 0 : fd_quic_fini( quic );
121 0 : }
122 :
123 : void
124 : txn_cmd_fn( args_t * args,
125 0 : config_t * config ) {
126 : /* wait until validator is ready to receive txns before sending */
127 0 : ready_cmd_fn( args, config );
128 :
129 0 : fd_quic_limits_t quic_limits = {
130 0 : .conn_cnt = 1UL,
131 0 : .handshake_cnt = 1UL,
132 0 : .conn_id_cnt = 4UL,
133 0 : .stream_id_cnt = 64UL,
134 0 : .inflight_frame_cnt = 64UL,
135 0 : .tx_buf_sz = fd_ulong_pow2_up( FD_TXN_MTU ),
136 0 : .stream_pool_cnt = 16UL
137 0 : };
138 0 : ulong quic_footprint = fd_quic_footprint( &quic_limits );
139 0 : FD_TEST( quic_footprint );
140 :
141 0 : fd_wksp_t * wksp = fd_wksp_new_anonymous( FD_SHMEM_NORMAL_PAGE_SZ,
142 0 : 1UL << 10,
143 0 : fd_shmem_cpu_idx( 0 ),
144 0 : "wksp",
145 0 : 0UL );
146 0 : FD_TEST( wksp );
147 0 : void * mem = fd_wksp_alloc_laddr( wksp, fd_quic_align(), quic_footprint, 1UL );
148 0 : fd_quic_t * quic = fd_quic_join( fd_quic_new( mem, &quic_limits ) );
149 0 : FD_TEST( quic );
150 :
151 : /* Signer */
152 0 : fd_rng_t _rng[1]; fd_rng_t * rng = fd_rng_join( fd_rng_new( _rng, 0U, 0UL ) );
153 0 : fd_tls_test_sign_ctx_t * sign_ctx = fd_wksp_alloc_laddr( wksp, alignof(fd_tls_test_sign_ctx_t), sizeof(fd_tls_test_sign_ctx_t), 1UL );
154 0 : fd_tls_test_sign_ctx( sign_ctx, rng );
155 :
156 0 : fd_memcpy( quic->config.identity_public_key, sign_ctx->public_key, 32UL );
157 0 : quic->config.sign_ctx = sign_ctx;
158 0 : quic->config.sign = fd_tls_test_sign_sign;
159 :
160 0 : fd_quic_udpsock_t _udpsock;
161 0 : fd_quic_udpsock_t * udpsock = fd_quic_client_create_udpsock( &_udpsock, wksp, fd_quic_get_aio_net_rx( quic ), 0 );
162 0 : FD_TEST( udpsock == &_udpsock );
163 :
164 0 : fd_quic_config_t * client_cfg = &quic->config;
165 0 : client_cfg->role = FD_QUIC_ROLE_CLIENT;
166 0 : client_cfg->idle_timeout = 200UL * 1000UL * 1000UL; /* 5000 millis */
167 0 : client_cfg->initial_rx_max_stream_data = 0; /* doesn't receive */
168 :
169 0 : fd_aio_pkt_info_t pkt[ MAX_TXN_COUNT ];
170 :
171 0 : if( FD_LIKELY( !args->txn.payload_base64 ) ) {
172 0 : FD_LOG_INFO(( "Transaction payload not specified, using hardcoded sample payload" ));
173 0 : for( ulong i=0; i<args->txn.count; i++ ) {
174 0 : pkt[ i ].buf = (void * )sample_transaction;
175 0 : pkt[ i ].buf_sz = (ushort )sample_transaction_sz;
176 0 : }
177 0 : } else {
178 0 : ulong payload_b64_sz = strlen( args->txn.payload_base64 );
179 :
180 0 : static uchar buf[ 1UL << 15UL ];
181 0 : if( FD_UNLIKELY( FD_BASE64_DEC_SZ( payload_b64_sz ) > sizeof(buf) ) )
182 0 : FD_LOG_ERR(( "Input payload is too large (max %lu bytes)", sizeof(buf) ));
183 :
184 0 : long buf_sz = fd_base64_decode( buf, args->txn.payload_base64, payload_b64_sz );
185 0 : if( FD_UNLIKELY( buf_sz<0L ) ) FD_LOG_ERR(( "bad payload input `%s`", args->txn.payload_base64 ));
186 :
187 0 : for( ulong i=0; i<args->txn.count; i++ ) {
188 0 : pkt[ i ].buf = (void * )buf;
189 0 : pkt[ i ].buf_sz = (ushort )buf_sz;
190 0 : }
191 0 : }
192 :
193 0 : uint dst_ip = config->net.ip_addr;
194 0 : if( FD_UNLIKELY( args->txn.dst_ip ) )
195 0 : if( FD_UNLIKELY( !fd_cstr_to_ip4_addr( args->txn.dst_ip, &dst_ip ) ) ) FD_LOG_ERR(( "invalid --dst-ip" ));
196 :
197 0 : ushort dst_port = config->tiles.quic.quic_transaction_listen_port;
198 0 : if( FD_UNLIKELY( args->txn.dst_port ) ) dst_port = args->txn.dst_port;
199 :
200 0 : FD_LOG_NOTICE(( "sending %lu transactions to "FD_IP4_ADDR_FMT":%hu", args->txn.count, FD_IP4_ADDR_FMT_ARGS(dst_ip), dst_port ));
201 :
202 0 : send_quic_transactions( quic, udpsock, args->txn.count, dst_ip, dst_port, pkt );
203 0 : fd_sys_util_exit_group( 0 );
204 0 : }
205 :
206 : action_t fd_action_txn = {
207 : .name = "txn",
208 : .args = txn_cmd_args,
209 : .fn = txn_cmd_fn,
210 : .description = "Send a transaction to an fddev instance"
211 : };
|