blob: c3230809ea1490a22114391dba12b9f4dbe423e5 [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"
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +010027
28extern "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
35void *tall_pcu_ctx;
36int16_t spoof_mnc = 0, spoof_mcc = 0;
37
Jacob Erlbeck08fe76a2015-02-23 15:10:20 +010038static 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 Freytherb8098662013-10-30 14:50:17 +010045static 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 Willmann48aa0b02014-07-16 18:54:10 +020055 gprs_rlcmac_tbf *dl_tbf = tbf_alloc_dl_tbf(the_bts.bts_data(),
56 NULL, 0,
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +010057 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 Willmann48aa0b02014-07-16 18:54:10 +020063 gprs_rlcmac_tbf *ul_tbf = tbf_alloc_ul_tbf(the_bts.bts_data(),
64 ul_tbf, 0,
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +010065 0, 0, 0);
66 ul_tbf->update_tlli(0x2342);
67 ul_tbf->tlli_mark_valid();
68
69
Daniel Willmannb59d61b2014-07-02 17:58:14 +020070 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 Freytherb8098662013-10-30 14:50:17 +010072
73
74 /*
75 * Now check.. that DL changes and that the timing advance
76 * has changed.
77 */
78 dl_tbf->update_tlli(0x4232);
Daniel Willmannb59d61b2014-07-02 17:58:14 +020079 OSMO_ASSERT(!the_bts.dl_tbf_by_tlli(0x2342));
80 OSMO_ASSERT(!the_bts.ul_tbf_by_tlli(0x2342));
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +010081
82
Daniel Willmannb59d61b2014-07-02 17:58:14 +020083 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 Freytherbc1626e2013-10-30 19:50:49 +010085
86 OSMO_ASSERT(the_bts.timing_advance()->recall(0x4232) == 4);
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +010087}
88
Daniel Willmann510d7d32014-08-15 18:19:41 +020089static uint8_t llc_data[200];
90
91int pcu_sock_send(struct msgb *msg)
92{
93 return 0;
94}
95
Jacob Erlbecka3e45092015-03-25 09:11:24 +010096static 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
108static 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 Erlbeck2493c662015-03-25 10:05:34 +0100136static 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 Erlbeck5e9f40d2015-02-23 14:26:59 +0100147enum test_tbf_final_ack_mode {
148 TEST_MODE_STANDARD,
149 TEST_MODE_REVERSE_FREE
150};
151
152static void test_tbf_final_ack(enum test_tbf_final_ack_mode test_mode)
Daniel Willmann510d7d32014-08-15 18:19:41 +0200153{
154 BTS the_bts;
155 gprs_rlcmac_bts *bts;
Jacob Erlbecka3e45092015-03-25 09:11:24 +0100156 uint8_t ts_no = 4;
157 unsigned i;
Daniel Willmann510d7d32014-08-15 18:19:41 +0200158 uint8_t ms_class = 45;
159 uint32_t fn;
Jacob Erlbeck2493c662015-03-25 10:05:34 +0100160 uint8_t block_nr;
Jacob Erlbecka3e45092015-03-25 09:11:24 +0100161 uint8_t trx_no;
Daniel Willmann510d7d32014-08-15 18:19:41 +0200162
163 uint8_t rbb[64/8];
164
Daniel Willmann510d7d32014-08-15 18:19:41 +0200165 gprs_rlcmac_dl_tbf *dl_tbf;
166 gprs_rlcmac_tbf *new_tbf;
167
168 bts = the_bts.bts_data();
Daniel Willmann510d7d32014-08-15 18:19:41 +0200169
Jacob Erlbecka3e45092015-03-25 09:11:24 +0100170 setup_bts(&the_bts, ts_no);
171 dl_tbf = create_dl_tbf(&the_bts, ms_class, &trx_no);
Daniel Willmann510d7d32014-08-15 18:19:41 +0200172
173 for (i = 0; i < sizeof(llc_data); i++)
174 llc_data[i] = i%256;
175
Jacob Erlbeck2493c662015-03-25 10:05:34 +0100176 /* Schedule two LLC frames */
Daniel Willmann0f58af62014-09-19 11:57:21 +0200177 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 Willmann510d7d32014-08-15 18:19:41 +0200179
180
Jacob Erlbeck2493c662015-03-25 10:05:34 +0100181 /* Send only a few RLC/MAC blocks */
Daniel Willmann510d7d32014-08-15 18:19:41 +0200182 fn = 0;
Jacob Erlbeck2493c662015-03-25 10:05:34 +0100183 block_nr = 0;
184 while (block_nr < 3) {
Daniel Willmann510d7d32014-08-15 18:19:41 +0200185 /* Request to send one block */
Jacob Erlbeck2493c662015-03-25 10:05:34 +0100186 send_rlc_block(bts, trx_no, ts_no, 0, &fn, &block_nr);
Daniel Willmann510d7d32014-08-15 18:19:41 +0200187 }
Jacob Erlbeck2493c662015-03-25 10:05:34 +0100188 OSMO_ASSERT(dl_tbf->have_data());
189 OSMO_ASSERT(dl_tbf->state_is(GPRS_RLCMAC_FLOW));
Daniel Willmann510d7d32014-08-15 18:19:41 +0200190
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 Erlbeck08fe76a2015-02-23 15:10:20 +0100199 check_tbf(new_tbf);
Daniel Willmann510d7d32014-08-15 18:19:41 +0200200 OSMO_ASSERT(new_tbf != dl_tbf);
201 OSMO_ASSERT(new_tbf->tfi() == 1);
Jacob Erlbeck08fe76a2015-02-23 15:10:20 +0100202 check_tbf(dl_tbf);
Daniel Willmann510d7d32014-08-15 18:19:41 +0200203 dl_tbf->dl_ass_state = GPRS_RLCMAC_DL_ASS_NONE;
Jacob Erlbeck5e9f40d2015-02-23 14:26:59 +0100204 if (test_mode == TEST_MODE_REVERSE_FREE) {
205 tbf_free(new_tbf);
Jacob Erlbeck08fe76a2015-02-23 15:10:20 +0100206 OSMO_ASSERT(dl_tbf->m_new_tbf != new_tbf);
207 check_tbf(dl_tbf);
Jacob Erlbeck5e9f40d2015-02-23 14:26:59 +0100208 tbf_free(dl_tbf);
209 } else {
210 tbf_free(dl_tbf);
211 OSMO_ASSERT(new_tbf->m_new_tbf != dl_tbf);
Jacob Erlbeck08fe76a2015-02-23 15:10:20 +0100212 check_tbf(new_tbf);
Jacob Erlbeck5e9f40d2015-02-23 14:26:59 +0100213 tbf_free(new_tbf);
214 }
Daniel Willmann510d7d32014-08-15 18:19:41 +0200215}
216
Jacob Erlbeck2cbe80b2015-03-25 10:48:52 +0100217static 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 Willmann341689d2014-06-11 18:33:14 +0200290static 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
303static int filter_fn(const struct log_context *ctx,
304 struct log_target *tar)
305{
306 return 1;
307}
308
309const 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 Freytherb8098662013-10-30 14:50:17 +0100315int 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 Willmann341689d2014-06-11 18:33:14 +0200322 osmo_init_logging(&debug_log_info);
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +0100323 log_set_use_color(osmo_stderr_target, 0);
324 log_set_print_filename(osmo_stderr_target, 0);
325
326 test_tbf_tlli_update();
Jacob Erlbeck5e9f40d2015-02-23 14:26:59 +0100327 test_tbf_final_ack(TEST_MODE_STANDARD);
328 test_tbf_final_ack(TEST_MODE_REVERSE_FREE);
Jacob Erlbeck2cbe80b2015-03-25 10:48:52 +0100329 test_tbf_delayed_release();
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +0100330 return EXIT_SUCCESS;
331}
332
333/*
334 * stubs that should not be reached
335 */
336extern "C" {
337void l1if_pdch_req() { abort(); }
338void l1if_connect_pdch() { abort(); }
339void l1if_close_pdch() { abort(); }
340void l1if_open_pdch() { abort(); }
341}