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=-10.1 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, 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 B5388C54FCB for ; Fri, 24 Apr 2020 02:12:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 94F4020CC7 for ; Fri, 24 Apr 2020 02:12:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1587694336; bh=nss2/GkzejrMBKoaESsf7nHmImMndCIo+/mk0139eVU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=wyR/YBAs7NqNz5+PbEXXbjvMYHW54bi2T0twwvljXCAB3TuGz2byKikKM6ceDw6aD jUcM0TwDZUDYlmpZYbGREDVo1qKlMxUuKVgXbtIr9H6ciQJY3bjgbCn4WyrVaMaT6u DTFjT4Fn7QDMXL2pIw+8SCG5pqlYPEEEL7RIsKqE= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726496AbgDXCMQ (ORCPT ); Thu, 23 Apr 2020 22:12:16 -0400 Received: from mail.kernel.org ([198.145.29.99]:37348 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726462AbgDXCML (ORCPT ); Thu, 23 Apr 2020 22:12:11 -0400 Received: from C02YQ0RWLVCF.internal.digitalocean.com (c-73-181-34-237.hsd1.co.comcast.net [73.181.34.237]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 338292087E; Fri, 24 Apr 2020 02:12:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1587694330; bh=nss2/GkzejrMBKoaESsf7nHmImMndCIo+/mk0139eVU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=K0zcJl5MM3dHlWL+5LFL7oXdf+wRpozFM5+HwlSKZU0AErhvi3PhMymnupRsaAC9M 0iyOSrUFQ61Ppb6VUlaoZgGTdSgWEf4BmDddgLdYH0+ithdAelpKWbl3z2oRIPQly0 HV4NC7lNRRRJla1eEJMCbrVUl1GPo+5601wZbqnM= From: David Ahern To: netdev@vger.kernel.org Cc: davem@davemloft.net, kuba@kernel.org, prashantbhole.linux@gmail.com, jasowang@redhat.com, brouer@redhat.com, toke@redhat.com, toshiaki.makita1@gmail.com, daniel@iogearbox.net, john.fastabend@gmail.com, ast@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, andriin@fb.com, dsahern@gmail.com, David Ahern Subject: [PATCH v2 bpf-next 13/17] libbpf: Add egress XDP support Date: Thu, 23 Apr 2020 20:11:44 -0600 Message-Id: <20200424021148.83015-14-dsahern@kernel.org> X-Mailer: git-send-email 2.21.1 (Apple Git-122.3) In-Reply-To: <20200424021148.83015-1-dsahern@kernel.org> References: <20200424021148.83015-1-dsahern@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: David Ahern Patch adds egress XDP support in libbpf. New section name hint, xdp_egress, is added to set expected attach type at program load. Programs can use xdp_egress as the prefix in the SEC statement to load the program with the BPF_XDP_EGRESS attach type set. egress is added to bpf_xdp_set_link_opts to specify egress type for use with bpf_set_link_xdp_fd_opts. Update library side to check for flag and set nla_type to IFLA_XDP_EGRESS. Add egress version of bpf_get_link_xdp_id to return id of program attached to the device in the egress direction. Signed-off-by: Prashant Bhole Co-developed-by: David Ahern Signed-off-by: David Ahern --- tools/lib/bpf/libbpf.c | 2 ++ tools/lib/bpf/libbpf.h | 6 +++++- tools/lib/bpf/libbpf.map | 5 +++++ tools/lib/bpf/netlink.c | 43 ++++++++++++++++++++++++++++++++++------ 4 files changed, 49 insertions(+), 7 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 8f480e29a6b0..32fc970495d9 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -6366,6 +6366,8 @@ static const struct bpf_sec_def section_defs[] = { .is_attach_btf = true, .expected_attach_type = BPF_LSM_MAC, .attach_fn = attach_lsm), + BPF_EAPROG_SEC("xdp_egress", BPF_PROG_TYPE_XDP, + BPF_XDP_EGRESS), BPF_PROG_SEC("xdp", BPF_PROG_TYPE_XDP), BPF_PROG_SEC("perf_event", BPF_PROG_TYPE_PERF_EVENT), BPF_PROG_SEC("lwt_in", BPF_PROG_TYPE_LWT_IN), diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index f1dacecb1619..dc58a071b964 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -454,13 +454,15 @@ struct xdp_link_info { __u32 hw_prog_id; __u32 skb_prog_id; __u8 attach_mode; + __u32 egress_core_prog_id; }; struct bpf_xdp_set_link_opts { size_t sz; int old_fd; + bool egress; }; -#define bpf_xdp_set_link_opts__last_field old_fd +#define bpf_xdp_set_link_opts__last_field egress LIBBPF_API int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags); LIBBPF_API int bpf_set_link_xdp_fd_opts(int ifindex, int fd, __u32 flags, @@ -468,6 +470,8 @@ LIBBPF_API int bpf_set_link_xdp_fd_opts(int ifindex, int fd, __u32 flags, LIBBPF_API int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags); LIBBPF_API int bpf_get_link_xdp_info(int ifindex, struct xdp_link_info *info, size_t info_size, __u32 flags); +LIBBPF_API int bpf_get_link_xdp_egress_id(int ifindex, __u32 *prog_id, + __u32 flags); struct perf_buffer; diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index bb8831605b25..cc2b33ba824e 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -254,3 +254,8 @@ LIBBPF_0.0.8 { bpf_program__set_lsm; bpf_set_link_xdp_fd_opts; } LIBBPF_0.0.7; + +LIBBPF_0.0.9 { + global: + bpf_get_link_xdp_egress_id; +} LIBBPF_0.0.8; diff --git a/tools/lib/bpf/netlink.c b/tools/lib/bpf/netlink.c index 90ced689e53b..714e23e3564e 100644 --- a/tools/lib/bpf/netlink.c +++ b/tools/lib/bpf/netlink.c @@ -133,7 +133,7 @@ static int bpf_netlink_recv(int sock, __u32 nl_pid, int seq, } static int __bpf_set_link_xdp_fd_replace(int ifindex, int fd, int old_fd, - __u32 flags) + __u32 flags, __u16 nla_type) { int sock, seq = 0, ret; struct nlattr *nla, *nla_xdp; @@ -160,7 +160,7 @@ static int __bpf_set_link_xdp_fd_replace(int ifindex, int fd, int old_fd, /* started nested attribute for XDP */ nla = (struct nlattr *)(((char *)&req) + NLMSG_ALIGN(req.nh.nlmsg_len)); - nla->nla_type = NLA_F_NESTED | IFLA_XDP; + nla->nla_type = NLA_F_NESTED | nla_type; nla->nla_len = NLA_HDRLEN; /* add XDP fd */ @@ -203,7 +203,9 @@ static int __bpf_set_link_xdp_fd_replace(int ifindex, int fd, int old_fd, int bpf_set_link_xdp_fd_opts(int ifindex, int fd, __u32 flags, const struct bpf_xdp_set_link_opts *opts) { + __u16 nla_type = IFLA_XDP; int old_fd = -1; + bool egress; if (!OPTS_VALID(opts, bpf_xdp_set_link_opts)) return -EINVAL; @@ -213,14 +215,17 @@ int bpf_set_link_xdp_fd_opts(int ifindex, int fd, __u32 flags, flags |= XDP_FLAGS_REPLACE; } - return __bpf_set_link_xdp_fd_replace(ifindex, fd, - old_fd, - flags); + egress = OPTS_GET(opts, egress, false); + if (egress) + nla_type = IFLA_XDP_EGRESS; + + return __bpf_set_link_xdp_fd_replace(ifindex, fd, old_fd, flags, + nla_type); } int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags) { - return __bpf_set_link_xdp_fd_replace(ifindex, fd, 0, flags); + return __bpf_set_link_xdp_fd_replace(ifindex, fd, 0, flags, IFLA_XDP); } static int __dump_link_nlmsg(struct nlmsghdr *nlh, @@ -271,6 +276,10 @@ static int process_xdp_attr(struct nlattr *tb, struct xdp_link_info *info) info->hw_prog_id = libbpf_nla_getattr_u32( xdp_tb[IFLA_XDP_HW_PROG_ID]); + if (xdp_tb[IFLA_XDP_EGRESS_CORE_PROG_ID]) + info->egress_core_prog_id = libbpf_nla_getattr_u32( + xdp_tb[IFLA_XDP_EGRESS_CORE_PROG_ID]); + return 0; } @@ -289,6 +298,12 @@ static int get_xdp_info(void *cookie, void *msg, struct nlattr **tb) return ret; } + if (tb[IFLA_XDP_EGRESS]) { + ret = process_xdp_attr(tb[IFLA_XDP_EGRESS], &xdp_id->info); + if (ret) + return ret; + } + return 0; } @@ -354,6 +369,22 @@ int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags) return ret; } +int bpf_get_link_xdp_egress_id(int ifindex, __u32 *prog_id, __u32 flags) +{ + struct xdp_link_info info = {}; + int ret; + + /* egress path does not support SKB, DRV or HW mode */ + if (flags & XDP_FLAGS_MODES) + return -EINVAL; + + ret = bpf_get_link_xdp_info(ifindex, &info, sizeof(info), flags); + if (!ret) + *prog_id = info.egress_core_prog_id; + + return ret; +} + int libbpf_nl_get_link(int sock, unsigned int nl_pid, libbpf_dump_nlmsg_t dump_link_nlmsg, void *cookie) { -- 2.21.1 (Apple Git-122.3)