aboutsummaryrefslogtreecommitdiffstats
path: root/src/libmsc/call_leg.c
blob: b1d0b1e48203ca1b7ef76727ab850f661b89f4b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/* Implementation to manage two RTP streams that make up an MO or MT call leg's RTP forwarding. */
/*
 * (C) 2019 by sysmocom - s.m.f.c. GmbH <info@sysmocom.de>
 * All Rights Reserved
 *
 * Author: Neels Hofmeyr
 *
 * SPDX-License-Identifier: GPL-2.0+
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
#include <osmocom/core/fsm.h>
#include <osmocom/mgcp_client/mgcp_client_endpoint_fsm.h>

#include <osmocom/msc/debug.h>
#include <osmocom/msc/gsm_data.h>
#include <osmocom/msc/msc_a.h>

#include <osmocom/msc/call_leg.h>
#include <osmocom/msc/rtp_stream.h>

#define LOG_CALL_LEG(cl, level, fmt, args...) \
	LOGPFSML(cl ? cl->fi : NULL, level, fmt, ##args)

static struct gsm_network *gsmnet = NULL;

enum call_leg_state {
	CALL_LEG_ST_ESTABLISHING,
	CALL_LEG_ST_ESTABLISHED,
	CALL_LEG_ST_RELEASING,
};

struct osmo_tdef g_mgw_tdefs[] = {
	{ .T=-1, .default_val=4, .desc="MGCP response timeout" },
	{ .T=-2, .default_val=30, .desc="RTP stream establishing timeout" },
	{}
};

static const struct osmo_tdef_state_timeout call_leg_fsm_timeouts[32] = {
	[CALL_LEG_ST_ESTABLISHING] = { .T = -2 },
	[CALL_LEG_ST_RELEASING] = { .T = -2 },
};

#define call_leg_state_chg(cl, state) \
	osmo_tdef_fsm_inst_state_chg((cl)->fi, state, call_leg_fsm_timeouts, g_mgw_tdefs, 10)

static struct osmo_fsm call_leg_fsm;

void call_leg_init(struct gsm_network *net)
{
	gsmnet = net;
	OSMO_ASSERT( osmo_fsm_register(&call_leg_fsm) == 0 );
}

/* Allocate a call leg FSM instance as child of an arbitrary other FSM instance.
 * The call leg FSM dispatches events to its parent FSM instance on specific events:
 * - parent_event_term: dispatch this to the parent FI when the call leg terminates (call ended, either planned or by
 *   failure).
 * - parent_event_rtp_addr_available: one of the rtp_stream instances managed by the call leg has received an RTP
 *   address from the MGW. The struct rtp_stream instance is passed as data argument for the event dispatch.
 * - parent_event_rtp_complete: one of the rtp_stream instances entered the RTP_STREAM_ST_ESTABLISHED state.
 */
struct call_leg *call_leg_alloc(struct osmo_fsm_inst *parent_fi,
				uint32_t parent_event_term,
				uint32_t parent_event_rtp_addr_available,
				uint32_t parent_event_rtp_complete)
{
	struct call_leg *cl;
	struct osmo_fsm_inst *fi = osmo_fsm_inst_alloc_child(&call_leg_fsm, parent_fi, parent_event_term);

	OSMO_ASSERT(fi);

	cl = talloc(fi, struct call_leg);
	OSMO_ASSERT(cl);
	fi->priv = cl;
	*cl = (struct call_leg){
		.fi = fi,
		.parent_event_rtp_addr_available = parent_event_rtp_addr_available,
		.parent_event_rtp_complete = parent_event_rtp_complete,
	};

	return cl;
}

void call_leg_reparent(struct call_leg *cl,
		       struct osmo_fsm_inst *new_parent_fi,
		       uint32_t parent_event_term,
		       uint32_t parent_event_rtp_addr_available,
		       uint32_t parent_event_rtp_complete)
{
	LOG_CALL_LEG(cl, LOGL_DEBUG, "Reparenting from parent %s to parent %s\n",
		     cl->fi->proc.parent->name, new_parent_fi->name);
	osmo_fsm_inst_change_parent(cl->fi, new_parent_fi, parent_event_term);
	talloc_steal(new_parent_fi, cl->fi);
	cl->parent_event_rtp_addr_available = parent_event_rtp_addr_available;
	cl->parent_event_rtp_complete = parent_event_rtp_complete;
}

static int call_leg_fsm_timer_cb(struct osmo_fsm_inst *fi)
{
	struct call_leg *cl = fi->priv;
	call_leg_release(cl);
	return 0;
}

void call_leg_release(struct call_leg *cl)
{
	if (!cl)
		return;
	if (cl->fi->state == CALL_LEG_ST_RELEASING)
		return;
	call_leg_state_chg(cl, CALL_LEG_ST_RELEASING);
}

static void call_leg_mgw_endpoint_gone(struct call_leg *cl)
{
	int i;
	cl->mgw_endpoint = NULL;
	for (i = 0; i < ARRAY_SIZE(cl->rtp); i++) {
		if (!cl->rtp[i])
			continue;
		cl->rtp[i]->ci = NULL;
	}
}

static void call_leg_fsm_establishing_established(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
	struct call_leg *cl = fi->priv;
	struct rtp_stream *rtps;
	int i;
	bool established;

	switch (event) {

	case CALL_LEG_EV_RTP_STREAM_ESTABLISHED:
		/* An rtp_stream says it is established. If all are now established, change to state
		 * CALL_LEG_ST_ESTABLISHED. */
		established = true;
		for (i = 0; i < ARRAY_SIZE(cl->rtp); i++) {
			if (!rtp_stream_is_established(cl->rtp[i])) {
				established = false;
				break;
			}
		}
		if (!established)
			break;
		call_leg_state_chg(cl, CALL_LEG_ST_ESTABLISHED);
		break;

	case CALL_LEG_EV_RTP_STREAM_ADDR_AVAILABLE:
		rtps = data;
		osmo_fsm_inst_dispatch(fi->proc.parent, cl->parent_event_rtp_addr_available, rtps);
		break;

	case CALL_LEG_EV_RTP_STREAM_GONE:
		call_leg_release(cl);
		break;

	case CALL_LEG_EV_MGW_ENDPOINT_GONE:
		call_leg_mgw_endpoint_gone(cl);
		call_leg_release(cl);
		break;

	default:
		OSMO_ASSERT(false);
	}
}

void call_leg_fsm_established_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
{
	struct call_leg *cl = fi->priv;
	osmo_fsm_inst_dispatch(fi->proc.parent, cl->parent_event_rtp_complete, cl);
}

void call_leg_fsm_releasing_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
{
	osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
}

static void call_leg_fsm_releasing(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
	struct call_leg *cl = fi->priv;

	switch (event) {

	case CALL_LEG_EV_RTP_STREAM_GONE:
		/* We're already terminating, child RTP streams will also terminate, there is nothing left to do. */
		break;

	case CALL_LEG_EV_MGW_ENDPOINT_GONE:
		call_leg_mgw_endpoint_gone(cl);
		break;

	default:
		OSMO_ASSERT(false);
	}
}

static const struct value_string call_leg_fsm_event_names[] = {
	OSMO_VALUE_STRING(CALL_LEG_EV_RTP_STREAM_ADDR_AVAILABLE),
	OSMO_VALUE_STRING(CALL_LEG_EV_RTP_STREAM_ESTABLISHED),
	OSMO_VALUE_STRING(CALL_LEG_EV_RTP_STREAM_GONE),
	OSMO_VALUE_STRING(CALL_LEG_EV_MGW_ENDPOINT_GONE),
	{}
};

#define S(x)	(1 << (x))

static const struct osmo_fsm_state call_leg_fsm_states[] = {
	[CALL_LEG_ST_ESTABLISHING] = {
		.name = "ESTABLISHING",
		.in_event_mask = 0
			| S(CALL_LEG_EV_RTP_STREAM_ADDR_AVAILABLE)
			| S(CALL_LEG_EV_RTP_STREAM_ESTABLISHED)
			| S(CALL_LEG_EV_RTP_STREAM_GONE)
			| S(CALL_LEG_EV_MGW_ENDPOINT_GONE)
			,
		.out_state_mask = 0
			| S(CALL_LEG_ST_ESTABLISHED)
			| S(CALL_LEG_ST_RELEASING)
			,
		.action = call_leg_fsm_establishing_established,
	},
	[CALL_LEG_ST_ESTABLISHED] = {
		.name = "ESTABLISHED",
		.in_event_mask = 0
			| S(CALL_LEG_EV_RTP_STREAM_ADDR_AVAILABLE)
			| S(CALL_LEG_EV_RTP_STREAM_ESTABLISHED)
			| S(CALL_LEG_EV_RTP_STREAM_GONE)
			| S(CALL_LEG_EV_MGW_ENDPOINT_GONE)
			,
		.out_state_mask = 0
			| S(CALL_LEG_ST_ESTABLISHING)
			| S(CALL_LEG_ST_RELEASING)
			,
		.onenter = call_leg_fsm_established_onenter,
		.action = call_leg_fsm_establishing_established, /* same action function as above */
	},
	[CALL_LEG_ST_RELEASING] = {
		.name = "RELEASING",
		.in_event_mask = 0
			| S(CALL_LEG_EV_RTP_STREAM_GONE)
			| S(CALL_LEG_EV_MGW_ENDPOINT_GONE)
			,
		.onenter = call_leg_fsm_releasing_onenter,
		.action = call_leg_fsm_releasing,
	},
};

static struct osmo_fsm call_leg_fsm = {
	.name = "call_leg",
	.states = call_leg_fsm_states,
	.num_states = ARRAY_SIZE(call_leg_fsm_states),
	.log_subsys = DCC,
	.event_names = call_leg_fsm_event_names,
	.timer_cb = call_leg_fsm_timer_cb,
};

const struct value_string rtp_direction_names[] = {
	OSMO_VALUE_STRING(RTP_TO_RAN),
	OSMO_VALUE_STRING(RTP_TO_CN),
	{}
};

int call_leg_ensure_rtp_alloc(struct call_leg *cl, enum rtp_direction dir, uint32_t call_id, struct gsm_trans *for_trans)
{
	if (cl->rtp[dir])
		return 0;

	if (!cl->mgw_endpoint)
		cl->mgw_endpoint = osmo_mgcpc_ep_alloc(cl->fi, CALL_LEG_EV_MGW_ENDPOINT_GONE,
						       gsmnet->mgw.client, gsmnet->mgw.tdefs, cl->fi->id,
						       "%s", mgcp_client_rtpbridge_wildcard(gsmnet->mgw.client));
	if (!cl->mgw_endpoint) {
		LOG_CALL_LEG(cl, LOGL_ERROR, "failed to setup MGW endpoint\n");
		return -EIO;
	}

	cl->rtp[dir] = rtp_stream_alloc(cl, dir, call_id, for_trans);
	OSMO_ASSERT(cl->rtp[dir]);
	return 0;
}

struct osmo_sockaddr_str *call_leg_local_ip(struct call_leg *cl, enum rtp_direction dir)
{
	struct rtp_stream *rtps;
	if (!cl)
		return NULL;
	rtps = cl->rtp[dir];
	if (!rtps)
		return NULL;
	if (!osmo_sockaddr_str_is_nonzero(&rtps->local))
		return NULL;
	return &rtps->local;
}

/* Make sure an MGW endpoint CI is set up for an RTP connection.
 * This is the one-stop for all to either completely set up a new endpoint connection, or to modify an existing one.
 * If not yet present, allocate the rtp_stream for the given direction.
 * Then, call rtp_stream_set_codec() if codec_if_known is non-NULL, and/or rtp_stream_set_remote_addr() if
 * remote_addr_if_known is non-NULL.
 * Finally make sure that a CRCX is sent out for this direction, if this has not already happened.
 * If the CRCX has already happened but new codec / remote_addr data was passed, call rtp_stream_commit() to trigger an
 * MDCX.
 */
int call_leg_ensure_ci(struct call_leg *cl, enum rtp_direction dir, uint32_t call_id, struct gsm_trans *for_trans,
		       const enum mgcp_codecs *codec_if_known, const struct osmo_sockaddr_str *remote_addr_if_known)
{
	if (call_leg_ensure_rtp_alloc(cl, dir, call_id, for_trans))
		return -EIO;
	cl->rtp[dir]->crcx_conn_mode = cl->crcx_conn_mode[dir];
	if (dir == RTP_TO_RAN && cl->ran_peer_supports_osmux) {
		cl->rtp[dir]->use_osmux = true;
		cl->rtp[dir]->remote_osmux_cid = -1; /* wildcard */
	}
	if (codec_if_known)
		rtp_stream_set_codec(cl->rtp[dir], *codec_if_known);
	if (remote_addr_if_known && osmo_sockaddr_str_is_nonzero(remote_addr_if_known))
		rtp_stream_set_remote_addr(cl->rtp[dir], remote_addr_if_known);
	return rtp_stream_ensure_ci(cl->rtp[dir], cl->mgw_endpoint);
}

int call_leg_local_bridge(struct call_leg *cl1, uint32_t call_id1, struct gsm_trans *trans1,
			  struct call_leg *cl2, uint32_t call_id2, struct gsm_trans *trans2)
{
	enum mgcp_codecs codec;

	cl1->local_bridge = cl2;
	cl2->local_bridge = cl1;

	/* 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
	 * also means that if both legs use different codecs the MGW must perform transcoding on the second leg. */
	if (!cl1->rtp[RTP_TO_RAN] || !cl1->rtp[RTP_TO_RAN]->codec_known) {
		LOG_CALL_LEG(cl1, LOGL_ERROR, "RAN-side RTP stream codec is not known, not ready for bridging\n");
		return -EINVAL;
	}
	codec = cl1->rtp[RTP_TO_RAN]->codec;

	call_leg_ensure_ci(cl1, RTP_TO_CN, call_id1, trans1,
			   &codec, &cl2->rtp[RTP_TO_CN]->local);
	call_leg_ensure_ci(cl2, RTP_TO_CN, call_id2, trans2,
			   &codec, &cl1->rtp[RTP_TO_CN]->local);
	return 0;
}