blob: 7bc865d2b0ec79c9b1c5bd6d16c9171bff4a9936 [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);
Jacob Erlbeck61205a72015-07-09 11:35:50 +020085 OSMO_ASSERT(the_bts->dl_tbf_by_tfi(
86 tbf->tfi(),
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +020087 tbf->trx->trx_no,
88 pdch_no) == tbf);
Jacob Erlbeck61205a72015-07-09 11:35:50 +020089 } else {
90 OSMO_ASSERT(pdch->ul_tbf_by_tfi(
91 tbf->tfi()) == tbf);
Jacob Erlbeck61205a72015-07-09 11:35:50 +020092 OSMO_ASSERT(the_bts->ul_tbf_by_tfi(
93 tbf->tfi(),
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +020094 tbf->trx->trx_no,
95 pdch_no) == tbf);
Jacob Erlbeck61205a72015-07-09 11:35:50 +020096 }
97 *tbf_var = tbf;
Jacob Erlbeck47a57f62015-07-08 12:53:16 +020098 OSMO_ASSERT(pdch->assigned_tfi(tbf->direction) &
99 (1 << tbf->tfi()));
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200100 }
101 }
102 }
103}
104
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200105static void test_alloc_a(gprs_rlcmac_tbf_direction dir,
106 uint8_t slots, const int count)
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200107{
108 int tfi;
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200109 int i;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200110 uint8_t used_trx, tmp_trx;
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200111 BTS the_bts;
112 struct gprs_rlcmac_bts *bts;
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200113 struct gprs_rlcmac_tbf *tbfs[32*8+1] = { 0, };
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200114
115 printf("Testing alloc_a direction(%d)\n", dir);
116
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200117 bts = the_bts.bts_data();
118 bts->alloc_algorithm = alloc_algorithm_a;
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200119
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200120 struct gprs_rlcmac_trx *trx = &bts->trx[0];
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200121 for (i = 0; i < 8; i += 1)
122 if (slots & (1 << i))
123 trx->pdch[i].enable();
124
125 OSMO_ASSERT(count >= 0 && count <= (int)ARRAY_SIZE(tbfs));
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200126
127 /**
128 * Currently alloc_a will only allocate from the first
129 * PDCH and all possible usf's. We run out of usf's before
130 * we are out of tfi's. Observe this and make sure that at
131 * least this part is working okay.
132 */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200133 for (i = 0; i < (int)ARRAY_SIZE(tbfs); ++i) {
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200134 tbfs[i] = tbf_alloc(bts, NULL, dir, -1, 0, 0);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200135 if (tbfs[i] == NULL)
136 break;
137
138 used_trx = tbfs[i]->trx->trx_no;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200139 tfi = the_bts.tfi_find_free(dir, &tmp_trx, used_trx);
140 OSMO_ASSERT(tbfs[i]->tfi() != tfi);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200141 }
142
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200143 check_tfi_usage(&the_bts);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200144
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200145 OSMO_ASSERT(i == count);
146
147 for (i = 0; i < count; ++i)
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200148 if (tbfs[i])
149 tbf_free(tbfs[i]);
150
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200151 tbfs[tfi] = tbf_alloc(bts, NULL, dir, -1, 0, 0);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200152 OSMO_ASSERT(tbfs[tfi]);
153 tbf_free(tbfs[tfi]);
154}
155
156static void test_alloc_a()
157{
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200158 /* slots 2 - 3 */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200159 test_alloc_a(GPRS_RLCMAC_DL_TBF, 0x0c, 32*2);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200160 test_alloc_a(GPRS_RLCMAC_UL_TBF, 0x0c, 14);
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200161
162 /* slots 1 - 5 */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200163 test_alloc_a(GPRS_RLCMAC_DL_TBF, 0x1e, 32*4);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200164 test_alloc_a(GPRS_RLCMAC_UL_TBF, 0x1e, 28);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200165}
166
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100167static void dump_assignment(struct gprs_rlcmac_tbf *tbf, const char *dir)
168{
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200169 for (size_t i = 0; i < ARRAY_SIZE(tbf->pdch); ++i)
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100170 if (tbf->pdch[i])
171 printf("PDCH[%d] is used for %s\n", i, dir);
172 printf("PDCH[%d] is control_ts for %s\n", tbf->control_ts, dir);
173 printf("PDCH[%d] is first common for %s\n", tbf->first_common_ts, dir);
174}
175
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100176static void test_alloc_b(int ms_class)
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100177{
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100178 printf("Going to test multislot assignment MS_CLASS=%d\n", ms_class);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100179 /*
180 * PDCH is on TS 6,7,8 and we start with a UL allocation and
181 * then follow two DL allocations (once single, once normal).
182 *
183 * Uplink assigned and still available..
184 */
185 {
186 BTS the_bts;
187 struct gprs_rlcmac_bts *bts;
188 struct gprs_rlcmac_trx *trx;
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200189 uint8_t trx_no;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100190
191 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
192
193 printf("Testing UL then DL assignment.\n");
194
195 bts = the_bts.bts_data();
196 bts->alloc_algorithm = alloc_algorithm_b;
197
198 trx = &bts->trx[0];
199 trx->pdch[5].enable();
200 trx->pdch[6].enable();
201 trx->pdch[7].enable();
202
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200203 ul_tbf = tbf_alloc_ul_tbf(bts, NULL, -1, ms_class, 1);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100204 OSMO_ASSERT(ul_tbf);
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200205 OSMO_ASSERT(ul_tbf->ms());
206 OSMO_ASSERT(ul_tbf->ms()->current_trx());
207 trx_no = ul_tbf->ms()->current_trx()->trx_no;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100208 dump_assignment(ul_tbf, "UL");
209
210 /* assume final ack has not been sent */
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200211 dl_tbf = tbf_alloc_dl_tbf(bts, ul_tbf->ms(), trx_no, ms_class, 0);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100212 OSMO_ASSERT(dl_tbf);
213 dump_assignment(dl_tbf, "DL");
214
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100215 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
216
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200217 check_tfi_usage(&the_bts);
218
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100219 tbf_free(dl_tbf);
220 tbf_free(ul_tbf);
221 }
222
223 /**
224 * Test with the other order.. first DL and then UL
225 */
226 {
227 BTS the_bts;
228 struct gprs_rlcmac_bts *bts;
229 struct gprs_rlcmac_trx *trx;
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200230 uint8_t trx_no;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100231
Daniel Willmann351a5732014-08-07 12:57:44 +0200232 gprs_rlcmac_ul_tbf *ul_tbf;
233 gprs_rlcmac_dl_tbf *dl_tbf;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100234
235 printf("Testing DL then UL assignment followed by update\n");
236
237 bts = the_bts.bts_data();
238 bts->alloc_algorithm = alloc_algorithm_b;
239
240 trx = &bts->trx[0];
241 trx->pdch[5].enable();
242 trx->pdch[6].enable();
243 trx->pdch[7].enable();
244
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200245 dl_tbf = tbf_alloc_dl_tbf(bts, NULL, -1, ms_class, 1);
Jacob Erlbeck767193e2015-05-20 12:06:46 +0200246 dl_tbf->update_ms(0x23, GPRS_RLCMAC_DL_TBF);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100247 OSMO_ASSERT(dl_tbf);
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200248 OSMO_ASSERT(dl_tbf->ms());
249 OSMO_ASSERT(dl_tbf->ms()->current_trx());
250 trx_no = dl_tbf->ms()->current_trx()->trx_no;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100251 dump_assignment(dl_tbf, "DL");
252
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200253 ul_tbf = tbf_alloc_ul_tbf(bts, dl_tbf->ms(), trx_no, ms_class, 0);
Jacob Erlbeck767193e2015-05-20 12:06:46 +0200254 ul_tbf->update_ms(0x23, GPRS_RLCMAC_UL_TBF);
Daniel Willmann7e994e32014-08-07 15:49:21 +0200255 ul_tbf->m_contention_resolution_done = 1;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100256 OSMO_ASSERT(ul_tbf);
257 dump_assignment(ul_tbf, "UL");
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 /* now update the dl_tbf */
262 dl_tbf->update();
263 dump_assignment(dl_tbf, "DL");
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100264 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100265
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200266 check_tfi_usage(&the_bts);
267
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100268 tbf_free(dl_tbf);
269 tbf_free(ul_tbf);
270 }
271
272 /* Andreas osmocom-pcu example */
273 {
274 BTS the_bts;
275 struct gprs_rlcmac_bts *bts;
276 struct gprs_rlcmac_trx *trx;
277 int tfi;
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200278 uint8_t trx_no;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100279
280 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
281
282 printf("Testing jolly example\n");
283
284 bts = the_bts.bts_data();
285 bts->alloc_algorithm = alloc_algorithm_b;
286
287 trx = &bts->trx[0];
288 trx->pdch[1].enable();
289 trx->pdch[2].enable();
290 trx->pdch[3].enable();
291 trx->pdch[4].enable();
292
293 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
294 OSMO_ASSERT(tfi >= 0);
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200295 ul_tbf = tbf_alloc_ul_tbf(bts, NULL, .1, ms_class, 0);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100296 OSMO_ASSERT(ul_tbf);
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200297 OSMO_ASSERT(ul_tbf->ms());
298 OSMO_ASSERT(ul_tbf->ms()->current_trx());
299 trx_no = ul_tbf->ms()->current_trx()->trx_no;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100300 dump_assignment(ul_tbf, "UL");
301
302 /* assume final ack has not been sent */
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200303 dl_tbf = tbf_alloc_dl_tbf(bts, ul_tbf->ms(), trx_no, ms_class, 0);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100304 OSMO_ASSERT(dl_tbf);
305 dump_assignment(dl_tbf, "DL");
306
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100307 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
308
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200309 check_tfi_usage(&the_bts);
310
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100311 tbf_free(dl_tbf);
312 tbf_free(ul_tbf);
313 }
314}
315
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100316#define ENABLE_PDCH(ts_no, enable_flag, trx) \
317 if (enable_flag) \
318 trx->pdch[ts_no].enable();
319
320static void test_alloc_b(bool ts0, bool ts1, bool ts2, bool ts3, bool ts4, bool ts5, bool ts6, bool ts7, int ms_class)
321{
322 /* we can test the allocation failures differently */
323 if (!ts0 && !ts1 && !ts2 && !ts3 && !ts4 && !ts5 && !ts6 && !ts7)
324 return;
325
326 printf("Mass test: TS0(%c%c%c%c%c%c%c%c)TS7 MS_Class=%d\n",
327 ts0 ? 'O' : 'x',
328 ts1 ? 'O' : 'x',
329 ts2 ? 'O' : 'x',
330 ts3 ? 'O' : 'x',
331 ts4 ? 'O' : 'x',
332 ts5 ? 'O' : 'x',
333 ts6 ? 'O' : 'x',
334 ts7 ? 'O' : 'x', ms_class);
335 fflush(stdout);
336
337 {
338 BTS the_bts;
339 struct gprs_rlcmac_bts *bts;
340 struct gprs_rlcmac_trx *trx;
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200341 uint8_t trx_no;
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100342
343 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
344
345 bts = the_bts.bts_data();
346 bts->alloc_algorithm = alloc_algorithm_b;
347
348 trx = &bts->trx[0];
349 ENABLE_PDCH(0, ts0, trx);
350 ENABLE_PDCH(1, ts1, trx);
351 ENABLE_PDCH(2, ts2, trx);
352 ENABLE_PDCH(3, ts3, trx);
353 ENABLE_PDCH(4, ts4, trx);
354 ENABLE_PDCH(5, ts5, trx);
355 ENABLE_PDCH(6, ts6, trx);
356 ENABLE_PDCH(7, ts7, trx);
357
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200358 ul_tbf = tbf_alloc_ul_tbf(bts, NULL, -1, ms_class, 1);
359 OSMO_ASSERT(ul_tbf->ms());
360 OSMO_ASSERT(ul_tbf->ms()->current_trx());
361 trx_no = ul_tbf->ms()->current_trx()->trx_no;
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100362 OSMO_ASSERT(ul_tbf);
363
364 /* assume final ack has not been sent */
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200365 dl_tbf = tbf_alloc_dl_tbf(bts, ul_tbf->ms(), trx_no, ms_class, 0);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100366 OSMO_ASSERT(dl_tbf);
367
368 /* verify that both are on the same ts */
369 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
370
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200371 check_tfi_usage(&the_bts);
372
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100373 tbf_free(dl_tbf);
374 tbf_free(ul_tbf);
375 }
376
377 /**
378 * Test with the other order.. first DL and then UL
379 */
380 {
381 BTS the_bts;
382 struct gprs_rlcmac_bts *bts;
383 struct gprs_rlcmac_trx *trx;
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200384 uint8_t trx_no;
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100385
Daniel Willmann351a5732014-08-07 12:57:44 +0200386 gprs_rlcmac_ul_tbf *ul_tbf;
387 gprs_rlcmac_dl_tbf *dl_tbf;
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100388
389 bts = the_bts.bts_data();
390 bts->alloc_algorithm = alloc_algorithm_b;
391
392 trx = &bts->trx[0];
393 ENABLE_PDCH(0, ts0, trx);
394 ENABLE_PDCH(1, ts1, trx);
395 ENABLE_PDCH(2, ts2, trx);
396 ENABLE_PDCH(3, ts3, trx);
397 ENABLE_PDCH(4, ts4, trx);
398 ENABLE_PDCH(5, ts5, trx);
399 ENABLE_PDCH(6, ts6, trx);
400 ENABLE_PDCH(7, ts7, trx);
401
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200402 dl_tbf = tbf_alloc_dl_tbf(bts, NULL, -1, ms_class, 1);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100403 OSMO_ASSERT(dl_tbf);
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200404 OSMO_ASSERT(dl_tbf->ms());
405 OSMO_ASSERT(dl_tbf->ms()->current_trx());
406 trx_no = dl_tbf->ms()->current_trx()->trx_no;
Jacob Erlbeck767193e2015-05-20 12:06:46 +0200407 dl_tbf->update_ms(0x23, GPRS_RLCMAC_DL_TBF);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100408
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200409 ul_tbf = tbf_alloc_ul_tbf(bts, dl_tbf->ms(), trx_no, ms_class, 0);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100410 OSMO_ASSERT(ul_tbf);
Jacob Erlbeck767193e2015-05-20 12:06:46 +0200411 ul_tbf->update_ms(0x23, GPRS_RLCMAC_UL_TBF);
Daniel Willmann7e994e32014-08-07 15:49:21 +0200412 ul_tbf->m_contention_resolution_done = 1;
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100413
414 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
415
416 /* now update the dl_tbf */
417 dl_tbf->update();
418 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
419
Jacob Erlbeckbefc7602015-06-02 12:33:30 +0200420 OSMO_ASSERT(ul_tbf->ms_class() == ms_class);
421 OSMO_ASSERT(dl_tbf->ms_class() == ms_class);
422
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200423 check_tfi_usage(&the_bts);
424
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100425 tbf_free(dl_tbf);
426 tbf_free(ul_tbf);
427 }
428}
429
430static void test_all_alloc_b()
431{
432 /* it is a bit crazy... */
433 for (uint8_t ts0 = 0; ts0 < 2; ++ts0)
434 for (uint8_t ts1 = 0; ts1 < 2; ++ts1)
435 for (uint8_t ts2 = 0; ts2 < 2; ++ts2)
436 for (uint8_t ts3 = 0; ts3 < 2; ++ts3)
437 for (uint8_t ts4 = 0; ts4 < 2; ++ts4)
438 for (uint8_t ts5 = 0; ts5 < 2; ++ts5)
439 for (uint8_t ts6 = 0; ts6 < 2; ++ts6)
440 for (uint8_t ts7 = 0; ts7 < 2; ++ts7)
441 for (int ms_class = 0; ms_class < 30; ++ms_class)
442 test_alloc_b(ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7, ms_class);
443}
444
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100445static void test_alloc_b()
446{
447 for (int i = 0; i < 30; ++i)
448 test_alloc_b(i);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100449
450 test_all_alloc_b();
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100451}
452
Jacob Erlbecke5655642015-06-29 12:19:52 +0200453typedef int (*algo_t)(struct gprs_rlcmac_bts *bts,
454 struct GprsMs *ms,
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200455 struct gprs_rlcmac_tbf *tbf, uint32_t cust, uint8_t single,
456 int use_trx);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200457
458static char get_dir_char(uint8_t mask, uint8_t tx, uint8_t rx)
459{
460 return (mask & tx & rx) ? 'C' :
461 (mask & tx) ? 'U' :
462 (mask & rx) ? 'D' :
463 '.';
464}
465
466enum test_mode {
467 TEST_MODE_UL_ONLY,
468 TEST_MODE_DL_ONLY,
469 TEST_MODE_UL_AND_DL,
470 TEST_MODE_DL_AND_UL,
471 TEST_MODE_DL_AFTER_UL,
472 TEST_MODE_UL_AFTER_DL,
473};
474
475static GprsMs *alloc_tbfs(BTS *the_bts, GprsMs *ms, unsigned ms_class,
476 enum test_mode mode)
477{
478 struct gprs_rlcmac_bts *bts;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200479 uint8_t trx_no = -1;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200480
481 bts = the_bts->bts_data();
482
483 gprs_rlcmac_tbf *tbf = NULL;
484
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200485 if (ms && ms->current_trx())
486 trx_no = ms->current_trx()->trx_no;
487
Jacob Erlbecke5655642015-06-29 12:19:52 +0200488 /* Allocate what is needed first */
489 switch (mode) {
490 case TEST_MODE_UL_ONLY:
491 case TEST_MODE_DL_AFTER_UL:
492 case TEST_MODE_UL_AND_DL:
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200493 tbf = tbf_alloc_ul_tbf(bts, ms, trx_no, ms_class, 0);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200494 if (tbf == NULL)
495 return NULL;
496 break;
497 case TEST_MODE_DL_ONLY:
498 case TEST_MODE_UL_AFTER_DL:
499 case TEST_MODE_DL_AND_UL:
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200500 tbf = tbf_alloc_dl_tbf(bts, ms, trx_no, ms_class, 0);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200501 if (tbf == NULL)
502 return NULL;
503 }
504
505 OSMO_ASSERT(tbf);
506 OSMO_ASSERT(tbf->ms());
507 OSMO_ASSERT(ms == NULL || ms == tbf->ms());
508 ms = tbf->ms();
509
510 GprsMs::Guard guard(ms);
511
Jacob Erlbeck14376a72015-07-07 11:31:28 +0200512 /* Continue with what is needed next */
513 switch (mode) {
514 case TEST_MODE_UL_ONLY:
515 case TEST_MODE_DL_ONLY:
516 /* We are done */
517 break;
518
519 case TEST_MODE_DL_AFTER_UL:
520 case TEST_MODE_UL_AND_DL:
521 ms = alloc_tbfs(the_bts, ms, ms_class, TEST_MODE_DL_ONLY);
522 break;
523
524 case TEST_MODE_UL_AFTER_DL:
525 case TEST_MODE_DL_AND_UL:
526 ms = alloc_tbfs(the_bts, ms, ms_class, TEST_MODE_UL_ONLY);
527 break;
528 }
529
Jacob Erlbecke5655642015-06-29 12:19:52 +0200530 /* Optionally delete the TBF */
531 switch (mode) {
532 case TEST_MODE_DL_AFTER_UL:
533 case TEST_MODE_UL_AFTER_DL:
534 tbf_free(tbf);
535 break;
536
537 default:
538 break;
539 }
540
Jacob Erlbeck14376a72015-07-07 11:31:28 +0200541 return ms;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200542}
543
544static void test_successive_allocation(algo_t algo, unsigned min_class,
545 unsigned max_class, enum test_mode mode,
546 unsigned expect_num, const char *text)
547{
548 BTS the_bts;
549 struct gprs_rlcmac_bts *bts;
550 struct gprs_rlcmac_trx *trx;
551 unsigned counter;
552 unsigned ms_class = min_class;
553
554 printf("Going to test assignment with many TBF, %s\n", text);
555
556 bts = the_bts.bts_data();
557 bts->alloc_algorithm = algo;
558
559 trx = &bts->trx[0];
560 trx->pdch[3].enable();
561 trx->pdch[4].enable();
562 trx->pdch[5].enable();
563 trx->pdch[6].enable();
564 trx->pdch[7].enable();
565
566 for (counter = 0; 1; counter += 1) {
567 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
568 uint8_t ul_slots = 0;
569 uint8_t dl_slots = 0;
570 unsigned i;
571 int tfi = -1;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200572 int tfi2;
573 uint8_t trx2;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200574 GprsMs *ms;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200575 enum gprs_rlcmac_tbf_direction dir;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200576
577 ms = alloc_tbfs(&the_bts, NULL, ms_class, mode);
578 if (!ms)
579 break;
580
581 ul_tbf = ms->ul_tbf();
582 dl_tbf = ms->dl_tbf();
583
584 if (ul_tbf) {
585 ul_slots = 1 << ul_tbf->first_common_ts;
586 tfi = ul_tbf->tfi();
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200587 dir = GPRS_RLCMAC_UL_TBF;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200588 } else if (dl_tbf) {
589 ul_slots = 1 << dl_tbf->first_common_ts;
590 tfi = dl_tbf->tfi();
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200591 dir = GPRS_RLCMAC_DL_TBF;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200592 }
593
594 for (i = 0; dl_tbf && i < ARRAY_SIZE(dl_tbf->pdch); i += 1)
595 if (dl_tbf->pdch[i])
596 dl_slots |= 1 << i;
597
598 printf(" TBF[%d] class %d reserves %c%c%c%c%c%c%c%c\n",
599 tfi, ms_class,
600 get_dir_char(0x01, ul_slots, dl_slots),
601 get_dir_char(0x02, ul_slots, dl_slots),
602 get_dir_char(0x04, ul_slots, dl_slots),
603 get_dir_char(0x08, ul_slots, dl_slots),
604 get_dir_char(0x10, ul_slots, dl_slots),
605 get_dir_char(0x20, ul_slots, dl_slots),
606 get_dir_char(0x40, ul_slots, dl_slots),
607 get_dir_char(0x80, ul_slots, dl_slots));
608
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200609 if (tfi >= 0) {
610 OSMO_ASSERT(ms->current_trx());
611 tfi2 = the_bts.tfi_find_free(dir, &trx2,
612 ms->current_trx()->trx_no);
613 OSMO_ASSERT(tfi != tfi2);
614 OSMO_ASSERT(trx2 == ms->current_trx()->trx_no);
615 }
616
Jacob Erlbecke5655642015-06-29 12:19:52 +0200617 ms_class += 1;
618 if (ms_class > max_class)
619 ms_class = min_class;
620 }
621
622 printf(" Successfully allocated %d UL TBFs\n", counter);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200623 OSMO_ASSERT(counter == expect_num);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200624
625 check_tfi_usage(&the_bts);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200626}
627
628static void test_successive_allocation()
629{
630 test_successive_allocation(alloc_algorithm_a, 1, 1, TEST_MODE_UL_AND_DL,
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200631 35, "algorithm A (UL and DL)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200632 test_successive_allocation(alloc_algorithm_b, 10, 10, TEST_MODE_UL_AND_DL,
Jacob Erlbeck16c6ecc2015-06-30 13:40:18 +0200633 32, "algorithm B class 10 (UL and DL)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200634 test_successive_allocation(alloc_algorithm_b, 12, 12, TEST_MODE_UL_AND_DL,
Jacob Erlbeck16c6ecc2015-06-30 13:40:18 +0200635 32, "algorithm B class 12 (UL and DL)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200636 test_successive_allocation(alloc_algorithm_b, 1, 12, TEST_MODE_UL_AND_DL,
Jacob Erlbecked46afd2015-07-01 12:19:40 +0200637 32, "algorithm B class 1-12 (UL and DL)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200638 test_successive_allocation(alloc_algorithm_b, 1, 29, TEST_MODE_UL_AND_DL,
Jacob Erlbeck16c6ecc2015-06-30 13:40:18 +0200639 32, "algorithm B class 1-29 (UL and DL)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200640
641 test_successive_allocation(alloc_algorithm_a, 1, 1, TEST_MODE_DL_AND_UL,
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200642 35, "algorithm A (DL and UL)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200643 test_successive_allocation(alloc_algorithm_b, 10, 10, TEST_MODE_DL_AND_UL,
Jacob Erlbeck16c6ecc2015-06-30 13:40:18 +0200644 32, "algorithm B class 10 (DL and UL)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200645
646 test_successive_allocation(alloc_algorithm_a, 1, 1, TEST_MODE_DL_AFTER_UL,
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200647 160, "algorithm A (DL after UL)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200648 test_successive_allocation(alloc_algorithm_b, 10, 10, TEST_MODE_DL_AFTER_UL,
Jacob Erlbeckec478752015-06-19 16:35:38 +0200649 32, "algorithm B class 10 (DL after UL)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200650
651 test_successive_allocation(alloc_algorithm_a, 1, 1, TEST_MODE_UL_AFTER_DL,
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200652 35, "algorithm A (UL after DL)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200653 test_successive_allocation(alloc_algorithm_b, 10, 10, TEST_MODE_UL_AFTER_DL,
Jacob Erlbeck16c6ecc2015-06-30 13:40:18 +0200654 32, "algorithm B class 10 (UL after DL)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200655
656 test_successive_allocation(alloc_algorithm_a, 1, 1, TEST_MODE_UL_ONLY,
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200657 35, "algorithm A (UL only)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200658 test_successive_allocation(alloc_algorithm_b, 10, 10, TEST_MODE_UL_ONLY,
Jacob Erlbeck16c6ecc2015-06-30 13:40:18 +0200659 32, "algorithm B class 10 (UL only)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200660
661 test_successive_allocation(alloc_algorithm_a, 1, 1, TEST_MODE_DL_ONLY,
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200662 160, "algorithm A (DL ONLY)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200663 test_successive_allocation(alloc_algorithm_b, 10, 10, TEST_MODE_DL_ONLY,
Jacob Erlbeckec478752015-06-19 16:35:38 +0200664 32, "algorithm B class 10 (DL ONLY)");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200665}
666
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200667int main(int argc, char **argv)
668{
669 tall_pcu_ctx = talloc_named_const(NULL, 1, "moiji-mobile AllocTest context");
670 if (!tall_pcu_ctx)
671 abort();
672
673 msgb_set_talloc_ctx(tall_pcu_ctx);
674 osmo_init_logging(&gprs_log_info);
675 log_set_use_color(osmo_stderr_target, 0);
676 log_set_print_filename(osmo_stderr_target, 0);
Jacob Erlbeck9ec49e22015-06-29 13:00:20 +0200677 if (getenv("LOGL_DEBUG"))
678 log_set_log_level(osmo_stderr_target, LOGL_DEBUG);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200679
680 test_alloc_a();
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100681 test_alloc_b();
Jacob Erlbecke5655642015-06-29 12:19:52 +0200682 test_successive_allocation();
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200683 return EXIT_SUCCESS;
684}
685
686/*
687 * stubs that should not be reached
688 */
689extern "C" {
690void l1if_pdch_req() { abort(); }
691void l1if_connect_pdch() { abort(); }
692void l1if_close_pdch() { abort(); }
693void l1if_open_pdch() { abort(); }
694}