blob: 6a92f83c98dc9f338c5c46f1020801a7bf36c5fa [file] [log] [blame]
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +01001/* nacc_fsm.c
2 *
3 * Copyright (C) 2021 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
4 * Author: Pau Espin Pedrol <pespin@sysmocom.de>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21#include <unistd.h>
22
23#include <talloc.h>
24
25#include <osmocom/core/rate_ctr.h>
26#include <osmocom/ctrl/control_cmd.h>
27#include <osmocom/ctrl/control_if.h>
28
29#include <osmocom/gsm/gsm48.h>
30#include <osmocom/gprs/gprs_bssgp.h>
31#include <osmocom/gprs/gprs_bssgp_rim.h>
32
33#include <nacc_fsm.h>
34#include <gprs_rlcmac.h>
35#include <gprs_debug.h>
36#include <gprs_ms.h>
37#include <encoding.h>
38#include <bts.h>
39#include <neigh_cache.h>
40
41#define X(s) (1 << (s))
42
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +010043static const struct osmo_tdef_state_timeout nacc_fsm_timeouts[32] = {
44 [NACC_ST_INITIAL] = {},
45 [NACC_ST_WAIT_RESOLVE_RAC_CI] = { .T = PCU_TDEF_NEIGH_RESOLVE_TO },
46 [NACC_ST_WAIT_REQUEST_SI] = { .T = PCU_TDEF_SI_RESOLVE_TO },
47 [NACC_ST_TX_NEIGHBOUR_DATA] = {},
48 [NACC_ST_TX_CELL_CHG_CONTINUE] = {},
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +010049 [NACC_ST_WAIT_CELL_CHG_CONTINUE_ACK] = {}, /* Timeout through event controlled by tbf::poll_timeout() */
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +010050 [NACC_ST_DONE] = {},
51};
52
53/* Transition to a state, using the T timer defined in assignment_fsm_timeouts.
54 * The actual timeout value is in turn obtained from conn->T_defs.
55 * Assumes local variable fi exists. */
56
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +010057#define nacc_fsm_state_chg(fi, NEXT_STATE) \
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +010058 osmo_tdef_fsm_inst_state_chg(fi, NEXT_STATE, \
59 nacc_fsm_timeouts, \
60 ((struct nacc_fsm_ctx*)(fi->priv))->ms->bts->pcu->T_defs, \
61 -1)
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +010062
63const struct value_string nacc_fsm_event_names[] = {
64 { NACC_EV_RX_CELL_CHG_NOTIFICATION, "RX_CELL_CHG_NOTIFICATION" },
65 { NACC_EV_RX_RAC_CI, "RX_RAC_CI" },
66 { NACC_EV_RX_SI, "RX_SI" },
67 { NACC_EV_CREATE_RLCMAC_MSG, "CREATE_RLCMAC_MSG" },
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +010068 { NACC_EV_RX_CELL_CHG_CONTINUE_ACK, "RX_CELL_CHG_CONTINUE_ACK"},
69 { NACC_EV_TIMEOUT_CELL_CHG_CONTINUE, "TIMEOUT_CELL_CHG_CONTINUE" },
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +010070 { 0, NULL }
71};
72
73/* TS 44 060 11.2.9e Packet Neighbour Cell Data */
74static struct msgb *create_packet_neighbour_cell_data(struct nacc_fsm_ctx *ctx,
75 const struct gprs_rlcmac_tbf *tbf,
76 bool *all_si_info_sent)
77{
78 struct msgb *msg;
79 int rc;
80 RlcMacDownlink_t *mac_control_block;
81 struct GprsMs *ms = tbf_ms(tbf);
82 OSMO_ASSERT(tbf_is_tfi_assigned(tbf));
83 uint8_t tfi_is_dl = tbf_direction(tbf) == GPRS_RLCMAC_DL_TBF;
84 uint8_t tfi = tbf_tfi(tbf);
85 uint8_t container_id = 0;
86 PNCDContainer_t container;
87 size_t max_len, len_to_write;
88 uint8_t *cont_buf;
89 uint8_t si_type = ctx->si_info.type_psi ? 0x01 : 0x0;
90
91 memset(&container, 0, sizeof(container));
92 if (ctx->container_idx == 0) {
93 container.UnionType = 1; /* with ID */
94 container.u.PNCD_Container_With_ID.ARFCN = ctx->neigh_key.tgt_arfcn;
95 container.u.PNCD_Container_With_ID.BSIC = ctx->neigh_key.tgt_bsic;
96 cont_buf = &container.u.PNCD_Container_With_ID.CONTAINER[0];
97 max_len = sizeof(container.u.PNCD_Container_With_ID.CONTAINER) - 1;
98 } else {
99 container.UnionType = 0; /* without ID */
100 cont_buf = &container.u.PNCD_Container_Without_ID.CONTAINER[0];
101 max_len = sizeof(container.u.PNCD_Container_Without_ID.CONTAINER) - 1;
102 }
103
104 len_to_write = ctx->si_info.si_len - ctx->si_info_bytes_sent;
105
106 if (len_to_write == 0) {
107 /* We sent all info on last message filing it exactly, we now send a zeroed one to finish */
108 *all_si_info_sent = true;
109 *cont_buf = (si_type << 5) | 0x00;
110 } else if (len_to_write >= max_len) {
111 /* We fill the rlcmac block, we'll need more messages */
112 *all_si_info_sent = false;
113 *cont_buf = (si_type << 5) | 0x1F;
114 memcpy(cont_buf + 1, &ctx->si_info.si_buf[ctx->si_info_bytes_sent], max_len);
115 ctx->si_info_bytes_sent += max_len;
116 } else {
117 /* Last block, we don't fill it exactly */
118 *all_si_info_sent = true;
119 *cont_buf = (si_type << 5) | (len_to_write & 0x1F);
120 memcpy(cont_buf + 1, &ctx->si_info.si_buf[ctx->si_info_bytes_sent], len_to_write);
121 ctx->si_info_bytes_sent += len_to_write;
122 }
123
124 msg = msgb_alloc(GSM_MACBLOCK_LEN, "neighbour_cell_data");
125 if (!msg)
126 return NULL;
127
128 /* Initialize a bit vector that uses allocated msgb as the data buffer. */
129 struct bitvec bv = {
130 .data = msgb_put(msg, GSM_MACBLOCK_LEN),
131 .data_len = GSM_MACBLOCK_LEN,
132 };
133 bitvec_unhex(&bv, DUMMY_VEC);
134
135 mac_control_block = (RlcMacDownlink_t *)talloc_zero(ctx->ms, RlcMacDownlink_t);
136
137 write_packet_neighbour_cell_data(mac_control_block,
138 tfi_is_dl, tfi, container_id,
139 ctx->container_idx, &container);
140 LOGP(DNACC, LOGL_DEBUG, "+++++++++++++++++++++++++ TX : Packet Neighbour Cell Data +++++++++++++++++++++++++\n");
141 rc = encode_gsm_rlcmac_downlink(&bv, mac_control_block);
142 if (rc < 0) {
143 LOGP(DTBF, LOGL_ERROR, "Encoding of Packet Neighbour Cell Data failed (%d)\n", rc);
144 goto free_ret;
145 }
146 LOGP(DNACC, LOGL_DEBUG, "------------------------- TX : Packet Neighbour Cell Data -------------------------\n");
147 rate_ctr_inc(&bts_rate_counters(ms->bts)->ctr[CTR_PKT_NEIGH_CELL_DATA]);
148 talloc_free(mac_control_block);
149
150 ctx->container_idx++;
151
152 return msg;
153
154free_ret:
155 talloc_free(mac_control_block);
156 msgb_free(msg);
157 return NULL;
158}
159
160/* TS 44 060 11.2.2a Packet Cell Change Continue */
161static struct msgb *create_packet_cell_chg_continue(const struct nacc_fsm_ctx *ctx,
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100162 const struct nacc_ev_create_rlcmac_msg_ctx *data,
163 uint32_t *new_poll_fn)
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100164{
165 struct msgb *msg;
166 int rc;
167 RlcMacDownlink_t *mac_control_block;
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100168 struct gprs_rlcmac_tbf *tbf = data->tbf;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100169 struct GprsMs *ms = tbf_ms(tbf);
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100170 unsigned int rrbp;
171
172 rc = tbf_check_polling(tbf, data->fn, data->ts, new_poll_fn, &rrbp);
173 if (rc < 0) {
174 LOGP(DTBF, LOGL_ERROR, "Failed registering poll for Pkt Cell Chg Continue (%d)\n", rc);
175 return NULL;
176 }
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100177
178 msg = msgb_alloc(GSM_MACBLOCK_LEN, "pkt_cell_chg_continue");
179 if (!msg)
180 return NULL;
181
182 /* Initialize a bit vector that uses allocated msgb as the data buffer. */
183 struct bitvec bv = {
184 .data = msgb_put(msg, GSM_MACBLOCK_LEN),
185 .data_len = GSM_MACBLOCK_LEN,
186 };
187 bitvec_unhex(&bv, DUMMY_VEC);
188
189 mac_control_block = (RlcMacDownlink_t *)talloc_zero(ctx->ms, RlcMacDownlink_t);
190
191 OSMO_ASSERT(tbf_is_tfi_assigned(tbf));
192 uint8_t tfi_is_dl = tbf_direction(tbf) == GPRS_RLCMAC_DL_TBF;
193 uint8_t tfi = tbf_tfi(tbf);
194 uint8_t container_id = 0;
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100195 write_packet_cell_change_continue(mac_control_block, 1, rrbp, tfi_is_dl, tfi, true,
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100196 ctx->neigh_key.tgt_arfcn, ctx->neigh_key.tgt_bsic, container_id);
197 LOGP(DNACC, LOGL_DEBUG, "+++++++++++++++++++++++++ TX : Packet Cell Change Continue +++++++++++++++++++++++++\n");
198 rc = encode_gsm_rlcmac_downlink(&bv, mac_control_block);
199 if (rc < 0) {
200 LOGP(DTBF, LOGL_ERROR, "Encoding of Packet Cell Change Continue failed (%d)\n", rc);
201 goto free_ret;
202 }
203 LOGP(DNACC, LOGL_DEBUG, "------------------------- TX : Packet Cell Change Continue -------------------------\n");
204 rate_ctr_inc(&bts_rate_counters(ms->bts)->ctr[CTR_PKT_CELL_CHG_CONTINUE]);
205 talloc_free(mac_control_block);
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100206 tbf_set_polling(tbf, *new_poll_fn, data->ts, GPRS_RLCMAC_POLL_CELL_CHG_CONTINUE);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100207 return msg;
208
209free_ret:
210 talloc_free(mac_control_block);
211 msgb_free(msg);
212 return NULL;
213}
214
215static int fill_rim_ran_info_req(const struct nacc_fsm_ctx *ctx, struct bssgp_ran_information_pdu *pdu)
216{
217 struct gprs_rlcmac_bts *bts = ctx->ms->bts;
218
219 *pdu = (struct bssgp_ran_information_pdu){
220 .routing_info_dest = {
221 .discr = BSSGP_RIM_ROUTING_INFO_GERAN,
222 .geran = {
223 .raid = {
224 .mcc = ctx->cgi_ps.rai.lac.plmn.mcc,
225 .mnc = ctx->cgi_ps.rai.lac.plmn.mnc,
226 .mnc_3_digits = ctx->cgi_ps.rai.lac.plmn.mnc_3_digits,
227 .lac = ctx->cgi_ps.rai.lac.lac,
228 .rac = ctx->cgi_ps.rai.rac,
229 },
230 .cid = ctx->cgi_ps.cell_identity,
231 },
232 },
233 .routing_info_src = {
234 .discr = BSSGP_RIM_ROUTING_INFO_GERAN,
235 .geran = {
236 .raid = {
237 .mcc = bts->cgi_ps.rai.lac.plmn.mcc,
238 .mnc = bts->cgi_ps.rai.lac.plmn.mnc,
239 .mnc_3_digits = bts->cgi_ps.rai.lac.plmn.mnc_3_digits,
240 .lac = bts->cgi_ps.rai.lac.lac,
241 .rac = bts->cgi_ps.rai.rac,
242 },
243 .cid = bts->cgi_ps.cell_identity,
244 },
245 },
246 .rim_cont_iei = BSSGP_IE_RI_REQ_RIM_CONTAINER,
247 .decoded_present = true,
248 .decoded = {
249 .req_rim_cont = {
250 .app_id = BSSGP_RAN_INF_APP_ID_NACC,
251 .seq_num = 1,
252 .pdu_ind = {
253 .ack_requested = 0,
254 .pdu_type_ext = RIM_PDU_TYPE_SING_REP,
255 },
256 .prot_ver = 1,
257 .son_trans_app_id = NULL,
258 .son_trans_app_id_len = 0,
259 .u = {
260 .app_cont_nacc = {
261 .reprt_cell = ctx->cgi_ps,
262 },
263 },
264 },
265 },
266 };
267
268 return 0;
269}
270
Pau Espin Pedrol44768f22021-02-02 13:11:30 +0100271#define SI_HDR_LEN 2
272static void bts_fill_si_cache_value(const struct gprs_rlcmac_bts *bts, struct si_cache_value *val)
273{
274 val->type_psi = false;
275 val->si_len = 0;
276 if (bts->si1_is_set) {
277 osmo_static_assert(sizeof(bts->si1) - SI_HDR_LEN == BSSGP_RIM_SI_LEN, _si1_header_size);
278 memcpy(&val->si_buf[val->si_len], bts->si1 + SI_HDR_LEN, BSSGP_RIM_SI_LEN);
279 val->si_len += BSSGP_RIM_SI_LEN;
280 }
281 if (bts->si3_is_set) {
282 osmo_static_assert(sizeof(bts->si3) - SI_HDR_LEN == BSSGP_RIM_SI_LEN, _si3_header_size);
283 memcpy(&val->si_buf[val->si_len], bts->si3 + SI_HDR_LEN, BSSGP_RIM_SI_LEN);
284 val->si_len += BSSGP_RIM_SI_LEN;
285 }
286 if (bts->si13_is_set) {
287 osmo_static_assert(sizeof(bts->si13) - SI_HDR_LEN == BSSGP_RIM_SI_LEN, _si13_header_size);
288 memcpy(&val->si_buf[val->si_len], bts->si13 + SI_HDR_LEN, BSSGP_RIM_SI_LEN);
289 val->si_len += BSSGP_RIM_SI_LEN;
290 }
291}
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100292
293////////////////
294// FSM states //
295////////////////
296
297static void st_initial(struct osmo_fsm_inst *fi, uint32_t event, void *data)
298{
299 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
300 struct gprs_rlcmac_bts *bts = ctx->ms->bts;
301 Packet_Cell_Change_Notification_t *notif;
302
303 switch (event) {
304 case NACC_EV_RX_CELL_CHG_NOTIFICATION:
305 notif = (Packet_Cell_Change_Notification_t *)data;
306 switch (notif->Target_Cell.UnionType) {
307 case 0: /* GSM */
308 ctx->neigh_key.local_lac = bts->cgi_ps.rai.lac.lac;
309 ctx->neigh_key.local_ci = bts->cgi_ps.cell_identity;
310 ctx->neigh_key.tgt_arfcn = notif->Target_Cell.u.Target_Cell_GSM_Notif.ARFCN;
311 ctx->neigh_key.tgt_bsic = notif->Target_Cell.u.Target_Cell_GSM_Notif.BSIC;
312 nacc_fsm_state_chg(fi, NACC_ST_WAIT_RESOLVE_RAC_CI);
313 break;
314 default:
315 LOGPFSML(fi, LOGL_NOTICE, "TargetCell type=0x%x not supported\n",
316 notif->Target_Cell.UnionType);
317 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
318 return;
319 }
320 break;
321 default:
322 OSMO_ASSERT(0);
323 }
324}
325
326static void st_wait_resolve_rac_ci_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
327{
328 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
329 struct gprs_rlcmac_bts *bts = ctx->ms->bts;
330 struct gprs_pcu *pcu = bts->pcu;
331 const struct osmo_cell_global_id_ps *cgi_ps;
Pau Espin Pedrolc0805e62021-01-27 17:16:59 +0100332 struct ctrl_cmd *cmd = NULL;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100333 int rc;
334
335 /* First try to find the value in the cache */
336 cgi_ps = neigh_cache_lookup_value(pcu->neigh_cache, &ctx->neigh_key);
337 if (cgi_ps) {
338 ctx->cgi_ps = *cgi_ps;
339 nacc_fsm_state_chg(fi, NACC_ST_WAIT_REQUEST_SI);
340 return;
341 }
342
343 /* CGI-PS not in cache, resolve it using BSC Neighbor Resolution CTRL interface */
344
345 LOGPFSML(fi, LOGL_DEBUG, "No CGI-PS found in cache, resolving " NEIGH_CACHE_ENTRY_KEY_FMT "...\n",
346 NEIGH_CACHE_ENTRY_KEY_ARGS(&ctx->neigh_key));
347
Pau Espin Pedrolc0805e62021-01-27 17:16:59 +0100348 rc = osmo_sock_init2_ofd(&ctx->neigh_ctrl_conn->write_queue.bfd,
349 AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP,
350 NULL, 0, pcu->vty.neigh_ctrl_addr, pcu->vty.neigh_ctrl_port,
351 OSMO_SOCK_F_CONNECT);
352 if (rc < 0) {
Pau Espin Pedrolf7e1df02021-01-29 16:48:54 +0100353 LOGPFSML(fi, LOGL_ERROR,
354 "Failed to establish CTRL (neighbor resolution) connection to BSC r=%s:%u\n\n",
355 pcu->vty.neigh_ctrl_addr, pcu->vty.neigh_ctrl_port);
Pau Espin Pedrolc0805e62021-01-27 17:16:59 +0100356 goto err_term;
357 }
358
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100359 cmd = ctrl_cmd_create(ctx, CTRL_TYPE_GET);
360 if (!cmd) {
361 LOGPFSML(fi, LOGL_ERROR, "CTRL msg creation failed\n");
362 goto err_term;
363 }
364
365 cmd->id = talloc_asprintf(cmd, "1");
366 cmd->variable = talloc_asprintf(cmd, "neighbor_resolve_cgi_ps_from_lac_ci.%d.%d.%d.%d",
367 ctx->neigh_key.local_lac, ctx->neigh_key.local_ci,
368 ctx->neigh_key.tgt_arfcn, ctx->neigh_key.tgt_bsic);
369 rc = ctrl_cmd_send(&ctx->neigh_ctrl_conn->write_queue, cmd);
370 if (rc) {
371 LOGPFSML(fi, LOGL_ERROR, "CTRL msg sent failed: %d\n", rc);
372 goto err_term;
373 }
374
375 talloc_free(cmd);
376 return;
377
378err_term:
379 talloc_free(cmd);
Pau Espin Pedrola06ac182021-01-26 19:13:43 +0100380 nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100381}
382
383
384static void st_wait_resolve_rac_ci(struct osmo_fsm_inst *fi, uint32_t event, void *data)
385{
386 switch (event) {
387 case NACC_EV_RX_CELL_CHG_NOTIFICATION:
388 break;
389 case NACC_EV_RX_RAC_CI:
390 /* Assumption: ctx->cgi_ps has been filled by caller of the event */
391 nacc_fsm_state_chg(fi, NACC_ST_WAIT_REQUEST_SI);
392 break;
393 default:
394 OSMO_ASSERT(0);
395 }
396}
397
398/* At this point, we expect correct tgt cell info to be already in ctx->cgi_ps */
399static void st_wait_request_si_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
400{
401 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
402 struct gprs_rlcmac_bts *bts = ctx->ms->bts;
403 struct gprs_pcu *pcu = bts->pcu;
404 struct bssgp_ran_information_pdu pdu;
405 const struct si_cache_value *si;
Pau Espin Pedrol44768f22021-02-02 13:11:30 +0100406 struct gprs_rlcmac_bts *bts_i;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100407 int rc;
408
Pau Espin Pedrol44768f22021-02-02 13:11:30 +0100409 /* First check if the CGI-PS addresses a cell managed by this PCU. If
410 * that's the case, we already have the info and there's no need to go
411 * the RIM way since we'd end up to this same PCU on the other end anyway.
412 */
413 llist_for_each_entry(bts_i, &the_pcu->bts_list, list) {
414 if (bts_i == bts) /* Makes no sense targeting the same cell */
415 continue;
416 if (osmo_cgi_ps_cmp(&ctx->cgi_ps, &bts_i->cgi_ps) != 0)
417 continue;
418
419 LOGPFSML(fi, LOGL_DEBUG, "neighbor CGI-PS %s addresses local BTS %d\n",
420 osmo_cgi_ps_name(&ctx->cgi_ps), bts_i->nr);
421 bts_fill_si_cache_value(bts, &ctx->si_info);
422 /* Tell the PCU scheduler we are ready to go, from here one we
423 * are polled/driven by the scheduler */
424 nacc_fsm_state_chg(fi, NACC_ST_TX_NEIGHBOUR_DATA);
425 return;
426 }
427
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100428 /* First check if we have SI info for the target cell in cache */
429 si = si_cache_lookup_value(pcu->si_cache, &ctx->cgi_ps);
430 if (si) {
431 /* Copy info since cache can be deleted at any point */
432 memcpy(&ctx->si_info, si, sizeof(ctx->si_info));
433 /* Tell the PCU scheduler we are ready to go, from here one we
434 * are polled/driven by the scheduler */
435 nacc_fsm_state_chg(fi, NACC_ST_TX_NEIGHBOUR_DATA);
436 return;
437 }
438
439 /* SI info not in cache, resolve it using RIM procedure against SGSN */
440 if (fill_rim_ran_info_req(ctx, &pdu) < 0) {
Pau Espin Pedrola06ac182021-01-26 19:13:43 +0100441 nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100442 return;
443 }
444
445 rc = bssgp_tx_rim(&pdu, gprs_ns2_nse_nsei(ctx->ms->bts->nse));
446 if (rc < 0) {
447 LOGPFSML(fi, LOGL_ERROR, "Failed transmitting RIM PDU: %d\n", rc);
Pau Espin Pedrola06ac182021-01-26 19:13:43 +0100448 nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100449 return;
450 }
451}
452
453
454static void st_wait_request_si(struct osmo_fsm_inst *fi, uint32_t event, void *data)
455{
456 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
457 struct si_cache_entry *entry;
458
459 switch (event) {
460 case NACC_EV_RX_SI:
461 entry = (struct si_cache_entry *)data;
462 /* Copy info since cache can be deleted at any point */
463 memcpy(&ctx->si_info, &entry->value, sizeof(ctx->si_info));
464 /* Tell the PCU scheduler we are ready to go, from here one we
465 * are polled/driven by the scheduler */
466 nacc_fsm_state_chg(fi, NACC_ST_TX_NEIGHBOUR_DATA);
467 break;
468 default:
469 OSMO_ASSERT(0);
470 }
471}
472
473/* st_tx_neighbour_data_on_enter:
474 * At this point, we already received all required SI information to send stored
475 * in struct nacc_fsm_ctx. We now wait for scheduler to ask us to construct
476 * RLCMAC DL CTRL messages to move FSM states forward
477 */
478
479static void st_tx_neighbour_data(struct osmo_fsm_inst *fi, uint32_t event, void *data)
480{
481 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
482 struct nacc_ev_create_rlcmac_msg_ctx *data_ctx;
483 bool all_si_info_sent;
484
485 switch (event) {
486 case NACC_EV_CREATE_RLCMAC_MSG:
487 data_ctx = (struct nacc_ev_create_rlcmac_msg_ctx *)data;
488 data_ctx->msg = create_packet_neighbour_cell_data(ctx, data_ctx->tbf, &all_si_info_sent);
489 if (!data_ctx->msg) {
490 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
491 return;
492 }
493 if (all_si_info_sent) /* DONE */
494 nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE);
495 break;
496 default:
497 OSMO_ASSERT(0);
498 }
499}
500
Pau Espin Pedrol1aef1132021-02-01 19:33:59 +0100501/* st_cell_chg_continue_on_enter:
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100502 * At this point, we already sent all Pkt Cell Neighbour Change rlcmac
503 * blocks, and we only need to wait to be scheduled again to send PKT
504 * CELL CHANGE NOTIFICATION and then we are done
505 */
506
Pau Espin Pedrol1aef1132021-02-01 19:33:59 +0100507static void st_cell_chg_continue(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100508{
509 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
510 struct nacc_ev_create_rlcmac_msg_ctx *data_ctx;
511
512 switch (event) {
513 case NACC_EV_CREATE_RLCMAC_MSG:
514 data_ctx = (struct nacc_ev_create_rlcmac_msg_ctx *)data;
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100515 data_ctx->msg = create_packet_cell_chg_continue(ctx, data_ctx, &ctx->continue_poll_fn);
516 if (data_ctx->msg) {
517 ctx->continue_poll_ts = data_ctx->ts;
518 nacc_fsm_state_chg(fi, NACC_ST_WAIT_CELL_CHG_CONTINUE_ACK);
519 }
520 break;
521 default:
522 OSMO_ASSERT(0);
523 }
524}
525
526static void st_wait_cell_chg_continue_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
527{
528 switch (event) {
529 case NACC_EV_TIMEOUT_CELL_CHG_CONTINUE:
530 nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE);
531 break;
532 case NACC_EV_RX_CELL_CHG_CONTINUE_ACK:
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100533 nacc_fsm_state_chg(fi, NACC_ST_DONE);
534 break;
535 default:
536 OSMO_ASSERT(0);
537 }
538}
539
540
541static void st_done_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
542{
543 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
544}
545
546static void nacc_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
547{
548 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
549 /* after cleanup() finishes, FSM termination calls osmo_fsm_inst_free,
550 so we need to avoid double-freeing it during ctx talloc free
551 destructor */
552 talloc_reparent(ctx, ctx->ms, ctx->fi);
553 ctx->fi = NULL;
554
555 /* remove references from owning MS and free entire ctx */
556 ctx->ms->nacc = NULL;
557 talloc_free(ctx);
558}
559
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +0100560static int nacc_fsm_timer_cb(struct osmo_fsm_inst *fi)
561{
562 switch (fi->T) {
563 case PCU_TDEF_NEIGH_RESOLVE_TO:
564 case PCU_TDEF_SI_RESOLVE_TO:
565 nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE);
566 break;
567 }
568 return 0;
569}
570
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100571static struct osmo_fsm_state nacc_fsm_states[] = {
572 [NACC_ST_INITIAL] = {
573 .in_event_mask =
574 X(NACC_EV_RX_CELL_CHG_NOTIFICATION),
575 .out_state_mask =
576 X(NACC_ST_WAIT_RESOLVE_RAC_CI),
577 .name = "INITIAL",
578 .action = st_initial,
579 },
580 [NACC_ST_WAIT_RESOLVE_RAC_CI] = {
581 .in_event_mask =
582 X(NACC_EV_RX_RAC_CI),
583 .out_state_mask =
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +0100584 X(NACC_ST_WAIT_REQUEST_SI) |
585 X(NACC_ST_TX_CELL_CHG_CONTINUE),
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100586 .name = "WAIT_RESOLVE_RAC_CI",
587 .onenter = st_wait_resolve_rac_ci_on_enter,
588 .action = st_wait_resolve_rac_ci,
589 },
590 [NACC_ST_WAIT_REQUEST_SI] = {
591 .in_event_mask =
592 X(NACC_EV_RX_CELL_CHG_NOTIFICATION) |
593 X(NACC_EV_RX_SI),
594 .out_state_mask =
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +0100595 X(NACC_ST_TX_NEIGHBOUR_DATA) |
596 X(NACC_ST_TX_CELL_CHG_CONTINUE),
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100597 .name = "WAIT_REQUEST_SI",
598 .onenter = st_wait_request_si_on_enter,
599 .action = st_wait_request_si,
600 },
601 [NACC_ST_TX_NEIGHBOUR_DATA] = {
602 .in_event_mask =
603 X(NACC_EV_RX_CELL_CHG_NOTIFICATION) |
604 X(NACC_EV_RX_SI) |
605 X(NACC_EV_CREATE_RLCMAC_MSG),
606 .out_state_mask =
607 X(NACC_ST_TX_CELL_CHG_CONTINUE),
608 .name = "TX_NEIGHBOUR_DATA",
609 .action = st_tx_neighbour_data,
610 },
611 [NACC_ST_TX_CELL_CHG_CONTINUE] = {
612 .in_event_mask =
613 X(NACC_EV_RX_CELL_CHG_NOTIFICATION) |
614 X(NACC_EV_RX_SI) |
615 X(NACC_EV_CREATE_RLCMAC_MSG),
616 .out_state_mask =
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100617 X(NACC_ST_WAIT_CELL_CHG_CONTINUE_ACK),
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100618 .name = "TX_CELL_CHG_CONTINUE",
Pau Espin Pedrol1aef1132021-02-01 19:33:59 +0100619 .action = st_cell_chg_continue,
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100620 },
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100621 [NACC_ST_WAIT_CELL_CHG_CONTINUE_ACK] = {
622 .in_event_mask =
623 X(NACC_EV_RX_CELL_CHG_CONTINUE_ACK) |
624 X(NACC_EV_TIMEOUT_CELL_CHG_CONTINUE),
625 .out_state_mask =
626 X(NACC_ST_TX_CELL_CHG_CONTINUE) |
627 X(NACC_ST_DONE),
628 .name = "WAIT_CELL_CHG_CONTINUE_ACK",
629 .action = st_wait_cell_chg_continue_ack,
630 },
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100631 [NACC_ST_DONE] = {
632 .in_event_mask = 0,
633 .out_state_mask = 0,
634 .name = "DONE",
635 .onenter = st_done_on_enter,
636 },
637};
638
639static struct osmo_fsm nacc_fsm = {
640 .name = "NACC",
641 .states = nacc_fsm_states,
642 .num_states = ARRAY_SIZE(nacc_fsm_states),
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +0100643 .timer_cb = nacc_fsm_timer_cb,
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100644 .cleanup = nacc_fsm_cleanup,
645 .log_subsys = DNACC,
646 .event_names = nacc_fsm_event_names,
647};
648
649static __attribute__((constructor)) void nacc_fsm_init(void)
650{
651 OSMO_ASSERT(osmo_fsm_register(&nacc_fsm) == 0);
652}
653
654void nacc_fsm_ctrl_reply_cb(struct ctrl_handle *ctrl, struct ctrl_cmd *cmd, void *data)
655{
656 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)data;
657 char *tmp = NULL, *tok, *saveptr;
658
659 LOGPFSML(ctx->fi, LOGL_NOTICE, "Received CTRL message: type=%d %s: %s\n",
660 cmd->type, cmd->variable, osmo_escape_str(cmd->reply, -1));
661
662 if (cmd->type != CTRL_TYPE_GET_REPLY || !cmd->reply) {
Pau Espin Pedrola06ac182021-01-26 19:13:43 +0100663 nacc_fsm_state_chg(ctx->fi, NACC_ST_TX_CELL_CHG_CONTINUE);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100664 return;
665 }
666
667 /* TODO: Potentially validate cmd->variable contains same params as we
668 sent, and that cmd->id matches the original set. We may want to keep
669 the original cmd around by setting cmd->defer=1 when sending it. */
670
671 tmp = talloc_strdup(cmd, cmd->reply);
672 if (!tmp)
673 goto free_ret;
674
675 if (!(tok = strtok_r(tmp, "-", &saveptr)))
676 goto free_ret;
677 ctx->cgi_ps.rai.lac.plmn.mcc = atoi(tok);
678
679 if (!(tok = strtok_r(NULL, "-", &saveptr)))
680 goto free_ret;
681 ctx->cgi_ps.rai.lac.plmn.mnc = atoi(tok);
682
683 if (!(tok = strtok_r(NULL, "-", &saveptr)))
684 goto free_ret;
685 ctx->cgi_ps.rai.lac.lac = atoi(tok);
686
687 if (!(tok = strtok_r(NULL, "-", &saveptr)))
688 goto free_ret;
689 ctx->cgi_ps.rai.rac = atoi(tok);
690
691 if (!(tok = strtok_r(NULL, "\0", &saveptr)))
692 goto free_ret;
693 ctx->cgi_ps.cell_identity = atoi(tok);
694
695 /* Cache the cgi_ps so we can avoid requesting again same resolution for a while */
696 neigh_cache_add(ctx->ms->bts->pcu->neigh_cache, &ctx->neigh_key, &ctx->cgi_ps);
697
698 osmo_fsm_inst_dispatch(ctx->fi, NACC_EV_RX_RAC_CI, NULL);
699 return;
700
701free_ret:
702 talloc_free(tmp);
Pau Espin Pedrola06ac182021-01-26 19:13:43 +0100703 nacc_fsm_state_chg(ctx->fi, NACC_ST_TX_CELL_CHG_CONTINUE);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100704 return;
705}
706
707static int nacc_fsm_ctx_talloc_destructor(struct nacc_fsm_ctx *ctx)
708{
709 if (ctx->fi) {
710 osmo_fsm_inst_free(ctx->fi);
711 ctx->fi = NULL;
712 }
713
714 if (ctx->neigh_ctrl_conn) {
715 if (ctx->neigh_ctrl_conn->write_queue.bfd.fd != -1) {
716 osmo_wqueue_clear(&ctx->neigh_ctrl_conn->write_queue);
717 osmo_fd_unregister(&ctx->neigh_ctrl_conn->write_queue.bfd);
718 close(ctx->neigh_ctrl_conn->write_queue.bfd.fd);
719 ctx->neigh_ctrl_conn->write_queue.bfd.fd = -1;
720 }
721 }
722
723 return 0;
724}
725
726struct nacc_fsm_ctx *nacc_fsm_alloc(struct GprsMs* ms)
727{
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100728 struct nacc_fsm_ctx *ctx = talloc_zero(ms, struct nacc_fsm_ctx);
729 char buf[64];
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100730
731 talloc_set_destructor(ctx, nacc_fsm_ctx_talloc_destructor);
732
733 ctx->ms = ms;
734
735 snprintf(buf, sizeof(buf), "TLLI-0x%08x", ms_tlli(ms));
736 ctx->fi = osmo_fsm_inst_alloc(&nacc_fsm, ctx, ctx, LOGL_INFO, buf);
737 if (!ctx->fi)
738 goto free_ret;
739
740 ctx->neigh_ctrl = ctrl_handle_alloc(ctx, ctx, NULL);
741 ctx->neigh_ctrl->reply_cb = nacc_fsm_ctrl_reply_cb;
742 ctx->neigh_ctrl_conn = osmo_ctrl_conn_alloc(ctx, ctx->neigh_ctrl);
743 if (!ctx->neigh_ctrl_conn)
744 goto free_ret;
Pau Espin Pedrol202a4782021-01-27 17:05:12 +0100745 /* Older versions of osmo_ctrl_conn_alloc didn't properly initialize fd to -1,
746 * so make sure to do it here otherwise fd may be valid fd 0 and cause trouble */
747 ctx->neigh_ctrl_conn->write_queue.bfd.fd = -1;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100748 llist_add(&ctx->neigh_ctrl_conn->list_entry, &ctx->neigh_ctrl->ccon_list);
749
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100750 return ctx;
751free_ret:
752 talloc_free(ctx);
753 return NULL;
754}