blob: b6f263f477987e1c80a39f23766903e68d11e533 [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,
Jacob Erlbeck5879c642015-07-10 10:41:36 +020041 uint8_t use_trx,
Daniel Willmann48aa0b02014-07-16 18:54:10 +020042 uint8_t ms_class, uint8_t single_slot)
43{
44 if (dir == GPRS_RLCMAC_UL_TBF)
Jacob Erlbeck5879c642015-07-10 10:41:36 +020045 return tbf_alloc_ul_tbf(bts, ms, use_trx, ms_class, single_slot);
Daniel Willmann48aa0b02014-07-16 18:54:10 +020046 else
Jacob Erlbeck5879c642015-07-10 10:41:36 +020047 return tbf_alloc_dl_tbf(bts, ms, use_trx, ms_class, single_slot);
Daniel Willmann48aa0b02014-07-16 18:54:10 +020048}
49
Jacob Erlbeck61205a72015-07-09 11:35:50 +020050static void check_tfi_usage(BTS *the_bts)
51{
52 int pdch_no;
53 struct gprs_rlcmac_bts *bts = the_bts->bts_data();
54
55 struct gprs_rlcmac_tbf *tfi_usage[8][8][2][32] = {{{{NULL}}}};
56 struct llist_head *tbf_lists[2] = {
57 &bts->ul_tbfs,
58 &bts->dl_tbfs
59 };
60
61 gprs_rlcmac_tbf *tbf;
62 struct llist_pods *lpods;
63 unsigned list_idx;
64 struct gprs_rlcmac_tbf **tbf_var;
65
66 for (list_idx = 0; list_idx < ARRAY_SIZE(tbf_lists); list_idx += 1)
67 {
68
69 llist_pods_for_each_entry(tbf, tbf_lists[list_idx], list, lpods) {
70 for (pdch_no = 0; pdch_no < 8; pdch_no += 1) {
71 struct gprs_rlcmac_pdch *pdch = tbf->pdch[pdch_no];
72 if (pdch == NULL)
73 continue;
74
75 tbf_var = &tfi_usage
76 [tbf->trx->trx_no]
77 [pdch_no]
78 [tbf->direction]
79 [tbf->tfi()];
80
81 OSMO_ASSERT(*tbf_var == NULL);
82 if (tbf->direction == GPRS_RLCMAC_DL_TBF) {
83 OSMO_ASSERT(pdch->dl_tbf_by_tfi(
84 tbf->tfi()) == tbf);
85 /* This assertion cannot hold with the
86 * current API and shared TFI */
87 OSMO_ASSERT(the_bts->dl_tbf_by_tfi(
88 tbf->tfi(),
89 tbf->trx->trx_no) == tbf);
90 } else {
91 OSMO_ASSERT(pdch->ul_tbf_by_tfi(
92 tbf->tfi()) == tbf);
93 /* This assertion cannot hold with the
94 * current API and shared TFI */
95 OSMO_ASSERT(the_bts->ul_tbf_by_tfi(
96 tbf->tfi(),
97 tbf->trx->trx_no) == tbf);
98 }
99 *tbf_var = tbf;
Jacob Erlbeck47a57f62015-07-08 12:53:16 +0200100 OSMO_ASSERT(pdch->assigned_tfi(tbf->direction) &
101 (1 << tbf->tfi()));
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200102 }
103 }
104 }
105}
106
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200107static void test_alloc_a(gprs_rlcmac_tbf_direction dir,
108 uint8_t slots, const int count)
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200109{
110 int tfi;
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200111 int i;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200112 uint8_t used_trx, tmp_trx;
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200113 BTS the_bts;
114 struct gprs_rlcmac_bts *bts;
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200115 struct gprs_rlcmac_tbf *tbfs[33] = { 0, };
116
117 printf("Testing alloc_a direction(%d)\n", dir);
118
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200119 bts = the_bts.bts_data();
120 bts->alloc_algorithm = alloc_algorithm_a;
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200121
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200122 struct gprs_rlcmac_trx *trx = &bts->trx[0];
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200123 for (i = 0; i < 8; i += 1)
124 if (slots & (1 << i))
125 trx->pdch[i].enable();
126
127 OSMO_ASSERT(count >= 0 && count <= (int)ARRAY_SIZE(tbfs));
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200128
129 /**
130 * Currently alloc_a will only allocate from the first
131 * PDCH and all possible usf's. We run out of usf's before
132 * we are out of tfi's. Observe this and make sure that at
133 * least this part is working okay.
134 */
135 for (int i = 0; i < count; ++i) {
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200136 tbfs[i] = tbf_alloc(bts, NULL, dir, -1, 0, 0);
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200137 OSMO_ASSERT(tbfs[i] != NULL);
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200138 tfi = the_bts.tfi_find_free(dir, &tmp_trx, used_trx);
139 OSMO_ASSERT(tbfs[i]->tfi() != tfi);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200140 }
141
142 /* Now check that there are still some TFIs */
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200143 tfi = the_bts.tfi_find_free(dir, &used_trx, 0);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200144 switch (dir) {
145 case GPRS_RLCMAC_UL_TBF:
146 OSMO_ASSERT(tfi >= 0);
147 break;
148 case GPRS_RLCMAC_DL_TBF:
149 OSMO_ASSERT(tfi < 0);
150 break;
151 }
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200152 OSMO_ASSERT(!tbf_alloc(bts, NULL, dir, -1, 0, 0));
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200153
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200154 for (size_t i = 0; i < ARRAY_SIZE(tbfs); ++i)
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200155 if (tbfs[i])
156 tbf_free(tbfs[i]);
157
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200158 tbfs[tfi] = tbf_alloc(bts, NULL, dir, -1, 0, 0);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200159 OSMO_ASSERT(tbfs[tfi]);
160 tbf_free(tbfs[tfi]);
161}
162
163static void test_alloc_a()
164{
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200165 /* slots 2 - 3 */
166 test_alloc_a(GPRS_RLCMAC_DL_TBF, 0x0c, 32);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200167 test_alloc_a(GPRS_RLCMAC_UL_TBF, 0x0c, 14);
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200168
169 /* slots 1 - 5 */
170 test_alloc_a(GPRS_RLCMAC_DL_TBF, 0x1e, 32);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200171 test_alloc_a(GPRS_RLCMAC_UL_TBF, 0x1e, 28);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200172}
173
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100174static void dump_assignment(struct gprs_rlcmac_tbf *tbf, const char *dir)
175{
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200176 for (size_t i = 0; i < ARRAY_SIZE(tbf->pdch); ++i)
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100177 if (tbf->pdch[i])
178 printf("PDCH[%d] is used for %s\n", i, dir);
179 printf("PDCH[%d] is control_ts for %s\n", tbf->control_ts, dir);
180 printf("PDCH[%d] is first common for %s\n", tbf->first_common_ts, dir);
181}
182
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100183static void test_alloc_b(int ms_class)
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100184{
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100185 printf("Going to test multislot assignment MS_CLASS=%d\n", ms_class);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100186 /*
187 * PDCH is on TS 6,7,8 and we start with a UL allocation and
188 * then follow two DL allocations (once single, once normal).
189 *
190 * Uplink assigned and still available..
191 */
192 {
193 BTS the_bts;
194 struct gprs_rlcmac_bts *bts;
195 struct gprs_rlcmac_trx *trx;
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200196 uint8_t trx_no;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100197
198 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
199
200 printf("Testing UL then DL assignment.\n");
201
202 bts = the_bts.bts_data();
203 bts->alloc_algorithm = alloc_algorithm_b;
204
205 trx = &bts->trx[0];
206 trx->pdch[5].enable();
207 trx->pdch[6].enable();
208 trx->pdch[7].enable();
209
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200210 ul_tbf = tbf_alloc_ul_tbf(bts, NULL, -1, ms_class, 1);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100211 OSMO_ASSERT(ul_tbf);
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200212 OSMO_ASSERT(ul_tbf->ms());
213 OSMO_ASSERT(ul_tbf->ms()->current_trx());
214 trx_no = ul_tbf->ms()->current_trx()->trx_no;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100215 dump_assignment(ul_tbf, "UL");
216
217 /* assume final ack has not been sent */
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200218 dl_tbf = tbf_alloc_dl_tbf(bts, ul_tbf->ms(), trx_no, ms_class, 0);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100219 OSMO_ASSERT(dl_tbf);
220 dump_assignment(dl_tbf, "DL");
221
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100222 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
223
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200224 check_tfi_usage(&the_bts);
225
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100226 tbf_free(dl_tbf);
227 tbf_free(ul_tbf);
228 }
229
230 /**
231 * Test with the other order.. first DL and then UL
232 */
233 {
234 BTS the_bts;
235 struct gprs_rlcmac_bts *bts;
236 struct gprs_rlcmac_trx *trx;
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200237 uint8_t trx_no;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100238
Daniel Willmann351a5732014-08-07 12:57:44 +0200239 gprs_rlcmac_ul_tbf *ul_tbf;
240 gprs_rlcmac_dl_tbf *dl_tbf;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100241
242 printf("Testing DL then UL assignment followed by update\n");
243
244 bts = the_bts.bts_data();
245 bts->alloc_algorithm = alloc_algorithm_b;
246
247 trx = &bts->trx[0];
248 trx->pdch[5].enable();
249 trx->pdch[6].enable();
250 trx->pdch[7].enable();
251
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200252 dl_tbf = tbf_alloc_dl_tbf(bts, NULL, -1, ms_class, 1);
Jacob Erlbeck767193e2015-05-20 12:06:46 +0200253 dl_tbf->update_ms(0x23, GPRS_RLCMAC_DL_TBF);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100254 OSMO_ASSERT(dl_tbf);
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200255 OSMO_ASSERT(dl_tbf->ms());
256 OSMO_ASSERT(dl_tbf->ms()->current_trx());
257 trx_no = dl_tbf->ms()->current_trx()->trx_no;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100258 dump_assignment(dl_tbf, "DL");
259
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200260 ul_tbf = tbf_alloc_ul_tbf(bts, dl_tbf->ms(), trx_no, ms_class, 0);
Jacob Erlbeck767193e2015-05-20 12:06:46 +0200261 ul_tbf->update_ms(0x23, GPRS_RLCMAC_UL_TBF);
Daniel Willmann7e994e32014-08-07 15:49:21 +0200262 ul_tbf->m_contention_resolution_done = 1;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100263 OSMO_ASSERT(ul_tbf);
264 dump_assignment(ul_tbf, "UL");
265
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100266 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
267
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100268 /* now update the dl_tbf */
269 dl_tbf->update();
270 dump_assignment(dl_tbf, "DL");
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100271 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100272
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200273 check_tfi_usage(&the_bts);
274
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100275 tbf_free(dl_tbf);
276 tbf_free(ul_tbf);
277 }
278
279 /* Andreas osmocom-pcu example */
280 {
281 BTS the_bts;
282 struct gprs_rlcmac_bts *bts;
283 struct gprs_rlcmac_trx *trx;
284 int tfi;
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200285 uint8_t trx_no;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100286
287 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
288
289 printf("Testing jolly example\n");
290
291 bts = the_bts.bts_data();
292 bts->alloc_algorithm = alloc_algorithm_b;
293
294 trx = &bts->trx[0];
295 trx->pdch[1].enable();
296 trx->pdch[2].enable();
297 trx->pdch[3].enable();
298 trx->pdch[4].enable();
299
300 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
301 OSMO_ASSERT(tfi >= 0);
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200302 ul_tbf = tbf_alloc_ul_tbf(bts, NULL, .1, ms_class, 0);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100303 OSMO_ASSERT(ul_tbf);
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200304 OSMO_ASSERT(ul_tbf->ms());
305 OSMO_ASSERT(ul_tbf->ms()->current_trx());
306 trx_no = ul_tbf->ms()->current_trx()->trx_no;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100307 dump_assignment(ul_tbf, "UL");
308
309 /* assume final ack has not been sent */
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200310 dl_tbf = tbf_alloc_dl_tbf(bts, ul_tbf->ms(), trx_no, ms_class, 0);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100311 OSMO_ASSERT(dl_tbf);
312 dump_assignment(dl_tbf, "DL");
313
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100314 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
315
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200316 check_tfi_usage(&the_bts);
317
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100318 tbf_free(dl_tbf);
319 tbf_free(ul_tbf);
320 }
321}
322
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100323#define ENABLE_PDCH(ts_no, enable_flag, trx) \
324 if (enable_flag) \
325 trx->pdch[ts_no].enable();
326
327static void test_alloc_b(bool ts0, bool ts1, bool ts2, bool ts3, bool ts4, bool ts5, bool ts6, bool ts7, int ms_class)
328{
329 /* we can test the allocation failures differently */
330 if (!ts0 && !ts1 && !ts2 && !ts3 && !ts4 && !ts5 && !ts6 && !ts7)
331 return;
332
333 printf("Mass test: TS0(%c%c%c%c%c%c%c%c)TS7 MS_Class=%d\n",
334 ts0 ? 'O' : 'x',
335 ts1 ? 'O' : 'x',
336 ts2 ? 'O' : 'x',
337 ts3 ? 'O' : 'x',
338 ts4 ? 'O' : 'x',
339 ts5 ? 'O' : 'x',
340 ts6 ? 'O' : 'x',
341 ts7 ? 'O' : 'x', ms_class);
342 fflush(stdout);
343
344 {
345 BTS the_bts;
346 struct gprs_rlcmac_bts *bts;
347 struct gprs_rlcmac_trx *trx;
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200348 uint8_t trx_no;
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100349
350 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
351
352 bts = the_bts.bts_data();
353 bts->alloc_algorithm = alloc_algorithm_b;
354
355 trx = &bts->trx[0];
356 ENABLE_PDCH(0, ts0, trx);
357 ENABLE_PDCH(1, ts1, trx);
358 ENABLE_PDCH(2, ts2, trx);
359 ENABLE_PDCH(3, ts3, trx);
360 ENABLE_PDCH(4, ts4, trx);
361 ENABLE_PDCH(5, ts5, trx);
362 ENABLE_PDCH(6, ts6, trx);
363 ENABLE_PDCH(7, ts7, trx);
364
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200365 ul_tbf = tbf_alloc_ul_tbf(bts, NULL, -1, ms_class, 1);
366 OSMO_ASSERT(ul_tbf->ms());
367 OSMO_ASSERT(ul_tbf->ms()->current_trx());
368 trx_no = ul_tbf->ms()->current_trx()->trx_no;
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100369 OSMO_ASSERT(ul_tbf);
370
371 /* assume final ack has not been sent */
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200372 dl_tbf = tbf_alloc_dl_tbf(bts, ul_tbf->ms(), trx_no, ms_class, 0);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100373 OSMO_ASSERT(dl_tbf);
374
375 /* verify that both are on the same ts */
376 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
377
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200378 check_tfi_usage(&the_bts);
379
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100380 tbf_free(dl_tbf);
381 tbf_free(ul_tbf);
382 }
383
384 /**
385 * Test with the other order.. first DL and then UL
386 */
387 {
388 BTS the_bts;
389 struct gprs_rlcmac_bts *bts;
390 struct gprs_rlcmac_trx *trx;
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200391 uint8_t trx_no;
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100392
Daniel Willmann351a5732014-08-07 12:57:44 +0200393 gprs_rlcmac_ul_tbf *ul_tbf;
394 gprs_rlcmac_dl_tbf *dl_tbf;
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100395
396 bts = the_bts.bts_data();
397 bts->alloc_algorithm = alloc_algorithm_b;
398
399 trx = &bts->trx[0];
400 ENABLE_PDCH(0, ts0, trx);
401 ENABLE_PDCH(1, ts1, trx);
402 ENABLE_PDCH(2, ts2, trx);
403 ENABLE_PDCH(3, ts3, trx);
404 ENABLE_PDCH(4, ts4, trx);
405 ENABLE_PDCH(5, ts5, trx);
406 ENABLE_PDCH(6, ts6, trx);
407 ENABLE_PDCH(7, ts7, trx);
408
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200409 dl_tbf = tbf_alloc_dl_tbf(bts, NULL, -1, ms_class, 1);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100410 OSMO_ASSERT(dl_tbf);
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200411 OSMO_ASSERT(dl_tbf->ms());
412 OSMO_ASSERT(dl_tbf->ms()->current_trx());
413 trx_no = dl_tbf->ms()->current_trx()->trx_no;
Jacob Erlbeck767193e2015-05-20 12:06:46 +0200414 dl_tbf->update_ms(0x23, GPRS_RLCMAC_DL_TBF);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100415
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200416 ul_tbf = tbf_alloc_ul_tbf(bts, dl_tbf->ms(), trx_no, ms_class, 0);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100417 OSMO_ASSERT(ul_tbf);
Jacob Erlbeck767193e2015-05-20 12:06:46 +0200418 ul_tbf->update_ms(0x23, GPRS_RLCMAC_UL_TBF);
Daniel Willmann7e994e32014-08-07 15:49:21 +0200419 ul_tbf->m_contention_resolution_done = 1;
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100420
421 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
422
423 /* now update the dl_tbf */
424 dl_tbf->update();
425 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
426
Jacob Erlbeckbefc7602015-06-02 12:33:30 +0200427 OSMO_ASSERT(ul_tbf->ms_class() == ms_class);
428 OSMO_ASSERT(dl_tbf->ms_class() == ms_class);
429
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200430 check_tfi_usage(&the_bts);
431
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100432 tbf_free(dl_tbf);
433 tbf_free(ul_tbf);
434 }
435}
436
437static void test_all_alloc_b()
438{
439 /* it is a bit crazy... */
440 for (uint8_t ts0 = 0; ts0 < 2; ++ts0)
441 for (uint8_t ts1 = 0; ts1 < 2; ++ts1)
442 for (uint8_t ts2 = 0; ts2 < 2; ++ts2)
443 for (uint8_t ts3 = 0; ts3 < 2; ++ts3)
444 for (uint8_t ts4 = 0; ts4 < 2; ++ts4)
445 for (uint8_t ts5 = 0; ts5 < 2; ++ts5)
446 for (uint8_t ts6 = 0; ts6 < 2; ++ts6)
447 for (uint8_t ts7 = 0; ts7 < 2; ++ts7)
448 for (int ms_class = 0; ms_class < 30; ++ms_class)
449 test_alloc_b(ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7, ms_class);
450}
451
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100452static void test_alloc_b()
453{
454 for (int i = 0; i < 30; ++i)
455 test_alloc_b(i);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100456
457 test_all_alloc_b();
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100458}
459
Jacob Erlbecke5655642015-06-29 12:19:52 +0200460typedef int (*algo_t)(struct gprs_rlcmac_bts *bts,
461 struct GprsMs *ms,
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200462 struct gprs_rlcmac_tbf *tbf, uint32_t cust, uint8_t single,
463 int use_trx);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200464
465static char get_dir_char(uint8_t mask, uint8_t tx, uint8_t rx)
466{
467 return (mask & tx & rx) ? 'C' :
468 (mask & tx) ? 'U' :
469 (mask & rx) ? 'D' :
470 '.';
471}
472
473enum test_mode {
474 TEST_MODE_UL_ONLY,
475 TEST_MODE_DL_ONLY,
476 TEST_MODE_UL_AND_DL,
477 TEST_MODE_DL_AND_UL,
478 TEST_MODE_DL_AFTER_UL,
479 TEST_MODE_UL_AFTER_DL,
480};
481
482static GprsMs *alloc_tbfs(BTS *the_bts, GprsMs *ms, unsigned ms_class,
483 enum test_mode mode)
484{
485 struct gprs_rlcmac_bts *bts;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200486 uint8_t trx_no = -1;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200487
488 bts = the_bts->bts_data();
489
490 gprs_rlcmac_tbf *tbf = NULL;
491
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200492 if (ms && ms->current_trx())
493 trx_no = ms->current_trx()->trx_no;
494
Jacob Erlbecke5655642015-06-29 12:19:52 +0200495 /* Allocate what is needed first */
496 switch (mode) {
497 case TEST_MODE_UL_ONLY:
498 case TEST_MODE_DL_AFTER_UL:
499 case TEST_MODE_UL_AND_DL:
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200500 tbf = tbf_alloc_ul_tbf(bts, ms, trx_no, ms_class, 0);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200501 if (tbf == NULL)
502 return NULL;
503 break;
504 case TEST_MODE_DL_ONLY:
505 case TEST_MODE_UL_AFTER_DL:
506 case TEST_MODE_DL_AND_UL:
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200507 tbf = tbf_alloc_dl_tbf(bts, ms, trx_no, ms_class, 0);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200508 if (tbf == NULL)
509 return NULL;
510 }
511
512 OSMO_ASSERT(tbf);
513 OSMO_ASSERT(tbf->ms());
514 OSMO_ASSERT(ms == NULL || ms == tbf->ms());
515 ms = tbf->ms();
516
517 GprsMs::Guard guard(ms);
518
Jacob Erlbeck14376a72015-07-07 11:31:28 +0200519 /* Continue with what is needed next */
520 switch (mode) {
521 case TEST_MODE_UL_ONLY:
522 case TEST_MODE_DL_ONLY:
523 /* We are done */
524 break;
525
526 case TEST_MODE_DL_AFTER_UL:
527 case TEST_MODE_UL_AND_DL:
528 ms = alloc_tbfs(the_bts, ms, ms_class, TEST_MODE_DL_ONLY);
529 break;
530
531 case TEST_MODE_UL_AFTER_DL:
532 case TEST_MODE_DL_AND_UL:
533 ms = alloc_tbfs(the_bts, ms, ms_class, TEST_MODE_UL_ONLY);
534 break;
535 }
536
Jacob Erlbecke5655642015-06-29 12:19:52 +0200537 /* Optionally delete the TBF */
538 switch (mode) {
539 case TEST_MODE_DL_AFTER_UL:
540 case TEST_MODE_UL_AFTER_DL:
541 tbf_free(tbf);
542 break;
543
544 default:
545 break;
546 }
547
Jacob Erlbeck14376a72015-07-07 11:31:28 +0200548 return ms;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200549}
550
551static void test_successive_allocation(algo_t algo, unsigned min_class,
552 unsigned max_class, enum test_mode mode,
553 unsigned expect_num, const char *text)
554{
555 BTS the_bts;
556 struct gprs_rlcmac_bts *bts;
557 struct gprs_rlcmac_trx *trx;
558 unsigned counter;
559 unsigned ms_class = min_class;
560
561 printf("Going to test assignment with many TBF, %s\n", text);
562
563 bts = the_bts.bts_data();
564 bts->alloc_algorithm = algo;
565
566 trx = &bts->trx[0];
567 trx->pdch[3].enable();
568 trx->pdch[4].enable();
569 trx->pdch[5].enable();
570 trx->pdch[6].enable();
571 trx->pdch[7].enable();
572
573 for (counter = 0; 1; counter += 1) {
574 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
575 uint8_t ul_slots = 0;
576 uint8_t dl_slots = 0;
577 unsigned i;
578 int tfi = -1;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200579 int tfi2;
580 uint8_t trx2;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200581 GprsMs *ms;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200582 enum gprs_rlcmac_tbf_direction dir;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200583
584 ms = alloc_tbfs(&the_bts, NULL, ms_class, mode);
585 if (!ms)
586 break;
587
588 ul_tbf = ms->ul_tbf();
589 dl_tbf = ms->dl_tbf();
590
591 if (ul_tbf) {
592 ul_slots = 1 << ul_tbf->first_common_ts;
593 tfi = ul_tbf->tfi();
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200594 dir = GPRS_RLCMAC_UL_TBF;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200595 } else if (dl_tbf) {
596 ul_slots = 1 << dl_tbf->first_common_ts;
597 tfi = dl_tbf->tfi();
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200598 dir = GPRS_RLCMAC_DL_TBF;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200599 }
600
601 for (i = 0; dl_tbf && i < ARRAY_SIZE(dl_tbf->pdch); i += 1)
602 if (dl_tbf->pdch[i])
603 dl_slots |= 1 << i;
604
605 printf(" TBF[%d] class %d reserves %c%c%c%c%c%c%c%c\n",
606 tfi, ms_class,
607 get_dir_char(0x01, ul_slots, dl_slots),
608 get_dir_char(0x02, ul_slots, dl_slots),
609 get_dir_char(0x04, ul_slots, dl_slots),
610 get_dir_char(0x08, ul_slots, dl_slots),
611 get_dir_char(0x10, ul_slots, dl_slots),
612 get_dir_char(0x20, ul_slots, dl_slots),
613 get_dir_char(0x40, ul_slots, dl_slots),
614 get_dir_char(0x80, ul_slots, dl_slots));
615
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200616 if (tfi >= 0) {
617 OSMO_ASSERT(ms->current_trx());
618 tfi2 = the_bts.tfi_find_free(dir, &trx2,
619 ms->current_trx()->trx_no);
620 OSMO_ASSERT(tfi != tfi2);
621 OSMO_ASSERT(trx2 == ms->current_trx()->trx_no);
622 }
623
Jacob Erlbecke5655642015-06-29 12:19:52 +0200624 ms_class += 1;
625 if (ms_class > max_class)
626 ms_class = min_class;
627 }
628
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200629 check_tfi_usage(&the_bts);
630
Jacob Erlbecke5655642015-06-29 12:19:52 +0200631 printf(" Successfully allocated %d UL TBFs\n", counter);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200632 OSMO_ASSERT(counter == expect_num);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200633}
634
635static void test_successive_allocation()
636{
637 test_successive_allocation(alloc_algorithm_a, 1, 1, TEST_MODE_UL_AND_DL,
Jacob Erlbeckec478752015-06-19 16:35:38 +0200638 32, "algorithm A (UL and DL)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200639 test_successive_allocation(alloc_algorithm_b, 10, 10, TEST_MODE_UL_AND_DL,
Jacob Erlbeck16c6ecc2015-06-30 13:40:18 +0200640 32, "algorithm B class 10 (UL and DL)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200641 test_successive_allocation(alloc_algorithm_b, 12, 12, TEST_MODE_UL_AND_DL,
Jacob Erlbeck16c6ecc2015-06-30 13:40:18 +0200642 32, "algorithm B class 12 (UL and DL)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200643 test_successive_allocation(alloc_algorithm_b, 1, 12, TEST_MODE_UL_AND_DL,
Jacob Erlbecked46afd2015-07-01 12:19:40 +0200644 32, "algorithm B class 1-12 (UL and DL)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200645 test_successive_allocation(alloc_algorithm_b, 1, 29, TEST_MODE_UL_AND_DL,
Jacob Erlbeck16c6ecc2015-06-30 13:40:18 +0200646 32, "algorithm B class 1-29 (UL and DL)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200647
648 test_successive_allocation(alloc_algorithm_a, 1, 1, TEST_MODE_DL_AND_UL,
Jacob Erlbeckec478752015-06-19 16:35:38 +0200649 32, "algorithm A (DL and UL)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200650 test_successive_allocation(alloc_algorithm_b, 10, 10, TEST_MODE_DL_AND_UL,
Jacob Erlbeck16c6ecc2015-06-30 13:40:18 +0200651 32, "algorithm B class 10 (DL and UL)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200652
653 test_successive_allocation(alloc_algorithm_a, 1, 1, TEST_MODE_DL_AFTER_UL,
Jacob Erlbeckec478752015-06-19 16:35:38 +0200654 32, "algorithm A (DL after UL)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200655 test_successive_allocation(alloc_algorithm_b, 10, 10, TEST_MODE_DL_AFTER_UL,
Jacob Erlbeckec478752015-06-19 16:35:38 +0200656 32, "algorithm B class 10 (DL after UL)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200657
658 test_successive_allocation(alloc_algorithm_a, 1, 1, TEST_MODE_UL_AFTER_DL,
Jacob Erlbeckefe62a72015-07-02 15:48:25 +0200659 32, "algorithm A (UL after DL)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200660 test_successive_allocation(alloc_algorithm_b, 10, 10, TEST_MODE_UL_AFTER_DL,
Jacob Erlbeck16c6ecc2015-06-30 13:40:18 +0200661 32, "algorithm B class 10 (UL after DL)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200662
663 test_successive_allocation(alloc_algorithm_a, 1, 1, TEST_MODE_UL_ONLY,
Jacob Erlbeckec478752015-06-19 16:35:38 +0200664 32, "algorithm A (UL only)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200665 test_successive_allocation(alloc_algorithm_b, 10, 10, TEST_MODE_UL_ONLY,
Jacob Erlbeck16c6ecc2015-06-30 13:40:18 +0200666 32, "algorithm B class 10 (UL only)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200667
668 test_successive_allocation(alloc_algorithm_a, 1, 1, TEST_MODE_DL_ONLY,
Jacob Erlbeckec478752015-06-19 16:35:38 +0200669 32, "algorithm A (DL ONLY)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200670 test_successive_allocation(alloc_algorithm_b, 10, 10, TEST_MODE_DL_ONLY,
Jacob Erlbeckec478752015-06-19 16:35:38 +0200671 32, "algorithm B class 10 (DL ONLY)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200672}
673
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200674int main(int argc, char **argv)
675{
676 tall_pcu_ctx = talloc_named_const(NULL, 1, "moiji-mobile AllocTest context");
677 if (!tall_pcu_ctx)
678 abort();
679
680 msgb_set_talloc_ctx(tall_pcu_ctx);
681 osmo_init_logging(&gprs_log_info);
682 log_set_use_color(osmo_stderr_target, 0);
683 log_set_print_filename(osmo_stderr_target, 0);
Jacob Erlbeck9ec49e22015-06-29 13:00:20 +0200684 if (getenv("LOGL_DEBUG"))
685 log_set_log_level(osmo_stderr_target, LOGL_DEBUG);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200686
687 test_alloc_a();
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100688 test_alloc_b();
Jacob Erlbecke5655642015-06-29 12:19:52 +0200689 test_successive_allocation();
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200690 return EXIT_SUCCESS;
691}
692
693/*
694 * stubs that should not be reached
695 */
696extern "C" {
697void l1if_pdch_req() { abort(); }
698void l1if_connect_pdch() { abort(); }
699void l1if_close_pdch() { abort(); }
700void l1if_open_pdch() { abort(); }
701}