blob: 6e344dcb17202f286e8478fd78492f19d26cd992 [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" {
Max9f460712018-01-23 20:57:08 +010029#include "mslot_class.h"
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020030#include <osmocom/core/application.h>
31#include <osmocom/core/msgb.h>
32#include <osmocom/core/talloc.h>
33#include <osmocom/core/utils.h>
34}
35
36/* globals used by the code */
37void *tall_pcu_ctx;
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020038int16_t spoof_mnc = 0, spoof_mcc = 0;
39
Daniel Willmann48aa0b02014-07-16 18:54:10 +020040static gprs_rlcmac_tbf *tbf_alloc(struct gprs_rlcmac_bts *bts,
Jacob Erlbecke2e004e2015-06-18 17:16:26 +020041 GprsMs *ms, gprs_rlcmac_tbf_direction dir,
Jacob Erlbeck5879c642015-07-10 10:41:36 +020042 uint8_t use_trx,
Maxe9fe0e32017-09-28 15:56:05 +020043 uint8_t ms_class, uint8_t egprs_ms_class, bool single_slot)
Daniel Willmann48aa0b02014-07-16 18:54:10 +020044{
45 if (dir == GPRS_RLCMAC_UL_TBF)
Jacob Erlbeck86b6f052015-11-27 15:17:34 +010046 return tbf_alloc_ul_tbf(bts, ms, use_trx,
47 ms_class, egprs_ms_class, single_slot);
Daniel Willmann48aa0b02014-07-16 18:54:10 +020048 else
Jacob Erlbeck86b6f052015-11-27 15:17:34 +010049 return tbf_alloc_dl_tbf(bts, ms, use_trx,
50 ms_class, egprs_ms_class, single_slot);
Daniel Willmann48aa0b02014-07-16 18:54:10 +020051}
52
Jacob Erlbeck61205a72015-07-09 11:35:50 +020053static void check_tfi_usage(BTS *the_bts)
54{
55 int pdch_no;
Jacob Erlbeck61205a72015-07-09 11:35:50 +020056
57 struct gprs_rlcmac_tbf *tfi_usage[8][8][2][32] = {{{{NULL}}}};
Jacob Erlbecked2dbf62015-12-28 19:15:40 +010058 LListHead<gprs_rlcmac_tbf> *tbf_lists[2] = {
59 &the_bts->ul_tbfs(),
60 &the_bts->dl_tbfs()
Jacob Erlbeck61205a72015-07-09 11:35:50 +020061 };
62
Jacob Erlbecked2dbf62015-12-28 19:15:40 +010063 LListHead<gprs_rlcmac_tbf> *pos;
Jacob Erlbeck61205a72015-07-09 11:35:50 +020064 gprs_rlcmac_tbf *tbf;
Jacob Erlbeck61205a72015-07-09 11:35:50 +020065 unsigned list_idx;
66 struct gprs_rlcmac_tbf **tbf_var;
67
68 for (list_idx = 0; list_idx < ARRAY_SIZE(tbf_lists); list_idx += 1)
69 {
70
Jacob Erlbecked2dbf62015-12-28 19:15:40 +010071 llist_for_each(pos, tbf_lists[list_idx]) {
72 tbf = pos->entry();
Jacob Erlbeck61205a72015-07-09 11:35:50 +020073 for (pdch_no = 0; pdch_no < 8; pdch_no += 1) {
74 struct gprs_rlcmac_pdch *pdch = tbf->pdch[pdch_no];
75 if (pdch == NULL)
76 continue;
77
78 tbf_var = &tfi_usage
79 [tbf->trx->trx_no]
80 [pdch_no]
81 [tbf->direction]
82 [tbf->tfi()];
83
84 OSMO_ASSERT(*tbf_var == NULL);
85 if (tbf->direction == GPRS_RLCMAC_DL_TBF) {
86 OSMO_ASSERT(pdch->dl_tbf_by_tfi(
87 tbf->tfi()) == tbf);
Jacob Erlbeck61205a72015-07-09 11:35:50 +020088 OSMO_ASSERT(the_bts->dl_tbf_by_tfi(
89 tbf->tfi(),
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +020090 tbf->trx->trx_no,
91 pdch_no) == tbf);
Jacob Erlbeck61205a72015-07-09 11:35:50 +020092 } else {
93 OSMO_ASSERT(pdch->ul_tbf_by_tfi(
94 tbf->tfi()) == tbf);
Jacob Erlbeck61205a72015-07-09 11:35:50 +020095 OSMO_ASSERT(the_bts->ul_tbf_by_tfi(
96 tbf->tfi(),
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +020097 tbf->trx->trx_no,
98 pdch_no) == tbf);
Jacob Erlbeck61205a72015-07-09 11:35:50 +020099 }
100 *tbf_var = tbf;
Jacob Erlbeck47a57f62015-07-08 12:53:16 +0200101 OSMO_ASSERT(pdch->assigned_tfi(tbf->direction) &
102 (1 << tbf->tfi()));
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200103 }
104 }
105 }
106}
107
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200108static void test_alloc_a(gprs_rlcmac_tbf_direction dir,
109 uint8_t slots, const int count)
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200110{
111 int tfi;
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200112 int i;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200113 uint8_t used_trx, tmp_trx;
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200114 BTS the_bts;
115 struct gprs_rlcmac_bts *bts;
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200116 struct gprs_rlcmac_tbf *tbfs[32*8+1] = { 0, };
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200117
118 printf("Testing alloc_a direction(%d)\n", dir);
119
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200120 bts = the_bts.bts_data();
121 bts->alloc_algorithm = alloc_algorithm_a;
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200122
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200123 struct gprs_rlcmac_trx *trx = &bts->trx[0];
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200124 for (i = 0; i < 8; i += 1)
125 if (slots & (1 << i))
126 trx->pdch[i].enable();
127
128 OSMO_ASSERT(count >= 0 && count <= (int)ARRAY_SIZE(tbfs));
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200129
130 /**
131 * Currently alloc_a will only allocate from the first
132 * PDCH and all possible usf's. We run out of usf's before
133 * we are out of tfi's. Observe this and make sure that at
134 * least this part is working okay.
135 */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200136 for (i = 0; i < (int)ARRAY_SIZE(tbfs); ++i) {
Jacob Erlbeck86b6f052015-11-27 15:17:34 +0100137 tbfs[i] = tbf_alloc(bts, NULL, dir, -1, 0, 0, 0);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200138 if (tbfs[i] == NULL)
139 break;
140
141 used_trx = tbfs[i]->trx->trx_no;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200142 tfi = the_bts.tfi_find_free(dir, &tmp_trx, used_trx);
143 OSMO_ASSERT(tbfs[i]->tfi() != tfi);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200144 }
145
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200146 check_tfi_usage(&the_bts);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200147
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200148 OSMO_ASSERT(i == count);
149
150 for (i = 0; i < count; ++i)
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200151 if (tbfs[i])
152 tbf_free(tbfs[i]);
153
Holger Hans Peter Freytherf3f1bde2016-02-22 15:14:01 +0100154 tbfs[0] = tbf_alloc(bts, NULL, dir, -1, 0, 0, 0);
155 OSMO_ASSERT(tbfs[0]);
156 tbf_free(tbfs[0]);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200157}
158
159static void test_alloc_a()
160{
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200161 /* slots 2 - 3 */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200162 test_alloc_a(GPRS_RLCMAC_DL_TBF, 0x0c, 32*2);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200163 test_alloc_a(GPRS_RLCMAC_UL_TBF, 0x0c, 14);
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200164
165 /* slots 1 - 5 */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200166 test_alloc_a(GPRS_RLCMAC_DL_TBF, 0x1e, 32*4);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200167 test_alloc_a(GPRS_RLCMAC_UL_TBF, 0x1e, 28);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200168}
169
Max2ecf0fd2017-11-21 18:13:31 +0100170static void dump_assignment(struct gprs_rlcmac_tbf *tbf, const char *dir, bool verbose)
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100171{
Max2ecf0fd2017-11-21 18:13:31 +0100172 if (!verbose)
173 return;
174
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200175 for (size_t i = 0; i < ARRAY_SIZE(tbf->pdch); ++i)
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100176 if (tbf->pdch[i])
Neels Hofmeyrd34646a2017-02-08 17:07:40 +0100177 printf("PDCH[%zu] is used for %s\n", i, dir);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100178 printf("PDCH[%d] is control_ts for %s\n", tbf->control_ts, dir);
179 printf("PDCH[%d] is first common for %s\n", tbf->first_common_ts, dir);
180}
181
Max2ecf0fd2017-11-21 18:13:31 +0100182#define ENABLE_PDCH(ts_no, enable_flag, trx) \
183 if (enable_flag) \
184 trx->pdch[ts_no].enable();
185
186static inline void enable_ts_on_bts(struct gprs_rlcmac_bts *bts,
187 bool ts0, bool ts1, bool ts2, bool ts3, bool ts4, bool ts5, bool ts6, bool ts7)
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100188{
Max2ecf0fd2017-11-21 18:13:31 +0100189 struct gprs_rlcmac_trx *trx = &bts->trx[0];
190
191 ENABLE_PDCH(0, ts0, trx);
192 ENABLE_PDCH(1, ts1, trx);
193 ENABLE_PDCH(2, ts2, trx);
194 ENABLE_PDCH(3, ts3, trx);
195 ENABLE_PDCH(4, ts4, trx);
196 ENABLE_PDCH(5, ts5, trx);
197 ENABLE_PDCH(6, ts6, trx);
198 ENABLE_PDCH(7, ts7, trx);
199}
200
201static inline bool test_alloc_b_ul_dl(bool ts0, bool ts1, bool ts2, bool ts3, bool ts4, bool ts5, bool ts6, bool ts7,
202 uint8_t ms_class, bool verbose)
203{
204 BTS the_bts;
205 struct gprs_rlcmac_bts *bts = the_bts.bts_data();
206 gprs_rlcmac_ul_tbf *ul_tbf;
207 gprs_rlcmac_dl_tbf *dl_tbf;
208
209 if (verbose)
210 printf("Testing UL then DL assignment.\n");
211
212 bts->alloc_algorithm = alloc_algorithm_b;
213
214 enable_ts_on_bts(bts, ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7);
215
Max92b7a502018-01-26 11:01:35 +0100216 ul_tbf = tbf_alloc_ul_tbf(bts, NULL, -1, ms_class, 0, true);
Max2ecf0fd2017-11-21 18:13:31 +0100217 if (!ul_tbf)
218 return false;
219
220 OSMO_ASSERT(ul_tbf->ms());
221 OSMO_ASSERT(ul_tbf->ms()->current_trx());
222
223 dump_assignment(ul_tbf, "UL", verbose);
224
225 /* assume final ack has not been sent */
Max92b7a502018-01-26 11:01:35 +0100226 dl_tbf = tbf_alloc_dl_tbf(bts, ul_tbf->ms(), ul_tbf->ms()->current_trx()->trx_no, ms_class, 0,
227 false);
Max2ecf0fd2017-11-21 18:13:31 +0100228 if (!dl_tbf)
229 return false;
230
231 dump_assignment(dl_tbf, "DL", verbose);
232
233 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
234
235 check_tfi_usage(&the_bts);
236
237 tbf_free(dl_tbf);
238 tbf_free(ul_tbf);
239
240 return true;
241}
242
243static inline bool test_alloc_b_dl_ul(bool ts0, bool ts1, bool ts2, bool ts3, bool ts4, bool ts5, bool ts6, bool ts7,
244 uint8_t ms_class, bool verbose)
245{
246 BTS the_bts;
247 struct gprs_rlcmac_bts *bts = the_bts.bts_data();
248 gprs_rlcmac_ul_tbf *ul_tbf;
249 gprs_rlcmac_dl_tbf *dl_tbf;
250
251 if (verbose)
252 printf("Testing DL then UL assignment followed by update\n");
253
254 bts->alloc_algorithm = alloc_algorithm_b;
255
256 enable_ts_on_bts(bts, ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7);
257
Max92b7a502018-01-26 11:01:35 +0100258 dl_tbf = tbf_alloc_dl_tbf(bts, NULL, -1, ms_class, 0, true);
Max2ecf0fd2017-11-21 18:13:31 +0100259 if (!dl_tbf)
260 return false;
261
262 dl_tbf->update_ms(0x23, GPRS_RLCMAC_DL_TBF);
263
264 OSMO_ASSERT(dl_tbf->ms());
265 OSMO_ASSERT(dl_tbf->ms()->current_trx());
266
267 dump_assignment(dl_tbf, "DL", verbose);
268
Max92b7a502018-01-26 11:01:35 +0100269 ul_tbf = tbf_alloc_ul_tbf(bts, dl_tbf->ms(), dl_tbf->ms()->current_trx()->trx_no, ms_class, 0,
270 false);
Max2ecf0fd2017-11-21 18:13:31 +0100271 if (!ul_tbf)
272 return false;
273
274 ul_tbf->update_ms(0x23, GPRS_RLCMAC_UL_TBF);
275 ul_tbf->m_contention_resolution_done = 1;
276
277 dump_assignment(ul_tbf, "UL", verbose);
278
279 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
280
281 /* now update the dl_tbf */
282 dl_tbf->update();
283 dump_assignment(dl_tbf, "DL", verbose);
284 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
285
286 check_tfi_usage(&the_bts);
287
288 tbf_free(dl_tbf);
289 tbf_free(ul_tbf);
290
291 return true;
292}
293
294static inline bool test_alloc_b_jolly(uint8_t ms_class)
295{
296 BTS the_bts;
297 struct gprs_rlcmac_bts *bts = the_bts.bts_data();
298 int tfi;
299 uint8_t trx_no;
300 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
301
302 printf("Testing jolly example\n");
303
304 bts->alloc_algorithm = alloc_algorithm_b;
305
306 enable_ts_on_bts(bts, false, true, true, true, true, false, false, false);
307
308 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
309 OSMO_ASSERT(tfi >= 0);
Max92b7a502018-01-26 11:01:35 +0100310 ul_tbf = tbf_alloc_ul_tbf(bts, NULL, .1, ms_class, 0, false);
Max2ecf0fd2017-11-21 18:13:31 +0100311 if (!ul_tbf)
312 return false;
313
314 OSMO_ASSERT(ul_tbf->ms());
315 OSMO_ASSERT(ul_tbf->ms()->current_trx());
316 trx_no = ul_tbf->ms()->current_trx()->trx_no;
317 dump_assignment(ul_tbf, "UL", true);
318
319 /* assume final ack has not been sent */
Max92b7a502018-01-26 11:01:35 +0100320 dl_tbf = tbf_alloc_dl_tbf(bts, ul_tbf->ms(), trx_no, ms_class, 0,
321 false);
Max2ecf0fd2017-11-21 18:13:31 +0100322 if (!dl_tbf)
323 return false;
324
325 dump_assignment(dl_tbf, "DL", true);
326
327 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
328
329 check_tfi_usage(&the_bts);
330
331 tbf_free(dl_tbf);
332 tbf_free(ul_tbf);
333
334 return true;
335}
336
337static void test_alloc_b_for_ms(uint8_t ms_class)
338{
339 bool rc;
340
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100341 printf("Going to test multislot assignment MS_CLASS=%d\n", ms_class);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100342 /*
343 * PDCH is on TS 6,7,8 and we start with a UL allocation and
344 * then follow two DL allocations (once single, once normal).
345 *
346 * Uplink assigned and still available..
347 */
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100348
Max2ecf0fd2017-11-21 18:13:31 +0100349 rc = test_alloc_b_ul_dl(false, false, false, false, false, true, true, true, ms_class, true);
350 if (!rc)
351 return;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100352
353 /**
354 * Test with the other order.. first DL and then UL
355 */
Max2ecf0fd2017-11-21 18:13:31 +0100356 rc = test_alloc_b_dl_ul(false, false, false, false, false, true, true, true, ms_class, true);
357 if (!rc)
358 return;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100359
360 /* Andreas osmocom-pcu example */
Max2ecf0fd2017-11-21 18:13:31 +0100361 test_alloc_b_jolly(ms_class);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100362}
363
Max2ecf0fd2017-11-21 18:13:31 +0100364static void test_alloc_mass(bool ts0, bool ts1, bool ts2, bool ts3, bool ts4, bool ts5, bool ts6, bool ts7, int ms_class)
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100365{
Max2ecf0fd2017-11-21 18:13:31 +0100366 bool rc;
367
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100368 /* we can test the allocation failures differently */
369 if (!ts0 && !ts1 && !ts2 && !ts3 && !ts4 && !ts5 && !ts6 && !ts7)
370 return;
371
372 printf("Mass test: TS0(%c%c%c%c%c%c%c%c)TS7 MS_Class=%d\n",
373 ts0 ? 'O' : 'x',
374 ts1 ? 'O' : 'x',
375 ts2 ? 'O' : 'x',
376 ts3 ? 'O' : 'x',
377 ts4 ? 'O' : 'x',
378 ts5 ? 'O' : 'x',
379 ts6 ? 'O' : 'x',
380 ts7 ? 'O' : 'x', ms_class);
381 fflush(stdout);
382
Max2ecf0fd2017-11-21 18:13:31 +0100383 rc = test_alloc_b_ul_dl(ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7, ms_class, false);
384 if (!rc)
385 return;
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100386
387 /**
388 * Test with the other order.. first DL and then UL
389 */
Max2ecf0fd2017-11-21 18:13:31 +0100390 test_alloc_b_dl_ul(ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7, ms_class, false);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100391}
392
393static void test_all_alloc_b()
394{
395 /* it is a bit crazy... */
396 for (uint8_t ts0 = 0; ts0 < 2; ++ts0)
397 for (uint8_t ts1 = 0; ts1 < 2; ++ts1)
398 for (uint8_t ts2 = 0; ts2 < 2; ++ts2)
399 for (uint8_t ts3 = 0; ts3 < 2; ++ts3)
400 for (uint8_t ts4 = 0; ts4 < 2; ++ts4)
401 for (uint8_t ts5 = 0; ts5 < 2; ++ts5)
402 for (uint8_t ts6 = 0; ts6 < 2; ++ts6)
403 for (uint8_t ts7 = 0; ts7 < 2; ++ts7)
Max9f460712018-01-23 20:57:08 +0100404 for (int ms_class = 0; ms_class < mslot_class_max(); ++ms_class)
Max2ecf0fd2017-11-21 18:13:31 +0100405 test_alloc_mass(ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7, ms_class);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100406}
407
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100408static void test_alloc_b()
409{
Max9f460712018-01-23 20:57:08 +0100410 for (int i = 0; i < mslot_class_max(); ++i)
Max2ecf0fd2017-11-21 18:13:31 +0100411 test_alloc_b_for_ms(i);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100412
413 test_all_alloc_b();
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100414}
415
Maxe9fe0e32017-09-28 15:56:05 +0200416typedef int (*algo_t)(struct gprs_rlcmac_bts *bts, struct GprsMs *ms, struct gprs_rlcmac_tbf *tbf, bool single,
417 int8_t use_trx);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200418
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200419static char get_dir_char(uint8_t mask, uint8_t tx, uint8_t rx, uint8_t busy)
Jacob Erlbecke5655642015-06-29 12:19:52 +0200420{
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200421 int offs = busy ? 32 : 0;
422 return (mask & tx & rx) ? 'C' + offs :
423 (mask & tx) ? 'U' + offs :
424 (mask & rx) ? 'D' + offs :
Jacob Erlbecke5655642015-06-29 12:19:52 +0200425 '.';
426}
427
428enum test_mode {
429 TEST_MODE_UL_ONLY,
430 TEST_MODE_DL_ONLY,
431 TEST_MODE_UL_AND_DL,
432 TEST_MODE_DL_AND_UL,
433 TEST_MODE_DL_AFTER_UL,
434 TEST_MODE_UL_AFTER_DL,
435};
436
Maxc59ef122017-11-27 13:21:41 +0100437static inline char *test_mode_descr(enum test_mode t)
438{
439 switch (t) {
440 case TEST_MODE_UL_ONLY: return (char*)"UL only";
441 case TEST_MODE_DL_ONLY: return (char*)"DL only";
442 case TEST_MODE_UL_AND_DL: return (char*)"UL and DL";
443 case TEST_MODE_DL_AND_UL: return (char*)"DL and UL";
444 case TEST_MODE_DL_AFTER_UL: return (char*)"DL after UL";
445 case TEST_MODE_UL_AFTER_DL: return (char*)"UL after DL";
446 default: return NULL;
447 }
448}
449
Jacob Erlbecke5655642015-06-29 12:19:52 +0200450static GprsMs *alloc_tbfs(BTS *the_bts, GprsMs *ms, unsigned ms_class,
451 enum test_mode mode)
452{
453 struct gprs_rlcmac_bts *bts;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200454 uint8_t trx_no = -1;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200455
456 bts = the_bts->bts_data();
457
458 gprs_rlcmac_tbf *tbf = NULL;
459
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200460 if (ms && ms->current_trx())
461 trx_no = ms->current_trx()->trx_no;
462
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200463 GprsMs::Guard guard1(ms);
464
Jacob Erlbecke5655642015-06-29 12:19:52 +0200465 /* Allocate what is needed first */
466 switch (mode) {
467 case TEST_MODE_UL_ONLY:
468 case TEST_MODE_DL_AFTER_UL:
469 case TEST_MODE_UL_AND_DL:
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200470 if (ms && ms->ul_tbf())
471 tbf_free(ms->ul_tbf());
Max92b7a502018-01-26 11:01:35 +0100472 tbf = tbf_alloc_ul_tbf(bts, ms, trx_no, ms_class, 0, false);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200473 if (tbf == NULL)
474 return NULL;
475 break;
476 case TEST_MODE_DL_ONLY:
477 case TEST_MODE_UL_AFTER_DL:
478 case TEST_MODE_DL_AND_UL:
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200479 if (ms && ms->dl_tbf())
480 tbf_free(ms->dl_tbf());
Max92b7a502018-01-26 11:01:35 +0100481 tbf = tbf_alloc_dl_tbf(bts, ms, trx_no, ms_class, 0, false);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200482 if (tbf == NULL)
483 return NULL;
484 }
485
486 OSMO_ASSERT(tbf);
487 OSMO_ASSERT(tbf->ms());
488 OSMO_ASSERT(ms == NULL || ms == tbf->ms());
489 ms = tbf->ms();
490
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200491 GprsMs::Guard guard2(ms);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200492
Jacob Erlbeck14376a72015-07-07 11:31:28 +0200493 /* Continue with what is needed next */
494 switch (mode) {
495 case TEST_MODE_UL_ONLY:
496 case TEST_MODE_DL_ONLY:
497 /* We are done */
498 break;
499
500 case TEST_MODE_DL_AFTER_UL:
501 case TEST_MODE_UL_AND_DL:
502 ms = alloc_tbfs(the_bts, ms, ms_class, TEST_MODE_DL_ONLY);
503 break;
504
505 case TEST_MODE_UL_AFTER_DL:
506 case TEST_MODE_DL_AND_UL:
507 ms = alloc_tbfs(the_bts, ms, ms_class, TEST_MODE_UL_ONLY);
508 break;
509 }
510
Jacob Erlbecke5655642015-06-29 12:19:52 +0200511 /* Optionally delete the TBF */
512 switch (mode) {
513 case TEST_MODE_DL_AFTER_UL:
514 case TEST_MODE_UL_AFTER_DL:
515 tbf_free(tbf);
Jacob Erlbeck0f352a62015-07-16 18:23:33 +0200516 tbf = NULL;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200517 break;
518
519 default:
520 break;
521 }
522
Jacob Erlbeck0f352a62015-07-16 18:23:33 +0200523 if (!ms && tbf)
524 tbf_free(tbf);
525
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200526 return guard2.is_idle() ? NULL : ms;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200527}
528
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200529static unsigned alloc_many_tbfs(BTS *the_bts, unsigned min_class,
530 unsigned max_class, enum test_mode mode)
Jacob Erlbecke5655642015-06-29 12:19:52 +0200531{
Jacob Erlbecke5655642015-06-29 12:19:52 +0200532 unsigned counter;
533 unsigned ms_class = min_class;
534
Jacob Erlbecke5655642015-06-29 12:19:52 +0200535 for (counter = 0; 1; counter += 1) {
536 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
537 uint8_t ul_slots = 0;
538 uint8_t dl_slots = 0;
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200539 uint8_t busy_slots = 0;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200540 unsigned i;
541 int tfi = -1;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200542 int tfi2;
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200543 uint8_t trx_no2;
544 struct gprs_rlcmac_trx *trx;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200545 GprsMs *ms;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200546 enum gprs_rlcmac_tbf_direction dir;
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200547 uint32_t tlli = counter + 0xc0000000;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200548
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200549 ms = the_bts->ms_by_tlli(tlli);
550
551 ms = alloc_tbfs(the_bts, ms, ms_class, mode);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200552 if (!ms)
553 break;
554
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200555 ms->set_tlli(tlli);
556
Jacob Erlbecke5655642015-06-29 12:19:52 +0200557 ul_tbf = ms->ul_tbf();
558 dl_tbf = ms->dl_tbf();
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200559 trx = ms->current_trx();
560
561 OSMO_ASSERT(ul_tbf || dl_tbf);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200562
563 if (ul_tbf) {
564 ul_slots = 1 << ul_tbf->first_common_ts;
565 tfi = ul_tbf->tfi();
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200566 dir = GPRS_RLCMAC_UL_TBF;
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200567 } else {
Jacob Erlbecke5655642015-06-29 12:19:52 +0200568 ul_slots = 1 << dl_tbf->first_common_ts;
569 tfi = dl_tbf->tfi();
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200570 dir = GPRS_RLCMAC_DL_TBF;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200571 }
572
573 for (i = 0; dl_tbf && i < ARRAY_SIZE(dl_tbf->pdch); i += 1)
574 if (dl_tbf->pdch[i])
575 dl_slots |= 1 << i;
576
Max5b0df1f2017-09-11 10:38:59 +0200577 for (i = 0; ul_tbf && i < ARRAY_SIZE(ul_tbf->pdch); i += 1)
578 if (ul_tbf->pdch[i])
579 ul_slots |= 1 << i;
580
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200581 for (i = 0; trx && i < ARRAY_SIZE(trx->pdch); i += 1) {
582 struct gprs_rlcmac_pdch *pdch = &trx->pdch[i];
583
584 if (ul_tbf && dl_tbf)
585 continue;
586
587 if (ul_tbf &&
588 pdch->assigned_tfi(GPRS_RLCMAC_DL_TBF) != 0xffffffff)
589 continue;
590
591 if (dl_tbf &&
592 pdch->assigned_tfi(GPRS_RLCMAC_UL_TBF) != 0xffffffff)
593 continue;
594
595 busy_slots |= 1 << i;
596 }
597
Jacob Erlbecke5655642015-06-29 12:19:52 +0200598 printf(" TBF[%d] class %d reserves %c%c%c%c%c%c%c%c\n",
599 tfi, ms_class,
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200600 get_dir_char(0x01, ul_slots, dl_slots, busy_slots),
601 get_dir_char(0x02, ul_slots, dl_slots, busy_slots),
602 get_dir_char(0x04, ul_slots, dl_slots, busy_slots),
603 get_dir_char(0x08, ul_slots, dl_slots, busy_slots),
604 get_dir_char(0x10, ul_slots, dl_slots, busy_slots),
605 get_dir_char(0x20, ul_slots, dl_slots, busy_slots),
606 get_dir_char(0x40, ul_slots, dl_slots, busy_slots),
607 get_dir_char(0x80, ul_slots, dl_slots, busy_slots));
Jacob Erlbecke5655642015-06-29 12:19:52 +0200608
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200609 if (tfi >= 0) {
610 OSMO_ASSERT(ms->current_trx());
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200611 tfi2 = the_bts->tfi_find_free(dir, &trx_no2,
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200612 ms->current_trx()->trx_no);
613 OSMO_ASSERT(tfi != tfi2);
Jacob Erlbeck7b3675b2015-07-16 18:28:22 +0200614 OSMO_ASSERT(tfi2 < 0 ||
615 trx_no2 == ms->current_trx()->trx_no);
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200616 }
617
Jacob Erlbecke5655642015-06-29 12:19:52 +0200618 ms_class += 1;
619 if (ms_class > max_class)
620 ms_class = min_class;
621 }
622
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200623 return counter;
624}
625
626static void test_successive_allocation(algo_t algo, unsigned min_class,
627 unsigned max_class, enum test_mode mode,
628 unsigned expect_num, const char *text)
629{
630 BTS the_bts;
631 struct gprs_rlcmac_bts *bts;
632 struct gprs_rlcmac_trx *trx;
633 unsigned counter;
634
Maxc59ef122017-11-27 13:21:41 +0100635 printf("Going to test assignment with many TBF, algorithm %s class %u..%u (%s)\n",
636 text, min_class, max_class, test_mode_descr(mode));
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200637
638 bts = the_bts.bts_data();
639 bts->alloc_algorithm = algo;
640
641 trx = &bts->trx[0];
642 trx->pdch[3].enable();
643 trx->pdch[4].enable();
644 trx->pdch[5].enable();
645 trx->pdch[6].enable();
646 trx->pdch[7].enable();
647
648 counter = alloc_many_tbfs(&the_bts, min_class, max_class, mode);
649
Maxc59ef122017-11-27 13:21:41 +0100650 printf(" Successfully allocated %u UL TBFs, algorithm %s class %u..%u (%s)\n",
651 counter, text, min_class, max_class, test_mode_descr(mode));
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200652 if (counter != expect_num)
Maxc59ef122017-11-27 13:21:41 +0100653 fprintf(stderr, " Expected %u TBFs (got %u), algorithm %s class %u..%u (%s)\n",
654 expect_num, counter, text, min_class, max_class, test_mode_descr(mode));
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200655
Jacob Erlbeckec478752015-06-19 16:35:38 +0200656 OSMO_ASSERT(counter == expect_num);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200657
658 check_tfi_usage(&the_bts);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200659}
660
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200661static void test_many_connections(algo_t algo, unsigned expect_num,
662 const char *text)
663{
664 BTS the_bts;
665 struct gprs_rlcmac_bts *bts;
666 struct gprs_rlcmac_trx *trx;
667 int counter1, counter2 = -1;
668 unsigned i;
669 enum test_mode mode_seq[] = {
670 TEST_MODE_DL_AFTER_UL,
671 TEST_MODE_UL_ONLY,
672 TEST_MODE_DL_AFTER_UL,
673 TEST_MODE_DL_ONLY,
674 };
675
Maxc59ef122017-11-27 13:21:41 +0100676 printf("Going to test assignment with many connections, algorithm %s\n", text);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200677
678 bts = the_bts.bts_data();
679 bts->alloc_algorithm = algo;
680
681 trx = &bts->trx[0];
682 trx->pdch[3].enable();
683 trx->pdch[4].enable();
684 trx->pdch[5].enable();
685 trx->pdch[6].enable();
686 trx->pdch[7].enable();
687
688 for (i = 0; i < ARRAY_SIZE(mode_seq); i += 1) {
Max9f460712018-01-23 20:57:08 +0100689 counter1 = alloc_many_tbfs(&the_bts, 1, mslot_class_max(), mode_seq[i]);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200690 fprintf(stderr, " Allocated %d TBFs (previously %d)\n",
691 counter1, counter2);
692
693 check_tfi_usage(&the_bts);
694
695 /* This will stop earlier due to USF shortage */
696 if (mode_seq[i] == TEST_MODE_UL_ONLY)
697 continue;
698
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200699 if (counter2 >= 0) {
700 if (counter1 < counter2)
701 fprintf(stderr, " Expected %d >= %d in %s\n",
702 counter1, counter2, text);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200703 OSMO_ASSERT(counter1 >= counter2);
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200704 }
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200705
706 counter2 = counter1;
707 }
708
709 printf(" Successfully allocated %d TBFs\n", counter1);
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200710 if (counter1 != (int)expect_num)
Maxc59ef122017-11-27 13:21:41 +0100711 fprintf(stderr, " Expected %d TBFs (got %d) for algorithm %s\n", expect_num, counter1, text);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200712
713 OSMO_ASSERT(expect_num == (unsigned)counter1);
714}
715
Maxc59ef122017-11-27 13:21:41 +0100716static inline void test_a_b_dyn(enum test_mode mode, uint8_t exp_A, uint8_t exp_B, uint8_t exp_dyn)
Jacob Erlbecke5655642015-06-29 12:19:52 +0200717{
Maxc59ef122017-11-27 13:21:41 +0100718 test_successive_allocation(alloc_algorithm_a, 1, 1, mode, exp_A, "A");
719 test_successive_allocation(alloc_algorithm_b, 10, 10, mode, exp_B, "B");
720 test_successive_allocation(alloc_algorithm_dynamic, 10, 10, mode, exp_dyn, "dynamic");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200721}
722
Maxc59ef122017-11-27 13:21:41 +0100723static void test_successive_allocations()
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200724{
Maxc59ef122017-11-27 13:21:41 +0100725 test_successive_allocation(alloc_algorithm_a, 1, 1, TEST_MODE_UL_AND_DL, 35, "A");
726 test_successive_allocation(alloc_algorithm_b, 10, 10, TEST_MODE_UL_AND_DL, 32, "B");
727 test_successive_allocation(alloc_algorithm_b, 12, 12, TEST_MODE_UL_AND_DL, 32, "B");
728
Max9f460712018-01-23 20:57:08 +0100729 test_successive_allocation(alloc_algorithm_b, 1, 12, TEST_MODE_UL_AND_DL, 32, "B");
Max01bd0cc2018-01-23 20:58:49 +0100730 test_successive_allocation(alloc_algorithm_b, 1, mslot_class_max(), TEST_MODE_UL_AND_DL, 32, "B");
Max9f460712018-01-23 20:57:08 +0100731 test_successive_allocation(alloc_algorithm_dynamic, 1, mslot_class_max(), TEST_MODE_UL_AND_DL, 35, "dynamic");
Maxc59ef122017-11-27 13:21:41 +0100732
733 test_a_b_dyn(TEST_MODE_DL_AND_UL, 35, 32, 32);
734 test_a_b_dyn(TEST_MODE_DL_AFTER_UL, 160, 32, 95);
735 test_a_b_dyn(TEST_MODE_UL_AFTER_DL, 35, 32, 35);
736 test_a_b_dyn(TEST_MODE_UL_ONLY, 35, 32, 35);
737 test_a_b_dyn(TEST_MODE_DL_ONLY, 160, 32, 101);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200738}
739
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530740static void test_2_consecutive_dl_tbfs()
741{
742 BTS the_bts;
743 struct gprs_rlcmac_bts *bts;
744 struct gprs_rlcmac_trx *trx;
745 uint8_t ms_class = 11;
746 uint8_t egprs_ms_class = 11;
747 gprs_rlcmac_tbf *dl_tbf1, *dl_tbf2;
748 uint8_t numTs1 = 0, numTs2 = 0;
749
750 printf("Testing DL TS allocation for Multi UEs\n");
751
752 bts = the_bts.bts_data();
753 bts->alloc_algorithm = alloc_algorithm_b;
754
755 trx = &bts->trx[0];
756 trx->pdch[4].enable();
757 trx->pdch[5].enable();
758 trx->pdch[6].enable();
759 trx->pdch[7].enable();
760
Max92b7a502018-01-26 11:01:35 +0100761 dl_tbf1 = tbf_alloc_dl_tbf(bts, NULL, 0, ms_class, egprs_ms_class,
762 false);
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530763 OSMO_ASSERT(dl_tbf1);
764
765 for (int i = 0; i < 8; i++) {
766 if (dl_tbf1->pdch[i])
767 numTs1++;
768 }
769 OSMO_ASSERT(numTs1 == 4);
770 printf("TBF1: numTs(%d)\n", numTs1);
771
Max92b7a502018-01-26 11:01:35 +0100772 dl_tbf2 = tbf_alloc_dl_tbf(bts, NULL, 0, ms_class, egprs_ms_class,
773 false);
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530774 OSMO_ASSERT(dl_tbf2);
775
776 for (int i = 0; i < 8; i++) {
777 if (dl_tbf2->pdch[i])
778 numTs2++;
779 }
780
781 /*
782 * TODO: currently 2nd DL TBF gets 3 TS
783 * This behaviour will be fixed in subsequent patch
784 */
785 printf("TBF2: numTs(%d)\n", numTs2);
786 OSMO_ASSERT(numTs2 == 3);
787
788 tbf_free(dl_tbf1);
789 tbf_free(dl_tbf2);
790}
791
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200792int main(int argc, char **argv)
793{
794 tall_pcu_ctx = talloc_named_const(NULL, 1, "moiji-mobile AllocTest context");
795 if (!tall_pcu_ctx)
796 abort();
797
Neels Hofmeyr78ce5912017-02-08 17:07:31 +0100798 msgb_talloc_ctx_init(tall_pcu_ctx, 0);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200799 osmo_init_logging(&gprs_log_info);
800 log_set_use_color(osmo_stderr_target, 0);
801 log_set_print_filename(osmo_stderr_target, 0);
Jacob Erlbeck9ec49e22015-06-29 13:00:20 +0200802 if (getenv("LOGL_DEBUG"))
803 log_set_log_level(osmo_stderr_target, LOGL_DEBUG);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200804
805 test_alloc_a();
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100806 test_alloc_b();
Maxc59ef122017-11-27 13:21:41 +0100807 test_successive_allocations();
808 test_many_connections(alloc_algorithm_a, 160, "A");
Max01bd0cc2018-01-23 20:58:49 +0100809 test_many_connections(alloc_algorithm_b, 32, "B");
Maxc59ef122017-11-27 13:21:41 +0100810 test_many_connections(alloc_algorithm_dynamic, 160, "dynamic");
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530811 test_2_consecutive_dl_tbfs();
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200812 return EXIT_SUCCESS;
813}
814
815/*
816 * stubs that should not be reached
817 */
818extern "C" {
819void l1if_pdch_req() { abort(); }
820void l1if_connect_pdch() { abort(); }
821void l1if_close_pdch() { abort(); }
822void l1if_open_pdch() { abort(); }
823}