Skip to contentSkip to conversation

New BPF helpers to accelerate synproxy

PATCH Superseded Maxim Mikityanskiy 4 participants 15 messages Apr 22, 2022 – Apr 27, 2022

Subsystems (9)

v6 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v6 cover New BPF helpers to accelerate synproxy

The first patch of this series is an improvement to the existing syncookie BPF helper. The second patch is a documentation fix.

The third patch allows BPF helpers to accept memory regions of fixed size without doing runtime size checks.

The two last patches add new functionality that allows XDP to accelerate iptables synproxy.

v1 of this series [1] used to include a patch that exposed conntrack lookup to BPF using stable helpers. It was superseded by series [2] by Kumar Kartikeya Dwivedi, which implements this functionality using unstable helpers.

The fourth patch adds new helpers to issue and check SYN cookies without binding to a socket, which is useful in the synproxy scenario.

The fifth patch adds a selftest, which consists of a script, an XDP program and a userspace control application. The XDP program uses socketless SYN cookie helpers and queries conntrack status instead of socket status. The userspace control application allows to tune parameters of the XDP program. This program also serves as a minimal example of usage of the new functionality.

The draft of the new functionality was presented on Netdev 0x15 [3].

v2 changes:

Split into two series, submitted bugfixes to bpf, dropped the conntrack patches, implemented the timestamp cookie in BPF using bpf_loop, dropped the timestamp cookie patch.

v3 changes:

Moved some patches from bpf to bpf-next, dropped the patch that changed error codes, split the new helpers into IPv4/IPv6, added verifier functionality to accept memory regions of fixed size.

v4 changes:

Converted the selftest to the test_progs runner. Replaced some deprecated functions in xdp_synproxy userspace helper.

v5 changes:

Fixed a bug in the selftest. Added questionable functionality to support new helpers in TC BPF, added selftests for it.

v6 changes:

Wrap the new helpers themselves into #ifdef CONFIG_SYN_COOKIES, replaced fclose with pclose and fixed the MSS for IPv6 in the selftest.

[1]: https://lore.kernel.org/bpf/[email protected]/t/
[2]: https://lore.kernel.org/bpf/[email protected]/
[3]: https://netdevconf.info/0x15/session.html?Accelerating-synproxy-with-XDP

Maxim Mikityanskiy (6):
bpf: Use ipv6_only_sock in bpf_tcp_gen_syncookie
bpf: Fix documentation of th_len in bpf_tcp_{gen,check}_syncookie
bpf: Allow helpers to accept pointers with a fixed size
bpf: Add helpers to issue and check SYN cookies in XDP
bpf: Add selftests for raw syncookie helpers
bpf: Allow the new syncookie helpers to work with SKBs

 include/linux/bpf.h                           |  10 +
 include/net/tcp.h                             |   1 +
 include/uapi/linux/bpf.h                      |  88 +-
 kernel/bpf/verifier.c                         |  26 +-
 net/core/filter.c                             | 130 ++-
 net/ipv4/tcp_input.c                          |   3 +-
 scripts/bpf_doc.py                            |   4 +
 tools/include/uapi/linux/bpf.h                |  88 +-
 tools/testing/selftests/bpf/.gitignore        |   1 +
 tools/testing/selftests/bpf/Makefile          |   2 +-
 .../selftests/bpf/prog_tests/xdp_synproxy.c   | 144 +++
 .../selftests/bpf/progs/xdp_synproxy_kern.c   | 819 ++++++++++++++++++
 tools/testing/selftests/bpf/xdp_synproxy.c    | 466 ++++++++++
 13 files changed, 1760 insertions(+), 22 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/xdp_synproxy.c
 create mode 100644 tools/testing/selftests/bpf/progs/xdp_synproxy_kern.c
 create mode 100644 tools/testing/selftests/bpf/xdp_synproxy.c
Signature · 1 line
2.30.2
#1 lore raw

PATCH v6 1/6 bpf: Use ipv6_only_sock in bpf_tcp_gen_syncookie

Instead of querying the sk_ipv6only field directly, use the dedicated ipv6_only_sock helper.

---
 net/core/filter.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/core/filter.c +1 -1
index 8847316ee20e..207a13db5c80 100644
@@ -7099,7 +7099,7 @@ BPF_CALL_5(bpf_tcp_gen_syncookie, struct sock *, sk, void *, iph, u32, iph_len,
 	 */
 	switch (((struct iphdr *)iph)->version) {
 	case 4:
-		if (sk->sk_family == AF_INET6 && sk->sk_ipv6only)
+		if (sk->sk_family == AF_INET6 && ipv6_only_sock(sk))
 			return -EINVAL;
 
 		mss = tcp_v4_get_syncookie(sk, iph, th, &cookie);
Signature · 1 line
2.30.2
#2 lore raw

PATCH v6 2/6 bpf: Fix documentation of th_len in bpf_tcp_{gen,check}_syncookie

In reply to Maxim Mikityanskiy

bpf_tcp_gen_syncookie expects the full length of the TCP header (with all options), and bpf_tcp_check_syncookie accepts lengths bigger than sizeof(struct tcphdr). Fix the documentation that says these lengths should be exactly sizeof(struct tcphdr).

While at it, fix a typo in the name of struct ipv6hdr.

---
 include/uapi/linux/bpf.h       | 10 ++++++----
 tools/include/uapi/linux/bpf.h | 10 ++++++----
 2 files changed, 12 insertions(+), 8 deletions(-)
include/uapi/linux/bpf.h +6 -4
index d14b10b85e51..5e1679af8282 100644
@@ -3587,10 +3587,11 @@ union bpf_attr {
  *
  * 		*iph* points to the start of the IPv4 or IPv6 header, while
  * 		*iph_len* contains **sizeof**\ (**struct iphdr**) or
- * 		**sizeof**\ (**struct ip6hdr**).
+ * 		**sizeof**\ (**struct ipv6hdr**).
  *
  * 		*th* points to the start of the TCP header, while *th_len*
- * 		contains **sizeof**\ (**struct tcphdr**).
+ *		contains the length of the TCP header (at least
+ *		**sizeof**\ (**struct tcphdr**)).
  * 	Return
  * 		0 if *iph* and *th* are a valid SYN cookie ACK, or a negative
  * 		error otherwise.
@@ -3773,10 +3774,11 @@ union bpf_attr {
  *
  *		*iph* points to the start of the IPv4 or IPv6 header, while
  *		*iph_len* contains **sizeof**\ (**struct iphdr**) or
- *		**sizeof**\ (**struct ip6hdr**).
+ *		**sizeof**\ (**struct ipv6hdr**).
  *
  *		*th* points to the start of the TCP header, while *th_len*
- *		contains the length of the TCP header.
+ *		contains the length of the TCP header with options (at least
+ *		**sizeof**\ (**struct tcphdr**)).
  *	Return
  *		On success, lower 32 bits hold the generated SYN cookie in
  *		followed by 16 bits which hold the MSS value for that cookie,
tools/include/uapi/linux/bpf.h +6 -4
index d14b10b85e51..5e1679af8282 100644
@@ -3587,10 +3587,11 @@ union bpf_attr {
  *
  * 		*iph* points to the start of the IPv4 or IPv6 header, while
  * 		*iph_len* contains **sizeof**\ (**struct iphdr**) or
- * 		**sizeof**\ (**struct ip6hdr**).
+ * 		**sizeof**\ (**struct ipv6hdr**).
  *
  * 		*th* points to the start of the TCP header, while *th_len*
- * 		contains **sizeof**\ (**struct tcphdr**).
+ *		contains the length of the TCP header (at least
+ *		**sizeof**\ (**struct tcphdr**)).
  * 	Return
  * 		0 if *iph* and *th* are a valid SYN cookie ACK, or a negative
  * 		error otherwise.
@@ -3773,10 +3774,11 @@ union bpf_attr {
  *
  *		*iph* points to the start of the IPv4 or IPv6 header, while
  *		*iph_len* contains **sizeof**\ (**struct iphdr**) or
- *		**sizeof**\ (**struct ip6hdr**).
+ *		**sizeof**\ (**struct ipv6hdr**).
  *
  *		*th* points to the start of the TCP header, while *th_len*
- *		contains the length of the TCP header.
+ *		contains the length of the TCP header with options (at least
+ *		**sizeof**\ (**struct tcphdr**)).
  *	Return
  *		On success, lower 32 bits hold the generated SYN cookie in
  *		followed by 16 bits which hold the MSS value for that cookie,
Signature · 1 line
2.30.2
#3 lore raw

PATCH v6 3/6 bpf: Allow helpers to accept pointers with a fixed size

In reply to Maxim Mikityanskiy

Before this commit, the BPF verifier required ARG_PTR_TO_MEM arguments to be followed by ARG_CONST_SIZE holding the size of the memory region. The helpers had to check that size in runtime.

There are cases where the size expected by a helper is a compile-time constant. Checking it in runtime is an unnecessary overhead and waste of BPF registers.

This commit allows helpers to accept ARG_PTR_TO_MEM arguments without the corresponding ARG_CONST_SIZE, given that they define the memory region size in struct bpf_func_proto.

---
 include/linux/bpf.h   | 10 ++++++++++
 kernel/bpf/verifier.c | 26 +++++++++++++++-----------
 2 files changed, 25 insertions(+), 11 deletions(-)
include/linux/bpf.h +10 -0
index 7bf441563ffc..914b571bbf3a 100644
@@ -465,6 +465,16 @@ struct bpf_func_proto {
 		};
 		u32 *arg_btf_id[5];
 	};
+	union {
+		struct {
+			size_t arg1_size;
+			size_t arg2_size;
+			size_t arg3_size;
+			size_t arg4_size;
+			size_t arg5_size;
+		};
+		size_t arg_size[5];
+	};
 	int *ret_btf_id; /* return value btf_id */
 	bool (*allowed)(const struct bpf_prog *prog);
 };
kernel/bpf/verifier.c +15 -11
index 71827d14724a..368fab3dfca5 100644
@@ -5602,6 +5602,11 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
 		 * next is_mem_size argument below.
 		 */
 		meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MEM);
+		if (fn->arg_size[arg]) {
+			err = check_helper_mem_access(env, regno,
+						      fn->arg_size[arg], false,
+						      meta);
+		}
 	} else if (arg_type_is_mem_size(arg_type)) {
 		bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO);
 
@@ -5941,13 +5946,12 @@ static bool check_raw_mode_ok(const struct bpf_func_proto *fn)
 	return count <= 1;
 }
 
-static bool check_args_pair_invalid(enum bpf_arg_type arg_curr,
-				    enum bpf_arg_type arg_next)
+static bool check_args_pair_invalid(const struct bpf_func_proto *fn, int arg)
 {
-	return (arg_type_is_mem_ptr(arg_curr) &&
-	        !arg_type_is_mem_size(arg_next)) ||
-	       (!arg_type_is_mem_ptr(arg_curr) &&
-		arg_type_is_mem_size(arg_next));
+	if (arg_type_is_mem_ptr(fn->arg_type[arg]))
+		return arg_type_is_mem_size(fn->arg_type[arg + 1]) ==
+			!!fn->arg_size[arg];
+	return arg_type_is_mem_size(fn->arg_type[arg + 1]) || fn->arg_size[arg];
 }
 
 static bool check_arg_pair_ok(const struct bpf_func_proto *fn)
@@ -5958,11 +5962,11 @@ static bool check_arg_pair_ok(const struct bpf_func_proto *fn)
 	 * helper function specification.
 	 */
 	if (arg_type_is_mem_size(fn->arg1_type) ||
-	    arg_type_is_mem_ptr(fn->arg5_type)  ||
-	    check_args_pair_invalid(fn->arg1_type, fn->arg2_type) ||
-	    check_args_pair_invalid(fn->arg2_type, fn->arg3_type) ||
-	    check_args_pair_invalid(fn->arg3_type, fn->arg4_type) ||
-	    check_args_pair_invalid(fn->arg4_type, fn->arg5_type))
+	    (arg_type_is_mem_ptr(fn->arg5_type) && !fn->arg5_size) ||
+	    check_args_pair_invalid(fn, 1) ||
+	    check_args_pair_invalid(fn, 2) ||
+	    check_args_pair_invalid(fn, 3) ||
+	    check_args_pair_invalid(fn, 4))
 		return false;
 
 	return true;
Signature · 1 line
2.30.2
#4 lore raw

Re: [PATCH bpf-next v6 3/6] bpf: Allow helpers to accept pointers with a fixed size

Thank you for the patch! Perhaps something to improve:

Hi Maxim,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on bpf-next/master]

url:    https://github.com/intel-lab-lkp/linux/commits/Maxim-Mikityanskiy/bpf-Use-ipv6_only_sock-in-bpf_tcp_gen_syncookie/20220423-022511
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
config: arc-randconfig-r036-20220422 (https://download.01.org/0day-ci/archive/20220423/[email protected]/config)
compiler: arc-elf-gcc (GCC) 11.2.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/e374c2f19a674b586212d155ecb708aaa86dcd2c
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Maxim-Mikityanskiy/bpf-Use-ipv6_only_sock-in-bpf_tcp_gen_syncookie/20220423-022511 git checkout e374c2f19a674b586212d155ecb708aaa86dcd2c
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross W=1 O=build_dir ARCH=arc SHELL=/bin/bash kernel/bpf/

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

kernel/bpf/verifier.c: In function 'check_helper_call':

>> kernel/bpf/verifier.c:5954:49: warning: array subscript 5 is above array bounds of 'const enum bpf_arg_type[5]' [-Warray-bounds]
    5954 |         return arg_type_is_mem_size(fn->arg_type[arg + 1]) || fn->arg_size[arg];
         |                                     ~~~~~~~~~~~~^~~~~~~~~
   In file included from include/linux/bpf-cgroup.h:5,
                    from kernel/bpf/verifier.c:7:
   include/linux/bpf.h:456:35: note: while referencing 'arg_type'
     456 |                 enum bpf_arg_type arg_type[5];
         |                                   ^~~~~~~~
   kernel/bpf/verifier.c:5952:57: warning: array subscript 5 is above array bounds of 'const enum bpf_arg_type[5]' [-Warray-bounds]
    5952 |                 return arg_type_is_mem_size(fn->arg_type[arg + 1]) ==
         |                                             ~~~~~~~~~~~~^~~~~~~~~
   In file included from include/linux/bpf-cgroup.h:5,
                    from kernel/bpf/verifier.c:7:
   include/linux/bpf.h:456:35: note: while referencing 'arg_type'
     456 |                 enum bpf_arg_type arg_type[5];
         |                                   ^~~~~~~~

vim +5954 kernel/bpf/verifier.c

5948
5949 static bool check_args_pair_invalid(const struct bpf_func_proto *fn, int arg)
5950 {
5951 if (arg_type_is_mem_ptr(fn->arg_type[arg]))
5952 return arg_type_is_mem_size(fn->arg_type[arg + 1]) ==
5953 !!fn->arg_size[arg];

> 5954		return arg_type_is_mem_size(fn->arg_type[arg + 1]) || fn->arg_size[arg];

5955 }
5956

Signature · 2 lines
0-DAY CI Kernel Test Service
https://01.org/lkp
#5 lore raw

PATCH v6 4/6 bpf: Add helpers to issue and check SYN cookies in XDP

In reply to Maxim Mikityanskiy

The new helpers bpf_tcp_raw_{gen,check}_syncookie_ipv{4,6} allow an XDP program to generate SYN cookies in response to TCP SYN packets and to check those cookies upon receiving the first ACK packet (the final packet of the TCP handshake).

Unlike bpf_tcp_{gen,check}_syncookie these new helpers don't need a listening socket on the local machine, which allows to use them together with synproxy to accelerate SYN cookie generation.

---
 include/net/tcp.h              |   1 +
 include/uapi/linux/bpf.h       |  78 ++++++++++++++++++++++
 net/core/filter.c              | 118 +++++++++++++++++++++++++++++++++
 net/ipv4/tcp_input.c           |   3 +-
 scripts/bpf_doc.py             |   4 ++
 tools/include/uapi/linux/bpf.h |  78 ++++++++++++++++++++++
 6 files changed, 281 insertions(+), 1 deletion(-)
include/net/tcp.h +1 -0
index 6d50a662bf89..b9617974e4d0 100644
@@ -432,6 +432,7 @@ u16 tcp_v4_get_syncookie(struct sock *sk, struct iphdr *iph,
 			 struct tcphdr *th, u32 *cookie);
 u16 tcp_v6_get_syncookie(struct sock *sk, struct ipv6hdr *iph,
 			 struct tcphdr *th, u32 *cookie);
+u16 tcp_parse_mss_option(const struct tcphdr *th, u16 user_mss);
 u16 tcp_get_syncookie_mss(struct request_sock_ops *rsk_ops,
 			  const struct tcp_request_sock_ops *af_ops,
 			  struct sock *sk, struct tcphdr *th);
include/uapi/linux/bpf.h +78 -0

90 lines not on this page · raw message

net/core/filter.c +118 -0

130 lines not on this page · raw message

net/ipv4/tcp_input.c +2 -1
index 1595b76ea2be..0eb77cc4c05d 100644
@@ -3963,7 +3963,7 @@ static bool smc_parse_options(const struct tcphdr *th,
 /* Try to parse the MSS option from the TCP header. Return 0 on failure, clamped
  * value on success.
  */
-static u16 tcp_parse_mss_option(const struct tcphdr *th, u16 user_mss)
+u16 tcp_parse_mss_option(const struct tcphdr *th, u16 user_mss)
 {
 	const unsigned char *ptr = (const unsigned char *)(th + 1);
 	int length = (th->doff * 4) - sizeof(struct tcphdr);
@@ -4002,6 +4002,7 @@ static u16 tcp_parse_mss_option(const struct tcphdr *th, u16 user_mss)
 	}
 	return mss;
 }
+EXPORT_SYMBOL_GPL(tcp_parse_mss_option);
 
 /* Look for tcp options. Normally only called on SYN and SYNACK packets.
  * But, this can also be called on packets in the established flow when
scripts/bpf_doc.py +4 -0
index 096625242475..3d0b65e6dea7 100755
@@ -633,6 +633,8 @@ class PrinterHelpers(Printer):
             'struct socket',
             'struct file',
             'struct bpf_timer',
+            'struct iphdr',
+            'struct ipv6hdr',
     ]
     known_types = {
             '...',
@@ -682,6 +684,8 @@ class PrinterHelpers(Printer):
             'struct socket',
             'struct file',
             'struct bpf_timer',
+            'struct iphdr',
+            'struct ipv6hdr',
     }
     mapped_types = {
             'u8': '__u8',
tools/include/uapi/linux/bpf.h +78 -0

90 lines not on this page · raw message

Signature · 1 line
2.30.2
#6 lore raw

PATCH v6 5/6 bpf: Add selftests for raw syncookie helpers

In reply to Maxim Mikityanskiy

This commit adds selftests for the new BPF helpers:
bpf_tcp_raw_{gen,check}_syncookie_ipv{4,6}.

xdp_synproxy_kern.c is a BPF program that generates SYN cookies on allowed TCP ports and sends SYNACKs to clients, accelerating synproxy iptables module.

xdp_synproxy.c is a userspace control application that allows to configure the following options in runtime: list of allowed ports, MSS, window scale, TTL.

A selftest is added to prog_tests that leverages the above programs to test the functionality of the new helpers.

---
 tools/testing/selftests/bpf/.gitignore        |   1 +
 tools/testing/selftests/bpf/Makefile          |   2 +-
 .../selftests/bpf/prog_tests/xdp_synproxy.c   | 109 +++
 .../selftests/bpf/progs/xdp_synproxy_kern.c   | 750 ++++++++++++++++++
 tools/testing/selftests/bpf/xdp_synproxy.c    | 418 ++++++++++
 5 files changed, 1279 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/xdp_synproxy.c
 create mode 100644 tools/testing/selftests/bpf/progs/xdp_synproxy_kern.c
 create mode 100644 tools/testing/selftests/bpf/xdp_synproxy.c
tools/testing/selftests/bpf/.gitignore +1 -0
index 595565eb68c0..ca2f47f45670 100644
@@ -43,3 +43,4 @@ test_cpp
 *.tmp
 xdpxceiver
 xdp_redirect_multi
+xdp_synproxy
tools/testing/selftests/bpf/Makefile +1 -1
index bafdc5373a13..1b117673778e 100644
@@ -82,7 +82,7 @@ TEST_PROGS_EXTENDED := with_addr.sh \
 TEST_GEN_PROGS_EXTENDED = test_sock_addr test_skb_cgroup_id_user \
 	flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user \
 	test_lirc_mode2_user xdping test_cpp runqslower bench bpf_testmod.ko \
-	xdpxceiver xdp_redirect_multi
+	xdpxceiver xdp_redirect_multi xdp_synproxy
 
 TEST_CUSTOM_PROGS = $(OUTPUT)/urandom_read
 
tools/testing/selftests/bpf/prog_tests/xdp_synproxy.c new file +109 -0

109 lines not on this page · raw message

tools/testing/selftests/bpf/progs/xdp_synproxy_kern.c new file +750 -0

750 lines not on this page · raw message

tools/testing/selftests/bpf/xdp_synproxy.c new file +418 -0

418 lines not on this page · raw message

Signature · 1 line
2.30.2
#7 lore raw

Re: [PATCH bpf-next v6 5/6] bpf: Add selftests for raw syncookie helpers

Quoted 8 lines · Maxim Mikityanskiy
tools/testing/selftests/bpf/prog_tests/xdp_synproxy.c new file +8 -0
+void test_xdp_synproxy(void)
+{
+	int server_fd = -1, client_fd = -1, accept_fd = -1;
+	struct nstoken *ns = NULL;
+	FILE *ctrl_file = NULL;
+	char buf[1024];
+	size_t size;
+

tools/testing/selftests/bpf/prog_tests/xdp_synproxy.c line 41

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/xdp_synproxy.c new file +10 -0
+	SYS("ip netns add synproxy");
+
+	SYS("ip link add tmp0 type veth peer name tmp1");
+	SYS("ip link set tmp1 netns synproxy");
+	SYS("ip link set tmp0 up");
+	SYS("ip addr replace 198.18.0.1/24 dev tmp0");
+
+	// When checksum offload is enabled, the XDP program sees wrong
+	// checksums and drops packets.
+	SYS("ethtool -K tmp0 tx off");

BPF CI image doesn't have ethtool installed.
It will take some time to get it updated. Until then we cannot land the patch set. Can you think of a way to run this test without shelling to ethtool?

#8 lore raw

Re: [PATCH bpf-next v6 5/6] bpf: Add selftests for raw syncookie helpers

Quoted 16 lines · Alexei Starovoitov
Quoted 14 lines · Maxim Mikityanskiy
tools/testing/selftests/bpf/prog_tests/xdp_synproxy.c new file +14 -0
+void test_xdp_synproxy(void)
+{
+     int server_fd = -1, client_fd = -1, accept_fd = -1;
+     struct nstoken *ns = NULL;
+     FILE *ctrl_file = NULL;
+     char buf[1024];
+     size_t size;
+
+     SYS("ip netns add synproxy");
+
+     SYS("ip link add tmp0 type veth peer name tmp1");
+     SYS("ip link set tmp1 netns synproxy");
+     SYS("ip link set tmp0 up");
+     SYS("ip addr replace 198.18.0.1/24 dev tmp0");
Quoted 8 lines

tools/testing/selftests/bpf/prog_tests/xdp_synproxy.c line 41

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/xdp_synproxy.c new file +4 -0
+
+     // When checksum offload is enabled, the XDP program sees wrong
+     // checksums and drops packets.
+     SYS("ethtool -K tmp0 tx off");

BPF CI image doesn't have ethtool installed.
It will take some time to get it updated. Until then we cannot land the patch set. Can you think of a way to run this test without shelling to ethtool?

Good news: we got updated CI image with ethtool, so that shouldn't be a problem anymore.

Bad news: this selftest still fails, but in different place:

test_synproxy:FAIL:iptables -t raw -I PREROUTING -i tmp1 -p tcp -m tcp --syn --dport 8080 -j CT --notrack unexpected error: 512 (errno 2)

See [0].

[0] https://github.com/kernel-patches/bpf/runs/6169439612?check_suite_focus=true

#9 lore raw

Re: [PATCH bpf-next v6 5/6] bpf: Add selftests for raw syncookie helpers

Quoted 27 lines · Andrii Nakryiko
Quoted 16 lines · Alexei Starovoitov
Quoted 14 lines · Maxim Mikityanskiy
tools/testing/selftests/bpf/prog_tests/xdp_synproxy.c new file +14 -0
+void test_xdp_synproxy(void)
+{
+     int server_fd = -1, client_fd = -1, accept_fd = -1;
+     struct nstoken *ns = NULL;
+     FILE *ctrl_file = NULL;
+     char buf[1024];
+     size_t size;
+
+     SYS("ip netns add synproxy");
+
+     SYS("ip link add tmp0 type veth peer name tmp1");
+     SYS("ip link set tmp1 netns synproxy");
+     SYS("ip link set tmp0 up");
+     SYS("ip addr replace 198.18.0.1/24 dev tmp0");
Quoted 8 lines

tools/testing/selftests/bpf/prog_tests/xdp_synproxy.c line 41

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/xdp_synproxy.c new file +4 -0
+
+     // When checksum offload is enabled, the XDP program sees wrong
+     // checksums and drops packets.
+     SYS("ethtool -K tmp0 tx off");

BPF CI image doesn't have ethtool installed.
It will take some time to get it updated. Until then we cannot land the patch set. Can you think of a way to run this test without shelling to ethtool?

Quoted 7 lines

Good news: we got updated CI image with ethtool, so that shouldn't be a problem anymore.

Bad news: this selftest still fails, but in different place:

test_synproxy:FAIL:iptables -t raw -I PREROUTING -i tmp1 -p tcp -m tcp --syn --dport 8080 -j CT --notrack unexpected error: 512 (errno 2)

That's simply a matter of missing kernel config options:

CONFIG_NETFILTER_SYNPROXY=y
CONFIG_NETFILTER_XT_TARGET_CT=y
CONFIG_NETFILTER_XT_MATCH_STATE=y
CONFIG_IP_NF_FILTER=y
CONFIG_IP_NF_TARGET_SYNPROXY=y
CONFIG_IP_NF_RAW=y

Shall I create a pull request on github to add these options to https://github.com/libbpf/libbpf/tree/master/travis-ci/vmtest/configs?

Quoted 3 lines

See [0].

   [0] https://github.com/kernel-patches/bpf/runs/6169439612?check_suite_focus=true
#10 lore raw

Re: [PATCH bpf-next v6 5/6] bpf: Add selftests for raw syncookie helpers

Quoted 46 lines · Maxim Mikityanskiy
Quoted 27 lines · Andrii Nakryiko
Quoted 16 lines · Alexei Starovoitov
Quoted 14 lines · Maxim Mikityanskiy
tools/testing/selftests/bpf/prog_tests/xdp_synproxy.c new file +14 -0
+void test_xdp_synproxy(void)
+{
+     int server_fd = -1, client_fd = -1, accept_fd = -1;
+     struct nstoken *ns = NULL;
+     FILE *ctrl_file = NULL;
+     char buf[1024];
+     size_t size;
+
+     SYS("ip netns add synproxy");
+
+     SYS("ip link add tmp0 type veth peer name tmp1");
+     SYS("ip link set tmp1 netns synproxy");
+     SYS("ip link set tmp0 up");
+     SYS("ip addr replace 198.18.0.1/24 dev tmp0");
Quoted 8 lines

tools/testing/selftests/bpf/prog_tests/xdp_synproxy.c line 41

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/xdp_synproxy.c new file +4 -0
+
+     // When checksum offload is enabled, the XDP program sees wrong
+     // checksums and drops packets.
+     SYS("ethtool -K tmp0 tx off");

BPF CI image doesn't have ethtool installed.
It will take some time to get it updated. Until then we cannot land the patch set. Can you think of a way to run this test without shelling to ethtool?

Quoted 7 lines

Good news: we got updated CI image with ethtool, so that shouldn't be a problem anymore.

Bad news: this selftest still fails, but in different place:

test_synproxy:FAIL:iptables -t raw -I PREROUTING -i tmp1 -p tcp -m tcp --syn --dport 8080 -j CT --notrack unexpected error: 512 (errno 2)

That's simply a matter of missing kernel config options:

CONFIG_NETFILTER_SYNPROXY=y
CONFIG_NETFILTER_XT_TARGET_CT=y
CONFIG_NETFILTER_XT_MATCH_STATE=y
CONFIG_IP_NF_FILTER=y
CONFIG_IP_NF_TARGET_SYNPROXY=y
CONFIG_IP_NF_RAW=y

Quoted 3 lines

Shall I create a pull request on github to add these options to
https://github.com/libbpf/libbpf/tree/master/travis-ci/vmtest/configs?

Yes, please. But also for [0], that's the one that tests all the not-yet-applied patches

[0] https://github.com/kernel-patches/vmtest/

Quoted 4 lines
Quoted 3 lines

See [0].

   [0] https://github.com/kernel-patches/bpf/runs/6169439612?check_suite_focus=true
#11 lore raw

Re: [PATCH bpf-next v6 5/6] bpf: Add selftests for raw syncookie helpers

Quoted 47 lines · Andrii Nakryiko
Quoted 46 lines · Maxim Mikityanskiy
Quoted 27 lines · Andrii Nakryiko
Quoted 16 lines · Alexei Starovoitov
Quoted 14 lines · Maxim Mikityanskiy
tools/testing/selftests/bpf/prog_tests/xdp_synproxy.c new file +14 -0
+void test_xdp_synproxy(void)
+{
+     int server_fd = -1, client_fd = -1, accept_fd = -1;
+     struct nstoken *ns = NULL;
+     FILE *ctrl_file = NULL;
+     char buf[1024];
+     size_t size;
+
+     SYS("ip netns add synproxy");
+
+     SYS("ip link add tmp0 type veth peer name tmp1");
+     SYS("ip link set tmp1 netns synproxy");
+     SYS("ip link set tmp0 up");
+     SYS("ip addr replace 198.18.0.1/24 dev tmp0");
Quoted 8 lines

tools/testing/selftests/bpf/prog_tests/xdp_synproxy.c line 41

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/xdp_synproxy.c new file +4 -0
+
+     // When checksum offload is enabled, the XDP program sees wrong
+     // checksums and drops packets.
+     SYS("ethtool -K tmp0 tx off");

BPF CI image doesn't have ethtool installed.
It will take some time to get it updated. Until then we cannot land the patch set. Can you think of a way to run this test without shelling to ethtool?

Quoted 7 lines

Good news: we got updated CI image with ethtool, so that shouldn't be a problem anymore.

Bad news: this selftest still fails, but in different place:

test_synproxy:FAIL:iptables -t raw -I PREROUTING -i tmp1 -p tcp -m tcp --syn --dport 8080 -j CT --notrack unexpected error: 512 (errno 2)

That's simply a matter of missing kernel config options:

CONFIG_NETFILTER_SYNPROXY=y
CONFIG_NETFILTER_XT_TARGET_CT=y
CONFIG_NETFILTER_XT_MATCH_STATE=y
CONFIG_IP_NF_FILTER=y
CONFIG_IP_NF_TARGET_SYNPROXY=y
CONFIG_IP_NF_RAW=y

Quoted 8 lines
Quoted 3 lines

Shall I create a pull request on github to add these options to
https://github.com/libbpf/libbpf/tree/master/travis-ci/vmtest/configs?

Yes, please. But also for [0], that's the one that tests all the not-yet-applied patches

[0] https://github.com/kernel-patches/vmtest/

Created pull requests:

https://github.com/kernel-patches/vmtest/pull/79
https://github.com/libbpf/libbpf/pull/490

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

See [0].

    [0] https://github.com/kernel-patches/bpf/runs/6169439612?check_suite_focus=true
#12 lore raw

Re: [PATCH bpf-next v6 5/6] bpf: Add selftests for raw syncookie helpers

Quoted 56 lines · Maxim Mikityanskiy
Quoted 54 lines · Andrii Nakryiko
Quoted 46 lines · Maxim Mikityanskiy
Quoted 27 lines · Andrii Nakryiko
Quoted 16 lines · Alexei Starovoitov
Quoted 14 lines · Maxim Mikityanskiy
tools/testing/selftests/bpf/prog_tests/xdp_synproxy.c new file +14 -0
+void test_xdp_synproxy(void)
+{
+     int server_fd = -1, client_fd = -1, accept_fd = -1;
+     struct nstoken *ns = NULL;
+     FILE *ctrl_file = NULL;
+     char buf[1024];
+     size_t size;
+
+     SYS("ip netns add synproxy");
+
+     SYS("ip link add tmp0 type veth peer name tmp1");
+     SYS("ip link set tmp1 netns synproxy");
+     SYS("ip link set tmp0 up");
+     SYS("ip addr replace 198.18.0.1/24 dev tmp0");
Quoted 8 lines

tools/testing/selftests/bpf/prog_tests/xdp_synproxy.c line 41

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/xdp_synproxy.c new file +4 -0
+
+     // When checksum offload is enabled, the XDP program sees wrong
+     // checksums and drops packets.
+     SYS("ethtool -K tmp0 tx off");

BPF CI image doesn't have ethtool installed.
It will take some time to get it updated. Until then we cannot land the patch set. Can you think of a way to run this test without shelling to ethtool?

Quoted 7 lines

Good news: we got updated CI image with ethtool, so that shouldn't be a problem anymore.

Bad news: this selftest still fails, but in different place:

test_synproxy:FAIL:iptables -t raw -I PREROUTING -i tmp1 -p tcp -m tcp --syn --dport 8080 -j CT --notrack unexpected error: 512 (errno 2)

That's simply a matter of missing kernel config options:

CONFIG_NETFILTER_SYNPROXY=y
CONFIG_NETFILTER_XT_TARGET_CT=y
CONFIG_NETFILTER_XT_MATCH_STATE=y
CONFIG_IP_NF_FILTER=y
CONFIG_IP_NF_TARGET_SYNPROXY=y
CONFIG_IP_NF_RAW=y

Quoted 3 lines

Shall I create a pull request on github to add these options to
https://github.com/libbpf/libbpf/tree/master/travis-ci/vmtest/configs?

Yes, please. But also for [0], that's the one that tests all the not-yet-applied patches

Quoted 7 lines
Quoted 1 line

[0] https://github.com/kernel-patches/vmtest/

Created pull requests:

https://github.com/kernel-patches/vmtest/pull/79
https://github.com/libbpf/libbpf/pull/490

Merged both, thanks.

Quoted 5 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

See [0].

    [0] https://github.com/kernel-patches/bpf/runs/6169439612?check_suite_focus=true
#13 lore raw

PATCH v6 6/6 bpf: Allow the new syncookie helpers to work with SKBs

In reply to Maxim Mikityanskiy

This commits allows the new BPF helpers to work in SKB context (in TC BPF programs): bpf_tcp_raw_{gen,check}_syncookie_ipv{4,6}.

The sample application and selftest are updated to support the TC mode. It's not the recommended mode of operation, because the SKB is already created at this point, and it's unlikely that the BPF program will provide any substantional speedup compared to regular SYN cookies or synproxy.

---
 net/core/filter.c                             |  10 ++
 .../selftests/bpf/prog_tests/xdp_synproxy.c   |  53 +++++--
 .../selftests/bpf/progs/xdp_synproxy_kern.c   | 141 +++++++++++++-----
 tools/testing/selftests/bpf/xdp_synproxy.c    |  94 +++++++++---
 4 files changed, 230 insertions(+), 68 deletions(-)
net/core/filter.c +10 -0
index ffb7e93f60c5..4f631cac3130 100644
@@ -7905,6 +7905,16 @@ tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 		return &bpf_sk_assign_proto;
 	case BPF_FUNC_skb_set_tstamp:
 		return &bpf_skb_set_tstamp_proto;
+#ifdef CONFIG_SYN_COOKIES
+	case BPF_FUNC_tcp_raw_gen_syncookie_ipv4:
+		return &bpf_tcp_raw_gen_syncookie_ipv4_proto;
+	case BPF_FUNC_tcp_raw_gen_syncookie_ipv6:
+		return &bpf_tcp_raw_gen_syncookie_ipv6_proto;
+	case BPF_FUNC_tcp_raw_check_syncookie_ipv4:
+		return &bpf_tcp_raw_check_syncookie_ipv4_proto;
+	case BPF_FUNC_tcp_raw_check_syncookie_ipv6:
+		return &bpf_tcp_raw_check_syncookie_ipv6_proto;
+#endif
 #endif
 	default:
 		return bpf_sk_base_func_proto(func_id);
tools/testing/selftests/bpf/prog_tests/xdp_synproxy.c +44 -9

98 lines not on this page · raw message

tools/testing/selftests/bpf/progs/xdp_synproxy_kern.c +105 -36

215 lines not on this page · raw message

tools/testing/selftests/bpf/xdp_synproxy.c +71 -23

174 lines not on this page · raw message

Signature · 1 line
2.30.2
#14 lore raw

Re: [PATCH bpf-next v6 6/6] bpf: Allow the new syncookie helpers to work with SKBs

Quoted 19 lines · Maxim Mikityanskiy

This commits allows the new BPF helpers to work in SKB context (in TC BPF programs): bpf_tcp_raw_{gen,check}_syncookie_ipv{4,6}.

The sample application and selftest are updated to support the TC mode. It's not the recommended mode of operation, because the SKB is already created at this point, and it's unlikely that the BPF program will provide any substantional speedup compared to regular SYN cookies or synproxy.

---
 net/core/filter.c                             |  10 ++
 .../selftests/bpf/prog_tests/xdp_synproxy.c   |  53 +++++--
 .../selftests/bpf/progs/xdp_synproxy_kern.c   | 141 +++++++++++++-----
 tools/testing/selftests/bpf/xdp_synproxy.c    |  94 +++++++++---
 4 files changed, 230 insertions(+), 68 deletions(-)

[...]

tools/testing/selftests/bpf/progs/xdp_synproxy_kern.c static __always_inline int syncookie_handle_ack(struct header_pointers *hdr) · line 775

Quoted 8 lines
tools/testing/selftests/bpf/progs/xdp_synproxy_kern.c +5 -2
-       return hdr.tcp->syn ? syncookie_handle_syn(&hdr, ctx, data, data_end) :
-                             syncookie_handle_ack(&hdr);
+       return hdr->tcp->syn ? syncookie_handle_syn(hdr, ctx, data, data_end, xdp) :
+                              syncookie_handle_ack(hdr);
+}
+
+SEC("xdp/syncookie")

SEC("xdp")? libbpf will reject SEC("xdp/syncookie") in strict libbpf 1.0 mode

Quoted 16 lines
tools/testing/selftests/bpf/progs/xdp_synproxy_kern.c +16 -0
+int syncookie_xdp(struct xdp_md *ctx)
+{
+       void *data_end = (void *)(long)ctx->data_end;
+       void *data = (void *)(long)ctx->data;
+       struct header_pointers hdr;
+       int ret;
+
+       ret = syncookie_part1(ctx, data, data_end, &hdr, true);
+       if (ret != XDP_TX)
+               return ret;
+
+       data_end = (void *)(long)ctx->data_end;
+       data = (void *)(long)ctx->data;
+
+       return syncookie_part2(ctx, data, data_end, &hdr, true);
+}

[...]