Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file fsm.c |
| 2 | * Osmocom generic Finite State Machine implementation. */ |
| 3 | /* |
Harald Welte | 7b74551 | 2019-05-18 21:03:55 +0200 | [diff] [blame] | 4 | * (C) 2016-2019 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 5 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | * |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 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 General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 21 | * MA 02110-1301, USA. |
| 22 | */ |
| 23 | |
| 24 | #include <errno.h> |
| 25 | #include <stdbool.h> |
Harald Welte | 8808bb4 | 2017-01-07 11:11:03 +0100 | [diff] [blame] | 26 | #include <string.h> |
Pau Espin Pedrol | 4f8857e | 2017-06-18 12:23:00 +0200 | [diff] [blame] | 27 | #include <inttypes.h> |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 28 | |
| 29 | #include <osmocom/core/fsm.h> |
| 30 | #include <osmocom/core/talloc.h> |
| 31 | #include <osmocom/core/logging.h> |
| 32 | #include <osmocom/core/utils.h> |
| 33 | |
| 34 | /*! \addtogroup fsm |
| 35 | * @{ |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 36 | * Finite State Machine abstraction |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 37 | * |
| 38 | * This is a generic C-language abstraction for implementing finite |
| 39 | * state machines within the Osmocom framework. It is intended to |
| 40 | * replace existing hand-coded or even only implicitly existing FSMs |
| 41 | * all over the existing code base. |
| 42 | * |
| 43 | * An libosmocore FSM is described by its \ref osmo_fsm description, |
| 44 | * which in turn refers to an array of \ref osmo_fsm_state descriptor, |
| 45 | * each describing a single state in the FSM. |
| 46 | * |
| 47 | * The general idea is that all actions performed within one state are |
| 48 | * located at one position in the code (the state's action function), |
| 49 | * as opposed to the 'message-centric' view of e.g. the existing |
Kévin Redon | c9a28a6 | 2019-05-09 18:31:13 +0200 | [diff] [blame] | 50 | * state machines of the LAPD(m) core, where there is one message for |
| 51 | * each possible event (primitive), and the function then needs to |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 52 | * concern itself on how to handle that event over all possible states. |
| 53 | * |
| 54 | * For each state, there is a bit-mask of permitted input events for |
| 55 | * this state, as well as a bit-mask of permitted new output states to |
| 56 | * which the state can change. Furthermore, there is a function |
| 57 | * pointer implementing the actual handling of the input events |
Kévin Redon | c9a28a6 | 2019-05-09 18:31:13 +0200 | [diff] [blame] | 58 | * occurring whilst in that state. |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 59 | * |
| 60 | * Furthermore, each state offers a function pointer that can be |
| 61 | * executed just before leaving a state, and another one just after |
| 62 | * entering a state. |
| 63 | * |
| 64 | * When transitioning into a new state, an optional timer number and |
| 65 | * time-out can be passed along. The timer is started just after |
| 66 | * entering the new state, and will call the \ref osmo_fsm timer_cb |
| 67 | * function once it expires. This is intended to be used in telecom |
| 68 | * state machines where a given timer (identified by a certain number) |
| 69 | * is started to terminate the fsm or terminate the fsm once expected |
| 70 | * events are not happening before timeout expiration. |
| 71 | * |
| 72 | * As there can often be many concurrent FSMs of one given class, we |
| 73 | * introduce the concept of \ref osmo_fsm_inst, i.e. an FSM instance. |
| 74 | * The instance keeps the actual state, while the \ref osmo_fsm |
| 75 | * descriptor contains the static/const descriptor of the FSM's states |
| 76 | * and possible transitions. |
| 77 | * |
| 78 | * osmo_fsm are integrated with the libosmocore logging system. The |
| 79 | * logging sub-system is determined by the FSM descriptor, as we assume |
| 80 | * one FSM (let's say one related to a location update procedure) is |
| 81 | * inevitably always tied to a sub-system. The logging level however |
| 82 | * is configurable for each FSM instance, to ensure that e.g. DEBUG |
| 83 | * logging can be used for the LU procedure of one subscriber, while |
| 84 | * NOTICE level is used for all other subscribers. |
| 85 | * |
| 86 | * In order to attach private state to the \ref osmo_fsm_inst, it |
Kévin Redon | c9a28a6 | 2019-05-09 18:31:13 +0200 | [diff] [blame] | 87 | * offers an opaque private pointer. |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 88 | * |
| 89 | * \file fsm.c */ |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 90 | |
Harald Welte | 3419391 | 2017-01-07 11:49:55 +0100 | [diff] [blame] | 91 | LLIST_HEAD(osmo_g_fsms); |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 92 | static bool fsm_log_addr = true; |
Neels Hofmeyr | 050f2d3 | 2018-05-31 15:30:15 +0200 | [diff] [blame] | 93 | static bool fsm_log_timeouts = false; |
Neels Hofmeyr | 1f9cc01 | 2019-03-24 05:56:21 +0100 | [diff] [blame] | 94 | /*! See osmo_fsm_term_safely(). */ |
| 95 | static bool fsm_term_safely_enabled = false; |
| 96 | |
| 97 | /*! Internal state for FSM instance termination cascades. */ |
| 98 | static __thread struct { |
| 99 | /*! The first FSM instance that invoked osmo_fsm_inst_term() in the current cascade. */ |
| 100 | struct osmo_fsm_inst *root_fi; |
| 101 | /*! 2 if a secondary FSM terminates, 3 if a secondary FSM causes a tertiary FSM to terminate, and so on. */ |
| 102 | unsigned int depth; |
| 103 | /*! Talloc context to collect all deferred deallocations (FSM instances, and talloc objects if any). */ |
| 104 | void *collect_ctx; |
| 105 | } fsm_term_safely; |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 106 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 107 | /*! specify if FSM instance addresses should be logged or not |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 108 | * |
| 109 | * By default, the FSM name includes the pointer address of the \ref |
Neels Hofmeyr | a3953e0 | 2016-12-14 18:34:30 +0100 | [diff] [blame] | 110 | * osmo_fsm_inst. This behavior can be disabled (and re-enabled) |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 111 | * using this function. |
| 112 | * |
| 113 | * \param[in] log_addr Indicate if FSM instance address shall be logged |
| 114 | */ |
| 115 | void osmo_fsm_log_addr(bool log_addr) |
| 116 | { |
Max | 61281f4 | 2016-11-01 10:49:31 +0100 | [diff] [blame] | 117 | fsm_log_addr = log_addr; |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 118 | } |
| 119 | |
Neels Hofmeyr | 050f2d3 | 2018-05-31 15:30:15 +0200 | [diff] [blame] | 120 | /*! Enable or disable logging of timeout values for FSM instance state changes. |
| 121 | * |
| 122 | * By default, state changes are logged by state name only, omitting the timeout. When passing true, each state change |
Neels Hofmeyr | 5734bff | 2019-02-21 02:27:48 +0100 | [diff] [blame] | 123 | * will also log the T number (or Osmocom-specific X number) and the chosen timeout in seconds. |
| 124 | * osmo_fsm_inst_state_chg_keep_timer() will log remaining timeout in millisecond precision. |
Neels Hofmeyr | 050f2d3 | 2018-05-31 15:30:15 +0200 | [diff] [blame] | 125 | * |
| 126 | * The default for this is false to reflect legacy behavior. Since various C tests that verify logging output already |
| 127 | * existed prior to this option, keeping timeout logging off makes sure that they continue to pass. Particularly, |
| 128 | * osmo_fsm_inst_state_chg_keep_timer() may cause non-deterministic logging of remaining timeout values. |
| 129 | * |
| 130 | * For any program that does not explicitly require deterministic logging output, i.e. anything besides regression tests |
| 131 | * involving FSM instances, it is recommended to call osmo_fsm_log_timeouts(true). |
| 132 | * |
| 133 | * \param[in] log_timeouts Pass true to log timeouts on state transitions, false to omit timeouts. |
| 134 | */ |
| 135 | void osmo_fsm_log_timeouts(bool log_timeouts) |
| 136 | { |
| 137 | fsm_log_timeouts = log_timeouts; |
| 138 | } |
| 139 | |
Neels Hofmeyr | 1f9cc01 | 2019-03-24 05:56:21 +0100 | [diff] [blame] | 140 | /*! Enable safer way to deallocate cascades of terminating FSM instances. |
| 141 | * |
| 142 | * For legacy compatibility, this is disabled by default. In newer programs / releases, it is recommended to enable this |
| 143 | * feature during main() startup, since it greatly simplifies deallocating child, parent and other FSM instances without |
| 144 | * running into double-free or use-after-free scenarios. When enabled, this feature changes the order of logging, which |
| 145 | * may break legacy unit test expectations, and changes the order of deallocation to after the parent term event is |
| 146 | * dispatched. |
| 147 | * |
| 148 | * When enabled, an FSM instance termination detects whether another FSM instance is already terminating, and instead of |
| 149 | * deallocating immediately, collects all terminating FSM instances in a talloc context, to be bulk deallocated once all |
| 150 | * event handling and termination cascades are done. |
| 151 | * |
| 152 | * For example, if an FSM's cleanup() sends an event to some "other" FSM, which in turn causes the FSM's parent to |
| 153 | * deallocate, then the parent would talloc_free() the child's memory, causing a use-after-free. There are infinite |
| 154 | * constellations like this, which all are trivially solved with this feature enabled. |
| 155 | * |
| 156 | * For illustration, see fsm_dealloc_test.c. |
| 157 | * |
| 158 | * \param[in] term_safely Pass true to switch to safer FSM instance termination behavior. |
| 159 | */ |
| 160 | void osmo_fsm_term_safely(bool term_safely) |
| 161 | { |
| 162 | fsm_term_safely_enabled = term_safely; |
| 163 | } |
| 164 | |
| 165 | /*! talloc_free() the given object immediately, or once ongoing FSM terminations are done. |
| 166 | * |
| 167 | * If an FSM deallocation cascade is ongoing, talloc_steal() the given talloc_object into the talloc context that is |
| 168 | * freed once the cascade is done. If no FSM deallocation cascade is ongoing, or if osmo_fsm_term_safely() is disabled, |
| 169 | * immediately talloc_free the object. |
| 170 | * |
| 171 | * This can be useful if some higher order talloc object, which is the talloc parent for FSM instances or their priv |
| 172 | * objects, is not itself tied to an FSM instance. This function allows safely freeing it without affecting ongoing FSM |
| 173 | * termination cascades. |
| 174 | * |
| 175 | * Once passed to this function, the talloc_object should be considered as already freed. Only FSM instance pre_term() |
| 176 | * and cleanup() functions as well as event handling caused by these may safely assume that it is still valid memory. |
| 177 | * |
| 178 | * The talloc_object should not have multiple parents. |
| 179 | * |
| 180 | * (This function may some day move to public API, which might be redundant if we introduce a select-loop volatile |
| 181 | * context mechanism to defer deallocation instead.) |
| 182 | * |
| 183 | * \param[in] talloc_object Object pointer to free. |
| 184 | */ |
| 185 | static void osmo_fsm_defer_free(void *talloc_object) |
| 186 | { |
| 187 | if (!fsm_term_safely.depth) { |
| 188 | talloc_free(talloc_object); |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | if (!fsm_term_safely.collect_ctx) { |
| 193 | /* This is actually the first other object / FSM instance besides the root terminating inst. Create the |
| 194 | * ctx to collect this and possibly more objects to free. Avoid talloc parent loops: don't make this ctx |
| 195 | * the child of the root inst or anything like that. */ |
| 196 | fsm_term_safely.collect_ctx = talloc_named_const(NULL, 0, "fsm_term_safely.collect_ctx"); |
| 197 | OSMO_ASSERT(fsm_term_safely.collect_ctx); |
| 198 | } |
| 199 | talloc_steal(fsm_term_safely.collect_ctx, talloc_object); |
| 200 | } |
| 201 | |
Harald Welte | 8808bb4 | 2017-01-07 11:11:03 +0100 | [diff] [blame] | 202 | struct osmo_fsm *osmo_fsm_find_by_name(const char *name) |
| 203 | { |
| 204 | struct osmo_fsm *fsm; |
Harald Welte | 3419391 | 2017-01-07 11:49:55 +0100 | [diff] [blame] | 205 | llist_for_each_entry(fsm, &osmo_g_fsms, list) { |
Harald Welte | 8808bb4 | 2017-01-07 11:11:03 +0100 | [diff] [blame] | 206 | if (!strcmp(name, fsm->name)) |
| 207 | return fsm; |
| 208 | } |
| 209 | return NULL; |
| 210 | } |
| 211 | |
Harald Welte | 4585e67 | 2017-04-16 17:23:56 +0200 | [diff] [blame] | 212 | struct osmo_fsm_inst *osmo_fsm_inst_find_by_name(const struct osmo_fsm *fsm, |
| 213 | const char *name) |
| 214 | { |
| 215 | struct osmo_fsm_inst *fi; |
| 216 | |
Neels Hofmeyr | 2bcc873 | 2018-04-09 01:35:02 +0200 | [diff] [blame] | 217 | if (!name) |
| 218 | return NULL; |
| 219 | |
Harald Welte | 4585e67 | 2017-04-16 17:23:56 +0200 | [diff] [blame] | 220 | llist_for_each_entry(fi, &fsm->instances, list) { |
Neels Hofmeyr | 2bcc873 | 2018-04-09 01:35:02 +0200 | [diff] [blame] | 221 | if (!fi->name) |
| 222 | continue; |
Harald Welte | 4585e67 | 2017-04-16 17:23:56 +0200 | [diff] [blame] | 223 | if (!strcmp(name, fi->name)) |
| 224 | return fi; |
| 225 | } |
| 226 | return NULL; |
| 227 | } |
| 228 | |
| 229 | struct osmo_fsm_inst *osmo_fsm_inst_find_by_id(const struct osmo_fsm *fsm, |
| 230 | const char *id) |
| 231 | { |
| 232 | struct osmo_fsm_inst *fi; |
| 233 | |
| 234 | llist_for_each_entry(fi, &fsm->instances, list) { |
| 235 | if (!strcmp(id, fi->id)) |
| 236 | return fi; |
| 237 | } |
| 238 | return NULL; |
| 239 | } |
| 240 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 241 | /*! register a FSM with the core |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 242 | * |
| 243 | * A FSM descriptor needs to be registered with the core before any |
| 244 | * instances can be created for it. |
| 245 | * |
| 246 | * \param[in] fsm Descriptor of Finite State Machine to be registered |
| 247 | * \returns 0 on success; negative on error |
| 248 | */ |
| 249 | int osmo_fsm_register(struct osmo_fsm *fsm) |
| 250 | { |
Harald Welte | 8c4f545 | 2017-10-03 17:44:03 +0800 | [diff] [blame] | 251 | if (!osmo_identifier_valid(fsm->name)) { |
| 252 | LOGP(DLGLOBAL, LOGL_ERROR, "Attempting to register FSM with illegal identifier '%s'\n", fsm->name); |
| 253 | return -EINVAL; |
| 254 | } |
Harald Welte | 8808bb4 | 2017-01-07 11:11:03 +0100 | [diff] [blame] | 255 | if (osmo_fsm_find_by_name(fsm->name)) |
| 256 | return -EEXIST; |
Stefan Sperling | 888dc7d | 2018-02-26 19:17:02 +0100 | [diff] [blame] | 257 | if (fsm->event_names == NULL) |
| 258 | LOGP(DLGLOBAL, LOGL_ERROR, "FSM '%s' has no event names! Please fix!\n", fsm->name); |
Harald Welte | 3419391 | 2017-01-07 11:49:55 +0100 | [diff] [blame] | 259 | llist_add_tail(&fsm->list, &osmo_g_fsms); |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 260 | INIT_LLIST_HEAD(&fsm->instances); |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 265 | /*! unregister a FSM from the core |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 266 | * |
| 267 | * Once the FSM descriptor is unregistered, active instances can still |
| 268 | * use it, but no new instances may be created for it. |
| 269 | * |
| 270 | * \param[in] fsm Descriptor of Finite State Machine to be removed |
| 271 | */ |
| 272 | void osmo_fsm_unregister(struct osmo_fsm *fsm) |
| 273 | { |
| 274 | llist_del(&fsm->list); |
| 275 | } |
| 276 | |
| 277 | /* small wrapper function around timer expiration (for logging) */ |
| 278 | static void fsm_tmr_cb(void *data) |
| 279 | { |
| 280 | struct osmo_fsm_inst *fi = data; |
| 281 | struct osmo_fsm *fsm = fi->fsm; |
Neels Hofmeyr | 5734bff | 2019-02-21 02:27:48 +0100 | [diff] [blame] | 282 | int32_t T = fi->T; |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 283 | |
Neels Hofmeyr | 5734bff | 2019-02-21 02:27:48 +0100 | [diff] [blame] | 284 | LOGPFSM(fi, "Timeout of " OSMO_T_FMT "\n", OSMO_T_FMT_ARGS(fi->T)); |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 285 | |
Harald Welte | f627c0f | 2016-06-18 10:36:25 +0200 | [diff] [blame] | 286 | if (fsm->timer_cb) { |
| 287 | int rc = fsm->timer_cb(fi); |
Neels Hofmeyr | 19ec7b9 | 2017-11-18 23:10:24 +0100 | [diff] [blame] | 288 | if (rc != 1) |
| 289 | /* We don't actually know whether fi exists anymore. |
| 290 | * Make sure to not access it and return right away. */ |
Harald Welte | f627c0f | 2016-06-18 10:36:25 +0200 | [diff] [blame] | 291 | return; |
Neels Hofmeyr | 19ec7b9 | 2017-11-18 23:10:24 +0100 | [diff] [blame] | 292 | /* The timer_cb told us to terminate, so we can safely assume |
| 293 | * that fi still exists. */ |
Harald Welte | f627c0f | 2016-06-18 10:36:25 +0200 | [diff] [blame] | 294 | LOGPFSM(fi, "timer_cb requested termination\n"); |
| 295 | } else |
| 296 | LOGPFSM(fi, "No timer_cb, automatic termination\n"); |
| 297 | |
| 298 | /* if timer_cb returns 1 or there is no timer_cb */ |
| 299 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_TIMEOUT, &T); |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 300 | } |
| 301 | |
Daniel Willmann | b0c43a6 | 2018-02-08 18:00:37 +0100 | [diff] [blame] | 302 | /*! Change id of the FSM instance |
| 303 | * \param[in] fi FSM instance |
| 304 | * \param[in] id new ID |
| 305 | * \returns 0 if the ID was updated, otherwise -EINVAL |
| 306 | */ |
| 307 | int osmo_fsm_inst_update_id(struct osmo_fsm_inst *fi, const char *id) |
| 308 | { |
Neels Hofmeyr | a64c45a | 2018-03-31 16:34:49 +0200 | [diff] [blame] | 309 | if (!id) |
| 310 | return osmo_fsm_inst_update_id_f(fi, NULL); |
| 311 | else |
| 312 | return osmo_fsm_inst_update_id_f(fi, "%s", id); |
| 313 | } |
| 314 | |
| 315 | static void update_name(struct osmo_fsm_inst *fi) |
| 316 | { |
| 317 | if (fi->name) |
| 318 | talloc_free((char*)fi->name); |
| 319 | |
| 320 | if (!fsm_log_addr) { |
| 321 | if (fi->id) |
| 322 | fi->name = talloc_asprintf(fi, "%s(%s)", fi->fsm->name, fi->id); |
| 323 | else |
| 324 | fi->name = talloc_asprintf(fi, "%s", fi->fsm->name); |
| 325 | } else { |
| 326 | if (fi->id) |
| 327 | fi->name = talloc_asprintf(fi, "%s(%s)[%p]", fi->fsm->name, fi->id, fi); |
| 328 | else |
| 329 | fi->name = talloc_asprintf(fi, "%s[%p]", fi->fsm->name, fi); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | /*! Change id of the FSM instance using a string format. |
| 334 | * \param[in] fi FSM instance. |
| 335 | * \param[in] fmt format string to compose new ID. |
| 336 | * \param[in] ... variable argument list for format string. |
| 337 | * \returns 0 if the ID was updated, otherwise -EINVAL. |
| 338 | */ |
| 339 | int osmo_fsm_inst_update_id_f(struct osmo_fsm_inst *fi, const char *fmt, ...) |
| 340 | { |
| 341 | char *id = NULL; |
| 342 | |
| 343 | if (fmt) { |
| 344 | va_list ap; |
| 345 | |
| 346 | va_start(ap, fmt); |
| 347 | id = talloc_vasprintf(fi, fmt, ap); |
| 348 | va_end(ap); |
| 349 | |
Daniel Willmann | b0c43a6 | 2018-02-08 18:00:37 +0100 | [diff] [blame] | 350 | if (!osmo_identifier_valid(id)) { |
Neels Hofmeyr | 6e8c088 | 2018-04-09 02:28:34 +0200 | [diff] [blame] | 351 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 352 | "Attempting to set illegal id for FSM instance of type '%s': %s\n", |
| 353 | fi->fsm->name, osmo_quote_str(id, -1)); |
Neels Hofmeyr | a64c45a | 2018-03-31 16:34:49 +0200 | [diff] [blame] | 354 | talloc_free(id); |
Daniel Willmann | b0c43a6 | 2018-02-08 18:00:37 +0100 | [diff] [blame] | 355 | return -EINVAL; |
| 356 | } |
Daniel Willmann | b0c43a6 | 2018-02-08 18:00:37 +0100 | [diff] [blame] | 357 | } |
Daniel Willmann | 04a2a32 | 2018-03-14 18:31:33 +0100 | [diff] [blame] | 358 | |
| 359 | if (fi->id) |
Neels Hofmeyr | a64c45a | 2018-03-31 16:34:49 +0200 | [diff] [blame] | 360 | talloc_free((char*)fi->id); |
| 361 | fi->id = id; |
Daniel Willmann | 04a2a32 | 2018-03-14 18:31:33 +0100 | [diff] [blame] | 362 | |
Neels Hofmeyr | a64c45a | 2018-03-31 16:34:49 +0200 | [diff] [blame] | 363 | update_name(fi); |
Daniel Willmann | 04a2a32 | 2018-03-14 18:31:33 +0100 | [diff] [blame] | 364 | return 0; |
Daniel Willmann | b0c43a6 | 2018-02-08 18:00:37 +0100 | [diff] [blame] | 365 | } |
| 366 | |
Neels Hofmeyr | b480b74 | 2019-04-11 06:58:44 +0200 | [diff] [blame] | 367 | /*! Change id of the FSM instance using a string format, and ensuring a valid id. |
| 368 | * Replace any characters that are not permitted as FSM identifier with replace_with. |
| 369 | * \param[in] fi FSM instance. |
| 370 | * \param[in] replace_with Character to use instead of non-permitted FSM id characters. |
| 371 | * Make sure to choose a legal character, e.g. '-'. |
| 372 | * \param[in] fmt format string to compose new ID. |
| 373 | * \param[in] ... variable argument list for format string. |
| 374 | * \returns 0 if the ID was updated, otherwise -EINVAL. |
| 375 | */ |
| 376 | int osmo_fsm_inst_update_id_f_sanitize(struct osmo_fsm_inst *fi, char replace_with, const char *fmt, ...) |
| 377 | { |
| 378 | char *id = NULL; |
| 379 | va_list ap; |
| 380 | int rc; |
| 381 | |
| 382 | if (!fmt) |
| 383 | return osmo_fsm_inst_update_id(fi, NULL); |
| 384 | |
| 385 | va_start(ap, fmt); |
| 386 | id = talloc_vasprintf(fi, fmt, ap); |
| 387 | va_end(ap); |
| 388 | |
| 389 | osmo_identifier_sanitize_buf(id, NULL, replace_with); |
| 390 | |
| 391 | rc = osmo_fsm_inst_update_id(fi, id); |
| 392 | talloc_free(id); |
| 393 | return rc; |
| 394 | } |
| 395 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 396 | /*! allocate a new instance of a specified FSM |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 397 | * \param[in] fsm Descriptor of the FSM |
| 398 | * \param[in] ctx talloc context from which to allocate memory |
| 399 | * \param[in] priv private data reference store in fsm instance |
| 400 | * \param[in] log_level The log level for events of this FSM |
Daniel Willmann | b0c43a6 | 2018-02-08 18:00:37 +0100 | [diff] [blame] | 401 | * \param[in] id The name/ID of the FSM instance |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 402 | * \returns newly-allocated, initialized and registered FSM instance |
| 403 | */ |
| 404 | struct osmo_fsm_inst *osmo_fsm_inst_alloc(struct osmo_fsm *fsm, void *ctx, void *priv, |
| 405 | int log_level, const char *id) |
| 406 | { |
| 407 | struct osmo_fsm_inst *fi = talloc_zero(ctx, struct osmo_fsm_inst); |
| 408 | |
| 409 | fi->fsm = fsm; |
| 410 | fi->priv = priv; |
| 411 | fi->log_level = log_level; |
Pablo Neira Ayuso | 44f423f | 2017-05-08 18:00:28 +0200 | [diff] [blame] | 412 | osmo_timer_setup(&fi->timer, fsm_tmr_cb, fi); |
Daniel Willmann | b0c43a6 | 2018-02-08 18:00:37 +0100 | [diff] [blame] | 413 | |
Neels Hofmeyr | 71f76a1 | 2018-03-31 16:30:25 +0200 | [diff] [blame] | 414 | if (osmo_fsm_inst_update_id(fi, id) < 0) { |
| 415 | talloc_free(fi); |
| 416 | return NULL; |
Harald Welte | 8c4f545 | 2017-10-03 17:44:03 +0800 | [diff] [blame] | 417 | } |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 418 | |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 419 | INIT_LLIST_HEAD(&fi->proc.children); |
| 420 | INIT_LLIST_HEAD(&fi->proc.child); |
| 421 | llist_add(&fi->list, &fsm->instances); |
| 422 | |
| 423 | LOGPFSM(fi, "Allocated\n"); |
| 424 | |
| 425 | return fi; |
| 426 | } |
| 427 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 428 | /*! allocate a new instance of a specified FSM as child of |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 429 | * other FSM instance |
| 430 | * |
| 431 | * This is like \ref osmo_fsm_inst_alloc but using the parent FSM as |
| 432 | * talloc context, and inheriting the log level of the parent. |
| 433 | * |
| 434 | * \param[in] fsm Descriptor of the to-be-allocated FSM |
| 435 | * \param[in] parent Parent FSM instance |
| 436 | * \param[in] parent_term_event Event to be sent to parent when terminating |
| 437 | * \returns newly-allocated, initialized and registered FSM instance |
| 438 | */ |
| 439 | struct osmo_fsm_inst *osmo_fsm_inst_alloc_child(struct osmo_fsm *fsm, |
| 440 | struct osmo_fsm_inst *parent, |
| 441 | uint32_t parent_term_event) |
| 442 | { |
| 443 | struct osmo_fsm_inst *fi; |
| 444 | |
| 445 | fi = osmo_fsm_inst_alloc(fsm, parent, NULL, parent->log_level, |
| 446 | parent->id); |
| 447 | if (!fi) { |
| 448 | /* indicate immediate termination to caller */ |
| 449 | osmo_fsm_inst_dispatch(parent, parent_term_event, NULL); |
| 450 | return NULL; |
| 451 | } |
| 452 | |
| 453 | LOGPFSM(fi, "is child of %s\n", osmo_fsm_inst_name(parent)); |
| 454 | |
Philipp Maier | 2a06a49 | 2018-01-16 18:45:56 +0100 | [diff] [blame] | 455 | osmo_fsm_inst_change_parent(fi, parent, parent_term_event); |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 456 | |
| 457 | return fi; |
| 458 | } |
| 459 | |
Philipp Maier | 2a06a49 | 2018-01-16 18:45:56 +0100 | [diff] [blame] | 460 | /*! unlink child FSM from its parent FSM. |
| 461 | * \param[in] fi Descriptor of the child FSM to unlink. |
Philipp Maier | d1f5793 | 2018-02-14 18:20:07 +0100 | [diff] [blame] | 462 | * \param[in] ctx New talloc context |
| 463 | * |
| 464 | * Never call this function from the cleanup callback, because at that time |
| 465 | * the child FSMs will already be terminated. If unlinking should be performed |
| 466 | * on FSM termination, use the grace callback instead. */ |
Philipp Maier | 2a06a49 | 2018-01-16 18:45:56 +0100 | [diff] [blame] | 467 | void osmo_fsm_inst_unlink_parent(struct osmo_fsm_inst *fi, void *ctx) |
| 468 | { |
| 469 | if (fi->proc.parent) { |
| 470 | talloc_steal(ctx, fi); |
| 471 | fi->proc.parent = NULL; |
| 472 | fi->proc.parent_term_event = 0; |
| 473 | llist_del(&fi->proc.child); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | /*! change parent instance of an FSM. |
| 478 | * \param[in] fi Descriptor of the to-be-allocated FSM. |
| 479 | * \param[in] new_parent New parent FSM instance. |
Philipp Maier | d1f5793 | 2018-02-14 18:20:07 +0100 | [diff] [blame] | 480 | * \param[in] new_parent_term_event Event to be sent to parent when terminating. |
| 481 | * |
| 482 | * Never call this function from the cleanup callback! |
| 483 | * (see also osmo_fsm_inst_unlink_parent()).*/ |
Philipp Maier | 2a06a49 | 2018-01-16 18:45:56 +0100 | [diff] [blame] | 484 | void osmo_fsm_inst_change_parent(struct osmo_fsm_inst *fi, |
| 485 | struct osmo_fsm_inst *new_parent, |
| 486 | uint32_t new_parent_term_event) |
| 487 | { |
| 488 | /* Make sure a possibly existing old parent is unlinked first |
| 489 | * (new_parent can be NULL) */ |
| 490 | osmo_fsm_inst_unlink_parent(fi, new_parent); |
| 491 | |
| 492 | /* Add new parent */ |
| 493 | if (new_parent) { |
| 494 | fi->proc.parent = new_parent; |
| 495 | fi->proc.parent_term_event = new_parent_term_event; |
| 496 | llist_add(&fi->proc.child, &new_parent->proc.children); |
| 497 | } |
| 498 | } |
| 499 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 500 | /*! delete a given instance of a FSM |
Vadim Yanitskiy | 2f65bb1 | 2019-03-25 15:57:09 +0700 | [diff] [blame] | 501 | * \param[in] fi FSM instance to be un-registered and deleted |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 502 | */ |
| 503 | void osmo_fsm_inst_free(struct osmo_fsm_inst *fi) |
| 504 | { |
| 505 | osmo_timer_del(&fi->timer); |
| 506 | llist_del(&fi->list); |
Neels Hofmeyr | 1f9cc01 | 2019-03-24 05:56:21 +0100 | [diff] [blame] | 507 | |
| 508 | if (fsm_term_safely.depth) { |
| 509 | /* Another FSM instance has caused this one to free and is still busy with its termination. Don't free |
| 510 | * yet, until the other FSM instance is done. */ |
| 511 | osmo_fsm_defer_free(fi); |
| 512 | /* The root_fi can't go missing really, but to be safe... */ |
| 513 | if (fsm_term_safely.root_fi) |
| 514 | LOGPFSM(fi, "Deferring: will deallocate with %s\n", fsm_term_safely.root_fi->name); |
| 515 | else |
| 516 | LOGPFSM(fi, "Deferring deallocation\n"); |
| 517 | |
| 518 | /* Don't free anything yet. Exit. */ |
| 519 | return; |
| 520 | } |
| 521 | |
| 522 | /* fsm_term_safely.depth == 0. |
| 523 | * - If fsm_term_safely is enabled, this is the original FSM instance that started terminating first. Free this |
| 524 | * and along with it all other collected terminated FSM instances. |
| 525 | * - If fsm_term_safely is disabled, this is just any FSM instance deallocating. */ |
| 526 | |
| 527 | if (fsm_term_safely.collect_ctx) { |
| 528 | /* The fi may be a child of any other FSM instances or objects collected in the collect_ctx. Don't |
| 529 | * deallocate separately to avoid use-after-free errors, put it in there and deallocate all at once. */ |
| 530 | LOGPFSM(fi, "Deallocated, including all deferred deallocations\n"); |
| 531 | osmo_fsm_defer_free(fi); |
| 532 | talloc_free(fsm_term_safely.collect_ctx); |
| 533 | fsm_term_safely.collect_ctx = NULL; |
| 534 | } else { |
| 535 | LOGPFSM(fi, "Deallocated\n"); |
| 536 | talloc_free(fi); |
| 537 | } |
| 538 | fsm_term_safely.root_fi = NULL; |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 539 | } |
| 540 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 541 | /*! get human-readable name of FSM event |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 542 | * \param[in] fsm FSM descriptor of event |
| 543 | * \param[in] event Event integer value |
| 544 | * \returns string rendering of the event |
| 545 | */ |
| 546 | const char *osmo_fsm_event_name(struct osmo_fsm *fsm, uint32_t event) |
| 547 | { |
Harald Welte | 171ef82 | 2019-03-28 10:49:05 +0100 | [diff] [blame] | 548 | static __thread char buf[32]; |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 549 | if (!fsm->event_names) { |
Pau Espin Pedrol | 4f8857e | 2017-06-18 12:23:00 +0200 | [diff] [blame] | 550 | snprintf(buf, sizeof(buf), "%"PRIu32, event); |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 551 | return buf; |
| 552 | } else |
| 553 | return get_value_string(fsm->event_names, event); |
| 554 | } |
| 555 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 556 | /*! get human-readable name of FSM instance |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 557 | * \param[in] fi FSM instance |
| 558 | * \returns string rendering of the FSM identity |
| 559 | */ |
| 560 | const char *osmo_fsm_inst_name(struct osmo_fsm_inst *fi) |
| 561 | { |
| 562 | if (!fi) |
| 563 | return "NULL"; |
| 564 | |
| 565 | if (fi->name) |
| 566 | return fi->name; |
| 567 | else |
| 568 | return fi->fsm->name; |
| 569 | } |
| 570 | |
Philipp Maier | af6710f | 2018-11-16 17:45:40 +0100 | [diff] [blame] | 571 | /*! get human-readable name of FSM state |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 572 | * \param[in] fsm FSM descriptor |
| 573 | * \param[in] state FSM state number |
| 574 | * \returns string rendering of the FSM state |
| 575 | */ |
| 576 | const char *osmo_fsm_state_name(struct osmo_fsm *fsm, uint32_t state) |
| 577 | { |
Harald Welte | 171ef82 | 2019-03-28 10:49:05 +0100 | [diff] [blame] | 578 | static __thread char buf[32]; |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 579 | if (state >= fsm->num_states) { |
Pau Espin Pedrol | 4f8857e | 2017-06-18 12:23:00 +0200 | [diff] [blame] | 580 | snprintf(buf, sizeof(buf), "unknown %"PRIu32, state); |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 581 | return buf; |
| 582 | } else |
| 583 | return fsm->states[state].name; |
| 584 | } |
| 585 | |
Neels Hofmeyr | 407df02 | 2018-05-25 18:20:06 +0200 | [diff] [blame] | 586 | static int state_chg(struct osmo_fsm_inst *fi, uint32_t new_state, |
Harald Welte | 7b74551 | 2019-05-18 21:03:55 +0200 | [diff] [blame] | 587 | bool keep_timer, unsigned long timeout_ms, int T, |
Neels Hofmeyr | 407df02 | 2018-05-25 18:20:06 +0200 | [diff] [blame] | 588 | const char *file, int line) |
| 589 | { |
| 590 | struct osmo_fsm *fsm = fi->fsm; |
| 591 | uint32_t old_state = fi->state; |
| 592 | const struct osmo_fsm_state *st = &fsm->states[fi->state]; |
Neels Hofmeyr | 050f2d3 | 2018-05-31 15:30:15 +0200 | [diff] [blame] | 593 | struct timeval remaining; |
Neels Hofmeyr | 407df02 | 2018-05-25 18:20:06 +0200 | [diff] [blame] | 594 | |
| 595 | /* validate if new_state is a valid state */ |
| 596 | if (!(st->out_state_mask & (1 << new_state))) { |
| 597 | LOGPFSMLSRC(fi, LOGL_ERROR, file, line, |
| 598 | "transition to state %s not permitted!\n", |
| 599 | osmo_fsm_state_name(fsm, new_state)); |
| 600 | return -EPERM; |
| 601 | } |
| 602 | |
| 603 | if (!keep_timer) { |
| 604 | /* delete the old timer */ |
| 605 | osmo_timer_del(&fi->timer); |
| 606 | } |
| 607 | |
| 608 | if (st->onleave) |
| 609 | st->onleave(fi, new_state); |
| 610 | |
Neels Hofmeyr | 050f2d3 | 2018-05-31 15:30:15 +0200 | [diff] [blame] | 611 | if (fsm_log_timeouts) { |
Harald Welte | cb5e831 | 2019-06-04 12:10:11 +0200 | [diff] [blame] | 612 | char trailer[64]; |
| 613 | trailer[0] = '\0'; |
Neels Hofmeyr | d4b79c8 | 2019-03-06 05:43:23 +0100 | [diff] [blame] | 614 | if (keep_timer && fi->timer.active) { |
| 615 | /* This should always give us a timeout, but just in case the return value indicates error, omit |
| 616 | * logging the remaining time. */ |
| 617 | if (osmo_timer_remaining(&fi->timer, NULL, &remaining)) |
Harald Welte | cb5e831 | 2019-06-04 12:10:11 +0200 | [diff] [blame] | 618 | snprintf(trailer, sizeof(trailer), "(keeping " OSMO_T_FMT ")", |
| 619 | OSMO_T_FMT_ARGS(fi->T)); |
Neels Hofmeyr | d4b79c8 | 2019-03-06 05:43:23 +0100 | [diff] [blame] | 620 | else |
Harald Welte | cb5e831 | 2019-06-04 12:10:11 +0200 | [diff] [blame] | 621 | snprintf(trailer, sizeof(trailer), "(keeping " OSMO_T_FMT |
| 622 | ", %ld.%03lds remaining)", OSMO_T_FMT_ARGS(fi->T), |
| 623 | remaining.tv_sec, remaining.tv_usec / 1000); |
Harald Welte | 7b74551 | 2019-05-18 21:03:55 +0200 | [diff] [blame] | 624 | } else if (timeout_ms) { |
Harald Welte | cb5e831 | 2019-06-04 12:10:11 +0200 | [diff] [blame] | 625 | if (timeout_ms % 1000 == 0) |
Harald Welte | 7b74551 | 2019-05-18 21:03:55 +0200 | [diff] [blame] | 626 | /* keep log output legacy compatible to avoid autotest failures */ |
Harald Welte | cb5e831 | 2019-06-04 12:10:11 +0200 | [diff] [blame] | 627 | snprintf(trailer, sizeof(trailer), "(" OSMO_T_FMT ", %lus)", |
Harald Welte | 7b74551 | 2019-05-18 21:03:55 +0200 | [diff] [blame] | 628 | OSMO_T_FMT_ARGS(T), timeout_ms/1000); |
Harald Welte | cb5e831 | 2019-06-04 12:10:11 +0200 | [diff] [blame] | 629 | else |
| 630 | snprintf(trailer, sizeof(trailer), "(" OSMO_T_FMT ", %lums)", |
Harald Welte | 7b74551 | 2019-05-18 21:03:55 +0200 | [diff] [blame] | 631 | OSMO_T_FMT_ARGS(T), timeout_ms); |
Harald Welte | 7b74551 | 2019-05-18 21:03:55 +0200 | [diff] [blame] | 632 | } else |
Harald Welte | cb5e831 | 2019-06-04 12:10:11 +0200 | [diff] [blame] | 633 | snprintf(trailer, sizeof(trailer), "(no timeout)"); |
| 634 | |
| 635 | LOGPFSMSRC(fi, file, line, "State change to %s %s\n", |
| 636 | osmo_fsm_state_name(fsm, new_state), trailer); |
Neels Hofmeyr | 050f2d3 | 2018-05-31 15:30:15 +0200 | [diff] [blame] | 637 | } else { |
| 638 | LOGPFSMSRC(fi, file, line, "state_chg to %s\n", |
| 639 | osmo_fsm_state_name(fsm, new_state)); |
| 640 | } |
| 641 | |
Neels Hofmeyr | 407df02 | 2018-05-25 18:20:06 +0200 | [diff] [blame] | 642 | fi->state = new_state; |
| 643 | st = &fsm->states[new_state]; |
| 644 | |
Neels Hofmeyr | d4b79c8 | 2019-03-06 05:43:23 +0100 | [diff] [blame] | 645 | if (!keep_timer |
| 646 | || (keep_timer && !osmo_timer_pending(&fi->timer))) { |
Neels Hofmeyr | 407df02 | 2018-05-25 18:20:06 +0200 | [diff] [blame] | 647 | fi->T = T; |
Harald Welte | 7b74551 | 2019-05-18 21:03:55 +0200 | [diff] [blame] | 648 | if (timeout_ms) |
| 649 | osmo_timer_schedule(&fi->timer, timeout_ms / 1000, timeout_ms % 1000); |
Neels Hofmeyr | 407df02 | 2018-05-25 18:20:06 +0200 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | /* Call 'onenter' last, user might terminate FSM from there */ |
| 653 | if (st->onenter) |
| 654 | st->onenter(fi, old_state); |
| 655 | |
| 656 | return 0; |
| 657 | } |
| 658 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 659 | /*! perform a state change of the given FSM instance |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 660 | * |
Neels Hofmeyr | 725698a | 2016-12-14 17:24:54 +0100 | [diff] [blame] | 661 | * Best invoke via the osmo_fsm_inst_state_chg() macro which logs the source |
| 662 | * file where the state change was effected. Alternatively, you may pass \a |
| 663 | * file as NULL to use the normal file/line indication instead. |
| 664 | * |
Neels Hofmeyr | 407df02 | 2018-05-25 18:20:06 +0200 | [diff] [blame] | 665 | * All changes to the FSM instance state must be made via an osmo_fsm_inst_state_chg_* |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 666 | * function. It verifies that the existing state actually permits a |
Neels Hofmeyr | 407df02 | 2018-05-25 18:20:06 +0200 | [diff] [blame] | 667 | * transition to new_state. |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 668 | * |
Neels Hofmeyr | bd5a1dc | 2019-01-28 15:38:09 +0100 | [diff] [blame] | 669 | * If timeout_secs is 0, stay in the new state indefinitely, without a timeout |
| 670 | * (stop the FSM instance's timer if it was runnning). |
| 671 | * |
| 672 | * If timeout_secs > 0, start or reset the FSM instance's timer with this |
| 673 | * timeout. On expiry, invoke the FSM instance's timer_cb -- if no timer_cb is |
| 674 | * set, an expired timer immediately terminates the FSM instance with |
| 675 | * OSMO_FSM_TERM_TIMEOUT. |
| 676 | * |
| 677 | * The value of T is stored in fi->T and is then available for query in |
| 678 | * timer_cb. If passing timeout_secs == 0, it is recommended to also pass T == |
| 679 | * 0, so that fi->T is reset to 0 when no timeout is invoked. |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 680 | * |
Neels Hofmeyr | 5734bff | 2019-02-21 02:27:48 +0100 | [diff] [blame] | 681 | * Positive values for T are considered to be 3GPP spec compliant and appear in |
| 682 | * logging and VTY as "T1234", while negative values are considered to be |
| 683 | * Osmocom specific timers, represented in logging and VTY as "X1234". |
| 684 | * |
Neels Hofmeyr | 0fd615f | 2019-01-26 20:36:12 +0100 | [diff] [blame] | 685 | * See also osmo_tdef_fsm_inst_state_chg() from the osmo_tdef API, which |
| 686 | * provides a unified way to configure and apply GSM style Tnnnn timers to FSM |
| 687 | * state transitions. |
| 688 | * |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 689 | * \param[in] fi FSM instance whose state is to change |
| 690 | * \param[in] new_state The new state into which we should change |
Neels Hofmeyr | 89991fd | 2019-01-28 19:06:53 +0100 | [diff] [blame] | 691 | * \param[in] timeout_secs Timeout in seconds (if !=0), maximum-clamped to 2147483647 seconds. |
Neels Hofmeyr | 5734bff | 2019-02-21 02:27:48 +0100 | [diff] [blame] | 692 | * \param[in] T Timer number, where positive numbers are considered to be 3GPP spec compliant timer numbers and are |
| 693 | * logged as "T1234", while negative numbers are considered Osmocom specific timer numbers logged as |
| 694 | * "X1234". |
Neels Hofmeyr | b805cc1 | 2016-12-23 04:23:18 +0100 | [diff] [blame] | 695 | * \param[in] file Calling source file (from osmo_fsm_inst_state_chg macro) |
| 696 | * \param[in] line Calling source line (from osmo_fsm_inst_state_chg macro) |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 697 | * \returns 0 on success; negative on error |
| 698 | */ |
Neels Hofmeyr | 725698a | 2016-12-14 17:24:54 +0100 | [diff] [blame] | 699 | int _osmo_fsm_inst_state_chg(struct osmo_fsm_inst *fi, uint32_t new_state, |
| 700 | unsigned long timeout_secs, int T, |
| 701 | const char *file, int line) |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 702 | { |
Harald Welte | 7b74551 | 2019-05-18 21:03:55 +0200 | [diff] [blame] | 703 | return state_chg(fi, new_state, false, timeout_secs*1000, T, file, line); |
| 704 | } |
| 705 | int _osmo_fsm_inst_state_chg_ms(struct osmo_fsm_inst *fi, uint32_t new_state, |
| 706 | unsigned long timeout_ms, int T, |
| 707 | const char *file, int line) |
| 708 | { |
| 709 | return state_chg(fi, new_state, false, timeout_ms, T, file, line); |
Neels Hofmeyr | 407df02 | 2018-05-25 18:20:06 +0200 | [diff] [blame] | 710 | } |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 711 | |
Neels Hofmeyr | 407df02 | 2018-05-25 18:20:06 +0200 | [diff] [blame] | 712 | /*! perform a state change while keeping the current timer running. |
| 713 | * |
| 714 | * This is useful to keep a timeout across several states (without having to round the |
| 715 | * remaining time to seconds). |
| 716 | * |
| 717 | * Best invoke via the osmo_fsm_inst_state_chg_keep_timer() macro which logs the source |
| 718 | * file where the state change was effected. Alternatively, you may pass \a |
| 719 | * file as NULL to use the normal file/line indication instead. |
| 720 | * |
| 721 | * All changes to the FSM instance state must be made via an osmo_fsm_inst_state_chg_* |
| 722 | * function. It verifies that the existing state actually permits a |
| 723 | * transition to new_state. |
| 724 | * |
| 725 | * \param[in] fi FSM instance whose state is to change |
| 726 | * \param[in] new_state The new state into which we should change |
| 727 | * \param[in] file Calling source file (from osmo_fsm_inst_state_chg macro) |
| 728 | * \param[in] line Calling source line (from osmo_fsm_inst_state_chg macro) |
| 729 | * \returns 0 on success; negative on error |
| 730 | */ |
| 731 | int _osmo_fsm_inst_state_chg_keep_timer(struct osmo_fsm_inst *fi, uint32_t new_state, |
| 732 | const char *file, int line) |
| 733 | { |
| 734 | return state_chg(fi, new_state, true, 0, 0, file, line); |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 735 | } |
| 736 | |
Neels Hofmeyr | d4b79c8 | 2019-03-06 05:43:23 +0100 | [diff] [blame] | 737 | /*! perform a state change while keeping the current timer if running, or starting a timer otherwise. |
| 738 | * |
| 739 | * This is useful to keep a timeout across several states, but to make sure that some timeout is actually running. |
| 740 | * |
| 741 | * Best invoke via the osmo_fsm_inst_state_chg_keep_or_start_timer() macro which logs the source file where the state |
| 742 | * change was effected. Alternatively, you may pass file as NULL to use the normal file/line indication instead. |
| 743 | * |
| 744 | * All changes to the FSM instance state must be made via an osmo_fsm_inst_state_chg_* |
| 745 | * function. It verifies that the existing state actually permits a |
| 746 | * transition to new_state. |
| 747 | * |
| 748 | * \param[in] fi FSM instance whose state is to change |
| 749 | * \param[in] new_state The new state into which we should change |
| 750 | * \param[in] timeout_secs If no timer is running yet, set this timeout in seconds (if !=0), maximum-clamped to |
| 751 | * 2147483647 seconds. |
| 752 | * \param[in] T Timer number, where positive numbers are considered to be 3GPP spec compliant timer numbers and are |
| 753 | * logged as "T1234", while negative numbers are considered Osmocom specific timer numbers logged as |
| 754 | * "X1234". |
| 755 | * \param[in] file Calling source file (from osmo_fsm_inst_state_chg macro) |
| 756 | * \param[in] line Calling source line (from osmo_fsm_inst_state_chg macro) |
| 757 | * \returns 0 on success; negative on error |
| 758 | */ |
| 759 | int _osmo_fsm_inst_state_chg_keep_or_start_timer(struct osmo_fsm_inst *fi, uint32_t new_state, |
| 760 | unsigned long timeout_secs, int T, |
| 761 | const char *file, int line) |
| 762 | { |
Harald Welte | 7b74551 | 2019-05-18 21:03:55 +0200 | [diff] [blame] | 763 | return state_chg(fi, new_state, true, timeout_secs*1000, T, file, line); |
Neels Hofmeyr | d4b79c8 | 2019-03-06 05:43:23 +0100 | [diff] [blame] | 764 | } |
Harald Welte | 7b74551 | 2019-05-18 21:03:55 +0200 | [diff] [blame] | 765 | int _osmo_fsm_inst_state_chg_keep_or_start_timer_ms(struct osmo_fsm_inst *fi, uint32_t new_state, |
| 766 | unsigned long timeout_ms, int T, |
| 767 | const char *file, int line) |
| 768 | { |
| 769 | return state_chg(fi, new_state, true, timeout_ms, T, file, line); |
| 770 | } |
| 771 | |
Neels Hofmeyr | d4b79c8 | 2019-03-06 05:43:23 +0100 | [diff] [blame] | 772 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 773 | /*! dispatch an event to an osmocom finite state machine instance |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 774 | * |
Neels Hofmeyr | 725698a | 2016-12-14 17:24:54 +0100 | [diff] [blame] | 775 | * Best invoke via the osmo_fsm_inst_dispatch() macro which logs the source |
| 776 | * file where the event was effected. Alternatively, you may pass \a file as |
| 777 | * NULL to use the normal file/line indication instead. |
| 778 | * |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 779 | * Any incoming events to \ref osmo_fsm instances must be dispatched to |
| 780 | * them via this function. It verifies, whether the event is permitted |
| 781 | * based on the current state of the FSM. If not, -1 is returned. |
| 782 | * |
| 783 | * \param[in] fi FSM instance |
| 784 | * \param[in] event Event to send to FSM instance |
| 785 | * \param[in] data Data to pass along with the event |
Neels Hofmeyr | b805cc1 | 2016-12-23 04:23:18 +0100 | [diff] [blame] | 786 | * \param[in] file Calling source file (from osmo_fsm_inst_dispatch macro) |
| 787 | * \param[in] line Calling source line (from osmo_fsm_inst_dispatch macro) |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 788 | * \returns 0 in case of success; negative on error |
| 789 | */ |
Neels Hofmeyr | 725698a | 2016-12-14 17:24:54 +0100 | [diff] [blame] | 790 | int _osmo_fsm_inst_dispatch(struct osmo_fsm_inst *fi, uint32_t event, void *data, |
| 791 | const char *file, int line) |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 792 | { |
| 793 | struct osmo_fsm *fsm; |
| 794 | const struct osmo_fsm_state *fs; |
| 795 | |
| 796 | if (!fi) { |
Neels Hofmeyr | c7155df | 2016-12-23 04:24:51 +0100 | [diff] [blame] | 797 | LOGPSRC(DLGLOBAL, LOGL_ERROR, file, line, |
Pau Espin Pedrol | 4f8857e | 2017-06-18 12:23:00 +0200 | [diff] [blame] | 798 | "Trying to dispatch event %"PRIu32" to non-existent" |
Neels Hofmeyr | c7155df | 2016-12-23 04:24:51 +0100 | [diff] [blame] | 799 | " FSM instance!\n", event); |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 800 | osmo_log_backtrace(DLGLOBAL, LOGL_ERROR); |
| 801 | return -ENODEV; |
| 802 | } |
| 803 | |
| 804 | fsm = fi->fsm; |
| 805 | OSMO_ASSERT(fi->state < fsm->num_states); |
| 806 | fs = &fi->fsm->states[fi->state]; |
| 807 | |
Neels Hofmeyr | 725698a | 2016-12-14 17:24:54 +0100 | [diff] [blame] | 808 | LOGPFSMSRC(fi, file, line, |
| 809 | "Received Event %s\n", osmo_fsm_event_name(fsm, event)); |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 810 | |
| 811 | if (((1 << event) & fsm->allstate_event_mask) && fsm->allstate_action) { |
| 812 | fsm->allstate_action(fi, event, data); |
| 813 | return 0; |
| 814 | } |
| 815 | |
| 816 | if (!((1 << event) & fs->in_event_mask)) { |
Neels Hofmeyr | 725698a | 2016-12-14 17:24:54 +0100 | [diff] [blame] | 817 | LOGPFSMLSRC(fi, LOGL_ERROR, file, line, |
| 818 | "Event %s not permitted\n", |
| 819 | osmo_fsm_event_name(fsm, event)); |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 820 | return -1; |
| 821 | } |
Philipp Maier | 3d4fb59 | 2018-05-15 10:06:22 +0200 | [diff] [blame] | 822 | |
| 823 | if (fs->action) |
| 824 | fs->action(fi, event, data); |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 825 | |
| 826 | return 0; |
| 827 | } |
| 828 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 829 | /*! Terminate FSM instance with given cause |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 830 | * |
| 831 | * This safely terminates the given FSM instance by first iterating |
| 832 | * over all children and sending them a termination event. Next, it |
| 833 | * calls the FSM descriptors cleanup function (if any), followed by |
| 834 | * releasing any memory associated with the FSM instance. |
| 835 | * |
| 836 | * Finally, the parent FSM instance (if any) is notified using the |
| 837 | * parent termination event configured at time of FSM instance start. |
| 838 | * |
| 839 | * \param[in] fi FSM instance to be terminated |
| 840 | * \param[in] cause Cause / reason for termination |
Neels Hofmeyr | b805cc1 | 2016-12-23 04:23:18 +0100 | [diff] [blame] | 841 | * \param[in] data Opaque event data to be passed with the parent term event |
| 842 | * \param[in] file Calling source file (from osmo_fsm_inst_term macro) |
| 843 | * \param[in] line Calling source line (from osmo_fsm_inst_term macro) |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 844 | */ |
Neels Hofmeyr | 725698a | 2016-12-14 17:24:54 +0100 | [diff] [blame] | 845 | void _osmo_fsm_inst_term(struct osmo_fsm_inst *fi, |
| 846 | enum osmo_fsm_term_cause cause, void *data, |
| 847 | const char *file, int line) |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 848 | { |
Neels Hofmeyr | 3faa014 | 2016-12-18 23:41:41 +0100 | [diff] [blame] | 849 | struct osmo_fsm_inst *parent; |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 850 | uint32_t parent_term_event = fi->proc.parent_term_event; |
| 851 | |
Neels Hofmeyr | 3b414a4 | 2019-04-08 00:33:53 +0200 | [diff] [blame] | 852 | if (fi->proc.terminating) { |
| 853 | LOGPFSMSRC(fi, file, line, "Ignoring trigger to terminate: already terminating\n"); |
| 854 | return; |
| 855 | } |
| 856 | fi->proc.terminating = true; |
| 857 | |
Neels Hofmeyr | 1f9cc01 | 2019-03-24 05:56:21 +0100 | [diff] [blame] | 858 | /* Start termination cascade handling only if the feature is enabled. Also check the current depth: though |
| 859 | * unlikely, theoretically the fsm_term_safely_enabled flag could be toggled in the middle of a cascaded |
| 860 | * termination, so make sure to continue if it already started. */ |
| 861 | if (fsm_term_safely_enabled || fsm_term_safely.depth) { |
| 862 | fsm_term_safely.depth++; |
| 863 | /* root_fi is just for logging, so no need to be extra careful about it. */ |
| 864 | if (!fsm_term_safely.root_fi) |
| 865 | fsm_term_safely.root_fi = fi; |
| 866 | } |
| 867 | |
| 868 | if (fsm_term_safely.depth > 1) { |
| 869 | /* fsm_term_safely is enabled and this is a secondary FSM instance terminated, caused by the root_fi. */ |
| 870 | LOGPFSMSRC(fi, file, line, "Terminating in cascade, depth %d (cause = %s, caused by: %s)\n", |
| 871 | fsm_term_safely.depth, osmo_fsm_term_cause_name(cause), |
| 872 | fsm_term_safely.root_fi ? fsm_term_safely.root_fi->name : "unknown"); |
| 873 | /* The root_fi can't go missing really, but to be safe, log "unknown" in that case. */ |
| 874 | } else { |
| 875 | /* fsm_term_safely is disabled, or this is the root_fi. */ |
| 876 | LOGPFSMSRC(fi, file, line, "Terminating (cause = %s)\n", osmo_fsm_term_cause_name(cause)); |
| 877 | } |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 878 | |
Philipp Maier | d1f5793 | 2018-02-14 18:20:07 +0100 | [diff] [blame] | 879 | /* graceful exit (optional) */ |
| 880 | if (fi->fsm->pre_term) |
| 881 | fi->fsm->pre_term(fi, cause); |
| 882 | |
Harald Welte | 6590044 | 2018-02-09 09:58:57 +0000 | [diff] [blame] | 883 | _osmo_fsm_inst_term_children(fi, OSMO_FSM_TERM_PARENT, NULL, |
| 884 | file, line); |
| 885 | |
Neels Hofmeyr | c014f60 | 2016-12-23 04:26:39 +0100 | [diff] [blame] | 886 | /* delete ourselves from the parent */ |
Neels Hofmeyr | 3faa014 | 2016-12-18 23:41:41 +0100 | [diff] [blame] | 887 | parent = fi->proc.parent; |
Philipp Maier | 23d3161 | 2018-01-16 18:50:23 +0100 | [diff] [blame] | 888 | if (parent) { |
Neels Hofmeyr | c014f60 | 2016-12-23 04:26:39 +0100 | [diff] [blame] | 889 | LOGPFSMSRC(fi, file, line, "Removing from parent %s\n", |
| 890 | osmo_fsm_inst_name(parent)); |
Philipp Maier | 23d3161 | 2018-01-16 18:50:23 +0100 | [diff] [blame] | 891 | llist_del(&fi->proc.child); |
| 892 | } |
Neels Hofmeyr | c014f60 | 2016-12-23 04:26:39 +0100 | [diff] [blame] | 893 | |
| 894 | /* call destructor / clean-up function */ |
| 895 | if (fi->fsm->cleanup) |
| 896 | fi->fsm->cleanup(fi, cause); |
| 897 | |
Neels Hofmeyr | 3faa014 | 2016-12-18 23:41:41 +0100 | [diff] [blame] | 898 | /* Fetch parent again in case it has changed. */ |
| 899 | parent = fi->proc.parent; |
Neels Hofmeyr | 1f9cc01 | 2019-03-24 05:56:21 +0100 | [diff] [blame] | 900 | |
| 901 | /* Legacy behavior if fsm_term_safely is disabled: free before dispatching parent event. (If fsm_term_safely is |
| 902 | * enabled, depth will *always* be > 0 here.) Pivot on depth instead of the enabled flag in case the enabled |
| 903 | * flag is toggled in the middle of an FSM term. */ |
| 904 | if (!fsm_term_safely.depth) { |
| 905 | LOGPFSMSRC(fi, file, line, "Freeing instance\n"); |
| 906 | osmo_fsm_inst_free(fi); |
| 907 | } |
Neels Hofmeyr | c014f60 | 2016-12-23 04:26:39 +0100 | [diff] [blame] | 908 | |
| 909 | /* indicate our termination to the parent */ |
| 910 | if (parent && cause != OSMO_FSM_TERM_PARENT) |
| 911 | _osmo_fsm_inst_dispatch(parent, parent_term_event, data, |
| 912 | file, line); |
Neels Hofmeyr | 1f9cc01 | 2019-03-24 05:56:21 +0100 | [diff] [blame] | 913 | |
| 914 | /* Newer, safe deallocation: free only after the parent_term_event was dispatched, to catch all termination |
| 915 | * cascades, and free all FSM instances at once. (If fsm_term_safely is enabled, depth will *always* be > 0 |
| 916 | * here.) osmo_fsm_inst_free() will do the defer magic depending on the fsm_term_safely.depth. */ |
| 917 | if (fsm_term_safely.depth) { |
| 918 | fsm_term_safely.depth--; |
| 919 | osmo_fsm_inst_free(fi); |
| 920 | } |
Neels Hofmeyr | c014f60 | 2016-12-23 04:26:39 +0100 | [diff] [blame] | 921 | } |
| 922 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 923 | /*! Terminate all child FSM instances of an FSM instance. |
Neels Hofmeyr | c014f60 | 2016-12-23 04:26:39 +0100 | [diff] [blame] | 924 | * |
| 925 | * Iterate over all children and send them a termination event, with the given |
| 926 | * cause. Pass OSMO_FSM_TERM_PARENT to avoid dispatching events from the |
| 927 | * terminated child FSMs. |
| 928 | * |
| 929 | * \param[in] fi FSM instance that should be cleared of child FSMs |
| 930 | * \param[in] cause Cause / reason for termination (OSMO_FSM_TERM_PARENT) |
| 931 | * \param[in] data Opaque event data to be passed with the parent term events |
| 932 | * \param[in] file Calling source file (from osmo_fsm_inst_term_children macro) |
| 933 | * \param[in] line Calling source line (from osmo_fsm_inst_term_children macro) |
| 934 | */ |
| 935 | void _osmo_fsm_inst_term_children(struct osmo_fsm_inst *fi, |
| 936 | enum osmo_fsm_term_cause cause, |
| 937 | void *data, |
| 938 | const char *file, int line) |
| 939 | { |
| 940 | struct osmo_fsm_inst *first_child, *last_seen_first_child; |
| 941 | |
Neels Hofmeyr | 06ac9b4 | 2016-12-20 12:05:19 +0100 | [diff] [blame] | 942 | /* iterate over all children, starting from the beginning every time: |
| 943 | * terminating an FSM may emit events that cause other FSMs to also |
| 944 | * terminate and remove themselves from this list. */ |
| 945 | last_seen_first_child = NULL; |
| 946 | while (!llist_empty(&fi->proc.children)) { |
| 947 | first_child = llist_entry(fi->proc.children.next, |
| 948 | typeof(*first_child), |
| 949 | proc.child); |
| 950 | |
| 951 | /* paranoia: do not loop forever */ |
| 952 | if (first_child == last_seen_first_child) { |
| 953 | LOGPFSMLSRC(fi, LOGL_ERROR, file, line, |
| 954 | "Internal error while terminating child" |
| 955 | " FSMs: a child FSM is stuck\n"); |
| 956 | break; |
| 957 | } |
| 958 | last_seen_first_child = first_child; |
| 959 | |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 960 | /* terminate child */ |
Neels Hofmeyr | c014f60 | 2016-12-23 04:26:39 +0100 | [diff] [blame] | 961 | _osmo_fsm_inst_term(first_child, cause, data, |
Neels Hofmeyr | 725698a | 2016-12-14 17:24:54 +0100 | [diff] [blame] | 962 | file, line); |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 963 | } |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 964 | } |
| 965 | |
Neels Hofmeyr | 5c5c78a | 2016-12-14 18:35:47 +0100 | [diff] [blame] | 966 | const struct value_string osmo_fsm_term_cause_names[] = { |
Neels Hofmeyr | 1808096 | 2016-12-16 13:43:54 +0100 | [diff] [blame] | 967 | OSMO_VALUE_STRING(OSMO_FSM_TERM_PARENT), |
| 968 | OSMO_VALUE_STRING(OSMO_FSM_TERM_REQUEST), |
| 969 | OSMO_VALUE_STRING(OSMO_FSM_TERM_REGULAR), |
| 970 | OSMO_VALUE_STRING(OSMO_FSM_TERM_ERROR), |
| 971 | OSMO_VALUE_STRING(OSMO_FSM_TERM_TIMEOUT), |
Neels Hofmeyr | 5c5c78a | 2016-12-14 18:35:47 +0100 | [diff] [blame] | 972 | { 0, NULL } |
| 973 | }; |
| 974 | |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 975 | /*! @} */ |