It’s commonplace in IT departments to stay on top of system patches and upgrades. There are internally developed standards as well as industry-defined processes (ITIL for example) that assist in managing the infrastructure. Is this true with product configuration as well within your organization? Each new release of product X undoubtedly introduces a different way of accomplishing the same goals. Sometimes this is due to a bug resolution, and sometimes it’s to take advantage of performance improvements. Often, the “old way” is preserved for compatibility, which shields users from unsuspected outages, but also allows configurations that are potentially sub-optimal to reside for years in the infrastructure. You should take this to heart with regards to iRules as well. Sometimes a new version necessarily “breaks” your iRule code going forward, but often it is supported. When you upgrade versions, I implore you to look at the new functionality and look at ways you might optimize and future-proof your iRules.
In this tech tip, I’m going to highlight three versions of an iRule that solves the same problem, but each subsequent version relies on improvements in TMOS. Note that version 1, whereas not at all an ideal iRule on the latest version of TMOS, will still run as is, albeit far less efficiently. All three rules are featured in the wiki:
Version 1 – Arrays and Global Variables
In the era before clustered multi-processing (pre v9.4), there was no concern with global variables. There’s always a concern with arrays, however, as a poor garbage collection logic, or, in some cases, no garbage collection logic, you can end up with out of control arrays that consume all memory and tank the box. But that’s what was available at the time.
<span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;">when</span> <span style="color: #cc6633;">RULE_INIT</span> {
<span id="lnum2" style="color: #606060;"> 2:</span> <span style="color: #0000ff;">set</span> ::maxquery 100
<span id="lnum3" style="color: #606060;"> 3:</span> <span style="color: #0000ff;">set</span> ::holdtime 600
<span id="lnum4" style="color: #606060;"> 4:</span> <span style="color: #0000ff;">array</span> <span style="color: #0000ff;">set</span> ::usertable { }
<span id="lnum5" style="color: #606060;"> 5:</span> <span style="color: #0000ff;">array</span> <span style="color: #0000ff;">set</span> ::blacklist { }
<span id="lnum6" style="color: #606060;"> 6:</span> }
<span id="lnum7" style="color: #606060;"> 7:</span>
<span id="lnum8" style="color: #606060;"> 8:</span> <span style="color: #0000ff;">when</span> <span style="color: #cc6633;">CLIENT_DATA</span> {
<span id="lnum9" style="color: #606060;"> 9:</span> <span style="color: #0000ff;">set</span> srcip [<span style="color: #0000ff;">IP::remote_addr</span>]
<span id="lnum10" style="color: #606060;"> 10:</span> <span style="color: #0000ff;">set</span> currtime [<span style="color: #0000ff;">clock</span> second]
<span id="lnum11" style="color: #606060;"> 11:</span> <span style="color: #0000ff;">if</span> { [ <span style="color: #0000ff;">info</span> exists ::blacklist($srcip) ] } {
<span id="lnum12" style="color: #606060;"> 12:</span> <span style="color: #0000ff;">if</span> { $::holdtime > [<span style="color: #0000ff;">expr</span> ${currtime} - $::blacklist($srcip) ] } {
<span id="lnum13" style="color: #606060;"> 13:</span> <span style="color: #0000ff;">drop</span>
<span id="lnum14" style="color: #606060;"> 14:</span> <span style="color: #0000ff;">return</span>
<span id="lnum15" style="color: #606060;"> 15:</span> } <span style="color: #0000ff;">else</span> {
<span id="lnum16" style="color: #606060;"> 16:</span> <span style="color: #0000ff;">unset</span> ::blacklist($srcip)
<span id="lnum17" style="color: #606060;"> 17:</span> }
<span id="lnum18" style="color: #606060;"> 18:</span> }
<span id="lnum19" style="color: #606060;"> 19:</span> <span style="color: #0000ff;">if</span> { [ <span style="color: #0000ff;">info</span> exists ::usertable(time,$srcip)] <span style="color: #0000ff;">and</span> $currtime == $::usertable(time,$srcip) } {
<span id="lnum20" style="color: #606060;"> 20:</span> <span style="color: #0000ff;">incr</span> ::usertable(freq,$srcip)
<span id="lnum21" style="color: #606060;"> 21:</span> <span style="color: #0000ff;">if</span> { $::usertable(freq,$srcip) > $::maxquery } {
<span id="lnum22" style="color: #606060;"> 22:</span> <span style="color: #0000ff;">set</span> ::blacklist($srcip) $currtime
<span id="lnum23" style="color: #606060;"> 23:</span> <span style="color: #0000ff;">unset</span> ::usertable(freq,$srcip)
<span id="lnum24" style="color: #606060;"> 24:</span> <span style="color: #0000ff;">unset</span> ::usertable(time,$srcip)
<span id="lnum25" style="color: #606060;"> 25:</span> <span style="color: #0000ff;">drop</span>
<span id="lnum26" style="color: #606060;"> 26:</span> <span style="color: #0000ff;">return</span>
<span id="lnum27" style="color: #606060;"> 27:</span> }
<span id="lnum28" style="color: #606060;"> 28:</span> } <span style="color: #0000ff;">else</span> {
<span id="lnum29" style="color: #606060;"> 29:</span> <span style="color: #0000ff;">set</span> ::usertable(freq,$srcip) 1
<span id="lnum30" style="color: #606060;"> 30:</span> <span style="color: #0000ff;">set</span> ::usertable(time,$srcip) $currtime
<span id="lnum31" style="color: #606060;"> 31:</span> }
<span id="lnum32" style="color: #606060;"> 32:</span> <span style="color: #0000ff;">pool</span> dnsserver
<span id="lnum33" style="color: #606060;"> 33:</span> }
The logic here is pretty straight forward. Two global variables are set, one for the maximum queries per second and the other for the amount of time that source will be prevented from a resolution response. Also in the RULE_INIT event, two arrays are built, the usertable where source IPs will be stored with their current counts, and then the blacklist where offending source IPs will be stored. The logic in the CLIENT_DATA event is best described in a workflow diagram:
Version 2 – Session Tables and Local Variables
Version two moves the variables from the global scope to local scope. This helps with CMP compatibility, but there is a performance cost due to the variables getting set on each connection instead of once when the iRule is initialized. Also in version two, arrays were scrapped in favor of the session table in hopes of CMP compatibility. Unfortunately, the session commands were not fully CMP compliant until version 10, so this approach is safer than arrays, but not as optimal on pre-v10 systems.
<span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;">when</span> <span style="color: #cc6633;">CLIENT_ACCEPTED</span> {
<span id="lnum2" style="color: #606060;"> 2:</span> <span style="color: #0000ff;">set</span> maxquery 2
<span id="lnum3" style="color: #606060;"> 3:</span> <span style="color: #0000ff;">set</span> holdtime 10
<span id="lnum4" style="color: #606060;"> 4:</span> }
<span id="lnum5" style="color: #606060;"> 5:</span> <span style="color: #0000ff;">when</span> <span style="color: #cc6633;">CLIENT_DATA</span> {
<span id="lnum6" style="color: #606060;"> 6:</span> <span style="color: #0000ff;">set</span> srcip [<span style="color: #0000ff;">IP::client_addr</span>]
<span id="lnum7" style="color: #606060;"> 7:</span> <span style="color: #0000ff;">set</span> c [<span style="color: #0000ff;">clock</span> second]
<span id="lnum8" style="color: #606060;"> 8:</span> <span style="color: #0000ff;">if</span> {[ <span style="color: #0000ff;">session</span> lookup uie <span style="color: #006080;">"b$c$srcip"</span> ] != <span style="color: #006080;">""</span>} {
<span id="lnum9" style="color: #606060;"> 9:</span> UDP::<span style="color: #0000ff;">drop</span>
<span id="lnum10" style="color: #606060;"> 10:</span> <span style="color: #0000ff;">return</span>
<span id="lnum11" style="color: #606060;"> 11:</span> }
<span id="lnum12" style="color: #606060;"> 12:</span> <span style="color: #0000ff;">set</span> f [<span style="color: #0000ff;">session</span> lookup uie <span style="color: #006080;">"u$c$srcip"</span>]
<span id="lnum13" style="color: #606060;"> 13:</span> <span style="color: #0000ff;">if</span> { $f != <span style="color: #006080;">""</span> } {
<span id="lnum14" style="color: #606060;"> 14:</span> <span style="color: #0000ff;">incr</span> f
<span id="lnum15" style="color: #606060;"> 15:</span> <span style="color: #0000ff;">if</span> { $f > $maxquery } {
<span id="lnum16" style="color: #606060;"> 16:</span> <span style="color: #0000ff;">for</span> { <span style="color: #0000ff;">set</span> i 2} { $i < [<span style="color: #0000ff;">expr</span> $holdtime + 2 ]} {<span style="color: #0000ff;">incr</span> i} {
<span id="lnum17" style="color: #606060;"> 17:</span> <span style="color: #0000ff;">session</span> add uie <span style="color: #006080;">"b$c$srcip"</span> b $i
<span id="lnum18" style="color: #606060;"> 18:</span> <span style="color: #0000ff;">incr</span> c
<span id="lnum19" style="color: #606060;"> 19:</span> }
<span id="lnum20" style="color: #606060;"> 20:</span> UDP::<span style="color: #0000ff;">drop</span>
<span id="lnum21" style="color: #606060;"> 21:</span> <span style="color: #0000ff;">return</span>
<span id="lnum22" style="color: #606060;"> 22:</span> } <span style="color: #0000ff;">else</span> {
<span id="lnum23" style="color: #606060;"> 23:</span> <span style="color: #0000ff;">session</span> add uie <span style="color: #006080;">"u$c$srcip"</span> $f 2
<span id="lnum24" style="color: #606060;"> 24:</span> }
<span id="lnum25" style="color: #606060;"> 25:</span> } <span style="color: #0000ff;">else</span> {
<span id="lnum26" style="color: #606060;"> 26:</span> <span style="color: #0000ff;">session</span> add uie <span style="color: #006080;">"u$c$srcip"</span> 1 2
<span id="lnum27" style="color: #606060;"> 27:</span> }
<span id="lnum28" style="color: #606060;"> 28:</span> }
The logic is only slightly different in that no arrays need to be unset, the sessions will just expire.
Version 3 – Table Command and Static Variables
BIG-IP version 10 introduced the static namespace and version 10.1 introduced the table command. The static namespace allows you to create global variables that are CMP compatible. Setting them once at initialization instead of on every connection is optimal, so this is a good change. The table command is a killer expansion of the session table functionality.
<span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;">when</span> <span style="color: #cc6633;">RULE_INIT</span> {
<span id="lnum2" style="color: #606060;"> 2:</span> <span style="color: #0000ff;">set</span> <span style="color: #0000ff;">static</span>::maxquery 100
<span id="lnum3" style="color: #606060;"> 3:</span> <span style="color: #0000ff;">set</span> <span style="color: #0000ff;">static</span>::holdtime 600
<span id="lnum4" style="color: #606060;"> 4:</span> }
<span id="lnum5" style="color: #606060;"> 5:</span> <span style="color: #0000ff;">when</span> <span style="color: #cc6633;">CLIENT_DATA</span> {
<span id="lnum6" style="color: #606060;"> 6:</span> <span style="color: #0000ff;">set</span> srcip [<span style="color: #0000ff;">IP::remote_addr</span>]
<span id="lnum7" style="color: #606060;"> 7:</span> <span style="color: #0000ff;">if</span> { [table lookup -subtable <span style="color: #006080;">"blacklist"</span> $srcip] != <span style="color: #006080;">""</span> } {
<span id="lnum8" style="color: #606060;"> 8:</span> <span style="color: #0000ff;">drop</span>
<span id="lnum9" style="color: #606060;"> 9:</span> <span style="color: #0000ff;">return</span>
<span id="lnum10" style="color: #606060;"> 10:</span> }
<span id="lnum11" style="color: #606060;"> 11:</span> <span style="color: #0000ff;">set</span> curtime [<span style="color: #0000ff;">clock</span> second]
<span id="lnum12" style="color: #606060;"> 12:</span> <span style="color: #0000ff;">set</span> key <span style="color: #006080;">"count:$srcip:$curtime"</span>
<span id="lnum13" style="color: #606060;"> 13:</span> <span style="color: #0000ff;">set</span> count [table <span style="color: #0000ff;">incr</span> $key]
<span id="lnum14" style="color: #606060;"> 14:</span> table lifetime $key 2
<span id="lnum15" style="color: #606060;"> 15:</span> <span style="color: #0000ff;">if</span> { $count > $<span style="color: #0000ff;">static</span>::maxquery } {
<span id="lnum16" style="color: #606060;"> 16:</span> table add -subtable <span style="color: #006080;">"blacklist"</span> $srcip <span style="color: #006080;">"blocked"</span> indef $<span style="color: #0000ff;">static</span>::holdtime
<span id="lnum17" style="color: #606060;"> 17:</span> table delete $key
<span id="lnum18" style="color: #606060;"> 18:</span> <span style="color: #0000ff;">drop</span>
<span id="lnum19" style="color: #606060;"> 19:</span> <span style="color: #0000ff;">return</span>
<span id="lnum20" style="color: #606060;"> 20:</span> }
<span id="lnum21" style="color: #606060;"> 21:</span> }
With each progression, notice the iRule length decreasing? Also, the readability is improved as well. Here’s the logic:
Conclusion
Surviving change is the most important step as you upgrade your infrastructure, but don’t forget to review your configurations for potential optimizations, particularly in your profiles and iRules as it relates to your BIG-IP infrastructure.
Related Articles
- v10.1 - The table Command - The Basics > DevCentral > F5 …
- v10.1 - The table Command - The Fine Print > DevCentral > F5 …
- v10.1 - iRules rate limiting with the table command > DevCentral …
- DevCentral Wiki: table
- vers. 10.1 “session” table limits and monitoring - DevCentral - F5 …
- viewing session table - DevCentral - F5 DevCentral > Forums …


