blob: e8c5dfea58e56e291efdb7d746dccd5abf7d757e [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
Jacob Erlbeckd0261b72015-04-02 13:58:09 +020065static void llc_timer_cb(void *_tbf)
66{
67 struct gprs_rlcmac_dl_tbf *tbf = (struct gprs_rlcmac_dl_tbf *)_tbf;
68
69 if (tbf->state_is_not(GPRS_RLCMAC_FLOW))
70 return;
71
72 LOGP(DRLCMAC, LOGL_DEBUG,
73 "%s LLC receive timeout, requesting DL ACK\n", tbf_name(tbf));
74
75 tbf->request_dl_ack();
76}
77
78void gprs_rlcmac_dl_tbf::cleanup()
79{
80 osmo_timer_del(&m_llc_timer);
81}
82
83void gprs_rlcmac_dl_tbf::start_llc_timer()
84{
85 if (bts_data()->llc_idle_ack_csec > 0) {
86 struct timeval tv;
87
88 /* TODO: this ought to be within a constructor */
89 m_llc_timer.data = this;
90 m_llc_timer.cb = &llc_timer_cb;
91
92 csecs_to_timeval(bts_data()->llc_idle_ack_csec, &tv);
93 osmo_timer_schedule(&m_llc_timer, tv.tv_sec, tv.tv_usec);
94 }
95}
96
Daniel Willmannca102af2014-08-08 12:14:12 +020097int gprs_rlcmac_dl_tbf::append_data(const uint8_t ms_class,
98 const uint16_t pdu_delay_csec,
99 const uint8_t *data, const uint16_t len)
100{
101 LOGP(DRLCMAC, LOGL_INFO, "%s append\n", tbf_name(this));
102 if (state_is(GPRS_RLCMAC_WAIT_RELEASE)) {
103 LOGP(DRLCMAC, LOGL_DEBUG,
104 "%s in WAIT RELEASE state "
105 "(T3193), so reuse TBF\n", tbf_name(this));
106 tbf_update_ms_class(this, ms_class);
107 reuse_tbf(data, len);
Jacob Erlbeckc4952092015-03-24 11:04:19 +0100108 } else if (!have_data()) {
109 m_llc.put_frame(data, len);
Jacob Erlbeck502bd1f2015-03-20 14:26:05 +0100110 m_last_dl_drained_fn = -1;
Jacob Erlbeckc4952092015-03-24 11:04:19 +0100111 bts->llc_frame_sched();
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100112 /* it is no longer drained */
113 m_last_dl_drained_fn = -1;
Jacob Erlbeckc4952092015-03-24 11:04:19 +0100114 tbf_update_ms_class(this, ms_class);
Jacob Erlbeckd0261b72015-04-02 13:58:09 +0200115 start_llc_timer();
Daniel Willmannca102af2014-08-08 12:14:12 +0200116 } else {
Jacob Erlbeckd0261b72015-04-02 13:58:09 +0200117 /* TODO: put this path into an llc_enqueue method */
Daniel Willmannca102af2014-08-08 12:14:12 +0200118 /* the TBF exists, so we must write it in the queue
119 * we prepend lifetime in front of PDU */
120 struct timeval *tv;
121 struct msgb *llc_msg = msgb_alloc(len + sizeof(*tv) * 2,
122 "llc_pdu_queue");
123 if (!llc_msg)
124 return -ENOMEM;
125 tv = (struct timeval *)msgb_put(llc_msg, sizeof(*tv));
126 gprs_llc::calc_pdu_lifetime(bts, pdu_delay_csec, tv);
127 tv = (struct timeval *)msgb_put(llc_msg, sizeof(*tv));
128 gettimeofday(tv, NULL);
129 memcpy(msgb_put(llc_msg, len), data, len);
130 m_llc.enqueue(llc_msg);
131 tbf_update_ms_class(this, ms_class);
Jacob Erlbeckd0261b72015-04-02 13:58:09 +0200132 start_llc_timer();
Daniel Willmannca102af2014-08-08 12:14:12 +0200133 }
134
135 return 0;
136}
137
138static struct gprs_rlcmac_dl_tbf *tbf_lookup_dl(BTS *bts,
139 const uint32_t tlli, const char *imsi)
140{
141 /* TODO: look up by IMSI first, then tlli, then old_tlli */
142 return bts->dl_tbf_by_tlli(tlli);
143}
144
145static int tbf_new_dl_assignment(struct gprs_rlcmac_bts *bts,
146 const char *imsi,
147 const uint32_t tlli, const uint8_t ms_class,
148 const uint8_t *data, const uint16_t len)
149{
150 uint8_t trx, ta, ss;
151 int8_t use_trx;
152 struct gprs_rlcmac_ul_tbf *ul_tbf, *old_ul_tbf;
153 struct gprs_rlcmac_dl_tbf *dl_tbf;
154 int8_t tfi; /* must be signed */
155 int rc;
156
157 /* check for uplink data, so we copy our informations */
158#warning "Do the same look up for IMSI, TLLI and OLD_TLLI"
159#warning "Refactor the below lines... into a new method"
160 ul_tbf = bts->bts->ul_tbf_by_tlli(tlli);
161 if (ul_tbf && ul_tbf->m_contention_resolution_done
162 && !ul_tbf->m_final_ack_sent) {
163 use_trx = ul_tbf->trx->trx_no;
164 ta = ul_tbf->ta;
165 ss = 0;
166 old_ul_tbf = ul_tbf;
167 } else {
168 use_trx = -1;
169 /* we already have an uplink TBF, so we use that TA */
170 if (ul_tbf)
171 ta = ul_tbf->ta;
172 else {
173 /* recall TA */
174 rc = bts->bts->timing_advance()->recall(tlli);
175 if (rc < 0) {
176 LOGP(DRLCMAC, LOGL_NOTICE, "TA unknown"
177 ", assuming 0\n");
178 ta = 0;
179 } else
180 ta = rc;
181 }
182 ss = 1; /* PCH assignment only allows one timeslot */
183 old_ul_tbf = NULL;
184 }
185
186 // Create new TBF (any TRX)
187#warning "Copy and paste with alloc_ul_tbf"
188 tfi = bts->bts->tfi_find_free(GPRS_RLCMAC_DL_TBF, &trx, use_trx);
189 if (tfi < 0) {
190 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource\n");
191 /* FIXME: send reject */
192 return -EBUSY;
193 }
194 /* set number of downlink slots according to multislot class */
195 dl_tbf = tbf_alloc_dl_tbf(bts, ul_tbf, tfi, trx, ms_class, ss);
196 if (!dl_tbf) {
197 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource\n");
198 /* FIXME: send reject */
199 return -EBUSY;
200 }
201 dl_tbf->m_tlli = tlli;
202 dl_tbf->m_tlli_valid = 1;
203 dl_tbf->ta = ta;
204
205 LOGP(DRLCMAC, LOGL_DEBUG, "%s [DOWNLINK] START\n", tbf_name(dl_tbf));
206
207 /* new TBF, so put first frame */
208 dl_tbf->m_llc.put_frame(data, len);
209 dl_tbf->bts->llc_frame_sched();
210
211 /* Store IMSI for later look-up and PCH retransmission */
212 dl_tbf->assign_imsi(imsi);
213
214 /* trigger downlink assignment and set state to ASSIGN.
215 * we don't use old_downlink, so the possible uplink is used
216 * to trigger downlink assignment. if there is no uplink,
217 * AGCH is used. */
218 dl_tbf->bts->trigger_dl_ass(dl_tbf, old_ul_tbf, imsi);
219 return 0;
220}
221
222/**
223 * TODO: split into unit test-able parts...
224 */
225int gprs_rlcmac_dl_tbf::handle(struct gprs_rlcmac_bts *bts,
226 const uint32_t tlli, const char *imsi,
227 const uint8_t ms_class, const uint16_t delay_csec,
228 const uint8_t *data, const uint16_t len)
229{
230 struct gprs_rlcmac_dl_tbf *dl_tbf;
231
232 /* check for existing TBF */
233 dl_tbf = tbf_lookup_dl(bts->bts, tlli, imsi);
234 if (dl_tbf) {
235 int rc = dl_tbf->append_data(ms_class, delay_csec, data, len);
236 if (rc >= 0)
237 dl_tbf->assign_imsi(imsi);
238 return rc;
239 }
240
241 return tbf_new_dl_assignment(bts, imsi, tlli, ms_class, data, len);
242}
243
244struct msgb *gprs_rlcmac_dl_tbf::llc_dequeue(bssgp_bvc_ctx *bctx)
245{
246 struct msgb *msg;
Jacob Erlbeck0c1c8772015-03-20 12:02:42 +0100247 struct timeval *tv, tv_now, tv_now2;
Daniel Willmannca102af2014-08-08 12:14:12 +0200248 uint32_t octets = 0, frames = 0;
Jacob Erlbeck0c1c8772015-03-20 12:02:42 +0100249 struct timeval hyst_delta = {0, 0};
250 const unsigned keep_small_thresh = 60;
251
252 if (bts_data()->llc_discard_csec)
253 csecs_to_timeval(bts_data()->llc_discard_csec, &hyst_delta);
Daniel Willmannca102af2014-08-08 12:14:12 +0200254
255 gettimeofday(&tv_now, NULL);
Jacob Erlbeck0c1c8772015-03-20 12:02:42 +0100256 timeradd(&tv_now, &hyst_delta, &tv_now2);
Daniel Willmannca102af2014-08-08 12:14:12 +0200257
258 while ((msg = m_llc.dequeue())) {
259 tv = (struct timeval *)msg->data;
260 msgb_pull(msg, sizeof(*tv));
261 msgb_pull(msg, sizeof(*tv));
262
Jacob Erlbeck0c1c8772015-03-20 12:02:42 +0100263 /* Is the age below the low water mark? */
264 if (!gprs_llc::is_frame_expired(&tv_now2, tv))
265 break;
266
267 /* Is the age below the high water mark */
268 if (!gprs_llc::is_frame_expired(&tv_now, tv)) {
269 /* Has the previous message not been dropped? */
270 if (frames == 0)
271 break;
272
273 /* Hysteresis mode, try to discard LLC messages until
274 * the low water mark has been reached */
275
276 /* Check whether to abort the hysteresis mode */
277
278 /* Is the frame small, perhaps only a TCP ACK? */
279 if (msg->len <= keep_small_thresh)
280 break;
281
282 /* Is it a GMM message? */
283 if (!gprs_llc::is_user_data_frame(msg->data, msg->len))
284 break;
Daniel Willmannca102af2014-08-08 12:14:12 +0200285 }
Jacob Erlbeck0c1c8772015-03-20 12:02:42 +0100286
287 bts->llc_timedout_frame();
288 frames++;
289 octets += msg->len;
290 msgb_free(msg);
291 continue;
Daniel Willmannca102af2014-08-08 12:14:12 +0200292 }
293
294 if (frames) {
Jacob Erlbeck0c1c8772015-03-20 12:02:42 +0100295 LOGP(DRLCMACDL, LOGL_NOTICE, "%s Discarding LLC PDU "
296 "because lifetime limit reached, "
297 "count=%u new_queue_size=%zu\n",
298 tbf_name(this), frames, m_llc.m_queue_size);
Daniel Willmannca102af2014-08-08 12:14:12 +0200299 if (frames > 0xff)
300 frames = 0xff;
301 if (octets > 0xffffff)
302 octets = 0xffffff;
303 bssgp_tx_llc_discarded(bctx, m_tlli, frames, octets);
304 }
305
306 return msg;
307}
308
309/*
310 * Create DL data block
311 * The messages are fragmented and forwarded as data blocks.
312 */
313struct msgb *gprs_rlcmac_dl_tbf::create_dl_acked_block(uint32_t fn, uint8_t ts)
314{
315 LOGP(DRLCMACDL, LOGL_DEBUG, "%s downlink (V(A)==%d .. "
316 "V(S)==%d)\n", tbf_name(this),
317 m_window.v_a(), m_window.v_s());
318
319do_resend:
320 /* check if there is a block with negative acknowledgement */
321 int resend_bsn = m_window.resend_needed();
322 if (resend_bsn >= 0) {
323 LOGP(DRLCMACDL, LOGL_DEBUG, "- Resending BSN %d\n", resend_bsn);
324 /* re-send block with negative aknowlegement */
325 m_window.m_v_b.mark_unacked(resend_bsn);
326 bts->rlc_resent();
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100327 return create_dl_acked_block(fn, ts, resend_bsn);
Daniel Willmannca102af2014-08-08 12:14:12 +0200328 }
329
330 /* if the window has stalled, or transfer is complete,
331 * send an unacknowledged block */
Jacob Erlbeck95340242015-03-19 13:22:07 +0100332 if (state_is(GPRS_RLCMAC_FINISHED)) {
333 LOGP(DRLCMACDL, LOGL_DEBUG, "- Restarting at BSN %d, "
334 "because all blocks have been transmitted.\n",
335 m_window.v_a());
336 bts->rlc_restarted();
337 } else if (dl_window_stalled()) {
338 LOGP(DRLCMACDL, LOGL_NOTICE, "- Restarting at BSN %d, "
339 "because all window is stalled.\n",
340 m_window.v_a());
341 bts->rlc_stalled();
Jacob Erlbecke25b5b92015-03-19 14:21:33 +0100342 } else if (have_data()) {
343 /* New blocks may be send */
344 return create_new_bsn(fn, ts);
345 } else if (!m_window.window_empty()) {
346 LOGP(DRLCMACDL, LOGL_DEBUG, "- Restarting at BSN %d, "
347 "because all blocks have been transmitted (FLOW).\n",
348 m_window.v_a());
349 bts->rlc_restarted();
Jacob Erlbeck95340242015-03-19 13:22:07 +0100350 } else {
Jacob Erlbecke25b5b92015-03-19 14:21:33 +0100351 /* Nothing left to send, create dummy LLC commands */
Jacob Erlbeck95340242015-03-19 13:22:07 +0100352 return create_new_bsn(fn, ts);
Daniel Willmannca102af2014-08-08 12:14:12 +0200353 }
354
Jacob Erlbeck95340242015-03-19 13:22:07 +0100355 /* If V(S) == V(A) and finished state, we would have received
356 * acknowledgement of all transmitted block. In this case we
357 * would have transmitted the final block, and received ack
358 * from MS. But in this case we did not receive the final ack
359 * indication from MS. This should never happen if MS works
360 * correctly. */
361 if (m_window.window_empty()) {
362 LOGP(DRLCMACDL, LOGL_DEBUG, "- MS acked all blocks, "
363 "so we re-transmit final block!\n");
364 /* we just send final block again */
365 int16_t index = m_window.v_s_mod(-1);
366 bts->rlc_resent();
367 return create_dl_acked_block(fn, ts, index);
368 }
369
370 /* cycle through all unacked blocks */
371 int resend = m_window.mark_for_resend();
372
373 /* At this point there should be at least one unacked block
374 * to be resent. If not, this is an software error. */
375 if (resend == 0) {
376 LOGP(DRLCMACDL, LOGL_ERROR, "Software error: "
377 "There are no unacknowledged blocks, but V(A) "
378 " != V(S). PLEASE FIX!\n");
379 /* we just send final block again */
380 int16_t index = m_window.v_s_mod(-1);
381 return create_dl_acked_block(fn, ts, index);
382 }
383 goto do_resend;
Daniel Willmannca102af2014-08-08 12:14:12 +0200384}
385
386struct msgb *gprs_rlcmac_dl_tbf::create_new_bsn(const uint32_t fn, const uint8_t ts)
387{
388 struct rlc_dl_header *rh;
389 struct rlc_li_field *li;
390 struct msgb *msg;
391 uint8_t *delimiter, *data, *e_pointer;
392 uint16_t space, chunk;
393 gprs_rlc_data *rlc_data;
Daniel Willmannca102af2014-08-08 12:14:12 +0200394 const uint16_t bsn = m_window.v_s();
395
396 LOGP(DRLCMACDL, LOGL_DEBUG, "- Sending new block at BSN %d\n",
397 m_window.v_s());
398
399#warning "Selection of the CS doesn't belong here"
400 if (cs == 0) {
401 cs = bts_data()->initial_cs_dl;
402 if (cs < 1 || cs > 4)
403 cs = 1;
404 }
405 /* total length of block, including spare bits */
406 const uint8_t block_length = gprs_rlcmac_cs[cs].block_length;
407 /* length of usable data of block, w/o spare bits, inc. MAC */
408 const uint8_t block_data_len = gprs_rlcmac_cs[cs].block_data;
409
410 /* now we still have untransmitted LLC data, so we fill mac block */
411 rlc_data = m_rlc.block(bsn);
412 data = rlc_data->prepare(block_data_len);
413
414 rh = (struct rlc_dl_header *)data;
415 rh->pt = 0; /* Data Block */
416 rh->rrbp = rh->s_p = 0; /* Polling, set later, if required */
417 rh->usf = 7; /* will be set at scheduler */
418 rh->pr = 0; /* FIXME: power reduction */
419 rh->tfi = m_tfi; /* TFI */
420 rh->fbi = 0; /* Final Block Indicator, set late, if true */
421 rh->bsn = bsn; /* Block Sequence Number */
422 rh->e = 0; /* Extension bit, maybe set later */
423 e_pointer = data + 2; /* points to E of current chunk */
424 data += sizeof(*rh);
425 delimiter = data; /* where next length header would be stored */
426 space = block_data_len - sizeof(*rh);
427 while (1) {
Jacob Erlbeckcbb1e702015-03-25 12:21:55 +0100428 if (m_llc.frame_length() == 0) {
429 /* A header will need to by added, so we just need
430 * space-1 octets */
431 m_llc.put_dummy_frame(space - 1);
432
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100433 /* The data just drained, store the current fn */
434 if (m_last_dl_drained_fn < 0)
435 m_last_dl_drained_fn = fn;
436
Jacob Erlbeckcbb1e702015-03-25 12:21:55 +0100437 /* It is not clear, when the next real data will
438 * arrive, so request a DL ack/nack now */
439 request_dl_ack();
440
441 LOGP(DRLCMACDL, LOGL_DEBUG,
442 "-- Empty chunk, "
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100443 "added LLC dummy command of size %d, "
444 "drained_since=%d\n",
445 m_llc.frame_length(), frames_since_last_drain(fn));
Jacob Erlbeckcbb1e702015-03-25 12:21:55 +0100446 }
447
Daniel Willmannca102af2014-08-08 12:14:12 +0200448 chunk = m_llc.chunk_size();
Jacob Erlbeckcbb1e702015-03-25 12:21:55 +0100449
Daniel Willmannca102af2014-08-08 12:14:12 +0200450 /* if chunk will exceed block limit */
451 if (chunk > space) {
452 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d "
453 "larger than space (%d) left in block: copy "
454 "only remaining space, and we are done\n",
455 chunk, space);
456 /* block is filled, so there is no extension */
457 *e_pointer |= 0x01;
458 /* fill only space */
459 m_llc.consume(data, space);
460 /* return data block as message */
461 break;
462 }
463 /* if FINAL chunk would fit precisely in space left */
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100464 if (chunk == space && llist_empty(&m_llc.queue) && !keep_open(fn))
465 {
Daniel Willmannca102af2014-08-08 12:14:12 +0200466 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d "
467 "would exactly fit into space (%d): because "
468 "this is a final block, we don't add length "
469 "header, and we are done\n", chunk, space);
470 LOGP(DRLCMACDL, LOGL_INFO, "Complete DL frame for "
471 "%s that fits precisely in last block: "
472 "len=%d\n", tbf_name(this), m_llc.frame_length());
473 gprs_rlcmac_dl_bw(this, m_llc.frame_length());
474 /* block is filled, so there is no extension */
475 *e_pointer |= 0x01;
476 /* fill space */
477 m_llc.consume(data, space);
478 m_llc.reset();
479 /* final block */
480 rh->fbi = 1; /* we indicate final block */
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100481 request_dl_ack();
Daniel Willmannca102af2014-08-08 12:14:12 +0200482 set_state(GPRS_RLCMAC_FINISHED);
483 /* return data block as message */
484 break;
485 }
486 /* if chunk would fit exactly in space left */
487 if (chunk == space) {
488 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d "
489 "would exactly fit into space (%d): add length "
490 "header with LI=0, to make frame extend to "
491 "next block, and we are done\n", chunk, space);
492 /* make space for delimiter */
493 if (delimiter != data)
494 memmove(delimiter + 1, delimiter,
495 data - delimiter);
496 data++;
497 space--;
498 /* add LI with 0 length */
499 li = (struct rlc_li_field *)delimiter;
500 li->e = 1; /* not more extension */
501 li->m = 0; /* shall be set to 0, in case of li = 0 */
502 li->li = 0; /* chunk fills the complete space */
503 // no need to set e_pointer nor increase delimiter
504 /* fill only space, which is 1 octet less than chunk */
505 m_llc.consume(data, space);
506 /* return data block as message */
507 break;
508 }
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100509
Daniel Willmannca102af2014-08-08 12:14:12 +0200510 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d is less "
511 "than remaining space (%d): add length header to "
512 "to delimit LLC frame\n", chunk, space);
513 /* the LLC frame chunk ends in this block */
514 /* make space for delimiter */
515 if (delimiter != data)
516 memmove(delimiter + 1, delimiter, data - delimiter);
517 data++;
518 space--;
519 /* add LI to delimit frame */
520 li = (struct rlc_li_field *)delimiter;
521 li->e = 0; /* Extension bit, maybe set later */
522 li->m = 0; /* will be set later, if there is more LLC data */
523 li->li = chunk; /* length of chunk */
524 e_pointer = delimiter; /* points to E of current delimiter */
525 delimiter++;
526 /* copy (rest of) LLC frame to space and reset later */
527 m_llc.consume(data, chunk);
528 data += chunk;
529 space -= chunk;
530 LOGP(DRLCMACDL, LOGL_INFO, "Complete DL frame for %s"
531 "len=%d\n", tbf_name(this), m_llc.frame_length());
532 gprs_rlcmac_dl_bw(this, m_llc.frame_length());
533 m_llc.reset();
534 /* dequeue next LLC frame, if any */
535 msg = llc_dequeue(gprs_bssgp_pcu_current_bctx());
536 if (msg) {
537 LOGP(DRLCMACDL, LOGL_INFO, "- Dequeue next LLC for "
538 "%s (len=%d)\n", tbf_name(this), msg->len);
539 m_llc.put_frame(msg->data, msg->len);
540 bts->llc_frame_sched();
541 msgb_free(msg);
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100542 m_last_dl_drained_fn = -1;
Daniel Willmannca102af2014-08-08 12:14:12 +0200543 }
544 /* if we have more data and we have space left */
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100545 if (space > 0 && (m_llc.frame_length() || keep_open(fn))) {
Daniel Willmannca102af2014-08-08 12:14:12 +0200546 li->m = 1; /* we indicate more frames to follow */
547 continue;
548 }
549 /* if we don't have more LLC frames */
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100550 if (!m_llc.frame_length() && !keep_open(fn)) {
Daniel Willmannca102af2014-08-08 12:14:12 +0200551 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Final block, so we "
552 "done.\n");
553 li->e = 1; /* we cannot extend */
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100554
Daniel Willmannca102af2014-08-08 12:14:12 +0200555 rh->fbi = 1; /* we indicate final block */
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100556 request_dl_ack();
Daniel Willmannca102af2014-08-08 12:14:12 +0200557 set_state(GPRS_RLCMAC_FINISHED);
558 break;
559 }
560 /* we have no space left */
561 LOGP(DRLCMACDL, LOGL_DEBUG, "-- No space left, so we are "
562 "done.\n");
563 li->e = 1; /* we cannot extend */
564 break;
565 }
566 LOGP(DRLCMACDL, LOGL_DEBUG, "data block: %s\n",
567 osmo_hexdump(rlc_data->block, block_length));
568#warning "move this up?"
569 rlc_data->len = block_length;
570 /* raise send state and set ack state array */
571 m_window.m_v_b.mark_unacked(bsn);
572 m_window.increment_send();
573
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100574 return create_dl_acked_block(fn, ts, bsn);
575}
576
Daniel Willmannca102af2014-08-08 12:14:12 +0200577struct msgb *gprs_rlcmac_dl_tbf::create_dl_acked_block(
578 const uint32_t fn, const uint8_t ts,
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100579 const int index)
Daniel Willmannca102af2014-08-08 12:14:12 +0200580{
581 uint8_t *data;
582 struct rlc_dl_header *rh;
583 struct msgb *dl_msg;
584 uint8_t len;
Daniel Willmannefd5dbb2014-08-25 16:20:23 +0200585 bool need_poll;
Daniel Willmannca102af2014-08-08 12:14:12 +0200586
587 /* get data and header from current block */
588 data = m_rlc.block(index)->block;
589 len = m_rlc.block(index)->len;
590 rh = (struct rlc_dl_header *)data;
591
Jacob Erlbeck005ee7f2015-03-20 14:53:54 +0100592 /* If the TBF has just started, relate frames_since_last_poll to the
593 * current fn */
594 if (m_last_dl_poll_fn < 0)
595 m_last_dl_poll_fn = fn;
596
Daniel Willmannefd5dbb2014-08-25 16:20:23 +0200597 need_poll = state_flags & (1 << GPRS_RLCMAC_FLAG_TO_DL_ACK);
Daniel Willmannca102af2014-08-08 12:14:12 +0200598 /* Clear Polling, if still set in history buffer */
599 rh->s_p = 0;
600
601 /* poll after POLL_ACK_AFTER_FRAMES frames, or when final block is tx.
602 */
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100603 if (m_tx_counter >= POLL_ACK_AFTER_FRAMES || m_dl_ack_requested ||
Daniel Willmannefd5dbb2014-08-25 16:20:23 +0200604 need_poll) {
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100605 if (m_dl_ack_requested) {
Daniel Willmannca102af2014-08-08 12:14:12 +0200606 LOGP(DRLCMACDL, LOGL_DEBUG, "- Scheduling Ack/Nack "
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100607 "polling, because is was requested explicitly "
608 "(e.g. first final block sent).\n");
Daniel Willmannefd5dbb2014-08-25 16:20:23 +0200609 } else if (need_poll) {
610 LOGP(DRLCMACDL, LOGL_DEBUG, "- Scheduling Ack/Nack "
Daniel Willmann635d47c2014-09-17 17:58:29 +0200611 "polling, because polling timed out.\n");
Daniel Willmannca102af2014-08-08 12:14:12 +0200612 } else {
613 LOGP(DRLCMACDL, LOGL_DEBUG, "- Scheduling Ack/Nack "
614 "polling, because %d blocks sent.\n",
615 POLL_ACK_AFTER_FRAMES);
616 }
617 /* scheduling not possible, because: */
618 if (poll_state != GPRS_RLCMAC_POLL_NONE)
619 LOGP(DRLCMACDL, LOGL_DEBUG, "Polling is already "
620 "sheduled for %s, so we must wait for "
621 "requesting downlink ack\n", tbf_name(this));
622 else if (control_ts != ts)
623 LOGP(DRLCMACDL, LOGL_DEBUG, "Polling cannot be "
624 "sheduled in this TS %d, waiting for "
625 "TS %d\n", ts, control_ts);
Daniel Willmannca102af2014-08-08 12:14:12 +0200626 else if (bts->sba()->find(trx->trx_no, ts, (fn + 13) % 2715648))
627 LOGP(DRLCMACDL, LOGL_DEBUG, "Polling cannot be "
628 "sheduled, because single block alllocation "
629 "already exists\n");
630 else {
631 LOGP(DRLCMACDL, LOGL_DEBUG, "Polling sheduled in this "
632 "TS %d\n", ts);
633 m_tx_counter = 0;
634 /* start timer whenever we send the final block */
635 if (rh->fbi == 1)
636 tbf_timer_start(this, 3191, bts_data()->t3191, 0);
637
638 /* schedule polling */
639 poll_state = GPRS_RLCMAC_POLL_SCHED;
640 poll_fn = (fn + 13) % 2715648;
641
Daniel Willmannefd5dbb2014-08-25 16:20:23 +0200642 /* Clear poll timeout flag */
643 state_flags &= ~(1 << GPRS_RLCMAC_FLAG_TO_DL_ACK);
644
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100645 /* Clear request flag */
646 m_dl_ack_requested = false;
647
Daniel Willmannca102af2014-08-08 12:14:12 +0200648 /* set polling in header */
649 rh->rrbp = 0; /* N+13 */
650 rh->s_p = 1; /* Polling */
Jacob Erlbeck005ee7f2015-03-20 14:53:54 +0100651
652 m_last_dl_poll_fn = poll_fn;
Daniel Willmannca102af2014-08-08 12:14:12 +0200653 }
654 }
655
656 /* return data block as message */
657 dl_msg = msgb_alloc(len, "rlcmac_dl_data");
658 if (!dl_msg)
659 return NULL;
660
661 /* Increment TX-counter */
662 m_tx_counter++;
663
664 memcpy(msgb_put(dl_msg, len), data, len);
665 bts->rlc_sent();
666
667 return dl_msg;
668}
669
670int gprs_rlcmac_dl_tbf::update_window(const uint8_t ssn, const uint8_t *rbb)
671{
672 int16_t dist; /* must be signed */
673 uint16_t lost = 0, received = 0;
674 char show_rbb[65];
675 char show_v_b[RLC_MAX_SNS + 1];
676 const uint16_t mod_sns = m_window.mod_sns();
677
678 Decoding::extract_rbb(rbb, show_rbb);
679 /* show received array in debug (bit 64..1) */
680 LOGP(DRLCMACDL, LOGL_DEBUG, "- ack: (BSN=%d)\"%s\""
681 "(BSN=%d) R=ACK I=NACK\n", (ssn - 64) & mod_sns,
682 show_rbb, (ssn - 1) & mod_sns);
683
684 /* apply received array to receive state (SSN-64..SSN-1) */
685 /* calculate distance of ssn from V(S) */
686 dist = (m_window.v_s() - ssn) & mod_sns;
687 /* check if distance is less than distance V(A)..V(S) */
688 if (dist >= m_window.distance()) {
689 /* this might happpen, if the downlink assignment
690 * was not received by ms and the ack refers
691 * to previous TBF
692 * FIXME: we should implement polling for
693 * control ack!*/
694 LOGP(DRLCMACDL, LOGL_NOTICE, "- ack range is out of "
695 "V(A)..V(S) range %s Free TBF!\n", tbf_name(this));
696 return 1; /* indicate to free TBF */
697 }
698
699 m_window.update(bts, show_rbb, ssn,
700 &lost, &received);
701
702 /* report lost and received packets */
703 gprs_rlcmac_received_lost(this, received, lost);
704
705 /* raise V(A), if possible */
706 m_window.raise(m_window.move_window());
707
708 /* show receive state array in debug (V(A)..V(S)-1) */
709 m_window.show_state(show_v_b);
710 LOGP(DRLCMACDL, LOGL_DEBUG, "- V(B): (V(A)=%d)\"%s\""
711 "(V(S)-1=%d) A=Acked N=Nacked U=Unacked "
712 "X=Resend-Unacked I=Invalid\n",
713 m_window.v_a(), show_v_b,
714 m_window.v_s_mod(-1));
715
716 if (state_is(GPRS_RLCMAC_FINISHED) && m_window.window_empty()) {
717 LOGP(DRLCMACDL, LOGL_NOTICE, "Received acknowledge of "
718 "all blocks, but without final ack "
719 "inidcation (don't worry)\n");
720 }
721 return 0;
722}
723
724
725int gprs_rlcmac_dl_tbf::maybe_start_new_window()
726{
727 struct msgb *msg;
728 uint16_t received;
729
730 LOGP(DRLCMACDL, LOGL_DEBUG, "- Final ACK received.\n");
731 /* range V(A)..V(S)-1 */
732 received = m_window.count_unacked();
733
734 /* report all outstanding packets as received */
735 gprs_rlcmac_received_lost(this, received, 0);
736
737 set_state(GPRS_RLCMAC_WAIT_RELEASE);
738
739 /* check for LLC PDU in the LLC Queue */
740 msg = llc_dequeue(gprs_bssgp_pcu_current_bctx());
741 if (!msg) {
742 /* no message, start T3193, change state to RELEASE */
743 LOGP(DRLCMACDL, LOGL_DEBUG, "- No new message, so we release.\n");
744 /* start T3193 */
745 tbf_timer_start(this, 3193,
746 bts_data()->t3193_msec / 1000,
747 (bts_data()->t3193_msec % 1000) * 1000);
748
749 return 0;
750 }
751
752 /* we have more data so we will re-use this tbf */
753 reuse_tbf(msg->data, msg->len);
754 msgb_free(msg);
755 return 0;
756}
757
758int gprs_rlcmac_dl_tbf::rcvd_dl_ack(uint8_t final_ack, uint8_t ssn, uint8_t *rbb)
759{
760 LOGP(DRLCMACDL, LOGL_DEBUG, "%s downlink acknowledge\n", tbf_name(this));
761
762 if (!final_ack)
763 return update_window(ssn, rbb);
764 return maybe_start_new_window();
765}
766
767void gprs_rlcmac_dl_tbf::reuse_tbf(const uint8_t *data, const uint16_t len)
768{
Daniel Willmanne4818152014-08-15 16:52:09 +0200769 uint8_t trx;
770 struct gprs_rlcmac_dl_tbf *new_tbf;
771 int8_t tfi; /* must be signed */
Daniel Willmanne4818152014-08-15 16:52:09 +0200772 struct msgb *msg;
773
Daniel Willmannca102af2014-08-08 12:14:12 +0200774 bts->tbf_reused();
Daniel Willmanne4818152014-08-15 16:52:09 +0200775
776 tfi = bts->tfi_find_free(GPRS_RLCMAC_DL_TBF, &trx, this->trx->trx_no);
777 if (tfi < 0) {
778 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource\n");
779 /* FIXME: send reject */
780 return;
781 }
782 new_tbf = tbf_alloc_dl_tbf(bts->bts_data(), NULL, tfi, trx, ms_class, 0);
783 if (!new_tbf) {
784 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource\n");
785 /* FIXME: send reject */
786 return;
787 }
788
789 new_tbf->m_tlli = m_tlli;
790 new_tbf->m_tlli_valid = m_tlli_valid;
791 new_tbf->ta = ta;
792 new_tbf->assign_imsi(m_imsi);
793
794 /* Copy over all data to the new TBF */
795 new_tbf->m_llc.put_frame(data, len);
Daniel Willmannca102af2014-08-08 12:14:12 +0200796 bts->llc_frame_sched();
797
Daniel Willmanne4818152014-08-15 16:52:09 +0200798 while ((msg = m_llc.dequeue()))
799 new_tbf->m_llc.enqueue(msg);
800
Daniel Willmannca102af2014-08-08 12:14:12 +0200801 /* reset rlc states */
802 m_tx_counter = 0;
803 m_wait_confirm = 0;
804 m_window.reset();
805
Jacob Erlbeck297edf72015-02-26 14:59:52 +0100806 /* mark TLLI as invalid */
807 m_tlli_valid = 0;
808
Daniel Willmannca102af2014-08-08 12:14:12 +0200809 /* keep to flags */
810 state_flags &= GPRS_RLCMAC_FLAG_TO_MASK;
811 state_flags &= ~(1 << GPRS_RLCMAC_FLAG_CCCH);
812
813 update();
814
815 LOGP(DRLCMAC, LOGL_DEBUG, "%s Trigger dowlink assignment on PACCH, "
816 "because another LLC PDU has arrived in between\n",
817 tbf_name(this));
Daniel Willmanne4818152014-08-15 16:52:09 +0200818 bts->trigger_dl_ass(new_tbf, this, NULL);
Daniel Willmannca102af2014-08-08 12:14:12 +0200819}
820
821bool gprs_rlcmac_dl_tbf::dl_window_stalled() const
822{
823 return m_window.window_stalled();
824}
825
Jacob Erlbeckeceb9102015-03-20 14:41:50 +0100826void gprs_rlcmac_dl_tbf::request_dl_ack()
827{
828 m_dl_ack_requested = true;
829}
830
831bool gprs_rlcmac_dl_tbf::need_control_ts() const
832{
833 if (poll_state != GPRS_RLCMAC_POLL_NONE)
834 return false;
835
836 return state_flags & (1 << GPRS_RLCMAC_FLAG_TO_DL_ACK) ||
837 m_tx_counter >= POLL_ACK_AFTER_FRAMES ||
838 m_dl_ack_requested;
839}
840
841bool gprs_rlcmac_dl_tbf::have_data() const
842{
843 return m_llc.chunk_size() > 0 || !llist_empty(&m_llc.queue);
844}
Jacob Erlbeck005ee7f2015-03-20 14:53:54 +0100845
846int gprs_rlcmac_dl_tbf::frames_since_last_poll(unsigned fn) const
847{
848 unsigned wrapped;
849 if (m_last_dl_poll_fn < 0)
850 return -1;
851
852 wrapped = (fn + 2715648 - m_last_dl_poll_fn) % 2715648;
853 if (wrapped < 2715648/2)
854 return wrapped;
855 else
856 return wrapped - 2715648;
857}
858
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100859int gprs_rlcmac_dl_tbf::frames_since_last_drain(unsigned fn) const
860{
861 unsigned wrapped;
862 if (m_last_dl_drained_fn < 0)
863 return -1;
864
865 wrapped = (fn + 2715648 - m_last_dl_drained_fn) % 2715648;
866 if (wrapped < 2715648/2)
867 return wrapped;
868 else
869 return wrapped - 2715648;
870}
871
872bool gprs_rlcmac_dl_tbf::keep_open(unsigned fn) const
873{
874 int keep_time_frames;
875
876 if (bts_data()->dl_tbf_idle_msec <= 0)
877 return false;
878
879 keep_time_frames = msecs_to_frames(bts_data()->dl_tbf_idle_msec);
880 return frames_since_last_drain(fn) <= keep_time_frames;
881}