Skip to contentSkip to conversation

netfilter: nft_dynset: support for element deletion

PATCH Superseded Ander Juaristi 2 participants 2 messages Jul 18, 2019

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 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        | 17 ++++++---
 include/uapi/linux/netfilter/nf_tables.h |  1 +
 net/netfilter/nft_dynset.c               | 44 +++++++++++++++++-------
 net/netfilter/nft_set_hash.c             | 19 ++++++++++
 4 files changed, 63 insertions(+), 18 deletions(-)
include/net/netfilter/nf_tables.h +12 -5
index 9e8493aad49d..ddea26ba14b1 100644
@@ -286,24 +286,25 @@ struct nft_expr;
 /**
  *	struct nft_set_ops - nf_tables set operations
  *
+ *	@update: update an element if exists, add it if doesn't exist
+ *	@delete: delete an element
  *	@lookup: look up an element within the set
  *	@insert: insert new element into set
  *	@activate: activate new element in the next generation
  *	@deactivate: lookup for element and deactivate it in the next generation
  *	@flush: deactivate element in the next generation
  *	@remove: remove element from set
- *	@walk: iterate over all set elemeennts
+ *	@walk: iterate over all set elements
  *	@get: get set elements
  *	@privsize: function to return size of set private data
  *	@init: initialize private data of new set instance
  *	@destroy: destroy private data of set instance
  *	@elemsize: element private size
+ *
+ *	Operations update and delete have simpler interfaces, and currently
+ *	only used in the packet path. All the rest are for the control plane.
  */
 struct nft_set_ops {
-	bool				(*lookup)(const struct net *net,
-						  const struct nft_set *set,
-						  const u32 *key,
-						  const struct nft_set_ext **ext);
 	bool				(*update)(struct nft_set *set,
 						  const u32 *key,
 						  void *(*new)(struct nft_set *,
@@ -312,7 +313,13 @@ 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);
 
+	bool				(*lookup)(const struct net *net,
+						  const struct nft_set *set,
+						  const u32 *key,
+						  const struct nft_set_ext **ext);
 	int				(*insert)(const struct net *net,
 						  const struct nft_set *set,
 						  const struct nft_set_elem *elem,
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 +31 -13

74 lines not on this page · raw message

net/netfilter/nft_set_hash.c +19 -0
index 03df08801e28..867abcb488bc 100644
@@ -237,6 +237,24 @@ 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;
+
+	return (rhashtable_remove_fast(&priv->ht, &he->node, nft_rhash_params) == 0);
+}
+
 static void nft_rhash_walk(const struct nft_ctx *ctx, struct nft_set *set,
 			   struct nft_set_iter *iter)
 {
@@ -665,6 +683,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 20 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.

---
 include/net/netfilter/nf_tables.h        | 17 ++++++---
 include/uapi/linux/netfilter/nf_tables.h |  1 +
 net/netfilter/nft_dynset.c               | 44 +++++++++++++++++-------
 net/netfilter/nft_set_hash.c             | 19 ++++++++++
 4 files changed, 63 insertions(+), 18 deletions(-)
include/net/netfilter/nf_tables.h +0 -0
index 9e8493aad49d..ddea26ba14b1 100644

include/net/netfilter/nf_tables.h struct nft_expr; · line 291

Quoted 6 lines
include/net/netfilter/nf_tables.h +2 -0
@@ -286,24 +286,25 @@ struct nft_expr;
 /**
  *	struct nft_set_ops - nf_tables set operations
  *
+ *	@update: update an element if exists, add it if doesn't exist
+ *	@delete: delete an element
  *	@lookup: look up an element within the set

I would suggest:

include/net/netfilter/nf_tables.h struct nft_expr; · line 305

Quoted 7 lines
include/net/netfilter/nf_tables.h +5 -0
  *	@lookup: look up an element within the set
+ *	@update: update an element if exists, add it if doesn't exist
+ *	@delete: delete an element
 
+ *
+ *	Operations update and delete have simpler interfaces, and currently
+ *	only used in the packet path. All the rest are for the control plane.

Not correct, update/delete/lookup are packet path.

include/net/netfilter/nf_tables.h struct nft_expr; · line 307

Quoted 2 lines
include/net/netfilter/nf_tables.h +0 -0
  */
 struct nft_set_ops {

You could add a comment here,

	/* data plane / fast path */

include/net/netfilter/nf_tables.h struct nft_expr; · line 306

Quoted 4 lines
include/net/netfilter/nf_tables.h +0 -4
-	bool				(*lookup)(const struct net *net,
-						  const struct nft_set *set,
-						  const u32 *key,
-						  const struct nft_set_ext **ext);

This looks confusing, why is this being moved around?
You can keep it where it is, its most likely the most frequently called op.

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

Quoted 9 lines
include/net/netfilter/nf_tables.h +2 -0
 	bool				(*update)(struct nft_set *set,
 						  const u32 *key,
 						  void *(*new)(struct nft_set *,
@@ -312,7 +313,13 @@ 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);

Adding delete here looks good.

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

Quoted 4 lines
include/net/netfilter/nf_tables.h +4 -0
+	bool				(*lookup)(const struct net *net,
+						  const struct nft_set *set,
+						  const u32 *key,
+						  const struct nft_set_ext **ext);

I would keep it at the start, no need to move it.

You could add a comment here,

	/* control plane / slow path */

Also, please run your patches through scripts/checkpatch.pl.

Quoted 6 lines
include/net/netfilter/nf_tables.h +6 -0
+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 = {

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

Quoted 10 lines
include/net/netfilter/nf_tables.h +10 -0
+		.genmask = NFT_GENMASK_ANY,
+		.set = set,
+		.key = key,
+	};
+
+	he = rhashtable_lookup(&priv->ht, &arg, nft_rhash_params);
+	if (he == NULL)
+		return false;
+
+	return (rhashtable_remove_fast(&priv->ht, &he->node, nft_rhash_params) == 0);

No need for those ().

	return rhashtable_remove_fast( ...) == 0;

Other than that this looks ready for merging.