blob: b5d4b9546792c951aba8ed29e76dd927504fc9a0 [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.
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020014 */
15
16#include "gprs_rlcmac.h"
17#include "gprs_debug.h"
18#include "tbf.h"
Pau Espin Pedrol9d1cdb12019-09-25 17:47:02 +020019#include "tbf_ul.h"
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010020#include "tbf_dl.h"
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +020021#include "bts.h"
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010022#include "gprs_ms.h"
Oliver Smith3f794702021-08-23 14:19:21 +020023#include "bts_pch_timer.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>
Pau Espin Pedroldc2aaac2021-05-14 12:50:46 +020034#include <osmocom/core/fsm.h>
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020035}
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,
Pau Espin Pedrol322456e2020-05-08 18:15:59 +020044 uint8_t use_trx, bool single_slot)
Daniel Willmann48aa0b02014-07-16 18:54:10 +020045{
Pau Espin Pedrol322456e2020-05-08 18:15:59 +020046 OSMO_ASSERT(ms != NULL);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +020047
Daniel Willmann48aa0b02014-07-16 18:54:10 +020048 if (dir == GPRS_RLCMAC_UL_TBF)
Pau Espin Pedrol17402a52020-05-08 17:44:33 +020049 return tbf_alloc_ul_tbf(bts, ms, use_trx, single_slot);
Daniel Willmann48aa0b02014-07-16 18:54:10 +020050 else
Pau Espin Pedrol322456e2020-05-08 18:15:59 +020051 return tbf_alloc_dl_tbf(bts, ms, use_trx, single_slot);
Daniel Willmann48aa0b02014-07-16 18:54:10 +020052}
53
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010054static void check_tfi_usage(struct gprs_rlcmac_bts *bts)
Jacob Erlbeck61205a72015-07-09 11:35:50 +020055{
56 int pdch_no;
Jacob Erlbeck61205a72015-07-09 11:35:50 +020057
58 struct gprs_rlcmac_tbf *tfi_usage[8][8][2][32] = {{{{NULL}}}};
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010059 struct llist_head *tbf_lists[2] = {
Pau Espin Pedrol1a1557a2021-05-13 18:39:36 +020060 &bts->trx[0].ul_tbfs,
61 &bts->trx[0].dl_tbfs
Jacob Erlbeck61205a72015-07-09 11:35:50 +020062 };
63
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010064 struct llist_item *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
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010072 llist_for_each_entry(pos, tbf_lists[list_idx], list) {
73 tbf = (struct gprs_rlcmac_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);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010089 OSMO_ASSERT(bts_dl_tbf_by_tfi(bts,
Jacob Erlbeck61205a72015-07-09 11:35:50 +020090 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);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010096 OSMO_ASSERT(bts_ul_tbf_by_tfi(bts,
Jacob Erlbeck61205a72015-07-09 11:35:50 +020097 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;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100115 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200116 GprsMs *ms;
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
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100121 the_pcu->alloc_algorithm = alloc_algorithm_a;
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200122
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200123 struct gprs_rlcmac_trx *trx = &bts->trx[0];
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200124 for (i = 0; i < 8; i += 1)
125 if (slots & (1 << i))
126 trx->pdch[i].enable();
127
128 OSMO_ASSERT(count >= 0 && count <= (int)ARRAY_SIZE(tbfs));
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200129
130 /**
131 * Currently alloc_a will only allocate from the first
132 * PDCH and all possible usf's. We run out of usf's before
133 * we are out of tfi's. Observe this and make sure that at
134 * least this part is working okay.
135 */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200136 for (i = 0; i < (int)ARRAY_SIZE(tbfs); ++i) {
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100137 ms = bts_alloc_ms(bts, 0, 0);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200138 tbfs[i] = tbf_alloc(bts, ms, dir, -1, 0);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200139 if (tbfs[i] == NULL)
140 break;
141
142 used_trx = tbfs[i]->trx->trx_no;
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100143 tfi = bts_tfi_find_free(bts, dir, &tmp_trx, used_trx);
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200144 OSMO_ASSERT(tbfs[i]->tfi() != tfi);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200145 }
146
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100147 check_tfi_usage(bts);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200148
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200149 OSMO_ASSERT(i == count);
150
Pau Espin Pedrol0dcbc072021-11-10 19:09:10 +0100151 OSMO_ASSERT(bts_all_pdch_allocated(bts));
152
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200153 for (i = 0; i < count; ++i)
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200154 if (tbfs[i])
155 tbf_free(tbfs[i]);
156
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100157 ms = bts_alloc_ms(bts, 0, 0);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200158 tbfs[0] = tbf_alloc(bts, ms, dir, -1, 0);
Holger Hans Peter Freytherf3f1bde2016-02-22 15:14:01 +0100159 OSMO_ASSERT(tbfs[0]);
160 tbf_free(tbfs[0]);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100161 talloc_free(bts);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200162}
163
164static void test_alloc_a()
165{
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200166 /* slots 2 - 3 */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200167 test_alloc_a(GPRS_RLCMAC_DL_TBF, 0x0c, 32*2);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200168 test_alloc_a(GPRS_RLCMAC_UL_TBF, 0x0c, 14);
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200169
170 /* slots 1 - 5 */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200171 test_alloc_a(GPRS_RLCMAC_DL_TBF, 0x1e, 32*4);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200172 test_alloc_a(GPRS_RLCMAC_UL_TBF, 0x1e, 28);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200173}
174
Max2ecf0fd2017-11-21 18:13:31 +0100175static void dump_assignment(struct gprs_rlcmac_tbf *tbf, const char *dir, bool verbose)
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100176{
Max2ecf0fd2017-11-21 18:13:31 +0100177 if (!verbose)
178 return;
179
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200180 for (size_t i = 0; i < ARRAY_SIZE(tbf->pdch); ++i)
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100181 if (tbf->pdch[i])
Neels Hofmeyrd34646a2017-02-08 17:07:40 +0100182 printf("PDCH[%zu] is used for %s\n", i, dir);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100183 printf("PDCH[%d] is control_ts for %s\n", tbf->control_ts, dir);
184 printf("PDCH[%d] is first common for %s\n", tbf->first_common_ts, dir);
185}
186
Max2ecf0fd2017-11-21 18:13:31 +0100187#define ENABLE_PDCH(ts_no, enable_flag, trx) \
188 if (enable_flag) \
189 trx->pdch[ts_no].enable();
190
191static inline void enable_ts_on_bts(struct gprs_rlcmac_bts *bts,
192 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 +0100193{
Max2ecf0fd2017-11-21 18:13:31 +0100194 struct gprs_rlcmac_trx *trx = &bts->trx[0];
195
196 ENABLE_PDCH(0, ts0, trx);
197 ENABLE_PDCH(1, ts1, trx);
198 ENABLE_PDCH(2, ts2, trx);
199 ENABLE_PDCH(3, ts3, trx);
200 ENABLE_PDCH(4, ts4, trx);
201 ENABLE_PDCH(5, ts5, trx);
202 ENABLE_PDCH(6, ts6, trx);
203 ENABLE_PDCH(7, ts7, trx);
204}
205
206static inline bool test_alloc_b_ul_dl(bool ts0, bool ts1, bool ts2, bool ts3, bool ts4, bool ts5, bool ts6, bool ts7,
207 uint8_t ms_class, bool verbose)
208{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100209 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200210 GprsMs *ms;
Max2ecf0fd2017-11-21 18:13:31 +0100211 gprs_rlcmac_ul_tbf *ul_tbf;
212 gprs_rlcmac_dl_tbf *dl_tbf;
213
214 if (verbose)
215 printf("Testing UL then DL assignment.\n");
216
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100217 the_pcu->alloc_algorithm = alloc_algorithm_b;
Max2ecf0fd2017-11-21 18:13:31 +0100218
219 enable_ts_on_bts(bts, ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7);
220
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100221 ms = bts_alloc_ms(bts, ms_class, 0);
Pau Espin Pedrol1e009472021-01-11 20:40:19 +0100222 /* Avoid delaying free to avoid tons of to-be-freed ms objects queuing */
223 ms_set_timeout(ms, 0);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200224 ul_tbf = tbf_alloc_ul_tbf(bts, ms, -1, true);
Max2ecf0fd2017-11-21 18:13:31 +0100225 if (!ul_tbf)
226 return false;
227
228 OSMO_ASSERT(ul_tbf->ms());
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100229 OSMO_ASSERT(ms_current_trx(ul_tbf->ms()));
Max2ecf0fd2017-11-21 18:13:31 +0100230
231 dump_assignment(ul_tbf, "UL", verbose);
232
233 /* assume final ack has not been sent */
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100234 dl_tbf = tbf_alloc_dl_tbf(bts, ms, ms_current_trx(ms)->trx_no, false);
Max2ecf0fd2017-11-21 18:13:31 +0100235 if (!dl_tbf)
236 return false;
237
238 dump_assignment(dl_tbf, "DL", verbose);
239
240 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
241
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100242 check_tfi_usage(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100243
244 tbf_free(dl_tbf);
245 tbf_free(ul_tbf);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100246 talloc_free(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100247 return true;
248}
249
250static inline bool test_alloc_b_dl_ul(bool ts0, bool ts1, bool ts2, bool ts3, bool ts4, bool ts5, bool ts6, bool ts7,
251 uint8_t ms_class, bool verbose)
252{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100253 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200254 GprsMs *ms;
Max2ecf0fd2017-11-21 18:13:31 +0100255 gprs_rlcmac_ul_tbf *ul_tbf;
256 gprs_rlcmac_dl_tbf *dl_tbf;
257
258 if (verbose)
259 printf("Testing DL then UL assignment followed by update\n");
260
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100261 the_pcu->alloc_algorithm = alloc_algorithm_b;
Max2ecf0fd2017-11-21 18:13:31 +0100262
263 enable_ts_on_bts(bts, ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7);
264
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100265 ms = bts_alloc_ms(bts, ms_class, 0);
Pau Espin Pedrol1e009472021-01-11 20:40:19 +0100266 /* Avoid delaying free to avoid tons of to-be-freed ms objects queuing */
267 ms_set_timeout(ms, 0);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200268 dl_tbf = tbf_alloc_dl_tbf(bts, ms, -1, true);
Max2ecf0fd2017-11-21 18:13:31 +0100269 if (!dl_tbf)
270 return false;
271
272 dl_tbf->update_ms(0x23, GPRS_RLCMAC_DL_TBF);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200273 OSMO_ASSERT(dl_tbf->ms() == ms);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100274 OSMO_ASSERT(ms_current_trx(dl_tbf->ms()));
Max2ecf0fd2017-11-21 18:13:31 +0100275
276 dump_assignment(dl_tbf, "DL", verbose);
277
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100278 ul_tbf = tbf_alloc_ul_tbf(bts, ms, ms_current_trx(ms)->trx_no, false);
Max2ecf0fd2017-11-21 18:13:31 +0100279 if (!ul_tbf)
280 return false;
281
282 ul_tbf->update_ms(0x23, GPRS_RLCMAC_UL_TBF);
283 ul_tbf->m_contention_resolution_done = 1;
284
285 dump_assignment(ul_tbf, "UL", verbose);
286
287 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
288
289 /* now update the dl_tbf */
290 dl_tbf->update();
291 dump_assignment(dl_tbf, "DL", verbose);
292 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
293
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100294 check_tfi_usage(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100295
296 tbf_free(dl_tbf);
297 tbf_free(ul_tbf);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100298 talloc_free(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100299 return true;
300}
301
302static inline bool test_alloc_b_jolly(uint8_t ms_class)
303{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100304 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200305 GprsMs *ms;
Max2ecf0fd2017-11-21 18:13:31 +0100306 int tfi;
307 uint8_t trx_no;
308 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
309
310 printf("Testing jolly example\n");
311
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100312 the_pcu->alloc_algorithm = alloc_algorithm_b;
Max2ecf0fd2017-11-21 18:13:31 +0100313
314 enable_ts_on_bts(bts, false, true, true, true, true, false, false, false);
315
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100316 tfi = bts_tfi_find_free(bts, GPRS_RLCMAC_UL_TBF, &trx_no, -1);
Max2ecf0fd2017-11-21 18:13:31 +0100317 OSMO_ASSERT(tfi >= 0);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100318 ms = bts_alloc_ms(bts, ms_class, 0);
Pau Espin Pedrol1e009472021-01-11 20:40:19 +0100319 /* Avoid delaying free to avoid tons of to-be-freed ms objects queuing */
320 ms_set_timeout(ms, 0);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200321 ul_tbf = tbf_alloc_ul_tbf(bts, ms, -1, false);
Max2ecf0fd2017-11-21 18:13:31 +0100322 if (!ul_tbf)
323 return false;
324
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200325 OSMO_ASSERT(ul_tbf->ms() == ms);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100326 OSMO_ASSERT(ms_current_trx(ul_tbf->ms()));
327 trx_no = ms_current_trx(ms)->trx_no;
Max2ecf0fd2017-11-21 18:13:31 +0100328 dump_assignment(ul_tbf, "UL", true);
329
330 /* assume final ack has not been sent */
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200331 dl_tbf = tbf_alloc_dl_tbf(bts, ms, trx_no, false);
Max2ecf0fd2017-11-21 18:13:31 +0100332 if (!dl_tbf)
333 return false;
334
335 dump_assignment(dl_tbf, "DL", true);
336
337 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
338
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100339 check_tfi_usage(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100340
341 tbf_free(dl_tbf);
342 tbf_free(ul_tbf);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100343 talloc_free(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100344 return true;
345}
346
347static void test_alloc_b_for_ms(uint8_t ms_class)
348{
349 bool rc;
350
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100351 printf("Going to test multislot assignment MS_CLASS=%d\n", ms_class);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100352 /*
353 * PDCH is on TS 6,7,8 and we start with a UL allocation and
354 * then follow two DL allocations (once single, once normal).
355 *
356 * Uplink assigned and still available..
357 */
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100358
Max2ecf0fd2017-11-21 18:13:31 +0100359 rc = test_alloc_b_ul_dl(false, false, false, false, false, true, true, true, ms_class, true);
360 if (!rc)
361 return;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100362
363 /**
364 * Test with the other order.. first DL and then UL
365 */
Max2ecf0fd2017-11-21 18:13:31 +0100366 rc = test_alloc_b_dl_ul(false, false, false, false, false, true, true, true, ms_class, true);
367 if (!rc)
368 return;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100369
370 /* Andreas osmocom-pcu example */
Max2ecf0fd2017-11-21 18:13:31 +0100371 test_alloc_b_jolly(ms_class);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100372}
373
Max2ecf0fd2017-11-21 18:13:31 +0100374static 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 +0100375{
Max2ecf0fd2017-11-21 18:13:31 +0100376 bool rc;
377
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100378 /* we can test the allocation failures differently */
379 if (!ts0 && !ts1 && !ts2 && !ts3 && !ts4 && !ts5 && !ts6 && !ts7)
380 return;
381
382 printf("Mass test: TS0(%c%c%c%c%c%c%c%c)TS7 MS_Class=%d\n",
383 ts0 ? 'O' : 'x',
384 ts1 ? 'O' : 'x',
385 ts2 ? 'O' : 'x',
386 ts3 ? 'O' : 'x',
387 ts4 ? 'O' : 'x',
388 ts5 ? 'O' : 'x',
389 ts6 ? 'O' : 'x',
390 ts7 ? 'O' : 'x', ms_class);
391 fflush(stdout);
392
Max2ecf0fd2017-11-21 18:13:31 +0100393 rc = test_alloc_b_ul_dl(ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7, ms_class, false);
394 if (!rc)
395 return;
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100396
397 /**
398 * Test with the other order.. first DL and then UL
399 */
Max2ecf0fd2017-11-21 18:13:31 +0100400 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 +0100401}
402
403static void test_all_alloc_b()
404{
405 /* it is a bit crazy... */
406 for (uint8_t ts0 = 0; ts0 < 2; ++ts0)
407 for (uint8_t ts1 = 0; ts1 < 2; ++ts1)
408 for (uint8_t ts2 = 0; ts2 < 2; ++ts2)
409 for (uint8_t ts3 = 0; ts3 < 2; ++ts3)
410 for (uint8_t ts4 = 0; ts4 < 2; ++ts4)
411 for (uint8_t ts5 = 0; ts5 < 2; ++ts5)
412 for (uint8_t ts6 = 0; ts6 < 2; ++ts6)
413 for (uint8_t ts7 = 0; ts7 < 2; ++ts7)
Max9f460712018-01-23 20:57:08 +0100414 for (int ms_class = 0; ms_class < mslot_class_max(); ++ms_class)
Max2ecf0fd2017-11-21 18:13:31 +0100415 test_alloc_mass(ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7, ms_class);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100416}
417
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100418static void test_alloc_b()
419{
Max9f460712018-01-23 20:57:08 +0100420 for (int i = 0; i < mslot_class_max(); ++i)
Max2ecf0fd2017-11-21 18:13:31 +0100421 test_alloc_b_for_ms(i);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100422
423 test_all_alloc_b();
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100424}
425
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200426static char get_dir_char(uint8_t mask, uint8_t tx, uint8_t rx, uint8_t busy)
Jacob Erlbecke5655642015-06-29 12:19:52 +0200427{
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200428 int offs = busy ? 32 : 0;
429 return (mask & tx & rx) ? 'C' + offs :
430 (mask & tx) ? 'U' + offs :
431 (mask & rx) ? 'D' + offs :
Jacob Erlbecke5655642015-06-29 12:19:52 +0200432 '.';
433}
434
435enum test_mode {
436 TEST_MODE_UL_ONLY,
437 TEST_MODE_DL_ONLY,
438 TEST_MODE_UL_AND_DL,
439 TEST_MODE_DL_AND_UL,
440 TEST_MODE_DL_AFTER_UL,
441 TEST_MODE_UL_AFTER_DL,
442};
443
Maxc59ef122017-11-27 13:21:41 +0100444static inline char *test_mode_descr(enum test_mode t)
445{
446 switch (t) {
447 case TEST_MODE_UL_ONLY: return (char*)"UL only";
448 case TEST_MODE_DL_ONLY: return (char*)"DL only";
449 case TEST_MODE_UL_AND_DL: return (char*)"UL and DL";
450 case TEST_MODE_DL_AND_UL: return (char*)"DL and UL";
451 case TEST_MODE_DL_AFTER_UL: return (char*)"DL after UL";
452 case TEST_MODE_UL_AFTER_DL: return (char*)"UL after DL";
453 default: return NULL;
454 }
455}
456
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100457static GprsMs *alloc_tbfs(struct gprs_rlcmac_bts *bts, struct GprsMs *old_ms, enum test_mode mode)
Jacob Erlbecke5655642015-06-29 12:19:52 +0200458{
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100459 struct GprsMs *ms, *new_ms;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200460 uint8_t trx_no = -1;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200461
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100462 OSMO_ASSERT(old_ms != NULL);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200463
Jacob Erlbecke5655642015-06-29 12:19:52 +0200464 gprs_rlcmac_tbf *tbf = NULL;
465
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100466 if (ms_current_trx(old_ms))
467 trx_no = ms_current_trx(old_ms)->trx_no;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200468
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100469 ms_ref(old_ms);
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200470
Jacob Erlbecke5655642015-06-29 12:19:52 +0200471 /* Allocate what is needed first */
472 switch (mode) {
473 case TEST_MODE_UL_ONLY:
474 case TEST_MODE_DL_AFTER_UL:
475 case TEST_MODE_UL_AND_DL:
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100476 if (ms_ul_tbf(old_ms))
477 tbf_free(ms_ul_tbf(old_ms));
478 tbf = tbf_alloc_ul_tbf(bts, old_ms, trx_no, false);
479 if (tbf == NULL) {
Pau Espin Pedrol0dcbc072021-11-10 19:09:10 +0100480 OSMO_ASSERT(trx_no != -1 || bts_all_pdch_allocated(bts));
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100481 ms_unref(old_ms);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200482 return NULL;
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100483 }
Jacob Erlbecke5655642015-06-29 12:19:52 +0200484 break;
485 case TEST_MODE_DL_ONLY:
486 case TEST_MODE_UL_AFTER_DL:
487 case TEST_MODE_DL_AND_UL:
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100488 if (ms_dl_tbf(old_ms))
489 tbf_free(ms_dl_tbf(old_ms));
490 tbf = tbf_alloc_dl_tbf(bts, old_ms, trx_no, false);
491 if (tbf == NULL) {
Pau Espin Pedrol0dcbc072021-11-10 19:09:10 +0100492 OSMO_ASSERT(trx_no != -1 || bts_all_pdch_allocated(bts));
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100493 ms_unref(old_ms);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200494 return NULL;
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100495 }
Jacob Erlbecke5655642015-06-29 12:19:52 +0200496 }
497
498 OSMO_ASSERT(tbf);
499 OSMO_ASSERT(tbf->ms());
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100500 OSMO_ASSERT(old_ms == tbf->ms());
Jacob Erlbecke5655642015-06-29 12:19:52 +0200501 ms = tbf->ms();
502
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100503 ms_ref(ms);
504 new_ms = ms;
Jacob Erlbeck14376a72015-07-07 11:31:28 +0200505 /* Continue with what is needed next */
506 switch (mode) {
507 case TEST_MODE_UL_ONLY:
508 case TEST_MODE_DL_ONLY:
509 /* We are done */
510 break;
511
512 case TEST_MODE_DL_AFTER_UL:
513 case TEST_MODE_UL_AND_DL:
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100514 new_ms = alloc_tbfs(bts, ms, TEST_MODE_DL_ONLY);
Jacob Erlbeck14376a72015-07-07 11:31:28 +0200515 break;
516
517 case TEST_MODE_UL_AFTER_DL:
518 case TEST_MODE_DL_AND_UL:
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100519 new_ms = alloc_tbfs(bts, ms, TEST_MODE_UL_ONLY);
Jacob Erlbeck14376a72015-07-07 11:31:28 +0200520 break;
521 }
522
Jacob Erlbecke5655642015-06-29 12:19:52 +0200523 /* Optionally delete the TBF */
524 switch (mode) {
525 case TEST_MODE_DL_AFTER_UL:
526 case TEST_MODE_UL_AFTER_DL:
527 tbf_free(tbf);
Jacob Erlbeck0f352a62015-07-16 18:23:33 +0200528 tbf = NULL;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200529 break;
530
531 default:
532 break;
533 }
534
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100535 if (!new_ms && tbf)
Jacob Erlbeck0f352a62015-07-16 18:23:33 +0200536 tbf_free(tbf);
537
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100538 ms_unref(old_ms);
539 ms_unref(ms);
540 return new_ms;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200541}
542
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100543static unsigned alloc_many_tbfs(struct gprs_rlcmac_bts *bts, unsigned min_class,
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200544 unsigned max_class, enum test_mode mode)
Jacob Erlbecke5655642015-06-29 12:19:52 +0200545{
Jacob Erlbecke5655642015-06-29 12:19:52 +0200546 unsigned counter;
547 unsigned ms_class = min_class;
548
Jacob Erlbecke5655642015-06-29 12:19:52 +0200549 for (counter = 0; 1; counter += 1) {
550 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
551 uint8_t ul_slots = 0;
552 uint8_t dl_slots = 0;
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200553 uint8_t busy_slots = 0;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200554 unsigned i;
555 int tfi = -1;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200556 int tfi2;
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200557 uint8_t trx_no2;
558 struct gprs_rlcmac_trx *trx;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200559 GprsMs *ms;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200560 enum gprs_rlcmac_tbf_direction dir;
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200561 uint32_t tlli = counter + 0xc0000000;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200562
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100563 ms = bts_ms_by_tlli(bts, tlli, GSM_RESERVED_TMSI);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200564 if (!ms)
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100565 ms = bts_alloc_ms(bts, 0, 0);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100566 ms_set_ms_class(ms, ms_class);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100567 ms = alloc_tbfs(bts, ms, mode);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200568 if (!ms)
569 break;
570
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100571 ms_set_tlli(ms, tlli);
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200572
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100573 ul_tbf = ms_ul_tbf(ms);
574 dl_tbf = ms_dl_tbf(ms);
575 trx = ms_current_trx(ms);
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200576
577 OSMO_ASSERT(ul_tbf || dl_tbf);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200578
579 if (ul_tbf) {
580 ul_slots = 1 << ul_tbf->first_common_ts;
581 tfi = ul_tbf->tfi();
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200582 dir = GPRS_RLCMAC_UL_TBF;
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200583 } else {
Jacob Erlbecke5655642015-06-29 12:19:52 +0200584 ul_slots = 1 << dl_tbf->first_common_ts;
585 tfi = dl_tbf->tfi();
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200586 dir = GPRS_RLCMAC_DL_TBF;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200587 }
588
589 for (i = 0; dl_tbf && i < ARRAY_SIZE(dl_tbf->pdch); i += 1)
590 if (dl_tbf->pdch[i])
591 dl_slots |= 1 << i;
592
Max5b0df1f2017-09-11 10:38:59 +0200593 for (i = 0; ul_tbf && i < ARRAY_SIZE(ul_tbf->pdch); i += 1)
594 if (ul_tbf->pdch[i])
595 ul_slots |= 1 << i;
596
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200597 for (i = 0; trx && i < ARRAY_SIZE(trx->pdch); i += 1) {
598 struct gprs_rlcmac_pdch *pdch = &trx->pdch[i];
599
600 if (ul_tbf && dl_tbf)
601 continue;
602
603 if (ul_tbf &&
Maxd000d802017-09-20 17:55:28 +0200604 pdch->assigned_tfi(GPRS_RLCMAC_DL_TBF) != NO_FREE_TFI)
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200605 continue;
606
607 if (dl_tbf &&
Maxd000d802017-09-20 17:55:28 +0200608 pdch->assigned_tfi(GPRS_RLCMAC_UL_TBF) != NO_FREE_TFI)
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200609 continue;
610
611 busy_slots |= 1 << i;
612 }
613
Maxd000d802017-09-20 17:55:28 +0200614 printf(" TBF[%d] class %d reserves " OSMO_BIT_SPEC "\n",
Jacob Erlbecke5655642015-06-29 12:19:52 +0200615 tfi, ms_class,
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200616 get_dir_char(0x01, ul_slots, dl_slots, busy_slots),
617 get_dir_char(0x02, ul_slots, dl_slots, busy_slots),
618 get_dir_char(0x04, ul_slots, dl_slots, busy_slots),
619 get_dir_char(0x08, ul_slots, dl_slots, busy_slots),
620 get_dir_char(0x10, ul_slots, dl_slots, busy_slots),
621 get_dir_char(0x20, ul_slots, dl_slots, busy_slots),
622 get_dir_char(0x40, ul_slots, dl_slots, busy_slots),
623 get_dir_char(0x80, ul_slots, dl_slots, busy_slots));
Jacob Erlbecke5655642015-06-29 12:19:52 +0200624
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200625 if (tfi >= 0) {
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100626 OSMO_ASSERT(ms_current_trx(ms));
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100627 tfi2 = bts_tfi_find_free(bts, dir, &trx_no2,
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100628 ms_current_trx(ms)->trx_no);
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200629 OSMO_ASSERT(tfi != tfi2);
Jacob Erlbeck7b3675b2015-07-16 18:28:22 +0200630 OSMO_ASSERT(tfi2 < 0 ||
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100631 trx_no2 == ms_current_trx(ms)->trx_no);
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200632 }
633
Jacob Erlbecke5655642015-06-29 12:19:52 +0200634 ms_class += 1;
635 if (ms_class > max_class)
636 ms_class = min_class;
637 }
638
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200639 return counter;
640}
641
Pau Espin Pedrolc85e0932021-02-25 18:08:10 +0100642static void test_successive_allocation(alloc_algorithm_func_t algo, unsigned min_class,
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200643 unsigned max_class, enum test_mode mode,
644 unsigned expect_num, const char *text)
645{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100646 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200647 struct gprs_rlcmac_trx *trx;
648 unsigned counter;
649
Maxc59ef122017-11-27 13:21:41 +0100650 printf("Going to test assignment with many TBF, algorithm %s class %u..%u (%s)\n",
651 text, min_class, max_class, test_mode_descr(mode));
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200652
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100653 the_pcu->alloc_algorithm = algo;
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200654
655 trx = &bts->trx[0];
656 trx->pdch[3].enable();
657 trx->pdch[4].enable();
658 trx->pdch[5].enable();
659 trx->pdch[6].enable();
660 trx->pdch[7].enable();
661
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100662 counter = alloc_many_tbfs(bts, min_class, max_class, mode);
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200663
Maxc59ef122017-11-27 13:21:41 +0100664 printf(" Successfully allocated %u UL TBFs, algorithm %s class %u..%u (%s)\n",
665 counter, text, min_class, max_class, test_mode_descr(mode));
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200666 if (counter != expect_num)
Maxc59ef122017-11-27 13:21:41 +0100667 fprintf(stderr, " Expected %u TBFs (got %u), algorithm %s class %u..%u (%s)\n",
668 expect_num, counter, text, min_class, max_class, test_mode_descr(mode));
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200669
Jacob Erlbeckec478752015-06-19 16:35:38 +0200670 OSMO_ASSERT(counter == expect_num);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200671
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100672 check_tfi_usage(bts);
673 talloc_free(bts);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200674}
675
Pau Espin Pedrolc85e0932021-02-25 18:08:10 +0100676static void test_many_connections(alloc_algorithm_func_t algo, unsigned expect_num,
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200677 const char *text)
678{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100679 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200680 struct gprs_rlcmac_trx *trx;
681 int counter1, counter2 = -1;
682 unsigned i;
683 enum test_mode mode_seq[] = {
684 TEST_MODE_DL_AFTER_UL,
685 TEST_MODE_UL_ONLY,
686 TEST_MODE_DL_AFTER_UL,
687 TEST_MODE_DL_ONLY,
688 };
689
Maxc59ef122017-11-27 13:21:41 +0100690 printf("Going to test assignment with many connections, algorithm %s\n", text);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200691
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100692 the_pcu->alloc_algorithm = algo;
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200693
694 trx = &bts->trx[0];
695 trx->pdch[3].enable();
696 trx->pdch[4].enable();
697 trx->pdch[5].enable();
698 trx->pdch[6].enable();
699 trx->pdch[7].enable();
700
701 for (i = 0; i < ARRAY_SIZE(mode_seq); i += 1) {
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100702 counter1 = alloc_many_tbfs(bts, 1, mslot_class_max(), mode_seq[i]);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200703 fprintf(stderr, " Allocated %d TBFs (previously %d)\n",
704 counter1, counter2);
705
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100706 check_tfi_usage(bts);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200707
708 /* This will stop earlier due to USF shortage */
709 if (mode_seq[i] == TEST_MODE_UL_ONLY)
710 continue;
711
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200712 if (counter2 >= 0) {
713 if (counter1 < counter2)
714 fprintf(stderr, " Expected %d >= %d in %s\n",
715 counter1, counter2, text);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200716 OSMO_ASSERT(counter1 >= counter2);
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200717 }
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200718
719 counter2 = counter1;
720 }
721
722 printf(" Successfully allocated %d TBFs\n", counter1);
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200723 if (counter1 != (int)expect_num)
Maxc59ef122017-11-27 13:21:41 +0100724 fprintf(stderr, " Expected %d TBFs (got %d) for algorithm %s\n", expect_num, counter1, text);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200725
726 OSMO_ASSERT(expect_num == (unsigned)counter1);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100727 talloc_free(bts);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200728}
729
Maxc59ef122017-11-27 13:21:41 +0100730static 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 +0200731{
Maxc59ef122017-11-27 13:21:41 +0100732 test_successive_allocation(alloc_algorithm_a, 1, 1, mode, exp_A, "A");
733 test_successive_allocation(alloc_algorithm_b, 10, 10, mode, exp_B, "B");
734 test_successive_allocation(alloc_algorithm_dynamic, 10, 10, mode, exp_dyn, "dynamic");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200735}
736
Maxc59ef122017-11-27 13:21:41 +0100737static void test_successive_allocations()
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200738{
Maxc59ef122017-11-27 13:21:41 +0100739 test_successive_allocation(alloc_algorithm_a, 1, 1, TEST_MODE_UL_AND_DL, 35, "A");
Pau Espin Pedroled2afa32021-02-22 17:20:15 +0100740 test_successive_allocation(alloc_algorithm_b, 10, 10, TEST_MODE_UL_AND_DL, 15, "B");
741 test_successive_allocation(alloc_algorithm_b, 12, 12, TEST_MODE_UL_AND_DL, 15, "B");
Maxc59ef122017-11-27 13:21:41 +0100742
Pau Espin Pedroled2afa32021-02-22 17:20:15 +0100743 test_successive_allocation(alloc_algorithm_b, 1, 12, TEST_MODE_UL_AND_DL, 23, "B");
744 test_successive_allocation(alloc_algorithm_b, 1, mslot_class_max(), TEST_MODE_UL_AND_DL, 17, "B");
745 test_successive_allocation(alloc_algorithm_dynamic, 1, mslot_class_max(), TEST_MODE_UL_AND_DL, 17, "dynamic");
Maxc59ef122017-11-27 13:21:41 +0100746
Pau Espin Pedroled2afa32021-02-22 17:20:15 +0100747 test_a_b_dyn(TEST_MODE_DL_AND_UL, 35, 15, 15);
748 test_a_b_dyn(TEST_MODE_DL_AFTER_UL, 160, 32, 101);
749 test_a_b_dyn(TEST_MODE_UL_AFTER_DL, 35, 15, 15);
750 test_a_b_dyn(TEST_MODE_UL_ONLY, 35, 15, 21);
Maxc59ef122017-11-27 13:21:41 +0100751 test_a_b_dyn(TEST_MODE_DL_ONLY, 160, 32, 101);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200752}
753
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530754static void test_2_consecutive_dl_tbfs()
755{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100756 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200757 GprsMs *ms;
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530758 struct gprs_rlcmac_trx *trx;
759 uint8_t ms_class = 11;
760 uint8_t egprs_ms_class = 11;
761 gprs_rlcmac_tbf *dl_tbf1, *dl_tbf2;
762 uint8_t numTs1 = 0, numTs2 = 0;
763
764 printf("Testing DL TS allocation for Multi UEs\n");
765
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100766 the_pcu->alloc_algorithm = alloc_algorithm_b;
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530767
768 trx = &bts->trx[0];
769 trx->pdch[4].enable();
770 trx->pdch[5].enable();
771 trx->pdch[6].enable();
772 trx->pdch[7].enable();
773
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100774 ms = bts_alloc_ms(bts, ms_class, egprs_ms_class);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200775 dl_tbf1 = tbf_alloc_dl_tbf(bts, ms, 0, false);
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530776 OSMO_ASSERT(dl_tbf1);
777
778 for (int i = 0; i < 8; i++) {
779 if (dl_tbf1->pdch[i])
780 numTs1++;
781 }
782 OSMO_ASSERT(numTs1 == 4);
783 printf("TBF1: numTs(%d)\n", numTs1);
784
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100785 ms = bts_alloc_ms(bts, ms_class, egprs_ms_class);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200786 dl_tbf2 = tbf_alloc_dl_tbf(bts, ms, 0, false);
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530787 OSMO_ASSERT(dl_tbf2);
788
789 for (int i = 0; i < 8; i++) {
790 if (dl_tbf2->pdch[i])
791 numTs2++;
792 }
793
794 /*
795 * TODO: currently 2nd DL TBF gets 3 TS
796 * This behaviour will be fixed in subsequent patch
797 */
798 printf("TBF2: numTs(%d)\n", numTs2);
799 OSMO_ASSERT(numTs2 == 3);
800
801 tbf_free(dl_tbf1);
802 tbf_free(dl_tbf2);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100803 talloc_free(bts);
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530804}
805
Oliver Smith3f794702021-08-23 14:19:21 +0200806static void test_bts_pch_timer(void)
807{
808 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100809 struct osmo_mobile_identity mi_imsi1, mi_imsi2;
Pau Espin Pedrol19b33922021-11-08 17:49:52 +0100810 struct osmo_mobile_identity mi_tmsi1;
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100811 mi_imsi1.type = mi_imsi2.type = GSM_MI_TYPE_IMSI;
Pau Espin Pedrol19b33922021-11-08 17:49:52 +0100812 mi_tmsi1.type = GSM_MI_TYPE_TMSI;
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100813 OSMO_STRLCPY_ARRAY(mi_imsi1.imsi, "1234");
814 OSMO_STRLCPY_ARRAY(mi_imsi2.imsi, "5678");
Pau Espin Pedrol19b33922021-11-08 17:49:52 +0100815 mi_tmsi1.tmsi = 987654321;
Oliver Smith3f794702021-08-23 14:19:21 +0200816
817 fprintf(stderr, "Testing bts_pch_timer dealloc on bts dealloc\n");
818 log_set_category_filter(osmo_stderr_target, DPCU, 1, LOGL_DEBUG);
819
820 fprintf(stderr, "Starting PCH timer for 2 IMSI\n");
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100821 bts_pch_timer_start(bts, &mi_imsi1, mi_imsi1.imsi);
822 bts_pch_timer_start(bts, &mi_imsi2, mi_imsi2.imsi);
Pau Espin Pedrol19b33922021-11-08 17:49:52 +0100823 fprintf(stderr, "Starting PCH timer for 1 TMSI\n");
824 bts_pch_timer_start(bts, &mi_tmsi1, "6666");
Oliver Smith3f794702021-08-23 14:19:21 +0200825
826 fprintf(stderr, "Deallocating BTS, expecting the PCH timer to be stopped and deallocated\n");
827 talloc_free(bts);
828}
829
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200830int main(int argc, char **argv)
831{
832 tall_pcu_ctx = talloc_named_const(NULL, 1, "moiji-mobile AllocTest context");
833 if (!tall_pcu_ctx)
834 abort();
835
Neels Hofmeyr78ce5912017-02-08 17:07:31 +0100836 msgb_talloc_ctx_init(tall_pcu_ctx, 0);
Neels Hofmeyr42f2d612018-04-01 16:54:40 +0200837 osmo_init_logging2(tall_pcu_ctx, &gprs_log_info);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200838 log_set_use_color(osmo_stderr_target, 0);
Pau Espin Pedrol00f52cc2021-02-19 14:01:52 +0100839 log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE);
Pau Espin Pedrolb18d2a52021-02-19 14:00:48 +0100840 log_set_print_category(osmo_stderr_target, 0);
841 log_set_print_category_hex(osmo_stderr_target, 0);
Philipp Maierde0e5582020-03-25 12:23:52 +0100842 log_set_category_filter(osmo_stderr_target, DTBF, 1, LOGL_INFO);
Jacob Erlbeck9ec49e22015-06-29 13:00:20 +0200843 if (getenv("LOGL_DEBUG"))
844 log_set_log_level(osmo_stderr_target, LOGL_DEBUG);
Pau Espin Pedroldc2aaac2021-05-14 12:50:46 +0200845 osmo_fsm_log_addr(false);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200846
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100847 the_pcu = gprs_pcu_alloc(tall_pcu_ctx);
848
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200849 test_alloc_a();
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100850 test_alloc_b();
Maxc59ef122017-11-27 13:21:41 +0100851 test_successive_allocations();
852 test_many_connections(alloc_algorithm_a, 160, "A");
Max01bd0cc2018-01-23 20:58:49 +0100853 test_many_connections(alloc_algorithm_b, 32, "B");
Maxc59ef122017-11-27 13:21:41 +0100854 test_many_connections(alloc_algorithm_dynamic, 160, "dynamic");
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530855 test_2_consecutive_dl_tbfs();
Oliver Smith3f794702021-08-23 14:19:21 +0200856 test_bts_pch_timer();
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100857
858 talloc_free(the_pcu);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200859 return EXIT_SUCCESS;
860}
861
862/*
863 * stubs that should not be reached
864 */
865extern "C" {
866void l1if_pdch_req() { abort(); }
867void l1if_connect_pdch() { abort(); }
868void l1if_close_pdch() { abort(); }
869void l1if_open_pdch() { abort(); }
870}