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 0 : config->net.dscp = 0;
252 0 : }
253 :
254 : static inline ulong
255 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
256 0 : ulong l = FD_LAYOUT_INIT;
257 0 : l = FD_LAYOUT_APPEND( l, alignof( fd_benchs_ctx_t ), sizeof( fd_benchs_ctx_t ) );
258 0 : if( !tile->benchs.no_quic ) {
259 0 : fd_quic_limits_t quic_limits = {0};
260 0 : populate_quic_limits( &quic_limits );
261 0 : ulong quic_fp = fd_quic_footprint( &quic_limits );
262 0 : l = FD_LAYOUT_APPEND( l, fd_quic_align(), quic_fp );
263 0 : l = FD_LAYOUT_APPEND( l, fd_aio_align(), fd_aio_footprint() );
264 0 : }
265 0 : return FD_LAYOUT_FINI( l, scratch_align() );
266 0 : }
267 :
268 : static inline int
269 : before_frag( fd_benchs_ctx_t * ctx,
270 : ulong in_idx,
271 : ulong seq,
272 0 : ulong sig ) {
273 0 : (void)in_idx;
274 0 : (void)sig;
275 :
276 0 : return (int)( (seq%ctx->round_robin_cnt)!=ctx->round_robin_id );
277 0 : }
278 :
279 : static inline void
280 : during_frag( fd_benchs_ctx_t * ctx,
281 : ulong in_idx FD_PARAM_UNUSED,
282 : ulong seq FD_PARAM_UNUSED,
283 : ulong sig FD_PARAM_UNUSED,
284 : ulong chunk,
285 : ulong sz,
286 0 : ulong ctl FD_PARAM_UNUSED ) {
287 0 : if( ctx->no_quic ) {
288 :
289 0 : if( FD_UNLIKELY( -1==send( ctx->conn_fd[ ctx->packet_cnt % ctx->conn_cnt ], fd_chunk_to_laddr( ctx->mem, chunk ), sz, 0 ) ) )
290 0 : FD_LOG_ERR(( "send() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
291 :
292 0 : ctx->packet_cnt++;
293 0 : } else {
294 : /* allows to accumulate multiple transactions before creating a UDP datagram */
295 : /* make this configurable */
296 0 : if( FD_UNLIKELY( ctx->service_ratio_idx++ == 8 ) ) {
297 0 : ctx->service_ratio_idx = 0;
298 0 : service_quic( ctx );
299 0 : fd_quic_service( ctx->quic );
300 0 : }
301 :
302 0 : if( FD_UNLIKELY( !ctx->quic_conn ) ) {
303 0 : ctx->no_stream = 0;
304 :
305 : /* try to connect */
306 0 : uint dest_ip = 0;
307 0 : ushort dest_port = fd_ushort_bswap( ctx->quic_port );
308 :
309 0 : ctx->quic_conn = fd_quic_connect( ctx->quic, dest_ip, dest_port, 0U, 12000 );
310 :
311 : /* failed? try later */
312 0 : if( FD_UNLIKELY( !ctx->quic_conn ) ) {
313 0 : service_quic( ctx );
314 0 : fd_quic_service( ctx->quic );
315 0 : return;
316 0 : }
317 :
318 0 : FD_LOG_NOTICE(( "connection created on port %d", (int)dest_port ));
319 :
320 : /* set the context to point to the location
321 : of the quic_conn pointer
322 : this allows the notification to NULL the value when
323 : a connection dies */
324 0 : fd_quic_conn_set_context( ctx->quic_conn, ctx );
325 :
326 0 : service_quic( ctx );
327 0 : fd_quic_service( ctx->quic );
328 :
329 : /* conn and streams may be invalidated by fd_quic_service */
330 :
331 0 : return;
332 0 : }
333 :
334 0 : fd_quic_stream_t * stream = ctx->stream;
335 0 : if( FD_UNLIKELY( !stream ) ) {
336 0 : ctx->stream = stream = fd_quic_conn_new_stream( ctx->quic_conn );
337 0 : if( FD_LIKELY( stream ) ) {
338 0 : fd_quic_stream_set_context( stream, ctx );
339 0 : }
340 0 : }
341 :
342 0 : if( FD_UNLIKELY( !stream ) ) {
343 0 : ctx->no_stream++;
344 0 : service_quic( ctx );
345 0 : fd_quic_service( ctx->quic );
346 :
347 : /* conn and streams may be invalidated by fd_quic_service */
348 :
349 0 : return;
350 0 : } else {
351 0 : int fin = 1;
352 0 : int rtn = fd_quic_stream_send( stream, fd_chunk_to_laddr( ctx->mem, chunk ), sz, fin );
353 0 : ctx->packet_cnt++;
354 :
355 0 : if( FD_LIKELY( rtn == FD_QUIC_SUCCESS ) ) {
356 : /* after using, fetch a new stream */
357 0 : ctx->stream = stream = fd_quic_conn_new_stream( ctx->quic_conn );
358 0 : if( FD_LIKELY( stream ) ) {
359 0 : fd_quic_stream_set_context( stream, ctx );
360 0 : }
361 0 : } else if( FD_UNLIKELY( rtn == 0 ) ) {
362 0 : FD_LOG_NOTICE(( "fd_quic_stream_send returned zero" ));
363 0 : } else {
364 : /* this can happen dring handshaking */
365 0 : if( rtn != FD_QUIC_SEND_ERR_INVAL_CONN ) {
366 0 : FD_LOG_ERR(( "fd_quic_stream_send failed with: %d", rtn ));
367 0 : }
368 0 : }
369 0 : }
370 0 : }
371 0 : }
372 :
373 : static void
374 : privileged_init( fd_topo_t * topo,
375 0 : fd_topo_tile_t * tile ) {
376 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
377 :
378 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
379 0 : fd_benchs_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_benchs_ctx_t ), sizeof( fd_benchs_ctx_t ) );
380 :
381 0 : int no_quic = ctx->no_quic = tile->benchs.no_quic;
382 :
383 0 : if( !no_quic ) {
384 0 : fd_quic_limits_t quic_limits = {0};
385 0 : populate_quic_limits( &quic_limits );
386 0 : ulong quic_fp = fd_quic_footprint( &quic_limits );
387 0 : if( FD_UNLIKELY( !quic_fp ) ) FD_LOG_ERR(( "invalid QUIC parameters" ));
388 0 : void * quic_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_quic_align(), quic_fp );
389 0 : fd_quic_t * quic = fd_quic_join( fd_quic_new( quic_mem, &quic_limits ) );
390 :
391 0 : populate_quic_config( &quic->config );
392 :
393 : /* Signer */
394 0 : fd_rng_t _rng[1];
395 0 : uint seed = 4242424242;
396 0 : fd_rng_t * rng = fd_rng_join( fd_rng_new( _rng, seed, 0UL ) );
397 :
398 0 : ctx->signer_ctx = signer_ctx( rng );
399 :
400 0 : quic->config.sign_ctx = &ctx->signer_ctx;
401 0 : quic->config.sign = signer;
402 :
403 0 : fd_memcpy( quic->config.identity_public_key, ctx->signer_ctx.public_key, 32UL );
404 :
405 : /* store the pointer to quic and quic_rx_aio for later use */
406 0 : ctx->quic = quic;
407 0 : ctx->quic_rx_aio = fd_quic_get_aio_net_rx( quic );
408 :
409 0 : ctx->quic_conn = NULL;
410 0 : ctx->stream = NULL;
411 0 : ctx->tx_idx = 0UL;
412 :
413 : /* call wallclock so glibc loads VDSO, which requires calling mmap while
414 : privileged */
415 0 : fd_log_wallclock();
416 0 : }
417 :
418 0 : ushort port = 12000;
419 :
420 0 : ctx->conn_cnt = tile->benchs.conn_cnt;
421 0 : if( !no_quic ) ctx->conn_cnt = 1;
422 0 : FD_TEST( ctx->conn_cnt <=sizeof(ctx->conn_fd)/sizeof(*ctx->conn_fd) );
423 0 : ctx->quic_port = tile->benchs.send_to_port;
424 0 : for( ulong i=0UL; i<ctx->conn_cnt ; i++ ) {
425 0 : int conn_fd = socket( AF_INET, SOCK_DGRAM, 0 );
426 0 : if( FD_UNLIKELY( -1==conn_fd ) ) FD_LOG_ERR(( "socket() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
427 :
428 0 : int recvbuff = 8<<20;
429 :
430 : // Set the buffer size
431 0 : if( setsockopt( conn_fd, SOL_SOCKET, SO_RCVBUF, &recvbuff, sizeof(recvbuff) ) < 0 ) {
432 0 : FD_LOG_ERR(( "Error setting receive buffer size. Error: %d %s", errno, strerror( errno ) ));
433 0 : }
434 :
435 0 : int sendbuff = 8<<20;
436 0 : if( setsockopt( conn_fd, SOL_SOCKET, SO_SNDBUF, &sendbuff, sizeof(sendbuff) ) < 0 ) {
437 0 : FD_LOG_ERR(( "Error setting transmit buffer size. Error: %d %s", errno, strerror( errno ) ));
438 0 : }
439 :
440 0 : ushort found_port = 0;
441 0 : for( ulong j=0UL; j<10UL; j++ ) {
442 0 : struct sockaddr_in addr = {
443 0 : .sin_family = AF_INET,
444 0 : .sin_port = fd_ushort_bswap( port ),
445 0 : .sin_addr.s_addr = fd_uint_bswap( INADDR_ANY ),
446 0 : };
447 0 : if( FD_UNLIKELY( -1!=bind( conn_fd, fd_type_pun( &addr ), sizeof(addr) ) ) ) {
448 0 : found_port = port;
449 0 : break;
450 0 : }
451 0 : if( FD_UNLIKELY( EADDRINUSE!=errno ) ) FD_LOG_ERR(( "bind() failed (%i-%s)", errno, fd_io_strerror( errno ) ) );
452 0 : port = (ushort)(port + ctx->conn_cnt); /* Make sure it round robins to the same tile index */
453 0 : }
454 0 : if( FD_UNLIKELY( !found_port ) ) FD_LOG_ERR(( "bind() failed to find a src port" ));
455 :
456 0 : struct sockaddr_in addr = {
457 0 : .sin_family = AF_INET,
458 0 : .sin_port = fd_ushort_bswap( tile->benchs.send_to_port ),
459 0 : .sin_addr.s_addr = tile->benchs.send_to_ip_addr,
460 0 : };
461 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 ) ));
462 :
463 0 : ctx->conn_fd[ i ] = conn_fd;
464 0 : if( !no_quic ) {
465 0 : ctx->poll_fd[i].fd = conn_fd;
466 0 : ctx->poll_fd[i].events = POLLIN;
467 0 : }
468 0 : port++;
469 0 : }
470 0 : }
471 :
472 : static void
473 : unprivileged_init( fd_topo_t * topo,
474 0 : fd_topo_tile_t * tile ) {
475 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
476 :
477 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
478 0 : fd_benchs_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_benchs_ctx_t ), sizeof( fd_benchs_ctx_t ) );
479 :
480 0 : ctx->packet_cnt = 0UL;
481 :
482 0 : ctx->round_robin_id = tile->kind_id;
483 0 : ctx->round_robin_cnt = fd_topo_tile_name_cnt( topo, "benchs" );
484 :
485 0 : ctx->mem = topo->workspaces[ topo->objs[ topo->links[ tile->in_link_id[ 0UL ] ].dcache_obj_id ].wksp_id ].wksp;
486 :
487 0 : void * aio_mem = NULL;
488 :
489 0 : if( !ctx->no_quic ) {
490 0 : fd_quic_limits_t quic_limits = {0};
491 0 : populate_quic_limits( &quic_limits );
492 0 : ulong quic_fp = fd_quic_footprint( &quic_limits );
493 0 : FD_SCRATCH_ALLOC_APPEND( l, fd_quic_align(), quic_fp );
494 :
495 0 : fd_quic_t * quic = ctx->quic;
496 0 : aio_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_aio_align(), fd_aio_footprint() );
497 0 : fd_aio_t * quic_tx_aio = fd_aio_join( fd_aio_new( aio_mem, ctx, quic_tx_aio_send ) );
498 :
499 0 : if( FD_UNLIKELY( !quic_tx_aio ) ) FD_LOG_ERR(( "fd_aio_join failed" ));
500 :
501 0 : ulong quic_idle_timeout_millis = 10000; /* idle timeout in milliseconds */
502 0 : quic->config.role = FD_QUIC_ROLE_CLIENT;
503 0 : quic->config.idle_timeout = quic_idle_timeout_millis * 1000000UL;
504 0 : quic->config.initial_rx_max_stream_data = 0;
505 0 : quic->config.retry = 0; /* unused on clients */
506 :
507 0 : quic->cb.conn_new = quic_conn_new;
508 0 : quic->cb.conn_hs_complete = handshake_complete;
509 0 : quic->cb.conn_final = conn_final;
510 0 : quic->cb.stream_notify = quic_stream_notify;
511 0 : quic->cb.now = quic_now;
512 0 : quic->cb.now_ctx = NULL;
513 0 : quic->cb.quic_ctx = ctx;
514 :
515 0 : fd_quic_set_aio_net_tx( quic, quic_tx_aio );
516 0 : if( FD_UNLIKELY( !fd_quic_init( quic ) ) ) FD_LOG_ERR(( "fd_quic_init failed" ));
517 :
518 0 : ulong hdr_sz = 20 + 8;
519 0 : for( ulong i = 0; i < IO_VEC_CNT; i++ ) {
520 : /* leave space for headers */
521 0 : ctx->rx_iovecs[i].iov_base = ctx->rx_bufs[i] + hdr_sz;
522 0 : ctx->rx_iovecs[i].iov_len = sizeof(ctx->rx_bufs[i]) - hdr_sz;
523 0 : ctx->rx_msgs[i].msg_hdr.msg_iov = &ctx->rx_iovecs[i];
524 0 : ctx->rx_msgs[i].msg_hdr.msg_iovlen = 1;
525 0 : }
526 0 : }
527 :
528 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
529 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
530 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
531 :
532 0 : }
533 :
534 : static void
535 0 : quic_tx_aio_send_flush( fd_benchs_ctx_t * ctx ) {
536 0 : if( FD_LIKELY( ctx->tx_idx ) ) {
537 0 : int flags = 0;
538 0 : int rtn = sendmmsg( ctx->conn_fd[0], ctx->tx_msgs, (uint)ctx->tx_idx, flags );
539 0 : if( FD_UNLIKELY( rtn < 0 ) ) {
540 0 : FD_LOG_NOTICE(( "Error occurred in sendmmsg. Error: %d %s",
541 0 : errno, strerror( errno ) ));
542 0 : }
543 0 : ctx->tx_idx = 0;
544 0 : }
545 0 : }
546 :
547 : static int
548 : quic_tx_aio_send( void * _ctx,
549 : fd_aio_pkt_info_t const * batch,
550 : ulong batch_cnt,
551 : ulong * opt_batch_idx,
552 0 : int flush ) {
553 0 : fd_benchs_ctx_t * ctx = _ctx;
554 :
555 : /* quic adds ip and udp headers which we don't need */
556 : /* assume 20 + 8 for those */
557 0 : ulong hdr_sz = 20+8;
558 :
559 0 : if( FD_LIKELY( batch_cnt ) ) {
560 : /* do we have space? */
561 0 : ulong remain = IO_VEC_CNT - ctx->tx_idx;
562 0 : if( FD_UNLIKELY( remain > batch_cnt ) ) {
563 0 : quic_tx_aio_send_flush( ctx );
564 :
565 : /* tx_idx may have changed */
566 0 : remain = IO_VEC_CNT - ctx->tx_idx;
567 0 : }
568 :
569 0 : ulong cnt = fd_ulong_min( remain, batch_cnt );
570 0 : ulong tx_idx = ctx->tx_idx;
571 0 : for( ulong j = 0; j < cnt; ++j ) {
572 0 : if( FD_UNLIKELY( batch[j].buf_sz < hdr_sz ) ) continue;
573 :
574 0 : char * tx_buf = ctx->tx_bufs[tx_idx];
575 :
576 : /* copy, stripping the header */
577 0 : fd_memcpy( tx_buf, (uchar*)batch[j].buf + hdr_sz, batch[j].buf_sz - hdr_sz );
578 :
579 0 : ctx->tx_iovecs[tx_idx].iov_base = tx_buf;
580 0 : ctx->tx_iovecs[tx_idx].iov_len = batch[j].buf_sz - hdr_sz;
581 0 : ctx->tx_msgs[tx_idx].msg_hdr.msg_iov = &ctx->tx_iovecs[tx_idx];
582 0 : ctx->tx_msgs[tx_idx].msg_hdr.msg_iovlen = 1;
583 :
584 0 : tx_idx++;
585 0 : }
586 :
587 : /* write back */
588 0 : ctx->tx_idx = tx_idx;
589 :
590 : // TODO count drops?
591 : // ctx->dropped += batch_cnt - remain;
592 0 : }
593 :
594 0 : if( FD_UNLIKELY( ctx->tx_idx == IO_VEC_CNT || flush ) ) {
595 0 : quic_tx_aio_send_flush( ctx );
596 0 : }
597 :
598 0 : if( FD_LIKELY( opt_batch_idx ) ) *opt_batch_idx = batch_cnt;
599 :
600 0 : return 0;
601 0 : }
602 :
603 0 : #define STEM_BURST (1UL)
604 :
605 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_benchs_ctx_t
606 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_benchs_ctx_t)
607 :
608 0 : #define STEM_CALLBACK_BEFORE_FRAG before_frag
609 0 : #define STEM_CALLBACK_DURING_FRAG during_frag
610 :
611 : #include "../../../../disco/stem/fd_stem.c"
612 :
613 : fd_topo_run_tile_t fd_tile_benchs = {
614 : .name = "benchs",
615 : .scratch_align = scratch_align,
616 : .scratch_footprint = scratch_footprint,
617 : .privileged_init = privileged_init,
618 : .unprivileged_init = unprivileged_init,
619 : .run = stem_run,
620 : };
|