Skip to contentSkip to conversation

bpf: adding support for mapinmap in libbpf

PATCH Superseded Nikita V. Shirokov 3 participants 8 messages Nov 20, 2018

Subsystems (6)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover bpf: adding support for mapinmap in libbpf

in this patch series i'm adding a helper for libbpf which would allow it to load map-in-map(BPF_MAP_TYPE_ARRAY_OF_MAPS and BPF_MAP_TYPE_HASH_OF_MAPS).
first patch contains new helper + explains proposed workflow second patch contains tests which also could be used as example of usage

Nikita V. Shirokov (2):
bpf: adding support for map in map in libbpf
bpf: adding tests for mapinmap helpber in libbpf

 tools/lib/bpf/libbpf.c                      |  7 +++
 tools/lib/bpf/libbpf.h                      |  2 +
 tools/testing/selftests/bpf/Makefile        |  3 +-
 tools/testing/selftests/bpf/test_mapinmap.c | 53 ++++++++++++++++++++
 tools/testing/selftests/bpf/test_maps.c     | 76 +++++++++++++++++++++++++++++
 5 files changed, 140 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/test_mapinmap.c
Signature · 1 line
2.15.1
#1 lore raw

PATCH 1/2 bpf: adding support for map in map in libbpf

idea is pretty simple. for specified map (pointed by struct bpf_map) we would provide descriptor of already loaded map, which is going to be used as a prototype for inner map. proposed workflow:
1) open bpf's object (bpf_object__open)
2) create bpf's map which is going to be used as a prototype
3) find (by name) map-in-map which you want to load and update w/ descriptor of inner map w/ a new helper from this patch
4) load bpf program w/ bpf_object__load

inner_map_fd is ignored by any other maps asidef from (hash|array) of maps

---
 tools/lib/bpf/libbpf.c | 7 +++++++
 tools/lib/bpf/libbpf.h | 2 ++
 2 files changed, 9 insertions(+)
tools/lib/bpf/libbpf.c +7 -0
index a01eb9584e52..a2ee1b1a93b6 100644
@@ -163,6 +163,7 @@ struct bpf_map {
 	char *name;
 	size_t offset;
 	int map_ifindex;
+	int inner_map_fd;
 	struct bpf_map_def def;
 	__u32 btf_key_type_id;
 	__u32 btf_value_type_id;
@@ -1146,6 +1147,7 @@ bpf_object__create_maps(struct bpf_object *obj)
 		create_attr.btf_fd = 0;
 		create_attr.btf_key_type_id = 0;
 		create_attr.btf_value_type_id = 0;
+		create_attr.inner_map_fd = map->inner_map_fd;
 
 		if (obj->btf && !bpf_map_find_btf_info(map, obj->btf)) {
 			create_attr.btf_fd = btf__fd(obj->btf);
@@ -2562,6 +2564,11 @@ void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex)
 	map->map_ifindex = ifindex;
 }
 
+void bpf_map__add_inner_map_fd(struct bpf_map *map, const int fd)
+{
+	map->inner_map_fd = fd;
+}
+
 static struct bpf_map *
 __bpf_map__iter(struct bpf_map *m, struct bpf_object *obj, int i)
 {
tools/lib/bpf/libbpf.h +2 -0
index b1686a787102..7cb00cd41789 100644
@@ -293,6 +293,8 @@ LIBBPF_API void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
 LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);
 LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path);
 
+LIBBPF_API void bpf_map__add_inner_map_fd(struct bpf_map *map, const int fd);
+
 LIBBPF_API long libbpf_get_error(const void *ptr);
 
 struct bpf_prog_load_attr {
Signature · 1 line
2.15.1
#2 lore raw

Re: [PATCH bpf-next 1/2] bpf: adding support for map in map in libbpf

Quoted 40 lines · Nikita V. Shirokov

idea is pretty simple. for specified map (pointed by struct bpf_map) we would provide descriptor of already loaded map, which is going to be used as a prototype for inner map. proposed workflow:
1) open bpf's object (bpf_object__open)
2) create bpf's map which is going to be used as a prototype
3) find (by name) map-in-map which you want to load and update w/ descriptor of inner map w/ a new helper from this patch
4) load bpf program w/ bpf_object__load

inner_map_fd is ignored by any other maps asidef from (hash|array) of maps

---
 tools/lib/bpf/libbpf.c | 7 +++++++
 tools/lib/bpf/libbpf.h | 2 ++
 2 files changed, 9 insertions(+)
tools/lib/bpf/libbpf.c +2 -0
index a01eb9584e52..a2ee1b1a93b6 100644
@@ -163,6 +163,7 @@ struct bpf_map {
        char *name;
        size_t offset;
        int map_ifindex;
+       int inner_map_fd;
        struct bpf_map_def def;
        __u32 btf_key_type_id;
        __u32 btf_value_type_id;
@@ -1146,6 +1147,7 @@ bpf_object__create_maps(struct bpf_object *obj)
                create_attr.btf_fd = 0;
                create_attr.btf_key_type_id = 0;
                create_attr.btf_value_type_id = 0;
+               create_attr.inner_map_fd = map->inner_map_fd;
 
                if (obj->btf && !bpf_map_find_btf_info(map, obj->btf)) {
                        create_attr.btf_fd = btf__fd(obj->btf);

tools/lib/bpf/libbpf.c void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex) · line 2567

Quoted 4 lines
tools/lib/bpf/libbpf.c +1 -0
@@ -2562,6 +2564,11 @@ void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex)
        map->map_ifindex = ifindex;
 }
 
+void bpf_map__add_inner_map_fd(struct bpf_map *map, const int fd)

Do we need "const" attribute here?

Quoted 23 lines
tools/lib/bpf/libbpf.c +4 -0
+{
+       map->inner_map_fd = fd;
+}
+
 static struct bpf_map *
 __bpf_map__iter(struct bpf_map *m, struct bpf_object *obj, int i)
 {
tools/lib/bpf/libbpf.h +2 -1
index b1686a787102..7cb00cd41789 100644
@@ -293,6 +293,8 @@ LIBBPF_API void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
 LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);
 LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path);
 
+LIBBPF_API void bpf_map__add_inner_map_fd(struct bpf_map *map, const int fd);
+
 LIBBPF_API long libbpf_get_error(const void *ptr);
 
 struct bpf_prog_load_attr {
--

2.15.1

#3 lore raw

Re: [PATCH bpf-next 1/2] bpf: adding support for map in map in libbpf

Quoted 44 lines · Y Song
Quoted 43 lines · Nikita V. Shirokov

idea is pretty simple. for specified map (pointed by struct bpf_map) we would provide descriptor of already loaded map, which is going to be used as a prototype for inner map. proposed workflow:
1) open bpf's object (bpf_object__open)
2) create bpf's map which is going to be used as a prototype
3) find (by name) map-in-map which you want to load and update w/ descriptor of inner map w/ a new helper from this patch
4) load bpf program w/ bpf_object__load

inner_map_fd is ignored by any other maps asidef from (hash|array) of maps

---
 tools/lib/bpf/libbpf.c | 7 +++++++
 tools/lib/bpf/libbpf.h | 2 ++
 2 files changed, 9 insertions(+)
tools/lib/bpf/libbpf.c +2 -0
index a01eb9584e52..a2ee1b1a93b6 100644
@@ -163,6 +163,7 @@ struct bpf_map {
        char *name;
        size_t offset;
        int map_ifindex;
+       int inner_map_fd;
        struct bpf_map_def def;
        __u32 btf_key_type_id;
        __u32 btf_value_type_id;
@@ -1146,6 +1147,7 @@ bpf_object__create_maps(struct bpf_object *obj)
                create_attr.btf_fd = 0;
                create_attr.btf_key_type_id = 0;
                create_attr.btf_value_type_id = 0;
+               create_attr.inner_map_fd = map->inner_map_fd;
 
                if (obj->btf && !bpf_map_find_btf_info(map, obj->btf)) {
                        create_attr.btf_fd = btf__fd(obj->btf);
@@ -2562,6 +2564,11 @@ void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex)
        map->map_ifindex = ifindex;
 }
 
Quoted 4 lines

tools/lib/bpf/libbpf.c void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex) · line 2567

Quoted 1 line
tools/lib/bpf/libbpf.c +1 -0
+void bpf_map__add_inner_map_fd(struct bpf_map *map, const int fd)

Do we need "const" attribute here?

i can drop it in v2

Quoted 23 lines
Quoted 23 lines
tools/lib/bpf/libbpf.c +4 -0
+{
+       map->inner_map_fd = fd;
+}
+
 static struct bpf_map *
 __bpf_map__iter(struct bpf_map *m, struct bpf_object *obj, int i)
 {
tools/lib/bpf/libbpf.h +2 -1
index b1686a787102..7cb00cd41789 100644
@@ -293,6 +293,8 @@ LIBBPF_API void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
 LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);
 LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path);
 
+LIBBPF_API void bpf_map__add_inner_map_fd(struct bpf_map *map, const int fd);
+
 LIBBPF_API long libbpf_get_error(const void *ptr);
 
 struct bpf_prog_load_attr {
--

2.15.1

#4 lore raw

Re: [PATCH bpf-next 1/2] bpf: adding support for map in map in libbpf

In reply to Nikita V. Shirokov
Quoted 10 lines · Nikita V. Shirokov

idea is pretty simple. for specified map (pointed by struct bpf_map) we would provide descriptor of already loaded map, which is going to be used as a prototype for inner map. proposed workflow:
1) open bpf's object (bpf_object__open)
2) create bpf's map which is going to be used as a prototype
3) find (by name) map-in-map which you want to load and update w/ descriptor of inner map w/ a new helper from this patch
4) load bpf program w/ bpf_object__load

inner_map_fd is ignored by any other maps asidef from (hash|array) of

"asidef" -> "aside"

#5 lore raw

PATCH 2/2 bpf: adding tests for mapinmap helpber in libbpf

In reply to Nikita V. Shirokov

adding test/example of bpf_map__add_inner_map_fd usage

---
 tools/testing/selftests/bpf/Makefile        |  3 +-
 tools/testing/selftests/bpf/test_mapinmap.c | 53 ++++++++++++++++++++
 tools/testing/selftests/bpf/test_maps.c     | 76 +++++++++++++++++++++++++++++
 3 files changed, 131 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/test_mapinmap.c
tools/testing/selftests/bpf/Makefile +2 -1
index 57b4712a6276..a3ea69dc9bdf 100644
@@ -38,7 +38,8 @@ TEST_GEN_FILES = test_pkt_access.o test_xdp.o test_l4lb.o test_tcp_estats.o test
 	test_lwt_seg6local.o sendmsg4_prog.o sendmsg6_prog.o test_lirc_mode2_kern.o \
 	get_cgroup_id_kern.o socket_cookie_prog.o test_select_reuseport_kern.o \
 	test_skb_cgroup_id_kern.o bpf_flow.o netcnt_prog.o \
-	test_sk_lookup_kern.o test_xdp_vlan.o test_queue_map.o test_stack_map.o
+	test_sk_lookup_kern.o test_xdp_vlan.o test_queue_map.o test_stack_map.o \
+	test_mapinmap.o
 
 # Order correspond to 'make run_tests' order
 TEST_PROGS := test_kmod.sh \
tools/testing/selftests/bpf/test_mapinmap.c new file +53 -0

53 lines not on this page · raw message

tools/testing/selftests/bpf/test_maps.c +76 -0

88 lines not on this page · raw message

Signature · 1 line
2.15.1
#6 lore raw

Re: [PATCH bpf-next 2/2] bpf: adding tests for mapinmap helpber in libbpf

Quoted 31 lines · Nikita V. Shirokov

adding test/example of bpf_map__add_inner_map_fd usage

---
 tools/testing/selftests/bpf/Makefile        |  3 +-
 tools/testing/selftests/bpf/test_mapinmap.c | 53 ++++++++++++++++++++
 tools/testing/selftests/bpf/test_maps.c     | 76 +++++++++++++++++++++++++++++
 3 files changed, 131 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/test_mapinmap.c
tools/testing/selftests/bpf/Makefile +2 -1
index 57b4712a6276..a3ea69dc9bdf 100644
@@ -38,7 +38,8 @@ TEST_GEN_FILES = test_pkt_access.o test_xdp.o test_l4lb.o test_tcp_estats.o test
        test_lwt_seg6local.o sendmsg4_prog.o sendmsg6_prog.o test_lirc_mode2_kern.o \
        get_cgroup_id_kern.o socket_cookie_prog.o test_select_reuseport_kern.o \
        test_skb_cgroup_id_kern.o bpf_flow.o netcnt_prog.o \
-       test_sk_lookup_kern.o test_xdp_vlan.o test_queue_map.o test_stack_map.o
+       test_sk_lookup_kern.o test_xdp_vlan.o test_queue_map.o test_stack_map.o \
+       test_mapinmap.o
 
 # Order correspond to 'make run_tests' order
 TEST_PROGS := test_kmod.sh \
tools/testing/selftests/bpf/test_mapinmap.c new file +0 -0
new file mode 100644
index 000000000000..8aef6c652c9c

tools/testing/selftests/bpf/test_mapinmap.c line 8

Quoted 8 lines
tools/testing/selftests/bpf/test_mapinmap.c new file +8 -0
@@ -0,0 +1,53 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2018 Facebook */
+#include <stddef.h>
+#include <linux/bpf.h>
+#include <linux/types.h>
+#include "bpf_helpers.h"
+
+

nit: extra new line

Quoted 10 lines
tools/testing/selftests/bpf/test_mapinmap.c new file +10 -0
+struct bpf_map_def SEC("maps") mim_array = {
+       .type = BPF_MAP_TYPE_ARRAY_OF_MAPS,
+       .key_size = sizeof(int),
+       /* must be sizeof(__u32) for map in map */
+       .value_size = sizeof(__u32),
+       .max_entries = 1,
+       .map_flags = 0,
+};
+
+struct bpf_map_def SEC("maps") mim_hash = {

tools/testing/selftests/bpf/test_mapinmap.c line 28

Quoted 10 lines
tools/testing/selftests/bpf/test_mapinmap.c new file +10 -0
+       .type = BPF_MAP_TYPE_HASH_OF_MAPS,
+       .key_size = sizeof(int),
+       /* must be sizeof(__u32) for map in map */
+       .value_size = sizeof(__u32),
+       .max_entries = 1,
+       .map_flags = 0,
+};
+
+
+

nit: extra new lines.

Quoted 13 lines
tools/testing/selftests/bpf/test_mapinmap.c new file +13 -0
+SEC("xdp_mimtest")
+int xdp_mimtest0(struct xdp_md *ctx)
+{
+       int value = 123;
+       int key = 0;
+       void *map;
+
+       map = bpf_map_lookup_elem(&mim_array, &key);
+       if (!map)
+               return XDP_DROP;
+
+       bpf_map_update_elem(map, &key, &value, 0);
+

tools/testing/selftests/bpf/test_mapinmap.c line 51

Quoted 10 lines
tools/testing/selftests/bpf/test_mapinmap.c new file +10 -0
+       map = bpf_map_lookup_elem(&mim_hash, &key);
+       if (!map)
+               return XDP_DROP;
+
+       bpf_map_update_elem(map, &key, &value, 0);
+
+       return XDP_PASS;
+}
+
+

nit: extra new line

Quoted 18 lines
tools/testing/selftests/bpf/test_mapinmap.c new file +2 -0
+int _version SEC("version") = 1;
+char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/test_maps.c +8 -0
index 4db2116e52be..a49ab294971d 100644
@@ -1080,6 +1080,80 @@ static void test_sockmap(int tasks, void *data)
        exit(1);
 }
 
+#define MAPINMAP_PROG "./test_mapinmap.o"
+static void test_mapinmap(void)
+{
+       struct bpf_program *prog;
+       struct bpf_object *obj;
+       struct bpf_map *map;
+       int mim_fd, fd;
+       int pos = 0;

tools/testing/selftests/bpf/test_maps.c static void test_sockmap(int tasks, void *data) · line 1100

Quoted 10 lines
tools/testing/selftests/bpf/test_maps.c +10 -0
+
+       obj = bpf_object__open(MAPINMAP_PROG);
+
+       fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(int), sizeof(int),
+                           2, 0);
+       if (fd < 0) {
+               printf("Failed to create hashmap '%s'!\n", strerror(errno));
+               exit(1);
+       }
+       printf("fd is %d\n", fd);

not sure whether you need to print fd in the console or not. Any particular reason?

Quoted 6 lines
tools/testing/selftests/bpf/test_maps.c +6 -0
+
+       map = bpf_object__find_map_by_name(obj, "mim_array");
+       if (IS_ERR(map)) {
+               printf("Failed to load array of maps from test prog\n");
+               goto out_mapinmap;
+       }

tools/testing/selftests/bpf/test_maps.c static void test_sockmap(int tasks, void *data) · line 1116

Quoted 10 lines
tools/testing/selftests/bpf/test_maps.c +10 -0
+       bpf_map__add_inner_map_fd(map, fd);
+
+       map = bpf_object__find_map_by_name(obj, "mim_hash");
+       if (IS_ERR(map)) {
+               printf("Failed to load hash of maps from test prog\n");
+               goto out_mapinmap;
+       }
+       bpf_map__add_inner_map_fd(map, fd);
+
+

nit: extra new line

Quoted 7 lines
tools/testing/selftests/bpf/test_maps.c +7 -0
+       bpf_object__for_each_program(prog, obj) {
+               bpf_program__set_xdp(prog);
+       }
+       bpf_object__load(obj);
+
+       map = bpf_object__find_map_by_name(obj, "mim_array");
+       if (IS_ERR(map)) {

tools/testing/selftests/bpf/test_maps.c static void test_sockmap(int tasks, void *data) · line 1133

Quoted 10 lines
tools/testing/selftests/bpf/test_maps.c +10 -0
+               printf("Failed to load array of maps from test prog\n");
+               goto out_mapinmap;
+       }
+       mim_fd = bpf_map__fd(map);
+       if (mim_fd < 0) {
+               printf("Failed to get descriptor for array of maps\n");
+               goto out_mapinmap;
+       }
+
+       bpf_map_update_elem(mim_fd, &pos, &fd, 0);

do you want to test return value of bpf_map_update_elem() to make sure update is successful?

tools/testing/selftests/bpf/test_maps.c static void test_sockmap(int tasks, void *data) · line 1145

Quoted 12 lines
tools/testing/selftests/bpf/test_maps.c +12 -0
+
+       map = bpf_object__find_map_by_name(obj, "mim_hash");
+       if (IS_ERR(map)) {
+               printf("Failed to load hash of maps from test prog\n");
+               goto out_mapinmap;
+       }
+       mim_fd = bpf_map__fd(map);
+       if (mim_fd < 0) {
+               printf("Failed to get descriptor for hash of maps\n");
+               goto out_mapinmap;
+       }
+       bpf_map_update_elem(mim_fd, &pos, &fd, 0);

do you want to test return value of bpf_map_update_elem()?

tools/testing/selftests/bpf/test_maps.c static void test_sockmap(int tasks, void *data) · line 1147

Quoted 2 lines
tools/testing/selftests/bpf/test_maps.c +2 -0
+
+

nit: extra new line

Quoted 24 lines
tools/testing/selftests/bpf/test_maps.c +11 -1
+       close(fd);
+       bpf_object__close(obj);
+       return;
+
+out_mapinmap:
+       close(fd);
+       exit(1);
+}
+
 #define MAP_SIZE (32 * 1024)
 
 static void test_map_large(void)
@@ -1554,6 +1628,8 @@ static void run_all_tests(void)
 
        test_queuemap(0, NULL);
        test_stackmap(0, NULL);
+
+       test_mapinmap();
 }
 
 int main(void)
--

2.15.1

#7 lore raw

Re: [PATCH bpf-next 2/2] bpf: adding tests for mapinmap helpber in libbpf

Quoted 109 lines · Y Song
Quoted 31 lines · Nikita V. Shirokov

adding test/example of bpf_map__add_inner_map_fd usage

---
 tools/testing/selftests/bpf/Makefile        |  3 +-
 tools/testing/selftests/bpf/test_mapinmap.c | 53 ++++++++++++++++++++
 tools/testing/selftests/bpf/test_maps.c     | 76 +++++++++++++++++++++++++++++
 3 files changed, 131 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/test_mapinmap.c
tools/testing/selftests/bpf/Makefile +2 -1
index 57b4712a6276..a3ea69dc9bdf 100644
@@ -38,7 +38,8 @@ TEST_GEN_FILES = test_pkt_access.o test_xdp.o test_l4lb.o test_tcp_estats.o test
        test_lwt_seg6local.o sendmsg4_prog.o sendmsg6_prog.o test_lirc_mode2_kern.o \
        get_cgroup_id_kern.o socket_cookie_prog.o test_select_reuseport_kern.o \
        test_skb_cgroup_id_kern.o bpf_flow.o netcnt_prog.o \
-       test_sk_lookup_kern.o test_xdp_vlan.o test_queue_map.o test_stack_map.o
+       test_sk_lookup_kern.o test_xdp_vlan.o test_queue_map.o test_stack_map.o \
+       test_mapinmap.o
 
 # Order correspond to 'make run_tests' order
 TEST_PROGS := test_kmod.sh \
tools/testing/selftests/bpf/test_mapinmap.c new file +0 -0
new file mode 100644
index 000000000000..8aef6c652c9c

tools/testing/selftests/bpf/test_mapinmap.c line 8

Quoted 8 lines
tools/testing/selftests/bpf/test_mapinmap.c new file +8 -0
@@ -0,0 +1,53 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2018 Facebook */
+#include <stddef.h>
+#include <linux/bpf.h>
+#include <linux/types.h>
+#include "bpf_helpers.h"
+
+

nit: extra new line

Quoted 10 lines
tools/testing/selftests/bpf/test_mapinmap.c new file +10 -0
+struct bpf_map_def SEC("maps") mim_array = {
+       .type = BPF_MAP_TYPE_ARRAY_OF_MAPS,
+       .key_size = sizeof(int),
+       /* must be sizeof(__u32) for map in map */
+       .value_size = sizeof(__u32),
+       .max_entries = 1,
+       .map_flags = 0,
+};
+
+struct bpf_map_def SEC("maps") mim_hash = {

tools/testing/selftests/bpf/test_mapinmap.c line 28

Quoted 10 lines
tools/testing/selftests/bpf/test_mapinmap.c new file +10 -0
+       .type = BPF_MAP_TYPE_HASH_OF_MAPS,
+       .key_size = sizeof(int),
+       /* must be sizeof(__u32) for map in map */
+       .value_size = sizeof(__u32),
+       .max_entries = 1,
+       .map_flags = 0,
+};
+
+
+

nit: extra new lines.

Quoted 13 lines
tools/testing/selftests/bpf/test_mapinmap.c new file +13 -0
+SEC("xdp_mimtest")
+int xdp_mimtest0(struct xdp_md *ctx)
+{
+       int value = 123;
+       int key = 0;
+       void *map;
+
+       map = bpf_map_lookup_elem(&mim_array, &key);
+       if (!map)
+               return XDP_DROP;
+
+       bpf_map_update_elem(map, &key, &value, 0);
+

tools/testing/selftests/bpf/test_mapinmap.c line 51

Quoted 10 lines
tools/testing/selftests/bpf/test_mapinmap.c new file +10 -0
+       map = bpf_map_lookup_elem(&mim_hash, &key);
+       if (!map)
+               return XDP_DROP;
+
+       bpf_map_update_elem(map, &key, &value, 0);
+
+       return XDP_PASS;
+}
+
+

nit: extra new line

Quoted 22 lines
tools/testing/selftests/bpf/test_mapinmap.c new file +2 -0
+int _version SEC("version") = 1;
+char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/test_maps.c +12 -0
index 4db2116e52be..a49ab294971d 100644
@@ -1080,6 +1080,80 @@ static void test_sockmap(int tasks, void *data)
        exit(1);
 }
 
+#define MAPINMAP_PROG "./test_mapinmap.o"
+static void test_mapinmap(void)
+{
+       struct bpf_program *prog;
+       struct bpf_object *obj;
+       struct bpf_map *map;
+       int mim_fd, fd;
+       int pos = 0;
+
+       obj = bpf_object__open(MAPINMAP_PROG);
+
+       fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(int), sizeof(int),
Quoted 8 lines

tools/testing/selftests/bpf/test_maps.c static void test_sockmap(int tasks, void *data) · line 1100

Quoted 6 lines
tools/testing/selftests/bpf/test_maps.c +6 -0
+                           2, 0);
+       if (fd < 0) {
+               printf("Failed to create hashmap '%s'!\n", strerror(errno));
+               exit(1);
+       }
+       printf("fd is %d\n", fd);

not sure whether you need to print fd in the console or not. Any particular reason?

was using for debugging. forget to remove.

Quoted 28 lines
Quoted 6 lines
tools/testing/selftests/bpf/test_maps.c +6 -0
+
+       map = bpf_object__find_map_by_name(obj, "mim_array");
+       if (IS_ERR(map)) {
+               printf("Failed to load array of maps from test prog\n");
+               goto out_mapinmap;
+       }

tools/testing/selftests/bpf/test_maps.c static void test_sockmap(int tasks, void *data) · line 1116

Quoted 10 lines
tools/testing/selftests/bpf/test_maps.c +10 -0
+       bpf_map__add_inner_map_fd(map, fd);
+
+       map = bpf_object__find_map_by_name(obj, "mim_hash");
+       if (IS_ERR(map)) {
+               printf("Failed to load hash of maps from test prog\n");
+               goto out_mapinmap;
+       }
+       bpf_map__add_inner_map_fd(map, fd);
+
+

nit: extra new line

Quoted 11 lines
tools/testing/selftests/bpf/test_maps.c +11 -0
+       bpf_object__for_each_program(prog, obj) {
+               bpf_program__set_xdp(prog);
+       }
+       bpf_object__load(obj);
+
+       map = bpf_object__find_map_by_name(obj, "mim_array");
+       if (IS_ERR(map)) {
+               printf("Failed to load array of maps from test prog\n");
+               goto out_mapinmap;
+       }
+       mim_fd = bpf_map__fd(map);
Quoted 8 lines

tools/testing/selftests/bpf/test_maps.c static void test_sockmap(int tasks, void *data) · line 1133

Quoted 6 lines
tools/testing/selftests/bpf/test_maps.c +6 -0
+       if (mim_fd < 0) {
+               printf("Failed to get descriptor for array of maps\n");
+               goto out_mapinmap;
+       }
+
+       bpf_map_update_elem(mim_fd, &pos, &fd, 0);

do you want to test return value of bpf_map_update_elem() to make sure update is successful?

sure.

Quoted 40 lines

tools/testing/selftests/bpf/test_maps.c static void test_sockmap(int tasks, void *data) · line 1145

Quoted 12 lines
tools/testing/selftests/bpf/test_maps.c +12 -0
+
+       map = bpf_object__find_map_by_name(obj, "mim_hash");
+       if (IS_ERR(map)) {
+               printf("Failed to load hash of maps from test prog\n");
+               goto out_mapinmap;
+       }
+       mim_fd = bpf_map__fd(map);
+       if (mim_fd < 0) {
+               printf("Failed to get descriptor for hash of maps\n");
+               goto out_mapinmap;
+       }
+       bpf_map_update_elem(mim_fd, &pos, &fd, 0);

do you want to test return value of bpf_map_update_elem()?

tools/testing/selftests/bpf/test_maps.c static void test_sockmap(int tasks, void *data) · line 1147

Quoted 2 lines
tools/testing/selftests/bpf/test_maps.c +2 -0
+
+

nit: extra new line

Quoted 24 lines
tools/testing/selftests/bpf/test_maps.c +11 -1
+       close(fd);
+       bpf_object__close(obj);
+       return;
+
+out_mapinmap:
+       close(fd);
+       exit(1);
+}
+
 #define MAP_SIZE (32 * 1024)
 
 static void test_map_large(void)
@@ -1554,6 +1628,8 @@ static void run_all_tests(void)
 
        test_queuemap(0, NULL);
        test_stackmap(0, NULL);
+
+       test_mapinmap();
 }
 
 int main(void)
--

2.15.1