Hi,
Here is final version of my iRule that seems to be working for both rerouting traffic to another VS (for testing performance of different AAM WA profiles) as well as returning to using switching VS (one with this iRule attached) after traffic was rerouted to another VS.
Probably not the best iRule in the world (still learning) but maybe will be of some use for others 🙂
when CLIENT_ACCEPTED {
Trick (not working when clientssl profile not assigned to VS, so not working for pass-through https VS
It makes code more universal by using correct protocol set for VS with iRule attached
(http, https) in 302 redirect used later on
if { [PROFILE::exists clientssl] == 1} {
set proto "https"
}
else {
set proto "http"
}
log local0. "Scheme is $proto"
}
when HTTP_REQUEST {
Insert Cookies for policy switching
set setcookie ""
log local0. "Cookie in setcookie is $setcookie"
log local0. "Client [IP::client_addr] connected from switch"
switch [string tolower [HTTP::uri]] {
"/none" {
set setcookie " \"Set-Cookie\" \"X-WA-Policy=none; Expires=Thu, 01 Jan 1970 00:00:01 GMT\""
}
"/tcp" {
set setcookie " \"Set-Cookie\" \"X-WA-Policy=tcp\""
}
"/compress" {
set setcookie " \"Set-Cookie\" \"X-WA-Policy=compress\""
}
"/ibr" {
set setcookie " \"Set-Cookie\" \"X-WA-Policy=ibr\""
}
"/img" {
set setcookie " \"Set-Cookie\" \"X-WA-Policy=img\""
}
"/reorder" {
set setcookie " \"Set-Cookie\" \"X-WA-Policy=reorder\""
}
"/spdy" {
set setcookie " \"Set-Cookie\" \"X-WA-Policy=spdy\""
}
"/http2" {
set setcookie " \"Set-Cookie\" \"X-WA-Policy=http2\""
}
"/oc" {
set setcookie " \"Set-Cookie\" \"X-WA-Policy=OneConnect\""
}
}
Redirect is necessary so after using path from URI entered in browser
it's reset in the browser and subsequesnt request are not using this fake path
if { [string length $setcookie] > 0 } {
log local0. "New cookie created $setcookie"
HTTP::uri "/"
set cmd "HTTP::respond 302 Location \"$proto://[HTTP::host][HTTP::uri]\" $setcookie"
log local0. "Redirect is $cmd"
eval $cmd
return
}
URI for checking what is current state of switching - what cookie is set in requests
if { [string tolower [HTTP::uri]] eq "/current" } {
HTTP::respond 200 content "Current cookie setting for X-WA-Policy is: [HTTP::cookie "X-WA-Policy"]"
}
set vip "/Common/v.dc.wa_default_0.app/v.dc.wa_default_0_vs"
log local0. "Cookie in request is [string tolower [HTTP::cookie "X-WA-Policy"]]"
switch [string tolower [HTTP::cookie "X-WA-Policy"]] {
"tcp" {
set vip "/Common/v.dc.wa_default_0.app/v.dc.wa_default_0_vs"
COMPRESS::disable
}
"compress" {
set vip "/Common/v.dc.wa_compress_1.app/v.dc.wa_compress_1_vs"
COMPRESS::enable
}
"ibr" {
set vip "/Common/v.dc.wa_ibr_2.app/v.dc.wa_ibr_2_vs"
COMPRESS::enable
}
"img" {
set vip "/Common/v.dc.wa_img_3.app/v.dc.wa_img_3_vs"
COMPRESS::enable
}
"reorder" {
set vip "/Common/v.dc.wa_reorder_4.app/v.dc.wa_reorder_4_vs"
COMPRESS::enable
}
"spdy" {
set vip "/Common/v.dc.wa_spdy_5.app/v.dc.wa_spdy_5_vs"
COMPRESS::enable
}
"http2" {
set vip "/Common/v.dc.wa_http2_6.app/v.dc.wa_http2_6_vs"
COMPRESS::enable
}
"OneConnect" {
set vip "/Common/v.dc.wa_oc_7.app/v.dc.wa_oc_7_vs"
COMPRESS::enable
}
default {
If URI path is "/" use switching VS itself (one with this iRule attached), do not switch to other VS
set vip ""
log local0. "LBSelect is [LB::select]. The default switch used"
LB::detach is necessary when no OneConnect is used for switching VS
After some VS is chosen using virtual command it will stick until another virtual comman is performed
In case when switching VS should be used after using another VS connection to previous VS has to be detached
Then instead of sending traffic to previously selected VS, traffic is again processed by switching VS
LB::detach
COMPRESS::disable
}
}
log local0. "Virtual selected is $vip"
log local0. "Current virtual server name: [virtual name]"
Use virtual command only if VS other than switching VS should be used for handling traffic
if { [string length $vip] > 0 } {
virtual $vip
}
}
when HTTP_RESPONSE {
Set cookie only if VS other than switching VS should be used for handling traffic
if { [string length $vip] > 0 } {
HTTP::header insert "X-DC-Virtual" $vip
}
}
Of course all credits goes to Jason Rahm who provided this iRule in
Project Acceleration: Test Infrastructure
Many thanks to you John as well for helping with this issue. Hope to find same time to test your version as well.
Piotr