blob: 646772039baa182c2d1b7ca1a1ebe4efb81444b8 [file] [log] [blame]
Harald Welte3d60dbd2019-03-08 15:10:30 +01001/* IPA keep-alive FSM; Periodically transmit IPA_PING and expect IPA_PONG in return.
2 *
3 * (C) 2019 by Harald Welte <laforge@gnumonks.org>
4 *
5 * All Rights Reserved
6 *
7 * SPDX-License-Identifier: GPL-2.0+
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25#include <osmocom/core/fsm.h>
26#include <osmocom/core/timer.h>
27#include <osmocom/core/msgb.h>
28#include <osmocom/core/logging.h>
29
30#include <osmocom/gsm/protocol/ipaccess.h>
31
32#include <osmocom/abis/ipa.h>
33
34#define S(x) (1 << (x))
35
36
37/* generate a msgb containing an IPA CCM PING message */
38static struct msgb *gen_ipa_ping(void)
39{
40 struct msgb *msg = msgb_alloc_headroom(64, 32, "IPA PING");
41 if (!msg)
42 return NULL;
43
44 msgb_put_u8(msg, IPAC_MSGT_PING);
45 ipa_msg_push_header(msg, IPAC_PROTO_IPACCESS);
46
47 return msg;
48}
49
50enum osmo_ipa_keepalive_state {
51 OSMO_IPA_KA_S_INIT,
52 OSMO_IPA_KA_S_IDLE, /* waiting for next interval */
53 OSMO_IPA_KA_S_WAIT_RESP, /* waiting for response to keepalive */
54};
55
56enum osmo_ipa_keepalive_event {
57 OSMO_IPA_KA_E_START,
58 OSMO_IPA_KA_E_STOP,
59 OSMO_IPA_KA_E_PONG,
60};
61
62static const struct value_string ipa_keepalive_event_names[] = {
63 OSMO_VALUE_STRING(OSMO_IPA_KA_E_START),
64 OSMO_VALUE_STRING(OSMO_IPA_KA_E_STOP),
65 OSMO_VALUE_STRING(OSMO_IPA_KA_E_PONG),
66 { 0, NULL }
67};
68
69enum ipa_fsm_timer {
70 T_SEND_NEXT_PING = 1,
71 T_PONG_NOT_RECEIVED = 2,
72};
73
74struct ipa_fsm_priv {
75 struct ipa_keepalive_params params;
76
77 struct ipa_server_conn *srv_conn;
78 struct ipa_client_conn *client_conn;
79 ipa_keepalive_timeout_cb_t *timeout_cb;
80};
81
82static void ipa_ka_init(struct osmo_fsm_inst *fi, uint32_t event, void *data)
83{
84 struct ipa_fsm_priv *ifp = fi->priv;
85
86 switch (event) {
87 case OSMO_IPA_KA_E_START:
88 osmo_fsm_inst_state_chg(fi, OSMO_IPA_KA_S_WAIT_RESP,
89 ifp->params.wait_for_resp, T_PONG_NOT_RECEIVED);
90 break;
91 default:
92 OSMO_ASSERT(0);
93 break;
94 }
95}
96
97static void ipa_ka_wait_resp_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
98{
99 struct ipa_fsm_priv *ifp = fi->priv;
100 struct msgb *msg;
101
102 /* Send an IPA PING to the peer */
103 msg = gen_ipa_ping();
104 OSMO_ASSERT(msg);
105
106 if (ifp->srv_conn)
107 ipa_server_conn_send(ifp->srv_conn, msg);
108 else {
109 OSMO_ASSERT(ifp->client_conn);
110 ipa_client_conn_send(ifp->client_conn, msg);
111 }
112}
113
114static void ipa_ka_wait_resp(struct osmo_fsm_inst *fi, uint32_t event, void *data)
115{
116 struct ipa_fsm_priv *ifp = fi->priv;
117
118 switch (event) {
119 case OSMO_IPA_KA_E_PONG:
120 osmo_fsm_inst_state_chg(fi, OSMO_IPA_KA_S_IDLE,
121 ifp->params.interval, T_SEND_NEXT_PING);
122 break;
123 default:
124 OSMO_ASSERT(0);
125 }
126}
127
128static int ipa_ka_fsm_timer_cb(struct osmo_fsm_inst *fi)
129{
130 struct ipa_fsm_priv *ifp = fi->priv;
131 void *conn;
132
133 switch (fi->T) {
134 case T_SEND_NEXT_PING:
135 /* send another PING */
136 osmo_fsm_inst_state_chg(fi, OSMO_IPA_KA_S_WAIT_RESP,
137 ifp->params.wait_for_resp, T_PONG_NOT_RECEIVED);
138 return 0;
139 case T_PONG_NOT_RECEIVED:
140 /* PONG not received within time */
141 if (ifp->srv_conn)
142 conn = ifp->srv_conn;
143 else
144 conn = ifp->client_conn;
145 if (ifp->timeout_cb)
146 ifp->timeout_cb(fi, conn);
147 /* ask fsm core to terminate us */
148 return 1;
149 default:
150 OSMO_ASSERT(0);
151 }
152}
153
154static void ipa_ka_allstate_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
155{
156 switch (event) {
157 case OSMO_IPA_KA_E_STOP:
158 osmo_fsm_inst_state_chg(fi, OSMO_IPA_KA_S_INIT, 0, 0);
159 break;
160 default:
161 OSMO_ASSERT(0);
162 break;
163 }
164}
165
166static const struct osmo_fsm_state ipa_keepalive_states[] = {
167 [OSMO_IPA_KA_S_INIT] = {
168 .name = "INIT",
169 .in_event_mask = S(OSMO_IPA_KA_E_START),
170 .out_state_mask = S(OSMO_IPA_KA_S_WAIT_RESP),
171 .action = ipa_ka_init,
172 },
173 [OSMO_IPA_KA_S_IDLE] = {
174 .name = "IDLE",
175 .out_state_mask = S(OSMO_IPA_KA_S_WAIT_RESP) | S(OSMO_IPA_KA_S_INIT),
176 /* no permitted events aside from E_START, which is handled in allstate_events */
177 },
178 [OSMO_IPA_KA_S_WAIT_RESP] = {
179 .name = "WAIT_RESP",
180 .in_event_mask = S(OSMO_IPA_KA_E_PONG),
181 .out_state_mask = S(OSMO_IPA_KA_S_IDLE) | S(OSMO_IPA_KA_S_INIT),
182 .action = ipa_ka_wait_resp,
183 .onenter = ipa_ka_wait_resp_onenter,
184 },
185};
186
187static struct osmo_fsm ipa_keepalive_fsm = {
188 .name = "IPA-KEEPALIVE",
189 .states = ipa_keepalive_states,
190 .num_states = ARRAY_SIZE(ipa_keepalive_states),
191 .log_subsys = DLINP,
Harald Welte02c5e9d2019-04-06 20:22:43 +0200192 .allstate_event_mask = S(OSMO_IPA_KA_E_STOP),
Harald Welte3d60dbd2019-03-08 15:10:30 +0100193 .allstate_action = ipa_ka_allstate_action,
194 .event_names = ipa_keepalive_event_names,
195 .timer_cb = ipa_ka_fsm_timer_cb,
196};
197
198static __attribute__((constructor)) void on_dso_load(void)
199{
200 osmo_fsm_register(&ipa_keepalive_fsm);
201}
202
203
204static struct osmo_fsm_inst *
205__ipa_conn_alloc_keepalive_fsm(void *ctx, const struct ipa_keepalive_params *params, const char *id)
206{
207 struct osmo_fsm_inst *fi;
208 struct ipa_fsm_priv *ifp;
209
210 fi = osmo_fsm_inst_alloc(&ipa_keepalive_fsm, ctx, NULL, LOGL_DEBUG, id);
211 if (!fi)
212 return NULL;
213 ifp = talloc_zero(fi, struct ipa_fsm_priv);
214 if (!ifp) {
215 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
216 return NULL;
217 }
218 memcpy(&ifp->params, params, sizeof(ifp->params));
219 fi->priv = ifp;
220
221 return fi;
222}
223
224/*! Create a new instance of an IPA keepalive FSM: Periodically transmit PING and expect PONG.
225 * \param[in] client The client connection for which to crate the FSM. Used as talloc context.
226 * \param[in] params Parameters describing the keepalive FSM time-outs.
227 * \param[in] id String used as identifier for the FSM.
228 * \returns pointer to the newly-created FSM instance; NULL in case of error. */
229struct osmo_fsm_inst *ipa_client_conn_alloc_keepalive_fsm(struct ipa_client_conn *client,
230 const struct ipa_keepalive_params *params,
231 const char *id)
232{
233 struct osmo_fsm_inst *fi;
234 struct ipa_fsm_priv *ifp;
235
236 fi = __ipa_conn_alloc_keepalive_fsm(client, params, id);
237 if (!fi)
238 return NULL;
239 ifp = fi->priv;
240 ifp->client_conn = client;
241 return fi;
242}
243
244/*! Create a new instance of an IPA keepalive FSM: Periodically transmit PING and expect PONG.
245 * \param[in] server The server connection for which to crate the FSM. Used as talloc context.
246 * \param[in] params Parameters describing the keepalive FSM time-outs.
247 * \param[in] id String used as identifier for the FSM.
248 * \returns pointer to the newly-created FSM instance; NULL in case of error. */
249struct osmo_fsm_inst *ipa_server_conn_alloc_keepalive_fsm(struct ipa_server_conn *server,
250 const struct ipa_keepalive_params *params,
251 const char *id)
252{
253 struct osmo_fsm_inst *fi;
254 struct ipa_fsm_priv *ifp;
255
256 fi = __ipa_conn_alloc_keepalive_fsm(server, params, id);
257 if (!fi)
258 return NULL;
259 ifp = fi->priv;
260 ifp->srv_conn = server;
261 return fi;
262}
263
264/*! Set a timeout call-back which is to be called once the peer doesn't respond anymore */
265void ipa_keepalive_fsm_set_timeout_cb(struct osmo_fsm_inst *fi, ipa_keepalive_timeout_cb_t *cb)
266{
267 struct ipa_fsm_priv *ifp = fi->priv;
268 OSMO_ASSERT(fi->fsm == &ipa_keepalive_fsm);
269 ifp->timeout_cb = cb;
270}
271
272/*! Inform IPA Keepalive FSM that a PONG has been received. */
273void ipa_keepalive_fsm_pong_received(struct osmo_fsm_inst *fi)
274{
275 OSMO_ASSERT(fi->fsm == &ipa_keepalive_fsm);
276 osmo_fsm_inst_dispatch(fi, OSMO_IPA_KA_E_PONG, NULL);
277}
278
279/*! Start the ping/pong procedure of the IPA Keepalive FSM. */
280void ipa_keepalive_fsm_start(struct osmo_fsm_inst *fi)
281{
282 OSMO_ASSERT(fi->fsm == &ipa_keepalive_fsm);
283 osmo_fsm_inst_dispatch(fi, OSMO_IPA_KA_E_START, NULL);
284}
285
286/*! Stop the ping/pong procedure of the IPA Keepalive FSM. */
287void ipa_keepalive_fsm_stop(struct osmo_fsm_inst *fi)
288{
289 OSMO_ASSERT(fi->fsm == &ipa_keepalive_fsm);
290 osmo_fsm_inst_dispatch(fi, OSMO_IPA_KA_E_STOP, NULL);
291}