blob: f8a75c451dd862eda837f4633074170eb428ea27 [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{
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{
191 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
192}
193
194static void call_leg_fsm_releasing(struct osmo_fsm_inst *fi, uint32_t event, void *data)
195{
196 struct call_leg *cl = fi->priv;
197
198 switch (event) {
199
200 case CALL_LEG_EV_RTP_STREAM_GONE:
201 /* We're already terminating, child RTP streams will also terminate, there is nothing left to do. */
202 break;
203
204 case CALL_LEG_EV_MGW_ENDPOINT_GONE:
205 call_leg_mgw_endpoint_gone(cl);
206 break;
207
208 default:
209 OSMO_ASSERT(false);
210 }
211}
212
213static const struct value_string call_leg_fsm_event_names[] = {
214 OSMO_VALUE_STRING(CALL_LEG_EV_RTP_STREAM_ADDR_AVAILABLE),
215 OSMO_VALUE_STRING(CALL_LEG_EV_RTP_STREAM_ESTABLISHED),
216 OSMO_VALUE_STRING(CALL_LEG_EV_RTP_STREAM_GONE),
217 OSMO_VALUE_STRING(CALL_LEG_EV_MGW_ENDPOINT_GONE),
218 {}
219};
220
221#define S(x) (1 << (x))
222
223static const struct osmo_fsm_state call_leg_fsm_states[] = {
224 [CALL_LEG_ST_ESTABLISHING] = {
225 .name = "ESTABLISHING",
226 .in_event_mask = 0
227 | S(CALL_LEG_EV_RTP_STREAM_ADDR_AVAILABLE)
228 | S(CALL_LEG_EV_RTP_STREAM_ESTABLISHED)
229 | S(CALL_LEG_EV_RTP_STREAM_GONE)
230 | S(CALL_LEG_EV_MGW_ENDPOINT_GONE)
231 ,
232 .out_state_mask = 0
233 | S(CALL_LEG_ST_ESTABLISHED)
234 | S(CALL_LEG_ST_RELEASING)
235 ,
236 .action = call_leg_fsm_establishing_established,
237 },
238 [CALL_LEG_ST_ESTABLISHED] = {
239 .name = "ESTABLISHED",
240 .in_event_mask = 0
241 | S(CALL_LEG_EV_RTP_STREAM_ADDR_AVAILABLE)
242 | S(CALL_LEG_EV_RTP_STREAM_ESTABLISHED)
243 | S(CALL_LEG_EV_RTP_STREAM_GONE)
244 | S(CALL_LEG_EV_MGW_ENDPOINT_GONE)
245 ,
246 .out_state_mask = 0
247 | S(CALL_LEG_ST_ESTABLISHING)
248 | S(CALL_LEG_ST_RELEASING)
249 ,
250 .onenter = call_leg_fsm_established_onenter,
251 .action = call_leg_fsm_establishing_established, /* same action function as above */
252 },
253 [CALL_LEG_ST_RELEASING] = {
254 .name = "RELEASING",
255 .in_event_mask = 0
256 | S(CALL_LEG_EV_RTP_STREAM_GONE)
257 | S(CALL_LEG_EV_MGW_ENDPOINT_GONE)
258 ,
259 .onenter = call_leg_fsm_releasing_onenter,
260 .action = call_leg_fsm_releasing,
261 },
262};
263
264static struct osmo_fsm call_leg_fsm = {
265 .name = "call_leg",
266 .states = call_leg_fsm_states,
267 .num_states = ARRAY_SIZE(call_leg_fsm_states),
268 .log_subsys = DCC,
269 .event_names = call_leg_fsm_event_names,
270 .timer_cb = call_leg_fsm_timer_cb,
271};
272
273const struct value_string rtp_direction_names[] = {
274 OSMO_VALUE_STRING(RTP_TO_RAN),
275 OSMO_VALUE_STRING(RTP_TO_CN),
276 {}
277};
278
279int call_leg_ensure_rtp_alloc(struct call_leg *cl, enum rtp_direction dir, uint32_t call_id, struct gsm_trans *for_trans)
280{
281 if (cl->rtp[dir])
282 return 0;
283
Pau Espin Pedrolb44cf2d2022-10-17 18:09:15 +0200284 if (!cl->mgw_endpoint) {
285 struct mgcp_client *mgcp_client = mgcp_client_pool_get(gsmnet->mgw.mgw_pool);
286 if (!mgcp_client) {
287 LOG_CALL_LEG(cl, LOGL_ERROR,
288 "cannot ensure MGW endpoint -- no MGW configured, check configuration!\n");
289 return -ENODEV;
290 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100291 cl->mgw_endpoint = osmo_mgcpc_ep_alloc(cl->fi, CALL_LEG_EV_MGW_ENDPOINT_GONE,
Pau Espin Pedrolb44cf2d2022-10-17 18:09:15 +0200292 mgcp_client, gsmnet->mgw.tdefs, cl->fi->id,
293 "%s", mgcp_client_rtpbridge_wildcard(mgcp_client));
294 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100295 if (!cl->mgw_endpoint) {
296 LOG_CALL_LEG(cl, LOGL_ERROR, "failed to setup MGW endpoint\n");
297 return -EIO;
298 }
299
300 cl->rtp[dir] = rtp_stream_alloc(cl, dir, call_id, for_trans);
301 OSMO_ASSERT(cl->rtp[dir]);
302 return 0;
303}
304
305struct osmo_sockaddr_str *call_leg_local_ip(struct call_leg *cl, enum rtp_direction dir)
306{
307 struct rtp_stream *rtps;
308 if (!cl)
309 return NULL;
310 rtps = cl->rtp[dir];
311 if (!rtps)
312 return NULL;
Neels Hofmeyr84ce2062019-10-05 05:15:25 +0200313 if (!osmo_sockaddr_str_is_nonzero(&rtps->local))
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100314 return NULL;
315 return &rtps->local;
316}
317
318/* Make sure an MGW endpoint CI is set up for an RTP connection.
319 * This is the one-stop for all to either completely set up a new endpoint connection, or to modify an existing one.
320 * If not yet present, allocate the rtp_stream for the given direction.
321 * Then, call rtp_stream_set_codec() if codec_if_known is non-NULL, and/or rtp_stream_set_remote_addr() if
322 * remote_addr_if_known is non-NULL.
323 * Finally make sure that a CRCX is sent out for this direction, if this has not already happened.
324 * If the CRCX has already happened but new codec / remote_addr data was passed, call rtp_stream_commit() to trigger an
325 * MDCX.
326 */
327int call_leg_ensure_ci(struct call_leg *cl, enum rtp_direction dir, uint32_t call_id, struct gsm_trans *for_trans,
328 const enum mgcp_codecs *codec_if_known, const struct osmo_sockaddr_str *remote_addr_if_known)
329{
330 if (call_leg_ensure_rtp_alloc(cl, dir, call_id, for_trans))
331 return -EIO;
332 cl->rtp[dir]->crcx_conn_mode = cl->crcx_conn_mode[dir];
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200333 if (dir == RTP_TO_RAN && cl->ran_peer_supports_osmux) {
334 cl->rtp[dir]->use_osmux = true;
335 cl->rtp[dir]->remote_osmux_cid = -1; /* wildcard */
336 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100337 if (codec_if_known)
338 rtp_stream_set_codec(cl->rtp[dir], *codec_if_known);
Neels Hofmeyr84ce2062019-10-05 05:15:25 +0200339 if (remote_addr_if_known && osmo_sockaddr_str_is_nonzero(remote_addr_if_known))
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100340 rtp_stream_set_remote_addr(cl->rtp[dir], remote_addr_if_known);
341 return rtp_stream_ensure_ci(cl->rtp[dir], cl->mgw_endpoint);
342}
343
344int call_leg_local_bridge(struct call_leg *cl1, uint32_t call_id1, struct gsm_trans *trans1,
345 struct call_leg *cl2, uint32_t call_id2, struct gsm_trans *trans2)
346{
347 enum mgcp_codecs codec;
348
349 cl1->local_bridge = cl2;
350 cl2->local_bridge = cl1;
351
352 /* 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
353 * also means that if both legs use different codecs the MGW must perform transcoding on the second leg. */
354 if (!cl1->rtp[RTP_TO_RAN] || !cl1->rtp[RTP_TO_RAN]->codec_known) {
355 LOG_CALL_LEG(cl1, LOGL_ERROR, "RAN-side RTP stream codec is not known, not ready for bridging\n");
356 return -EINVAL;
357 }
358 codec = cl1->rtp[RTP_TO_RAN]->codec;
359
Pau Espin Pedroldd262262022-01-13 14:06:44 +0100360 if (!cl1->rtp[RTP_TO_CN] || !cl2->rtp[RTP_TO_CN])
361 return -ENOTCONN;
362
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100363 call_leg_ensure_ci(cl1, RTP_TO_CN, call_id1, trans1,
364 &codec, &cl2->rtp[RTP_TO_CN]->local);
365 call_leg_ensure_ci(cl2, RTP_TO_CN, call_id2, trans2,
366 &codec, &cl1->rtp[RTP_TO_CN]->local);
367 return 0;
368}