Blame examples/bpf/bpf_shared.c

Packit Service 3880ab
#include "../../include/bpf_api.h"
Packit Service 3880ab
Packit Service 3880ab
/* Minimal, stand-alone toy map pinning example:
Packit Service 3880ab
 *
Packit Service 3880ab
 * clang -target bpf -O2 [...] -o bpf_shared.o -c bpf_shared.c
Packit Service 3880ab
 * tc filter add dev foo parent 1: bpf obj bpf_shared.o sec egress
Packit Service 3880ab
 * tc filter add dev foo parent ffff: bpf obj bpf_shared.o sec ingress
Packit Service 3880ab
 *
Packit Service 3880ab
 * Both classifier will share the very same map instance in this example,
Packit Service 3880ab
 * so map content can be accessed from ingress *and* egress side!
Packit Service 3880ab
 *
Packit Service 3880ab
 * This example has a pinning of PIN_OBJECT_NS, so it's private and
Packit Service 3880ab
 * thus shared among various program sections within the object.
Packit Service 3880ab
 *
Packit Service 3880ab
 * A setting of PIN_GLOBAL_NS would place it into a global namespace,
Packit Service 3880ab
 * so that it can be shared among different object files. A setting
Packit Service 3880ab
 * of PIN_NONE (= 0) means no sharing, so each tc invocation a new map
Packit Service 3880ab
 * instance is being created.
Packit Service 3880ab
 */
Packit Service 3880ab
Packit Service 3880ab
struct bpf_elf_map __section_maps map_sh = {
Packit Service 3880ab
	.type		= BPF_MAP_TYPE_ARRAY,
Packit Service 3880ab
	.size_key	= sizeof(uint32_t),
Packit Service 3880ab
	.size_value	= sizeof(uint32_t),
Packit Service 3880ab
	.pinning	= PIN_OBJECT_NS, /* or PIN_GLOBAL_NS, or PIN_NONE */
Packit Service 3880ab
	.max_elem	= 1,
Packit Service 3880ab
};
Packit Service 3880ab
Packit Service 3880ab
__section("egress")
Packit Service 3880ab
int emain(struct __sk_buff *skb)
Packit Service 3880ab
{
Packit Service 3880ab
	int key = 0, *val;
Packit Service 3880ab
Packit Service 3880ab
	val = map_lookup_elem(&map_sh, &key);
Packit Service 3880ab
	if (val)
Packit Service 3880ab
		lock_xadd(val, 1);
Packit Service 3880ab
Packit Service 3880ab
	return BPF_H_DEFAULT;
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
__section("ingress")
Packit Service 3880ab
int imain(struct __sk_buff *skb)
Packit Service 3880ab
{
Packit Service 3880ab
	int key = 0, *val;
Packit Service 3880ab
Packit Service 3880ab
	val = map_lookup_elem(&map_sh, &key);
Packit Service 3880ab
	if (val)
Packit Service 3880ab
		printt("map val: %d\n", *val);
Packit Service 3880ab
Packit Service 3880ab
	return BPF_H_DEFAULT;
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
BPF_LICENSE("GPL");