Skip to content

bridge: batch firewall updates for published ports - #54011

Merged
vvoland merged 1 commit into
moby:masterfrom
akasakariko:fix/53466-batch-port-firewall
Oct 26, 2026
Merged

vvoland merged 1 commit into
moby:masterfrom
akasakariko:fix/53466-batch-port-firewall

Conversation

@akasakariko

@akasakariko akasakariko commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Fixes: #53466

Summary

Collect all port bindings before updating the firewall, allowing nftables to apply one transaction per address family instead of one per port group

Keep grouped host-port allocation and start userland proxies only after the firewall batch is installed

Extend the existing port-mapping tests to check batching and cleanup after firewall or proxy failures, including simultaneous proxy-stop and firewall-cleanup errors

AI assistance: ChatGPT in pi investigated the issue, drafted the implementation, regression tests and review revisions, and ran local validation

Validation

Validated in a privileged Colima Linux/arm64 container using Go 1.26.10 and nftables 1.1.3

  • The regression assertion fails against the original implementation with two firewall calls instead of one
  • The extended cleanup regression fails before the review fix because the firewall error hides proxy-stop errors, and passes with errors.Join
  • Related bridge, firewall backend, port mapper, allocator, registry and types tests pass
  • The bridge and nftables tests also pass with the libnftables build tag
  • Bridge race tests, golangci-lint 2.14.0, go vet, formatting and Linux/amd64 compilation pass
  • Concurrent test variants initially collided on the fixed network namespace used by TestCreateVeth, then ten isolated repetitions and the full libnftables rerun passed

A local runtime probe with 1000 existing IPv4 port bindings measured the actual nft transactions used by the bridge port-mapping path

New bindings Transactions before Transactions after Time before Time after
10 10 1 499 ms 64 ms
50 50 1 2523 ms 72 ms
200 200 1 10977 ms 84 ms

These are single-run port-mapping measurements, not complete container startup timings

The full daemon integration suite has not been run

Release notes (optional)

Reduce nftables port-publishing overhead by batching firewall updates per endpoint and address family

Created with: ChatGPT (pi)

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Cross-backend firewall and proxy rollback sequencing warrants maintainer review, particularly with the full daemon integration suite not run.

No open issues 🎉

What changed in this PR

This PR addresses #53466 by batching bridge port-publishing firewall updates, reducing nftables ruleset rewrites from one per port group to one per address family.

Changes:

  • Preserve grouped host-port allocation while collecting bindings for a single firewall update.
  • Start userland proxies after firewall installation and separate resource cleanup from firewall rollback.
  • Extend regression tests to verify batching and cleanup after firewall or proxy failures.
File Description
daemon/​libnetwork/​drivers/​bridge/​port_mapping_linux.go Batch firewall updates and reorganize allocation, proxy startup, and rollback.
daemon/​libnetwork/​drivers/​bridge/​port_mapping_linux_test.go Check firewall call counts, failure cleanup, and host-port reuse.

Review effort: Balanced

@robmry robmry 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.

Thank you @akasakariko - batching the firewall update looks good to me.

Could you drop the second commit (libnetwork: isolate TestDNSOptions and retry flaky TestAllocator)? Those CI failures are unrelated to this change, and master has been passing. Renaming TestAllocator to TestFlakyAllocator would hide a nil pointer panic in that test, so it needs looking into separately rather than retrying. The TestDNSOptions fix looks reasonable, but in a separate PR.

Allocate all requested port bindings before applying their firewall rules, so nftables rewrites each affected chain once per address family rather than once per port group

Keep grouped port allocation, start proxies after the firewall update, and roll back resources and installed rules if either stage fails

Extend the port-mapping tests to check a single firewall update and verify cleanup and rebinding after setup failures

Signed-off-by: akasakariko <[email protected]>
@akasakariko
akasakariko force-pushed the fix/53466-batch-port-firewall branch from faa2268 to e83b503 Compare October 26, 2026 15:34
@akasakariko

Copy link
Copy Markdown
Contributor Author

Thanks for the review

Removed the unrelated DNS and allocator commit and folded the requested changes into e83b503

  • Added the comment explaining the possible nftables rollback warning
  • Used errors.Join to preserve both port cleanup and firewall cleanup errors
  • Extended the existing release-failure test and confirmed it fails without the fix

Related package tests, the libnftables variant, bridge race tests, lint, vet, formatting and Linux/amd64 compilation pass locally in Colima

The full daemon integration suite has not been run

@robmry robmry added the kind/bugfix PR's that fix bugs label Oct 26, 2026

@robmry robmry 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.

LGTM - thank you @akasakariko.

@thaJeztah thaJeztah added this to the 29.10.0 milestone Oct 26, 2026
@vvoland
vvoland merged commit 4237bc6 into moby:master Oct 26, 2026
339 of 349 checks passed
akasakariko added a commit to akasakariko/akasakariko that referenced this pull request Oct 26, 2026
@vvoland

vvoland commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

Hi @akasakariko are you interested in opening a follow up PR with the TestDNSOptions changes from faa2268 ?

@akasakariko

Copy link
Copy Markdown
Contributor Author

Of course, happy to open a follow-up PR with the TestDNSOptions changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

5 participants