Kamailio 5.8.3与rtpengine双网卡SBC集成要点
本文档总结了将Kamailio 5.8.3与rtpengine(配置为双网卡模式)集成以实现SIP+RTP媒体流转发(包括音视频和RTCP)的关键配置要点和最佳实践。用户场景包括:无NAT、公私网双向呼叫、通过dispatcher模块对公私网两侧的多网关进行负载均衡。
1. Kamailio rtpengine模块核心配置与使用
1.1. 模块加载与参数
loadmodule "rtpengine.so"modparam("rtpengine", "rtpengine_sock", "udp:127.0.0.1:22222") # 与rtpengine的NG协议通信地址和端口
# 根据rtpengine mr13.1.1.6的配置,NG监听在127.0.0.1:22222# 可选参数,根据需要调整:
# modparam("rtpengine", "rtpengine_tout_ms", 1000) # rtpengine请求超时时间 (毫秒)
# modparam("rtpengine", "rtpengine_retr", 5) # rtpengine请求重试次数
# modparam("rtpengine", "hash_table_size", 2048) # 内部哈希表大小
# modparam("rtpengine", "setid_avp", "$avp(rtpengine_setid)") # 如果使用多个rtpengine set
1.2. 核心函数调用
rtpengine_offer([flags])
: 在处理初始INVITE时调用,用于协商媒体。rtpengine_answer([flags])
: 在处理2xx响应时调用,用于确认媒体协商。rtpengine_delete([flags])
: 在处理BYE或会话结束时调用,用于释放rtpengine资源。rtpengine_manage([flags])
: 一个更通用的函数,可以替代rtpengine_offer
和rtpengine_answer
,并提供更多灵活性。通常建议使用rtpengine_manage
。
1.3. 关键Flags(用于rtpengine_manage
等函数)
rtpengine的强大之处在于通过flags精细控制其行为。对于双网卡场景,核心是告知rtpengine媒体流的“方向”或应使用的接口。
- 指定接口/方向:
- rtpengine侧配置了逻辑接口名,例如
interface = public/PUBLIC_IP!PUBLIC_IP
和interface = private/PRIVATE_IP!PRIVATE_IP
。 - Kamailio需要通过flags告诉rtpengine使用哪个逻辑接口。常用的方法是利用
direction=public
、direction=private
这样的自定义标记,并在rtpengine的rtpengine_offer
/rtpengine_answer
/rtpengine_manage
的flags参数中传递。例如:
- rtpengine侧配置了逻辑接口名,例如
- 对于来自公网的呼叫,发往私网的媒体描述,应指示rtpengine使用其“私网”接口:
rtpengine_manage("direction=public direction=private ...")
- 对于来自私网的呼叫,发往公网的媒体描述,应指示rtpengine使用其“公网”接口:
rtpengine_manage("direction=private direction=public ...")
- 对于来自公网的呼叫,发往私网的媒体描述,应指示rtpengine使用其“私网”接口:
- 另一种方式是直接在flags中指定IP地址,例如
internal-ip=PRIVATE_IP external-ip=PUBLIC_IP
,但这通常不如使用逻辑接口名灵活。 - 重要:Kamailio脚本需要有逻辑来判断呼叫的来源和去向,以便设置正确的
direction
flag。
- 另一种方式是直接在flags中指定IP地址,例如
- 媒体类型支持 (音视频):
- rtpengine默认会尝试处理SDP中描述的所有媒体流(音频和视频)。
- 确保SDP中正确描述了音视频媒体行 (
m=audio ...
,m=video ...
)。 - Flags示例:
RTP/AVP
(标准RTP),RTP/SAVP
(SRTP, 如果需要加密)。
- RTCP支持:
- rtpengine会自动处理与RTP流配对的RTCP流。
rtcp-mux-offer
,rtcp-mux-answer
,rtcp-mux-require
: 用于处理RTCP与RTP在同一端口复用的情况 (RFC 5761)。如果终端支持,建议启用以节省端口资源。
- 例如:
rtpengine_manage("rtcp-mux-offer ...")
- 例如:
- NAT处理 (用户场景为无NAT,但相关flags仍需注意):
trust-address
: 信任SDP中的连接地址。replace-origin
: 替换SDP中o=行中的地址。replace-session-connection
: 替换会话级c=行中的地址。- 在无NAT直连IP场景,这些替换标志通常也需要,以确保SDP中的IP地址是rtpengine的接口地址。
- 编解码器协商:
- rtpengine可以进行编解码器过滤和转码(如果配置了转码模块)。
- Flags:
codec-strip=all
,codec-mask=all,!PCMA,!PCMU
,codec-transcode=PCMA
等。 - 用户场景未明确要求转码,但如果需要,rtpengine具备此能力。
- 示例组合Flag (根据呼叫方向调整):
rtpengine_manage("trust-address replace-origin replace-session-connection direction=public direction=private RTP/AVP rtcp-mux-offer")
rtpengine_manage("trust-address replace-origin replace-session-connection direction=private direction=public RTP/AVP rtcp-mux-answer")
2. Kamailio dispatcher模块配置
用户需要在公网和私网两侧都通过dispatcher对多个网关进行负载均衡。
2.1. 定义Dispatcher Set
需要在kamailio.cfg
的全局部分或dispatcher
模块加载时定义网关组。
loadmodule "dispatcher.so"# 定义公网网关组 (set id 1)
modparam("dispatcher", "list_file", "/etc/kamailio/dispatcher_public.list")
# 或者直接在配置文件中定义:
# modparam("dispatcher", "set_uri", "1 sip:gw_public1.example.com:5060")
# modparam("dispatcher", "set_uri", "1 sip:gw_public2.example.com:5060")# 定义私网网关组 (set id 2)
modparam("dispatcher", "list_file", "/etc/kamailio/dispatcher_private.list")
# 或者直接在配置文件中定义:
# modparam("dispatcher", "set_uri", "2 sip:gw_private1.internal:5060")
# modparam("dispatcher", "set_uri", "2 sip:gw_private2.internal:5060")
dispatcher_public.list
文件示例:
1 sip:public_gw1_ip:5060
1 sip:public_gw2_ip:5060 flags=ap # a for active, p for PINGs
dispatcher_private.list
文件示例:
2 sip:private_gw1_ip:5060
2 sip:private_gw2_ip:5060
2.2. 选择网关
在路由逻辑中,使用ds_select_dst()
或ds_select_domain()
选择目标网关。
# 呼叫发往公网
if (is_from_private()) { # 自定义逻辑判断是否来自私网ds_select_dst("1", "4"); # 选择公网网关组 (set 1), 算法4 (round-robin)if ($rc < 0 || $ru == $null) {send_reply("503", "Service Unavailable - No Public Gateway");exit;}# $du 会被设置为选中的网关URIt_set_destination_uri($du);# ... 后续路由和rtpengine_manage调用 (direction=public)
}# 呼叫发往私网
if (is_from_public()) { # 自定义逻辑判断是否来自公网ds_select_dst("2", "4"); # 选择私网网关组 (set 2), 算法4 (round-robin)if ($rc < 0 || $ru == $null) {send_reply("503", "Service Unavailable - No Private Gateway");exit;}# $du 会被设置为选中的网关URIt_set_destination_uri($du);# ... 后续路由和rtpengine_manage调用 (direction=private)
}
3. Kamailio路由逻辑中的双网卡处理
Kamailio需要能够区分流量的来源(公网/私网)和去向,以便:
- 选择正确的dispatcher网关组。
- 为
rtpengine_manage
设置正确的direction
flag。
3.1. 识别接口/网络
- 可以使用
if ($si == "PUBLIC_KAM_IP" && $sp == 5060)
或if (src_ip == "PRIVATE_KAM_IP")
等条件判断SIP包到达的Kamailio接口。 - 或者通过自定义的
is_from_public()
/is_from_private()
函数,基于源IP地址或其他头部信息进行判断。
3.2. 示例路由块结构
request_route {# ... 初始处理 (max_forwards, sanity checks etc.)if (is_from_public()) { # 假设这是来自公网的呼叫xlog("L_INFO", "Call from Public to Private Network\n");# 目标是私网ds_select_dst("2", "4"); # 选择私网网关组if ($rc < 0 || $ru == $null) {send_reply("503", "No Private Gateway Available");exit;}t_set_destination_uri($du);if (has_body("application/sdp")) {rtpengine_manage("trust-address replace-origin replace-session-connection direction=private RTP/AVP rtcp-mux-offer");}route(RELAY_TO_PRIVATE);} else if (is_from_private()) { # 假设这是来自私网的呼叫xlog("L_INFO", "Call from Private to Public Network\n");# 目标是公网ds_select_dst("1", "4"); # 选择公网网关组if ($rc < 0 || $ru == $null) {send_reply("503", "No Public Gateway Available");exit;}t_set_destination_uri($du);if (has_body("application/sdp")) {rtpengine_manage("trust-address replace-origin replace-session-connection direction=public RTP/AVP rtcp-mux-offer");}route(RELAY_TO_PUBLIC);} else {xlog("L_WARN", "Call from unknown source, dropping.\n");send_reply("403", "Forbidden - Unknown Source");exit;}
}route[RELAY_TO_PRIVATE] {# ... 可能的额外处理if (!t_relay()) {sl_reply_error();}exit;
}route[RELAY_TO_PUBLIC] {# ... 可能的额外处理if (!t_relay()) {sl_reply_error();}exit;
}# 对于响应的处理
onreply_route {if (status=~"^[12]") {if (has_body("application/sdp")) {# 需要判断响应的方向来设置正确的direction flag# 这通常基于事务状态或自定义的标志if (is_reply_to_public_originated_call()) { # 伪代码,需要实现逻辑rtpengine_manage("trust-address replace-origin replace-session-connection direction=private direction=public RTP/AVP rtcp-mux-answer");} else if (is_reply_to_private_originated_call()) { # 伪代码rtpengine_manage("trust-address replace-origin replace-session-connection direction=public direction=private RTP/AVP rtcp-mux-answer");}}}# ... 其他响应处理
}branch_route[MANAGE_FAILURE] {xlog("L_ERR", "Failed to manage rtpengine for branch\n");
}failure_route[MANAGE_FAILURE] {xlog("L_ERR", "Failed to manage rtpengine for request\n");
}
4. 其他注意事项
- Kamailio版本:用户指定5.8.3。上述模块和参数在此版本中应可用。建议查阅Kamailio 5.8.x的官方文档确认细节。
- SDP处理:确保
rtpengine_manage
在SDP存在时被调用,并且在请求和响应路径中都被正确处理。 - 错误处理:
rtpengine_manage
调用失败时,应有适当的错误处理逻辑。 - RTCP:rtpengine会自动处理RTCP。
rtcp-mux
flags是推荐的最佳实践。 - 安全性:虽然用户未明确要求,但生产环境中应考虑SIP信令的认证和授权,以及可能的TLS加密。
- 调试:充分利用Kamailio的
xlog
和rtpengine的日志进行调试。
这份总结将作为起草Kamailio配置文件的基础。
空空如常
求真得真