# CMP compatible strict Round Robin

**URL:** <https://community.f5.com/t/cmp-compatible-strict-round-robin/4014>\
**Category:** Technical Forums\
**Tags:** dev, irules, devops, application-delivery\
**Created:** [November 2, 2010, 2:49pm UTC](https://community.f5.com/t/cmp-compatible-strict-round-robin/4014 "2010-11-02T14:49:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AppleBee\_108607](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@AppleBee\_108607](https://community.f5.com/u/AppleBee_108607)\
**Post date:** [November 2, 2010, 2:49pm UTC](https://community.f5.com/t/cmp-compatible-strict-round-robin/4014/1 "2010-11-02T14:49:19Z")

</div>

Hi,

In addition to my another post, I have to urgently write an iRule to facilitate strict Round Robin event if the Virtual is CMPenabled.

I wrote code as follows but still have two difficulities:

(1) Initializing data in table

It seems “table” command cannot be used in RULE\_INIT event.

(2) I’m not clear how to specify variable type: numeric or string (Ooops..)

and having difficulty in calculating variables.

(3) Want to set general variables in RULE\_INIT

--\> Probably I can just use “static::xxx”

Excuse me for newbee question.

--------------------------------

when RULE\_INIT {

}

when CLIENT\_ACCEPTED {

set poolname “test-pool”

set num\_pool\_member 4

set pool\_port 80

set ip\_pm0 “10.100.11.11”

set ip\_pm1 “10.100.11.12”

set ip\_pm2 “10.100.12.11”

set ip\_pm3 “10.100.12.12”

get next poolmember’s index

set last\_mod [table lookup -subtable “rr\_table” “last\_mod”]

---- Here, not sure how to set correct data tye ------

set new\_mod [expr { expr { $last\_mod + 1 } % $num\_pool\_member}]

table set -subtable “rr\_table” “last\_mod” $new\_mod

-----

Here I want to check the validily of the variable as “table” cannot be initialized in RULE\_INIT

I also want to check if it’s not empty but non-numeric value is set.

if { $last\_mod == “” } {

table set -subtable “rr\_table” “last\_mod” 0 indefinite indefinite

set new\_mod 1

}

switch pool member based on the index

switch $new\_mod {

0 {

if { [LB::status pool $poolname member $ip\_pm0 $pool\_port up] } {

pool $::poolname member $ip\_pm0 $pool\_port

log local0.info “LBed to pm 0”

} else {

continue

}

}

1 {

if { [LB::status pool $poolname member $ip\_pm1 $pool\_port up] } {

pool $poolname member $ip\_pm1 $pool\_port

log local0.info “LBed to pm 1”

} else {

continue

}

}

2 {

if { [LB::status pool $poolname member $ip\_pm2 $pool\_port up] } {

pool $::poolname member $ip\_pm2 $pool\_port

log local0.info “LBed to pm 2”

} else {

continue

}

}

3 {

if { [LB::status pool $::poolname member $ip\_pm3 $pool\_port up] } {

pool $::poolname member $ip\_pm3 $pool\_port

log local0.info “LBed to pm 3”

} else {

continue

}

}

default {

log “no pool to assign for Virtual Server XX”

}

}

}

--------------------------------

---

<div class="post-metadata">

**Author:** ![AppleBee\_108607](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@AppleBee\_108607](https://community.f5.com/u/AppleBee_108607)\
**Post date:** [November 2, 2010, 7:49pm UTC](https://community.f5.com/t/cmp-compatible-strict-round-robin/4014/2 "2010-11-02T19:49:21Z")

</div>

Wheeww. I finally got it working, referring to an iRule from my colleague, which I guess comes from somewhere in DevCentral.

This one is much nicer coz you have to only modify the pool name.

I hope “initial setup” section can be moved to other EVENTs that is triggered only once after the iRule is loaded. And periodically update the list of pool member with “after” command or something.

---------------------------------------------------------

when RULE\_INIT {

set pool name

set static::poolname “test-pool”

}

when CLIENT\_ACCEPTED {

initial setup

$num\_pm: number of the pool member

set num\_pm [members $static::poolname]

set pm\_list [members -list $static::poolname]

table add -subtable tbl\_rr\_$static::poolname “last\_mod” 0

per connection process

choose index of the pool member to LB to

set last\_mod [table lookup -subtable tbl\_rr\_$static::poolname “last\_mod”]

set new\_mod [expr {[expr {$last\_mod + 1}] % $num\_pm}]

table set -subtable tbl\_rr\_$static::poolname “last\_mod” $new\_mod

check pool availability

if { [active\_members $static::poolname] \< 1 } {

if there is no active member, reject

reject

}

round robin LB

set pm [lindex $pm\_list $new\_mod]

set mon\_stat [LB::status pool $static::poolname member [lindex $pm 0] [lindex $pm 1]]

if {$mon\_stat == “up”} {

pool $static::poolname member [lindex $pm 0] [lindex $pm 1]

} else {

for {set i 1} {$i \< $num\_pm} {incr i} {

set j [expr {[expr {$i + $new\_mod}] % $num\_pm}]

set pm [lindex $pm\_list $j]

set mon\_stat [LB::status pool $static::poolname member [lindex $pm 0] [lindex $pm 1]]

if {$mon\_stat == “up”} {

pool $static::poolname member [lindex $pm 0] [lindex $pm 1]

table set -subtable tbl\_rr\_$static::poolname “last\_mod” $J

break

}

}

}

}

----------------------------------------------------------------

---

<div class="post-metadata">

**Author:** ![spark\_86682](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@spark\_86682](https://community.f5.com/u/spark_86682)\
**Post date:** [November 2, 2010, 8:08pm UTC](https://community.f5.com/t/cmp-compatible-strict-round-robin/4014/3 "2010-11-02T20:08:52Z")

</div>

To answer your questions:

You can’t access the session table in RULE\_INIT or any other global event, that’s true. However, don’t forget that entries in the session table deliberately expire, so even if you could initialize them in RULE\_INIT, they may be gone later, and your iRule has to cope with that (that’s one reason there are so many variations on things like the set command). Fortunately, the table commands are written with that in mind as well, and tend to Do The Right Thing in the case where there is no existing entry. To give one example (that will become relevant shortly), if you do something like increment a key that doesn’t exist, it automatically starts it as 0.

I don’t think there is a way to explicitly say that a value is a string or a number; Tcl generally converts automatically when necessary. However, I can’t see where you’re running into this problem. Could you elaborate a bit?

Yes, to set variables in RULE\_INIT, just use “set static::myvarname myvalue”.

As for your round-robin iRule, I think that you’re waaaaaay overthinking this. How about something like:

```tcl
when CLIENT_ACCEPTED {
  set poolname "test-pool"

  if { [active_members $poolname] < 1 } {
     No active pool members; reset client
    reject
    return
  }

  set count [members $poolname]
  set attempt 0
  while { $attempt < $count } {
    set num [table incr "round-robin:$poolname"]
    set num [expr $num % $count]
    set mbr [lindex [members -list $poolname]]
    set mbr_ip [lindex $mbr 0]
    set mbr_port [lindex $mbr 1]
    if { [LB::status pool $poolname member $mbr_ip $mbr_port up } {
      pool $poolname member $mbr_ip $mbr_port
      return
    }
    incr attempt
  }
}

```

Again, not actually tested or syntax-checked code, and written for clarity, not compactness or performance.

---

<div class="post-metadata">

**Author:** ![spark\_86682](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@spark\_86682](https://community.f5.com/u/spark_86682)\
**Post date:** [November 2, 2010, 8:16pm UTC](https://community.f5.com/t/cmp-compatible-strict-round-robin/4014/4 "2010-11-02T20:16:11Z")

</div>

This rule is good, but the session table is a shared global structure, and this iRule does what’s referred to as a “read-modify-write” to that data, which is bad. The three lines are:

```tcl
set last_mod [table lookup -subtable tbl_rr_$static::poolname "last_mod"]
    set new_mod [expr {[expr {$last_mod + 1}] % $num_pm}]
    table set -subtable tbl_rr_$static::poolname "last_mod" $new_mod

```

The first one reads some data from the session table, the second one modifies that data, and the third one writes it back. Hence, “read-modify-write”. Consider what would happen if two different connections came in at the same time and both executed this code. They would both read in a value (say, 3), both modify it (so new\_mod would be 4), and both write it back out. So the two connections would both use the 4th pool member, which isn’t what you want. In my other post, I use “table incr”, which does an atomic increment, so this couldn’t happen, since there’s no read-modify-write happening. If two connections do an increment at the same time, one incrment command will actually be serviced first, so the “table incr” command will never return the same value to the two connections, so they’ll always go to different pool members. Make sense?

---

<div class="post-metadata">

**Author:** ![AppleBee\_108607](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@AppleBee\_108607](https://community.f5.com/u/AppleBee_108607)\
**Post date:** [November 2, 2010, 10:50pm UTC](https://community.f5.com/t/cmp-compatible-strict-round-robin/4014/5 "2010-11-02T22:50:41Z")

</div>

Hi Spark,

Thanks aaa lllooott!

It’s amazing how you can write these logic into this compact code.

I just made small syntax-wise modification and it worked off the shelf. I will use this at my customer. Yep, and I was also aware of the implication of read-modify-write time gap, but just didn’t know how to tackle it.

Once again, thanks a lot!

----------------------------------------------------

when CLIENT\_ACCEPTED {

set poolname “test-pool”

if { [active\_members $poolname] \< 1 } {

No active pool members; reset client

reject

return

}

set count [members $poolname]

set attempt 0

while { $attempt \< $count } {

set num [table incr “round-robin:$poolname”]

set num [expr {$num % $count}]

set mbr [lindex [members -list $poolname] $num]

set mbr\_ip [lindex $mbr 0]

set mbr\_port [lindex $mbr 1]

if { [LB::status pool $poolname member $mbr\_ip $mbr\_port up] } {

pool $poolname member $mbr\_ip $mbr\_port

return

}

incr attempt

}

}

----------------------------------------------------
