^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) ================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) bpftool-gen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) ================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) -------------------------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) tool for BPF code-generation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) -------------------------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) :Manual section: 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) SYNOPSIS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) ========
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) **bpftool** [*OPTIONS*] **gen** *COMMAND*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) *OPTIONS* := { { **-j** | **--json** } [{ **-p** | **--pretty** }] }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) *COMMAND* := { **skeleton** | **help** }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) GEN COMMANDS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) =============
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) | **bpftool** **gen skeleton** *FILE*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) | **bpftool** **gen help**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) DESCRIPTION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) ===========
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) **bpftool gen skeleton** *FILE*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) Generate BPF skeleton C header file for a given *FILE*.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) BPF skeleton is an alternative interface to existing libbpf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) APIs for working with BPF objects. Skeleton code is intended
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) to significantly shorten and simplify code to load and work
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) with BPF programs from userspace side. Generated code is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) tailored to specific input BPF object *FILE*, reflecting its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) structure by listing out available maps, program, variables,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) etc. Skeleton eliminates the need to lookup mentioned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) components by name. Instead, if skeleton instantiation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) succeeds, they are populated in skeleton structure as valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) libbpf types (e.g., **struct bpf_map** pointer) and can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) passed to existing generic libbpf APIs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) In addition to simple and reliable access to maps and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) programs, skeleton provides a storage for BPF links (**struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) bpf_link**) for each BPF program within BPF object. When
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) requested, supported BPF programs will be automatically
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) attached and resulting BPF links stored for further use by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) user in pre-allocated fields in skeleton struct. For BPF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) programs that can't be automatically attached by libbpf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) user can attach them manually, but store resulting BPF link
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) in per-program link field. All such set up links will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) automatically destroyed on BPF skeleton destruction. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) eliminates the need for users to manage links manually and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) rely on libbpf support to detach programs and free up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) resources.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) Another facility provided by BPF skeleton is an interface to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) global variables of all supported kinds: mutable, read-only,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) as well as extern ones. This interface allows to pre-setup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) initial values of variables before BPF object is loaded and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) verified by kernel. For non-read-only variables, the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) interface can be used to fetch values of global variables on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) userspace side, even if they are modified by BPF code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) During skeleton generation, contents of source BPF object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) *FILE* is embedded within generated code and is thus not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) necessary to keep around. This ensures skeleton and BPF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) object file are matching 1-to-1 and always stay in sync.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) Generated code is dual-licensed under LGPL-2.1 and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) BSD-2-Clause licenses.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) It is a design goal and guarantee that skeleton interfaces
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) are interoperable with generic libbpf APIs. User should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) always be able to use skeleton API to create and load BPF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) object, and later use libbpf APIs to keep working with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) specific maps, programs, etc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) As part of skeleton, few custom functions are generated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) Each of them is prefixed with object name, derived from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) object file name. I.e., if BPF object file name is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) **example.o**, BPF object name will be **example**. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) following custom functions are provided in such case:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) - **example__open** and **example__open_opts**.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) These functions are used to instantiate skeleton. It
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) corresponds to libbpf's **bpf_object__open**\ () API.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) **_opts** variants accepts extra **bpf_object_open_opts**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) options.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) - **example__load**.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) This function creates maps, loads and verifies BPF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) programs, initializes global data maps. It corresponds to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) libppf's **bpf_object__load**\ () API.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) - **example__open_and_load** combines **example__open** and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) **example__load** invocations in one commonly used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) - **example__attach** and **example__detach**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) This pair of functions allow to attach and detach,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) correspondingly, already loaded BPF object. Only BPF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) programs of types supported by libbpf for auto-attachment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) will be auto-attached and their corresponding BPF links
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) instantiated. For other BPF programs, user can manually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) create a BPF link and assign it to corresponding fields in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) skeleton struct. **example__detach** will detach both
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) links created automatically, as well as those populated by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) user manually.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) - **example__destroy**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) Detach and unload BPF programs, free up all the resources
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) used by skeleton and BPF object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) If BPF object has global variables, corresponding structs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) with memory layout corresponding to global data data section
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) layout will be created. Currently supported ones are: *.data*,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) *.bss*, *.rodata*, and *.kconfig* structs/data sections.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) These data sections/structs can be used to set up initial
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) values of variables, if set before **example__load**.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) Afterwards, if target kernel supports memory-mapped BPF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) arrays, same structs can be used to fetch and update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) (non-read-only) data from userspace, with same simplicity
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) as for BPF side.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) **bpftool gen help**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) Print short help message.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) OPTIONS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) =======
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) .. include:: common_options.rst
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) EXAMPLES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) ========
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) **$ cat example.c**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) ::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) #include <stdbool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) #include <linux/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) #include <linux/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) #include "bpf_helpers.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) const volatile int param1 = 42;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) bool global_flag = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) struct { int x; } data = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) __uint(type, BPF_MAP_TYPE_HASH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) __uint(max_entries, 128);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) __type(key, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) __type(value, long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) } my_map SEC(".maps");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) SEC("raw_tp/sys_enter")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) int handle_sys_enter(struct pt_regs *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) static long my_static_var;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (global_flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) my_static_var++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) data.x += param1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) SEC("raw_tp/sys_exit")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) int handle_sys_exit(struct pt_regs *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) int zero = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) bpf_map_lookup_elem(&my_map, &zero);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) This is example BPF application with two BPF programs and a mix of BPF maps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) and global variables.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) **$ bpftool gen skeleton example.o**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) ::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) /* THIS FILE IS AUTOGENERATED! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) #ifndef __EXAMPLE_SKEL_H__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) #define __EXAMPLE_SKEL_H__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) #include <bpf/libbpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) struct example {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) struct bpf_object_skeleton *skeleton;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) struct bpf_object *obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) struct bpf_map *rodata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) struct bpf_map *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) struct bpf_map *bss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) struct bpf_map *my_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) } maps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) struct bpf_program *handle_sys_enter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) struct bpf_program *handle_sys_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) } progs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) struct bpf_link *handle_sys_enter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) struct bpf_link *handle_sys_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) } links;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) struct example__bss {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) int x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) } data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) } *bss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) struct example__data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) _Bool global_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) long int handle_sys_enter_my_static_var;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) } *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) struct example__rodata {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) int param1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) } *rodata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) static void example__destroy(struct example *obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) static inline struct example *example__open_opts(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) const struct bpf_object_open_opts *opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) static inline struct example *example__open();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) static inline int example__load(struct example *obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) static inline struct example *example__open_and_load();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) static inline int example__attach(struct example *obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) static inline void example__detach(struct example *obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) #endif /* __EXAMPLE_SKEL_H__ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) **$ cat example_user.c**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) ::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) #include "example.skel.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) int main()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) struct example *skel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) skel = example__open();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (!skel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) skel->rodata->param1 = 128;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) err = example__load(skel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) err = example__attach(skel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) /* all libbpf APIs are usable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) printf("my_map name: %s\n", bpf_map__name(skel->maps.my_map));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) printf("sys_enter prog FD: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) bpf_program__fd(skel->progs.handle_sys_enter));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) /* detach and re-attach sys_exit program */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) bpf_link__destroy(skel->links.handle_sys_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) skel->links.handle_sys_exit =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) bpf_program__attach(skel->progs.handle_sys_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) printf("my_static_var: %ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) skel->bss->handle_sys_enter_my_static_var);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) example__destroy(skel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) **# ./example_user**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) ::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) my_map name: my_map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) sys_enter prog FD: 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) my_static_var: 7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) This is a stripped-out version of skeleton generated for above example code.