Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 1 | /* (C) 2017 by sysmocom s.f.m.c. GmbH |
| 2 | * All Rights Reserved |
| 3 | * |
| 4 | * Author: Philipp Maier |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU Affero General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU Affero General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Affero General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <osmocom/core/logging.h> |
| 22 | #include <osmocom/core/utils.h> |
| 23 | #include <osmocom/core/timer.h> |
| 24 | #include <osmocom/core/fsm.h> |
| 25 | #include <unistd.h> |
| 26 | #include <errno.h> |
| 27 | #include <string.h> |
Neels Hofmeyr | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 28 | #include <osmocom/msc/debug.h> |
Harald Welte | 4bd35d4 | 2018-01-25 00:26:59 +0100 | [diff] [blame] | 29 | #include <osmocom/msc/a_reset.h> |
Neels Hofmeyr | 7992122 | 2018-11-30 01:46:51 +0100 | [diff] [blame] | 30 | #include <osmocom/msc/msc_common.h> |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 31 | |
Philipp Maier | f913e5f | 2018-05-07 10:15:49 +0200 | [diff] [blame] | 32 | #define RESET_RESEND_INTERVAL 2 /* sec */ |
| 33 | #define RESET_RESEND_TIMER_NO 16 /* See also 3GPP TS 48.008 Chapter 3.1.4.1.3.2 */ |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 34 | |
Philipp Maier | f913e5f | 2018-05-07 10:15:49 +0200 | [diff] [blame] | 35 | enum reset_fsm_states { |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 36 | ST_DISC, /* Disconnected from remote end */ |
| 37 | ST_CONN, /* We have a confirmed connection */ |
| 38 | }; |
| 39 | |
Philipp Maier | f913e5f | 2018-05-07 10:15:49 +0200 | [diff] [blame] | 40 | enum reset_fsm_evt { |
| 41 | EV_CONN_ACK, /* Received either BSSMAP RESET or BSSMAP RESET |
| 42 | * ACK from the remote end */ |
| 43 | }; |
| 44 | |
| 45 | /* Reset context data (callbacks, state machine etc...) */ |
| 46 | struct reset_ctx { |
| 47 | /* Callback function to be called when a connection |
| 48 | * failure is detected and a rest must occur */ |
| 49 | void (*cb)(void *priv); |
| 50 | |
| 51 | /* Privated data for the callback function */ |
| 52 | void *priv; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 53 | }; |
| 54 | |
Philipp Maier | 5861678 | 2018-02-26 12:40:27 +0100 | [diff] [blame] | 55 | static const struct value_string fsm_event_names[] = { |
Philipp Maier | f913e5f | 2018-05-07 10:15:49 +0200 | [diff] [blame] | 56 | OSMO_VALUE_STRING(EV_CONN_ACK), |
Philipp Maier | 5861678 | 2018-02-26 12:40:27 +0100 | [diff] [blame] | 57 | {0, NULL} |
| 58 | }; |
| 59 | |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 60 | /* Disconnected state */ |
| 61 | static void fsm_disc_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 62 | { |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 63 | osmo_fsm_inst_state_chg(fi, ST_CONN, 0, 0); |
| 64 | } |
| 65 | |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 66 | /* Timer callback to retransmit the reset signal */ |
| 67 | static int fsm_reset_ack_timeout_cb(struct osmo_fsm_inst *fi) |
| 68 | { |
Philipp Maier | f913e5f | 2018-05-07 10:15:49 +0200 | [diff] [blame] | 69 | struct reset_ctx *reset_ctx = (struct reset_ctx *)fi->priv; |
| 70 | LOGPFSML(fi, LOGL_NOTICE, "(re)sending BSSMAP RESET message...\n"); |
| 71 | reset_ctx->cb(reset_ctx->priv); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 72 | osmo_fsm_inst_state_chg(fi, ST_DISC, RESET_RESEND_INTERVAL, RESET_RESEND_TIMER_NO); |
| 73 | return 0; |
| 74 | } |
| 75 | |
Philipp Maier | f913e5f | 2018-05-07 10:15:49 +0200 | [diff] [blame] | 76 | static struct osmo_fsm_state reset_fsm_states[] = { |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 77 | [ST_DISC] = { |
Philipp Maier | f913e5f | 2018-05-07 10:15:49 +0200 | [diff] [blame] | 78 | .in_event_mask = (1 << EV_CONN_ACK), |
| 79 | .out_state_mask = (1 << ST_CONN) | (1 << ST_DISC), |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 80 | .name = "DISC", |
| 81 | .action = fsm_disc_cb, |
| 82 | }, |
| 83 | [ST_CONN] = { |
Philipp Maier | f913e5f | 2018-05-07 10:15:49 +0200 | [diff] [blame] | 84 | .in_event_mask = (1 << EV_CONN_ACK), |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 85 | .name = "CONN", |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 86 | }, |
| 87 | }; |
| 88 | |
| 89 | /* State machine definition */ |
| 90 | static struct osmo_fsm fsm = { |
Harald Welte | 6556d3cb | 2017-10-25 03:28:03 +0200 | [diff] [blame] | 91 | .name = "A-RESET", |
Philipp Maier | f913e5f | 2018-05-07 10:15:49 +0200 | [diff] [blame] | 92 | .states = reset_fsm_states, |
| 93 | .num_states = ARRAY_SIZE(reset_fsm_states), |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 94 | .log_subsys = DMSC, |
| 95 | .timer_cb = fsm_reset_ack_timeout_cb, |
Philipp Maier | 5861678 | 2018-02-26 12:40:27 +0100 | [diff] [blame] | 96 | .event_names = fsm_event_names, |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | /* Create and start state machine which handles the reset/reset-ack procedure */ |
Philipp Maier | f913e5f | 2018-05-07 10:15:49 +0200 | [diff] [blame] | 100 | struct osmo_fsm_inst *a_reset_alloc(void *ctx, const char *name, void *cb, |
| 101 | void *priv, bool already_connected) |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 102 | { |
| 103 | OSMO_ASSERT(name); |
| 104 | |
Philipp Maier | f913e5f | 2018-05-07 10:15:49 +0200 | [diff] [blame] | 105 | struct reset_ctx *reset_ctx; |
| 106 | struct osmo_fsm_inst *reset_fsm; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 107 | |
| 108 | /* Register the fsm description (if not already done) */ |
| 109 | if (osmo_fsm_find_by_name(fsm.name) != &fsm) |
| 110 | osmo_fsm_register(&fsm); |
| 111 | |
| 112 | /* Allocate and configure a new fsm instance */ |
Philipp Maier | f913e5f | 2018-05-07 10:15:49 +0200 | [diff] [blame] | 113 | reset_ctx = talloc_zero(ctx, struct reset_ctx); |
| 114 | OSMO_ASSERT(reset_ctx); |
| 115 | reset_ctx->priv = priv; |
| 116 | reset_ctx->cb = cb; |
| 117 | reset_fsm = osmo_fsm_inst_alloc(&fsm, ctx, reset_ctx, LOGL_DEBUG, name); |
| 118 | OSMO_ASSERT(reset_fsm); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 119 | |
Harald Welte | b6777fb | 2018-02-08 23:59:19 +0100 | [diff] [blame] | 120 | if (already_connected) |
Philipp Maier | f913e5f | 2018-05-07 10:15:49 +0200 | [diff] [blame] | 121 | osmo_fsm_inst_state_chg(reset_fsm, ST_CONN, 0, 0); |
Harald Welte | b6777fb | 2018-02-08 23:59:19 +0100 | [diff] [blame] | 122 | else { |
| 123 | /* kick off reset-ack sending mechanism */ |
Philipp Maier | f913e5f | 2018-05-07 10:15:49 +0200 | [diff] [blame] | 124 | osmo_fsm_inst_state_chg(reset_fsm, ST_DISC, RESET_RESEND_INTERVAL, |
Harald Welte | b6777fb | 2018-02-08 23:59:19 +0100 | [diff] [blame] | 125 | RESET_RESEND_TIMER_NO); |
| 126 | } |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 127 | |
Philipp Maier | f913e5f | 2018-05-07 10:15:49 +0200 | [diff] [blame] | 128 | return reset_fsm; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /* Confirm that we sucessfully received a reset acknowlege message */ |
Philipp Maier | f913e5f | 2018-05-07 10:15:49 +0200 | [diff] [blame] | 132 | void a_reset_ack_confirm(struct osmo_fsm_inst *reset_fsm) |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 133 | { |
Philipp Maier | f913e5f | 2018-05-07 10:15:49 +0200 | [diff] [blame] | 134 | OSMO_ASSERT(reset_fsm); |
| 135 | osmo_fsm_inst_dispatch(reset_fsm, EV_CONN_ACK, NULL); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /* Check if we have a connection to a specified msc */ |
Philipp Maier | f913e5f | 2018-05-07 10:15:49 +0200 | [diff] [blame] | 139 | bool a_reset_conn_ready(struct osmo_fsm_inst *reset_fsm) |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 140 | { |
| 141 | /* If no reset context is supplied, we assume that |
| 142 | * the connection can't be ready! */ |
Philipp Maier | f913e5f | 2018-05-07 10:15:49 +0200 | [diff] [blame] | 143 | if (!reset_fsm) |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 144 | return false; |
| 145 | |
Philipp Maier | f913e5f | 2018-05-07 10:15:49 +0200 | [diff] [blame] | 146 | if (reset_fsm->state == ST_CONN) |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 147 | return true; |
| 148 | |
| 149 | return false; |
| 150 | } |