Irule CONFIGUR
Irule CONFIGUR
Irule CONFIGUR
Introduction
Components:
EVENT
CONDITION
ACTION COMMAND
Other Components
VARIABLES
OPERATORS
CONDITIONAL STATEMENTS
EVENT QUERY COMMANDS
Reference Links
Introduction
iRule is a powerful feature on BIG-IP platform that allows you to manipulate server and client
side traffic. It helps in intercepting and modifying traffic pattern depending upon changing
network conditions. Using iRule you can change application behaviour at TMOS instead of
rewriting the application code.
Its an easy to learn scripting language based on TCL. TCL was chosen considering its speed,
efficiency and ease of coding. Not all TCL commands are supported in iRule to avoid any kind
system level misbehaviour because of those commands. But it also adds some own set of
commands to it which helps engineers to code based on TMOS behaviour.
Using iRule you can Intercept, Inspect, Transform, Direct and Track traffic.
Components
EVENT
When traffic enters TMOS it flows through different connection states and an EVENT is
associated with each state. When connection enters a specific state, EVENT triggers.
Occurrence of some EVENTs is dependent on profile attached to VIP while some are global
EVENTs which are profile independent.
when EVENT_NAME
{
“code”
}
● CLIENT_ACCEPTED
● CLIENT_DATA
● HTTP_REQUEST
● LB_SELECTED
● SERVER_CONNECTED
● SERVER_DATA
● HTTP_RESPONSE
Global Events
when RULE_INIT {
code
}
CONDITION
Conditional statements in TCL code are used to define a condition. Return value will be a
boolean value (True or False) (Yes or No). Using conditionals statements, you can define
execution of code only when a condition is fulfilled.
when EVENT_OCCURS
{
If { CONDITION }
{
“Code”
}
}
ACTION COMMAND
Action commands defines action to be executed. These commands generally do not return any
value but perform tasks like redirection, logging, pool selection, dropping the connection etc.
Consider following example where we are manipulating pool selection and also logging a
string.
{
pool pool-object
log local0. “String”
}
Summary:
Following example gives us an idea about writing an iRule with all the components discussed
above.
when DAY_IS_SATURDAY
{
If { (Movie released?) AND (Are reviews good?) }
{
“go for movie”
}
else
{
“go for dinner”
}
}
Other Components
VARIABLES
Variable is a representation of data stored in memory. You can recall the data stored in
variable later in an iRule for traffic manipulation. Variables can be either static or dynamic.
Static Variables:
set num1 10
set num2 10.50
set mystring "hisenburger"
set list1 {1, 2, 3, 4, 5}
set list2 {"Vishnu", "Vishal", "Pawan", "Raju", "Madhu"}
Dynamic Variables:
set host_value [HTTP::host]
set uri_value [HTTP::uri]
Example:
https://www.facebook.com/login.php
host_value = www.facebook.com
uri_value = /login.php
OPERATORS
{
If { [HTTP::host] starts_with “face”]}
{
drop
}
}
CONDITIONAL STATEMENTS
Conditional statements are used to make sure that code is executed only when certain
conditions are met. In following examples, “if and else” and “switch” commands are used for
making sure we are executing code for the right connection.
If Statement
when CLIENT_ACCEPTED {
if { [TCP::local_port] == 80 } {
SSL::disable
pool HTTP-POOL
} elseif { [TCP::local_port] == 443 } {
pool HTTPS-POOL
}
else {
drop
}
}
Switch statement
when HTTP_REQUEST {
set CIP [IP::client_addr]
switch -glob $CIP {
"10.[0-255].[0-255].[0-255]" { log local0. "Private User from 10.0.0.0/8"}
"192.168.*.*" { log local0. "Private User from 192.168.0.0/16"}
default {log local0. "Public User"}
}
}
when HTTP_REQUEST {
set URI [HTTP::uri]
switch -glob $URI {
"*jpeg" -
"*png" -
"*gif" { log local0. "Graphic Content"}
default {log local0. "Non Graphic Content"}
}
}
When an event is triggered, an event query command is used to get important information
from traffic flowing through TMOS. For an example, when 3 way handshake between client
and F5 completes, CLIENT_ACCEPTED event triggers and we can use query command like
[IP::client_addr] which returns client IP address.
Event Query Options
HTTP_REQUEST header
host
method
URI
version
Cipher
Session ID
HTTP_RESPONSE data
cookie
header
payload
status code
In following example, code will not be executed but sequentially depending on which event
occurs first. Also in action command, query commands are used which will get connection
details from the established connection, but it only works when event and query command
are compatible.
when LB_SELECTED
{
log local0. "Pool member [LB::server_addr]"
}
when HTTP_RESPONSE
{
log local0. "Status code [HTTP::status]"
}
when CLIENT_ACCEPTED
{
log local0. "Client IP: [IP::client_addr]"
}
when HTTP_REQUEST
{
log local0. "User accessing [HTTP::uri]"
}
when HTTP_REQUEST {
if { [active_members HTTP-POOL] < 2 } {
HTTP::redirect http://maintenance-portal.html
}
when HTTP_REQUEST {
set host [HTTP::host]
switch -glob $host {
"abc" {HTTP::redirect "http://abc.com" }
}
}
when CLIENT_ACCEPTED {
#Check if requested port is outside 1000 - 2000
if { [TCP::client_port] < 1000 or [TCP::client_port] > 2000}{
drop
}
}
Example 4: Pool selection
when HTTP_REQUEST {
if { [HTTP::header User-Agent] contains "MSIE"}{
pool IE-POOL
}
elseif { [HTTP::header User-Agent] contains "MOZILA"}{
pool MZ-POOL
}
else {
pool DEFAULT-POOL
}
}
when HTTP_REQUEST {
if { [HTTP::uri] contains "windows" } {
pool WINDOWS-POOL
}
elseif { [HTTP::uri] contains "linux"}{
pool LINUX-POOL
}
else {
HTTP::redirect "http://sorrypage.com
}
}
when HTTP_REQUEST {
HTTP::redirect "https://[HTTP::host][HTTP::uri]"
}
Example 6: Sorry Page
when HTTP_RESPONSE {
if { ([HTTP::status] equals "404") or ([HTTP::status] equals "500") }{
HTTP::respond 200 content {
<HTML><TITLE>Sorry Page</TITLE>
<BODY>Site is under maintenance.</BODY></HTML>
}
}
}
Reference Links