Skip to contentSkip to conversation

net: eliminate the duplicate code in the ct nat functions of ovs and tc

PATCH Superseded Xin Long 2 participants 9 messages Dec 6, 2022 – Dec 8, 2022

Subsystems (5)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Xin Long

author
#0 lore raw

v3 cover net: eliminate the duplicate code in the ct nat functions of ovs and tc

The changes in the patchset:

"net: add helper support in tc act_ct for ovs offloading"

had moved some common ct code used by both OVS and TC into netfilter.

There are still some big functions pretty similar defined and used in each of OVS and TC. It is not good to maintain such big function in 2 places. This patchset is to extract the functions for NAT processing from OVS and TC to netfilter.

To make this change clear and safe, this patchset gets the common code out of OVS and TC step by step: The patch 1-4 make some minor changes in OVS and TC to make the NAT code of them completely the same, then the patch 5 moves the common code to the netfilter and exports one function called by each of OVS and TC.

v1->v2:
- Create nf_nat_ovs.c to include the nat functions, as Pablo suggested.
v2->v3:
- Fix a typo in subject of patch 2/5, as Marcelo noticed.
- Fix in openvswitch to keep OVS ct nat and TC ct nat consistent in patch 3/5 instead of in tc, as Marcelo noticed.
- Use BIT(var) macro instead of (1 << var) in patch 5/5, as Marcelo suggested.
- Use ifdef in netfilter/Makefile to build nf_nat_ovs only when OVS or TC ct action is enabled in patch 5/5, as Marcelo suggested.

Xin Long (5):
openvswitch: delete the unncessary skb_pull_rcsum call in
ovs_ct_nat_execute
openvswitch: return NF_ACCEPT when OVS_CT_NAT is not set in info nat
openvswitch: return NF_DROP when fails to add nat ext in ovs_ct_nat
net: sched: update the nat flag for icmp error packets in
ct_nat_execute
net: move the nat function to nf_nat_ovs for ovs and tc

 include/net/netfilter/nf_nat.h |   4 +
 net/netfilter/Makefile         |   6 ++
 net/netfilter/nf_nat_ovs.c     | 135 ++++++++++++++++++++++++++++++
 net/openvswitch/conntrack.c    | 146 +++------------------------------
 net/sched/act_ct.c             | 136 +++---------------------------
 5 files changed, 169 insertions(+), 258 deletions(-)
 create mode 100644 net/netfilter/nf_nat_ovs.c
Signature · 1 line
2.31.1
author
#1 lore raw

PATCH v3 1/5 openvswitch: delete the unncessary skb_pull_rcsum call in ovs_ct_nat_execute

The calls to ovs_ct_nat_execute() are as below:

ovs_ct_execute()

    ovs_ct_lookup()
      __ovs_ct_lookup()
        ovs_ct_nat()
          ovs_ct_nat_execute()
    ovs_ct_commit()
      __ovs_ct_lookup()
        ovs_ct_nat()
          ovs_ct_nat_execute()

and since skb_pull_rcsum() and skb_push_rcsum() are already called in ovs_ct_execute(), there's no need to do it again in ovs_ct_nat_execute().

---
 net/openvswitch/conntrack.c | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)
net/openvswitch/conntrack.c +6 -11
index d78f0fc4337d..dff093a10d6d 100644
@@ -735,10 +735,7 @@ static int ovs_ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct,
 			      const struct nf_nat_range2 *range,
 			      enum nf_nat_manip_type maniptype, struct sw_flow_key *key)
 {
-	int hooknum, nh_off, err = NF_ACCEPT;
-
-	nh_off = skb_network_offset(skb);
-	skb_pull_rcsum(skb, nh_off);
+	int hooknum, err = NF_ACCEPT;
 
 	/* See HOOK2MANIP(). */
 	if (maniptype == NF_NAT_MANIP_SRC)
@@ -755,7 +752,7 @@ static int ovs_ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct,
 			if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo,
 							   hooknum))
 				err = NF_DROP;
-			goto push;
+			goto out;
 		} else if (IS_ENABLED(CONFIG_IPV6) &&
 			   skb->protocol == htons(ETH_P_IPV6)) {
 			__be16 frag_off;
@@ -770,7 +767,7 @@ static int ovs_ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct,
 								     hooknum,
 								     hdrlen))
 					err = NF_DROP;
-				goto push;
+				goto out;
 			}
 		}
 		/* Non-ICMP, fall thru to initialize if needed. */
@@ -788,7 +785,7 @@ static int ovs_ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct,
 				? nf_nat_setup_info(ct, range, maniptype)
 				: nf_nat_alloc_null_binding(ct, hooknum);
 			if (err != NF_ACCEPT)
-				goto push;
+				goto out;
 		}
 		break;
 
@@ -798,13 +795,11 @@ static int ovs_ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct,
 
 	default:
 		err = NF_DROP;
-		goto push;
+		goto out;
 	}
 
 	err = nf_nat_packet(ct, ctinfo, hooknum, skb);
-push:
-	skb_push_rcsum(skb, nh_off);
-
+out:
 	/* Update the flow key if NAT successful. */
 	if (err == NF_ACCEPT)
 		ovs_nat_update_key(key, skb, maniptype);
Signature · 1 line
2.31.1
author
#2 lore raw

PATCH v3 2/5 openvswitch: return NF_ACCEPT when OVS_CT_NAT is not set in info nat

In reply to Xin Long

Either OVS_CT_SRC_NAT or OVS_CT_DST_NAT is set, OVS_CT_NAT must be set in info->nat. Thus, if OVS_CT_NAT is not set in info->nat, it will definitely not do NAT but returns NF_ACCEPT in ovs_ct_nat().

This patch changes nothing funcational but only makes this return earlier in ovs_ct_nat() to keep consistent with TC's processing in tcf_ct_act_nat().

---
 net/openvswitch/conntrack.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
net/openvswitch/conntrack.c +4 -2
index dff093a10d6d..5ea74270da46 100644
@@ -816,6 +816,9 @@ static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
 	enum nf_nat_manip_type maniptype;
 	int err;
 
+	if (!(info->nat & OVS_CT_NAT))
+		return NF_ACCEPT;
+
 	/* Add NAT extension if not confirmed yet. */
 	if (!nf_ct_is_confirmed(ct) && !nf_ct_nat_ext_add(ct))
 		return NF_ACCEPT;   /* Can't NAT. */
@@ -825,8 +828,7 @@ static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
 	 * Make sure new expected connections (IP_CT_RELATED) are NATted only
 	 * when committing.
 	 */
-	if (info->nat & OVS_CT_NAT && ctinfo != IP_CT_NEW &&
-	    ct->status & IPS_NAT_MASK &&
+	if (ctinfo != IP_CT_NEW && ct->status & IPS_NAT_MASK &&
 	    (ctinfo != IP_CT_RELATED || info->commit)) {
 		/* NAT an established or related connection like before. */
 		if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY)
Signature · 1 line
2.31.1
author
#3 lore raw

PATCH v3 3/5 openvswitch: return NF_DROP when fails to add nat ext in ovs_ct_nat

In reply to Xin Long

When it fails to allocate nat ext, the packet should be dropped, like the memory allocation failures in other places in ovs_ct_nat().

This patch changes to return NF_DROP when fails to add nat ext before doing NAT in ovs_ct_nat(), also it would keep consistent with tc action ct' processing in tcf_ct_act_nat().

---
 net/openvswitch/conntrack.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/openvswitch/conntrack.c +1 -1
index 5ea74270da46..58c9f0edc3c4 100644
@@ -821,7 +821,7 @@ static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
 
 	/* Add NAT extension if not confirmed yet. */
 	if (!nf_ct_is_confirmed(ct) && !nf_ct_nat_ext_add(ct))
-		return NF_ACCEPT;   /* Can't NAT. */
+		return NF_DROP;   /* Can't NAT. */
 
 	/* Determine NAT type.
 	 * Check if the NAT type can be deduced from the tracked connection.
Signature · 1 line
2.31.1
author
#4 lore raw

PATCH v3 4/5 net: sched: update the nat flag for icmp error packets in ct_nat_execute

In reply to Xin Long

In ovs_ct_nat_execute(), the packet flow key nat flags are updated when it processes ICMP(v6) error packets translation successfully.

In ct_nat_execute() when processing ICMP(v6) error packets translation successfully, it should have done the same in ct_nat_execute() to set post_ct_s/dnat flag, which will be used to update flow key nat flags in OVS module later.

---
 net/sched/act_ct.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/sched/act_ct.c +1 -1
index dd5ae7551956..bb87d1e910ea 100644
@@ -936,13 +936,13 @@ static int ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct,
 	}
 
 	err = nf_nat_packet(ct, ctinfo, hooknum, skb);
+out:
 	if (err == NF_ACCEPT) {
 		if (maniptype == NF_NAT_MANIP_SRC)
 			tc_skb_cb(skb)->post_ct_snat = 1;
 		if (maniptype == NF_NAT_MANIP_DST)
 			tc_skb_cb(skb)->post_ct_dnat = 1;
 	}
-out:
 	return err;
 }
 #endif /* CONFIG_NF_NAT */
Signature · 1 line
2.31.1
author
#5 lore raw

PATCH v3 5/5 net: move the nat function to nf_nat_ovs for ovs and tc

In reply to Xin Long

There are two nat functions are nearly the same in both OVS and TC code, (ovs_)ct_nat_execute() and ovs_ct_nat/tcf_ct_act_nat().

This patch creates nf_nat_ovs.c under netfilter and moves them there then exports nf_ct_nat() so that it can be shared by both OVS and TC, and keeps the nat (type) check and nat flag update in OVS and TC's own place, as these parts are different between OVS and TC.

Note that in OVS nat function it was using skb->protocol to get the proto as it already skips vlans in key_extract(), while it doesn't in TC, and TC has to call skb_protocol() to get proto. So in nf_ct_nat_execute(), we keep using skb_protocol() which works for both OVS and TC contrack.

---
 include/net/netfilter/nf_nat.h |   4 +
 net/netfilter/Makefile         |   6 ++
 net/netfilter/nf_nat_ovs.c     | 135 ++++++++++++++++++++++++++++++++
 net/openvswitch/conntrack.c    | 137 +++------------------------------
 net/sched/act_ct.c             | 136 +++-----------------------------
 5 files changed, 166 insertions(+), 252 deletions(-)
 create mode 100644 net/netfilter/nf_nat_ovs.c
include/net/netfilter/nf_nat.h +4 -0
index e9eb01e99d2f..9877f064548a 100644
@@ -104,6 +104,10 @@ unsigned int
 nf_nat_inet_fn(void *priv, struct sk_buff *skb,
 	       const struct nf_hook_state *state);
 
+int nf_ct_nat(struct sk_buff *skb, struct nf_conn *ct,
+	      enum ip_conntrack_info ctinfo, int *action,
+	      const struct nf_nat_range2 *range, bool commit);
+
 static inline int nf_nat_initialized(const struct nf_conn *ct,
 				     enum nf_nat_manip_type manip)
 {
net/netfilter/Makefile +6 -0
index 1d4db1943936..0976d34b1e5f 100644
@@ -54,6 +54,12 @@ obj-$(CONFIG_NF_CONNTRACK_TFTP) += nf_conntrack_tftp.o
 
 nf_nat-y	:= nf_nat_core.o nf_nat_proto.o nf_nat_helper.o
 
+ifdef CONFIG_OPENVSWITCH
+nf_nat-y += nf_nat_ovs.o
+else ifdef CONFIG_NET_ACT_CT
+nf_nat-y += nf_nat_ovs.o
+endif
+
 obj-$(CONFIG_NF_LOG_SYSLOG) += nf_log_syslog.o
 
 obj-$(CONFIG_NF_NAT) += nf_nat.o
net/netfilter/nf_nat_ovs.c new file +135 -0

135 lines not on this page · raw message

net/openvswitch/conntrack.c +10 -127

154 lines not on this page · raw message

net/sched/act_ct.c +11 -125

153 lines not on this page · raw message

Signature · 1 line
2.31.1
#6 lore raw

Re: [PATCHv3 net-next 5/5] net: move the nat function to nf_nat_ovs for ovs and tc

On Tue, Dec 06, 2022 at 06:31:16PM -0500, Xin Long wrote:
[...]

net/netfilter/Makefile obj-$(CONFIG_NF_CONNTRACK_TFTP) += nf_conntrack_tftp.o · line 61

Quoted 13 lines
net/netfilter/Makefile +5 -0
index 1d4db1943936..0976d34b1e5f 100644
@@ -54,6 +54,12 @@ obj-$(CONFIG_NF_CONNTRACK_TFTP) += nf_conntrack_tftp.o
 
 nf_nat-y	:= nf_nat_core.o nf_nat_proto.o nf_nat_helper.o
 
+ifdef CONFIG_OPENVSWITCH
+nf_nat-y += nf_nat_ovs.o
+else ifdef CONFIG_NET_ACT_CT
+nf_nat-y += nf_nat_ovs.o
+endif

Maybe add CONFIG_NF_NAT_OVS and select it from OPENVSWITCH Kconfig (select is a hammer, but it should be fine in this case since OPENVSWITCH already depends on NF_NAT?).

Then in Makefile:

nf_nat-$(CONFIG_NF_NAT_OVS) += nf_nat_ovs.o

And CONFIG_NF_NAT_OVS depends on OPENVSWITCH.

Quoted 3 lines
net/netfilter/Makefile +0 -0
 obj-$(CONFIG_NF_LOG_SYSLOG) += nf_log_syslog.o
 
 obj-$(CONFIG_NF_NAT) += nf_nat.o
author
#7 lore raw

Re: [PATCHv3 net-next 5/5] net: move the nat function to nf_nat_ovs for ovs and tc

Quoted 11 lines · Pablo Neira Ayuso

On Tue, Dec 06, 2022 at 06:31:16PM -0500, Xin Long wrote:
[...]

Quoted 8 lines
net/netfilter/Makefile +0 -0
index 1d4db1943936..0976d34b1e5f 100644
@@ -54,6 +54,12 @@ obj-$(CONFIG_NF_CONNTRACK_TFTP) += nf_conntrack_tftp.o
 
 nf_nat-y     := nf_nat_core.o nf_nat_proto.o nf_nat_helper.o
 
Quoted 9 lines

net/netfilter/Makefile obj-$(CONFIG_NF_CONNTRACK_TFTP) += nf_conntrack_tftp.o · line 61

Quoted 5 lines
net/netfilter/Makefile +5 -0
+ifdef CONFIG_OPENVSWITCH
+nf_nat-y += nf_nat_ovs.o
+else ifdef CONFIG_NET_ACT_CT
+nf_nat-y += nf_nat_ovs.o
+endif

Maybe add CONFIG_NF_NAT_OVS and select it from OPENVSWITCH Kconfig (select is a hammer, but it should be fine in this case since OPENVSWITCH already depends on NF_NAT?).

not really completely depends, it's:

  depends on (!NF_NAT || NF_NAT)

but it's fine, the select will be:

select NF_NAT_OVS if NF_NAT

Quoted 6 lines

Then in Makefile:

nf_nat-$(CONFIG_NF_NAT_OVS) += nf_nat_ovs.o

And CONFIG_NF_NAT_OVS depends on OPENVSWITCH.

Sounds great!
Then it will be:

net/netfilter/Kconfig +9 -3
@@ -459,6 +459,10 @@ config NF_NAT_REDIRECT
 config NF_NAT_MASQUERADE
        bool
 
+config NF_NAT_OVS
+       bool
+       depends on OPENVSWITCH || NET_ACT_CT
+
 
--- a/net/netfilter/Makefile
+++ b/net/netfilter/Makefile
@@ -59,6 +59,7 @@ obj-$(CONFIG_NF_LOG_SYSLOG) += nf_log_syslog.o
 obj-$(CONFIG_NF_NAT) += nf_nat.o
 nf_nat-$(CONFIG_NF_NAT_REDIRECT) += nf_nat_redirect.o
 nf_nat-$(CONFIG_NF_NAT_MASQUERADE) += nf_nat_masquerade.o
+nf_nat-$(CONFIG_NF_NAT_OVS)  += nf_nat_ovs.o
 
--- a/net/openvswitch/Kconfig
+++ b/net/openvswitch/Kconfig
@@ -15,6 +15,7 @@ config OPENVSWITCH
        select NET_MPLS_GSO
        select DST_CACHE
        select NET_NSH
+       select NF_NAT_OVS if NF_NAT
 
--- a/net/sched/Kconfig
+++ b/net/sched/Kconfig
@@ -977,6 +977,7 @@ config NET_ACT_TUNNEL_KEY
 config NET_ACT_CT
        tristate "connection tracking tc action"
        depends on NET_CLS_ACT && NF_CONNTRACK && (!NF_NAT || NF_NAT)

&& NF_FLOW_TABLE
+ select NF_NAT_OVS if NF_NAT

I will prepare v4, Thanks.

author
#8 lore raw

Re: [PATCHv3 net-next 5/5] net: move the nat function to nf_nat_ovs for ovs and tc

Quoted 42 lines · Xin Long
Quoted 11 lines · Pablo Neira Ayuso

On Tue, Dec 06, 2022 at 06:31:16PM -0500, Xin Long wrote:
[...]

Quoted 8 lines
net/netfilter/Makefile +0 -0
index 1d4db1943936..0976d34b1e5f 100644
@@ -54,6 +54,12 @@ obj-$(CONFIG_NF_CONNTRACK_TFTP) += nf_conntrack_tftp.o
 
 nf_nat-y     := nf_nat_core.o nf_nat_proto.o nf_nat_helper.o
 
Quoted 9 lines

net/netfilter/Makefile obj-$(CONFIG_NF_CONNTRACK_TFTP) += nf_conntrack_tftp.o · line 61

Quoted 5 lines
net/netfilter/Makefile +5 -0
+ifdef CONFIG_OPENVSWITCH
+nf_nat-y += nf_nat_ovs.o
+else ifdef CONFIG_NET_ACT_CT
+nf_nat-y += nf_nat_ovs.o
+endif

Maybe add CONFIG_NF_NAT_OVS and select it from OPENVSWITCH Kconfig (select is a hammer, but it should be fine in this case since OPENVSWITCH already depends on NF_NAT?).

not really completely depends, it's:

  depends on (!NF_NAT || NF_NAT)

but it's fine, the select will be:

select NF_NAT_OVS if NF_NAT

Quoted 6 lines

Then in Makefile:

nf_nat-$(CONFIG_NF_NAT_OVS) += nf_nat_ovs.o

And CONFIG_NF_NAT_OVS depends on OPENVSWITCH.

Sounds great!
Then it will be:

net/netfilter/Kconfig config NF_NAT_REDIRECT · line 465

Quoted 7 lines
net/netfilter/Kconfig +4 -0
@@ -459,6 +459,10 @@ config NF_NAT_REDIRECT
 config NF_NAT_MASQUERADE
        bool
 
+config NF_NAT_OVS
+       bool
+       depends on OPENVSWITCH || NET_ACT_CT
+

Just FYI, "depends on" is not necessary in this case. Even without this "depends on OPENVSWITCH || NET_ACT_CT", it will still be disabled automatically if OPENVSWITCH and NET_ACT_CT are disabled, and you can't enable it manually either.

Thanks.

Quoted 28 lines
net/netfilter/Makefile +1 -0
@@ -59,6 +59,7 @@ obj-$(CONFIG_NF_LOG_SYSLOG) += nf_log_syslog.o
 obj-$(CONFIG_NF_NAT) += nf_nat.o
 nf_nat-$(CONFIG_NF_NAT_REDIRECT) += nf_nat_redirect.o
 nf_nat-$(CONFIG_NF_NAT_MASQUERADE) += nf_nat_masquerade.o
+nf_nat-$(CONFIG_NF_NAT_OVS)  += nf_nat_ovs.o
 
net/openvswitch/Kconfig +1 -0
@@ -15,6 +15,7 @@ config OPENVSWITCH
        select NET_MPLS_GSO
        select DST_CACHE
        select NET_NSH
+       select NF_NAT_OVS if NF_NAT
 
net/sched/Kconfig +0 -0
@@ -977,6 +977,7 @@ config NET_ACT_TUNNEL_KEY
 config NET_ACT_CT
        tristate "connection tracking tc action"
        depends on NET_CLS_ACT && NF_CONNTRACK && (!NF_NAT || NF_NAT)

&& NF_FLOW_TABLE
+ select NF_NAT_OVS if NF_NAT

I will prepare v4, Thanks.