blob: 76552fbb85575d8f17a8dfb83dad6b540f96d046 [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
113 /*! Endpoint connection slots. Note that each connection has its own set of FSM event numbers to signal success
114 * and failure, depending on its index within this array. See CI_EV_SUCCESS and CI_EV_FAILURE. */
115 struct osmo_mgcpc_ep_ci ci[USABLE_CI];
116};
117
118const struct value_string osmo_mgcp_verb_names[] = {
119 { MGCP_VERB_CRCX, "CRCX" },
120 { MGCP_VERB_MDCX, "MDCX" },
121 { MGCP_VERB_DLCX, "DLCX" },
122 { MGCP_VERB_AUEP, "AUEP" },
123 { MGCP_VERB_RSIP, "RSIP" },
124 {}
125};
126
127static struct osmo_mgcpc_ep_ci *osmo_mgcpc_ep_check_ci(struct osmo_mgcpc_ep_ci *ci)
128{
129 if (!ci)
130 return NULL;
131 if (!ci->ep)
132 return NULL;
133 if (ci < ci->ep->ci || ci >= &ci->ep->ci[USABLE_CI])
134 return NULL;
135 return ci;
136}
137
138static struct osmo_mgcpc_ep_ci *osmo_mgcpc_ep_ci_for_event(struct osmo_mgcpc_ep *ep, uint32_t event)
139{
140 int idx;
141 if (event < FIRST_CI_EVENT)
142 return NULL;
143 idx = EV_TO_CI_IDX(event);
144 if (idx >= sizeof(ep->ci))
145 return NULL;
146 return osmo_mgcpc_ep_check_ci(&ep->ci[idx]);
147}
148
149const char *osmo_mgcpc_ep_name(const struct osmo_mgcpc_ep *ep)
150{
151 if (!ep)
152 return "NULL";
153 if (ep->endpoint[0])
154 return ep->endpoint;
155 return osmo_fsm_inst_name(ep->fi);
156}
157
158const char *mgcp_conn_peer_name(const struct mgcp_conn_peer *info)
159{
160 /* I'd be fine with a smaller buffer and accept truncation, but gcc possibly refuses to build if
161 * this buffer is too small. */
162 static char buf[1024];
163
164 if (!info)
165 return "NULL";
166
167 if (info->endpoint[0]
168 && info->addr[0])
169 snprintf(buf, sizeof(buf), "%s:%s:%u",
170 info->endpoint, info->addr, info->port);
171 else if (info->endpoint[0])
172 snprintf(buf, sizeof(buf), "%s", info->endpoint);
173 else if (info->addr[0])
174 snprintf(buf, sizeof(buf), "%s:%u", info->addr, info->port);
175 else
176 return "empty";
177 return buf;
178}
179
180const char *osmo_mgcpc_ep_ci_name(const struct osmo_mgcpc_ep_ci *ci)
181{
182 const struct mgcp_conn_peer *rtp_info;
183
184 if (!ci)
185 return "NULL";
186
187 rtp_info = osmo_mgcpc_ep_ci_get_rtp_info(ci);
188
189 if (rtp_info)
190 return mgcp_conn_peer_name(rtp_info);
191 return osmo_mgcpc_ep_name(ci->ep);
192}
193
194const char *osmo_mgcpc_ep_ci_id(const struct osmo_mgcpc_ep_ci *ci)
195{
196 if (!ci || !ci->mgcp_ci_str[0])
197 return NULL;
198 return ci->mgcp_ci_str;
199}
200
201static struct value_string osmo_mgcpc_ep_fsm_event_names[33] = {};
202
203static char osmo_mgcpc_ep_fsm_event_name_bufs[32][32] = {};
204
205static void fill_event_names()
206{
207 int i;
208 for (i = 0; i < (ARRAY_SIZE(osmo_mgcpc_ep_fsm_event_names) - 1); i++) {
209 if (i < _OSMO_MGCPC_EP_EV_LAST)
210 continue;
211 if (i < FIRST_CI_EVENT || EV_TO_CI_IDX(i) > USABLE_CI) {
212 osmo_mgcpc_ep_fsm_event_names[i] = (struct value_string){i, "Unused"};
213 continue;
214 }
215 snprintf(osmo_mgcpc_ep_fsm_event_name_bufs[i], sizeof(osmo_mgcpc_ep_fsm_event_name_bufs[i]),
216 "MGW Response for CI #%d", EV_TO_CI_IDX(i));
217 osmo_mgcpc_ep_fsm_event_names[i] = (struct value_string){i, osmo_mgcpc_ep_fsm_event_name_bufs[i]};
218 }
219}
220
221/* T_defs is used to obtain an (Osmocom specific) T2427001: timeout for an MGCP response (note, 2427 corresponds to the
222 * default MGCP port in osmo-mgw). */
223static __attribute__((constructor)) void osmo_mgcpc_ep_fsm_init()
224{
225 OSMO_ASSERT(osmo_fsm_register(&osmo_mgcpc_ep_fsm) == 0);
226 fill_event_names();
227}
228
229struct osmo_mgcpc_ep *osmo_mgcpc_ep_fi_mgwep(struct osmo_fsm_inst *fi)
230{
231 OSMO_ASSERT(fi);
232 OSMO_ASSERT(fi->fsm == &osmo_mgcpc_ep_fsm);
233 OSMO_ASSERT(fi->priv);
234 return fi->priv;
235}
236
237/*! Allocate an osmo_mgcpc_ep FSM.
238 * MGCP messages to set up the endpoint will be sent on the given mgcp_client, as soon as the first
239 * osmo_mgcpc_ep_ci_request() is invoked.
240 *
241 * A typical sequence of events would be:
242 *
243 * ep = osmo_mgcpc_ep_alloc(..., mgcp_client_rtpbridge_wildcard(client));
244 * ci_to_ran = osmo_mgcpc_ep_ci_add(ep);
245 * osmo_mgcpc_ep_ci_request(ci_to_ran, MGCP_VERB_CRCX, verb_info,
246 * my_call_fsm, MY_EVENT_MGCP_OK, MY_EVENT_MGCP_FAIL);
247 * ci_to_cn = osmo_mgcpc_ep_ci_add(ep);
248 * osmo_mgcpc_ep_ci_request(ci_to_cn, MGCP_VERB_CRCX, verb_info,
249 * my_call_fsm, MY_EVENT_MGCP_OK, MY_EVENT_MGCP_FAIL);
250 * ...
251 * osmo_mgcpc_ep_ci_request(ci_to_ran, MGCP_VERB_MDCX, ...);
252 * ...
253 * osmo_mgcpc_ep_clear(ep);
254 * ep = NULL;
255 *
256 * \param parent Parent FSM.
257 * \param parent_term_event Event to dispatch to the parent on termination of this FSM instance.
258 * \param mgcp_client Connection to the MGW.
259 * \param T_defs Timeout definitions to be used for FSM states, see osmo_mgcpc_ep_fsm_timeouts.
260 * \param fsm_id FSM instance ID.
261 * \param endpoint_str_fmt The endpoint string format to send to the MGW upon the first CRCX.
262 * See mgcp_client_rtpbridge_wildcard() for "rtpbridge" endpoints.
263 */
264struct osmo_mgcpc_ep *osmo_mgcpc_ep_alloc(struct osmo_fsm_inst *parent, uint32_t parent_term_event,
265 struct mgcp_client *mgcp_client,
266 const struct osmo_tdef *T_defs,
267 const char *fsm_id,
268 const char *endpoint_str_fmt, ...)
269{
270 va_list ap;
271 struct osmo_fsm_inst *fi;
272 struct osmo_mgcpc_ep *ep;
273 int rc;
274
275 if (!mgcp_client)
276 return NULL;
277
278 fi = osmo_fsm_inst_alloc_child(&osmo_mgcpc_ep_fsm, parent, parent_term_event);
279 OSMO_ASSERT(fi);
280
281 osmo_fsm_inst_update_id(fi, fsm_id);
282
283 ep = talloc_zero(fi, struct osmo_mgcpc_ep);
284 OSMO_ASSERT(ep);
285
286 *ep = (struct osmo_mgcpc_ep){
287 .mgcp_client = mgcp_client,
288 .fi = fi,
289 .T_defs = T_defs,
290 };
291 fi->priv = ep;
292
293 va_start(ap, endpoint_str_fmt);
294 rc = vsnprintf(ep->endpoint, sizeof(ep->endpoint), endpoint_str_fmt ? : "", ap);
295 va_end(ap);
296
297 if (rc <= 0 || rc >= sizeof(ep->endpoint)) {
298 LOG_MGCPC_EP(ep, LOGL_ERROR, "Endpoint name too long or too short: %s\n",
299 ep->endpoint);
300 osmo_fsm_inst_term(ep->fi, OSMO_FSM_TERM_ERROR, 0);
301 return NULL;
302 }
303
304 return ep;
305}
306
307/*! Add a connection to an endpoint.
308 * Allocate a connection identifier slot in the osmo_mgcpc_ep instance, do not yet dispatch a CRCX.
309 * The CRCX is dispatched only upon the first osmo_mgcpc_ep_ci_request().
310 * \param ep Parent endpoint instance.
311 * \param label_fmt Label for logging.
312 */
313struct osmo_mgcpc_ep_ci *osmo_mgcpc_ep_ci_add(struct osmo_mgcpc_ep *ep,
314 const char *label_fmt, ...)
315{
316 va_list ap;
317 int i;
318 struct osmo_mgcpc_ep_ci *ci;
319
320 for (i = 0; i < USABLE_CI; i++) {
321 ci = &ep->ci[i];
322
323 if (ci->occupied || ci->mgcp_client_fi)
324 continue;
325
326 *ci = (struct osmo_mgcpc_ep_ci){
327 .ep = ep,
328 .occupied = true,
329 };
330 if (label_fmt) {
331 va_start(ap, label_fmt);
332 vsnprintf(ci->label, sizeof(ci->label), label_fmt, ap);
333 va_end(ap);
334 }
335 return ci;
336 }
337
338 LOG_MGCPC_EP(ep, LOGL_ERROR,
339 "Cannot allocate another endpoint, all "
340 OSMO_STRINGIFY_VAL(USABLE_CI) " are in use\n");
341
342 return NULL;
343}
344
345static bool osmo_mgcpc_ep_fsm_check_state_chg_after_response(struct osmo_fsm_inst *fi);
346
347static void on_failure(struct osmo_mgcpc_ep_ci *ci)
348{
349 struct osmo_fsm_inst *notify = ci->notify;
350 uint32_t notify_failure = ci->notify_failure;
351 void *notify_data = ci->notify_data;
352
353 if (!ci->occupied)
354 return;
355
356 *ci = (struct osmo_mgcpc_ep_ci){
357 .ep = ci->ep,
358 };
359
360 /* If this check has terminated the FSM instance, don't fire any more events to prevent use-after-free problems.
361 * The endpoint FSM does dispatch a term event to its parent, and everything should be cleaned like that. */
362 if (!osmo_mgcpc_ep_fsm_check_state_chg_after_response(ci->ep->fi))
363 return;
364
365 if (notify)
366 osmo_fsm_inst_dispatch(notify, notify_failure, notify_data);
367}
368
369static void on_success(struct osmo_mgcpc_ep_ci *ci, void *data)
370{
371 struct mgcp_conn_peer *rtp_info;
372
373 if (!ci->occupied)
374 return;
375
376 ci->pending = false;
377
378 switch (ci->verb) {
379 case MGCP_VERB_CRCX:
380 /* If we sent a wildcarded endpoint name on CRCX, we need to store the resulting endpoint
381 * name here. Also, we receive the MGW's RTP port information. */
382 rtp_info = data;
383 OSMO_ASSERT(rtp_info);
384 ci->got_port_info = true;
385 ci->rtp_info = *rtp_info;
386 osmo_strlcpy(ci->mgcp_ci_str, mgcp_conn_get_ci(ci->mgcp_client_fi),
387 sizeof(ci->mgcp_ci_str));
388 if (rtp_info->endpoint[0]) {
389 int rc;
390 rc = osmo_strlcpy(ci->ep->endpoint, rtp_info->endpoint,
391 sizeof(ci->ep->endpoint));
392 if (rc <= 0 || rc >= sizeof(ci->ep->endpoint)) {
393 LOG_CI(ci, LOGL_ERROR, "Unable to copy endpoint name '%s'\n",
394 rtp_info->endpoint);
395 osmo_mgcpc_ep_ci_dlcx(ci);
396 on_failure(ci);
397 return;
398 }
399 }
400 break;
401
402 default:
403 break;
404 }
405
406 LOG_CI(ci, LOGL_DEBUG, "received successful response to %s: RTP=%s%s\n",
407 osmo_mgcp_verb_name(ci->verb),
408 mgcp_conn_peer_name(ci->got_port_info? &ci->rtp_info : NULL),
409 ci->notify ? "" : " (not sending a notification)");
410
411 if (ci->notify)
412 osmo_fsm_inst_dispatch(ci->notify, ci->notify_success, ci->notify_data);
413
414 osmo_mgcpc_ep_fsm_check_state_chg_after_response(ci->ep->fi);
415}
416
417/*! Return the MGW's RTP port information for this connection, as returned by the last CRCX/MDCX OK message. */
418const struct mgcp_conn_peer *osmo_mgcpc_ep_ci_get_rtp_info(const struct osmo_mgcpc_ep_ci *ci)
419{
420 ci = osmo_mgcpc_ep_check_ci((struct osmo_mgcpc_ep_ci*)ci);
421 if (!ci)
422 return NULL;
423 if (!ci->got_port_info)
424 return NULL;
425 return &ci->rtp_info;
426}
427
428/*! Return the MGW's RTP port information for this connection, as returned by the last CRCX/MDCX OK message. */
429bool osmo_mgcpc_ep_ci_get_crcx_info_to_sockaddr(const struct osmo_mgcpc_ep_ci *ci, struct sockaddr_storage *dest)
430{
431 const struct mgcp_conn_peer *rtp_info;
432 struct sockaddr_in *sin;
433
434 rtp_info = osmo_mgcpc_ep_ci_get_rtp_info(ci);
435 if (!rtp_info)
436 return false;
437
438 sin = (struct sockaddr_in *)dest;
439
440 sin->sin_family = AF_INET;
441 sin->sin_addr.s_addr = inet_addr(rtp_info->addr);
442 sin->sin_port = osmo_ntohs(rtp_info->port);
443 return true;
444}
445
446
447static const struct osmo_tdef_state_timeout osmo_mgcpc_ep_fsm_timeouts[32] = {
448 [OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE] = { .T=2427001 },
449};
450
451/* Transition to a state, using the T timer defined in assignment_fsm_timeouts.
452 * The actual timeout value is in turn obtained from osmo_mgcpc_ep.T_defs.
453 * Assumes local variable fi exists. */
454#define osmo_mgcpc_ep_fsm_state_chg(state) \
455 osmo_tdef_fsm_inst_state_chg(fi, state, osmo_mgcpc_ep_fsm_timeouts, \
456 ((struct osmo_mgcpc_ep*)fi->priv)->T_defs, 5)
457
458/*! Dispatch an actual CRCX/MDCX/DLCX message for this connection.
459 * \param ci Connection identifier as obtained from osmo_mgcpc_ep_ci_add().
460 * \param verb MGCP operation to dispatch.
461 * \param verb_info Parameters for the MGCP operation.
462 * \param notify Peer FSM instance to notify of completed/failed operation.
463 * \param event_success Which event to dispatch to 'notify' upon OK response.
464 * \param event_failure Which event to dispatch to 'notify' upon failure response.
465 * \param notify_data Data pointer to pass to the event dispatch for both success and failure.
466 */
467void osmo_mgcpc_ep_ci_request(struct osmo_mgcpc_ep_ci *ci,
468 enum mgcp_verb verb, const struct mgcp_conn_peer *verb_info,
469 struct osmo_fsm_inst *notify,
470 uint32_t event_success, uint32_t event_failure,
471 void *notify_data)
472{
473 struct osmo_mgcpc_ep *ep;
474 struct osmo_fsm_inst *fi;
475 struct osmo_mgcpc_ep_ci cleared_ci;
476 ci = osmo_mgcpc_ep_check_ci(ci);
477
478 if (!ci) {
479 LOGP(DLGLOBAL, LOGL_ERROR, "Invalid MGW endpoint request: no ci\n");
480 goto dispatch_error;
481 }
482 if (!verb_info && verb != MGCP_VERB_DLCX) {
483 LOG_CI(ci, LOGL_ERROR, "Invalid MGW endpoint request: missing verb details for %s\n",
484 osmo_mgcp_verb_name(verb));
485 goto dispatch_error;
486 }
487 if ((verb < 0) || (verb > MGCP_VERB_RSIP)) {
488 LOG_CI(ci, LOGL_ERROR, "Invalid MGW endpoint request: unknown verb: %s\n",
489 osmo_mgcp_verb_name(verb));
490 goto dispatch_error;
491 }
492
493 ep = ci->ep;
494 fi = ep->fi;
495
496 /* Clear volatile state by explicitly keeping those that should remain. Because we can't assign
497 * the char[] directly, dance through cleared_ci and copy back. */
498 cleared_ci = (struct osmo_mgcpc_ep_ci){
499 .ep = ep,
500 .mgcp_client_fi = ci->mgcp_client_fi,
501 .got_port_info = ci->got_port_info,
502 .rtp_info = ci->rtp_info,
503
504 .occupied = true,
505 /* .pending = true follows below */
506 .verb = verb,
507 .notify = notify,
508 .notify_success = event_success,
509 .notify_failure = event_failure,
510 .notify_data = notify_data,
511 };
512 osmo_strlcpy(cleared_ci.label, ci->label, sizeof(cleared_ci.label));
513 osmo_strlcpy(cleared_ci.mgcp_ci_str, ci->mgcp_ci_str, sizeof(cleared_ci.mgcp_ci_str));
514 *ci = cleared_ci;
515
516 LOG_CI_VERB(ci, LOGL_DEBUG, "notify=%s\n", osmo_fsm_inst_name(ci->notify));
517
518 if (verb_info)
519 ci->verb_info = *verb_info;
520
521 if (ep->endpoint[0]) {
522 if (ci->verb_info.endpoint[0] && strcmp(ci->verb_info.endpoint, ep->endpoint))
523 LOG_CI(ci, LOGL_ERROR,
524 "Warning: Requested %s on endpoint %s, but this CI is on endpoint %s."
525 " Using the proper endpoint instead.\n",
526 osmo_mgcp_verb_name(verb), ci->verb_info.endpoint, ep->endpoint);
527 osmo_strlcpy(ci->verb_info.endpoint, ep->endpoint, sizeof(ci->verb_info.endpoint));
528 }
529
530 switch (ci->verb) {
531 case MGCP_VERB_CRCX:
532 if (ci->mgcp_client_fi) {
533 LOG_CI(ci, LOGL_ERROR, "CRCX can be called only once per MGW endpoint CI\n");
534 on_failure(ci);
535 return;
536 }
537 break;
538
539 case MGCP_VERB_MDCX:
540 if (!ci->mgcp_client_fi) {
541 LOG_CI_VERB(ci, LOGL_ERROR, "The first verb on an unused MGW endpoint CI must be CRCX, not %s\n",
542 osmo_mgcp_verb_name(ci->verb));
543 on_failure(ci);
544 return;
545 }
546 break;
547
548 case MGCP_VERB_DLCX:
549 if (!ci->mgcp_client_fi) {
550 LOG_CI_VERB(ci, LOGL_DEBUG, "Ignoring DLCX on unused MGW endpoint CI\n");
551 return;
552 }
553 break;
554
555 default:
556 LOG_CI(ci, LOGL_ERROR, "This verb is not supported: %s\n", osmo_mgcp_verb_name(ci->verb));
557 on_failure(ci);
558 return;
559 }
560
561 ci->pending = true;
562
563 LOG_CI_VERB(ci, LOGL_DEBUG, "Scheduling\n");
564
565 if (ep->fi->state != OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE)
566 osmo_mgcpc_ep_fsm_state_chg(OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE);
567
568 return;
569dispatch_error:
570 if (notify)
571 osmo_fsm_inst_dispatch(notify, event_failure, notify_data);
572}
573
574static int send_verb(struct osmo_mgcpc_ep_ci *ci)
575{
576 int rc;
577 struct osmo_mgcpc_ep *ep = ci->ep;
578
579 if (!ci->occupied || !ci->pending || ci->sent)
580 return 0;
581
582 switch (ci->verb) {
583
584 case MGCP_VERB_CRCX:
585 OSMO_ASSERT(!ci->mgcp_client_fi);
586 LOG_CI_VERB(ci, LOGL_DEBUG, "Sending\n");
587 ci->mgcp_client_fi = mgcp_conn_create(ep->mgcp_client, ep->fi,
588 CI_EV_FAILURE(ci), CI_EV_SUCCESS(ci),
589 &ci->verb_info);
590 ci->sent = true;
591 if (!ci->mgcp_client_fi){
592 LOG_CI_VERB(ci, LOGL_ERROR, "Cannot send\n");
593 on_failure(ci);
594 }
595 osmo_fsm_inst_update_id(ci->mgcp_client_fi, ci->label);
596 break;
597
598 case MGCP_VERB_MDCX:
599 OSMO_ASSERT(ci->mgcp_client_fi);
600 LOG_CI_VERB(ci, LOGL_DEBUG, "Sending\n");
601 rc = mgcp_conn_modify(ci->mgcp_client_fi, CI_EV_SUCCESS(ci), &ci->verb_info);
602 ci->sent = true;
603 if (rc) {
604 LOG_CI_VERB(ci, LOGL_ERROR, "Cannot send (rc=%d %s)\n", rc, strerror(-rc));
605 on_failure(ci);
606 }
607 break;
608
609 case MGCP_VERB_DLCX:
610 LOG_CI(ci, LOGL_DEBUG, "Sending MGCP: %s %s\n",
611 osmo_mgcp_verb_name(ci->verb), ci->mgcp_ci_str);
612 /* The way this is designed, we actually need to forget all about the ci right away. */
613 mgcp_conn_delete(ci->mgcp_client_fi);
614 if (ci->notify)
615 osmo_fsm_inst_dispatch(ci->notify, ci->notify_success, ci->notify_data);
616 *ci = (struct osmo_mgcpc_ep_ci){
617 .ep = ep,
618 };
619 break;
620
621 default:
622 OSMO_ASSERT(false);
623 }
624
625 return 1;
626}
627
628/*! DLCX all connections, terminate the endpoint FSM and free. */
629void osmo_mgcpc_ep_clear(struct osmo_mgcpc_ep *ep)
630{
631 if (!ep)
632 return;
633 osmo_fsm_inst_term(ep->fi, OSMO_FSM_TERM_REGULAR, 0);
634}
635
636static void osmo_mgcpc_ep_count(struct osmo_mgcpc_ep *ep, int *occupied, int *pending_not_sent,
637 int *waiting_for_response)
638{
639 int i;
640
641 if (occupied)
642 *occupied = 0;
643
644 if (pending_not_sent)
645 *pending_not_sent = 0;
646
647 if (waiting_for_response)
648 *waiting_for_response = 0;
649
650 for (i = 0; i < ARRAY_SIZE(ep->ci); i++) {
651 struct osmo_mgcpc_ep_ci *ci = &ep->ci[i];
652 if (ci->occupied) {
653 if (occupied)
654 (*occupied)++;
655 } else
656 continue;
657
658 if (ci->pending)
659 LOG_CI_VERB(ci, LOGL_DEBUG, "%s\n",
660 ci->sent ? "waiting for response" : "waiting to be sent");
661 else
662 LOG_CI_VERB(ci, LOGL_DEBUG, "done (%s)\n", mgcp_conn_peer_name(osmo_mgcpc_ep_ci_get_rtp_info(ci)));
663
664 if (ci->pending && ci->sent)
665 if (waiting_for_response)
666 (*waiting_for_response)++;
667 if (ci->pending && !ci->sent)
668 if (pending_not_sent)
669 (*pending_not_sent)++;
670 }
671}
672
673static bool osmo_mgcpc_ep_fsm_check_state_chg_after_response(struct osmo_fsm_inst *fi)
674{
675 int waiting_for_response;
676 int occupied;
677 struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi);
678
679 osmo_mgcpc_ep_count(ep, &occupied, NULL, &waiting_for_response);
680 LOG_MGCPC_EP(ep, LOGL_DEBUG, "CI in use: %d, waiting for response: %d\n", occupied, waiting_for_response);
681
682 if (!occupied) {
683 /* All CI have been released. The endpoint no longer exists. Notify the parent FSM, by
684 * terminating. */
685 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, 0);
686 return false;
687 }
688
689 if (!waiting_for_response) {
690 if (fi->state != OSMO_MGCPC_EP_ST_IN_USE)
691 osmo_mgcpc_ep_fsm_state_chg(OSMO_MGCPC_EP_ST_IN_USE);
692 }
693
694 return true;
695}
696
697static void osmo_mgcpc_ep_fsm_wait_mgw_response_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
698{
699 int count = 0;
700 int i;
701 struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi);
702
703 for (i = 0; i < ARRAY_SIZE(ep->ci); i++) {
704 count += send_verb(&ep->ci[i]);
705 }
706
707 LOG_MGCPC_EP(ep, LOGL_DEBUG, "Sent messages: %d\n", count);
708 osmo_mgcpc_ep_fsm_check_state_chg_after_response(fi);
709}
710
711static void osmo_mgcpc_ep_fsm_handle_ci_events(struct osmo_fsm_inst *fi, uint32_t event, void *data)
712{
713 struct osmo_mgcpc_ep_ci *ci;
714 struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi);
715 ci = osmo_mgcpc_ep_ci_for_event(ep, event);
716 if (ci) {
717 if (event == CI_EV_SUCCESS(ci))
718 on_success(ci, data);
719 else
720 on_failure(ci);
721 }
722}
723
724static void osmo_mgcpc_ep_fsm_in_use_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
725{
726 int pending_not_sent;
727 struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi);
728
729 osmo_mgcpc_ep_count(ep, NULL, &pending_not_sent, NULL);
730 if (pending_not_sent)
731 osmo_mgcpc_ep_fsm_state_chg(OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE);
732}
733
734#define S(x) (1 << (x))
735
736static const struct osmo_fsm_state osmo_mgcpc_ep_fsm_states[] = {
737 [OSMO_MGCPC_EP_ST_UNUSED] = {
738 .name = "UNUSED",
739 .in_event_mask = 0,
740 .out_state_mask = 0
741 | S(OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE)
742 ,
743 },
744 [OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE] = {
745 .name = "WAIT_MGW_RESPONSE",
746 .onenter = osmo_mgcpc_ep_fsm_wait_mgw_response_onenter,
747 .action = osmo_mgcpc_ep_fsm_handle_ci_events,
748 .in_event_mask = 0xffffffff,
749 .out_state_mask = 0
750 | S(OSMO_MGCPC_EP_ST_IN_USE)
751 ,
752 },
753 [OSMO_MGCPC_EP_ST_IN_USE] = {
754 .name = "IN_USE",
755 .onenter = osmo_mgcpc_ep_fsm_in_use_onenter,
756 .action = osmo_mgcpc_ep_fsm_handle_ci_events,
757 .in_event_mask = 0xffffffff, /* mgcp_client_fsm may send parent term anytime */
758 .out_state_mask = 0
759 | S(OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE)
760 ,
761 },
762};
763
764static int osmo_mgcpc_ep_fsm_timer_cb(struct osmo_fsm_inst *fi)
765{
766 int i;
767 struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi);
768
769 switch (fi->T) {
770 default:
771 for (i = 0; i < ARRAY_SIZE(ep->ci); i++) {
772 struct osmo_mgcpc_ep_ci *ci = &ep->ci[i];
773 if (!ci->occupied)
774 continue;
775 if (!(ci->pending && ci->sent))
776 continue;
777 on_failure(ci);
778 }
779 return 0;
780 }
781
782 return 0;
783}
784
785static struct osmo_fsm osmo_mgcpc_ep_fsm = {
786 .name = "mgw-endpoint",
787 .states = osmo_mgcpc_ep_fsm_states,
788 .num_states = ARRAY_SIZE(osmo_mgcpc_ep_fsm_states),
789 .log_subsys = DLMGCP,
790 .event_names = osmo_mgcpc_ep_fsm_event_names,
791 .timer_cb = osmo_mgcpc_ep_fsm_timer_cb,
792 /* The FSM termination will automatically trigger any mgcp_client_fsm instances to DLCX. */
793};