141d6e33fc9e5b459b7f715acbd6d8dbeddf58576Daniel Borkmann#include "../../include/bpf_api.h" 232e93fb7f66d55d597b52ec3b10fd44a47784114Daniel Borkmann 332e93fb7f66d55d597b52ec3b10fd44a47784114Daniel Borkmann/* Minimal, stand-alone toy map pinning example: 432e93fb7f66d55d597b52ec3b10fd44a47784114Daniel Borkmann * 532e93fb7f66d55d597b52ec3b10fd44a47784114Daniel Borkmann * clang -target bpf -O2 [...] -o bpf_shared.o -c bpf_shared.c 632e93fb7f66d55d597b52ec3b10fd44a47784114Daniel Borkmann * tc filter add dev foo parent 1: bpf obj bpf_shared.o sec egress 732e93fb7f66d55d597b52ec3b10fd44a47784114Daniel Borkmann * tc filter add dev foo parent ffff: bpf obj bpf_shared.o sec ingress 832e93fb7f66d55d597b52ec3b10fd44a47784114Daniel Borkmann * 932e93fb7f66d55d597b52ec3b10fd44a47784114Daniel Borkmann * Both classifier will share the very same map instance in this example, 1032e93fb7f66d55d597b52ec3b10fd44a47784114Daniel Borkmann * so map content can be accessed from ingress *and* egress side! 1132e93fb7f66d55d597b52ec3b10fd44a47784114Daniel Borkmann * 1232e93fb7f66d55d597b52ec3b10fd44a47784114Daniel Borkmann * This example has a pinning of PIN_OBJECT_NS, so it's private and 1332e93fb7f66d55d597b52ec3b10fd44a47784114Daniel Borkmann * thus shared among various program sections within the object. 1432e93fb7f66d55d597b52ec3b10fd44a47784114Daniel Borkmann * 1532e93fb7f66d55d597b52ec3b10fd44a47784114Daniel Borkmann * A setting of PIN_GLOBAL_NS would place it into a global namespace, 1632e93fb7f66d55d597b52ec3b10fd44a47784114Daniel Borkmann * so that it can be shared among different object files. A setting 1732e93fb7f66d55d597b52ec3b10fd44a47784114Daniel Borkmann * of PIN_NONE (= 0) means no sharing, so each tc invocation a new map 1832e93fb7f66d55d597b52ec3b10fd44a47784114Daniel Borkmann * instance is being created. 1932e93fb7f66d55d597b52ec3b10fd44a47784114Daniel Borkmann */ 2032e93fb7f66d55d597b52ec3b10fd44a47784114Daniel Borkmann 2141d6e33fc9e5b459b7f715acbd6d8dbeddf58576Daniel BorkmannBPF_ARRAY4(map_sh, 0, PIN_OBJECT_NS, 1); /* or PIN_GLOBAL_NS, or PIN_NONE */ 2232e93fb7f66d55d597b52ec3b10fd44a47784114Daniel Borkmann 2341d6e33fc9e5b459b7f715acbd6d8dbeddf58576Daniel Borkmann__section("egress") 2441d6e33fc9e5b459b7f715acbd6d8dbeddf58576Daniel Borkmannint emain(struct __sk_buff *skb) 2532e93fb7f66d55d597b52ec3b10fd44a47784114Daniel Borkmann{ 2632e93fb7f66d55d597b52ec3b10fd44a47784114Daniel Borkmann int key = 0, *val; 2732e93fb7f66d55d597b52ec3b10fd44a47784114Daniel Borkmann 2841d6e33fc9e5b459b7f715acbd6d8dbeddf58576Daniel Borkmann val = map_lookup_elem(&map_sh, &key); 2932e93fb7f66d55d597b52ec3b10fd44a47784114Daniel Borkmann if (val) 3041d6e33fc9e5b459b7f715acbd6d8dbeddf58576Daniel Borkmann lock_xadd(val, 1); 3132e93fb7f66d55d597b52ec3b10fd44a47784114Daniel Borkmann 3241d6e33fc9e5b459b7f715acbd6d8dbeddf58576Daniel Borkmann return BPF_H_DEFAULT; 3332e93fb7f66d55d597b52ec3b10fd44a47784114Daniel Borkmann} 3432e93fb7f66d55d597b52ec3b10fd44a47784114Daniel Borkmann 3541d6e33fc9e5b459b7f715acbd6d8dbeddf58576Daniel Borkmann__section("ingress") 3641d6e33fc9e5b459b7f715acbd6d8dbeddf58576Daniel Borkmannint imain(struct __sk_buff *skb) 3732e93fb7f66d55d597b52ec3b10fd44a47784114Daniel Borkmann{ 3832e93fb7f66d55d597b52ec3b10fd44a47784114Daniel Borkmann char fmt[] = "map val: %d\n"; 3932e93fb7f66d55d597b52ec3b10fd44a47784114Daniel Borkmann int key = 0, *val; 4032e93fb7f66d55d597b52ec3b10fd44a47784114Daniel Borkmann 4141d6e33fc9e5b459b7f715acbd6d8dbeddf58576Daniel Borkmann val = map_lookup_elem(&map_sh, &key); 4232e93fb7f66d55d597b52ec3b10fd44a47784114Daniel Borkmann if (val) 4341d6e33fc9e5b459b7f715acbd6d8dbeddf58576Daniel Borkmann trace_printk(fmt, sizeof(fmt), *val); 4432e93fb7f66d55d597b52ec3b10fd44a47784114Daniel Borkmann 4541d6e33fc9e5b459b7f715acbd6d8dbeddf58576Daniel Borkmann return BPF_H_DEFAULT; 4632e93fb7f66d55d597b52ec3b10fd44a47784114Daniel Borkmann} 4732e93fb7f66d55d597b52ec3b10fd44a47784114Daniel Borkmann 4841d6e33fc9e5b459b7f715acbd6d8dbeddf58576Daniel BorkmannBPF_LICENSE("GPL"); 49