diff options
author | David Gibson <david@gibson.dropbear.id.au> | 2025-04-04 21:15:33 +1100 |
---|---|---|
committer | Stefano Brivio <sbrivio@redhat.com> | 2025-04-07 21:29:23 +0200 |
commit | 84ab1305fabaf07b5badf433e55a458de5b86918 (patch) | |
tree | d98831b1c114a6aabf23e4f65b69eb8ff1cb6c82 | |
parent | 1d7bbb101a0b1dcbc99c51cd65abb90a0144ac7b (diff) | |
download | passt-84ab1305fabaf07b5badf433e55a458de5b86918.tar passt-84ab1305fabaf07b5badf433e55a458de5b86918.tar.gz passt-84ab1305fabaf07b5badf433e55a458de5b86918.tar.bz2 passt-84ab1305fabaf07b5badf433e55a458de5b86918.tar.lz passt-84ab1305fabaf07b5badf433e55a458de5b86918.tar.xz passt-84ab1305fabaf07b5badf433e55a458de5b86918.tar.zst passt-84ab1305fabaf07b5badf433e55a458de5b86918.zip |
udp: Polish udp_vu_sock_info() and remove from vu specific code
udp_vu_sock_info() uses MSG_PEEK to look ahead at the next datagram to be
received and gets its source address. Currently we only use it in the
vhost-user path, but there's nothing inherently vhost-user specific about
it. We have upcoming uses for it elsewhere so rename and move to udp.c.
While we're there, polish its error reporting a litle.
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
[sbrivio: Drop excess newline before udp_sock_recv()]
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
-rw-r--r-- | udp.c | 24 | ||||
-rw-r--r-- | udp_internal.h | 1 | ||||
-rw-r--r-- | udp_vu.c | 19 |
3 files changed, 26 insertions, 18 deletions
@@ -630,6 +630,30 @@ static int udp_sock_errs(const struct ctx *c, union epoll_ref ref) } /** + * udp_peek_addr() - Get source address for next packet + * @s: Socket to get information from + * @src: Socket address (output) + * + * Return: 0 on success, -1 otherwise + */ +int udp_peek_addr(int s, union sockaddr_inany *src) +{ + struct msghdr msg = { + .msg_name = src, + .msg_namelen = sizeof(*src), + }; + int rc; + + rc = recvmsg(s, &msg, MSG_PEEK | MSG_DONTWAIT); + if (rc < 0) { + if (errno != EAGAIN && errno != EWOULDBLOCK) + warn_perror("Error peeking at socket address"); + return rc; + } + return 0; +} + +/** * udp_sock_recv() - Receive datagrams from a socket * @c: Execution context * @s: Socket to receive from diff --git a/udp_internal.h b/udp_internal.h index 02724e5..43a6109 100644 --- a/udp_internal.h +++ b/udp_internal.h @@ -30,5 +30,6 @@ size_t udp_update_hdr4(struct iphdr *ip4h, struct udp_payload_t *bp, size_t udp_update_hdr6(struct ipv6hdr *ip6h, struct udp_payload_t *bp, const struct flowside *toside, size_t dlen, bool no_udp_csum); +int udp_peek_addr(int s, union sockaddr_inany *src); #endif /* UDP_INTERNAL_H */ @@ -58,23 +58,6 @@ static size_t udp_vu_hdrlen(bool v6) } /** - * udp_vu_sock_info() - get socket information - * @s: Socket to get information from - * @s_in: Socket address (output) - * - * Return: 0 if socket address can be read, -1 otherwise - */ -static int udp_vu_sock_info(int s, union sockaddr_inany *s_in) -{ - struct msghdr msg = { - .msg_name = s_in, - .msg_namelen = sizeof(union sockaddr_inany), - }; - - return recvmsg(s, &msg, MSG_PEEK | MSG_DONTWAIT); -} - -/** * udp_vu_sock_recv() - Receive datagrams from socket into vhost-user buffers * @c: Execution context * @s: Socket to receive from @@ -230,7 +213,7 @@ void udp_vu_listen_sock_data(const struct ctx *c, union epoll_ref ref, int iov_used; bool v6; - if (udp_vu_sock_info(ref.fd, &s_in) < 0) + if (udp_peek_addr(ref.fd, &s_in) < 0) break; sidx = udp_flow_from_sock(c, ref, &s_in, now); |