~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/bpf/prog_tests/flow_dissector_load_bytes.c

Version: ~ [ linux-5.17-rc1 ] ~ [ linux-5.16.2 ] ~ [ linux-5.15.16 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.93 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.173 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.225 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.262 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.297 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.299 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 }
 49 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp