LCR (Least Cost Routing) Module

Juha Heinanen

   <jh@tutpro.com>

Edited by

Juha Heinanen

   <jh@tutpro.com>

Ovidiu Sas

   <osas@voipembedded.com>

   Copyright © 2005-2014 Juha Heinanen
     __________________________________________________________________

   Table of Contents

   1. Admin Guide

        1. Overview
        2. Dependencies

              2.1. Kamailio Modules
              2.2. External libraries or applications

        3. Parameters

              3.1. db_url (string)
              3.2. lcr_gw_table (string)
              3.3. id_column (string)
              3.4. lcr_id_column (string)
              3.5. gw_name_column (string)
              3.6. ip_addr_column (string)
              3.7. hostname_column (string)
              3.8. port_column (string)
              3.9. params_column (string)
              3.10. uri_scheme_column (string)
              3.11. transport_column (string)
              3.12. strip_column (string)
              3.13. tag_column (string)
              3.14. flags_column (string)
              3.15. defunct_column (string)
              3.16. lcr_rule_table (string)
              3.17. prefix_column (string)
              3.18. from_uri_column (string)
              3.19. mt_tvalue_column (string)
              3.20. request_uri_column (string)
              3.21. stopper_column (string)
              3.22. enabled_column (string)
              3.23. lcr_rule_target_table (string)
              3.24. rule_id_column (string)
              3.25. gw_id_column (string)
              3.26. priority_column (string)
              3.27. weight_column (string)
              3.28. lcr_count (integer)
              3.29. gw_uri_avp (AVP string)
              3.30. ruri_user_avp (AVP string)
              3.31. tag_avp (AVP string)
              3.32. flags_avp (AVP string)
              3.33. rule_id_avp (AVP string)
              3.34. mt_pv_values (AVP string)
              3.35. mtree (string)
              3.36. defunct_capability (integer)
              3.37. lcr_id_avp (AVP string)
              3.38. defunct_gw_avp (AVP string)
              3.39. lcr_rule_hash_size (integer)
              3.40. lcr_gw_count (integer)
              3.41. dont_strip_or_prefix_flag (integer)
              3.42. priority_ordering (integer)
              3.43. fetch_rows (integer)
              3.44. ping_interval (integer)
              3.45. ping_inactivate_threshold (integer)
              3.46. ping_valid_reply_codes (string)
              3.47. ping_from (string)
              3.48. ping_socket (string)

        4. Functions

              4.1. load_gws(lcr_id[, uri_user[, caller_uri]])
              4.2. next_gw()
              4.3. inactivate_gw()
              4.4. defunct_gw(period)
              4.5. from_gw(lcr_id[, ip_addr, proto[, src_port]])
              4.6. from_any_gw([ip_addr, proto[, src_port]])
              4.7. to_gw(lcr_id[, ip_addr, proto])
              4.8. to_any_gw([ip_addr, proto])

        5. RPC Commands

              5.1. lcr.reload
              5.2. lcr.dump_gws
              5.3. lcr.dump_rules
              5.4. lcr.load_gws
              5.5. lcr.defunct_gw

        6. Known Limitations

   List of Examples

   1.1. Setting db_url module parameter
   1.2. Setting lcr_gw_table module parameter
   1.3. Setting id_column module parameter
   1.4. Setting lcr_id_column module parameter
   1.5. Setting gw_name_column module parameter
   1.6. Setting ip_addr_column module parameter
   1.7. Setting hostname_column module parameter
   1.8. Setting port_column module parameter
   1.9. Setting params_column module parameter
   1.10. Setting uri_scheme_column module parameter
   1.11. Setting transport_column module parameter
   1.12. Setting strip_column module parameter
   1.13. Setting tag_column module parameter
   1.14. Setting flags_column module parameter
   1.15. Setting defunct_column module parameter
   1.16. Setting lcr_rule_table module parameter
   1.17. Setting prefix_column module parameter
   1.18. Setting from_uri_column module parameter
   1.19. Setting from_uri_column module parameter
   1.20. Setting request_uri_column module parameter
   1.21. Setting stopper_column module parameter
   1.22. Setting enabled_column module parameter
   1.23. Setting lcr_rule_target_table module parameter
   1.24. Setting rule_id_column module parameter
   1.25. Setting gw_id_column module parameter
   1.26. Setting priority_column module parameter
   1.27. Setting weight_column module parameter
   1.28. Setting lcr_count module parameter
   1.29. Setting gw_uri_avp module parameter
   1.30. Setting ruri_user_avp module parameter
   1.31. Setting tag_avp module parameter
   1.32. Setting flags_avp module parameter
   1.33. Setting rule_id_avp module parameter
   1.34. Setting tag_avp module parameter
   1.35. Setting mtree module parameter
   1.36. Setting defunct_capability module parameter
   1.37. Setting lcr_id_avp module parameter
   1.38. Setting defunct_gw_avp module parameter
   1.39. Setting lcr_rule_hash_size module parameter
   1.40. Setting lcr_gw_count module parameter
   1.41. Setting dont_strip_or_prefix_flag module parameter
   1.42. Setting priority_ordering module parameter
   1.43. Set fetch_rows parameter
   1.44. Set ping_interval parameter
   1.45. Set ping_inactivate_threshold parameter
   1.46. Set ping_valid_reply_codes parameter
   1.47. Set ping_from parameter
   1.48. Set ping_socket parameter
   1.49. load_gws usage
   1.50. next_gw usage from a route block
   1.51. next_gw usage from a failure route block
   1.52. inactivate_gw usage
   1.53. defunct_gw usage
   1.54. from_gw usage
   1.55. from_gw usage
   1.56. to_gw usage
   1.57. to_gw usage
   1.58. lcr.reload RPC example
   1.59. lcr.dump_gws RPC example
   1.60. lcr.dump_rules RPC example
   1.61. lcr.load_gws RPC example
   1.62. lcr.defunct_gw RPC example

Chapter 1. Admin Guide

   Table of Contents

   1. Overview
   2. Dependencies

        2.1. Kamailio Modules
        2.2. External libraries or applications

   3. Parameters

        3.1. db_url (string)
        3.2. lcr_gw_table (string)
        3.3. id_column (string)
        3.4. lcr_id_column (string)
        3.5. gw_name_column (string)
        3.6. ip_addr_column (string)
        3.7. hostname_column (string)
        3.8. port_column (string)
        3.9. params_column (string)
        3.10. uri_scheme_column (string)
        3.11. transport_column (string)
        3.12. strip_column (string)
        3.13. tag_column (string)
        3.14. flags_column (string)
        3.15. defunct_column (string)
        3.16. lcr_rule_table (string)
        3.17. prefix_column (string)
        3.18. from_uri_column (string)
        3.19. mt_tvalue_column (string)
        3.20. request_uri_column (string)
        3.21. stopper_column (string)
        3.22. enabled_column (string)
        3.23. lcr_rule_target_table (string)
        3.24. rule_id_column (string)
        3.25. gw_id_column (string)
        3.26. priority_column (string)
        3.27. weight_column (string)
        3.28. lcr_count (integer)
        3.29. gw_uri_avp (AVP string)
        3.30. ruri_user_avp (AVP string)
        3.31. tag_avp (AVP string)
        3.32. flags_avp (AVP string)
        3.33. rule_id_avp (AVP string)
        3.34. mt_pv_values (AVP string)
        3.35. mtree (string)
        3.36. defunct_capability (integer)
        3.37. lcr_id_avp (AVP string)
        3.38. defunct_gw_avp (AVP string)
        3.39. lcr_rule_hash_size (integer)
        3.40. lcr_gw_count (integer)
        3.41. dont_strip_or_prefix_flag (integer)
        3.42. priority_ordering (integer)
        3.43. fetch_rows (integer)
        3.44. ping_interval (integer)
        3.45. ping_inactivate_threshold (integer)
        3.46. ping_valid_reply_codes (string)
        3.47. ping_from (string)
        3.48. ping_socket (string)

   4. Functions

        4.1. load_gws(lcr_id[, uri_user[, caller_uri]])
        4.2. next_gw()
        4.3. inactivate_gw()
        4.4. defunct_gw(period)
        4.5. from_gw(lcr_id[, ip_addr, proto[, src_port]])
        4.6. from_any_gw([ip_addr, proto[, src_port]])
        4.7. to_gw(lcr_id[, ip_addr, proto])
        4.8. to_any_gw([ip_addr, proto])

   5. RPC Commands

        5.1. lcr.reload
        5.2. lcr.dump_gws
        5.3. lcr.dump_rules
        5.4. lcr.load_gws
        5.5. lcr.defunct_gw

   6. Known Limitations

1. Overview

   The Least Cost Routing (LCR) module implements capability to serially
   forward a request to one or more gateways so that the order in which
   the gateways is tried is based on admin defined "least cost" rules.

   The LCR module supports many independent LCR instances (gateways and
   least cost rules). Each such instance has its own LCR instance
   identifier. Identifiers of normal LCR instances are positive integers.
   Gateways may belong to special LCR instance with identifier 0 meaning
   that such gateways belong to all normal LCR instances.

   For the purpose of facilitating least cost routing of requests, each
   gateway of an LCR instance is associated with one or more <prefix, from
   uri pattern, from uri userpart, request uri pattern, priority, weight>
   tuples. A gateway matches a request if user part of Request-URI matches
   "prefix", caller URI matches "from_uri" pattern, caller URI userpart
   matches mtree with "mt_value", and callee URI matches "request_uri"
   pattern in a tuple that is associated with the gateway.

   When the function load_gws() is called, matching gateways (that are not
   currently designated as defunct) are ordered for forwarding purposes as
   follows:

    1. according to longest Request-URI user part match
    2. according to tuple's priority
    3. according to tuple's randomized weight

   or, if priority_ordering parameter is set to value 1, as follows:

    1. according to tuple's priority
    2. according to tuple's randomized weight

   A tuple can be marked as a "stopper" tuple. If a "stopper" tuple
   matches, then matching stops at it and all other tuples with shorter
   prefixes are not considered.

   Prefix is a string of characters or NULL. From-URI pattern and
   Request-URI pattern are regular expressions (see 'man pcresyntax' for
   syntax), an empty string, or NULL. An empty or NULL From-URI pattern,
   Request-URI pattern or prefix matches anything. Smaller priority value
   means higher priority (highest priority value being 0 and lowest being
   255).

   Weight is an integer value from 1 to 254. Weight implementation is
   fast, but unfair favoring larger weight values at the expense smaller
   ones. For example, if two gateways have weights 1 and 2, probability
   that the gateway with weight 1 is tried first is 1/4, not 1/3. Two
   scripts are provided in lcr/utils directory that can be used to check
   the probabilities resulting from a given set of weight values. Same can
   be done with command 'kamctl eval_weights'.

   The function next_gw() can then be used to select one gateway at a time
   for forwarding. Upon each call, unless "dont_strip_or_prefix_flag" flag
   is set, user part of the original Request-URI is first stripped by the
   number of characters as specified by the gateway's strip count and then
   prefixed by the gateway's prefix. Upon each call, if a gateway's
   hostname is NULL, Request-URI will be rewritten based on gateway's URI
   scheme, IP address, port, parameters, and transport protocol. If
   hostname is not NULL and IP address is NULL, Request-URI will be
   rewritten based on the gateway's URI scheme, hostname, port, parameters
   and transport protocol. If both hostname and IP address are not NULL,
   Request-URI will be rewritten based on gateway's URI scheme, hostname,
   and parameters, and destination URI is set based on gateway's URI
   scheme, IP address, port, and transport protocol.

   Valid URI scheme values are NULL = sip, 1 = sip and 2 = sips. Currently
   valid transport protocol values are NULL and 0 = none, 1 = udp, 2 =
   tcp, 3 = tls, and 4 = sctp.

   As a side effect of gateway selection, selected gateway's tag and flags
   (that may contain information about the gateway and its capabilities)
   are stored to tag_avp and flags_avp, respectively, if the corresponding
   module parameter has been defined. In the same way, rule_id_avp, if
   defined, contains the id of the rule that selected the gateway.

2. Dependencies

   2.1. Kamailio Modules
   2.2. External libraries or applications

2.1. Kamailio Modules

   The following modules must be loaded before this module:
     * A database module like mysql, postgres or dbtext.
     * mtree module if mt_pv_values parameter is set.

2.2. External libraries or applications

   The following libraries or applications must be installed before
   running SIP Router with this module:
     * libpcre

3. Parameters

   3.1. db_url (string)
   3.2. lcr_gw_table (string)
   3.3. id_column (string)
   3.4. lcr_id_column (string)
   3.5. gw_name_column (string)
   3.6. ip_addr_column (string)
   3.7. hostname_column (string)
   3.8. port_column (string)
   3.9. params_column (string)
   3.10. uri_scheme_column (string)
   3.11. transport_column (string)
   3.12. strip_column (string)
   3.13. tag_column (string)
   3.14. flags_column (string)
   3.15. defunct_column (string)
   3.16. lcr_rule_table (string)
   3.17. prefix_column (string)
   3.18. from_uri_column (string)
   3.19. mt_tvalue_column (string)
   3.20. request_uri_column (string)
   3.21. stopper_column (string)
   3.22. enabled_column (string)
   3.23. lcr_rule_target_table (string)
   3.24. rule_id_column (string)
   3.25. gw_id_column (string)
   3.26. priority_column (string)
   3.27. weight_column (string)
   3.28. lcr_count (integer)
   3.29. gw_uri_avp (AVP string)
   3.30. ruri_user_avp (AVP string)
   3.31. tag_avp (AVP string)
   3.32. flags_avp (AVP string)
   3.33. rule_id_avp (AVP string)
   3.34. mt_pv_values (AVP string)
   3.35. mtree (string)
   3.36. defunct_capability (integer)
   3.37. lcr_id_avp (AVP string)
   3.38. defunct_gw_avp (AVP string)
   3.39. lcr_rule_hash_size (integer)
   3.40. lcr_gw_count (integer)
   3.41. dont_strip_or_prefix_flag (integer)
   3.42. priority_ordering (integer)
   3.43. fetch_rows (integer)
   3.44. ping_interval (integer)
   3.45. ping_inactivate_threshold (integer)
   3.46. ping_valid_reply_codes (string)
   3.47. ping_from (string)
   3.48. ping_socket (string)

3.1. db_url (string)

   URL of the database table to be used.

   Default value is “mysql://kamailioro:kamailioro@localhost/kamailio”.

   Example 1.1. Setting db_url module parameter
...
modparam("lcr","db_url","dbdriver://username:password@dbhost/dbname")
...

3.2. lcr_gw_table (string)

   Name of the table holding gateways definitions.

   Default value is “lcr_gw”.

   Example 1.2. Setting lcr_gw_table module parameter
...
modparam("lcr", "lcr_gw_table","gw")
...

3.3. id_column (string)

   Name of the auto-increment, primary key column. Common to all lcr
   module tables.

   Default value is “id”.

   Example 1.3. Setting id_column module parameter
...
modparam("lcr", "id_column", "row_id")
...

3.4. lcr_id_column (string)

   Name of the column holding the identifier of an LCR instance. Common to
   all lcr module tables. In lcr_rule and lcr_rule_target tables, value of
   the column is integer from 1 to lcr_count. In lcr_gw table, value of
   the column is from 0 to lcr_count.

   Default value is “lcr_id”.

   Example 1.4. Setting lcr_id_column module parameter
...
modparam("lcr", "lcr_id_column", "lcr_identifier")
...

3.5. gw_name_column (string)

   Name of the column holding gateway's name for documentation purpose.

   Default value is “gw_name”.

   Example 1.5. Setting gw_name_column module parameter
...
modparam("lcr", "gw_name_column", "name")
...

3.6. ip_addr_column (string)

   Name of the column holding the IPv4 or IPv6 address of the gateway.

   Default value is “ip_addr”.

   Example 1.6. Setting ip_addr_column module parameter
...
modparam("lcr", "ip_addr_column", "ip")
...

3.7. hostname_column (string)

   Name of the column holding gateway's hostname that is used in
   Request-URI hostpart, when request is sent to the gateway.

   Default value is “hostname”.

   Example 1.7. Setting hostname_column module parameter
...
modparam("lcr", "hostname_column", "host")
...

3.8. port_column (string)

   Name of the column holding the port number of the gateway.

   Default value is “port”.

   Example 1.8. Setting port_column module parameter
...
modparam("lcr", "port_column", "port")
...

3.9. params_column (string)

   Name of the column holding gateway's parameters that is used in
   Request-URI, when request is sent to the gateway.

   Default value is “params”.

   Example 1.9. Setting params_column module parameter
...
modparam("lcr", "params_column", "parameters")
...

3.10. uri_scheme_column (string)

   Name of the column holding the uri scheme of the gateway.

   Default value is “uri_scheme”.

   Example 1.10. Setting uri_scheme_column module parameter
...
modparam("lcr", "uri_scheme_column", "uri_scheme")
...

3.11. transport_column (string)

   Name of the column holding the transport protocol to be used for the
   gateway.

   Default value is “transport”.

   Example 1.11. Setting transport_column module parameter
...
modparam("lcr", "transport_column", "trans")
...

3.12. strip_column (string)

   Name of the column holding the number of characters to be stripped from
   the front of Request-URI user part before inserting tag.

   Default value is “strip”.

   Example 1.12. Setting strip_column module parameter
...
modparam("lcr", "strip_column", "strip_count")
...

3.13. tag_column (string)

   Name of the column holding gateway specific tag string that is added to
   Request URI userpart after stripping.

   Default value is “tag”.

   Example 1.13. Setting tag_column module parameter
...
modparam("lcr", "tag_column", "gw_tag")
...

3.14. flags_column (string)

   Name of the column holding gateway specific flag values.

   Default value is “flags”.

   Example 1.14. Setting flags_column module parameter
...
modparam("lcr", "flags_column", "gw_flags")
...

3.15. defunct_column (string)

   Name of the column holding UNIX timestamp telling the time until which
   the gw is considered as defunct. If timestamp value is 4294967295 (=
   max UNIX timestamp value) or greater, gw is considered currently unused
   and is not loaded into memory at all.

   Default value is “defunct”.

   Example 1.15. Setting defunct_column module parameter
...
modparam("lcr", "defunct_column", "defunct_until")
...

3.16. lcr_rule_table (string)

   Name of the table holding the LCR rules.

   Default value is “lcr_rule”.

   Example 1.16. Setting lcr_rule_table module parameter
...
modparam("lcr", "lcr_rule_table", "rules")
...

3.17. prefix_column (string)

   Name of the column in lcr_rule and lcr_gw tables holding prefix of
   Request-URI user part and prefix of gateway, respectively.

   Default value is “prefix”.

   Example 1.17. Setting prefix_column module parameter
...
modparam("lcr", "prefix_column", "number_prefix")
...

3.18. from_uri_column (string)

   Name of the column holding the From (caller's) URI.

   Default value is “from_uri”.

   Example 1.18. Setting from_uri_column module parameter
...
modparam("lcr", "from_uri_column", "caller_uri")
...

3.19. mt_tvalue_column (string)

   Name of the column holding mtree tvalue.

   Default value is “mt_tvalue”.

   Example 1.19. Setting from_uri_column module parameter
...
modparam("lcr", "mt_tvalue_column", "tree_value")
...

3.20. request_uri_column (string)

   Name of the column holding the regular expression to match against the
   complete request URI (including the "sip:" prefix).

   Default value is “request_uri”.

   Example 1.20. Setting request_uri_column module parameter
...
modparam("lcr", "request_uri_column", "callee_uri")
...

3.21. stopper_column (string)

   Name of the column holding rule's stopper attribute.

   Default value is “stopper”.

   Example 1.21. Setting stopper_column module parameter
...
modparam("lcr", "stopper_column", "stop")
...

3.22. enabled_column (string)

   Name of the column telling is the rule is currently enabled or
   disabled.

   Default value is “enabled”.

   Example 1.22. Setting enabled_column module parameter
...
modparam("lcr", "enabled_column", "in_use")
...

3.23. lcr_rule_target_table (string)

   Name of the table holding information about the LCR rule targets
   (gateways).

   Default value is “lcr_rule_target”.

   Example 1.23. Setting lcr_rule_target_table module parameter
...
modparam("lcr", "lcr_rule_target_table", "rules")
...

3.24. rule_id_column (string)

   Name of lcr_rule_target_table column containing an id of lcr_rule
   table.

   Default value is “rule_id”.

   Example 1.24. Setting rule_id_column module parameter
...
modparam("lcr", "rule_id_column", "rule")
...

3.25. gw_id_column (string)

   Name of lcr_rule_target_table column containing an id of lcr_gw table.

   Default value is “gw_id”.

   Example 1.25. Setting gw_id_column module parameter
...
modparam("lcr", "gw_id_column", "gw")
...

3.26. priority_column (string)

   Name of the column holding the priority of the rule target.

   Default value is “priority”.

   Example 1.26. Setting priority_column module parameter
...
modparam("lcr", "priority_column", "priority")
...

3.27. weight_column (string)

   Name of the column holding weight of rule target.

   Default value is “weight”.

   Example 1.27. Setting weight_column module parameter
...
modparam("lcr","weight_column", "target_weight")
...

3.28. lcr_count (integer)

   Maximum value of lcr_id.

   Default value is 1.

   Example 1.28.  Setting lcr_count module parameter
...
modparam("lcr", "lcr_count", 10)
...

3.29. gw_uri_avp (AVP string)

   Internal AVP that load_gws() function uses to store information of
   matching gateways.

   There is NO default value, thus this variable must be defined in
   sip-router.cfg.

   Example 1.29. Setting gw_uri_avp module parameter
...
modparam("lcr", "gw_uri_avp", "$avp(lcr_gwuri)")
...

3.30. ruri_user_avp (AVP string)

   Internal AVP that next_gw function uses to store Request-URI user for
   subsequent next_gw calls.

   There is NO default value, thus this variable must be defined in
   sip-router.cfg.

   Example 1.30. Setting ruri_user_avp module parameter
...
modparam("lcr", "ruri_user_avp", "$avp(lcr_ruri_user)")
...

3.31. tag_avp (AVP string)

   If defined, an AVP where successful next_gw and from_gw functions store
   gateway's tag.

   There is NO default value, i.e, if not defined, gateway's tag is not
   stored anywhere.

   Example 1.31. Setting tag_avp module parameter
...
modparam("lcr", "tag_avp", "$avp(lcr_tag)")
...

3.32. flags_avp (AVP string)

   If defined, an AVP where successful next_gw and from_gw functions store
   gateway's flags.

   There is NO default value, i.e, if not defined, gateway's flags are not
   stored anywhere.

   Example 1.32. Setting flags_avp module parameter
...
modparam("lcr", "flags_avp", "$avp(lcr_flags)")
...

3.33. rule_id_avp (AVP string)

   If defined, an AVP where successful next_gw and from_gw functions store
   matching rule's id.

   There is NO default value, i.e, if not defined, matching rule's id is
   not stored anywhere.

   Example 1.33. Setting rule_id_avp module parameter
...
modparam("lcr", "rule_id_avp", "$avp(lcr_ruleid)")
...

3.34. mt_pv_values (AVP string)

   If defined and mt_value is given for a rule, load_gws() matches caller
   URI userpart to a mtree given as mtree parameter.

   If defined, must have the same value as mtree module pv_values
   parameter.

   There is NO default value.

   Example 1.34. Setting tag_avp module parameter
...
modparam("lcr", "mt_pv_values", "$avp(lcr_mt_values)")
...

3.35. mtree (string)

   Name of mtree to which load_gws() matches caller URI userpart.

   Default value is "lcr".

   Example 1.35. Setting mtree module parameter
...
modparam("lcr", "mtree", "caller_tree")
...

3.36. defunct_capability (integer)

   Tells if defunct capability of (non-responsive) gateways is supported.
   Non-zero value turns on defunct capability.

   Default value is 0.

   Example 1.36.  Setting defunct_capability module parameter
...
modparam("lcr", "defunct_capability", 1)
...

3.37. lcr_id_avp (AVP string)

   Internal AVP that load_gws() function uses to store LCR instance
   identifier of loaded gateways. Only needed if gateway defunct
   capability has been activated.

   There is NO default value.

   Example 1.37. Setting lcr_id_avp module parameter
...
modparam("lcr", "lcr_id_avp", "$avp(lcr_id)")
...

3.38. defunct_gw_avp (AVP string)

   Internal AVP that next_gw() function uses to store internal index of
   the selected gateway for later use by defunct_gw() function. Only
   needed if gateway defunct capability has been activated.

   There is NO default value.

   Example 1.38. Setting defunct_gw_avp module parameter
...
modparam("lcr", "defunct_gw_avp", "$avp(lcr_defunct_gw)")
...

3.39. lcr_rule_hash_size (integer)

   Defines the size of hash table used to store LCR rules. Hashing is done
   based on rule's prefix. Larger value means less collisions with other
   prefixes. Hash size value should be a power of 2.

   Default value is 128.

   Example 1.39.  Setting lcr_rule_hash_size module parameter
...
modparam("lcr", "lcr_rule_hash_size", 1024)
...

3.40. lcr_gw_count (integer)

   Defines the maximum number of gateways in lcr_gw table.

   Default value is 128.

   Example 1.40.  Setting lcr_gw_count module parameter
...
modparam("lcr", "lcr_gw_count", 1024)
...

3.41. dont_strip_or_prefix_flag (integer)

   Defines the flag number used to tell if stripping and tagging is done
   for the selected gateway.

   Default value is -1 meaning that the flag is not defined.

   Example 1.41.  Setting dont_strip_or_prefix_flag module parameter
...
modparam("lcr", "dont_strip_or_prefix_flag", 10)
...

3.42. priority_ordering (integer)

   Defines how matching gateways are ordered (see Overview section).

   Default value is 0.

   Example 1.42.  Setting priority_ordering module parameter
...
modparam("lcr", "priority_ordering", 1)
...

3.43. fetch_rows (integer)

   The number of the rows to be fetched at once from database when loading
   data from lcr_rule table. This value can be used to tune the load time
   at startup. For 1MB of private memory (default) it should be below
   3750. In order for this parameter to have effect, the database driver
   must support fetch_result() capability.

   Default value is “1024”.

   Example 1.43. Set fetch_rows parameter
...
modparam("lcr", "fetch_rows", 3000)
...

3.44. ping_interval (integer)

   Interval in seconds for sending OPTIONS ping requests to gateways that,
   due to failures, have been marked as inactive by inactivate_gw()
   function call. If an inactive gateway later gives a valid response (see
   ping_valid_reply_codes) to a ping request, it is marked again as
   active.

   If value of this parameter is greater than zero, tm module must have
   been loaded and parameters lcr_id_avp and defunct_gw_avp must have been
   defined. Value “0” disables sending of OPTIONS ping requests to failed
   gateways.

   Default value is “0”.

   Example 1.44.  Set ping_interval parameter
...
modparam("lcr", "ping_interval", 15)
...

3.45. ping_inactivate_threshold (integer)

   Tells after how many failures (= inactivate_gw() function calls) a
   gateway is marked as inactive.

   Default value is “1”, i.e., gateway is inactivated after first failure.

   Example 1.45.  Set ping_inactivate_threshold parameter
...
modparam("lcr", "ping_inactivate_threshold", 3)
...

3.46. ping_valid_reply_codes (string)

   A comma separated list of SIP reply codes, which are accepted as valid
   replies to OPTIONS ping requests. Reply codes 2xx are by default
   accepted as valid replies and they don't need to be listed here.

   Default value is “”, i.e., only 2xx replies are considered as valid
   replies.

   Example 1.46.  Set ping_valid_reply_codes parameter
...
modparam("lcr", "ping_valid_reply_codes", "403,405,501")
...

3.47. ping_from (string)

   From URI used in OPTIONS ping requests.

   Default value is “sip:pinger@localhost”.

   Example 1.47.  Set ping_from parameter
...
modparam("lcr", "ping_from", "sip:proxy.operator.com")
...

3.48. ping_socket (string)

   Socket to be used for sending OPTIONS ping request. If not set or set
   to “”, default socket is used.

   Default value is “”.

   Example 1.48.  Set ping_socket parameter
...
modparam("lcr", "ping_socket", "192.98.102.10:5060")
...

4. Functions

   4.1. load_gws(lcr_id[, uri_user[, caller_uri]])
   4.2. next_gw()
   4.3. inactivate_gw()
   4.4. defunct_gw(period)
   4.5. from_gw(lcr_id[, ip_addr, proto[, src_port]])
   4.6. from_any_gw([ip_addr, proto[, src_port]])
   4.7. to_gw(lcr_id[, ip_addr, proto])
   4.8. to_any_gw([ip_addr, proto])

4.1.  load_gws(lcr_id[, uri_user[, caller_uri]])

   Loads attributes of matching gateways to gw_uri_avp (see Overview
   section). Argument lcr_id specifies the used LCR instance. It can be a
   positive integer or a pseudo variable containing an integer value. If
   uri_user is given, it is used, instead of Request-URI user part, to
   look for matching gateways. Caller's URI may be given by caller_uri
   argument. If caller_uri argument is omitted, it defaults to empty
   string. Both uri_user and caller_uri argument may be a string or a
   pseudo variable containing a string value.

   Returns 1 if at least one matching gateway was found, 2 if no matching
   gateways was found, and -1 on error.

   Execution time of load_gws() function is O(N) * O(M), where N is number
   of different prefix lengths and M is number of collisions for matching
   prefix(es) in lcr rules hash table of the LCR instance.

   This function can be used from REQUEST_ROUTE.

   Example 1.49. load_gws usage
...
if (!load_gws(1, $rU, $var(caller_uri))) {
        sl_send_reply("500", "Server Internal Error - Cannot load gateways");
        exit;
};
...

4.2.  next_gw()

   Upon first call, fetches attribute values stored in first gw_uri_avp,
   destroys that AVP, and rewrites Request-URI and possibly also
   destination URI as described in the Overview section. Saves user part
   of Request-URI into ruri_user_avp for use in subsequent next_gw()
   calls.

   Upon subsequent calls, does the same as in above, but takes user part
   of Request-URI from ruri_user_avp.

   As a side effect, stores gateway's tag and flags to tag_avp and
   flags_avp, respectively, if the corresponding module parameter has been
   defined. In the same way, rule_id_avp, if defined, contains the id of
   the rule that selected the gateway.

   Returns 1 on success and -1 if there were no gateways left or if an
   error occurred (see syslog).

   Must be preceded by successful load_gws() call.

   This function can be used from REQUEST_ROUTE, FAILURE_ROUTE.

   Example 1.50. next_gw usage from a route block
...
if (!next_gw()) {
        sl_send_reply("503", "Service not available - No gateways");
        exit;
};
...

   Example 1.51. next_gw usage from a failure route block
...
if (!next_gw()) {
        t_reply("503", "Service not available - No more gateways");
        exit;
};
...

4.3.  inactivate_gw()

   Inactivates the gateway denoted by lcr_id_avp and defunct_gw_avp (which
   were set by previous next_gw() call). Use of this function requires
   that ping_interval module parameter has been set to a positive value
   allowing an inactivated gateway to be automatically activated by a
   positive response to OPTIONS ping request.

   Returns 1 on success and -1 in case of error (see syslog).

   This function can be used from REQUEST_ROUTE and FAILURE_ROUTE.

   Example 1.52. inactivate_gw usage
...
failure_route [GW_FAILURE] {
...
    if (t_check_status("408|503")) {
        inactivate_gw();
    };
...

4.4.  defunct_gw(period)

   Defuncts gateway denoted by lcr_id_avp and defunct_gw_avp (which were
   set by previuos next_gw() call) for a period of seconds given as
   argument. Argument must be a positive integer constant or a pseudo
   variable with positive integer value. Value of defunct column in
   database is not updated.

   Returns 1 on success and -1 in case of error (see syslog).

   This function can be used from REQUEST_ROUTE, FAILURE_ROUTE.

   Example 1.53. defunct_gw usage
...
defunct_gw(60);
...

4.5.  from_gw(lcr_id[, ip_addr, proto[, src_port]])

   Checks if request comes from IP address, transport protocol and source
   port specified for a gateway in LCR instance lcr_id. Fails if the LCR
   instance includes one or more gateways without IP address. IP address,
   transport protocol and source port to be checked are either taken from
   source IP address and port of the request or (if present) from ip_addr,
   proto and src_port arguments.

   lcr_id can be an integer constant or a pseudo variable holding an
   integer value. ip_addr can be a string or a pseudo variable holding a
   string value. proto can be an integer constant (0 = ANY, 1 = UDP, 2 =
   TCP, 3 = TLS, 4 = SCTP) or a pseudo variable holding such an integer
   value. src_port can be an integer or a pseudo variable holding such an
   integer value.

   If request comes from a gateway, gateway's tag and flags are stored as
   a side effect to tag_avp and flags_avp, respectively, if the
   corresponding module parameter has been defined. In the same way,
   rule_id_avp, if defined, contains the id of the rule that selected the
   gateway.

   Returns 1 on success and -1 on failure or on error.

   Execution time of from_gw() function is O(log N), where N is number of
   gateways in the LCR instance.

   This function can be used from REQUEST_ROUTE, FAILURE_ROUTE,
   ONREPLY_ROUTE.

   Example 1.54. from_gw usage
...
if (from_gw(1, $avp(real_source_addr), 2, 5060) {
        ...
};
...

4.6.  from_any_gw([ip_addr, proto[, src_port]])

   Checks if request comes from IP address, transport protocol and source
   port specified for any gateway. Only LCR instances, where all gateways
   have IP address, are included in the test. IP address, transport
   protocol and source port to be checked are either taken from source IP
   address, transport protocol and source port of the request or (if
   present) from ip_addr, proto and src_port arguments. See from_gw()
   function for more info about the arguments.

   If any gateway has the IP address, transport protocol and source port,
   function returns LCR identifier of the gateway. Returns -1 on error or
   if the request does not come from a gateway.

   If request comes from a gateway, gateway's tag and flags are stored as
   a side effect to tag_avp and flags_avp, respectively, if the
   corresponding module parameter has been defined. In the same way,
   rule_id_avp, if defined, contains the id of the rule that selected the
   gateway.

   Execution time of from_gw() function is M * O(log N), where M is number
   of LCR instances and N is average number of gateways in LCR instances.

   This function can be used from REQUEST_ROUTE, FAILURE_ROUTE,
   ONREPLY_ROUTE.

   Example 1.55. from_gw usage
...
$var(lcr_id) = from_any_gw("192.168.1.1", 3, 5061);
...

4.7.  to_gw(lcr_id[, ip_addr, proto])

   Checks if in-dialog request goes to IP address and transport protocol
   specified for a gateway in LCR instance lcr_id. Fails if LCR instance
   includes one or more gateways without IP address. IP address and
   transport protocol to be checked are either taken from Request-URI or
   (if present) from ip_addr and proto arguments. See from_gw() for more
   info regarding the arguments.

   Returns 1 on success and -1 on failure and error.

   Execution time of to_gw() function is O(log N), where N is number of
   gateways in the LCR instance.

   This function can be used from REQUEST_ROUTE, FAILURE_ROUTE.

   Example 1.56. to_gw usage
...
if (to_gw("1")) {
        ...
        exit;
};
...

4.8.  to_any_gw([ip_addr, proto])

   Checks if in-dialog request goes to IP address and transport protocol
   of any gateway. Only LCR instances, where all gateways have IP address,
   are included in the test. IP address and transport protocol to be
   checked are either taken from Request-URI or (if present) from ip_addr
   and proto arguments. See from_gw() for more info regarding the
   arguments.

   Execution time of to_any_gw() function is M * O(log N), where M is
   number of LCR instances and N is average number of gateways in LCR
   instances.

   If any gateway has the IP address, returns LCR identifier of the
   gateway. Returns -1 if request does not go to a gateway and on error.

   This function can be used from REQUEST_ROUTE, FAILURE_ROUTE.

   Example 1.57. to_gw usage
...
if (to_any_gw("192.55.66.2", 1)) {
        ...
        exit;
};
...

5. RPC Commands

   5.1. lcr.reload
   5.2. lcr.dump_gws
   5.3. lcr.dump_rules
   5.4. lcr.load_gws
   5.5. lcr.defunct_gw

5.1. lcr.reload

   Causes lcr module to re-read the contents of LCR tables into memory.

   Name: lcr.reload

   Parameters: none

   Example 1.58. lcr.reload RPC example
                $ kamcmd lcr.reload

5.2. lcr.dump_gws

   Causes lcr module to dump the contents of its in-memory gw table.

   Parameters: none

   Example 1.59. lcr.dump_gws RPC example
                $ kamcmd lcr.dump_gws

5.3. lcr.dump_rules

   Causes lcr module to dump the contents of its in-memory lcr_rule and
   lcr_rule_target tables. Rules can be filtered by lcr_id or lcr_id and
   prefix. The filters are passed as optional parameters.

   Parameters:
     * lcr_id filter rules based on lcr_id.
     * prefix filter rules based on prefix (prefixes shorter or equal to
       the give prefix) in addition to the previous lcr_id parameter.

   Example 1.60. lcr.dump_rules RPC example
                $ kamcmd lcr.dump_rules

5.4. lcr.load_gws

   Loads gateways and prints ids of matching ones in priority order.

   Name: lcr.load_gws

   Parameters: lcr_id uri_user [caller_uri request_uri]

   Example 1.61. lcr.load_gws RPC example
                $ kamcmd lcr.load_gws 2 s:0447058050

5.5. lcr.defunct_gw

   Defuncts gateway loaded into memory for a period of time (seconds)
   without a need to store gateway's defunct value into database and
   reload the tables.

   Name: lcr.defunct_gw

   Parameters: lcr_id gw_id period

   Example 1.62. lcr.defunct_gw RPC example
                $ kamcmd lcr.defunct_gw 1 4 120

6. Known Limitations

   In-memory LCR rules and gw tables are switched by two consecutive
   machine instructions. If lcr reload process is interrupted after the
   first one, in-memory gateway table does not match in-memory rule table
   until execution of lcr reload process is resumed.
