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