Skip to content

Commit

Permalink
WIP integrated kamailio LB
Browse files Browse the repository at this point in the history
  • Loading branch information
sboily committed Oct 6, 2015
1 parent d3d6bcf commit 1ee88d3
Show file tree
Hide file tree
Showing 5 changed files with 266 additions and 1 deletion.
14 changes: 14 additions & 0 deletions contribs/kamailio/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from debian:jessie

MAINTAINER Sylvain Boily "[email protected]"

RUN echo "deb http://deb.kamailio.org/kamailio jessie main" > /etc/apt/sources.list.d/kamailio.list
RUN apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 0xfb40d3e6508ea4c8
RUN apt-get -yqq update \
&& apt-get -yqq install kamailio

ADD config/* /etc/kamailio/

EXPOSE 5060/udp

CMD kamailio -ddDDe
1 change: 1 addition & 0 deletions contribs/kamailio/config/dispatcher.list
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 sip:asterisk:5060
240 changes: 240 additions & 0 deletions contribs/kamailio/config/kamailio.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
debug=4
log_stderror=yes

memdbg=5
memlog=5

disable_tcp=yes
auto_aliases=no

fork=no
children=4

dns_cache_init=no
use_dns_cache=no

port=5060
listen=udp:0.0.0.0:5060

# ------------------ module loading ----------------------------------
mpath="/usr/lib/x86_64-linux-gnu/kamailio/modules"
loadmodule "mi_fifo.so"
loadmodule "kex.so"
loadmodule "tm.so"
loadmodule "tmx.so"
loadmodule "sl.so"
loadmodule "rr.so"
loadmodule "pv.so"
loadmodule "maxfwd.so"
loadmodule "textops.so"
loadmodule "siputils.so"
loadmodule "xlog.so"
loadmodule "sanity.so"
loadmodule "ctl.so"
loadmodule "mi_rpc.so"
loadmodule "acc.so"
loadmodule "dispatcher.so"
loadmodule "path.so"

# ----------------- setting module-specific parameters ---------------

# ----- mi_fifo params -----
modparam("mi_fifo", "fifo_name", "/var/run/kamailio/kamailio_fifo")
modparam("ctl", "binrpc", "unix:/var/run/kamailio/kamailio_ctl")

# ----- rr params -----
modparam("rr", "enable_full_lr", 1)
modparam("rr", "append_fromtag", 0)

# ----- tm params -----
modparam("tm", "fr_timer", 2000)
modparam("tm", "fr_inv_timer", 40000)

# ----- acc params -----
modparam("acc", "log_flag", 1)
modparam("acc", "failed_transaction_flag", 3)
modparam("acc", "log_extra",
"src_user=$fU;src_domain=$fd;dst_ouser=$tU;dst_user=$rU;dst_domain=$rd;src_ip=$si")

# ----- path params -----
modparam("path", "use_received", 1)

# -- dispatcher params --
modparam("dispatcher", "list_file", "/etc/kamailio/dispatcher.list")
modparam("dispatcher", "flags", 2)
modparam("dispatcher", "dst_avp", "$avp(AVP_DST)")
modparam("dispatcher", "grp_avp", "$avp(AVP_GRP)")
modparam("dispatcher", "cnt_avp", "$avp(AVP_CNT)")
modparam("dispatcher", "ds_ping_method", "OPTIONS")
modparam("dispatcher", "ds_ping_interval", 30)
modparam("dispatcher", "ds_probing_mode", 1)
modparam("dispatcher", "ds_probing_threshold", 3)

####### Routing Logic ########

# main request routing logic

route {

# per request initial checks
route(REQINIT);

# handle requests within SIP dialogs
route(WITHINDLG);

### only initial requests (no To tag)

# CANCEL processing
if (is_method("CANCEL"))
{
if (t_check_trans())
t_relay();
exit;
}

t_check_trans();

# record routing for dialog forming requests (in case they are routed)
# - remove preloaded route headers
remove_hf("Route");
if (is_method("INVITE|SUBSCRIBE"))
record_route();

# account only INVITEs
if (is_method("INVITE"))
{
setflag(1); # do accounting
}

# handle presence related requests
route(PRESENCE);


# handle registrations
route(REGISTRAR);

if ($rU==$null)
{
# request with no Username in RURI
sl_send_reply("484","Address Incomplete");
exit;
}

# dispatch destinations
route(DISPATCH);
}

route[REGISTRAR] {
if (!is_method("REGISTER"))
{
if (!add_path("loadbalancer", "ob")) {
sl_send_reply("503", "Internal Path Error");
}

if(!ds_select_dst("1", "4"))
{
send_reply("404", "No destination");
exit;
}
xlog("L_DBG", "--- SCRIPT: going to <$ru> via <$du>\n");
rewritehost("$du");
route(DISPATCH);
}
}


route[RELAY] {
if (!t_relay()) {
sl_reply_error();
}
exit;
}

# Per SIP request initial checks
route[REQINIT] {
if (!mf_process_maxfwd_header("10")) {
sl_send_reply("483","Too Many Hops");
exit;
}

if(!sanity_check("1511", "7"))
{
xlog("Malformed SIP message from $si:$sp\n");
exit;
}
}

# Handle requests within SIP dialogs
route[WITHINDLG] {
if (has_totag()) {
# sequential request withing a dialog should
# take the path determined by record-routing
if (loose_route()) {
if (is_method("BYE")) {
setflag(1); # do accounting ...
setflag(3); # ... even if the transaction fails
}
route(RELAY);
} else {
if (is_method("SUBSCRIBE") && uri == myself) {
# in-dialog subscribe requests
route(PRESENCE);
exit;
}
if ( is_method("ACK") ) {
if ( t_check_trans() ) {
# non loose-route, but stateful ACK;
# must be ACK after a 487 or e.g. 404 from upstream server
t_relay();
exit;
} else {
# ACK without matching transaction ... ignore and discard.
exit;
}
}
sl_send_reply("404","Not here");
}
exit;
}
}

# Presence server route
route[PRESENCE] {
if(!is_method("PUBLISH|SUBSCRIBE"))
return;

sl_send_reply("404", "Not here");
exit;
}

# Dispatch requests
route[DISPATCH] {
# round robin dispatching on gateways group '1'
if(!ds_select_dst("1", "4"))
{
send_reply("404", "No destination");
exit;
}
xlog("L_DBG", "--- SCRIPT: going to <$ru> via <$du>\n");
t_on_failure("RTF_DISPATCH");
route(RELAY);
exit;
}

# Sample failure route
failure_route[RTF_DISPATCH] {
if (t_is_canceled()) {
exit;
}
# next DST - only for 500 or local timeout
if (t_check_status("500")
or (t_branch_timeout() and !t_branch_replied()))
{
if(ds_next_dst())
{
t_on_failure("RTF_DISPATCH");
route(RELAY);
exit;
}
}
}
2 changes: 1 addition & 1 deletion contribs/linphone/linphone.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

pulseaudio --system &> /tmp/pulse.log &
echo "Init linphone daemon"
linphonecsh init -a
linphonecsh init -a -d6 -S -l /tmp/linphone.log
sleep 1
echo "Register linphone daemon"
linphonecsh register --host asterisk --username sylvain --password sylvain
Expand Down
10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,17 @@ lb:
expose:
- "8888"
- "1936"
kamailio:
build: ./contribs/kamailio
links:
- consul
- asterisk
ports:
- "5060:5060/udp"
expose:
- 5060/udp
phone:
build: ./contribs/linphone
links:
- asterisk
- kamailio

0 comments on commit 1ee88d3

Please sign in to comment.