blob: 1aae0965739a60ce190bf8fb813b4c5ccb63414a [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,
192 .allstate_action = ipa_ka_allstate_action,
193 .event_names = ipa_keepalive_event_names,
194 .timer_cb = ipa_ka_fsm_timer_cb,
195};
196
197static __attribute__((constructor)) void on_dso_load(void)
198{
199 osmo_fsm_register(&ipa_keepalive_fsm);
200}
201
202
203static struct osmo_fsm_inst *
204__ipa_conn_alloc_keepalive_fsm(void *ctx, const struct ipa_keepalive_params *params, const char *id)
205{
206 struct osmo_fsm_inst *fi;
207 struct ipa_fsm_priv *ifp;
208
209 fi = osmo_fsm_inst_alloc(&ipa_keepalive_fsm, ctx, NULL, LOGL_DEBUG, id);
210 if (!fi)
211 return NULL;
212 ifp = talloc_zero(fi, struct ipa_fsm_priv);
213 if (!ifp) {
214 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
215 return NULL;
216 }
217 memcpy(&ifp->params, params, sizeof(ifp->params));
218 fi->priv = ifp;
219
220 return fi;
221}
222
223/*! Create a new instance of an IPA keepalive FSM: Periodically transmit PING and expect PONG.
224 * \param[in] client The client connection for which to crate the FSM. Used as talloc context.
225 * \param[in] params Parameters describing the keepalive FSM time-outs.
226 * \param[in] id String used as identifier for the FSM.
227 * \returns pointer to the newly-created FSM instance; NULL in case of error. */
228struct osmo_fsm_inst *ipa_client_conn_alloc_keepalive_fsm(struct ipa_client_conn *client,
229 const struct ipa_keepalive_params *params,
230 const char *id)
231{
232 struct osmo_fsm_inst *fi;
233 struct ipa_fsm_priv *ifp;
234
235 fi = __ipa_conn_alloc_keepalive_fsm(client, params, id);
236 if (!fi)
237 return NULL;
238 ifp = fi->priv;
239 ifp->client_conn = client;
240 return fi;
241}
242
243/*! Create a new instance of an IPA keepalive FSM: Periodically transmit PING and expect PONG.
244 * \param[in] server The server connection for which to crate the FSM. Used as talloc context.
245 * \param[in] params Parameters describing the keepalive FSM time-outs.
246 * \param[in] id String used as identifier for the FSM.
247 * \returns pointer to the newly-created FSM instance; NULL in case of error. */
248struct osmo_fsm_inst *ipa_server_conn_alloc_keepalive_fsm(struct ipa_server_conn *server,
249 const struct ipa_keepalive_params *params,
250 const char *id)
251{
252 struct osmo_fsm_inst *fi;
253 struct ipa_fsm_priv *ifp;
254
255 fi = __ipa_conn_alloc_keepalive_fsm(server, params, id);
256 if (!fi)
257 return NULL;
258 ifp = fi->priv;
259 ifp->srv_conn = server;
260 return fi;
261}
262
263/*! Set a timeout call-back which is to be called once the peer doesn't respond anymore */
264void ipa_keepalive_fsm_set_timeout_cb(struct osmo_fsm_inst *fi, ipa_keepalive_timeout_cb_t *cb)
265{
266 struct ipa_fsm_priv *ifp = fi->priv;
267 OSMO_ASSERT(fi->fsm == &ipa_keepalive_fsm);
268 ifp->timeout_cb = cb;
269}
270
271/*! Inform IPA Keepalive FSM that a PONG has been received. */
272void ipa_keepalive_fsm_pong_received(struct osmo_fsm_inst *fi)
273{
274 OSMO_ASSERT(fi->fsm == &ipa_keepalive_fsm);
275 osmo_fsm_inst_dispatch(fi, OSMO_IPA_KA_E_PONG, NULL);
276}
277
278/*! Start the ping/pong procedure of the IPA Keepalive FSM. */
279void ipa_keepalive_fsm_start(struct osmo_fsm_inst *fi)
280{
281 OSMO_ASSERT(fi->fsm == &ipa_keepalive_fsm);
282 osmo_fsm_inst_dispatch(fi, OSMO_IPA_KA_E_START, NULL);
283}
284
285/*! Stop the ping/pong procedure of the IPA Keepalive FSM. */
286void ipa_keepalive_fsm_stop(struct osmo_fsm_inst *fi)
287{
288 OSMO_ASSERT(fi->fsm == &ipa_keepalive_fsm);
289 osmo_fsm_inst_dispatch(fi, OSMO_IPA_KA_E_STOP, NULL);
290}