Skip to content

Commit

Permalink
xdp-synproxy: code optimization to reduce latency
Browse files Browse the repository at this point in the history
the XDP synproxy program from kernel selftest
seems aiming to test the correctness of BPF infrastructure.
not necessarily aiming for production code efficiency,
and production performance. Make a few code optimizations
to reduce real production latency.

see #7

Signed-off-by: Vincent Li <[email protected]>
  • Loading branch information
vincentmli committed Nov 10, 2024
1 parent e2a03b1 commit 0659e96
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions xdp-synproxy/xdp_synproxy.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
#define DEFAULT_WSCALE 7
#define DEFAULT_TTL 64
#define MAX_ALLOWED_PORTS 8
#define MAX_WINDOW_SIZE 65535

#define MAX_PACKET_OFF 0xffff

Expand Down Expand Up @@ -265,11 +266,8 @@ static int tscookie_tcpopt_parse(struct tcpopt_context *ctx)

static int tscookie_tcpopt_parse_batch(__u32 index, void *context)
{
int i;

for (i = 0; i < 7; i++)
if (tscookie_tcpopt_parse(context))
return 1;
if (tscookie_tcpopt_parse(context))
return 1;
return 0;
}

Expand All @@ -293,7 +291,9 @@ static __always_inline bool tscookie_init(struct tcphdr *tcp_header,
};
u32 cookie;

bpf_loop(6, tscookie_tcpopt_parse_batch, &loop_ctx, 0);
/* limit bpf_loop to number of tcp options */
u32 tcp_opts = tcp_len > 20 ? (tcp_len - 20) : 0;
bpf_loop(tcp_opts, tscookie_tcpopt_parse_batch, &loop_ctx, 0);

if (!loop_ctx.option_timestamp)
return false;
Expand Down Expand Up @@ -524,12 +524,12 @@ static __always_inline void tcp_gen_synack(struct tcphdr *tcp_header,
swap(tcp_header->source, tcp_header->dest);
tcp_header->ack_seq = bpf_htonl(bpf_ntohl(tcp_header->seq) + 1);
tcp_header->seq = bpf_htonl(cookie);
tcp_header->window = 0;
tcp_header->window = bpf_htons(MAX_WINDOW_SIZE); /* set window size to max window size */
tcp_header->urg_ptr = 0;
tcp_header->check = 0; /* Calculate checksum later. */

tcp_options = (void *)(tcp_header + 1);
tcp_header->doff += tcp_mkoptions(tcp_options, tsopt, mss, wscale);
tcp_header->doff += (tcp_mkoptions(tcp_options, tsopt, mss, wscale)) / 4;
}

static __always_inline void tcpv4_gen_synack(struct header_pointers *hdr,
Expand Down

0 comments on commit 0659e96

Please sign in to comment.