aboutsummaryrefslogtreecommitdiffstats
path: root/src/tbf_ul_ass_fsm.c
blob: 6f88f112e323fa612f5e1fff794ae2984d420baa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/* tbf_ul_ass_fsm.c
 *
 * Copyright (C) 2021 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
 * Author: Pau Espin Pedrol <pespin@sysmocom.de>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <unistd.h>

#include <talloc.h>

#include <osmocom/core/bitvec.h>

#include <tbf_ul_ass_fsm.h>
#include <gprs_rlcmac.h>
#include <gprs_debug.h>
#include <gprs_ms.h>
#include <encoding.h>
#include <bts.h>
#include <tbf.h>
#include <tbf_ul.h>

#define X(s) (1 << (s))

static const struct osmo_tdef_state_timeout tbf_ul_ass_fsm_timeouts[32] = {
	[TBF_UL_ASS_NONE] = {},
	[TBF_UL_ASS_SEND_ASS] = { .keep_timer = true },
	[TBF_UL_ASS_SEND_ASS_REJ] = {},
	[TBF_UL_ASS_WAIT_ACK] = { .keep_timer = true },
};

static const struct value_string tbf_ul_ass_fsm_event_names[] = {
	{ TBF_UL_ASS_EV_SCHED_ASS, "SCHED_ASS" },
	{ TBF_UL_ASS_EV_SCHED_ASS_REJ, "SCHED_ASS_REJ" },
	{ TBF_UL_ASS_EV_CREATE_RLCMAC_MSG, "CREATE_RLCMAC_MSG" },
	{ TBF_UL_ASS_EV_RX_ASS_CTRL_ACK, "RX_ASS_CTRL_ACK" },
	{ TBF_UL_ASS_EV_ASS_POLL_TIMEOUT, "ASS_POLL_TIMEOUT" },
	{ TBF_UL_ASS_EV_ABORT, "ABORT" },
	{ 0, NULL }
};

/* Transition to a state, using the T timer defined in tbf_ul_ass_fsm_timeouts.
 * The actual timeout value is in turn obtained from conn->T_defs.
 * Assumes local variable fi exists. */
#define tbf_ul_ass_fsm_state_chg(fi, NEXT_STATE) \
	osmo_tdef_fsm_inst_state_chg(fi, NEXT_STATE, \
				     tbf_ul_ass_fsm_timeouts, \
				     the_pcu->T_defs, \
				     -1)

static struct msgb *create_packet_access_reject(const struct tbf_ul_ass_fsm_ctx *ctx)
{
	struct msgb *msg;
	struct GprsMs *ms = tbf_ms(ctx->tbf);

	msg = msgb_alloc(GSM_MACBLOCK_LEN, "rlcmac_ul_ass_rej");

	struct bitvec *packet_access_rej = bitvec_alloc(GSM_MACBLOCK_LEN, ctx->tbf);

	bitvec_unhex(packet_access_rej, DUMMY_VEC);

	write_packet_access_reject(packet_access_rej, ms_tlli(ms),
				   osmo_tdef_get(ms->bts->pcu->T_defs, 3172, OSMO_TDEF_MS, -1));

	bts_do_rate_ctr_inc(ms->bts, CTR_PKT_ACCESS_REJ);

	bitvec_pack(packet_access_rej, msgb_put(msg, GSM_MACBLOCK_LEN));

	bitvec_free(packet_access_rej);
	return msg;

}

static struct msgb *create_packet_ul_assign(const struct tbf_ul_ass_fsm_ctx *ctx,
				     const struct tbf_ul_ass_ev_create_rlcmac_msg_ctx *d)
{
	struct msgb *msg = NULL;
	struct gprs_rlcmac_ul_tbf *new_tbf = NULL;
	RlcMacDownlink_t *mac_control_block = NULL;
	struct GprsMs *ms = tbf_ms(ctx->tbf);
	uint32_t tlli;
	int rc;
	unsigned int rrbp;
	uint32_t new_poll_fn;

	rc = tbf_check_polling(ctx->tbf, d->pdch, d->fn, &new_poll_fn, &rrbp);
	if (rc < 0)
		return NULL;

	new_tbf = ms_ul_tbf(ms);
	if (!new_tbf) {
		LOGPTBF(ctx->tbf, LOGL_ERROR,
			"We have a schedule for uplink assignment, but there is no uplink TBF\n");
		tbf_ul_ass_fsm_state_chg(ctx->fi, TBF_UL_ASS_NONE);
		return NULL;
	}

	msg = msgb_alloc(GSM_MACBLOCK_LEN, "rlcmac_ul_ass");
	if (!msg)
		return NULL;

	/* Initialize a bit vector that uses allocated msgb as the data buffer. */
	struct bitvec bv = {
		.data = msgb_put(msg, GSM_MACBLOCK_LEN),
		.data_len = GSM_MACBLOCK_LEN,
	};
	bitvec_unhex(&bv, DUMMY_VEC);

	if (ctx->tbf != ul_tbf_as_tbf_const(new_tbf))
		LOGPTBF(ctx->tbf, LOGL_INFO, "start Packet Uplink Assignment (PACCH) for %s\n",
			  tbf_name(ul_tbf_as_tbf_const(new_tbf)));
	else
		LOGPTBF(ctx->tbf, LOGL_INFO, "start Packet Uplink Assignment (PACCH)\n");

	mac_control_block = (RlcMacDownlink_t *)talloc_zero(ctx->tbf, RlcMacDownlink_t);
	tlli = ms_tlli(ms);
	write_packet_uplink_assignment(mac_control_block, tbf_tfi(ctx->tbf),
		(tbf_direction(ctx->tbf) == GPRS_RLCMAC_DL_TBF), ms_tlli(ms),
		 tlli != GSM_RESERVED_TMSI, new_tbf, 1, rrbp, bts_get_ms_pwr_alpha(ms->bts),
		the_pcu->vty.gamma, -1, tbf_is_egprs_enabled(ctx->tbf));

	LOGP(DTBF, LOGL_DEBUG, "+++++++++++++++++++++++++ TX : Packet Uplink Assignment +++++++++++++++++++++++++\n");
	rc = encode_gsm_rlcmac_downlink(&bv, mac_control_block);
	if (rc < 0) {
		LOGP(DTBF, LOGL_ERROR, "Encoding of Packet Uplink Ass failed (%d)\n", rc);
		goto free_ret;
	}
	LOGP(DTBF, LOGL_DEBUG, "------------------------- TX : Packet Uplink Assignment -------------------------\n");
	bts_do_rate_ctr_inc(ms->bts, CTR_PKT_UL_ASSIGNMENT);

	tbf_set_polling(ctx->tbf, d->pdch, new_poll_fn, PDCH_ULC_POLL_UL_ASS);
	LOGPTBF(ctx->tbf, LOGL_INFO, "Scheduled UL Assignment polling on PACCH (FN=%d, TS=%d)\n",
		  new_poll_fn, d->pdch->ts_no);

	talloc_free(mac_control_block);
	return msg;

free_ret:
	talloc_free(mac_control_block);
	msgb_free(msg);
	return NULL;
}

static void st_none_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
{
	struct tbf_ul_ass_fsm_ctx *ctx = (struct tbf_ul_ass_fsm_ctx *)fi->priv;
	unsigned long val;
	unsigned int sec, micro;

	/* Start release after rejecting only if it is UL TBF */
	if (prev_state == TBF_UL_ASS_SEND_ASS_REJ &&
	    tbf_direction(ctx->tbf) == GPRS_RLCMAC_UL_TBF) {
		/* tbf_free() called upon trigger */
		fi->T = -2000;
		val = osmo_tdef_get(the_pcu->T_defs, fi->T, OSMO_TDEF_MS, -1);
		sec = val / 1000;
		micro = (val % 1000) * 1000;
		LOGPTBF(ctx->tbf, LOGL_DEBUG, "Starting timer X2000 [reject (PACCH)] with %u sec. %u microsec\n",
			sec, micro);
		osmo_timer_schedule(&fi->timer, sec, micro);
	}

}

static void st_none(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
	switch (event) {
	case TBF_UL_ASS_EV_SCHED_ASS:
		tbf_ul_ass_fsm_state_chg(fi, TBF_UL_ASS_SEND_ASS);
		break;
	case TBF_UL_ASS_EV_SCHED_ASS_REJ:
		tbf_ul_ass_fsm_state_chg(fi, TBF_UL_ASS_SEND_ASS_REJ);
		break;
	case TBF_UL_ASS_EV_ABORT:
		/* Nothing to do, we are already in proper state */
		break;
	default:
		OSMO_ASSERT(0);
	}
}

static void st_send_ass_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
{
	struct tbf_ul_ass_fsm_ctx *ctx = (struct tbf_ul_ass_fsm_ctx *)fi->priv;
	unsigned long val;
	unsigned int sec, micro;
	struct GprsMs *ms = tbf_ms(ctx->tbf);

	/* Here it's point in time where we received PKT RES REQ or DL ACK/NACK to request a new UL TBF,
	 * so MS will be gone after T3168 (* 4 retrans, 8.1.1.1.2) if we are unable to seize it.
	 * Hence, attempt re-scheduling PKT UL ASS (states SEND_ASS<->WAIT_ACK ping-pong) until T3168 we
	 * announced (SI13) to the MS expires:
	 */
	if (prev_state == TBF_UL_ASS_NONE) {
		/* tbf_free() called upon trigger */
		fi->T = 3168;
		val = osmo_tdef_get(ms->bts->T_defs_bts, fi->T, OSMO_TDEF_MS, -1);
		val *= 4; /* 4 PKT RES REQ retransmit */
		sec = val / 1000;
		micro = (val % 1000) * 1000;
		LOGPTBF(ctx->tbf, LOGL_DEBUG, "Starting timer T3168 [PKT UL ASS PACCH] with %u sec. %u microsec\n",
			sec, micro);
		osmo_timer_schedule(&fi->timer, sec, micro);
	}

}

static void st_send_ass(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
	struct tbf_ul_ass_fsm_ctx *ctx = (struct tbf_ul_ass_fsm_ctx *)fi->priv;
	struct tbf_ul_ass_ev_create_rlcmac_msg_ctx *data_ctx;

	switch (event) {
	case TBF_UL_ASS_EV_CREATE_RLCMAC_MSG:
		data_ctx = (struct tbf_ul_ass_ev_create_rlcmac_msg_ctx *)data;
		data_ctx->msg = create_packet_ul_assign(ctx, data_ctx);
		if (!data_ctx->msg)
			return;
		tbf_ul_ass_fsm_state_chg(fi, TBF_UL_ASS_WAIT_ACK);
		break;
	case TBF_UL_ASS_EV_ABORT:
		/* Cancel pending schedule for Pkt Ul Ass: */
		tbf_ul_ass_fsm_state_chg(fi, TBF_UL_ASS_NONE);
		break;
	default:
		OSMO_ASSERT(0);
	}
}

static void st_send_ass_rej(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
	struct tbf_ul_ass_fsm_ctx *ctx = (struct tbf_ul_ass_fsm_ctx *)fi->priv;
	struct tbf_ul_ass_ev_create_rlcmac_msg_ctx *data_ctx;

	switch (event) {
	case TBF_UL_ASS_EV_CREATE_RLCMAC_MSG:
		data_ctx = (struct tbf_ul_ass_ev_create_rlcmac_msg_ctx *)data;
		data_ctx->msg = create_packet_access_reject(ctx);
		if (!data_ctx->msg)
			return;
		tbf_ul_ass_fsm_state_chg(fi, TBF_UL_ASS_NONE);
		break;
	case TBF_UL_ASS_EV_ABORT:
		/* Cancel pending schedule for Pkt Ul Ass Rej: */
		tbf_ul_ass_fsm_state_chg(fi, TBF_UL_ASS_NONE);
		break;
	default:
		OSMO_ASSERT(0);
	}
}

static void st_wait_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
	struct tbf_ul_ass_fsm_ctx *ctx = (struct tbf_ul_ass_fsm_ctx *)fi->priv;

	switch (event) {
	case TBF_UL_ASS_EV_RX_ASS_CTRL_ACK:
		tbf_ul_ass_fsm_state_chg(fi, TBF_UL_ASS_NONE);
		break;
	case TBF_UL_ASS_EV_ASS_POLL_TIMEOUT:
		LOGPTBF(ctx->tbf, LOGL_NOTICE,
			"Timeout for polling PACKET CONTROL ACK for PACKET UPLINK ASSIGNMENT: %s\n",
			tbf_rlcmac_diag(ctx->tbf));
		if (tbf_state(ctx->tbf) == TBF_ST_ASSIGN) {
			/* Reschedule Pkt Ul Ass */
			tbf_ul_ass_fsm_state_chg(fi, TBF_UL_ASS_SEND_ASS);
		} else {
			/* We are most probably in RELEASING, so stop retrying. */
			tbf_ul_ass_fsm_state_chg(fi, TBF_UL_ASS_NONE);
		}
		break;
	case TBF_UL_ASS_EV_ABORT:
		/* There's nothing we can do here, we already transmitted and
		 * hence we must keep the POLL since the MS is already expected
		 * to transmit there. Whenever we receive event CTRL_ACK or
		 * TIMEOUT above, it will move back to ST_NONE autoamtically */
		break;
	default:
		OSMO_ASSERT(0);
	}
}

static int tbf_ul_ass_fsm_timer_cb(struct osmo_fsm_inst *fi)
{
	struct tbf_ul_ass_fsm_ctx *ctx = (struct tbf_ul_ass_fsm_ctx *)fi->priv;
	switch (fi->T) {
	case -2000:
	case 3168:
		tbf_free(ctx->tbf);
		break;
	default:
		OSMO_ASSERT(0);
	}
	return 0;
}

static struct osmo_fsm_state tbf_ul_ass_fsm_states[] = {
	[TBF_UL_ASS_NONE] = {
		.in_event_mask =
			X(TBF_UL_ASS_EV_SCHED_ASS) |
			X(TBF_UL_ASS_EV_SCHED_ASS_REJ) |
			X(TBF_UL_ASS_EV_ABORT),
		.out_state_mask =
			X(TBF_UL_ASS_SEND_ASS) |
			X(TBF_UL_ASS_SEND_ASS_REJ),
		.name = "NONE",
		.action = st_none,
		.onenter = st_none_on_enter,
	},
	[TBF_UL_ASS_SEND_ASS] = {
		.in_event_mask =
			X(TBF_UL_ASS_EV_CREATE_RLCMAC_MSG) |
			X(TBF_UL_ASS_EV_ABORT),
		.out_state_mask =
			X(TBF_UL_ASS_WAIT_ACK) |
			X(TBF_UL_ASS_NONE),
		.name = "SEND_ASS",
		.action = st_send_ass,
		.onenter = st_send_ass_on_enter,
	},
	[TBF_UL_ASS_SEND_ASS_REJ] = {
		.in_event_mask =
			X(TBF_UL_ASS_EV_CREATE_RLCMAC_MSG) |
			X(TBF_UL_ASS_EV_ABORT),
		.out_state_mask = X(TBF_UL_ASS_NONE),
		.name = "SEND_ASS_REJ",
		.action = st_send_ass_rej,
	},
	[TBF_UL_ASS_WAIT_ACK] = {
		.in_event_mask =
			X(TBF_UL_ASS_EV_RX_ASS_CTRL_ACK) |
			X(TBF_UL_ASS_EV_ASS_POLL_TIMEOUT) |
			X(TBF_UL_ASS_EV_ABORT),
		.out_state_mask =
			X(TBF_UL_ASS_NONE) |
			X(TBF_UL_ASS_SEND_ASS),
		.name = "WAIT_ACK",
		.action = st_wait_ack,
	},
};

struct osmo_fsm tbf_ul_ass_fsm = {
	.name = "UL_ASS_TBF",
	.states = tbf_ul_ass_fsm_states,
	.num_states = ARRAY_SIZE(tbf_ul_ass_fsm_states),
	.timer_cb = tbf_ul_ass_fsm_timer_cb,
	.log_subsys = DTBF,
	.event_names = tbf_ul_ass_fsm_event_names,
};

static __attribute__((constructor)) void tbf_ul_ass_fsm_init(void)
{
	OSMO_ASSERT(osmo_fsm_register(&tbf_ul_ass_fsm) == 0);
}


struct msgb *tbf_ul_ass_create_rlcmac_msg(const struct gprs_rlcmac_tbf *tbf,
					  const struct gprs_rlcmac_pdch *pdch,
					  uint32_t fn)
{
	int rc;
	struct tbf_ul_ass_ev_create_rlcmac_msg_ctx data_ctx = {
		.pdch = pdch,
		.fn = fn,
		.msg = NULL,
	};

	rc = osmo_fsm_inst_dispatch(tbf_ul_ass_fi(tbf), TBF_UL_ASS_EV_CREATE_RLCMAC_MSG, &data_ctx);
	if (rc != 0 || !data_ctx.msg)
		return NULL;
	return data_ctx.msg;
}

bool tbf_ul_ass_rts(const struct gprs_rlcmac_tbf *tbf, const struct gprs_rlcmac_pdch *pdch)
{
	struct osmo_fsm_inst *fi;

	if (!tbf_is_control_ts(tbf, pdch))
		return false;

	fi = tbf_ul_ass_fi(tbf);
	return fi->state == TBF_UL_ASS_SEND_ASS || fi->state == TBF_UL_ASS_SEND_ASS_REJ;
}