blob: d5bcb674f1c715d03f1a2584d9f6346cf8bed984 [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;
54 the_bts.bts_data()->alloc_algorithm = alloc_algorithm_a;
55 the_bts.bts_data()->trx[0].pdch[2].enable();
56 the_bts.bts_data()->trx[0].pdch[3].enable();
57
58 /*
59 * Make a uplink and downlink allocation
60 */
Daniel Willmann48aa0b02014-07-16 18:54:10 +020061 gprs_rlcmac_tbf *dl_tbf = tbf_alloc_dl_tbf(the_bts.bts_data(),
62 NULL, 0,
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +010063 0, 0, 0);
64 dl_tbf->update_tlli(0x2342);
65 dl_tbf->tlli_mark_valid();
66 dl_tbf->ta = 4;
67 the_bts.timing_advance()->remember(0x2342, dl_tbf->ta);
68
Daniel Willmann48aa0b02014-07-16 18:54:10 +020069 gprs_rlcmac_tbf *ul_tbf = tbf_alloc_ul_tbf(the_bts.bts_data(),
Jacob Erlbeck801d6fe2015-05-04 09:44:19 +020070 dl_tbf, 0,
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +010071 0, 0, 0);
72 ul_tbf->update_tlli(0x2342);
73 ul_tbf->tlli_mark_valid();
74
75
Daniel Willmannb59d61b2014-07-02 17:58:14 +020076 OSMO_ASSERT(the_bts.dl_tbf_by_tlli(0x2342) == dl_tbf);
77 OSMO_ASSERT(the_bts.ul_tbf_by_tlli(0x2342) == ul_tbf);
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +010078
79
80 /*
81 * Now check.. that DL changes and that the timing advance
82 * has changed.
83 */
84 dl_tbf->update_tlli(0x4232);
Daniel Willmannb59d61b2014-07-02 17:58:14 +020085 OSMO_ASSERT(!the_bts.dl_tbf_by_tlli(0x2342));
86 OSMO_ASSERT(!the_bts.ul_tbf_by_tlli(0x2342));
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +010087
88
Daniel Willmannb59d61b2014-07-02 17:58:14 +020089 OSMO_ASSERT(the_bts.dl_tbf_by_tlli(0x4232) == dl_tbf);
90 OSMO_ASSERT(the_bts.ul_tbf_by_tlli(0x4232) == ul_tbf);
Holger Hans Peter Freytherbc1626e2013-10-30 19:50:49 +010091
92 OSMO_ASSERT(the_bts.timing_advance()->recall(0x4232) == 4);
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +010093}
94
Daniel Willmann510d7d32014-08-15 18:19:41 +020095static uint8_t llc_data[200];
96
97int pcu_sock_send(struct msgb *msg)
98{
99 return 0;
100}
101
Jacob Erlbecka3e45092015-03-25 09:11:24 +0100102static void setup_bts(BTS *the_bts, uint8_t ts_no)
103{
104 gprs_rlcmac_bts *bts;
105 gprs_rlcmac_trx *trx;
106
107 bts = the_bts->bts_data();
108 bts->alloc_algorithm = alloc_algorithm_a;
109 trx = &bts->trx[0];
110
111 trx->pdch[ts_no].enable();
112}
113
114static gprs_rlcmac_dl_tbf *create_dl_tbf(BTS *the_bts, uint8_t ms_class,
115 uint8_t *trx_no_)
116{
117 gprs_rlcmac_bts *bts;
118 int tfi;
119 uint8_t trx_no;
120
121 gprs_rlcmac_dl_tbf *dl_tbf;
122
123 bts = the_bts->bts_data();
124
125 tfi = the_bts->tfi_find_free(GPRS_RLCMAC_DL_TBF, &trx_no, -1);
126 OSMO_ASSERT(tfi >= 0);
127 dl_tbf = tbf_alloc_dl_tbf(bts, NULL, tfi, trx_no, ms_class, 1);
128 check_tbf(dl_tbf);
129
130 /* "Establish" the DL TBF */
131 dl_tbf->dl_ass_state = GPRS_RLCMAC_DL_ASS_SEND_ASS;
132 dl_tbf->set_state(GPRS_RLCMAC_FLOW);
133 dl_tbf->m_wait_confirm = 0;
134 dl_tbf->set_new_tbf(dl_tbf);
135 check_tbf(dl_tbf);
136
137 *trx_no_ = trx_no;
138
139 return dl_tbf;
140}
141
Jacob Erlbeck2493c662015-03-25 10:05:34 +0100142static void send_rlc_block(struct gprs_rlcmac_bts *bts,
143 uint8_t trx_no, uint8_t ts_no, uint16_t arfcn,
144 uint32_t *fn, uint8_t *block_nr)
145{
146 gprs_rlcmac_rcv_rts_block(bts, trx_no, ts_no, 0, *fn, *block_nr);
147 *fn += 4;
148 if ((*fn % 13) == 12)
149 *fn += 1;
150 *block_nr += 1;
151}
152
Jacob Erlbeck5e9f40d2015-02-23 14:26:59 +0100153enum test_tbf_final_ack_mode {
154 TEST_MODE_STANDARD,
155 TEST_MODE_REVERSE_FREE
156};
157
158static void test_tbf_final_ack(enum test_tbf_final_ack_mode test_mode)
Daniel Willmann510d7d32014-08-15 18:19:41 +0200159{
160 BTS the_bts;
161 gprs_rlcmac_bts *bts;
Jacob Erlbecka3e45092015-03-25 09:11:24 +0100162 uint8_t ts_no = 4;
163 unsigned i;
Daniel Willmann510d7d32014-08-15 18:19:41 +0200164 uint8_t ms_class = 45;
165 uint32_t fn;
Jacob Erlbeck2493c662015-03-25 10:05:34 +0100166 uint8_t block_nr;
Jacob Erlbecka3e45092015-03-25 09:11:24 +0100167 uint8_t trx_no;
Daniel Willmann510d7d32014-08-15 18:19:41 +0200168
169 uint8_t rbb[64/8];
170
Daniel Willmann510d7d32014-08-15 18:19:41 +0200171 gprs_rlcmac_dl_tbf *dl_tbf;
172 gprs_rlcmac_tbf *new_tbf;
173
174 bts = the_bts.bts_data();
Daniel Willmann510d7d32014-08-15 18:19:41 +0200175
Jacob Erlbecka3e45092015-03-25 09:11:24 +0100176 setup_bts(&the_bts, ts_no);
177 dl_tbf = create_dl_tbf(&the_bts, ms_class, &trx_no);
Daniel Willmann510d7d32014-08-15 18:19:41 +0200178
179 for (i = 0; i < sizeof(llc_data); i++)
180 llc_data[i] = i%256;
181
Jacob Erlbeck2493c662015-03-25 10:05:34 +0100182 /* Schedule two LLC frames */
Daniel Willmann0f58af62014-09-19 11:57:21 +0200183 dl_tbf->append_data(ms_class, 1000, llc_data, sizeof(llc_data));
184 dl_tbf->append_data(ms_class, 1000, llc_data, sizeof(llc_data));
Daniel Willmann510d7d32014-08-15 18:19:41 +0200185
186
Jacob Erlbeck2493c662015-03-25 10:05:34 +0100187 /* Send only a few RLC/MAC blocks */
Daniel Willmann510d7d32014-08-15 18:19:41 +0200188 fn = 0;
Jacob Erlbeck2493c662015-03-25 10:05:34 +0100189 block_nr = 0;
190 while (block_nr < 3) {
Daniel Willmann510d7d32014-08-15 18:19:41 +0200191 /* Request to send one block */
Jacob Erlbeck2493c662015-03-25 10:05:34 +0100192 send_rlc_block(bts, trx_no, ts_no, 0, &fn, &block_nr);
Daniel Willmann510d7d32014-08-15 18:19:41 +0200193 }
Jacob Erlbeck2493c662015-03-25 10:05:34 +0100194 OSMO_ASSERT(dl_tbf->have_data());
195 OSMO_ASSERT(dl_tbf->state_is(GPRS_RLCMAC_FLOW));
Daniel Willmann510d7d32014-08-15 18:19:41 +0200196
197 /* Queue a final ACK */
198 memset(rbb, 0, sizeof(rbb));
199 /* Receive a final ACK */
200 dl_tbf->rcvd_dl_ack(1, 1, rbb);
201
202 /* Clean up and ensure tbfs are in the correct state */
203 OSMO_ASSERT(dl_tbf->state_is(GPRS_RLCMAC_WAIT_RELEASE));
204 new_tbf = dl_tbf->new_tbf();
Jacob Erlbeck08fe76a2015-02-23 15:10:20 +0100205 check_tbf(new_tbf);
Daniel Willmann510d7d32014-08-15 18:19:41 +0200206 OSMO_ASSERT(new_tbf != dl_tbf);
207 OSMO_ASSERT(new_tbf->tfi() == 1);
Jacob Erlbeck08fe76a2015-02-23 15:10:20 +0100208 check_tbf(dl_tbf);
Daniel Willmann510d7d32014-08-15 18:19:41 +0200209 dl_tbf->dl_ass_state = GPRS_RLCMAC_DL_ASS_NONE;
Jacob Erlbeck5e9f40d2015-02-23 14:26:59 +0100210 if (test_mode == TEST_MODE_REVERSE_FREE) {
211 tbf_free(new_tbf);
Jacob Erlbeck08fe76a2015-02-23 15:10:20 +0100212 OSMO_ASSERT(dl_tbf->m_new_tbf != new_tbf);
213 check_tbf(dl_tbf);
Jacob Erlbeck5e9f40d2015-02-23 14:26:59 +0100214 tbf_free(dl_tbf);
215 } else {
216 tbf_free(dl_tbf);
217 OSMO_ASSERT(new_tbf->m_new_tbf != dl_tbf);
Jacob Erlbeck08fe76a2015-02-23 15:10:20 +0100218 check_tbf(new_tbf);
Jacob Erlbeck5e9f40d2015-02-23 14:26:59 +0100219 tbf_free(new_tbf);
220 }
Daniel Willmann510d7d32014-08-15 18:19:41 +0200221}
222
Jacob Erlbeck2cbe80b2015-03-25 10:48:52 +0100223static void test_tbf_delayed_release()
224{
225 BTS the_bts;
226 gprs_rlcmac_bts *bts;
227 uint8_t ts_no = 4;
228 unsigned i;
229 uint8_t ms_class = 45;
230 uint32_t fn = 0;
231 uint8_t block_nr = 0;
232 uint8_t trx_no;
233
234 uint8_t rbb[64/8];
235
236 gprs_rlcmac_dl_tbf *dl_tbf;
237
238 printf("=== start %s ===\n", __func__);
239
240 bts = the_bts.bts_data();
241
242 setup_bts(&the_bts, ts_no);
243 bts->dl_tbf_idle_msec = 200;
244
245 dl_tbf = create_dl_tbf(&the_bts, ms_class, &trx_no);
246
247 for (i = 0; i < sizeof(llc_data); i++)
248 llc_data[i] = i%256;
249
250 OSMO_ASSERT(dl_tbf->state_is(GPRS_RLCMAC_FLOW));
251
252 /* Schedule two LLC frames */
253 dl_tbf->append_data(ms_class, 1000, llc_data, sizeof(llc_data));
254 dl_tbf->append_data(ms_class, 1000, llc_data, sizeof(llc_data));
255
256 OSMO_ASSERT(dl_tbf->state_is(GPRS_RLCMAC_FLOW));
257
258 /* Drain the queue */
259 while (dl_tbf->have_data())
260 /* Request to send one RLC/MAC block */
261 send_rlc_block(bts, trx_no, ts_no, 0, &fn, &block_nr);
262
263 OSMO_ASSERT(dl_tbf->state_is(GPRS_RLCMAC_FLOW));
264
265 /* ACK all blocks */
266 memset(rbb, 0xff, sizeof(rbb));
267 /* Receive an ACK */
268 dl_tbf->rcvd_dl_ack(0, dl_tbf->m_window.v_s(), rbb);
269 OSMO_ASSERT(dl_tbf->m_window.window_empty());
270
271 /* Force sending of a single block containing an LLC dummy command */
272 send_rlc_block(bts, trx_no, ts_no, 0, &fn, &block_nr);
273
274 /* Receive an ACK */
275 dl_tbf->rcvd_dl_ack(0, dl_tbf->m_window.v_s(), rbb);
276 OSMO_ASSERT(dl_tbf->m_window.window_empty());
277
278 /* Timeout (make sure fn % 52 remains valid) */
279 fn += 52 * ((msecs_to_frames(bts->dl_tbf_idle_msec + 100) + 51)/ 52);
280 send_rlc_block(bts, trx_no, ts_no, 0, &fn, &block_nr);
281
282 OSMO_ASSERT(dl_tbf->state_is(GPRS_RLCMAC_FINISHED));
283
284 /* Receive a final ACK */
285 dl_tbf->rcvd_dl_ack(1, dl_tbf->m_window.v_s(), rbb);
286
287 /* Clean up and ensure tbfs are in the correct state */
288 OSMO_ASSERT(dl_tbf->state_is(GPRS_RLCMAC_WAIT_RELEASE));
289 OSMO_ASSERT(dl_tbf->new_tbf() == dl_tbf);
290 dl_tbf->dl_ass_state = GPRS_RLCMAC_DL_ASS_NONE;
291 check_tbf(dl_tbf);
292 tbf_free(dl_tbf);
293 printf("=== end %s ===\n", __func__);
294}
295
Jacob Erlbeckd58b7112015-04-09 19:17:21 +0200296static void test_tbf_exhaustion()
297{
298 BTS the_bts;
299 gprs_rlcmac_bts *bts;
300 unsigned i;
301 uint8_t ts_no = 4;
302 uint8_t ms_class = 45;
303 int rc = 0;
304
305 uint8_t buf[256] = {0};
306
307 printf("=== start %s ===\n", __func__);
308
309 bts = the_bts.bts_data();
310 setup_bts(&the_bts, ts_no);
311 gprs_bssgp_create_and_connect(bts, 33001, 0, 33001,
312 1234, 1234, 1234, 1, 1, 0, 0, 0);
313
314 for (i = 0; i < 1024; i++) {
315 uint32_t tlli = 0xc0000000 + i;
316 char imsi[16] = {0};
317 unsigned delay_csec = 1000;
318
319 snprintf(imsi, sizeof(imsi)-1, "001001%9d", i);
320
321 rc = gprs_rlcmac_dl_tbf::handle(bts, tlli, imsi, ms_class,
322 delay_csec, buf, sizeof(buf));
323
324 if (rc < 0)
325 break;
326 }
327
328 OSMO_ASSERT(rc == -EBUSY);
329 printf("=== end %s ===\n", __func__);
330
331 gprs_bssgp_destroy();
332}
333
Daniel Willmann341689d2014-06-11 18:33:14 +0200334static const struct log_info_cat default_categories[] = {
335 {"DCSN1", "\033[1;31m", "Concrete Syntax Notation One (CSN1)", LOGL_INFO, 0},
336 {"DL1IF", "\033[1;32m", "GPRS PCU L1 interface (L1IF)", LOGL_DEBUG, 1},
337 {"DRLCMAC", "\033[0;33m", "GPRS RLC/MAC layer (RLCMAC)", LOGL_DEBUG, 1},
338 {"DRLCMACDATA", "\033[0;33m", "GPRS RLC/MAC layer Data (RLCMAC)", LOGL_DEBUG, 1},
339 {"DRLCMACDL", "\033[1;33m", "GPRS RLC/MAC layer Downlink (RLCMAC)", LOGL_DEBUG, 1},
340 {"DRLCMACUL", "\033[1;36m", "GPRS RLC/MAC layer Uplink (RLCMAC)", LOGL_DEBUG, 1},
341 {"DRLCMACSCHED", "\033[0;36m", "GPRS RLC/MAC layer Scheduling (RLCMAC)", LOGL_DEBUG, 1},
342 {"DRLCMACMEAS", "\033[1;31m", "GPRS RLC/MAC layer Measurements (RLCMAC)", LOGL_INFO, 1},
343 {"DBSSGP","\033[1;34m", "GPRS BSS Gateway Protocol (BSSGP)", LOGL_INFO , 1},
344 {"DPCU", "\033[1;35m", "GPRS Packet Control Unit (PCU)", LOGL_NOTICE, 1},
345};
346
347static int filter_fn(const struct log_context *ctx,
348 struct log_target *tar)
349{
350 return 1;
351}
352
353const struct log_info debug_log_info = {
354 filter_fn,
355 (struct log_info_cat*)default_categories,
356 ARRAY_SIZE(default_categories),
357};
358
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +0100359int main(int argc, char **argv)
360{
Jacob Erlbeckd58b7112015-04-09 19:17:21 +0200361 struct vty_app_info pcu_vty_info = {0};
362
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +0100363 tall_pcu_ctx = talloc_named_const(NULL, 1, "moiji-mobile TbfTest context");
364 if (!tall_pcu_ctx)
365 abort();
366
367 msgb_set_talloc_ctx(tall_pcu_ctx);
Daniel Willmann341689d2014-06-11 18:33:14 +0200368 osmo_init_logging(&debug_log_info);
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +0100369 log_set_use_color(osmo_stderr_target, 0);
370 log_set_print_filename(osmo_stderr_target, 0);
Jacob Erlbeckd58b7112015-04-09 19:17:21 +0200371 bssgp_set_log_ss(DBSSGP);
372
373 vty_init(&pcu_vty_info);
374 pcu_vty_init(&debug_log_info);
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +0100375
376 test_tbf_tlli_update();
Jacob Erlbeck5e9f40d2015-02-23 14:26:59 +0100377 test_tbf_final_ack(TEST_MODE_STANDARD);
378 test_tbf_final_ack(TEST_MODE_REVERSE_FREE);
Jacob Erlbeck2cbe80b2015-03-25 10:48:52 +0100379 test_tbf_delayed_release();
Jacob Erlbeckd58b7112015-04-09 19:17:21 +0200380 test_tbf_exhaustion();
Jacob Erlbeck67c38502015-05-11 10:32:40 +0200381
382 if (getenv("TALLOC_REPORT_FULL"))
383 talloc_report_full(tall_pcu_ctx, stderr);
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +0100384 return EXIT_SUCCESS;
385}
386
387/*
388 * stubs that should not be reached
389 */
390extern "C" {
391void l1if_pdch_req() { abort(); }
392void l1if_connect_pdch() { abort(); }
393void l1if_close_pdch() { abort(); }
394void l1if_open_pdch() { abort(); }
395}