blob: bdae4436d487a19148e52747b1073d5c420f667e [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);
Jacob Erlbeckbe0cbc12015-05-18 14:35:11 +020067 dl_tbf->update_ms(0x2342, GPRS_RLCMAC_DL_TBF);
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +010068 dl_tbf->ta = 4;
69 the_bts.timing_advance()->remember(0x2342, dl_tbf->ta);
70
Daniel Willmann48aa0b02014-07-16 18:54:10 +020071 gprs_rlcmac_tbf *ul_tbf = tbf_alloc_ul_tbf(the_bts.bts_data(),
Jacob Erlbeck801d6fe2015-05-04 09:44:19 +020072 dl_tbf, 0,
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +010073 0, 0, 0);
74 ul_tbf->update_tlli(0x2342);
Jacob Erlbeck0e50ce62015-05-21 16:58:22 +020075 ul_tbf->update_ms(0x2342, GPRS_RLCMAC_UL_TBF);
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +010076
Jacob Erlbeck93990462015-05-15 15:50:43 +020077 ms = the_bts.ms_by_tlli(0x2342);
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +010078
Jacob Erlbeck93990462015-05-15 15:50:43 +020079 OSMO_ASSERT(ms != NULL);
80 OSMO_ASSERT(ms->dl_tbf() == dl_tbf);
81 OSMO_ASSERT(ms->ul_tbf() == ul_tbf);
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +010082
83 /*
84 * Now check.. that DL changes and that the timing advance
85 * has changed.
86 */
87 dl_tbf->update_tlli(0x4232);
Jacob Erlbeck0e50ce62015-05-21 16:58:22 +020088 dl_tbf->update_ms(0x4232, GPRS_RLCMAC_DL_TBF);
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +010089
Jacob Erlbeck93990462015-05-15 15:50:43 +020090 /* It is still there, since the new TLLI has not been used for UL yet */
91 ms_new = the_bts.ms_by_tlli(0x2342);
92 OSMO_ASSERT(ms == ms_new);
93
94 ms_new = the_bts.ms_by_tlli(0x4232);
95 OSMO_ASSERT(ms == ms_new);
96 OSMO_ASSERT(ms->dl_tbf() == dl_tbf);
97 OSMO_ASSERT(ms->ul_tbf() == ul_tbf);
98
99 /* Now use the new TLLI for UL */
Jacob Erlbeck0e50ce62015-05-21 16:58:22 +0200100 ul_tbf->update_ms(0x4232, GPRS_RLCMAC_UL_TBF);
Jacob Erlbeck93990462015-05-15 15:50:43 +0200101 ms_new = the_bts.ms_by_tlli(0x2342);
102 OSMO_ASSERT(ms_new == NULL);
Holger Hans Peter Freytherbc1626e2013-10-30 19:50:49 +0100103
104 OSMO_ASSERT(the_bts.timing_advance()->recall(0x4232) == 4);
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +0100105}
106
Daniel Willmann510d7d32014-08-15 18:19:41 +0200107static uint8_t llc_data[200];
108
109int pcu_sock_send(struct msgb *msg)
110{
111 return 0;
112}
113
Jacob Erlbecka3e45092015-03-25 09:11:24 +0100114static void setup_bts(BTS *the_bts, uint8_t ts_no)
115{
116 gprs_rlcmac_bts *bts;
117 gprs_rlcmac_trx *trx;
118
119 bts = the_bts->bts_data();
120 bts->alloc_algorithm = alloc_algorithm_a;
121 trx = &bts->trx[0];
122
123 trx->pdch[ts_no].enable();
124}
125
126static gprs_rlcmac_dl_tbf *create_dl_tbf(BTS *the_bts, uint8_t ms_class,
127 uint8_t *trx_no_)
128{
129 gprs_rlcmac_bts *bts;
130 int tfi;
131 uint8_t trx_no;
132
133 gprs_rlcmac_dl_tbf *dl_tbf;
134
135 bts = the_bts->bts_data();
136
137 tfi = the_bts->tfi_find_free(GPRS_RLCMAC_DL_TBF, &trx_no, -1);
138 OSMO_ASSERT(tfi >= 0);
139 dl_tbf = tbf_alloc_dl_tbf(bts, NULL, tfi, trx_no, ms_class, 1);
140 check_tbf(dl_tbf);
141
142 /* "Establish" the DL TBF */
143 dl_tbf->dl_ass_state = GPRS_RLCMAC_DL_ASS_SEND_ASS;
144 dl_tbf->set_state(GPRS_RLCMAC_FLOW);
145 dl_tbf->m_wait_confirm = 0;
146 dl_tbf->set_new_tbf(dl_tbf);
147 check_tbf(dl_tbf);
148
149 *trx_no_ = trx_no;
150
151 return dl_tbf;
152}
153
Jacob Erlbeck2493c662015-03-25 10:05:34 +0100154static void send_rlc_block(struct gprs_rlcmac_bts *bts,
155 uint8_t trx_no, uint8_t ts_no, uint16_t arfcn,
156 uint32_t *fn, uint8_t *block_nr)
157{
158 gprs_rlcmac_rcv_rts_block(bts, trx_no, ts_no, 0, *fn, *block_nr);
159 *fn += 4;
160 if ((*fn % 13) == 12)
161 *fn += 1;
162 *block_nr += 1;
163}
164
Jacob Erlbeck5e9f40d2015-02-23 14:26:59 +0100165enum test_tbf_final_ack_mode {
166 TEST_MODE_STANDARD,
167 TEST_MODE_REVERSE_FREE
168};
169
170static void test_tbf_final_ack(enum test_tbf_final_ack_mode test_mode)
Daniel Willmann510d7d32014-08-15 18:19:41 +0200171{
172 BTS the_bts;
173 gprs_rlcmac_bts *bts;
Jacob Erlbecka3e45092015-03-25 09:11:24 +0100174 uint8_t ts_no = 4;
175 unsigned i;
Daniel Willmann510d7d32014-08-15 18:19:41 +0200176 uint8_t ms_class = 45;
177 uint32_t fn;
Jacob Erlbeck2493c662015-03-25 10:05:34 +0100178 uint8_t block_nr;
Jacob Erlbecka3e45092015-03-25 09:11:24 +0100179 uint8_t trx_no;
Daniel Willmann510d7d32014-08-15 18:19:41 +0200180
181 uint8_t rbb[64/8];
182
Daniel Willmann510d7d32014-08-15 18:19:41 +0200183 gprs_rlcmac_dl_tbf *dl_tbf;
184 gprs_rlcmac_tbf *new_tbf;
185
186 bts = the_bts.bts_data();
Daniel Willmann510d7d32014-08-15 18:19:41 +0200187
Jacob Erlbecka3e45092015-03-25 09:11:24 +0100188 setup_bts(&the_bts, ts_no);
189 dl_tbf = create_dl_tbf(&the_bts, ms_class, &trx_no);
Daniel Willmann510d7d32014-08-15 18:19:41 +0200190
191 for (i = 0; i < sizeof(llc_data); i++)
192 llc_data[i] = i%256;
193
Jacob Erlbeck2493c662015-03-25 10:05:34 +0100194 /* Schedule two LLC frames */
Daniel Willmann0f58af62014-09-19 11:57:21 +0200195 dl_tbf->append_data(ms_class, 1000, llc_data, sizeof(llc_data));
196 dl_tbf->append_data(ms_class, 1000, llc_data, sizeof(llc_data));
Daniel Willmann510d7d32014-08-15 18:19:41 +0200197
198
Jacob Erlbeck2493c662015-03-25 10:05:34 +0100199 /* Send only a few RLC/MAC blocks */
Daniel Willmann510d7d32014-08-15 18:19:41 +0200200 fn = 0;
Jacob Erlbeck2493c662015-03-25 10:05:34 +0100201 block_nr = 0;
202 while (block_nr < 3) {
Daniel Willmann510d7d32014-08-15 18:19:41 +0200203 /* Request to send one block */
Jacob Erlbeck2493c662015-03-25 10:05:34 +0100204 send_rlc_block(bts, trx_no, ts_no, 0, &fn, &block_nr);
Daniel Willmann510d7d32014-08-15 18:19:41 +0200205 }
Jacob Erlbeck2493c662015-03-25 10:05:34 +0100206 OSMO_ASSERT(dl_tbf->have_data());
207 OSMO_ASSERT(dl_tbf->state_is(GPRS_RLCMAC_FLOW));
Daniel Willmann510d7d32014-08-15 18:19:41 +0200208
209 /* Queue a final ACK */
210 memset(rbb, 0, sizeof(rbb));
211 /* Receive a final ACK */
212 dl_tbf->rcvd_dl_ack(1, 1, rbb);
213
214 /* Clean up and ensure tbfs are in the correct state */
215 OSMO_ASSERT(dl_tbf->state_is(GPRS_RLCMAC_WAIT_RELEASE));
216 new_tbf = dl_tbf->new_tbf();
Jacob Erlbeck08fe76a2015-02-23 15:10:20 +0100217 check_tbf(new_tbf);
Daniel Willmann510d7d32014-08-15 18:19:41 +0200218 OSMO_ASSERT(new_tbf != dl_tbf);
219 OSMO_ASSERT(new_tbf->tfi() == 1);
Jacob Erlbeck08fe76a2015-02-23 15:10:20 +0100220 check_tbf(dl_tbf);
Daniel Willmann510d7d32014-08-15 18:19:41 +0200221 dl_tbf->dl_ass_state = GPRS_RLCMAC_DL_ASS_NONE;
Jacob Erlbeck5e9f40d2015-02-23 14:26:59 +0100222 if (test_mode == TEST_MODE_REVERSE_FREE) {
223 tbf_free(new_tbf);
Jacob Erlbeck08fe76a2015-02-23 15:10:20 +0100224 OSMO_ASSERT(dl_tbf->m_new_tbf != new_tbf);
225 check_tbf(dl_tbf);
Jacob Erlbeck5e9f40d2015-02-23 14:26:59 +0100226 tbf_free(dl_tbf);
227 } else {
228 tbf_free(dl_tbf);
229 OSMO_ASSERT(new_tbf->m_new_tbf != dl_tbf);
Jacob Erlbeck08fe76a2015-02-23 15:10:20 +0100230 check_tbf(new_tbf);
Jacob Erlbeck5e9f40d2015-02-23 14:26:59 +0100231 tbf_free(new_tbf);
232 }
Daniel Willmann510d7d32014-08-15 18:19:41 +0200233}
234
Jacob Erlbeck2cbe80b2015-03-25 10:48:52 +0100235static void test_tbf_delayed_release()
236{
237 BTS the_bts;
238 gprs_rlcmac_bts *bts;
239 uint8_t ts_no = 4;
240 unsigned i;
241 uint8_t ms_class = 45;
242 uint32_t fn = 0;
243 uint8_t block_nr = 0;
244 uint8_t trx_no;
245
246 uint8_t rbb[64/8];
247
248 gprs_rlcmac_dl_tbf *dl_tbf;
249
250 printf("=== start %s ===\n", __func__);
251
252 bts = the_bts.bts_data();
253
254 setup_bts(&the_bts, ts_no);
255 bts->dl_tbf_idle_msec = 200;
256
257 dl_tbf = create_dl_tbf(&the_bts, ms_class, &trx_no);
258
259 for (i = 0; i < sizeof(llc_data); i++)
260 llc_data[i] = i%256;
261
262 OSMO_ASSERT(dl_tbf->state_is(GPRS_RLCMAC_FLOW));
263
264 /* Schedule two LLC frames */
265 dl_tbf->append_data(ms_class, 1000, llc_data, sizeof(llc_data));
266 dl_tbf->append_data(ms_class, 1000, llc_data, sizeof(llc_data));
267
268 OSMO_ASSERT(dl_tbf->state_is(GPRS_RLCMAC_FLOW));
269
270 /* Drain the queue */
271 while (dl_tbf->have_data())
272 /* Request to send one RLC/MAC block */
273 send_rlc_block(bts, trx_no, ts_no, 0, &fn, &block_nr);
274
275 OSMO_ASSERT(dl_tbf->state_is(GPRS_RLCMAC_FLOW));
276
277 /* ACK all blocks */
278 memset(rbb, 0xff, sizeof(rbb));
279 /* Receive an ACK */
280 dl_tbf->rcvd_dl_ack(0, dl_tbf->m_window.v_s(), rbb);
281 OSMO_ASSERT(dl_tbf->m_window.window_empty());
282
283 /* Force sending of a single block containing an LLC dummy command */
284 send_rlc_block(bts, trx_no, ts_no, 0, &fn, &block_nr);
285
286 /* Receive an ACK */
287 dl_tbf->rcvd_dl_ack(0, dl_tbf->m_window.v_s(), rbb);
288 OSMO_ASSERT(dl_tbf->m_window.window_empty());
289
290 /* Timeout (make sure fn % 52 remains valid) */
291 fn += 52 * ((msecs_to_frames(bts->dl_tbf_idle_msec + 100) + 51)/ 52);
292 send_rlc_block(bts, trx_no, ts_no, 0, &fn, &block_nr);
293
294 OSMO_ASSERT(dl_tbf->state_is(GPRS_RLCMAC_FINISHED));
295
296 /* Receive a final ACK */
297 dl_tbf->rcvd_dl_ack(1, dl_tbf->m_window.v_s(), rbb);
298
299 /* Clean up and ensure tbfs are in the correct state */
300 OSMO_ASSERT(dl_tbf->state_is(GPRS_RLCMAC_WAIT_RELEASE));
301 OSMO_ASSERT(dl_tbf->new_tbf() == dl_tbf);
302 dl_tbf->dl_ass_state = GPRS_RLCMAC_DL_ASS_NONE;
303 check_tbf(dl_tbf);
304 tbf_free(dl_tbf);
305 printf("=== end %s ===\n", __func__);
306}
307
Jacob Erlbeckd58b7112015-04-09 19:17:21 +0200308static void test_tbf_exhaustion()
309{
310 BTS the_bts;
311 gprs_rlcmac_bts *bts;
312 unsigned i;
313 uint8_t ts_no = 4;
314 uint8_t ms_class = 45;
315 int rc = 0;
316
317 uint8_t buf[256] = {0};
318
319 printf("=== start %s ===\n", __func__);
320
321 bts = the_bts.bts_data();
322 setup_bts(&the_bts, ts_no);
323 gprs_bssgp_create_and_connect(bts, 33001, 0, 33001,
324 1234, 1234, 1234, 1, 1, 0, 0, 0);
325
326 for (i = 0; i < 1024; i++) {
327 uint32_t tlli = 0xc0000000 + i;
328 char imsi[16] = {0};
329 unsigned delay_csec = 1000;
330
331 snprintf(imsi, sizeof(imsi)-1, "001001%9d", i);
332
Jacob Erlbeck93990462015-05-15 15:50:43 +0200333 rc = gprs_rlcmac_dl_tbf::handle(bts, tlli, 0, imsi, ms_class,
Jacob Erlbeckd58b7112015-04-09 19:17:21 +0200334 delay_csec, buf, sizeof(buf));
335
336 if (rc < 0)
337 break;
338 }
339
340 OSMO_ASSERT(rc == -EBUSY);
341 printf("=== end %s ===\n", __func__);
342
343 gprs_bssgp_destroy();
344}
345
Daniel Willmann341689d2014-06-11 18:33:14 +0200346static const struct log_info_cat default_categories[] = {
347 {"DCSN1", "\033[1;31m", "Concrete Syntax Notation One (CSN1)", LOGL_INFO, 0},
348 {"DL1IF", "\033[1;32m", "GPRS PCU L1 interface (L1IF)", LOGL_DEBUG, 1},
349 {"DRLCMAC", "\033[0;33m", "GPRS RLC/MAC layer (RLCMAC)", LOGL_DEBUG, 1},
350 {"DRLCMACDATA", "\033[0;33m", "GPRS RLC/MAC layer Data (RLCMAC)", LOGL_DEBUG, 1},
351 {"DRLCMACDL", "\033[1;33m", "GPRS RLC/MAC layer Downlink (RLCMAC)", LOGL_DEBUG, 1},
352 {"DRLCMACUL", "\033[1;36m", "GPRS RLC/MAC layer Uplink (RLCMAC)", LOGL_DEBUG, 1},
353 {"DRLCMACSCHED", "\033[0;36m", "GPRS RLC/MAC layer Scheduling (RLCMAC)", LOGL_DEBUG, 1},
354 {"DRLCMACMEAS", "\033[1;31m", "GPRS RLC/MAC layer Measurements (RLCMAC)", LOGL_INFO, 1},
355 {"DBSSGP","\033[1;34m", "GPRS BSS Gateway Protocol (BSSGP)", LOGL_INFO , 1},
356 {"DPCU", "\033[1;35m", "GPRS Packet Control Unit (PCU)", LOGL_NOTICE, 1},
357};
358
359static int filter_fn(const struct log_context *ctx,
360 struct log_target *tar)
361{
362 return 1;
363}
364
365const struct log_info debug_log_info = {
366 filter_fn,
367 (struct log_info_cat*)default_categories,
368 ARRAY_SIZE(default_categories),
369};
370
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +0100371int main(int argc, char **argv)
372{
Jacob Erlbeckd58b7112015-04-09 19:17:21 +0200373 struct vty_app_info pcu_vty_info = {0};
374
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +0100375 tall_pcu_ctx = talloc_named_const(NULL, 1, "moiji-mobile TbfTest context");
376 if (!tall_pcu_ctx)
377 abort();
378
379 msgb_set_talloc_ctx(tall_pcu_ctx);
Daniel Willmann341689d2014-06-11 18:33:14 +0200380 osmo_init_logging(&debug_log_info);
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +0100381 log_set_use_color(osmo_stderr_target, 0);
382 log_set_print_filename(osmo_stderr_target, 0);
Jacob Erlbeckd58b7112015-04-09 19:17:21 +0200383 bssgp_set_log_ss(DBSSGP);
384
385 vty_init(&pcu_vty_info);
386 pcu_vty_init(&debug_log_info);
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +0100387
388 test_tbf_tlli_update();
Jacob Erlbeck5e9f40d2015-02-23 14:26:59 +0100389 test_tbf_final_ack(TEST_MODE_STANDARD);
390 test_tbf_final_ack(TEST_MODE_REVERSE_FREE);
Jacob Erlbeck2cbe80b2015-03-25 10:48:52 +0100391 test_tbf_delayed_release();
Jacob Erlbeckd58b7112015-04-09 19:17:21 +0200392 test_tbf_exhaustion();
Jacob Erlbeck67c38502015-05-11 10:32:40 +0200393
394 if (getenv("TALLOC_REPORT_FULL"))
395 talloc_report_full(tall_pcu_ctx, stderr);
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +0100396 return EXIT_SUCCESS;
397}
398
399/*
400 * stubs that should not be reached
401 */
402extern "C" {
403void l1if_pdch_req() { abort(); }
404void l1if_connect_pdch() { abort(); }
405void l1if_close_pdch() { abort(); }
406void l1if_open_pdch() { abort(); }
407}