blob: 2fdee3f21dd14fe52fa9f7e671554bd7cefe6371 [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"
Pau Espin Pedrol9d1cdb12019-09-25 17:47:02 +020023#include "tbf_ul.h"
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +020024#include "bts.h"
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020025
26#include <string.h>
27#include <stdio.h>
28
29extern "C" {
Max9f460712018-01-23 20:57:08 +010030#include "mslot_class.h"
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020031#include <osmocom/core/application.h>
32#include <osmocom/core/msgb.h>
33#include <osmocom/core/talloc.h>
34#include <osmocom/core/utils.h>
35}
36
37/* globals used by the code */
38void *tall_pcu_ctx;
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020039int16_t spoof_mnc = 0, spoof_mcc = 0;
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +010040bool spoof_mnc_3_digits = false;
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020041
Daniel Willmann48aa0b02014-07-16 18:54:10 +020042static gprs_rlcmac_tbf *tbf_alloc(struct gprs_rlcmac_bts *bts,
Jacob Erlbecke2e004e2015-06-18 17:16:26 +020043 GprsMs *ms, gprs_rlcmac_tbf_direction dir,
Jacob Erlbeck5879c642015-07-10 10:41:36 +020044 uint8_t use_trx,
Maxe9fe0e32017-09-28 15:56:05 +020045 uint8_t ms_class, uint8_t egprs_ms_class, bool single_slot)
Daniel Willmann48aa0b02014-07-16 18:54:10 +020046{
47 if (dir == GPRS_RLCMAC_UL_TBF)
Jacob Erlbeck86b6f052015-11-27 15:17:34 +010048 return tbf_alloc_ul_tbf(bts, ms, use_trx,
49 ms_class, egprs_ms_class, single_slot);
Daniel Willmann48aa0b02014-07-16 18:54:10 +020050 else
Jacob Erlbeck86b6f052015-11-27 15:17:34 +010051 return tbf_alloc_dl_tbf(bts, ms, use_trx,
52 ms_class, egprs_ms_class, single_slot);
Daniel Willmann48aa0b02014-07-16 18:54:10 +020053}
54
Jacob Erlbeck61205a72015-07-09 11:35:50 +020055static void check_tfi_usage(BTS *the_bts)
56{
57 int pdch_no;
Jacob Erlbeck61205a72015-07-09 11:35:50 +020058
59 struct gprs_rlcmac_tbf *tfi_usage[8][8][2][32] = {{{{NULL}}}};
Jacob Erlbecked2dbf62015-12-28 19:15:40 +010060 LListHead<gprs_rlcmac_tbf> *tbf_lists[2] = {
61 &the_bts->ul_tbfs(),
62 &the_bts->dl_tbfs()
Jacob Erlbeck61205a72015-07-09 11:35:50 +020063 };
64
Jacob Erlbecked2dbf62015-12-28 19:15:40 +010065 LListHead<gprs_rlcmac_tbf> *pos;
Jacob Erlbeck61205a72015-07-09 11:35:50 +020066 gprs_rlcmac_tbf *tbf;
Jacob Erlbeck61205a72015-07-09 11:35:50 +020067 unsigned list_idx;
68 struct gprs_rlcmac_tbf **tbf_var;
69
70 for (list_idx = 0; list_idx < ARRAY_SIZE(tbf_lists); list_idx += 1)
71 {
72
Jacob Erlbecked2dbf62015-12-28 19:15:40 +010073 llist_for_each(pos, tbf_lists[list_idx]) {
74 tbf = pos->entry();
Jacob Erlbeck61205a72015-07-09 11:35:50 +020075 for (pdch_no = 0; pdch_no < 8; pdch_no += 1) {
76 struct gprs_rlcmac_pdch *pdch = tbf->pdch[pdch_no];
77 if (pdch == NULL)
78 continue;
79
80 tbf_var = &tfi_usage
81 [tbf->trx->trx_no]
82 [pdch_no]
83 [tbf->direction]
84 [tbf->tfi()];
85
86 OSMO_ASSERT(*tbf_var == NULL);
87 if (tbf->direction == GPRS_RLCMAC_DL_TBF) {
88 OSMO_ASSERT(pdch->dl_tbf_by_tfi(
89 tbf->tfi()) == tbf);
Jacob Erlbeck61205a72015-07-09 11:35:50 +020090 OSMO_ASSERT(the_bts->dl_tbf_by_tfi(
91 tbf->tfi(),
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +020092 tbf->trx->trx_no,
93 pdch_no) == tbf);
Jacob Erlbeck61205a72015-07-09 11:35:50 +020094 } else {
95 OSMO_ASSERT(pdch->ul_tbf_by_tfi(
96 tbf->tfi()) == tbf);
Jacob Erlbeck61205a72015-07-09 11:35:50 +020097 OSMO_ASSERT(the_bts->ul_tbf_by_tfi(
98 tbf->tfi(),
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +020099 tbf->trx->trx_no,
100 pdch_no) == tbf);
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200101 }
102 *tbf_var = tbf;
Jacob Erlbeck47a57f62015-07-08 12:53:16 +0200103 OSMO_ASSERT(pdch->assigned_tfi(tbf->direction) &
104 (1 << tbf->tfi()));
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200105 }
106 }
107 }
108}
109
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200110static void test_alloc_a(gprs_rlcmac_tbf_direction dir,
111 uint8_t slots, const int count)
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200112{
113 int tfi;
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200114 int i;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200115 uint8_t used_trx, tmp_trx;
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200116 BTS the_bts;
117 struct gprs_rlcmac_bts *bts;
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200118 struct gprs_rlcmac_tbf *tbfs[32*8+1] = { 0, };
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200119
120 printf("Testing alloc_a direction(%d)\n", dir);
121
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200122 bts = the_bts.bts_data();
123 bts->alloc_algorithm = alloc_algorithm_a;
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200124
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200125 struct gprs_rlcmac_trx *trx = &bts->trx[0];
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200126 for (i = 0; i < 8; i += 1)
127 if (slots & (1 << i))
128 trx->pdch[i].enable();
129
130 OSMO_ASSERT(count >= 0 && count <= (int)ARRAY_SIZE(tbfs));
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200131
132 /**
133 * Currently alloc_a will only allocate from the first
134 * PDCH and all possible usf's. We run out of usf's before
135 * we are out of tfi's. Observe this and make sure that at
136 * least this part is working okay.
137 */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200138 for (i = 0; i < (int)ARRAY_SIZE(tbfs); ++i) {
Jacob Erlbeck86b6f052015-11-27 15:17:34 +0100139 tbfs[i] = tbf_alloc(bts, NULL, dir, -1, 0, 0, 0);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200140 if (tbfs[i] == NULL)
141 break;
142
143 used_trx = tbfs[i]->trx->trx_no;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200144 tfi = the_bts.tfi_find_free(dir, &tmp_trx, used_trx);
145 OSMO_ASSERT(tbfs[i]->tfi() != tfi);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200146 }
147
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200148 check_tfi_usage(&the_bts);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200149
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200150 OSMO_ASSERT(i == count);
151
152 for (i = 0; i < count; ++i)
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200153 if (tbfs[i])
154 tbf_free(tbfs[i]);
155
Holger Hans Peter Freytherf3f1bde2016-02-22 15:14:01 +0100156 tbfs[0] = tbf_alloc(bts, NULL, dir, -1, 0, 0, 0);
157 OSMO_ASSERT(tbfs[0]);
158 tbf_free(tbfs[0]);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200159}
160
161static void test_alloc_a()
162{
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200163 /* slots 2 - 3 */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200164 test_alloc_a(GPRS_RLCMAC_DL_TBF, 0x0c, 32*2);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200165 test_alloc_a(GPRS_RLCMAC_UL_TBF, 0x0c, 14);
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200166
167 /* slots 1 - 5 */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200168 test_alloc_a(GPRS_RLCMAC_DL_TBF, 0x1e, 32*4);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200169 test_alloc_a(GPRS_RLCMAC_UL_TBF, 0x1e, 28);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200170}
171
Max2ecf0fd2017-11-21 18:13:31 +0100172static void dump_assignment(struct gprs_rlcmac_tbf *tbf, const char *dir, bool verbose)
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100173{
Max2ecf0fd2017-11-21 18:13:31 +0100174 if (!verbose)
175 return;
176
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200177 for (size_t i = 0; i < ARRAY_SIZE(tbf->pdch); ++i)
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100178 if (tbf->pdch[i])
Neels Hofmeyrd34646a2017-02-08 17:07:40 +0100179 printf("PDCH[%zu] is used for %s\n", i, dir);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100180 printf("PDCH[%d] is control_ts for %s\n", tbf->control_ts, dir);
181 printf("PDCH[%d] is first common for %s\n", tbf->first_common_ts, dir);
182}
183
Max2ecf0fd2017-11-21 18:13:31 +0100184#define ENABLE_PDCH(ts_no, enable_flag, trx) \
185 if (enable_flag) \
186 trx->pdch[ts_no].enable();
187
188static inline void enable_ts_on_bts(struct gprs_rlcmac_bts *bts,
189 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 +0100190{
Max2ecf0fd2017-11-21 18:13:31 +0100191 struct gprs_rlcmac_trx *trx = &bts->trx[0];
192
193 ENABLE_PDCH(0, ts0, trx);
194 ENABLE_PDCH(1, ts1, trx);
195 ENABLE_PDCH(2, ts2, trx);
196 ENABLE_PDCH(3, ts3, trx);
197 ENABLE_PDCH(4, ts4, trx);
198 ENABLE_PDCH(5, ts5, trx);
199 ENABLE_PDCH(6, ts6, trx);
200 ENABLE_PDCH(7, ts7, trx);
201}
202
203static inline bool test_alloc_b_ul_dl(bool ts0, bool ts1, bool ts2, bool ts3, bool ts4, bool ts5, bool ts6, bool ts7,
204 uint8_t ms_class, bool verbose)
205{
206 BTS the_bts;
207 struct gprs_rlcmac_bts *bts = the_bts.bts_data();
208 gprs_rlcmac_ul_tbf *ul_tbf;
209 gprs_rlcmac_dl_tbf *dl_tbf;
210
211 if (verbose)
212 printf("Testing UL then DL assignment.\n");
213
214 bts->alloc_algorithm = alloc_algorithm_b;
215
216 enable_ts_on_bts(bts, ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7);
217
Max92b7a502018-01-26 11:01:35 +0100218 ul_tbf = tbf_alloc_ul_tbf(bts, NULL, -1, ms_class, 0, true);
Max2ecf0fd2017-11-21 18:13:31 +0100219 if (!ul_tbf)
220 return false;
221
222 OSMO_ASSERT(ul_tbf->ms());
223 OSMO_ASSERT(ul_tbf->ms()->current_trx());
224
225 dump_assignment(ul_tbf, "UL", verbose);
226
227 /* assume final ack has not been sent */
Max92b7a502018-01-26 11:01:35 +0100228 dl_tbf = tbf_alloc_dl_tbf(bts, ul_tbf->ms(), ul_tbf->ms()->current_trx()->trx_no, ms_class, 0,
229 false);
Max2ecf0fd2017-11-21 18:13:31 +0100230 if (!dl_tbf)
231 return false;
232
233 dump_assignment(dl_tbf, "DL", verbose);
234
235 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
236
237 check_tfi_usage(&the_bts);
238
239 tbf_free(dl_tbf);
240 tbf_free(ul_tbf);
241
242 return true;
243}
244
245static inline bool test_alloc_b_dl_ul(bool ts0, bool ts1, bool ts2, bool ts3, bool ts4, bool ts5, bool ts6, bool ts7,
246 uint8_t ms_class, bool verbose)
247{
248 BTS the_bts;
249 struct gprs_rlcmac_bts *bts = the_bts.bts_data();
250 gprs_rlcmac_ul_tbf *ul_tbf;
251 gprs_rlcmac_dl_tbf *dl_tbf;
252
253 if (verbose)
254 printf("Testing DL then UL assignment followed by update\n");
255
256 bts->alloc_algorithm = alloc_algorithm_b;
257
258 enable_ts_on_bts(bts, ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7);
259
Max92b7a502018-01-26 11:01:35 +0100260 dl_tbf = tbf_alloc_dl_tbf(bts, NULL, -1, ms_class, 0, true);
Max2ecf0fd2017-11-21 18:13:31 +0100261 if (!dl_tbf)
262 return false;
263
264 dl_tbf->update_ms(0x23, GPRS_RLCMAC_DL_TBF);
265
266 OSMO_ASSERT(dl_tbf->ms());
267 OSMO_ASSERT(dl_tbf->ms()->current_trx());
268
269 dump_assignment(dl_tbf, "DL", verbose);
270
Max92b7a502018-01-26 11:01:35 +0100271 ul_tbf = tbf_alloc_ul_tbf(bts, dl_tbf->ms(), dl_tbf->ms()->current_trx()->trx_no, ms_class, 0,
272 false);
Max2ecf0fd2017-11-21 18:13:31 +0100273 if (!ul_tbf)
274 return false;
275
276 ul_tbf->update_ms(0x23, GPRS_RLCMAC_UL_TBF);
277 ul_tbf->m_contention_resolution_done = 1;
278
279 dump_assignment(ul_tbf, "UL", verbose);
280
281 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
282
283 /* now update the dl_tbf */
284 dl_tbf->update();
285 dump_assignment(dl_tbf, "DL", verbose);
286 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
287
288 check_tfi_usage(&the_bts);
289
290 tbf_free(dl_tbf);
291 tbf_free(ul_tbf);
292
293 return true;
294}
295
296static inline bool test_alloc_b_jolly(uint8_t ms_class)
297{
298 BTS the_bts;
299 struct gprs_rlcmac_bts *bts = the_bts.bts_data();
300 int tfi;
301 uint8_t trx_no;
302 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
303
304 printf("Testing jolly example\n");
305
306 bts->alloc_algorithm = alloc_algorithm_b;
307
308 enable_ts_on_bts(bts, false, true, true, true, true, false, false, false);
309
310 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
311 OSMO_ASSERT(tfi >= 0);
Vadim Yanitskiy9f62b922020-01-25 05:14:13 +0700312 ul_tbf = tbf_alloc_ul_tbf(bts, NULL, -1, ms_class, 0, false);
Max2ecf0fd2017-11-21 18:13:31 +0100313 if (!ul_tbf)
314 return false;
315
316 OSMO_ASSERT(ul_tbf->ms());
317 OSMO_ASSERT(ul_tbf->ms()->current_trx());
318 trx_no = ul_tbf->ms()->current_trx()->trx_no;
319 dump_assignment(ul_tbf, "UL", true);
320
321 /* assume final ack has not been sent */
Max92b7a502018-01-26 11:01:35 +0100322 dl_tbf = tbf_alloc_dl_tbf(bts, ul_tbf->ms(), trx_no, ms_class, 0,
323 false);
Max2ecf0fd2017-11-21 18:13:31 +0100324 if (!dl_tbf)
325 return false;
326
327 dump_assignment(dl_tbf, "DL", true);
328
329 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
330
331 check_tfi_usage(&the_bts);
332
333 tbf_free(dl_tbf);
334 tbf_free(ul_tbf);
335
336 return true;
337}
338
339static void test_alloc_b_for_ms(uint8_t ms_class)
340{
341 bool rc;
342
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100343 printf("Going to test multislot assignment MS_CLASS=%d\n", ms_class);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100344 /*
345 * PDCH is on TS 6,7,8 and we start with a UL allocation and
346 * then follow two DL allocations (once single, once normal).
347 *
348 * Uplink assigned and still available..
349 */
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100350
Max2ecf0fd2017-11-21 18:13:31 +0100351 rc = test_alloc_b_ul_dl(false, false, false, false, false, true, true, true, ms_class, true);
352 if (!rc)
353 return;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100354
355 /**
356 * Test with the other order.. first DL and then UL
357 */
Max2ecf0fd2017-11-21 18:13:31 +0100358 rc = test_alloc_b_dl_ul(false, false, false, false, false, true, true, true, ms_class, true);
359 if (!rc)
360 return;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100361
362 /* Andreas osmocom-pcu example */
Max2ecf0fd2017-11-21 18:13:31 +0100363 test_alloc_b_jolly(ms_class);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100364}
365
Max2ecf0fd2017-11-21 18:13:31 +0100366static 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 +0100367{
Max2ecf0fd2017-11-21 18:13:31 +0100368 bool rc;
369
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100370 /* we can test the allocation failures differently */
371 if (!ts0 && !ts1 && !ts2 && !ts3 && !ts4 && !ts5 && !ts6 && !ts7)
372 return;
373
374 printf("Mass test: TS0(%c%c%c%c%c%c%c%c)TS7 MS_Class=%d\n",
375 ts0 ? 'O' : 'x',
376 ts1 ? 'O' : 'x',
377 ts2 ? 'O' : 'x',
378 ts3 ? 'O' : 'x',
379 ts4 ? 'O' : 'x',
380 ts5 ? 'O' : 'x',
381 ts6 ? 'O' : 'x',
382 ts7 ? 'O' : 'x', ms_class);
383 fflush(stdout);
384
Max2ecf0fd2017-11-21 18:13:31 +0100385 rc = test_alloc_b_ul_dl(ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7, ms_class, false);
386 if (!rc)
387 return;
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100388
389 /**
390 * Test with the other order.. first DL and then UL
391 */
Max2ecf0fd2017-11-21 18:13:31 +0100392 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 +0100393}
394
395static void test_all_alloc_b()
396{
397 /* it is a bit crazy... */
398 for (uint8_t ts0 = 0; ts0 < 2; ++ts0)
399 for (uint8_t ts1 = 0; ts1 < 2; ++ts1)
400 for (uint8_t ts2 = 0; ts2 < 2; ++ts2)
401 for (uint8_t ts3 = 0; ts3 < 2; ++ts3)
402 for (uint8_t ts4 = 0; ts4 < 2; ++ts4)
403 for (uint8_t ts5 = 0; ts5 < 2; ++ts5)
404 for (uint8_t ts6 = 0; ts6 < 2; ++ts6)
405 for (uint8_t ts7 = 0; ts7 < 2; ++ts7)
Max9f460712018-01-23 20:57:08 +0100406 for (int ms_class = 0; ms_class < mslot_class_max(); ++ms_class)
Max2ecf0fd2017-11-21 18:13:31 +0100407 test_alloc_mass(ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7, ms_class);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100408}
409
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100410static void test_alloc_b()
411{
Max9f460712018-01-23 20:57:08 +0100412 for (int i = 0; i < mslot_class_max(); ++i)
Max2ecf0fd2017-11-21 18:13:31 +0100413 test_alloc_b_for_ms(i);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100414
415 test_all_alloc_b();
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100416}
417
Maxe9fe0e32017-09-28 15:56:05 +0200418typedef int (*algo_t)(struct gprs_rlcmac_bts *bts, struct GprsMs *ms, struct gprs_rlcmac_tbf *tbf, bool single,
419 int8_t use_trx);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200420
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200421static char get_dir_char(uint8_t mask, uint8_t tx, uint8_t rx, uint8_t busy)
Jacob Erlbecke5655642015-06-29 12:19:52 +0200422{
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200423 int offs = busy ? 32 : 0;
424 return (mask & tx & rx) ? 'C' + offs :
425 (mask & tx) ? 'U' + offs :
426 (mask & rx) ? 'D' + offs :
Jacob Erlbecke5655642015-06-29 12:19:52 +0200427 '.';
428}
429
430enum test_mode {
431 TEST_MODE_UL_ONLY,
432 TEST_MODE_DL_ONLY,
433 TEST_MODE_UL_AND_DL,
434 TEST_MODE_DL_AND_UL,
435 TEST_MODE_DL_AFTER_UL,
436 TEST_MODE_UL_AFTER_DL,
437};
438
Maxc59ef122017-11-27 13:21:41 +0100439static inline char *test_mode_descr(enum test_mode t)
440{
441 switch (t) {
442 case TEST_MODE_UL_ONLY: return (char*)"UL only";
443 case TEST_MODE_DL_ONLY: return (char*)"DL only";
444 case TEST_MODE_UL_AND_DL: return (char*)"UL and DL";
445 case TEST_MODE_DL_AND_UL: return (char*)"DL and UL";
446 case TEST_MODE_DL_AFTER_UL: return (char*)"DL after UL";
447 case TEST_MODE_UL_AFTER_DL: return (char*)"UL after DL";
448 default: return NULL;
449 }
450}
451
Jacob Erlbecke5655642015-06-29 12:19:52 +0200452static GprsMs *alloc_tbfs(BTS *the_bts, GprsMs *ms, unsigned ms_class,
453 enum test_mode mode)
454{
455 struct gprs_rlcmac_bts *bts;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200456 uint8_t trx_no = -1;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200457
458 bts = the_bts->bts_data();
459
460 gprs_rlcmac_tbf *tbf = NULL;
461
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200462 if (ms && ms->current_trx())
463 trx_no = ms->current_trx()->trx_no;
464
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200465 GprsMs::Guard guard1(ms);
466
Jacob Erlbecke5655642015-06-29 12:19:52 +0200467 /* Allocate what is needed first */
468 switch (mode) {
469 case TEST_MODE_UL_ONLY:
470 case TEST_MODE_DL_AFTER_UL:
471 case TEST_MODE_UL_AND_DL:
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200472 if (ms && ms->ul_tbf())
473 tbf_free(ms->ul_tbf());
Max92b7a502018-01-26 11:01:35 +0100474 tbf = tbf_alloc_ul_tbf(bts, ms, trx_no, ms_class, 0, false);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200475 if (tbf == NULL)
476 return NULL;
477 break;
478 case TEST_MODE_DL_ONLY:
479 case TEST_MODE_UL_AFTER_DL:
480 case TEST_MODE_DL_AND_UL:
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200481 if (ms && ms->dl_tbf())
482 tbf_free(ms->dl_tbf());
Max92b7a502018-01-26 11:01:35 +0100483 tbf = tbf_alloc_dl_tbf(bts, ms, trx_no, ms_class, 0, false);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200484 if (tbf == NULL)
485 return NULL;
486 }
487
488 OSMO_ASSERT(tbf);
489 OSMO_ASSERT(tbf->ms());
490 OSMO_ASSERT(ms == NULL || ms == tbf->ms());
491 ms = tbf->ms();
492
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200493 GprsMs::Guard guard2(ms);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200494
Jacob Erlbeck14376a72015-07-07 11:31:28 +0200495 /* Continue with what is needed next */
496 switch (mode) {
497 case TEST_MODE_UL_ONLY:
498 case TEST_MODE_DL_ONLY:
499 /* We are done */
500 break;
501
502 case TEST_MODE_DL_AFTER_UL:
503 case TEST_MODE_UL_AND_DL:
504 ms = alloc_tbfs(the_bts, ms, ms_class, TEST_MODE_DL_ONLY);
505 break;
506
507 case TEST_MODE_UL_AFTER_DL:
508 case TEST_MODE_DL_AND_UL:
509 ms = alloc_tbfs(the_bts, ms, ms_class, TEST_MODE_UL_ONLY);
510 break;
511 }
512
Jacob Erlbecke5655642015-06-29 12:19:52 +0200513 /* Optionally delete the TBF */
514 switch (mode) {
515 case TEST_MODE_DL_AFTER_UL:
516 case TEST_MODE_UL_AFTER_DL:
517 tbf_free(tbf);
Jacob Erlbeck0f352a62015-07-16 18:23:33 +0200518 tbf = NULL;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200519 break;
520
521 default:
522 break;
523 }
524
Jacob Erlbeck0f352a62015-07-16 18:23:33 +0200525 if (!ms && tbf)
526 tbf_free(tbf);
527
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200528 return guard2.is_idle() ? NULL : ms;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200529}
530
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200531static unsigned alloc_many_tbfs(BTS *the_bts, unsigned min_class,
532 unsigned max_class, enum test_mode mode)
Jacob Erlbecke5655642015-06-29 12:19:52 +0200533{
Jacob Erlbecke5655642015-06-29 12:19:52 +0200534 unsigned counter;
535 unsigned ms_class = min_class;
536
Jacob Erlbecke5655642015-06-29 12:19:52 +0200537 for (counter = 0; 1; counter += 1) {
538 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
539 uint8_t ul_slots = 0;
540 uint8_t dl_slots = 0;
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200541 uint8_t busy_slots = 0;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200542 unsigned i;
543 int tfi = -1;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200544 int tfi2;
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200545 uint8_t trx_no2;
546 struct gprs_rlcmac_trx *trx;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200547 GprsMs *ms;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200548 enum gprs_rlcmac_tbf_direction dir;
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200549 uint32_t tlli = counter + 0xc0000000;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200550
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200551 ms = the_bts->ms_by_tlli(tlli);
552
553 ms = alloc_tbfs(the_bts, ms, ms_class, mode);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200554 if (!ms)
555 break;
556
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200557 ms->set_tlli(tlli);
558
Jacob Erlbecke5655642015-06-29 12:19:52 +0200559 ul_tbf = ms->ul_tbf();
560 dl_tbf = ms->dl_tbf();
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200561 trx = ms->current_trx();
562
563 OSMO_ASSERT(ul_tbf || dl_tbf);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200564
565 if (ul_tbf) {
566 ul_slots = 1 << ul_tbf->first_common_ts;
567 tfi = ul_tbf->tfi();
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200568 dir = GPRS_RLCMAC_UL_TBF;
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200569 } else {
Jacob Erlbecke5655642015-06-29 12:19:52 +0200570 ul_slots = 1 << dl_tbf->first_common_ts;
571 tfi = dl_tbf->tfi();
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200572 dir = GPRS_RLCMAC_DL_TBF;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200573 }
574
575 for (i = 0; dl_tbf && i < ARRAY_SIZE(dl_tbf->pdch); i += 1)
576 if (dl_tbf->pdch[i])
577 dl_slots |= 1 << i;
578
Max5b0df1f2017-09-11 10:38:59 +0200579 for (i = 0; ul_tbf && i < ARRAY_SIZE(ul_tbf->pdch); i += 1)
580 if (ul_tbf->pdch[i])
581 ul_slots |= 1 << i;
582
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200583 for (i = 0; trx && i < ARRAY_SIZE(trx->pdch); i += 1) {
584 struct gprs_rlcmac_pdch *pdch = &trx->pdch[i];
585
586 if (ul_tbf && dl_tbf)
587 continue;
588
589 if (ul_tbf &&
Maxd000d802017-09-20 17:55:28 +0200590 pdch->assigned_tfi(GPRS_RLCMAC_DL_TBF) != NO_FREE_TFI)
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200591 continue;
592
593 if (dl_tbf &&
Maxd000d802017-09-20 17:55:28 +0200594 pdch->assigned_tfi(GPRS_RLCMAC_UL_TBF) != NO_FREE_TFI)
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200595 continue;
596
597 busy_slots |= 1 << i;
598 }
599
Maxd000d802017-09-20 17:55:28 +0200600 printf(" TBF[%d] class %d reserves " OSMO_BIT_SPEC "\n",
Jacob Erlbecke5655642015-06-29 12:19:52 +0200601 tfi, ms_class,
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200602 get_dir_char(0x01, ul_slots, dl_slots, busy_slots),
603 get_dir_char(0x02, ul_slots, dl_slots, busy_slots),
604 get_dir_char(0x04, ul_slots, dl_slots, busy_slots),
605 get_dir_char(0x08, ul_slots, dl_slots, busy_slots),
606 get_dir_char(0x10, ul_slots, dl_slots, busy_slots),
607 get_dir_char(0x20, ul_slots, dl_slots, busy_slots),
608 get_dir_char(0x40, ul_slots, dl_slots, busy_slots),
609 get_dir_char(0x80, ul_slots, dl_slots, busy_slots));
Jacob Erlbecke5655642015-06-29 12:19:52 +0200610
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200611 if (tfi >= 0) {
612 OSMO_ASSERT(ms->current_trx());
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200613 tfi2 = the_bts->tfi_find_free(dir, &trx_no2,
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200614 ms->current_trx()->trx_no);
615 OSMO_ASSERT(tfi != tfi2);
Jacob Erlbeck7b3675b2015-07-16 18:28:22 +0200616 OSMO_ASSERT(tfi2 < 0 ||
617 trx_no2 == ms->current_trx()->trx_no);
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200618 }
619
Jacob Erlbecke5655642015-06-29 12:19:52 +0200620 ms_class += 1;
621 if (ms_class > max_class)
622 ms_class = min_class;
623 }
624
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200625 return counter;
626}
627
628static void test_successive_allocation(algo_t algo, unsigned min_class,
629 unsigned max_class, enum test_mode mode,
630 unsigned expect_num, const char *text)
631{
632 BTS the_bts;
633 struct gprs_rlcmac_bts *bts;
634 struct gprs_rlcmac_trx *trx;
635 unsigned counter;
636
Maxc59ef122017-11-27 13:21:41 +0100637 printf("Going to test assignment with many TBF, algorithm %s class %u..%u (%s)\n",
638 text, min_class, max_class, test_mode_descr(mode));
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200639
640 bts = the_bts.bts_data();
641 bts->alloc_algorithm = algo;
642
643 trx = &bts->trx[0];
644 trx->pdch[3].enable();
645 trx->pdch[4].enable();
646 trx->pdch[5].enable();
647 trx->pdch[6].enable();
648 trx->pdch[7].enable();
649
650 counter = alloc_many_tbfs(&the_bts, min_class, max_class, mode);
651
Maxc59ef122017-11-27 13:21:41 +0100652 printf(" Successfully allocated %u UL TBFs, algorithm %s class %u..%u (%s)\n",
653 counter, text, min_class, max_class, test_mode_descr(mode));
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200654 if (counter != expect_num)
Maxc59ef122017-11-27 13:21:41 +0100655 fprintf(stderr, " Expected %u TBFs (got %u), algorithm %s class %u..%u (%s)\n",
656 expect_num, counter, text, min_class, max_class, test_mode_descr(mode));
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200657
Jacob Erlbeckec478752015-06-19 16:35:38 +0200658 OSMO_ASSERT(counter == expect_num);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200659
660 check_tfi_usage(&the_bts);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200661}
662
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200663static void test_many_connections(algo_t algo, unsigned expect_num,
664 const char *text)
665{
666 BTS the_bts;
667 struct gprs_rlcmac_bts *bts;
668 struct gprs_rlcmac_trx *trx;
669 int counter1, counter2 = -1;
670 unsigned i;
671 enum test_mode mode_seq[] = {
672 TEST_MODE_DL_AFTER_UL,
673 TEST_MODE_UL_ONLY,
674 TEST_MODE_DL_AFTER_UL,
675 TEST_MODE_DL_ONLY,
676 };
677
Maxc59ef122017-11-27 13:21:41 +0100678 printf("Going to test assignment with many connections, algorithm %s\n", text);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200679
680 bts = the_bts.bts_data();
681 bts->alloc_algorithm = algo;
682
683 trx = &bts->trx[0];
684 trx->pdch[3].enable();
685 trx->pdch[4].enable();
686 trx->pdch[5].enable();
687 trx->pdch[6].enable();
688 trx->pdch[7].enable();
689
690 for (i = 0; i < ARRAY_SIZE(mode_seq); i += 1) {
Max9f460712018-01-23 20:57:08 +0100691 counter1 = alloc_many_tbfs(&the_bts, 1, mslot_class_max(), mode_seq[i]);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200692 fprintf(stderr, " Allocated %d TBFs (previously %d)\n",
693 counter1, counter2);
694
695 check_tfi_usage(&the_bts);
696
697 /* This will stop earlier due to USF shortage */
698 if (mode_seq[i] == TEST_MODE_UL_ONLY)
699 continue;
700
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200701 if (counter2 >= 0) {
702 if (counter1 < counter2)
703 fprintf(stderr, " Expected %d >= %d in %s\n",
704 counter1, counter2, text);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200705 OSMO_ASSERT(counter1 >= counter2);
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200706 }
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200707
708 counter2 = counter1;
709 }
710
711 printf(" Successfully allocated %d TBFs\n", counter1);
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200712 if (counter1 != (int)expect_num)
Maxc59ef122017-11-27 13:21:41 +0100713 fprintf(stderr, " Expected %d TBFs (got %d) for algorithm %s\n", expect_num, counter1, text);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200714
715 OSMO_ASSERT(expect_num == (unsigned)counter1);
716}
717
Maxc59ef122017-11-27 13:21:41 +0100718static 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 +0200719{
Maxc59ef122017-11-27 13:21:41 +0100720 test_successive_allocation(alloc_algorithm_a, 1, 1, mode, exp_A, "A");
721 test_successive_allocation(alloc_algorithm_b, 10, 10, mode, exp_B, "B");
722 test_successive_allocation(alloc_algorithm_dynamic, 10, 10, mode, exp_dyn, "dynamic");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200723}
724
Maxc59ef122017-11-27 13:21:41 +0100725static void test_successive_allocations()
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200726{
Maxc59ef122017-11-27 13:21:41 +0100727 test_successive_allocation(alloc_algorithm_a, 1, 1, TEST_MODE_UL_AND_DL, 35, "A");
728 test_successive_allocation(alloc_algorithm_b, 10, 10, TEST_MODE_UL_AND_DL, 32, "B");
729 test_successive_allocation(alloc_algorithm_b, 12, 12, TEST_MODE_UL_AND_DL, 32, "B");
730
Max9f460712018-01-23 20:57:08 +0100731 test_successive_allocation(alloc_algorithm_b, 1, 12, TEST_MODE_UL_AND_DL, 32, "B");
Max01bd0cc2018-01-23 20:58:49 +0100732 test_successive_allocation(alloc_algorithm_b, 1, mslot_class_max(), TEST_MODE_UL_AND_DL, 32, "B");
Max9f460712018-01-23 20:57:08 +0100733 test_successive_allocation(alloc_algorithm_dynamic, 1, mslot_class_max(), TEST_MODE_UL_AND_DL, 35, "dynamic");
Maxc59ef122017-11-27 13:21:41 +0100734
735 test_a_b_dyn(TEST_MODE_DL_AND_UL, 35, 32, 32);
736 test_a_b_dyn(TEST_MODE_DL_AFTER_UL, 160, 32, 95);
737 test_a_b_dyn(TEST_MODE_UL_AFTER_DL, 35, 32, 35);
738 test_a_b_dyn(TEST_MODE_UL_ONLY, 35, 32, 35);
739 test_a_b_dyn(TEST_MODE_DL_ONLY, 160, 32, 101);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200740}
741
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530742static void test_2_consecutive_dl_tbfs()
743{
744 BTS the_bts;
745 struct gprs_rlcmac_bts *bts;
746 struct gprs_rlcmac_trx *trx;
747 uint8_t ms_class = 11;
748 uint8_t egprs_ms_class = 11;
749 gprs_rlcmac_tbf *dl_tbf1, *dl_tbf2;
750 uint8_t numTs1 = 0, numTs2 = 0;
751
752 printf("Testing DL TS allocation for Multi UEs\n");
753
754 bts = the_bts.bts_data();
755 bts->alloc_algorithm = alloc_algorithm_b;
756
757 trx = &bts->trx[0];
758 trx->pdch[4].enable();
759 trx->pdch[5].enable();
760 trx->pdch[6].enable();
761 trx->pdch[7].enable();
762
Max92b7a502018-01-26 11:01:35 +0100763 dl_tbf1 = tbf_alloc_dl_tbf(bts, NULL, 0, ms_class, egprs_ms_class,
764 false);
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530765 OSMO_ASSERT(dl_tbf1);
766
767 for (int i = 0; i < 8; i++) {
768 if (dl_tbf1->pdch[i])
769 numTs1++;
770 }
771 OSMO_ASSERT(numTs1 == 4);
772 printf("TBF1: numTs(%d)\n", numTs1);
773
Max92b7a502018-01-26 11:01:35 +0100774 dl_tbf2 = tbf_alloc_dl_tbf(bts, NULL, 0, ms_class, egprs_ms_class,
775 false);
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530776 OSMO_ASSERT(dl_tbf2);
777
778 for (int i = 0; i < 8; i++) {
779 if (dl_tbf2->pdch[i])
780 numTs2++;
781 }
782
783 /*
784 * TODO: currently 2nd DL TBF gets 3 TS
785 * This behaviour will be fixed in subsequent patch
786 */
787 printf("TBF2: numTs(%d)\n", numTs2);
788 OSMO_ASSERT(numTs2 == 3);
789
790 tbf_free(dl_tbf1);
791 tbf_free(dl_tbf2);
792}
793
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200794int main(int argc, char **argv)
795{
796 tall_pcu_ctx = talloc_named_const(NULL, 1, "moiji-mobile AllocTest context");
797 if (!tall_pcu_ctx)
798 abort();
799
Neels Hofmeyr78ce5912017-02-08 17:07:31 +0100800 msgb_talloc_ctx_init(tall_pcu_ctx, 0);
Neels Hofmeyr42f2d612018-04-01 16:54:40 +0200801 osmo_init_logging2(tall_pcu_ctx, &gprs_log_info);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200802 log_set_use_color(osmo_stderr_target, 0);
803 log_set_print_filename(osmo_stderr_target, 0);
Jacob Erlbeck9ec49e22015-06-29 13:00:20 +0200804 if (getenv("LOGL_DEBUG"))
805 log_set_log_level(osmo_stderr_target, LOGL_DEBUG);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200806
807 test_alloc_a();
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100808 test_alloc_b();
Maxc59ef122017-11-27 13:21:41 +0100809 test_successive_allocations();
810 test_many_connections(alloc_algorithm_a, 160, "A");
Max01bd0cc2018-01-23 20:58:49 +0100811 test_many_connections(alloc_algorithm_b, 32, "B");
Maxc59ef122017-11-27 13:21:41 +0100812 test_many_connections(alloc_algorithm_dynamic, 160, "dynamic");
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530813 test_2_consecutive_dl_tbfs();
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200814 return EXIT_SUCCESS;
815}
816
817/*
818 * stubs that should not be reached
819 */
820extern "C" {
821void l1if_pdch_req() { abort(); }
822void l1if_connect_pdch() { abort(); }
823void l1if_close_pdch() { abort(); }
824void l1if_open_pdch() { abort(); }
825}