Pau Espin Pedrol | ea8dbdd | 2021-07-29 18:39:16 +0200 | [diff] [blame] | 1 | /* tbf_ul_ack_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. |
Pau Espin Pedrol | ea8dbdd | 2021-07-29 18:39:16 +0200 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <unistd.h> |
| 18 | |
| 19 | #include <talloc.h> |
| 20 | |
| 21 | #include <osmocom/core/bitvec.h> |
| 22 | |
| 23 | #include <tbf_ul_ack_fsm.h> |
| 24 | #include <gprs_rlcmac.h> |
| 25 | #include <gprs_debug.h> |
| 26 | #include <gprs_ms.h> |
| 27 | #include <encoding.h> |
| 28 | #include <bts.h> |
| 29 | #include <tbf.h> |
| 30 | #include <tbf_ul.h> |
| 31 | |
| 32 | #include <tbf_ul_ack_fsm.h> |
| 33 | |
| 34 | #define X(s) (1 << (s)) |
| 35 | |
Pau Espin Pedrol | 76ef666 | 2023-01-27 13:19:55 +0100 | [diff] [blame] | 36 | static const struct osmo_tdef_state_timeout tbf_ul_ack_fsm_timeouts[32] = { |
Pau Espin Pedrol | ea8dbdd | 2021-07-29 18:39:16 +0200 | [diff] [blame] | 37 | [TBF_UL_ACK_ST_NONE] = {}, |
| 38 | [TBF_UL_ACK_ST_SCHED_UL_ACK] = {}, |
| 39 | [TBF_UL_ACK_ST_WAIT_ACK] = {}, |
| 40 | }; |
| 41 | |
Pau Espin Pedrol | 76ef666 | 2023-01-27 13:19:55 +0100 | [diff] [blame] | 42 | static const struct value_string tbf_ul_ack_fsm_event_names[] = { |
Pau Espin Pedrol | ea8dbdd | 2021-07-29 18:39:16 +0200 | [diff] [blame] | 43 | { TBF_UL_ACK_EV_SCHED_ACK, "SCHED_ACK" }, |
| 44 | { TBF_UL_ACK_EV_CREATE_RLCMAC_MSG, "CREATE_RLCMAC_MSG" }, |
| 45 | { TBF_UL_ACK_EV_RX_CTRL_ACK, "RX_CTRL_ACK" }, |
| 46 | { TBF_UL_ACK_EV_POLL_TIMEOUT, "POLL_TIMEOUT" }, |
| 47 | { 0, NULL } |
| 48 | }; |
| 49 | |
Pau Espin Pedrol | 76ef666 | 2023-01-27 13:19:55 +0100 | [diff] [blame] | 50 | /* Transition to a state, using the T timer defined in tbf_ul_ack_fsm_timeouts. |
| 51 | * The actual timeout value is in turn obtained from conn->T_defs. |
| 52 | * Assumes local variable fi exists. */ |
| 53 | #define tbf_ul_ack_fsm_state_chg(fi, NEXT_STATE) \ |
| 54 | osmo_tdef_fsm_inst_state_chg(fi, NEXT_STATE, \ |
| 55 | tbf_ul_ack_fsm_timeouts, \ |
| 56 | the_pcu->T_defs, \ |
| 57 | -1) |
| 58 | |
Pau Espin Pedrol | ea8dbdd | 2021-07-29 18:39:16 +0200 | [diff] [blame] | 59 | static struct msgb *create_ul_ack_nack(const struct tbf_ul_ack_fsm_ctx *ctx, |
| 60 | const struct tbf_ul_ack_ev_create_rlcmac_msg_ctx *d, |
| 61 | bool final) |
| 62 | { |
| 63 | struct msgb *msg; |
| 64 | int rc; |
| 65 | unsigned int rrbp = 0; |
| 66 | uint32_t new_poll_fn = 0; |
Pau Espin Pedrol | 24dd3bc | 2022-10-27 15:50:56 +0200 | [diff] [blame] | 67 | struct gprs_rlcmac_ul_tbf *tbf = ctx->tbf; |
| 68 | struct GprsMs *ms = tbf_ms(ul_tbf_as_tbf(tbf)); |
Pau Espin Pedrol | ea8dbdd | 2021-07-29 18:39:16 +0200 | [diff] [blame] | 69 | |
| 70 | if (final) { |
Pau Espin Pedrol | fd74e79 | 2022-12-15 18:23:12 +0100 | [diff] [blame] | 71 | rc = tbf_check_polling(ul_tbf_as_tbf(tbf), d->pdch, d->fn, &new_poll_fn, &rrbp); |
Pau Espin Pedrol | ea8dbdd | 2021-07-29 18:39:16 +0200 | [diff] [blame] | 72 | if (rc < 0) |
| 73 | return NULL; |
| 74 | } |
| 75 | |
| 76 | msg = msgb_alloc(23, "rlcmac_ul_ack"); |
| 77 | if (!msg) |
| 78 | return NULL; |
| 79 | struct bitvec *ack_vec = bitvec_alloc(23, tbf); |
| 80 | if (!ack_vec) { |
| 81 | msgb_free(msg); |
| 82 | return NULL; |
| 83 | } |
| 84 | bitvec_unhex(ack_vec, DUMMY_VEC); |
| 85 | write_packet_uplink_ack(ack_vec, ctx->tbf, final, rrbp); |
| 86 | bitvec_pack(ack_vec, msgb_put(msg, 23)); |
| 87 | bitvec_free(ack_vec); |
| 88 | |
| 89 | /* TS 44.060 7a.2.1.1: "The contention resolution is completed on |
| 90 | * the network side when the network receives an RLC data block that |
| 91 | * comprises the TLLI value that identifies the mobile station and the |
Pau Espin Pedrol | 338a5ae | 2022-10-28 18:47:44 +0200 | [diff] [blame] | 92 | * TFI value associated with the TBF." (see TBF_EV_FIRST_UL_DATA_RECVD). |
| 93 | * |
| 94 | * However, it's handier for us to mark contention resolution success here |
| 95 | * since upon rx UL ACK is the time at which MS realizes contention resolution |
| 96 | * succeeds: |
| 97 | * TS 44.060 7.1.2.3: "The contention resolution is successfully completed |
| 98 | * on the mobile station side when the mobile station receives a |
| 99 | * PACKET UPLINK ACK/NACK" |
| 100 | */ |
Pau Espin Pedrol | f38a47e | 2022-10-28 19:06:45 +0200 | [diff] [blame] | 101 | if (ms_tlli(ms) != GSM_RESERVED_TMSI && !ul_tbf_contention_resolution_done(ctx->tbf)) |
| 102 | osmo_fsm_inst_dispatch(tbf_state_fi(ul_tbf_as_tbf(ctx->tbf)), TBF_EV_CONTENTION_RESOLUTION_MS_SUCCESS, NULL); |
Pau Espin Pedrol | ea8dbdd | 2021-07-29 18:39:16 +0200 | [diff] [blame] | 103 | |
Pau Espin Pedrol | a161bf4 | 2021-07-30 13:42:06 +0200 | [diff] [blame] | 104 | if (final) { |
Pau Espin Pedrol | 5b7eeec | 2022-12-15 19:08:13 +0100 | [diff] [blame] | 105 | tbf_set_polling(ul_tbf_as_tbf(tbf), d->pdch, new_poll_fn, PDCH_ULC_POLL_UL_ACK); |
Pau Espin Pedrol | 24dd3bc | 2022-10-27 15:50:56 +0200 | [diff] [blame] | 106 | LOGPTBFUL(tbf, LOGL_DEBUG, |
Pau Espin Pedrol | bd1f01f | 2022-10-27 15:19:39 +0200 | [diff] [blame] | 107 | "Scheduled UL Acknowledgement polling on PACCH (FN=%d, TS=%d)\n", |
Pau Espin Pedrol | 5ba3ef9 | 2022-12-12 18:02:25 +0100 | [diff] [blame] | 108 | new_poll_fn, d->pdch->ts_no); |
Pau Espin Pedrol | a161bf4 | 2021-07-30 13:42:06 +0200 | [diff] [blame] | 109 | } |
Pau Espin Pedrol | ea8dbdd | 2021-07-29 18:39:16 +0200 | [diff] [blame] | 110 | |
| 111 | return msg; |
| 112 | } |
| 113 | |
| 114 | static void st_none(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 115 | { |
| 116 | switch (event) { |
| 117 | case TBF_UL_ACK_EV_SCHED_ACK: |
| 118 | tbf_ul_ack_fsm_state_chg(fi, TBF_UL_ACK_ST_SCHED_UL_ACK); |
| 119 | break; |
| 120 | default: |
| 121 | OSMO_ASSERT(0); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | static void st_sched_ul_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 126 | { |
| 127 | struct tbf_ul_ack_fsm_ctx *ctx = (struct tbf_ul_ack_fsm_ctx *)fi->priv; |
Pau Espin Pedrol | bd1f01f | 2022-10-27 15:19:39 +0200 | [diff] [blame] | 128 | struct gprs_rlcmac_ul_tbf *tbf = ctx->tbf; |
Pau Espin Pedrol | ea8dbdd | 2021-07-29 18:39:16 +0200 | [diff] [blame] | 129 | struct tbf_ul_ack_ev_create_rlcmac_msg_ctx *data_ctx; |
| 130 | bool final; |
| 131 | |
| 132 | switch (event) { |
| 133 | case TBF_UL_ACK_EV_SCHED_ACK: |
| 134 | LOGPTBFUL(tbf, LOGL_DEBUG, |
| 135 | "Sending Ack/Nack already scheduled, no need to re-schedule\n"); |
| 136 | break; |
| 137 | case TBF_UL_ACK_EV_CREATE_RLCMAC_MSG: |
| 138 | data_ctx = (struct tbf_ul_ack_ev_create_rlcmac_msg_ctx *)data; |
Pau Espin Pedrol | bd1f01f | 2022-10-27 15:19:39 +0200 | [diff] [blame] | 139 | final = tbf_state(ul_tbf_as_tbf(tbf)) == TBF_ST_FINISHED; |
Pau Espin Pedrol | ea8dbdd | 2021-07-29 18:39:16 +0200 | [diff] [blame] | 140 | data_ctx->msg = create_ul_ack_nack(ctx, data_ctx, final); |
| 141 | if (!data_ctx->msg) |
| 142 | return; |
| 143 | if (final) /* poll set */ |
| 144 | tbf_ul_ack_fsm_state_chg(fi, TBF_UL_ACK_ST_WAIT_ACK); |
| 145 | else |
| 146 | tbf_ul_ack_fsm_state_chg(fi, TBF_UL_ACK_ST_NONE); |
| 147 | break; |
| 148 | default: |
| 149 | OSMO_ASSERT(0); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | static void st_wait_ctrl_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 154 | { |
| 155 | struct tbf_ul_ack_fsm_ctx *ctx = (struct tbf_ul_ack_fsm_ctx *)fi->priv; |
Pau Espin Pedrol | 24dd3bc | 2022-10-27 15:50:56 +0200 | [diff] [blame] | 156 | struct gprs_rlcmac_ul_tbf *tbf = ctx->tbf; |
Pau Espin Pedrol | ea8dbdd | 2021-07-29 18:39:16 +0200 | [diff] [blame] | 157 | |
| 158 | switch (event) { |
| 159 | case TBF_UL_ACK_EV_SCHED_ACK: |
| 160 | /* ignore, we are in the middle of waiting for a response */ |
| 161 | break; |
| 162 | case TBF_UL_ACK_EV_RX_CTRL_ACK: |
| 163 | tbf_ul_ack_fsm_state_chg(fi, TBF_UL_ACK_ST_NONE); |
| 164 | break; |
| 165 | case TBF_UL_ACK_EV_POLL_TIMEOUT: |
Pau Espin Pedrol | 24dd3bc | 2022-10-27 15:50:56 +0200 | [diff] [blame] | 166 | LOGPTBFUL(tbf, LOGL_NOTICE, |
Pau Espin Pedrol | ea8dbdd | 2021-07-29 18:39:16 +0200 | [diff] [blame] | 167 | "Timeout for polling PACKET CONTROL ACK for PACKET UPLINK ACK: %s\n", |
Pau Espin Pedrol | 24dd3bc | 2022-10-27 15:50:56 +0200 | [diff] [blame] | 168 | tbf_rlcmac_diag(ul_tbf_as_tbf(tbf))); |
Pau Espin Pedrol | ea8dbdd | 2021-07-29 18:39:16 +0200 | [diff] [blame] | 169 | /* Reschedule Ul Ack/NAck */ |
| 170 | tbf_ul_ack_fsm_state_chg(fi, TBF_UL_ACK_ST_SCHED_UL_ACK); |
| 171 | break; |
| 172 | default: |
| 173 | OSMO_ASSERT(0); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | static int tbf_ul_ack_fsm_timer_cb(struct osmo_fsm_inst *fi) |
| 178 | { |
| 179 | switch (fi->T) { |
| 180 | default: |
| 181 | OSMO_ASSERT(0); |
| 182 | } |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | static struct osmo_fsm_state tbf_ul_ack_fsm_states[] = { |
| 187 | [TBF_UL_ACK_ST_NONE] = { |
| 188 | .in_event_mask = |
| 189 | X(TBF_UL_ACK_EV_SCHED_ACK), |
| 190 | .out_state_mask = |
| 191 | X(TBF_UL_ACK_ST_SCHED_UL_ACK), |
| 192 | .name = "NONE", |
| 193 | .action = st_none, |
| 194 | }, |
| 195 | [TBF_UL_ACK_ST_SCHED_UL_ACK] = { |
| 196 | .in_event_mask = |
| 197 | X(TBF_UL_ACK_EV_SCHED_ACK) | |
| 198 | X(TBF_UL_ACK_EV_CREATE_RLCMAC_MSG), |
| 199 | .out_state_mask = |
| 200 | X(TBF_UL_ACK_ST_NONE) | |
| 201 | X(TBF_UL_ACK_ST_WAIT_ACK), |
| 202 | .name = "SCHED_UL_ACK", |
| 203 | .action = st_sched_ul_ack, |
| 204 | }, |
| 205 | [TBF_UL_ACK_ST_WAIT_ACK] = { |
| 206 | .in_event_mask = |
| 207 | X(TBF_UL_ACK_EV_SCHED_ACK) | |
| 208 | X(TBF_UL_ACK_EV_RX_CTRL_ACK) | |
| 209 | X(TBF_UL_ACK_EV_POLL_TIMEOUT), |
| 210 | .out_state_mask = |
| 211 | X(TBF_UL_ACK_ST_NONE) | |
| 212 | X(TBF_UL_ACK_ST_SCHED_UL_ACK), |
| 213 | .name = "WAIT_ACK", |
| 214 | .action = st_wait_ctrl_ack, |
| 215 | }, |
| 216 | }; |
| 217 | |
| 218 | struct osmo_fsm tbf_ul_ack_fsm = { |
| 219 | .name = "UL_ACK_TBF", |
| 220 | .states = tbf_ul_ack_fsm_states, |
| 221 | .num_states = ARRAY_SIZE(tbf_ul_ack_fsm_states), |
| 222 | .timer_cb = tbf_ul_ack_fsm_timer_cb, |
| 223 | .log_subsys = DTBFUL, |
| 224 | .event_names = tbf_ul_ack_fsm_event_names, |
| 225 | }; |
| 226 | |
| 227 | static __attribute__((constructor)) void tbf_ul_ack_fsm_init(void) |
| 228 | { |
| 229 | OSMO_ASSERT(osmo_fsm_register(&tbf_ul_ack_fsm) == 0); |
| 230 | } |
| 231 | |
| 232 | |
Pau Espin Pedrol | 5ba3ef9 | 2022-12-12 18:02:25 +0100 | [diff] [blame] | 233 | struct msgb *tbf_ul_ack_create_rlcmac_msg(const struct gprs_rlcmac_ul_tbf *ul_tbf, |
| 234 | const struct gprs_rlcmac_pdch *pdch, |
| 235 | uint32_t fn) |
Pau Espin Pedrol | ea8dbdd | 2021-07-29 18:39:16 +0200 | [diff] [blame] | 236 | { |
| 237 | int rc; |
| 238 | struct tbf_ul_ack_ev_create_rlcmac_msg_ctx data_ctx = { |
Pau Espin Pedrol | 5ba3ef9 | 2022-12-12 18:02:25 +0100 | [diff] [blame] | 239 | .pdch = pdch, |
Pau Espin Pedrol | ea8dbdd | 2021-07-29 18:39:16 +0200 | [diff] [blame] | 240 | .fn = fn, |
Pau Espin Pedrol | ea8dbdd | 2021-07-29 18:39:16 +0200 | [diff] [blame] | 241 | .msg = NULL, |
| 242 | }; |
Pau Espin Pedrol | ea8dbdd | 2021-07-29 18:39:16 +0200 | [diff] [blame] | 243 | |
Pau Espin Pedrol | bf2842e | 2022-10-26 20:14:58 +0200 | [diff] [blame] | 244 | rc = osmo_fsm_inst_dispatch(tbf_ul_ack_fi(ul_tbf), TBF_UL_ACK_EV_CREATE_RLCMAC_MSG, &data_ctx); |
Pau Espin Pedrol | ea8dbdd | 2021-07-29 18:39:16 +0200 | [diff] [blame] | 245 | if (rc != 0 || !data_ctx.msg) |
| 246 | return NULL; |
| 247 | return data_ctx.msg; |
| 248 | } |
| 249 | |
Pau Espin Pedrol | 106f2a0 | 2022-12-12 17:29:10 +0100 | [diff] [blame] | 250 | bool tbf_ul_ack_rts(const struct gprs_rlcmac_ul_tbf *ul_tbf, const struct gprs_rlcmac_pdch *pdch) |
Pau Espin Pedrol | ea8dbdd | 2021-07-29 18:39:16 +0200 | [diff] [blame] | 251 | { |
Pau Espin Pedrol | 106f2a0 | 2022-12-12 17:29:10 +0100 | [diff] [blame] | 252 | struct osmo_fsm_inst *fi; |
| 253 | |
| 254 | if (!tbf_is_control_ts(ul_tbf_as_tbf_const(ul_tbf), pdch)) |
| 255 | return false; |
| 256 | |
| 257 | fi = tbf_ul_ack_fi(ul_tbf); |
Pau Espin Pedrol | ea8dbdd | 2021-07-29 18:39:16 +0200 | [diff] [blame] | 258 | return fi->state == TBF_UL_ACK_ST_SCHED_UL_ACK; |
| 259 | } |
| 260 | |
| 261 | /* Did we already send the Final ACK and we are waiting for its confirmation (CTRL ACK) ? */ |
Pau Espin Pedrol | bf2842e | 2022-10-26 20:14:58 +0200 | [diff] [blame] | 262 | bool tbf_ul_ack_waiting_cnf_final_ack(const struct gprs_rlcmac_ul_tbf *ul_tbf) |
Pau Espin Pedrol | ea8dbdd | 2021-07-29 18:39:16 +0200 | [diff] [blame] | 263 | { |
Pau Espin Pedrol | bf2842e | 2022-10-26 20:14:58 +0200 | [diff] [blame] | 264 | struct osmo_fsm_inst *fi = tbf_ul_ack_fi(ul_tbf); |
Pau Espin Pedrol | ea8dbdd | 2021-07-29 18:39:16 +0200 | [diff] [blame] | 265 | return fi->state == TBF_UL_ACK_ST_WAIT_ACK; |
| 266 | } |
| 267 | |
Pau Espin Pedrol | bf2842e | 2022-10-26 20:14:58 +0200 | [diff] [blame] | 268 | bool tbf_ul_ack_exp_ctrl_ack(const struct gprs_rlcmac_ul_tbf *ul_tbf, uint32_t fn, uint8_t ts) |
Pau Espin Pedrol | ea8dbdd | 2021-07-29 18:39:16 +0200 | [diff] [blame] | 269 | { |
Pau Espin Pedrol | bf2842e | 2022-10-26 20:14:58 +0200 | [diff] [blame] | 270 | struct osmo_fsm_inst *fi = tbf_ul_ack_fi(ul_tbf); |
Pau Espin Pedrol | ea8dbdd | 2021-07-29 18:39:16 +0200 | [diff] [blame] | 271 | return fi->state == TBF_UL_ACK_ST_WAIT_ACK; |
| 272 | /* FIXME: validate FN and TS match: && ctx->poll_fn = fn && ctx->poll_ts == ts */ |
| 273 | } |