From patchwork Fri Oct 23 03:38:51 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Hangbin Liu X-Patchwork-Id: 11852215 X-Patchwork-Delegate: bpf@iogearbox.net 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=-12.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,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 795F0C56201 for ; Fri, 23 Oct 2020 03:39:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 21CAB221EB for ; Fri, 23 Oct 2020 03:39:25 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ZxW0+OMa" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S374950AbgJWDjY (ORCPT ); Thu, 22 Oct 2020 23:39:24 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:36150 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S374886AbgJWDjX (ORCPT ); Thu, 22 Oct 2020 23:39:23 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1603424363; 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=B3C3SM+mTODeDpoZqXrRuy+dLn2cT9haezbBAbLVhV4=; b=ZxW0+OMaAjRRqZpXwm1DTALDm653bAh6eEnLuXZML3DgCxS4xBkec4K0sB5uMBfm2Q1W9R XPk070Lv6XKnC3womDZaskktbdIh63JvFoub9oXXVrvrV1O77oisZHfOordMiwGqLn28SD BAk1p2YkTFZztzIgbMnuCVodXZFW1fs= Received: from mail-pf1-f197.google.com (mail-pf1-f197.google.com [209.85.210.197]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-310-AEXQAsT_PRiSYr47s74jNA-1; Thu, 22 Oct 2020 23:39:21 -0400 X-MC-Unique: AEXQAsT_PRiSYr47s74jNA-1 Received: by mail-pf1-f197.google.com with SMTP id u24so26530pfh.4 for ; Thu, 22 Oct 2020 20:39:21 -0700 (PDT) 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=B3C3SM+mTODeDpoZqXrRuy+dLn2cT9haezbBAbLVhV4=; b=iVLTB6BBB2/65lOASMUyfUJY7Io1k/n7sO4q2+0dJ/bYSuCfA7w1vFTbZzDURzbgPB 0nfSEBceUonSLaJS4TGCua0wuUZOqZeJg/gps76/WK45RgzHTODXLb5xXPcM9S6hn7gx uIMzwx7eCq6L4xZz4ohxgfvTyvp9nrDY6xuc1zmFJDCOHPPH1tgkUTtq6VIvs2WWiIHK Fmt7vryb8lsqSyM2/zGkb5hHOrKF8y+nZLpcbC5gJJ9XcNMHkvZjTfoqCyEITPDugAmq 5v+H/F6qUbTJrGngND6F9zTrVKQUGv5KDeJm/40VR+UkXFoS6iLSVMOInQWyzB4GxAqN pxmw== X-Gm-Message-State: AOAM533UT6p6rDgriYQ3VLDQ3gEv19syZRPBv2Nm+cGbhaCaRQWyxAv9 t9k4krNlPNNTvUnw2hxpPJH3SdnGvDVKanr6y7CQIJAY/+7w9Bmcd1w1TwBBwHABFW45ZQztqWY z/QX1Eo4gBD4= X-Received: by 2002:a17:902:7102:b029:d3:ef48:e51e with SMTP id a2-20020a1709027102b02900d3ef48e51emr381258pll.72.1603424360479; Thu, 22 Oct 2020 20:39:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwQDklxYjdiPoiDKucBCtpdVJp7y3DdzO7/hWtma/5zMPURfCb/2pQ1Ulp7RYDQXxssh+BnTA== X-Received: by 2002:a17:902:7102:b029:d3:ef48:e51e with SMTP id a2-20020a1709027102b02900d3ef48e51emr381242pll.72.1603424360239; Thu, 22 Oct 2020 20:39:20 -0700 (PDT) Received: from localhost.localdomain.com ([209.132.188.80]) by smtp.gmail.com with ESMTPSA id e23sm185442pfi.191.2020.10.22.20.39.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Oct 2020 20:39:19 -0700 (PDT) From: Hangbin Liu To: Stephen Hemminger , Daniel Borkmann , David Ahern , Alexei Starovoitov Cc: Martin KaFai Lau , Song Liu , Yonghong Song , David Miller , Jesper Dangaard Brouer , netdev@vger.kernel.org, bpf@vger.kernel.org, Jiri Benc , Andrii Nakryiko , =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rge?= =?utf-8?q?nsen?= , Hangbin Liu Subject: [PATCH iproute2-next 1/5] configure: add check_libbpf() for later libbpf support Date: Fri, 23 Oct 2020 11:38:51 +0800 Message-Id: <20201023033855.3894509-2-haliu@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: <20201023033855.3894509-1-haliu@redhat.com> References: <20201023033855.3894509-1-haliu@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org This patch adds a check to see if we support libbpf. By default the system libbpf will be used, but static linking against a custom libbpf version can be achieved by passing LIBBPF_DIR to configure. FORCE_LIBBPF can be set to force configure to abort if no suitable libbpf is found, which is useful for automatic packaging that wants to enforce the dependency. Reviewed-by: Toke Høiland-Jørgensen Signed-off-by: Hangbin Liu --- configure | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/configure b/configure index 307912aa..77f475d9 100755 --- a/configure +++ b/configure @@ -240,6 +240,51 @@ check_elf() fi } +check_libbpf() +{ + if ${PKG_CONFIG} libbpf --exists || [ -n "$LIBBPF_DIR" ] ; then + + if [ -n "$LIBBPF_DIR" ]; then + LIBBPF_CFLAGS="-I${LIBBPF_DIR}/include -L${LIBBPF_DIR}/lib64" + LIBBPF_LDLIBS="${LIBBPF_DIR}/lib64/libbpf.a -lz -lelf" + else + LIBBPF_CFLAGS=$(${PKG_CONFIG} libbpf --cflags) + LIBBPF_LDLIBS=$(${PKG_CONFIG} libbpf --libs) + fi + + cat >$TMPDIR/libbpftest.c < +int main(int argc, char **argv) { + void *ptr; + DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts, .relaxed_maps = true, .pin_root_path = "/path"); + (void) bpf_object__open_file("file", &opts); + (void) bpf_map__name(ptr); + (void) bpf_map__ifindex(ptr); + (void) bpf_map__reuse_fd(ptr, 0); + (void) bpf_map__pin(ptr, "/path"); + return 0; +} +EOF + + if $CC -o $TMPDIR/libbpftest $TMPDIR/libbpftest.c $LIBBPF_CFLAGS -lbpf 2>&1; then + echo "HAVE_LIBBPF:=y" >>$CONFIG + echo 'CFLAGS += -DHAVE_LIBBPF ' $LIBBPF_CFLAGS >> $CONFIG + echo 'LDLIBS += ' $LIBBPF_LDLIBS >>$CONFIG + echo "yes" + return 0 + fi + fi + + echo "no" + + # if set FORCE_LIBBPF but no libbpf support, just exist the config + # process to make sure we don't build without libbpf. + if [ -n "$FORCE_LIBBPF" ]; then + echo "FORCE_LIBBPF set, but couldn't find a usable libbpf" + exit 1 + fi +} + check_selinux() # SELinux is a compile time option in the ss utility { @@ -385,6 +430,9 @@ check_setns echo -n "SELinux support: " check_selinux +echo -n "libbpf support: " +check_libbpf + echo -n "ELF support: " check_elf From patchwork Fri Oct 23 03:38:52 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Hangbin Liu X-Patchwork-Id: 11852219 X-Patchwork-Delegate: bpf@iogearbox.net 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=-12.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,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 CFA53C388F7 for ; Fri, 23 Oct 2020 03:39:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5E259207C4 for ; Fri, 23 Oct 2020 03:39:31 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="CwOeH3EN" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S374957AbgJWDja (ORCPT ); Thu, 22 Oct 2020 23:39:30 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:29198 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S374955AbgJWDj3 (ORCPT ); Thu, 22 Oct 2020 23:39:29 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1603424367; 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=M7X83YXQrTBsbNhEbcdOm/nIWlpTd9yUgaPY4aMl/O4=; b=CwOeH3ENTztRZDSy7S8czrec/oCKk8ZFbT59jqVE1E+Zr8h74FAdCeNuiye4PwWtbFXORL PJMWonwvKGI+w2DeuL0MJh1AFMvCAQh+VAEkIXrSbyDWb6WKstHvuPx6EDS5FiCw2ucjBK imc5MbARYuiDe8L5aCHUlQ9C1JRqXwM= Received: from mail-pl1-f198.google.com (mail-pl1-f198.google.com [209.85.214.198]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-362-y3ISr9qvO4qLUg1dmmfy1g-1; Thu, 22 Oct 2020 23:39:26 -0400 X-MC-Unique: y3ISr9qvO4qLUg1dmmfy1g-1 Received: by mail-pl1-f198.google.com with SMTP id r9so158840plo.13 for ; Thu, 22 Oct 2020 20:39:26 -0700 (PDT) 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=M7X83YXQrTBsbNhEbcdOm/nIWlpTd9yUgaPY4aMl/O4=; b=BwFDJwz1uDS1y2XqFEmSEeI4cnldsqe0pLgXl/0armVEQtj2yg0vVZrmsWQbQsMDuw DCKygXpsS5JFoLYIct4q4tigv6ouJyfCcG2Wo2ufOgi+/nbmdACu1MAXLdQ2wROSsmnO gKFQAU+xAD2dKuGOk/fhqsgGB+bLH2sV04Pgx4UQJIkytu7tOBJl9mio4PzRgJyyAtZC K1He+7B59oJpCChpNPcv8Hycgx7afghybc9kEPLMCm3CMf9prcHXZbpW9UUjlMdAoY+f SXwwjFuBPOxWG2+08Gv8MLxo+RTXXD7D+iX9bxBo07KsU+IDI4OYPMkRz9gDu99+IU74 9PYw== X-Gm-Message-State: AOAM532mS4xL2ter0qik+BE9yHLeDL6x74i7eYFuIm2MiG7wfE1GjKEJ ayq2k8yGBjNHD1IgOR3UV9pyT/LmuZ6uvqF0ovK9VxqclT1OeBN8JBvOBWH84ukjfAXDcbUx7WY QhjJD5ljknbE= X-Received: by 2002:a17:90a:ec0c:: with SMTP id l12mr252275pjy.28.1603424365154; Thu, 22 Oct 2020 20:39:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxcRUn/glA9TRTzKrnRqfKZFDiZ3Xu+FZlXvWDtlcHwvo1DW0RP9TkJFBkK3HbwpbbYXbs47Q== X-Received: by 2002:a17:90a:ec0c:: with SMTP id l12mr252260pjy.28.1603424364979; Thu, 22 Oct 2020 20:39:24 -0700 (PDT) Received: from localhost.localdomain.com ([209.132.188.80]) by smtp.gmail.com with ESMTPSA id e23sm185442pfi.191.2020.10.22.20.39.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Oct 2020 20:39:24 -0700 (PDT) From: Hangbin Liu To: Stephen Hemminger , Daniel Borkmann , David Ahern , Alexei Starovoitov Cc: Martin KaFai Lau , Song Liu , Yonghong Song , David Miller , Jesper Dangaard Brouer , netdev@vger.kernel.org, bpf@vger.kernel.org, Jiri Benc , Andrii Nakryiko , =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rge?= =?utf-8?q?nsen?= , Hangbin Liu Subject: [PATCH iproute2-next 2/5] lib: rename bpf.c to bpf_legacy.c Date: Fri, 23 Oct 2020 11:38:52 +0800 Message-Id: <20201023033855.3894509-3-haliu@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: <20201023033855.3894509-1-haliu@redhat.com> References: <20201023033855.3894509-1-haliu@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org This is a preparation for later libbpf support in iproute2. Function bpf_prog_load() is also renamed to bpf_prog_load_buf() as there is a conflict with libbpf. Reviewed-by: Toke Høiland-Jørgensen Signed-off-by: Hangbin Liu --- include/bpf_util.h | 6 +++--- ip/ipvrf.c | 4 ++-- lib/Makefile | 2 +- lib/{bpf.c => bpf_legacy.c} | 6 +++--- 4 files changed, 9 insertions(+), 9 deletions(-) rename lib/{bpf.c => bpf_legacy.c} (99%) diff --git a/include/bpf_util.h b/include/bpf_util.h index 63db07ca..72d3a32c 100644 --- a/include/bpf_util.h +++ b/include/bpf_util.h @@ -274,9 +274,9 @@ 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_buf(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); diff --git a/ip/ipvrf.c b/ip/ipvrf.c index 28dd8e25..33150ac2 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_prog_load_buf(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) diff --git a/lib/Makefile b/lib/Makefile index 7cba1857..a326fb9f 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_legacy.o exec.o fs.o cg_map.o NLOBJ=libgenl.o libnetlink.o 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..2e6e0602 100644 --- a/lib/bpf.c +++ b/lib/bpf_legacy.c @@ -1109,9 +1109,9 @@ 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) +int bpf_prog_load_buf(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); From patchwork Fri Oct 23 03:38:53 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Hangbin Liu X-Patchwork-Id: 11852213 X-Patchwork-Delegate: bpf@iogearbox.net 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=-12.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable 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 DB2BCC388F9 for ; Fri, 23 Oct 2020 03:39:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7C38C22248 for ; Fri, 23 Oct 2020 03:39:38 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="W0P9odrk" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S374970AbgJWDjg (ORCPT ); Thu, 22 Oct 2020 23:39:36 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:37381 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S374956AbgJWDjg (ORCPT ); Thu, 22 Oct 2020 23:39:36 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1603424373; 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=8jsPm/FRZsQLcybrvMOSJl09SBL/lnuKMuihBTCPYj0=; b=W0P9odrkcZXIFraaAxzh1sRu+U8PkWFohucjlGylFVyqAnJUDTZ5RKbGjgkc9VDzQUmQyL IC82foapKXkUSoOjJGzp493XHHTo7TzI+rLp2N+If+WKBvtsR1y+Da9zrke01Tnb6AyBr5 oarT9DyDoMUX8Hi68EtRrSRJhR3to58= Received: from mail-pl1-f197.google.com (mail-pl1-f197.google.com [209.85.214.197]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-574-mQ8G2yh5MgyoIfC6aRYNKQ-1; Thu, 22 Oct 2020 23:39:31 -0400 X-MC-Unique: mQ8G2yh5MgyoIfC6aRYNKQ-1 Received: by mail-pl1-f197.google.com with SMTP id bb2so167194plb.8 for ; Thu, 22 Oct 2020 20:39:31 -0700 (PDT) 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=8jsPm/FRZsQLcybrvMOSJl09SBL/lnuKMuihBTCPYj0=; b=BY+u2S9QD+W13PqOz4QhwRzTk4cgvXQ6Rb4VGBoRntS06H/3H9Nr3L1k/iJnoz2YHt UzfpqPlZaofIc/0JlXRNXd6BqHqL9N/c/YodLe3YvvO98ke9JR78vbqZfCZnHwuIaUub 3dxPoUCVa4ZDIGwfDTz7tpCpypzJCvXJnVHg3Ra0PhywIWmJt4VOP2n0QhWsys0WKbY7 q4mQYlL4lurGIhukZFVmEHQEwkRa5BrtgiBa/HRgcG1SQo/QFYlNokErGqd+ecSJjoGU rIZOzLoJheN4AC1wMFaw42ojZkG7rCWb75sCStgpes/UgXN+tK2pS8dtQng580bJ2dae +DIA== X-Gm-Message-State: AOAM533M2wqsDqfB8TdgCu0mVNXSoMsS8l4Bw5ZHvWZkFPnvn0/hTsN2 bFVvu7L8ZZDTZhf2Yx4lVrMQS+K40RnjLhT36P+FnMPCt0U98or3QkBudxIe+L538QO4leeUGfg 5904ALuBu6m0= X-Received: by 2002:a17:90a:7d06:: with SMTP id g6mr220857pjl.113.1603424370124; Thu, 22 Oct 2020 20:39:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxwCOEnK3TDTT/QJ0nHCdJRjSRPW22FuskHSHz4gkVQzRfZDLl/ld4/2NVHpk3ftzfU3XvYrQ== X-Received: by 2002:a17:90a:7d06:: with SMTP id g6mr220835pjl.113.1603424369770; Thu, 22 Oct 2020 20:39:29 -0700 (PDT) Received: from localhost.localdomain.com ([209.132.188.80]) by smtp.gmail.com with ESMTPSA id e23sm185442pfi.191.2020.10.22.20.39.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Oct 2020 20:39:29 -0700 (PDT) From: Hangbin Liu To: Stephen Hemminger , Daniel Borkmann , David Ahern , Alexei Starovoitov Cc: Martin KaFai Lau , Song Liu , Yonghong Song , David Miller , Jesper Dangaard Brouer , netdev@vger.kernel.org, bpf@vger.kernel.org, Jiri Benc , Andrii Nakryiko , =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rge?= =?utf-8?q?nsen?= , Hangbin Liu Subject: [PATCH iproute2-next 3/5] lib: add libbpf support Date: Fri, 23 Oct 2020 11:38:53 +0800 Message-Id: <20201023033855.3894509-4-haliu@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: <20201023033855.3894509-1-haliu@redhat.com> References: <20201023033855.3894509-1-haliu@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org 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. Note: ip/ipvrf.c is not convert to use libbpf as it only encodes a few instructions and load 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 --- include/bpf_util.h | 11 ++ lib/Makefile | 4 + lib/bpf_legacy.c | 178 ++++++++++++++++++++++++ lib/bpf_libbpf.c | 338 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 531 insertions(+) create mode 100644 lib/bpf_libbpf.c diff --git a/include/bpf_util.h b/include/bpf_util.h index 72d3a32c..e200c107 100644 --- a/include/bpf_util.h +++ b/include/bpf_util.h @@ -300,4 +300,15 @@ static inline int bpf_recv_map_fds(const char *path, int *fds, return -1; } #endif /* HAVE_ELF */ + +#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 */ #endif /* __BPF_UTIL__ */ diff --git a/lib/Makefile b/lib/Makefile index a326fb9f..82d6e465 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -7,6 +7,10 @@ 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 exec.o fs.o cg_map.o +ifeq ($(HAVE_LIBBPF),y) +UTILOBJ += bpf_libbpf.o +endif + NLOBJ=libgenl.o libnetlink.o all: libnetlink.a libutil.a diff --git a/lib/bpf_legacy.c b/lib/bpf_legacy.c index 2e6e0602..c5ff3e32 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); @@ -3165,3 +3168,178 @@ int bpf_recv_map_fds(const char *path, int *fds, struct bpf_map_aux *aux, return ret; } #endif /* HAVE_ELF */ + +#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 */ diff --git a/lib/bpf_libbpf.c b/lib/bpf_libbpf.c new file mode 100644 index 00000000..9e3b9787 --- /dev/null +++ b/lib/bpf_libbpf.c @@ -0,0 +1,338 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include + +#include "bpf_util.h" + +#define MAX_ERRNO 4095 +#define IS_ERR_VALUE(x) ((x) >= (unsigned long)-MAX_ERRNO) + +static inline bool IS_ERR_OR_NULL(const void *ptr) +{ + return (!ptr) || IS_ERR_VALUE((unsigned long)ptr); +} + +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 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 = 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 = 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 (IS_ERR_OR_NULL(obj)) + 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(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 Fri Oct 23 03:38:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Hangbin Liu X-Patchwork-Id: 11852211 X-Patchwork-Delegate: bpf@iogearbox.net 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=-12.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=unavailable 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 773A0C388F7 for ; Fri, 23 Oct 2020 03:39:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2025E207C4 for ; Fri, 23 Oct 2020 03:39:43 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="d5UEDz1R" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S374963AbgJWDjm (ORCPT ); Thu, 22 Oct 2020 23:39:42 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:27979 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S374972AbgJWDjl (ORCPT ); Thu, 22 Oct 2020 23:39:41 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1603424380; 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=d5UEDz1R3AorbWVrF2YhJ9/pgYR2iG/Hbp5tnaQiRk7rsigZKX828qiWxiHU4gceTICkhL M76xSJ83icxXSC6Gz0Vq0L6Y5YxMojl7ii1jxs4/ykWMvB5PFiyES2Hwt2w+JrsH3BEajU ZGCmpJbEH735TA4ReB93/inFwS3ynaY= Received: from mail-pf1-f200.google.com (mail-pf1-f200.google.com [209.85.210.200]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-306-pcgJxn8IN2SnP0njz-OCyw-1; Thu, 22 Oct 2020 23:39:36 -0400 X-MC-Unique: pcgJxn8IN2SnP0njz-OCyw-1 Received: by mail-pf1-f200.google.com with SMTP id c21so9142pfd.16 for ; Thu, 22 Oct 2020 20:39:35 -0700 (PDT) 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=jmNFbss/T1B9jrVthYCaOu6oJvrDFUuSrwUbbWg36Ce4dVOQWSowsF5qMe0HnuxhsS qDJYn4MecXs9oI/QywDEL9nB0fXOeUIdwE/5ULoZLa4y2cQbGbwZloqDheHOMPmBEVB4 DqR4+5z0HtR3khJtJ+UOFiamaCLE0oLy1zJmgxxD75V5tqbLOtA2Ve+SjS5mQg1o559s /pL0RjKU3Gltd1RbSIgqjMNjgkkjbsbeCSgvwd3L8aoeH5NFZ+MmkYGrGiuCw+D+hlph LppJGkxer5cRefGDnJGrcIb8Qwbiu7AWzXdUi8mPfw9D9tpp4HcTYg23h1kOOZoOmTcu P2Fw== X-Gm-Message-State: AOAM532GYxh0w5IWIhHYaTATLRnnCyGoDWR64NpuLOq/Ifgx0fj7lJaP wYucwNqp+8m1HNixaufACRluQrLk/a7VkqRd6RwYXqGXtB7bQTvcEyQGMMX30T32v9vrXj2kgYa lZzKfJxew290= X-Received: by 2002:a17:90a:bc91:: with SMTP id x17mr208599pjr.113.1603424374901; Thu, 22 Oct 2020 20:39:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxb6X7iFVyBOdkc1P8IS4rXhXm/ksx4Fok6nmpT/8VOQN0G2/eemBNsS6kqSxGgEWt+nqqepg== X-Received: by 2002:a17:90a:bc91:: with SMTP id x17mr208582pjr.113.1603424374664; Thu, 22 Oct 2020 20:39:34 -0700 (PDT) Received: from localhost.localdomain.com ([209.132.188.80]) by smtp.gmail.com with ESMTPSA id e23sm185442pfi.191.2020.10.22.20.39.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Oct 2020 20:39:34 -0700 (PDT) From: Hangbin Liu To: Stephen Hemminger , Daniel Borkmann , David Ahern , Alexei Starovoitov Cc: Martin KaFai Lau , Song Liu , Yonghong Song , David Miller , Jesper Dangaard Brouer , netdev@vger.kernel.org, bpf@vger.kernel.org, Jiri Benc , Andrii Nakryiko , =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rge?= =?utf-8?q?nsen?= , Hangbin Liu Subject: [PATCH iproute2-next 4/5] examples/bpf: move struct bpf_elf_map defined maps to legacy folder Date: Fri, 23 Oct 2020 11:38:54 +0800 Message-Id: <20201023033855.3894509-5-haliu@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: <20201023033855.3894509-1-haliu@redhat.com> References: <20201023033855.3894509-1-haliu@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org 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 Fri Oct 23 03:38:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Hangbin Liu X-Patchwork-Id: 11852217 X-Patchwork-Delegate: bpf@iogearbox.net 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=-12.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,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 8799AC4363A for ; Fri, 23 Oct 2020 03:39:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1447A207C4 for ; Fri, 23 Oct 2020 03:39:48 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="DiFXm7lY" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S375003AbgJWDjr (ORCPT ); Thu, 22 Oct 2020 23:39:47 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:39488 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S374972AbgJWDjp (ORCPT ); Thu, 22 Oct 2020 23:39:45 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1603424382; 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=DiFXm7lYzRpONrMYAE+o2tq94B6PggkQ039BavbkjK/dulfA98fIy6ageaSmAFvEb28Y1D JbVEGi9lmDHCqxqGdcrssBWfWJ3PTdxswUcblA++oOEzfX9WBE7zan0msIlfcDMoU769Ej 4UO50yP/8iC6yiFtebhr9h+Srh5GZc8= 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-262-b0UPK3JgNFaOOshzQ4n1Zw-1; Thu, 22 Oct 2020 23:39:40 -0400 X-MC-Unique: b0UPK3JgNFaOOshzQ4n1Zw-1 Received: by mail-pf1-f199.google.com with SMTP id g24so32363pfo.1 for ; Thu, 22 Oct 2020 20:39:40 -0700 (PDT) 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=b2dT1nNomZXFF+L+laPuGyu/3y4TTI7yadVpBYPu7XyNr4nhmFCsoxB/myZKM+zFU6 UDjsivNERrRv4V/TPYl7llHogbknMmF4jBcuL5bf16FneM3uJzgB+6t/M4U8MF51eQNm o9yCAB42VvYUvvWXfQOiSPqpQMqCSUNe43Dj6Y8F5cy7WhRKNrO5XA4YUYMIaiNPtGh0 5JaKTTwi/IWLNa49WQ2wJepGjHg93C28a8tZpr/GF/zMB0Fm4rx4uadSJR/XGBQj9dWO 6UtUf/zriJ/mnkGzLgBd636xHA51+CPC2WkV6tMGnXm0T2Bmk0a7bqd+YOzJFiDu8gKo feCw== X-Gm-Message-State: AOAM533q1AOzTsFN/xe6MDVbjc8G/zm9jjo0peZDY3O9jPsXismKeR5F ACMXROJ0m8UfPx7AC+g2qtnaOzKiWS+1eV/WdYqlvoEXJ8OwmDlOMRjhQORXL3Qe8QJePvRKFAU Mu1ccoVXrGI0= X-Received: by 2002:a17:90a:fa96:: with SMTP id cu22mr231923pjb.80.1603424379755; Thu, 22 Oct 2020 20:39:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz4xiU89odVDnj7icVmrqB6X6PMVtfCkeN3FXBtqwJtLcmLO76O7PAANpizBcpXQC+F03suEw== X-Received: by 2002:a17:90a:fa96:: with SMTP id cu22mr231905pjb.80.1603424379502; Thu, 22 Oct 2020 20:39:39 -0700 (PDT) Received: from localhost.localdomain.com ([209.132.188.80]) by smtp.gmail.com with ESMTPSA id e23sm185442pfi.191.2020.10.22.20.39.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Oct 2020 20:39:38 -0700 (PDT) From: Hangbin Liu To: Stephen Hemminger , Daniel Borkmann , David Ahern , Alexei Starovoitov Cc: Martin KaFai Lau , Song Liu , Yonghong Song , David Miller , Jesper Dangaard Brouer , netdev@vger.kernel.org, bpf@vger.kernel.org, Jiri Benc , Andrii Nakryiko , =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rge?= =?utf-8?q?nsen?= , Hangbin Liu Subject: [PATCH iproute2-next 5/5] examples/bpf: add bpf examples with BTF defined maps Date: Fri, 23 Oct 2020 11:38:55 +0800 Message-Id: <20201023033855.3894509-6-haliu@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: <20201023033855.3894509-1-haliu@redhat.com> References: <20201023033855.3894509-1-haliu@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org 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