blob: e890f753ad050563ce3687a31e82d136907d3a90 [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/*
3 * (C) 2019 by sysmocom - s.m.f.c. GmbH <info@sysmocom.de>
4 * 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{
125 int i;
126 cl->mgw_endpoint = NULL;
127 for (i = 0; i < ARRAY_SIZE(cl->rtp); i++) {
128 if (!cl->rtp[i])
129 continue;
130 cl->rtp[i]->ci = NULL;
131 }
132}
133
134static void call_leg_fsm_establishing_established(struct osmo_fsm_inst *fi, uint32_t event, void *data)
135{
136 struct call_leg *cl = fi->priv;
137 struct rtp_stream *rtps;
138 int i;
139 bool established;
140
141 switch (event) {
142
143 case CALL_LEG_EV_RTP_STREAM_ESTABLISHED:
144 /* An rtp_stream says it is established. If all are now established, change to state
145 * CALL_LEG_ST_ESTABLISHED. */
146 established = true;
147 for (i = 0; i < ARRAY_SIZE(cl->rtp); i++) {
148 if (!rtp_stream_is_established(cl->rtp[i])) {
149 established = false;
150 break;
151 }
152 }
153 if (!established)
154 break;
155 call_leg_state_chg(cl, CALL_LEG_ST_ESTABLISHED);
156 break;
157
158 case CALL_LEG_EV_RTP_STREAM_ADDR_AVAILABLE:
159 rtps = data;
160 osmo_fsm_inst_dispatch(fi->proc.parent, cl->parent_event_rtp_addr_available, rtps);
161 break;
162
163 case CALL_LEG_EV_RTP_STREAM_GONE:
164 call_leg_release(cl);
165 break;
166
167 case CALL_LEG_EV_MGW_ENDPOINT_GONE:
168 call_leg_mgw_endpoint_gone(cl);
169 call_leg_release(cl);
170 break;
171
172 default:
173 OSMO_ASSERT(false);
174 }
175}
176
177void call_leg_fsm_established_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
178{
179 struct call_leg *cl = fi->priv;
180 osmo_fsm_inst_dispatch(fi->proc.parent, cl->parent_event_rtp_complete, cl);
181}
182
183void call_leg_fsm_releasing_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
184{
185 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
186}
187
188static void call_leg_fsm_releasing(struct osmo_fsm_inst *fi, uint32_t event, void *data)
189{
190 struct call_leg *cl = fi->priv;
191
192 switch (event) {
193
194 case CALL_LEG_EV_RTP_STREAM_GONE:
195 /* We're already terminating, child RTP streams will also terminate, there is nothing left to do. */
196 break;
197
198 case CALL_LEG_EV_MGW_ENDPOINT_GONE:
199 call_leg_mgw_endpoint_gone(cl);
200 break;
201
202 default:
203 OSMO_ASSERT(false);
204 }
205}
206
207static const struct value_string call_leg_fsm_event_names[] = {
208 OSMO_VALUE_STRING(CALL_LEG_EV_RTP_STREAM_ADDR_AVAILABLE),
209 OSMO_VALUE_STRING(CALL_LEG_EV_RTP_STREAM_ESTABLISHED),
210 OSMO_VALUE_STRING(CALL_LEG_EV_RTP_STREAM_GONE),
211 OSMO_VALUE_STRING(CALL_LEG_EV_MGW_ENDPOINT_GONE),
212 {}
213};
214
215#define S(x) (1 << (x))
216
217static const struct osmo_fsm_state call_leg_fsm_states[] = {
218 [CALL_LEG_ST_ESTABLISHING] = {
219 .name = "ESTABLISHING",
220 .in_event_mask = 0
221 | S(CALL_LEG_EV_RTP_STREAM_ADDR_AVAILABLE)
222 | S(CALL_LEG_EV_RTP_STREAM_ESTABLISHED)
223 | S(CALL_LEG_EV_RTP_STREAM_GONE)
224 | S(CALL_LEG_EV_MGW_ENDPOINT_GONE)
225 ,
226 .out_state_mask = 0
227 | S(CALL_LEG_ST_ESTABLISHED)
228 | S(CALL_LEG_ST_RELEASING)
229 ,
230 .action = call_leg_fsm_establishing_established,
231 },
232 [CALL_LEG_ST_ESTABLISHED] = {
233 .name = "ESTABLISHED",
234 .in_event_mask = 0
235 | S(CALL_LEG_EV_RTP_STREAM_ADDR_AVAILABLE)
236 | S(CALL_LEG_EV_RTP_STREAM_ESTABLISHED)
237 | S(CALL_LEG_EV_RTP_STREAM_GONE)
238 | S(CALL_LEG_EV_MGW_ENDPOINT_GONE)
239 ,
240 .out_state_mask = 0
241 | S(CALL_LEG_ST_ESTABLISHING)
242 | S(CALL_LEG_ST_RELEASING)
243 ,
244 .onenter = call_leg_fsm_established_onenter,
245 .action = call_leg_fsm_establishing_established, /* same action function as above */
246 },
247 [CALL_LEG_ST_RELEASING] = {
248 .name = "RELEASING",
249 .in_event_mask = 0
250 | S(CALL_LEG_EV_RTP_STREAM_GONE)
251 | S(CALL_LEG_EV_MGW_ENDPOINT_GONE)
252 ,
253 .onenter = call_leg_fsm_releasing_onenter,
254 .action = call_leg_fsm_releasing,
255 },
256};
257
258static struct osmo_fsm call_leg_fsm = {
259 .name = "call_leg",
260 .states = call_leg_fsm_states,
261 .num_states = ARRAY_SIZE(call_leg_fsm_states),
262 .log_subsys = DCC,
263 .event_names = call_leg_fsm_event_names,
264 .timer_cb = call_leg_fsm_timer_cb,
265};
266
267const struct value_string rtp_direction_names[] = {
268 OSMO_VALUE_STRING(RTP_TO_RAN),
269 OSMO_VALUE_STRING(RTP_TO_CN),
270 {}
271};
272
273int call_leg_ensure_rtp_alloc(struct call_leg *cl, enum rtp_direction dir, uint32_t call_id, struct gsm_trans *for_trans)
274{
275 if (cl->rtp[dir])
276 return 0;
277
278 if (!cl->mgw_endpoint)
279 cl->mgw_endpoint = osmo_mgcpc_ep_alloc(cl->fi, CALL_LEG_EV_MGW_ENDPOINT_GONE,
280 gsmnet->mgw.client, gsmnet->mgw.tdefs, cl->fi->id,
281 "%s", mgcp_client_rtpbridge_wildcard(gsmnet->mgw.client));
282 if (!cl->mgw_endpoint) {
283 LOG_CALL_LEG(cl, LOGL_ERROR, "failed to setup MGW endpoint\n");
284 return -EIO;
285 }
286
287 cl->rtp[dir] = rtp_stream_alloc(cl, dir, call_id, for_trans);
288 OSMO_ASSERT(cl->rtp[dir]);
289 return 0;
290}
291
292struct osmo_sockaddr_str *call_leg_local_ip(struct call_leg *cl, enum rtp_direction dir)
293{
294 struct rtp_stream *rtps;
295 if (!cl)
296 return NULL;
297 rtps = cl->rtp[dir];
298 if (!rtps)
299 return NULL;
Neels Hofmeyr84ce2062019-10-05 05:15:25 +0200300 if (!osmo_sockaddr_str_is_nonzero(&rtps->local))
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100301 return NULL;
302 return &rtps->local;
303}
304
305/* Make sure an MGW endpoint CI is set up for an RTP connection.
306 * This is the one-stop for all to either completely set up a new endpoint connection, or to modify an existing one.
307 * If not yet present, allocate the rtp_stream for the given direction.
308 * Then, call rtp_stream_set_codec() if codec_if_known is non-NULL, and/or rtp_stream_set_remote_addr() if
309 * remote_addr_if_known is non-NULL.
310 * Finally make sure that a CRCX is sent out for this direction, if this has not already happened.
311 * If the CRCX has already happened but new codec / remote_addr data was passed, call rtp_stream_commit() to trigger an
312 * MDCX.
313 */
314int call_leg_ensure_ci(struct call_leg *cl, enum rtp_direction dir, uint32_t call_id, struct gsm_trans *for_trans,
315 const enum mgcp_codecs *codec_if_known, const struct osmo_sockaddr_str *remote_addr_if_known)
316{
317 if (call_leg_ensure_rtp_alloc(cl, dir, call_id, for_trans))
318 return -EIO;
319 cl->rtp[dir]->crcx_conn_mode = cl->crcx_conn_mode[dir];
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200320 if (dir == RTP_TO_RAN && cl->ran_peer_supports_osmux) {
321 cl->rtp[dir]->use_osmux = true;
322 cl->rtp[dir]->remote_osmux_cid = -1; /* wildcard */
323 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100324 if (codec_if_known)
325 rtp_stream_set_codec(cl->rtp[dir], *codec_if_known);
Neels Hofmeyr84ce2062019-10-05 05:15:25 +0200326 if (remote_addr_if_known && osmo_sockaddr_str_is_nonzero(remote_addr_if_known))
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100327 rtp_stream_set_remote_addr(cl->rtp[dir], remote_addr_if_known);
328 return rtp_stream_ensure_ci(cl->rtp[dir], cl->mgw_endpoint);
329}
330
331int call_leg_local_bridge(struct call_leg *cl1, uint32_t call_id1, struct gsm_trans *trans1,
332 struct call_leg *cl2, uint32_t call_id2, struct gsm_trans *trans2)
333{
334 enum mgcp_codecs codec;
335
336 cl1->local_bridge = cl2;
337 cl2->local_bridge = cl1;
338
339 /* 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
340 * also means that if both legs use different codecs the MGW must perform transcoding on the second leg. */
341 if (!cl1->rtp[RTP_TO_RAN] || !cl1->rtp[RTP_TO_RAN]->codec_known) {
342 LOG_CALL_LEG(cl1, LOGL_ERROR, "RAN-side RTP stream codec is not known, not ready for bridging\n");
343 return -EINVAL;
344 }
345 codec = cl1->rtp[RTP_TO_RAN]->codec;
346
Pau Espin Pedroldd262262022-01-13 14:06:44 +0100347 if (!cl1->rtp[RTP_TO_CN] || !cl2->rtp[RTP_TO_CN])
348 return -ENOTCONN;
349
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100350 call_leg_ensure_ci(cl1, RTP_TO_CN, call_id1, trans1,
351 &codec, &cl2->rtp[RTP_TO_CN]->local);
352 call_leg_ensure_ci(cl2, RTP_TO_CN, call_id2, trans2,
353 &codec, &cl1->rtp[RTP_TO_CN]->local);
354 return 0;
355}