Debian Codegraph @

examples/bpf/legacy/bpf_tailcall.c

raw
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #include "../../../include/bpf_api.h"
3
4 #define ENTRY_INIT 3
5 #define ENTRY_0 0
6 #define ENTRY_1 1
7 #define MAX_JMP_SIZE 2
8
9 #define FOO 42
10 #define BAR 43
11
12 /* This example doesn't really do anything useful, but it's purpose is to
13 * demonstrate eBPF tail calls on a very simple example.
14 *
15 * cls_entry() is our classifier entry point, from there we jump based on
16 * skb->hash into cls_case1() or cls_case2(). They are both part of the
17 * program array jmp_tc. Indicated via __section_tail(), the tc loader
18 * populates the program arrays with the loaded file descriptors already.
19 *
20 * To demonstrate nested jumps, cls_case2() jumps within the same jmp_tc
21 * array to cls_case1(). And whenever we arrive at cls_case1(), we jump
22 * into cls_exit(), part of the jump array jmp_ex.
23 *
24 * Also, to show it's possible, all programs share map_sh and dump the value
25 * that the entry point incremented. The sections that are loaded into a
26 * program array can be atomically replaced during run-time, e.g. to change
27 * classifier behaviour.
28 */
29
30 struct bpf_elf_map __section_maps jmp_tc = {
31 .type = BPF_MAP_TYPE_PROG_ARRAY,
32 .id = FOO,
33 .size_key = sizeof(uint32_t),
34 .size_value = sizeof(uint32_t),
35 .pinning = PIN_OBJECT_NS,
36 .max_elem = MAX_JMP_SIZE,
37 };
38
39 struct bpf_elf_map __section_maps jmp_ex = {
40 .type = BPF_MAP_TYPE_PROG_ARRAY,
41 .id = BAR,
42 .size_key = sizeof(uint32_t),
43 .size_value = sizeof(uint32_t),
44 .pinning = PIN_OBJECT_NS,
45 .max_elem = 1,
46 };
47
48 struct bpf_elf_map __section_maps map_sh = {
49 .type = BPF_MAP_TYPE_ARRAY,
50 .size_key = sizeof(uint32_t),
51 .size_value = sizeof(uint32_t),
52 .pinning = PIN_OBJECT_NS,
53 .max_elem = 1,
54 };
55
56 __section_tail(FOO, ENTRY_0)
57 int cls_case1(struct __sk_buff *skb)
58 {
59 int key = 0, *val;
60
61 val = map_lookup_elem(&map_sh, &key);
62 if (val)
63 printt("case1: map-val: %d from:%u\n", *val, skb->cb[0]);
64
65 skb->cb[0] = ENTRY_0;
66 tail_call(skb, &jmp_ex, ENTRY_0);
67
68 return BPF_H_DEFAULT;
69 }
70
71 __section_tail(FOO, ENTRY_1)
72 int cls_case2(struct __sk_buff *skb)
73 {
74 int key = 0, *val;
75
76 val = map_lookup_elem(&map_sh, &key);
77 if (val)
78 printt("case2: map-val: %d from:%u\n", *val, skb->cb[0]);
79
80 skb->cb[0] = ENTRY_1;
81 tail_call(skb, &jmp_tc, ENTRY_0);
82
83 return BPF_H_DEFAULT;
84 }
85
86 __section_tail(BAR, ENTRY_0)
87 int cls_exit(struct __sk_buff *skb)
88 {
89 int key = 0, *val;
90
91 val = map_lookup_elem(&map_sh, &key);
92 if (val)
93 printt("exit: map-val: %d from:%u\n", *val, skb->cb[0]);
94
95 /* Termination point. */
96 return BPF_H_DEFAULT;
97 }
98
99 __section_cls_entry
100 int cls_entry(struct __sk_buff *skb)
101 {
102 int key = 0, *val;
103
104 /* For transferring state, we can use skb->cb[0] ... skb->cb[4]. */
105 val = map_lookup_elem(&map_sh, &key);
106 if (val) {
107 lock_xadd(val, 1);
108
109 skb->cb[0] = ENTRY_INIT;
110 tail_call(skb, &jmp_tc, skb->hash & (MAX_JMP_SIZE - 1));
111 }
112
113 printt("fallthrough\n");
114 return BPF_H_DEFAULT;
115 }
116
117 BPF_LICENSE("GPL");