Skip to contentSkip to conversation

netfilter: nf_tables: Introduce stateful object update operation

PATCH Superseded Fernando Fernandez Mancera 2 participants 10 messages Aug 22, 2019 – Aug 26, 2019

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 netfilter: nf_tables: Introduce stateful object update operation

This patch adds the infrastructure needed for the stateful object update support.

---
 include/net/netfilter/nf_tables.h | 10 ++++
 net/netfilter/nf_tables_api.c     | 90 ++++++++++++++++++++++++++++---
 2 files changed, 93 insertions(+), 7 deletions(-)
include/net/netfilter/nf_tables.h +10 -0
index dc301e3d6739..3b6e300b21af 100644
@@ -1123,6 +1123,10 @@ struct nft_object_ops {
 	int				(*dump)(struct sk_buff *skb,
 						struct nft_object *obj,
 						bool reset);
+	int				(*update)(const struct nft_ctx *ctx,
+						  const struct nlattr *const tb[],
+						  struct nft_object *obj,
+						  bool commit);
 	const struct nft_object_type	*type;
 };
 
@@ -1405,10 +1409,16 @@ struct nft_trans_elem {
 
 struct nft_trans_obj {
 	struct nft_object		*obj;
+	struct nlattr			**tb;
+	bool				update;
 };
 
 #define nft_trans_obj(trans)	\
 	(((struct nft_trans_obj *)trans->data)->obj)
+#define nft_trans_obj_tb(trans) \
+	(((struct nft_trans_obj *)trans->data)->tb)
+#define nft_trans_obj_update(trans)	\
+	(((struct nft_trans_obj *)trans->data)->update)
 
 struct nft_trans_flowtable {
 	struct nft_flowtable		*flowtable;
net/netfilter/nf_tables_api.c +83 -7

120 lines not on this page · raw message

Signature · 1 line
2.20.1
#1 lore raw

PATCH v2 2/2 netfilter: nft_quota: add quota object update support

---
 net/netfilter/nft_quota.c | 42 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
net/netfilter/nft_quota.c +42 -0
index c8745d454bf8..2afea3f50a51 100644
@@ -105,6 +105,47 @@ static int nft_quota_obj_init(const struct nft_ctx *ctx,
 	return nft_quota_do_init(tb, priv);
 }
 
+static int nft_quota_do_update(const struct nlattr * const tb[],
+			       struct nft_quota * priv, bool commit)
+{
+	unsigned long flags;
+	u64 quota;
+
+	flags = priv->flags;
+	quota = priv->quota;
+
+	if (tb[NFTA_QUOTA_BYTES]) {
+		quota = be64_to_cpu(nla_get_be64(tb[NFTA_QUOTA_BYTES]));
+		if (quota > S64_MAX)
+			return -EOVERFLOW;
+	}
+
+	if (tb[NFTA_QUOTA_FLAGS]) {
+		flags = ntohl(nla_get_be32(tb[NFTA_QUOTA_FLAGS]));
+		if (flags & ~NFT_QUOTA_F_INV)
+			return -EINVAL;
+		if (flags & ~NFT_QUOTA_F_DEPLETED)
+			return -EOPNOTSUPP;
+	}
+
+	if (commit) {
+		priv->quota = quota;
+		priv->flags = flags;
+	}
+
+	return 0;
+}
+
+static int nft_quota_obj_update(const struct nft_ctx *ctx,
+				const struct nlattr * const tb[],
+				struct nft_object *obj,
+				bool commit)
+{
+	struct nft_quota *priv = nft_obj_data(obj);
+
+	return nft_quota_do_update(tb, priv, commit);
+}
+
 static int nft_quota_do_dump(struct sk_buff *skb, struct nft_quota *priv,
 			     bool reset)
 {
@@ -155,6 +196,7 @@ static const struct nft_object_ops nft_quota_obj_ops = {
 	.init		= nft_quota_obj_init,
 	.eval		= nft_quota_obj_eval,
 	.dump		= nft_quota_obj_dump,
+	.update		= nft_quota_obj_update,
 };
 
 static struct nft_object_type nft_quota_obj_type __read_mostly = {
Signature · 1 line
2.20.1
#2 lore raw
In reply to Fernando Fernandez Mancera

include/net/netfilter/nf_tables.h struct nft_trans_elem { · line 1412

Quoted 5 lines · Fernando Fernandez Mancera
include/net/netfilter/nf_tables.h +1 -0
@@ -1405,10 +1409,16 @@ struct nft_trans_elem {
 
 struct nft_trans_obj {
 	struct nft_object		*obj;
+	struct nlattr			**tb;

Instead of annotatint tb[] on the object, you can probably add here:

union {
        struct quota {
                uint64_t                consumed;
                uint64_t                quota;
      } quota;
};

So the initial update annotates the values in the transaction.

I guess you will need two new indirections? Something like prepare_update() and update().

#3 lore raw
Quoted 16 lines · Pablo Neira Ayuso

include/net/netfilter/nf_tables.h struct nft_trans_elem { · line 1412

Quoted 5 lines · Fernando Fernandez Mancera
include/net/netfilter/nf_tables.h +1 -0
@@ -1405,10 +1409,16 @@ struct nft_trans_elem {
 
 struct nft_trans_obj {
 	struct nft_object		*obj;
+	struct nlattr			**tb;

Instead of annotatint tb[] on the object, you can probably add here:

union {
        struct quota {
                uint64_t                consumed;
                uint64_t                quota;
      } quota;
};
Quoted 4 lines

So the initial update annotates the values in the transaction.

I guess you will need two new indirections? Something like prepare_update() and update().

Or you have a single update() and pass enum nft_trans_phase as parameter, so this only needs one single indirection.

#4 lore raw
Quoted 17 lines · Pablo Neira Ayuso
Quoted 16 lines · Pablo Neira Ayuso

include/net/netfilter/nf_tables.h struct nft_trans_elem { · line 1412

Quoted 5 lines · Fernando Fernandez Mancera
include/net/netfilter/nf_tables.h +1 -0
@@ -1405,10 +1409,16 @@ struct nft_trans_elem {
 
 struct nft_trans_obj {
 	struct nft_object		*obj;
+	struct nlattr			**tb;

Instead of annotatint tb[] on the object, you can probably add here:

union {
        struct quota {
                uint64_t                consumed;
                uint64_t                quota;
      } quota;
};
Quoted 8 lines
Quoted 4 lines

So the initial update annotates the values in the transaction.

I guess you will need two new indirections? Something like prepare_update() and update().

Or you have a single update() and pass enum nft_trans_phase as parameter, so this only needs one single indirection.

But also we would need to continue passing the 'bool commit' as a parameter too right? I will take a look to nft_trans_phase. Thanks! :-)

#5 lore raw
Quoted 20 lines · Fernando Fernandez Mancera
Quoted 17 lines · Pablo Neira Ayuso
Quoted 16 lines · Pablo Neira Ayuso

include/net/netfilter/nf_tables.h struct nft_trans_elem { · line 1412

Quoted 5 lines · Fernando Fernandez Mancera
include/net/netfilter/nf_tables.h +1 -0
@@ -1405,10 +1409,16 @@ struct nft_trans_elem {
 
 struct nft_trans_obj {
 	struct nft_object		*obj;
+	struct nlattr			**tb;

Instead of annotatint tb[] on the object, you can probably add here:

union {
        struct quota {
                uint64_t                consumed;
                uint64_t                quota;
      } quota;
};
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

So the initial update annotates the values in the transaction.

If we follow that pattern then the indirection would need the nft_trans_phase enum, the quota struct and also the tb[] as parameters because in the preparation phase we always need the tb[] array.

Why is that better than annotating tb[] on the object? Sorry, I think that I am missing something here. Thanks!

Quoted 9 lines
Quoted 6 lines
Quoted 2 lines

I guess you will need two new indirections? Something like prepare_update() and update().

Or you have a single update() and pass enum nft_trans_phase as parameter, so this only needs one single indirection.

But also we would need to continue passing the 'bool commit' as a parameter too right? I will take a look to nft_trans_phase. Thanks! :-)

#6 lore raw
Quoted 21 lines · Fernando Fernandez Mancera
Quoted 20 lines · Fernando Fernandez Mancera
Quoted 17 lines · Pablo Neira Ayuso
Quoted 16 lines · Pablo Neira Ayuso

include/net/netfilter/nf_tables.h struct nft_trans_elem { · line 1412

Quoted 5 lines · Fernando Fernandez Mancera
include/net/netfilter/nf_tables.h +1 -0
@@ -1405,10 +1409,16 @@ struct nft_trans_elem {
 
 struct nft_trans_obj {
 	struct nft_object		*obj;
+	struct nlattr			**tb;

Instead of annotatint tb[] on the object, you can probably add here:

union {
        struct quota {
                uint64_t                consumed;
                uint64_t                quota;
      } quota;
};
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

So the initial update annotates the values in the transaction.

If we follow that pattern then the indirection would need the nft_trans_phase enum, the quota struct and also the tb[] as parameters because in the preparation phase we always need the tb[] array.

Right, so this is my next idea :-)

For the update case, I'd suggest you use the existing 'obj' field in the transaction object. The idea would be to allocate a new object via nft_obj_init() from the update path. Hence, you can use the existing expr->ops->init() interface to parse the attributes - I find the existing parsing for ->update() a bit redundant.

Then, from the commit path, you use the new ->update() interface to update the object accordingly taking this new object as input. I think you cannot update u64 quota like you do in this patch. On 32-bit arches, an assignment of u64 won't be atomic. So you have to use atomic64_set() and atomic64_read() to make sure that packet path does not observes an inconsistent state. BTW, once you have updated the existing object, you can just release the object in the transaction coming in this update. I think you will need a 'bool update' field on the transaction object, so the commit path knows how to handle the update.

#7 lore raw

Hi Pablo,

Quoted 37 lines · Pablo Neira Ayuso
Quoted 21 lines · Fernando Fernandez Mancera
Quoted 20 lines · Fernando Fernandez Mancera
Quoted 17 lines · Pablo Neira Ayuso
Quoted 16 lines · Pablo Neira Ayuso

include/net/netfilter/nf_tables.h struct nft_trans_elem { · line 1412

Quoted 5 lines · Fernando Fernandez Mancera
include/net/netfilter/nf_tables.h +1 -0
@@ -1405,10 +1409,16 @@ struct nft_trans_elem {
 
 struct nft_trans_obj {
 	struct nft_object		*obj;
+	struct nlattr			**tb;

Instead of annotatint tb[] on the object, you can probably add here:

union {
        struct quota {
                uint64_t                consumed;
                uint64_t                quota;
      } quota;
};
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

So the initial update annotates the values in the transaction.

If we follow that pattern then the indirection would need the nft_trans_phase enum, the quota struct and also the tb[] as parameters because in the preparation phase we always need the tb[] array.

Right, so this is my next idea :-)

For the update case, I'd suggest you use the existing 'obj' field in the transaction object. The idea would be to allocate a new object via nft_obj_init() from the update path. Hence, you can use the existing expr->ops->init() interface to parse the attributes - I find the existing parsing for ->update() a bit redundant.

Quoted 11 lines

Then, from the commit path, you use the new ->update() interface to update the object accordingly taking this new object as input. I think you cannot update u64 quota like you do in this patch. On 32-bit arches, an assignment of u64 won't be atomic. So you have to use atomic64_set() and atomic64_read() to make sure that packet path does not observes an inconsistent state. BTW, once you have updated the existing object, you can just release the object in the transaction coming in this update. I think you will need a 'bool update' field on the transaction object, so the commit path knows how to handle the update.

I would need to add a second 'obj' field in the transaction object in order to have the existing object pointer and the new one.

Also, right now we are not using atomic64_set() when initializing u64 quota is that a bug then? If it is, I could include a patch fixing it.

Thanks!

#8 lore raw
Quoted 52 lines · Fernando Fernandez Mancera

Hi Pablo,

Quoted 37 lines · Pablo Neira Ayuso
Quoted 21 lines · Fernando Fernandez Mancera
Quoted 20 lines · Fernando Fernandez Mancera
Quoted 17 lines · Pablo Neira Ayuso
Quoted 16 lines · Pablo Neira Ayuso

Review struct nft_trans_elem { · line 1412

Quoted 5 lines · Fernando Fernandez Mancera
(quoted diff) +1 -0
@@ -1405,10 +1409,16 @@ struct nft_trans_elem {
 
 struct nft_trans_obj {
 	struct nft_object		*obj;
+	struct nlattr			**tb;

Instead of annotatint tb[] on the object, you can probably add here:

union {
        struct quota {
                uint64_t                consumed;
                uint64_t                quota;
      } quota;
};
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

So the initial update annotates the values in the transaction.

If we follow that pattern then the indirection would need the nft_trans_phase enum, the quota struct and also the tb[] as parameters because in the preparation phase we always need the tb[] array.

Right, so this is my next idea :-)

For the update case, I'd suggest you use the existing 'obj' field in the transaction object. The idea would be to allocate a new object via nft_obj_init() from the update path. Hence, you can use the existing expr->ops->init() interface to parse the attributes - I find the existing parsing for ->update() a bit redundant.

Quoted 11 lines

Then, from the commit path, you use the new ->update() interface to update the object accordingly taking this new object as input. I think you cannot update u64 quota like you do in this patch. On 32-bit arches, an assignment of u64 won't be atomic. So you have to use atomic64_set() and atomic64_read() to make sure that packet path does not observes an inconsistent state. BTW, once you have updated the existing object, you can just release the object in the transaction coming in this update. I think you will need a 'bool update' field on the transaction object, so the commit path knows how to handle the update.

Quoted 2 lines

I would need to add a second 'obj' field in the transaction object in order to have the existing object pointer and the new one.

Hm, this might be convenient, we might need to swap other objects.

Quoted 2 lines

Also, right now we are not using atomic64_set() when initializing u64 quota is that a bug then? If it is, I could include a patch fixing it.

No packets see that quota limit by when it is set for first time.

With quota updates, packets are walking over this state while this is updated from the control plane.

#9 lore raw
Quoted 58 lines · Pablo Neira Ayuso
Quoted 52 lines · Fernando Fernandez Mancera

Hi Pablo,

Quoted 37 lines · Pablo Neira Ayuso
Quoted 21 lines · Fernando Fernandez Mancera
Quoted 20 lines · Fernando Fernandez Mancera
Quoted 17 lines · Pablo Neira Ayuso
Quoted 16 lines · Pablo Neira Ayuso

Review struct nft_trans_elem { · line 1412

Quoted 5 lines · Fernando Fernandez Mancera
(quoted diff) +1 -0
@@ -1405,10 +1409,16 @@ struct nft_trans_elem {
 
 struct nft_trans_obj {
 	struct nft_object		*obj;
+	struct nlattr			**tb;

Instead of annotatint tb[] on the object, you can probably add here:

union {
        struct quota {
                uint64_t                consumed;
                uint64_t                quota;
      } quota;
};
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

So the initial update annotates the values in the transaction.

If we follow that pattern then the indirection would need the nft_trans_phase enum, the quota struct and also the tb[] as parameters because in the preparation phase we always need the tb[] array.

Right, so this is my next idea :-)

For the update case, I'd suggest you use the existing 'obj' field in the transaction object. The idea would be to allocate a new object via nft_obj_init() from the update path. Hence, you can use the existing expr->ops->init() interface to parse the attributes - I find the existing parsing for ->update() a bit redundant.

Quoted 11 lines

Then, from the commit path, you use the new ->update() interface to update the object accordingly taking this new object as input. I think you cannot update u64 quota like you do in this patch. On 32-bit arches, an assignment of u64 won't be atomic. So you have to use atomic64_set() and atomic64_read() to make sure that packet path does not observes an inconsistent state. BTW, once you have updated the existing object, you can just release the object in the transaction coming in this update. I think you will need a 'bool update' field on the transaction object, so the commit path knows how to handle the update.

Quoted 2 lines

I would need to add a second 'obj' field in the transaction object in order to have the existing object pointer and the new one.

Hm, this might be convenient, we might need to swap other objects.

Quoted 8 lines
Quoted 2 lines

Also, right now we are not using atomic64_set() when initializing u64 quota is that a bug then? If it is, I could include a patch fixing it.

No packets see that quota limit by when it is set for first time.

With quota updates, packets are walking over this state while this is updated from the control plane.

Thanks for explaining. I am preparing a patch. Thanks Pablo!