Skip to contentSkip to conversation

netfilter: nft_dynset: support for element deletion

PATCH In review Ander Juaristi 3 participants 5 messages Jul 13, 2019 – Aug 8, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 netfilter: nft_dynset: support for element deletion

This patch implements the delete operation from the ruleset.

It implements a new delete() function in nft_set_rhash. It is simpler to use than the already existing remove(), because it only takes the set and the key as arguments, whereas remove() expects a full nft_set_elem structure.

---
 include/net/netfilter/nf_tables.h        |  2 ++
 include/uapi/linux/netfilter/nf_tables.h |  1 +
 net/netfilter/nft_dynset.c               | 43 +++++++++++++++++-------
 net/netfilter/nft_set_hash.c             | 20 +++++++++++
 4 files changed, 53 insertions(+), 13 deletions(-)
include/net/netfilter/nf_tables.h +2 -0
index 9e8493aad49d..b24a3c8ec0a4 100644
@@ -312,6 +312,8 @@ struct nft_set_ops {
 						  const struct nft_expr *expr,
 						  struct nft_regs *regs,
 						  const struct nft_set_ext **ext);
+	bool				(*delete)(const struct nft_set *set,
+						  const u32 *key);
 
 	int				(*insert)(const struct net *net,
 						  const struct nft_set *set,
include/uapi/linux/netfilter/nf_tables.h +1 -0
index c6c8ec5c7c00..e8483e1e7146 100644
@@ -634,6 +634,7 @@ enum nft_lookup_attributes {
 enum nft_dynset_ops {
 	NFT_DYNSET_OP_ADD,
 	NFT_DYNSET_OP_UPDATE,
+	NFT_DYNSET_OP_DELETE,
 };
 
 enum nft_dynset_flags {
net/netfilter/nft_dynset.c +30 -13

73 lines not on this page · raw message

net/netfilter/nft_set_hash.c +20 -0
index 03df08801e28..aeb83dfa9583 100644
@@ -237,6 +237,25 @@ static void nft_rhash_remove(const struct net *net,
 	rhashtable_remove_fast(&priv->ht, &he->node, nft_rhash_params);
 }
 
+static bool nft_rhash_delete(const struct nft_set *set,
+			     const u32 *key)
+{
+	struct nft_rhash *priv = nft_set_priv(set);
+	struct nft_rhash_elem *he;
+	struct nft_rhash_cmp_arg arg = {
+		.genmask = NFT_GENMASK_ANY,
+		.set = set,
+		.key = key,
+	};
+
+	he = rhashtable_lookup(&priv->ht, &arg, nft_rhash_params);
+	if (he == NULL)
+		return false;
+
+	rhashtable_remove_fast(&priv->ht, &he->node, nft_rhash_params);
+	return true;
+}
+
 static void nft_rhash_walk(const struct nft_ctx *ctx, struct nft_set *set,
 			   struct nft_set_iter *iter)
 {
@@ -665,6 +684,7 @@ struct nft_set_type nft_set_rhash_type __read_mostly = {
 		.remove		= nft_rhash_remove,
 		.lookup		= nft_rhash_lookup,
 		.update		= nft_rhash_update,
+		.delete		= nft_rhash_delete,
 		.walk		= nft_rhash_walk,
 		.get		= nft_rhash_get,
 	},
Signature · 1 line
2.17.1
maintainer
#1 lore raw
Quoted 6 lines · Ander Juaristi

This patch implements the delete operation from the ruleset.

It implements a new delete() function in nft_set_rhash. It is simpler to use than the already existing remove(), because it only takes the set and the key as arguments, whereas remove() expects a full nft_set_elem structure.

Looks good, I plan to review all your posted patches on monday.

some nits below,

include/net/netfilter/nf_tables.h struct nft_set_ops { · line 316

Quoted 8 lines
include/net/netfilter/nf_tables.h +2 -0
@@ -312,6 +312,8 @@ struct nft_set_ops {
 						  const struct nft_expr *expr,
 						  struct nft_regs *regs,
 						  const struct nft_set_ext **ext);
+	bool				(*delete)(const struct nft_set *set,
+						  const u32 *key);

Can you add a small comment here that explains the functions at the start are those used on packet path, and rest are control plane/slow path ones?

include/net/netfilter/nf_tables.h struct nft_set_ops { · line 317

Quoted 1 line
include/net/netfilter/nf_tables.h +1 -0
+static bool nft_dynset_update(uint8_t sreg_key, int op, u64 timeout,

Can you use plain u8 here? In case you haven't seen it, there is scripts/checkpatch.pl in the kernel tree.

include/net/netfilter/nf_tables.h struct nft_set_ops {

Quoted 5 lines
include/net/netfilter/nf_tables.h +5 -0
+	if (he == NULL)
+		return false;
+
+	rhashtable_remove_fast(&priv->ht, &he->node, nft_rhash_params);
+	return true;

Perhaps add a small comment here that rhashtable_remove_fast retval is ignored intentionally?

I.e., don't make this return false in case two cpus race to remove same entry.

Otherwise, this looks really good, thanks for working on this!

#2 lore raw
Quoted 10 lines · Florian Westphal

net/netfilter/nft_set_hash.c static void nft_rhash_remove(const struct net *net, · line 256

Quoted 6 lines
net/netfilter/nft_set_hash.c +5 -0
+	if (he == NULL)
+		return false;
+
+	rhashtable_remove_fast(&priv->ht, &he->node, nft_rhash_params);
+	return true;

Perhaps add a small comment here that rhashtable_remove_fast retval is ignored intentionally?

Quoted 2 lines

I.e., don't make this return false in case two cpus race to remove same entry.

Hmm, this made me think. I don't know if this was all too intentional from me.

Maybe rather than ignoring it, it would be better to return true only if rhashtable_remove_fast returned 0, which will only happen if the element was actually deleted (locking is done internally so two cpus cannot race in there). Else, if return value is -ENOENT, we should return false.

And taking this reasoning further, maybe the initial call to rhashtable_lookup wouldn't be needed either?

WDYT?

maintainer
#3 lore raw
Quoted 17 lines · Ander Juaristi
Quoted 10 lines · Florian Westphal

net/netfilter/nft_set_hash.c static void nft_rhash_remove(const struct net *net, · line 256

Quoted 6 lines
net/netfilter/nft_set_hash.c +5 -0
+	if (he == NULL)
+		return false;
+
+	rhashtable_remove_fast(&priv->ht, &he->node, nft_rhash_params);
+	return true;

Perhaps add a small comment here that rhashtable_remove_fast retval is ignored intentionally?

Quoted 2 lines

I.e., don't make this return false in case two cpus race to remove same entry.

Hmm, this made me think. I don't know if this was all too intentional from me.

Quoted 7 lines

Maybe rather than ignoring it, it would be better to return true only if rhashtable_remove_fast returned 0, which will only happen if the element was actually deleted (locking is done internally so two cpus cannot race in there). Else, if return value is -ENOENT, we should return false.

And taking this reasoning further, maybe the initial call to rhashtable_lookup wouldn't be needed either?

You need it to obtain he->node, no?

Wrt. retval, I might be overthinking it indeed, so making this a "return rhashtable_remove_fast() == 0;" seems fine too, saves the comment :-)

#4 lore raw
In reply to Ander Juaristi
Thank you for the patch! Yet something to improve:

Hi Ander,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on linus/master]
[cannot apply to v5.3-rc3 next-20190808]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Ander-Juaristi/netfilter-nft_dynset-support-for-element-deletion/20190714-122358
config: x86_64-rhel-7.6 (attached as .config)
compiler: gcc-7 (Debian 7.4.0-10) 7.4.0 reproduce:

        # save the attached .config to linux build tree
        make ARCH=x86_64 

If you fix the issue, kindly add following tag

All errors (new ones prefixed by >>):

net/netfilter/nft_dynset.c: In function 'nft_dynset_eval':

>> net/netfilter/nft_dynset.c:115:2: error: expected ';' before '}' token
     } else {
     ^

vim +115 net/netfilter/nft_dynset.c

106
107 void nft_dynset_eval(const struct nft_expr *expr,
108 struct nft_regs *regs, const struct nft_pktinfo *pkt)
109 {
110 const struct nft_dynset *priv = nft_expr_priv(expr);
111 struct nft_set *set = priv->set;
112
113 if (priv->op == NFT_DYNSET_OP_DELETE) {
114 set->ops->delete(set, &regs->data[priv->sreg_key])

 > 115		} else {

116 if (nft_dynset_update(priv->sreg_key, priv->op, priv->timeout, expr, pkt, regs, set)) {
117 if (priv->invert)
118 regs->verdict.code = NFT_BREAK;
119 return;
120 }
121
122 if (!priv->invert)
123 regs->verdict.code = NFT_BREAK;
124 }
125 }
126

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation