Holger Hans Peter Freyther | b809866 | 2013-10-30 14:50:17 +0100 | [diff] [blame] | 1 | /* |
| 2 | * TbfTest.cpp |
| 3 | * |
| 4 | * Copyright (C) 2013 by Holger Hans Peter Freyther |
| 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU Affero General Public License as published by |
| 10 | * the Free Software Foundation; either version 3 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Affero General Public License |
| 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include "bts.h" |
| 24 | #include "tbf.h" |
| 25 | #include "gprs_debug.h" |
Jacob Erlbeck | 2cbe80b | 2015-03-25 10:48:52 +0100 | [diff] [blame] | 26 | #include "pcu_utils.h" |
Holger Hans Peter Freyther | b809866 | 2013-10-30 14:50:17 +0100 | [diff] [blame] | 27 | |
| 28 | extern "C" { |
| 29 | #include <osmocom/core/application.h> |
| 30 | #include <osmocom/core/msgb.h> |
| 31 | #include <osmocom/core/talloc.h> |
| 32 | #include <osmocom/core/utils.h> |
| 33 | } |
| 34 | |
| 35 | void *tall_pcu_ctx; |
| 36 | int16_t spoof_mnc = 0, spoof_mcc = 0; |
| 37 | |
Jacob Erlbeck | 08fe76a | 2015-02-23 15:10:20 +0100 | [diff] [blame] | 38 | static void check_tbf(gprs_rlcmac_tbf *tbf) |
| 39 | { |
| 40 | OSMO_ASSERT(tbf); |
| 41 | OSMO_ASSERT(tbf->m_new_tbf == NULL || tbf->m_new_tbf->m_old_tbf == tbf); |
| 42 | OSMO_ASSERT(tbf->m_old_tbf == NULL || tbf->m_old_tbf->m_new_tbf == tbf); |
| 43 | } |
| 44 | |
Holger Hans Peter Freyther | b809866 | 2013-10-30 14:50:17 +0100 | [diff] [blame] | 45 | static void test_tbf_tlli_update() |
| 46 | { |
| 47 | BTS the_bts; |
| 48 | the_bts.bts_data()->alloc_algorithm = alloc_algorithm_a; |
| 49 | the_bts.bts_data()->trx[0].pdch[2].enable(); |
| 50 | the_bts.bts_data()->trx[0].pdch[3].enable(); |
| 51 | |
| 52 | /* |
| 53 | * Make a uplink and downlink allocation |
| 54 | */ |
Daniel Willmann | 48aa0b0 | 2014-07-16 18:54:10 +0200 | [diff] [blame] | 55 | gprs_rlcmac_tbf *dl_tbf = tbf_alloc_dl_tbf(the_bts.bts_data(), |
| 56 | NULL, 0, |
Holger Hans Peter Freyther | b809866 | 2013-10-30 14:50:17 +0100 | [diff] [blame] | 57 | 0, 0, 0); |
| 58 | dl_tbf->update_tlli(0x2342); |
| 59 | dl_tbf->tlli_mark_valid(); |
| 60 | dl_tbf->ta = 4; |
| 61 | the_bts.timing_advance()->remember(0x2342, dl_tbf->ta); |
| 62 | |
Daniel Willmann | 48aa0b0 | 2014-07-16 18:54:10 +0200 | [diff] [blame] | 63 | gprs_rlcmac_tbf *ul_tbf = tbf_alloc_ul_tbf(the_bts.bts_data(), |
| 64 | ul_tbf, 0, |
Holger Hans Peter Freyther | b809866 | 2013-10-30 14:50:17 +0100 | [diff] [blame] | 65 | 0, 0, 0); |
| 66 | ul_tbf->update_tlli(0x2342); |
| 67 | ul_tbf->tlli_mark_valid(); |
| 68 | |
| 69 | |
Daniel Willmann | b59d61b | 2014-07-02 17:58:14 +0200 | [diff] [blame] | 70 | OSMO_ASSERT(the_bts.dl_tbf_by_tlli(0x2342) == dl_tbf); |
| 71 | OSMO_ASSERT(the_bts.ul_tbf_by_tlli(0x2342) == ul_tbf); |
Holger Hans Peter Freyther | b809866 | 2013-10-30 14:50:17 +0100 | [diff] [blame] | 72 | |
| 73 | |
| 74 | /* |
| 75 | * Now check.. that DL changes and that the timing advance |
| 76 | * has changed. |
| 77 | */ |
| 78 | dl_tbf->update_tlli(0x4232); |
Daniel Willmann | b59d61b | 2014-07-02 17:58:14 +0200 | [diff] [blame] | 79 | OSMO_ASSERT(!the_bts.dl_tbf_by_tlli(0x2342)); |
| 80 | OSMO_ASSERT(!the_bts.ul_tbf_by_tlli(0x2342)); |
Holger Hans Peter Freyther | b809866 | 2013-10-30 14:50:17 +0100 | [diff] [blame] | 81 | |
| 82 | |
Daniel Willmann | b59d61b | 2014-07-02 17:58:14 +0200 | [diff] [blame] | 83 | OSMO_ASSERT(the_bts.dl_tbf_by_tlli(0x4232) == dl_tbf); |
| 84 | OSMO_ASSERT(the_bts.ul_tbf_by_tlli(0x4232) == ul_tbf); |
Holger Hans Peter Freyther | bc1626e | 2013-10-30 19:50:49 +0100 | [diff] [blame] | 85 | |
| 86 | OSMO_ASSERT(the_bts.timing_advance()->recall(0x4232) == 4); |
Holger Hans Peter Freyther | b809866 | 2013-10-30 14:50:17 +0100 | [diff] [blame] | 87 | } |
| 88 | |
Daniel Willmann | 510d7d3 | 2014-08-15 18:19:41 +0200 | [diff] [blame] | 89 | static uint8_t llc_data[200]; |
| 90 | |
| 91 | int pcu_sock_send(struct msgb *msg) |
| 92 | { |
| 93 | return 0; |
| 94 | } |
| 95 | |
Jacob Erlbeck | a3e4509 | 2015-03-25 09:11:24 +0100 | [diff] [blame] | 96 | static void setup_bts(BTS *the_bts, uint8_t ts_no) |
| 97 | { |
| 98 | gprs_rlcmac_bts *bts; |
| 99 | gprs_rlcmac_trx *trx; |
| 100 | |
| 101 | bts = the_bts->bts_data(); |
| 102 | bts->alloc_algorithm = alloc_algorithm_a; |
| 103 | trx = &bts->trx[0]; |
| 104 | |
| 105 | trx->pdch[ts_no].enable(); |
| 106 | } |
| 107 | |
| 108 | static gprs_rlcmac_dl_tbf *create_dl_tbf(BTS *the_bts, uint8_t ms_class, |
| 109 | uint8_t *trx_no_) |
| 110 | { |
| 111 | gprs_rlcmac_bts *bts; |
| 112 | int tfi; |
| 113 | uint8_t trx_no; |
| 114 | |
| 115 | gprs_rlcmac_dl_tbf *dl_tbf; |
| 116 | |
| 117 | bts = the_bts->bts_data(); |
| 118 | |
| 119 | tfi = the_bts->tfi_find_free(GPRS_RLCMAC_DL_TBF, &trx_no, -1); |
| 120 | OSMO_ASSERT(tfi >= 0); |
| 121 | dl_tbf = tbf_alloc_dl_tbf(bts, NULL, tfi, trx_no, ms_class, 1); |
| 122 | check_tbf(dl_tbf); |
| 123 | |
| 124 | /* "Establish" the DL TBF */ |
| 125 | dl_tbf->dl_ass_state = GPRS_RLCMAC_DL_ASS_SEND_ASS; |
| 126 | dl_tbf->set_state(GPRS_RLCMAC_FLOW); |
| 127 | dl_tbf->m_wait_confirm = 0; |
| 128 | dl_tbf->set_new_tbf(dl_tbf); |
| 129 | check_tbf(dl_tbf); |
| 130 | |
| 131 | *trx_no_ = trx_no; |
| 132 | |
| 133 | return dl_tbf; |
| 134 | } |
| 135 | |
Jacob Erlbeck | 2493c66 | 2015-03-25 10:05:34 +0100 | [diff] [blame] | 136 | static void send_rlc_block(struct gprs_rlcmac_bts *bts, |
| 137 | uint8_t trx_no, uint8_t ts_no, uint16_t arfcn, |
| 138 | uint32_t *fn, uint8_t *block_nr) |
| 139 | { |
| 140 | gprs_rlcmac_rcv_rts_block(bts, trx_no, ts_no, 0, *fn, *block_nr); |
| 141 | *fn += 4; |
| 142 | if ((*fn % 13) == 12) |
| 143 | *fn += 1; |
| 144 | *block_nr += 1; |
| 145 | } |
| 146 | |
Jacob Erlbeck | 5e9f40d | 2015-02-23 14:26:59 +0100 | [diff] [blame] | 147 | enum test_tbf_final_ack_mode { |
| 148 | TEST_MODE_STANDARD, |
| 149 | TEST_MODE_REVERSE_FREE |
| 150 | }; |
| 151 | |
| 152 | static void test_tbf_final_ack(enum test_tbf_final_ack_mode test_mode) |
Daniel Willmann | 510d7d3 | 2014-08-15 18:19:41 +0200 | [diff] [blame] | 153 | { |
| 154 | BTS the_bts; |
| 155 | gprs_rlcmac_bts *bts; |
Jacob Erlbeck | a3e4509 | 2015-03-25 09:11:24 +0100 | [diff] [blame] | 156 | uint8_t ts_no = 4; |
| 157 | unsigned i; |
Daniel Willmann | 510d7d3 | 2014-08-15 18:19:41 +0200 | [diff] [blame] | 158 | uint8_t ms_class = 45; |
| 159 | uint32_t fn; |
Jacob Erlbeck | 2493c66 | 2015-03-25 10:05:34 +0100 | [diff] [blame] | 160 | uint8_t block_nr; |
Jacob Erlbeck | a3e4509 | 2015-03-25 09:11:24 +0100 | [diff] [blame] | 161 | uint8_t trx_no; |
Daniel Willmann | 510d7d3 | 2014-08-15 18:19:41 +0200 | [diff] [blame] | 162 | |
| 163 | uint8_t rbb[64/8]; |
| 164 | |
Daniel Willmann | 510d7d3 | 2014-08-15 18:19:41 +0200 | [diff] [blame] | 165 | gprs_rlcmac_dl_tbf *dl_tbf; |
| 166 | gprs_rlcmac_tbf *new_tbf; |
| 167 | |
| 168 | bts = the_bts.bts_data(); |
Daniel Willmann | 510d7d3 | 2014-08-15 18:19:41 +0200 | [diff] [blame] | 169 | |
Jacob Erlbeck | a3e4509 | 2015-03-25 09:11:24 +0100 | [diff] [blame] | 170 | setup_bts(&the_bts, ts_no); |
| 171 | dl_tbf = create_dl_tbf(&the_bts, ms_class, &trx_no); |
Daniel Willmann | 510d7d3 | 2014-08-15 18:19:41 +0200 | [diff] [blame] | 172 | |
| 173 | for (i = 0; i < sizeof(llc_data); i++) |
| 174 | llc_data[i] = i%256; |
| 175 | |
Jacob Erlbeck | 2493c66 | 2015-03-25 10:05:34 +0100 | [diff] [blame] | 176 | /* Schedule two LLC frames */ |
Daniel Willmann | 0f58af6 | 2014-09-19 11:57:21 +0200 | [diff] [blame] | 177 | dl_tbf->append_data(ms_class, 1000, llc_data, sizeof(llc_data)); |
| 178 | dl_tbf->append_data(ms_class, 1000, llc_data, sizeof(llc_data)); |
Daniel Willmann | 510d7d3 | 2014-08-15 18:19:41 +0200 | [diff] [blame] | 179 | |
| 180 | |
Jacob Erlbeck | 2493c66 | 2015-03-25 10:05:34 +0100 | [diff] [blame] | 181 | /* Send only a few RLC/MAC blocks */ |
Daniel Willmann | 510d7d3 | 2014-08-15 18:19:41 +0200 | [diff] [blame] | 182 | fn = 0; |
Jacob Erlbeck | 2493c66 | 2015-03-25 10:05:34 +0100 | [diff] [blame] | 183 | block_nr = 0; |
| 184 | while (block_nr < 3) { |
Daniel Willmann | 510d7d3 | 2014-08-15 18:19:41 +0200 | [diff] [blame] | 185 | /* Request to send one block */ |
Jacob Erlbeck | 2493c66 | 2015-03-25 10:05:34 +0100 | [diff] [blame] | 186 | send_rlc_block(bts, trx_no, ts_no, 0, &fn, &block_nr); |
Daniel Willmann | 510d7d3 | 2014-08-15 18:19:41 +0200 | [diff] [blame] | 187 | } |
Jacob Erlbeck | 2493c66 | 2015-03-25 10:05:34 +0100 | [diff] [blame] | 188 | OSMO_ASSERT(dl_tbf->have_data()); |
| 189 | OSMO_ASSERT(dl_tbf->state_is(GPRS_RLCMAC_FLOW)); |
Daniel Willmann | 510d7d3 | 2014-08-15 18:19:41 +0200 | [diff] [blame] | 190 | |
| 191 | /* Queue a final ACK */ |
| 192 | memset(rbb, 0, sizeof(rbb)); |
| 193 | /* Receive a final ACK */ |
| 194 | dl_tbf->rcvd_dl_ack(1, 1, rbb); |
| 195 | |
| 196 | /* Clean up and ensure tbfs are in the correct state */ |
| 197 | OSMO_ASSERT(dl_tbf->state_is(GPRS_RLCMAC_WAIT_RELEASE)); |
| 198 | new_tbf = dl_tbf->new_tbf(); |
Jacob Erlbeck | 08fe76a | 2015-02-23 15:10:20 +0100 | [diff] [blame] | 199 | check_tbf(new_tbf); |
Daniel Willmann | 510d7d3 | 2014-08-15 18:19:41 +0200 | [diff] [blame] | 200 | OSMO_ASSERT(new_tbf != dl_tbf); |
| 201 | OSMO_ASSERT(new_tbf->tfi() == 1); |
Jacob Erlbeck | 08fe76a | 2015-02-23 15:10:20 +0100 | [diff] [blame] | 202 | check_tbf(dl_tbf); |
Daniel Willmann | 510d7d3 | 2014-08-15 18:19:41 +0200 | [diff] [blame] | 203 | dl_tbf->dl_ass_state = GPRS_RLCMAC_DL_ASS_NONE; |
Jacob Erlbeck | 5e9f40d | 2015-02-23 14:26:59 +0100 | [diff] [blame] | 204 | if (test_mode == TEST_MODE_REVERSE_FREE) { |
| 205 | tbf_free(new_tbf); |
Jacob Erlbeck | 08fe76a | 2015-02-23 15:10:20 +0100 | [diff] [blame] | 206 | OSMO_ASSERT(dl_tbf->m_new_tbf != new_tbf); |
| 207 | check_tbf(dl_tbf); |
Jacob Erlbeck | 5e9f40d | 2015-02-23 14:26:59 +0100 | [diff] [blame] | 208 | tbf_free(dl_tbf); |
| 209 | } else { |
| 210 | tbf_free(dl_tbf); |
| 211 | OSMO_ASSERT(new_tbf->m_new_tbf != dl_tbf); |
Jacob Erlbeck | 08fe76a | 2015-02-23 15:10:20 +0100 | [diff] [blame] | 212 | check_tbf(new_tbf); |
Jacob Erlbeck | 5e9f40d | 2015-02-23 14:26:59 +0100 | [diff] [blame] | 213 | tbf_free(new_tbf); |
| 214 | } |
Daniel Willmann | 510d7d3 | 2014-08-15 18:19:41 +0200 | [diff] [blame] | 215 | } |
| 216 | |
Jacob Erlbeck | 2cbe80b | 2015-03-25 10:48:52 +0100 | [diff] [blame] | 217 | static void test_tbf_delayed_release() |
| 218 | { |
| 219 | BTS the_bts; |
| 220 | gprs_rlcmac_bts *bts; |
| 221 | uint8_t ts_no = 4; |
| 222 | unsigned i; |
| 223 | uint8_t ms_class = 45; |
| 224 | uint32_t fn = 0; |
| 225 | uint8_t block_nr = 0; |
| 226 | uint8_t trx_no; |
| 227 | |
| 228 | uint8_t rbb[64/8]; |
| 229 | |
| 230 | gprs_rlcmac_dl_tbf *dl_tbf; |
| 231 | |
| 232 | printf("=== start %s ===\n", __func__); |
| 233 | |
| 234 | bts = the_bts.bts_data(); |
| 235 | |
| 236 | setup_bts(&the_bts, ts_no); |
| 237 | bts->dl_tbf_idle_msec = 200; |
| 238 | |
| 239 | dl_tbf = create_dl_tbf(&the_bts, ms_class, &trx_no); |
| 240 | |
| 241 | for (i = 0; i < sizeof(llc_data); i++) |
| 242 | llc_data[i] = i%256; |
| 243 | |
| 244 | OSMO_ASSERT(dl_tbf->state_is(GPRS_RLCMAC_FLOW)); |
| 245 | |
| 246 | /* Schedule two LLC frames */ |
| 247 | dl_tbf->append_data(ms_class, 1000, llc_data, sizeof(llc_data)); |
| 248 | dl_tbf->append_data(ms_class, 1000, llc_data, sizeof(llc_data)); |
| 249 | |
| 250 | OSMO_ASSERT(dl_tbf->state_is(GPRS_RLCMAC_FLOW)); |
| 251 | |
| 252 | /* Drain the queue */ |
| 253 | while (dl_tbf->have_data()) |
| 254 | /* Request to send one RLC/MAC block */ |
| 255 | send_rlc_block(bts, trx_no, ts_no, 0, &fn, &block_nr); |
| 256 | |
| 257 | OSMO_ASSERT(dl_tbf->state_is(GPRS_RLCMAC_FLOW)); |
| 258 | |
| 259 | /* ACK all blocks */ |
| 260 | memset(rbb, 0xff, sizeof(rbb)); |
| 261 | /* Receive an ACK */ |
| 262 | dl_tbf->rcvd_dl_ack(0, dl_tbf->m_window.v_s(), rbb); |
| 263 | OSMO_ASSERT(dl_tbf->m_window.window_empty()); |
| 264 | |
| 265 | /* Force sending of a single block containing an LLC dummy command */ |
| 266 | send_rlc_block(bts, trx_no, ts_no, 0, &fn, &block_nr); |
| 267 | |
| 268 | /* Receive an ACK */ |
| 269 | dl_tbf->rcvd_dl_ack(0, dl_tbf->m_window.v_s(), rbb); |
| 270 | OSMO_ASSERT(dl_tbf->m_window.window_empty()); |
| 271 | |
| 272 | /* Timeout (make sure fn % 52 remains valid) */ |
| 273 | fn += 52 * ((msecs_to_frames(bts->dl_tbf_idle_msec + 100) + 51)/ 52); |
| 274 | send_rlc_block(bts, trx_no, ts_no, 0, &fn, &block_nr); |
| 275 | |
| 276 | OSMO_ASSERT(dl_tbf->state_is(GPRS_RLCMAC_FINISHED)); |
| 277 | |
| 278 | /* Receive a final ACK */ |
| 279 | dl_tbf->rcvd_dl_ack(1, dl_tbf->m_window.v_s(), rbb); |
| 280 | |
| 281 | /* Clean up and ensure tbfs are in the correct state */ |
| 282 | OSMO_ASSERT(dl_tbf->state_is(GPRS_RLCMAC_WAIT_RELEASE)); |
| 283 | OSMO_ASSERT(dl_tbf->new_tbf() == dl_tbf); |
| 284 | dl_tbf->dl_ass_state = GPRS_RLCMAC_DL_ASS_NONE; |
| 285 | check_tbf(dl_tbf); |
| 286 | tbf_free(dl_tbf); |
| 287 | printf("=== end %s ===\n", __func__); |
| 288 | } |
| 289 | |
Daniel Willmann | 341689d | 2014-06-11 18:33:14 +0200 | [diff] [blame] | 290 | static const struct log_info_cat default_categories[] = { |
| 291 | {"DCSN1", "\033[1;31m", "Concrete Syntax Notation One (CSN1)", LOGL_INFO, 0}, |
| 292 | {"DL1IF", "\033[1;32m", "GPRS PCU L1 interface (L1IF)", LOGL_DEBUG, 1}, |
| 293 | {"DRLCMAC", "\033[0;33m", "GPRS RLC/MAC layer (RLCMAC)", LOGL_DEBUG, 1}, |
| 294 | {"DRLCMACDATA", "\033[0;33m", "GPRS RLC/MAC layer Data (RLCMAC)", LOGL_DEBUG, 1}, |
| 295 | {"DRLCMACDL", "\033[1;33m", "GPRS RLC/MAC layer Downlink (RLCMAC)", LOGL_DEBUG, 1}, |
| 296 | {"DRLCMACUL", "\033[1;36m", "GPRS RLC/MAC layer Uplink (RLCMAC)", LOGL_DEBUG, 1}, |
| 297 | {"DRLCMACSCHED", "\033[0;36m", "GPRS RLC/MAC layer Scheduling (RLCMAC)", LOGL_DEBUG, 1}, |
| 298 | {"DRLCMACMEAS", "\033[1;31m", "GPRS RLC/MAC layer Measurements (RLCMAC)", LOGL_INFO, 1}, |
| 299 | {"DBSSGP","\033[1;34m", "GPRS BSS Gateway Protocol (BSSGP)", LOGL_INFO , 1}, |
| 300 | {"DPCU", "\033[1;35m", "GPRS Packet Control Unit (PCU)", LOGL_NOTICE, 1}, |
| 301 | }; |
| 302 | |
| 303 | static int filter_fn(const struct log_context *ctx, |
| 304 | struct log_target *tar) |
| 305 | { |
| 306 | return 1; |
| 307 | } |
| 308 | |
| 309 | const struct log_info debug_log_info = { |
| 310 | filter_fn, |
| 311 | (struct log_info_cat*)default_categories, |
| 312 | ARRAY_SIZE(default_categories), |
| 313 | }; |
| 314 | |
Holger Hans Peter Freyther | b809866 | 2013-10-30 14:50:17 +0100 | [diff] [blame] | 315 | int main(int argc, char **argv) |
| 316 | { |
| 317 | tall_pcu_ctx = talloc_named_const(NULL, 1, "moiji-mobile TbfTest context"); |
| 318 | if (!tall_pcu_ctx) |
| 319 | abort(); |
| 320 | |
| 321 | msgb_set_talloc_ctx(tall_pcu_ctx); |
Daniel Willmann | 341689d | 2014-06-11 18:33:14 +0200 | [diff] [blame] | 322 | osmo_init_logging(&debug_log_info); |
Holger Hans Peter Freyther | b809866 | 2013-10-30 14:50:17 +0100 | [diff] [blame] | 323 | log_set_use_color(osmo_stderr_target, 0); |
| 324 | log_set_print_filename(osmo_stderr_target, 0); |
| 325 | |
| 326 | test_tbf_tlli_update(); |
Jacob Erlbeck | 5e9f40d | 2015-02-23 14:26:59 +0100 | [diff] [blame] | 327 | test_tbf_final_ack(TEST_MODE_STANDARD); |
| 328 | test_tbf_final_ack(TEST_MODE_REVERSE_FREE); |
Jacob Erlbeck | 2cbe80b | 2015-03-25 10:48:52 +0100 | [diff] [blame] | 329 | test_tbf_delayed_release(); |
Holger Hans Peter Freyther | b809866 | 2013-10-30 14:50:17 +0100 | [diff] [blame] | 330 | return EXIT_SUCCESS; |
| 331 | } |
| 332 | |
| 333 | /* |
| 334 | * stubs that should not be reached |
| 335 | */ |
| 336 | extern "C" { |
| 337 | void l1if_pdch_req() { abort(); } |
| 338 | void l1if_connect_pdch() { abort(); } |
| 339 | void l1if_close_pdch() { abort(); } |
| 340 | void l1if_open_pdch() { abort(); } |
| 341 | } |