blob: 1fccc5f05d959286ef7f458a4a9d728e0d072477 [file] [log] [blame]
Philipp Maier39f62bb2017-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 Hofmeyrc0164792017-09-04 15:15:32 +020028#include <osmocom/bsc/debug.h>
29#include <osmocom/bsc/bsc_msc_data.h>
30#include <osmocom/bsc/osmo_bsc_sigtran.h>
Philipp Maier39f62bb2017-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
Philipp Maier39f62bb2017-04-09 12:32:51 +020041enum fsm_evt {
42 EV_RESET_ACK, /* got reset acknowlegement from remote end */
43 EV_N_DISCONNECT, /* lost a connection */
44 EV_N_CONNECT, /* made a successful connection */
45};
46
Philipp Maier3d5818a2018-02-26 21:47:06 +010047static const struct value_string fsm_event_names[] = {
48 OSMO_VALUE_STRING(EV_RESET_ACK),
49 OSMO_VALUE_STRING(EV_N_DISCONNECT),
50 OSMO_VALUE_STRING(EV_N_CONNECT),
51 {0, NULL}
52};
53
Philipp Maier39f62bb2017-04-09 12:32:51 +020054/* Disconnected state */
55static void fsm_disc_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
56{
57 struct a_reset_ctx *reset = (struct a_reset_ctx *)data;
58 OSMO_ASSERT(reset);
Philipp Maier7e0c2692017-11-07 12:47:59 +010059 OSMO_ASSERT(reset->fsm);
Philipp Maiere4cfa742017-11-24 11:41:36 +010060 LOGPFSML(reset->fsm, LOGL_NOTICE, "SIGTRAN connection succeded.\n");
Philipp Maier39f62bb2017-04-09 12:32:51 +020061
62 reset->conn_loss_counter = 0;
63 osmo_fsm_inst_state_chg(fi, ST_CONN, 0, 0);
64}
65
66/* Connected state */
67static void fsm_conn_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
68{
69 struct a_reset_ctx *reset = (struct a_reset_ctx *)data;
70 OSMO_ASSERT(reset);
71
Philipp Maier39f62bb2017-04-09 12:32:51 +020072 switch (event) {
73 case EV_N_DISCONNECT:
74 if (reset->conn_loss_counter >= BAD_CONNECTION_THRESOLD) {
Philipp Maier7e0c2692017-11-07 12:47:59 +010075 LOGPFSML(reset->fsm, LOGL_NOTICE, "SIGTRAN connection down, reconnecting...\n");
Philipp Maier39f62bb2017-04-09 12:32:51 +020076 osmo_fsm_inst_state_chg(fi, ST_DISC, RESET_RESEND_INTERVAL, RESET_RESEND_TIMER_NO);
77 } else
78 reset->conn_loss_counter++;
79 break;
80 case EV_N_CONNECT:
81 reset->conn_loss_counter = 0;
82 break;
83 }
84}
85
86/* Timer callback to retransmit the reset signal */
87static int fsm_reset_ack_timeout_cb(struct osmo_fsm_inst *fi)
88{
89 struct a_reset_ctx *reset = (struct a_reset_ctx *)fi->priv;
Philipp Maier7e0c2692017-11-07 12:47:59 +010090 OSMO_ASSERT(reset->fsm);
Philipp Maier39f62bb2017-04-09 12:32:51 +020091
Philipp Maiere4cfa742017-11-24 11:41:36 +010092 LOGPFSML(reset->fsm, LOGL_NOTICE, "(re)sending BSSMAP RESET message...\n");
Philipp Maier39f62bb2017-04-09 12:32:51 +020093
94 reset->cb(reset->priv);
95
96 osmo_fsm_inst_state_chg(fi, ST_DISC, RESET_RESEND_INTERVAL, RESET_RESEND_TIMER_NO);
97 return 0;
98}
99
100static struct osmo_fsm_state fsm_states[] = {
101 [ST_DISC] = {
102 .in_event_mask = (1 << EV_RESET_ACK),
103 .out_state_mask = (1 << ST_DISC) | (1 << ST_CONN),
104 .name = "DISC",
105 .action = fsm_disc_cb,
106 },
107 [ST_CONN] = {
108 .in_event_mask = (1 << EV_N_DISCONNECT) | (1 << EV_N_CONNECT),
109 .out_state_mask = (1 << ST_DISC) | (1 << ST_CONN),
110 .name = "CONN",
111 .action = fsm_conn_cb,
112 },
113};
114
115/* State machine definition */
116static struct osmo_fsm fsm = {
Harald Welte5de72852017-10-24 18:18:53 +0200117 .name = "A-RESET",
Philipp Maier39f62bb2017-04-09 12:32:51 +0200118 .states = fsm_states,
119 .num_states = ARRAY_SIZE(fsm_states),
120 .log_subsys = DMSC,
121 .timer_cb = fsm_reset_ack_timeout_cb,
Philipp Maier3d5818a2018-02-26 21:47:06 +0100122 .event_names = fsm_event_names,
Philipp Maier39f62bb2017-04-09 12:32:51 +0200123};
124
125/* Create and start state machine which handles the reset/reset-ack procedure */
126struct a_reset_ctx *a_reset_alloc(const void *ctx, const char *name, void *cb, void *priv)
127{
128 OSMO_ASSERT(name);
129
130 struct a_reset_ctx *reset;
131
132 /* Register the fsm description (if not already done) */
133 if (osmo_fsm_find_by_name(fsm.name) != &fsm)
134 osmo_fsm_register(&fsm);
135
136 /* Allocate and configure a new fsm instance */
137 reset = talloc_zero(ctx, struct a_reset_ctx);
138 OSMO_ASSERT(reset);
139 reset->priv = priv;
140 reset->cb = cb;
Philipp Maier39f62bb2017-04-09 12:32:51 +0200141 reset->conn_loss_counter = 0;
Philipp Maier7e0c2692017-11-07 12:47:59 +0100142 reset->fsm = osmo_fsm_inst_alloc(&fsm, NULL, NULL, LOGL_DEBUG, name);
Philipp Maier39f62bb2017-04-09 12:32:51 +0200143 OSMO_ASSERT(reset->fsm);
144 reset->fsm->priv = reset;
Philipp Maier39f62bb2017-04-09 12:32:51 +0200145
146 /* kick off reset-ack sending mechanism */
147 osmo_fsm_inst_state_chg(reset->fsm, ST_DISC, RESET_RESEND_INTERVAL, RESET_RESEND_TIMER_NO);
148
149 return reset;
150}
151
152/* Tear down state machine */
153void a_reset_free(struct a_reset_ctx *reset)
154{
155 OSMO_ASSERT(reset);
156 OSMO_ASSERT(reset->fsm);
157
158 osmo_fsm_inst_free(reset->fsm);
159 reset->fsm = NULL;
160
161 memset(reset, 0, sizeof(*reset));
162 talloc_free(reset);
Philipp Maier39f62bb2017-04-09 12:32:51 +0200163}
164
165/* Confirm that we sucessfully received a reset acknowlege message */
166void a_reset_ack_confirm(struct a_reset_ctx *reset)
167{
168 OSMO_ASSERT(reset);
169 OSMO_ASSERT(reset->fsm);
170
171 osmo_fsm_inst_dispatch(reset->fsm, EV_RESET_ACK, reset);
172}
173
174/* Report a failed connection */
175void a_reset_conn_fail(struct a_reset_ctx *reset)
176{
177 /* If no reset context is supplied, just drop the info */
178 if (!reset)
179 return;
180
181 OSMO_ASSERT(reset->fsm);
182
183 osmo_fsm_inst_dispatch(reset->fsm, EV_N_DISCONNECT, reset);
184}
185
186/* Report a successful connection */
187void a_reset_conn_success(struct a_reset_ctx *reset)
188{
189 /* If no reset context is supplied, just drop the info */
190 if (!reset)
191 return;
192
193 OSMO_ASSERT(reset->fsm);
194
195 osmo_fsm_inst_dispatch(reset->fsm, EV_N_CONNECT, reset);
196}
197
198/* Check if we have a connection to a specified msc */
199bool a_reset_conn_ready(struct a_reset_ctx *reset)
200{
201 /* If no reset context is supplied, we assume that
202 * the connection can't be ready! */
203 if (!reset)
204 return false;
205
206 OSMO_ASSERT(reset->fsm);
207 if (reset->fsm->state == ST_CONN)
208 return true;
209
210 return false;
211}