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