blob: aeec23f2b8d938fa9ad36edc0b06c1f85729dac1 [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;
Jacob Erlbeck0c1c8772015-03-20 12:02:42 +0100212 struct timeval *tv, tv_now, tv_now2;
Daniel Willmannca102af2014-08-08 12:14:12 +0200213 uint32_t octets = 0, frames = 0;
Jacob Erlbeck0c1c8772015-03-20 12:02:42 +0100214 struct timeval hyst_delta = {0, 0};
215 const unsigned keep_small_thresh = 60;
216
217 if (bts_data()->llc_discard_csec)
218 csecs_to_timeval(bts_data()->llc_discard_csec, &hyst_delta);
Daniel Willmannca102af2014-08-08 12:14:12 +0200219
220 gettimeofday(&tv_now, NULL);
Jacob Erlbeck0c1c8772015-03-20 12:02:42 +0100221 timeradd(&tv_now, &hyst_delta, &tv_now2);
Daniel Willmannca102af2014-08-08 12:14:12 +0200222
223 while ((msg = m_llc.dequeue())) {
224 tv = (struct timeval *)msg->data;
225 msgb_pull(msg, sizeof(*tv));
226 msgb_pull(msg, sizeof(*tv));
227
Jacob Erlbeck0c1c8772015-03-20 12:02:42 +0100228 /* Is the age below the low water mark? */
229 if (!gprs_llc::is_frame_expired(&tv_now2, tv))
230 break;
231
232 /* Is the age below the high water mark */
233 if (!gprs_llc::is_frame_expired(&tv_now, tv)) {
234 /* Has the previous message not been dropped? */
235 if (frames == 0)
236 break;
237
238 /* Hysteresis mode, try to discard LLC messages until
239 * the low water mark has been reached */
240
241 /* Check whether to abort the hysteresis mode */
242
243 /* Is the frame small, perhaps only a TCP ACK? */
244 if (msg->len <= keep_small_thresh)
245 break;
246
247 /* Is it a GMM message? */
248 if (!gprs_llc::is_user_data_frame(msg->data, msg->len))
249 break;
Daniel Willmannca102af2014-08-08 12:14:12 +0200250 }
Jacob Erlbeck0c1c8772015-03-20 12:02:42 +0100251
252 bts->llc_timedout_frame();
253 frames++;
254 octets += msg->len;
255 msgb_free(msg);
256 continue;
Daniel Willmannca102af2014-08-08 12:14:12 +0200257 }
258
259 if (frames) {
Jacob Erlbeck0c1c8772015-03-20 12:02:42 +0100260 LOGP(DRLCMACDL, LOGL_NOTICE, "%s Discarding LLC PDU "
261 "because lifetime limit reached, "
262 "count=%u new_queue_size=%zu\n",
263 tbf_name(this), frames, m_llc.m_queue_size);
Daniel Willmannca102af2014-08-08 12:14:12 +0200264 if (frames > 0xff)
265 frames = 0xff;
266 if (octets > 0xffffff)
267 octets = 0xffffff;
268 bssgp_tx_llc_discarded(bctx, m_tlli, frames, octets);
269 }
270
271 return msg;
272}
273
274/*
275 * Create DL data block
276 * The messages are fragmented and forwarded as data blocks.
277 */
278struct msgb *gprs_rlcmac_dl_tbf::create_dl_acked_block(uint32_t fn, uint8_t ts)
279{
280 LOGP(DRLCMACDL, LOGL_DEBUG, "%s downlink (V(A)==%d .. "
281 "V(S)==%d)\n", tbf_name(this),
282 m_window.v_a(), m_window.v_s());
283
284do_resend:
285 /* check if there is a block with negative acknowledgement */
286 int resend_bsn = m_window.resend_needed();
287 if (resend_bsn >= 0) {
288 LOGP(DRLCMACDL, LOGL_DEBUG, "- Resending BSN %d\n", resend_bsn);
289 /* re-send block with negative aknowlegement */
290 m_window.m_v_b.mark_unacked(resend_bsn);
291 bts->rlc_resent();
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100292 return create_dl_acked_block(fn, ts, resend_bsn);
Daniel Willmannca102af2014-08-08 12:14:12 +0200293 }
294
295 /* if the window has stalled, or transfer is complete,
296 * send an unacknowledged block */
Jacob Erlbeck95340242015-03-19 13:22:07 +0100297 if (state_is(GPRS_RLCMAC_FINISHED)) {
298 LOGP(DRLCMACDL, LOGL_DEBUG, "- Restarting at BSN %d, "
299 "because all blocks have been transmitted.\n",
300 m_window.v_a());
301 bts->rlc_restarted();
302 } else if (dl_window_stalled()) {
303 LOGP(DRLCMACDL, LOGL_NOTICE, "- Restarting at BSN %d, "
304 "because all window is stalled.\n",
305 m_window.v_a());
306 bts->rlc_stalled();
Jacob Erlbecke25b5b92015-03-19 14:21:33 +0100307 } else if (have_data()) {
308 /* New blocks may be send */
309 return create_new_bsn(fn, ts);
310 } else if (!m_window.window_empty()) {
311 LOGP(DRLCMACDL, LOGL_DEBUG, "- Restarting at BSN %d, "
312 "because all blocks have been transmitted (FLOW).\n",
313 m_window.v_a());
314 bts->rlc_restarted();
Jacob Erlbeck95340242015-03-19 13:22:07 +0100315 } else {
Jacob Erlbecke25b5b92015-03-19 14:21:33 +0100316 /* Nothing left to send, create dummy LLC commands */
Jacob Erlbeck95340242015-03-19 13:22:07 +0100317 return create_new_bsn(fn, ts);
Daniel Willmannca102af2014-08-08 12:14:12 +0200318 }
319
Jacob Erlbeck95340242015-03-19 13:22:07 +0100320 /* If V(S) == V(A) and finished state, we would have received
321 * acknowledgement of all transmitted block. In this case we
322 * would have transmitted the final block, and received ack
323 * from MS. But in this case we did not receive the final ack
324 * indication from MS. This should never happen if MS works
325 * correctly. */
326 if (m_window.window_empty()) {
327 LOGP(DRLCMACDL, LOGL_DEBUG, "- MS acked all blocks, "
328 "so we re-transmit final block!\n");
329 /* we just send final block again */
330 int16_t index = m_window.v_s_mod(-1);
331 bts->rlc_resent();
332 return create_dl_acked_block(fn, ts, index);
333 }
334
335 /* cycle through all unacked blocks */
336 int resend = m_window.mark_for_resend();
337
338 /* At this point there should be at least one unacked block
339 * to be resent. If not, this is an software error. */
340 if (resend == 0) {
341 LOGP(DRLCMACDL, LOGL_ERROR, "Software error: "
342 "There are no unacknowledged blocks, but V(A) "
343 " != V(S). PLEASE FIX!\n");
344 /* we just send final block again */
345 int16_t index = m_window.v_s_mod(-1);
346 return create_dl_acked_block(fn, ts, index);
347 }
348 goto do_resend;
Daniel Willmannca102af2014-08-08 12:14:12 +0200349}
350
351struct msgb *gprs_rlcmac_dl_tbf::create_new_bsn(const uint32_t fn, const uint8_t ts)
352{
353 struct rlc_dl_header *rh;
354 struct rlc_li_field *li;
355 struct msgb *msg;
356 uint8_t *delimiter, *data, *e_pointer;
357 uint16_t space, chunk;
358 gprs_rlc_data *rlc_data;
Daniel Willmannca102af2014-08-08 12:14:12 +0200359 const uint16_t bsn = m_window.v_s();
360
361 LOGP(DRLCMACDL, LOGL_DEBUG, "- Sending new block at BSN %d\n",
362 m_window.v_s());
363
364#warning "Selection of the CS doesn't belong here"
365 if (cs == 0) {
366 cs = bts_data()->initial_cs_dl;
367 if (cs < 1 || cs > 4)
368 cs = 1;
369 }
370 /* total length of block, including spare bits */
371 const uint8_t block_length = gprs_rlcmac_cs[cs].block_length;
372 /* length of usable data of block, w/o spare bits, inc. MAC */
373 const uint8_t block_data_len = gprs_rlcmac_cs[cs].block_data;
374
375 /* now we still have untransmitted LLC data, so we fill mac block */
376 rlc_data = m_rlc.block(bsn);
377 data = rlc_data->prepare(block_data_len);
378
379 rh = (struct rlc_dl_header *)data;
380 rh->pt = 0; /* Data Block */
381 rh->rrbp = rh->s_p = 0; /* Polling, set later, if required */
382 rh->usf = 7; /* will be set at scheduler */
383 rh->pr = 0; /* FIXME: power reduction */
384 rh->tfi = m_tfi; /* TFI */
385 rh->fbi = 0; /* Final Block Indicator, set late, if true */
386 rh->bsn = bsn; /* Block Sequence Number */
387 rh->e = 0; /* Extension bit, maybe set later */
388 e_pointer = data + 2; /* points to E of current chunk */
389 data += sizeof(*rh);
390 delimiter = data; /* where next length header would be stored */
391 space = block_data_len - sizeof(*rh);
392 while (1) {
Jacob Erlbeckcbb1e702015-03-25 12:21:55 +0100393 if (m_llc.frame_length() == 0) {
394 /* A header will need to by added, so we just need
395 * space-1 octets */
396 m_llc.put_dummy_frame(space - 1);
397
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100398 /* The data just drained, store the current fn */
399 if (m_last_dl_drained_fn < 0)
400 m_last_dl_drained_fn = fn;
401
Jacob Erlbeckcbb1e702015-03-25 12:21:55 +0100402 /* It is not clear, when the next real data will
403 * arrive, so request a DL ack/nack now */
404 request_dl_ack();
405
406 LOGP(DRLCMACDL, LOGL_DEBUG,
407 "-- Empty chunk, "
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100408 "added LLC dummy command of size %d, "
409 "drained_since=%d\n",
410 m_llc.frame_length(), frames_since_last_drain(fn));
Jacob Erlbeckcbb1e702015-03-25 12:21:55 +0100411 }
412
Daniel Willmannca102af2014-08-08 12:14:12 +0200413 chunk = m_llc.chunk_size();
Jacob Erlbeckcbb1e702015-03-25 12:21:55 +0100414
Daniel Willmannca102af2014-08-08 12:14:12 +0200415 /* if chunk will exceed block limit */
416 if (chunk > space) {
417 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d "
418 "larger than space (%d) left in block: copy "
419 "only remaining space, and we are done\n",
420 chunk, space);
421 /* block is filled, so there is no extension */
422 *e_pointer |= 0x01;
423 /* fill only space */
424 m_llc.consume(data, space);
425 /* return data block as message */
426 break;
427 }
428 /* if FINAL chunk would fit precisely in space left */
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100429 if (chunk == space && llist_empty(&m_llc.queue) && !keep_open(fn))
430 {
Daniel Willmannca102af2014-08-08 12:14:12 +0200431 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d "
432 "would exactly fit into space (%d): because "
433 "this is a final block, we don't add length "
434 "header, and we are done\n", chunk, space);
435 LOGP(DRLCMACDL, LOGL_INFO, "Complete DL frame for "
436 "%s that fits precisely in last block: "
437 "len=%d\n", tbf_name(this), m_llc.frame_length());
438 gprs_rlcmac_dl_bw(this, m_llc.frame_length());
439 /* block is filled, so there is no extension */
440 *e_pointer |= 0x01;
441 /* fill space */
442 m_llc.consume(data, space);
443 m_llc.reset();
444 /* final block */
445 rh->fbi = 1; /* we indicate final block */
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100446 request_dl_ack();
Daniel Willmannca102af2014-08-08 12:14:12 +0200447 set_state(GPRS_RLCMAC_FINISHED);
448 /* return data block as message */
449 break;
450 }
451 /* if chunk would fit exactly in space left */
452 if (chunk == space) {
453 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d "
454 "would exactly fit into space (%d): add length "
455 "header with LI=0, to make frame extend to "
456 "next block, and we are done\n", chunk, space);
457 /* make space for delimiter */
458 if (delimiter != data)
459 memmove(delimiter + 1, delimiter,
460 data - delimiter);
461 data++;
462 space--;
463 /* add LI with 0 length */
464 li = (struct rlc_li_field *)delimiter;
465 li->e = 1; /* not more extension */
466 li->m = 0; /* shall be set to 0, in case of li = 0 */
467 li->li = 0; /* chunk fills the complete space */
468 // no need to set e_pointer nor increase delimiter
469 /* fill only space, which is 1 octet less than chunk */
470 m_llc.consume(data, space);
471 /* return data block as message */
472 break;
473 }
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100474
Daniel Willmannca102af2014-08-08 12:14:12 +0200475 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d is less "
476 "than remaining space (%d): add length header to "
477 "to delimit LLC frame\n", chunk, space);
478 /* the LLC frame chunk ends in this block */
479 /* make space for delimiter */
480 if (delimiter != data)
481 memmove(delimiter + 1, delimiter, data - delimiter);
482 data++;
483 space--;
484 /* add LI to delimit frame */
485 li = (struct rlc_li_field *)delimiter;
486 li->e = 0; /* Extension bit, maybe set later */
487 li->m = 0; /* will be set later, if there is more LLC data */
488 li->li = chunk; /* length of chunk */
489 e_pointer = delimiter; /* points to E of current delimiter */
490 delimiter++;
491 /* copy (rest of) LLC frame to space and reset later */
492 m_llc.consume(data, chunk);
493 data += chunk;
494 space -= chunk;
495 LOGP(DRLCMACDL, LOGL_INFO, "Complete DL frame for %s"
496 "len=%d\n", tbf_name(this), m_llc.frame_length());
497 gprs_rlcmac_dl_bw(this, m_llc.frame_length());
498 m_llc.reset();
499 /* dequeue next LLC frame, if any */
500 msg = llc_dequeue(gprs_bssgp_pcu_current_bctx());
501 if (msg) {
502 LOGP(DRLCMACDL, LOGL_INFO, "- Dequeue next LLC for "
503 "%s (len=%d)\n", tbf_name(this), msg->len);
504 m_llc.put_frame(msg->data, msg->len);
505 bts->llc_frame_sched();
506 msgb_free(msg);
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100507 m_last_dl_drained_fn = -1;
Daniel Willmannca102af2014-08-08 12:14:12 +0200508 }
509 /* if we have more data and we have space left */
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100510 if (space > 0 && (m_llc.frame_length() || keep_open(fn))) {
Daniel Willmannca102af2014-08-08 12:14:12 +0200511 li->m = 1; /* we indicate more frames to follow */
512 continue;
513 }
514 /* if we don't have more LLC frames */
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100515 if (!m_llc.frame_length() && !keep_open(fn)) {
Daniel Willmannca102af2014-08-08 12:14:12 +0200516 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Final block, so we "
517 "done.\n");
518 li->e = 1; /* we cannot extend */
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100519
Daniel Willmannca102af2014-08-08 12:14:12 +0200520 rh->fbi = 1; /* we indicate final block */
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100521 request_dl_ack();
Daniel Willmannca102af2014-08-08 12:14:12 +0200522 set_state(GPRS_RLCMAC_FINISHED);
523 break;
524 }
525 /* we have no space left */
526 LOGP(DRLCMACDL, LOGL_DEBUG, "-- No space left, so we are "
527 "done.\n");
528 li->e = 1; /* we cannot extend */
529 break;
530 }
531 LOGP(DRLCMACDL, LOGL_DEBUG, "data block: %s\n",
532 osmo_hexdump(rlc_data->block, block_length));
533#warning "move this up?"
534 rlc_data->len = block_length;
535 /* raise send state and set ack state array */
536 m_window.m_v_b.mark_unacked(bsn);
537 m_window.increment_send();
538
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100539 return create_dl_acked_block(fn, ts, bsn);
540}
541
Daniel Willmannca102af2014-08-08 12:14:12 +0200542struct msgb *gprs_rlcmac_dl_tbf::create_dl_acked_block(
543 const uint32_t fn, const uint8_t ts,
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100544 const int index)
Daniel Willmannca102af2014-08-08 12:14:12 +0200545{
546 uint8_t *data;
547 struct rlc_dl_header *rh;
548 struct msgb *dl_msg;
549 uint8_t len;
Daniel Willmannefd5dbb2014-08-25 16:20:23 +0200550 bool need_poll;
Daniel Willmannca102af2014-08-08 12:14:12 +0200551
552 /* get data and header from current block */
553 data = m_rlc.block(index)->block;
554 len = m_rlc.block(index)->len;
555 rh = (struct rlc_dl_header *)data;
556
Jacob Erlbeck005ee7f2015-03-20 14:53:54 +0100557 /* If the TBF has just started, relate frames_since_last_poll to the
558 * current fn */
559 if (m_last_dl_poll_fn < 0)
560 m_last_dl_poll_fn = fn;
561
Daniel Willmannefd5dbb2014-08-25 16:20:23 +0200562 need_poll = state_flags & (1 << GPRS_RLCMAC_FLAG_TO_DL_ACK);
Daniel Willmannca102af2014-08-08 12:14:12 +0200563 /* Clear Polling, if still set in history buffer */
564 rh->s_p = 0;
565
566 /* poll after POLL_ACK_AFTER_FRAMES frames, or when final block is tx.
567 */
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100568 if (m_tx_counter >= POLL_ACK_AFTER_FRAMES || m_dl_ack_requested ||
Daniel Willmannefd5dbb2014-08-25 16:20:23 +0200569 need_poll) {
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100570 if (m_dl_ack_requested) {
Daniel Willmannca102af2014-08-08 12:14:12 +0200571 LOGP(DRLCMACDL, LOGL_DEBUG, "- Scheduling Ack/Nack "
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100572 "polling, because is was requested explicitly "
573 "(e.g. first final block sent).\n");
Daniel Willmannefd5dbb2014-08-25 16:20:23 +0200574 } else if (need_poll) {
575 LOGP(DRLCMACDL, LOGL_DEBUG, "- Scheduling Ack/Nack "
Daniel Willmann635d47c2014-09-17 17:58:29 +0200576 "polling, because polling timed out.\n");
Daniel Willmannca102af2014-08-08 12:14:12 +0200577 } else {
578 LOGP(DRLCMACDL, LOGL_DEBUG, "- Scheduling Ack/Nack "
579 "polling, because %d blocks sent.\n",
580 POLL_ACK_AFTER_FRAMES);
581 }
582 /* scheduling not possible, because: */
583 if (poll_state != GPRS_RLCMAC_POLL_NONE)
584 LOGP(DRLCMACDL, LOGL_DEBUG, "Polling is already "
585 "sheduled for %s, so we must wait for "
586 "requesting downlink ack\n", tbf_name(this));
587 else if (control_ts != ts)
588 LOGP(DRLCMACDL, LOGL_DEBUG, "Polling cannot be "
589 "sheduled in this TS %d, waiting for "
590 "TS %d\n", ts, control_ts);
Daniel Willmannca102af2014-08-08 12:14:12 +0200591 else if (bts->sba()->find(trx->trx_no, ts, (fn + 13) % 2715648))
592 LOGP(DRLCMACDL, LOGL_DEBUG, "Polling cannot be "
593 "sheduled, because single block alllocation "
594 "already exists\n");
595 else {
596 LOGP(DRLCMACDL, LOGL_DEBUG, "Polling sheduled in this "
597 "TS %d\n", ts);
598 m_tx_counter = 0;
599 /* start timer whenever we send the final block */
600 if (rh->fbi == 1)
601 tbf_timer_start(this, 3191, bts_data()->t3191, 0);
602
603 /* schedule polling */
604 poll_state = GPRS_RLCMAC_POLL_SCHED;
605 poll_fn = (fn + 13) % 2715648;
606
Daniel Willmannefd5dbb2014-08-25 16:20:23 +0200607 /* Clear poll timeout flag */
608 state_flags &= ~(1 << GPRS_RLCMAC_FLAG_TO_DL_ACK);
609
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100610 /* Clear request flag */
611 m_dl_ack_requested = false;
612
Daniel Willmannca102af2014-08-08 12:14:12 +0200613 /* set polling in header */
614 rh->rrbp = 0; /* N+13 */
615 rh->s_p = 1; /* Polling */
Jacob Erlbeck005ee7f2015-03-20 14:53:54 +0100616
617 m_last_dl_poll_fn = poll_fn;
Daniel Willmannca102af2014-08-08 12:14:12 +0200618 }
619 }
620
621 /* return data block as message */
622 dl_msg = msgb_alloc(len, "rlcmac_dl_data");
623 if (!dl_msg)
624 return NULL;
625
626 /* Increment TX-counter */
627 m_tx_counter++;
628
629 memcpy(msgb_put(dl_msg, len), data, len);
630 bts->rlc_sent();
631
632 return dl_msg;
633}
634
635int gprs_rlcmac_dl_tbf::update_window(const uint8_t ssn, const uint8_t *rbb)
636{
637 int16_t dist; /* must be signed */
638 uint16_t lost = 0, received = 0;
639 char show_rbb[65];
640 char show_v_b[RLC_MAX_SNS + 1];
641 const uint16_t mod_sns = m_window.mod_sns();
642
643 Decoding::extract_rbb(rbb, show_rbb);
644 /* show received array in debug (bit 64..1) */
645 LOGP(DRLCMACDL, LOGL_DEBUG, "- ack: (BSN=%d)\"%s\""
646 "(BSN=%d) R=ACK I=NACK\n", (ssn - 64) & mod_sns,
647 show_rbb, (ssn - 1) & mod_sns);
648
649 /* apply received array to receive state (SSN-64..SSN-1) */
650 /* calculate distance of ssn from V(S) */
651 dist = (m_window.v_s() - ssn) & mod_sns;
652 /* check if distance is less than distance V(A)..V(S) */
653 if (dist >= m_window.distance()) {
654 /* this might happpen, if the downlink assignment
655 * was not received by ms and the ack refers
656 * to previous TBF
657 * FIXME: we should implement polling for
658 * control ack!*/
659 LOGP(DRLCMACDL, LOGL_NOTICE, "- ack range is out of "
660 "V(A)..V(S) range %s Free TBF!\n", tbf_name(this));
661 return 1; /* indicate to free TBF */
662 }
663
664 m_window.update(bts, show_rbb, ssn,
665 &lost, &received);
666
667 /* report lost and received packets */
668 gprs_rlcmac_received_lost(this, received, lost);
669
670 /* raise V(A), if possible */
671 m_window.raise(m_window.move_window());
672
673 /* show receive state array in debug (V(A)..V(S)-1) */
674 m_window.show_state(show_v_b);
675 LOGP(DRLCMACDL, LOGL_DEBUG, "- V(B): (V(A)=%d)\"%s\""
676 "(V(S)-1=%d) A=Acked N=Nacked U=Unacked "
677 "X=Resend-Unacked I=Invalid\n",
678 m_window.v_a(), show_v_b,
679 m_window.v_s_mod(-1));
680
681 if (state_is(GPRS_RLCMAC_FINISHED) && m_window.window_empty()) {
682 LOGP(DRLCMACDL, LOGL_NOTICE, "Received acknowledge of "
683 "all blocks, but without final ack "
684 "inidcation (don't worry)\n");
685 }
686 return 0;
687}
688
689
690int gprs_rlcmac_dl_tbf::maybe_start_new_window()
691{
692 struct msgb *msg;
693 uint16_t received;
694
695 LOGP(DRLCMACDL, LOGL_DEBUG, "- Final ACK received.\n");
696 /* range V(A)..V(S)-1 */
697 received = m_window.count_unacked();
698
699 /* report all outstanding packets as received */
700 gprs_rlcmac_received_lost(this, received, 0);
701
702 set_state(GPRS_RLCMAC_WAIT_RELEASE);
703
704 /* check for LLC PDU in the LLC Queue */
705 msg = llc_dequeue(gprs_bssgp_pcu_current_bctx());
706 if (!msg) {
707 /* no message, start T3193, change state to RELEASE */
708 LOGP(DRLCMACDL, LOGL_DEBUG, "- No new message, so we release.\n");
709 /* start T3193 */
710 tbf_timer_start(this, 3193,
711 bts_data()->t3193_msec / 1000,
712 (bts_data()->t3193_msec % 1000) * 1000);
713
714 return 0;
715 }
716
717 /* we have more data so we will re-use this tbf */
718 reuse_tbf(msg->data, msg->len);
719 msgb_free(msg);
720 return 0;
721}
722
723int gprs_rlcmac_dl_tbf::rcvd_dl_ack(uint8_t final_ack, uint8_t ssn, uint8_t *rbb)
724{
725 LOGP(DRLCMACDL, LOGL_DEBUG, "%s downlink acknowledge\n", tbf_name(this));
726
727 if (!final_ack)
728 return update_window(ssn, rbb);
729 return maybe_start_new_window();
730}
731
732void gprs_rlcmac_dl_tbf::reuse_tbf(const uint8_t *data, const uint16_t len)
733{
Daniel Willmanne4818152014-08-15 16:52:09 +0200734 uint8_t trx;
735 struct gprs_rlcmac_dl_tbf *new_tbf;
736 int8_t tfi; /* must be signed */
Daniel Willmanne4818152014-08-15 16:52:09 +0200737 struct msgb *msg;
738
Daniel Willmannca102af2014-08-08 12:14:12 +0200739 bts->tbf_reused();
Daniel Willmanne4818152014-08-15 16:52:09 +0200740
741 tfi = bts->tfi_find_free(GPRS_RLCMAC_DL_TBF, &trx, this->trx->trx_no);
742 if (tfi < 0) {
743 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource\n");
744 /* FIXME: send reject */
745 return;
746 }
747 new_tbf = tbf_alloc_dl_tbf(bts->bts_data(), NULL, tfi, trx, ms_class, 0);
748 if (!new_tbf) {
749 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource\n");
750 /* FIXME: send reject */
751 return;
752 }
753
754 new_tbf->m_tlli = m_tlli;
755 new_tbf->m_tlli_valid = m_tlli_valid;
756 new_tbf->ta = ta;
757 new_tbf->assign_imsi(m_imsi);
758
759 /* Copy over all data to the new TBF */
760 new_tbf->m_llc.put_frame(data, len);
Daniel Willmannca102af2014-08-08 12:14:12 +0200761 bts->llc_frame_sched();
762
Daniel Willmanne4818152014-08-15 16:52:09 +0200763 while ((msg = m_llc.dequeue()))
764 new_tbf->m_llc.enqueue(msg);
765
Daniel Willmannca102af2014-08-08 12:14:12 +0200766 /* reset rlc states */
767 m_tx_counter = 0;
768 m_wait_confirm = 0;
769 m_window.reset();
770
Jacob Erlbeck297edf72015-02-26 14:59:52 +0100771 /* mark TLLI as invalid */
772 m_tlli_valid = 0;
773
Daniel Willmannca102af2014-08-08 12:14:12 +0200774 /* keep to flags */
775 state_flags &= GPRS_RLCMAC_FLAG_TO_MASK;
776 state_flags &= ~(1 << GPRS_RLCMAC_FLAG_CCCH);
777
778 update();
779
780 LOGP(DRLCMAC, LOGL_DEBUG, "%s Trigger dowlink assignment on PACCH, "
781 "because another LLC PDU has arrived in between\n",
782 tbf_name(this));
Daniel Willmanne4818152014-08-15 16:52:09 +0200783 bts->trigger_dl_ass(new_tbf, this, NULL);
Daniel Willmannca102af2014-08-08 12:14:12 +0200784}
785
786bool gprs_rlcmac_dl_tbf::dl_window_stalled() const
787{
788 return m_window.window_stalled();
789}
790
Jacob Erlbeckeceb9102015-03-20 14:41:50 +0100791void gprs_rlcmac_dl_tbf::request_dl_ack()
792{
793 m_dl_ack_requested = true;
794}
795
796bool gprs_rlcmac_dl_tbf::need_control_ts() const
797{
798 if (poll_state != GPRS_RLCMAC_POLL_NONE)
799 return false;
800
801 return state_flags & (1 << GPRS_RLCMAC_FLAG_TO_DL_ACK) ||
802 m_tx_counter >= POLL_ACK_AFTER_FRAMES ||
803 m_dl_ack_requested;
804}
805
806bool gprs_rlcmac_dl_tbf::have_data() const
807{
808 return m_llc.chunk_size() > 0 || !llist_empty(&m_llc.queue);
809}
Jacob Erlbeck005ee7f2015-03-20 14:53:54 +0100810
811int gprs_rlcmac_dl_tbf::frames_since_last_poll(unsigned fn) const
812{
813 unsigned wrapped;
814 if (m_last_dl_poll_fn < 0)
815 return -1;
816
817 wrapped = (fn + 2715648 - m_last_dl_poll_fn) % 2715648;
818 if (wrapped < 2715648/2)
819 return wrapped;
820 else
821 return wrapped - 2715648;
822}
823
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100824int gprs_rlcmac_dl_tbf::frames_since_last_drain(unsigned fn) const
825{
826 unsigned wrapped;
827 if (m_last_dl_drained_fn < 0)
828 return -1;
829
830 wrapped = (fn + 2715648 - m_last_dl_drained_fn) % 2715648;
831 if (wrapped < 2715648/2)
832 return wrapped;
833 else
834 return wrapped - 2715648;
835}
836
837bool gprs_rlcmac_dl_tbf::keep_open(unsigned fn) const
838{
839 int keep_time_frames;
840
841 if (bts_data()->dl_tbf_idle_msec <= 0)
842 return false;
843
844 keep_time_frames = msecs_to_frames(bts_data()->dl_tbf_idle_msec);
845 return frames_since_last_drain(fn) <= keep_time_frames;
846}