root/tools/testing/selftests/bpf/prog_tests/flow_dissector_load_bytes.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. test_flow_dissector_load_bytes

   1 // SPDX-License-Identifier: GPL-2.0
   2 #include <test_progs.h>
   3 
   4 void test_flow_dissector_load_bytes(void)
   5 {
   6         struct bpf_flow_keys flow_keys;
   7         __u32 duration = 0, retval, size;
   8         struct bpf_insn prog[] = {
   9                 // BPF_REG_1 - 1st argument: context
  10                 // BPF_REG_2 - 2nd argument: offset, start at first byte
  11                 BPF_MOV64_IMM(BPF_REG_2, 0),
  12                 // BPF_REG_3 - 3rd argument: destination, reserve byte on stack
  13                 BPF_ALU64_REG(BPF_MOV, BPF_REG_3, BPF_REG_10),
  14                 BPF_ALU64_IMM(BPF_ADD, BPF_REG_3, -1),
  15                 // BPF_REG_4 - 4th argument: copy one byte
  16                 BPF_MOV64_IMM(BPF_REG_4, 1),
  17                 // bpf_skb_load_bytes(ctx, sizeof(pkt_v4), ptr, 1)
  18                 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  19                              BPF_FUNC_skb_load_bytes),
  20                 BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 2),
  21                 // if (ret == 0) return BPF_DROP (2)
  22                 BPF_MOV64_IMM(BPF_REG_0, BPF_DROP),
  23                 BPF_EXIT_INSN(),
  24                 // if (ret != 0) return BPF_OK (0)
  25                 BPF_MOV64_IMM(BPF_REG_0, BPF_OK),
  26                 BPF_EXIT_INSN(),
  27         };
  28         int fd, err;
  29 
  30         /* make sure bpf_skb_load_bytes is not allowed from skb-less context
  31          */
  32         fd = bpf_load_program(BPF_PROG_TYPE_FLOW_DISSECTOR, prog,
  33                               ARRAY_SIZE(prog), "GPL", 0, NULL, 0);
  34         CHECK(fd < 0,
  35               "flow_dissector-bpf_skb_load_bytes-load",
  36               "fd %d errno %d\n",
  37               fd, errno);
  38 
  39         err = bpf_prog_test_run(fd, 1, &pkt_v4, sizeof(pkt_v4),
  40                                 &flow_keys, &size, &retval, &duration);
  41         CHECK(size != sizeof(flow_keys) || err || retval != 1,
  42               "flow_dissector-bpf_skb_load_bytes",
  43               "err %d errno %d retval %d duration %d size %u/%zu\n",
  44               err, errno, retval, duration, size, sizeof(flow_keys));
  45 
  46         if (fd >= -1)
  47                 close(fd);
  48 }

/* [<][>][^][v][top][bottom][index][help] */