Philipp Maier | 39f62bb | 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> |
| 28 | #include <openbsc/debug.h> |
| 29 | #include <openbsc/bsc_msc_data.h> |
| 30 | #include <openbsc/osmo_bsc_sigtran.h> |
| 31 | |
| 32 | #define RESET_RESEND_INTERVAL 2 /* sec */ |
| 33 | #define RESET_RESEND_TIMER_NO 1234 /* FIXME: dig out the real timer number */ |
| 34 | #define BAD_CONNECTION_THRESOLD 3 /* connection failures */ |
| 35 | |
| 36 | enum fsm_states { |
| 37 | ST_DISC, /* Disconnected from remote end */ |
| 38 | ST_CONN, /* We have a confirmed connection */ |
| 39 | }; |
| 40 | |
| 41 | static const struct value_string fsm_state_names[] = { |
| 42 | {ST_DISC, "ST_DISC (disconnected)"}, |
| 43 | {ST_CONN, "ST_CONN (connected)"}, |
| 44 | {0, NULL}, |
| 45 | }; |
| 46 | |
| 47 | enum fsm_evt { |
| 48 | EV_RESET_ACK, /* got reset acknowlegement from remote end */ |
| 49 | EV_N_DISCONNECT, /* lost a connection */ |
| 50 | EV_N_CONNECT, /* made a successful connection */ |
| 51 | }; |
| 52 | |
| 53 | static const struct value_string fsm_evt_names[] = { |
| 54 | {EV_RESET_ACK, "EV_RESET_ACK"}, |
| 55 | {EV_N_DISCONNECT, "EV_N_DISCONNECT"}, |
| 56 | {EV_N_CONNECT, "EV_N_CONNECT"}, |
| 57 | {0, NULL}, |
| 58 | }; |
| 59 | |
| 60 | /* Disconnected state */ |
| 61 | static void fsm_disc_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 62 | { |
| 63 | struct a_reset_ctx *reset = (struct a_reset_ctx *)data; |
| 64 | OSMO_ASSERT(reset); |
| 65 | |
| 66 | LOGP(DMSC, LOGL_NOTICE, "(%s) fsm-state (msc-reset): %s, fsm-event: %s\n", reset->name, |
| 67 | get_value_string(fsm_state_names, ST_CONN), get_value_string(fsm_evt_names, event)); |
| 68 | |
| 69 | reset->conn_loss_counter = 0; |
| 70 | osmo_fsm_inst_state_chg(fi, ST_CONN, 0, 0); |
| 71 | } |
| 72 | |
| 73 | /* Connected state */ |
| 74 | static void fsm_conn_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 75 | { |
| 76 | struct a_reset_ctx *reset = (struct a_reset_ctx *)data; |
| 77 | OSMO_ASSERT(reset); |
| 78 | |
| 79 | LOGP(DMSC, LOGL_NOTICE, "(%s) fsm-state (msc-reset): %s, fsm-event: %s\n", reset->name, |
| 80 | get_value_string(fsm_state_names, ST_CONN), get_value_string(fsm_evt_names, event)); |
| 81 | |
| 82 | switch (event) { |
| 83 | case EV_N_DISCONNECT: |
| 84 | if (reset->conn_loss_counter >= BAD_CONNECTION_THRESOLD) { |
| 85 | LOGP(DMSC, LOGL_NOTICE, "(%s) SIGTRAN connection down, reconnecting...\n", reset->name); |
| 86 | osmo_fsm_inst_state_chg(fi, ST_DISC, RESET_RESEND_INTERVAL, RESET_RESEND_TIMER_NO); |
| 87 | } else |
| 88 | reset->conn_loss_counter++; |
| 89 | break; |
| 90 | case EV_N_CONNECT: |
| 91 | reset->conn_loss_counter = 0; |
| 92 | break; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /* Timer callback to retransmit the reset signal */ |
| 97 | static int fsm_reset_ack_timeout_cb(struct osmo_fsm_inst *fi) |
| 98 | { |
| 99 | struct a_reset_ctx *reset = (struct a_reset_ctx *)fi->priv; |
| 100 | |
| 101 | LOGP(DMSC, LOGL_NOTICE, "(%s) reset-ack timeout (T%i) in state %s, resending...\n", reset->name, fi->T, |
| 102 | get_value_string(fsm_state_names, fi->state)); |
| 103 | |
| 104 | reset->cb(reset->priv); |
| 105 | |
| 106 | osmo_fsm_inst_state_chg(fi, ST_DISC, RESET_RESEND_INTERVAL, RESET_RESEND_TIMER_NO); |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static struct osmo_fsm_state fsm_states[] = { |
| 111 | [ST_DISC] = { |
| 112 | .in_event_mask = (1 << EV_RESET_ACK), |
| 113 | .out_state_mask = (1 << ST_DISC) | (1 << ST_CONN), |
| 114 | .name = "DISC", |
| 115 | .action = fsm_disc_cb, |
| 116 | }, |
| 117 | [ST_CONN] = { |
| 118 | .in_event_mask = (1 << EV_N_DISCONNECT) | (1 << EV_N_CONNECT), |
| 119 | .out_state_mask = (1 << ST_DISC) | (1 << ST_CONN), |
| 120 | .name = "CONN", |
| 121 | .action = fsm_conn_cb, |
| 122 | }, |
| 123 | }; |
| 124 | |
| 125 | /* State machine definition */ |
| 126 | static struct osmo_fsm fsm = { |
| 127 | .name = "FSM RESET", |
| 128 | .states = fsm_states, |
| 129 | .num_states = ARRAY_SIZE(fsm_states), |
| 130 | .log_subsys = DMSC, |
| 131 | .timer_cb = fsm_reset_ack_timeout_cb, |
| 132 | }; |
| 133 | |
| 134 | /* Create and start state machine which handles the reset/reset-ack procedure */ |
| 135 | struct a_reset_ctx *a_reset_alloc(const void *ctx, const char *name, void *cb, void *priv) |
| 136 | { |
| 137 | OSMO_ASSERT(name); |
| 138 | |
| 139 | struct a_reset_ctx *reset; |
| 140 | |
| 141 | /* Register the fsm description (if not already done) */ |
| 142 | if (osmo_fsm_find_by_name(fsm.name) != &fsm) |
| 143 | osmo_fsm_register(&fsm); |
| 144 | |
| 145 | /* Allocate and configure a new fsm instance */ |
| 146 | reset = talloc_zero(ctx, struct a_reset_ctx); |
| 147 | OSMO_ASSERT(reset); |
| 148 | reset->priv = priv; |
| 149 | reset->cb = cb; |
| 150 | strncpy(reset->name, name, sizeof(reset->name)); |
| 151 | reset->conn_loss_counter = 0; |
| 152 | reset->fsm = osmo_fsm_inst_alloc(&fsm, NULL, NULL, LOGL_DEBUG, "FSM RESET INST"); |
| 153 | OSMO_ASSERT(reset->fsm); |
| 154 | reset->fsm->priv = reset; |
| 155 | LOGP(DMSC, LOGL_NOTICE, "(%s) reset handler fsm created.\n", reset->name); |
| 156 | |
| 157 | /* kick off reset-ack sending mechanism */ |
| 158 | osmo_fsm_inst_state_chg(reset->fsm, ST_DISC, RESET_RESEND_INTERVAL, RESET_RESEND_TIMER_NO); |
| 159 | |
| 160 | return reset; |
| 161 | } |
| 162 | |
| 163 | /* Tear down state machine */ |
| 164 | void a_reset_free(struct a_reset_ctx *reset) |
| 165 | { |
| 166 | OSMO_ASSERT(reset); |
| 167 | OSMO_ASSERT(reset->fsm); |
| 168 | |
| 169 | osmo_fsm_inst_free(reset->fsm); |
| 170 | reset->fsm = NULL; |
| 171 | |
| 172 | memset(reset, 0, sizeof(*reset)); |
| 173 | talloc_free(reset); |
| 174 | |
| 175 | LOGP(DMSC, LOGL_NOTICE, "(%s) reset handler fsm destroyed.\n", reset->name); |
| 176 | } |
| 177 | |
| 178 | /* Confirm that we sucessfully received a reset acknowlege message */ |
| 179 | void a_reset_ack_confirm(struct a_reset_ctx *reset) |
| 180 | { |
| 181 | OSMO_ASSERT(reset); |
| 182 | OSMO_ASSERT(reset->fsm); |
| 183 | |
| 184 | osmo_fsm_inst_dispatch(reset->fsm, EV_RESET_ACK, reset); |
| 185 | } |
| 186 | |
| 187 | /* Report a failed connection */ |
| 188 | void a_reset_conn_fail(struct a_reset_ctx *reset) |
| 189 | { |
| 190 | /* If no reset context is supplied, just drop the info */ |
| 191 | if (!reset) |
| 192 | return; |
| 193 | |
| 194 | OSMO_ASSERT(reset->fsm); |
| 195 | |
| 196 | osmo_fsm_inst_dispatch(reset->fsm, EV_N_DISCONNECT, reset); |
| 197 | } |
| 198 | |
| 199 | /* Report a successful connection */ |
| 200 | void a_reset_conn_success(struct a_reset_ctx *reset) |
| 201 | { |
| 202 | /* If no reset context is supplied, just drop the info */ |
| 203 | if (!reset) |
| 204 | return; |
| 205 | |
| 206 | OSMO_ASSERT(reset->fsm); |
| 207 | |
| 208 | osmo_fsm_inst_dispatch(reset->fsm, EV_N_CONNECT, reset); |
| 209 | } |
| 210 | |
| 211 | /* Check if we have a connection to a specified msc */ |
| 212 | bool a_reset_conn_ready(struct a_reset_ctx *reset) |
| 213 | { |
| 214 | /* If no reset context is supplied, we assume that |
| 215 | * the connection can't be ready! */ |
| 216 | if (!reset) |
| 217 | return false; |
| 218 | |
| 219 | OSMO_ASSERT(reset->fsm); |
| 220 | if (reset->fsm->state == ST_CONN) |
| 221 | return true; |
| 222 | |
| 223 | return false; |
| 224 | } |