LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Security
User Name
Password
Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.

Notices


Reply
  Search this Thread
Old 11-23-2011, 03:03 AM   #1
Guigoune31
LQ Newbie
 
Registered: Nov 2011
Posts: 4

Rep: Reputation: Disabled
Question IPTables optimization


Hello all !

I am running an IPTables with 20000+ rules. This of course is quite a high number, and I have performance issues (packet drops) which I suspect may be caused by this.

In order to reduce the number of rules, I am thinking of using the "multiport" module, which could save quite a number of iptables entries.

I am however wondering if this will indeed bring improvement, as I do not know whether "multiport" is a "usability" enhancement, or if it actually reduces the number of checks that happen when a packet is received.

If the use of multiport allows me to have 15000 rules instead of 20000, will I actually get performance gains?

Thanks !
 
Old 11-23-2011, 03:53 AM   #2
fukawi1
Member
 
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 193Reputation: 193
As I understand things (and i stand to be corrected)

When a packet is processed by iptables, it is checked against each rule, until one is matched.

So I would imagine, that minimising the number of rules to check each packet against, would be of some help to increasing performance.

I would suggest:
-> using multiport and iprange modules where applicable, to reduce the over all rule count.
-> reduce jumping backwards (-j RETURN) and forwards(-j *) between chains (if you do much of that).
-> put your related and established rules as close to the top as possible.
-> prioritizing your rules so that the most frequently used rules are close to the top, (web/email/etc).

But this is coming from somebody with 160 lines worth of iptables script, so I probably wouldn't pay much attention to me :P
 
Old 11-23-2011, 04:01 AM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Indeed rule order and minimization should be done first. Also see Ipset / Iplist: http://www.linuxquestions.org/questi...kernel-912292/
 
Old 11-23-2011, 04:09 AM   #4
Guigoune31
LQ Newbie
 
Registered: Nov 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanks a lot for these answers.

I do not have IP ranges (and can't actually) and I only go "forth" in chains (and not back and forth). I do not use "state" rules either. So far the only minimization option I have is the multiport.

If you think that reduces the number of checks, and that there is no important tradeoff in terms of CPU loads, I'm happy.

Do not hesitate to give more opinions !

Cheers,

G.
 
Old 11-23-2011, 05:50 AM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Of course if you would have a single line for each IP address that has a different destination port like 20, 21, 22, 25 and 80 then using multiport would make sense. Other than that, without any relevant details there's no sense in telling you what to do or expect. Wrt performance and solutions check Netfilter Performance Testing (PDF), chapter 4.3 "Performance dependency on the number of rules" and see which solutions exist. If you dismiss them you should do that based on empirical evidence.
 
1 members found this post helpful.
Old 11-23-2011, 06:57 AM   #6
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 4,070

Rep: Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897
Quote:
Originally Posted by Guigoune31 View Post
Hello all !

I am running an IPTables with 20000+ rules...If the use of multiport allows me to have 15000 rules instead of 20000, will I actually get performance gains?
For me (and bear in mind that I am not in the position that you are to test the particular rule constellation on the hardware that you have and with the load that you have) that sounds like way too many. However, I need to point out the the number of rules is not a strict determinant of performance.

For a simple explanatory case:

Imagine a situation in which you do lots of ip address filtering on a certain type of packet
  • if, for example, you can ensure that the majority of packets do not pass through this (over-)long filtering sequence, it may have little impact on the overall throughput of the box (assuming that throughput is your main performance criterion)
  • OTOH, you may have a situation in which all of the packets do go through an extended filtering sequence, and then that might cause a throughput impact, even though the total number of rules is fewer than the previous case

So, I would recommend several things
  • Think carefully whether you really need to do all this filtering...what would go wrong if you didn't do it?
  • Can you design the system such that most of the packets do not hit the long chain? (eg, look carefully at packet stats, and whether the most common packets can go off into short chains and by looking at the order of comparisons, ensure that only the packets for which it is really needed, do they go through the long chain)
  • There may be a case for splitting the workload
  • Can you confirm that the perf shortfall is actually connected to the dropped packets?
  • Given that I've never played with Ipset, I'd put that off until last; I'm sure that it is a very worthwhile option, I'm just very conservative about that kind of 'leap in the dark' option (an option where I can't really be sure about the downsides until I have committed a lot of time to it). I'm sure that is just me being conservative, and someone will turn up and tell you how easy it was and how it solves all problems, without introducing any new issues. Won't they?

I believe in the 'short, stubby chains are best' approach to iptables performance (and that some people just add rules, one after another, without ever thinking about efficiency), so I'd tend to try to push in that direction, but, like fukawi1, you are way out of what would be my comfort zone, here.

Another thing to note would be that some Iptables modules are not exactly lightweight, and going at eg, conntrack or some of the fancier and more obscure matching modules, like a starving man at an all you can eat buffet can cause performance limitations even before you get to very high numbers of rules. (Something I should have thought of earlier; do ensure that the box on which this runs is not memory-starved; if the box is memory starved, and, at periods of peak load and using whatever set of complex rules and modules this causes it to start swapping vigorously, the performance will be reduced by the swapping activity, and that is almost certain to start causing problems.)

And if you really, really need all that filtering and the majority of the packets have to pass through it, Ipset may just be your best option.
 
Old 11-23-2011, 07:33 AM   #7
Guigoune31
LQ Newbie
 
Registered: Nov 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanks for all these very valuable comments.

I now feel I have not been precise enough. I have 20000+ rules in total, but they are not all in one sequence.

Instead, I have a set of about 4000 rules, each of them pointing to a different chain with averagely 4 rules each. So at most the packets are checked against about 4000+4 rules at most.

But my main question seems to have been answered, which is, using multiport to consolidate rules with same IP source and destination and different ports could bring improvement, and at least would not make things worse.

Last edited by Guigoune31; 11-23-2011 at 08:22 AM.
 
Old 11-23-2011, 12:28 PM   #8
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 4,070

Rep: Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897
Quote:
Originally Posted by Guigoune31 View Post
Instead, I have a set of about 4000 rules, each of them pointing to a different chain with averagely 4 rules each. So at most the packets are checked against about 4000+4 rules at most.
Sorry, I jumped to conclusions. The only way that I could imagine anyone coming up with anything like that number of rules was to imagine that you had one big long list of bad addresses that you were trying to filter out. Should have asked more questions before replying...
 
Old 11-23-2011, 09:51 PM   #9
fukawi1
Member
 
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 193Reputation: 193
Out of curiosity, can I ask why so many?
 
Old 11-24-2011, 04:59 AM   #10
Guigoune31
LQ Newbie
 
Registered: Nov 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
4000 individual users with an individual rule set each.
 
Old 11-24-2011, 08:09 PM   #11
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Since pictures speak louder than words: Iptables rule traversal: bandwidth at >= 10K of IP addresses
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
memory optimization smartgupta Linux - General 1 04-20-2011 07:35 AM
iptables error in android: iptables-save and iptables-restore not working preetb123 Linux - Mobile 5 04-11-2011 01:56 PM
iptables v1.2.9: Unknown arg `/sbin/iptables' Try `iptables -h' or 'iptables --help' Niceman2005 Linux - Security 4 12-29-2005 08:20 PM
Optimization Question dr_dre Linux From Scratch 4 11-15-2005 10:15 AM
kde optimization? alaios Linux - General 2 09-14-2003 02:55 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Security

All times are GMT -5. The time now is 10:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration