blob: 32c00ae536145f6da0e8d6bddfdf0b5975af62e5 [file] [log] [blame]
Philipp Maierfbf66102017-04-09 12:32:51 +02001/* (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 Hofmeyr90843962017-09-04 15:04:35 +020028#include <osmocom/msc/debug.h>
29#include <osmocom/msc/bsc_msc_data.h>
30#include <osmocom/msc/osmo_bsc_sigtran.h>
Philipp Maierfbf66102017-04-09 12:32:51 +020031
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
36enum fsm_states {
37 ST_DISC, /* Disconnected from remote end */
38 ST_CONN, /* We have a confirmed connection */
39};
40
41static const struct value_string fsm_state_names[] = {
42 {ST_DISC, "ST_DISC (disconnected)"},
43 {ST_CONN, "ST_CONN (connected)"},
44 {0, NULL},
45};
46
47enum 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
53static 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 */
61static 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);
Philipp Maier8ae3c922017-11-09 11:17:24 +010065 OSMO_ASSERT(reset->fsm);
Philipp Maierfbf66102017-04-09 12:32:51 +020066
Philipp Maier8ae3c922017-11-09 11:17:24 +010067 LOGPFSML(reset->fsm, LOGL_NOTICE, "fsm-state (msc-reset): %s, fsm-event: %s\n",
Philipp Maierfbf66102017-04-09 12:32:51 +020068 get_value_string(fsm_state_names, ST_CONN), get_value_string(fsm_evt_names, event));
69
70 reset->conn_loss_counter = 0;
71 osmo_fsm_inst_state_chg(fi, ST_CONN, 0, 0);
72}
73
74/* Connected state */
75static void fsm_conn_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
76{
77 struct a_reset_ctx *reset = (struct a_reset_ctx *)data;
78 OSMO_ASSERT(reset);
79
Philipp Maier8ae3c922017-11-09 11:17:24 +010080 LOGPFSML(reset->fsm, LOGL_NOTICE, "fsm-state (msc-reset): %s, fsm-event: %s\n",
Philipp Maierfbf66102017-04-09 12:32:51 +020081 get_value_string(fsm_state_names, ST_CONN), get_value_string(fsm_evt_names, event));
82
83 switch (event) {
84 case EV_N_DISCONNECT:
85 if (reset->conn_loss_counter >= BAD_CONNECTION_THRESOLD) {
Philipp Maier8ae3c922017-11-09 11:17:24 +010086 LOGPFSML(reset->fsm, LOGL_NOTICE, "SIGTRAN connection down, reconnecting...\n");
Philipp Maierfbf66102017-04-09 12:32:51 +020087 osmo_fsm_inst_state_chg(fi, ST_DISC, RESET_RESEND_INTERVAL, RESET_RESEND_TIMER_NO);
88 } else
89 reset->conn_loss_counter++;
90 break;
91 case EV_N_CONNECT:
92 reset->conn_loss_counter = 0;
93 break;
94 }
95}
96
97/* Timer callback to retransmit the reset signal */
98static int fsm_reset_ack_timeout_cb(struct osmo_fsm_inst *fi)
99{
100 struct a_reset_ctx *reset = (struct a_reset_ctx *)fi->priv;
Philipp Maier8ae3c922017-11-09 11:17:24 +0100101 OSMO_ASSERT(reset->fsm);
Philipp Maierfbf66102017-04-09 12:32:51 +0200102
Philipp Maier8ae3c922017-11-09 11:17:24 +0100103 LOGPFSML(reset->fsm, LOGL_NOTICE, "reset-ack timeout (T%i) in state %s, resending...\n", fi->T,
Philipp Maierfbf66102017-04-09 12:32:51 +0200104 get_value_string(fsm_state_names, fi->state));
105
106 reset->cb(reset->priv);
107
108 osmo_fsm_inst_state_chg(fi, ST_DISC, RESET_RESEND_INTERVAL, RESET_RESEND_TIMER_NO);
109 return 0;
110}
111
112static struct osmo_fsm_state fsm_states[] = {
113 [ST_DISC] = {
114 .in_event_mask = (1 << EV_RESET_ACK),
115 .out_state_mask = (1 << ST_DISC) | (1 << ST_CONN),
116 .name = "DISC",
117 .action = fsm_disc_cb,
118 },
119 [ST_CONN] = {
120 .in_event_mask = (1 << EV_N_DISCONNECT) | (1 << EV_N_CONNECT),
121 .out_state_mask = (1 << ST_DISC) | (1 << ST_CONN),
122 .name = "CONN",
123 .action = fsm_conn_cb,
124 },
125};
126
127/* State machine definition */
128static struct osmo_fsm fsm = {
Harald Welte6556d3cb2017-10-25 03:28:03 +0200129 .name = "A-RESET",
Philipp Maierfbf66102017-04-09 12:32:51 +0200130 .states = fsm_states,
131 .num_states = ARRAY_SIZE(fsm_states),
132 .log_subsys = DMSC,
133 .timer_cb = fsm_reset_ack_timeout_cb,
134};
135
136/* Create and start state machine which handles the reset/reset-ack procedure */
137struct a_reset_ctx *a_reset_alloc(const void *ctx, const char *name, void *cb, void *priv)
138{
139 OSMO_ASSERT(name);
140
141 struct a_reset_ctx *reset;
142
143 /* Register the fsm description (if not already done) */
144 if (osmo_fsm_find_by_name(fsm.name) != &fsm)
145 osmo_fsm_register(&fsm);
146
147 /* Allocate and configure a new fsm instance */
148 reset = talloc_zero(ctx, struct a_reset_ctx);
149 OSMO_ASSERT(reset);
150 reset->priv = priv;
151 reset->cb = cb;
Philipp Maierfbf66102017-04-09 12:32:51 +0200152 reset->conn_loss_counter = 0;
Philipp Maier8ae3c922017-11-09 11:17:24 +0100153 reset->fsm = osmo_fsm_inst_alloc(&fsm, NULL, NULL, LOGL_DEBUG, name);
Philipp Maierfbf66102017-04-09 12:32:51 +0200154 OSMO_ASSERT(reset->fsm);
155 reset->fsm->priv = reset;
Philipp Maier8ae3c922017-11-09 11:17:24 +0100156 LOGPFSML(reset->fsm, LOGL_NOTICE, "reset handler fsm created.\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200157
158 /* kick off reset-ack sending mechanism */
159 osmo_fsm_inst_state_chg(reset->fsm, ST_DISC, RESET_RESEND_INTERVAL, RESET_RESEND_TIMER_NO);
160
161 return reset;
162}
163
164/* Tear down state machine */
165void a_reset_free(struct a_reset_ctx *reset)
166{
167 OSMO_ASSERT(reset);
168 OSMO_ASSERT(reset->fsm);
169
170 osmo_fsm_inst_free(reset->fsm);
171 reset->fsm = NULL;
172
173 memset(reset, 0, sizeof(*reset));
174 talloc_free(reset);
175
Philipp Maier8ae3c922017-11-09 11:17:24 +0100176 LOGPFSML(reset->fsm, LOGL_NOTICE, "reset handler fsm destroyed.\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200177}
178
179/* Confirm that we sucessfully received a reset acknowlege message */
180void a_reset_ack_confirm(struct a_reset_ctx *reset)
181{
182 OSMO_ASSERT(reset);
183 OSMO_ASSERT(reset->fsm);
184
185 osmo_fsm_inst_dispatch(reset->fsm, EV_RESET_ACK, reset);
186}
187
188/* Report a failed connection */
189void a_reset_conn_fail(struct a_reset_ctx *reset)
190{
191 /* If no reset context is supplied, just drop the info */
192 if (!reset)
193 return;
194
195 OSMO_ASSERT(reset->fsm);
196
197 osmo_fsm_inst_dispatch(reset->fsm, EV_N_DISCONNECT, reset);
198}
199
200/* Report a successful connection */
201void a_reset_conn_success(struct a_reset_ctx *reset)
202{
203 /* If no reset context is supplied, just drop the info */
204 if (!reset)
205 return;
206
207 OSMO_ASSERT(reset->fsm);
208
209 osmo_fsm_inst_dispatch(reset->fsm, EV_N_CONNECT, reset);
210}
211
212/* Check if we have a connection to a specified msc */
213bool a_reset_conn_ready(struct a_reset_ctx *reset)
214{
215 /* If no reset context is supplied, we assume that
216 * the connection can't be ready! */
217 if (!reset)
218 return false;
219
220 OSMO_ASSERT(reset->fsm);
221 if (reset->fsm->state == ST_CONN)
222 return true;
223
224 return false;
225}