blob: 64d6a50fca2a4f3cc4812587bc6e38c6538c0179 [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;
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +010039bool spoof_mnc_3_digits = false;
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020040
Daniel Willmann48aa0b02014-07-16 18:54:10 +020041static gprs_rlcmac_tbf *tbf_alloc(struct gprs_rlcmac_bts *bts,
Jacob Erlbecke2e004e2015-06-18 17:16:26 +020042 GprsMs *ms, gprs_rlcmac_tbf_direction dir,
Jacob Erlbeck5879c642015-07-10 10:41:36 +020043 uint8_t use_trx,
Maxe9fe0e32017-09-28 15:56:05 +020044 uint8_t ms_class, uint8_t egprs_ms_class, bool single_slot)
Daniel Willmann48aa0b02014-07-16 18:54:10 +020045{
46 if (dir == GPRS_RLCMAC_UL_TBF)
Jacob Erlbeck86b6f052015-11-27 15:17:34 +010047 return tbf_alloc_ul_tbf(bts, ms, use_trx,
48 ms_class, egprs_ms_class, single_slot);
Daniel Willmann48aa0b02014-07-16 18:54:10 +020049 else
Jacob Erlbeck86b6f052015-11-27 15:17:34 +010050 return tbf_alloc_dl_tbf(bts, ms, use_trx,
51 ms_class, egprs_ms_class, single_slot);
Daniel Willmann48aa0b02014-07-16 18:54:10 +020052}
53
Jacob Erlbeck61205a72015-07-09 11:35:50 +020054static void check_tfi_usage(BTS *the_bts)
55{
56 int pdch_no;
Jacob Erlbeck61205a72015-07-09 11:35:50 +020057
58 struct gprs_rlcmac_tbf *tfi_usage[8][8][2][32] = {{{{NULL}}}};
Jacob Erlbecked2dbf62015-12-28 19:15:40 +010059 LListHead<gprs_rlcmac_tbf> *tbf_lists[2] = {
60 &the_bts->ul_tbfs(),
61 &the_bts->dl_tbfs()
Jacob Erlbeck61205a72015-07-09 11:35:50 +020062 };
63
Jacob Erlbecked2dbf62015-12-28 19:15:40 +010064 LListHead<gprs_rlcmac_tbf> *pos;
Jacob Erlbeck61205a72015-07-09 11:35:50 +020065 gprs_rlcmac_tbf *tbf;
Jacob Erlbeck61205a72015-07-09 11:35:50 +020066 unsigned list_idx;
67 struct gprs_rlcmac_tbf **tbf_var;
68
69 for (list_idx = 0; list_idx < ARRAY_SIZE(tbf_lists); list_idx += 1)
70 {
71
Jacob Erlbecked2dbf62015-12-28 19:15:40 +010072 llist_for_each(pos, tbf_lists[list_idx]) {
73 tbf = pos->entry();
Jacob Erlbeck61205a72015-07-09 11:35:50 +020074 for (pdch_no = 0; pdch_no < 8; pdch_no += 1) {
75 struct gprs_rlcmac_pdch *pdch = tbf->pdch[pdch_no];
76 if (pdch == NULL)
77 continue;
78
79 tbf_var = &tfi_usage
80 [tbf->trx->trx_no]
81 [pdch_no]
82 [tbf->direction]
83 [tbf->tfi()];
84
85 OSMO_ASSERT(*tbf_var == NULL);
86 if (tbf->direction == GPRS_RLCMAC_DL_TBF) {
87 OSMO_ASSERT(pdch->dl_tbf_by_tfi(
88 tbf->tfi()) == tbf);
Jacob Erlbeck61205a72015-07-09 11:35:50 +020089 OSMO_ASSERT(the_bts->dl_tbf_by_tfi(
90 tbf->tfi(),
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +020091 tbf->trx->trx_no,
92 pdch_no) == tbf);
Jacob Erlbeck61205a72015-07-09 11:35:50 +020093 } else {
94 OSMO_ASSERT(pdch->ul_tbf_by_tfi(
95 tbf->tfi()) == tbf);
Jacob Erlbeck61205a72015-07-09 11:35:50 +020096 OSMO_ASSERT(the_bts->ul_tbf_by_tfi(
97 tbf->tfi(),
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +020098 tbf->trx->trx_no,
99 pdch_no) == tbf);
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200100 }
101 *tbf_var = tbf;
Jacob Erlbeck47a57f62015-07-08 12:53:16 +0200102 OSMO_ASSERT(pdch->assigned_tfi(tbf->direction) &
103 (1 << tbf->tfi()));
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200104 }
105 }
106 }
107}
108
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200109static void test_alloc_a(gprs_rlcmac_tbf_direction dir,
110 uint8_t slots, const int count)
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200111{
112 int tfi;
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200113 int i;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200114 uint8_t used_trx, tmp_trx;
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200115 BTS the_bts;
116 struct gprs_rlcmac_bts *bts;
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200117 struct gprs_rlcmac_tbf *tbfs[32*8+1] = { 0, };
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200118
119 printf("Testing alloc_a direction(%d)\n", dir);
120
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200121 bts = the_bts.bts_data();
122 bts->alloc_algorithm = alloc_algorithm_a;
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200123
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200124 struct gprs_rlcmac_trx *trx = &bts->trx[0];
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200125 for (i = 0; i < 8; i += 1)
126 if (slots & (1 << i))
127 trx->pdch[i].enable();
128
129 OSMO_ASSERT(count >= 0 && count <= (int)ARRAY_SIZE(tbfs));
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200130
131 /**
132 * Currently alloc_a will only allocate from the first
133 * PDCH and all possible usf's. We run out of usf's before
134 * we are out of tfi's. Observe this and make sure that at
135 * least this part is working okay.
136 */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200137 for (i = 0; i < (int)ARRAY_SIZE(tbfs); ++i) {
Jacob Erlbeck86b6f052015-11-27 15:17:34 +0100138 tbfs[i] = tbf_alloc(bts, NULL, dir, -1, 0, 0, 0);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200139 if (tbfs[i] == NULL)
140 break;
141
142 used_trx = tbfs[i]->trx->trx_no;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200143 tfi = the_bts.tfi_find_free(dir, &tmp_trx, used_trx);
144 OSMO_ASSERT(tbfs[i]->tfi() != tfi);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200145 }
146
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200147 check_tfi_usage(&the_bts);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200148
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200149 OSMO_ASSERT(i == count);
150
151 for (i = 0; i < count; ++i)
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200152 if (tbfs[i])
153 tbf_free(tbfs[i]);
154
Holger Hans Peter Freytherf3f1bde2016-02-22 15:14:01 +0100155 tbfs[0] = tbf_alloc(bts, NULL, dir, -1, 0, 0, 0);
156 OSMO_ASSERT(tbfs[0]);
157 tbf_free(tbfs[0]);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200158}
159
160static void test_alloc_a()
161{
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200162 /* slots 2 - 3 */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200163 test_alloc_a(GPRS_RLCMAC_DL_TBF, 0x0c, 32*2);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200164 test_alloc_a(GPRS_RLCMAC_UL_TBF, 0x0c, 14);
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200165
166 /* slots 1 - 5 */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200167 test_alloc_a(GPRS_RLCMAC_DL_TBF, 0x1e, 32*4);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200168 test_alloc_a(GPRS_RLCMAC_UL_TBF, 0x1e, 28);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200169}
170
Max2ecf0fd2017-11-21 18:13:31 +0100171static void dump_assignment(struct gprs_rlcmac_tbf *tbf, const char *dir, bool verbose)
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100172{
Max2ecf0fd2017-11-21 18:13:31 +0100173 if (!verbose)
174 return;
175
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200176 for (size_t i = 0; i < ARRAY_SIZE(tbf->pdch); ++i)
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100177 if (tbf->pdch[i])
Neels Hofmeyrd34646a2017-02-08 17:07:40 +0100178 printf("PDCH[%zu] is used for %s\n", i, dir);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100179 printf("PDCH[%d] is control_ts for %s\n", tbf->control_ts, dir);
180 printf("PDCH[%d] is first common for %s\n", tbf->first_common_ts, dir);
181}
182
Max2ecf0fd2017-11-21 18:13:31 +0100183#define ENABLE_PDCH(ts_no, enable_flag, trx) \
184 if (enable_flag) \
185 trx->pdch[ts_no].enable();
186
187static inline void enable_ts_on_bts(struct gprs_rlcmac_bts *bts,
188 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 +0100189{
Max2ecf0fd2017-11-21 18:13:31 +0100190 struct gprs_rlcmac_trx *trx = &bts->trx[0];
191
192 ENABLE_PDCH(0, ts0, trx);
193 ENABLE_PDCH(1, ts1, trx);
194 ENABLE_PDCH(2, ts2, trx);
195 ENABLE_PDCH(3, ts3, trx);
196 ENABLE_PDCH(4, ts4, trx);
197 ENABLE_PDCH(5, ts5, trx);
198 ENABLE_PDCH(6, ts6, trx);
199 ENABLE_PDCH(7, ts7, trx);
200}
201
202static inline bool test_alloc_b_ul_dl(bool ts0, bool ts1, bool ts2, bool ts3, bool ts4, bool ts5, bool ts6, bool ts7,
203 uint8_t ms_class, bool verbose)
204{
205 BTS the_bts;
206 struct gprs_rlcmac_bts *bts = the_bts.bts_data();
207 gprs_rlcmac_ul_tbf *ul_tbf;
208 gprs_rlcmac_dl_tbf *dl_tbf;
209
210 if (verbose)
211 printf("Testing UL then DL assignment.\n");
212
213 bts->alloc_algorithm = alloc_algorithm_b;
214
215 enable_ts_on_bts(bts, ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7);
216
Max92b7a502018-01-26 11:01:35 +0100217 ul_tbf = tbf_alloc_ul_tbf(bts, NULL, -1, ms_class, 0, true);
Max2ecf0fd2017-11-21 18:13:31 +0100218 if (!ul_tbf)
219 return false;
220
221 OSMO_ASSERT(ul_tbf->ms());
222 OSMO_ASSERT(ul_tbf->ms()->current_trx());
223
224 dump_assignment(ul_tbf, "UL", verbose);
225
226 /* assume final ack has not been sent */
Max92b7a502018-01-26 11:01:35 +0100227 dl_tbf = tbf_alloc_dl_tbf(bts, ul_tbf->ms(), ul_tbf->ms()->current_trx()->trx_no, ms_class, 0,
228 false);
Max2ecf0fd2017-11-21 18:13:31 +0100229 if (!dl_tbf)
230 return false;
231
232 dump_assignment(dl_tbf, "DL", verbose);
233
234 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
235
236 check_tfi_usage(&the_bts);
237
238 tbf_free(dl_tbf);
239 tbf_free(ul_tbf);
240
241 return true;
242}
243
244static inline bool test_alloc_b_dl_ul(bool ts0, bool ts1, bool ts2, bool ts3, bool ts4, bool ts5, bool ts6, bool ts7,
245 uint8_t ms_class, bool verbose)
246{
247 BTS the_bts;
248 struct gprs_rlcmac_bts *bts = the_bts.bts_data();
249 gprs_rlcmac_ul_tbf *ul_tbf;
250 gprs_rlcmac_dl_tbf *dl_tbf;
251
252 if (verbose)
253 printf("Testing DL then UL assignment followed by update\n");
254
255 bts->alloc_algorithm = alloc_algorithm_b;
256
257 enable_ts_on_bts(bts, ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7);
258
Max92b7a502018-01-26 11:01:35 +0100259 dl_tbf = tbf_alloc_dl_tbf(bts, NULL, -1, ms_class, 0, true);
Max2ecf0fd2017-11-21 18:13:31 +0100260 if (!dl_tbf)
261 return false;
262
263 dl_tbf->update_ms(0x23, GPRS_RLCMAC_DL_TBF);
264
265 OSMO_ASSERT(dl_tbf->ms());
266 OSMO_ASSERT(dl_tbf->ms()->current_trx());
267
268 dump_assignment(dl_tbf, "DL", verbose);
269
Max92b7a502018-01-26 11:01:35 +0100270 ul_tbf = tbf_alloc_ul_tbf(bts, dl_tbf->ms(), dl_tbf->ms()->current_trx()->trx_no, ms_class, 0,
271 false);
Max2ecf0fd2017-11-21 18:13:31 +0100272 if (!ul_tbf)
273 return false;
274
275 ul_tbf->update_ms(0x23, GPRS_RLCMAC_UL_TBF);
276 ul_tbf->m_contention_resolution_done = 1;
277
278 dump_assignment(ul_tbf, "UL", verbose);
279
280 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
281
282 /* now update the dl_tbf */
283 dl_tbf->update();
284 dump_assignment(dl_tbf, "DL", verbose);
285 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
286
287 check_tfi_usage(&the_bts);
288
289 tbf_free(dl_tbf);
290 tbf_free(ul_tbf);
291
292 return true;
293}
294
295static inline bool test_alloc_b_jolly(uint8_t ms_class)
296{
297 BTS the_bts;
298 struct gprs_rlcmac_bts *bts = the_bts.bts_data();
299 int tfi;
300 uint8_t trx_no;
301 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
302
303 printf("Testing jolly example\n");
304
305 bts->alloc_algorithm = alloc_algorithm_b;
306
307 enable_ts_on_bts(bts, false, true, true, true, true, false, false, false);
308
309 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
310 OSMO_ASSERT(tfi >= 0);
Max92b7a502018-01-26 11:01:35 +0100311 ul_tbf = tbf_alloc_ul_tbf(bts, NULL, .1, ms_class, 0, false);
Max2ecf0fd2017-11-21 18:13:31 +0100312 if (!ul_tbf)
313 return false;
314
315 OSMO_ASSERT(ul_tbf->ms());
316 OSMO_ASSERT(ul_tbf->ms()->current_trx());
317 trx_no = ul_tbf->ms()->current_trx()->trx_no;
318 dump_assignment(ul_tbf, "UL", true);
319
320 /* assume final ack has not been sent */
Max92b7a502018-01-26 11:01:35 +0100321 dl_tbf = tbf_alloc_dl_tbf(bts, ul_tbf->ms(), trx_no, ms_class, 0,
322 false);
Max2ecf0fd2017-11-21 18:13:31 +0100323 if (!dl_tbf)
324 return false;
325
326 dump_assignment(dl_tbf, "DL", true);
327
328 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
329
330 check_tfi_usage(&the_bts);
331
332 tbf_free(dl_tbf);
333 tbf_free(ul_tbf);
334
335 return true;
336}
337
338static void test_alloc_b_for_ms(uint8_t ms_class)
339{
340 bool rc;
341
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100342 printf("Going to test multislot assignment MS_CLASS=%d\n", ms_class);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100343 /*
344 * PDCH is on TS 6,7,8 and we start with a UL allocation and
345 * then follow two DL allocations (once single, once normal).
346 *
347 * Uplink assigned and still available..
348 */
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100349
Max2ecf0fd2017-11-21 18:13:31 +0100350 rc = test_alloc_b_ul_dl(false, false, false, false, false, true, true, true, ms_class, true);
351 if (!rc)
352 return;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100353
354 /**
355 * Test with the other order.. first DL and then UL
356 */
Max2ecf0fd2017-11-21 18:13:31 +0100357 rc = test_alloc_b_dl_ul(false, false, false, false, false, true, true, true, ms_class, true);
358 if (!rc)
359 return;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100360
361 /* Andreas osmocom-pcu example */
Max2ecf0fd2017-11-21 18:13:31 +0100362 test_alloc_b_jolly(ms_class);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100363}
364
Max2ecf0fd2017-11-21 18:13:31 +0100365static 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 +0100366{
Max2ecf0fd2017-11-21 18:13:31 +0100367 bool rc;
368
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100369 /* we can test the allocation failures differently */
370 if (!ts0 && !ts1 && !ts2 && !ts3 && !ts4 && !ts5 && !ts6 && !ts7)
371 return;
372
373 printf("Mass test: TS0(%c%c%c%c%c%c%c%c)TS7 MS_Class=%d\n",
374 ts0 ? 'O' : 'x',
375 ts1 ? 'O' : 'x',
376 ts2 ? 'O' : 'x',
377 ts3 ? 'O' : 'x',
378 ts4 ? 'O' : 'x',
379 ts5 ? 'O' : 'x',
380 ts6 ? 'O' : 'x',
381 ts7 ? 'O' : 'x', ms_class);
382 fflush(stdout);
383
Max2ecf0fd2017-11-21 18:13:31 +0100384 rc = test_alloc_b_ul_dl(ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7, ms_class, false);
385 if (!rc)
386 return;
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100387
388 /**
389 * Test with the other order.. first DL and then UL
390 */
Max2ecf0fd2017-11-21 18:13:31 +0100391 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 +0100392}
393
394static void test_all_alloc_b()
395{
396 /* it is a bit crazy... */
397 for (uint8_t ts0 = 0; ts0 < 2; ++ts0)
398 for (uint8_t ts1 = 0; ts1 < 2; ++ts1)
399 for (uint8_t ts2 = 0; ts2 < 2; ++ts2)
400 for (uint8_t ts3 = 0; ts3 < 2; ++ts3)
401 for (uint8_t ts4 = 0; ts4 < 2; ++ts4)
402 for (uint8_t ts5 = 0; ts5 < 2; ++ts5)
403 for (uint8_t ts6 = 0; ts6 < 2; ++ts6)
404 for (uint8_t ts7 = 0; ts7 < 2; ++ts7)
Max9f460712018-01-23 20:57:08 +0100405 for (int ms_class = 0; ms_class < mslot_class_max(); ++ms_class)
Max2ecf0fd2017-11-21 18:13:31 +0100406 test_alloc_mass(ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7, ms_class);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100407}
408
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100409static void test_alloc_b()
410{
Max9f460712018-01-23 20:57:08 +0100411 for (int i = 0; i < mslot_class_max(); ++i)
Max2ecf0fd2017-11-21 18:13:31 +0100412 test_alloc_b_for_ms(i);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100413
414 test_all_alloc_b();
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100415}
416
Maxe9fe0e32017-09-28 15:56:05 +0200417typedef int (*algo_t)(struct gprs_rlcmac_bts *bts, struct GprsMs *ms, struct gprs_rlcmac_tbf *tbf, bool single,
418 int8_t use_trx);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200419
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200420static char get_dir_char(uint8_t mask, uint8_t tx, uint8_t rx, uint8_t busy)
Jacob Erlbecke5655642015-06-29 12:19:52 +0200421{
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200422 int offs = busy ? 32 : 0;
423 return (mask & tx & rx) ? 'C' + offs :
424 (mask & tx) ? 'U' + offs :
425 (mask & rx) ? 'D' + offs :
Jacob Erlbecke5655642015-06-29 12:19:52 +0200426 '.';
427}
428
429enum test_mode {
430 TEST_MODE_UL_ONLY,
431 TEST_MODE_DL_ONLY,
432 TEST_MODE_UL_AND_DL,
433 TEST_MODE_DL_AND_UL,
434 TEST_MODE_DL_AFTER_UL,
435 TEST_MODE_UL_AFTER_DL,
436};
437
Maxc59ef122017-11-27 13:21:41 +0100438static inline char *test_mode_descr(enum test_mode t)
439{
440 switch (t) {
441 case TEST_MODE_UL_ONLY: return (char*)"UL only";
442 case TEST_MODE_DL_ONLY: return (char*)"DL only";
443 case TEST_MODE_UL_AND_DL: return (char*)"UL and DL";
444 case TEST_MODE_DL_AND_UL: return (char*)"DL and UL";
445 case TEST_MODE_DL_AFTER_UL: return (char*)"DL after UL";
446 case TEST_MODE_UL_AFTER_DL: return (char*)"UL after DL";
447 default: return NULL;
448 }
449}
450
Jacob Erlbecke5655642015-06-29 12:19:52 +0200451static GprsMs *alloc_tbfs(BTS *the_bts, GprsMs *ms, unsigned ms_class,
452 enum test_mode mode)
453{
454 struct gprs_rlcmac_bts *bts;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200455 uint8_t trx_no = -1;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200456
457 bts = the_bts->bts_data();
458
459 gprs_rlcmac_tbf *tbf = NULL;
460
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200461 if (ms && ms->current_trx())
462 trx_no = ms->current_trx()->trx_no;
463
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200464 GprsMs::Guard guard1(ms);
465
Jacob Erlbecke5655642015-06-29 12:19:52 +0200466 /* Allocate what is needed first */
467 switch (mode) {
468 case TEST_MODE_UL_ONLY:
469 case TEST_MODE_DL_AFTER_UL:
470 case TEST_MODE_UL_AND_DL:
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200471 if (ms && ms->ul_tbf())
472 tbf_free(ms->ul_tbf());
Max92b7a502018-01-26 11:01:35 +0100473 tbf = tbf_alloc_ul_tbf(bts, ms, trx_no, ms_class, 0, false);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200474 if (tbf == NULL)
475 return NULL;
476 break;
477 case TEST_MODE_DL_ONLY:
478 case TEST_MODE_UL_AFTER_DL:
479 case TEST_MODE_DL_AND_UL:
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200480 if (ms && ms->dl_tbf())
481 tbf_free(ms->dl_tbf());
Max92b7a502018-01-26 11:01:35 +0100482 tbf = tbf_alloc_dl_tbf(bts, ms, trx_no, ms_class, 0, false);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200483 if (tbf == NULL)
484 return NULL;
485 }
486
487 OSMO_ASSERT(tbf);
488 OSMO_ASSERT(tbf->ms());
489 OSMO_ASSERT(ms == NULL || ms == tbf->ms());
490 ms = tbf->ms();
491
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200492 GprsMs::Guard guard2(ms);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200493
Jacob Erlbeck14376a72015-07-07 11:31:28 +0200494 /* Continue with what is needed next */
495 switch (mode) {
496 case TEST_MODE_UL_ONLY:
497 case TEST_MODE_DL_ONLY:
498 /* We are done */
499 break;
500
501 case TEST_MODE_DL_AFTER_UL:
502 case TEST_MODE_UL_AND_DL:
503 ms = alloc_tbfs(the_bts, ms, ms_class, TEST_MODE_DL_ONLY);
504 break;
505
506 case TEST_MODE_UL_AFTER_DL:
507 case TEST_MODE_DL_AND_UL:
508 ms = alloc_tbfs(the_bts, ms, ms_class, TEST_MODE_UL_ONLY);
509 break;
510 }
511
Jacob Erlbecke5655642015-06-29 12:19:52 +0200512 /* Optionally delete the TBF */
513 switch (mode) {
514 case TEST_MODE_DL_AFTER_UL:
515 case TEST_MODE_UL_AFTER_DL:
516 tbf_free(tbf);
Jacob Erlbeck0f352a62015-07-16 18:23:33 +0200517 tbf = NULL;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200518 break;
519
520 default:
521 break;
522 }
523
Jacob Erlbeck0f352a62015-07-16 18:23:33 +0200524 if (!ms && tbf)
525 tbf_free(tbf);
526
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200527 return guard2.is_idle() ? NULL : ms;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200528}
529
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200530static unsigned alloc_many_tbfs(BTS *the_bts, unsigned min_class,
531 unsigned max_class, enum test_mode mode)
Jacob Erlbecke5655642015-06-29 12:19:52 +0200532{
Jacob Erlbecke5655642015-06-29 12:19:52 +0200533 unsigned counter;
534 unsigned ms_class = min_class;
535
Jacob Erlbecke5655642015-06-29 12:19:52 +0200536 for (counter = 0; 1; counter += 1) {
537 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
538 uint8_t ul_slots = 0;
539 uint8_t dl_slots = 0;
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200540 uint8_t busy_slots = 0;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200541 unsigned i;
542 int tfi = -1;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200543 int tfi2;
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200544 uint8_t trx_no2;
545 struct gprs_rlcmac_trx *trx;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200546 GprsMs *ms;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200547 enum gprs_rlcmac_tbf_direction dir;
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200548 uint32_t tlli = counter + 0xc0000000;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200549
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200550 ms = the_bts->ms_by_tlli(tlli);
551
552 ms = alloc_tbfs(the_bts, ms, ms_class, mode);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200553 if (!ms)
554 break;
555
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200556 ms->set_tlli(tlli);
557
Jacob Erlbecke5655642015-06-29 12:19:52 +0200558 ul_tbf = ms->ul_tbf();
559 dl_tbf = ms->dl_tbf();
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200560 trx = ms->current_trx();
561
562 OSMO_ASSERT(ul_tbf || dl_tbf);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200563
564 if (ul_tbf) {
565 ul_slots = 1 << ul_tbf->first_common_ts;
566 tfi = ul_tbf->tfi();
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200567 dir = GPRS_RLCMAC_UL_TBF;
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200568 } else {
Jacob Erlbecke5655642015-06-29 12:19:52 +0200569 ul_slots = 1 << dl_tbf->first_common_ts;
570 tfi = dl_tbf->tfi();
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200571 dir = GPRS_RLCMAC_DL_TBF;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200572 }
573
574 for (i = 0; dl_tbf && i < ARRAY_SIZE(dl_tbf->pdch); i += 1)
575 if (dl_tbf->pdch[i])
576 dl_slots |= 1 << i;
577
Max5b0df1f2017-09-11 10:38:59 +0200578 for (i = 0; ul_tbf && i < ARRAY_SIZE(ul_tbf->pdch); i += 1)
579 if (ul_tbf->pdch[i])
580 ul_slots |= 1 << i;
581
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200582 for (i = 0; trx && i < ARRAY_SIZE(trx->pdch); i += 1) {
583 struct gprs_rlcmac_pdch *pdch = &trx->pdch[i];
584
585 if (ul_tbf && dl_tbf)
586 continue;
587
588 if (ul_tbf &&
Maxd000d802017-09-20 17:55:28 +0200589 pdch->assigned_tfi(GPRS_RLCMAC_DL_TBF) != NO_FREE_TFI)
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200590 continue;
591
592 if (dl_tbf &&
Maxd000d802017-09-20 17:55:28 +0200593 pdch->assigned_tfi(GPRS_RLCMAC_UL_TBF) != NO_FREE_TFI)
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200594 continue;
595
596 busy_slots |= 1 << i;
597 }
598
Maxd000d802017-09-20 17:55:28 +0200599 printf(" TBF[%d] class %d reserves " OSMO_BIT_SPEC "\n",
Jacob Erlbecke5655642015-06-29 12:19:52 +0200600 tfi, ms_class,
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200601 get_dir_char(0x01, ul_slots, dl_slots, busy_slots),
602 get_dir_char(0x02, ul_slots, dl_slots, busy_slots),
603 get_dir_char(0x04, ul_slots, dl_slots, busy_slots),
604 get_dir_char(0x08, ul_slots, dl_slots, busy_slots),
605 get_dir_char(0x10, ul_slots, dl_slots, busy_slots),
606 get_dir_char(0x20, ul_slots, dl_slots, busy_slots),
607 get_dir_char(0x40, ul_slots, dl_slots, busy_slots),
608 get_dir_char(0x80, ul_slots, dl_slots, busy_slots));
Jacob Erlbecke5655642015-06-29 12:19:52 +0200609
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200610 if (tfi >= 0) {
611 OSMO_ASSERT(ms->current_trx());
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200612 tfi2 = the_bts->tfi_find_free(dir, &trx_no2,
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200613 ms->current_trx()->trx_no);
614 OSMO_ASSERT(tfi != tfi2);
Jacob Erlbeck7b3675b2015-07-16 18:28:22 +0200615 OSMO_ASSERT(tfi2 < 0 ||
616 trx_no2 == ms->current_trx()->trx_no);
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200617 }
618
Jacob Erlbecke5655642015-06-29 12:19:52 +0200619 ms_class += 1;
620 if (ms_class > max_class)
621 ms_class = min_class;
622 }
623
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200624 return counter;
625}
626
627static void test_successive_allocation(algo_t algo, unsigned min_class,
628 unsigned max_class, enum test_mode mode,
629 unsigned expect_num, const char *text)
630{
631 BTS the_bts;
632 struct gprs_rlcmac_bts *bts;
633 struct gprs_rlcmac_trx *trx;
634 unsigned counter;
635
Maxc59ef122017-11-27 13:21:41 +0100636 printf("Going to test assignment with many TBF, algorithm %s class %u..%u (%s)\n",
637 text, min_class, max_class, test_mode_descr(mode));
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200638
639 bts = the_bts.bts_data();
640 bts->alloc_algorithm = algo;
641
642 trx = &bts->trx[0];
643 trx->pdch[3].enable();
644 trx->pdch[4].enable();
645 trx->pdch[5].enable();
646 trx->pdch[6].enable();
647 trx->pdch[7].enable();
648
649 counter = alloc_many_tbfs(&the_bts, min_class, max_class, mode);
650
Maxc59ef122017-11-27 13:21:41 +0100651 printf(" Successfully allocated %u UL TBFs, algorithm %s class %u..%u (%s)\n",
652 counter, text, min_class, max_class, test_mode_descr(mode));
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200653 if (counter != expect_num)
Maxc59ef122017-11-27 13:21:41 +0100654 fprintf(stderr, " Expected %u TBFs (got %u), algorithm %s class %u..%u (%s)\n",
655 expect_num, counter, text, min_class, max_class, test_mode_descr(mode));
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200656
Jacob Erlbeckec478752015-06-19 16:35:38 +0200657 OSMO_ASSERT(counter == expect_num);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200658
659 check_tfi_usage(&the_bts);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200660}
661
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200662static void test_many_connections(algo_t algo, unsigned expect_num,
663 const char *text)
664{
665 BTS the_bts;
666 struct gprs_rlcmac_bts *bts;
667 struct gprs_rlcmac_trx *trx;
668 int counter1, counter2 = -1;
669 unsigned i;
670 enum test_mode mode_seq[] = {
671 TEST_MODE_DL_AFTER_UL,
672 TEST_MODE_UL_ONLY,
673 TEST_MODE_DL_AFTER_UL,
674 TEST_MODE_DL_ONLY,
675 };
676
Maxc59ef122017-11-27 13:21:41 +0100677 printf("Going to test assignment with many connections, algorithm %s\n", text);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200678
679 bts = the_bts.bts_data();
680 bts->alloc_algorithm = algo;
681
682 trx = &bts->trx[0];
683 trx->pdch[3].enable();
684 trx->pdch[4].enable();
685 trx->pdch[5].enable();
686 trx->pdch[6].enable();
687 trx->pdch[7].enable();
688
689 for (i = 0; i < ARRAY_SIZE(mode_seq); i += 1) {
Max9f460712018-01-23 20:57:08 +0100690 counter1 = alloc_many_tbfs(&the_bts, 1, mslot_class_max(), mode_seq[i]);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200691 fprintf(stderr, " Allocated %d TBFs (previously %d)\n",
692 counter1, counter2);
693
694 check_tfi_usage(&the_bts);
695
696 /* This will stop earlier due to USF shortage */
697 if (mode_seq[i] == TEST_MODE_UL_ONLY)
698 continue;
699
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200700 if (counter2 >= 0) {
701 if (counter1 < counter2)
702 fprintf(stderr, " Expected %d >= %d in %s\n",
703 counter1, counter2, text);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200704 OSMO_ASSERT(counter1 >= counter2);
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200705 }
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200706
707 counter2 = counter1;
708 }
709
710 printf(" Successfully allocated %d TBFs\n", counter1);
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200711 if (counter1 != (int)expect_num)
Maxc59ef122017-11-27 13:21:41 +0100712 fprintf(stderr, " Expected %d TBFs (got %d) for algorithm %s\n", expect_num, counter1, text);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200713
714 OSMO_ASSERT(expect_num == (unsigned)counter1);
715}
716
Maxc59ef122017-11-27 13:21:41 +0100717static 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 +0200718{
Maxc59ef122017-11-27 13:21:41 +0100719 test_successive_allocation(alloc_algorithm_a, 1, 1, mode, exp_A, "A");
720 test_successive_allocation(alloc_algorithm_b, 10, 10, mode, exp_B, "B");
721 test_successive_allocation(alloc_algorithm_dynamic, 10, 10, mode, exp_dyn, "dynamic");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200722}
723
Maxc59ef122017-11-27 13:21:41 +0100724static void test_successive_allocations()
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200725{
Maxc59ef122017-11-27 13:21:41 +0100726 test_successive_allocation(alloc_algorithm_a, 1, 1, TEST_MODE_UL_AND_DL, 35, "A");
727 test_successive_allocation(alloc_algorithm_b, 10, 10, TEST_MODE_UL_AND_DL, 32, "B");
728 test_successive_allocation(alloc_algorithm_b, 12, 12, TEST_MODE_UL_AND_DL, 32, "B");
729
Max9f460712018-01-23 20:57:08 +0100730 test_successive_allocation(alloc_algorithm_b, 1, 12, TEST_MODE_UL_AND_DL, 32, "B");
Max01bd0cc2018-01-23 20:58:49 +0100731 test_successive_allocation(alloc_algorithm_b, 1, mslot_class_max(), TEST_MODE_UL_AND_DL, 32, "B");
Max9f460712018-01-23 20:57:08 +0100732 test_successive_allocation(alloc_algorithm_dynamic, 1, mslot_class_max(), TEST_MODE_UL_AND_DL, 35, "dynamic");
Maxc59ef122017-11-27 13:21:41 +0100733
734 test_a_b_dyn(TEST_MODE_DL_AND_UL, 35, 32, 32);
735 test_a_b_dyn(TEST_MODE_DL_AFTER_UL, 160, 32, 95);
736 test_a_b_dyn(TEST_MODE_UL_AFTER_DL, 35, 32, 35);
737 test_a_b_dyn(TEST_MODE_UL_ONLY, 35, 32, 35);
738 test_a_b_dyn(TEST_MODE_DL_ONLY, 160, 32, 101);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200739}
740
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530741static void test_2_consecutive_dl_tbfs()
742{
743 BTS the_bts;
744 struct gprs_rlcmac_bts *bts;
745 struct gprs_rlcmac_trx *trx;
746 uint8_t ms_class = 11;
747 uint8_t egprs_ms_class = 11;
748 gprs_rlcmac_tbf *dl_tbf1, *dl_tbf2;
749 uint8_t numTs1 = 0, numTs2 = 0;
750
751 printf("Testing DL TS allocation for Multi UEs\n");
752
753 bts = the_bts.bts_data();
754 bts->alloc_algorithm = alloc_algorithm_b;
755
756 trx = &bts->trx[0];
757 trx->pdch[4].enable();
758 trx->pdch[5].enable();
759 trx->pdch[6].enable();
760 trx->pdch[7].enable();
761
Max92b7a502018-01-26 11:01:35 +0100762 dl_tbf1 = tbf_alloc_dl_tbf(bts, NULL, 0, ms_class, egprs_ms_class,
763 false);
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530764 OSMO_ASSERT(dl_tbf1);
765
766 for (int i = 0; i < 8; i++) {
767 if (dl_tbf1->pdch[i])
768 numTs1++;
769 }
770 OSMO_ASSERT(numTs1 == 4);
771 printf("TBF1: numTs(%d)\n", numTs1);
772
Max92b7a502018-01-26 11:01:35 +0100773 dl_tbf2 = tbf_alloc_dl_tbf(bts, NULL, 0, ms_class, egprs_ms_class,
774 false);
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530775 OSMO_ASSERT(dl_tbf2);
776
777 for (int i = 0; i < 8; i++) {
778 if (dl_tbf2->pdch[i])
779 numTs2++;
780 }
781
782 /*
783 * TODO: currently 2nd DL TBF gets 3 TS
784 * This behaviour will be fixed in subsequent patch
785 */
786 printf("TBF2: numTs(%d)\n", numTs2);
787 OSMO_ASSERT(numTs2 == 3);
788
789 tbf_free(dl_tbf1);
790 tbf_free(dl_tbf2);
791}
792
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200793int main(int argc, char **argv)
794{
795 tall_pcu_ctx = talloc_named_const(NULL, 1, "moiji-mobile AllocTest context");
796 if (!tall_pcu_ctx)
797 abort();
798
Neels Hofmeyr78ce5912017-02-08 17:07:31 +0100799 msgb_talloc_ctx_init(tall_pcu_ctx, 0);
Neels Hofmeyr42f2d612018-04-01 16:54:40 +0200800 osmo_init_logging2(tall_pcu_ctx, &gprs_log_info);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200801 log_set_use_color(osmo_stderr_target, 0);
802 log_set_print_filename(osmo_stderr_target, 0);
Jacob Erlbeck9ec49e22015-06-29 13:00:20 +0200803 if (getenv("LOGL_DEBUG"))
804 log_set_log_level(osmo_stderr_target, LOGL_DEBUG);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200805
806 test_alloc_a();
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100807 test_alloc_b();
Maxc59ef122017-11-27 13:21:41 +0100808 test_successive_allocations();
809 test_many_connections(alloc_algorithm_a, 160, "A");
Max01bd0cc2018-01-23 20:58:49 +0100810 test_many_connections(alloc_algorithm_b, 32, "B");
Maxc59ef122017-11-27 13:21:41 +0100811 test_many_connections(alloc_algorithm_dynamic, 160, "dynamic");
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530812 test_2_consecutive_dl_tbfs();
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200813 return EXIT_SUCCESS;
814}
815
816/*
817 * stubs that should not be reached
818 */
819extern "C" {
820void l1if_pdch_req() { abort(); }
821void l1if_connect_pdch() { abort(); }
822void l1if_close_pdch() { abort(); }
823void l1if_open_pdch() { abort(); }
824}