1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include <network_helpers.h>
5 void test_skb_ctx(void)
7 struct __sk_buff skb
= {
21 struct bpf_prog_test_run_attr tattr
= {
23 .data_size_in
= sizeof(pkt_v4
),
25 .ctx_size_in
= sizeof(skb
),
27 .ctx_size_out
= sizeof(skb
),
29 struct bpf_object
*obj
;
33 err
= bpf_prog_load("./test_skb_ctx.o", BPF_PROG_TYPE_SCHED_CLS
, &obj
,
35 if (CHECK_ATTR(err
, "load", "err %d errno %d\n", err
, errno
))
38 /* ctx_in != NULL, ctx_size_in == 0 */
40 tattr
.ctx_size_in
= 0;
41 err
= bpf_prog_test_run_xattr(&tattr
);
42 CHECK_ATTR(err
== 0, "ctx_size_in", "err %d errno %d\n", err
, errno
);
43 tattr
.ctx_size_in
= sizeof(skb
);
45 /* ctx_out != NULL, ctx_size_out == 0 */
47 tattr
.ctx_size_out
= 0;
48 err
= bpf_prog_test_run_xattr(&tattr
);
49 CHECK_ATTR(err
== 0, "ctx_size_out", "err %d errno %d\n", err
, errno
);
50 tattr
.ctx_size_out
= sizeof(skb
);
52 /* non-zero [len, tc_index] fields should be rejected*/
55 err
= bpf_prog_test_run_xattr(&tattr
);
56 CHECK_ATTR(err
== 0, "len", "err %d errno %d\n", err
, errno
);
60 err
= bpf_prog_test_run_xattr(&tattr
);
61 CHECK_ATTR(err
== 0, "tc_index", "err %d errno %d\n", err
, errno
);
64 /* non-zero [hash, sk] fields should be rejected */
67 err
= bpf_prog_test_run_xattr(&tattr
);
68 CHECK_ATTR(err
== 0, "hash", "err %d errno %d\n", err
, errno
);
71 skb
.sk
= (struct bpf_sock
*)1;
72 err
= bpf_prog_test_run_xattr(&tattr
);
73 CHECK_ATTR(err
== 0, "sk", "err %d errno %d\n", err
, errno
);
76 err
= bpf_prog_test_run_xattr(&tattr
);
77 CHECK_ATTR(err
!= 0 || tattr
.retval
,
79 "err %d errno %d retval %d\n",
80 err
, errno
, tattr
.retval
);
82 CHECK_ATTR(tattr
.ctx_size_out
!= sizeof(skb
),
84 "incorrect output size, want %zu have %u\n",
85 sizeof(skb
), tattr
.ctx_size_out
);
87 for (i
= 0; i
< 5; i
++)
88 CHECK_ATTR(skb
.cb
[i
] != i
+ 2,
90 "skb->cb[i] == %d, expected %d\n",
92 CHECK_ATTR(skb
.priority
!= 7,
94 "skb->priority == %d, expected %d\n",
96 CHECK_ATTR(skb
.ifindex
!= 1,
98 "skb->ifindex == %d, expected %d\n",
100 CHECK_ATTR(skb
.tstamp
!= 8,
102 "skb->tstamp == %lld, expected %d\n",
104 CHECK_ATTR(skb
.mark
!= 10,
106 "skb->mark == %u, expected %d\n",