From mboxrd@z Thu Jan 1 00:00:00 1970 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 024CBC2D0A3 for ; Mon, 9 Nov 2020 07:09:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A07D420789 for ; Mon, 9 Nov 2020 07:09:17 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ZLRBMl8N" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729648AbgKIHJQ (ORCPT ); Mon, 9 Nov 2020 02:09:16 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:31345 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729645AbgKIHJQ (ORCPT ); Mon, 9 Nov 2020 02:09:16 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1604905754; 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=2wjlxhJNJX6q85v1x1b4SdM4od9GwiPjqobsc3U1ceo=; b=ZLRBMl8NopdRxAn3QU6m9fhG6D94Feom/rJ9HcR3rpLVki0875490daUHqR9mRbIU/YKvP X4FIytkwuHYIZiiZNoatp46MuaBE5PSEWd6/KQkVu27e0EZo9C4Uwd5jyOIAIkRuF6xAwX C6wmUPjaBj/MoeDWTMvu6d3yA6/GOj4= 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-528-q8BaTpXrOkCImfXdAn0YSQ-1; Mon, 09 Nov 2020 02:09:12 -0500 X-MC-Unique: q8BaTpXrOkCImfXdAn0YSQ-1 Received: by mail-pf1-f199.google.com with SMTP id d6so5849744pfn.15 for ; Sun, 08 Nov 2020 23:09:11 -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=2wjlxhJNJX6q85v1x1b4SdM4od9GwiPjqobsc3U1ceo=; b=Jf2W+kfw1cTrKPkDPm8QrQ9zyjcgsZGo9KvBUE7EncyXeGdcExP2JTVLSY1IDJqO+0 uPvkPj5TLLxqQJQFUZq4GlKItDzEEnPRSf2xHI4gqA+Ya0y8GaUBnp+7ULQ6LFpV4KyU P8WNi1/gnE2ns9TP0PCfdCzB+3TpvP1Oah0zkPQQONV+rM27L3k+tmmDohmlg6/dH+hE qDfucf/ws1aVmo76w2vup1h5wmBBp2T5ORggAwe40Fwu1cE3+byDlWIqxBhyHa8MwoUQ OHvC9DN68IsSXHJmX/sMASeJ4b43hcBiM09bjgpqV7Ma6L8n28G0StFjPOoyqDsG68zg N/qA== X-Gm-Message-State: AOAM533JxYIhEJOscQ4laBXZYQMkxUPWiBrI6AKU1cm+slxG9idN9X4c d8kcBsDdVs+2G/pDe4ZayUdOhH9E5iv9NuyVi/6FoEmiTPpPPr4yeZTFI5w6fzSiYSVPMAB14GB CvjomCBZyazw= X-Received: by 2002:a17:902:8685:b029:d7:bb:aa2 with SMTP id g5-20020a1709028685b02900d700bb0aa2mr11433826plo.13.1604905750981; Sun, 08 Nov 2020 23:09:10 -0800 (PST) X-Google-Smtp-Source: ABdhPJwbKAyOwrePJDSDko8yA3wWfUcx8GyndtnLQ9Jylh28aPIJHbtD5vznd0exoOgxZO7KpUeOjw== X-Received: by 2002:a17:902:8685:b029:d7:bb:aa2 with SMTP id g5-20020a1709028685b02900d700bb0aa2mr11433814plo.13.1604905750758; Sun, 08 Nov 2020 23:09:10 -0800 (PST) Received: from localhost.localdomain.com ([209.132.188.80]) by smtp.gmail.com with ESMTPSA id f17sm2492483pfk.70.2020.11.08.23.09.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Nov 2020 23:09:10 -0800 (PST) From: Hangbin Liu To: Stephen Hemminger , David Ahern Cc: Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , David Miller , Jesper Dangaard Brouer , netdev@vger.kernel.org, bpf@vger.kernel.org, Jiri Benc , =?UTF-8?q?Toke=20H=C3=B8iland-J=C3=B8rgensen?= , Hangbin Liu Subject: [PATCHv4 iproute2-next 2/5] lib: rename bpf.c to bpf_legacy.c Date: Mon, 9 Nov 2020 15:07:59 +0800 Message-Id: <20201109070802.3638167-3-haliu@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: <20201109070802.3638167-1-haliu@redhat.com> References: <20201029151146.3810859-1-haliu@redhat.com> <20201109070802.3638167-1-haliu@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org This is a preparation for later main libbpf support in iproute2. bpf.c is moved to bpf_legacy.c first. A new file bpf_glue.c is added which could call both legacy libbpf code. There are two wrapper functions added for ipvrf. Function bpf_prog_load() is removed as it's conflict with libbpf function name. v4: Add new file bpf_glue.c to for libbpf/legacy mixed bpf calls. v2-v3: no update Reviewed-by: Toke Høiland-Jørgensen Signed-off-by: Hangbin Liu --- include/bpf_util.h | 10 +++++++--- ip/ipvrf.c | 6 +++--- lib/Makefile | 2 +- lib/bpf_glue.c | 35 +++++++++++++++++++++++++++++++++++ lib/{bpf.c => bpf_legacy.c} | 15 +++------------ 5 files changed, 49 insertions(+), 19 deletions(-) create mode 100644 lib/bpf_glue.c rename lib/{bpf.c => bpf_legacy.c} (99%) diff --git a/include/bpf_util.h b/include/bpf_util.h index 63db07ca..82217cc6 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 7cba1857..c9502f6a 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 bpf_glue.o exec.o fs.o cg_map.o NLOBJ=libgenl.o libnetlink.o diff --git a/lib/bpf_glue.c b/lib/bpf_glue.c new file mode 100644 index 00000000..7626a893 --- /dev/null +++ b/lib/bpf_glue.c @@ -0,0 +1,35 @@ +/* + * bpf_glue.c BPF code to call both legacy and libbpf code + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version + * 2 of the License, or (at your option) any later version. + * + * Authors: Hangbin Liu + * + */ +#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_load_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 +} 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; -- 2.25.4