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