Forum Discussion

Stevenson_88156's avatar
Stevenson_88156
Icon for Nimbostratus rankNimbostratus
Jan 14, 2011

RULE_INIT

Hi

 

 

I am a newbie when it comes to iRULES and when I am reading the about the description for the RULE_INIT event, it says the following:

 

 

 

"Triggered when an iRule is added or is modified. Specify this event to initialize global variables used inside iRules."

 

http://devcentral.f5.com/wiki/defau..._INIT.html

 

 

 

I also read some few articles that any variables declared in this event are automatically global and that global variables has issues in F5 version 10 due to CMP (ie. Global variables exist within a single TMM).

 

 

 

So my question is when you first create and save an iRULE and has the event RULE_INIT, would it automatically load the global variable in the TMM?

 

 

 

Also, I also read about the use of "static" that could potentially fix the CMP issue, would that mean that once I created and save the iRULE, it would load the global variable in memory.

 

 

 

So in theory, I can just create one iRULE with the event RULE_INIT and load all my "static" global variables there once without referencing the iRULE to any virtual server. Then never have to put the RULE_INIT in any iRULES I have in the future?

 

 

 

Thanks.

 

  • Good explanation Aaron! The wiki article explained it relatively well but I was definitely confused about whether the rule had to be applied to a Virtual Server too.
  • Thank you very much Aaron. That really cleared a lot of things up about RULE_INIT and static global variables.
  • Hi Guys,

     

     

    Can you clarify some use cases for me please. My understandind is that global variables are always scoped to the virtual but I got asked the following.

     

     

    Case 1.

     

    If * separate* iRules A and B, both using the RULE_INIT event, applied to different virtual's just happen to use global variables of the same name, will one overwrite the values of the other when RULE_INIT is triggered for one of them? In other words are global variables shared across all virtual's by default or only on the virtual to which it is applied.

     

     

    Case 2.

     

    If the *same* iRule is applied across more than one virtual do all those virtual's then share the same RULE_INIT global variable space. If virtual A modifies global variable X does virtual B and C see that modification if they use the same iRule.

     

    Case 3.

     

    Case 2 begs the question, when you save an update to the iRule is the system saving an internal copy per virtual? In other words the globals and other variables are only scoped to that virtual.

     

    Case 4.

     

    If case 2 is true, does the command [virtual] have any meaning in the context of RULE_INIT. Can it be used in this event?

     

     

    I'll start testing these as well.

     

  • Hi Kevin,

     

     

    Case 1: Any variable declared in RULE_INIT is global in scope. There would be trampling across RULE_INIT events if the RULE_INIT event set a variable with the same name to different values. The last RULE_INIT to run would set the value.

     

     

    Case2: Yes and yes.

     

     

    Case 3: No, global variables and any variable set in RULE_INIT are shared across any virtual server and any connection.

     

     

    Case 4: No, [virtual name] cannot be run in the RULE_INIT event as it's global in scope and not run in the context of a virtual server or connection.

     

     

    The other case not discussed yet is that local variables in the same or separate iRules attached to the same virtual server will also overwrite each other.

     

     

    Basically, if you want to ensure that variables aren't trampled you should name them uniquely across iRules. For example, I no longer use a generic $debug or $static::debug variable. If I did this across all iRules and changed one rule to enable debug, I'd get debug logging enabled on all of them. Instead I create a unique per-iRule debug name like $static::cookie_debug.

     

     

    Hope this helps,

     

    Aaron
  • Aaron,

     

     

    That helps a great deal. Thank you for clarifying for me. Mind if I update the Wiki page as this is not clear at all. I did some testing since I asked and this showed me that globals really are across the entire F5. I suppose the last question is are global variables synced between HA pairs?

     

     

    Thanks

     

  • Done. Please have a look over the Wiki update for accuracy.

     

     

    https://devcentral.f5.com/wiki/iRules.RULE_INIT.ashx

     

     

    Are global variables synced between HA pairs? I know session tables are.

     

     

    Many thanks Aaron.
  • Thanks for updating the RULE_INIT page. No, global variables aren't synched to a peer unit as they're not part of the session table.

     

     

    Aaron