This artical will talk about how to create map-in-map program.

  1. We need to create a outer map with type BPF_MAP_TYPE_ARRAY_OF_MAPS or BPF_MAP_TYPE_HASH_OF_MAPS.

MAX_ENTRIES = 4

struct bpf_map_def SEC(“maps”) inner_map = { .type = BPF_MAP_TYPE_ARRAY, .key_size = sizeof(u32), .value_size = sizeof(int), .max_entries = MAX_ENTRIES, };

struct bpf_map_def SEC(“maps”) outer_map = { .type = BPF_MAP_TYPE_ARRAY_OF_MAPS, .key_size = sizeof(u32), .value_size = sizeof(u32), .max_entries = MAX_ENTRIES, };

  1. In userspace program, we need:

struct bpf_object_open_attr obj_open_attr = { .prog_type = BPF_PROG_TYPE_XDP, }; obj = bpf_object__open_xattr(&obj_open_attr);

//inner_map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(__u32), sizeof(int), MAX_ENTRIES, 0); inner_map_fd = bpf_object__find_map_fd_by_name(obj, “inner_map”); outer_map = bpf_object__find_map_by_name(obj, “outer_map”); bpf_map__set_inner_map_fd(outer_map, inmap_fd);

bpf_object__load(obj); prog = bpf_program__next(NULL, obj);