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