Neels Hofmeyr | 538d2c5 | 2019-01-28 03:51:35 +0100 | [diff] [blame] | 1 | /* 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 | |
| 56 | enum 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 | |
| 62 | enum 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 | |
| 74 | static struct osmo_fsm osmo_mgcpc_ep_fsm; |
| 75 | |
Neels Hofmeyr | 3ff7128 | 2019-10-29 17:41:20 +0100 | [diff] [blame] | 76 | struct fsm_notify { |
Neels Hofmeyr | f2bf8dc | 2019-10-29 17:39:56 +0100 | [diff] [blame] | 77 | struct llist_head entry; |
Neels Hofmeyr | 3ff7128 | 2019-10-29 17:41:20 +0100 | [diff] [blame] | 78 | struct osmo_fsm_inst *fi; |
| 79 | uint32_t success; |
| 80 | uint32_t failure; |
| 81 | void *data; |
| 82 | }; |
| 83 | |
Neels Hofmeyr | 538d2c5 | 2019-01-28 03:51:35 +0100 | [diff] [blame] | 84 | /*! 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 | */ |
| 87 | struct 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 Hofmeyr | 3ff7128 | 2019-10-29 17:41:20 +0100 | [diff] [blame] | 98 | struct fsm_notify notify; |
Neels Hofmeyr | 538d2c5 | 2019-01-28 03:51:35 +0100 | [diff] [blame] | 99 | |
| 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". */ |
| 106 | struct 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 Hofmeyr | c5479fe | 2019-04-05 01:36:06 +0200 | [diff] [blame] | 118 | /*! 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 Hofmeyr | 538d2c5 | 2019-01-28 03:51:35 +0100 | [diff] [blame] | 123 | /*! 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 Hofmeyr | f2bf8dc | 2019-10-29 17:39:56 +0100 | [diff] [blame] | 126 | |
| 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 Hofmeyr | 538d2c5 | 2019-01-28 03:51:35 +0100 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | const 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 Hofmeyr | c5479fe | 2019-04-05 01:36:06 +0200 | [diff] [blame] | 141 | static void osmo_mgcpc_ep_count(struct osmo_mgcpc_ep *ep, int *occupied, int *pending_not_sent, |
| 142 | int *waiting_for_response); |
| 143 | |
Neels Hofmeyr | 538d2c5 | 2019-01-28 03:51:35 +0100 | [diff] [blame] | 144 | static 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 | |
| 155 | static 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 | |
| 166 | const 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 | |
| 175 | const 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 | |
| 197 | const 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 | |
| 211 | const 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 | |
| 218 | static struct value_string osmo_mgcpc_ep_fsm_event_names[33] = {}; |
| 219 | |
| 220 | static char osmo_mgcpc_ep_fsm_event_name_bufs[32][32] = {}; |
| 221 | |
| 222 | static 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). */ |
| 240 | static __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 | |
| 246 | struct 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 | * |
| 258 | * A typical sequence of events would be: |
| 259 | * |
| 260 | * ep = osmo_mgcpc_ep_alloc(..., mgcp_client_rtpbridge_wildcard(client)); |
| 261 | * ci_to_ran = osmo_mgcpc_ep_ci_add(ep); |
| 262 | * osmo_mgcpc_ep_ci_request(ci_to_ran, MGCP_VERB_CRCX, verb_info, |
| 263 | * my_call_fsm, MY_EVENT_MGCP_OK, MY_EVENT_MGCP_FAIL); |
| 264 | * ci_to_cn = osmo_mgcpc_ep_ci_add(ep); |
| 265 | * osmo_mgcpc_ep_ci_request(ci_to_cn, MGCP_VERB_CRCX, verb_info, |
| 266 | * my_call_fsm, MY_EVENT_MGCP_OK, MY_EVENT_MGCP_FAIL); |
| 267 | * ... |
| 268 | * osmo_mgcpc_ep_ci_request(ci_to_ran, MGCP_VERB_MDCX, ...); |
| 269 | * ... |
| 270 | * osmo_mgcpc_ep_clear(ep); |
| 271 | * ep = NULL; |
| 272 | * |
| 273 | * \param parent Parent FSM. |
| 274 | * \param parent_term_event Event to dispatch to the parent on termination of this FSM instance. |
| 275 | * \param mgcp_client Connection to the MGW. |
| 276 | * \param T_defs Timeout definitions to be used for FSM states, see osmo_mgcpc_ep_fsm_timeouts. |
| 277 | * \param fsm_id FSM instance ID. |
| 278 | * \param endpoint_str_fmt The endpoint string format to send to the MGW upon the first CRCX. |
| 279 | * See mgcp_client_rtpbridge_wildcard() for "rtpbridge" endpoints. |
| 280 | */ |
| 281 | struct osmo_mgcpc_ep *osmo_mgcpc_ep_alloc(struct osmo_fsm_inst *parent, uint32_t parent_term_event, |
| 282 | struct mgcp_client *mgcp_client, |
| 283 | const struct osmo_tdef *T_defs, |
| 284 | const char *fsm_id, |
| 285 | const char *endpoint_str_fmt, ...) |
| 286 | { |
| 287 | va_list ap; |
| 288 | struct osmo_fsm_inst *fi; |
| 289 | struct osmo_mgcpc_ep *ep; |
| 290 | int rc; |
| 291 | |
| 292 | if (!mgcp_client) |
| 293 | return NULL; |
| 294 | |
| 295 | fi = osmo_fsm_inst_alloc_child(&osmo_mgcpc_ep_fsm, parent, parent_term_event); |
| 296 | OSMO_ASSERT(fi); |
| 297 | |
| 298 | osmo_fsm_inst_update_id(fi, fsm_id); |
| 299 | |
| 300 | ep = talloc_zero(fi, struct osmo_mgcpc_ep); |
| 301 | OSMO_ASSERT(ep); |
| 302 | |
| 303 | *ep = (struct osmo_mgcpc_ep){ |
| 304 | .mgcp_client = mgcp_client, |
| 305 | .fi = fi, |
| 306 | .T_defs = T_defs, |
| 307 | }; |
Neels Hofmeyr | f2bf8dc | 2019-10-29 17:39:56 +0100 | [diff] [blame] | 308 | INIT_LLIST_HEAD(&ep->background_notify); |
Neels Hofmeyr | 538d2c5 | 2019-01-28 03:51:35 +0100 | [diff] [blame] | 309 | fi->priv = ep; |
| 310 | |
| 311 | va_start(ap, endpoint_str_fmt); |
| 312 | rc = vsnprintf(ep->endpoint, sizeof(ep->endpoint), endpoint_str_fmt ? : "", ap); |
| 313 | va_end(ap); |
| 314 | |
| 315 | if (rc <= 0 || rc >= sizeof(ep->endpoint)) { |
| 316 | LOG_MGCPC_EP(ep, LOGL_ERROR, "Endpoint name too long or too short: %s\n", |
| 317 | ep->endpoint); |
| 318 | osmo_fsm_inst_term(ep->fi, OSMO_FSM_TERM_ERROR, 0); |
| 319 | return NULL; |
| 320 | } |
| 321 | |
| 322 | return ep; |
| 323 | } |
| 324 | |
| 325 | /*! Add a connection to an endpoint. |
| 326 | * Allocate a connection identifier slot in the osmo_mgcpc_ep instance, do not yet dispatch a CRCX. |
| 327 | * The CRCX is dispatched only upon the first osmo_mgcpc_ep_ci_request(). |
| 328 | * \param ep Parent endpoint instance. |
| 329 | * \param label_fmt Label for logging. |
| 330 | */ |
| 331 | struct osmo_mgcpc_ep_ci *osmo_mgcpc_ep_ci_add(struct osmo_mgcpc_ep *ep, |
| 332 | const char *label_fmt, ...) |
| 333 | { |
| 334 | va_list ap; |
| 335 | int i; |
| 336 | struct osmo_mgcpc_ep_ci *ci; |
| 337 | |
| 338 | for (i = 0; i < USABLE_CI; i++) { |
| 339 | ci = &ep->ci[i]; |
| 340 | |
| 341 | if (ci->occupied || ci->mgcp_client_fi) |
| 342 | continue; |
| 343 | |
| 344 | *ci = (struct osmo_mgcpc_ep_ci){ |
| 345 | .ep = ep, |
| 346 | .occupied = true, |
| 347 | }; |
| 348 | if (label_fmt) { |
| 349 | va_start(ap, label_fmt); |
| 350 | vsnprintf(ci->label, sizeof(ci->label), label_fmt, ap); |
| 351 | va_end(ap); |
| 352 | } |
| 353 | return ci; |
| 354 | } |
| 355 | |
| 356 | LOG_MGCPC_EP(ep, LOGL_ERROR, |
| 357 | "Cannot allocate another endpoint, all " |
| 358 | OSMO_STRINGIFY_VAL(USABLE_CI) " are in use\n"); |
| 359 | |
| 360 | return NULL; |
| 361 | } |
| 362 | |
| 363 | static bool osmo_mgcpc_ep_fsm_check_state_chg_after_response(struct osmo_fsm_inst *fi); |
| 364 | |
| 365 | static void on_failure(struct osmo_mgcpc_ep_ci *ci) |
| 366 | { |
Neels Hofmeyr | cc0b97e | 2019-10-01 19:44:10 +0200 | [diff] [blame] | 367 | struct osmo_mgcpc_ep *ep = ci->ep; |
Neels Hofmeyr | 3ff7128 | 2019-10-29 17:41:20 +0100 | [diff] [blame] | 368 | struct fsm_notify notify; |
Neels Hofmeyr | cc0b97e | 2019-10-01 19:44:10 +0200 | [diff] [blame] | 369 | int i; |
Neels Hofmeyr | 538d2c5 | 2019-01-28 03:51:35 +0100 | [diff] [blame] | 370 | |
| 371 | if (!ci->occupied) |
| 372 | return; |
| 373 | |
Neels Hofmeyr | 3ff7128 | 2019-10-29 17:41:20 +0100 | [diff] [blame] | 374 | /* When dispatching an event for this CI, the user may decide to trigger the next request for this conn right |
| 375 | * away. So we must be ready with a cleared *ci. Store the notify separately and clear before dispatching. */ |
| 376 | notify = ci->notify; |
Neels Hofmeyr | f2bf8dc | 2019-10-29 17:39:56 +0100 | [diff] [blame] | 377 | /* Register the planned notification in ep->background_notify so we also catch any osmo_mgcpc_ep_cancel_notify() |
| 378 | * that might be triggered between clearing the ci and actually dispatching the event. */ |
| 379 | llist_add(¬ify.entry, &ep->background_notify); |
Neels Hofmeyr | 3ff7128 | 2019-10-29 17:41:20 +0100 | [diff] [blame] | 380 | |
Neels Hofmeyr | 538d2c5 | 2019-01-28 03:51:35 +0100 | [diff] [blame] | 381 | *ci = (struct osmo_mgcpc_ep_ci){ |
| 382 | .ep = ci->ep, |
| 383 | }; |
| 384 | |
Neels Hofmeyr | cc0b97e | 2019-10-01 19:44:10 +0200 | [diff] [blame] | 385 | /* An MGCP failure typically means the endpoint becomes unusable, cancel all pending request (except DLCX). |
| 386 | * Particularly, if two CRCX were scheduled and the first fails, we must no longer dispatch the second CRCX. */ |
| 387 | for (i = 0; i < ARRAY_SIZE(ep->ci); i++) { |
| 388 | struct osmo_mgcpc_ep_ci *other_ci = &ep->ci[i]; |
| 389 | if (other_ci == ci) |
| 390 | continue; |
| 391 | if (!other_ci->occupied) |
| 392 | continue; |
| 393 | if (!other_ci->pending) |
| 394 | continue; |
| 395 | if (other_ci->sent) |
| 396 | continue; |
| 397 | if (other_ci->verb == MGCP_VERB_DLCX) |
| 398 | continue; |
| 399 | /* Just clear the pending request, don't fire more events than below. */ |
| 400 | other_ci->pending = false; |
| 401 | } |
| 402 | |
Neels Hofmeyr | 538d2c5 | 2019-01-28 03:51:35 +0100 | [diff] [blame] | 403 | /* If this check has terminated the FSM instance, don't fire any more events to prevent use-after-free problems. |
| 404 | * The endpoint FSM does dispatch a term event to its parent, and everything should be cleaned like that. */ |
Neels Hofmeyr | f2bf8dc | 2019-10-29 17:39:56 +0100 | [diff] [blame] | 405 | if (!osmo_mgcpc_ep_fsm_check_state_chg_after_response(ep->fi)) { |
| 406 | /* The ep has deallocated, no need to llist_del(¬ify.entry) here. */ |
Neels Hofmeyr | 538d2c5 | 2019-01-28 03:51:35 +0100 | [diff] [blame] | 407 | return; |
Neels Hofmeyr | f2bf8dc | 2019-10-29 17:39:56 +0100 | [diff] [blame] | 408 | } |
Neels Hofmeyr | 538d2c5 | 2019-01-28 03:51:35 +0100 | [diff] [blame] | 409 | |
Neels Hofmeyr | 3ff7128 | 2019-10-29 17:41:20 +0100 | [diff] [blame] | 410 | if (notify.fi) |
| 411 | osmo_fsm_inst_dispatch(notify.fi, notify.failure, notify.data); |
Neels Hofmeyr | f2bf8dc | 2019-10-29 17:39:56 +0100 | [diff] [blame] | 412 | |
| 413 | llist_del(¬ify.entry); |
Neels Hofmeyr | 538d2c5 | 2019-01-28 03:51:35 +0100 | [diff] [blame] | 414 | } |
| 415 | |
Neels Hofmeyr | c5479fe | 2019-04-05 01:36:06 +0200 | [diff] [blame] | 416 | static int update_endpoint_name(struct osmo_mgcpc_ep_ci *ci, const char *new_endpoint_name) |
| 417 | { |
| 418 | struct osmo_mgcpc_ep *ep = ci->ep; |
| 419 | int rc; |
| 420 | int i; |
| 421 | |
| 422 | if (!strcmp(ep->endpoint, new_endpoint_name)) { |
| 423 | /* Same endpoint name, nothing to do. */ |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | /* The endpoint name should only change on the very first CRCX response. */ |
| 428 | if (ep->first_crcx_complete) { |
| 429 | LOG_CI(ci, LOGL_ERROR, "Reponse returned mismatching endpoint name." |
| 430 | " This is endpoint %s, instead received %s\n", |
| 431 | ep->endpoint, new_endpoint_name); |
| 432 | on_failure(ci); |
| 433 | return -EINVAL; |
| 434 | } |
| 435 | |
| 436 | /* This is the first CRCX response, update endpoint name. */ |
| 437 | rc = OSMO_STRLCPY_ARRAY(ep->endpoint, new_endpoint_name); |
| 438 | if (rc <= 0 || rc >= sizeof(ep->endpoint)) { |
| 439 | LOG_CI(ci, LOGL_ERROR, "Unable to copy endpoint name %s\n", osmo_quote_str(new_endpoint_name, -1)); |
| 440 | osmo_mgcpc_ep_ci_dlcx(ci); |
| 441 | on_failure(ci); |
| 442 | return -ENOSPC; |
| 443 | } |
| 444 | |
| 445 | /* Make sure already pending requests use this updated endpoint name. */ |
| 446 | for (i = 0; i < ARRAY_SIZE(ep->ci); i++) { |
| 447 | struct osmo_mgcpc_ep_ci *other_ci = &ep->ci[i]; |
| 448 | if (!other_ci->occupied) |
| 449 | continue; |
| 450 | if (!other_ci->pending) |
| 451 | continue; |
| 452 | if (other_ci->sent) |
| 453 | continue; |
| 454 | OSMO_STRLCPY_ARRAY(other_ci->verb_info.endpoint, ep->endpoint); |
| 455 | } |
| 456 | return 0; |
| 457 | } |
| 458 | |
Neels Hofmeyr | 538d2c5 | 2019-01-28 03:51:35 +0100 | [diff] [blame] | 459 | static void on_success(struct osmo_mgcpc_ep_ci *ci, void *data) |
| 460 | { |
| 461 | struct mgcp_conn_peer *rtp_info; |
| 462 | |
| 463 | if (!ci->occupied) |
| 464 | return; |
| 465 | |
| 466 | ci->pending = false; |
| 467 | |
| 468 | switch (ci->verb) { |
| 469 | case MGCP_VERB_CRCX: |
| 470 | /* If we sent a wildcarded endpoint name on CRCX, we need to store the resulting endpoint |
| 471 | * name here. Also, we receive the MGW's RTP port information. */ |
| 472 | rtp_info = data; |
| 473 | OSMO_ASSERT(rtp_info); |
| 474 | ci->got_port_info = true; |
| 475 | ci->rtp_info = *rtp_info; |
| 476 | osmo_strlcpy(ci->mgcp_ci_str, mgcp_conn_get_ci(ci->mgcp_client_fi), |
| 477 | sizeof(ci->mgcp_ci_str)); |
| 478 | if (rtp_info->endpoint[0]) { |
Neels Hofmeyr | c5479fe | 2019-04-05 01:36:06 +0200 | [diff] [blame] | 479 | /* On errors, this instance might already be deallocated. Make sure to not access anything after |
| 480 | * error. */ |
| 481 | if (update_endpoint_name(ci, rtp_info->endpoint)) |
Neels Hofmeyr | 538d2c5 | 2019-01-28 03:51:35 +0100 | [diff] [blame] | 482 | return; |
Neels Hofmeyr | 538d2c5 | 2019-01-28 03:51:35 +0100 | [diff] [blame] | 483 | } |
Neels Hofmeyr | c5479fe | 2019-04-05 01:36:06 +0200 | [diff] [blame] | 484 | ci->ep->first_crcx_complete = true; |
Neels Hofmeyr | 538d2c5 | 2019-01-28 03:51:35 +0100 | [diff] [blame] | 485 | break; |
| 486 | |
| 487 | default: |
| 488 | break; |
| 489 | } |
| 490 | |
| 491 | LOG_CI(ci, LOGL_DEBUG, "received successful response to %s: RTP=%s%s\n", |
| 492 | osmo_mgcp_verb_name(ci->verb), |
| 493 | mgcp_conn_peer_name(ci->got_port_info? &ci->rtp_info : NULL), |
Neels Hofmeyr | 3ff7128 | 2019-10-29 17:41:20 +0100 | [diff] [blame] | 494 | ci->notify.fi ? "" : " (not sending a notification)"); |
Neels Hofmeyr | 538d2c5 | 2019-01-28 03:51:35 +0100 | [diff] [blame] | 495 | |
Neels Hofmeyr | 3ff7128 | 2019-10-29 17:41:20 +0100 | [diff] [blame] | 496 | if (ci->notify.fi) |
| 497 | osmo_fsm_inst_dispatch(ci->notify.fi, ci->notify.success, ci->notify.data); |
Neels Hofmeyr | 538d2c5 | 2019-01-28 03:51:35 +0100 | [diff] [blame] | 498 | |
| 499 | osmo_mgcpc_ep_fsm_check_state_chg_after_response(ci->ep->fi); |
| 500 | } |
| 501 | |
| 502 | /*! Return the MGW's RTP port information for this connection, as returned by the last CRCX/MDCX OK message. */ |
| 503 | const struct mgcp_conn_peer *osmo_mgcpc_ep_ci_get_rtp_info(const struct osmo_mgcpc_ep_ci *ci) |
| 504 | { |
| 505 | ci = osmo_mgcpc_ep_check_ci((struct osmo_mgcpc_ep_ci*)ci); |
| 506 | if (!ci) |
| 507 | return NULL; |
| 508 | if (!ci->got_port_info) |
| 509 | return NULL; |
| 510 | return &ci->rtp_info; |
| 511 | } |
| 512 | |
| 513 | /*! Return the MGW's RTP port information for this connection, as returned by the last CRCX/MDCX OK message. */ |
| 514 | bool osmo_mgcpc_ep_ci_get_crcx_info_to_sockaddr(const struct osmo_mgcpc_ep_ci *ci, struct sockaddr_storage *dest) |
| 515 | { |
| 516 | const struct mgcp_conn_peer *rtp_info; |
| 517 | struct sockaddr_in *sin; |
| 518 | |
| 519 | rtp_info = osmo_mgcpc_ep_ci_get_rtp_info(ci); |
| 520 | if (!rtp_info) |
| 521 | return false; |
| 522 | |
| 523 | sin = (struct sockaddr_in *)dest; |
| 524 | |
| 525 | sin->sin_family = AF_INET; |
| 526 | sin->sin_addr.s_addr = inet_addr(rtp_info->addr); |
| 527 | sin->sin_port = osmo_ntohs(rtp_info->port); |
| 528 | return true; |
| 529 | } |
| 530 | |
Pau Espin Pedrol | 30907dc | 2019-05-06 11:54:17 +0200 | [diff] [blame] | 531 | bool osmo_mgcpc_ep_ci_get_crcx_info_to_osmux_cid(const struct osmo_mgcpc_ep_ci *ci, uint8_t* cid) |
| 532 | { |
| 533 | const struct mgcp_conn_peer *rtp_info; |
| 534 | |
| 535 | rtp_info = osmo_mgcpc_ep_ci_get_rtp_info(ci); |
| 536 | if (!rtp_info) |
| 537 | return false; |
| 538 | |
| 539 | if (!rtp_info->x_osmo_osmux_use) |
| 540 | return false; |
| 541 | |
| 542 | *cid = rtp_info->x_osmo_osmux_cid; |
| 543 | return true; |
| 544 | } |
Neels Hofmeyr | 538d2c5 | 2019-01-28 03:51:35 +0100 | [diff] [blame] | 545 | |
| 546 | static const struct osmo_tdef_state_timeout osmo_mgcpc_ep_fsm_timeouts[32] = { |
| 547 | [OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE] = { .T=2427001 }, |
| 548 | }; |
| 549 | |
| 550 | /* Transition to a state, using the T timer defined in assignment_fsm_timeouts. |
| 551 | * The actual timeout value is in turn obtained from osmo_mgcpc_ep.T_defs. |
| 552 | * Assumes local variable fi exists. */ |
| 553 | #define osmo_mgcpc_ep_fsm_state_chg(state) \ |
| 554 | osmo_tdef_fsm_inst_state_chg(fi, state, osmo_mgcpc_ep_fsm_timeouts, \ |
| 555 | ((struct osmo_mgcpc_ep*)fi->priv)->T_defs, 5) |
| 556 | |
| 557 | /*! Dispatch an actual CRCX/MDCX/DLCX message for this connection. |
Neels Hofmeyr | f2bf8dc | 2019-10-29 17:39:56 +0100 | [diff] [blame] | 558 | * |
| 559 | * If the 'notify' instance deallocates before it received a notification of event_success or event_failure, |
| 560 | * osmo_mgcpc_ep_ci_cancel_notify() or osmo_mgcpc_ep_cancel_notify() must be called. It is not harmful to cancel |
| 561 | * notification after an event has been received. |
| 562 | * |
Neels Hofmeyr | 538d2c5 | 2019-01-28 03:51:35 +0100 | [diff] [blame] | 563 | * \param ci Connection identifier as obtained from osmo_mgcpc_ep_ci_add(). |
| 564 | * \param verb MGCP operation to dispatch. |
| 565 | * \param verb_info Parameters for the MGCP operation. |
| 566 | * \param notify Peer FSM instance to notify of completed/failed operation. |
| 567 | * \param event_success Which event to dispatch to 'notify' upon OK response. |
| 568 | * \param event_failure Which event to dispatch to 'notify' upon failure response. |
| 569 | * \param notify_data Data pointer to pass to the event dispatch for both success and failure. |
| 570 | */ |
| 571 | void osmo_mgcpc_ep_ci_request(struct osmo_mgcpc_ep_ci *ci, |
| 572 | enum mgcp_verb verb, const struct mgcp_conn_peer *verb_info, |
| 573 | struct osmo_fsm_inst *notify, |
| 574 | uint32_t event_success, uint32_t event_failure, |
| 575 | void *notify_data) |
| 576 | { |
| 577 | struct osmo_mgcpc_ep *ep; |
| 578 | struct osmo_fsm_inst *fi; |
| 579 | struct osmo_mgcpc_ep_ci cleared_ci; |
| 580 | ci = osmo_mgcpc_ep_check_ci(ci); |
| 581 | |
| 582 | if (!ci) { |
| 583 | LOGP(DLGLOBAL, LOGL_ERROR, "Invalid MGW endpoint request: no ci\n"); |
| 584 | goto dispatch_error; |
| 585 | } |
| 586 | if (!verb_info && verb != MGCP_VERB_DLCX) { |
| 587 | LOG_CI(ci, LOGL_ERROR, "Invalid MGW endpoint request: missing verb details for %s\n", |
| 588 | osmo_mgcp_verb_name(verb)); |
| 589 | goto dispatch_error; |
| 590 | } |
| 591 | if ((verb < 0) || (verb > MGCP_VERB_RSIP)) { |
| 592 | LOG_CI(ci, LOGL_ERROR, "Invalid MGW endpoint request: unknown verb: %s\n", |
| 593 | osmo_mgcp_verb_name(verb)); |
| 594 | goto dispatch_error; |
| 595 | } |
| 596 | |
| 597 | ep = ci->ep; |
| 598 | fi = ep->fi; |
| 599 | |
| 600 | /* Clear volatile state by explicitly keeping those that should remain. Because we can't assign |
| 601 | * the char[] directly, dance through cleared_ci and copy back. */ |
| 602 | cleared_ci = (struct osmo_mgcpc_ep_ci){ |
| 603 | .ep = ep, |
| 604 | .mgcp_client_fi = ci->mgcp_client_fi, |
| 605 | .got_port_info = ci->got_port_info, |
| 606 | .rtp_info = ci->rtp_info, |
| 607 | |
| 608 | .occupied = true, |
| 609 | /* .pending = true follows below */ |
| 610 | .verb = verb, |
Neels Hofmeyr | 3ff7128 | 2019-10-29 17:41:20 +0100 | [diff] [blame] | 611 | .notify = { |
| 612 | .fi = notify, |
| 613 | .success = event_success, |
| 614 | .failure = event_failure, |
| 615 | .data = notify_data, |
| 616 | } |
Neels Hofmeyr | 538d2c5 | 2019-01-28 03:51:35 +0100 | [diff] [blame] | 617 | }; |
| 618 | osmo_strlcpy(cleared_ci.label, ci->label, sizeof(cleared_ci.label)); |
| 619 | osmo_strlcpy(cleared_ci.mgcp_ci_str, ci->mgcp_ci_str, sizeof(cleared_ci.mgcp_ci_str)); |
| 620 | *ci = cleared_ci; |
| 621 | |
Neels Hofmeyr | 3ff7128 | 2019-10-29 17:41:20 +0100 | [diff] [blame] | 622 | LOG_CI_VERB(ci, LOGL_DEBUG, "notify=%s\n", osmo_fsm_inst_name(ci->notify.fi)); |
Neels Hofmeyr | 538d2c5 | 2019-01-28 03:51:35 +0100 | [diff] [blame] | 623 | |
| 624 | if (verb_info) |
| 625 | ci->verb_info = *verb_info; |
| 626 | |
| 627 | if (ep->endpoint[0]) { |
| 628 | if (ci->verb_info.endpoint[0] && strcmp(ci->verb_info.endpoint, ep->endpoint)) |
| 629 | LOG_CI(ci, LOGL_ERROR, |
| 630 | "Warning: Requested %s on endpoint %s, but this CI is on endpoint %s." |
| 631 | " Using the proper endpoint instead.\n", |
| 632 | osmo_mgcp_verb_name(verb), ci->verb_info.endpoint, ep->endpoint); |
| 633 | osmo_strlcpy(ci->verb_info.endpoint, ep->endpoint, sizeof(ci->verb_info.endpoint)); |
| 634 | } |
| 635 | |
| 636 | switch (ci->verb) { |
| 637 | case MGCP_VERB_CRCX: |
| 638 | if (ci->mgcp_client_fi) { |
| 639 | LOG_CI(ci, LOGL_ERROR, "CRCX can be called only once per MGW endpoint CI\n"); |
| 640 | on_failure(ci); |
| 641 | return; |
| 642 | } |
| 643 | break; |
| 644 | |
| 645 | case MGCP_VERB_MDCX: |
| 646 | if (!ci->mgcp_client_fi) { |
| 647 | LOG_CI_VERB(ci, LOGL_ERROR, "The first verb on an unused MGW endpoint CI must be CRCX, not %s\n", |
| 648 | osmo_mgcp_verb_name(ci->verb)); |
| 649 | on_failure(ci); |
| 650 | return; |
| 651 | } |
| 652 | break; |
| 653 | |
| 654 | case MGCP_VERB_DLCX: |
| 655 | if (!ci->mgcp_client_fi) { |
| 656 | LOG_CI_VERB(ci, LOGL_DEBUG, "Ignoring DLCX on unused MGW endpoint CI\n"); |
| 657 | return; |
| 658 | } |
| 659 | break; |
| 660 | |
| 661 | default: |
| 662 | LOG_CI(ci, LOGL_ERROR, "This verb is not supported: %s\n", osmo_mgcp_verb_name(ci->verb)); |
| 663 | on_failure(ci); |
| 664 | return; |
| 665 | } |
| 666 | |
| 667 | ci->pending = true; |
| 668 | |
| 669 | LOG_CI_VERB(ci, LOGL_DEBUG, "Scheduling\n"); |
| 670 | |
| 671 | if (ep->fi->state != OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE) |
| 672 | osmo_mgcpc_ep_fsm_state_chg(OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE); |
| 673 | |
| 674 | return; |
| 675 | dispatch_error: |
| 676 | if (notify) |
| 677 | osmo_fsm_inst_dispatch(notify, event_failure, notify_data); |
| 678 | } |
| 679 | |
Neels Hofmeyr | f2bf8dc | 2019-10-29 17:39:56 +0100 | [diff] [blame] | 680 | /*! No longer notify for any state changes for any conns of this endpoint. |
| 681 | * Useful if the notify instance passed to osmo_mgcpc_ep_ci_request() is about to deallocate. |
| 682 | * \param ep The endpoint FSM instance. |
| 683 | * \param notify Which target to cancel notification for, if NULL cancel all notifications. */ |
| 684 | void osmo_mgcpc_ep_cancel_notify(struct osmo_mgcpc_ep *ep, struct osmo_fsm_inst *notify) |
| 685 | { |
| 686 | struct fsm_notify *n; |
| 687 | int i; |
| 688 | for (i = 0; i < ARRAY_SIZE(ep->ci); i++) { |
| 689 | struct osmo_mgcpc_ep_ci *ci = &ep->ci[i]; |
| 690 | if (!notify || ci->notify.fi == notify) |
| 691 | ci->notify.fi = NULL; |
| 692 | } |
| 693 | llist_for_each_entry(n, &ep->background_notify, entry) { |
| 694 | if (!notify || n->fi == notify) |
| 695 | n->fi = NULL; |
| 696 | } |
| 697 | |
| 698 | } |
| 699 | |
Neels Hofmeyr | 923d60b | 2019-10-29 17:40:08 +0100 | [diff] [blame] | 700 | /* Return the osmo_mgcpc_ep that this conn belongs to. */ |
| 701 | struct osmo_mgcpc_ep *osmo_mgcpc_ep_ci_ep(struct osmo_mgcpc_ep_ci *conn) |
| 702 | { |
| 703 | if (!conn) |
| 704 | return NULL; |
| 705 | return conn->ep; |
| 706 | } |
| 707 | |
Neels Hofmeyr | 538d2c5 | 2019-01-28 03:51:35 +0100 | [diff] [blame] | 708 | static int send_verb(struct osmo_mgcpc_ep_ci *ci) |
| 709 | { |
| 710 | int rc; |
| 711 | struct osmo_mgcpc_ep *ep = ci->ep; |
Neels Hofmeyr | 055ded7 | 2019-10-29 17:46:30 +0100 | [diff] [blame] | 712 | struct fsm_notify notify; |
Neels Hofmeyr | 538d2c5 | 2019-01-28 03:51:35 +0100 | [diff] [blame] | 713 | |
| 714 | if (!ci->occupied || !ci->pending || ci->sent) |
| 715 | return 0; |
| 716 | |
| 717 | switch (ci->verb) { |
| 718 | |
| 719 | case MGCP_VERB_CRCX: |
| 720 | OSMO_ASSERT(!ci->mgcp_client_fi); |
| 721 | LOG_CI_VERB(ci, LOGL_DEBUG, "Sending\n"); |
| 722 | ci->mgcp_client_fi = mgcp_conn_create(ep->mgcp_client, ep->fi, |
| 723 | CI_EV_FAILURE(ci), CI_EV_SUCCESS(ci), |
| 724 | &ci->verb_info); |
| 725 | ci->sent = true; |
| 726 | if (!ci->mgcp_client_fi){ |
| 727 | LOG_CI_VERB(ci, LOGL_ERROR, "Cannot send\n"); |
| 728 | on_failure(ci); |
Neels Hofmeyr | c5479fe | 2019-04-05 01:36:06 +0200 | [diff] [blame] | 729 | return -EINVAL; |
Neels Hofmeyr | 538d2c5 | 2019-01-28 03:51:35 +0100 | [diff] [blame] | 730 | } |
| 731 | osmo_fsm_inst_update_id(ci->mgcp_client_fi, ci->label); |
| 732 | break; |
| 733 | |
| 734 | case MGCP_VERB_MDCX: |
| 735 | OSMO_ASSERT(ci->mgcp_client_fi); |
| 736 | LOG_CI_VERB(ci, LOGL_DEBUG, "Sending\n"); |
| 737 | rc = mgcp_conn_modify(ci->mgcp_client_fi, CI_EV_SUCCESS(ci), &ci->verb_info); |
| 738 | ci->sent = true; |
| 739 | if (rc) { |
| 740 | LOG_CI_VERB(ci, LOGL_ERROR, "Cannot send (rc=%d %s)\n", rc, strerror(-rc)); |
| 741 | on_failure(ci); |
Neels Hofmeyr | c5479fe | 2019-04-05 01:36:06 +0200 | [diff] [blame] | 742 | return -EINVAL; |
Neels Hofmeyr | 538d2c5 | 2019-01-28 03:51:35 +0100 | [diff] [blame] | 743 | } |
| 744 | break; |
| 745 | |
| 746 | case MGCP_VERB_DLCX: |
| 747 | LOG_CI(ci, LOGL_DEBUG, "Sending MGCP: %s %s\n", |
| 748 | osmo_mgcp_verb_name(ci->verb), ci->mgcp_ci_str); |
| 749 | /* The way this is designed, we actually need to forget all about the ci right away. */ |
| 750 | mgcp_conn_delete(ci->mgcp_client_fi); |
Neels Hofmeyr | 055ded7 | 2019-10-29 17:46:30 +0100 | [diff] [blame] | 751 | notify = ci->notify; |
Neels Hofmeyr | 538d2c5 | 2019-01-28 03:51:35 +0100 | [diff] [blame] | 752 | *ci = (struct osmo_mgcpc_ep_ci){ |
| 753 | .ep = ep, |
| 754 | }; |
Neels Hofmeyr | 055ded7 | 2019-10-29 17:46:30 +0100 | [diff] [blame] | 755 | /* When dispatching an event for this CI, the user may decide to trigger the next request for this conn |
| 756 | * right away. So we must be ready with a cleared *ci. */ |
| 757 | if (notify.fi) |
| 758 | osmo_fsm_inst_dispatch(notify.fi, notify.success, notify.data); |
Neels Hofmeyr | 538d2c5 | 2019-01-28 03:51:35 +0100 | [diff] [blame] | 759 | break; |
| 760 | |
| 761 | default: |
| 762 | OSMO_ASSERT(false); |
| 763 | } |
| 764 | |
| 765 | return 1; |
| 766 | } |
| 767 | |
| 768 | /*! DLCX all connections, terminate the endpoint FSM and free. */ |
| 769 | void osmo_mgcpc_ep_clear(struct osmo_mgcpc_ep *ep) |
| 770 | { |
| 771 | if (!ep) |
| 772 | return; |
Neels Hofmeyr | f2bf8dc | 2019-10-29 17:39:56 +0100 | [diff] [blame] | 773 | osmo_mgcpc_ep_cancel_notify(ep, NULL); |
Neels Hofmeyr | 538d2c5 | 2019-01-28 03:51:35 +0100 | [diff] [blame] | 774 | osmo_fsm_inst_term(ep->fi, OSMO_FSM_TERM_REGULAR, 0); |
| 775 | } |
| 776 | |
| 777 | static void osmo_mgcpc_ep_count(struct osmo_mgcpc_ep *ep, int *occupied, int *pending_not_sent, |
| 778 | int *waiting_for_response) |
| 779 | { |
| 780 | int i; |
| 781 | |
| 782 | if (occupied) |
| 783 | *occupied = 0; |
| 784 | |
| 785 | if (pending_not_sent) |
| 786 | *pending_not_sent = 0; |
| 787 | |
| 788 | if (waiting_for_response) |
| 789 | *waiting_for_response = 0; |
| 790 | |
| 791 | for (i = 0; i < ARRAY_SIZE(ep->ci); i++) { |
| 792 | struct osmo_mgcpc_ep_ci *ci = &ep->ci[i]; |
| 793 | if (ci->occupied) { |
| 794 | if (occupied) |
| 795 | (*occupied)++; |
| 796 | } else |
| 797 | continue; |
| 798 | |
| 799 | if (ci->pending) |
| 800 | LOG_CI_VERB(ci, LOGL_DEBUG, "%s\n", |
| 801 | ci->sent ? "waiting for response" : "waiting to be sent"); |
| 802 | else |
| 803 | LOG_CI_VERB(ci, LOGL_DEBUG, "done (%s)\n", mgcp_conn_peer_name(osmo_mgcpc_ep_ci_get_rtp_info(ci))); |
| 804 | |
| 805 | if (ci->pending && ci->sent) |
| 806 | if (waiting_for_response) |
| 807 | (*waiting_for_response)++; |
| 808 | if (ci->pending && !ci->sent) |
| 809 | if (pending_not_sent) |
| 810 | (*pending_not_sent)++; |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | static bool osmo_mgcpc_ep_fsm_check_state_chg_after_response(struct osmo_fsm_inst *fi) |
| 815 | { |
| 816 | int waiting_for_response; |
| 817 | int occupied; |
| 818 | struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi); |
| 819 | |
| 820 | osmo_mgcpc_ep_count(ep, &occupied, NULL, &waiting_for_response); |
| 821 | LOG_MGCPC_EP(ep, LOGL_DEBUG, "CI in use: %d, waiting for response: %d\n", occupied, waiting_for_response); |
| 822 | |
| 823 | if (!occupied) { |
| 824 | /* All CI have been released. The endpoint no longer exists. Notify the parent FSM, by |
| 825 | * terminating. */ |
| 826 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, 0); |
| 827 | return false; |
| 828 | } |
| 829 | |
| 830 | if (!waiting_for_response) { |
| 831 | if (fi->state != OSMO_MGCPC_EP_ST_IN_USE) |
| 832 | osmo_mgcpc_ep_fsm_state_chg(OSMO_MGCPC_EP_ST_IN_USE); |
| 833 | } |
| 834 | |
| 835 | return true; |
| 836 | } |
| 837 | |
| 838 | static void osmo_mgcpc_ep_fsm_wait_mgw_response_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 839 | { |
Neels Hofmeyr | c5479fe | 2019-04-05 01:36:06 +0200 | [diff] [blame] | 840 | static int re_enter = 0; |
| 841 | int rc; |
Neels Hofmeyr | 538d2c5 | 2019-01-28 03:51:35 +0100 | [diff] [blame] | 842 | int count = 0; |
| 843 | int i; |
| 844 | struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi); |
| 845 | |
Neels Hofmeyr | c5479fe | 2019-04-05 01:36:06 +0200 | [diff] [blame] | 846 | re_enter++; |
| 847 | OSMO_ASSERT(re_enter < 10); |
| 848 | |
| 849 | /* The first CRCX gives us the endpoint name in the CRCX response. So we must wait for the first CRCX endpoint |
| 850 | * response to come in before sending any other MGCP requests -- otherwise we might end up creating new |
| 851 | * endpoints instead of acting on the same. This FSM always sends out N requests and waits for all of them to |
| 852 | * complete before sending out new requests. Hence we're safe when the very first time at most one request is |
| 853 | * sent (which needs to be a CRCX). */ |
| 854 | |
Neels Hofmeyr | 538d2c5 | 2019-01-28 03:51:35 +0100 | [diff] [blame] | 855 | for (i = 0; i < ARRAY_SIZE(ep->ci); i++) { |
Neels Hofmeyr | c5479fe | 2019-04-05 01:36:06 +0200 | [diff] [blame] | 856 | struct osmo_mgcpc_ep_ci *ci = &ep->ci[i]; |
| 857 | |
| 858 | /* Make sure that only CRCX get dispatched if no CRCX were sent yet. */ |
| 859 | if (!ep->first_crcx_complete) { |
| 860 | if (ci->occupied && ci->verb != MGCP_VERB_CRCX) |
| 861 | continue; |
| 862 | } |
| 863 | |
| 864 | rc = send_verb(&ep->ci[i]); |
| 865 | /* Need to be careful not to access the instance after failure. Event chains may already have |
| 866 | * deallocated this memory. */ |
| 867 | if (rc < 0) |
| 868 | return; |
| 869 | if (!rc) |
| 870 | continue; |
| 871 | count++; |
| 872 | /* Make sure that we wait for the first CRCX response before dispatching more requests. */ |
| 873 | if (!ep->first_crcx_complete) |
| 874 | break; |
Neels Hofmeyr | 538d2c5 | 2019-01-28 03:51:35 +0100 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | LOG_MGCPC_EP(ep, LOGL_DEBUG, "Sent messages: %d\n", count); |
Neels Hofmeyr | c5479fe | 2019-04-05 01:36:06 +0200 | [diff] [blame] | 878 | if (ep->first_crcx_complete) |
| 879 | osmo_mgcpc_ep_fsm_check_state_chg_after_response(fi); |
| 880 | re_enter--; |
Neels Hofmeyr | 538d2c5 | 2019-01-28 03:51:35 +0100 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | static void osmo_mgcpc_ep_fsm_handle_ci_events(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 884 | { |
| 885 | struct osmo_mgcpc_ep_ci *ci; |
| 886 | struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi); |
| 887 | ci = osmo_mgcpc_ep_ci_for_event(ep, event); |
| 888 | if (ci) { |
| 889 | if (event == CI_EV_SUCCESS(ci)) |
| 890 | on_success(ci, data); |
| 891 | else |
| 892 | on_failure(ci); |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | static void osmo_mgcpc_ep_fsm_in_use_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 897 | { |
| 898 | int pending_not_sent; |
| 899 | struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi); |
| 900 | |
| 901 | osmo_mgcpc_ep_count(ep, NULL, &pending_not_sent, NULL); |
| 902 | if (pending_not_sent) |
| 903 | osmo_mgcpc_ep_fsm_state_chg(OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE); |
| 904 | } |
| 905 | |
| 906 | #define S(x) (1 << (x)) |
| 907 | |
| 908 | static const struct osmo_fsm_state osmo_mgcpc_ep_fsm_states[] = { |
| 909 | [OSMO_MGCPC_EP_ST_UNUSED] = { |
| 910 | .name = "UNUSED", |
| 911 | .in_event_mask = 0, |
| 912 | .out_state_mask = 0 |
| 913 | | S(OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE) |
| 914 | , |
| 915 | }, |
| 916 | [OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE] = { |
| 917 | .name = "WAIT_MGW_RESPONSE", |
| 918 | .onenter = osmo_mgcpc_ep_fsm_wait_mgw_response_onenter, |
| 919 | .action = osmo_mgcpc_ep_fsm_handle_ci_events, |
| 920 | .in_event_mask = 0xffffffff, |
| 921 | .out_state_mask = 0 |
| 922 | | S(OSMO_MGCPC_EP_ST_IN_USE) |
| 923 | , |
| 924 | }, |
| 925 | [OSMO_MGCPC_EP_ST_IN_USE] = { |
| 926 | .name = "IN_USE", |
| 927 | .onenter = osmo_mgcpc_ep_fsm_in_use_onenter, |
| 928 | .action = osmo_mgcpc_ep_fsm_handle_ci_events, |
| 929 | .in_event_mask = 0xffffffff, /* mgcp_client_fsm may send parent term anytime */ |
| 930 | .out_state_mask = 0 |
| 931 | | S(OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE) |
| 932 | , |
| 933 | }, |
| 934 | }; |
| 935 | |
| 936 | static int osmo_mgcpc_ep_fsm_timer_cb(struct osmo_fsm_inst *fi) |
| 937 | { |
| 938 | int i; |
| 939 | struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi); |
| 940 | |
| 941 | switch (fi->T) { |
| 942 | default: |
| 943 | for (i = 0; i < ARRAY_SIZE(ep->ci); i++) { |
| 944 | struct osmo_mgcpc_ep_ci *ci = &ep->ci[i]; |
| 945 | if (!ci->occupied) |
| 946 | continue; |
| 947 | if (!(ci->pending && ci->sent)) |
| 948 | continue; |
| 949 | on_failure(ci); |
| 950 | } |
| 951 | return 0; |
| 952 | } |
| 953 | |
| 954 | return 0; |
| 955 | } |
| 956 | |
| 957 | static struct osmo_fsm osmo_mgcpc_ep_fsm = { |
Pau Espin Pedrol | 182ca3b | 2019-05-08 14:01:20 +0200 | [diff] [blame] | 958 | .name = "mgw-endp", |
Neels Hofmeyr | 538d2c5 | 2019-01-28 03:51:35 +0100 | [diff] [blame] | 959 | .states = osmo_mgcpc_ep_fsm_states, |
| 960 | .num_states = ARRAY_SIZE(osmo_mgcpc_ep_fsm_states), |
| 961 | .log_subsys = DLMGCP, |
| 962 | .event_names = osmo_mgcpc_ep_fsm_event_names, |
| 963 | .timer_cb = osmo_mgcpc_ep_fsm_timer_cb, |
| 964 | /* The FSM termination will automatically trigger any mgcp_client_fsm instances to DLCX. */ |
| 965 | }; |