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

2018-11-19 Thread Nikita V. Shirokov
O Mon, Nov 19, 2018 at 05:18:46PM -0800, Y Song wrote:
> On Mon, Nov 19, 2018 at 4:13 PM Nikita V. Shirokov  
> wrote:
> >
> > adding test/example of bpf_map__add_inner_map_fd usage
> >
> > Signed-off-by: Nikita V. Shirokov 
> > ---
> >  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
> >
> > diff --git a/tools/testing/selftests/bpf/Makefile 
> > b/tools/testing/selftests/bpf/Makefile
> > index 57b4712a6276..a3ea69dc9bdf 100644
> > --- a/tools/testing/selftests/bpf/Makefile
> > +++ b/tools/testing/selftests/bpf/Makefile
> > @@ -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 \
> > diff --git a/tools/testing/selftests/bpf/test_mapinmap.c 
> > b/tools/testing/selftests/bpf/test_mapinmap.c
> > new file mode 100644
> > index ..8aef6c652c9c
> > --- /dev/null
> > +++ b/tools/testing/selftests/bpf/test_mapinmap.c
> > @@ -0,0 +1,53 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +/* Copyright (c) 2018 Facebook */
> > +#include 
> > +#include 
> > +#include 
> > +#include "bpf_helpers.h"
> > +
> > +
> nit: extra new line
> 
> > +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 = {
> > +   .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.
> > +SEC("xdp_mimtest")
> > +int xdp_mimtest0(struct xdp_md *ctx)
> > +{
> > +   int value = 123;
> > +   int key = 0;
> > +   void *map;
> > +
> > +   map = bpf_map_lookup_elem(_array, );
> > +   if (!map)
> > +   return XDP_DROP;
> > +
> > +   bpf_map_update_elem(map, , , 0);
> > +
> > +   map = bpf_map_lookup_elem(_hash, );
> > +   if (!map)
> > +   return XDP_DROP;
> > +
> > +   bpf_map_update_elem(map, , , 0);
> > +
> > +   return XDP_PASS;
> > +}
> > +
> > +
> nit: extra new line
> > +int _version SEC("version") = 1;
> > +char _license[] SEC("license") = "GPL";
> > diff --git a/tools/testing/selftests/bpf/test_maps.c 
> > b/tools/testing/selftests/bpf/test_maps.c
> > index 4db2116e52be..a49ab294971d 100644
> > --- a/tools/testing/selftests/bpf/test_maps.c
> > +++ b/tools/testing/selftests/bpf/test_maps.c
> > @@ -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),
> > +   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.

> > +
> > +   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;
> > +   }
> > +   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
> > +   bpf_object__for_each_program(prog, obj) {
> > +   

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

2018-11-19 Thread Y Song
On Mon, Nov 19, 2018 at 4:13 PM Nikita V. Shirokov  wrote:
>
> adding test/example of bpf_map__add_inner_map_fd usage
>
> Signed-off-by: Nikita V. Shirokov 
> ---
>  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
>
> diff --git a/tools/testing/selftests/bpf/Makefile 
> b/tools/testing/selftests/bpf/Makefile
> index 57b4712a6276..a3ea69dc9bdf 100644
> --- a/tools/testing/selftests/bpf/Makefile
> +++ b/tools/testing/selftests/bpf/Makefile
> @@ -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 \
> diff --git a/tools/testing/selftests/bpf/test_mapinmap.c 
> b/tools/testing/selftests/bpf/test_mapinmap.c
> new file mode 100644
> index ..8aef6c652c9c
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/test_mapinmap.c
> @@ -0,0 +1,53 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/* Copyright (c) 2018 Facebook */
> +#include 
> +#include 
> +#include 
> +#include "bpf_helpers.h"
> +
> +
nit: extra new line

> +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 = {
> +   .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.
> +SEC("xdp_mimtest")
> +int xdp_mimtest0(struct xdp_md *ctx)
> +{
> +   int value = 123;
> +   int key = 0;
> +   void *map;
> +
> +   map = bpf_map_lookup_elem(_array, );
> +   if (!map)
> +   return XDP_DROP;
> +
> +   bpf_map_update_elem(map, , , 0);
> +
> +   map = bpf_map_lookup_elem(_hash, );
> +   if (!map)
> +   return XDP_DROP;
> +
> +   bpf_map_update_elem(map, , , 0);
> +
> +   return XDP_PASS;
> +}
> +
> +
nit: extra new line
> +int _version SEC("version") = 1;
> +char _license[] SEC("license") = "GPL";
> diff --git a/tools/testing/selftests/bpf/test_maps.c 
> b/tools/testing/selftests/bpf/test_maps.c
> index 4db2116e52be..a49ab294971d 100644
> --- a/tools/testing/selftests/bpf/test_maps.c
> +++ b/tools/testing/selftests/bpf/test_maps.c
> @@ -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),
> +   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?
> +
> +   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;
> +   }
> +   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
> +   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);
> +   if (mim_fd < 0) {
> +   printf("Failed to get descriptor