blob: 64ebea4f102f68e3b194c1963250f32c5603d242 [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>
Jacob Erlbeckd4ad7312015-07-17 16:39:09 +020028#include <gprs_codel.h>
Daniel Willmannca102af2014-08-08 12:14:12 +020029#include <decoding.h>
Jacob Erlbeck3a3b6a72016-01-11 16:15:45 +010030#include <encoding.h>
Daniel Willmannca102af2014-08-08 12:14:12 +020031
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +010032#include "pcu_utils.h"
33
Daniel Willmannca102af2014-08-08 12:14:12 +020034extern "C" {
35#include <osmocom/core/msgb.h>
36#include <osmocom/core/talloc.h>
37}
38
39#include <errno.h>
40#include <string.h>
Jacob Erlbeckd4ad7312015-07-17 16:39:09 +020041#include <math.h>
Daniel Willmannca102af2014-08-08 12:14:12 +020042
43/* After sending these frames, we poll for ack/nack. */
44#define POLL_ACK_AFTER_FRAMES 20
45
Daniel Willmannca102af2014-08-08 12:14:12 +020046extern "C" {
47int bssgp_tx_llc_discarded(struct bssgp_bvc_ctx *bctx, uint32_t tlli,
48 uint8_t num_frames, uint32_t num_octets);
49}
50
51static inline void tbf_update_ms_class(struct gprs_rlcmac_tbf *tbf,
52 const uint8_t ms_class)
53{
Jacob Erlbeckbefc7602015-06-02 12:33:30 +020054 if (!tbf->ms_class() && ms_class)
55 tbf->set_ms_class(ms_class);
Daniel Willmannca102af2014-08-08 12:14:12 +020056}
57
Jacob Erlbeckd0261b72015-04-02 13:58:09 +020058static void llc_timer_cb(void *_tbf)
59{
60 struct gprs_rlcmac_dl_tbf *tbf = (struct gprs_rlcmac_dl_tbf *)_tbf;
61
62 if (tbf->state_is_not(GPRS_RLCMAC_FLOW))
63 return;
64
65 LOGP(DRLCMAC, LOGL_DEBUG,
66 "%s LLC receive timeout, requesting DL ACK\n", tbf_name(tbf));
67
68 tbf->request_dl_ack();
69}
70
71void gprs_rlcmac_dl_tbf::cleanup()
72{
73 osmo_timer_del(&m_llc_timer);
74}
75
76void gprs_rlcmac_dl_tbf::start_llc_timer()
77{
78 if (bts_data()->llc_idle_ack_csec > 0) {
79 struct timeval tv;
80
81 /* TODO: this ought to be within a constructor */
82 m_llc_timer.data = this;
83 m_llc_timer.cb = &llc_timer_cb;
84
85 csecs_to_timeval(bts_data()->llc_idle_ack_csec, &tv);
86 osmo_timer_schedule(&m_llc_timer, tv.tv_sec, tv.tv_usec);
87 }
88}
89
Daniel Willmannca102af2014-08-08 12:14:12 +020090int gprs_rlcmac_dl_tbf::append_data(const uint8_t ms_class,
91 const uint16_t pdu_delay_csec,
92 const uint8_t *data, const uint16_t len)
93{
94 LOGP(DRLCMAC, LOGL_INFO, "%s append\n", tbf_name(this));
Jacob Erlbeckb671dbf2015-06-15 14:32:33 +020095 gprs_llc_queue::MetaInfo info;
96 struct msgb *llc_msg = msgb_alloc(len, "llc_pdu_queue");
Jacob Erlbeck409efa12015-06-12 14:06:09 +020097 if (!llc_msg)
98 return -ENOMEM;
Jacob Erlbeckb671dbf2015-06-15 14:32:33 +020099
100 gprs_llc_queue::calc_pdu_lifetime(bts, pdu_delay_csec, &info.expire_time);
101 gettimeofday(&info.recv_time, NULL);
Jacob Erlbeck409efa12015-06-12 14:06:09 +0200102 memcpy(msgb_put(llc_msg, len), data, len);
Jacob Erlbeckb671dbf2015-06-15 14:32:33 +0200103 llc_queue()->enqueue(llc_msg, &info);
Jacob Erlbeck409efa12015-06-12 14:06:09 +0200104 tbf_update_ms_class(this, ms_class);
105 start_llc_timer();
106
Daniel Willmannca102af2014-08-08 12:14:12 +0200107 if (state_is(GPRS_RLCMAC_WAIT_RELEASE)) {
108 LOGP(DRLCMAC, LOGL_DEBUG,
109 "%s in WAIT RELEASE state "
110 "(T3193), so reuse TBF\n", tbf_name(this));
111 tbf_update_ms_class(this, ms_class);
Jacob Erlbeck91ff7d12015-09-01 11:20:29 +0200112 establish_dl_tbf_on_pacch();
Daniel Willmannca102af2014-08-08 12:14:12 +0200113 }
114
115 return 0;
116}
117
Daniel Willmannca102af2014-08-08 12:14:12 +0200118static int tbf_new_dl_assignment(struct gprs_rlcmac_bts *bts,
119 const char *imsi,
Jacob Erlbeck93990462015-05-15 15:50:43 +0200120 const uint32_t tlli, const uint32_t tlli_old,
121 const uint8_t ms_class,
Jacob Erlbeck86b6f052015-11-27 15:17:34 +0100122 const uint8_t egprs_ms_class,
Jacob Erlbeck409efa12015-06-12 14:06:09 +0200123 struct gprs_rlcmac_dl_tbf **tbf)
Daniel Willmannca102af2014-08-08 12:14:12 +0200124{
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200125 uint8_t ss;
Daniel Willmannca102af2014-08-08 12:14:12 +0200126 int8_t use_trx;
Jacob Erlbeck9200ce62015-05-22 17:48:04 +0200127 uint16_t ta = 0;
Jacob Erlbecke43460b2015-05-13 13:33:12 +0200128 struct gprs_rlcmac_ul_tbf *ul_tbf = NULL, *old_ul_tbf;
Jacob Erlbeck90de3a72015-04-09 19:18:59 +0200129 struct gprs_rlcmac_dl_tbf *dl_tbf = NULL;
Jacob Erlbecke43460b2015-05-13 13:33:12 +0200130 GprsMs *ms;
Daniel Willmannca102af2014-08-08 12:14:12 +0200131
132 /* check for uplink data, so we copy our informations */
133#warning "Do the same look up for IMSI, TLLI and OLD_TLLI"
134#warning "Refactor the below lines... into a new method"
Jacob Erlbeck93990462015-05-15 15:50:43 +0200135 ms = bts->bts->ms_store().get_ms(tlli, tlli_old, imsi);
Jacob Erlbeck9200ce62015-05-22 17:48:04 +0200136 if (ms) {
Jacob Erlbecke43460b2015-05-13 13:33:12 +0200137 ul_tbf = ms->ul_tbf();
Jacob Erlbeck9200ce62015-05-22 17:48:04 +0200138 ta = ms->ta();
139 }
Jacob Erlbecke2e004e2015-06-18 17:16:26 +0200140 /* TODO: if (!ms) create MS before tbf_alloc is called? */
Jacob Erlbecke43460b2015-05-13 13:33:12 +0200141
Daniel Willmannca102af2014-08-08 12:14:12 +0200142 if (ul_tbf && ul_tbf->m_contention_resolution_done
143 && !ul_tbf->m_final_ack_sent) {
144 use_trx = ul_tbf->trx->trx_no;
Daniel Willmannca102af2014-08-08 12:14:12 +0200145 ss = 0;
146 old_ul_tbf = ul_tbf;
147 } else {
148 use_trx = -1;
Daniel Willmannca102af2014-08-08 12:14:12 +0200149 ss = 1; /* PCH assignment only allows one timeslot */
150 old_ul_tbf = NULL;
151 }
152
153 // Create new TBF (any TRX)
154#warning "Copy and paste with alloc_ul_tbf"
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200155 /* set number of downlink slots according to multislot class */
Jacob Erlbeck86b6f052015-11-27 15:17:34 +0100156 dl_tbf = tbf_alloc_dl_tbf(bts, ms, use_trx, ms_class, egprs_ms_class, ss);
Jacob Erlbeck90de3a72015-04-09 19:18:59 +0200157
Daniel Willmannca102af2014-08-08 12:14:12 +0200158 if (!dl_tbf) {
159 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource\n");
Daniel Willmannca102af2014-08-08 12:14:12 +0200160 return -EBUSY;
161 }
Jacob Erlbeckbe0cbc12015-05-18 14:35:11 +0200162 dl_tbf->update_ms(tlli, GPRS_RLCMAC_DL_TBF);
Jacob Erlbeck9200ce62015-05-22 17:48:04 +0200163 dl_tbf->ms()->set_ta(ta);
Daniel Willmannca102af2014-08-08 12:14:12 +0200164
165 LOGP(DRLCMAC, LOGL_DEBUG, "%s [DOWNLINK] START\n", tbf_name(dl_tbf));
166
Daniel Willmannca102af2014-08-08 12:14:12 +0200167 /* 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. */
Jacob Erlbeck71e55112015-05-21 11:10:15 +0200174 dl_tbf->bts->trigger_dl_ass(dl_tbf, old_ul_tbf);
Jacob Erlbeck409efa12015-06-12 14:06:09 +0200175 *tbf = dl_tbf;
Daniel Willmannca102af2014-08-08 12:14:12 +0200176 return 0;
177}
178
179/**
180 * TODO: split into unit test-able parts...
181 */
182int gprs_rlcmac_dl_tbf::handle(struct gprs_rlcmac_bts *bts,
Jacob Erlbeck93990462015-05-15 15:50:43 +0200183 const uint32_t tlli, const uint32_t tlli_old, const char *imsi,
Jacob Erlbeckf4bb4242016-01-20 13:30:34 +0100184 uint8_t ms_class, uint8_t egprs_ms_class,
Jacob Erlbeck14e00f82015-11-27 18:10:39 +0100185 const uint16_t delay_csec,
Daniel Willmannca102af2014-08-08 12:14:12 +0200186 const uint8_t *data, const uint16_t len)
187{
Jacob Erlbeckac289052015-08-14 12:50:54 +0200188 struct gprs_rlcmac_dl_tbf *dl_tbf = NULL;
Jacob Erlbeck409efa12015-06-12 14:06:09 +0200189 int rc;
Jacob Erlbeckac289052015-08-14 12:50:54 +0200190 GprsMs *ms, *ms_old;
Daniel Willmannca102af2014-08-08 12:14:12 +0200191
192 /* check for existing TBF */
Jacob Erlbeckac289052015-08-14 12:50:54 +0200193 ms = bts->bts->ms_store().get_ms(tlli, tlli_old, imsi);
Jacob Erlbeckf4bb4242016-01-20 13:30:34 +0100194 if (ms) {
Jacob Erlbeckac289052015-08-14 12:50:54 +0200195 dl_tbf = ms->dl_tbf();
196
Jacob Erlbeckf4bb4242016-01-20 13:30:34 +0100197 /* If we known the GPRS/EGPRS MS class, use it */
198 if (ms->ms_class() || ms->egprs_ms_class()) {
199 ms_class = ms->ms_class();
200 egprs_ms_class = ms->egprs_ms_class();
201 }
202 }
Jacob Erlbecka47aaa42016-01-07 16:47:34 +0100203
Jacob Erlbeckac289052015-08-14 12:50:54 +0200204 if (ms && strlen(ms->imsi()) == 0) {
205 ms_old = bts->bts->ms_store().get_ms(0, 0, imsi);
206 if (ms_old && ms_old != ms) {
207 /* The TLLI has changed (RAU), so there are two MS
208 * objects for the same MS */
209 LOGP(DRLCMAC, LOGL_NOTICE,
210 "There is a new MS object for the same MS: "
211 "(0x%08x, '%s') -> (0x%08x, '%s')\n",
212 ms_old->tlli(), ms_old->imsi(),
213 ms->tlli(), ms->imsi());
214
215 GprsMs::Guard guard_old(ms_old);
216
217 if (!dl_tbf && ms_old->dl_tbf()) {
218 LOGP(DRLCMAC, LOGL_NOTICE,
219 "%s IMSI %s: "
220 "moving DL TBF to new MS object\n",
221 dl_tbf->name(), imsi);
222 dl_tbf = ms_old->dl_tbf();
223 /* Move the DL TBF to the new MS */
224 dl_tbf->set_ms(ms);
225 }
226 /* Clean up the old MS object */
227 /* TODO: Put this into a separate function, use timer? */
228 if (ms_old->ul_tbf() && ms_old->ul_tbf()->T == 0)
229 tbf_free(ms_old->ul_tbf());
230 if (ms_old->dl_tbf() && ms_old->dl_tbf()->T == 0)
231 tbf_free(ms_old->dl_tbf());
232
Jacob Erlbeck2b349b52015-08-18 11:55:03 +0200233 ms->merge_old_ms(ms_old);
Jacob Erlbeckac289052015-08-14 12:50:54 +0200234 }
235 }
236
Jacob Erlbeck409efa12015-06-12 14:06:09 +0200237 if (!dl_tbf) {
Jacob Erlbeck86b6f052015-11-27 15:17:34 +0100238 rc = tbf_new_dl_assignment(bts, imsi, tlli, tlli_old,
Jacob Erlbeck14e00f82015-11-27 18:10:39 +0100239 ms_class, egprs_ms_class, &dl_tbf);
Jacob Erlbeck409efa12015-06-12 14:06:09 +0200240 if (rc < 0)
241 return rc;
242 }
Jacob Erlbeck93990462015-05-15 15:50:43 +0200243
Jacob Erlbeck14e00f82015-11-27 18:10:39 +0100244 /* TODO: ms_class vs. egprs_ms_class is not handled here */
Jacob Erlbeck409efa12015-06-12 14:06:09 +0200245 rc = dl_tbf->append_data(ms_class, delay_csec, data, len);
Jacob Erlbeckaf9a39d2015-08-16 21:27:34 +0200246 dl_tbf->update_ms(tlli, GPRS_RLCMAC_DL_TBF);
Jacob Erlbeckac289052015-08-14 12:50:54 +0200247 dl_tbf->assign_imsi(imsi);
Jacob Erlbeck409efa12015-06-12 14:06:09 +0200248
249 return rc;
Daniel Willmannca102af2014-08-08 12:14:12 +0200250}
251
252struct msgb *gprs_rlcmac_dl_tbf::llc_dequeue(bssgp_bvc_ctx *bctx)
253{
254 struct msgb *msg;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200255 struct timeval tv_now, tv_now2;
Daniel Willmannca102af2014-08-08 12:14:12 +0200256 uint32_t octets = 0, frames = 0;
Jacob Erlbeck0c1c8772015-03-20 12:02:42 +0100257 struct timeval hyst_delta = {0, 0};
258 const unsigned keep_small_thresh = 60;
Jacob Erlbeckb671dbf2015-06-15 14:32:33 +0200259 const gprs_llc_queue::MetaInfo *info;
Jacob Erlbeck0c1c8772015-03-20 12:02:42 +0100260
261 if (bts_data()->llc_discard_csec)
262 csecs_to_timeval(bts_data()->llc_discard_csec, &hyst_delta);
Daniel Willmannca102af2014-08-08 12:14:12 +0200263
264 gettimeofday(&tv_now, NULL);
Jacob Erlbeck0c1c8772015-03-20 12:02:42 +0100265 timeradd(&tv_now, &hyst_delta, &tv_now2);
Daniel Willmannca102af2014-08-08 12:14:12 +0200266
Jacob Erlbeckb671dbf2015-06-15 14:32:33 +0200267 while ((msg = llc_queue()->dequeue(&info))) {
268 const struct timeval *tv_disc = &info->expire_time;
269 const struct timeval *tv_recv = &info->recv_time;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200270
271 gprs_bssgp_update_queue_delay(tv_recv, &tv_now);
Daniel Willmannca102af2014-08-08 12:14:12 +0200272
Jacob Erlbeckd4ad7312015-07-17 16:39:09 +0200273 if (ms() && ms()->codel_state()) {
274 int bytes = llc_queue()->octets();
275 if (gprs_codel_control(ms()->codel_state(),
276 tv_recv, &tv_now, bytes))
277 goto drop_frame;
278 }
279
Jacob Erlbeck0c1c8772015-03-20 12:02:42 +0100280 /* Is the age below the low water mark? */
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +0200281 if (!gprs_llc_queue::is_frame_expired(&tv_now2, tv_disc))
Jacob Erlbeck0c1c8772015-03-20 12:02:42 +0100282 break;
283
284 /* Is the age below the high water mark */
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +0200285 if (!gprs_llc_queue::is_frame_expired(&tv_now, tv_disc)) {
Jacob Erlbeck0c1c8772015-03-20 12:02:42 +0100286 /* Has the previous message not been dropped? */
287 if (frames == 0)
288 break;
289
290 /* Hysteresis mode, try to discard LLC messages until
291 * the low water mark has been reached */
292
293 /* Check whether to abort the hysteresis mode */
294
295 /* Is the frame small, perhaps only a TCP ACK? */
296 if (msg->len <= keep_small_thresh)
297 break;
298
299 /* Is it a GMM message? */
300 if (!gprs_llc::is_user_data_frame(msg->data, msg->len))
301 break;
Daniel Willmannca102af2014-08-08 12:14:12 +0200302 }
Jacob Erlbeck0c1c8772015-03-20 12:02:42 +0100303
304 bts->llc_timedout_frame();
Jacob Erlbeckd4ad7312015-07-17 16:39:09 +0200305drop_frame:
Jacob Erlbeck0c1c8772015-03-20 12:02:42 +0100306 frames++;
307 octets += msg->len;
308 msgb_free(msg);
Jacob Erlbeck29d91e92015-04-29 14:46:52 +0200309 bts->llc_dropped_frame();
Jacob Erlbeck0c1c8772015-03-20 12:02:42 +0100310 continue;
Daniel Willmannca102af2014-08-08 12:14:12 +0200311 }
312
313 if (frames) {
Jacob Erlbeck0c1c8772015-03-20 12:02:42 +0100314 LOGP(DRLCMACDL, LOGL_NOTICE, "%s Discarding LLC PDU "
315 "because lifetime limit reached, "
316 "count=%u new_queue_size=%zu\n",
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +0200317 tbf_name(this), frames, llc_queue()->size());
Daniel Willmannca102af2014-08-08 12:14:12 +0200318 if (frames > 0xff)
319 frames = 0xff;
320 if (octets > 0xffffff)
321 octets = 0xffffff;
Jacob Erlbeck6e013a12015-08-21 15:10:23 +0200322 if (bctx)
323 bssgp_tx_llc_discarded(bctx, tlli(), frames, octets);
Daniel Willmannca102af2014-08-08 12:14:12 +0200324 }
325
326 return msg;
327}
328
329/*
330 * Create DL data block
331 * The messages are fragmented and forwarded as data blocks.
332 */
333struct msgb *gprs_rlcmac_dl_tbf::create_dl_acked_block(uint32_t fn, uint8_t ts)
334{
335 LOGP(DRLCMACDL, LOGL_DEBUG, "%s downlink (V(A)==%d .. "
336 "V(S)==%d)\n", tbf_name(this),
337 m_window.v_a(), m_window.v_s());
338
339do_resend:
340 /* check if there is a block with negative acknowledgement */
341 int resend_bsn = m_window.resend_needed();
342 if (resend_bsn >= 0) {
343 LOGP(DRLCMACDL, LOGL_DEBUG, "- Resending BSN %d\n", resend_bsn);
344 /* re-send block with negative aknowlegement */
345 m_window.m_v_b.mark_unacked(resend_bsn);
346 bts->rlc_resent();
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100347 return create_dl_acked_block(fn, ts, resend_bsn);
Daniel Willmannca102af2014-08-08 12:14:12 +0200348 }
349
350 /* if the window has stalled, or transfer is complete,
351 * send an unacknowledged block */
Jacob Erlbeck95340242015-03-19 13:22:07 +0100352 if (state_is(GPRS_RLCMAC_FINISHED)) {
353 LOGP(DRLCMACDL, LOGL_DEBUG, "- Restarting at BSN %d, "
354 "because all blocks have been transmitted.\n",
355 m_window.v_a());
356 bts->rlc_restarted();
357 } else if (dl_window_stalled()) {
358 LOGP(DRLCMACDL, LOGL_NOTICE, "- Restarting at BSN %d, "
359 "because all window is stalled.\n",
360 m_window.v_a());
361 bts->rlc_stalled();
Jacob Erlbecke25b5b92015-03-19 14:21:33 +0100362 } else if (have_data()) {
363 /* New blocks may be send */
364 return create_new_bsn(fn, ts);
365 } else if (!m_window.window_empty()) {
366 LOGP(DRLCMACDL, LOGL_DEBUG, "- Restarting at BSN %d, "
367 "because all blocks have been transmitted (FLOW).\n",
368 m_window.v_a());
369 bts->rlc_restarted();
Jacob Erlbeck95340242015-03-19 13:22:07 +0100370 } else {
Jacob Erlbecke25b5b92015-03-19 14:21:33 +0100371 /* Nothing left to send, create dummy LLC commands */
Jacob Erlbeck95340242015-03-19 13:22:07 +0100372 return create_new_bsn(fn, ts);
Daniel Willmannca102af2014-08-08 12:14:12 +0200373 }
374
Jacob Erlbeck95340242015-03-19 13:22:07 +0100375 /* If V(S) == V(A) and finished state, we would have received
376 * acknowledgement of all transmitted block. In this case we
377 * would have transmitted the final block, and received ack
378 * from MS. But in this case we did not receive the final ack
379 * indication from MS. This should never happen if MS works
380 * correctly. */
381 if (m_window.window_empty()) {
382 LOGP(DRLCMACDL, LOGL_DEBUG, "- MS acked all blocks, "
383 "so we re-transmit final block!\n");
384 /* we just send final block again */
385 int16_t index = m_window.v_s_mod(-1);
386 bts->rlc_resent();
387 return create_dl_acked_block(fn, ts, index);
388 }
389
390 /* cycle through all unacked blocks */
391 int resend = m_window.mark_for_resend();
392
393 /* At this point there should be at least one unacked block
394 * to be resent. If not, this is an software error. */
395 if (resend == 0) {
396 LOGP(DRLCMACDL, LOGL_ERROR, "Software error: "
397 "There are no unacknowledged blocks, but V(A) "
398 " != V(S). PLEASE FIX!\n");
399 /* we just send final block again */
400 int16_t index = m_window.v_s_mod(-1);
401 return create_dl_acked_block(fn, ts, index);
402 }
403 goto do_resend;
Daniel Willmannca102af2014-08-08 12:14:12 +0200404}
405
Jacob Erlbeck409efa12015-06-12 14:06:09 +0200406void gprs_rlcmac_dl_tbf::schedule_next_frame()
407{
408 struct msgb *msg;
409
410 if (m_llc.frame_length() != 0)
411 return;
412
413 /* dequeue next LLC frame, if any */
414 msg = llc_dequeue(gprs_bssgp_pcu_current_bctx());
415 if (!msg)
416 return;
417
418 LOGP(DRLCMACDL, LOGL_INFO,
419 "- Dequeue next LLC for %s (len=%d)\n",
420 tbf_name(this), msg->len);
421
422 m_llc.put_frame(msg->data, msg->len);
423 bts->llc_frame_sched();
424 msgb_free(msg);
425 m_last_dl_drained_fn = -1;
426}
427
Daniel Willmannca102af2014-08-08 12:14:12 +0200428struct msgb *gprs_rlcmac_dl_tbf::create_new_bsn(const uint32_t fn, const uint8_t ts)
429{
Jacob Erlbeckfec94d12016-01-12 17:02:49 +0100430 uint8_t *data;
Daniel Willmannca102af2014-08-08 12:14:12 +0200431 gprs_rlc_data *rlc_data;
Daniel Willmannca102af2014-08-08 12:14:12 +0200432 const uint16_t bsn = m_window.v_s();
Jacob Erlbeckcb728902016-01-05 15:33:03 +0100433 GprsCodingScheme cs = current_cs();
Jacob Erlbeck3a3b6a72016-01-11 16:15:45 +0100434 gprs_rlc_data_block_info *rdbi;
Jacob Erlbeckfec94d12016-01-12 17:02:49 +0100435 int num_chunks = 0;
436 int write_offset = 0;
437 Encoding::AppendResult ar;
Daniel Willmannca102af2014-08-08 12:14:12 +0200438
Jacob Erlbeck409efa12015-06-12 14:06:09 +0200439 if (m_llc.frame_length() == 0)
440 schedule_next_frame();
441
Jacob Erlbeckcb728902016-01-05 15:33:03 +0100442 LOGP(DRLCMACDL, LOGL_DEBUG, "- Sending new block at BSN %d, CS=%s\n",
443 m_window.v_s(), cs.name());
Jacob Erlbecka700dd92015-06-02 16:00:41 +0200444
Jacob Erlbeckcb728902016-01-05 15:33:03 +0100445 OSMO_ASSERT(cs.isValid());
Jacob Erlbecka700dd92015-06-02 16:00:41 +0200446
Jacob Erlbeck3a3b6a72016-01-11 16:15:45 +0100447 /* length of usable data block (single data unit w/o header) */
448 const uint8_t block_data_len = cs.maxDataBlockBytes();
Daniel Willmannca102af2014-08-08 12:14:12 +0200449
450 /* now we still have untransmitted LLC data, so we fill mac block */
451 rlc_data = m_rlc.block(bsn);
452 data = rlc_data->prepare(block_data_len);
Jacob Erlbeck1751c622015-06-04 12:12:32 +0200453 rlc_data->cs = cs;
Jacob Erlbeck3a3b6a72016-01-11 16:15:45 +0100454 rlc_data->len = block_data_len;
Daniel Willmannca102af2014-08-08 12:14:12 +0200455
Jacob Erlbeck3a3b6a72016-01-11 16:15:45 +0100456 rdbi = &(rlc_data->block_info);
457 memset(rdbi, 0, sizeof(*rdbi));
458 rdbi->data_len = block_data_len;
459
460 rdbi->cv = 15; /* Final Block Indicator, set late, if true */
461 rdbi->bsn = bsn; /* Block Sequence Number */
462 rdbi->e = 1; /* Extension bit, maybe set later (1: no extension) */
463
Jacob Erlbeckfec94d12016-01-12 17:02:49 +0100464 do {
465 bool is_final;
466
Jacob Erlbeckcbb1e702015-03-25 12:21:55 +0100467 if (m_llc.frame_length() == 0) {
Jacob Erlbeckfec94d12016-01-12 17:02:49 +0100468 int space = block_data_len - write_offset;
Jacob Erlbeckcbb1e702015-03-25 12:21:55 +0100469 /* A header will need to by added, so we just need
470 * space-1 octets */
471 m_llc.put_dummy_frame(space - 1);
472
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100473 /* The data just drained, store the current fn */
474 if (m_last_dl_drained_fn < 0)
475 m_last_dl_drained_fn = fn;
476
Jacob Erlbeckcbb1e702015-03-25 12:21:55 +0100477 /* It is not clear, when the next real data will
478 * arrive, so request a DL ack/nack now */
479 request_dl_ack();
480
481 LOGP(DRLCMACDL, LOGL_DEBUG,
482 "-- Empty chunk, "
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100483 "added LLC dummy command of size %d, "
484 "drained_since=%d\n",
485 m_llc.frame_length(), frames_since_last_drain(fn));
Jacob Erlbeckcbb1e702015-03-25 12:21:55 +0100486 }
487
Jacob Erlbeckfec94d12016-01-12 17:02:49 +0100488 is_final = llc_queue()->size() == 0 && !keep_open(fn);
Jacob Erlbeckcbb1e702015-03-25 12:21:55 +0100489
Jacob Erlbeck5058bd62016-01-13 10:51:25 +0100490 ar = Encoding::rlc_data_to_dl_append(rdbi, cs,
Jacob Erlbeckfec94d12016-01-12 17:02:49 +0100491 &m_llc, &write_offset, &num_chunks, data, is_final);
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100492
Jacob Erlbeckfec94d12016-01-12 17:02:49 +0100493 if (ar == Encoding::AR_NEED_MORE_BLOCKS)
494 break;
495
Daniel Willmannca102af2014-08-08 12:14:12 +0200496 LOGP(DRLCMACDL, LOGL_INFO, "Complete DL frame for %s"
497 "len=%d\n", tbf_name(this), m_llc.frame_length());
498 gprs_rlcmac_dl_bw(this, m_llc.frame_length());
499 m_llc.reset();
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100500
Jacob Erlbeckfec94d12016-01-12 17:02:49 +0100501 if (is_final) {
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100502 request_dl_ack();
Daniel Willmannca102af2014-08-08 12:14:12 +0200503 set_state(GPRS_RLCMAC_FINISHED);
Daniel Willmannca102af2014-08-08 12:14:12 +0200504 }
Jacob Erlbeckfec94d12016-01-12 17:02:49 +0100505
506 /* dequeue next LLC frame, if any */
507 schedule_next_frame();
508 } while (ar == Encoding::AR_COMPLETED_SPACE_LEFT);
509
Jacob Erlbeck3a3b6a72016-01-11 16:15:45 +0100510 LOGP(DRLCMACDL, LOGL_DEBUG, "data block (BSN %d, %s): %s\n",
511 bsn, rlc_data->cs.name(),
512 osmo_hexdump(rlc_data->block, block_data_len));
Daniel Willmannca102af2014-08-08 12:14:12 +0200513 /* raise send state and set ack state array */
514 m_window.m_v_b.mark_unacked(bsn);
515 m_window.increment_send();
516
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100517 return create_dl_acked_block(fn, ts, bsn);
518}
519
Daniel Willmannca102af2014-08-08 12:14:12 +0200520struct msgb *gprs_rlcmac_dl_tbf::create_dl_acked_block(
521 const uint32_t fn, const uint8_t ts,
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100522 const int index)
Daniel Willmannca102af2014-08-08 12:14:12 +0200523{
Jacob Erlbeck3a3b6a72016-01-11 16:15:45 +0100524 uint8_t *block_data, *msg_data;
Daniel Willmannca102af2014-08-08 12:14:12 +0200525 struct msgb *dl_msg;
Jacob Erlbeck3a3b6a72016-01-11 16:15:45 +0100526 unsigned msg_len;
Daniel Willmannefd5dbb2014-08-25 16:20:23 +0200527 bool need_poll;
Jacob Erlbeck3a3b6a72016-01-11 16:15:45 +0100528 /* TODO: support MCS-7 - MCS-9, where data_block_idx can be 1 */
529 unsigned int data_block_idx = 0;
530
531 gprs_rlc_data_info rlc;
532 GprsCodingScheme cs;
533 gprs_rlc_data_block_info *rdbi;
Daniel Willmannca102af2014-08-08 12:14:12 +0200534
535 /* get data and header from current block */
Jacob Erlbeck3a3b6a72016-01-11 16:15:45 +0100536 block_data = m_rlc.block(index)->block;
537
538 cs = m_rlc.block(index)->cs;
539
540 gprs_rlc_data_info_init_dl(&rlc, cs);
541
542 rlc.usf = 7; /* will be set at scheduler */
543 rlc.pr = 0; /* FIXME: power reduction */
544 rlc.tfi = m_tfi; /* TFI */
Jacob Erlbeck70955c72016-01-13 13:09:09 +0100545 rlc.cps = gprs_rlc_mcs_cps(cs, 0, 0);
Jacob Erlbeck3a3b6a72016-01-11 16:15:45 +0100546
547 rlc.block_info[data_block_idx] = m_rlc.block(index)->block_info;
548 rdbi = &rlc.block_info[data_block_idx];
Daniel Willmannca102af2014-08-08 12:14:12 +0200549
Jacob Erlbeck005ee7f2015-03-20 14:53:54 +0100550 /* If the TBF has just started, relate frames_since_last_poll to the
551 * current fn */
552 if (m_last_dl_poll_fn < 0)
553 m_last_dl_poll_fn = fn;
554
Daniel Willmannefd5dbb2014-08-25 16:20:23 +0200555 need_poll = state_flags & (1 << GPRS_RLCMAC_FLAG_TO_DL_ACK);
Jacob Erlbeck3a3b6a72016-01-11 16:15:45 +0100556
Daniel Willmannca102af2014-08-08 12:14:12 +0200557 /* poll after POLL_ACK_AFTER_FRAMES frames, or when final block is tx.
558 */
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100559 if (m_tx_counter >= POLL_ACK_AFTER_FRAMES || m_dl_ack_requested ||
Daniel Willmannefd5dbb2014-08-25 16:20:23 +0200560 need_poll) {
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100561 if (m_dl_ack_requested) {
Daniel Willmannca102af2014-08-08 12:14:12 +0200562 LOGP(DRLCMACDL, LOGL_DEBUG, "- Scheduling Ack/Nack "
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100563 "polling, because is was requested explicitly "
564 "(e.g. first final block sent).\n");
Daniel Willmannefd5dbb2014-08-25 16:20:23 +0200565 } else if (need_poll) {
566 LOGP(DRLCMACDL, LOGL_DEBUG, "- Scheduling Ack/Nack "
Daniel Willmann635d47c2014-09-17 17:58:29 +0200567 "polling, because polling timed out.\n");
Daniel Willmannca102af2014-08-08 12:14:12 +0200568 } else {
569 LOGP(DRLCMACDL, LOGL_DEBUG, "- Scheduling Ack/Nack "
570 "polling, because %d blocks sent.\n",
571 POLL_ACK_AFTER_FRAMES);
572 }
573 /* scheduling not possible, because: */
574 if (poll_state != GPRS_RLCMAC_POLL_NONE)
575 LOGP(DRLCMACDL, LOGL_DEBUG, "Polling is already "
576 "sheduled for %s, so we must wait for "
577 "requesting downlink ack\n", tbf_name(this));
578 else if (control_ts != ts)
579 LOGP(DRLCMACDL, LOGL_DEBUG, "Polling cannot be "
580 "sheduled in this TS %d, waiting for "
581 "TS %d\n", ts, control_ts);
Daniel Willmannca102af2014-08-08 12:14:12 +0200582 else if (bts->sba()->find(trx->trx_no, ts, (fn + 13) % 2715648))
583 LOGP(DRLCMACDL, LOGL_DEBUG, "Polling cannot be "
584 "sheduled, because single block alllocation "
585 "already exists\n");
586 else {
587 LOGP(DRLCMACDL, LOGL_DEBUG, "Polling sheduled in this "
588 "TS %d\n", ts);
589 m_tx_counter = 0;
590 /* start timer whenever we send the final block */
Jacob Erlbeck3a3b6a72016-01-11 16:15:45 +0100591 if (rdbi->cv == 0)
Daniel Willmannca102af2014-08-08 12:14:12 +0200592 tbf_timer_start(this, 3191, bts_data()->t3191, 0);
593
594 /* schedule polling */
595 poll_state = GPRS_RLCMAC_POLL_SCHED;
596 poll_fn = (fn + 13) % 2715648;
597
Daniel Willmannefd5dbb2014-08-25 16:20:23 +0200598 /* Clear poll timeout flag */
599 state_flags &= ~(1 << GPRS_RLCMAC_FLAG_TO_DL_ACK);
600
Jacob Erlbeck7c444152015-03-12 12:08:54 +0100601 /* Clear request flag */
602 m_dl_ack_requested = false;
603
Daniel Willmannca102af2014-08-08 12:14:12 +0200604 /* set polling in header */
Jacob Erlbeck3a3b6a72016-01-11 16:15:45 +0100605 rlc.rrbp = 0; /* N+13 */
606 rlc.es_p = 1; /* Polling */
Jacob Erlbeck005ee7f2015-03-20 14:53:54 +0100607
608 m_last_dl_poll_fn = poll_fn;
Jacob Erlbeck16d29c72015-08-24 13:30:39 +0200609
610 LOGP(DRLCMACDL, LOGL_INFO,
611 "%s Scheduled Ack/Nack polling on FN=%d, TS=%d\n",
612 name(), poll_fn, ts);
Daniel Willmannca102af2014-08-08 12:14:12 +0200613 }
614 }
615
Jacob Erlbeck3a3b6a72016-01-11 16:15:45 +0100616 msg_len = cs.sizeDL();
617
Daniel Willmannca102af2014-08-08 12:14:12 +0200618 /* return data block as message */
Jacob Erlbeck3a3b6a72016-01-11 16:15:45 +0100619 dl_msg = msgb_alloc(msg_len, "rlcmac_dl_data");
Daniel Willmannca102af2014-08-08 12:14:12 +0200620 if (!dl_msg)
621 return NULL;
622
Jacob Erlbeck3a3b6a72016-01-11 16:15:45 +0100623 msg_data = msgb_put(dl_msg, msg_len);
624 Encoding::rlc_write_dl_data_header(&rlc, msg_data);
625 Encoding::rlc_copy_from_aligned_buffer(&rlc, data_block_idx, msg_data,
626 block_data);
627
628 LOGP(DRLCMACDL, LOGL_DEBUG, "msg block (BSN %d, %s): %s\n",
629 index, cs.name(),
630 msgb_hexdump(dl_msg));
631
Daniel Willmannca102af2014-08-08 12:14:12 +0200632 /* Increment TX-counter */
633 m_tx_counter++;
634
Daniel Willmannca102af2014-08-08 12:14:12 +0200635 bts->rlc_sent();
636
637 return dl_msg;
638}
639
Jacob Erlbeck93c55d02015-12-23 16:29:07 +0100640static uint16_t bitnum_to_bsn(int bitnum, uint16_t ssn)
Jacob Erlbeck1751c622015-06-04 12:12:32 +0200641{
Jacob Erlbeck93c55d02015-12-23 16:29:07 +0100642 return ssn - 1 - bitnum;
Jacob Erlbeck1751c622015-06-04 12:12:32 +0200643}
644
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200645int gprs_rlcmac_dl_tbf::analyse_errors(char *show_rbb, uint8_t ssn,
646 ana_result *res)
Jacob Erlbeck1751c622015-06-04 12:12:32 +0200647{
648 gprs_rlc_data *rlc_data;
649 uint16_t lost = 0, received = 0, skipped = 0;
Jacob Erlbeckb41262f2016-02-05 17:13:24 +0100650 char info[RLC_MAX_WS + 1];
651 memset(info, '.', m_window.ws());
652 info[m_window.ws()] = 0;
Jacob Erlbecka17fccb2015-06-04 16:52:23 +0200653 uint16_t bsn = 0;
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200654 unsigned received_bytes = 0, lost_bytes = 0;
655 unsigned received_packets = 0, lost_packets = 0;
Jacob Erlbeckb41262f2016-02-05 17:13:24 +0100656 unsigned num_blocks = strlen(show_rbb);
Jacob Erlbeck1751c622015-06-04 12:12:32 +0200657
658 /* SSN - 1 is in range V(A)..V(S)-1 */
Jacob Erlbeckb41262f2016-02-05 17:13:24 +0100659 for (unsigned int bitpos = 0; bitpos < num_blocks; bitpos++) {
660 bool is_received;
661 int index = num_blocks - 1 - bitpos;
662
663 is_received = (index >= 0 && show_rbb[index] == 'R');
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200664
Jacob Erlbeck93c55d02015-12-23 16:29:07 +0100665 bsn = m_window.mod_sns(bitnum_to_bsn(bitpos, ssn));
Jacob Erlbeck1751c622015-06-04 12:12:32 +0200666
Jacob Erlbeck93c55d02015-12-23 16:29:07 +0100667 if (bsn == m_window.mod_sns(m_window.v_a() - 1)) {
Jacob Erlbecka17fccb2015-06-04 16:52:23 +0200668 info[bitpos] = '$';
Jacob Erlbeck1751c622015-06-04 12:12:32 +0200669 break;
Jacob Erlbecka17fccb2015-06-04 16:52:23 +0200670 }
Jacob Erlbeck1751c622015-06-04 12:12:32 +0200671
672 rlc_data = m_rlc.block(bsn);
Jacob Erlbecka17fccb2015-06-04 16:52:23 +0200673 if (!rlc_data) {
674 info[bitpos] = '0';
Jacob Erlbeck1751c622015-06-04 12:12:32 +0200675 continue;
Jacob Erlbecka17fccb2015-06-04 16:52:23 +0200676 }
Jacob Erlbeck1751c622015-06-04 12:12:32 +0200677
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200678 /* Get general statistics */
679 if (is_received && !m_window.m_v_b.is_acked(bsn)) {
680 received_packets += 1;
681 received_bytes += rlc_data->len;
682 } else if (!is_received && !m_window.m_v_b.is_nacked(bsn)) {
683 lost_packets += 1;
684 lost_bytes += rlc_data->len;
685 }
686
687 /* Get statistics for current CS */
688
Jacob Erlbeckcb728902016-01-05 15:33:03 +0100689 if (rlc_data->cs != current_cs()) {
Jacob Erlbeck1751c622015-06-04 12:12:32 +0200690 /* This block has already been encoded with a different
691 * CS, so it doesn't help us to decide, whether the
692 * current CS is ok. Ignore it. */
Jacob Erlbecka17fccb2015-06-04 16:52:23 +0200693 info[bitpos] = 'x';
Jacob Erlbeck1751c622015-06-04 12:12:32 +0200694 skipped += 1;
695 continue;
696 }
697
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200698 if (is_received) {
Jacob Erlbecka17fccb2015-06-04 16:52:23 +0200699 if (!m_window.m_v_b.is_acked(bsn)) {
Jacob Erlbeck1751c622015-06-04 12:12:32 +0200700 received += 1;
Jacob Erlbecka17fccb2015-06-04 16:52:23 +0200701 info[bitpos] = 'R';
702 } else {
703 info[bitpos] = 'r';
704 }
Jacob Erlbeck1751c622015-06-04 12:12:32 +0200705 } else {
Jacob Erlbecka17fccb2015-06-04 16:52:23 +0200706 info[bitpos] = 'L';
Jacob Erlbeck1751c622015-06-04 12:12:32 +0200707 lost += 1;
708 }
709 }
710
Jacob Erlbecka17fccb2015-06-04 16:52:23 +0200711 LOGP(DRLCMACDL, LOGL_DEBUG, "%s DL analysis, range=%d:%d, lost=%d, recv=%d, "
712 "skipped=%d, bsn=%d, info='%s'\n",
713 name(), m_window.v_a(), m_window.v_s(), lost, received,
714 skipped, bsn, info);
Jacob Erlbeck1751c622015-06-04 12:12:32 +0200715
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200716 res->received_packets = received_packets;
717 res->lost_packets = lost_packets;
718 res->received_bytes = received_bytes;
719 res->lost_bytes = lost_bytes;
720
Jacob Erlbeck144a1d02015-06-04 16:51:44 +0200721 if (lost + received <= 1)
Jacob Erlbeck1751c622015-06-04 12:12:32 +0200722 return -1;
723
724 return lost * 100 / (lost + received);
725}
726
Jacob Erlbeckeb08f862016-02-05 17:07:12 +0100727int gprs_rlcmac_dl_tbf::update_window(unsigned first_bsn,
728 const struct bitvec *rbb)
729{
730 int16_t dist; /* must be signed */
731 uint16_t lost = 0, received = 0;
732 char show_v_b[RLC_MAX_SNS + 1];
733 char show_rbb[RLC_MAX_SNS + 1];
734 int error_rate;
735 struct ana_result ana_res;
736 unsigned num_blocks = rbb->cur_bit;
737 unsigned behind_last_bsn = m_window.mod_sns(first_bsn + num_blocks);
738
739 Decoding::extract_rbb(rbb, show_rbb);
740 /* show received array in debug */
741 LOGP(DRLCMACDL, LOGL_DEBUG, "- ack: (BSN=%d)\"%s\""
742 "(BSN=%d) R=ACK I=NACK\n", first_bsn,
743 show_rbb, m_window.mod_sns(behind_last_bsn - 1));
744
745 /* apply received array to receive state (first_bsn..behind_last_bsn-1) */
746 if (num_blocks > 0) {
747 /* calculate distance of ssn from V(S) */
748 dist = m_window.mod_sns(m_window.v_s() - behind_last_bsn);
749 /* check if distance is less than distance V(A)..V(S) */
750 if (dist >= m_window.distance()) {
751 /* this might happpen, if the downlink assignment
752 * was not received by ms and the ack refers
753 * to previous TBF
754 * FIXME: we should implement polling for
755 * control ack!
756 * TODO: check whether this FIXME still makes sense
757 */
758 LOGP(DRLCMACDL, LOGL_NOTICE, "- ack range is out of "
759 "V(A)..V(S) range %s Free TBF!\n", tbf_name(this));
760 return 1; /* indicate to free TBF */
761 }
762 }
763
764 error_rate = analyse_errors(show_rbb, behind_last_bsn, &ana_res);
765
766 if (bts_data()->cs_adj_enabled && ms())
767 ms()->update_error_rate(this, error_rate);
768
769 m_window.update(bts, rbb, first_bsn, &lost, &received);
770
771 /* report lost and received packets */
772 gprs_rlcmac_received_lost(this, received, lost);
773
774 /* Used to measure the leak rate */
775 gprs_bssgp_update_bytes_received(ana_res.received_bytes,
776 ana_res.received_packets + ana_res.lost_packets);
777
778 /* raise V(A), if possible */
779 m_window.raise(m_window.move_window());
780
781 /* show receive state array in debug (V(A)..V(S)-1) */
782 m_window.show_state(show_v_b);
783 LOGP(DRLCMACDL, LOGL_DEBUG, "- V(B): (V(A)=%d)\"%s\""
784 "(V(S)-1=%d) A=Acked N=Nacked U=Unacked "
785 "X=Resend-Unacked I=Invalid\n",
786 m_window.v_a(), show_v_b,
787 m_window.v_s_mod(-1));
Jacob Erlbeckeb08f862016-02-05 17:07:12 +0100788 return 0;
789}
Jacob Erlbeck1751c622015-06-04 12:12:32 +0200790
Daniel Willmannca102af2014-08-08 12:14:12 +0200791int gprs_rlcmac_dl_tbf::update_window(const uint8_t ssn, const uint8_t *rbb)
792{
793 int16_t dist; /* must be signed */
794 uint16_t lost = 0, received = 0;
795 char show_rbb[65];
796 char show_v_b[RLC_MAX_SNS + 1];
Jacob Erlbeck1751c622015-06-04 12:12:32 +0200797 int error_rate;
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200798 struct ana_result ana_res;
Daniel Willmannca102af2014-08-08 12:14:12 +0200799
800 Decoding::extract_rbb(rbb, show_rbb);
801 /* show received array in debug (bit 64..1) */
802 LOGP(DRLCMACDL, LOGL_DEBUG, "- ack: (BSN=%d)\"%s\""
Jacob Erlbeck93c55d02015-12-23 16:29:07 +0100803 "(BSN=%d) R=ACK I=NACK\n", m_window.mod_sns(ssn - 64),
804 show_rbb, m_window.mod_sns(ssn - 1));
Daniel Willmannca102af2014-08-08 12:14:12 +0200805
806 /* apply received array to receive state (SSN-64..SSN-1) */
807 /* calculate distance of ssn from V(S) */
Jacob Erlbeck93c55d02015-12-23 16:29:07 +0100808 dist = m_window.mod_sns(m_window.v_s() - ssn);
Daniel Willmannca102af2014-08-08 12:14:12 +0200809 /* check if distance is less than distance V(A)..V(S) */
810 if (dist >= m_window.distance()) {
811 /* this might happpen, if the downlink assignment
812 * was not received by ms and the ack refers
813 * to previous TBF
814 * FIXME: we should implement polling for
815 * control ack!*/
816 LOGP(DRLCMACDL, LOGL_NOTICE, "- ack range is out of "
817 "V(A)..V(S) range %s Free TBF!\n", tbf_name(this));
818 return 1; /* indicate to free TBF */
819 }
820
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200821 error_rate = analyse_errors(show_rbb, ssn, &ana_res);
822
823 if (bts_data()->cs_adj_enabled && ms())
Jacob Erlbeck1751c622015-06-04 12:12:32 +0200824 ms()->update_error_rate(this, error_rate);
Jacob Erlbeck1751c622015-06-04 12:12:32 +0200825
Daniel Willmannca102af2014-08-08 12:14:12 +0200826 m_window.update(bts, show_rbb, ssn,
827 &lost, &received);
828
829 /* report lost and received packets */
830 gprs_rlcmac_received_lost(this, received, lost);
831
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200832 /* Used to measure the leak rate */
833 gprs_bssgp_update_bytes_received(ana_res.received_bytes,
834 ana_res.received_packets + ana_res.lost_packets);
835
Daniel Willmannca102af2014-08-08 12:14:12 +0200836 /* raise V(A), if possible */
837 m_window.raise(m_window.move_window());
838
839 /* show receive state array in debug (V(A)..V(S)-1) */
840 m_window.show_state(show_v_b);
841 LOGP(DRLCMACDL, LOGL_DEBUG, "- V(B): (V(A)=%d)\"%s\""
842 "(V(S)-1=%d) A=Acked N=Nacked U=Unacked "
843 "X=Resend-Unacked I=Invalid\n",
844 m_window.v_a(), show_v_b,
845 m_window.v_s_mod(-1));
846
847 if (state_is(GPRS_RLCMAC_FINISHED) && m_window.window_empty()) {
848 LOGP(DRLCMACDL, LOGL_NOTICE, "Received acknowledge of "
849 "all blocks, but without final ack "
850 "inidcation (don't worry)\n");
851 }
852 return 0;
853}
854
855
856int gprs_rlcmac_dl_tbf::maybe_start_new_window()
857{
Jacob Erlbeck91ff7d12015-09-01 11:20:29 +0200858 release();
859
860 /* check for LLC PDU in the LLC Queue */
861 if (llc_queue()->size() > 0)
862 /* we have more data so we will re-use this tbf */
863 establish_dl_tbf_on_pacch();
864
865 return 0;
866}
867
868int gprs_rlcmac_dl_tbf::release()
869{
Daniel Willmannca102af2014-08-08 12:14:12 +0200870 uint16_t received;
871
Daniel Willmannca102af2014-08-08 12:14:12 +0200872 /* range V(A)..V(S)-1 */
873 received = m_window.count_unacked();
874
875 /* report all outstanding packets as received */
876 gprs_rlcmac_received_lost(this, received, 0);
877
878 set_state(GPRS_RLCMAC_WAIT_RELEASE);
879
Jacob Erlbeck04e72d32015-08-13 18:36:56 +0200880 /* start T3193 */
881 tbf_timer_start(this, 3193,
882 bts_data()->t3193_msec / 1000,
883 (bts_data()->t3193_msec % 1000) * 1000);
884
Daniel Willmannca102af2014-08-08 12:14:12 +0200885 /* reset rlc states */
886 m_tx_counter = 0;
887 m_wait_confirm = 0;
888 m_window.reset();
889
890 /* keep to flags */
891 state_flags &= GPRS_RLCMAC_FLAG_TO_MASK;
892 state_flags &= ~(1 << GPRS_RLCMAC_FLAG_CCCH);
893
Jacob Erlbeck91ff7d12015-09-01 11:20:29 +0200894 return 0;
895}
896
Jacob Erlbeckf04a5b32016-01-21 20:42:40 +0100897int gprs_rlcmac_dl_tbf::abort()
898{
899 uint16_t lost;
900
901 if (state_is(GPRS_RLCMAC_FLOW)) {
902 /* range V(A)..V(S)-1 */
903 lost = m_window.count_unacked();
904
905 /* report all outstanding packets as lost */
906 gprs_rlcmac_received_lost(this, 0, lost);
907 gprs_rlcmac_lost_rep(this);
908
909 /* TODO: Reschedule all LLC frames starting with the one that is
910 * (partly) encoded in chunk 1 of block V(A). (optional) */
911 }
912
913 set_state(GPRS_RLCMAC_RELEASING);
914
915 /* reset rlc states */
916 m_window.reset();
917
918 /* keep to flags */
919 state_flags &= GPRS_RLCMAC_FLAG_TO_MASK;
920 state_flags &= ~(1 << GPRS_RLCMAC_FLAG_CCCH);
921
922 return 0;
923}
Jacob Erlbeck91ff7d12015-09-01 11:20:29 +0200924
Jacob Erlbeckeb08f862016-02-05 17:07:12 +0100925int gprs_rlcmac_dl_tbf::rcvd_dl_ack(uint8_t final_ack, unsigned first_bsn,
926 struct bitvec *rbb)
927{
Jacob Erlbeckb4beb542016-01-15 13:38:31 +0100928 int rc;
Jacob Erlbeckeb08f862016-02-05 17:07:12 +0100929 LOGP(DRLCMACDL, LOGL_DEBUG, "%s downlink acknowledge\n", tbf_name(this));
930
Jacob Erlbeckb4beb542016-01-15 13:38:31 +0100931 rc = update_window(first_bsn, rbb);
Jacob Erlbeckeb08f862016-02-05 17:07:12 +0100932
Jacob Erlbeckb4beb542016-01-15 13:38:31 +0100933 if (final_ack) {
934 LOGP(DRLCMACDL, LOGL_DEBUG, "- Final ACK received.\n");
935 rc = maybe_start_new_window();
936 } else if (state_is(GPRS_RLCMAC_FINISHED) && m_window.window_empty()) {
937 LOGP(DRLCMACDL, LOGL_NOTICE, "Received acknowledge of "
938 "all blocks, but without final ack "
939 "indication (don't worry)\n");
940 }
941
942 return rc;
Jacob Erlbeckeb08f862016-02-05 17:07:12 +0100943}
944
Jacob Erlbeck91ff7d12015-09-01 11:20:29 +0200945int gprs_rlcmac_dl_tbf::rcvd_dl_ack(uint8_t final_ack, uint8_t ssn, uint8_t *rbb)
946{
947 LOGP(DRLCMACDL, LOGL_DEBUG, "%s downlink acknowledge\n", tbf_name(this));
948
949 if (!final_ack)
950 return update_window(ssn, rbb);
951
952 LOGP(DRLCMACDL, LOGL_DEBUG, "- Final ACK received.\n");
953 return maybe_start_new_window();
Daniel Willmannca102af2014-08-08 12:14:12 +0200954}
955
956bool gprs_rlcmac_dl_tbf::dl_window_stalled() const
957{
958 return m_window.window_stalled();
959}
960
Jacob Erlbeckeceb9102015-03-20 14:41:50 +0100961void gprs_rlcmac_dl_tbf::request_dl_ack()
962{
963 m_dl_ack_requested = true;
964}
965
966bool gprs_rlcmac_dl_tbf::need_control_ts() const
967{
968 if (poll_state != GPRS_RLCMAC_POLL_NONE)
969 return false;
970
971 return state_flags & (1 << GPRS_RLCMAC_FLAG_TO_DL_ACK) ||
972 m_tx_counter >= POLL_ACK_AFTER_FRAMES ||
973 m_dl_ack_requested;
974}
975
976bool gprs_rlcmac_dl_tbf::have_data() const
977{
Jacob Erlbeck489a2b32015-05-28 19:07:01 +0200978 return m_llc.chunk_size() > 0 ||
979 (llc_queue() && llc_queue()->size() > 0);
Jacob Erlbeckeceb9102015-03-20 14:41:50 +0100980}
Jacob Erlbeck005ee7f2015-03-20 14:53:54 +0100981
982int gprs_rlcmac_dl_tbf::frames_since_last_poll(unsigned fn) const
983{
984 unsigned wrapped;
985 if (m_last_dl_poll_fn < 0)
986 return -1;
987
988 wrapped = (fn + 2715648 - m_last_dl_poll_fn) % 2715648;
989 if (wrapped < 2715648/2)
990 return wrapped;
991 else
992 return wrapped - 2715648;
993}
994
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +0100995int gprs_rlcmac_dl_tbf::frames_since_last_drain(unsigned fn) const
996{
997 unsigned wrapped;
998 if (m_last_dl_drained_fn < 0)
999 return -1;
1000
1001 wrapped = (fn + 2715648 - m_last_dl_drained_fn) % 2715648;
1002 if (wrapped < 2715648/2)
1003 return wrapped;
1004 else
1005 return wrapped - 2715648;
1006}
1007
1008bool gprs_rlcmac_dl_tbf::keep_open(unsigned fn) const
1009{
1010 int keep_time_frames;
1011
1012 if (bts_data()->dl_tbf_idle_msec <= 0)
1013 return false;
1014
1015 keep_time_frames = msecs_to_frames(bts_data()->dl_tbf_idle_msec);
1016 return frames_since_last_drain(fn) <= keep_time_frames;
1017}