Skip to contentSkip to conversation

Best way to reduce system call overhead for tun device I/O?

DISCUSSION Guus Sliepen 5 participants 7 messages Mar 29, 2016 – Apr 4, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

I'm trying to reduce system call overhead when reading/writing to/from a tun device in userspace. For sockets, one can use sendmmsg()/recvmmsg(), but a tun fd is not a socket fd, so this doesn't work. I'm see several options to allow userspace to read/write multiple packets with one syscall:

- Implement a TX/RX ring buffer that is mmap()ed, like with AF_PACKET sockets.

- Implement a ioctl() to emulate sendmmsg()/recvmmsg().

- Add a flag that can be set using TUNSETIFF that makes regular read()/write() calls handle multiple packets in one go.

- Expose a socket fd to userspace, so regular sendmmsg()/recvmmsg() can be used. There is tun_get_socket() which is used internally in the kernel, but this is not exposed to userspace, and doesn't look trivial to do either.

What would be the right way to do this?

Signature · 2 lines
Met vriendelijke groet / with kind regards,
     Guus Sliepen <[email protected]>
#1 lore raw
Quoted 14 lines · Guus Sliepen

I'm trying to reduce system call overhead when reading/writing to/from a tun device in userspace. For sockets, one can use sendmmsg()/recvmmsg(), but a tun fd is not a socket fd, so this doesn't work. I'm see several options to allow userspace to read/write multiple packets with one syscall:

- Implement a TX/RX ring buffer that is mmap()ed, like with AF_PACKET sockets.

- Implement a ioctl() to emulate sendmmsg()/recvmmsg().

- Add a flag that can be set using TUNSETIFF that makes regular read()/write() calls handle multiple packets in one go.

Quoted 7 lines

- Expose a socket fd to userspace, so regular sendmmsg()/recvmmsg() can be used. There is tun_get_socket() which is used internally in the kernel, but this is not exposed to userspace, and doesn't look trivial to do either.

What would be the right way to do this?

Personally I think tun could benefit greatly if it were implemented as a socket instead of character interface. One thing that could be much better is sending/receiving of meta data attached to skbuf. For instance GSO data could be in ancillary data in a socket instead of inline with packet data as tun seems to be doing now.

Tom

Quoted 3 lines

--
Met vriendelijke groet / with kind regards,
Guus Sliepen <[email protected]>

maintainer
#2 lore raw

From: Tom Herbert <[email protected]>
Date: Thu, 31 Mar 2016 17:18:48 -0400

Quoted 20 lines · Guus Sliepen
Quoted 19 lines

I'm trying to reduce system call overhead when reading/writing to/from a tun device in userspace. For sockets, one can use sendmmsg()/recvmmsg(), but a tun fd is not a socket fd, so this doesn't work. I'm see several options to allow userspace to read/write multiple packets with one syscall:

- Implement a TX/RX ring buffer that is mmap()ed, like with AF_PACKET sockets.

- Implement a ioctl() to emulate sendmmsg()/recvmmsg().

- Add a flag that can be set using TUNSETIFF that makes regular read()/write() calls handle multiple packets in one go.

- Expose a socket fd to userspace, so regular sendmmsg()/recvmmsg() can be used. There is tun_get_socket() which is used internally in the kernel, but this is not exposed to userspace, and doesn't look trivial to do either.

Quoted 7 lines
Quoted 2 lines

What would be the right way to do this?

Personally I think tun could benefit greatly if it were implemented as a socket instead of character interface. One thing that could be much better is sending/receiving of meta data attached to skbuf. For instance GSO data could be in ancillary data in a socket instead of inline with packet data as tun seems to be doing now.

Agreed.

#3 lore raw
Quoted 10 lines · David Miller
Quoted 8 lines
Quoted 3 lines

I'm trying to reduce system call overhead when reading/writing to/from a tun device in userspace. [...] What would be the right way to do this?

Personally I think tun could benefit greatly if it were implemented as a socket instead of character interface. One thing that could be much better is sending/receiving of meta data attached to skbuf. For instance GSO data could be in ancillary data in a socket instead of inline with packet data as tun seems to be doing now.

Agreed.

Ok. So how should the userspace API work? Creating an AF_PACKET socket and then using a tun ioctl to create a tun interface and bind it to the socket?

int fd = socket(AF_PACKET, ...)
struct ifreq ifr = {...};
ioctl(fd, TUNSETIFF, &ifr);

Signature · 2 lines
Met vriendelijke groet / with kind regards,
     Guus Sliepen <[email protected]>
#4 lore raw
Quoted 13 lines · On Fri, 1 Apr 2016 00:28:57 +0200 Guus Sliepen <[email protected]> wrote:
Quoted 10 lines · David Miller
Quoted 8 lines
Quoted 3 lines

I'm trying to reduce system call overhead when reading/writing to/from a tun device in userspace. [...] What would be the right way to do this?

Personally I think tun could benefit greatly if it were implemented as a socket instead of character interface. One thing that could be much better is sending/receiving of meta data attached to skbuf. For instance GSO data could be in ancillary data in a socket instead of inline with packet data as tun seems to be doing now.

Agreed.

Quoted 8 lines

Ok. So how should the userspace API work? Creating an AF_PACKET socket and then using a tun ioctl to create a tun interface and bind it to the socket?

int fd = socket(AF_PACKET, ...)
struct ifreq ifr = {...};
ioctl(fd, TUNSETIFF, &ifr);

Rather than bodge AF_PACKET onto TUN, why not just create a new device type and control it from something modern like netlink.

#5 lore raw
Quoted 20 lines · Stephen Hemminger
Quoted 17 lines · On Fri, 1 Apr 2016 00:28:57 +0200 Guus Sliepen <[email protected]> wrote:
Quoted 10 lines · David Miller
Quoted 8 lines
Quoted 3 lines

I'm trying to reduce system call overhead when reading/writing to/from a tun device in userspace. [...] What would be the right way to do this?

Personally I think tun could benefit greatly if it were implemented as a socket instead of character interface. One thing that could be much better is sending/receiving of meta data attached to skbuf. For instance GSO data could be in ancillary data in a socket instead of inline with packet data as tun seems to be doing now.

Agreed.

Ok. So how should the userspace API work? Creating an AF_PACKET socket and then using a tun ioctl to create a tun interface and bind it to the socket?

Quoted 7 lines
Quoted 4 lines

int fd = socket(AF_PACKET, ...)
struct ifreq ifr = {...};
ioctl(fd, TUNSETIFF, &ifr);

Rather than bodge AF_PACKET onto TUN, why not just create a new device type and control it from something modern like netlink.

Depending on the use-case, it may be sufficient to extend AF_PACKET with limited tap functionality:

- add a po->xmit mode that reinjects into the kernel receive path, analogous to pktgen's M_NETIF_RECEIVE mode.

- optionally drop packets in __netif_receive_skb_core and xmit_one if any of the registered packet sockets accepted the packet and has a new intercept feature flag enabled.

This can be applied to a dummy device, but much more interesting is to interpose on the flow of a normal nic. It is clearly not a drop-in replacement for a tap (let alone tun) device. I have some preliminary code.

#6 lore raw
Quoted 4 lines · Willem de Bruijn
Quoted 2 lines · Stephen Hemminger

Rather than bodge AF_PACKET onto TUN, why not just create a new device type and control it from something modern like netlink.

Do we really want to introduce a whole new device type? The tun device is working perfectly fine, except for the fact that there is no way to send/receive multiple packets in one go.

Quoted 6 lines

Depending on the use-case, it may be sufficient to extend AF_PACKET with limited tap functionality:

- add a po->xmit mode that reinjects into the kernel receive path, analogous to pktgen's M_NETIF_RECEIVE mode.

Quoted 8 lines

- optionally drop packets in __netif_receive_skb_core and xmit_one if any of the registered packet sockets accepted the packet and has a new intercept feature flag enabled.

This can be applied to a dummy device, but much more interesting is to interpose on the flow of a normal nic. It is clearly not a drop-in replacement for a tap (let alone tun) device. I have some preliminary code.

It's not really tinc's use case, but I did try using socket(AF_PACKET) bound to a tun interface, just to see if sendmmsg()/recvmmsg() works then. It does, but indeed for packets that are sent to the socket, they need to be reinjected into the kernel receive path. So I'll be happy to test out your preliminary code.

Signature · 2 lines
Met vriendelijke groet / with kind regards,
     Guus Sliepen <[email protected]>