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