public inbox for netdev@vger.kernel.org 
 help / color / mirror / Atom feed
From: "Nikita V. Shirokov" <tehnerd@tehnerd•com>
To: Alexei Starovoitov <ast@kernel•org>,
	Daniel Borkmann <daniel@iogearbox•net>
Cc: netdev@vger•kernel.org, "Nikita V. Shirokov" <tehnerd@tehnerd•com>
Subject: [PATCH bpf-next v3 01/11] bpf: adding bpf_xdp_adjust_tail helper
Date: Tue, 17 Apr 2018 21:42:13 -0700	[thread overview]
Message-ID: <20180418044223.17685-2-tehnerd@tehnerd.com> (raw)
In-Reply-To: <20180418044223.17685-1-tehnerd@tehnerd.com>

Adding new bpf helper which would allow us to manipulate
xdp's data_end pointer, and allow us to reduce packet's size
indended use case: to generate ICMP messages from XDP context,
where such message would contain truncated original packet.

Signed-off-by: Nikita V. Shirokov <tehnerd@tehnerd•com>
---
 include/uapi/linux/bpf.h | 10 +++++++++-
 net/core/filter.c        | 29 ++++++++++++++++++++++++++++-
 2 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index c5ec89732a8d..9a2d1a04eb24 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -755,6 +755,13 @@ union bpf_attr {
  *     @addr: pointer to struct sockaddr to bind socket to
  *     @addr_len: length of sockaddr structure
  *     Return: 0 on success or negative error code
+ *
+ * int bpf_xdp_adjust_tail(xdp_md, delta)
+ *     Adjust the xdp_md.data_end by delta. Only shrinking of packet's
+ *     size is supported.
+ *     @xdp_md: pointer to xdp_md
+ *     @delta: A negative integer to be added to xdp_md.data_end
+ *     Return: 0 on success or negative on error
  */
 #define __BPF_FUNC_MAPPER(FN)		\
 	FN(unspec),			\
@@ -821,7 +828,8 @@ union bpf_attr {
 	FN(msg_apply_bytes),		\
 	FN(msg_cork_bytes),		\
 	FN(msg_pull_data),		\
-	FN(bind),
+	FN(bind),			\
+	FN(xdp_adjust_tail),
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  * function eBPF program intends to call
diff --git a/net/core/filter.c b/net/core/filter.c
index a374b8560bc4..29318598fd60 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -2725,6 +2725,30 @@ static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
 	.arg2_type	= ARG_ANYTHING,
 };
 
+BPF_CALL_2(bpf_xdp_adjust_tail, struct xdp_buff *, xdp, int, offset)
+{
+	void *data_end = xdp->data_end + offset;
+
+	/* only shrinking is allowed for now. */
+	if (unlikely(offset >= 0))
+		return -EINVAL;
+
+	if (unlikely(data_end < xdp->data + ETH_HLEN))
+		return -EINVAL;
+
+	xdp->data_end = data_end;
+
+	return 0;
+}
+
+static const struct bpf_func_proto bpf_xdp_adjust_tail_proto = {
+	.func		= bpf_xdp_adjust_tail,
+	.gpl_only	= false,
+	.ret_type	= RET_INTEGER,
+	.arg1_type	= ARG_PTR_TO_CTX,
+	.arg2_type	= ARG_ANYTHING,
+};
+
 BPF_CALL_2(bpf_xdp_adjust_meta, struct xdp_buff *, xdp, int, offset)
 {
 	void *meta = xdp->data_meta + offset;
@@ -3074,7 +3098,8 @@ bool bpf_helper_changes_pkt_data(void *func)
 	    func == bpf_l4_csum_replace ||
 	    func == bpf_xdp_adjust_head ||
 	    func == bpf_xdp_adjust_meta ||
-	    func == bpf_msg_pull_data)
+	    func == bpf_msg_pull_data ||
+	    func == bpf_xdp_adjust_tail)
 		return true;
 
 	return false;
@@ -3888,6 +3913,8 @@ xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 		return &bpf_xdp_redirect_proto;
 	case BPF_FUNC_redirect_map:
 		return &bpf_xdp_redirect_map_proto;
+	case BPF_FUNC_xdp_adjust_tail:
+		return &bpf_xdp_adjust_tail_proto;
 	default:
 		return bpf_base_func_proto(func_id);
 	}
-- 
2.15.1

  reply	other threads:[~2018-04-18 13:22 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-04-18  4:42 [PATCH bpf-next v3 00/11] introduction of bpf_xdp_adjust_tail Nikita V. Shirokov
2018-04-18  4:42 ` Nikita V. Shirokov [this message]
2018-04-18 17:01   ` [PATCH bpf-next v3 01/11] bpf: adding bpf_xdp_adjust_tail helper Alexei Starovoitov
2018-04-18  4:42 ` [PATCH bpf-next v3 02/11] bpf: make generic xdp compatible w/ bpf_xdp_adjust_tail Nikita V. Shirokov
2018-04-18  4:42 ` [PATCH bpf-next v3 03/11] bpf: make mlx4 " Nikita V. Shirokov
2018-04-18  4:42 ` [PATCH bpf-next v3 04/11] bpf: make bnxt " Nikita V. Shirokov
2018-04-18 17:39   ` Michael Chan
2018-04-18  4:42 ` [PATCH bpf-next v3 05/11] bpf: make cavium thunder " Nikita V. Shirokov
2018-04-18  4:42 ` [PATCH bpf-next v3 06/11] bpf: make netronome nfp " Nikita V. Shirokov
2018-04-18  4:42 ` [PATCH bpf-next v3 07/11] bpf: make tun " Nikita V. Shirokov
2018-04-18 16:16   ` Michael S. Tsirkin
2018-04-18  4:42 ` [PATCH bpf-next v3 08/11] bpf: make virtio " Nikita V. Shirokov
2018-04-18  4:42 ` [PATCH bpf-next v3 09/11] bpf: making bpf_prog_test run aware of possible data_end ptr change Nikita V. Shirokov
2018-04-18  4:42 ` [PATCH bpf-next v3 10/11] bpf: adding tests for bpf_xdp_adjust_tail Nikita V. Shirokov
2018-04-18  4:42 ` [PATCH bpf-next v3 11/11] bpf: add bpf_xdp_adjust_tail sample prog Nikita V. Shirokov
2018-04-18 22:05 ` [PATCH bpf-next v3 00/11] introduction of bpf_xdp_adjust_tail Daniel Borkmann

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=20180418044223.17685-2-tehnerd@tehnerd.com \
    --to=tehnerd@tehnerd$(echo .)com \
    --cc=ast@kernel$(echo .)org \
    --cc=daniel@iogearbox$(echo .)net \
    --cc=netdev@vger$(echo .)kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
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