# htb and prio qdiscs, any tc gurus around? [SOLVED]

## Sadako

Hey, I'm trying to use both htb and prio qdiscs together, but can't quite get it to work.

What I want exactly is a htb qdisc on the device root, with two htb classes, one for normal and the other (very) low priority traffic.

Then I want a prio qdisc "attached" to each of those two htb classes, with perhaps 3 bands in each prio qdisc and maybe sfq attached to each.

This is what I've tried so far, on 512kbit upstream link;

```
tc qdisc del dev eth0 root

tc qdisc add dev eth0 root handle 1:0 htb default 20

tc class add dev eth0 parent 1:0 classid 1:1 htb rate 512kbit ceil 512kbit

tc class add dev eth0 parent 1:1 classid 1:2 htb rate 448kbit ceil 512kbit prio 0

tc class add dev eth0 parent 1:1 classid 1:3 htb rate 64kbit ceil 448kbit prio 1

tc qdisc add dev eth0 parent 1:2 handle 2:0 prio bands 2 priomap 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

tc qdisc add dev eth0 parent 1:3 handle 3:0 prio bands 2 priomap 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

tc qdisc add dev eth0 parent 2:1 handle 10:0 sfq headdrop

tc qdisc add dev eth0 parent 2:2 handle 20:0 sfq headdrop

tc qdisc add dev eth0 parent 3:1 handle 30:0 sfq headdrop

tc qdisc add dev eth0 parent 3:2 handle 40:0 sfq headdrop
```

The thing is, I don't know exactly how to get the traffic to the sfq qdiscs, no matter what I've tried only the htb qdisc attached to root seems to be "used".

I need to mark the traffic with iptables as I'm classifying the low priority traffic by the UID of the sender (via the owner iptables match), that worked fine when I was just using -j CLASSIFY --set-class when using htb alone.

With the prio qdiscs, I've tried (for example);

```
iptables -t mangle -A POSTROUTING -j MARK --set-mark 4 -m owner --uid-owner 1337

tc filter add dev eth0 parent 1: protocol ip prio 1 handle 4 fw flowid 3:2
```

but this has had no effect.

tbh, I don't have a great grasp of the fundamentals, especially wrt to handles and tc filters, which is where I think I'm getting stuck.

For reference, here's what I'm currently using, which isn't exactly what I want but does work as expected;

```
tc qdisc add dev eth0 root handle 1: htb default 20

tc class add dev eth0 parent 1: classid 1:1 htb rate 512kbit ceil 512kbit

tc class add dev eth0 parent 1:1 classid 1:10 htb rate 224kbit ceil 512kbit prio 0

tc class add dev eth0 parent 1:1 classid 1:20 htb rate 256kbit ceil 512kbit prio 1

tc class add dev eth0 parent 1:1 classid 1:2 htb rate 32kbit ceil 384kbit prio 2

tc class add dev eth0 parent 1:2 classid 1:30 htb rate 24kbit ceil 384kbit prio 3

tc class add dev eth0 parent 1:2 classid 1:40 htb rate 8kbit ceil 128kbit prio 4

tc qdisc add dev eth0 parent 1:10 handle 10: sfq headdrop

tc qdisc add dev eth0 parent 1:20 handle 20: sfq headdrop

tc qdisc add dev eth0 parent 1:30 handle 30: sfq headdrop

tc qdisc add dev eth0 parent 1:40 handle 40: sfq headdrop

iptables -t mangle -A POSTROUTING -j CLASSIFY --set-class 1:10 -m length --length 0:68

iptables -t mangle -A POSTROUTING -j CLASSIFY --set-class 1:30 -m owner --uid-owner 1337

iptables -t mangle -A POSTROUTING -j CLASSIFY --set-class 1:40 -m owner --uid-owner 1338
```

Any ideas?

And thanks to anyone to have actually read this far.  :Wink: 

----------

## Sadako

Okay, I've got this working now, found two problems with what I was trying, one makes sense but the other doesn't, both seemingly related to using a classful qdisc within a classful qdisc;

The CLASSIFY iptables mark works as intended, but I can only set one target class per packet (obviously), meaning I can target the right htb class but can't use it to set the right prio class, or use it to set the prio class I want but have no way to send traffic to anything but the default htb class.

That makes sense, but what doesn't (to me) is that I can't seem to attach tc filters to anything but the "root" qdisc of a device, 'parent 1:' is the only thing which seems to work, with other parents the tc command seems to succeed but no filter is attached, `tc filter show eth0` only shows filters attached to 1:.

Anyways, the following works for me;

```
tc qdisc del dev eth0 root

tc qdisc add dev eth0 root handle 1:0 htb default 2

tc class add dev eth0 parent 1:0 classid 1:1 htb rate 480kbit ceil 480kbit

tc class add dev eth0 parent 1:1 classid 1:2 htb rate 448kbit ceil 480kbit prio 0

tc class add dev eth0 parent 1:1 classid 1:3 htb rate 32kbit ceil 448kbit prio 1

tc qdisc add dev eth0 parent 1:2 handle 2:0 prio bands 3

tc qdisc add dev eth0 parent 1:3 handle 3:0 prio bands 3

tc filter add dev eth0 parent 1:0 prio 1 handle 1337 fw classid 1:3
```

With an iptables rule to set a netfilter mark (1337) based on the packet uid, and then using the CLASSIFY iptables target to prioritize as I want.

Just posting this on the off chance anyone else runs into similiar issues, and to get this thread out of the unanswered threads search.

----------

