blob: 65b164eb1ef7f33325d8f234074940e80a629bd5 [file] [log] [blame]
Daniel Willmannca102af2014-08-08 12:14:12 +02001/* Copied from tbf.cpp
2 *
3 * Copyright (C) 2012 Ivan Klyuchnikov
4 * Copyright (C) 2012 Andreas Eversberg <jolly@eversberg.eu>
5 * Copyright (C) 2013 by Holger Hans Peter Freyther
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21
22#include <bts.h>
23#include <tbf.h>
24#include <rlc.h>
25#include <gprs_rlcmac.h>
26#include <gprs_debug.h>
27#include <gprs_bssgp_pcu.h>
28#include <decoding.h>
29
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +010030#include "pcu_utils.h"
31
Daniel Willmannca102af2014-08-08 12:14:12 +020032extern "C" {
33#include <osmocom/core/msgb.h>
34#include <osmocom/core/talloc.h>
35}
36
37#include <errno.h>
38#include <string.h>
39
40/* After sending these frames, we poll for ack/nack. */
41#define POLL_ACK_AFTER_FRAMES 20
42
43
44static const struct gprs_rlcmac_cs gprs_rlcmac_cs[] = {
45/* frame length data block max payload */
46 { 0, 0, 0 },
47 { 23, 23, 20 }, /* CS-1 */
48 { 34, 33, 30 }, /* CS-2 */
49 { 40, 39, 36 }, /* CS-3 */
50 { 54, 53, 50 }, /* CS-4 */
51};
52
53extern "C" {
54int bssgp_tx_llc_discarded(struct bssgp_bvc_ctx *bctx, uint32_t tlli,
55 uint8_t num_frames, uint32_t num_octets);
56}
57
58static inline void tbf_update_ms_class(struct gprs_rlcmac_tbf *tbf,
59 const uint8_t ms_class)
60{
61 if (!tbf->ms_class && ms_class)
62 tbf->ms_class = ms_class;
63}
64
65int gprs_rlcmac_dl_tbf::append_data(const uint8_t ms_class,
66 const uint16_t pdu_delay_csec,
67 const uint8_t *data, const uint16_t len)
68{
69 LOGP(DRLCMAC, LOGL_INFO, "%s append\n", tbf_name(this));
70 if (state_is(GPRS_RLCMAC_WAIT_RELEASE)) {
71 LOGP(DRLCMAC, LOGL_DEBUG,
72 "%s in WAIT RELEASE state "
73 "(T3193), so reuse TBF\n", tbf_name(this));
74 tbf_update_ms_class(this, ms_class);
75 reuse_tbf(data, len);
Jacob Erlbeckc4952092015-03-24 11:04:19 +010076 } else if (!have_data()) {
77 m_llc.put_frame(data, len);
78 bts->llc_frame_sched();
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +010079 /* it is no longer drained */
80 m_last_dl_drained_fn = -1;
Jacob Erlbeckc4952092015-03-24 11:04:19 +010081 tbf_update_ms_class(this, ms_class);
Daniel Willmannca102af2014-08-08 12:14:12 +020082 } else {
83 /* the TBF exists, so we must write it in the queue
84 * we prepend lifetime in front of PDU */
85 struct timeval *tv;
86 struct msgb *llc_msg = msgb_alloc(len + sizeof(*tv) * 2,
87 "llc_pdu_queue");
88 if (!llc_msg)
89 return -ENOMEM;
90 tv = (struct timeval *)msgb_put(llc_msg, sizeof(*tv));
91 gprs_llc::calc_pdu_lifetime(bts, pdu_delay_csec, tv);
92 tv = (struct timeval *)msgb_put(llc_msg, sizeof(*tv));
93 gettimeofday(tv, NULL);
94 memcpy(msgb_put(llc_msg, len), data, len);
95 m_llc.enqueue(llc_msg);
96 tbf_update_ms_class(this, ms_class);
97 }
98
99 return 0;
100}
101
102static struct gprs_rlcmac_dl_tbf *tbf_lookup_dl(BTS *bts,
103 const uint32_t tlli, const char *imsi)
104{
105 /* TODO: look up by IMSI first, then tlli, then old_tlli */
106 return bts->dl_tbf_by_tlli(tlli);
107}
108
109static int tbf_new_dl_assignment(struct gprs_rlcmac_bts *bts,
110 const char *imsi,
111 const uint32_t tlli, const uint8_t ms_class,
112 const uint8_t *data, const uint16_t len)
113{
114 uint8_t trx, ta, ss;
115 int8_t use_trx;
116 struct gprs_rlcmac_ul_tbf *ul_tbf, *old_ul_tbf;
117 struct gprs_rlcmac_dl_tbf *dl_tbf;
118 int8_t tfi; /* must be signed */
119 int rc;
120
121 /* check for uplink data, so we copy our informations */
122#warning "Do the same look up for IMSI, TLLI and OLD_TLLI"
123#warning "Refactor the below lines... into a new method"
124 ul_tbf = bts->bts->ul_tbf_by_tlli(tlli);
125 if (ul_tbf && ul_tbf->m_contention_resolution_done
126 && !ul_tbf->m_final_ack_sent) {
127 use_trx = ul_tbf->trx->trx_no;
128 ta = ul_tbf->ta;
129 ss = 0;
130 old_ul_tbf = ul_tbf;
131 } else {
132 use_trx = -1;
133 /* we already have an uplink TBF, so we use that TA */
134 if (ul_tbf)
135 ta = ul_tbf->ta;
136 else {
137 /* recall TA */
138 rc = bts->bts->timing_advance()->recall(tlli);
139 if (rc < 0) {
140 LOGP(DRLCMAC, LOGL_NOTICE, "TA unknown"
141 ", assuming 0\n");
142 ta = 0;
143 } else
144 ta = rc;
145 }
146 ss = 1; /* PCH assignment only allows one timeslot */
147 old_ul_tbf = NULL;
148 }
149
150 // Create new TBF (any TRX)
151#warning "Copy and paste with alloc_ul_tbf"
152 tfi = bts->bts->tfi_find_free(GPRS_RLCMAC_DL_TBF, &trx, use_trx);
153 if (tfi < 0) {
154 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource\n");
155 /* FIXME: send reject */
156 return -EBUSY;
157 }
158 /* set number of downlink slots according to multislot class */
159 dl_tbf = tbf_alloc_dl_tbf(bts, ul_tbf, tfi, trx, ms_class, ss);
160 if (!dl_tbf) {
161 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource\n");
162 /* FIXME: send reject */
163 return -EBUSY;
164 }
165 dl_tbf->m_tlli = tlli;
166 dl_tbf->m_tlli_valid = 1;
167 dl_tbf->ta = ta;
168
169 LOGP(DRLCMAC, LOGL_DEBUG, "%s [DOWNLINK] START\n", tbf_name(dl_tbf));
170
171 /* new TBF, so put first frame */
172 dl_tbf->m_llc.put_frame(data, len);
173 dl_tbf->bts->llc_frame_sched();
174
175 /* Store IMSI for later look-up and PCH retransmission */
176 dl_tbf->assign_imsi(imsi);
177
178 /* trigger downlink assignment and set state to ASSIGN.
179 * we don't use old_downlink, so the possible uplink is used
180 * to trigger downlink assignment. if there is no uplink,
181 * AGCH is used. */
182 dl_tbf->bts->trigger_dl_ass(dl_tbf, old_ul_tbf, imsi);
183 return 0;
184}
185
186/**
187 * TODO: split into unit test-able parts...
188 */
189int gprs_rlcmac_dl_tbf::handle(struct gprs_rlcmac_bts *bts,
190 const uint32_t tlli, const char *imsi,
191 const uint8_t ms_class, const uint16_t delay_csec,
192 const uint8_t *data, const uint16_t len)
193{
194 struct gprs_rlcmac_dl_tbf *dl_tbf;
195
196 /* check for existing TBF */
197 dl_tbf = tbf_lookup_dl(bts->bts, tlli, imsi);
198 if (dl_tbf) {
199 int rc = dl_tbf->append_data(ms_class, delay_csec, data, len);
200 if (rc >= 0)
201 dl_tbf->assign_imsi(imsi);
202 return rc;
203 }
204
205 return tbf_new_dl_assignment(bts, imsi, tlli, ms_class, data, len);
206}
207
208struct msgb *gprs_rlcmac_dl_tbf::llc_dequeue(bssgp_bvc_ctx *bctx)
209{
210 struct msgb *msg;
211 struct timeval *tv, tv_now;
212 uint32_t octets = 0, frames = 0;
213
214 gettimeofday(&tv_now, NULL);
215
216 while ((msg = m_llc.dequeue())) {
217 tv = (struct timeval *)msg->data;
218 msgb_pull(msg, sizeof(*tv));
219 msgb_pull(msg, sizeof(*tv));
220
221 if (gprs_llc::is_frame_expired(&tv_now, tv)) {
222 LOGP(DRLCMACDL, LOGL_NOTICE, "%s Discarding LLC PDU "
223 "because lifetime limit reached. Queue size %zu\n",
224 tbf_name(this), m_llc.m_queue_size);
225 bts->llc_timedout_frame();
226 frames++;
227 octets += msg->len;
228 msgb_free(msg);
229 continue;
230 }
231 break;
232 }
233
234 if (frames) {
235 if (frames > 0xff)
236 frames = 0xff;
237 if (octets > 0xffffff)
238 octets = 0xffffff;
239 bssgp_tx_llc_discarded(bctx, m_tlli, frames, octets);
240 }
241
242 return msg;
243}
244
245/*
246 * Create DL data block
247 * The messages are fragmented and forwarded as data blocks.
248 */
249struct msgb *gprs_rlcmac_dl_tbf::create_dl_acked_block(uint32_t fn, uint8_t ts)
250{
251 LOGP(DRLCMACDL, LOGL_DEBUG, "%s downlink (V(A)==%d .. "
252 "V(S)==%d)\n", tbf_name(this),
253 m_window.v_a(), m_window.v_s());
254
255do_resend:
256 /* check if there is a block with negative acknowledgement */
257 int resend_bsn = m_window.resend_needed();
258 if (resend_bsn >= 0) {
259 LOGP(DRLCMACDL, LOGL_DEBUG, "- Resending BSN %d\n", resend_bsn);
260 /* re-send block with negative aknowlegement */
261 m_window.m_v_b.mark_unacked(resend_bsn);
262 bts->rlc_resent();
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100263 return create_dl_acked_block(fn, ts, resend_bsn);
Daniel Willmannca102af2014-08-08 12:14:12 +0200264 }
265
266 /* if the window has stalled, or transfer is complete,
267 * send an unacknowledged block */
Jacob Erlbeck95340242015-03-19 13:22:07 +0100268 if (state_is(GPRS_RLCMAC_FINISHED)) {
269 LOGP(DRLCMACDL, LOGL_DEBUG, "- Restarting at BSN %d, "
270 "because all blocks have been transmitted.\n",
271 m_window.v_a());
272 bts->rlc_restarted();
273 } else if (dl_window_stalled()) {
274 LOGP(DRLCMACDL, LOGL_NOTICE, "- Restarting at BSN %d, "
275 "because all window is stalled.\n",
276 m_window.v_a());
277 bts->rlc_stalled();
278 } else {
279 /* No blocks are left */
280 return create_new_bsn(fn, ts);
Daniel Willmannca102af2014-08-08 12:14:12 +0200281 }
282
Jacob Erlbeck95340242015-03-19 13:22:07 +0100283 /* If V(S) == V(A) and finished state, we would have received
284 * acknowledgement of all transmitted block. In this case we
285 * would have transmitted the final block, and received ack
286 * from MS. But in this case we did not receive the final ack
287 * indication from MS. This should never happen if MS works
288 * correctly. */
289 if (m_window.window_empty()) {
290 LOGP(DRLCMACDL, LOGL_DEBUG, "- MS acked all blocks, "
291 "so we re-transmit final block!\n");
292 /* we just send final block again */
293 int16_t index = m_window.v_s_mod(-1);
294 bts->rlc_resent();
295 return create_dl_acked_block(fn, ts, index);
296 }
297
298 /* cycle through all unacked blocks */
299 int resend = m_window.mark_for_resend();
300
301 /* At this point there should be at least one unacked block
302 * to be resent. If not, this is an software error. */
303 if (resend == 0) {
304 LOGP(DRLCMACDL, LOGL_ERROR, "Software error: "
305 "There are no unacknowledged blocks, but V(A) "
306 " != V(S). PLEASE FIX!\n");
307 /* we just send final block again */
308 int16_t index = m_window.v_s_mod(-1);
309 return create_dl_acked_block(fn, ts, index);
310 }
311 goto do_resend;
Daniel Willmannca102af2014-08-08 12:14:12 +0200312}
313
314struct msgb *gprs_rlcmac_dl_tbf::create_new_bsn(const uint32_t fn, const uint8_t ts)
315{
316 struct rlc_dl_header *rh;
317 struct rlc_li_field *li;
318 struct msgb *msg;
319 uint8_t *delimiter, *data, *e_pointer;
320 uint16_t space, chunk;
321 gprs_rlc_data *rlc_data;
Daniel Willmannca102af2014-08-08 12:14:12 +0200322 const uint16_t bsn = m_window.v_s();
323
324 LOGP(DRLCMACDL, LOGL_DEBUG, "- Sending new block at BSN %d\n",
325 m_window.v_s());
326
327#warning "Selection of the CS doesn't belong here"
328 if (cs == 0) {
329 cs = bts_data()->initial_cs_dl;
330 if (cs < 1 || cs > 4)
331 cs = 1;
332 }
333 /* total length of block, including spare bits */
334 const uint8_t block_length = gprs_rlcmac_cs[cs].block_length;
335 /* length of usable data of block, w/o spare bits, inc. MAC */
336 const uint8_t block_data_len = gprs_rlcmac_cs[cs].block_data;
337
338 /* now we still have untransmitted LLC data, so we fill mac block */
339 rlc_data = m_rlc.block(bsn);
340 data = rlc_data->prepare(block_data_len);
341
342 rh = (struct rlc_dl_header *)data;
343 rh->pt = 0; /* Data Block */
344 rh->rrbp = rh->s_p = 0; /* Polling, set later, if required */
345 rh->usf = 7; /* will be set at scheduler */
346 rh->pr = 0; /* FIXME: power reduction */
347 rh->tfi = m_tfi; /* TFI */
348 rh->fbi = 0; /* Final Block Indicator, set late, if true */
349 rh->bsn = bsn; /* Block Sequence Number */
350 rh->e = 0; /* Extension bit, maybe set later */
351 e_pointer = data + 2; /* points to E of current chunk */
352 data += sizeof(*rh);
353 delimiter = data; /* where next length header would be stored */
354 space = block_data_len - sizeof(*rh);
355 while (1) {
Jacob Erlbeckcbb1e702015-03-25 12:21:55 +0100356 if (m_llc.frame_length() == 0) {
357 /* A header will need to by added, so we just need
358 * space-1 octets */
359 m_llc.put_dummy_frame(space - 1);
360
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100361 /* The data just drained, store the current fn */
362 if (m_last_dl_drained_fn < 0)
363 m_last_dl_drained_fn = fn;
364
Jacob Erlbeckcbb1e702015-03-25 12:21:55 +0100365 /* It is not clear, when the next real data will
366 * arrive, so request a DL ack/nack now */
367 request_dl_ack();
368
369 LOGP(DRLCMACDL, LOGL_DEBUG,
370 "-- Empty chunk, "
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100371 "added LLC dummy command of size %d, "
372 "drained_since=%d\n",
373 m_llc.frame_length(), frames_since_last_drain(fn));
Jacob Erlbeckcbb1e702015-03-25 12:21:55 +0100374 }
375
Daniel Willmannca102af2014-08-08 12:14:12 +0200376 chunk = m_llc.chunk_size();
Jacob Erlbeckcbb1e702015-03-25 12:21:55 +0100377
Daniel Willmannca102af2014-08-08 12:14:12 +0200378 /* if chunk will exceed block limit */
379 if (chunk > space) {
380 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d "
381 "larger than space (%d) left in block: copy "
382 "only remaining space, and we are done\n",
383 chunk, space);
384 /* block is filled, so there is no extension */
385 *e_pointer |= 0x01;
386 /* fill only space */
387 m_llc.consume(data, space);
388 /* return data block as message */
389 break;
390 }
391 /* if FINAL chunk would fit precisely in space left */
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100392 if (chunk == space && llist_empty(&m_llc.queue) && !keep_open(fn))
393 {
Daniel Willmannca102af2014-08-08 12:14:12 +0200394 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d "
395 "would exactly fit into space (%d): because "
396 "this is a final block, we don't add length "
397 "header, and we are done\n", chunk, space);
398 LOGP(DRLCMACDL, LOGL_INFO, "Complete DL frame for "
399 "%s that fits precisely in last block: "
400 "len=%d\n", tbf_name(this), m_llc.frame_length());
401 gprs_rlcmac_dl_bw(this, m_llc.frame_length());
402 /* block is filled, so there is no extension */
403 *e_pointer |= 0x01;
404 /* fill space */
405 m_llc.consume(data, space);
406 m_llc.reset();
407 /* final block */
408 rh->fbi = 1; /* we indicate final block */
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100409 request_dl_ack();
Daniel Willmannca102af2014-08-08 12:14:12 +0200410 set_state(GPRS_RLCMAC_FINISHED);
411 /* return data block as message */
412 break;
413 }
414 /* if chunk would fit exactly in space left */
415 if (chunk == space) {
416 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d "
417 "would exactly fit into space (%d): add length "
418 "header with LI=0, to make frame extend to "
419 "next block, and we are done\n", chunk, space);
420 /* make space for delimiter */
421 if (delimiter != data)
422 memmove(delimiter + 1, delimiter,
423 data - delimiter);
424 data++;
425 space--;
426 /* add LI with 0 length */
427 li = (struct rlc_li_field *)delimiter;
428 li->e = 1; /* not more extension */
429 li->m = 0; /* shall be set to 0, in case of li = 0 */
430 li->li = 0; /* chunk fills the complete space */
431 // no need to set e_pointer nor increase delimiter
432 /* fill only space, which is 1 octet less than chunk */
433 m_llc.consume(data, space);
434 /* return data block as message */
435 break;
436 }
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100437
Daniel Willmannca102af2014-08-08 12:14:12 +0200438 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d is less "
439 "than remaining space (%d): add length header to "
440 "to delimit LLC frame\n", chunk, space);
441 /* the LLC frame chunk ends in this block */
442 /* make space for delimiter */
443 if (delimiter != data)
444 memmove(delimiter + 1, delimiter, data - delimiter);
445 data++;
446 space--;
447 /* add LI to delimit frame */
448 li = (struct rlc_li_field *)delimiter;
449 li->e = 0; /* Extension bit, maybe set later */
450 li->m = 0; /* will be set later, if there is more LLC data */
451 li->li = chunk; /* length of chunk */
452 e_pointer = delimiter; /* points to E of current delimiter */
453 delimiter++;
454 /* copy (rest of) LLC frame to space and reset later */
455 m_llc.consume(data, chunk);
456 data += chunk;
457 space -= chunk;
458 LOGP(DRLCMACDL, LOGL_INFO, "Complete DL frame for %s"
459 "len=%d\n", tbf_name(this), m_llc.frame_length());
460 gprs_rlcmac_dl_bw(this, m_llc.frame_length());
461 m_llc.reset();
462 /* dequeue next LLC frame, if any */
463 msg = llc_dequeue(gprs_bssgp_pcu_current_bctx());
464 if (msg) {
465 LOGP(DRLCMACDL, LOGL_INFO, "- Dequeue next LLC for "
466 "%s (len=%d)\n", tbf_name(this), msg->len);
467 m_llc.put_frame(msg->data, msg->len);
468 bts->llc_frame_sched();
469 msgb_free(msg);
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100470 m_last_dl_drained_fn = -1;
Daniel Willmannca102af2014-08-08 12:14:12 +0200471 }
472 /* if we have more data and we have space left */
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100473 if (space > 0 && (m_llc.frame_length() || keep_open(fn))) {
Daniel Willmannca102af2014-08-08 12:14:12 +0200474 li->m = 1; /* we indicate more frames to follow */
475 continue;
476 }
477 /* if we don't have more LLC frames */
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100478 if (!m_llc.frame_length() && !keep_open(fn)) {
Daniel Willmannca102af2014-08-08 12:14:12 +0200479 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Final block, so we "
480 "done.\n");
481 li->e = 1; /* we cannot extend */
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100482
Daniel Willmannca102af2014-08-08 12:14:12 +0200483 rh->fbi = 1; /* we indicate final block */
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100484 request_dl_ack();
Daniel Willmannca102af2014-08-08 12:14:12 +0200485 set_state(GPRS_RLCMAC_FINISHED);
486 break;
487 }
488 /* we have no space left */
489 LOGP(DRLCMACDL, LOGL_DEBUG, "-- No space left, so we are "
490 "done.\n");
491 li->e = 1; /* we cannot extend */
492 break;
493 }
494 LOGP(DRLCMACDL, LOGL_DEBUG, "data block: %s\n",
495 osmo_hexdump(rlc_data->block, block_length));
496#warning "move this up?"
497 rlc_data->len = block_length;
498 /* raise send state and set ack state array */
499 m_window.m_v_b.mark_unacked(bsn);
500 m_window.increment_send();
501
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100502 return create_dl_acked_block(fn, ts, bsn);
503}
504
Daniel Willmannca102af2014-08-08 12:14:12 +0200505struct msgb *gprs_rlcmac_dl_tbf::create_dl_acked_block(
506 const uint32_t fn, const uint8_t ts,
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100507 const int index)
Daniel Willmannca102af2014-08-08 12:14:12 +0200508{
509 uint8_t *data;
510 struct rlc_dl_header *rh;
511 struct msgb *dl_msg;
512 uint8_t len;
Daniel Willmannefd5dbb2014-08-25 16:20:23 +0200513 bool need_poll;
Daniel Willmannca102af2014-08-08 12:14:12 +0200514
515 /* get data and header from current block */
516 data = m_rlc.block(index)->block;
517 len = m_rlc.block(index)->len;
518 rh = (struct rlc_dl_header *)data;
519
Jacob Erlbeck005ee7f2015-03-20 14:53:54 +0100520 /* If the TBF has just started, relate frames_since_last_poll to the
521 * current fn */
522 if (m_last_dl_poll_fn < 0)
523 m_last_dl_poll_fn = fn;
524
Daniel Willmannefd5dbb2014-08-25 16:20:23 +0200525 need_poll = state_flags & (1 << GPRS_RLCMAC_FLAG_TO_DL_ACK);
Daniel Willmannca102af2014-08-08 12:14:12 +0200526 /* Clear Polling, if still set in history buffer */
527 rh->s_p = 0;
528
529 /* poll after POLL_ACK_AFTER_FRAMES frames, or when final block is tx.
530 */
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100531 if (m_tx_counter >= POLL_ACK_AFTER_FRAMES || m_dl_ack_requested ||
Daniel Willmannefd5dbb2014-08-25 16:20:23 +0200532 need_poll) {
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100533 if (m_dl_ack_requested) {
Daniel Willmannca102af2014-08-08 12:14:12 +0200534 LOGP(DRLCMACDL, LOGL_DEBUG, "- Scheduling Ack/Nack "
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100535 "polling, because is was requested explicitly "
536 "(e.g. first final block sent).\n");
Daniel Willmannefd5dbb2014-08-25 16:20:23 +0200537 } else if (need_poll) {
538 LOGP(DRLCMACDL, LOGL_DEBUG, "- Scheduling Ack/Nack "
Daniel Willmann635d47c2014-09-17 17:58:29 +0200539 "polling, because polling timed out.\n");
Daniel Willmannca102af2014-08-08 12:14:12 +0200540 } else {
541 LOGP(DRLCMACDL, LOGL_DEBUG, "- Scheduling Ack/Nack "
542 "polling, because %d blocks sent.\n",
543 POLL_ACK_AFTER_FRAMES);
544 }
545 /* scheduling not possible, because: */
546 if (poll_state != GPRS_RLCMAC_POLL_NONE)
547 LOGP(DRLCMACDL, LOGL_DEBUG, "Polling is already "
548 "sheduled for %s, so we must wait for "
549 "requesting downlink ack\n", tbf_name(this));
550 else if (control_ts != ts)
551 LOGP(DRLCMACDL, LOGL_DEBUG, "Polling cannot be "
552 "sheduled in this TS %d, waiting for "
553 "TS %d\n", ts, control_ts);
Daniel Willmannca102af2014-08-08 12:14:12 +0200554 else if (bts->sba()->find(trx->trx_no, ts, (fn + 13) % 2715648))
555 LOGP(DRLCMACDL, LOGL_DEBUG, "Polling cannot be "
556 "sheduled, because single block alllocation "
557 "already exists\n");
558 else {
559 LOGP(DRLCMACDL, LOGL_DEBUG, "Polling sheduled in this "
560 "TS %d\n", ts);
561 m_tx_counter = 0;
562 /* start timer whenever we send the final block */
563 if (rh->fbi == 1)
564 tbf_timer_start(this, 3191, bts_data()->t3191, 0);
565
566 /* schedule polling */
567 poll_state = GPRS_RLCMAC_POLL_SCHED;
568 poll_fn = (fn + 13) % 2715648;
569
Daniel Willmannefd5dbb2014-08-25 16:20:23 +0200570 /* Clear poll timeout flag */
571 state_flags &= ~(1 << GPRS_RLCMAC_FLAG_TO_DL_ACK);
572
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100573 /* Clear request flag */
574 m_dl_ack_requested = false;
575
Daniel Willmannca102af2014-08-08 12:14:12 +0200576 /* set polling in header */
577 rh->rrbp = 0; /* N+13 */
578 rh->s_p = 1; /* Polling */
Jacob Erlbeck005ee7f2015-03-20 14:53:54 +0100579
580 m_last_dl_poll_fn = poll_fn;
Daniel Willmannca102af2014-08-08 12:14:12 +0200581 }
582 }
583
584 /* return data block as message */
585 dl_msg = msgb_alloc(len, "rlcmac_dl_data");
586 if (!dl_msg)
587 return NULL;
588
589 /* Increment TX-counter */
590 m_tx_counter++;
591
592 memcpy(msgb_put(dl_msg, len), data, len);
593 bts->rlc_sent();
594
595 return dl_msg;
596}
597
598int gprs_rlcmac_dl_tbf::update_window(const uint8_t ssn, const uint8_t *rbb)
599{
600 int16_t dist; /* must be signed */
601 uint16_t lost = 0, received = 0;
602 char show_rbb[65];
603 char show_v_b[RLC_MAX_SNS + 1];
604 const uint16_t mod_sns = m_window.mod_sns();
605
606 Decoding::extract_rbb(rbb, show_rbb);
607 /* show received array in debug (bit 64..1) */
608 LOGP(DRLCMACDL, LOGL_DEBUG, "- ack: (BSN=%d)\"%s\""
609 "(BSN=%d) R=ACK I=NACK\n", (ssn - 64) & mod_sns,
610 show_rbb, (ssn - 1) & mod_sns);
611
612 /* apply received array to receive state (SSN-64..SSN-1) */
613 /* calculate distance of ssn from V(S) */
614 dist = (m_window.v_s() - ssn) & mod_sns;
615 /* check if distance is less than distance V(A)..V(S) */
616 if (dist >= m_window.distance()) {
617 /* this might happpen, if the downlink assignment
618 * was not received by ms and the ack refers
619 * to previous TBF
620 * FIXME: we should implement polling for
621 * control ack!*/
622 LOGP(DRLCMACDL, LOGL_NOTICE, "- ack range is out of "
623 "V(A)..V(S) range %s Free TBF!\n", tbf_name(this));
624 return 1; /* indicate to free TBF */
625 }
626
627 m_window.update(bts, show_rbb, ssn,
628 &lost, &received);
629
630 /* report lost and received packets */
631 gprs_rlcmac_received_lost(this, received, lost);
632
633 /* raise V(A), if possible */
634 m_window.raise(m_window.move_window());
635
636 /* show receive state array in debug (V(A)..V(S)-1) */
637 m_window.show_state(show_v_b);
638 LOGP(DRLCMACDL, LOGL_DEBUG, "- V(B): (V(A)=%d)\"%s\""
639 "(V(S)-1=%d) A=Acked N=Nacked U=Unacked "
640 "X=Resend-Unacked I=Invalid\n",
641 m_window.v_a(), show_v_b,
642 m_window.v_s_mod(-1));
643
644 if (state_is(GPRS_RLCMAC_FINISHED) && m_window.window_empty()) {
645 LOGP(DRLCMACDL, LOGL_NOTICE, "Received acknowledge of "
646 "all blocks, but without final ack "
647 "inidcation (don't worry)\n");
648 }
649 return 0;
650}
651
652
653int gprs_rlcmac_dl_tbf::maybe_start_new_window()
654{
655 struct msgb *msg;
656 uint16_t received;
657
658 LOGP(DRLCMACDL, LOGL_DEBUG, "- Final ACK received.\n");
659 /* range V(A)..V(S)-1 */
660 received = m_window.count_unacked();
661
662 /* report all outstanding packets as received */
663 gprs_rlcmac_received_lost(this, received, 0);
664
665 set_state(GPRS_RLCMAC_WAIT_RELEASE);
666
667 /* check for LLC PDU in the LLC Queue */
668 msg = llc_dequeue(gprs_bssgp_pcu_current_bctx());
669 if (!msg) {
670 /* no message, start T3193, change state to RELEASE */
671 LOGP(DRLCMACDL, LOGL_DEBUG, "- No new message, so we release.\n");
672 /* start T3193 */
673 tbf_timer_start(this, 3193,
674 bts_data()->t3193_msec / 1000,
675 (bts_data()->t3193_msec % 1000) * 1000);
676
677 return 0;
678 }
679
680 /* we have more data so we will re-use this tbf */
681 reuse_tbf(msg->data, msg->len);
682 msgb_free(msg);
683 return 0;
684}
685
686int gprs_rlcmac_dl_tbf::rcvd_dl_ack(uint8_t final_ack, uint8_t ssn, uint8_t *rbb)
687{
688 LOGP(DRLCMACDL, LOGL_DEBUG, "%s downlink acknowledge\n", tbf_name(this));
689
690 if (!final_ack)
691 return update_window(ssn, rbb);
692 return maybe_start_new_window();
693}
694
695void gprs_rlcmac_dl_tbf::reuse_tbf(const uint8_t *data, const uint16_t len)
696{
Daniel Willmanne4818152014-08-15 16:52:09 +0200697 uint8_t trx;
698 struct gprs_rlcmac_dl_tbf *new_tbf;
699 int8_t tfi; /* must be signed */
Daniel Willmanne4818152014-08-15 16:52:09 +0200700 struct msgb *msg;
701
Daniel Willmannca102af2014-08-08 12:14:12 +0200702 bts->tbf_reused();
Daniel Willmanne4818152014-08-15 16:52:09 +0200703
704 tfi = bts->tfi_find_free(GPRS_RLCMAC_DL_TBF, &trx, this->trx->trx_no);
705 if (tfi < 0) {
706 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource\n");
707 /* FIXME: send reject */
708 return;
709 }
710 new_tbf = tbf_alloc_dl_tbf(bts->bts_data(), NULL, tfi, trx, ms_class, 0);
711 if (!new_tbf) {
712 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource\n");
713 /* FIXME: send reject */
714 return;
715 }
716
717 new_tbf->m_tlli = m_tlli;
718 new_tbf->m_tlli_valid = m_tlli_valid;
719 new_tbf->ta = ta;
720 new_tbf->assign_imsi(m_imsi);
721
722 /* Copy over all data to the new TBF */
723 new_tbf->m_llc.put_frame(data, len);
Daniel Willmannca102af2014-08-08 12:14:12 +0200724 bts->llc_frame_sched();
725
Daniel Willmanne4818152014-08-15 16:52:09 +0200726 while ((msg = m_llc.dequeue()))
727 new_tbf->m_llc.enqueue(msg);
728
Daniel Willmannca102af2014-08-08 12:14:12 +0200729 /* reset rlc states */
730 m_tx_counter = 0;
731 m_wait_confirm = 0;
732 m_window.reset();
733
Jacob Erlbeck297edf72015-02-26 14:59:52 +0100734 /* mark TLLI as invalid */
735 m_tlli_valid = 0;
736
Daniel Willmannca102af2014-08-08 12:14:12 +0200737 /* keep to flags */
738 state_flags &= GPRS_RLCMAC_FLAG_TO_MASK;
739 state_flags &= ~(1 << GPRS_RLCMAC_FLAG_CCCH);
740
741 update();
742
743 LOGP(DRLCMAC, LOGL_DEBUG, "%s Trigger dowlink assignment on PACCH, "
744 "because another LLC PDU has arrived in between\n",
745 tbf_name(this));
Daniel Willmanne4818152014-08-15 16:52:09 +0200746 bts->trigger_dl_ass(new_tbf, this, NULL);
Daniel Willmannca102af2014-08-08 12:14:12 +0200747}
748
749bool gprs_rlcmac_dl_tbf::dl_window_stalled() const
750{
751 return m_window.window_stalled();
752}
753
Jacob Erlbeckeceb9102015-03-20 14:41:50 +0100754void gprs_rlcmac_dl_tbf::request_dl_ack()
755{
756 m_dl_ack_requested = true;
757}
758
759bool gprs_rlcmac_dl_tbf::need_control_ts() const
760{
761 if (poll_state != GPRS_RLCMAC_POLL_NONE)
762 return false;
763
764 return state_flags & (1 << GPRS_RLCMAC_FLAG_TO_DL_ACK) ||
765 m_tx_counter >= POLL_ACK_AFTER_FRAMES ||
766 m_dl_ack_requested;
767}
768
769bool gprs_rlcmac_dl_tbf::have_data() const
770{
771 return m_llc.chunk_size() > 0 || !llist_empty(&m_llc.queue);
772}
Jacob Erlbeck005ee7f2015-03-20 14:53:54 +0100773
774int gprs_rlcmac_dl_tbf::frames_since_last_poll(unsigned fn) const
775{
776 unsigned wrapped;
777 if (m_last_dl_poll_fn < 0)
778 return -1;
779
780 wrapped = (fn + 2715648 - m_last_dl_poll_fn) % 2715648;
781 if (wrapped < 2715648/2)
782 return wrapped;
783 else
784 return wrapped - 2715648;
785}
786
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100787int gprs_rlcmac_dl_tbf::frames_since_last_drain(unsigned fn) const
788{
789 unsigned wrapped;
790 if (m_last_dl_drained_fn < 0)
791 return -1;
792
793 wrapped = (fn + 2715648 - m_last_dl_drained_fn) % 2715648;
794 if (wrapped < 2715648/2)
795 return wrapped;
796 else
797 return wrapped - 2715648;
798}
799
800bool gprs_rlcmac_dl_tbf::keep_open(unsigned fn) const
801{
802 int keep_time_frames;
803
804 if (bts_data()->dl_tbf_idle_msec <= 0)
805 return false;
806
807 keep_time_frames = msecs_to_frames(bts_data()->dl_tbf_idle_msec);
808 return frames_since_last_drain(fn) <= keep_time_frames;
809}