Skip to contentSkip to conversation

net/sched: Introduce tc block ports tracking and use

PATCH Superseded Victor Nogueira 3 participants 7 messages Dec 14, 2023 – Dec 15, 2023

Subsystems (3)

v6 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v6 cover net/sched: Introduce tc block ports tracking and use

__context__
The "tc block" is a collection of netdevs/ports which allow qdiscs to share match-action block instances (as opposed to the traditional tc filter per netdev/port)[1].

Up to this point in the implementation, the block is unaware of its ports. This patch makes the tc block ports available to the datapath.

For the datapath we provide a use case of the tc block in a mirred action in patch 3. For users can levarage mirred to do something like the following:

$ tc qdisc add dev ens7 ingress_block 22
$ tc qdisc add dev ens8 ingress_block 22
$ tc qdisc add dev ens9 ingress_block 22
$ tc filter add block 22 protocol ip pref 25 \
  flower dst_ip 192.168.0.0/16 action mirred egress mirror blockid 22

In this case, if the packet arrives on ens8, it will be copied and sent to all ports in the block including ens8. Note that the packet is still in the pipeline at this point - meaning other actions could be added after the mirror because mirred copies/clones the skb. Example the following is valid:

$ tc filter add block 22 protocol ip pref 25 flower dst_ip 192.168.0.0/16 \
action mirred egress mirror blockid 22 \
action action vlan push id 123 \
action mirred egress redirect dev dummy0

redirect behavior always steals the packet from the pipeline and therefore the skb is no longer available for a subsequent action as illustrated above (in redirecting to dummy0).

The behavior of redirecting to a tc block is therefore adapted to work in the same manner. So a setup as such:

$ tc qdisc add dev ens7 ingress_block 22
$ tc qdisc add dev ens8 ingress_block 22
$ tc qdisc add dev ens9 ingress_block 22
$ tc filter add block 22 protocol ip pref 25 \
  flower dst_ip 192.168.0.0/16 action mirred egress redirect blockid 22

for a matching packet arriving on ens7 will first send a copy/clone to ens8 (as in the "mirror" behavior) then to ens9 as in the redirect behavior above. Once this processing is done - no other actions are able to process this skb. i.e it is removed from the "pipeline".

In this case, if the packet arrives on ens8, it will be copied and sent to all ports in the block including ens8.

Patch 1 separates/exports mirror and redirect functions from act_mirred Patch 2 introduces the required infra.
Patch 3 Allows mirred to blocks

Subsequent patches will come with tdc test cases.

__Acknowledgements__
Suggestions from Vlad Buslov and Marcelo Ricardo Leitner made this patchset
better. The idea of integrating the ports into the tc block was suggested
by Jiri Pirko.

[1] See commit ca46abd6f89f ("Merge branch'net-sched-allow-qdiscs-to-share-filter-block-instances'")

Changes in v2:
- Remove RFC tag
- Add more details in patch 0(Jiri)
- When CONFIG_NET_TC_SKB_EXT is selected we have unused qdisc_cb

[email protected])
- Fix bad dev dereference in printk of blockcast action (Simon)

Changes in v3:
- Add missing xa_destroy (pointed out by Vlad)
- Remove bugfix pointed by Vlad (will send in separate patch)
- Removed ports from subject in patch #2 and typos (suggested by Marcelo)
- Remove net_notice_ratelimited debug messages in error cases (suggested by Marcelo)
- Minor changes to appease sparse's lock context warning

Changes in v4:
- Avoid code repetition using gotos in cast_one (suggested by Paolo)
- Fix typo in cover letter (pointed out by Paolo)
- Create a module description for act_blockcast
(reported by Paolo and CI)

Changes in v5:
- Added new patch which separated mirred into mirror and redirect functions (suggested by Jiri)
- Instead of repeating the code to mirror in blockcast use mirror exported function by patch1 (tcf_mirror_act)
- Make Block ID into act_blockcast's parameter passed by user space instead of always getting it from SKB (suggested by Jiri)
- Add tx_type parameter which will specify what transmission behaviour we want (as described earlier)

Changes in v6:
- Remove blockcast and make it a part of mirred
- Block ID is now a mirred parameter
- We now allow redirecting and mirroring to either ingress or egress

Victor Nogueira (3):
net/sched: Introduce tc block netdev tracking infra net/sched: cls_api: Expose tc block to the datapath net/sched: act_mirred: Allow mirred to block

 include/net/sch_generic.h             |   6 +
 include/net/tc_act/tc_mirred.h        |   1 +
 include/uapi/linux/tc_act/tc_mirred.h |   1 +
 net/sched/act_mirred.c                | 280 +++++++++++++++++++-------
 net/sched/cls_api.c                   |   5 +-
 net/sched/sch_api.c                   |  55 +++++
 net/sched/sch_generic.c               |  31 ++-
 7 files changed, 302 insertions(+), 77 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v6 1/3 net/sched: Introduce tc block netdev tracking infra

This commit makes tc blocks track which ports have been added to them. And, with that, we'll be able to use this new information to send packets to the block's ports. Which will be done in the patch #3 of this series.

---
 include/net/sch_generic.h |  4 +++
 net/sched/cls_api.c       |  2 ++
 net/sched/sch_api.c       | 55 +++++++++++++++++++++++++++++++++++++++
 net/sched/sch_generic.c   | 31 ++++++++++++++++++++--
 4 files changed, 90 insertions(+), 2 deletions(-)
include/net/sch_generic.h +4 -0
index dcb9160e6467..cefca55dd4f9 100644
@@ -19,6 +19,7 @@
 #include <net/gen_stats.h>
 #include <net/rtnetlink.h>
 #include <net/flow_offload.h>
+#include <linux/xarray.h>
 
 struct Qdisc_ops;
 struct qdisc_walker;
@@ -126,6 +127,8 @@ struct Qdisc {
 
 	struct rcu_head		rcu;
 	netdevice_tracker	dev_tracker;
+	netdevice_tracker	in_block_tracker;
+	netdevice_tracker	eg_block_tracker;
 	/* private data */
 	long privdata[] ____cacheline_aligned;
 };
@@ -457,6 +460,7 @@ struct tcf_chain {
 };
 
 struct tcf_block {
+	struct xarray ports; /* datapath accessible */
 	/* Lock protects tcf_block and lifetime-management data of chains
 	 * attached to the block (refcnt, action_refcnt, explicitly_created).
 	 */
net/sched/cls_api.c +2 -0
index dc1c19a25882..6020a32ecff2 100644
@@ -531,6 +531,7 @@ static void tcf_block_destroy(struct tcf_block *block)
 {
 	mutex_destroy(&block->lock);
 	mutex_destroy(&block->proto_destroy_lock);
+	xa_destroy(&block->ports);
 	kfree_rcu(block, rcu);
 }
 
@@ -1002,6 +1003,7 @@ static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
 	refcount_set(&block->refcnt, 1);
 	block->net = net;
 	block->index = block_index;
+	xa_init(&block->ports);
 
 	/* Don't store q pointer for blocks which are shared */
 	if (!tcf_block_shared(block))
net/sched/sch_api.c +55 -0

67 lines not on this page · raw message

net/sched/sch_generic.c +29 -2
index 8dd0e5925342..32bed60dea9f 100644
@@ -1050,7 +1050,11 @@ static void qdisc_free_cb(struct rcu_head *head)
 
 static void __qdisc_destroy(struct Qdisc *qdisc)
 {
-	const struct Qdisc_ops  *ops = qdisc->ops;
+	struct net_device *dev = qdisc_dev(qdisc);
+	const struct Qdisc_ops *ops = qdisc->ops;
+	const struct Qdisc_class_ops *cops;
+	struct tcf_block *block;
+	u32 block_index;
 
 #ifdef CONFIG_NET_SCHED
 	qdisc_hash_del(qdisc);
@@ -1061,11 +1065,34 @@ static void __qdisc_destroy(struct Qdisc *qdisc)
 
 	qdisc_reset(qdisc);
 
+	cops = ops->cl_ops;
+	if (ops->ingress_block_get) {
+		block_index = ops->ingress_block_get(qdisc);
+		if (block_index) {
+			block = cops->tcf_block(qdisc, TC_H_MIN_INGRESS, NULL);
+			if (block) {
+				if (xa_erase(&block->ports, dev->ifindex))
+					netdev_put(dev, &qdisc->in_block_tracker);
+			}
+		}
+	}
+
+	if (ops->egress_block_get) {
+		block_index = ops->egress_block_get(qdisc);
+		if (block_index) {
+			block = cops->tcf_block(qdisc, TC_H_MIN_EGRESS, NULL);
+			if (block) {
+				if (xa_erase(&block->ports, dev->ifindex))
+					netdev_put(dev, &qdisc->eg_block_tracker);
+			}
+		}
+	}
+
 	if (ops->destroy)
 		ops->destroy(qdisc);
 
 	module_put(ops->owner);
-	netdev_put(qdisc_dev(qdisc), &qdisc->dev_tracker);
+	netdev_put(dev, &qdisc->dev_tracker);
 
 	trace_qdisc_destroy(qdisc);
 
Signature · 1 line
2.25.1
#2 lore raw

PATCH v6 2/3 net/sched: cls_api: Expose tc block to the datapath

In reply to Victor Nogueira

The datapath can now find the block of the port in which the packet arrived at.

In the next patch we show a possible usage of this patch in a new version of mirred that multicasts to all ports except for the port in which the packet arrived on.

---
 include/net/sch_generic.h | 2 ++
 net/sched/cls_api.c       | 3 ++-
 2 files changed, 4 insertions(+), 1 deletion(-)
include/net/sch_generic.h +2 -0
index cefca55dd4f9..479bc195bb0f 100644
@@ -487,6 +487,8 @@ struct tcf_block {
 	struct mutex proto_destroy_lock; /* Lock for proto_destroy hashtable. */
 };
 
+struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index);
+
 static inline bool lockdep_tcf_chain_is_locked(struct tcf_chain *chain)
 {
 	return lockdep_is_held(&chain->filter_chain_lock);
net/sched/cls_api.c +2 -1
index 6020a32ecff2..618f68733012 100644
@@ -1011,12 +1011,13 @@ static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
 	return block;
 }
 
-static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
+struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
 {
 	struct tcf_net *tn = net_generic(net, tcf_net_id);
 
 	return idr_find(&tn->idr, block_index);
 }
+EXPORT_SYMBOL(tcf_block_lookup);
 
 static struct tcf_block *tcf_block_refcnt_get(struct net *net, u32 block_index)
 {
Signature · 1 line
2.25.1
#3 lore raw

PATCH v6 3/3 net/sched: act_mirred: Allow mirred to block

In reply to Victor Nogueira

So far the mirred action has dealt with syntax that handles mirror/redirection for netdev. A matching packet is redirected or mirrored to a target netdev.

In this patch we enable mirred to mirror to a tc block as well.
IOW, the new syntax looks as follows:
... mirred <ingress | egress> <mirror | redirect> [index INDEX] < <blockid BLOCKID> | <dev <devname>> >

Examples of mirroring or redirecting to a tc block:

$ tc filter add block 22 protocol ip pref 25 \
  flower dst_ip 192.168.0.0/16 action mirred egress mirror blockid 22
$ tc filter add block 22 protocol ip pref 25 \
  flower dst_ip 10.10.10.10/32 action mirred egress redirect blockid 22
---
 include/net/tc_act/tc_mirred.h        |   1 +
 include/uapi/linux/tc_act/tc_mirred.h |   1 +
 net/sched/act_mirred.c                | 280 +++++++++++++++++++-------
 3 files changed, 208 insertions(+), 74 deletions(-)
include/net/tc_act/tc_mirred.h +1 -0
index 32ce8ea36950..75722d967bf2 100644
@@ -8,6 +8,7 @@
 struct tcf_mirred {
 	struct tc_action	common;
 	int			tcfm_eaction;
+	u32                     tcfm_blockid;
 	bool			tcfm_mac_header_xmit;
 	struct net_device __rcu	*tcfm_dev;
 	netdevice_tracker	tcfm_dev_tracker;
include/uapi/linux/tc_act/tc_mirred.h +1 -0
index 2500a0005d05..54df06658bc8 100644
@@ -20,6 +20,7 @@ enum {
 	TCA_MIRRED_UNSPEC,
 	TCA_MIRRED_TM,
 	TCA_MIRRED_PARMS,
+	TCA_MIRRED_BLOCKID,
 	TCA_MIRRED_PAD,
 	__TCA_MIRRED_MAX
 };
net/sched/act_mirred.c +206 -74

390 lines not on this page · raw message

Signature · 1 line
2.25.1
maintainer
#4 lore raw

Re: [PATCH net-next v6 3/3] net/sched: act_mirred: Allow mirred to block

Quoted 7 lines · On Thu, 14 Dec 2023 11:10:06 -0300 Victor Nogueira wrote:

So far the mirred action has dealt with syntax that handles mirror/redirection for netdev. A matching packet is redirected or mirrored to a target netdev.

In this patch we enable mirred to mirror to a tc block as well.
IOW, the new syntax looks as follows:
... mirred <ingress | egress> <mirror | redirect> [index INDEX] < <blockid BLOCKID> | <dev <devname>> >

Quoted 6 lines

Examples of mirroring or redirecting to a tc block:

$ tc filter add block 22 protocol ip pref 25 \
  flower dst_ip 192.168.0.0/16 action mirred egress mirror blockid 22
$ tc filter add block 22 protocol ip pref 25 \
  flower dst_ip 10.10.10.10/32 action mirred egress redirect blockid 22

net/sched/act_mirred.c:424:6: warning: variable 'err' set but not used [-Wunused-but-set-variable]
424 | int err = 0;
| ^

Signature · 1 line
pw-bot: cr
#5 lore raw

Re: [PATCH net-next v6 3/3] net/sched: act_mirred: Allow mirred to block

Quoted 12 lines · Jakub Kicinski
Quoted 11 lines · On Thu, 14 Dec 2023 11:10:06 -0300 Victor Nogueira wrote:

So far the mirred action has dealt with syntax that handles mirror/redirection for netdev. A matching packet is redirected or mirrored to a target netdev.

In this patch we enable mirred to mirror to a tc block as well.
IOW, the new syntax looks as follows:
... mirred <ingress | egress> <mirror | redirect> [index INDEX] < <blockid BLOCKID> | <dev <devname>> >

Examples of mirroring or redirecting to a tc block:

$ tc filter add block 22 protocol ip pref 25 \
   flower dst_ip 192.168.0.0/16 action mirred egress mirror blockid 22
Quoted 6 lines
Quoted 2 lines
$ tc filter add block 22 protocol ip pref 25 \
   flower dst_ip 10.10.10.10/32 action mirred egress redirect blockid 22

net/sched/act_mirred.c:424:6: warning: variable 'err' set but not used [-Wunused-but-set-variable]
424 | int err = 0;
| ^

Thank you for the catch.
Sent a v7 fixing it.

cheers,
Victor