blob: 7ddc06580e8c400fa8397e39764b728d6aaacd7d [file] [log] [blame]
Harald Welte3dcdd202019-03-09 13:06:46 +01001/* (C) 2018-2019 by Harald Welte <laforge@gnumonks.org>
2 *
3 * All Rights Reserved
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
Harald Welte707c85a2019-03-09 12:56:35 +010022#include <stdint.h>
23#include <string.h>
24#include <errno.h>
25
26#include <talloc.h>
27
28#include <osmocom/core/logging.h>
29#include <osmocom/core/utils.h>
30#include <osmocom/core/msgb.h>
31#include <osmocom/core/fsm.h>
32
33#include <osmocom/abis/ipa.h>
34#include <osmocom/gsm/protocol/ipaccess.h>
35
36#include "client.h"
37#include "rspro_util.h"
38
39#define S(x) (1 << (x))
40
41static void bankd_updown_cb(struct ipa_client_conn *conn, int up)
42{
43 struct bankd_client *bc = conn->data;
44
45 printf("RSPRO link to %s:%d %s\n", conn->addr, conn->port, up ? "UP" : "DOWN");
46
47 osmo_fsm_inst_dispatch(bc->bankd_fi, up ? BDC_E_TCP_UP: BDC_E_TCP_DOWN, 0);
48}
49
Harald Welte2f89b962019-03-30 08:58:40 +010050/* internal function, bypassing FSM state */
51static int _bankd_conn_send_rspro(struct bankd_client *bc, RsproPDU_t *rspro)
Harald Weltea844bb02019-03-09 13:38:50 +010052{
53 return ipa_client_conn_send_rspro(bc->bankd_conn, rspro);
54}
55
Harald Welte2f89b962019-03-30 08:58:40 +010056int bankd_conn_send_rspro(struct bankd_client *bc, RsproPDU_t *rspro)
57{
58 if (osmo_fsm_inst_dispatch(bc->bankd_fi, BDC_E_RSPRO_TX, rspro) < 0) {
59 ASN_STRUCT_FREE(asn_DEF_RsproPDU, rspro);
60 return -1;
61 }
62 return 0;
63}
64
Harald Welte707c85a2019-03-09 12:56:35 +010065/***********************************************************************
Harald Welte0a684972019-12-02 20:10:59 +010066 * client-side FSM for RSPRO connection to remsim-bankd
67 *
68 * This is part of remsim-client and manages the connection to remsim-bankd,
69 * over which actual TPDU exchanges happen.
Harald Welte707c85a2019-03-09 12:56:35 +010070 ***********************************************************************/
71
72enum bankd_conn_fsm_state {
Harald Welte0a684972019-12-02 20:10:59 +010073 /* waiting for initial connection to remsim-bankd */
Harald Welte707c85a2019-03-09 12:56:35 +010074 BDC_ST_INIT,
75 /* bankd connection established, waiting for ClientConnectRes */
76 BDC_ST_ESTABLISHED,
Harald Welte0a684972019-12-02 20:10:59 +010077 /* bankd connection established, ClientConnect succeeded */
Harald Welte707c85a2019-03-09 12:56:35 +010078 BDC_ST_CONNECTED,
79 /* connection lost, we're waiting for a re-establish */
80 BDC_ST_REESTABLISH,
81};
82
83static const struct value_string remsim_client_bankd_fsm_event_names[] = {
84 OSMO_VALUE_STRING(BDC_E_ESTABLISH),
85 OSMO_VALUE_STRING(BDC_E_TCP_UP),
86 OSMO_VALUE_STRING(BDC_E_TCP_DOWN),
87 OSMO_VALUE_STRING(BDC_E_CLIENT_CONN_RES),
Harald Welte2f89b962019-03-30 08:58:40 +010088 OSMO_VALUE_STRING(BDC_E_RSPRO_TX),
Harald Welte707c85a2019-03-09 12:56:35 +010089 { 0, NULL }
90};
91
92#define T1_WAIT_CLIENT_CONN_RES 10
93#define T2_RECONNECT 10
94
95
96static void bdc_st_init(struct osmo_fsm_inst *fi, uint32_t event, void *data)
97{
98 switch (event) {
99 case BDC_E_ESTABLISH:
100 osmo_fsm_inst_state_chg(fi, BDC_ST_REESTABLISH, T2_RECONNECT, 2);
101 break;
102 default:
103 OSMO_ASSERT(0);
104 }
105}
106
107static void bdc_st_established_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
108{
109 struct bankd_client *bc = (struct bankd_client *) fi->priv;
110 RsproPDU_t *pdu;
111
Harald Weltef5116672019-04-02 20:55:06 +0200112 /* Send ClientConnReq */
Harald Welte707c85a2019-03-09 12:56:35 +0100113 pdu = rspro_gen_ConnectClientReq(&bc->srv_conn.own_comp_id, bc->srv_conn.clslot);
Harald Welte2f89b962019-03-30 08:58:40 +0100114 _bankd_conn_send_rspro(bc, pdu);
Harald Welte707c85a2019-03-09 12:56:35 +0100115}
116
117static void bdc_st_established(struct osmo_fsm_inst *fi, uint32_t event, void *data)
118{
119 switch (event) {
120 case BDC_E_TCP_DOWN:
121 osmo_fsm_inst_state_chg(fi, BDC_ST_REESTABLISH, T2_RECONNECT, 2);
122 break;
123 case BDC_E_CLIENT_CONN_RES:
124 /* somehow notify the main code? */
125 osmo_fsm_inst_state_chg(fi, BDC_ST_CONNECTED, 0, 0);
126 break;
127 default:
128 OSMO_ASSERT(0);
129 }
130}
131
132static void bdc_st_connected(struct osmo_fsm_inst *fi, uint32_t event, void *data)
133{
Harald Welte2f89b962019-03-30 08:58:40 +0100134 struct bankd_client *bc = (struct bankd_client *) fi->priv;
135 RsproPDU_t *pdu = NULL;
136
Harald Welte707c85a2019-03-09 12:56:35 +0100137 switch (event) {
138 case BDC_E_TCP_DOWN:
139 osmo_fsm_inst_state_chg(fi, BDC_ST_REESTABLISH, T2_RECONNECT, 2);
140 break;
Harald Welte2f89b962019-03-30 08:58:40 +0100141 case BDC_E_RSPRO_TX:
142 pdu = data;
143 _bankd_conn_send_rspro(bc, pdu);
144 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100145 default:
146 OSMO_ASSERT(0);
147 }
148}
149
150static void bdc_st_reestablish_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
151{
152 struct bankd_client *bc = (struct bankd_client *) fi->priv;
153 int rc;
154
155 /* re-create bankd_conn */
156 if (bc->bankd_conn) {
157 LOGPFSML(fi, LOGL_INFO, "Destroying existing connection to bankd\n");
Harald Welte13ebf432019-11-07 13:00:39 +0100158 ipa_client_conn_close(bc->bankd_conn);
Harald Welte707c85a2019-03-09 12:56:35 +0100159 ipa_client_conn_destroy(bc->bankd_conn);
160 bc->bankd_conn = NULL;
161 }
162 LOGPFSML(fi, LOGL_INFO, "Creating TCP connection to bankd at %s:%u\n",
163 bc->bankd_host, bc->bankd_port);
164 bc->bankd_conn = ipa_client_conn_create(bc, NULL, 0, bc->bankd_host, bc->bankd_port,
165 bankd_updown_cb, bankd_read_cb, NULL, bc);
166 if (!bc->bankd_conn) {
167 fprintf(stderr, "Unable to create socket: %s\n", strerror(errno));
168 exit(1);
169 }
170
171 /* Attempt to connect TCP socket */
172 rc = ipa_client_conn_open(bc->bankd_conn);
173 if (rc < 0) {
174 fprintf(stderr, "Unable to connect RSPRO to %s:%d - %s\n",
175 bc->bankd_conn->addr, bc->bankd_conn->port, strerror(errno));
176 /* FIXME: retry? Timer? Abort? */
177 OSMO_ASSERT(0);
178 }
179}
180
181static void bdc_st_reestablish(struct osmo_fsm_inst *fi, uint32_t event, void *data)
182{
183 switch (event) {
184 case BDC_E_TCP_UP:
185 osmo_fsm_inst_state_chg(fi, BDC_ST_ESTABLISHED, T1_WAIT_CLIENT_CONN_RES, 1);
186 break;
187 case BDC_E_TCP_DOWN:
188 /* wait for normal T2 timeout */
189 break;
190 default:
191 OSMO_ASSERT(0);
192 }
193}
194
195static void bdc_allstate_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
196{
197 switch (event) {
Harald Welte75f56032019-03-17 09:35:41 +0100198 case BDC_E_ESTABLISH:
Harald Welte707c85a2019-03-09 12:56:35 +0100199 osmo_fsm_inst_state_chg(fi, BDC_ST_REESTABLISH, T2_RECONNECT, 2);
200 break;
201 default:
202 OSMO_ASSERT(0);
203 }
204}
205
206static int remsim_client_bankd_fsm_timer_cb(struct osmo_fsm_inst *fi)
207{
208 switch (fi->T) {
209 case 2:
210 /* TCP reconnect failed: retry */
211 osmo_fsm_inst_state_chg(fi, BDC_ST_REESTABLISH, T2_RECONNECT, 2);
212 break;
213 case 1:
214 /* no ClientConnectRes received: disconnect + reconnect */
215 osmo_fsm_inst_state_chg(fi, BDC_ST_REESTABLISH, T2_RECONNECT, 2);
216 break;
217 default:
218 OSMO_ASSERT(0);
219 }
220 return 0;
221}
222
223static const struct osmo_fsm_state bankd_conn_fsm_states[] = {
224 [BDC_ST_INIT] = {
225 .name = "INIT",
226 .in_event_mask = 0, /* S(BDC_E_ESTABLISH) via allstate */
227 .out_state_mask = S(BDC_ST_REESTABLISH),
228 .action = bdc_st_init,
229 },
230 [BDC_ST_ESTABLISHED] = {
231 .name = "ESTABLISHED",
232 .in_event_mask = S(BDC_E_TCP_DOWN) | S(BDC_E_CLIENT_CONN_RES),
233 .out_state_mask = S(BDC_ST_CONNECTED) | S(BDC_ST_REESTABLISH),
234 .action = bdc_st_established,
235 .onenter = bdc_st_established_onenter,
236 },
237 [BDC_ST_CONNECTED] = {
238 .name = "CONNECTED",
Harald Welte2f89b962019-03-30 08:58:40 +0100239 .in_event_mask = S(BDC_E_TCP_DOWN) | S(BDC_E_RSPRO_TX),
Harald Welte707c85a2019-03-09 12:56:35 +0100240 .out_state_mask = S(BDC_ST_REESTABLISH),
241 .action = bdc_st_connected,
242 },
243 [BDC_ST_REESTABLISH] = {
244 .name = "REESTABLISH",
245 .in_event_mask = S(BDC_E_TCP_UP) | S(BDC_E_TCP_DOWN),
246 .out_state_mask = S(BDC_ST_ESTABLISHED) | S(BDC_ST_REESTABLISH),
247 .action = bdc_st_reestablish,
248 .onenter = bdc_st_reestablish_onenter,
249 },
250};
251
252struct osmo_fsm remsim_client_bankd_fsm = {
253 .name = "BANKD_CONN",
254 .states = bankd_conn_fsm_states,
255 .num_states = ARRAY_SIZE(bankd_conn_fsm_states),
256 .allstate_event_mask = S(BDC_E_ESTABLISH),
257 .allstate_action = bdc_allstate_action,
258 .timer_cb = remsim_client_bankd_fsm_timer_cb,
259 .log_subsys = DMAIN,
260 .event_names = remsim_client_bankd_fsm_event_names,
261};
262
263int bankd_conn_fsm_alloc(struct bankd_client *bc)
264{
265 struct osmo_fsm_inst *fi;
266
267 fi = osmo_fsm_inst_alloc(&remsim_client_bankd_fsm, bc, bc, LOGL_DEBUG, "bankd");
268 if (!fi)
269 return -1;
270
271 bc->bankd_fi = fi;
272 return 0;
273}
274
275static __attribute__((constructor)) void on_dso_load(void)
276{
Harald Welte4ccd2fc2019-12-01 13:32:54 +0100277 OSMO_ASSERT(osmo_fsm_register(&remsim_client_bankd_fsm) == 0);
Harald Welte707c85a2019-03-09 12:56:35 +0100278}