blob: b078c770c104593243b435542cde7a5f6e97ac68 [file] [log] [blame]
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001/* Implementation to manage two RTP streams that make up an MO or MT call leg's RTP forwarding. */
2/*
Vadim Yanitskiy999a5932023-05-18 17:22:26 +07003 * (C) 2019 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01004 * All Rights Reserved
5 *
6 * Author: Neels Hofmeyr
7 *
8 * SPDX-License-Identifier: GPL-2.0+
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010019 */
20#include <osmocom/core/fsm.h>
21#include <osmocom/mgcp_client/mgcp_client_endpoint_fsm.h>
22
23#include <osmocom/msc/debug.h>
24#include <osmocom/msc/gsm_data.h>
25#include <osmocom/msc/msc_a.h>
26
27#include <osmocom/msc/call_leg.h>
28#include <osmocom/msc/rtp_stream.h>
29
30#define LOG_CALL_LEG(cl, level, fmt, args...) \
31 LOGPFSML(cl ? cl->fi : NULL, level, fmt, ##args)
32
33static struct gsm_network *gsmnet = NULL;
34
35enum call_leg_state {
36 CALL_LEG_ST_ESTABLISHING,
37 CALL_LEG_ST_ESTABLISHED,
38 CALL_LEG_ST_RELEASING,
39};
40
41struct osmo_tdef g_mgw_tdefs[] = {
Neels Hofmeyrd4099c32020-09-16 01:59:29 +020042 { .T=-2427, .default_val=4, .desc="MGCP response timeout" },
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010043 { .T=-2, .default_val=30, .desc="RTP stream establishing timeout" },
44 {}
45};
46
47static const struct osmo_tdef_state_timeout call_leg_fsm_timeouts[32] = {
48 [CALL_LEG_ST_ESTABLISHING] = { .T = -2 },
49 [CALL_LEG_ST_RELEASING] = { .T = -2 },
50};
51
52#define call_leg_state_chg(cl, state) \
53 osmo_tdef_fsm_inst_state_chg((cl)->fi, state, call_leg_fsm_timeouts, g_mgw_tdefs, 10)
54
55static struct osmo_fsm call_leg_fsm;
56
57void call_leg_init(struct gsm_network *net)
58{
59 gsmnet = net;
60 OSMO_ASSERT( osmo_fsm_register(&call_leg_fsm) == 0 );
61}
62
Neels Hofmeyrf50d1302019-05-09 16:23:11 +020063/* Allocate a call leg FSM instance as child of an arbitrary other FSM instance.
64 * The call leg FSM dispatches events to its parent FSM instance on specific events:
65 * - parent_event_term: dispatch this to the parent FI when the call leg terminates (call ended, either planned or by
66 * failure).
67 * - parent_event_rtp_addr_available: one of the rtp_stream instances managed by the call leg has received an RTP
68 * address from the MGW. The struct rtp_stream instance is passed as data argument for the event dispatch.
69 * - parent_event_rtp_complete: one of the rtp_stream instances entered the RTP_STREAM_ST_ESTABLISHED state.
70 */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010071struct call_leg *call_leg_alloc(struct osmo_fsm_inst *parent_fi,
72 uint32_t parent_event_term,
73 uint32_t parent_event_rtp_addr_available,
Neels Hofmeyr265a4c72019-05-09 16:20:51 +020074 uint32_t parent_event_rtp_complete)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010075{
76 struct call_leg *cl;
77 struct osmo_fsm_inst *fi = osmo_fsm_inst_alloc_child(&call_leg_fsm, parent_fi, parent_event_term);
78
79 OSMO_ASSERT(fi);
80
81 cl = talloc(fi, struct call_leg);
82 OSMO_ASSERT(cl);
83 fi->priv = cl;
84 *cl = (struct call_leg){
85 .fi = fi,
86 .parent_event_rtp_addr_available = parent_event_rtp_addr_available,
87 .parent_event_rtp_complete = parent_event_rtp_complete,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010088 };
89
90 return cl;
91}
92
93void call_leg_reparent(struct call_leg *cl,
94 struct osmo_fsm_inst *new_parent_fi,
95 uint32_t parent_event_term,
96 uint32_t parent_event_rtp_addr_available,
Neels Hofmeyr265a4c72019-05-09 16:20:51 +020097 uint32_t parent_event_rtp_complete)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010098{
99 LOG_CALL_LEG(cl, LOGL_DEBUG, "Reparenting from parent %s to parent %s\n",
100 cl->fi->proc.parent->name, new_parent_fi->name);
101 osmo_fsm_inst_change_parent(cl->fi, new_parent_fi, parent_event_term);
102 talloc_steal(new_parent_fi, cl->fi);
103 cl->parent_event_rtp_addr_available = parent_event_rtp_addr_available;
104 cl->parent_event_rtp_complete = parent_event_rtp_complete;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100105}
106
107static int call_leg_fsm_timer_cb(struct osmo_fsm_inst *fi)
108{
109 struct call_leg *cl = fi->priv;
110 call_leg_release(cl);
111 return 0;
112}
113
114void call_leg_release(struct call_leg *cl)
115{
116 if (!cl)
117 return;
118 if (cl->fi->state == CALL_LEG_ST_RELEASING)
119 return;
120 call_leg_state_chg(cl, CALL_LEG_ST_RELEASING);
121}
122
123static void call_leg_mgw_endpoint_gone(struct call_leg *cl)
124{
Pau Espin Pedrolb44cf2d2022-10-17 18:09:15 +0200125 struct mgcp_client *mgcp_client;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100126 int i;
Pau Espin Pedrolb44cf2d2022-10-17 18:09:15 +0200127
128 /* Put MGCP client back into MGW pool */
129 mgcp_client = osmo_mgcpc_ep_client(cl->mgw_endpoint);
130 mgcp_client_pool_put(mgcp_client);
131
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100132 cl->mgw_endpoint = NULL;
133 for (i = 0; i < ARRAY_SIZE(cl->rtp); i++) {
134 if (!cl->rtp[i])
135 continue;
136 cl->rtp[i]->ci = NULL;
137 }
138}
139
140static void call_leg_fsm_establishing_established(struct osmo_fsm_inst *fi, uint32_t event, void *data)
141{
142 struct call_leg *cl = fi->priv;
143 struct rtp_stream *rtps;
144 int i;
145 bool established;
146
147 switch (event) {
148
149 case CALL_LEG_EV_RTP_STREAM_ESTABLISHED:
150 /* An rtp_stream says it is established. If all are now established, change to state
151 * CALL_LEG_ST_ESTABLISHED. */
152 established = true;
153 for (i = 0; i < ARRAY_SIZE(cl->rtp); i++) {
154 if (!rtp_stream_is_established(cl->rtp[i])) {
155 established = false;
156 break;
157 }
158 }
159 if (!established)
160 break;
161 call_leg_state_chg(cl, CALL_LEG_ST_ESTABLISHED);
162 break;
163
164 case CALL_LEG_EV_RTP_STREAM_ADDR_AVAILABLE:
165 rtps = data;
166 osmo_fsm_inst_dispatch(fi->proc.parent, cl->parent_event_rtp_addr_available, rtps);
167 break;
168
169 case CALL_LEG_EV_RTP_STREAM_GONE:
170 call_leg_release(cl);
171 break;
172
173 case CALL_LEG_EV_MGW_ENDPOINT_GONE:
174 call_leg_mgw_endpoint_gone(cl);
175 call_leg_release(cl);
176 break;
177
178 default:
179 OSMO_ASSERT(false);
180 }
181}
182
183void call_leg_fsm_established_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
184{
185 struct call_leg *cl = fi->priv;
186 osmo_fsm_inst_dispatch(fi->proc.parent, cl->parent_event_rtp_complete, cl);
187}
188
189void call_leg_fsm_releasing_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
190{
Pau Espin Pedrol093fd2e2022-10-19 16:56:43 +0200191 /* Trigger termination of children FSMs (rtp_stream(s)) before
192 * terminating ourselves, otherwise we are not able to receive
193 * CALL_LEG_EV_MGW_ENDPOINT_GONE from cl->mgw_endpoint (call_leg =>
194 * rtp_stream => mgw_endpoint), because osmo_fsm disabled dispatching
195 * events to an FSM in process of terminating. */
196 osmo_fsm_inst_term_children(fi, OSMO_FSM_TERM_PARENT, NULL);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100197 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
198}
199
200static void call_leg_fsm_releasing(struct osmo_fsm_inst *fi, uint32_t event, void *data)
201{
202 struct call_leg *cl = fi->priv;
203
204 switch (event) {
205
206 case CALL_LEG_EV_RTP_STREAM_GONE:
207 /* We're already terminating, child RTP streams will also terminate, there is nothing left to do. */
208 break;
209
210 case CALL_LEG_EV_MGW_ENDPOINT_GONE:
211 call_leg_mgw_endpoint_gone(cl);
212 break;
213
214 default:
215 OSMO_ASSERT(false);
216 }
217}
218
219static const struct value_string call_leg_fsm_event_names[] = {
220 OSMO_VALUE_STRING(CALL_LEG_EV_RTP_STREAM_ADDR_AVAILABLE),
221 OSMO_VALUE_STRING(CALL_LEG_EV_RTP_STREAM_ESTABLISHED),
222 OSMO_VALUE_STRING(CALL_LEG_EV_RTP_STREAM_GONE),
223 OSMO_VALUE_STRING(CALL_LEG_EV_MGW_ENDPOINT_GONE),
224 {}
225};
226
227#define S(x) (1 << (x))
228
229static const struct osmo_fsm_state call_leg_fsm_states[] = {
230 [CALL_LEG_ST_ESTABLISHING] = {
231 .name = "ESTABLISHING",
232 .in_event_mask = 0
233 | S(CALL_LEG_EV_RTP_STREAM_ADDR_AVAILABLE)
234 | S(CALL_LEG_EV_RTP_STREAM_ESTABLISHED)
235 | S(CALL_LEG_EV_RTP_STREAM_GONE)
236 | S(CALL_LEG_EV_MGW_ENDPOINT_GONE)
237 ,
238 .out_state_mask = 0
239 | S(CALL_LEG_ST_ESTABLISHED)
240 | S(CALL_LEG_ST_RELEASING)
241 ,
242 .action = call_leg_fsm_establishing_established,
243 },
244 [CALL_LEG_ST_ESTABLISHED] = {
245 .name = "ESTABLISHED",
246 .in_event_mask = 0
247 | S(CALL_LEG_EV_RTP_STREAM_ADDR_AVAILABLE)
248 | S(CALL_LEG_EV_RTP_STREAM_ESTABLISHED)
249 | S(CALL_LEG_EV_RTP_STREAM_GONE)
250 | S(CALL_LEG_EV_MGW_ENDPOINT_GONE)
251 ,
252 .out_state_mask = 0
253 | S(CALL_LEG_ST_ESTABLISHING)
254 | S(CALL_LEG_ST_RELEASING)
255 ,
256 .onenter = call_leg_fsm_established_onenter,
257 .action = call_leg_fsm_establishing_established, /* same action function as above */
258 },
259 [CALL_LEG_ST_RELEASING] = {
260 .name = "RELEASING",
261 .in_event_mask = 0
262 | S(CALL_LEG_EV_RTP_STREAM_GONE)
263 | S(CALL_LEG_EV_MGW_ENDPOINT_GONE)
264 ,
265 .onenter = call_leg_fsm_releasing_onenter,
266 .action = call_leg_fsm_releasing,
267 },
268};
269
270static struct osmo_fsm call_leg_fsm = {
271 .name = "call_leg",
272 .states = call_leg_fsm_states,
273 .num_states = ARRAY_SIZE(call_leg_fsm_states),
274 .log_subsys = DCC,
275 .event_names = call_leg_fsm_event_names,
276 .timer_cb = call_leg_fsm_timer_cb,
277};
278
279const struct value_string rtp_direction_names[] = {
280 OSMO_VALUE_STRING(RTP_TO_RAN),
281 OSMO_VALUE_STRING(RTP_TO_CN),
282 {}
283};
284
285int call_leg_ensure_rtp_alloc(struct call_leg *cl, enum rtp_direction dir, uint32_t call_id, struct gsm_trans *for_trans)
286{
287 if (cl->rtp[dir])
288 return 0;
289
Pau Espin Pedrolb44cf2d2022-10-17 18:09:15 +0200290 if (!cl->mgw_endpoint) {
291 struct mgcp_client *mgcp_client = mgcp_client_pool_get(gsmnet->mgw.mgw_pool);
292 if (!mgcp_client) {
293 LOG_CALL_LEG(cl, LOGL_ERROR,
294 "cannot ensure MGW endpoint -- no MGW configured, check configuration!\n");
295 return -ENODEV;
296 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100297 cl->mgw_endpoint = osmo_mgcpc_ep_alloc(cl->fi, CALL_LEG_EV_MGW_ENDPOINT_GONE,
Pau Espin Pedrolb44cf2d2022-10-17 18:09:15 +0200298 mgcp_client, gsmnet->mgw.tdefs, cl->fi->id,
299 "%s", mgcp_client_rtpbridge_wildcard(mgcp_client));
300 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100301 if (!cl->mgw_endpoint) {
302 LOG_CALL_LEG(cl, LOGL_ERROR, "failed to setup MGW endpoint\n");
303 return -EIO;
304 }
305
306 cl->rtp[dir] = rtp_stream_alloc(cl, dir, call_id, for_trans);
307 OSMO_ASSERT(cl->rtp[dir]);
308 return 0;
309}
310
311struct osmo_sockaddr_str *call_leg_local_ip(struct call_leg *cl, enum rtp_direction dir)
312{
313 struct rtp_stream *rtps;
314 if (!cl)
315 return NULL;
316 rtps = cl->rtp[dir];
317 if (!rtps)
318 return NULL;
Neels Hofmeyr84ce2062019-10-05 05:15:25 +0200319 if (!osmo_sockaddr_str_is_nonzero(&rtps->local))
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100320 return NULL;
321 return &rtps->local;
322}
323
324/* Make sure an MGW endpoint CI is set up for an RTP connection.
325 * This is the one-stop for all to either completely set up a new endpoint connection, or to modify an existing one.
326 * If not yet present, allocate the rtp_stream for the given direction.
Neels Hofmeyr62bfa372022-10-31 18:51:07 +0100327 * Then, call rtp_stream_set_codecs() if codecs_if_known is non-NULL, and/or rtp_stream_set_remote_addr() if
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100328 * remote_addr_if_known is non-NULL.
329 * Finally make sure that a CRCX is sent out for this direction, if this has not already happened.
330 * If the CRCX has already happened but new codec / remote_addr data was passed, call rtp_stream_commit() to trigger an
331 * MDCX.
332 */
333int call_leg_ensure_ci(struct call_leg *cl, enum rtp_direction dir, uint32_t call_id, struct gsm_trans *for_trans,
Neels Hofmeyr62bfa372022-10-31 18:51:07 +0100334 const struct sdp_audio_codecs *codecs_if_known,
335 const struct osmo_sockaddr_str *remote_addr_if_known)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100336{
337 if (call_leg_ensure_rtp_alloc(cl, dir, call_id, for_trans))
338 return -EIO;
Andreas Eversberg58fe2e02023-06-21 12:37:18 +0200339 rtp_stream_set_mode(cl->rtp[dir], cl->crcx_conn_mode[dir]);
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200340 if (dir == RTP_TO_RAN && cl->ran_peer_supports_osmux) {
341 cl->rtp[dir]->use_osmux = true;
342 cl->rtp[dir]->remote_osmux_cid = -1; /* wildcard */
343 }
Neels Hofmeyr62bfa372022-10-31 18:51:07 +0100344 if (codecs_if_known)
345 rtp_stream_set_codecs(cl->rtp[dir], codecs_if_known);
Neels Hofmeyr84ce2062019-10-05 05:15:25 +0200346 if (remote_addr_if_known && osmo_sockaddr_str_is_nonzero(remote_addr_if_known))
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100347 rtp_stream_set_remote_addr(cl->rtp[dir], remote_addr_if_known);
348 return rtp_stream_ensure_ci(cl->rtp[dir], cl->mgw_endpoint);
349}
350
351int call_leg_local_bridge(struct call_leg *cl1, uint32_t call_id1, struct gsm_trans *trans1,
352 struct call_leg *cl2, uint32_t call_id2, struct gsm_trans *trans2)
353{
Neels Hofmeyr62bfa372022-10-31 18:51:07 +0100354 struct sdp_audio_codecs *codecs;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100355
356 cl1->local_bridge = cl2;
357 cl2->local_bridge = cl1;
358
359 /* We may just copy the codec info we have for the RAN side of the first leg to the CN side of both legs. This
360 * also means that if both legs use different codecs the MGW must perform transcoding on the second leg. */
Neels Hofmeyr62bfa372022-10-31 18:51:07 +0100361 if (!cl1->rtp[RTP_TO_RAN] || !cl1->rtp[RTP_TO_RAN]->codecs_known) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100362 LOG_CALL_LEG(cl1, LOGL_ERROR, "RAN-side RTP stream codec is not known, not ready for bridging\n");
363 return -EINVAL;
364 }
Neels Hofmeyr62bfa372022-10-31 18:51:07 +0100365 codecs = &cl1->rtp[RTP_TO_RAN]->codecs;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100366
Pau Espin Pedroldd262262022-01-13 14:06:44 +0100367 if (!cl1->rtp[RTP_TO_CN] || !cl2->rtp[RTP_TO_CN])
368 return -ENOTCONN;
369
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100370 call_leg_ensure_ci(cl1, RTP_TO_CN, call_id1, trans1,
Neels Hofmeyr62bfa372022-10-31 18:51:07 +0100371 codecs, &cl2->rtp[RTP_TO_CN]->local);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100372 call_leg_ensure_ci(cl2, RTP_TO_CN, call_id2, trans2,
Neels Hofmeyr62bfa372022-10-31 18:51:07 +0100373 codecs, &cl1->rtp[RTP_TO_CN]->local);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100374 return 0;
375}