blob: 6f3de43dd0a0efc224163705b829ec527173cf6d [file] [log] [blame]
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +02001/* AllocTest.cpp
2 *
3 * Copyright (C) 2013 by Holger Hans Peter Freyther
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20#include "gprs_rlcmac.h"
21#include "gprs_debug.h"
22#include "tbf.h"
Pau Espin Pedrol9d1cdb12019-09-25 17:47:02 +020023#include "tbf_ul.h"
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010024#include "tbf_dl.h"
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +020025#include "bts.h"
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010026#include "gprs_ms.h"
Oliver Smith3f794702021-08-23 14:19:21 +020027#include "bts_pch_timer.h"
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020028
29#include <string.h>
30#include <stdio.h>
31
32extern "C" {
Max9f460712018-01-23 20:57:08 +010033#include "mslot_class.h"
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020034#include <osmocom/core/application.h>
35#include <osmocom/core/msgb.h>
36#include <osmocom/core/talloc.h>
37#include <osmocom/core/utils.h>
Pau Espin Pedroldc2aaac2021-05-14 12:50:46 +020038#include <osmocom/core/fsm.h>
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020039}
40
41/* globals used by the code */
42void *tall_pcu_ctx;
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020043int16_t spoof_mnc = 0, spoof_mcc = 0;
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +010044bool spoof_mnc_3_digits = false;
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020045
Daniel Willmann48aa0b02014-07-16 18:54:10 +020046static gprs_rlcmac_tbf *tbf_alloc(struct gprs_rlcmac_bts *bts,
Jacob Erlbecke2e004e2015-06-18 17:16:26 +020047 GprsMs *ms, gprs_rlcmac_tbf_direction dir,
Pau Espin Pedrol322456e2020-05-08 18:15:59 +020048 uint8_t use_trx, bool single_slot)
Daniel Willmann48aa0b02014-07-16 18:54:10 +020049{
Pau Espin Pedrol322456e2020-05-08 18:15:59 +020050 OSMO_ASSERT(ms != NULL);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +020051
Daniel Willmann48aa0b02014-07-16 18:54:10 +020052 if (dir == GPRS_RLCMAC_UL_TBF)
Pau Espin Pedrol17402a52020-05-08 17:44:33 +020053 return tbf_alloc_ul_tbf(bts, ms, use_trx, single_slot);
Daniel Willmann48aa0b02014-07-16 18:54:10 +020054 else
Pau Espin Pedrol322456e2020-05-08 18:15:59 +020055 return tbf_alloc_dl_tbf(bts, ms, use_trx, single_slot);
Daniel Willmann48aa0b02014-07-16 18:54:10 +020056}
57
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010058static void check_tfi_usage(struct gprs_rlcmac_bts *bts)
Jacob Erlbeck61205a72015-07-09 11:35:50 +020059{
60 int pdch_no;
Jacob Erlbeck61205a72015-07-09 11:35:50 +020061
62 struct gprs_rlcmac_tbf *tfi_usage[8][8][2][32] = {{{{NULL}}}};
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010063 struct llist_head *tbf_lists[2] = {
Pau Espin Pedrol1a1557a2021-05-13 18:39:36 +020064 &bts->trx[0].ul_tbfs,
65 &bts->trx[0].dl_tbfs
Jacob Erlbeck61205a72015-07-09 11:35:50 +020066 };
67
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010068 struct llist_item *pos;
Jacob Erlbeck61205a72015-07-09 11:35:50 +020069 gprs_rlcmac_tbf *tbf;
Jacob Erlbeck61205a72015-07-09 11:35:50 +020070 unsigned list_idx;
71 struct gprs_rlcmac_tbf **tbf_var;
72
73 for (list_idx = 0; list_idx < ARRAY_SIZE(tbf_lists); list_idx += 1)
74 {
75
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010076 llist_for_each_entry(pos, tbf_lists[list_idx], list) {
77 tbf = (struct gprs_rlcmac_tbf *)pos->entry;
Jacob Erlbeck61205a72015-07-09 11:35:50 +020078 for (pdch_no = 0; pdch_no < 8; pdch_no += 1) {
79 struct gprs_rlcmac_pdch *pdch = tbf->pdch[pdch_no];
80 if (pdch == NULL)
81 continue;
82
83 tbf_var = &tfi_usage
84 [tbf->trx->trx_no]
85 [pdch_no]
86 [tbf->direction]
87 [tbf->tfi()];
88
89 OSMO_ASSERT(*tbf_var == NULL);
90 if (tbf->direction == GPRS_RLCMAC_DL_TBF) {
91 OSMO_ASSERT(pdch->dl_tbf_by_tfi(
92 tbf->tfi()) == tbf);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010093 OSMO_ASSERT(bts_dl_tbf_by_tfi(bts,
Jacob Erlbeck61205a72015-07-09 11:35:50 +020094 tbf->tfi(),
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +020095 tbf->trx->trx_no,
96 pdch_no) == tbf);
Jacob Erlbeck61205a72015-07-09 11:35:50 +020097 } else {
98 OSMO_ASSERT(pdch->ul_tbf_by_tfi(
99 tbf->tfi()) == tbf);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100100 OSMO_ASSERT(bts_ul_tbf_by_tfi(bts,
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200101 tbf->tfi(),
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200102 tbf->trx->trx_no,
103 pdch_no) == tbf);
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200104 }
105 *tbf_var = tbf;
Jacob Erlbeck47a57f62015-07-08 12:53:16 +0200106 OSMO_ASSERT(pdch->assigned_tfi(tbf->direction) &
107 (1 << tbf->tfi()));
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200108 }
109 }
110 }
111}
112
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200113static void test_alloc_a(gprs_rlcmac_tbf_direction dir,
114 uint8_t slots, const int count)
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200115{
116 int tfi;
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200117 int i;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200118 uint8_t used_trx, tmp_trx;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100119 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200120 GprsMs *ms;
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200121 struct gprs_rlcmac_tbf *tbfs[32*8+1] = { 0, };
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200122
123 printf("Testing alloc_a direction(%d)\n", dir);
124
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100125 the_pcu->alloc_algorithm = alloc_algorithm_a;
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200126
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200127 struct gprs_rlcmac_trx *trx = &bts->trx[0];
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200128 for (i = 0; i < 8; i += 1)
129 if (slots & (1 << i))
130 trx->pdch[i].enable();
131
132 OSMO_ASSERT(count >= 0 && count <= (int)ARRAY_SIZE(tbfs));
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200133
134 /**
135 * Currently alloc_a will only allocate from the first
136 * PDCH and all possible usf's. We run out of usf's before
137 * we are out of tfi's. Observe this and make sure that at
138 * least this part is working okay.
139 */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200140 for (i = 0; i < (int)ARRAY_SIZE(tbfs); ++i) {
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100141 ms = bts_alloc_ms(bts, 0, 0);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200142 tbfs[i] = tbf_alloc(bts, ms, dir, -1, 0);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200143 if (tbfs[i] == NULL)
144 break;
145
146 used_trx = tbfs[i]->trx->trx_no;
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100147 tfi = bts_tfi_find_free(bts, dir, &tmp_trx, used_trx);
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200148 OSMO_ASSERT(tbfs[i]->tfi() != tfi);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200149 }
150
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100151 check_tfi_usage(bts);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200152
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200153 OSMO_ASSERT(i == count);
154
Pau Espin Pedrol0dcbc072021-11-10 19:09:10 +0100155 OSMO_ASSERT(bts_all_pdch_allocated(bts));
156
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200157 for (i = 0; i < count; ++i)
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200158 if (tbfs[i])
159 tbf_free(tbfs[i]);
160
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100161 ms = bts_alloc_ms(bts, 0, 0);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200162 tbfs[0] = tbf_alloc(bts, ms, dir, -1, 0);
Holger Hans Peter Freytherf3f1bde2016-02-22 15:14:01 +0100163 OSMO_ASSERT(tbfs[0]);
164 tbf_free(tbfs[0]);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100165 talloc_free(bts);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200166}
167
168static void test_alloc_a()
169{
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200170 /* slots 2 - 3 */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200171 test_alloc_a(GPRS_RLCMAC_DL_TBF, 0x0c, 32*2);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200172 test_alloc_a(GPRS_RLCMAC_UL_TBF, 0x0c, 14);
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200173
174 /* slots 1 - 5 */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200175 test_alloc_a(GPRS_RLCMAC_DL_TBF, 0x1e, 32*4);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200176 test_alloc_a(GPRS_RLCMAC_UL_TBF, 0x1e, 28);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200177}
178
Max2ecf0fd2017-11-21 18:13:31 +0100179static void dump_assignment(struct gprs_rlcmac_tbf *tbf, const char *dir, bool verbose)
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100180{
Max2ecf0fd2017-11-21 18:13:31 +0100181 if (!verbose)
182 return;
183
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200184 for (size_t i = 0; i < ARRAY_SIZE(tbf->pdch); ++i)
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100185 if (tbf->pdch[i])
Neels Hofmeyrd34646a2017-02-08 17:07:40 +0100186 printf("PDCH[%zu] is used for %s\n", i, dir);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100187 printf("PDCH[%d] is control_ts for %s\n", tbf->control_ts, dir);
188 printf("PDCH[%d] is first common for %s\n", tbf->first_common_ts, dir);
189}
190
Max2ecf0fd2017-11-21 18:13:31 +0100191#define ENABLE_PDCH(ts_no, enable_flag, trx) \
192 if (enable_flag) \
193 trx->pdch[ts_no].enable();
194
195static inline void enable_ts_on_bts(struct gprs_rlcmac_bts *bts,
196 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 +0100197{
Max2ecf0fd2017-11-21 18:13:31 +0100198 struct gprs_rlcmac_trx *trx = &bts->trx[0];
199
200 ENABLE_PDCH(0, ts0, trx);
201 ENABLE_PDCH(1, ts1, trx);
202 ENABLE_PDCH(2, ts2, trx);
203 ENABLE_PDCH(3, ts3, trx);
204 ENABLE_PDCH(4, ts4, trx);
205 ENABLE_PDCH(5, ts5, trx);
206 ENABLE_PDCH(6, ts6, trx);
207 ENABLE_PDCH(7, ts7, trx);
208}
209
210static inline bool test_alloc_b_ul_dl(bool ts0, bool ts1, bool ts2, bool ts3, bool ts4, bool ts5, bool ts6, bool ts7,
211 uint8_t ms_class, bool verbose)
212{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100213 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200214 GprsMs *ms;
Max2ecf0fd2017-11-21 18:13:31 +0100215 gprs_rlcmac_ul_tbf *ul_tbf;
216 gprs_rlcmac_dl_tbf *dl_tbf;
217
218 if (verbose)
219 printf("Testing UL then DL assignment.\n");
220
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100221 the_pcu->alloc_algorithm = alloc_algorithm_b;
Max2ecf0fd2017-11-21 18:13:31 +0100222
223 enable_ts_on_bts(bts, ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7);
224
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100225 ms = bts_alloc_ms(bts, ms_class, 0);
Pau Espin Pedrol1e009472021-01-11 20:40:19 +0100226 /* Avoid delaying free to avoid tons of to-be-freed ms objects queuing */
227 ms_set_timeout(ms, 0);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200228 ul_tbf = tbf_alloc_ul_tbf(bts, ms, -1, true);
Max2ecf0fd2017-11-21 18:13:31 +0100229 if (!ul_tbf)
230 return false;
231
232 OSMO_ASSERT(ul_tbf->ms());
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100233 OSMO_ASSERT(ms_current_trx(ul_tbf->ms()));
Max2ecf0fd2017-11-21 18:13:31 +0100234
235 dump_assignment(ul_tbf, "UL", verbose);
236
237 /* assume final ack has not been sent */
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100238 dl_tbf = tbf_alloc_dl_tbf(bts, ms, ms_current_trx(ms)->trx_no, false);
Max2ecf0fd2017-11-21 18:13:31 +0100239 if (!dl_tbf)
240 return false;
241
242 dump_assignment(dl_tbf, "DL", verbose);
243
244 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
245
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100246 check_tfi_usage(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100247
248 tbf_free(dl_tbf);
249 tbf_free(ul_tbf);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100250 talloc_free(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100251 return true;
252}
253
254static inline bool test_alloc_b_dl_ul(bool ts0, bool ts1, bool ts2, bool ts3, bool ts4, bool ts5, bool ts6, bool ts7,
255 uint8_t ms_class, bool verbose)
256{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100257 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200258 GprsMs *ms;
Max2ecf0fd2017-11-21 18:13:31 +0100259 gprs_rlcmac_ul_tbf *ul_tbf;
260 gprs_rlcmac_dl_tbf *dl_tbf;
261
262 if (verbose)
263 printf("Testing DL then UL assignment followed by update\n");
264
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100265 the_pcu->alloc_algorithm = alloc_algorithm_b;
Max2ecf0fd2017-11-21 18:13:31 +0100266
267 enable_ts_on_bts(bts, ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7);
268
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100269 ms = bts_alloc_ms(bts, ms_class, 0);
Pau Espin Pedrol1e009472021-01-11 20:40:19 +0100270 /* Avoid delaying free to avoid tons of to-be-freed ms objects queuing */
271 ms_set_timeout(ms, 0);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200272 dl_tbf = tbf_alloc_dl_tbf(bts, ms, -1, true);
Max2ecf0fd2017-11-21 18:13:31 +0100273 if (!dl_tbf)
274 return false;
275
276 dl_tbf->update_ms(0x23, GPRS_RLCMAC_DL_TBF);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200277 OSMO_ASSERT(dl_tbf->ms() == ms);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100278 OSMO_ASSERT(ms_current_trx(dl_tbf->ms()));
Max2ecf0fd2017-11-21 18:13:31 +0100279
280 dump_assignment(dl_tbf, "DL", verbose);
281
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100282 ul_tbf = tbf_alloc_ul_tbf(bts, ms, ms_current_trx(ms)->trx_no, false);
Max2ecf0fd2017-11-21 18:13:31 +0100283 if (!ul_tbf)
284 return false;
285
286 ul_tbf->update_ms(0x23, GPRS_RLCMAC_UL_TBF);
287 ul_tbf->m_contention_resolution_done = 1;
288
289 dump_assignment(ul_tbf, "UL", verbose);
290
291 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
292
293 /* now update the dl_tbf */
294 dl_tbf->update();
295 dump_assignment(dl_tbf, "DL", verbose);
296 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
297
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100298 check_tfi_usage(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100299
300 tbf_free(dl_tbf);
301 tbf_free(ul_tbf);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100302 talloc_free(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100303 return true;
304}
305
306static inline bool test_alloc_b_jolly(uint8_t ms_class)
307{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100308 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200309 GprsMs *ms;
Max2ecf0fd2017-11-21 18:13:31 +0100310 int tfi;
311 uint8_t trx_no;
312 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
313
314 printf("Testing jolly example\n");
315
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100316 the_pcu->alloc_algorithm = alloc_algorithm_b;
Max2ecf0fd2017-11-21 18:13:31 +0100317
318 enable_ts_on_bts(bts, false, true, true, true, true, false, false, false);
319
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100320 tfi = bts_tfi_find_free(bts, GPRS_RLCMAC_UL_TBF, &trx_no, -1);
Max2ecf0fd2017-11-21 18:13:31 +0100321 OSMO_ASSERT(tfi >= 0);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100322 ms = bts_alloc_ms(bts, ms_class, 0);
Pau Espin Pedrol1e009472021-01-11 20:40:19 +0100323 /* Avoid delaying free to avoid tons of to-be-freed ms objects queuing */
324 ms_set_timeout(ms, 0);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200325 ul_tbf = tbf_alloc_ul_tbf(bts, ms, -1, false);
Max2ecf0fd2017-11-21 18:13:31 +0100326 if (!ul_tbf)
327 return false;
328
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200329 OSMO_ASSERT(ul_tbf->ms() == ms);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100330 OSMO_ASSERT(ms_current_trx(ul_tbf->ms()));
331 trx_no = ms_current_trx(ms)->trx_no;
Max2ecf0fd2017-11-21 18:13:31 +0100332 dump_assignment(ul_tbf, "UL", true);
333
334 /* assume final ack has not been sent */
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200335 dl_tbf = tbf_alloc_dl_tbf(bts, ms, trx_no, false);
Max2ecf0fd2017-11-21 18:13:31 +0100336 if (!dl_tbf)
337 return false;
338
339 dump_assignment(dl_tbf, "DL", true);
340
341 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
342
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100343 check_tfi_usage(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100344
345 tbf_free(dl_tbf);
346 tbf_free(ul_tbf);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100347 talloc_free(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100348 return true;
349}
350
351static void test_alloc_b_for_ms(uint8_t ms_class)
352{
353 bool rc;
354
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100355 printf("Going to test multislot assignment MS_CLASS=%d\n", ms_class);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100356 /*
357 * PDCH is on TS 6,7,8 and we start with a UL allocation and
358 * then follow two DL allocations (once single, once normal).
359 *
360 * Uplink assigned and still available..
361 */
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100362
Max2ecf0fd2017-11-21 18:13:31 +0100363 rc = test_alloc_b_ul_dl(false, false, false, false, false, true, true, true, ms_class, true);
364 if (!rc)
365 return;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100366
367 /**
368 * Test with the other order.. first DL and then UL
369 */
Max2ecf0fd2017-11-21 18:13:31 +0100370 rc = test_alloc_b_dl_ul(false, false, false, false, false, true, true, true, ms_class, true);
371 if (!rc)
372 return;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100373
374 /* Andreas osmocom-pcu example */
Max2ecf0fd2017-11-21 18:13:31 +0100375 test_alloc_b_jolly(ms_class);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100376}
377
Max2ecf0fd2017-11-21 18:13:31 +0100378static 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 +0100379{
Max2ecf0fd2017-11-21 18:13:31 +0100380 bool rc;
381
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100382 /* we can test the allocation failures differently */
383 if (!ts0 && !ts1 && !ts2 && !ts3 && !ts4 && !ts5 && !ts6 && !ts7)
384 return;
385
386 printf("Mass test: TS0(%c%c%c%c%c%c%c%c)TS7 MS_Class=%d\n",
387 ts0 ? 'O' : 'x',
388 ts1 ? 'O' : 'x',
389 ts2 ? 'O' : 'x',
390 ts3 ? 'O' : 'x',
391 ts4 ? 'O' : 'x',
392 ts5 ? 'O' : 'x',
393 ts6 ? 'O' : 'x',
394 ts7 ? 'O' : 'x', ms_class);
395 fflush(stdout);
396
Max2ecf0fd2017-11-21 18:13:31 +0100397 rc = test_alloc_b_ul_dl(ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7, ms_class, false);
398 if (!rc)
399 return;
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100400
401 /**
402 * Test with the other order.. first DL and then UL
403 */
Max2ecf0fd2017-11-21 18:13:31 +0100404 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 +0100405}
406
407static void test_all_alloc_b()
408{
409 /* it is a bit crazy... */
410 for (uint8_t ts0 = 0; ts0 < 2; ++ts0)
411 for (uint8_t ts1 = 0; ts1 < 2; ++ts1)
412 for (uint8_t ts2 = 0; ts2 < 2; ++ts2)
413 for (uint8_t ts3 = 0; ts3 < 2; ++ts3)
414 for (uint8_t ts4 = 0; ts4 < 2; ++ts4)
415 for (uint8_t ts5 = 0; ts5 < 2; ++ts5)
416 for (uint8_t ts6 = 0; ts6 < 2; ++ts6)
417 for (uint8_t ts7 = 0; ts7 < 2; ++ts7)
Max9f460712018-01-23 20:57:08 +0100418 for (int ms_class = 0; ms_class < mslot_class_max(); ++ms_class)
Max2ecf0fd2017-11-21 18:13:31 +0100419 test_alloc_mass(ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7, ms_class);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100420}
421
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100422static void test_alloc_b()
423{
Max9f460712018-01-23 20:57:08 +0100424 for (int i = 0; i < mslot_class_max(); ++i)
Max2ecf0fd2017-11-21 18:13:31 +0100425 test_alloc_b_for_ms(i);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100426
427 test_all_alloc_b();
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100428}
429
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200430static char get_dir_char(uint8_t mask, uint8_t tx, uint8_t rx, uint8_t busy)
Jacob Erlbecke5655642015-06-29 12:19:52 +0200431{
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200432 int offs = busy ? 32 : 0;
433 return (mask & tx & rx) ? 'C' + offs :
434 (mask & tx) ? 'U' + offs :
435 (mask & rx) ? 'D' + offs :
Jacob Erlbecke5655642015-06-29 12:19:52 +0200436 '.';
437}
438
439enum test_mode {
440 TEST_MODE_UL_ONLY,
441 TEST_MODE_DL_ONLY,
442 TEST_MODE_UL_AND_DL,
443 TEST_MODE_DL_AND_UL,
444 TEST_MODE_DL_AFTER_UL,
445 TEST_MODE_UL_AFTER_DL,
446};
447
Maxc59ef122017-11-27 13:21:41 +0100448static inline char *test_mode_descr(enum test_mode t)
449{
450 switch (t) {
451 case TEST_MODE_UL_ONLY: return (char*)"UL only";
452 case TEST_MODE_DL_ONLY: return (char*)"DL only";
453 case TEST_MODE_UL_AND_DL: return (char*)"UL and DL";
454 case TEST_MODE_DL_AND_UL: return (char*)"DL and UL";
455 case TEST_MODE_DL_AFTER_UL: return (char*)"DL after UL";
456 case TEST_MODE_UL_AFTER_DL: return (char*)"UL after DL";
457 default: return NULL;
458 }
459}
460
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100461static GprsMs *alloc_tbfs(struct gprs_rlcmac_bts *bts, struct GprsMs *old_ms, enum test_mode mode)
Jacob Erlbecke5655642015-06-29 12:19:52 +0200462{
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100463 struct GprsMs *ms, *new_ms;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200464 uint8_t trx_no = -1;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200465
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100466 OSMO_ASSERT(old_ms != NULL);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200467
Jacob Erlbecke5655642015-06-29 12:19:52 +0200468 gprs_rlcmac_tbf *tbf = NULL;
469
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100470 if (ms_current_trx(old_ms))
471 trx_no = ms_current_trx(old_ms)->trx_no;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200472
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100473 ms_ref(old_ms);
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200474
Jacob Erlbecke5655642015-06-29 12:19:52 +0200475 /* Allocate what is needed first */
476 switch (mode) {
477 case TEST_MODE_UL_ONLY:
478 case TEST_MODE_DL_AFTER_UL:
479 case TEST_MODE_UL_AND_DL:
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100480 if (ms_ul_tbf(old_ms))
481 tbf_free(ms_ul_tbf(old_ms));
482 tbf = tbf_alloc_ul_tbf(bts, old_ms, trx_no, false);
483 if (tbf == NULL) {
Pau Espin Pedrol0dcbc072021-11-10 19:09:10 +0100484 OSMO_ASSERT(trx_no != -1 || bts_all_pdch_allocated(bts));
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100485 ms_unref(old_ms);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200486 return NULL;
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100487 }
Jacob Erlbecke5655642015-06-29 12:19:52 +0200488 break;
489 case TEST_MODE_DL_ONLY:
490 case TEST_MODE_UL_AFTER_DL:
491 case TEST_MODE_DL_AND_UL:
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100492 if (ms_dl_tbf(old_ms))
493 tbf_free(ms_dl_tbf(old_ms));
494 tbf = tbf_alloc_dl_tbf(bts, old_ms, trx_no, false);
495 if (tbf == NULL) {
Pau Espin Pedrol0dcbc072021-11-10 19:09:10 +0100496 OSMO_ASSERT(trx_no != -1 || bts_all_pdch_allocated(bts));
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100497 ms_unref(old_ms);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200498 return NULL;
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100499 }
Jacob Erlbecke5655642015-06-29 12:19:52 +0200500 }
501
502 OSMO_ASSERT(tbf);
503 OSMO_ASSERT(tbf->ms());
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100504 OSMO_ASSERT(old_ms == tbf->ms());
Jacob Erlbecke5655642015-06-29 12:19:52 +0200505 ms = tbf->ms();
506
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100507 ms_ref(ms);
508 new_ms = ms;
Jacob Erlbeck14376a72015-07-07 11:31:28 +0200509 /* Continue with what is needed next */
510 switch (mode) {
511 case TEST_MODE_UL_ONLY:
512 case TEST_MODE_DL_ONLY:
513 /* We are done */
514 break;
515
516 case TEST_MODE_DL_AFTER_UL:
517 case TEST_MODE_UL_AND_DL:
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100518 new_ms = alloc_tbfs(bts, ms, TEST_MODE_DL_ONLY);
Jacob Erlbeck14376a72015-07-07 11:31:28 +0200519 break;
520
521 case TEST_MODE_UL_AFTER_DL:
522 case TEST_MODE_DL_AND_UL:
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100523 new_ms = alloc_tbfs(bts, ms, TEST_MODE_UL_ONLY);
Jacob Erlbeck14376a72015-07-07 11:31:28 +0200524 break;
525 }
526
Jacob Erlbecke5655642015-06-29 12:19:52 +0200527 /* Optionally delete the TBF */
528 switch (mode) {
529 case TEST_MODE_DL_AFTER_UL:
530 case TEST_MODE_UL_AFTER_DL:
531 tbf_free(tbf);
Jacob Erlbeck0f352a62015-07-16 18:23:33 +0200532 tbf = NULL;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200533 break;
534
535 default:
536 break;
537 }
538
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100539 if (!new_ms && tbf)
Jacob Erlbeck0f352a62015-07-16 18:23:33 +0200540 tbf_free(tbf);
541
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100542 ms_unref(old_ms);
543 ms_unref(ms);
544 return new_ms;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200545}
546
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100547static unsigned alloc_many_tbfs(struct gprs_rlcmac_bts *bts, unsigned min_class,
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200548 unsigned max_class, enum test_mode mode)
Jacob Erlbecke5655642015-06-29 12:19:52 +0200549{
Jacob Erlbecke5655642015-06-29 12:19:52 +0200550 unsigned counter;
551 unsigned ms_class = min_class;
552
Jacob Erlbecke5655642015-06-29 12:19:52 +0200553 for (counter = 0; 1; counter += 1) {
554 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
555 uint8_t ul_slots = 0;
556 uint8_t dl_slots = 0;
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200557 uint8_t busy_slots = 0;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200558 unsigned i;
559 int tfi = -1;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200560 int tfi2;
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200561 uint8_t trx_no2;
562 struct gprs_rlcmac_trx *trx;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200563 GprsMs *ms;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200564 enum gprs_rlcmac_tbf_direction dir;
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200565 uint32_t tlli = counter + 0xc0000000;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200566
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100567 ms = bts_ms_by_tlli(bts, tlli, GSM_RESERVED_TMSI);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200568 if (!ms)
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100569 ms = bts_alloc_ms(bts, 0, 0);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100570 ms_set_ms_class(ms, ms_class);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100571 ms = alloc_tbfs(bts, ms, mode);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200572 if (!ms)
573 break;
574
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100575 ms_set_tlli(ms, tlli);
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200576
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100577 ul_tbf = ms_ul_tbf(ms);
578 dl_tbf = ms_dl_tbf(ms);
579 trx = ms_current_trx(ms);
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200580
581 OSMO_ASSERT(ul_tbf || dl_tbf);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200582
583 if (ul_tbf) {
584 ul_slots = 1 << ul_tbf->first_common_ts;
585 tfi = ul_tbf->tfi();
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200586 dir = GPRS_RLCMAC_UL_TBF;
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200587 } else {
Jacob Erlbecke5655642015-06-29 12:19:52 +0200588 ul_slots = 1 << dl_tbf->first_common_ts;
589 tfi = dl_tbf->tfi();
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200590 dir = GPRS_RLCMAC_DL_TBF;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200591 }
592
593 for (i = 0; dl_tbf && i < ARRAY_SIZE(dl_tbf->pdch); i += 1)
594 if (dl_tbf->pdch[i])
595 dl_slots |= 1 << i;
596
Max5b0df1f2017-09-11 10:38:59 +0200597 for (i = 0; ul_tbf && i < ARRAY_SIZE(ul_tbf->pdch); i += 1)
598 if (ul_tbf->pdch[i])
599 ul_slots |= 1 << i;
600
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200601 for (i = 0; trx && i < ARRAY_SIZE(trx->pdch); i += 1) {
602 struct gprs_rlcmac_pdch *pdch = &trx->pdch[i];
603
604 if (ul_tbf && dl_tbf)
605 continue;
606
607 if (ul_tbf &&
Maxd000d802017-09-20 17:55:28 +0200608 pdch->assigned_tfi(GPRS_RLCMAC_DL_TBF) != NO_FREE_TFI)
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200609 continue;
610
611 if (dl_tbf &&
Maxd000d802017-09-20 17:55:28 +0200612 pdch->assigned_tfi(GPRS_RLCMAC_UL_TBF) != NO_FREE_TFI)
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200613 continue;
614
615 busy_slots |= 1 << i;
616 }
617
Maxd000d802017-09-20 17:55:28 +0200618 printf(" TBF[%d] class %d reserves " OSMO_BIT_SPEC "\n",
Jacob Erlbecke5655642015-06-29 12:19:52 +0200619 tfi, ms_class,
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200620 get_dir_char(0x01, ul_slots, dl_slots, busy_slots),
621 get_dir_char(0x02, ul_slots, dl_slots, busy_slots),
622 get_dir_char(0x04, ul_slots, dl_slots, busy_slots),
623 get_dir_char(0x08, ul_slots, dl_slots, busy_slots),
624 get_dir_char(0x10, ul_slots, dl_slots, busy_slots),
625 get_dir_char(0x20, ul_slots, dl_slots, busy_slots),
626 get_dir_char(0x40, ul_slots, dl_slots, busy_slots),
627 get_dir_char(0x80, ul_slots, dl_slots, busy_slots));
Jacob Erlbecke5655642015-06-29 12:19:52 +0200628
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200629 if (tfi >= 0) {
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100630 OSMO_ASSERT(ms_current_trx(ms));
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100631 tfi2 = bts_tfi_find_free(bts, dir, &trx_no2,
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100632 ms_current_trx(ms)->trx_no);
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200633 OSMO_ASSERT(tfi != tfi2);
Jacob Erlbeck7b3675b2015-07-16 18:28:22 +0200634 OSMO_ASSERT(tfi2 < 0 ||
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100635 trx_no2 == ms_current_trx(ms)->trx_no);
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200636 }
637
Jacob Erlbecke5655642015-06-29 12:19:52 +0200638 ms_class += 1;
639 if (ms_class > max_class)
640 ms_class = min_class;
641 }
642
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200643 return counter;
644}
645
Pau Espin Pedrolc85e0932021-02-25 18:08:10 +0100646static void test_successive_allocation(alloc_algorithm_func_t algo, unsigned min_class,
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200647 unsigned max_class, enum test_mode mode,
648 unsigned expect_num, const char *text)
649{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100650 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200651 struct gprs_rlcmac_trx *trx;
652 unsigned counter;
653
Maxc59ef122017-11-27 13:21:41 +0100654 printf("Going to test assignment with many TBF, algorithm %s class %u..%u (%s)\n",
655 text, min_class, max_class, test_mode_descr(mode));
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200656
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100657 the_pcu->alloc_algorithm = algo;
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200658
659 trx = &bts->trx[0];
660 trx->pdch[3].enable();
661 trx->pdch[4].enable();
662 trx->pdch[5].enable();
663 trx->pdch[6].enable();
664 trx->pdch[7].enable();
665
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100666 counter = alloc_many_tbfs(bts, min_class, max_class, mode);
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200667
Maxc59ef122017-11-27 13:21:41 +0100668 printf(" Successfully allocated %u UL TBFs, algorithm %s class %u..%u (%s)\n",
669 counter, text, min_class, max_class, test_mode_descr(mode));
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200670 if (counter != expect_num)
Maxc59ef122017-11-27 13:21:41 +0100671 fprintf(stderr, " Expected %u TBFs (got %u), algorithm %s class %u..%u (%s)\n",
672 expect_num, counter, text, min_class, max_class, test_mode_descr(mode));
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200673
Jacob Erlbeckec478752015-06-19 16:35:38 +0200674 OSMO_ASSERT(counter == expect_num);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200675
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100676 check_tfi_usage(bts);
677 talloc_free(bts);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200678}
679
Pau Espin Pedrolc85e0932021-02-25 18:08:10 +0100680static void test_many_connections(alloc_algorithm_func_t algo, unsigned expect_num,
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200681 const char *text)
682{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100683 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200684 struct gprs_rlcmac_trx *trx;
685 int counter1, counter2 = -1;
686 unsigned i;
687 enum test_mode mode_seq[] = {
688 TEST_MODE_DL_AFTER_UL,
689 TEST_MODE_UL_ONLY,
690 TEST_MODE_DL_AFTER_UL,
691 TEST_MODE_DL_ONLY,
692 };
693
Maxc59ef122017-11-27 13:21:41 +0100694 printf("Going to test assignment with many connections, algorithm %s\n", text);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200695
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100696 the_pcu->alloc_algorithm = algo;
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200697
698 trx = &bts->trx[0];
699 trx->pdch[3].enable();
700 trx->pdch[4].enable();
701 trx->pdch[5].enable();
702 trx->pdch[6].enable();
703 trx->pdch[7].enable();
704
705 for (i = 0; i < ARRAY_SIZE(mode_seq); i += 1) {
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100706 counter1 = alloc_many_tbfs(bts, 1, mslot_class_max(), mode_seq[i]);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200707 fprintf(stderr, " Allocated %d TBFs (previously %d)\n",
708 counter1, counter2);
709
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100710 check_tfi_usage(bts);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200711
712 /* This will stop earlier due to USF shortage */
713 if (mode_seq[i] == TEST_MODE_UL_ONLY)
714 continue;
715
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200716 if (counter2 >= 0) {
717 if (counter1 < counter2)
718 fprintf(stderr, " Expected %d >= %d in %s\n",
719 counter1, counter2, text);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200720 OSMO_ASSERT(counter1 >= counter2);
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200721 }
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200722
723 counter2 = counter1;
724 }
725
726 printf(" Successfully allocated %d TBFs\n", counter1);
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200727 if (counter1 != (int)expect_num)
Maxc59ef122017-11-27 13:21:41 +0100728 fprintf(stderr, " Expected %d TBFs (got %d) for algorithm %s\n", expect_num, counter1, text);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200729
730 OSMO_ASSERT(expect_num == (unsigned)counter1);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100731 talloc_free(bts);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200732}
733
Maxc59ef122017-11-27 13:21:41 +0100734static 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 +0200735{
Maxc59ef122017-11-27 13:21:41 +0100736 test_successive_allocation(alloc_algorithm_a, 1, 1, mode, exp_A, "A");
737 test_successive_allocation(alloc_algorithm_b, 10, 10, mode, exp_B, "B");
738 test_successive_allocation(alloc_algorithm_dynamic, 10, 10, mode, exp_dyn, "dynamic");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200739}
740
Maxc59ef122017-11-27 13:21:41 +0100741static void test_successive_allocations()
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200742{
Maxc59ef122017-11-27 13:21:41 +0100743 test_successive_allocation(alloc_algorithm_a, 1, 1, TEST_MODE_UL_AND_DL, 35, "A");
Pau Espin Pedroled2afa32021-02-22 17:20:15 +0100744 test_successive_allocation(alloc_algorithm_b, 10, 10, TEST_MODE_UL_AND_DL, 15, "B");
745 test_successive_allocation(alloc_algorithm_b, 12, 12, TEST_MODE_UL_AND_DL, 15, "B");
Maxc59ef122017-11-27 13:21:41 +0100746
Pau Espin Pedroled2afa32021-02-22 17:20:15 +0100747 test_successive_allocation(alloc_algorithm_b, 1, 12, TEST_MODE_UL_AND_DL, 23, "B");
748 test_successive_allocation(alloc_algorithm_b, 1, mslot_class_max(), TEST_MODE_UL_AND_DL, 17, "B");
749 test_successive_allocation(alloc_algorithm_dynamic, 1, mslot_class_max(), TEST_MODE_UL_AND_DL, 17, "dynamic");
Maxc59ef122017-11-27 13:21:41 +0100750
Pau Espin Pedroled2afa32021-02-22 17:20:15 +0100751 test_a_b_dyn(TEST_MODE_DL_AND_UL, 35, 15, 15);
752 test_a_b_dyn(TEST_MODE_DL_AFTER_UL, 160, 32, 101);
753 test_a_b_dyn(TEST_MODE_UL_AFTER_DL, 35, 15, 15);
754 test_a_b_dyn(TEST_MODE_UL_ONLY, 35, 15, 21);
Maxc59ef122017-11-27 13:21:41 +0100755 test_a_b_dyn(TEST_MODE_DL_ONLY, 160, 32, 101);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200756}
757
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530758static void test_2_consecutive_dl_tbfs()
759{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100760 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200761 GprsMs *ms;
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530762 struct gprs_rlcmac_trx *trx;
763 uint8_t ms_class = 11;
764 uint8_t egprs_ms_class = 11;
765 gprs_rlcmac_tbf *dl_tbf1, *dl_tbf2;
766 uint8_t numTs1 = 0, numTs2 = 0;
767
768 printf("Testing DL TS allocation for Multi UEs\n");
769
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100770 the_pcu->alloc_algorithm = alloc_algorithm_b;
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530771
772 trx = &bts->trx[0];
773 trx->pdch[4].enable();
774 trx->pdch[5].enable();
775 trx->pdch[6].enable();
776 trx->pdch[7].enable();
777
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100778 ms = bts_alloc_ms(bts, ms_class, egprs_ms_class);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200779 dl_tbf1 = tbf_alloc_dl_tbf(bts, ms, 0, false);
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530780 OSMO_ASSERT(dl_tbf1);
781
782 for (int i = 0; i < 8; i++) {
783 if (dl_tbf1->pdch[i])
784 numTs1++;
785 }
786 OSMO_ASSERT(numTs1 == 4);
787 printf("TBF1: numTs(%d)\n", numTs1);
788
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100789 ms = bts_alloc_ms(bts, ms_class, egprs_ms_class);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200790 dl_tbf2 = tbf_alloc_dl_tbf(bts, ms, 0, false);
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530791 OSMO_ASSERT(dl_tbf2);
792
793 for (int i = 0; i < 8; i++) {
794 if (dl_tbf2->pdch[i])
795 numTs2++;
796 }
797
798 /*
799 * TODO: currently 2nd DL TBF gets 3 TS
800 * This behaviour will be fixed in subsequent patch
801 */
802 printf("TBF2: numTs(%d)\n", numTs2);
803 OSMO_ASSERT(numTs2 == 3);
804
805 tbf_free(dl_tbf1);
806 tbf_free(dl_tbf2);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100807 talloc_free(bts);
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530808}
809
Oliver Smith3f794702021-08-23 14:19:21 +0200810static void test_bts_pch_timer(void)
811{
812 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100813 struct osmo_mobile_identity mi_imsi1, mi_imsi2;
Pau Espin Pedrol19b33922021-11-08 17:49:52 +0100814 struct osmo_mobile_identity mi_tmsi1;
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100815 mi_imsi1.type = mi_imsi2.type = GSM_MI_TYPE_IMSI;
Pau Espin Pedrol19b33922021-11-08 17:49:52 +0100816 mi_tmsi1.type = GSM_MI_TYPE_TMSI;
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100817 OSMO_STRLCPY_ARRAY(mi_imsi1.imsi, "1234");
818 OSMO_STRLCPY_ARRAY(mi_imsi2.imsi, "5678");
Pau Espin Pedrol19b33922021-11-08 17:49:52 +0100819 mi_tmsi1.tmsi = 987654321;
Oliver Smith3f794702021-08-23 14:19:21 +0200820
821 fprintf(stderr, "Testing bts_pch_timer dealloc on bts dealloc\n");
822 log_set_category_filter(osmo_stderr_target, DPCU, 1, LOGL_DEBUG);
823
824 fprintf(stderr, "Starting PCH timer for 2 IMSI\n");
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100825 bts_pch_timer_start(bts, &mi_imsi1, mi_imsi1.imsi);
826 bts_pch_timer_start(bts, &mi_imsi2, mi_imsi2.imsi);
Pau Espin Pedrol19b33922021-11-08 17:49:52 +0100827 fprintf(stderr, "Starting PCH timer for 1 TMSI\n");
828 bts_pch_timer_start(bts, &mi_tmsi1, "6666");
Oliver Smith3f794702021-08-23 14:19:21 +0200829
830 fprintf(stderr, "Deallocating BTS, expecting the PCH timer to be stopped and deallocated\n");
831 talloc_free(bts);
832}
833
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200834int main(int argc, char **argv)
835{
836 tall_pcu_ctx = talloc_named_const(NULL, 1, "moiji-mobile AllocTest context");
837 if (!tall_pcu_ctx)
838 abort();
839
Neels Hofmeyr78ce5912017-02-08 17:07:31 +0100840 msgb_talloc_ctx_init(tall_pcu_ctx, 0);
Neels Hofmeyr42f2d612018-04-01 16:54:40 +0200841 osmo_init_logging2(tall_pcu_ctx, &gprs_log_info);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200842 log_set_use_color(osmo_stderr_target, 0);
Pau Espin Pedrol00f52cc2021-02-19 14:01:52 +0100843 log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE);
Pau Espin Pedrolb18d2a52021-02-19 14:00:48 +0100844 log_set_print_category(osmo_stderr_target, 0);
845 log_set_print_category_hex(osmo_stderr_target, 0);
Philipp Maierde0e5582020-03-25 12:23:52 +0100846 log_set_category_filter(osmo_stderr_target, DTBF, 1, LOGL_INFO);
Jacob Erlbeck9ec49e22015-06-29 13:00:20 +0200847 if (getenv("LOGL_DEBUG"))
848 log_set_log_level(osmo_stderr_target, LOGL_DEBUG);
Pau Espin Pedroldc2aaac2021-05-14 12:50:46 +0200849 osmo_fsm_log_addr(false);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200850
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100851 the_pcu = gprs_pcu_alloc(tall_pcu_ctx);
852
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200853 test_alloc_a();
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100854 test_alloc_b();
Maxc59ef122017-11-27 13:21:41 +0100855 test_successive_allocations();
856 test_many_connections(alloc_algorithm_a, 160, "A");
Max01bd0cc2018-01-23 20:58:49 +0100857 test_many_connections(alloc_algorithm_b, 32, "B");
Maxc59ef122017-11-27 13:21:41 +0100858 test_many_connections(alloc_algorithm_dynamic, 160, "dynamic");
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530859 test_2_consecutive_dl_tbfs();
Oliver Smith3f794702021-08-23 14:19:21 +0200860 test_bts_pch_timer();
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100861
862 talloc_free(the_pcu);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200863 return EXIT_SUCCESS;
864}
865
866/*
867 * stubs that should not be reached
868 */
869extern "C" {
870void l1if_pdch_req() { abort(); }
871void l1if_connect_pdch() { abort(); }
872void l1if_close_pdch() { abort(); }
873void l1if_open_pdch() { abort(); }
874}