From 126e0038a73b38fac7b3e03173b2d791734cc497 Mon Sep 17 00:00:00 2001 From: Vadim Kochan Date: Tue, 15 Dec 2015 23:09:15 +0200 Subject: trafgen: Added option to pass macro/define for C preprocessor Add -D,--define option which allows to pass multiple macro/defines which can be used in trafgen script (e.g. by #ifdef ). Signed-off-by: Vadim Kochan Signed-off-by: Tobias Klauser --- trafgen.8 | 4 ++++ trafgen.c | 19 +++++++++++++++---- trafgen_conf.h | 3 ++- trafgen_parser.y | 5 +++-- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/trafgen.8 b/trafgen.8 index 18b2b61..6f5d318 100644 --- a/trafgen.8 +++ b/trafgen.8 @@ -79,6 +79,10 @@ Pass the packet configuration to the C preprocessor before reading it into trafgen. This allows #define and #include directives (e.g. to include definitions from system headers) to be used in the trafgen configuration file. .PP +.SS -D =, --define = +Add macro definition for the C preprocessor to use it within trafgen file. This +option is used in combination with the -p,--cpp option. +.PP .SS -J, --jumbo-support By default trafgen's ring buffer frames are of a fixed size of 2048 bytes. This means that if you're expecting jumbo frames or even super jumbo frames to diff --git a/trafgen.c b/trafgen.c index d01f160..df2ecf4 100644 --- a/trafgen.c +++ b/trafgen.c @@ -83,7 +83,7 @@ size_t plen = 0; struct packet_dyn *packet_dyn = NULL; size_t dlen = 0; -static const char *short_options = "d:c:n:t:vJhS:rk:i:o:VRs:P:eE:pu:g:CHQq"; +static const char *short_options = "d:c:n:t:vJhS:rk:i:o:VRs:P:eE:pu:g:CHQqD:"; static const struct option long_options[] = { {"dev", required_argument, NULL, 'd'}, {"out", required_argument, NULL, 'o'}, @@ -105,6 +105,7 @@ static const struct option long_options[] = { {"jumbo-support", no_argument, NULL, 'J'}, {"no-cpu-stats", no_argument, NULL, 'C'}, {"cpp", no_argument, NULL, 'p'}, + {"define", required_argument, NULL, 'D'}, {"rfraw", no_argument, NULL, 'R'}, {"rand", no_argument, NULL, 'r'}, {"verbose", no_argument, NULL, 'V'}, @@ -163,6 +164,7 @@ static void __noreturn help(void) " -i|-c|--in|--conf Packet configuration file/stdin\n" " -o|-d|--out|--dev Networking device i.e., eth0\n" " -p|--cpp Run packet config through C preprocessor\n" + " -D|--define Add macro/define for C preprocessor\n" " -J|--jumbo-support Support 64KB super jumbo frames (def: 2048B)\n" " -R|--rfraw Inject raw 802.11 frames\n" " -s|--smoke-test Probe if machine survived fuzz-tested packet\n" @@ -823,12 +825,13 @@ static void xmit_packet_precheck(struct ctx *ctx, unsigned int cpu) } static void main_loop(struct ctx *ctx, char *confname, bool slow, - unsigned int cpu, bool invoke_cpp, unsigned long orig_num) + unsigned int cpu, bool invoke_cpp, char **cpp_argv, + unsigned long orig_num) { if (ctx->packet_str) compile_packets_str(ctx->packet_str, ctx->verbose, cpu); else - compile_packets(confname, ctx->verbose, cpu, invoke_cpp); + compile_packets(confname, ctx->verbose, cpu, invoke_cpp, cpp_argv); preprocess_packets(); @@ -896,6 +899,8 @@ int main(int argc, char **argv) unsigned long long tx_packets, tx_bytes; struct ctx ctx; int min_opts = 5; + char **cpp_argv = NULL; + size_t cpp_argc = 0; fmemset(&ctx, 0, sizeof(ctx)); ctx.cpus = get_number_cpus_online(); @@ -924,6 +929,10 @@ int main(int argc, char **argv) case 'p': invoke_cpp = true; break; + case 'D': + cpp_argv = argv_insert(cpp_argv, &cpp_argc, "-D"); + cpp_argv = argv_insert(cpp_argv, &cpp_argc, optarg); + break; case 'V': ctx.verbose = true; break; @@ -1133,7 +1142,8 @@ int main(int argc, char **argv) srand(seed); cpu_affinity(i); - main_loop(&ctx, confname, slow, i, invoke_cpp, orig_num); + main_loop(&ctx, confname, slow, i, invoke_cpp, + cpp_argv, orig_num); goto thread_out; case -1: @@ -1179,6 +1189,7 @@ thread_out: if (set_irq_aff) device_restore_irq_affinity_list(); + argv_free(cpp_argv); free(ctx.device); free(ctx.device_trans); free(ctx.rhost); diff --git a/trafgen_conf.h b/trafgen_conf.h index deadb7c..db3258f 100644 --- a/trafgen_conf.h +++ b/trafgen_conf.h @@ -56,7 +56,8 @@ static inline bool packet_dyn_has_only_csums(struct packet_dyn *p) } extern void compile_packets_str(char *str, bool verbose, unsigned int cpu); -extern void compile_packets(char *file, bool verbose, unsigned int cpu, bool invoke_cpp); +extern void compile_packets(char *file, bool verbose, unsigned int cpu, + bool invoke_cpp, char **cpp_argv); extern void cleanup_packets(void); #endif /* TRAFGEN_CONF */ diff --git a/trafgen_parser.y b/trafgen_parser.y index 8a0f3b7..24370ee 100644 --- a/trafgen_parser.y +++ b/trafgen_parser.y @@ -590,7 +590,8 @@ void cleanup_packets(void) free(packet_dyn); } -void compile_packets(char *file, bool verbose, unsigned int cpu, bool invoke_cpp) +void compile_packets(char *file, bool verbose, unsigned int cpu, bool invoke_cpp, + char **cpp_argv) { char tmp_file[128]; int ret = -1; @@ -599,7 +600,7 @@ void compile_packets(char *file, bool verbose, unsigned int cpu, bool invoke_cpp our_cpu = cpu; if (invoke_cpp) { - if (cpp_exec(file, tmp_file, sizeof(tmp_file), NULL)) { + if (cpp_exec(file, tmp_file, sizeof(tmp_file), cpp_argv)) { fprintf(stderr, "Failed to invoke C preprocessor!\n"); goto err; } -- cgit v1.2.3-54-g00ecf