blob: 7e0883b566f7bb975178b8bb7750020402036e82 [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);
Jacob Erlbeck502bd1f2015-03-20 14:26:05 +010078 m_last_dl_drained_fn = -1;
Jacob Erlbeckc4952092015-03-24 11:04:19 +010079 bts->llc_frame_sched();
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +010080 /* it is no longer drained */
81 m_last_dl_drained_fn = -1;
Jacob Erlbeckc4952092015-03-24 11:04:19 +010082 tbf_update_ms_class(this, ms_class);
Daniel Willmannca102af2014-08-08 12:14:12 +020083 } else {
84 /* the TBF exists, so we must write it in the queue
85 * we prepend lifetime in front of PDU */
86 struct timeval *tv;
87 struct msgb *llc_msg = msgb_alloc(len + sizeof(*tv) * 2,
88 "llc_pdu_queue");
89 if (!llc_msg)
90 return -ENOMEM;
91 tv = (struct timeval *)msgb_put(llc_msg, sizeof(*tv));
92 gprs_llc::calc_pdu_lifetime(bts, pdu_delay_csec, tv);
93 tv = (struct timeval *)msgb_put(llc_msg, sizeof(*tv));
94 gettimeofday(tv, NULL);
95 memcpy(msgb_put(llc_msg, len), data, len);
96 m_llc.enqueue(llc_msg);
97 tbf_update_ms_class(this, ms_class);
98 }
99
100 return 0;
101}
102
103static struct gprs_rlcmac_dl_tbf *tbf_lookup_dl(BTS *bts,
104 const uint32_t tlli, const char *imsi)
105{
106 /* TODO: look up by IMSI first, then tlli, then old_tlli */
107 return bts->dl_tbf_by_tlli(tlli);
108}
109
110static int tbf_new_dl_assignment(struct gprs_rlcmac_bts *bts,
111 const char *imsi,
112 const uint32_t tlli, const uint8_t ms_class,
113 const uint8_t *data, const uint16_t len)
114{
115 uint8_t trx, ta, ss;
116 int8_t use_trx;
117 struct gprs_rlcmac_ul_tbf *ul_tbf, *old_ul_tbf;
118 struct gprs_rlcmac_dl_tbf *dl_tbf;
119 int8_t tfi; /* must be signed */
120 int rc;
121
122 /* check for uplink data, so we copy our informations */
123#warning "Do the same look up for IMSI, TLLI and OLD_TLLI"
124#warning "Refactor the below lines... into a new method"
125 ul_tbf = bts->bts->ul_tbf_by_tlli(tlli);
126 if (ul_tbf && ul_tbf->m_contention_resolution_done
127 && !ul_tbf->m_final_ack_sent) {
128 use_trx = ul_tbf->trx->trx_no;
129 ta = ul_tbf->ta;
130 ss = 0;
131 old_ul_tbf = ul_tbf;
132 } else {
133 use_trx = -1;
134 /* we already have an uplink TBF, so we use that TA */
135 if (ul_tbf)
136 ta = ul_tbf->ta;
137 else {
138 /* recall TA */
139 rc = bts->bts->timing_advance()->recall(tlli);
140 if (rc < 0) {
141 LOGP(DRLCMAC, LOGL_NOTICE, "TA unknown"
142 ", assuming 0\n");
143 ta = 0;
144 } else
145 ta = rc;
146 }
147 ss = 1; /* PCH assignment only allows one timeslot */
148 old_ul_tbf = NULL;
149 }
150
151 // Create new TBF (any TRX)
152#warning "Copy and paste with alloc_ul_tbf"
153 tfi = bts->bts->tfi_find_free(GPRS_RLCMAC_DL_TBF, &trx, use_trx);
154 if (tfi < 0) {
155 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource\n");
156 /* FIXME: send reject */
157 return -EBUSY;
158 }
159 /* set number of downlink slots according to multislot class */
160 dl_tbf = tbf_alloc_dl_tbf(bts, ul_tbf, tfi, trx, ms_class, ss);
161 if (!dl_tbf) {
162 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource\n");
163 /* FIXME: send reject */
164 return -EBUSY;
165 }
166 dl_tbf->m_tlli = tlli;
167 dl_tbf->m_tlli_valid = 1;
168 dl_tbf->ta = ta;
169
170 LOGP(DRLCMAC, LOGL_DEBUG, "%s [DOWNLINK] START\n", tbf_name(dl_tbf));
171
172 /* new TBF, so put first frame */
173 dl_tbf->m_llc.put_frame(data, len);
174 dl_tbf->bts->llc_frame_sched();
175
176 /* Store IMSI for later look-up and PCH retransmission */
177 dl_tbf->assign_imsi(imsi);
178
179 /* trigger downlink assignment and set state to ASSIGN.
180 * we don't use old_downlink, so the possible uplink is used
181 * to trigger downlink assignment. if there is no uplink,
182 * AGCH is used. */
183 dl_tbf->bts->trigger_dl_ass(dl_tbf, old_ul_tbf, imsi);
184 return 0;
185}
186
187/**
188 * TODO: split into unit test-able parts...
189 */
190int gprs_rlcmac_dl_tbf::handle(struct gprs_rlcmac_bts *bts,
191 const uint32_t tlli, const char *imsi,
192 const uint8_t ms_class, const uint16_t delay_csec,
193 const uint8_t *data, const uint16_t len)
194{
195 struct gprs_rlcmac_dl_tbf *dl_tbf;
196
197 /* check for existing TBF */
198 dl_tbf = tbf_lookup_dl(bts->bts, tlli, imsi);
199 if (dl_tbf) {
200 int rc = dl_tbf->append_data(ms_class, delay_csec, data, len);
201 if (rc >= 0)
202 dl_tbf->assign_imsi(imsi);
203 return rc;
204 }
205
206 return tbf_new_dl_assignment(bts, imsi, tlli, ms_class, data, len);
207}
208
209struct msgb *gprs_rlcmac_dl_tbf::llc_dequeue(bssgp_bvc_ctx *bctx)
210{
211 struct msgb *msg;
212 struct timeval *tv, tv_now;
213 uint32_t octets = 0, frames = 0;
214
215 gettimeofday(&tv_now, NULL);
216
217 while ((msg = m_llc.dequeue())) {
218 tv = (struct timeval *)msg->data;
219 msgb_pull(msg, sizeof(*tv));
220 msgb_pull(msg, sizeof(*tv));
221
222 if (gprs_llc::is_frame_expired(&tv_now, tv)) {
223 LOGP(DRLCMACDL, LOGL_NOTICE, "%s Discarding LLC PDU "
224 "because lifetime limit reached. Queue size %zu\n",
225 tbf_name(this), m_llc.m_queue_size);
226 bts->llc_timedout_frame();
227 frames++;
228 octets += msg->len;
229 msgb_free(msg);
230 continue;
231 }
232 break;
233 }
234
235 if (frames) {
236 if (frames > 0xff)
237 frames = 0xff;
238 if (octets > 0xffffff)
239 octets = 0xffffff;
240 bssgp_tx_llc_discarded(bctx, m_tlli, frames, octets);
241 }
242
243 return msg;
244}
245
246/*
247 * Create DL data block
248 * The messages are fragmented and forwarded as data blocks.
249 */
250struct msgb *gprs_rlcmac_dl_tbf::create_dl_acked_block(uint32_t fn, uint8_t ts)
251{
252 LOGP(DRLCMACDL, LOGL_DEBUG, "%s downlink (V(A)==%d .. "
253 "V(S)==%d)\n", tbf_name(this),
254 m_window.v_a(), m_window.v_s());
255
256do_resend:
257 /* check if there is a block with negative acknowledgement */
258 int resend_bsn = m_window.resend_needed();
259 if (resend_bsn >= 0) {
260 LOGP(DRLCMACDL, LOGL_DEBUG, "- Resending BSN %d\n", resend_bsn);
261 /* re-send block with negative aknowlegement */
262 m_window.m_v_b.mark_unacked(resend_bsn);
263 bts->rlc_resent();
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100264 return create_dl_acked_block(fn, ts, resend_bsn);
Daniel Willmannca102af2014-08-08 12:14:12 +0200265 }
266
267 /* if the window has stalled, or transfer is complete,
268 * send an unacknowledged block */
Jacob Erlbeck95340242015-03-19 13:22:07 +0100269 if (state_is(GPRS_RLCMAC_FINISHED)) {
270 LOGP(DRLCMACDL, LOGL_DEBUG, "- Restarting at BSN %d, "
271 "because all blocks have been transmitted.\n",
272 m_window.v_a());
273 bts->rlc_restarted();
274 } else if (dl_window_stalled()) {
275 LOGP(DRLCMACDL, LOGL_NOTICE, "- Restarting at BSN %d, "
276 "because all window is stalled.\n",
277 m_window.v_a());
278 bts->rlc_stalled();
Jacob Erlbecke25b5b92015-03-19 14:21:33 +0100279 } else if (have_data()) {
280 /* New blocks may be send */
281 return create_new_bsn(fn, ts);
282 } else if (!m_window.window_empty()) {
283 LOGP(DRLCMACDL, LOGL_DEBUG, "- Restarting at BSN %d, "
284 "because all blocks have been transmitted (FLOW).\n",
285 m_window.v_a());
286 bts->rlc_restarted();
Jacob Erlbeck95340242015-03-19 13:22:07 +0100287 } else {
Jacob Erlbecke25b5b92015-03-19 14:21:33 +0100288 /* Nothing left to send, create dummy LLC commands */
Jacob Erlbeck95340242015-03-19 13:22:07 +0100289 return create_new_bsn(fn, ts);
Daniel Willmannca102af2014-08-08 12:14:12 +0200290 }
291
Jacob Erlbeck95340242015-03-19 13:22:07 +0100292 /* If V(S) == V(A) and finished state, we would have received
293 * acknowledgement of all transmitted block. In this case we
294 * would have transmitted the final block, and received ack
295 * from MS. But in this case we did not receive the final ack
296 * indication from MS. This should never happen if MS works
297 * correctly. */
298 if (m_window.window_empty()) {
299 LOGP(DRLCMACDL, LOGL_DEBUG, "- MS acked all blocks, "
300 "so we re-transmit final block!\n");
301 /* we just send final block again */
302 int16_t index = m_window.v_s_mod(-1);
303 bts->rlc_resent();
304 return create_dl_acked_block(fn, ts, index);
305 }
306
307 /* cycle through all unacked blocks */
308 int resend = m_window.mark_for_resend();
309
310 /* At this point there should be at least one unacked block
311 * to be resent. If not, this is an software error. */
312 if (resend == 0) {
313 LOGP(DRLCMACDL, LOGL_ERROR, "Software error: "
314 "There are no unacknowledged blocks, but V(A) "
315 " != V(S). PLEASE FIX!\n");
316 /* we just send final block again */
317 int16_t index = m_window.v_s_mod(-1);
318 return create_dl_acked_block(fn, ts, index);
319 }
320 goto do_resend;
Daniel Willmannca102af2014-08-08 12:14:12 +0200321}
322
323struct msgb *gprs_rlcmac_dl_tbf::create_new_bsn(const uint32_t fn, const uint8_t ts)
324{
325 struct rlc_dl_header *rh;
326 struct rlc_li_field *li;
327 struct msgb *msg;
328 uint8_t *delimiter, *data, *e_pointer;
329 uint16_t space, chunk;
330 gprs_rlc_data *rlc_data;
Daniel Willmannca102af2014-08-08 12:14:12 +0200331 const uint16_t bsn = m_window.v_s();
332
333 LOGP(DRLCMACDL, LOGL_DEBUG, "- Sending new block at BSN %d\n",
334 m_window.v_s());
335
336#warning "Selection of the CS doesn't belong here"
337 if (cs == 0) {
338 cs = bts_data()->initial_cs_dl;
339 if (cs < 1 || cs > 4)
340 cs = 1;
341 }
342 /* total length of block, including spare bits */
343 const uint8_t block_length = gprs_rlcmac_cs[cs].block_length;
344 /* length of usable data of block, w/o spare bits, inc. MAC */
345 const uint8_t block_data_len = gprs_rlcmac_cs[cs].block_data;
346
347 /* now we still have untransmitted LLC data, so we fill mac block */
348 rlc_data = m_rlc.block(bsn);
349 data = rlc_data->prepare(block_data_len);
350
351 rh = (struct rlc_dl_header *)data;
352 rh->pt = 0; /* Data Block */
353 rh->rrbp = rh->s_p = 0; /* Polling, set later, if required */
354 rh->usf = 7; /* will be set at scheduler */
355 rh->pr = 0; /* FIXME: power reduction */
356 rh->tfi = m_tfi; /* TFI */
357 rh->fbi = 0; /* Final Block Indicator, set late, if true */
358 rh->bsn = bsn; /* Block Sequence Number */
359 rh->e = 0; /* Extension bit, maybe set later */
360 e_pointer = data + 2; /* points to E of current chunk */
361 data += sizeof(*rh);
362 delimiter = data; /* where next length header would be stored */
363 space = block_data_len - sizeof(*rh);
364 while (1) {
Jacob Erlbeckcbb1e702015-03-25 12:21:55 +0100365 if (m_llc.frame_length() == 0) {
366 /* A header will need to by added, so we just need
367 * space-1 octets */
368 m_llc.put_dummy_frame(space - 1);
369
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100370 /* The data just drained, store the current fn */
371 if (m_last_dl_drained_fn < 0)
372 m_last_dl_drained_fn = fn;
373
Jacob Erlbeckcbb1e702015-03-25 12:21:55 +0100374 /* It is not clear, when the next real data will
375 * arrive, so request a DL ack/nack now */
376 request_dl_ack();
377
378 LOGP(DRLCMACDL, LOGL_DEBUG,
379 "-- Empty chunk, "
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100380 "added LLC dummy command of size %d, "
381 "drained_since=%d\n",
382 m_llc.frame_length(), frames_since_last_drain(fn));
Jacob Erlbeckcbb1e702015-03-25 12:21:55 +0100383 }
384
Daniel Willmannca102af2014-08-08 12:14:12 +0200385 chunk = m_llc.chunk_size();
Jacob Erlbeckcbb1e702015-03-25 12:21:55 +0100386
Daniel Willmannca102af2014-08-08 12:14:12 +0200387 /* if chunk will exceed block limit */
388 if (chunk > space) {
389 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d "
390 "larger than space (%d) left in block: copy "
391 "only remaining space, and we are done\n",
392 chunk, space);
393 /* block is filled, so there is no extension */
394 *e_pointer |= 0x01;
395 /* fill only space */
396 m_llc.consume(data, space);
397 /* return data block as message */
398 break;
399 }
400 /* if FINAL chunk would fit precisely in space left */
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100401 if (chunk == space && llist_empty(&m_llc.queue) && !keep_open(fn))
402 {
Daniel Willmannca102af2014-08-08 12:14:12 +0200403 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d "
404 "would exactly fit into space (%d): because "
405 "this is a final block, we don't add length "
406 "header, and we are done\n", chunk, space);
407 LOGP(DRLCMACDL, LOGL_INFO, "Complete DL frame for "
408 "%s that fits precisely in last block: "
409 "len=%d\n", tbf_name(this), m_llc.frame_length());
410 gprs_rlcmac_dl_bw(this, m_llc.frame_length());
411 /* block is filled, so there is no extension */
412 *e_pointer |= 0x01;
413 /* fill space */
414 m_llc.consume(data, space);
415 m_llc.reset();
416 /* final block */
417 rh->fbi = 1; /* we indicate final block */
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100418 request_dl_ack();
Daniel Willmannca102af2014-08-08 12:14:12 +0200419 set_state(GPRS_RLCMAC_FINISHED);
420 /* return data block as message */
421 break;
422 }
423 /* if chunk would fit exactly in space left */
424 if (chunk == space) {
425 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d "
426 "would exactly fit into space (%d): add length "
427 "header with LI=0, to make frame extend to "
428 "next block, and we are done\n", chunk, space);
429 /* make space for delimiter */
430 if (delimiter != data)
431 memmove(delimiter + 1, delimiter,
432 data - delimiter);
433 data++;
434 space--;
435 /* add LI with 0 length */
436 li = (struct rlc_li_field *)delimiter;
437 li->e = 1; /* not more extension */
438 li->m = 0; /* shall be set to 0, in case of li = 0 */
439 li->li = 0; /* chunk fills the complete space */
440 // no need to set e_pointer nor increase delimiter
441 /* fill only space, which is 1 octet less than chunk */
442 m_llc.consume(data, space);
443 /* return data block as message */
444 break;
445 }
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100446
Daniel Willmannca102af2014-08-08 12:14:12 +0200447 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d is less "
448 "than remaining space (%d): add length header to "
449 "to delimit LLC frame\n", chunk, space);
450 /* the LLC frame chunk ends in this block */
451 /* make space for delimiter */
452 if (delimiter != data)
453 memmove(delimiter + 1, delimiter, data - delimiter);
454 data++;
455 space--;
456 /* add LI to delimit frame */
457 li = (struct rlc_li_field *)delimiter;
458 li->e = 0; /* Extension bit, maybe set later */
459 li->m = 0; /* will be set later, if there is more LLC data */
460 li->li = chunk; /* length of chunk */
461 e_pointer = delimiter; /* points to E of current delimiter */
462 delimiter++;
463 /* copy (rest of) LLC frame to space and reset later */
464 m_llc.consume(data, chunk);
465 data += chunk;
466 space -= chunk;
467 LOGP(DRLCMACDL, LOGL_INFO, "Complete DL frame for %s"
468 "len=%d\n", tbf_name(this), m_llc.frame_length());
469 gprs_rlcmac_dl_bw(this, m_llc.frame_length());
470 m_llc.reset();
471 /* dequeue next LLC frame, if any */
472 msg = llc_dequeue(gprs_bssgp_pcu_current_bctx());
473 if (msg) {
474 LOGP(DRLCMACDL, LOGL_INFO, "- Dequeue next LLC for "
475 "%s (len=%d)\n", tbf_name(this), msg->len);
476 m_llc.put_frame(msg->data, msg->len);
477 bts->llc_frame_sched();
478 msgb_free(msg);
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100479 m_last_dl_drained_fn = -1;
Daniel Willmannca102af2014-08-08 12:14:12 +0200480 }
481 /* if we have more data and we have space left */
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100482 if (space > 0 && (m_llc.frame_length() || keep_open(fn))) {
Daniel Willmannca102af2014-08-08 12:14:12 +0200483 li->m = 1; /* we indicate more frames to follow */
484 continue;
485 }
486 /* if we don't have more LLC frames */
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100487 if (!m_llc.frame_length() && !keep_open(fn)) {
Daniel Willmannca102af2014-08-08 12:14:12 +0200488 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Final block, so we "
489 "done.\n");
490 li->e = 1; /* we cannot extend */
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100491
Daniel Willmannca102af2014-08-08 12:14:12 +0200492 rh->fbi = 1; /* we indicate final block */
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100493 request_dl_ack();
Daniel Willmannca102af2014-08-08 12:14:12 +0200494 set_state(GPRS_RLCMAC_FINISHED);
495 break;
496 }
497 /* we have no space left */
498 LOGP(DRLCMACDL, LOGL_DEBUG, "-- No space left, so we are "
499 "done.\n");
500 li->e = 1; /* we cannot extend */
501 break;
502 }
503 LOGP(DRLCMACDL, LOGL_DEBUG, "data block: %s\n",
504 osmo_hexdump(rlc_data->block, block_length));
505#warning "move this up?"
506 rlc_data->len = block_length;
507 /* raise send state and set ack state array */
508 m_window.m_v_b.mark_unacked(bsn);
509 m_window.increment_send();
510
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100511 return create_dl_acked_block(fn, ts, bsn);
512}
513
Daniel Willmannca102af2014-08-08 12:14:12 +0200514struct msgb *gprs_rlcmac_dl_tbf::create_dl_acked_block(
515 const uint32_t fn, const uint8_t ts,
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100516 const int index)
Daniel Willmannca102af2014-08-08 12:14:12 +0200517{
518 uint8_t *data;
519 struct rlc_dl_header *rh;
520 struct msgb *dl_msg;
521 uint8_t len;
Daniel Willmannefd5dbb2014-08-25 16:20:23 +0200522 bool need_poll;
Daniel Willmannca102af2014-08-08 12:14:12 +0200523
524 /* get data and header from current block */
525 data = m_rlc.block(index)->block;
526 len = m_rlc.block(index)->len;
527 rh = (struct rlc_dl_header *)data;
528
Jacob Erlbeck005ee7f2015-03-20 14:53:54 +0100529 /* If the TBF has just started, relate frames_since_last_poll to the
530 * current fn */
531 if (m_last_dl_poll_fn < 0)
532 m_last_dl_poll_fn = fn;
533
Daniel Willmannefd5dbb2014-08-25 16:20:23 +0200534 need_poll = state_flags & (1 << GPRS_RLCMAC_FLAG_TO_DL_ACK);
Daniel Willmannca102af2014-08-08 12:14:12 +0200535 /* Clear Polling, if still set in history buffer */
536 rh->s_p = 0;
537
538 /* poll after POLL_ACK_AFTER_FRAMES frames, or when final block is tx.
539 */
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100540 if (m_tx_counter >= POLL_ACK_AFTER_FRAMES || m_dl_ack_requested ||
Daniel Willmannefd5dbb2014-08-25 16:20:23 +0200541 need_poll) {
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100542 if (m_dl_ack_requested) {
Daniel Willmannca102af2014-08-08 12:14:12 +0200543 LOGP(DRLCMACDL, LOGL_DEBUG, "- Scheduling Ack/Nack "
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100544 "polling, because is was requested explicitly "
545 "(e.g. first final block sent).\n");
Daniel Willmannefd5dbb2014-08-25 16:20:23 +0200546 } else if (need_poll) {
547 LOGP(DRLCMACDL, LOGL_DEBUG, "- Scheduling Ack/Nack "
Daniel Willmann635d47c2014-09-17 17:58:29 +0200548 "polling, because polling timed out.\n");
Daniel Willmannca102af2014-08-08 12:14:12 +0200549 } else {
550 LOGP(DRLCMACDL, LOGL_DEBUG, "- Scheduling Ack/Nack "
551 "polling, because %d blocks sent.\n",
552 POLL_ACK_AFTER_FRAMES);
553 }
554 /* scheduling not possible, because: */
555 if (poll_state != GPRS_RLCMAC_POLL_NONE)
556 LOGP(DRLCMACDL, LOGL_DEBUG, "Polling is already "
557 "sheduled for %s, so we must wait for "
558 "requesting downlink ack\n", tbf_name(this));
559 else if (control_ts != ts)
560 LOGP(DRLCMACDL, LOGL_DEBUG, "Polling cannot be "
561 "sheduled in this TS %d, waiting for "
562 "TS %d\n", ts, control_ts);
Daniel Willmannca102af2014-08-08 12:14:12 +0200563 else if (bts->sba()->find(trx->trx_no, ts, (fn + 13) % 2715648))
564 LOGP(DRLCMACDL, LOGL_DEBUG, "Polling cannot be "
565 "sheduled, because single block alllocation "
566 "already exists\n");
567 else {
568 LOGP(DRLCMACDL, LOGL_DEBUG, "Polling sheduled in this "
569 "TS %d\n", ts);
570 m_tx_counter = 0;
571 /* start timer whenever we send the final block */
572 if (rh->fbi == 1)
573 tbf_timer_start(this, 3191, bts_data()->t3191, 0);
574
575 /* schedule polling */
576 poll_state = GPRS_RLCMAC_POLL_SCHED;
577 poll_fn = (fn + 13) % 2715648;
578
Daniel Willmannefd5dbb2014-08-25 16:20:23 +0200579 /* Clear poll timeout flag */
580 state_flags &= ~(1 << GPRS_RLCMAC_FLAG_TO_DL_ACK);
581
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100582 /* Clear request flag */
583 m_dl_ack_requested = false;
584
Daniel Willmannca102af2014-08-08 12:14:12 +0200585 /* set polling in header */
586 rh->rrbp = 0; /* N+13 */
587 rh->s_p = 1; /* Polling */
Jacob Erlbeck005ee7f2015-03-20 14:53:54 +0100588
589 m_last_dl_poll_fn = poll_fn;
Daniel Willmannca102af2014-08-08 12:14:12 +0200590 }
591 }
592
593 /* return data block as message */
594 dl_msg = msgb_alloc(len, "rlcmac_dl_data");
595 if (!dl_msg)
596 return NULL;
597
598 /* Increment TX-counter */
599 m_tx_counter++;
600
601 memcpy(msgb_put(dl_msg, len), data, len);
602 bts->rlc_sent();
603
604 return dl_msg;
605}
606
607int gprs_rlcmac_dl_tbf::update_window(const uint8_t ssn, const uint8_t *rbb)
608{
609 int16_t dist; /* must be signed */
610 uint16_t lost = 0, received = 0;
611 char show_rbb[65];
612 char show_v_b[RLC_MAX_SNS + 1];
613 const uint16_t mod_sns = m_window.mod_sns();
614
615 Decoding::extract_rbb(rbb, show_rbb);
616 /* show received array in debug (bit 64..1) */
617 LOGP(DRLCMACDL, LOGL_DEBUG, "- ack: (BSN=%d)\"%s\""
618 "(BSN=%d) R=ACK I=NACK\n", (ssn - 64) & mod_sns,
619 show_rbb, (ssn - 1) & mod_sns);
620
621 /* apply received array to receive state (SSN-64..SSN-1) */
622 /* calculate distance of ssn from V(S) */
623 dist = (m_window.v_s() - ssn) & mod_sns;
624 /* check if distance is less than distance V(A)..V(S) */
625 if (dist >= m_window.distance()) {
626 /* this might happpen, if the downlink assignment
627 * was not received by ms and the ack refers
628 * to previous TBF
629 * FIXME: we should implement polling for
630 * control ack!*/
631 LOGP(DRLCMACDL, LOGL_NOTICE, "- ack range is out of "
632 "V(A)..V(S) range %s Free TBF!\n", tbf_name(this));
633 return 1; /* indicate to free TBF */
634 }
635
636 m_window.update(bts, show_rbb, ssn,
637 &lost, &received);
638
639 /* report lost and received packets */
640 gprs_rlcmac_received_lost(this, received, lost);
641
642 /* raise V(A), if possible */
643 m_window.raise(m_window.move_window());
644
645 /* show receive state array in debug (V(A)..V(S)-1) */
646 m_window.show_state(show_v_b);
647 LOGP(DRLCMACDL, LOGL_DEBUG, "- V(B): (V(A)=%d)\"%s\""
648 "(V(S)-1=%d) A=Acked N=Nacked U=Unacked "
649 "X=Resend-Unacked I=Invalid\n",
650 m_window.v_a(), show_v_b,
651 m_window.v_s_mod(-1));
652
653 if (state_is(GPRS_RLCMAC_FINISHED) && m_window.window_empty()) {
654 LOGP(DRLCMACDL, LOGL_NOTICE, "Received acknowledge of "
655 "all blocks, but without final ack "
656 "inidcation (don't worry)\n");
657 }
658 return 0;
659}
660
661
662int gprs_rlcmac_dl_tbf::maybe_start_new_window()
663{
664 struct msgb *msg;
665 uint16_t received;
666
667 LOGP(DRLCMACDL, LOGL_DEBUG, "- Final ACK received.\n");
668 /* range V(A)..V(S)-1 */
669 received = m_window.count_unacked();
670
671 /* report all outstanding packets as received */
672 gprs_rlcmac_received_lost(this, received, 0);
673
674 set_state(GPRS_RLCMAC_WAIT_RELEASE);
675
676 /* check for LLC PDU in the LLC Queue */
677 msg = llc_dequeue(gprs_bssgp_pcu_current_bctx());
678 if (!msg) {
679 /* no message, start T3193, change state to RELEASE */
680 LOGP(DRLCMACDL, LOGL_DEBUG, "- No new message, so we release.\n");
681 /* start T3193 */
682 tbf_timer_start(this, 3193,
683 bts_data()->t3193_msec / 1000,
684 (bts_data()->t3193_msec % 1000) * 1000);
685
686 return 0;
687 }
688
689 /* we have more data so we will re-use this tbf */
690 reuse_tbf(msg->data, msg->len);
691 msgb_free(msg);
692 return 0;
693}
694
695int gprs_rlcmac_dl_tbf::rcvd_dl_ack(uint8_t final_ack, uint8_t ssn, uint8_t *rbb)
696{
697 LOGP(DRLCMACDL, LOGL_DEBUG, "%s downlink acknowledge\n", tbf_name(this));
698
699 if (!final_ack)
700 return update_window(ssn, rbb);
701 return maybe_start_new_window();
702}
703
704void gprs_rlcmac_dl_tbf::reuse_tbf(const uint8_t *data, const uint16_t len)
705{
Daniel Willmanne4818152014-08-15 16:52:09 +0200706 uint8_t trx;
707 struct gprs_rlcmac_dl_tbf *new_tbf;
708 int8_t tfi; /* must be signed */
Daniel Willmanne4818152014-08-15 16:52:09 +0200709 struct msgb *msg;
710
Daniel Willmannca102af2014-08-08 12:14:12 +0200711 bts->tbf_reused();
Daniel Willmanne4818152014-08-15 16:52:09 +0200712
713 tfi = bts->tfi_find_free(GPRS_RLCMAC_DL_TBF, &trx, this->trx->trx_no);
714 if (tfi < 0) {
715 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource\n");
716 /* FIXME: send reject */
717 return;
718 }
719 new_tbf = tbf_alloc_dl_tbf(bts->bts_data(), NULL, tfi, trx, ms_class, 0);
720 if (!new_tbf) {
721 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource\n");
722 /* FIXME: send reject */
723 return;
724 }
725
726 new_tbf->m_tlli = m_tlli;
727 new_tbf->m_tlli_valid = m_tlli_valid;
728 new_tbf->ta = ta;
729 new_tbf->assign_imsi(m_imsi);
730
731 /* Copy over all data to the new TBF */
732 new_tbf->m_llc.put_frame(data, len);
Daniel Willmannca102af2014-08-08 12:14:12 +0200733 bts->llc_frame_sched();
734
Daniel Willmanne4818152014-08-15 16:52:09 +0200735 while ((msg = m_llc.dequeue()))
736 new_tbf->m_llc.enqueue(msg);
737
Daniel Willmannca102af2014-08-08 12:14:12 +0200738 /* reset rlc states */
739 m_tx_counter = 0;
740 m_wait_confirm = 0;
741 m_window.reset();
742
Jacob Erlbeck297edf72015-02-26 14:59:52 +0100743 /* mark TLLI as invalid */
744 m_tlli_valid = 0;
745
Daniel Willmannca102af2014-08-08 12:14:12 +0200746 /* keep to flags */
747 state_flags &= GPRS_RLCMAC_FLAG_TO_MASK;
748 state_flags &= ~(1 << GPRS_RLCMAC_FLAG_CCCH);
749
750 update();
751
752 LOGP(DRLCMAC, LOGL_DEBUG, "%s Trigger dowlink assignment on PACCH, "
753 "because another LLC PDU has arrived in between\n",
754 tbf_name(this));
Daniel Willmanne4818152014-08-15 16:52:09 +0200755 bts->trigger_dl_ass(new_tbf, this, NULL);
Daniel Willmannca102af2014-08-08 12:14:12 +0200756}
757
758bool gprs_rlcmac_dl_tbf::dl_window_stalled() const
759{
760 return m_window.window_stalled();
761}
762
Jacob Erlbeckeceb9102015-03-20 14:41:50 +0100763void gprs_rlcmac_dl_tbf::request_dl_ack()
764{
765 m_dl_ack_requested = true;
766}
767
768bool gprs_rlcmac_dl_tbf::need_control_ts() const
769{
770 if (poll_state != GPRS_RLCMAC_POLL_NONE)
771 return false;
772
773 return state_flags & (1 << GPRS_RLCMAC_FLAG_TO_DL_ACK) ||
774 m_tx_counter >= POLL_ACK_AFTER_FRAMES ||
775 m_dl_ack_requested;
776}
777
778bool gprs_rlcmac_dl_tbf::have_data() const
779{
780 return m_llc.chunk_size() > 0 || !llist_empty(&m_llc.queue);
781}
Jacob Erlbeck005ee7f2015-03-20 14:53:54 +0100782
783int gprs_rlcmac_dl_tbf::frames_since_last_poll(unsigned fn) const
784{
785 unsigned wrapped;
786 if (m_last_dl_poll_fn < 0)
787 return -1;
788
789 wrapped = (fn + 2715648 - m_last_dl_poll_fn) % 2715648;
790 if (wrapped < 2715648/2)
791 return wrapped;
792 else
793 return wrapped - 2715648;
794}
795
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100796int gprs_rlcmac_dl_tbf::frames_since_last_drain(unsigned fn) const
797{
798 unsigned wrapped;
799 if (m_last_dl_drained_fn < 0)
800 return -1;
801
802 wrapped = (fn + 2715648 - m_last_dl_drained_fn) % 2715648;
803 if (wrapped < 2715648/2)
804 return wrapped;
805 else
806 return wrapped - 2715648;
807}
808
809bool gprs_rlcmac_dl_tbf::keep_open(unsigned fn) const
810{
811 int keep_time_frames;
812
813 if (bts_data()->dl_tbf_idle_msec <= 0)
814 return false;
815
816 keep_time_frames = msecs_to_frames(bts_data()->dl_tbf_idle_msec);
817 return frames_since_last_drain(fn) <= keep_time_frames;
818}