Skip to content

Conversation

@hongliangl
Copy link
Contributor

@hongliangl hongliangl commented Jun 19, 2025

This commit adds support of Antrea Egress in hybrid mode. The key changes include:

  • IPTables rules added in table mangle, chains ANTREA-PREROUTING and ANTREA-POSTROUTING

    # Set connmark for the first packets of connections originated remote Pods.
    -A ANTREA-PREROUTING ! -s 10.244.0.0/24 -i antrea-gw0 -m comment --comment "Antrea: set connmark for the first Egress request packet" -m conntrack --ctstate NEW -m mark ! --mark 0x0/0xff -j CONNMARK --set-xmark 0x40000000/0x40000000
    
    # Restore fwmark from connmark for reply packets of connections originated from remote Pods.
    -A ANTREA-PREROUTING -m comment --comment "Antrea: restore fwmark from connmark for reply packets sourced from remote Pods" -m conntrack --ctstate ESTABLISHED -m conntrack --ctdir REPLY -m connmark --mark 0x40000000/0x40000000 -j CONNMARK --restore-mark --nfmask 0x40000000 --ctmask 0x40000000
    
    # Clear fwmark from reply packets of connections originated from remote Pods.
    -A ANTREA-POSTROUTING -m comment --comment "Antrea: clear fwmark from reply Egress packets to remote Pods" -m conntrack --ctstate ESTABLISHED -m conntrack --ctdir REPLY -j MARK --set-xmark 0x0/0x40000000
  • IP rule

    root@kind-control-plane:/# ip rule | grep 141
    32765:  from all fwmark 0x40000000/0x40000000 lookup 141
  • A default route in the policy-routing table 141 and its corresponding static ip neighbor
    are used to forward reply Egress packets back to OVS pipeline.

     root@kind-control-plane:/# ip route list table 141
     default via 169.254.0.251 dev antrea-gw0 onlink
    
     root@kind-control-plane:/# ip neigh | grep 169.254.0.251
     169.254.0.251 dev antrea-gw0 lladdr aa:bb:cc:dd:ee:ff PERMANENT
  • OVS flows

    # Encapsulate packets sourced from antrea-gw0 and destined to remote Pod CIDRs via tunnel.
    table=L3Forwarding, priority=201,ct_mark=0x1/0xf,ip,in_port=antrea-gw0, nw_dst=10.244.1.0/24 actions=set_field:ba:5e:d1:55:aa:c0->eth_src,set_field:aa:bb:cc:dd:ee:ff->eth_dst,set_field:172.18.0.3->tun_dst,set_field:0x10/0xf0->reg0,goto_table:L3DecTTL
    table=L3Forwarding, priority=201,ct_mark=0x1/0xf,ip,in_port=antrea-gw0, nw_dst=10.244.2.0/24 actions=set_field:ba:5e:d1:55:aa:c0->eth_src,set_field:aa:bb:cc:dd:ee:ff->eth_dst,set_field:172.18.0.4->tun_dst,set_field:0x10/0xf0->reg0,goto_table:L3DecTTL
    table=L3Forwarding, priority=201,ct_mark=0x1/0xf,ip,in_port=antrea-gw0, nw_dst=10.244.3.0/24 actions=set_field:ba:5e:d1:55:aa:c0->eth_src,set_field:aa:bb:cc:dd:ee:ff->eth_dst,set_field:172.18.0.5->tun_dst,set_field:0x10/0xf0->reg0,goto_table:L3DecTTL

The benchmark of hybrid vs encap of Egress traffic:

Pod Payload Encap Hybrid Hybrid/Encap
remotepodipv4-cluster TCP_STREAM 16724.83 15910.85 0.92
remotepodipv4-cluster TCP_RR 24005.69 24099.39 0.97
remotepodipv4-cluster TCP_CRR 3254.50 3254.88 1 1
localpodipv4-cluster TCP_STREAM 23728.62 22551.3 0.95
localpodipv4-cluster TCP_RR 29130.42 29629.38 1.01
localpodipv4-cluster TCP_CRR 3255.32 3254.93 1 1

Dependencies:

@hongliangl hongliangl force-pushed the egress-hybrid branch 3 times, most recently from 5bcb6c8 to 2f0eae9 Compare June 26, 2025 17:25
@hongliangl hongliangl marked this pull request as ready for review June 26, 2025 17:26
@hongliangl hongliangl added action/release-note Indicates a PR that should be included in release notes. area/transit/egress Issues or PRs related to Egress (SNAT for traffic egressing the cluster). labels Jun 26, 2025
@hongliangl hongliangl force-pushed the egress-hybrid branch 5 times, most recently from 61f8d42 to c36407b Compare June 30, 2025 09:22
@hongliangl hongliangl added this to the Antrea v2.5 release milestone Jul 11, 2025
@hongliangl hongliangl force-pushed the egress-hybrid branch 2 times, most recently from 15b17e8 to afbbf6d Compare July 24, 2025 15:43
@luolanzone
Copy link
Contributor

@hongliangl please check if patch ut coverage can be improved.

@hongliangl
Copy link
Contributor Author

@hongliangl please check if patch ut coverage can be improved.

Will check.

Cookie(f.cookieAllocator.Request(f.category).Raw()).
MatchProtocol(ipProtocol).
MatchRegMark(FromGatewayRegMark, FromExternalRegMark).
MatchPktMark(types.RemotePodSourceMark, &types.RemotePodSourceMark).
Copy link
Contributor

Choose a reason for hiding this comment

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

Why we need to matach a remote Pod mark here? Could not we just match egress traffic, since destination is already matched by MatchDstIPNet(peerSubnet)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is to exclude the request packets of external-to-Service (backend is a remote Pod) connections. I will add comment to this line to explain.

@hongliangl hongliangl force-pushed the egress-hybrid branch 2 times, most recently from acf1edf to ca06d56 Compare August 12, 2025 03:07
@hongliangl hongliangl force-pushed the egress-hybrid branch 3 times, most recently from b5210d6 to 507b3fe Compare August 12, 2025 09:29
Copy link
Contributor

@antoninbas antoninbas left a comment

Choose a reason for hiding this comment

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

some initial comments

Copy link
Contributor

@antoninbas antoninbas left a comment

Choose a reason for hiding this comment

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

Change looks good to me, I think @tnqn should review as well

@hongliangl hongliangl force-pushed the egress-hybrid branch 2 times, most recently from 24013fb to 9e0e31b Compare October 20, 2025 05:43
@hongliangl hongliangl requested a review from antoninbas October 20, 2025 05:53
@hongliangl hongliangl force-pushed the egress-hybrid branch 2 times, most recently from 383dfe9 to 64cb0d9 Compare October 24, 2025 07:07
@hongliangl hongliangl requested a review from tnqn October 29, 2025 02:28
@hongliangl hongliangl force-pushed the egress-hybrid branch 3 times, most recently from 46ec86a to 01e4486 Compare November 3, 2025 02:24
This commit adds support of Antrea Egress in hybrid mode. The key changes include:

- IPTables rules added in table `mangle`, chains `ANTREA-PREROUTING` and `ANTREA-POSTROUTING`
  ```shell
  # Set connmark for the first packets of connections originated remote Pods.
  -A ANTREA-PREROUTING ! -s 10.244.0.0/24 -i antrea-gw0 -m comment --comment "Antrea: set connmark for the first Egress request packet" -m conntrack --ctstate NEW -m mark ! --mark 0x0/0xff -j CONNMARK --set-xmark 0x40000000/0x40000000

  # Restore fwmark from connmark for reply packets of connections originated from remote Pods.
  -A ANTREA-PREROUTING -m comment --comment "Antrea: restore fwmark from connmark for reply packets sourced from remote Pods" -m conntrack --ctstate ESTABLISHED -m conntrack --ctdir REPLY -m connmark --mark 0x40000000/0x40000000 -j CONNMARK --restore-mark --nfmask 0x40000000 --ctmask 0x40000000

  # Clear fwmark from reply packets of connections originated from remote Pods.
  -A ANTREA-POSTROUTING -m comment --comment "Antrea: clear fwmark from reply Egress packets to remote Pods" -m conntrack --ctstate ESTABLISHED -m conntrack --ctdir REPLY -j MARK --set-xmark 0x0/0x40000000
    ```

- IP rule
  ```shell
  root@kind-control-plane:/# ip rule | grep 141
  32765:  from all fwmark 0x40000000/0x40000000 lookup 141
  ```

- A default route in the policy-routing table 141 and its corresponding static ip neighbor
  are used to forward reply Egress packets back to OVS pipeline.
  ```shell
   root@kind-control-plane:/# ip route list table 141
   default via 169.254.0.251 dev antrea-gw0 onlink

   root@kind-control-plane:/# ip neigh | grep 169.254.0.251
   169.254.0.251 dev antrea-gw0 lladdr aa:bb:cc:dd:ee:ff PERMANENT
  ```

- OVS flows
  ```shell
  # Encapsulate packets sourced from antrea-gw0 and destined to remote Pod CIDRs via tunnel.
  table=L3Forwarding, priority=201,ct_mark=0x1/0xf,ip,in_port=antrea-gw0, nw_dst=10.244.1.0/24 actions=set_field:ba:5e:d1:55:aa:c0->eth_src,set_field:aa:bb:cc:dd:ee:ff->eth_dst,set_field:172.18.0.3->tun_dst,set_field:0x10/0xf0->reg0,goto_table:L3DecTTL
  table=L3Forwarding, priority=201,ct_mark=0x1/0xf,ip,in_port=antrea-gw0, nw_dst=10.244.2.0/24 actions=set_field:ba:5e:d1:55:aa:c0->eth_src,set_field:aa:bb:cc:dd:ee:ff->eth_dst,set_field:172.18.0.4->tun_dst,set_field:0x10/0xf0->reg0,goto_table:L3DecTTL
  table=L3Forwarding, priority=201,ct_mark=0x1/0xf,ip,in_port=antrea-gw0, nw_dst=10.244.3.0/24 actions=set_field:ba:5e:d1:55:aa:c0->eth_src,set_field:aa:bb:cc:dd:ee:ff->eth_dst,set_field:172.18.0.5->tun_dst,set_field:0x10/0xf0->reg0,goto_table:L3DecTTL
  ```

Signed-off-by: Hongliang Liu <[email protected]>
Signed-off-by: Hongliang Liu <[email protected]>
Copy link
Member

@tnqn tnqn left a comment

Choose a reason for hiding this comment

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

LGTM

@hongliangl
Copy link
Contributor Author

/test-all

Copy link
Contributor

@antoninbas antoninbas left a comment

Choose a reason for hiding this comment

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

@hongliangl
Copy link
Contributor Author

hongliangl commented Nov 11, 2025

LGTM - but I did ask for a clarification in https://github.com/antrea-io/antrea/pull/7239/files#r2505192467, PTAL

@antoninbas Replied in #7239 (comment). If the performance loss is acceptable using a unified pkt_mark regardless of traffic modes, we can refine the implementation in a subsequent PR.

Created an issue #7554 to track that.

@antoninbas antoninbas merged commit 6a1997f into antrea-io:main Nov 12, 2025
69 of 72 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

action/release-note Indicates a PR that should be included in release notes. area/transit/egress Issues or PRs related to Egress (SNAT for traffic egressing the cluster).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants