1
0
mirror of https://passt.top/passt synced 2024-11-09 14:19:56 +00:00

flow: Helper to create sockets based on flowside

We have upcoming use cases where it's useful to create new bound socket
based on information from the flow table.  Add flowside_sock_l4() to do
this for either PIF_HOST or PIF_SPLICE sockets.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
David Gibson 2024-07-18 15:26:41 +10:00 committed by Stefano Brivio
parent 2faf6fcd8b
commit 781164e25b
4 changed files with 102 additions and 3 deletions

93
flow.c
View File

@ -5,9 +5,11 @@
* Tracking for logical "flows" of packets. * Tracking for logical "flows" of packets.
*/ */
#include <errno.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <sched.h>
#include <string.h> #include <string.h>
#include "util.h" #include "util.h"
@ -143,6 +145,97 @@ static void flowside_from_af(struct flowside *side, sa_family_t af,
side->eport = eport; side->eport = eport;
} }
/**
* struct flowside_sock_args - Parameters for flowside_sock_splice()
* @c: Execution context
* @fd: Filled in with new socket fd
* @err: Filled in with errno if something failed
* @type: Socket epoll type
* @sa: Socket address
* @sl: Length of @sa
* @data: epoll reference data
*/
struct flowside_sock_args {
const struct ctx *c;
int fd;
int err;
enum epoll_type type;
const struct sockaddr *sa;
socklen_t sl;
const char *path;
uint32_t data;
};
/** flowside_sock_splice() - Create and bind socket for PIF_SPLICE based on flowside
* @arg: Argument as a struct flowside_sock_args
*
* Return: 0
*/
static int flowside_sock_splice(void *arg)
{
struct flowside_sock_args *a = arg;
ns_enter(a->c);
a->fd = sock_l4_sa(a->c, a->type, a->sa, a->sl, NULL,
a->sa->sa_family == AF_INET6, a->data);
a->err = errno;
return 0;
}
/** flowside_sock_l4() - Create and bind socket based on flowside
* @c: Execution context
* @type: Socket epoll type
* @pif: Interface for this socket
* @tgt: Target flowside
* @data: epoll reference portion for protocol handlers
*
* Return: socket fd of protocol @proto bound to the forwarding address and port
* from @tgt (if specified).
*/
/* cppcheck-suppress unusedFunction */
int flowside_sock_l4(const struct ctx *c, enum epoll_type type, uint8_t pif,
const struct flowside *tgt, uint32_t data)
{
const char *ifname = NULL;
union sockaddr_inany sa;
socklen_t sl;
ASSERT(pif_is_socket(pif));
pif_sockaddr(c, &sa, &sl, pif, &tgt->faddr, tgt->fport);
switch (pif) {
case PIF_HOST:
if (inany_is_loopback(&tgt->faddr))
ifname = NULL;
else if (sa.sa_family == AF_INET)
ifname = c->ip4.ifname_out;
else if (sa.sa_family == AF_INET6)
ifname = c->ip6.ifname_out;
return sock_l4_sa(c, type, &sa, sl, ifname,
sa.sa_family == AF_INET6, data);
case PIF_SPLICE: {
struct flowside_sock_args args = {
.c = c, .type = type,
.sa = &sa.sa, .sl = sl, .data = data,
};
NS_CALL(flowside_sock_splice, &args);
errno = args.err;
return args.fd;
}
default:
/* If we add new socket pifs, they'll need to be implemented
* here
*/
ASSERT(0);
}
}
/** flow_log_ - Log flow-related message /** flow_log_ - Log flow-related message
* @f: flow the message is related to * @f: flow the message is related to
* @pri: Log priority * @pri: Log priority

3
flow.h
View File

@ -164,6 +164,9 @@ static inline bool flowside_eq(const struct flowside *left,
left->fport == right->fport; left->fport == right->fport;
} }
int flowside_sock_l4(const struct ctx *c, enum epoll_type type, uint8_t pif,
const struct flowside *tgt, uint32_t data);
/** /**
* struct flow_common - Common fields for packet flows * struct flow_common - Common fields for packet flows
* @state: State of the flow table entry * @state: State of the flow table entry

6
util.c
View File

@ -45,9 +45,9 @@
* *
* Return: newly created socket, negative error code on failure * Return: newly created socket, negative error code on failure
*/ */
static int sock_l4_sa(const struct ctx *c, enum epoll_type type, int sock_l4_sa(const struct ctx *c, enum epoll_type type,
const void *sa, socklen_t sl, const void *sa, socklen_t sl,
const char *ifname, bool v6only, uint32_t data) const char *ifname, bool v6only, uint32_t data)
{ {
sa_family_t af = ((const struct sockaddr *)sa)->sa_family; sa_family_t af = ((const struct sockaddr *)sa)->sa_family;
union epoll_ref ref = { .type = type, .data = data }; union epoll_ref ref = { .type = type, .data = data };

3
util.h
View File

@ -144,6 +144,9 @@ struct ctx;
/* cppcheck-suppress funcArgNamesDifferent */ /* cppcheck-suppress funcArgNamesDifferent */
__attribute__ ((weak)) int ffsl(long int i) { return __builtin_ffsl(i); } __attribute__ ((weak)) int ffsl(long int i) { return __builtin_ffsl(i); }
int sock_l4_sa(const struct ctx *c, enum epoll_type type,
const void *sa, socklen_t sl,
const char *ifname, bool v6only, uint32_t data);
int sock_l4(const struct ctx *c, sa_family_t af, enum epoll_type type, int sock_l4(const struct ctx *c, sa_family_t af, enum epoll_type type,
const void *bind_addr, const char *ifname, uint16_t port, const void *bind_addr, const char *ifname, uint16_t port,
uint32_t data); uint32_t data);