Line data Source code
1 : #include "../dev.h"
2 : #include "../../../shared/commands/configure/configure.h" /* CONFIGURE_CMD_INIT */
3 : #include "../../../shared/commands/run/run.h" /* fdctl_check_configure */
4 : #include "../../../../disco/net/fd_net_tile.h"
5 : #include "../../../../disco/metrics/fd_metrics.h"
6 : #include "../../../../disco/topo/fd_topob.h"
7 : #include "../../../../disco/topo/fd_cpu_topo.h"
8 : #include "../../../../util/net/fd_eth.h"
9 : #include "../../../../util/net/fd_ip4.h"
10 :
11 : #include <stdio.h> /* printf */
12 : #include <unistd.h> /* isatty */
13 : #include <sys/ioctl.h>
14 : #include <poll.h>
15 :
16 : extern fd_topo_obj_callbacks_t * CALLBACKS[];
17 :
18 : fd_topo_run_tile_t
19 : fdctl_tile_run( fd_topo_tile_t const * tile );
20 :
21 : static void
22 0 : pktgen_topo( config_t * config ) {
23 0 : ulong net_tile_cnt = config->layout.net_tile_count;
24 :
25 0 : char const * affinity = config->development.pktgen.affinity;
26 0 : int is_auto_affinity = !strcmp( affinity, "auto" );
27 :
28 0 : ushort parsed_tile_to_cpu[ FD_TILE_MAX ];
29 0 : for( ulong i=0UL; i<FD_TILE_MAX; i++ ) parsed_tile_to_cpu[ i ] = USHORT_MAX;
30 :
31 0 : fd_topo_cpus_t cpus[1];
32 0 : fd_topo_cpus_init( cpus );
33 :
34 0 : ulong affinity_tile_cnt = 0UL;
35 0 : ulong required_tile_cnt = 3UL+net_tile_cnt;
36 0 : if( FD_LIKELY( !is_auto_affinity ) ) affinity_tile_cnt = fd_topob_parse_affinity_cstr( affinity, parsed_tile_to_cpu, 0, 1 );
37 :
38 0 : ulong tile_to_cpu[ FD_TILE_MAX ] = {0};
39 0 : for( ulong i=0UL; i<affinity_tile_cnt; i++ ) {
40 0 : ushort cpu_idx = (ushort)( parsed_tile_to_cpu[ i ] & ~FD_TOPOB_CPU_SHARED );
41 0 : if( FD_UNLIKELY( parsed_tile_to_cpu[ i ]!=USHORT_MAX && cpu_idx>=cpus->cpu_cnt ) )
42 0 : FD_LOG_ERR(( "The CPU affinity string in the configuration file under [development.pktgen.affinity] specifies a CPU index of %hu, but the system "
43 0 : "only has %lu CPUs. You should either change the CPU allocations in the affinity string, or increase the number of CPUs "
44 0 : "in the system.",
45 0 : cpu_idx, cpus->cpu_cnt ));
46 0 : tile_to_cpu[ i ] = fd_ulong_if( parsed_tile_to_cpu[ i ]==USHORT_MAX, ULONG_MAX, (ulong)parsed_tile_to_cpu[ i ] );
47 0 : }
48 0 : if( FD_LIKELY( !is_auto_affinity ) ) {
49 0 : if( FD_UNLIKELY( affinity_tile_cnt!=required_tile_cnt ) )
50 0 : FD_LOG_ERR(( "Invalid [development.pktgen.affinity]: must include exactly %lu CPUs", required_tile_cnt ));
51 0 : }
52 :
53 : /* Reset topology from scratch */
54 0 : fd_topo_t * topo = &config->topo;
55 0 : fd_topob_new( &config->topo, config->name );
56 0 : topo->max_page_size = fd_cstr_to_shmem_page_sz( config->hugetlbfs.max_page_size );
57 :
58 0 : fd_topob_wksp( topo, "metric" );
59 0 : fd_topob_wksp( topo, "metric_in" );
60 0 : fd_topos_net_tiles( topo, net_tile_cnt, &config->net, config->tiles.netlink.max_routes, config->tiles.netlink.max_peer_routes, config->tiles.netlink.max_neighbors, tile_to_cpu );
61 0 : fd_topob_tile( topo, "metric", "metric", "metric_in", tile_to_cpu[ topo->tile_cnt ], 0, 0, 1 );
62 :
63 0 : char const * net_tile_name = fd_net_tile_name( config->net.provider );
64 :
65 0 : fd_topob_wksp( topo, "pktgen" );
66 0 : fd_topo_tile_t * pktgen_tile = fd_topob_tile( topo, "pktgen", "pktgen", "pktgen", tile_to_cpu[ topo->tile_cnt ], 0, 0, 0 );
67 0 : if( FD_UNLIKELY( !fd_cstr_to_ip4_addr( config->development.pktgen.fake_dst_ip, &pktgen_tile->pktgen.fake_dst_ip ) ) ) {
68 0 : FD_LOG_ERR(( "Invalid [development.pktgen.fake_dst_ip]" ));
69 0 : }
70 0 : fd_topob_link( topo, "pktgen_net", "pktgen", 32768UL, FD_NET_MTU, 1UL );
71 0 : fd_topob_tile_out( topo, "pktgen", 0UL, "pktgen_net", 0UL );
72 0 : for( ulong i=0UL; i<net_tile_cnt; i++ ) {
73 0 : fd_topob_tile_in( topo, net_tile_name, i, "metric_in", "pktgen_net", 0UL, FD_TOPOB_UNRELIABLE, FD_TOPOB_POLLED );
74 :
75 : /* Create dummy RX link */
76 0 : fd_topos_net_rx_link( topo, "net_quic", i, config->net.ingress_buffer_size );
77 0 : fd_topob_tile_in( topo, "pktgen", 0UL, "metric_in", "net_quic", i, FD_TOPOB_UNRELIABLE, FD_TOPOB_POLLED );
78 :
79 0 : fd_topos_net_tile_finish( topo, i );
80 0 : }
81 :
82 :
83 0 : if( FD_UNLIKELY( is_auto_affinity ) ) fd_topob_auto_layout( topo );
84 0 : fd_topob_waker( topo );
85 0 : fd_topob_finish( topo, CALLBACKS );
86 0 : fd_topo_print_log( /* stdout */ 1, topo );
87 0 : }
88 :
89 : /* PKTGEN_PKT_SZ_{MIN,MAX} bound --pkt-sz, the size of the L3 header +
90 : payload. MIN yields a minimum size Ethernet frame (60 bytes with the
91 : 14 byte Ethernet header). MAX is limited by the net tile, which caps
92 : the full Ethernet frame (including the Ethernet header) at
93 : FD_ETH_PAYLOAD_MAX (1500) bytes (see fd_net_in_frag_validate). */
94 :
95 0 : #define PKTGEN_PKT_SZ_MIN (46UL)
96 : #define PKTGEN_PKT_SZ_MAX (FD_ETH_PAYLOAD_MAX-sizeof(fd_eth_hdr_t))
97 :
98 : /* The --help text below hardcodes these bounds */
99 : FD_STATIC_ASSERT( PKTGEN_PKT_SZ_MIN==46UL, pktgen_pkt_sz_min );
100 : FD_STATIC_ASSERT( PKTGEN_PKT_SZ_MAX==1486UL, pktgen_pkt_sz_max );
101 :
102 : void
103 : pktgen_cmd_args( int * pargc,
104 : char *** pargv,
105 0 : args_t * args ) {
106 0 : ulong pkt_sz = fd_env_strip_cmdline_ulong( pargc, pargv, "--pkt-sz", NULL, PKTGEN_PKT_SZ_MIN );
107 0 : if( FD_UNLIKELY( pkt_sz<PKTGEN_PKT_SZ_MIN || pkt_sz>PKTGEN_PKT_SZ_MAX ) ) {
108 0 : FD_LOG_ERR(( "Invalid --pkt-sz %lu: size of the L3 header + payload must be in [%lu,%lu]",
109 0 : pkt_sz, PKTGEN_PKT_SZ_MIN, PKTGEN_PKT_SZ_MAX ));
110 0 : }
111 0 : args->pktgen.pkt_sz = pkt_sz;
112 0 : }
113 :
114 : static void
115 0 : pktgen_args_help( fd_action_help_t * help ) {
116 0 : fd_action_help_arg( help, "--pkt-sz", "<bytes>", "Size of the L3 header + payload of each generated packet\n"
117 0 : "(default 46, range [46,1486])" );
118 0 : }
119 :
120 : /* Hacky: Since the pktgen runs in the same process, use globals to
121 : share state */
122 : extern uint fd_pktgen_active;
123 :
124 : /* fd_net_stats stores the stats for a single net tile. */
125 :
126 : struct fd_net_stats {
127 : double busy_r;
128 : double rx_ok_pps;
129 : double rx_bps;
130 : double rx_drop_pps;
131 : double tx_ok_pps;
132 : double tx_bps;
133 :
134 : ulong rx_bufs_idle;
135 : ulong rx_bufs_busy;
136 : ulong tx_bufs_idle;
137 : ulong tx_bufs_busy;
138 : };
139 : typedef struct fd_net_stats fd_net_stats_t;
140 :
141 : /* fd_net_metrics_last stores recent key metrics for a single net
142 : tile, used to calculate the next net_stats. */
143 :
144 : struct fd_net_metrics_last {
145 : long ts;
146 : ulong cum_idle;
147 : ulong cum_tick;
148 : ulong rx_ok;
149 : ulong rx_byte;
150 : ulong rx_drop;
151 : ulong tx_ok;
152 : ulong tx_byte;
153 : };
154 : typedef struct fd_net_metrics_last fd_net_metrics_last_t;
155 :
156 : /* get_net_stats computes and returns per net tile stats. */
157 :
158 : static fd_net_stats_t const *
159 : get_net_stats( ulong volatile const * net_metrics[ FD_TOPO_MAX_TILES ],
160 : ulong net_tile_cnt,
161 0 : char const * provider ) {
162 :
163 0 : static fd_net_stats_t net_stats [ FD_TOPO_MAX_TILES ];
164 0 : static fd_net_metrics_last_t net_metrics_last[ FD_TOPO_MAX_TILES ];
165 :
166 0 : long now = fd_log_wallclock();
167 :
168 0 : for( ulong kind_id=0UL; kind_id<net_tile_cnt; kind_id++ ) {
169 0 : ulong volatile const * metrics = net_metrics [ kind_id ];
170 0 : fd_net_metrics_last_t * last = &net_metrics_last[ kind_id ];
171 0 : fd_net_stats_t * stats = &net_stats [ kind_id ];
172 :
173 0 : if( FD_UNLIKELY( last->ts==0L ) ) last->ts = now;
174 :
175 0 : long dt = now-last->ts;
176 0 : if( dt<=(long)10e6 ) continue;
177 :
178 0 : ulong cum_idle_now = metrics[ MIDX( COUNTER, TILE, REGIME_DURATION_NANOS_CAUGHT_UP_POSTFRAG ) ];
179 0 : /* */ cum_idle_now += metrics[ MIDX( COUNTER, TILE, REGIME_DURATION_NANOS_CAUGHT_UP_SLEEPING ) ];
180 0 : ulong cum_tick_now = cum_idle_now;
181 0 : /* */ cum_tick_now += metrics[ MIDX( COUNTER, TILE, REGIME_DURATION_NANOS_BACKPRESSURE_SLEEPING ) ];
182 0 : /* */ cum_tick_now += metrics[ MIDX( COUNTER, TILE, REGIME_DURATION_NANOS_CAUGHT_UP_HOUSEKEEPING ) ];
183 0 : /* */ cum_tick_now += metrics[ MIDX( COUNTER, TILE, REGIME_DURATION_NANOS_PROCESSING_HOUSEKEEPING ) ];
184 0 : /* */ cum_tick_now += metrics[ MIDX( COUNTER, TILE, REGIME_DURATION_NANOS_BACKPRESSURE_HOUSEKEEPING ) ];
185 0 : /* */ cum_tick_now += metrics[ MIDX( COUNTER, TILE, REGIME_DURATION_NANOS_CAUGHT_UP_PREFRAG ) ];
186 0 : /* */ cum_tick_now += metrics[ MIDX( COUNTER, TILE, REGIME_DURATION_NANOS_PROCESSING_PREFRAG ) ];
187 0 : /* */ cum_tick_now += metrics[ MIDX( COUNTER, TILE, REGIME_DURATION_NANOS_BACKPRESSURE_PREFRAG ) ];
188 0 : /* */ cum_tick_now += metrics[ MIDX( COUNTER, TILE, REGIME_DURATION_NANOS_PROCESSING_POSTFRAG ) ];
189 :
190 0 : ulong rx_ok_now = 0;
191 0 : ulong rx_byte_now = 0;
192 0 : ulong rx_drop_now = 0;
193 0 : ulong tx_ok_now = 0;
194 0 : ulong tx_byte_now = 0;
195 :
196 0 : if( 0==strcmp( provider, "xdp" ) ) {
197 0 : stats->rx_bufs_idle = metrics[ MIDX( GAUGE, NET, RX_BUFFER_FREE ) ];
198 0 : stats->rx_bufs_busy = metrics[ MIDX( GAUGE, NET, RX_BUFFER_USED ) ];
199 0 : stats->tx_bufs_idle = metrics[ MIDX( GAUGE, NET, TX_BUFFER_FREE ) ];
200 0 : stats->tx_bufs_busy = metrics[ MIDX( GAUGE, NET, TX_BUFFER_USED ) ];
201 :
202 0 : rx_ok_now = metrics[ MIDX( COUNTER, NET, PKT_RX ) ];
203 0 : rx_byte_now = metrics[ MIDX( COUNTER, NET, PKT_RX_BYTES ) ];
204 0 : rx_drop_now = metrics[ MIDX( COUNTER, NET, RX_FILL_RING_STALLED ) ];
205 0 : /* */ rx_drop_now += metrics[ MIDX( COUNTER, NET, XDP_RX_OTHER_DROPPED ) ];
206 0 : /* */ rx_drop_now += metrics[ MIDX( COUNTER, NET, XDP_RX_INVALID_DESCRIPTOR_DROPPED ) ];
207 0 : /* */ rx_drop_now += metrics[ MIDX( COUNTER, NET, XDP_RX_RING_FULL_DROPPED ) ];
208 0 : tx_ok_now = metrics[ MIDX( COUNTER, NET, PKT_TX_COMPLETED ) ];
209 0 : tx_byte_now = metrics[ MIDX( COUNTER, NET, PKT_TX_SUBMITTED_BYTES ) ];
210 :
211 0 : } else if( 0==strcmp( provider, "mlx5" ) ) {
212 0 : stats->rx_bufs_idle = metrics[ MIDX( GAUGE, MLX5, RX_BUFFER_FREE ) ];
213 0 : stats->rx_bufs_busy = metrics[ MIDX( GAUGE, MLX5, RX_BUFFER_USED ) ];
214 0 : stats->tx_bufs_idle = metrics[ MIDX( GAUGE, MLX5, TX_BUFFER_FREE ) ];
215 0 : stats->tx_bufs_busy = metrics[ MIDX( GAUGE, MLX5, TX_BUFFER_USED ) ];
216 :
217 0 : rx_ok_now = metrics[ MIDX( COUNTER, MLX5, PKT_RX ) ];
218 0 : rx_byte_now = metrics[ MIDX( COUNTER, MLX5, PKT_RX_BYTES ) ];
219 0 : rx_drop_now = 0UL;
220 0 : for( ulong r=0UL; r<FD_METRICS_ENUM_PKT_RX_DROP_REASON_CNT; r++ ) rx_drop_now += metrics[ MIDX( COUNTER, MLX5, PKT_RX_DROPPED )+r ];
221 0 : tx_ok_now = metrics[ MIDX( COUNTER, MLX5, PKT_TX_COMPLETED ) ];
222 0 : tx_byte_now = metrics[ MIDX( COUNTER, MLX5, PKT_TX_COMPLETED_BYTES ) ];
223 0 : } else if( 0==strcmp( provider, "iavf" ) ) {
224 0 : stats->rx_bufs_idle = metrics[ MIDX( GAUGE, IAVF, RX_BUFFER_FREE ) ];
225 0 : stats->rx_bufs_busy = metrics[ MIDX( GAUGE, IAVF, RX_BUFFER_USED ) ];
226 0 : stats->tx_bufs_idle = metrics[ MIDX( GAUGE, IAVF, TX_BUFFER_FREE ) ];
227 0 : stats->tx_bufs_busy = metrics[ MIDX( GAUGE, IAVF, TX_BUFFER_USED ) ];
228 :
229 0 : rx_ok_now = metrics[ MIDX( COUNTER, IAVF, PKT_RX ) ];
230 0 : rx_byte_now = metrics[ MIDX( COUNTER, IAVF, PKT_RX_BYTES ) ];
231 0 : rx_drop_now = 0UL;
232 0 : for( ulong r=0UL; r<FD_METRICS_ENUM_PKT_RX_DROP_REASON_CNT; r++ ) rx_drop_now += metrics[ MIDX( COUNTER, IAVF, PKT_RX_DROPPED )+r ];
233 0 : rx_drop_now += metrics[ MIDX( COUNTER, IAVF, PKT_RX_NO_LINK_DROPPED ) ];
234 0 : tx_ok_now = metrics[ MIDX( COUNTER, IAVF, PKT_TX_COMPLETED ) ];
235 0 : tx_byte_now = metrics[ MIDX( COUNTER, IAVF, PKT_TX_COMPLETED_BYTES ) ];
236 0 : } else if( 0==strcmp( provider, "socket" ) ) {
237 : /* The socket provider doesn't have equivalent metrics for idle and active buffers. */
238 0 : rx_ok_now = metrics[ MIDX( COUNTER, SOCK, PKT_RX ) ];
239 0 : rx_byte_now = metrics[ MIDX( COUNTER, SOCK, PKT_RX_BYTES ) ];
240 0 : rx_drop_now = 0UL;
241 0 : tx_ok_now = metrics[ MIDX( COUNTER, SOCK, PKT_TX) ];
242 0 : tx_byte_now = metrics[ MIDX( COUNTER, SOCK, PKT_TX_BYTES ) ];
243 0 : }
244 :
245 :
246 0 : ulong cum_idle_delta = cum_idle_now-last->cum_idle;
247 0 : ulong cum_tick_delta = cum_tick_now-last->cum_tick;
248 0 : ulong rx_ok_delta = rx_ok_now -last->rx_ok;
249 0 : ulong rx_byte_delta = rx_byte_now -last->rx_byte;
250 0 : ulong rx_drop_delta = rx_drop_now -last->rx_drop;
251 0 : ulong tx_ok_delta = tx_ok_now -last->tx_ok;
252 0 : ulong tx_byte_delta = tx_byte_now -last->tx_byte;
253 :
254 0 : stats->busy_r = 1.0 - ( (double)cum_idle_delta / (double)cum_tick_delta );
255 0 : stats->rx_ok_pps = 1e9*( (double)rx_ok_delta /(double)dt );
256 0 : stats->rx_bps = 8e9*( (double)rx_byte_delta/(double)dt );
257 0 : stats->rx_drop_pps = 1e9*( (double)rx_drop_delta/(double)dt );
258 0 : stats->tx_ok_pps = 1e9*( (double)tx_ok_delta /(double)dt );
259 0 : stats->tx_bps = 8e9*( (double)tx_byte_delta/(double)dt );
260 :
261 0 : last->ts = now;
262 0 : last->cum_idle = cum_idle_now;
263 0 : last->cum_tick = cum_tick_now;
264 0 : last->rx_ok = rx_ok_now;
265 0 : last->rx_byte = rx_byte_now;
266 0 : last->rx_drop = rx_drop_now;
267 0 : last->tx_ok = tx_ok_now;
268 0 : last->tx_byte = tx_byte_now;
269 0 : }
270 :
271 0 : return net_stats;
272 0 : }
273 :
274 : /* render_status prints statistics at the top of the screen.
275 : Should be called at a low rate (~500ms). */
276 :
277 : static void
278 : render_status( ulong volatile const * net_metrics[ FD_TOPO_MAX_TILES ],
279 : ulong net_tile_cnt,
280 : char const * provider,
281 0 : ulong pkt_sz ) {
282 0 : fputs( "\0337" /* save cursor position */
283 0 : "\033[H" /* move cursor to (0,0) */
284 0 : "\033[2K\n", /* create an empty line to avoid spamming look back buffer */
285 0 : stdout );
286 0 : printf( "\033[2K" "[Firedancer pktgen] mode=%s pkt_sz=%lu\n",
287 0 : FD_VOLATILE_CONST( fd_pktgen_active ) ? "send+recv" : "recv",
288 0 : pkt_sz );
289 :
290 0 : fd_net_stats_t const * stats = get_net_stats( net_metrics, net_tile_cnt, provider );
291 :
292 0 : fd_net_stats_t summary = {0};
293 0 : for( ulong i=0UL; i<net_tile_cnt; i++ ) {
294 0 : summary.busy_r += stats[ i ].busy_r;
295 0 : summary.rx_ok_pps += stats[ i ].rx_ok_pps;
296 0 : summary.rx_bps += stats[ i ].rx_bps;
297 0 : summary.rx_drop_pps += stats[ i ].rx_drop_pps;
298 0 : summary.tx_ok_pps += stats[ i ].tx_ok_pps;
299 0 : summary.tx_bps += stats[ i ].tx_bps;
300 0 : summary.rx_bufs_idle += stats[ i ].rx_bufs_idle;
301 0 : summary.rx_bufs_busy += stats[ i ].rx_bufs_busy;
302 0 : summary.tx_bufs_idle += stats[ i ].tx_bufs_idle;
303 0 : summary.tx_bufs_busy += stats[ i ].tx_bufs_busy;
304 0 : }
305 0 : summary.busy_r /= (double)net_tile_cnt;
306 :
307 0 : printf( "\033[2K" "Overall stats:\n" );
308 :
309 : /* Render packet per second rates */
310 0 : printf( "\033[2K" " Net busy: %.2f%%\n"
311 0 : "\033[2K" " RX ok: %10.3e pps %10.3e bps\n"
312 0 : "\033[2K" " RX drop: %10.3e pps\n"
313 0 : "\033[2K" " TX ok: %10.3e pps %10.3e bps\n"
314 0 : "\033[2K" " RX bufs: %6lu idle %6lu busy\n"
315 0 : "\033[2K" " TX bufs: %6lu idle %6lu busy\n",
316 0 : 100.*summary.busy_r,
317 0 : summary.rx_ok_pps, summary.rx_bps,
318 0 : summary.rx_drop_pps,
319 0 : summary.tx_ok_pps, summary.tx_bps,
320 0 : summary.rx_bufs_idle, summary.rx_bufs_busy,
321 0 : summary.tx_bufs_idle, summary.tx_bufs_busy );
322 :
323 0 : printf( "\033[2K" "Individual net tiles:\n" );
324 :
325 0 : for( ulong i=0UL; i<net_tile_cnt; i++ ) {
326 0 : printf( "\033[2K" " net:%-3lu busy %6.2f%% | RX(ok) %10.3e pps | TX(ok) %10.3e pps\n",
327 0 : i,
328 0 : 100.*stats[ i ].busy_r,
329 0 : stats[ i ].rx_ok_pps,
330 0 : stats[ i ].tx_ok_pps );
331 0 : }
332 :
333 0 : fputs( "\0338", stdout ); /* restore cursor position */
334 0 : fflush( stdout );
335 0 : }
336 :
337 : /* FIXME fixup screen on window size changes */
338 :
339 : void
340 : pktgen_cmd_fn( args_t * args,
341 0 : config_t * config ) {
342 0 : fd_topo_t * topo = &config->topo;
343 0 : ulong net_tile_cnt = config->layout.net_tile_count;
344 0 : if( FD_UNLIKELY( net_tile_cnt>FD_TOPO_MAX_TILES ) ) {
345 0 : FD_LOG_ERR(( "more net tiles (%lu) than max num tiles allowed (%lu)", net_tile_cnt, FD_TOPO_MAX_TILES ));
346 0 : }
347 :
348 0 : fd_topo_tile_t * metric_tile = &topo->tiles[ fd_topo_find_tile( topo, "metric", 0UL ) ];
349 0 : fd_topo_tile_t * pktgen_tile = &topo->tiles[ fd_topo_find_tile( topo, "pktgen", 0UL ) ];
350 0 : pktgen_tile->pktgen.pkt_sz = args->pktgen.pkt_sz;
351 :
352 0 : fd_topo_tile_t * net_tiles [ FD_TOPO_MAX_TILES ];
353 0 : ulong volatile const * net_metrics[ FD_TOPO_MAX_TILES ];
354 :
355 0 : ushort const listen_port = 9000;
356 0 : config->tiles.quic.regular_transaction_listen_port = listen_port;
357 0 : for( ulong kind_id=0UL; kind_id<net_tile_cnt; kind_id++ ) {
358 0 : net_tiles[ kind_id ] = &topo->tiles[ fd_topo_find_tile( topo, fd_net_tile_name( config->net.provider ), kind_id ) ];
359 0 : net_tiles[ kind_id ]->net.legacy_transaction_listen_port = listen_port;
360 0 : }
361 :
362 0 : if( FD_UNLIKELY( !fd_cstr_to_ip4_addr( config->tiles.metric.prometheus_listen_address, &metric_tile->metric.prometheus_listen_addr ) ) )
363 0 : FD_LOG_ERR(( "failed to parse prometheus listen address `%s`", config->tiles.metric.prometheus_listen_address ));
364 0 : metric_tile->metric.prometheus_listen_port = config->tiles.metric.prometheus_listen_port;
365 :
366 0 : configure_stage( &fd_cfg_stage_mlx5, CONFIGURE_CMD_INIT, config );
367 0 : configure_stage( &fd_cfg_stage_iavf, CONFIGURE_CMD_INIT, config );
368 0 : configure_stage( &fd_cfg_stage_sysctl, CONFIGURE_CMD_INIT, config );
369 0 : configure_stage( &fd_cfg_stage_hugetlbfs, CONFIGURE_CMD_INIT, config );
370 0 : configure_stage( &fd_cfg_stage_bonding, CONFIGURE_CMD_INIT, config );
371 0 : configure_stage( &fd_cfg_stage_ethtool_channels, CONFIGURE_CMD_INIT, config );
372 0 : configure_stage( &fd_cfg_stage_ethtool_offloads, CONFIGURE_CMD_INIT, config );
373 0 : configure_stage( &fd_cfg_stage_sysfs_poll, CONFIGURE_CMD_INIT, config );
374 :
375 0 : fdctl_check_configure( config );
376 : /* FIXME this allocates lots of memory unnecessarily */
377 0 : initialize_workspaces( config );
378 0 : initialize_stacks( config );
379 0 : if( 0==strcmp( config->net.provider, "xdp" ) ) {
380 0 : fd_topo_install_xdp_simple( &config->topo, config->net.bind_address_parsed );
381 0 : }
382 0 : fd_topo_join_workspaces( topo, FD_SHMEM_JOIN_MODE_READ_WRITE, FD_TOPO_CORE_DUMP_LEVEL_DISABLED );
383 0 : if( 0==strcmp( config->net.provider, "mlx5" ) ) {
384 0 : fd_topo_install_mlx5( topo, NULL );
385 0 : }
386 0 : if( 0==strcmp( config->net.provider, "iavf" ) ) {
387 0 : fd_topo_install_iavf( topo, NULL );
388 0 : }
389 :
390 : /* FIXME allow running sandboxed/multiprocess */
391 0 : fd_topo_run_single_process( topo, config->uid, config->gid, fdctl_tile_run );
392 :
393 0 : for( ulong kind_id=0UL; kind_id<net_tile_cnt; kind_id++ ) {
394 0 : net_metrics[ kind_id ] = fd_metrics_tile( net_tiles[ kind_id ]->metrics );
395 0 : }
396 :
397 : /* Don't attempt to render TTY */
398 0 : if( !isatty( STDOUT_FILENO ) ) {
399 0 : puts( "stdout is not a tty, not taking commands" );
400 0 : FD_VOLATILE( fd_pktgen_active ) = 1;
401 0 : for(;;) pause();
402 0 : return;
403 0 : }
404 :
405 : /* Clear screen */
406 0 : struct winsize w;
407 0 : if( FD_UNLIKELY( 0!=ioctl( STDOUT_FILENO, TIOCGWINSZ, &w ) ) ) {
408 0 : FD_LOG_WARNING(( "ioctl(STDOUT_FILENO,TIOCGWINSZ) failed" ));
409 0 : } else {
410 0 : for( ulong i=0UL; i<w.ws_row; i++ ) putc( '\n', stdout );
411 0 : }
412 :
413 : /* Simple REPL loop */
414 0 : puts( "Running firedancer-dev pktgen" );
415 0 : printf( "%s socket listening on port %u\n", config->net.provider, (uint)listen_port );
416 0 : puts( "Available commands: start, stop, quit" );
417 0 : puts( "" );
418 0 : char input[ 256 ] = {0};
419 0 : for(;;) {
420 0 : render_status( net_metrics, net_tile_cnt, config->net.provider, args->pktgen.pkt_sz );
421 0 : fputs( "pktgen> ", stdout );
422 0 : fflush( stdout );
423 :
424 0 : for(;;) {
425 0 : struct pollfd fds[1] = {{ .fd=STDIN_FILENO, .events=POLLIN }};
426 0 : int poll_res = poll( fds, 1, 500 );
427 0 : if( poll_res==0 ) {
428 0 : render_status( net_metrics, net_tile_cnt, config->net.provider, args->pktgen.pkt_sz );
429 0 : continue;
430 0 : } else if( poll_res>0 ) {
431 0 : break;
432 0 : } else {
433 0 : FD_LOG_ERR(( "poll(STDIN_FILENO) failed" ));
434 0 : break;
435 0 : }
436 0 : }
437 :
438 0 : if( fgets( input, sizeof(input), stdin )==NULL ) {
439 0 : putc( '\n', stdout );
440 0 : break;
441 0 : }
442 0 : input[ strcspn( input, "\n" ) ] = '\0';
443 0 : input[ sizeof(input)-1 ] = '\0';
444 :
445 0 : if( !input[0] ) {
446 : /* No command */
447 0 : } else if( !strcmp( input, "exit" ) || !strcmp( input, "quit" ) ) {
448 0 : break;
449 0 : } else if( !strcmp( input, "start" ) ) {
450 0 : FD_VOLATILE( fd_pktgen_active ) = 1U;
451 0 : } else if( !strcmp( input, "stop" ) ) {
452 0 : FD_VOLATILE( fd_pktgen_active ) = 0U;
453 0 : } else {
454 0 : fputs( "Unknown command\n", stdout );
455 0 : }
456 0 : }
457 0 : puts( "Exiting" );
458 0 : }
459 :
460 : action_t fd_action_pktgen = {
461 : .name = "pktgen",
462 : .topo = pktgen_topo,
463 : .args = pktgen_cmd_args,
464 : .fn = pktgen_cmd_fn,
465 : .perm = dev_cmd_perm,
466 : .description = "Flood interface with invalid Ethernet frames",
467 : .usage = "pktgen [--pkt-sz <bytes>]",
468 : .args_help = pktgen_args_help
469 : };
|