blob: bbc8c7e98af631063ffd03e9ad80c20b21babfa4 [file] [log] [blame]
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +02001/* AllocTest.cpp
2 *
3 * Copyright (C) 2013 by Holger Hans Peter Freyther
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20#include "gprs_rlcmac.h"
21#include "gprs_debug.h"
22#include "tbf.h"
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +020023#include "bts.h"
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020024
25#include <string.h>
26#include <stdio.h>
27
28extern "C" {
29#include <osmocom/core/application.h>
30#include <osmocom/core/msgb.h>
31#include <osmocom/core/talloc.h>
32#include <osmocom/core/utils.h>
33}
34
35/* globals used by the code */
36void *tall_pcu_ctx;
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020037int16_t spoof_mnc = 0, spoof_mcc = 0;
38
Daniel Willmann48aa0b02014-07-16 18:54:10 +020039static gprs_rlcmac_tbf *tbf_alloc(struct gprs_rlcmac_bts *bts,
Jacob Erlbecke2e004e2015-06-18 17:16:26 +020040 GprsMs *ms, gprs_rlcmac_tbf_direction dir,
Jacob Erlbeck5879c642015-07-10 10:41:36 +020041 uint8_t use_trx,
Jacob Erlbeck86b6f052015-11-27 15:17:34 +010042 uint8_t ms_class, uint8_t egprs_ms_class, uint8_t single_slot)
Daniel Willmann48aa0b02014-07-16 18:54:10 +020043{
44 if (dir == GPRS_RLCMAC_UL_TBF)
Jacob Erlbeck86b6f052015-11-27 15:17:34 +010045 return tbf_alloc_ul_tbf(bts, ms, use_trx,
46 ms_class, egprs_ms_class, single_slot);
Daniel Willmann48aa0b02014-07-16 18:54:10 +020047 else
Jacob Erlbeck86b6f052015-11-27 15:17:34 +010048 return tbf_alloc_dl_tbf(bts, ms, use_trx,
49 ms_class, egprs_ms_class, single_slot);
Daniel Willmann48aa0b02014-07-16 18:54:10 +020050}
51
Jacob Erlbeck61205a72015-07-09 11:35:50 +020052static void check_tfi_usage(BTS *the_bts)
53{
54 int pdch_no;
Jacob Erlbeck61205a72015-07-09 11:35:50 +020055
56 struct gprs_rlcmac_tbf *tfi_usage[8][8][2][32] = {{{{NULL}}}};
Jacob Erlbecked2dbf62015-12-28 19:15:40 +010057 LListHead<gprs_rlcmac_tbf> *tbf_lists[2] = {
58 &the_bts->ul_tbfs(),
59 &the_bts->dl_tbfs()
Jacob Erlbeck61205a72015-07-09 11:35:50 +020060 };
61
Jacob Erlbecked2dbf62015-12-28 19:15:40 +010062 LListHead<gprs_rlcmac_tbf> *pos;
Jacob Erlbeck61205a72015-07-09 11:35:50 +020063 gprs_rlcmac_tbf *tbf;
Jacob Erlbeck61205a72015-07-09 11:35:50 +020064 unsigned list_idx;
65 struct gprs_rlcmac_tbf **tbf_var;
66
67 for (list_idx = 0; list_idx < ARRAY_SIZE(tbf_lists); list_idx += 1)
68 {
69
Jacob Erlbecked2dbf62015-12-28 19:15:40 +010070 llist_for_each(pos, tbf_lists[list_idx]) {
71 tbf = pos->entry();
Jacob Erlbeck61205a72015-07-09 11:35:50 +020072 for (pdch_no = 0; pdch_no < 8; pdch_no += 1) {
73 struct gprs_rlcmac_pdch *pdch = tbf->pdch[pdch_no];
74 if (pdch == NULL)
75 continue;
76
77 tbf_var = &tfi_usage
78 [tbf->trx->trx_no]
79 [pdch_no]
80 [tbf->direction]
81 [tbf->tfi()];
82
83 OSMO_ASSERT(*tbf_var == NULL);
84 if (tbf->direction == GPRS_RLCMAC_DL_TBF) {
85 OSMO_ASSERT(pdch->dl_tbf_by_tfi(
86 tbf->tfi()) == tbf);
Jacob Erlbeck61205a72015-07-09 11:35:50 +020087 OSMO_ASSERT(the_bts->dl_tbf_by_tfi(
88 tbf->tfi(),
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +020089 tbf->trx->trx_no,
90 pdch_no) == tbf);
Jacob Erlbeck61205a72015-07-09 11:35:50 +020091 } else {
92 OSMO_ASSERT(pdch->ul_tbf_by_tfi(
93 tbf->tfi()) == tbf);
Jacob Erlbeck61205a72015-07-09 11:35:50 +020094 OSMO_ASSERT(the_bts->ul_tbf_by_tfi(
95 tbf->tfi(),
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +020096 tbf->trx->trx_no,
97 pdch_no) == tbf);
Jacob Erlbeck61205a72015-07-09 11:35:50 +020098 }
99 *tbf_var = tbf;
Jacob Erlbeck47a57f62015-07-08 12:53:16 +0200100 OSMO_ASSERT(pdch->assigned_tfi(tbf->direction) &
101 (1 << tbf->tfi()));
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200102 }
103 }
104 }
105}
106
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200107static void test_alloc_a(gprs_rlcmac_tbf_direction dir,
108 uint8_t slots, const int count)
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200109{
110 int tfi;
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200111 int i;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200112 uint8_t used_trx, tmp_trx;
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200113 BTS the_bts;
114 struct gprs_rlcmac_bts *bts;
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200115 struct gprs_rlcmac_tbf *tbfs[32*8+1] = { 0, };
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200116
117 printf("Testing alloc_a direction(%d)\n", dir);
118
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200119 bts = the_bts.bts_data();
120 bts->alloc_algorithm = alloc_algorithm_a;
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200121
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200122 struct gprs_rlcmac_trx *trx = &bts->trx[0];
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200123 for (i = 0; i < 8; i += 1)
124 if (slots & (1 << i))
125 trx->pdch[i].enable();
126
127 OSMO_ASSERT(count >= 0 && count <= (int)ARRAY_SIZE(tbfs));
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200128
129 /**
130 * Currently alloc_a will only allocate from the first
131 * PDCH and all possible usf's. We run out of usf's before
132 * we are out of tfi's. Observe this and make sure that at
133 * least this part is working okay.
134 */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200135 for (i = 0; i < (int)ARRAY_SIZE(tbfs); ++i) {
Jacob Erlbeck86b6f052015-11-27 15:17:34 +0100136 tbfs[i] = tbf_alloc(bts, NULL, dir, -1, 0, 0, 0);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200137 if (tbfs[i] == NULL)
138 break;
139
140 used_trx = tbfs[i]->trx->trx_no;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200141 tfi = the_bts.tfi_find_free(dir, &tmp_trx, used_trx);
142 OSMO_ASSERT(tbfs[i]->tfi() != tfi);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200143 }
144
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200145 check_tfi_usage(&the_bts);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200146
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200147 OSMO_ASSERT(i == count);
148
149 for (i = 0; i < count; ++i)
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200150 if (tbfs[i])
151 tbf_free(tbfs[i]);
152
Holger Hans Peter Freytherf3f1bde2016-02-22 15:14:01 +0100153 tbfs[0] = tbf_alloc(bts, NULL, dir, -1, 0, 0, 0);
154 OSMO_ASSERT(tbfs[0]);
155 tbf_free(tbfs[0]);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200156}
157
158static void test_alloc_a()
159{
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200160 /* slots 2 - 3 */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200161 test_alloc_a(GPRS_RLCMAC_DL_TBF, 0x0c, 32*2);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200162 test_alloc_a(GPRS_RLCMAC_UL_TBF, 0x0c, 14);
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200163
164 /* slots 1 - 5 */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200165 test_alloc_a(GPRS_RLCMAC_DL_TBF, 0x1e, 32*4);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200166 test_alloc_a(GPRS_RLCMAC_UL_TBF, 0x1e, 28);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200167}
168
Max2ecf0fd2017-11-21 18:13:31 +0100169static void dump_assignment(struct gprs_rlcmac_tbf *tbf, const char *dir, bool verbose)
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100170{
Max2ecf0fd2017-11-21 18:13:31 +0100171 if (!verbose)
172 return;
173
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200174 for (size_t i = 0; i < ARRAY_SIZE(tbf->pdch); ++i)
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100175 if (tbf->pdch[i])
Neels Hofmeyrd34646a2017-02-08 17:07:40 +0100176 printf("PDCH[%zu] is used for %s\n", i, dir);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100177 printf("PDCH[%d] is control_ts for %s\n", tbf->control_ts, dir);
178 printf("PDCH[%d] is first common for %s\n", tbf->first_common_ts, dir);
179}
180
Max2ecf0fd2017-11-21 18:13:31 +0100181#define ENABLE_PDCH(ts_no, enable_flag, trx) \
182 if (enable_flag) \
183 trx->pdch[ts_no].enable();
184
185static inline void enable_ts_on_bts(struct gprs_rlcmac_bts *bts,
186 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 +0100187{
Max2ecf0fd2017-11-21 18:13:31 +0100188 struct gprs_rlcmac_trx *trx = &bts->trx[0];
189
190 ENABLE_PDCH(0, ts0, trx);
191 ENABLE_PDCH(1, ts1, trx);
192 ENABLE_PDCH(2, ts2, trx);
193 ENABLE_PDCH(3, ts3, trx);
194 ENABLE_PDCH(4, ts4, trx);
195 ENABLE_PDCH(5, ts5, trx);
196 ENABLE_PDCH(6, ts6, trx);
197 ENABLE_PDCH(7, ts7, trx);
198}
199
200static inline bool test_alloc_b_ul_dl(bool ts0, bool ts1, bool ts2, bool ts3, bool ts4, bool ts5, bool ts6, bool ts7,
201 uint8_t ms_class, bool verbose)
202{
203 BTS the_bts;
204 struct gprs_rlcmac_bts *bts = the_bts.bts_data();
205 gprs_rlcmac_ul_tbf *ul_tbf;
206 gprs_rlcmac_dl_tbf *dl_tbf;
207
208 if (verbose)
209 printf("Testing UL then DL assignment.\n");
210
211 bts->alloc_algorithm = alloc_algorithm_b;
212
213 enable_ts_on_bts(bts, ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7);
214
215 ul_tbf = tbf_alloc_ul_tbf(bts, NULL, -1, ms_class, 0, 1);
216 if (!ul_tbf)
217 return false;
218
219 OSMO_ASSERT(ul_tbf->ms());
220 OSMO_ASSERT(ul_tbf->ms()->current_trx());
221
222 dump_assignment(ul_tbf, "UL", verbose);
223
224 /* assume final ack has not been sent */
225 dl_tbf = tbf_alloc_dl_tbf(bts, ul_tbf->ms(), ul_tbf->ms()->current_trx()->trx_no, ms_class, 0, 0);
226 if (!dl_tbf)
227 return false;
228
229 dump_assignment(dl_tbf, "DL", verbose);
230
231 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
232
233 check_tfi_usage(&the_bts);
234
235 tbf_free(dl_tbf);
236 tbf_free(ul_tbf);
237
238 return true;
239}
240
241static inline bool test_alloc_b_dl_ul(bool ts0, bool ts1, bool ts2, bool ts3, bool ts4, bool ts5, bool ts6, bool ts7,
242 uint8_t ms_class, bool verbose)
243{
244 BTS the_bts;
245 struct gprs_rlcmac_bts *bts = the_bts.bts_data();
246 gprs_rlcmac_ul_tbf *ul_tbf;
247 gprs_rlcmac_dl_tbf *dl_tbf;
248
249 if (verbose)
250 printf("Testing DL then UL assignment followed by update\n");
251
252 bts->alloc_algorithm = alloc_algorithm_b;
253
254 enable_ts_on_bts(bts, ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7);
255
256 dl_tbf = tbf_alloc_dl_tbf(bts, NULL, -1, ms_class, 0, 1);
257 if (!dl_tbf)
258 return false;
259
260 dl_tbf->update_ms(0x23, GPRS_RLCMAC_DL_TBF);
261
262 OSMO_ASSERT(dl_tbf->ms());
263 OSMO_ASSERT(dl_tbf->ms()->current_trx());
264
265 dump_assignment(dl_tbf, "DL", verbose);
266
267 ul_tbf = tbf_alloc_ul_tbf(bts, dl_tbf->ms(), dl_tbf->ms()->current_trx()->trx_no, ms_class, 0, 0);
268 if (!ul_tbf)
269 return false;
270
271 ul_tbf->update_ms(0x23, GPRS_RLCMAC_UL_TBF);
272 ul_tbf->m_contention_resolution_done = 1;
273
274 dump_assignment(ul_tbf, "UL", verbose);
275
276 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
277
278 /* now update the dl_tbf */
279 dl_tbf->update();
280 dump_assignment(dl_tbf, "DL", verbose);
281 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
282
283 check_tfi_usage(&the_bts);
284
285 tbf_free(dl_tbf);
286 tbf_free(ul_tbf);
287
288 return true;
289}
290
291static inline bool test_alloc_b_jolly(uint8_t ms_class)
292{
293 BTS the_bts;
294 struct gprs_rlcmac_bts *bts = the_bts.bts_data();
295 int tfi;
296 uint8_t trx_no;
297 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
298
299 printf("Testing jolly example\n");
300
301 bts->alloc_algorithm = alloc_algorithm_b;
302
303 enable_ts_on_bts(bts, false, true, true, true, true, false, false, false);
304
305 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
306 OSMO_ASSERT(tfi >= 0);
307 ul_tbf = tbf_alloc_ul_tbf(bts, NULL, .1, ms_class, 0, 0);
308 if (!ul_tbf)
309 return false;
310
311 OSMO_ASSERT(ul_tbf->ms());
312 OSMO_ASSERT(ul_tbf->ms()->current_trx());
313 trx_no = ul_tbf->ms()->current_trx()->trx_no;
314 dump_assignment(ul_tbf, "UL", true);
315
316 /* assume final ack has not been sent */
317 dl_tbf = tbf_alloc_dl_tbf(bts, ul_tbf->ms(), trx_no, ms_class, 0, 0);
318 if (!dl_tbf)
319 return false;
320
321 dump_assignment(dl_tbf, "DL", true);
322
323 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
324
325 check_tfi_usage(&the_bts);
326
327 tbf_free(dl_tbf);
328 tbf_free(ul_tbf);
329
330 return true;
331}
332
333static void test_alloc_b_for_ms(uint8_t ms_class)
334{
335 bool rc;
336
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100337 printf("Going to test multislot assignment MS_CLASS=%d\n", ms_class);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100338 /*
339 * PDCH is on TS 6,7,8 and we start with a UL allocation and
340 * then follow two DL allocations (once single, once normal).
341 *
342 * Uplink assigned and still available..
343 */
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100344
Max2ecf0fd2017-11-21 18:13:31 +0100345 rc = test_alloc_b_ul_dl(false, false, false, false, false, true, true, true, ms_class, true);
346 if (!rc)
347 return;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100348
349 /**
350 * Test with the other order.. first DL and then UL
351 */
Max2ecf0fd2017-11-21 18:13:31 +0100352 rc = test_alloc_b_dl_ul(false, false, false, false, false, true, true, true, ms_class, true);
353 if (!rc)
354 return;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100355
356 /* Andreas osmocom-pcu example */
Max2ecf0fd2017-11-21 18:13:31 +0100357 test_alloc_b_jolly(ms_class);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100358}
359
Max2ecf0fd2017-11-21 18:13:31 +0100360static 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 +0100361{
Max2ecf0fd2017-11-21 18:13:31 +0100362 bool rc;
363
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100364 /* we can test the allocation failures differently */
365 if (!ts0 && !ts1 && !ts2 && !ts3 && !ts4 && !ts5 && !ts6 && !ts7)
366 return;
367
368 printf("Mass test: TS0(%c%c%c%c%c%c%c%c)TS7 MS_Class=%d\n",
369 ts0 ? 'O' : 'x',
370 ts1 ? 'O' : 'x',
371 ts2 ? 'O' : 'x',
372 ts3 ? 'O' : 'x',
373 ts4 ? 'O' : 'x',
374 ts5 ? 'O' : 'x',
375 ts6 ? 'O' : 'x',
376 ts7 ? 'O' : 'x', ms_class);
377 fflush(stdout);
378
Max2ecf0fd2017-11-21 18:13:31 +0100379 rc = test_alloc_b_ul_dl(ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7, ms_class, false);
380 if (!rc)
381 return;
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100382
383 /**
384 * Test with the other order.. first DL and then UL
385 */
Max2ecf0fd2017-11-21 18:13:31 +0100386 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 +0100387}
388
389static void test_all_alloc_b()
390{
391 /* it is a bit crazy... */
392 for (uint8_t ts0 = 0; ts0 < 2; ++ts0)
393 for (uint8_t ts1 = 0; ts1 < 2; ++ts1)
394 for (uint8_t ts2 = 0; ts2 < 2; ++ts2)
395 for (uint8_t ts3 = 0; ts3 < 2; ++ts3)
396 for (uint8_t ts4 = 0; ts4 < 2; ++ts4)
397 for (uint8_t ts5 = 0; ts5 < 2; ++ts5)
398 for (uint8_t ts6 = 0; ts6 < 2; ++ts6)
399 for (uint8_t ts7 = 0; ts7 < 2; ++ts7)
400 for (int ms_class = 0; ms_class < 30; ++ms_class)
Max2ecf0fd2017-11-21 18:13:31 +0100401 test_alloc_mass(ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7, ms_class);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100402}
403
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100404static void test_alloc_b()
405{
406 for (int i = 0; i < 30; ++i)
Max2ecf0fd2017-11-21 18:13:31 +0100407 test_alloc_b_for_ms(i);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100408
409 test_all_alloc_b();
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100410}
411
Jacob Erlbecke5655642015-06-29 12:19:52 +0200412typedef int (*algo_t)(struct gprs_rlcmac_bts *bts,
413 struct GprsMs *ms,
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200414 struct gprs_rlcmac_tbf *tbf, uint32_t cust, uint8_t single,
415 int use_trx);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200416
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200417static char get_dir_char(uint8_t mask, uint8_t tx, uint8_t rx, uint8_t busy)
Jacob Erlbecke5655642015-06-29 12:19:52 +0200418{
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200419 int offs = busy ? 32 : 0;
420 return (mask & tx & rx) ? 'C' + offs :
421 (mask & tx) ? 'U' + offs :
422 (mask & rx) ? 'D' + offs :
Jacob Erlbecke5655642015-06-29 12:19:52 +0200423 '.';
424}
425
426enum test_mode {
427 TEST_MODE_UL_ONLY,
428 TEST_MODE_DL_ONLY,
429 TEST_MODE_UL_AND_DL,
430 TEST_MODE_DL_AND_UL,
431 TEST_MODE_DL_AFTER_UL,
432 TEST_MODE_UL_AFTER_DL,
433};
434
Maxc59ef122017-11-27 13:21:41 +0100435static inline char *test_mode_descr(enum test_mode t)
436{
437 switch (t) {
438 case TEST_MODE_UL_ONLY: return (char*)"UL only";
439 case TEST_MODE_DL_ONLY: return (char*)"DL only";
440 case TEST_MODE_UL_AND_DL: return (char*)"UL and DL";
441 case TEST_MODE_DL_AND_UL: return (char*)"DL and UL";
442 case TEST_MODE_DL_AFTER_UL: return (char*)"DL after UL";
443 case TEST_MODE_UL_AFTER_DL: return (char*)"UL after DL";
444 default: return NULL;
445 }
446}
447
Jacob Erlbecke5655642015-06-29 12:19:52 +0200448static GprsMs *alloc_tbfs(BTS *the_bts, GprsMs *ms, unsigned ms_class,
449 enum test_mode mode)
450{
451 struct gprs_rlcmac_bts *bts;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200452 uint8_t trx_no = -1;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200453
454 bts = the_bts->bts_data();
455
456 gprs_rlcmac_tbf *tbf = NULL;
457
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200458 if (ms && ms->current_trx())
459 trx_no = ms->current_trx()->trx_no;
460
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200461 GprsMs::Guard guard1(ms);
462
Jacob Erlbecke5655642015-06-29 12:19:52 +0200463 /* Allocate what is needed first */
464 switch (mode) {
465 case TEST_MODE_UL_ONLY:
466 case TEST_MODE_DL_AFTER_UL:
467 case TEST_MODE_UL_AND_DL:
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200468 if (ms && ms->ul_tbf())
469 tbf_free(ms->ul_tbf());
Jacob Erlbeck86b6f052015-11-27 15:17:34 +0100470 tbf = tbf_alloc_ul_tbf(bts, ms, trx_no, ms_class, 0, 0);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200471 if (tbf == NULL)
472 return NULL;
473 break;
474 case TEST_MODE_DL_ONLY:
475 case TEST_MODE_UL_AFTER_DL:
476 case TEST_MODE_DL_AND_UL:
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200477 if (ms && ms->dl_tbf())
478 tbf_free(ms->dl_tbf());
Jacob Erlbeck86b6f052015-11-27 15:17:34 +0100479 tbf = tbf_alloc_dl_tbf(bts, ms, trx_no, ms_class, 0, 0);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200480 if (tbf == NULL)
481 return NULL;
482 }
483
484 OSMO_ASSERT(tbf);
485 OSMO_ASSERT(tbf->ms());
486 OSMO_ASSERT(ms == NULL || ms == tbf->ms());
487 ms = tbf->ms();
488
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200489 GprsMs::Guard guard2(ms);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200490
Jacob Erlbeck14376a72015-07-07 11:31:28 +0200491 /* Continue with what is needed next */
492 switch (mode) {
493 case TEST_MODE_UL_ONLY:
494 case TEST_MODE_DL_ONLY:
495 /* We are done */
496 break;
497
498 case TEST_MODE_DL_AFTER_UL:
499 case TEST_MODE_UL_AND_DL:
500 ms = alloc_tbfs(the_bts, ms, ms_class, TEST_MODE_DL_ONLY);
501 break;
502
503 case TEST_MODE_UL_AFTER_DL:
504 case TEST_MODE_DL_AND_UL:
505 ms = alloc_tbfs(the_bts, ms, ms_class, TEST_MODE_UL_ONLY);
506 break;
507 }
508
Jacob Erlbecke5655642015-06-29 12:19:52 +0200509 /* Optionally delete the TBF */
510 switch (mode) {
511 case TEST_MODE_DL_AFTER_UL:
512 case TEST_MODE_UL_AFTER_DL:
513 tbf_free(tbf);
Jacob Erlbeck0f352a62015-07-16 18:23:33 +0200514 tbf = NULL;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200515 break;
516
517 default:
518 break;
519 }
520
Jacob Erlbeck0f352a62015-07-16 18:23:33 +0200521 if (!ms && tbf)
522 tbf_free(tbf);
523
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200524 return guard2.is_idle() ? NULL : ms;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200525}
526
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200527static unsigned alloc_many_tbfs(BTS *the_bts, unsigned min_class,
528 unsigned max_class, enum test_mode mode)
Jacob Erlbecke5655642015-06-29 12:19:52 +0200529{
Jacob Erlbecke5655642015-06-29 12:19:52 +0200530 unsigned counter;
531 unsigned ms_class = min_class;
532
Jacob Erlbecke5655642015-06-29 12:19:52 +0200533 for (counter = 0; 1; counter += 1) {
534 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
535 uint8_t ul_slots = 0;
536 uint8_t dl_slots = 0;
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200537 uint8_t busy_slots = 0;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200538 unsigned i;
539 int tfi = -1;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200540 int tfi2;
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200541 uint8_t trx_no2;
542 struct gprs_rlcmac_trx *trx;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200543 GprsMs *ms;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200544 enum gprs_rlcmac_tbf_direction dir;
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200545 uint32_t tlli = counter + 0xc0000000;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200546
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200547 ms = the_bts->ms_by_tlli(tlli);
548
549 ms = alloc_tbfs(the_bts, ms, ms_class, mode);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200550 if (!ms)
551 break;
552
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200553 ms->set_tlli(tlli);
554
Jacob Erlbecke5655642015-06-29 12:19:52 +0200555 ul_tbf = ms->ul_tbf();
556 dl_tbf = ms->dl_tbf();
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200557 trx = ms->current_trx();
558
559 OSMO_ASSERT(ul_tbf || dl_tbf);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200560
561 if (ul_tbf) {
562 ul_slots = 1 << ul_tbf->first_common_ts;
563 tfi = ul_tbf->tfi();
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200564 dir = GPRS_RLCMAC_UL_TBF;
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200565 } else {
Jacob Erlbecke5655642015-06-29 12:19:52 +0200566 ul_slots = 1 << dl_tbf->first_common_ts;
567 tfi = dl_tbf->tfi();
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200568 dir = GPRS_RLCMAC_DL_TBF;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200569 }
570
571 for (i = 0; dl_tbf && i < ARRAY_SIZE(dl_tbf->pdch); i += 1)
572 if (dl_tbf->pdch[i])
573 dl_slots |= 1 << i;
574
Max5b0df1f2017-09-11 10:38:59 +0200575 for (i = 0; ul_tbf && i < ARRAY_SIZE(ul_tbf->pdch); i += 1)
576 if (ul_tbf->pdch[i])
577 ul_slots |= 1 << i;
578
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200579 for (i = 0; trx && i < ARRAY_SIZE(trx->pdch); i += 1) {
580 struct gprs_rlcmac_pdch *pdch = &trx->pdch[i];
581
582 if (ul_tbf && dl_tbf)
583 continue;
584
585 if (ul_tbf &&
586 pdch->assigned_tfi(GPRS_RLCMAC_DL_TBF) != 0xffffffff)
587 continue;
588
589 if (dl_tbf &&
590 pdch->assigned_tfi(GPRS_RLCMAC_UL_TBF) != 0xffffffff)
591 continue;
592
593 busy_slots |= 1 << i;
594 }
595
Jacob Erlbecke5655642015-06-29 12:19:52 +0200596 printf(" TBF[%d] class %d reserves %c%c%c%c%c%c%c%c\n",
597 tfi, ms_class,
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200598 get_dir_char(0x01, ul_slots, dl_slots, busy_slots),
599 get_dir_char(0x02, ul_slots, dl_slots, busy_slots),
600 get_dir_char(0x04, ul_slots, dl_slots, busy_slots),
601 get_dir_char(0x08, ul_slots, dl_slots, busy_slots),
602 get_dir_char(0x10, ul_slots, dl_slots, busy_slots),
603 get_dir_char(0x20, ul_slots, dl_slots, busy_slots),
604 get_dir_char(0x40, ul_slots, dl_slots, busy_slots),
605 get_dir_char(0x80, ul_slots, dl_slots, busy_slots));
Jacob Erlbecke5655642015-06-29 12:19:52 +0200606
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200607 if (tfi >= 0) {
608 OSMO_ASSERT(ms->current_trx());
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200609 tfi2 = the_bts->tfi_find_free(dir, &trx_no2,
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200610 ms->current_trx()->trx_no);
611 OSMO_ASSERT(tfi != tfi2);
Jacob Erlbeck7b3675b2015-07-16 18:28:22 +0200612 OSMO_ASSERT(tfi2 < 0 ||
613 trx_no2 == ms->current_trx()->trx_no);
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200614 }
615
Jacob Erlbecke5655642015-06-29 12:19:52 +0200616 ms_class += 1;
617 if (ms_class > max_class)
618 ms_class = min_class;
619 }
620
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200621 return counter;
622}
623
624static void test_successive_allocation(algo_t algo, unsigned min_class,
625 unsigned max_class, enum test_mode mode,
626 unsigned expect_num, const char *text)
627{
628 BTS the_bts;
629 struct gprs_rlcmac_bts *bts;
630 struct gprs_rlcmac_trx *trx;
631 unsigned counter;
632
Maxc59ef122017-11-27 13:21:41 +0100633 printf("Going to test assignment with many TBF, algorithm %s class %u..%u (%s)\n",
634 text, min_class, max_class, test_mode_descr(mode));
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200635
636 bts = the_bts.bts_data();
637 bts->alloc_algorithm = algo;
638
639 trx = &bts->trx[0];
640 trx->pdch[3].enable();
641 trx->pdch[4].enable();
642 trx->pdch[5].enable();
643 trx->pdch[6].enable();
644 trx->pdch[7].enable();
645
646 counter = alloc_many_tbfs(&the_bts, min_class, max_class, mode);
647
Maxc59ef122017-11-27 13:21:41 +0100648 printf(" Successfully allocated %u UL TBFs, algorithm %s class %u..%u (%s)\n",
649 counter, text, min_class, max_class, test_mode_descr(mode));
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200650 if (counter != expect_num)
Maxc59ef122017-11-27 13:21:41 +0100651 fprintf(stderr, " Expected %u TBFs (got %u), algorithm %s class %u..%u (%s)\n",
652 expect_num, counter, text, min_class, max_class, test_mode_descr(mode));
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200653
Jacob Erlbeckec478752015-06-19 16:35:38 +0200654 OSMO_ASSERT(counter == expect_num);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200655
656 check_tfi_usage(&the_bts);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200657}
658
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200659static void test_many_connections(algo_t algo, unsigned expect_num,
660 const char *text)
661{
662 BTS the_bts;
663 struct gprs_rlcmac_bts *bts;
664 struct gprs_rlcmac_trx *trx;
665 int counter1, counter2 = -1;
666 unsigned i;
667 enum test_mode mode_seq[] = {
668 TEST_MODE_DL_AFTER_UL,
669 TEST_MODE_UL_ONLY,
670 TEST_MODE_DL_AFTER_UL,
671 TEST_MODE_DL_ONLY,
672 };
673
Maxc59ef122017-11-27 13:21:41 +0100674 printf("Going to test assignment with many connections, algorithm %s\n", text);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200675
676 bts = the_bts.bts_data();
677 bts->alloc_algorithm = algo;
678
679 trx = &bts->trx[0];
680 trx->pdch[3].enable();
681 trx->pdch[4].enable();
682 trx->pdch[5].enable();
683 trx->pdch[6].enable();
684 trx->pdch[7].enable();
685
686 for (i = 0; i < ARRAY_SIZE(mode_seq); i += 1) {
687 counter1 = alloc_many_tbfs(&the_bts, 1, 29, mode_seq[i]);
688 fprintf(stderr, " Allocated %d TBFs (previously %d)\n",
689 counter1, counter2);
690
691 check_tfi_usage(&the_bts);
692
693 /* This will stop earlier due to USF shortage */
694 if (mode_seq[i] == TEST_MODE_UL_ONLY)
695 continue;
696
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200697 if (counter2 >= 0) {
698 if (counter1 < counter2)
699 fprintf(stderr, " Expected %d >= %d in %s\n",
700 counter1, counter2, text);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200701 OSMO_ASSERT(counter1 >= counter2);
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200702 }
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200703
704 counter2 = counter1;
705 }
706
707 printf(" Successfully allocated %d TBFs\n", counter1);
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200708 if (counter1 != (int)expect_num)
Maxc59ef122017-11-27 13:21:41 +0100709 fprintf(stderr, " Expected %d TBFs (got %d) for algorithm %s\n", expect_num, counter1, text);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200710
711 OSMO_ASSERT(expect_num == (unsigned)counter1);
712}
713
Maxc59ef122017-11-27 13:21:41 +0100714static 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 +0200715{
Maxc59ef122017-11-27 13:21:41 +0100716 test_successive_allocation(alloc_algorithm_a, 1, 1, mode, exp_A, "A");
717 test_successive_allocation(alloc_algorithm_b, 10, 10, mode, exp_B, "B");
718 test_successive_allocation(alloc_algorithm_dynamic, 10, 10, mode, exp_dyn, "dynamic");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200719}
720
Maxc59ef122017-11-27 13:21:41 +0100721static void test_successive_allocations()
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200722{
Maxc59ef122017-11-27 13:21:41 +0100723 test_successive_allocation(alloc_algorithm_a, 1, 1, TEST_MODE_UL_AND_DL, 35, "A");
724 test_successive_allocation(alloc_algorithm_b, 10, 10, TEST_MODE_UL_AND_DL, 32, "B");
725 test_successive_allocation(alloc_algorithm_b, 12, 12, TEST_MODE_UL_AND_DL, 32, "B");
726
727 test_successive_allocation(alloc_algorithm_b, 1, 12, TEST_MODE_UL_AND_DL, 32, "B");
728 test_successive_allocation(alloc_algorithm_b, 1, 29, TEST_MODE_UL_AND_DL, 32, "B");
729 test_successive_allocation(alloc_algorithm_dynamic, 1, 29, TEST_MODE_UL_AND_DL, 35, "dynamic");
730
731 test_a_b_dyn(TEST_MODE_DL_AND_UL, 35, 32, 32);
732 test_a_b_dyn(TEST_MODE_DL_AFTER_UL, 160, 32, 95);
733 test_a_b_dyn(TEST_MODE_UL_AFTER_DL, 35, 32, 35);
734 test_a_b_dyn(TEST_MODE_UL_ONLY, 35, 32, 35);
735 test_a_b_dyn(TEST_MODE_DL_ONLY, 160, 32, 101);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200736}
737
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530738static void test_2_consecutive_dl_tbfs()
739{
740 BTS the_bts;
741 struct gprs_rlcmac_bts *bts;
742 struct gprs_rlcmac_trx *trx;
743 uint8_t ms_class = 11;
744 uint8_t egprs_ms_class = 11;
745 gprs_rlcmac_tbf *dl_tbf1, *dl_tbf2;
746 uint8_t numTs1 = 0, numTs2 = 0;
747
748 printf("Testing DL TS allocation for Multi UEs\n");
749
750 bts = the_bts.bts_data();
751 bts->alloc_algorithm = alloc_algorithm_b;
752
753 trx = &bts->trx[0];
754 trx->pdch[4].enable();
755 trx->pdch[5].enable();
756 trx->pdch[6].enable();
757 trx->pdch[7].enable();
758
759 dl_tbf1 = tbf_alloc_dl_tbf(bts, NULL, 0, ms_class, egprs_ms_class, 0);
760 OSMO_ASSERT(dl_tbf1);
761
762 for (int i = 0; i < 8; i++) {
763 if (dl_tbf1->pdch[i])
764 numTs1++;
765 }
766 OSMO_ASSERT(numTs1 == 4);
767 printf("TBF1: numTs(%d)\n", numTs1);
768
769 dl_tbf2 = tbf_alloc_dl_tbf(bts, NULL, 0, ms_class, egprs_ms_class, 0);
770 OSMO_ASSERT(dl_tbf2);
771
772 for (int i = 0; i < 8; i++) {
773 if (dl_tbf2->pdch[i])
774 numTs2++;
775 }
776
777 /*
778 * TODO: currently 2nd DL TBF gets 3 TS
779 * This behaviour will be fixed in subsequent patch
780 */
781 printf("TBF2: numTs(%d)\n", numTs2);
782 OSMO_ASSERT(numTs2 == 3);
783
784 tbf_free(dl_tbf1);
785 tbf_free(dl_tbf2);
786}
787
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200788int main(int argc, char **argv)
789{
790 tall_pcu_ctx = talloc_named_const(NULL, 1, "moiji-mobile AllocTest context");
791 if (!tall_pcu_ctx)
792 abort();
793
Neels Hofmeyr78ce5912017-02-08 17:07:31 +0100794 msgb_talloc_ctx_init(tall_pcu_ctx, 0);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200795 osmo_init_logging(&gprs_log_info);
796 log_set_use_color(osmo_stderr_target, 0);
797 log_set_print_filename(osmo_stderr_target, 0);
Jacob Erlbeck9ec49e22015-06-29 13:00:20 +0200798 if (getenv("LOGL_DEBUG"))
799 log_set_log_level(osmo_stderr_target, LOGL_DEBUG);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200800
801 test_alloc_a();
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100802 test_alloc_b();
Maxc59ef122017-11-27 13:21:41 +0100803 test_successive_allocations();
804 test_many_connections(alloc_algorithm_a, 160, "A");
805 test_many_connections(alloc_algorithm_b, 32, "B");
806 test_many_connections(alloc_algorithm_dynamic, 160, "dynamic");
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530807 test_2_consecutive_dl_tbfs();
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200808 return EXIT_SUCCESS;
809}
810
811/*
812 * stubs that should not be reached
813 */
814extern "C" {
815void l1if_pdch_req() { abort(); }
816void l1if_connect_pdch() { abort(); }
817void l1if_close_pdch() { abort(); }
818void l1if_open_pdch() { abort(); }
819}