blob: 8d833f54ee160e6b9612a26e43b899f7ef2fe800 [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
Pau Espin Pedrol4d363912023-04-21 19:32:16 +020043/* override, requires '-Wl,--wrap=pcu_sock_send' */
44int __real_pcu_sock_send(struct msgb *msg);
45extern "C" int __wrap_pcu_sock_send(struct msgb *msg)
46{
47 return 0;
48}
49
Daniel Willmann48aa0b02014-07-16 18:54:10 +020050static gprs_rlcmac_tbf *tbf_alloc(struct gprs_rlcmac_bts *bts,
Jacob Erlbecke2e004e2015-06-18 17:16:26 +020051 GprsMs *ms, gprs_rlcmac_tbf_direction dir,
Pau Espin Pedrol322456e2020-05-08 18:15:59 +020052 uint8_t use_trx, bool single_slot)
Daniel Willmann48aa0b02014-07-16 18:54:10 +020053{
Pau Espin Pedrol322456e2020-05-08 18:15:59 +020054 OSMO_ASSERT(ms != NULL);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +020055
Pau Espin Pedrol4d363912023-04-21 19:32:16 +020056 const struct alloc_resources_req req = {
57 .bts = bts,
58 .ms = ms,
59 .direction = dir,
60 .single = single_slot,
61 .use_trx = (int8_t)use_trx,
62 };
63 struct alloc_resources_res res = {};
64 int rc;
65
66 rc = the_pcu->alloc_algorithm(&req, &res);
67 if (rc < 0) {
68 LOGPMS(ms, DTBF, LOGL_NOTICE,
69 "Timeslot Allocation failed: trx = %d, single_slot = %d\n",
70 req.use_trx, req.single);
71 bts_do_rate_ctr_inc(ms->bts, CTR_TBF_ALLOC_FAIL);
72 return NULL;
73 }
74
75 /* Update MS, really allocate the resources */
76 if (res.reserved_ul_slots != ms_reserved_ul_slots(ms) ||
77 res.reserved_dl_slots != ms_reserved_dl_slots(ms)) {
78 /* The reserved slots have changed, update the MS */
79 ms_set_reserved_slots(ms, res.trx, res.reserved_ul_slots, res.reserved_dl_slots);
80 }
81 ms_set_first_common_ts(ms, res.first_common_ts);
82
83 if (dir == GPRS_RLCMAC_UL_TBF) {
84 struct gprs_rlcmac_ul_tbf *ul_tbf;
85 ul_tbf = ul_tbf_alloc(bts, ms);
86 ul_tbf_apply_allocated_resources(ul_tbf, &res);
87 ms_attach_tbf(ms, ul_tbf_as_tbf(ul_tbf));
88 return ul_tbf;
89 } else {
90 struct gprs_rlcmac_dl_tbf *dl_tbf;
91 dl_tbf = dl_tbf_alloc(bts, ms);
92 dl_tbf_apply_allocated_resources(dl_tbf, &res);
93 ms_attach_tbf(ms, dl_tbf_as_tbf(dl_tbf));
94 return dl_tbf;
95 }
Daniel Willmann48aa0b02014-07-16 18:54:10 +020096}
97
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010098static void check_tfi_usage(struct gprs_rlcmac_bts *bts)
Jacob Erlbeck61205a72015-07-09 11:35:50 +020099{
100 int pdch_no;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200101
102 struct gprs_rlcmac_tbf *tfi_usage[8][8][2][32] = {{{{NULL}}}};
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100103 struct llist_head *tbf_lists[2] = {
Pau Espin Pedrol1a1557a2021-05-13 18:39:36 +0200104 &bts->trx[0].ul_tbfs,
105 &bts->trx[0].dl_tbfs
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200106 };
107
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100108 struct llist_item *pos;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200109 gprs_rlcmac_tbf *tbf;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200110 unsigned list_idx;
111 struct gprs_rlcmac_tbf **tbf_var;
112
113 for (list_idx = 0; list_idx < ARRAY_SIZE(tbf_lists); list_idx += 1)
114 {
115
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100116 llist_for_each_entry(pos, tbf_lists[list_idx], list) {
117 tbf = (struct gprs_rlcmac_tbf *)pos->entry;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200118 for (pdch_no = 0; pdch_no < 8; pdch_no += 1) {
119 struct gprs_rlcmac_pdch *pdch = tbf->pdch[pdch_no];
120 if (pdch == NULL)
121 continue;
122
123 tbf_var = &tfi_usage
124 [tbf->trx->trx_no]
125 [pdch_no]
126 [tbf->direction]
127 [tbf->tfi()];
128
129 OSMO_ASSERT(*tbf_var == NULL);
130 if (tbf->direction == GPRS_RLCMAC_DL_TBF) {
131 OSMO_ASSERT(pdch->dl_tbf_by_tfi(
132 tbf->tfi()) == tbf);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100133 OSMO_ASSERT(bts_dl_tbf_by_tfi(bts,
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200134 tbf->tfi(),
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200135 tbf->trx->trx_no,
136 pdch_no) == tbf);
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200137 } else {
138 OSMO_ASSERT(pdch->ul_tbf_by_tfi(
139 tbf->tfi()) == tbf);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100140 OSMO_ASSERT(bts_ul_tbf_by_tfi(bts,
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200141 tbf->tfi(),
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200142 tbf->trx->trx_no,
143 pdch_no) == tbf);
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200144 }
145 *tbf_var = tbf;
Jacob Erlbeck47a57f62015-07-08 12:53:16 +0200146 OSMO_ASSERT(pdch->assigned_tfi(tbf->direction) &
147 (1 << tbf->tfi()));
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200148 }
149 }
150 }
151}
152
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200153static void test_alloc_a(gprs_rlcmac_tbf_direction dir,
154 uint8_t slots, const int count)
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200155{
156 int tfi;
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200157 int i;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200158 uint8_t used_trx, tmp_trx;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100159 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200160 GprsMs *ms;
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200161 struct gprs_rlcmac_tbf *tbfs[32*8+1] = { 0, };
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200162
163 printf("Testing alloc_a direction(%d)\n", dir);
164
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100165 the_pcu->alloc_algorithm = alloc_algorithm_a;
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200166
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200167 struct gprs_rlcmac_trx *trx = &bts->trx[0];
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200168 for (i = 0; i < 8; i += 1)
169 if (slots & (1 << i))
170 trx->pdch[i].enable();
171
172 OSMO_ASSERT(count >= 0 && count <= (int)ARRAY_SIZE(tbfs));
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200173
174 /**
175 * Currently alloc_a will only allocate from the first
176 * PDCH and all possible usf's. We run out of usf's before
177 * we are out of tfi's. Observe this and make sure that at
178 * least this part is working okay.
179 */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200180 for (i = 0; i < (int)ARRAY_SIZE(tbfs); ++i) {
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200181 ms = ms_alloc(bts, __func__);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200182 tbfs[i] = tbf_alloc(bts, ms, dir, -1, 0);
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200183 ms_unref(ms, __func__);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200184 if (tbfs[i] == NULL)
185 break;
186
187 used_trx = tbfs[i]->trx->trx_no;
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100188 tfi = bts_tfi_find_free(bts, dir, &tmp_trx, used_trx);
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200189 OSMO_ASSERT(tbfs[i]->tfi() != tfi);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200190 }
191
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100192 check_tfi_usage(bts);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200193
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200194 OSMO_ASSERT(i == count);
195
Pau Espin Pedrol0dcbc072021-11-10 19:09:10 +0100196 OSMO_ASSERT(bts_all_pdch_allocated(bts));
197
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200198 for (i = 0; i < count; ++i)
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200199 if (tbfs[i])
200 tbf_free(tbfs[i]);
201
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200202 ms = ms_alloc(bts, NULL);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200203 tbfs[0] = tbf_alloc(bts, ms, dir, -1, 0);
Holger Hans Peter Freytherf3f1bde2016-02-22 15:14:01 +0100204 OSMO_ASSERT(tbfs[0]);
205 tbf_free(tbfs[0]);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100206 talloc_free(bts);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200207}
208
209static void test_alloc_a()
210{
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200211 /* slots 2 - 3 */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200212 test_alloc_a(GPRS_RLCMAC_DL_TBF, 0x0c, 32*2);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200213 test_alloc_a(GPRS_RLCMAC_UL_TBF, 0x0c, 14);
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200214
215 /* slots 1 - 5 */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200216 test_alloc_a(GPRS_RLCMAC_DL_TBF, 0x1e, 32*4);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200217 test_alloc_a(GPRS_RLCMAC_UL_TBF, 0x1e, 28);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200218}
219
Max2ecf0fd2017-11-21 18:13:31 +0100220static void dump_assignment(struct gprs_rlcmac_tbf *tbf, const char *dir, bool verbose)
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100221{
Max2ecf0fd2017-11-21 18:13:31 +0100222 if (!verbose)
223 return;
Pau Espin Pedrol345d9ad2022-12-12 19:22:44 +0100224 const struct GprsMs *ms = tbf_ms(tbf);
Pau Espin Pedrol9935d0d2022-12-13 18:29:25 +0100225 const struct gprs_rlcmac_pdch *first_common = ms_first_common_ts(ms);
Max2ecf0fd2017-11-21 18:13:31 +0100226
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200227 for (size_t i = 0; i < ARRAY_SIZE(tbf->pdch); ++i)
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100228 if (tbf->pdch[i])
Neels Hofmeyrd34646a2017-02-08 17:07:40 +0100229 printf("PDCH[%zu] is used for %s\n", i, dir);
Pau Espin Pedrole2ed40d2022-12-15 17:35:05 +0100230 printf("%s is control_ts for %s\n", tbf->control_ts ? pdch_name(tbf->control_ts) : "(none)", dir);
231 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 +0100232}
233
Max2ecf0fd2017-11-21 18:13:31 +0100234#define ENABLE_PDCH(ts_no, enable_flag, trx) \
235 if (enable_flag) \
236 trx->pdch[ts_no].enable();
237
238static inline void enable_ts_on_bts(struct gprs_rlcmac_bts *bts,
239 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 +0100240{
Max2ecf0fd2017-11-21 18:13:31 +0100241 struct gprs_rlcmac_trx *trx = &bts->trx[0];
242
243 ENABLE_PDCH(0, ts0, trx);
244 ENABLE_PDCH(1, ts1, trx);
245 ENABLE_PDCH(2, ts2, trx);
246 ENABLE_PDCH(3, ts3, trx);
247 ENABLE_PDCH(4, ts4, trx);
248 ENABLE_PDCH(5, ts5, trx);
249 ENABLE_PDCH(6, ts6, trx);
250 ENABLE_PDCH(7, ts7, trx);
251}
252
253static inline bool test_alloc_b_ul_dl(bool ts0, bool ts1, bool ts2, bool ts3, bool ts4, bool ts5, bool ts6, bool ts7,
254 uint8_t ms_class, bool verbose)
255{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100256 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200257 GprsMs *ms;
Max2ecf0fd2017-11-21 18:13:31 +0100258 gprs_rlcmac_ul_tbf *ul_tbf;
259 gprs_rlcmac_dl_tbf *dl_tbf;
260
261 if (verbose)
262 printf("Testing UL then DL assignment.\n");
263
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100264 the_pcu->alloc_algorithm = alloc_algorithm_b;
Max2ecf0fd2017-11-21 18:13:31 +0100265
266 enable_ts_on_bts(bts, ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7);
267
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200268 ms = ms_alloc(bts, __func__);
Pau Espin Pedrolbfc97562023-04-17 14:49:29 +0200269 ms_set_ms_class(ms, ms_class);
Pau Espin Pedrol1e009472021-01-11 20:40:19 +0100270 /* Avoid delaying free to avoid tons of to-be-freed ms objects queuing */
Pau Espin Pedrolb53230a2023-04-20 16:42:54 +0200271 OSMO_ASSERT(osmo_tdef_set(the_pcu->T_defs, -2030, 0, OSMO_TDEF_S) == 0);
Pau Espin Pedrol4d363912023-04-21 19:32:16 +0200272 ul_tbf = (gprs_rlcmac_ul_tbf *)tbf_alloc(bts, ms, GPRS_RLCMAC_UL_TBF, -1, true);
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200273 if (!ul_tbf) {
274 ms_unref(ms, __func__);
Max2ecf0fd2017-11-21 18:13:31 +0100275 return false;
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200276 }
Max2ecf0fd2017-11-21 18:13:31 +0100277
278 OSMO_ASSERT(ul_tbf->ms());
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100279 OSMO_ASSERT(ms_current_trx(ul_tbf->ms()));
Max2ecf0fd2017-11-21 18:13:31 +0100280
281 dump_assignment(ul_tbf, "UL", verbose);
282
283 /* assume final ack has not been sent */
Pau Espin Pedrol4d363912023-04-21 19:32:16 +0200284 dl_tbf = (gprs_rlcmac_dl_tbf *)tbf_alloc(bts, ms, GPRS_RLCMAC_DL_TBF, ms_current_trx(ms)->trx_no, false);
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200285 if (!dl_tbf) {
286 ms_unref(ms, __func__);
Max2ecf0fd2017-11-21 18:13:31 +0100287 return false;
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200288 }
Max2ecf0fd2017-11-21 18:13:31 +0100289
290 dump_assignment(dl_tbf, "DL", verbose);
291
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100292 check_tfi_usage(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100293
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200294 ms_unref(ms, __func__);
Max2ecf0fd2017-11-21 18:13:31 +0100295 tbf_free(dl_tbf);
296 tbf_free(ul_tbf);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100297 talloc_free(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100298 return true;
299}
300
301static inline bool test_alloc_b_dl_ul(bool ts0, bool ts1, bool ts2, bool ts3, bool ts4, bool ts5, bool ts6, bool ts7,
302 uint8_t ms_class, bool verbose)
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 gprs_rlcmac_ul_tbf *ul_tbf;
307 gprs_rlcmac_dl_tbf *dl_tbf;
308
309 if (verbose)
310 printf("Testing DL then UL assignment followed by update\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, ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7);
315
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200316 ms = ms_alloc(bts, __func__);
Pau Espin Pedrolbfc97562023-04-17 14:49:29 +0200317 ms_set_ms_class(ms, ms_class);
Pau Espin Pedrol1e009472021-01-11 20:40:19 +0100318 /* Avoid delaying free to avoid tons of to-be-freed ms objects queuing */
Pau Espin Pedrolb53230a2023-04-20 16:42:54 +0200319 OSMO_ASSERT(osmo_tdef_set(the_pcu->T_defs, -2030, 0, OSMO_TDEF_S) == 0);
Pau Espin Pedrol4d363912023-04-21 19:32:16 +0200320 dl_tbf = (gprs_rlcmac_dl_tbf *)tbf_alloc(bts, ms, GPRS_RLCMAC_DL_TBF, -1, true);
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200321 if (!dl_tbf) {
322 ms_unref(ms, __func__);
Max2ecf0fd2017-11-21 18:13:31 +0100323 return false;
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200324 }
Max2ecf0fd2017-11-21 18:13:31 +0100325
Pau Espin Pedrol8abe13c2022-10-21 18:49:48 +0200326 ms_confirm_tlli(ms, 0x23);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200327 OSMO_ASSERT(dl_tbf->ms() == ms);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100328 OSMO_ASSERT(ms_current_trx(dl_tbf->ms()));
Max2ecf0fd2017-11-21 18:13:31 +0100329
330 dump_assignment(dl_tbf, "DL", verbose);
331
Pau Espin Pedrol4d363912023-04-21 19:32:16 +0200332 ul_tbf = (gprs_rlcmac_ul_tbf *)tbf_alloc(bts, ms, GPRS_RLCMAC_UL_TBF, ms_current_trx(ms)->trx_no, false);
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200333 if (!ul_tbf) {
334 ms_unref(ms, __func__);
Max2ecf0fd2017-11-21 18:13:31 +0100335 return false;
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200336 }
Max2ecf0fd2017-11-21 18:13:31 +0100337
Pau Espin Pedrol8abe13c2022-10-21 18:49:48 +0200338 ms_update_announced_tlli(ms, 0x23);
Pau Espin Pedrol0f859562022-10-31 10:51:20 +0100339 ul_tbf->m_contention_resolution_done = true;
Max2ecf0fd2017-11-21 18:13:31 +0100340
341 dump_assignment(ul_tbf, "UL", verbose);
342
Pau Espin Pedrolb7a2b592022-12-13 14:43:59 +0100343 /* now upgrade the dl_tbf */
344 OSMO_ASSERT(dl_tbf_upgrade_to_multislot(dl_tbf) == 0);
Max2ecf0fd2017-11-21 18:13:31 +0100345 dump_assignment(dl_tbf, "DL", verbose);
Max2ecf0fd2017-11-21 18:13:31 +0100346
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100347 check_tfi_usage(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100348
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200349 ms_unref(ms, __func__);
Max2ecf0fd2017-11-21 18:13:31 +0100350 tbf_free(dl_tbf);
351 tbf_free(ul_tbf);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100352 talloc_free(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100353 return true;
354}
355
356static inline bool test_alloc_b_jolly(uint8_t ms_class)
357{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100358 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200359 GprsMs *ms;
Max2ecf0fd2017-11-21 18:13:31 +0100360 int tfi;
361 uint8_t trx_no;
362 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
363
364 printf("Testing jolly example\n");
365
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100366 the_pcu->alloc_algorithm = alloc_algorithm_b;
Max2ecf0fd2017-11-21 18:13:31 +0100367
368 enable_ts_on_bts(bts, false, true, true, true, true, false, false, false);
369
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100370 tfi = bts_tfi_find_free(bts, GPRS_RLCMAC_UL_TBF, &trx_no, -1);
Max2ecf0fd2017-11-21 18:13:31 +0100371 OSMO_ASSERT(tfi >= 0);
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200372 ms = ms_alloc(bts, __func__);
Pau Espin Pedrolbfc97562023-04-17 14:49:29 +0200373 ms_set_ms_class(ms, ms_class);
Pau Espin Pedrol1e009472021-01-11 20:40:19 +0100374 /* Avoid delaying free to avoid tons of to-be-freed ms objects queuing */
Pau Espin Pedrolb53230a2023-04-20 16:42:54 +0200375 OSMO_ASSERT(osmo_tdef_set(the_pcu->T_defs, -2030, 0, OSMO_TDEF_S) == 0);
Pau Espin Pedrol4d363912023-04-21 19:32:16 +0200376 ul_tbf = tbf_alloc(bts, ms, GPRS_RLCMAC_UL_TBF, -1, false);
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200377 if (!ul_tbf) {
378 ms_unref(ms, __func__);
Max2ecf0fd2017-11-21 18:13:31 +0100379 return false;
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200380 }
Max2ecf0fd2017-11-21 18:13:31 +0100381
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200382 OSMO_ASSERT(ul_tbf->ms() == ms);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100383 OSMO_ASSERT(ms_current_trx(ul_tbf->ms()));
384 trx_no = ms_current_trx(ms)->trx_no;
Max2ecf0fd2017-11-21 18:13:31 +0100385 dump_assignment(ul_tbf, "UL", true);
386
387 /* assume final ack has not been sent */
Pau Espin Pedrol4d363912023-04-21 19:32:16 +0200388 dl_tbf = tbf_alloc(bts, ms, GPRS_RLCMAC_DL_TBF, trx_no, false);
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200389 if (!dl_tbf) {
390 ms_unref(ms, __func__);
Max2ecf0fd2017-11-21 18:13:31 +0100391 return false;
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200392 }
Max2ecf0fd2017-11-21 18:13:31 +0100393
394 dump_assignment(dl_tbf, "DL", true);
395
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100396 check_tfi_usage(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100397
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200398 ms_unref(ms, __func__);
Max2ecf0fd2017-11-21 18:13:31 +0100399 tbf_free(dl_tbf);
400 tbf_free(ul_tbf);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100401 talloc_free(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100402 return true;
403}
404
405static void test_alloc_b_for_ms(uint8_t ms_class)
406{
407 bool rc;
408
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100409 printf("Going to test multislot assignment MS_CLASS=%d\n", ms_class);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100410 /*
411 * PDCH is on TS 6,7,8 and we start with a UL allocation and
412 * then follow two DL allocations (once single, once normal).
413 *
414 * Uplink assigned and still available..
415 */
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100416
Max2ecf0fd2017-11-21 18:13:31 +0100417 rc = test_alloc_b_ul_dl(false, false, false, false, false, true, true, true, ms_class, true);
418 if (!rc)
419 return;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100420
421 /**
422 * Test with the other order.. first DL and then UL
423 */
Max2ecf0fd2017-11-21 18:13:31 +0100424 rc = test_alloc_b_dl_ul(false, false, false, false, false, true, true, true, ms_class, true);
425 if (!rc)
426 return;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100427
428 /* Andreas osmocom-pcu example */
Max2ecf0fd2017-11-21 18:13:31 +0100429 test_alloc_b_jolly(ms_class);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100430}
431
Max2ecf0fd2017-11-21 18:13:31 +0100432static 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 +0100433{
Max2ecf0fd2017-11-21 18:13:31 +0100434 bool rc;
435
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100436 /* we can test the allocation failures differently */
437 if (!ts0 && !ts1 && !ts2 && !ts3 && !ts4 && !ts5 && !ts6 && !ts7)
438 return;
439
440 printf("Mass test: TS0(%c%c%c%c%c%c%c%c)TS7 MS_Class=%d\n",
441 ts0 ? 'O' : 'x',
442 ts1 ? 'O' : 'x',
443 ts2 ? 'O' : 'x',
444 ts3 ? 'O' : 'x',
445 ts4 ? 'O' : 'x',
446 ts5 ? 'O' : 'x',
447 ts6 ? 'O' : 'x',
448 ts7 ? 'O' : 'x', ms_class);
449 fflush(stdout);
450
Max2ecf0fd2017-11-21 18:13:31 +0100451 rc = test_alloc_b_ul_dl(ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7, ms_class, false);
452 if (!rc)
453 return;
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100454
455 /**
456 * Test with the other order.. first DL and then UL
457 */
Max2ecf0fd2017-11-21 18:13:31 +0100458 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 +0100459}
460
461static void test_all_alloc_b()
462{
463 /* it is a bit crazy... */
464 for (uint8_t ts0 = 0; ts0 < 2; ++ts0)
465 for (uint8_t ts1 = 0; ts1 < 2; ++ts1)
466 for (uint8_t ts2 = 0; ts2 < 2; ++ts2)
467 for (uint8_t ts3 = 0; ts3 < 2; ++ts3)
468 for (uint8_t ts4 = 0; ts4 < 2; ++ts4)
469 for (uint8_t ts5 = 0; ts5 < 2; ++ts5)
470 for (uint8_t ts6 = 0; ts6 < 2; ++ts6)
471 for (uint8_t ts7 = 0; ts7 < 2; ++ts7)
Max9f460712018-01-23 20:57:08 +0100472 for (int ms_class = 0; ms_class < mslot_class_max(); ++ms_class)
Max2ecf0fd2017-11-21 18:13:31 +0100473 test_alloc_mass(ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7, ms_class);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100474}
475
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100476static void test_alloc_b()
477{
Max9f460712018-01-23 20:57:08 +0100478 for (int i = 0; i < mslot_class_max(); ++i)
Max2ecf0fd2017-11-21 18:13:31 +0100479 test_alloc_b_for_ms(i);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100480
481 test_all_alloc_b();
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100482}
483
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200484static char get_dir_char(uint8_t mask, uint8_t tx, uint8_t rx, uint8_t busy)
Jacob Erlbecke5655642015-06-29 12:19:52 +0200485{
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200486 int offs = busy ? 32 : 0;
487 return (mask & tx & rx) ? 'C' + offs :
488 (mask & tx) ? 'U' + offs :
489 (mask & rx) ? 'D' + offs :
Jacob Erlbecke5655642015-06-29 12:19:52 +0200490 '.';
491}
492
493enum test_mode {
494 TEST_MODE_UL_ONLY,
495 TEST_MODE_DL_ONLY,
496 TEST_MODE_UL_AND_DL,
497 TEST_MODE_DL_AND_UL,
498 TEST_MODE_DL_AFTER_UL,
499 TEST_MODE_UL_AFTER_DL,
500};
501
Maxc59ef122017-11-27 13:21:41 +0100502static inline char *test_mode_descr(enum test_mode t)
503{
504 switch (t) {
505 case TEST_MODE_UL_ONLY: return (char*)"UL only";
506 case TEST_MODE_DL_ONLY: return (char*)"DL only";
507 case TEST_MODE_UL_AND_DL: return (char*)"UL and DL";
508 case TEST_MODE_DL_AND_UL: return (char*)"DL and UL";
509 case TEST_MODE_DL_AFTER_UL: return (char*)"DL after UL";
510 case TEST_MODE_UL_AFTER_DL: return (char*)"UL after DL";
511 default: return NULL;
512 }
513}
514
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100515static GprsMs *alloc_tbfs(struct gprs_rlcmac_bts *bts, struct GprsMs *old_ms, enum test_mode mode)
Jacob Erlbecke5655642015-06-29 12:19:52 +0200516{
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100517 struct GprsMs *ms, *new_ms;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200518 uint8_t trx_no = -1;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200519
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100520 OSMO_ASSERT(old_ms != NULL);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200521
Jacob Erlbecke5655642015-06-29 12:19:52 +0200522 gprs_rlcmac_tbf *tbf = NULL;
523
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100524 if (ms_current_trx(old_ms))
525 trx_no = ms_current_trx(old_ms)->trx_no;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200526
Pau Espin Pedrol403e0482023-04-17 20:28:10 +0200527 ms_ref(old_ms, __func__);
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200528
Jacob Erlbecke5655642015-06-29 12:19:52 +0200529 /* Allocate what is needed first */
530 switch (mode) {
531 case TEST_MODE_UL_ONLY:
532 case TEST_MODE_DL_AFTER_UL:
533 case TEST_MODE_UL_AND_DL:
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100534 if (ms_ul_tbf(old_ms))
535 tbf_free(ms_ul_tbf(old_ms));
Pau Espin Pedrol4d363912023-04-21 19:32:16 +0200536 tbf = tbf_alloc(bts, old_ms, GPRS_RLCMAC_UL_TBF, trx_no, false);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100537 if (tbf == NULL) {
Pau Espin Pedrol0dcbc072021-11-10 19:09:10 +0100538 OSMO_ASSERT(trx_no != -1 || bts_all_pdch_allocated(bts));
Pau Espin Pedrol403e0482023-04-17 20:28:10 +0200539 ms_unref(old_ms, __func__);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200540 return NULL;
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100541 }
Jacob Erlbecke5655642015-06-29 12:19:52 +0200542 break;
543 case TEST_MODE_DL_ONLY:
544 case TEST_MODE_UL_AFTER_DL:
545 case TEST_MODE_DL_AND_UL:
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100546 if (ms_dl_tbf(old_ms))
547 tbf_free(ms_dl_tbf(old_ms));
Pau Espin Pedrol4d363912023-04-21 19:32:16 +0200548 tbf = tbf_alloc(bts, old_ms, GPRS_RLCMAC_DL_TBF, trx_no, false);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100549 if (tbf == NULL) {
Pau Espin Pedrol0dcbc072021-11-10 19:09:10 +0100550 OSMO_ASSERT(trx_no != -1 || bts_all_pdch_allocated(bts));
Pau Espin Pedrol403e0482023-04-17 20:28:10 +0200551 ms_unref(old_ms, __func__);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200552 return NULL;
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100553 }
Jacob Erlbecke5655642015-06-29 12:19:52 +0200554 }
555
556 OSMO_ASSERT(tbf);
557 OSMO_ASSERT(tbf->ms());
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100558 OSMO_ASSERT(old_ms == tbf->ms());
Jacob Erlbecke5655642015-06-29 12:19:52 +0200559 ms = tbf->ms();
560
Pau Espin Pedrol403e0482023-04-17 20:28:10 +0200561 ms_ref(ms, __func__);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100562 new_ms = ms;
Jacob Erlbeck14376a72015-07-07 11:31:28 +0200563 /* Continue with what is needed next */
564 switch (mode) {
565 case TEST_MODE_UL_ONLY:
566 case TEST_MODE_DL_ONLY:
567 /* We are done */
568 break;
569
570 case TEST_MODE_DL_AFTER_UL:
571 case TEST_MODE_UL_AND_DL:
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100572 new_ms = alloc_tbfs(bts, ms, TEST_MODE_DL_ONLY);
Jacob Erlbeck14376a72015-07-07 11:31:28 +0200573 break;
574
575 case TEST_MODE_UL_AFTER_DL:
576 case TEST_MODE_DL_AND_UL:
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100577 new_ms = alloc_tbfs(bts, ms, TEST_MODE_UL_ONLY);
Jacob Erlbeck14376a72015-07-07 11:31:28 +0200578 break;
579 }
580
Jacob Erlbecke5655642015-06-29 12:19:52 +0200581 /* Optionally delete the TBF */
582 switch (mode) {
583 case TEST_MODE_DL_AFTER_UL:
584 case TEST_MODE_UL_AFTER_DL:
585 tbf_free(tbf);
Jacob Erlbeck0f352a62015-07-16 18:23:33 +0200586 tbf = NULL;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200587 break;
588
589 default:
590 break;
591 }
592
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100593 if (!new_ms && tbf)
Jacob Erlbeck0f352a62015-07-16 18:23:33 +0200594 tbf_free(tbf);
595
Pau Espin Pedrol403e0482023-04-17 20:28:10 +0200596 ms_unref(old_ms, __func__);
597 ms_unref(ms, __func__);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100598 return new_ms;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200599}
600
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100601static unsigned alloc_many_tbfs(struct gprs_rlcmac_bts *bts, unsigned min_class,
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200602 unsigned max_class, enum test_mode mode)
Jacob Erlbecke5655642015-06-29 12:19:52 +0200603{
Jacob Erlbecke5655642015-06-29 12:19:52 +0200604 unsigned counter;
605 unsigned ms_class = min_class;
606
Jacob Erlbecke5655642015-06-29 12:19:52 +0200607 for (counter = 0; 1; counter += 1) {
608 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
609 uint8_t ul_slots = 0;
610 uint8_t dl_slots = 0;
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200611 uint8_t busy_slots = 0;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200612 unsigned i;
613 int tfi = -1;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200614 int tfi2;
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200615 uint8_t trx_no2;
616 struct gprs_rlcmac_trx *trx;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200617 GprsMs *ms;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200618 enum gprs_rlcmac_tbf_direction dir;
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200619 uint32_t tlli = counter + 0xc0000000;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200620
Pau Espin Pedrolcde18c52023-04-17 18:16:48 +0200621 ms = bts_get_ms_by_tlli(bts, tlli, GSM_RESERVED_TMSI);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200622 if (!ms)
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200623 ms = ms_alloc(bts, NULL);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100624 ms_set_ms_class(ms, ms_class);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100625 ms = alloc_tbfs(bts, ms, mode);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200626 if (!ms)
627 break;
628
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100629 ms_set_tlli(ms, tlli);
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200630
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100631 ul_tbf = ms_ul_tbf(ms);
632 dl_tbf = ms_dl_tbf(ms);
633 trx = ms_current_trx(ms);
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200634
635 OSMO_ASSERT(ul_tbf || dl_tbf);
Pau Espin Pedrol9935d0d2022-12-13 18:29:25 +0100636 OSMO_ASSERT(ms_first_common_ts(ms) != NULL);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200637 if (ul_tbf) {
Pau Espin Pedrol9935d0d2022-12-13 18:29:25 +0100638 ul_slots = 1 << (uint8_t)ms_first_common_ts(ms)->ts_no;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200639 tfi = ul_tbf->tfi();
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200640 dir = GPRS_RLCMAC_UL_TBF;
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200641 } else {
Pau Espin Pedrol9935d0d2022-12-13 18:29:25 +0100642 ul_slots = 1 << (uint8_t)ms_first_common_ts(ms)->ts_no;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200643 tfi = dl_tbf->tfi();
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200644 dir = GPRS_RLCMAC_DL_TBF;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200645 }
646
647 for (i = 0; dl_tbf && i < ARRAY_SIZE(dl_tbf->pdch); i += 1)
648 if (dl_tbf->pdch[i])
649 dl_slots |= 1 << i;
650
Max5b0df1f2017-09-11 10:38:59 +0200651 for (i = 0; ul_tbf && i < ARRAY_SIZE(ul_tbf->pdch); i += 1)
652 if (ul_tbf->pdch[i])
653 ul_slots |= 1 << i;
654
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200655 for (i = 0; trx && i < ARRAY_SIZE(trx->pdch); i += 1) {
656 struct gprs_rlcmac_pdch *pdch = &trx->pdch[i];
657
658 if (ul_tbf && dl_tbf)
659 continue;
660
661 if (ul_tbf &&
Maxd000d802017-09-20 17:55:28 +0200662 pdch->assigned_tfi(GPRS_RLCMAC_DL_TBF) != NO_FREE_TFI)
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200663 continue;
664
665 if (dl_tbf &&
Maxd000d802017-09-20 17:55:28 +0200666 pdch->assigned_tfi(GPRS_RLCMAC_UL_TBF) != NO_FREE_TFI)
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200667 continue;
668
669 busy_slots |= 1 << i;
670 }
671
Maxd000d802017-09-20 17:55:28 +0200672 printf(" TBF[%d] class %d reserves " OSMO_BIT_SPEC "\n",
Jacob Erlbecke5655642015-06-29 12:19:52 +0200673 tfi, ms_class,
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200674 get_dir_char(0x01, ul_slots, dl_slots, busy_slots),
675 get_dir_char(0x02, ul_slots, dl_slots, busy_slots),
676 get_dir_char(0x04, ul_slots, dl_slots, busy_slots),
677 get_dir_char(0x08, ul_slots, dl_slots, busy_slots),
678 get_dir_char(0x10, ul_slots, dl_slots, busy_slots),
679 get_dir_char(0x20, ul_slots, dl_slots, busy_slots),
680 get_dir_char(0x40, ul_slots, dl_slots, busy_slots),
681 get_dir_char(0x80, ul_slots, dl_slots, busy_slots));
Jacob Erlbecke5655642015-06-29 12:19:52 +0200682
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200683 if (tfi >= 0) {
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100684 OSMO_ASSERT(ms_current_trx(ms));
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100685 tfi2 = bts_tfi_find_free(bts, dir, &trx_no2,
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100686 ms_current_trx(ms)->trx_no);
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200687 OSMO_ASSERT(tfi != tfi2);
Jacob Erlbeck7b3675b2015-07-16 18:28:22 +0200688 OSMO_ASSERT(tfi2 < 0 ||
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100689 trx_no2 == ms_current_trx(ms)->trx_no);
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200690 }
691
Jacob Erlbecke5655642015-06-29 12:19:52 +0200692 ms_class += 1;
693 if (ms_class > max_class)
694 ms_class = min_class;
695 }
696
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200697 return counter;
698}
699
Pau Espin Pedrolc85e0932021-02-25 18:08:10 +0100700static void test_successive_allocation(alloc_algorithm_func_t algo, unsigned min_class,
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200701 unsigned max_class, enum test_mode mode,
702 unsigned expect_num, const char *text)
703{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100704 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200705 struct gprs_rlcmac_trx *trx;
706 unsigned counter;
707
Maxc59ef122017-11-27 13:21:41 +0100708 printf("Going to test assignment with many TBF, algorithm %s class %u..%u (%s)\n",
709 text, min_class, max_class, test_mode_descr(mode));
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200710
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100711 the_pcu->alloc_algorithm = algo;
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200712
713 trx = &bts->trx[0];
714 trx->pdch[3].enable();
715 trx->pdch[4].enable();
716 trx->pdch[5].enable();
717 trx->pdch[6].enable();
718 trx->pdch[7].enable();
719
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100720 counter = alloc_many_tbfs(bts, min_class, max_class, mode);
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200721
Maxc59ef122017-11-27 13:21:41 +0100722 printf(" Successfully allocated %u UL TBFs, algorithm %s class %u..%u (%s)\n",
723 counter, text, min_class, max_class, test_mode_descr(mode));
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200724 if (counter != expect_num)
Maxc59ef122017-11-27 13:21:41 +0100725 fprintf(stderr, " Expected %u TBFs (got %u), algorithm %s class %u..%u (%s)\n",
726 expect_num, counter, text, min_class, max_class, test_mode_descr(mode));
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200727
Jacob Erlbeckec478752015-06-19 16:35:38 +0200728 OSMO_ASSERT(counter == expect_num);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200729
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100730 check_tfi_usage(bts);
731 talloc_free(bts);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200732}
733
Pau Espin Pedrolc85e0932021-02-25 18:08:10 +0100734static void test_many_connections(alloc_algorithm_func_t algo, unsigned expect_num,
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200735 const char *text)
736{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100737 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200738 struct gprs_rlcmac_trx *trx;
739 int counter1, counter2 = -1;
740 unsigned i;
741 enum test_mode mode_seq[] = {
742 TEST_MODE_DL_AFTER_UL,
743 TEST_MODE_UL_ONLY,
744 TEST_MODE_DL_AFTER_UL,
745 TEST_MODE_DL_ONLY,
746 };
747
Maxc59ef122017-11-27 13:21:41 +0100748 printf("Going to test assignment with many connections, algorithm %s\n", text);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200749
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100750 the_pcu->alloc_algorithm = algo;
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200751
752 trx = &bts->trx[0];
753 trx->pdch[3].enable();
754 trx->pdch[4].enable();
755 trx->pdch[5].enable();
756 trx->pdch[6].enable();
757 trx->pdch[7].enable();
758
759 for (i = 0; i < ARRAY_SIZE(mode_seq); i += 1) {
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100760 counter1 = alloc_many_tbfs(bts, 1, mslot_class_max(), mode_seq[i]);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200761 fprintf(stderr, " Allocated %d TBFs (previously %d)\n",
762 counter1, counter2);
763
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100764 check_tfi_usage(bts);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200765
766 /* This will stop earlier due to USF shortage */
767 if (mode_seq[i] == TEST_MODE_UL_ONLY)
768 continue;
769
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200770 if (counter2 >= 0) {
771 if (counter1 < counter2)
772 fprintf(stderr, " Expected %d >= %d in %s\n",
773 counter1, counter2, text);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200774 OSMO_ASSERT(counter1 >= counter2);
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200775 }
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200776
777 counter2 = counter1;
778 }
779
780 printf(" Successfully allocated %d TBFs\n", counter1);
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200781 if (counter1 != (int)expect_num)
Maxc59ef122017-11-27 13:21:41 +0100782 fprintf(stderr, " Expected %d TBFs (got %d) for algorithm %s\n", expect_num, counter1, text);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200783
784 OSMO_ASSERT(expect_num == (unsigned)counter1);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100785 talloc_free(bts);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200786}
787
Maxc59ef122017-11-27 13:21:41 +0100788static 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 +0200789{
Maxc59ef122017-11-27 13:21:41 +0100790 test_successive_allocation(alloc_algorithm_a, 1, 1, mode, exp_A, "A");
791 test_successive_allocation(alloc_algorithm_b, 10, 10, mode, exp_B, "B");
792 test_successive_allocation(alloc_algorithm_dynamic, 10, 10, mode, exp_dyn, "dynamic");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200793}
794
Maxc59ef122017-11-27 13:21:41 +0100795static void test_successive_allocations()
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200796{
Maxc59ef122017-11-27 13:21:41 +0100797 test_successive_allocation(alloc_algorithm_a, 1, 1, TEST_MODE_UL_AND_DL, 35, "A");
Pau Espin Pedroled2afa32021-02-22 17:20:15 +0100798 test_successive_allocation(alloc_algorithm_b, 10, 10, TEST_MODE_UL_AND_DL, 15, "B");
799 test_successive_allocation(alloc_algorithm_b, 12, 12, TEST_MODE_UL_AND_DL, 15, "B");
Maxc59ef122017-11-27 13:21:41 +0100800
Pau Espin Pedroled2afa32021-02-22 17:20:15 +0100801 test_successive_allocation(alloc_algorithm_b, 1, 12, TEST_MODE_UL_AND_DL, 23, "B");
802 test_successive_allocation(alloc_algorithm_b, 1, mslot_class_max(), TEST_MODE_UL_AND_DL, 17, "B");
803 test_successive_allocation(alloc_algorithm_dynamic, 1, mslot_class_max(), TEST_MODE_UL_AND_DL, 17, "dynamic");
Maxc59ef122017-11-27 13:21:41 +0100804
Pau Espin Pedroled2afa32021-02-22 17:20:15 +0100805 test_a_b_dyn(TEST_MODE_DL_AND_UL, 35, 15, 15);
806 test_a_b_dyn(TEST_MODE_DL_AFTER_UL, 160, 32, 101);
807 test_a_b_dyn(TEST_MODE_UL_AFTER_DL, 35, 15, 15);
808 test_a_b_dyn(TEST_MODE_UL_ONLY, 35, 15, 21);
Maxc59ef122017-11-27 13:21:41 +0100809 test_a_b_dyn(TEST_MODE_DL_ONLY, 160, 32, 101);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200810}
811
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530812static void test_2_consecutive_dl_tbfs()
813{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100814 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200815 GprsMs *ms;
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530816 struct gprs_rlcmac_trx *trx;
817 uint8_t ms_class = 11;
818 uint8_t egprs_ms_class = 11;
819 gprs_rlcmac_tbf *dl_tbf1, *dl_tbf2;
820 uint8_t numTs1 = 0, numTs2 = 0;
821
822 printf("Testing DL TS allocation for Multi UEs\n");
823
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100824 the_pcu->alloc_algorithm = alloc_algorithm_b;
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530825
826 trx = &bts->trx[0];
827 trx->pdch[4].enable();
828 trx->pdch[5].enable();
829 trx->pdch[6].enable();
830 trx->pdch[7].enable();
831
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200832 ms = ms_alloc(bts, NULL);
Pau Espin Pedrolbfc97562023-04-17 14:49:29 +0200833 ms_set_ms_class(ms, ms_class);
834 ms_set_egprs_ms_class(ms, egprs_ms_class);
Pau Espin Pedrol4d363912023-04-21 19:32:16 +0200835 dl_tbf1 = tbf_alloc(bts, ms, GPRS_RLCMAC_DL_TBF, 0, false);
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530836 OSMO_ASSERT(dl_tbf1);
837
838 for (int i = 0; i < 8; i++) {
839 if (dl_tbf1->pdch[i])
840 numTs1++;
841 }
842 OSMO_ASSERT(numTs1 == 4);
843 printf("TBF1: numTs(%d)\n", numTs1);
844
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200845 ms = ms_alloc(bts, NULL);
Pau Espin Pedrolbfc97562023-04-17 14:49:29 +0200846 ms_set_ms_class(ms, ms_class);
847 ms_set_egprs_ms_class(ms, egprs_ms_class);
Pau Espin Pedrol4d363912023-04-21 19:32:16 +0200848 dl_tbf2 = tbf_alloc(bts, ms, GPRS_RLCMAC_DL_TBF, 0, false);
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530849 OSMO_ASSERT(dl_tbf2);
850
851 for (int i = 0; i < 8; i++) {
852 if (dl_tbf2->pdch[i])
853 numTs2++;
854 }
855
856 /*
857 * TODO: currently 2nd DL TBF gets 3 TS
858 * This behaviour will be fixed in subsequent patch
859 */
860 printf("TBF2: numTs(%d)\n", numTs2);
861 OSMO_ASSERT(numTs2 == 3);
862
863 tbf_free(dl_tbf1);
864 tbf_free(dl_tbf2);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100865 talloc_free(bts);
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530866}
867
Oliver Smith3f794702021-08-23 14:19:21 +0200868static void test_bts_pch_timer(void)
869{
870 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100871 struct osmo_mobile_identity mi_imsi1, mi_imsi2;
Pau Espin Pedrol19b33922021-11-08 17:49:52 +0100872 struct osmo_mobile_identity mi_tmsi1;
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100873 mi_imsi1.type = mi_imsi2.type = GSM_MI_TYPE_IMSI;
Pau Espin Pedrol19b33922021-11-08 17:49:52 +0100874 mi_tmsi1.type = GSM_MI_TYPE_TMSI;
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100875 OSMO_STRLCPY_ARRAY(mi_imsi1.imsi, "1234");
876 OSMO_STRLCPY_ARRAY(mi_imsi2.imsi, "5678");
Pau Espin Pedrol19b33922021-11-08 17:49:52 +0100877 mi_tmsi1.tmsi = 987654321;
Oliver Smith3f794702021-08-23 14:19:21 +0200878
879 fprintf(stderr, "Testing bts_pch_timer dealloc on bts dealloc\n");
880 log_set_category_filter(osmo_stderr_target, DPCU, 1, LOGL_DEBUG);
881
882 fprintf(stderr, "Starting PCH timer for 2 IMSI\n");
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100883 bts_pch_timer_start(bts, &mi_imsi1, mi_imsi1.imsi);
884 bts_pch_timer_start(bts, &mi_imsi2, mi_imsi2.imsi);
Pau Espin Pedrol19b33922021-11-08 17:49:52 +0100885 fprintf(stderr, "Starting PCH timer for 1 TMSI\n");
886 bts_pch_timer_start(bts, &mi_tmsi1, "6666");
Oliver Smith3f794702021-08-23 14:19:21 +0200887
888 fprintf(stderr, "Deallocating BTS, expecting the PCH timer to be stopped and deallocated\n");
889 talloc_free(bts);
890}
891
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200892int main(int argc, char **argv)
893{
894 tall_pcu_ctx = talloc_named_const(NULL, 1, "moiji-mobile AllocTest context");
895 if (!tall_pcu_ctx)
896 abort();
897
Neels Hofmeyr78ce5912017-02-08 17:07:31 +0100898 msgb_talloc_ctx_init(tall_pcu_ctx, 0);
Neels Hofmeyr42f2d612018-04-01 16:54:40 +0200899 osmo_init_logging2(tall_pcu_ctx, &gprs_log_info);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200900 log_set_use_color(osmo_stderr_target, 0);
Pau Espin Pedrol00f52cc2021-02-19 14:01:52 +0100901 log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE);
Pau Espin Pedrolb18d2a52021-02-19 14:00:48 +0100902 log_set_print_category(osmo_stderr_target, 0);
903 log_set_print_category_hex(osmo_stderr_target, 0);
Philipp Maierde0e5582020-03-25 12:23:52 +0100904 log_set_category_filter(osmo_stderr_target, DTBF, 1, LOGL_INFO);
Jacob Erlbeck9ec49e22015-06-29 13:00:20 +0200905 if (getenv("LOGL_DEBUG"))
906 log_set_log_level(osmo_stderr_target, LOGL_DEBUG);
Pau Espin Pedroldc2aaac2021-05-14 12:50:46 +0200907 osmo_fsm_log_addr(false);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200908
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100909 the_pcu = gprs_pcu_alloc(tall_pcu_ctx);
910
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200911 test_alloc_a();
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100912 test_alloc_b();
Maxc59ef122017-11-27 13:21:41 +0100913 test_successive_allocations();
914 test_many_connections(alloc_algorithm_a, 160, "A");
Max01bd0cc2018-01-23 20:58:49 +0100915 test_many_connections(alloc_algorithm_b, 32, "B");
Maxc59ef122017-11-27 13:21:41 +0100916 test_many_connections(alloc_algorithm_dynamic, 160, "dynamic");
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530917 test_2_consecutive_dl_tbfs();
Oliver Smith3f794702021-08-23 14:19:21 +0200918 test_bts_pch_timer();
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100919
920 talloc_free(the_pcu);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200921 return EXIT_SUCCESS;
922}
923
924/*
925 * stubs that should not be reached
926 */
927extern "C" {
928void l1if_pdch_req() { abort(); }
929void l1if_connect_pdch() { abort(); }
Philipp Maier72ed3332023-02-27 15:32:00 +0100930void l1if_disconnect_pdch() { abort(); }
Philipp Maier6ee8d132023-04-25 09:41:17 +0200931void l1if_close_trx() { abort(); }
932void l1if_open_trx() { abort(); }
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200933}