From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by passt.top (Postfix) with ESMTP id F11825A004F for ; Wed, 10 Jul 2024 23:32:44 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1720647163; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Wx+WbtzbDevQPLA/ZACpx77p0WuT0haKqvQkCdxJxMA=; b=f9pQiiQafmfkJAqVQELfdDzdofnONxbmH8p7ZCOulS8SKCrsB61y+eXt7ie5JNBB9vCt3A Xr+LflP5lKXn+0MrxLH36uF1s7JckbflUdoN83qkgWRb/fSm1nzMbkFhGzOR9dWgkn7cPn LoLjetBt0xLCw90amFTcX9tWev+1N2k= Received: from mail-vk1-f200.google.com (mail-vk1-f200.google.com [209.85.221.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-512-OGrezoFWMm-KiZ79w2gk5w-1; Wed, 10 Jul 2024 17:32:39 -0400 X-MC-Unique: OGrezoFWMm-KiZ79w2gk5w-1 Received: by mail-vk1-f200.google.com with SMTP id 71dfb90a1353d-4f2e0955cf1so68005e0c.3 for ; Wed, 10 Jul 2024 14:32:39 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720647159; x=1721251959; h=content-transfer-encoding:mime-version:organization:references :in-reply-to:message-id:subject:cc:to:from:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=Wx+WbtzbDevQPLA/ZACpx77p0WuT0haKqvQkCdxJxMA=; b=RHtqYnC2tTVYZYlDeguC4Ou5sKNcIxHHskDN5Qv9kUrWdrHfE5s4v7O+p2n892GUpn LbW/5IDZHA31JLQnwl+LLQi8ReEk41FFv1mlwFTYCdMMQbLX3lZjgHIO4ogBbMMA2qKQ Fj9rdh9KUMrqcK9XvfL2lxZdL2W/ROuh4mE0eSIA17UQIvPIg/U09Yw9EJktJG/CiXrk gkfLMWKfwLSaEFYD5FzVlxFfVGF7cJ75/Ro/+AICgppjfSHDk8pi07thwuuz3wfBEg/F ZyKt5ETt/LCu/cGKJ4+P/mP2UtrBL3vuJ5hQijurgHB0ZfruDro7aVovKNOVbwFhGxVk yu1g== X-Gm-Message-State: AOJu0YwipY2zuWsL6S/wzz9DdcY0I76337gij02mtJii00NOCCfsVOOw KtfT34Sx6Nkn4WVwRpUWV0mGom1pgv/gemBJw+qRU7FxDx+Y2PiDXxP/bgFVNFBnmztoGHWjxOH 1UeqfeWSZ656e3dm3OAByOtiOQA6KHV6k2dHPVA7BYvqL88hMrg== X-Received: by 2002:a05:6122:1d44:b0:4ef:27e0:3f8c with SMTP id 71dfb90a1353d-4f33ef74b4dmr8799144e0c.0.1720647159236; Wed, 10 Jul 2024 14:32:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE9zAywkmiJOSsZNwftHskkJOS7CaIPJhgLaUEe+t7qDAddt2MPecJnoPg8ruKf6Hys7o5nuA== X-Received: by 2002:a05:6122:1d44:b0:4ef:27e0:3f8c with SMTP id 71dfb90a1353d-4f33ef74b4dmr8799129e0c.0.1720647158866; Wed, 10 Jul 2024 14:32:38 -0700 (PDT) Received: from maya.cloud.tilaa.com (maya.cloud.tilaa.com. [164.138.29.33]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-447f9b3e973sm23618151cf.37.2024.07.10.14.32.37 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 10 Jul 2024 14:32:38 -0700 (PDT) Date: Wed, 10 Jul 2024 23:32:01 +0200 From: Stefano Brivio To: David Gibson Subject: Re: [PATCH v7 15/27] flow: Helper to create sockets based on flowside Message-ID: <20240710233201.3a05342b@elisabeth> In-Reply-To: <20240705020724.3447719-16-david@gibson.dropbear.id.au> References: <20240705020724.3447719-1-david@gibson.dropbear.id.au> <20240705020724.3447719-16-david@gibson.dropbear.id.au> Organization: Red Hat X-Mailer: Claws Mail 4.2.0 (GTK 3.24.41; x86_64-pc-linux-gnu) MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Message-ID-Hash: 6DRO7GNGMUTLQ5NCSRYENZDPFHMXADYL X-Message-ID-Hash: 6DRO7GNGMUTLQ5NCSRYENZDPFHMXADYL X-MailFrom: sbrivio@redhat.com 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: passt-dev@passt.top, jmaloy@redhat.com 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: On Fri, 5 Jul 2024 12:07:12 +1000 David Gibson wrote: > 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 | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > flow.h | 3 ++ > util.c | 6 ++-- > util.h | 3 ++ > 4 files changed, 101 insertions(+), 3 deletions(-) > > diff --git a/flow.c b/flow.c > index 6f09781a..2d0a8a32 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,96 @@ static void flowside_from_af(struct flowside *fside, sa_family_t af, > fside->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, Nit: assuming you wanted the extra whitespace here to align the assignment with the one of a->err below, I'd rather write this (at least for consistency) as "a->fd = ...". > + 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 */ For consistency: /* If we add new socket pifs, they'll need to be implemented * here */ there are a few occurrences in the next patches, not so important I guess, I can also do a pass later at some point. > + 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 c3a15ca6..e27f99be 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 9a73fbb9..f2994a79 100644 > --- a/util.c > +++ b/util.c > @@ -44,9 +44,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 d0150396..f2e4f8cf 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); -- Stefano