blob: ef4da17348fb22a7e7ac07cedcaf77976a89126e [file] [log] [blame]
Neels Hofmeyr538d2c52019-01-28 03:51:35 +01001/* FSM to manage multiple connections of an MGW endpoint
2 *
3 * (C) 2018-2019 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
4 * All Rights Reserved
5 *
6 * Author: Neels Hofmeyr <neels@hofmeyr.de>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#include <sys/socket.h>
24#include <netinet/in.h>
25#include <arpa/inet.h>
26
27#include <osmocom/core/fsm.h>
28#include <osmocom/core/byteswap.h>
29#include <osmocom/core/tdef.h>
30
31#include <osmocom/mgcp_client/mgcp_client_endpoint_fsm.h>
32
33#define LOG_CI(ci, level, fmt, args...) do { \
34 if (!ci || !ci->ep) \
35 LOGP(DLGLOBAL, level, "(unknown MGW endpoint) " fmt, ## args); \
36 else \
37 LOG_MGCPC_EP(ci->ep, level, "CI[%d] %s%s%s: " fmt, \
38 (int)(ci - ci->ep->ci), \
39 ci->label ? : "-", \
40 ci->mgcp_ci_str[0] ? " CI=" : "", \
41 ci->mgcp_ci_str[0] ? ci->mgcp_ci_str : "", \
42 ## args); \
43 } while(0)
44
45#define LOG_CI_VERB(ci, level, fmt, args...) do { \
46 if (ci->verb_info.addr[0]) \
47 LOG_CI(ci, level, "%s %s:%u: " fmt, \
48 osmo_mgcp_verb_name(ci->verb), ci->verb_info.addr, ci->verb_info.port, \
49 ## args); \
50 else \
51 LOG_CI(ci, level, "%s: " fmt, \
52 osmo_mgcp_verb_name(ci->verb), \
53 ## args); \
54 } while(0)
55
56enum osmo_mgcpc_ep_fsm_state {
57 OSMO_MGCPC_EP_ST_UNUSED = 0,
58 OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE,
59 OSMO_MGCPC_EP_ST_IN_USE,
60};
61
62enum osmo_mgcpc_ep_fsm_event {
63 _OSMO_MGCPC_EP_EV_LAST = 0,
64 /* and MGW response events are allocated dynamically */
65};
66
67#define FIRST_CI_EVENT (_OSMO_MGCPC_EP_EV_LAST + (_OSMO_MGCPC_EP_EV_LAST & 1)) /* rounded up to even nr */
68#define USABLE_CI ((32 - FIRST_CI_EVENT)/2)
69#define EV_TO_CI_IDX(event) ((event - FIRST_CI_EVENT) / 2)
70
71#define CI_EV_SUCCESS(ci) (FIRST_CI_EVENT + (((ci) - ci->ep->ci) * 2))
72#define CI_EV_FAILURE(ci) (CI_EV_SUCCESS(ci) + 1)
73
74static struct osmo_fsm osmo_mgcpc_ep_fsm;
75
Neels Hofmeyr3ff71282019-10-29 17:41:20 +010076struct fsm_notify {
Neels Hofmeyrf2bf8dc2019-10-29 17:39:56 +010077 struct llist_head entry;
Neels Hofmeyr3ff71282019-10-29 17:41:20 +010078 struct osmo_fsm_inst *fi;
79 uint32_t success;
80 uint32_t failure;
81 void *data;
82};
83
Neels Hofmeyr538d2c52019-01-28 03:51:35 +010084/*! One connection on an endpoint, corresponding to a connection identifier (CI) as returned by the MGW.
85 * An endpoint has a fixed number of slots of these, which may or may not be in use.
86 */
87struct osmo_mgcpc_ep_ci {
88 struct osmo_mgcpc_ep *ep;
89
90 bool occupied;
91 char label[64];
92 struct osmo_fsm_inst *mgcp_client_fi;
93
94 bool pending;
95 bool sent;
96 enum mgcp_verb verb;
97 struct mgcp_conn_peer verb_info;
Neels Hofmeyr3ff71282019-10-29 17:41:20 +010098 struct fsm_notify notify;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +010099
100 bool got_port_info;
101 struct mgcp_conn_peer rtp_info;
102 char mgcp_ci_str[MGCP_CONN_ID_LENGTH];
103};
104
105/*! An MGW endpoint with N connections, like "rtpbridge/23@mgw". */
106struct osmo_mgcpc_ep {
107 /*! MGCP client connection to the MGW. */
108 struct mgcp_client *mgcp_client;
109 struct osmo_fsm_inst *fi;
110
111 /*! Endpoint string; at first this might be a wildcard, and upon the first CRCX OK response, this will reflect
112 * the endpoint name returned by the MGW. */
113 char endpoint[MGCP_ENDPOINT_MAXLEN];
114
115 /*! Timeout definitions used for this endpoint, see osmo_mgcpc_ep_fsm_timeouts. */
116 const struct osmo_tdef *T_defs;
117
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200118 /*! True as soon as the first CRCX OK is received. The endpoint name may be determined by the first CRCX
119 * response, so to dispatch any other messages, the FSM instance *must* wait for the first CRCX OK to arrive
120 * first. Once the endpoint name is pinpointed, any amount of operations may be dispatched concurrently. */
121 bool first_crcx_complete;
122
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100123 /*! Endpoint connection slots. Note that each connection has its own set of FSM event numbers to signal success
124 * and failure, depending on its index within this array. See CI_EV_SUCCESS and CI_EV_FAILURE. */
125 struct osmo_mgcpc_ep_ci ci[USABLE_CI];
Neels Hofmeyrf2bf8dc2019-10-29 17:39:56 +0100126
127 /*! Internal use: if a function keeps an fsm_notify for later dispatch while already clearing or re-using the
128 * ci[], the fsm_notify should be kept here to also get canceled by osmo_mgcpc_ep_cancel_notify(). */
129 struct llist_head background_notify;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100130};
131
132const struct value_string osmo_mgcp_verb_names[] = {
133 { MGCP_VERB_CRCX, "CRCX" },
134 { MGCP_VERB_MDCX, "MDCX" },
135 { MGCP_VERB_DLCX, "DLCX" },
136 { MGCP_VERB_AUEP, "AUEP" },
137 { MGCP_VERB_RSIP, "RSIP" },
138 {}
139};
140
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200141static void osmo_mgcpc_ep_count(struct osmo_mgcpc_ep *ep, int *occupied, int *pending_not_sent,
142 int *waiting_for_response);
143
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100144static struct osmo_mgcpc_ep_ci *osmo_mgcpc_ep_check_ci(struct osmo_mgcpc_ep_ci *ci)
145{
146 if (!ci)
147 return NULL;
148 if (!ci->ep)
149 return NULL;
150 if (ci < ci->ep->ci || ci >= &ci->ep->ci[USABLE_CI])
151 return NULL;
152 return ci;
153}
154
155static struct osmo_mgcpc_ep_ci *osmo_mgcpc_ep_ci_for_event(struct osmo_mgcpc_ep *ep, uint32_t event)
156{
157 int idx;
158 if (event < FIRST_CI_EVENT)
159 return NULL;
160 idx = EV_TO_CI_IDX(event);
161 if (idx >= sizeof(ep->ci))
162 return NULL;
163 return osmo_mgcpc_ep_check_ci(&ep->ci[idx]);
164}
165
166const char *osmo_mgcpc_ep_name(const struct osmo_mgcpc_ep *ep)
167{
168 if (!ep)
169 return "NULL";
170 if (ep->endpoint[0])
171 return ep->endpoint;
172 return osmo_fsm_inst_name(ep->fi);
173}
174
175const char *mgcp_conn_peer_name(const struct mgcp_conn_peer *info)
176{
177 /* I'd be fine with a smaller buffer and accept truncation, but gcc possibly refuses to build if
178 * this buffer is too small. */
179 static char buf[1024];
180
181 if (!info)
182 return "NULL";
183
184 if (info->endpoint[0]
185 && info->addr[0])
186 snprintf(buf, sizeof(buf), "%s:%s:%u",
187 info->endpoint, info->addr, info->port);
188 else if (info->endpoint[0])
189 snprintf(buf, sizeof(buf), "%s", info->endpoint);
190 else if (info->addr[0])
191 snprintf(buf, sizeof(buf), "%s:%u", info->addr, info->port);
192 else
193 return "empty";
194 return buf;
195}
196
197const char *osmo_mgcpc_ep_ci_name(const struct osmo_mgcpc_ep_ci *ci)
198{
199 const struct mgcp_conn_peer *rtp_info;
200
201 if (!ci)
202 return "NULL";
203
204 rtp_info = osmo_mgcpc_ep_ci_get_rtp_info(ci);
205
206 if (rtp_info)
207 return mgcp_conn_peer_name(rtp_info);
208 return osmo_mgcpc_ep_name(ci->ep);
209}
210
211const char *osmo_mgcpc_ep_ci_id(const struct osmo_mgcpc_ep_ci *ci)
212{
213 if (!ci || !ci->mgcp_ci_str[0])
214 return NULL;
215 return ci->mgcp_ci_str;
216}
217
218static struct value_string osmo_mgcpc_ep_fsm_event_names[33] = {};
219
220static char osmo_mgcpc_ep_fsm_event_name_bufs[32][32] = {};
221
222static void fill_event_names()
223{
224 int i;
225 for (i = 0; i < (ARRAY_SIZE(osmo_mgcpc_ep_fsm_event_names) - 1); i++) {
226 if (i < _OSMO_MGCPC_EP_EV_LAST)
227 continue;
228 if (i < FIRST_CI_EVENT || EV_TO_CI_IDX(i) > USABLE_CI) {
229 osmo_mgcpc_ep_fsm_event_names[i] = (struct value_string){i, "Unused"};
230 continue;
231 }
232 snprintf(osmo_mgcpc_ep_fsm_event_name_bufs[i], sizeof(osmo_mgcpc_ep_fsm_event_name_bufs[i]),
233 "MGW Response for CI #%d", EV_TO_CI_IDX(i));
234 osmo_mgcpc_ep_fsm_event_names[i] = (struct value_string){i, osmo_mgcpc_ep_fsm_event_name_bufs[i]};
235 }
236}
237
238/* T_defs is used to obtain an (Osmocom specific) T2427001: timeout for an MGCP response (note, 2427 corresponds to the
239 * default MGCP port in osmo-mgw). */
240static __attribute__((constructor)) void osmo_mgcpc_ep_fsm_init()
241{
242 OSMO_ASSERT(osmo_fsm_register(&osmo_mgcpc_ep_fsm) == 0);
243 fill_event_names();
244}
245
246struct osmo_mgcpc_ep *osmo_mgcpc_ep_fi_mgwep(struct osmo_fsm_inst *fi)
247{
248 OSMO_ASSERT(fi);
249 OSMO_ASSERT(fi->fsm == &osmo_mgcpc_ep_fsm);
250 OSMO_ASSERT(fi->priv);
251 return fi->priv;
252}
253
254/*! Allocate an osmo_mgcpc_ep FSM.
255 * MGCP messages to set up the endpoint will be sent on the given mgcp_client, as soon as the first
256 * osmo_mgcpc_ep_ci_request() is invoked.
257 *
Neels Hofmeyrca2aec02019-10-04 22:47:31 +0200258 * IMPORTANT: To avoid use-after-free problems, using this FSM requires use of deferred FSM deallocation using
259 * osmo_fsm_set_dealloc_ctx(), e.g. using osmo_select_main_ctx(OTC_SELECT) with osmo_select_main_ctx() as main loop.
260 *
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100261 * A typical sequence of events would be:
262 *
263 * ep = osmo_mgcpc_ep_alloc(..., mgcp_client_rtpbridge_wildcard(client));
264 * ci_to_ran = osmo_mgcpc_ep_ci_add(ep);
265 * osmo_mgcpc_ep_ci_request(ci_to_ran, MGCP_VERB_CRCX, verb_info,
266 * my_call_fsm, MY_EVENT_MGCP_OK, MY_EVENT_MGCP_FAIL);
267 * ci_to_cn = osmo_mgcpc_ep_ci_add(ep);
268 * osmo_mgcpc_ep_ci_request(ci_to_cn, MGCP_VERB_CRCX, verb_info,
269 * my_call_fsm, MY_EVENT_MGCP_OK, MY_EVENT_MGCP_FAIL);
270 * ...
271 * osmo_mgcpc_ep_ci_request(ci_to_ran, MGCP_VERB_MDCX, ...);
272 * ...
273 * osmo_mgcpc_ep_clear(ep);
274 * ep = NULL;
275 *
276 * \param parent Parent FSM.
277 * \param parent_term_event Event to dispatch to the parent on termination of this FSM instance.
278 * \param mgcp_client Connection to the MGW.
279 * \param T_defs Timeout definitions to be used for FSM states, see osmo_mgcpc_ep_fsm_timeouts.
280 * \param fsm_id FSM instance ID.
281 * \param endpoint_str_fmt The endpoint string format to send to the MGW upon the first CRCX.
282 * See mgcp_client_rtpbridge_wildcard() for "rtpbridge" endpoints.
283 */
284struct osmo_mgcpc_ep *osmo_mgcpc_ep_alloc(struct osmo_fsm_inst *parent, uint32_t parent_term_event,
285 struct mgcp_client *mgcp_client,
286 const struct osmo_tdef *T_defs,
287 const char *fsm_id,
288 const char *endpoint_str_fmt, ...)
289{
290 va_list ap;
291 struct osmo_fsm_inst *fi;
292 struct osmo_mgcpc_ep *ep;
293 int rc;
294
295 if (!mgcp_client)
296 return NULL;
297
298 fi = osmo_fsm_inst_alloc_child(&osmo_mgcpc_ep_fsm, parent, parent_term_event);
299 OSMO_ASSERT(fi);
300
301 osmo_fsm_inst_update_id(fi, fsm_id);
302
303 ep = talloc_zero(fi, struct osmo_mgcpc_ep);
304 OSMO_ASSERT(ep);
305
306 *ep = (struct osmo_mgcpc_ep){
307 .mgcp_client = mgcp_client,
308 .fi = fi,
309 .T_defs = T_defs,
310 };
Neels Hofmeyrf2bf8dc2019-10-29 17:39:56 +0100311 INIT_LLIST_HEAD(&ep->background_notify);
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100312 fi->priv = ep;
313
314 va_start(ap, endpoint_str_fmt);
315 rc = vsnprintf(ep->endpoint, sizeof(ep->endpoint), endpoint_str_fmt ? : "", ap);
316 va_end(ap);
317
318 if (rc <= 0 || rc >= sizeof(ep->endpoint)) {
319 LOG_MGCPC_EP(ep, LOGL_ERROR, "Endpoint name too long or too short: %s\n",
320 ep->endpoint);
321 osmo_fsm_inst_term(ep->fi, OSMO_FSM_TERM_ERROR, 0);
322 return NULL;
323 }
324
325 return ep;
326}
327
328/*! Add a connection to an endpoint.
329 * Allocate a connection identifier slot in the osmo_mgcpc_ep instance, do not yet dispatch a CRCX.
330 * The CRCX is dispatched only upon the first osmo_mgcpc_ep_ci_request().
331 * \param ep Parent endpoint instance.
332 * \param label_fmt Label for logging.
333 */
334struct osmo_mgcpc_ep_ci *osmo_mgcpc_ep_ci_add(struct osmo_mgcpc_ep *ep,
335 const char *label_fmt, ...)
336{
337 va_list ap;
338 int i;
339 struct osmo_mgcpc_ep_ci *ci;
340
341 for (i = 0; i < USABLE_CI; i++) {
342 ci = &ep->ci[i];
343
344 if (ci->occupied || ci->mgcp_client_fi)
345 continue;
346
347 *ci = (struct osmo_mgcpc_ep_ci){
348 .ep = ep,
349 .occupied = true,
350 };
351 if (label_fmt) {
352 va_start(ap, label_fmt);
353 vsnprintf(ci->label, sizeof(ci->label), label_fmt, ap);
354 va_end(ap);
355 }
356 return ci;
357 }
358
359 LOG_MGCPC_EP(ep, LOGL_ERROR,
360 "Cannot allocate another endpoint, all "
361 OSMO_STRINGIFY_VAL(USABLE_CI) " are in use\n");
362
363 return NULL;
364}
365
366static bool osmo_mgcpc_ep_fsm_check_state_chg_after_response(struct osmo_fsm_inst *fi);
367
368static void on_failure(struct osmo_mgcpc_ep_ci *ci)
369{
Neels Hofmeyrcc0b97e2019-10-01 19:44:10 +0200370 struct osmo_mgcpc_ep *ep = ci->ep;
Neels Hofmeyr3ff71282019-10-29 17:41:20 +0100371 struct fsm_notify notify;
Neels Hofmeyrcc0b97e2019-10-01 19:44:10 +0200372 int i;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100373
374 if (!ci->occupied)
375 return;
376
Neels Hofmeyr3ff71282019-10-29 17:41:20 +0100377 /* When dispatching an event for this CI, the user may decide to trigger the next request for this conn right
378 * away. So we must be ready with a cleared *ci. Store the notify separately and clear before dispatching. */
379 notify = ci->notify;
Neels Hofmeyrf2bf8dc2019-10-29 17:39:56 +0100380 /* Register the planned notification in ep->background_notify so we also catch any osmo_mgcpc_ep_cancel_notify()
381 * that might be triggered between clearing the ci and actually dispatching the event. */
382 llist_add(&notify.entry, &ep->background_notify);
Neels Hofmeyr3ff71282019-10-29 17:41:20 +0100383
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100384 *ci = (struct osmo_mgcpc_ep_ci){
385 .ep = ci->ep,
386 };
387
Neels Hofmeyrcc0b97e2019-10-01 19:44:10 +0200388 /* An MGCP failure typically means the endpoint becomes unusable, cancel all pending request (except DLCX).
389 * Particularly, if two CRCX were scheduled and the first fails, we must no longer dispatch the second CRCX. */
390 for (i = 0; i < ARRAY_SIZE(ep->ci); i++) {
391 struct osmo_mgcpc_ep_ci *other_ci = &ep->ci[i];
392 if (other_ci == ci)
393 continue;
394 if (!other_ci->occupied)
395 continue;
396 if (!other_ci->pending)
397 continue;
398 if (other_ci->sent)
399 continue;
400 if (other_ci->verb == MGCP_VERB_DLCX)
401 continue;
402 /* Just clear the pending request, don't fire more events than below. */
403 other_ci->pending = false;
404 }
405
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100406 /* If this check has terminated the FSM instance, don't fire any more events to prevent use-after-free problems.
407 * The endpoint FSM does dispatch a term event to its parent, and everything should be cleaned like that. */
Neels Hofmeyrf2bf8dc2019-10-29 17:39:56 +0100408 if (!osmo_mgcpc_ep_fsm_check_state_chg_after_response(ep->fi)) {
409 /* The ep has deallocated, no need to llist_del(&notify.entry) here. */
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100410 return;
Neels Hofmeyrf2bf8dc2019-10-29 17:39:56 +0100411 }
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100412
Neels Hofmeyr3ff71282019-10-29 17:41:20 +0100413 if (notify.fi)
414 osmo_fsm_inst_dispatch(notify.fi, notify.failure, notify.data);
Neels Hofmeyrf2bf8dc2019-10-29 17:39:56 +0100415
416 llist_del(&notify.entry);
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100417}
418
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200419static int update_endpoint_name(struct osmo_mgcpc_ep_ci *ci, const char *new_endpoint_name)
420{
421 struct osmo_mgcpc_ep *ep = ci->ep;
422 int rc;
423 int i;
424
425 if (!strcmp(ep->endpoint, new_endpoint_name)) {
426 /* Same endpoint name, nothing to do. */
427 return 0;
428 }
429
430 /* The endpoint name should only change on the very first CRCX response. */
431 if (ep->first_crcx_complete) {
432 LOG_CI(ci, LOGL_ERROR, "Reponse returned mismatching endpoint name."
433 " This is endpoint %s, instead received %s\n",
434 ep->endpoint, new_endpoint_name);
435 on_failure(ci);
436 return -EINVAL;
437 }
438
439 /* This is the first CRCX response, update endpoint name. */
440 rc = OSMO_STRLCPY_ARRAY(ep->endpoint, new_endpoint_name);
441 if (rc <= 0 || rc >= sizeof(ep->endpoint)) {
442 LOG_CI(ci, LOGL_ERROR, "Unable to copy endpoint name %s\n", osmo_quote_str(new_endpoint_name, -1));
443 osmo_mgcpc_ep_ci_dlcx(ci);
444 on_failure(ci);
445 return -ENOSPC;
446 }
447
448 /* Make sure already pending requests use this updated endpoint name. */
449 for (i = 0; i < ARRAY_SIZE(ep->ci); i++) {
450 struct osmo_mgcpc_ep_ci *other_ci = &ep->ci[i];
451 if (!other_ci->occupied)
452 continue;
453 if (!other_ci->pending)
454 continue;
455 if (other_ci->sent)
456 continue;
457 OSMO_STRLCPY_ARRAY(other_ci->verb_info.endpoint, ep->endpoint);
458 }
459 return 0;
460}
461
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100462static void on_success(struct osmo_mgcpc_ep_ci *ci, void *data)
463{
464 struct mgcp_conn_peer *rtp_info;
465
466 if (!ci->occupied)
467 return;
468
469 ci->pending = false;
470
471 switch (ci->verb) {
472 case MGCP_VERB_CRCX:
473 /* If we sent a wildcarded endpoint name on CRCX, we need to store the resulting endpoint
474 * name here. Also, we receive the MGW's RTP port information. */
475 rtp_info = data;
476 OSMO_ASSERT(rtp_info);
477 ci->got_port_info = true;
478 ci->rtp_info = *rtp_info;
479 osmo_strlcpy(ci->mgcp_ci_str, mgcp_conn_get_ci(ci->mgcp_client_fi),
480 sizeof(ci->mgcp_ci_str));
481 if (rtp_info->endpoint[0]) {
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200482 /* On errors, this instance might already be deallocated. Make sure to not access anything after
483 * error. */
484 if (update_endpoint_name(ci, rtp_info->endpoint))
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100485 return;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100486 }
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200487 ci->ep->first_crcx_complete = true;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100488 break;
489
490 default:
491 break;
492 }
493
494 LOG_CI(ci, LOGL_DEBUG, "received successful response to %s: RTP=%s%s\n",
495 osmo_mgcp_verb_name(ci->verb),
496 mgcp_conn_peer_name(ci->got_port_info? &ci->rtp_info : NULL),
Neels Hofmeyr3ff71282019-10-29 17:41:20 +0100497 ci->notify.fi ? "" : " (not sending a notification)");
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100498
Neels Hofmeyr3ff71282019-10-29 17:41:20 +0100499 if (ci->notify.fi)
500 osmo_fsm_inst_dispatch(ci->notify.fi, ci->notify.success, ci->notify.data);
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100501
502 osmo_mgcpc_ep_fsm_check_state_chg_after_response(ci->ep->fi);
503}
504
505/*! Return the MGW's RTP port information for this connection, as returned by the last CRCX/MDCX OK message. */
506const struct mgcp_conn_peer *osmo_mgcpc_ep_ci_get_rtp_info(const struct osmo_mgcpc_ep_ci *ci)
507{
508 ci = osmo_mgcpc_ep_check_ci((struct osmo_mgcpc_ep_ci*)ci);
509 if (!ci)
510 return NULL;
511 if (!ci->got_port_info)
512 return NULL;
513 return &ci->rtp_info;
514}
515
516/*! Return the MGW's RTP port information for this connection, as returned by the last CRCX/MDCX OK message. */
517bool osmo_mgcpc_ep_ci_get_crcx_info_to_sockaddr(const struct osmo_mgcpc_ep_ci *ci, struct sockaddr_storage *dest)
518{
519 const struct mgcp_conn_peer *rtp_info;
520 struct sockaddr_in *sin;
521
522 rtp_info = osmo_mgcpc_ep_ci_get_rtp_info(ci);
523 if (!rtp_info)
524 return false;
525
526 sin = (struct sockaddr_in *)dest;
527
528 sin->sin_family = AF_INET;
529 sin->sin_addr.s_addr = inet_addr(rtp_info->addr);
530 sin->sin_port = osmo_ntohs(rtp_info->port);
531 return true;
532}
533
Pau Espin Pedrol30907dc2019-05-06 11:54:17 +0200534bool osmo_mgcpc_ep_ci_get_crcx_info_to_osmux_cid(const struct osmo_mgcpc_ep_ci *ci, uint8_t* cid)
535{
536 const struct mgcp_conn_peer *rtp_info;
537
538 rtp_info = osmo_mgcpc_ep_ci_get_rtp_info(ci);
539 if (!rtp_info)
540 return false;
541
542 if (!rtp_info->x_osmo_osmux_use)
543 return false;
544
545 *cid = rtp_info->x_osmo_osmux_cid;
546 return true;
547}
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100548
549static const struct osmo_tdef_state_timeout osmo_mgcpc_ep_fsm_timeouts[32] = {
550 [OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE] = { .T=2427001 },
551};
552
553/* Transition to a state, using the T timer defined in assignment_fsm_timeouts.
554 * The actual timeout value is in turn obtained from osmo_mgcpc_ep.T_defs.
555 * Assumes local variable fi exists. */
556#define osmo_mgcpc_ep_fsm_state_chg(state) \
557 osmo_tdef_fsm_inst_state_chg(fi, state, osmo_mgcpc_ep_fsm_timeouts, \
558 ((struct osmo_mgcpc_ep*)fi->priv)->T_defs, 5)
559
560/*! Dispatch an actual CRCX/MDCX/DLCX message for this connection.
Neels Hofmeyrf2bf8dc2019-10-29 17:39:56 +0100561 *
562 * If the 'notify' instance deallocates before it received a notification of event_success or event_failure,
563 * osmo_mgcpc_ep_ci_cancel_notify() or osmo_mgcpc_ep_cancel_notify() must be called. It is not harmful to cancel
564 * notification after an event has been received.
565 *
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100566 * \param ci Connection identifier as obtained from osmo_mgcpc_ep_ci_add().
567 * \param verb MGCP operation to dispatch.
568 * \param verb_info Parameters for the MGCP operation.
569 * \param notify Peer FSM instance to notify of completed/failed operation.
570 * \param event_success Which event to dispatch to 'notify' upon OK response.
571 * \param event_failure Which event to dispatch to 'notify' upon failure response.
572 * \param notify_data Data pointer to pass to the event dispatch for both success and failure.
573 */
574void osmo_mgcpc_ep_ci_request(struct osmo_mgcpc_ep_ci *ci,
575 enum mgcp_verb verb, const struct mgcp_conn_peer *verb_info,
576 struct osmo_fsm_inst *notify,
577 uint32_t event_success, uint32_t event_failure,
578 void *notify_data)
579{
580 struct osmo_mgcpc_ep *ep;
581 struct osmo_fsm_inst *fi;
582 struct osmo_mgcpc_ep_ci cleared_ci;
583 ci = osmo_mgcpc_ep_check_ci(ci);
584
585 if (!ci) {
586 LOGP(DLGLOBAL, LOGL_ERROR, "Invalid MGW endpoint request: no ci\n");
587 goto dispatch_error;
588 }
589 if (!verb_info && verb != MGCP_VERB_DLCX) {
590 LOG_CI(ci, LOGL_ERROR, "Invalid MGW endpoint request: missing verb details for %s\n",
591 osmo_mgcp_verb_name(verb));
592 goto dispatch_error;
593 }
594 if ((verb < 0) || (verb > MGCP_VERB_RSIP)) {
595 LOG_CI(ci, LOGL_ERROR, "Invalid MGW endpoint request: unknown verb: %s\n",
596 osmo_mgcp_verb_name(verb));
597 goto dispatch_error;
598 }
599
600 ep = ci->ep;
601 fi = ep->fi;
602
603 /* Clear volatile state by explicitly keeping those that should remain. Because we can't assign
604 * the char[] directly, dance through cleared_ci and copy back. */
605 cleared_ci = (struct osmo_mgcpc_ep_ci){
606 .ep = ep,
607 .mgcp_client_fi = ci->mgcp_client_fi,
608 .got_port_info = ci->got_port_info,
609 .rtp_info = ci->rtp_info,
610
611 .occupied = true,
612 /* .pending = true follows below */
613 .verb = verb,
Neels Hofmeyr3ff71282019-10-29 17:41:20 +0100614 .notify = {
615 .fi = notify,
616 .success = event_success,
617 .failure = event_failure,
618 .data = notify_data,
619 }
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100620 };
621 osmo_strlcpy(cleared_ci.label, ci->label, sizeof(cleared_ci.label));
622 osmo_strlcpy(cleared_ci.mgcp_ci_str, ci->mgcp_ci_str, sizeof(cleared_ci.mgcp_ci_str));
623 *ci = cleared_ci;
624
Neels Hofmeyr3ff71282019-10-29 17:41:20 +0100625 LOG_CI_VERB(ci, LOGL_DEBUG, "notify=%s\n", osmo_fsm_inst_name(ci->notify.fi));
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100626
627 if (verb_info)
628 ci->verb_info = *verb_info;
629
630 if (ep->endpoint[0]) {
631 if (ci->verb_info.endpoint[0] && strcmp(ci->verb_info.endpoint, ep->endpoint))
632 LOG_CI(ci, LOGL_ERROR,
633 "Warning: Requested %s on endpoint %s, but this CI is on endpoint %s."
634 " Using the proper endpoint instead.\n",
635 osmo_mgcp_verb_name(verb), ci->verb_info.endpoint, ep->endpoint);
636 osmo_strlcpy(ci->verb_info.endpoint, ep->endpoint, sizeof(ci->verb_info.endpoint));
637 }
638
639 switch (ci->verb) {
640 case MGCP_VERB_CRCX:
641 if (ci->mgcp_client_fi) {
642 LOG_CI(ci, LOGL_ERROR, "CRCX can be called only once per MGW endpoint CI\n");
643 on_failure(ci);
644 return;
645 }
646 break;
647
648 case MGCP_VERB_MDCX:
649 if (!ci->mgcp_client_fi) {
650 LOG_CI_VERB(ci, LOGL_ERROR, "The first verb on an unused MGW endpoint CI must be CRCX, not %s\n",
651 osmo_mgcp_verb_name(ci->verb));
652 on_failure(ci);
653 return;
654 }
655 break;
656
657 case MGCP_VERB_DLCX:
658 if (!ci->mgcp_client_fi) {
659 LOG_CI_VERB(ci, LOGL_DEBUG, "Ignoring DLCX on unused MGW endpoint CI\n");
660 return;
661 }
662 break;
663
664 default:
665 LOG_CI(ci, LOGL_ERROR, "This verb is not supported: %s\n", osmo_mgcp_verb_name(ci->verb));
666 on_failure(ci);
667 return;
668 }
669
670 ci->pending = true;
671
672 LOG_CI_VERB(ci, LOGL_DEBUG, "Scheduling\n");
673
674 if (ep->fi->state != OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE)
675 osmo_mgcpc_ep_fsm_state_chg(OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE);
676
677 return;
678dispatch_error:
679 if (notify)
680 osmo_fsm_inst_dispatch(notify, event_failure, notify_data);
681}
682
Neels Hofmeyrf2bf8dc2019-10-29 17:39:56 +0100683/*! No longer notify for any state changes for any conns of this endpoint.
684 * Useful if the notify instance passed to osmo_mgcpc_ep_ci_request() is about to deallocate.
685 * \param ep The endpoint FSM instance.
686 * \param notify Which target to cancel notification for, if NULL cancel all notifications. */
687void osmo_mgcpc_ep_cancel_notify(struct osmo_mgcpc_ep *ep, struct osmo_fsm_inst *notify)
688{
689 struct fsm_notify *n;
690 int i;
691 for (i = 0; i < ARRAY_SIZE(ep->ci); i++) {
692 struct osmo_mgcpc_ep_ci *ci = &ep->ci[i];
693 if (!notify || ci->notify.fi == notify)
694 ci->notify.fi = NULL;
695 }
696 llist_for_each_entry(n, &ep->background_notify, entry) {
697 if (!notify || n->fi == notify)
698 n->fi = NULL;
699 }
700
701}
702
Neels Hofmeyr923d60b2019-10-29 17:40:08 +0100703/* Return the osmo_mgcpc_ep that this conn belongs to. */
704struct osmo_mgcpc_ep *osmo_mgcpc_ep_ci_ep(struct osmo_mgcpc_ep_ci *conn)
705{
706 if (!conn)
707 return NULL;
708 return conn->ep;
709}
710
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100711static int send_verb(struct osmo_mgcpc_ep_ci *ci)
712{
713 int rc;
714 struct osmo_mgcpc_ep *ep = ci->ep;
Neels Hofmeyr055ded72019-10-29 17:46:30 +0100715 struct fsm_notify notify;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100716
717 if (!ci->occupied || !ci->pending || ci->sent)
718 return 0;
719
720 switch (ci->verb) {
721
722 case MGCP_VERB_CRCX:
723 OSMO_ASSERT(!ci->mgcp_client_fi);
724 LOG_CI_VERB(ci, LOGL_DEBUG, "Sending\n");
725 ci->mgcp_client_fi = mgcp_conn_create(ep->mgcp_client, ep->fi,
726 CI_EV_FAILURE(ci), CI_EV_SUCCESS(ci),
727 &ci->verb_info);
728 ci->sent = true;
729 if (!ci->mgcp_client_fi){
730 LOG_CI_VERB(ci, LOGL_ERROR, "Cannot send\n");
731 on_failure(ci);
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200732 return -EINVAL;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100733 }
734 osmo_fsm_inst_update_id(ci->mgcp_client_fi, ci->label);
735 break;
736
737 case MGCP_VERB_MDCX:
738 OSMO_ASSERT(ci->mgcp_client_fi);
739 LOG_CI_VERB(ci, LOGL_DEBUG, "Sending\n");
740 rc = mgcp_conn_modify(ci->mgcp_client_fi, CI_EV_SUCCESS(ci), &ci->verb_info);
741 ci->sent = true;
742 if (rc) {
743 LOG_CI_VERB(ci, LOGL_ERROR, "Cannot send (rc=%d %s)\n", rc, strerror(-rc));
744 on_failure(ci);
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200745 return -EINVAL;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100746 }
747 break;
748
749 case MGCP_VERB_DLCX:
750 LOG_CI(ci, LOGL_DEBUG, "Sending MGCP: %s %s\n",
751 osmo_mgcp_verb_name(ci->verb), ci->mgcp_ci_str);
752 /* The way this is designed, we actually need to forget all about the ci right away. */
753 mgcp_conn_delete(ci->mgcp_client_fi);
Neels Hofmeyr055ded72019-10-29 17:46:30 +0100754 notify = ci->notify;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100755 *ci = (struct osmo_mgcpc_ep_ci){
756 .ep = ep,
757 };
Neels Hofmeyr055ded72019-10-29 17:46:30 +0100758 /* When dispatching an event for this CI, the user may decide to trigger the next request for this conn
759 * right away. So we must be ready with a cleared *ci. */
760 if (notify.fi)
761 osmo_fsm_inst_dispatch(notify.fi, notify.success, notify.data);
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100762 break;
763
764 default:
765 OSMO_ASSERT(false);
766 }
767
768 return 1;
769}
770
771/*! DLCX all connections, terminate the endpoint FSM and free. */
772void osmo_mgcpc_ep_clear(struct osmo_mgcpc_ep *ep)
773{
774 if (!ep)
775 return;
Neels Hofmeyrf2bf8dc2019-10-29 17:39:56 +0100776 osmo_mgcpc_ep_cancel_notify(ep, NULL);
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100777 osmo_fsm_inst_term(ep->fi, OSMO_FSM_TERM_REGULAR, 0);
778}
779
780static void osmo_mgcpc_ep_count(struct osmo_mgcpc_ep *ep, int *occupied, int *pending_not_sent,
781 int *waiting_for_response)
782{
783 int i;
784
785 if (occupied)
786 *occupied = 0;
787
788 if (pending_not_sent)
789 *pending_not_sent = 0;
790
791 if (waiting_for_response)
792 *waiting_for_response = 0;
793
794 for (i = 0; i < ARRAY_SIZE(ep->ci); i++) {
795 struct osmo_mgcpc_ep_ci *ci = &ep->ci[i];
796 if (ci->occupied) {
797 if (occupied)
798 (*occupied)++;
799 } else
800 continue;
801
802 if (ci->pending)
803 LOG_CI_VERB(ci, LOGL_DEBUG, "%s\n",
804 ci->sent ? "waiting for response" : "waiting to be sent");
805 else
806 LOG_CI_VERB(ci, LOGL_DEBUG, "done (%s)\n", mgcp_conn_peer_name(osmo_mgcpc_ep_ci_get_rtp_info(ci)));
807
808 if (ci->pending && ci->sent)
809 if (waiting_for_response)
810 (*waiting_for_response)++;
811 if (ci->pending && !ci->sent)
812 if (pending_not_sent)
813 (*pending_not_sent)++;
814 }
815}
816
817static bool osmo_mgcpc_ep_fsm_check_state_chg_after_response(struct osmo_fsm_inst *fi)
818{
819 int waiting_for_response;
820 int occupied;
821 struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi);
822
823 osmo_mgcpc_ep_count(ep, &occupied, NULL, &waiting_for_response);
824 LOG_MGCPC_EP(ep, LOGL_DEBUG, "CI in use: %d, waiting for response: %d\n", occupied, waiting_for_response);
825
826 if (!occupied) {
827 /* All CI have been released. The endpoint no longer exists. Notify the parent FSM, by
828 * terminating. */
829 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, 0);
830 return false;
831 }
832
833 if (!waiting_for_response) {
834 if (fi->state != OSMO_MGCPC_EP_ST_IN_USE)
835 osmo_mgcpc_ep_fsm_state_chg(OSMO_MGCPC_EP_ST_IN_USE);
836 }
837
838 return true;
839}
840
841static void osmo_mgcpc_ep_fsm_wait_mgw_response_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
842{
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200843 static int re_enter = 0;
844 int rc;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100845 int count = 0;
846 int i;
847 struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi);
848
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200849 re_enter++;
850 OSMO_ASSERT(re_enter < 10);
851
852 /* The first CRCX gives us the endpoint name in the CRCX response. So we must wait for the first CRCX endpoint
853 * response to come in before sending any other MGCP requests -- otherwise we might end up creating new
854 * endpoints instead of acting on the same. This FSM always sends out N requests and waits for all of them to
855 * complete before sending out new requests. Hence we're safe when the very first time at most one request is
856 * sent (which needs to be a CRCX). */
857
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100858 for (i = 0; i < ARRAY_SIZE(ep->ci); i++) {
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200859 struct osmo_mgcpc_ep_ci *ci = &ep->ci[i];
860
861 /* Make sure that only CRCX get dispatched if no CRCX were sent yet. */
862 if (!ep->first_crcx_complete) {
863 if (ci->occupied && ci->verb != MGCP_VERB_CRCX)
864 continue;
865 }
866
867 rc = send_verb(&ep->ci[i]);
868 /* Need to be careful not to access the instance after failure. Event chains may already have
869 * deallocated this memory. */
870 if (rc < 0)
871 return;
872 if (!rc)
873 continue;
874 count++;
875 /* Make sure that we wait for the first CRCX response before dispatching more requests. */
876 if (!ep->first_crcx_complete)
877 break;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100878 }
879
880 LOG_MGCPC_EP(ep, LOGL_DEBUG, "Sent messages: %d\n", count);
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200881 if (ep->first_crcx_complete)
882 osmo_mgcpc_ep_fsm_check_state_chg_after_response(fi);
883 re_enter--;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100884}
885
886static void osmo_mgcpc_ep_fsm_handle_ci_events(struct osmo_fsm_inst *fi, uint32_t event, void *data)
887{
888 struct osmo_mgcpc_ep_ci *ci;
889 struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi);
890 ci = osmo_mgcpc_ep_ci_for_event(ep, event);
891 if (ci) {
892 if (event == CI_EV_SUCCESS(ci))
893 on_success(ci, data);
894 else
895 on_failure(ci);
896 }
897}
898
899static void osmo_mgcpc_ep_fsm_in_use_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
900{
901 int pending_not_sent;
902 struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi);
903
904 osmo_mgcpc_ep_count(ep, NULL, &pending_not_sent, NULL);
905 if (pending_not_sent)
906 osmo_mgcpc_ep_fsm_state_chg(OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE);
907}
908
909#define S(x) (1 << (x))
910
911static const struct osmo_fsm_state osmo_mgcpc_ep_fsm_states[] = {
912 [OSMO_MGCPC_EP_ST_UNUSED] = {
913 .name = "UNUSED",
914 .in_event_mask = 0,
915 .out_state_mask = 0
916 | S(OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE)
917 ,
918 },
919 [OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE] = {
920 .name = "WAIT_MGW_RESPONSE",
921 .onenter = osmo_mgcpc_ep_fsm_wait_mgw_response_onenter,
922 .action = osmo_mgcpc_ep_fsm_handle_ci_events,
923 .in_event_mask = 0xffffffff,
924 .out_state_mask = 0
925 | S(OSMO_MGCPC_EP_ST_IN_USE)
926 ,
927 },
928 [OSMO_MGCPC_EP_ST_IN_USE] = {
929 .name = "IN_USE",
930 .onenter = osmo_mgcpc_ep_fsm_in_use_onenter,
931 .action = osmo_mgcpc_ep_fsm_handle_ci_events,
932 .in_event_mask = 0xffffffff, /* mgcp_client_fsm may send parent term anytime */
933 .out_state_mask = 0
934 | S(OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE)
935 ,
936 },
937};
938
939static int osmo_mgcpc_ep_fsm_timer_cb(struct osmo_fsm_inst *fi)
940{
941 int i;
942 struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi);
943
944 switch (fi->T) {
945 default:
946 for (i = 0; i < ARRAY_SIZE(ep->ci); i++) {
947 struct osmo_mgcpc_ep_ci *ci = &ep->ci[i];
948 if (!ci->occupied)
949 continue;
950 if (!(ci->pending && ci->sent))
951 continue;
952 on_failure(ci);
953 }
954 return 0;
955 }
956
957 return 0;
958}
959
960static struct osmo_fsm osmo_mgcpc_ep_fsm = {
Pau Espin Pedrol182ca3b2019-05-08 14:01:20 +0200961 .name = "mgw-endp",
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100962 .states = osmo_mgcpc_ep_fsm_states,
963 .num_states = ARRAY_SIZE(osmo_mgcpc_ep_fsm_states),
964 .log_subsys = DLMGCP,
965 .event_names = osmo_mgcpc_ep_fsm_event_names,
966 .timer_cb = osmo_mgcpc_ep_fsm_timer_cb,
967 /* The FSM termination will automatically trigger any mgcp_client_fsm instances to DLCX. */
968};