Skip to content

Commit 4320408

Browse files
committed
Update tracereplay to use trace_construct_packet_zc()
* improves performance by reducing memory copies * avoids adding padding to replayed packets that had been truncated when captured
1 parent 8a7b835 commit 4320408

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

tools/tracereplay/tracereplay.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ static void replace_transport_checksum(libtrace_packet_t *packet)
8686
cryptopan. Same for TCP and UDP. No other protocols are supported at the
8787
moment.
8888
*/
89-
static libtrace_packet_t *per_packet(libtrace_packet_t *packet)
90-
{
89+
static libtrace_packet_t *per_packet(libtrace_packet_t *packet,
90+
char **localbuf) {
9191
uint32_t remaining = 0;
9292
libtrace_linktype_t linktype = 0;
9393
libtrace_packet_t *new_packet;
@@ -97,6 +97,7 @@ static libtrace_packet_t *per_packet(libtrace_packet_t *packet)
9797
int i;
9898
char *newbuf;
9999

100+
*localbuf = NULL;
100101
if (IS_LIBTRACE_META_PACKET(packet)) {
101102
return NULL;
102103
}
@@ -127,10 +128,13 @@ static libtrace_packet_t *per_packet(libtrace_packet_t *packet)
127128
memcpy(newbuf, FAKE_ETHERNET_HEADER, sizeof(libtrace_ether_t));
128129
l2_header = newbuf;
129130
wire_length += sizeof(libtrace_ether_t);
131+
remaining += sizeof(libtrace_ether_t);
130132
linktype = TRACE_TYPE_ETH;
133+
*localbuf = newbuf;
131134
}
132135

133-
trace_construct_packet(new_packet, linktype, l2_header, wire_length);
136+
trace_construct_packet_zc(new_packet, linktype, l2_header, remaining,
137+
wire_length);
134138
new_packet = trace_strip_packet(new_packet);
135139

136140
if (broadcast) {
@@ -236,6 +240,7 @@ int main(int argc, char *argv[])
236240
int speedup = 1;
237241
int tx_max_queue = 1;
238242
bool tx_max_set = 0;
243+
char *localbuf = NULL;
239244

240245
while (1) {
241246
int option_index;
@@ -358,29 +363,36 @@ int main(int argc, char *argv[])
358363
}
359364

360365
/* Got a packet - let's do something with it */
361-
new = per_packet(packet);
366+
new = per_packet(packet, &localbuf);
362367

363368
if (!new)
364369
continue;
365370

366371
if (trace_write_packet(output, new) < 0) {
372+
if (localbuf) {
373+
free(localbuf);
374+
}
367375
trace_perror_output(output, "Writing packet");
368376
trace_destroy(trace);
369377
trace_destroy_output(output);
370378
trace_destroy_packet(packet);
371379
return 1;
372380
}
381+
382+
if (localbuf) {
383+
free(localbuf);
384+
}
373385
trace_destroy_packet(new);
374386
}
375387
if (trace_is_err(trace)) {
376388
trace_perror(trace, "%s", uri);
377389
}
378390
free(uri);
379-
trace_destroy(trace);
380391
if (filter != NULL) {
381392
trace_destroy_filter(filter);
382393
}
383394
trace_destroy_output(output);
384395
trace_destroy_packet(packet);
396+
trace_destroy(trace);
385397
return 0;
386398
}

0 commit comments

Comments
 (0)