From patchwork Wed Nov 21 04:55:56 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: 1000916 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="Eho6+zKE"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 4309JT1HgSz9s4s for ; Wed, 21 Nov 2018 15:56:09 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727396AbeKUP24 (ORCPT ); Wed, 21 Nov 2018 10:28:56 -0500 Received: from mail-it1-f195.google.com ([209.85.166.195]:39765 "EHLO mail-it1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725939AbeKUP2z (ORCPT ); Wed, 21 Nov 2018 10:28:55 -0500 Received: by mail-it1-f195.google.com with SMTP id m15so7116695itl.4 for ; Tue, 20 Nov 2018 20:56:05 -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=F3mtU/jqTySaISh5BDiQfPaV9h2+3X8V48RDgLq+vRs=; b=Eho6+zKE24tWkazjoa/JEXfAxMeSOsGsbNtDwNLopGhFqOBN/jZvgwLSuVGyodXBE5 EdWRb3PxlzsjzLWVAtRrFGFuqwPh+aHH/0S5CfBuct1M/k7fihXiUSrpFn2kXdaGgANQ TqRtBlfOHttdGWSzzuO42HmYFBcbxuh2DR08YryuTYbiDJIavrnrdzGoQODfGyhnGMdC Kb19LcgQ4FzNqPD/5qU35T4oGhr7uxooA2P3Ux6n0Cz9hfLOPNRgAe+xKhDFE4WYc2e9 UN80hAvtOQxTNvBoVeO2Y0EBLCMV4Z2SLWUwCdvJ4IrW53m/eP3sVEy3lOLOTgAH2TF9 ZKZQ== 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=F3mtU/jqTySaISh5BDiQfPaV9h2+3X8V48RDgLq+vRs=; b=THew92+vOgUA/t3tUistCAtGu2GVNmRGAzAXMtwjZNPtsZeVdXQD+haT/cLBEMLjpy Aqd0mjp0I0dKjsTRU+T/i2BJ7ICoc6zQtN9H+17/2/mPjDeZUSgb9s2O5Ir+7PIduvlg xUutjf+4tuILFJoC3XTtbNBnkxw+s6ORC1LNAdkICL+ZCuQokbw38le7gvvecO7FyYv7 6+A/2pPR+UNSaHMdhLnPKrKQcn6rTvpugxL5ZEiBlM+550bSPsed8LKKCaOvBuTlG18/ tkh/h5+8jRIWtPBsnSFcOwX+bwlNGkLqypJKFuzn9MNuOXayePCrdOsQqymFKXKGRaZn o0FQ== X-Gm-Message-State: AA+aEWaHUhTxZekr/vXTEtqsS4y2m6y12mkIaMK/+ePrzMaI7/Wy4Ufi v3l4YDdqmOWz5hrsSS9eb8xjdw== X-Google-Smtp-Source: AJdET5e5SbHrgU2af9BdyLUyTCkEB/6sRZ9cXpohqV6dRyx3g/26T0494mNwpAYFSPUq+z/YfCFzwA== X-Received: by 2002:a24:940f:: with SMTP id j15-v6mr4641921ite.12.1542776165447; Tue, 20 Nov 2018 20:56:05 -0800 (PST) Received: from maindev.thefacebook.com ([199.201.64.4]) by smtp.gmail.com with ESMTPSA id r18sm5376193ita.6.2018.11.20.20.56.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 20 Nov 2018 20:56:04 -0800 (PST) From: "Nikita V. Shirokov" To: Alexei Starovoitov , Daniel Borkmann , Jakub Kicinski Cc: netdev@vger.kernel.org, "Nikita V. Shirokov" Subject: [PATCH v5 bpf-next 1/2] bpf: adding support for map in map in libbpf Date: Tue, 20 Nov 2018 20:55:56 -0800 Message-Id: <20181121045557.17924-2-tehnerd@tehnerd.com> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20181121045557.17924-1-tehnerd@tehnerd.com> References: <20181121045557.17924-1-tehnerd@tehnerd.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org 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 Signed-off-by: Nikita V. Shirokov Acked-by: Yonghong Song --- tools/lib/bpf/libbpf.c | 40 ++++++++++++++++++++++++++++++++++------ tools/lib/bpf/libbpf.h | 2 ++ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index cb6565d79603..ba12e070f182 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -167,6 +167,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; @@ -594,6 +595,14 @@ static int compare_bpf_map(const void *_a, const void *_b) return a->offset - b->offset; } +static bool bpf_map_type__is_map_in_map(enum bpf_map_type type) +{ + if (type == BPF_MAP_TYPE_ARRAY_OF_MAPS || + type == BPF_MAP_TYPE_HASH_OF_MAPS) + return true; + return false; +} + static int bpf_object__init_maps(struct bpf_object *obj, int flags) { @@ -657,13 +666,15 @@ bpf_object__init_maps(struct bpf_object *obj, int flags) } obj->nr_maps = nr_maps; - /* - * fill all fd with -1 so won't close incorrect - * fd (fd=0 is stdin) when failure (zclose won't close - * negative fd)). - */ - for (i = 0; i < nr_maps; i++) + for (i = 0; i < nr_maps; i++) { + /* + * fill all fd with -1 so won't close incorrect + * fd (fd=0 is stdin) when failure (zclose won't close + * negative fd)). + */ obj->maps[i].fd = -1; + obj->maps[i].inner_map_fd = -1; + } /* * Fill obj->maps using data in "maps" section. @@ -1164,6 +1175,9 @@ 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; + if (bpf_map_type__is_map_in_map(def->type) && + map->inner_map_fd >= 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); @@ -2621,6 +2635,20 @@ void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex) map->map_ifindex = ifindex; } +int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd) +{ + if (!bpf_map_type__is_map_in_map(map->def.type)) { + pr_warning("error: unsupported map type\n"); + return -EINVAL; + } + if (map->inner_map_fd != -1) { + pr_warning("error: inner_map_fd already specified\n"); + return -EINVAL; + } + map->inner_map_fd = fd; + return 0; +} + static struct bpf_map * __bpf_map__iter(struct bpf_map *m, struct bpf_object *obj, int i) { diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index b1686a787102..16158b6b213f 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -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 int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd); + LIBBPF_API long libbpf_get_error(const void *ptr); struct bpf_prog_load_attr { From patchwork Wed Nov 21 04:55:57 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: 1000917 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="QqfYIeek"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 4309JV5M49z9s3x for ; Wed, 21 Nov 2018 15:56:10 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727438AbeKUP26 (ORCPT ); Wed, 21 Nov 2018 10:28:58 -0500 Received: from mail-it1-f195.google.com ([209.85.166.195]:35306 "EHLO mail-it1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725939AbeKUP25 (ORCPT ); Wed, 21 Nov 2018 10:28:57 -0500 Received: by mail-it1-f195.google.com with SMTP id v11so7156581itj.0 for ; Tue, 20 Nov 2018 20:56:07 -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=B08xHZi8koiJ40FPk/GeF9RBDJ8IN4tqXOdmTmPQo3o=; b=QqfYIeeklRzXLThzFzabb6EHnaefhBohYupJU3pg/WYWHFvBNzU+vRDE8FEx+GgZWN ZjQpC/YjXfd2tKG8lHNoRbEcswfbcU0hlOPQ96DjVYY9fDMC0ubfx2Fm5ZQMffeWYVHS sH1S57t3unUtdQX687uLpcgcln1wK43Ibhv+YIufUsxaqT1TPyO4xrNWOh/GrW+MJMx8 KveU5/u0Hjg8VQpa6gUWqPgOLx1KFA/Dr+mVcHSzcGub8bJzslJ7mCG62uDMx9iprZ9Q GfZiz9kSdsJJPgpLzFn8kuzWJdVMqwUbtbdQZNqvTXlWkGsU62yAwybXyFHvHxZD6un+ BYpw== 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=B08xHZi8koiJ40FPk/GeF9RBDJ8IN4tqXOdmTmPQo3o=; b=mZOUdVUQEr/aJRM/xev0shb2EPr9Us1HOsUf1eFz8zI88EYG+i9ANLspPGnRXtZLCg t7W70fpe7GjNBipLt+MiGC3NrWLP063X8jANCvUGlMW6AFgwKce9BoUE7ctcGfpZNqQ4 FdUdfWe8NIeJSuPiQlsNJnC306krTOIZ/ftX2EgtU2jpL+6fWPgcGiKsznhnbkvEnMRx 1LnCAtaO4k9UAcM8kxX5QwBtDkq6C/EDLh6ROdrKQw1V1cw8P/kB3A3gGMBJYMBf5Q/e gVRK90sBUrdx1DfOEvuwdD0aaYHxDPjL1NDHKZyzLecv8x8obNGg3UMdZTReD61cAdaY vzlQ== X-Gm-Message-State: AA+aEWboN3dE2pa29LGF+LKOVD6wODSI/BCjHa/DF36JneRaUFvEROwE GpSkaMFrlb7kXKNYVd/H1gAnOIE67pU= X-Google-Smtp-Source: AJdET5cQoO+eFXBpVXoEBBHcXcIYzlmluzN+y+iw7txmnMsLh1DFXjZoPIVqvmQtdD657YtU8FvsPQ== X-Received: by 2002:a02:98d2:: with SMTP id c18mr3985974jak.11.1542776166951; Tue, 20 Nov 2018 20:56:06 -0800 (PST) Received: from maindev.thefacebook.com ([199.201.64.4]) by smtp.gmail.com with ESMTPSA id r18sm5376193ita.6.2018.11.20.20.56.05 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 20 Nov 2018 20:56:06 -0800 (PST) From: "Nikita V. Shirokov" To: Alexei Starovoitov , Daniel Borkmann , Jakub Kicinski Cc: netdev@vger.kernel.org, "Nikita V. Shirokov" Subject: [PATCH v5 bpf-next 2/2] bpf: adding tests for map_in_map helpber in libbpf Date: Tue, 20 Nov 2018 20:55:57 -0800 Message-Id: <20181121045557.17924-3-tehnerd@tehnerd.com> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20181121045557.17924-1-tehnerd@tehnerd.com> References: <20181121045557.17924-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__set_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_map_in_map.c | 49 +++++++++++++++ tools/testing/selftests/bpf/test_maps.c | 90 +++++++++++++++++++++++++++ 3 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/test_map_in_map.c diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile index 1dde03ea1484..43157bd89165 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_map_in_map.o # Order correspond to 'make run_tests' order TEST_PROGS := test_kmod.sh \ diff --git a/tools/testing/selftests/bpf/test_map_in_map.c b/tools/testing/selftests/bpf/test_map_in_map.c new file mode 100644 index 000000000000..ce923e67e08e --- /dev/null +++ b/tools/testing/selftests/bpf/test_map_in_map.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 9f0a5b16a246..9c79ee017df3 100644 --- a/tools/testing/selftests/bpf/test_maps.c +++ b/tools/testing/selftests/bpf/test_maps.c @@ -1125,6 +1125,94 @@ static void test_sockmap(int tasks, void *data) exit(1); } +#define MAPINMAP_PROG "./test_map_in_map.o" +static void test_map_in_map(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_map_in_map; + } + err = bpf_map__set_inner_map_fd(map, fd); + if (err) { + printf("Failed to set inner_map_fd for array of maps\n"); + goto out_map_in_map; + } + + 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_map_in_map; + } + err = bpf_map__set_inner_map_fd(map, fd); + if (err) { + printf("Failed to set inner_map_fd for hash of maps\n"); + goto out_map_in_map; + } + + 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_map_in_map; + } + mim_fd = bpf_map__fd(map); + if (mim_fd < 0) { + printf("Failed to get descriptor for array of maps\n"); + goto out_map_in_map; + } + + err = bpf_map_update_elem(mim_fd, &pos, &fd, 0); + if (err) { + printf("Failed to update array of maps\n"); + goto out_map_in_map; + } + + 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_map_in_map; + } + mim_fd = bpf_map__fd(map); + if (mim_fd < 0) { + printf("Failed to get descriptor for hash of maps\n"); + goto out_map_in_map; + } + + err = bpf_map_update_elem(mim_fd, &pos, &fd, 0); + if (err) { + printf("Failed to update hash of maps\n"); + goto out_map_in_map; + } + + close(fd); + bpf_object__close(obj); + return; + +out_map_in_map: + close(fd); + exit(1); +} + #define MAP_SIZE (32 * 1024) static void test_map_large(void) @@ -1600,6 +1688,8 @@ static void run_all_tests(void) test_queuemap(0, NULL); test_stackmap(0, NULL); + + test_map_in_map(); } int main(void)