Reputation: 135
I am creating an entry into the map of type BPF_MAP_TYPE_HASH in one SEC in the program and trying to access that entry in another SEC. The key is defined as a struct.
struct map_key
{
__u16 key_1;
__u16 key_2;
}
struct map_value
{
__u16 value_1;
__u16 value_2;
}
struct
{
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, struct map_key);
__type(value, struct map_value);
__uint(max_entries, 1024);
} bpf_map SEC(".maps");
SEC("sec1")
int sec1_func(struct __sk_buff *skb)
{
struct map_key key = {0}
key.key1 = 10;
key.key2 = 20;
check = bpf_map_lookup_elem(&bpf_map, &key);
if (check == NULL) {
bpf_trace_printk("Trouble.", sizeof("Trouble"));
}
}
SEC("sec2")
{
struct map_key key = {0}
key.key1 = 10;
key.key2 = 20;
struct map_value value = {0}
value.value1 = 15
value.value2 = 25
bpf_map_update_elem(&map_key, &key, &value, BPF_NOEXIST);
}
sec2 is executed before sec1, so the map is definitely created before bpf_map_lookup_elem is called. But still, i'm getting null value returned from it when i call this function in sec1.
Upvotes: 0
Views: 787
Reputation: 135
So the fix is,
struct map_key
{
__u16 key_1;
__u16 key_2;
}
struct map_value
{
__u16 value_1;
__u16 value_2;
}
struct
{
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, struct map_key);
__type(value, struct map_value);
__uint(max_entries, 1024);
__uint(pinning, 1);
} bpf_map SEC(".maps");
SEC("sec1")
int sec1_func(struct __sk_buff *skb)
{
struct map_key key = {0}
key.key1 = 10;
key.key2 = 20;
check = bpf_map_lookup_elem(&bpf_map, &key);
if (check == NULL) {
bpf_trace_printk("Trouble.", sizeof("Trouble"));
}
}
SEC("sec2")
{
struct map_key key = {0}
key.key1 = 10;
key.key2 = 20;
struct map_value value = {0}
value.value1 = 15
value.value2 = 25
bpf_map_update_elem(&map_key, &key, &value, BPF_NOEXIST);
}
The additional pin field allows me to use the map in different elf sections within the same object file.
Upvotes: 1