Daniel Borkmann | 41d6e33 | 2015-12-02 00:25:36 +0100 | [diff] [blame] | 1 | #include "../../include/bpf_api.h" |
Daniel Borkmann | 0b7e3fc | 2015-11-26 15:38:46 +0100 | [diff] [blame] | 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 | */ |
Daniel Borkmann | 0b7e3fc | 2015-11-26 15:38:46 +0100 | [diff] [blame] | 28 | |
Daniel Borkmann | 4dd3f50 | 2016-04-09 00:32:05 +0200 | [diff] [blame] | 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 | }; |
Daniel Borkmann | 0b7e3fc | 2015-11-26 15:38:46 +0100 | [diff] [blame] | 37 | |
Daniel Borkmann | 4dd3f50 | 2016-04-09 00:32:05 +0200 | [diff] [blame] | 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 | }; |
Daniel Borkmann | 0b7e3fc | 2015-11-26 15:38:46 +0100 | [diff] [blame] | 54 | |
Daniel Borkmann | 41d6e33 | 2015-12-02 00:25:36 +0100 | [diff] [blame] | 55 | __section_tail(FOO, ENTRY_0) |
| 56 | int cls_case1(struct __sk_buff *skb) |
Daniel Borkmann | 0b7e3fc | 2015-11-26 15:38:46 +0100 | [diff] [blame] | 57 | { |
Daniel Borkmann | 0b7e3fc | 2015-11-26 15:38:46 +0100 | [diff] [blame] | 58 | int key = 0, *val; |
| 59 | |
Daniel Borkmann | 41d6e33 | 2015-12-02 00:25:36 +0100 | [diff] [blame] | 60 | val = map_lookup_elem(&map_sh, &key); |
Daniel Borkmann | 0b7e3fc | 2015-11-26 15:38:46 +0100 | [diff] [blame] | 61 | if (val) |
Daniel Borkmann | 92a3699 | 2016-02-07 02:11:50 +0100 | [diff] [blame] | 62 | printt("case1: map-val: %d from:%u\n", *val, skb->cb[0]); |
Daniel Borkmann | 0b7e3fc | 2015-11-26 15:38:46 +0100 | [diff] [blame] | 63 | |
| 64 | skb->cb[0] = ENTRY_0; |
Daniel Borkmann | 41d6e33 | 2015-12-02 00:25:36 +0100 | [diff] [blame] | 65 | tail_call(skb, &jmp_ex, ENTRY_0); |
| 66 | |
| 67 | return BPF_H_DEFAULT; |
Daniel Borkmann | 0b7e3fc | 2015-11-26 15:38:46 +0100 | [diff] [blame] | 68 | } |
| 69 | |
Daniel Borkmann | 41d6e33 | 2015-12-02 00:25:36 +0100 | [diff] [blame] | 70 | __section_tail(FOO, ENTRY_1) |
| 71 | int cls_case2(struct __sk_buff *skb) |
Daniel Borkmann | 0b7e3fc | 2015-11-26 15:38:46 +0100 | [diff] [blame] | 72 | { |
Daniel Borkmann | 0b7e3fc | 2015-11-26 15:38:46 +0100 | [diff] [blame] | 73 | int key = 0, *val; |
| 74 | |
Daniel Borkmann | 41d6e33 | 2015-12-02 00:25:36 +0100 | [diff] [blame] | 75 | val = map_lookup_elem(&map_sh, &key); |
Daniel Borkmann | 0b7e3fc | 2015-11-26 15:38:46 +0100 | [diff] [blame] | 76 | if (val) |
Daniel Borkmann | 92a3699 | 2016-02-07 02:11:50 +0100 | [diff] [blame] | 77 | printt("case2: map-val: %d from:%u\n", *val, skb->cb[0]); |
Daniel Borkmann | 0b7e3fc | 2015-11-26 15:38:46 +0100 | [diff] [blame] | 78 | |
| 79 | skb->cb[0] = ENTRY_1; |
Daniel Borkmann | 41d6e33 | 2015-12-02 00:25:36 +0100 | [diff] [blame] | 80 | tail_call(skb, &jmp_tc, ENTRY_0); |
| 81 | |
| 82 | return BPF_H_DEFAULT; |
Daniel Borkmann | 0b7e3fc | 2015-11-26 15:38:46 +0100 | [diff] [blame] | 83 | } |
| 84 | |
Daniel Borkmann | 41d6e33 | 2015-12-02 00:25:36 +0100 | [diff] [blame] | 85 | __section_tail(BAR, ENTRY_0) |
| 86 | int cls_exit(struct __sk_buff *skb) |
Daniel Borkmann | 0b7e3fc | 2015-11-26 15:38:46 +0100 | [diff] [blame] | 87 | { |
Daniel Borkmann | 0b7e3fc | 2015-11-26 15:38:46 +0100 | [diff] [blame] | 88 | int key = 0, *val; |
| 89 | |
Daniel Borkmann | 41d6e33 | 2015-12-02 00:25:36 +0100 | [diff] [blame] | 90 | val = map_lookup_elem(&map_sh, &key); |
Daniel Borkmann | 0b7e3fc | 2015-11-26 15:38:46 +0100 | [diff] [blame] | 91 | if (val) |
Daniel Borkmann | 92a3699 | 2016-02-07 02:11:50 +0100 | [diff] [blame] | 92 | printt("exit: map-val: %d from:%u\n", *val, skb->cb[0]); |
Daniel Borkmann | 0b7e3fc | 2015-11-26 15:38:46 +0100 | [diff] [blame] | 93 | |
| 94 | /* Termination point. */ |
Daniel Borkmann | 41d6e33 | 2015-12-02 00:25:36 +0100 | [diff] [blame] | 95 | return BPF_H_DEFAULT; |
Daniel Borkmann | 0b7e3fc | 2015-11-26 15:38:46 +0100 | [diff] [blame] | 96 | } |
| 97 | |
Daniel Borkmann | 41d6e33 | 2015-12-02 00:25:36 +0100 | [diff] [blame] | 98 | __section_cls_entry |
| 99 | int cls_entry(struct __sk_buff *skb) |
Daniel Borkmann | 0b7e3fc | 2015-11-26 15:38:46 +0100 | [diff] [blame] | 100 | { |
Daniel Borkmann | 0b7e3fc | 2015-11-26 15:38:46 +0100 | [diff] [blame] | 101 | int key = 0, *val; |
| 102 | |
| 103 | /* For transferring state, we can use skb->cb[0] ... skb->cb[4]. */ |
Daniel Borkmann | 41d6e33 | 2015-12-02 00:25:36 +0100 | [diff] [blame] | 104 | val = map_lookup_elem(&map_sh, &key); |
Daniel Borkmann | 0b7e3fc | 2015-11-26 15:38:46 +0100 | [diff] [blame] | 105 | if (val) { |
Daniel Borkmann | 41d6e33 | 2015-12-02 00:25:36 +0100 | [diff] [blame] | 106 | lock_xadd(val, 1); |
Daniel Borkmann | 0b7e3fc | 2015-11-26 15:38:46 +0100 | [diff] [blame] | 107 | |
| 108 | skb->cb[0] = ENTRY_INIT; |
Daniel Borkmann | 41d6e33 | 2015-12-02 00:25:36 +0100 | [diff] [blame] | 109 | tail_call(skb, &jmp_tc, skb->hash & (MAX_JMP_SIZE - 1)); |
Daniel Borkmann | 0b7e3fc | 2015-11-26 15:38:46 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Daniel Borkmann | 92a3699 | 2016-02-07 02:11:50 +0100 | [diff] [blame] | 112 | printt("fallthrough\n"); |
Daniel Borkmann | 41d6e33 | 2015-12-02 00:25:36 +0100 | [diff] [blame] | 113 | return BPF_H_DEFAULT; |
Daniel Borkmann | 0b7e3fc | 2015-11-26 15:38:46 +0100 | [diff] [blame] | 114 | } |
| 115 | |
Daniel Borkmann | 41d6e33 | 2015-12-02 00:25:36 +0100 | [diff] [blame] | 116 | BPF_LICENSE("GPL"); |