Skip to contentSkip to conversation

net/sched: Introduce skb hash classifier

PATCH In review Jamal Hadi Salim 3 participants 10 messages Aug 7, 2020 – Aug 19, 2020

Subsystems (3)
lore
Share & tools
lore

Jamal Hadi Salim

author maintainer
#0 lore raw

PATCH 1/1 net/sched: Introduce skb hash classifier

From: Jamal Hadi Salim <[email protected]>

his classifier, in the same spirit as the tc skb mark classifier, provides a generic (fast lookup) approach to filter on the hash value and optional mask.

like skb->mark, skb->hash could be set by multiple entities in the datapath including but not limited to hardware offloaded classification policies, hardware RSS (which already sets it today), XDP, ebpf programs and even by other tc actions like skbedit and IFE.

Example use:

$TC qdisc add dev $DEV1 ingress
... offloaded to hardware using flower ...
$TC filter add dev $DEV1 ingress protocol ip prio 1 flower hash 0x1f/0xff skip_sw flowid 1:1 \
action ok
... and when it shows up in s/w tagged from hardware...
$TC filter add dev $DEV1 parent ffff: protocol ip prio 2 handle 0x11/0xff skbhash flowid 1:11 \
action mirred egress redirect dev $DEV2
$TC filter add dev $DEV1 parent ffff: protocol ip prio 3 handle 0x12 skbhash flowid 1:12 \
action mirred egress redirect dev $DEV3

---
 include/uapi/linux/pkt_cls.h |  13 ++
 net/sched/Kconfig            |  10 +
 net/sched/Makefile           |   1 +
 net/sched/cls_skbhash.c      | 441 +++++++++++++++++++++++++++++++++++
 4 files changed, 465 insertions(+)
 create mode 100644 net/sched/cls_skbhash.c
include/uapi/linux/pkt_cls.h +13 -0
index ee95f42fb0ec..804cc326c2ce 100644
@@ -770,4 +770,17 @@ enum {
 	TCF_EM_OPND_LT
 };
 
+/* skbhash filter */
+
+enum {
+	TCA_SKBHASH_UNSPEC,
+	TCA_SKBHASH_CLASSID,
+	TCA_SKBHASH_POLICE,
+	TCA_SKBHASH_ACT,
+	TCA_SKBHASH_MASK,
+	__TCA_SKBHASH_MAX
+};
+
+#define TCA_SKBHASH_MAX (__TCA_SKBHASH_MAX - 1)
+
 #endif
net/sched/Kconfig +10 -0
index a3b37d88800e..aed2c74466e6 100644
@@ -640,6 +640,16 @@ config NET_CLS_MATCHALL
 	  To compile this code as a module, choose M here: the module will
 	  be called cls_matchall.
 
+config NET_CLS_SKBHASH
+	tristate "skb->hash classifier"
+	select NET_CLS
+	---help---
+	  If you say Y here, you will be able to classify packets
+	  according to the skb->hash value
+
+	  To compile this code as a module, choose M here: the
+	  module will be called cls_skbhash.
+
 config NET_EMATCH
 	bool "Extended Matches"
 	select NET_CLS
net/sched/Makefile +1 -0
index 66bbf9a98f9e..cbffba3b1fa1 100644
@@ -68,6 +68,7 @@ obj-$(CONFIG_NET_SCH_TAPRIO)	+= sch_taprio.o
 obj-$(CONFIG_NET_CLS_U32)	+= cls_u32.o
 obj-$(CONFIG_NET_CLS_ROUTE4)	+= cls_route.o
 obj-$(CONFIG_NET_CLS_FW)	+= cls_fw.o
+obj-$(CONFIG_NET_CLS_SKBHASH)	+= cls_skbhash.o
 obj-$(CONFIG_NET_CLS_RSVP)	+= cls_rsvp.o
 obj-$(CONFIG_NET_CLS_TCINDEX)	+= cls_tcindex.o
 obj-$(CONFIG_NET_CLS_RSVP6)	+= cls_rsvp6.o
net/sched/cls_skbhash.c new file +441 -0

441 lines not on this page · raw message

Signature · 1 line
2.20.1
#1 lore raw
Quoted 11 lines · Jamal Hadi Salim

From: Jamal Hadi Salim <[email protected]>

his classifier, in the same spirit as the tc skb mark classifier, provides a generic (fast lookup) approach to filter on the hash value and optional mask.

like skb->mark, skb->hash could be set by multiple entities in the datapath including but not limited to hardware offloaded classification policies, hardware RSS (which already sets it today), XDP, ebpf programs and even by other tc actions like skbedit and IFE.

Looks like a lot of code duplication with cls_fw, is there any way to reuse the code?

And I wonder if it is time to introduce a filter to match multiple skb fields, as adding a filter for each skb field clearly does not scale. Perhaps something like:

$TC filter add dev $DEV1 parent ffff: protocol ip prio 3 handle X skb \
hash Y mark Z flowid 1:12

Thanks.

author maintainer
#2 lore raw
Quoted 8 lines · Cong Wang
Quoted 7 lines · Jamal Hadi Salim

From: Jamal Hadi Salim <[email protected]>

his classifier, in the same spirit as the tc skb mark classifier, provides a generic (fast lookup) approach to filter on the hash value and optional mask.

Quoted 8 lines
Quoted 4 lines

like skb->mark, skb->hash could be set by multiple entities in the datapath including but not limited to hardware offloaded classification policies, hardware RSS (which already sets it today), XDP, ebpf programs and even by other tc actions like skbedit and IFE.

Looks like a lot of code duplication with cls_fw, is there any way to reuse the code?

Yeah, it was the simplest way to code this.
They are very similar since they both use 32 bit keys.
I am not exactly thrilled by the current 255 buckets(limited by rcu structure) - it limits performance in presence of large number of entries (since they can only be spread across 255 buckets). If we have a million entries, there will be a lot of hash collisions.

Quoted 7 lines

And I wonder if it is time to introduce a filter to match multiple skb fields, as adding a filter for each skb field clearly does not scale. Perhaps something like:

$TC filter add dev $DEV1 parent ffff: protocol ip prio 3 handle X skb \
hash Y mark Z flowid 1:12

Interesting idea. Note: my experience is that typical setup is to have only one of those (from offload perspective). Ariel, are your use cases requiring say both fields?

From policy perspective, i think above will get more complex mostly because you have to deal with either mark or hash being optional. It also opens doors for more complex matching requirements. Example "match mark X AND hash Y" and "match mark X OR hash Y".
The new classifier will have to deal with that semantic.

With fw and hash being the complex/optional semantics are easy:

"match mark X AND hash Y":
$TC filter add dev $DEV1 parent ffff: protocol ip prio 3 handle X skbhash flowid 1:12 action continue $TC filter add dev $DEV1 parent ffff: protocol ip prio 4 handle Y fw flowid 1:12 action ok

"match mark X OR hash Y":
$TC filter add dev $DEV1 parent ffff: protocol ip prio 3 handle X skbhash flowid 1:12 action ok $TC filter add dev $DEV1 parent ffff: protocol ip prio 4 handle Y fw flowid 1:12 action ok

Then the question is how to implement? is it one hash table for both or two(one for mark and one for hash), etc.

cheers,
jamal

#3 lore raw
Quoted 20 lines · Jamal Hadi Salim

Interesting idea. Note: my experience is that typical setup is to have only one of those (from offload perspective). Ariel, are your use cases requiring say both fields?

From policy perspective, i think above will get more complex
mostly because you have to deal with either mark or hash being optional. It also opens doors for more complex matching requirements. Example "match mark X AND hash Y" and "match mark X OR hash Y".
The new classifier will have to deal with that semantic.

With fw and hash being the complex/optional semantics are easy:

"match mark X AND hash Y":
$TC filter add dev $DEV1 parent ffff: protocol ip prio 3 handle X skbhash flowid 1:12 action continue
$TC filter add dev $DEV1 parent ffff: protocol ip prio 4 handle Y fw flowid 1:12 action ok

Quoted 5 lines

"match mark X OR hash Y":
$TC filter add dev $DEV1 parent ffff: protocol ip prio 3 handle X skbhash flowid 1:12 action ok
$TC filter add dev $DEV1 parent ffff: protocol ip prio 4 handle Y fw flowid 1:12 action ok

Not sure if I get you correctly, but with a combined implementation you can do above too, right? Something like:

(AND case)
$TC filter add dev $DEV1 parent ffff: protocol ip prio 3 handle 1 skb hash Y mark X flowid 1:12 action ok

(OR case)
$TC filter add dev $DEV1 parent ffff: protocol ip prio 3 handle 1 skb hash Y flowid 1:12 action ok
$TC filter add dev $DEV1 parent ffff: protocol ip prio 4 handle 2 skb mark X flowid 1:12 action ok

Side note: you don't have to use handle as the value of hash/mark, which gives people freedom to choose different handles.

Quoted 4 lines

Then the question is how to implement? is it one hash table for both or two(one for mark and one for hash), etc.

Good question. I am not sure, maybe no hash table at all? Unless there are a lot of filters, we do not have to organize them in a hash table, do we?

Thanks.

#4 lore raw
Quoted 27 lines · Cong Wang
Quoted 20 lines · Jamal Hadi Salim

Interesting idea. Note: my experience is that typical setup is to have only one of those (from offload perspective). Ariel, are your use cases requiring say both fields?

From policy perspective, i think above will get more complex
mostly because you have to deal with either mark or hash being optional. It also opens doors for more complex matching requirements. Example "match mark X AND hash Y" and "match mark X OR hash Y".
The new classifier will have to deal with that semantic.

With fw and hash being the complex/optional semantics are easy:

"match mark X AND hash Y":
$TC filter add dev $DEV1 parent ffff: protocol ip prio 3 handle X skbhash flowid 1:12 action continue
$TC filter add dev $DEV1 parent ffff: protocol ip prio 4 handle Y fw flowid 1:12 action ok

Quoted 5 lines

"match mark X OR hash Y":
$TC filter add dev $DEV1 parent ffff: protocol ip prio 3 handle X skbhash flowid 1:12 action ok
$TC filter add dev $DEV1 parent ffff: protocol ip prio 4 handle Y fw flowid 1:12 action ok

Quoted 6 lines

Not sure if I get you correctly, but with a combined implementation you can do above too, right? Something like:

(AND case)
$TC filter add dev $DEV1 parent ffff: protocol ip prio 3 handle 1 skb hash Y mark X flowid 1:12 action ok

I probably missed something, but this kind of matching is pretty much what flower does today. Is it just to avoid key extraction/flow dissector or did I miss something? I know there was a thread on how to match on this hash before, but other than what I just said, I can't recall other arguments.

author maintainer
#5 lore raw
In reply to Cong Wang
Quoted 1 line · Cong Wang

On Sun, Aug 9, 2020 at 4:41 PM Jamal Hadi Salim <[email protected]> wrote:

[..]

Quoted 8 lines

Not sure if I get you correctly, but with a combined implementation you can do above too, right? Something like:

(AND case)
$TC filter add dev $DEV1 parent ffff: protocol ip prio 3 handle 1 skb hash Y mark X flowid 1:12 action ok

Quoted 6 lines

(OR case)
$TC filter add dev $DEV1 parent ffff: protocol ip prio 3 handle 1 skb hash Y flowid 1:12 action ok
$TC filter add dev $DEV1 parent ffff: protocol ip prio 4 handle 2 skb mark X flowid 1:12 action ok

It will work but what i was tring to say is it is tricky to implement. More below

Quoted 3 lines

Side note: you don't have to use handle as the value of hash/mark, which gives people freedom to choose different handles.

Same comment here as above. More below.

Quoted 10 lines
Quoted 5 lines

Then the question is how to implement? is it one hash table for both or two(one for mark and one for hash), etc.

Good question. I am not sure, maybe no hash table at all? Unless there are a lot of filters, we do not have to organize them in a hash table, do we?

The _main_ requirement is to scale to a large number of filters (a million is a good handwave number). Scale means
1) fast datapath lookup time + 2) fast insertion/deletion/get/dump from control/user space.
fwmark is good at all these goals today for #2. It is good for #1 for maybe 1K rules (limitation is the 256 buckets, constrained by rcu trickery). Then you start having collisions in a bucket and your lookup requires long linked list walks.

Generally something like a hash table with sufficient number of buckets will work out ok.
There maybe other approaches (IDR in the kernel looks interesting, but i didnt look closely).

So to the implementation issue:
Major issue is removing ambiguity while at the same time trying to get good performance.

Lets say we decided to classify skbmark and skbhash at this point. For a hash table, one simple approach is to set lookupkey = hash<<32|mark

the key is used as input to the hash algo to find the bucket.

There are two outstanding challenges in my mind:

1) To use the policy like you describe above
as an example:

$TC filter add dev $DEV1 parent ffff: protocol ip prio 3 handle 1 skb hash Y flowid 1:12 action ok

and say you receive a packet with both skb->hash and skb->mark set Then there is ambiguity

How do you know whether to use hash or mark or both
for that specific key?
You can probably do some trick but I cant think of a cheap way to achieve this goal. Of course this issue doesnt exist if you have separate classifiers.

2) If you decide tomorrow to add tcindex/prio etc, you will have to rework this as well.

#2 is not as a big deal as #1.

cheers,
jamal

#6 lore raw
Quoted 30 lines · Jamal Hadi Salim

The _main_ requirement is to scale to a large number of filters (a million is a good handwave number). Scale means
1) fast datapath lookup time + 2) fast insertion/deletion/get/dump from control/user space.
fwmark is good at all these goals today for #2. It is good for #1 for maybe 1K rules (limitation is the 256 buckets, constrained by rcu trickery). Then you start having collisions in a bucket and your lookup requires long linked list walks.

Generally something like a hash table with sufficient number of buckets will work out ok.
There maybe other approaches (IDR in the kernel looks interesting, but i didnt look closely).

So to the implementation issue:
Major issue is removing ambiguity while at the same time trying to get good performance.

Lets say we decided to classify skbmark and skbhash at this point. For a hash table, one simple approach is to set lookupkey = hash<<32|mark

the key is used as input to the hash algo to find the bucket.

There are two outstanding challenges in my mind:

1) To use the policy like you describe above
as an example:

Quoted 8 lines

$TC filter add dev $DEV1 parent ffff: protocol ip prio 3 handle 1 skb hash Y flowid 1:12 action ok

and say you receive a packet with both skb->hash and skb->mark set Then there is ambiguity

How do you know whether to use hash or mark or both for that specific key?

Hmm, you can just unconditionally pass skb->hash and skb->mark, no? Something like:

if (filter_parameter_has_hash) {
    match skb->hash with cls->param_hash
}
if (filter_parameter_has_mark) {
    match skb->mark with cls->param_mark
}

fw_classify() uses skb->mark unconditionally anyway, without checking whether it is set or not first.

But if filters were put in a global hashtable, the above would be much harder to implement.

Quoted 8 lines

You can probably do some trick but I cant think of a cheap way to achieve this goal. Of course this issue doesnt exist if you have separate classifiers.

2) If you decide tomorrow to add tcindex/prio etc, you will have to rework this as well.

#2 is not as a big deal as #1.

Well, I think #2 is more serious than #1, if we have to use a hashtable. (If we don't have to, then it would be much easier to extend, of course.)

THanks.

author maintainer
#7 lore raw
Quoted 1 line · Cong Wang

On Thu, Aug 13, 2020 at 5:52 AM Jamal Hadi Salim <[email protected]> wrote:

[..]

Quoted 10 lines
Quoted 2 lines

How do you know whether to use hash or mark or both for that specific key?

Hmm, you can just unconditionally pass skb->hash and skb->mark, no? Something like:

if (filter_parameter_has_hash) {
     match skb->hash with cls->param_hash
}
Quoted 8 lines
if (filter_parameter_has_mark) {
     match skb->mark with cls->param_mark
}

fw_classify() uses skb->mark unconditionally anyway, without checking whether it is set or not first.

There is no ambiguity of intent in the fw case, there is only one field. In the case of having multiple fields it is ambigious if you unconditionally look.

Example: policy says to match skb mark of 5 and hash of 3. If packet arrives with skb->mark is 5 and skb->hash is 3 very clearly matched the intent of the policy.
If packet arrives withj skb->mark 7 and hash 3 it clearly did not match the intent. etc.

Quoted 3 lines

But if filters were put in a global hashtable, the above would be much harder to implement.

Ok, yes. My assumption has been you will have some global shared structure where all filters will be installed on.

I think i may have misunderstood all along what you were saying which is:

a) add the rules so they are each _independent with different priorities_ in a chain.

b) when i do lookup for packet arrival, i will only see a filter that matches "match mark 5 and hash 3" (meaning there is no ambiguity on intent). If packet data doesnt match policy then i will iterate to another filter on the chain list with lower priority.

Am i correct in my understanding?

If i am - then we still have a problem with lookup scale in presence of a large number of filters since essentially this approach is linear lookup (similar problem iptables has). I am afraid a hash table or something with similar principle goals is needed.

Quoted 5 lines
Quoted 5 lines

You can probably do some trick but I cant think of a cheap way to achieve this goal. Of course this issue doesnt exist if you have separate classifiers.

Quoted 8 lines
Quoted 4 lines

2) If you decide tomorrow to add tcindex/prio etc, you will have to rework this as well.

#2 is not as a big deal as #1.

Well, I think #2 is more serious than #1, if we have to use a hashtable. (If we don't have to, then it would be much easier to extend, of course.)

In both cases youd have to extend the existing code.

cheers,
jamal

#8 lore raw
Quoted 29 lines · Jamal Hadi Salim
Quoted 1 line · Cong Wang

On Thu, Aug 13, 2020 at 5:52 AM Jamal Hadi Salim <[email protected]> wrote:

[..]

Quoted 10 lines
Quoted 2 lines

How do you know whether to use hash or mark or both for that specific key?

Hmm, you can just unconditionally pass skb->hash and skb->mark, no? Something like:

if (filter_parameter_has_hash) {
     match skb->hash with cls->param_hash
}
Quoted 8 lines
if (filter_parameter_has_mark) {
     match skb->mark with cls->param_mark
}

fw_classify() uses skb->mark unconditionally anyway, without checking whether it is set or not first.

There is no ambiguity of intent in the fw case, there is only one field. In the case of having multiple fields it is ambigious if you unconditionally look.

Quoted 5 lines

Example: policy says to match skb mark of 5 and hash of 3. If packet arrives with skb->mark is 5 and skb->hash is 3 very clearly matched the intent of the policy.
If packet arrives withj skb->mark 7 and hash 3 it clearly did not match the intent. etc.

This example clearly shows no ambiguous, right? ;)

Quoted 7 lines
Quoted 4 lines

But if filters were put in a global hashtable, the above would be much harder to implement.

Ok, yes. My assumption has been you will have some global shared structure where all filters will be installed on.

Sure, if not hashtable, we could simply put them in a list:

list_for_each_filter {

  if (filter_parameter_has_hash) {
    match skb->hash with cls->param_hash
  }
  if (filter_parameter_has_mark) {
    match skb->mark with cls->param_mark

}
}

Quoted 6 lines

I think i may have misunderstood all along what you were saying which is:

a) add the rules so they are each _independent with different priorities_ in a chain.

Yes, because this gives users freedom to pick a different prio from its value (hash or mark).

Quoted 6 lines

b) when i do lookup for packet arrival, i will only see a filter that matches "match mark 5 and hash 3" (meaning there is no ambiguity on intent). If packet data doesnt match policy then i will iterate to another filter on the chain list with lower priority.

Right. Multiple values mean AND, not OR, so if you specify mark 5 and hash 3, it will match skb->mark==5 && skb->hash==3. If not matched, it will continue the iteration until the end.

Quoted 7 lines

Am i correct in my understanding?

If i am - then we still have a problem with lookup scale in presence of a large number of filters since essentially this approach is linear lookup (similar problem iptables has). I am afraid a hash table or something with similar principle goals is needed.

Yeah, this is why I asked you whether we have to put them in a hashtable in previous emails, as hashtable organizes them with a key, it is hard to combine multiple fields in one key and allow to extend easily in the future. But other people smarter than me may have better ideas here.

Thanks.

author maintainer
#9 lore raw
Quoted 2 lines · Cong Wang
Quoted 1 line · Jamal Hadi Salim

[..]

Quoted 4 lines
Quoted 4 lines

There is no ambiguity of intent in the fw case, there is only one field. In the case of having multiple fields it is ambigious if you unconditionally look.

Quoted 8 lines
Quoted 5 lines

Example: policy says to match skb mark of 5 and hash of 3. If packet arrives with skb->mark is 5 and skb->hash is 3 very clearly matched the intent of the policy.
If packet arrives withj skb->mark 7 and hash 3 it clearly did not match the intent. etc.

This example clearly shows no ambiguous, right? ;)

Ambigious only from the perspective of relational AND vs OR (your original pseudo code had it in OR relation).

Quoted 11 lines
Quoted 8 lines
Quoted 5 lines

But if filters were put in a global hashtable, the above would be much harder to implement.

Ok, yes. My assumption has been you will have some global shared structure where all filters will be installed on.

Sure, if not hashtable, we could simply put them in a list:

Quoted 9 lines
list_for_each_filter {
   if (filter_parameter_has_hash) {
     match skb->hash with cls->param_hash
   }
   if (filter_parameter_has_mark) {
     match skb->mark with cls->param_mark
   }
}

Yes, that would work - but iteration is linear.

Quoted 11 lines
Quoted 7 lines

I think i may have misunderstood all along what you were saying which is:

a) add the rules so they are each _independent with different priorities_ in a chain.

Yes, because this gives users freedom to pick a different prio from its value (hash or mark).

ok.

Quoted 8 lines
Quoted 7 lines

b) when i do lookup for packet arrival, i will only see a filter that matches "match mark 5 and hash 3" (meaning there is no ambiguity on intent). If packet data doesnt match policy then i will iterate to another filter on the chain list with lower priority.

Quoted 4 lines

Right. Multiple values mean AND, not OR, so if you specify mark 5 and hash 3, it will match skb->mark==5 && skb->hash==3. If not matched, it will continue the iteration until the end.

That would remove the ambiguity (assuming iteration with "continue" to create the AND effect).

Quoted 8 lines
Quoted 7 lines

Am i correct in my understanding?

If i am - then we still have a problem with lookup scale in presence of a large number of filters since essentially this approach is linear lookup (similar problem iptables has). I am afraid a hash table or something with similar principle goals is needed.

Quoted 5 lines

Yeah, this is why I asked you whether we have to put them in a hashtable in previous emails, as hashtable organizes them with a key, it is hard to combine multiple fields in one key and allow to extend easily in the future. But other people smarter than me may have better ideas here.

To achieve reasonable performance (with many filters) I dont think there is escape from having something that is centralized (per priority) - sort of what fw, u32 or flower do. A hash table is the most common approach; i was hoping that IDR maybe useful since the skb->hash maps nicely to "32 bit key" but Vlad was saying at the tc workshop that he was seeing bottlenecks with IDR.

I will test a few hash algorithms (including common one like jenkins) for this use case.
Problem right now is we have that rcu-inspired upper bucket limit (which exists) in fw as well: If i have 1M entries which can only be spread to 256 buckets perfectly then i have ~4K entries per bucket which are a linked list. So we are still not doing so well. Do you have time to make that better for fw (since you are an rcu maestro).

cheers,
jamal