blob: a55309d07f32ed13d081e858513c72b6ffb6da43 [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{
213 switch (event) {
214 case TBF_UL_ASS_EV_RX_ASS_CTRL_ACK:
215 tbf_ul_ass_fsm_state_chg(fi, TBF_UL_ASS_NONE);
216 break;
217 case TBF_UL_ASS_EV_ASS_POLL_TIMEOUT:
218 /* Reschedule Pkt Ul Ass */
219 tbf_ul_ass_fsm_state_chg(fi, TBF_UL_ASS_SEND_ASS);
220 break;
221 default:
222 OSMO_ASSERT(0);
223 }
224}
225
226static int tbf_ul_ass_fsm_timer_cb(struct osmo_fsm_inst *fi)
227{
228 struct tbf_ul_ass_fsm_ctx *ctx = (struct tbf_ul_ass_fsm_ctx *)fi->priv;
229 switch (fi->T) {
230 case -2000:
231 tbf_free(ctx->tbf);
232 break;
233 default:
234 OSMO_ASSERT(0);
235 }
236 return 0;
237}
238
239static struct osmo_fsm_state tbf_ul_ass_fsm_states[] = {
240 [TBF_UL_ASS_NONE] = {
241 .in_event_mask =
242 X(TBF_UL_ASS_EV_SCHED_ASS) |
243 X(TBF_UL_ASS_EV_SCHED_ASS_REJ),
244 .out_state_mask =
245 X(TBF_UL_ASS_SEND_ASS) |
246 X(TBF_UL_ASS_SEND_ASS_REJ),
247 .name = "NONE",
248 .action = st_none,
249 .onenter = st_none_on_enter,
250 },
251 [TBF_UL_ASS_SEND_ASS] = {
252 .in_event_mask = X(TBF_UL_ASS_EV_CREATE_RLCMAC_MSG),
253 .out_state_mask = X(TBF_UL_ASS_WAIT_ACK),
254 .name = "SEND_ASS",
255 .action = st_send_ass,
256 },
257 [TBF_UL_ASS_SEND_ASS_REJ] = {
258 .in_event_mask = X(TBF_UL_ASS_EV_CREATE_RLCMAC_MSG),
259 .out_state_mask = X(TBF_UL_ASS_NONE),
260 .name = "SEND_ASS_REJ",
261 .action = st_send_ass_rej,
262 },
263 [TBF_UL_ASS_WAIT_ACK] = {
264 .in_event_mask =
265 X(TBF_UL_ASS_EV_RX_ASS_CTRL_ACK) |
266 X(TBF_UL_ASS_EV_ASS_POLL_TIMEOUT),
267 .out_state_mask =
268 X(TBF_UL_ASS_NONE) |
269 X(TBF_UL_ASS_SEND_ASS),
270 .name = "WAIT_ACK",
271 .action = st_wait_ack,
272 },
273};
274
275struct osmo_fsm tbf_ul_ass_fsm = {
276 .name = "UL_ASS_TBF",
277 .states = tbf_ul_ass_fsm_states,
278 .num_states = ARRAY_SIZE(tbf_ul_ass_fsm_states),
279 .timer_cb = tbf_ul_ass_fsm_timer_cb,
280 .log_subsys = DTBF,
281 .event_names = tbf_ul_ass_fsm_event_names,
282};
283
284static __attribute__((constructor)) void tbf_ul_ass_fsm_init(void)
285{
286 OSMO_ASSERT(osmo_fsm_register(&tbf_ul_ass_fsm) == 0);
287}
288
289
290struct msgb *tbf_ul_ass_create_rlcmac_msg(const struct gprs_rlcmac_tbf* tbf, uint32_t fn, uint8_t ts)
291{
292 int rc;
293 struct tbf_ul_ass_ev_create_rlcmac_msg_ctx data_ctx = {
294 .fn = fn,
295 .ts = ts,
296 .msg = NULL,
297 };
298
299 rc = osmo_fsm_inst_dispatch(tbf_ul_ass_fi(tbf), TBF_UL_ASS_EV_CREATE_RLCMAC_MSG, &data_ctx);
300 if (rc != 0 || !data_ctx.msg)
301 return NULL;
302 return data_ctx.msg;
303}
304
305bool tbf_ul_ass_rts(const struct gprs_rlcmac_tbf* tbf)
306{
307 struct osmo_fsm_inst *fi = tbf_ul_ass_fi(tbf);
308 return fi->state == TBF_UL_ASS_SEND_ASS || fi->state == TBF_UL_ASS_SEND_ASS_REJ;
309}