blob: 3ab71f05b267b5ec0b0f43cf63b9c13a76cc6608 [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>
30
31extern "C" {
32#include <osmocom/core/msgb.h>
33#include <osmocom/core/talloc.h>
34}
35
36#include <errno.h>
37#include <string.h>
38
39/* After receiving these frames, we send ack/nack. */
40#define SEND_ACK_AFTER_FRAMES 20
41
42extern void *tall_pcu_ctx;
43
44
45/*
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 rlc_ul_header *rh = (const struct rlc_ul_header *) data;
54 uint8_t e, m;
55 struct rlc_li_field *li;
56 uint8_t frame_offset[16], offset = 0, chunk;
57 int i, frames = 0;
58
59 LOGP(DRLCMACUL, LOGL_DEBUG, "- Assembling frames: (len=%d)\n", len);
60
61 data += 3;
62 len -= 3;
63 e = rh->e; /* if extended */
64 m = 1; /* more frames, that means: the first frame */
65
66 /* Parse frame offsets from length indicator(s), if any. */
67 while (1) {
68 if (frames == (int)sizeof(frame_offset)) {
69 LOGP(DRLCMACUL, LOGL_ERROR, "%s too many frames in "
70 "block\n", tbf_name(this));
71 return -EINVAL;
72 }
73 frame_offset[frames++] = offset;
74 LOGP(DRLCMACUL, LOGL_DEBUG, "-- Frame %d starts at offset "
75 "%d\n", frames, offset);
76 if (!len)
77 break;
78 /* M == 0 and E == 0 is not allowed in this version. */
79 if (!m && !e) {
80 LOGP(DRLCMACUL, LOGL_NOTICE, "%s UL DATA "
81 "ignored, because M='0' and E='0'.\n",
82 tbf_name(this));
83 return 0;
84 }
85 /* no more frames in this segment */
86 if (e) {
87 break;
88 }
89 /* There is a new frame and an LI that delimits it. */
90 if (m) {
91 li = (struct rlc_li_field *)data;
92 LOGP(DRLCMACUL, LOGL_DEBUG, "-- Delimiter len=%d\n",
93 li->li);
94 /* Special case: LI == 0
95 * If the last segment would fit precisely into the
96 * rest of the RLC MAC block, there would be no way
97 * to delimit that this segment ends and is not
98 * continued in the next block.
99 * The special LI (0) is used to force the segment to
100 * extend into the next block, so it is delimited there.
101 * This LI must be skipped. Also it is the last LI.
102 */
103 if (li->li == 0) {
104 data++;
105 len--;
106 m = 1; /* M is ignored, we know there is more */
107 break; /* handle E as '1', so we break! */
108 }
109 e = li->e;
110 m = li->m;
111 offset += li->li;
112 data++;
113 len--;
114 continue;
115 }
116 }
117 if (!m) {
118 LOGP(DRLCMACUL, LOGL_DEBUG, "- Last frame carries spare "
119 "data\n");
120 }
121
122 LOGP(DRLCMACUL, LOGL_DEBUG, "- Data length after length fields: %d\n",
123 len);
124 /* TLLI */
125 if (rh->ti) {
126 if (len < 4) {
127 LOGP(DRLCMACUL, LOGL_NOTICE, "%s UL DATA TLLI out of "
128 "frame border\n", tbf_name(this));
129 return -EINVAL;
130 }
131 data += 4;
132 len -= 4;
133 LOGP(DRLCMACUL, LOGL_DEBUG, "- Length after skipping TLLI: "
134 "%d\n", len);
135 }
136
137 /* PFI */
138 if (rh->pi) {
139 LOGP(DRLCMACUL, LOGL_ERROR, "ERROR: PFI not supported, "
140 "please disable in SYSTEM INFORMATION\n");
141 if (len < 1) {
142 LOGP(DRLCMACUL, LOGL_NOTICE, "%s UL DATA PFI out of "
143 "frame border\n", tbf_name(this));
144 return -EINVAL;
145 }
146 data++;
147 len--;
148 LOGP(DRLCMACUL, LOGL_DEBUG, "- Length after skipping PFI: "
149 "%d\n", len);
150 }
151
152 /* Now we have:
153 * - a list of frames offsets: frame_offset[]
154 * - number of frames: i
155 * - m == 0: Last frame carries spare data (end of TBF).
156 */
157
158 /* Check if last offset would exceed frame. */
159 if (offset > len) {
160 LOGP(DRLCMACUL, LOGL_NOTICE, "%s UL DATA ignored, "
161 "because LI delimits data that exceeds block size.\n",
162 tbf_name(this));
163 return -EINVAL;
164 }
165
166 /* create LLC frames */
167 for (i = 0; i < frames; i++) {
168 /* last frame ? */
169 if (i == frames - 1) {
170 /* no more data in last frame */
171 if (!m)
172 break;
173 /* data until end of frame */
174 chunk = len - frame_offset[i];
175 } else {
176 /* data until next frame */
177 chunk = frame_offset[i + 1] - frame_offset[i];
178 }
179 if (!m_llc.fits_in_current_frame(chunk)) {
180 LOGP(DRLCMACUL, LOGL_NOTICE, "%s LLC frame exceeds "
181 "maximum size %u.\n", tbf_name(this),
182 m_llc.remaining_space());
183 chunk = m_llc.remaining_space();
184 }
185 m_llc.append_frame(data + frame_offset[i], chunk);
186 m_llc.consume(chunk);
187 /* not last frame. */
188 if (i != frames - 1) {
189 /* send frame to SGSN */
190 LOGP(DRLCMACUL, LOGL_INFO, "%s complete UL frame len=%d\n",
191 tbf_name(this) , m_llc.frame_length());
192 snd_ul_ud();
193 m_llc.reset();
194 /* also check if CV==0, because the frame may fill up the
195 * block precisely, then it is also complete. normally the
196 * frame would be extended into the next block with a 0-length
197 * delimiter added to this block. */
198 } else if (rh->cv == 0) {
199 /* send frame to SGSN */
200 LOGP(DRLCMACUL, LOGL_INFO, "%s complete UL frame "
201 "that fits precisely in last block: "
202 "len=%d\n", tbf_name(this), m_llc.frame_length());
203 snd_ul_ud();
204 m_llc.reset();
205 }
206 }
207
208 return 0;
209}
210
211
212struct msgb *gprs_rlcmac_ul_tbf::create_ul_ack(uint32_t fn)
213{
214 int final = (state_is(GPRS_RLCMAC_FINISHED));
215 struct msgb *msg;
216
217 if (final) {
218 if (poll_state != GPRS_RLCMAC_POLL_NONE) {
219 LOGP(DRLCMACUL, LOGL_DEBUG, "Polling is already "
220 "sheduled for %s, so we must wait for "
221 "final uplink ack...\n", tbf_name(this));
222 return NULL;
223 }
224 if (bts->sba()->find(trx->trx_no, control_ts, (fn + 13) % 2715648)) {
225 LOGP(DRLCMACUL, LOGL_DEBUG, "Polling is already "
226 "scheduled for single block allocation...\n");
227 return NULL;
228 }
229 }
230
231 msg = msgb_alloc(23, "rlcmac_ul_ack");
232 if (!msg)
233 return NULL;
234 bitvec *ack_vec = bitvec_alloc(23);
235 if (!ack_vec) {
236 msgb_free(msg);
237 return NULL;
238 }
239 bitvec_unhex(ack_vec,
240 "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b");
241 RlcMacDownlink_t * mac_control_block = (RlcMacDownlink_t *)talloc_zero(tall_pcu_ctx, RlcMacDownlink_t);
242 Encoding::write_packet_uplink_ack(bts_data(), mac_control_block, this, final);
243 encode_gsm_rlcmac_downlink(ack_vec, mac_control_block);
244 bitvec_pack(ack_vec, msgb_put(msg, 23));
245 bitvec_free(ack_vec);
246 talloc_free(mac_control_block);
247
248 /* now we must set this flag, so we are allowed to assign downlink
249 * TBF on PACCH. it is only allowed when TLLI is acknowledged. */
250 m_contention_resolution_done = 1;
251
252 if (final) {
253 poll_state = GPRS_RLCMAC_POLL_SCHED;
254 poll_fn = (fn + 13) % 2715648;
255 /* waiting for final acknowledge */
256 ul_ack_state = GPRS_RLCMAC_UL_ACK_WAIT_ACK;
257 m_final_ack_sent = 1;
258 } else
259 ul_ack_state = GPRS_RLCMAC_UL_ACK_NONE;
260
261 return msg;
262}
263
264int gprs_rlcmac_ul_tbf::rcv_data_block_acknowledged(const uint8_t *data, size_t len, int8_t rssi)
265{
266 struct rlc_ul_header *rh = (struct rlc_ul_header *)data;
267 int rc;
268
269 const uint16_t mod_sns = m_window.mod_sns();
270 const uint16_t ws = m_window.ws();
271
272 this->state_flags |= (1 << GPRS_RLCMAC_FLAG_UL_DATA);
273
274 LOGP(DRLCMACUL, LOGL_DEBUG, "UL DATA TFI=%d received (V(Q)=%d .. "
275 "V(R)=%d)\n", rh->tfi, this->m_window.v_q(),
276 this->m_window.v_r());
277
278 /* process RSSI */
279 gprs_rlcmac_rssi(this, rssi);
280
281 /* get TLLI */
282 if (!this->is_tlli_valid()) {
283 if (!extract_tlli(data, len))
284 return 0;
285 /* already have TLLI, but we stille get another one */
286 } else if (rh->ti) {
287 uint32_t tlli;
288 rc = Decoding::tlli_from_ul_data(data, len, &tlli);
289 if (rc) {
290 LOGP(DRLCMACUL, LOGL_NOTICE, "Failed to decode TLLI "
291 "of UL DATA TFI=%d.\n", rh->tfi);
292 return 0;
293 }
294 if (tlli != this->tlli()) {
295 LOGP(DRLCMACUL, LOGL_NOTICE, "TLLI mismatch on UL "
296 "DATA TFI=%d. (Ignoring due to contention "
297 "resolution)\n", rh->tfi);
298 return 0;
299 }
300 }
301
302 /* restart T3169 */
303 tbf_timer_start(this, 3169, bts_data()->t3169, 0);
304
305 /* Increment RX-counter */
306 this->m_rx_counter++;
307
308 if (!m_window.is_in_window(rh->bsn)) {
309 LOGP(DRLCMACUL, LOGL_DEBUG, "- BSN %d out of window "
310 "%d..%d (it's normal)\n", rh->bsn,
311 m_window.v_q(),
312 (m_window.v_q() + ws - 1) & mod_sns);
313 maybe_schedule_uplink_acknack(rh);
314 return 0;
315 }
316
317 /* Write block to buffer and set receive state array. */
318 m_rlc.block(rh->bsn)->put_data(data, len);
319 LOGP(DRLCMACUL, LOGL_DEBUG, "- BSN %d storing in window (%d..%d)\n",
320 rh->bsn, m_window.v_q(),
321 (m_window.v_q() + ws - 1) & mod_sns);
322
323 /* Raise V(Q) if possible, and retrieve LLC frames from blocks.
324 * This is looped until there is a gap (non received block) or
325 * the window is empty.*/
326 const uint16_t v_q_beg = m_window.v_q();
327
328 const uint16_t count = m_window.receive_bsn(rh->bsn);
329
330 /* Retrieve LLC frames from blocks that are ready */
331 for (uint16_t i = 0; i < count; ++i) {
332 uint16_t index = (v_q_beg + i) & mod_sns;
333 assemble_forward_llc(m_rlc.block(index));
334 }
335
336 /* Check CV of last frame in buffer */
337 if (this->state_is(GPRS_RLCMAC_FLOW) /* still in flow state */
338 && this->m_window.v_q() == this->m_window.v_r()) { /* if complete */
339 struct rlc_ul_header *last_rh = (struct rlc_ul_header *)
340 m_rlc.block((m_window.v_r() - 1) & mod_sns)->block;
341 LOGP(DRLCMACUL, LOGL_DEBUG, "- No gaps in received block, "
342 "last block: BSN=%d CV=%d\n", last_rh->bsn,
343 last_rh->cv);
344 if (last_rh->cv == 0) {
345 LOGP(DRLCMACUL, LOGL_DEBUG, "- Finished with UL "
346 "TBF\n");
347 set_state(GPRS_RLCMAC_FINISHED);
348 /* Reset N3103 counter. */
349 this->m_n3103 = 0;
350 }
351 }
352
353 /* If TLLI is included or if we received half of the window, we send
354 * an ack/nack */
355 maybe_schedule_uplink_acknack(rh);
356
357 return 0;
358}
359
360void gprs_rlcmac_ul_tbf::maybe_schedule_uplink_acknack(const rlc_ul_header *rh)
361{
362 if (rh->si || rh->ti || state_is(GPRS_RLCMAC_FINISHED)
363 || (m_rx_counter % SEND_ACK_AFTER_FRAMES) == 0) {
364 if (rh->si) {
365 LOGP(DRLCMACUL, LOGL_NOTICE, "- Scheduling Ack/Nack, "
366 "because MS is stalled.\n");
367 }
368 if (rh->ti) {
369 LOGP(DRLCMACUL, LOGL_DEBUG, "- Scheduling Ack/Nack, "
370 "because TLLI is included.\n");
371 }
372 if (state_is(GPRS_RLCMAC_FINISHED)) {
373 LOGP(DRLCMACUL, LOGL_DEBUG, "- Scheduling Ack/Nack, "
374 "because last block has CV==0.\n");
375 }
376 if ((m_rx_counter % SEND_ACK_AFTER_FRAMES) == 0) {
377 LOGP(DRLCMACUL, LOGL_DEBUG, "- Scheduling Ack/Nack, "
378 "because %d frames received.\n",
379 SEND_ACK_AFTER_FRAMES);
380 }
381 if (ul_ack_state == GPRS_RLCMAC_UL_ACK_NONE) {
382 /* trigger sending at next RTS */
383 ul_ack_state = GPRS_RLCMAC_UL_ACK_SEND_ACK;
384 } else {
385 /* already triggered */
386 LOGP(DRLCMACUL, LOGL_DEBUG, "- Sending Ack/Nack is "
387 "already triggered, don't schedule!\n");
388 }
389 }
390}
391
392/* Send Uplink unit-data to SGSN. */
393int gprs_rlcmac_ul_tbf::snd_ul_ud()
394{
395 uint8_t qos_profile[3];
396 struct msgb *llc_pdu;
397 unsigned msg_len = NS_HDR_LEN + BSSGP_HDR_LEN + m_llc.frame_length();
398 struct bssgp_bvc_ctx *bctx = gprs_bssgp_pcu_current_bctx();
399
400 LOGP(DBSSGP, LOGL_INFO, "LLC [PCU -> SGSN] %s len=%d\n", tbf_name(this), m_llc.frame_length());
401 if (!bctx) {
402 LOGP(DBSSGP, LOGL_ERROR, "No bctx\n");
403 m_llc.reset_frame_space();
404 return -EIO;
405 }
406
407 llc_pdu = msgb_alloc_headroom(msg_len, msg_len,"llc_pdu");
408 uint8_t *buf = msgb_push(llc_pdu, TL16V_GROSS_LEN(sizeof(uint8_t)*m_llc.frame_length()));
409 tl16v_put(buf, BSSGP_IE_LLC_PDU, sizeof(uint8_t)*m_llc.frame_length(), m_llc.frame);
410 qos_profile[0] = QOS_PROFILE >> 16;
411 qos_profile[1] = QOS_PROFILE >> 8;
412 qos_profile[2] = QOS_PROFILE;
413 bssgp_tx_ul_ud(bctx, tlli(), qos_profile, llc_pdu);
414
415 m_llc.reset_frame_space();
416 return 0;
417}
418