Commit Graph

3 Commits

Author SHA1 Message Date
Artem Savkov ab9c109bf0 selftests/bpf: tests for using dynptrs to parse skb and xdp buffers
Bugzilla: https://bugzilla.redhat.com/2221599

commit cfa7b011894d689cccfa88a25da324fa5c34e4ed
Author: Joanne Koong <joannelkoong@gmail.com>
Date:   Wed Mar 1 07:49:53 2023 -0800

    selftests/bpf: tests for using dynptrs to parse skb and xdp buffers
    
    Test skb and xdp dynptr functionality in the following ways:
    
    1) progs/test_cls_redirect_dynptr.c
       * Rewrite "progs/test_cls_redirect.c" test to use dynptrs to parse
         skb data
    
       * This is a great example of how dynptrs can be used to simplify a
         lot of the parsing logic for non-statically known values.
    
         When measuring the user + system time between the original version
         vs. using dynptrs, and averaging the time for 10 runs (using
         "time ./test_progs -t cls_redirect"):
             original version: 0.092 sec
             with dynptrs: 0.078 sec
    
    2) progs/test_xdp_dynptr.c
       * Rewrite "progs/test_xdp.c" test to use dynptrs to parse xdp data
    
         When measuring the user + system time between the original version
         vs. using dynptrs, and averaging the time for 10 runs (using
         "time ./test_progs -t xdp_attach"):
             original version: 0.118 sec
             with dynptrs: 0.094 sec
    
    3) progs/test_l4lb_noinline_dynptr.c
       * Rewrite "progs/test_l4lb_noinline.c" test to use dynptrs to parse
         skb data
    
         When measuring the user + system time between the original version
         vs. using dynptrs, and averaging the time for 10 runs (using
         "time ./test_progs -t l4lb_all"):
             original version: 0.062 sec
             with dynptrs: 0.081 sec
    
         For number of processed verifier instructions:
             original version: 6268 insns
             with dynptrs: 2588 insns
    
    4) progs/test_parse_tcp_hdr_opt_dynptr.c
       * Add sample code for parsing tcp hdr opt lookup using dynptrs.
         This logic is lifted from a real-world use case of packet parsing
         in katran [0], a layer 4 load balancer. The original version
         "progs/test_parse_tcp_hdr_opt.c" (not using dynptrs) is included
         here as well, for comparison.
    
         When measuring the user + system time between the original version
         vs. using dynptrs, and averaging the time for 10 runs (using
         "time ./test_progs -t parse_tcp_hdr_opt"):
             original version: 0.031 sec
             with dynptrs: 0.045 sec
    
    5) progs/dynptr_success.c
       * Add test case "test_skb_readonly" for testing attempts at writes
         on a prog type with read-only skb ctx.
       * Add "test_dynptr_skb_data" for testing that bpf_dynptr_data isn't
         supported for skb progs.
    
    6) progs/dynptr_fail.c
       * Add test cases "skb_invalid_data_slice{1,2,3,4}" and
         "xdp_invalid_data_slice{1,2}" for testing that helpers that modify the
         underlying packet buffer automatically invalidate the associated
         data slice.
       * Add test cases "skb_invalid_ctx" and "xdp_invalid_ctx" for testing
         that prog types that do not support bpf_dynptr_from_skb/xdp don't
         have access to the API.
       * Add test case "dynptr_slice_var_len{1,2}" for testing that
         variable-sized len can't be passed in to bpf_dynptr_slice
       * Add test case "skb_invalid_slice_write" for testing that writes to a
         read-only data slice are rejected by the verifier.
       * Add test case "data_slice_out_of_bounds_skb" for testing that
         writes to an area outside the slice are rejected.
       * Add test case "invalid_slice_rdwr_rdonly" for testing that prog
         types that don't allow writes to packet data don't accept any calls
         to bpf_dynptr_slice_rdwr.
    
    [0] https://github.com/facebookincubator/katran/blob/main/katran/lib/bpf/pckt_parsing.h
    
    Signed-off-by: Joanne Koong <joannelkoong@gmail.com>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/r/20230301154953.641654-11-joannelkoong@gmail.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-09-22 09:12:08 +02:00
Martin KaFai Lau 96d46c5085 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
2020-10-02 11:34:48 -07:00
Martin KaFai Lau ad2f8eb009 bpf: selftests: Tcp header options
This patch adds tests for the new bpf tcp header option feature.

test_tcp_hdr_options.c:
- It tests header option writing and parsing in 3WHS: regular
  connection establishment, fastopen, and syncookie.
- In syncookie, the passive side's bpf prog is asking the active side
  to resend its bpf header option by specifying a RESEND bit in the
  outgoing SYNACK. handle_active_estab() and write_nodata_opt() has
  some details.
- handle_passive_estab() has comments on fastopen.
- It also has test for header writing and parsing in FIN packet.
- Most of the tests is writing an experimental option 254 with magic 0xeB9F.
- The no_exprm_estab() also tests writing a regular TCP option
  without any magic.

test_misc_tcp_options.c:
- It is an one directional test.  Active side writes option and
  passive side parses option.  The focus is to exercise
  the new helpers and API.
- Testing the new helper: bpf_load_hdr_opt() and bpf_store_hdr_opt().
- Testing the bpf_getsockopt(TCP_BPF_SYN).
- Negative tests for the above helpers.
- Testing the sock_ops->skb_data.

Signed-off-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200820190117.2886749-1-kafai@fb.com
2020-08-24 14:35:00 -07:00