blob: bba512831c39a3efa989d3746febdc1dc5f5000f [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 Weltea844bb02019-03-09 13:38:50 +010050int bankd_conn_send_rspro(struct bankd_client *bc, RsproPDU_t *rspro)
51{
52 return ipa_client_conn_send_rspro(bc->bankd_conn, rspro);
53}
54
Harald Welte707c85a2019-03-09 12:56:35 +010055/***********************************************************************
56 * bankd connection FSM: Remsim Client connection to Bankd
57 ***********************************************************************/
58
59enum bankd_conn_fsm_state {
60 /* waiting for initial connectiong to remsim-bankd */
61 BDC_ST_INIT,
62 /* bankd connection established, waiting for ClientConnectRes */
63 BDC_ST_ESTABLISHED,
64 /* bankd connection etsablished, ClientConnect succeeded */
65 BDC_ST_CONNECTED,
66 /* connection lost, we're waiting for a re-establish */
67 BDC_ST_REESTABLISH,
68};
69
70static const struct value_string remsim_client_bankd_fsm_event_names[] = {
71 OSMO_VALUE_STRING(BDC_E_ESTABLISH),
72 OSMO_VALUE_STRING(BDC_E_TCP_UP),
73 OSMO_VALUE_STRING(BDC_E_TCP_DOWN),
74 OSMO_VALUE_STRING(BDC_E_CLIENT_CONN_RES),
75 { 0, NULL }
76};
77
78#define T1_WAIT_CLIENT_CONN_RES 10
79#define T2_RECONNECT 10
80
81
82static void bdc_st_init(struct osmo_fsm_inst *fi, uint32_t event, void *data)
83{
84 switch (event) {
85 case BDC_E_ESTABLISH:
86 osmo_fsm_inst_state_chg(fi, BDC_ST_REESTABLISH, T2_RECONNECT, 2);
87 break;
88 default:
89 OSMO_ASSERT(0);
90 }
91}
92
93static void bdc_st_established_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
94{
95 struct bankd_client *bc = (struct bankd_client *) fi->priv;
96 RsproPDU_t *pdu;
97
98 /* FIXME: Send ClientConnReq */
99 pdu = rspro_gen_ConnectClientReq(&bc->srv_conn.own_comp_id, bc->srv_conn.clslot);
Harald Weltea844bb02019-03-09 13:38:50 +0100100 bankd_conn_send_rspro(bc, pdu);
Harald Welte707c85a2019-03-09 12:56:35 +0100101}
102
103static void bdc_st_established(struct osmo_fsm_inst *fi, uint32_t event, void *data)
104{
105 switch (event) {
106 case BDC_E_TCP_DOWN:
107 osmo_fsm_inst_state_chg(fi, BDC_ST_REESTABLISH, T2_RECONNECT, 2);
108 break;
109 case BDC_E_CLIENT_CONN_RES:
110 /* somehow notify the main code? */
111 osmo_fsm_inst_state_chg(fi, BDC_ST_CONNECTED, 0, 0);
112 break;
113 default:
114 OSMO_ASSERT(0);
115 }
116}
117
118static void bdc_st_connected(struct osmo_fsm_inst *fi, uint32_t event, void *data)
119{
120 switch (event) {
121 case BDC_E_TCP_DOWN:
122 osmo_fsm_inst_state_chg(fi, BDC_ST_REESTABLISH, T2_RECONNECT, 2);
123 break;
124 default:
125 OSMO_ASSERT(0);
126 }
127}
128
129static void bdc_st_reestablish_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
130{
131 struct bankd_client *bc = (struct bankd_client *) fi->priv;
132 int rc;
133
134 /* re-create bankd_conn */
135 if (bc->bankd_conn) {
136 LOGPFSML(fi, LOGL_INFO, "Destroying existing connection to bankd\n");
137 ipa_client_conn_destroy(bc->bankd_conn);
138 bc->bankd_conn = NULL;
139 }
140 LOGPFSML(fi, LOGL_INFO, "Creating TCP connection to bankd at %s:%u\n",
141 bc->bankd_host, bc->bankd_port);
142 bc->bankd_conn = ipa_client_conn_create(bc, NULL, 0, bc->bankd_host, bc->bankd_port,
143 bankd_updown_cb, bankd_read_cb, NULL, bc);
144 if (!bc->bankd_conn) {
145 fprintf(stderr, "Unable to create socket: %s\n", strerror(errno));
146 exit(1);
147 }
148
149 /* Attempt to connect TCP socket */
150 rc = ipa_client_conn_open(bc->bankd_conn);
151 if (rc < 0) {
152 fprintf(stderr, "Unable to connect RSPRO to %s:%d - %s\n",
153 bc->bankd_conn->addr, bc->bankd_conn->port, strerror(errno));
154 /* FIXME: retry? Timer? Abort? */
155 OSMO_ASSERT(0);
156 }
157}
158
159static void bdc_st_reestablish(struct osmo_fsm_inst *fi, uint32_t event, void *data)
160{
161 switch (event) {
162 case BDC_E_TCP_UP:
163 osmo_fsm_inst_state_chg(fi, BDC_ST_ESTABLISHED, T1_WAIT_CLIENT_CONN_RES, 1);
164 break;
165 case BDC_E_TCP_DOWN:
166 /* wait for normal T2 timeout */
167 break;
168 default:
169 OSMO_ASSERT(0);
170 }
171}
172
173static void bdc_allstate_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
174{
175 switch (event) {
Harald Welte75f56032019-03-17 09:35:41 +0100176 case BDC_E_ESTABLISH:
Harald Welte707c85a2019-03-09 12:56:35 +0100177 osmo_fsm_inst_state_chg(fi, BDC_ST_REESTABLISH, T2_RECONNECT, 2);
178 break;
179 default:
180 OSMO_ASSERT(0);
181 }
182}
183
184static int remsim_client_bankd_fsm_timer_cb(struct osmo_fsm_inst *fi)
185{
186 switch (fi->T) {
187 case 2:
188 /* TCP reconnect failed: retry */
189 osmo_fsm_inst_state_chg(fi, BDC_ST_REESTABLISH, T2_RECONNECT, 2);
190 break;
191 case 1:
192 /* no ClientConnectRes received: disconnect + reconnect */
193 osmo_fsm_inst_state_chg(fi, BDC_ST_REESTABLISH, T2_RECONNECT, 2);
194 break;
195 default:
196 OSMO_ASSERT(0);
197 }
198 return 0;
199}
200
201static const struct osmo_fsm_state bankd_conn_fsm_states[] = {
202 [BDC_ST_INIT] = {
203 .name = "INIT",
204 .in_event_mask = 0, /* S(BDC_E_ESTABLISH) via allstate */
205 .out_state_mask = S(BDC_ST_REESTABLISH),
206 .action = bdc_st_init,
207 },
208 [BDC_ST_ESTABLISHED] = {
209 .name = "ESTABLISHED",
210 .in_event_mask = S(BDC_E_TCP_DOWN) | S(BDC_E_CLIENT_CONN_RES),
211 .out_state_mask = S(BDC_ST_CONNECTED) | S(BDC_ST_REESTABLISH),
212 .action = bdc_st_established,
213 .onenter = bdc_st_established_onenter,
214 },
215 [BDC_ST_CONNECTED] = {
216 .name = "CONNECTED",
217 .in_event_mask = S(BDC_E_TCP_DOWN),
218 .out_state_mask = S(BDC_ST_REESTABLISH),
219 .action = bdc_st_connected,
220 },
221 [BDC_ST_REESTABLISH] = {
222 .name = "REESTABLISH",
223 .in_event_mask = S(BDC_E_TCP_UP) | S(BDC_E_TCP_DOWN),
224 .out_state_mask = S(BDC_ST_ESTABLISHED) | S(BDC_ST_REESTABLISH),
225 .action = bdc_st_reestablish,
226 .onenter = bdc_st_reestablish_onenter,
227 },
228};
229
230struct osmo_fsm remsim_client_bankd_fsm = {
231 .name = "BANKD_CONN",
232 .states = bankd_conn_fsm_states,
233 .num_states = ARRAY_SIZE(bankd_conn_fsm_states),
234 .allstate_event_mask = S(BDC_E_ESTABLISH),
235 .allstate_action = bdc_allstate_action,
236 .timer_cb = remsim_client_bankd_fsm_timer_cb,
237 .log_subsys = DMAIN,
238 .event_names = remsim_client_bankd_fsm_event_names,
239};
240
241int bankd_conn_fsm_alloc(struct bankd_client *bc)
242{
243 struct osmo_fsm_inst *fi;
244
245 fi = osmo_fsm_inst_alloc(&remsim_client_bankd_fsm, bc, bc, LOGL_DEBUG, "bankd");
246 if (!fi)
247 return -1;
248
249 bc->bankd_fi = fi;
250 return 0;
251}
252
253static __attribute__((constructor)) void on_dso_load(void)
254{
255 osmo_fsm_register(&remsim_client_bankd_fsm);
256}