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 | /* This example demonstrates how classifier run-time behaviour |
| 4 | * can be altered with tail calls. We start out with an empty |
| 5 | * jmp_tc array, then add section aaa to the array slot 0, and |
| 6 | * later on atomically replace it with section bbb. Note that |
| 7 | * as shown in other examples, the tc loader can prepopulate |
| 8 | * tail called sections, here we start out with an empty one |
| 9 | * on purpose to show it can also be done this way. |
| 10 | * |
| 11 | * tc filter add dev foo parent ffff: bpf obj graft.o |
| 12 | * tc exec bpf dbg |
| 13 | * [...] |
| 14 | * Socket Thread-20229 [001] ..s. 138993.003923: : fallthrough |
| 15 | * <idle>-0 [001] ..s. 138993.202265: : fallthrough |
| 16 | * Socket Thread-20229 [001] ..s. 138994.004149: : fallthrough |
| 17 | * [...] |
| 18 | * |
| 19 | * tc exec bpf graft m:globals/jmp_tc key 0 obj graft.o sec aaa |
| 20 | * tc exec bpf dbg |
| 21 | * [...] |
| 22 | * Socket Thread-19818 [002] ..s. 139012.053587: : aaa |
| 23 | * <idle>-0 [002] ..s. 139012.172359: : aaa |
| 24 | * Socket Thread-19818 [001] ..s. 139012.173556: : aaa |
| 25 | * [...] |
| 26 | * |
| 27 | * tc exec bpf graft m:globals/jmp_tc key 0 obj graft.o sec bbb |
| 28 | * tc exec bpf dbg |
| 29 | * [...] |
| 30 | * Socket Thread-19818 [002] ..s. 139022.102967: : bbb |
| 31 | * <idle>-0 [002] ..s. 139022.155640: : bbb |
| 32 | * Socket Thread-19818 [001] ..s. 139022.156730: : bbb |
| 33 | * [...] |
| 34 | */ |
Daniel Borkmann | 0b7e3fc | 2015-11-26 15:38:46 +0100 | [diff] [blame] | 35 | |
Daniel Borkmann | 4dd3f50 | 2016-04-09 00:32:05 +0200 | [diff] [blame] | 36 | struct bpf_elf_map __section_maps jmp_tc = { |
| 37 | .type = BPF_MAP_TYPE_PROG_ARRAY, |
| 38 | .size_key = sizeof(uint32_t), |
| 39 | .size_value = sizeof(uint32_t), |
| 40 | .pinning = PIN_GLOBAL_NS, |
| 41 | .max_elem = 1, |
| 42 | }; |
Daniel Borkmann | 41d6e33 | 2015-12-02 00:25:36 +0100 | [diff] [blame] | 43 | |
| 44 | __section("aaa") |
| 45 | int cls_aaa(struct __sk_buff *skb) |
Daniel Borkmann | 0b7e3fc | 2015-11-26 15:38:46 +0100 | [diff] [blame] | 46 | { |
Daniel Borkmann | 92a3699 | 2016-02-07 02:11:50 +0100 | [diff] [blame] | 47 | printt("aaa\n"); |
Daniel Borkmann | 41d6e33 | 2015-12-02 00:25:36 +0100 | [diff] [blame] | 48 | return TC_H_MAKE(1, 42); |
Daniel Borkmann | 0b7e3fc | 2015-11-26 15:38:46 +0100 | [diff] [blame] | 49 | } |
| 50 | |
Daniel Borkmann | 41d6e33 | 2015-12-02 00:25:36 +0100 | [diff] [blame] | 51 | __section("bbb") |
| 52 | int cls_bbb(struct __sk_buff *skb) |
Daniel Borkmann | 0b7e3fc | 2015-11-26 15:38:46 +0100 | [diff] [blame] | 53 | { |
Daniel Borkmann | 92a3699 | 2016-02-07 02:11:50 +0100 | [diff] [blame] | 54 | printt("bbb\n"); |
Daniel Borkmann | 41d6e33 | 2015-12-02 00:25:36 +0100 | [diff] [blame] | 55 | return TC_H_MAKE(1, 43); |
Daniel Borkmann | 0b7e3fc | 2015-11-26 15:38:46 +0100 | [diff] [blame] | 56 | } |
| 57 | |
Daniel Borkmann | 41d6e33 | 2015-12-02 00:25:36 +0100 | [diff] [blame] | 58 | __section_cls_entry |
| 59 | int cls_entry(struct __sk_buff *skb) |
Daniel Borkmann | 0b7e3fc | 2015-11-26 15:38:46 +0100 | [diff] [blame] | 60 | { |
Daniel Borkmann | 41d6e33 | 2015-12-02 00:25:36 +0100 | [diff] [blame] | 61 | tail_call(skb, &jmp_tc, 0); |
Daniel Borkmann | 92a3699 | 2016-02-07 02:11:50 +0100 | [diff] [blame] | 62 | printt("fallthrough\n"); |
Daniel Borkmann | 41d6e33 | 2015-12-02 00:25:36 +0100 | [diff] [blame] | 63 | return BPF_H_DEFAULT; |
Daniel Borkmann | 0b7e3fc | 2015-11-26 15:38:46 +0100 | [diff] [blame] | 64 | } |
| 65 | |
Daniel Borkmann | 41d6e33 | 2015-12-02 00:25:36 +0100 | [diff] [blame] | 66 | BPF_LICENSE("GPL"); |