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