Adding IP for matcher for queues (not just packet marks)

I had requested this feature to mikrotik support but it appears I'm the only one. (SUP-210597)

The request is to allow an IP matcher for queue tree instead of packet matcher. I'm hitting performance limits related to matching every packet for packet marks, matching on IP should reduce CPU usage by a large margin, not having to add and track packet marks.

If you have any interest in this feature for building queue trees, especially QoE trees, please upvote this. You might also submit a support ticket requesting it. Thanks.

First you mark connections by your ip and then apply packet marks on that connection.

I think you've missed the point, the packet marks are expensive on the CPU and dramatically limit the throughput of the box.

I think you missed my point as well.

I definitely didn't, the marking of the packets itself running through mangle is the expensive operation. Using a connection mark is no less expensive on the CPU (or negligably less expensive) when it's every single packet for use in a queue tree. That's because every packet must go through mangle no matter what lookup method is used. connection marks are great to help bypass running the full firewall chain for known packets, but that doesn't apply for 'mark every packet' for queues because they are forced through mangle to get the mark.

Matching in the queue tree on IP doesn't have to go through mangle, it's ONLY the cost of the IP lookup. That ends up around 70% of the CPU cost of a heirachical queue tree using packet marks on an rb5009 or ccr2004.

This is what you wrote. I pointed out a solution so you don't need to match every packet anymore. Of course you'd still need to mark the packets. But the matching part is gone. You probably still hit a performance bottleneck, but that's a different story. Simple queue is something that works as you requested, there you can do IP matching. But unknown to me if that really has any performance gains.

you gotta look at the entire message not just one little bit. 'for packet marks' is doing all the heavy lifting here. simple queues lack the flexability required for a queue tree else I'd use those with IP matchers. simple queues are much faster and with ~200 IPs to track can be more than 2x the throughput.

packet marks are the slow point. I pointed that out and you can back with a smug comment.

You did not provide a solution at all, your suggestion doesn't even touch the issue of packet mark CPU cost. Marking connactions doesn't actually reduce CPU load at all because the packets still have to go through the chain, it actually increases it slightly. ie, now there is an IP lookup, then a connection lookup, THEN the firewall chain to get to mangle. it's a worse solution for marking all packets, though only slightly because those lookups aren't that expensive.

the issue remains, packet marks consume too many CPU resources when every packet needs inspected and every packet needs marked, which is required for queue tree matching. adding IP to a queue tree entry solves this because you don't need to hit mangle.

Mikrotik isn't going to put this on the roadmap without other users that need it saying it. This thread's purpose is to get it in front of people for that purpose.

How did you come to the conclusion that it's specifically packet marks that are slowing you down?

This would seem counter-intuitive. Mikrotik uses iptables, which registers all chain handlers any time it's loaded, so mangle is traversed even if empty. The connection tracking lookup (especially updating the status of entry) is indeed expensive, but that happens before the packet enters firewall prerouting, regardless of mangle or packet marking. The packet mark (I mean the field itself) is present regardless of whether it was set ot not.

thorough testing.

the packets passing down the firewall is expensive enough, and there's no way to skip that with any shaping on routeros. however, mangling the packets ads a 'pit stop' in mangle for the packet which requires more than just a copy, so there's the added CPU burn. Adding that packet mark costs CPU time, a copy to memory, etc. The packet itself doesn't get a mark, that's entirely an in memory mapping and the CPU has to do that.

You can demonstrate this by making a firewall rule matching a busy IP and set action to 'accept'. Take note of CPU usage. Now switch that to mark packet and see the CPU climb.

It's a 'cheap' process made expensive at >100kpps. and this issue is mostly a mikrotik one. On plain linux you can 'fasttrack' around most of the firewall without skipping queues.

Did you make sure to set passthrough=no on the marking rule, otherwise your tests would not be fair.

This sounded so strange to me that (resisting the urge to just dismiss the idea,) I did my own measurements.

The setup:
hAP ax2 on 7.25.2 (stable)
synthetic routed traffic, 512 byte udp packets, 50 kpps

To an empty firewall, with connection tracking turned on, I added the following rule (this rule matches the generated traffic):

/ip firewall mangle add chain=prerouting dst-address=192.168.80.3 action=passthrough

Then I replaced it with this:

/ip firewall mangle add chain=prerouting dst-address=192.168.80.3 action=mark-packet new-packet-mark=mymark

With passthrough the profile shows:

firewall              3  7%
[...]
cpu3                     77%

with mark-packet it shows:

firewall      3  6.5%
[...]
cpu3             74.5%

(of course the measurements jump around quite a bit.)

This makes sense considering that the packet is accompanied by a metadata struct during its travels through the firewall, which is allocated when it first enters it. This struct has the packet mark field allocated and set to the value corresponding to no-mark. When a mark-routing action is executed, the packet is not copied and no new metadata structures get allocated, the already allocated packet-mark field is merely updated to the new value.

What could actually be done is to make the connection mark available as a matched field in the queue, since this could be made available even when the packet is travelling in fasttrack.("could", as in: this isn't currently implemented, but possibly could be in the future.) is

That does make sense, but only if there are a ton of rules. But then surely address lists would be used instead...

I'd just point out you should be able to use no-mark as the mark in a queue, and then queues filter would apply. But I'd have to believe the firewall adding the mark for the matched traffic than always looking at all traffic in queue is cleaner/faster... So not recommending this method.

Also, something is doing a comparison. And it's comparision/matching, not marking, that likely more CPU cycles.

I think OP's chief complaint is that fasttrack and per-IP queueing are incompatible. This could possibly be solved by either making connection marks possible selectors, since fasttrack could have access to them (the marking happens on the initial or the first reply packets, where fasttrack doesn't yet kick in, and the mark is subsequently available from the conntrack entry itself.)

The other solution would be to allow direct IP selectors, as OP proposed.

Anyway, the performance problems are not specifically due to marking, but to the inability to bypass the full (slow) firewall path.

correct, and that is in use. no-mark as well as no passthrough to at least limit the number of matches. further, dividing things up into groupings and using jump rules also really helps with the RBAR model of mangle rules. Still traversing the whole firewall though.

The IP matching is a binary operation, literally 00001010.00010100.00110010.00000111/11111111.11111111.11111111.00000000 = %IP% is much 'cheaper' on the CPU than adding a packet mark to a reference table. This can be demonstrated rather easily but just making the opposing mangle rules and measuring the CPU and pps. Packet marking is multiple operations. matching first, then marking, then matching the mark. it's at a minimum 3x the CPU work.

well, getting fasttrack to drop back in before traffic shaping would be a fantastic tool also. That's not the specific request, but that would be super useful for all sorts of things. having to traverse the entire packetflow path is the pain point ultimately, and doing the multi-step process of finding packets, marking packets, then finding marks being an expensive part of that whole chain.

Note to my post above, IP matching itself is cheap, running through a thousand mangle rules to find a match is expensive and so connection marking would be a great place to start if we could match on connection and skip mangle and the rest of the flow right there. Better than my original request, but requires multiple changes.

The main benefit of connection marking is the ability to skip the list of matchers by the fasttrack rule at the top and then skip the rest of the firewall. Mangle on every packet is what kills connection marking's usefulness because we can never skip the packet flow, making connection marking an additional step.

If mikrotik would allow matching queues on connection marks AND CRITICALLY allowing fasttrack to drop out of that path right before route/nat lookup instead of after so queues would work, that would be the ultimate. Then, it would be one mangle RBAR traversal per connection and then skipping the vast majority of the CPU expensive bits and drop into the queue tree, matching on that connection mark and not packet mark or IP. Maybe a new queue tab that was specifically designed around a new fastpathqueue rule.