blob: cb7e6ea8009750820931670a891bb089d49efa82 [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.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 */
24#include <osmocom/core/fsm.h>
25#include <osmocom/mgcp_client/mgcp_client_endpoint_fsm.h>
26
27#include <osmocom/msc/debug.h>
28#include <osmocom/msc/gsm_data.h>
29#include <osmocom/msc/msc_a.h>
30
31#include <osmocom/msc/call_leg.h>
32#include <osmocom/msc/rtp_stream.h>
33
34#define LOG_CALL_LEG(cl, level, fmt, args...) \
35 LOGPFSML(cl ? cl->fi : NULL, level, fmt, ##args)
36
37static struct gsm_network *gsmnet = NULL;
38
39enum call_leg_state {
40 CALL_LEG_ST_ESTABLISHING,
41 CALL_LEG_ST_ESTABLISHED,
42 CALL_LEG_ST_RELEASING,
43};
44
45struct osmo_tdef g_mgw_tdefs[] = {
46 { .T=-1, .default_val=4, .desc="MGCP response timeout" },
47 { .T=-2, .default_val=30, .desc="RTP stream establishing timeout" },
48 {}
49};
50
51static const struct osmo_tdef_state_timeout call_leg_fsm_timeouts[32] = {
52 [CALL_LEG_ST_ESTABLISHING] = { .T = -2 },
53 [CALL_LEG_ST_RELEASING] = { .T = -2 },
54};
55
56#define call_leg_state_chg(cl, state) \
57 osmo_tdef_fsm_inst_state_chg((cl)->fi, state, call_leg_fsm_timeouts, g_mgw_tdefs, 10)
58
59static struct osmo_fsm call_leg_fsm;
60
61void call_leg_init(struct gsm_network *net)
62{
63 gsmnet = net;
64 OSMO_ASSERT( osmo_fsm_register(&call_leg_fsm) == 0 );
65}
66
67struct call_leg *call_leg_alloc(struct osmo_fsm_inst *parent_fi,
68 uint32_t parent_event_term,
69 uint32_t parent_event_rtp_addr_available,
70 uint32_t parent_event_rtp_complete,
71 uint32_t parent_event_rtp_released)
72{
73 struct call_leg *cl;
74 struct osmo_fsm_inst *fi = osmo_fsm_inst_alloc_child(&call_leg_fsm, parent_fi, parent_event_term);
75
76 OSMO_ASSERT(fi);
77
78 cl = talloc(fi, struct call_leg);
79 OSMO_ASSERT(cl);
80 fi->priv = cl;
81 *cl = (struct call_leg){
82 .fi = fi,
83 .parent_event_rtp_addr_available = parent_event_rtp_addr_available,
84 .parent_event_rtp_complete = parent_event_rtp_complete,
85 .parent_event_rtp_released = parent_event_rtp_released,
86 };
87
88 return cl;
89}
90
91void call_leg_reparent(struct call_leg *cl,
92 struct osmo_fsm_inst *new_parent_fi,
93 uint32_t parent_event_term,
94 uint32_t parent_event_rtp_addr_available,
95 uint32_t parent_event_rtp_complete,
96 uint32_t parent_event_rtp_released)
97{
98 LOG_CALL_LEG(cl, LOGL_DEBUG, "Reparenting from parent %s to parent %s\n",
99 cl->fi->proc.parent->name, new_parent_fi->name);
100 osmo_fsm_inst_change_parent(cl->fi, new_parent_fi, parent_event_term);
101 talloc_steal(new_parent_fi, cl->fi);
102 cl->parent_event_rtp_addr_available = parent_event_rtp_addr_available;
103 cl->parent_event_rtp_complete = parent_event_rtp_complete;
104 cl->parent_event_rtp_released = parent_event_rtp_released;
105}
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;
300 if (!osmo_sockaddr_str_is_set(&rtps->local))
301 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];
320 if (codec_if_known)
321 rtp_stream_set_codec(cl->rtp[dir], *codec_if_known);
322 if (remote_addr_if_known && osmo_sockaddr_str_is_set(remote_addr_if_known))
323 rtp_stream_set_remote_addr(cl->rtp[dir], remote_addr_if_known);
324 return rtp_stream_ensure_ci(cl->rtp[dir], cl->mgw_endpoint);
325}
326
327int call_leg_local_bridge(struct call_leg *cl1, uint32_t call_id1, struct gsm_trans *trans1,
328 struct call_leg *cl2, uint32_t call_id2, struct gsm_trans *trans2)
329{
330 enum mgcp_codecs codec;
331
332 cl1->local_bridge = cl2;
333 cl2->local_bridge = cl1;
334
335 /* 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
336 * also means that if both legs use different codecs the MGW must perform transcoding on the second leg. */
337 if (!cl1->rtp[RTP_TO_RAN] || !cl1->rtp[RTP_TO_RAN]->codec_known) {
338 LOG_CALL_LEG(cl1, LOGL_ERROR, "RAN-side RTP stream codec is not known, not ready for bridging\n");
339 return -EINVAL;
340 }
341 codec = cl1->rtp[RTP_TO_RAN]->codec;
342
343 call_leg_ensure_ci(cl1, RTP_TO_CN, call_id1, trans1,
344 &codec, &cl2->rtp[RTP_TO_CN]->local);
345 call_leg_ensure_ci(cl2, RTP_TO_CN, call_id2, trans2,
346 &codec, &cl1->rtp[RTP_TO_CN]->local);
347 return 0;
348}