linux/tools/testing/selftests/bpf/prog_tests/core_read_macros.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/* Copyright (c) 2020 Facebook */
   3
   4#include <test_progs.h>
   5
   6struct callback_head {
   7        struct callback_head *next;
   8        void (*func)(struct callback_head *head);
   9};
  10
  11/* ___shuffled flavor is just an illusion for BPF code, it doesn't really
  12 * exist and user-space needs to provide data in the memory layout that
  13 * matches callback_head. We just defined ___shuffled flavor to make it easier
  14 * to work with the skeleton
  15 */
  16struct callback_head___shuffled {
  17        struct callback_head___shuffled *next;
  18        void (*func)(struct callback_head *head);
  19};
  20
  21#include "test_core_read_macros.skel.h"
  22
  23void test_core_read_macros(void)
  24{
  25        int duration = 0, err;
  26        struct test_core_read_macros* skel;
  27        struct test_core_read_macros__bss *bss;
  28        struct callback_head u_probe_in;
  29        struct callback_head___shuffled u_core_in;
  30
  31        skel = test_core_read_macros__open_and_load();
  32        if (CHECK(!skel, "skel_open", "failed to open skeleton\n"))
  33                return;
  34        bss = skel->bss;
  35        bss->my_pid = getpid();
  36
  37        /* next pointers have to be set from the kernel side */
  38        bss->k_probe_in.func = (void *)(long)0x1234;
  39        bss->k_core_in.func = (void *)(long)0xabcd;
  40
  41        u_probe_in.next = &u_probe_in;
  42        u_probe_in.func = (void *)(long)0x5678;
  43        bss->u_probe_in = &u_probe_in;
  44
  45        u_core_in.next = &u_core_in;
  46        u_core_in.func = (void *)(long)0xdbca;
  47        bss->u_core_in = &u_core_in;
  48
  49        err = test_core_read_macros__attach(skel);
  50        if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err))
  51                goto cleanup;
  52
  53        /* trigger tracepoint */
  54        usleep(1);
  55
  56        ASSERT_EQ(bss->k_probe_out, 0x1234, "k_probe_out");
  57        ASSERT_EQ(bss->k_core_out, 0xabcd, "k_core_out");
  58
  59        ASSERT_EQ(bss->u_probe_out, 0x5678, "u_probe_out");
  60        ASSERT_EQ(bss->u_core_out, 0xdbca, "u_core_out");
  61
  62cleanup:
  63        test_core_read_macros__destroy(skel);
  64}
  65