iptables rules optimization

Post new topic   Reply to topic    DD-WRT Forum Index -> Advanced Networking
Goto page 1, 2, 3, 4, 5  Next
Author Message
Diosbejgli
DD-WRT Novice


Joined: 23 Aug 2008
Posts: 30

PostPosted: Sun Mar 21, 2010 19:18    Post subject: iptables rules optimization Reply with quote
Hi.
I was experimenting with eko's 13577 build for a WRT54GL (dd-wrt.v24-13577_NEWD_std-nokaid_nohotspot_nostor.bin) and I found out that some of the iptables rules could be better optimized by changing the order of certain rules. It might seem unimportant but the router's CPU isn't as powerful as one in a desktop PC and measuring the difference with iperf proved this. Interpreting and checking iptables rules uses CPU cycles

Original iptables rule order:
Code:
root@DD-WRT:~# iptables -L FORWARD -v -n
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 ACCEPT     47   --  *      vlan5   192.168.1.0/24       0.0.0.0/0
    0     0 ACCEPT     tcp  --  *      vlan5   192.168.1.0/24       0.0.0.0/0           tcp dpt:1723
    0     0 ACCEPT     0    --  br0    br0     0.0.0.0/0            0.0.0.0/0
    0     0 TCPMSS     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp flags:0x06/0x02 TCPMSS clamp to PMTU
    0     0 lan2wan    0    --  *      *       0.0.0.0/0            0.0.0.0/0
    0     0 ACCEPT     0    --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
[output omitted]


I strongly recommend moving the state RELATED,ESTABLISHED line to the first position in the FORWARD chain because if a connection already passed to the ESTABLISHED state it's not necessary to check each packet again for the same rules.
Also, the TCPMSS clamp line matches TCP SYN packets only which clearly can't be an ESTABLISHED state.

I've also made several tests with iperf to check how the transfer speed changes when I move the RELATED,ESTABLISHED rule to the first line in the chain.

Deleting the rule and inserting it as the first line:
Code:

iptables -D FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT

Code:
Default rules:
[ ID] Interval       Transfer     Bandwidth
[136]  0.0-10.0 sec  60.7 MBytes  50.9 Mbits/sec

Rule moved to first line:
[ ID] Interval       Transfer     Bandwidth
[164]  0.0-10.0 sec  66.3 MBytes  55.5 Mbits/sec


This test was made with iperf between 2 boxes on a 100 Mbps connection (different networks) and the router was doing NAT between them. The performance gain is caused by CPU needs to check less rules for the packets which are in the ESTABLISHED state.

I hope developers will consider changing the iptables rules' order in the factory default startup scripts. When the user or the UPnP daemon creates port forwarding rules they should also be created after the state RELATED,ESTABLISHED rule.


Last edited by Diosbejgli on Mon Mar 22, 2010 15:24; edited 1 time in total
Sponsor
autobot
DD-WRT Guru


Joined: 07 May 2009
Posts: 1596

PostPosted: Sun Mar 21, 2010 20:26    Post subject: Reply with quote
Assuming that output is predictable, that's a nice little increase in thruput. Where did you gain such knowledge of iptables? Or were you just experimenting?

If your correct in the order and it doesn't break anything, submit your find to the DD-WRT Trac so it can be implemented.....or ignored.

_________________
Eko Builds

BrainSlayer Builds

DD-WRT Changelog RSS Feed
Diosbejgli
DD-WRT Novice


Joined: 23 Aug 2008
Posts: 30

PostPosted: Sun Mar 21, 2010 22:18    Post subject: Reply with quote
I'm working with iptables for years and read through couple of documentation regarding these questions. Knowing how TCP is working and how a packet filter should handle its packets has nothing special to do with iptables anyway.

It certainly does not break anything because as you can see these are just ACCEPT rules, moving that rule to the first line will not drop anything. It just ensures that packets in already established flows to be accepted at the beginning and not wasting CPU cycles on the same rules for each packet.
phuzi0n
DD-WRT Guru


Joined: 10 Oct 2006
Posts: 10141

PostPosted: Sun Mar 21, 2010 22:41    Post subject: Reply with quote
There may be a fringe case that needs the TCPMSS above the RELATED/ESTABLISHED rule for urgent flagged out of band packets (they're part of that rule), although there are other rules above it which may break things in such a case and I'm not sure if such packets even need to be clamped...

Although it would be nice to have it higher up by default, I don't think it's realistic to expect it to stay there. There are many many guides that require inserting rules and they can't assume anything about the user's setup so they need to insert the rules at the top of the chain. The UPnP daemon could be rewritten to insert further down the chain, but then user inserted rules could still push the RELATED,ESTABLISHED rule below where UPnP inserts them.

I doubt the devs will want to restructure a bunch of code to try to keep it at the top. The most I would expect is that it might get moved higher by default, but more likely you'll just have to keep doing it yourself.

_________________
Read the forum announcements thoroughly! Be cautious if you're inexperienced.
Available for paid consulting. (Don't PM about complicated setups otherwise)
Looking for bricks and spare routers to expand my collection. (not interested in G spec models)
Diosbejgli
DD-WRT Novice


Joined: 23 Aug 2008
Posts: 30

PostPosted: Mon Mar 22, 2010 0:57    Post subject: Reply with quote
Having a flow of packets in an ESTABLISHED state is usually the most often. That's why that rule should be at the first lines. Because we are not dropping anything there, it will not cause harm and the changed order only helps free up CPU cycles.

User portforward entries could be inserted to the second line with iptables -I 2 ...
UPnP could do the same. Anyway, having it at the top by default would be better.
fggs
DD-WRT Guru


Joined: 28 Jan 2008
Posts: 1741

PostPosted: Mon Mar 22, 2010 1:48    Post subject: Reply with quote
That's why I asked for "iptables-restore format" instead of loading each rule with iptables command, but the devs doesn't seem interested in changing that. With "iptables-restore format" you don't need -I, because ruleset will be restored in sequence..
autobot
DD-WRT Guru


Joined: 07 May 2009
Posts: 1596

PostPosted: Mon Mar 22, 2010 3:02    Post subject: Reply with quote
fggs wrote:
That's why I asked for "iptables-restore format" instead of loading each rule with iptables command, but the devs doesn't seem interested in changing that. With "iptables-restore format" you don't need -I, because ruleset will be restored in sequence..


You mean you asked for it to be included and it isn't?

_________________
Eko Builds

BrainSlayer Builds

DD-WRT Changelog RSS Feed
fggs
DD-WRT Guru


Joined: 28 Jan 2008
Posts: 1741

PostPosted: Mon Mar 22, 2010 3:12    Post subject: Reply with quote
I started a topic about it, requesting that "Save as firewall" would convert whatever is typed there to iptables-restore format, but low interest from forum users.. perhaps is not a good idea anyway.. but would be nice to have a "firewall file" so you can edit all in one
crashfly
DD-WRT Guru


Joined: 24 Feb 2009
Posts: 2026
Location: Sol System > Earth > USA > Arkansas

PostPosted: Mon Mar 22, 2010 3:29    Post subject: Reply with quote
Hell, I am for anything that would improve the performance of the router in those cases. Since it will probably take a while to get this implemented (if it was even considered), what steps would need to be taken to make this happen on a default install (that possibly already has some firewall rules)?

Is it going to be as simple as placing the above listed commands:
Code:
iptables -D FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
as the last lines in the DD-WRT firewall script?

If it is that simple (since the firewall script will get run if anything in the firewall changes), then why don't we have it implemented in that manner for now?

_________________
E3000 22200M KongVPN K26
WRT600n v1.1 refirb mega 18767 BS K24 NEWD2 [not used]
WRT54G v2 16214 BS K24 [access point]

Try Dropbox for syncing files - get 2.5gb online for free by signing up.

Read! Peacock thread
*PLEASE* upgrade PAST v24SP1 or no support.
autobot
DD-WRT Guru


Joined: 07 May 2009
Posts: 1596

PostPosted: Mon Mar 22, 2010 4:10    Post subject: Reply with quote
fggs wrote:
I started a topic about it, requesting that "Save as firewall" would convert whatever is typed there to iptables-restore format, but low interest from forum users.. perhaps is not a good idea anyway.. but would be nice to have a "firewall file" so you can edit all in one


It would be good if there were an option to use iptables-save/restore, maybe the way (changing the default webif) you suggested it would not be optimal.

It could be done in "MyPage" without too much trouble I imagine....and speaking of MyPage, I wonder what Eko has been up to lately.

_________________
Eko Builds

BrainSlayer Builds

DD-WRT Changelog RSS Feed
phuzi0n
DD-WRT Guru


Joined: 10 Oct 2006
Posts: 10141

PostPosted: Mon Mar 22, 2010 4:19    Post subject: Reply with quote
crashfly wrote:
Is it going to be as simple as placing the above listed commands:
Code:
iptables -D FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
as the last lines in the DD-WRT firewall script?

Yes.

_________________
Read the forum announcements thoroughly! Be cautious if you're inexperienced.
Available for paid consulting. (Don't PM about complicated setups otherwise)
Looking for bricks and spare routers to expand my collection. (not interested in G spec models)
fggs
DD-WRT Guru


Joined: 28 Jan 2008
Posts: 1741

PostPosted: Mon Mar 22, 2010 4:55    Post subject: Reply with quote
@ autobot: Unfortunately dd-wrt doesn't have iptables-save...
autobot
DD-WRT Guru


Joined: 07 May 2009
Posts: 1596

PostPosted: Mon Mar 22, 2010 5:35    Post subject: Reply with quote
fggs wrote:
dd-wrt doesn't have iptables-save...


That is easily resolved by one of three ways.

1. Use jffs to save iptables-save/restore.

2. Use the firmware modkit to insert iptables-save/restore into the firmware permanently.

3. Build an install script for iptables-save/restore.

For me either 2 or 3 would be optimal.

I would have to look, but isn't iptables-save just a shell script anyway?

Previously I had tried to insert a uuencoded gzipped shell script (my miniupnpd installer) into an nvram variable and zcat it back through uudecode then sh, but haven't gotten this to work quite right. I like this method, I feel it would work (survive reboots), no need for jffs2 overhead, but this is not very safe. So I ended up just creating a firewall script that downloads the actual installation script and executes it upon each reload of the firewall, you could do something similar for iptables-save/restore.

_________________
Eko Builds

BrainSlayer Builds

DD-WRT Changelog RSS Feed


Last edited by autobot on Mon Mar 22, 2010 20:40; edited 1 time in total
fggs
DD-WRT Guru


Joined: 28 Jan 2008
Posts: 1741

PostPosted: Mon Mar 22, 2010 5:41    Post subject: Reply with quote
That would be nice, because once saved, you can edit even dd-wrt stock rules..

I just want it to load by reading the output file of iptables-save during router startup..
autobot
DD-WRT Guru


Joined: 07 May 2009
Posts: 1596

PostPosted: Mon Mar 22, 2010 6:16    Post subject: Reply with quote
I think there must be a reason why it isn't included, but if used at the proper time I don't see too much of a problem. The rules are generated dynamically based on nvram variables, so I think you would have to diff the old iptables-saved chains if you changed any setting from the webif before running iptables-restore....otherwise your rules would be incorrect (out of sync).

I think I'm too tired right now to figure out any kind of proper way to integrate save/restore reliably. Diff is probably not included either.

I just realized this is not what you want, you want DD-WRT to save your webif entries in iptables-restore format and call the command after creating the default rules....therefore inserting your rules you entered from the webif alongside the defaults. The firmware modkit would be necessary I think, you would need to edit the startup scripts....prewall I believe is the script that would hold the key.

_________________
Eko Builds

BrainSlayer Builds

DD-WRT Changelog RSS Feed
Goto page 1, 2, 3, 4, 5  Next Display posts from previous:    Page 1 of 5
Post new topic   Reply to topic    DD-WRT Forum Index -> Advanced Networking All times are GMT

Navigation

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum