From patchwork Mon Nov 23 13:11:57 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hangbin Liu X-Patchwork-Id: 11925165 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 564C1C64E7A for ; Mon, 23 Nov 2020 13:14:10 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 04ADB2075A for ; Mon, 23 Nov 2020 13:14:09 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="NVm+mnVn" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388498AbgKWNN0 (ORCPT ); Mon, 23 Nov 2020 08:13:26 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:30216 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388493AbgKWNNZ (ORCPT ); Mon, 23 Nov 2020 08:13:25 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1606137203; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IsLM1UDMLXmRkRLkuy+KFrXPGu3beCudF+xPd0mVg1s=; b=NVm+mnVnxCOmYsLpsIyplsZ9+RneojwTnOAT9gCeMQ6yp8HiOXJaysToS2Rv8bnUlCv4Vw yH1cy1RNBXhIXroE+bzwsYyC9DtpGFlMlTPk2+6DuBZsQkyJK/jqCvVNknmSFB/8Ra75gX jCISRu1m8brnFBwcYGdv7RYPB/HIxwo= Received: from mail-pg1-f199.google.com (mail-pg1-f199.google.com [209.85.215.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-282-lys2TwcsNxmmETCw6uZZsw-1; Mon, 23 Nov 2020 08:13:21 -0500 X-MC-Unique: lys2TwcsNxmmETCw6uZZsw-1 Received: by mail-pg1-f199.google.com with SMTP id u26so1469841pgl.15 for ; Mon, 23 Nov 2020 05:13:20 -0800 (PST) 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:mime-version:content-transfer-encoding; bh=IsLM1UDMLXmRkRLkuy+KFrXPGu3beCudF+xPd0mVg1s=; b=Pl/qjpxRopjVHmPFL2kE5M3H5Xx8ro0IBVKamyDxhu3ZEqZSlKCSqgOa0gbjzdg1Au cxREBSwoPi6inVxRZo6oobTuVtWZ5KG2lAlvAFoWHjkhaIdSGzmWRWsC4HmkcTIjLxt4 /rVNbyvhtTZJTqKC6REKIljD5vyDJA+USV1VqL7JjgPpEGuudRMT9nj0Wi6MkvlLFF1d IrYHJjVt3xqXjlTo3ek9TULrEbHNOrudpwKg3aCmNze32Lpi9nDwAWVC5P8tNCIdSSTf cRtcS2WtmVwvTQu0YGZgbbM1XNLSHzNrVEUxiR8isG/pr1yetdbw4cnFX5lxhLMzfb0u wHSw== X-Gm-Message-State: AOAM531Gnwra/tVfZOu4xXdXbhZ7aqkNCL2XgWRdz2meduz7IudBGr2p R8s+BWKO+kfxutOSMglTvw5Y3CDSXKRnfgenXNJD0CwbuHIDzD78Ytv//WQIBwoRhZ9DLIqTQC6 2Hl95DT8A21g= X-Received: by 2002:a63:ff03:: with SMTP id k3mr27087344pgi.304.1606137199649; Mon, 23 Nov 2020 05:13:19 -0800 (PST) X-Google-Smtp-Source: ABdhPJwGUkrVhRlwnNPySOSVHrOOtkW9IHSay4gtS8Hbby0hAFRIWJnYTKS/RMZG4JY3FlemOcZa1w== X-Received: by 2002:a63:ff03:: with SMTP id k3mr27087319pgi.304.1606137199304; Mon, 23 Nov 2020 05:13:19 -0800 (PST) Received: from localhost.localdomain.com ([209.132.188.80]) by smtp.gmail.com with ESMTPSA id 84sm12075505pfu.53.2020.11.23.05.13.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Nov 2020 05:13:18 -0800 (PST) From: Hangbin Liu To: Stephen Hemminger , David Ahern Cc: Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , David Miller , netdev@vger.kernel.org, bpf@vger.kernel.org, Jiri Benc , =?utf-8?q?Toke_H=C3=B8il?= =?utf-8?q?and-J=C3=B8rgensen?= , Jesper Dangaard Brouer , Alexei Starovoitov , Hangbin Liu Subject: [PATCHv6 iproute2-next 1/5] iproute2: add check_libbpf() and get_libbpf_version() Date: Mon, 23 Nov 2020 21:11:57 +0800 Message-Id: <20201123131201.4108483-2-haliu@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: <20201123131201.4108483-1-haliu@redhat.com> References: <20201116065305.1010651-1-haliu@redhat.com> <20201123131201.4108483-1-haliu@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: dsahern@gmail.com This patch aim to add basic checking functions for later iproute2 libbpf support. First we add check_libbpf() in configure to see if we have bpf library support. By default the system libbpf will be used, but static linking against a custom libbpf version can be achieved by passing libbpf DESTDIR to variable LIBBPF_DIR for configure. Another variable LIBBPF_FORCE is used to control whether to build iproute2 with libbpf. If set to on, then force to build with libbpf and exit if not available. If set to off, then force to not build with libbpf. When dynamically linking against libbpf, we can't be sure that the version we discovered at compile time is actually the one we are using at runtime. This can lead to hard-to-debug errors. So we add a new file lib/bpf_glue.c and a helper function get_libbpf_version() to get correct libbpf version at runtime. Signed-off-by: Hangbin Liu --- v6: 1) Add a new helper get_libbpf_version() to get runtime libbpf version based on Toke's xdp-tools patch. The libbpf version will be printed when exec ip -V or tc -V. v5: 1) Fix LIBBPF_DIR type and description, use libbpf DESTDIR as LIBBPF_DIR dest. v4: 1) Remove duplicate LIBBPF_CFLAGS 2) Remove un-needed -L since using static libbpf.a 3) Fix == not supported in dash 4) Extend LIBBPF_FORCE to support on/off, when set to on, stop building when there is no libbpf support. If set to off, discard libbpf check. 5) Print libbpf version after checking v3: Check function bpf_program__section_name() separately and only use it on higher libbpf version. v2: No update --- configure | 113 +++++++++++++++++++++++++++++++++++++++++++++ include/bpf_util.h | 3 ++ ip/ip.c | 10 +++- lib/Makefile | 2 +- lib/bpf_glue.c | 63 +++++++++++++++++++++++++ tc/tc.c | 10 +++- 6 files changed, 196 insertions(+), 5 deletions(-) create mode 100644 lib/bpf_glue.c diff --git a/configure b/configure index 307912aa..2c363d3b 100755 --- a/configure +++ b/configure @@ -2,6 +2,11 @@ # SPDX-License-Identifier: GPL-2.0 # This is not an autoconf generated configure # +# Influential LIBBPF environment variables: +# LIBBPF_FORCE={on,off} on: require link against libbpf; +# off: disable libbpf probing +# LIBBPF_DIR Path to libbpf DESTDIR to use + INCLUDE=${1:-"$PWD/include"} # Output file which is input to Makefile @@ -240,6 +245,111 @@ check_elf() fi } +have_libbpf_basic() +{ + cat >$TMPDIR/libbpf_test.c < +int main(int argc, char **argv) { + bpf_program__set_autoload(NULL, false); + bpf_map__ifindex(NULL); + bpf_map__set_pin_path(NULL, NULL); + bpf_object__open_file(NULL, NULL); + return 0; +} +EOF + + $CC -o $TMPDIR/libbpf_test $TMPDIR/libbpf_test.c $LIBBPF_CFLAGS $LIBBPF_LDLIBS >/dev/null 2>&1 + local ret=$? + + rm -f $TMPDIR/libbpf_test.c $TMPDIR/libbpf_test + return $ret +} + +have_libbpf_sec_name() +{ + cat >$TMPDIR/libbpf_sec_test.c < +int main(int argc, char **argv) { + void *ptr; + bpf_program__section_name(NULL); + return 0; +} +EOF + + $CC -o $TMPDIR/libbpf_sec_test $TMPDIR/libbpf_sec_test.c $LIBBPF_CFLAGS $LIBBPF_LDLIBS >/dev/null 2>&1 + local ret=$? + + rm -f $TMPDIR/libbpf_sec_test.c $TMPDIR/libbpf_sec_test + return $ret +} + +check_force_libbpf_on() +{ + # if set LIBBPF_FORCE=on but no libbpf support, just exist the config + # process to make sure we don't build without libbpf. + if [ "$LIBBPF_FORCE" = on ]; then + echo " LIBBPF_FORCE=on set, but couldn't find a usable libbpf" + exit 1 + fi +} + +check_libbpf() +{ + # if set LIBBPF_FORCE=off, disable libbpf entirely + if [ "$LIBBPF_FORCE" = off ]; then + echo "no" + return + fi + + if ! ${PKG_CONFIG} libbpf --exists && [ -z "$LIBBPF_DIR" ] ; then + echo "no" + check_force_libbpf_on + return + fi + + if [ $(uname -m) = x86_64 ]; then + local LIBBPF_LIBDIR="${LIBBPF_DIR}/usr/lib64" + else + local LIBBPF_LIBDIR="${LIBBPF_DIR}/usr/lib" + fi + + if [ -n "$LIBBPF_DIR" ]; then + LIBBPF_CFLAGS="-I${LIBBPF_DIR}/usr/include" + LIBBPF_LDLIBS="${LIBBPF_LIBDIR}/libbpf.a -lz -lelf" + LIBBPF_VERSION=$(PKG_CONFIG_LIBDIR=${LIBBPF_LIBDIR}/pkgconfig ${PKG_CONFIG} libbpf --modversion) + else + LIBBPF_CFLAGS=$(${PKG_CONFIG} libbpf --cflags) + LIBBPF_LDLIBS=$(${PKG_CONFIG} libbpf --libs) + LIBBPF_VERSION=$(${PKG_CONFIG} libbpf --modversion) + fi + + if ! have_libbpf_basic; then + echo "no" + echo " libbpf version $LIBBPF_VERSION is too low, please update it to at least 0.1.0" + check_force_libbpf_on + return + else + echo "HAVE_LIBBPF:=y" >> $CONFIG + echo 'CFLAGS += -DHAVE_LIBBPF ' $LIBBPF_CFLAGS >> $CONFIG + echo "CFLAGS += -DLIBBPF_VERSION=\\\"$LIBBPF_VERSION\\\"" >> $CONFIG + echo 'LDLIBS += ' $LIBBPF_LDLIBS >> $CONFIG + + if [ -z "$LIBBPF_DIR" ]; then + echo "CFLAGS += -DLIBBPF_DYNAMIC" >> $CONFIG + fi + fi + + # bpf_program__title() is deprecated since libbpf 0.2.0, use + # bpf_program__section_name() instead if we support + if have_libbpf_sec_name; then + echo "HAVE_LIBBPF_SECTION_NAME:=y" >> $CONFIG + echo 'CFLAGS += -DHAVE_LIBBPF_SECTION_NAME ' >> $CONFIG + fi + + echo "yes" + echo " libbpf version $LIBBPF_VERSION" +} + check_selinux() # SELinux is a compile time option in the ss utility { @@ -385,6 +495,9 @@ check_setns echo -n "SELinux support: " check_selinux +echo -n "libbpf support: " +check_libbpf + echo -n "ELF support: " check_elf diff --git a/include/bpf_util.h b/include/bpf_util.h index 63db07ca..dee5bb02 100644 --- a/include/bpf_util.h +++ b/include/bpf_util.h @@ -300,4 +300,7 @@ static inline int bpf_recv_map_fds(const char *path, int *fds, return -1; } #endif /* HAVE_ELF */ + +const char *get_libbpf_version(void); + #endif /* __BPF_UTIL__ */ diff --git a/ip/ip.c b/ip/ip.c index 5e31957f..466dbb52 100644 --- a/ip/ip.c +++ b/ip/ip.c @@ -24,6 +24,7 @@ #include "namespace.h" #include "color.h" #include "rt_names.h" +#include "bpf_util.h" int preferred_family = AF_UNSPEC; int human_readable; @@ -147,8 +148,9 @@ static int batch(const char *name) int main(int argc, char **argv) { - char *basename; + const char *libbpf_version; char *batch_file = NULL; + char *basename; int color = 0; /* to run vrf exec without root, capabilities might be set, drop them @@ -229,7 +231,11 @@ int main(int argc, char **argv) ++timestamp; ++timestamp_short; } else if (matches(opt, "-Version") == 0) { - printf("ip utility, iproute2-%s\n", version); + printf("ip utility, iproute2-%s", version); + libbpf_version = get_libbpf_version(); + if (libbpf_version) + printf(", libbpf %s", libbpf_version); + printf("\n"); exit(0); } else if (matches(opt, "-force") == 0) { ++force; diff --git a/lib/Makefile b/lib/Makefile index 13f4ee15..a02775a5 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -5,7 +5,7 @@ CFLAGS += -fPIC UTILOBJ = utils.o rt_names.o ll_map.o ll_types.o ll_proto.o ll_addr.o \ inet_proto.o namespace.o json_writer.o json_print.o \ - names.o color.o bpf.o exec.o fs.o cg_map.o + names.o color.o bpf.o bpf_glue.o exec.o fs.o cg_map.o NLOBJ=libgenl.o libnetlink.o mnl_utils.o diff --git a/lib/bpf_glue.c b/lib/bpf_glue.c new file mode 100644 index 00000000..67c41c22 --- /dev/null +++ b/lib/bpf_glue.c @@ -0,0 +1,63 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * bpf_glue.c: BPF code to call both legacy and libbpf code + * Authors: Hangbin Liu + * + */ +#include "bpf_util.h" + +#ifdef HAVE_LIBBPF +static const char *_libbpf_compile_version = LIBBPF_VERSION; +static char _libbpf_version[10] = {}; + +const char *get_libbpf_version(void) +{ + /* Start by copying compile-time version into buffer so we have a + * fallback value in case we are dynamically linked, or can't find a + * version in /proc/self/maps below. + */ + strncpy(_libbpf_version, _libbpf_compile_version, + sizeof(_libbpf_version)-1); +#ifdef LIBBPF_DYNAMIC + char buf[PATH_MAX], *s; + bool found = false; + FILE *fp; + + /* When dynamically linking against libbpf, we can't be sure that the + * version we discovered at compile time is actually the one we are + * using at runtime. This can lead to hard-to-debug errors, so we try to + * discover the correct version at runtime. + * + * The simple solution to this would be if libbpf itself exported a + * version in its API. But since it doesn't, we work around this by + * parsing the mappings of the binary at runtime, looking for the full + * filename of libbpf.so and using that. + */ + fp = fopen("/proc/self/maps", "r"); + if (fp == NULL) + goto out; + + while ((s = fgets(buf, sizeof(buf), fp)) != NULL) { + if ((s = strstr(buf, "libbpf.so.")) != NULL) { + strncpy(_libbpf_version, s+10, sizeof(_libbpf_version)-1); + strtok(_libbpf_version, "\n"); + found = true; + break; + } + } + + fclose(fp); +out: + if (!found) + fprintf(stderr, "Couldn't find runtime libbpf version - falling back to compile-time value!\n"); +#endif /* LIBBPF_DYNAMIC */ + + _libbpf_version[sizeof(_libbpf_version)-1] = '\0'; + return _libbpf_version; +} +#else +const char *get_libbpf_version(void) +{ + return NULL; +} +#endif /* HAVE_LIBBPF */ diff --git a/tc/tc.c b/tc/tc.c index af9b21da..7557b977 100644 --- a/tc/tc.c +++ b/tc/tc.c @@ -30,6 +30,7 @@ #include "tc_common.h" #include "namespace.h" #include "rt_names.h" +#include "bpf_util.h" int show_stats; int show_details; @@ -259,8 +260,9 @@ static int batch(const char *name) int main(int argc, char **argv) { - int ret; + const char *libbpf_version; char *batch_file = NULL; + int ret; while (argc > 1) { if (argv[1][0] != '-') @@ -277,7 +279,11 @@ int main(int argc, char **argv) } else if (matches(argv[1], "-graph") == 0) { show_graph = 1; } else if (matches(argv[1], "-Version") == 0) { - printf("tc utility, iproute2-%s\n", version); + printf("tc utility, iproute2-%s", version); + libbpf_version = get_libbpf_version(); + if (libbpf_version) + printf(", libbpf %s", libbpf_version); + printf("\n"); return 0; } else if (matches(argv[1], "-iec") == 0) { ++use_iec; From patchwork Mon Nov 23 13:11:58 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Hangbin Liu X-Patchwork-Id: 11925169 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B8CAEC64E7C for ; Mon, 23 Nov 2020 13:14:10 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 770532075A for ; Mon, 23 Nov 2020 13:14:10 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="EEaCeBaJ" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388556AbgKWNNd (ORCPT ); Mon, 23 Nov 2020 08:13:33 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:58400 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388493AbgKWNN3 (ORCPT ); Mon, 23 Nov 2020 08:13:29 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1606137207; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ZA6nwB26MLn+tYTLxJodRLjBoMCDEYfGLLzqE2UpdxU=; b=EEaCeBaJZGDk6YXjza5Aezz6G24xSYcg3JzbsWIP622ny3blRDJWgu/PTbiF84iFhoP/lL qLBFT1TJzhWP/jwwGrM5h8BZH0NS+biw0h4846Eo0Sdj0RYAtwfD6QOaYCmp0/VXZxpiXN E/WM2XHXUKyQ2OexFE1Pm0k/dhe1rhQ= Received: from mail-pg1-f199.google.com (mail-pg1-f199.google.com [209.85.215.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-68-818_XVC9OQuNFCO3Gu1dIA-1; Mon, 23 Nov 2020 08:13:25 -0500 X-MC-Unique: 818_XVC9OQuNFCO3Gu1dIA-1 Received: by mail-pg1-f199.google.com with SMTP id u26so1469981pgl.15 for ; Mon, 23 Nov 2020 05:13:25 -0800 (PST) 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:mime-version:content-transfer-encoding; bh=ZA6nwB26MLn+tYTLxJodRLjBoMCDEYfGLLzqE2UpdxU=; b=p1Q6/seWL2i89uVptG4S5CtEelCQg5E1fTQqAtNZ7NCtxg6SbTJEb1+b8CZlhWJs7I LsRWWaTS77zKaERjZiU4vuQQ74Tl2lYPuB+CnL1EaWOBgh+9NFZsABNun8Z0Z4rIleDi ltx+boUrGqcc7UTAay2KPJ3xbrIiuI/mxN+zDteJcG1pVSTfrBJlQdKQHOGFIFqQOAw/ wazPn1WkLU7DVcfqa3lfCvv3f9PkaKO1pjvv4nlLkov8RQV1GfEHXU4970rB3qCXszmk XSqe76TjlkURr2lEC/jhmFNc60A7LvvmL3NV2VIiDqiyteFCH5CHwkTUjkcyA1XtOpGr +lZQ== X-Gm-Message-State: AOAM533l85Qovdc9fJaquTdTtGgF1R0vZ/RusKD0wm2so9NDb3bPSCG3 vlNYslVsazL3wsmyZbxSm66W9f1/+3HDdbnCDdylnUV5rBq785xxm97pItpHolTKirFKmUDtL8b 2aI4YCBR66kk= X-Received: by 2002:a17:902:6b4a:b029:d9:f601:1fd9 with SMTP id g10-20020a1709026b4ab02900d9f6011fd9mr8850484plt.67.1606137204343; Mon, 23 Nov 2020 05:13:24 -0800 (PST) X-Google-Smtp-Source: ABdhPJzKuU5R4n712A5OmStNkmXIY57+abmjBwK3RnS1B9ZZ+1KZK8YBlltkj1QOMzujbUkz1F81XA== X-Received: by 2002:a17:902:6b4a:b029:d9:f601:1fd9 with SMTP id g10-20020a1709026b4ab02900d9f6011fd9mr8850461plt.67.1606137204099; Mon, 23 Nov 2020 05:13:24 -0800 (PST) Received: from localhost.localdomain.com ([209.132.188.80]) by smtp.gmail.com with ESMTPSA id 84sm12075505pfu.53.2020.11.23.05.13.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Nov 2020 05:13:23 -0800 (PST) From: Hangbin Liu To: Stephen Hemminger , David Ahern Cc: Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , David Miller , netdev@vger.kernel.org, bpf@vger.kernel.org, Jiri Benc , =?utf-8?q?Toke_H=C3=B8il?= =?utf-8?q?and-J=C3=B8rgensen?= , Jesper Dangaard Brouer , Alexei Starovoitov , Hangbin Liu Subject: [PATCHv6 iproute2-next 2/5] lib: make ipvrf able to use libbpf and fix function name conflicts Date: Mon, 23 Nov 2020 21:11:58 +0800 Message-Id: <20201123131201.4108483-3-haliu@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: <20201123131201.4108483-1-haliu@redhat.com> References: <20201116065305.1010651-1-haliu@redhat.com> <20201123131201.4108483-1-haliu@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: dsahern@gmail.com There are directly calls in libbpf for bpf program load/attach. So we could just use two wrapper functions for ipvrf and convert them with libbpf support. Function bpf_prog_load() is removed as it's conflict with libbpf function name. bpf.c is moved to bpf_legacy.c for later main libbpf support in iproute2. Reviewed-by: Toke Høiland-Jørgensen Signed-off-by: Hangbin Liu --- v6: bpf_glue.c is created in previous patch. So I changed the commit description. v5: Fix bpf_prog_load_dev typo. v4: Add new file bpf_glue.c v2-v3: no update --- include/bpf_util.h | 10 +++++++--- ip/ipvrf.c | 6 +++--- lib/Makefile | 2 +- lib/bpf_glue.c | 23 +++++++++++++++++++++++ lib/{bpf.c => bpf_legacy.c} | 15 +++------------ 5 files changed, 37 insertions(+), 19 deletions(-) rename lib/{bpf.c => bpf_legacy.c} (99%) diff --git a/include/bpf_util.h b/include/bpf_util.h index dee5bb02..3235c34e 100644 --- a/include/bpf_util.h +++ b/include/bpf_util.h @@ -274,12 +274,16 @@ int bpf_trace_pipe(void); void bpf_print_ops(struct rtattr *bpf_ops, __u16 len); -int bpf_prog_load(enum bpf_prog_type type, const struct bpf_insn *insns, - size_t size_insns, const char *license, char *log, - size_t size_log); +int bpf_prog_load_dev(enum bpf_prog_type type, const struct bpf_insn *insns, + size_t size_insns, const char *license, __u32 ifindex, + char *log, size_t size_log); +int bpf_program_load(enum bpf_prog_type type, const struct bpf_insn *insns, + size_t size_insns, const char *license, char *log, + size_t size_log); int bpf_prog_attach_fd(int prog_fd, int target_fd, enum bpf_attach_type type); int bpf_prog_detach_fd(int target_fd, enum bpf_attach_type type); +int bpf_program_attach(int prog_fd, int target_fd, enum bpf_attach_type type); int bpf_dump_prog_info(FILE *f, uint32_t id); diff --git a/ip/ipvrf.c b/ip/ipvrf.c index 28dd8e25..42779e5c 100644 --- a/ip/ipvrf.c +++ b/ip/ipvrf.c @@ -256,8 +256,8 @@ static int prog_load(int idx) BPF_EXIT_INSN(), }; - return bpf_prog_load(BPF_PROG_TYPE_CGROUP_SOCK, prog, sizeof(prog), - "GPL", bpf_log_buf, sizeof(bpf_log_buf)); + return bpf_program_load(BPF_PROG_TYPE_CGROUP_SOCK, prog, sizeof(prog), + "GPL", bpf_log_buf, sizeof(bpf_log_buf)); } static int vrf_configure_cgroup(const char *path, int ifindex) @@ -288,7 +288,7 @@ static int vrf_configure_cgroup(const char *path, int ifindex) goto out; } - if (bpf_prog_attach_fd(prog_fd, cg_fd, BPF_CGROUP_INET_SOCK_CREATE)) { + if (bpf_program_attach(prog_fd, cg_fd, BPF_CGROUP_INET_SOCK_CREATE)) { fprintf(stderr, "Failed to attach prog to cgroup: '%s'\n", strerror(errno)); goto out; diff --git a/lib/Makefile b/lib/Makefile index a02775a5..7c8a197c 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -5,7 +5,7 @@ CFLAGS += -fPIC UTILOBJ = utils.o rt_names.o ll_map.o ll_types.o ll_proto.o ll_addr.o \ inet_proto.o namespace.o json_writer.o json_print.o \ - names.o color.o bpf.o bpf_glue.o exec.o fs.o cg_map.o + names.o color.o bpf_legacy.o bpf_glue.o exec.o fs.o cg_map.o NLOBJ=libgenl.o libnetlink.o mnl_utils.o diff --git a/lib/bpf_glue.c b/lib/bpf_glue.c index 67c41c22..fa609bfe 100644 --- a/lib/bpf_glue.c +++ b/lib/bpf_glue.c @@ -5,6 +5,29 @@ * */ #include "bpf_util.h" +#ifdef HAVE_LIBBPF +#include +#endif + +int bpf_program_load(enum bpf_prog_type type, const struct bpf_insn *insns, + size_t size_insns, const char *license, char *log, + size_t size_log) +{ +#ifdef HAVE_LIBBPF + return bpf_load_program(type, insns, size_insns, license, 0, log, size_log); +#else + return bpf_prog_load_dev(type, insns, size_insns, license, 0, log, size_log); +#endif +} + +int bpf_program_attach(int prog_fd, int target_fd, enum bpf_attach_type type) +{ +#ifdef HAVE_LIBBPF + return bpf_prog_attach(prog_fd, target_fd, type, 0); +#else + return bpf_prog_attach_fd(prog_fd, target_fd, type); +#endif +} #ifdef HAVE_LIBBPF static const char *_libbpf_compile_version = LIBBPF_VERSION; diff --git a/lib/bpf.c b/lib/bpf_legacy.c similarity index 99% rename from lib/bpf.c rename to lib/bpf_legacy.c index c7d45077..4246fb76 100644 --- a/lib/bpf.c +++ b/lib/bpf_legacy.c @@ -1087,10 +1087,9 @@ int bpf_prog_detach_fd(int target_fd, enum bpf_attach_type type) return bpf(BPF_PROG_DETACH, &attr, sizeof(attr)); } -static int bpf_prog_load_dev(enum bpf_prog_type type, - const struct bpf_insn *insns, size_t size_insns, - const char *license, __u32 ifindex, - char *log, size_t size_log) +int bpf_prog_load_dev(enum bpf_prog_type type, const struct bpf_insn *insns, + size_t size_insns, const char *license, __u32 ifindex, + char *log, size_t size_log) { union bpf_attr attr = {}; @@ -1109,14 +1108,6 @@ static int bpf_prog_load_dev(enum bpf_prog_type type, return bpf(BPF_PROG_LOAD, &attr, sizeof(attr)); } -int bpf_prog_load(enum bpf_prog_type type, const struct bpf_insn *insns, - size_t size_insns, const char *license, char *log, - size_t size_log) -{ - return bpf_prog_load_dev(type, insns, size_insns, license, 0, - log, size_log); -} - #ifdef HAVE_ELF struct bpf_elf_prog { enum bpf_prog_type type; From patchwork Mon Nov 23 13:11:59 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Hangbin Liu X-Patchwork-Id: 11925173 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 55917C71155 for ; Mon, 23 Nov 2020 13:14:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EE02B2075A for ; Mon, 23 Nov 2020 13:14:10 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="e/fL5puQ" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388558AbgKWNNi (ORCPT ); Mon, 23 Nov 2020 08:13:38 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:27024 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388522AbgKWNNg (ORCPT ); Mon, 23 Nov 2020 08:13:36 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1606137212; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mgDD3hZuqRpdyTLabxn5w6eqJnCMSPbJaLGE+s8ZXRg=; b=e/fL5puQ/ar0k4i3qQyAh0SEJAh+rysGOcS7Ys497k7ecHtfYG/Fn843Bk36QffiOUZpB2 h1aCxM7xp8rhIc6gqFDOFFrjA/JwD51oy7wPzMMghDCKNN+JRG8MbCiPBk03yl9dH2EQ1f 4nP2O0JK9BvNlo5SpACD4XO0oQyjXa0= Received: from mail-pg1-f197.google.com (mail-pg1-f197.google.com [209.85.215.197]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-310-BYrjaEwdP2ufjP10YIvgLQ-1; Mon, 23 Nov 2020 08:13:31 -0500 X-MC-Unique: BYrjaEwdP2ufjP10YIvgLQ-1 Received: by mail-pg1-f197.google.com with SMTP id c4so11845125pgb.0 for ; Mon, 23 Nov 2020 05:13:30 -0800 (PST) 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:mime-version:content-transfer-encoding; bh=mgDD3hZuqRpdyTLabxn5w6eqJnCMSPbJaLGE+s8ZXRg=; b=TOxKDr87sF6AL8FzCyfLnbaYLYAmG3j7kyrzNOEvihcFyafmRaAGeSmLL9zfokXWNW 0acyianttdeXMvQC09mRR++DuzBE1S3ga+kPX1EtbIR2PkFIocBtrsWuyRXxc4SfHdm6 Cu1zXZVRNEzHA8Wz911YP/MH44XKumecUYlQffpnb5XNvgiEsePvvf+OBaGhNasIGvpk a4ZQridLrdW3TGgJ0qn9hR72rCS6bnykPmkuM/4TeYeFgZMLgIIgEfW7uRrJyXw0W2YY 486s/iMAtPtB4k0BD0zI9Elrz+vEv3UIxhkOvFMLhJz9EtKx8hJ4n0xVdStrEb1ne4wy LUXg== X-Gm-Message-State: AOAM533s5cAHJSKwcMx3579npElrp80/NbtoQ5u1Tm4wWYItPKTsye5r 1gJlUGXVx7/b2u2kVojxeY20bz+0JEzMQtjGDY4BkbBCx495GRK+8Wu2XjIaT3m59wEFF6c3Cco Ybw4mD5RDRik= X-Received: by 2002:a17:90a:1bcb:: with SMTP id r11mr23326099pjr.27.1606137209792; Mon, 23 Nov 2020 05:13:29 -0800 (PST) X-Google-Smtp-Source: ABdhPJzkXuNP927CTEDXqn0ipJvSVQNKrMNcIiVwdIbEqq3b3tozgVLcVNugnvTQi3BfV4R5wbr0aQ== X-Received: by 2002:a17:90a:1bcb:: with SMTP id r11mr23326082pjr.27.1606137209500; Mon, 23 Nov 2020 05:13:29 -0800 (PST) Received: from localhost.localdomain.com ([209.132.188.80]) by smtp.gmail.com with ESMTPSA id 84sm12075505pfu.53.2020.11.23.05.13.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Nov 2020 05:13:28 -0800 (PST) From: Hangbin Liu To: Stephen Hemminger , David Ahern Cc: Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , David Miller , netdev@vger.kernel.org, bpf@vger.kernel.org, Jiri Benc , =?utf-8?q?Toke_H=C3=B8il?= =?utf-8?q?and-J=C3=B8rgensen?= , Jesper Dangaard Brouer , Alexei Starovoitov , Hangbin Liu Subject: [PATCHv6 iproute2-next 3/5] lib: add libbpf support Date: Mon, 23 Nov 2020 21:11:59 +0800 Message-Id: <20201123131201.4108483-4-haliu@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: <20201123131201.4108483-1-haliu@redhat.com> References: <20201116065305.1010651-1-haliu@redhat.com> <20201123131201.4108483-1-haliu@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: dsahern@gmail.com This patch converts iproute2 to use libbpf for loading and attaching BPF programs when it is available, which is started by Toke's implementation[1]. With libbpf iproute2 could correctly process BTF information and support the new-style BTF-defined maps, while keeping compatibility with the old internal map definition syntax. The old iproute2 bpf code is kept and will be used if no suitable libbpf is available. When using libbpf, wrapper code in bpf_legacy.c ensures that iproute2 will still understand the old map definition format, including populating map-in-map and tail call maps before load. In bpf_libbpf.c, we init iproute2 ctx and elf info first to check the legacy bytes. When handling the legacy maps, for map-in-maps, we create them manually and re-use the fd as they are associated with id/inner_id. For pin maps, we only set the pin path and let libbp load to handle it. For tail calls, we find it first and update the element after prog load. Other maps/progs will be loaded by libbpf directly. [1] https://lore.kernel.org/bpf/20190820114706.18546-1-toke@redhat.com/ Reviewed-by: Toke Høiland-Jørgensen Signed-off-by: Hangbin Liu --- v6: also make bpf_libbpf.c build depend on HAVE_ELF v5: no update v4: Move ipvrf code to patch 02 Move HAVE_LIBBPF inside HAVE_ELF definition as libbpf depends on elf. v3: Add a new function get_bpf_program__section_name() to choose whether use bpf_program__title() or not. v2: Remove self defined IS_ERR_OR_NULL and use libbpf_get_error() instead. Add ipvrf with libbpf support. --- include/bpf_util.h | 17 +++ lib/Makefile | 6 + lib/bpf_legacy.c | 178 +++++++++++++++++++++++ lib/bpf_libbpf.c | 348 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 549 insertions(+) create mode 100644 lib/bpf_libbpf.c diff --git a/include/bpf_util.h b/include/bpf_util.h index 3235c34e..53acc410 100644 --- a/include/bpf_util.h +++ b/include/bpf_util.h @@ -291,6 +291,16 @@ int bpf_dump_prog_info(FILE *f, uint32_t id); int bpf_send_map_fds(const char *path, const char *obj); int bpf_recv_map_fds(const char *path, int *fds, struct bpf_map_aux *aux, unsigned int entries); +#ifdef HAVE_LIBBPF +int iproute2_bpf_elf_ctx_init(struct bpf_cfg_in *cfg); +int iproute2_bpf_fetch_ancillary(void); +int iproute2_get_root_path(char *root_path, size_t len); +bool iproute2_is_pin_map(const char *libbpf_map_name, char *pathname); +bool iproute2_is_map_in_map(const char *libbpf_map_name, struct bpf_elf_map *imap, + struct bpf_elf_map *omap, char *omap_name); +int iproute2_find_map_name_by_id(unsigned int map_id, char *name); +int iproute2_load_libbpf(struct bpf_cfg_in *cfg); +#endif /* HAVE_LIBBPF */ #else static inline int bpf_send_map_fds(const char *path, const char *obj) { @@ -303,6 +313,13 @@ static inline int bpf_recv_map_fds(const char *path, int *fds, { return -1; } +#ifdef HAVE_LIBBPF +static inline int iproute2_load_libbpf(struct bpf_cfg_in *cfg) +{ + fprintf(stderr, "No ELF library support compiled in.\n"); + return -1; +} +#endif /* HAVE_LIBBPF */ #endif /* HAVE_ELF */ const char *get_libbpf_version(void); diff --git a/lib/Makefile b/lib/Makefile index 7c8a197c..e37585c6 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -7,6 +7,12 @@ UTILOBJ = utils.o rt_names.o ll_map.o ll_types.o ll_proto.o ll_addr.o \ inet_proto.o namespace.o json_writer.o json_print.o \ names.o color.o bpf_legacy.o bpf_glue.o exec.o fs.o cg_map.o +ifeq ($(HAVE_ELF),y) +ifeq ($(HAVE_LIBBPF),y) +UTILOBJ += bpf_libbpf.o +endif +endif + NLOBJ=libgenl.o libnetlink.o mnl_utils.o all: libnetlink.a libutil.a diff --git a/lib/bpf_legacy.c b/lib/bpf_legacy.c index 4246fb76..bc869c3f 100644 --- a/lib/bpf_legacy.c +++ b/lib/bpf_legacy.c @@ -940,6 +940,9 @@ static int bpf_do_parse(struct bpf_cfg_in *cfg, const bool *opt_tbl) static int bpf_do_load(struct bpf_cfg_in *cfg) { if (cfg->mode == EBPF_OBJECT) { +#ifdef HAVE_LIBBPF + return iproute2_load_libbpf(cfg); +#endif cfg->prog_fd = bpf_obj_open(cfg->object, cfg->type, cfg->section, cfg->ifindex, cfg->verbose); @@ -3155,4 +3158,179 @@ int bpf_recv_map_fds(const char *path, int *fds, struct bpf_map_aux *aux, close(fd); return ret; } + +#ifdef HAVE_LIBBPF +/* The following functions are wrapper functions for libbpf code to be + * compatible with the legacy format. So all the functions have prefix + * with iproute2_ + */ +int iproute2_bpf_elf_ctx_init(struct bpf_cfg_in *cfg) +{ + struct bpf_elf_ctx *ctx = &__ctx; + + return bpf_elf_ctx_init(ctx, cfg->object, cfg->type, cfg->ifindex, cfg->verbose); +} + +int iproute2_bpf_fetch_ancillary(void) +{ + struct bpf_elf_ctx *ctx = &__ctx; + struct bpf_elf_sec_data data; + int i, ret = 0; + + for (i = 1; i < ctx->elf_hdr.e_shnum; i++) { + ret = bpf_fill_section_data(ctx, i, &data); + if (ret < 0) + continue; + + if (data.sec_hdr.sh_type == SHT_PROGBITS && + !strcmp(data.sec_name, ELF_SECTION_MAPS)) + ret = bpf_fetch_maps_begin(ctx, i, &data); + else if (data.sec_hdr.sh_type == SHT_SYMTAB && + !strcmp(data.sec_name, ".symtab")) + ret = bpf_fetch_symtab(ctx, i, &data); + else if (data.sec_hdr.sh_type == SHT_STRTAB && + !strcmp(data.sec_name, ".strtab")) + ret = bpf_fetch_strtab(ctx, i, &data); + if (ret < 0) { + fprintf(stderr, "Error parsing section %d! Perhaps check with readelf -a?\n", + i); + return ret; + } + } + + if (bpf_has_map_data(ctx)) { + ret = bpf_fetch_maps_end(ctx); + if (ret < 0) { + fprintf(stderr, "Error fixing up map structure, incompatible struct bpf_elf_map used?\n"); + return ret; + } + } + + return ret; +} + +int iproute2_get_root_path(char *root_path, size_t len) +{ + struct bpf_elf_ctx *ctx = &__ctx; + int ret = 0; + + snprintf(root_path, len, "%s/%s", + bpf_get_work_dir(ctx->type), BPF_DIR_GLOBALS); + + ret = mkdir(root_path, S_IRWXU); + if (ret && errno != EEXIST) { + fprintf(stderr, "mkdir %s failed: %s\n", root_path, strerror(errno)); + return ret; + } + + return 0; +} + +bool iproute2_is_pin_map(const char *libbpf_map_name, char *pathname) +{ + struct bpf_elf_ctx *ctx = &__ctx; + const char *map_name, *tmp; + unsigned int pinning; + int i, ret = 0; + + for (i = 0; i < ctx->map_num; i++) { + if (ctx->maps[i].pinning == PIN_OBJECT_NS && + ctx->noafalg) { + fprintf(stderr, "Missing kernel AF_ALG support for PIN_OBJECT_NS!\n"); + return false; + } + + map_name = bpf_map_fetch_name(ctx, i); + if (!map_name) { + return false; + } + + if (strcmp(libbpf_map_name, map_name)) + continue; + + pinning = ctx->maps[i].pinning; + + if (bpf_no_pinning(ctx, pinning) || !bpf_get_work_dir(ctx->type)) + return false; + + if (pinning == PIN_OBJECT_NS) + ret = bpf_make_obj_path(ctx); + else if ((tmp = bpf_custom_pinning(ctx, pinning))) + ret = bpf_make_custom_path(ctx, tmp); + if (ret < 0) + return false; + + bpf_make_pathname(pathname, PATH_MAX, map_name, ctx, pinning); + + return true; + } + + return false; +} + +bool iproute2_is_map_in_map(const char *libbpf_map_name, struct bpf_elf_map *imap, + struct bpf_elf_map *omap, char *omap_name) +{ + struct bpf_elf_ctx *ctx = &__ctx; + const char *inner_map_name, *outer_map_name; + int i, j; + + for (i = 0; i < ctx->map_num; i++) { + inner_map_name = bpf_map_fetch_name(ctx, i); + if (!inner_map_name) { + return false; + } + + if (strcmp(libbpf_map_name, inner_map_name)) + continue; + + if (!ctx->maps[i].id || + ctx->maps[i].inner_id || + ctx->maps[i].inner_idx == -1) + continue; + + *imap = ctx->maps[i]; + + for (j = 0; j < ctx->map_num; j++) { + if (!bpf_is_map_in_map_type(&ctx->maps[j])) + continue; + if (ctx->maps[j].inner_id != ctx->maps[i].id) + continue; + + *omap = ctx->maps[j]; + outer_map_name = bpf_map_fetch_name(ctx, j); + memcpy(omap_name, outer_map_name, strlen(outer_map_name) + 1); + + return true; + } + } + + return false; +} + +int iproute2_find_map_name_by_id(unsigned int map_id, char *name) +{ + struct bpf_elf_ctx *ctx = &__ctx; + const char *map_name; + int i, idx = -1; + + for (i = 0; i < ctx->map_num; i++) { + if (ctx->maps[i].id == map_id && + ctx->maps[i].type == BPF_MAP_TYPE_PROG_ARRAY) { + idx = i; + break; + } + } + + if (idx < 0) + return -1; + + map_name = bpf_map_fetch_name(ctx, idx); + if (!map_name) + return -1; + + memcpy(name, map_name, strlen(map_name) + 1); + return 0; +} +#endif /* HAVE_LIBBPF */ #endif /* HAVE_ELF */ diff --git a/lib/bpf_libbpf.c b/lib/bpf_libbpf.c new file mode 100644 index 00000000..d05737a4 --- /dev/null +++ b/lib/bpf_libbpf.c @@ -0,0 +1,348 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * bpf_libbpf.c BPF code relay on libbpf + * Authors: Hangbin Liu + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include + +#include "bpf_util.h" + +static int verbose_print(enum libbpf_print_level level, const char *format, va_list args) +{ + return vfprintf(stderr, format, args); +} + +static int silent_print(enum libbpf_print_level level, const char *format, va_list args) +{ + if (level > LIBBPF_WARN) + return 0; + + /* Skip warning from bpf_object__init_user_maps() for legacy maps */ + if (strstr(format, "has unrecognized, non-zero options")) + return 0; + + return vfprintf(stderr, format, args); +} + +static const char *get_bpf_program__section_name(const struct bpf_program *prog) +{ +#ifdef HAVE_LIBBPF_SECTION_NAME + return bpf_program__section_name(prog); +#else + return bpf_program__title(prog, false); +#endif +} + +static int create_map(const char *name, struct bpf_elf_map *map, + __u32 ifindex, int inner_fd) +{ + struct bpf_create_map_attr map_attr = {}; + + map_attr.name = name; + map_attr.map_type = map->type; + map_attr.map_flags = map->flags; + map_attr.key_size = map->size_key; + map_attr.value_size = map->size_value; + map_attr.max_entries = map->max_elem; + map_attr.map_ifindex = ifindex; + map_attr.inner_map_fd = inner_fd; + + return bpf_create_map_xattr(&map_attr); +} + +static int create_map_in_map(struct bpf_object *obj, struct bpf_map *map, + struct bpf_elf_map *elf_map, int inner_fd, + bool *reuse_pin_map) +{ + char pathname[PATH_MAX]; + const char *map_name; + bool pin_map = false; + int map_fd, ret = 0; + + map_name = bpf_map__name(map); + + if (iproute2_is_pin_map(map_name, pathname)) { + pin_map = true; + + /* Check if there already has a pinned map */ + map_fd = bpf_obj_get(pathname); + if (map_fd > 0) { + if (reuse_pin_map) + *reuse_pin_map = true; + close(map_fd); + return bpf_map__set_pin_path(map, pathname); + } + } + + map_fd = create_map(map_name, elf_map, bpf_map__ifindex(map), inner_fd); + if (map_fd < 0) { + fprintf(stderr, "create map %s failed\n", map_name); + return map_fd; + } + + ret = bpf_map__reuse_fd(map, map_fd); + if (ret < 0) { + fprintf(stderr, "map %s reuse fd failed\n", map_name); + goto err_out; + } + + if (pin_map) { + ret = bpf_map__set_pin_path(map, pathname); + if (ret < 0) + goto err_out; + } + + return 0; +err_out: + close(map_fd); + return ret; +} + +static int +handle_legacy_map_in_map(struct bpf_object *obj, struct bpf_map *inner_map, + const char *inner_map_name) +{ + int inner_fd, outer_fd, inner_idx, ret = 0; + struct bpf_elf_map imap, omap; + struct bpf_map *outer_map; + /* What's the size limit of map name? */ + char outer_map_name[128]; + bool reuse_pin_map = false; + + /* Deal with map-in-map */ + if (iproute2_is_map_in_map(inner_map_name, &imap, &omap, outer_map_name)) { + ret = create_map_in_map(obj, inner_map, &imap, -1, NULL); + if (ret < 0) + return ret; + + inner_fd = bpf_map__fd(inner_map); + outer_map = bpf_object__find_map_by_name(obj, outer_map_name); + ret = create_map_in_map(obj, outer_map, &omap, inner_fd, &reuse_pin_map); + if (ret < 0) + return ret; + + if (!reuse_pin_map) { + inner_idx = imap.inner_idx; + outer_fd = bpf_map__fd(outer_map); + ret = bpf_map_update_elem(outer_fd, &inner_idx, &inner_fd, 0); + if (ret < 0) + fprintf(stderr, "Cannot update inner_idx into outer_map\n"); + } + } + + return ret; +} + +static int find_legacy_tail_calls(struct bpf_program *prog, struct bpf_object *obj) +{ + unsigned int map_id, key_id; + const char *sec_name; + struct bpf_map *map; + char map_name[128]; + int ret; + + /* Handle iproute2 tail call */ + sec_name = get_bpf_program__section_name(prog); + ret = sscanf(sec_name, "%i/%i", &map_id, &key_id); + if (ret != 2) + return -1; + + ret = iproute2_find_map_name_by_id(map_id, map_name); + if (ret < 0) { + fprintf(stderr, "unable to find map id %u for tail call\n", map_id); + return ret; + } + + map = bpf_object__find_map_by_name(obj, map_name); + if (!map) + return -1; + + /* Save the map here for later updating */ + bpf_program__set_priv(prog, map, NULL); + + return 0; +} + +static int update_legacy_tail_call_maps(struct bpf_object *obj) +{ + int prog_fd, map_fd, ret = 0; + unsigned int map_id, key_id; + struct bpf_program *prog; + const char *sec_name; + struct bpf_map *map; + + bpf_object__for_each_program(prog, obj) { + map = bpf_program__priv(prog); + if (!map) + continue; + + prog_fd = bpf_program__fd(prog); + if (prog_fd < 0) + continue; + + sec_name = get_bpf_program__section_name(prog); + ret = sscanf(sec_name, "%i/%i", &map_id, &key_id); + if (ret != 2) + continue; + + map_fd = bpf_map__fd(map); + ret = bpf_map_update_elem(map_fd, &key_id, &prog_fd, 0); + if (ret < 0) { + fprintf(stderr, "Cannot update map key for tail call!\n"); + return ret; + } + } + + return 0; +} + +static int handle_legacy_maps(struct bpf_object *obj) +{ + char pathname[PATH_MAX]; + struct bpf_map *map; + const char *map_name; + int map_fd, ret = 0; + + bpf_object__for_each_map(map, obj) { + map_name = bpf_map__name(map); + + ret = handle_legacy_map_in_map(obj, map, map_name); + if (ret) + return ret; + + /* If it is a iproute2 legacy pin maps, just set pin path + * and let bpf_object__load() to deal with the map creation. + * We need to ignore map-in-maps which have pinned maps manually + */ + map_fd = bpf_map__fd(map); + if (map_fd < 0 && iproute2_is_pin_map(map_name, pathname)) { + ret = bpf_map__set_pin_path(map, pathname); + if (ret) { + fprintf(stderr, "map '%s': couldn't set pin path.\n", map_name); + break; + } + } + + } + + return ret; +} + +static int load_bpf_object(struct bpf_cfg_in *cfg) +{ + struct bpf_program *p, *prog = NULL; + struct bpf_object *obj; + char root_path[PATH_MAX]; + struct bpf_map *map; + int prog_fd, ret = 0; + + ret = iproute2_get_root_path(root_path, PATH_MAX); + if (ret) + return ret; + + DECLARE_LIBBPF_OPTS(bpf_object_open_opts, open_opts, + .relaxed_maps = true, + .pin_root_path = root_path, + ); + + obj = bpf_object__open_file(cfg->object, &open_opts); + if (libbpf_get_error(obj)) { + fprintf(stderr, "ERROR: opening BPF object file failed\n"); + return -ENOENT; + } + + bpf_object__for_each_program(p, obj) { + /* Only load the programs that will either be subsequently + * attached or inserted into a tail call map */ + if (find_legacy_tail_calls(p, obj) < 0 && cfg->section && + strcmp(get_bpf_program__section_name(p), cfg->section)) { + ret = bpf_program__set_autoload(p, false); + if (ret) + return -EINVAL; + continue; + } + + bpf_program__set_type(p, cfg->type); + bpf_program__set_ifindex(p, cfg->ifindex); + if (!prog) + prog = p; + } + + bpf_object__for_each_map(map, obj) { + if (!bpf_map__is_offload_neutral(map)) + bpf_map__set_ifindex(map, cfg->ifindex); + } + + if (!prog) { + fprintf(stderr, "object file doesn't contain sec %s\n", cfg->section); + return -ENOENT; + } + + /* Handle iproute2 legacy pin maps and map-in-maps */ + ret = handle_legacy_maps(obj); + if (ret) + goto unload_obj; + + ret = bpf_object__load(obj); + if (ret) + goto unload_obj; + + ret = update_legacy_tail_call_maps(obj); + if (ret) + goto unload_obj; + + prog_fd = fcntl(bpf_program__fd(prog), F_DUPFD_CLOEXEC, 1); + if (prog_fd < 0) + ret = -errno; + else + cfg->prog_fd = prog_fd; + +unload_obj: + /* Close obj as we don't need it */ + bpf_object__close(obj); + return ret; +} + +/* Load ebpf and return prog fd */ +int iproute2_load_libbpf(struct bpf_cfg_in *cfg) +{ + int ret = 0; + + if (cfg->verbose) + libbpf_set_print(verbose_print); + else + libbpf_set_print(silent_print); + + ret = iproute2_bpf_elf_ctx_init(cfg); + if (ret < 0) { + fprintf(stderr, "Cannot initialize ELF context!\n"); + return ret; + } + + ret = iproute2_bpf_fetch_ancillary(); + if (ret < 0) { + fprintf(stderr, "Error fetching ELF ancillary data!\n"); + return ret; + } + + ret = load_bpf_object(cfg); + if (ret) + return ret; + + return cfg->prog_fd; +} From patchwork Mon Nov 23 13:12:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Hangbin Liu X-Patchwork-Id: 11925175 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 79A76C6379D for ; Mon, 23 Nov 2020 13:14:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1C8772075A for ; Mon, 23 Nov 2020 13:14:14 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="gKcdZHjO" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388592AbgKWNOB (ORCPT ); Mon, 23 Nov 2020 08:14:01 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:56083 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388559AbgKWNNk (ORCPT ); Mon, 23 Nov 2020 08:13:40 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1606137218; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=es+GSQz2MU3igYX62Y5g10wVw6Q1DpGz8rOsY1uqmXw=; b=gKcdZHjO1TnH0QLzjU0TGg6XEr+x+n6fbczfVuzTNa4DK/GW2hSgn6/BQjR4FemsXCklgS GAITmsKNBqhylV7/KBtJH4vLMjwiug1RNTv8pGcmhSMNeXqX7Wf+tmeRpXFIjxZYBayAEA k5TuFlfZWocJl5iYy+mwcIyimkSHCMw= Received: from mail-pf1-f199.google.com (mail-pf1-f199.google.com [209.85.210.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-40-HIq93tSdOO2P065KEbX7pg-1; Mon, 23 Nov 2020 08:13:36 -0500 X-MC-Unique: HIq93tSdOO2P065KEbX7pg-1 Received: by mail-pf1-f199.google.com with SMTP id v2so12703908pfi.11 for ; Mon, 23 Nov 2020 05:13:36 -0800 (PST) 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:mime-version:content-transfer-encoding; bh=es+GSQz2MU3igYX62Y5g10wVw6Q1DpGz8rOsY1uqmXw=; b=EFnkOe9hJzazYxrRggTabeL8bR1t3MUy/1VQJkmZZnetTc2bprxePAyGww/KwaWKUB S0blRmXi3tcqnY8UFeAiKSD7Y4ncgOC7qST1pF6rPeLV3GvRl8CKCpKJN2EfOEo0fOMG MB32TVe8QPSja+0hCLd1w+095HEaTqsCJY1/RYzhizdJiqOFzlkXuuI0Wdt9tuDKGFcT vgfSma3ly2feaGzAcnwaWhQReRvmA+hZD3/OSQ0/aZFRiE+jRUEh9OyI6icIvmVjOR97 ZHXYjy42ZS7/HniHPSo1nChjgVoTXBAbdnaFSRtSdJrkduGtkNo9Jtp3xnWOneqdK4S8 WsmQ== X-Gm-Message-State: AOAM533DQOXh9C1N8ZGqF8hGCKL/dSRpup9Jh/EvEGNpBqvrJvotm9r8 Rj6yacgQq3+LNEE3FLPgyo3QxQ1Ecbgx2+cijK/HR8LJCiY72Us/k3MQ8MMRWPLXg4Ql0D1dief A0MNVlJylU1U= X-Received: by 2002:a65:5588:: with SMTP id j8mr27014282pgs.245.1606137214553; Mon, 23 Nov 2020 05:13:34 -0800 (PST) X-Google-Smtp-Source: ABdhPJwNAS1DOyVHPl2ZmtTO7x07AfiymOEs/m9uJIgk9I9nSJ7qXj5gIpYpa9FRA6tsXohvYHNqXQ== X-Received: by 2002:a65:5588:: with SMTP id j8mr27014271pgs.245.1606137214318; Mon, 23 Nov 2020 05:13:34 -0800 (PST) Received: from localhost.localdomain.com ([209.132.188.80]) by smtp.gmail.com with ESMTPSA id 84sm12075505pfu.53.2020.11.23.05.13.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Nov 2020 05:13:33 -0800 (PST) From: Hangbin Liu To: Stephen Hemminger , David Ahern Cc: Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , David Miller , netdev@vger.kernel.org, bpf@vger.kernel.org, Jiri Benc , =?utf-8?q?Toke_H=C3=B8il?= =?utf-8?q?and-J=C3=B8rgensen?= , Jesper Dangaard Brouer , Alexei Starovoitov , Hangbin Liu Subject: [PATCHv6 iproute2-next 4/5] examples/bpf: move struct bpf_elf_map defined maps to legacy folder Date: Mon, 23 Nov 2020 21:12:00 +0800 Message-Id: <20201123131201.4108483-5-haliu@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: <20201123131201.4108483-1-haliu@redhat.com> References: <20201116065305.1010651-1-haliu@redhat.com> <20201123131201.4108483-1-haliu@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: dsahern@gmail.com Reviewed-by: Toke Høiland-Jørgensen Signed-off-by: Hangbin Liu --- examples/bpf/README | 14 +++++++++----- examples/bpf/{ => legacy}/bpf_cyclic.c | 2 +- examples/bpf/{ => legacy}/bpf_graft.c | 2 +- examples/bpf/{ => legacy}/bpf_map_in_map.c | 2 +- examples/bpf/{ => legacy}/bpf_shared.c | 2 +- examples/bpf/{ => legacy}/bpf_tailcall.c | 2 +- 6 files changed, 14 insertions(+), 10 deletions(-) rename examples/bpf/{ => legacy}/bpf_cyclic.c (95%) rename examples/bpf/{ => legacy}/bpf_graft.c (97%) rename examples/bpf/{ => legacy}/bpf_map_in_map.c (96%) rename examples/bpf/{ => legacy}/bpf_shared.c (97%) rename examples/bpf/{ => legacy}/bpf_tailcall.c (98%) diff --git a/examples/bpf/README b/examples/bpf/README index 1bbdda3f..732bcc83 100644 --- a/examples/bpf/README +++ b/examples/bpf/README @@ -1,8 +1,12 @@ eBPF toy code examples (running in kernel) to familiarize yourself with syntax and features: - - bpf_shared.c -> Ingress/egress map sharing example - - bpf_tailcall.c -> Using tail call chains - - bpf_cyclic.c -> Simple cycle as tail calls - - bpf_graft.c -> Demo on altering runtime behaviour - - bpf_map_in_map.c -> Using map in map example + - legacy/bpf_shared.c -> Ingress/egress map sharing example + - legacy/bpf_tailcall.c -> Using tail call chains + - legacy/bpf_cyclic.c -> Simple cycle as tail calls + - legacy/bpf_graft.c -> Demo on altering runtime behaviour + - legacy/bpf_map_in_map.c -> Using map in map example + +Note: Users should use new BTF way to defined the maps, the examples +in legacy folder which is using struct bpf_elf_map defined maps is not +recommanded. diff --git a/examples/bpf/bpf_cyclic.c b/examples/bpf/legacy/bpf_cyclic.c similarity index 95% rename from examples/bpf/bpf_cyclic.c rename to examples/bpf/legacy/bpf_cyclic.c index 11d1c061..33590730 100644 --- a/examples/bpf/bpf_cyclic.c +++ b/examples/bpf/legacy/bpf_cyclic.c @@ -1,4 +1,4 @@ -#include "../../include/bpf_api.h" +#include "../../../include/bpf_api.h" /* Cyclic dependency example to test the kernel's runtime upper * bound on loops. Also demonstrates on how to use direct-actions, diff --git a/examples/bpf/bpf_graft.c b/examples/bpf/legacy/bpf_graft.c similarity index 97% rename from examples/bpf/bpf_graft.c rename to examples/bpf/legacy/bpf_graft.c index 07113d4a..f4c920cc 100644 --- a/examples/bpf/bpf_graft.c +++ b/examples/bpf/legacy/bpf_graft.c @@ -1,4 +1,4 @@ -#include "../../include/bpf_api.h" +#include "../../../include/bpf_api.h" /* This example demonstrates how classifier run-time behaviour * can be altered with tail calls. We start out with an empty diff --git a/examples/bpf/bpf_map_in_map.c b/examples/bpf/legacy/bpf_map_in_map.c similarity index 96% rename from examples/bpf/bpf_map_in_map.c rename to examples/bpf/legacy/bpf_map_in_map.c index ff0e623a..575f8812 100644 --- a/examples/bpf/bpf_map_in_map.c +++ b/examples/bpf/legacy/bpf_map_in_map.c @@ -1,4 +1,4 @@ -#include "../../include/bpf_api.h" +#include "../../../include/bpf_api.h" #define MAP_INNER_ID 42 diff --git a/examples/bpf/bpf_shared.c b/examples/bpf/legacy/bpf_shared.c similarity index 97% rename from examples/bpf/bpf_shared.c rename to examples/bpf/legacy/bpf_shared.c index 21fe6f1e..05b2b9ef 100644 --- a/examples/bpf/bpf_shared.c +++ b/examples/bpf/legacy/bpf_shared.c @@ -1,4 +1,4 @@ -#include "../../include/bpf_api.h" +#include "../../../include/bpf_api.h" /* Minimal, stand-alone toy map pinning example: * diff --git a/examples/bpf/bpf_tailcall.c b/examples/bpf/legacy/bpf_tailcall.c similarity index 98% rename from examples/bpf/bpf_tailcall.c rename to examples/bpf/legacy/bpf_tailcall.c index 161eb606..8ebc554c 100644 --- a/examples/bpf/bpf_tailcall.c +++ b/examples/bpf/legacy/bpf_tailcall.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-2.0 */ -#include "../../include/bpf_api.h" +#include "../../../include/bpf_api.h" #define ENTRY_INIT 3 #define ENTRY_0 0 From patchwork Mon Nov 23 13:12:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Hangbin Liu X-Patchwork-Id: 11925171 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 21FF0C64E90 for ; Mon, 23 Nov 2020 13:14:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D83862076E for ; Mon, 23 Nov 2020 13:14:11 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="h2/ua5nF" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387401AbgKWNNo (ORCPT ); Mon, 23 Nov 2020 08:13:44 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:56767 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731730AbgKWNNo (ORCPT ); Mon, 23 Nov 2020 08:13:44 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1606137222; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mNpTnhqVT/lkkDzOBStmitBv9qqoRpOHiUUiZ8tRN8I=; b=h2/ua5nF5dsGOZXkx1eQvZTzpL1KUtXtO3+tR4VTvI5rKutJmcMLajsUBS1ttRP6LQQ6Fb 5bplkaahqzQtz7SKG2KXD7+UN9FKMiDIgL6GC1tGFXs4DDVgG9bjX0BjtowElb6lZwW3cL m14H2VQfA8ZjwwSzbVyM9YCBxDBi/O4= Received: from mail-pg1-f200.google.com (mail-pg1-f200.google.com [209.85.215.200]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-190-7Tml3It8OCiElPBWMxf4gQ-1; Mon, 23 Nov 2020 08:13:40 -0500 X-MC-Unique: 7Tml3It8OCiElPBWMxf4gQ-1 Received: by mail-pg1-f200.google.com with SMTP id a20so12434702pgb.21 for ; Mon, 23 Nov 2020 05:13:40 -0800 (PST) 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:mime-version:content-transfer-encoding; bh=mNpTnhqVT/lkkDzOBStmitBv9qqoRpOHiUUiZ8tRN8I=; b=AhyZv/rxrnG/5Bx+XqAJSDbVMFpRmKeFSWv9bI5bsqsp7xPuMb4vwQCeNmu3EmggJp XWEoOYHVyvyoMmWJ6xRrheAmj/5Q/uyQ/2ldiPwjMo+1VvqXq/tOGkCpruLFWbKXL82l 8Tez2Sci8EzLhGmGdGpbmOpMrB+iZi0W+o+SspCVebGb8gaMkqOGWXocn1YlKrRxNVc0 BMr3WuK8DTnWKVdx6KCOk7myYPGCITh4uHBP5Pl+jDDOB8ZrLPeVfCPoLrhN68T38gA3 EBnlbY/xdRf/fwoZkFqgsPFb8gCykIk0LTRTqxfAa26i0fv3sYiPq5Ivu7+vuN+yrLQB vreQ== X-Gm-Message-State: AOAM530u73r700ROgjsW7PQ4P5r7YhHCW3N+1KS/eN7xs+TwEll7sylX kt3BPIWqQZFGjM+UasS9Syj0/sLs9KylXf4fT8u+9AMeCJiguzGM2EBfQbrpHvvHzvkpBqlNBPj 0TZjH02P7488= X-Received: by 2002:a17:90b:b15:: with SMTP id bf21mr19755400pjb.21.1606137219442; Mon, 23 Nov 2020 05:13:39 -0800 (PST) X-Google-Smtp-Source: ABdhPJwBOG0l/Zu18yg12VyJ5W4oaN/nbRzZ/hwb8daUy6SRR3YTXPEHc0TkzVBcKsFuBUiomdjA+Q== X-Received: by 2002:a17:90b:b15:: with SMTP id bf21mr19755375pjb.21.1606137219218; Mon, 23 Nov 2020 05:13:39 -0800 (PST) Received: from localhost.localdomain.com ([209.132.188.80]) by smtp.gmail.com with ESMTPSA id 84sm12075505pfu.53.2020.11.23.05.13.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Nov 2020 05:13:38 -0800 (PST) From: Hangbin Liu To: Stephen Hemminger , David Ahern Cc: Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , David Miller , netdev@vger.kernel.org, bpf@vger.kernel.org, Jiri Benc , =?utf-8?q?Toke_H=C3=B8il?= =?utf-8?q?and-J=C3=B8rgensen?= , Jesper Dangaard Brouer , Alexei Starovoitov , Hangbin Liu Subject: [PATCHv6 iproute2-next 5/5] examples/bpf: add bpf examples with BTF defined maps Date: Mon, 23 Nov 2020 21:12:01 +0800 Message-Id: <20201123131201.4108483-6-haliu@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: <20201123131201.4108483-1-haliu@redhat.com> References: <20201116065305.1010651-1-haliu@redhat.com> <20201123131201.4108483-1-haliu@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: dsahern@gmail.com Users should try use the new BTF defined maps instead of struct bpf_elf_map defined maps. The tail call examples are not added yet as libbpf doesn't currently support declaratively populating tail call maps. Reviewed-by: Toke Høiland-Jørgensen Signed-off-by: Hangbin Liu --- examples/bpf/README | 6 ++++ examples/bpf/bpf_graft.c | 66 +++++++++++++++++++++++++++++++++++ examples/bpf/bpf_map_in_map.c | 55 +++++++++++++++++++++++++++++ examples/bpf/bpf_shared.c | 53 ++++++++++++++++++++++++++++ include/bpf_api.h | 13 +++++++ 5 files changed, 193 insertions(+) create mode 100644 examples/bpf/bpf_graft.c create mode 100644 examples/bpf/bpf_map_in_map.c create mode 100644 examples/bpf/bpf_shared.c diff --git a/examples/bpf/README b/examples/bpf/README index 732bcc83..b7261191 100644 --- a/examples/bpf/README +++ b/examples/bpf/README @@ -1,6 +1,12 @@ eBPF toy code examples (running in kernel) to familiarize yourself with syntax and features: +- BTF defined map examples + - bpf_graft.c -> Demo on altering runtime behaviour + - bpf_shared.c -> Ingress/egress map sharing example + - bpf_map_in_map.c -> Using map in map example + +- legacy struct bpf_elf_map defined map examples - legacy/bpf_shared.c -> Ingress/egress map sharing example - legacy/bpf_tailcall.c -> Using tail call chains - legacy/bpf_cyclic.c -> Simple cycle as tail calls diff --git a/examples/bpf/bpf_graft.c b/examples/bpf/bpf_graft.c new file mode 100644 index 00000000..8066dcce --- /dev/null +++ b/examples/bpf/bpf_graft.c @@ -0,0 +1,66 @@ +#include "../../include/bpf_api.h" + +/* This example demonstrates how classifier run-time behaviour + * can be altered with tail calls. We start out with an empty + * jmp_tc array, then add section aaa to the array slot 0, and + * later on atomically replace it with section bbb. Note that + * as shown in other examples, the tc loader can prepopulate + * tail called sections, here we start out with an empty one + * on purpose to show it can also be done this way. + * + * tc filter add dev foo parent ffff: bpf obj graft.o + * tc exec bpf dbg + * [...] + * Socket Thread-20229 [001] ..s. 138993.003923: : fallthrough + * -0 [001] ..s. 138993.202265: : fallthrough + * Socket Thread-20229 [001] ..s. 138994.004149: : fallthrough + * [...] + * + * tc exec bpf graft m:globals/jmp_tc key 0 obj graft.o sec aaa + * tc exec bpf dbg + * [...] + * Socket Thread-19818 [002] ..s. 139012.053587: : aaa + * -0 [002] ..s. 139012.172359: : aaa + * Socket Thread-19818 [001] ..s. 139012.173556: : aaa + * [...] + * + * tc exec bpf graft m:globals/jmp_tc key 0 obj graft.o sec bbb + * tc exec bpf dbg + * [...] + * Socket Thread-19818 [002] ..s. 139022.102967: : bbb + * -0 [002] ..s. 139022.155640: : bbb + * Socket Thread-19818 [001] ..s. 139022.156730: : bbb + * [...] + */ + +struct { + __uint(type, BPF_MAP_TYPE_PROG_ARRAY); + __uint(key_size, sizeof(uint32_t)); + __uint(value_size, sizeof(uint32_t)); + __uint(max_entries, 1); + __uint(pinning, LIBBPF_PIN_BY_NAME); +} jmp_tc __section(".maps"); + +__section("aaa") +int cls_aaa(struct __sk_buff *skb) +{ + printt("aaa\n"); + return TC_H_MAKE(1, 42); +} + +__section("bbb") +int cls_bbb(struct __sk_buff *skb) +{ + printt("bbb\n"); + return TC_H_MAKE(1, 43); +} + +__section_cls_entry +int cls_entry(struct __sk_buff *skb) +{ + tail_call(skb, &jmp_tc, 0); + printt("fallthrough\n"); + return BPF_H_DEFAULT; +} + +BPF_LICENSE("GPL"); diff --git a/examples/bpf/bpf_map_in_map.c b/examples/bpf/bpf_map_in_map.c new file mode 100644 index 00000000..39c86268 --- /dev/null +++ b/examples/bpf/bpf_map_in_map.c @@ -0,0 +1,55 @@ +#include "../../include/bpf_api.h" + +struct inner_map { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(key_size, sizeof(uint32_t)); + __uint(value_size, sizeof(uint32_t)); + __uint(max_entries, 1); +} map_inner __section(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS); + __uint(key_size, sizeof(uint32_t)); + __uint(value_size, sizeof(uint32_t)); + __uint(max_entries, 1); + __uint(pinning, LIBBPF_PIN_BY_NAME); + __array(values, struct inner_map); +} map_outer __section(".maps") = { + .values = { + [0] = &map_inner, + }, +}; + +__section("egress") +int emain(struct __sk_buff *skb) +{ + struct bpf_elf_map *map_inner; + int key = 0, *val; + + map_inner = map_lookup_elem(&map_outer, &key); + if (map_inner) { + val = map_lookup_elem(map_inner, &key); + if (val) + lock_xadd(val, 1); + } + + return BPF_H_DEFAULT; +} + +__section("ingress") +int imain(struct __sk_buff *skb) +{ + struct bpf_elf_map *map_inner; + int key = 0, *val; + + map_inner = map_lookup_elem(&map_outer, &key); + if (map_inner) { + val = map_lookup_elem(map_inner, &key); + if (val) + printt("map val: %d\n", *val); + } + + return BPF_H_DEFAULT; +} + +BPF_LICENSE("GPL"); diff --git a/examples/bpf/bpf_shared.c b/examples/bpf/bpf_shared.c new file mode 100644 index 00000000..99a332f4 --- /dev/null +++ b/examples/bpf/bpf_shared.c @@ -0,0 +1,53 @@ +#include "../../include/bpf_api.h" + +/* Minimal, stand-alone toy map pinning example: + * + * clang -target bpf -O2 [...] -o bpf_shared.o -c bpf_shared.c + * tc filter add dev foo parent 1: bpf obj bpf_shared.o sec egress + * tc filter add dev foo parent ffff: bpf obj bpf_shared.o sec ingress + * + * Both classifier will share the very same map instance in this example, + * so map content can be accessed from ingress *and* egress side! + * + * This example has a pinning of PIN_OBJECT_NS, so it's private and + * thus shared among various program sections within the object. + * + * A setting of PIN_GLOBAL_NS would place it into a global namespace, + * so that it can be shared among different object files. A setting + * of PIN_NONE (= 0) means no sharing, so each tc invocation a new map + * instance is being created. + */ + +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(key_size, sizeof(uint32_t)); + __uint(value_size, sizeof(uint32_t)); + __uint(max_entries, 1); + __uint(pinning, LIBBPF_PIN_BY_NAME); /* or LIBBPF_PIN_NONE */ +} map_sh __section(".maps"); + +__section("egress") +int emain(struct __sk_buff *skb) +{ + int key = 0, *val; + + val = map_lookup_elem(&map_sh, &key); + if (val) + lock_xadd(val, 1); + + return BPF_H_DEFAULT; +} + +__section("ingress") +int imain(struct __sk_buff *skb) +{ + int key = 0, *val; + + val = map_lookup_elem(&map_sh, &key); + if (val) + printt("map val: %d\n", *val); + + return BPF_H_DEFAULT; +} + +BPF_LICENSE("GPL"); diff --git a/include/bpf_api.h b/include/bpf_api.h index 89d3488d..82c47089 100644 --- a/include/bpf_api.h +++ b/include/bpf_api.h @@ -19,6 +19,19 @@ #include "bpf_elf.h" +/** libbpf pin type. */ +enum libbpf_pin_type { + LIBBPF_PIN_NONE, + /* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */ + LIBBPF_PIN_BY_NAME, +}; + +/** Type helper macros. */ + +#define __uint(name, val) int (*name)[val] +#define __type(name, val) typeof(val) *name +#define __array(name, val) typeof(val) *name[] + /** Misc macros. */ #ifndef __stringify