Skip to content

Commit

Permalink
Feature #884 add --include / --exclude options
Browse files Browse the repository at this point in the history
Add ability to select the packets you want sent
  • Loading branch information
fklassen committed Jun 24, 2024
1 parent 57bed13 commit 43b859a
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 14 deletions.
21 changes: 21 additions & 0 deletions src/send_packets.c
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,17 @@ send_packets(tcpreplay_t *ctx, pcap_t *pcap, int idx)
now_is_now = false;
packetnum++;
#if defined TCPREPLAY || defined TCPREPLAY_EDIT
/* look for include or exclude LIST match */
if (options->list != NULL) {
bool rule_set = check_list(options->list, packetnum);
if ((rule_set && options->is_exclude) || (!rule_set && !options->is_exclude)) {
dbgx(2, "packet " COUNTER_SPEC " not sent due to %s rule",
packetnum,
options->is_exclude ? "exclude" : "include");
continue;
}
}

/* do we use the snaplen (caplen) or the "actual" packet len? */
pktlen = options->use_pkthdr_len ? (COUNTER)pkthdr.len : (COUNTER)pkthdr.caplen;
#elif TCPBRIDGE
Expand Down Expand Up @@ -667,6 +678,16 @@ send_dual_packets(tcpreplay_t *ctx, pcap_t *pcap1, int cache_file_idx1, pcap_t *
while (!ctx->abort && !(pktdata1 == NULL && pktdata2 == NULL)) {
now_is_now = false;
packetnum++;
/* look for include or exclude LIST match */
if (options->list != NULL) {
bool rule_set = check_list(options->list, packetnum);
if ((rule_set && options->is_exclude) || (!rule_set && !options->is_exclude)) {
dbgx(2, "packet " COUNTER_SPEC " not sent due to %s rule",
packetnum,
options->is_exclude ? "exclude" : "include");
continue;
}
}

/* figure out which pcap file we need to process next
* when get_next_packet() returns null for pktdata, the pkthdr
Expand Down
3 changes: 3 additions & 0 deletions src/tcpreplay_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,9 @@ tcpreplay_close(tcpreplay_t *ctx)
intlist = intlistnext;
}
}

/* free --include / --exclude list */
free_list(options->list);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/tcpreplay_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "defines.h"
#include "config.h"
#include <common/interface.h>
#include <common/list.h>
#include <common/sendpacket.h>
#include <common/tcpdump.h>
#include <common/utils.h>
Expand Down Expand Up @@ -134,6 +135,10 @@ typedef struct tcpreplay_opt_s {
int source_cnt;
tcpreplay_source_t sources[MAX_FILES];

/* --include / --exclude flag and rules list */
bool is_exclude;
tcpr_list_t *list;

#ifdef ENABLE_VERBOSE
/* tcpdump verbose printing */
bool verbose;
Expand Down
92 changes: 78 additions & 14 deletions src/tcpreplay_opts.def
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ config-header = "config.h";

include = "#include \"defines.h\"\n"
"#include \"tcpreplay.h\"\n"
"#include \"tcpreplay_api.h\"\n"
"#include \"common.h\"\n"
"#include \"config.h\"\n"
"#include <stdlib.h>\n"
"#include <sys/types.h>\n"
"#include <unistd.h>\n";
"#include <unistd.h>\n"
"extern tcpreplay_t *ctx;";

homerc = "$$/";

Expand Down Expand Up @@ -286,6 +288,66 @@ option with --cachefile.
EOText;
};

flag = {
name = include;
arg-type = string;
max = 1;
descrip = "Send only selected packet numbers";
flags-cant = exclude;
flag-code = <<- EOInclude

char *include;
include = safe_strdup(OPT_ARG(INCLUDE));

ctx->options->is_exclude = false;
if (!parse_list(&ctx->options->list, include))
errx(-1, "Unable to parse include/exclude rule: %s", OPT_ARG(INCLUDE));

free(include);

EOInclude;
doc = <<- EOText
Override default of processing all packets stored in the capture file and only
send packets that are part of a supplied list of packet numbers.

@example
-x P:1-5,9,15,72-
@end example
would skip packets 1 through 5, the 9th and 15th packet, and packets 72 until the
end of the file
EOText;
};

flag = {
name = exclude;
arg-type = string;
max = 1;
descrip = "Send all but selected packet numbers";
flags-cant = include;
flag-code = <<- EOExclude

char *exclude;
exclude = safe_strdup(OPT_ARG(EXCLUDE));

ctx->options->is_exclude = true;
if (!parse_list(&ctx->options->list, exclude))
errx(-1, "Unable to parse include/exclude rule: %s", OPT_ARG(EXCLUDE));

free(exclude);

EOExclude;
doc = <<- EOText
Override default of processing all packets stored in the capture file and only
send packets that are NOT part of a supplied list of packet numbers.

@example
-x P:1-5,9,15,72-
@end example
would skip packets 1 through 5, the 9th and 15th packet, and packets 72 until the
end of the file
EOText;
};


flag = {
ifdef = ENABLE_PCAP_FINDALLDEVS;
Expand Down Expand Up @@ -543,19 +605,6 @@ are fully up before netmap transmit. Requires netmap option. Default is 10 secon
EOText;
};

flag = {
ifdef = HAVE_LIBXDP;
name = xdp;
descrip = "Write packets directly to AF_XDP enabled network adapter";
doc = <<- EOText
This feature will detect AF_XDP capable network drivers on Linux systems
that have 'libxdp-dev' and 'libbpf-dev' installed. If detected, the network
stack is bypassed and packets are sent directly to an eBPF enabled driver directly.
This will allow you to achieve full line rates on commodity network adapters, similar to rates
achieved by commercial network traffic generators.
EOText;
};


flag = {
name = no-flow-stats;
Expand Down Expand Up @@ -626,6 +675,21 @@ sending packets may cause equally long delays between printing statistics.
EOText;
};


flag = {
ifdef = HAVE_LIBXDP;
name = xdp;
descrip = "Write packets directly to AF_XDP enabled network adapter";
doc = <<- EOText
This feature will detect AF_XDP capable network drivers on Linux systems
that have 'libxdp-dev' and 'libbpf-dev' installed. If detected, the network
stack is bypassed and packets are sent directly to an eBPF enabled driver directly.
This will allow you to achieve full line rates on commodity network adapters, similar to rates
achieved by commercial network traffic generators.
EOText;
};


flag = {
ifdef = HAVE_LIBXDP;
name = xdp-batch-size;
Expand Down

0 comments on commit 43b859a

Please sign in to comment.