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