blob: 081feef22fa1f3662248a25a92c4e0e122756fdb [file] [log] [blame]
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +01001/*
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 Erlbeck2cbe80b2015-03-25 10:48:52 +010026#include "pcu_utils.h"
Jacob Erlbeckd58b7112015-04-09 19:17:21 +020027#include "gprs_bssgp_pcu.h"
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +010028
29extern "C" {
Jacob Erlbeckd58b7112015-04-09 19:17:21 +020030#include "pcu_vty.h"
31
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +010032#include <osmocom/core/application.h>
33#include <osmocom/core/msgb.h>
34#include <osmocom/core/talloc.h>
35#include <osmocom/core/utils.h>
Jacob Erlbeckd58b7112015-04-09 19:17:21 +020036#include <osmocom/vty/vty.h>
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +010037}
38
Jacob Erlbeckd58b7112015-04-09 19:17:21 +020039#include <errno.h>
40
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +010041void *tall_pcu_ctx;
42int16_t spoof_mnc = 0, spoof_mcc = 0;
43
Jacob Erlbeck08fe76a2015-02-23 15:10:20 +010044static void check_tbf(gprs_rlcmac_tbf *tbf)
45{
46 OSMO_ASSERT(tbf);
47 OSMO_ASSERT(tbf->m_new_tbf == NULL || tbf->m_new_tbf->m_old_tbf == tbf);
48 OSMO_ASSERT(tbf->m_old_tbf == NULL || tbf->m_old_tbf->m_new_tbf == tbf);
49}
50
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +010051static void test_tbf_tlli_update()
52{
53 BTS the_bts;
Jacob Erlbeck93990462015-05-15 15:50:43 +020054 GprsMs *ms, *ms_new;
55
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +010056 the_bts.bts_data()->alloc_algorithm = alloc_algorithm_a;
57 the_bts.bts_data()->trx[0].pdch[2].enable();
58 the_bts.bts_data()->trx[0].pdch[3].enable();
59
60 /*
61 * Make a uplink and downlink allocation
62 */
Daniel Willmann48aa0b02014-07-16 18:54:10 +020063 gprs_rlcmac_tbf *dl_tbf = tbf_alloc_dl_tbf(the_bts.bts_data(),
64 NULL, 0,
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +010065 0, 0, 0);
66 dl_tbf->update_tlli(0x2342);
67 dl_tbf->tlli_mark_valid();
Jacob Erlbeckbe0cbc12015-05-18 14:35:11 +020068 dl_tbf->update_ms(0x2342, GPRS_RLCMAC_DL_TBF);
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +010069 dl_tbf->ta = 4;
70 the_bts.timing_advance()->remember(0x2342, dl_tbf->ta);
71
Daniel Willmann48aa0b02014-07-16 18:54:10 +020072 gprs_rlcmac_tbf *ul_tbf = tbf_alloc_ul_tbf(the_bts.bts_data(),
Jacob Erlbeck801d6fe2015-05-04 09:44:19 +020073 dl_tbf, 0,
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +010074 0, 0, 0);
75 ul_tbf->update_tlli(0x2342);
76 ul_tbf->tlli_mark_valid();
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +010077
Jacob Erlbeck93990462015-05-15 15:50:43 +020078 ms = the_bts.ms_by_tlli(0x2342);
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +010079
Jacob Erlbeck93990462015-05-15 15:50:43 +020080 OSMO_ASSERT(ms != NULL);
81 OSMO_ASSERT(ms->dl_tbf() == dl_tbf);
82 OSMO_ASSERT(ms->ul_tbf() == ul_tbf);
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +010083
84 /*
85 * Now check.. that DL changes and that the timing advance
86 * has changed.
87 */
88 dl_tbf->update_tlli(0x4232);
Jacob Erlbeck93990462015-05-15 15:50:43 +020089 ms->confirm_tlli(0x4232);
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +010090
Jacob Erlbeck93990462015-05-15 15:50:43 +020091 /* It is still there, since the new TLLI has not been used for UL yet */
92 ms_new = the_bts.ms_by_tlli(0x2342);
93 OSMO_ASSERT(ms == ms_new);
94
95 ms_new = the_bts.ms_by_tlli(0x4232);
96 OSMO_ASSERT(ms == ms_new);
97 OSMO_ASSERT(ms->dl_tbf() == dl_tbf);
98 OSMO_ASSERT(ms->ul_tbf() == ul_tbf);
99
100 /* Now use the new TLLI for UL */
101 ms->set_tlli(0x4232);
102 ms_new = the_bts.ms_by_tlli(0x2342);
103 OSMO_ASSERT(ms_new == NULL);
Holger Hans Peter Freytherbc1626e2013-10-30 19:50:49 +0100104
105 OSMO_ASSERT(the_bts.timing_advance()->recall(0x4232) == 4);
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +0100106}
107
Daniel Willmann510d7d32014-08-15 18:19:41 +0200108static uint8_t llc_data[200];
109
110int pcu_sock_send(struct msgb *msg)
111{
112 return 0;
113}
114
Jacob Erlbecka3e45092015-03-25 09:11:24 +0100115static void setup_bts(BTS *the_bts, uint8_t ts_no)
116{
117 gprs_rlcmac_bts *bts;
118 gprs_rlcmac_trx *trx;
119
120 bts = the_bts->bts_data();
121 bts->alloc_algorithm = alloc_algorithm_a;
122 trx = &bts->trx[0];
123
124 trx->pdch[ts_no].enable();
125}
126
127static gprs_rlcmac_dl_tbf *create_dl_tbf(BTS *the_bts, uint8_t ms_class,
128 uint8_t *trx_no_)
129{
130 gprs_rlcmac_bts *bts;
131 int tfi;
132 uint8_t trx_no;
133
134 gprs_rlcmac_dl_tbf *dl_tbf;
135
136 bts = the_bts->bts_data();
137
138 tfi = the_bts->tfi_find_free(GPRS_RLCMAC_DL_TBF, &trx_no, -1);
139 OSMO_ASSERT(tfi >= 0);
140 dl_tbf = tbf_alloc_dl_tbf(bts, NULL, tfi, trx_no, ms_class, 1);
141 check_tbf(dl_tbf);
142
143 /* "Establish" the DL TBF */
144 dl_tbf->dl_ass_state = GPRS_RLCMAC_DL_ASS_SEND_ASS;
145 dl_tbf->set_state(GPRS_RLCMAC_FLOW);
146 dl_tbf->m_wait_confirm = 0;
147 dl_tbf->set_new_tbf(dl_tbf);
148 check_tbf(dl_tbf);
149
150 *trx_no_ = trx_no;
151
152 return dl_tbf;
153}
154
Jacob Erlbeck2493c662015-03-25 10:05:34 +0100155static void send_rlc_block(struct gprs_rlcmac_bts *bts,
156 uint8_t trx_no, uint8_t ts_no, uint16_t arfcn,
157 uint32_t *fn, uint8_t *block_nr)
158{
159 gprs_rlcmac_rcv_rts_block(bts, trx_no, ts_no, 0, *fn, *block_nr);
160 *fn += 4;
161 if ((*fn % 13) == 12)
162 *fn += 1;
163 *block_nr += 1;
164}
165
Jacob Erlbeck5e9f40d2015-02-23 14:26:59 +0100166enum test_tbf_final_ack_mode {
167 TEST_MODE_STANDARD,
168 TEST_MODE_REVERSE_FREE
169};
170
171static void test_tbf_final_ack(enum test_tbf_final_ack_mode test_mode)
Daniel Willmann510d7d32014-08-15 18:19:41 +0200172{
173 BTS the_bts;
174 gprs_rlcmac_bts *bts;
Jacob Erlbecka3e45092015-03-25 09:11:24 +0100175 uint8_t ts_no = 4;
176 unsigned i;
Daniel Willmann510d7d32014-08-15 18:19:41 +0200177 uint8_t ms_class = 45;
178 uint32_t fn;
Jacob Erlbeck2493c662015-03-25 10:05:34 +0100179 uint8_t block_nr;
Jacob Erlbecka3e45092015-03-25 09:11:24 +0100180 uint8_t trx_no;
Daniel Willmann510d7d32014-08-15 18:19:41 +0200181
182 uint8_t rbb[64/8];
183
Daniel Willmann510d7d32014-08-15 18:19:41 +0200184 gprs_rlcmac_dl_tbf *dl_tbf;
185 gprs_rlcmac_tbf *new_tbf;
186
187 bts = the_bts.bts_data();
Daniel Willmann510d7d32014-08-15 18:19:41 +0200188
Jacob Erlbecka3e45092015-03-25 09:11:24 +0100189 setup_bts(&the_bts, ts_no);
190 dl_tbf = create_dl_tbf(&the_bts, ms_class, &trx_no);
Daniel Willmann510d7d32014-08-15 18:19:41 +0200191
192 for (i = 0; i < sizeof(llc_data); i++)
193 llc_data[i] = i%256;
194
Jacob Erlbeck2493c662015-03-25 10:05:34 +0100195 /* Schedule two LLC frames */
Daniel Willmann0f58af62014-09-19 11:57:21 +0200196 dl_tbf->append_data(ms_class, 1000, llc_data, sizeof(llc_data));
197 dl_tbf->append_data(ms_class, 1000, llc_data, sizeof(llc_data));
Daniel Willmann510d7d32014-08-15 18:19:41 +0200198
199
Jacob Erlbeck2493c662015-03-25 10:05:34 +0100200 /* Send only a few RLC/MAC blocks */
Daniel Willmann510d7d32014-08-15 18:19:41 +0200201 fn = 0;
Jacob Erlbeck2493c662015-03-25 10:05:34 +0100202 block_nr = 0;
203 while (block_nr < 3) {
Daniel Willmann510d7d32014-08-15 18:19:41 +0200204 /* Request to send one block */
Jacob Erlbeck2493c662015-03-25 10:05:34 +0100205 send_rlc_block(bts, trx_no, ts_no, 0, &fn, &block_nr);
Daniel Willmann510d7d32014-08-15 18:19:41 +0200206 }
Jacob Erlbeck2493c662015-03-25 10:05:34 +0100207 OSMO_ASSERT(dl_tbf->have_data());
208 OSMO_ASSERT(dl_tbf->state_is(GPRS_RLCMAC_FLOW));
Daniel Willmann510d7d32014-08-15 18:19:41 +0200209
210 /* Queue a final ACK */
211 memset(rbb, 0, sizeof(rbb));
212 /* Receive a final ACK */
213 dl_tbf->rcvd_dl_ack(1, 1, rbb);
214
215 /* Clean up and ensure tbfs are in the correct state */
216 OSMO_ASSERT(dl_tbf->state_is(GPRS_RLCMAC_WAIT_RELEASE));
217 new_tbf = dl_tbf->new_tbf();
Jacob Erlbeck08fe76a2015-02-23 15:10:20 +0100218 check_tbf(new_tbf);
Daniel Willmann510d7d32014-08-15 18:19:41 +0200219 OSMO_ASSERT(new_tbf != dl_tbf);
220 OSMO_ASSERT(new_tbf->tfi() == 1);
Jacob Erlbeck08fe76a2015-02-23 15:10:20 +0100221 check_tbf(dl_tbf);
Daniel Willmann510d7d32014-08-15 18:19:41 +0200222 dl_tbf->dl_ass_state = GPRS_RLCMAC_DL_ASS_NONE;
Jacob Erlbeck5e9f40d2015-02-23 14:26:59 +0100223 if (test_mode == TEST_MODE_REVERSE_FREE) {
224 tbf_free(new_tbf);
Jacob Erlbeck08fe76a2015-02-23 15:10:20 +0100225 OSMO_ASSERT(dl_tbf->m_new_tbf != new_tbf);
226 check_tbf(dl_tbf);
Jacob Erlbeck5e9f40d2015-02-23 14:26:59 +0100227 tbf_free(dl_tbf);
228 } else {
229 tbf_free(dl_tbf);
230 OSMO_ASSERT(new_tbf->m_new_tbf != dl_tbf);
Jacob Erlbeck08fe76a2015-02-23 15:10:20 +0100231 check_tbf(new_tbf);
Jacob Erlbeck5e9f40d2015-02-23 14:26:59 +0100232 tbf_free(new_tbf);
233 }
Daniel Willmann510d7d32014-08-15 18:19:41 +0200234}
235
Jacob Erlbeck2cbe80b2015-03-25 10:48:52 +0100236static void test_tbf_delayed_release()
237{
238 BTS the_bts;
239 gprs_rlcmac_bts *bts;
240 uint8_t ts_no = 4;
241 unsigned i;
242 uint8_t ms_class = 45;
243 uint32_t fn = 0;
244 uint8_t block_nr = 0;
245 uint8_t trx_no;
246
247 uint8_t rbb[64/8];
248
249 gprs_rlcmac_dl_tbf *dl_tbf;
250
251 printf("=== start %s ===\n", __func__);
252
253 bts = the_bts.bts_data();
254
255 setup_bts(&the_bts, ts_no);
256 bts->dl_tbf_idle_msec = 200;
257
258 dl_tbf = create_dl_tbf(&the_bts, ms_class, &trx_no);
259
260 for (i = 0; i < sizeof(llc_data); i++)
261 llc_data[i] = i%256;
262
263 OSMO_ASSERT(dl_tbf->state_is(GPRS_RLCMAC_FLOW));
264
265 /* Schedule two LLC frames */
266 dl_tbf->append_data(ms_class, 1000, llc_data, sizeof(llc_data));
267 dl_tbf->append_data(ms_class, 1000, llc_data, sizeof(llc_data));
268
269 OSMO_ASSERT(dl_tbf->state_is(GPRS_RLCMAC_FLOW));
270
271 /* Drain the queue */
272 while (dl_tbf->have_data())
273 /* Request to send one RLC/MAC block */
274 send_rlc_block(bts, trx_no, ts_no, 0, &fn, &block_nr);
275
276 OSMO_ASSERT(dl_tbf->state_is(GPRS_RLCMAC_FLOW));
277
278 /* ACK all blocks */
279 memset(rbb, 0xff, sizeof(rbb));
280 /* Receive an ACK */
281 dl_tbf->rcvd_dl_ack(0, dl_tbf->m_window.v_s(), rbb);
282 OSMO_ASSERT(dl_tbf->m_window.window_empty());
283
284 /* Force sending of a single block containing an LLC dummy command */
285 send_rlc_block(bts, trx_no, ts_no, 0, &fn, &block_nr);
286
287 /* Receive an ACK */
288 dl_tbf->rcvd_dl_ack(0, dl_tbf->m_window.v_s(), rbb);
289 OSMO_ASSERT(dl_tbf->m_window.window_empty());
290
291 /* Timeout (make sure fn % 52 remains valid) */
292 fn += 52 * ((msecs_to_frames(bts->dl_tbf_idle_msec + 100) + 51)/ 52);
293 send_rlc_block(bts, trx_no, ts_no, 0, &fn, &block_nr);
294
295 OSMO_ASSERT(dl_tbf->state_is(GPRS_RLCMAC_FINISHED));
296
297 /* Receive a final ACK */
298 dl_tbf->rcvd_dl_ack(1, dl_tbf->m_window.v_s(), rbb);
299
300 /* Clean up and ensure tbfs are in the correct state */
301 OSMO_ASSERT(dl_tbf->state_is(GPRS_RLCMAC_WAIT_RELEASE));
302 OSMO_ASSERT(dl_tbf->new_tbf() == dl_tbf);
303 dl_tbf->dl_ass_state = GPRS_RLCMAC_DL_ASS_NONE;
304 check_tbf(dl_tbf);
305 tbf_free(dl_tbf);
306 printf("=== end %s ===\n", __func__);
307}
308
Jacob Erlbeckd58b7112015-04-09 19:17:21 +0200309static void test_tbf_exhaustion()
310{
311 BTS the_bts;
312 gprs_rlcmac_bts *bts;
313 unsigned i;
314 uint8_t ts_no = 4;
315 uint8_t ms_class = 45;
316 int rc = 0;
317
318 uint8_t buf[256] = {0};
319
320 printf("=== start %s ===\n", __func__);
321
322 bts = the_bts.bts_data();
323 setup_bts(&the_bts, ts_no);
324 gprs_bssgp_create_and_connect(bts, 33001, 0, 33001,
325 1234, 1234, 1234, 1, 1, 0, 0, 0);
326
327 for (i = 0; i < 1024; i++) {
328 uint32_t tlli = 0xc0000000 + i;
329 char imsi[16] = {0};
330 unsigned delay_csec = 1000;
331
332 snprintf(imsi, sizeof(imsi)-1, "001001%9d", i);
333
Jacob Erlbeck93990462015-05-15 15:50:43 +0200334 rc = gprs_rlcmac_dl_tbf::handle(bts, tlli, 0, imsi, ms_class,
Jacob Erlbeckd58b7112015-04-09 19:17:21 +0200335 delay_csec, buf, sizeof(buf));
336
337 if (rc < 0)
338 break;
339 }
340
341 OSMO_ASSERT(rc == -EBUSY);
342 printf("=== end %s ===\n", __func__);
343
344 gprs_bssgp_destroy();
345}
346
Daniel Willmann341689d2014-06-11 18:33:14 +0200347static const struct log_info_cat default_categories[] = {
348 {"DCSN1", "\033[1;31m", "Concrete Syntax Notation One (CSN1)", LOGL_INFO, 0},
349 {"DL1IF", "\033[1;32m", "GPRS PCU L1 interface (L1IF)", LOGL_DEBUG, 1},
350 {"DRLCMAC", "\033[0;33m", "GPRS RLC/MAC layer (RLCMAC)", LOGL_DEBUG, 1},
351 {"DRLCMACDATA", "\033[0;33m", "GPRS RLC/MAC layer Data (RLCMAC)", LOGL_DEBUG, 1},
352 {"DRLCMACDL", "\033[1;33m", "GPRS RLC/MAC layer Downlink (RLCMAC)", LOGL_DEBUG, 1},
353 {"DRLCMACUL", "\033[1;36m", "GPRS RLC/MAC layer Uplink (RLCMAC)", LOGL_DEBUG, 1},
354 {"DRLCMACSCHED", "\033[0;36m", "GPRS RLC/MAC layer Scheduling (RLCMAC)", LOGL_DEBUG, 1},
355 {"DRLCMACMEAS", "\033[1;31m", "GPRS RLC/MAC layer Measurements (RLCMAC)", LOGL_INFO, 1},
356 {"DBSSGP","\033[1;34m", "GPRS BSS Gateway Protocol (BSSGP)", LOGL_INFO , 1},
357 {"DPCU", "\033[1;35m", "GPRS Packet Control Unit (PCU)", LOGL_NOTICE, 1},
358};
359
360static int filter_fn(const struct log_context *ctx,
361 struct log_target *tar)
362{
363 return 1;
364}
365
366const struct log_info debug_log_info = {
367 filter_fn,
368 (struct log_info_cat*)default_categories,
369 ARRAY_SIZE(default_categories),
370};
371
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +0100372int main(int argc, char **argv)
373{
Jacob Erlbeckd58b7112015-04-09 19:17:21 +0200374 struct vty_app_info pcu_vty_info = {0};
375
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +0100376 tall_pcu_ctx = talloc_named_const(NULL, 1, "moiji-mobile TbfTest context");
377 if (!tall_pcu_ctx)
378 abort();
379
380 msgb_set_talloc_ctx(tall_pcu_ctx);
Daniel Willmann341689d2014-06-11 18:33:14 +0200381 osmo_init_logging(&debug_log_info);
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +0100382 log_set_use_color(osmo_stderr_target, 0);
383 log_set_print_filename(osmo_stderr_target, 0);
Jacob Erlbeckd58b7112015-04-09 19:17:21 +0200384 bssgp_set_log_ss(DBSSGP);
385
386 vty_init(&pcu_vty_info);
387 pcu_vty_init(&debug_log_info);
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +0100388
389 test_tbf_tlli_update();
Jacob Erlbeck5e9f40d2015-02-23 14:26:59 +0100390 test_tbf_final_ack(TEST_MODE_STANDARD);
391 test_tbf_final_ack(TEST_MODE_REVERSE_FREE);
Jacob Erlbeck2cbe80b2015-03-25 10:48:52 +0100392 test_tbf_delayed_release();
Jacob Erlbeckd58b7112015-04-09 19:17:21 +0200393 test_tbf_exhaustion();
Jacob Erlbeck67c38502015-05-11 10:32:40 +0200394
395 if (getenv("TALLOC_REPORT_FULL"))
396 talloc_report_full(tall_pcu_ctx, stderr);
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +0100397 return EXIT_SUCCESS;
398}
399
400/*
401 * stubs that should not be reached
402 */
403extern "C" {
404void l1if_pdch_req() { abort(); }
405void l1if_connect_pdch() { abort(); }
406void l1if_close_pdch() { abort(); }
407void l1if_open_pdch() { abort(); }
408}