blob: a84bd641fc605febe3d6eef3339f933248d9995d [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 Pedroleae91472023-04-19 19:42:05 +0200138 ms = ms_alloc(bts, __func__);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200139 tbfs[i] = tbf_alloc(bts, ms, dir, -1, 0);
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200140 ms_unref(ms, __func__);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200141 if (tbfs[i] == NULL)
142 break;
143
144 used_trx = tbfs[i]->trx->trx_no;
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100145 tfi = bts_tfi_find_free(bts, dir, &tmp_trx, used_trx);
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200146 OSMO_ASSERT(tbfs[i]->tfi() != tfi);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200147 }
148
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100149 check_tfi_usage(bts);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200150
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200151 OSMO_ASSERT(i == count);
152
Pau Espin Pedrol0dcbc072021-11-10 19:09:10 +0100153 OSMO_ASSERT(bts_all_pdch_allocated(bts));
154
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200155 for (i = 0; i < count; ++i)
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200156 if (tbfs[i])
157 tbf_free(tbfs[i]);
158
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200159 ms = ms_alloc(bts, NULL);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200160 tbfs[0] = tbf_alloc(bts, ms, dir, -1, 0);
Holger Hans Peter Freytherf3f1bde2016-02-22 15:14:01 +0100161 OSMO_ASSERT(tbfs[0]);
162 tbf_free(tbfs[0]);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100163 talloc_free(bts);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200164}
165
166static void test_alloc_a()
167{
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200168 /* slots 2 - 3 */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200169 test_alloc_a(GPRS_RLCMAC_DL_TBF, 0x0c, 32*2);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200170 test_alloc_a(GPRS_RLCMAC_UL_TBF, 0x0c, 14);
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200171
172 /* slots 1 - 5 */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200173 test_alloc_a(GPRS_RLCMAC_DL_TBF, 0x1e, 32*4);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200174 test_alloc_a(GPRS_RLCMAC_UL_TBF, 0x1e, 28);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200175}
176
Max2ecf0fd2017-11-21 18:13:31 +0100177static void dump_assignment(struct gprs_rlcmac_tbf *tbf, const char *dir, bool verbose)
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100178{
Max2ecf0fd2017-11-21 18:13:31 +0100179 if (!verbose)
180 return;
Pau Espin Pedrol345d9ad2022-12-12 19:22:44 +0100181 const struct GprsMs *ms = tbf_ms(tbf);
Pau Espin Pedrol9935d0d2022-12-13 18:29:25 +0100182 const struct gprs_rlcmac_pdch *first_common = ms_first_common_ts(ms);
Max2ecf0fd2017-11-21 18:13:31 +0100183
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200184 for (size_t i = 0; i < ARRAY_SIZE(tbf->pdch); ++i)
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100185 if (tbf->pdch[i])
Neels Hofmeyrd34646a2017-02-08 17:07:40 +0100186 printf("PDCH[%zu] is used for %s\n", i, dir);
Pau Espin Pedrole2ed40d2022-12-15 17:35:05 +0100187 printf("%s is control_ts for %s\n", tbf->control_ts ? pdch_name(tbf->control_ts) : "(none)", dir);
188 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 +0100189}
190
Max2ecf0fd2017-11-21 18:13:31 +0100191#define ENABLE_PDCH(ts_no, enable_flag, trx) \
192 if (enable_flag) \
193 trx->pdch[ts_no].enable();
194
195static inline void enable_ts_on_bts(struct gprs_rlcmac_bts *bts,
196 bool ts0, bool ts1, bool ts2, bool ts3, bool ts4, bool ts5, bool ts6, bool ts7)
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100197{
Max2ecf0fd2017-11-21 18:13:31 +0100198 struct gprs_rlcmac_trx *trx = &bts->trx[0];
199
200 ENABLE_PDCH(0, ts0, trx);
201 ENABLE_PDCH(1, ts1, trx);
202 ENABLE_PDCH(2, ts2, trx);
203 ENABLE_PDCH(3, ts3, trx);
204 ENABLE_PDCH(4, ts4, trx);
205 ENABLE_PDCH(5, ts5, trx);
206 ENABLE_PDCH(6, ts6, trx);
207 ENABLE_PDCH(7, ts7, trx);
208}
209
210static inline bool test_alloc_b_ul_dl(bool ts0, bool ts1, bool ts2, bool ts3, bool ts4, bool ts5, bool ts6, bool ts7,
211 uint8_t ms_class, bool verbose)
212{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100213 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200214 GprsMs *ms;
Max2ecf0fd2017-11-21 18:13:31 +0100215 gprs_rlcmac_ul_tbf *ul_tbf;
216 gprs_rlcmac_dl_tbf *dl_tbf;
217
218 if (verbose)
219 printf("Testing UL then DL assignment.\n");
220
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100221 the_pcu->alloc_algorithm = alloc_algorithm_b;
Max2ecf0fd2017-11-21 18:13:31 +0100222
223 enable_ts_on_bts(bts, ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7);
224
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200225 ms = ms_alloc(bts, __func__);
Pau Espin Pedrolbfc97562023-04-17 14:49:29 +0200226 ms_set_ms_class(ms, ms_class);
Pau Espin Pedrol1e009472021-01-11 20:40:19 +0100227 /* Avoid delaying free to avoid tons of to-be-freed ms objects queuing */
Pau Espin Pedrolb53230a2023-04-20 16:42:54 +0200228 OSMO_ASSERT(osmo_tdef_set(the_pcu->T_defs, -2030, 0, OSMO_TDEF_S) == 0);
Pau Espin Pedrolbda7bb72022-10-31 14:33:09 +0100229 ul_tbf = ul_tbf_alloc(bts, ms, -1, true);
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200230 if (!ul_tbf) {
231 ms_unref(ms, __func__);
Max2ecf0fd2017-11-21 18:13:31 +0100232 return false;
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200233 }
Max2ecf0fd2017-11-21 18:13:31 +0100234
235 OSMO_ASSERT(ul_tbf->ms());
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100236 OSMO_ASSERT(ms_current_trx(ul_tbf->ms()));
Max2ecf0fd2017-11-21 18:13:31 +0100237
238 dump_assignment(ul_tbf, "UL", verbose);
239
240 /* assume final ack has not been sent */
Pau Espin Pedrol87573842022-10-26 20:23:45 +0200241 dl_tbf = dl_tbf_alloc(bts, ms, ms_current_trx(ms)->trx_no, false);
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200242 if (!dl_tbf) {
243 ms_unref(ms, __func__);
Max2ecf0fd2017-11-21 18:13:31 +0100244 return false;
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200245 }
Max2ecf0fd2017-11-21 18:13:31 +0100246
247 dump_assignment(dl_tbf, "DL", verbose);
248
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100249 check_tfi_usage(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100250
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200251 ms_unref(ms, __func__);
Max2ecf0fd2017-11-21 18:13:31 +0100252 tbf_free(dl_tbf);
253 tbf_free(ul_tbf);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100254 talloc_free(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100255 return true;
256}
257
258static inline bool test_alloc_b_dl_ul(bool ts0, bool ts1, bool ts2, bool ts3, bool ts4, bool ts5, bool ts6, bool ts7,
259 uint8_t ms_class, bool verbose)
260{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100261 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200262 GprsMs *ms;
Max2ecf0fd2017-11-21 18:13:31 +0100263 gprs_rlcmac_ul_tbf *ul_tbf;
264 gprs_rlcmac_dl_tbf *dl_tbf;
265
266 if (verbose)
267 printf("Testing DL then UL assignment followed by update\n");
268
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100269 the_pcu->alloc_algorithm = alloc_algorithm_b;
Max2ecf0fd2017-11-21 18:13:31 +0100270
271 enable_ts_on_bts(bts, ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7);
272
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200273 ms = ms_alloc(bts, __func__);
Pau Espin Pedrolbfc97562023-04-17 14:49:29 +0200274 ms_set_ms_class(ms, ms_class);
Pau Espin Pedrol1e009472021-01-11 20:40:19 +0100275 /* Avoid delaying free to avoid tons of to-be-freed ms objects queuing */
Pau Espin Pedrolb53230a2023-04-20 16:42:54 +0200276 OSMO_ASSERT(osmo_tdef_set(the_pcu->T_defs, -2030, 0, OSMO_TDEF_S) == 0);
Pau Espin Pedrol87573842022-10-26 20:23:45 +0200277 dl_tbf = dl_tbf_alloc(bts, ms, -1, true);
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200278 if (!dl_tbf) {
279 ms_unref(ms, __func__);
Max2ecf0fd2017-11-21 18:13:31 +0100280 return false;
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200281 }
Max2ecf0fd2017-11-21 18:13:31 +0100282
Pau Espin Pedrol8abe13c2022-10-21 18:49:48 +0200283 ms_confirm_tlli(ms, 0x23);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200284 OSMO_ASSERT(dl_tbf->ms() == ms);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100285 OSMO_ASSERT(ms_current_trx(dl_tbf->ms()));
Max2ecf0fd2017-11-21 18:13:31 +0100286
287 dump_assignment(dl_tbf, "DL", verbose);
288
Pau Espin Pedrolbda7bb72022-10-31 14:33:09 +0100289 ul_tbf = ul_tbf_alloc(bts, ms, ms_current_trx(ms)->trx_no, false);
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200290 if (!ul_tbf) {
291 ms_unref(ms, __func__);
Max2ecf0fd2017-11-21 18:13:31 +0100292 return false;
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200293 }
Max2ecf0fd2017-11-21 18:13:31 +0100294
Pau Espin Pedrol8abe13c2022-10-21 18:49:48 +0200295 ms_update_announced_tlli(ms, 0x23);
Pau Espin Pedrol0f859562022-10-31 10:51:20 +0100296 ul_tbf->m_contention_resolution_done = true;
Max2ecf0fd2017-11-21 18:13:31 +0100297
298 dump_assignment(ul_tbf, "UL", verbose);
299
Pau Espin Pedrolb7a2b592022-12-13 14:43:59 +0100300 /* now upgrade the dl_tbf */
301 OSMO_ASSERT(dl_tbf_upgrade_to_multislot(dl_tbf) == 0);
Max2ecf0fd2017-11-21 18:13:31 +0100302 dump_assignment(dl_tbf, "DL", verbose);
Max2ecf0fd2017-11-21 18:13:31 +0100303
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100304 check_tfi_usage(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100305
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200306 ms_unref(ms, __func__);
Max2ecf0fd2017-11-21 18:13:31 +0100307 tbf_free(dl_tbf);
308 tbf_free(ul_tbf);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100309 talloc_free(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100310 return true;
311}
312
313static inline bool test_alloc_b_jolly(uint8_t ms_class)
314{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100315 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200316 GprsMs *ms;
Max2ecf0fd2017-11-21 18:13:31 +0100317 int tfi;
318 uint8_t trx_no;
319 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
320
321 printf("Testing jolly example\n");
322
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100323 the_pcu->alloc_algorithm = alloc_algorithm_b;
Max2ecf0fd2017-11-21 18:13:31 +0100324
325 enable_ts_on_bts(bts, false, true, true, true, true, false, false, false);
326
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100327 tfi = bts_tfi_find_free(bts, GPRS_RLCMAC_UL_TBF, &trx_no, -1);
Max2ecf0fd2017-11-21 18:13:31 +0100328 OSMO_ASSERT(tfi >= 0);
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200329 ms = ms_alloc(bts, __func__);
Pau Espin Pedrolbfc97562023-04-17 14:49:29 +0200330 ms_set_ms_class(ms, ms_class);
Pau Espin Pedrol1e009472021-01-11 20:40:19 +0100331 /* Avoid delaying free to avoid tons of to-be-freed ms objects queuing */
Pau Espin Pedrolb53230a2023-04-20 16:42:54 +0200332 OSMO_ASSERT(osmo_tdef_set(the_pcu->T_defs, -2030, 0, OSMO_TDEF_S) == 0);
Pau Espin Pedrolbda7bb72022-10-31 14:33:09 +0100333 ul_tbf = ul_tbf_alloc(bts, ms, -1, false);
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200334 if (!ul_tbf) {
335 ms_unref(ms, __func__);
Max2ecf0fd2017-11-21 18:13:31 +0100336 return false;
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200337 }
Max2ecf0fd2017-11-21 18:13:31 +0100338
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200339 OSMO_ASSERT(ul_tbf->ms() == ms);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100340 OSMO_ASSERT(ms_current_trx(ul_tbf->ms()));
341 trx_no = ms_current_trx(ms)->trx_no;
Max2ecf0fd2017-11-21 18:13:31 +0100342 dump_assignment(ul_tbf, "UL", true);
343
344 /* assume final ack has not been sent */
Pau Espin Pedrol87573842022-10-26 20:23:45 +0200345 dl_tbf = dl_tbf_alloc(bts, ms, trx_no, false);
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200346 if (!dl_tbf) {
347 ms_unref(ms, __func__);
Max2ecf0fd2017-11-21 18:13:31 +0100348 return false;
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200349 }
Max2ecf0fd2017-11-21 18:13:31 +0100350
351 dump_assignment(dl_tbf, "DL", true);
352
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100353 check_tfi_usage(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100354
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200355 ms_unref(ms, __func__);
Max2ecf0fd2017-11-21 18:13:31 +0100356 tbf_free(dl_tbf);
357 tbf_free(ul_tbf);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100358 talloc_free(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100359 return true;
360}
361
362static void test_alloc_b_for_ms(uint8_t ms_class)
363{
364 bool rc;
365
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100366 printf("Going to test multislot assignment MS_CLASS=%d\n", ms_class);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100367 /*
368 * PDCH is on TS 6,7,8 and we start with a UL allocation and
369 * then follow two DL allocations (once single, once normal).
370 *
371 * Uplink assigned and still available..
372 */
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100373
Max2ecf0fd2017-11-21 18:13:31 +0100374 rc = test_alloc_b_ul_dl(false, false, false, false, false, true, true, true, ms_class, true);
375 if (!rc)
376 return;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100377
378 /**
379 * Test with the other order.. first DL and then UL
380 */
Max2ecf0fd2017-11-21 18:13:31 +0100381 rc = test_alloc_b_dl_ul(false, false, false, false, false, true, true, true, ms_class, true);
382 if (!rc)
383 return;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100384
385 /* Andreas osmocom-pcu example */
Max2ecf0fd2017-11-21 18:13:31 +0100386 test_alloc_b_jolly(ms_class);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100387}
388
Max2ecf0fd2017-11-21 18:13:31 +0100389static 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 +0100390{
Max2ecf0fd2017-11-21 18:13:31 +0100391 bool rc;
392
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100393 /* we can test the allocation failures differently */
394 if (!ts0 && !ts1 && !ts2 && !ts3 && !ts4 && !ts5 && !ts6 && !ts7)
395 return;
396
397 printf("Mass test: TS0(%c%c%c%c%c%c%c%c)TS7 MS_Class=%d\n",
398 ts0 ? 'O' : 'x',
399 ts1 ? 'O' : 'x',
400 ts2 ? 'O' : 'x',
401 ts3 ? 'O' : 'x',
402 ts4 ? 'O' : 'x',
403 ts5 ? 'O' : 'x',
404 ts6 ? 'O' : 'x',
405 ts7 ? 'O' : 'x', ms_class);
406 fflush(stdout);
407
Max2ecf0fd2017-11-21 18:13:31 +0100408 rc = test_alloc_b_ul_dl(ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7, ms_class, false);
409 if (!rc)
410 return;
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100411
412 /**
413 * Test with the other order.. first DL and then UL
414 */
Max2ecf0fd2017-11-21 18:13:31 +0100415 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 +0100416}
417
418static void test_all_alloc_b()
419{
420 /* it is a bit crazy... */
421 for (uint8_t ts0 = 0; ts0 < 2; ++ts0)
422 for (uint8_t ts1 = 0; ts1 < 2; ++ts1)
423 for (uint8_t ts2 = 0; ts2 < 2; ++ts2)
424 for (uint8_t ts3 = 0; ts3 < 2; ++ts3)
425 for (uint8_t ts4 = 0; ts4 < 2; ++ts4)
426 for (uint8_t ts5 = 0; ts5 < 2; ++ts5)
427 for (uint8_t ts6 = 0; ts6 < 2; ++ts6)
428 for (uint8_t ts7 = 0; ts7 < 2; ++ts7)
Max9f460712018-01-23 20:57:08 +0100429 for (int ms_class = 0; ms_class < mslot_class_max(); ++ms_class)
Max2ecf0fd2017-11-21 18:13:31 +0100430 test_alloc_mass(ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7, ms_class);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100431}
432
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100433static void test_alloc_b()
434{
Max9f460712018-01-23 20:57:08 +0100435 for (int i = 0; i < mslot_class_max(); ++i)
Max2ecf0fd2017-11-21 18:13:31 +0100436 test_alloc_b_for_ms(i);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100437
438 test_all_alloc_b();
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100439}
440
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200441static char get_dir_char(uint8_t mask, uint8_t tx, uint8_t rx, uint8_t busy)
Jacob Erlbecke5655642015-06-29 12:19:52 +0200442{
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200443 int offs = busy ? 32 : 0;
444 return (mask & tx & rx) ? 'C' + offs :
445 (mask & tx) ? 'U' + offs :
446 (mask & rx) ? 'D' + offs :
Jacob Erlbecke5655642015-06-29 12:19:52 +0200447 '.';
448}
449
450enum test_mode {
451 TEST_MODE_UL_ONLY,
452 TEST_MODE_DL_ONLY,
453 TEST_MODE_UL_AND_DL,
454 TEST_MODE_DL_AND_UL,
455 TEST_MODE_DL_AFTER_UL,
456 TEST_MODE_UL_AFTER_DL,
457};
458
Maxc59ef122017-11-27 13:21:41 +0100459static inline char *test_mode_descr(enum test_mode t)
460{
461 switch (t) {
462 case TEST_MODE_UL_ONLY: return (char*)"UL only";
463 case TEST_MODE_DL_ONLY: return (char*)"DL only";
464 case TEST_MODE_UL_AND_DL: return (char*)"UL and DL";
465 case TEST_MODE_DL_AND_UL: return (char*)"DL and UL";
466 case TEST_MODE_DL_AFTER_UL: return (char*)"DL after UL";
467 case TEST_MODE_UL_AFTER_DL: return (char*)"UL after DL";
468 default: return NULL;
469 }
470}
471
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100472static GprsMs *alloc_tbfs(struct gprs_rlcmac_bts *bts, struct GprsMs *old_ms, enum test_mode mode)
Jacob Erlbecke5655642015-06-29 12:19:52 +0200473{
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100474 struct GprsMs *ms, *new_ms;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200475 uint8_t trx_no = -1;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200476
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100477 OSMO_ASSERT(old_ms != NULL);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200478
Jacob Erlbecke5655642015-06-29 12:19:52 +0200479 gprs_rlcmac_tbf *tbf = NULL;
480
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100481 if (ms_current_trx(old_ms))
482 trx_no = ms_current_trx(old_ms)->trx_no;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200483
Pau Espin Pedrol403e0482023-04-17 20:28:10 +0200484 ms_ref(old_ms, __func__);
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200485
Jacob Erlbecke5655642015-06-29 12:19:52 +0200486 /* Allocate what is needed first */
487 switch (mode) {
488 case TEST_MODE_UL_ONLY:
489 case TEST_MODE_DL_AFTER_UL:
490 case TEST_MODE_UL_AND_DL:
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100491 if (ms_ul_tbf(old_ms))
492 tbf_free(ms_ul_tbf(old_ms));
Pau Espin Pedrolbda7bb72022-10-31 14:33:09 +0100493 tbf = ul_tbf_alloc(bts, old_ms, trx_no, false);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100494 if (tbf == NULL) {
Pau Espin Pedrol0dcbc072021-11-10 19:09:10 +0100495 OSMO_ASSERT(trx_no != -1 || bts_all_pdch_allocated(bts));
Pau Espin Pedrol403e0482023-04-17 20:28:10 +0200496 ms_unref(old_ms, __func__);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200497 return NULL;
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100498 }
Jacob Erlbecke5655642015-06-29 12:19:52 +0200499 break;
500 case TEST_MODE_DL_ONLY:
501 case TEST_MODE_UL_AFTER_DL:
502 case TEST_MODE_DL_AND_UL:
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100503 if (ms_dl_tbf(old_ms))
504 tbf_free(ms_dl_tbf(old_ms));
Pau Espin Pedrol87573842022-10-26 20:23:45 +0200505 tbf = dl_tbf_alloc(bts, old_ms, trx_no, false);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100506 if (tbf == NULL) {
Pau Espin Pedrol0dcbc072021-11-10 19:09:10 +0100507 OSMO_ASSERT(trx_no != -1 || bts_all_pdch_allocated(bts));
Pau Espin Pedrol403e0482023-04-17 20:28:10 +0200508 ms_unref(old_ms, __func__);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200509 return NULL;
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100510 }
Jacob Erlbecke5655642015-06-29 12:19:52 +0200511 }
512
513 OSMO_ASSERT(tbf);
514 OSMO_ASSERT(tbf->ms());
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100515 OSMO_ASSERT(old_ms == tbf->ms());
Jacob Erlbecke5655642015-06-29 12:19:52 +0200516 ms = tbf->ms();
517
Pau Espin Pedrol403e0482023-04-17 20:28:10 +0200518 ms_ref(ms, __func__);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100519 new_ms = ms;
Jacob Erlbeck14376a72015-07-07 11:31:28 +0200520 /* Continue with what is needed next */
521 switch (mode) {
522 case TEST_MODE_UL_ONLY:
523 case TEST_MODE_DL_ONLY:
524 /* We are done */
525 break;
526
527 case TEST_MODE_DL_AFTER_UL:
528 case TEST_MODE_UL_AND_DL:
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100529 new_ms = alloc_tbfs(bts, ms, TEST_MODE_DL_ONLY);
Jacob Erlbeck14376a72015-07-07 11:31:28 +0200530 break;
531
532 case TEST_MODE_UL_AFTER_DL:
533 case TEST_MODE_DL_AND_UL:
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100534 new_ms = alloc_tbfs(bts, ms, TEST_MODE_UL_ONLY);
Jacob Erlbeck14376a72015-07-07 11:31:28 +0200535 break;
536 }
537
Jacob Erlbecke5655642015-06-29 12:19:52 +0200538 /* Optionally delete the TBF */
539 switch (mode) {
540 case TEST_MODE_DL_AFTER_UL:
541 case TEST_MODE_UL_AFTER_DL:
542 tbf_free(tbf);
Jacob Erlbeck0f352a62015-07-16 18:23:33 +0200543 tbf = NULL;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200544 break;
545
546 default:
547 break;
548 }
549
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100550 if (!new_ms && tbf)
Jacob Erlbeck0f352a62015-07-16 18:23:33 +0200551 tbf_free(tbf);
552
Pau Espin Pedrol403e0482023-04-17 20:28:10 +0200553 ms_unref(old_ms, __func__);
554 ms_unref(ms, __func__);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100555 return new_ms;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200556}
557
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100558static unsigned alloc_many_tbfs(struct gprs_rlcmac_bts *bts, unsigned min_class,
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200559 unsigned max_class, enum test_mode mode)
Jacob Erlbecke5655642015-06-29 12:19:52 +0200560{
Jacob Erlbecke5655642015-06-29 12:19:52 +0200561 unsigned counter;
562 unsigned ms_class = min_class;
563
Jacob Erlbecke5655642015-06-29 12:19:52 +0200564 for (counter = 0; 1; counter += 1) {
565 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
566 uint8_t ul_slots = 0;
567 uint8_t dl_slots = 0;
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200568 uint8_t busy_slots = 0;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200569 unsigned i;
570 int tfi = -1;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200571 int tfi2;
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200572 uint8_t trx_no2;
573 struct gprs_rlcmac_trx *trx;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200574 GprsMs *ms;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200575 enum gprs_rlcmac_tbf_direction dir;
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200576 uint32_t tlli = counter + 0xc0000000;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200577
Pau Espin Pedrolcde18c52023-04-17 18:16:48 +0200578 ms = bts_get_ms_by_tlli(bts, tlli, GSM_RESERVED_TMSI);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200579 if (!ms)
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200580 ms = ms_alloc(bts, NULL);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100581 ms_set_ms_class(ms, ms_class);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100582 ms = alloc_tbfs(bts, ms, mode);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200583 if (!ms)
584 break;
585
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100586 ms_set_tlli(ms, tlli);
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200587
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100588 ul_tbf = ms_ul_tbf(ms);
589 dl_tbf = ms_dl_tbf(ms);
590 trx = ms_current_trx(ms);
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200591
592 OSMO_ASSERT(ul_tbf || dl_tbf);
Pau Espin Pedrol9935d0d2022-12-13 18:29:25 +0100593 OSMO_ASSERT(ms_first_common_ts(ms) != NULL);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200594 if (ul_tbf) {
Pau Espin Pedrol9935d0d2022-12-13 18:29:25 +0100595 ul_slots = 1 << (uint8_t)ms_first_common_ts(ms)->ts_no;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200596 tfi = ul_tbf->tfi();
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200597 dir = GPRS_RLCMAC_UL_TBF;
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200598 } else {
Pau Espin Pedrol9935d0d2022-12-13 18:29:25 +0100599 ul_slots = 1 << (uint8_t)ms_first_common_ts(ms)->ts_no;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200600 tfi = dl_tbf->tfi();
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200601 dir = GPRS_RLCMAC_DL_TBF;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200602 }
603
604 for (i = 0; dl_tbf && i < ARRAY_SIZE(dl_tbf->pdch); i += 1)
605 if (dl_tbf->pdch[i])
606 dl_slots |= 1 << i;
607
Max5b0df1f2017-09-11 10:38:59 +0200608 for (i = 0; ul_tbf && i < ARRAY_SIZE(ul_tbf->pdch); i += 1)
609 if (ul_tbf->pdch[i])
610 ul_slots |= 1 << i;
611
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200612 for (i = 0; trx && i < ARRAY_SIZE(trx->pdch); i += 1) {
613 struct gprs_rlcmac_pdch *pdch = &trx->pdch[i];
614
615 if (ul_tbf && dl_tbf)
616 continue;
617
618 if (ul_tbf &&
Maxd000d802017-09-20 17:55:28 +0200619 pdch->assigned_tfi(GPRS_RLCMAC_DL_TBF) != NO_FREE_TFI)
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200620 continue;
621
622 if (dl_tbf &&
Maxd000d802017-09-20 17:55:28 +0200623 pdch->assigned_tfi(GPRS_RLCMAC_UL_TBF) != NO_FREE_TFI)
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200624 continue;
625
626 busy_slots |= 1 << i;
627 }
628
Maxd000d802017-09-20 17:55:28 +0200629 printf(" TBF[%d] class %d reserves " OSMO_BIT_SPEC "\n",
Jacob Erlbecke5655642015-06-29 12:19:52 +0200630 tfi, ms_class,
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200631 get_dir_char(0x01, ul_slots, dl_slots, busy_slots),
632 get_dir_char(0x02, ul_slots, dl_slots, busy_slots),
633 get_dir_char(0x04, ul_slots, dl_slots, busy_slots),
634 get_dir_char(0x08, ul_slots, dl_slots, busy_slots),
635 get_dir_char(0x10, ul_slots, dl_slots, busy_slots),
636 get_dir_char(0x20, ul_slots, dl_slots, busy_slots),
637 get_dir_char(0x40, ul_slots, dl_slots, busy_slots),
638 get_dir_char(0x80, ul_slots, dl_slots, busy_slots));
Jacob Erlbecke5655642015-06-29 12:19:52 +0200639
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200640 if (tfi >= 0) {
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100641 OSMO_ASSERT(ms_current_trx(ms));
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100642 tfi2 = bts_tfi_find_free(bts, dir, &trx_no2,
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100643 ms_current_trx(ms)->trx_no);
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200644 OSMO_ASSERT(tfi != tfi2);
Jacob Erlbeck7b3675b2015-07-16 18:28:22 +0200645 OSMO_ASSERT(tfi2 < 0 ||
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100646 trx_no2 == ms_current_trx(ms)->trx_no);
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200647 }
648
Jacob Erlbecke5655642015-06-29 12:19:52 +0200649 ms_class += 1;
650 if (ms_class > max_class)
651 ms_class = min_class;
652 }
653
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200654 return counter;
655}
656
Pau Espin Pedrolc85e0932021-02-25 18:08:10 +0100657static void test_successive_allocation(alloc_algorithm_func_t algo, unsigned min_class,
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200658 unsigned max_class, enum test_mode mode,
659 unsigned expect_num, const char *text)
660{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100661 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200662 struct gprs_rlcmac_trx *trx;
663 unsigned counter;
664
Maxc59ef122017-11-27 13:21:41 +0100665 printf("Going to test assignment with many TBF, algorithm %s class %u..%u (%s)\n",
666 text, min_class, max_class, test_mode_descr(mode));
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200667
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100668 the_pcu->alloc_algorithm = algo;
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200669
670 trx = &bts->trx[0];
671 trx->pdch[3].enable();
672 trx->pdch[4].enable();
673 trx->pdch[5].enable();
674 trx->pdch[6].enable();
675 trx->pdch[7].enable();
676
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100677 counter = alloc_many_tbfs(bts, min_class, max_class, mode);
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200678
Maxc59ef122017-11-27 13:21:41 +0100679 printf(" Successfully allocated %u UL TBFs, algorithm %s class %u..%u (%s)\n",
680 counter, text, min_class, max_class, test_mode_descr(mode));
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200681 if (counter != expect_num)
Maxc59ef122017-11-27 13:21:41 +0100682 fprintf(stderr, " Expected %u TBFs (got %u), algorithm %s class %u..%u (%s)\n",
683 expect_num, counter, text, min_class, max_class, test_mode_descr(mode));
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200684
Jacob Erlbeckec478752015-06-19 16:35:38 +0200685 OSMO_ASSERT(counter == expect_num);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200686
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100687 check_tfi_usage(bts);
688 talloc_free(bts);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200689}
690
Pau Espin Pedrolc85e0932021-02-25 18:08:10 +0100691static void test_many_connections(alloc_algorithm_func_t algo, unsigned expect_num,
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200692 const char *text)
693{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100694 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200695 struct gprs_rlcmac_trx *trx;
696 int counter1, counter2 = -1;
697 unsigned i;
698 enum test_mode mode_seq[] = {
699 TEST_MODE_DL_AFTER_UL,
700 TEST_MODE_UL_ONLY,
701 TEST_MODE_DL_AFTER_UL,
702 TEST_MODE_DL_ONLY,
703 };
704
Maxc59ef122017-11-27 13:21:41 +0100705 printf("Going to test assignment with many connections, algorithm %s\n", text);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200706
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100707 the_pcu->alloc_algorithm = algo;
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200708
709 trx = &bts->trx[0];
710 trx->pdch[3].enable();
711 trx->pdch[4].enable();
712 trx->pdch[5].enable();
713 trx->pdch[6].enable();
714 trx->pdch[7].enable();
715
716 for (i = 0; i < ARRAY_SIZE(mode_seq); i += 1) {
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100717 counter1 = alloc_many_tbfs(bts, 1, mslot_class_max(), mode_seq[i]);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200718 fprintf(stderr, " Allocated %d TBFs (previously %d)\n",
719 counter1, counter2);
720
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100721 check_tfi_usage(bts);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200722
723 /* This will stop earlier due to USF shortage */
724 if (mode_seq[i] == TEST_MODE_UL_ONLY)
725 continue;
726
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200727 if (counter2 >= 0) {
728 if (counter1 < counter2)
729 fprintf(stderr, " Expected %d >= %d in %s\n",
730 counter1, counter2, text);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200731 OSMO_ASSERT(counter1 >= counter2);
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200732 }
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200733
734 counter2 = counter1;
735 }
736
737 printf(" Successfully allocated %d TBFs\n", counter1);
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200738 if (counter1 != (int)expect_num)
Maxc59ef122017-11-27 13:21:41 +0100739 fprintf(stderr, " Expected %d TBFs (got %d) for algorithm %s\n", expect_num, counter1, text);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200740
741 OSMO_ASSERT(expect_num == (unsigned)counter1);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100742 talloc_free(bts);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200743}
744
Maxc59ef122017-11-27 13:21:41 +0100745static 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 +0200746{
Maxc59ef122017-11-27 13:21:41 +0100747 test_successive_allocation(alloc_algorithm_a, 1, 1, mode, exp_A, "A");
748 test_successive_allocation(alloc_algorithm_b, 10, 10, mode, exp_B, "B");
749 test_successive_allocation(alloc_algorithm_dynamic, 10, 10, mode, exp_dyn, "dynamic");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200750}
751
Maxc59ef122017-11-27 13:21:41 +0100752static void test_successive_allocations()
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200753{
Maxc59ef122017-11-27 13:21:41 +0100754 test_successive_allocation(alloc_algorithm_a, 1, 1, TEST_MODE_UL_AND_DL, 35, "A");
Pau Espin Pedroled2afa32021-02-22 17:20:15 +0100755 test_successive_allocation(alloc_algorithm_b, 10, 10, TEST_MODE_UL_AND_DL, 15, "B");
756 test_successive_allocation(alloc_algorithm_b, 12, 12, TEST_MODE_UL_AND_DL, 15, "B");
Maxc59ef122017-11-27 13:21:41 +0100757
Pau Espin Pedroled2afa32021-02-22 17:20:15 +0100758 test_successive_allocation(alloc_algorithm_b, 1, 12, TEST_MODE_UL_AND_DL, 23, "B");
759 test_successive_allocation(alloc_algorithm_b, 1, mslot_class_max(), TEST_MODE_UL_AND_DL, 17, "B");
760 test_successive_allocation(alloc_algorithm_dynamic, 1, mslot_class_max(), TEST_MODE_UL_AND_DL, 17, "dynamic");
Maxc59ef122017-11-27 13:21:41 +0100761
Pau Espin Pedroled2afa32021-02-22 17:20:15 +0100762 test_a_b_dyn(TEST_MODE_DL_AND_UL, 35, 15, 15);
763 test_a_b_dyn(TEST_MODE_DL_AFTER_UL, 160, 32, 101);
764 test_a_b_dyn(TEST_MODE_UL_AFTER_DL, 35, 15, 15);
765 test_a_b_dyn(TEST_MODE_UL_ONLY, 35, 15, 21);
Maxc59ef122017-11-27 13:21:41 +0100766 test_a_b_dyn(TEST_MODE_DL_ONLY, 160, 32, 101);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200767}
768
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530769static void test_2_consecutive_dl_tbfs()
770{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100771 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200772 GprsMs *ms;
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530773 struct gprs_rlcmac_trx *trx;
774 uint8_t ms_class = 11;
775 uint8_t egprs_ms_class = 11;
776 gprs_rlcmac_tbf *dl_tbf1, *dl_tbf2;
777 uint8_t numTs1 = 0, numTs2 = 0;
778
779 printf("Testing DL TS allocation for Multi UEs\n");
780
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100781 the_pcu->alloc_algorithm = alloc_algorithm_b;
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530782
783 trx = &bts->trx[0];
784 trx->pdch[4].enable();
785 trx->pdch[5].enable();
786 trx->pdch[6].enable();
787 trx->pdch[7].enable();
788
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200789 ms = ms_alloc(bts, NULL);
Pau Espin Pedrolbfc97562023-04-17 14:49:29 +0200790 ms_set_ms_class(ms, ms_class);
791 ms_set_egprs_ms_class(ms, egprs_ms_class);
Pau Espin Pedrol87573842022-10-26 20:23:45 +0200792 dl_tbf1 = dl_tbf_alloc(bts, ms, 0, false);
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530793 OSMO_ASSERT(dl_tbf1);
794
795 for (int i = 0; i < 8; i++) {
796 if (dl_tbf1->pdch[i])
797 numTs1++;
798 }
799 OSMO_ASSERT(numTs1 == 4);
800 printf("TBF1: numTs(%d)\n", numTs1);
801
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200802 ms = ms_alloc(bts, NULL);
Pau Espin Pedrolbfc97562023-04-17 14:49:29 +0200803 ms_set_ms_class(ms, ms_class);
804 ms_set_egprs_ms_class(ms, egprs_ms_class);
Pau Espin Pedrol87573842022-10-26 20:23:45 +0200805 dl_tbf2 = dl_tbf_alloc(bts, ms, 0, false);
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530806 OSMO_ASSERT(dl_tbf2);
807
808 for (int i = 0; i < 8; i++) {
809 if (dl_tbf2->pdch[i])
810 numTs2++;
811 }
812
813 /*
814 * TODO: currently 2nd DL TBF gets 3 TS
815 * This behaviour will be fixed in subsequent patch
816 */
817 printf("TBF2: numTs(%d)\n", numTs2);
818 OSMO_ASSERT(numTs2 == 3);
819
820 tbf_free(dl_tbf1);
821 tbf_free(dl_tbf2);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100822 talloc_free(bts);
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530823}
824
Oliver Smith3f794702021-08-23 14:19:21 +0200825static void test_bts_pch_timer(void)
826{
827 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100828 struct osmo_mobile_identity mi_imsi1, mi_imsi2;
Pau Espin Pedrol19b33922021-11-08 17:49:52 +0100829 struct osmo_mobile_identity mi_tmsi1;
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100830 mi_imsi1.type = mi_imsi2.type = GSM_MI_TYPE_IMSI;
Pau Espin Pedrol19b33922021-11-08 17:49:52 +0100831 mi_tmsi1.type = GSM_MI_TYPE_TMSI;
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100832 OSMO_STRLCPY_ARRAY(mi_imsi1.imsi, "1234");
833 OSMO_STRLCPY_ARRAY(mi_imsi2.imsi, "5678");
Pau Espin Pedrol19b33922021-11-08 17:49:52 +0100834 mi_tmsi1.tmsi = 987654321;
Oliver Smith3f794702021-08-23 14:19:21 +0200835
836 fprintf(stderr, "Testing bts_pch_timer dealloc on bts dealloc\n");
837 log_set_category_filter(osmo_stderr_target, DPCU, 1, LOGL_DEBUG);
838
839 fprintf(stderr, "Starting PCH timer for 2 IMSI\n");
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100840 bts_pch_timer_start(bts, &mi_imsi1, mi_imsi1.imsi);
841 bts_pch_timer_start(bts, &mi_imsi2, mi_imsi2.imsi);
Pau Espin Pedrol19b33922021-11-08 17:49:52 +0100842 fprintf(stderr, "Starting PCH timer for 1 TMSI\n");
843 bts_pch_timer_start(bts, &mi_tmsi1, "6666");
Oliver Smith3f794702021-08-23 14:19:21 +0200844
845 fprintf(stderr, "Deallocating BTS, expecting the PCH timer to be stopped and deallocated\n");
846 talloc_free(bts);
847}
848
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200849int main(int argc, char **argv)
850{
851 tall_pcu_ctx = talloc_named_const(NULL, 1, "moiji-mobile AllocTest context");
852 if (!tall_pcu_ctx)
853 abort();
854
Neels Hofmeyr78ce5912017-02-08 17:07:31 +0100855 msgb_talloc_ctx_init(tall_pcu_ctx, 0);
Neels Hofmeyr42f2d612018-04-01 16:54:40 +0200856 osmo_init_logging2(tall_pcu_ctx, &gprs_log_info);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200857 log_set_use_color(osmo_stderr_target, 0);
Pau Espin Pedrol00f52cc2021-02-19 14:01:52 +0100858 log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE);
Pau Espin Pedrolb18d2a52021-02-19 14:00:48 +0100859 log_set_print_category(osmo_stderr_target, 0);
860 log_set_print_category_hex(osmo_stderr_target, 0);
Philipp Maierde0e5582020-03-25 12:23:52 +0100861 log_set_category_filter(osmo_stderr_target, DTBF, 1, LOGL_INFO);
Jacob Erlbeck9ec49e22015-06-29 13:00:20 +0200862 if (getenv("LOGL_DEBUG"))
863 log_set_log_level(osmo_stderr_target, LOGL_DEBUG);
Pau Espin Pedroldc2aaac2021-05-14 12:50:46 +0200864 osmo_fsm_log_addr(false);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200865
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100866 the_pcu = gprs_pcu_alloc(tall_pcu_ctx);
867
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200868 test_alloc_a();
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100869 test_alloc_b();
Maxc59ef122017-11-27 13:21:41 +0100870 test_successive_allocations();
871 test_many_connections(alloc_algorithm_a, 160, "A");
Max01bd0cc2018-01-23 20:58:49 +0100872 test_many_connections(alloc_algorithm_b, 32, "B");
Maxc59ef122017-11-27 13:21:41 +0100873 test_many_connections(alloc_algorithm_dynamic, 160, "dynamic");
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530874 test_2_consecutive_dl_tbfs();
Oliver Smith3f794702021-08-23 14:19:21 +0200875 test_bts_pch_timer();
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100876
877 talloc_free(the_pcu);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200878 return EXIT_SUCCESS;
879}
880
881/*
882 * stubs that should not be reached
883 */
884extern "C" {
885void l1if_pdch_req() { abort(); }
886void l1if_connect_pdch() { abort(); }
Philipp Maier72ed3332023-02-27 15:32:00 +0100887void l1if_disconnect_pdch() { abort(); }
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200888void l1if_close_pdch() { abort(); }
889void l1if_open_pdch() { abort(); }
890}