]> git.proxmox.com Git - mirror_iproute2.git/blame - examples/bpf/bpf_tailcall.c
examples, bpf: further improve examples
[mirror_iproute2.git] / examples / bpf / bpf_tailcall.c
CommitLineData
41d6e33f 1#include "../../include/bpf_api.h"
0b7e3fc8
DB
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 */
41d6e33f
DB
28
29BPF_PROG_ARRAY(jmp_tc, FOO, PIN_OBJECT_NS, MAX_JMP_SIZE);
30BPF_PROG_ARRAY(jmp_ex, BAR, PIN_OBJECT_NS, 1);
31
32BPF_ARRAY4(map_sh, 0, PIN_OBJECT_NS, 1);
33
34__section_tail(FOO, ENTRY_0)
35int cls_case1(struct __sk_buff *skb)
0b7e3fc8
DB
36{
37 char fmt[] = "case1: map-val: %d from:%u\n";
38 int key = 0, *val;
39
41d6e33f 40 val = map_lookup_elem(&map_sh, &key);
0b7e3fc8 41 if (val)
41d6e33f 42 trace_printk(fmt, sizeof(fmt), *val, skb->cb[0]);
0b7e3fc8
DB
43
44 skb->cb[0] = ENTRY_0;
41d6e33f
DB
45 tail_call(skb, &jmp_ex, ENTRY_0);
46
47 return BPF_H_DEFAULT;
0b7e3fc8
DB
48}
49
41d6e33f
DB
50__section_tail(FOO, ENTRY_1)
51int cls_case2(struct __sk_buff *skb)
0b7e3fc8
DB
52{
53 char fmt[] = "case2: map-val: %d from:%u\n";
54 int key = 0, *val;
55
41d6e33f 56 val = map_lookup_elem(&map_sh, &key);
0b7e3fc8 57 if (val)
41d6e33f 58 trace_printk(fmt, sizeof(fmt), *val, skb->cb[0]);
0b7e3fc8
DB
59
60 skb->cb[0] = ENTRY_1;
41d6e33f
DB
61 tail_call(skb, &jmp_tc, ENTRY_0);
62
63 return BPF_H_DEFAULT;
0b7e3fc8
DB
64}
65
41d6e33f
DB
66__section_tail(BAR, ENTRY_0)
67int cls_exit(struct __sk_buff *skb)
0b7e3fc8
DB
68{
69 char fmt[] = "exit: map-val: %d from:%u\n";
70 int key = 0, *val;
71
41d6e33f 72 val = map_lookup_elem(&map_sh, &key);
0b7e3fc8 73 if (val)
41d6e33f 74 trace_printk(fmt, sizeof(fmt), *val, skb->cb[0]);
0b7e3fc8
DB
75
76 /* Termination point. */
41d6e33f 77 return BPF_H_DEFAULT;
0b7e3fc8
DB
78}
79
41d6e33f
DB
80__section_cls_entry
81int cls_entry(struct __sk_buff *skb)
0b7e3fc8
DB
82{
83 char fmt[] = "fallthrough\n";
84 int key = 0, *val;
85
86 /* For transferring state, we can use skb->cb[0] ... skb->cb[4]. */
41d6e33f 87 val = map_lookup_elem(&map_sh, &key);
0b7e3fc8 88 if (val) {
41d6e33f 89 lock_xadd(val, 1);
0b7e3fc8
DB
90
91 skb->cb[0] = ENTRY_INIT;
41d6e33f 92 tail_call(skb, &jmp_tc, skb->hash & (MAX_JMP_SIZE - 1));
0b7e3fc8
DB
93 }
94
41d6e33f
DB
95 trace_printk(fmt, sizeof(fmt));
96 return BPF_H_DEFAULT;
0b7e3fc8
DB
97}
98
41d6e33f 99BPF_LICENSE("GPL");