Forum Discussion
Chris_Phillips
Nimbostratus
Aug 03, 2006iRule as stored function or similar
Hi,
I have an iRule with a dozen lines or so and amd really hoping that there is some way to use this one single iRule as a stored function for instances of it. I mean, i will be using seperate instances of this iRule maybe 20 times, and would really like to be able to call the function with different parameters for each instance rather than having to copy all the code each time, e.g. "call my_iRule(param1, param2)".
I'm not that hopeful, but hoping to be amazed!
Thanks
Chris
- Colin_Walker_12Historic F5 AccountThere isn't currently a mechanism for calling iRules in a function-like manner as you described. The closest I've seen people come is to make the iRule read from a class to fill in the info specific to the Virtual it's assigned to, then create just that class of info for each Virtual.
- unRuleY_95363Historic F5 AccountActually, we've discussed this quite a bit in designing iRules.
- Chris_Phillips
Nimbostratus
these sound very interesting, but do you have any examples? i generally undestand the class principle, presumably you'd need something identifiable like the virtual server address and/or port which would let you identify a single instance. As far as second suggestion, that's really gone way over my head... intersting sounding words but really wouldn't know where to start! - JRahm
Admin
Check out this post - Deb_Allen_18Historic F5 AccountTo re-use the same rule with different variable values for different virtuals without creating multiple instances, I've used this approach with good results:
Then you can include this code in your rule to extract the variable values based on the virtual upon which the traffic is processed (use in any event but RULE_INIT):class cl_VSVars { vs_VirtualServer1 valueA valueB valueC vs_VirtualServer2 valueA valueB valueC vs_VirtualServer3 valueA valueB valueC }
If you want to store the class name in a variable, you can do so like this:set myVSVars [findclass [virtual name] cl_VSVars] set myVar1 [getfield $myVSVars " " 1] set myVar2 [getfield $myVSVars " " 2] set myVar3 [getfield $myVSVars " " 3]
HTHwhen RULE_INIT { set ::variableClass ::VSVars } when CLIENT_ACCEPTED { set myVSVars [findclass [virtual name] [set $::variableClass]] ... }
- Chris_Phillips
Nimbostratus
Thanks guys, this all looks great. - Chris_Phillips
Nimbostratus
Hi,when RULE_INIT { number of times an ip address can be used between lookups set ::max_usage_count 3 array of resolved ip addresses array set ::server_ipaddr { } array of resolved address uses array set ::usage_count { } log local0. "Completed RULE_INIT on lookup" } when CLIENT_ACCEPTED { initialize usage count if not yet done for this VS if { [ info exists ::usage_count([virtual name]) ] == 0 } { set ::usage_count([virtual name]) 0 } use server_ipaddr if it exists, use dummy localhost if not if { [ info exists ::server_ipaddr([virtual name]) ] == 1 } { node $::server_ipaddr([virtual name]) [getfield [virtual name] "_" 3] } else { this WILL fail, but only occurs on very first usage when no IP address is known yet. node 127.0.0.1 } decrement usage_count for this virtual server incr ::usage_count([virtual name]) -1 call lookup if maximum usages has happened and reset usage counter if { $::usage_count([virtual name]) <= 0 } { set ::usage_count([virtual name]) $::max_usage_count NAME::lookup [getfield [virtual name] "_" 2] } } when NAME_RESOLVED { assign resolved name to server_ipaddr array if {[scan [NAME::response] "%d.%d.%d.%d" a b c d] == 4 } { set ::server_ipaddr([virtual name]) [NAME::response] } }
- JRahm
Admin
Remember too that you can test all your variations against the timing command to find the most efficient solution specific to your environment:rule my_fast_rule { timing on when HTTP_REQUEST { Do some stuff } }
rule my_slow_rule { when HTTP_REQUEST timing on { Do some other stuff } }
- hoolio
Cirrostratus
Chris, - Chris_Phillips
Nimbostratus
Yeah that would be fairly obvioous now wouldn't it? i only used that as that's what was added to my rule when it was converted to a wiki entry.
Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects