From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by mail.toke.dk (Postfix) with ESMTPS id 1C24F982D14 for ; Tue, 28 Jun 2022 21:49:33 +0200 (CEST) Authentication-Results: mail.toke.dk; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=aLI8duRc DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1656445774; x=1687981774; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=TNfBgVKLQyn2LgZtWghMUTiye2sgpvYA6TvdfH/NL10=; b=aLI8duRc1LPdsnCslT3zvmorIxyMNzgzynHCpBnc15221vIg04wPVNVg Xjp3Byp6LBkWx1Aajgb2wrzLKmw1A3ZeI0XpOR0E7x3dZEFT93OeBrgsj gVQ9NO4dZwp9XZFp4IQDOCq5Rx1seTqyi7vi+izywu0i6jDeyXluDVFGW 12PpRlHjhmY98FI0S31vN5u6FbqOy86rJaVD2dl1KslhLo9YjaoOlQijx KYrEjscanuBRMgwkTSLExzM8jQjnRCCZssBhIkqgf4xuQqBmTgR4N/HNM WhdX490nJla283AAdu+DuuoeMiJsNlbAfKIA3aFIGjlHxfh7Qqca4+6Mb w==; X-IronPort-AV: E=McAfee;i="6400,9594,10392"; a="270595828" X-IronPort-AV: E=Sophos;i="5.92,229,1650956400"; d="scan'208";a="270595828" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Jun 2022 12:49:16 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.92,229,1650956400"; d="scan'208";a="587988501" Received: from irvmail001.ir.intel.com ([10.43.11.63]) by orsmga007.jf.intel.com with ESMTP; 28 Jun 2022 12:49:11 -0700 Received: from newjersey.igk.intel.com (newjersey.igk.intel.com [10.102.20.203]) by irvmail001.ir.intel.com (8.14.3/8.13.6/MailSET/Hub) with ESMTP id 25SJmr9C022013; Tue, 28 Jun 2022 20:49:10 +0100 From: Alexander Lobakin To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Date: Tue, 28 Jun 2022 21:47:32 +0200 Message-Id: <20220628194812.1453059-13-alexandr.lobakin@intel.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220628194812.1453059-1-alexandr.lobakin@intel.com> References: <20220628194812.1453059-1-alexandr.lobakin@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: LZ7E6YGLZDVPX557ZQORLW3U2DYL5N74 X-Message-ID-Hash: LZ7E6YGLZDVPX557ZQORLW3U2DYL5N74 X-MailFrom: alexandr.lobakin@intel.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Alexander Lobakin , Larysa Zaremba , Michal Swiatkowski , Jesper Dangaard Brouer , =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= , Magnus Karlsson , Maciej Fijalkowski , Jonathan Lemon , Toke Hoiland-Jorgensen , Lorenzo Bianconi , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Jesse Brandeburg , John Fastabend , Yajun Deng , Willem de Bruijn , bpf@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, xdp-hints@xdp-project.net X-Mailman-Version: 3.3.5 Precedence: list Subject: [xdp-hints] [PATCH RFC bpf-next 12/52] libbpf: add ability to set the BTF/type ID on setting XDP prog List-Id: XDP hardware hints design discussion Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Covered functions: * bpf_link_create() - via &bpf_link_create_ops; * bpf_link_update() - via &bpf_link_update_ops; * bpf_xdp_attach() - via &bpf_xdp_attach_ops; * bpf_set_link_xdp_fd_opts() - via &bpf_xdp_set_link_opts; bpf_link_update() got the ability to pass arbitrary link type-specific data to the kernel, not just the old and new FDs. No support for bpf_get_link_xdp_info()/&xdp_link_info as we store additional data such as flags and BTF ID in the kernel in BPF link mode only. Signed-off-by: Alexander Lobakin --- tools/lib/bpf/bpf.c | 19 +++++++++++++++++++ tools/lib/bpf/bpf.h | 16 +++++++++++++++- tools/lib/bpf/libbpf.h | 8 ++++++-- tools/lib/bpf/netlink.c | 11 +++++++++++ 4 files changed, 51 insertions(+), 3 deletions(-) diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c index 240186aac8e6..6036dc75cc7b 100644 --- a/tools/lib/bpf/bpf.c +++ b/tools/lib/bpf/bpf.c @@ -805,6 +805,11 @@ int bpf_link_create(int prog_fd, int target_fd, if (!OPTS_ZEROED(opts, tracing)) return libbpf_err(-EINVAL); break; + case BPF_XDP: + attr.link_create.xdp.btf_id = OPTS_GET(opts, xdp.btf_id, 0); + if (!OPTS_ZEROED(opts, xdp)) + return libbpf_err(-EINVAL); + break; default: if (!OPTS_ZEROED(opts, flags)) return libbpf_err(-EINVAL); @@ -872,6 +877,20 @@ int bpf_link_update(int link_fd, int new_prog_fd, attr.link_update.flags = OPTS_GET(opts, flags, 0); attr.link_update.old_prog_fd = OPTS_GET(opts, old_prog_fd, 0); + /* As the union in both @attr and @opts is unnamed, just use a pointer + * to any of its members to copy the whole rest of the union/opts + */ + if (opts && opts->sz > offsetof(typeof(*opts), xdp)) { + __u32 attr_left, opts_left; + + attr_left = sizeof(attr.link_update) - + offsetof(typeof(attr.link_update), xdp); + opts_left = opts->sz - offsetof(typeof(*opts), xdp); + + memcpy(&attr.link_update.xdp, &opts->xdp, + min(attr_left, opts_left)); + } + ret = sys_bpf(BPF_LINK_UPDATE, &attr, sizeof(attr)); return libbpf_err_errno(ret); } diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h index cabc03703e29..4e17995fdaff 100644 --- a/tools/lib/bpf/bpf.h +++ b/tools/lib/bpf/bpf.h @@ -382,6 +382,10 @@ struct bpf_link_create_opts { struct { __u64 cookie; } tracing; + struct { + /* target metadata BTF + type ID */ + __aligned_u64 btf_id; + } xdp; }; size_t :0; }; @@ -397,8 +401,18 @@ struct bpf_link_update_opts { size_t sz; /* size of this struct for forward/backward compatibility */ __u32 flags; /* extra flags */ __u32 old_prog_fd; /* expected old program FD */ + /* must have the same layout as the same union from + * bpf_attr::link_update, uses direct memcpy() to there + */ + union { + struct { + /* new target metadata BTF + type ID */ + __aligned_u64 new_btf_id; + } xdp; + }; + size_t :0; }; -#define bpf_link_update_opts__last_field old_prog_fd +#define bpf_link_update_opts__last_field xdp.new_btf_id LIBBPF_API int bpf_link_update(int link_fd, int new_prog_fd, const struct bpf_link_update_opts *opts); diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index 4056e9038086..4f77128ba770 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -1193,9 +1193,11 @@ struct xdp_link_info { struct bpf_xdp_set_link_opts { size_t sz; int old_fd; + __u32 :32; + __u64 btf_id; size_t :0; }; -#define bpf_xdp_set_link_opts__last_field old_fd +#define bpf_xdp_set_link_opts__last_field btf_id LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_xdp_attach() instead") LIBBPF_API int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags); @@ -1211,9 +1213,11 @@ LIBBPF_API int bpf_get_link_xdp_info(int ifindex, struct xdp_link_info *info, struct bpf_xdp_attach_opts { size_t sz; int old_prog_fd; + __u32 :32; + __u64 btf_id; size_t :0; }; -#define bpf_xdp_attach_opts__last_field old_prog_fd +#define bpf_xdp_attach_opts__last_field btf_id struct bpf_xdp_query_opts { size_t sz; diff --git a/tools/lib/bpf/netlink.c b/tools/lib/bpf/netlink.c index 3a25178d0d12..104a809d5fb2 100644 --- a/tools/lib/bpf/netlink.c +++ b/tools/lib/bpf/netlink.c @@ -235,6 +235,7 @@ struct __bpf_set_link_xdp_fd_opts { int fd; int old_fd; __u32 flags; + __u64 btf_id; }; static int @@ -269,6 +270,12 @@ __bpf_set_link_xdp_fd_replace(const struct __bpf_set_link_xdp_fd_opts *opts) if (ret < 0) return ret; } + if (opts->btf_id) { + ret = nlattr_add(&req, IFLA_XDP_BTF_ID, &opts->btf_id, + sizeof(opts->btf_id)); + if (ret < 0) + return ret; + } nlattr_end_nested(&req, nla); return libbpf_netlink_send_recv(&req, NULL, NULL, NULL); @@ -292,6 +299,8 @@ int bpf_xdp_attach(int ifindex, int prog_fd, __u32 flags, const struct bpf_xdp_a else sl_opts.old_fd = -1; + sl_opts.btf_id = OPTS_GET(opts, btf_id, 0); + err = __bpf_set_link_xdp_fd_replace(&sl_opts); return libbpf_err(err); } @@ -320,6 +329,8 @@ int bpf_set_link_xdp_fd_opts(int ifindex, int fd, __u32 flags, flags |= XDP_FLAGS_REPLACE; } + sl_opts.btf_id = OPTS_GET(opts, btf_id, 0); + ret = __bpf_set_link_xdp_fd_replace(&sl_opts); return libbpf_err(ret); } -- 2.36.1