Blame examples/bpf/legacy/bpf_shared.c

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