Skip to content

Move SNAT/DNAT to raw table early_snat chain#6

Merged
kvaps merged 2 commits into
mainfrom
raw_table
Jun 19, 2025
Merged

Move SNAT/DNAT to raw table early_snat chain#6
kvaps merged 2 commits into
mainfrom
raw_table

Conversation

@kvaps
Copy link
Copy Markdown
Member

@kvaps kvaps commented Jun 19, 2025

Use:

        chain early_snat {
                type filter hook prerouting priority raw; policy accept;
                ip saddr set ip saddr map @pod_svc
                ip daddr set ip daddr map @svc_pod
        }

instead of

        chain raw_prerouting {
                type filter hook prerouting priority raw; policy accept;
                ip saddr @pod notrack return
                ip daddr @svc notrack return
        }

        chain nat_output {
                type filter hook postrouting priority srcnat + 1; policy accept;
                ip saddr set ip saddr map @pod_svc
        }

        chain nat_prerouting {
                type filter hook prerouting priority dstnat + 1; policy accept;
                ip daddr set ip daddr map @svc_pod
        }

Summary by CodeRabbit

  • Refactor
    • Simplified the network rule setup by consolidating multiple chains into a single chain for improved clarity.
    • Streamlined logging to reflect the updated chain structure.
    • Removed management of IP address sets to enhance rule processing efficiency.

Signed-off-by: Andrei Kvapil <kvapss@gmail.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jun 19, 2025

Walkthrough

The nftables rule initialization logic in the NFTProxyProcessor has been refactored. Multiple chains (raw_prerouting, nat_output, nat_prerouting) are replaced by a single early_snat chain, consolidating SNAT and DNAT rules into this chain. Management of podSet and svcSet IP sets and related notrack rules have been removed. Logging is updated accordingly, and no public APIs are changed.

Changes

File(s) Change Summary
pkg/proxy/nft.go Removed podSet and svcSet fields and all related set management; replaced three nftables chains with one early_snat chain; consolidated SNAT/DNAT rules; removed notrack rules; updated logging.

Poem

In the proxy’s warren, chains once three,
Now one “early_snat” hops with glee!
SNAT and DNAT, side by side,
In a single chain, they now reside.
No more rules to scatter or track—
Just one neat path, no looking back!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@kvaps kvaps changed the title raw table Move SNAT/DNAT to raw table early_snat chain Jun 19, 2025
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🔭 Outside diff range comments (1)
pkg/proxy/nft.go (1)

30-34: Remove unused struct fields.

The struct fields rawPrerouting, natOutput, and natPrerouting are no longer used after the refactoring to a single early_snat chain but remain in the struct definition.

 	// Chains.
-	rawPrerouting *nftables.Chain // Chain "raw_prerouting": for notrack rules.
-	natOutput     *nftables.Chain // Chain "nat_output": for SNAT rule.
-	natPrerouting *nftables.Chain // Chain "nat_prerouting": for DNAT rule.
+	earlySNAT     *nftables.Chain // Chain "early_snat": for SNAT and DNAT rules.
🧹 Nitpick comments (1)
pkg/proxy/nft.go (1)

322-322: Update outdated comment reference.

The comment references raw_prerouting chain which no longer exists after the refactoring.

-// This ensures that the raw_prerouting chain no longer matches these IPs.
+// This ensures that the early_snat chain no longer matches these IPs.
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f0ead2b and bc4faf0.

📒 Files selected for processing (1)
  • pkg/proxy/nft.go (3 hunks)
🔇 Additional comments (2)
pkg/proxy/nft.go (2)

133-194: LGTM: Rules correctly assigned to new chain.

The SNAT and DNAT rules are properly assigned to the new earlySNAT chain, and the logging has been updated appropriately.


123-130: Verify the architectural impact of consolidating chains.

The refactoring consolidates three separate chains into a single early_snat chain in the raw table. This removes the previous notrack functionality that was applied to pod and service IPs.

Please verify that removing the notrack rules doesn't break connection tracking behavior for the proxy functionality. The previous implementation had explicit notrack rules that are now absent.

#!/bin/bash
# Search for any references to notrack functionality or connection tracking requirements
rg -i "notrack|conntrack|connection.track" --type go

Signed-off-by: Andrei Kvapil <kvapss@gmail.com>
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bc4faf0 and d15fd91.

📒 Files selected for processing (1)
  • pkg/proxy/nft.go (4 hunks)
🔇 Additional comments (6)
pkg/proxy/nft.go (6)

102-132: SNAT rule implementation looks correct.

The SNAT rule correctly:

  • Reads the source IP from network header offset 12 (IPv4 source address)
  • Performs map lookup in pod_svc map
  • Writes the translated IP back to the source address field
  • Updates checksums appropriately

133-162: DNAT rule implementation looks correct.

The DNAT rule correctly:

  • Reads the destination IP from network header offset 16 (IPv4 destination address)
  • Performs map lookup in svc_pod map
  • Writes the translated IP back to the destination address field
  • Updates checksums appropriately

163-163: Logging message accurately reflects the consolidated approach.

The updated log message correctly indicates that both SNAT and DNAT rules are now added to the single early_snat chain.


268-268: Comment correctly updated to reflect removed raw set management.

The comment has been appropriately updated to remove references to the raw pod set that was eliminated in this refactoring.


310-310: Logging message correctly updated to reflect removed raw set management.

The log message has been appropriately updated to remove references to raw set elements that are no longer managed.


96-98: Verify the implications of using raw priority for NAT operations.

Using ChainPriorityRaw for NAT operations means the address translation happens before connection tracking. This could have several implications:

  1. Connection tracking won't see the original addresses
  2. Applications expecting to see original addresses may break
  3. Stateful firewalling based on original addresses won't work

Ensure this early processing aligns with your networking requirements.

#!/bin/bash
# Check if there are any existing iptables/nftables rules that might conflict
# or depend on seeing original addresses before NAT

echo "Checking for existing NAT rules..."
nft list ruleset 2>/dev/null | grep -A5 -B5 "nat\|snat\|dnat" || echo "No existing NAT rules found"

echo "Checking for connection tracking rules..."
nft list ruleset 2>/dev/null | grep -A5 -B5 "ct\|conntrack" || echo "No connection tracking rules found"

Comment thread pkg/proxy/nft.go
@kvaps kvaps merged commit dada50a into main Jun 19, 2025
2 checks passed
kvaps added a commit to cozystack/cozystack that referenced this pull request Jun 20, 2025
This PR includes the following change
cozystack/cozy-proxy#6

Which makes source-based-routing working with wholeIP services


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **Chores**
- Updated Helm chart and Docker image versions for cozy-proxy to v0.2.0.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
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.

1 participant