blob: a5effd38fe84625c241cffb2eae86b9436aca59b [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"
26
27extern "C" {
28#include <osmocom/core/application.h>
29#include <osmocom/core/msgb.h>
30#include <osmocom/core/talloc.h>
31#include <osmocom/core/utils.h>
32}
33
34void *tall_pcu_ctx;
35int16_t spoof_mnc = 0, spoof_mcc = 0;
36
Jacob Erlbeck08fe76a2015-02-23 15:10:20 +010037static void check_tbf(gprs_rlcmac_tbf *tbf)
38{
39 OSMO_ASSERT(tbf);
40 OSMO_ASSERT(tbf->m_new_tbf == NULL || tbf->m_new_tbf->m_old_tbf == tbf);
41 OSMO_ASSERT(tbf->m_old_tbf == NULL || tbf->m_old_tbf->m_new_tbf == tbf);
42}
43
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +010044static void test_tbf_tlli_update()
45{
46 BTS the_bts;
47 the_bts.bts_data()->alloc_algorithm = alloc_algorithm_a;
48 the_bts.bts_data()->trx[0].pdch[2].enable();
49 the_bts.bts_data()->trx[0].pdch[3].enable();
50
51 /*
52 * Make a uplink and downlink allocation
53 */
Daniel Willmann48aa0b02014-07-16 18:54:10 +020054 gprs_rlcmac_tbf *dl_tbf = tbf_alloc_dl_tbf(the_bts.bts_data(),
55 NULL, 0,
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +010056 0, 0, 0);
57 dl_tbf->update_tlli(0x2342);
58 dl_tbf->tlli_mark_valid();
59 dl_tbf->ta = 4;
60 the_bts.timing_advance()->remember(0x2342, dl_tbf->ta);
61
Daniel Willmann48aa0b02014-07-16 18:54:10 +020062 gprs_rlcmac_tbf *ul_tbf = tbf_alloc_ul_tbf(the_bts.bts_data(),
63 ul_tbf, 0,
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +010064 0, 0, 0);
65 ul_tbf->update_tlli(0x2342);
66 ul_tbf->tlli_mark_valid();
67
68
Daniel Willmannb59d61b2014-07-02 17:58:14 +020069 OSMO_ASSERT(the_bts.dl_tbf_by_tlli(0x2342) == dl_tbf);
70 OSMO_ASSERT(the_bts.ul_tbf_by_tlli(0x2342) == ul_tbf);
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +010071
72
73 /*
74 * Now check.. that DL changes and that the timing advance
75 * has changed.
76 */
77 dl_tbf->update_tlli(0x4232);
Daniel Willmannb59d61b2014-07-02 17:58:14 +020078 OSMO_ASSERT(!the_bts.dl_tbf_by_tlli(0x2342));
79 OSMO_ASSERT(!the_bts.ul_tbf_by_tlli(0x2342));
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +010080
81
Daniel Willmannb59d61b2014-07-02 17:58:14 +020082 OSMO_ASSERT(the_bts.dl_tbf_by_tlli(0x4232) == dl_tbf);
83 OSMO_ASSERT(the_bts.ul_tbf_by_tlli(0x4232) == ul_tbf);
Holger Hans Peter Freytherbc1626e2013-10-30 19:50:49 +010084
85 OSMO_ASSERT(the_bts.timing_advance()->recall(0x4232) == 4);
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +010086}
87
Daniel Willmann510d7d32014-08-15 18:19:41 +020088static uint8_t llc_data[200];
89
90int pcu_sock_send(struct msgb *msg)
91{
92 return 0;
93}
94
Jacob Erlbecka3e45092015-03-25 09:11:24 +010095static void setup_bts(BTS *the_bts, uint8_t ts_no)
96{
97 gprs_rlcmac_bts *bts;
98 gprs_rlcmac_trx *trx;
99
100 bts = the_bts->bts_data();
101 bts->alloc_algorithm = alloc_algorithm_a;
102 trx = &bts->trx[0];
103
104 trx->pdch[ts_no].enable();
105}
106
107static gprs_rlcmac_dl_tbf *create_dl_tbf(BTS *the_bts, uint8_t ms_class,
108 uint8_t *trx_no_)
109{
110 gprs_rlcmac_bts *bts;
111 int tfi;
112 uint8_t trx_no;
113
114 gprs_rlcmac_dl_tbf *dl_tbf;
115
116 bts = the_bts->bts_data();
117
118 tfi = the_bts->tfi_find_free(GPRS_RLCMAC_DL_TBF, &trx_no, -1);
119 OSMO_ASSERT(tfi >= 0);
120 dl_tbf = tbf_alloc_dl_tbf(bts, NULL, tfi, trx_no, ms_class, 1);
121 check_tbf(dl_tbf);
122
123 /* "Establish" the DL TBF */
124 dl_tbf->dl_ass_state = GPRS_RLCMAC_DL_ASS_SEND_ASS;
125 dl_tbf->set_state(GPRS_RLCMAC_FLOW);
126 dl_tbf->m_wait_confirm = 0;
127 dl_tbf->set_new_tbf(dl_tbf);
128 check_tbf(dl_tbf);
129
130 *trx_no_ = trx_no;
131
132 return dl_tbf;
133}
134
Jacob Erlbeck2493c662015-03-25 10:05:34 +0100135static void send_rlc_block(struct gprs_rlcmac_bts *bts,
136 uint8_t trx_no, uint8_t ts_no, uint16_t arfcn,
137 uint32_t *fn, uint8_t *block_nr)
138{
139 gprs_rlcmac_rcv_rts_block(bts, trx_no, ts_no, 0, *fn, *block_nr);
140 *fn += 4;
141 if ((*fn % 13) == 12)
142 *fn += 1;
143 *block_nr += 1;
144}
145
Jacob Erlbeck5e9f40d2015-02-23 14:26:59 +0100146enum test_tbf_final_ack_mode {
147 TEST_MODE_STANDARD,
148 TEST_MODE_REVERSE_FREE
149};
150
151static void test_tbf_final_ack(enum test_tbf_final_ack_mode test_mode)
Daniel Willmann510d7d32014-08-15 18:19:41 +0200152{
153 BTS the_bts;
154 gprs_rlcmac_bts *bts;
Jacob Erlbecka3e45092015-03-25 09:11:24 +0100155 uint8_t ts_no = 4;
156 unsigned i;
Daniel Willmann510d7d32014-08-15 18:19:41 +0200157 uint8_t ms_class = 45;
158 uint32_t fn;
Jacob Erlbeck2493c662015-03-25 10:05:34 +0100159 uint8_t block_nr;
Jacob Erlbecka3e45092015-03-25 09:11:24 +0100160 uint8_t trx_no;
Daniel Willmann510d7d32014-08-15 18:19:41 +0200161
162 uint8_t rbb[64/8];
163
Daniel Willmann510d7d32014-08-15 18:19:41 +0200164 gprs_rlcmac_dl_tbf *dl_tbf;
165 gprs_rlcmac_tbf *new_tbf;
166
167 bts = the_bts.bts_data();
Daniel Willmann510d7d32014-08-15 18:19:41 +0200168
Jacob Erlbecka3e45092015-03-25 09:11:24 +0100169 setup_bts(&the_bts, ts_no);
170 dl_tbf = create_dl_tbf(&the_bts, ms_class, &trx_no);
Daniel Willmann510d7d32014-08-15 18:19:41 +0200171
172 for (i = 0; i < sizeof(llc_data); i++)
173 llc_data[i] = i%256;
174
Jacob Erlbeck2493c662015-03-25 10:05:34 +0100175 /* Schedule two LLC frames */
Daniel Willmann0f58af62014-09-19 11:57:21 +0200176 dl_tbf->append_data(ms_class, 1000, llc_data, sizeof(llc_data));
177 dl_tbf->append_data(ms_class, 1000, llc_data, sizeof(llc_data));
Daniel Willmann510d7d32014-08-15 18:19:41 +0200178
179
Jacob Erlbeck2493c662015-03-25 10:05:34 +0100180 /* Send only a few RLC/MAC blocks */
Daniel Willmann510d7d32014-08-15 18:19:41 +0200181 fn = 0;
Jacob Erlbeck2493c662015-03-25 10:05:34 +0100182 block_nr = 0;
183 while (block_nr < 3) {
Daniel Willmann510d7d32014-08-15 18:19:41 +0200184 /* Request to send one block */
Jacob Erlbeck2493c662015-03-25 10:05:34 +0100185 send_rlc_block(bts, trx_no, ts_no, 0, &fn, &block_nr);
Daniel Willmann510d7d32014-08-15 18:19:41 +0200186 }
Jacob Erlbeck2493c662015-03-25 10:05:34 +0100187 OSMO_ASSERT(dl_tbf->have_data());
188 OSMO_ASSERT(dl_tbf->state_is(GPRS_RLCMAC_FLOW));
Daniel Willmann510d7d32014-08-15 18:19:41 +0200189
190 /* Queue a final ACK */
191 memset(rbb, 0, sizeof(rbb));
192 /* Receive a final ACK */
193 dl_tbf->rcvd_dl_ack(1, 1, rbb);
194
195 /* Clean up and ensure tbfs are in the correct state */
196 OSMO_ASSERT(dl_tbf->state_is(GPRS_RLCMAC_WAIT_RELEASE));
197 new_tbf = dl_tbf->new_tbf();
Jacob Erlbeck08fe76a2015-02-23 15:10:20 +0100198 check_tbf(new_tbf);
Daniel Willmann510d7d32014-08-15 18:19:41 +0200199 OSMO_ASSERT(new_tbf != dl_tbf);
200 OSMO_ASSERT(new_tbf->tfi() == 1);
Jacob Erlbeck08fe76a2015-02-23 15:10:20 +0100201 check_tbf(dl_tbf);
Daniel Willmann510d7d32014-08-15 18:19:41 +0200202 dl_tbf->dl_ass_state = GPRS_RLCMAC_DL_ASS_NONE;
Jacob Erlbeck5e9f40d2015-02-23 14:26:59 +0100203 if (test_mode == TEST_MODE_REVERSE_FREE) {
204 tbf_free(new_tbf);
Jacob Erlbeck08fe76a2015-02-23 15:10:20 +0100205 OSMO_ASSERT(dl_tbf->m_new_tbf != new_tbf);
206 check_tbf(dl_tbf);
Jacob Erlbeck5e9f40d2015-02-23 14:26:59 +0100207 tbf_free(dl_tbf);
208 } else {
209 tbf_free(dl_tbf);
210 OSMO_ASSERT(new_tbf->m_new_tbf != dl_tbf);
Jacob Erlbeck08fe76a2015-02-23 15:10:20 +0100211 check_tbf(new_tbf);
Jacob Erlbeck5e9f40d2015-02-23 14:26:59 +0100212 tbf_free(new_tbf);
213 }
Daniel Willmann510d7d32014-08-15 18:19:41 +0200214}
215
Daniel Willmann341689d2014-06-11 18:33:14 +0200216static const struct log_info_cat default_categories[] = {
217 {"DCSN1", "\033[1;31m", "Concrete Syntax Notation One (CSN1)", LOGL_INFO, 0},
218 {"DL1IF", "\033[1;32m", "GPRS PCU L1 interface (L1IF)", LOGL_DEBUG, 1},
219 {"DRLCMAC", "\033[0;33m", "GPRS RLC/MAC layer (RLCMAC)", LOGL_DEBUG, 1},
220 {"DRLCMACDATA", "\033[0;33m", "GPRS RLC/MAC layer Data (RLCMAC)", LOGL_DEBUG, 1},
221 {"DRLCMACDL", "\033[1;33m", "GPRS RLC/MAC layer Downlink (RLCMAC)", LOGL_DEBUG, 1},
222 {"DRLCMACUL", "\033[1;36m", "GPRS RLC/MAC layer Uplink (RLCMAC)", LOGL_DEBUG, 1},
223 {"DRLCMACSCHED", "\033[0;36m", "GPRS RLC/MAC layer Scheduling (RLCMAC)", LOGL_DEBUG, 1},
224 {"DRLCMACMEAS", "\033[1;31m", "GPRS RLC/MAC layer Measurements (RLCMAC)", LOGL_INFO, 1},
225 {"DBSSGP","\033[1;34m", "GPRS BSS Gateway Protocol (BSSGP)", LOGL_INFO , 1},
226 {"DPCU", "\033[1;35m", "GPRS Packet Control Unit (PCU)", LOGL_NOTICE, 1},
227};
228
229static int filter_fn(const struct log_context *ctx,
230 struct log_target *tar)
231{
232 return 1;
233}
234
235const struct log_info debug_log_info = {
236 filter_fn,
237 (struct log_info_cat*)default_categories,
238 ARRAY_SIZE(default_categories),
239};
240
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +0100241int main(int argc, char **argv)
242{
243 tall_pcu_ctx = talloc_named_const(NULL, 1, "moiji-mobile TbfTest context");
244 if (!tall_pcu_ctx)
245 abort();
246
247 msgb_set_talloc_ctx(tall_pcu_ctx);
Daniel Willmann341689d2014-06-11 18:33:14 +0200248 osmo_init_logging(&debug_log_info);
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +0100249 log_set_use_color(osmo_stderr_target, 0);
250 log_set_print_filename(osmo_stderr_target, 0);
251
252 test_tbf_tlli_update();
Jacob Erlbeck5e9f40d2015-02-23 14:26:59 +0100253 test_tbf_final_ack(TEST_MODE_STANDARD);
254 test_tbf_final_ack(TEST_MODE_REVERSE_FREE);
Holger Hans Peter Freytherb8098662013-10-30 14:50:17 +0100255 return EXIT_SUCCESS;
256}
257
258/*
259 * stubs that should not be reached
260 */
261extern "C" {
262void l1if_pdch_req() { abort(); }
263void l1if_connect_pdch() { abort(); }
264void l1if_close_pdch() { abort(); }
265void l1if_open_pdch() { abort(); }
266}