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