blob: 072181d7862415cd7a3a37957327dff55ffaf8c1 [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>
Harald Welte4bd35d42018-01-25 00:26:59 +010029#include <osmocom/msc/a_reset.h>
Philipp Maierfbf66102017-04-09 12:32:51 +020030
31#define RESET_RESEND_INTERVAL 2 /* sec */
32#define RESET_RESEND_TIMER_NO 1234 /* FIXME: dig out the real timer number */
33#define BAD_CONNECTION_THRESOLD 3 /* connection failures */
34
35enum fsm_states {
36 ST_DISC, /* Disconnected from remote end */
37 ST_CONN, /* We have a confirmed connection */
38};
39
Philipp Maierfbf66102017-04-09 12:32:51 +020040enum fsm_evt {
41 EV_RESET_ACK, /* got reset acknowlegement from remote end */
42 EV_N_DISCONNECT, /* lost a connection */
43 EV_N_CONNECT, /* made a successful connection */
44};
45
Philipp Maierfbf66102017-04-09 12:32:51 +020046/* Disconnected state */
47static void fsm_disc_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
48{
49 struct a_reset_ctx *reset = (struct a_reset_ctx *)data;
50 OSMO_ASSERT(reset);
Philipp Maier8ae3c922017-11-09 11:17:24 +010051 OSMO_ASSERT(reset->fsm);
Philipp Maierb8acdcd2017-11-24 11:39:10 +010052 LOGPFSML(reset->fsm, LOGL_NOTICE, "SIGTRAN connection succeded.\n");
Philipp Maierfbf66102017-04-09 12:32:51 +020053
54 reset->conn_loss_counter = 0;
55 osmo_fsm_inst_state_chg(fi, ST_CONN, 0, 0);
56}
57
58/* Connected state */
59static void fsm_conn_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
60{
61 struct a_reset_ctx *reset = (struct a_reset_ctx *)data;
62 OSMO_ASSERT(reset);
63
Philipp Maierfbf66102017-04-09 12:32:51 +020064 switch (event) {
65 case EV_N_DISCONNECT:
66 if (reset->conn_loss_counter >= BAD_CONNECTION_THRESOLD) {
Philipp Maier8ae3c922017-11-09 11:17:24 +010067 LOGPFSML(reset->fsm, LOGL_NOTICE, "SIGTRAN connection down, reconnecting...\n");
Philipp Maierfbf66102017-04-09 12:32:51 +020068 osmo_fsm_inst_state_chg(fi, ST_DISC, RESET_RESEND_INTERVAL, RESET_RESEND_TIMER_NO);
69 } else
70 reset->conn_loss_counter++;
71 break;
72 case EV_N_CONNECT:
73 reset->conn_loss_counter = 0;
74 break;
75 }
76}
77
78/* Timer callback to retransmit the reset signal */
79static int fsm_reset_ack_timeout_cb(struct osmo_fsm_inst *fi)
80{
81 struct a_reset_ctx *reset = (struct a_reset_ctx *)fi->priv;
Philipp Maier8ae3c922017-11-09 11:17:24 +010082 OSMO_ASSERT(reset->fsm);
Philipp Maierfbf66102017-04-09 12:32:51 +020083
Philipp Maierb8acdcd2017-11-24 11:39:10 +010084 LOGPFSML(reset->fsm, LOGL_NOTICE, "(re)sending BSSMAP RESET message...\n");
Philipp Maierfbf66102017-04-09 12:32:51 +020085
86 reset->cb(reset->priv);
87
88 osmo_fsm_inst_state_chg(fi, ST_DISC, RESET_RESEND_INTERVAL, RESET_RESEND_TIMER_NO);
89 return 0;
90}
91
92static struct osmo_fsm_state fsm_states[] = {
93 [ST_DISC] = {
94 .in_event_mask = (1 << EV_RESET_ACK),
95 .out_state_mask = (1 << ST_DISC) | (1 << ST_CONN),
96 .name = "DISC",
97 .action = fsm_disc_cb,
98 },
99 [ST_CONN] = {
100 .in_event_mask = (1 << EV_N_DISCONNECT) | (1 << EV_N_CONNECT),
101 .out_state_mask = (1 << ST_DISC) | (1 << ST_CONN),
102 .name = "CONN",
103 .action = fsm_conn_cb,
104 },
105};
106
107/* State machine definition */
108static struct osmo_fsm fsm = {
Harald Welte6556d3cb2017-10-25 03:28:03 +0200109 .name = "A-RESET",
Philipp Maierfbf66102017-04-09 12:32:51 +0200110 .states = fsm_states,
111 .num_states = ARRAY_SIZE(fsm_states),
112 .log_subsys = DMSC,
113 .timer_cb = fsm_reset_ack_timeout_cb,
114};
115
116/* Create and start state machine which handles the reset/reset-ack procedure */
117struct a_reset_ctx *a_reset_alloc(const void *ctx, const char *name, void *cb, void *priv)
118{
119 OSMO_ASSERT(name);
120
121 struct a_reset_ctx *reset;
122
123 /* Register the fsm description (if not already done) */
124 if (osmo_fsm_find_by_name(fsm.name) != &fsm)
125 osmo_fsm_register(&fsm);
126
127 /* Allocate and configure a new fsm instance */
128 reset = talloc_zero(ctx, struct a_reset_ctx);
129 OSMO_ASSERT(reset);
130 reset->priv = priv;
131 reset->cb = cb;
Philipp Maierfbf66102017-04-09 12:32:51 +0200132 reset->conn_loss_counter = 0;
Philipp Maier8ae3c922017-11-09 11:17:24 +0100133 reset->fsm = osmo_fsm_inst_alloc(&fsm, NULL, NULL, LOGL_DEBUG, name);
Philipp Maierfbf66102017-04-09 12:32:51 +0200134 OSMO_ASSERT(reset->fsm);
135 reset->fsm->priv = reset;
Philipp Maierfbf66102017-04-09 12:32:51 +0200136
137 /* kick off reset-ack sending mechanism */
138 osmo_fsm_inst_state_chg(reset->fsm, ST_DISC, RESET_RESEND_INTERVAL, RESET_RESEND_TIMER_NO);
139
140 return reset;
141}
142
143/* Tear down state machine */
144void a_reset_free(struct a_reset_ctx *reset)
145{
146 OSMO_ASSERT(reset);
147 OSMO_ASSERT(reset->fsm);
148
149 osmo_fsm_inst_free(reset->fsm);
150 reset->fsm = NULL;
151
152 memset(reset, 0, sizeof(*reset));
153 talloc_free(reset);
Philipp Maierfbf66102017-04-09 12:32:51 +0200154}
155
156/* Confirm that we sucessfully received a reset acknowlege message */
157void a_reset_ack_confirm(struct a_reset_ctx *reset)
158{
159 OSMO_ASSERT(reset);
160 OSMO_ASSERT(reset->fsm);
161
162 osmo_fsm_inst_dispatch(reset->fsm, EV_RESET_ACK, reset);
163}
164
165/* Report a failed connection */
166void a_reset_conn_fail(struct a_reset_ctx *reset)
167{
168 /* If no reset context is supplied, just drop the info */
169 if (!reset)
170 return;
171
172 OSMO_ASSERT(reset->fsm);
173
174 osmo_fsm_inst_dispatch(reset->fsm, EV_N_DISCONNECT, reset);
175}
176
177/* Report a successful connection */
178void a_reset_conn_success(struct a_reset_ctx *reset)
179{
180 /* If no reset context is supplied, just drop the info */
181 if (!reset)
182 return;
183
184 OSMO_ASSERT(reset->fsm);
185
186 osmo_fsm_inst_dispatch(reset->fsm, EV_N_CONNECT, reset);
187}
188
189/* Check if we have a connection to a specified msc */
190bool a_reset_conn_ready(struct a_reset_ctx *reset)
191{
192 /* If no reset context is supplied, we assume that
193 * the connection can't be ready! */
194 if (!reset)
195 return false;
196
197 OSMO_ASSERT(reset->fsm);
198 if (reset->fsm->state == ST_CONN)
199 return true;
200
201 return false;
202}