找回密码
 立即注册
首页 业界区 业界 Kamailio 5.8.3与rtpengine双网卡SBC集成要点

Kamailio 5.8.3与rtpengine双网卡SBC集成要点

禄磊 7 小时前
1.jpeg

 
本文档总结了将Kamailio 5.8.3与rtpengine(配置为双网卡模式)集成以实现SIP+RTP媒体流转发(包括音视频和RTCP)的关键配置要点和最佳实践。用户场景包括:无NAT、公私网双向呼叫、通过dispatcher模块对公私网两侧的多网关进行负载均衡。
1. Kamailio rtpengine模块核心配置与使用

1.1. 模块加载与参数
  1. loadmodule "rtpengine.so"
  2. modparam("rtpengine", "rtpengine_sock", "udp:127.0.0.1:22222") # 与rtpengine的NG协议通信地址和端口
  3. # 根据rtpengine mr13.1.1.6的配置,NG监听在127.0.0.1:22222
  4. # 可选参数,根据需要调整:
  5. # modparam("rtpengine", "rtpengine_tout_ms", 1000) # rtpengine请求超时时间 (毫秒)
  6. # modparam("rtpengine", "rtpengine_retr", 5)      # rtpengine请求重试次数
  7. # modparam("rtpengine", "hash_table_size", 2048) # 内部哈希表大小
  8. # 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_manage("direction=public direction=private ...")
    • 对于来自私网的呼叫,发往公网的媒体描述,应指示rtpengine使用其“公网”接口:rtpengine_manage("direction=private direction=public ...")




    • 另一种方式是直接在flags中指定IP地址,例如 internal-ip=PRIVATE_IP external-ip=PUBLIC_IP,但这通常不如使用逻辑接口名灵活。
    • 重要:Kamailio脚本需要有逻辑来判断呼叫的来源和去向,以便设置正确的direction flag。



  • 媒体类型支持 (音视频)



    • 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模块加载时定义网关组。
  1. loadmodule "dispatcher.so"
  2. # 定义公网网关组 (set id 1)
  3. modparam("dispatcher", "list_file", "/etc/kamailio/dispatcher_public.list")
  4. # 或者直接在配置文件中定义:
  5. # modparam("dispatcher", "set_uri", "1 sip:gw_public1.example.com:5060")
  6. # modparam("dispatcher", "set_uri", "1 sip:gw_public2.example.com:5060")
  7. # 定义私网网关组 (set id 2)
  8. modparam("dispatcher", "list_file", "/etc/kamailio/dispatcher_private.list")
  9. # 或者直接在配置文件中定义:
  10. # modparam("dispatcher", "set_uri", "2 sip:gw_private1.internal:5060")
  11. # modparam("dispatcher", "set_uri", "2 sip:gw_private2.internal:5060")
复制代码
dispatcher_public.list 文件示例:
  1. 1 sip:public_gw1_ip:5060
  2. 1 sip:public_gw2_ip:5060 flags=ap # a for active, p for PINGs
复制代码
dispatcher_private.list 文件示例:
  1. 2 sip:private_gw1_ip:5060
  2. 2 sip:private_gw2_ip:5060
复制代码
2.2. 选择网关

在路由逻辑中,使用ds_select_dst()或ds_select_domain()选择目标网关。
  1. # 呼叫发往公网
  2. if (is_from_private()) { # 自定义逻辑判断是否来自私网
  3.     ds_select_dst("1", "4"); # 选择公网网关组 (set 1), 算法4 (round-robin)
  4.     if ($rc < 0 || $ru == $null) {
  5.         send_reply("503", "Service Unavailable - No Public Gateway");
  6.         exit;
  7.     }
  8.     # $du 会被设置为选中的网关URI
  9.     t_set_destination_uri($du);
  10.     # ... 后续路由和rtpengine_manage调用 (direction=public)
  11. }
  12. # 呼叫发往私网
  13. if (is_from_public()) { # 自定义逻辑判断是否来自公网
  14.     ds_select_dst("2", "4"); # 选择私网网关组 (set 2), 算法4 (round-robin)
  15.     if ($rc < 0 || $ru == $null) {
  16.         send_reply("503", "Service Unavailable - No Private Gateway");
  17.         exit;
  18.     }
  19.     # $du 会被设置为选中的网关URI
  20.     t_set_destination_uri($du);
  21.     # ... 后续路由和rtpengine_manage调用 (direction=private)
  22. }
复制代码
3. Kamailio路由逻辑中的双网卡处理

Kamailio需要能够区分流量的来源(公网/私网)和去向,以便:

  • 选择正确的dispatcher网关组。
  • 为rtpengine_manage设置正确的direction flag。
3.1. 识别接口/网络


  • 可以使用if ($si == "UBLIC_KAM_IP" && $sp == 5060) 或 if (src_ip == "RIVATE_KAM_IP") 等条件判断SIP包到达的Kamailio接口。
  • 或者通过自定义的is_from_public() / is_from_private() 函数,基于源IP地址或其他头部信息进行判断。
3.2. 示例路由块结构
  1. request_route {
  2.     # ... 初始处理 (max_forwards, sanity checks etc.)
  3.     if (is_from_public()) { # 假设这是来自公网的呼叫
  4.         xlog("L_INFO", "Call from Public to Private Network\n");
  5.         # 目标是私网
  6.         ds_select_dst("2", "4"); # 选择私网网关组
  7.         if ($rc < 0 || $ru == $null) {
  8.             send_reply("503", "No Private Gateway Available");
  9.             exit;
  10.         }
  11.         t_set_destination_uri($du);
  12.         if (has_body("application/sdp")) {
  13.             rtpengine_manage("trust-address replace-origin replace-session-connection direction=private RTP/AVP rtcp-mux-offer");
  14.         }
  15.         route(RELAY_TO_PRIVATE);
  16.     } else if (is_from_private()) { # 假设这是来自私网的呼叫
  17.         xlog("L_INFO", "Call from Private to Public Network\n");
  18.         # 目标是公网
  19.         ds_select_dst("1", "4"); # 选择公网网关组
  20.         if ($rc < 0 || $ru == $null) {
  21.             send_reply("503", "No Public Gateway Available");
  22.             exit;
  23.         }
  24.         t_set_destination_uri($du);
  25.         if (has_body("application/sdp")) {
  26.             rtpengine_manage("trust-address replace-origin replace-session-connection direction=public RTP/AVP rtcp-mux-offer");
  27.         }
  28.         route(RELAY_TO_PUBLIC);
  29.     } else {
  30.         xlog("L_WARN", "Call from unknown source, dropping.\n");
  31.         send_reply("403", "Forbidden - Unknown Source");
  32.         exit;
  33.     }
  34. }
  35. route[RELAY_TO_PRIVATE] {
  36.     # ... 可能的额外处理
  37.     if (!t_relay()) {
  38.         sl_reply_error();
  39.     }
  40.     exit;
  41. }
  42. route[RELAY_TO_PUBLIC] {
  43.     # ... 可能的额外处理
  44.     if (!t_relay()) {
  45.         sl_reply_error();
  46.     }
  47.     exit;
  48. }
  49. # 对于响应的处理
  50. onreply_route {
  51.     if (status=~"^[12]") {
  52.         if (has_body("application/sdp")) {
  53.             # 需要判断响应的方向来设置正确的direction flag
  54.             # 这通常基于事务状态或自定义的标志
  55.             if (is_reply_to_public_originated_call()) { # 伪代码,需要实现逻辑
  56.                  rtpengine_manage("trust-address replace-origin replace-session-connection direction=private direction=public RTP/AVP rtcp-mux-answer");
  57.             } else if (is_reply_to_private_originated_call()) { # 伪代码
  58.                  rtpengine_manage("trust-address replace-origin replace-session-connection direction=public direction=private RTP/AVP rtcp-mux-answer");
  59.             }
  60.         }
  61.     }
  62.     # ... 其他响应处理
  63. }
  64. branch_route[MANAGE_FAILURE] {
  65.     xlog("L_ERR", "Failed to manage rtpengine for branch\n");
  66. }
  67. failure_route[MANAGE_FAILURE] {
  68.     xlog("L_ERR", "Failed to manage rtpengine for request\n");
  69. }
复制代码
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配置文件的基础。
 
空空如常
求真得真

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册