Pool redirect based on source IP Range

Hi guys,

I need to write an IRule to route traffic to different pools based on source IP range.

Would this be the best syntax to use?

when CLIENT_ACCEPTED {

if { [IP::addr [IP::client_addr] equals 10.10.10.10] } {

pool my_pool

}

}

Can I just add a subnet mask to the “equals 10.10.10.10” in \ format?

Thanks in advance.

D

Hi,

With IP::addr (Click here), you can use /cidr notation:

when CLIENT_ACCEPTED { 
    if { [IP::addr [IP::client_addr]/24 equals 10.10.10.0] } { 
       pool my_pool 
    } 
 }

Aaron

Yep,

Thanks for that - should work fine.

I actually need to do it this way round however;

when CLIENT_ACCEPTED {

if { [IP::addr [IP::client_addr] equals 10.10.10.0/24] } {

pool my_pool

}

}

Cheers,

D

Yep they are for that example however:

I dont want to define the client IP

I want to check [IP::addr [IP::client_addr] against a known defined range and only redirect IP’s of that range off to another pool.

Or am I missunderstanding you here hehe.

Also with this example

when CLIENT_ACCEPTED {

if { [IP::addr [IP::client_addr] equals 10.10.10.0/24] } {

pool my_pool

}

}

Would i need an else pool default_pool ? Or is it implied.

D

It’s the same whether you’re explicitly providing an IP address or using IP::client_addr to get the client IP address. So the first two are equivalent, as are the second two if the client IP address for the test was 10.42.2.2

[IP::addr 10.42.2.2/24 equals 10.42.2.1]: 1

[IP::addr 10.42.2.2 equals 10.42.2.1/24]: 1

[IP::addr [IP::client_addr]/24 equals 10.42.2.1]: 1

[IP::addr [IP::client_addr] equals 10.42.2.1/24]: 1

If you have a default pool on the VIP and you’re making the pool decision for the entire TCP connection, you don’t need to explicitly define a pool in an else clause.

Aaron

is there a way to specify a range for ips?

like from 67.195.0.1 to 67.195.254.254

If 67.195.0.0 - 67.195.255.255 is close enough you could use [IP::addr [IP::client_addr]/16 equals 67.195.0.0].

Aaron

awesome - THANK YOU

Where would a rule like this be applied ?

I’ve applied the following rule to the Virtual Server that users would access:

when CLIENT_ACCEPTED {

if { [IP::addr [IP::client_addr] equals 209.221.139.195] } {

pool Segregation_Pool

}

}

However, requests coming from the IP 209.221.139.195 are NOT redirected to the pool called Segregation_Pool, and instead are directed to the ‘default pool’

Hi Randy,

That iRule looks straightforward enough. Which LTM version are you testing with? Do you have ASM or WAM enabled on the VIP? What type of VIP are you using the iRule on?

If it’s not working as you expect, can you add debug logging to confirm the client IP address and whether the pool selection is being hit?

when CLIENT_ACCEPTED { 
  
    log local0. "client IP:port [IP::client_addr][TCP::client_port]: [virtual name] [IP::local_addr][TCP::local_port]" 
    if { [IP::addr [IP::client_addr] equals 209.221.139.195] } { 
       log local0. "client IP:port [IP::client_addr][TCP::client_port]: Matched IP check, selecting Segregation_Pool" 
       pool Segregation_Pool 
    } 
 } 
 when LB_SELECTED { 
    log local0. "client IP:port [IP::client_addr][TCP::client_port]: Selected server: [LB::server]" 
 } 
 when SERVER_CONNECTED { 
    log local0. "client IP:port [IP::client_addr][TCP::client_port]: Connected server: [IP::server_addr]:[TCP::server_port]" 
 }

Aaron

Thanks !

I had actually gotten this working with a slight change -

when HTTP_REQUEST {

if { [IP::addr [IP::client_addr] equals 209.221.139.195] } {

pool Segregation_Pool

}

}

Next step of my project is to evaluate for a different IP … Would this be as simple as adding another IP::addr statement ?

when HTTP_REQUEST {

if { [IP::addr [IP::client_addr] equals 209.221.139.195] } {

or if { [IP::addr [IP::client_addr] equals 222.201.139.195] } {

pool Segregation_Pool

}

}

Hi Randy,

Adding another check would be as simple as the following

when HTTP_REQUEST { 
 if { [IP::addr [IP::client_addr] equals 209.221.139.195] } { 
    pool Segregation_Pool 
 } else if  { [IP::addr [IP::client_addr] equals 209.221.139.196] } { 
    pool Segregation_pool2 
 }  
 }

Or using matchclass which would contain a list of IP addresses

You can find more details here

http://devcentral.f5.com/wiki/default.aspx/iRules/IP__addr.html

I hope this helps

Bhattman

Hi Randy,

I’m also in a similar situation as you.

The following iRule is also not working for me:

when CLIENT_ACCEPTED {

set rserver1 “xserver”

set rserver2 “yserver”

if { [IP::addr [IP::client_addr] equals a.a.a.a] } {

pool mypool member $rserver1 80

}

if { [IP::addr [IP::client_addr] equals b.b.b.b] } {

pool mypool member $rserver2 80

}

}

I will test with HTTP_REQUEST tomorrow. But what are is the difference between the 2 and why would it work with one and not with the other? Also I’m using type “standard” as virtual server.

Any suggestion is very appreciated. Thanks.

if you are matching an exact IP address you only need IP::client_addr. If you are matching a network, you need a mask with that IP::addr command. Please check the wiki page for IP::addr for examples. If strictly doing IP matching/comparison, no need to use HTTP_REQUEST. Finally, depending on how many servers you end up with, a switch would be preferable to multiple if statements. If you can give me some specifics of what you are trying to accomplish (sanitized) I can cook up a sample for you.

Hi Jason,

Thank you for your quick reply.

The setup is as follow.

There are 2 proxy servers (a and b) which connects to this VS. Behind this VS there is a pool with 2 members (x and y). We see that connections are only going to server y. Only when server y is down will the connections be forwarded to server x. We think that the number of sources is the issue.

The VS has the following setup: - VS type is “standard” - TCP protocol with custom idle timeout - Default persistence profile is “source_addr”

The pool has the follow setup: - Load balancing method is “least connection”

With this setup we see the issue happening that I have described on top.

What we would like to achieve is to have each proxy server go to a specific pool member. Only when this specific pool member is down will it go to the other active one.

So normal situation: a → x and b → y

If either x or y is down: a → y and b → y or a → x and b → x

I hope it’s clear what we want to achieve.

Thanks in advance!