blob: d711ad89227a2cfc598b9c2c8d9e98e2b4a1bba0 [file] [log] [blame]
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +02001/* AllocTest.cpp
2 *
3 * Copyright (C) 2013 by Holger Hans Peter Freyther
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20#include "gprs_rlcmac.h"
21#include "gprs_debug.h"
22#include "tbf.h"
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +020023#include "bts.h"
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020024
25#include <string.h>
26#include <stdio.h>
27
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
35/* globals used by the code */
36void *tall_pcu_ctx;
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020037int16_t spoof_mnc = 0, spoof_mcc = 0;
38
Daniel Willmann48aa0b02014-07-16 18:54:10 +020039static gprs_rlcmac_tbf *tbf_alloc(struct gprs_rlcmac_bts *bts,
40 struct gprs_rlcmac_tbf *old_tbf, gprs_rlcmac_tbf_direction dir,
41 uint8_t tfi, uint8_t trx,
42 uint8_t ms_class, uint8_t single_slot)
43{
44 if (dir == GPRS_RLCMAC_UL_TBF)
45 return tbf_alloc_ul_tbf(bts, old_tbf, tfi, trx, ms_class, single_slot);
46 else
47 return tbf_alloc_dl_tbf(bts, old_tbf, tfi, trx, ms_class, single_slot);
48}
49
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020050static void test_alloc_a(gprs_rlcmac_tbf_direction dir, const int count)
51{
52 int tfi;
53 uint8_t used_trx;
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +020054 BTS the_bts;
55 struct gprs_rlcmac_bts *bts;
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020056 struct gprs_rlcmac_tbf *tbfs[33] = { 0, };
57
58 printf("Testing alloc_a direction(%d)\n", dir);
59
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +020060 bts = the_bts.bts_data();
61 bts->alloc_algorithm = alloc_algorithm_a;
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020062
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +020063 struct gprs_rlcmac_trx *trx = &bts->trx[0];
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +020064 trx->pdch[2].enable();
65 trx->pdch[3].enable();
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020066
67 /**
68 * Currently alloc_a will only allocate from the first
69 * PDCH and all possible usf's. We run out of usf's before
70 * we are out of tfi's. Observe this and make sure that at
71 * least this part is working okay.
72 */
73 for (int i = 0; i < count; ++i) {
74 struct gprs_rlcmac_tbf *tbf;
75
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +020076 tfi = the_bts.tfi_find_free(dir, &used_trx, 0);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020077 OSMO_ASSERT(tfi >= 0);
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +020078 tbfs[i] = tbf_alloc(bts, NULL, dir, tfi, used_trx, 0, 0);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020079 }
80
81 /* Now check that there are still some TFIs */
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +020082 tfi = the_bts.tfi_find_free(dir, &used_trx, 0);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020083 switch (dir) {
84 case GPRS_RLCMAC_UL_TBF:
85 OSMO_ASSERT(tfi >= 0);
86 break;
87 case GPRS_RLCMAC_DL_TBF:
88 OSMO_ASSERT(tfi < 0);
89 break;
90 }
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +020091 OSMO_ASSERT(!tbf_alloc(bts, NULL, dir, tfi, used_trx, 0, 0));
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020092
93 for (int i = 0; i < ARRAY_SIZE(tbfs); ++i)
94 if (tbfs[i])
95 tbf_free(tbfs[i]);
96
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +020097 tfi = the_bts.tfi_find_free(dir, &used_trx, 0);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020098 OSMO_ASSERT(tfi >= 0);
99
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200100 tbfs[tfi] = tbf_alloc(bts, NULL, dir, tfi, used_trx, 0, 0);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200101 OSMO_ASSERT(tbfs[tfi]);
102 tbf_free(tbfs[tfi]);
103}
104
105static void test_alloc_a()
106{
107 test_alloc_a(GPRS_RLCMAC_DL_TBF, 32);
108 test_alloc_a(GPRS_RLCMAC_UL_TBF, 7);
109}
110
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100111static void dump_assignment(struct gprs_rlcmac_tbf *tbf, const char *dir)
112{
113 for (int i = 0; i < ARRAY_SIZE(tbf->pdch); ++i)
114 if (tbf->pdch[i])
115 printf("PDCH[%d] is used for %s\n", i, dir);
116 printf("PDCH[%d] is control_ts for %s\n", tbf->control_ts, dir);
117 printf("PDCH[%d] is first common for %s\n", tbf->first_common_ts, dir);
118}
119
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100120static void test_alloc_b(int ms_class)
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100121{
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100122 printf("Going to test multislot assignment MS_CLASS=%d\n", ms_class);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100123 /*
124 * PDCH is on TS 6,7,8 and we start with a UL allocation and
125 * then follow two DL allocations (once single, once normal).
126 *
127 * Uplink assigned and still available..
128 */
129 {
130 BTS the_bts;
131 struct gprs_rlcmac_bts *bts;
132 struct gprs_rlcmac_trx *trx;
133 int tfi;
134 uint8_t ts_no, trx_no;
135
136 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
137
138 printf("Testing UL then DL assignment.\n");
139
140 bts = the_bts.bts_data();
141 bts->alloc_algorithm = alloc_algorithm_b;
142
143 trx = &bts->trx[0];
144 trx->pdch[5].enable();
145 trx->pdch[6].enable();
146 trx->pdch[7].enable();
147
148 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
149 OSMO_ASSERT(tfi >= 0);
Daniel Willmann48aa0b02014-07-16 18:54:10 +0200150 ul_tbf = tbf_alloc_ul_tbf(bts, NULL, tfi, trx_no, ms_class, 1);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100151 OSMO_ASSERT(ul_tbf);
152 dump_assignment(ul_tbf, "UL");
153
154 /* assume final ack has not been sent */
155 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
156 OSMO_ASSERT(tfi >= 0);
Daniel Willmann48aa0b02014-07-16 18:54:10 +0200157 dl_tbf = tbf_alloc_dl_tbf(bts, ul_tbf, tfi, trx_no, ms_class, 0);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100158 OSMO_ASSERT(dl_tbf);
159 dump_assignment(dl_tbf, "DL");
160
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100161 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
162
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100163 tbf_free(dl_tbf);
164 tbf_free(ul_tbf);
165 }
166
167 /**
168 * Test with the other order.. first DL and then UL
169 */
170 {
171 BTS the_bts;
172 struct gprs_rlcmac_bts *bts;
173 struct gprs_rlcmac_trx *trx;
174 int tfi;
175 uint8_t ts_no, trx_no;
176
Daniel Willmann351a5732014-08-07 12:57:44 +0200177 gprs_rlcmac_ul_tbf *ul_tbf;
178 gprs_rlcmac_dl_tbf *dl_tbf;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100179
180 printf("Testing DL then UL assignment followed by update\n");
181
182 bts = the_bts.bts_data();
183 bts->alloc_algorithm = alloc_algorithm_b;
184
185 trx = &bts->trx[0];
186 trx->pdch[5].enable();
187 trx->pdch[6].enable();
188 trx->pdch[7].enable();
189
190 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
191 OSMO_ASSERT(tfi >= 0);
Daniel Willmann48aa0b02014-07-16 18:54:10 +0200192 dl_tbf = tbf_alloc_dl_tbf(bts, NULL, tfi, trx_no, ms_class, 1);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100193 dl_tbf->m_tlli = 0x23;
194 dl_tbf->m_tlli_valid = true;
195 OSMO_ASSERT(dl_tbf);
196 dump_assignment(dl_tbf, "DL");
197
198 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
199 OSMO_ASSERT(tfi >= 0);
Daniel Willmann48aa0b02014-07-16 18:54:10 +0200200 ul_tbf = tbf_alloc_ul_tbf(bts, dl_tbf, tfi, trx_no, ms_class, 0);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100201 ul_tbf->m_tlli = 0x23;
202 ul_tbf->m_tlli_valid = true;
Daniel Willmann7e994e32014-08-07 15:49:21 +0200203 ul_tbf->m_contention_resolution_done = 1;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100204 OSMO_ASSERT(ul_tbf);
205 dump_assignment(ul_tbf, "UL");
206
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100207 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
208
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100209 /* now update the dl_tbf */
210 dl_tbf->update();
211 dump_assignment(dl_tbf, "DL");
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100212 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100213
214 tbf_free(dl_tbf);
215 tbf_free(ul_tbf);
216 }
217
218 /* Andreas osmocom-pcu example */
219 {
220 BTS the_bts;
221 struct gprs_rlcmac_bts *bts;
222 struct gprs_rlcmac_trx *trx;
223 int tfi;
224 uint8_t ts_no, trx_no;
225
226 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
227
228 printf("Testing jolly example\n");
229
230 bts = the_bts.bts_data();
231 bts->alloc_algorithm = alloc_algorithm_b;
232
233 trx = &bts->trx[0];
234 trx->pdch[1].enable();
235 trx->pdch[2].enable();
236 trx->pdch[3].enable();
237 trx->pdch[4].enable();
238
239 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
240 OSMO_ASSERT(tfi >= 0);
Daniel Willmann48aa0b02014-07-16 18:54:10 +0200241 ul_tbf = tbf_alloc_ul_tbf(bts, NULL, tfi, trx_no, ms_class, 0);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100242 OSMO_ASSERT(ul_tbf);
243 dump_assignment(ul_tbf, "UL");
244
245 /* assume final ack has not been sent */
246 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
247 OSMO_ASSERT(tfi >= 0);
Daniel Willmann48aa0b02014-07-16 18:54:10 +0200248 dl_tbf = tbf_alloc_dl_tbf(bts, ul_tbf, tfi, trx_no, ms_class, 0);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100249 OSMO_ASSERT(dl_tbf);
250 dump_assignment(dl_tbf, "DL");
251
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100252 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
253
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100254 tbf_free(dl_tbf);
255 tbf_free(ul_tbf);
256 }
257}
258
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100259#define ENABLE_PDCH(ts_no, enable_flag, trx) \
260 if (enable_flag) \
261 trx->pdch[ts_no].enable();
262
263static void test_alloc_b(bool ts0, bool ts1, bool ts2, bool ts3, bool ts4, bool ts5, bool ts6, bool ts7, int ms_class)
264{
265 /* we can test the allocation failures differently */
266 if (!ts0 && !ts1 && !ts2 && !ts3 && !ts4 && !ts5 && !ts6 && !ts7)
267 return;
268
269 printf("Mass test: TS0(%c%c%c%c%c%c%c%c)TS7 MS_Class=%d\n",
270 ts0 ? 'O' : 'x',
271 ts1 ? 'O' : 'x',
272 ts2 ? 'O' : 'x',
273 ts3 ? 'O' : 'x',
274 ts4 ? 'O' : 'x',
275 ts5 ? 'O' : 'x',
276 ts6 ? 'O' : 'x',
277 ts7 ? 'O' : 'x', ms_class);
278 fflush(stdout);
279
280 {
281 BTS the_bts;
282 struct gprs_rlcmac_bts *bts;
283 struct gprs_rlcmac_trx *trx;
284 int tfi;
285 uint8_t ts_no, trx_no;
286
287 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
288
289 bts = the_bts.bts_data();
290 bts->alloc_algorithm = alloc_algorithm_b;
291
292 trx = &bts->trx[0];
293 ENABLE_PDCH(0, ts0, trx);
294 ENABLE_PDCH(1, ts1, trx);
295 ENABLE_PDCH(2, ts2, trx);
296 ENABLE_PDCH(3, ts3, trx);
297 ENABLE_PDCH(4, ts4, trx);
298 ENABLE_PDCH(5, ts5, trx);
299 ENABLE_PDCH(6, ts6, trx);
300 ENABLE_PDCH(7, ts7, trx);
301
302 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
303
304 OSMO_ASSERT(tfi >= 0);
Daniel Willmann48aa0b02014-07-16 18:54:10 +0200305 ul_tbf = tbf_alloc_ul_tbf(bts, NULL, tfi, trx_no, ms_class, 1);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100306 OSMO_ASSERT(ul_tbf);
307
308 /* assume final ack has not been sent */
309 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
310 OSMO_ASSERT(tfi >= 0);
Daniel Willmann48aa0b02014-07-16 18:54:10 +0200311 dl_tbf = tbf_alloc_dl_tbf(bts, ul_tbf, tfi, trx_no, ms_class, 0);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100312 OSMO_ASSERT(dl_tbf);
313
314 /* verify that both are on the same ts */
315 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
316
317 tbf_free(dl_tbf);
318 tbf_free(ul_tbf);
319 }
320
321 /**
322 * Test with the other order.. first DL and then UL
323 */
324 {
325 BTS the_bts;
326 struct gprs_rlcmac_bts *bts;
327 struct gprs_rlcmac_trx *trx;
328 int tfi;
329 uint8_t ts_no, trx_no;
330
Daniel Willmann351a5732014-08-07 12:57:44 +0200331 gprs_rlcmac_ul_tbf *ul_tbf;
332 gprs_rlcmac_dl_tbf *dl_tbf;
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100333
334 bts = the_bts.bts_data();
335 bts->alloc_algorithm = alloc_algorithm_b;
336
337 trx = &bts->trx[0];
338 ENABLE_PDCH(0, ts0, trx);
339 ENABLE_PDCH(1, ts1, trx);
340 ENABLE_PDCH(2, ts2, trx);
341 ENABLE_PDCH(3, ts3, trx);
342 ENABLE_PDCH(4, ts4, trx);
343 ENABLE_PDCH(5, ts5, trx);
344 ENABLE_PDCH(6, ts6, trx);
345 ENABLE_PDCH(7, ts7, trx);
346
347 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
348 OSMO_ASSERT(tfi >= 0);
Daniel Willmann48aa0b02014-07-16 18:54:10 +0200349 dl_tbf = tbf_alloc_dl_tbf(bts, NULL, tfi, trx_no, ms_class, 1);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100350 OSMO_ASSERT(dl_tbf);
351 dl_tbf->m_tlli = 0x23;
352 dl_tbf->m_tlli_valid = true;
353
354 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
355 OSMO_ASSERT(tfi >= 0);
Daniel Willmann48aa0b02014-07-16 18:54:10 +0200356 ul_tbf = tbf_alloc_ul_tbf(bts, dl_tbf, tfi, trx_no, ms_class, 0);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100357 OSMO_ASSERT(ul_tbf);
358 ul_tbf->m_tlli = 0x23;
359 ul_tbf->m_tlli_valid = true;
Daniel Willmann7e994e32014-08-07 15:49:21 +0200360 ul_tbf->m_contention_resolution_done = 1;
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100361
362 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
363
364 /* now update the dl_tbf */
365 dl_tbf->update();
366 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
367
368 tbf_free(dl_tbf);
369 tbf_free(ul_tbf);
370 }
371}
372
373static void test_all_alloc_b()
374{
375 /* it is a bit crazy... */
376 for (uint8_t ts0 = 0; ts0 < 2; ++ts0)
377 for (uint8_t ts1 = 0; ts1 < 2; ++ts1)
378 for (uint8_t ts2 = 0; ts2 < 2; ++ts2)
379 for (uint8_t ts3 = 0; ts3 < 2; ++ts3)
380 for (uint8_t ts4 = 0; ts4 < 2; ++ts4)
381 for (uint8_t ts5 = 0; ts5 < 2; ++ts5)
382 for (uint8_t ts6 = 0; ts6 < 2; ++ts6)
383 for (uint8_t ts7 = 0; ts7 < 2; ++ts7)
384 for (int ms_class = 0; ms_class < 30; ++ms_class)
385 test_alloc_b(ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7, ms_class);
386}
387
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100388static void test_alloc_b()
389{
390 for (int i = 0; i < 30; ++i)
391 test_alloc_b(i);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100392
393 test_all_alloc_b();
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100394}
395
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200396int main(int argc, char **argv)
397{
398 tall_pcu_ctx = talloc_named_const(NULL, 1, "moiji-mobile AllocTest context");
399 if (!tall_pcu_ctx)
400 abort();
401
402 msgb_set_talloc_ctx(tall_pcu_ctx);
403 osmo_init_logging(&gprs_log_info);
404 log_set_use_color(osmo_stderr_target, 0);
405 log_set_print_filename(osmo_stderr_target, 0);
406
407 test_alloc_a();
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100408 test_alloc_b();
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200409 return EXIT_SUCCESS;
410}
411
412/*
413 * stubs that should not be reached
414 */
415extern "C" {
416void l1if_pdch_req() { abort(); }
417void l1if_connect_pdch() { abort(); }
418void l1if_close_pdch() { abort(); }
419void l1if_open_pdch() { abort(); }
420}