1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3
test_skb_ctx(void)4 void test_skb_ctx(void)
5 {
6 struct __sk_buff skb = {
7 .cb[0] = 1,
8 .cb[1] = 2,
9 .cb[2] = 3,
10 .cb[3] = 4,
11 .cb[4] = 5,
12 .priority = 6,
13 .tstamp = 7,
14 };
15 struct bpf_prog_test_run_attr tattr = {
16 .data_in = &pkt_v4,
17 .data_size_in = sizeof(pkt_v4),
18 .ctx_in = &skb,
19 .ctx_size_in = sizeof(skb),
20 .ctx_out = &skb,
21 .ctx_size_out = sizeof(skb),
22 };
23 struct bpf_object *obj;
24 int err;
25 int i;
26
27 err = bpf_prog_load("./test_skb_ctx.o", BPF_PROG_TYPE_SCHED_CLS, &obj,
28 &tattr.prog_fd);
29 if (CHECK_ATTR(err, "load", "err %d errno %d\n", err, errno))
30 return;
31
32 /* ctx_in != NULL, ctx_size_in == 0 */
33
34 tattr.ctx_size_in = 0;
35 err = bpf_prog_test_run_xattr(&tattr);
36 CHECK_ATTR(err == 0, "ctx_size_in", "err %d errno %d\n", err, errno);
37 tattr.ctx_size_in = sizeof(skb);
38
39 /* ctx_out != NULL, ctx_size_out == 0 */
40
41 tattr.ctx_size_out = 0;
42 err = bpf_prog_test_run_xattr(&tattr);
43 CHECK_ATTR(err == 0, "ctx_size_out", "err %d errno %d\n", err, errno);
44 tattr.ctx_size_out = sizeof(skb);
45
46 /* non-zero [len, tc_index] fields should be rejected*/
47
48 skb.len = 1;
49 err = bpf_prog_test_run_xattr(&tattr);
50 CHECK_ATTR(err == 0, "len", "err %d errno %d\n", err, errno);
51 skb.len = 0;
52
53 skb.tc_index = 1;
54 err = bpf_prog_test_run_xattr(&tattr);
55 CHECK_ATTR(err == 0, "tc_index", "err %d errno %d\n", err, errno);
56 skb.tc_index = 0;
57
58 /* non-zero [hash, sk] fields should be rejected */
59
60 skb.hash = 1;
61 err = bpf_prog_test_run_xattr(&tattr);
62 CHECK_ATTR(err == 0, "hash", "err %d errno %d\n", err, errno);
63 skb.hash = 0;
64
65 skb.sk = (struct bpf_sock *)1;
66 err = bpf_prog_test_run_xattr(&tattr);
67 CHECK_ATTR(err == 0, "sk", "err %d errno %d\n", err, errno);
68 skb.sk = 0;
69
70 err = bpf_prog_test_run_xattr(&tattr);
71 CHECK_ATTR(err != 0 || tattr.retval,
72 "run",
73 "err %d errno %d retval %d\n",
74 err, errno, tattr.retval);
75
76 CHECK_ATTR(tattr.ctx_size_out != sizeof(skb),
77 "ctx_size_out",
78 "incorrect output size, want %lu have %u\n",
79 sizeof(skb), tattr.ctx_size_out);
80
81 for (i = 0; i < 5; i++)
82 CHECK_ATTR(skb.cb[i] != i + 2,
83 "ctx_out_cb",
84 "skb->cb[i] == %d, expected %d\n",
85 skb.cb[i], i + 2);
86 CHECK_ATTR(skb.priority != 7,
87 "ctx_out_priority",
88 "skb->priority == %d, expected %d\n",
89 skb.priority, 7);
90 CHECK_ATTR(skb.tstamp != 8,
91 "ctx_out_tstamp",
92 "skb->tstamp == %lld, expected %d\n",
93 skb.tstamp, 8);
94 }
95