Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpf: selftest: Ensure the child sk inherited all bpf_sock_ops_cb_flags

This patch adds a test to ensure the child sk inherited everything
from the bpf_sock_ops_cb_flags of the listen sk:
1. Sets one more cb_flags (BPF_SOCK_OPS_STATE_CB_FLAG) to the listen sk
in test_tcp_hdr_options.c
2. Saves the skops->bpf_sock_ops_cb_flags when handling the newly
established passive connection
3. CHECK() it is the same as the listen sk

This also covers the fastopen case as the existing test_tcp_hdr_options.c
does.

Signed-off-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20201002013454.2542367-1-kafai@fb.com

authored by

Martin KaFai Lau and committed by
Alexei Starovoitov
96d46c50 82f45c6c

+22 -6
+12
tools/testing/selftests/bpf/prog_tests/tcp_hdr_options.c
··· 264 264 265 265 static void check_hdr_and_close_fds(struct sk_fds *sk_fds) 266 266 { 267 + const __u32 expected_inherit_cb_flags = 268 + BPF_SOCK_OPS_PARSE_UNKNOWN_HDR_OPT_CB_FLAG | 269 + BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG | 270 + BPF_SOCK_OPS_STATE_CB_FLAG; 271 + 267 272 if (sk_fds_shutdown(sk_fds)) 273 + goto check_linum; 274 + 275 + if (CHECK(expected_inherit_cb_flags != skel->bss->inherit_cb_flags, 276 + "Unexpected inherit_cb_flags", "0x%x != 0x%x\n", 277 + skel->bss->inherit_cb_flags, expected_inherit_cb_flags)) 268 278 goto check_linum; 269 279 270 280 if (check_hdr_stg(&exp_passive_hdr_stg, sk_fds->passive_fd, ··· 330 320 331 321 memset(&skel->bss->active_estab_in, 0, optsize); 332 322 memset(&skel->bss->active_fin_in, 0, optsize); 323 + 324 + skel->bss->inherit_cb_flags = 0; 333 325 334 326 skel->data->test_kind = TCPOPT_EXP; 335 327 skel->data->test_magic = 0xeB9F;
+2 -2
tools/testing/selftests/bpf/progs/test_misc_tcp_hdr_options.c
··· 304 304 passive_lport_n = __bpf_htons(passive_lport_h); 305 305 bpf_setsockopt(skops, SOL_TCP, TCP_SAVE_SYN, 306 306 &true_val, sizeof(true_val)); 307 - set_hdr_cb_flags(skops); 307 + set_hdr_cb_flags(skops, 0); 308 308 break; 309 309 case BPF_SOCK_OPS_TCP_CONNECT_CB: 310 - set_hdr_cb_flags(skops); 310 + set_hdr_cb_flags(skops, 0); 311 311 break; 312 312 case BPF_SOCK_OPS_PARSE_HDR_OPT_CB: 313 313 return handle_parse_hdr(skops);
+5 -2
tools/testing/selftests/bpf/progs/test_tcp_hdr_options.c
··· 21 21 22 22 __u8 test_kind = TCPOPT_EXP; 23 23 __u16 test_magic = 0xeB9F; 24 + __u32 inherit_cb_flags = 0; 24 25 25 26 struct bpf_test_option passive_synack_out = {}; 26 27 struct bpf_test_option passive_fin_out = {}; ··· 468 467 struct tcphdr *th; 469 468 int err; 470 469 470 + inherit_cb_flags = skops->bpf_sock_ops_cb_flags; 471 + 471 472 err = load_option(skops, &passive_estab_in, true); 472 473 if (err == -ENOENT) { 473 474 /* saved_syn is not found. It was in syncookie mode. ··· 603 600 case BPF_SOCK_OPS_TCP_LISTEN_CB: 604 601 bpf_setsockopt(skops, SOL_TCP, TCP_SAVE_SYN, 605 602 &true_val, sizeof(true_val)); 606 - set_hdr_cb_flags(skops); 603 + set_hdr_cb_flags(skops, BPF_SOCK_OPS_STATE_CB_FLAG); 607 604 break; 608 605 case BPF_SOCK_OPS_TCP_CONNECT_CB: 609 - set_hdr_cb_flags(skops); 606 + set_hdr_cb_flags(skops, 0); 610 607 break; 611 608 case BPF_SOCK_OPS_PARSE_HDR_OPT_CB: 612 609 return handle_parse_hdr(skops);
+3 -2
tools/testing/selftests/bpf/test_tcp_hdr_options.h
··· 110 110 BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG)); 111 111 } 112 112 113 - static inline void set_hdr_cb_flags(struct bpf_sock_ops *skops) 113 + static inline void set_hdr_cb_flags(struct bpf_sock_ops *skops, __u32 extra) 114 114 { 115 115 bpf_sock_ops_cb_flags_set(skops, 116 116 skops->bpf_sock_ops_cb_flags | 117 117 BPF_SOCK_OPS_PARSE_UNKNOWN_HDR_OPT_CB_FLAG | 118 - BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG); 118 + BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG | 119 + extra); 119 120 } 120 121 static inline void 121 122 clear_parse_all_hdr_cb_flags(struct bpf_sock_ops *skops)