blob: 4237162a9832f8220bf34a6e6f70b899fe3ab65a [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;
53 const struct gprs_rlc_ul_data_block_info *rdbi = &_data->block_info;
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 Willmannca102af2014-08-08 12:14:12 +020089
90struct 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 Erlbecka24e1cd2015-12-22 14:59:13 +0100119 /*
Daniel Willmannca102af2014-08-08 12:14:12 +0200120 RlcMacDownlink_t * mac_control_block = (RlcMacDownlink_t *)talloc_zero(tall_pcu_ctx, RlcMacDownlink_t);
121 Encoding::write_packet_uplink_ack(bts_data(), mac_control_block, this, final);
122 encode_gsm_rlcmac_downlink(ack_vec, mac_control_block);
Jacob Erlbecka24e1cd2015-12-22 14:59:13 +0100123 */
124 Encoding::write_packet_uplink_ack(bts_data(), ack_vec, this, final);
Daniel Willmannca102af2014-08-08 12:14:12 +0200125 bitvec_pack(ack_vec, msgb_put(msg, 23));
126 bitvec_free(ack_vec);
Jacob Erlbecka24e1cd2015-12-22 14:59:13 +0100127 /*
Daniel Willmannca102af2014-08-08 12:14:12 +0200128 talloc_free(mac_control_block);
Jacob Erlbecka24e1cd2015-12-22 14:59:13 +0100129 */
Daniel Willmannca102af2014-08-08 12:14:12 +0200130
131 /* now we must set this flag, so we are allowed to assign downlink
132 * TBF on PACCH. it is only allowed when TLLI is acknowledged. */
133 m_contention_resolution_done = 1;
134
135 if (final) {
136 poll_state = GPRS_RLCMAC_POLL_SCHED;
137 poll_fn = (fn + 13) % 2715648;
138 /* waiting for final acknowledge */
139 ul_ack_state = GPRS_RLCMAC_UL_ACK_WAIT_ACK;
140 m_final_ack_sent = 1;
141 } else
142 ul_ack_state = GPRS_RLCMAC_UL_ACK_NONE;
143
144 return msg;
145}
146
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100147int gprs_rlcmac_ul_tbf::rcv_data_block_acknowledged(
148 const struct gprs_rlc_ul_header_egprs *rlc,
149 uint8_t *data, uint8_t len, struct pcu_l1_meas *meas)
150{
151 int8_t rssi = meas->have_rssi ? meas->rssi : 0;
152
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100153 const uint16_t ws = m_window.ws();
154
155 this->state_flags |= (1 << GPRS_RLCMAC_FLAG_UL_DATA);
156
157 LOGP(DRLCMACUL, LOGL_DEBUG, "UL DATA TFI=%d received (V(Q)=%d .. "
158 "V(R)=%d)\n", rlc->tfi, this->m_window.v_q(),
159 this->m_window.v_r());
160
161 /* process RSSI */
162 gprs_rlcmac_rssi(this, rssi);
163
164 /* store measurement values */
165 if (ms())
166 ms()->update_l1_meas(meas);
167
168 uint32_t new_tlli = 0;
169 unsigned int block_idx;
170
171 /* restart T3169 */
172 tbf_timer_start(this, 3169, bts_data()->t3169, 0);
173
174 /* Increment RX-counter */
175 this->m_rx_counter++;
176
177 /* Loop over num_blocks */
178 for (block_idx = 0; block_idx < rlc->num_data_blocks; block_idx++) {
179 int num_chunks;
180 uint8_t *rlc_data;
181 const struct gprs_rlc_ul_data_block_info *rdbi =
182 &rlc->block_info[block_idx];
183 bool need_rlc_data = false;
184 struct gprs_rlc_data *block;
185
186 LOGP(DRLCMACUL, LOGL_DEBUG,
187 "%s: Got %s RLC data block: "
188 "CV=%d, BSN=%d, SPB=%d, "
189 "PI=%d, E=%d, TI=%d, bitoffs=%d\n",
190 name(), rlc->cs.name(),
191 rdbi->cv, rdbi->bsn, rdbi->spb,
192 rdbi->pi, rdbi->e, rdbi->ti,
193 rlc->data_offs_bits[block_idx]);
194
195 /* Check whether the block needs to be decoded */
196
197 if (!m_window.is_in_window(rdbi->bsn)) {
198 LOGP(DRLCMACUL, LOGL_DEBUG, "- BSN %d out of window "
199 "%d..%d (it's normal)\n", rdbi->bsn,
200 m_window.v_q(),
Jacob Erlbeck93c55d02015-12-23 16:29:07 +0100201 m_window.mod_sns(m_window.v_q() + ws - 1));
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100202 } else if (m_window.is_received(rdbi->bsn)) {
203 LOGP(DRLCMACUL, LOGL_DEBUG,
204 "- BSN %d already received\n", rdbi->bsn);
205 } else {
206 need_rlc_data = true;
207 }
208
209 if (!is_tlli_valid()) {
210 if (!rdbi->ti) {
211 LOGP(DRLCMACUL, LOGL_NOTICE,
212 "%s: Missing TLLI within UL DATA.\n",
213 name());
214 continue;
215 }
216 need_rlc_data = true;
217 }
218
219 if (!need_rlc_data)
220 continue;
221
222 /* Store block and meta info to BSN buffer */
223
224 LOGP(DRLCMACUL, LOGL_DEBUG, "- BSN %d storing in window (%d..%d)\n",
225 rdbi->bsn, m_window.v_q(),
Jacob Erlbeck93c55d02015-12-23 16:29:07 +0100226 m_window.mod_sns(m_window.v_q() + ws - 1));
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100227 block = m_rlc.block(rdbi->bsn);
228 block->block_info = *rdbi;
229 block->cs = rlc->cs;
230 OSMO_ASSERT(rdbi->data_len < sizeof(block->block));
231 rlc_data = &(block->block[0]);
232 /* TODO: Handle SPB != 0 -> Set length to 2*len, add offset if
233 * 2nd part. Note that resegmentation is currently disabled
234 * within the UL assignment.
235 */
236 if (rdbi->spb) {
237 LOGP(DRLCMACUL, LOGL_NOTICE,
238 "Got SPB != 0 but resegmentation has been "
239 "disabled, skipping %s data block with BSN %d, "
240 "TFI=%d.\n", rlc->cs.name(), rdbi->bsn,
241 rlc->tfi);
242 continue;
243 }
244
245 block->len =
246 Decoding::rlc_copy_to_aligned_buffer(rlc, block_idx, data,
247 rlc_data);
248
Jacob Erlbeckd88bb2e2015-12-11 14:53:29 +0100249 LOGP(DRLCMACUL, LOGL_DEBUG,
250 "%s: data_length=%d, data=%s\n",
251 name(), block->len, osmo_hexdump(rlc_data, block->len));
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100252
253 /* TODO: Handle SPB != 0 -> set state to partly received
254 * (upper/lower) and continue with the loop, unless the other
255 * part is already present.
256 */
257
258 /* Get/Handle TLLI */
259 if (rdbi->ti) {
260 num_chunks = Decoding::rlc_data_from_ul_data(
261 rdbi, rlc->cs, rlc_data, NULL, 0, &new_tlli);
262
263 if (num_chunks < 0) {
264 bts->decode_error();
265 LOGP(DRLCMACUL, LOGL_NOTICE,
266 "Failed to decode TLLI of %s UL DATA "
267 "TFI=%d.\n", rlc->cs.name(), rlc->tfi);
268 m_window.invalidate_bsn(rdbi->bsn);
269 continue;
270 }
271 if (!this->is_tlli_valid()) {
272 if (!new_tlli) {
273 LOGP(DRLCMACUL, LOGL_NOTICE,
274 "%s: TLLI = 0 within UL DATA.\n",
275 name());
276 m_window.invalidate_bsn(rdbi->bsn);
277 continue;
278 }
279 LOGP(DRLCMACUL, LOGL_INFO,
280 "Decoded premier TLLI=0x%08x of "
281 "UL DATA TFI=%d.\n", tlli(), rlc->tfi);
282 set_tlli_from_ul(new_tlli);
283 } else if (new_tlli && new_tlli != tlli()) {
284 LOGP(DRLCMACUL, LOGL_NOTICE, "TLLI mismatch on UL "
285 "DATA TFI=%d. (Ignoring due to contention "
286 "resolution)\n", rlc->tfi);
287 m_window.invalidate_bsn(rdbi->bsn);
288 continue;
289 }
290 }
291
292 m_window.receive_bsn(rdbi->bsn);
293 }
294
295 /* Raise V(Q) if possible, and retrieve LLC frames from blocks.
296 * This is looped until there is a gap (non received block) or
297 * the window is empty.*/
298 const uint16_t v_q_beg = m_window.v_q();
299 const uint16_t count = m_window.raise_v_q();
300
301 /* Retrieve LLC frames from blocks that are ready */
302 for (uint16_t i = 0; i < count; ++i) {
Jacob Erlbeck93c55d02015-12-23 16:29:07 +0100303 uint16_t index = m_window.mod_sns(v_q_beg + i);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100304 assemble_forward_llc(m_rlc.block(index));
305 }
306
307 /* Check CV of last frame in buffer */
308 if (this->state_is(GPRS_RLCMAC_FLOW) /* still in flow state */
309 && this->m_window.v_q() == this->m_window.v_r()) { /* if complete */
310 struct gprs_rlc_data *block =
Jacob Erlbeck93c55d02015-12-23 16:29:07 +0100311 m_rlc.block(m_window.mod_sns(m_window.v_r() - 1));
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100312 const struct gprs_rlc_ul_data_block_info *rdbi =
313 &block->block_info;
314
315 LOGP(DRLCMACUL, LOGL_DEBUG, "- No gaps in received block, "
316 "last block: BSN=%d CV=%d\n", rdbi->bsn,
317 rdbi->cv);
318 if (rdbi->cv == 0) {
319 LOGP(DRLCMACUL, LOGL_DEBUG, "- Finished with UL "
320 "TBF\n");
321 set_state(GPRS_RLCMAC_FINISHED);
322 /* Reset N3103 counter. */
323 this->m_n3103 = 0;
324 }
325 }
326
327 /* If TLLI is included or if we received half of the window, we send
328 * an ack/nack */
329 maybe_schedule_uplink_acknack(rlc);
330
331 return 0;
332}
333
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100334void gprs_rlcmac_ul_tbf::maybe_schedule_uplink_acknack(
335 const gprs_rlc_ul_header_egprs *rlc)
336{
337 bool have_ti = rlc->block_info[0].ti ||
338 (rlc->num_data_blocks > 1 && rlc->block_info[1].ti);
339
340 if (rlc->si || have_ti || state_is(GPRS_RLCMAC_FINISHED) ||
341 (m_rx_counter % SEND_ACK_AFTER_FRAMES) == 0)
342 {
343 if (rlc->si) {
344 LOGP(DRLCMACUL, LOGL_NOTICE, "- Scheduling Ack/Nack, "
345 "because MS is stalled.\n");
346 }
347 if (have_ti) {
348 LOGP(DRLCMACUL, LOGL_DEBUG, "- Scheduling Ack/Nack, "
349 "because TLLI is included.\n");
350 }
351 if (state_is(GPRS_RLCMAC_FINISHED)) {
352 LOGP(DRLCMACUL, LOGL_DEBUG, "- Scheduling Ack/Nack, "
353 "because last block has CV==0.\n");
354 }
355 if ((m_rx_counter % SEND_ACK_AFTER_FRAMES) == 0) {
356 LOGP(DRLCMACUL, LOGL_DEBUG, "- Scheduling Ack/Nack, "
357 "because %d frames received.\n",
358 SEND_ACK_AFTER_FRAMES);
359 }
360 if (ul_ack_state == GPRS_RLCMAC_UL_ACK_NONE) {
361 /* trigger sending at next RTS */
362 ul_ack_state = GPRS_RLCMAC_UL_ACK_SEND_ACK;
363 } else {
364 /* already triggered */
365 LOGP(DRLCMACUL, LOGL_DEBUG, "- Sending Ack/Nack is "
366 "already triggered, don't schedule!\n");
367 }
368 }
369}
370
Daniel Willmannca102af2014-08-08 12:14:12 +0200371/* Send Uplink unit-data to SGSN. */
372int gprs_rlcmac_ul_tbf::snd_ul_ud()
373{
374 uint8_t qos_profile[3];
375 struct msgb *llc_pdu;
376 unsigned msg_len = NS_HDR_LEN + BSSGP_HDR_LEN + m_llc.frame_length();
377 struct bssgp_bvc_ctx *bctx = gprs_bssgp_pcu_current_bctx();
378
379 LOGP(DBSSGP, LOGL_INFO, "LLC [PCU -> SGSN] %s len=%d\n", tbf_name(this), m_llc.frame_length());
380 if (!bctx) {
381 LOGP(DBSSGP, LOGL_ERROR, "No bctx\n");
382 m_llc.reset_frame_space();
383 return -EIO;
384 }
385
386 llc_pdu = msgb_alloc_headroom(msg_len, msg_len,"llc_pdu");
387 uint8_t *buf = msgb_push(llc_pdu, TL16V_GROSS_LEN(sizeof(uint8_t)*m_llc.frame_length()));
388 tl16v_put(buf, BSSGP_IE_LLC_PDU, sizeof(uint8_t)*m_llc.frame_length(), m_llc.frame);
389 qos_profile[0] = QOS_PROFILE >> 16;
390 qos_profile[1] = QOS_PROFILE >> 8;
391 qos_profile[2] = QOS_PROFILE;
392 bssgp_tx_ul_ud(bctx, tlli(), qos_profile, llc_pdu);
393
394 m_llc.reset_frame_space();
395 return 0;
396}
397