Skip to content

fix(connlib): don't buffer when recreating DNS resource NAT - #8935

Merged
thomaseizinger merged 3 commits into
mainfrom
fix/no-buffer-packets-recreating-dns-resource
Apr 30, 2025
Merged

fix(connlib): don't buffer when recreating DNS resource NAT#8935
thomaseizinger merged 3 commits into
mainfrom
fix/no-buffer-packets-recreating-dns-resource

Conversation

@thomaseizinger

@thomaseizinger thomaseizinger commented Apr 30, 2025

Copy link
Copy Markdown
Member

In order to detect changes to DNS records of DNS resources, connlib will recreate the DNS resource NAT whenever it receives a query for a DNS resource. The way we implemented this was by clearing the local state of the DNS resource NAT, which triggered us to perform the handshake with the Gateway again upon the next packet for this resource. The Gateway would then perform the DNS query and respond back when this was finished.

In order to not drop any packets, connlib has a buffer where it keeps the packets that are arriving in the meantime. This works reasonably well when the connection is first set up because we are only buffering a TCP SYN or equivalent handshake packet. Yet, when the connection is full use, and the application just so happens to make another DNS query, we halt the entire flow of packets until this is confirmed again. To prevent high memory use, the buffer for this packets is constrained to 32 packets which is nowhere near enough when a connection is actively transferring data (like a file upload).

In most cases, the DNS query on the Gateway will yield the exact same results as because the records haven't changed. Thus, there is no reason for us to actually halt the flow of these packets when we are recreating the DNS resource NAT. That way, this handshake happens in parallel to the actual packet flow and does not interrupt anything in the happy path case.

@vercel

vercel Bot commented Apr 30, 2025

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
firezone ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 30, 2025 3:55am

@thomaseizinger
thomaseizinger force-pushed the fix/no-buffer-packets-recreating-dns-resource branch from 6fa0d51 to b5270f1 Compare April 30, 2025 03:47
@thomaseizinger
thomaseizinger requested a review from jamilbk April 30, 2025 03:49
@thomaseizinger
thomaseizinger marked this pull request as ready for review April 30, 2025 03:49
Copilot AI review requested due to automatic review settings April 30, 2025 03:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR improves DNS resource NAT handling by allowing DNS handshakes to occur in parallel with packet flow when recreating a DNS resource NAT, thereby reducing packet buffering delays. Key changes include:

  • Adding telemetry for tracking dropped packets due to full buffers.
  • Introducing a new DNS NAT state "Recreating" and a new is_recreating flag.
  • Refactoring DNS resource NAT recreation logic in the client.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
rust/connlib/tunnel/src/unique_packet_buffer.rs Enhanced logging and telemetry tracking when buffering packets.
rust/connlib/tunnel/src/client.rs Added new state and flag for DNS NAT recreation as well as updating DNS NAT handling methods.

Comment thread rust/connlib/tunnel/src/client.rs Outdated
@thomaseizinger

Copy link
Copy Markdown
Member Author

@jamilbk Let me know what you think of the changelog entry, I figured it is important enough to include but wasn't quite sure how to best word this.

@jamilbk jamilbk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting find. I'm sure this fixes an edge case or two.

///
/// We model the [`DnsResourceNatState::Recreating`] state differently from just removing the entry to allow packets
/// to continue flowing to the Gateway while the DNS resource NAT is being recreated.
/// In most cases, the DNS records will not change and as such, performing this will not interrupt the flow of packets.

@jamilbk jamilbk Apr 30, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to confirm, does musl's dns on the gateway always return the same IP if there are multiple A or AAAA records for a domain? If not would that cause anything to break here?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These codepaths don't affect Mac/Windows?

@thomaseizinger
thomaseizinger added this pull request to the merge queue Apr 30, 2025
Merged via the queue into main with commit 6dc5f85 Apr 30, 2025
@thomaseizinger
thomaseizinger deleted the fix/no-buffer-packets-recreating-dns-resource branch April 30, 2025 04:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants