blob: b804aab213175b8fb5cef406b2e750e3a924dc63 [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,
Jacob Erlbecke2e004e2015-06-18 17:16:26 +020040 GprsMs *ms, gprs_rlcmac_tbf_direction dir,
Daniel Willmann48aa0b02014-07-16 18:54:10 +020041 uint8_t tfi, uint8_t trx,
42 uint8_t ms_class, uint8_t single_slot)
43{
44 if (dir == GPRS_RLCMAC_UL_TBF)
Jacob Erlbecke2e004e2015-06-18 17:16:26 +020045 return tbf_alloc_ul_tbf(bts, ms, tfi, trx, ms_class, single_slot);
Daniel Willmann48aa0b02014-07-16 18:54:10 +020046 else
Jacob Erlbecke2e004e2015-06-18 17:16:26 +020047 return tbf_alloc_dl_tbf(bts, ms, tfi, trx, ms_class, single_slot);
Daniel Willmann48aa0b02014-07-16 18:54:10 +020048}
49
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +020050static void test_alloc_a(gprs_rlcmac_tbf_direction dir,
51 uint8_t slots, const int count)
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020052{
53 int tfi;
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +020054 int i;
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020055 uint8_t used_trx;
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +020056 BTS the_bts;
57 struct gprs_rlcmac_bts *bts;
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020058 struct gprs_rlcmac_tbf *tbfs[33] = { 0, };
59
60 printf("Testing alloc_a direction(%d)\n", dir);
61
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +020062 bts = the_bts.bts_data();
63 bts->alloc_algorithm = alloc_algorithm_a;
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020064
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +020065 struct gprs_rlcmac_trx *trx = &bts->trx[0];
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +020066 for (i = 0; i < 8; i += 1)
67 if (slots & (1 << i))
68 trx->pdch[i].enable();
69
70 OSMO_ASSERT(count >= 0 && count <= (int)ARRAY_SIZE(tbfs));
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020071
72 /**
73 * Currently alloc_a will only allocate from the first
74 * PDCH and all possible usf's. We run out of usf's before
75 * we are out of tfi's. Observe this and make sure that at
76 * least this part is working okay.
77 */
78 for (int i = 0; i < count; ++i) {
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +020079 tfi = the_bts.tfi_find_free(dir, &used_trx, 0);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020080 OSMO_ASSERT(tfi >= 0);
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +020081 tbfs[i] = tbf_alloc(bts, NULL, dir, tfi, used_trx, 0, 0);
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +020082 OSMO_ASSERT(tbfs[i] != NULL);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020083 }
84
85 /* Now check that there are still some TFIs */
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +020086 tfi = the_bts.tfi_find_free(dir, &used_trx, 0);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020087 switch (dir) {
88 case GPRS_RLCMAC_UL_TBF:
89 OSMO_ASSERT(tfi >= 0);
90 break;
91 case GPRS_RLCMAC_DL_TBF:
92 OSMO_ASSERT(tfi < 0);
93 break;
94 }
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +020095 OSMO_ASSERT(!tbf_alloc(bts, NULL, dir, tfi, used_trx, 0, 0));
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020096
Jacob Erlbeck1f332942015-05-04 08:21:17 +020097 for (size_t i = 0; i < ARRAY_SIZE(tbfs); ++i)
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020098 if (tbfs[i])
99 tbf_free(tbfs[i]);
100
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200101 tfi = the_bts.tfi_find_free(dir, &used_trx, 0);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200102 OSMO_ASSERT(tfi >= 0);
103
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200104 tbfs[tfi] = tbf_alloc(bts, NULL, dir, tfi, used_trx, 0, 0);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200105 OSMO_ASSERT(tbfs[tfi]);
106 tbf_free(tbfs[tfi]);
107}
108
109static void test_alloc_a()
110{
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200111 /* slots 2 - 3 */
112 test_alloc_a(GPRS_RLCMAC_DL_TBF, 0x0c, 32);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200113 test_alloc_a(GPRS_RLCMAC_UL_TBF, 0x0c, 14);
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200114
115 /* slots 1 - 5 */
116 test_alloc_a(GPRS_RLCMAC_DL_TBF, 0x1e, 32);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200117 test_alloc_a(GPRS_RLCMAC_UL_TBF, 0x1e, 28);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200118}
119
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100120static void dump_assignment(struct gprs_rlcmac_tbf *tbf, const char *dir)
121{
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200122 for (size_t i = 0; i < ARRAY_SIZE(tbf->pdch); ++i)
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100123 if (tbf->pdch[i])
124 printf("PDCH[%d] is used for %s\n", i, dir);
125 printf("PDCH[%d] is control_ts for %s\n", tbf->control_ts, dir);
126 printf("PDCH[%d] is first common for %s\n", tbf->first_common_ts, dir);
127}
128
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100129static void test_alloc_b(int ms_class)
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100130{
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100131 printf("Going to test multislot assignment MS_CLASS=%d\n", ms_class);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100132 /*
133 * PDCH is on TS 6,7,8 and we start with a UL allocation and
134 * then follow two DL allocations (once single, once normal).
135 *
136 * Uplink assigned and still available..
137 */
138 {
139 BTS the_bts;
140 struct gprs_rlcmac_bts *bts;
141 struct gprs_rlcmac_trx *trx;
142 int tfi;
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200143 uint8_t trx_no;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100144
145 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
146
147 printf("Testing UL then DL assignment.\n");
148
149 bts = the_bts.bts_data();
150 bts->alloc_algorithm = alloc_algorithm_b;
151
152 trx = &bts->trx[0];
153 trx->pdch[5].enable();
154 trx->pdch[6].enable();
155 trx->pdch[7].enable();
156
157 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
158 OSMO_ASSERT(tfi >= 0);
Daniel Willmann48aa0b02014-07-16 18:54:10 +0200159 ul_tbf = tbf_alloc_ul_tbf(bts, NULL, tfi, trx_no, ms_class, 1);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100160 OSMO_ASSERT(ul_tbf);
161 dump_assignment(ul_tbf, "UL");
162
163 /* assume final ack has not been sent */
164 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
165 OSMO_ASSERT(tfi >= 0);
Jacob Erlbecke2e004e2015-06-18 17:16:26 +0200166 dl_tbf = tbf_alloc_dl_tbf(bts, ul_tbf->ms(), tfi, trx_no, ms_class, 0);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100167 OSMO_ASSERT(dl_tbf);
168 dump_assignment(dl_tbf, "DL");
169
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100170 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
171
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100172 tbf_free(dl_tbf);
173 tbf_free(ul_tbf);
174 }
175
176 /**
177 * Test with the other order.. first DL and then UL
178 */
179 {
180 BTS the_bts;
181 struct gprs_rlcmac_bts *bts;
182 struct gprs_rlcmac_trx *trx;
183 int tfi;
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200184 uint8_t trx_no;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100185
Daniel Willmann351a5732014-08-07 12:57:44 +0200186 gprs_rlcmac_ul_tbf *ul_tbf;
187 gprs_rlcmac_dl_tbf *dl_tbf;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100188
189 printf("Testing DL then UL assignment followed by update\n");
190
191 bts = the_bts.bts_data();
192 bts->alloc_algorithm = alloc_algorithm_b;
193
194 trx = &bts->trx[0];
195 trx->pdch[5].enable();
196 trx->pdch[6].enable();
197 trx->pdch[7].enable();
198
199 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
200 OSMO_ASSERT(tfi >= 0);
Daniel Willmann48aa0b02014-07-16 18:54:10 +0200201 dl_tbf = tbf_alloc_dl_tbf(bts, NULL, tfi, trx_no, ms_class, 1);
Jacob Erlbeck767193e2015-05-20 12:06:46 +0200202 dl_tbf->update_ms(0x23, GPRS_RLCMAC_DL_TBF);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100203 OSMO_ASSERT(dl_tbf);
204 dump_assignment(dl_tbf, "DL");
205
206 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
207 OSMO_ASSERT(tfi >= 0);
Jacob Erlbecke2e004e2015-06-18 17:16:26 +0200208 ul_tbf = tbf_alloc_ul_tbf(bts, dl_tbf->ms(), tfi, trx_no, ms_class, 0);
Jacob Erlbeck767193e2015-05-20 12:06:46 +0200209 ul_tbf->update_ms(0x23, GPRS_RLCMAC_UL_TBF);
Daniel Willmann7e994e32014-08-07 15:49:21 +0200210 ul_tbf->m_contention_resolution_done = 1;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100211 OSMO_ASSERT(ul_tbf);
212 dump_assignment(ul_tbf, "UL");
213
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100214 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
215
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100216 /* now update the dl_tbf */
217 dl_tbf->update();
218 dump_assignment(dl_tbf, "DL");
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100219 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100220
221 tbf_free(dl_tbf);
222 tbf_free(ul_tbf);
223 }
224
225 /* Andreas osmocom-pcu example */
226 {
227 BTS the_bts;
228 struct gprs_rlcmac_bts *bts;
229 struct gprs_rlcmac_trx *trx;
230 int tfi;
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200231 uint8_t trx_no;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100232
233 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
234
235 printf("Testing jolly example\n");
236
237 bts = the_bts.bts_data();
238 bts->alloc_algorithm = alloc_algorithm_b;
239
240 trx = &bts->trx[0];
241 trx->pdch[1].enable();
242 trx->pdch[2].enable();
243 trx->pdch[3].enable();
244 trx->pdch[4].enable();
245
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 ul_tbf = tbf_alloc_ul_tbf(bts, NULL, tfi, trx_no, ms_class, 0);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100249 OSMO_ASSERT(ul_tbf);
250 dump_assignment(ul_tbf, "UL");
251
252 /* assume final ack has not been sent */
253 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
254 OSMO_ASSERT(tfi >= 0);
Jacob Erlbecke2e004e2015-06-18 17:16:26 +0200255 dl_tbf = tbf_alloc_dl_tbf(bts, ul_tbf->ms(), tfi, trx_no, ms_class, 0);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100256 OSMO_ASSERT(dl_tbf);
257 dump_assignment(dl_tbf, "DL");
258
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100259 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
260
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100261 tbf_free(dl_tbf);
262 tbf_free(ul_tbf);
263 }
264}
265
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100266#define ENABLE_PDCH(ts_no, enable_flag, trx) \
267 if (enable_flag) \
268 trx->pdch[ts_no].enable();
269
270static void test_alloc_b(bool ts0, bool ts1, bool ts2, bool ts3, bool ts4, bool ts5, bool ts6, bool ts7, int ms_class)
271{
272 /* we can test the allocation failures differently */
273 if (!ts0 && !ts1 && !ts2 && !ts3 && !ts4 && !ts5 && !ts6 && !ts7)
274 return;
275
276 printf("Mass test: TS0(%c%c%c%c%c%c%c%c)TS7 MS_Class=%d\n",
277 ts0 ? 'O' : 'x',
278 ts1 ? 'O' : 'x',
279 ts2 ? 'O' : 'x',
280 ts3 ? 'O' : 'x',
281 ts4 ? 'O' : 'x',
282 ts5 ? 'O' : 'x',
283 ts6 ? 'O' : 'x',
284 ts7 ? 'O' : 'x', ms_class);
285 fflush(stdout);
286
287 {
288 BTS the_bts;
289 struct gprs_rlcmac_bts *bts;
290 struct gprs_rlcmac_trx *trx;
291 int tfi;
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200292 uint8_t trx_no;
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100293
294 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
295
296 bts = the_bts.bts_data();
297 bts->alloc_algorithm = alloc_algorithm_b;
298
299 trx = &bts->trx[0];
300 ENABLE_PDCH(0, ts0, trx);
301 ENABLE_PDCH(1, ts1, trx);
302 ENABLE_PDCH(2, ts2, trx);
303 ENABLE_PDCH(3, ts3, trx);
304 ENABLE_PDCH(4, ts4, trx);
305 ENABLE_PDCH(5, ts5, trx);
306 ENABLE_PDCH(6, ts6, trx);
307 ENABLE_PDCH(7, ts7, trx);
308
309 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
310
311 OSMO_ASSERT(tfi >= 0);
Daniel Willmann48aa0b02014-07-16 18:54:10 +0200312 ul_tbf = tbf_alloc_ul_tbf(bts, NULL, tfi, trx_no, ms_class, 1);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100313 OSMO_ASSERT(ul_tbf);
314
315 /* assume final ack has not been sent */
316 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
317 OSMO_ASSERT(tfi >= 0);
Jacob Erlbecke2e004e2015-06-18 17:16:26 +0200318 dl_tbf = tbf_alloc_dl_tbf(bts, ul_tbf->ms(), tfi, trx_no, ms_class, 0);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100319 OSMO_ASSERT(dl_tbf);
320
321 /* verify that both are on the same ts */
322 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
323
324 tbf_free(dl_tbf);
325 tbf_free(ul_tbf);
326 }
327
328 /**
329 * Test with the other order.. first DL and then UL
330 */
331 {
332 BTS the_bts;
333 struct gprs_rlcmac_bts *bts;
334 struct gprs_rlcmac_trx *trx;
335 int tfi;
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200336 uint8_t trx_no;
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100337
Daniel Willmann351a5732014-08-07 12:57:44 +0200338 gprs_rlcmac_ul_tbf *ul_tbf;
339 gprs_rlcmac_dl_tbf *dl_tbf;
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100340
341 bts = the_bts.bts_data();
342 bts->alloc_algorithm = alloc_algorithm_b;
343
344 trx = &bts->trx[0];
345 ENABLE_PDCH(0, ts0, trx);
346 ENABLE_PDCH(1, ts1, trx);
347 ENABLE_PDCH(2, ts2, trx);
348 ENABLE_PDCH(3, ts3, trx);
349 ENABLE_PDCH(4, ts4, trx);
350 ENABLE_PDCH(5, ts5, trx);
351 ENABLE_PDCH(6, ts6, trx);
352 ENABLE_PDCH(7, ts7, trx);
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 dl_tbf = tbf_alloc_dl_tbf(bts, NULL, tfi, trx_no, ms_class, 1);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100357 OSMO_ASSERT(dl_tbf);
Jacob Erlbeck767193e2015-05-20 12:06:46 +0200358 dl_tbf->update_ms(0x23, GPRS_RLCMAC_DL_TBF);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100359
360 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
361 OSMO_ASSERT(tfi >= 0);
Jacob Erlbecke2e004e2015-06-18 17:16:26 +0200362 ul_tbf = tbf_alloc_ul_tbf(bts, dl_tbf->ms(), tfi, trx_no, ms_class, 0);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100363 OSMO_ASSERT(ul_tbf);
Jacob Erlbeck767193e2015-05-20 12:06:46 +0200364 ul_tbf->update_ms(0x23, GPRS_RLCMAC_UL_TBF);
Daniel Willmann7e994e32014-08-07 15:49:21 +0200365 ul_tbf->m_contention_resolution_done = 1;
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100366
367 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
368
369 /* now update the dl_tbf */
370 dl_tbf->update();
371 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
372
Jacob Erlbeckbefc7602015-06-02 12:33:30 +0200373 OSMO_ASSERT(ul_tbf->ms_class() == ms_class);
374 OSMO_ASSERT(dl_tbf->ms_class() == ms_class);
375
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100376 tbf_free(dl_tbf);
377 tbf_free(ul_tbf);
378 }
379}
380
381static void test_all_alloc_b()
382{
383 /* it is a bit crazy... */
384 for (uint8_t ts0 = 0; ts0 < 2; ++ts0)
385 for (uint8_t ts1 = 0; ts1 < 2; ++ts1)
386 for (uint8_t ts2 = 0; ts2 < 2; ++ts2)
387 for (uint8_t ts3 = 0; ts3 < 2; ++ts3)
388 for (uint8_t ts4 = 0; ts4 < 2; ++ts4)
389 for (uint8_t ts5 = 0; ts5 < 2; ++ts5)
390 for (uint8_t ts6 = 0; ts6 < 2; ++ts6)
391 for (uint8_t ts7 = 0; ts7 < 2; ++ts7)
392 for (int ms_class = 0; ms_class < 30; ++ms_class)
393 test_alloc_b(ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7, ms_class);
394}
395
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100396static void test_alloc_b()
397{
398 for (int i = 0; i < 30; ++i)
399 test_alloc_b(i);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100400
401 test_all_alloc_b();
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100402}
403
Jacob Erlbecke5655642015-06-29 12:19:52 +0200404typedef int (*algo_t)(struct gprs_rlcmac_bts *bts,
405 struct GprsMs *ms,
406 struct gprs_rlcmac_tbf *tbf, uint32_t cust, uint8_t single);
407
408static char get_dir_char(uint8_t mask, uint8_t tx, uint8_t rx)
409{
410 return (mask & tx & rx) ? 'C' :
411 (mask & tx) ? 'U' :
412 (mask & rx) ? 'D' :
413 '.';
414}
415
416enum test_mode {
417 TEST_MODE_UL_ONLY,
418 TEST_MODE_DL_ONLY,
419 TEST_MODE_UL_AND_DL,
420 TEST_MODE_DL_AND_UL,
421 TEST_MODE_DL_AFTER_UL,
422 TEST_MODE_UL_AFTER_DL,
423};
424
425static GprsMs *alloc_tbfs(BTS *the_bts, GprsMs *ms, unsigned ms_class,
426 enum test_mode mode)
427{
428 struct gprs_rlcmac_bts *bts;
429 int tfi;
430 uint8_t trx_no;
431
432 bts = the_bts->bts_data();
433
434 gprs_rlcmac_tbf *tbf = NULL;
435
436 /* Allocate what is needed first */
437 switch (mode) {
438 case TEST_MODE_UL_ONLY:
439 case TEST_MODE_DL_AFTER_UL:
440 case TEST_MODE_UL_AND_DL:
441 tfi = the_bts->tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
442 if (tfi < 0)
443 return NULL;
444 tbf = tbf_alloc_ul_tbf(bts, ms, tfi, trx_no, ms_class, 0);
445 if (tbf == NULL)
446 return NULL;
447 break;
448 case TEST_MODE_DL_ONLY:
449 case TEST_MODE_UL_AFTER_DL:
450 case TEST_MODE_DL_AND_UL:
451 tfi = the_bts->tfi_find_free(GPRS_RLCMAC_DL_TBF, &trx_no, -1);
452 tbf = tbf_alloc_dl_tbf(bts, ms, tfi, trx_no, ms_class, 0);
453 if (tbf == NULL)
454 return NULL;
455 }
456
457 OSMO_ASSERT(tbf);
458 OSMO_ASSERT(tbf->ms());
459 OSMO_ASSERT(ms == NULL || ms == tbf->ms());
460 ms = tbf->ms();
461
462 GprsMs::Guard guard(ms);
463
464 /* Optionally delete the TBF */
465 switch (mode) {
466 case TEST_MODE_DL_AFTER_UL:
467 case TEST_MODE_UL_AFTER_DL:
468 tbf_free(tbf);
469 break;
470
471 default:
472 break;
473 }
474
475 /* Continue with what is needed next */
476 switch (mode) {
477 case TEST_MODE_UL_ONLY:
478 case TEST_MODE_DL_ONLY:
479 /* We are done */
480 return ms;
481
482 case TEST_MODE_DL_AFTER_UL:
483 case TEST_MODE_UL_AND_DL:
484 return alloc_tbfs(the_bts, ms, ms_class, TEST_MODE_DL_ONLY);
485
486 case TEST_MODE_UL_AFTER_DL:
487 case TEST_MODE_DL_AND_UL:
488 return alloc_tbfs(the_bts, ms, ms_class, TEST_MODE_UL_ONLY);
489 }
490
491 return NULL;
492}
493
494static void test_successive_allocation(algo_t algo, unsigned min_class,
495 unsigned max_class, enum test_mode mode,
496 unsigned expect_num, const char *text)
497{
498 BTS the_bts;
499 struct gprs_rlcmac_bts *bts;
500 struct gprs_rlcmac_trx *trx;
501 unsigned counter;
502 unsigned ms_class = min_class;
503
504 printf("Going to test assignment with many TBF, %s\n", text);
505
506 bts = the_bts.bts_data();
507 bts->alloc_algorithm = algo;
508
509 trx = &bts->trx[0];
510 trx->pdch[3].enable();
511 trx->pdch[4].enable();
512 trx->pdch[5].enable();
513 trx->pdch[6].enable();
514 trx->pdch[7].enable();
515
516 for (counter = 0; 1; counter += 1) {
517 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
518 uint8_t ul_slots = 0;
519 uint8_t dl_slots = 0;
520 unsigned i;
521 int tfi = -1;
522 GprsMs *ms;
523
524 ms = alloc_tbfs(&the_bts, NULL, ms_class, mode);
525 if (!ms)
526 break;
527
528 ul_tbf = ms->ul_tbf();
529 dl_tbf = ms->dl_tbf();
530
531 if (ul_tbf) {
532 ul_slots = 1 << ul_tbf->first_common_ts;
533 tfi = ul_tbf->tfi();
534 } else if (dl_tbf) {
535 ul_slots = 1 << dl_tbf->first_common_ts;
536 tfi = dl_tbf->tfi();
537 }
538
539 for (i = 0; dl_tbf && i < ARRAY_SIZE(dl_tbf->pdch); i += 1)
540 if (dl_tbf->pdch[i])
541 dl_slots |= 1 << i;
542
543 printf(" TBF[%d] class %d reserves %c%c%c%c%c%c%c%c\n",
544 tfi, ms_class,
545 get_dir_char(0x01, ul_slots, dl_slots),
546 get_dir_char(0x02, ul_slots, dl_slots),
547 get_dir_char(0x04, ul_slots, dl_slots),
548 get_dir_char(0x08, ul_slots, dl_slots),
549 get_dir_char(0x10, ul_slots, dl_slots),
550 get_dir_char(0x20, ul_slots, dl_slots),
551 get_dir_char(0x40, ul_slots, dl_slots),
552 get_dir_char(0x80, ul_slots, dl_slots));
553
554 ms_class += 1;
555 if (ms_class > max_class)
556 ms_class = min_class;
557 }
558
559 printf(" Successfully allocated %d UL TBFs\n", counter);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200560 OSMO_ASSERT(counter == expect_num);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200561}
562
563static void test_successive_allocation()
564{
565 test_successive_allocation(alloc_algorithm_a, 1, 1, TEST_MODE_UL_AND_DL,
Jacob Erlbeckec478752015-06-19 16:35:38 +0200566 32, "algorithm A (UL and DL)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200567 test_successive_allocation(alloc_algorithm_b, 10, 10, TEST_MODE_UL_AND_DL,
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200568 32, "algorithm B class 10 (UL and DL)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200569 test_successive_allocation(alloc_algorithm_b, 12, 12, TEST_MODE_UL_AND_DL,
Jacob Erlbecked46afd2015-07-01 12:19:40 +0200570 32, "algorithm B class 12 (UL and DL)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200571 test_successive_allocation(alloc_algorithm_b, 1, 12, TEST_MODE_UL_AND_DL,
Jacob Erlbecked46afd2015-07-01 12:19:40 +0200572 32, "algorithm B class 1-12 (UL and DL)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200573 test_successive_allocation(alloc_algorithm_b, 1, 29, TEST_MODE_UL_AND_DL,
Jacob Erlbecked46afd2015-07-01 12:19:40 +0200574 31, "algorithm B class 1-29 (UL and DL)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200575
576 test_successive_allocation(alloc_algorithm_a, 1, 1, TEST_MODE_DL_AND_UL,
Jacob Erlbeckec478752015-06-19 16:35:38 +0200577 32, "algorithm A (DL and UL)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200578 test_successive_allocation(alloc_algorithm_b, 10, 10, TEST_MODE_DL_AND_UL,
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200579 15, "algorithm B class 10 (DL and UL)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200580
581 test_successive_allocation(alloc_algorithm_a, 1, 1, TEST_MODE_DL_AFTER_UL,
Jacob Erlbeckec478752015-06-19 16:35:38 +0200582 32, "algorithm A (DL after UL)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200583 test_successive_allocation(alloc_algorithm_b, 10, 10, TEST_MODE_DL_AFTER_UL,
Jacob Erlbeckec478752015-06-19 16:35:38 +0200584 32, "algorithm B class 10 (DL after UL)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200585
586 test_successive_allocation(alloc_algorithm_a, 1, 1, TEST_MODE_UL_AFTER_DL,
Jacob Erlbeckec478752015-06-19 16:35:38 +0200587 32, "algorithm A (UL after DL)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200588 test_successive_allocation(alloc_algorithm_b, 10, 10, TEST_MODE_UL_AFTER_DL,
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200589 32, "algorithm B class 10 (UL after DL)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200590
591 test_successive_allocation(alloc_algorithm_a, 1, 1, TEST_MODE_UL_ONLY,
Jacob Erlbeckec478752015-06-19 16:35:38 +0200592 32, "algorithm A (UL only)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200593 test_successive_allocation(alloc_algorithm_b, 10, 10, TEST_MODE_UL_ONLY,
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200594 32, "algorithm B class 10 (UL only)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200595
596 test_successive_allocation(alloc_algorithm_a, 1, 1, TEST_MODE_DL_ONLY,
Jacob Erlbeckec478752015-06-19 16:35:38 +0200597 32, "algorithm A (DL ONLY)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200598 test_successive_allocation(alloc_algorithm_b, 10, 10, TEST_MODE_DL_ONLY,
Jacob Erlbeckec478752015-06-19 16:35:38 +0200599 32, "algorithm B class 10 (DL ONLY)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200600}
601
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200602int main(int argc, char **argv)
603{
604 tall_pcu_ctx = talloc_named_const(NULL, 1, "moiji-mobile AllocTest context");
605 if (!tall_pcu_ctx)
606 abort();
607
608 msgb_set_talloc_ctx(tall_pcu_ctx);
609 osmo_init_logging(&gprs_log_info);
610 log_set_use_color(osmo_stderr_target, 0);
611 log_set_print_filename(osmo_stderr_target, 0);
Jacob Erlbeck9ec49e22015-06-29 13:00:20 +0200612 if (getenv("LOGL_DEBUG"))
613 log_set_log_level(osmo_stderr_target, LOGL_DEBUG);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200614
615 test_alloc_a();
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100616 test_alloc_b();
Jacob Erlbecke5655642015-06-29 12:19:52 +0200617 test_successive_allocation();
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200618 return EXIT_SUCCESS;
619}
620
621/*
622 * stubs that should not be reached
623 */
624extern "C" {
625void l1if_pdch_req() { abort(); }
626void l1if_connect_pdch() { abort(); }
627void l1if_close_pdch() { abort(); }
628void l1if_open_pdch() { abort(); }
629}