aboutsummaryrefslogtreecommitdiffstats
path: root/src/input/ipa_keepalive.c
blob: 81b5a264c4b314d06f882c055f46b8f91abeaf39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/* IPA keep-alive FSM; Periodically transmit IPA_PING and expect IPA_PONG in return.
 *
 * (C) 2019 by Harald Welte <laforge@gnumonks.org>
 *
 * All Rights Reserved
 *
 * SPDX-License-Identifier: GPL-2.0+
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#include <osmocom/core/fsm.h>
#include <osmocom/core/timer.h>
#include <osmocom/core/msgb.h>
#include <osmocom/core/logging.h>

#include <osmocom/gsm/protocol/ipaccess.h>

#include <osmocom/abis/ipa.h>

#define S(x)	(1 << (x))


/* generate a msgb containing an IPA CCM PING message */
static struct msgb *gen_ipa_ping(void)
{
	struct msgb *msg = msgb_alloc_headroom(64, 32, "IPA PING");
	if (!msg)
		return NULL;

	msgb_put_u8(msg, IPAC_MSGT_PING);
	ipa_msg_push_header(msg, IPAC_PROTO_IPACCESS);

	return msg;
}

enum osmo_ipa_keepalive_state {
	OSMO_IPA_KA_S_INIT,
	OSMO_IPA_KA_S_IDLE,		/* waiting for next interval */
	OSMO_IPA_KA_S_WAIT_RESP,	/* waiting for response to keepalive */
};

enum osmo_ipa_keepalive_event {
	OSMO_IPA_KA_E_START,
	OSMO_IPA_KA_E_STOP,
	OSMO_IPA_KA_E_PONG,
};

static const struct value_string ipa_keepalive_event_names[] = {
	OSMO_VALUE_STRING(OSMO_IPA_KA_E_START),
	OSMO_VALUE_STRING(OSMO_IPA_KA_E_STOP),
	OSMO_VALUE_STRING(OSMO_IPA_KA_E_PONG),
	{ 0, NULL }
};

enum ipa_fsm_timer {
	T_SEND_NEXT_PING = 1,
	T_PONG_NOT_RECEIVED = 2,
};

struct ipa_fsm_priv {
	struct ipa_keepalive_params params;

	struct ipa_server_conn *srv_conn;
	struct ipa_client_conn *client_conn;
	ipa_keepalive_timeout_cb_t *timeout_cb;
};

static void ipa_ka_init(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
	struct ipa_fsm_priv *ifp = fi->priv;

	switch (event) {
	case OSMO_IPA_KA_E_START:
		osmo_fsm_inst_state_chg(fi, OSMO_IPA_KA_S_WAIT_RESP,
					ifp->params.wait_for_resp, T_PONG_NOT_RECEIVED);
		break;
	default:
		OSMO_ASSERT(0);
		break;
	}
}

static void ipa_ka_wait_resp_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
{
	struct ipa_fsm_priv *ifp = fi->priv;
	struct msgb *msg;

	/* Send an IPA PING to the peer */
	msg = gen_ipa_ping();
	OSMO_ASSERT(msg);

	if (ifp->srv_conn)
		ipa_server_conn_send(ifp->srv_conn, msg);
	else {
		OSMO_ASSERT(ifp->client_conn);
		ipa_client_conn_send(ifp->client_conn, msg);
	}
}

static void ipa_ka_wait_resp(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
	struct ipa_fsm_priv *ifp = fi->priv;

	switch (event) {
	case OSMO_IPA_KA_E_PONG:
		osmo_fsm_inst_state_chg(fi, OSMO_IPA_KA_S_IDLE,
					ifp->params.interval, T_SEND_NEXT_PING);
		break;
	default:
		OSMO_ASSERT(0);
	}
}

static int ipa_ka_fsm_timer_cb(struct osmo_fsm_inst *fi)
{
	struct ipa_fsm_priv *ifp = fi->priv;
	void *conn;

	switch (fi->T) {
	case T_SEND_NEXT_PING:
		/* send another PING */
		osmo_fsm_inst_state_chg(fi, OSMO_IPA_KA_S_WAIT_RESP,
					ifp->params.wait_for_resp, T_PONG_NOT_RECEIVED);
		return 0;
	case T_PONG_NOT_RECEIVED:
		/* PONG not received within time */
		if (ifp->srv_conn)
			conn = ifp->srv_conn;
		else
			conn = ifp->client_conn;
		if (ifp->timeout_cb)
			ifp->timeout_cb(fi, conn);
		/* ask fsm core to terminate us */
		return 1;
	default:
		OSMO_ASSERT(0);
	}
}

static void ipa_ka_allstate_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
	switch (event) {
	case OSMO_IPA_KA_E_STOP:
		osmo_fsm_inst_state_chg(fi, OSMO_IPA_KA_S_INIT, 0, 0);
		break;
	default:
		OSMO_ASSERT(0);
		break;
	}
}

static const struct osmo_fsm_state ipa_keepalive_states[] = {
	[OSMO_IPA_KA_S_INIT] = {
		.name = "INIT",
		.in_event_mask = S(OSMO_IPA_KA_E_START),
		.out_state_mask = S(OSMO_IPA_KA_S_WAIT_RESP) | S(OSMO_IPA_KA_S_INIT),
		.action = ipa_ka_init,
	},
	[OSMO_IPA_KA_S_IDLE] = {
		.name = "IDLE",
		.out_state_mask = S(OSMO_IPA_KA_S_WAIT_RESP) | S(OSMO_IPA_KA_S_INIT),
		/* no permitted events aside from E_START, which is handled in allstate_events */
	},
	[OSMO_IPA_KA_S_WAIT_RESP] = {
		.name = "WAIT_RESP",
		.in_event_mask = S(OSMO_IPA_KA_E_PONG),
		.out_state_mask = S(OSMO_IPA_KA_S_IDLE) | S(OSMO_IPA_KA_S_INIT),
		.action = ipa_ka_wait_resp,
		.onenter = ipa_ka_wait_resp_onenter,
	},
};

static struct osmo_fsm ipa_keepalive_fsm = {
	.name = "IPA-KEEPALIVE",
	.states = ipa_keepalive_states,
	.num_states = ARRAY_SIZE(ipa_keepalive_states),
	.log_subsys = DLINP,
	.allstate_event_mask = S(OSMO_IPA_KA_E_STOP),
	.allstate_action = ipa_ka_allstate_action,
	.event_names = ipa_keepalive_event_names,
	.timer_cb = ipa_ka_fsm_timer_cb,
};

static __attribute__((constructor)) void on_dso_load(void)
{
	osmo_fsm_register(&ipa_keepalive_fsm);
}


static struct osmo_fsm_inst *
__ipa_conn_alloc_keepalive_fsm(void *ctx, const struct ipa_keepalive_params *params, const char *id)
{
	struct osmo_fsm_inst *fi;
	struct ipa_fsm_priv *ifp;

	fi = osmo_fsm_inst_alloc(&ipa_keepalive_fsm, ctx, NULL, LOGL_DEBUG, id);
	if (!fi)
		return NULL;
	ifp = talloc_zero(fi, struct ipa_fsm_priv);
	if (!ifp) {
		osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
		return NULL;
	}
	memcpy(&ifp->params, params, sizeof(ifp->params));
	fi->priv = ifp;

	return fi;
}

/*! Create a new instance of an IPA keepalive FSM: Periodically transmit PING and expect PONG.
 *  \param[in] client The client connection for which to crate the FSM. Used as talloc context.
 *  \param[in] params Parameters describing the keepalive FSM time-outs.
 *  \param[in] id String used as identifier for the FSM.
 *  \returns pointer to the newly-created FSM instance; NULL in case of error. */
struct osmo_fsm_inst *ipa_client_conn_alloc_keepalive_fsm(struct ipa_client_conn *client,
							  const struct ipa_keepalive_params *params,
							  const char *id)
{
	struct osmo_fsm_inst *fi;
	struct ipa_fsm_priv *ifp;

	fi = __ipa_conn_alloc_keepalive_fsm(client, params, id);
	if (!fi)
		return NULL;
	ifp = fi->priv;
	ifp->client_conn = client;
	return fi;
}

/*! Create a new instance of an IPA keepalive FSM: Periodically transmit PING and expect PONG.
 *  \param[in] server The server connection for which to crate the FSM. Used as talloc context.
 *  \param[in] params Parameters describing the keepalive FSM time-outs.
 *  \param[in] id String used as identifier for the FSM.
 *  \returns pointer to the newly-created FSM instance; NULL in case of error. */
struct osmo_fsm_inst *ipa_server_conn_alloc_keepalive_fsm(struct ipa_server_conn *server,
							  const struct ipa_keepalive_params *params,
							  const char *id)
{
	struct osmo_fsm_inst *fi;
	struct ipa_fsm_priv *ifp;

	fi = __ipa_conn_alloc_keepalive_fsm(server, params, id);
	if (!fi)
		return NULL;
	ifp = fi->priv;
	ifp->srv_conn = server;
	return fi;
}

/*! Set a timeout call-back which is to be called once the peer doesn't respond anymore */
void ipa_keepalive_fsm_set_timeout_cb(struct osmo_fsm_inst *fi, ipa_keepalive_timeout_cb_t *cb)
{
	struct ipa_fsm_priv *ifp = fi->priv;
	OSMO_ASSERT(fi->fsm == &ipa_keepalive_fsm);
	ifp->timeout_cb = cb;
}

/*! Inform IPA Keepalive FSM that a PONG has been received. */
void ipa_keepalive_fsm_pong_received(struct osmo_fsm_inst *fi)
{
	OSMO_ASSERT(fi->fsm == &ipa_keepalive_fsm);
	osmo_fsm_inst_dispatch(fi, OSMO_IPA_KA_E_PONG, NULL);
}

/*! Start the ping/pong procedure of the IPA Keepalive FSM. */
void ipa_keepalive_fsm_start(struct osmo_fsm_inst *fi)
{
	OSMO_ASSERT(fi->fsm == &ipa_keepalive_fsm);
	osmo_fsm_inst_dispatch(fi, OSMO_IPA_KA_E_START, NULL);
}

/*! Stop the ping/pong procedure of the IPA Keepalive FSM. */
void ipa_keepalive_fsm_stop(struct osmo_fsm_inst *fi)
{
	OSMO_ASSERT(fi->fsm == &ipa_keepalive_fsm);
	osmo_fsm_inst_dispatch(fi, OSMO_IPA_KA_E_STOP, NULL);
}