diff --git "a/_posts/2024-08-14-KamailioWorld-05sipp\346\265\213\350\257\225.md" "b/_posts/2024-08-14-KamailioWorld-05sipp\346\265\213\350\257\225.md" index df5261e4593..681e8879ff0 100644 --- "a/_posts/2024-08-14-KamailioWorld-05sipp\346\265\213\350\257\225.md" +++ "b/_posts/2024-08-14-KamailioWorld-05sipp\346\265\213\350\257\225.md" @@ -1,31 +1,583 @@ +--- +layout: post +title: Kamailio-sipp测试 -SipP是一个短小精悍的脚本工具,可以支持模拟客户端与服务端的模式 +subtitle: sipp +date: 2024-09-12 +author: Claire +header-img: img/post-bg-github-cup.jpg +catalog: true +tags: + - Kamailio + - sipp +--- -## About server +SIPp 是一个短小精悍的脚本工具,可以支持模拟客户端与服务端的模式,对于生产压测、功能测试都有一定帮助。 + +- [如何安装?](#如何安装) + - [编译好的版本](#编译好的版本) + - [自己构建](#自己构建) + - [环境和背景条件要求](#环境和背景条件要求) +- [如何使用?](#如何使用) + - [About server](#about-server) + - [About client](#about-client) + - [About testcase](#about-testcase) + - [successful call bye from client](#successful-call-bye-from-client) + +## 如何安装? + +可以参考[官方说明文档](https://sipp.readthedocs.io/en/latest/),一步步带你安装、了解特性、开始测试 + +### 编译好的版本 + +可以直接在 Github 下载稳定编译好的版本:[Github 版本发布页](https://github.com/SIPp/sipp/releases) + +### 自己构建 + +#### 环境和背景条件要求 + +官方是支持在 Linux和Cygwin(Windows平台上运行的UNIX模拟环境)下可使用,其他环境未经测试,可能可以使用。 + +此外你需要具备: + +- C++ 编译器 +- curses or ncurses library +- For TLS 支持: OpenSSL >= 0.9.8 or WolfSSL >= 3.15.0 +- For pcap play 支持: libpcap and libnet +- For SCTP 支持: lksctp-tools +- For distributed pauses: Gnu Scientific Libraries + +常规如果你不需要支持TLS/SCTP/PCAP 的话,只需要 + +```bash +tar -xvzf sipp-xxx.tar +cd sipp +cmake . +make +``` + +4个步骤就可以开启编译 + +## 如何使用? + +包含的参数属性很多: + +```bash +# ./sipp --help +/usr/bin/pager: No such file or directory + +Usage: + + sipp remote_host[:remote_port] [options] + +Example: + + Run SIPp with embedded server (uas) scenario: + ./sipp -sn uas + On the same host, run SIPp with embedded client (uac) scenario: + ./sipp -sn uac 127.0.0.1 + + Available options: + + +*** Scenario file options: + + -sd : Dumps a default scenario (embedded in the SIPp executable) + -sf : Loads an alternate XML scenario file. To learn more about XML scenario + syntax, use the -sd option to dump embedded scenarios. They contain all the + necessary help. + -oocsf : Load out-of-call scenario. + -oocsn : Load out-of-call scenario. + -sn : Use a default scenario (embedded in the SIPp executable). If this option is + omitted, the Standard SipStone UAC scenario is loaded. + Available values in this version: + + - 'uac' : Standard SipStone UAC (default). + - 'uas' : Simple UAS responder. + - 'regexp' : Standard SipStone UAC - with regexp and variables. + - 'branchc' : Branching and conditional branching in scenarios - client. + - 'branchs' : Branching and conditional branching in scenarios - server. + + Default 3pcc scenarios (see -3pcc option): + + - '3pcc-C-A' : Controller A side (must be started after all other 3pcc + scenarios) + - '3pcc-C-B' : Controller B side. + - '3pcc-A' : A side. + - '3pcc-B' : B side. + + +*** IP, port and protocol options: + + -t : Set the transport mode: + - u1: UDP with one socket (default), + - un: UDP with one socket per call, + - ui: UDP with one socket per IP address. The IP addresses must be defined + in the injection file. + - t1: TCP with one socket, + - tn: TCP with one socket per call, + - c1: u1 + compression (only if compression plugin loaded), + - cn: un + compression (only if compression plugin loaded). This plugin is + not provided with SIPp. + + -i : Set the local IP address for 'Contact:','Via:', and 'From:' headers. Default + is primary host IP address. + + -p : Set the local port number. Default is a random free port chosen by the + system. + -bind_local : Bind socket to local IP address, i.e. the local IP address is used as the + source IP address. If SIPp runs in server mode it will only listen on the + local IP address instead of all IP addresses. + -ci : Set the local control IP address + -cp : Set the local control port number. Default is 8888. + -max_socket : Set the max number of sockets to open simultaneously. This option is + significant if you use one socket per call. Once this limit is reached, + traffic is distributed over the sockets already opened. Default value is + 50000 + -max_reconnect : Set the the maximum number of reconnection. + -reconnect_close : Should calls be closed on reconnect? + -reconnect_sleep : How long (in milliseconds) to sleep between the close and reconnect? + -rsa : Set the remote sending address to host:port for sending the messages. + +*** SIPp overall behavior options: + + -v : Display version and copyright information. + -bg : Launch SIPp in background mode. + -nostdin : Disable stdin. + + -plugin : Load a plugin. + -sleep : How long to sleep for at startup. Default unit is seconds. + -skip_rlimit : Do not perform rlimit tuning of file descriptor limits. Default: false. + -buff_size : Set the send and receive buffer size. + -sendbuffer_warn : Produce warnings instead of errors on SendBuffer failures. + -lost : Set the number of packets to lose by default (scenario specifications + override this value). + -key : keyword value + Set the generic parameter named "keyword" to "value". + -set : variable value + Set the global variable parameter named "variable" to "value". + -tdmmap : Generate and handle a table of TDM circuits. + A circuit must be available for the call to be placed. + Format: -tdmmap {0-3}{99}{5-8}{1-31} + -dynamicStart : variable value + Set the start offset of dynamic_id variable + -dynamicMax : variable value + Set the maximum of dynamic_id variable + -dynamicStep : variable value + Set the increment of dynamic_id variable + +*** Call behavior options: + + -aa : Enable automatic 200 OK answer for INFO, NOTIFY, OPTIONS and UPDATE. + -base_cseq : Start value of [cseq] for each call. + -cid_str : Call ID string (default %u-%p@%s). %u=call_number, %s=ip_address, + %p=process_number, %%=% (in any order). + -d : Controls the length of calls. More precisely, this controls the duration of + 'pause' instructions in the scenario, if they do not have a 'milliseconds' + section. Default value is 0 and default unit is milliseconds. + -deadcall_wait : How long the Call-ID and final status of calls should be kept to improve + message and error logs (default unit is ms). + -auth_uri : Force the value of the URI for authentication. + By default, the URI is composed of remote_ip:remote_port. + -au : Set authorization username for authentication challenges. Default is taken + from -s argument + -ap : Set the password for authentication challenges. Default is 'password' + -s : Set the username part of the request URI. Default is 'service'. + -default_behaviors: Set the default behaviors that SIPp will use. Possible values are: + - all Use all default behaviors + - none Use no default behaviors + - bye Send byes for aborted calls + - abortunexp Abort calls on unexpected messages + - pingreply Reply to ping requests + - cseq Check CSeq of ACKs + If a behavior is prefaced with a -, then it is turned off. Example: + all,-bye + + -nd : No Default. Disable all default behavior of SIPp which are the following: + - On UDP retransmission timeout, abort the call by sending a BYE or a CANCEL + - On receive timeout with no ontimeout attribute, abort the call by sending + a BYE or a CANCEL + - On unexpected BYE send a 200 OK and close the call + - On unexpected CANCEL send a 200 OK and close the call + - On unexpected PING send a 200 OK and continue the call + - On unexpected ACK CSeq do nothing + - On any other unexpected message, abort the call by sending a BYE or a + CANCEL + + -pause_msg_ign : Ignore the messages received during a pause defined in the scenario + -callid_slash_ign: Don't treat a triple-slash in Call-IDs as indicating an extra SIPp prefix. + +*** Injection file options: + + -inf : Inject values from an external CSV file during calls into the scenarios. + First line of this file say whether the data is to be read in sequence + (SEQUENTIAL), random (RANDOM), or user (USER) order. + Each line corresponds to one call and has one or more ';' delimited data + fields. Those fields can be referred as [field0], [field1], ... in the xml + scenario file. Several CSV files can be used simultaneously (syntax: -inf + f1.csv -inf f2.csv ...) + -infindex : file field + Create an index of file using field. For example -inf ../path/to/users.csv + -infindex users.csv 0 creates an index on the first key. + -ip_field : Set which field from the injection file contains the IP address from which + the client will send its messages. + If this option is omitted and the '-t ui' option is present, then field 0 is + assumed. + Use this option together with '-t ui' + +*** RTP behaviour options: + + -mi : Set the local media IP address (default: local primary host IP address) + -rtp_echo : Enable RTP echo. RTP/UDP packets received on media port are echoed to their + sender. + RTP/UDP packets coming on this port + 2 are also echoed to their sender + (used for sound and video echo). + -mb : Set the RTP echo buffer size (default: 2048). + -min_rtp_port : Minimum port number for RTP socket range. + -max_rtp_port : Maximum port number for RTP socket range. + -rtp_payload : RTP default payload type. + -rtp_threadtasks : RTP number of playback tasks per thread. + -rtp_buffsize : Set the rtp socket send/receive buffer size. + -rtpcheck_debug : Write RTP check debug information to file + -audiotolerance : Audio error tolerance for RTP checks (0.0-1.0) -- default: 1.0 + -videotolerance : Video error tolerance for RTP checks (0.0-1.0) -- default: 1.0 + +*** Call rate options: + + -r : Set the call rate (in calls per seconds). This value can bechanged during + test by pressing '+', '_', '*' or '/'. Default is 10. + pressing '+' key to increase call rate by 1 * rate_scale, + pressing '-' key to decrease call rate by 1 * rate_scale, + pressing '*' key to increase call rate by 10 * rate_scale, + pressing '/' key to decrease call rate by 10 * rate_scale. + + -rp : Specify the rate period for the call rate. Default is 1 second and default + unit is milliseconds. This allows you to have n calls every m milliseconds + (by using -r n -rp m). + Example: -r 7 -rp 2000 ==> 7 calls every 2 seconds. + -r 10 -rp 5s => 10 calls every 5 seconds. + -rate_scale : Control the units for the '+', '-', '*', and '/' keys. + -rate_increase : Specify the rate increase every -rate_interval units (default is seconds). + This allows you to increase the load for each independent logging period. + Example: -rate_increase 10 -rate_interval 10s + ==> increase calls by 10 every 10 seconds. + -rate_max : If -rate_increase is set, then quit after the rate reaches this value. + Example: -rate_increase 10 -rate_max 100 + ==> increase calls by 10 until 100 cps is hit. + -rate_interval : Set the interval by which the call rate is increased. Defaults to the value + of -fd. + -no_rate_quit : If -rate_increase is set, do not quit after the rate reaches -rate_max. + -l : Set the maximum number of simultaneous calls. Once this limit is reached, + traffic is decreased until the number of open calls goes down. Default: + (3 * call_duration (s) * rate). + -m : Stop the test and exit when 'calls' calls are processed + -users : Instead of starting calls at a fixed rate, begin 'users' calls at startup, + and keep the number of calls constant. + +*** Retransmission and timeout options: + + -recv_timeout : Global receive timeout. Default unit is milliseconds. If the expected message + is not received, the call times out and is aborted. + -send_timeout : Global send timeout. Default unit is milliseconds. If a message is not sent + (due to congestion), the call times out and is aborted. + -timeout : Global timeout. Default unit is seconds. If this option is set, SIPp quits + after nb units (-timeout 20s quits after 20 seconds). + -timeout_error : SIPp fails if the global timeout is reached is set (-timeout option + required). + -max_retrans : Maximum number of UDP retransmissions before call ends on timeout. Default + is 5 for INVITE transactions and 7 for others. + -max_invite_retrans: Maximum number of UDP retransmissions for invite transactions before call + ends on timeout. + -max_non_invite_retrans: Maximum number of UDP retransmissions for non-invite transactions before call + ends on timeout. + -nr : Disable retransmission in UDP mode. + -rtcheck : Select the retransmission detection method: full (default) or loose. + -T2 : Global T2-timer in milli seconds + +*** Third-party call control options: + + -3pcc : Launch the tool in 3pcc mode ("Third Party call control"). The passed IP + address depends on the 3PCC role. + - When the first twin command is 'sendCmd' then this is the address of the + remote twin socket. SIPp will try to connect to this address:port to send + the twin command (This instance must be started after all other 3PCC + scenarios). + Example: 3PCC-C-A scenario. + - When the first twin command is 'recvCmd' then this is the address of the + local twin socket. SIPp will open this address:port to listen for twin + command. + Example: 3PCC-C-B scenario. + -master : 3pcc extended mode: indicates the master number + -slave : 3pcc extended mode: indicates the slave number + -slave_cfg : 3pcc extended mode: indicates the file where the master and slave addresses + are stored + +*** Performance and watchdog options: + + -timer_resol : Set the timer resolution. Default unit is milliseconds. This option has an + impact on timers precision.Small values allow more precise scheduling but + impacts CPU usage.If the compression is on, the value is set to 50ms. The + default value is 10ms. + -max_recv_loops : Set the maximum number of messages received read per cycle. Increase this + value for high traffic level. The default value is 1000. + -max_sched_loops : Set the maximum number of calls run per event loop. Increase this value for + high traffic level. The default value is 1000. + -watchdog_interval: Set gap between watchdog timer firings. Default is 400. + -watchdog_reset : If the watchdog timer has not fired in more than this time period, then reset + the max triggers counters. Default is 10 minutes. + -watchdog_minor_threshold: If it has been longer than this period between watchdog executions count a + minor trip. Default is 500. + -watchdog_major_threshold: If it has been longer than this period between watchdog executions count a + major trip. Default is 3000. + -watchdog_major_maxtriggers: How many times the major watchdog timer can be tripped before the test is + terminated. Default is 10. + -watchdog_minor_maxtriggers: How many times the minor watchdog timer can be tripped before the test is + terminated. Default is 120. + +*** Tracing, logging and statistics options: + + -f : Set the statistics report frequency on screen. Default is 1 and default unit + is seconds. + -trace_stat : Dumps all statistics in _.csv file. Use the '-h stat' + option for a detailed description of the statistics file content. + -stat_delimiter : Set the delimiter for the statistics file + -stf : Set the file name to use to dump statistics + -fd : Set the statistics dump log report frequency. Default is 60 and default unit + is seconds. + -periodic_rtd : Reset response time partition counters each logging interval. + -trace_msg : Displays sent and received SIP messages in __messages.log + -message_file : Set the name of the message log file. + -message_overwrite: Overwrite the message log file (default true). + -trace_shortmsg : Displays sent and received SIP messages as CSV in __shortmessages.log + -shortmessage_file: Set the name of the short message log file. + -shortmessage_overwrite: Overwrite the short message log file (default true). + -trace_counts : Dumps individual message counts in a CSV file. + -trace_err : Trace all unexpected messages in __errors.log. + -error_file : Set the name of the error log file. + -error_overwrite : Overwrite the error log file (default true). + -trace_error_codes: Dumps the SIP response codes of unexpected messages to __error_codes.log. + -trace_calldebug : Dumps debugging information about aborted calls to + __calldebug.log file. + -calldebug_file : Set the name of the call debug file. + -calldebug_overwrite: Overwrite the call debug file (default true). + -trace_screen : Dump statistic screens in the __screens.log file when + quitting SIPp. Useful to get a final status report in background mode (-bg + option). + -screen_file : Set the name of the screen file. + -screen_overwrite: Overwrite the screen file (default true). + -trace_rtt : Allow tracing of all response times in __rtt.csv. + -rtt_freq : freq is mandatory. Dump response times every freq calls in the log file + defined by -trace_rtt. Default value is 200. + -trace_logs : Allow tracing of actions in __logs.log. + -log_file : Set the name of the log actions log file. + -log_overwrite : Overwrite the log actions log file (default true). + -ringbuffer_files: How many error, message, shortmessage and calldebug files should be kept + after rotation? + -ringbuffer_size : How large should error, message, shortmessage and calldebug files be before + they get rotated? + -max_log_size : What is the limit for error, message, shortmessage and calldebug file sizes. + + +Signal handling: + + SIPp can be controlled using POSIX signals. The following signals + are handled: + USR1: Similar to pressing the 'q' key. It triggers a soft exit + of SIPp. No more new calls are placed and all ongoing calls + are finished before SIPp exits. + Example: kill -SIGUSR1 732 + USR2: Triggers a dump of all statistics screens in + __screens.log file. Especially useful + in background mode to know what the current status is. + Example: kill -SIGUSR2 732 + +Exit codes: + + Upon exit (on fatal error or when the number of asked calls (-m + option) is reached, SIPp exits with one of the following exit + code: + 0: All calls were successful + 1: At least one call failed + 97: Exit on internal command. Calls may have been processed + 99: Normal exit without calls processed + 253: RTP validation failure + -1: Fatal error + -2: Fatal error binding a socket +``` + +### About server + +如何启动服务端,开启一个UAS ```bash ./sipp -sf example-server.xml -i host -p port --max_socket 100000 -l 500000 ``` -## About client +![img](../img/blog/Kamailio-SIPp-UAS.png) + +服务端脚本一般如何设计: + +一般根据实际生产情况,存在client-bye, server-bye, server-rejected, client-cancel, server-cancel等多种情况 +以下展示最为常见的主叫挂断:client-bye,即被叫服务侧需要能够接受客服端的bye指令,并回复200ACK + +```xml + + + + + + + + + + + + + + + Content-Type: application/sdp + Content-Length: [len] + + v=0 + o=user1 53655765 2353687637 IN IP[local_ip_type] [local_ip] + s=- + c=IN IP[media_ip_type] [media_ip] + t=0 0 + m=audio [media_port] RTP/AVP 0 + a=rtpmap:0 PCMU/8000 + ]]> + + + + + + + + + + + Content-Length: 0 + ]]> + + + +``` + +1. 收到 INVITE 回200 +2. 收到 ACK +3. 收到 BYE 回200 + +### About client + +如何启动服务端,开启一个UAC + +指定送往的代理地址,指定并发量,指定总量级,指定被叫(含前缀) ```bash ./sipp proxyhost:port -sf example-client.xml -r batchsize -d 10000 -s caller -m totalcount ``` -## About testcase +![img](../img/blog/Kamailio-SIPp-UAC.png) -### successful call bye from client +对于客户端的脚本,通常要与服务端对应,也可以模拟一些异常情况,随机回复到异常的情况,比如呼叫后取消、呼叫后保持、呼叫后挂起一段时间等 -```bash -./sipp -sf server-200-client-bye.xml -i host -p port --max_socket 100000 -l 500000 -./sipp proxyhost:port -sf client-200-bye.xml -r 5 -d 10000 -s 20240715 -m 5 +```xml + + + + ;tag=[call_number] + To: sut + Call-ID: [call_id] + Cseq: 1 INVITE + Contact: + Content-Type: application/sdp + Content-Length: [len] + + v=0 + o=user1 53655765 2353687637 IN IP[local_ip_type] [local_ip] + s=- + t=0 0 + c=IN IP[media_ip_type] [media_ip] + m=audio [media_port] RTP/AVP 0 + a=rtpmap:0 PCMU/8000 + ]]> + + + + + + + + + + + + + ;tag=[call_number] + To: sut [peer_tag_param] + Call-ID: [call_id] + Cseq: 1 ACK + Contact: + Content-Length: 0 + ]]> + + + + ;tag=[call_number] + To: sut [peer_tag_param] + Call-ID: [call_id] + Cseq: 2 BYE + Contact: + Content-Length: 0 + ]]> + + + + + + ``` -### successful call bye from server +1. 发送 INVITE +2. 等待100、180的可选指令 ,200 的必要指令 +3. 收到200后,发送 ACK +4. 而后发出 BYE指令,最终等待200返回 + +### About testcase + +根据以上描述的内容,需要构建好双端的测试脚本,生产情况需要覆盖尽可能多的业务场景,压测情况可以采用正常来回的脚本 + +### successful call bye from client ```bash -./sipp -sf server-200-bye.xml -i host -p port --max_socket 100000 -l 500000 -./sipp proxyhost:port -sf client-200-server-bye.xml -r 5 -d 10000 -s 20240715 -m 5 +./sipp -sf uas.xml -i host -p port --max_socket 100000 -l 500000 +./sipp proxyhost:port -sf uac.xml -r 5 -d 10000 -s 20240715 -m 5 ``` +正常先开启服务端,再启动客户端,根据代理的能力,服务端可以多端口多开 diff --git "a/_posts/2024-08-16-KamailioWorld-06\350\264\237\350\275\275\345\235\207\350\241\241\345\256\236\350\267\265.md" "b/_posts/2024-08-16-KamailioWorld-06\350\264\237\350\275\275\345\235\207\350\241\241\345\256\236\350\267\265.md" index e69de29bb2d..03487c484da 100644 --- "a/_posts/2024-08-16-KamailioWorld-06\350\264\237\350\275\275\345\235\207\350\241\241\345\256\236\350\267\265.md" +++ "b/_posts/2024-08-16-KamailioWorld-06\350\264\237\350\275\275\345\235\207\350\241\241\345\256\236\350\267\265.md" @@ -0,0 +1,513 @@ +--- +layout: post +title: Kamailio-dispatcher负载均衡 + +subtitle: kamailio dispatcher +date: 2024-09-12 +author: Claire +header-img: img/post-bg-github-cup.jpg +catalog: true +tags: + - Kamailio + - sipp +--- + +Kamailio 负载均衡的功能主要依靠 Dispatcher 模块完成,[模块官方文档参看](https://www.kamailio.org/docs/modules/devel/modules/dispatcher.html) + +- [为什么要引入负载均衡?](#为什么要引入负载均衡) +- [如何使用?](#如何使用) + - [引入和配置](#引入和配置) + - [功能路由调用](#功能路由调用) + - [命令行指令](#命令行指令) + +## 为什么要引入负载均衡? + +Q: 如果单台VOIP服务的性能不能满足业务需求了,需要扩展至多个节点,那么Kamailio将如何进行请求的代理呢? + +A: 就是需要负载均衡 + +Q: 如果多个后端VOIP服务有不可用,转过去的请求全部失败,那么Kamailio可以如何保障整体流程的健壮性? + +A: 就是需要负载均衡剔除异常服务节点,转发至可用节点 + +此外,依赖负载均衡的基础功能,还可以实现AB测试、区分权重、通道量分发等业务相关逻辑 + +既然看上去功能强大且十分诱人的功能,要如何才能使用呢? + +![img](../img/blog/Kamailio-Dispatcher.png) + +## 如何使用? + +这个模块是自带的,不需要额外加入并构建,在modules的目录下存在 + +### 引入和配置 + +在 `kamailio.cfg` 中,引入模块,并增加配置项 + +常规的DB方式配置 + +```xml +loadmodule "dispatcher.so" + +# ----- dispatcher params ----- +modparam("dispatcher", "db_url", DBURL) +modparam("dispatcher", "table_name", "dispatcher") +modparam("dispatcher", "flags", 2) +modparam("dispatcher", "xavp_dst", "_dsdst_") +modparam("dispatcher", "xavp_ctx", "_dsctx_") +modparam("dispatcher", "ds_ping_from", "sip:proxy@kamailio.org") +modparam("dispatcher", "ds_ping_interval", 60) +modparam("dispatcher", "ds_probing_mode", 1) +modparam("dispatcher", "ds_timer_mode", 1) +``` + +可以通过DB的方式加载分发的列表,也可以通过最基础的文件的方式引入,都支持动态加载 + +以下是文件方式 + +```xml +loadmodule "dispatcher.so" + +# ----- dispatcher params ----- +modparam("dispatcher", "list_file", "/usr/local/etc/kamailio/dispatcher.list") +modparam("dispatcher", "force_dst", 1) +#剩余参数取决于你的负载均衡模式 +``` + +### 功能路由调用 + +```xml +request_route { + .... + # dispatch destinations + route(DISPATCH); +} + +# Dispatch requests +route[DISPATCH] { + # round robin dispatching on gateways group '1' + if(!ds_select_dst("1", "4")) { + send_reply("404", "No destination"); + exit; + } + xdbg("--- SCRIPT: going to <$ru> via <$du> (attrs: $xavp(_dsdst_=>attrs))\n"); + t_on_failure("RTF_DISPATCH"); + route(RELAY); + exit; +} +``` + +着重需要关注的方法就是 `ds_select_dst(set, alg[, limit])`,即挑选哪一个分组的地址列表,采用什么样的算法进行路由选择 + +第一个参数set,就是地址列表,第二参数alg就是算法,第三个limit是指在AVP列表中最多可以存储的item数 。 + +算法有很多种,也是对负载均衡流程核心的参数,除了12种官方支持的,还可以自定义开发去使用: + +- “0” - hash over callid +- “1” - hash over from URI. +- “2” - hash over to URI. +- “3” - hash over request-URI user. +- “4” - round-robin (next destination). +- -“5” - hash over authorization-username (Proxy-Authorization or "normal" authorization). If no username is found, round robin is used. +- “6” - random destination (using rand()). +- “7” - hash over the content of PVs string. Note: This works only when the parameter hash_pvar is set. +- “8” - select destination sorted by priority attribute value (serial forking ordered by priority). +- “9” - use weight based load distribution. You have to set the attribute 'weight' for each address (gateway) in destination set. See also the description of the 'weight' attribute in the 'Special Attributes' section. +- “10” - use call load distribution. You have to set the attribute 'duid' (as an unique string id) per each address in destination set. Also, you must set the parameter 'ds_hash_size'. +- “11” - use relative weight based load distribution. You have to set the attribute 'rweight' per each address in destination set. Active host usage probability is rweight/(SUM of all active host rweights in destination group). +- “12” - dispatch to all destination in setid at once (parallel forking). Note that the XAVPs are no longer set with the values of the destination records, no re-routing making sense in this case. +- “13” - latency optimized dispatching +- “64” - round-robin (next destination) with over load control (destination record is skipped based on over load control rate). +- “X” - if the algorithm is not implemented, the first entry in set is chosen. + +默认样例都是用round-robin去做负载,初期使用可以用这个方式,后续生产中,应该 9、10、11、13 结合实际业务压力做更好地拥塞控制 + +alg-4 测试示例: + +```yml +... +#!KAMAILIO +# +# sample config file for dispatcher module +# - load balancing of VoIP calls with round robin +# - no TPC listening +# - don't dispatch REGISTER and presence requests +# +# Kamailio SIP Server +# - web: https://www.kamailio.org +# - git: http://github.com/kamailio/ +# +# Direct your questions about this file to: sr-users@lists.kamailio.org +# +# Refer to the Core CookBook at https://www.kamailio.org/w/documentation/ +# for an explanation of possible statements, functions and parameters. +# +# Several features can be enabled using '#!define WITH_FEATURE' directives: +# +# *** To run in debug mode: +# - define WITH_DEBUG +# + +#!ifndef DBURL +#!define DBURL "mysql://kamailio:kamailiorw@localhost/kamailio" +#!endif + +# - flags +# FLT_ - per transaction (message) flags +# FLB_ - per branch flags +#!define FLT_ACC 1 +#!define FLT_ACCMISSED 2 +#!define FLT_ACCFAILED 3 + +####### Global Parameters ######### + +#!ifdef WITH_DEBUG +debug=4 +log_stderror=yes +#!else +debug=2 +log_stderror=no +#!endif + +memdbg=5 +memlog=5 + +log_facility=LOG_LOCAL0 + +fork=yes +children=4 + +/* comment the next line to enable TCP */ +disable_tcp=yes + +/* uncomment the next line to disable the auto discovery of local aliases + based on revers DNS on IPs (default on) */ +auto_aliases=no + +/* add local domain aliases */ +# alias="mysipserver.com" + +port=5060 + +/* uncomment and configure the following line if you want Kamailio to + bind on a specific interface/port/proto (default bind on all available) */ +# listen=udp:127.0.0.1:5060 + +sip_warning=no + +####### Modules Section ######## + +# set module path +#mpath="/usr/local/lib/kamailio/modules/" + +loadmodule "db_mysql.so" +loadmodule "jsonrpcs.so" +loadmodule "kex.so" +loadmodule "corex.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 "acc.so" +loadmodule "dispatcher.so" + + +# ----------------- setting module-specific parameters --------------- + + +# ----- jsonrpcs params ----- +modparam("jsonrpcs", "pretty_format", 1) + + +# ----- rr params ----- +# add value to ;lr param to cope with most of the UAs +modparam("rr", "enable_full_lr", 1) +# do not append from tag to the RR (no need for this script) +modparam("rr", "append_fromtag", 0) + + +# ----- acc params ----- +modparam("acc", "log_flag", FLT_ACC) +modparam("acc", "failed_transaction_flag", FLT_ACCFAILED) +modparam("acc", "log_extra", + "src_user=$fU;src_domain=$fd;dst_ouser=$tU;dst_user=$rU;dst_domain=$rd;src_ip=$si") + +# ----- tm params ----- +modparam("tm", "fr_timer", 2000) +modparam("tm", "fr_inv_timer", 40000) + +# ----- dispatcher params ----- +modparam("dispatcher", "db_url", DBURL) +modparam("dispatcher", "table_name", "dispatcher") +modparam("dispatcher", "flags", 2) +modparam("dispatcher", "xavp_dst", "_dsdst_") +modparam("dispatcher", "xavp_ctx", "_dsctx_") +modparam("dispatcher", "ds_ping_from", "sip:proxy@kamailio.org") +modparam("dispatcher", "ds_ping_interval", 60) +modparam("dispatcher", "ds_probing_mode", 1) +modparam("dispatcher", "ds_timer_mode", 1) + +####### Routing Logic ######## + + +# main request routing logic + +request_route { + + # per request initial checks + route(REQINIT); + + # CANCEL processing + if (is_method("CANCEL")) { + if (t_check_trans()) { + route(RELAY); + } + exit; + } + + # handle retransmissions + if (!is_method("ACK")) { + if(t_precheck_trans()) { + t_check_trans(); + exit; + } + t_check_trans(); + } + + # handle requests within SIP dialogs + route(WITHINDLG); + + ### only initial requests (no To tag) + + # 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(FLT_ACC); # 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[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()) { + return; + } + + # sequential request within a dialog should + # take the path determined by record-routing + if (loose_route()) { + if (is_method("BYE")) { + setflag(FLT_ACC); # do accounting ... + setflag(FLT_ACCFAILED); # ... even if the transaction fails + } + route(RELAY); + } + + if (is_method("SUBSCRIBE") && uri == myself) { + # in-dialog subscribe requests + route(PRESENCE); + } + + 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; +} + +# Handle SIP registrations +route[REGISTRAR] { + if(!is_method("REGISTER")) + return; + + sl_send_reply("404", "No registrar"); + 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; + } + xdbg("--- SCRIPT: going to <$ru> via <$du> (attrs: $xavp(_dsdst_=>attrs))\n"); + t_on_failure("RTF_DISPATCH"); + route(RELAY); + exit; +} + +# Try next destionations in 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()) { + xdbg("--- SCRIPT: retrying to <$ru> via <$du> (attrs: $xavp(_dsdst_=>attrs))\n"); + t_on_failure("RTF_DISPATCH"); + route(RELAY); + exit; + } + } +} + +... +``` + +aalg-11 测试示例: + +```yml +... +# sample of SQL provisioning statements +INSERT INTO "dispatcher" +VALUES(1,1,'sip:192.168.0.1:5060',0,12,'rweight=50;weight=50;cc=1;',''); +INSERT INTO "dispatcher" +VALUES(2,1,'sip:192.168.0.2:5060',0,12,'rweight=50;weight=50;cc=1;',''); +... +modparam("dispatcher", "ds_ping_interval", 1) # ping gateways once/second +modparam("dispatcher", "ds_ping_latency_stats", 1) # update congestion metrics +# configure the latency estimator +modparam("dispatcher", "ds_latency_estimator_alpha", 900) +... +if (!ds_select_dst("1", "11")) { # use relative weight based load distribution +... +# sample of output from 'kamcmd dispatcher.list' +DEST: { + URI: sip:192.168.0.1:5060 + FLAGS: AP + PRIORITY: 12 + ATTRS: { + BODY: rweight=50;weight=50;cc=1 # configuration values + DUID: + MAXLOAD: 0 + WEIGHT: 50 + RWEIGHT: 50 + SOCKET: + SOCKNAME: + OBPROXY: + } + LATENCY: { + AVG: 20.104000 + STD: 1.273000 + # estimated congestion is currently 25ms = 45ms(EST) -20ms(AVG) + EST: 45.005000 + MAX: 132 + TIMEOUT: 3 + } +} +... +``` + +### 命令行指令 + +```bash +# prototype: kamcmd dispatcher.set_state _state_ _group_ _address_ +kamcmd dispatcher.set_state ip 2 sip:127.0.0.1:5080 +kamcmd dispatcher.set_state ip 3 all + +# prototype: kamcmd dispatcher.set_duid_state _state_ _group_ _duid_ +kamcmd dispatcher.set_duid_state ip 2 xyz + +kamcmd dispatcher.list +... +DEST: { + URI: sip:192.168.0.1:5060 + FLAGS: AP + PRIORITY: 12 +} +... + +kamcmd dispatcher.reload + +# prototype: kamcmd dispatcher.ping_active _state_ +kamcmd dispatcher.ping_active 0 + +# prototype: kamcmd dispatcher.add _group_ _address_ _flags_ _priority_ _attrs_ +kamcmd dispatcher.add 2 sip:127.0.0.1:5080 +kamcmd dispatcher.add 3 sip:127.0.0.1:5075 8 +kamcmd dispatcher.add 3 sip:127.0.0.1:5075 0 0 duid=abc;socket=udp:127.0.0.1:5060 +... + +# prototype: kamcmd dispatcher.remove _group_ _address_ +kamcmd dispatcher.remove 2 sip:127.0.0.1:5080 +kamcmd dispatcher.remove 3 sip:127.0.0.1:5075;transport=udp + +# prototype: kamctl rpc dispatcher.hash _nslots_ _val1_ [_val2_] +kamctl rpc dispatcher.hash 0 alice server.com +kamctl rpc dispatcher.hash 4 bob server.com + +# prototype: kamcli dispatcher.oclist _group_ +kamcli dispatcher.oclist 1 +``` + +最为常用的就是增、删、查、热加载,因为通常是新节点加入、老节点剔除、配置热加载这样的需求。 + diff --git a/img/blog/Kamailio-Dispatcher.png b/img/blog/Kamailio-Dispatcher.png new file mode 100644 index 00000000000..018b1ae0dbc Binary files /dev/null and b/img/blog/Kamailio-Dispatcher.png differ diff --git a/img/blog/Kamailio-SIPp-UAC.png b/img/blog/Kamailio-SIPp-UAC.png new file mode 100644 index 00000000000..7230d890a59 Binary files /dev/null and b/img/blog/Kamailio-SIPp-UAC.png differ diff --git a/img/blog/Kamailio-SIPp-UAS.png b/img/blog/Kamailio-SIPp-UAS.png new file mode 100644 index 00000000000..bcf275ffc52 Binary files /dev/null and b/img/blog/Kamailio-SIPp-UAS.png differ