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 | |
| 45 | |
| 46 | /* |
| 47 | * Store received block data in LLC message(s) and forward to SGSN |
| 48 | * if complete. |
| 49 | */ |
| 50 | int gprs_rlcmac_ul_tbf::assemble_forward_llc(const gprs_rlc_data *_data) |
| 51 | { |
| 52 | const uint8_t *data = _data->block; |
| 53 | uint8_t len = _data->len; |
| 54 | const struct rlc_ul_header *rh = (const struct rlc_ul_header *) data; |
| 55 | uint8_t e, m; |
| 56 | struct rlc_li_field *li; |
| 57 | uint8_t frame_offset[16], offset = 0, chunk; |
| 58 | int i, frames = 0; |
| 59 | |
| 60 | LOGP(DRLCMACUL, LOGL_DEBUG, "- Assembling frames: (len=%d)\n", len); |
| 61 | |
| 62 | data += 3; |
| 63 | len -= 3; |
| 64 | e = rh->e; /* if extended */ |
| 65 | m = 1; /* more frames, that means: the first frame */ |
| 66 | |
| 67 | /* Parse frame offsets from length indicator(s), if any. */ |
| 68 | while (1) { |
| 69 | if (frames == (int)sizeof(frame_offset)) { |
| 70 | LOGP(DRLCMACUL, LOGL_ERROR, "%s too many frames in " |
| 71 | "block\n", tbf_name(this)); |
| 72 | return -EINVAL; |
| 73 | } |
| 74 | frame_offset[frames++] = offset; |
| 75 | LOGP(DRLCMACUL, LOGL_DEBUG, "-- Frame %d starts at offset " |
| 76 | "%d\n", frames, offset); |
| 77 | if (!len) |
| 78 | break; |
| 79 | /* M == 0 and E == 0 is not allowed in this version. */ |
| 80 | if (!m && !e) { |
| 81 | LOGP(DRLCMACUL, LOGL_NOTICE, "%s UL DATA " |
| 82 | "ignored, because M='0' and E='0'.\n", |
| 83 | tbf_name(this)); |
| 84 | return 0; |
| 85 | } |
| 86 | /* no more frames in this segment */ |
| 87 | if (e) { |
| 88 | break; |
| 89 | } |
| 90 | /* There is a new frame and an LI that delimits it. */ |
| 91 | if (m) { |
| 92 | li = (struct rlc_li_field *)data; |
| 93 | LOGP(DRLCMACUL, LOGL_DEBUG, "-- Delimiter len=%d\n", |
| 94 | li->li); |
| 95 | /* Special case: LI == 0 |
| 96 | * If the last segment would fit precisely into the |
| 97 | * rest of the RLC MAC block, there would be no way |
| 98 | * to delimit that this segment ends and is not |
| 99 | * continued in the next block. |
| 100 | * The special LI (0) is used to force the segment to |
| 101 | * extend into the next block, so it is delimited there. |
| 102 | * This LI must be skipped. Also it is the last LI. |
| 103 | */ |
| 104 | if (li->li == 0) { |
| 105 | data++; |
| 106 | len--; |
| 107 | m = 1; /* M is ignored, we know there is more */ |
| 108 | break; /* handle E as '1', so we break! */ |
| 109 | } |
| 110 | e = li->e; |
| 111 | m = li->m; |
| 112 | offset += li->li; |
| 113 | data++; |
| 114 | len--; |
| 115 | continue; |
| 116 | } |
| 117 | } |
| 118 | if (!m) { |
| 119 | LOGP(DRLCMACUL, LOGL_DEBUG, "- Last frame carries spare " |
| 120 | "data\n"); |
| 121 | } |
| 122 | |
| 123 | LOGP(DRLCMACUL, LOGL_DEBUG, "- Data length after length fields: %d\n", |
| 124 | len); |
| 125 | /* TLLI */ |
| 126 | if (rh->ti) { |
| 127 | if (len < 4) { |
| 128 | LOGP(DRLCMACUL, LOGL_NOTICE, "%s UL DATA TLLI out of " |
| 129 | "frame border\n", tbf_name(this)); |
| 130 | return -EINVAL; |
| 131 | } |
| 132 | data += 4; |
| 133 | len -= 4; |
| 134 | LOGP(DRLCMACUL, LOGL_DEBUG, "- Length after skipping TLLI: " |
| 135 | "%d\n", len); |
| 136 | } |
| 137 | |
| 138 | /* PFI */ |
| 139 | if (rh->pi) { |
| 140 | LOGP(DRLCMACUL, LOGL_ERROR, "ERROR: PFI not supported, " |
| 141 | "please disable in SYSTEM INFORMATION\n"); |
| 142 | if (len < 1) { |
| 143 | LOGP(DRLCMACUL, LOGL_NOTICE, "%s UL DATA PFI out of " |
| 144 | "frame border\n", tbf_name(this)); |
| 145 | return -EINVAL; |
| 146 | } |
| 147 | data++; |
| 148 | len--; |
| 149 | LOGP(DRLCMACUL, LOGL_DEBUG, "- Length after skipping PFI: " |
| 150 | "%d\n", len); |
| 151 | } |
| 152 | |
| 153 | /* Now we have: |
| 154 | * - a list of frames offsets: frame_offset[] |
| 155 | * - number of frames: i |
| 156 | * - m == 0: Last frame carries spare data (end of TBF). |
| 157 | */ |
| 158 | |
| 159 | /* Check if last offset would exceed frame. */ |
| 160 | if (offset > len) { |
| 161 | LOGP(DRLCMACUL, LOGL_NOTICE, "%s UL DATA ignored, " |
| 162 | "because LI delimits data that exceeds block size.\n", |
| 163 | tbf_name(this)); |
| 164 | return -EINVAL; |
| 165 | } |
| 166 | |
| 167 | /* create LLC frames */ |
| 168 | for (i = 0; i < frames; i++) { |
| 169 | /* last frame ? */ |
| 170 | if (i == frames - 1) { |
| 171 | /* no more data in last frame */ |
| 172 | if (!m) |
| 173 | break; |
| 174 | /* data until end of frame */ |
| 175 | chunk = len - frame_offset[i]; |
| 176 | } else { |
| 177 | /* data until next frame */ |
| 178 | chunk = frame_offset[i + 1] - frame_offset[i]; |
| 179 | } |
| 180 | if (!m_llc.fits_in_current_frame(chunk)) { |
| 181 | LOGP(DRLCMACUL, LOGL_NOTICE, "%s LLC frame exceeds " |
| 182 | "maximum size %u.\n", tbf_name(this), |
| 183 | m_llc.remaining_space()); |
| 184 | chunk = m_llc.remaining_space(); |
| 185 | } |
| 186 | m_llc.append_frame(data + frame_offset[i], chunk); |
| 187 | m_llc.consume(chunk); |
| 188 | /* not last frame. */ |
| 189 | if (i != frames - 1) { |
| 190 | /* send frame to SGSN */ |
| 191 | LOGP(DRLCMACUL, LOGL_INFO, "%s complete UL frame len=%d\n", |
| 192 | tbf_name(this) , m_llc.frame_length()); |
| 193 | snd_ul_ud(); |
| 194 | m_llc.reset(); |
| 195 | /* also check if CV==0, because the frame may fill up the |
| 196 | * block precisely, then it is also complete. normally the |
| 197 | * frame would be extended into the next block with a 0-length |
| 198 | * delimiter added to this block. */ |
| 199 | } else if (rh->cv == 0) { |
| 200 | /* send frame to SGSN */ |
| 201 | LOGP(DRLCMACUL, LOGL_INFO, "%s complete UL frame " |
| 202 | "that fits precisely in last block: " |
| 203 | "len=%d\n", tbf_name(this), m_llc.frame_length()); |
| 204 | snd_ul_ud(); |
| 205 | m_llc.reset(); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | |
| 213 | struct msgb *gprs_rlcmac_ul_tbf::create_ul_ack(uint32_t fn) |
| 214 | { |
| 215 | int final = (state_is(GPRS_RLCMAC_FINISHED)); |
| 216 | struct msgb *msg; |
| 217 | |
| 218 | if (final) { |
| 219 | if (poll_state != GPRS_RLCMAC_POLL_NONE) { |
| 220 | LOGP(DRLCMACUL, LOGL_DEBUG, "Polling is already " |
| 221 | "sheduled for %s, so we must wait for " |
| 222 | "final uplink ack...\n", tbf_name(this)); |
| 223 | return NULL; |
| 224 | } |
| 225 | if (bts->sba()->find(trx->trx_no, control_ts, (fn + 13) % 2715648)) { |
| 226 | LOGP(DRLCMACUL, LOGL_DEBUG, "Polling is already " |
| 227 | "scheduled for single block allocation...\n"); |
| 228 | return NULL; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | msg = msgb_alloc(23, "rlcmac_ul_ack"); |
| 233 | if (!msg) |
| 234 | return NULL; |
| 235 | bitvec *ack_vec = bitvec_alloc(23); |
| 236 | if (!ack_vec) { |
| 237 | msgb_free(msg); |
| 238 | return NULL; |
| 239 | } |
| 240 | bitvec_unhex(ack_vec, |
| 241 | "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b"); |
| 242 | RlcMacDownlink_t * mac_control_block = (RlcMacDownlink_t *)talloc_zero(tall_pcu_ctx, RlcMacDownlink_t); |
| 243 | Encoding::write_packet_uplink_ack(bts_data(), mac_control_block, this, final); |
| 244 | encode_gsm_rlcmac_downlink(ack_vec, mac_control_block); |
| 245 | bitvec_pack(ack_vec, msgb_put(msg, 23)); |
| 246 | bitvec_free(ack_vec); |
| 247 | talloc_free(mac_control_block); |
| 248 | |
| 249 | /* now we must set this flag, so we are allowed to assign downlink |
| 250 | * TBF on PACCH. it is only allowed when TLLI is acknowledged. */ |
| 251 | m_contention_resolution_done = 1; |
| 252 | |
| 253 | if (final) { |
| 254 | poll_state = GPRS_RLCMAC_POLL_SCHED; |
| 255 | poll_fn = (fn + 13) % 2715648; |
| 256 | /* waiting for final acknowledge */ |
| 257 | ul_ack_state = GPRS_RLCMAC_UL_ACK_WAIT_ACK; |
| 258 | m_final_ack_sent = 1; |
| 259 | } else |
| 260 | ul_ack_state = GPRS_RLCMAC_UL_ACK_NONE; |
| 261 | |
| 262 | return msg; |
| 263 | } |
| 264 | |
Jacob Erlbeck | 20f6fd1 | 2015-06-08 11:05:45 +0200 | [diff] [blame] | 265 | int gprs_rlcmac_ul_tbf::rcv_data_block_acknowledged(const uint8_t *data, |
| 266 | size_t len, struct pcu_l1_meas *meas) |
Daniel Willmann | ca102af | 2014-08-08 12:14:12 +0200 | [diff] [blame] | 267 | { |
| 268 | struct rlc_ul_header *rh = (struct rlc_ul_header *)data; |
| 269 | int rc; |
Jacob Erlbeck | 20f6fd1 | 2015-06-08 11:05:45 +0200 | [diff] [blame] | 270 | int8_t rssi = meas->have_rssi ? meas->rssi : 0; |
Daniel Willmann | ca102af | 2014-08-08 12:14:12 +0200 | [diff] [blame] | 271 | |
| 272 | const uint16_t mod_sns = m_window.mod_sns(); |
| 273 | const uint16_t ws = m_window.ws(); |
| 274 | |
| 275 | this->state_flags |= (1 << GPRS_RLCMAC_FLAG_UL_DATA); |
| 276 | |
| 277 | LOGP(DRLCMACUL, LOGL_DEBUG, "UL DATA TFI=%d received (V(Q)=%d .. " |
| 278 | "V(R)=%d)\n", rh->tfi, this->m_window.v_q(), |
| 279 | this->m_window.v_r()); |
| 280 | |
| 281 | /* process RSSI */ |
| 282 | gprs_rlcmac_rssi(this, rssi); |
| 283 | |
Jacob Erlbeck | e4bcb62 | 2015-06-08 11:26:38 +0200 | [diff] [blame] | 284 | /* store measurement values */ |
| 285 | if (ms()) |
| 286 | ms()->update_l1_meas(meas); |
| 287 | |
Daniel Willmann | ca102af | 2014-08-08 12:14:12 +0200 | [diff] [blame] | 288 | /* get TLLI */ |
| 289 | if (!this->is_tlli_valid()) { |
| 290 | if (!extract_tlli(data, len)) |
| 291 | return 0; |
| 292 | /* already have TLLI, but we stille get another one */ |
| 293 | } else if (rh->ti) { |
| 294 | uint32_t tlli; |
| 295 | rc = Decoding::tlli_from_ul_data(data, len, &tlli); |
| 296 | if (rc) { |
| 297 | LOGP(DRLCMACUL, LOGL_NOTICE, "Failed to decode TLLI " |
| 298 | "of UL DATA TFI=%d.\n", rh->tfi); |
| 299 | return 0; |
| 300 | } |
| 301 | if (tlli != this->tlli()) { |
| 302 | LOGP(DRLCMACUL, LOGL_NOTICE, "TLLI mismatch on UL " |
| 303 | "DATA TFI=%d. (Ignoring due to contention " |
| 304 | "resolution)\n", rh->tfi); |
| 305 | return 0; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | /* restart T3169 */ |
| 310 | tbf_timer_start(this, 3169, bts_data()->t3169, 0); |
| 311 | |
| 312 | /* Increment RX-counter */ |
| 313 | this->m_rx_counter++; |
| 314 | |
| 315 | if (!m_window.is_in_window(rh->bsn)) { |
| 316 | LOGP(DRLCMACUL, LOGL_DEBUG, "- BSN %d out of window " |
| 317 | "%d..%d (it's normal)\n", rh->bsn, |
| 318 | m_window.v_q(), |
| 319 | (m_window.v_q() + ws - 1) & mod_sns); |
| 320 | maybe_schedule_uplink_acknack(rh); |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | /* Write block to buffer and set receive state array. */ |
| 325 | m_rlc.block(rh->bsn)->put_data(data, len); |
| 326 | LOGP(DRLCMACUL, LOGL_DEBUG, "- BSN %d storing in window (%d..%d)\n", |
| 327 | rh->bsn, m_window.v_q(), |
| 328 | (m_window.v_q() + ws - 1) & mod_sns); |
| 329 | |
| 330 | /* Raise V(Q) if possible, and retrieve LLC frames from blocks. |
| 331 | * This is looped until there is a gap (non received block) or |
| 332 | * the window is empty.*/ |
| 333 | const uint16_t v_q_beg = m_window.v_q(); |
| 334 | |
| 335 | const uint16_t count = m_window.receive_bsn(rh->bsn); |
| 336 | |
| 337 | /* Retrieve LLC frames from blocks that are ready */ |
| 338 | for (uint16_t i = 0; i < count; ++i) { |
| 339 | uint16_t index = (v_q_beg + i) & mod_sns; |
| 340 | assemble_forward_llc(m_rlc.block(index)); |
| 341 | } |
| 342 | |
| 343 | /* Check CV of last frame in buffer */ |
| 344 | if (this->state_is(GPRS_RLCMAC_FLOW) /* still in flow state */ |
| 345 | && this->m_window.v_q() == this->m_window.v_r()) { /* if complete */ |
| 346 | struct rlc_ul_header *last_rh = (struct rlc_ul_header *) |
| 347 | m_rlc.block((m_window.v_r() - 1) & mod_sns)->block; |
| 348 | LOGP(DRLCMACUL, LOGL_DEBUG, "- No gaps in received block, " |
| 349 | "last block: BSN=%d CV=%d\n", last_rh->bsn, |
| 350 | last_rh->cv); |
| 351 | if (last_rh->cv == 0) { |
| 352 | LOGP(DRLCMACUL, LOGL_DEBUG, "- Finished with UL " |
| 353 | "TBF\n"); |
| 354 | set_state(GPRS_RLCMAC_FINISHED); |
| 355 | /* Reset N3103 counter. */ |
| 356 | this->m_n3103 = 0; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | /* If TLLI is included or if we received half of the window, we send |
| 361 | * an ack/nack */ |
| 362 | maybe_schedule_uplink_acknack(rh); |
| 363 | |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | void gprs_rlcmac_ul_tbf::maybe_schedule_uplink_acknack(const rlc_ul_header *rh) |
| 368 | { |
| 369 | if (rh->si || rh->ti || state_is(GPRS_RLCMAC_FINISHED) |
| 370 | || (m_rx_counter % SEND_ACK_AFTER_FRAMES) == 0) { |
| 371 | if (rh->si) { |
| 372 | LOGP(DRLCMACUL, LOGL_NOTICE, "- Scheduling Ack/Nack, " |
| 373 | "because MS is stalled.\n"); |
| 374 | } |
| 375 | if (rh->ti) { |
| 376 | LOGP(DRLCMACUL, LOGL_DEBUG, "- Scheduling Ack/Nack, " |
| 377 | "because TLLI is included.\n"); |
| 378 | } |
| 379 | if (state_is(GPRS_RLCMAC_FINISHED)) { |
| 380 | LOGP(DRLCMACUL, LOGL_DEBUG, "- Scheduling Ack/Nack, " |
| 381 | "because last block has CV==0.\n"); |
| 382 | } |
| 383 | if ((m_rx_counter % SEND_ACK_AFTER_FRAMES) == 0) { |
| 384 | LOGP(DRLCMACUL, LOGL_DEBUG, "- Scheduling Ack/Nack, " |
| 385 | "because %d frames received.\n", |
| 386 | SEND_ACK_AFTER_FRAMES); |
| 387 | } |
| 388 | if (ul_ack_state == GPRS_RLCMAC_UL_ACK_NONE) { |
| 389 | /* trigger sending at next RTS */ |
| 390 | ul_ack_state = GPRS_RLCMAC_UL_ACK_SEND_ACK; |
| 391 | } else { |
| 392 | /* already triggered */ |
| 393 | LOGP(DRLCMACUL, LOGL_DEBUG, "- Sending Ack/Nack is " |
| 394 | "already triggered, don't schedule!\n"); |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | /* Send Uplink unit-data to SGSN. */ |
| 400 | int gprs_rlcmac_ul_tbf::snd_ul_ud() |
| 401 | { |
| 402 | uint8_t qos_profile[3]; |
| 403 | struct msgb *llc_pdu; |
| 404 | unsigned msg_len = NS_HDR_LEN + BSSGP_HDR_LEN + m_llc.frame_length(); |
| 405 | struct bssgp_bvc_ctx *bctx = gprs_bssgp_pcu_current_bctx(); |
| 406 | |
| 407 | LOGP(DBSSGP, LOGL_INFO, "LLC [PCU -> SGSN] %s len=%d\n", tbf_name(this), m_llc.frame_length()); |
| 408 | if (!bctx) { |
| 409 | LOGP(DBSSGP, LOGL_ERROR, "No bctx\n"); |
| 410 | m_llc.reset_frame_space(); |
| 411 | return -EIO; |
| 412 | } |
| 413 | |
| 414 | llc_pdu = msgb_alloc_headroom(msg_len, msg_len,"llc_pdu"); |
| 415 | uint8_t *buf = msgb_push(llc_pdu, TL16V_GROSS_LEN(sizeof(uint8_t)*m_llc.frame_length())); |
| 416 | tl16v_put(buf, BSSGP_IE_LLC_PDU, sizeof(uint8_t)*m_llc.frame_length(), m_llc.frame); |
| 417 | qos_profile[0] = QOS_PROFILE >> 16; |
| 418 | qos_profile[1] = QOS_PROFILE >> 8; |
| 419 | qos_profile[2] = QOS_PROFILE; |
| 420 | bssgp_tx_ul_ud(bctx, tlli(), qos_profile, llc_pdu); |
| 421 | |
| 422 | m_llc.reset_frame_space(); |
| 423 | return 0; |
| 424 | } |
| 425 | |