Skip to contentSkip to conversation

introduction of bpf_xdp_adjust_tail

PATCH Superseded Nikita V. Shirokov 5 participants 22 messages Apr 17, 2018 – Apr 18, 2018

Subsystems (14)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover introduction of bpf_xdp_adjust_tail

In this patch series i'm adding new bpf helper which allow to manupulate xdp's data_end pointer. right now only "shrinking" (reduce packet's size by moving pointer) is supported (and i see no use case for "growing"). Main use case for such helper is to be able to generate controll (ICMP) messages from XDP context. such messages usually contains first N bytes from original packets as a payload, and this is exactly what this helper would allow us to do (see patch 3 for sample program, where we generate ICMP "packet too big" message). This helper could be usefull for load balancing applications where after additional encapsulation, resulting packet could be bigger then interface MTU.
Aside from new helper this patch series contains minor changes in device drivers (for ones which requires), so they would recal packet's length not only when head pointer was adjusted, but if tail's one as well.

Nikita V. Shirokov (10):
[bpf]: adding bpf_xdp_adjust_tail helper
[bpf]: adding tests for bpf_xdp_adjust_tail
[bpf]: add bpf_xdp_adjust_tail sample prog
[bpf]: make generic xdp compatible w/ bpf_xdp_adjust_tail [bpf]: make mlx4 compatible w/ bpf_xdp_adjust_tail [bpf]: make bnxt compatible w/ bpf_xdp_adjust_tail [bpf]: make cavium thunder compatible w/ bpf_xdp_adjust_tail [bpf]: make netronome nfp compatible w/ bpf_xdp_adjust_tail [bpf]: make tun compatible w/ bpf_xdp_adjust_tail [bpf]: make virtio compatible w/ bpf_xdp_adjust_tail

 drivers/net/ethernet/broadcom/bnxt/bnxt_xdp.c      |   2 +-
 drivers/net/ethernet/cavium/thunder/nicvf_main.c   |   2 +-
 drivers/net/ethernet/mellanox/mlx4/en_rx.c         |   2 +-
 .../net/ethernet/netronome/nfp/nfp_net_common.c    |   2 +-
 drivers/net/tun.c                                  |   3 +-
 drivers/net/virtio_net.c                           |   7 +-
 include/uapi/linux/bpf.h                           |  10 +-
 net/bpf/test_run.c                                 |   3 +-
 net/core/dev.c                                     |  10 +-
 net/core/filter.c                                  |  29 +++-
 samples/bpf/Makefile                               |   4 +
 samples/bpf/xdp_adjust_tail_kern.c                 | 151 +++++++++++++++++++++
 samples/bpf/xdp_adjust_tail_user.c                 | 141 +++++++++++++++++++
 tools/include/uapi/linux/bpf.h                     |  11 +-
 tools/testing/selftests/bpf/Makefile               |   2 +-
 tools/testing/selftests/bpf/bpf_helpers.h          |   5 +
 tools/testing/selftests/bpf/test_adjust_tail.c     |  29 ++++
 tools/testing/selftests/bpf/test_progs.c           |  32 +++++
 18 files changed, 433 insertions(+), 12 deletions(-)
 create mode 100644 samples/bpf/xdp_adjust_tail_kern.c
 create mode 100644 samples/bpf/xdp_adjust_tail_user.c
 create mode 100644 tools/testing/selftests/bpf/test_adjust_tail.c
Signature · 1 line
2.15.1
#1 lore raw

PATCH 1/10 : adding bpf_xdp_adjust_tail helper

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.

---
 include/uapi/linux/bpf.h | 10 +++++++++-
 net/core/filter.c        | 29 ++++++++++++++++++++++++++++-
 2 files changed, 37 insertions(+), 2 deletions(-)
include/uapi/linux/bpf.h +9 -1
index c5ec89732a8d..9a2d1a04eb24 100644
@@ -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
net/core/filter.c +28 -1
index d31aff93270d..6c8ac7b548d6 100644
@@ -2717,6 +2717,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)
+{
+	/* only shrinking is allowed for now. */
+	if (unlikely(offset > 0))
+		return -EINVAL;
+
+	void *data_end = xdp->data_end + offset;
+
+	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;
@@ -3053,7 +3077,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;
@@ -3867,6 +3892,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);
 	}
Signature · 1 line
2.15.1
#4 lore raw

PATCH 2/10 : adding tests for bpf_xdp_adjust_tail

In reply to Nikita V. Shirokov

adding selftests for bpf_xdp_adjust_tail helper. in this syntetic test we are testing that 1) if data_end < data helper will return EINVAL
2) for normal use case packet's length would be reduced.

aside from adding new tests i'm changing behaviour of bpf_prog_test_run so it would recalculate packet's length if only data_end pointer was changed

---
 net/bpf/test_run.c                             |  3 ++-
 tools/include/uapi/linux/bpf.h                 | 11 ++++++++-
 tools/testing/selftests/bpf/Makefile           |  2 +-
 tools/testing/selftests/bpf/bpf_helpers.h      |  3 +++
 tools/testing/selftests/bpf/test_adjust_tail.c | 29 +++++++++++++++++++++++
 tools/testing/selftests/bpf/test_progs.c       | 32 ++++++++++++++++++++++++++
 6 files changed, 77 insertions(+), 3 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/test_adjust_tail.c
net/bpf/test_run.c +2 -1
index 2ced48662c1f..68c3578343b4 100644
@@ -170,7 +170,8 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
 	xdp.rxq = &rxqueue->xdp_rxq;
 
 	retval = bpf_test_run(prog, &xdp, repeat, &duration);
-	if (xdp.data != data + XDP_PACKET_HEADROOM + NET_IP_ALIGN)
+	if (xdp.data != data + XDP_PACKET_HEADROOM + NET_IP_ALIGN ||
+	    xdp.data_end != xdp.data + size)
 		size = xdp.data_end - xdp.data;
 	ret = bpf_test_finish(kattr, uattr, xdp.data, size, retval, duration);
 	kfree(data);
tools/include/uapi/linux/bpf.h +10 -1
index 9d07465023a2..9a2d1a04eb24 100644
@@ -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
@@ -864,6 +872,7 @@ enum bpf_func_id {
 /* BPF_FUNC_skb_set_tunnel_key flags. */
 #define BPF_F_ZERO_CSUM_TX		(1ULL << 1)
 #define BPF_F_DONT_FRAGMENT		(1ULL << 2)
+#define BPF_F_SEQ_NUMBER		(1ULL << 3)
 
 /* BPF_FUNC_perf_event_output, BPF_FUNC_perf_event_read and
  * BPF_FUNC_perf_event_read_value flags.
tools/testing/selftests/bpf/Makefile +1 -1
index 0a315ddabbf4..3e819dc70bee 100644
@@ -31,7 +31,7 @@ TEST_GEN_FILES = test_pkt_access.o test_xdp.o test_l4lb.o test_tcp_estats.o test
 	sockmap_verdict_prog.o dev_cgroup.o sample_ret0.o test_tracepoint.o \
 	test_l4lb_noinline.o test_xdp_noinline.o test_stacktrace_map.o \
 	sample_map_ret0.o test_tcpbpf_kern.o test_stacktrace_build_id.o \
-	sockmap_tcp_msg_prog.o connect4_prog.o connect6_prog.o
+	sockmap_tcp_msg_prog.o connect4_prog.o connect6_prog.o test_adjust_tail.o
 
 # Order correspond to 'make run_tests' order
 TEST_PROGS := test_kmod.sh \
tools/testing/selftests/bpf/bpf_helpers.h +3 -0
index d8223d99f96d..50c607014b22 100644
@@ -96,6 +96,9 @@ static int (*bpf_msg_pull_data)(void *ctx, int start, int end, int flags) =
 	(void *) BPF_FUNC_msg_pull_data;
 static int (*bpf_bind)(void *ctx, void *addr, int addr_len) =
 	(void *) BPF_FUNC_bind;
+static int (*bpf_xdp_adjust_tail)(void *ctx, int offset) =
+	(void *) BPF_FUNC_xdp_adjust_tail;
+
 
 /* llvm builtin functions that eBPF C program may use to
  * emit BPF_LD_ABS and BPF_LD_IND instructions
tools/testing/selftests/bpf/test_adjust_tail.c new file +29 -0
new file mode 100644
index 000000000000..86239e792d6d
@@ -0,0 +1,29 @@
+/* Copyright (c) 2016,2017 Facebook
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU General Public
+ * License as published by the Free Software Foundation.
+ */
+#include <linux/bpf.h>
+#include <linux/if_ether.h>
+#include "bpf_helpers.h"
+
+int _version SEC("version") = 1;
+
+SEC("xdp_adjust_tail")
+int _xdp_adjust_tail(struct xdp_md *xdp)
+{
+	void *data_end = (void *)(long)xdp->data_end;
+	void *data = (void *)(long)xdp->data;
+	int offset = 0;
+
+	if (data_end - data == 54)
+		offset = 256;
+	else
+		offset = 20;
+	if (bpf_xdp_adjust_tail(xdp, 0 - offset))
+		return XDP_DROP;
+	return XDP_TX;
+}
+
+char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/test_progs.c +32 -0
index faadbe233966..eedda98d7bb1 100644
@@ -166,6 +166,37 @@ static void test_xdp(void)
 	bpf_object__close(obj);
 }
 
+static void test_xdp_adjust_tail(void)
+{
+	const char *file = "./test_adjust_tail.o";
+	struct bpf_object *obj;
+	char buf[128];
+	__u32 duration, retval, size;
+	int err, prog_fd;
+
+	err = bpf_prog_load(file, BPF_PROG_TYPE_XDP, &obj, &prog_fd);
+	if (err) {
+		error_cnt++;
+		return;
+	}
+
+	err = bpf_prog_test_run(prog_fd, 1, &pkt_v4, sizeof(pkt_v4),
+				buf, &size, &retval, &duration);
+
+	CHECK(err || errno || retval != XDP_DROP,
+	      "ipv4", "err %d errno %d retval %d size %d\n",
+	      err, errno, retval, size);
+
+	err = bpf_prog_test_run(prog_fd, 1, &pkt_v6, sizeof(pkt_v6),
+				buf, &size, &retval, &duration);
+	CHECK(err || errno || retval != XDP_TX || size != 54,
+	      "ipv6", "err %d errno %d retval %d size %d\n",
+	      err, errno, retval, size);
+	bpf_object__close(obj);
+}
+
+
+
 #define MAGIC_VAL 0x1234
 #define NUM_ITER 100000
 #define VIP_NUM 5
@@ -1177,6 +1208,7 @@ int main(void)
 {
 	test_pkt_access();
 	test_xdp();
+	test_xdp_adjust_tail();
 	test_l4lb_all();
 	test_xdp_noinline();
 	test_tcp_estats();
Signature · 1 line
2.15.1
#6 lore raw

PATCH 3/10 : add bpf_xdp_adjust_tail sample prog

In reply to Nikita V. Shirokov

adding bpf's sample program which is using bpf_xdp_adjust_tail helper by generating ICMPv4 "packet to big" message if ingress packet's size is bigger then 600 bytes

---
 samples/bpf/Makefile                      |   4 +
 samples/bpf/xdp_adjust_tail_kern.c        | 151 ++++++++++++++++++++++++++++++
 samples/bpf/xdp_adjust_tail_user.c        | 141 ++++++++++++++++++++++++++++
 tools/testing/selftests/bpf/bpf_helpers.h |   2 +
 4 files changed, 298 insertions(+)
 create mode 100644 samples/bpf/xdp_adjust_tail_kern.c
 create mode 100644 samples/bpf/xdp_adjust_tail_user.c
samples/bpf/Makefile +4 -0
index 4d6a6edd4bf6..aa8c392e2e52 100644
@@ -44,6 +44,7 @@ hostprogs-y += xdp_monitor
 hostprogs-y += xdp_rxq_info
 hostprogs-y += syscall_tp
 hostprogs-y += cpustat
+hostprogs-y += xdp_adjust_tail
 
 # Libbpf dependencies
 LIBBPF := ../../tools/lib/bpf/bpf.o ../../tools/lib/bpf/nlattr.o
@@ -95,6 +96,7 @@ xdp_monitor-objs := bpf_load.o $(LIBBPF) xdp_monitor_user.o
 xdp_rxq_info-objs := bpf_load.o $(LIBBPF) xdp_rxq_info_user.o
 syscall_tp-objs := bpf_load.o $(LIBBPF) syscall_tp_user.o
 cpustat-objs := bpf_load.o $(LIBBPF) cpustat_user.o
+xdp_adjust_tail-objs := bpf_load.o $(LIBBPF) xdp_adjust_tail_user.o
 
 # Tell kbuild to always build the programs
 always := $(hostprogs-y)
@@ -148,6 +150,7 @@ always += xdp_rxq_info_kern.o
 always += xdp2skb_meta_kern.o
 always += syscall_tp_kern.o
 always += cpustat_kern.o
+always += xdp_adjust_tail_kern.o
 
 HOSTCFLAGS += -I$(objtree)/usr/include
 HOSTCFLAGS += -I$(srctree)/tools/lib/
@@ -193,6 +196,7 @@ HOSTLOADLIBES_xdp_monitor += -lelf
 HOSTLOADLIBES_xdp_rxq_info += -lelf
 HOSTLOADLIBES_syscall_tp += -lelf
 HOSTLOADLIBES_cpustat += -lelf
+HOSTLOADLIBES_xdp_adjust_tail += -lelf
 
 # Allows pointing LLC/CLANG to a LLVM backend with bpf support, redefine on cmdline:
 #  make samples/bpf/ LLC=~/git/llvm/build/bin/llc CLANG=~/git/llvm/build/bin/clang
samples/bpf/xdp_adjust_tail_kern.c new file +151 -0

151 lines not on this page · raw message

samples/bpf/xdp_adjust_tail_user.c new file +141 -0

141 lines not on this page · raw message

tools/testing/selftests/bpf/bpf_helpers.h +2 -0
index 50c607014b22..9271576bdc8f 100644
@@ -132,6 +132,8 @@ static int (*bpf_l3_csum_replace)(void *ctx, int off, int from, int to, int flag
 	(void *) BPF_FUNC_l3_csum_replace;
 static int (*bpf_l4_csum_replace)(void *ctx, int off, int from, int to, int flags) =
 	(void *) BPF_FUNC_l4_csum_replace;
+static int (*bpf_csum_diff)(void *from, int from_size, void *to, int to_size, int seed) =
+	(void *) BPF_FUNC_csum_diff;
 static int (*bpf_skb_under_cgroup)(void *ctx, void *map, int index) =
 	(void *) BPF_FUNC_skb_under_cgroup;
 static int (*bpf_skb_change_head)(void *, int len, int flags) =
Signature · 1 line
2.15.1
#7 lore raw

PATCH 4/10 : make generic xdp compatible w/ bpf_xdp_adjust_tail

In reply to Nikita V. Shirokov

w/ bpf_xdp_adjust_tail helper xdp's data_end pointer could be changed as well (only "decrease" of pointer's location is going to be supported). changing of this pointer will change packet's size.
for generic XDP we need to reflect this packet's length change by adjusting skb's tail pointer

---
 net/core/dev.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
net/core/dev.c +9 -1
index 969462ebb296..11c789231a03 100644
@@ -3996,9 +3996,9 @@ static u32 netif_receive_generic_xdp(struct sk_buff *skb,
 				     struct bpf_prog *xdp_prog)
 {
 	struct netdev_rx_queue *rxqueue;
+	void *orig_data, *orig_data_end;
 	u32 metalen, act = XDP_DROP;
 	struct xdp_buff xdp;
-	void *orig_data;
 	int hlen, off;
 	u32 mac_len;
 
@@ -4037,6 +4037,7 @@ static u32 netif_receive_generic_xdp(struct sk_buff *skb,
 	xdp.data_meta = xdp.data;
 	xdp.data_end = xdp.data + hlen;
 	xdp.data_hard_start = skb->data - skb_headroom(skb);
+	orig_data_end = xdp.data_end;
 	orig_data = xdp.data;
 
 	rxqueue = netif_get_rxqueue(skb);
@@ -4051,6 +4052,13 @@ static u32 netif_receive_generic_xdp(struct sk_buff *skb,
 		__skb_push(skb, -off);
 	skb->mac_header += off;
 
+	/* check if bpf_xdp_adjust_tail was used. it can only "shrink"
+	 * pckt.
+	 */
+	off = orig_data_end - xdp.data_end;
+	if (off != 0)
+		skb_set_tail_pointer(skb, xdp.data_end - xdp.data);
+
 	switch (act) {
 	case XDP_REDIRECT:
 	case XDP_TX:
Signature · 1 line
2.15.1
#9 lore raw

PATCH 5/10 : make mlx4 compatible w/ bpf_xdp_adjust_tail

In reply to Nikita V. Shirokov

w/ bpf_xdp_adjust_tail helper xdp's data_end pointer could be changed as well (only "decrease" of pointer's location is going to be supported). changing of this pointer will change packet's size.
for mlx4 driver we will just calculate packet's length unconditionally (the same way as it's already being done in mlx5)

---
 drivers/net/ethernet/mellanox/mlx4/en_rx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/mellanox/mlx4/en_rx.c +1 -1
index 5c613c6663da..efc55feddc5c 100644
@@ -775,8 +775,8 @@ int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int bud
 
 			act = bpf_prog_run_xdp(xdp_prog, &xdp);
 
+			length = xdp.data_end - xdp.data;
 			if (xdp.data != orig_data) {
-				length = xdp.data_end - xdp.data;
 				frags[0].page_offset = xdp.data -
 					xdp.data_hard_start;
 				va = xdp.data;
Signature · 1 line
2.15.1
#11 lore raw

PATCH 6/10 : make bnxt compatible w/ bpf_xdp_adjust_tail

In reply to Nikita V. Shirokov

w/ bpf_xdp_adjust_tail helper xdp's data_end pointer could be changed as well (only "decrease" of pointer's location is going to be supported). changing of this pointer will change packet's size.
for bnxt driver we will just calculate packet's length unconditionally

---
 drivers/net/ethernet/broadcom/bnxt/bnxt_xdp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/broadcom/bnxt/bnxt_xdp.c +1 -1
index 1389ab5e05df..1f0e872d0667 100644
@@ -113,10 +113,10 @@ bool bnxt_rx_xdp(struct bnxt *bp, struct bnxt_rx_ring_info *rxr, u16 cons,
 	if (tx_avail != bp->tx_ring_size)
 		*event &= ~BNXT_RX_EVENT;
 
+	*len = xdp.data_end - xdp.data;
 	if (orig_data != xdp.data) {
 		offset = xdp.data - xdp.data_hard_start;
 		*data_ptr = xdp.data_hard_start + offset;
-		*len = xdp.data_end - xdp.data;
 	}
 	switch (act) {
 	case XDP_PASS:
Signature · 1 line
2.15.1
#13 lore raw

PATCH 7/10 : make cavium thunder compatible w/ bpf_xdp_adjust_tail

In reply to Nikita V. Shirokov

w/ bpf_xdp_adjust_tail helper xdp's data_end pointer could be changed as well (only "decrease" of pointer's location is going to be supported). changing of this pointer will change packet's size.
for cavium's thunder driver we will just calculate packet's length unconditionally

---
 drivers/net/ethernet/cavium/thunder/nicvf_main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/cavium/thunder/nicvf_main.c +1 -1
index 707db3304396..7135db45927e 100644
@@ -538,9 +538,9 @@ static inline bool nicvf_xdp_rx(struct nicvf *nic, struct bpf_prog *prog,
 	action = bpf_prog_run_xdp(prog, &xdp);
 	rcu_read_unlock();
 
+	len = xdp.data_end - xdp.data;
 	/* Check if XDP program has changed headers */
 	if (orig_data != xdp.data) {
-		len = xdp.data_end - xdp.data;
 		offset = orig_data - xdp.data;
 		dma_addr -= offset;
 	}
Signature · 1 line
2.15.1
#15 lore raw

PATCH 8/10 : make netronome nfp compatible w/ bpf_xdp_adjust_tail

In reply to Nikita V. Shirokov

w/ bpf_xdp_adjust_tail helper xdp's data_end pointer could be changed as well (only "decrease" of pointer's location is going to be supported). changing of this pointer will change packet's size.
for nfp driver we will just calculate packet's length unconditionally

---
 drivers/net/ethernet/netronome/nfp/nfp_net_common.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/netronome/nfp/nfp_net_common.c +1 -1
index 1eb6549f2a54..d9111c077699 100644
@@ -1722,7 +1722,7 @@ static int nfp_net_rx(struct nfp_net_rx_ring *rx_ring, int budget)
 
 			act = bpf_prog_run_xdp(xdp_prog, &xdp);
 
-			pkt_len -= xdp.data - orig_data;
+			pkt_len = xdp.data_end - xdp.data;
 			pkt_off += xdp.data - orig_data;
 
 			switch (act) {
Signature · 1 line
2.15.1
#18 lore raw

PATCH 9/10 : make tun compatible w/ bpf_xdp_adjust_tail

In reply to Nikita V. Shirokov

w/ bpf_xdp_adjust_tail helper xdp's data_end pointer could be changed as well (only "decrease" of pointer's location is going to be supported). changing of this pointer will change packet's size.
for tun driver we need to adjust XDP_PASS handling by recalculating length of the packet if it was passed to the TCP/IP stack (in case if after xdp's prog run data_end pointer was adjusted)

---
 drivers/net/tun.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/tun.c +2 -1
index 28583aa0c17d..0b488a958076 100644
@@ -1688,6 +1688,7 @@ static struct sk_buff *tun_build_skb(struct tun_struct *tun,
 			return NULL;
 		case XDP_PASS:
 			delta = orig_data - xdp.data;
+			len = xdp.data_end - xdp.data;
 			break;
 		default:
 			bpf_warn_invalid_xdp_action(act);
@@ -1708,7 +1709,7 @@ static struct sk_buff *tun_build_skb(struct tun_struct *tun,
 	}
 
 	skb_reserve(skb, pad - delta);
-	skb_put(skb, len + delta);
+	skb_put(skb, len);
 	get_page(alloc_frag->page);
 	alloc_frag->offset += buflen;
 
Signature · 1 line
2.15.1
#20 lore raw

PATCH 10/10 : make virtio compatible w/ bpf_xdp_adjust_tail

In reply to Nikita V. Shirokov

w/ bpf_xdp_adjust_tail helper xdp's data_end pointer could be changed as well (only "decrease" of pointer's location is going to be supported). changing of this pointer will change packet's size.
for virtio driver we need to adjust XDP_PASS handling by recalculating length of the packet if it was passed to the TCP/IP stack

---
 drivers/net/virtio_net.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/net/virtio_net.c +6 -1
index 7b187ec7411e..115d85f7360a 100644
@@ -604,6 +604,7 @@ static struct sk_buff *receive_small(struct net_device *dev,
 		case XDP_PASS:
 			/* Recalculate length in case bpf program changed it */
 			delta = orig_data - xdp.data;
+			len = xdp.data_end - xdp.data;
 			break;
 		case XDP_TX:
 			sent = __virtnet_xdp_xmit(vi, &xdp);
@@ -637,7 +638,7 @@ static struct sk_buff *receive_small(struct net_device *dev,
 		goto err;
 	}
 	skb_reserve(skb, headroom - delta);
-	skb_put(skb, len + delta);
+	skb_put(skb, len);
 	if (!delta) {
 		buf += header_offset;
 		memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len);
@@ -752,6 +753,10 @@ static struct sk_buff *receive_mergeable(struct net_device *dev,
 			offset = xdp.data -
 					page_address(xdp_page) - vi->hdr_len;
 
+			/* recalculate len if xdp.data or xdp.data_end were
+			 * adjusted
+			 */
+			len = xdp.data_end - xdp.data;
 			/* We can only create skb based on xdp_page. */
 			if (unlikely(xdp_page != page)) {
 				rcu_read_unlock();
Signature · 1 line
2.15.1
#2 lore raw

Re: [PATCH bpf-next 01/10] [bpf]: adding bpf_xdp_adjust_tail helper

In reply to Nikita V. Shirokov
Thank you for the patch! Perhaps something to improve:

Hi Nikita,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on bpf-next/master]

url:    https://github.com/0day-ci/linux/commits/Nikita-V-Shirokov/introduction-of-bpf_xdp_adjust_tail/20180417-211905
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
config: i386-randconfig-s1-201815 (attached as .config)
compiler: gcc-6 (Debian 6.4.0-9) 6.4.0 20171026
reproduce:
        # save the attached .config to linux build tree
        make ARCH=i386 

All warnings (new ones prefixed by >>):

net/core/filter.c: In function '____bpf_xdp_adjust_tail':

>> net/core/filter.c:2726:2: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
     void *data_end = xdp->data_end + offset;
     ^~~~

vim +2726 net/core/filter.c

2719
2720 BPF_CALL_2(bpf_xdp_adjust_tail, struct xdp_buff *, xdp, int, offset)
2721 {
2722 /* only shrinking is allowed for now. */
2723 if (unlikely(offset > 0))
2724 return -EINVAL;
2725

> 2726		void *data_end = xdp->data_end + offset;

2727
2728 if (unlikely(data_end < xdp->data + ETH_HLEN))
2729 return -EINVAL;
2730
2731 xdp->data_end = data_end;
2732
2733 return 0;
2734 }
2735

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#3 lore raw

Re: [PATCH bpf-next 01/10] [bpf]: adding bpf_xdp_adjust_tail helper

In reply to Nikita V. Shirokov
Quoted 44 lines · Nikita V. Shirokov

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.

---
 include/uapi/linux/bpf.h | 10 +++++++++-
 net/core/filter.c        | 29 ++++++++++++++++++++++++++++-
 2 files changed, 37 insertions(+), 2 deletions(-)
include/uapi/linux/bpf.h +9 -1
index c5ec89732a8d..9a2d1a04eb24 100644
@@ -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
net/core/filter.c +0 -0
index d31aff93270d..6c8ac7b548d6 100644

net/core/filter.c static const struct bpf_func_proto bpf_xdp_adjust_head_proto = { · line 2724

Quoted 8 lines
net/core/filter.c +5 -0
@@ -2717,6 +2717,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)
+{
+	/* only shrinking is allowed for now. */
+	if (unlikely(offset > 0))
+		return -EINVAL;

why allow offset == 0 ?
It's a nop. xdp_adjust_head allows it, but it's not a reason to repeat the same here.
Like we may decide to do something with offset==0 in the future. Let's keep it reserved.

In the subject please replace
[bpf]: adding bpf_xdp_adjust_tail helper
with
bpf: adding bpf_xdp_adjust_tail helper

"[bpf] foo bar" subject used to be llvm patch convention, but lately we switched it to kernel style as well with "bpf: foo bar"

#5 lore raw

Re: [PATCH bpf-next 02/10] [bpf]: adding tests for bpf_xdp_adjust_tail

In reply to Nikita V. Shirokov
Quoted 24 lines · Nikita V. Shirokov

adding selftests for bpf_xdp_adjust_tail helper. in this syntetic test we are testing that 1) if data_end < data helper will return EINVAL
2) for normal use case packet's length would be reduced.

aside from adding new tests i'm changing behaviour of bpf_prog_test_run so it would recalculate packet's length if only data_end pointer was changed

---
 net/bpf/test_run.c                             |  3 ++-
 tools/include/uapi/linux/bpf.h                 | 11 ++++++++-
 tools/testing/selftests/bpf/Makefile           |  2 +-
 tools/testing/selftests/bpf/bpf_helpers.h      |  3 +++
 tools/testing/selftests/bpf/test_adjust_tail.c | 29 +++++++++++++++++++++++
 tools/testing/selftests/bpf/test_progs.c       | 32 ++++++++++++++++++++++++++
 6 files changed, 77 insertions(+), 3 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/test_adjust_tail.c
net/bpf/test_run.c +0 -0
index 2ced48662c1f..68c3578343b4 100644

net/bpf/test_run.c int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr, · line 174

Quoted 6 lines
net/bpf/test_run.c +2 -1
@@ -170,7 +170,8 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
 	xdp.rxq = &rxqueue->xdp_rxq;
 
 	retval = bpf_test_run(prog, &xdp, repeat, &duration);
-	if (xdp.data != data + XDP_PACKET_HEADROOM + NET_IP_ALIGN)
+	if (xdp.data != data + XDP_PACKET_HEADROOM + NET_IP_ALIGN ||
+	    xdp.data_end != xdp.data + size)

please split fixing prog_test_run for adjust_tail into separate patch and selftests into another one.

Quoted 32 lines
net/bpf/test_run.c +0 -0
 		size = xdp.data_end - xdp.data;
 	ret = bpf_test_finish(kattr, uattr, xdp.data, size, retval, duration);
 	kfree(data);
tools/include/uapi/linux/bpf.h +9 -1
index 9d07465023a2..9a2d1a04eb24 100644
@@ -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

tools/include/uapi/linux/bpf.h enum bpf_func_id { · line 875

Quoted 4 lines
tools/include/uapi/linux/bpf.h +1 -0
@@ -864,6 +872,7 @@ enum bpf_func_id {
 /* BPF_FUNC_skb_set_tunnel_key flags. */
 #define BPF_F_ZERO_CSUM_TX		(1ULL << 1)
 #define BPF_F_DONT_FRAGMENT		(1ULL << 2)
+#define BPF_F_SEQ_NUMBER		(1ULL << 3)

William Tu missed adding it to tools/include/uapi/bpf.h when it was added to main uapi/bpf.h
but don't add it as part of this patch.
I saw a separate patch for this passing by in tip tree from Arnaldo. I'm not sure how quickly it will get into Linus tree,
let's not create extra merge conflicts.

Quoted 36 lines
tools/include/uapi/linux/bpf.h +0 -0
 /* BPF_FUNC_perf_event_output, BPF_FUNC_perf_event_read and
  * BPF_FUNC_perf_event_read_value flags.
tools/testing/selftests/bpf/Makefile +1 -1
index 0a315ddabbf4..3e819dc70bee 100644
@@ -31,7 +31,7 @@ TEST_GEN_FILES = test_pkt_access.o test_xdp.o test_l4lb.o test_tcp_estats.o test
 	sockmap_verdict_prog.o dev_cgroup.o sample_ret0.o test_tracepoint.o \
 	test_l4lb_noinline.o test_xdp_noinline.o test_stacktrace_map.o \
 	sample_map_ret0.o test_tcpbpf_kern.o test_stacktrace_build_id.o \
-	sockmap_tcp_msg_prog.o connect4_prog.o connect6_prog.o
+	sockmap_tcp_msg_prog.o connect4_prog.o connect6_prog.o test_adjust_tail.o
 
 # Order correspond to 'make run_tests' order
 TEST_PROGS := test_kmod.sh \
tools/testing/selftests/bpf/bpf_helpers.h +3 -0
index d8223d99f96d..50c607014b22 100644
@@ -96,6 +96,9 @@ static int (*bpf_msg_pull_data)(void *ctx, int start, int end, int flags) =
 	(void *) BPF_FUNC_msg_pull_data;
 static int (*bpf_bind)(void *ctx, void *addr, int addr_len) =
 	(void *) BPF_FUNC_bind;
+static int (*bpf_xdp_adjust_tail)(void *ctx, int offset) =
+	(void *) BPF_FUNC_xdp_adjust_tail;
+
 
 /* llvm builtin functions that eBPF C program may use to
  * emit BPF_LD_ABS and BPF_LD_IND instructions
tools/testing/selftests/bpf/test_adjust_tail.c new file +0 -0
new file mode 100644
index 000000000000..86239e792d6d

tools/testing/selftests/bpf/test_adjust_tail.c line 6

Quoted 6 lines
tools/testing/selftests/bpf/test_adjust_tail.c new file +6 -0
@@ -0,0 +1,29 @@
+/* Copyright (c) 2016,2017 Facebook
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU General Public
+ * License as published by the Free Software Foundation.
+ */

Please use SPDX header here and in other patches.

tools/testing/selftests/bpf/test_adjust_tail.c line 11

Quoted 5 lines
tools/testing/selftests/bpf/test_adjust_tail.c new file +5 -0
+#include <linux/bpf.h>
+#include <linux/if_ether.h>
+#include "bpf_helpers.h"
+
+int _version SEC("version") = 1;

we really should fix libbpf to avoid requiring that for all program types. It's annoying to see this in every networking test.

Acked-by on v1 4/10
#8 lore raw

Re: [PATCH bpf-next 04/10] [bpf]: make generic xdp compatible w/ bpf_xdp_adjust_tail

In reply to Nikita V. Shirokov
Quoted 7 lines · Nikita V. Shirokov

w/ bpf_xdp_adjust_tail helper xdp's data_end pointer could be changed as well (only "decrease" of pointer's location is going to be supported). changing of this pointer will change packet's size.
for generic XDP we need to reflect this packet's length change by adjusting skb's tail pointer

pls also change the order of the test/sample patches. they should come last, since they will work only after this one and all other driver support.

Acked-by on v1 5/10
#10 lore raw

Re: [PATCH bpf-next 05/10] [bpf]: make mlx4 compatible w/ bpf_xdp_adjust_tail

In reply to Nikita V. Shirokov
Quoted 7 lines · Nikita V. Shirokov

w/ bpf_xdp_adjust_tail helper xdp's data_end pointer could be changed as well (only "decrease" of pointer's location is going to be supported). changing of this pointer will change packet's size.
for mlx4 driver we will just calculate packet's length unconditionally (the same way as it's already being done in mlx5)

Acked-by on v1 6/10
#12 lore raw

Re: [PATCH bpf-next 06/10] [bpf]: make bnxt compatible w/ bpf_xdp_adjust_tail

In reply to Nikita V. Shirokov
Quoted 6 lines · Nikita V. Shirokov

w/ bpf_xdp_adjust_tail helper xdp's data_end pointer could be changed as well (only "decrease" of pointer's location is going to be supported). changing of this pointer will change packet's size.
for bnxt driver we will just calculate packet's length unconditionally

Acked-by on v1 7/10
#14 lore raw

Re: [PATCH bpf-next 07/10] [bpf]: make cavium thunder compatible w/ bpf_xdp_adjust_tail

In reply to Nikita V. Shirokov
Quoted 7 lines · Nikita V. Shirokov

w/ bpf_xdp_adjust_tail helper xdp's data_end pointer could be changed as well (only "decrease" of pointer's location is going to be supported). changing of this pointer will change packet's size.
for cavium's thunder driver we will just calculate packet's length unconditionally

#16 lore raw

Re: [PATCH bpf-next 08/10] [bpf]: make netronome nfp compatible w/ bpf_xdp_adjust_tail

In reply to Nikita V. Shirokov
Quoted 15 lines · Nikita V. Shirokov

w/ bpf_xdp_adjust_tail helper xdp's data_end pointer could be changed as well (only "decrease" of pointer's location is going to be supported). changing of this pointer will change packet's size.
for nfp driver we will just calculate packet's length unconditionally

---
 drivers/net/ethernet/netronome/nfp/nfp_net_common.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/netronome/nfp/nfp_net_common.c +0 -0
index 1eb6549f2a54..d9111c077699 100644

drivers/net/ethernet/netronome/nfp/nfp_net_common.c static int nfp_net_rx(struct nfp_net_rx_ring *rx_ring, int budget) · line 1725

Quoted 5 lines
drivers/net/ethernet/netronome/nfp/nfp_net_common.c +1 -1
@@ -1722,7 +1722,7 @@ static int nfp_net_rx(struct nfp_net_rx_ring *rx_ring, int budget)
 
 			act = bpf_prog_run_xdp(xdp_prog, &xdp);
 
-			pkt_len -= xdp.data - orig_data;
+			pkt_len = xdp.data_end - xdp.data;

Looks correct, but Jakub please review.

#17 lore raw

Re: [PATCH bpf-next 08/10] [bpf]: make netronome nfp compatible w/ bpf_xdp_adjust_tail

Quoted 17 lines · Alexei Starovoitov
Quoted 16 lines · Nikita V. Shirokov

w/ bpf_xdp_adjust_tail helper xdp's data_end pointer could be changed as well (only "decrease" of pointer's location is going to be supported). changing of this pointer will change packet's size.
for nfp driver we will just calculate packet's length unconditionally

---
 drivers/net/ethernet/netronome/nfp/nfp_net_common.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/netronome/nfp/nfp_net_common.c +0 -0
index 1eb6549f2a54..d9111c077699 100644
@@ -1722,7 +1722,7 @@ static int nfp_net_rx(struct nfp_net_rx_ring *rx_ring, int budget)
 
Quoted 6 lines

drivers/net/ethernet/netronome/nfp/nfp_net_common.c static int nfp_net_rx(struct nfp_net_rx_ring *rx_ring, int budget) · line 1725

Quoted 4 lines
drivers/net/ethernet/netronome/nfp/nfp_net_common.c +1 -1
 			act = bpf_prog_run_xdp(xdp_prog, &xdp);
 
-			pkt_len -= xdp.data - orig_data;
+			pkt_len = xdp.data_end - xdp.data;  

Looks correct, but Jakub please review.

Indeed:

Thanks!

Reviewed-by on v1 9/10
#19 lore raw

Re: [PATCH bpf-next 09/10] [bpf]: make tun compatible w/ bpf_xdp_adjust_tail

In reply to Nikita V. Shirokov
Quoted 25 lines · Nikita V. Shirokov

w/ bpf_xdp_adjust_tail helper xdp's data_end pointer could be changed as well (only "decrease" of pointer's location is going to be supported). changing of this pointer will change packet's size.
for tun driver we need to adjust XDP_PASS handling by recalculating length of the packet if it was passed to the TCP/IP stack (in case if after xdp's prog run data_end pointer was adjusted)

---
  drivers/net/tun.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/tun.c +1 -0
index 28583aa0c17d..0b488a958076 100644
@@ -1688,6 +1688,7 @@ static struct sk_buff *tun_build_skb(struct tun_struct *tun,
  			return NULL;
  		case XDP_PASS:
  			delta = orig_data - xdp.data;
+			len = xdp.data_end - xdp.data;
  			break;
  		default:
  			bpf_warn_invalid_xdp_action(act);

drivers/net/tun.c static struct sk_buff *tun_build_skb(struct tun_struct *tun, · line 1715

Quoted 8 lines
drivers/net/tun.c +1 -1
@@ -1708,7 +1709,7 @@ static struct sk_buff *tun_build_skb(struct tun_struct *tun,
  	}
 
  	skb_reserve(skb, pad - delta);
-	skb_put(skb, len + delta);
+	skb_put(skb, len);
  	get_page(alloc_frag->page);
  	alloc_frag->offset += buflen;
 
Reviewed-by on v1 10/10
#21 lore raw

Re: [PATCH bpf-next 10/10] [bpf]: make virtio compatible w/ bpf_xdp_adjust_tail

In reply to Nikita V. Shirokov
Quoted 33 lines · Nikita V. Shirokov

w/ bpf_xdp_adjust_tail helper xdp's data_end pointer could be changed as well (only "decrease" of pointer's location is going to be supported). changing of this pointer will change packet's size.
for virtio driver we need to adjust XDP_PASS handling by recalculating length of the packet if it was passed to the TCP/IP stack

---
  drivers/net/virtio_net.c | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
drivers/net/virtio_net.c +2 -1
index 7b187ec7411e..115d85f7360a 100644
@@ -604,6 +604,7 @@ static struct sk_buff *receive_small(struct net_device *dev,
  		case XDP_PASS:
  			/* Recalculate length in case bpf program changed it */
  			delta = orig_data - xdp.data;
+			len = xdp.data_end - xdp.data;
  			break;
  		case XDP_TX:
  			sent = __virtnet_xdp_xmit(vi, &xdp);
@@ -637,7 +638,7 @@ static struct sk_buff *receive_small(struct net_device *dev,
  		goto err;
  	}
  	skb_reserve(skb, headroom - delta);
-	skb_put(skb, len + delta);
+	skb_put(skb, len);
  	if (!delta) {
  		buf += header_offset;
  		memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len);

drivers/net/virtio_net.c static struct sk_buff *receive_mergeable(struct net_device *dev, · line 762

Quoted 10 lines
drivers/net/virtio_net.c +4 -0
@@ -752,6 +753,10 @@ static struct sk_buff *receive_mergeable(struct net_device *dev,
  			offset = xdp.data -
  					page_address(xdp_page) - vi->hdr_len;
 
+			/* recalculate len if xdp.data or xdp.data_end were
+			 * adjusted
+			 */
+			len = xdp.data_end - xdp.data;
  			/* We can only create skb based on xdp_page. */
  			if (unlikely(xdp_page != page)) {
  				rcu_read_unlock();