Skip to content

support eip ping eip#6114

Closed
zbb88888 wants to merge 2 commits intokubeovn:masterfrom
zbb88888:fix-nat-gw-fip-not-ping-snat
Closed

support eip ping eip#6114
zbb88888 wants to merge 2 commits intokubeovn:masterfrom
zbb88888:fix-nat-gw-fip-not-ping-snat

Conversation

@zbb88888
Copy link
Copy Markdown
Collaborator

@zbb88888 zbb88888 commented Dec 29, 2025

Pull Request

What type of this PR

Examples of user facing changes:

  • Features
add_snat(eip,cidr) 
  ├─ 添加 SHARED_SNAT(幂等:已存在则跳过)
  └─ 如果 cidr == vpcCidr → add_hairpin_snat(eip,cidr)
                              ├─ 相同规则存在 → 跳过
                              ├─ 不同规则存在 → 警告并跳过
                              └─ 无规则 → 添加

del_snat(eip,cidr)
  ├─ 删除 SHARED_SNAT(幂等:不存在则跳过)
  └─ 如果 cidr == vpcCidr → del_hairpin_snat(eip,cidr)
                              ├─ 规则存在 → 删除
                              └─ 规则不存在 → 跳过

公网场景使用 vpc-nat-gw 只允许绑定一个子网,也只允许路由通一个子网。(vpc-nat-gw controller 需要简化 vpc 内其他路由的逻辑)

  • 出网的时候 policy route reroute 只能到本网段

Which issue(s) this PR fixes

Fixes #(issue-number)

#5572 (comment)

@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Dec 29, 2025
@zbb88888 zbb88888 marked this pull request as draft December 29, 2025 14:40
@dosubot dosubot bot added feature New network feature vpc labels Dec 29, 2025
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @zbb88888, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the network address translation capabilities of the vpc-nat-gw by introducing hairpin SNAT. This feature addresses a common networking challenge where virtual machines within the same Virtual Private Cloud (VPC) might fail to establish connections to other internal VMs that are exposed through Floating IPs (FIPs) and EIPs. By ensuring that traffic originating and terminating within the VPC but routed through an EIP is correctly source-NATed back to the EIP, it prevents asymmetric routing issues and enables seamless internal communication.

Highlights

  • Hairpin SNAT Introduction: Introduced hairpin SNAT functionality to allow internal Virtual Machines (VMs) to access other internal VMs' Floating IPs (FIPs) via External IPs (EIPs) within the same Virtual Private Cloud (VPC).
  • New Commands and Chains: Added hairpin-snat-add and hairpin-snat-del commands for managing hairpin SNAT rules, along with a new HAIRPIN_SNAT iptables chain.
  • Automatic Hairpin SNAT Management: Integrated automatic hairpin SNAT rule creation and deletion within the add_snat and del_snat functions. These rules are triggered when a shared SNAT rule's internal CIDR matches the VPC's internal CIDR.
  • VPC CIDR Detection: Added a get_vpc_cidr helper function to dynamically determine the VPC's internal network CIDR from the VPC_INTERFACE's directly connected route.
  • Hairpin SNAT Rule Limitation: Enforced a limitation allowing only one active hairpin SNAT rule at a time, with warnings issued if an attempt is made to add a different rule when one already exists.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

这个 PR 增加了对 SNAT EIP 和 FIP EIP 之间 hairpin 模式的支持,允许 VPC 内的 Pod 通过 EIP 地址访问其他 Pod。实现方式是引入了一个新的 HAIRPIN_SNAT iptables 链,并在 add_snatdel_snat 中增加了相应的逻辑来管理 hairpin 规则。整体实现逻辑清晰,注释也很详尽。我的审查意见主要集中在提高 shell 脚本的健壮性上。目前的实现多处使用了 iptables-save | grep 的方式来检查规则是否存在,这种方式比较脆弱,容易因为 iptables-save 输出格式的变化而出错。我建议改用 iptables -C 命令来检查规则,这样更可靠,也更符合 iptables 的最佳实践。

@zbb88888 zbb88888 force-pushed the fix-nat-gw-fip-not-ping-snat branch from 56ecfa5 to 4038d82 Compare December 30, 2025 12:01
@zbb88888 zbb88888 marked this pull request as ready for review December 31, 2025 03:21
@zbb88888
Copy link
Copy Markdown
Collaborator Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

本次 PR 增加了对 Hairpin SNAT 的支持,以允许 VPC 内部的虚拟机通过其浮动 IP(FIP)互相访问。主要变更集中在 nat-gateway.sh 脚本中,通过引入新的 HAIRPIN_SNAT iptables 链,并在为内部 CIDR 管理 SNAT 规则时自动添加/删除 Hairpin 规则。整体逻辑清晰,注释详尽。我的审查意见主要集中在提高检查现有 iptables 规则的健壮性,以防止潜在的匹配错误。

@zbb88888 zbb88888 force-pushed the fix-nat-gw-fip-not-ping-snat branch from 4038d82 to 1913851 Compare December 31, 2025 04:04
@zbb88888
Copy link
Copy Markdown
Collaborator Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces support for hairpin SNAT, enabling internal services to communicate via their external IPs. The changes in nat-gateway.sh are well-implemented, adding new functions to manage hairpin rules and integrating them into the existing SNAT logic. The approach to detect internal CIDRs and conditionally add hairpin rules is sound. I have one suggestion to improve the performance of the add_hairpin_snat function by reducing redundant iptables-save calls.

@zbb88888 zbb88888 force-pushed the fix-nat-gw-fip-not-ping-snat branch from 1913851 to b6a1e6c Compare December 31, 2025 04:55
Signed-off-by: zbb88888 <jmdxjsjgcxy@gmail.com>
@zbb88888 zbb88888 force-pushed the fix-nat-gw-fip-not-ping-snat branch from b6a1e6c to 16472bc Compare December 31, 2025 05:14
@zbb88888
Copy link
Copy Markdown
Collaborator Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces hairpin SNAT functionality to the nat-gateway.sh script, enabling internal VMs to access other internal VMs via Floating IPs. Key changes include adding hairpin-snat-add and hairpin-snat-del commands, creating a new HAIRPIN_SNAT iptables chain, and implementing is_internal_cidr to determine if a SNAT rule requires hairpin SNAT. The add_snat and del_snat functions were updated to conditionally manage hairpin SNAT rules. The review comments suggest a performance optimization for both add_hairpin_snat and del_hairpin_snat functions, recommending that the iptables-save command be executed once outside the processing loop to cache results and avoid redundant calls for each rule.

@zbb88888
Copy link
Copy Markdown
Collaborator Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces support for hairpin SNAT in the VPC NAT gateway, enabling internal VMs to access services on other internal VMs via their external IPs (EIP/FIP). The modifications in nat-gateway.sh correctly implement the hairpin logic by adding a new HAIRPIN_SNAT iptables chain and associated rules. The approach to automatically manage hairpin rules alongside SNAT rules is well-designed. The e2e tests in e2e_test.go have been appropriately expanded to validate this new feature. My review has identified a minor robustness issue in the shell script and a bug within the new e2e tests that should be addressed.

@zbb88888 zbb88888 force-pushed the fix-nat-gw-fip-not-ping-snat branch from eb9ef16 to 671026f Compare December 31, 2025 07:51
@zbb88888
Copy link
Copy Markdown
Collaborator Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

此 PR 增加了对 hairpin SNAT 的支持,允许内部虚拟机通过其外部 IP (EIP/FIP) 相互访问。这是通过在 nat-gateway.sh 中添加一个新的 HAIRPIN_SNAT iptables 链和相关逻辑来实现的,当管理内部 CIDR 的 SNAT 规则时,会自动添加/删除 hairpin 规则。代码变更结构清晰,并包含了相应的 E2E 测试来验证新功能。

我的审查主要集中在提高 shell 脚本的健壮性和修复新 E2E 测试中的一个错误。总的来说,这是一次很棒的更新。


// Verify hairpin SNAT rule is created for shared SNAT as well
ginkgo.By("Verifying hairpin SNAT rule exists for shared snat")
verifyHairpinSnatRule(vpcNatGwPodName, overlaySubnetV4Cidr, shareEip.Spec.V4ip, true)
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.

critical

这里的 shareEip 变量引用的是用于创建的初始对象,其中 Spec.V4ip 是空的,因为 IP 是动态分配的。测试应该在验证 hairpin 规则之前获取更新后的 IptablesEIP 对象,以从其 status 中获取已分配的 IP。使用 shareEip.Spec.V4ip 会传递一个空的 EIP 字符串,导致测试失败。

Suggested change
verifyHairpinSnatRule(vpcNatGwPodName, overlaySubnetV4Cidr, shareEip.Spec.V4ip, true)
verifyHairpinSnatRule(vpcNatGwPodName, overlaySubnetV4Cidr, iptablesEIPClient.Get(sharedEipName).Status.IP, true)

@zbb88888 zbb88888 force-pushed the fix-nat-gw-fip-not-ping-snat branch from 671026f to 902316f Compare December 31, 2025 08:58
Signed-off-by: zbb88888 <jmdxjsjgcxy@gmail.com>
@zbb88888 zbb88888 force-pushed the fix-nat-gw-fip-not-ping-snat branch from 902316f to 5273855 Compare December 31, 2025 09:11
@zbb88888
Copy link
Copy Markdown
Collaborator Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request adds support for hairpin SNAT, which is crucial for enabling communication between internal VMs using their external Floating IPs. The changes involve modifications to the nat-gateway.sh script to manage hairpin iptables rules and additions to the E2E tests to verify this new functionality. The implementation is mostly solid, and the tests are comprehensive. I've identified one area for improvement in the shell script to make rule deletion more robust. Overall, this is a great addition that addresses an important networking scenario.

@SkalaNetworks
Copy link
Copy Markdown
Member

I think this would be an interesting feature to have

@zbb88888
Copy link
Copy Markdown
Collaborator Author

I think this would be an interesting feature to have

@SkalaNetworks I will finish it, too busy recently!

@zbb88888 zbb88888 marked this pull request as draft February 13, 2026 03:22
@zbb88888
Copy link
Copy Markdown
Collaborator Author

  Issue #5572 问题分析

  场景:
  - VM A (10.0.1.6) 配置 SNAT 访问外网,EIP 为 10.1.69.219
  - VM B (10.0.1.11) 绑定 FIP,EIP 为 10.1.69.216
  - VM A 和 VM B 在同一内网 10.0.1.0/24

  问题:VM A 无法 ping VM B 的 FIP (10.1.69.216)

  根因分析:
  VM A (10.0.1.6) ---> FIP EIP (10.1.69.216)
                            |
                      NAT GW DNAT
                            ↓
                      dst: 10.0.1.11 (但 src 还是 10.0.1.6)
                            |
                      VM B 收到包,发现源是同子网
                            ↓
                      直接回包给 10.0.1.6 (绕过 NAT GW)
                            |
                      VM A 收到来自 10.0.1.11 的包,但期望来自 10.1.69.216
                            ↓
                      连接失败(不对称路由)

  解决方案:添加 hairpin SNAT 规则
  iptables -t nat -A HAIRPIN_SNAT -s 10.0.1.0/24 -d 10.0.1.0/24 -j SNAT --to-source <SNAT_EIP>

  这样源地址被改成 EIP,VM B 回包时会发往 EIP,强制经过 NAT GW,conntrack 自动完成反向转换。

  ---
  重新评估当前实现

  1. 耦合问题:hairpin 逻辑嵌入在 add_snat/del_snat 里,如果将来要单独管理 hairpin 规则会比较麻烦
  2. 多 SNAT 场景:如果同一个 internalCIDR 有多个 SNAT 规则(不同 EIP),当前实现只会用第一个 EIP 创建 hairpin 规则,删除时也只删除对应的那条
  3. 与 FIP 的关系:实际上 hairpin 只在有 FIP 的时候才需要,但当前实现是只要 SNAT 的 CIDR 是内网就添加

@zbb88888 zbb88888 changed the title support snat eip ping fip eip support eip ping eip Feb 24, 2026
@zbb88888 zbb88888 closed this Feb 24, 2026
@zbb88888 zbb88888 deleted the fix-nat-gw-fip-not-ping-snat branch April 5, 2026 06:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New network feature size:L This PR changes 100-499 lines, ignoring generated files. vpc

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants