Skip to content

Resolving communication issue between pods after enabling IPsec#1496

Merged
kmesh-bot merged 7 commits intokmesh-net:mainfrom
zrggw:ipsec_issue
Sep 3, 2025
Merged

Resolving communication issue between pods after enabling IPsec#1496
kmesh-bot merged 7 commits intokmesh-net:mainfrom
zrggw:ipsec_issue

Conversation

@zrggw
Copy link
Copy Markdown
Contributor

@zrggw zrggw commented Sep 2, 2025

What type of PR is this?
/kind bug

What this PR does / why we need it:
Refactored traffic decrypt marking logic.
Added OutputMark configuration in ipsec_handler.go

If the protocol is ESP, it directly enters the decryption phase to match the xfrm state and then re-enters ingress. If the packet is successfully decrypted, it will be marked as 0xd0. If the packet is not ESP protocol and has not been decrypted(mark != 0xd0), its mark will be set to 0x0, ensuring it does not match any policy and avoiding the issue described previously.
Which issue(s) this PR fixes:
Fixes #1481

Special notes for your reviewer:

Does this PR introduce a user-facing change?:


Signed-off-by: aicee <hhbin2000@foxmail.com>
Signed-off-by: aicee <hhbin2000@foxmail.com>
Signed-off-by: aicee <hhbin2000@foxmail.com>
Copilot AI review requested due to automatic review settings September 2, 2025 08:19
@kmesh-bot kmesh-bot added the kind/bug Something isn't working label Sep 2, 2025
Copy link
Copy Markdown

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

Summary of Changes

Hello @zrggw, 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 addresses a critical bug where inter-pod communication breaks down after IPsec is enabled. The core issue stemmed from how packets were handled post-decryption. By refining the BPF logic that processes traffic and explicitly marking decrypted packets, the system can now correctly route traffic that has undergone IPsec decryption, preventing packets from being dropped or misrouted.

Highlights

  • Traffic Decryption Logic Refinement: Refactored the BPF traffic decryption marking logic to ensure correct handling of packets after IPsec decryption.
  • Explicit Packet Marking: Introduced explicit marking for successfully decrypted packets (0xd0) and un-decrypted packets (0x0) within the BPF program, preventing misrouting.
  • IPsec State Configuration: Configured the IPsec state creation to apply an output mark (0xd0) upon successful decryption, aligning with the BPF logic.
  • Communication Issue Resolution: Resolved communication failures between pods when IPsec is enabled by ensuring proper packet flow and policy matching post-decryption.
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

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 issue 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.


Encrypted stream, Marked and sent to its true path, Pods now speak freely.

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

Copilot AI left a comment

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 resolves a communication issue between pods after enabling IPsec by refactoring the traffic decrypt marking logic. The changes implement a new approach where ESP packets directly enter decryption and successfully decrypted packets are marked with 0xd0, while unencrypted packets get mark 0x0 to avoid matching policies.

  • Refactored traffic decrypt marking logic in the BPF program to handle ESP protocol and decrypted packets differently
  • Added OutputMark configuration to XFRM state creation for proper packet marking after decryption

Reviewed Changes

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

File Description
pkg/controller/encryption/ipsec/ipsec_handler.go Adds OutputMark configuration to XFRM state with value 0x00d0
bpf/kmesh/general/tc_mark_decrypt.c Refactors marking logic to handle ESP packets and check decryption status

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines 21 to 28
}
nodeinfo = check_remote_manage_by_kmesh(ctx, &info, info.iph->saddr, info.ip6h->saddr.s6_addr32);
if (!nodeinfo) {
if (is_ipv4(&info)) {
protocol = info.iph->protocol;
} else if (is_ipv6(&info)) {
protocol = info.ip6h->nexthdr;
} else {
return TC_ACT_OK;
}
Copy link

Copilot AI Sep 2, 2025

Choose a reason for hiding this comment

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

The removed nodeinfo check logic appears to be replaced without equivalent validation. Consider if the remote node management check is still needed or if this creates a security gap.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown

@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 refactors the traffic decryption marking logic to resolve a communication issue between pods with IPsec enabled. It introduces an OutputMark on the XFRM state to mark decrypted packets and updates the tc_mark_decrypt BPF program to correctly handle these packets and reset marks for other traffic.

While the approach seems correct, I have identified a few areas for improvement:

  • Broken Unit Test: The changes in bpf/kmesh/general/tc_mark_decrypt.c will likely cause the unit test in test/bpf_ut/tc_mark_decrypt_test.c to fail. The test still asserts the old behavior where ctx->mark is set to 0x00d0, whereas the new logic sets it to 0 for the tested scenario. It is critical to update the unit tests to match the new logic and ensure correctness.
  • Magic Numbers: The value 0x00d0 is used as a magic number in both the Go code (ipsec_handler.go) and the BPF C code (tc_mark_decrypt.c). This makes the code hard to maintain and prone to errors. This value should be defined as a named constant and shared appropriately. I've left specific comments on this.

Signed-off-by: aicee <hhbin2000@foxmail.com>
@kmesh-bot kmesh-bot added size/M and removed size/S labels Sep 2, 2025
Signed-off-by: aicee <hhbin2000@foxmail.com>
@kmesh-bot kmesh-bot added size/L and removed size/M labels Sep 3, 2025
Signed-off-by: aicee <hhbin2000@foxmail.com>
@codecov
Copy link
Copy Markdown

codecov bot commented Sep 3, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 38.27%. Comparing base (89a29e7) to head (bf64ca4).
⚠️ Report is 64 commits behind head on main.

Files with missing lines Coverage Δ
pkg/controller/encryption/ipsec/ipsec_handler.go 42.90% <100.00%> (+0.84%) ⬆️

... and 7 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 73ea840...bf64ca4. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

if (parser_tc_info(ctx, &info)) {
return TC_ACT_OK;
}
nodeinfo = check_remote_manage_by_kmesh(ctx, &info, info.iph->saddr, info.ip6h->saddr.s6_addr32);
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.

You need to remove the related bpf map as well

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

bpf map still need to be used in tc_mark_encrypt.c

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.

got it

Signed-off-by: aicee <hhbin2000@foxmail.com>
@hzxuzhonghu
Copy link
Copy Markdown
Member

/assign @bitcoffeeiux

@bitcoffeeiux
Copy link
Copy Markdown
Contributor

/lgtm

@kmesh-bot kmesh-bot added the lgtm label Sep 3, 2025
@hzxuzhonghu
Copy link
Copy Markdown
Member

/approve

@kmesh-bot
Copy link
Copy Markdown
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: hzxuzhonghu

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kmesh-bot kmesh-bot merged commit 098968a into kmesh-net:main Sep 3, 2025
12 checks passed
@zrggw zrggw deleted the ipsec_issue branch September 5, 2025 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Some issues encountered in writing Ipsec E2E tests

5 participants