Tech Tips on DevCentral
   
You are here: Tutorials > Tech Tips

Current Articles | Search | Syndication

iRules Optimization 101 - #01 - if, elseif and switch

by Colin Walker - 14969 views Article Rating

A question that comes up often when a member of the DC Team is out and about giving a talk is "How can I optimize my iRules?". Well, this series, "iRules Optimization 101" is intended to walk you through some of the ways in which you can do just that. Other articles in the series:

Just like the popular iRule Security 101 series, this series will start off with some simple tips and related examples, and then work up to more complex optimization techniques to help you keep your code lean and mean. There is an excellent document called "How To Write Fast Rules" that is located here which gives an overview of several things you can do to optimize your code. This is a great starting point and we're going to cover many of the same topics in this series, with more detail and examples, as well as branching out some from the current doc.

For this first installation we're going to start out with a comparison of "if" chains vs. "if/elseif" chains and some examples of "switch" usage. I know this is pretty basic stuff, but it's important to put the building blocks in place before moving to more advanced concepts.

Section One: if vs. if/elseif

One of the most basic logical operators that we offer is the "if" operator. This simple comparison is often used multiple times throughout a single rule and it's not rare to see strings of if comparisons together, such as:

 
when CLIENT_ACCEPTED { 
  if {[IP::client_addr] equals "192.168.5.42" } { 
    pool pool1 
  } 

  if {[IP::client_addr] equals "192.168.5.43" } { 
    pool pool2 
  } 
  
  if {[IP::client_addr] equals "192.168.5.44" } { 
    pool pool3 
  } 
... 
} 

While this is technically correct, and will function as desired, it is not by any means efficient. The logic here is checking each IP address in turn to determine which pool to send the client to. The issue is, even after a match has been made, the code is going to continue processing and checking every other IP address, even though we know we've already found the correct pool.

Instead, we implement a slightly altered logic set, using if and elseif. We can also make use of a simple "else" clause to finish this string of if/elseif checks, thereby defining a default action to perform if none of the above statements match.

 
when CLIENT_ACCEPTED { 
  if {[IP::client_addr] equals "192.168.5.42" } { 
    pool pool1 
  } elseif {[IP::client_addr] equals "192.168.5.43" } { 
    pool pool2 
  } elseif {[IP::client_addr] equals "192.168.5.44" } { 
    pool pool3 
  } else {
    pool default_pool
  }
} 

In this manner we can ensure that once we've found the matching IP address we no longer continue to process every if statement. This means we don't have to evaluate the logical statement in each if, which can save us valuable CPU time.

Section Two: switch

An even more efficient manner of dealing with multiple if statements is to get rid of them all together! No, I don't mean remove the logic from your rule, I just mean use switch instead. The switch command is more efficient, in general, than using a string of if or even if/elseif statements. It's also flexible in the matching that you can do. The above example can be simplified by making use of the switch command to look like:

 
when CLIENT_ACCEPTED { 
  switch [IP::client_addr] { 
    192.168.5.42 { pool pool1 } 
    192.168.5.43 { pool pool2 } 
    192.168.5.44 { pool pool3 } 
    default { pool default_pool }
  } 
... 
} 

As you can see this continues to simplify the code, as well as optimize it. Notice the "default" statement in the switch command replaces the functionality of the last "else" statement in the "if/elseif" chain. If none of the first logical statements match, the default action is performed. The real power of switch becomes more obvious when you start getting into some of the other things you can do with the command like glob style matching and nested comparisons. Something slightly more advanced that uses both of these might look like:

when CLIENT_ACCEPTED { 
  switch -glob [IP::client_addr] { 
    "192.168.4.24" -
    "192.168.4.25" - 
    "192.168.5.*" { 
      append myPool "pool" [getfield [IP::client_addr] "." 4]
      pool $myPool
    } 
    "192.168.6.4*" { 
      if { [active_members backupPool] > 2 } {
        pool backupPool
      }
    } 
    default { 
      log local0. "[IP::client_addr] fell through, sending to defaultPool"
      pool defaultPool
    }
  } 
} 

As you can see, there is a lot of potential with this for complex logic structures. I'll leave learning about the rest of the tricks up to you, though. ;) You can read more about switch here

Get the Flash Player to see this player.


Rate This Article:

COMMENTS

There are currently no comments, be the first to post one.
Only registered users may post comments.
  

TechTips by Category

Filter by:
ARX Import Restrictions for NetApp Volumes by jmccarron (30 Views)
Two-Factor Authentication With Google Authenticator And APM by watkins (297 Views)
Controlling a Pool Members Ratio and Priority Group with iControl by Joe (243 Views)
F5 ARX WAN Optimization with BIG-IP WAN Optimization Manager (WOM) by mfabiano (284 Views)
iRules Concepts: Tcl, The How and Why by Colin (957 Views)
Populating Tables With CSV Data Via Sideband Connections by watkins (958 Views)
Introduction to iStats Part 1: Overview by Colin (1069 Views)
Google reCAPTCHA Verification With Sideband Connections by watkins (1430 Views)
Transparent Web Application Bot Protection by Joe (1266 Views)
v11: iRules Data Group Updates by citizen_elah (2927 Views)
HTTP Request Cloning via iRules, Part 1 by Colin (1342 Views)
Managing Ramcache Entries with Pycontrol by citizen_elah (909 Views)
iRules Concepts: Connection States and Command Suspension by Colin (1727 Views)
v11.1: DNS Blackhole with iRules by citizen_elah (2089 Views)
CodeShare Refresh: HTTP Session Limit by Colin (1400 Views)
Page 1 of 4First   Previous   [1]  2  3  4  Next   Last   
  

Most Viewed Tech Tips

Mitigating Slow HTTP Post DDoS Attacks With iRules by watkins
(6045 Views) Published on Friday, November 05, 2010
APM Session Invalidation Using ASM by Colin
(6030 Views) Published on Monday, October 17, 2011
Web Application Login Integration with APM by Colin
(5800 Views) Published on Monday, April 18, 2011
iRules Data Group Formatting Rules by citizen_elah
(5490 Views) Published on Tuesday, March 29, 2011
Multiple Certs, One VIP: TLS Server Name Indication via iRules by Colin
(5422 Views) Published on Tuesday, April 05, 2011
One Time Passwords via an SMS Gateway with BIG-IP Access Policy Manager by citizen_elah
(5386 Views) Published on Tuesday, February 08, 2011
BIG-IP APM–Customized Logon Page by citizen_elah
(5104 Views) Published on Tuesday, June 21, 2011
SSL Profiles Part 3: Certificate Chain Implementation by citizen_elah
(5050 Views) Published on Wednesday, December 01, 2010
SSL Profiles: Part 1 by citizen_elah
(4747 Views) Published on Wednesday, November 17, 2010
v11: RDP Access via BIG-IP APM–Part 3 by citizen_elah
(4715 Views) Published on Tuesday, October 04, 2011
Automating Web App Deployments with Opscode Chef and iControl by watkins
(4565 Views) Published on Friday, July 08, 2011
  

 Top Contributors

Techgeeeg
fatmcgav
mikand
MarkM
Chris Phillips
genseek
K-Dubb
adiezma
Ankush Narang
Beinhard
  

93,050 Members in 191 Countries and Growing!

Join DevCentral Today!

About DevCentral

F5 DevCentral is your source for the best technical documentation, discussion forums, blogs, media and more related to application delivery networking.

So dive in, meet your peers, and get familiar with DevCentral. We hope it makes your job easier and helps you get more from your F5 investment. If new to DevCentral, check out the Getting Started section. And if you have any problems, or think something could be easier to use, let us know.

Got It !

We've received your comment and transmitted it directly to DevCentral HQ.

Thanks for taking time to let us know what's on your mind. At DevCentral | Community Matters!

Get In Touch With Us

Have questions, suggestions or just want to get something off your chest?

Use our handy form below to Direct Connect with DevCentral Mission Control.

Send Us Feedback      or