blob: 9e763f4cadebe47390d2b6faaff1f639ad956e08 [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(
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 Willmannca102af2014-08-08 12:14:12 +020089
Jacob Erlbeck5a3c84d2016-01-22 17:25:38 +010090struct msgb *gprs_rlcmac_ul_tbf::create_ul_ack(uint32_t fn, uint8_t ts)
Daniel Willmannca102af2014-08-08 12:14:12 +020091{
92 int final = (state_is(GPRS_RLCMAC_FINISHED));
93 struct msgb *msg;
Jacob Erlbeckf2694b72016-01-26 21:46:26 +010094 int rc;
95 unsigned int rrbp = 0;
96 uint32_t new_poll_fn = 0;
Daniel Willmannca102af2014-08-08 12:14:12 +020097
98 if (final) {
Jacob Erlbeckf2694b72016-01-26 21:46:26 +010099 if (poll_state == GPRS_RLCMAC_POLL_SCHED &&
100 ul_ack_state == GPRS_RLCMAC_UL_ACK_WAIT_ACK) {
Daniel Willmannca102af2014-08-08 12:14:12 +0200101 LOGP(DRLCMACUL, LOGL_DEBUG, "Polling is already "
Jacob Erlbeckf2694b72016-01-26 21:46:26 +0100102 "scheduled for %s, so we must wait for "
103 "the final uplink ack...\n", tbf_name(this));
Daniel Willmannca102af2014-08-08 12:14:12 +0200104 return NULL;
105 }
Jacob Erlbeckf2694b72016-01-26 21:46:26 +0100106
107 rc = check_polling(fn, ts, &new_poll_fn, &rrbp);
108 if (rc < 0)
Daniel Willmannca102af2014-08-08 12:14:12 +0200109 return NULL;
Daniel Willmannca102af2014-08-08 12:14:12 +0200110 }
111
112 msg = msgb_alloc(23, "rlcmac_ul_ack");
113 if (!msg)
114 return NULL;
115 bitvec *ack_vec = bitvec_alloc(23);
116 if (!ack_vec) {
117 msgb_free(msg);
118 return NULL;
119 }
120 bitvec_unhex(ack_vec,
121 "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b");
Jacob Erlbeckf2694b72016-01-26 21:46:26 +0100122 Encoding::write_packet_uplink_ack(bts_data(), ack_vec, this, final, rrbp);
Daniel Willmannca102af2014-08-08 12:14:12 +0200123 bitvec_pack(ack_vec, msgb_put(msg, 23));
124 bitvec_free(ack_vec);
Daniel Willmannca102af2014-08-08 12:14:12 +0200125
126 /* now we must set this flag, so we are allowed to assign downlink
127 * TBF on PACCH. it is only allowed when TLLI is acknowledged. */
128 m_contention_resolution_done = 1;
129
130 if (final) {
Jacob Erlbeckf2694b72016-01-26 21:46:26 +0100131 set_polling(new_poll_fn, ts);
Daniel Willmannca102af2014-08-08 12:14:12 +0200132 /* waiting for final acknowledge */
133 ul_ack_state = GPRS_RLCMAC_UL_ACK_WAIT_ACK;
134 m_final_ack_sent = 1;
135 } else
136 ul_ack_state = GPRS_RLCMAC_UL_ACK_NONE;
137
138 return msg;
139}
140
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100141int gprs_rlcmac_ul_tbf::rcv_data_block_acknowledged(
Jacob Erlbeckf2ba4cb2016-01-07 18:59:28 +0100142 const struct gprs_rlc_data_info *rlc,
Jacob Erlbeckfc1b3e62016-01-11 09:58:11 +0100143 uint8_t *data, struct pcu_l1_meas *meas)
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100144{
145 int8_t rssi = meas->have_rssi ? meas->rssi : 0;
146
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100147 const uint16_t ws = m_window.ws();
148
149 this->state_flags |= (1 << GPRS_RLCMAC_FLAG_UL_DATA);
150
151 LOGP(DRLCMACUL, LOGL_DEBUG, "UL DATA TFI=%d received (V(Q)=%d .. "
152 "V(R)=%d)\n", rlc->tfi, this->m_window.v_q(),
153 this->m_window.v_r());
154
155 /* process RSSI */
156 gprs_rlcmac_rssi(this, rssi);
157
158 /* store measurement values */
159 if (ms())
160 ms()->update_l1_meas(meas);
161
162 uint32_t new_tlli = 0;
163 unsigned int block_idx;
164
165 /* restart T3169 */
166 tbf_timer_start(this, 3169, bts_data()->t3169, 0);
167
168 /* Increment RX-counter */
169 this->m_rx_counter++;
170
171 /* Loop over num_blocks */
172 for (block_idx = 0; block_idx < rlc->num_data_blocks; block_idx++) {
173 int num_chunks;
174 uint8_t *rlc_data;
Jacob Erlbeckf2ba4cb2016-01-07 18:59:28 +0100175 const struct gprs_rlc_data_block_info *rdbi =
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100176 &rlc->block_info[block_idx];
177 bool need_rlc_data = false;
178 struct gprs_rlc_data *block;
179
180 LOGP(DRLCMACUL, LOGL_DEBUG,
181 "%s: Got %s RLC data block: "
182 "CV=%d, BSN=%d, SPB=%d, "
183 "PI=%d, E=%d, TI=%d, bitoffs=%d\n",
184 name(), rlc->cs.name(),
185 rdbi->cv, rdbi->bsn, rdbi->spb,
186 rdbi->pi, rdbi->e, rdbi->ti,
187 rlc->data_offs_bits[block_idx]);
188
189 /* Check whether the block needs to be decoded */
190
191 if (!m_window.is_in_window(rdbi->bsn)) {
192 LOGP(DRLCMACUL, LOGL_DEBUG, "- BSN %d out of window "
193 "%d..%d (it's normal)\n", rdbi->bsn,
194 m_window.v_q(),
Jacob Erlbeck93c55d02015-12-23 16:29:07 +0100195 m_window.mod_sns(m_window.v_q() + ws - 1));
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100196 } else if (m_window.is_received(rdbi->bsn)) {
197 LOGP(DRLCMACUL, LOGL_DEBUG,
198 "- BSN %d already received\n", rdbi->bsn);
199 } else {
200 need_rlc_data = true;
201 }
202
203 if (!is_tlli_valid()) {
204 if (!rdbi->ti) {
205 LOGP(DRLCMACUL, LOGL_NOTICE,
206 "%s: Missing TLLI within UL DATA.\n",
207 name());
208 continue;
209 }
210 need_rlc_data = true;
211 }
212
213 if (!need_rlc_data)
214 continue;
215
216 /* Store block and meta info to BSN buffer */
217
218 LOGP(DRLCMACUL, LOGL_DEBUG, "- BSN %d storing in window (%d..%d)\n",
219 rdbi->bsn, m_window.v_q(),
Jacob Erlbeck93c55d02015-12-23 16:29:07 +0100220 m_window.mod_sns(m_window.v_q() + ws - 1));
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100221 block = m_rlc.block(rdbi->bsn);
222 block->block_info = *rdbi;
223 block->cs = rlc->cs;
224 OSMO_ASSERT(rdbi->data_len < sizeof(block->block));
225 rlc_data = &(block->block[0]);
226 /* TODO: Handle SPB != 0 -> Set length to 2*len, add offset if
227 * 2nd part. Note that resegmentation is currently disabled
228 * within the UL assignment.
229 */
230 if (rdbi->spb) {
231 LOGP(DRLCMACUL, LOGL_NOTICE,
232 "Got SPB != 0 but resegmentation has been "
233 "disabled, skipping %s data block with BSN %d, "
234 "TFI=%d.\n", rlc->cs.name(), rdbi->bsn,
235 rlc->tfi);
236 continue;
237 }
238
239 block->len =
240 Decoding::rlc_copy_to_aligned_buffer(rlc, block_idx, data,
241 rlc_data);
242
Jacob Erlbeckd88bb2e2015-12-11 14:53:29 +0100243 LOGP(DRLCMACUL, LOGL_DEBUG,
244 "%s: data_length=%d, data=%s\n",
245 name(), block->len, osmo_hexdump(rlc_data, block->len));
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100246
247 /* TODO: Handle SPB != 0 -> set state to partly received
248 * (upper/lower) and continue with the loop, unless the other
249 * part is already present.
250 */
251
252 /* Get/Handle TLLI */
253 if (rdbi->ti) {
254 num_chunks = Decoding::rlc_data_from_ul_data(
255 rdbi, rlc->cs, rlc_data, NULL, 0, &new_tlli);
256
257 if (num_chunks < 0) {
258 bts->decode_error();
259 LOGP(DRLCMACUL, LOGL_NOTICE,
260 "Failed to decode TLLI of %s UL DATA "
261 "TFI=%d.\n", rlc->cs.name(), rlc->tfi);
262 m_window.invalidate_bsn(rdbi->bsn);
263 continue;
264 }
265 if (!this->is_tlli_valid()) {
266 if (!new_tlli) {
267 LOGP(DRLCMACUL, LOGL_NOTICE,
268 "%s: TLLI = 0 within UL DATA.\n",
269 name());
270 m_window.invalidate_bsn(rdbi->bsn);
271 continue;
272 }
273 LOGP(DRLCMACUL, LOGL_INFO,
274 "Decoded premier TLLI=0x%08x of "
275 "UL DATA TFI=%d.\n", tlli(), rlc->tfi);
276 set_tlli_from_ul(new_tlli);
277 } else if (new_tlli && new_tlli != tlli()) {
278 LOGP(DRLCMACUL, LOGL_NOTICE, "TLLI mismatch on UL "
279 "DATA TFI=%d. (Ignoring due to contention "
280 "resolution)\n", rlc->tfi);
281 m_window.invalidate_bsn(rdbi->bsn);
282 continue;
283 }
284 }
285
286 m_window.receive_bsn(rdbi->bsn);
287 }
288
289 /* Raise V(Q) if possible, and retrieve LLC frames from blocks.
290 * This is looped until there is a gap (non received block) or
291 * the window is empty.*/
292 const uint16_t v_q_beg = m_window.v_q();
293 const uint16_t count = m_window.raise_v_q();
294
295 /* Retrieve LLC frames from blocks that are ready */
296 for (uint16_t i = 0; i < count; ++i) {
Jacob Erlbeck93c55d02015-12-23 16:29:07 +0100297 uint16_t index = m_window.mod_sns(v_q_beg + i);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100298 assemble_forward_llc(m_rlc.block(index));
299 }
300
301 /* Check CV of last frame in buffer */
302 if (this->state_is(GPRS_RLCMAC_FLOW) /* still in flow state */
303 && this->m_window.v_q() == this->m_window.v_r()) { /* if complete */
304 struct gprs_rlc_data *block =
Jacob Erlbeck93c55d02015-12-23 16:29:07 +0100305 m_rlc.block(m_window.mod_sns(m_window.v_r() - 1));
Jacob Erlbeckf2ba4cb2016-01-07 18:59:28 +0100306 const struct gprs_rlc_data_block_info *rdbi =
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100307 &block->block_info;
308
309 LOGP(DRLCMACUL, LOGL_DEBUG, "- No gaps in received block, "
310 "last block: BSN=%d CV=%d\n", rdbi->bsn,
311 rdbi->cv);
312 if (rdbi->cv == 0) {
313 LOGP(DRLCMACUL, LOGL_DEBUG, "- Finished with UL "
314 "TBF\n");
315 set_state(GPRS_RLCMAC_FINISHED);
316 /* Reset N3103 counter. */
317 this->m_n3103 = 0;
318 }
319 }
320
321 /* If TLLI is included or if we received half of the window, we send
322 * an ack/nack */
323 maybe_schedule_uplink_acknack(rlc);
324
325 return 0;
326}
327
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100328void gprs_rlcmac_ul_tbf::maybe_schedule_uplink_acknack(
Jacob Erlbeckf2ba4cb2016-01-07 18:59:28 +0100329 const gprs_rlc_data_info *rlc)
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100330{
331 bool have_ti = rlc->block_info[0].ti ||
332 (rlc->num_data_blocks > 1 && rlc->block_info[1].ti);
333
334 if (rlc->si || have_ti || state_is(GPRS_RLCMAC_FINISHED) ||
335 (m_rx_counter % SEND_ACK_AFTER_FRAMES) == 0)
336 {
337 if (rlc->si) {
338 LOGP(DRLCMACUL, LOGL_NOTICE, "- Scheduling Ack/Nack, "
339 "because MS is stalled.\n");
340 }
341 if (have_ti) {
342 LOGP(DRLCMACUL, LOGL_DEBUG, "- Scheduling Ack/Nack, "
343 "because TLLI is included.\n");
344 }
345 if (state_is(GPRS_RLCMAC_FINISHED)) {
346 LOGP(DRLCMACUL, LOGL_DEBUG, "- Scheduling Ack/Nack, "
347 "because last block has CV==0.\n");
348 }
349 if ((m_rx_counter % SEND_ACK_AFTER_FRAMES) == 0) {
350 LOGP(DRLCMACUL, LOGL_DEBUG, "- Scheduling Ack/Nack, "
351 "because %d frames received.\n",
352 SEND_ACK_AFTER_FRAMES);
353 }
354 if (ul_ack_state == GPRS_RLCMAC_UL_ACK_NONE) {
355 /* trigger sending at next RTS */
356 ul_ack_state = GPRS_RLCMAC_UL_ACK_SEND_ACK;
357 } else {
358 /* already triggered */
359 LOGP(DRLCMACUL, LOGL_DEBUG, "- Sending Ack/Nack is "
360 "already triggered, don't schedule!\n");
361 }
362 }
363}
364
Daniel Willmannca102af2014-08-08 12:14:12 +0200365/* Send Uplink unit-data to SGSN. */
366int gprs_rlcmac_ul_tbf::snd_ul_ud()
367{
368 uint8_t qos_profile[3];
369 struct msgb *llc_pdu;
370 unsigned msg_len = NS_HDR_LEN + BSSGP_HDR_LEN + m_llc.frame_length();
371 struct bssgp_bvc_ctx *bctx = gprs_bssgp_pcu_current_bctx();
372
373 LOGP(DBSSGP, LOGL_INFO, "LLC [PCU -> SGSN] %s len=%d\n", tbf_name(this), m_llc.frame_length());
374 if (!bctx) {
375 LOGP(DBSSGP, LOGL_ERROR, "No bctx\n");
376 m_llc.reset_frame_space();
377 return -EIO;
378 }
379
380 llc_pdu = msgb_alloc_headroom(msg_len, msg_len,"llc_pdu");
381 uint8_t *buf = msgb_push(llc_pdu, TL16V_GROSS_LEN(sizeof(uint8_t)*m_llc.frame_length()));
382 tl16v_put(buf, BSSGP_IE_LLC_PDU, sizeof(uint8_t)*m_llc.frame_length(), m_llc.frame);
383 qos_profile[0] = QOS_PROFILE >> 16;
384 qos_profile[1] = QOS_PROFILE >> 8;
385 qos_profile[2] = QOS_PROFILE;
386 bssgp_tx_ul_ud(bctx, tlli(), qos_profile, llc_pdu);
387
388 m_llc.reset_frame_space();
389 return 0;
390}
391