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