blob: 42b602cbeb5f45f6897b20f91aa8e3865ede52d7 [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
23extern "C" {
24#include <osmocom/core/utils.h>
25}
26
27
28uint8_t *gprs_rlc_data::prepare(size_t block_data_len)
29{
30 /* todo.. only set it once if it turns out to be a bottleneck */
Holger Hans Peter Freyther88553ab2013-11-26 21:01:04 +010031 memset(block, 0x0, sizeof(block));
Holger Hans Peter Freyther58db60c2013-11-13 20:45:40 +010032 memset(block, 0x2b, block_data_len);
33
34 return block;
35}
Holger Hans Peter Freyther321f3c32013-11-23 16:06:54 +010036
Holger Hans Peter Freyther423dd222013-11-25 23:24:29 +010037void gprs_rlc_data::put_data(const uint8_t *data, size_t data_len)
38{
39 memcpy(block, data, data_len);
40 len = data_len;
41}
42
Holger Hans Peter Freyther321f3c32013-11-23 16:06:54 +010043void gprs_rlc_v_b::reset()
44{
45 for (size_t i = 0; i < ARRAY_SIZE(m_v_b); ++i)
46 mark_invalid(i);
47}
Holger Hans Peter Freyther15777792013-11-24 00:18:47 +010048
Daniel Willmann7e994e32014-08-07 15:49:21 +020049void gprs_rlc_dl_window::reset()
50{
51 m_v_s = 0;
52 m_v_a = 0;
53 m_v_b.reset();
54}
55
Daniel Willmann146514e2013-12-28 18:24:42 +010056int gprs_rlc_dl_window::resend_needed()
Holger Hans Peter Freyther15777792013-11-24 00:18:47 +010057{
Daniel Willmann146514e2013-12-28 18:24:42 +010058 for (uint16_t bsn = v_a(); bsn != v_s(); bsn = (bsn + 1) & mod_sns()) {
59 if (m_v_b.is_nacked(bsn) || m_v_b.is_resend(bsn))
Holger Hans Peter Freyther15777792013-11-24 00:18:47 +010060 return bsn;
61 }
62
63 return -1;
64}
Holger Hans Peter Freyther8b16ae32013-11-24 00:38:54 +010065
Daniel Willmann146514e2013-12-28 18:24:42 +010066int gprs_rlc_dl_window::mark_for_resend()
Holger Hans Peter Freyther8b16ae32013-11-24 00:38:54 +010067{
68 int resend = 0;
69
Daniel Willmann146514e2013-12-28 18:24:42 +010070 for (uint16_t bsn = v_a(); bsn != v_s(); bsn = (bsn + 1) & mod_sns()) {
71 if (m_v_b.is_unacked(bsn)) {
Holger Hans Peter Freyther8b16ae32013-11-24 00:38:54 +010072 /* mark to be re-send */
Daniel Willmann146514e2013-12-28 18:24:42 +010073 m_v_b.mark_resend(bsn);
Holger Hans Peter Freyther8b16ae32013-11-24 00:38:54 +010074 resend += 1;
75 }
76 }
77
78 return resend;
79}
Holger Hans Peter Freytherdf6b4f52013-11-24 17:05:48 +010080
Daniel Willmann146514e2013-12-28 18:24:42 +010081int gprs_rlc_dl_window::count_unacked()
Holger Hans Peter Freyther9c5539d2013-11-24 17:34:17 +010082{
83 uint16_t unacked = 0;
84 uint16_t bsn;
85
Daniel Willmann146514e2013-12-28 18:24:42 +010086 for (bsn = v_a(); bsn != v_s(); bsn = (bsn + 1) & mod_sns()) {
87 if (!m_v_b.is_acked(bsn))
Holger Hans Peter Freyther9c5539d2013-11-24 17:34:17 +010088 unacked += 1;
89 }
90
91 return unacked;
92}
93
Daniel Willmanncc5a4cb2013-12-11 20:04:29 +010094static uint16_t bitnum_to_bsn(int bitnum, uint16_t ssn, uint16_t mod_sns)
95{
96 return (ssn - 1 - bitnum) & mod_sns;
97}
98
Daniel Willmann146514e2013-12-28 18:24:42 +010099void gprs_rlc_dl_window::update(BTS *bts, char *show_rbb, uint8_t ssn,
Holger Hans Peter Freytherdf6b4f52013-11-24 17:05:48 +0100100 uint16_t *lost, uint16_t *received)
101{
Holger Hans Peter Freytherdf6b4f52013-11-24 17:05:48 +0100102 /* SSN - 1 is in range V(A)..V(S)-1 */
Daniel Willmann146514e2013-12-28 18:24:42 +0100103 for (int bitpos = 0; bitpos < ws(); bitpos++) {
104 uint16_t bsn = bitnum_to_bsn(bitpos, ssn, mod_sns());
Holger Hans Peter Freytherdf6b4f52013-11-24 17:05:48 +0100105
Daniel Willmann146514e2013-12-28 18:24:42 +0100106 if (bsn == ((v_a() - 1) & mod_sns()))
Daniel Willmanncc5a4cb2013-12-11 20:04:29 +0100107 break;
108
Daniel Willmann146514e2013-12-28 18:24:42 +0100109 if (show_rbb[ws() - 1 - bitpos] == 'R') {
Holger Hans Peter Freytherdf6b4f52013-11-24 17:05:48 +0100110 LOGP(DRLCMACDL, LOGL_DEBUG, "- got ack for BSN=%d\n", bsn);
Daniel Willmann146514e2013-12-28 18:24:42 +0100111 if (!m_v_b.is_acked(bsn))
Holger Hans Peter Freytherdf6b4f52013-11-24 17:05:48 +0100112 *received += 1;
Daniel Willmann146514e2013-12-28 18:24:42 +0100113 m_v_b.mark_acked(bsn);
Holger Hans Peter Freytherdf6b4f52013-11-24 17:05:48 +0100114 } else {
115 LOGP(DRLCMACDL, LOGL_DEBUG, "- got NACK for BSN=%d\n", bsn);
Daniel Willmann146514e2013-12-28 18:24:42 +0100116 m_v_b.mark_nacked(bsn);
Holger Hans Peter Freytherdf6b4f52013-11-24 17:05:48 +0100117 bts->rlc_nacked();
118 *lost += 1;
119 }
120 }
121}
Holger Hans Peter Freythere358ff82013-11-24 17:13:10 +0100122
Daniel Willmann146514e2013-12-28 18:24:42 +0100123int gprs_rlc_dl_window::move_window()
Holger Hans Peter Freythere358ff82013-11-24 17:13:10 +0100124{
125 int i;
126 uint16_t bsn;
127 int moved = 0;
128
Daniel Willmann146514e2013-12-28 18:24:42 +0100129 for (i = 0, bsn = v_a(); bsn != v_s(); i++, bsn = (bsn + 1) & mod_sns()) {
130 if (m_v_b.is_acked(bsn)) {
131 m_v_b.mark_invalid(bsn);
Holger Hans Peter Freythere358ff82013-11-24 17:13:10 +0100132 moved += 1;
133 } else
134 break;
135 }
136
137 return moved;
138}
Holger Hans Peter Freytherbc155702013-11-24 17:20:35 +0100139
Daniel Willmann3ce011f2014-01-15 12:45:56 +0100140void gprs_rlc_dl_window::show_state(char *show_v_b)
Holger Hans Peter Freytherbc155702013-11-24 17:20:35 +0100141{
142 int i;
143 uint16_t bsn;
144
Daniel Willmann146514e2013-12-28 18:24:42 +0100145 for (i = 0, bsn = v_a(); bsn != v_s(); i++, bsn = (bsn + 1) & mod_sns()) {
Holger Hans Peter Freyther3cbf9e02013-11-26 21:43:58 +0100146 uint16_t index = bsn & mod_sns_half();
Daniel Willmannd54d9f52013-12-28 21:16:13 +0100147 switch(m_v_b.get_state(index)) {
148 case GPRS_RLC_DL_BSN_INVALID:
149 show_v_b[i] = 'I';
150 break;
151 case GPRS_RLC_DL_BSN_ACKED:
152 show_v_b[i] = 'A';
153 break;
154 case GPRS_RLC_DL_BSN_RESEND:
155 show_v_b[i] = 'X';
156 break;
157 case GPRS_RLC_DL_BSN_NACKED:
158 show_v_b[i] = 'N';
159 break;
Daniel Willmann3ce011f2014-01-15 12:45:56 +0100160 default:
161 show_v_b[i] = '?';
Daniel Willmannd54d9f52013-12-28 21:16:13 +0100162 }
Holger Hans Peter Freytherbc155702013-11-24 17:20:35 +0100163 }
164 show_v_b[i] = '\0';
165}
Holger Hans Peter Freythere9b1ebb2013-11-24 22:00:43 +0100166
167void gprs_rlc_v_n::reset()
168{
Daniel Willmannd54d9f52013-12-28 21:16:13 +0100169 for (size_t i = 0; i < ARRAY_SIZE(m_v_n); ++i)
170 m_v_n[i] = GPRS_RLC_UL_BSN_INVALID;
Holger Hans Peter Freythere9b1ebb2013-11-24 22:00:43 +0100171}
Holger Hans Peter Freythercbb00eb2013-11-25 23:26:06 +0100172
Daniel Willmann8a31f9e2013-11-27 17:08:35 +0100173/* Update the receive block bitmap */
Daniel Willmann7c3751b2013-12-28 13:59:24 +0100174void gprs_rlc_ul_window::update_rbb(char *rbb)
Daniel Willmann8a31f9e2013-11-27 17:08:35 +0100175{
176 int i;
177 for (i=0; i < ws(); i++) {
Daniel Willmann7c3751b2013-12-28 13:59:24 +0100178 if (m_v_n.is_received(ssn()-1-i))
Daniel Willmann8a31f9e2013-11-27 17:08:35 +0100179 rbb[ws()-1-i] = 'R';
180 else
181 rbb[ws()-1-i] = 'I';
182 }
183}
184
Holger Hans Peter Freythercbb00eb2013-11-25 23:26:06 +0100185/* Raise V(R) to highest received sequence number not received. */
Daniel Willmann7c3751b2013-12-28 13:59:24 +0100186void gprs_rlc_ul_window::raise_v_r(const uint16_t bsn)
Holger Hans Peter Freythercbb00eb2013-11-25 23:26:06 +0100187{
188 uint16_t offset_v_r;
189 offset_v_r = (bsn + 1 - v_r()) & mod_sns();
190 /* Positive offset, so raise. */
191 if (offset_v_r < (sns() >> 1)) {
192 while (offset_v_r--) {
193 if (offset_v_r) /* all except the received block */
Daniel Willmann7c3751b2013-12-28 13:59:24 +0100194 m_v_n.mark_missing(v_r());
Daniel Willmannf4a1ec62013-12-28 13:57:31 +0100195 raise_v_r_to(1);
Holger Hans Peter Freythercbb00eb2013-11-25 23:26:06 +0100196 }
197 LOGP(DRLCMACUL, LOGL_DEBUG, "- Raising V(R) to %d\n", v_r());
198 }
199}
Holger Hans Peter Freyther7f3e6622013-11-25 23:51:19 +0100200
201/*
202 * Raise V(Q) if possible. This is looped until there is a gap
203 * (non received block) or the window is empty.
204 */
Daniel Willmann7c3751b2013-12-28 13:59:24 +0100205uint16_t gprs_rlc_ul_window::raise_v_q()
Holger Hans Peter Freyther7f3e6622013-11-25 23:51:19 +0100206{
207 uint16_t count = 0;
208
209 while (v_q() != v_r()) {
Daniel Willmann7c3751b2013-12-28 13:59:24 +0100210 if (!m_v_n.is_received(v_q()))
Holger Hans Peter Freyther7f3e6622013-11-25 23:51:19 +0100211 break;
212 LOGP(DRLCMACUL, LOGL_DEBUG, "- Taking block %d out, raising "
213 "V(Q) to %d\n", v_q(), (v_q() + 1) & mod_sns());
214 raise_v_q(1);
215 count += 1;
216 }
217
218 return count;
219}
Daniel Willmann55844792013-12-28 14:41:00 +0100220
221uint16_t gprs_rlc_ul_window::receive_bsn(const uint16_t bsn)
222{
223 m_v_n.mark_received(bsn);
224 raise_v_r(bsn);
225
226 return raise_v_q();
227}