blob: 3ee0058fc31cdee80944f0fc23d30d77b71ba10a [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
Philipp Maierfbf66102017-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 Maierfbf66102017-04-09 12:32:51 +020047/* Disconnected state */
48static void fsm_disc_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
49{
50 struct a_reset_ctx *reset = (struct a_reset_ctx *)data;
51 OSMO_ASSERT(reset);
Philipp Maier8ae3c922017-11-09 11:17:24 +010052 OSMO_ASSERT(reset->fsm);
Philipp Maierb8acdcd2017-11-24 11:39:10 +010053 LOGPFSML(reset->fsm, LOGL_NOTICE, "SIGTRAN connection succeded.\n");
Philipp Maierfbf66102017-04-09 12:32:51 +020054
55 reset->conn_loss_counter = 0;
56 osmo_fsm_inst_state_chg(fi, ST_CONN, 0, 0);
57}
58
59/* Connected state */
60static void fsm_conn_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
61{
62 struct a_reset_ctx *reset = (struct a_reset_ctx *)data;
63 OSMO_ASSERT(reset);
64
Philipp Maierfbf66102017-04-09 12:32:51 +020065 switch (event) {
66 case EV_N_DISCONNECT:
67 if (reset->conn_loss_counter >= BAD_CONNECTION_THRESOLD) {
Philipp Maier8ae3c922017-11-09 11:17:24 +010068 LOGPFSML(reset->fsm, LOGL_NOTICE, "SIGTRAN connection down, reconnecting...\n");
Philipp Maierfbf66102017-04-09 12:32:51 +020069 osmo_fsm_inst_state_chg(fi, ST_DISC, RESET_RESEND_INTERVAL, RESET_RESEND_TIMER_NO);
70 } else
71 reset->conn_loss_counter++;
72 break;
73 case EV_N_CONNECT:
74 reset->conn_loss_counter = 0;
75 break;
76 }
77}
78
79/* Timer callback to retransmit the reset signal */
80static int fsm_reset_ack_timeout_cb(struct osmo_fsm_inst *fi)
81{
82 struct a_reset_ctx *reset = (struct a_reset_ctx *)fi->priv;
Philipp Maier8ae3c922017-11-09 11:17:24 +010083 OSMO_ASSERT(reset->fsm);
Philipp Maierfbf66102017-04-09 12:32:51 +020084
Philipp Maierb8acdcd2017-11-24 11:39:10 +010085 LOGPFSML(reset->fsm, LOGL_NOTICE, "(re)sending BSSMAP RESET message...\n");
Philipp Maierfbf66102017-04-09 12:32:51 +020086
87 reset->cb(reset->priv);
88
89 osmo_fsm_inst_state_chg(fi, ST_DISC, RESET_RESEND_INTERVAL, RESET_RESEND_TIMER_NO);
90 return 0;
91}
92
93static struct osmo_fsm_state fsm_states[] = {
94 [ST_DISC] = {
95 .in_event_mask = (1 << EV_RESET_ACK),
96 .out_state_mask = (1 << ST_DISC) | (1 << ST_CONN),
97 .name = "DISC",
98 .action = fsm_disc_cb,
99 },
100 [ST_CONN] = {
101 .in_event_mask = (1 << EV_N_DISCONNECT) | (1 << EV_N_CONNECT),
102 .out_state_mask = (1 << ST_DISC) | (1 << ST_CONN),
103 .name = "CONN",
104 .action = fsm_conn_cb,
105 },
106};
107
108/* State machine definition */
109static struct osmo_fsm fsm = {
Harald Welte6556d3cb2017-10-25 03:28:03 +0200110 .name = "A-RESET",
Philipp Maierfbf66102017-04-09 12:32:51 +0200111 .states = fsm_states,
112 .num_states = ARRAY_SIZE(fsm_states),
113 .log_subsys = DMSC,
114 .timer_cb = fsm_reset_ack_timeout_cb,
115};
116
117/* Create and start state machine which handles the reset/reset-ack procedure */
118struct a_reset_ctx *a_reset_alloc(const void *ctx, const char *name, void *cb, void *priv)
119{
120 OSMO_ASSERT(name);
121
122 struct a_reset_ctx *reset;
123
124 /* Register the fsm description (if not already done) */
125 if (osmo_fsm_find_by_name(fsm.name) != &fsm)
126 osmo_fsm_register(&fsm);
127
128 /* Allocate and configure a new fsm instance */
129 reset = talloc_zero(ctx, struct a_reset_ctx);
130 OSMO_ASSERT(reset);
131 reset->priv = priv;
132 reset->cb = cb;
Philipp Maierfbf66102017-04-09 12:32:51 +0200133 reset->conn_loss_counter = 0;
Philipp Maier8ae3c922017-11-09 11:17:24 +0100134 reset->fsm = osmo_fsm_inst_alloc(&fsm, NULL, NULL, LOGL_DEBUG, name);
Philipp Maierfbf66102017-04-09 12:32:51 +0200135 OSMO_ASSERT(reset->fsm);
136 reset->fsm->priv = reset;
Philipp Maierfbf66102017-04-09 12:32:51 +0200137
138 /* kick off reset-ack sending mechanism */
139 osmo_fsm_inst_state_chg(reset->fsm, ST_DISC, RESET_RESEND_INTERVAL, RESET_RESEND_TIMER_NO);
140
141 return reset;
142}
143
144/* Tear down state machine */
145void a_reset_free(struct a_reset_ctx *reset)
146{
147 OSMO_ASSERT(reset);
148 OSMO_ASSERT(reset->fsm);
149
150 osmo_fsm_inst_free(reset->fsm);
151 reset->fsm = NULL;
152
153 memset(reset, 0, sizeof(*reset));
154 talloc_free(reset);
Philipp Maierfbf66102017-04-09 12:32:51 +0200155}
156
157/* Confirm that we sucessfully received a reset acknowlege message */
158void a_reset_ack_confirm(struct a_reset_ctx *reset)
159{
160 OSMO_ASSERT(reset);
161 OSMO_ASSERT(reset->fsm);
162
163 osmo_fsm_inst_dispatch(reset->fsm, EV_RESET_ACK, reset);
164}
165
166/* Report a failed connection */
167void a_reset_conn_fail(struct a_reset_ctx *reset)
168{
169 /* If no reset context is supplied, just drop the info */
170 if (!reset)
171 return;
172
173 OSMO_ASSERT(reset->fsm);
174
175 osmo_fsm_inst_dispatch(reset->fsm, EV_N_DISCONNECT, reset);
176}
177
178/* Report a successful connection */
179void a_reset_conn_success(struct a_reset_ctx *reset)
180{
181 /* If no reset context is supplied, just drop the info */
182 if (!reset)
183 return;
184
185 OSMO_ASSERT(reset->fsm);
186
187 osmo_fsm_inst_dispatch(reset->fsm, EV_N_CONNECT, reset);
188}
189
190/* Check if we have a connection to a specified msc */
191bool a_reset_conn_ready(struct a_reset_ctx *reset)
192{
193 /* If no reset context is supplied, we assume that
194 * the connection can't be ready! */
195 if (!reset)
196 return false;
197
198 OSMO_ASSERT(reset->fsm);
199 if (reset->fsm->state == ST_CONN)
200 return true;
201
202 return false;
203}