From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=none (p=none dis=none) header.from=gibson.dropbear.id.au Authentication-Results: passt.top; dkim=pass (2048-bit key; secure) header.d=gibson.dropbear.id.au header.i=@gibson.dropbear.id.au header.a=rsa-sha256 header.s=202502 header.b=IfkvLtrX; dkim-atps=neutral Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 658905A026F for ; Wed, 26 Mar 2025 04:44:28 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202502; t=1742960649; bh=A8U9ZoE6OAPjZ8ZFo2BGGHIIfZF4EGSGj65XHH2oTuI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IfkvLtrXLwkW92/3yMSxE5lNBY/QdE1N5KCuV3v2T4cuXQIB/WdSCgAkTanjAgJ3B Jr3ovE6OuVjJsGeoDWPjyEQFa1rF/PjhKMDDyF+sP0o16FvlUHQbYfMuCmlyzWlCl1 01eBAxNRnQr3JQX2JGxtHbf0npED70t1KaUxO+tuoaQDYyAjiYny+i4sO76SKTdmYJ LX2L+lUJOd2xJ3Li+3TeAbAqRAO4//+u0k8pJk38lGJTieJFtzKNewx2qvFmC0OtkU pQxL0Awj9fPZPIBQSSlH2bSC89soii81d21+YBK1yn7R+7/hQD8NN+N32ImcBEz6y2 w2paIfYpuPhgQ== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4ZMt215m9Qz4x8f; Wed, 26 Mar 2025 14:44:09 +1100 (AEDT) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH v2 7/7] udp: Add helper function for creating connected UDP socket Date: Wed, 26 Mar 2025 14:44:07 +1100 Message-ID: <20250326034407.2240846-8-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250326034407.2240846-1-david@gibson.dropbear.id.au> References: <20250326034407.2240846-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: A45RS3XH2NOC47I6R3K7V5QCWX33OX3D X-Message-ID-Hash: A45RS3XH2NOC47I6R3K7V5QCWX33OX3D 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: 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: Currently udp_flow_new() open codes creating and connecting a socket to use for reply messages. We have in mind some more places to use this logic, plus it just makes for a rather large function. Split this handling out into a new udp_flow_sock() function. Signed-off-by: David Gibson --- udp_flow.c | 104 +++++++++++++++++++++++++++++------------------------ 1 file changed, 58 insertions(+), 46 deletions(-) diff --git a/udp_flow.c b/udp_flow.c index 7e809244..bf4b8965 100644 --- a/udp_flow.c +++ b/udp_flow.c @@ -61,6 +61,61 @@ void udp_flow_close(const struct ctx *c, struct udp_flow *uflow) uflow->closed = true; } +/** + * udp_flow_sock() - Create, bind and connect a flow specific UDP socket + * @c: Execution context + * @uflow: UDP flow to open socket for + * @sidei: Side of @uflow to open socket for + * + * Return: fd of new socket on success, -ve error code on failure + */ +static int udp_flow_sock(const struct ctx *c, + const struct udp_flow *uflow, unsigned sidei) +{ + const struct flowside *side = &uflow->f.side[sidei]; + struct mmsghdr discard[UIO_MAXIOV] = { 0 }; + uint8_t pif = uflow->f.pif[sidei]; + union { + flow_sidx_t sidx; + uint32_t data; + } fref = { .sidx = FLOW_SIDX(uflow, sidei) }; + int rc, s; + + s = flowside_sock_l4(c, EPOLL_TYPE_UDP_REPLY, pif, side, fref.data); + if (s < 0) { + flow_dbg_perror(uflow, "Couldn't open flow specific socket"); + return s; + } + + if (flowside_connect(c, s, pif, side) < 0) { + rc = -errno; + flow_dbg_perror(uflow, "Couldn't connect flow socket"); + return rc; + } + + /* It's possible, if unlikely, that we could receive some unrelated + * packets in between the bind() and connect() of this socket. For now + * we just discard these. + * + * FIXME: Redirect these to an appropriate handler + */ + rc = recvmmsg(s, discard, ARRAY_SIZE(discard), MSG_DONTWAIT, NULL); + if (rc >= ARRAY_SIZE(discard)) { + flow_dbg(uflow, "Too many (%d) spurious reply datagrams", rc); + return -E2BIG; + } + + if (rc > 0) { + flow_trace(uflow, "Discarded %d spurious reply datagrams", rc); + } else if (errno != EAGAIN) { + rc = -errno; + flow_perror(uflow, "Unexpected error discarding datagrams"); + return rc; + } + + return s; +} + /** * udp_flow_new() - Common setup for a new UDP flow * @c: Execution context @@ -74,13 +129,10 @@ static flow_sidx_t udp_flow_new(const struct ctx *c, union flow *flow, int s_ini, const struct timespec *now) { struct udp_flow *uflow = NULL; - const struct flowside *tgt; unsigned sidei; - uint8_t tgtpif; - if (!(tgt = flow_target(c, flow, IPPROTO_UDP))) + if (!flow_target(c, flow, IPPROTO_UDP)) goto cancel; - tgtpif = flow->f.pif[TGTSIDE]; uflow = FLOW_SET_TYPE(flow, FLOW_UDP, udp); uflow->ts = now->tv_sec; @@ -98,49 +150,9 @@ static flow_sidx_t udp_flow_new(const struct ctx *c, union flow *flow, } } - if (pif_is_socket(tgtpif)) { - struct mmsghdr discard[UIO_MAXIOV] = { 0 }; - union { - flow_sidx_t sidx; - uint32_t data; - } fref = { - .sidx = FLOW_SIDX(flow, TGTSIDE), - }; - int rc; - - uflow->s[TGTSIDE] = flowside_sock_l4(c, EPOLL_TYPE_UDP_REPLY, - tgtpif, tgt, fref.data); - if (uflow->s[TGTSIDE] < 0) { - flow_dbg_perror(uflow, - "Couldn't open socket for spliced flow"); + if (pif_is_socket(flow->f.pif[TGTSIDE])) + if ((uflow->s[TGTSIDE] = udp_flow_sock(c, uflow, TGTSIDE)) < 0) goto cancel; - } - - if (flowside_connect(c, uflow->s[TGTSIDE], tgtpif, tgt) < 0) { - flow_dbg_perror(uflow, "Couldn't connect flow socket"); - goto cancel; - } - - /* It's possible, if unlikely, that we could receive some - * unrelated packets in between the bind() and connect() of this - * socket. For now we just discard these. We could consider - * trying to redirect these to an appropriate handler, if we - * need to. - */ - rc = recvmmsg(uflow->s[TGTSIDE], discard, ARRAY_SIZE(discard), - MSG_DONTWAIT, NULL); - if (rc >= ARRAY_SIZE(discard)) { - flow_dbg(uflow, - "Too many (%d) spurious reply datagrams", rc); - goto cancel; - } else if (rc > 0) { - flow_trace(uflow, - "Discarded %d spurious reply datagrams", rc); - } else if (errno != EAGAIN) { - flow_perror(uflow, - "Unexpected error discarding datagrams"); - } - } /* Tap sides always need to be looked up by hash. Socket sides don't * always, but sometimes do (receiving packets on a socket not specific -- 2.49.0