You seem to have some round-robin login in the rule too. If I understand, the heart of the matter is:
1. Client connects to port N (where N is even)
2. This connection should be round-robin (or otherwise) load-balanced
3. The client (from the same IP) connects to port N+1 (which is odd)
4. This connection must go to the same server as in step 2.
If this is correct, then how about something like this:
when RULE_INIT {
set ::minport 49151
set ::maxport 49250
}
when LB_SELECTED {
set lport [TCP::local_port]
if { $lport > $::minport && $lport < $::maxport } {
if { ! [expr { $lport & 1 }] } {
even number port; 1st connection
Persist on remoteIP:localport.
Also, set up another persist entry for
the odd number port connection coming next
persist add uie "[IP::remote_addr]:$lport" 300
set lport [expr $lport + 1]
persist add uie "[IP::remote_addr]:$lport" 300
}
}
}
when CLIENT_ACCEPTED {
set lport [TCP::local_port]
if { $lport > $::minport && $lport < $::maxport } {
if { [expr { $lport & 1}] } {
odd number port; 2nd connection
Persist on remoteIP:localport.
Persist entry should already be there.
if { [string length [persist lookup uie "[IP::remote_addr]:$lport"]] ==
0 } {
log "ERROR! Connection from [IP::remote_addr]:[TCP::remote_port] to [IP::local_addr]:[TCP::local_port] had no persist record!"
reject
} else {
persist uie "[IP::remote_addr]:$lport"
}
}
}
}
Is that clear? The first connection comes in, gets load balanced, and creates two persistence entries, one of which is for the yet-unsent odd number port. When that second connection comes in, the persist entry is ready and waiting in CLIENT_ACCEPTED. Also, this way multiple connections from the same source IP but on different (sets of) ports will get load balanced separately. Plus you can just use the built-in load balancing algorithm. Easy!
Now, the persistence entry for the even number port will get expired independently of the odd number one. So if traffic flows over the first and not the second, the second persistence entry might get deleted while the first still exists. So another connection on the odd number port will get independently load balanced. If this is a problem, then there's some Deep Magical way of linking the two, but I'm not familiar with it.