blob: 227fa36266f5a8be7724b4ad98ace82205270ab2 [file] [log] [blame]
Holger Hans Peter Freyther58db60c2013-11-13 20:45:40 +01001/*
2 * Copyright (C) 2013 by Holger Hans Peter Freyther
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19#include "tbf.h"
Holger Hans Peter Freytherdf6b4f52013-11-24 17:05:48 +010020#include "bts.h"
21#include "gprs_debug.h"
Holger Hans Peter Freyther58db60c2013-11-13 20:45:40 +010022
Jacob Erlbeck4abc6862015-12-08 15:14:05 +010023#include <errno.h>
24
Holger Hans Peter Freyther58db60c2013-11-13 20:45:40 +010025extern "C" {
26#include <osmocom/core/utils.h>
27}
28
29
30uint8_t *gprs_rlc_data::prepare(size_t block_data_len)
31{
32 /* todo.. only set it once if it turns out to be a bottleneck */
Holger Hans Peter Freyther88553ab2013-11-26 21:01:04 +010033 memset(block, 0x0, sizeof(block));
Holger Hans Peter Freyther58db60c2013-11-13 20:45:40 +010034 memset(block, 0x2b, block_data_len);
35
36 return block;
37}
Holger Hans Peter Freyther321f3c32013-11-23 16:06:54 +010038
Holger Hans Peter Freyther423dd222013-11-25 23:24:29 +010039void gprs_rlc_data::put_data(const uint8_t *data, size_t data_len)
40{
41 memcpy(block, data, data_len);
42 len = data_len;
43}
44
Holger Hans Peter Freyther321f3c32013-11-23 16:06:54 +010045void gprs_rlc_v_b::reset()
46{
47 for (size_t i = 0; i < ARRAY_SIZE(m_v_b); ++i)
48 mark_invalid(i);
49}
Holger Hans Peter Freyther15777792013-11-24 00:18:47 +010050
Daniel Willmann7e994e32014-08-07 15:49:21 +020051void gprs_rlc_dl_window::reset()
52{
53 m_v_s = 0;
54 m_v_a = 0;
55 m_v_b.reset();
56}
57
Daniel Willmann146514e2013-12-28 18:24:42 +010058int gprs_rlc_dl_window::resend_needed()
Holger Hans Peter Freyther15777792013-11-24 00:18:47 +010059{
Daniel Willmann146514e2013-12-28 18:24:42 +010060 for (uint16_t bsn = v_a(); bsn != v_s(); bsn = (bsn + 1) & mod_sns()) {
61 if (m_v_b.is_nacked(bsn) || m_v_b.is_resend(bsn))
Holger Hans Peter Freyther15777792013-11-24 00:18:47 +010062 return bsn;
63 }
64
65 return -1;
66}
Holger Hans Peter Freyther8b16ae32013-11-24 00:38:54 +010067
Daniel Willmann146514e2013-12-28 18:24:42 +010068int gprs_rlc_dl_window::mark_for_resend()
Holger Hans Peter Freyther8b16ae32013-11-24 00:38:54 +010069{
70 int resend = 0;
71
Daniel Willmann146514e2013-12-28 18:24:42 +010072 for (uint16_t bsn = v_a(); bsn != v_s(); bsn = (bsn + 1) & mod_sns()) {
73 if (m_v_b.is_unacked(bsn)) {
Holger Hans Peter Freyther8b16ae32013-11-24 00:38:54 +010074 /* mark to be re-send */
Daniel Willmann146514e2013-12-28 18:24:42 +010075 m_v_b.mark_resend(bsn);
Holger Hans Peter Freyther8b16ae32013-11-24 00:38:54 +010076 resend += 1;
77 }
78 }
79
80 return resend;
81}
Holger Hans Peter Freytherdf6b4f52013-11-24 17:05:48 +010082
Daniel Willmann146514e2013-12-28 18:24:42 +010083int gprs_rlc_dl_window::count_unacked()
Holger Hans Peter Freyther9c5539d2013-11-24 17:34:17 +010084{
85 uint16_t unacked = 0;
86 uint16_t bsn;
87
Daniel Willmann146514e2013-12-28 18:24:42 +010088 for (bsn = v_a(); bsn != v_s(); bsn = (bsn + 1) & mod_sns()) {
89 if (!m_v_b.is_acked(bsn))
Holger Hans Peter Freyther9c5539d2013-11-24 17:34:17 +010090 unacked += 1;
91 }
92
93 return unacked;
94}
95
Daniel Willmanncc5a4cb2013-12-11 20:04:29 +010096static uint16_t bitnum_to_bsn(int bitnum, uint16_t ssn, uint16_t mod_sns)
97{
98 return (ssn - 1 - bitnum) & mod_sns;
99}
100
Daniel Willmann146514e2013-12-28 18:24:42 +0100101void gprs_rlc_dl_window::update(BTS *bts, char *show_rbb, uint8_t ssn,
Holger Hans Peter Freytherdf6b4f52013-11-24 17:05:48 +0100102 uint16_t *lost, uint16_t *received)
103{
Holger Hans Peter Freytherdf6b4f52013-11-24 17:05:48 +0100104 /* SSN - 1 is in range V(A)..V(S)-1 */
Daniel Willmann146514e2013-12-28 18:24:42 +0100105 for (int bitpos = 0; bitpos < ws(); bitpos++) {
106 uint16_t bsn = bitnum_to_bsn(bitpos, ssn, mod_sns());
Holger Hans Peter Freytherdf6b4f52013-11-24 17:05:48 +0100107
Daniel Willmann146514e2013-12-28 18:24:42 +0100108 if (bsn == ((v_a() - 1) & mod_sns()))
Daniel Willmanncc5a4cb2013-12-11 20:04:29 +0100109 break;
110
Daniel Willmann146514e2013-12-28 18:24:42 +0100111 if (show_rbb[ws() - 1 - bitpos] == 'R') {
Holger Hans Peter Freytherdf6b4f52013-11-24 17:05:48 +0100112 LOGP(DRLCMACDL, LOGL_DEBUG, "- got ack for BSN=%d\n", bsn);
Daniel Willmann146514e2013-12-28 18:24:42 +0100113 if (!m_v_b.is_acked(bsn))
Holger Hans Peter Freytherdf6b4f52013-11-24 17:05:48 +0100114 *received += 1;
Daniel Willmann146514e2013-12-28 18:24:42 +0100115 m_v_b.mark_acked(bsn);
Holger Hans Peter Freytherdf6b4f52013-11-24 17:05:48 +0100116 } else {
117 LOGP(DRLCMACDL, LOGL_DEBUG, "- got NACK for BSN=%d\n", bsn);
Daniel Willmann146514e2013-12-28 18:24:42 +0100118 m_v_b.mark_nacked(bsn);
Holger Hans Peter Freytherdf6b4f52013-11-24 17:05:48 +0100119 bts->rlc_nacked();
120 *lost += 1;
121 }
122 }
123}
Holger Hans Peter Freythere358ff82013-11-24 17:13:10 +0100124
Daniel Willmann146514e2013-12-28 18:24:42 +0100125int gprs_rlc_dl_window::move_window()
Holger Hans Peter Freythere358ff82013-11-24 17:13:10 +0100126{
127 int i;
128 uint16_t bsn;
129 int moved = 0;
130
Daniel Willmann146514e2013-12-28 18:24:42 +0100131 for (i = 0, bsn = v_a(); bsn != v_s(); i++, bsn = (bsn + 1) & mod_sns()) {
132 if (m_v_b.is_acked(bsn)) {
133 m_v_b.mark_invalid(bsn);
Holger Hans Peter Freythere358ff82013-11-24 17:13:10 +0100134 moved += 1;
135 } else
136 break;
137 }
138
139 return moved;
140}
Holger Hans Peter Freytherbc155702013-11-24 17:20:35 +0100141
Daniel Willmann3ce011f2014-01-15 12:45:56 +0100142void gprs_rlc_dl_window::show_state(char *show_v_b)
Holger Hans Peter Freytherbc155702013-11-24 17:20:35 +0100143{
144 int i;
145 uint16_t bsn;
146
Daniel Willmann146514e2013-12-28 18:24:42 +0100147 for (i = 0, bsn = v_a(); bsn != v_s(); i++, bsn = (bsn + 1) & mod_sns()) {
Holger Hans Peter Freyther3cbf9e02013-11-26 21:43:58 +0100148 uint16_t index = bsn & mod_sns_half();
Daniel Willmannd54d9f52013-12-28 21:16:13 +0100149 switch(m_v_b.get_state(index)) {
150 case GPRS_RLC_DL_BSN_INVALID:
151 show_v_b[i] = 'I';
152 break;
153 case GPRS_RLC_DL_BSN_ACKED:
154 show_v_b[i] = 'A';
155 break;
156 case GPRS_RLC_DL_BSN_RESEND:
157 show_v_b[i] = 'X';
158 break;
159 case GPRS_RLC_DL_BSN_NACKED:
160 show_v_b[i] = 'N';
161 break;
Daniel Willmann3ce011f2014-01-15 12:45:56 +0100162 default:
163 show_v_b[i] = '?';
Daniel Willmannd54d9f52013-12-28 21:16:13 +0100164 }
Holger Hans Peter Freytherbc155702013-11-24 17:20:35 +0100165 }
166 show_v_b[i] = '\0';
167}
Holger Hans Peter Freythere9b1ebb2013-11-24 22:00:43 +0100168
169void gprs_rlc_v_n::reset()
170{
Daniel Willmannd54d9f52013-12-28 21:16:13 +0100171 for (size_t i = 0; i < ARRAY_SIZE(m_v_n); ++i)
172 m_v_n[i] = GPRS_RLC_UL_BSN_INVALID;
Holger Hans Peter Freythere9b1ebb2013-11-24 22:00:43 +0100173}
Holger Hans Peter Freythercbb00eb2013-11-25 23:26:06 +0100174
Daniel Willmann8a31f9e2013-11-27 17:08:35 +0100175/* Update the receive block bitmap */
Daniel Willmann7c3751b2013-12-28 13:59:24 +0100176void gprs_rlc_ul_window::update_rbb(char *rbb)
Daniel Willmann8a31f9e2013-11-27 17:08:35 +0100177{
178 int i;
179 for (i=0; i < ws(); i++) {
Daniel Willmann7c3751b2013-12-28 13:59:24 +0100180 if (m_v_n.is_received(ssn()-1-i))
Daniel Willmann8a31f9e2013-11-27 17:08:35 +0100181 rbb[ws()-1-i] = 'R';
182 else
183 rbb[ws()-1-i] = 'I';
184 }
185}
186
Holger Hans Peter Freythercbb00eb2013-11-25 23:26:06 +0100187/* Raise V(R) to highest received sequence number not received. */
Daniel Willmann7c3751b2013-12-28 13:59:24 +0100188void gprs_rlc_ul_window::raise_v_r(const uint16_t bsn)
Holger Hans Peter Freythercbb00eb2013-11-25 23:26:06 +0100189{
190 uint16_t offset_v_r;
191 offset_v_r = (bsn + 1 - v_r()) & mod_sns();
192 /* Positive offset, so raise. */
193 if (offset_v_r < (sns() >> 1)) {
194 while (offset_v_r--) {
195 if (offset_v_r) /* all except the received block */
Daniel Willmann7c3751b2013-12-28 13:59:24 +0100196 m_v_n.mark_missing(v_r());
Daniel Willmannf4a1ec62013-12-28 13:57:31 +0100197 raise_v_r_to(1);
Holger Hans Peter Freythercbb00eb2013-11-25 23:26:06 +0100198 }
199 LOGP(DRLCMACUL, LOGL_DEBUG, "- Raising V(R) to %d\n", v_r());
200 }
201}
Holger Hans Peter Freyther7f3e6622013-11-25 23:51:19 +0100202
203/*
204 * Raise V(Q) if possible. This is looped until there is a gap
205 * (non received block) or the window is empty.
206 */
Daniel Willmann7c3751b2013-12-28 13:59:24 +0100207uint16_t gprs_rlc_ul_window::raise_v_q()
Holger Hans Peter Freyther7f3e6622013-11-25 23:51:19 +0100208{
209 uint16_t count = 0;
210
211 while (v_q() != v_r()) {
Daniel Willmann7c3751b2013-12-28 13:59:24 +0100212 if (!m_v_n.is_received(v_q()))
Holger Hans Peter Freyther7f3e6622013-11-25 23:51:19 +0100213 break;
214 LOGP(DRLCMACUL, LOGL_DEBUG, "- Taking block %d out, raising "
215 "V(Q) to %d\n", v_q(), (v_q() + 1) & mod_sns());
216 raise_v_q(1);
217 count += 1;
218 }
219
220 return count;
221}
Daniel Willmann55844792013-12-28 14:41:00 +0100222
Jacob Erlbeckd87e1d62015-12-14 11:43:04 +0100223void gprs_rlc_ul_window::receive_bsn(const uint16_t bsn)
Daniel Willmann55844792013-12-28 14:41:00 +0100224{
225 m_v_n.mark_received(bsn);
226 raise_v_r(bsn);
Jacob Erlbeck784a0bd2015-12-14 11:54:29 +0100227}
Daniel Willmann55844792013-12-28 14:41:00 +0100228
Jacob Erlbeck784a0bd2015-12-14 11:54:29 +0100229bool gprs_rlc_ul_window::invalidate_bsn(const uint16_t bsn)
230{
231 bool was_valid = m_v_n.is_received(bsn);
232 m_v_n.mark_missing(bsn);
233
234 return was_valid;
Daniel Willmann55844792013-12-28 14:41:00 +0100235}