Holger Hans Peter Freyther | 58db60c | 2013-11-13 20:45:40 +0100 | [diff] [blame] | 1 | /* |
| 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 Freyther | df6b4f5 | 2013-11-24 17:05:48 +0100 | [diff] [blame] | 20 | #include "bts.h" |
| 21 | #include "gprs_debug.h" |
Holger Hans Peter Freyther | 58db60c | 2013-11-13 20:45:40 +0100 | [diff] [blame] | 22 | |
| 23 | extern "C" { |
| 24 | #include <osmocom/core/utils.h> |
| 25 | } |
| 26 | |
| 27 | |
| 28 | uint8_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 Freyther | 88553ab | 2013-11-26 21:01:04 +0100 | [diff] [blame] | 31 | memset(block, 0x0, sizeof(block)); |
Holger Hans Peter Freyther | 58db60c | 2013-11-13 20:45:40 +0100 | [diff] [blame] | 32 | memset(block, 0x2b, block_data_len); |
| 33 | |
| 34 | return block; |
| 35 | } |
Holger Hans Peter Freyther | 321f3c3 | 2013-11-23 16:06:54 +0100 | [diff] [blame] | 36 | |
Holger Hans Peter Freyther | 423dd22 | 2013-11-25 23:24:29 +0100 | [diff] [blame] | 37 | void 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 Freyther | 321f3c3 | 2013-11-23 16:06:54 +0100 | [diff] [blame] | 43 | void 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 Freyther | 1577779 | 2013-11-24 00:18:47 +0100 | [diff] [blame] | 48 | |
Daniel Willmann | 7e994e3 | 2014-08-07 15:49:21 +0200 | [diff] [blame] | 49 | void gprs_rlc_dl_window::reset() |
| 50 | { |
| 51 | m_v_s = 0; |
| 52 | m_v_a = 0; |
| 53 | m_v_b.reset(); |
| 54 | } |
| 55 | |
Daniel Willmann | 146514e | 2013-12-28 18:24:42 +0100 | [diff] [blame] | 56 | int gprs_rlc_dl_window::resend_needed() |
Holger Hans Peter Freyther | 1577779 | 2013-11-24 00:18:47 +0100 | [diff] [blame] | 57 | { |
Daniel Willmann | 146514e | 2013-12-28 18:24:42 +0100 | [diff] [blame] | 58 | 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 Freyther | 1577779 | 2013-11-24 00:18:47 +0100 | [diff] [blame] | 60 | return bsn; |
| 61 | } |
| 62 | |
| 63 | return -1; |
| 64 | } |
Holger Hans Peter Freyther | 8b16ae3 | 2013-11-24 00:38:54 +0100 | [diff] [blame] | 65 | |
Daniel Willmann | 146514e | 2013-12-28 18:24:42 +0100 | [diff] [blame] | 66 | int gprs_rlc_dl_window::mark_for_resend() |
Holger Hans Peter Freyther | 8b16ae3 | 2013-11-24 00:38:54 +0100 | [diff] [blame] | 67 | { |
| 68 | int resend = 0; |
| 69 | |
Daniel Willmann | 146514e | 2013-12-28 18:24:42 +0100 | [diff] [blame] | 70 | 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 Freyther | 8b16ae3 | 2013-11-24 00:38:54 +0100 | [diff] [blame] | 72 | /* mark to be re-send */ |
Daniel Willmann | 146514e | 2013-12-28 18:24:42 +0100 | [diff] [blame] | 73 | m_v_b.mark_resend(bsn); |
Holger Hans Peter Freyther | 8b16ae3 | 2013-11-24 00:38:54 +0100 | [diff] [blame] | 74 | resend += 1; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | return resend; |
| 79 | } |
Holger Hans Peter Freyther | df6b4f5 | 2013-11-24 17:05:48 +0100 | [diff] [blame] | 80 | |
Daniel Willmann | 146514e | 2013-12-28 18:24:42 +0100 | [diff] [blame] | 81 | int gprs_rlc_dl_window::count_unacked() |
Holger Hans Peter Freyther | 9c5539d | 2013-11-24 17:34:17 +0100 | [diff] [blame] | 82 | { |
| 83 | uint16_t unacked = 0; |
| 84 | uint16_t bsn; |
| 85 | |
Daniel Willmann | 146514e | 2013-12-28 18:24:42 +0100 | [diff] [blame] | 86 | for (bsn = v_a(); bsn != v_s(); bsn = (bsn + 1) & mod_sns()) { |
| 87 | if (!m_v_b.is_acked(bsn)) |
Holger Hans Peter Freyther | 9c5539d | 2013-11-24 17:34:17 +0100 | [diff] [blame] | 88 | unacked += 1; |
| 89 | } |
| 90 | |
| 91 | return unacked; |
| 92 | } |
| 93 | |
Daniel Willmann | cc5a4cb | 2013-12-11 20:04:29 +0100 | [diff] [blame] | 94 | static 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 Willmann | 146514e | 2013-12-28 18:24:42 +0100 | [diff] [blame] | 99 | void gprs_rlc_dl_window::update(BTS *bts, char *show_rbb, uint8_t ssn, |
Holger Hans Peter Freyther | df6b4f5 | 2013-11-24 17:05:48 +0100 | [diff] [blame] | 100 | uint16_t *lost, uint16_t *received) |
| 101 | { |
Holger Hans Peter Freyther | df6b4f5 | 2013-11-24 17:05:48 +0100 | [diff] [blame] | 102 | /* SSN - 1 is in range V(A)..V(S)-1 */ |
Daniel Willmann | 146514e | 2013-12-28 18:24:42 +0100 | [diff] [blame] | 103 | for (int bitpos = 0; bitpos < ws(); bitpos++) { |
| 104 | uint16_t bsn = bitnum_to_bsn(bitpos, ssn, mod_sns()); |
Holger Hans Peter Freyther | df6b4f5 | 2013-11-24 17:05:48 +0100 | [diff] [blame] | 105 | |
Daniel Willmann | 146514e | 2013-12-28 18:24:42 +0100 | [diff] [blame] | 106 | if (bsn == ((v_a() - 1) & mod_sns())) |
Daniel Willmann | cc5a4cb | 2013-12-11 20:04:29 +0100 | [diff] [blame] | 107 | break; |
| 108 | |
Daniel Willmann | 146514e | 2013-12-28 18:24:42 +0100 | [diff] [blame] | 109 | if (show_rbb[ws() - 1 - bitpos] == 'R') { |
Holger Hans Peter Freyther | df6b4f5 | 2013-11-24 17:05:48 +0100 | [diff] [blame] | 110 | LOGP(DRLCMACDL, LOGL_DEBUG, "- got ack for BSN=%d\n", bsn); |
Daniel Willmann | 146514e | 2013-12-28 18:24:42 +0100 | [diff] [blame] | 111 | if (!m_v_b.is_acked(bsn)) |
Holger Hans Peter Freyther | df6b4f5 | 2013-11-24 17:05:48 +0100 | [diff] [blame] | 112 | *received += 1; |
Daniel Willmann | 146514e | 2013-12-28 18:24:42 +0100 | [diff] [blame] | 113 | m_v_b.mark_acked(bsn); |
Holger Hans Peter Freyther | df6b4f5 | 2013-11-24 17:05:48 +0100 | [diff] [blame] | 114 | } else { |
| 115 | LOGP(DRLCMACDL, LOGL_DEBUG, "- got NACK for BSN=%d\n", bsn); |
Daniel Willmann | 146514e | 2013-12-28 18:24:42 +0100 | [diff] [blame] | 116 | m_v_b.mark_nacked(bsn); |
Holger Hans Peter Freyther | df6b4f5 | 2013-11-24 17:05:48 +0100 | [diff] [blame] | 117 | bts->rlc_nacked(); |
| 118 | *lost += 1; |
| 119 | } |
| 120 | } |
| 121 | } |
Holger Hans Peter Freyther | e358ff8 | 2013-11-24 17:13:10 +0100 | [diff] [blame] | 122 | |
Daniel Willmann | 146514e | 2013-12-28 18:24:42 +0100 | [diff] [blame] | 123 | int gprs_rlc_dl_window::move_window() |
Holger Hans Peter Freyther | e358ff8 | 2013-11-24 17:13:10 +0100 | [diff] [blame] | 124 | { |
| 125 | int i; |
| 126 | uint16_t bsn; |
| 127 | int moved = 0; |
| 128 | |
Daniel Willmann | 146514e | 2013-12-28 18:24:42 +0100 | [diff] [blame] | 129 | 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 Freyther | e358ff8 | 2013-11-24 17:13:10 +0100 | [diff] [blame] | 132 | moved += 1; |
| 133 | } else |
| 134 | break; |
| 135 | } |
| 136 | |
| 137 | return moved; |
| 138 | } |
Holger Hans Peter Freyther | bc15570 | 2013-11-24 17:20:35 +0100 | [diff] [blame] | 139 | |
Daniel Willmann | 3ce011f | 2014-01-15 12:45:56 +0100 | [diff] [blame] | 140 | void gprs_rlc_dl_window::show_state(char *show_v_b) |
Holger Hans Peter Freyther | bc15570 | 2013-11-24 17:20:35 +0100 | [diff] [blame] | 141 | { |
| 142 | int i; |
| 143 | uint16_t bsn; |
| 144 | |
Daniel Willmann | 146514e | 2013-12-28 18:24:42 +0100 | [diff] [blame] | 145 | for (i = 0, bsn = v_a(); bsn != v_s(); i++, bsn = (bsn + 1) & mod_sns()) { |
Holger Hans Peter Freyther | 3cbf9e0 | 2013-11-26 21:43:58 +0100 | [diff] [blame] | 146 | uint16_t index = bsn & mod_sns_half(); |
Daniel Willmann | d54d9f5 | 2013-12-28 21:16:13 +0100 | [diff] [blame] | 147 | 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 Willmann | 3ce011f | 2014-01-15 12:45:56 +0100 | [diff] [blame] | 160 | default: |
| 161 | show_v_b[i] = '?'; |
Daniel Willmann | d54d9f5 | 2013-12-28 21:16:13 +0100 | [diff] [blame] | 162 | } |
Holger Hans Peter Freyther | bc15570 | 2013-11-24 17:20:35 +0100 | [diff] [blame] | 163 | } |
| 164 | show_v_b[i] = '\0'; |
| 165 | } |
Holger Hans Peter Freyther | e9b1ebb | 2013-11-24 22:00:43 +0100 | [diff] [blame] | 166 | |
| 167 | void gprs_rlc_v_n::reset() |
| 168 | { |
Daniel Willmann | d54d9f5 | 2013-12-28 21:16:13 +0100 | [diff] [blame] | 169 | 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 Freyther | e9b1ebb | 2013-11-24 22:00:43 +0100 | [diff] [blame] | 171 | } |
Holger Hans Peter Freyther | cbb00eb | 2013-11-25 23:26:06 +0100 | [diff] [blame] | 172 | |
Daniel Willmann | 8a31f9e | 2013-11-27 17:08:35 +0100 | [diff] [blame] | 173 | /* Update the receive block bitmap */ |
Daniel Willmann | 7c3751b | 2013-12-28 13:59:24 +0100 | [diff] [blame] | 174 | void gprs_rlc_ul_window::update_rbb(char *rbb) |
Daniel Willmann | 8a31f9e | 2013-11-27 17:08:35 +0100 | [diff] [blame] | 175 | { |
| 176 | int i; |
| 177 | for (i=0; i < ws(); i++) { |
Daniel Willmann | 7c3751b | 2013-12-28 13:59:24 +0100 | [diff] [blame] | 178 | if (m_v_n.is_received(ssn()-1-i)) |
Daniel Willmann | 8a31f9e | 2013-11-27 17:08:35 +0100 | [diff] [blame] | 179 | rbb[ws()-1-i] = 'R'; |
| 180 | else |
| 181 | rbb[ws()-1-i] = 'I'; |
| 182 | } |
| 183 | } |
| 184 | |
Holger Hans Peter Freyther | cbb00eb | 2013-11-25 23:26:06 +0100 | [diff] [blame] | 185 | /* Raise V(R) to highest received sequence number not received. */ |
Daniel Willmann | 7c3751b | 2013-12-28 13:59:24 +0100 | [diff] [blame] | 186 | void gprs_rlc_ul_window::raise_v_r(const uint16_t bsn) |
Holger Hans Peter Freyther | cbb00eb | 2013-11-25 23:26:06 +0100 | [diff] [blame] | 187 | { |
| 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 Willmann | 7c3751b | 2013-12-28 13:59:24 +0100 | [diff] [blame] | 194 | m_v_n.mark_missing(v_r()); |
Daniel Willmann | f4a1ec6 | 2013-12-28 13:57:31 +0100 | [diff] [blame] | 195 | raise_v_r_to(1); |
Holger Hans Peter Freyther | cbb00eb | 2013-11-25 23:26:06 +0100 | [diff] [blame] | 196 | } |
| 197 | LOGP(DRLCMACUL, LOGL_DEBUG, "- Raising V(R) to %d\n", v_r()); |
| 198 | } |
| 199 | } |
Holger Hans Peter Freyther | 7f3e662 | 2013-11-25 23:51:19 +0100 | [diff] [blame] | 200 | |
| 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 Willmann | 7c3751b | 2013-12-28 13:59:24 +0100 | [diff] [blame] | 205 | uint16_t gprs_rlc_ul_window::raise_v_q() |
Holger Hans Peter Freyther | 7f3e662 | 2013-11-25 23:51:19 +0100 | [diff] [blame] | 206 | { |
| 207 | uint16_t count = 0; |
| 208 | |
| 209 | while (v_q() != v_r()) { |
Daniel Willmann | 7c3751b | 2013-12-28 13:59:24 +0100 | [diff] [blame] | 210 | if (!m_v_n.is_received(v_q())) |
Holger Hans Peter Freyther | 7f3e662 | 2013-11-25 23:51:19 +0100 | [diff] [blame] | 211 | 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 Willmann | 5584479 | 2013-12-28 14:41:00 +0100 | [diff] [blame] | 220 | |
| 221 | uint16_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 | } |