]> git.proxmox.com Git - mirror_iproute2.git/blame - examples/bpf/bpf_shared.c
examples, bpf: further improve examples
[mirror_iproute2.git] / examples / bpf / bpf_shared.c
CommitLineData
41d6e33f 1#include "../../include/bpf_api.h"
32e93fb7
DB
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
41d6e33f 21BPF_ARRAY4(map_sh, 0, PIN_OBJECT_NS, 1); /* or PIN_GLOBAL_NS, or PIN_NONE */
32e93fb7 22
41d6e33f
DB
23__section("egress")
24int emain(struct __sk_buff *skb)
32e93fb7
DB
25{
26 int key = 0, *val;
27
41d6e33f 28 val = map_lookup_elem(&map_sh, &key);
32e93fb7 29 if (val)
41d6e33f 30 lock_xadd(val, 1);
32e93fb7 31
41d6e33f 32 return BPF_H_DEFAULT;
32e93fb7
DB
33}
34
41d6e33f
DB
35__section("ingress")
36int imain(struct __sk_buff *skb)
32e93fb7
DB
37{
38 char fmt[] = "map val: %d\n";
39 int key = 0, *val;
40
41d6e33f 41 val = map_lookup_elem(&map_sh, &key);
32e93fb7 42 if (val)
41d6e33f 43 trace_printk(fmt, sizeof(fmt), *val);
32e93fb7 44
41d6e33f 45 return BPF_H_DEFAULT;
32e93fb7
DB
46}
47
41d6e33f 48BPF_LICENSE("GPL");