What is Kamailio
Kamailio is basically a SIP Router. The full form of SIP is Session Initiation Protocol. This is the protocol used for session establishment between two parties in the Call. Once the season is established between Caller and Callee the Voice traffic can be transmitted to each other. Here, I assume the reader has a basic understanding of SIP Call flow. Below is the image to understand the basic SIP call flow.
Above, Alice and Bob are two parties. Alice initiates a call via INVITE message and Bob answers the INVITE message and sends 200 OK. Then Alice sends confirmation message ACK. After this ACK message the real Voice call traffic can transmit between two users. You can see RTP which stands for Real Time Protocol flowing bidirectional. At the end any one party can send a BYE request to terminate the session and to end the call. The numbers mentioned in the above image ate the transactions.
1. From request INVITE to 200 OK is one transaction
2. ACK is the transaction
3. From request BYE to 200 OK is the transaction
The whole conversation is called Dialog which is shown in the image above. The second transaction of ACK is important which must happen. If this ACK message does not reach properly then it will retry for around 30 seconds and then premature call will be auto hangup after that.
We can assign many Roles to the Kamailio server as per our need and use cases like SIP Registrar Server, Load Balancer etc.
With Kamailio we can perform load balancing of Incoming SIP Call traffic. Suppose we have multiple Asterisk server nodes and we want to distribute call traffic to all available media servers. In this case we can use the Kamailio server and can configure the Dispatcher Module for it. This way we can serve Incoming Call Requests via available healthy nodes only and have load distributed among all servers equally, so no single node is getting overloaded with calls.
Likewise, we can use Kamailio as a SIP User Registration server, so all SIP Registration traffic can be handled by Kamailio server and Asterisk nodes can have a reduced amount of load on it.
So, Kamailio is used for SIP call traffic management. It can modify the SIP message and route the message to any other SIP server. It can set up thousands of calls per second. So, for session establishment it can be used and then media traffic can be handled via Asterisk and Freeswitch like PBX software. Remember the Kamailio is not a PBX. Means IVR and Voicemail like service we can not run on it. For the media service we can use Asterisk or Freeswitch like software which has a very healthy amount of applications for voice.
Why Kamailio
Kamailio is a very fast and powerful server. It has a very good call setup capacity with a very minimal amount of hardware. The reason behind using Kamailio is to enhance the capacity of PBX software. The single Asterisk server (PBX software) can not handle large volumes of calls. So, here to enhance and scale the capacity of media servers we need to use the Kamailio server.
Suppose, we have one Kamailio server in front of the Farm of Asterisk servers. Whenever we feel Call traffic is getting increased we just need to spin up a new instance of the Asterisk media server to handle more calls. Also we can terminate the instance if call traffic is getting decreased to normal condition. This way the infrastructure can be fully scalable. We can stretch it as per our need.
Likewise, if we take the User Registration process on Kamailio, it will greatly reduce load on the Asterisk server. Assume like 500 users are getting registered at a time, in that case Kamailio can handle it gracefully.
Use cases
The most useful use cases are:
a) PJSIP web phone Registration/Authentication on Kamailio
Here, we can use Asterisk’s PJSIP Real time users available in the database table. These users will be Authenticated via Kamailio. Once Authenticated we can save its location to the Kamailio database.
b) Forwarding Registration details to Asterisk server
Here, the registration requests which are authenticated by Kamailio server can be forwarded to Asterisk server. So Asterisk server can know the location of endpoint and can know where to ring phone during Dial (PJSIP/101) into dialplan, as we know now we are not directly registering users on Asterisk server to reduce load on it.
c) Inbound Call Distribution to Asterisk servers
Here, we can use Kamailio’s Dispatcher module to distribute calls among available Asterisk nodes.
d) Using Real time Queue Application in sharing with multiple Asterisk nodes
Here, we can create one Queue in the Real time database and will use it among all Asterisk servers. Below is the example I gave for what we can Achieve.
E.g.
Suppose Asterisk’s Queue1 has two members added 101 and 102
Incoming Call1 => Kamailio => Asterisk1 => Queue1 => Rings 101
Incoming Call2 => Kamailio => Asterisk2 => Queue1 => Rings 102
Here you can see Queue1 is common in both Calls on different Asterisk servers.
How Kamailio is working
In Kamailio, the file located at location /etc/kamailio/kamailio.cfg is the main configuration file. There are some variables like Local, Global and Custom which can be declared inside it. There are three main activities happening in this configuration file. They are as follows:
a. Load Module
b. Set Module Parameters
c. Use Module Functions to achieve desired task
Here, Call Routing logic exactly starts at request_route in the configuration file. Any incoming SIP request coming to the Kamailio server, starts processing from here only. Here you can write your custom logic and code to process SIP requests as per your need.
Benefits of Kamailio
- Managed by single configurations file kamailio.cfg
- Number of useful modules available to satisfy all SIP routing needs
- Can read SIP header and modify it if needed
- Helps in scaling the infrastructure
- Good Call setup and distribution capacity
- Good SIP user registration capacity
- Can be integrated with Asterisk/Freeswitch like PBX to enhance media services
I would like to share a complete book for achieving above use cases which is available on the Amazon at link below: