blob: 420fc0f5b0755abf280e9b0798937eed8042b335 [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
76/*! One connection on an endpoint, corresponding to a connection identifier (CI) as returned by the MGW.
77 * An endpoint has a fixed number of slots of these, which may or may not be in use.
78 */
79struct osmo_mgcpc_ep_ci {
80 struct osmo_mgcpc_ep *ep;
81
82 bool occupied;
83 char label[64];
84 struct osmo_fsm_inst *mgcp_client_fi;
85
86 bool pending;
87 bool sent;
88 enum mgcp_verb verb;
89 struct mgcp_conn_peer verb_info;
90 struct osmo_fsm_inst *notify;
91 uint32_t notify_success;
92 uint32_t notify_failure;
93 void *notify_data;
94
95 bool got_port_info;
96 struct mgcp_conn_peer rtp_info;
97 char mgcp_ci_str[MGCP_CONN_ID_LENGTH];
98};
99
100/*! An MGW endpoint with N connections, like "rtpbridge/23@mgw". */
101struct osmo_mgcpc_ep {
102 /*! MGCP client connection to the MGW. */
103 struct mgcp_client *mgcp_client;
104 struct osmo_fsm_inst *fi;
105
106 /*! Endpoint string; at first this might be a wildcard, and upon the first CRCX OK response, this will reflect
107 * the endpoint name returned by the MGW. */
108 char endpoint[MGCP_ENDPOINT_MAXLEN];
109
110 /*! Timeout definitions used for this endpoint, see osmo_mgcpc_ep_fsm_timeouts. */
111 const struct osmo_tdef *T_defs;
112
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200113 /*! True as soon as the first CRCX OK is received. The endpoint name may be determined by the first CRCX
114 * response, so to dispatch any other messages, the FSM instance *must* wait for the first CRCX OK to arrive
115 * first. Once the endpoint name is pinpointed, any amount of operations may be dispatched concurrently. */
116 bool first_crcx_complete;
117
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100118 /*! Endpoint connection slots. Note that each connection has its own set of FSM event numbers to signal success
119 * and failure, depending on its index within this array. See CI_EV_SUCCESS and CI_EV_FAILURE. */
120 struct osmo_mgcpc_ep_ci ci[USABLE_CI];
121};
122
123const struct value_string osmo_mgcp_verb_names[] = {
124 { MGCP_VERB_CRCX, "CRCX" },
125 { MGCP_VERB_MDCX, "MDCX" },
126 { MGCP_VERB_DLCX, "DLCX" },
127 { MGCP_VERB_AUEP, "AUEP" },
128 { MGCP_VERB_RSIP, "RSIP" },
129 {}
130};
131
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200132static void osmo_mgcpc_ep_count(struct osmo_mgcpc_ep *ep, int *occupied, int *pending_not_sent,
133 int *waiting_for_response);
134
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100135static struct osmo_mgcpc_ep_ci *osmo_mgcpc_ep_check_ci(struct osmo_mgcpc_ep_ci *ci)
136{
137 if (!ci)
138 return NULL;
139 if (!ci->ep)
140 return NULL;
141 if (ci < ci->ep->ci || ci >= &ci->ep->ci[USABLE_CI])
142 return NULL;
143 return ci;
144}
145
146static struct osmo_mgcpc_ep_ci *osmo_mgcpc_ep_ci_for_event(struct osmo_mgcpc_ep *ep, uint32_t event)
147{
148 int idx;
149 if (event < FIRST_CI_EVENT)
150 return NULL;
151 idx = EV_TO_CI_IDX(event);
152 if (idx >= sizeof(ep->ci))
153 return NULL;
154 return osmo_mgcpc_ep_check_ci(&ep->ci[idx]);
155}
156
157const char *osmo_mgcpc_ep_name(const struct osmo_mgcpc_ep *ep)
158{
159 if (!ep)
160 return "NULL";
161 if (ep->endpoint[0])
162 return ep->endpoint;
163 return osmo_fsm_inst_name(ep->fi);
164}
165
166const char *mgcp_conn_peer_name(const struct mgcp_conn_peer *info)
167{
168 /* I'd be fine with a smaller buffer and accept truncation, but gcc possibly refuses to build if
169 * this buffer is too small. */
170 static char buf[1024];
171
172 if (!info)
173 return "NULL";
174
175 if (info->endpoint[0]
176 && info->addr[0])
177 snprintf(buf, sizeof(buf), "%s:%s:%u",
178 info->endpoint, info->addr, info->port);
179 else if (info->endpoint[0])
180 snprintf(buf, sizeof(buf), "%s", info->endpoint);
181 else if (info->addr[0])
182 snprintf(buf, sizeof(buf), "%s:%u", info->addr, info->port);
183 else
184 return "empty";
185 return buf;
186}
187
188const char *osmo_mgcpc_ep_ci_name(const struct osmo_mgcpc_ep_ci *ci)
189{
190 const struct mgcp_conn_peer *rtp_info;
191
192 if (!ci)
193 return "NULL";
194
195 rtp_info = osmo_mgcpc_ep_ci_get_rtp_info(ci);
196
197 if (rtp_info)
198 return mgcp_conn_peer_name(rtp_info);
199 return osmo_mgcpc_ep_name(ci->ep);
200}
201
202const char *osmo_mgcpc_ep_ci_id(const struct osmo_mgcpc_ep_ci *ci)
203{
204 if (!ci || !ci->mgcp_ci_str[0])
205 return NULL;
206 return ci->mgcp_ci_str;
207}
208
209static struct value_string osmo_mgcpc_ep_fsm_event_names[33] = {};
210
211static char osmo_mgcpc_ep_fsm_event_name_bufs[32][32] = {};
212
213static void fill_event_names()
214{
215 int i;
216 for (i = 0; i < (ARRAY_SIZE(osmo_mgcpc_ep_fsm_event_names) - 1); i++) {
217 if (i < _OSMO_MGCPC_EP_EV_LAST)
218 continue;
219 if (i < FIRST_CI_EVENT || EV_TO_CI_IDX(i) > USABLE_CI) {
220 osmo_mgcpc_ep_fsm_event_names[i] = (struct value_string){i, "Unused"};
221 continue;
222 }
223 snprintf(osmo_mgcpc_ep_fsm_event_name_bufs[i], sizeof(osmo_mgcpc_ep_fsm_event_name_bufs[i]),
224 "MGW Response for CI #%d", EV_TO_CI_IDX(i));
225 osmo_mgcpc_ep_fsm_event_names[i] = (struct value_string){i, osmo_mgcpc_ep_fsm_event_name_bufs[i]};
226 }
227}
228
229/* T_defs is used to obtain an (Osmocom specific) T2427001: timeout for an MGCP response (note, 2427 corresponds to the
230 * default MGCP port in osmo-mgw). */
231static __attribute__((constructor)) void osmo_mgcpc_ep_fsm_init()
232{
233 OSMO_ASSERT(osmo_fsm_register(&osmo_mgcpc_ep_fsm) == 0);
234 fill_event_names();
235}
236
237struct osmo_mgcpc_ep *osmo_mgcpc_ep_fi_mgwep(struct osmo_fsm_inst *fi)
238{
239 OSMO_ASSERT(fi);
240 OSMO_ASSERT(fi->fsm == &osmo_mgcpc_ep_fsm);
241 OSMO_ASSERT(fi->priv);
242 return fi->priv;
243}
244
245/*! Allocate an osmo_mgcpc_ep FSM.
246 * MGCP messages to set up the endpoint will be sent on the given mgcp_client, as soon as the first
247 * osmo_mgcpc_ep_ci_request() is invoked.
248 *
249 * A typical sequence of events would be:
250 *
251 * ep = osmo_mgcpc_ep_alloc(..., mgcp_client_rtpbridge_wildcard(client));
252 * ci_to_ran = osmo_mgcpc_ep_ci_add(ep);
253 * osmo_mgcpc_ep_ci_request(ci_to_ran, MGCP_VERB_CRCX, verb_info,
254 * my_call_fsm, MY_EVENT_MGCP_OK, MY_EVENT_MGCP_FAIL);
255 * ci_to_cn = osmo_mgcpc_ep_ci_add(ep);
256 * osmo_mgcpc_ep_ci_request(ci_to_cn, MGCP_VERB_CRCX, verb_info,
257 * my_call_fsm, MY_EVENT_MGCP_OK, MY_EVENT_MGCP_FAIL);
258 * ...
259 * osmo_mgcpc_ep_ci_request(ci_to_ran, MGCP_VERB_MDCX, ...);
260 * ...
261 * osmo_mgcpc_ep_clear(ep);
262 * ep = NULL;
263 *
264 * \param parent Parent FSM.
265 * \param parent_term_event Event to dispatch to the parent on termination of this FSM instance.
266 * \param mgcp_client Connection to the MGW.
267 * \param T_defs Timeout definitions to be used for FSM states, see osmo_mgcpc_ep_fsm_timeouts.
268 * \param fsm_id FSM instance ID.
269 * \param endpoint_str_fmt The endpoint string format to send to the MGW upon the first CRCX.
270 * See mgcp_client_rtpbridge_wildcard() for "rtpbridge" endpoints.
271 */
272struct osmo_mgcpc_ep *osmo_mgcpc_ep_alloc(struct osmo_fsm_inst *parent, uint32_t parent_term_event,
273 struct mgcp_client *mgcp_client,
274 const struct osmo_tdef *T_defs,
275 const char *fsm_id,
276 const char *endpoint_str_fmt, ...)
277{
278 va_list ap;
279 struct osmo_fsm_inst *fi;
280 struct osmo_mgcpc_ep *ep;
281 int rc;
282
283 if (!mgcp_client)
284 return NULL;
285
286 fi = osmo_fsm_inst_alloc_child(&osmo_mgcpc_ep_fsm, parent, parent_term_event);
287 OSMO_ASSERT(fi);
288
289 osmo_fsm_inst_update_id(fi, fsm_id);
290
291 ep = talloc_zero(fi, struct osmo_mgcpc_ep);
292 OSMO_ASSERT(ep);
293
294 *ep = (struct osmo_mgcpc_ep){
295 .mgcp_client = mgcp_client,
296 .fi = fi,
297 .T_defs = T_defs,
298 };
299 fi->priv = ep;
300
301 va_start(ap, endpoint_str_fmt);
302 rc = vsnprintf(ep->endpoint, sizeof(ep->endpoint), endpoint_str_fmt ? : "", ap);
303 va_end(ap);
304
305 if (rc <= 0 || rc >= sizeof(ep->endpoint)) {
306 LOG_MGCPC_EP(ep, LOGL_ERROR, "Endpoint name too long or too short: %s\n",
307 ep->endpoint);
308 osmo_fsm_inst_term(ep->fi, OSMO_FSM_TERM_ERROR, 0);
309 return NULL;
310 }
311
312 return ep;
313}
314
315/*! Add a connection to an endpoint.
316 * Allocate a connection identifier slot in the osmo_mgcpc_ep instance, do not yet dispatch a CRCX.
317 * The CRCX is dispatched only upon the first osmo_mgcpc_ep_ci_request().
318 * \param ep Parent endpoint instance.
319 * \param label_fmt Label for logging.
320 */
321struct osmo_mgcpc_ep_ci *osmo_mgcpc_ep_ci_add(struct osmo_mgcpc_ep *ep,
322 const char *label_fmt, ...)
323{
324 va_list ap;
325 int i;
326 struct osmo_mgcpc_ep_ci *ci;
327
328 for (i = 0; i < USABLE_CI; i++) {
329 ci = &ep->ci[i];
330
331 if (ci->occupied || ci->mgcp_client_fi)
332 continue;
333
334 *ci = (struct osmo_mgcpc_ep_ci){
335 .ep = ep,
336 .occupied = true,
337 };
338 if (label_fmt) {
339 va_start(ap, label_fmt);
340 vsnprintf(ci->label, sizeof(ci->label), label_fmt, ap);
341 va_end(ap);
342 }
343 return ci;
344 }
345
346 LOG_MGCPC_EP(ep, LOGL_ERROR,
347 "Cannot allocate another endpoint, all "
348 OSMO_STRINGIFY_VAL(USABLE_CI) " are in use\n");
349
350 return NULL;
351}
352
353static bool osmo_mgcpc_ep_fsm_check_state_chg_after_response(struct osmo_fsm_inst *fi);
354
355static void on_failure(struct osmo_mgcpc_ep_ci *ci)
356{
357 struct osmo_fsm_inst *notify = ci->notify;
358 uint32_t notify_failure = ci->notify_failure;
359 void *notify_data = ci->notify_data;
360
361 if (!ci->occupied)
362 return;
363
364 *ci = (struct osmo_mgcpc_ep_ci){
365 .ep = ci->ep,
366 };
367
368 /* If this check has terminated the FSM instance, don't fire any more events to prevent use-after-free problems.
369 * The endpoint FSM does dispatch a term event to its parent, and everything should be cleaned like that. */
370 if (!osmo_mgcpc_ep_fsm_check_state_chg_after_response(ci->ep->fi))
371 return;
372
373 if (notify)
374 osmo_fsm_inst_dispatch(notify, notify_failure, notify_data);
375}
376
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200377static int update_endpoint_name(struct osmo_mgcpc_ep_ci *ci, const char *new_endpoint_name)
378{
379 struct osmo_mgcpc_ep *ep = ci->ep;
380 int rc;
381 int i;
382
383 if (!strcmp(ep->endpoint, new_endpoint_name)) {
384 /* Same endpoint name, nothing to do. */
385 return 0;
386 }
387
388 /* The endpoint name should only change on the very first CRCX response. */
389 if (ep->first_crcx_complete) {
390 LOG_CI(ci, LOGL_ERROR, "Reponse returned mismatching endpoint name."
391 " This is endpoint %s, instead received %s\n",
392 ep->endpoint, new_endpoint_name);
393 on_failure(ci);
394 return -EINVAL;
395 }
396
397 /* This is the first CRCX response, update endpoint name. */
398 rc = OSMO_STRLCPY_ARRAY(ep->endpoint, new_endpoint_name);
399 if (rc <= 0 || rc >= sizeof(ep->endpoint)) {
400 LOG_CI(ci, LOGL_ERROR, "Unable to copy endpoint name %s\n", osmo_quote_str(new_endpoint_name, -1));
401 osmo_mgcpc_ep_ci_dlcx(ci);
402 on_failure(ci);
403 return -ENOSPC;
404 }
405
406 /* Make sure already pending requests use this updated endpoint name. */
407 for (i = 0; i < ARRAY_SIZE(ep->ci); i++) {
408 struct osmo_mgcpc_ep_ci *other_ci = &ep->ci[i];
409 if (!other_ci->occupied)
410 continue;
411 if (!other_ci->pending)
412 continue;
413 if (other_ci->sent)
414 continue;
415 OSMO_STRLCPY_ARRAY(other_ci->verb_info.endpoint, ep->endpoint);
416 }
417 return 0;
418}
419
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100420static void on_success(struct osmo_mgcpc_ep_ci *ci, void *data)
421{
422 struct mgcp_conn_peer *rtp_info;
423
424 if (!ci->occupied)
425 return;
426
427 ci->pending = false;
428
429 switch (ci->verb) {
430 case MGCP_VERB_CRCX:
431 /* If we sent a wildcarded endpoint name on CRCX, we need to store the resulting endpoint
432 * name here. Also, we receive the MGW's RTP port information. */
433 rtp_info = data;
434 OSMO_ASSERT(rtp_info);
435 ci->got_port_info = true;
436 ci->rtp_info = *rtp_info;
437 osmo_strlcpy(ci->mgcp_ci_str, mgcp_conn_get_ci(ci->mgcp_client_fi),
438 sizeof(ci->mgcp_ci_str));
439 if (rtp_info->endpoint[0]) {
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200440 /* On errors, this instance might already be deallocated. Make sure to not access anything after
441 * error. */
442 if (update_endpoint_name(ci, rtp_info->endpoint))
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100443 return;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100444 }
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200445 ci->ep->first_crcx_complete = true;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100446 break;
447
448 default:
449 break;
450 }
451
452 LOG_CI(ci, LOGL_DEBUG, "received successful response to %s: RTP=%s%s\n",
453 osmo_mgcp_verb_name(ci->verb),
454 mgcp_conn_peer_name(ci->got_port_info? &ci->rtp_info : NULL),
455 ci->notify ? "" : " (not sending a notification)");
456
457 if (ci->notify)
458 osmo_fsm_inst_dispatch(ci->notify, ci->notify_success, ci->notify_data);
459
460 osmo_mgcpc_ep_fsm_check_state_chg_after_response(ci->ep->fi);
461}
462
463/*! Return the MGW's RTP port information for this connection, as returned by the last CRCX/MDCX OK message. */
464const struct mgcp_conn_peer *osmo_mgcpc_ep_ci_get_rtp_info(const struct osmo_mgcpc_ep_ci *ci)
465{
466 ci = osmo_mgcpc_ep_check_ci((struct osmo_mgcpc_ep_ci*)ci);
467 if (!ci)
468 return NULL;
469 if (!ci->got_port_info)
470 return NULL;
471 return &ci->rtp_info;
472}
473
474/*! Return the MGW's RTP port information for this connection, as returned by the last CRCX/MDCX OK message. */
475bool osmo_mgcpc_ep_ci_get_crcx_info_to_sockaddr(const struct osmo_mgcpc_ep_ci *ci, struct sockaddr_storage *dest)
476{
477 const struct mgcp_conn_peer *rtp_info;
478 struct sockaddr_in *sin;
479
480 rtp_info = osmo_mgcpc_ep_ci_get_rtp_info(ci);
481 if (!rtp_info)
482 return false;
483
484 sin = (struct sockaddr_in *)dest;
485
486 sin->sin_family = AF_INET;
487 sin->sin_addr.s_addr = inet_addr(rtp_info->addr);
488 sin->sin_port = osmo_ntohs(rtp_info->port);
489 return true;
490}
491
Pau Espin Pedrol30907dc2019-05-06 11:54:17 +0200492bool osmo_mgcpc_ep_ci_get_crcx_info_to_osmux_cid(const struct osmo_mgcpc_ep_ci *ci, uint8_t* cid)
493{
494 const struct mgcp_conn_peer *rtp_info;
495
496 rtp_info = osmo_mgcpc_ep_ci_get_rtp_info(ci);
497 if (!rtp_info)
498 return false;
499
500 if (!rtp_info->x_osmo_osmux_use)
501 return false;
502
503 *cid = rtp_info->x_osmo_osmux_cid;
504 return true;
505}
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100506
507static const struct osmo_tdef_state_timeout osmo_mgcpc_ep_fsm_timeouts[32] = {
508 [OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE] = { .T=2427001 },
509};
510
511/* Transition to a state, using the T timer defined in assignment_fsm_timeouts.
512 * The actual timeout value is in turn obtained from osmo_mgcpc_ep.T_defs.
513 * Assumes local variable fi exists. */
514#define osmo_mgcpc_ep_fsm_state_chg(state) \
515 osmo_tdef_fsm_inst_state_chg(fi, state, osmo_mgcpc_ep_fsm_timeouts, \
516 ((struct osmo_mgcpc_ep*)fi->priv)->T_defs, 5)
517
518/*! Dispatch an actual CRCX/MDCX/DLCX message for this connection.
519 * \param ci Connection identifier as obtained from osmo_mgcpc_ep_ci_add().
520 * \param verb MGCP operation to dispatch.
521 * \param verb_info Parameters for the MGCP operation.
522 * \param notify Peer FSM instance to notify of completed/failed operation.
523 * \param event_success Which event to dispatch to 'notify' upon OK response.
524 * \param event_failure Which event to dispatch to 'notify' upon failure response.
525 * \param notify_data Data pointer to pass to the event dispatch for both success and failure.
526 */
527void osmo_mgcpc_ep_ci_request(struct osmo_mgcpc_ep_ci *ci,
528 enum mgcp_verb verb, const struct mgcp_conn_peer *verb_info,
529 struct osmo_fsm_inst *notify,
530 uint32_t event_success, uint32_t event_failure,
531 void *notify_data)
532{
533 struct osmo_mgcpc_ep *ep;
534 struct osmo_fsm_inst *fi;
535 struct osmo_mgcpc_ep_ci cleared_ci;
536 ci = osmo_mgcpc_ep_check_ci(ci);
537
538 if (!ci) {
539 LOGP(DLGLOBAL, LOGL_ERROR, "Invalid MGW endpoint request: no ci\n");
540 goto dispatch_error;
541 }
542 if (!verb_info && verb != MGCP_VERB_DLCX) {
543 LOG_CI(ci, LOGL_ERROR, "Invalid MGW endpoint request: missing verb details for %s\n",
544 osmo_mgcp_verb_name(verb));
545 goto dispatch_error;
546 }
547 if ((verb < 0) || (verb > MGCP_VERB_RSIP)) {
548 LOG_CI(ci, LOGL_ERROR, "Invalid MGW endpoint request: unknown verb: %s\n",
549 osmo_mgcp_verb_name(verb));
550 goto dispatch_error;
551 }
552
553 ep = ci->ep;
554 fi = ep->fi;
555
556 /* Clear volatile state by explicitly keeping those that should remain. Because we can't assign
557 * the char[] directly, dance through cleared_ci and copy back. */
558 cleared_ci = (struct osmo_mgcpc_ep_ci){
559 .ep = ep,
560 .mgcp_client_fi = ci->mgcp_client_fi,
561 .got_port_info = ci->got_port_info,
562 .rtp_info = ci->rtp_info,
563
564 .occupied = true,
565 /* .pending = true follows below */
566 .verb = verb,
567 .notify = notify,
568 .notify_success = event_success,
569 .notify_failure = event_failure,
570 .notify_data = notify_data,
571 };
572 osmo_strlcpy(cleared_ci.label, ci->label, sizeof(cleared_ci.label));
573 osmo_strlcpy(cleared_ci.mgcp_ci_str, ci->mgcp_ci_str, sizeof(cleared_ci.mgcp_ci_str));
574 *ci = cleared_ci;
575
576 LOG_CI_VERB(ci, LOGL_DEBUG, "notify=%s\n", osmo_fsm_inst_name(ci->notify));
577
578 if (verb_info)
579 ci->verb_info = *verb_info;
580
581 if (ep->endpoint[0]) {
582 if (ci->verb_info.endpoint[0] && strcmp(ci->verb_info.endpoint, ep->endpoint))
583 LOG_CI(ci, LOGL_ERROR,
584 "Warning: Requested %s on endpoint %s, but this CI is on endpoint %s."
585 " Using the proper endpoint instead.\n",
586 osmo_mgcp_verb_name(verb), ci->verb_info.endpoint, ep->endpoint);
587 osmo_strlcpy(ci->verb_info.endpoint, ep->endpoint, sizeof(ci->verb_info.endpoint));
588 }
589
590 switch (ci->verb) {
591 case MGCP_VERB_CRCX:
592 if (ci->mgcp_client_fi) {
593 LOG_CI(ci, LOGL_ERROR, "CRCX can be called only once per MGW endpoint CI\n");
594 on_failure(ci);
595 return;
596 }
597 break;
598
599 case MGCP_VERB_MDCX:
600 if (!ci->mgcp_client_fi) {
601 LOG_CI_VERB(ci, LOGL_ERROR, "The first verb on an unused MGW endpoint CI must be CRCX, not %s\n",
602 osmo_mgcp_verb_name(ci->verb));
603 on_failure(ci);
604 return;
605 }
606 break;
607
608 case MGCP_VERB_DLCX:
609 if (!ci->mgcp_client_fi) {
610 LOG_CI_VERB(ci, LOGL_DEBUG, "Ignoring DLCX on unused MGW endpoint CI\n");
611 return;
612 }
613 break;
614
615 default:
616 LOG_CI(ci, LOGL_ERROR, "This verb is not supported: %s\n", osmo_mgcp_verb_name(ci->verb));
617 on_failure(ci);
618 return;
619 }
620
621 ci->pending = true;
622
623 LOG_CI_VERB(ci, LOGL_DEBUG, "Scheduling\n");
624
625 if (ep->fi->state != OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE)
626 osmo_mgcpc_ep_fsm_state_chg(OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE);
627
628 return;
629dispatch_error:
630 if (notify)
631 osmo_fsm_inst_dispatch(notify, event_failure, notify_data);
632}
633
634static int send_verb(struct osmo_mgcpc_ep_ci *ci)
635{
636 int rc;
637 struct osmo_mgcpc_ep *ep = ci->ep;
638
639 if (!ci->occupied || !ci->pending || ci->sent)
640 return 0;
641
642 switch (ci->verb) {
643
644 case MGCP_VERB_CRCX:
645 OSMO_ASSERT(!ci->mgcp_client_fi);
646 LOG_CI_VERB(ci, LOGL_DEBUG, "Sending\n");
647 ci->mgcp_client_fi = mgcp_conn_create(ep->mgcp_client, ep->fi,
648 CI_EV_FAILURE(ci), CI_EV_SUCCESS(ci),
649 &ci->verb_info);
650 ci->sent = true;
651 if (!ci->mgcp_client_fi){
652 LOG_CI_VERB(ci, LOGL_ERROR, "Cannot send\n");
653 on_failure(ci);
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200654 return -EINVAL;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100655 }
656 osmo_fsm_inst_update_id(ci->mgcp_client_fi, ci->label);
657 break;
658
659 case MGCP_VERB_MDCX:
660 OSMO_ASSERT(ci->mgcp_client_fi);
661 LOG_CI_VERB(ci, LOGL_DEBUG, "Sending\n");
662 rc = mgcp_conn_modify(ci->mgcp_client_fi, CI_EV_SUCCESS(ci), &ci->verb_info);
663 ci->sent = true;
664 if (rc) {
665 LOG_CI_VERB(ci, LOGL_ERROR, "Cannot send (rc=%d %s)\n", rc, strerror(-rc));
666 on_failure(ci);
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200667 return -EINVAL;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100668 }
669 break;
670
671 case MGCP_VERB_DLCX:
672 LOG_CI(ci, LOGL_DEBUG, "Sending MGCP: %s %s\n",
673 osmo_mgcp_verb_name(ci->verb), ci->mgcp_ci_str);
674 /* The way this is designed, we actually need to forget all about the ci right away. */
675 mgcp_conn_delete(ci->mgcp_client_fi);
676 if (ci->notify)
677 osmo_fsm_inst_dispatch(ci->notify, ci->notify_success, ci->notify_data);
678 *ci = (struct osmo_mgcpc_ep_ci){
679 .ep = ep,
680 };
681 break;
682
683 default:
684 OSMO_ASSERT(false);
685 }
686
687 return 1;
688}
689
690/*! DLCX all connections, terminate the endpoint FSM and free. */
691void osmo_mgcpc_ep_clear(struct osmo_mgcpc_ep *ep)
692{
693 if (!ep)
694 return;
695 osmo_fsm_inst_term(ep->fi, OSMO_FSM_TERM_REGULAR, 0);
696}
697
698static void osmo_mgcpc_ep_count(struct osmo_mgcpc_ep *ep, int *occupied, int *pending_not_sent,
699 int *waiting_for_response)
700{
701 int i;
702
703 if (occupied)
704 *occupied = 0;
705
706 if (pending_not_sent)
707 *pending_not_sent = 0;
708
709 if (waiting_for_response)
710 *waiting_for_response = 0;
711
712 for (i = 0; i < ARRAY_SIZE(ep->ci); i++) {
713 struct osmo_mgcpc_ep_ci *ci = &ep->ci[i];
714 if (ci->occupied) {
715 if (occupied)
716 (*occupied)++;
717 } else
718 continue;
719
720 if (ci->pending)
721 LOG_CI_VERB(ci, LOGL_DEBUG, "%s\n",
722 ci->sent ? "waiting for response" : "waiting to be sent");
723 else
724 LOG_CI_VERB(ci, LOGL_DEBUG, "done (%s)\n", mgcp_conn_peer_name(osmo_mgcpc_ep_ci_get_rtp_info(ci)));
725
726 if (ci->pending && ci->sent)
727 if (waiting_for_response)
728 (*waiting_for_response)++;
729 if (ci->pending && !ci->sent)
730 if (pending_not_sent)
731 (*pending_not_sent)++;
732 }
733}
734
735static bool osmo_mgcpc_ep_fsm_check_state_chg_after_response(struct osmo_fsm_inst *fi)
736{
737 int waiting_for_response;
738 int occupied;
739 struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi);
740
741 osmo_mgcpc_ep_count(ep, &occupied, NULL, &waiting_for_response);
742 LOG_MGCPC_EP(ep, LOGL_DEBUG, "CI in use: %d, waiting for response: %d\n", occupied, waiting_for_response);
743
744 if (!occupied) {
745 /* All CI have been released. The endpoint no longer exists. Notify the parent FSM, by
746 * terminating. */
747 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, 0);
748 return false;
749 }
750
751 if (!waiting_for_response) {
752 if (fi->state != OSMO_MGCPC_EP_ST_IN_USE)
753 osmo_mgcpc_ep_fsm_state_chg(OSMO_MGCPC_EP_ST_IN_USE);
754 }
755
756 return true;
757}
758
759static void osmo_mgcpc_ep_fsm_wait_mgw_response_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
760{
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200761 static int re_enter = 0;
762 int rc;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100763 int count = 0;
764 int i;
765 struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi);
766
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200767 re_enter++;
768 OSMO_ASSERT(re_enter < 10);
769
770 /* The first CRCX gives us the endpoint name in the CRCX response. So we must wait for the first CRCX endpoint
771 * response to come in before sending any other MGCP requests -- otherwise we might end up creating new
772 * endpoints instead of acting on the same. This FSM always sends out N requests and waits for all of them to
773 * complete before sending out new requests. Hence we're safe when the very first time at most one request is
774 * sent (which needs to be a CRCX). */
775
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100776 for (i = 0; i < ARRAY_SIZE(ep->ci); i++) {
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200777 struct osmo_mgcpc_ep_ci *ci = &ep->ci[i];
778
779 /* Make sure that only CRCX get dispatched if no CRCX were sent yet. */
780 if (!ep->first_crcx_complete) {
781 if (ci->occupied && ci->verb != MGCP_VERB_CRCX)
782 continue;
783 }
784
785 rc = send_verb(&ep->ci[i]);
786 /* Need to be careful not to access the instance after failure. Event chains may already have
787 * deallocated this memory. */
788 if (rc < 0)
789 return;
790 if (!rc)
791 continue;
792 count++;
793 /* Make sure that we wait for the first CRCX response before dispatching more requests. */
794 if (!ep->first_crcx_complete)
795 break;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100796 }
797
798 LOG_MGCPC_EP(ep, LOGL_DEBUG, "Sent messages: %d\n", count);
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200799 if (ep->first_crcx_complete)
800 osmo_mgcpc_ep_fsm_check_state_chg_after_response(fi);
801 re_enter--;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100802}
803
804static void osmo_mgcpc_ep_fsm_handle_ci_events(struct osmo_fsm_inst *fi, uint32_t event, void *data)
805{
806 struct osmo_mgcpc_ep_ci *ci;
807 struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi);
808 ci = osmo_mgcpc_ep_ci_for_event(ep, event);
809 if (ci) {
810 if (event == CI_EV_SUCCESS(ci))
811 on_success(ci, data);
812 else
813 on_failure(ci);
814 }
815}
816
817static void osmo_mgcpc_ep_fsm_in_use_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
818{
819 int pending_not_sent;
820 struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi);
821
822 osmo_mgcpc_ep_count(ep, NULL, &pending_not_sent, NULL);
823 if (pending_not_sent)
824 osmo_mgcpc_ep_fsm_state_chg(OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE);
825}
826
827#define S(x) (1 << (x))
828
829static const struct osmo_fsm_state osmo_mgcpc_ep_fsm_states[] = {
830 [OSMO_MGCPC_EP_ST_UNUSED] = {
831 .name = "UNUSED",
832 .in_event_mask = 0,
833 .out_state_mask = 0
834 | S(OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE)
835 ,
836 },
837 [OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE] = {
838 .name = "WAIT_MGW_RESPONSE",
839 .onenter = osmo_mgcpc_ep_fsm_wait_mgw_response_onenter,
840 .action = osmo_mgcpc_ep_fsm_handle_ci_events,
841 .in_event_mask = 0xffffffff,
842 .out_state_mask = 0
843 | S(OSMO_MGCPC_EP_ST_IN_USE)
844 ,
845 },
846 [OSMO_MGCPC_EP_ST_IN_USE] = {
847 .name = "IN_USE",
848 .onenter = osmo_mgcpc_ep_fsm_in_use_onenter,
849 .action = osmo_mgcpc_ep_fsm_handle_ci_events,
850 .in_event_mask = 0xffffffff, /* mgcp_client_fsm may send parent term anytime */
851 .out_state_mask = 0
852 | S(OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE)
853 ,
854 },
855};
856
857static int osmo_mgcpc_ep_fsm_timer_cb(struct osmo_fsm_inst *fi)
858{
859 int i;
860 struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi);
861
862 switch (fi->T) {
863 default:
864 for (i = 0; i < ARRAY_SIZE(ep->ci); i++) {
865 struct osmo_mgcpc_ep_ci *ci = &ep->ci[i];
866 if (!ci->occupied)
867 continue;
868 if (!(ci->pending && ci->sent))
869 continue;
870 on_failure(ci);
871 }
872 return 0;
873 }
874
875 return 0;
876}
877
878static struct osmo_fsm osmo_mgcpc_ep_fsm = {
Pau Espin Pedrol182ca3b2019-05-08 14:01:20 +0200879 .name = "mgw-endp",
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100880 .states = osmo_mgcpc_ep_fsm_states,
881 .num_states = ARRAY_SIZE(osmo_mgcpc_ep_fsm_states),
882 .log_subsys = DLMGCP,
883 .event_names = osmo_mgcpc_ep_fsm_event_names,
884 .timer_cb = osmo_mgcpc_ep_fsm_timer_cb,
885 /* The FSM termination will automatically trigger any mgcp_client_fsm instances to DLCX. */
886};