Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ipsec: Safely delete Xfrm state #32450

Merged

Conversation

jschwinger233
Copy link
Member

@jschwinger233 jschwinger233 commented May 10, 2024

Commit messages elaborate details. Although this is to fix an upgrade issue from 1.13, it doesn't harm main.

If the cilium is upgraded from an old version, it's possible a latest version cilium cluster has the old xfrm states left over. In this case, this PR performs unnecessary xfrm churn, but won't affect any traffic.

If the cilium is installed from scratch, it indeed doesn't need this fix at all. In this situation, the SafelyDeleteXfrmState() doesn't even cause xfrm churn because it can't find the old style xfrm state to delete.

This can be removed along with https://github.com/cilium/cilium/blob/v1.15.4/pkg/datapath/linux/ipsec/ipsec_linux.go#L351.

@maintainer-s-little-helper maintainer-s-little-helper bot added the dont-merge/needs-release-note-label The author needs to describe the release impact of these changes. label May 10, 2024
@jschwinger233 jschwinger233 force-pushed the gray/non-atomic-delete-ipsec-sa branch 2 times, most recently from 4a47d10 to 6ba68e9 Compare May 10, 2024 04:50
@jschwinger233
Copy link
Member Author

/test

No functional change, just to make further revision easier.

Signed-off-by: gray <[email protected]>
@jschwinger233 jschwinger233 force-pushed the gray/non-atomic-delete-ipsec-sa branch from 6ba68e9 to 30f969e Compare May 10, 2024 11:12
@jschwinger233 jschwinger233 added the release-note/bug This PR fixes an issue in a previous release of Cilium. label May 10, 2024
@maintainer-s-little-helper maintainer-s-little-helper bot removed the dont-merge/needs-release-note-label The author needs to describe the release impact of these changes. label May 10, 2024
@jschwinger233 jschwinger233 added sig/datapath Impacts bpf/ or low-level forwarding details, including map management and monitor messages. area/encryption Impacts encryption support such as IPSec, WireGuard, or kTLS. sig/agent Cilium agent related. feature/ipsec Relates to Cilium's IPsec feature labels May 10, 2024
@jschwinger233
Copy link
Member Author

/test

@jschwinger233 jschwinger233 added upgrade-impact This PR has potential upgrade or downgrade impact. needs-backport/1.13 This PR / issue needs backporting to the v1.13 branch needs-backport/1.14 This PR / issue needs backporting to the v1.14 branch needs-backport/1.15 This PR / issue needs backporting to the v1.15 branch labels May 10, 2024
@maintainer-s-little-helper maintainer-s-little-helper bot added this to Needs backport from main in 1.15.5 May 10, 2024
@maintainer-s-little-helper maintainer-s-little-helper bot added this to Needs backport from main in 1.14.11 May 10, 2024
@nebril nebril added this to Needs backport from main in 1.14.12 May 10, 2024
@nebril nebril removed this from Needs backport from main in 1.14.11 May 10, 2024
@nebril nebril added this to Needs backport from main in 1.15.6 May 10, 2024
@nebril nebril removed this from Needs backport from main in 1.15.5 May 10, 2024
@julianwiedmann julianwiedmann added the kind/bug This is a bug in the Cilium logic. label May 10, 2024
@jschwinger233 jschwinger233 marked this pull request as ready for review May 13, 2024 03:22
@jschwinger233 jschwinger233 requested a review from a team as a code owner May 13, 2024 03:22
@jschwinger233 jschwinger233 force-pushed the gray/non-atomic-delete-ipsec-sa branch from 30f969e to 1eb9e26 Compare May 14, 2024 04:08
@jschwinger233 jschwinger233 force-pushed the gray/non-atomic-delete-ipsec-sa branch from 1eb9e26 to 71f722f Compare May 14, 2024 04:11
@jschwinger233 jschwinger233 requested a review from rgo3 May 14, 2024 04:17
@jschwinger233
Copy link
Member Author

/test

@jschwinger233 jschwinger233 force-pushed the gray/non-atomic-delete-ipsec-sa branch from 71f722f to a7903f4 Compare May 15, 2024 09:50
@jschwinger233
Copy link
Member Author

/test

This patch introduces a workaround to avoid kernel issue when deleting
xfrm states.

Let's start from the kernel issue.

After installing two xfrm states on the same host using below commands
(please note the differences on the mark, mask, src):

```
ip x s a src 10.244.1.43 dst 10.244.3.114 proto esp spi 0x00000003 reqid 1 mode tunnel replay-window 0 mark 0x2a450d00 mask 0xffff0f00 output-mark 0xd00 mask 0xffffff00 aead 'rfc4106(gcm(aes))' 0x42a89014074c49243219a20cc87cadd7b9c0d7d1 128 sel src 0.0.0.0/0 dst 0.0.0.0/0
ip x s a src 0.0.0.0 dst 10.244.3.114 proto esp spi 0x00000003 reqid 1 mode tunnel replay-window 0 mark 0xd00 mask 0xf00 output-mark 0xd00 mask 0xffffff00 aead 'rfc4106(gcm(aes))' 0x42a89014074c49243219a20cc87cadd7b9c0d7d1 128 sel src 0.0.0.0/0 dst 0.0.0.0/0
```

When trying to delete the first xfrm state using the following command,
Linux kernel will instead remove the second xfrm state but keep the
first:

```
ip x s d src 10.244.1.43 dst 10.244.3.114 proto esp spi 0x00000003 mark 0x2a450d00 mask 0xffff0f00
```

This causes troubles for cilium upgrade.

A real world scenario for cilium upgrade from 1.13.12 to 1.13.14 could
be like:
1. Before upgrade, the node has "old-style" xfrm state to catch mark
   "0xd00/0xf00" for ingress traffic; old bpf programs also set "0xd00"
   mark to ingress skbs;
2. Upgrade begins, bpf programs are reloaded to new version, thereafter
   ingress skbs are marked with "0xXXXX0d00";
3. After a short while, cilium-agent installs new xfrm states to catch
   traffic with specific mark "0xXXXX0d00";

During window between step 2 and 3, cilium relies on "old-style" xfrm
states "0xd00/0xf00" to catch traffic with specific mark "0xXXXX0d00".

So far so good.

However, in a large scale cluster it's inevitable to receive
NodeDeletion events during upgrade due to node churn. Once seeing a
NodeDeletion event, cilium-agent will remove the xfrm state for that
gone-away remote node.

Now we hit the aforementioned kernel issue: cilium-agent tries to delete
the xfrm state catching more specific mark, but kernel wrongly removes
the one catching general mark.

This causing traffic disruption until upgrade completes with all new xfrm
states installed.

This patch provides an elegant solution at low cost: if cilium-agent
wants to remove a xfrm state catching specific mark, it has to
temporarily remove the xfrm state catching general mark first and add it
back after:
1. Temporarily remove the xfrm states catching the general mark;
2. Remove the xfrm state we really care abot;
3. Add back the temporaily removed one on step 1;

Indeed there will be a small window between temporary removing and
adding back, but our past test shows the window lasts 200-900µs only, so short
that we shoudn't see many drops.

Suggested-by: Julian Wiedmann <[email protected]>
Signed-off-by: gray <[email protected]>
@jschwinger233 jschwinger233 force-pushed the gray/non-atomic-delete-ipsec-sa branch from a7903f4 to af478a8 Compare May 15, 2024 10:48
@jschwinger233
Copy link
Member Author

/test

@julianwiedmann julianwiedmann self-requested a review May 16, 2024 08:19
Copy link
Member

@pchaigno pchaigno left a comment

Choose a reason for hiding this comment

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

😭

Thanks Gray! Neat pull request!

pkg/datapath/linux/ipsec/ipsec_linux.go Show resolved Hide resolved
pkg/datapath/linux/ipsec/ipsec_linux.go Show resolved Hide resolved
@maintainer-s-little-helper maintainer-s-little-helper bot added the ready-to-merge This PR has passed all tests and received consensus from code owners to merge. label May 16, 2024
@jschwinger233 jschwinger233 removed the upgrade-impact This PR has potential upgrade or downgrade impact. label May 17, 2024
@julianwiedmann julianwiedmann added this pull request to the merge queue May 17, 2024
Merged via the queue into cilium:main with commit 9a68ec2 May 17, 2024
64 checks passed
@jschwinger233 jschwinger233 deleted the gray/non-atomic-delete-ipsec-sa branch May 17, 2024 07:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/encryption Impacts encryption support such as IPSec, WireGuard, or kTLS. feature/ipsec Relates to Cilium's IPsec feature kind/bug This is a bug in the Cilium logic. needs-backport/1.13 This PR / issue needs backporting to the v1.13 branch needs-backport/1.14 This PR / issue needs backporting to the v1.14 branch needs-backport/1.15 This PR / issue needs backporting to the v1.15 branch ready-to-merge This PR has passed all tests and received consensus from code owners to merge. release-note/bug This PR fixes an issue in a previous release of Cilium. sig/agent Cilium agent related. sig/datapath Impacts bpf/ or low-level forwarding details, including map management and monitor messages.
Projects
1.14.12
Needs backport from main
1.15.6
Needs backport from main
Development

Successfully merging this pull request may close these issues.

None yet

4 participants