blob: 0baf08089f554261fdc8c2efce969baa2ebdf1fe [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>
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
36enum fsm_states {
37 ST_DISC, /* Disconnected from MSC */
38 ST_CONN, /* We have a confirmed connection to the MSC */
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 the MSC */
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 bsc_msc_data *msc = (struct bsc_msc_data *)data;
64
65 LOGP(DMSC, LOGL_NOTICE, "fsm-state (msc-reset): %s, fsm-event: %s, MSC No.: %i\n",
66 get_value_string(fsm_state_names, ST_DISC), get_value_string(fsm_evt_names, event), msc->nr);
67 msc->msc_con->msc_conn_loss_count = 0;
68 osmo_fsm_inst_state_chg(fi, ST_CONN, 0, 0);
69}
70
71/* Connected state */
72static void fsm_conn_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
73{
74 struct bsc_msc_data *msc = (struct bsc_msc_data *)data;
75
76 LOGP(DMSC, LOGL_NOTICE, "fsm-state (msc-reset): %s, fsm-event: %s, MSC No.: %i\n",
77 get_value_string(fsm_state_names, ST_CONN), get_value_string(fsm_evt_names, event), msc->nr);
78
79 OSMO_ASSERT(msc);
80
81 switch (event) {
82 case EV_N_DISCONNECT:
83 if (msc->msc_con->msc_conn_loss_count >= BAD_CONNECTION_THRESOLD) {
84 LOGP(DMSC, LOGL_NOTICE, "SIGTRAN connection to MSC No.: %i down, reconnecting...\n", msc->nr);
85 osmo_fsm_inst_state_chg(fi, ST_DISC, RESET_RESEND_INTERVAL, RESET_RESEND_TIMER_NO);
86 } else
87 msc->msc_con->msc_conn_loss_count++;
88 break;
89 case EV_N_CONNECT:
90 msc->msc_con->msc_conn_loss_count = 0;
91 break;
92 }
93}
94
95/* Timer callback to retransmit the reset signal */
96static int fsm_reset_ack_timeout_cb(struct osmo_fsm_inst *fi)
97{
98 struct bsc_msc_data *msc = (struct bsc_msc_data *)fi->priv;
99
100 LOGP(DMSC, LOGL_NOTICE, "reset-ack timeout (T%i) in state %s, MSC No.: %i, resending...\n", fi->T,
101 get_value_string(fsm_state_names, fi->state), msc->nr);
102
103 osmo_bsc_sigtran_reset(msc);
104 osmo_bsc_sigtran_tx_reset(msc);
105
106 osmo_fsm_inst_state_chg(fi, ST_DISC, RESET_RESEND_INTERVAL, RESET_RESEND_TIMER_NO);
107 return 0;
108}
109
110static 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 */
126static 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 */
135void start_reset_fsm(struct bsc_msc_data *msc)
136{
137 OSMO_ASSERT(msc);
138 OSMO_ASSERT(msc->msc_con);
139
140 osmo_fsm_register(&fsm);
141 msc->msc_con->fsm_reset = osmo_fsm_inst_alloc(&fsm, NULL, NULL, LOGL_DEBUG, "FSM RESET INST");
142 OSMO_ASSERT(msc->msc_con->fsm_reset);
143
144 msc->msc_con->fsm_reset->priv = msc;
145
146 /* kick off reset-ack sending mechanism */
147 osmo_fsm_inst_state_chg(msc->msc_con->fsm_reset, ST_DISC, RESET_RESEND_INTERVAL, RESET_RESEND_TIMER_NO);
148}
149
150/* Confirm that we sucessfully received a reset acknowlege message */
151void reset_ack_confirm(struct bsc_msc_data *msc)
152{
153 OSMO_ASSERT(msc);
154 OSMO_ASSERT(msc->msc_con);
155 OSMO_ASSERT(msc->msc_con->fsm_reset);
156
157 osmo_fsm_inst_dispatch(msc->msc_con->fsm_reset, EV_RESET_ACK, msc);
158}
159
160/* Report a failed connection */
161void report_conn_fail(struct bsc_msc_data *msc)
162{
163 OSMO_ASSERT(msc);
164 OSMO_ASSERT(msc->msc_con);
165 OSMO_ASSERT(msc->msc_con->fsm_reset);
166
167 osmo_fsm_inst_dispatch(msc->msc_con->fsm_reset, EV_N_DISCONNECT, msc);
168}
169
170/* Report a successful connection */
171void report_conn_success(struct bsc_msc_data *msc)
172{
173 OSMO_ASSERT(msc);
174 OSMO_ASSERT(msc->msc_con);
175 OSMO_ASSERT(msc->msc_con->fsm_reset);
176
177 osmo_fsm_inst_dispatch(msc->msc_con->fsm_reset, EV_N_CONNECT, msc);
178}
179
180/* Check if we have a connection to a specified msc */
181bool sccp_conn_ready(struct bsc_msc_data *msc)
182{
183 OSMO_ASSERT(msc);
184 OSMO_ASSERT(msc->msc_con);
185 OSMO_ASSERT(msc->msc_con->fsm_reset);
186 if (msc->msc_con->fsm_reset->state == ST_CONN)
187 return true;
188
189 return false;
190}