blob: 00f4bfd3754fa2e027f9c877615988f94cf442a9 [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);
130
131 talloc_free(mac_control_block);
132 return msg;
133
134free_ret:
135 talloc_free(mac_control_block);
136 msgb_free(msg);
137 return NULL;
138}
139
140static void st_none_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
141{
142 struct tbf_ul_ass_fsm_ctx *ctx = (struct tbf_ul_ass_fsm_ctx *)fi->priv;
143 unsigned long val;
144 unsigned int sec, micro;
145
146 /* Start release after rejecting only if it is UL TBF */
147 if (prev_state == TBF_UL_ASS_SEND_ASS_REJ &&
148 tbf_direction(ctx->tbf) == GPRS_RLCMAC_UL_TBF) {
149 /* tbf_free() called upon trigger */
150 fi->T = -2000;
151 val = osmo_tdef_get(the_pcu->T_defs, fi->T, OSMO_TDEF_MS, -1);
152 sec = val / 1000;
153 micro = (val % 1000) * 1000;
154 LOGPTBF(ctx->tbf, LOGL_DEBUG, "starting timer X2000 [reject (PACCH)] with %u sec. %u microsec\n",
155 sec, micro);
156 osmo_timer_schedule(&fi->timer, sec, micro);
157 }
158
159}
160
161static void st_none(struct osmo_fsm_inst *fi, uint32_t event, void *data)
162{
163 switch (event) {
164 case TBF_UL_ASS_EV_SCHED_ASS:
165 tbf_ul_ass_fsm_state_chg(fi, TBF_UL_ASS_SEND_ASS);
166 break;
167 case TBF_UL_ASS_EV_SCHED_ASS_REJ:
168 tbf_ul_ass_fsm_state_chg(fi, TBF_UL_ASS_SEND_ASS_REJ);
169 break;
170 default:
171 OSMO_ASSERT(0);
172 }
173}
174
175static void st_send_ass(struct osmo_fsm_inst *fi, uint32_t event, void *data)
176{
177 struct tbf_ul_ass_fsm_ctx *ctx = (struct tbf_ul_ass_fsm_ctx *)fi->priv;
178 struct tbf_ul_ass_ev_create_rlcmac_msg_ctx *data_ctx;
179
180 switch (event) {
181 case TBF_UL_ASS_EV_CREATE_RLCMAC_MSG:
182 data_ctx = (struct tbf_ul_ass_ev_create_rlcmac_msg_ctx *)data;
183 data_ctx->msg = create_packet_ul_assign(ctx, data_ctx);
184 if (!data_ctx->msg)
185 return;
186 tbf_ul_ass_fsm_state_chg(fi, TBF_UL_ASS_WAIT_ACK);
187 break;
188 default:
189 OSMO_ASSERT(0);
190 }
191}
192
193static void st_send_ass_rej(struct osmo_fsm_inst *fi, uint32_t event, void *data)
194{
195 struct tbf_ul_ass_fsm_ctx *ctx = (struct tbf_ul_ass_fsm_ctx *)fi->priv;
196 struct tbf_ul_ass_ev_create_rlcmac_msg_ctx *data_ctx;
197
198 switch (event) {
199 case TBF_UL_ASS_EV_CREATE_RLCMAC_MSG:
200 data_ctx = (struct tbf_ul_ass_ev_create_rlcmac_msg_ctx *)data;
201 data_ctx->msg = create_packet_access_reject(ctx);
202 if (!data_ctx->msg)
203 return;
204 tbf_ul_ass_fsm_state_chg(fi, TBF_UL_ASS_NONE);
205 break;
206 default:
207 OSMO_ASSERT(0);
208 }
209}
210
211static void st_wait_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
212{
Pau Espin Pedrol432d4f32021-07-27 16:09:30 +0200213 struct tbf_ul_ass_fsm_ctx *ctx = (struct tbf_ul_ass_fsm_ctx *)fi->priv;
214
Pau Espin Pedrol6ad11a62021-07-27 12:27:08 +0200215 switch (event) {
216 case TBF_UL_ASS_EV_RX_ASS_CTRL_ACK:
217 tbf_ul_ass_fsm_state_chg(fi, TBF_UL_ASS_NONE);
218 break;
219 case TBF_UL_ASS_EV_ASS_POLL_TIMEOUT:
Pau Espin Pedrol432d4f32021-07-27 16:09:30 +0200220 LOGPTBF(ctx->tbf, LOGL_NOTICE,
221 "Timeout for polling PACKET CONTROL ACK for PACKET UPLINK ASSIGNMENT: %s\n",
222 tbf_rlcmac_diag(ctx->tbf));
Pau Espin Pedrol6ad11a62021-07-27 12:27:08 +0200223 /* Reschedule Pkt Ul Ass */
224 tbf_ul_ass_fsm_state_chg(fi, TBF_UL_ASS_SEND_ASS);
225 break;
226 default:
227 OSMO_ASSERT(0);
228 }
229}
230
231static int tbf_ul_ass_fsm_timer_cb(struct osmo_fsm_inst *fi)
232{
233 struct tbf_ul_ass_fsm_ctx *ctx = (struct tbf_ul_ass_fsm_ctx *)fi->priv;
234 switch (fi->T) {
235 case -2000:
236 tbf_free(ctx->tbf);
237 break;
238 default:
239 OSMO_ASSERT(0);
240 }
241 return 0;
242}
243
244static struct osmo_fsm_state tbf_ul_ass_fsm_states[] = {
245 [TBF_UL_ASS_NONE] = {
246 .in_event_mask =
247 X(TBF_UL_ASS_EV_SCHED_ASS) |
248 X(TBF_UL_ASS_EV_SCHED_ASS_REJ),
249 .out_state_mask =
250 X(TBF_UL_ASS_SEND_ASS) |
251 X(TBF_UL_ASS_SEND_ASS_REJ),
252 .name = "NONE",
253 .action = st_none,
254 .onenter = st_none_on_enter,
255 },
256 [TBF_UL_ASS_SEND_ASS] = {
257 .in_event_mask = X(TBF_UL_ASS_EV_CREATE_RLCMAC_MSG),
258 .out_state_mask = X(TBF_UL_ASS_WAIT_ACK),
259 .name = "SEND_ASS",
260 .action = st_send_ass,
261 },
262 [TBF_UL_ASS_SEND_ASS_REJ] = {
263 .in_event_mask = X(TBF_UL_ASS_EV_CREATE_RLCMAC_MSG),
264 .out_state_mask = X(TBF_UL_ASS_NONE),
265 .name = "SEND_ASS_REJ",
266 .action = st_send_ass_rej,
267 },
268 [TBF_UL_ASS_WAIT_ACK] = {
269 .in_event_mask =
270 X(TBF_UL_ASS_EV_RX_ASS_CTRL_ACK) |
271 X(TBF_UL_ASS_EV_ASS_POLL_TIMEOUT),
272 .out_state_mask =
273 X(TBF_UL_ASS_NONE) |
274 X(TBF_UL_ASS_SEND_ASS),
275 .name = "WAIT_ACK",
276 .action = st_wait_ack,
277 },
278};
279
280struct osmo_fsm tbf_ul_ass_fsm = {
281 .name = "UL_ASS_TBF",
282 .states = tbf_ul_ass_fsm_states,
283 .num_states = ARRAY_SIZE(tbf_ul_ass_fsm_states),
284 .timer_cb = tbf_ul_ass_fsm_timer_cb,
285 .log_subsys = DTBF,
286 .event_names = tbf_ul_ass_fsm_event_names,
287};
288
289static __attribute__((constructor)) void tbf_ul_ass_fsm_init(void)
290{
291 OSMO_ASSERT(osmo_fsm_register(&tbf_ul_ass_fsm) == 0);
292}
293
294
295struct msgb *tbf_ul_ass_create_rlcmac_msg(const struct gprs_rlcmac_tbf* tbf, uint32_t fn, uint8_t ts)
296{
297 int rc;
298 struct tbf_ul_ass_ev_create_rlcmac_msg_ctx data_ctx = {
299 .fn = fn,
300 .ts = ts,
301 .msg = NULL,
302 };
303
304 rc = osmo_fsm_inst_dispatch(tbf_ul_ass_fi(tbf), TBF_UL_ASS_EV_CREATE_RLCMAC_MSG, &data_ctx);
305 if (rc != 0 || !data_ctx.msg)
306 return NULL;
307 return data_ctx.msg;
308}
309
310bool tbf_ul_ass_rts(const struct gprs_rlcmac_tbf* tbf)
311{
312 struct osmo_fsm_inst *fi = tbf_ul_ass_fi(tbf);
313 return fi->state == TBF_UL_ASS_SEND_ASS || fi->state == TBF_UL_ASS_SEND_ASS_REJ;
314}