Does std:;net::UdpSocket require a call / syscall per packet? (x86_64 linux)

Socket2 appears to provide vectored ops: Socket in socket2 - Rust

I am wondering, with std::net::UdpSocket, if there is a way to read/send multiple packets with one call.

Neither socket2 nor std::net supports reading multiple packets at once. The linked Socket::recv_vectored() method delegates to recvmsg(), which only reads a single packet into multiple buffers. To read multiple packets, you need recvmmsg(), which nix offers as nix::sys::socket::recvmmsg(); you can use UdpSocket::as_raw_fd() to get the RawFd needed to call the function.

1 Like

Nix's implementation is horrible. Since it wants to be able to take an iterator, on every call it does n+1 allocs to hold the headers and iov. There is a straight forward fix for that with unsafe and some pointer math, but you are still allocing the header array on every call.

I'm not sure why the implementation is so bad, but a good one is needed because async and udp play together very poorly if every send it wrapped in an async block. If you are sending out 10 packets, that is 10 times you have to pass throug that yield point.

I have to do it at some points for my stuff, but I'm hoping somebody else does it before I get to it. I'm likely to just do a builder-like interface so the allocs can be done up front and reused for each new send cal.

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.