当键为结构时,c-bpf_map_lookup_elem不工作



我正在程序的一个SEC中创建一个类型为BPF_map_type_HASH的映射条目,并试图在另一个SEC访问该条目。键被定义为结构。

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在sec1之前执行,因此映射肯定是在调用bpf_map_lookup_elem之前创建的。但是,当我在sec1中调用这个函数时,我仍然得到了从中返回的null值。

所以修复方法是,

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);
}

额外的pin字段允许我在同一个对象文件中的不同elf部分中使用地图。

相关内容

  • 没有找到相关文章

最新更新