RTSP_redirect

Problem this snippet solves:

It redirects and load balances RTSP requests to 2 or more video servers. Persistence is is done by MD5 hash calculation, so requests to same file are redirected to same server. RTSP traffic does not pass through Big-IP.

"First it checks RTSP method client sends, if its OPTIONS we respond with "200 OK". SETUP or DESCRIBE methods we take requested filename from url with regexp and calculate MD5 hash to each active video server and select winning node. Then we simply respond with RTSP redirect with url that points to winning node. No traffic is passed through Big-IP to video servers. Big-IP monitors video servers with healt-check.

Code :

# Note: Works for version 9.4.2 or above.

when RTSP_REQUEST {
 if { [RTSP::method] contains "OPTIONS" } {
  RTSP::respond 200 OK "Server: F5-redirector\r\nPublic: OPTIONS, SETUP\r\n\r\n"
 }
 if { [RTSP::method] contains "SETUP" or [RTSP::method] contains "DESCRIBE" } {
  set client [IP::remote_addr]
  regexp "rtsp://.*/(.*)$" [RTSP::uri] url file
  # MD5 Hash & Persistence
  set S ""
  foreach N [active_members -list vod] {
    if { [md5 $N$file] > $S } {
      set S [md5 $N$file]
      set W $N
    }
  }
  set vod [lindex $W 0]
  set newuri "rtsp://$vod:554/$file"
  RTSP::respond 302 MOVED_TEMPORARILY "Server: F5-redirector\r\nLocation: $newuri\r\nConnection: close\r\n\r\n"
  log "Client ($client) request to $file redirected to $newuri"
 }
}

# Note: Works for version v9.0.0 to v9.4.1 (contributed by Bhattman at gmail dot com)

# Description: Because active member -list doesn't exist in versions prior to v9.4.2, you need statically add IP addresses of the pool in the iRule.

# iRule Source 2

when RTSP_REQUEST {
 if { [RTSP::method] contains "OPTIONS" } {
  RTSP::respond 200 OK "Server: F5-redirector\r\nPublic: OPTIONS, SETUP\r\n\r\n"
 }
 if { [RTSP::method] contains "SETUP" or [RTSP::method] contains "DESCRIBE" } {
  set client [IP::remote_addr]
  regexp "rtsp://.*/(.*)$" [RTSP::uri] url file

  # Mimics active member -list  found on v9.4.2 or above
  set GOOD_NODES {}
  set NS {     }
  foreach N $NS {
    if { [LB::status pool vod member $N ] eq "up" } {
      lappend GOOD_NODES $N
    }      
  }
  # MD5 Hash & Persistence
  set S ""
  foreach N $GOOD_NODES {
    if { [md5 $N$file] > $S } {
      set S [md5 $N$file]
      set W $N
    }
  }
  set vod [lindex $W 0]
  set newuri "rtsp://$vod:554/$file"
  RTSP::respond 302 MOVED_TEMPORARILY "Server: F5-redirector\r\nLocation: $newuri\r\nConnection: close\r\n\r\n"
  log "Client ($client) request to $file redirected to $newuri"
 }
}
Published Mar 18, 2015
Version 1.0
No CommentsBe the first to comment