SNI Name-based Pool Selection in L4 Proxy


Starting with Avi Vantage 18.2.5, it is possible to load balance a layer 4 application based on a server name in the SNI extension on TLS client hello. The DataScript ensures that it is invoked after having sufficient bytes to inspect the TLS payload. Once load balancing is done, it stops invoking DataScript again for subsequent packets in the same connection.

Avi UI Configuration

  • Navigate to Templates > Scripts

Templates Scripts screen

  • Click on Create. The below DataScript set editor window will appear. Key in a value for Name.

DataScript set editor

  • Scroll down to expose the L4 Events field. You can import the file by clicking on IMPORT FILE button. You can also specify the description in the Description field.

DataScript uploaded and visible

  • In the Protocol Parser field, select Default-TLS option from the drop-down list. That parser needs to be attached to the DataScript so that the script can avail itself of TLS parsing capabilities and API calls.

  • Click on Save when done.

Sample DataScript

The Default-TLS protocol parser needs to be selected. By associating this DataScript with the virtual service, the first TLS packet from the client — which ideally is a client hello — will get inspected by the DataScript.

local avi_tls = require "Default-TLS"
-- Buffer minimum of 20 bytes of data
buffered = avi.l4.collect(20)
-- Read the gathered payload
payload =
-- Obtain the length of the payload
len = avi_tls.get_req_buffer_size(payload)
if ( buffered < len ) then
-- Wait till len bytes are collected at TCP layer
-- Verify if client hello and handshake.
if ( avi_tls.sanity_check(payload) ) then
-- Parse the TLS payload
   local h = avi_tls.parse_record(payload)
-- Obtain the SNI name
   local sname = avi_tls.get_sni(h)
   if sname == nil then
      avi.vs.log('SNI not present')
      avi.vs.log("SNI=".. sname)
-- Stop invoking DataScript after the first payload.
avi_tls = nil

In this example, the SNI name is obtained by parsing the TLS payload.

avi_tls.parse_record parses the payload and the helper API avi_tls.get_sni(h) is used to obtain the SNI name.

This SNI name can be now used to select a pool server or load balance and create a persistence entry for the same.

Alternatively, this can also be used to select a specific pool using"pool_name").

Note: The pool name and SNI name need not be the same, however, for certain use cases the SNI name could be same as pool name to select a pool based on the SNI name