Netfilter-Devel Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH] netfilter: nft_dynset: support for element deletion
@ 2019-07-13 16:03 Ander Juaristi
  2019-07-13 16:59 ` Florian Westphal
  2019-08-08 20:41 ` kbuild test robot
  0 siblings, 2 replies; 5+ messages in thread
From: Ander Juaristi @ 2019-07-13 16:03 UTC (permalink / raw)
  To: netfilter-devel; +Cc: 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.

Signed-off-by: Ander Juaristi <a@juaristi.eus>
---
 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(-)

diff --git a/include/net/netfilter/nf_tables.h b/include/net/netfilter/nf_tables.h
index 9e8493aad49d..b24a3c8ec0a4 100644
--- a/include/net/netfilter/nf_tables.h
+++ b/include/net/netfilter/nf_tables.h
@@ -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,
diff --git a/include/uapi/linux/netfilter/nf_tables.h b/include/uapi/linux/netfilter/nf_tables.h
index c6c8ec5c7c00..e8483e1e7146 100644
--- a/include/uapi/linux/netfilter/nf_tables.h
+++ b/include/uapi/linux/netfilter/nf_tables.h
@@ -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 {
diff --git a/net/netfilter/nft_dynset.c b/net/netfilter/nft_dynset.c
index bfb9f7463b03..83b88ca656d4 100644
--- a/net/netfilter/nft_dynset.c
+++ b/net/netfilter/nft_dynset.c
@@ -79,37 +79,53 @@ static void *nft_dynset_new(struct nft_set *set, const struct nft_expr *expr,
 	return NULL;
 }
 
-void nft_dynset_eval(const struct nft_expr *expr,
-		     struct nft_regs *regs, const struct nft_pktinfo *pkt)
+static bool nft_dynset_update(uint8_t sreg_key, int op, u64 timeout,
+			      const struct nft_expr *expr,
+			      const struct nft_pktinfo *pkt,
+			      struct nft_regs *regs, struct nft_set *set)
 {
-	const struct nft_dynset *priv = nft_expr_priv(expr);
-	struct nft_set *set = priv->set;
 	const struct nft_set_ext *ext;
 	const struct nft_expr *sexpr;
-	u64 timeout;
 
-	if (set->ops->update(set, &regs->data[priv->sreg_key], nft_dynset_new,
+	if (set->ops->update(set, &regs->data[sreg_key], nft_dynset_new,
 			     expr, regs, &ext)) {
 		sexpr = NULL;
 		if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR))
 			sexpr = nft_set_ext_expr(ext);
 
-		if (priv->op == NFT_DYNSET_OP_UPDATE &&
+		if (op == NFT_DYNSET_OP_UPDATE &&
 		    nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
-			timeout = priv->timeout ? : set->timeout;
+			timeout = timeout ? : set->timeout;
 			*nft_set_ext_expiration(ext) = get_jiffies_64() + timeout;
 		}
 
 		if (sexpr != NULL)
 			sexpr->ops->eval(sexpr, regs, pkt);
 
-		if (priv->invert)
-			regs->verdict.code = NFT_BREAK;
-		return;
+		return true;
 	}
 
-	if (!priv->invert)
-		regs->verdict.code = NFT_BREAK;
+	return false;
+}
+
+void nft_dynset_eval(const struct nft_expr *expr,
+		     struct nft_regs *regs, const struct nft_pktinfo *pkt)
+{
+	const struct nft_dynset *priv = nft_expr_priv(expr);
+	struct nft_set *set = priv->set;
+
+	if (priv->op == NFT_DYNSET_OP_DELETE) {
+		set->ops->delete(set, &regs->data[priv->sreg_key])
+	} else {
+		if (nft_dynset_update(priv->sreg_key, priv->op, priv->timeout, expr, pkt, regs, set)) {
+			if (priv->invert)
+				regs->verdict.code = NFT_BREAK;
+			return;
+		}
+
+		if (!priv->invert)
+			regs->verdict.code = NFT_BREAK;
+	}
 }
 
 static const struct nla_policy nft_dynset_policy[NFTA_DYNSET_MAX + 1] = {
@@ -165,6 +181,7 @@ static int nft_dynset_init(const struct nft_ctx *ctx,
 	priv->op = ntohl(nla_get_be32(tb[NFTA_DYNSET_OP]));
 	switch (priv->op) {
 	case NFT_DYNSET_OP_ADD:
+	case NFT_DYNSET_OP_DELETE:
 		break;
 	case NFT_DYNSET_OP_UPDATE:
 		if (!(set->flags & NFT_SET_TIMEOUT))
diff --git a/net/netfilter/nft_set_hash.c b/net/netfilter/nft_set_hash.c
index 03df08801e28..aeb83dfa9583 100644
--- a/net/netfilter/nft_set_hash.c
+++ b/net/netfilter/nft_set_hash.c
@@ -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,
 	},
-- 
2.17.1


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] netfilter: nft_dynset: support for element deletion
  2019-07-13 16:03 [PATCH] netfilter: nft_dynset: support for element deletion Ander Juaristi
@ 2019-07-13 16:59 ` Florian Westphal
  2019-07-15 16:22   ` Ander Juaristi
  2019-08-08 20:41 ` kbuild test robot
  1 sibling, 1 reply; 5+ messages in thread
From: Florian Westphal @ 2019-07-13 16:59 UTC (permalink / raw)
  To: Ander Juaristi; +Cc: netfilter-devel

Ander Juaristi <a@juaristi.eus> wrote:
> 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, 

> --- a/include/net/netfilter/nf_tables.h
> +++ b/include/net/netfilter/nf_tables.h
> @@ -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?

> +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.

> +	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!

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] netfilter: nft_dynset: support for element deletion
  2019-07-13 16:59 ` Florian Westphal
@ 2019-07-15 16:22   ` Ander Juaristi
  2019-07-15 19:54     ` Florian Westphal
  0 siblings, 1 reply; 5+ messages in thread
From: Ander Juaristi @ 2019-07-15 16:22 UTC (permalink / raw)
  To: Florian Westphal; +Cc: netfilter-devel

[-- Attachment #1: Type: text/plain, Size: 874 bytes --]

On 13/7/19 18:59, Florian Westphal wrote:
> 
>> +	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.

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?

[-- Attachment #2: pEpkey.asc --]
[-- Type: application/pgp-keys, Size: 6263 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] netfilter: nft_dynset: support for element deletion
  2019-07-15 16:22   ` Ander Juaristi
@ 2019-07-15 19:54     ` Florian Westphal
  0 siblings, 0 replies; 5+ messages in thread
From: Florian Westphal @ 2019-07-15 19:54 UTC (permalink / raw)
  To: Ander Juaristi; +Cc: Florian Westphal, netfilter-devel

Ander Juaristi <a@juaristi.eus> wrote:
> On 13/7/19 18:59, Florian Westphal wrote:
> > 
> >> +	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.
> 
> 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?

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 :-)

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] netfilter: nft_dynset: support for element deletion
  2019-07-13 16:03 [PATCH] netfilter: nft_dynset: support for element deletion Ander Juaristi
  2019-07-13 16:59 ` Florian Westphal
@ 2019-08-08 20:41 ` kbuild test robot
  1 sibling, 0 replies; 5+ messages in thread
From: kbuild test robot @ 2019-08-08 20:41 UTC (permalink / raw)
  To: Ander Juaristi; +Cc: kbuild-all, netfilter-devel, Ander Juaristi

[-- Attachment #1: Type: text/plain, Size: 1723 bytes --]

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
Reported-by: kbuild test robot <lkp@intel.com>

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

[-- Attachment #2: .config.gz --]
[-- Type: application/gzip, Size: 47827 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, back to index

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-13 16:03 [PATCH] netfilter: nft_dynset: support for element deletion Ander Juaristi
2019-07-13 16:59 ` Florian Westphal
2019-07-15 16:22   ` Ander Juaristi
2019-07-15 19:54     ` Florian Westphal
2019-08-08 20:41 ` kbuild test robot

Netfilter-Devel Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/netfilter-devel/0 netfilter-devel/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 netfilter-devel netfilter-devel/ https://lore.kernel.org/netfilter-devel \
		netfilter-devel@vger.kernel.org netfilter-devel@archiver.kernel.org
	public-inbox-index netfilter-devel


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.netfilter-devel


AGPL code for this site: git clone https://public-inbox.org/ public-inbox