| 1 |
#include "../../../include/bpf_api.h" |
| 2 |
|
| 3 |
/* Minimal, stand-alone toy map pinning example: |
| 4 |
* |
| 5 |
* clang -target bpf -O2 [...] -o bpf_shared.o -c bpf_shared.c |
| 6 |
* tc filter add dev foo parent 1: bpf obj bpf_shared.o sec egress |
| 7 |
* tc filter add dev foo parent ffff: bpf obj bpf_shared.o sec ingress |
| 8 |
* |
| 9 |
* Both classifier will share the very same map instance in this example, |
| 10 |
* so map content can be accessed from ingress *and* egress side! |
| 11 |
* |
| 12 |
* This example has a pinning of PIN_OBJECT_NS, so it's private and |
| 13 |
* thus shared among various program sections within the object. |
| 14 |
* |
| 15 |
* A setting of PIN_GLOBAL_NS would place it into a global namespace, |
| 16 |
* so that it can be shared among different object files. A setting |
| 17 |
* of PIN_NONE (= 0) means no sharing, so each tc invocation a new map |
| 18 |
* instance is being created. |
| 19 |
*/ |
| 20 |
|
| 21 |
struct bpf_elf_map __section_maps map_sh = { |
| 22 |
.type = BPF_MAP_TYPE_ARRAY, |
| 23 |
.size_key = sizeof(uint32_t), |
| 24 |
.size_value = sizeof(uint32_t), |
| 25 |
.pinning = PIN_OBJECT_NS, /* or PIN_GLOBAL_NS, or PIN_NONE */ |
| 26 |
.max_elem = 1, |
| 27 |
}; |
| 28 |
|
| 29 |
__section("egress") |
| 30 |
int emain(struct __sk_buff *skb) |
| 31 |
{ |
| 32 |
int key = 0, *val; |
| 33 |
|
| 34 |
val = map_lookup_elem(&map_sh, &key); |
| 35 |
if (val) |
| 36 |
lock_xadd(val, 1); |
| 37 |
|
| 38 |
return BPF_H_DEFAULT; |
| 39 |
} |
| 40 |
|
| 41 |
__section("ingress") |
| 42 |
int imain(struct __sk_buff *skb) |
| 43 |
{ |
| 44 |
int key = 0, *val; |
| 45 |
|
| 46 |
val = map_lookup_elem(&map_sh, &key); |
| 47 |
if (val) |
| 48 |
printt("map val: %d\n", *val); |
| 49 |
|
| 50 |
return BPF_H_DEFAULT; |
| 51 |
} |
| 52 |
|
| 53 |
BPF_LICENSE("GPL"); |