Skip to contentSkip to conversation

Traffic shaping with nftables maps and tc

DISCUSSION Tomas Mudrunka 2 participants 6 messages Nov 2, 2017 – Nov 3, 2017

lore
Share & tools
lore
#0 lore raw

Hello,
i am thinking about improving the way we do bandwidth shaping (traffic control) in our network.

Currently we have like 80000 rules that do something like this:

iptables -A post_10_11_171_248_29 -d 10.11.171.249/32 -o vlan1100 -j CLASSIFY --set-class 0001:3065

And even when there is some level of optimalization using subchains, i guess that nftables can optimize this simply by internal hashing in named map (or dictionary - i did not get how these two are different).

I need to have $IP : $CLASS map like this:

10.11.171.247/32 : 0001:4095
10.11.171.248/32 : 0001:3065
10.11.171.249/32 : 0001:2023

And then simply classify the packets using single rule like this:

-d $IP -j CLASSIFY --set-class $CLASS

So i can later shape the classified packets using tc. (BTW i've heard that nft has something called "ingress hook" that could replace tc, so i could have everything configured thru nft, is that true?)

Is there way to do this in nftables?

Signature · 3 lines
S pozdravem
Best regards
     Tomáš Mudruňka - SPOJE.NET s.r.o.
#1 lore raw

I've been doing some online research and done several experiments:

1.) This seems to load properly, but i have not checked if it sets the classid correctly:

table ip filter {
	chain input {
		type filter hook input priority 0; policy accept;
		meta priority set 1:3065
	}
}

however this is not really usefull as it does not do any real hashing / maping, so i can't just use it for lots of rules. So i tried to adapt it to use maps or dictionaries:

=======================================================

2.) This does not load:

table ip filter {
	map prometheus {
		type ipv4_addr : classid
		elements = {
			10.11.171.247 : "0001:4095",
			10.11.171.248 : "0001:3065",
			10.11.171.249 : "0001:2023"
		}
	}
}

test2.nft:3:34-40: Error: syntax error, unexpected classid, expecting string or dscp or ecn or mark

		type ipv4_addr : classid
                                 ^^^^^^^
test2.nft:2:13-22: Error: map definition does not specify key data type
	map prometheus {
            ^^^^^^^^^^

=======================================================

3.) This also does not load:

table ip filter {
	map prometheus {
		type ipv4_addr : verdict
		elements = {
			10.11.171.247 : meta priority set "0001:4095",
			10.11.171.248 : meta priority set "0001:3065",
			10.11.171.249 : meta priority set "0001:2023"
		}
	}
}

test2.nft:5:41-44: Error: syntax error, unexpected meta

			10.11.171.247 : meta priority set "0001:4095",
                                        ^^^^
test2.nft:6:25-37: Error: syntax error, unexpected string
			10.11.171.248 : meta priority set "0001:3065",
                        ^^^^^^^^^^^^^
test2.nft:7:25-37: Error: syntax error, unexpected string
			10.11.171.249 : meta priority set "0001:2023"
                        ^^^^^^^^^^^^^

test2.nft:10:1-1: Error: syntax error, unexpected '}' }
^

It seems to me that maps are not generic enough to work with relatively new "meta priority set" feature.

Do you have any suggestions how to make this work?

Signature · 3 lines
S pozdravem
Best regards
     Tomáš Mudruňka - SPOJE.NET s.r.o.
#3 lore raw
Quoted 2 lines

Try this:
meta priority set ip daddr map { 1.1.1.2 : 2:2222, 1.1.1.3 : 3:3333 }

Thank you, that looks promising. However is there way to name this map, so i can dynamicaly edit it? I've checked nft describe:

$ nft describe ip daddr
payload expression, datatype ipv4_addr (IPv4 address) (basetype integer), 32 bits
$ nft describe meta priority
meta expression, datatype classid (TC classid) (basetype integer), 32 bits

But then later "classid" seems to fail when name defining table:

# nft add map filter deucalion { type ipv4_addr : classid\; }
<cmdline>:1:45-51: Error: syntax error, unexpected classid, expecting string or dscp or ecn or mark add map filter deucalion { type ipv4_addr : classid; }

                                            ^^^^^^^
<cmdline>:1:26-52: Error: map definition does not specify key data type
add map filter deucalion { type ipv4_addr : classid; }
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^

Same goes for this:

	map deucalion {
		type ipv4_addr : classid;
		elements = { 1.1.1.2 : 2:2222, 1.1.1.3 : 3:3333 }
	}


	chain input {
		type filter hook input priority 0; policy accept;
		meta priority set ip daddr map @deucalion;
	}
Signature · 3 lines
S pozdravem
Best regards
     Tomáš Mudruňka - SPOJE.NET s.r.o.
#4 lore raw
Quoted 6 lines · Tomas Mudrunka
Quoted 2 lines

Try this:
meta priority set ip daddr map { 1.1.1.2 : 2:2222, 1.1.1.3 : 3:3333 }

Thank you, that looks promising. However is there way to name this map, so i can dynamicaly edit it? I've checked nft describe:

It seems there is some issue with the parser.

Could you please test the attached patch? It is quick and dirty, not even compiled tested.
Hope there aren't shift/reduce errors from bison.

parser: allow classid as set key

From: Arturo Borrero Gonzalez <[email protected]>

Allow TC classid as set key.

---
 src/parser_bison.y |    1 +
 1 file changed, 1 insertion(+)
src/parser_bison.y +1 -0
index 7016f5b..2918875 100644
@@ -1545,6 +1545,7 @@ type_identifier		:	STRING	{ $$ = $1; }
 			|	MARK	{ $$ = xstrdup("mark"); }
 			|	DSCP	{ $$ = xstrdup("dscp"); }
 			|	ECN	{ $$ = xstrdup("ecn"); }
+			|	CLASSID { $$ = xstrdup("classid"); }
 			;
 
 hook_spec		:	TYPE		STRING		HOOK		STRING		dev_spec	PRIORITY	prio_spec
#5 lore raw
Quoted 2 lines

Could you please test the attached patch? It is quick and dirty, not even compiled tested.

Hell yeah! It loaded :-) Thank you.
That's one small line for a bison, one giant leap for traffic shaping on nftables.

Quoted 8 lines

+++ b/src/parser_bison.y

src/parser_bison.y +1 -0
@@ -1545,6 +1545,7 @@ type_identifier		:	STRING	{ $$ = $1; }
 			|	MARK	{ $$ = xstrdup("mark"); }
 			|	DSCP	{ $$ = xstrdup("dscp"); }
 			|	ECN	{ $$ = xstrdup("ecn"); }
+			|	CLASSID { $$ = xstrdup("classid"); }
 			;
 
Signature · 3 lines
S pozdravem
Best regards
     Tomáš Mudruňka - SPOJE.NET s.r.o.