Line data Source code
1 : /* _GNU_SOURCE for recvmmsg and sendmmsg */
2 : #define _GNU_SOURCE
3 :
4 : #include "../../../disco/topo/fd_topo.h"
5 : #include "../../../waltz/quic/fd_quic.h"
6 : #include "../../../waltz/tls/test_tls_helper.h"
7 :
8 : #include <errno.h>
9 : #include <linux/unistd.h>
10 : #include <sys/types.h>
11 : #include <sys/socket.h>
12 : #include <netinet/in.h>
13 : #include <string.h>
14 : #include <unistd.h>
15 : #include <poll.h>
16 :
17 : #include <stdio.h>
18 : #include <stdlib.h>
19 :
20 : #include <time.h>
21 :
22 : /* max number of buffers batched for receive */
23 0 : #define IO_VEC_CNT 128
24 :
25 :
26 : struct signer_ctx {
27 : fd_sha512_t sha512[ 1 ];
28 :
29 : uchar public_key[ 32UL ];
30 : uchar private_key[ 32UL ];
31 : };
32 : typedef struct signer_ctx signer_ctx_t;
33 :
34 :
35 : static void
36 : signer( void * _ctx,
37 : uchar signature[ static 64 ],
38 0 : uchar const payload[ static 130 ] ) {
39 0 : fd_tls_test_sign_ctx_t * ctx = (fd_tls_test_sign_ctx_t *)_ctx;
40 0 : fd_ed25519_sign( signature, payload, 130UL, ctx->public_key, ctx->private_key, ctx->sha512 );
41 0 : }
42 :
43 : static FD_FN_UNUSED
44 : signer_ctx_t
45 0 : signer_ctx( fd_rng_t * rng ) {
46 0 : signer_ctx_t ctx[1];
47 0 : FD_TEST( fd_sha512_join( fd_sha512_new( ctx->sha512 ) ) );
48 0 : for( ulong b=0; b<32UL; b++ ) ctx->private_key[b] = fd_rng_uchar( rng );
49 0 : fd_ed25519_public_from_private( ctx->public_key, ctx->private_key, ctx->sha512 );
50 :
51 0 : return *ctx;
52 0 : }
53 :
54 : static int
55 : quic_tx_aio_send( void * _ctx,
56 : fd_aio_pkt_info_t const * batch,
57 : ulong batch_cnt,
58 : ulong * opt_batch_idx,
59 : int flush );
60 :
61 :
62 : /* quic_now is called by the QUIC engine to get the current timestamp in
63 : UNIX time. */
64 :
65 : static ulong
66 0 : quic_now( void * ctx ) {
67 0 : (void)ctx;
68 0 : return (ulong)fd_log_wallclock();
69 0 : }
70 :
71 : typedef struct {
72 : ulong round_robin_cnt;
73 : ulong round_robin_id;
74 :
75 : ulong packet_cnt;
76 :
77 : ulong conn_cnt;
78 : int conn_fd[ 128UL ];
79 : struct pollfd poll_fd[ 128UL ];
80 :
81 : signer_ctx_t signer_ctx;
82 : int no_quic;
83 : fd_quic_t * quic;
84 : ushort quic_port;
85 : fd_quic_conn_t * quic_conn;
86 : const fd_aio_t * quic_rx_aio;
87 : ulong no_stream;
88 : uint service_ratio_idx;
89 :
90 : // vector receive members
91 : struct mmsghdr rx_msgs[IO_VEC_CNT];
92 : struct mmsghdr tx_msgs[IO_VEC_CNT];
93 : struct iovec rx_iovecs[IO_VEC_CNT];
94 : struct iovec tx_iovecs[IO_VEC_CNT];
95 : char rx_bufs[IO_VEC_CNT][2048];
96 : char tx_bufs[IO_VEC_CNT][2048];
97 :
98 : ulong tx_idx;
99 :
100 : fd_quic_stream_t * stream;
101 :
102 : fd_wksp_t * mem;
103 : } fd_benchs_ctx_t;
104 :
105 : void
106 0 : service_quic( fd_benchs_ctx_t * ctx ) {
107 :
108 0 : if( !ctx->no_quic ) {
109 : /* Publishes to mcache via callbacks */
110 :
111 : /* receive from socket, and pass to quic */
112 0 : int poll_rc = poll( ctx->poll_fd, ctx->conn_cnt, 0 );
113 0 : if( FD_LIKELY( poll_rc == 0 ) ) {
114 0 : return;
115 0 : } if( FD_UNLIKELY( poll_rc == -1 ) ) {
116 0 : if( FD_UNLIKELY( errno == EINTR ) ) return; /* will try later */
117 0 : FD_LOG_ERR(( "Error occurred during poll: %d %s", errno,
118 0 : strerror( errno ) ));
119 0 : }
120 :
121 0 : for( ulong j = 0; j < ctx->conn_cnt; ++j ) {
122 0 : int revents = ctx->poll_fd[j].revents;
123 0 : if( FD_LIKELY( revents & POLLIN ) ) {
124 : /* data available - receive up to IO_VEC_CNT buffers */
125 0 : struct timespec timeout = {0};
126 0 : int retval = recvmmsg( ctx->poll_fd[j].fd, ctx->rx_msgs, IO_VEC_CNT, 0, &timeout );
127 0 : if( FD_UNLIKELY( retval < 0 ) ) {
128 0 : FD_LOG_ERR(( "Error occurred on recvmmsg: %d %s", errno, strerror( errno ) ));
129 0 : }
130 : /* pass buffers to QUIC */
131 0 : fd_aio_pkt_info_t pkt[IO_VEC_CNT];
132 0 : ulong hdr_sz = 20 + 8;
133 0 : for( ulong k = 0; k < (ulong)retval; k++ ) {
134 0 : pkt[j].buf = ctx->rx_bufs[k];
135 0 : pkt[j].buf_sz = (ushort)( ctx->rx_msgs[k].msg_len + hdr_sz );
136 :
137 : /* set some required values */
138 0 : uint payload_len = ctx->rx_msgs[k].msg_len;
139 0 : uint udp_len = payload_len + 8;
140 0 : uint ip_len = udp_len + 20;
141 :
142 0 : uchar * buf = pkt[k].buf;
143 :
144 : /* set ver and len */
145 0 : buf[0] = 0x45;
146 :
147 : /* set protocol */
148 0 : buf[9] = 17;
149 :
150 : /* set udp length */
151 0 : buf[20 + 4] = (uchar)( udp_len >> 8 );
152 0 : buf[20 + 5] = (uchar)( udp_len );
153 :
154 : /* set ip length */
155 0 : buf[2] = (uchar)( ip_len >> 8 );
156 0 : buf[3] = (uchar)( ip_len );
157 0 : }
158 0 : fd_aio_send( ctx->quic_rx_aio, pkt, (ulong)retval, NULL, 1 );
159 0 : } else if( FD_UNLIKELY( revents & POLLERR ) ) {
160 0 : int error = 0;
161 0 : socklen_t errlen = sizeof(error);
162 :
163 0 : if( getsockopt( ctx->poll_fd[j].fd, SOL_SOCKET, SO_ERROR, (void *)&error, &errlen ) == -1 ) {
164 0 : FD_LOG_ERR(( "Unknown error on socket" ));
165 0 : } else {
166 0 : FD_LOG_ERR(( "Error on socket: %d %s", error, strerror( error ) ));
167 0 : }
168 0 : }
169 0 : }
170 0 : }
171 0 : }
172 :
173 : static void
174 : quic_stream_notify( fd_quic_stream_t * stream,
175 : void * stream_ctx,
176 0 : int type ) {
177 0 : (void)type;
178 :
179 0 : fd_benchs_ctx_t * ctx = (fd_benchs_ctx_t*)stream_ctx;
180 0 : if( FD_LIKELY( ctx ) ) {
181 0 : if( FD_LIKELY( ctx->stream == stream ) ) {
182 0 : ctx->stream = NULL;
183 0 : }
184 0 : } else {
185 0 : FD_LOG_ERR(( "quic_stream_notify - no context" ));
186 0 : }
187 0 : }
188 :
189 : /* quic_conn_new is invoked by the QUIC engine whenever a new connection
190 : is being established. */
191 : static void
192 : quic_conn_new( fd_quic_conn_t * conn,
193 0 : void * _ctx ) {
194 0 : (void)conn;
195 0 : (void)_ctx;
196 0 : }
197 :
198 :
199 : void
200 : handshake_complete( fd_quic_conn_t * conn,
201 0 : void * _ctx ) {
202 0 : (void)conn;
203 0 : (void)_ctx;
204 0 : FD_LOG_NOTICE(( "client handshake complete" ));
205 0 : }
206 :
207 :
208 : static void
209 : quic_stream_notify( fd_quic_stream_t * stream,
210 : void * stream_ctx,
211 : int type );
212 :
213 : static void
214 : conn_final( fd_quic_conn_t * conn,
215 0 : void * _ctx ) {
216 0 : (void)conn;
217 :
218 0 : fd_benchs_ctx_t * ctx = (fd_benchs_ctx_t *)_ctx;
219 :
220 0 : if( ctx ) {
221 0 : ctx->quic_conn = NULL;
222 0 : ctx->stream = NULL;
223 0 : }
224 0 : }
225 :
226 : FD_FN_CONST static inline ulong
227 0 : scratch_align( void ) {
228 0 : return fd_ulong_max( fd_quic_align(), alignof( fd_benchs_ctx_t ) );
229 0 : }
230 :
231 : void
232 0 : populate_quic_limits( fd_quic_limits_t * limits ) {
233 : //int argc = 0;
234 : //char * args[] = { NULL };
235 : //char ** argv = args;
236 : //fd_quic_limits_from_env( &argc, &argv, limits );
237 0 : limits->conn_cnt = 2;
238 0 : limits->handshake_cnt = limits->conn_cnt;
239 0 : limits->conn_id_cnt = 16;
240 0 : limits->inflight_pkt_cnt = 1500;
241 0 : limits->tx_buf_sz = FD_TXN_MTU;
242 0 : limits->stream_pool_cnt = 1UL<<16;
243 0 : limits->stream_id_cnt = 1UL<<16;
244 0 : }
245 :
246 : void
247 0 : populate_quic_config( fd_quic_config_t * config ) {
248 0 : config->role = FD_QUIC_ROLE_CLIENT;
249 0 : config->retry = 0;
250 0 : config->initial_rx_max_stream_data = 0; /* we don't expect the server to initiate streams */
251 :
252 0 : config->net.ephem_udp_port.lo = 12000;
253 0 : config->net.ephem_udp_port.hi = 12100;
254 :
255 0 : config->net.dscp = 0;
256 0 : }
257 :
258 : static inline ulong
259 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
260 0 : ulong l = FD_LAYOUT_INIT;
261 0 : l = FD_LAYOUT_APPEND( l, alignof( fd_benchs_ctx_t ), sizeof( fd_benchs_ctx_t ) );
262 0 : if( !tile->benchs.no_quic ) {
263 0 : fd_quic_limits_t quic_limits = {0};
264 0 : populate_quic_limits( &quic_limits );
265 0 : ulong quic_fp = fd_quic_footprint( &quic_limits );
266 0 : l = FD_LAYOUT_APPEND( l, fd_quic_align(), quic_fp );
267 0 : l = FD_LAYOUT_APPEND( l, fd_aio_align(), fd_aio_footprint() );
268 0 : }
269 0 : return FD_LAYOUT_FINI( l, scratch_align() );
270 0 : }
271 :
272 : static inline int
273 : before_frag( fd_benchs_ctx_t * ctx,
274 : ulong in_idx,
275 : ulong seq,
276 0 : ulong sig ) {
277 0 : (void)in_idx;
278 0 : (void)sig;
279 :
280 0 : return (int)( (seq%ctx->round_robin_cnt)!=ctx->round_robin_id );
281 0 : }
282 :
283 : static inline void
284 : during_frag( fd_benchs_ctx_t * ctx,
285 : ulong in_idx FD_PARAM_UNUSED,
286 : ulong seq FD_PARAM_UNUSED,
287 : ulong sig FD_PARAM_UNUSED,
288 : ulong chunk,
289 : ulong sz,
290 0 : ulong ctl FD_PARAM_UNUSED ) {
291 0 : if( ctx->no_quic ) {
292 :
293 0 : if( FD_UNLIKELY( -1==send( ctx->conn_fd[ ctx->packet_cnt % ctx->conn_cnt ], fd_chunk_to_laddr( ctx->mem, chunk ), sz, 0 ) ) )
294 0 : FD_LOG_ERR(( "send() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
295 :
296 0 : ctx->packet_cnt++;
297 0 : } else {
298 : /* allows to accumulate multiple transactions before creating a UDP datagram */
299 : /* make this configurable */
300 0 : if( FD_UNLIKELY( ctx->service_ratio_idx++ == 8 ) ) {
301 0 : ctx->service_ratio_idx = 0;
302 0 : service_quic( ctx );
303 0 : fd_quic_service( ctx->quic );
304 0 : }
305 :
306 0 : if( FD_UNLIKELY( !ctx->quic_conn ) ) {
307 0 : ctx->no_stream = 0;
308 :
309 : /* try to connect */
310 0 : uint dest_ip = 0;
311 0 : ushort dest_port = fd_ushort_bswap( ctx->quic_port );
312 :
313 0 : ctx->quic_conn = fd_quic_connect( ctx->quic, dest_ip, dest_port );
314 :
315 : /* failed? try later */
316 0 : if( FD_UNLIKELY( !ctx->quic_conn ) ) {
317 0 : service_quic( ctx );
318 0 : fd_quic_service( ctx->quic );
319 0 : return;
320 0 : }
321 :
322 0 : FD_LOG_NOTICE(( "connection created on port %d", (int)dest_port ));
323 :
324 : /* set the context to point to the location
325 : of the quic_conn pointer
326 : this allows the notification to NULL the value when
327 : a connection dies */
328 0 : fd_quic_conn_set_context( ctx->quic_conn, ctx );
329 :
330 0 : service_quic( ctx );
331 0 : fd_quic_service( ctx->quic );
332 :
333 : /* conn and streams may be invalidated by fd_quic_service */
334 :
335 0 : return;
336 0 : }
337 :
338 0 : fd_quic_stream_t * stream = ctx->stream;
339 0 : if( FD_UNLIKELY( !stream ) ) {
340 0 : ctx->stream = stream = fd_quic_conn_new_stream( ctx->quic_conn );
341 0 : if( FD_LIKELY( stream ) ) {
342 0 : fd_quic_stream_set_context( stream, ctx );
343 0 : }
344 0 : }
345 :
346 0 : if( FD_UNLIKELY( !stream ) ) {
347 0 : ctx->no_stream++;
348 0 : service_quic( ctx );
349 0 : fd_quic_service( ctx->quic );
350 :
351 : /* conn and streams may be invalidated by fd_quic_service */
352 :
353 0 : return;
354 0 : } else {
355 0 : int fin = 1;
356 0 : int rtn = fd_quic_stream_send( stream, fd_chunk_to_laddr( ctx->mem, chunk ), sz, fin );
357 0 : ctx->packet_cnt++;
358 :
359 0 : if( FD_LIKELY( rtn == FD_QUIC_SUCCESS ) ) {
360 : /* after using, fetch a new stream */
361 0 : ctx->stream = stream = fd_quic_conn_new_stream( ctx->quic_conn );
362 0 : if( FD_LIKELY( stream ) ) {
363 0 : fd_quic_stream_set_context( stream, ctx );
364 0 : }
365 0 : } else if( FD_UNLIKELY( rtn == 0 ) ) {
366 0 : FD_LOG_NOTICE(( "fd_quic_stream_send returned zero" ));
367 0 : } else {
368 : /* this can happen dring handshaking */
369 0 : if( rtn != FD_QUIC_SEND_ERR_INVAL_CONN ) {
370 0 : FD_LOG_ERR(( "fd_quic_stream_send failed with: %d", rtn ));
371 0 : }
372 0 : }
373 0 : }
374 0 : }
375 0 : }
376 :
377 : static void
378 : privileged_init( fd_topo_t * topo,
379 0 : fd_topo_tile_t * tile ) {
380 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
381 :
382 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
383 0 : fd_benchs_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_benchs_ctx_t ), sizeof( fd_benchs_ctx_t ) );
384 :
385 0 : int no_quic = ctx->no_quic = tile->benchs.no_quic;
386 :
387 0 : if( !no_quic ) {
388 0 : fd_quic_limits_t quic_limits = {0};
389 0 : populate_quic_limits( &quic_limits );
390 0 : ulong quic_fp = fd_quic_footprint( &quic_limits );
391 0 : if( FD_UNLIKELY( !quic_fp ) ) FD_LOG_ERR(( "invalid QUIC parameters" ));
392 0 : void * quic_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_quic_align(), quic_fp );
393 0 : fd_quic_t * quic = fd_quic_join( fd_quic_new( quic_mem, &quic_limits ) );
394 :
395 0 : populate_quic_config( &quic->config );
396 :
397 : /* Signer */
398 0 : fd_rng_t _rng[1];
399 0 : uint seed = 4242424242;
400 0 : fd_rng_t * rng = fd_rng_join( fd_rng_new( _rng, seed, 0UL ) );
401 :
402 0 : ctx->signer_ctx = signer_ctx( rng );
403 :
404 0 : quic->config.sign_ctx = &ctx->signer_ctx;
405 0 : quic->config.sign = signer;
406 :
407 0 : fd_memcpy( quic->config.identity_public_key, ctx->signer_ctx.public_key, 32UL );
408 :
409 : /* store the pointer to quic and quic_rx_aio for later use */
410 0 : ctx->quic = quic;
411 0 : ctx->quic_rx_aio = fd_quic_get_aio_net_rx( quic );
412 :
413 0 : ctx->quic_conn = NULL;
414 0 : ctx->stream = NULL;
415 0 : ctx->tx_idx = 0UL;
416 :
417 : /* call wallclock so glibc loads VDSO, which requires calling mmap while
418 : privileged */
419 0 : fd_log_wallclock();
420 0 : }
421 :
422 0 : ushort port = 12000;
423 :
424 0 : ctx->conn_cnt = tile->benchs.conn_cnt;
425 0 : if( !no_quic ) ctx->conn_cnt = 1;
426 0 : FD_TEST( ctx->conn_cnt <=sizeof(ctx->conn_fd)/sizeof(*ctx->conn_fd) );
427 0 : ctx->quic_port = tile->benchs.send_to_port;
428 0 : for( ulong i=0UL; i<ctx->conn_cnt ; i++ ) {
429 0 : int conn_fd = socket( AF_INET, SOCK_DGRAM, 0 );
430 0 : if( FD_UNLIKELY( -1==conn_fd ) ) FD_LOG_ERR(( "socket() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
431 :
432 0 : int recvbuff = 8<<20;
433 :
434 : // Set the buffer size
435 0 : if( setsockopt( conn_fd, SOL_SOCKET, SO_RCVBUF, &recvbuff, sizeof(recvbuff) ) < 0 ) {
436 0 : FD_LOG_ERR(( "Error setting receive buffer size. Error: %d %s", errno, strerror( errno ) ));
437 0 : }
438 :
439 0 : int sendbuff = 8<<20;
440 0 : if( setsockopt( conn_fd, SOL_SOCKET, SO_SNDBUF, &sendbuff, sizeof(sendbuff) ) < 0 ) {
441 0 : FD_LOG_ERR(( "Error setting transmit buffer size. Error: %d %s", errno, strerror( errno ) ));
442 0 : }
443 :
444 0 : ushort found_port = 0;
445 0 : for( ulong j=0UL; j<10UL; j++ ) {
446 0 : struct sockaddr_in addr = {
447 0 : .sin_family = AF_INET,
448 0 : .sin_port = fd_ushort_bswap( port ),
449 0 : .sin_addr.s_addr = fd_uint_bswap( INADDR_ANY ),
450 0 : };
451 0 : if( FD_UNLIKELY( -1!=bind( conn_fd, fd_type_pun( &addr ), sizeof(addr) ) ) ) {
452 0 : found_port = port;
453 0 : break;
454 0 : }
455 0 : if( FD_UNLIKELY( EADDRINUSE!=errno ) ) FD_LOG_ERR(( "bind() failed (%i-%s)", errno, fd_io_strerror( errno ) ) );
456 0 : port = (ushort)(port + ctx->conn_cnt); /* Make sure it round robins to the same tile index */
457 0 : }
458 0 : if( FD_UNLIKELY( !found_port ) ) FD_LOG_ERR(( "bind() failed to find a src port" ));
459 :
460 0 : struct sockaddr_in addr = {
461 0 : .sin_family = AF_INET,
462 0 : .sin_port = fd_ushort_bswap( tile->benchs.send_to_port ),
463 0 : .sin_addr.s_addr = tile->benchs.send_to_ip_addr,
464 0 : };
465 0 : if( FD_UNLIKELY( -1==connect( conn_fd, fd_type_pun( &addr ), sizeof(addr) ) ) ) FD_LOG_ERR(( "connect() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
466 :
467 0 : ctx->conn_fd[ i ] = conn_fd;
468 0 : if( !no_quic ) {
469 0 : ctx->poll_fd[i].fd = conn_fd;
470 0 : ctx->poll_fd[i].events = POLLIN;
471 0 : }
472 0 : port++;
473 0 : }
474 0 : }
475 :
476 : static void
477 : unprivileged_init( fd_topo_t * topo,
478 0 : fd_topo_tile_t * tile ) {
479 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
480 :
481 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
482 0 : fd_benchs_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_benchs_ctx_t ), sizeof( fd_benchs_ctx_t ) );
483 :
484 0 : ctx->packet_cnt = 0UL;
485 :
486 0 : ctx->round_robin_id = tile->kind_id;
487 0 : ctx->round_robin_cnt = fd_topo_tile_name_cnt( topo, "benchs" );
488 :
489 0 : ctx->mem = topo->workspaces[ topo->objs[ topo->links[ tile->in_link_id[ 0UL ] ].dcache_obj_id ].wksp_id ].wksp;
490 :
491 0 : void * aio_mem = NULL;
492 :
493 0 : if( !ctx->no_quic ) {
494 0 : fd_quic_limits_t quic_limits = {0};
495 0 : populate_quic_limits( &quic_limits );
496 0 : ulong quic_fp = fd_quic_footprint( &quic_limits );
497 0 : FD_SCRATCH_ALLOC_APPEND( l, fd_quic_align(), quic_fp );
498 :
499 0 : fd_quic_t * quic = ctx->quic;
500 0 : aio_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_aio_align(), fd_aio_footprint() );
501 0 : fd_aio_t * quic_tx_aio = fd_aio_join( fd_aio_new( aio_mem, ctx, quic_tx_aio_send ) );
502 :
503 0 : if( FD_UNLIKELY( !quic_tx_aio ) ) FD_LOG_ERR(( "fd_aio_join failed" ));
504 :
505 0 : uint quic_ip_addr = 0; /* TODO fetch the quic destination ip addr */
506 0 : ulong quic_idle_timeout_millis = 10000; /* idle timeout in milliseconds */
507 0 : quic->config.role = FD_QUIC_ROLE_CLIENT;
508 0 : quic->config.net.ip_addr = quic_ip_addr;
509 0 : quic->config.net.listen_udp_port = 42424; /* should be unused */
510 0 : quic->config.idle_timeout = quic_idle_timeout_millis * 1000000UL;
511 0 : quic->config.initial_rx_max_stream_data = 0;
512 0 : quic->config.retry = 0; /* unused on clients */
513 :
514 0 : quic->cb.conn_new = quic_conn_new;
515 0 : quic->cb.conn_hs_complete = handshake_complete;
516 0 : quic->cb.conn_final = conn_final;
517 0 : quic->cb.stream_notify = quic_stream_notify;
518 0 : quic->cb.now = quic_now;
519 0 : quic->cb.now_ctx = NULL;
520 0 : quic->cb.quic_ctx = ctx;
521 :
522 0 : fd_quic_set_aio_net_tx( quic, quic_tx_aio );
523 0 : if( FD_UNLIKELY( !fd_quic_init( quic ) ) ) FD_LOG_ERR(( "fd_quic_init failed" ));
524 :
525 0 : ulong hdr_sz = 20 + 8;
526 0 : for( ulong i = 0; i < IO_VEC_CNT; i++ ) {
527 : /* leave space for headers */
528 0 : ctx->rx_iovecs[i].iov_base = ctx->rx_bufs[i] + hdr_sz;
529 0 : ctx->rx_iovecs[i].iov_len = sizeof(ctx->rx_bufs[i]) - hdr_sz;
530 0 : ctx->rx_msgs[i].msg_hdr.msg_iov = &ctx->rx_iovecs[i];
531 0 : ctx->rx_msgs[i].msg_hdr.msg_iovlen = 1;
532 0 : }
533 0 : }
534 :
535 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
536 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
537 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
538 :
539 0 : }
540 :
541 : static void
542 0 : quic_tx_aio_send_flush( fd_benchs_ctx_t * ctx ) {
543 0 : if( FD_LIKELY( ctx->tx_idx ) ) {
544 0 : int flags = 0;
545 0 : int rtn = sendmmsg( ctx->conn_fd[0], ctx->tx_msgs, (uint)ctx->tx_idx, flags );
546 0 : if( FD_UNLIKELY( rtn < 0 ) ) {
547 0 : FD_LOG_NOTICE(( "Error occurred in sendmmsg. Error: %d %s",
548 0 : errno, strerror( errno ) ));
549 0 : }
550 0 : ctx->tx_idx = 0;
551 0 : }
552 0 : }
553 :
554 : static int
555 : quic_tx_aio_send( void * _ctx,
556 : fd_aio_pkt_info_t const * batch,
557 : ulong batch_cnt,
558 : ulong * opt_batch_idx,
559 0 : int flush ) {
560 0 : fd_benchs_ctx_t * ctx = _ctx;
561 :
562 : /* quic adds ip and udp headers which we don't need */
563 : /* assume 20 + 8 for those */
564 0 : ulong hdr_sz = 20+8;
565 :
566 0 : if( FD_LIKELY( batch_cnt ) ) {
567 : /* do we have space? */
568 0 : ulong remain = IO_VEC_CNT - ctx->tx_idx;
569 0 : if( FD_UNLIKELY( remain > batch_cnt ) ) {
570 0 : quic_tx_aio_send_flush( ctx );
571 :
572 : /* tx_idx may have changed */
573 0 : remain = IO_VEC_CNT - ctx->tx_idx;
574 0 : }
575 :
576 0 : ulong cnt = fd_ulong_min( remain, batch_cnt );
577 0 : ulong tx_idx = ctx->tx_idx;
578 0 : for( ulong j = 0; j < cnt; ++j ) {
579 0 : if( FD_UNLIKELY( batch[j].buf_sz < hdr_sz ) ) continue;
580 :
581 0 : char * tx_buf = ctx->tx_bufs[tx_idx];
582 :
583 : /* copy, stripping the header */
584 0 : fd_memcpy( tx_buf, (uchar*)batch[j].buf + hdr_sz, batch[j].buf_sz - hdr_sz );
585 :
586 0 : ctx->tx_iovecs[tx_idx].iov_base = tx_buf;
587 0 : ctx->tx_iovecs[tx_idx].iov_len = batch[j].buf_sz - hdr_sz;
588 0 : ctx->tx_msgs[tx_idx].msg_hdr.msg_iov = &ctx->tx_iovecs[tx_idx];
589 0 : ctx->tx_msgs[tx_idx].msg_hdr.msg_iovlen = 1;
590 :
591 0 : tx_idx++;
592 0 : }
593 :
594 : /* write back */
595 0 : ctx->tx_idx = tx_idx;
596 :
597 : // TODO count drops?
598 : // ctx->dropped += batch_cnt - remain;
599 0 : }
600 :
601 0 : if( FD_UNLIKELY( ctx->tx_idx == IO_VEC_CNT || flush ) ) {
602 0 : quic_tx_aio_send_flush( ctx );
603 0 : }
604 :
605 0 : if( FD_LIKELY( opt_batch_idx ) ) *opt_batch_idx = batch_cnt;
606 :
607 0 : return 0;
608 0 : }
609 :
610 0 : #define STEM_BURST (1UL)
611 :
612 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_benchs_ctx_t
613 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_benchs_ctx_t)
614 :
615 0 : #define STEM_CALLBACK_BEFORE_FRAG before_frag
616 0 : #define STEM_CALLBACK_DURING_FRAG during_frag
617 :
618 : #include "../../../disco/stem/fd_stem.c"
619 :
620 : fd_topo_run_tile_t fd_tile_benchs = {
621 : .name = "benchs",
622 : .scratch_align = scratch_align,
623 : .scratch_footprint = scratch_footprint,
624 : .privileged_init = privileged_init,
625 : .unprivileged_init = unprivileged_init,
626 : .run = stem_run,
627 : };
|