From b5d757ae6de3b28f326644c63f7e2b9df4109764 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Mon, 2 Nov 2015 16:33:54 +0100 Subject: trafgen: Pre-calculate checksums if possible The TCP/UDP checksums cannot be calculated in the parser as the packet payload following the TCP/UDP header is not yet know. However, we can calculate these checksums before the send loop if the only dynamic elements of the packet are checksums. This change avoids the overhead of recalculating the checksums for every iteration the send loop in these cases. Suggested-by: Daniel Borkmann Signed-off-by: Tobias Klauser --- trafgen.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/trafgen.c b/trafgen.c index c35182e..8752255 100644 --- a/trafgen.c +++ b/trafgen.c @@ -366,6 +366,21 @@ static void apply_csum16(int id) } } +static void preprocess_packets(void) +{ + size_t i; + + for (i = 0; i < plen; i++) { + struct packet_dyn *pktd = &packet_dyn[i]; + + if (packet_dyn_has_only_csums(pktd)) { + apply_csum16(i); + pktd->slen = 0; + xfree(pktd->csum); + } + } +} + static struct cpu_stats *setup_shared_var(unsigned int cpus) { int fd; @@ -539,6 +554,8 @@ static void xmit_slowpath_or_die(struct ctx *ctx, unsigned int cpu, unsigned lon drop_privileges(ctx->enforce, ctx->uid, ctx->gid); + preprocess_packets(); + bug_on(gettimeofday(&start, NULL)); while (likely(sigint == 0 && num > 0 && plen > 0)) { @@ -628,6 +645,8 @@ static void xmit_fastpath_or_die(struct ctx *ctx, unsigned int cpu, unsigned lon if (ctx->num == 0 && orig_num > 0) num = 0; + preprocess_packets(); + bug_on(gettimeofday(&start, NULL)); while (likely(sigint == 0 && num > 0 && plen > 0)) { -- cgit v1.2.3-54-g00ecf