From d0941a1ee2a7b34c6862db10d1c4596a5a546053 Mon Sep 17 00:00:00 2001 From: Alfredo Cardigliano Date: Wed, 15 May 2024 12:47:42 +0200 Subject: [PATCH] Add -U / -K options to control flow distribution --- userland/examples/pfsend.c | 57 ++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 15 deletions(-) diff --git a/userland/examples/pfsend.c b/userland/examples/pfsend.c index 2001e2b93..2c359686e 100644 --- a/userland/examples/pfsend.c +++ b/userland/examples/pfsend.c @@ -97,6 +97,7 @@ pfring_stat pfringStats; char *device = NULL, *twin_device = NULL, *cidr = NULL; u_int8_t wait_for_packet = 1, do_shutdown = 0; u_int32_t pkt_loop = 0, pkt_loop_sent = 0, uniq_pkts_per_sec = 0; +u_int32_t uniq_k_pkts_per_pkts = 1, uniq_pkts_per_pkts = 0; u_int64_t num_pkt_good_sent = 0, last_num_pkt_good_sent = 0; u_int64_t num_bytes_good_sent = 0, last_num_bytes_good_sent = 0; u_int32_t mtu = 1500; @@ -230,8 +231,10 @@ void printHelp(void) { printf("-D Use as destination IP (default: 192.168.0.1)\n"); printf("-V Generate IP version packets (default: 4, mixed: 0)\n"); printf("-8 Send the same packets times before moving to the next\n"); - printf("-A Add different packets (e.g. -b) every second\n"); printf("-O On the fly reforging instead of preprocessing (-b)\n"); + printf("-A Add different packets every second up to -b\n"); + printf("-U Add K more different packets (-K) every packets up to -b\n"); + printf("-K Add more different packets every -U packets up to -b\n"); printf("-z Randomize generated IPs sequence (requires -b)\n"); printf("-o Offset for generated IPs (-b) or packets in pcap (-f)\n"); printf("-W [,] Forge VLAN packets with the specified VLAN ID (and QinQ ID if specified after comma)\n"); @@ -444,8 +447,9 @@ int main(int argc, char* argv[]) { #if !(defined(__arm__) || defined(__mips__)) ticks tick_start = 0, tick_prev = 0, tick_delta = 0; #endif - u_int32_t uniq_pkts_limit = 0; - u_int32_t on_the_fly_sent = 0; + u_int64_t curr_uniq_pkts_limit = 0; + u_int64_t on_the_fly_sent = 0; + u_int64_t uniq_pkts_per_pkts_cnt = 0; ticks hz = 0; struct packet *tosend; int num_uniq_pkts = 1, watermark = 0; @@ -467,7 +471,7 @@ int main(int argc, char* argv[]) { srcaddr.s_addr = 0x0100000A /* 10.0.0.1 */; dstaddr.s_addr = 0x0100A8C0 /* 192.168.0.1 */; - while((c = getopt(argc, argv, "A:b:B:c:dD:hi:n:g:G:l:L:o:Oaf:Fr:vm:M:p:P:S:t:V:w:W:z8:0:")) != -1) { + while((c = getopt(argc, argv, "A:b:B:c:dD:hi:n:g:G:l:L:o:Oaf:Fr:vm:M:p:P:S:t:K:U:V:w:W:z8:0:")) != -1) { switch(c) { case 'A': uniq_pkts_per_sec = atoi(optarg); @@ -575,6 +579,12 @@ int main(int argc, char* argv[]) { case 'P': pidFileName = strdup(optarg); break; + case 'K': + uniq_k_pkts_per_pkts = atoi(optarg); + break; + case 'U': + uniq_pkts_per_pkts = atoi(optarg); + break; case 'V': ip_v = atoi(optarg); break; @@ -944,7 +954,9 @@ int main(int argc, char* argv[]) { reforging_idx = 0; if (uniq_pkts_per_sec) { /* init limit */ - uniq_pkts_limit = uniq_pkts_per_sec; + curr_uniq_pkts_limit = uniq_pkts_per_sec; + } else if (uniq_pkts_per_pkts) { + curr_uniq_pkts_limit = 1; } pfring_set_application_stats(pd, "Statistics not yet computed: please try again..."); @@ -1066,21 +1078,36 @@ int main(int argc, char* argv[]) { } } - /* add N uniq packets per second */ - if (uniq_pkts_per_sec) { - if (uniq_pkts_limit < num_uniq_pkts) { - if (getticks() - tick_prev > hz) { - /* 1s elapsed, add N uniq packets */ - uniq_pkts_limit += uniq_pkts_per_sec; - tick_prev = getticks(); + if (uniq_pkts_per_sec || uniq_pkts_per_pkts) { + + if (curr_uniq_pkts_limit < num_uniq_pkts) { + if (uniq_pkts_per_sec) { + /* add N uniq packets per second */ + if (getticks() - tick_prev > hz) { + /* 1s elapsed, add N uniq packets */ + curr_uniq_pkts_limit += uniq_pkts_per_sec; + tick_prev = getticks(); + } + } else if (uniq_pkts_per_pkts) { + /* add K more uniq packet every N packets */ + uniq_pkts_per_pkts_cnt++; + if (uniq_pkts_per_pkts_cnt >= uniq_pkts_per_pkts) { + uniq_pkts_per_pkts_cnt = 0; + curr_uniq_pkts_limit += uniq_k_pkts_per_pkts; + } } } + /* check the uniq packets limit */ - if (tosend->id >= uniq_pkts_limit) + if (tosend->id >= curr_uniq_pkts_limit) tosend = pkt_head; - if (on_the_fly_reforging && on_the_fly_sent >= uniq_pkts_limit) - on_the_fly_sent = 0; + + if (on_the_fly_reforging) { + if (on_the_fly_sent >= curr_uniq_pkts_limit) + on_the_fly_sent = 0; + } } + #endif if(num_to_send > 0) i++;