Skip to contentSkip to conversation

xsk: support AF_PACKET

PATCH In review Xuan Zhuo 7 participants 13 messages May 28, 2021 1 message not in the archive

Subsystems (5)
lore
Share & tools
lore

Xuan Zhuo

author
#0 lore raw

PATCH 1/1 xsk: support AF_PACKET

In xsk mode, users cannot use AF_PACKET(tcpdump) to observe the current rx/tx data packets. This feature is very important in many cases. So this patch allows AF_PACKET to obtain xsk packages.

By default, AF_PACKET is based on ptype_base/ptype_all in dev.c to obtain data packets. But xsk is not suitable for calling these callbacks, because it may send the packet to other protocol stacks. So the method I used is to let AF_PACKET get the data packet from xsk alone.

---
 include/net/xdp_sock.h |  15 +++++
 net/packet/af_packet.c |  35 +++++++++--
 net/packet/internal.h  |   7 +++
 net/xdp/Makefile       |   2 +-
 net/xdp/xsk.c          |   9 +++
 net/xdp/xsk_packet.c   | 129 +++++++++++++++++++++++++++++++++++++++++
 net/xdp/xsk_packet.h   |  44 ++++++++++++++
 7 files changed, 234 insertions(+), 7 deletions(-)
 create mode 100644 net/xdp/xsk_packet.c
 create mode 100644 net/xdp/xsk_packet.h
include/net/xdp_sock.h +15 -0
index 9c0722c6d7ac..b0acf0293132 100644
@@ -17,6 +17,11 @@ struct net_device;
 struct xsk_queue;
 struct xdp_buff;
 
+struct xsk_packet {
+	struct list_head list;
+	struct packet_type *pt;
+};
+
 struct xdp_umem {
 	void *addrs;
 	u64 size;
@@ -79,6 +84,8 @@ struct xdp_sock {
 int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp);
 int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp);
 void __xsk_map_flush(void);
+void xsk_add_pack(struct xsk_packet *xpt);
+void __xsk_remove_pack(struct xsk_packet *xpt);
 
 #else
 
@@ -96,6 +103,14 @@ static inline void __xsk_map_flush(void)
 {
 }
 
+void xsk_add_pack(struct xsk_packet *xpt)
+{
+}
+
+void __xsk_remove_pack(struct xsk_packet *xpt)
+{
+}
+
 #endif /* CONFIG_XDP_SOCKETS */
 
 #endif /* _LINUX_XDP_SOCK_H */
net/packet/af_packet.c +29 -6
index 597d798ac0a5..2720b51d13a6 100644
@@ -303,10 +303,14 @@ static void __register_prot_hook(struct sock *sk)
 	struct packet_sock *po = pkt_sk(sk);
 
 	if (!po->running) {
-		if (po->fanout)
+		if (po->fanout) {
 			__fanout_link(sk, po);
-		else
+		} else {
 			dev_add_pack(&po->prot_hook);
+#ifdef CONFIG_XDP_SOCKETS
+			xsk_add_pack(&po->xsk_pt);
+#endif
+		}
 
 		sock_hold(sk);
 		po->running = 1;
@@ -333,10 +337,14 @@ static void __unregister_prot_hook(struct sock *sk, bool sync)
 
 	po->running = 0;
 
-	if (po->fanout)
+	if (po->fanout) {
 		__fanout_unlink(sk, po);
-	else
+	} else {
 		__dev_remove_pack(&po->prot_hook);
+#ifdef CONFIG_XDP_SOCKETS
+		__xsk_remove_pack(&po->xsk_pt);
+#endif
+	}
 
 	__sock_put(sk);
 
@@ -1483,8 +1491,12 @@ static void __fanout_link(struct sock *sk, struct packet_sock *po)
 	rcu_assign_pointer(f->arr[f->num_members], sk);
 	smp_wmb();
 	f->num_members++;
-	if (f->num_members == 1)
+	if (f->num_members == 1) {
 		dev_add_pack(&f->prot_hook);
+#ifdef CONFIG_XDP_SOCKETS
+		xsk_add_pack(&f->xsk_pt);
+#endif
+	}
 	spin_unlock(&f->lock);
 }
 
@@ -1504,8 +1516,12 @@ static void __fanout_unlink(struct sock *sk, struct packet_sock *po)
 			   rcu_dereference_protected(f->arr[f->num_members - 1],
 						     lockdep_is_held(&f->lock)));
 	f->num_members--;
-	if (f->num_members == 0)
+	if (f->num_members == 0) {
 		__dev_remove_pack(&f->prot_hook);
+#ifdef CONFIG_XDP_SOCKETS
+		__xsk_remove_pack(&po->xsk_pt);
+#endif
+	}
 	spin_unlock(&f->lock);
 }
 
@@ -1737,6 +1753,10 @@ static int fanout_add(struct sock *sk, struct fanout_args *args)
 		match->prot_hook.af_packet_priv = match;
 		match->prot_hook.id_match = match_fanout_group;
 		match->max_num_members = args->max_num_members;
+#ifdef CONFIG_XDP_SOCKETS
+		match->xsk_pt.pt = &match->prot_hook;
+#endif
+
 		list_add(&match->list, &fanout_list);
 	}
 	err = -EINVAL;
@@ -3315,6 +3335,9 @@ static int packet_create(struct net *net, struct socket *sock, int protocol,
 		po->prot_hook.func = packet_rcv_spkt;
 
 	po->prot_hook.af_packet_priv = sk;
+#ifdef CONFIG_XDP_SOCKETS
+	po->xsk_pt.pt = &po->prot_hook;
+#endif
 
 	if (proto) {
 		po->prot_hook.type = proto;
net/packet/internal.h +7 -0
index 48af35b1aed2..d224b926588a 100644
@@ -3,6 +3,7 @@
 #define __PACKET_INTERNAL_H__
 
 #include <linux/refcount.h>
+#include <net/xdp_sock.h>
 
 struct packet_mclist {
 	struct packet_mclist	*next;
@@ -94,6 +95,9 @@ struct packet_fanout {
 	spinlock_t		lock;
 	refcount_t		sk_ref;
 	struct packet_type	prot_hook ____cacheline_aligned_in_smp;
+#ifdef CONFIG_XDP_SOCKETS
+	struct xsk_packet	xsk_pt;
+#endif
 	struct sock	__rcu	*arr[];
 };
 
@@ -136,6 +140,9 @@ struct packet_sock {
 	struct net_device __rcu	*cached_dev;
 	int			(*xmit)(struct sk_buff *skb);
 	struct packet_type	prot_hook ____cacheline_aligned_in_smp;
+#ifdef CONFIG_XDP_SOCKETS
+	struct xsk_packet	xsk_pt;
+#endif
 	atomic_t		tp_drops ____cacheline_aligned_in_smp;
 };
 
net/xdp/Makefile +1 -1
index 30cdc4315f42..bcac0591879b 100644
@@ -1,4 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0-only
-obj-$(CONFIG_XDP_SOCKETS) += xsk.o xdp_umem.o xsk_queue.o xskmap.o
+obj-$(CONFIG_XDP_SOCKETS) += xsk.o xdp_umem.o xsk_queue.o xskmap.o xsk_packet.o
 obj-$(CONFIG_XDP_SOCKETS) += xsk_buff_pool.o
 obj-$(CONFIG_XDP_SOCKETS_DIAG) += xsk_diag.o
net/xdp/xsk.c +9 -0
index cd62d4ba87a9..fc97e7f9e4cb 100644
@@ -28,6 +28,7 @@
 
 #include "xsk_queue.h"
 #include "xdp_umem.h"
+#include "xsk_packet.h"
 #include "xsk.h"
 
 #define TX_BATCH_SIZE 32
@@ -156,6 +157,7 @@ static int __xsk_rcv_zc(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
 	int err;
 
 	addr = xp_get_handle(xskb);
+	xsk_rx_packet_deliver(xs, addr, len);
 	err = xskq_prod_reserve_desc(xs->rx, addr, len);
 	if (err) {
 		xs->rx_queue_full++;
@@ -347,6 +349,8 @@ bool xsk_tx_peek_desc(struct xsk_buff_pool *pool, struct xdp_desc *desc)
 		if (xskq_prod_reserve_addr(pool->cq, desc->addr))
 			goto out;
 
+		xsk_tx_zc_packet_deliver(xs, desc);
+
 		xskq_cons_release(xs->tx);
 		rcu_read_unlock();
 		return true;
@@ -576,6 +580,8 @@ static int xsk_generic_xmit(struct sock *sk)
 		}
 		spin_unlock_irqrestore(&xs->pool->cq_lock, flags);
 
+		xsk_tx_packet_deliver(xs, &desc, skb);
+
 		err = __dev_direct_xmit(skb, xs->queue_id);
 		if  (err == NETDEV_TX_BUSY) {
 			/* Tell user-space to retry the send */
@@ -1467,6 +1473,9 @@ static int __init xsk_init(void)
 
 	for_each_possible_cpu(cpu)
 		INIT_LIST_HEAD(&per_cpu(xskmap_flush_list, cpu));
+
+	INIT_LIST_HEAD(&xsk_pt);
+
 	return 0;
 
 out_pernet:
net/xdp/xsk_packet.c new file +129 -0

129 lines not on this page · raw message

net/xdp/xsk_packet.h new file +44 -0

44 lines not on this page · raw message

Signature · 1 line
2.31.0
#1 lore raw
Thank you for the patch! Yet something to improve:

Hi Xuan,

Thank you for the patch! Yet something to improve:

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

url:    https://github.com/0day-ci/linux/commits/Xuan-Zhuo/xsk-support-AF_PACKET/20210528-140828
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
config: h8300-randconfig-r012-20210528 (attached as .config)
compiler: h8300-linux-gcc (GCC) 9.3.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/814526a7683da5d39bbe21f94f18df3bb85a07e7
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Xuan-Zhuo/xsk-support-AF_PACKET/20210528-140828 git checkout 814526a7683da5d39bbe21f94f18df3bb85a07e7
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=h8300

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

h8300-linux-ld: net/core/xdp.o: in function `xsk_add_pack':

>> xdp.c:(.text+0x204): multiple definition of `xsk_add_pack'; net/core/filter.o:filter.c:(.text+0xe01c): first defined here
   h8300-linux-ld: net/core/xdp.o: in function `__xsk_remove_pack':
>> xdp.c:(.text+0x210): multiple definition of `__xsk_remove_pack'; net/core/filter.o:filter.c:(.text+0xe028): first defined here

h8300-linux-ld: net/ethtool/ioctl.o: in function `xsk_add_pack':
ioctl.c:(.text+0x23dc): multiple definition of `xsk_add_pack'; net/core/filter.o:filter.c:(.text+0xe01c): first defined here
h8300-linux-ld: net/ethtool/ioctl.o: in function `__xsk_remove_pack':
ioctl.c:(.text+0x23e8): multiple definition of `__xsk_remove_pack'; net/core/filter.o:filter.c:(.text+0xe028): first defined here
h8300-linux-ld: net/ethtool/channels.o: in function `xsk_add_pack':
channels.c:(.text+0x250): multiple definition of `xsk_add_pack'; net/core/filter.o:filter.c:(.text+0xe01c): first defined here
h8300-linux-ld: net/ethtool/channels.o: in function `__xsk_remove_pack':
channels.c:(.text+0x25c): multiple definition of `__xsk_remove_pack'; net/core/filter.o:filter.c:(.text+0xe028): first defined here

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#2 lore raw
In reply to Xuan Zhuo
Thank you for the patch! Perhaps something to improve:

Hi Xuan,

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/Xuan-Zhuo/xsk-support-AF_PACKET/20210528-140828
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
config: x86_64-randconfig-a013-20210528 (attached as .config)
compiler: clang version 13.0.0 (https://github.com/llvm/llvm-project 6505c630407c5feec818f0bb1c284f9eeebf2071)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install x86_64 cross compiling tool for clang build
# apt-get install binutils-x86-64-linux-gnu
# https://github.com/0day-ci/linux/commit/814526a7683da5d39bbe21f94f18df3bb85a07e7
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Xuan-Zhuo/xsk-support-AF_PACKET/20210528-140828 git checkout 814526a7683da5d39bbe21f94f18df3bb85a07e7
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=x86_64

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

In file included from net/packet/af_packet.c:93:
In file included from net/packet/internal.h:6:

>> include/net/xdp_sock.h:106:6: warning: no previous prototype for function 'xsk_add_pack' [-Wmissing-prototypes]
   void xsk_add_pack(struct xsk_packet *xpt)
        ^
   include/net/xdp_sock.h:106:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   void xsk_add_pack(struct xsk_packet *xpt)
   ^
   static 
>> include/net/xdp_sock.h:110:6: warning: no previous prototype for function '__xsk_remove_pack' [-Wmissing-prototypes]
   void __xsk_remove_pack(struct xsk_packet *xpt)
        ^

include/net/xdp_sock.h:110:1: note: declare 'static' if the function is not intended to be used outside of this translation unit void __xsk_remove_pack(struct xsk_packet *xpt)
^
static
2 warnings generated.

vim +/xsk_add_pack +106 include/net/xdp_sock.h

105

 > 106	void xsk_add_pack(struct xsk_packet *xpt)
   107	{
   108	}
   109	
 > 110	void __xsk_remove_pack(struct xsk_packet *xpt)

111 {
112 }
113

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#3 lore raw
In reply to Xuan Zhuo
Quoted 3 lines · Xuan Zhuo

In xsk mode, users cannot use AF_PACKET(tcpdump) to observe the current rx/tx data packets. This feature is very important in many cases. So this patch allows AF_PACKET to obtain xsk packages.

You can use xdpdump to dump the packets from the XDP program before it gets redirected into the XSK:
https://github.com/xdp-project/xdp-tools/tree/master/xdp-dump

Doens't currently work on egress, but if/when we get a proper TX hook that should be doable as well.

Wiring up XSK to AF_PACKET sounds a bit nonsensical: XSK is already a transport to userspace, why would you need a second one?

-Toke

A message that is not in the archive (sent off-list or not yet received): [email protected]

#5 lore raw
Quoted 14 lines · Xuan Zhuo
Quoted 9 lines · Toke Høiland-Jørgensen
Quoted 3 lines · Xuan Zhuo

In xsk mode, users cannot use AF_PACKET(tcpdump) to observe the current rx/tx data packets. This feature is very important in many cases. So this patch allows AF_PACKET to obtain xsk packages.

You can use xdpdump to dump the packets from the XDP program before it gets redirected into the XSK:
https://github.com/xdp-project/xdp-tools/tree/master/xdp-dump

Wow, this is a good idea.

Quoted 1 line
Quoted 8 lines
Quoted 5 lines

Doens't currently work on egress, but if/when we get a proper TX hook that should be doable as well.

Wiring up XSK to AF_PACKET sounds a bit nonsensical: XSK is already a transport to userspace, why would you need a second one?

I have some different ideas. In my opinion, just like AF_PACKET can monitor tcp/udp packets, AF_PACKET monitors xsk packets is the same.

But you're adding code in the fast path to do this, in a code path where others have been working quite hard to squeeze out every drop of performance (literally chasing single nanoseconds). So I'm sorry, but this approach is just not going to fly.

What is your use case anyway? Yes, being able to run tcpdump and see the packets is nice and convenient, but what do you actually want to use this for? Just for debugging your application? System monitoring? Something else?

-Toke

#6 lore raw
Quoted 22 lines · Toke Høiland-Jørgensen
Quoted 20 lines · Xuan Zhuo
Quoted 9 lines · Toke Høiland-Jørgensen
Quoted 3 lines · Xuan Zhuo

In xsk mode, users cannot use AF_PACKET(tcpdump) to observe the current rx/tx data packets. This feature is very important in many cases. So this patch allows AF_PACKET to obtain xsk packages.

You can use xdpdump to dump the packets from the XDP program before it gets redirected into the XSK:
https://github.com/xdp-project/xdp-tools/tree/master/xdp-dump

Wow, this is a good idea.

Quoted 6 lines

Doens't currently work on egress, but if/when we get a proper TX hook that should be doable as well.

Wiring up XSK to AF_PACKET sounds a bit nonsensical: XSK is already a transport to userspace, why would you need a second one?

Quoted 7 lines
Quoted 2 lines

I have some different ideas. In my opinion, just like AF_PACKET can monitor tcp/udp packets, AF_PACKET monitors xsk packets is the same.

But you're adding code in the fast path to do this, in a code path where others have been working quite hard to squeeze out every drop of performance (literally chasing single nanoseconds). So I'm sorry, but this approach is just not going to fly.

+1. Probably would be better for everyone if Xuan started a thread on list what is his need.

Quoted 8 lines

What is your use case anyway? Yes, being able to run tcpdump and see the packets is nice and convenient, but what do you actually want to use this for? Just for debugging your application? System monitoring? Something else?

-Toke

#7 lore raw
In reply to a message not in the archive
Quoted 7 lines · On Fri, 28 May 2021 17:02:01 +0800 Xuan Zhuo <[email protected]> wrote:
Quoted 6 lines · Toke Høiland-Jørgensen
Quoted 3 lines · Xuan Zhuo

In xsk mode, users cannot use AF_PACKET(tcpdump) to observe the current rx/tx data packets. This feature is very important in many cases. So this patch allows AF_PACKET to obtain xsk packages.

Quoted 5 lines
Quoted 3 lines

You can use xdpdump to dump the packets from the XDP program before it gets redirected into the XSK:
https://github.com/xdp-project/xdp-tools/tree/master/xdp-dump

Wow, this is a good idea.

Yes, it is rather cool (credit to Eelco). Notice the extra info you can capture from 'exit', like XDP return codes, if_index, rx_queue.

The tool uses the perf ring-buffer to send/copy data to userspace. This is actually surprisingly fast, but I still think AF_XDP will be faster (but it usually 'steals' the packet).

Another (crazy?) idea is to extend this (and xdpdump), is to leverage Hangbin's recent XDP_REDIRECT extension e624d4ed4aa8 ("xdp: Extend xdp_redirect_map with broadcast support"). We now have a xdp_redirect_map flag BPF_F_BROADCAST, what if we create a BPF_F_CLONE_PASS flag?

The semantic meaning of BPF_F_CLONE_PASS flag is to copy/clone the packet for the specified map target index (e.g AF_XDP map), but afterwards it does like veth/cpumap and creates an SKB from the xdp_frame (see __xdp_build_skb_from_frame()) and send to netstack. (Feel free to kick me if this doesn't make any sense)

Signature · 4 lines
Best regards,
  Jesper Dangaard Brouer
  MSc.CS, Principal Kernel Engineer at Red Hat
  LinkedIn: http://www.linkedin.com/in/brouer
#8 lore raw
Quoted 30 lines · Jesper Dangaard Brouer
Quoted 7 lines · On Fri, 28 May 2021 17:02:01 +0800 Xuan Zhuo <[email protected]> wrote:
Quoted 6 lines · Toke Høiland-Jørgensen
Quoted 3 lines · Xuan Zhuo

In xsk mode, users cannot use AF_PACKET(tcpdump) to observe the current rx/tx data packets. This feature is very important in many cases. So this patch allows AF_PACKET to obtain xsk packages.

Quoted 5 lines
Quoted 3 lines

You can use xdpdump to dump the packets from the XDP program before it gets redirected into the XSK:
https://github.com/xdp-project/xdp-tools/tree/master/xdp-dump

Wow, this is a good idea.

Yes, it is rather cool (credit to Eelco). Notice the extra info you can capture from 'exit', like XDP return codes, if_index, rx_queue.

The tool uses the perf ring-buffer to send/copy data to userspace. This is actually surprisingly fast, but I still think AF_XDP will be faster (but it usually 'steals' the packet).

Another (crazy?) idea is to extend this (and xdpdump), is to leverage Hangbin's recent XDP_REDIRECT extension e624d4ed4aa8 ("xdp: Extend xdp_redirect_map with broadcast support"). We now have a xdp_redirect_map flag BPF_F_BROADCAST, what if we create a BPF_F_CLONE_PASS flag?

Quoted 5 lines

The semantic meaning of BPF_F_CLONE_PASS flag is to copy/clone the packet for the specified map target index (e.g AF_XDP map), but afterwards it does like veth/cpumap and creates an SKB from the xdp_frame (see __xdp_build_skb_from_frame()) and send to netstack. (Feel free to kick me if this doesn't make any sense)

This would be a smooth way to implement clone support for AF_XDP. If we had this and someone added AF_XDP support to libpcap, we could both capture AF_XDP traffic with tcpdump (using this clone functionality in the XDP program) and speed up tcpdump for dumping traffic destined for regular sockets. Would that solve your use case Xuan? Note that I have not looked into the BPF_F_CLONE_PASS code, so do not know at this point what it would take to support this for XSKMAPs.

Quoted 6 lines

--
Best regards,
Jesper Dangaard Brouer
MSc.CS, Principal Kernel Engineer at Red Hat
LinkedIn: http://www.linkedin.com/in/brouer

maintainer
#9 lore raw
Quoted 36 lines · Magnus Karlsson
Quoted 28 lines · Jesper Dangaard Brouer
Quoted 7 lines · On Fri, 28 May 2021 17:02:01 +0800 Xuan Zhuo <[email protected]> wrote:
Quoted 6 lines · Toke Høiland-Jørgensen
Quoted 3 lines · Xuan Zhuo

In xsk mode, users cannot use AF_PACKET(tcpdump) to observe the current rx/tx data packets. This feature is very important in many cases. So this patch allows AF_PACKET to obtain xsk packages.

Quoted 5 lines
Quoted 3 lines

You can use xdpdump to dump the packets from the XDP program before it gets redirected into the XSK:
https://github.com/xdp-project/xdp-tools/tree/master/xdp-dump

Wow, this is a good idea.

Yes, it is rather cool (credit to Eelco). Notice the extra info you can capture from 'exit', like XDP return codes, if_index, rx_queue.

The tool uses the perf ring-buffer to send/copy data to userspace. This is actually surprisingly fast, but I still think AF_XDP will be faster (but it usually 'steals' the packet).

Another (crazy?) idea is to extend this (and xdpdump), is to leverage Hangbin's recent XDP_REDIRECT extension e624d4ed4aa8 ("xdp: Extend xdp_redirect_map with broadcast support"). We now have a xdp_redirect_map flag BPF_F_BROADCAST, what if we create a BPF_F_CLONE_PASS flag?

Quoted 5 lines

The semantic meaning of BPF_F_CLONE_PASS flag is to copy/clone the packet for the specified map target index (e.g AF_XDP map), but afterwards it does like veth/cpumap and creates an SKB from the xdp_frame (see __xdp_build_skb_from_frame()) and send to netstack. (Feel free to kick me if this doesn't make any sense)

Quoted 7 lines

This would be a smooth way to implement clone support for AF_XDP. If we had this and someone added AF_XDP support to libpcap, we could both capture AF_XDP traffic with tcpdump (using this clone functionality in the XDP program) and speed up tcpdump for dumping traffic destined for regular sockets. Would that solve your use case Xuan? Note that I have not looked into the BPF_F_CLONE_PASS code, so do not know at this point what it would take to support this for XSKMAPs.

Recently also ended up with something similar for our XDP LB to record pcaps [0] ;) My question is.. tcpdump doesn't really care where the packet data comes from, so why not extending libpcap's Linux-related internals to either capture from perf RB or BPF ringbuf rather than AF_PACKET sockets? Cloning is slow, and if you need to end up creating an skb which is then cloned once again inside AF_PACKET it's even worse. Just relying and reading out, say, perf RB you don't need any clones at all.

Thanks,
Daniel

[0] https://cilium.io/blog/2021/05/20/cilium-110#pcap

#10 lore raw
Quoted 45 lines · Daniel Borkmann
Quoted 36 lines · Magnus Karlsson
Quoted 28 lines · Jesper Dangaard Brouer
Quoted 7 lines · On Fri, 28 May 2021 17:02:01 +0800 Xuan Zhuo <[email protected]> wrote:
Quoted 6 lines · Toke Høiland-Jørgensen
Quoted 3 lines · Xuan Zhuo

In xsk mode, users cannot use AF_PACKET(tcpdump) to observe the current rx/tx data packets. This feature is very important in many cases. So this patch allows AF_PACKET to obtain xsk packages.

Quoted 5 lines
Quoted 3 lines

You can use xdpdump to dump the packets from the XDP program before it gets redirected into the XSK:
https://github.com/xdp-project/xdp-tools/tree/master/xdp-dump

Wow, this is a good idea.

Yes, it is rather cool (credit to Eelco). Notice the extra info you can capture from 'exit', like XDP return codes, if_index, rx_queue.

The tool uses the perf ring-buffer to send/copy data to userspace. This is actually surprisingly fast, but I still think AF_XDP will be faster (but it usually 'steals' the packet).

Another (crazy?) idea is to extend this (and xdpdump), is to leverage Hangbin's recent XDP_REDIRECT extension e624d4ed4aa8 ("xdp: Extend xdp_redirect_map with broadcast support"). We now have a xdp_redirect_map flag BPF_F_BROADCAST, what if we create a BPF_F_CLONE_PASS flag?

Quoted 5 lines

The semantic meaning of BPF_F_CLONE_PASS flag is to copy/clone the packet for the specified map target index (e.g AF_XDP map), but afterwards it does like veth/cpumap and creates an SKB from the xdp_frame (see __xdp_build_skb_from_frame()) and send to netstack. (Feel free to kick me if this doesn't make any sense)

Quoted 7 lines

This would be a smooth way to implement clone support for AF_XDP. If we had this and someone added AF_XDP support to libpcap, we could both capture AF_XDP traffic with tcpdump (using this clone functionality in the XDP program) and speed up tcpdump for dumping traffic destined for regular sockets. Would that solve your use case Xuan? Note that I have not looked into the BPF_F_CLONE_PASS code, so do not know at this point what it would take to support this for XSKMAPs.

Quoted 7 lines

Recently also ended up with something similar for our XDP LB to record pcaps [0] ;) My question is.. tcpdump doesn't really care where the packet data comes from, so why not extending libpcap's Linux-related internals to either capture from perf RB or BPF ringbuf rather than AF_PACKET sockets? Cloning is slow, and if you need to end up creating an skb which is then cloned once again inside AF_PACKET it's even worse. Just relying and reading out, say, perf RB you don't need any clones at all.

We discussed this when creating xdpdump and decided to keep it as a separate tool for the time being. I forget the details of the discussion, maybe Eelco remembers.

Anyway, xdpdump does have a "pipe pcap to stdout" feature so you can do xdpdump | tcpdump and get the interactive output; and it will also save pcap information to disk, of course (using pcap-ng so it can also save metadata like XDP program name and return code).

-Toke

maintainer
#11 lore raw
Quoted 54 lines · Toke Høiland-Jørgensen
Quoted 45 lines · Daniel Borkmann
Quoted 36 lines · Magnus Karlsson
Quoted 28 lines · Jesper Dangaard Brouer
Quoted 7 lines · On Fri, 28 May 2021 17:02:01 +0800 Xuan Zhuo <[email protected]> wrote:
Quoted 6 lines · Toke Høiland-Jørgensen
Quoted 3 lines · Xuan Zhuo

In xsk mode, users cannot use AF_PACKET(tcpdump) to observe the current rx/tx data packets. This feature is very important in many cases. So this patch allows AF_PACKET to obtain xsk packages.

Quoted 5 lines
Quoted 3 lines

You can use xdpdump to dump the packets from the XDP program before it gets redirected into the XSK:
https://github.com/xdp-project/xdp-tools/tree/master/xdp-dump

Wow, this is a good idea.

Yes, it is rather cool (credit to Eelco). Notice the extra info you can capture from 'exit', like XDP return codes, if_index, rx_queue.

The tool uses the perf ring-buffer to send/copy data to userspace. This is actually surprisingly fast, but I still think AF_XDP will be faster (but it usually 'steals' the packet).

Another (crazy?) idea is to extend this (and xdpdump), is to leverage Hangbin's recent XDP_REDIRECT extension e624d4ed4aa8 ("xdp: Extend xdp_redirect_map with broadcast support"). We now have a xdp_redirect_map flag BPF_F_BROADCAST, what if we create a BPF_F_CLONE_PASS flag?

Quoted 5 lines

The semantic meaning of BPF_F_CLONE_PASS flag is to copy/clone the packet for the specified map target index (e.g AF_XDP map), but afterwards it does like veth/cpumap and creates an SKB from the xdp_frame (see __xdp_build_skb_from_frame()) and send to netstack. (Feel free to kick me if this doesn't make any sense)

Quoted 7 lines

This would be a smooth way to implement clone support for AF_XDP. If we had this and someone added AF_XDP support to libpcap, we could both capture AF_XDP traffic with tcpdump (using this clone functionality in the XDP program) and speed up tcpdump for dumping traffic destined for regular sockets. Would that solve your use case Xuan? Note that I have not looked into the BPF_F_CLONE_PASS code, so do not know at this point what it would take to support this for XSKMAPs.

Quoted 7 lines

Recently also ended up with something similar for our XDP LB to record pcaps [0] ;) My question is.. tcpdump doesn't really care where the packet data comes from, so why not extending libpcap's Linux-related internals to either capture from perf RB or BPF ringbuf rather than AF_PACKET sockets? Cloning is slow, and if you need to end up creating an skb which is then cloned once again inside AF_PACKET it's even worse. Just relying and reading out, say, perf RB you don't need any clones at all.

Quoted 8 lines

We discussed this when creating xdpdump and decided to keep it as a separate tool for the time being. I forget the details of the discussion, maybe Eelco remembers.

Anyway, xdpdump does have a "pipe pcap to stdout" feature so you can do xdpdump | tcpdump and get the interactive output; and it will also save pcap information to disk, of course (using pcap-ng so it can also save metadata like XDP program name and return code).

Right, and this should yield a significantly better performance compared to cloning & pushing traffic into AF_PACKET. I presume not many folks are aware of xdpdump (yet) which is probably why such patch was created here.. a native libpcap implementation could solve that aspect fwiw and additionally hook at the same points as AF_PACKET via BPF but without the hassle/overhead of things like dev_queue_xmit_nit() in fast path. (Maybe another option could be to have a drop-in replacement libpcap.so for tcpdump using it transparently.)

Thanks,
Daniel

#12 lore raw
Quoted 59 lines · Daniel Borkmann
Quoted 58 lines · Toke Høiland-Jørgensen
Quoted 45 lines · Daniel Borkmann
Quoted 36 lines · Magnus Karlsson
Quoted 28 lines · Jesper Dangaard Brouer
Quoted 7 lines · On Fri, 28 May 2021 17:02:01 +0800 Xuan Zhuo <[email protected]> wrote:
Quoted 6 lines · Toke Høiland-Jørgensen
Quoted 3 lines · Xuan Zhuo

In xsk mode, users cannot use AF_PACKET(tcpdump) to observe the current rx/tx data packets. This feature is very important in many cases. So this patch allows AF_PACKET to obtain xsk packages.

Quoted 5 lines
Quoted 3 lines

You can use xdpdump to dump the packets from the XDP program before it gets redirected into the XSK:
https://github.com/xdp-project/xdp-tools/tree/master/xdp-dump

Wow, this is a good idea.

Yes, it is rather cool (credit to Eelco). Notice the extra info you can capture from 'exit', like XDP return codes, if_index, rx_queue.

The tool uses the perf ring-buffer to send/copy data to userspace. This is actually surprisingly fast, but I still think AF_XDP will be faster (but it usually 'steals' the packet).

Another (crazy?) idea is to extend this (and xdpdump), is to leverage Hangbin's recent XDP_REDIRECT extension e624d4ed4aa8 ("xdp: Extend xdp_redirect_map with broadcast support"). We now have a xdp_redirect_map flag BPF_F_BROADCAST, what if we create a BPF_F_CLONE_PASS flag?

Quoted 5 lines

The semantic meaning of BPF_F_CLONE_PASS flag is to copy/clone the packet for the specified map target index (e.g AF_XDP map), but afterwards it does like veth/cpumap and creates an SKB from the xdp_frame (see __xdp_build_skb_from_frame()) and send to netstack. (Feel free to kick me if this doesn't make any sense)

Quoted 7 lines

This would be a smooth way to implement clone support for AF_XDP. If we had this and someone added AF_XDP support to libpcap, we could both capture AF_XDP traffic with tcpdump (using this clone functionality in the XDP program) and speed up tcpdump for dumping traffic destined for regular sockets. Would that solve your use case Xuan? Note that I have not looked into the BPF_F_CLONE_PASS code, so do not know at this point what it would take to support this for XSKMAPs.

Quoted 7 lines

Recently also ended up with something similar for our XDP LB to record pcaps [0] ;) My question is.. tcpdump doesn't really care where the packet data comes from, so why not extending libpcap's Linux-related internals to either capture from perf RB or BPF ringbuf rather than AF_PACKET sockets? Cloning is slow, and if you need to end up creating an skb which is then cloned once again inside AF_PACKET it's even worse. Just relying and reading out, say, perf RB you don't need any clones at all.

We discussed this when creating xdpdump and decided to keep it as a separate tool for the time being. I forget the details of the discussion, maybe Eelco remembers.

Quoted 8 lines
Quoted 4 lines

Anyway, xdpdump does have a "pipe pcap to stdout" feature so you can do xdpdump | tcpdump and get the interactive output; and it will also save pcap information to disk, of course (using pcap-ng so it can also save metadata like XDP program name and return code).

Right, and this should yield a significantly better performance compared to cloning & pushing traffic into AF_PACKET. I presume not many folks are aware of xdpdump (yet) which is probably why such patch was created here..

What, are you implying we haven't achieved world domination yet? Inconceivable! ;)

Quoted 5 lines

a native libpcap implementation could solve that aspect fwiw and additionally hook at the same points as AF_PACKET via BPF but without the hassle/overhead of things like dev_queue_xmit_nit() in fast path. (Maybe another option could be to have a drop-in replacement libpcap.so for tcpdump using it transparently.)

I do believe that Michael was open to adding something like this to tcpdump/libpcap when I last talked to him about it; and I'm certainly not opposed to it either! Hooking up tcpdump like this may be a bit of a firehose, though, so it would be nice to be able to carry over the kernel-side filtering as well. I suppose it should be possible to write an eBPF bytecode generator that does a bit of setup and then just translates the cBPF packet filtering ops, no? This would be cool to have in any case; IIRC Cloudflare did something like that but took a detour through C code generation?

-Toke

#13 lore raw
In reply to Daniel Borkmann
Quoted 37 lines · On Fri, 28 May 2021 12:22:40 +0200 Daniel Borkmann <[email protected]> wrote:
Quoted 36 lines · Magnus Karlsson
Quoted 33 lines · Jesper Dangaard Brouer
Quoted 7 lines · On Fri, 28 May 2021 17:02:01 +0800 Xuan Zhuo <[email protected]> wrote:
Quoted 6 lines · Toke Høiland-Jørgensen
Quoted 3 lines · Xuan Zhuo

In xsk mode, users cannot use AF_PACKET(tcpdump) to observe the current rx/tx data packets. This feature is very important in many cases. So this patch allows AF_PACKET to obtain xsk packages.

Quoted 5 lines
Quoted 3 lines

You can use xdpdump to dump the packets from the XDP program before it gets redirected into the XSK:
https://github.com/xdp-project/xdp-tools/tree/master/xdp-dump

Wow, this is a good idea.

Yes, it is rather cool (credit to Eelco). Notice the extra info you can capture from 'exit', like XDP return codes, if_index, rx_queue.

The tool uses the perf ring-buffer to send/copy data to userspace. This is actually surprisingly fast, but I still think AF_XDP will be faster (but it usually 'steals' the packet).

Another (crazy?) idea is to extend this (and xdpdump), is to leverage Hangbin's recent XDP_REDIRECT extension e624d4ed4aa8 ("xdp: Extend xdp_redirect_map with broadcast support"). We now have a xdp_redirect_map flag BPF_F_BROADCAST, what if we create a BPF_F_CLONE_PASS flag?

The semantic meaning of BPF_F_CLONE_PASS flag is to copy/clone the packet for the specified map target index (e.g AF_XDP map), but afterwards it does like veth/cpumap and creates an SKB from the xdp_frame (see __xdp_build_skb_from_frame()) and send to netstack. (Feel free to kick me if this doesn't make any sense)

Quoted 7 lines
Quoted 7 lines

This would be a smooth way to implement clone support for AF_XDP. If we had this and someone added AF_XDP support to libpcap, we could both capture AF_XDP traffic with tcpdump (using this clone functionality in the XDP program) and speed up tcpdump for dumping traffic destined for regular sockets. Would that solve your use case Xuan? Note that I have not looked into the BPF_F_CLONE_PASS code, so do not know at this point what it would take to support this for XSKMAPs.

There is no spoon... the BPF_F_CLONE_PASS code is an idea.

Quoted 5 lines

Recently also ended up with something similar for our XDP LB to record pcaps [0] ;) My question is.. tcpdump doesn't really care where the packet data comes from, so why not extending libpcap's Linux-related internals to either capture from perf RB or BPF ringbuf

Just want to first mention, that I do like adding a perf ring-buffer (BPF ringbuf) interface to AF_PACKET. But this is basically what xdpdump already does. The cool thing is that it is super flexible for adding extra info like xdpdump does with XDP-return codes.

Quoted 4 lines

rather than AF_PACKET sockets? Cloning is slow, and if
you need to end up creating an skb which is then cloned once again inside AF_PACKET it's even worse. Just relying and reading out, say, perf RB you don't need any clones at all.

Well, this is exactly what we avoid with my idea of BPF_F_CLONE_PASS when combined with AF_XDP.

I should explain this idea better. The trick is that AF_XDP have
preallocated all the packets it will every use (at setup time). Thus,
the AF_XDP copy-mode does no allocations, which is why it is fast (of-cause ZC mode is faster, but copy-mode AF_XDP is also VERY fast!).

(Details and step with AF_XDP code notes:)
When the xdp_do_redirect happens with ri->flags BPF_F_CLONE_PASS, then the map specific enqueue (e.g. __xsk_map_redirect), will do a copy of the xdp_buff (AF_XDP calls xsk_copy_xdp()) and for AF_XDP we don't need to do a (real) allocation. Instead of freeing the xdp_buff in xsk_rcv() (see call to xdp_return_buff()) then we do the xdp_frame to SKB work.

Signature · 4 lines
Best regards,
  Jesper Dangaard Brouer
  MSc.CS, Principal Kernel Engineer at Red Hat
  LinkedIn: http://www.linkedin.com/in/brouer