-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharg_parser.c
More file actions
1112 lines (1009 loc) · 38 KB
/
Copy patharg_parser.c
File metadata and controls
1112 lines (1009 loc) · 38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SPDX-License-Identifier: Apache-2.0
* Copyright 2026 Zhaochen Zhang and Alibaba Cloud Computing Co., Ltd.
*/
#include <ctype.h>
#include <getopt.h>
#include <rte_argparse.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <yaml.h>
#include "common.h"
#include "fake_roce_pkt.h"
#include "state_machine.h"
#include "utils.h"
// Look up the index in peer_info matching the given IP string; returns -1 if not found
int get_peer_idx_from_ip(char *ip)
{
if (!ip || !peer_info)
return -1;
for (int i = 0; i < peer_cnt; ++i) {
if (peer_info[i].ip_str && strcmp(peer_info[i].ip_str, ip) == 0) {
return i;
}
}
printf(":: error: peer IP %s not found in peer_info\n", ip);
return -1;
}
// Build a new peer_info array from the given index array, containing only the peers at the specified indices
peer_info_t *get_peer_list_from_idx(uint16_t *idx, uint16_t cnt)
{
if (!idx || cnt == 0 || !peer_info)
return NULL;
peer_info_t *list = calloc(cnt, sizeof(peer_info_t));
if (!list)
return NULL;
for (uint16_t i = 0; i < cnt; ++i) {
int src = idx[i];
if (src < 0 || src >= peer_cnt) {
printf(":: error: idx[%d]=%d out of range\n", i, src);
free(list);
return NULL;
}
// Shallow-copy the pointers and struct members
list[i] = peer_info[src];
}
return list;
}
/* ========== Document API helper functions ========== */
// Get the scalar string value of a node; returns NULL if the node is not a scalar
static const char *get_scalar_value(yaml_node_t *node)
{
if (!node || node->type != YAML_SCALAR_NODE)
return NULL;
return (const char *)node->data.scalar.value;
}
// Find the value node for the given key within a mapping node
static yaml_node_t *find_mapping_value(yaml_document_t *doc, yaml_node_t *mapping, const char *key)
{
if (!doc || !mapping || !key || mapping->type != YAML_MAPPING_NODE)
return NULL;
yaml_node_pair_t *pair;
for (pair = mapping->data.mapping.pairs.start; pair < mapping->data.mapping.pairs.top; pair++) {
yaml_node_t *key_node = yaml_document_get_node(doc, pair->key);
const char *key_str = get_scalar_value(key_node);
if (key_str && strcmp(key_str, key) == 0) {
return yaml_document_get_node(doc, pair->value);
}
}
return NULL;
}
// Parse a scalar node as an integer
static int get_int_value(yaml_node_t *node, int default_value)
{
const char *str = get_scalar_value(node);
return str ? atoi(str) : default_value;
}
// Parse a scalar node as an unsigned integer (base 0, supports decimal and 0x hex, e.g. PSN 0x1000)
static uint32_t get_uint_value(yaml_node_t *node, uint32_t default_value)
{
const char *str = get_scalar_value(node);
return str ? (uint32_t)strtoul(str, NULL, 0) : default_value;
}
// Parse a scalar node as a boolean (supports true/false/1/0)
static bool get_bool_value(yaml_node_t *node, bool default_value)
{
const char *str = get_scalar_value(node);
if (!str)
return default_value;
return (strcmp(str, "true") == 0 || strcmp(str, "1") == 0);
}
// Determine whether a scalar string is a decimal integer
static bool is_numeric_scalar(const char *str)
{
if (!str || *str == '\0')
return false;
if (*str == '+' || *str == '-') {
str++;
if (*str == '\0')
return false;
}
while (*str) {
if (!isdigit((unsigned char)*str))
return false;
str++;
}
return true;
}
// Resolve a scalar to a peer index; accepts either a direct index or an IP address
static int resolve_peer_idx_from_scalar(const char *value, const char *label)
{
if (!value)
return -1;
int idx = -1;
if (is_numeric_scalar(value)) {
idx = atoi(value);
if (idx < 0) {
printf(":: error: %s index %d is negative\n", label ? label : "peer", idx);
return -1;
}
printf(":: %s resolved numeric idx: %d\n", label ? label : "peer", idx);
} else {
idx = get_peer_idx_from_ip((char *)value);
printf(":: %s ip: %s, idx: %d\n", label ? label : "peer", value, idx);
}
return idx;
}
// Count the number of elements contained in a sequence node
static int get_sequence_length(yaml_node_t *sequence_node)
{
if (!sequence_node || sequence_node->type != YAML_SEQUENCE_NODE)
return -1;
return (int)(sequence_node->data.sequence.items.top - sequence_node->data.sequence.items.start);
}
/* ========== Section parsing functions ========== */
// Parse a single CNP state object
static int parse_cnp_state_mapping(yaml_document_t *doc, yaml_node_t *state_node)
{
if (!state_node || state_node->type != YAML_MAPPING_NODE) {
printf(":: error: CNP state node is not a mapping\n");
return -1;
}
uint32_t duration_us = 0;
task_execute_mode_t send_cnp = TASK_EXECUTE_NONE;
uint32_t cnp_interval_us = 0;
// Iterate over all fields of the state object
yaml_node_pair_t *pair;
for (pair = state_node->data.mapping.pairs.start; pair < state_node->data.mapping.pairs.top; pair++) {
yaml_node_t *key_node = yaml_document_get_node(doc, pair->key);
yaml_node_t *value_node = yaml_document_get_node(doc, pair->value);
const char *key = get_scalar_value(key_node);
const char *value = get_scalar_value(value_node);
if (!key || !value)
continue;
printf(":: parsing state key: %s, value: %s\n", key, value);
if (strcmp(key, "duration_us") == 0) {
duration_us = (uint32_t)atoi(value);
} else if (strcmp(key, "send_cnp") == 0) {
if (strcmp(value, "NONE") == 0) {
send_cnp = TASK_EXECUTE_NONE;
} else if (strcmp(value, "ONCE") == 0) {
send_cnp = TASK_EXECUTE_ONCE;
} else if (strcmp(value, "MANY") == 0) {
send_cnp = TASK_EXECUTE_MANY;
}
} else if (strcmp(key, "cnp_interval_us") == 0) {
cnp_interval_us = (uint32_t)atoi(value);
}
}
// Add the parsed state
printf(":: adding CNP state: duration_us=%u, send_cnp=%d, cnp_interval_us=%u\n", duration_us, send_cnp,
cnp_interval_us);
if (duration_us > 0) {
if (add_cnp_state_legacy(g_cnp_state_machine, duration_us, send_cnp, 0, 0, cnp_interval_us) != 0) {
printf(":: error: failed to add CNP state\n");
return -1;
} else {
printf(":: successfully added CNP state\n");
}
} else {
printf(":: warning: skipping state with duration_us=0\n");
}
return 0;
}
// Parse the cnp_state_machine section
static int parse_cnp_state_machine_section(yaml_document_t *doc, yaml_node_t *section)
{
if (!section || section->type != YAML_MAPPING_NODE)
return -1;
// Check whether it is enabled
yaml_node_t *enabled_node = find_mapping_value(doc, section, "enabled");
if (enabled_node && get_bool_value(enabled_node, false)) {
g_cnp_state_machine = create_state_machine();
if (g_cnp_state_machine == NULL) {
printf(":: error: failed to create CNP state machine\n");
return -1;
}
if (init_state_machine(g_cnp_state_machine) != 0) {
printf(":: error: failed to initialize CNP state machine\n");
destroy_state_machine(g_cnp_state_machine);
g_cnp_state_machine = NULL;
return -1;
}
}
// Parse state_count (optional)
yaml_node_t *count_node = find_mapping_value(doc, section, "state_count");
if (count_node) {
printf(":: cnp_state_machine state_count: %d\n", get_int_value(count_node, 0));
}
// Parse the states array
yaml_node_t *states_node = find_mapping_value(doc, section, "states");
if (states_node && states_node->type == YAML_SEQUENCE_NODE) {
printf(":: parsing cnp_state_machine states sequence\n");
yaml_node_item_t *item;
for (item = states_node->data.sequence.items.start; item < states_node->data.sequence.items.top; item++) {
yaml_node_t *state_node = yaml_document_get_node(doc, *item);
if (parse_cnp_state_mapping(doc, state_node) != 0) {
printf(":: error: failed to parse CNP state mapping\n");
}
}
}
return 0;
}
// Parse the experiment section
static int parse_experiment_section(yaml_document_t *doc, yaml_node_t *section)
{
if (!section || section->type != YAML_MAPPING_NODE)
return -1;
yaml_node_pair_t *pair;
for (pair = section->data.mapping.pairs.start; pair < section->data.mapping.pairs.top; pair++) {
yaml_node_t *key_node = yaml_document_get_node(doc, pair->key);
yaml_node_t *value_node = yaml_document_get_node(doc, pair->value);
const char *key = get_scalar_value(key_node);
if (!key)
continue;
if (strcmp(key, "exp_mode") == 0) {
const char *mode = get_scalar_value(value_node);
if (mode) {
if (strcmp(mode, "SENDER") == 0)
cur_exp_mode = SENDER;
else if (strcmp(mode, "RECEIVER") == 0)
cur_exp_mode = RECEIVER;
else if (strcmp(mode, "SWITCH") == 0)
cur_exp_mode = SWITCH;
else if (strcmp(mode, "ROCE") == 0)
cur_exp_mode = ROCE;
else if (strcmp(mode, "RECEIVER_CUSTOM_SEQ") == 0)
cur_exp_mode = RECEIVER_CUSTOM_SEQ;
else if (strcmp(mode, "RP_EXP_CAPABILITY_TEST") == 0)
cur_exp_mode = RP_EXP_CAPABILITY_TEST;
else
printf(":: error: unknown exp_mode '%s'\n", mode);
}
printf(":: exp_mode: %s\n", mode);
} else if (strcmp(key, "send_interval_ns") == 0) {
fake_roce_flow_params.send_interval_ns = get_int_value(value_node, 0);
} else if (strcmp(key, "ackreq_interval") == 0) {
fake_roce_flow_params.ack_req_interval = get_int_value(value_node, 0);
} else if (strcmp(key, "gen_bench_enabled") == 0) {
gen_bench_enabled = get_bool_value(value_node, false);
printf(":: gen_bench_enabled: %s\n", gen_bench_enabled ? "true" : "false");
} else if (strcmp(key, "gen_bench_target") == 0) {
const char *t = get_scalar_value(value_node);
if (t) {
if (strcmp(t, "multi") == 0) {
gen_bench_target = GEN_BENCH_TARGET_MULTI;
} else if (strcmp(t, "dynpsn") == 0) {
gen_bench_target = GEN_BENCH_TARGET_DYNPSN;
} else {
printf(":: error: unknown gen_bench_target '%s' (use 'multi' or 'dynpsn')\n", t);
}
}
} else if (strcmp(key, "gen_bench_duration_s") == 0) {
gen_bench_duration_s = (uint32_t)get_int_value(value_node, 0);
} else if (strcmp(key, "gen_bench_warmup_s") == 0) {
gen_bench_warmup_s = (uint32_t)get_int_value(value_node, 0);
} else if (strcmp(key, "gen_bench_wqe_len") == 0) {
gen_bench_wqe_len = (uint32_t)get_int_value(value_node, 0);
} else if (strcmp(key, "fixed_prepared_pkt_num_enabled") == 0) {
fixed_prepared_pkt_num_enabled = get_bool_value(value_node, false);
printf(":: fixed_prepared_pkt_num_enabled: %s\n", fixed_prepared_pkt_num_enabled ? "true" : "false");
} else if (strcmp(key, "dummy_rdma_conn_enabled") == 0) {
dummy_rdma_conn_enabled = get_bool_value(value_node, false);
printf(":: dummy_rdma_conn_enabled: %s\n", dummy_rdma_conn_enabled ? "true" : "false");
} else if (strcmp(key, "qp_cnt") == 0) {
qp_cnt = get_int_value(value_node, 0);
} else if (strcmp(key, "qp_mod") == 0) {
qp_mod = get_int_value(value_node, 0);
} else if (strcmp(key, "customed_mtu") == 0) {
customed_mtu = get_int_value(value_node, 0);
} else if (strcmp(key, "padding_pkt_size") == 0) {
fake_roce_flow_params.padding_pkt_size = get_int_value(value_node, 0);
} else if (strcmp(key, "padding_pkt_cnt") == 0) {
fake_roce_flow_params.padding_pkt_cnt = get_int_value(value_node, 0);
} else if (strcmp(key, "tail_padding_size") == 0) {
fake_roce_flow_params.tail_padding_size = get_int_value(value_node, 0);
} else if (strcmp(key, "tx_batch_size") == 0) {
fake_roce_flow_params.tx_batch_size = (uint16_t)get_int_value(value_node, 0);
} else if (strcmp(key, "tx_mode") == 0) {
const char *m = get_scalar_value(value_node);
if (m) {
if (strcmp(m, "default") == 0) {
fake_roce_flow_params.tx_mode = TX_MODE_DEFAULT;
} else if (strcmp(m, "dynpsn") == 0) {
fake_roce_flow_params.tx_mode = TX_MODE_DYNAMIC_PSN;
} else if (strcmp(m, "dynpsn_mt") == 0) {
fake_roce_flow_params.tx_mode = TX_MODE_DYNAMIC_PSN_MT;
} else {
printf(":: error: unknown tx_mode '%s' (use 'default' / 'dynpsn' / 'dynpsn_mt')\n", m);
}
printf(":: tx_mode: %s\n", m);
}
} else if (strcmp(key, "auto_stop_seconds") == 0) {
auto_stop_seconds = (uint32_t)get_int_value(value_node, 0);
} else if (strcmp(key, "send_times") == 0) {
send_times = get_int_value(value_node, 0);
} else if (strcmp(key, "detailed_ts_enabled") == 0) {
detailed_ts_enabled = get_bool_value(value_node, false);
} else if (strcmp(key, "detailed_lat_enabled") == 0) {
detailed_lat_enabled = get_bool_value(value_node, false);
} else if (strcmp(key, "detailed_enabled_qps") == 0 && value_node->type == YAML_SEQUENCE_NODE) {
// Parse the array of QP indices for which detailed statistics are enabled
int count = 0;
yaml_node_item_t *it;
for (it = value_node->data.sequence.items.start; it < value_node->data.sequence.items.top; it++) {
count++;
}
if (detailed_enabled_qps) {
free(detailed_enabled_qps);
detailed_enabled_qps = NULL;
}
detailed_enabled_qps_count = 0;
if (count > 0) {
detailed_enabled_qps = calloc(count, sizeof(uint16_t));
if (!detailed_enabled_qps) {
printf(":: error: failed to allocate detailed_enabled_qps list (count=%d)\n", count);
return -1;
}
int idx = 0;
for (it = value_node->data.sequence.items.start; it < value_node->data.sequence.items.top; it++) {
yaml_node_t *n = yaml_document_get_node(doc, *it);
uint16_t qp_index = (uint16_t)get_int_value(n, 0);
detailed_enabled_qps[idx++] = qp_index;
printf(":: detailed_enabled_qps[%d]: %u\n", idx - 1, qp_index);
}
detailed_enabled_qps_count = count;
printf(":: detailed_enabled_qps enabled with %d entries\n", detailed_enabled_qps_count);
}
} else if (strcmp(key, "opcode") == 0) {
const char *opcode_str = get_scalar_value(value_node);
if (opcode_str) {
if (strcmp(opcode_str, "WRITE") == 0)
fake_roce_flow_params.opcode = WRITE;
else if (strcmp(opcode_str, "WRITE_ONLY") == 0)
fake_roce_flow_params.opcode = WRITE_ONLY;
else
printf(":: error: unknown opcode '%s'\n", opcode_str);
}
} else if (strcmp(key, "customed_qp_dscp") == 0 && value_node->type == YAML_SEQUENCE_NODE) {
// Parse the DSCP array
int dscp_idx = 0;
yaml_node_item_t *item;
for (item = value_node->data.sequence.items.start;
item < value_node->data.sequence.items.top && dscp_idx < 8; item++) {
yaml_node_t *dscp_node = yaml_document_get_node(doc, *item);
customed_qp_dscp_list[dscp_idx] = get_int_value(dscp_node, 0);
printf(":: customed_qp_dscp[%d]: %u\n", dscp_idx, customed_qp_dscp_list[dscp_idx]);
dscp_idx++;
}
customed_qp_dscp_enabled = true;
printf(":: customed_qp_dscp enabled with %d values\n", dscp_idx);
} else if (strcmp(key, "cnp_response_min_interval_us") == 0) {
g_cnp_response_min_interval_us = (uint32_t)get_int_value(value_node, 0);
printf(":: cnp_response_min_interval_us: %u\n", g_cnp_response_min_interval_us);
} else if (strcmp(key, "custom_sequence") == 0 && value_node->type == YAML_SEQUENCE_NODE) {
// Parse the custom packet sequence (each element is a mapping)
// Fields: rp_idx, qp_idx, psn, ack_req, ecn, opcode
int count = 0;
yaml_node_item_t *it;
for (it = value_node->data.sequence.items.start; it < value_node->data.sequence.items.top; it++) {
count++;
}
if (g_custom_pkt_sequence) {
free(g_custom_pkt_sequence);
g_custom_pkt_sequence = NULL;
}
g_custom_pkt_sequence_len = 0;
if (count <= 0) {
printf(":: error: custom_sequence is empty\n");
return -1;
}
g_custom_pkt_sequence = calloc(count, sizeof(custom_pkt_sequence_t));
if (!g_custom_pkt_sequence) {
printf(":: error: failed to allocate custom_sequence (count=%d)\n", count);
return -1;
}
int idx = 0;
for (it = value_node->data.sequence.items.start; it < value_node->data.sequence.items.top; it++) {
yaml_node_t *entry = yaml_document_get_node(doc, *it);
if (!entry || entry->type != YAML_MAPPING_NODE) {
printf(":: error: custom_sequence[%d] is not a mapping\n", idx);
free(g_custom_pkt_sequence);
g_custom_pkt_sequence = NULL;
return -1;
}
const char *opcode_str = get_scalar_value(find_mapping_value(doc, entry, "opcode"));
int opcode = parse_custom_seq_opcode(opcode_str);
if (opcode < 0) {
printf(":: error: custom_sequence[%d] unknown opcode '%s'\n", idx,
opcode_str ? opcode_str : "(null)");
free(g_custom_pkt_sequence);
g_custom_pkt_sequence = NULL;
return -1;
}
g_custom_pkt_sequence[idx].rp_idx =
(uint16_t)get_uint_value(find_mapping_value(doc, entry, "rp_idx"), 0);
g_custom_pkt_sequence[idx].qp_idx =
(uint16_t)get_uint_value(find_mapping_value(doc, entry, "qp_idx"), 0);
g_custom_pkt_sequence[idx].psn = get_uint_value(find_mapping_value(doc, entry, "psn"), 0);
g_custom_pkt_sequence[idx].ack_req =
(uint8_t)get_uint_value(find_mapping_value(doc, entry, "ack_req"), 0);
g_custom_pkt_sequence[idx].ecn = (uint8_t)get_uint_value(find_mapping_value(doc, entry, "ecn"), 0);
g_custom_pkt_sequence[idx].opcode = (uint8_t)opcode;
printf(":: custom_sequence[%d]: rp=%u qp=%u psn=0x%x ack_req=%u ecn=%u opcode=%s\n", idx,
g_custom_pkt_sequence[idx].rp_idx, g_custom_pkt_sequence[idx].qp_idx,
g_custom_pkt_sequence[idx].psn, g_custom_pkt_sequence[idx].ack_req,
g_custom_pkt_sequence[idx].ecn, opcode_str);
idx++;
}
g_custom_pkt_sequence_len = (uint32_t)count;
printf(":: custom_sequence loaded with %d entries\n", count);
}
}
return 0;
}
// Parse the servers section
static int parse_servers_section(yaml_document_t *doc, yaml_node_t *section)
{
if (!section || section->type != YAML_MAPPING_NODE)
return -1;
yaml_node_pair_t *pair;
for (pair = section->data.mapping.pairs.start; pair < section->data.mapping.pairs.top; pair++) {
yaml_node_t *key_node = yaml_document_get_node(doc, pair->key);
yaml_node_t *value_node = yaml_document_get_node(doc, pair->value);
const char *key = get_scalar_value(key_node);
if (!key)
continue;
if (strcmp(key, "cp") == 0) {
const char *cp_value = get_scalar_value(value_node);
if (cp_value) {
int idx = resolve_peer_idx_from_scalar(cp_value, "cp");
if (idx >= 0)
server_config.cp_idx = (uint16_t)idx;
}
} else if (strcmp(key, "np") == 0) {
const char *np_value = get_scalar_value(value_node);
if (np_value) {
int idx = resolve_peer_idx_from_scalar(np_value, "np");
if (idx >= 0)
server_config.np_idx = (uint16_t)idx;
}
} else if (strcmp(key, "peer_ip") == 0 && value_node->type == YAML_SEQUENCE_NODE) {
// Parse the peer_ip array
int ip_count = get_sequence_length(value_node);
if (ip_count < 0) {
printf(":: error: failed to count peer_ip entries\n");
return -1;
}
peer_cnt = ip_count;
printf(":: peer_cnt inferred from peer_ip array: %d\n", peer_cnt);
if (peer_cnt == 0) {
printf(":: warning: peer_cnt is zero, skipping peer_ip parsing\n");
continue;
}
if (!peer_info) {
peer_info = calloc(peer_cnt, sizeof(peer_info_t));
if (!peer_info) {
printf(":: error: failed to allocate peer_info array\n");
return -1;
}
}
if (ip_count > peer_cnt) {
printf(":: warning: peer_ip entries (%d) exceed peer_cnt (%d), extra entries ignored\n", ip_count,
peer_cnt);
}
int ip_idx = 0;
yaml_node_item_t *item;
for (item = value_node->data.sequence.items.start;
item < value_node->data.sequence.items.top && ip_idx < peer_cnt; item++) {
yaml_node_t *ip_node = yaml_document_get_node(doc, *item);
const char *ip_str = get_scalar_value(ip_node);
if (ip_str) {
peer_info[ip_idx].ip_str = strdup(ip_str);
printf(":: peer_ip[%d]: %s\n", ip_idx, ip_str);
ip_idx++;
}
}
} else if (strcmp(key, "rp") == 0 && value_node->type == YAML_SEQUENCE_NODE) {
// Parse the rp array
int rp_value_cnt = get_sequence_length(value_node);
if (rp_value_cnt < 0) {
printf(":: error: failed to count rp entries\n");
return -1;
}
server_config.rp_cnt = rp_value_cnt;
printf(":: rp_cnt inferred from rp array: %d\n", server_config.rp_cnt);
if (server_config.rp_cnt == 0) {
printf(":: warning: rp_cnt is zero, skipping rp parsing\n");
continue;
}
if (!server_config.rp_idx) {
server_config.rp_idx = calloc(server_config.rp_cnt, sizeof(uint16_t));
if (!server_config.rp_idx) {
printf(":: error: failed to allocate rp_idx array\n");
return -1;
}
}
if (rp_value_cnt > server_config.rp_cnt) {
printf(":: warning: rp entries (%d) exceed rp_cnt (%d), extra entries ignored\n", rp_value_cnt,
server_config.rp_cnt);
}
int rp_idx = 0;
yaml_node_item_t *item;
for (item = value_node->data.sequence.items.start;
item < value_node->data.sequence.items.top && rp_idx < server_config.rp_cnt; item++) {
yaml_node_t *rp_node = yaml_document_get_node(doc, *item);
const char *rp_value = get_scalar_value(rp_node);
if (!rp_value)
continue;
int resolved_idx = resolve_peer_idx_from_scalar(rp_value, "rp");
if (resolved_idx < 0)
continue;
server_config.rp_idx[rp_idx] = (uint16_t)resolved_idx;
printf(":: rp_idx[%d] resolved to %d\n", rp_idx, resolved_idx);
rp_idx++;
}
}
}
return 0;
}
// Parse a single QP RDMA state configuration
static int parse_qp_rdma_state_config(yaml_document_t *doc, yaml_node_t *state_node, qp_rdma_state_config_t *config)
{
if (!state_node || state_node->type != YAML_MAPPING_NODE || !config) {
printf(":: error: QP RDMA state node is not a mapping\n");
return -1;
}
// Initialize default values
config->duration_us = 0;
config->send_rdma = TASK_EXECUTE_NONE;
config->rdma_interval_us = 0;
config->rdma_len = 0; // 0 means use the default value
// Iterate over all fields of the state object
yaml_node_pair_t *pair;
for (pair = state_node->data.mapping.pairs.start; pair < state_node->data.mapping.pairs.top; pair++) {
yaml_node_t *key_node = yaml_document_get_node(doc, pair->key);
yaml_node_t *value_node = yaml_document_get_node(doc, pair->value);
const char *key = get_scalar_value(key_node);
const char *value = get_scalar_value(value_node);
if (!key || !value)
continue;
if (strcmp(key, "duration_us") == 0) {
config->duration_us = (uint32_t)atoi(value);
} else if (strcmp(key, "send_rdma") == 0) {
if (strcmp(value, "NONE") == 0) {
config->send_rdma = TASK_EXECUTE_NONE;
} else if (strcmp(value, "ONCE") == 0) {
config->send_rdma = TASK_EXECUTE_ONCE;
} else if (strcmp(value, "MANY") == 0) {
config->send_rdma = TASK_EXECUTE_MANY;
} else {
printf(":: error: unknown send_rdma mode '%s'\n", value);
}
} else if (strcmp(key, "rdma_interval_us") == 0) {
config->rdma_interval_us = (uint32_t)atoi(value);
} else if (strcmp(key, "rdma_len") == 0) {
config->rdma_len = (uint32_t)atoi(value);
}
}
printf(":: parsed QP RDMA state: duration_us=%u, send_rdma=%d, rdma_interval_us=%u, rdma_len=%u\n",
config->duration_us, config->send_rdma, config->rdma_interval_us, config->rdma_len);
return 0;
}
// Apply the state configuration to the specified list of QPs
static int apply_states_to_qps(yaml_document_t *doc, yaml_node_t *qp_indices_node, yaml_node_t *states_node,
yaml_node_t *max_depth_node)
{
if (!qp_indices_node || !states_node) {
printf(":: error: missing qp_indices or states\n");
return -1;
}
// Parse the QP index list
if (qp_indices_node->type != YAML_SEQUENCE_NODE) {
printf(":: error: qp_indices is not a sequence\n");
return -1;
}
// Parse the states array
if (states_node->type != YAML_SEQUENCE_NODE) {
printf(":: error: states is not a sequence\n");
return -1;
}
// Count the number of states
int state_count = 0;
yaml_node_item_t *item;
for (item = states_node->data.sequence.items.start; item < states_node->data.sequence.items.top; item++) {
state_count++;
}
if (state_count == 0) {
printf(":: warning: no states defined for QPs\n");
return 0;
}
printf(":: parsed %d states\n", state_count);
// Allocate a temporary states array
qp_rdma_state_config_t *states = calloc(state_count, sizeof(qp_rdma_state_config_t));
if (!states) {
printf(":: error: failed to allocate states array\n");
return -1;
}
// Parse each state
int state_idx = 0;
for (item = states_node->data.sequence.items.start; item < states_node->data.sequence.items.top; item++) {
yaml_node_t *state_node = yaml_document_get_node(doc, *item);
if (parse_qp_rdma_state_config(doc, state_node, &states[state_idx]) != 0) {
printf(":: error: failed to parse QP RDMA state %d\n", state_idx);
free(states);
return -1;
}
state_idx++;
}
// Parse the parallel max_depth (applies to all QPs affected by this configuration; 0 means ignore)
int configured_max_depth = 0;
if (max_depth_node) {
configured_max_depth = get_int_value(max_depth_node, 0);
if (configured_max_depth < 0)
configured_max_depth = 0;
}
// Apply to each QP: directly create the state machine and the parallel parameter arrays
for (item = qp_indices_node->data.sequence.items.start; item < qp_indices_node->data.sequence.items.top; item++) {
yaml_node_t *qp_idx_node = yaml_document_get_node(doc, *item);
int qp_index = get_int_value(qp_idx_node, -1);
if (qp_index < 0 || qp_index >= g_qp_state_machine_info.qp_count) {
printf(":: error: QP index %d out of range [0, %d)\n", qp_index, g_qp_state_machine_info.qp_count);
free(states);
return -1;
}
// If a state machine has not yet been created for this QP, create and initialize it
if (g_qp_state_machine_info.state_machines[qp_index] == NULL) {
g_qp_state_machine_info.state_machines[qp_index] = create_state_machine();
if (!g_qp_state_machine_info.state_machines[qp_index]) {
printf(":: error: failed to create state machine for QP %d\n", qp_index);
free(states);
return -1;
}
if (init_state_machine(g_qp_state_machine_info.state_machines[qp_index]) != 0) {
printf(":: error: failed to init state machine for QP %d\n", qp_index);
destroy_state_machine(g_qp_state_machine_info.state_machines[qp_index]);
g_qp_state_machine_info.state_machines[qp_index] = NULL;
free(states);
return -1;
}
}
// Allocate the parallel rdma_len array for this QP (overwriting the old one)
if (g_qp_state_machine_info.rdma_len[qp_index]) {
free(g_qp_state_machine_info.rdma_len[qp_index]);
}
g_qp_state_machine_info.rdma_len[qp_index] = calloc(state_count, sizeof(uint32_t));
if (!g_qp_state_machine_info.rdma_len[qp_index]) {
printf(":: error: failed to allocate rdma_len array for QP %d\n", qp_index);
free(states);
return -1;
}
// Record the number of states
g_qp_state_machine_info.state_counts[qp_index] = state_count;
// Add the states to the state machine (tasks empty for now) and fill in the parallel rdma_len array
for (int si = 0; si < state_count; si++) {
qp_rdma_state_config_t *cfg = &states[si];
task_t empty = create_empty_task();
if (add_state(g_qp_state_machine_info.state_machines[qp_index], cfg->duration_us, cfg->send_rdma, &empty,
cfg->rdma_interval_us) != 0) {
printf(":: error: add_state failed for QP %d state %d\n", qp_index, si);
free(states);
return -1;
}
g_qp_state_machine_info.rdma_len[qp_index][si] = cfg->rdma_len;
}
// Apply max_depth (if configured)
if (configured_max_depth > 0) {
if (g_qp_state_machine_info.qp_max_depth) {
g_qp_state_machine_info.qp_max_depth[qp_index] = (uint32_t)configured_max_depth;
}
printf(":: QP[%d]: max_depth=%d\n", qp_index, configured_max_depth);
}
printf(":: built state_machine for QP %d with %d states\n", qp_index, state_count);
}
free(states);
return 0;
}
// Parse the qp_state_machines section
static int parse_qp_state_machines_section(yaml_document_t *doc, yaml_node_t *section)
{
if (!section || section->type != YAML_MAPPING_NODE)
return -1;
printf(":: parsing qp_state_machines section\n");
// Check whether it is enabled
yaml_node_t *enabled_node = find_mapping_value(doc, section, "enabled");
if (!enabled_node || !get_bool_value(enabled_node, false)) {
printf(":: qp_state_machines not enabled, skipping\n");
return 0;
}
// Ensure the QP count is known
if (qp_cnt == 0) {
printf(":: error: qp_cnt not set before parsing qp_state_machines\n");
return -1;
}
// Initialize the new build-artifact arrays
g_qp_state_machine_info.qp_count = qp_cnt; // still used for index range validation
// Allocate the pointer arrays
g_qp_state_machine_info.state_machines = calloc(g_qp_state_machine_info.qp_count, sizeof(state_machine_t *));
g_qp_state_machine_info.rdma_len = calloc(g_qp_state_machine_info.qp_count, sizeof(uint32_t *));
g_qp_state_machine_info.state_counts = calloc(g_qp_state_machine_info.qp_count, sizeof(int));
g_qp_state_machine_info.qp_max_depth = calloc(g_qp_state_machine_info.qp_count, sizeof(uint32_t));
if (!g_qp_state_machine_info.state_machines || !g_qp_state_machine_info.rdma_len ||
!g_qp_state_machine_info.state_counts || !g_qp_state_machine_info.qp_max_depth) {
printf(":: error: failed to allocate qp state machine build artifacts\n");
return -1;
}
printf(":: initialized qp state machine build artifacts for %d QPs\n", g_qp_state_machine_info.qp_count);
// Parse the qps array
yaml_node_t *qps_node = find_mapping_value(doc, section, "qps");
if (!qps_node || qps_node->type != YAML_SEQUENCE_NODE) {
printf(":: error: qps is not a sequence\n");
return -1;
}
// Iterate over each QP configuration
yaml_node_item_t *item;
for (item = qps_node->data.sequence.items.start; item < qps_node->data.sequence.items.top; item++) {
yaml_node_t *qp_config_node = yaml_document_get_node(doc, *item);
if (qp_config_node->type != YAML_MAPPING_NODE) {
printf(":: error: qp config is not a mapping\n");
continue;
}
// Look up qp_indices, states, and the optional max_depth
yaml_node_t *qp_indices_node = find_mapping_value(doc, qp_config_node, "qp_indices");
yaml_node_t *states_node = find_mapping_value(doc, qp_config_node, "states");
yaml_node_t *max_depth_node = find_mapping_value(doc, qp_config_node, "max_depth");
if (apply_states_to_qps(doc, qp_indices_node, states_node, max_depth_node) != 0) {
printf(":: error: failed to apply states to QPs\n");
return -1;
}
}
// Print summary
printf(":: QP state machine build summary:\n");
for (int i = 0; i < g_qp_state_machine_info.qp_count; i++) {
if (g_qp_state_machine_info.state_machines && g_qp_state_machine_info.state_machines[i] &&
g_qp_state_machine_info.state_counts && g_qp_state_machine_info.state_counts[i] > 0) {
printf(":: QP[%d]: %d states\n", i, g_qp_state_machine_info.state_counts[i]);
} else {
printf(":: QP[%d]: not configured (will be skipped)\n", i);
}
}
return 0;
}
// Parse the YAML configuration file
int parse_yaml_config(const char *filename)
{
FILE *fh = fopen(filename, "r");
if (!fh) {
printf(":: error: cannot open config file %s\n", filename);
return -1;
}
yaml_parser_t parser;
yaml_document_t document;
int ret = 0;
// Initialize the parser
if (!yaml_parser_initialize(&parser)) {
printf(":: error: failed to initialize YAML parser\n");
fclose(fh);
return -1;
}
yaml_parser_set_input_file(&parser, fh);
// Load the document
if (!yaml_parser_load(&parser, &document)) {
printf(":: error: failed to load YAML document\n");
yaml_parser_delete(&parser);
fclose(fh);
return -1;
}
// Get the root node (should be a mapping)
yaml_node_t *root = yaml_document_get_root_node(&document);
if (!root) {
printf(":: error: empty YAML document\n");
ret = -1;
goto cleanup;
}
if (root->type != YAML_MAPPING_NODE) {
printf(":: error: root node is not a mapping\n");
ret = -1;
goto cleanup;
}
// Iterate over the top-level sections
yaml_node_pair_t *pair;
for (pair = root->data.mapping.pairs.start; pair < root->data.mapping.pairs.top; pair++) {
yaml_node_t *key_node = yaml_document_get_node(&document, pair->key);
yaml_node_t *value_node = yaml_document_get_node(&document, pair->value);
const char *section_name = get_scalar_value(key_node);
if (!section_name)
continue;
printf(":: parsing section: %s\n", section_name);
if (strcmp(section_name, "servers") == 0) {
ret = parse_servers_section(&document, value_node);
} else if (strcmp(section_name, "experiment") == 0) {
ret = parse_experiment_section(&document, value_node);
} else if (strcmp(section_name, "cnp_state_machine") == 0) {
ret = parse_cnp_state_machine_section(&document, value_node);
} else if (strcmp(section_name, "qp_state_machines") == 0) {
ret = parse_qp_state_machines_section(&document, value_node);
} else {
printf(":: warning: unknown section '%s'\n", section_name);
}
if (ret != 0) {
printf(":: error: failed to parse section '%s'\n", section_name);
goto cleanup;
}
}
cleanup:
yaml_document_delete(&document);
yaml_parser_delete(&parser);
fclose(fh);
return ret;
}
/**
* @brief Clean up the helper arrays from the QP state machine build phase (does not destroy the state machines
* themselves)
*/
void cleanup_qp_state_machine_build_artifacts(void)
{
if (g_qp_state_machine_info.rdma_len) {
for (int i = 0; i < g_qp_state_machine_info.qp_count; i++) {
if (g_qp_state_machine_info.rdma_len[i]) {
free(g_qp_state_machine_info.rdma_len[i]);
g_qp_state_machine_info.rdma_len[i] = NULL;
}
}
free(g_qp_state_machine_info.rdma_len);
g_qp_state_machine_info.rdma_len = NULL;
}
if (g_qp_state_machine_info.state_machines) {
// Note: this only frees the pointer array itself, not the state machine objects it points to
free(g_qp_state_machine_info.state_machines);
g_qp_state_machine_info.state_machines = NULL;
}
if (g_qp_state_machine_info.state_counts) {
free(g_qp_state_machine_info.state_counts);
g_qp_state_machine_info.state_counts = NULL;
}
if (g_qp_state_machine_info.qp_max_depth) {
free(g_qp_state_machine_info.qp_max_depth);
g_qp_state_machine_info.qp_max_depth = NULL;
}
}
/* Arg parser for command line arguments used in DPDK 24.11
int anytest_parse_args_string(uint32_t index, const char *value, void *opaque)
{
// This function is used to parse string arguments.
if (index == 0) { // --config or -c
if (value == NULL || strlen(value) == 0) {
printf(":: error: --peer requires a value\n");
return -1;
}
if (strlen(value) >= sizeof(g_config_file)) {
printf(":: error: --config value is too long\n");
return -1;
}
strncpy(g_config_file, value, sizeof(g_config_file) - 1);
g_config_file[sizeof(g_config_file) - 1] = 0;
printf(":: config file set to: %s\n", g_config_file);
} else if (index == 1) { // --local-ip or -l
local_ip_str = strdup(value);
}
return 0;
}
// Parse the argument given in the command line of the application
int anytest_parse_args(int argc, char **argv)
{
int ret = 0;
static struct rte_argparse obj = {
.prog_name = "anytest",
.usage = "[EAL options] -- [optional parameters]",
.descriptor = NULL,
.epilog = NULL,
.exit_on_error = false,
.callback = anytest_parse_args_string,
.opaque = NULL,
.args =
{
{
"--config",
"-c",
"Config file path in YAML format",
NULL,
(void *)0,
RTE_ARGPARSE_ARG_REQUIRED_VALUE,
},
{
"--local-ip",
"-l",
"Local ipv4 address",
NULL,