blob: dcc337125c7211e27c2234cdbf22af6799d6c3e9 [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) {
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +020074 tfi = the_bts.tfi_find_free(dir, &used_trx, 0);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020075 OSMO_ASSERT(tfi >= 0);
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +020076 tbfs[i] = tbf_alloc(bts, NULL, dir, tfi, used_trx, 0, 0);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020077 }
78
79 /* Now check that there are still some TFIs */
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +020080 tfi = the_bts.tfi_find_free(dir, &used_trx, 0);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020081 switch (dir) {
82 case GPRS_RLCMAC_UL_TBF:
83 OSMO_ASSERT(tfi >= 0);
84 break;
85 case GPRS_RLCMAC_DL_TBF:
86 OSMO_ASSERT(tfi < 0);
87 break;
88 }
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +020089 OSMO_ASSERT(!tbf_alloc(bts, NULL, dir, tfi, used_trx, 0, 0));
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020090
Jacob Erlbeck1f332942015-05-04 08:21:17 +020091 for (size_t i = 0; i < ARRAY_SIZE(tbfs); ++i)
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020092 if (tbfs[i])
93 tbf_free(tbfs[i]);
94
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +020095 tfi = the_bts.tfi_find_free(dir, &used_trx, 0);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020096 OSMO_ASSERT(tfi >= 0);
97
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +020098 tbfs[tfi] = tbf_alloc(bts, NULL, dir, tfi, used_trx, 0, 0);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020099 OSMO_ASSERT(tbfs[tfi]);
100 tbf_free(tbfs[tfi]);
101}
102
103static void test_alloc_a()
104{
105 test_alloc_a(GPRS_RLCMAC_DL_TBF, 32);
106 test_alloc_a(GPRS_RLCMAC_UL_TBF, 7);
107}
108
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100109static void dump_assignment(struct gprs_rlcmac_tbf *tbf, const char *dir)
110{
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200111 for (size_t i = 0; i < ARRAY_SIZE(tbf->pdch); ++i)
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100112 if (tbf->pdch[i])
113 printf("PDCH[%d] is used for %s\n", i, dir);
114 printf("PDCH[%d] is control_ts for %s\n", tbf->control_ts, dir);
115 printf("PDCH[%d] is first common for %s\n", tbf->first_common_ts, dir);
116}
117
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100118static void test_alloc_b(int ms_class)
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100119{
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100120 printf("Going to test multislot assignment MS_CLASS=%d\n", ms_class);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100121 /*
122 * PDCH is on TS 6,7,8 and we start with a UL allocation and
123 * then follow two DL allocations (once single, once normal).
124 *
125 * Uplink assigned and still available..
126 */
127 {
128 BTS the_bts;
129 struct gprs_rlcmac_bts *bts;
130 struct gprs_rlcmac_trx *trx;
131 int tfi;
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200132 uint8_t trx_no;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100133
134 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
135
136 printf("Testing UL then DL assignment.\n");
137
138 bts = the_bts.bts_data();
139 bts->alloc_algorithm = alloc_algorithm_b;
140
141 trx = &bts->trx[0];
142 trx->pdch[5].enable();
143 trx->pdch[6].enable();
144 trx->pdch[7].enable();
145
146 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
147 OSMO_ASSERT(tfi >= 0);
Daniel Willmann48aa0b02014-07-16 18:54:10 +0200148 ul_tbf = tbf_alloc_ul_tbf(bts, NULL, tfi, trx_no, ms_class, 1);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100149 OSMO_ASSERT(ul_tbf);
150 dump_assignment(ul_tbf, "UL");
151
152 /* assume final ack has not been sent */
153 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
154 OSMO_ASSERT(tfi >= 0);
Daniel Willmann48aa0b02014-07-16 18:54:10 +0200155 dl_tbf = tbf_alloc_dl_tbf(bts, ul_tbf, tfi, trx_no, ms_class, 0);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100156 OSMO_ASSERT(dl_tbf);
157 dump_assignment(dl_tbf, "DL");
158
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100159 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
160
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100161 tbf_free(dl_tbf);
162 tbf_free(ul_tbf);
163 }
164
165 /**
166 * Test with the other order.. first DL and then UL
167 */
168 {
169 BTS the_bts;
170 struct gprs_rlcmac_bts *bts;
171 struct gprs_rlcmac_trx *trx;
172 int tfi;
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200173 uint8_t trx_no;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100174
Daniel Willmann351a5732014-08-07 12:57:44 +0200175 gprs_rlcmac_ul_tbf *ul_tbf;
176 gprs_rlcmac_dl_tbf *dl_tbf;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100177
178 printf("Testing DL then UL assignment followed by update\n");
179
180 bts = the_bts.bts_data();
181 bts->alloc_algorithm = alloc_algorithm_b;
182
183 trx = &bts->trx[0];
184 trx->pdch[5].enable();
185 trx->pdch[6].enable();
186 trx->pdch[7].enable();
187
188 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
189 OSMO_ASSERT(tfi >= 0);
Daniel Willmann48aa0b02014-07-16 18:54:10 +0200190 dl_tbf = tbf_alloc_dl_tbf(bts, NULL, tfi, trx_no, ms_class, 1);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100191 dl_tbf->m_tlli = 0x23;
192 dl_tbf->m_tlli_valid = true;
193 OSMO_ASSERT(dl_tbf);
194 dump_assignment(dl_tbf, "DL");
195
196 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
197 OSMO_ASSERT(tfi >= 0);
Daniel Willmann48aa0b02014-07-16 18:54:10 +0200198 ul_tbf = tbf_alloc_ul_tbf(bts, dl_tbf, tfi, trx_no, ms_class, 0);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100199 ul_tbf->m_tlli = 0x23;
200 ul_tbf->m_tlli_valid = true;
Daniel Willmann7e994e32014-08-07 15:49:21 +0200201 ul_tbf->m_contention_resolution_done = 1;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100202 OSMO_ASSERT(ul_tbf);
203 dump_assignment(ul_tbf, "UL");
204
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100205 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
206
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100207 /* now update the dl_tbf */
208 dl_tbf->update();
209 dump_assignment(dl_tbf, "DL");
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100210 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100211
212 tbf_free(dl_tbf);
213 tbf_free(ul_tbf);
214 }
215
216 /* Andreas osmocom-pcu example */
217 {
218 BTS the_bts;
219 struct gprs_rlcmac_bts *bts;
220 struct gprs_rlcmac_trx *trx;
221 int tfi;
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200222 uint8_t trx_no;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100223
224 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
225
226 printf("Testing jolly example\n");
227
228 bts = the_bts.bts_data();
229 bts->alloc_algorithm = alloc_algorithm_b;
230
231 trx = &bts->trx[0];
232 trx->pdch[1].enable();
233 trx->pdch[2].enable();
234 trx->pdch[3].enable();
235 trx->pdch[4].enable();
236
237 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
238 OSMO_ASSERT(tfi >= 0);
Daniel Willmann48aa0b02014-07-16 18:54:10 +0200239 ul_tbf = tbf_alloc_ul_tbf(bts, NULL, tfi, trx_no, ms_class, 0);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100240 OSMO_ASSERT(ul_tbf);
241 dump_assignment(ul_tbf, "UL");
242
243 /* assume final ack has not been sent */
244 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
245 OSMO_ASSERT(tfi >= 0);
Daniel Willmann48aa0b02014-07-16 18:54:10 +0200246 dl_tbf = tbf_alloc_dl_tbf(bts, ul_tbf, tfi, trx_no, ms_class, 0);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100247 OSMO_ASSERT(dl_tbf);
248 dump_assignment(dl_tbf, "DL");
249
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100250 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
251
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100252 tbf_free(dl_tbf);
253 tbf_free(ul_tbf);
254 }
255}
256
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100257#define ENABLE_PDCH(ts_no, enable_flag, trx) \
258 if (enable_flag) \
259 trx->pdch[ts_no].enable();
260
261static void test_alloc_b(bool ts0, bool ts1, bool ts2, bool ts3, bool ts4, bool ts5, bool ts6, bool ts7, int ms_class)
262{
263 /* we can test the allocation failures differently */
264 if (!ts0 && !ts1 && !ts2 && !ts3 && !ts4 && !ts5 && !ts6 && !ts7)
265 return;
266
267 printf("Mass test: TS0(%c%c%c%c%c%c%c%c)TS7 MS_Class=%d\n",
268 ts0 ? 'O' : 'x',
269 ts1 ? 'O' : 'x',
270 ts2 ? 'O' : 'x',
271 ts3 ? 'O' : 'x',
272 ts4 ? 'O' : 'x',
273 ts5 ? 'O' : 'x',
274 ts6 ? 'O' : 'x',
275 ts7 ? 'O' : 'x', ms_class);
276 fflush(stdout);
277
278 {
279 BTS the_bts;
280 struct gprs_rlcmac_bts *bts;
281 struct gprs_rlcmac_trx *trx;
282 int tfi;
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200283 uint8_t trx_no;
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100284
285 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
286
287 bts = the_bts.bts_data();
288 bts->alloc_algorithm = alloc_algorithm_b;
289
290 trx = &bts->trx[0];
291 ENABLE_PDCH(0, ts0, trx);
292 ENABLE_PDCH(1, ts1, trx);
293 ENABLE_PDCH(2, ts2, trx);
294 ENABLE_PDCH(3, ts3, trx);
295 ENABLE_PDCH(4, ts4, trx);
296 ENABLE_PDCH(5, ts5, trx);
297 ENABLE_PDCH(6, ts6, trx);
298 ENABLE_PDCH(7, ts7, trx);
299
300 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
301
302 OSMO_ASSERT(tfi >= 0);
Daniel Willmann48aa0b02014-07-16 18:54:10 +0200303 ul_tbf = tbf_alloc_ul_tbf(bts, NULL, tfi, trx_no, ms_class, 1);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100304 OSMO_ASSERT(ul_tbf);
305
306 /* assume final ack has not been sent */
307 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
308 OSMO_ASSERT(tfi >= 0);
Daniel Willmann48aa0b02014-07-16 18:54:10 +0200309 dl_tbf = tbf_alloc_dl_tbf(bts, ul_tbf, tfi, trx_no, ms_class, 0);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100310 OSMO_ASSERT(dl_tbf);
311
312 /* verify that both are on the same ts */
313 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
314
315 tbf_free(dl_tbf);
316 tbf_free(ul_tbf);
317 }
318
319 /**
320 * Test with the other order.. first DL and then UL
321 */
322 {
323 BTS the_bts;
324 struct gprs_rlcmac_bts *bts;
325 struct gprs_rlcmac_trx *trx;
326 int tfi;
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200327 uint8_t trx_no;
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100328
Daniel Willmann351a5732014-08-07 12:57:44 +0200329 gprs_rlcmac_ul_tbf *ul_tbf;
330 gprs_rlcmac_dl_tbf *dl_tbf;
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100331
332 bts = the_bts.bts_data();
333 bts->alloc_algorithm = alloc_algorithm_b;
334
335 trx = &bts->trx[0];
336 ENABLE_PDCH(0, ts0, trx);
337 ENABLE_PDCH(1, ts1, trx);
338 ENABLE_PDCH(2, ts2, trx);
339 ENABLE_PDCH(3, ts3, trx);
340 ENABLE_PDCH(4, ts4, trx);
341 ENABLE_PDCH(5, ts5, trx);
342 ENABLE_PDCH(6, ts6, trx);
343 ENABLE_PDCH(7, ts7, trx);
344
345 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
346 OSMO_ASSERT(tfi >= 0);
Daniel Willmann48aa0b02014-07-16 18:54:10 +0200347 dl_tbf = tbf_alloc_dl_tbf(bts, NULL, tfi, trx_no, ms_class, 1);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100348 OSMO_ASSERT(dl_tbf);
349 dl_tbf->m_tlli = 0x23;
350 dl_tbf->m_tlli_valid = true;
351
352 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
353 OSMO_ASSERT(tfi >= 0);
Daniel Willmann48aa0b02014-07-16 18:54:10 +0200354 ul_tbf = tbf_alloc_ul_tbf(bts, dl_tbf, tfi, trx_no, ms_class, 0);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100355 OSMO_ASSERT(ul_tbf);
356 ul_tbf->m_tlli = 0x23;
357 ul_tbf->m_tlli_valid = true;
Daniel Willmann7e994e32014-08-07 15:49:21 +0200358 ul_tbf->m_contention_resolution_done = 1;
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100359
360 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
361
362 /* now update the dl_tbf */
363 dl_tbf->update();
364 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
365
366 tbf_free(dl_tbf);
367 tbf_free(ul_tbf);
368 }
369}
370
371static void test_all_alloc_b()
372{
373 /* it is a bit crazy... */
374 for (uint8_t ts0 = 0; ts0 < 2; ++ts0)
375 for (uint8_t ts1 = 0; ts1 < 2; ++ts1)
376 for (uint8_t ts2 = 0; ts2 < 2; ++ts2)
377 for (uint8_t ts3 = 0; ts3 < 2; ++ts3)
378 for (uint8_t ts4 = 0; ts4 < 2; ++ts4)
379 for (uint8_t ts5 = 0; ts5 < 2; ++ts5)
380 for (uint8_t ts6 = 0; ts6 < 2; ++ts6)
381 for (uint8_t ts7 = 0; ts7 < 2; ++ts7)
382 for (int ms_class = 0; ms_class < 30; ++ms_class)
383 test_alloc_b(ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7, ms_class);
384}
385
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100386static void test_alloc_b()
387{
388 for (int i = 0; i < 30; ++i)
389 test_alloc_b(i);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100390
391 test_all_alloc_b();
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100392}
393
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200394int main(int argc, char **argv)
395{
396 tall_pcu_ctx = talloc_named_const(NULL, 1, "moiji-mobile AllocTest context");
397 if (!tall_pcu_ctx)
398 abort();
399
400 msgb_set_talloc_ctx(tall_pcu_ctx);
401 osmo_init_logging(&gprs_log_info);
402 log_set_use_color(osmo_stderr_target, 0);
403 log_set_print_filename(osmo_stderr_target, 0);
404
405 test_alloc_a();
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100406 test_alloc_b();
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200407 return EXIT_SUCCESS;
408}
409
410/*
411 * stubs that should not be reached
412 */
413extern "C" {
414void l1if_pdch_req() { abort(); }
415void l1if_connect_pdch() { abort(); }
416void l1if_close_pdch() { abort(); }
417void l1if_open_pdch() { abort(); }
418}