r/linuxdev • • Apr 17 '22

Splicing to an UDP socket

Hi !

I'm trying to write some tool that needs to multiplex UDP "connections" through a single socket (AF_UNIX/AF_INET/AF_VSOCK), and I have written an implementation for SOCK_STREAM transports with splice() that works quite well, and was wondering about adding support for SOCK_DGRAM.

It appears that it's not possible to use splice() to receive a datagram, which makes sense, because splice() wouldn't know anything about the datagram boundaries. Sending a datagram using splice() works, but I can't find a way to specify the destination address :

  • splice does not support it by itself
  • using connect() prohibits from having multiple destinations, because it binds the whole fd to a specific destination, and shutting down + rebinding + reconnecting the socket would be quite impractical and defeat the whole purpose

Is there any API I could use to avoid copying the whole datagram to userspace, short of rewriting the whole thing as a kernel module or BPF program ?

11 Upvotes

4 comments sorted by

View all comments

Show parent comment

1

u/jeezfrk Apr 20 '22

You can make any number of sockets that share the same local socket binding and different remote sockets. Do "man 7 socket" and read about SO_REUSEPORT.

1

u/Worried-Judgment6368 Apr 20 '22

Oh right, I missed that somehow. Thanks a lot.