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