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