1
0
mirror of https://passt.top/passt synced 2024-12-22 05:35:23 +00:00

udp: Prepare udp.c to be shared with vhost-user

Export udp_payload_t, udp_update_hdr4(), udp_update_hdr6() and
udp_sock_errs().

Rename udp_listen_sock_handler() to udp_buf_listen_sock_handler() and
udp_reply_sock_handler to udp_buf_reply_sock_handler().

Signed-off-by: Laurent Vivier <lvivier@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
Laurent Vivier 2024-11-22 17:43:31 +01:00 committed by Stefano Brivio
parent 31117b27c6
commit 5a8b33c667
2 changed files with 79 additions and 29 deletions

66
udp.c
View File

@ -109,8 +109,7 @@
#include "pcap.h" #include "pcap.h"
#include "log.h" #include "log.h"
#include "flow_table.h" #include "flow_table.h"
#include "udp_internal.h"
#define UDP_MAX_FRAMES 32 /* max # of frames to receive at once */
/* "Spliced" sockets indexed by bound port (host order) */ /* "Spliced" sockets indexed by bound port (host order) */
static int udp_splice_ns [IP_VERSIONS][NUM_PORTS]; static int udp_splice_ns [IP_VERSIONS][NUM_PORTS];
@ -118,20 +117,8 @@ static int udp_splice_init[IP_VERSIONS][NUM_PORTS];
/* Static buffers */ /* Static buffers */
/** /* UDP header and data for inbound messages */
* struct udp_payload_t - UDP header and data for inbound messages static struct udp_payload_t udp_payload[UDP_MAX_FRAMES];
* @uh: UDP header
* @data: UDP data
*/
static struct udp_payload_t {
struct udphdr uh;
char data[USHRT_MAX - sizeof(struct udphdr)];
#ifdef __AVX2__
} __attribute__ ((packed, aligned(32)))
#else
} __attribute__ ((packed, aligned(__alignof__(unsigned int))))
#endif
udp_payload[UDP_MAX_FRAMES];
/* Ethernet header for IPv4 frames */ /* Ethernet header for IPv4 frames */
static struct ethhdr udp4_eth_hdr; static struct ethhdr udp4_eth_hdr;
@ -302,7 +289,7 @@ static void udp_splice_send(const struct ctx *c, size_t start, size_t n,
* *
* Return: size of IPv4 payload (UDP header + data) * Return: size of IPv4 payload (UDP header + data)
*/ */
static size_t udp_update_hdr4(struct iphdr *ip4h, struct udp_payload_t *bp, size_t udp_update_hdr4(struct iphdr *ip4h, struct udp_payload_t *bp,
const struct flowside *toside, size_t dlen, const struct flowside *toside, size_t dlen,
bool no_udp_csum) bool no_udp_csum)
{ {
@ -345,7 +332,7 @@ static size_t udp_update_hdr4(struct iphdr *ip4h, struct udp_payload_t *bp,
* *
* Return: size of IPv6 payload (UDP header + data) * Return: size of IPv6 payload (UDP header + data)
*/ */
static size_t udp_update_hdr6(struct ipv6hdr *ip6h, 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, const struct flowside *toside, size_t dlen,
bool no_udp_csum) bool no_udp_csum)
{ {
@ -477,7 +464,7 @@ static int udp_sock_recverr(int s)
* *
* Return: Number of errors handled, or < 0 if we have an unrecoverable error * Return: Number of errors handled, or < 0 if we have an unrecoverable error
*/ */
static int udp_sock_errs(const struct ctx *c, int s, uint32_t events) int udp_sock_errs(const struct ctx *c, int s, uint32_t events)
{ {
unsigned n_err = 0; unsigned n_err = 0;
socklen_t errlen; socklen_t errlen;
@ -554,7 +541,7 @@ static int udp_sock_recv(const struct ctx *c, int s, uint32_t events,
} }
/** /**
* udp_listen_sock_handler() - Handle new data from socket * udp_buf_listen_sock_handler() - Handle new data from socket
* @c: Execution context * @c: Execution context
* @ref: epoll reference * @ref: epoll reference
* @events: epoll events bitmap * @events: epoll events bitmap
@ -562,8 +549,9 @@ static int udp_sock_recv(const struct ctx *c, int s, uint32_t events,
* *
* #syscalls recvmmsg * #syscalls recvmmsg
*/ */
void udp_listen_sock_handler(const struct ctx *c, union epoll_ref ref, static void udp_buf_listen_sock_handler(const struct ctx *c,
uint32_t events, const struct timespec *now) union epoll_ref ref, uint32_t events,
const struct timespec *now)
{ {
const socklen_t sasize = sizeof(udp_meta[0].s_in); const socklen_t sasize = sizeof(udp_meta[0].s_in);
int n, i; int n, i;
@ -630,7 +618,21 @@ void udp_listen_sock_handler(const struct ctx *c, union epoll_ref ref,
} }
/** /**
* udp_reply_sock_handler() - Handle new data from flow specific socket * udp_listen_sock_handler() - Handle new data from socket
* @c: Execution context
* @ref: epoll reference
* @events: epoll events bitmap
* @now: Current timestamp
*/
void udp_listen_sock_handler(const struct ctx *c,
union epoll_ref ref, uint32_t events,
const struct timespec *now)
{
udp_buf_listen_sock_handler(c, ref, events, now);
}
/**
* udp_buf_reply_sock_handler() - Handle new data from flow specific socket
* @c: Execution context * @c: Execution context
* @ref: epoll reference * @ref: epoll reference
* @events: epoll events bitmap * @events: epoll events bitmap
@ -638,8 +640,9 @@ void udp_listen_sock_handler(const struct ctx *c, union epoll_ref ref,
* *
* #syscalls recvmmsg * #syscalls recvmmsg
*/ */
void udp_reply_sock_handler(const struct ctx *c, union epoll_ref ref, static void udp_buf_reply_sock_handler(const struct ctx *c, union epoll_ref ref,
uint32_t events, const struct timespec *now) uint32_t events,
const struct timespec *now)
{ {
flow_sidx_t tosidx = flow_sidx_opposite(ref.flowside); flow_sidx_t tosidx = flow_sidx_opposite(ref.flowside);
const struct flowside *toside = flowside_at_sidx(tosidx); const struct flowside *toside = flowside_at_sidx(tosidx);
@ -685,6 +688,19 @@ void udp_reply_sock_handler(const struct ctx *c, union epoll_ref ref,
} }
} }
/**
* udp_reply_sock_handler() - Handle new data from flow specific socket
* @c: Execution context
* @ref: epoll reference
* @events: epoll events bitmap
* @now: Current timestamp
*/
void udp_reply_sock_handler(const struct ctx *c, union epoll_ref ref,
uint32_t events, const struct timespec *now)
{
udp_buf_reply_sock_handler(c, ref, events, now);
}
/** /**
* udp_tap_handler() - Handle packets from tap * udp_tap_handler() - Handle packets from tap
* @c: Execution context * @c: Execution context

34
udp_internal.h Normal file
View File

@ -0,0 +1,34 @@
/* SPDX-License-Identifier: GPL-2.0-or-later
* Copyright (c) 2021 Red Hat GmbH
* Author: Stefano Brivio <sbrivio@redhat.com>
*/
#ifndef UDP_INTERNAL_H
#define UDP_INTERNAL_H
#include "tap.h" /* needed by udp_meta_t */
#define UDP_MAX_FRAMES 32 /* max # of frames to receive at once */
/**
* struct udp_payload_t - UDP header and data for inbound messages
* @uh: UDP header
* @data: UDP data
*/
struct udp_payload_t {
struct udphdr uh;
char data[USHRT_MAX - sizeof(struct udphdr)];
#ifdef __AVX2__
} __attribute__ ((packed, aligned(32)));
#else
} __attribute__ ((packed, aligned(__alignof__(unsigned int))));
#endif
size_t udp_update_hdr4(struct iphdr *ip4h, struct udp_payload_t *bp,
const struct flowside *toside, size_t dlen,
bool no_udp_csum);
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_sock_errs(const struct ctx *c, int s, uint32_t events);
#endif /* UDP_INTERNAL_H */