blob: 618e0722b27f1f404cf6677fa91edfb077fe0eed [file] [log] [blame]
Pau Espin Pedrol6ad11a62021-07-27 12:27:08 +02001/* tbf_ul_ass_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/bitvec.h>
26
27#include <tbf_ul_ass_fsm.h>
28#include <gprs_rlcmac.h>
29#include <gprs_debug.h>
30#include <gprs_ms.h>
31#include <encoding.h>
32#include <bts.h>
33#include <tbf.h>
34#include <tbf_ul.h>
35
36#define X(s) (1 << (s))
37
38const struct osmo_tdef_state_timeout tbf_ul_ass_fsm_timeouts[32] = {
39 [TBF_UL_ASS_NONE] = {},
40 [TBF_UL_ASS_SEND_ASS] = {},
41 [TBF_UL_ASS_SEND_ASS_REJ] = {},
42 [TBF_UL_ASS_WAIT_ACK] = {},
43};
44
45const struct value_string tbf_ul_ass_fsm_event_names[] = {
46 { TBF_UL_ASS_EV_SCHED_ASS, "SCHED_ASS" },
47 { TBF_UL_ASS_EV_SCHED_ASS_REJ, "SCHED_ASS_REJ" },
48 { TBF_UL_ASS_EV_CREATE_RLCMAC_MSG, "CREATE_RLCMAC_MSG" },
49 { TBF_UL_ASS_EV_RX_ASS_CTRL_ACK, "RX_ASS_CTRL_ACK" },
50 { TBF_UL_ASS_EV_ASS_POLL_TIMEOUT, "ASS_POLL_TIMEOUT" },
51 { 0, NULL }
52};
53
54static struct msgb *create_packet_access_reject(const struct tbf_ul_ass_fsm_ctx *ctx)
55{
56 struct msgb *msg;
57 struct GprsMs *ms = tbf_ms(ctx->tbf);
58
59 msg = msgb_alloc(GSM_MACBLOCK_LEN, "rlcmac_ul_ass_rej");
60
61 struct bitvec *packet_access_rej = bitvec_alloc(GSM_MACBLOCK_LEN, ctx->tbf);
62
63 bitvec_unhex(packet_access_rej, DUMMY_VEC);
64
65 write_packet_access_reject(packet_access_rej, ms_tlli(ms),
66 osmo_tdef_get(ms->bts->T_defs_bts, 3172, OSMO_TDEF_MS, -1));
67
68 bts_do_rate_ctr_inc(ms->bts, CTR_PKT_ACCESS_REJ);
69
70 bitvec_pack(packet_access_rej, msgb_put(msg, GSM_MACBLOCK_LEN));
71
72 bitvec_free(packet_access_rej);
73 return msg;
74
75}
76
77struct msgb *create_packet_ul_assign(const struct tbf_ul_ass_fsm_ctx *ctx,
78 const struct tbf_ul_ass_ev_create_rlcmac_msg_ctx *d)
79{
80 struct msgb *msg = NULL;
81 struct gprs_rlcmac_ul_tbf *new_tbf = NULL;
82 RlcMacDownlink_t *mac_control_block = NULL;
83 struct GprsMs *ms = tbf_ms(ctx->tbf);
84 uint32_t tlli;
85 int rc;
86 unsigned int rrbp;
87 uint32_t new_poll_fn;
88
89 rc = tbf_check_polling(ctx->tbf, d->fn, d->ts, &new_poll_fn, &rrbp);
90 if (rc < 0)
91 return NULL;
92
93 new_tbf = ms_ul_tbf(ms);
94 if (!new_tbf) {
95 LOGPTBF(ctx->tbf, LOGL_ERROR,
96 "We have a schedule for uplink assignment, but there is no uplink TBF\n");
97 tbf_ul_ass_fsm_state_chg(ctx->fi, TBF_UL_ASS_NONE);
98 return NULL;
99 }
100
101 msg = msgb_alloc(GSM_MACBLOCK_LEN, "rlcmac_ul_ass");
102 if (!msg)
103 return NULL;
104
105 /* Initialize a bit vector that uses allocated msgb as the data buffer. */
106 struct bitvec bv = {
107 .data = msgb_put(msg, GSM_MACBLOCK_LEN),
108 .data_len = GSM_MACBLOCK_LEN,
109 };
110 bitvec_unhex(&bv, DUMMY_VEC);
111
112 LOGPTBFUL((const struct gprs_rlcmac_tbf *)new_tbf, LOGL_INFO, "start Packet Uplink Assignment (PACCH)\n");
113 mac_control_block = (RlcMacDownlink_t *)talloc_zero(ctx->tbf, RlcMacDownlink_t);
114 tlli = ms_tlli(ms);
115 write_packet_uplink_assignment(mac_control_block, tbf_tfi(ctx->tbf),
116 (tbf_direction(ctx->tbf) == GPRS_RLCMAC_DL_TBF), ms_tlli(ms),
117 tlli != GSM_RESERVED_TMSI, new_tbf, 1, rrbp, bts_get_ms_pwr_alpha(ms->bts),
118 the_pcu->vty.gamma, -1, tbf_is_egprs_enabled(ctx->tbf));
119
120 LOGP(DTBF, LOGL_DEBUG, "+++++++++++++++++++++++++ TX : Packet Uplink Assignment +++++++++++++++++++++++++\n");
121 rc = encode_gsm_rlcmac_downlink(&bv, mac_control_block);
122 if (rc < 0) {
123 LOGP(DTBF, LOGL_ERROR, "Encoding of Packet Uplink Ass failed (%d)\n", rc);
124 goto free_ret;
125 }
126 LOGP(DTBF, LOGL_DEBUG, "------------------------- TX : Packet Uplink Assignment -------------------------\n");
127 bts_do_rate_ctr_inc(ms->bts, CTR_PKT_UL_ASSIGNMENT);
128
129 tbf_set_polling(ctx->tbf, new_poll_fn, d->ts, PDCH_ULC_POLL_UL_ASS);
Pau Espin Pedrola161bf42021-07-30 13:42:06 +0200130 LOGPTBFDL(ctx->tbf, LOGL_INFO, "Scheduled UL Assignment polling on PACCH (FN=%d, TS=%d)\n",
131 new_poll_fn, d->ts);
Pau Espin Pedrol6ad11a62021-07-27 12:27:08 +0200132
133 talloc_free(mac_control_block);
134 return msg;
135
136free_ret:
137 talloc_free(mac_control_block);
138 msgb_free(msg);
139 return NULL;
140}
141
142static void st_none_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
143{
144 struct tbf_ul_ass_fsm_ctx *ctx = (struct tbf_ul_ass_fsm_ctx *)fi->priv;
145 unsigned long val;
146 unsigned int sec, micro;
147
148 /* Start release after rejecting only if it is UL TBF */
149 if (prev_state == TBF_UL_ASS_SEND_ASS_REJ &&
150 tbf_direction(ctx->tbf) == GPRS_RLCMAC_UL_TBF) {
151 /* tbf_free() called upon trigger */
152 fi->T = -2000;
153 val = osmo_tdef_get(the_pcu->T_defs, fi->T, OSMO_TDEF_MS, -1);
154 sec = val / 1000;
155 micro = (val % 1000) * 1000;
156 LOGPTBF(ctx->tbf, LOGL_DEBUG, "starting timer X2000 [reject (PACCH)] with %u sec. %u microsec\n",
157 sec, micro);
158 osmo_timer_schedule(&fi->timer, sec, micro);
159 }
160
161}
162
163static void st_none(struct osmo_fsm_inst *fi, uint32_t event, void *data)
164{
165 switch (event) {
166 case TBF_UL_ASS_EV_SCHED_ASS:
167 tbf_ul_ass_fsm_state_chg(fi, TBF_UL_ASS_SEND_ASS);
168 break;
169 case TBF_UL_ASS_EV_SCHED_ASS_REJ:
170 tbf_ul_ass_fsm_state_chg(fi, TBF_UL_ASS_SEND_ASS_REJ);
171 break;
172 default:
173 OSMO_ASSERT(0);
174 }
175}
176
177static void st_send_ass(struct osmo_fsm_inst *fi, uint32_t event, void *data)
178{
179 struct tbf_ul_ass_fsm_ctx *ctx = (struct tbf_ul_ass_fsm_ctx *)fi->priv;
180 struct tbf_ul_ass_ev_create_rlcmac_msg_ctx *data_ctx;
181
182 switch (event) {
183 case TBF_UL_ASS_EV_CREATE_RLCMAC_MSG:
184 data_ctx = (struct tbf_ul_ass_ev_create_rlcmac_msg_ctx *)data;
185 data_ctx->msg = create_packet_ul_assign(ctx, data_ctx);
186 if (!data_ctx->msg)
187 return;
188 tbf_ul_ass_fsm_state_chg(fi, TBF_UL_ASS_WAIT_ACK);
189 break;
190 default:
191 OSMO_ASSERT(0);
192 }
193}
194
195static void st_send_ass_rej(struct osmo_fsm_inst *fi, uint32_t event, void *data)
196{
197 struct tbf_ul_ass_fsm_ctx *ctx = (struct tbf_ul_ass_fsm_ctx *)fi->priv;
198 struct tbf_ul_ass_ev_create_rlcmac_msg_ctx *data_ctx;
199
200 switch (event) {
201 case TBF_UL_ASS_EV_CREATE_RLCMAC_MSG:
202 data_ctx = (struct tbf_ul_ass_ev_create_rlcmac_msg_ctx *)data;
203 data_ctx->msg = create_packet_access_reject(ctx);
204 if (!data_ctx->msg)
205 return;
206 tbf_ul_ass_fsm_state_chg(fi, TBF_UL_ASS_NONE);
207 break;
208 default:
209 OSMO_ASSERT(0);
210 }
211}
212
213static void st_wait_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
214{
Pau Espin Pedrol432d4f32021-07-27 16:09:30 +0200215 struct tbf_ul_ass_fsm_ctx *ctx = (struct tbf_ul_ass_fsm_ctx *)fi->priv;
216
Pau Espin Pedrol6ad11a62021-07-27 12:27:08 +0200217 switch (event) {
218 case TBF_UL_ASS_EV_RX_ASS_CTRL_ACK:
219 tbf_ul_ass_fsm_state_chg(fi, TBF_UL_ASS_NONE);
220 break;
221 case TBF_UL_ASS_EV_ASS_POLL_TIMEOUT:
Pau Espin Pedrol432d4f32021-07-27 16:09:30 +0200222 LOGPTBF(ctx->tbf, LOGL_NOTICE,
223 "Timeout for polling PACKET CONTROL ACK for PACKET UPLINK ASSIGNMENT: %s\n",
224 tbf_rlcmac_diag(ctx->tbf));
Pau Espin Pedrol6ad11a62021-07-27 12:27:08 +0200225 /* Reschedule Pkt Ul Ass */
226 tbf_ul_ass_fsm_state_chg(fi, TBF_UL_ASS_SEND_ASS);
227 break;
228 default:
229 OSMO_ASSERT(0);
230 }
231}
232
233static int tbf_ul_ass_fsm_timer_cb(struct osmo_fsm_inst *fi)
234{
235 struct tbf_ul_ass_fsm_ctx *ctx = (struct tbf_ul_ass_fsm_ctx *)fi->priv;
236 switch (fi->T) {
237 case -2000:
238 tbf_free(ctx->tbf);
239 break;
240 default:
241 OSMO_ASSERT(0);
242 }
243 return 0;
244}
245
246static struct osmo_fsm_state tbf_ul_ass_fsm_states[] = {
247 [TBF_UL_ASS_NONE] = {
248 .in_event_mask =
249 X(TBF_UL_ASS_EV_SCHED_ASS) |
250 X(TBF_UL_ASS_EV_SCHED_ASS_REJ),
251 .out_state_mask =
252 X(TBF_UL_ASS_SEND_ASS) |
253 X(TBF_UL_ASS_SEND_ASS_REJ),
254 .name = "NONE",
255 .action = st_none,
256 .onenter = st_none_on_enter,
257 },
258 [TBF_UL_ASS_SEND_ASS] = {
259 .in_event_mask = X(TBF_UL_ASS_EV_CREATE_RLCMAC_MSG),
Pau Espin Pedrolea7cb482021-09-22 16:53:41 +0200260 .out_state_mask =
261 X(TBF_UL_ASS_WAIT_ACK) |
262 X(TBF_UL_ASS_NONE),
Pau Espin Pedrol6ad11a62021-07-27 12:27:08 +0200263 .name = "SEND_ASS",
264 .action = st_send_ass,
265 },
266 [TBF_UL_ASS_SEND_ASS_REJ] = {
267 .in_event_mask = X(TBF_UL_ASS_EV_CREATE_RLCMAC_MSG),
268 .out_state_mask = X(TBF_UL_ASS_NONE),
269 .name = "SEND_ASS_REJ",
270 .action = st_send_ass_rej,
271 },
272 [TBF_UL_ASS_WAIT_ACK] = {
273 .in_event_mask =
274 X(TBF_UL_ASS_EV_RX_ASS_CTRL_ACK) |
275 X(TBF_UL_ASS_EV_ASS_POLL_TIMEOUT),
276 .out_state_mask =
277 X(TBF_UL_ASS_NONE) |
278 X(TBF_UL_ASS_SEND_ASS),
279 .name = "WAIT_ACK",
280 .action = st_wait_ack,
281 },
282};
283
284struct osmo_fsm tbf_ul_ass_fsm = {
285 .name = "UL_ASS_TBF",
286 .states = tbf_ul_ass_fsm_states,
287 .num_states = ARRAY_SIZE(tbf_ul_ass_fsm_states),
288 .timer_cb = tbf_ul_ass_fsm_timer_cb,
289 .log_subsys = DTBF,
290 .event_names = tbf_ul_ass_fsm_event_names,
291};
292
293static __attribute__((constructor)) void tbf_ul_ass_fsm_init(void)
294{
295 OSMO_ASSERT(osmo_fsm_register(&tbf_ul_ass_fsm) == 0);
296}
297
298
299struct msgb *tbf_ul_ass_create_rlcmac_msg(const struct gprs_rlcmac_tbf* tbf, uint32_t fn, uint8_t ts)
300{
301 int rc;
302 struct tbf_ul_ass_ev_create_rlcmac_msg_ctx data_ctx = {
303 .fn = fn,
304 .ts = ts,
305 .msg = NULL,
306 };
307
308 rc = osmo_fsm_inst_dispatch(tbf_ul_ass_fi(tbf), TBF_UL_ASS_EV_CREATE_RLCMAC_MSG, &data_ctx);
309 if (rc != 0 || !data_ctx.msg)
310 return NULL;
311 return data_ctx.msg;
312}
313
314bool tbf_ul_ass_rts(const struct gprs_rlcmac_tbf* tbf)
315{
316 struct osmo_fsm_inst *fi = tbf_ul_ass_fi(tbf);
317 return fi->state == TBF_UL_ASS_SEND_ASS || fi->state == TBF_UL_ASS_SEND_ASS_REJ;
318}