blob: c9ad058cdc564cc6997151cca06f225a2a607695 [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
30extern "C" {
31#include <osmocom/core/msgb.h>
32#include <osmocom/core/talloc.h>
33}
34
35#include <errno.h>
36#include <string.h>
37
38/* After sending these frames, we poll for ack/nack. */
39#define POLL_ACK_AFTER_FRAMES 20
40
41
42static const struct gprs_rlcmac_cs gprs_rlcmac_cs[] = {
43/* frame length data block max payload */
44 { 0, 0, 0 },
45 { 23, 23, 20 }, /* CS-1 */
46 { 34, 33, 30 }, /* CS-2 */
47 { 40, 39, 36 }, /* CS-3 */
48 { 54, 53, 50 }, /* CS-4 */
49};
50
51extern "C" {
52int bssgp_tx_llc_discarded(struct bssgp_bvc_ctx *bctx, uint32_t tlli,
53 uint8_t num_frames, uint32_t num_octets);
54}
55
56static inline void tbf_update_ms_class(struct gprs_rlcmac_tbf *tbf,
57 const uint8_t ms_class)
58{
59 if (!tbf->ms_class && ms_class)
60 tbf->ms_class = ms_class;
61}
62
63int gprs_rlcmac_dl_tbf::append_data(const uint8_t ms_class,
64 const uint16_t pdu_delay_csec,
65 const uint8_t *data, const uint16_t len)
66{
67 LOGP(DRLCMAC, LOGL_INFO, "%s append\n", tbf_name(this));
68 if (state_is(GPRS_RLCMAC_WAIT_RELEASE)) {
69 LOGP(DRLCMAC, LOGL_DEBUG,
70 "%s in WAIT RELEASE state "
71 "(T3193), so reuse TBF\n", tbf_name(this));
72 tbf_update_ms_class(this, ms_class);
73 reuse_tbf(data, len);
74 } else {
75 /* the TBF exists, so we must write it in the queue
76 * we prepend lifetime in front of PDU */
77 struct timeval *tv;
78 struct msgb *llc_msg = msgb_alloc(len + sizeof(*tv) * 2,
79 "llc_pdu_queue");
80 if (!llc_msg)
81 return -ENOMEM;
82 tv = (struct timeval *)msgb_put(llc_msg, sizeof(*tv));
83 gprs_llc::calc_pdu_lifetime(bts, pdu_delay_csec, tv);
84 tv = (struct timeval *)msgb_put(llc_msg, sizeof(*tv));
85 gettimeofday(tv, NULL);
86 memcpy(msgb_put(llc_msg, len), data, len);
87 m_llc.enqueue(llc_msg);
88 tbf_update_ms_class(this, ms_class);
89 }
90
91 return 0;
92}
93
94static struct gprs_rlcmac_dl_tbf *tbf_lookup_dl(BTS *bts,
95 const uint32_t tlli, const char *imsi)
96{
97 /* TODO: look up by IMSI first, then tlli, then old_tlli */
98 return bts->dl_tbf_by_tlli(tlli);
99}
100
101static int tbf_new_dl_assignment(struct gprs_rlcmac_bts *bts,
102 const char *imsi,
103 const uint32_t tlli, const uint8_t ms_class,
104 const uint8_t *data, const uint16_t len)
105{
106 uint8_t trx, ta, ss;
107 int8_t use_trx;
108 struct gprs_rlcmac_ul_tbf *ul_tbf, *old_ul_tbf;
109 struct gprs_rlcmac_dl_tbf *dl_tbf;
110 int8_t tfi; /* must be signed */
111 int rc;
112
113 /* check for uplink data, so we copy our informations */
114#warning "Do the same look up for IMSI, TLLI and OLD_TLLI"
115#warning "Refactor the below lines... into a new method"
116 ul_tbf = bts->bts->ul_tbf_by_tlli(tlli);
117 if (ul_tbf && ul_tbf->m_contention_resolution_done
118 && !ul_tbf->m_final_ack_sent) {
119 use_trx = ul_tbf->trx->trx_no;
120 ta = ul_tbf->ta;
121 ss = 0;
122 old_ul_tbf = ul_tbf;
123 } else {
124 use_trx = -1;
125 /* we already have an uplink TBF, so we use that TA */
126 if (ul_tbf)
127 ta = ul_tbf->ta;
128 else {
129 /* recall TA */
130 rc = bts->bts->timing_advance()->recall(tlli);
131 if (rc < 0) {
132 LOGP(DRLCMAC, LOGL_NOTICE, "TA unknown"
133 ", assuming 0\n");
134 ta = 0;
135 } else
136 ta = rc;
137 }
138 ss = 1; /* PCH assignment only allows one timeslot */
139 old_ul_tbf = NULL;
140 }
141
142 // Create new TBF (any TRX)
143#warning "Copy and paste with alloc_ul_tbf"
144 tfi = bts->bts->tfi_find_free(GPRS_RLCMAC_DL_TBF, &trx, use_trx);
145 if (tfi < 0) {
146 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource\n");
147 /* FIXME: send reject */
148 return -EBUSY;
149 }
150 /* set number of downlink slots according to multislot class */
151 dl_tbf = tbf_alloc_dl_tbf(bts, ul_tbf, tfi, trx, ms_class, ss);
152 if (!dl_tbf) {
153 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource\n");
154 /* FIXME: send reject */
155 return -EBUSY;
156 }
157 dl_tbf->m_tlli = tlli;
158 dl_tbf->m_tlli_valid = 1;
159 dl_tbf->ta = ta;
160
161 LOGP(DRLCMAC, LOGL_DEBUG, "%s [DOWNLINK] START\n", tbf_name(dl_tbf));
162
163 /* new TBF, so put first frame */
164 dl_tbf->m_llc.put_frame(data, len);
165 dl_tbf->bts->llc_frame_sched();
166
167 /* Store IMSI for later look-up and PCH retransmission */
168 dl_tbf->assign_imsi(imsi);
169
170 /* trigger downlink assignment and set state to ASSIGN.
171 * we don't use old_downlink, so the possible uplink is used
172 * to trigger downlink assignment. if there is no uplink,
173 * AGCH is used. */
174 dl_tbf->bts->trigger_dl_ass(dl_tbf, old_ul_tbf, imsi);
175 return 0;
176}
177
178/**
179 * TODO: split into unit test-able parts...
180 */
181int gprs_rlcmac_dl_tbf::handle(struct gprs_rlcmac_bts *bts,
182 const uint32_t tlli, const char *imsi,
183 const uint8_t ms_class, const uint16_t delay_csec,
184 const uint8_t *data, const uint16_t len)
185{
186 struct gprs_rlcmac_dl_tbf *dl_tbf;
187
188 /* check for existing TBF */
189 dl_tbf = tbf_lookup_dl(bts->bts, tlli, imsi);
190 if (dl_tbf) {
191 int rc = dl_tbf->append_data(ms_class, delay_csec, data, len);
192 if (rc >= 0)
193 dl_tbf->assign_imsi(imsi);
194 return rc;
195 }
196
197 return tbf_new_dl_assignment(bts, imsi, tlli, ms_class, data, len);
198}
199
200struct msgb *gprs_rlcmac_dl_tbf::llc_dequeue(bssgp_bvc_ctx *bctx)
201{
202 struct msgb *msg;
203 struct timeval *tv, tv_now;
204 uint32_t octets = 0, frames = 0;
205
206 gettimeofday(&tv_now, NULL);
207
208 while ((msg = m_llc.dequeue())) {
209 tv = (struct timeval *)msg->data;
210 msgb_pull(msg, sizeof(*tv));
211 msgb_pull(msg, sizeof(*tv));
212
213 if (gprs_llc::is_frame_expired(&tv_now, tv)) {
214 LOGP(DRLCMACDL, LOGL_NOTICE, "%s Discarding LLC PDU "
215 "because lifetime limit reached. Queue size %zu\n",
216 tbf_name(this), m_llc.m_queue_size);
217 bts->llc_timedout_frame();
218 frames++;
219 octets += msg->len;
220 msgb_free(msg);
221 continue;
222 }
223 break;
224 }
225
226 if (frames) {
227 if (frames > 0xff)
228 frames = 0xff;
229 if (octets > 0xffffff)
230 octets = 0xffffff;
231 bssgp_tx_llc_discarded(bctx, m_tlli, frames, octets);
232 }
233
234 return msg;
235}
236
237/*
238 * Create DL data block
239 * The messages are fragmented and forwarded as data blocks.
240 */
241struct msgb *gprs_rlcmac_dl_tbf::create_dl_acked_block(uint32_t fn, uint8_t ts)
242{
243 LOGP(DRLCMACDL, LOGL_DEBUG, "%s downlink (V(A)==%d .. "
244 "V(S)==%d)\n", tbf_name(this),
245 m_window.v_a(), m_window.v_s());
246
247do_resend:
248 /* check if there is a block with negative acknowledgement */
249 int resend_bsn = m_window.resend_needed();
250 if (resend_bsn >= 0) {
251 LOGP(DRLCMACDL, LOGL_DEBUG, "- Resending BSN %d\n", resend_bsn);
252 /* re-send block with negative aknowlegement */
253 m_window.m_v_b.mark_unacked(resend_bsn);
254 bts->rlc_resent();
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100255 return create_dl_acked_block(fn, ts, resend_bsn);
Daniel Willmannca102af2014-08-08 12:14:12 +0200256 }
257
258 /* if the window has stalled, or transfer is complete,
259 * send an unacknowledged block */
Jacob Erlbeck95340242015-03-19 13:22:07 +0100260 if (state_is(GPRS_RLCMAC_FINISHED)) {
261 LOGP(DRLCMACDL, LOGL_DEBUG, "- Restarting at BSN %d, "
262 "because all blocks have been transmitted.\n",
263 m_window.v_a());
264 bts->rlc_restarted();
265 } else if (dl_window_stalled()) {
266 LOGP(DRLCMACDL, LOGL_NOTICE, "- Restarting at BSN %d, "
267 "because all window is stalled.\n",
268 m_window.v_a());
269 bts->rlc_stalled();
270 } else {
271 /* No blocks are left */
272 return create_new_bsn(fn, ts);
Daniel Willmannca102af2014-08-08 12:14:12 +0200273 }
274
Jacob Erlbeck95340242015-03-19 13:22:07 +0100275 /* If V(S) == V(A) and finished state, we would have received
276 * acknowledgement of all transmitted block. In this case we
277 * would have transmitted the final block, and received ack
278 * from MS. But in this case we did not receive the final ack
279 * indication from MS. This should never happen if MS works
280 * correctly. */
281 if (m_window.window_empty()) {
282 LOGP(DRLCMACDL, LOGL_DEBUG, "- MS acked all blocks, "
283 "so we re-transmit final block!\n");
284 /* we just send final block again */
285 int16_t index = m_window.v_s_mod(-1);
286 bts->rlc_resent();
287 return create_dl_acked_block(fn, ts, index);
288 }
289
290 /* cycle through all unacked blocks */
291 int resend = m_window.mark_for_resend();
292
293 /* At this point there should be at least one unacked block
294 * to be resent. If not, this is an software error. */
295 if (resend == 0) {
296 LOGP(DRLCMACDL, LOGL_ERROR, "Software error: "
297 "There are no unacknowledged blocks, but V(A) "
298 " != V(S). PLEASE FIX!\n");
299 /* we just send final block again */
300 int16_t index = m_window.v_s_mod(-1);
301 return create_dl_acked_block(fn, ts, index);
302 }
303 goto do_resend;
Daniel Willmannca102af2014-08-08 12:14:12 +0200304}
305
306struct msgb *gprs_rlcmac_dl_tbf::create_new_bsn(const uint32_t fn, const uint8_t ts)
307{
308 struct rlc_dl_header *rh;
309 struct rlc_li_field *li;
310 struct msgb *msg;
311 uint8_t *delimiter, *data, *e_pointer;
312 uint16_t space, chunk;
313 gprs_rlc_data *rlc_data;
Daniel Willmannca102af2014-08-08 12:14:12 +0200314 const uint16_t bsn = m_window.v_s();
315
316 LOGP(DRLCMACDL, LOGL_DEBUG, "- Sending new block at BSN %d\n",
317 m_window.v_s());
318
319#warning "Selection of the CS doesn't belong here"
320 if (cs == 0) {
321 cs = bts_data()->initial_cs_dl;
322 if (cs < 1 || cs > 4)
323 cs = 1;
324 }
325 /* total length of block, including spare bits */
326 const uint8_t block_length = gprs_rlcmac_cs[cs].block_length;
327 /* length of usable data of block, w/o spare bits, inc. MAC */
328 const uint8_t block_data_len = gprs_rlcmac_cs[cs].block_data;
329
330 /* now we still have untransmitted LLC data, so we fill mac block */
331 rlc_data = m_rlc.block(bsn);
332 data = rlc_data->prepare(block_data_len);
333
334 rh = (struct rlc_dl_header *)data;
335 rh->pt = 0; /* Data Block */
336 rh->rrbp = rh->s_p = 0; /* Polling, set later, if required */
337 rh->usf = 7; /* will be set at scheduler */
338 rh->pr = 0; /* FIXME: power reduction */
339 rh->tfi = m_tfi; /* TFI */
340 rh->fbi = 0; /* Final Block Indicator, set late, if true */
341 rh->bsn = bsn; /* Block Sequence Number */
342 rh->e = 0; /* Extension bit, maybe set later */
343 e_pointer = data + 2; /* points to E of current chunk */
344 data += sizeof(*rh);
345 delimiter = data; /* where next length header would be stored */
346 space = block_data_len - sizeof(*rh);
347 while (1) {
348 chunk = m_llc.chunk_size();
349 /* if chunk will exceed block limit */
350 if (chunk > space) {
351 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d "
352 "larger than space (%d) left in block: copy "
353 "only remaining space, and we are done\n",
354 chunk, space);
355 /* block is filled, so there is no extension */
356 *e_pointer |= 0x01;
357 /* fill only space */
358 m_llc.consume(data, space);
359 /* return data block as message */
360 break;
361 }
362 /* if FINAL chunk would fit precisely in space left */
363 if (chunk == space && llist_empty(&m_llc.queue)) {
364 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d "
365 "would exactly fit into space (%d): because "
366 "this is a final block, we don't add length "
367 "header, and we are done\n", chunk, space);
368 LOGP(DRLCMACDL, LOGL_INFO, "Complete DL frame for "
369 "%s that fits precisely in last block: "
370 "len=%d\n", tbf_name(this), m_llc.frame_length());
371 gprs_rlcmac_dl_bw(this, m_llc.frame_length());
372 /* block is filled, so there is no extension */
373 *e_pointer |= 0x01;
374 /* fill space */
375 m_llc.consume(data, space);
376 m_llc.reset();
377 /* final block */
378 rh->fbi = 1; /* we indicate final block */
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100379 request_dl_ack();
Daniel Willmannca102af2014-08-08 12:14:12 +0200380 set_state(GPRS_RLCMAC_FINISHED);
381 /* return data block as message */
382 break;
383 }
384 /* if chunk would fit exactly in space left */
385 if (chunk == space) {
386 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d "
387 "would exactly fit into space (%d): add length "
388 "header with LI=0, to make frame extend to "
389 "next block, and we are done\n", chunk, space);
390 /* make space for delimiter */
391 if (delimiter != data)
392 memmove(delimiter + 1, delimiter,
393 data - delimiter);
394 data++;
395 space--;
396 /* add LI with 0 length */
397 li = (struct rlc_li_field *)delimiter;
398 li->e = 1; /* not more extension */
399 li->m = 0; /* shall be set to 0, in case of li = 0 */
400 li->li = 0; /* chunk fills the complete space */
401 // no need to set e_pointer nor increase delimiter
402 /* fill only space, which is 1 octet less than chunk */
403 m_llc.consume(data, space);
404 /* return data block as message */
405 break;
406 }
407 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d is less "
408 "than remaining space (%d): add length header to "
409 "to delimit LLC frame\n", chunk, space);
410 /* the LLC frame chunk ends in this block */
411 /* make space for delimiter */
412 if (delimiter != data)
413 memmove(delimiter + 1, delimiter, data - delimiter);
414 data++;
415 space--;
416 /* add LI to delimit frame */
417 li = (struct rlc_li_field *)delimiter;
418 li->e = 0; /* Extension bit, maybe set later */
419 li->m = 0; /* will be set later, if there is more LLC data */
420 li->li = chunk; /* length of chunk */
421 e_pointer = delimiter; /* points to E of current delimiter */
422 delimiter++;
423 /* copy (rest of) LLC frame to space and reset later */
424 m_llc.consume(data, chunk);
425 data += chunk;
426 space -= chunk;
427 LOGP(DRLCMACDL, LOGL_INFO, "Complete DL frame for %s"
428 "len=%d\n", tbf_name(this), m_llc.frame_length());
429 gprs_rlcmac_dl_bw(this, m_llc.frame_length());
430 m_llc.reset();
431 /* dequeue next LLC frame, if any */
432 msg = llc_dequeue(gprs_bssgp_pcu_current_bctx());
433 if (msg) {
434 LOGP(DRLCMACDL, LOGL_INFO, "- Dequeue next LLC for "
435 "%s (len=%d)\n", tbf_name(this), msg->len);
436 m_llc.put_frame(msg->data, msg->len);
437 bts->llc_frame_sched();
438 msgb_free(msg);
439 }
440 /* if we have more data and we have space left */
441 if (space > 0 && m_llc.frame_length()) {
442 li->m = 1; /* we indicate more frames to follow */
443 continue;
444 }
445 /* if we don't have more LLC frames */
446 if (!m_llc.frame_length()) {
447 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Final block, so we "
448 "done.\n");
449 li->e = 1; /* we cannot extend */
450 rh->fbi = 1; /* we indicate final block */
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100451 request_dl_ack();
Daniel Willmannca102af2014-08-08 12:14:12 +0200452 set_state(GPRS_RLCMAC_FINISHED);
453 break;
454 }
455 /* we have no space left */
456 LOGP(DRLCMACDL, LOGL_DEBUG, "-- No space left, so we are "
457 "done.\n");
458 li->e = 1; /* we cannot extend */
459 break;
460 }
461 LOGP(DRLCMACDL, LOGL_DEBUG, "data block: %s\n",
462 osmo_hexdump(rlc_data->block, block_length));
463#warning "move this up?"
464 rlc_data->len = block_length;
465 /* raise send state and set ack state array */
466 m_window.m_v_b.mark_unacked(bsn);
467 m_window.increment_send();
468
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100469 return create_dl_acked_block(fn, ts, bsn);
470}
471
Daniel Willmannca102af2014-08-08 12:14:12 +0200472struct msgb *gprs_rlcmac_dl_tbf::create_dl_acked_block(
473 const uint32_t fn, const uint8_t ts,
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100474 const int index)
Daniel Willmannca102af2014-08-08 12:14:12 +0200475{
476 uint8_t *data;
477 struct rlc_dl_header *rh;
478 struct msgb *dl_msg;
479 uint8_t len;
Daniel Willmannefd5dbb2014-08-25 16:20:23 +0200480 bool need_poll;
Daniel Willmannca102af2014-08-08 12:14:12 +0200481
482 /* get data and header from current block */
483 data = m_rlc.block(index)->block;
484 len = m_rlc.block(index)->len;
485 rh = (struct rlc_dl_header *)data;
486
Daniel Willmannefd5dbb2014-08-25 16:20:23 +0200487 need_poll = state_flags & (1 << GPRS_RLCMAC_FLAG_TO_DL_ACK);
Daniel Willmannca102af2014-08-08 12:14:12 +0200488 /* Clear Polling, if still set in history buffer */
489 rh->s_p = 0;
490
491 /* poll after POLL_ACK_AFTER_FRAMES frames, or when final block is tx.
492 */
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100493 if (m_tx_counter >= POLL_ACK_AFTER_FRAMES || m_dl_ack_requested ||
Daniel Willmannefd5dbb2014-08-25 16:20:23 +0200494 need_poll) {
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100495 if (m_dl_ack_requested) {
Daniel Willmannca102af2014-08-08 12:14:12 +0200496 LOGP(DRLCMACDL, LOGL_DEBUG, "- Scheduling Ack/Nack "
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100497 "polling, because is was requested explicitly "
498 "(e.g. first final block sent).\n");
Daniel Willmannefd5dbb2014-08-25 16:20:23 +0200499 } else if (need_poll) {
500 LOGP(DRLCMACDL, LOGL_DEBUG, "- Scheduling Ack/Nack "
Daniel Willmann635d47c2014-09-17 17:58:29 +0200501 "polling, because polling timed out.\n");
Daniel Willmannca102af2014-08-08 12:14:12 +0200502 } else {
503 LOGP(DRLCMACDL, LOGL_DEBUG, "- Scheduling Ack/Nack "
504 "polling, because %d blocks sent.\n",
505 POLL_ACK_AFTER_FRAMES);
506 }
507 /* scheduling not possible, because: */
508 if (poll_state != GPRS_RLCMAC_POLL_NONE)
509 LOGP(DRLCMACDL, LOGL_DEBUG, "Polling is already "
510 "sheduled for %s, so we must wait for "
511 "requesting downlink ack\n", tbf_name(this));
512 else if (control_ts != ts)
513 LOGP(DRLCMACDL, LOGL_DEBUG, "Polling cannot be "
514 "sheduled in this TS %d, waiting for "
515 "TS %d\n", ts, control_ts);
Daniel Willmannca102af2014-08-08 12:14:12 +0200516 else if (bts->sba()->find(trx->trx_no, ts, (fn + 13) % 2715648))
517 LOGP(DRLCMACDL, LOGL_DEBUG, "Polling cannot be "
518 "sheduled, because single block alllocation "
519 "already exists\n");
520 else {
521 LOGP(DRLCMACDL, LOGL_DEBUG, "Polling sheduled in this "
522 "TS %d\n", ts);
523 m_tx_counter = 0;
524 /* start timer whenever we send the final block */
525 if (rh->fbi == 1)
526 tbf_timer_start(this, 3191, bts_data()->t3191, 0);
527
528 /* schedule polling */
529 poll_state = GPRS_RLCMAC_POLL_SCHED;
530 poll_fn = (fn + 13) % 2715648;
531
Daniel Willmannefd5dbb2014-08-25 16:20:23 +0200532 /* Clear poll timeout flag */
533 state_flags &= ~(1 << GPRS_RLCMAC_FLAG_TO_DL_ACK);
534
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100535 /* Clear request flag */
536 m_dl_ack_requested = false;
537
Daniel Willmannca102af2014-08-08 12:14:12 +0200538 /* set polling in header */
539 rh->rrbp = 0; /* N+13 */
540 rh->s_p = 1; /* Polling */
541 }
542 }
543
544 /* return data block as message */
545 dl_msg = msgb_alloc(len, "rlcmac_dl_data");
546 if (!dl_msg)
547 return NULL;
548
549 /* Increment TX-counter */
550 m_tx_counter++;
551
552 memcpy(msgb_put(dl_msg, len), data, len);
553 bts->rlc_sent();
554
555 return dl_msg;
556}
557
558int gprs_rlcmac_dl_tbf::update_window(const uint8_t ssn, const uint8_t *rbb)
559{
560 int16_t dist; /* must be signed */
561 uint16_t lost = 0, received = 0;
562 char show_rbb[65];
563 char show_v_b[RLC_MAX_SNS + 1];
564 const uint16_t mod_sns = m_window.mod_sns();
565
566 Decoding::extract_rbb(rbb, show_rbb);
567 /* show received array in debug (bit 64..1) */
568 LOGP(DRLCMACDL, LOGL_DEBUG, "- ack: (BSN=%d)\"%s\""
569 "(BSN=%d) R=ACK I=NACK\n", (ssn - 64) & mod_sns,
570 show_rbb, (ssn - 1) & mod_sns);
571
572 /* apply received array to receive state (SSN-64..SSN-1) */
573 /* calculate distance of ssn from V(S) */
574 dist = (m_window.v_s() - ssn) & mod_sns;
575 /* check if distance is less than distance V(A)..V(S) */
576 if (dist >= m_window.distance()) {
577 /* this might happpen, if the downlink assignment
578 * was not received by ms and the ack refers
579 * to previous TBF
580 * FIXME: we should implement polling for
581 * control ack!*/
582 LOGP(DRLCMACDL, LOGL_NOTICE, "- ack range is out of "
583 "V(A)..V(S) range %s Free TBF!\n", tbf_name(this));
584 return 1; /* indicate to free TBF */
585 }
586
587 m_window.update(bts, show_rbb, ssn,
588 &lost, &received);
589
590 /* report lost and received packets */
591 gprs_rlcmac_received_lost(this, received, lost);
592
593 /* raise V(A), if possible */
594 m_window.raise(m_window.move_window());
595
596 /* show receive state array in debug (V(A)..V(S)-1) */
597 m_window.show_state(show_v_b);
598 LOGP(DRLCMACDL, LOGL_DEBUG, "- V(B): (V(A)=%d)\"%s\""
599 "(V(S)-1=%d) A=Acked N=Nacked U=Unacked "
600 "X=Resend-Unacked I=Invalid\n",
601 m_window.v_a(), show_v_b,
602 m_window.v_s_mod(-1));
603
604 if (state_is(GPRS_RLCMAC_FINISHED) && m_window.window_empty()) {
605 LOGP(DRLCMACDL, LOGL_NOTICE, "Received acknowledge of "
606 "all blocks, but without final ack "
607 "inidcation (don't worry)\n");
608 }
609 return 0;
610}
611
612
613int gprs_rlcmac_dl_tbf::maybe_start_new_window()
614{
615 struct msgb *msg;
616 uint16_t received;
617
618 LOGP(DRLCMACDL, LOGL_DEBUG, "- Final ACK received.\n");
619 /* range V(A)..V(S)-1 */
620 received = m_window.count_unacked();
621
622 /* report all outstanding packets as received */
623 gprs_rlcmac_received_lost(this, received, 0);
624
625 set_state(GPRS_RLCMAC_WAIT_RELEASE);
626
627 /* check for LLC PDU in the LLC Queue */
628 msg = llc_dequeue(gprs_bssgp_pcu_current_bctx());
629 if (!msg) {
630 /* no message, start T3193, change state to RELEASE */
631 LOGP(DRLCMACDL, LOGL_DEBUG, "- No new message, so we release.\n");
632 /* start T3193 */
633 tbf_timer_start(this, 3193,
634 bts_data()->t3193_msec / 1000,
635 (bts_data()->t3193_msec % 1000) * 1000);
636
637 return 0;
638 }
639
640 /* we have more data so we will re-use this tbf */
641 reuse_tbf(msg->data, msg->len);
642 msgb_free(msg);
643 return 0;
644}
645
646int gprs_rlcmac_dl_tbf::rcvd_dl_ack(uint8_t final_ack, uint8_t ssn, uint8_t *rbb)
647{
648 LOGP(DRLCMACDL, LOGL_DEBUG, "%s downlink acknowledge\n", tbf_name(this));
649
650 if (!final_ack)
651 return update_window(ssn, rbb);
652 return maybe_start_new_window();
653}
654
655void gprs_rlcmac_dl_tbf::reuse_tbf(const uint8_t *data, const uint16_t len)
656{
Daniel Willmanne4818152014-08-15 16:52:09 +0200657 uint8_t trx;
658 struct gprs_rlcmac_dl_tbf *new_tbf;
659 int8_t tfi; /* must be signed */
Daniel Willmanne4818152014-08-15 16:52:09 +0200660 struct msgb *msg;
661
Daniel Willmannca102af2014-08-08 12:14:12 +0200662 bts->tbf_reused();
Daniel Willmanne4818152014-08-15 16:52:09 +0200663
664 tfi = bts->tfi_find_free(GPRS_RLCMAC_DL_TBF, &trx, this->trx->trx_no);
665 if (tfi < 0) {
666 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource\n");
667 /* FIXME: send reject */
668 return;
669 }
670 new_tbf = tbf_alloc_dl_tbf(bts->bts_data(), NULL, tfi, trx, ms_class, 0);
671 if (!new_tbf) {
672 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource\n");
673 /* FIXME: send reject */
674 return;
675 }
676
677 new_tbf->m_tlli = m_tlli;
678 new_tbf->m_tlli_valid = m_tlli_valid;
679 new_tbf->ta = ta;
680 new_tbf->assign_imsi(m_imsi);
681
682 /* Copy over all data to the new TBF */
683 new_tbf->m_llc.put_frame(data, len);
Daniel Willmannca102af2014-08-08 12:14:12 +0200684 bts->llc_frame_sched();
685
Daniel Willmanne4818152014-08-15 16:52:09 +0200686 while ((msg = m_llc.dequeue()))
687 new_tbf->m_llc.enqueue(msg);
688
Daniel Willmannca102af2014-08-08 12:14:12 +0200689 /* reset rlc states */
690 m_tx_counter = 0;
691 m_wait_confirm = 0;
692 m_window.reset();
693
Jacob Erlbeck297edf72015-02-26 14:59:52 +0100694 /* mark TLLI as invalid */
695 m_tlli_valid = 0;
696
Daniel Willmannca102af2014-08-08 12:14:12 +0200697 /* keep to flags */
698 state_flags &= GPRS_RLCMAC_FLAG_TO_MASK;
699 state_flags &= ~(1 << GPRS_RLCMAC_FLAG_CCCH);
700
701 update();
702
703 LOGP(DRLCMAC, LOGL_DEBUG, "%s Trigger dowlink assignment on PACCH, "
704 "because another LLC PDU has arrived in between\n",
705 tbf_name(this));
Daniel Willmanne4818152014-08-15 16:52:09 +0200706 bts->trigger_dl_ass(new_tbf, this, NULL);
Daniel Willmannca102af2014-08-08 12:14:12 +0200707}
708
709bool gprs_rlcmac_dl_tbf::dl_window_stalled() const
710{
711 return m_window.window_stalled();
712}
713
Jacob Erlbeckeceb9102015-03-20 14:41:50 +0100714void gprs_rlcmac_dl_tbf::request_dl_ack()
715{
716 m_dl_ack_requested = true;
717}
718
719bool gprs_rlcmac_dl_tbf::need_control_ts() const
720{
721 if (poll_state != GPRS_RLCMAC_POLL_NONE)
722 return false;
723
724 return state_flags & (1 << GPRS_RLCMAC_FLAG_TO_DL_ACK) ||
725 m_tx_counter >= POLL_ACK_AFTER_FRAMES ||
726 m_dl_ack_requested;
727}
728
729bool gprs_rlcmac_dl_tbf::have_data() const
730{
731 return m_llc.chunk_size() > 0 || !llist_empty(&m_llc.queue);
732}