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 <encoding.h> |
| 26 | #include <gprs_rlcmac.h> |
| 27 | #include <gprs_debug.h> |
| 28 | #include <gprs_bssgp_pcu.h> |
| 29 | #include <decoding.h> |
Jacob Erlbeck | 20f6fd1 | 2015-06-08 11:05:45 +0200 | [diff] [blame] | 30 | #include <pcu_l1_if.h> |
Daniel Willmann | ca102af | 2014-08-08 12:14:12 +0200 | [diff] [blame] | 31 | |
| 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 receiving these frames, we send ack/nack. */ |
| 41 | #define SEND_ACK_AFTER_FRAMES 20 |
| 42 | |
| 43 | extern void *tall_pcu_ctx; |
| 44 | |
Jacob Erlbeck | b3100e1 | 2015-12-14 13:36:13 +0100 | [diff] [blame] | 45 | /* |
| 46 | * Store received block data in LLC message(s) and forward to SGSN |
| 47 | * if complete. |
| 48 | */ |
| 49 | int gprs_rlcmac_ul_tbf::assemble_forward_llc(const gprs_rlc_data *_data) |
| 50 | { |
| 51 | const uint8_t *data = _data->block; |
| 52 | uint8_t len = _data->len; |
Jacob Erlbeck | f2ba4cb | 2016-01-07 18:59:28 +0100 | [diff] [blame] | 53 | const struct gprs_rlc_data_block_info *rdbi = &_data->block_info; |
Jacob Erlbeck | b3100e1 | 2015-12-14 13:36:13 +0100 | [diff] [blame] | 54 | GprsCodingScheme cs = _data->cs; |
| 55 | |
| 56 | Decoding::RlcData frames[16], *frame; |
| 57 | int i, num_frames = 0; |
| 58 | uint32_t dummy_tlli; |
| 59 | |
| 60 | LOGP(DRLCMACUL, LOGL_DEBUG, "- Assembling frames: (len=%d)\n", len); |
| 61 | |
| 62 | num_frames = Decoding::rlc_data_from_ul_data( |
| 63 | rdbi, cs, data, &(frames[0]), sizeof(frames), |
| 64 | &dummy_tlli); |
| 65 | |
| 66 | /* create LLC frames */ |
| 67 | for (i = 0; i < num_frames; i++) { |
| 68 | frame = frames + i; |
| 69 | |
| 70 | LOGP(DRLCMACUL, LOGL_DEBUG, "-- Frame %d starts at offset %d, " |
| 71 | "length=%d, is_complete=%d\n", |
| 72 | i + 1, frame->offset, frame->length, frame->is_complete); |
| 73 | |
| 74 | m_llc.append_frame(data + frame->offset, frame->length); |
| 75 | m_llc.consume(frame->length); |
| 76 | |
| 77 | if (frame->is_complete) { |
| 78 | /* send frame to SGSN */ |
| 79 | LOGP(DRLCMACUL, LOGL_INFO, "%s complete UL frame len=%d\n", |
| 80 | tbf_name(this) , m_llc.frame_length()); |
| 81 | snd_ul_ud(); |
| 82 | m_llc.reset(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
Daniel Willmann | ca102af | 2014-08-08 12:14:12 +0200 | [diff] [blame] | 89 | |
| 90 | struct msgb *gprs_rlcmac_ul_tbf::create_ul_ack(uint32_t fn) |
| 91 | { |
| 92 | int final = (state_is(GPRS_RLCMAC_FINISHED)); |
| 93 | struct msgb *msg; |
| 94 | |
| 95 | if (final) { |
| 96 | if (poll_state != GPRS_RLCMAC_POLL_NONE) { |
| 97 | LOGP(DRLCMACUL, LOGL_DEBUG, "Polling is already " |
| 98 | "sheduled for %s, so we must wait for " |
| 99 | "final uplink ack...\n", tbf_name(this)); |
| 100 | return NULL; |
| 101 | } |
| 102 | if (bts->sba()->find(trx->trx_no, control_ts, (fn + 13) % 2715648)) { |
| 103 | LOGP(DRLCMACUL, LOGL_DEBUG, "Polling is already " |
| 104 | "scheduled for single block allocation...\n"); |
| 105 | return NULL; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | msg = msgb_alloc(23, "rlcmac_ul_ack"); |
| 110 | if (!msg) |
| 111 | return NULL; |
| 112 | bitvec *ack_vec = bitvec_alloc(23); |
| 113 | if (!ack_vec) { |
| 114 | msgb_free(msg); |
| 115 | return NULL; |
| 116 | } |
| 117 | bitvec_unhex(ack_vec, |
| 118 | "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b"); |
Jacob Erlbeck | a24e1cd | 2015-12-22 14:59:13 +0100 | [diff] [blame] | 119 | Encoding::write_packet_uplink_ack(bts_data(), ack_vec, this, final); |
Daniel Willmann | ca102af | 2014-08-08 12:14:12 +0200 | [diff] [blame] | 120 | bitvec_pack(ack_vec, msgb_put(msg, 23)); |
| 121 | bitvec_free(ack_vec); |
Daniel Willmann | ca102af | 2014-08-08 12:14:12 +0200 | [diff] [blame] | 122 | |
| 123 | /* now we must set this flag, so we are allowed to assign downlink |
| 124 | * TBF on PACCH. it is only allowed when TLLI is acknowledged. */ |
| 125 | m_contention_resolution_done = 1; |
| 126 | |
| 127 | if (final) { |
| 128 | poll_state = GPRS_RLCMAC_POLL_SCHED; |
| 129 | poll_fn = (fn + 13) % 2715648; |
| 130 | /* waiting for final acknowledge */ |
| 131 | ul_ack_state = GPRS_RLCMAC_UL_ACK_WAIT_ACK; |
| 132 | m_final_ack_sent = 1; |
| 133 | } else |
| 134 | ul_ack_state = GPRS_RLCMAC_UL_ACK_NONE; |
| 135 | |
| 136 | return msg; |
| 137 | } |
| 138 | |
Jacob Erlbeck | b3100e1 | 2015-12-14 13:36:13 +0100 | [diff] [blame] | 139 | int gprs_rlcmac_ul_tbf::rcv_data_block_acknowledged( |
Jacob Erlbeck | f2ba4cb | 2016-01-07 18:59:28 +0100 | [diff] [blame] | 140 | const struct gprs_rlc_data_info *rlc, |
Jacob Erlbeck | fc1b3e6 | 2016-01-11 09:58:11 +0100 | [diff] [blame] | 141 | uint8_t *data, struct pcu_l1_meas *meas) |
Jacob Erlbeck | b3100e1 | 2015-12-14 13:36:13 +0100 | [diff] [blame] | 142 | { |
| 143 | int8_t rssi = meas->have_rssi ? meas->rssi : 0; |
| 144 | |
Jacob Erlbeck | b3100e1 | 2015-12-14 13:36:13 +0100 | [diff] [blame] | 145 | const uint16_t ws = m_window.ws(); |
| 146 | |
| 147 | this->state_flags |= (1 << GPRS_RLCMAC_FLAG_UL_DATA); |
| 148 | |
| 149 | LOGP(DRLCMACUL, LOGL_DEBUG, "UL DATA TFI=%d received (V(Q)=%d .. " |
| 150 | "V(R)=%d)\n", rlc->tfi, this->m_window.v_q(), |
| 151 | this->m_window.v_r()); |
| 152 | |
| 153 | /* process RSSI */ |
| 154 | gprs_rlcmac_rssi(this, rssi); |
| 155 | |
| 156 | /* store measurement values */ |
| 157 | if (ms()) |
| 158 | ms()->update_l1_meas(meas); |
| 159 | |
| 160 | uint32_t new_tlli = 0; |
| 161 | unsigned int block_idx; |
| 162 | |
| 163 | /* restart T3169 */ |
| 164 | tbf_timer_start(this, 3169, bts_data()->t3169, 0); |
| 165 | |
| 166 | /* Increment RX-counter */ |
| 167 | this->m_rx_counter++; |
| 168 | |
| 169 | /* Loop over num_blocks */ |
| 170 | for (block_idx = 0; block_idx < rlc->num_data_blocks; block_idx++) { |
| 171 | int num_chunks; |
| 172 | uint8_t *rlc_data; |
Jacob Erlbeck | f2ba4cb | 2016-01-07 18:59:28 +0100 | [diff] [blame] | 173 | const struct gprs_rlc_data_block_info *rdbi = |
Jacob Erlbeck | b3100e1 | 2015-12-14 13:36:13 +0100 | [diff] [blame] | 174 | &rlc->block_info[block_idx]; |
| 175 | bool need_rlc_data = false; |
| 176 | struct gprs_rlc_data *block; |
| 177 | |
| 178 | LOGP(DRLCMACUL, LOGL_DEBUG, |
| 179 | "%s: Got %s RLC data block: " |
| 180 | "CV=%d, BSN=%d, SPB=%d, " |
| 181 | "PI=%d, E=%d, TI=%d, bitoffs=%d\n", |
| 182 | name(), rlc->cs.name(), |
| 183 | rdbi->cv, rdbi->bsn, rdbi->spb, |
| 184 | rdbi->pi, rdbi->e, rdbi->ti, |
| 185 | rlc->data_offs_bits[block_idx]); |
| 186 | |
| 187 | /* Check whether the block needs to be decoded */ |
| 188 | |
| 189 | if (!m_window.is_in_window(rdbi->bsn)) { |
| 190 | LOGP(DRLCMACUL, LOGL_DEBUG, "- BSN %d out of window " |
| 191 | "%d..%d (it's normal)\n", rdbi->bsn, |
| 192 | m_window.v_q(), |
Jacob Erlbeck | 93c55d0 | 2015-12-23 16:29:07 +0100 | [diff] [blame] | 193 | m_window.mod_sns(m_window.v_q() + ws - 1)); |
Jacob Erlbeck | b3100e1 | 2015-12-14 13:36:13 +0100 | [diff] [blame] | 194 | } else if (m_window.is_received(rdbi->bsn)) { |
| 195 | LOGP(DRLCMACUL, LOGL_DEBUG, |
| 196 | "- BSN %d already received\n", rdbi->bsn); |
| 197 | } else { |
| 198 | need_rlc_data = true; |
| 199 | } |
| 200 | |
| 201 | if (!is_tlli_valid()) { |
| 202 | if (!rdbi->ti) { |
| 203 | LOGP(DRLCMACUL, LOGL_NOTICE, |
| 204 | "%s: Missing TLLI within UL DATA.\n", |
| 205 | name()); |
| 206 | continue; |
| 207 | } |
| 208 | need_rlc_data = true; |
| 209 | } |
| 210 | |
| 211 | if (!need_rlc_data) |
| 212 | continue; |
| 213 | |
| 214 | /* Store block and meta info to BSN buffer */ |
| 215 | |
| 216 | LOGP(DRLCMACUL, LOGL_DEBUG, "- BSN %d storing in window (%d..%d)\n", |
| 217 | rdbi->bsn, m_window.v_q(), |
Jacob Erlbeck | 93c55d0 | 2015-12-23 16:29:07 +0100 | [diff] [blame] | 218 | m_window.mod_sns(m_window.v_q() + ws - 1)); |
Jacob Erlbeck | b3100e1 | 2015-12-14 13:36:13 +0100 | [diff] [blame] | 219 | block = m_rlc.block(rdbi->bsn); |
| 220 | block->block_info = *rdbi; |
| 221 | block->cs = rlc->cs; |
| 222 | OSMO_ASSERT(rdbi->data_len < sizeof(block->block)); |
| 223 | rlc_data = &(block->block[0]); |
| 224 | /* TODO: Handle SPB != 0 -> Set length to 2*len, add offset if |
| 225 | * 2nd part. Note that resegmentation is currently disabled |
| 226 | * within the UL assignment. |
| 227 | */ |
| 228 | if (rdbi->spb) { |
| 229 | LOGP(DRLCMACUL, LOGL_NOTICE, |
| 230 | "Got SPB != 0 but resegmentation has been " |
| 231 | "disabled, skipping %s data block with BSN %d, " |
| 232 | "TFI=%d.\n", rlc->cs.name(), rdbi->bsn, |
| 233 | rlc->tfi); |
| 234 | continue; |
| 235 | } |
| 236 | |
| 237 | block->len = |
| 238 | Decoding::rlc_copy_to_aligned_buffer(rlc, block_idx, data, |
| 239 | rlc_data); |
| 240 | |
Jacob Erlbeck | d88bb2e | 2015-12-11 14:53:29 +0100 | [diff] [blame] | 241 | LOGP(DRLCMACUL, LOGL_DEBUG, |
| 242 | "%s: data_length=%d, data=%s\n", |
| 243 | name(), block->len, osmo_hexdump(rlc_data, block->len)); |
Jacob Erlbeck | b3100e1 | 2015-12-14 13:36:13 +0100 | [diff] [blame] | 244 | |
| 245 | /* TODO: Handle SPB != 0 -> set state to partly received |
| 246 | * (upper/lower) and continue with the loop, unless the other |
| 247 | * part is already present. |
| 248 | */ |
| 249 | |
| 250 | /* Get/Handle TLLI */ |
| 251 | if (rdbi->ti) { |
| 252 | num_chunks = Decoding::rlc_data_from_ul_data( |
| 253 | rdbi, rlc->cs, rlc_data, NULL, 0, &new_tlli); |
| 254 | |
| 255 | if (num_chunks < 0) { |
| 256 | bts->decode_error(); |
| 257 | LOGP(DRLCMACUL, LOGL_NOTICE, |
| 258 | "Failed to decode TLLI of %s UL DATA " |
| 259 | "TFI=%d.\n", rlc->cs.name(), rlc->tfi); |
| 260 | m_window.invalidate_bsn(rdbi->bsn); |
| 261 | continue; |
| 262 | } |
| 263 | if (!this->is_tlli_valid()) { |
| 264 | if (!new_tlli) { |
| 265 | LOGP(DRLCMACUL, LOGL_NOTICE, |
| 266 | "%s: TLLI = 0 within UL DATA.\n", |
| 267 | name()); |
| 268 | m_window.invalidate_bsn(rdbi->bsn); |
| 269 | continue; |
| 270 | } |
| 271 | LOGP(DRLCMACUL, LOGL_INFO, |
| 272 | "Decoded premier TLLI=0x%08x of " |
| 273 | "UL DATA TFI=%d.\n", tlli(), rlc->tfi); |
| 274 | set_tlli_from_ul(new_tlli); |
| 275 | } else if (new_tlli && new_tlli != tlli()) { |
| 276 | LOGP(DRLCMACUL, LOGL_NOTICE, "TLLI mismatch on UL " |
| 277 | "DATA TFI=%d. (Ignoring due to contention " |
| 278 | "resolution)\n", rlc->tfi); |
| 279 | m_window.invalidate_bsn(rdbi->bsn); |
| 280 | continue; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | m_window.receive_bsn(rdbi->bsn); |
| 285 | } |
| 286 | |
| 287 | /* Raise V(Q) if possible, and retrieve LLC frames from blocks. |
| 288 | * This is looped until there is a gap (non received block) or |
| 289 | * the window is empty.*/ |
| 290 | const uint16_t v_q_beg = m_window.v_q(); |
| 291 | const uint16_t count = m_window.raise_v_q(); |
| 292 | |
| 293 | /* Retrieve LLC frames from blocks that are ready */ |
| 294 | for (uint16_t i = 0; i < count; ++i) { |
Jacob Erlbeck | 93c55d0 | 2015-12-23 16:29:07 +0100 | [diff] [blame] | 295 | uint16_t index = m_window.mod_sns(v_q_beg + i); |
Jacob Erlbeck | b3100e1 | 2015-12-14 13:36:13 +0100 | [diff] [blame] | 296 | assemble_forward_llc(m_rlc.block(index)); |
| 297 | } |
| 298 | |
| 299 | /* Check CV of last frame in buffer */ |
| 300 | if (this->state_is(GPRS_RLCMAC_FLOW) /* still in flow state */ |
| 301 | && this->m_window.v_q() == this->m_window.v_r()) { /* if complete */ |
| 302 | struct gprs_rlc_data *block = |
Jacob Erlbeck | 93c55d0 | 2015-12-23 16:29:07 +0100 | [diff] [blame] | 303 | m_rlc.block(m_window.mod_sns(m_window.v_r() - 1)); |
Jacob Erlbeck | f2ba4cb | 2016-01-07 18:59:28 +0100 | [diff] [blame] | 304 | const struct gprs_rlc_data_block_info *rdbi = |
Jacob Erlbeck | b3100e1 | 2015-12-14 13:36:13 +0100 | [diff] [blame] | 305 | &block->block_info; |
| 306 | |
| 307 | LOGP(DRLCMACUL, LOGL_DEBUG, "- No gaps in received block, " |
| 308 | "last block: BSN=%d CV=%d\n", rdbi->bsn, |
| 309 | rdbi->cv); |
| 310 | if (rdbi->cv == 0) { |
| 311 | LOGP(DRLCMACUL, LOGL_DEBUG, "- Finished with UL " |
| 312 | "TBF\n"); |
| 313 | set_state(GPRS_RLCMAC_FINISHED); |
| 314 | /* Reset N3103 counter. */ |
| 315 | this->m_n3103 = 0; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | /* If TLLI is included or if we received half of the window, we send |
| 320 | * an ack/nack */ |
| 321 | maybe_schedule_uplink_acknack(rlc); |
| 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
Jacob Erlbeck | b3100e1 | 2015-12-14 13:36:13 +0100 | [diff] [blame] | 326 | void gprs_rlcmac_ul_tbf::maybe_schedule_uplink_acknack( |
Jacob Erlbeck | f2ba4cb | 2016-01-07 18:59:28 +0100 | [diff] [blame] | 327 | const gprs_rlc_data_info *rlc) |
Jacob Erlbeck | b3100e1 | 2015-12-14 13:36:13 +0100 | [diff] [blame] | 328 | { |
| 329 | bool have_ti = rlc->block_info[0].ti || |
| 330 | (rlc->num_data_blocks > 1 && rlc->block_info[1].ti); |
| 331 | |
| 332 | if (rlc->si || have_ti || state_is(GPRS_RLCMAC_FINISHED) || |
| 333 | (m_rx_counter % SEND_ACK_AFTER_FRAMES) == 0) |
| 334 | { |
| 335 | if (rlc->si) { |
| 336 | LOGP(DRLCMACUL, LOGL_NOTICE, "- Scheduling Ack/Nack, " |
| 337 | "because MS is stalled.\n"); |
| 338 | } |
| 339 | if (have_ti) { |
| 340 | LOGP(DRLCMACUL, LOGL_DEBUG, "- Scheduling Ack/Nack, " |
| 341 | "because TLLI is included.\n"); |
| 342 | } |
| 343 | if (state_is(GPRS_RLCMAC_FINISHED)) { |
| 344 | LOGP(DRLCMACUL, LOGL_DEBUG, "- Scheduling Ack/Nack, " |
| 345 | "because last block has CV==0.\n"); |
| 346 | } |
| 347 | if ((m_rx_counter % SEND_ACK_AFTER_FRAMES) == 0) { |
| 348 | LOGP(DRLCMACUL, LOGL_DEBUG, "- Scheduling Ack/Nack, " |
| 349 | "because %d frames received.\n", |
| 350 | SEND_ACK_AFTER_FRAMES); |
| 351 | } |
| 352 | if (ul_ack_state == GPRS_RLCMAC_UL_ACK_NONE) { |
| 353 | /* trigger sending at next RTS */ |
| 354 | ul_ack_state = GPRS_RLCMAC_UL_ACK_SEND_ACK; |
| 355 | } else { |
| 356 | /* already triggered */ |
| 357 | LOGP(DRLCMACUL, LOGL_DEBUG, "- Sending Ack/Nack is " |
| 358 | "already triggered, don't schedule!\n"); |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
Daniel Willmann | ca102af | 2014-08-08 12:14:12 +0200 | [diff] [blame] | 363 | /* Send Uplink unit-data to SGSN. */ |
| 364 | int gprs_rlcmac_ul_tbf::snd_ul_ud() |
| 365 | { |
| 366 | uint8_t qos_profile[3]; |
| 367 | struct msgb *llc_pdu; |
| 368 | unsigned msg_len = NS_HDR_LEN + BSSGP_HDR_LEN + m_llc.frame_length(); |
| 369 | struct bssgp_bvc_ctx *bctx = gprs_bssgp_pcu_current_bctx(); |
| 370 | |
| 371 | LOGP(DBSSGP, LOGL_INFO, "LLC [PCU -> SGSN] %s len=%d\n", tbf_name(this), m_llc.frame_length()); |
| 372 | if (!bctx) { |
| 373 | LOGP(DBSSGP, LOGL_ERROR, "No bctx\n"); |
| 374 | m_llc.reset_frame_space(); |
| 375 | return -EIO; |
| 376 | } |
| 377 | |
| 378 | llc_pdu = msgb_alloc_headroom(msg_len, msg_len,"llc_pdu"); |
| 379 | uint8_t *buf = msgb_push(llc_pdu, TL16V_GROSS_LEN(sizeof(uint8_t)*m_llc.frame_length())); |
| 380 | tl16v_put(buf, BSSGP_IE_LLC_PDU, sizeof(uint8_t)*m_llc.frame_length(), m_llc.frame); |
| 381 | qos_profile[0] = QOS_PROFILE >> 16; |
| 382 | qos_profile[1] = QOS_PROFILE >> 8; |
| 383 | qos_profile[2] = QOS_PROFILE; |
| 384 | bssgp_tx_ul_ud(bctx, tlli(), qos_profile, llc_pdu); |
| 385 | |
| 386 | m_llc.reset_frame_space(); |
| 387 | return 0; |
| 388 | } |
| 389 | |