Skip to contentSkip to conversation

net: add initial support for AF_XDP network backend

PATCH Superseded Ilya Maximets 3 participants 32 messages Jun 22, 2023 – Jul 11, 2023

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: add initial support for AF_XDP network backend

AF_XDP is a network socket family that allows communication directly with the network device driver in the kernel, bypassing most or all of the kernel networking stack. In the essence, the technology is pretty similar to netmap. But, unlike netmap, AF_XDP is Linux-native and works with any network interfaces without driver modifications. Unlike vhost-based backends (kernel, user, vdpa), AF_XDP doesn't require access to character devices or unix sockets. Only access to the network interface itself is necessary.

This patch implements a network backend that communicates with the kernel by creating an AF_XDP socket. A chunk of userspace memory is shared between QEMU and the host kernel. 4 ring buffers (Tx, Rx, Fill and Completion) are placed in that memory along with a pool of memory buffers for the packet data. Data transmission is done by allocating one of the buffers, copying packet data into it and placing the pointer into Tx ring. After transmission, device will return the buffer via Completion ring. On Rx, device will take a buffer form a pre-populated Fill ring, write the packet data into it and place the buffer into Rx ring.

AF_XDP network backend takes on the communication with the host kernel and the network interface and forwards packets to/from the peer device in QEMU.

Usage example:

-device virtio-net-pci,netdev=guest1,mac=00:16:35:AF:AA:5C -netdev af-xdp,ifname=ens6f1np1,id=guest1,mode=native,queues=1

XDP program bridges the socket with a network interface. It can be attached to the interface in 2 different modes:

1. skb - this mode should work for any interface and doesn't require driver support. With a caveat of lower performance.

2. native - this does require support from the driver and allows to bypass skb allocation in the kernel and potentially use zero-copy while getting packets in/out userspace.

By default, QEMU will try to use native mode and fall back to skb. Mode can be forced via 'mode' option. To force 'copy' even in native mode, use 'force-copy=on' option. This might be useful if there is some issue with the driver.

Option 'queues=N' allows to specify how many device queues should be open. Note that all the queues that are not open are still functional and can receive traffic, but it will not be delivered to QEMU. So, the number of device queues should generally match the QEMU configuration, unless the device is shared with something else and the traffic re-direction to appropriate queues is correctly configured on a device level (e.g. with ethtool -N). 'start-queue=M' option can be used to specify from which queue id QEMU should start configuring 'N' queues. It might also be necessary to use this option with certain NICs, e.g. MLX5 NICs. See the docs for examples.

In a general case QEMU will need CAP_NET_ADMIN and CAP_SYS_ADMIN capabilities in order to load default XSK/XDP programs to the network interface and configure BTF maps. It is possible, however, to run only with CAP_NET_RAW. For that to work, an external process with admin capabilities will need to pre-load default XSK program and pass an open file descriptor for this program's 'xsks_map' to QEMU process on startup. Network backend will need to be configured with 'inhibit=on' to avoid loading of the programs. The file descriptor for 'xsks_map' can be passed via 'xsks-map-fd=N' option.

There are few performance challenges with the current network backends.

First is that they do not support IO threads. This means that data path is handled by the main thread in QEMU and may slow down other work or may be slowed down by some other work. This also means that taking advantage of multi-queue is generally not possible today.

Another thing is that data path is going through the device emulation code, which is not really optimized for performance. The fastest "frontend" device is virtio-net. But it's not optimized for heavy traffic either, because it expects such use-cases to be handled via some implementation of vhost (user, kernel, vdpa). In practice, we have virtio notifications and rcu lock/unlock on a per-packet basis and not very efficient accesses to the guest memory. Communication channels between backend and frontend devices do not allow passing more than one packet at a time as well.

Some of these challenges can be avoided in the future by adding better batching into device emulation or by implementing vhost-af-xdp variant.

There are also a few kernel limitations. AF_XDP sockets do not support any kinds of checksum or segmentation offloading. Buffers are limited to a page size (4K), i.e. MTU is limited. Multi-buffer support is not implemented for AF_XDP today. Also, transmission in all non-zero-copy modes is synchronous, i.e. done in a syscall. That doesn't allow high packet rates on virtual interfaces.

However, keeping in mind all of these challenges, current implementation of the AF_XDP backend shows a decent performance while running on top of a physical NIC with zero-copy support.

Test setup:

2 VMs running on 2 physical hosts connected via ConnectX6-Dx card. Network backend is configured to open the NIC directly in native mode. The driver supports zero-copy. NIC is configured to use 1 queue.

Inside a VM - iperf3 for basic TCP performance testing and dpdk-testpmd for PPS testing.

iperf3 result:
TCP stream : 19.1 Gbps

dpdk-testpmd (single queue, single CPU core, 64 B packets) results:
Tx only : 3.4 Mpps
Rx only : 2.0 Mpps
L2 FWD Loopback : 1.5 Mpps

In skb mode the same setup shows much lower performance, similar to the setup where pair of physical NICs is replaced with veth a pair:

iperf3 result:
TCP stream : 9 Gbps

dpdk-testpmd (single queue, single CPU core, 64 B packets) results:
Tx only : 1.2 Mpps
Rx only : 1.0 Mpps
L2 FWD Loopback : 0.7 Mpps

Results in skb mode or over the veth are close to results of a tap backend with vhost=on and disabled segmentation offloading bridged with a NIC.

---
 MAINTAINERS                                   |   4 +
 hmp-commands.hx                               |   2 +-
 meson.build                                   |  14 +
 meson_options.txt                             |   2 +
 net/af-xdp.c                                  | 501 ++++++++++++++++++
 net/clients.h                                 |   5 +
 net/meson.build                               |   3 +
 net/net.c                                     |   6 +
 qapi/net.json                                 |  54 +-
 qemu-options.hx                               |  61 ++-
 .../ci/org.centos/stream/8/x86_64/configure   |   1 +
 scripts/meson-buildoptions.sh                 |   3 +
 tests/docker/dockerfiles/debian-amd64.docker  |   1 +
 13 files changed, 654 insertions(+), 3 deletions(-)
 create mode 100644 net/af-xdp.c
MAINTAINERS +4 -0
index 7f323cd2eb..ca85422676 100644
@@ -2925,6 +2925,10 @@ W: http://info.iet.unipi.it/~luigi/netmap/
 S: Maintained
 F: net/netmap.c
 
+AF_XDP network backend
+R: Ilya Maximets <[email protected]>
+F: net/af-xdp.c
+
 Host Memory Backends
 M: David Hildenbrand <[email protected]>
 M: Igor Mammedov <[email protected]>
hmp-commands.hx +1 -1
index 2cbd0f77a0..af9ffe4681 100644
@@ -1295,7 +1295,7 @@ ERST
     {
         .name       = "netdev_add",
         .args_type  = "netdev:O",
-        .params     = "[user|tap|socket|stream|dgram|vde|bridge|hubport|netmap|vhost-user"
+        .params     = "[user|tap|socket|stream|dgram|vde|bridge|hubport|netmap|af-xdp|vhost-user"
 #ifdef CONFIG_VMNET
                       "|vmnet-host|vmnet-shared|vmnet-bridged"
 #endif
meson.build +14 -0
index 6ef78ea278..d0abb658c5 100644
@@ -1883,6 +1883,18 @@ if libbpf.found() and not cc.links('''
   endif
 endif
 
+# libxdp
+libxdp = dependency('libxdp', required: get_option('af_xdp'), method: 'pkg-config')
+if libxdp.found() and \
+      not (libbpf.found() and libbpf.version().version_compare('>=0.7'))
+  libxdp = not_found
+  if get_option('af_xdp').enabled()
+    error('af-xdp support requires libbpf version >= 0.7')
+  else
+    warning('af-xdp support requires libbpf version >= 0.7, disabling')
+  endif
+endif
+
 # libdw
 libdw = not_found
 if not get_option('libdw').auto() or \
@@ -2106,6 +2118,7 @@ config_host_data.set('CONFIG_HEXAGON_IDEF_PARSER', get_option('hexagon_idef_pars
 config_host_data.set('CONFIG_LIBATTR', have_old_libattr)
 config_host_data.set('CONFIG_LIBCAP_NG', libcap_ng.found())
 config_host_data.set('CONFIG_EBPF', libbpf.found())
+config_host_data.set('CONFIG_AF_XDP', libxdp.found())
 config_host_data.set('CONFIG_LIBDAXCTL', libdaxctl.found())
 config_host_data.set('CONFIG_LIBISCSI', libiscsi.found())
 config_host_data.set('CONFIG_LIBNFS', libnfs.found())
@@ -4279,6 +4292,7 @@ summary_info += {'PVRDMA support':    have_pvrdma}
 summary_info += {'fdt support':       fdt_opt == 'disabled' ? false : fdt_opt}
 summary_info += {'libcap-ng support': libcap_ng}
 summary_info += {'bpf support':       libbpf}
+summary_info += {'AF_XDP support':    libxdp}
 summary_info += {'rbd support':       rbd}
 summary_info += {'smartcard support': cacard}
 summary_info += {'U2F support':       u2f}
meson_options.txt +2 -0
index 90237389e2..31596d59f1 100644
@@ -120,6 +120,8 @@ option('avx512bw', type: 'feature', value: 'auto',
 option('keyring', type: 'feature', value: 'auto',
        description: 'Linux keyring support')
 
+option('af_xdp', type : 'feature', value : 'auto',
+       description: 'AF_XDP network backend support')
 option('attr', type : 'feature', value : 'auto',
        description: 'attr/xattr support')
 option('auth_pam', type : 'feature', value : 'auto',
net/af-xdp.c new file +501 -0

501 lines not on this page · raw message

net/clients.h +5 -0
index ed8bdfff1e..be53794582 100644
@@ -64,6 +64,11 @@ int net_init_netmap(const Netdev *netdev, const char *name,
                     NetClientState *peer, Error **errp);
 #endif
 
+#ifdef CONFIG_AF_XDP
+int net_init_af_xdp(const Netdev *netdev, const char *name,
+                    NetClientState *peer, Error **errp);
+#endif
+
 int net_init_vhost_user(const Netdev *netdev, const char *name,
                         NetClientState *peer, Error **errp);
 
net/meson.build +3 -0
index bdf564a57b..61628d4684 100644
@@ -36,6 +36,9 @@ system_ss.add(when: vde, if_true: files('vde.c'))
 if have_netmap
   system_ss.add(files('netmap.c'))
 endif
+
+system_ss.add(when: libxdp, if_true: files('af-xdp.c'))
+
 if have_vhost_net_user
   system_ss.add(when: 'CONFIG_VIRTIO_NET', if_true: files('vhost-user.c'), if_false: files('vhost-user-stub.c'))
   system_ss.add(when: 'CONFIG_ALL', if_true: files('vhost-user-stub.c'))
net/net.c +6 -0
index 6492ad530e..127f70932b 100644
@@ -1082,6 +1082,9 @@ static int (* const net_client_init_fun[NET_CLIENT_DRIVER__MAX])(
 #ifdef CONFIG_NETMAP
         [NET_CLIENT_DRIVER_NETMAP]    = net_init_netmap,
 #endif
+#ifdef CONFIG_AF_XDP
+        [NET_CLIENT_DRIVER_AF_XDP]    = net_init_af_xdp,
+#endif
 #ifdef CONFIG_NET_BRIDGE
         [NET_CLIENT_DRIVER_BRIDGE]    = net_init_bridge,
 #endif
@@ -1186,6 +1189,9 @@ void show_netdevs(void)
 #ifdef CONFIG_NETMAP
         "netmap",
 #endif
+#ifdef CONFIG_AF_XDP
+        "af-xdp",
+#endif
 #ifdef CONFIG_POSIX
         "vhost-user",
 #endif
qapi/net.json +53 -1

78 lines not on this page · raw message

qemu-options.hx +60 -1

91 lines not on this page · raw message

scripts/ci/org.centos/stream/8/x86_64/configure +1 -0
index d02b09a4b9..7585c4c4ed 100755
@@ -35,6 +35,7 @@
 --block-drv-ro-whitelist="vmdk,vhdx,vpc,https,ssh" \
 --with-coroutine=ucontext \
 --tls-priority=@QEMU,SYSTEM \
+--disable-af-xdp \
 --disable-attr \
 --disable-auth-pam \
 --disable-avx2 \
scripts/meson-buildoptions.sh +3 -0
index 5714fd93d9..e1490fd4fe 100644
@@ -75,6 +75,7 @@ meson_options_help() {
   printf "%s\n" 'disabled with --disable-FEATURE, default is enabled if available'
   printf "%s\n" '(unless built with --without-default-features):'
   printf "%s\n" ''
+  printf "%s\n" '  af-xdp          AF_XDP network backend support'
   printf "%s\n" '  alsa            ALSA sound support'
   printf "%s\n" '  attr            attr/xattr support'
   printf "%s\n" '  auth-pam        PAM access control'
@@ -208,6 +209,8 @@ meson_options_help() {
 }
 _meson_option_parse() {
   case $1 in
+    --enable-af-xdp) printf "%s" -Daf_xdp=enabled ;;
+    --disable-af-xdp) printf "%s" -Daf_xdp=disabled ;;
     --enable-alsa) printf "%s" -Dalsa=enabled ;;
     --disable-alsa) printf "%s" -Dalsa=disabled ;;
     --enable-attr) printf "%s" -Dattr=enabled ;;
tests/docker/dockerfiles/debian-amd64.docker +1 -0
index e39871c7bb..207f7adfb9 100644
@@ -97,6 +97,7 @@ RUN export DEBIAN_FRONTEND=noninteractive && \
                       libvirglrenderer-dev \
                       libvte-2.91-dev \
                       libxen-dev \
+                      libxdp-dev \
                       libzstd-dev \
                       llvm \
                       locales \
Signature · 1 line
2.40.1
#1 lore raw
Quoted 57 lines · Ilya Maximets

AF_XDP is a network socket family that allows communication directly with the network device driver in the kernel, bypassing most or all of the kernel networking stack. In the essence, the technology is pretty similar to netmap. But, unlike netmap, AF_XDP is Linux-native and works with any network interfaces without driver modifications. Unlike vhost-based backends (kernel, user, vdpa), AF_XDP doesn't require access to character devices or unix sockets. Only access to the network interface itself is necessary.

This patch implements a network backend that communicates with the kernel by creating an AF_XDP socket. A chunk of userspace memory is shared between QEMU and the host kernel. 4 ring buffers (Tx, Rx, Fill and Completion) are placed in that memory along with a pool of memory buffers for the packet data. Data transmission is done by allocating one of the buffers, copying packet data into it and placing the pointer into Tx ring. After transmission, device will return the buffer via Completion ring. On Rx, device will take a buffer form a pre-populated Fill ring, write the packet data into it and place the buffer into Rx ring.

AF_XDP network backend takes on the communication with the host kernel and the network interface and forwards packets to/from the peer device in QEMU.

Usage example:

-device virtio-net-pci,netdev=guest1,mac=00:16:35:AF:AA:5C -netdev af-xdp,ifname=ens6f1np1,id=guest1,mode=native,queues=1

XDP program bridges the socket with a network interface. It can be attached to the interface in 2 different modes:

1. skb - this mode should work for any interface and doesn't require driver support. With a caveat of lower performance.

2. native - this does require support from the driver and allows to bypass skb allocation in the kernel and potentially use zero-copy while getting packets in/out userspace.

By default, QEMU will try to use native mode and fall back to skb. Mode can be forced via 'mode' option. To force 'copy' even in native mode, use 'force-copy=on' option. This might be useful if there is some issue with the driver.

Option 'queues=N' allows to specify how many device queues should be open. Note that all the queues that are not open are still functional and can receive traffic, but it will not be delivered to QEMU. So, the number of device queues should generally match the QEMU configuration, unless the device is shared with something else and the traffic re-direction to appropriate queues is correctly configured on a device level (e.g. with ethtool -N). 'start-queue=M' option can be used to specify from which queue id QEMU should start configuring 'N' queues. It might also be necessary to use this option with certain NICs, e.g. MLX5 NICs. See the docs for examples.

Quoted 3 lines

In a general case QEMU will need CAP_NET_ADMIN and CAP_SYS_ADMIN capabilities in order to load default XSK/XDP programs to the network interface and configure BTF maps.

I think you mean "BPF" actually?

Quoted 2 lines

It is possible, however,
to run only with CAP_NET_RAW.

Qemu often runs without any privileges, so we need to fix it.

I think adding support for SCM_RIGHTS via monitor would be a way to go.

Quoted 10 lines

For that to work, an external process
with admin capabilities will need to pre-load default XSK program and pass an open file descriptor for this program's 'xsks_map' to QEMU process on startup. Network backend will need to be configured with 'inhibit=on' to avoid loading of the programs. The file descriptor for 'xsks_map' can be passed via 'xsks-map-fd=N' option.

There are few performance challenges with the current network backends.

First is that they do not support IO threads.

The current networking codes needs some major recatoring to support IO threads which I'm not sure is worthwhile.

Quoted 15 lines

This means that data
path is handled by the main thread in QEMU and may slow down other work or may be slowed down by some other work. This also means that taking advantage of multi-queue is generally not possible today.

Another thing is that data path is going through the device emulation code, which is not really optimized for performance. The fastest "frontend" device is virtio-net. But it's not optimized for heavy traffic either, because it expects such use-cases to be handled via some implementation of vhost (user, kernel, vdpa). In practice, we have virtio notifications and rcu lock/unlock on a per-packet basis and not very efficient accesses to the guest memory. Communication channels between backend and frontend devices do not allow passing more than one packet at a time as well.

Quoted 2 lines

Some of these challenges can be avoided in the future by adding better batching into device emulation or by implementing vhost-af-xdp variant.

It might require you to register(pin) the whole guest memory to XSK or there could be a copy. Both of them are sub-optimal.

A really interesting project is to do AF_XDP passthrough, then we don't need to care about pin and copy and we will get ultra speed in the guest. (But again, it might needs BPF support in virtio-net).

Quoted 21 lines

There are also a few kernel limitations. AF_XDP sockets do not support any kinds of checksum or segmentation offloading. Buffers are limited to a page size (4K), i.e. MTU is limited. Multi-buffer support is not implemented for AF_XDP today. Also, transmission in all non-zero-copy modes is synchronous, i.e. done in a syscall. That doesn't allow high packet rates on virtual interfaces.

However, keeping in mind all of these challenges, current implementation of the AF_XDP backend shows a decent performance while running on top of a physical NIC with zero-copy support.

Test setup:

2 VMs running on 2 physical hosts connected via ConnectX6-Dx card. Network backend is configured to open the NIC directly in native mode. The driver supports zero-copy. NIC is configured to use 1 queue.

Inside a VM - iperf3 for basic TCP performance testing and dpdk-testpmd for PPS testing.

Quoted 7 lines

iperf3 result:
TCP stream : 19.1 Gbps

dpdk-testpmd (single queue, single CPU core, 64 B packets) results:
Tx only : 3.4 Mpps
Rx only : 2.0 Mpps
L2 FWD Loopback : 1.5 Mpps

I don't object to merging this backend (considering we've already merged netmap) once the code is fine, but the number is not amazing so I wonder what is the use case for this backend?

Thanks

#2 lore raw
Quoted 136 lines · Jason Wang
Quoted 57 lines · Ilya Maximets

AF_XDP is a network socket family that allows communication directly with the network device driver in the kernel, bypassing most or all of the kernel networking stack. In the essence, the technology is pretty similar to netmap. But, unlike netmap, AF_XDP is Linux-native and works with any network interfaces without driver modifications. Unlike vhost-based backends (kernel, user, vdpa), AF_XDP doesn't require access to character devices or unix sockets. Only access to the network interface itself is necessary.

This patch implements a network backend that communicates with the kernel by creating an AF_XDP socket. A chunk of userspace memory is shared between QEMU and the host kernel. 4 ring buffers (Tx, Rx, Fill and Completion) are placed in that memory along with a pool of memory buffers for the packet data. Data transmission is done by allocating one of the buffers, copying packet data into it and placing the pointer into Tx ring. After transmission, device will return the buffer via Completion ring. On Rx, device will take a buffer form a pre-populated Fill ring, write the packet data into it and place the buffer into Rx ring.

AF_XDP network backend takes on the communication with the host kernel and the network interface and forwards packets to/from the peer device in QEMU.

Usage example:

-device virtio-net-pci,netdev=guest1,mac=00:16:35:AF:AA:5C -netdev af-xdp,ifname=ens6f1np1,id=guest1,mode=native,queues=1

XDP program bridges the socket with a network interface. It can be attached to the interface in 2 different modes:

1. skb - this mode should work for any interface and doesn't require driver support. With a caveat of lower performance.

2. native - this does require support from the driver and allows to bypass skb allocation in the kernel and potentially use zero-copy while getting packets in/out userspace.

By default, QEMU will try to use native mode and fall back to skb. Mode can be forced via 'mode' option. To force 'copy' even in native mode, use 'force-copy=on' option. This might be useful if there is some issue with the driver.

Option 'queues=N' allows to specify how many device queues should be open. Note that all the queues that are not open are still functional and can receive traffic, but it will not be delivered to QEMU. So, the number of device queues should generally match the QEMU configuration, unless the device is shared with something else and the traffic re-direction to appropriate queues is correctly configured on a device level (e.g. with ethtool -N). 'start-queue=M' option can be used to specify from which queue id QEMU should start configuring 'N' queues. It might also be necessary to use this option with certain NICs, e.g. MLX5 NICs. See the docs for examples.

Quoted 3 lines

In a general case QEMU will need CAP_NET_ADMIN and CAP_SYS_ADMIN capabilities in order to load default XSK/XDP programs to the network interface and configure BTF maps.

I think you mean "BPF" actually?

Quoted 2 lines

It is possible, however,
to run only with CAP_NET_RAW.

Qemu often runs without any privileges, so we need to fix it.

I think adding support for SCM_RIGHTS via monitor would be a way to go.

Quoted 10 lines

For that to work, an external process
with admin capabilities will need to pre-load default XSK program and pass an open file descriptor for this program's 'xsks_map' to QEMU process on startup. Network backend will need to be configured with 'inhibit=on' to avoid loading of the programs. The file descriptor for 'xsks_map' can be passed via 'xsks-map-fd=N' option.

There are few performance challenges with the current network backends.

First is that they do not support IO threads.

The current networking codes needs some major recatoring to support IO threads which I'm not sure is worthwhile.

Quoted 15 lines

This means that data
path is handled by the main thread in QEMU and may slow down other work or may be slowed down by some other work. This also means that taking advantage of multi-queue is generally not possible today.

Another thing is that data path is going through the device emulation code, which is not really optimized for performance. The fastest "frontend" device is virtio-net. But it's not optimized for heavy traffic either, because it expects such use-cases to be handled via some implementation of vhost (user, kernel, vdpa). In practice, we have virtio notifications and rcu lock/unlock on a per-packet basis and not very efficient accesses to the guest memory. Communication channels between backend and frontend devices do not allow passing more than one packet at a time as well.

Quoted 2 lines

Some of these challenges can be avoided in the future by adding better batching into device emulation or by implementing vhost-af-xdp variant.

It might require you to register(pin) the whole guest memory to XSK or there could be a copy. Both of them are sub-optimal.

A really interesting project is to do AF_XDP passthrough, then we don't need to care about pin and copy and we will get ultra speed in the guest. (But again, it might needs BPF support in virtio-net).

Quoted 24 lines

There are also a few kernel limitations. AF_XDP sockets do not support any kinds of checksum or segmentation offloading. Buffers are limited to a page size (4K), i.e. MTU is limited. Multi-buffer support is not implemented for AF_XDP today. Also, transmission in all non-zero-copy modes is synchronous, i.e. done in a syscall. That doesn't allow high packet rates on virtual interfaces.

However, keeping in mind all of these challenges, current implementation of the AF_XDP backend shows a decent performance while running on top of a physical NIC with zero-copy support.

Test setup:

2 VMs running on 2 physical hosts connected via ConnectX6-Dx card. Network backend is configured to open the NIC directly in native mode. The driver supports zero-copy. NIC is configured to use 1 queue.

Inside a VM - iperf3 for basic TCP performance testing and dpdk-testpmd for PPS testing.

iperf3 result:
TCP stream : 19.1 Gbps

Quoted 8 lines
Quoted 4 lines

dpdk-testpmd (single queue, single CPU core, 64 B packets) results:
Tx only : 3.4 Mpps
Rx only : 2.0 Mpps
L2 FWD Loopback : 1.5 Mpps

I don't object to merging this backend (considering we've already merged netmap) once the code is fine, but the number is not amazing so I wonder what is the use case for this backend?

A more ambitious method is to reuse DPDK via dedicated threads, then we can reuse any of its PMD like AF_XDP.

Thanks

Quoted 2 lines

Thanks

#3 lore raw
Quoted 60 lines · Jason Wang
Quoted 59 lines · Jason Wang
Quoted 57 lines · Ilya Maximets

AF_XDP is a network socket family that allows communication directly with the network device driver in the kernel, bypassing most or all of the kernel networking stack. In the essence, the technology is pretty similar to netmap. But, unlike netmap, AF_XDP is Linux-native and works with any network interfaces without driver modifications. Unlike vhost-based backends (kernel, user, vdpa), AF_XDP doesn't require access to character devices or unix sockets. Only access to the network interface itself is necessary.

This patch implements a network backend that communicates with the kernel by creating an AF_XDP socket. A chunk of userspace memory is shared between QEMU and the host kernel. 4 ring buffers (Tx, Rx, Fill and Completion) are placed in that memory along with a pool of memory buffers for the packet data. Data transmission is done by allocating one of the buffers, copying packet data into it and placing the pointer into Tx ring. After transmission, device will return the buffer via Completion ring. On Rx, device will take a buffer form a pre-populated Fill ring, write the packet data into it and place the buffer into Rx ring.

AF_XDP network backend takes on the communication with the host kernel and the network interface and forwards packets to/from the peer device in QEMU.

Usage example:

-device virtio-net-pci,netdev=guest1,mac=00:16:35:AF:AA:5C -netdev af-xdp,ifname=ens6f1np1,id=guest1,mode=native,queues=1

XDP program bridges the socket with a network interface. It can be attached to the interface in 2 different modes:

1. skb - this mode should work for any interface and doesn't require driver support. With a caveat of lower performance.

2. native - this does require support from the driver and allows to bypass skb allocation in the kernel and potentially use zero-copy while getting packets in/out userspace.

By default, QEMU will try to use native mode and fall back to skb. Mode can be forced via 'mode' option. To force 'copy' even in native mode, use 'force-copy=on' option. This might be useful if there is some issue with the driver.

Option 'queues=N' allows to specify how many device queues should be open. Note that all the queues that are not open are still functional and can receive traffic, but it will not be delivered to QEMU. So, the number of device queues should generally match the QEMU configuration, unless the device is shared with something else and the traffic re-direction to appropriate queues is correctly configured on a device level (e.g. with ethtool -N). 'start-queue=M' option can be used to specify from which queue id QEMU should start configuring 'N' queues. It might also be necessary to use this option with certain NICs, e.g. MLX5 NICs. See the docs for examples.

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

In a general case QEMU will need CAP_NET_ADMIN and CAP_SYS_ADMIN capabilities in order to load default XSK/XDP programs to the network interface and configure BTF maps.

I think you mean "BPF" actually?

"BPF Type Format maps" kind of makes some sense, but yes. :)

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

It is possible, however,
to run only with CAP_NET_RAW.

Qemu often runs without any privileges, so we need to fix it.

I think adding support for SCM_RIGHTS via monitor would be a way to go.

I looked through the code and it seems like we can run completely non-privileged as far as kernel concerned. We'll need an API modification in libxdp though.

The thing is, IIUC, the only syscall that requires CAP_NET_RAW is a base socket creation. Binding and other configuration doesn't require any privileges. So, we could create a socket externally and pass it to QEMU. Should work, unless it's an oversight from the kernel side that needs to be patched. :) libxdp doesn't have a way to specify externally created socket today, so we'll need to change that. Should be easy to do though. I can explore.

In case the bind syscall will actually need CAP_NET_RAW for some reason, we could change the kernel and allow non-privileged bind by utilizing, e.g. SO_BINDTODEVICE. i.e., let the privileged process bind the socket to a particular device, so QEMU can't bind it to a random one. Might be a good use case to allow even if not strictly necessary.

Quoted 31 lines
Quoted 31 lines
Quoted 9 lines

For that to work, an external process
with admin capabilities will need to pre-load default XSK program and pass an open file descriptor for this program's 'xsks_map' to QEMU process on startup. Network backend will need to be configured with 'inhibit=on' to avoid loading of the programs. The file descriptor for 'xsks_map' can be passed via 'xsks-map-fd=N' option.

Quoted 3 lines

There are few performance challenges with the current network backends.

First is that they do not support IO threads.

The current networking codes needs some major recatoring to support IO threads which I'm not sure is worthwhile.

Quoted 15 lines

This means that data
path is handled by the main thread in QEMU and may slow down other work or may be slowed down by some other work. This also means that taking advantage of multi-queue is generally not possible today.

Another thing is that data path is going through the device emulation code, which is not really optimized for performance. The fastest "frontend" device is virtio-net. But it's not optimized for heavy traffic either, because it expects such use-cases to be handled via some implementation of vhost (user, kernel, vdpa). In practice, we have virtio notifications and rcu lock/unlock on a per-packet basis and not very efficient accesses to the guest memory. Communication channels between backend and frontend devices do not allow passing more than one packet at a time as well.

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

Some of these challenges can be avoided in the future by adding better batching into device emulation or by implementing vhost-af-xdp variant.

It might require you to register(pin) the whole guest memory to XSK or there could be a copy. Both of them are sub-optimal.

A single copy by itself shouldn't be a huge problem, right? vhost-user and -kernel do copy packets.

Quoted 4 lines
Quoted 4 lines

A really interesting project is to do AF_XDP passthrough, then we don't need to care about pin and copy and we will get ultra speed in the guest. (But again, it might needs BPF support in virtio-net).

I suppose, if we're doing pass-through we need a new device type and a driver in the kernel/dpdk. There is no point pretending it's a virtio-net and translating between different ring layouts. Or is there?

Quoted 25 lines
Quoted 25 lines
Quoted 25 lines

There are also a few kernel limitations. AF_XDP sockets do not support any kinds of checksum or segmentation offloading. Buffers are limited to a page size (4K), i.e. MTU is limited. Multi-buffer support is not implemented for AF_XDP today. Also, transmission in all non-zero-copy modes is synchronous, i.e. done in a syscall. That doesn't allow high packet rates on virtual interfaces.

However, keeping in mind all of these challenges, current implementation of the AF_XDP backend shows a decent performance while running on top of a physical NIC with zero-copy support.

Test setup:

2 VMs running on 2 physical hosts connected via ConnectX6-Dx card. Network backend is configured to open the NIC directly in native mode. The driver supports zero-copy. NIC is configured to use 1 queue.

Inside a VM - iperf3 for basic TCP performance testing and dpdk-testpmd for PPS testing.

iperf3 result:
TCP stream : 19.1 Gbps

Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

dpdk-testpmd (single queue, single CPU core, 64 B packets) results:
Tx only : 3.4 Mpps
Rx only : 2.0 Mpps
L2 FWD Loopback : 1.5 Mpps

I don't object to merging this backend (considering we've already merged netmap) once the code is fine, but the number is not amazing so I wonder what is the use case for this backend?

I don't think there is a use case right now that would significantly benefit from the current implementation, so I'm fine if the merge is postponed. It is noticeably more performant than a tap with vhost=on in terms of PPS. So, that might be one case. Taking into account that just rcu lock and unlock in virtio-net code takes more time than a packet copy, some batching on QEMU side should improve performance significantly. And it shouldn't be too hard to implement.

Performance over virtual interfaces may potentially be improved by creating a kernel thread for async Tx. Similarly to what io_uring allows. Currently Tx on non-zero-copy interfaces is synchronous, and that doesn't allow to scale well.

So, I do think that there is a potential in this backend.

The main benefit, assuming we can reach performance comparable with other high-performance backends (vhost-user), I think, is the fact that it's Linux-native and doesn't require talking with any other devices (like chardevs/sockets), except for a network interface itself. i.e. it could be easier to manage in complex environments.

Quoted 2 lines

A more ambitious method is to reuse DPDK via dedicated threads, then we can reuse any of its PMD like AF_XDP.

Linking with DPDK will make configuration much more complex. I don't think it makes sense to bring it in for AF_XDP specifically. Might be a separate project though, sure.

Best regards, Ilya Maximets.

#4 lore raw
Quoted 78 lines · Ilya Maximets
Quoted 60 lines · Jason Wang
Quoted 59 lines · Jason Wang
Quoted 57 lines · Ilya Maximets

AF_XDP is a network socket family that allows communication directly with the network device driver in the kernel, bypassing most or all of the kernel networking stack. In the essence, the technology is pretty similar to netmap. But, unlike netmap, AF_XDP is Linux-native and works with any network interfaces without driver modifications. Unlike vhost-based backends (kernel, user, vdpa), AF_XDP doesn't require access to character devices or unix sockets. Only access to the network interface itself is necessary.

This patch implements a network backend that communicates with the kernel by creating an AF_XDP socket. A chunk of userspace memory is shared between QEMU and the host kernel. 4 ring buffers (Tx, Rx, Fill and Completion) are placed in that memory along with a pool of memory buffers for the packet data. Data transmission is done by allocating one of the buffers, copying packet data into it and placing the pointer into Tx ring. After transmission, device will return the buffer via Completion ring. On Rx, device will take a buffer form a pre-populated Fill ring, write the packet data into it and place the buffer into Rx ring.

AF_XDP network backend takes on the communication with the host kernel and the network interface and forwards packets to/from the peer device in QEMU.

Usage example:

-device virtio-net-pci,netdev=guest1,mac=00:16:35:AF:AA:5C -netdev af-xdp,ifname=ens6f1np1,id=guest1,mode=native,queues=1

XDP program bridges the socket with a network interface. It can be attached to the interface in 2 different modes:

1. skb - this mode should work for any interface and doesn't require driver support. With a caveat of lower performance.

2. native - this does require support from the driver and allows to bypass skb allocation in the kernel and potentially use zero-copy while getting packets in/out userspace.

By default, QEMU will try to use native mode and fall back to skb. Mode can be forced via 'mode' option. To force 'copy' even in native mode, use 'force-copy=on' option. This might be useful if there is some issue with the driver.

Option 'queues=N' allows to specify how many device queues should be open. Note that all the queues that are not open are still functional and can receive traffic, but it will not be delivered to QEMU. So, the number of device queues should generally match the QEMU configuration, unless the device is shared with something else and the traffic re-direction to appropriate queues is correctly configured on a device level (e.g. with ethtool -N). 'start-queue=M' option can be used to specify from which queue id QEMU should start configuring 'N' queues. It might also be necessary to use this option with certain NICs, e.g. MLX5 NICs. See the docs for examples.

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

In a general case QEMU will need CAP_NET_ADMIN and CAP_SYS_ADMIN capabilities in order to load default XSK/XDP programs to the network interface and configure BTF maps.

I think you mean "BPF" actually?

"BPF Type Format maps" kind of makes some sense, but yes. :)

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

It is possible, however,
to run only with CAP_NET_RAW.

Qemu often runs without any privileges, so we need to fix it.

I think adding support for SCM_RIGHTS via monitor would be a way to go.

Quoted 8 lines

I looked through the code and it seems like we can run completely non-privileged as far as kernel concerned. We'll need an API modification in libxdp though.

The thing is, IIUC, the only syscall that requires CAP_NET_RAW is a base socket creation. Binding and other configuration doesn't require any privileges. So, we could create a socket externally and pass it to QEMU.

That's the way TAP works for example.

Quoted 4 lines

Should work, unless it's an oversight from
the kernel side that needs to be patched. :) libxdp doesn't have a way to specify externally created socket today, so we'll need to change that. Should be easy to do though. I can explore.

Please do that.

Quoted 7 lines

In case the bind syscall will actually need CAP_NET_RAW for some reason, we could change the kernel and allow non-privileged bind by utilizing, e.g. SO_BINDTODEVICE. i.e., let the privileged process bind the socket to a particular device, so QEMU can't bind it to a random one. Might be a good use case to allow even if not strictly necessary.

Yes.

Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
Quoted 10 lines

For that to work, an external process
with admin capabilities will need to pre-load default XSK program and pass an open file descriptor for this program's 'xsks_map' to QEMU process on startup. Network backend will need to be configured with 'inhibit=on' to avoid loading of the programs. The file descriptor for 'xsks_map' can be passed via 'xsks-map-fd=N' option.

Quoted 3 lines

There are few performance challenges with the current network backends.

First is that they do not support IO threads.

The current networking codes needs some major recatoring to support IO threads which I'm not sure is worthwhile.

Quoted 15 lines

This means that data
path is handled by the main thread in QEMU and may slow down other work or may be slowed down by some other work. This also means that taking advantage of multi-queue is generally not possible today.

Another thing is that data path is going through the device emulation code, which is not really optimized for performance. The fastest "frontend" device is virtio-net. But it's not optimized for heavy traffic either, because it expects such use-cases to be handled via some implementation of vhost (user, kernel, vdpa). In practice, we have virtio notifications and rcu lock/unlock on a per-packet basis and not very efficient accesses to the guest memory. Communication channels between backend and frontend devices do not allow passing more than one packet at a time as well.

Quoted 7 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

Some of these challenges can be avoided in the future by adding better batching into device emulation or by implementing vhost-af-xdp variant.

It might require you to register(pin) the whole guest memory to XSK or there could be a copy. Both of them are sub-optimal.

A single copy by itself shouldn't be a huge problem, right?

Probably.

Quoted 10 lines

vhost-user and -kernel do copy packets.

Quoted 4 lines
Quoted 4 lines

A really interesting project is to do AF_XDP passthrough, then we don't need to care about pin and copy and we will get ultra speed in the guest. (But again, it might needs BPF support in virtio-net).

I suppose, if we're doing pass-through we need a new device type and a driver in the kernel/dpdk. There is no point pretending it's a virtio-net and translating between different ring layouts.

Yes.

Quoted 32 lines

Or is there?

Quoted 30 lines
Quoted 30 lines
Quoted 29 lines

There are also a few kernel limitations. AF_XDP sockets do not support any kinds of checksum or segmentation offloading. Buffers are limited to a page size (4K), i.e. MTU is limited. Multi-buffer support is not implemented for AF_XDP today. Also, transmission in all non-zero-copy modes is synchronous, i.e. done in a syscall. That doesn't allow high packet rates on virtual interfaces.

However, keeping in mind all of these challenges, current implementation of the AF_XDP backend shows a decent performance while running on top of a physical NIC with zero-copy support.

Test setup:

2 VMs running on 2 physical hosts connected via ConnectX6-Dx card. Network backend is configured to open the NIC directly in native mode. The driver supports zero-copy. NIC is configured to use 1 queue.

Inside a VM - iperf3 for basic TCP performance testing and dpdk-testpmd for PPS testing.

iperf3 result:
TCP stream : 19.1 Gbps

dpdk-testpmd (single queue, single CPU core, 64 B packets) results:
Tx only : 3.4 Mpps
Rx only : 2.0 Mpps
L2 FWD Loopback : 1.5 Mpps

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

I don't object to merging this backend (considering we've already merged netmap) once the code is fine, but the number is not amazing so I wonder what is the use case for this backend?

I don't think there is a use case right now that would significantly benefit from the current implementation, so I'm fine if the merge is postponed.

Just to be clear, I don't want to postpone this if we decide to invest/enhance it. I will go through the codes and get back.

Quoted 10 lines

It is noticeably more performant than a tap with vhost=on in terms of PPS. So, that might be one case. Taking into account that just rcu lock and unlock in virtio-net code takes more time than a packet copy, some batching on QEMU side should improve performance significantly. And it shouldn't be too hard to implement.

Performance over virtual interfaces may potentially be improved by creating a kernel thread for async Tx. Similarly to what io_uring allows. Currently Tx on non-zero-copy interfaces is synchronous, and that doesn't allow to scale well.

Interestingly, actually, there are a lot of "duplication" between io_uring and AF_XDP:

1) both have similar memory model (user register)
2) both use ring for communication

I wonder if we can let io_uring talks directly to AF_XDP.

Quoted 8 lines

So, I do think that there is a potential in this backend.

The main benefit, assuming we can reach performance comparable with other high-performance backends (vhost-user), I think, is the fact that it's Linux-native and doesn't require talking with any other devices (like chardevs/sockets), except for a network interface itself. i.e. it could be easier to manage in complex environments.

Yes.

Quoted 7 lines
Quoted 3 lines

A more ambitious method is to reuse DPDK via dedicated threads, then we can reuse any of its PMD like AF_XDP.

Linking with DPDK will make configuration much more complex. I don't think it makes sense to bring it in for AF_XDP specifically. Might be a separate project though, sure.

Right.

Thanks

Quoted 3 lines

Best regards, Ilya Maximets.

#5 lore raw
Quoted 88 lines · Jason Wang
Quoted 78 lines · Ilya Maximets
Quoted 60 lines · Jason Wang
Quoted 59 lines · Jason Wang
Quoted 57 lines · Ilya Maximets

AF_XDP is a network socket family that allows communication directly with the network device driver in the kernel, bypassing most or all of the kernel networking stack. In the essence, the technology is pretty similar to netmap. But, unlike netmap, AF_XDP is Linux-native and works with any network interfaces without driver modifications. Unlike vhost-based backends (kernel, user, vdpa), AF_XDP doesn't require access to character devices or unix sockets. Only access to the network interface itself is necessary.

This patch implements a network backend that communicates with the kernel by creating an AF_XDP socket. A chunk of userspace memory is shared between QEMU and the host kernel. 4 ring buffers (Tx, Rx, Fill and Completion) are placed in that memory along with a pool of memory buffers for the packet data. Data transmission is done by allocating one of the buffers, copying packet data into it and placing the pointer into Tx ring. After transmission, device will return the buffer via Completion ring. On Rx, device will take a buffer form a pre-populated Fill ring, write the packet data into it and place the buffer into Rx ring.

AF_XDP network backend takes on the communication with the host kernel and the network interface and forwards packets to/from the peer device in QEMU.

Usage example:

-device virtio-net-pci,netdev=guest1,mac=00:16:35:AF:AA:5C -netdev af-xdp,ifname=ens6f1np1,id=guest1,mode=native,queues=1

XDP program bridges the socket with a network interface. It can be attached to the interface in 2 different modes:

1. skb - this mode should work for any interface and doesn't require driver support. With a caveat of lower performance.

2. native - this does require support from the driver and allows to bypass skb allocation in the kernel and potentially use zero-copy while getting packets in/out userspace.

By default, QEMU will try to use native mode and fall back to skb. Mode can be forced via 'mode' option. To force 'copy' even in native mode, use 'force-copy=on' option. This might be useful if there is some issue with the driver.

Option 'queues=N' allows to specify how many device queues should be open. Note that all the queues that are not open are still functional and can receive traffic, but it will not be delivered to QEMU. So, the number of device queues should generally match the QEMU configuration, unless the device is shared with something else and the traffic re-direction to appropriate queues is correctly configured on a device level (e.g. with ethtool -N). 'start-queue=M' option can be used to specify from which queue id QEMU should start configuring 'N' queues. It might also be necessary to use this option with certain NICs, e.g. MLX5 NICs. See the docs for examples.

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

In a general case QEMU will need CAP_NET_ADMIN and CAP_SYS_ADMIN capabilities in order to load default XSK/XDP programs to the network interface and configure BTF maps.

I think you mean "BPF" actually?

"BPF Type Format maps" kind of makes some sense, but yes. :)

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

It is possible, however,
to run only with CAP_NET_RAW.

Qemu often runs without any privileges, so we need to fix it.

I think adding support for SCM_RIGHTS via monitor would be a way to go.

Quoted 8 lines

I looked through the code and it seems like we can run completely non-privileged as far as kernel concerned. We'll need an API modification in libxdp though.

The thing is, IIUC, the only syscall that requires CAP_NET_RAW is a base socket creation. Binding and other configuration doesn't require any privileges. So, we could create a socket externally and pass it to QEMU.

Quoted 8 lines

That's the way TAP works for example.

Quoted 4 lines

Should work, unless it's an oversight from
the kernel side that needs to be patched. :) libxdp doesn't have a way to specify externally created socket today, so we'll need to change that. Should be easy to do though. I can explore.

Please do that.

I have a prototype:
https://github.com/igsilya/xdp-tools/commit/db73e90945e3aa5e451ac88c42c83cb9389642d3

Need to test it out and then submit PR to xdp-tools project.

Quoted 10 lines
Quoted 8 lines

In case the bind syscall will actually need CAP_NET_RAW for some reason, we could change the kernel and allow non-privileged bind by utilizing, e.g. SO_BINDTODEVICE. i.e., let the privileged process bind the socket to a particular device, so QEMU can't bind it to a random one. Might be a good use case to allow even if not strictly necessary.

Yes.

Will propose something for a kernel as well. We might want something more granular though, e.g. bind to a queue instead of a device. In case we want better control in the device sharing scenario.

Quoted 92 lines
Quoted 33 lines
Quoted 33 lines
Quoted 33 lines
Quoted 11 lines

For that to work, an external process
with admin capabilities will need to pre-load default XSK program and pass an open file descriptor for this program's 'xsks_map' to QEMU process on startup. Network backend will need to be configured with 'inhibit=on' to avoid loading of the programs. The file descriptor for 'xsks_map' can be passed via 'xsks-map-fd=N' option.

Quoted 3 lines

There are few performance challenges with the current network backends.

First is that they do not support IO threads.

The current networking codes needs some major recatoring to support IO threads which I'm not sure is worthwhile.

Quoted 15 lines

This means that data
path is handled by the main thread in QEMU and may slow down other work or may be slowed down by some other work. This also means that taking advantage of multi-queue is generally not possible today.

Another thing is that data path is going through the device emulation code, which is not really optimized for performance. The fastest "frontend" device is virtio-net. But it's not optimized for heavy traffic either, because it expects such use-cases to be handled via some implementation of vhost (user, kernel, vdpa). In practice, we have virtio notifications and rcu lock/unlock on a per-packet basis and not very efficient accesses to the guest memory. Communication channels between backend and frontend devices do not allow passing more than one packet at a time as well.

Quoted 7 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

Some of these challenges can be avoided in the future by adding better batching into device emulation or by implementing vhost-af-xdp variant.

It might require you to register(pin) the whole guest memory to XSK or there could be a copy. Both of them are sub-optimal.

A single copy by itself shouldn't be a huge problem, right?

Probably.

Quoted 10 lines

vhost-user and -kernel do copy packets.

Quoted 4 lines
Quoted 4 lines

A really interesting project is to do AF_XDP passthrough, then we don't need to care about pin and copy and we will get ultra speed in the guest. (But again, it might needs BPF support in virtio-net).

I suppose, if we're doing pass-through we need a new device type and a driver in the kernel/dpdk. There is no point pretending it's a virtio-net and translating between different ring layouts.

Yes.

Quoted 36 lines

Or is there?

Quoted 33 lines
Quoted 33 lines
Quoted 22 lines

There are also a few kernel limitations. AF_XDP sockets do not support any kinds of checksum or segmentation offloading. Buffers are limited to a page size (4K), i.e. MTU is limited. Multi-buffer support is not implemented for AF_XDP today. Also, transmission in all non-zero-copy modes is synchronous, i.e. done in a syscall. That doesn't allow high packet rates on virtual interfaces.

However, keeping in mind all of these challenges, current implementation of the AF_XDP backend shows a decent performance while running on top of a physical NIC with zero-copy support.

Test setup:

2 VMs running on 2 physical hosts connected via ConnectX6-Dx card. Network backend is configured to open the NIC directly in native mode. The driver supports zero-copy. NIC is configured to use 1 queue.

Inside a VM - iperf3 for basic TCP performance testing and dpdk-testpmd for PPS testing.

Quoted 7 lines

iperf3 result:
TCP stream : 19.1 Gbps

dpdk-testpmd (single queue, single CPU core, 64 B packets) results:
Tx only : 3.4 Mpps
Rx only : 2.0 Mpps
L2 FWD Loopback : 1.5 Mpps

I don't object to merging this backend (considering we've already merged netmap) once the code is fine, but the number is not amazing so I wonder what is the use case for this backend?

Quoted 5 lines
Quoted 2 lines

I don't think there is a use case right now that would significantly benefit from the current implementation, so I'm fine if the merge is postponed.

Just to be clear, I don't want to postpone this if we decide to invest/enhance it. I will go through the codes and get back.

Ack. Thanks.

Quoted 12 lines
Quoted 11 lines

It is noticeably more performant than a tap with vhost=on in terms of PPS. So, that might be one case. Taking into account that just rcu lock and unlock in virtio-net code takes more time than a packet copy, some batching on QEMU side should improve performance significantly. And it shouldn't be too hard to implement.

Performance over virtual interfaces may potentially be improved by creating a kernel thread for async Tx. Similarly to what io_uring allows. Currently Tx on non-zero-copy interfaces is synchronous, and that doesn't allow to scale well.

Quoted 7 lines

Interestingly, actually, there are a lot of "duplication" between io_uring and AF_XDP:

1) both have similar memory model (user register)
2) both use ring for communication

I wonder if we can let io_uring talks directly to AF_XDP.

Well, if we submit poll() in QEMU main loop via io_uring, then we can avoid cost of the synchronous Tx for non-zero-copy modes, i.e. for virtual interfaces. io_uring thread in the kernel will be able to perform transmission for us.

But yeah, there are way too many way too similar ring buffer interfaces in the kernel.

Quoted 28 lines
Quoted 9 lines

So, I do think that there is a potential in this backend.

The main benefit, assuming we can reach performance comparable with other high-performance backends (vhost-user), I think, is the fact that it's Linux-native and doesn't require talking with any other devices (like chardevs/sockets), except for a network interface itself. i.e. it could be easier to manage in complex environments.

Yes.

Quoted 7 lines
Quoted 3 lines

A more ambitious method is to reuse DPDK via dedicated threads, then we can reuse any of its PMD like AF_XDP.

Linking with DPDK will make configuration much more complex. I don't think it makes sense to bring it in for AF_XDP specifically. Might be a separate project though, sure.

Right.

Thanks

Quoted 3 lines

Best regards, Ilya Maximets.

#6 lore raw
Quoted 113 lines · Ilya Maximets
Quoted 88 lines · Jason Wang
Quoted 78 lines · Ilya Maximets
Quoted 60 lines · Jason Wang
Quoted 59 lines · Jason Wang
Quoted 57 lines · Ilya Maximets

AF_XDP is a network socket family that allows communication directly with the network device driver in the kernel, bypassing most or all of the kernel networking stack. In the essence, the technology is pretty similar to netmap. But, unlike netmap, AF_XDP is Linux-native and works with any network interfaces without driver modifications. Unlike vhost-based backends (kernel, user, vdpa), AF_XDP doesn't require access to character devices or unix sockets. Only access to the network interface itself is necessary.

This patch implements a network backend that communicates with the kernel by creating an AF_XDP socket. A chunk of userspace memory is shared between QEMU and the host kernel. 4 ring buffers (Tx, Rx, Fill and Completion) are placed in that memory along with a pool of memory buffers for the packet data. Data transmission is done by allocating one of the buffers, copying packet data into it and placing the pointer into Tx ring. After transmission, device will return the buffer via Completion ring. On Rx, device will take a buffer form a pre-populated Fill ring, write the packet data into it and place the buffer into Rx ring.

AF_XDP network backend takes on the communication with the host kernel and the network interface and forwards packets to/from the peer device in QEMU.

Usage example:

-device virtio-net-pci,netdev=guest1,mac=00:16:35:AF:AA:5C -netdev af-xdp,ifname=ens6f1np1,id=guest1,mode=native,queues=1

XDP program bridges the socket with a network interface. It can be attached to the interface in 2 different modes:

1. skb - this mode should work for any interface and doesn't require driver support. With a caveat of lower performance.

2. native - this does require support from the driver and allows to bypass skb allocation in the kernel and potentially use zero-copy while getting packets in/out userspace.

By default, QEMU will try to use native mode and fall back to skb. Mode can be forced via 'mode' option. To force 'copy' even in native mode, use 'force-copy=on' option. This might be useful if there is some issue with the driver.

Option 'queues=N' allows to specify how many device queues should be open. Note that all the queues that are not open are still functional and can receive traffic, but it will not be delivered to QEMU. So, the number of device queues should generally match the QEMU configuration, unless the device is shared with something else and the traffic re-direction to appropriate queues is correctly configured on a device level (e.g. with ethtool -N). 'start-queue=M' option can be used to specify from which queue id QEMU should start configuring 'N' queues. It might also be necessary to use this option with certain NICs, e.g. MLX5 NICs. See the docs for examples.

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

In a general case QEMU will need CAP_NET_ADMIN and CAP_SYS_ADMIN capabilities in order to load default XSK/XDP programs to the network interface and configure BTF maps.

I think you mean "BPF" actually?

"BPF Type Format maps" kind of makes some sense, but yes. :)

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

It is possible, however,
to run only with CAP_NET_RAW.

Qemu often runs without any privileges, so we need to fix it.

I think adding support for SCM_RIGHTS via monitor would be a way to go.

Quoted 8 lines

I looked through the code and it seems like we can run completely non-privileged as far as kernel concerned. We'll need an API modification in libxdp though.

The thing is, IIUC, the only syscall that requires CAP_NET_RAW is a base socket creation. Binding and other configuration doesn't require any privileges. So, we could create a socket externally and pass it to QEMU.

Quoted 8 lines

That's the way TAP works for example.

Quoted 4 lines

Should work, unless it's an oversight from
the kernel side that needs to be patched. :) libxdp doesn't have a way to specify externally created socket today, so we'll need to change that. Should be easy to do though. I can explore.

Please do that.

I have a prototype:
https://github.com/igsilya/xdp-tools/commit/db73e90945e3aa5e451ac88c42c83cb9389642d3

Need to test it out and then submit PR to xdp-tools project.

Quoted 9 lines
Quoted 8 lines

In case the bind syscall will actually need CAP_NET_RAW for some reason, we could change the kernel and allow non-privileged bind by utilizing, e.g. SO_BINDTODEVICE. i.e., let the privileged process bind the socket to a particular device, so QEMU can't bind it to a random one. Might be a good use case to allow even if not strictly necessary.

Quoted 5 lines
Quoted 1 line

Yes.

Will propose something for a kernel as well. We might want something more granular though, e.g. bind to a queue instead of a device. In case we want better control in the device sharing scenario.

I may miss something but the bind is already done at dev plus queue right now, isn't it?

Quoted 119 lines
Quoted 93 lines
Quoted 34 lines
Quoted 34 lines
Quoted 34 lines
Quoted 12 lines

For that to work, an external process
with admin capabilities will need to pre-load default XSK program and pass an open file descriptor for this program's 'xsks_map' to QEMU process on startup. Network backend will need to be configured with 'inhibit=on' to avoid loading of the programs. The file descriptor for 'xsks_map' can be passed via 'xsks-map-fd=N' option.

Quoted 3 lines

There are few performance challenges with the current network backends.

First is that they do not support IO threads.

The current networking codes needs some major recatoring to support IO threads which I'm not sure is worthwhile.

Quoted 15 lines

This means that data
path is handled by the main thread in QEMU and may slow down other work or may be slowed down by some other work. This also means that taking advantage of multi-queue is generally not possible today.

Another thing is that data path is going through the device emulation code, which is not really optimized for performance. The fastest "frontend" device is virtio-net. But it's not optimized for heavy traffic either, because it expects such use-cases to be handled via some implementation of vhost (user, kernel, vdpa). In practice, we have virtio notifications and rcu lock/unlock on a per-packet basis and not very efficient accesses to the guest memory. Communication channels between backend and frontend devices do not allow passing more than one packet at a time as well.

Quoted 7 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

Some of these challenges can be avoided in the future by adding better batching into device emulation or by implementing vhost-af-xdp variant.

It might require you to register(pin) the whole guest memory to XSK or there could be a copy. Both of them are sub-optimal.

A single copy by itself shouldn't be a huge problem, right?

Probably.

Quoted 10 lines

vhost-user and -kernel do copy packets.

Quoted 4 lines
Quoted 4 lines

A really interesting project is to do AF_XDP passthrough, then we don't need to care about pin and copy and we will get ultra speed in the guest. (But again, it might needs BPF support in virtio-net).

I suppose, if we're doing pass-through we need a new device type and a driver in the kernel/dpdk. There is no point pretending it's a virtio-net and translating between different ring layouts.

Yes.

Quoted 36 lines

Or is there?

Quoted 33 lines
Quoted 33 lines
Quoted 22 lines

There are also a few kernel limitations. AF_XDP sockets do not support any kinds of checksum or segmentation offloading. Buffers are limited to a page size (4K), i.e. MTU is limited. Multi-buffer support is not implemented for AF_XDP today. Also, transmission in all non-zero-copy modes is synchronous, i.e. done in a syscall. That doesn't allow high packet rates on virtual interfaces.

However, keeping in mind all of these challenges, current implementation of the AF_XDP backend shows a decent performance while running on top of a physical NIC with zero-copy support.

Test setup:

2 VMs running on 2 physical hosts connected via ConnectX6-Dx card. Network backend is configured to open the NIC directly in native mode. The driver supports zero-copy. NIC is configured to use 1 queue.

Inside a VM - iperf3 for basic TCP performance testing and dpdk-testpmd for PPS testing.

Quoted 7 lines

iperf3 result:
TCP stream : 19.1 Gbps

dpdk-testpmd (single queue, single CPU core, 64 B packets) results:
Tx only : 3.4 Mpps
Rx only : 2.0 Mpps
L2 FWD Loopback : 1.5 Mpps

I don't object to merging this backend (considering we've already merged netmap) once the code is fine, but the number is not amazing so I wonder what is the use case for this backend?

Quoted 5 lines
Quoted 2 lines

I don't think there is a use case right now that would significantly benefit from the current implementation, so I'm fine if the merge is postponed.

Just to be clear, I don't want to postpone this if we decide to invest/enhance it. I will go through the codes and get back.

Ack. Thanks.

Quoted 18 lines
Quoted 11 lines

It is noticeably more performant than a tap with vhost=on in terms of PPS. So, that might be one case. Taking into account that just rcu lock and unlock in virtio-net code takes more time than a packet copy, some batching on QEMU side should improve performance significantly. And it shouldn't be too hard to implement.

Performance over virtual interfaces may potentially be improved by creating a kernel thread for async Tx. Similarly to what io_uring allows. Currently Tx on non-zero-copy interfaces is synchronous, and that doesn't allow to scale well.

Interestingly, actually, there are a lot of "duplication" between io_uring and AF_XDP:

1) both have similar memory model (user register)
2) both use ring for communication

Quoted 6 lines
Quoted 1 line

I wonder if we can let io_uring talks directly to AF_XDP.

Well, if we submit poll() in QEMU main loop via io_uring, then we can avoid cost of the synchronous Tx for non-zero-copy modes, i.e. for virtual interfaces. io_uring thread in the kernel will be able to perform transmission for us.

It would be nice if we can use iothread/vhost other than the main loop even if io_uring can use kthreads. We can avoid the memory translation cost.

Thanks

Quoted 33 lines

But yeah, there are way too many way too similar ring buffer interfaces in the kernel.

Quoted 28 lines
Quoted 9 lines

So, I do think that there is a potential in this backend.

The main benefit, assuming we can reach performance comparable with other high-performance backends (vhost-user), I think, is the fact that it's Linux-native and doesn't require talking with any other devices (like chardevs/sockets), except for a network interface itself. i.e. it could be easier to manage in complex environments.

Yes.

Quoted 7 lines
Quoted 3 lines

A more ambitious method is to reuse DPDK via dedicated threads, then we can reuse any of its PMD like AF_XDP.

Linking with DPDK will make configuration much more complex. I don't think it makes sense to bring it in for AF_XDP specifically. Might be a separate project though, sure.

Right.

Thanks

Quoted 3 lines

Best regards, Ilya Maximets.

#7 lore raw
Quoted 29 lines · Jason Wang
Quoted 27 lines · Ilya Maximets
Quoted 24 lines · Jason Wang
Quoted 11 lines · Ilya Maximets
Quoted 3 lines · Jason Wang
Quoted 2 lines · Jason Wang

On Fri, Jun 23, 2023 at 5:58 AM Ilya Maximets <[email protected]> wrote:

It is noticeably more performant than a tap with vhost=on in terms of PPS. So, that might be one case. Taking into account that just rcu lock and unlock in virtio-net code takes more time than a packet copy, some batching on QEMU side should improve performance significantly. And it shouldn't be too hard to implement.

Quoted 4 lines

Performance over virtual interfaces may potentially be improved by creating a kernel thread for async Tx. Similarly to what io_uring allows. Currently Tx on non-zero-copy interfaces is synchronous, and that doesn't allow to scale well.

Interestingly, actually, there are a lot of "duplication" between io_uring and AF_XDP:

1) both have similar memory model (user register)
2) both use ring for communication

I wonder if we can let io_uring talks directly to AF_XDP.

Quoted 8 lines
Quoted 4 lines

Well, if we submit poll() in QEMU main loop via io_uring, then we can avoid cost of the synchronous Tx for non-zero-copy modes, i.e. for virtual interfaces. io_uring thread in the kernel will be able to perform transmission for us.

It would be nice if we can use iothread/vhost other than the main loop even if io_uring can use kthreads. We can avoid the memory translation cost.

The QEMU event loop (AioContext) has io_uring code (utils/fdmon-io_uring.c) but it's disabled at the moment. I'm working on patches to re-enable it and will probably send them in July. The patches also add an API to submit arbitrary io_uring operations so that you can do stuff besides file descriptor monitoring. Both the main loop and IOThreads will be able to use io_uring on Linux hosts.

Stefan

#8 lore raw
Quoted 40 lines · Stefan Hajnoczi
Quoted 29 lines · Jason Wang
Quoted 27 lines · Ilya Maximets
Quoted 24 lines · Jason Wang
Quoted 11 lines · Ilya Maximets
Quoted 3 lines · Jason Wang
Quoted 2 lines · Jason Wang

On Fri, Jun 23, 2023 at 5:58 AM Ilya Maximets <[email protected]> wrote:

It is noticeably more performant than a tap with vhost=on in terms of PPS. So, that might be one case. Taking into account that just rcu lock and unlock in virtio-net code takes more time than a packet copy, some batching on QEMU side should improve performance significantly. And it shouldn't be too hard to implement.

Quoted 4 lines

Performance over virtual interfaces may potentially be improved by creating a kernel thread for async Tx. Similarly to what io_uring allows. Currently Tx on non-zero-copy interfaces is synchronous, and that doesn't allow to scale well.

Interestingly, actually, there are a lot of "duplication" between io_uring and AF_XDP:

1) both have similar memory model (user register)
2) both use ring for communication

I wonder if we can let io_uring talks directly to AF_XDP.

Quoted 8 lines
Quoted 4 lines

Well, if we submit poll() in QEMU main loop via io_uring, then we can avoid cost of the synchronous Tx for non-zero-copy modes, i.e. for virtual interfaces. io_uring thread in the kernel will be able to perform transmission for us.

It would be nice if we can use iothread/vhost other than the main loop even if io_uring can use kthreads. We can avoid the memory translation cost.

Quoted 6 lines

The QEMU event loop (AioContext) has io_uring code (utils/fdmon-io_uring.c) but it's disabled at the moment. I'm working on patches to re-enable it and will probably send them in July. The patches also add an API to submit arbitrary io_uring operations so that you can do stuff besides file descriptor monitoring. Both the main loop and IOThreads will be able to use io_uring on Linux hosts.

Just to make sure I understand. If we still need a copy from guest to io_uring buffer, we still need to go via memory API for GPA which seems expensive.

Vhost seems to be a shortcut for this.

Thanks

Quoted 3 lines

Stefan

#9 lore raw
Quoted 49 lines · Jason Wang
Quoted 40 lines · Stefan Hajnoczi
Quoted 29 lines · Jason Wang
Quoted 27 lines · Ilya Maximets
Quoted 24 lines · Jason Wang
Quoted 11 lines · Ilya Maximets
Quoted 3 lines · Jason Wang
Quoted 2 lines · Jason Wang

On Fri, Jun 23, 2023 at 5:58 AM Ilya Maximets <[email protected]> wrote:

It is noticeably more performant than a tap with vhost=on in terms of PPS. So, that might be one case. Taking into account that just rcu lock and unlock in virtio-net code takes more time than a packet copy, some batching on QEMU side should improve performance significantly. And it shouldn't be too hard to implement.

Quoted 4 lines

Performance over virtual interfaces may potentially be improved by creating a kernel thread for async Tx. Similarly to what io_uring allows. Currently Tx on non-zero-copy interfaces is synchronous, and that doesn't allow to scale well.

Interestingly, actually, there are a lot of "duplication" between io_uring and AF_XDP:

1) both have similar memory model (user register)
2) both use ring for communication

I wonder if we can let io_uring talks directly to AF_XDP.

Quoted 8 lines
Quoted 4 lines

Well, if we submit poll() in QEMU main loop via io_uring, then we can avoid cost of the synchronous Tx for non-zero-copy modes, i.e. for virtual interfaces. io_uring thread in the kernel will be able to perform transmission for us.

It would be nice if we can use iothread/vhost other than the main loop even if io_uring can use kthreads. We can avoid the memory translation cost.

Quoted 6 lines

The QEMU event loop (AioContext) has io_uring code (utils/fdmon-io_uring.c) but it's disabled at the moment. I'm working on patches to re-enable it and will probably send them in July. The patches also add an API to submit arbitrary io_uring operations so that you can do stuff besides file descriptor monitoring. Both the main loop and IOThreads will be able to use io_uring on Linux hosts.

Quoted 5 lines

Just to make sure I understand. If we still need a copy from guest to io_uring buffer, we still need to go via memory API for GPA which seems expensive.

Vhost seems to be a shortcut for this.

I'm not sure how exactly you're thinking of using io_uring.

Simply using io_uring for the event loop (file descriptor monitoring) doesn't involve an extra buffer, but the packet payload still needs to reside in AF_XDP umem, so there is a copy between guest memory and umem. If umem encompasses guest memory, it may be possible to avoid copying the packet payload.

Stefan

#10 lore raw
Quoted 55 lines · Stefan Hajnoczi
Quoted 53 lines · Jason Wang
Quoted 40 lines · Stefan Hajnoczi
Quoted 29 lines · Jason Wang
Quoted 27 lines · Ilya Maximets
Quoted 24 lines · Jason Wang
Quoted 11 lines · Ilya Maximets
Quoted 3 lines · Jason Wang
Quoted 2 lines · Jason Wang

On Fri, Jun 23, 2023 at 5:58 AM Ilya Maximets <[email protected]> wrote:

It is noticeably more performant than a tap with vhost=on in terms of PPS. So, that might be one case. Taking into account that just rcu lock and unlock in virtio-net code takes more time than a packet copy, some batching on QEMU side should improve performance significantly. And it shouldn't be too hard to implement.

Quoted 4 lines

Performance over virtual interfaces may potentially be improved by creating a kernel thread for async Tx. Similarly to what io_uring allows. Currently Tx on non-zero-copy interfaces is synchronous, and that doesn't allow to scale well.

Interestingly, actually, there are a lot of "duplication" between io_uring and AF_XDP:

1) both have similar memory model (user register)
2) both use ring for communication

I wonder if we can let io_uring talks directly to AF_XDP.

Quoted 8 lines
Quoted 4 lines

Well, if we submit poll() in QEMU main loop via io_uring, then we can avoid cost of the synchronous Tx for non-zero-copy modes, i.e. for virtual interfaces. io_uring thread in the kernel will be able to perform transmission for us.

It would be nice if we can use iothread/vhost other than the main loop even if io_uring can use kthreads. We can avoid the memory translation cost.

Quoted 6 lines

The QEMU event loop (AioContext) has io_uring code (utils/fdmon-io_uring.c) but it's disabled at the moment. I'm working on patches to re-enable it and will probably send them in July. The patches also add an API to submit arbitrary io_uring operations so that you can do stuff besides file descriptor monitoring. Both the main loop and IOThreads will be able to use io_uring on Linux hosts.

Just to make sure I understand. If we still need a copy from guest to io_uring buffer, we still need to go via memory API for GPA which seems expensive.

Quoted 8 lines
Quoted 1 line

Vhost seems to be a shortcut for this.

I'm not sure how exactly you're thinking of using io_uring.

Simply using io_uring for the event loop (file descriptor monitoring) doesn't involve an extra buffer, but the packet payload still needs to reside in AF_XDP umem, so there is a copy between guest memory and umem.

So there would be a translation from GPA to HVA (unless io_uring support 2 stages) which needs to go via qemu memory core. And this part seems to be very expensive according to my test in the past.

Quoted 1 line

If umem encompasses guest memory,

It requires you to pin the whole guest memory and a GPA to HVA translation is still required.

Thanks

Quoted 5 lines

it may be possible to avoid
copying the packet payload.

Stefan

#11 lore raw
Quoted 61 lines · Jason Wang
Quoted 59 lines · Stefan Hajnoczi
Quoted 49 lines · Jason Wang
Quoted 40 lines · Stefan Hajnoczi
Quoted 29 lines · Jason Wang
Quoted 27 lines · Ilya Maximets
Quoted 24 lines · Jason Wang
Quoted 11 lines · Ilya Maximets
Quoted 3 lines · Jason Wang
Quoted 2 lines · Jason Wang

On Fri, Jun 23, 2023 at 5:58 AM Ilya Maximets <[email protected]> wrote:

It is noticeably more performant than a tap with vhost=on in terms of PPS. So, that might be one case. Taking into account that just rcu lock and unlock in virtio-net code takes more time than a packet copy, some batching on QEMU side should improve performance significantly. And it shouldn't be too hard to implement.

Quoted 4 lines

Performance over virtual interfaces may potentially be improved by creating a kernel thread for async Tx. Similarly to what io_uring allows. Currently Tx on non-zero-copy interfaces is synchronous, and that doesn't allow to scale well.

Interestingly, actually, there are a lot of "duplication" between io_uring and AF_XDP:

1) both have similar memory model (user register)
2) both use ring for communication

I wonder if we can let io_uring talks directly to AF_XDP.

Quoted 8 lines
Quoted 4 lines

Well, if we submit poll() in QEMU main loop via io_uring, then we can avoid cost of the synchronous Tx for non-zero-copy modes, i.e. for virtual interfaces. io_uring thread in the kernel will be able to perform transmission for us.

It would be nice if we can use iothread/vhost other than the main loop even if io_uring can use kthreads. We can avoid the memory translation cost.

Quoted 6 lines

The QEMU event loop (AioContext) has io_uring code (utils/fdmon-io_uring.c) but it's disabled at the moment. I'm working on patches to re-enable it and will probably send them in July. The patches also add an API to submit arbitrary io_uring operations so that you can do stuff besides file descriptor monitoring. Both the main loop and IOThreads will be able to use io_uring on Linux hosts.

Quoted 5 lines

Just to make sure I understand. If we still need a copy from guest to io_uring buffer, we still need to go via memory API for GPA which seems expensive.

Vhost seems to be a shortcut for this.

I'm not sure how exactly you're thinking of using io_uring.

Quoted 8 lines
Quoted 4 lines

Simply using io_uring for the event loop (file descriptor monitoring) doesn't involve an extra buffer, but the packet payload still needs to reside in AF_XDP umem, so there is a copy between guest memory and umem.

So there would be a translation from GPA to HVA (unless io_uring support 2 stages) which needs to go via qemu memory core. And this part seems to be very expensive according to my test in the past.

Yes, but in the current approach where AF_XDP is implemented as a QEMU netdev, there is already QEMU device emulation (e.g. virtio-net) happening. So the GPA to HVA translation will happen anyway in device emulation.

Are you thinking about AF_XDP passthrough where the guest directly interacts with AF_XDP?

Quoted 4 lines
Quoted 1 line

If umem encompasses guest memory,

It requires you to pin the whole guest memory and a GPA to HVA translation is still required.

Ilya mentioned that umem uses relative offsets instead of absolute memory addresses. In the AF_XDP passthrough case this means no address translation needs to be added to AF_XDP.

Regarding pinning - I wonder if that's something that can be refined in the kernel by adding an AF_XDP flag that enables on-demand pinning of umem. That way only rx and tx buffers that are currently in use will be pinned. The disadvantage is the runtime overhead to pin/unpin pages. I'm not sure whether it's possible to implement this, I haven't checked the kernel code.

Stefan

#12 lore raw
Quoted 68 lines · Stefan Hajnoczi
Quoted 66 lines · Jason Wang
Quoted 63 lines · Stefan Hajnoczi
Quoted 49 lines · Jason Wang
Quoted 40 lines · Stefan Hajnoczi
Quoted 29 lines · Jason Wang
Quoted 27 lines · Ilya Maximets
Quoted 24 lines · Jason Wang
Quoted 11 lines · Ilya Maximets
Quoted 3 lines · Jason Wang
Quoted 2 lines · Jason Wang

On Fri, Jun 23, 2023 at 5:58 AM Ilya Maximets <[email protected]> wrote:

It is noticeably more performant than a tap with vhost=on in terms of PPS. So, that might be one case. Taking into account that just rcu lock and unlock in virtio-net code takes more time than a packet copy, some batching on QEMU side should improve performance significantly. And it shouldn't be too hard to implement.

Quoted 4 lines

Performance over virtual interfaces may potentially be improved by creating a kernel thread for async Tx. Similarly to what io_uring allows. Currently Tx on non-zero-copy interfaces is synchronous, and that doesn't allow to scale well.

Interestingly, actually, there are a lot of "duplication" between io_uring and AF_XDP:

1) both have similar memory model (user register)
2) both use ring for communication

I wonder if we can let io_uring talks directly to AF_XDP.

Quoted 8 lines
Quoted 4 lines

Well, if we submit poll() in QEMU main loop via io_uring, then we can avoid cost of the synchronous Tx for non-zero-copy modes, i.e. for virtual interfaces. io_uring thread in the kernel will be able to perform transmission for us.

It would be nice if we can use iothread/vhost other than the main loop even if io_uring can use kthreads. We can avoid the memory translation cost.

Quoted 6 lines

The QEMU event loop (AioContext) has io_uring code (utils/fdmon-io_uring.c) but it's disabled at the moment. I'm working on patches to re-enable it and will probably send them in July. The patches also add an API to submit arbitrary io_uring operations so that you can do stuff besides file descriptor monitoring. Both the main loop and IOThreads will be able to use io_uring on Linux hosts.

Quoted 5 lines

Just to make sure I understand. If we still need a copy from guest to io_uring buffer, we still need to go via memory API for GPA which seems expensive.

Vhost seems to be a shortcut for this.

I'm not sure how exactly you're thinking of using io_uring.

Simply using io_uring for the event loop (file descriptor monitoring) doesn't involve an extra buffer, but the packet payload still needs to reside in AF_XDP umem, so there is a copy between guest memory and umem.

Quoted 8 lines
Quoted 3 lines

So there would be a translation from GPA to HVA (unless io_uring support 2 stages) which needs to go via qemu memory core. And this part seems to be very expensive according to my test in the past.

Yes, but in the current approach where AF_XDP is implemented as a QEMU netdev, there is already QEMU device emulation (e.g. virtio-net) happening. So the GPA to HVA translation will happen anyway in device emulation.

Just to make sure we're on the same page.

I meant, AF_XDP can do more than e.g 10Mpps. So if we still use the QEMU netdev, it would be very hard to achieve that if we stick to using the Qemu memory core translations which need to take care about too much extra stuff. That's why I suggest using vhost in io threads which only cares about ram so the translation could be very fast.

Quoted 3 lines

Are you thinking about AF_XDP passthrough where the guest directly interacts with AF_XDP?

This could be another way to solve, since it won't use Qemu's memory core to do the translation.

Quoted 9 lines
Quoted 5 lines
Quoted 2 lines

If umem encompasses guest memory,

It requires you to pin the whole guest memory and a GPA to HVA translation is still required.

Ilya mentioned that umem uses relative offsets instead of absolute memory addresses. In the AF_XDP passthrough case this means no address translation needs to be added to AF_XDP.

I don't see how it can avoid the translations as it works at the level of HVA. But what guests submit is PA or even IOVA.

What's more, guest memory could be backed by different memory backends, this means a single umem may not even work.

Quoted 7 lines

Regarding pinning - I wonder if that's something that can be refined in the kernel by adding an AF_XDP flag that enables on-demand pinning of umem. That way only rx and tx buffers that are currently in use will be pinned. The disadvantage is the runtime overhead to pin/unpin pages. I'm not sure whether it's possible to implement this, I haven't checked the kernel code.

It requires the device to do page faults which is not commonly supported nowadays.

Thanks

Quoted 3 lines

Stefan

#13 lore raw
Quoted 79 lines · Jason Wang
Quoted 68 lines · Stefan Hajnoczi
Quoted 66 lines · Jason Wang
Quoted 63 lines · Stefan Hajnoczi
Quoted 49 lines · Jason Wang
Quoted 40 lines · Stefan Hajnoczi
Quoted 29 lines · Jason Wang
Quoted 27 lines · Ilya Maximets
Quoted 24 lines · Jason Wang
Quoted 11 lines · Ilya Maximets
Quoted 3 lines · Jason Wang
Quoted 2 lines · Jason Wang

On Fri, Jun 23, 2023 at 5:58 AM Ilya Maximets <[email protected]> wrote:

It is noticeably more performant than a tap with vhost=on in terms of PPS. So, that might be one case. Taking into account that just rcu lock and unlock in virtio-net code takes more time than a packet copy, some batching on QEMU side should improve performance significantly. And it shouldn't be too hard to implement.

Quoted 4 lines

Performance over virtual interfaces may potentially be improved by creating a kernel thread for async Tx. Similarly to what io_uring allows. Currently Tx on non-zero-copy interfaces is synchronous, and that doesn't allow to scale well.

Interestingly, actually, there are a lot of "duplication" between io_uring and AF_XDP:

1) both have similar memory model (user register)
2) both use ring for communication

I wonder if we can let io_uring talks directly to AF_XDP.

Quoted 8 lines
Quoted 4 lines

Well, if we submit poll() in QEMU main loop via io_uring, then we can avoid cost of the synchronous Tx for non-zero-copy modes, i.e. for virtual interfaces. io_uring thread in the kernel will be able to perform transmission for us.

It would be nice if we can use iothread/vhost other than the main loop even if io_uring can use kthreads. We can avoid the memory translation cost.

Quoted 6 lines

The QEMU event loop (AioContext) has io_uring code (utils/fdmon-io_uring.c) but it's disabled at the moment. I'm working on patches to re-enable it and will probably send them in July. The patches also add an API to submit arbitrary io_uring operations so that you can do stuff besides file descriptor monitoring. Both the main loop and IOThreads will be able to use io_uring on Linux hosts.

Quoted 5 lines

Just to make sure I understand. If we still need a copy from guest to io_uring buffer, we still need to go via memory API for GPA which seems expensive.

Vhost seems to be a shortcut for this.

I'm not sure how exactly you're thinking of using io_uring.

Simply using io_uring for the event loop (file descriptor monitoring) doesn't involve an extra buffer, but the packet payload still needs to reside in AF_XDP umem, so there is a copy between guest memory and umem.

Quoted 8 lines
Quoted 3 lines

So there would be a translation from GPA to HVA (unless io_uring support 2 stages) which needs to go via qemu memory core. And this part seems to be very expensive according to my test in the past.

Yes, but in the current approach where AF_XDP is implemented as a QEMU netdev, there is already QEMU device emulation (e.g. virtio-net) happening. So the GPA to HVA translation will happen anyway in device emulation.

Quoted 7 lines

Just to make sure we're on the same page.

I meant, AF_XDP can do more than e.g 10Mpps. So if we still use the QEMU netdev, it would be very hard to achieve that if we stick to using the Qemu memory core translations which need to take care about too much extra stuff. That's why I suggest using vhost in io threads which only cares about ram so the translation could be very fast.

What does using "vhost in io threads" mean? Is that a vhost kernel approach where userspace dedicates threads (the stuff that Mike Christie has been working on)? I haven't looked at how Mike's recent patches work, but I wouldn't call that approach QEMU IOThreads, because the threads probably don't run the AioContext event loop and instead execute vhost kernel code the entire time.

But despite these questions, I think I'm beginning to understand that you're proposing a vhost_net.ko AF_XDP implementation instead of a userspace QEMU AF_XDP netdev implementation. I wonder if any optimizations can be made when the AF_XDP user is kernel code instead of userspace code.

Quoted 13 lines
Quoted 3 lines

Are you thinking about AF_XDP passthrough where the guest directly interacts with AF_XDP?

This could be another way to solve, since it won't use Qemu's memory core to do the translation.

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

If umem encompasses guest memory,

It requires you to pin the whole guest memory and a GPA to HVA translation is still required.

Quoted 6 lines
Quoted 3 lines

Ilya mentioned that umem uses relative offsets instead of absolute memory addresses. In the AF_XDP passthrough case this means no address translation needs to be added to AF_XDP.

I don't see how it can avoid the translations as it works at the level of HVA. But what guests submit is PA or even IOVA.

In a passthrough scenario the guest is doing AF_XDP, so it writes relative umem offsets, thereby eliminating address translation concerns (the addresses are not PAs or IOVAs). However, this approach probably won't work well with memory hotplug - or at least it will end up becoming a memory translation mechanism in order to support memory hotplug.

Quoted 3 lines

What's more, guest memory could be backed by different memory backends, this means a single umem may not even work.

Maybe. I don't know the nature of umem. If there can be multiple vmas in the umem range, then there should be no issue mixing different memory backends.

Quoted 11 lines
Quoted 8 lines

Regarding pinning - I wonder if that's something that can be refined in the kernel by adding an AF_XDP flag that enables on-demand pinning of umem. That way only rx and tx buffers that are currently in use will be pinned. The disadvantage is the runtime overhead to pin/unpin pages. I'm not sure whether it's possible to implement this, I haven't checked the kernel code.

It requires the device to do page faults which is not commonly supported nowadays.

I don't understand this comment. AF_XDP processes each rx/tx descriptor. At that point it can getuserpages() or similar in order to pin the page. When the memory is no longer needed, it can put those pages. No fault mechanism is needed. What am I missing?

Stefan

#14 lore raw
Quoted 83 lines · Stefan Hajnoczi
Quoted 81 lines · Jason Wang
Quoted 68 lines · Stefan Hajnoczi
Quoted 66 lines · Jason Wang
Quoted 63 lines · Stefan Hajnoczi
Quoted 49 lines · Jason Wang
Quoted 40 lines · Stefan Hajnoczi
Quoted 29 lines · Jason Wang
Quoted 27 lines · Ilya Maximets
Quoted 24 lines · Jason Wang
Quoted 11 lines · Ilya Maximets
Quoted 3 lines · Jason Wang
Quoted 2 lines · Jason Wang

On Fri, Jun 23, 2023 at 5:58 AM Ilya Maximets <[email protected]> wrote:

It is noticeably more performant than a tap with vhost=on in terms of PPS. So, that might be one case. Taking into account that just rcu lock and unlock in virtio-net code takes more time than a packet copy, some batching on QEMU side should improve performance significantly. And it shouldn't be too hard to implement.

Quoted 4 lines

Performance over virtual interfaces may potentially be improved by creating a kernel thread for async Tx. Similarly to what io_uring allows. Currently Tx on non-zero-copy interfaces is synchronous, and that doesn't allow to scale well.

Interestingly, actually, there are a lot of "duplication" between io_uring and AF_XDP:

1) both have similar memory model (user register)
2) both use ring for communication

I wonder if we can let io_uring talks directly to AF_XDP.

Quoted 8 lines
Quoted 4 lines

Well, if we submit poll() in QEMU main loop via io_uring, then we can avoid cost of the synchronous Tx for non-zero-copy modes, i.e. for virtual interfaces. io_uring thread in the kernel will be able to perform transmission for us.

It would be nice if we can use iothread/vhost other than the main loop even if io_uring can use kthreads. We can avoid the memory translation cost.

Quoted 6 lines

The QEMU event loop (AioContext) has io_uring code (utils/fdmon-io_uring.c) but it's disabled at the moment. I'm working on patches to re-enable it and will probably send them in July. The patches also add an API to submit arbitrary io_uring operations so that you can do stuff besides file descriptor monitoring. Both the main loop and IOThreads will be able to use io_uring on Linux hosts.

Quoted 5 lines

Just to make sure I understand. If we still need a copy from guest to io_uring buffer, we still need to go via memory API for GPA which seems expensive.

Vhost seems to be a shortcut for this.

I'm not sure how exactly you're thinking of using io_uring.

Simply using io_uring for the event loop (file descriptor monitoring) doesn't involve an extra buffer, but the packet payload still needs to reside in AF_XDP umem, so there is a copy between guest memory and umem.

Quoted 8 lines
Quoted 3 lines

So there would be a translation from GPA to HVA (unless io_uring support 2 stages) which needs to go via qemu memory core. And this part seems to be very expensive according to my test in the past.

Yes, but in the current approach where AF_XDP is implemented as a QEMU netdev, there is already QEMU device emulation (e.g. virtio-net) happening. So the GPA to HVA translation will happen anyway in device emulation.

Just to make sure we're on the same page.

Quoted 7 lines
Quoted 5 lines

I meant, AF_XDP can do more than e.g 10Mpps. So if we still use the QEMU netdev, it would be very hard to achieve that if we stick to using the Qemu memory core translations which need to take care about too much extra stuff. That's why I suggest using vhost in io threads which only cares about ram so the translation could be very fast.

What does using "vhost in io threads" mean?

It means a vhost userspace dataplane that is implemented via io threads.

Quoted 10 lines

Is that a vhost kernel
approach where userspace dedicates threads (the stuff that Mike Christie has been working on)? I haven't looked at how Mike's recent patches work, but I wouldn't call that approach QEMU IOThreads, because the threads probably don't run the AioContext event loop and instead execute vhost kernel code the entire time.

But despite these questions, I think I'm beginning to understand that you're proposing a vhost_net.ko AF_XDP implementation instead of a userspace QEMU AF_XDP netdev implementation.

Sorry for being unclear, but I'm not proposing that.

Quoted 3 lines

I wonder if any
optimizations can be made when the AF_XDP user is kernel code instead of userspace code.

The only possible way to go is to adapt AF_XDP umem memory model to vhost which I'm not sure of anything we can gain.

Quoted 21 lines
Quoted 14 lines
Quoted 4 lines

Are you thinking about AF_XDP passthrough where the guest directly interacts with AF_XDP?

This could be another way to solve, since it won't use Qemu's memory core to do the translation.

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

If umem encompasses guest memory,

It requires you to pin the whole guest memory and a GPA to HVA translation is still required.

Quoted 6 lines
Quoted 3 lines

Ilya mentioned that umem uses relative offsets instead of absolute memory addresses. In the AF_XDP passthrough case this means no address translation needs to be added to AF_XDP.

I don't see how it can avoid the translations as it works at the level of HVA. But what guests submit is PA or even IOVA.

Quoted 6 lines

In a passthrough scenario the guest is doing AF_XDP, so it writes relative umem offsets, thereby eliminating address translation concerns (the addresses are not PAs or IOVAs). However, this approach probably won't work well with memory hotplug - or at least it will end up becoming a memory translation mechanism in order to support memory hotplug.

Ok.

Quoted 8 lines
Quoted 4 lines

What's more, guest memory could be backed by different memory backends, this means a single umem may not even work.

Maybe. I don't know the nature of umem. If there can be multiple vmas in the umem range, then there should be no issue mixing different memory backends.

If I understand correctly, a single umem requires contiguous VA at least.

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines

Regarding pinning - I wonder if that's something that can be refined in the kernel by adding an AF_XDP flag that enables on-demand pinning of umem. That way only rx and tx buffers that are currently in use will be pinned. The disadvantage is the runtime overhead to pin/unpin pages. I'm not sure whether it's possible to implement this, I haven't checked the kernel code.

Quoted 7 lines
Quoted 2 lines

It requires the device to do page faults which is not commonly supported nowadays.

I don't understand this comment. AF_XDP processes each rx/tx descriptor. At that point it can getuserpages() or similar in order to pin the page. When the memory is no longer needed, it can put those pages. No fault mechanism is needed. What am I missing?

Ok, I think I kind of get you, you mean doing pinning while processing rx/tx buffers? It's not easy since GUP itself is not very fast, it may hit PPS for sure.

Thanks

Quoted 3 lines

Stefan

#15 lore raw
Quoted 91 lines · Jason Wang
Quoted 89 lines · Stefan Hajnoczi
Quoted 86 lines · Jason Wang
Quoted 68 lines · Stefan Hajnoczi
Quoted 66 lines · Jason Wang
Quoted 63 lines · Stefan Hajnoczi
Quoted 49 lines · Jason Wang
Quoted 40 lines · Stefan Hajnoczi
Quoted 29 lines · Jason Wang
Quoted 27 lines · Ilya Maximets
Quoted 24 lines · Jason Wang
Quoted 11 lines · Ilya Maximets
Quoted 3 lines · Jason Wang

On Sun, Jun 25, 2023 at 3:06 PM Jason Wang <[email protected]> wrote:

On Fri, Jun 23, 2023 at 5:58 AM Ilya Maximets <[email protected]> wrote:

It is noticeably more performant than a tap with vhost=on in terms of PPS. So, that might be one case. Taking into account that just rcu lock and unlock in virtio-net code takes more time than a packet copy, some batching on QEMU side should improve performance significantly. And it shouldn't be too hard to implement.

Quoted 4 lines

Performance over virtual interfaces may potentially be improved by creating a kernel thread for async Tx. Similarly to what io_uring allows. Currently Tx on non-zero-copy interfaces is synchronous, and that doesn't allow to scale well.

Interestingly, actually, there are a lot of "duplication" between io_uring and AF_XDP:

1) both have similar memory model (user register)
2) both use ring for communication

I wonder if we can let io_uring talks directly to AF_XDP.

Quoted 8 lines
Quoted 4 lines

Well, if we submit poll() in QEMU main loop via io_uring, then we can avoid cost of the synchronous Tx for non-zero-copy modes, i.e. for virtual interfaces. io_uring thread in the kernel will be able to perform transmission for us.

It would be nice if we can use iothread/vhost other than the main loop even if io_uring can use kthreads. We can avoid the memory translation cost.

Quoted 6 lines

The QEMU event loop (AioContext) has io_uring code (utils/fdmon-io_uring.c) but it's disabled at the moment. I'm working on patches to re-enable it and will probably send them in July. The patches also add an API to submit arbitrary io_uring operations so that you can do stuff besides file descriptor monitoring. Both the main loop and IOThreads will be able to use io_uring on Linux hosts.

Quoted 5 lines

Just to make sure I understand. If we still need a copy from guest to io_uring buffer, we still need to go via memory API for GPA which seems expensive.

Vhost seems to be a shortcut for this.

I'm not sure how exactly you're thinking of using io_uring.

Simply using io_uring for the event loop (file descriptor monitoring) doesn't involve an extra buffer, but the packet payload still needs to reside in AF_XDP umem, so there is a copy between guest memory and umem.

Quoted 8 lines
Quoted 3 lines

So there would be a translation from GPA to HVA (unless io_uring support 2 stages) which needs to go via qemu memory core. And this part seems to be very expensive according to my test in the past.

Yes, but in the current approach where AF_XDP is implemented as a QEMU netdev, there is already QEMU device emulation (e.g. virtio-net) happening. So the GPA to HVA translation will happen anyway in device emulation.

Just to make sure we're on the same page.

I meant, AF_XDP can do more than e.g 10Mpps. So if we still use the QEMU netdev, it would be very hard to achieve that if we stick to using the Qemu memory core translations which need to take care about too much extra stuff. That's why I suggest using vhost in io threads which only cares about ram so the translation could be very fast.

Quoted 3 lines
Quoted 1 line

What does using "vhost in io threads" mean?

It means a vhost userspace dataplane that is implemented via io threads.

AFAIK this does not exist today. QEMU's built-in devices that use IOThreads don't use vhost code. QEMU vhost code is for vhost kernel, vhost-user, or vDPA but not built-in devices that use IOThreads. The built-in devices implement VirtioDeviceClass callbacks directly and use AioContext APIs to run in IOThreads.

Do you have an idea for using vhost code for built-in devices? Maybe it's fastest if you explain your idea and its advantages instead of me guessing.

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
Quoted 6 lines

Regarding pinning - I wonder if that's something that can be refined in the kernel by adding an AF_XDP flag that enables on-demand pinning of umem. That way only rx and tx buffers that are currently in use will be pinned. The disadvantage is the runtime overhead to pin/unpin pages. I'm not sure whether it's possible to implement this, I haven't checked the kernel code.

It requires the device to do page faults which is not commonly supported nowadays.

Quoted 8 lines
Quoted 4 lines

I don't understand this comment. AF_XDP processes each rx/tx descriptor. At that point it can getuserpages() or similar in order to pin the page. When the memory is no longer needed, it can put those pages. No fault mechanism is needed. What am I missing?

Ok, I think I kind of get you, you mean doing pinning while processing rx/tx buffers? It's not easy since GUP itself is not very fast, it may hit PPS for sure.

Yes. It's not as fast as permanently pinning rx/tx buffers, but it supports unpinned guest RAM.

There are variations on this approach, like keeping a certain amount of pages pinned after they have been used so the cost of pinning/unpinning can be avoided when the same pages are reused in the future, but I don't know how effective that is in practice.

Is there a more efficient approach without relying on hardware page fault support?

My understanding is that hardware page fault support is not yet deployed. We'd be left with pinning guest RAM permanently or using a runtime pinning/unpinning approach like I've described.

Stefan

#16 lore raw
Quoted 95 lines · Stefan Hajnoczi
Quoted 93 lines · Jason Wang
Quoted 90 lines · Stefan Hajnoczi
Quoted 79 lines · Jason Wang
Quoted 68 lines · Stefan Hajnoczi
Quoted 66 lines · Jason Wang
Quoted 63 lines · Stefan Hajnoczi
Quoted 49 lines · Jason Wang
Quoted 40 lines · Stefan Hajnoczi
Quoted 29 lines · Jason Wang
Quoted 27 lines · Ilya Maximets
Quoted 24 lines · Jason Wang
Quoted 15 lines · Ilya Maximets

On 6/26/23 08:32, Jason Wang wrote:
On Sun, Jun 25, 2023 at 3:06 PM Jason Wang <[email protected]> wrote:

On Fri, Jun 23, 2023 at 5:58 AM Ilya Maximets <[email protected]> wrote:
It is noticeably more performant than a tap with vhost=on in terms of PPS.
So, that might be one case. Taking into account that just rcu lock and
unlock in virtio-net code takes more time than a packet copy, some batching
on QEMU side should improve performance significantly. And it shouldn't be
too hard to implement.

Performance over virtual interfaces may potentially be improved by creating
a kernel thread for async Tx. Similarly to what io_uring allows. Currently
Tx on non-zero-copy interfaces is synchronous, and that doesn't allow to
scale well.

Interestingly, actually, there are a lot of "duplication" between io_uring and AF_XDP:

1) both have similar memory model (user register)
2) both use ring for communication

I wonder if we can let io_uring talks directly to AF_XDP.

Quoted 8 lines
Quoted 4 lines

Well, if we submit poll() in QEMU main loop via io_uring, then we can avoid cost of the synchronous Tx for non-zero-copy modes, i.e. for virtual interfaces. io_uring thread in the kernel will be able to perform transmission for us.

It would be nice if we can use iothread/vhost other than the main loop even if io_uring can use kthreads. We can avoid the memory translation cost.

Quoted 6 lines

The QEMU event loop (AioContext) has io_uring code (utils/fdmon-io_uring.c) but it's disabled at the moment. I'm working on patches to re-enable it and will probably send them in July. The patches also add an API to submit arbitrary io_uring operations so that you can do stuff besides file descriptor monitoring. Both the main loop and IOThreads will be able to use io_uring on Linux hosts.

Quoted 5 lines

Just to make sure I understand. If we still need a copy from guest to io_uring buffer, we still need to go via memory API for GPA which seems expensive.

Vhost seems to be a shortcut for this.

I'm not sure how exactly you're thinking of using io_uring.

Simply using io_uring for the event loop (file descriptor monitoring) doesn't involve an extra buffer, but the packet payload still needs to reside in AF_XDP umem, so there is a copy between guest memory and umem.

Quoted 8 lines
Quoted 3 lines

So there would be a translation from GPA to HVA (unless io_uring support 2 stages) which needs to go via qemu memory core. And this part seems to be very expensive according to my test in the past.

Yes, but in the current approach where AF_XDP is implemented as a QEMU netdev, there is already QEMU device emulation (e.g. virtio-net) happening. So the GPA to HVA translation will happen anyway in device emulation.

Quoted 7 lines

Just to make sure we're on the same page.

I meant, AF_XDP can do more than e.g 10Mpps. So if we still use the QEMU netdev, it would be very hard to achieve that if we stick to using the Qemu memory core translations which need to take care about too much extra stuff. That's why I suggest using vhost in io threads which only cares about ram so the translation could be very fast.

What does using "vhost in io threads" mean?

Quoted 7 lines
Quoted 1 line

It means a vhost userspace dataplane that is implemented via io threads.

AFAIK this does not exist today. QEMU's built-in devices that use IOThreads don't use vhost code. QEMU vhost code is for vhost kernel, vhost-user, or vDPA but not built-in devices that use IOThreads. The built-in devices implement VirtioDeviceClass callbacks directly and use AioContext APIs to run in IOThreads.

Yes.

Quoted 4 lines

Do you have an idea for using vhost code for built-in devices? Maybe it's fastest if you explain your idea and its advantages instead of me guessing.

It's something like I'd proposed in [1]:

1) a vhost that is implemented via IOThreads
2) memory translation is done via vhost memory table/IOTLB

The advantages are:

1) No 3rd application like DPDK application
2) Attack surface were reduced
3) Better understanding/interactions with device model for things like RSS and IOMMU

There could be some dis-advantages but it's not obvious to me :)

It's something like linking SPDK/DPDK to Qemu.

Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
Quoted 10 lines
Quoted 7 lines

Regarding pinning - I wonder if that's something that can be refined in the kernel by adding an AF_XDP flag that enables on-demand pinning of umem. That way only rx and tx buffers that are currently in use will be pinned. The disadvantage is the runtime overhead to pin/unpin pages. I'm not sure whether it's possible to implement this, I haven't checked the kernel code.

It requires the device to do page faults which is not commonly supported nowadays.

I don't understand this comment. AF_XDP processes each rx/tx descriptor. At that point it can getuserpages() or similar in order to pin the page. When the memory is no longer needed, it can put those pages. No fault mechanism is needed. What am I missing?

Quoted 6 lines
Quoted 3 lines

Ok, I think I kind of get you, you mean doing pinning while processing rx/tx buffers? It's not easy since GUP itself is not very fast, it may hit PPS for sure.

Yes. It's not as fast as permanently pinning rx/tx buffers, but it supports unpinned guest RAM.

Right, it's a balance between pin and PPS. PPS seems to be more important in this case.

Quoted 8 lines

There are variations on this approach, like keeping a certain amount of pages pinned after they have been used so the cost of pinning/unpinning can be avoided when the same pages are reused in the future, but I don't know how effective that is in practice.

Is there a more efficient approach without relying on hardware page fault support?

I guess so, I see some slides that say device page fault is very slow.

Quoted 4 lines

My understanding is that hardware page fault support is not yet deployed. We'd be left with pinning guest RAM permanently or using a runtime pinning/unpinning approach like I've described.

Probably.

Thanks

Quoted 3 lines

Stefan

#17 lore raw
Quoted 117 lines · Jason Wang
Quoted 95 lines · Stefan Hajnoczi
Quoted 93 lines · Jason Wang
Quoted 90 lines · Stefan Hajnoczi
Quoted 79 lines · Jason Wang
Quoted 68 lines · Stefan Hajnoczi
Quoted 66 lines · Jason Wang
Quoted 63 lines · Stefan Hajnoczi
Quoted 49 lines · Jason Wang
Quoted 40 lines · Stefan Hajnoczi
Quoted 29 lines · Jason Wang
Quoted 27 lines · Ilya Maximets
Quoted 24 lines · Jason Wang

On Mon, Jun 26, 2023 at 9:17 PM Ilya Maximets <[email protected]> wrote:

On 6/26/23 08:32, Jason Wang wrote:
On Sun, Jun 25, 2023 at 3:06 PM Jason Wang <[email protected]> wrote:

On Fri, Jun 23, 2023 at 5:58 AM Ilya Maximets <[email protected]> wrote:
It is noticeably more performant than a tap with vhost=on in terms of PPS.
So, that might be one case. Taking into account that just rcu lock and
unlock in virtio-net code takes more time than a packet copy, some batching
on QEMU side should improve performance significantly. And it shouldn't be
too hard to implement.

Performance over virtual interfaces may potentially be improved by creating
a kernel thread for async Tx. Similarly to what io_uring allows. Currently
Tx on non-zero-copy interfaces is synchronous, and that doesn't allow to
scale well.

Interestingly, actually, there are a lot of "duplication" between
io_uring and AF_XDP:

1) both have similar memory model (user register)
2) both use ring for communication

I wonder if we can let io_uring talks directly to AF_XDP.

Quoted 8 lines
Quoted 4 lines

Well, if we submit poll() in QEMU main loop via io_uring, then we can avoid cost of the synchronous Tx for non-zero-copy modes, i.e. for virtual interfaces. io_uring thread in the kernel will be able to perform transmission for us.

It would be nice if we can use iothread/vhost other than the main loop even if io_uring can use kthreads. We can avoid the memory translation cost.

Quoted 6 lines

The QEMU event loop (AioContext) has io_uring code (utils/fdmon-io_uring.c) but it's disabled at the moment. I'm working on patches to re-enable it and will probably send them in July. The patches also add an API to submit arbitrary io_uring operations so that you can do stuff besides file descriptor monitoring. Both the main loop and IOThreads will be able to use io_uring on Linux hosts.

Quoted 5 lines

Just to make sure I understand. If we still need a copy from guest to io_uring buffer, we still need to go via memory API for GPA which seems expensive.

Vhost seems to be a shortcut for this.

I'm not sure how exactly you're thinking of using io_uring.

Simply using io_uring for the event loop (file descriptor monitoring) doesn't involve an extra buffer, but the packet payload still needs to reside in AF_XDP umem, so there is a copy between guest memory and umem.

Quoted 8 lines
Quoted 3 lines

So there would be a translation from GPA to HVA (unless io_uring support 2 stages) which needs to go via qemu memory core. And this part seems to be very expensive according to my test in the past.

Yes, but in the current approach where AF_XDP is implemented as a QEMU netdev, there is already QEMU device emulation (e.g. virtio-net) happening. So the GPA to HVA translation will happen anyway in device emulation.

Quoted 7 lines

Just to make sure we're on the same page.

I meant, AF_XDP can do more than e.g 10Mpps. So if we still use the QEMU netdev, it would be very hard to achieve that if we stick to using the Qemu memory core translations which need to take care about too much extra stuff. That's why I suggest using vhost in io threads which only cares about ram so the translation could be very fast.

What does using "vhost in io threads" mean?

Quoted 7 lines
Quoted 1 line

It means a vhost userspace dataplane that is implemented via io threads.

AFAIK this does not exist today. QEMU's built-in devices that use IOThreads don't use vhost code. QEMU vhost code is for vhost kernel, vhost-user, or vDPA but not built-in devices that use IOThreads. The built-in devices implement VirtioDeviceClass callbacks directly and use AioContext APIs to run in IOThreads.

Yes.

Quoted 4 lines

Do you have an idea for using vhost code for built-in devices? Maybe it's fastest if you explain your idea and its advantages instead of me guessing.

It's something like I'd proposed in [1]:

1) a vhost that is implemented via IOThreads
2) memory translation is done via vhost memory table/IOTLB

Quoted 8 lines

The advantages are:

1) No 3rd application like DPDK application
2) Attack surface were reduced
3) Better understanding/interactions with device model for things like RSS and IOMMU

There could be some dis-advantages but it's not obvious to me :)

Why is QEMU's native device emulation API not the natural choice for writing built-in devices? I don't understand why the vhost interface is desirable for built-in devices.

Quoted 2 lines

It's something like linking SPDK/DPDK to Qemu.

Sergio Lopez tried loading vhost-user devices as shared libraries that run in the QEMU process. It worked as an experiment but wasn't pursued further.

I think that might make sense in specific cases where there is an existing vhost-user codebase that needs to run as part of QEMU.

In this case the AF_XDP code is new, so it's not a case of moving existing code into QEMU.

Quoted 51 lines
Quoted 17 lines
Quoted 17 lines
Quoted 16 lines
Quoted 11 lines
Quoted 8 lines

Regarding pinning - I wonder if that's something that can be refined in the kernel by adding an AF_XDP flag that enables on-demand pinning of umem. That way only rx and tx buffers that are currently in use will be pinned. The disadvantage is the runtime overhead to pin/unpin pages. I'm not sure whether it's possible to implement this, I haven't checked the kernel code.

It requires the device to do page faults which is not commonly supported nowadays.

I don't understand this comment. AF_XDP processes each rx/tx descriptor. At that point it can getuserpages() or similar in order to pin the page. When the memory is no longer needed, it can put those pages. No fault mechanism is needed. What am I missing?

Quoted 6 lines
Quoted 3 lines

Ok, I think I kind of get you, you mean doing pinning while processing rx/tx buffers? It's not easy since GUP itself is not very fast, it may hit PPS for sure.

Yes. It's not as fast as permanently pinning rx/tx buffers, but it supports unpinned guest RAM.

Right, it's a balance between pin and PPS. PPS seems to be more important in this case.

Quoted 8 lines

There are variations on this approach, like keeping a certain amount of pages pinned after they have been used so the cost of pinning/unpinning can be avoided when the same pages are reused in the future, but I don't know how effective that is in practice.

Is there a more efficient approach without relying on hardware page fault support?

I guess so, I see some slides that say device page fault is very slow.

Quoted 4 lines

My understanding is that hardware page fault support is not yet deployed. We'd be left with pinning guest RAM permanently or using a runtime pinning/unpinning approach like I've described.

Probably.

Thanks

Quoted 3 lines

Stefan

#18 lore raw
Quoted 126 lines · Stefan Hajnoczi
Quoted 124 lines · Jason Wang
Quoted 95 lines · Stefan Hajnoczi
Quoted 93 lines · Jason Wang
Quoted 90 lines · Stefan Hajnoczi
Quoted 79 lines · Jason Wang
Quoted 68 lines · Stefan Hajnoczi
Quoted 66 lines · Jason Wang
Quoted 63 lines · Stefan Hajnoczi
Quoted 49 lines · Jason Wang
Quoted 40 lines · Stefan Hajnoczi
Quoted 29 lines · Jason Wang
Quoted 27 lines · Ilya Maximets

On 6/27/23 04:54, Jason Wang wrote:
On Mon, Jun 26, 2023 at 9:17 PM Ilya Maximets <[email protected]> wrote:

On 6/26/23 08:32, Jason Wang wrote:
On Sun, Jun 25, 2023 at 3:06 PM Jason Wang <[email protected]> wrote:

On Fri, Jun 23, 2023 at 5:58 AM Ilya Maximets <[email protected]> wrote:
It is noticeably more performant than a tap with vhost=on in terms of PPS.
So, that might be one case. Taking into account that just rcu lock and
unlock in virtio-net code takes more time than a packet copy, some batching
on QEMU side should improve performance significantly. And it shouldn't be
too hard to implement.

Performance over virtual interfaces may potentially be improved by creating
a kernel thread for async Tx. Similarly to what io_uring allows. Currently
Tx on non-zero-copy interfaces is synchronous, and that doesn't allow to
scale well.

Interestingly, actually, there are a lot of "duplication" between
io_uring and AF_XDP:

1) both have similar memory model (user register)
2) both use ring for communication

I wonder if we can let io_uring talks directly to AF_XDP.

Quoted 8 lines
Quoted 4 lines

Well, if we submit poll() in QEMU main loop via io_uring, then we can
avoid cost of the synchronous Tx for non-zero-copy modes, i.e. for
virtual interfaces. io_uring thread in the kernel will be able to
perform transmission for us.

It would be nice if we can use iothread/vhost other than the main loop even if io_uring can use kthreads. We can avoid the memory translation cost.

Quoted 6 lines

The QEMU event loop (AioContext) has io_uring code (utils/fdmon-io_uring.c) but it's disabled at the moment. I'm working on patches to re-enable it and will probably send them in July. The patches also add an API to submit arbitrary io_uring operations so that you can do stuff besides file descriptor monitoring. Both the main loop and IOThreads will be able to use io_uring on Linux hosts.

Quoted 5 lines

Just to make sure I understand. If we still need a copy from guest to io_uring buffer, we still need to go via memory API for GPA which seems expensive.

Vhost seems to be a shortcut for this.

I'm not sure how exactly you're thinking of using io_uring.

Simply using io_uring for the event loop (file descriptor monitoring) doesn't involve an extra buffer, but the packet payload still needs to reside in AF_XDP umem, so there is a copy between guest memory and umem.

Quoted 8 lines
Quoted 3 lines

So there would be a translation from GPA to HVA (unless io_uring support 2 stages) which needs to go via qemu memory core. And this part seems to be very expensive according to my test in the past.

Yes, but in the current approach where AF_XDP is implemented as a QEMU netdev, there is already QEMU device emulation (e.g. virtio-net) happening. So the GPA to HVA translation will happen anyway in device emulation.

Quoted 7 lines

Just to make sure we're on the same page.

I meant, AF_XDP can do more than e.g 10Mpps. So if we still use the QEMU netdev, it would be very hard to achieve that if we stick to using the Qemu memory core translations which need to take care about too much extra stuff. That's why I suggest using vhost in io threads which only cares about ram so the translation could be very fast.

What does using "vhost in io threads" mean?

Quoted 7 lines
Quoted 1 line

It means a vhost userspace dataplane that is implemented via io threads.

AFAIK this does not exist today. QEMU's built-in devices that use IOThreads don't use vhost code. QEMU vhost code is for vhost kernel, vhost-user, or vDPA but not built-in devices that use IOThreads. The built-in devices implement VirtioDeviceClass callbacks directly and use AioContext APIs to run in IOThreads.

Yes.

Quoted 4 lines

Do you have an idea for using vhost code for built-in devices? Maybe it's fastest if you explain your idea and its advantages instead of me guessing.

It's something like I'd proposed in [1]:

1) a vhost that is implemented via IOThreads
2) memory translation is done via vhost memory table/IOTLB

The advantages are:

1) No 3rd application like DPDK application
2) Attack surface were reduced
3) Better understanding/interactions with device model for things like RSS and IOMMU

Quoted 5 lines
Quoted 1 line

There could be some dis-advantages but it's not obvious to me :)

Why is QEMU's native device emulation API not the natural choice for writing built-in devices? I don't understand why the vhost interface is desirable for built-in devices.

Unless the memory helpers (like address translations) were optimized fully to satisfy this 10M+ PPS.

Not sure if this is too hard, but last time I benchmark, perf told me most of the time spent in the translation.

Using a vhost is a workaround since its memory model is much more simpler so it can skip lots of memory sections like I/O and ROM etc.

Thanks

Quoted 66 lines
Quoted 3 lines

It's something like linking SPDK/DPDK to Qemu.

Sergio Lopez tried loading vhost-user devices as shared libraries that run in the QEMU process. It worked as an experiment but wasn't pursued further.

I think that might make sense in specific cases where there is an existing vhost-user codebase that needs to run as part of QEMU.

In this case the AF_XDP code is new, so it's not a case of moving existing code into QEMU.

Quoted 51 lines
Quoted 17 lines
Quoted 17 lines
Quoted 16 lines
Quoted 11 lines
Quoted 8 lines

Regarding pinning - I wonder if that's something that can be refined in the kernel by adding an AF_XDP flag that enables on-demand pinning of umem. That way only rx and tx buffers that are currently in use will be pinned. The disadvantage is the runtime overhead to pin/unpin pages. I'm not sure whether it's possible to implement this, I haven't checked the kernel code.

It requires the device to do page faults which is not commonly supported nowadays.

I don't understand this comment. AF_XDP processes each rx/tx descriptor. At that point it can getuserpages() or similar in order to pin the page. When the memory is no longer needed, it can put those pages. No fault mechanism is needed. What am I missing?

Quoted 6 lines
Quoted 3 lines

Ok, I think I kind of get you, you mean doing pinning while processing rx/tx buffers? It's not easy since GUP itself is not very fast, it may hit PPS for sure.

Yes. It's not as fast as permanently pinning rx/tx buffers, but it supports unpinned guest RAM.

Right, it's a balance between pin and PPS. PPS seems to be more important in this case.

Quoted 8 lines

There are variations on this approach, like keeping a certain amount of pages pinned after they have been used so the cost of pinning/unpinning can be avoided when the same pages are reused in the future, but I don't know how effective that is in practice.

Is there a more efficient approach without relying on hardware page fault support?

I guess so, I see some slides that say device page fault is very slow.

Quoted 4 lines

My understanding is that hardware page fault support is not yet deployed. We'd be left with pinning guest RAM permanently or using a runtime pinning/unpinning approach like I've described.

Probably.

Thanks

Quoted 3 lines

Stefan

#19 lore raw
Quoted 133 lines · Jason Wang
Quoted 126 lines · Stefan Hajnoczi
Quoted 124 lines · Jason Wang
Quoted 95 lines · Stefan Hajnoczi
Quoted 93 lines · Jason Wang
Quoted 90 lines · Stefan Hajnoczi
Quoted 79 lines · Jason Wang
Quoted 68 lines · Stefan Hajnoczi
Quoted 66 lines · Jason Wang
Quoted 63 lines · Stefan Hajnoczi
Quoted 49 lines · Jason Wang
Quoted 40 lines · Stefan Hajnoczi
Quoted 37 lines · Jason Wang

On Wed, Jun 28, 2023 at 6:45 AM Ilya Maximets <[email protected]> wrote:

On 6/27/23 04:54, Jason Wang wrote:
On Mon, Jun 26, 2023 at 9:17 PM Ilya Maximets <[email protected]> wrote:

On 6/26/23 08:32, Jason Wang wrote:
On Sun, Jun 25, 2023 at 3:06 PM Jason Wang <[email protected]> wrote:

On Fri, Jun 23, 2023 at 5:58 AM Ilya Maximets <[email protected]> wrote:
It is noticeably more performant than a tap with vhost=on in terms of PPS.
So, that might be one case. Taking into account that just rcu lock and
unlock in virtio-net code takes more time than a packet copy, some batching
on QEMU side should improve performance significantly. And it shouldn't be
too hard to implement.

Performance over virtual interfaces may potentially be improved by creating
a kernel thread for async Tx. Similarly to what io_uring allows. Currently
Tx on non-zero-copy interfaces is synchronous, and that doesn't allow to
scale well.

Interestingly, actually, there are a lot of "duplication" between
io_uring and AF_XDP:

1) both have similar memory model (user register)
2) both use ring for communication

I wonder if we can let io_uring talks directly to AF_XDP.

Well, if we submit poll() in QEMU main loop via io_uring, then we can
avoid cost of the synchronous Tx for non-zero-copy modes, i.e. for
virtual interfaces. io_uring thread in the kernel will be able to
perform transmission for us.

It would be nice if we can use iothread/vhost other than the main loop
even if io_uring can use kthreads. We can avoid the memory translation
cost.

Quoted 6 lines

The QEMU event loop (AioContext) has io_uring code (utils/fdmon-io_uring.c) but it's disabled at the moment. I'm working on patches to re-enable it and will probably send them in July. The patches also add an API to submit arbitrary io_uring operations so that you can do stuff besides file descriptor monitoring. Both the main loop and IOThreads will be able to use io_uring on Linux hosts.

Quoted 5 lines

Just to make sure I understand. If we still need a copy from guest to io_uring buffer, we still need to go via memory API for GPA which seems expensive.

Vhost seems to be a shortcut for this.

I'm not sure how exactly you're thinking of using io_uring.

Simply using io_uring for the event loop (file descriptor monitoring) doesn't involve an extra buffer, but the packet payload still needs to reside in AF_XDP umem, so there is a copy between guest memory and umem.

Quoted 8 lines
Quoted 3 lines

So there would be a translation from GPA to HVA (unless io_uring support 2 stages) which needs to go via qemu memory core. And this part seems to be very expensive according to my test in the past.

Yes, but in the current approach where AF_XDP is implemented as a QEMU netdev, there is already QEMU device emulation (e.g. virtio-net) happening. So the GPA to HVA translation will happen anyway in device emulation.

Quoted 7 lines

Just to make sure we're on the same page.

I meant, AF_XDP can do more than e.g 10Mpps. So if we still use the QEMU netdev, it would be very hard to achieve that if we stick to using the Qemu memory core translations which need to take care about too much extra stuff. That's why I suggest using vhost in io threads which only cares about ram so the translation could be very fast.

What does using "vhost in io threads" mean?

Quoted 7 lines
Quoted 1 line

It means a vhost userspace dataplane that is implemented via io threads.

AFAIK this does not exist today. QEMU's built-in devices that use IOThreads don't use vhost code. QEMU vhost code is for vhost kernel, vhost-user, or vDPA but not built-in devices that use IOThreads. The built-in devices implement VirtioDeviceClass callbacks directly and use AioContext APIs to run in IOThreads.

Yes.

Quoted 4 lines

Do you have an idea for using vhost code for built-in devices? Maybe it's fastest if you explain your idea and its advantages instead of me guessing.

It's something like I'd proposed in [1]:

1) a vhost that is implemented via IOThreads
2) memory translation is done via vhost memory table/IOTLB

The advantages are:

1) No 3rd application like DPDK application
2) Attack surface were reduced
3) Better understanding/interactions with device model for things like RSS and IOMMU

Quoted 5 lines
Quoted 1 line

There could be some dis-advantages but it's not obvious to me :)

Why is QEMU's native device emulation API not the natural choice for writing built-in devices? I don't understand why the vhost interface is desirable for built-in devices.

Quoted 8 lines

Unless the memory helpers (like address translations) were optimized fully to satisfy this 10M+ PPS.

Not sure if this is too hard, but last time I benchmark, perf told me most of the time spent in the translation.

Using a vhost is a workaround since its memory model is much more simpler so it can skip lots of memory sections like I/O and ROM etc.

So, we can have a thread running as part of QEMU process that implements vhost functionality for a virtio-net device. And this thread has an optimized way to access memory. What prevents current virtio-net emulation code accessing memory in the same optimized way? i.e. we likely don't actually need to implement the whole vhost-virtio communication protocol in order to have faster memory access from the device emulation code. I mean, if vhost can access device memory faster, why device itself can't?

With that we could probably split the "datapath" part of the virtio-net emulation into a separate thread driven by iothread loop.

Then add batch API for communication with a network backend (af-xdp) to avoid per-packet calls.

These are 3 more or less independent tasks that should allow the similar performance to a full fledged vhost control and dataplane implementation inside QEMU.

Or am I missing something? (Probably)

Quoted 70 lines

Thanks

Quoted 66 lines
Quoted 3 lines

It's something like linking SPDK/DPDK to Qemu.

Sergio Lopez tried loading vhost-user devices as shared libraries that run in the QEMU process. It worked as an experiment but wasn't pursued further.

I think that might make sense in specific cases where there is an existing vhost-user codebase that needs to run as part of QEMU.

In this case the AF_XDP code is new, so it's not a case of moving existing code into QEMU.

Quoted 51 lines
Quoted 17 lines
Quoted 17 lines
Quoted 16 lines
Quoted 11 lines
Quoted 8 lines

Regarding pinning - I wonder if that's something that can be refined in the kernel by adding an AF_XDP flag that enables on-demand pinning of umem. That way only rx and tx buffers that are currently in use will be pinned. The disadvantage is the runtime overhead to pin/unpin pages. I'm not sure whether it's possible to implement this, I haven't checked the kernel code.

It requires the device to do page faults which is not commonly supported nowadays.

I don't understand this comment. AF_XDP processes each rx/tx descriptor. At that point it can getuserpages() or similar in order to pin the page. When the memory is no longer needed, it can put those pages. No fault mechanism is needed. What am I missing?

Quoted 6 lines
Quoted 3 lines

Ok, I think I kind of get you, you mean doing pinning while processing rx/tx buffers? It's not easy since GUP itself is not very fast, it may hit PPS for sure.

Yes. It's not as fast as permanently pinning rx/tx buffers, but it supports unpinned guest RAM.

Right, it's a balance between pin and PPS. PPS seems to be more important in this case.

Quoted 8 lines

There are variations on this approach, like keeping a certain amount of pages pinned after they have been used so the cost of pinning/unpinning can be avoided when the same pages are reused in the future, but I don't know how effective that is in practice.

Is there a more efficient approach without relying on hardware page fault support?

I guess so, I see some slides that say device page fault is very slow.

Quoted 4 lines

My understanding is that hardware page fault support is not yet deployed. We'd be left with pinning guest RAM permanently or using a runtime pinning/unpinning approach like I've described.

Probably.

Thanks

Quoted 3 lines

Stefan

#20 lore raw
Quoted 141 lines · Ilya Maximets
Quoted 139 lines · Jason Wang
Quoted 126 lines · Stefan Hajnoczi
Quoted 124 lines · Jason Wang
Quoted 95 lines · Stefan Hajnoczi
Quoted 93 lines · Jason Wang
Quoted 90 lines · Stefan Hajnoczi
Quoted 79 lines · Jason Wang
Quoted 68 lines · Stefan Hajnoczi
Quoted 66 lines · Jason Wang
Quoted 63 lines · Stefan Hajnoczi
Quoted 49 lines · Jason Wang
Quoted 46 lines · Stefan Hajnoczi

On Wed, 28 Jun 2023 at 05:28, Jason Wang <[email protected]> wrote:

On Wed, Jun 28, 2023 at 6:45 AM Ilya Maximets <[email protected]> wrote:

On 6/27/23 04:54, Jason Wang wrote:
On Mon, Jun 26, 2023 at 9:17 PM Ilya Maximets <[email protected]> wrote:

On 6/26/23 08:32, Jason Wang wrote:
On Sun, Jun 25, 2023 at 3:06 PM Jason Wang <[email protected]> wrote:

On Fri, Jun 23, 2023 at 5:58 AM Ilya Maximets <[email protected]> wrote:
It is noticeably more performant than a tap with vhost=on in terms of PPS.
So, that might be one case. Taking into account that just rcu lock and
unlock in virtio-net code takes more time than a packet copy, some batching
on QEMU side should improve performance significantly. And it shouldn't be
too hard to implement.

Performance over virtual interfaces may potentially be improved by creating
a kernel thread for async Tx. Similarly to what io_uring allows. Currently
Tx on non-zero-copy interfaces is synchronous, and that doesn't allow to
scale well.

Interestingly, actually, there are a lot of "duplication" between
io_uring and AF_XDP:

1) both have similar memory model (user register)
2) both use ring for communication

I wonder if we can let io_uring talks directly to AF_XDP.

Well, if we submit poll() in QEMU main loop via io_uring, then we can
avoid cost of the synchronous Tx for non-zero-copy modes, i.e. for
virtual interfaces. io_uring thread in the kernel will be able to
perform transmission for us.

It would be nice if we can use iothread/vhost other than the main loop
even if io_uring can use kthreads. We can avoid the memory translation
cost.

The QEMU event loop (AioContext) has io_uring code
(utils/fdmon-io_uring.c) but it's disabled at the moment. I'm working
on patches to re-enable it and will probably send them in July. The
patches also add an API to submit arbitrary io_uring operations so
that you can do stuff besides file descriptor monitoring. Both the
main loop and IOThreads will be able to use io_uring on Linux hosts.

Quoted 5 lines

Just to make sure I understand. If we still need a copy from guest to io_uring buffer, we still need to go via memory API for GPA which seems expensive.

Vhost seems to be a shortcut for this.

I'm not sure how exactly you're thinking of using io_uring.

Simply using io_uring for the event loop (file descriptor monitoring) doesn't involve an extra buffer, but the packet payload still needs to reside in AF_XDP umem, so there is a copy between guest memory and umem.

Quoted 8 lines
Quoted 3 lines

So there would be a translation from GPA to HVA (unless io_uring support 2 stages) which needs to go via qemu memory core. And this part seems to be very expensive according to my test in the past.

Yes, but in the current approach where AF_XDP is implemented as a QEMU netdev, there is already QEMU device emulation (e.g. virtio-net) happening. So the GPA to HVA translation will happen anyway in device emulation.

Quoted 7 lines

Just to make sure we're on the same page.

I meant, AF_XDP can do more than e.g 10Mpps. So if we still use the QEMU netdev, it would be very hard to achieve that if we stick to using the Qemu memory core translations which need to take care about too much extra stuff. That's why I suggest using vhost in io threads which only cares about ram so the translation could be very fast.

What does using "vhost in io threads" mean?

Quoted 7 lines
Quoted 1 line

It means a vhost userspace dataplane that is implemented via io threads.

AFAIK this does not exist today. QEMU's built-in devices that use IOThreads don't use vhost code. QEMU vhost code is for vhost kernel, vhost-user, or vDPA but not built-in devices that use IOThreads. The built-in devices implement VirtioDeviceClass callbacks directly and use AioContext APIs to run in IOThreads.

Yes.

Quoted 4 lines

Do you have an idea for using vhost code for built-in devices? Maybe it's fastest if you explain your idea and its advantages instead of me guessing.

It's something like I'd proposed in [1]:

1) a vhost that is implemented via IOThreads
2) memory translation is done via vhost memory table/IOTLB

The advantages are:

1) No 3rd application like DPDK application
2) Attack surface were reduced
3) Better understanding/interactions with device model for things like RSS and IOMMU

Quoted 5 lines
Quoted 1 line

There could be some dis-advantages but it's not obvious to me :)

Why is QEMU's native device emulation API not the natural choice for writing built-in devices? I don't understand why the vhost interface is desirable for built-in devices.

Unless the memory helpers (like address translations) were optimized fully to satisfy this 10M+ PPS.

Not sure if this is too hard, but last time I benchmark, perf told me most of the time spent in the translation.

Quoted 7 lines
Quoted 2 lines

Using a vhost is a workaround since its memory model is much more simpler so it can skip lots of memory sections like I/O and ROM etc.

So, we can have a thread running as part of QEMU process that implements vhost functionality for a virtio-net device. And this thread has an optimized way to access memory. What prevents current virtio-net emulation code accessing memory in the same optimized way?

Current emulation using memory core accessors which needs to take care of a lot of stuff like MMIO or even P2P. Such kind of stuff is not considered since day0 of vhost. You can do some experiment on this e.g just dropping packets after fetching it from the TX ring.

Quoted 4 lines

i.e. we likely don't
actually need to implement the whole vhost-virtio communication protocol in order to have faster memory access from the device emulation code. I mean, if vhost can access device memory faster, why device itself can't?

I'm not saying it can't but it would end up with something similar to vhost. And that's why I'm saying using vhost is a shortcut (at least for a POC).

Thanks

Quoted 84 lines

With that we could probably split the "datapath" part of the virtio-net emulation into a separate thread driven by iothread loop.

Then add batch API for communication with a network backend (af-xdp) to avoid per-packet calls.

These are 3 more or less independent tasks that should allow the similar performance to a full fledged vhost control and dataplane implementation inside QEMU.

Or am I missing something? (Probably)

Quoted 70 lines

Thanks

Quoted 66 lines
Quoted 3 lines

It's something like linking SPDK/DPDK to Qemu.

Sergio Lopez tried loading vhost-user devices as shared libraries that run in the QEMU process. It worked as an experiment but wasn't pursued further.

I think that might make sense in specific cases where there is an existing vhost-user codebase that needs to run as part of QEMU.

In this case the AF_XDP code is new, so it's not a case of moving existing code into QEMU.

Quoted 51 lines
Quoted 17 lines
Quoted 17 lines
Quoted 16 lines
Quoted 11 lines
Quoted 8 lines

Regarding pinning - I wonder if that's something that can be refined in the kernel by adding an AF_XDP flag that enables on-demand pinning of umem. That way only rx and tx buffers that are currently in use will be pinned. The disadvantage is the runtime overhead to pin/unpin pages. I'm not sure whether it's possible to implement this, I haven't checked the kernel code.

It requires the device to do page faults which is not commonly supported nowadays.

I don't understand this comment. AF_XDP processes each rx/tx descriptor. At that point it can getuserpages() or similar in order to pin the page. When the memory is no longer needed, it can put those pages. No fault mechanism is needed. What am I missing?

Quoted 6 lines
Quoted 3 lines

Ok, I think I kind of get you, you mean doing pinning while processing rx/tx buffers? It's not easy since GUP itself is not very fast, it may hit PPS for sure.

Yes. It's not as fast as permanently pinning rx/tx buffers, but it supports unpinned guest RAM.

Right, it's a balance between pin and PPS. PPS seems to be more important in this case.

Quoted 8 lines

There are variations on this approach, like keeping a certain amount of pages pinned after they have been used so the cost of pinning/unpinning can be avoided when the same pages are reused in the future, but I don't know how effective that is in practice.

Is there a more efficient approach without relying on hardware page fault support?

I guess so, I see some slides that say device page fault is very slow.

Quoted 4 lines

My understanding is that hardware page fault support is not yet deployed. We'd be left with pinning guest RAM permanently or using a runtime pinning/unpinning approach like I've described.

Probably.

Thanks

Quoted 3 lines

Stefan

#21 lore raw
Quoted 150 lines · Jason Wang
Quoted 141 lines · Ilya Maximets
Quoted 139 lines · Jason Wang
Quoted 126 lines · Stefan Hajnoczi
Quoted 124 lines · Jason Wang
Quoted 95 lines · Stefan Hajnoczi
Quoted 93 lines · Jason Wang
Quoted 90 lines · Stefan Hajnoczi
Quoted 79 lines · Jason Wang
Quoted 68 lines · Stefan Hajnoczi
Quoted 66 lines · Jason Wang
Quoted 63 lines · Stefan Hajnoczi
Quoted 54 lines · Jason Wang

On Wed, Jun 28, 2023 at 3:46 PM Stefan Hajnoczi <[email protected]> wrote:

On Wed, 28 Jun 2023 at 05:28, Jason Wang <[email protected]> wrote:

On Wed, Jun 28, 2023 at 6:45 AM Ilya Maximets <[email protected]> wrote:

On 6/27/23 04:54, Jason Wang wrote:
On Mon, Jun 26, 2023 at 9:17 PM Ilya Maximets <[email protected]> wrote:

On 6/26/23 08:32, Jason Wang wrote:
On Sun, Jun 25, 2023 at 3:06 PM Jason Wang <[email protected]> wrote:

On Fri, Jun 23, 2023 at 5:58 AM Ilya Maximets <[email protected]> wrote:
It is noticeably more performant than a tap with vhost=on in terms of PPS.
So, that might be one case. Taking into account that just rcu lock and
unlock in virtio-net code takes more time than a packet copy, some batching
on QEMU side should improve performance significantly. And it shouldn't be
too hard to implement.

Performance over virtual interfaces may potentially be improved by creating
a kernel thread for async Tx. Similarly to what io_uring allows. Currently
Tx on non-zero-copy interfaces is synchronous, and that doesn't allow to
scale well.

Interestingly, actually, there are a lot of "duplication" between
io_uring and AF_XDP:

1) both have similar memory model (user register)
2) both use ring for communication

I wonder if we can let io_uring talks directly to AF_XDP.

Well, if we submit poll() in QEMU main loop via io_uring, then we can
avoid cost of the synchronous Tx for non-zero-copy modes, i.e. for
virtual interfaces. io_uring thread in the kernel will be able to
perform transmission for us.

It would be nice if we can use iothread/vhost other than the main loop
even if io_uring can use kthreads. We can avoid the memory translation
cost.

The QEMU event loop (AioContext) has io_uring code
(utils/fdmon-io_uring.c) but it's disabled at the moment. I'm working
on patches to re-enable it and will probably send them in July. The
patches also add an API to submit arbitrary io_uring operations so
that you can do stuff besides file descriptor monitoring. Both the
main loop and IOThreads will be able to use io_uring on Linux hosts.

Just to make sure I understand. If we still need a copy from guest to
io_uring buffer, we still need to go via memory API for GPA which
seems expensive.

Vhost seems to be a shortcut for this.

I'm not sure how exactly you're thinking of using io_uring.

Simply using io_uring for the event loop (file descriptor monitoring) doesn't involve an extra buffer, but the packet payload still needs to reside in AF_XDP umem, so there is a copy between guest memory and umem.

Quoted 8 lines
Quoted 3 lines

So there would be a translation from GPA to HVA (unless io_uring support 2 stages) which needs to go via qemu memory core. And this part seems to be very expensive according to my test in the past.

Yes, but in the current approach where AF_XDP is implemented as a QEMU netdev, there is already QEMU device emulation (e.g. virtio-net) happening. So the GPA to HVA translation will happen anyway in device emulation.

Quoted 7 lines

Just to make sure we're on the same page.

I meant, AF_XDP can do more than e.g 10Mpps. So if we still use the QEMU netdev, it would be very hard to achieve that if we stick to using the Qemu memory core translations which need to take care about too much extra stuff. That's why I suggest using vhost in io threads which only cares about ram so the translation could be very fast.

What does using "vhost in io threads" mean?

Quoted 7 lines
Quoted 1 line

It means a vhost userspace dataplane that is implemented via io threads.

AFAIK this does not exist today. QEMU's built-in devices that use IOThreads don't use vhost code. QEMU vhost code is for vhost kernel, vhost-user, or vDPA but not built-in devices that use IOThreads. The built-in devices implement VirtioDeviceClass callbacks directly and use AioContext APIs to run in IOThreads.

Yes.

Quoted 4 lines

Do you have an idea for using vhost code for built-in devices? Maybe it's fastest if you explain your idea and its advantages instead of me guessing.

It's something like I'd proposed in [1]:

1) a vhost that is implemented via IOThreads
2) memory translation is done via vhost memory table/IOTLB

The advantages are:

1) No 3rd application like DPDK application
2) Attack surface were reduced
3) Better understanding/interactions with device model for things like RSS and IOMMU

Quoted 5 lines
Quoted 1 line

There could be some dis-advantages but it's not obvious to me :)

Why is QEMU's native device emulation API not the natural choice for writing built-in devices? I don't understand why the vhost interface is desirable for built-in devices.

Unless the memory helpers (like address translations) were optimized fully to satisfy this 10M+ PPS.

Not sure if this is too hard, but last time I benchmark, perf told me most of the time spent in the translation.

Quoted 7 lines
Quoted 2 lines

Using a vhost is a workaround since its memory model is much more simpler so it can skip lots of memory sections like I/O and ROM etc.

So, we can have a thread running as part of QEMU process that implements vhost functionality for a virtio-net device. And this thread has an optimized way to access memory. What prevents current virtio-net emulation code accessing memory in the same optimized way?

Quoted 4 lines

Current emulation using memory core accessors which needs to take care of a lot of stuff like MMIO or even P2P. Such kind of stuff is not considered since day0 of vhost. You can do some experiment on this e.g just dropping packets after fetching it from the TX ring.

If I'm reading that right, virtio implementation is using address space caching by utilizing a memory listener and pre-translated addresses of interesting memory regions. Then it's performing address_space_read_cached, which is bypassing all the memory address translation logic on a cache hit. That sounds pretty similar to how memory table is prepared for vhost.

Quoted 97 lines
Quoted 5 lines

i.e. we likely don't
actually need to implement the whole vhost-virtio communication protocol in order to have faster memory access from the device emulation code. I mean, if vhost can access device memory faster, why device itself can't?

I'm not saying it can't but it would end up with something similar to vhost. And that's why I'm saying using vhost is a shortcut (at least for a POC).

Thanks

Quoted 84 lines

With that we could probably split the "datapath" part of the virtio-net emulation into a separate thread driven by iothread loop.

Then add batch API for communication with a network backend (af-xdp) to avoid per-packet calls.

These are 3 more or less independent tasks that should allow the similar performance to a full fledged vhost control and dataplane implementation inside QEMU.

Or am I missing something? (Probably)

Quoted 70 lines

Thanks

Quoted 66 lines
Quoted 3 lines

It's something like linking SPDK/DPDK to Qemu.

Sergio Lopez tried loading vhost-user devices as shared libraries that run in the QEMU process. It worked as an experiment but wasn't pursued further.

I think that might make sense in specific cases where there is an existing vhost-user codebase that needs to run as part of QEMU.

In this case the AF_XDP code is new, so it's not a case of moving existing code into QEMU.

Quoted 51 lines
Quoted 17 lines
Quoted 17 lines
Quoted 16 lines
Quoted 11 lines
Quoted 8 lines

Regarding pinning - I wonder if that's something that can be refined in the kernel by adding an AF_XDP flag that enables on-demand pinning of umem. That way only rx and tx buffers that are currently in use will be pinned. The disadvantage is the runtime overhead to pin/unpin pages. I'm not sure whether it's possible to implement this, I haven't checked the kernel code.

It requires the device to do page faults which is not commonly supported nowadays.

I don't understand this comment. AF_XDP processes each rx/tx descriptor. At that point it can getuserpages() or similar in order to pin the page. When the memory is no longer needed, it can put those pages. No fault mechanism is needed. What am I missing?

Quoted 6 lines
Quoted 3 lines

Ok, I think I kind of get you, you mean doing pinning while processing rx/tx buffers? It's not easy since GUP itself is not very fast, it may hit PPS for sure.

Yes. It's not as fast as permanently pinning rx/tx buffers, but it supports unpinned guest RAM.

Right, it's a balance between pin and PPS. PPS seems to be more important in this case.

Quoted 8 lines

There are variations on this approach, like keeping a certain amount of pages pinned after they have been used so the cost of pinning/unpinning can be avoided when the same pages are reused in the future, but I don't know how effective that is in practice.

Is there a more efficient approach without relying on hardware page fault support?

I guess so, I see some slides that say device page fault is very slow.

Quoted 4 lines

My understanding is that hardware page fault support is not yet deployed. We'd be left with pinning guest RAM permanently or using a runtime pinning/unpinning approach like I've described.

Probably.

Thanks

Quoted 3 lines

Stefan

#22 lore raw
Quoted 157 lines · Ilya Maximets
Quoted 150 lines · Jason Wang
Quoted 141 lines · Ilya Maximets
Quoted 139 lines · Jason Wang
Quoted 126 lines · Stefan Hajnoczi
Quoted 124 lines · Jason Wang
Quoted 95 lines · Stefan Hajnoczi
Quoted 93 lines · Jason Wang
Quoted 90 lines · Stefan Hajnoczi
Quoted 79 lines · Jason Wang
Quoted 68 lines · Stefan Hajnoczi
Quoted 66 lines · Jason Wang
Quoted 63 lines · Stefan Hajnoczi

On Wed, 28 Jun 2023 at 09:59, Jason Wang <[email protected]> wrote:

On Wed, Jun 28, 2023 at 3:46 PM Stefan Hajnoczi <[email protected]> wrote:

On Wed, 28 Jun 2023 at 05:28, Jason Wang <[email protected]> wrote:

On Wed, Jun 28, 2023 at 6:45 AM Ilya Maximets <[email protected]> wrote:

On 6/27/23 04:54, Jason Wang wrote:
On Mon, Jun 26, 2023 at 9:17 PM Ilya Maximets <[email protected]> wrote:

On 6/26/23 08:32, Jason Wang wrote:
On Sun, Jun 25, 2023 at 3:06 PM Jason Wang <[email protected]> wrote:

On Fri, Jun 23, 2023 at 5:58 AM Ilya Maximets <[email protected]> wrote:
It is noticeably more performant than a tap with vhost=on in terms of PPS.
So, that might be one case. Taking into account that just rcu lock and
unlock in virtio-net code takes more time than a packet copy, some batching
on QEMU side should improve performance significantly. And it shouldn't be
too hard to implement.

Performance over virtual interfaces may potentially be improved by creating
a kernel thread for async Tx. Similarly to what io_uring allows. Currently
Tx on non-zero-copy interfaces is synchronous, and that doesn't allow to
scale well.

Interestingly, actually, there are a lot of "duplication" between
io_uring and AF_XDP:

1) both have similar memory model (user register)
2) both use ring for communication

I wonder if we can let io_uring talks directly to AF_XDP.

Well, if we submit poll() in QEMU main loop via io_uring, then we can
avoid cost of the synchronous Tx for non-zero-copy modes, i.e. for
virtual interfaces. io_uring thread in the kernel will be able to
perform transmission for us.

It would be nice if we can use iothread/vhost other than the main loop
even if io_uring can use kthreads. We can avoid the memory translation
cost.

The QEMU event loop (AioContext) has io_uring code
(utils/fdmon-io_uring.c) but it's disabled at the moment. I'm working
on patches to re-enable it and will probably send them in July. The
patches also add an API to submit arbitrary io_uring operations so
that you can do stuff besides file descriptor monitoring. Both the
main loop and IOThreads will be able to use io_uring on Linux hosts.

Just to make sure I understand. If we still need a copy from guest to
io_uring buffer, we still need to go via memory API for GPA which
seems expensive.

Vhost seems to be a shortcut for this.

I'm not sure how exactly you're thinking of using io_uring.

Simply using io_uring for the event loop (file descriptor monitoring)
doesn't involve an extra buffer, but the packet payload still needs to
reside in AF_XDP umem, so there is a copy between guest memory and
umem.

Quoted 8 lines
Quoted 3 lines

So there would be a translation from GPA to HVA (unless io_uring support 2 stages) which needs to go via qemu memory core. And this part seems to be very expensive according to my test in the past.

Yes, but in the current approach where AF_XDP is implemented as a QEMU netdev, there is already QEMU device emulation (e.g. virtio-net) happening. So the GPA to HVA translation will happen anyway in device emulation.

Quoted 7 lines

Just to make sure we're on the same page.

I meant, AF_XDP can do more than e.g 10Mpps. So if we still use the QEMU netdev, it would be very hard to achieve that if we stick to using the Qemu memory core translations which need to take care about too much extra stuff. That's why I suggest using vhost in io threads which only cares about ram so the translation could be very fast.

What does using "vhost in io threads" mean?

Quoted 7 lines
Quoted 1 line

It means a vhost userspace dataplane that is implemented via io threads.

AFAIK this does not exist today. QEMU's built-in devices that use IOThreads don't use vhost code. QEMU vhost code is for vhost kernel, vhost-user, or vDPA but not built-in devices that use IOThreads. The built-in devices implement VirtioDeviceClass callbacks directly and use AioContext APIs to run in IOThreads.

Yes.

Quoted 4 lines

Do you have an idea for using vhost code for built-in devices? Maybe it's fastest if you explain your idea and its advantages instead of me guessing.

It's something like I'd proposed in [1]:

1) a vhost that is implemented via IOThreads
2) memory translation is done via vhost memory table/IOTLB

The advantages are:

1) No 3rd application like DPDK application
2) Attack surface were reduced
3) Better understanding/interactions with device model for things like RSS and IOMMU

Quoted 5 lines
Quoted 1 line

There could be some dis-advantages but it's not obvious to me :)

Why is QEMU's native device emulation API not the natural choice for writing built-in devices? I don't understand why the vhost interface is desirable for built-in devices.

Unless the memory helpers (like address translations) were optimized fully to satisfy this 10M+ PPS.

Not sure if this is too hard, but last time I benchmark, perf told me most of the time spent in the translation.

Quoted 7 lines
Quoted 2 lines

Using a vhost is a workaround since its memory model is much more simpler so it can skip lots of memory sections like I/O and ROM etc.

So, we can have a thread running as part of QEMU process that implements vhost functionality for a virtio-net device. And this thread has an optimized way to access memory. What prevents current virtio-net emulation code accessing memory in the same optimized way?

Quoted 4 lines

Current emulation using memory core accessors which needs to take care of a lot of stuff like MMIO or even P2P. Such kind of stuff is not considered since day0 of vhost. You can do some experiment on this e.g just dropping packets after fetching it from the TX ring.

Quoted 5 lines

If I'm reading that right, virtio implementation is using address space caching by utilizing a memory listener and pre-translated addresses of interesting memory regions. Then it's performing address_space_read_cached, which is bypassing all the memory address translation logic on a cache hit. That sounds pretty similar to how memory table is prepared for vhost.

Exactly, but only for the vring memory structures (avail, used, and descriptor rings in the Split Virtqueue Layout).

The packet headers and payloads are still translated using the uncached virtqueue_pop() -> dma_memory_map() -> address_space_map() API.

Running a tx packet drop benchmark as Jason suggested and checking if memory translation is a bottleneck seems worthwhile. Improving dma_memory_map() performance would speed up all built-in QEMU devices.

Jason: When you noticed this bottleneck, were you using a normal virtio-net-pci device without vIOMMU?

Stefan

#23 lore raw
Quoted 159 lines · Stefan Hajnoczi
Quoted 157 lines · Ilya Maximets
Quoted 154 lines · Jason Wang
Quoted 141 lines · Ilya Maximets
Quoted 139 lines · Jason Wang
Quoted 126 lines · Stefan Hajnoczi
Quoted 124 lines · Jason Wang
Quoted 95 lines · Stefan Hajnoczi
Quoted 93 lines · Jason Wang
Quoted 90 lines · Stefan Hajnoczi
Quoted 79 lines · Jason Wang
Quoted 68 lines · Stefan Hajnoczi
Quoted 66 lines · Jason Wang

On Wed, Jun 28, 2023 at 4:15 PM Stefan Hajnoczi <[email protected]> wrote:

On Wed, 28 Jun 2023 at 09:59, Jason Wang <[email protected]> wrote:

On Wed, Jun 28, 2023 at 3:46 PM Stefan Hajnoczi <[email protected]> wrote:

On Wed, 28 Jun 2023 at 05:28, Jason Wang <[email protected]> wrote:

On Wed, Jun 28, 2023 at 6:45 AM Ilya Maximets <[email protected]> wrote:

On 6/27/23 04:54, Jason Wang wrote:
On Mon, Jun 26, 2023 at 9:17 PM Ilya Maximets <[email protected]> wrote:

On 6/26/23 08:32, Jason Wang wrote:
On Sun, Jun 25, 2023 at 3:06 PM Jason Wang <[email protected]> wrote:

On Fri, Jun 23, 2023 at 5:58 AM Ilya Maximets <[email protected]> wrote:
It is noticeably more performant than a tap with vhost=on in terms of PPS.
So, that might be one case. Taking into account that just rcu lock and
unlock in virtio-net code takes more time than a packet copy, some batching
on QEMU side should improve performance significantly. And it shouldn't be
too hard to implement.

Performance over virtual interfaces may potentially be improved by creating
a kernel thread for async Tx. Similarly to what io_uring allows. Currently
Tx on non-zero-copy interfaces is synchronous, and that doesn't allow to
scale well.

Interestingly, actually, there are a lot of "duplication" between
io_uring and AF_XDP:

1) both have similar memory model (user register)
2) both use ring for communication

I wonder if we can let io_uring talks directly to AF_XDP.

Well, if we submit poll() in QEMU main loop via io_uring, then we can
avoid cost of the synchronous Tx for non-zero-copy modes, i.e. for
virtual interfaces. io_uring thread in the kernel will be able to
perform transmission for us.

It would be nice if we can use iothread/vhost other than the main loop
even if io_uring can use kthreads. We can avoid the memory translation
cost.

The QEMU event loop (AioContext) has io_uring code
(utils/fdmon-io_uring.c) but it's disabled at the moment. I'm working
on patches to re-enable it and will probably send them in July. The
patches also add an API to submit arbitrary io_uring operations so
that you can do stuff besides file descriptor monitoring. Both the
main loop and IOThreads will be able to use io_uring on Linux hosts.

Just to make sure I understand. If we still need a copy from guest to
io_uring buffer, we still need to go via memory API for GPA which
seems expensive.

Vhost seems to be a shortcut for this.

I'm not sure how exactly you're thinking of using io_uring.

Simply using io_uring for the event loop (file descriptor monitoring)
doesn't involve an extra buffer, but the packet payload still needs to
reside in AF_XDP umem, so there is a copy between guest memory and
umem.

Quoted 8 lines
Quoted 3 lines

So there would be a translation from GPA to HVA (unless io_uring
support 2 stages) which needs to go via qemu memory core. And this
part seems to be very expensive according to my test in the past.

Yes, but in the current approach where AF_XDP is implemented as a QEMU netdev, there is already QEMU device emulation (e.g. virtio-net) happening. So the GPA to HVA translation will happen anyway in device emulation.

Quoted 7 lines

Just to make sure we're on the same page.

I meant, AF_XDP can do more than e.g 10Mpps. So if we still use the QEMU netdev, it would be very hard to achieve that if we stick to using the Qemu memory core translations which need to take care about too much extra stuff. That's why I suggest using vhost in io threads which only cares about ram so the translation could be very fast.

What does using "vhost in io threads" mean?

Quoted 7 lines
Quoted 1 line

It means a vhost userspace dataplane that is implemented via io threads.

AFAIK this does not exist today. QEMU's built-in devices that use IOThreads don't use vhost code. QEMU vhost code is for vhost kernel, vhost-user, or vDPA but not built-in devices that use IOThreads. The built-in devices implement VirtioDeviceClass callbacks directly and use AioContext APIs to run in IOThreads.

Yes.

Quoted 4 lines

Do you have an idea for using vhost code for built-in devices? Maybe it's fastest if you explain your idea and its advantages instead of me guessing.

It's something like I'd proposed in [1]:

1) a vhost that is implemented via IOThreads
2) memory translation is done via vhost memory table/IOTLB

The advantages are:

1) No 3rd application like DPDK application
2) Attack surface were reduced
3) Better understanding/interactions with device model for things like RSS and IOMMU

Quoted 5 lines
Quoted 1 line

There could be some dis-advantages but it's not obvious to me :)

Why is QEMU's native device emulation API not the natural choice for writing built-in devices? I don't understand why the vhost interface is desirable for built-in devices.

Unless the memory helpers (like address translations) were optimized fully to satisfy this 10M+ PPS.

Not sure if this is too hard, but last time I benchmark, perf told me most of the time spent in the translation.

Quoted 7 lines
Quoted 2 lines

Using a vhost is a workaround since its memory model is much more simpler so it can skip lots of memory sections like I/O and ROM etc.

So, we can have a thread running as part of QEMU process that implements vhost functionality for a virtio-net device. And this thread has an optimized way to access memory. What prevents current virtio-net emulation code accessing memory in the same optimized way?

Current emulation using memory core accessors which needs to take care of a lot of stuff like MMIO or even P2P. Such kind of stuff is not considered since day0 of vhost. You can do some experiment on this e.g just dropping packets after fetching it from the TX ring.

Quoted 8 lines
Quoted 5 lines

If I'm reading that right, virtio implementation is using address space caching by utilizing a memory listener and pre-translated addresses of interesting memory regions. Then it's performing address_space_read_cached, which is bypassing all the memory address translation logic on a cache hit. That sounds pretty similar to how memory table is prepared for vhost.

Exactly, but only for the vring memory structures (avail, used, and descriptor rings in the Split Virtqueue Layout).

Yes. It should speed up somehow.

Quoted 8 lines

The packet headers and payloads are still translated using the uncached virtqueue_pop() -> dma_memory_map() -> address_space_map() API.

Running a tx packet drop benchmark as Jason suggested and checking if memory translation is a bottleneck seems worthwhile. Improving dma_memory_map() performance would speed up all built-in QEMU devices.

+1

Quoted 3 lines

Jason: When you noticed this bottleneck, were you using a normal virtio-net-pci device without vIOMMU?

Normal virtio-net-pci device without vIOMMU.

Thanks

Quoted 3 lines

Stefan

#24 lore raw
In reply to Ilya Maximets
Quoted 157 lines · Ilya Maximets
Quoted 150 lines · Jason Wang
Quoted 141 lines · Ilya Maximets
Quoted 139 lines · Jason Wang
Quoted 126 lines · Stefan Hajnoczi
Quoted 124 lines · Jason Wang
Quoted 95 lines · Stefan Hajnoczi
Quoted 93 lines · Jason Wang
Quoted 90 lines · Stefan Hajnoczi
Quoted 79 lines · Jason Wang
Quoted 68 lines · Stefan Hajnoczi
Quoted 66 lines · Jason Wang
Quoted 63 lines · Stefan Hajnoczi

On Wed, 28 Jun 2023 at 09:59, Jason Wang <[email protected]> wrote:

On Wed, Jun 28, 2023 at 3:46 PM Stefan Hajnoczi <[email protected]> wrote:

On Wed, 28 Jun 2023 at 05:28, Jason Wang <[email protected]> wrote:

On Wed, Jun 28, 2023 at 6:45 AM Ilya Maximets <[email protected]> wrote:

On 6/27/23 04:54, Jason Wang wrote:
On Mon, Jun 26, 2023 at 9:17 PM Ilya Maximets <[email protected]> wrote:

On 6/26/23 08:32, Jason Wang wrote:
On Sun, Jun 25, 2023 at 3:06 PM Jason Wang <[email protected]> wrote:

On Fri, Jun 23, 2023 at 5:58 AM Ilya Maximets <[email protected]> wrote:
It is noticeably more performant than a tap with vhost=on in terms of PPS.
So, that might be one case. Taking into account that just rcu lock and
unlock in virtio-net code takes more time than a packet copy, some batching
on QEMU side should improve performance significantly. And it shouldn't be
too hard to implement.

Performance over virtual interfaces may potentially be improved by creating
a kernel thread for async Tx. Similarly to what io_uring allows. Currently
Tx on non-zero-copy interfaces is synchronous, and that doesn't allow to
scale well.

Interestingly, actually, there are a lot of "duplication" between
io_uring and AF_XDP:

1) both have similar memory model (user register)
2) both use ring for communication

I wonder if we can let io_uring talks directly to AF_XDP.

Well, if we submit poll() in QEMU main loop via io_uring, then we can
avoid cost of the synchronous Tx for non-zero-copy modes, i.e. for
virtual interfaces. io_uring thread in the kernel will be able to
perform transmission for us.

It would be nice if we can use iothread/vhost other than the main loop
even if io_uring can use kthreads. We can avoid the memory translation
cost.

The QEMU event loop (AioContext) has io_uring code
(utils/fdmon-io_uring.c) but it's disabled at the moment. I'm working
on patches to re-enable it and will probably send them in July. The
patches also add an API to submit arbitrary io_uring operations so
that you can do stuff besides file descriptor monitoring. Both the
main loop and IOThreads will be able to use io_uring on Linux hosts.

Just to make sure I understand. If we still need a copy from guest to
io_uring buffer, we still need to go via memory API for GPA which
seems expensive.

Vhost seems to be a shortcut for this.

I'm not sure how exactly you're thinking of using io_uring.

Simply using io_uring for the event loop (file descriptor monitoring)
doesn't involve an extra buffer, but the packet payload still needs to
reside in AF_XDP umem, so there is a copy between guest memory and
umem.

Quoted 8 lines
Quoted 3 lines

So there would be a translation from GPA to HVA (unless io_uring support 2 stages) which needs to go via qemu memory core. And this part seems to be very expensive according to my test in the past.

Yes, but in the current approach where AF_XDP is implemented as a QEMU netdev, there is already QEMU device emulation (e.g. virtio-net) happening. So the GPA to HVA translation will happen anyway in device emulation.

Quoted 7 lines

Just to make sure we're on the same page.

I meant, AF_XDP can do more than e.g 10Mpps. So if we still use the QEMU netdev, it would be very hard to achieve that if we stick to using the Qemu memory core translations which need to take care about too much extra stuff. That's why I suggest using vhost in io threads which only cares about ram so the translation could be very fast.

What does using "vhost in io threads" mean?

Quoted 7 lines
Quoted 1 line

It means a vhost userspace dataplane that is implemented via io threads.

AFAIK this does not exist today. QEMU's built-in devices that use IOThreads don't use vhost code. QEMU vhost code is for vhost kernel, vhost-user, or vDPA but not built-in devices that use IOThreads. The built-in devices implement VirtioDeviceClass callbacks directly and use AioContext APIs to run in IOThreads.

Yes.

Quoted 4 lines

Do you have an idea for using vhost code for built-in devices? Maybe it's fastest if you explain your idea and its advantages instead of me guessing.

It's something like I'd proposed in [1]:

1) a vhost that is implemented via IOThreads
2) memory translation is done via vhost memory table/IOTLB

The advantages are:

1) No 3rd application like DPDK application
2) Attack surface were reduced
3) Better understanding/interactions with device model for things like RSS and IOMMU

Quoted 5 lines
Quoted 1 line

There could be some dis-advantages but it's not obvious to me :)

Why is QEMU's native device emulation API not the natural choice for writing built-in devices? I don't understand why the vhost interface is desirable for built-in devices.

Unless the memory helpers (like address translations) were optimized fully to satisfy this 10M+ PPS.

Not sure if this is too hard, but last time I benchmark, perf told me most of the time spent in the translation.

Quoted 7 lines
Quoted 2 lines

Using a vhost is a workaround since its memory model is much more simpler so it can skip lots of memory sections like I/O and ROM etc.

So, we can have a thread running as part of QEMU process that implements vhost functionality for a virtio-net device. And this thread has an optimized way to access memory. What prevents current virtio-net emulation code accessing memory in the same optimized way?

Quoted 4 lines

Current emulation using memory core accessors which needs to take care of a lot of stuff like MMIO or even P2P. Such kind of stuff is not considered since day0 of vhost. You can do some experiment on this e.g just dropping packets after fetching it from the TX ring.

Quoted 5 lines

If I'm reading that right, virtio implementation is using address space caching by utilizing a memory listener and pre-translated addresses of interesting memory regions. Then it's performing address_space_read_cached, which is bypassing all the memory address translation logic on a cache hit. That sounds pretty similar to how memory table is prepared for vhost.

It's only done for virtqueue metadata (desc, driver and device area), we still need to do dma map for the packet buffer itself.

Thanks

Quoted 99 lines
Quoted 98 lines
Quoted 6 lines

i.e. we likely don't
actually need to implement the whole vhost-virtio communication protocol in order to have faster memory access from the device emulation code. I mean, if vhost can access device memory faster, why device itself can't?

I'm not saying it can't but it would end up with something similar to vhost. And that's why I'm saying using vhost is a shortcut (at least for a POC).

Thanks

Quoted 84 lines

With that we could probably split the "datapath" part of the virtio-net emulation into a separate thread driven by iothread loop.

Then add batch API for communication with a network backend (af-xdp) to avoid per-packet calls.

These are 3 more or less independent tasks that should allow the similar performance to a full fledged vhost control and dataplane implementation inside QEMU.

Or am I missing something? (Probably)

Quoted 70 lines

Thanks

Quoted 66 lines
Quoted 3 lines

It's something like linking SPDK/DPDK to Qemu.

Sergio Lopez tried loading vhost-user devices as shared libraries that run in the QEMU process. It worked as an experiment but wasn't pursued further.

I think that might make sense in specific cases where there is an existing vhost-user codebase that needs to run as part of QEMU.

In this case the AF_XDP code is new, so it's not a case of moving existing code into QEMU.

Quoted 51 lines
Quoted 17 lines
Quoted 17 lines
Quoted 16 lines
Quoted 11 lines
Quoted 8 lines

Regarding pinning - I wonder if that's something that can be refined in the kernel by adding an AF_XDP flag that enables on-demand pinning of umem. That way only rx and tx buffers that are currently in use will be pinned. The disadvantage is the runtime overhead to pin/unpin pages. I'm not sure whether it's possible to implement this, I haven't checked the kernel code.

It requires the device to do page faults which is not commonly supported nowadays.

I don't understand this comment. AF_XDP processes each rx/tx descriptor. At that point it can getuserpages() or similar in order to pin the page. When the memory is no longer needed, it can put those pages. No fault mechanism is needed. What am I missing?

Quoted 6 lines
Quoted 3 lines

Ok, I think I kind of get you, you mean doing pinning while processing rx/tx buffers? It's not easy since GUP itself is not very fast, it may hit PPS for sure.

Yes. It's not as fast as permanently pinning rx/tx buffers, but it supports unpinned guest RAM.

Right, it's a balance between pin and PPS. PPS seems to be more important in this case.

Quoted 8 lines

There are variations on this approach, like keeping a certain amount of pages pinned after they have been used so the cost of pinning/unpinning can be avoided when the same pages are reused in the future, but I don't know how effective that is in practice.

Is there a more efficient approach without relying on hardware page fault support?

I guess so, I see some slides that say device page fault is very slow.

Quoted 4 lines

My understanding is that hardware page fault support is not yet deployed. We'd be left with pinning guest RAM permanently or using a runtime pinning/unpinning approach like I've described.

Probably.

Thanks

Quoted 3 lines

Stefan

#25 lore raw
In reply to Jason Wang
Quoted 134 lines · Jason Wang
Quoted 126 lines · Stefan Hajnoczi
Quoted 124 lines · Jason Wang
Quoted 95 lines · Stefan Hajnoczi
Quoted 93 lines · Jason Wang
Quoted 90 lines · Stefan Hajnoczi
Quoted 79 lines · Jason Wang
Quoted 68 lines · Stefan Hajnoczi
Quoted 66 lines · Jason Wang
Quoted 63 lines · Stefan Hajnoczi
Quoted 49 lines · Jason Wang
Quoted 40 lines · Stefan Hajnoczi
Quoted 37 lines · Jason Wang

On Wed, Jun 28, 2023 at 6:45 AM Ilya Maximets <[email protected]> wrote:

On 6/27/23 04:54, Jason Wang wrote:
On Mon, Jun 26, 2023 at 9:17 PM Ilya Maximets <[email protected]> wrote:

On 6/26/23 08:32, Jason Wang wrote:
On Sun, Jun 25, 2023 at 3:06 PM Jason Wang <[email protected]> wrote:

On Fri, Jun 23, 2023 at 5:58 AM Ilya Maximets <[email protected]> wrote:
It is noticeably more performant than a tap with vhost=on in terms of PPS.
So, that might be one case. Taking into account that just rcu lock and
unlock in virtio-net code takes more time than a packet copy, some batching
on QEMU side should improve performance significantly. And it shouldn't be
too hard to implement.

Performance over virtual interfaces may potentially be improved by creating
a kernel thread for async Tx. Similarly to what io_uring allows. Currently
Tx on non-zero-copy interfaces is synchronous, and that doesn't allow to
scale well.

Interestingly, actually, there are a lot of "duplication" between
io_uring and AF_XDP:

1) both have similar memory model (user register)
2) both use ring for communication

I wonder if we can let io_uring talks directly to AF_XDP.

Well, if we submit poll() in QEMU main loop via io_uring, then we can
avoid cost of the synchronous Tx for non-zero-copy modes, i.e. for
virtual interfaces. io_uring thread in the kernel will be able to
perform transmission for us.

It would be nice if we can use iothread/vhost other than the main loop
even if io_uring can use kthreads. We can avoid the memory translation
cost.

Quoted 6 lines

The QEMU event loop (AioContext) has io_uring code (utils/fdmon-io_uring.c) but it's disabled at the moment. I'm working on patches to re-enable it and will probably send them in July. The patches also add an API to submit arbitrary io_uring operations so that you can do stuff besides file descriptor monitoring. Both the main loop and IOThreads will be able to use io_uring on Linux hosts.

Quoted 5 lines

Just to make sure I understand. If we still need a copy from guest to io_uring buffer, we still need to go via memory API for GPA which seems expensive.

Vhost seems to be a shortcut for this.

I'm not sure how exactly you're thinking of using io_uring.

Simply using io_uring for the event loop (file descriptor monitoring) doesn't involve an extra buffer, but the packet payload still needs to reside in AF_XDP umem, so there is a copy between guest memory and umem.

Quoted 8 lines
Quoted 3 lines

So there would be a translation from GPA to HVA (unless io_uring support 2 stages) which needs to go via qemu memory core. And this part seems to be very expensive according to my test in the past.

Yes, but in the current approach where AF_XDP is implemented as a QEMU netdev, there is already QEMU device emulation (e.g. virtio-net) happening. So the GPA to HVA translation will happen anyway in device emulation.

Quoted 7 lines

Just to make sure we're on the same page.

I meant, AF_XDP can do more than e.g 10Mpps. So if we still use the QEMU netdev, it would be very hard to achieve that if we stick to using the Qemu memory core translations which need to take care about too much extra stuff. That's why I suggest using vhost in io threads which only cares about ram so the translation could be very fast.

What does using "vhost in io threads" mean?

Quoted 7 lines
Quoted 1 line

It means a vhost userspace dataplane that is implemented via io threads.

AFAIK this does not exist today. QEMU's built-in devices that use IOThreads don't use vhost code. QEMU vhost code is for vhost kernel, vhost-user, or vDPA but not built-in devices that use IOThreads. The built-in devices implement VirtioDeviceClass callbacks directly and use AioContext APIs to run in IOThreads.

Yes.

Quoted 4 lines

Do you have an idea for using vhost code for built-in devices? Maybe it's fastest if you explain your idea and its advantages instead of me guessing.

It's something like I'd proposed in [1]:

1) a vhost that is implemented via IOThreads
2) memory translation is done via vhost memory table/IOTLB

The advantages are:

1) No 3rd application like DPDK application
2) Attack surface were reduced
3) Better understanding/interactions with device model for things like RSS and IOMMU

Quoted 5 lines
Quoted 1 line

There could be some dis-advantages but it's not obvious to me :)

Why is QEMU's native device emulation API not the natural choice for writing built-in devices? I don't understand why the vhost interface is desirable for built-in devices.

Quoted 8 lines

Unless the memory helpers (like address translations) were optimized fully to satisfy this 10M+ PPS.

Not sure if this is too hard, but last time I benchmark, perf told me most of the time spent in the translation.

Using a vhost is a workaround since its memory model is much more simpler so it can skip lots of memory sections like I/O and ROM etc.

I see, that sounds like a question of optimization. Most DMA transfers will be to/from guest RAM and it seems like QEMU's memory API could be optimized for that case. PIO/MMIO dispatch could use a different API from DMA transfers, if necessary.

I don't think there is a fundamental reason why QEMU's own device emulation code cannot translate memory as fast as vhost devices can.

Stefan

#26 lore raw
Quoted 142 lines · Stefan Hajnoczi
Quoted 140 lines · Jason Wang
Quoted 126 lines · Stefan Hajnoczi
Quoted 124 lines · Jason Wang
Quoted 95 lines · Stefan Hajnoczi
Quoted 93 lines · Jason Wang
Quoted 90 lines · Stefan Hajnoczi
Quoted 79 lines · Jason Wang
Quoted 68 lines · Stefan Hajnoczi
Quoted 66 lines · Jason Wang
Quoted 63 lines · Stefan Hajnoczi
Quoted 49 lines · Jason Wang
Quoted 46 lines · Stefan Hajnoczi

On Wed, 28 Jun 2023 at 05:28, Jason Wang <[email protected]> wrote:

On Wed, Jun 28, 2023 at 6:45 AM Ilya Maximets <[email protected]> wrote:

On 6/27/23 04:54, Jason Wang wrote:
On Mon, Jun 26, 2023 at 9:17 PM Ilya Maximets <[email protected]> wrote:

On 6/26/23 08:32, Jason Wang wrote:
On Sun, Jun 25, 2023 at 3:06 PM Jason Wang <[email protected]> wrote:

On Fri, Jun 23, 2023 at 5:58 AM Ilya Maximets <[email protected]> wrote:
It is noticeably more performant than a tap with vhost=on in terms of PPS.
So, that might be one case. Taking into account that just rcu lock and
unlock in virtio-net code takes more time than a packet copy, some batching
on QEMU side should improve performance significantly. And it shouldn't be
too hard to implement.

Performance over virtual interfaces may potentially be improved by creating
a kernel thread for async Tx. Similarly to what io_uring allows. Currently
Tx on non-zero-copy interfaces is synchronous, and that doesn't allow to
scale well.

Interestingly, actually, there are a lot of "duplication" between
io_uring and AF_XDP:

1) both have similar memory model (user register)
2) both use ring for communication

I wonder if we can let io_uring talks directly to AF_XDP.

Well, if we submit poll() in QEMU main loop via io_uring, then we can
avoid cost of the synchronous Tx for non-zero-copy modes, i.e. for
virtual interfaces. io_uring thread in the kernel will be able to
perform transmission for us.

It would be nice if we can use iothread/vhost other than the main loop
even if io_uring can use kthreads. We can avoid the memory translation
cost.

The QEMU event loop (AioContext) has io_uring code
(utils/fdmon-io_uring.c) but it's disabled at the moment. I'm working
on patches to re-enable it and will probably send them in July. The
patches also add an API to submit arbitrary io_uring operations so
that you can do stuff besides file descriptor monitoring. Both the
main loop and IOThreads will be able to use io_uring on Linux hosts.

Quoted 5 lines

Just to make sure I understand. If we still need a copy from guest to io_uring buffer, we still need to go via memory API for GPA which seems expensive.

Vhost seems to be a shortcut for this.

I'm not sure how exactly you're thinking of using io_uring.

Simply using io_uring for the event loop (file descriptor monitoring) doesn't involve an extra buffer, but the packet payload still needs to reside in AF_XDP umem, so there is a copy between guest memory and umem.

Quoted 8 lines
Quoted 3 lines

So there would be a translation from GPA to HVA (unless io_uring support 2 stages) which needs to go via qemu memory core. And this part seems to be very expensive according to my test in the past.

Yes, but in the current approach where AF_XDP is implemented as a QEMU netdev, there is already QEMU device emulation (e.g. virtio-net) happening. So the GPA to HVA translation will happen anyway in device emulation.

Quoted 7 lines

Just to make sure we're on the same page.

I meant, AF_XDP can do more than e.g 10Mpps. So if we still use the QEMU netdev, it would be very hard to achieve that if we stick to using the Qemu memory core translations which need to take care about too much extra stuff. That's why I suggest using vhost in io threads which only cares about ram so the translation could be very fast.

What does using "vhost in io threads" mean?

Quoted 7 lines
Quoted 1 line

It means a vhost userspace dataplane that is implemented via io threads.

AFAIK this does not exist today. QEMU's built-in devices that use IOThreads don't use vhost code. QEMU vhost code is for vhost kernel, vhost-user, or vDPA but not built-in devices that use IOThreads. The built-in devices implement VirtioDeviceClass callbacks directly and use AioContext APIs to run in IOThreads.

Yes.

Quoted 4 lines

Do you have an idea for using vhost code for built-in devices? Maybe it's fastest if you explain your idea and its advantages instead of me guessing.

It's something like I'd proposed in [1]:

1) a vhost that is implemented via IOThreads
2) memory translation is done via vhost memory table/IOTLB

The advantages are:

1) No 3rd application like DPDK application
2) Attack surface were reduced
3) Better understanding/interactions with device model for things like RSS and IOMMU

Quoted 5 lines
Quoted 1 line

There could be some dis-advantages but it's not obvious to me :)

Why is QEMU's native device emulation API not the natural choice for writing built-in devices? I don't understand why the vhost interface is desirable for built-in devices.

Unless the memory helpers (like address translations) were optimized fully to satisfy this 10M+ PPS.

Not sure if this is too hard, but last time I benchmark, perf told me most of the time spent in the translation.

Quoted 7 lines
Quoted 2 lines

Using a vhost is a workaround since its memory model is much more simpler so it can skip lots of memory sections like I/O and ROM etc.

I see, that sounds like a question of optimization. Most DMA transfers will be to/from guest RAM and it seems like QEMU's memory API could be optimized for that case. PIO/MMIO dispatch could use a different API from DMA transfers, if necessary.

Probably.

Quoted 3 lines

I don't think there is a fundamental reason why QEMU's own device emulation code cannot translate memory as fast as vhost devices can.

Yes, it can do what vhost can do. Starting from a vhost may help us to know where we could go for the optimization of the memory core.

Thanks

Quoted 3 lines

Stefan

#27 lore raw
In reply to Jason Wang
Quoted 114 lines · Jason Wang
Quoted 113 lines · Ilya Maximets
Quoted 88 lines · Jason Wang
Quoted 78 lines · Ilya Maximets
Quoted 60 lines · Jason Wang
Quoted 59 lines · Jason Wang
Quoted 57 lines · Ilya Maximets

AF_XDP is a network socket family that allows communication directly with the network device driver in the kernel, bypassing most or all of the kernel networking stack. In the essence, the technology is pretty similar to netmap. But, unlike netmap, AF_XDP is Linux-native and works with any network interfaces without driver modifications. Unlike vhost-based backends (kernel, user, vdpa), AF_XDP doesn't require access to character devices or unix sockets. Only access to the network interface itself is necessary.

This patch implements a network backend that communicates with the kernel by creating an AF_XDP socket. A chunk of userspace memory is shared between QEMU and the host kernel. 4 ring buffers (Tx, Rx, Fill and Completion) are placed in that memory along with a pool of memory buffers for the packet data. Data transmission is done by allocating one of the buffers, copying packet data into it and placing the pointer into Tx ring. After transmission, device will return the buffer via Completion ring. On Rx, device will take a buffer form a pre-populated Fill ring, write the packet data into it and place the buffer into Rx ring.

AF_XDP network backend takes on the communication with the host kernel and the network interface and forwards packets to/from the peer device in QEMU.

Usage example:

-device virtio-net-pci,netdev=guest1,mac=00:16:35:AF:AA:5C -netdev af-xdp,ifname=ens6f1np1,id=guest1,mode=native,queues=1

XDP program bridges the socket with a network interface. It can be attached to the interface in 2 different modes:

1. skb - this mode should work for any interface and doesn't require driver support. With a caveat of lower performance.

2. native - this does require support from the driver and allows to bypass skb allocation in the kernel and potentially use zero-copy while getting packets in/out userspace.

By default, QEMU will try to use native mode and fall back to skb. Mode can be forced via 'mode' option. To force 'copy' even in native mode, use 'force-copy=on' option. This might be useful if there is some issue with the driver.

Option 'queues=N' allows to specify how many device queues should be open. Note that all the queues that are not open are still functional and can receive traffic, but it will not be delivered to QEMU. So, the number of device queues should generally match the QEMU configuration, unless the device is shared with something else and the traffic re-direction to appropriate queues is correctly configured on a device level (e.g. with ethtool -N). 'start-queue=M' option can be used to specify from which queue id QEMU should start configuring 'N' queues. It might also be necessary to use this option with certain NICs, e.g. MLX5 NICs. See the docs for examples.

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

In a general case QEMU will need CAP_NET_ADMIN and CAP_SYS_ADMIN capabilities in order to load default XSK/XDP programs to the network interface and configure BTF maps.

I think you mean "BPF" actually?

"BPF Type Format maps" kind of makes some sense, but yes. :)

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

It is possible, however,
to run only with CAP_NET_RAW.

Qemu often runs without any privileges, so we need to fix it.

I think adding support for SCM_RIGHTS via monitor would be a way to go.

Quoted 8 lines

I looked through the code and it seems like we can run completely non-privileged as far as kernel concerned. We'll need an API modification in libxdp though.

The thing is, IIUC, the only syscall that requires CAP_NET_RAW is a base socket creation. Binding and other configuration doesn't require any privileges. So, we could create a socket externally and pass it to QEMU.

Quoted 8 lines

That's the way TAP works for example.

Quoted 4 lines

Should work, unless it's an oversight from
the kernel side that needs to be patched. :) libxdp doesn't have a way to specify externally created socket today, so we'll need to change that. Should be easy to do though. I can explore.

Please do that.

I have a prototype:
https://github.com/igsilya/xdp-tools/commit/db73e90945e3aa5e451ac88c42c83cb9389642d3

Need to test it out and then submit PR to xdp-tools project.

Quoted 9 lines
Quoted 8 lines

In case the bind syscall will actually need CAP_NET_RAW for some reason, we could change the kernel and allow non-privileged bind by utilizing, e.g. SO_BINDTODEVICE. i.e., let the privileged process bind the socket to a particular device, so QEMU can't bind it to a random one. Might be a good use case to allow even if not strictly necessary.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Yes.

Will propose something for a kernel as well. We might want something more granular though, e.g. bind to a queue instead of a device. In case we want better control in the device sharing scenario.

I may miss something but the bind is already done at dev plus queue right now, isn't it?

Yes, the bind() syscall will bind socket to the dev+queue. I was talking about SO_BINDTODEVICE that only ties the socket to a particular device, but not a queue.

Assuming SO_BINDTODEVICE is implemented for AF_XDP sockets and assuming a privileged process does:

  fd = socket(AF_XDP, ...);
  setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, <device>);

And sends fd to a non-privileged process. That non-privileged process will be able to call:

  bind(fd, <device>, <random queue>);

It will have to use the same device, but can choose any queue, if that queue is not already busy with another socket.

So, I was thinking maybe implementing something like XDP_BINDTOQID option. This way the privileged process may call:

  setsockopt(fd, SOL_XDP, XDP_BINDTOQID, <device>, <queue>);

And later kernel will be able to refuse bind() for any other queue for this particular socket.

Not sure if that is necessary though.
Since we're allocating the socket in the privileged process, that process may add the socket to the BPF map on the correct queue id. This way the non-privileged process will not be able to receive any packets from any other queue on this socket, even if bound to it. And no other AF_XDP socket will be able to be bound to that other queue as well. So, the rogue QEMU will be able to hog one extra queue, but it will not be able to intercept traffic any from it, AFAICT. May not be a huge problem after all.

SO_BINDTODEVICE would still be nice to have. Especially for cases where we give the whole device to one VM.

Best regards, Ilya Maximets.

#28 lore raw
Quoted 144 lines · Ilya Maximets
Quoted 114 lines · Jason Wang
Quoted 113 lines · Ilya Maximets
Quoted 88 lines · Jason Wang
Quoted 78 lines · Ilya Maximets
Quoted 60 lines · Jason Wang
Quoted 59 lines · Jason Wang
Quoted 57 lines · Ilya Maximets

AF_XDP is a network socket family that allows communication directly with the network device driver in the kernel, bypassing most or all of the kernel networking stack. In the essence, the technology is pretty similar to netmap. But, unlike netmap, AF_XDP is Linux-native and works with any network interfaces without driver modifications. Unlike vhost-based backends (kernel, user, vdpa), AF_XDP doesn't require access to character devices or unix sockets. Only access to the network interface itself is necessary.

This patch implements a network backend that communicates with the kernel by creating an AF_XDP socket. A chunk of userspace memory is shared between QEMU and the host kernel. 4 ring buffers (Tx, Rx, Fill and Completion) are placed in that memory along with a pool of memory buffers for the packet data. Data transmission is done by allocating one of the buffers, copying packet data into it and placing the pointer into Tx ring. After transmission, device will return the buffer via Completion ring. On Rx, device will take a buffer form a pre-populated Fill ring, write the packet data into it and place the buffer into Rx ring.

AF_XDP network backend takes on the communication with the host kernel and the network interface and forwards packets to/from the peer device in QEMU.

Usage example:

-device virtio-net-pci,netdev=guest1,mac=00:16:35:AF:AA:5C -netdev af-xdp,ifname=ens6f1np1,id=guest1,mode=native,queues=1

XDP program bridges the socket with a network interface. It can be attached to the interface in 2 different modes:

1. skb - this mode should work for any interface and doesn't require driver support. With a caveat of lower performance.

2. native - this does require support from the driver and allows to bypass skb allocation in the kernel and potentially use zero-copy while getting packets in/out userspace.

By default, QEMU will try to use native mode and fall back to skb. Mode can be forced via 'mode' option. To force 'copy' even in native mode, use 'force-copy=on' option. This might be useful if there is some issue with the driver.

Option 'queues=N' allows to specify how many device queues should be open. Note that all the queues that are not open are still functional and can receive traffic, but it will not be delivered to QEMU. So, the number of device queues should generally match the QEMU configuration, unless the device is shared with something else and the traffic re-direction to appropriate queues is correctly configured on a device level (e.g. with ethtool -N). 'start-queue=M' option can be used to specify from which queue id QEMU should start configuring 'N' queues. It might also be necessary to use this option with certain NICs, e.g. MLX5 NICs. See the docs for examples.

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

In a general case QEMU will need CAP_NET_ADMIN and CAP_SYS_ADMIN capabilities in order to load default XSK/XDP programs to the network interface and configure BTF maps.

I think you mean "BPF" actually?

"BPF Type Format maps" kind of makes some sense, but yes. :)

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

It is possible, however,
to run only with CAP_NET_RAW.

Qemu often runs without any privileges, so we need to fix it.

I think adding support for SCM_RIGHTS via monitor would be a way to go.

Quoted 8 lines

I looked through the code and it seems like we can run completely non-privileged as far as kernel concerned. We'll need an API modification in libxdp though.

The thing is, IIUC, the only syscall that requires CAP_NET_RAW is a base socket creation. Binding and other configuration doesn't require any privileges. So, we could create a socket externally and pass it to QEMU.

Quoted 8 lines

That's the way TAP works for example.

Quoted 4 lines

Should work, unless it's an oversight from
the kernel side that needs to be patched. :) libxdp doesn't have a way to specify externally created socket today, so we'll need to change that. Should be easy to do though. I can explore.

Please do that.

I have a prototype:
https://github.com/igsilya/xdp-tools/commit/db73e90945e3aa5e451ac88c42c83cb9389642d3

Need to test it out and then submit PR to xdp-tools project.

Quoted 9 lines
Quoted 8 lines

In case the bind syscall will actually need CAP_NET_RAW for some reason, we could change the kernel and allow non-privileged bind by utilizing, e.g. SO_BINDTODEVICE. i.e., let the privileged process bind the socket to a particular device, so QEMU can't bind it to a random one. Might be a good use case to allow even if not strictly necessary.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Yes.

Will propose something for a kernel as well. We might want something more granular though, e.g. bind to a queue instead of a device. In case we want better control in the device sharing scenario.

I may miss something but the bind is already done at dev plus queue right now, isn't it?

Yes, the bind() syscall will bind socket to the dev+queue. I was talking about SO_BINDTODEVICE that only ties the socket to a particular device, but not a queue.

Assuming SO_BINDTODEVICE is implemented for AF_XDP sockets and assuming a privileged process does:

  fd = socket(AF_XDP, ...);
  setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, <device>);

And sends fd to a non-privileged process. That non-privileged process will be able to call:

  bind(fd, <device>, <random queue>);

It will have to use the same device, but can choose any queue, if that queue is not already busy with another socket.

Quoted 7 lines

So, I was thinking maybe implementing something like XDP_BINDTOQID option. This way the privileged process may call:

  setsockopt(fd, SOL_XDP, XDP_BINDTOQID, <device>, <queue>);

And later kernel will be able to refuse bind() for any other queue for this particular socket.

Not sure, if file descriptor passing works, we probably don't need another way.

Quoted 7 lines

Not sure if that is necessary though.
Since we're allocating the socket in the privileged process, that process may add the socket to the BPF map on the correct queue id. This way the non-privileged process will not be able to receive any packets from any other queue on this socket, even if bound to it. And no other AF_XDP socket will be able to be bound to that other queue as well.

I think that's by design, or anything wrong with this model?

Quoted 7 lines

So, the
rogue QEMU will be able to hog one extra queue, but it will not be able to intercept traffic any from it, AFAICT. May not be a huge problem after all.

SO_BINDTODEVICE would still be nice to have. Especially for cases where we give the whole device to one VM.

Then we need to use AF_XDP in the guest which seems to be a different topic. Alibaba is working on the AF_XDP support for virtio-net.

Thanks

Quoted 3 lines

Best regards, Ilya Maximets.

#29 lore raw
Quoted 101 lines · Jason Wang
Quoted 100 lines · Ilya Maximets
Quoted 98 lines · Jason Wang
Quoted 97 lines · Ilya Maximets
Quoted 95 lines · Jason Wang
Quoted 78 lines · Ilya Maximets
Quoted 60 lines · Jason Wang
Quoted 59 lines · Jason Wang
Quoted 57 lines · Ilya Maximets

AF_XDP is a network socket family that allows communication directly with the network device driver in the kernel, bypassing most or all of the kernel networking stack. In the essence, the technology is pretty similar to netmap. But, unlike netmap, AF_XDP is Linux-native and works with any network interfaces without driver modifications. Unlike vhost-based backends (kernel, user, vdpa), AF_XDP doesn't require access to character devices or unix sockets. Only access to the network interface itself is necessary.

This patch implements a network backend that communicates with the kernel by creating an AF_XDP socket. A chunk of userspace memory is shared between QEMU and the host kernel. 4 ring buffers (Tx, Rx, Fill and Completion) are placed in that memory along with a pool of memory buffers for the packet data. Data transmission is done by allocating one of the buffers, copying packet data into it and placing the pointer into Tx ring. After transmission, device will return the buffer via Completion ring. On Rx, device will take a buffer form a pre-populated Fill ring, write the packet data into it and place the buffer into Rx ring.

AF_XDP network backend takes on the communication with the host kernel and the network interface and forwards packets to/from the peer device in QEMU.

Usage example:

-device virtio-net-pci,netdev=guest1,mac=00:16:35:AF:AA:5C -netdev af-xdp,ifname=ens6f1np1,id=guest1,mode=native,queues=1

XDP program bridges the socket with a network interface. It can be attached to the interface in 2 different modes:

1. skb - this mode should work for any interface and doesn't require driver support. With a caveat of lower performance.

2. native - this does require support from the driver and allows to bypass skb allocation in the kernel and potentially use zero-copy while getting packets in/out userspace.

By default, QEMU will try to use native mode and fall back to skb. Mode can be forced via 'mode' option. To force 'copy' even in native mode, use 'force-copy=on' option. This might be useful if there is some issue with the driver.

Option 'queues=N' allows to specify how many device queues should be open. Note that all the queues that are not open are still functional and can receive traffic, but it will not be delivered to QEMU. So, the number of device queues should generally match the QEMU configuration, unless the device is shared with something else and the traffic re-direction to appropriate queues is correctly configured on a device level (e.g. with ethtool -N). 'start-queue=M' option can be used to specify from which queue id QEMU should start configuring 'N' queues. It might also be necessary to use this option with certain NICs, e.g. MLX5 NICs. See the docs for examples.

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

In a general case QEMU will need CAP_NET_ADMIN and CAP_SYS_ADMIN capabilities in order to load default XSK/XDP programs to the network interface and configure BTF maps.

I think you mean "BPF" actually?

"BPF Type Format maps" kind of makes some sense, but yes. :)

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

It is possible, however,
to run only with CAP_NET_RAW.

Qemu often runs without any privileges, so we need to fix it.

I think adding support for SCM_RIGHTS via monitor would be a way to go.

Quoted 8 lines

I looked through the code and it seems like we can run completely non-privileged as far as kernel concerned. We'll need an API modification in libxdp though.

The thing is, IIUC, the only syscall that requires CAP_NET_RAW is a base socket creation. Binding and other configuration doesn't require any privileges. So, we could create a socket externally and pass it to QEMU.

That's the way TAP works for example.

Quoted 4 lines

Should work, unless it's an oversight from
the kernel side that needs to be patched. :) libxdp doesn't have a way to specify externally created socket today, so we'll need to change that. Should be easy to do though. I can explore.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 1 line

Please do that.

I have a prototype:
https://github.com/igsilya/xdp-tools/commit/db73e90945e3aa5e451ac88c42c83cb9389642d3

Need to test it out and then submit PR to xdp-tools project.

The change is now accepted:
https://github.com/xdp-project/xdp-tools/commit/740c839806a02517da5bce7bd0ccaba908b3f675

I can update the QEMU patch with support for passing socket fds. It may look like this:

-netved af-xdp,eth0,queues=2,inhibit=on,sock-fds=fd1,fd2

We'll need an fd per queue. And we may require these fds to be already added to the xsks map, so QEMU doesn't need xsks-map-fd.

I'd say we'll need to compile support for that conditionally based on availability of xsk_umem__create_with_fd() as it may not be available in distributions for some time.
Alternative is to require libxdp >= 1.4.0, which is not released yet.

The last restriction will be that QEMU will need 32 MB of RLIMIT_MEMLOCK per queue for umem registration, but that should not be a huge deal, right? Alternative is to have CAP_IPC_LOCK.

And I'd keep the xsks-map-fd parameter for setups that do not have latest libxdp and can allow CAP_NET_RAW. So, they could still do:

-netdev af-xdp,eth0,queues=2,inhibit=on,xsks-map-fd=fd

What do you think?

Quoted 49 lines
Quoted 38 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines

In case the bind syscall will actually need CAP_NET_RAW for some reason, we could change the kernel and allow non-privileged bind by utilizing, e.g. SO_BINDTODEVICE. i.e., let the privileged process bind the socket to a particular device, so QEMU can't bind it to a random one. Might be a good use case to allow even if not strictly necessary.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Yes.

Will propose something for a kernel as well. We might want something more granular though, e.g. bind to a queue instead of a device. In case we want better control in the device sharing scenario.

I may miss something but the bind is already done at dev plus queue right now, isn't it?

Yes, the bind() syscall will bind socket to the dev+queue. I was talking about SO_BINDTODEVICE that only ties the socket to a particular device, but not a queue.

Assuming SO_BINDTODEVICE is implemented for AF_XDP sockets and assuming a privileged process does:

  fd = socket(AF_XDP, ...);
  setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, <device>);

And sends fd to a non-privileged process. That non-privileged process will be able to call:

  bind(fd, <device>, <random queue>);

It will have to use the same device, but can choose any queue, if that queue is not already busy with another socket.

Quoted 7 lines

So, I was thinking maybe implementing something like XDP_BINDTOQID option. This way the privileged process may call:

  setsockopt(fd, SOL_XDP, XDP_BINDTOQID, <device>, <queue>);

And later kernel will be able to refuse bind() for any other queue for this particular socket.

Not sure, if file descriptor passing works, we probably don't need another way.

Quoted 1 line
Quoted 8 lines
Quoted 6 lines

Not sure if that is necessary though.
Since we're allocating the socket in the privileged process, that process may add the socket to the BPF map on the correct queue id. This way the non-privileged process will not be able to receive any packets from any other queue on this socket, even if bound to it. And no other AF_XDP socket will be able to be bound to that other queue as well.

I think that's by design, or anything wrong with this model?

No, should be fine. I'll posted a simple SO_BINDTODEVICE change to bpf-next as an RFC for now since the tree is closed:
https://lore.kernel.org/netdev/[email protected]/

Will re-send a non-RFC once it is open (after 10th of July, IIRC).

Quoted 18 lines
Quoted 8 lines

So, the
rogue QEMU will be able to hog one extra queue, but it will not be able to intercept traffic any from it, AFAICT. May not be a huge problem after all.

SO_BINDTODEVICE would still be nice to have. Especially for cases where we give the whole device to one VM.

Then we need to use AF_XDP in the guest which seems to be a different topic. Alibaba is working on the AF_XDP support for virtio-net.

Thanks

Quoted 3 lines

Best regards, Ilya Maximets.

#30 lore raw
In reply to Ilya Maximets

Can multiple VMs share a host netdev by filtering incoming traffic based on each VM's MAC address and directing it to the appropriate XSK? If yes, then I think AF_XDP is interesting when SR-IOV or similar hardware features are not available.

The idea of an AF_XDP passthrough device seems interesting because it would minimize the overhead and avoid some of the existing software limitations (mostly in QEMU's networking subsystem) that you described. I don't know whether the AF_XDP API is suitable or can be extended to build a hardware emulation interface, but it seems plausible. When Stefano Garzarella played with io_uring passthrough into the guest, one of the issues was guest memory translation (since the guest doesn't use host userspace virtual addresses). I guess AF_XDP would need an API for adding/removing memory translations or operate in a mode where addresses are relative offsets from the start of the umem regions (but this may be impractical if it limits where the guest can allocate packet payload buffers).

Whether you pursue the passthrough approach or not, making -netdev af-xdp work in an environment where QEMU runs unprivileged seems like the most important practical issue to solve.

Stefan

#31 lore raw
Quoted 4 lines · Stefan Hajnoczi

Can multiple VMs share a host netdev by filtering incoming traffic based on each VM's MAC address and directing it to the appropriate XSK? If yes, then I think AF_XDP is interesting when SR-IOV or similar hardware features are not available.

Good point. Thanks!

Yes, they can. Traffic can be re-directed via 'ethtool -N' similarly to an example in the patch. Or, potentially, via custom XDP program. Then different QEMU instances may use different start-queue arguments and use their own range of queues this way.

Quoted 12 lines

The idea of an AF_XDP passthrough device seems interesting because it would minimize the overhead and avoid some of the existing software limitations (mostly in QEMU's networking subsystem) that you described. I don't know whether the AF_XDP API is suitable or can be extended to build a hardware emulation interface, but it seems plausible. When Stefano Garzarella played with io_uring passthrough into the guest, one of the issues was guest memory translation (since the guest doesn't use host userspace virtual addresses). I guess AF_XDP would need an API for adding/removing memory translations or operate in a mode where addresses are relative offsets from the start of the umem regions

Actually, addresses in AF_XDP rings are already offsets from the start of the umem region. For example, xsk_umem__get_data is implemented as &((char *)umem_area)[addr]; inside libxdp. So, that should not be an issue.

Quoted 2 lines

(but this may be impractical if it limits where
the guest can allocate packet payload buffers).

Yeah, we will either need to:

a. register the whole guest memory as umem and offset buffer pointers
in the guest driver by the start of guest physical memory.

(I'm not familiar much with QEMU memory subsystem. Is guest physical
memory always start at 0? I know that it's not always true for the real hardware.)

b. or require the guest driver to allocate a chunk of aligned contiguous
memory and copy all the packets there on Tx. And populate the Fill ring only with buffers from that area. Assuming guest pages align with the host pages. Again, a single copy might not be that bad, but it's hard to tell what the actual impact will be without testing.

Quoted 4 lines

Whether you pursue the passthrough approach or not, making -netdev af-xdp work in an environment where QEMU runs unprivileged seems like the most important practical issue to solve.

Yes, working on it. Doesn't seem to be hard to do, but I need to test.

Best regards, Ilya Maximets.