From: Magnus Karlsson <magnus.karlsson@gmail.com>
To: Mariusz Dudek <mariusz.dudek@gmail.com>
Cc: "Andrii Nakryiko" <andrii.nakryiko@gmail.com>,
"Karlsson, Magnus" <magnus.karlsson@intel.com>,
"Björn Töpel" <bjorn.topel@intel.com>,
"Alexei Starovoitov" <ast@kernel.org>,
"Daniel Borkmann" <daniel@iogearbox.net>,
"Network Development" <netdev@vger.kernel.org>,
"Jonathan Lemon" <jonathan.lemon@gmail.com>,
bpf <bpf@vger.kernel.org>,
"Mariusz Dudek" <mariuszx.dudek@intel.com>
Subject: Re: [PATCH v5 bpf-next 1/2] libbpf: separate XDP program load with xsk socket creation
Date: Fri, 27 Nov 2020 10:06:45 +0100 [thread overview]
Message-ID: <CAJ8uoz1TkEQ0-YJmqHaOULKvTGKbvfuMNB9c71vE-39RM6YvJw@mail.gmail.com> (raw)
In-Reply-To: <20201127082601.4762-2-mariuszx.dudek@intel.com>
On Fri, Nov 27, 2020 at 9:52 AM <mariusz.dudek@gmail.com> wrote:
>
> From: Mariusz Dudek <mariuszx.dudek@intel.com>
>
> Add support for separation of eBPF program load and xsk socket
> creation.
>
> This is needed for use-case when you want to privide as little
> privileges as possible to the data plane application that will
> handle xsk socket creation and incoming traffic.
>
> With this patch the data entity container can be run with only
> CAP_NET_RAW capability to fulfill its purpose of creating xsk
> socket and handling packages. In case your umem is larger or
> equal process limit for MEMLOCK you need either increase the
> limit or CAP_IPC_LOCK capability.
>
> To resolve privileges issue two APIs are introduced:
>
> - xsk_setup_xdp_prog - loads the built in XDP program. It can
> also return xsks_map_fd which is needed by unprivileged process
> to update xsks_map with AF_XDP socket "fd"
>
> - xsk_socket__update_xskmap - inserts an AF_XDP socket into an xskmap
> for a particular xsk_socket
>
> Signed-off-by: Mariusz Dudek <mariuszx.dudek@intel.com>
> ---
> tools/lib/bpf/libbpf.map | 2 +
> tools/lib/bpf/xsk.c | 92 ++++++++++++++++++++++++++++++++++++----
> tools/lib/bpf/xsk.h | 5 +++
> 3 files changed, 90 insertions(+), 9 deletions(-)
Acked-by: Magnus Karlsson <magnus.karlsson@intel.com>
> diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
> index 29ff4807b909..d939d5ac092e 100644
> --- a/tools/lib/bpf/libbpf.map
> +++ b/tools/lib/bpf/libbpf.map
> @@ -345,4 +345,6 @@ LIBBPF_0.3.0 {
> btf__parse_split;
> btf__new_empty_split;
> btf__new_split;
> + xsk_setup_xdp_prog;
> + xsk_socket__update_xskmap;
> } LIBBPF_0.2.0;
> diff --git a/tools/lib/bpf/xsk.c b/tools/lib/bpf/xsk.c
> index 9bc537d0b92d..4b051ec7cfbb 100644
> --- a/tools/lib/bpf/xsk.c
> +++ b/tools/lib/bpf/xsk.c
> @@ -566,8 +566,35 @@ static int xsk_set_bpf_maps(struct xsk_socket *xsk)
> &xsk->fd, 0);
> }
>
> -static int xsk_setup_xdp_prog(struct xsk_socket *xsk)
> +static int xsk_create_xsk_struct(int ifindex, struct xsk_socket *xsk)
> {
> + char ifname[IFNAMSIZ];
> + struct xsk_ctx *ctx;
> + char *interface;
> +
> + ctx = calloc(1, sizeof(*ctx));
> + if (!ctx)
> + return -ENOMEM;
> +
> + interface = if_indextoname(ifindex, &ifname[0]);
> + if (!interface) {
> + free(ctx);
> + return -errno;
> + }
> +
> + ctx->ifindex = ifindex;
> + strncpy(ctx->ifname, ifname, IFNAMSIZ - 1);
> + ctx->ifname[IFNAMSIZ - 1] = 0;
> +
> + xsk->ctx = ctx;
> +
> + return 0;
> +}
> +
> +static int __xsk_setup_xdp_prog(struct xsk_socket *_xdp,
> + int *xsks_map_fd)
> +{
> + struct xsk_socket *xsk = _xdp;
> struct xsk_ctx *ctx = xsk->ctx;
> __u32 prog_id = 0;
> int err;
> @@ -584,8 +611,7 @@ static int xsk_setup_xdp_prog(struct xsk_socket *xsk)
>
> err = xsk_load_xdp_prog(xsk);
> if (err) {
> - xsk_delete_bpf_maps(xsk);
> - return err;
> + goto err_load_xdp_prog;
> }
> } else {
> ctx->prog_fd = bpf_prog_get_fd_by_id(prog_id);
> @@ -598,15 +624,29 @@ static int xsk_setup_xdp_prog(struct xsk_socket *xsk)
> }
> }
>
> - if (xsk->rx)
> + if (xsk->rx) {
> err = xsk_set_bpf_maps(xsk);
> - if (err) {
> - xsk_delete_bpf_maps(xsk);
> - close(ctx->prog_fd);
> - return err;
> + if (err) {
> + if (!prog_id) {
> + goto err_set_bpf_maps;
> + } else {
> + close(ctx->prog_fd);
> + return err;
> + }
> + }
> }
> + if (xsks_map_fd)
> + *xsks_map_fd = ctx->xsks_map_fd;
>
> return 0;
> +
> +err_set_bpf_maps:
> + close(ctx->prog_fd);
> + bpf_set_link_xdp_fd(ctx->ifindex, -1, 0);
> +err_load_xdp_prog:
> + xsk_delete_bpf_maps(xsk);
> +
> + return err;
> }
>
> static struct xsk_ctx *xsk_get_ctx(struct xsk_umem *umem, int ifindex,
> @@ -689,6 +729,40 @@ static struct xsk_ctx *xsk_create_ctx(struct xsk_socket *xsk,
> return ctx;
> }
>
> +static void xsk_destroy_xsk_struct(struct xsk_socket *xsk)
> +{
> + free(xsk->ctx);
> + free(xsk);
> +}
> +
> +int xsk_socket__update_xskmap(struct xsk_socket *xsk, int fd)
> +{
> + xsk->ctx->xsks_map_fd = fd;
> + return xsk_set_bpf_maps(xsk);
> +}
> +
> +int xsk_setup_xdp_prog(int ifindex, int *xsks_map_fd)
> +{
> + struct xsk_socket *xsk;
> + int res;
> +
> + xsk = calloc(1, sizeof(*xsk));
> + if (!xsk)
> + return -ENOMEM;
> +
> + res = xsk_create_xsk_struct(ifindex, xsk);
> + if (res) {
> + free(xsk);
> + return -EINVAL;
> + }
> +
> + res = __xsk_setup_xdp_prog(xsk, xsks_map_fd);
> +
> + xsk_destroy_xsk_struct(xsk);
> +
> + return res;
> +}
> +
> int xsk_socket__create_shared(struct xsk_socket **xsk_ptr,
> const char *ifname,
> __u32 queue_id, struct xsk_umem *umem,
> @@ -838,7 +912,7 @@ int xsk_socket__create_shared(struct xsk_socket **xsk_ptr,
> ctx->prog_fd = -1;
>
> if (!(xsk->config.libbpf_flags & XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD)) {
> - err = xsk_setup_xdp_prog(xsk);
> + err = __xsk_setup_xdp_prog(xsk, NULL);
> if (err)
> goto out_mmap_tx;
> }
> diff --git a/tools/lib/bpf/xsk.h b/tools/lib/bpf/xsk.h
> index 1719a327e5f9..10b4259f8875 100644
> --- a/tools/lib/bpf/xsk.h
> +++ b/tools/lib/bpf/xsk.h
> @@ -207,6 +207,11 @@ struct xsk_umem_config {
> __u32 flags;
> };
>
> +LIBBPF_API int xsk_setup_xdp_prog(int ifindex,
> + int *xsks_map_fd);
> +LIBBPF_API int xsk_socket__update_xskmap(struct xsk_socket *xsk,
> + int xsks_map_fd);
> +
> /* Flags for the libbpf_flags field. */
> #define XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD (1 << 0)
>
> --
> 2.20.1
>
next prev parent reply other threads:[~2020-11-27 9:07 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-11-27 8:25 [PATCH v5 bpf-next 0/2] libbpf: add support for privileged/unprivileged control separation mariusz.dudek
2020-11-27 8:26 ` [PATCH v5 bpf-next 1/2] libbpf: separate XDP program load with xsk socket creation mariusz.dudek
2020-11-27 9:06 ` Magnus Karlsson [this message]
2020-11-27 8:26 ` [PATCH v5 bpf-next 2/2] samples/bpf: sample application for eBPF load and socket creation split mariusz.dudek
2020-11-27 9:07 ` Magnus Karlsson
2020-12-02 1:14 ` [PATCH v5 bpf-next 0/2] libbpf: add support for privileged/unprivileged control separation Andrii Nakryiko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAJ8uoz1TkEQ0-YJmqHaOULKvTGKbvfuMNB9c71vE-39RM6YvJw@mail.gmail.com \
--to=magnus.karlsson@gmail.com \
--cc=andrii.nakryiko@gmail.com \
--cc=ast@kernel.org \
--cc=bjorn.topel@intel.com \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=jonathan.lemon@gmail.com \
--cc=magnus.karlsson@intel.com \
--cc=mariusz.dudek@gmail.com \
--cc=mariuszx.dudek@intel.com \
--cc=netdev@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).