blob: 98962d17fde8417d122a864558b9ac6becfdc891 [file] [log] [blame]
Daniel Willmannca102af2014-08-08 12:14:12 +02001/* Copied from tbf.cpp
2 *
3 * Copyright (C) 2012 Ivan Klyuchnikov
4 * Copyright (C) 2012 Andreas Eversberg <jolly@eversberg.eu>
5 * Copyright (C) 2013 by Holger Hans Peter Freyther
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21
22#include <bts.h>
23#include <tbf.h>
24#include <rlc.h>
25#include <encoding.h>
26#include <gprs_rlcmac.h>
27#include <gprs_debug.h>
28#include <gprs_bssgp_pcu.h>
29#include <decoding.h>
Jacob Erlbeck20f6fd12015-06-08 11:05:45 +020030#include <pcu_l1_if.h>
Daniel Willmannca102af2014-08-08 12:14:12 +020031
32extern "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
43extern void *tall_pcu_ctx;
44
Jacob Erlbeckb3100e12015-12-14 13:36:13 +010045/*
46 * Store received block data in LLC message(s) and forward to SGSN
47 * if complete.
48 */
49int 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 Erlbeckf2ba4cb2016-01-07 18:59:28 +010053 const struct gprs_rlc_data_block_info *rdbi = &_data->block_info;
Jacob Erlbeckb3100e12015-12-14 13:36:13 +010054 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(
Alexander Couzensce936f32016-05-24 14:45:41 +020063 rdbi, cs, data, &(frames[0]), ARRAY_SIZE(frames),
Jacob Erlbeckb3100e12015-12-14 13:36:13 +010064 &dummy_tlli);
65
66 /* create LLC frames */
67 for (i = 0; i < num_frames; i++) {
68 frame = frames + i;
69
Alexander Couzens7fdbf892016-05-21 19:45:23 +020070 bts->rlc_ul_payload_bytes(frame->length);
71
Jacob Erlbeckb3100e12015-12-14 13:36:13 +010072 LOGP(DRLCMACUL, LOGL_DEBUG, "-- Frame %d starts at offset %d, "
73 "length=%d, is_complete=%d\n",
74 i + 1, frame->offset, frame->length, frame->is_complete);
75
76 m_llc.append_frame(data + frame->offset, frame->length);
77 m_llc.consume(frame->length);
78
79 if (frame->is_complete) {
80 /* send frame to SGSN */
81 LOGP(DRLCMACUL, LOGL_INFO, "%s complete UL frame len=%d\n",
82 tbf_name(this) , m_llc.frame_length());
83 snd_ul_ud();
Alexander Couzens7fdbf892016-05-21 19:45:23 +020084 bts->llc_ul_bytes(m_llc.frame_length());
Jacob Erlbeckb3100e12015-12-14 13:36:13 +010085 m_llc.reset();
86 }
87 }
88
89 return 0;
90}
91
Daniel Willmannca102af2014-08-08 12:14:12 +020092
Jacob Erlbeck5a3c84d2016-01-22 17:25:38 +010093struct msgb *gprs_rlcmac_ul_tbf::create_ul_ack(uint32_t fn, uint8_t ts)
Daniel Willmannca102af2014-08-08 12:14:12 +020094{
95 int final = (state_is(GPRS_RLCMAC_FINISHED));
96 struct msgb *msg;
Jacob Erlbeckf2694b72016-01-26 21:46:26 +010097 int rc;
98 unsigned int rrbp = 0;
99 uint32_t new_poll_fn = 0;
Daniel Willmannca102af2014-08-08 12:14:12 +0200100
101 if (final) {
Jacob Erlbeckf2694b72016-01-26 21:46:26 +0100102 if (poll_state == GPRS_RLCMAC_POLL_SCHED &&
103 ul_ack_state == GPRS_RLCMAC_UL_ACK_WAIT_ACK) {
Daniel Willmannca102af2014-08-08 12:14:12 +0200104 LOGP(DRLCMACUL, LOGL_DEBUG, "Polling is already "
Jacob Erlbeckf2694b72016-01-26 21:46:26 +0100105 "scheduled for %s, so we must wait for "
106 "the final uplink ack...\n", tbf_name(this));
Daniel Willmannca102af2014-08-08 12:14:12 +0200107 return NULL;
108 }
Jacob Erlbeckf2694b72016-01-26 21:46:26 +0100109
110 rc = check_polling(fn, ts, &new_poll_fn, &rrbp);
111 if (rc < 0)
Daniel Willmannca102af2014-08-08 12:14:12 +0200112 return NULL;
Daniel Willmannca102af2014-08-08 12:14:12 +0200113 }
114
115 msg = msgb_alloc(23, "rlcmac_ul_ack");
116 if (!msg)
117 return NULL;
118 bitvec *ack_vec = bitvec_alloc(23);
119 if (!ack_vec) {
120 msgb_free(msg);
121 return NULL;
122 }
123 bitvec_unhex(ack_vec,
124 "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b");
Jacob Erlbeckf2694b72016-01-26 21:46:26 +0100125 Encoding::write_packet_uplink_ack(bts_data(), ack_vec, this, final, rrbp);
Daniel Willmannca102af2014-08-08 12:14:12 +0200126 bitvec_pack(ack_vec, msgb_put(msg, 23));
127 bitvec_free(ack_vec);
Daniel Willmannca102af2014-08-08 12:14:12 +0200128
129 /* now we must set this flag, so we are allowed to assign downlink
130 * TBF on PACCH. it is only allowed when TLLI is acknowledged. */
131 m_contention_resolution_done = 1;
132
133 if (final) {
Jacob Erlbeckf2694b72016-01-26 21:46:26 +0100134 set_polling(new_poll_fn, ts);
Daniel Willmannca102af2014-08-08 12:14:12 +0200135 /* waiting for final acknowledge */
136 ul_ack_state = GPRS_RLCMAC_UL_ACK_WAIT_ACK;
137 m_final_ack_sent = 1;
138 } else
139 ul_ack_state = GPRS_RLCMAC_UL_ACK_NONE;
140
141 return msg;
142}
143
Alexander Couzens2fcfc292016-05-24 14:40:03 +0200144/*! \brief receive data from PDCH/L1 */
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100145int gprs_rlcmac_ul_tbf::rcv_data_block_acknowledged(
Jacob Erlbeckf2ba4cb2016-01-07 18:59:28 +0100146 const struct gprs_rlc_data_info *rlc,
Jacob Erlbeckfc1b3e62016-01-11 09:58:11 +0100147 uint8_t *data, struct pcu_l1_meas *meas)
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100148{
149 int8_t rssi = meas->have_rssi ? meas->rssi : 0;
150
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100151 const uint16_t ws = m_window.ws();
152
153 this->state_flags |= (1 << GPRS_RLCMAC_FLAG_UL_DATA);
154
155 LOGP(DRLCMACUL, LOGL_DEBUG, "UL DATA TFI=%d received (V(Q)=%d .. "
156 "V(R)=%d)\n", rlc->tfi, this->m_window.v_q(),
157 this->m_window.v_r());
158
159 /* process RSSI */
160 gprs_rlcmac_rssi(this, rssi);
161
162 /* store measurement values */
163 if (ms())
164 ms()->update_l1_meas(meas);
165
166 uint32_t new_tlli = 0;
167 unsigned int block_idx;
168
169 /* restart T3169 */
170 tbf_timer_start(this, 3169, bts_data()->t3169, 0);
171
172 /* Increment RX-counter */
173 this->m_rx_counter++;
174
175 /* Loop over num_blocks */
176 for (block_idx = 0; block_idx < rlc->num_data_blocks; block_idx++) {
177 int num_chunks;
178 uint8_t *rlc_data;
Jacob Erlbeckf2ba4cb2016-01-07 18:59:28 +0100179 const struct gprs_rlc_data_block_info *rdbi =
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100180 &rlc->block_info[block_idx];
181 bool need_rlc_data = false;
182 struct gprs_rlc_data *block;
183
184 LOGP(DRLCMACUL, LOGL_DEBUG,
185 "%s: Got %s RLC data block: "
186 "CV=%d, BSN=%d, SPB=%d, "
187 "PI=%d, E=%d, TI=%d, bitoffs=%d\n",
188 name(), rlc->cs.name(),
189 rdbi->cv, rdbi->bsn, rdbi->spb,
190 rdbi->pi, rdbi->e, rdbi->ti,
191 rlc->data_offs_bits[block_idx]);
192
193 /* Check whether the block needs to be decoded */
194
195 if (!m_window.is_in_window(rdbi->bsn)) {
196 LOGP(DRLCMACUL, LOGL_DEBUG, "- BSN %d out of window "
197 "%d..%d (it's normal)\n", rdbi->bsn,
198 m_window.v_q(),
Jacob Erlbeck93c55d02015-12-23 16:29:07 +0100199 m_window.mod_sns(m_window.v_q() + ws - 1));
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100200 } else if (m_window.is_received(rdbi->bsn)) {
201 LOGP(DRLCMACUL, LOGL_DEBUG,
202 "- BSN %d already received\n", rdbi->bsn);
203 } else {
204 need_rlc_data = true;
205 }
206
207 if (!is_tlli_valid()) {
208 if (!rdbi->ti) {
209 LOGP(DRLCMACUL, LOGL_NOTICE,
210 "%s: Missing TLLI within UL DATA.\n",
211 name());
212 continue;
213 }
214 need_rlc_data = true;
215 }
216
217 if (!need_rlc_data)
218 continue;
219
220 /* Store block and meta info to BSN buffer */
221
222 LOGP(DRLCMACUL, LOGL_DEBUG, "- BSN %d storing in window (%d..%d)\n",
223 rdbi->bsn, m_window.v_q(),
Jacob Erlbeck93c55d02015-12-23 16:29:07 +0100224 m_window.mod_sns(m_window.v_q() + ws - 1));
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100225 block = m_rlc.block(rdbi->bsn);
226 block->block_info = *rdbi;
227 block->cs = rlc->cs;
228 OSMO_ASSERT(rdbi->data_len < sizeof(block->block));
229 rlc_data = &(block->block[0]);
230 /* TODO: Handle SPB != 0 -> Set length to 2*len, add offset if
231 * 2nd part. Note that resegmentation is currently disabled
232 * within the UL assignment.
233 */
234 if (rdbi->spb) {
235 LOGP(DRLCMACUL, LOGL_NOTICE,
236 "Got SPB != 0 but resegmentation has been "
237 "disabled, skipping %s data block with BSN %d, "
238 "TFI=%d.\n", rlc->cs.name(), rdbi->bsn,
239 rlc->tfi);
240 continue;
241 }
242
243 block->len =
244 Decoding::rlc_copy_to_aligned_buffer(rlc, block_idx, data,
245 rlc_data);
246
Jacob Erlbeckd88bb2e2015-12-11 14:53:29 +0100247 LOGP(DRLCMACUL, LOGL_DEBUG,
248 "%s: data_length=%d, data=%s\n",
249 name(), block->len, osmo_hexdump(rlc_data, block->len));
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100250
251 /* TODO: Handle SPB != 0 -> set state to partly received
252 * (upper/lower) and continue with the loop, unless the other
253 * part is already present.
254 */
255
256 /* Get/Handle TLLI */
257 if (rdbi->ti) {
258 num_chunks = Decoding::rlc_data_from_ul_data(
259 rdbi, rlc->cs, rlc_data, NULL, 0, &new_tlli);
260
261 if (num_chunks < 0) {
262 bts->decode_error();
263 LOGP(DRLCMACUL, LOGL_NOTICE,
264 "Failed to decode TLLI of %s UL DATA "
265 "TFI=%d.\n", rlc->cs.name(), rlc->tfi);
266 m_window.invalidate_bsn(rdbi->bsn);
267 continue;
268 }
269 if (!this->is_tlli_valid()) {
270 if (!new_tlli) {
271 LOGP(DRLCMACUL, LOGL_NOTICE,
272 "%s: TLLI = 0 within UL DATA.\n",
273 name());
274 m_window.invalidate_bsn(rdbi->bsn);
275 continue;
276 }
277 LOGP(DRLCMACUL, LOGL_INFO,
278 "Decoded premier TLLI=0x%08x of "
279 "UL DATA TFI=%d.\n", tlli(), rlc->tfi);
280 set_tlli_from_ul(new_tlli);
281 } else if (new_tlli && new_tlli != tlli()) {
282 LOGP(DRLCMACUL, LOGL_NOTICE, "TLLI mismatch on UL "
283 "DATA TFI=%d. (Ignoring due to contention "
284 "resolution)\n", rlc->tfi);
285 m_window.invalidate_bsn(rdbi->bsn);
286 continue;
287 }
288 }
289
290 m_window.receive_bsn(rdbi->bsn);
291 }
292
293 /* Raise V(Q) if possible, and retrieve LLC frames from blocks.
294 * This is looped until there is a gap (non received block) or
295 * the window is empty.*/
296 const uint16_t v_q_beg = m_window.v_q();
297 const uint16_t count = m_window.raise_v_q();
298
299 /* Retrieve LLC frames from blocks that are ready */
300 for (uint16_t i = 0; i < count; ++i) {
Jacob Erlbeck93c55d02015-12-23 16:29:07 +0100301 uint16_t index = m_window.mod_sns(v_q_beg + i);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100302 assemble_forward_llc(m_rlc.block(index));
303 }
304
305 /* Check CV of last frame in buffer */
306 if (this->state_is(GPRS_RLCMAC_FLOW) /* still in flow state */
307 && this->m_window.v_q() == this->m_window.v_r()) { /* if complete */
308 struct gprs_rlc_data *block =
Jacob Erlbeck93c55d02015-12-23 16:29:07 +0100309 m_rlc.block(m_window.mod_sns(m_window.v_r() - 1));
Jacob Erlbeckf2ba4cb2016-01-07 18:59:28 +0100310 const struct gprs_rlc_data_block_info *rdbi =
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100311 &block->block_info;
312
313 LOGP(DRLCMACUL, LOGL_DEBUG, "- No gaps in received block, "
314 "last block: BSN=%d CV=%d\n", rdbi->bsn,
315 rdbi->cv);
316 if (rdbi->cv == 0) {
317 LOGP(DRLCMACUL, LOGL_DEBUG, "- Finished with UL "
318 "TBF\n");
319 set_state(GPRS_RLCMAC_FINISHED);
320 /* Reset N3103 counter. */
321 this->m_n3103 = 0;
322 }
323 }
324
325 /* If TLLI is included or if we received half of the window, we send
326 * an ack/nack */
327 maybe_schedule_uplink_acknack(rlc);
328
329 return 0;
330}
331
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100332void gprs_rlcmac_ul_tbf::maybe_schedule_uplink_acknack(
Jacob Erlbeckf2ba4cb2016-01-07 18:59:28 +0100333 const gprs_rlc_data_info *rlc)
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100334{
335 bool have_ti = rlc->block_info[0].ti ||
336 (rlc->num_data_blocks > 1 && rlc->block_info[1].ti);
337
338 if (rlc->si || have_ti || state_is(GPRS_RLCMAC_FINISHED) ||
339 (m_rx_counter % SEND_ACK_AFTER_FRAMES) == 0)
340 {
341 if (rlc->si) {
342 LOGP(DRLCMACUL, LOGL_NOTICE, "- Scheduling Ack/Nack, "
343 "because MS is stalled.\n");
344 }
345 if (have_ti) {
346 LOGP(DRLCMACUL, LOGL_DEBUG, "- Scheduling Ack/Nack, "
347 "because TLLI is included.\n");
348 }
349 if (state_is(GPRS_RLCMAC_FINISHED)) {
350 LOGP(DRLCMACUL, LOGL_DEBUG, "- Scheduling Ack/Nack, "
351 "because last block has CV==0.\n");
352 }
353 if ((m_rx_counter % SEND_ACK_AFTER_FRAMES) == 0) {
354 LOGP(DRLCMACUL, LOGL_DEBUG, "- Scheduling Ack/Nack, "
355 "because %d frames received.\n",
356 SEND_ACK_AFTER_FRAMES);
357 }
358 if (ul_ack_state == GPRS_RLCMAC_UL_ACK_NONE) {
359 /* trigger sending at next RTS */
360 ul_ack_state = GPRS_RLCMAC_UL_ACK_SEND_ACK;
361 } else {
362 /* already triggered */
363 LOGP(DRLCMACUL, LOGL_DEBUG, "- Sending Ack/Nack is "
364 "already triggered, don't schedule!\n");
365 }
366 }
367}
368
Daniel Willmannca102af2014-08-08 12:14:12 +0200369/* Send Uplink unit-data to SGSN. */
370int gprs_rlcmac_ul_tbf::snd_ul_ud()
371{
372 uint8_t qos_profile[3];
373 struct msgb *llc_pdu;
374 unsigned msg_len = NS_HDR_LEN + BSSGP_HDR_LEN + m_llc.frame_length();
375 struct bssgp_bvc_ctx *bctx = gprs_bssgp_pcu_current_bctx();
376
377 LOGP(DBSSGP, LOGL_INFO, "LLC [PCU -> SGSN] %s len=%d\n", tbf_name(this), m_llc.frame_length());
378 if (!bctx) {
379 LOGP(DBSSGP, LOGL_ERROR, "No bctx\n");
380 m_llc.reset_frame_space();
381 return -EIO;
382 }
383
384 llc_pdu = msgb_alloc_headroom(msg_len, msg_len,"llc_pdu");
385 uint8_t *buf = msgb_push(llc_pdu, TL16V_GROSS_LEN(sizeof(uint8_t)*m_llc.frame_length()));
386 tl16v_put(buf, BSSGP_IE_LLC_PDU, sizeof(uint8_t)*m_llc.frame_length(), m_llc.frame);
387 qos_profile[0] = QOS_PROFILE >> 16;
388 qos_profile[1] = QOS_PROFILE >> 8;
389 qos_profile[2] = QOS_PROFILE;
390 bssgp_tx_ul_ud(bctx, tlli(), qos_profile, llc_pdu);
391
392 m_llc.reset_frame_space();
393 return 0;
394}
395