<?xml version="1.0" encoding='ISO-8859-1'?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd" [

<!-- Include general documentation entities -->
<!ENTITY % docentities SYSTEM "../../../../doc/docbook/entities.xml">
%docentities;

]>
<!-- Module User's Guide -->

<chapter>

	<title>&adminguide;</title>

	<section>
	<title>Overview</title>
	<para>
	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.
	</para>
	<para>
	The LCR module supports many independent LCR instances (gateways and
	least cost rules).  Each such instance has its own <emphasis>LCR
	instance identifier</emphasis>.  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.
	</para>
	<para>
	For the purpose of facilitating least cost routing of requests,
	each gateway of an LCR instance is associated with one or more
	&lt;prefix, from uri pattern, from uri userpart, request uri
    pattern, priority, weight&gt;
	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.
	</para>
	<para>
	When the function <emphasis>load_gws()</emphasis> is called,
	matching gateways (that are not currently designated as defunct)
	are ordered for forwarding purposes as follows:
	</para>
	<para>
	<orderedlist>
            <listitem>
                <para>according to longest Request-URI user part match
                </para>
            </listitem>
            <listitem>
                <para>according to tuple's priority
                </para>
            </listitem>
            <listitem>
                <para>according to tuple's randomized weight
                </para>
            </listitem>
        </orderedlist>
	</para>
	<para>
    or, if priority_ordering parameter is set to value 1, as follows:
	</para>
	<para>
	<orderedlist>
            <listitem>
                <para>according to tuple's priority
                </para>
            </listitem>
            <listitem>
                <para>according to tuple's randomized weight
                </para>
            </listitem>
        </orderedlist>
	</para>
	<para>
	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.
	</para>
	<para>
	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).
	</para>
	<para>
	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'.
	</para>
	<para>
        The function <emphasis>next_gw()</emphasis> 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.
	</para>
        <para>
        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.
	</para>
	<para>
	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.
	</para>
	</section>

	<section>
	<title>Dependencies</title>
	<section>
	<title>&kamailio; Modules</title>
	<para>
		The following modules must be loaded before this module:
		<itemizedlist>
		<listitem>
		<para>
			<emphasis>A database module like mysql, postgres or
			dbtext</emphasis>.
		</para>
		</listitem>
		<listitem>
		<para>
			<emphasis>mtree module if mt_pv_values parameter is set</emphasis>.
		</para>
		</listitem>
		</itemizedlist>
	</para>
	</section>
	<section>
	<title>External libraries or applications</title>
	<para>
		The following libraries or applications must be installed before
		running &siprouter; with this module:
			<itemizedlist>
			<listitem>
			<para>
				<emphasis>libpcre</emphasis>
			</para>
			</listitem>
			</itemizedlist>
	</para>
	</section>
	</section>

	<section>
	<title>Parameters</title>
	<section id="lcr.p.db_url">
		<title><varname>db_url</varname> (string)</title>
		<para>
		&url; of the database table to be used.
		</para>
		<para>
		<emphasis>
			Default value is
			<quote>&defaultrodb;</quote>.
		</emphasis>
		</para>
		<example>
		<title>Setting <varname>db_url</varname> module parameter</title>
		<programlisting format="linespecific">
...
modparam("lcr","db_url","&exampledb;")
...
</programlisting>
		</example>
	</section>

	<section id="lcr.p.lcr_gw_table">
		<title><varname>lcr_gw_table</varname> (string)</title>
		<para>
		Name of the table holding gateways definitions.
		</para>
		<para>
		<emphasis>
			Default value is <quote>lcr_gw</quote>.
		</emphasis>
		</para>
		<example>
		<title>Setting <varname>lcr_gw_table</varname> module parameter</title>
		<programlisting format="linespecific">
...
modparam("lcr", "lcr_gw_table","gw")
...
</programlisting>
		</example>
	</section>

	<section>
		<title><varname>id_column</varname> (string)</title>
		<para>
		Name of the auto-increment, primary key column.  Common
		to all lcr module tables.
		</para>
		<para>
		<emphasis>
			Default value is <quote>id</quote>.
		</emphasis>
		</para>
		<example>
		<title>Setting <varname>id_column</varname> module
		parameter</title>
		<programlisting format="linespecific">
...
modparam("lcr", "id_column", "row_id")
...
</programlisting>
		</example>
	</section>

	<section>
		<title><varname>lcr_id_column</varname> (string)</title>
		<para>
		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.
		</para>
		<para>
		<emphasis>
			Default value is <quote>lcr_id</quote>.
		</emphasis>
		</para>
		<example>
		<title>Setting <varname>lcr_id_column</varname> module
		parameter</title>
		<programlisting format="linespecific">
...
modparam("lcr", "lcr_id_column", "lcr_identifier")
...
</programlisting>
		</example>
	</section>

	<section>
		<title><varname>gw_name_column</varname> (string)</title>
		<para>
		Name of the column holding gateway's name for
		documentation purpose.
		</para>
		<para>
		<emphasis>
			Default value is <quote>gw_name</quote>.
		</emphasis>
		</para>
		<example>
		<title>Setting <varname>gw_name_column</varname> module parameter</title>
		<programlisting format="linespecific">
...
modparam("lcr", "gw_name_column", "name")
...
</programlisting>
		</example>
	</section>

	<section>
		<title><varname>ip_addr_column</varname> (string)</title>
		<para>
		Name of the column holding the IPv4 or IPv6 address of
		the gateway.
		</para>
		<para>
		<emphasis>
			Default value is <quote>ip_addr</quote>.
		</emphasis>
		</para>
		<example>
		<title>Setting <varname>ip_addr_column</varname> module
		parameter</title>
		<programlisting format="linespecific">
...
modparam("lcr", "ip_addr_column", "ip")
...
</programlisting>
		</example>
	</section>

	<section>
		<title><varname>hostname_column</varname> (string)</title>
		<para>
		Name of the column holding gateway's hostname that is
		used in Request-URI hostpart, when request is sent to the
		gateway.
		</para>
		<para>
		<emphasis>
			Default value is <quote>hostname</quote>.
		</emphasis>
		</para>
		<example>
		<title>Setting <varname>hostname_column</varname> module parameter</title>
		<programlisting format="linespecific">
...
modparam("lcr", "hostname_column", "host")
...
</programlisting>
		</example>
	</section>

	<section>
		<title><varname>port_column</varname> (string)</title>
		<para>
		Name of the column holding the port number of the gateway.
		</para>
		<para>
		<emphasis>
			Default value is <quote>port</quote>.
		</emphasis>
		</para>
		<example>
		<title>Setting <varname>port_column</varname> module parameter</title>
		<programlisting format="linespecific">
...
modparam("lcr", "port_column", "port")
...
</programlisting>
		</example>
	</section>

	<section>
		<title><varname>params_column</varname> (string)</title>
		<para>
		Name of the column holding gateway's parameters that is
		used in Request-URI, when request is sent to the
		gateway.
		</para>
		<para>
		<emphasis>
			Default value is <quote>params</quote>.
		</emphasis>
		</para>
		<example>
		<title>Setting <varname>params_column</varname> module parameter</title>
		<programlisting format="linespecific">
...
modparam("lcr", "params_column", "parameters")
...
</programlisting>
		</example>
	</section>

	<section>
		<title><varname>uri_scheme_column</varname> (string)</title>
		<para>
		Name of the column holding the uri scheme of the gateway.
		</para>
		<para>
		<emphasis>
			Default value is <quote>uri_scheme</quote>.
		</emphasis>
		</para>
		<example>
		<title>Setting <varname>uri_scheme_column</varname> module
		parameter</title>
		<programlisting format="linespecific">
...
modparam("lcr", "uri_scheme_column", "uri_scheme")
...
</programlisting>
		</example>
	</section>

	<section>
		<title><varname>transport_column</varname> (string)</title>
		<para>
		Name of the column holding the transport protocol to be
		used for the gateway.
		</para>
		<para>
		<emphasis>
			Default value is <quote>transport</quote>.
		</emphasis>
		</para>
		<example>
		<title>Setting <varname>transport_column</varname> module
		parameter</title>
		<programlisting format="linespecific">
...
modparam("lcr", "transport_column", "trans")
...
</programlisting>
		</example>
	</section>

	<section>
		<title><varname>strip_column</varname> (string)</title>
		<para>
		Name of the column holding the number of characters
		to be stripped from the front of Request-URI user part
		before inserting tag.
		</para>
		<para>
		<emphasis>
			Default value is <quote>strip</quote>.
		</emphasis>
		</para>
		<example>
		<title>Setting <varname>strip_column</varname> module
		parameter</title>
		<programlisting format="linespecific">
...
modparam("lcr", "strip_column", "strip_count")
...
</programlisting>
		</example>
	</section>

	<section>
		<title><varname>tag_column</varname> (string)</title>
		<para>
		Name of the column holding gateway specific tag string
		that is added to Request URI userpart after stripping.
		</para>
		<para>
		<emphasis>
			Default value is <quote>tag</quote>.
		</emphasis>
		</para>
		<example>
		<title>Setting <varname>tag_column</varname> module parameter</title>
		<programlisting format="linespecific">
...
modparam("lcr", "tag_column", "gw_tag")
...
</programlisting>
		</example>
	</section>

	<section>
		<title><varname>flags_column</varname> (string)</title>
		<para>
		Name of the column holding gateway specific flag values.
		</para>
		<para>
		<emphasis>
			Default value is <quote>flags</quote>.
		</emphasis>
		</para>
		<example>
		<title>Setting <varname>flags_column</varname> module parameter</title>
		<programlisting format="linespecific">
...
modparam("lcr", "flags_column", "gw_flags")
...
</programlisting>
		</example>
	</section>

	<section>
		<title><varname>defunct_column</varname> (string)</title>
		<para>
		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.
		</para>
		<para>
		<emphasis>
			Default value is <quote>defunct</quote>.
		</emphasis>
		</para>
		<example>
		<title>Setting <varname>defunct_column</varname> module parameter</title>
		<programlisting format="linespecific">
...
modparam("lcr", "defunct_column", "defunct_until")
...
</programlisting>
		</example>
	</section>

	<section id="lcr.p.lcr_rule_table">
		<title><varname>lcr_rule_table</varname> (string)</title>
		<para>
		Name of the table holding the LCR rules.
		</para>
		<para>
		<emphasis>
			Default value is <quote>lcr_rule</quote>.
		</emphasis>
		</para>
		<example>
		<title>Setting <varname>lcr_rule_table</varname> module parameter</title>
		<programlisting format="linespecific">
...
modparam("lcr", "lcr_rule_table", "rules")
...
</programlisting>
		</example>
	</section>

	<section>
		<title><varname>prefix_column</varname> (string)</title>
		<para>
		Name of the column in lcr_rule and lcr_gw tables holding prefix
        of Request-URI user part and prefix of gateway, respectively.
		</para>
		<para>
		<emphasis>
			Default value is <quote>prefix</quote>.
		</emphasis>
		</para>
		<example>
		<title>Setting <varname>prefix_column</varname> module
		parameter</title>
		<programlisting format="linespecific">
...
modparam("lcr", "prefix_column", "number_prefix")
...
</programlisting>
		</example>
	</section>

	<section>
		<title><varname>from_uri_column</varname> (string)</title>
		<para>
		Name of the column holding the From (caller's) URI.
		</para>
		<para>
		<emphasis>
			Default value is <quote>from_uri</quote>.
		</emphasis>
		</para>
		<example>
		<title>Setting <varname>from_uri_column</varname> module parameter</title>
		<programlisting format="linespecific">
...
modparam("lcr", "from_uri_column", "caller_uri")
...
</programlisting>
		</example>
	</section>

	<section>
		<title><varname>mt_tvalue_column</varname> (string)</title>
		<para>
		Name of the column holding mtree tvalue.
		</para>
		<para>
		<emphasis>
			Default value is <quote>mt_tvalue</quote>.
		</emphasis>
		</para>
		<example>
		<title>Setting <varname>from_uri_column</varname> module parameter</title>
		<programlisting format="linespecific">
...
modparam("lcr", "mt_tvalue_column", "tree_value")
...
</programlisting>
		</example>
	</section>

	<section>
		<title><varname>request_uri_column</varname> (string)</title>
		<para>
		Name of the column holding the regular expression to match
		against the complete request URI (including the "sip:" prefix).
		</para>
		<para>
		<emphasis>
			Default value is <quote>request_uri</quote>.
		</emphasis>
		</para>
		<example>
		<title>Setting <varname>request_uri_column</varname> module parameter</title>
		<programlisting format="linespecific">
...
modparam("lcr", "request_uri_column", "callee_uri")
...
</programlisting>
		</example>
	</section>

	<section>
		<title><varname>stopper_column</varname> (string)</title>
		<para>
		Name of the column holding rule's stopper attribute.
		</para>
		<para>
		<emphasis>
			Default value is <quote>stopper</quote>.
		</emphasis>
		</para>
		<example>
		<title>Setting <varname>stopper_column</varname> module
		parameter
			</title>
		<programlisting format="linespecific">
...
modparam("lcr", "stopper_column", "stop")
...
</programlisting>
		</example>
	</section>

	<section>
		<title><varname>enabled_column</varname> (string)</title>
		<para>
		Name of the column telling is the rule is currently
		enabled or disabled.
		</para>
		<para>
		<emphasis>
			Default value is <quote>enabled</quote>.
		</emphasis>
		</para>
		<example>
		<title>Setting <varname>enabled_column</varname> module
		parameter
		</title>
		<programlisting format="linespecific">
...
modparam("lcr", "enabled_column", "in_use")
...
</programlisting>
		</example>
	</section>

	<section id="lcr.p.lcr_rule_target_table">
		<title><varname>lcr_rule_target_table</varname> (string)</title>
		<para>
		Name of the table holding information about the LCR rule
		targets (gateways).
		</para>
		<para>
		<emphasis>
			Default value is <quote>lcr_rule_target</quote>.
		</emphasis>
		</para>
		<example>
		<title>Setting <varname>lcr_rule_target_table</varname>
		module parameter</title>
		<programlisting format="linespecific">
...
modparam("lcr", "lcr_rule_target_table", "rules")
...
</programlisting>
		</example>
	</section>

	<section>
		<title><varname>rule_id_column</varname> (string)</title>
		<para>
		Name of lcr_rule_target_table column containing an id of
		lcr_rule table.
		</para>
		<para>
		<emphasis>
			Default value is <quote>rule_id</quote>.
		</emphasis>
		</para>
		<example>
		<title>Setting <varname>rule_id_column</varname> module
		parameter
			</title>
		<programlisting format="linespecific">
...
modparam("lcr", "rule_id_column", "rule")
...
</programlisting>
		</example>
	</section>

	<section>
		<title><varname>gw_id_column</varname> (string)</title>
		<para>
		Name of lcr_rule_target_table column containing an id of
		lcr_gw table.
		</para>
		<para>
		<emphasis>
			Default value is <quote>gw_id</quote>.
		</emphasis>
		</para>
		<example>
		<title>Setting <varname>gw_id_column</varname> module
		parameter
			</title>
		<programlisting format="linespecific">
...
modparam("lcr", "gw_id_column", "gw")
...
</programlisting>
		</example>
	</section>

	<section>
		<title><varname>priority_column</varname> (string)</title>
		<para>
		Name of the column holding the priority of the rule
		target.
		</para>
		<para>
		<emphasis>
			Default value is <quote>priority</quote>.
		</emphasis>
		</para>
		<example>
		<title>Setting <varname>priority_column</varname> module
		parameter</title>
		<programlisting format="linespecific">
...
modparam("lcr", "priority_column", "priority")
...
</programlisting>
		</example>
	</section>

	<section>
		<title><varname>weight_column</varname> (string)</title>
		<para>
		Name of the column holding weight of rule target.
		</para>
		<para>
		<emphasis>
			Default value is <quote>weight</quote>.
		</emphasis>
		</para>
		<example>
		<title>Setting <varname>weight_column</varname> module
		parameter</title>
		<programlisting format="linespecific">
...
modparam("lcr","weight_column", "target_weight")
...
</programlisting>
		</example>
	</section>

	<section id="lcr.p.lcr_count">
		<title><varname>lcr_count</varname> (integer)</title>
		<para>
		Maximum value of lcr_id.
		</para>
		<para>
		<emphasis>
			Default value is 1.
		</emphasis>
		</para>
		<example>
		<title>
		Setting <varname>lcr_count</varname> module
		parameter
		</title>
		<programlisting format="linespecific">
...
modparam("lcr", "lcr_count", 10)
...
</programlisting>
                </example>
	</section>

	<section id="lcr.p.gw_uri_avp">
		<title><varname>gw_uri_avp</varname> (AVP string)</title>
		<para>
		Internal AVP that load_gws() function uses to store
		information of matching gateways.
		</para>
		<para>
		<emphasis>
			There is NO default value, thus this variable must
			be defined in &siprouterconfig;.
		</emphasis>
		</para>
		<example>
		<title>Setting <varname>gw_uri_avp</varname> module
		parameter</title>
		<programlisting format="linespecific">
...
modparam("lcr", "gw_uri_avp", "$avp(lcr_gwuri)")
...
</programlisting>
		</example>
	</section>

	<section id="lcr.p.ruri_user_avp">
		<title><varname>ruri_user_avp</varname> (AVP string)</title>
		<para>
   		Internal AVP that next_gw function uses to store
		Request-URI user for subsequent next_gw calls.
		</para>
		<para>
		<emphasis>
			There is NO default value, thus this variable must
			be defined in &siprouterconfig;.
		</emphasis>
		</para>
		<example>
		<title>Setting <varname>ruri_user_avp</varname> module parameter</title>
		<programlisting format="linespecific">
...
modparam("lcr", "ruri_user_avp", "$avp(lcr_ruri_user)")
...
</programlisting>
		</example>
	</section>

	<section id="lcr.p.tag_avp">
		<title><varname>tag_avp</varname> (AVP string)</title>
		<para>
		If defined, an AVP where successful next_gw and from_gw
		functions store gateway's tag.
		</para>
		<para>
		<emphasis>
			There is NO default value, i.e, if not defined,
			gateway's tag is not stored anywhere.
		</emphasis>
		</para>
		<example>
		<title>Setting <varname>tag_avp</varname> module parameter</title>
		<programlisting format="linespecific">
...
modparam("lcr", "tag_avp", "$avp(lcr_tag)")
...
</programlisting>
		</example>
	</section>

	<section id="lcr.p.flags_avp">
		<title><varname>flags_avp</varname> (AVP string)</title>
		<para>
		If defined, an AVP where successful next_gw and from_gw
		functions store gateway's flags.
		</para>
		<para>
		<emphasis>
			There is NO default value, i.e, if not defined,
			gateway's flags are not stored anywhere.
		</emphasis>
		</para>
		<example>
		<title>Setting <varname>flags_avp</varname> module parameter</title>
		<programlisting format="linespecific">
...
modparam("lcr", "flags_avp", "$avp(lcr_flags)")
...
</programlisting>
		</example>
	</section>

	<section id="lcr.p.rule_id_avp">
		<title><varname>rule_id_avp</varname> (AVP string)</title>
		<para>
			If defined, an AVP where successful next_gw and from_gw
			functions store matching rule's id.
		</para>
		<para>
			<emphasis>
				There is NO default value, i.e, if not defined,
				matching rule's id is not stored anywhere.
			</emphasis>
		</para>
		<example>
			<title>Setting <varname>rule_id_avp</varname> module parameter</title>
			<programlisting format="linespecific">
...
modparam("lcr", "rule_id_avp", "$avp(lcr_ruleid)")
...
			</programlisting>
		</example>
	</section>

	<section id="lcr.p.mt_pv_values">
		<title><varname>mt_pv_values</varname> (AVP string)</title>
		<para>
		If defined and mt_value is given for a rule, load_gws() matches
        caller URI userpart to a mtree given as mtree parameter.
		</para>
		<para>
		If defined, must have the same value as mtree module pv_values
        parameter.
		</para>
		<para>
		<emphasis>
			There is NO default value.
		</emphasis>
		</para>
		<example>
		<title>Setting <varname>tag_avp</varname> module parameter</title>
		<programlisting format="linespecific">
...
modparam("lcr", "mt_pv_values", "$avp(lcr_mt_values)")
...
</programlisting>
		</example>
	</section>

	<section id="lcr.p.mtree">
		<title><varname>mtree</varname> (string)</title>
		<para>
		Name of mtree to which load_gws() matches caller URI userpart.
		</para>
		<para>
		<emphasis>
			Default value is "lcr".
		</emphasis>
		</para>
		<example>
		<title>Setting <varname>mtree</varname> module parameter</title>
		<programlisting format="linespecific">
...
modparam("lcr", "mtree", "caller_tree")
...
</programlisting>
		</example>
	</section>

	<section id="lcr.p.defunct_capability">
		<title><varname>defunct_capability</varname> (integer)</title>
		<para>
		Tells if defunct capability of (non-responsive) gateways is
		supported.  Non-zero value turns on defunct capability.
		</para>
		<para>
		<emphasis>
			Default value is 0.
		</emphasis>
		</para>
		<example>
		<title>
		Setting <varname>defunct_capability</varname> module
		parameter
		</title>
		<programlisting format="linespecific">
...
modparam("lcr", "defunct_capability", 1)
...
</programlisting>
                </example>
	</section>

	<section id="lcr.p.lcr_id_avp">
		<title><varname>lcr_id_avp</varname> (AVP string)</title>
		<para>
		Internal AVP that load_gws() function uses to store
		LCR instance identifier of loaded gateways.  Only needed if
		gateway defunct capability has been activated.
		</para>
		<para>
		<emphasis>
			There is NO default value.
		</emphasis>
		</para>
		<example>
		<title>Setting <varname>lcr_id_avp</varname> module
		parameter</title>
		<programlisting format="linespecific">
...
modparam("lcr", "lcr_id_avp", "$avp(lcr_id)")
...
</programlisting>
		</example>
	</section>

	<section id="lcr.p.defunct_gw_avp">
		<title><varname>defunct_gw_avp</varname> (AVP string)</title>
		<para>
		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.
		</para>
		<para>
		<emphasis>
			There is NO default value.
		</emphasis>
		</para>
		<example>
		<title>Setting <varname>defunct_gw_avp</varname> module
		parameter</title>
		<programlisting format="linespecific">
...
modparam("lcr", "defunct_gw_avp", "$avp(lcr_defunct_gw)")
...
</programlisting>
		</example>
	</section>

	<section id="lcr.p.lcr_rule_hash_size">
		<title><varname>lcr_rule_hash_size</varname> (integer)</title>
		<para>
		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.
		</para>
		<para>
		<emphasis>
			Default value is 128.
		</emphasis>
		</para>
		<example>
		<title>
		Setting <varname>lcr_rule_hash_size</varname> module
		parameter
		</title>
		<programlisting format="linespecific">
...
modparam("lcr", "lcr_rule_hash_size", 1024)
...
</programlisting>
                </example>
	</section>

	<section id="lcr.p.lcr_gw_count">
		<title><varname>lcr_gw_count</varname> (integer)</title>
		<para>
		Defines the maximum number of gateways in lcr_gw table.
		</para>
		<para>
		<emphasis>
			Default value is 128.
		</emphasis>
		</para>
		<example>
		<title>
		Setting <varname>lcr_gw_count</varname> module parameter
		</title>
		<programlisting format="linespecific">
...
modparam("lcr", "lcr_gw_count", 1024)
...
</programlisting>
                </example>
	</section>

	<section id="lcr.p.dont_strip_or_prefix_flag">
		<title><varname>dont_strip_or_prefix_flag</varname> (integer)</title>
		<para>
		Defines the flag number used to tell if stripping and
		tagging is done for the selected gateway.
		</para>
		<para>
		<emphasis>
			Default value is -1 meaning that the flag is not
		defined.
		</emphasis>
		</para>
		<example>
		<title>
		Setting <varname>dont_strip_or_prefix_flag</varname> module
		parameter
		</title>
		<programlisting format="linespecific">
...
modparam("lcr", "dont_strip_or_prefix_flag", 10)
...
</programlisting>
                </example>
	</section>

	<section id="lcr.p.priority_ordering">
		<title><varname>priority_ordering</varname> (integer)</title>
		<para>
		Defines how matching gateways are ordered (see Overview section).
		</para>
		<para>
		<emphasis>
			Default value is 0.
		</emphasis>
		</para>
		<example>
		<title>
		Setting <varname>priority_ordering</varname> module
		parameter
		</title>
		<programlisting format="linespecific">
...
modparam("lcr", "priority_ordering", 1)
...
</programlisting>
                </example>
	</section>

	<section id="lcr.p.fetch_rows">
		<title><varname>fetch_rows</varname> (integer)</title>
		<para>
		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.
		</para>
		<para>
		<emphasis>
			Default value is <quote>1024</quote>.
		</emphasis>
		</para>
		<example>
		<title>Set <varname>fetch_rows</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("lcr", "fetch_rows", 3000)
...
</programlisting>
		</example>
	</section>

	<section id="lcr.p.ping_interval">
		<title><varname>ping_interval</varname> (integer)</title>
		<para>
		  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
		  <varname>ping_valid_reply_codes</varname>) to a ping
		  request, it is marked again as active.
		</para>
		<para>
		  If value of this parameter is greater
		  than zero, tm module must have been loaded and
		  parameters <varname>lcr_id_avp</varname> and
		  <varname>defunct_gw_avp</varname> must have been defined.
		  Value <quote>0</quote> disables sending of OPTIONS ping
		  requests to failed gateways.
		</para>
		<para>
		<emphasis>
			Default value is <quote>0</quote>.
		</emphasis>
		</para>
		<example>
		<title>
		Set <varname>ping_interval</varname> parameter
		</title>
<programlisting format="linespecific">
...
modparam("lcr", "ping_interval", 15)
...
</programlisting>
		</example>
	</section>

	<section id="lcr.p.ping_inactivate_threshold">
		<title><varname>ping_inactivate_threshold</varname> (integer)</title>
		<para>
		  Tells after how many failures (= inactivate_gw() function
		  calls) a gateway is marked as inactive.
		</para>
		<para>
		<emphasis>
			Default value is <quote>1</quote>, i.e., gateway is
			inactivated after first failure.
		</emphasis>
		</para>
		<example>
		<title>
		Set <varname>ping_inactivate_threshold</varname> parameter
		</title>
<programlisting format="linespecific">
...
modparam("lcr", "ping_inactivate_threshold", 3)
...
</programlisting>
		</example>
	</section>

	<section id="lcr.p.ping_valid_reply_codes">
		<title><varname>ping_valid_reply_codes</varname> (string)</title>
		<para>
		  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.
		</para>
		<para>
		<emphasis>
			Default value is <quote></quote>, i.e., only 2xx
			replies are considered as valid replies.
		</emphasis>
		</para>
		<example>
		<title>
		Set <varname>ping_valid_reply_codes</varname> parameter
		</title>
<programlisting format="linespecific">
...
modparam("lcr", "ping_valid_reply_codes", "403,405,501")
...
</programlisting>
		</example>
	</section>

	<section id="lcr.p.ping_from">
		<title><varname>ping_from</varname> (string)</title>
		<para>
		  From URI used in OPTIONS ping requests.
		</para>
		<para>
		<emphasis>
			Default value is <quote>sip:pinger@localhost</quote>.
		</emphasis>
		</para>
		<example>
		<title>
		Set <varname>ping_from</varname> parameter
		</title>
<programlisting format="linespecific">
...
modparam("lcr", "ping_from", "sip:proxy.operator.com")
...
</programlisting>
		</example>
	</section>

	<section id="lcr.p.ping_socket">
		<title><varname>ping_socket</varname> (string)</title>
		<para>
		  Socket to be used for sending OPTIONS ping request.  If not
		  set or set to <quote></quote>, default socket is used.
		</para>
		<para>
		<emphasis>
			Default value is <quote></quote>.
		</emphasis>
		</para>
		<example>
		<title>
		Set <varname>ping_socket</varname> parameter
		</title>
<programlisting format="linespecific">
...
modparam("lcr", "ping_socket", "192.98.102.10:5060")
...
</programlisting>
		</example>
	</section>

	</section>

	<section>
	<title>Functions</title>
	<section id="lcr.f.load_gws">
		<title>
		<function moreinfo="none">load_gws(lcr_id[, uri_user[, caller_uri]])
		</function>
		</title>
		<para>
		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.
		</para>
		<para>
		Returns 1 if at least one matching gateway was found, 2
		if no matching gateways was found, and -1 on error.
		</para>
		<para>
		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.
		</para>
		<para>
		This function can be used from REQUEST_ROUTE.
		</para>
		<example>
		<title><function>load_gws</function> usage</title>
		<programlisting format="linespecific">
...
if (!load_gws(1, $rU, $var(caller_uri))) {
	sl_send_reply("500", "Server Internal Error - Cannot load gateways");
	exit;
};
...
</programlisting>
		</example>
	</section>

	<section id="lcr.f.next_gw">
		<title>
		<function moreinfo="none">next_gw()</function>
		</title>
		<para>
		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.
		</para>
		<para>
		Upon subsequent calls, does the same as in above, but
		takes user part of Request-URI from ruri_user_avp.
		</para>
		<para>
		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.
		</para>
		<para>
		Returns 1 on success and -1 if there were no gateways
		left or if an error occurred (see syslog).
		</para>
		<para>
		Must be preceded by successful load_gws() call.
		</para>
		<para>
		This function can be used from REQUEST_ROUTE, FAILURE_ROUTE.
		</para>
		<example>
		<title><function>next_gw</function> usage from a route block</title>
		<programlisting format="linespecific">
...
if (!next_gw()) {
	sl_send_reply("503", "Service not available - No gateways");
	exit;
};
...
</programlisting>
		</example>
		<example>
		<title><function>next_gw</function> usage from a failure
		route block
			</title>
		<programlisting format="linespecific">
...
if (!next_gw()) {
	t_reply("503", "Service not available - No more gateways");
	exit;
};
...
</programlisting>
		</example>
	</section>

	<section id="lcr.f.inactivate_gw">
		<title>
		<function moreinfo="none">inactivate_gw()</function>
		</title>
		<para>
		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 <varname>ping_interval</varname> 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.
		</para>
		<para>
		Returns 1 on success and -1 in case of error (see syslog).
		</para>
		<para>
		This function can be used from REQUEST_ROUTE and FAILURE_ROUTE.
		</para>
		<example>
		<title><function>inactivate_gw</function> usage</title>
		<programlisting format="linespecific">
...
failure_route [GW_FAILURE] {
...
    if (t_check_status("408|503")) {
        inactivate_gw();
    };
...
		</programlisting>
		</example>
	</section>

	<section id="lcr.f.defunct_gw">
		<title>
		<function moreinfo="none">defunct_gw(period)</function>
		</title>
		<para>
		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.
		</para>
		<para>
		Returns 1 on success and -1 in case of error (see syslog).
		</para>
		<para>
		This function can be used from REQUEST_ROUTE, FAILURE_ROUTE.
		</para>
		<example>
		<title><function>defunct_gw</function> usage</title>
		<programlisting format="linespecific">
...
defunct_gw(60);
...
</programlisting>
		</example>
	</section>

	<section id="lcr.f.from_gw">
		<title>
		<function moreinfo="none">from_gw(lcr_id[, ip_addr, proto[, src_port]])</function>
		</title>
		<para>
		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.
		</para>
		<para>
		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.
		</para>
		<para>
		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.
		</para>
		<para>
		Returns 1 on success and -1 on failure or on error.
		</para>
		<para>
		Execution time of from_gw() function is O(log N),
		where N is number of gateways in the LCR instance.
		</para>
		<para>
		This function can be used from REQUEST_ROUTE, FAILURE_ROUTE,
		ONREPLY_ROUTE.
		</para>
		<example>
		<title><function>from_gw</function> usage</title>
		<programlisting format="linespecific">
...
if (from_gw(1, $avp(real_source_addr), 2, 5060) {
	...
};
...
</programlisting>
		</example>
		</section>

	<section id="lcr.f.from_any_gw">
		<title>
		<function moreinfo="none">from_any_gw([ip_addr, proto[, src_port]])</function>
		</title>
		<para>
		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.
		</para>
		<para>
		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.
		</para>
		<para>
		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.
		</para>
		<para>
		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.
		</para>
		<para>
		This function can be used from REQUEST_ROUTE, FAILURE_ROUTE,
		ONREPLY_ROUTE.
		</para>
		<example>
		<title><function>from_gw</function> usage</title>
		<programlisting format="linespecific">
...
$var(lcr_id) = from_any_gw("192.168.1.1", 3, 5061);
...
</programlisting>
		</example>
		</section>

		<section id="lcr.f.to_gw">
		<title>
		<function moreinfo="none">to_gw(lcr_id[, ip_addr, proto])</function>
		</title>
		<para>
		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.
		</para>
		<para>
		Returns 1 on success and -1 on failure and error.
		</para>
		<para>
		Execution time of to_gw() function is O(log N),
		where N is number of gateways in the LCR instance.
		</para>
		<para>
		This function can be used from REQUEST_ROUTE, FAILURE_ROUTE.
		</para>
		<example>
		<title><function>to_gw</function> usage</title>
		<programlisting format = "linespecific">
...
if (to_gw("1")) {
	...
	exit;
};
...
</programlisting>
		</example>
	</section>

	<section id="lcr.f.to_any_gw">
		<title>
		<function moreinfo="none">to_any_gw([ip_addr, proto])</function>
		</title>
		<para>
		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.
		</para>
		<para>
		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.
		</para>
		<para>
		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.
		</para>
		<para>
		This function can be used from REQUEST_ROUTE, FAILURE_ROUTE.
		</para>
		<example>
		<title><function>to_gw</function> usage</title>
		<programlisting format = "linespecific">
...
if (to_any_gw("192.55.66.2", 1)) {
	...
	exit;
};
...
</programlisting>
		</example>
	</section>

	</section>

	<section>
	<title>RPC Commands</title>
		<section id="lcr.rpc.reload">
		<title><function>lcr.reload</function></title>
		<para>
			Causes lcr module to re-read the contents of
			LCR tables into memory.
		</para>
		<para>
		Name: <emphasis>lcr.reload</emphasis>
		</para>
		<para>Parameters: <emphasis>none</emphasis></para>
		<example>
		<title><function>lcr.reload</function> RPC example</title>
		<programlisting  format="linespecific">
		$ &sercmd; lcr.reload
		</programlisting>
		</example>
		</section>

		<section id="lcr.rpc.dump_gws">
		<title><function>lcr.dump_gws</function></title>
		<para>
			Causes lcr module to dump the contents of its
			in-memory gw table.
		</para>
		<para>Parameters: <emphasis>none</emphasis></para>
		<example>
		<title><function>lcr.dump_gws</function> RPC example</title>
		<programlisting  format="linespecific">
		$ &sercmd; lcr.dump_gws
		</programlisting>
		</example>
		</section>

		<section id="lcr.rpc.dump_rules">
		<title><function>lcr.dump_rules</function></title>
		<para>
			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.
		</para>
		<para>Parameters:</para>
		<itemizedlist>
			<listitem><para>
			<emphasis>lcr_id</emphasis> filter rules based on lcr_id.
			</para></listitem>
			<listitem><para>
			<emphasis>prefix</emphasis> filter rules based on prefix
			(prefixes shorter or equal to the give prefix) in addition
			to the previous lcr_id parameter.
			</para></listitem>
		</itemizedlist>
 		<example>
		<title><function>lcr.dump_rules</function> RPC example</title>
        <programlisting  format="linespecific">
		$ &sercmd; lcr.dump_rules
		</programlisting>
		</example>
		</section>

		<section id="lcr.rpc.load_gws">
		<title><function>lcr.load_gws</function></title>
		<para>
			Loads gateways and prints ids of matching ones in priority order.
		</para>
		<para>
		Name: <emphasis>lcr.load_gws</emphasis>
		</para>
		<para>Parameters: <emphasis>lcr_id uri_user [caller_uri request_uri]</emphasis></para>
		<example>
		<title><function>lcr.load_gws</function> RPC example</title>
		<programlisting  format="linespecific">
		$ &sercmd; lcr.load_gws 2 s:0447058050
		</programlisting>
		</example>
		</section>

		<section id="lcr.rpc.defunct_gw">
		<title><function>lcr.defunct_gw</function></title>
		<para>
			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.
		</para>
		<para>
		Name: <emphasis>lcr.defunct_gw</emphasis>
		</para>
		<para>Parameters: <emphasis>lcr_id gw_id period</emphasis></para>
		<example>
		<title><function>lcr.defunct_gw</function> RPC example</title>
		<programlisting  format="linespecific">
		$ &sercmd; lcr.defunct_gw 1 4 120
		</programlisting>
		</example>
		</section>
	</section>
	<section>
	<title>Known Limitations</title>
	<para>
		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.
	</para>
	</section>
</chapter>
