Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simulate packet loss #755

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/send_packets.c
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,11 @@ send_packets(tcpreplay_t *ctx, pcap_t *pcap, int idx)
#endif

dbgx(2, "Sending packet #" COUNTER_SPEC, packetnum);
/* write packet out on network */
if (sendpacket(sp, pktdata, pktlen, &pkthdr) < (int)pktlen) {
/* write packet out on network, skipping it in random cases when asked for drop */
if (
(options->drop == 1.0f || rand() > options->drop * RAND_MAX) &&
sendpacket(sp, pktdata, pktlen, &pkthdr) < (int)pktlen
) {
warnx("Unable to send packet: %s", sendpacket_geterr(sp));
break;
}
Expand Down
3 changes: 3 additions & 0 deletions src/tcpreplay.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifdef HAVE_FTS_H
#include <fts.h>
#endif
Expand Down Expand Up @@ -66,6 +67,8 @@ main(int argc, char *argv[])

fflush(NULL);

srand(time(NULL));

ctx = tcpreplay_init();
optct = optionProcess(&tcpreplayOptions, argc, argv);
argc -= optct;
Expand Down
6 changes: 6 additions & 0 deletions src/tcpreplay_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ tcpreplay_post_args(tcpreplay_t *ctx, int argc)
options->speed.multiplier = atof(OPT_ARG(MULTIPLIER));
}

if (HAVE_OPT(DROP)) {
options->drop = atof(OPT_ARG(DROP));
} else {
options->drop = 1.0f;
}

if (HAVE_OPT(MAXSLEEP)) {
options->maxsleep.tv_sec = OPT_VALUE_MAXSLEEP / 1000;
options->maxsleep.tv_nsec = (OPT_VALUE_MAXSLEEP % 1000) * 1000 * 1000;
Expand Down
1 change: 1 addition & 0 deletions src/tcpreplay_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ typedef struct tcpreplay_opt_s {
tcpreplay_speed_t speed;
COUNTER loop;
u_int32_t loopdelay_ms;
float drop;

int stats;
bool use_pkthdr_len;
Expand Down
14 changes: 14 additions & 0 deletions src/tcpreplay_opts.def
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,20 @@ thus allowing for longer sleep times which can be more accurately implemented.
EOText;
};

flag = {
name = drop;
arg-type = string;
max = 1;
descrip = "Simluate random packet loss";
doc = <<- EOText
Set to a value between 0 and 1 to simulate packet loss at send.
Examples:
@example
0.25 will drop around one quarter of packets (they will not be sent)
@end example
EOText;
};

flag = {
name = unique-ip;
flags-must = loop;
Expand Down