From patchwork Tue Nov 20 06:42:22 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nikita V. Shirokov" X-Patchwork-Id: 1000692 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=tehnerd.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=tehnerd-com.20150623.gappssmtp.com header.i=@tehnerd-com.20150623.gappssmtp.com header.b="YjWFfh1s"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 42zxpK6RNRz9s4s for ; Wed, 21 Nov 2018 07:17:45 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726183AbeKUGsj (ORCPT ); Wed, 21 Nov 2018 01:48:39 -0500 Received: from mail-io1-f67.google.com ([209.85.166.67]:46444 "EHLO mail-io1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725865AbeKUGsi (ORCPT ); Wed, 21 Nov 2018 01:48:38 -0500 Received: by mail-io1-f67.google.com with SMTP id m1-v6so2329577ioc.13 for ; Tue, 20 Nov 2018 12:17:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tehnerd-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=lfdIa97m5hV5YLKwtEsR7zvcxhVNs5mw75D+cn30AFs=; b=YjWFfh1spHM8IqT2eajZevbqGS0hkJLg6BJ5nKTyc7ltO4N2+GObz+kcsMdXZXS7e0 O0cH0t/tt8MWK0Hb43ZZ1UvlXDKOQVp2ADlwe3kUxLP430Ls+abpeWtTQhBBe/IW1ffm 72kyvmKMx0iMi/v3MWxmyTBWvutzCQSYIqLoN+ZR7gsA5Qynzf/yKcPX/oYAaxm1L/S3 MdirrXCm3YguEtlNlMQFJxfE2Ozj0kqSw9EKd+Nww7GyDcalnD/SW4Jg7DychrVLynOc BMB605JnNIShmaGouR7d/ffpGYIGhdhucXtGINCFYRbJsBfnnvGZny4u830y5Qi99UH+ 1jGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=lfdIa97m5hV5YLKwtEsR7zvcxhVNs5mw75D+cn30AFs=; b=rqhEe8HyQN9SaaNr53E5qmozZJklWTVLjVIooR7IRjE2vUO+8DwRmDrHeCwwyFdJ0E b6FAfnz+YBg5LUVIPlK1RFbSpqxVey9JFf9tfmDgoyt0d2dkHgUElzFUwT6wnnhQKXgW Ssh4c570R/cOF2n5pxQfeVpJeL4N0FTqtsphdVjL+9Y+WL0V6E6okxP5uggtD3B5FEds XhRM9eYDIj7M2Cs2qI09eRgIYfirI473Q9cC15vHlNJEvrrOfJaHJWKsERA5UJZ5y2YA G1UQml+N8feMVFU37BcmDKMejN/ap1aKgbfXlZ2btPw6bGP3xvCjPBot72X0vCeNOysw quYw== X-Gm-Message-State: AA+aEWbqsCqbi2ViI4XLo8M9cLkzEktRzFCJGw4G6IbmDLaITZwMvfnm 25VuHaQIJ1BeTZwdS6Yv+kC99g== X-Google-Smtp-Source: AFSGD/VyqtJAL2aJ1/KnfCfuhUgPkmV+9QtFWwoR3yZ/Ti54JPJiovoLTY8hFBVUqvlVnilzz9LStg== X-Received: by 2002:a6b:f611:: with SMTP id n17-v6mr2277821ioh.99.1542745063023; Tue, 20 Nov 2018 12:17:43 -0800 (PST) Received: from maindev.thefacebook.com ([199.201.64.4]) by smtp.gmail.com with ESMTPSA id z186-v6sm14042000itd.43.2018.11.20.12.17.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 20 Nov 2018 12:17:42 -0800 (PST) From: "Nikita V. Shirokov" To: Alexei Starovoitov , Daniel Borkmann , Jakub Kicinski Cc: netdev@vger.kernel.org, "Nikita V. Shirokov" Subject: [PATCH v3 bpf-next 2/2] bpf: adding tests for mapinmap helpber in libbpf Date: Mon, 19 Nov 2018 22:42:22 -0800 Message-Id: <20181120064222.5606-3-tehnerd@tehnerd.com> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20181120064222.5606-1-tehnerd@tehnerd.com> References: <20181120064222.5606-1-tehnerd@tehnerd.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org adding test/example of bpf_map__add_inner_map_fd usage Signed-off-by: Nikita V. Shirokov Acked-by: Yonghong Song --- tools/testing/selftests/bpf/Makefile | 3 +- tools/testing/selftests/bpf/test_mapinmap.c | 49 +++++++++++++++++ tools/testing/selftests/bpf/test_maps.c | 82 +++++++++++++++++++++++++++++ 3 files changed, 133 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 000000000000..ce923e67e08e --- /dev/null +++ b/tools/testing/selftests/bpf/test_mapinmap.c @@ -0,0 +1,49 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2018 Facebook */ +#include +#include +#include +#include "bpf_helpers.h" + +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, +}; + +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); + + map = bpf_map_lookup_elem(&mim_hash, &key); + if (!map) + return XDP_DROP; + + bpf_map_update_elem(map, &key, &value, 0); + + return XDP_PASS; +} + +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..6bbcb3ba5787 100644 --- a/tools/testing/selftests/bpf/test_maps.c +++ b/tools/testing/selftests/bpf/test_maps.c @@ -1080,6 +1080,86 @@ 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, err; + 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); + } + + 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); + + 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 for array of maps\n"); + goto out_mapinmap; + } + + err = bpf_map_update_elem(mim_fd, &pos, &fd, 0); + if (err) { + printf("Failed to update array of maps\n"); + goto out_mapinmap; + } + + 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; + } + + err = bpf_map_update_elem(mim_fd, &pos, &fd, 0); + if (err) { + printf("Failed to update hash of maps\n"); + goto out_mapinmap; + } + + 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 +1634,8 @@ static void run_all_tests(void) test_queuemap(0, NULL); test_stackmap(0, NULL); + + test_mapinmap(); } int main(void)