blob: 3282268105debb1aa3dad167d4cd9a42479b64e4 [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"
Pau Espin Pedrolff7c5812022-12-14 18:49:06 +010030#include "alloc_algo.h"
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020031#include <osmocom/core/application.h>
32#include <osmocom/core/msgb.h>
33#include <osmocom/core/talloc.h>
34#include <osmocom/core/utils.h>
Pau Espin Pedroldc2aaac2021-05-14 12:50:46 +020035#include <osmocom/core/fsm.h>
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020036}
37
38/* globals used by the code */
39void *tall_pcu_ctx;
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020040int16_t spoof_mnc = 0, spoof_mcc = 0;
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +010041bool spoof_mnc_3_digits = false;
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020042
Daniel Willmann48aa0b02014-07-16 18:54:10 +020043static gprs_rlcmac_tbf *tbf_alloc(struct gprs_rlcmac_bts *bts,
Jacob Erlbecke2e004e2015-06-18 17:16:26 +020044 GprsMs *ms, gprs_rlcmac_tbf_direction dir,
Pau Espin Pedrol322456e2020-05-08 18:15:59 +020045 uint8_t use_trx, bool single_slot)
Daniel Willmann48aa0b02014-07-16 18:54:10 +020046{
Pau Espin Pedrol322456e2020-05-08 18:15:59 +020047 OSMO_ASSERT(ms != NULL);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +020048
Daniel Willmann48aa0b02014-07-16 18:54:10 +020049 if (dir == GPRS_RLCMAC_UL_TBF)
Pau Espin Pedrolbda7bb72022-10-31 14:33:09 +010050 return ul_tbf_alloc(bts, ms, use_trx, single_slot);
Daniel Willmann48aa0b02014-07-16 18:54:10 +020051 else
Pau Espin Pedrol87573842022-10-26 20:23:45 +020052 return dl_tbf_alloc(bts, ms, use_trx, single_slot);
Daniel Willmann48aa0b02014-07-16 18:54:10 +020053}
54
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010055static void check_tfi_usage(struct gprs_rlcmac_bts *bts)
Jacob Erlbeck61205a72015-07-09 11:35:50 +020056{
57 int pdch_no;
Jacob Erlbeck61205a72015-07-09 11:35:50 +020058
59 struct gprs_rlcmac_tbf *tfi_usage[8][8][2][32] = {{{{NULL}}}};
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010060 struct llist_head *tbf_lists[2] = {
Pau Espin Pedrol1a1557a2021-05-13 18:39:36 +020061 &bts->trx[0].ul_tbfs,
62 &bts->trx[0].dl_tbfs
Jacob Erlbeck61205a72015-07-09 11:35:50 +020063 };
64
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010065 struct llist_item *pos;
Jacob Erlbeck61205a72015-07-09 11:35:50 +020066 gprs_rlcmac_tbf *tbf;
Jacob Erlbeck61205a72015-07-09 11:35:50 +020067 unsigned list_idx;
68 struct gprs_rlcmac_tbf **tbf_var;
69
70 for (list_idx = 0; list_idx < ARRAY_SIZE(tbf_lists); list_idx += 1)
71 {
72
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010073 llist_for_each_entry(pos, tbf_lists[list_idx], list) {
74 tbf = (struct gprs_rlcmac_tbf *)pos->entry;
Jacob Erlbeck61205a72015-07-09 11:35:50 +020075 for (pdch_no = 0; pdch_no < 8; pdch_no += 1) {
76 struct gprs_rlcmac_pdch *pdch = tbf->pdch[pdch_no];
77 if (pdch == NULL)
78 continue;
79
80 tbf_var = &tfi_usage
81 [tbf->trx->trx_no]
82 [pdch_no]
83 [tbf->direction]
84 [tbf->tfi()];
85
86 OSMO_ASSERT(*tbf_var == NULL);
87 if (tbf->direction == GPRS_RLCMAC_DL_TBF) {
88 OSMO_ASSERT(pdch->dl_tbf_by_tfi(
89 tbf->tfi()) == tbf);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010090 OSMO_ASSERT(bts_dl_tbf_by_tfi(bts,
Jacob Erlbeck61205a72015-07-09 11:35:50 +020091 tbf->tfi(),
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +020092 tbf->trx->trx_no,
93 pdch_no) == tbf);
Jacob Erlbeck61205a72015-07-09 11:35:50 +020094 } else {
95 OSMO_ASSERT(pdch->ul_tbf_by_tfi(
96 tbf->tfi()) == tbf);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010097 OSMO_ASSERT(bts_ul_tbf_by_tfi(bts,
Jacob Erlbeck61205a72015-07-09 11:35:50 +020098 tbf->tfi(),
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +020099 tbf->trx->trx_no,
100 pdch_no) == tbf);
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200101 }
102 *tbf_var = tbf;
Jacob Erlbeck47a57f62015-07-08 12:53:16 +0200103 OSMO_ASSERT(pdch->assigned_tfi(tbf->direction) &
104 (1 << tbf->tfi()));
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200105 }
106 }
107 }
108}
109
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200110static void test_alloc_a(gprs_rlcmac_tbf_direction dir,
111 uint8_t slots, const int count)
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200112{
113 int tfi;
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200114 int i;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200115 uint8_t used_trx, tmp_trx;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100116 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200117 GprsMs *ms;
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200118 struct gprs_rlcmac_tbf *tbfs[32*8+1] = { 0, };
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200119
120 printf("Testing alloc_a direction(%d)\n", dir);
121
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100122 the_pcu->alloc_algorithm = alloc_algorithm_a;
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200123
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200124 struct gprs_rlcmac_trx *trx = &bts->trx[0];
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200125 for (i = 0; i < 8; i += 1)
126 if (slots & (1 << i))
127 trx->pdch[i].enable();
128
129 OSMO_ASSERT(count >= 0 && count <= (int)ARRAY_SIZE(tbfs));
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200130
131 /**
132 * Currently alloc_a will only allocate from the first
133 * PDCH and all possible usf's. We run out of usf's before
134 * we are out of tfi's. Observe this and make sure that at
135 * least this part is working okay.
136 */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200137 for (i = 0; i < (int)ARRAY_SIZE(tbfs); ++i) {
Pau Espin Pedrolbfc97562023-04-17 14:49:29 +0200138 ms = bts_alloc_ms(bts);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200139 tbfs[i] = tbf_alloc(bts, ms, dir, -1, 0);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200140 if (tbfs[i] == NULL)
141 break;
142
143 used_trx = tbfs[i]->trx->trx_no;
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100144 tfi = bts_tfi_find_free(bts, dir, &tmp_trx, used_trx);
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200145 OSMO_ASSERT(tbfs[i]->tfi() != tfi);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200146 }
147
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100148 check_tfi_usage(bts);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200149
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200150 OSMO_ASSERT(i == count);
151
Pau Espin Pedrol0dcbc072021-11-10 19:09:10 +0100152 OSMO_ASSERT(bts_all_pdch_allocated(bts));
153
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200154 for (i = 0; i < count; ++i)
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200155 if (tbfs[i])
156 tbf_free(tbfs[i]);
157
Pau Espin Pedrolbfc97562023-04-17 14:49:29 +0200158 ms = bts_alloc_ms(bts);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200159 tbfs[0] = tbf_alloc(bts, ms, dir, -1, 0);
Holger Hans Peter Freytherf3f1bde2016-02-22 15:14:01 +0100160 OSMO_ASSERT(tbfs[0]);
161 tbf_free(tbfs[0]);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100162 talloc_free(bts);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200163}
164
165static void test_alloc_a()
166{
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200167 /* slots 2 - 3 */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200168 test_alloc_a(GPRS_RLCMAC_DL_TBF, 0x0c, 32*2);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200169 test_alloc_a(GPRS_RLCMAC_UL_TBF, 0x0c, 14);
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200170
171 /* slots 1 - 5 */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200172 test_alloc_a(GPRS_RLCMAC_DL_TBF, 0x1e, 32*4);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200173 test_alloc_a(GPRS_RLCMAC_UL_TBF, 0x1e, 28);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200174}
175
Max2ecf0fd2017-11-21 18:13:31 +0100176static void dump_assignment(struct gprs_rlcmac_tbf *tbf, const char *dir, bool verbose)
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100177{
Max2ecf0fd2017-11-21 18:13:31 +0100178 if (!verbose)
179 return;
Pau Espin Pedrol345d9ad2022-12-12 19:22:44 +0100180 const struct GprsMs *ms = tbf_ms(tbf);
Pau Espin Pedrol9935d0d2022-12-13 18:29:25 +0100181 const struct gprs_rlcmac_pdch *first_common = ms_first_common_ts(ms);
Max2ecf0fd2017-11-21 18:13:31 +0100182
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200183 for (size_t i = 0; i < ARRAY_SIZE(tbf->pdch); ++i)
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100184 if (tbf->pdch[i])
Neels Hofmeyrd34646a2017-02-08 17:07:40 +0100185 printf("PDCH[%zu] is used for %s\n", i, dir);
Pau Espin Pedrole2ed40d2022-12-15 17:35:05 +0100186 printf("%s is control_ts for %s\n", tbf->control_ts ? pdch_name(tbf->control_ts) : "(none)", dir);
187 printf("%s is first common for %s\n", first_common ? pdch_name(first_common) : "(none)", dir);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100188}
189
Max2ecf0fd2017-11-21 18:13:31 +0100190#define ENABLE_PDCH(ts_no, enable_flag, trx) \
191 if (enable_flag) \
192 trx->pdch[ts_no].enable();
193
194static inline void enable_ts_on_bts(struct gprs_rlcmac_bts *bts,
195 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 +0100196{
Max2ecf0fd2017-11-21 18:13:31 +0100197 struct gprs_rlcmac_trx *trx = &bts->trx[0];
198
199 ENABLE_PDCH(0, ts0, trx);
200 ENABLE_PDCH(1, ts1, trx);
201 ENABLE_PDCH(2, ts2, trx);
202 ENABLE_PDCH(3, ts3, trx);
203 ENABLE_PDCH(4, ts4, trx);
204 ENABLE_PDCH(5, ts5, trx);
205 ENABLE_PDCH(6, ts6, trx);
206 ENABLE_PDCH(7, ts7, trx);
207}
208
209static inline bool test_alloc_b_ul_dl(bool ts0, bool ts1, bool ts2, bool ts3, bool ts4, bool ts5, bool ts6, bool ts7,
210 uint8_t ms_class, bool verbose)
211{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100212 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200213 GprsMs *ms;
Max2ecf0fd2017-11-21 18:13:31 +0100214 gprs_rlcmac_ul_tbf *ul_tbf;
215 gprs_rlcmac_dl_tbf *dl_tbf;
216
217 if (verbose)
218 printf("Testing UL then DL assignment.\n");
219
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100220 the_pcu->alloc_algorithm = alloc_algorithm_b;
Max2ecf0fd2017-11-21 18:13:31 +0100221
222 enable_ts_on_bts(bts, ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7);
223
Pau Espin Pedrolbfc97562023-04-17 14:49:29 +0200224 ms = bts_alloc_ms(bts);
225 ms_set_ms_class(ms, ms_class);
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 Pedrolbda7bb72022-10-31 14:33:09 +0100228 ul_tbf = ul_tbf_alloc(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 Pedrol87573842022-10-26 20:23:45 +0200238 dl_tbf = dl_tbf_alloc(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
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100244 check_tfi_usage(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100245
246 tbf_free(dl_tbf);
247 tbf_free(ul_tbf);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100248 talloc_free(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100249 return true;
250}
251
252static inline bool test_alloc_b_dl_ul(bool ts0, bool ts1, bool ts2, bool ts3, bool ts4, bool ts5, bool ts6, bool ts7,
253 uint8_t ms_class, bool verbose)
254{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100255 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200256 GprsMs *ms;
Max2ecf0fd2017-11-21 18:13:31 +0100257 gprs_rlcmac_ul_tbf *ul_tbf;
258 gprs_rlcmac_dl_tbf *dl_tbf;
259
260 if (verbose)
261 printf("Testing DL then UL assignment followed by update\n");
262
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100263 the_pcu->alloc_algorithm = alloc_algorithm_b;
Max2ecf0fd2017-11-21 18:13:31 +0100264
265 enable_ts_on_bts(bts, ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7);
266
Pau Espin Pedrolbfc97562023-04-17 14:49:29 +0200267 ms = bts_alloc_ms(bts);
268 ms_set_ms_class(ms, ms_class);
Pau Espin Pedrol1e009472021-01-11 20:40:19 +0100269 /* Avoid delaying free to avoid tons of to-be-freed ms objects queuing */
270 ms_set_timeout(ms, 0);
Pau Espin Pedrol87573842022-10-26 20:23:45 +0200271 dl_tbf = dl_tbf_alloc(bts, ms, -1, true);
Max2ecf0fd2017-11-21 18:13:31 +0100272 if (!dl_tbf)
273 return false;
274
Pau Espin Pedrol8abe13c2022-10-21 18:49:48 +0200275 ms_confirm_tlli(ms, 0x23);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200276 OSMO_ASSERT(dl_tbf->ms() == ms);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100277 OSMO_ASSERT(ms_current_trx(dl_tbf->ms()));
Max2ecf0fd2017-11-21 18:13:31 +0100278
279 dump_assignment(dl_tbf, "DL", verbose);
280
Pau Espin Pedrolbda7bb72022-10-31 14:33:09 +0100281 ul_tbf = ul_tbf_alloc(bts, ms, ms_current_trx(ms)->trx_no, false);
Max2ecf0fd2017-11-21 18:13:31 +0100282 if (!ul_tbf)
283 return false;
284
Pau Espin Pedrol8abe13c2022-10-21 18:49:48 +0200285 ms_update_announced_tlli(ms, 0x23);
Pau Espin Pedrol0f859562022-10-31 10:51:20 +0100286 ul_tbf->m_contention_resolution_done = true;
Max2ecf0fd2017-11-21 18:13:31 +0100287
288 dump_assignment(ul_tbf, "UL", verbose);
289
Pau Espin Pedrolb7a2b592022-12-13 14:43:59 +0100290 /* now upgrade the dl_tbf */
291 OSMO_ASSERT(dl_tbf_upgrade_to_multislot(dl_tbf) == 0);
Max2ecf0fd2017-11-21 18:13:31 +0100292 dump_assignment(dl_tbf, "DL", verbose);
Max2ecf0fd2017-11-21 18:13:31 +0100293
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 Pedrolbfc97562023-04-17 14:49:29 +0200318 ms = bts_alloc_ms(bts);
319 ms_set_ms_class(ms, ms_class);
Pau Espin Pedrol1e009472021-01-11 20:40:19 +0100320 /* Avoid delaying free to avoid tons of to-be-freed ms objects queuing */
321 ms_set_timeout(ms, 0);
Pau Espin Pedrolbda7bb72022-10-31 14:33:09 +0100322 ul_tbf = ul_tbf_alloc(bts, ms, -1, false);
Max2ecf0fd2017-11-21 18:13:31 +0100323 if (!ul_tbf)
324 return false;
325
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200326 OSMO_ASSERT(ul_tbf->ms() == ms);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100327 OSMO_ASSERT(ms_current_trx(ul_tbf->ms()));
328 trx_no = ms_current_trx(ms)->trx_no;
Max2ecf0fd2017-11-21 18:13:31 +0100329 dump_assignment(ul_tbf, "UL", true);
330
331 /* assume final ack has not been sent */
Pau Espin Pedrol87573842022-10-26 20:23:45 +0200332 dl_tbf = dl_tbf_alloc(bts, ms, trx_no, false);
Max2ecf0fd2017-11-21 18:13:31 +0100333 if (!dl_tbf)
334 return false;
335
336 dump_assignment(dl_tbf, "DL", true);
337
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100338 check_tfi_usage(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100339
340 tbf_free(dl_tbf);
341 tbf_free(ul_tbf);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100342 talloc_free(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100343 return true;
344}
345
346static void test_alloc_b_for_ms(uint8_t ms_class)
347{
348 bool rc;
349
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100350 printf("Going to test multislot assignment MS_CLASS=%d\n", ms_class);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100351 /*
352 * PDCH is on TS 6,7,8 and we start with a UL allocation and
353 * then follow two DL allocations (once single, once normal).
354 *
355 * Uplink assigned and still available..
356 */
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100357
Max2ecf0fd2017-11-21 18:13:31 +0100358 rc = test_alloc_b_ul_dl(false, false, false, false, false, true, true, true, ms_class, true);
359 if (!rc)
360 return;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100361
362 /**
363 * Test with the other order.. first DL and then UL
364 */
Max2ecf0fd2017-11-21 18:13:31 +0100365 rc = test_alloc_b_dl_ul(false, false, false, false, false, true, true, true, ms_class, true);
366 if (!rc)
367 return;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100368
369 /* Andreas osmocom-pcu example */
Max2ecf0fd2017-11-21 18:13:31 +0100370 test_alloc_b_jolly(ms_class);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100371}
372
Max2ecf0fd2017-11-21 18:13:31 +0100373static 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 +0100374{
Max2ecf0fd2017-11-21 18:13:31 +0100375 bool rc;
376
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100377 /* we can test the allocation failures differently */
378 if (!ts0 && !ts1 && !ts2 && !ts3 && !ts4 && !ts5 && !ts6 && !ts7)
379 return;
380
381 printf("Mass test: TS0(%c%c%c%c%c%c%c%c)TS7 MS_Class=%d\n",
382 ts0 ? 'O' : 'x',
383 ts1 ? 'O' : 'x',
384 ts2 ? 'O' : 'x',
385 ts3 ? 'O' : 'x',
386 ts4 ? 'O' : 'x',
387 ts5 ? 'O' : 'x',
388 ts6 ? 'O' : 'x',
389 ts7 ? 'O' : 'x', ms_class);
390 fflush(stdout);
391
Max2ecf0fd2017-11-21 18:13:31 +0100392 rc = test_alloc_b_ul_dl(ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7, ms_class, false);
393 if (!rc)
394 return;
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100395
396 /**
397 * Test with the other order.. first DL and then UL
398 */
Max2ecf0fd2017-11-21 18:13:31 +0100399 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 +0100400}
401
402static void test_all_alloc_b()
403{
404 /* it is a bit crazy... */
405 for (uint8_t ts0 = 0; ts0 < 2; ++ts0)
406 for (uint8_t ts1 = 0; ts1 < 2; ++ts1)
407 for (uint8_t ts2 = 0; ts2 < 2; ++ts2)
408 for (uint8_t ts3 = 0; ts3 < 2; ++ts3)
409 for (uint8_t ts4 = 0; ts4 < 2; ++ts4)
410 for (uint8_t ts5 = 0; ts5 < 2; ++ts5)
411 for (uint8_t ts6 = 0; ts6 < 2; ++ts6)
412 for (uint8_t ts7 = 0; ts7 < 2; ++ts7)
Max9f460712018-01-23 20:57:08 +0100413 for (int ms_class = 0; ms_class < mslot_class_max(); ++ms_class)
Max2ecf0fd2017-11-21 18:13:31 +0100414 test_alloc_mass(ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7, ms_class);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100415}
416
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100417static void test_alloc_b()
418{
Max9f460712018-01-23 20:57:08 +0100419 for (int i = 0; i < mslot_class_max(); ++i)
Max2ecf0fd2017-11-21 18:13:31 +0100420 test_alloc_b_for_ms(i);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100421
422 test_all_alloc_b();
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100423}
424
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200425static char get_dir_char(uint8_t mask, uint8_t tx, uint8_t rx, uint8_t busy)
Jacob Erlbecke5655642015-06-29 12:19:52 +0200426{
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200427 int offs = busy ? 32 : 0;
428 return (mask & tx & rx) ? 'C' + offs :
429 (mask & tx) ? 'U' + offs :
430 (mask & rx) ? 'D' + offs :
Jacob Erlbecke5655642015-06-29 12:19:52 +0200431 '.';
432}
433
434enum test_mode {
435 TEST_MODE_UL_ONLY,
436 TEST_MODE_DL_ONLY,
437 TEST_MODE_UL_AND_DL,
438 TEST_MODE_DL_AND_UL,
439 TEST_MODE_DL_AFTER_UL,
440 TEST_MODE_UL_AFTER_DL,
441};
442
Maxc59ef122017-11-27 13:21:41 +0100443static inline char *test_mode_descr(enum test_mode t)
444{
445 switch (t) {
446 case TEST_MODE_UL_ONLY: return (char*)"UL only";
447 case TEST_MODE_DL_ONLY: return (char*)"DL only";
448 case TEST_MODE_UL_AND_DL: return (char*)"UL and DL";
449 case TEST_MODE_DL_AND_UL: return (char*)"DL and UL";
450 case TEST_MODE_DL_AFTER_UL: return (char*)"DL after UL";
451 case TEST_MODE_UL_AFTER_DL: return (char*)"UL after DL";
452 default: return NULL;
453 }
454}
455
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100456static GprsMs *alloc_tbfs(struct gprs_rlcmac_bts *bts, struct GprsMs *old_ms, enum test_mode mode)
Jacob Erlbecke5655642015-06-29 12:19:52 +0200457{
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100458 struct GprsMs *ms, *new_ms;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200459 uint8_t trx_no = -1;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200460
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100461 OSMO_ASSERT(old_ms != NULL);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200462
Jacob Erlbecke5655642015-06-29 12:19:52 +0200463 gprs_rlcmac_tbf *tbf = NULL;
464
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100465 if (ms_current_trx(old_ms))
466 trx_no = ms_current_trx(old_ms)->trx_no;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200467
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100468 ms_ref(old_ms);
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200469
Jacob Erlbecke5655642015-06-29 12:19:52 +0200470 /* Allocate what is needed first */
471 switch (mode) {
472 case TEST_MODE_UL_ONLY:
473 case TEST_MODE_DL_AFTER_UL:
474 case TEST_MODE_UL_AND_DL:
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100475 if (ms_ul_tbf(old_ms))
476 tbf_free(ms_ul_tbf(old_ms));
Pau Espin Pedrolbda7bb72022-10-31 14:33:09 +0100477 tbf = ul_tbf_alloc(bts, old_ms, trx_no, false);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100478 if (tbf == NULL) {
Pau Espin Pedrol0dcbc072021-11-10 19:09:10 +0100479 OSMO_ASSERT(trx_no != -1 || bts_all_pdch_allocated(bts));
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100480 ms_unref(old_ms);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200481 return NULL;
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100482 }
Jacob Erlbecke5655642015-06-29 12:19:52 +0200483 break;
484 case TEST_MODE_DL_ONLY:
485 case TEST_MODE_UL_AFTER_DL:
486 case TEST_MODE_DL_AND_UL:
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100487 if (ms_dl_tbf(old_ms))
488 tbf_free(ms_dl_tbf(old_ms));
Pau Espin Pedrol87573842022-10-26 20:23:45 +0200489 tbf = dl_tbf_alloc(bts, old_ms, trx_no, false);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100490 if (tbf == NULL) {
Pau Espin Pedrol0dcbc072021-11-10 19:09:10 +0100491 OSMO_ASSERT(trx_no != -1 || bts_all_pdch_allocated(bts));
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100492 ms_unref(old_ms);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200493 return NULL;
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100494 }
Jacob Erlbecke5655642015-06-29 12:19:52 +0200495 }
496
497 OSMO_ASSERT(tbf);
498 OSMO_ASSERT(tbf->ms());
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100499 OSMO_ASSERT(old_ms == tbf->ms());
Jacob Erlbecke5655642015-06-29 12:19:52 +0200500 ms = tbf->ms();
501
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100502 ms_ref(ms);
503 new_ms = ms;
Jacob Erlbeck14376a72015-07-07 11:31:28 +0200504 /* Continue with what is needed next */
505 switch (mode) {
506 case TEST_MODE_UL_ONLY:
507 case TEST_MODE_DL_ONLY:
508 /* We are done */
509 break;
510
511 case TEST_MODE_DL_AFTER_UL:
512 case TEST_MODE_UL_AND_DL:
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100513 new_ms = alloc_tbfs(bts, ms, TEST_MODE_DL_ONLY);
Jacob Erlbeck14376a72015-07-07 11:31:28 +0200514 break;
515
516 case TEST_MODE_UL_AFTER_DL:
517 case TEST_MODE_DL_AND_UL:
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100518 new_ms = alloc_tbfs(bts, ms, TEST_MODE_UL_ONLY);
Jacob Erlbeck14376a72015-07-07 11:31:28 +0200519 break;
520 }
521
Jacob Erlbecke5655642015-06-29 12:19:52 +0200522 /* Optionally delete the TBF */
523 switch (mode) {
524 case TEST_MODE_DL_AFTER_UL:
525 case TEST_MODE_UL_AFTER_DL:
526 tbf_free(tbf);
Jacob Erlbeck0f352a62015-07-16 18:23:33 +0200527 tbf = NULL;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200528 break;
529
530 default:
531 break;
532 }
533
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100534 if (!new_ms && tbf)
Jacob Erlbeck0f352a62015-07-16 18:23:33 +0200535 tbf_free(tbf);
536
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100537 ms_unref(old_ms);
538 ms_unref(ms);
539 return new_ms;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200540}
541
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100542static unsigned alloc_many_tbfs(struct gprs_rlcmac_bts *bts, unsigned min_class,
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200543 unsigned max_class, enum test_mode mode)
Jacob Erlbecke5655642015-06-29 12:19:52 +0200544{
Jacob Erlbecke5655642015-06-29 12:19:52 +0200545 unsigned counter;
546 unsigned ms_class = min_class;
547
Jacob Erlbecke5655642015-06-29 12:19:52 +0200548 for (counter = 0; 1; counter += 1) {
549 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
550 uint8_t ul_slots = 0;
551 uint8_t dl_slots = 0;
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200552 uint8_t busy_slots = 0;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200553 unsigned i;
554 int tfi = -1;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200555 int tfi2;
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200556 uint8_t trx_no2;
557 struct gprs_rlcmac_trx *trx;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200558 GprsMs *ms;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200559 enum gprs_rlcmac_tbf_direction dir;
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200560 uint32_t tlli = counter + 0xc0000000;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200561
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100562 ms = bts_ms_by_tlli(bts, tlli, GSM_RESERVED_TMSI);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200563 if (!ms)
Pau Espin Pedrolbfc97562023-04-17 14:49:29 +0200564 ms = bts_alloc_ms(bts);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100565 ms_set_ms_class(ms, ms_class);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100566 ms = alloc_tbfs(bts, ms, mode);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200567 if (!ms)
568 break;
569
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100570 ms_set_tlli(ms, tlli);
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200571
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100572 ul_tbf = ms_ul_tbf(ms);
573 dl_tbf = ms_dl_tbf(ms);
574 trx = ms_current_trx(ms);
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200575
576 OSMO_ASSERT(ul_tbf || dl_tbf);
Pau Espin Pedrol9935d0d2022-12-13 18:29:25 +0100577 OSMO_ASSERT(ms_first_common_ts(ms) != NULL);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200578 if (ul_tbf) {
Pau Espin Pedrol9935d0d2022-12-13 18:29:25 +0100579 ul_slots = 1 << (uint8_t)ms_first_common_ts(ms)->ts_no;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200580 tfi = ul_tbf->tfi();
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200581 dir = GPRS_RLCMAC_UL_TBF;
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200582 } else {
Pau Espin Pedrol9935d0d2022-12-13 18:29:25 +0100583 ul_slots = 1 << (uint8_t)ms_first_common_ts(ms)->ts_no;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200584 tfi = dl_tbf->tfi();
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200585 dir = GPRS_RLCMAC_DL_TBF;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200586 }
587
588 for (i = 0; dl_tbf && i < ARRAY_SIZE(dl_tbf->pdch); i += 1)
589 if (dl_tbf->pdch[i])
590 dl_slots |= 1 << i;
591
Max5b0df1f2017-09-11 10:38:59 +0200592 for (i = 0; ul_tbf && i < ARRAY_SIZE(ul_tbf->pdch); i += 1)
593 if (ul_tbf->pdch[i])
594 ul_slots |= 1 << i;
595
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200596 for (i = 0; trx && i < ARRAY_SIZE(trx->pdch); i += 1) {
597 struct gprs_rlcmac_pdch *pdch = &trx->pdch[i];
598
599 if (ul_tbf && dl_tbf)
600 continue;
601
602 if (ul_tbf &&
Maxd000d802017-09-20 17:55:28 +0200603 pdch->assigned_tfi(GPRS_RLCMAC_DL_TBF) != NO_FREE_TFI)
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200604 continue;
605
606 if (dl_tbf &&
Maxd000d802017-09-20 17:55:28 +0200607 pdch->assigned_tfi(GPRS_RLCMAC_UL_TBF) != NO_FREE_TFI)
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200608 continue;
609
610 busy_slots |= 1 << i;
611 }
612
Maxd000d802017-09-20 17:55:28 +0200613 printf(" TBF[%d] class %d reserves " OSMO_BIT_SPEC "\n",
Jacob Erlbecke5655642015-06-29 12:19:52 +0200614 tfi, ms_class,
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200615 get_dir_char(0x01, ul_slots, dl_slots, busy_slots),
616 get_dir_char(0x02, ul_slots, dl_slots, busy_slots),
617 get_dir_char(0x04, ul_slots, dl_slots, busy_slots),
618 get_dir_char(0x08, ul_slots, dl_slots, busy_slots),
619 get_dir_char(0x10, ul_slots, dl_slots, busy_slots),
620 get_dir_char(0x20, ul_slots, dl_slots, busy_slots),
621 get_dir_char(0x40, ul_slots, dl_slots, busy_slots),
622 get_dir_char(0x80, ul_slots, dl_slots, busy_slots));
Jacob Erlbecke5655642015-06-29 12:19:52 +0200623
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200624 if (tfi >= 0) {
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100625 OSMO_ASSERT(ms_current_trx(ms));
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100626 tfi2 = bts_tfi_find_free(bts, dir, &trx_no2,
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100627 ms_current_trx(ms)->trx_no);
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200628 OSMO_ASSERT(tfi != tfi2);
Jacob Erlbeck7b3675b2015-07-16 18:28:22 +0200629 OSMO_ASSERT(tfi2 < 0 ||
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100630 trx_no2 == ms_current_trx(ms)->trx_no);
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200631 }
632
Jacob Erlbecke5655642015-06-29 12:19:52 +0200633 ms_class += 1;
634 if (ms_class > max_class)
635 ms_class = min_class;
636 }
637
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200638 return counter;
639}
640
Pau Espin Pedrolc85e0932021-02-25 18:08:10 +0100641static void test_successive_allocation(alloc_algorithm_func_t algo, unsigned min_class,
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200642 unsigned max_class, enum test_mode mode,
643 unsigned expect_num, const char *text)
644{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100645 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200646 struct gprs_rlcmac_trx *trx;
647 unsigned counter;
648
Maxc59ef122017-11-27 13:21:41 +0100649 printf("Going to test assignment with many TBF, algorithm %s class %u..%u (%s)\n",
650 text, min_class, max_class, test_mode_descr(mode));
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200651
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100652 the_pcu->alloc_algorithm = algo;
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200653
654 trx = &bts->trx[0];
655 trx->pdch[3].enable();
656 trx->pdch[4].enable();
657 trx->pdch[5].enable();
658 trx->pdch[6].enable();
659 trx->pdch[7].enable();
660
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100661 counter = alloc_many_tbfs(bts, min_class, max_class, mode);
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200662
Maxc59ef122017-11-27 13:21:41 +0100663 printf(" Successfully allocated %u UL TBFs, algorithm %s class %u..%u (%s)\n",
664 counter, text, min_class, max_class, test_mode_descr(mode));
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200665 if (counter != expect_num)
Maxc59ef122017-11-27 13:21:41 +0100666 fprintf(stderr, " Expected %u TBFs (got %u), algorithm %s class %u..%u (%s)\n",
667 expect_num, counter, text, min_class, max_class, test_mode_descr(mode));
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200668
Jacob Erlbeckec478752015-06-19 16:35:38 +0200669 OSMO_ASSERT(counter == expect_num);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200670
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100671 check_tfi_usage(bts);
672 talloc_free(bts);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200673}
674
Pau Espin Pedrolc85e0932021-02-25 18:08:10 +0100675static void test_many_connections(alloc_algorithm_func_t algo, unsigned expect_num,
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200676 const char *text)
677{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100678 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200679 struct gprs_rlcmac_trx *trx;
680 int counter1, counter2 = -1;
681 unsigned i;
682 enum test_mode mode_seq[] = {
683 TEST_MODE_DL_AFTER_UL,
684 TEST_MODE_UL_ONLY,
685 TEST_MODE_DL_AFTER_UL,
686 TEST_MODE_DL_ONLY,
687 };
688
Maxc59ef122017-11-27 13:21:41 +0100689 printf("Going to test assignment with many connections, algorithm %s\n", text);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200690
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100691 the_pcu->alloc_algorithm = algo;
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200692
693 trx = &bts->trx[0];
694 trx->pdch[3].enable();
695 trx->pdch[4].enable();
696 trx->pdch[5].enable();
697 trx->pdch[6].enable();
698 trx->pdch[7].enable();
699
700 for (i = 0; i < ARRAY_SIZE(mode_seq); i += 1) {
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100701 counter1 = alloc_many_tbfs(bts, 1, mslot_class_max(), mode_seq[i]);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200702 fprintf(stderr, " Allocated %d TBFs (previously %d)\n",
703 counter1, counter2);
704
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100705 check_tfi_usage(bts);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200706
707 /* This will stop earlier due to USF shortage */
708 if (mode_seq[i] == TEST_MODE_UL_ONLY)
709 continue;
710
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200711 if (counter2 >= 0) {
712 if (counter1 < counter2)
713 fprintf(stderr, " Expected %d >= %d in %s\n",
714 counter1, counter2, text);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200715 OSMO_ASSERT(counter1 >= counter2);
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200716 }
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200717
718 counter2 = counter1;
719 }
720
721 printf(" Successfully allocated %d TBFs\n", counter1);
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200722 if (counter1 != (int)expect_num)
Maxc59ef122017-11-27 13:21:41 +0100723 fprintf(stderr, " Expected %d TBFs (got %d) for algorithm %s\n", expect_num, counter1, text);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200724
725 OSMO_ASSERT(expect_num == (unsigned)counter1);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100726 talloc_free(bts);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200727}
728
Maxc59ef122017-11-27 13:21:41 +0100729static 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 +0200730{
Maxc59ef122017-11-27 13:21:41 +0100731 test_successive_allocation(alloc_algorithm_a, 1, 1, mode, exp_A, "A");
732 test_successive_allocation(alloc_algorithm_b, 10, 10, mode, exp_B, "B");
733 test_successive_allocation(alloc_algorithm_dynamic, 10, 10, mode, exp_dyn, "dynamic");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200734}
735
Maxc59ef122017-11-27 13:21:41 +0100736static void test_successive_allocations()
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200737{
Maxc59ef122017-11-27 13:21:41 +0100738 test_successive_allocation(alloc_algorithm_a, 1, 1, TEST_MODE_UL_AND_DL, 35, "A");
Pau Espin Pedroled2afa32021-02-22 17:20:15 +0100739 test_successive_allocation(alloc_algorithm_b, 10, 10, TEST_MODE_UL_AND_DL, 15, "B");
740 test_successive_allocation(alloc_algorithm_b, 12, 12, TEST_MODE_UL_AND_DL, 15, "B");
Maxc59ef122017-11-27 13:21:41 +0100741
Pau Espin Pedroled2afa32021-02-22 17:20:15 +0100742 test_successive_allocation(alloc_algorithm_b, 1, 12, TEST_MODE_UL_AND_DL, 23, "B");
743 test_successive_allocation(alloc_algorithm_b, 1, mslot_class_max(), TEST_MODE_UL_AND_DL, 17, "B");
744 test_successive_allocation(alloc_algorithm_dynamic, 1, mslot_class_max(), TEST_MODE_UL_AND_DL, 17, "dynamic");
Maxc59ef122017-11-27 13:21:41 +0100745
Pau Espin Pedroled2afa32021-02-22 17:20:15 +0100746 test_a_b_dyn(TEST_MODE_DL_AND_UL, 35, 15, 15);
747 test_a_b_dyn(TEST_MODE_DL_AFTER_UL, 160, 32, 101);
748 test_a_b_dyn(TEST_MODE_UL_AFTER_DL, 35, 15, 15);
749 test_a_b_dyn(TEST_MODE_UL_ONLY, 35, 15, 21);
Maxc59ef122017-11-27 13:21:41 +0100750 test_a_b_dyn(TEST_MODE_DL_ONLY, 160, 32, 101);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200751}
752
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530753static void test_2_consecutive_dl_tbfs()
754{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100755 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200756 GprsMs *ms;
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530757 struct gprs_rlcmac_trx *trx;
758 uint8_t ms_class = 11;
759 uint8_t egprs_ms_class = 11;
760 gprs_rlcmac_tbf *dl_tbf1, *dl_tbf2;
761 uint8_t numTs1 = 0, numTs2 = 0;
762
763 printf("Testing DL TS allocation for Multi UEs\n");
764
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100765 the_pcu->alloc_algorithm = alloc_algorithm_b;
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530766
767 trx = &bts->trx[0];
768 trx->pdch[4].enable();
769 trx->pdch[5].enable();
770 trx->pdch[6].enable();
771 trx->pdch[7].enable();
772
Pau Espin Pedrolbfc97562023-04-17 14:49:29 +0200773 ms = bts_alloc_ms(bts);
774 ms_set_ms_class(ms, ms_class);
775 ms_set_egprs_ms_class(ms, egprs_ms_class);
Pau Espin Pedrol87573842022-10-26 20:23:45 +0200776 dl_tbf1 = dl_tbf_alloc(bts, ms, 0, false);
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530777 OSMO_ASSERT(dl_tbf1);
778
779 for (int i = 0; i < 8; i++) {
780 if (dl_tbf1->pdch[i])
781 numTs1++;
782 }
783 OSMO_ASSERT(numTs1 == 4);
784 printf("TBF1: numTs(%d)\n", numTs1);
785
Pau Espin Pedrolbfc97562023-04-17 14:49:29 +0200786 ms = bts_alloc_ms(bts);
787 ms_set_ms_class(ms, ms_class);
788 ms_set_egprs_ms_class(ms, egprs_ms_class);
Pau Espin Pedrol87573842022-10-26 20:23:45 +0200789 dl_tbf2 = dl_tbf_alloc(bts, ms, 0, false);
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530790 OSMO_ASSERT(dl_tbf2);
791
792 for (int i = 0; i < 8; i++) {
793 if (dl_tbf2->pdch[i])
794 numTs2++;
795 }
796
797 /*
798 * TODO: currently 2nd DL TBF gets 3 TS
799 * This behaviour will be fixed in subsequent patch
800 */
801 printf("TBF2: numTs(%d)\n", numTs2);
802 OSMO_ASSERT(numTs2 == 3);
803
804 tbf_free(dl_tbf1);
805 tbf_free(dl_tbf2);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100806 talloc_free(bts);
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530807}
808
Oliver Smith3f794702021-08-23 14:19:21 +0200809static void test_bts_pch_timer(void)
810{
811 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100812 struct osmo_mobile_identity mi_imsi1, mi_imsi2;
Pau Espin Pedrol19b33922021-11-08 17:49:52 +0100813 struct osmo_mobile_identity mi_tmsi1;
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100814 mi_imsi1.type = mi_imsi2.type = GSM_MI_TYPE_IMSI;
Pau Espin Pedrol19b33922021-11-08 17:49:52 +0100815 mi_tmsi1.type = GSM_MI_TYPE_TMSI;
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100816 OSMO_STRLCPY_ARRAY(mi_imsi1.imsi, "1234");
817 OSMO_STRLCPY_ARRAY(mi_imsi2.imsi, "5678");
Pau Espin Pedrol19b33922021-11-08 17:49:52 +0100818 mi_tmsi1.tmsi = 987654321;
Oliver Smith3f794702021-08-23 14:19:21 +0200819
820 fprintf(stderr, "Testing bts_pch_timer dealloc on bts dealloc\n");
821 log_set_category_filter(osmo_stderr_target, DPCU, 1, LOGL_DEBUG);
822
823 fprintf(stderr, "Starting PCH timer for 2 IMSI\n");
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100824 bts_pch_timer_start(bts, &mi_imsi1, mi_imsi1.imsi);
825 bts_pch_timer_start(bts, &mi_imsi2, mi_imsi2.imsi);
Pau Espin Pedrol19b33922021-11-08 17:49:52 +0100826 fprintf(stderr, "Starting PCH timer for 1 TMSI\n");
827 bts_pch_timer_start(bts, &mi_tmsi1, "6666");
Oliver Smith3f794702021-08-23 14:19:21 +0200828
829 fprintf(stderr, "Deallocating BTS, expecting the PCH timer to be stopped and deallocated\n");
830 talloc_free(bts);
831}
832
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200833int main(int argc, char **argv)
834{
835 tall_pcu_ctx = talloc_named_const(NULL, 1, "moiji-mobile AllocTest context");
836 if (!tall_pcu_ctx)
837 abort();
838
Neels Hofmeyr78ce5912017-02-08 17:07:31 +0100839 msgb_talloc_ctx_init(tall_pcu_ctx, 0);
Neels Hofmeyr42f2d612018-04-01 16:54:40 +0200840 osmo_init_logging2(tall_pcu_ctx, &gprs_log_info);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200841 log_set_use_color(osmo_stderr_target, 0);
Pau Espin Pedrol00f52cc2021-02-19 14:01:52 +0100842 log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE);
Pau Espin Pedrolb18d2a52021-02-19 14:00:48 +0100843 log_set_print_category(osmo_stderr_target, 0);
844 log_set_print_category_hex(osmo_stderr_target, 0);
Philipp Maierde0e5582020-03-25 12:23:52 +0100845 log_set_category_filter(osmo_stderr_target, DTBF, 1, LOGL_INFO);
Jacob Erlbeck9ec49e22015-06-29 13:00:20 +0200846 if (getenv("LOGL_DEBUG"))
847 log_set_log_level(osmo_stderr_target, LOGL_DEBUG);
Pau Espin Pedroldc2aaac2021-05-14 12:50:46 +0200848 osmo_fsm_log_addr(false);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200849
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100850 the_pcu = gprs_pcu_alloc(tall_pcu_ctx);
851
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200852 test_alloc_a();
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100853 test_alloc_b();
Maxc59ef122017-11-27 13:21:41 +0100854 test_successive_allocations();
855 test_many_connections(alloc_algorithm_a, 160, "A");
Max01bd0cc2018-01-23 20:58:49 +0100856 test_many_connections(alloc_algorithm_b, 32, "B");
Maxc59ef122017-11-27 13:21:41 +0100857 test_many_connections(alloc_algorithm_dynamic, 160, "dynamic");
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530858 test_2_consecutive_dl_tbfs();
Oliver Smith3f794702021-08-23 14:19:21 +0200859 test_bts_pch_timer();
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100860
861 talloc_free(the_pcu);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200862 return EXIT_SUCCESS;
863}
864
865/*
866 * stubs that should not be reached
867 */
868extern "C" {
869void l1if_pdch_req() { abort(); }
870void l1if_connect_pdch() { abort(); }
Philipp Maier72ed3332023-02-27 15:32:00 +0100871void l1if_disconnect_pdch() { abort(); }
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200872void l1if_close_pdch() { abort(); }
873void l1if_open_pdch() { abort(); }
874}