From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: passt.top; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=MBviMVKA; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by passt.top (Postfix) with ESMTPS id 3754C5A0271 for ; Tue, 18 Mar 2025 18:15:33 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1742318132; 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=lG8RwC7zBmZP2atGGNK4AjuaySdwPDqg6DGJhHt6RqU=; b=MBviMVKAr9fA915a7q3WXqYvLphegYroqGTXlnP3xcCuoBsKclQYGatjCE5VJybtWIUjqK 2/vGiT0BI/nuyx879C+c+Yyvp+riRhLqTdofBfdRjZGcCBvIMptlhkQN8QVMnd9Iam9C8h jkP4qq0vMvSisEKwi45uiKb68eJyJlI= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-356-qH-u7vmKP8uVNBynkK9RKg-1; Tue, 18 Mar 2025 13:15:30 -0400 X-MC-Unique: qH-u7vmKP8uVNBynkK9RKg-1 X-Mimecast-MFC-AGG-ID: qH-u7vmKP8uVNBynkK9RKg_1742318129 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-39141ffa913so3530718f8f.2 for ; Tue, 18 Mar 2025 10:15:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742318129; x=1742922929; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=lG8RwC7zBmZP2atGGNK4AjuaySdwPDqg6DGJhHt6RqU=; b=fkr+H4juNmbOXYDFAiCW9D1fxeT61BAAswvbqwABz7pfBPRwLqMGEa0TSCD3YVvS16 gr0o+8UIsBZqErabr+Kai8QMeHFkRgXRZP4mzoMwmng80xqqoa6BOXZ4bfhwIFe8mIZU FlZYmEVvOVB9AGGqmGAy1JGc0k+QgEUu4bDPNTwPEQX0NEy0e+e9qzKXWqo9zlJNLZaV mnQTFnuKuqev2e87+6FxLyk9P/88htG/mVjMCV6qtprrTwplA5rKOfhVrS4IyhBguivc sRwJ7sV5YxxuAew2ZIPkP4PkK/q0A5i2sWR2YTrEL9I292hV9h75SqIh4o0Zk1fsvRE4 3kLQ== X-Forwarded-Encrypted: i=1; AJvYcCUHt13zEKk30n1/TDpCC2GqCTMNBN0DoxWVJ+NF+sl5DnDoLIGUT1LDOGH9cHs9Xhz2d+5U0WyhuZ8=@passt.top X-Gm-Message-State: AOJu0YxMfY3ehS5IoUTotShWFb+8PXiX/eBQfYy9wMJM3QLssC3IYMOM HS5vFMoAa+PuUa0hnFaVK02XBM8GPvzRNo5FoR9Cr/Aq0xqLE0hMhwftIPyXxuz6p5a0i6+sbdV FSQS+1237fjO95qD/oEr0Ba5PcipYadF3cFrczRW/sx/iqpFvZQ== X-Gm-Gg: ASbGncsi9RCpiW2dA3hzpOwd6Ye04Z+xTtYwQOa32Gu3aqJ90KPlLzK9SWA7MNQn4o5 +x7/juKSZGvfjmQKYFdNmI1Cyb0DqIwoWlvxzOM/jH55TSNzbGLjUWgMXbAouSonh1DgI7zsqkR zZeT+saXVsh9SaGXhrq7lwoTw4POV1bxQrhm9eHjj/U4tVR57Lh2brVErVb68GDb5l0K/CJuQiH NcCyPIEo6ZfHvH2Ey2vQwT3rG+d/1nXqglrl6E3gwqIwt3qARTK96lch+mJuWCmlD6+5tLsdLGA OqsAxpvEZI1XZcuJjmDL X-Received: by 2002:a5d:598d:0:b0:391:1652:f0bf with SMTP id ffacd0b85a97d-3971e592875mr22743341f8f.33.1742318129152; Tue, 18 Mar 2025 10:15:29 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEVjGcmmUi08A/h8sLYQ+bYzUKonzNEmNdHr6Ouy0Lk6aMxcJEZxGf40R55OCyIqdwDVnZVDg== X-Received: by 2002:a5d:598d:0:b0:391:1652:f0bf with SMTP id ffacd0b85a97d-3971e592875mr22743296f8f.33.1742318128689; Tue, 18 Mar 2025 10:15:28 -0700 (PDT) Received: from [192.168.188.25] ([80.243.52.135]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43d1fdda38esm144313095e9.8.2025.03.18.10.15.27 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 18 Mar 2025 10:15:28 -0700 (PDT) Message-ID: <9c80668b-acd9-4cb9-963e-f7411701c8da@redhat.com> Date: Tue, 18 Mar 2025 18:15:27 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2] conf: Unify several paths in conf_ports() To: Stefano Brivio References: <20250312034359.2776948-1-david@gibson.dropbear.id.au> <20250315005028.294ca8d3@elisabeth> <20250318175437.1d62bf37@elisabeth> From: Paul Holzinger In-Reply-To: <20250318175437.1d62bf37@elisabeth> X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: bVJJ3Dxyx8nxWxd1luHgtdVCjNERehrvI7UqDsaFIZc_1742318129 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Message-ID-Hash: 77UMP4VIK7NWFZF33P7IDONBOJYQPMY6 X-Message-ID-Hash: 77UMP4VIK7NWFZF33P7IDONBOJYQPMY6 X-MailFrom: pholzing@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: David Gibson , passt-dev@passt.top 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 18/03/2025 17:54, Stefano Brivio wrote: > On Mon, 17 Mar 2025 12:50:36 +0100 > Paul Holzinger wrote: > >> On 15/03/2025 00:50, Stefano Brivio wrote: >>> On Wed, 12 Mar 2025 14:43:59 +1100 >>> David Gibson wrote: >>> >>>> In conf_ports() we have three different paths which actually do the setup >>>> of an individual forwarded port: one for the "all" case, one for the >>>> exclusions only case and one for the range of ports with possible >>>> exclusions case. >>>> >>>> We can unify those cases using a new helper which handles a single range >>>> of ports, with a bitmap of exclusions. Although this is slightly longer >>>> (largely due to the new helpers function comment), it reduces duplicated >>>> logic. It will also make future improvements to the tracking of port >>>> forwards easier. >>>> >>>> The new conf_ports_range_except() function has a pretty prodigious >>>> parameter list, but I still think it's an overall improvement in conceptual >>>> complexity. >>>> >>>> Signed-off-by: David Gibson >>>> --- >>>> conf.c | 173 ++++++++++++++++++++++++++++++--------------------------- >>>> 1 file changed, 90 insertions(+), 83 deletions(-) >>>> >>>> v2: >>>> * Commit message updated slightly, but otherwise unmodified. >>>> >>>> >>>> diff --git a/conf.c b/conf.c >>>> index 065e7201..4e0099ba 100644 >>>> --- a/conf.c >>>> +++ b/conf.c >>>> @@ -123,6 +123,75 @@ static int parse_port_range(const char *s, char **endptr, >>>> return 0; >>>> } >>>> >>>> +/** >>>> + * conf_ports_range_except() - Set up forwarding for a range of ports minus a >>>> + * bitmap of exclusions >>>> + * @c: Execution context >>>> + * @optname: Short option name, t, T, u, or U >>>> + * @optarg: Option argument (port specification) >>>> + * @fwd: Pointer to @fwd_ports to be updated >>>> + * @addr: Listening address >>>> + * @ifname: Listening interface >>>> + * @first: First port to forward >>>> + * @last: Last port to forward >>>> + * @exclude: Bitmap of ports to exclude >>>> + * @to: Port to translate @first to when forwarding >>>> + * @weak: Ignore errors, as long as at least one port is mapped >>>> + */ >>>> +static void conf_ports_range_except(const struct ctx *c, char optname, >>>> + const char *optarg, struct fwd_ports *fwd, >>>> + const union inany_addr *addr, >>>> + const char *ifname, >>>> + uint16_t first, uint16_t last, >>>> + const uint8_t *exclude, uint16_t to, >>>> + bool weak) >>>> +{ >>>> + bool bound_one = false; >>>> + unsigned i; >>>> + int ret; >>>> + >>>> + if (first == 0) { >>>> + die("Can't forward port 0 for option '-%c %s'", >>>> + optname, optarg); >>>> + } >>> This introduces two subtle functional changes that are a bit unexpected >>> given the commit message. Before: >>> >>> $ ./pasta -t 0 >>> $ >>> >>> $ ./pasta -t 0-1025 >>> Failed to bind port 1 (Permission denied) for option '-t 0-1025', exiting >>> >>> After: >>> >>> $ ./pasta -t 0 >>> Can't forward port 0 for option '-t 0' >>> >>> $ ./pasta -t 0-1025 >>> Can't forward port 0 for option '-t 0-1025' >>> >>> ...anyway, I doubt anybody would use -t 0 on purpose (to get a port >>> automatically assigned), and while it probably works for TCP (check >>> bound ports after starting pasta, use the assigned one), it wouldn't >>> necessarily work as expected for UDP if the application relies on our >>> flow tracking. >> Why would this not work for UDP? bind() wise you can still bind 0 fine >> and get a free port assigned? > The bind() part itself would work, but with the current implementation > we wouldn't be able to track flows corresponding to this specific port > forwarding, so I expect that the "return" (outbound) traffic won't > work. > > It's a matter of implementation (or lack thereof), we could get it to > work with a getsockname() after bind(). > > Before this change, it happened to work *by mistake* for TCP, not for > UDP. With this change, it doesn't work for TCP. We can add it back with > a proper syntax (-t ...any?), as David mentioned. > >>> For TCP, actually, -t 0 might be useful, see e.g. random_free_port() in >>> Podman tests (/test/system/helpers.network.bash). We should print the >>> port number that was bound, though, and document the feature. >>> >>> More than that: that could actually be the only race-free possibility >>> of picking and forwarding a port where the number doesn't matter. >> Yes it could be useful for podman but then it should also work with udp. > We can get it to work if needed. We would need, I guess: > > - that getsockname() for UDP, whatever is missing for the UDP case > > - a new configuration sub-option > > - documentation > >> I am less worried about the tests, this issue is in podman proper as you >> can do "-p 80", then podman assigns a free host port. Except that this >> is super broken in podman because we do this once when we create the >> container so this is totally racy and non conflict free[1]. > Ouch, I wasn't aware of that. For pasta it should be relatively easy to > do that in a race-free way, because the kernel guarantees that. > >> The thing of >> course is for podman we have to deal with like 4 other port forwarder >> implementations that we would need to support. As such I don't see us >> ever finding time to properly fix it unless it magically gets a ton of >> priority. So if pasta does not support for it I have no problems with >> that, however maybe one day we like to reconsider. >> >> [1] >> https://github.com/containers/podman/issues/10205#issuecomment-1010055023 > I would wait for David's feedback on this, but to me it looks like a > small-ish thing we can add without much thinking and planning. > > I'm not sure you can close that issue if we implement it in pasta as > long as forwarding is done like it's done now for custom networks, but > the issue would look less serious I guess. > > I don't know about the Podman side of it, but probably that would look > trivial to you (-t any:80 maybe? or -t :80 ?). I guess something like -t 0:80 could also work. That would allow me to store unassigned ports as 0 and the the convert to pasta cli code would not need any special handling for this case. Overall it will be more complicated in Podman which is why I never bothered to take this on. The issue is that commands like podman inspect or podman port need to know the actual ports that were assigned. So it would need some form of interface from pasta that prints out which host port it uses for each namespace port. Then podman must need to gain support to store two different set of ports, dynamic and static (currently) so we can keep track of the ports pasta returned to us. All stuff we can implement but until someone pushes hard for it I don't think it will ever make it. I think there are better network things we can spend our time on. -- Paul Holzinger