13daf8e70SStanislav Fomichev // SPDX-License-Identifier: GPL-2.0
23daf8e70SStanislav Fomichev #include <test_progs.h>
3488a23b8SStanislav Fomichev #include <network_helpers.h>
43daf8e70SStanislav Fomichev
test_skb_ctx(void)53daf8e70SStanislav Fomichev void test_skb_ctx(void)
63daf8e70SStanislav Fomichev {
73daf8e70SStanislav Fomichev struct __sk_buff skb = {
83daf8e70SStanislav Fomichev .cb[0] = 1,
93daf8e70SStanislav Fomichev .cb[1] = 2,
103daf8e70SStanislav Fomichev .cb[2] = 3,
113daf8e70SStanislav Fomichev .cb[3] = 4,
123daf8e70SStanislav Fomichev .cb[4] = 5,
133daf8e70SStanislav Fomichev .priority = 6,
14b238290bSNeil Spring .ingress_ifindex = 11,
1521594c44SDmitry Yakunin .ifindex = 1,
1695fbda1eSStanislav Fomichev .tstamp = 7,
17a06bf42fSStanislav Fomichev .wire_len = 100,
18a06bf42fSStanislav Fomichev .gso_segs = 8,
196de6c1f8SNikita V. Shirokov .mark = 9,
2062511ceaSWillem de Bruijn .gso_size = 10,
213384c7c7SVadim Fedorenko .hwtstamp = 11,
223daf8e70SStanislav Fomichev };
2339316183SDelyan Kratunov LIBBPF_OPTS(bpf_test_run_opts, tattr,
243daf8e70SStanislav Fomichev .data_in = &pkt_v4,
253daf8e70SStanislav Fomichev .data_size_in = sizeof(pkt_v4),
263daf8e70SStanislav Fomichev .ctx_in = &skb,
273daf8e70SStanislav Fomichev .ctx_size_in = sizeof(skb),
283daf8e70SStanislav Fomichev .ctx_out = &skb,
293daf8e70SStanislav Fomichev .ctx_size_out = sizeof(skb),
3039316183SDelyan Kratunov );
313daf8e70SStanislav Fomichev struct bpf_object *obj;
3239316183SDelyan Kratunov int err, prog_fd, i;
333daf8e70SStanislav Fomichev
34*afef88e6SDaniel Müller err = bpf_prog_test_load("./test_skb_ctx.bpf.o", BPF_PROG_TYPE_SCHED_CLS,
3539316183SDelyan Kratunov &obj, &prog_fd);
3639316183SDelyan Kratunov if (!ASSERT_OK(err, "load"))
373daf8e70SStanislav Fomichev return;
383daf8e70SStanislav Fomichev
393daf8e70SStanislav Fomichev /* ctx_in != NULL, ctx_size_in == 0 */
403daf8e70SStanislav Fomichev
413daf8e70SStanislav Fomichev tattr.ctx_size_in = 0;
4239316183SDelyan Kratunov err = bpf_prog_test_run_opts(prog_fd, &tattr);
4339316183SDelyan Kratunov ASSERT_NEQ(err, 0, "ctx_size_in");
443daf8e70SStanislav Fomichev tattr.ctx_size_in = sizeof(skb);
453daf8e70SStanislav Fomichev
463daf8e70SStanislav Fomichev /* ctx_out != NULL, ctx_size_out == 0 */
473daf8e70SStanislav Fomichev
483daf8e70SStanislav Fomichev tattr.ctx_size_out = 0;
4939316183SDelyan Kratunov err = bpf_prog_test_run_opts(prog_fd, &tattr);
5039316183SDelyan Kratunov ASSERT_NEQ(err, 0, "ctx_size_out");
513daf8e70SStanislav Fomichev tattr.ctx_size_out = sizeof(skb);
523daf8e70SStanislav Fomichev
533daf8e70SStanislav Fomichev /* non-zero [len, tc_index] fields should be rejected*/
543daf8e70SStanislav Fomichev
553daf8e70SStanislav Fomichev skb.len = 1;
5639316183SDelyan Kratunov err = bpf_prog_test_run_opts(prog_fd, &tattr);
5739316183SDelyan Kratunov ASSERT_NEQ(err, 0, "len");
583daf8e70SStanislav Fomichev skb.len = 0;
593daf8e70SStanislav Fomichev
603daf8e70SStanislav Fomichev skb.tc_index = 1;
6139316183SDelyan Kratunov err = bpf_prog_test_run_opts(prog_fd, &tattr);
6239316183SDelyan Kratunov ASSERT_NEQ(err, 0, "tc_index");
633daf8e70SStanislav Fomichev skb.tc_index = 0;
643daf8e70SStanislav Fomichev
653daf8e70SStanislav Fomichev /* non-zero [hash, sk] fields should be rejected */
663daf8e70SStanislav Fomichev
673daf8e70SStanislav Fomichev skb.hash = 1;
6839316183SDelyan Kratunov err = bpf_prog_test_run_opts(prog_fd, &tattr);
6939316183SDelyan Kratunov ASSERT_NEQ(err, 0, "hash");
703daf8e70SStanislav Fomichev skb.hash = 0;
713daf8e70SStanislav Fomichev
723daf8e70SStanislav Fomichev skb.sk = (struct bpf_sock *)1;
7339316183SDelyan Kratunov err = bpf_prog_test_run_opts(prog_fd, &tattr);
7439316183SDelyan Kratunov ASSERT_NEQ(err, 0, "sk");
753daf8e70SStanislav Fomichev skb.sk = 0;
763daf8e70SStanislav Fomichev
7739316183SDelyan Kratunov err = bpf_prog_test_run_opts(prog_fd, &tattr);
7839316183SDelyan Kratunov ASSERT_OK(err, "test_run");
7939316183SDelyan Kratunov ASSERT_OK(tattr.retval, "test_run retval");
8039316183SDelyan Kratunov ASSERT_EQ(tattr.ctx_size_out, sizeof(skb), "ctx_size_out");
813daf8e70SStanislav Fomichev
823daf8e70SStanislav Fomichev for (i = 0; i < 5; i++)
8339316183SDelyan Kratunov ASSERT_EQ(skb.cb[i], i + 2, "ctx_out_cb");
8439316183SDelyan Kratunov ASSERT_EQ(skb.priority, 7, "ctx_out_priority");
8539316183SDelyan Kratunov ASSERT_EQ(skb.ifindex, 1, "ctx_out_ifindex");
8639316183SDelyan Kratunov ASSERT_EQ(skb.ingress_ifindex, 11, "ctx_out_ingress_ifindex");
8739316183SDelyan Kratunov ASSERT_EQ(skb.tstamp, 8, "ctx_out_tstamp");
8839316183SDelyan Kratunov ASSERT_EQ(skb.mark, 10, "ctx_out_mark");
898c7a9552SAndrii Nakryiko
908c7a9552SAndrii Nakryiko bpf_object__close(obj);
913daf8e70SStanislav Fomichev }
92