Skip to contentSkip to conversation

netfilter: nf_tables_offload: clean offload things when the device unregister

PATCH Superseded [email protected] 2 participants 6 messages Sep 4, 2019 – Sep 5, 2019

Subsystems (2)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 cover netfilter: nf_tables_offload: clean offload things when the device unregister

From: wenxu <[email protected]>

This series clean the offload things for both chain and rules when the related device unregister

This version add a netdev notify to clean this things in the nft_offload_init

wenxu (3):
netfilter: nf_offload: refactor the nft_flow_offload_chain function
netfilter: nf_offload: refactor the nft_flow_offload_rule function
netfilter: nf_offload: clean offload things when the device unregister

 include/net/netfilter/nf_tables_offload.h |   2 +-
 net/netfilter/nf_tables_api.c             |   9 ++-
 net/netfilter/nf_tables_offload.c         | 110 +++++++++++++++++++++++++-----
 3 files changed, 102 insertions(+), 19 deletions(-)
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH v2 1/3 netfilter: nf_offload: refactor the nft_flow_offload_chain function

From: wenxu <[email protected]>

Refactor nft_flow_offload_chain and make it more common

---
 net/netfilter/nf_tables_offload.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)
net/netfilter/nf_tables_offload.c +15 -8
index 3f49fe8..9419486 100644
@@ -273,10 +273,9 @@ static int nft_indr_block_offload_cmd(struct nft_base_chain *chain,
 
 #define FLOW_SETUP_BLOCK TC_SETUP_BLOCK
 
-static int nft_flow_offload_chain(struct nft_trans *trans,
+static int nft_flow_offload_chain(struct nft_chain *chain,
 				  enum flow_block_command cmd)
 {
-	struct nft_chain *chain = trans->ctx.chain;
 	struct nft_base_chain *basechain;
 	struct net_device *dev;
 
@@ -288,16 +287,24 @@ static int nft_flow_offload_chain(struct nft_trans *trans,
 	if (!dev)
 		return -EOPNOTSUPP;
 
+	if (dev->netdev_ops->ndo_setup_tc)
+		return nft_block_offload_cmd(basechain, dev, cmd);
+	else
+		return nft_indr_block_offload_cmd(basechain, dev, cmd);
+}
+
+static int nft_flow_offload_chain_commit(struct nft_trans *trans,
+					 enum flow_block_command cmd)
+{
+	struct nft_chain *chain = trans->ctx.chain;
+
 	/* Only default policy to accept is supported for now. */
 	if (cmd == FLOW_BLOCK_BIND &&
 	    nft_trans_chain_policy(trans) != -1 &&
 	    nft_trans_chain_policy(trans) != NF_ACCEPT)
 		return -EOPNOTSUPP;
 
-	if (dev->netdev_ops->ndo_setup_tc)
-		return nft_block_offload_cmd(basechain, dev, cmd);
-	else
-		return nft_indr_block_offload_cmd(basechain, dev, cmd);
+	return nft_flow_offload_chain(chain, cmd);
 }
 
 int nft_flow_rule_offload_commit(struct net *net)
@@ -314,13 +321,13 @@ int nft_flow_rule_offload_commit(struct net *net)
 			if (!(trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD))
 				continue;
 
-			err = nft_flow_offload_chain(trans, FLOW_BLOCK_BIND);
+			err = nft_flow_offload_chain_commit(trans, FLOW_BLOCK_BIND);
 			break;
 		case NFT_MSG_DELCHAIN:
 			if (!(trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD))
 				continue;
 
-			err = nft_flow_offload_chain(trans, FLOW_BLOCK_UNBIND);
+			err = nft_flow_offload_chain_commit(trans, FLOW_BLOCK_UNBIND);
 			break;
 		case NFT_MSG_NEWRULE:
 			if (!(trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD))
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH v2 2/3 netfilter: nf_offload: refactor the nft_flow_offload_rule function

In reply to [email protected]

From: wenxu <[email protected]>

Refactor nft_flow_offload_rule and make it more common

---
 net/netfilter/nf_tables_offload.c | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)
net/netfilter/nf_tables_offload.c +17 -7
index 9419486..9657001 100644
@@ -134,20 +134,20 @@ int nft_chain_offload_priority(struct nft_base_chain *basechain)
 	return 0;
 }
 
-static int nft_flow_offload_rule(struct nft_trans *trans,
+static int nft_flow_offload_rule(struct nft_chain *chain,
+				 struct nft_rule *rule,
+				 struct nft_flow_rule *flow,
 				 enum flow_cls_command command)
 {
-	struct nft_flow_rule *flow = nft_trans_flow_rule(trans);
-	struct nft_rule *rule = nft_trans_rule(trans);
 	struct flow_cls_offload cls_flow = {};
 	struct nft_base_chain *basechain;
 	struct netlink_ext_ack extack;
 	__be16 proto = ETH_P_ALL;
 
-	if (!nft_is_base_chain(trans->ctx.chain))
+	if (!nft_is_base_chain(chain))
 		return -EOPNOTSUPP;
 
-	basechain = nft_base_chain(trans->ctx.chain);
+	basechain = nft_base_chain(chain);
 
 	if (flow)
 		proto = flow->proto;
@@ -162,6 +162,16 @@ static int nft_flow_offload_rule(struct nft_trans *trans,
 	return nft_setup_cb_call(basechain, TC_SETUP_CLSFLOWER, &cls_flow);
 }
 
+static int nft_flow_offload_rule_commit(struct nft_trans *trans,
+					enum flow_cls_command command)
+{
+	struct nft_flow_rule *flow = nft_trans_flow_rule(trans);
+	struct nft_rule *rule = nft_trans_rule(trans);
+	struct nft_chain *chain = trans->ctx.chain;
+
+	return nft_flow_offload_rule(chain, rule, flow, command);
+}
+
 static int nft_flow_offload_bind(struct flow_block_offload *bo,
 				 struct nft_base_chain *basechain)
 {
@@ -337,14 +347,14 @@ int nft_flow_rule_offload_commit(struct net *net)
 			    !(trans->ctx.flags & NLM_F_APPEND))
 				return -EOPNOTSUPP;
 
-			err = nft_flow_offload_rule(trans, FLOW_CLS_REPLACE);
+			err = nft_flow_offload_rule_commit(trans, FLOW_CLS_REPLACE);
 			nft_flow_rule_destroy(nft_trans_flow_rule(trans));
 			break;
 		case NFT_MSG_DELRULE:
 			if (!(trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD))
 				continue;
 
-			err = nft_flow_offload_rule(trans, FLOW_CLS_DESTROY);
+			err = nft_flow_offload_rule_commit(trans, FLOW_CLS_DESTROY);
 			break;
 		}
 
Signature · 1 line
1.8.3.1
#3 lore raw

PATCH v2 3/3 netfilter: nf_tables_offload: clean offload things when the device unregister

In reply to [email protected]

From: wenxu <[email protected]>

When the net_device unregister, the netdevice_notifier will release the related netdev basedchain and rules in this chains. So it is also need to clean the offload things before the chain is destroy.

---
 include/net/netfilter/nf_tables_offload.h |  2 +-
 net/netfilter/nf_tables_api.c             |  9 ++++-
 net/netfilter/nf_tables_offload.c         | 63 ++++++++++++++++++++++++++++++-
 3 files changed, 70 insertions(+), 4 deletions(-)
include/net/netfilter/nf_tables_offload.h +1 -1
index 643152f..acb6621 100644
@@ -80,7 +80,7 @@ void nft_indr_block_get_and_ing_cmd(struct net_device *dev,
 
 int nft_chain_offload_priority(struct nft_base_chain *basechain);
 
-void nft_offload_init(void);
+int nft_offload_init(void);
 void nft_offload_exit(void);
 
 #endif
net/netfilter/nf_tables_api.c +7 -2
index a3d7e82..3c64b32 100644
@@ -7691,15 +7691,20 @@ static int __init nf_tables_module_init(void)
 	if (err < 0)
 		goto err4;
 
+	err = nft_offload_init();
+	if (err < 0)
+		goto err5;
+
 	/* must be last */
 	err = nfnetlink_subsys_register(&nf_tables_subsys);
 	if (err < 0)
-		goto err5;
+		goto err6;
 
 	nft_chain_route_init();
-	nft_offload_init();
 
 	return err;
+err6:
+	nft_offload_exit();
 err5:
 	rhltable_destroy(&nft_objname_ht);
 err4:
net/netfilter/nf_tables_offload.c +62 -1

79 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#4 lore raw

Thanks for working on this.

Quoted 42 lines · On Wed, Sep 04, 2019 at 03:07:31PM +0800, [email protected] wrote:
net/netfilter/nf_tables_offload.c +34 -0
index 9657001..9fa3bdb 100644
@@ -396,17 +396,78 @@ static void nft_indr_block_cb(struct net_device *dev,
 	mutex_unlock(&net->nft.commit_mutex);
 }
 
+static void nft_offload_chain_clean(struct nft_chain *chain)
+{
+	struct nft_rule *rule;
+
+	list_for_each_entry(rule, &chain->rules, list) {
+		nft_flow_offload_rule(chain, rule,
+				      NULL, FLOW_CLS_DESTROY);
+	}
+
+	nft_flow_offload_chain(chain, FLOW_BLOCK_UNBIND);
+}
+
+static int nft_offload_netdev_event(struct notifier_block *this,
+				    unsigned long event, void *ptr)
+{
+	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
+	struct nft_base_chain *basechain;
+	struct net *net = dev_net(dev);
+	struct nft_table *table;
+	struct nft_chain *chain;
+
+	if (event != NETDEV_UNREGISTER)
+		return NOTIFY_DONE;
+
+	mutex_lock(&net->nft.commit_mutex);
+	list_for_each_entry(table, &net->nft.tables, list) {
+		if (table->family != NFPROTO_NETDEV)
+			continue;
+
+		list_for_each_entry(chain, &table->chains, list) {
+			if (!nft_is_base_chain(chain) ||
+			    !(chain->flags & NFT_CHAIN_HW_OFFLOAD))
+				continue;
+

net/netfilter/nf_tables_offload.c static void nft_indr_block_cb(struct net_device *dev, · line 442

Quoted 10 lines
net/netfilter/nf_tables_offload.c +10 -0
+			basechain = nft_base_chain(chain);
+			if (strncmp(basechain->dev_name, dev->name, IFNAMSIZ))
+				continue;
+
+			nft_offload_chain_clean(chain);
+			mutex_unlock(&net->nft.commit_mutex);
+			return NOTIFY_DONE;
+		}
+	}
+	mutex_unlock(&net->nft.commit_mutex);

This code around the mutex look very similar to nft_block_indr_cb(), could you consolidate this? Probably something like nft_offload_netdev_iterate() and add a callback.

net/netfilter/nf_tables_offload.c static void nft_indr_block_cb(struct net_device *dev, · line 453

Quoted 11 lines
net/netfilter/nf_tables_offload.c +5 -1
+	return NOTIFY_DONE;
+}
+
 static struct flow_indr_block_ing_entry block_ing_entry = {
 	.cb	= nft_indr_block_cb,
 	.list	= LIST_HEAD_INIT(block_ing_entry.list),
 };
 
-void nft_offload_init(void)
+static struct notifier_block nft_offload_netdev_notifier = {
+	.notifier_call	= nft_offload_netdev_event,

No need for priority because of registration order, right?

author
#5 lore raw
Quoted 50 lines · Pablo Neira Ayuso

Thanks for working on this.

Quoted 47 lines · On Wed, Sep 04, 2019 at 03:07:31PM +0800, [email protected] wrote:
net/netfilter/nf_tables_offload.c +39 -0
index 9657001..9fa3bdb 100644
@@ -396,17 +396,78 @@ static void nft_indr_block_cb(struct net_device *dev,
 	mutex_unlock(&net->nft.commit_mutex);
 }
 
+static void nft_offload_chain_clean(struct nft_chain *chain)
+{
+	struct nft_rule *rule;
+
+	list_for_each_entry(rule, &chain->rules, list) {
+		nft_flow_offload_rule(chain, rule,
+				      NULL, FLOW_CLS_DESTROY);
+	}
+
+	nft_flow_offload_chain(chain, FLOW_BLOCK_UNBIND);
+}
+
+static int nft_offload_netdev_event(struct notifier_block *this,
+				    unsigned long event, void *ptr)
+{
+	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
+	struct nft_base_chain *basechain;
+	struct net *net = dev_net(dev);
+	struct nft_table *table;
+	struct nft_chain *chain;
+
+	if (event != NETDEV_UNREGISTER)
+		return NOTIFY_DONE;
+
+	mutex_lock(&net->nft.commit_mutex);
+	list_for_each_entry(table, &net->nft.tables, list) {
+		if (table->family != NFPROTO_NETDEV)
+			continue;
+
+		list_for_each_entry(chain, &table->chains, list) {
+			if (!nft_is_base_chain(chain) ||
+			    !(chain->flags & NFT_CHAIN_HW_OFFLOAD))
+				continue;
+
+			basechain = nft_base_chain(chain);
+			if (strncmp(basechain->dev_name, dev->name, IFNAMSIZ))
+				continue;
+
+			nft_offload_chain_clean(chain);
Quoted 8 lines

net/netfilter/nf_tables_offload.c static void nft_indr_block_cb(struct net_device *dev, · line 442

Quoted 5 lines
net/netfilter/nf_tables_offload.c +5 -0
+			mutex_unlock(&net->nft.commit_mutex);
+			return NOTIFY_DONE;
+		}
+	}
+	mutex_unlock(&net->nft.commit_mutex);

This code around the mutex look very similar to nft_block_indr_cb(), could you consolidate this? Probably something like nft_offload_netdev_iterate() and add a callback.

nft_offload_netdev_iterate is a good idear, But maybe it can't provide a common callback. The nft_offload_netdev_iterate(without mutex) can return the chain amke each function(with mutex) use it

Quoted 9 lines
Quoted 9 lines
net/netfilter/nf_tables_offload.c +3 -0
+	return NOTIFY_DONE;
+}
+
 static struct flow_indr_block_ing_entry block_ing_entry = {
 	.cb	= nft_indr_block_cb,
 	.list	= LIST_HEAD_INIT(block_ing_entry.list),
 };
 
Quoted 4 lines

net/netfilter/nf_tables_offload.c static void nft_indr_block_cb(struct net_device *dev, · line 453

Quoted 3 lines
net/netfilter/nf_tables_offload.c +2 -1
-void nft_offload_init(void)
+static struct notifier_block nft_offload_netdev_notifier = {
+	.notifier_call	= nft_offload_netdev_event,

No need for priority because of registration order, right?

yes,  nft_offload_init will early than nft_chain_filter mod init

Quoted 1 line