Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 1 | /* 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 Pedrol | 069a637 | 2021-02-10 17:33:13 +0100 | [diff] [blame] | 43 | /* Infer CTRL id (seqnum) for a given tgt arfcn+bsic (bsic range: 0-63) */ |
| 44 | #define arfcn_bsic_2_ctrl_id(arfcn, bsic) ((arfcn) * 100 + (bsic)) |
| 45 | |
Pau Espin Pedrol | 41a22a7 | 2021-01-26 19:00:37 +0100 | [diff] [blame] | 46 | static const struct osmo_tdef_state_timeout nacc_fsm_timeouts[32] = { |
| 47 | [NACC_ST_INITIAL] = {}, |
| 48 | [NACC_ST_WAIT_RESOLVE_RAC_CI] = { .T = PCU_TDEF_NEIGH_RESOLVE_TO }, |
| 49 | [NACC_ST_WAIT_REQUEST_SI] = { .T = PCU_TDEF_SI_RESOLVE_TO }, |
| 50 | [NACC_ST_TX_NEIGHBOUR_DATA] = {}, |
| 51 | [NACC_ST_TX_CELL_CHG_CONTINUE] = {}, |
Pau Espin Pedrol | 952cb3d | 2021-02-01 14:52:48 +0100 | [diff] [blame] | 52 | [NACC_ST_WAIT_CELL_CHG_CONTINUE_ACK] = {}, /* Timeout through event controlled by tbf::poll_timeout() */ |
Pau Espin Pedrol | 41a22a7 | 2021-01-26 19:00:37 +0100 | [diff] [blame] | 53 | [NACC_ST_DONE] = {}, |
| 54 | }; |
| 55 | |
Pau Espin Pedrol | 628d881 | 2021-07-29 13:46:34 +0200 | [diff] [blame] | 56 | /* Transition to a state, using the T timer defined in nacc_fsm_timeouts. |
Pau Espin Pedrol | 41a22a7 | 2021-01-26 19:00:37 +0100 | [diff] [blame] | 57 | * The actual timeout value is in turn obtained from conn->T_defs. |
| 58 | * Assumes local variable fi exists. */ |
| 59 | |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 60 | #define nacc_fsm_state_chg(fi, NEXT_STATE) \ |
Pau Espin Pedrol | 41a22a7 | 2021-01-26 19:00:37 +0100 | [diff] [blame] | 61 | osmo_tdef_fsm_inst_state_chg(fi, NEXT_STATE, \ |
| 62 | nacc_fsm_timeouts, \ |
| 63 | ((struct nacc_fsm_ctx*)(fi->priv))->ms->bts->pcu->T_defs, \ |
| 64 | -1) |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 65 | |
| 66 | const struct value_string nacc_fsm_event_names[] = { |
| 67 | { NACC_EV_RX_CELL_CHG_NOTIFICATION, "RX_CELL_CHG_NOTIFICATION" }, |
| 68 | { NACC_EV_RX_RAC_CI, "RX_RAC_CI" }, |
| 69 | { NACC_EV_RX_SI, "RX_SI" }, |
| 70 | { NACC_EV_CREATE_RLCMAC_MSG, "CREATE_RLCMAC_MSG" }, |
Pau Espin Pedrol | 952cb3d | 2021-02-01 14:52:48 +0100 | [diff] [blame] | 71 | { NACC_EV_RX_CELL_CHG_CONTINUE_ACK, "RX_CELL_CHG_CONTINUE_ACK"}, |
| 72 | { NACC_EV_TIMEOUT_CELL_CHG_CONTINUE, "TIMEOUT_CELL_CHG_CONTINUE" }, |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 73 | { 0, NULL } |
| 74 | }; |
| 75 | |
| 76 | /* TS 44 060 11.2.9e Packet Neighbour Cell Data */ |
| 77 | static struct msgb *create_packet_neighbour_cell_data(struct nacc_fsm_ctx *ctx, |
| 78 | const struct gprs_rlcmac_tbf *tbf, |
| 79 | bool *all_si_info_sent) |
| 80 | { |
| 81 | struct msgb *msg; |
| 82 | int rc; |
| 83 | RlcMacDownlink_t *mac_control_block; |
| 84 | struct GprsMs *ms = tbf_ms(tbf); |
| 85 | OSMO_ASSERT(tbf_is_tfi_assigned(tbf)); |
| 86 | uint8_t tfi_is_dl = tbf_direction(tbf) == GPRS_RLCMAC_DL_TBF; |
| 87 | uint8_t tfi = tbf_tfi(tbf); |
| 88 | uint8_t container_id = 0; |
| 89 | PNCDContainer_t container; |
| 90 | size_t max_len, len_to_write; |
| 91 | uint8_t *cont_buf; |
| 92 | uint8_t si_type = ctx->si_info.type_psi ? 0x01 : 0x0; |
| 93 | |
| 94 | memset(&container, 0, sizeof(container)); |
| 95 | if (ctx->container_idx == 0) { |
| 96 | container.UnionType = 1; /* with ID */ |
| 97 | container.u.PNCD_Container_With_ID.ARFCN = ctx->neigh_key.tgt_arfcn; |
| 98 | container.u.PNCD_Container_With_ID.BSIC = ctx->neigh_key.tgt_bsic; |
| 99 | cont_buf = &container.u.PNCD_Container_With_ID.CONTAINER[0]; |
| 100 | max_len = sizeof(container.u.PNCD_Container_With_ID.CONTAINER) - 1; |
| 101 | } else { |
| 102 | container.UnionType = 0; /* without ID */ |
| 103 | cont_buf = &container.u.PNCD_Container_Without_ID.CONTAINER[0]; |
| 104 | max_len = sizeof(container.u.PNCD_Container_Without_ID.CONTAINER) - 1; |
| 105 | } |
| 106 | |
| 107 | len_to_write = ctx->si_info.si_len - ctx->si_info_bytes_sent; |
| 108 | |
| 109 | if (len_to_write == 0) { |
| 110 | /* We sent all info on last message filing it exactly, we now send a zeroed one to finish */ |
| 111 | *all_si_info_sent = true; |
| 112 | *cont_buf = (si_type << 5) | 0x00; |
| 113 | } else if (len_to_write >= max_len) { |
| 114 | /* We fill the rlcmac block, we'll need more messages */ |
| 115 | *all_si_info_sent = false; |
| 116 | *cont_buf = (si_type << 5) | 0x1F; |
| 117 | memcpy(cont_buf + 1, &ctx->si_info.si_buf[ctx->si_info_bytes_sent], max_len); |
| 118 | ctx->si_info_bytes_sent += max_len; |
| 119 | } else { |
| 120 | /* Last block, we don't fill it exactly */ |
| 121 | *all_si_info_sent = true; |
| 122 | *cont_buf = (si_type << 5) | (len_to_write & 0x1F); |
| 123 | memcpy(cont_buf + 1, &ctx->si_info.si_buf[ctx->si_info_bytes_sent], len_to_write); |
| 124 | ctx->si_info_bytes_sent += len_to_write; |
| 125 | } |
| 126 | |
| 127 | msg = msgb_alloc(GSM_MACBLOCK_LEN, "neighbour_cell_data"); |
| 128 | if (!msg) |
| 129 | return NULL; |
| 130 | |
| 131 | /* Initialize a bit vector that uses allocated msgb as the data buffer. */ |
| 132 | struct bitvec bv = { |
| 133 | .data = msgb_put(msg, GSM_MACBLOCK_LEN), |
| 134 | .data_len = GSM_MACBLOCK_LEN, |
| 135 | }; |
| 136 | bitvec_unhex(&bv, DUMMY_VEC); |
| 137 | |
| 138 | mac_control_block = (RlcMacDownlink_t *)talloc_zero(ctx->ms, RlcMacDownlink_t); |
| 139 | |
| 140 | write_packet_neighbour_cell_data(mac_control_block, |
| 141 | tfi_is_dl, tfi, container_id, |
| 142 | ctx->container_idx, &container); |
| 143 | LOGP(DNACC, LOGL_DEBUG, "+++++++++++++++++++++++++ TX : Packet Neighbour Cell Data +++++++++++++++++++++++++\n"); |
| 144 | rc = encode_gsm_rlcmac_downlink(&bv, mac_control_block); |
| 145 | if (rc < 0) { |
| 146 | LOGP(DTBF, LOGL_ERROR, "Encoding of Packet Neighbour Cell Data failed (%d)\n", rc); |
| 147 | goto free_ret; |
| 148 | } |
| 149 | LOGP(DNACC, LOGL_DEBUG, "------------------------- TX : Packet Neighbour Cell Data -------------------------\n"); |
Pau Espin Pedrol | 9c1db17 | 2021-06-04 17:05:51 +0200 | [diff] [blame] | 150 | rate_ctr_inc(rate_ctr_group_get_ctr(bts_rate_counters(ms->bts), CTR_PKT_NEIGH_CELL_DATA)); |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 151 | talloc_free(mac_control_block); |
| 152 | |
| 153 | ctx->container_idx++; |
| 154 | |
| 155 | return msg; |
| 156 | |
| 157 | free_ret: |
| 158 | talloc_free(mac_control_block); |
| 159 | msgb_free(msg); |
| 160 | return NULL; |
| 161 | } |
| 162 | |
| 163 | /* TS 44 060 11.2.2a Packet Cell Change Continue */ |
| 164 | static struct msgb *create_packet_cell_chg_continue(const struct nacc_fsm_ctx *ctx, |
Pau Espin Pedrol | 952cb3d | 2021-02-01 14:52:48 +0100 | [diff] [blame] | 165 | const struct nacc_ev_create_rlcmac_msg_ctx *data, |
| 166 | uint32_t *new_poll_fn) |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 167 | { |
| 168 | struct msgb *msg; |
| 169 | int rc; |
| 170 | RlcMacDownlink_t *mac_control_block; |
Pau Espin Pedrol | 952cb3d | 2021-02-01 14:52:48 +0100 | [diff] [blame] | 171 | struct gprs_rlcmac_tbf *tbf = data->tbf; |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 172 | struct GprsMs *ms = tbf_ms(tbf); |
Pau Espin Pedrol | 952cb3d | 2021-02-01 14:52:48 +0100 | [diff] [blame] | 173 | unsigned int rrbp; |
| 174 | |
| 175 | rc = tbf_check_polling(tbf, data->fn, data->ts, new_poll_fn, &rrbp); |
| 176 | if (rc < 0) { |
| 177 | LOGP(DTBF, LOGL_ERROR, "Failed registering poll for Pkt Cell Chg Continue (%d)\n", rc); |
| 178 | return NULL; |
| 179 | } |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 180 | |
| 181 | msg = msgb_alloc(GSM_MACBLOCK_LEN, "pkt_cell_chg_continue"); |
| 182 | if (!msg) |
| 183 | return NULL; |
| 184 | |
| 185 | /* Initialize a bit vector that uses allocated msgb as the data buffer. */ |
| 186 | struct bitvec bv = { |
| 187 | .data = msgb_put(msg, GSM_MACBLOCK_LEN), |
| 188 | .data_len = GSM_MACBLOCK_LEN, |
| 189 | }; |
| 190 | bitvec_unhex(&bv, DUMMY_VEC); |
| 191 | |
| 192 | mac_control_block = (RlcMacDownlink_t *)talloc_zero(ctx->ms, RlcMacDownlink_t); |
| 193 | |
| 194 | OSMO_ASSERT(tbf_is_tfi_assigned(tbf)); |
| 195 | uint8_t tfi_is_dl = tbf_direction(tbf) == GPRS_RLCMAC_DL_TBF; |
| 196 | uint8_t tfi = tbf_tfi(tbf); |
| 197 | uint8_t container_id = 0; |
Pau Espin Pedrol | 952cb3d | 2021-02-01 14:52:48 +0100 | [diff] [blame] | 198 | write_packet_cell_change_continue(mac_control_block, 1, rrbp, tfi_is_dl, tfi, true, |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 199 | ctx->neigh_key.tgt_arfcn, ctx->neigh_key.tgt_bsic, container_id); |
| 200 | LOGP(DNACC, LOGL_DEBUG, "+++++++++++++++++++++++++ TX : Packet Cell Change Continue +++++++++++++++++++++++++\n"); |
| 201 | rc = encode_gsm_rlcmac_downlink(&bv, mac_control_block); |
| 202 | if (rc < 0) { |
| 203 | LOGP(DTBF, LOGL_ERROR, "Encoding of Packet Cell Change Continue failed (%d)\n", rc); |
| 204 | goto free_ret; |
| 205 | } |
| 206 | LOGP(DNACC, LOGL_DEBUG, "------------------------- TX : Packet Cell Change Continue -------------------------\n"); |
Pau Espin Pedrol | 9c1db17 | 2021-06-04 17:05:51 +0200 | [diff] [blame] | 207 | rate_ctr_inc(rate_ctr_group_get_ctr(bts_rate_counters(ms->bts), CTR_PKT_CELL_CHG_CONTINUE)); |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 208 | talloc_free(mac_control_block); |
Pau Espin Pedrol | 86580e1 | 2021-03-29 18:15:30 +0200 | [diff] [blame] | 209 | tbf_set_polling(tbf, *new_poll_fn, data->ts, PDCH_ULC_POLL_CELL_CHG_CONTINUE); |
Pau Espin Pedrol | a161bf4 | 2021-07-30 13:42:06 +0200 | [diff] [blame] | 210 | LOGPTBFDL(tbf, LOGL_DEBUG, "Scheduled 'Packet Cell Change Continue' polling on PACCH (FN=%d, TS=%d)\n", |
| 211 | *new_poll_fn, data->ts); |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 212 | return msg; |
| 213 | |
| 214 | free_ret: |
| 215 | talloc_free(mac_control_block); |
| 216 | msgb_free(msg); |
| 217 | return NULL; |
| 218 | } |
| 219 | |
| 220 | static int fill_rim_ran_info_req(const struct nacc_fsm_ctx *ctx, struct bssgp_ran_information_pdu *pdu) |
| 221 | { |
| 222 | struct gprs_rlcmac_bts *bts = ctx->ms->bts; |
| 223 | |
| 224 | *pdu = (struct bssgp_ran_information_pdu){ |
| 225 | .routing_info_dest = { |
| 226 | .discr = BSSGP_RIM_ROUTING_INFO_GERAN, |
| 227 | .geran = { |
| 228 | .raid = { |
| 229 | .mcc = ctx->cgi_ps.rai.lac.plmn.mcc, |
| 230 | .mnc = ctx->cgi_ps.rai.lac.plmn.mnc, |
| 231 | .mnc_3_digits = ctx->cgi_ps.rai.lac.plmn.mnc_3_digits, |
| 232 | .lac = ctx->cgi_ps.rai.lac.lac, |
| 233 | .rac = ctx->cgi_ps.rai.rac, |
| 234 | }, |
| 235 | .cid = ctx->cgi_ps.cell_identity, |
| 236 | }, |
| 237 | }, |
| 238 | .routing_info_src = { |
| 239 | .discr = BSSGP_RIM_ROUTING_INFO_GERAN, |
| 240 | .geran = { |
| 241 | .raid = { |
| 242 | .mcc = bts->cgi_ps.rai.lac.plmn.mcc, |
| 243 | .mnc = bts->cgi_ps.rai.lac.plmn.mnc, |
| 244 | .mnc_3_digits = bts->cgi_ps.rai.lac.plmn.mnc_3_digits, |
| 245 | .lac = bts->cgi_ps.rai.lac.lac, |
| 246 | .rac = bts->cgi_ps.rai.rac, |
| 247 | }, |
| 248 | .cid = bts->cgi_ps.cell_identity, |
| 249 | }, |
| 250 | }, |
| 251 | .rim_cont_iei = BSSGP_IE_RI_REQ_RIM_CONTAINER, |
| 252 | .decoded_present = true, |
| 253 | .decoded = { |
| 254 | .req_rim_cont = { |
| 255 | .app_id = BSSGP_RAN_INF_APP_ID_NACC, |
| 256 | .seq_num = 1, |
| 257 | .pdu_ind = { |
| 258 | .ack_requested = 0, |
| 259 | .pdu_type_ext = RIM_PDU_TYPE_SING_REP, |
| 260 | }, |
| 261 | .prot_ver = 1, |
| 262 | .son_trans_app_id = NULL, |
| 263 | .son_trans_app_id_len = 0, |
| 264 | .u = { |
| 265 | .app_cont_nacc = { |
| 266 | .reprt_cell = ctx->cgi_ps, |
| 267 | }, |
| 268 | }, |
| 269 | }, |
| 270 | }, |
| 271 | }; |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
Pau Espin Pedrol | 0c10b3c | 2021-02-10 17:12:02 +0100 | [diff] [blame] | 276 | static int fill_neigh_key_from_bts_pkt_cell_chg_not(struct neigh_cache_entry_key *neigh_key, |
| 277 | const struct gprs_rlcmac_bts *bts, |
| 278 | const Packet_Cell_Change_Notification_t *notif) |
| 279 | { |
| 280 | switch (notif->Target_Cell.UnionType) { |
| 281 | case 0: /* GSM */ |
| 282 | neigh_key->local_lac = bts->cgi_ps.rai.lac.lac; |
| 283 | neigh_key->local_ci = bts->cgi_ps.cell_identity; |
| 284 | neigh_key->tgt_arfcn = notif->Target_Cell.u.Target_Cell_GSM_Notif.ARFCN; |
| 285 | neigh_key->tgt_bsic = notif->Target_Cell.u.Target_Cell_GSM_Notif.BSIC; |
| 286 | return 0; |
| 287 | default: |
| 288 | return -ENOTSUP; |
| 289 | } |
| 290 | } |
| 291 | |
Pau Espin Pedrol | 44768f2 | 2021-02-02 13:11:30 +0100 | [diff] [blame] | 292 | #define SI_HDR_LEN 2 |
| 293 | static void bts_fill_si_cache_value(const struct gprs_rlcmac_bts *bts, struct si_cache_value *val) |
| 294 | { |
| 295 | val->type_psi = false; |
| 296 | val->si_len = 0; |
| 297 | if (bts->si1_is_set) { |
| 298 | osmo_static_assert(sizeof(bts->si1) - SI_HDR_LEN == BSSGP_RIM_SI_LEN, _si1_header_size); |
| 299 | memcpy(&val->si_buf[val->si_len], bts->si1 + SI_HDR_LEN, BSSGP_RIM_SI_LEN); |
| 300 | val->si_len += BSSGP_RIM_SI_LEN; |
| 301 | } |
| 302 | if (bts->si3_is_set) { |
| 303 | osmo_static_assert(sizeof(bts->si3) - SI_HDR_LEN == BSSGP_RIM_SI_LEN, _si3_header_size); |
| 304 | memcpy(&val->si_buf[val->si_len], bts->si3 + SI_HDR_LEN, BSSGP_RIM_SI_LEN); |
| 305 | val->si_len += BSSGP_RIM_SI_LEN; |
| 306 | } |
| 307 | if (bts->si13_is_set) { |
| 308 | osmo_static_assert(sizeof(bts->si13) - SI_HDR_LEN == BSSGP_RIM_SI_LEN, _si13_header_size); |
| 309 | memcpy(&val->si_buf[val->si_len], bts->si13 + SI_HDR_LEN, BSSGP_RIM_SI_LEN); |
| 310 | val->si_len += BSSGP_RIM_SI_LEN; |
| 311 | } |
| 312 | } |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 313 | |
Pau Espin Pedrol | 8e69fc0 | 2021-02-10 17:44:27 +0100 | [diff] [blame] | 314 | /* Called on event NACC_EV_RX_CELL_CHG_NOTIFICATION on states after |
| 315 | * WAIT_RESOLVE_RAC_CI. Ignore duplicate messages, transition back if target |
| 316 | * cell changed. |
| 317 | */ |
| 318 | static void handle_retrans_pkt_cell_chg_notif(struct nacc_fsm_ctx *ctx, const Packet_Cell_Change_Notification_t *notif) |
| 319 | { |
| 320 | struct gprs_rlcmac_bts *bts = ctx->ms->bts; |
| 321 | struct neigh_cache_entry_key neigh_key; |
| 322 | |
| 323 | if (fill_neigh_key_from_bts_pkt_cell_chg_not(&neigh_key, bts, notif) < 0) { |
| 324 | LOGPFSML(ctx->fi, LOGL_NOTICE, "TargetCell type=0x%x not supported\n", |
| 325 | notif->Target_Cell.UnionType); |
Pau Espin Pedrol | a78f0d5 | 2021-02-11 12:53:13 +0100 | [diff] [blame] | 326 | if (ctx->fi->state != NACC_ST_TX_CELL_CHG_CONTINUE) |
| 327 | nacc_fsm_state_chg(ctx->fi, NACC_ST_TX_CELL_CHG_CONTINUE); |
Pau Espin Pedrol | 8e69fc0 | 2021-02-10 17:44:27 +0100 | [diff] [blame] | 328 | return; |
| 329 | } |
| 330 | /* If tgt cell changed, restart resolving it */ |
| 331 | if (!neigh_cache_entry_key_eq(&ctx->neigh_key, &neigh_key)) { |
| 332 | ctx->neigh_key = neigh_key; |
| 333 | nacc_fsm_state_chg(ctx->fi, NACC_ST_WAIT_RESOLVE_RAC_CI); |
| 334 | } |
| 335 | /* else: ignore it, it's a dup, carry on what we were doing */ |
| 336 | } |
| 337 | |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 338 | //////////////// |
| 339 | // FSM states // |
| 340 | //////////////// |
| 341 | |
| 342 | static void st_initial(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 343 | { |
| 344 | struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv; |
| 345 | struct gprs_rlcmac_bts *bts = ctx->ms->bts; |
| 346 | Packet_Cell_Change_Notification_t *notif; |
| 347 | |
| 348 | switch (event) { |
| 349 | case NACC_EV_RX_CELL_CHG_NOTIFICATION: |
| 350 | notif = (Packet_Cell_Change_Notification_t *)data; |
Pau Espin Pedrol | 0c10b3c | 2021-02-10 17:12:02 +0100 | [diff] [blame] | 351 | if (fill_neigh_key_from_bts_pkt_cell_chg_not(&ctx->neigh_key, bts, notif) < 0) { |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 352 | LOGPFSML(fi, LOGL_NOTICE, "TargetCell type=0x%x not supported\n", |
| 353 | notif->Target_Cell.UnionType); |
| 354 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL); |
Pau Espin Pedrol | 0c10b3c | 2021-02-10 17:12:02 +0100 | [diff] [blame] | 355 | } else { |
| 356 | nacc_fsm_state_chg(fi, NACC_ST_WAIT_RESOLVE_RAC_CI); |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 357 | } |
| 358 | break; |
| 359 | default: |
| 360 | OSMO_ASSERT(0); |
| 361 | } |
| 362 | } |
| 363 | |
Pau Espin Pedrol | 5557c0a | 2021-09-07 14:09:50 +0200 | [diff] [blame] | 364 | static int send_neigh_addr_req_ctrl_iface(struct nacc_fsm_ctx *ctx) |
| 365 | { |
| 366 | struct gprs_rlcmac_bts *bts = ctx->ms->bts; |
| 367 | struct gprs_pcu *pcu = bts->pcu; |
| 368 | struct ctrl_cmd *cmd = NULL; |
| 369 | int rc; |
| 370 | |
| 371 | /* We may have changed to this state previously (eg: we are handling |
| 372 | * another Pkt cell Change Notify with different target). Avoid |
| 373 | * re-creating the socket in that case. */ |
| 374 | if (ctx->neigh_ctrl_conn->write_queue.bfd.fd == -1) { |
| 375 | rc = osmo_sock_init2_ofd(&ctx->neigh_ctrl_conn->write_queue.bfd, |
| 376 | AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, |
| 377 | NULL, 0, pcu->vty.neigh_ctrl_addr, pcu->vty.neigh_ctrl_port, |
| 378 | OSMO_SOCK_F_CONNECT); |
| 379 | if (rc < 0) { |
| 380 | LOGPFSML(ctx->fi, LOGL_ERROR, |
| 381 | "Failed to establish CTRL (neighbor resolution) connection to BSC r=%s:%u\n\n", |
| 382 | pcu->vty.neigh_ctrl_addr, pcu->vty.neigh_ctrl_port); |
| 383 | goto err_term; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | cmd = ctrl_cmd_create(ctx, CTRL_TYPE_GET); |
| 388 | if (!cmd) { |
| 389 | LOGPFSML(ctx->fi, LOGL_ERROR, "CTRL msg creation failed\n"); |
| 390 | goto err_term; |
| 391 | } |
| 392 | |
| 393 | cmd->id = talloc_asprintf(cmd, "%u", arfcn_bsic_2_ctrl_id(ctx->neigh_key.tgt_arfcn, |
| 394 | ctx->neigh_key.tgt_bsic)); |
| 395 | cmd->variable = talloc_asprintf(cmd, "neighbor_resolve_cgi_ps_from_lac_ci.%d.%d.%d.%d", |
| 396 | ctx->neigh_key.local_lac, ctx->neigh_key.local_ci, |
| 397 | ctx->neigh_key.tgt_arfcn, ctx->neigh_key.tgt_bsic); |
| 398 | rc = ctrl_cmd_send(&ctx->neigh_ctrl_conn->write_queue, cmd); |
| 399 | if (rc) { |
| 400 | LOGPFSML(ctx->fi, LOGL_ERROR, "CTRL msg sent failed: %d\n", rc); |
| 401 | goto err_term; |
| 402 | } |
| 403 | |
| 404 | talloc_free(cmd); |
| 405 | return 0; |
| 406 | |
| 407 | err_term: |
| 408 | talloc_free(cmd); |
| 409 | return -1; |
| 410 | } |
| 411 | |
| 412 | static int send_neigh_addr_req(struct nacc_fsm_ctx *ctx) |
| 413 | { |
| 414 | struct gprs_rlcmac_bts *bts = ctx->ms->bts; |
| 415 | |
| 416 | /* If PCU was configured to use the old CTRL interface, use it: */ |
| 417 | if (ctx->neigh_ctrl_conn) |
| 418 | return send_neigh_addr_req_ctrl_iface(ctx); |
| 419 | |
| 420 | /* Otherwise, by default the new PCUIF over IPA Abis multiplex proto should be used: */ |
| 421 | return pcu_tx_neigh_addr_res_req(bts, &ctx->neigh_key); |
| 422 | } |
| 423 | |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 424 | static void st_wait_resolve_rac_ci_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 425 | { |
| 426 | struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv; |
| 427 | struct gprs_rlcmac_bts *bts = ctx->ms->bts; |
| 428 | struct gprs_pcu *pcu = bts->pcu; |
| 429 | const struct osmo_cell_global_id_ps *cgi_ps; |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 430 | |
| 431 | /* First try to find the value in the cache */ |
| 432 | cgi_ps = neigh_cache_lookup_value(pcu->neigh_cache, &ctx->neigh_key); |
| 433 | if (cgi_ps) { |
| 434 | ctx->cgi_ps = *cgi_ps; |
| 435 | nacc_fsm_state_chg(fi, NACC_ST_WAIT_REQUEST_SI); |
| 436 | return; |
| 437 | } |
| 438 | |
| 439 | /* CGI-PS not in cache, resolve it using BSC Neighbor Resolution CTRL interface */ |
| 440 | |
| 441 | LOGPFSML(fi, LOGL_DEBUG, "No CGI-PS found in cache, resolving " NEIGH_CACHE_ENTRY_KEY_FMT "...\n", |
| 442 | NEIGH_CACHE_ENTRY_KEY_ARGS(&ctx->neigh_key)); |
| 443 | |
Pau Espin Pedrol | 5557c0a | 2021-09-07 14:09:50 +0200 | [diff] [blame] | 444 | if (send_neigh_addr_req(ctx) < 0) |
| 445 | nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE); |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 446 | } |
| 447 | |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 448 | static void st_wait_resolve_rac_ci(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 449 | { |
Pau Espin Pedrol | 069a637 | 2021-02-10 17:33:13 +0100 | [diff] [blame] | 450 | struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv; |
Pau Espin Pedrol | 8e69fc0 | 2021-02-10 17:44:27 +0100 | [diff] [blame] | 451 | const Packet_Cell_Change_Notification_t *notif; |
Pau Espin Pedrol | 069a637 | 2021-02-10 17:33:13 +0100 | [diff] [blame] | 452 | |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 453 | switch (event) { |
| 454 | case NACC_EV_RX_CELL_CHG_NOTIFICATION: |
Pau Espin Pedrol | 8e69fc0 | 2021-02-10 17:44:27 +0100 | [diff] [blame] | 455 | notif = (const Packet_Cell_Change_Notification_t *)data; |
| 456 | handle_retrans_pkt_cell_chg_notif(ctx, notif); |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 457 | break; |
| 458 | case NACC_EV_RX_RAC_CI: |
Pau Espin Pedrol | 5557c0a | 2021-09-07 14:09:50 +0200 | [diff] [blame] | 459 | /* data is NULL upon failure */ |
| 460 | if (data) { |
| 461 | ctx->cgi_ps = *(struct osmo_cell_global_id_ps *)data; |
| 462 | nacc_fsm_state_chg(fi, NACC_ST_WAIT_REQUEST_SI); |
| 463 | } else { |
| 464 | nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE); |
| 465 | } |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 466 | break; |
| 467 | default: |
| 468 | OSMO_ASSERT(0); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | /* At this point, we expect correct tgt cell info to be already in ctx->cgi_ps */ |
| 473 | static void st_wait_request_si_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 474 | { |
| 475 | struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv; |
| 476 | struct gprs_rlcmac_bts *bts = ctx->ms->bts; |
| 477 | struct gprs_pcu *pcu = bts->pcu; |
| 478 | struct bssgp_ran_information_pdu pdu; |
| 479 | const struct si_cache_value *si; |
Pau Espin Pedrol | 44768f2 | 2021-02-02 13:11:30 +0100 | [diff] [blame] | 480 | struct gprs_rlcmac_bts *bts_i; |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 481 | int rc; |
| 482 | |
Pau Espin Pedrol | 44768f2 | 2021-02-02 13:11:30 +0100 | [diff] [blame] | 483 | /* First check if the CGI-PS addresses a cell managed by this PCU. If |
| 484 | * that's the case, we already have the info and there's no need to go |
| 485 | * the RIM way since we'd end up to this same PCU on the other end anyway. |
| 486 | */ |
| 487 | llist_for_each_entry(bts_i, &the_pcu->bts_list, list) { |
| 488 | if (bts_i == bts) /* Makes no sense targeting the same cell */ |
| 489 | continue; |
| 490 | if (osmo_cgi_ps_cmp(&ctx->cgi_ps, &bts_i->cgi_ps) != 0) |
| 491 | continue; |
| 492 | |
| 493 | LOGPFSML(fi, LOGL_DEBUG, "neighbor CGI-PS %s addresses local BTS %d\n", |
| 494 | osmo_cgi_ps_name(&ctx->cgi_ps), bts_i->nr); |
| 495 | bts_fill_si_cache_value(bts, &ctx->si_info); |
| 496 | /* Tell the PCU scheduler we are ready to go, from here one we |
| 497 | * are polled/driven by the scheduler */ |
| 498 | nacc_fsm_state_chg(fi, NACC_ST_TX_NEIGHBOUR_DATA); |
| 499 | return; |
| 500 | } |
| 501 | |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 502 | /* First check if we have SI info for the target cell in cache */ |
| 503 | si = si_cache_lookup_value(pcu->si_cache, &ctx->cgi_ps); |
| 504 | if (si) { |
| 505 | /* Copy info since cache can be deleted at any point */ |
| 506 | memcpy(&ctx->si_info, si, sizeof(ctx->si_info)); |
| 507 | /* Tell the PCU scheduler we are ready to go, from here one we |
| 508 | * are polled/driven by the scheduler */ |
| 509 | nacc_fsm_state_chg(fi, NACC_ST_TX_NEIGHBOUR_DATA); |
| 510 | return; |
| 511 | } |
| 512 | |
| 513 | /* SI info not in cache, resolve it using RIM procedure against SGSN */ |
| 514 | if (fill_rim_ran_info_req(ctx, &pdu) < 0) { |
Pau Espin Pedrol | a06ac18 | 2021-01-26 19:13:43 +0100 | [diff] [blame] | 515 | nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE); |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 516 | return; |
| 517 | } |
| 518 | |
Pau Espin Pedrol | 9313da5 | 2021-02-15 12:12:44 +0100 | [diff] [blame] | 519 | LOGPFSML(fi, LOGL_INFO, "Tx RIM RAN-INFO to request SI of %s\n", |
| 520 | osmo_cgi_ps_name(&ctx->cgi_ps)); |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 521 | rc = bssgp_tx_rim(&pdu, gprs_ns2_nse_nsei(ctx->ms->bts->nse)); |
| 522 | if (rc < 0) { |
Pau Espin Pedrol | 9313da5 | 2021-02-15 12:12:44 +0100 | [diff] [blame] | 523 | LOGPFSML(fi, LOGL_ERROR, "Failed transmitting RIM RAN-INFO %s PDU: %d\n", |
| 524 | osmo_cgi_ps_name(&ctx->cgi_ps), rc); |
Pau Espin Pedrol | a06ac18 | 2021-01-26 19:13:43 +0100 | [diff] [blame] | 525 | nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE); |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 526 | return; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | |
| 531 | static void st_wait_request_si(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 532 | { |
| 533 | struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv; |
Pau Espin Pedrol | 8e69fc0 | 2021-02-10 17:44:27 +0100 | [diff] [blame] | 534 | const Packet_Cell_Change_Notification_t *notif; |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 535 | struct si_cache_entry *entry; |
| 536 | |
| 537 | switch (event) { |
Pau Espin Pedrol | 8e69fc0 | 2021-02-10 17:44:27 +0100 | [diff] [blame] | 538 | case NACC_EV_RX_CELL_CHG_NOTIFICATION: |
| 539 | notif = (const Packet_Cell_Change_Notification_t *)data; |
| 540 | handle_retrans_pkt_cell_chg_notif(ctx, notif); |
| 541 | break; |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 542 | case NACC_EV_RX_SI: |
| 543 | entry = (struct si_cache_entry *)data; |
| 544 | /* Copy info since cache can be deleted at any point */ |
| 545 | memcpy(&ctx->si_info, &entry->value, sizeof(ctx->si_info)); |
| 546 | /* Tell the PCU scheduler we are ready to go, from here one we |
| 547 | * are polled/driven by the scheduler */ |
| 548 | nacc_fsm_state_chg(fi, NACC_ST_TX_NEIGHBOUR_DATA); |
| 549 | break; |
| 550 | default: |
| 551 | OSMO_ASSERT(0); |
| 552 | } |
| 553 | } |
| 554 | |
Pau Espin Pedrol | a78f0d5 | 2021-02-11 12:53:13 +0100 | [diff] [blame] | 555 | /* At this point, we already received all required SI information to send stored |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 556 | * in struct nacc_fsm_ctx. We now wait for scheduler to ask us to construct |
| 557 | * RLCMAC DL CTRL messages to move FSM states forward |
| 558 | */ |
Pau Espin Pedrol | a78f0d5 | 2021-02-11 12:53:13 +0100 | [diff] [blame] | 559 | static void st_tx_neighbour_data_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 560 | { |
| 561 | struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv; |
| 562 | ctx->si_info_bytes_sent = 0; |
| 563 | ctx->container_idx = 0; |
| 564 | } |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 565 | |
| 566 | static void st_tx_neighbour_data(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 567 | { |
| 568 | struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv; |
Pau Espin Pedrol | a78f0d5 | 2021-02-11 12:53:13 +0100 | [diff] [blame] | 569 | const Packet_Cell_Change_Notification_t *notif; |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 570 | struct nacc_ev_create_rlcmac_msg_ctx *data_ctx; |
| 571 | bool all_si_info_sent; |
| 572 | |
| 573 | switch (event) { |
Pau Espin Pedrol | a78f0d5 | 2021-02-11 12:53:13 +0100 | [diff] [blame] | 574 | case NACC_EV_RX_CELL_CHG_NOTIFICATION: |
| 575 | notif = (const Packet_Cell_Change_Notification_t *)data; |
| 576 | handle_retrans_pkt_cell_chg_notif(ctx, notif); |
| 577 | break; |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 578 | case NACC_EV_CREATE_RLCMAC_MSG: |
| 579 | data_ctx = (struct nacc_ev_create_rlcmac_msg_ctx *)data; |
| 580 | data_ctx->msg = create_packet_neighbour_cell_data(ctx, data_ctx->tbf, &all_si_info_sent); |
| 581 | if (!data_ctx->msg) { |
| 582 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL); |
| 583 | return; |
| 584 | } |
| 585 | if (all_si_info_sent) /* DONE */ |
| 586 | nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE); |
| 587 | break; |
| 588 | default: |
| 589 | OSMO_ASSERT(0); |
| 590 | } |
| 591 | } |
| 592 | |
Pau Espin Pedrol | 1aef113 | 2021-02-01 19:33:59 +0100 | [diff] [blame] | 593 | /* st_cell_chg_continue_on_enter: |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 594 | * At this point, we already sent all Pkt Cell Neighbour Change rlcmac |
| 595 | * blocks, and we only need to wait to be scheduled again to send PKT |
| 596 | * CELL CHANGE NOTIFICATION and then we are done |
| 597 | */ |
| 598 | |
Pau Espin Pedrol | 1aef113 | 2021-02-01 19:33:59 +0100 | [diff] [blame] | 599 | static void st_cell_chg_continue(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 600 | { |
| 601 | struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv; |
Pau Espin Pedrol | a78f0d5 | 2021-02-11 12:53:13 +0100 | [diff] [blame] | 602 | const Packet_Cell_Change_Notification_t *notif; |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 603 | struct nacc_ev_create_rlcmac_msg_ctx *data_ctx; |
| 604 | |
| 605 | switch (event) { |
Pau Espin Pedrol | a78f0d5 | 2021-02-11 12:53:13 +0100 | [diff] [blame] | 606 | case NACC_EV_RX_CELL_CHG_NOTIFICATION: |
| 607 | notif = (const Packet_Cell_Change_Notification_t *)data; |
| 608 | handle_retrans_pkt_cell_chg_notif(ctx, notif); |
| 609 | break; |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 610 | case NACC_EV_CREATE_RLCMAC_MSG: |
| 611 | data_ctx = (struct nacc_ev_create_rlcmac_msg_ctx *)data; |
Pau Espin Pedrol | 952cb3d | 2021-02-01 14:52:48 +0100 | [diff] [blame] | 612 | data_ctx->msg = create_packet_cell_chg_continue(ctx, data_ctx, &ctx->continue_poll_fn); |
| 613 | if (data_ctx->msg) { |
| 614 | ctx->continue_poll_ts = data_ctx->ts; |
| 615 | nacc_fsm_state_chg(fi, NACC_ST_WAIT_CELL_CHG_CONTINUE_ACK); |
| 616 | } |
| 617 | break; |
| 618 | default: |
| 619 | OSMO_ASSERT(0); |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | static void st_wait_cell_chg_continue_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 624 | { |
Pau Espin Pedrol | a78f0d5 | 2021-02-11 12:53:13 +0100 | [diff] [blame] | 625 | struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv; |
| 626 | const Packet_Cell_Change_Notification_t *notif; |
| 627 | |
Pau Espin Pedrol | 952cb3d | 2021-02-01 14:52:48 +0100 | [diff] [blame] | 628 | switch (event) { |
Pau Espin Pedrol | a78f0d5 | 2021-02-11 12:53:13 +0100 | [diff] [blame] | 629 | case NACC_EV_RX_CELL_CHG_NOTIFICATION: |
| 630 | notif = (const Packet_Cell_Change_Notification_t *)data; |
| 631 | handle_retrans_pkt_cell_chg_notif(ctx, notif); |
| 632 | break; |
Pau Espin Pedrol | 952cb3d | 2021-02-01 14:52:48 +0100 | [diff] [blame] | 633 | case NACC_EV_TIMEOUT_CELL_CHG_CONTINUE: |
| 634 | nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE); |
| 635 | break; |
| 636 | case NACC_EV_RX_CELL_CHG_CONTINUE_ACK: |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 637 | nacc_fsm_state_chg(fi, NACC_ST_DONE); |
| 638 | break; |
| 639 | default: |
| 640 | OSMO_ASSERT(0); |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | |
| 645 | static void st_done_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 646 | { |
| 647 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL); |
| 648 | } |
| 649 | |
| 650 | static void nacc_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause) |
| 651 | { |
| 652 | struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv; |
| 653 | /* after cleanup() finishes, FSM termination calls osmo_fsm_inst_free, |
| 654 | so we need to avoid double-freeing it during ctx talloc free |
| 655 | destructor */ |
| 656 | talloc_reparent(ctx, ctx->ms, ctx->fi); |
| 657 | ctx->fi = NULL; |
| 658 | |
| 659 | /* remove references from owning MS and free entire ctx */ |
| 660 | ctx->ms->nacc = NULL; |
| 661 | talloc_free(ctx); |
| 662 | } |
| 663 | |
Pau Espin Pedrol | 41a22a7 | 2021-01-26 19:00:37 +0100 | [diff] [blame] | 664 | static int nacc_fsm_timer_cb(struct osmo_fsm_inst *fi) |
| 665 | { |
| 666 | switch (fi->T) { |
| 667 | case PCU_TDEF_NEIGH_RESOLVE_TO: |
| 668 | case PCU_TDEF_SI_RESOLVE_TO: |
| 669 | nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE); |
| 670 | break; |
| 671 | } |
| 672 | return 0; |
| 673 | } |
| 674 | |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 675 | static struct osmo_fsm_state nacc_fsm_states[] = { |
| 676 | [NACC_ST_INITIAL] = { |
| 677 | .in_event_mask = |
| 678 | X(NACC_EV_RX_CELL_CHG_NOTIFICATION), |
| 679 | .out_state_mask = |
| 680 | X(NACC_ST_WAIT_RESOLVE_RAC_CI), |
| 681 | .name = "INITIAL", |
| 682 | .action = st_initial, |
| 683 | }, |
| 684 | [NACC_ST_WAIT_RESOLVE_RAC_CI] = { |
| 685 | .in_event_mask = |
Pau Espin Pedrol | 069a637 | 2021-02-10 17:33:13 +0100 | [diff] [blame] | 686 | X(NACC_EV_RX_CELL_CHG_NOTIFICATION) | |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 687 | X(NACC_EV_RX_RAC_CI), |
| 688 | .out_state_mask = |
Pau Espin Pedrol | 069a637 | 2021-02-10 17:33:13 +0100 | [diff] [blame] | 689 | X(NACC_ST_WAIT_RESOLVE_RAC_CI) | |
Pau Espin Pedrol | 41a22a7 | 2021-01-26 19:00:37 +0100 | [diff] [blame] | 690 | X(NACC_ST_WAIT_REQUEST_SI) | |
| 691 | X(NACC_ST_TX_CELL_CHG_CONTINUE), |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 692 | .name = "WAIT_RESOLVE_RAC_CI", |
| 693 | .onenter = st_wait_resolve_rac_ci_on_enter, |
| 694 | .action = st_wait_resolve_rac_ci, |
| 695 | }, |
| 696 | [NACC_ST_WAIT_REQUEST_SI] = { |
| 697 | .in_event_mask = |
| 698 | X(NACC_EV_RX_CELL_CHG_NOTIFICATION) | |
| 699 | X(NACC_EV_RX_SI), |
| 700 | .out_state_mask = |
Pau Espin Pedrol | 8e69fc0 | 2021-02-10 17:44:27 +0100 | [diff] [blame] | 701 | X(NACC_ST_WAIT_RESOLVE_RAC_CI) | |
Pau Espin Pedrol | 41a22a7 | 2021-01-26 19:00:37 +0100 | [diff] [blame] | 702 | X(NACC_ST_TX_NEIGHBOUR_DATA) | |
| 703 | X(NACC_ST_TX_CELL_CHG_CONTINUE), |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 704 | .name = "WAIT_REQUEST_SI", |
| 705 | .onenter = st_wait_request_si_on_enter, |
| 706 | .action = st_wait_request_si, |
| 707 | }, |
| 708 | [NACC_ST_TX_NEIGHBOUR_DATA] = { |
| 709 | .in_event_mask = |
| 710 | X(NACC_EV_RX_CELL_CHG_NOTIFICATION) | |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 711 | X(NACC_EV_CREATE_RLCMAC_MSG), |
| 712 | .out_state_mask = |
Pau Espin Pedrol | a78f0d5 | 2021-02-11 12:53:13 +0100 | [diff] [blame] | 713 | X(NACC_ST_WAIT_RESOLVE_RAC_CI) | |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 714 | X(NACC_ST_TX_CELL_CHG_CONTINUE), |
| 715 | .name = "TX_NEIGHBOUR_DATA", |
Pau Espin Pedrol | a78f0d5 | 2021-02-11 12:53:13 +0100 | [diff] [blame] | 716 | .onenter = st_tx_neighbour_data_on_enter, |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 717 | .action = st_tx_neighbour_data, |
| 718 | }, |
| 719 | [NACC_ST_TX_CELL_CHG_CONTINUE] = { |
| 720 | .in_event_mask = |
| 721 | X(NACC_EV_RX_CELL_CHG_NOTIFICATION) | |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 722 | X(NACC_EV_CREATE_RLCMAC_MSG), |
| 723 | .out_state_mask = |
Pau Espin Pedrol | a78f0d5 | 2021-02-11 12:53:13 +0100 | [diff] [blame] | 724 | X(NACC_ST_WAIT_RESOLVE_RAC_CI) | |
Pau Espin Pedrol | 952cb3d | 2021-02-01 14:52:48 +0100 | [diff] [blame] | 725 | X(NACC_ST_WAIT_CELL_CHG_CONTINUE_ACK), |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 726 | .name = "TX_CELL_CHG_CONTINUE", |
Pau Espin Pedrol | 1aef113 | 2021-02-01 19:33:59 +0100 | [diff] [blame] | 727 | .action = st_cell_chg_continue, |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 728 | }, |
Pau Espin Pedrol | 952cb3d | 2021-02-01 14:52:48 +0100 | [diff] [blame] | 729 | [NACC_ST_WAIT_CELL_CHG_CONTINUE_ACK] = { |
| 730 | .in_event_mask = |
Pau Espin Pedrol | a78f0d5 | 2021-02-11 12:53:13 +0100 | [diff] [blame] | 731 | X(NACC_EV_RX_CELL_CHG_NOTIFICATION) | |
Pau Espin Pedrol | 952cb3d | 2021-02-01 14:52:48 +0100 | [diff] [blame] | 732 | X(NACC_EV_RX_CELL_CHG_CONTINUE_ACK) | |
| 733 | X(NACC_EV_TIMEOUT_CELL_CHG_CONTINUE), |
| 734 | .out_state_mask = |
Pau Espin Pedrol | a78f0d5 | 2021-02-11 12:53:13 +0100 | [diff] [blame] | 735 | X(NACC_ST_WAIT_RESOLVE_RAC_CI) | |
Pau Espin Pedrol | 952cb3d | 2021-02-01 14:52:48 +0100 | [diff] [blame] | 736 | X(NACC_ST_TX_CELL_CHG_CONTINUE) | |
| 737 | X(NACC_ST_DONE), |
| 738 | .name = "WAIT_CELL_CHG_CONTINUE_ACK", |
| 739 | .action = st_wait_cell_chg_continue_ack, |
| 740 | }, |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 741 | [NACC_ST_DONE] = { |
| 742 | .in_event_mask = 0, |
| 743 | .out_state_mask = 0, |
| 744 | .name = "DONE", |
| 745 | .onenter = st_done_on_enter, |
| 746 | }, |
| 747 | }; |
| 748 | |
| 749 | static struct osmo_fsm nacc_fsm = { |
| 750 | .name = "NACC", |
| 751 | .states = nacc_fsm_states, |
| 752 | .num_states = ARRAY_SIZE(nacc_fsm_states), |
Pau Espin Pedrol | 41a22a7 | 2021-01-26 19:00:37 +0100 | [diff] [blame] | 753 | .timer_cb = nacc_fsm_timer_cb, |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 754 | .cleanup = nacc_fsm_cleanup, |
| 755 | .log_subsys = DNACC, |
| 756 | .event_names = nacc_fsm_event_names, |
| 757 | }; |
| 758 | |
| 759 | static __attribute__((constructor)) void nacc_fsm_init(void) |
| 760 | { |
| 761 | OSMO_ASSERT(osmo_fsm_register(&nacc_fsm) == 0); |
| 762 | } |
| 763 | |
| 764 | void nacc_fsm_ctrl_reply_cb(struct ctrl_handle *ctrl, struct ctrl_cmd *cmd, void *data) |
| 765 | { |
| 766 | struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)data; |
| 767 | char *tmp = NULL, *tok, *saveptr; |
Pau Espin Pedrol | 069a637 | 2021-02-10 17:33:13 +0100 | [diff] [blame] | 768 | unsigned int exp_id; |
Pau Espin Pedrol | 5557c0a | 2021-09-07 14:09:50 +0200 | [diff] [blame] | 769 | struct osmo_cell_global_id_ps cgi_ps; |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 770 | |
Pau Espin Pedrol | 069a637 | 2021-02-10 17:33:13 +0100 | [diff] [blame] | 771 | LOGPFSML(ctx->fi, LOGL_NOTICE, "Received CTRL message: type=%d %s %s: %s\n", |
| 772 | cmd->type, cmd->variable, cmd->id, osmo_escape_str(cmd->reply, -1)); |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 773 | |
| 774 | if (cmd->type != CTRL_TYPE_GET_REPLY || !cmd->reply) { |
Pau Espin Pedrol | 5557c0a | 2021-09-07 14:09:50 +0200 | [diff] [blame] | 775 | osmo_fsm_inst_dispatch(ctx->fi, NACC_EV_RX_RAC_CI, NULL); |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 776 | return; |
| 777 | } |
| 778 | |
Pau Espin Pedrol | 069a637 | 2021-02-10 17:33:13 +0100 | [diff] [blame] | 779 | /* Validate it's the seqnum from our last GET cmd, and not from older |
| 780 | * one we may have requested in case MS decided to resend Pkt Cell |
| 781 | * Change Notify with a different tgt cell: |
| 782 | */ |
| 783 | exp_id = arfcn_bsic_2_ctrl_id(ctx->neigh_key.tgt_arfcn, ctx->neigh_key.tgt_bsic); |
| 784 | if ((unsigned int)atoi(cmd->id) != exp_id) { |
| 785 | LOGPFSML(ctx->fi, LOGL_INFO, |
| 786 | "Received CTRL message with id=%s doesn't match our expected last id=%d, ignoring\n", |
| 787 | cmd->id, exp_id); |
| 788 | return; |
| 789 | } |
| 790 | |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 791 | /* TODO: Potentially validate cmd->variable contains same params as we |
| 792 | sent, and that cmd->id matches the original set. We may want to keep |
| 793 | the original cmd around by setting cmd->defer=1 when sending it. */ |
| 794 | |
| 795 | tmp = talloc_strdup(cmd, cmd->reply); |
| 796 | if (!tmp) |
| 797 | goto free_ret; |
| 798 | |
| 799 | if (!(tok = strtok_r(tmp, "-", &saveptr))) |
| 800 | goto free_ret; |
Pau Espin Pedrol | 5557c0a | 2021-09-07 14:09:50 +0200 | [diff] [blame] | 801 | cgi_ps.rai.lac.plmn.mcc = atoi(tok); |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 802 | |
| 803 | if (!(tok = strtok_r(NULL, "-", &saveptr))) |
| 804 | goto free_ret; |
Pau Espin Pedrol | 5557c0a | 2021-09-07 14:09:50 +0200 | [diff] [blame] | 805 | cgi_ps.rai.lac.plmn.mnc = atoi(tok); |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 806 | |
| 807 | if (!(tok = strtok_r(NULL, "-", &saveptr))) |
| 808 | goto free_ret; |
Pau Espin Pedrol | 5557c0a | 2021-09-07 14:09:50 +0200 | [diff] [blame] | 809 | cgi_ps.rai.lac.lac = atoi(tok); |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 810 | |
| 811 | if (!(tok = strtok_r(NULL, "-", &saveptr))) |
| 812 | goto free_ret; |
Pau Espin Pedrol | 5557c0a | 2021-09-07 14:09:50 +0200 | [diff] [blame] | 813 | cgi_ps.rai.rac = atoi(tok); |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 814 | |
| 815 | if (!(tok = strtok_r(NULL, "\0", &saveptr))) |
| 816 | goto free_ret; |
Pau Espin Pedrol | 5557c0a | 2021-09-07 14:09:50 +0200 | [diff] [blame] | 817 | cgi_ps.cell_identity = atoi(tok); |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 818 | |
| 819 | /* Cache the cgi_ps so we can avoid requesting again same resolution for a while */ |
Pau Espin Pedrol | 5557c0a | 2021-09-07 14:09:50 +0200 | [diff] [blame] | 820 | neigh_cache_add(ctx->ms->bts->pcu->neigh_cache, &ctx->neigh_key, &cgi_ps); |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 821 | |
Pau Espin Pedrol | 5557c0a | 2021-09-07 14:09:50 +0200 | [diff] [blame] | 822 | osmo_fsm_inst_dispatch(ctx->fi, NACC_EV_RX_RAC_CI, &cgi_ps); |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 823 | return; |
| 824 | |
| 825 | free_ret: |
| 826 | talloc_free(tmp); |
Pau Espin Pedrol | 5557c0a | 2021-09-07 14:09:50 +0200 | [diff] [blame] | 827 | osmo_fsm_inst_dispatch(ctx->fi, NACC_EV_RX_RAC_CI, NULL); |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 828 | return; |
| 829 | } |
| 830 | |
| 831 | static int nacc_fsm_ctx_talloc_destructor(struct nacc_fsm_ctx *ctx) |
| 832 | { |
| 833 | if (ctx->fi) { |
| 834 | osmo_fsm_inst_free(ctx->fi); |
| 835 | ctx->fi = NULL; |
| 836 | } |
| 837 | |
| 838 | if (ctx->neigh_ctrl_conn) { |
| 839 | if (ctx->neigh_ctrl_conn->write_queue.bfd.fd != -1) { |
| 840 | osmo_wqueue_clear(&ctx->neigh_ctrl_conn->write_queue); |
| 841 | osmo_fd_unregister(&ctx->neigh_ctrl_conn->write_queue.bfd); |
| 842 | close(ctx->neigh_ctrl_conn->write_queue.bfd.fd); |
| 843 | ctx->neigh_ctrl_conn->write_queue.bfd.fd = -1; |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | return 0; |
| 848 | } |
| 849 | |
| 850 | struct nacc_fsm_ctx *nacc_fsm_alloc(struct GprsMs* ms) |
| 851 | { |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 852 | struct nacc_fsm_ctx *ctx = talloc_zero(ms, struct nacc_fsm_ctx); |
| 853 | char buf[64]; |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 854 | |
| 855 | talloc_set_destructor(ctx, nacc_fsm_ctx_talloc_destructor); |
| 856 | |
| 857 | ctx->ms = ms; |
| 858 | |
| 859 | snprintf(buf, sizeof(buf), "TLLI-0x%08x", ms_tlli(ms)); |
| 860 | ctx->fi = osmo_fsm_inst_alloc(&nacc_fsm, ctx, ctx, LOGL_INFO, buf); |
| 861 | if (!ctx->fi) |
| 862 | goto free_ret; |
| 863 | |
Pau Espin Pedrol | 5557c0a | 2021-09-07 14:09:50 +0200 | [diff] [blame] | 864 | /* If CTRL ip present, use the old CTRL interface for neighbor resolution */ |
| 865 | if (ms->bts->pcu->vty.neigh_ctrl_addr) { |
| 866 | ctx->neigh_ctrl = ctrl_handle_alloc(ctx, ctx, NULL); |
| 867 | ctx->neigh_ctrl->reply_cb = nacc_fsm_ctrl_reply_cb; |
| 868 | ctx->neigh_ctrl_conn = osmo_ctrl_conn_alloc(ctx, ctx->neigh_ctrl); |
| 869 | if (!ctx->neigh_ctrl_conn) |
| 870 | goto free_ret; |
| 871 | /* Older versions of osmo_ctrl_conn_alloc didn't properly initialize fd to -1, |
| 872 | * so make sure to do it here otherwise fd may be valid fd 0 and cause trouble */ |
| 873 | ctx->neigh_ctrl_conn->write_queue.bfd.fd = -1; |
| 874 | llist_add(&ctx->neigh_ctrl_conn->list_entry, &ctx->neigh_ctrl->ccon_list); |
| 875 | } |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 876 | |
Pau Espin Pedrol | c0a250d | 2021-01-21 18:46:13 +0100 | [diff] [blame] | 877 | return ctx; |
| 878 | free_ret: |
| 879 | talloc_free(ctx); |
| 880 | return NULL; |
| 881 | } |
Pau Espin Pedrol | 5557c0a | 2021-09-07 14:09:50 +0200 | [diff] [blame] | 882 | |
| 883 | bool nacc_fsm_is_waiting_addr_resolution(const struct nacc_fsm_ctx *ctx, |
| 884 | const struct neigh_cache_entry_key *neigh_key) |
| 885 | { |
| 886 | if (ctx->fi->state != NACC_ST_WAIT_RESOLVE_RAC_CI) |
| 887 | return false; |
| 888 | return neigh_cache_entry_key_eq(&ctx->neigh_key, neigh_key); |
| 889 | } |
Pau Espin Pedrol | 6c81add | 2021-09-07 14:43:46 +0200 | [diff] [blame] | 890 | |
| 891 | bool nacc_fsm_is_waiting_si_resolution(const struct nacc_fsm_ctx *ctx, |
| 892 | const struct osmo_cell_global_id_ps *cgi_ps) |
| 893 | { |
| 894 | if (ctx->fi->state != NACC_ST_WAIT_REQUEST_SI) |
| 895 | return false; |
| 896 | return !osmo_cgi_ps_cmp(&ctx->cgi_ps, cgi_ps); |
| 897 | } |
Pau Espin Pedrol | c276e49 | 2021-09-28 12:53:35 +0200 | [diff] [blame] | 898 | |
| 899 | bool nacc_fsm_exp_ctrl_ack(const struct nacc_fsm_ctx *ctx, uint32_t fn, uint8_t ts) |
| 900 | { |
| 901 | return ctx->fi->state == NACC_ST_WAIT_CELL_CHG_CONTINUE_ACK && |
| 902 | ctx->continue_poll_fn == fn && |
| 903 | ctx->continue_poll_ts == ts; |
| 904 | } |