Skip to contentSkip to conversation

netfilter: nf_tables: set selection

PATCH In review Patrick McHardy 2 participants 8 messages Mar 28, 2014 – Apr 14, 2014

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

cover netfilter: nf_tables: set selection

These patches implement the basic set selection mechanism based on data characteristics provided by userspace. So far we only need to know the maximum number of elements.

Instead of trying to anticipate the information future set types will need, the plan is to add more as we require it under the assumption that we will converge to a state where a new set type already has all information required available. The expectation is that most set types will require the same information for estimation since they most likely will be some type of tree or trie.

Please apply.

#1 lore raw

PATCH 1/2 netfilter: nf_tables: implement proper set selection

The current set selection simply choses the first set type that provides the requested features, which always results in the rbtree being chosen by virtue of being the first set in the list.

What we actually want to do is choose the implementation that can provide the requested features and is optimal from either a performance or memory perspective depending on the characteristics of the elements and the preferences specified by the user.

The elements are not known when creating a set. Even if we would provide them for anonymous (literal) sets, we'd still have standalone sets where the elements are not known in advance. We therefore need an abstract description of the data charcteristics.

The kernel already knows the size of the key, this patch starts by introducing a nested set description which so far contains only the maximum amount of elements. Based on this the set implementations are changed to provide an estimate of the required amount of memory and the lookup complexity class.

The set ops have a new callback ->estimate() that is invoked during set selection. It receives a structure containing the attributes known to the kernel and is supposed to populate a struct nft_set_estimate with the complexity class and, in case the size is known, the complete amount of memory required, or the amount of memory required per element otherwise.

Based on the policy specified by the user (performance/memory, defaulting to performance) the kernel will then select the best suited implementation.

Even if the set implementation would allow to add more than the specified maximum amount of elements, they are enforced since new implementations might not be able to add more than maximum based on which they were selected.

---
 include/net/netfilter/nf_tables.h        |  46 ++++++++++++
 include/uapi/linux/netfilter/nf_tables.h |  27 +++++++
 net/netfilter/nf_tables_api.c            | 121 +++++++++++++++++++++++++++----
 net/netfilter/nft_hash.c                 |  45 +++++++++++-
 net/netfilter/nft_rbtree.c               |  21 ++++++
 5 files changed, 242 insertions(+), 18 deletions(-)
include/net/netfilter/nf_tables.h +46 -0
index e6bc14d..29ff1dc 100644
@@ -146,6 +146,44 @@ struct nft_set_iter {
 };
 
 /**
+ *	struct nft_set_desc - description of set elements
+ *
+ *	@klen: key length
+ *	@dlen: data length
+ *	@size: number of set elements
+ */
+struct nft_set_desc {
+	unsigned int		klen;
+	unsigned int		dlen;
+	unsigned int		size;
+};
+
+/**
+ *	enum nft_set_class - performance class
+ *
+ *	@NFT_LOOKUP_O_1: constant, O(1)
+ *	@NFT_LOOKUP_O_LOG_N: logarithmic, O(log N)
+ *	@NFT_LOOKUP_O_N: linear, O(N)
+ */
+enum nft_set_class {
+	NFT_SET_CLASS_O_1,
+	NFT_SET_CLASS_O_LOG_N,
+	NFT_SET_CLASS_O_N,
+};
+
+/**
+ *	struct nft_set_estimate - estimation of memory and performance
+ *				  characteristics
+ *
+ *	@size: required memory
+ *	@class: lookup performance class
+ */
+struct nft_set_estimate {
+	unsigned int		size;
+	enum nft_set_class	class;
+};
+
+/**
  *	struct nft_set_ops - nf_tables set operations
  *
  *	@lookup: look up an element within the set
@@ -174,7 +212,11 @@ struct nft_set_ops {
 						struct nft_set_iter *iter);
 
 	unsigned int			(*privsize)(const struct nlattr * const nla[]);
+	bool				(*estimate)(const struct nft_set_desc *desc,
+						    u32 features,
+						    struct nft_set_estimate *est);
 	int				(*init)(const struct nft_set *set,
+						const struct nft_set_desc *desc,
 						const struct nlattr * const nla[]);
 	void				(*destroy)(const struct nft_set *set);
 
@@ -194,6 +236,8 @@ void nft_unregister_set(struct nft_set_ops *ops);
  * 	@name: name of the set
  * 	@ktype: key type (numeric type defined by userspace, not used in the kernel)
  * 	@dtype: data type (verdict or numeric type defined by userspace)
+ * 	@size: maximum set size
+ * 	@nelems: number of elements
  * 	@ops: set ops
  * 	@flags: set flags
  * 	@klen: key length
@@ -206,6 +250,8 @@ struct nft_set {
 	char				name[IFNAMSIZ];
 	u32				ktype;
 	u32				dtype;
+	u32				size;
+	u32				nelems;
 	/* runtime data below here */
 	const struct nft_set_ops	*ops ____cacheline_aligned;
 	u16				flags;
include/uapi/linux/netfilter/nf_tables.h +27 -0
index c88ccbf..1601592 100644
@@ -212,6 +212,29 @@ enum nft_set_flags {
 };
 
 /**
+ * enum nft_set_policies - set selection policy
+ *
+ * @NFT_SET_POL_PERFORMANCE: prefer high performance over low memory use
+ * @NFT_SET_POL_MEMORY: prefer low memory use over high performance
+ */
+enum nft_set_policies {
+	NFT_SET_POL_PERFORMANCE,
+	NFT_SET_POL_MEMORY,
+};
+
+/**
+ * enum nft_set_desc_attributes - set element description
+ *
+ * @NFTA_SET_DESC_SIZE: number of elements in set (NLA_U32)
+ */
+enum nft_set_desc_attributes {
+	NFTA_SET_DESC_UNSPEC,
+	NFTA_SET_DESC_SIZE,
+	__NFTA_SET_DESC_MAX
+};
+#define NFTA_SET_DESC_MAX	(__NFTA_SET_DESC_MAX - 1)
+
+/**
  * enum nft_set_attributes - nf_tables set netlink attributes
  *
  * @NFTA_SET_TABLE: table name (NLA_STRING)
@@ -221,6 +244,8 @@ enum nft_set_flags {
  * @NFTA_SET_KEY_LEN: key data length (NLA_U32)
  * @NFTA_SET_DATA_TYPE: mapping data type (NLA_U32)
  * @NFTA_SET_DATA_LEN: mapping data length (NLA_U32)
+ * @NFTA_SET_POLICY: selection policy (NLA_U32)
+ * @NFTA_SET_DESC: set description (NLA_NESTED)
  */
 enum nft_set_attributes {
 	NFTA_SET_UNSPEC,
@@ -231,6 +256,8 @@ enum nft_set_attributes {
 	NFTA_SET_KEY_LEN,
 	NFTA_SET_DATA_TYPE,
 	NFTA_SET_DATA_LEN,
+	NFTA_SET_POLICY,
+	NFTA_SET_DESC,
 	__NFTA_SET_MAX
 };
 #define NFTA_SET_MAX		(__NFTA_SET_MAX - 1)
net/netfilter/nf_tables_api.c +105 -16

233 lines not on this page · raw message

net/netfilter/nft_hash.c +43 -2

87 lines not on this page · raw message

net/netfilter/nft_rbtree.c +21 -0
index e21d69d..072e611 100644
@@ -201,6 +201,7 @@ static unsigned int nft_rbtree_privsize(const struct nlattr * const nla[])
 }
 
 static int nft_rbtree_init(const struct nft_set *set,
+			   const struct nft_set_desc *desc,
 			   const struct nlattr * const nla[])
 {
 	struct nft_rbtree *priv = nft_set_priv(set);
@@ -222,8 +223,28 @@ static void nft_rbtree_destroy(const struct nft_set *set)
 	}
 }
 
+static bool nft_rbtree_estimate(const struct nft_set_desc *desc, u32 features,
+				struct nft_set_estimate *est)
+{
+	unsigned int nsize;
+
+	nsize = sizeof(struct nft_rbtree_elem);
+	if (features & NFT_SET_MAP)
+		nsize += FIELD_SIZEOF(struct nft_rbtree_elem, data[0]);
+
+	if (desc->size)
+		est->size = sizeof(struct nft_rbtree) + desc->size * nsize;
+	else
+		est->size = nsize;
+
+	est->class = NFT_SET_CLASS_O_LOG_N;
+
+	return true;
+}
+
 static struct nft_set_ops nft_rbtree_ops __read_mostly = {
 	.privsize	= nft_rbtree_privsize,
+	.estimate	= nft_rbtree_estimate,
 	.init		= nft_rbtree_init,
 	.destroy	= nft_rbtree_destroy,
 	.insert		= nft_rbtree_insert,
Signature · 1 line
1.9.0
#2 lore raw

PATCH 2/2 netfilter: nft_hash: use set global element counter instead of private one

In reply to Patrick McHardy

Now that nf_tables performs global accounting of set elements, it is not needed in the hash type anymore.

---
 net/netfilter/nft_hash.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
net/netfilter/nft_hash.c +2 -7
index c0ede25b..bfb732d 100644
@@ -27,7 +27,6 @@ struct nft_hash {
 
 struct nft_hash_table {
 	unsigned int			size;
-	unsigned int			elements;
 	struct nft_hash_elem __rcu	*buckets[];
 };
 
@@ -166,7 +165,6 @@ static int nft_hash_tbl_expand(const struct nft_set *set, struct nft_hash *priv)
 			break;
 		}
 	}
-	ntbl->elements = tbl->elements;
 
 	/* Publish new table */
 	rcu_assign_pointer(priv->tbl, ntbl);
@@ -206,7 +204,6 @@ static int nft_hash_tbl_shrink(const struct nft_set *set, struct nft_hash *priv)
 			;
 		RCU_INIT_POINTER(*pprev, tbl->buckets[i + ntbl->size]);
 	}
-	ntbl->elements = tbl->elements;
 
 	/* Publish new table */
 	rcu_assign_pointer(priv->tbl, ntbl);
@@ -242,10 +239,9 @@ static int nft_hash_insert(const struct nft_set *set,
 	h = nft_hash_data(&he->key, tbl->size, set->klen);
 	RCU_INIT_POINTER(he->next, tbl->buckets[h]);
 	rcu_assign_pointer(tbl->buckets[h], he);
-	tbl->elements++;
 
 	/* Expand table when exceeding 75% load */
-	if (tbl->elements > tbl->size / 4 * 3)
+	if (set->nelems + 1 > tbl->size / 4 * 3)
 		nft_hash_tbl_expand(set, priv);
 
 	return 0;
@@ -273,10 +269,9 @@ static void nft_hash_remove(const struct nft_set *set,
 	RCU_INIT_POINTER(*pprev, he->next);
 	synchronize_rcu();
 	kfree(he);
-	tbl->elements--;
 
 	/* Shrink table beneath 30% load */
-	if (tbl->elements < tbl->size * 3 / 10 &&
+	if (set->nelems - 1 < tbl->size * 3 / 10 &&
 	    tbl->size > NFT_HASH_MIN_SIZE)
 		nft_hash_tbl_shrink(set, priv);
 }
Signature · 1 line
1.9.0
#3 lore raw
In reply to Patrick McHardy
Quoted 10 lines · Patrick McHardy

These patches implement the basic set selection mechanism based on data characteristics provided by userspace. So far we only need to know the maximum number of elements.

Instead of trying to anticipate the information future set types will need, the plan is to add more as we require it under the assumption that we will converge to a state where a new set type already has all information required available. The expectation is that most set types will require the same information for estimation since they most likely will be some type of tree or trie.

I'm going to include this in the next batch for net-next before merge window closes, which should happen soon. Thanks a lot Patrick.

#4 lore raw
Quoted 12 lines · Pablo Neira Ayuso
Quoted 10 lines · Patrick McHardy

These patches implement the basic set selection mechanism based on data characteristics provided by userspace. So far we only need to know the maximum number of elements.

Instead of trying to anticipate the information future set types will need, the plan is to add more as we require it under the assumption that we will converge to a state where a new set type already has all information required available. The expectation is that most set types will require the same information for estimation since they most likely will be some type of tree or trie.

Quoted 2 lines

I'm going to include this in the next batch for net-next before merge window closes, which should happen soon. Thanks a lot Patrick.

I can't find neither this series, nor the patch
"netfilter: nf_tables: handle more than 8 * PAGE_SIZE set name allocations" anywhere in your trees or upstream.

Am I missing something?

#5 lore raw
Quoted 13 lines · Patrick McHardy
Quoted 12 lines · Pablo Neira Ayuso
Quoted 10 lines · Patrick McHardy

These patches implement the basic set selection mechanism based on data characteristics provided by userspace. So far we only need to know the maximum number of elements.

Instead of trying to anticipate the information future set types will need, the plan is to add more as we require it under the assumption that we will converge to a state where a new set type already has all information required available. The expectation is that most set types will require the same information for estimation since they most likely will be some type of tree or trie.

Quoted 6 lines
Quoted 2 lines

I'm going to include this in the next batch for net-next before merge window closes, which should happen soon. Thanks a lot Patrick.

I can't find neither this series, nor the patch
"netfilter: nf_tables: handle more than 8 * PAGE_SIZE set name allocations" anywhere in your trees or upstream.

That one I missed, I'll enqueue it to the nftables tree.

Quoted 1 line

Am I missing something?

http://git.kernel.org/cgit/linux/kernel/git/pablo/nftables.git/log/

#6 lore raw
Quoted 21 lines · Pablo Neira Ayuso
Quoted 13 lines · Patrick McHardy
Quoted 12 lines · Pablo Neira Ayuso
Quoted 10 lines · Patrick McHardy

These patches implement the basic set selection mechanism based on data characteristics provided by userspace. So far we only need to know the maximum number of elements.

Instead of trying to anticipate the information future set types will need, the plan is to add more as we require it under the assumption that we will converge to a state where a new set type already has all information required available. The expectation is that most set types will require the same information for estimation since they most likely will be some type of tree or trie.

Quoted 6 lines
Quoted 2 lines

I'm going to include this in the next batch for net-next before merge window closes, which should happen soon. Thanks a lot Patrick.

I can't find neither this series, nor the patch
"netfilter: nf_tables: handle more than 8 * PAGE_SIZE set name allocations" anywhere in your trees or upstream.

Quoted 5 lines

That one I missed, I'll enqueue it to the nftables tree.

Quoted 1 line

Am I missing something?

http://git.kernel.org/cgit/linux/kernel/git/pablo/nftables.git/log/

I see. I thought that tree was retired and things moved to nf/nf-next?

#7 lore raw
Quoted 22 lines · Patrick McHardy
Quoted 21 lines · Pablo Neira Ayuso
Quoted 19 lines · Patrick McHardy
Quoted 12 lines · Pablo Neira Ayuso
Quoted 10 lines · Patrick McHardy

These patches implement the basic set selection mechanism based on data characteristics provided by userspace. So far we only need to know the maximum number of elements.

Instead of trying to anticipate the information future set types will need, the plan is to add more as we require it under the assumption that we will converge to a state where a new set type already has all information required available. The expectation is that most set types will require the same information for estimation since they most likely will be some type of tree or trie.

Quoted 2 lines

I'm going to include this in the next batch for net-next before merge window closes, which should happen soon. Thanks a lot Patrick.

I can't find neither this series, nor the patch
"netfilter: nf_tables: handle more than 8 * PAGE_SIZE set name allocations" anywhere in your trees or upstream.

Quoted 7 lines
Quoted 5 lines

That one I missed, I'll enqueue it to the nftables tree.

Quoted 1 line

Am I missing something?

http://git.kernel.org/cgit/linux/kernel/git/pablo/nftables.git/log/

I see. I thought that tree was retired and things moved to nf/nf-next?

I already mentioned here:

http://marc.info/?l=netfilter-devel&m=139634116115356&w=2

net-next / nf-next is closed at this moment. So I kept the nftables kernel tree open to collect things until net-next opens back not to mix with other non-nftables things.