From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 103D45A0322 for ; Thu, 18 Jul 2024 07:27:09 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1721280418; bh=K0p2EnZmOzJxLfNCRELuZZAfu6qrGVi7oMZHnGO10o8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KyCGx3wauiUk0MrO1JjdBWuvHkBph9dOhgbq0ZQ7FlGd/w8pezwEJWPAz41otZbkB hUeFQKscKl1Qif5qT60I0HtXhMg0M1BPVNyLgLtlX58rTEGv20yEjDxbP8HgNhyuTr dq/C9XrFpwt5PCHDsY2robU1vAW+ONYCI5lkQ8XZH076992LnWY7tS4i/Atnmpaflw BWJzEMa9GxCJYHIDsoy08YjcRLZoIeNWa4e8F1b7PQypT97Pm1aANM0WCwlKmcWUox S/5nxXR7qG4QacWklRh+pNS1sQsALoerT1jUk/XWRCmNO3jkmRP27f/SrSab5RdERe DT6UW599cdQwg== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4WPhBV71NLz4x6l; Thu, 18 Jul 2024 15:26:58 +1000 (AEST) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH v8 15/27] flow: Helper to create sockets based on flowside Date: Thu, 18 Jul 2024 15:26:41 +1000 Message-ID: <20240718052653.3241585-16-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240718052653.3241585-1-david@gibson.dropbear.id.au> References: <20240718052653.3241585-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: UK4EJ52HA6QFJS6JS3HRE7Z46NCJXF32 X-Message-ID-Hash: UK4EJ52HA6QFJS6JS3HRE7Z46NCJXF32 X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: jmaloy@redhat.com, David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 --- flow.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ flow.h | 3 ++ util.c | 6 ++-- util.h | 3 ++ 4 files changed, 102 insertions(+), 3 deletions(-) diff --git a/flow.c b/flow.c index 8eea8615..6ba8a62e 100644 --- a/flow.c +++ b/flow.c @@ -5,9 +5,11 @@ * Tracking for logical "flows" of packets. */ +#include #include #include #include +#include #include #include "util.h" @@ -143,6 +145,97 @@ static void flowside_from_af(struct flowside *side, sa_family_t af, 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 * @f: flow the message is related to * @pri: Log priority diff --git a/flow.h b/flow.h index e3a778a7..bf6b8459 100644 --- a/flow.h +++ b/flow.h @@ -164,6 +164,9 @@ static inline bool flowside_eq(const struct flowside *left, 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 * @state: State of the flow table entry diff --git a/util.c b/util.c index 1569f1c0..6b51fc51 100644 --- a/util.c +++ b/util.c @@ -45,9 +45,9 @@ * * Return: newly created socket, negative error code on failure */ -static 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_sa(const struct ctx *c, enum epoll_type type, + const void *sa, socklen_t sl, + const char *ifname, bool v6only, uint32_t data) { sa_family_t af = ((const struct sockaddr *)sa)->sa_family; union epoll_ref ref = { .type = type, .data = data }; diff --git a/util.h b/util.h index 1d479ddf..826614cf 100644 --- a/util.h +++ b/util.h @@ -144,6 +144,9 @@ struct ctx; /* cppcheck-suppress funcArgNamesDifferent */ __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, const void *bind_addr, const char *ifname, uint16_t port, uint32_t data); -- 2.45.2