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