1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
4 void test_skb_ctx(void)
6 struct __sk_buff skb
= {
14 struct bpf_prog_test_run_attr tattr
= {
16 .data_size_in
= sizeof(pkt_v4
),
18 .ctx_size_in
= sizeof(skb
),
20 .ctx_size_out
= sizeof(skb
),
22 struct bpf_object
*obj
;
26 err
= bpf_prog_load("./test_skb_ctx.o", BPF_PROG_TYPE_SCHED_CLS
, &obj
,
28 if (CHECK_ATTR(err
, "load", "err %d errno %d\n", err
, errno
))
31 /* ctx_in != NULL, ctx_size_in == 0 */
33 tattr
.ctx_size_in
= 0;
34 err
= bpf_prog_test_run_xattr(&tattr
);
35 CHECK_ATTR(err
== 0, "ctx_size_in", "err %d errno %d\n", err
, errno
);
36 tattr
.ctx_size_in
= sizeof(skb
);
38 /* ctx_out != NULL, ctx_size_out == 0 */
40 tattr
.ctx_size_out
= 0;
41 err
= bpf_prog_test_run_xattr(&tattr
);
42 CHECK_ATTR(err
== 0, "ctx_size_out", "err %d errno %d\n", err
, errno
);
43 tattr
.ctx_size_out
= sizeof(skb
);
45 /* non-zero [len, tc_index] fields should be rejected*/
48 err
= bpf_prog_test_run_xattr(&tattr
);
49 CHECK_ATTR(err
== 0, "len", "err %d errno %d\n", err
, errno
);
53 err
= bpf_prog_test_run_xattr(&tattr
);
54 CHECK_ATTR(err
== 0, "tc_index", "err %d errno %d\n", err
, errno
);
57 /* non-zero [hash, sk] fields should be rejected */
60 err
= bpf_prog_test_run_xattr(&tattr
);
61 CHECK_ATTR(err
== 0, "hash", "err %d errno %d\n", err
, errno
);
64 skb
.sk
= (struct bpf_sock
*)1;
65 err
= bpf_prog_test_run_xattr(&tattr
);
66 CHECK_ATTR(err
== 0, "sk", "err %d errno %d\n", err
, errno
);
69 err
= bpf_prog_test_run_xattr(&tattr
);
70 CHECK_ATTR(err
!= 0 || tattr
.retval
,
72 "err %d errno %d retval %d\n",
73 err
, errno
, tattr
.retval
);
75 CHECK_ATTR(tattr
.ctx_size_out
!= sizeof(skb
),
77 "incorrect output size, want %lu have %u\n",
78 sizeof(skb
), tattr
.ctx_size_out
);
80 for (i
= 0; i
< 5; i
++)
81 CHECK_ATTR(skb
.cb
[i
] != i
+ 2,
83 "skb->cb[i] == %d, expected %d\n",
85 CHECK_ATTR(skb
.priority
!= 7,
87 "skb->priority == %d, expected %d\n",