Skip to content

Commit

Permalink
Add -U / -K options to control flow distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
cardigliano committed May 15, 2024
1 parent 1cfe9a6 commit d0941a1
Showing 1 changed file with 42 additions and 15 deletions.
57 changes: 42 additions & 15 deletions userland/examples/pfsend.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -230,8 +231,10 @@ void printHelp(void) {
printf("-D <ip> Use <ip> as destination IP (default: 192.168.0.1)\n");
printf("-V <version> Generate IP version <version> packets (default: 4, mixed: 0)\n");
printf("-8 <num> Send the same packets <num> times before moving to the next\n");
printf("-A <num> Add <num> different packets (e.g. -b) every second\n");
printf("-O On the fly reforging instead of preprocessing (-b)\n");
printf("-A <num> Add <num> different packets every second up to -b\n");
printf("-U <num> Add K more different packets (-K) every <num> packets up to -b\n");
printf("-K <num> Add <num> more different packets every -U packets up to -b\n");
printf("-z Randomize generated IPs sequence (requires -b)\n");
printf("-o <num> Offset for generated IPs (-b) or packets in pcap (-f)\n");
printf("-W <ID>[,<ID>] Forge VLAN packets with the specified VLAN ID (and QinQ ID if specified after comma)\n");
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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...");
Expand Down Expand Up @@ -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++;
Expand Down

0 comments on commit d0941a1

Please sign in to comment.