blob: 08f5910a4fa42983f6377bef5ccbaaabbd0a1c75 [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 Pedrol2182e622021-01-14 16:48:38 +0100138 ms = bts_alloc_ms(bts, 0, 0);
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 Pedrol2182e622021-01-14 16:48:38 +0100158 ms = bts_alloc_ms(bts, 0, 0);
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 Pedrol2182e622021-01-14 16:48:38 +0100224 ms = bts_alloc_ms(bts, ms_class, 0);
Pau Espin Pedrol1e009472021-01-11 20:40:19 +0100225 /* Avoid delaying free to avoid tons of to-be-freed ms objects queuing */
226 ms_set_timeout(ms, 0);
Pau Espin Pedrolbda7bb72022-10-31 14:33:09 +0100227 ul_tbf = ul_tbf_alloc(bts, ms, -1, true);
Max2ecf0fd2017-11-21 18:13:31 +0100228 if (!ul_tbf)
229 return false;
230
231 OSMO_ASSERT(ul_tbf->ms());
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100232 OSMO_ASSERT(ms_current_trx(ul_tbf->ms()));
Max2ecf0fd2017-11-21 18:13:31 +0100233
234 dump_assignment(ul_tbf, "UL", verbose);
235
236 /* assume final ack has not been sent */
Pau Espin Pedrol87573842022-10-26 20:23:45 +0200237 dl_tbf = dl_tbf_alloc(bts, ms, ms_current_trx(ms)->trx_no, false);
Max2ecf0fd2017-11-21 18:13:31 +0100238 if (!dl_tbf)
239 return false;
240
241 dump_assignment(dl_tbf, "DL", verbose);
242
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100243 check_tfi_usage(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100244
245 tbf_free(dl_tbf);
246 tbf_free(ul_tbf);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100247 talloc_free(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100248 return true;
249}
250
251static inline bool test_alloc_b_dl_ul(bool ts0, bool ts1, bool ts2, bool ts3, bool ts4, bool ts5, bool ts6, bool ts7,
252 uint8_t ms_class, bool verbose)
253{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100254 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200255 GprsMs *ms;
Max2ecf0fd2017-11-21 18:13:31 +0100256 gprs_rlcmac_ul_tbf *ul_tbf;
257 gprs_rlcmac_dl_tbf *dl_tbf;
258
259 if (verbose)
260 printf("Testing DL then UL assignment followed by update\n");
261
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100262 the_pcu->alloc_algorithm = alloc_algorithm_b;
Max2ecf0fd2017-11-21 18:13:31 +0100263
264 enable_ts_on_bts(bts, ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7);
265
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100266 ms = bts_alloc_ms(bts, ms_class, 0);
Pau Espin Pedrol1e009472021-01-11 20:40:19 +0100267 /* Avoid delaying free to avoid tons of to-be-freed ms objects queuing */
268 ms_set_timeout(ms, 0);
Pau Espin Pedrol87573842022-10-26 20:23:45 +0200269 dl_tbf = dl_tbf_alloc(bts, ms, -1, true);
Max2ecf0fd2017-11-21 18:13:31 +0100270 if (!dl_tbf)
271 return false;
272
Pau Espin Pedrol8abe13c2022-10-21 18:49:48 +0200273 ms_confirm_tlli(ms, 0x23);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200274 OSMO_ASSERT(dl_tbf->ms() == ms);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100275 OSMO_ASSERT(ms_current_trx(dl_tbf->ms()));
Max2ecf0fd2017-11-21 18:13:31 +0100276
277 dump_assignment(dl_tbf, "DL", verbose);
278
Pau Espin Pedrolbda7bb72022-10-31 14:33:09 +0100279 ul_tbf = ul_tbf_alloc(bts, ms, ms_current_trx(ms)->trx_no, false);
Max2ecf0fd2017-11-21 18:13:31 +0100280 if (!ul_tbf)
281 return false;
282
Pau Espin Pedrol8abe13c2022-10-21 18:49:48 +0200283 ms_update_announced_tlli(ms, 0x23);
Pau Espin Pedrol0f859562022-10-31 10:51:20 +0100284 ul_tbf->m_contention_resolution_done = true;
Max2ecf0fd2017-11-21 18:13:31 +0100285
286 dump_assignment(ul_tbf, "UL", verbose);
287
Pau Espin Pedrolb7a2b592022-12-13 14:43:59 +0100288 /* now upgrade the dl_tbf */
289 OSMO_ASSERT(dl_tbf_upgrade_to_multislot(dl_tbf) == 0);
Max2ecf0fd2017-11-21 18:13:31 +0100290 dump_assignment(dl_tbf, "DL", verbose);
Max2ecf0fd2017-11-21 18:13:31 +0100291
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100292 check_tfi_usage(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100293
294 tbf_free(dl_tbf);
295 tbf_free(ul_tbf);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100296 talloc_free(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100297 return true;
298}
299
300static inline bool test_alloc_b_jolly(uint8_t ms_class)
301{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100302 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200303 GprsMs *ms;
Max2ecf0fd2017-11-21 18:13:31 +0100304 int tfi;
305 uint8_t trx_no;
306 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
307
308 printf("Testing jolly example\n");
309
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100310 the_pcu->alloc_algorithm = alloc_algorithm_b;
Max2ecf0fd2017-11-21 18:13:31 +0100311
312 enable_ts_on_bts(bts, false, true, true, true, true, false, false, false);
313
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100314 tfi = bts_tfi_find_free(bts, GPRS_RLCMAC_UL_TBF, &trx_no, -1);
Max2ecf0fd2017-11-21 18:13:31 +0100315 OSMO_ASSERT(tfi >= 0);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100316 ms = bts_alloc_ms(bts, ms_class, 0);
Pau Espin Pedrol1e009472021-01-11 20:40:19 +0100317 /* Avoid delaying free to avoid tons of to-be-freed ms objects queuing */
318 ms_set_timeout(ms, 0);
Pau Espin Pedrolbda7bb72022-10-31 14:33:09 +0100319 ul_tbf = ul_tbf_alloc(bts, ms, -1, false);
Max2ecf0fd2017-11-21 18:13:31 +0100320 if (!ul_tbf)
321 return false;
322
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200323 OSMO_ASSERT(ul_tbf->ms() == ms);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100324 OSMO_ASSERT(ms_current_trx(ul_tbf->ms()));
325 trx_no = ms_current_trx(ms)->trx_no;
Max2ecf0fd2017-11-21 18:13:31 +0100326 dump_assignment(ul_tbf, "UL", true);
327
328 /* assume final ack has not been sent */
Pau Espin Pedrol87573842022-10-26 20:23:45 +0200329 dl_tbf = dl_tbf_alloc(bts, ms, trx_no, false);
Max2ecf0fd2017-11-21 18:13:31 +0100330 if (!dl_tbf)
331 return false;
332
333 dump_assignment(dl_tbf, "DL", true);
334
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100335 check_tfi_usage(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100336
337 tbf_free(dl_tbf);
338 tbf_free(ul_tbf);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100339 talloc_free(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100340 return true;
341}
342
343static void test_alloc_b_for_ms(uint8_t ms_class)
344{
345 bool rc;
346
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100347 printf("Going to test multislot assignment MS_CLASS=%d\n", ms_class);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100348 /*
349 * PDCH is on TS 6,7,8 and we start with a UL allocation and
350 * then follow two DL allocations (once single, once normal).
351 *
352 * Uplink assigned and still available..
353 */
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100354
Max2ecf0fd2017-11-21 18:13:31 +0100355 rc = test_alloc_b_ul_dl(false, false, false, false, false, true, true, true, ms_class, true);
356 if (!rc)
357 return;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100358
359 /**
360 * Test with the other order.. first DL and then UL
361 */
Max2ecf0fd2017-11-21 18:13:31 +0100362 rc = test_alloc_b_dl_ul(false, false, false, false, false, true, true, true, ms_class, true);
363 if (!rc)
364 return;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100365
366 /* Andreas osmocom-pcu example */
Max2ecf0fd2017-11-21 18:13:31 +0100367 test_alloc_b_jolly(ms_class);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100368}
369
Max2ecf0fd2017-11-21 18:13:31 +0100370static 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 +0100371{
Max2ecf0fd2017-11-21 18:13:31 +0100372 bool rc;
373
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100374 /* we can test the allocation failures differently */
375 if (!ts0 && !ts1 && !ts2 && !ts3 && !ts4 && !ts5 && !ts6 && !ts7)
376 return;
377
378 printf("Mass test: TS0(%c%c%c%c%c%c%c%c)TS7 MS_Class=%d\n",
379 ts0 ? 'O' : 'x',
380 ts1 ? 'O' : 'x',
381 ts2 ? 'O' : 'x',
382 ts3 ? 'O' : 'x',
383 ts4 ? 'O' : 'x',
384 ts5 ? 'O' : 'x',
385 ts6 ? 'O' : 'x',
386 ts7 ? 'O' : 'x', ms_class);
387 fflush(stdout);
388
Max2ecf0fd2017-11-21 18:13:31 +0100389 rc = test_alloc_b_ul_dl(ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7, ms_class, false);
390 if (!rc)
391 return;
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100392
393 /**
394 * Test with the other order.. first DL and then UL
395 */
Max2ecf0fd2017-11-21 18:13:31 +0100396 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 +0100397}
398
399static void test_all_alloc_b()
400{
401 /* it is a bit crazy... */
402 for (uint8_t ts0 = 0; ts0 < 2; ++ts0)
403 for (uint8_t ts1 = 0; ts1 < 2; ++ts1)
404 for (uint8_t ts2 = 0; ts2 < 2; ++ts2)
405 for (uint8_t ts3 = 0; ts3 < 2; ++ts3)
406 for (uint8_t ts4 = 0; ts4 < 2; ++ts4)
407 for (uint8_t ts5 = 0; ts5 < 2; ++ts5)
408 for (uint8_t ts6 = 0; ts6 < 2; ++ts6)
409 for (uint8_t ts7 = 0; ts7 < 2; ++ts7)
Max9f460712018-01-23 20:57:08 +0100410 for (int ms_class = 0; ms_class < mslot_class_max(); ++ms_class)
Max2ecf0fd2017-11-21 18:13:31 +0100411 test_alloc_mass(ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7, ms_class);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100412}
413
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100414static void test_alloc_b()
415{
Max9f460712018-01-23 20:57:08 +0100416 for (int i = 0; i < mslot_class_max(); ++i)
Max2ecf0fd2017-11-21 18:13:31 +0100417 test_alloc_b_for_ms(i);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100418
419 test_all_alloc_b();
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100420}
421
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200422static char get_dir_char(uint8_t mask, uint8_t tx, uint8_t rx, uint8_t busy)
Jacob Erlbecke5655642015-06-29 12:19:52 +0200423{
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200424 int offs = busy ? 32 : 0;
425 return (mask & tx & rx) ? 'C' + offs :
426 (mask & tx) ? 'U' + offs :
427 (mask & rx) ? 'D' + offs :
Jacob Erlbecke5655642015-06-29 12:19:52 +0200428 '.';
429}
430
431enum test_mode {
432 TEST_MODE_UL_ONLY,
433 TEST_MODE_DL_ONLY,
434 TEST_MODE_UL_AND_DL,
435 TEST_MODE_DL_AND_UL,
436 TEST_MODE_DL_AFTER_UL,
437 TEST_MODE_UL_AFTER_DL,
438};
439
Maxc59ef122017-11-27 13:21:41 +0100440static inline char *test_mode_descr(enum test_mode t)
441{
442 switch (t) {
443 case TEST_MODE_UL_ONLY: return (char*)"UL only";
444 case TEST_MODE_DL_ONLY: return (char*)"DL only";
445 case TEST_MODE_UL_AND_DL: return (char*)"UL and DL";
446 case TEST_MODE_DL_AND_UL: return (char*)"DL and UL";
447 case TEST_MODE_DL_AFTER_UL: return (char*)"DL after UL";
448 case TEST_MODE_UL_AFTER_DL: return (char*)"UL after DL";
449 default: return NULL;
450 }
451}
452
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100453static GprsMs *alloc_tbfs(struct gprs_rlcmac_bts *bts, struct GprsMs *old_ms, enum test_mode mode)
Jacob Erlbecke5655642015-06-29 12:19:52 +0200454{
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100455 struct GprsMs *ms, *new_ms;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200456 uint8_t trx_no = -1;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200457
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100458 OSMO_ASSERT(old_ms != NULL);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200459
Jacob Erlbecke5655642015-06-29 12:19:52 +0200460 gprs_rlcmac_tbf *tbf = NULL;
461
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100462 if (ms_current_trx(old_ms))
463 trx_no = ms_current_trx(old_ms)->trx_no;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200464
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100465 ms_ref(old_ms);
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200466
Jacob Erlbecke5655642015-06-29 12:19:52 +0200467 /* Allocate what is needed first */
468 switch (mode) {
469 case TEST_MODE_UL_ONLY:
470 case TEST_MODE_DL_AFTER_UL:
471 case TEST_MODE_UL_AND_DL:
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100472 if (ms_ul_tbf(old_ms))
473 tbf_free(ms_ul_tbf(old_ms));
Pau Espin Pedrolbda7bb72022-10-31 14:33:09 +0100474 tbf = ul_tbf_alloc(bts, old_ms, trx_no, false);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100475 if (tbf == NULL) {
Pau Espin Pedrol0dcbc072021-11-10 19:09:10 +0100476 OSMO_ASSERT(trx_no != -1 || bts_all_pdch_allocated(bts));
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100477 ms_unref(old_ms);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200478 return NULL;
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100479 }
Jacob Erlbecke5655642015-06-29 12:19:52 +0200480 break;
481 case TEST_MODE_DL_ONLY:
482 case TEST_MODE_UL_AFTER_DL:
483 case TEST_MODE_DL_AND_UL:
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100484 if (ms_dl_tbf(old_ms))
485 tbf_free(ms_dl_tbf(old_ms));
Pau Espin Pedrol87573842022-10-26 20:23:45 +0200486 tbf = dl_tbf_alloc(bts, old_ms, trx_no, false);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100487 if (tbf == NULL) {
Pau Espin Pedrol0dcbc072021-11-10 19:09:10 +0100488 OSMO_ASSERT(trx_no != -1 || bts_all_pdch_allocated(bts));
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100489 ms_unref(old_ms);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200490 return NULL;
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100491 }
Jacob Erlbecke5655642015-06-29 12:19:52 +0200492 }
493
494 OSMO_ASSERT(tbf);
495 OSMO_ASSERT(tbf->ms());
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100496 OSMO_ASSERT(old_ms == tbf->ms());
Jacob Erlbecke5655642015-06-29 12:19:52 +0200497 ms = tbf->ms();
498
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100499 ms_ref(ms);
500 new_ms = ms;
Jacob Erlbeck14376a72015-07-07 11:31:28 +0200501 /* Continue with what is needed next */
502 switch (mode) {
503 case TEST_MODE_UL_ONLY:
504 case TEST_MODE_DL_ONLY:
505 /* We are done */
506 break;
507
508 case TEST_MODE_DL_AFTER_UL:
509 case TEST_MODE_UL_AND_DL:
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100510 new_ms = alloc_tbfs(bts, ms, TEST_MODE_DL_ONLY);
Jacob Erlbeck14376a72015-07-07 11:31:28 +0200511 break;
512
513 case TEST_MODE_UL_AFTER_DL:
514 case TEST_MODE_DL_AND_UL:
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100515 new_ms = alloc_tbfs(bts, ms, TEST_MODE_UL_ONLY);
Jacob Erlbeck14376a72015-07-07 11:31:28 +0200516 break;
517 }
518
Jacob Erlbecke5655642015-06-29 12:19:52 +0200519 /* Optionally delete the TBF */
520 switch (mode) {
521 case TEST_MODE_DL_AFTER_UL:
522 case TEST_MODE_UL_AFTER_DL:
523 tbf_free(tbf);
Jacob Erlbeck0f352a62015-07-16 18:23:33 +0200524 tbf = NULL;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200525 break;
526
527 default:
528 break;
529 }
530
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100531 if (!new_ms && tbf)
Jacob Erlbeck0f352a62015-07-16 18:23:33 +0200532 tbf_free(tbf);
533
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100534 ms_unref(old_ms);
535 ms_unref(ms);
536 return new_ms;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200537}
538
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100539static unsigned alloc_many_tbfs(struct gprs_rlcmac_bts *bts, unsigned min_class,
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200540 unsigned max_class, enum test_mode mode)
Jacob Erlbecke5655642015-06-29 12:19:52 +0200541{
Jacob Erlbecke5655642015-06-29 12:19:52 +0200542 unsigned counter;
543 unsigned ms_class = min_class;
544
Jacob Erlbecke5655642015-06-29 12:19:52 +0200545 for (counter = 0; 1; counter += 1) {
546 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
547 uint8_t ul_slots = 0;
548 uint8_t dl_slots = 0;
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200549 uint8_t busy_slots = 0;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200550 unsigned i;
551 int tfi = -1;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200552 int tfi2;
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200553 uint8_t trx_no2;
554 struct gprs_rlcmac_trx *trx;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200555 GprsMs *ms;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200556 enum gprs_rlcmac_tbf_direction dir;
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200557 uint32_t tlli = counter + 0xc0000000;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200558
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100559 ms = bts_ms_by_tlli(bts, tlli, GSM_RESERVED_TMSI);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200560 if (!ms)
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100561 ms = bts_alloc_ms(bts, 0, 0);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100562 ms_set_ms_class(ms, ms_class);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100563 ms = alloc_tbfs(bts, ms, mode);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200564 if (!ms)
565 break;
566
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100567 ms_set_tlli(ms, tlli);
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200568
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100569 ul_tbf = ms_ul_tbf(ms);
570 dl_tbf = ms_dl_tbf(ms);
571 trx = ms_current_trx(ms);
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200572
573 OSMO_ASSERT(ul_tbf || dl_tbf);
Pau Espin Pedrol9935d0d2022-12-13 18:29:25 +0100574 OSMO_ASSERT(ms_first_common_ts(ms) != NULL);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200575 if (ul_tbf) {
Pau Espin Pedrol9935d0d2022-12-13 18:29:25 +0100576 ul_slots = 1 << (uint8_t)ms_first_common_ts(ms)->ts_no;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200577 tfi = ul_tbf->tfi();
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200578 dir = GPRS_RLCMAC_UL_TBF;
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200579 } else {
Pau Espin Pedrol9935d0d2022-12-13 18:29:25 +0100580 ul_slots = 1 << (uint8_t)ms_first_common_ts(ms)->ts_no;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200581 tfi = dl_tbf->tfi();
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200582 dir = GPRS_RLCMAC_DL_TBF;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200583 }
584
585 for (i = 0; dl_tbf && i < ARRAY_SIZE(dl_tbf->pdch); i += 1)
586 if (dl_tbf->pdch[i])
587 dl_slots |= 1 << i;
588
Max5b0df1f2017-09-11 10:38:59 +0200589 for (i = 0; ul_tbf && i < ARRAY_SIZE(ul_tbf->pdch); i += 1)
590 if (ul_tbf->pdch[i])
591 ul_slots |= 1 << i;
592
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200593 for (i = 0; trx && i < ARRAY_SIZE(trx->pdch); i += 1) {
594 struct gprs_rlcmac_pdch *pdch = &trx->pdch[i];
595
596 if (ul_tbf && dl_tbf)
597 continue;
598
599 if (ul_tbf &&
Maxd000d802017-09-20 17:55:28 +0200600 pdch->assigned_tfi(GPRS_RLCMAC_DL_TBF) != NO_FREE_TFI)
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200601 continue;
602
603 if (dl_tbf &&
Maxd000d802017-09-20 17:55:28 +0200604 pdch->assigned_tfi(GPRS_RLCMAC_UL_TBF) != NO_FREE_TFI)
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200605 continue;
606
607 busy_slots |= 1 << i;
608 }
609
Maxd000d802017-09-20 17:55:28 +0200610 printf(" TBF[%d] class %d reserves " OSMO_BIT_SPEC "\n",
Jacob Erlbecke5655642015-06-29 12:19:52 +0200611 tfi, ms_class,
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200612 get_dir_char(0x01, ul_slots, dl_slots, busy_slots),
613 get_dir_char(0x02, ul_slots, dl_slots, busy_slots),
614 get_dir_char(0x04, ul_slots, dl_slots, busy_slots),
615 get_dir_char(0x08, ul_slots, dl_slots, busy_slots),
616 get_dir_char(0x10, ul_slots, dl_slots, busy_slots),
617 get_dir_char(0x20, ul_slots, dl_slots, busy_slots),
618 get_dir_char(0x40, ul_slots, dl_slots, busy_slots),
619 get_dir_char(0x80, ul_slots, dl_slots, busy_slots));
Jacob Erlbecke5655642015-06-29 12:19:52 +0200620
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200621 if (tfi >= 0) {
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100622 OSMO_ASSERT(ms_current_trx(ms));
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100623 tfi2 = bts_tfi_find_free(bts, dir, &trx_no2,
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100624 ms_current_trx(ms)->trx_no);
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200625 OSMO_ASSERT(tfi != tfi2);
Jacob Erlbeck7b3675b2015-07-16 18:28:22 +0200626 OSMO_ASSERT(tfi2 < 0 ||
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100627 trx_no2 == ms_current_trx(ms)->trx_no);
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200628 }
629
Jacob Erlbecke5655642015-06-29 12:19:52 +0200630 ms_class += 1;
631 if (ms_class > max_class)
632 ms_class = min_class;
633 }
634
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200635 return counter;
636}
637
Pau Espin Pedrolc85e0932021-02-25 18:08:10 +0100638static void test_successive_allocation(alloc_algorithm_func_t algo, unsigned min_class,
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200639 unsigned max_class, enum test_mode mode,
640 unsigned expect_num, const char *text)
641{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100642 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200643 struct gprs_rlcmac_trx *trx;
644 unsigned counter;
645
Maxc59ef122017-11-27 13:21:41 +0100646 printf("Going to test assignment with many TBF, algorithm %s class %u..%u (%s)\n",
647 text, min_class, max_class, test_mode_descr(mode));
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200648
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100649 the_pcu->alloc_algorithm = algo;
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200650
651 trx = &bts->trx[0];
652 trx->pdch[3].enable();
653 trx->pdch[4].enable();
654 trx->pdch[5].enable();
655 trx->pdch[6].enable();
656 trx->pdch[7].enable();
657
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100658 counter = alloc_many_tbfs(bts, min_class, max_class, mode);
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200659
Maxc59ef122017-11-27 13:21:41 +0100660 printf(" Successfully allocated %u UL TBFs, algorithm %s class %u..%u (%s)\n",
661 counter, text, min_class, max_class, test_mode_descr(mode));
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200662 if (counter != expect_num)
Maxc59ef122017-11-27 13:21:41 +0100663 fprintf(stderr, " Expected %u TBFs (got %u), algorithm %s class %u..%u (%s)\n",
664 expect_num, counter, text, min_class, max_class, test_mode_descr(mode));
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200665
Jacob Erlbeckec478752015-06-19 16:35:38 +0200666 OSMO_ASSERT(counter == expect_num);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200667
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100668 check_tfi_usage(bts);
669 talloc_free(bts);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200670}
671
Pau Espin Pedrolc85e0932021-02-25 18:08:10 +0100672static void test_many_connections(alloc_algorithm_func_t algo, unsigned expect_num,
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200673 const char *text)
674{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100675 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200676 struct gprs_rlcmac_trx *trx;
677 int counter1, counter2 = -1;
678 unsigned i;
679 enum test_mode mode_seq[] = {
680 TEST_MODE_DL_AFTER_UL,
681 TEST_MODE_UL_ONLY,
682 TEST_MODE_DL_AFTER_UL,
683 TEST_MODE_DL_ONLY,
684 };
685
Maxc59ef122017-11-27 13:21:41 +0100686 printf("Going to test assignment with many connections, algorithm %s\n", text);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200687
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100688 the_pcu->alloc_algorithm = algo;
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200689
690 trx = &bts->trx[0];
691 trx->pdch[3].enable();
692 trx->pdch[4].enable();
693 trx->pdch[5].enable();
694 trx->pdch[6].enable();
695 trx->pdch[7].enable();
696
697 for (i = 0; i < ARRAY_SIZE(mode_seq); i += 1) {
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100698 counter1 = alloc_many_tbfs(bts, 1, mslot_class_max(), mode_seq[i]);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200699 fprintf(stderr, " Allocated %d TBFs (previously %d)\n",
700 counter1, counter2);
701
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100702 check_tfi_usage(bts);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200703
704 /* This will stop earlier due to USF shortage */
705 if (mode_seq[i] == TEST_MODE_UL_ONLY)
706 continue;
707
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200708 if (counter2 >= 0) {
709 if (counter1 < counter2)
710 fprintf(stderr, " Expected %d >= %d in %s\n",
711 counter1, counter2, text);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200712 OSMO_ASSERT(counter1 >= counter2);
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200713 }
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200714
715 counter2 = counter1;
716 }
717
718 printf(" Successfully allocated %d TBFs\n", counter1);
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200719 if (counter1 != (int)expect_num)
Maxc59ef122017-11-27 13:21:41 +0100720 fprintf(stderr, " Expected %d TBFs (got %d) for algorithm %s\n", expect_num, counter1, text);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200721
722 OSMO_ASSERT(expect_num == (unsigned)counter1);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100723 talloc_free(bts);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200724}
725
Maxc59ef122017-11-27 13:21:41 +0100726static 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 +0200727{
Maxc59ef122017-11-27 13:21:41 +0100728 test_successive_allocation(alloc_algorithm_a, 1, 1, mode, exp_A, "A");
729 test_successive_allocation(alloc_algorithm_b, 10, 10, mode, exp_B, "B");
730 test_successive_allocation(alloc_algorithm_dynamic, 10, 10, mode, exp_dyn, "dynamic");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200731}
732
Maxc59ef122017-11-27 13:21:41 +0100733static void test_successive_allocations()
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200734{
Maxc59ef122017-11-27 13:21:41 +0100735 test_successive_allocation(alloc_algorithm_a, 1, 1, TEST_MODE_UL_AND_DL, 35, "A");
Pau Espin Pedroled2afa32021-02-22 17:20:15 +0100736 test_successive_allocation(alloc_algorithm_b, 10, 10, TEST_MODE_UL_AND_DL, 15, "B");
737 test_successive_allocation(alloc_algorithm_b, 12, 12, TEST_MODE_UL_AND_DL, 15, "B");
Maxc59ef122017-11-27 13:21:41 +0100738
Pau Espin Pedroled2afa32021-02-22 17:20:15 +0100739 test_successive_allocation(alloc_algorithm_b, 1, 12, TEST_MODE_UL_AND_DL, 23, "B");
740 test_successive_allocation(alloc_algorithm_b, 1, mslot_class_max(), TEST_MODE_UL_AND_DL, 17, "B");
741 test_successive_allocation(alloc_algorithm_dynamic, 1, mslot_class_max(), TEST_MODE_UL_AND_DL, 17, "dynamic");
Maxc59ef122017-11-27 13:21:41 +0100742
Pau Espin Pedroled2afa32021-02-22 17:20:15 +0100743 test_a_b_dyn(TEST_MODE_DL_AND_UL, 35, 15, 15);
744 test_a_b_dyn(TEST_MODE_DL_AFTER_UL, 160, 32, 101);
745 test_a_b_dyn(TEST_MODE_UL_AFTER_DL, 35, 15, 15);
746 test_a_b_dyn(TEST_MODE_UL_ONLY, 35, 15, 21);
Maxc59ef122017-11-27 13:21:41 +0100747 test_a_b_dyn(TEST_MODE_DL_ONLY, 160, 32, 101);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200748}
749
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530750static void test_2_consecutive_dl_tbfs()
751{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100752 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200753 GprsMs *ms;
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530754 struct gprs_rlcmac_trx *trx;
755 uint8_t ms_class = 11;
756 uint8_t egprs_ms_class = 11;
757 gprs_rlcmac_tbf *dl_tbf1, *dl_tbf2;
758 uint8_t numTs1 = 0, numTs2 = 0;
759
760 printf("Testing DL TS allocation for Multi UEs\n");
761
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100762 the_pcu->alloc_algorithm = alloc_algorithm_b;
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530763
764 trx = &bts->trx[0];
765 trx->pdch[4].enable();
766 trx->pdch[5].enable();
767 trx->pdch[6].enable();
768 trx->pdch[7].enable();
769
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100770 ms = bts_alloc_ms(bts, ms_class, egprs_ms_class);
Pau Espin Pedrol87573842022-10-26 20:23:45 +0200771 dl_tbf1 = dl_tbf_alloc(bts, ms, 0, false);
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530772 OSMO_ASSERT(dl_tbf1);
773
774 for (int i = 0; i < 8; i++) {
775 if (dl_tbf1->pdch[i])
776 numTs1++;
777 }
778 OSMO_ASSERT(numTs1 == 4);
779 printf("TBF1: numTs(%d)\n", numTs1);
780
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100781 ms = bts_alloc_ms(bts, ms_class, egprs_ms_class);
Pau Espin Pedrol87573842022-10-26 20:23:45 +0200782 dl_tbf2 = dl_tbf_alloc(bts, ms, 0, false);
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530783 OSMO_ASSERT(dl_tbf2);
784
785 for (int i = 0; i < 8; i++) {
786 if (dl_tbf2->pdch[i])
787 numTs2++;
788 }
789
790 /*
791 * TODO: currently 2nd DL TBF gets 3 TS
792 * This behaviour will be fixed in subsequent patch
793 */
794 printf("TBF2: numTs(%d)\n", numTs2);
795 OSMO_ASSERT(numTs2 == 3);
796
797 tbf_free(dl_tbf1);
798 tbf_free(dl_tbf2);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100799 talloc_free(bts);
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530800}
801
Oliver Smith3f794702021-08-23 14:19:21 +0200802static void test_bts_pch_timer(void)
803{
804 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100805 struct osmo_mobile_identity mi_imsi1, mi_imsi2;
Pau Espin Pedrol19b33922021-11-08 17:49:52 +0100806 struct osmo_mobile_identity mi_tmsi1;
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100807 mi_imsi1.type = mi_imsi2.type = GSM_MI_TYPE_IMSI;
Pau Espin Pedrol19b33922021-11-08 17:49:52 +0100808 mi_tmsi1.type = GSM_MI_TYPE_TMSI;
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100809 OSMO_STRLCPY_ARRAY(mi_imsi1.imsi, "1234");
810 OSMO_STRLCPY_ARRAY(mi_imsi2.imsi, "5678");
Pau Espin Pedrol19b33922021-11-08 17:49:52 +0100811 mi_tmsi1.tmsi = 987654321;
Oliver Smith3f794702021-08-23 14:19:21 +0200812
813 fprintf(stderr, "Testing bts_pch_timer dealloc on bts dealloc\n");
814 log_set_category_filter(osmo_stderr_target, DPCU, 1, LOGL_DEBUG);
815
816 fprintf(stderr, "Starting PCH timer for 2 IMSI\n");
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100817 bts_pch_timer_start(bts, &mi_imsi1, mi_imsi1.imsi);
818 bts_pch_timer_start(bts, &mi_imsi2, mi_imsi2.imsi);
Pau Espin Pedrol19b33922021-11-08 17:49:52 +0100819 fprintf(stderr, "Starting PCH timer for 1 TMSI\n");
820 bts_pch_timer_start(bts, &mi_tmsi1, "6666");
Oliver Smith3f794702021-08-23 14:19:21 +0200821
822 fprintf(stderr, "Deallocating BTS, expecting the PCH timer to be stopped and deallocated\n");
823 talloc_free(bts);
824}
825
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200826int main(int argc, char **argv)
827{
828 tall_pcu_ctx = talloc_named_const(NULL, 1, "moiji-mobile AllocTest context");
829 if (!tall_pcu_ctx)
830 abort();
831
Neels Hofmeyr78ce5912017-02-08 17:07:31 +0100832 msgb_talloc_ctx_init(tall_pcu_ctx, 0);
Neels Hofmeyr42f2d612018-04-01 16:54:40 +0200833 osmo_init_logging2(tall_pcu_ctx, &gprs_log_info);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200834 log_set_use_color(osmo_stderr_target, 0);
Pau Espin Pedrol00f52cc2021-02-19 14:01:52 +0100835 log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE);
Pau Espin Pedrolb18d2a52021-02-19 14:00:48 +0100836 log_set_print_category(osmo_stderr_target, 0);
837 log_set_print_category_hex(osmo_stderr_target, 0);
Philipp Maierde0e5582020-03-25 12:23:52 +0100838 log_set_category_filter(osmo_stderr_target, DTBF, 1, LOGL_INFO);
Jacob Erlbeck9ec49e22015-06-29 13:00:20 +0200839 if (getenv("LOGL_DEBUG"))
840 log_set_log_level(osmo_stderr_target, LOGL_DEBUG);
Pau Espin Pedroldc2aaac2021-05-14 12:50:46 +0200841 osmo_fsm_log_addr(false);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200842
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100843 the_pcu = gprs_pcu_alloc(tall_pcu_ctx);
844
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200845 test_alloc_a();
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100846 test_alloc_b();
Maxc59ef122017-11-27 13:21:41 +0100847 test_successive_allocations();
848 test_many_connections(alloc_algorithm_a, 160, "A");
Max01bd0cc2018-01-23 20:58:49 +0100849 test_many_connections(alloc_algorithm_b, 32, "B");
Maxc59ef122017-11-27 13:21:41 +0100850 test_many_connections(alloc_algorithm_dynamic, 160, "dynamic");
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530851 test_2_consecutive_dl_tbfs();
Oliver Smith3f794702021-08-23 14:19:21 +0200852 test_bts_pch_timer();
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100853
854 talloc_free(the_pcu);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200855 return EXIT_SUCCESS;
856}
857
858/*
859 * stubs that should not be reached
860 */
861extern "C" {
862void l1if_pdch_req() { abort(); }
863void l1if_connect_pdch() { abort(); }
Philipp Maier72ed3332023-02-27 15:32:00 +0100864void l1if_disconnect_pdch() { abort(); }
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200865void l1if_close_pdch() { abort(); }
866void l1if_open_pdch() { abort(); }
867}