selftests: bpf: add selftest for __sk_buff context in BPF_PROG_TEST_RUN
Simple test that sets cb to {1,2,3,4,5} and priority to 6, runs bpf
program that fails if cb is not what we expect and increments cb[i] and
priority. When the test finishes, we check that cb is now {2,3,4,5,6}
and priority is 7.
We also test the sanity checks:
* ctx_in is provided, but ctx_size_in is zero (same for
ctx_out/ctx_size_out)
* unexpected non-zero fields in __sk_buff return EINVAL
Signed-off-by: Stanislav Fomichev <sdf@google.com>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-04-09 11:49:11 -07:00
// SPDX-License-Identifier: GPL-2.0
# include <test_progs.h>
2020-05-08 10:46:09 -07:00
# include <network_helpers.h>
selftests: bpf: add selftest for __sk_buff context in BPF_PROG_TEST_RUN
Simple test that sets cb to {1,2,3,4,5} and priority to 6, runs bpf
program that fails if cb is not what we expect and increments cb[i] and
priority. When the test finishes, we check that cb is now {2,3,4,5,6}
and priority is 7.
We also test the sanity checks:
* ctx_in is provided, but ctx_size_in is zero (same for
ctx_out/ctx_size_out)
* unexpected non-zero fields in __sk_buff return EINVAL
Signed-off-by: Stanislav Fomichev <sdf@google.com>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-04-09 11:49:11 -07:00
void test_skb_ctx ( void )
{
struct __sk_buff skb = {
. cb [ 0 ] = 1 ,
. cb [ 1 ] = 2 ,
. cb [ 2 ] = 3 ,
. cb [ 3 ] = 4 ,
. cb [ 4 ] = 5 ,
. priority = 6 ,
2021-08-30 20:33:56 -07:00
. ingress_ifindex = 11 ,
2020-08-03 12:05:45 +03:00
. ifindex = 1 ,
2019-10-15 11:31:25 -07:00
. tstamp = 7 ,
2019-12-13 14:30:28 -08:00
. wire_len = 100 ,
. gso_segs = 8 ,
2019-12-18 12:57:47 -08:00
. mark = 9 ,
2020-03-03 15:05:03 -05:00
. gso_size = 10 ,
2021-09-10 01:04:09 +03:00
. hwtstamp = 11 ,
selftests: bpf: add selftest for __sk_buff context in BPF_PROG_TEST_RUN
Simple test that sets cb to {1,2,3,4,5} and priority to 6, runs bpf
program that fails if cb is not what we expect and increments cb[i] and
priority. When the test finishes, we check that cb is now {2,3,4,5,6}
and priority is 7.
We also test the sanity checks:
* ctx_in is provided, but ctx_size_in is zero (same for
ctx_out/ctx_size_out)
* unexpected non-zero fields in __sk_buff return EINVAL
Signed-off-by: Stanislav Fomichev <sdf@google.com>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-04-09 11:49:11 -07:00
} ;
2022-02-02 15:54:21 -08:00
LIBBPF_OPTS ( bpf_test_run_opts , tattr ,
selftests: bpf: add selftest for __sk_buff context in BPF_PROG_TEST_RUN
Simple test that sets cb to {1,2,3,4,5} and priority to 6, runs bpf
program that fails if cb is not what we expect and increments cb[i] and
priority. When the test finishes, we check that cb is now {2,3,4,5,6}
and priority is 7.
We also test the sanity checks:
* ctx_in is provided, but ctx_size_in is zero (same for
ctx_out/ctx_size_out)
* unexpected non-zero fields in __sk_buff return EINVAL
Signed-off-by: Stanislav Fomichev <sdf@google.com>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-04-09 11:49:11 -07:00
. data_in = & pkt_v4 ,
. data_size_in = sizeof ( pkt_v4 ) ,
. ctx_in = & skb ,
. ctx_size_in = sizeof ( skb ) ,
. ctx_out = & skb ,
. ctx_size_out = sizeof ( skb ) ,
2022-02-02 15:54:21 -08:00
) ;
selftests: bpf: add selftest for __sk_buff context in BPF_PROG_TEST_RUN
Simple test that sets cb to {1,2,3,4,5} and priority to 6, runs bpf
program that fails if cb is not what we expect and increments cb[i] and
priority. When the test finishes, we check that cb is now {2,3,4,5,6}
and priority is 7.
We also test the sanity checks:
* ctx_in is provided, but ctx_size_in is zero (same for
ctx_out/ctx_size_out)
* unexpected non-zero fields in __sk_buff return EINVAL
Signed-off-by: Stanislav Fomichev <sdf@google.com>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-04-09 11:49:11 -07:00
struct bpf_object * obj ;
2022-02-02 15:54:21 -08:00
int err , prog_fd , i ;
selftests: bpf: add selftest for __sk_buff context in BPF_PROG_TEST_RUN
Simple test that sets cb to {1,2,3,4,5} and priority to 6, runs bpf
program that fails if cb is not what we expect and increments cb[i] and
priority. When the test finishes, we check that cb is now {2,3,4,5,6}
and priority is 7.
We also test the sanity checks:
* ctx_in is provided, but ctx_size_in is zero (same for
ctx_out/ctx_size_out)
* unexpected non-zero fields in __sk_buff return EINVAL
Signed-off-by: Stanislav Fomichev <sdf@google.com>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-04-09 11:49:11 -07:00
2022-09-01 22:22:53 +00:00
err = bpf_prog_test_load ( " ./test_skb_ctx.bpf.o " , BPF_PROG_TYPE_SCHED_CLS ,
2022-02-02 15:54:21 -08:00
& obj , & prog_fd ) ;
if ( ! ASSERT_OK ( err , " load " ) )
selftests: bpf: add selftest for __sk_buff context in BPF_PROG_TEST_RUN
Simple test that sets cb to {1,2,3,4,5} and priority to 6, runs bpf
program that fails if cb is not what we expect and increments cb[i] and
priority. When the test finishes, we check that cb is now {2,3,4,5,6}
and priority is 7.
We also test the sanity checks:
* ctx_in is provided, but ctx_size_in is zero (same for
ctx_out/ctx_size_out)
* unexpected non-zero fields in __sk_buff return EINVAL
Signed-off-by: Stanislav Fomichev <sdf@google.com>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-04-09 11:49:11 -07:00
return ;
/* ctx_in != NULL, ctx_size_in == 0 */
tattr . ctx_size_in = 0 ;
2022-02-02 15:54:21 -08:00
err = bpf_prog_test_run_opts ( prog_fd , & tattr ) ;
ASSERT_NEQ ( err , 0 , " ctx_size_in " ) ;
selftests: bpf: add selftest for __sk_buff context in BPF_PROG_TEST_RUN
Simple test that sets cb to {1,2,3,4,5} and priority to 6, runs bpf
program that fails if cb is not what we expect and increments cb[i] and
priority. When the test finishes, we check that cb is now {2,3,4,5,6}
and priority is 7.
We also test the sanity checks:
* ctx_in is provided, but ctx_size_in is zero (same for
ctx_out/ctx_size_out)
* unexpected non-zero fields in __sk_buff return EINVAL
Signed-off-by: Stanislav Fomichev <sdf@google.com>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-04-09 11:49:11 -07:00
tattr . ctx_size_in = sizeof ( skb ) ;
/* ctx_out != NULL, ctx_size_out == 0 */
tattr . ctx_size_out = 0 ;
2022-02-02 15:54:21 -08:00
err = bpf_prog_test_run_opts ( prog_fd , & tattr ) ;
ASSERT_NEQ ( err , 0 , " ctx_size_out " ) ;
selftests: bpf: add selftest for __sk_buff context in BPF_PROG_TEST_RUN
Simple test that sets cb to {1,2,3,4,5} and priority to 6, runs bpf
program that fails if cb is not what we expect and increments cb[i] and
priority. When the test finishes, we check that cb is now {2,3,4,5,6}
and priority is 7.
We also test the sanity checks:
* ctx_in is provided, but ctx_size_in is zero (same for
ctx_out/ctx_size_out)
* unexpected non-zero fields in __sk_buff return EINVAL
Signed-off-by: Stanislav Fomichev <sdf@google.com>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-04-09 11:49:11 -07:00
tattr . ctx_size_out = sizeof ( skb ) ;
/* non-zero [len, tc_index] fields should be rejected*/
skb . len = 1 ;
2022-02-02 15:54:21 -08:00
err = bpf_prog_test_run_opts ( prog_fd , & tattr ) ;
ASSERT_NEQ ( err , 0 , " len " ) ;
selftests: bpf: add selftest for __sk_buff context in BPF_PROG_TEST_RUN
Simple test that sets cb to {1,2,3,4,5} and priority to 6, runs bpf
program that fails if cb is not what we expect and increments cb[i] and
priority. When the test finishes, we check that cb is now {2,3,4,5,6}
and priority is 7.
We also test the sanity checks:
* ctx_in is provided, but ctx_size_in is zero (same for
ctx_out/ctx_size_out)
* unexpected non-zero fields in __sk_buff return EINVAL
Signed-off-by: Stanislav Fomichev <sdf@google.com>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-04-09 11:49:11 -07:00
skb . len = 0 ;
skb . tc_index = 1 ;
2022-02-02 15:54:21 -08:00
err = bpf_prog_test_run_opts ( prog_fd , & tattr ) ;
ASSERT_NEQ ( err , 0 , " tc_index " ) ;
selftests: bpf: add selftest for __sk_buff context in BPF_PROG_TEST_RUN
Simple test that sets cb to {1,2,3,4,5} and priority to 6, runs bpf
program that fails if cb is not what we expect and increments cb[i] and
priority. When the test finishes, we check that cb is now {2,3,4,5,6}
and priority is 7.
We also test the sanity checks:
* ctx_in is provided, but ctx_size_in is zero (same for
ctx_out/ctx_size_out)
* unexpected non-zero fields in __sk_buff return EINVAL
Signed-off-by: Stanislav Fomichev <sdf@google.com>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-04-09 11:49:11 -07:00
skb . tc_index = 0 ;
/* non-zero [hash, sk] fields should be rejected */
skb . hash = 1 ;
2022-02-02 15:54:21 -08:00
err = bpf_prog_test_run_opts ( prog_fd , & tattr ) ;
ASSERT_NEQ ( err , 0 , " hash " ) ;
selftests: bpf: add selftest for __sk_buff context in BPF_PROG_TEST_RUN
Simple test that sets cb to {1,2,3,4,5} and priority to 6, runs bpf
program that fails if cb is not what we expect and increments cb[i] and
priority. When the test finishes, we check that cb is now {2,3,4,5,6}
and priority is 7.
We also test the sanity checks:
* ctx_in is provided, but ctx_size_in is zero (same for
ctx_out/ctx_size_out)
* unexpected non-zero fields in __sk_buff return EINVAL
Signed-off-by: Stanislav Fomichev <sdf@google.com>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-04-09 11:49:11 -07:00
skb . hash = 0 ;
skb . sk = ( struct bpf_sock * ) 1 ;
2022-02-02 15:54:21 -08:00
err = bpf_prog_test_run_opts ( prog_fd , & tattr ) ;
ASSERT_NEQ ( err , 0 , " sk " ) ;
selftests: bpf: add selftest for __sk_buff context in BPF_PROG_TEST_RUN
Simple test that sets cb to {1,2,3,4,5} and priority to 6, runs bpf
program that fails if cb is not what we expect and increments cb[i] and
priority. When the test finishes, we check that cb is now {2,3,4,5,6}
and priority is 7.
We also test the sanity checks:
* ctx_in is provided, but ctx_size_in is zero (same for
ctx_out/ctx_size_out)
* unexpected non-zero fields in __sk_buff return EINVAL
Signed-off-by: Stanislav Fomichev <sdf@google.com>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-04-09 11:49:11 -07:00
skb . sk = 0 ;
2022-02-02 15:54:21 -08:00
err = bpf_prog_test_run_opts ( prog_fd , & tattr ) ;
ASSERT_OK ( err , " test_run " ) ;
ASSERT_OK ( tattr . retval , " test_run retval " ) ;
ASSERT_EQ ( tattr . ctx_size_out , sizeof ( skb ) , " ctx_size_out " ) ;
selftests: bpf: add selftest for __sk_buff context in BPF_PROG_TEST_RUN
Simple test that sets cb to {1,2,3,4,5} and priority to 6, runs bpf
program that fails if cb is not what we expect and increments cb[i] and
priority. When the test finishes, we check that cb is now {2,3,4,5,6}
and priority is 7.
We also test the sanity checks:
* ctx_in is provided, but ctx_size_in is zero (same for
ctx_out/ctx_size_out)
* unexpected non-zero fields in __sk_buff return EINVAL
Signed-off-by: Stanislav Fomichev <sdf@google.com>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-04-09 11:49:11 -07:00
for ( i = 0 ; i < 5 ; i + + )
2022-02-02 15:54:21 -08:00
ASSERT_EQ ( skb . cb [ i ] , i + 2 , " ctx_out_cb " ) ;
ASSERT_EQ ( skb . priority , 7 , " ctx_out_priority " ) ;
ASSERT_EQ ( skb . ifindex , 1 , " ctx_out_ifindex " ) ;
ASSERT_EQ ( skb . ingress_ifindex , 11 , " ctx_out_ingress_ifindex " ) ;
ASSERT_EQ ( skb . tstamp , 8 , " ctx_out_tstamp " ) ;
ASSERT_EQ ( skb . mark , 10 , " ctx_out_mark " ) ;
2021-11-07 08:55:21 -08:00
bpf_object__close ( obj ) ;
selftests: bpf: add selftest for __sk_buff context in BPF_PROG_TEST_RUN
Simple test that sets cb to {1,2,3,4,5} and priority to 6, runs bpf
program that fails if cb is not what we expect and increments cb[i] and
priority. When the test finishes, we check that cb is now {2,3,4,5,6}
and priority is 7.
We also test the sanity checks:
* ctx_in is provided, but ctx_size_in is zero (same for
ctx_out/ctx_size_out)
* unexpected non-zero fields in __sk_buff return EINVAL
Signed-off-by: Stanislav Fomichev <sdf@google.com>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-04-09 11:49:11 -07:00
}