blob: 33167024e4b216521a2c1c205f09d756b5ca1615 [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"
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020030#include <osmocom/core/application.h>
31#include <osmocom/core/msgb.h>
32#include <osmocom/core/talloc.h>
33#include <osmocom/core/utils.h>
Pau Espin Pedroldc2aaac2021-05-14 12:50:46 +020034#include <osmocom/core/fsm.h>
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020035}
36
37/* globals used by the code */
38void *tall_pcu_ctx;
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020039int16_t spoof_mnc = 0, spoof_mcc = 0;
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +010040bool spoof_mnc_3_digits = false;
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020041
Daniel Willmann48aa0b02014-07-16 18:54:10 +020042static gprs_rlcmac_tbf *tbf_alloc(struct gprs_rlcmac_bts *bts,
Jacob Erlbecke2e004e2015-06-18 17:16:26 +020043 GprsMs *ms, gprs_rlcmac_tbf_direction dir,
Pau Espin Pedrol322456e2020-05-08 18:15:59 +020044 uint8_t use_trx, bool single_slot)
Daniel Willmann48aa0b02014-07-16 18:54:10 +020045{
Pau Espin Pedrol322456e2020-05-08 18:15:59 +020046 OSMO_ASSERT(ms != NULL);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +020047
Daniel Willmann48aa0b02014-07-16 18:54:10 +020048 if (dir == GPRS_RLCMAC_UL_TBF)
Pau Espin Pedrolbda7bb72022-10-31 14:33:09 +010049 return ul_tbf_alloc(bts, ms, use_trx, single_slot);
Daniel Willmann48aa0b02014-07-16 18:54:10 +020050 else
Pau Espin Pedrol87573842022-10-26 20:23:45 +020051 return dl_tbf_alloc(bts, ms, use_trx, single_slot);
Daniel Willmann48aa0b02014-07-16 18:54:10 +020052}
53
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010054static void check_tfi_usage(struct gprs_rlcmac_bts *bts)
Jacob Erlbeck61205a72015-07-09 11:35:50 +020055{
56 int pdch_no;
Jacob Erlbeck61205a72015-07-09 11:35:50 +020057
58 struct gprs_rlcmac_tbf *tfi_usage[8][8][2][32] = {{{{NULL}}}};
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010059 struct llist_head *tbf_lists[2] = {
Pau Espin Pedrol1a1557a2021-05-13 18:39:36 +020060 &bts->trx[0].ul_tbfs,
61 &bts->trx[0].dl_tbfs
Jacob Erlbeck61205a72015-07-09 11:35:50 +020062 };
63
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010064 struct llist_item *pos;
Jacob Erlbeck61205a72015-07-09 11:35:50 +020065 gprs_rlcmac_tbf *tbf;
Jacob Erlbeck61205a72015-07-09 11:35:50 +020066 unsigned list_idx;
67 struct gprs_rlcmac_tbf **tbf_var;
68
69 for (list_idx = 0; list_idx < ARRAY_SIZE(tbf_lists); list_idx += 1)
70 {
71
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010072 llist_for_each_entry(pos, tbf_lists[list_idx], list) {
73 tbf = (struct gprs_rlcmac_tbf *)pos->entry;
Jacob Erlbeck61205a72015-07-09 11:35:50 +020074 for (pdch_no = 0; pdch_no < 8; pdch_no += 1) {
75 struct gprs_rlcmac_pdch *pdch = tbf->pdch[pdch_no];
76 if (pdch == NULL)
77 continue;
78
79 tbf_var = &tfi_usage
80 [tbf->trx->trx_no]
81 [pdch_no]
82 [tbf->direction]
83 [tbf->tfi()];
84
85 OSMO_ASSERT(*tbf_var == NULL);
86 if (tbf->direction == GPRS_RLCMAC_DL_TBF) {
87 OSMO_ASSERT(pdch->dl_tbf_by_tfi(
88 tbf->tfi()) == tbf);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010089 OSMO_ASSERT(bts_dl_tbf_by_tfi(bts,
Jacob Erlbeck61205a72015-07-09 11:35:50 +020090 tbf->tfi(),
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +020091 tbf->trx->trx_no,
92 pdch_no) == tbf);
Jacob Erlbeck61205a72015-07-09 11:35:50 +020093 } else {
94 OSMO_ASSERT(pdch->ul_tbf_by_tfi(
95 tbf->tfi()) == tbf);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010096 OSMO_ASSERT(bts_ul_tbf_by_tfi(bts,
Jacob Erlbeck61205a72015-07-09 11:35:50 +020097 tbf->tfi(),
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +020098 tbf->trx->trx_no,
99 pdch_no) == tbf);
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200100 }
101 *tbf_var = tbf;
Jacob Erlbeck47a57f62015-07-08 12:53:16 +0200102 OSMO_ASSERT(pdch->assigned_tfi(tbf->direction) &
103 (1 << tbf->tfi()));
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200104 }
105 }
106 }
107}
108
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200109static void test_alloc_a(gprs_rlcmac_tbf_direction dir,
110 uint8_t slots, const int count)
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200111{
112 int tfi;
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200113 int i;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200114 uint8_t used_trx, tmp_trx;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100115 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200116 GprsMs *ms;
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200117 struct gprs_rlcmac_tbf *tbfs[32*8+1] = { 0, };
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200118
119 printf("Testing alloc_a direction(%d)\n", dir);
120
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100121 the_pcu->alloc_algorithm = alloc_algorithm_a;
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200122
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200123 struct gprs_rlcmac_trx *trx = &bts->trx[0];
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200124 for (i = 0; i < 8; i += 1)
125 if (slots & (1 << i))
126 trx->pdch[i].enable();
127
128 OSMO_ASSERT(count >= 0 && count <= (int)ARRAY_SIZE(tbfs));
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200129
130 /**
131 * Currently alloc_a will only allocate from the first
132 * PDCH and all possible usf's. We run out of usf's before
133 * we are out of tfi's. Observe this and make sure that at
134 * least this part is working okay.
135 */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200136 for (i = 0; i < (int)ARRAY_SIZE(tbfs); ++i) {
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100137 ms = bts_alloc_ms(bts, 0, 0);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200138 tbfs[i] = tbf_alloc(bts, ms, dir, -1, 0);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200139 if (tbfs[i] == NULL)
140 break;
141
142 used_trx = tbfs[i]->trx->trx_no;
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100143 tfi = bts_tfi_find_free(bts, dir, &tmp_trx, used_trx);
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200144 OSMO_ASSERT(tbfs[i]->tfi() != tfi);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200145 }
146
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100147 check_tfi_usage(bts);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200148
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200149 OSMO_ASSERT(i == count);
150
Pau Espin Pedrol0dcbc072021-11-10 19:09:10 +0100151 OSMO_ASSERT(bts_all_pdch_allocated(bts));
152
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200153 for (i = 0; i < count; ++i)
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200154 if (tbfs[i])
155 tbf_free(tbfs[i]);
156
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100157 ms = bts_alloc_ms(bts, 0, 0);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200158 tbfs[0] = tbf_alloc(bts, ms, dir, -1, 0);
Holger Hans Peter Freytherf3f1bde2016-02-22 15:14:01 +0100159 OSMO_ASSERT(tbfs[0]);
160 tbf_free(tbfs[0]);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100161 talloc_free(bts);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200162}
163
164static void test_alloc_a()
165{
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200166 /* slots 2 - 3 */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200167 test_alloc_a(GPRS_RLCMAC_DL_TBF, 0x0c, 32*2);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200168 test_alloc_a(GPRS_RLCMAC_UL_TBF, 0x0c, 14);
Jacob Erlbeckfa464bb2015-06-29 12:45:11 +0200169
170 /* slots 1 - 5 */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200171 test_alloc_a(GPRS_RLCMAC_DL_TBF, 0x1e, 32*4);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200172 test_alloc_a(GPRS_RLCMAC_UL_TBF, 0x1e, 28);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200173}
174
Max2ecf0fd2017-11-21 18:13:31 +0100175static void dump_assignment(struct gprs_rlcmac_tbf *tbf, const char *dir, bool verbose)
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100176{
Max2ecf0fd2017-11-21 18:13:31 +0100177 if (!verbose)
178 return;
Pau Espin Pedrol345d9ad2022-12-12 19:22:44 +0100179 const struct GprsMs *ms = tbf_ms(tbf);
Max2ecf0fd2017-11-21 18:13:31 +0100180
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200181 for (size_t i = 0; i < ARRAY_SIZE(tbf->pdch); ++i)
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100182 if (tbf->pdch[i])
Neels Hofmeyrd34646a2017-02-08 17:07:40 +0100183 printf("PDCH[%zu] is used for %s\n", i, dir);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100184 printf("PDCH[%d] is control_ts for %s\n", tbf->control_ts, dir);
Pau Espin Pedrol345d9ad2022-12-12 19:22:44 +0100185 printf("PDCH[%d] is first common for %s\n", ms_first_common_ts(ms), dir);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100186}
187
Max2ecf0fd2017-11-21 18:13:31 +0100188#define ENABLE_PDCH(ts_no, enable_flag, trx) \
189 if (enable_flag) \
190 trx->pdch[ts_no].enable();
191
192static inline void enable_ts_on_bts(struct gprs_rlcmac_bts *bts,
193 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 +0100194{
Max2ecf0fd2017-11-21 18:13:31 +0100195 struct gprs_rlcmac_trx *trx = &bts->trx[0];
196
197 ENABLE_PDCH(0, ts0, trx);
198 ENABLE_PDCH(1, ts1, trx);
199 ENABLE_PDCH(2, ts2, trx);
200 ENABLE_PDCH(3, ts3, trx);
201 ENABLE_PDCH(4, ts4, trx);
202 ENABLE_PDCH(5, ts5, trx);
203 ENABLE_PDCH(6, ts6, trx);
204 ENABLE_PDCH(7, ts7, trx);
205}
206
207static inline bool test_alloc_b_ul_dl(bool ts0, bool ts1, bool ts2, bool ts3, bool ts4, bool ts5, bool ts6, bool ts7,
208 uint8_t ms_class, bool verbose)
209{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100210 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200211 GprsMs *ms;
Max2ecf0fd2017-11-21 18:13:31 +0100212 gprs_rlcmac_ul_tbf *ul_tbf;
213 gprs_rlcmac_dl_tbf *dl_tbf;
214
215 if (verbose)
216 printf("Testing UL then DL assignment.\n");
217
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100218 the_pcu->alloc_algorithm = alloc_algorithm_b;
Max2ecf0fd2017-11-21 18:13:31 +0100219
220 enable_ts_on_bts(bts, ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7);
221
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100222 ms = bts_alloc_ms(bts, ms_class, 0);
Pau Espin Pedrol1e009472021-01-11 20:40:19 +0100223 /* Avoid delaying free to avoid tons of to-be-freed ms objects queuing */
224 ms_set_timeout(ms, 0);
Pau Espin Pedrolbda7bb72022-10-31 14:33:09 +0100225 ul_tbf = ul_tbf_alloc(bts, ms, -1, true);
Max2ecf0fd2017-11-21 18:13:31 +0100226 if (!ul_tbf)
227 return false;
228
229 OSMO_ASSERT(ul_tbf->ms());
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100230 OSMO_ASSERT(ms_current_trx(ul_tbf->ms()));
Max2ecf0fd2017-11-21 18:13:31 +0100231
232 dump_assignment(ul_tbf, "UL", verbose);
233
234 /* assume final ack has not been sent */
Pau Espin Pedrol87573842022-10-26 20:23:45 +0200235 dl_tbf = dl_tbf_alloc(bts, ms, ms_current_trx(ms)->trx_no, false);
Max2ecf0fd2017-11-21 18:13:31 +0100236 if (!dl_tbf)
237 return false;
238
239 dump_assignment(dl_tbf, "DL", verbose);
240
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100241 check_tfi_usage(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100242
243 tbf_free(dl_tbf);
244 tbf_free(ul_tbf);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100245 talloc_free(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100246 return true;
247}
248
249static inline bool test_alloc_b_dl_ul(bool ts0, bool ts1, bool ts2, bool ts3, bool ts4, bool ts5, bool ts6, bool ts7,
250 uint8_t ms_class, bool verbose)
251{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100252 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200253 GprsMs *ms;
Max2ecf0fd2017-11-21 18:13:31 +0100254 gprs_rlcmac_ul_tbf *ul_tbf;
255 gprs_rlcmac_dl_tbf *dl_tbf;
256
257 if (verbose)
258 printf("Testing DL then UL assignment followed by update\n");
259
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100260 the_pcu->alloc_algorithm = alloc_algorithm_b;
Max2ecf0fd2017-11-21 18:13:31 +0100261
262 enable_ts_on_bts(bts, ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7);
263
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100264 ms = bts_alloc_ms(bts, ms_class, 0);
Pau Espin Pedrol1e009472021-01-11 20:40:19 +0100265 /* Avoid delaying free to avoid tons of to-be-freed ms objects queuing */
266 ms_set_timeout(ms, 0);
Pau Espin Pedrol87573842022-10-26 20:23:45 +0200267 dl_tbf = dl_tbf_alloc(bts, ms, -1, true);
Max2ecf0fd2017-11-21 18:13:31 +0100268 if (!dl_tbf)
269 return false;
270
Pau Espin Pedrol8abe13c2022-10-21 18:49:48 +0200271 ms_confirm_tlli(ms, 0x23);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200272 OSMO_ASSERT(dl_tbf->ms() == ms);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100273 OSMO_ASSERT(ms_current_trx(dl_tbf->ms()));
Max2ecf0fd2017-11-21 18:13:31 +0100274
275 dump_assignment(dl_tbf, "DL", verbose);
276
Pau Espin Pedrolbda7bb72022-10-31 14:33:09 +0100277 ul_tbf = ul_tbf_alloc(bts, ms, ms_current_trx(ms)->trx_no, false);
Max2ecf0fd2017-11-21 18:13:31 +0100278 if (!ul_tbf)
279 return false;
280
Pau Espin Pedrol8abe13c2022-10-21 18:49:48 +0200281 ms_update_announced_tlli(ms, 0x23);
Pau Espin Pedrol0f859562022-10-31 10:51:20 +0100282 ul_tbf->m_contention_resolution_done = true;
Max2ecf0fd2017-11-21 18:13:31 +0100283
284 dump_assignment(ul_tbf, "UL", verbose);
285
Max2ecf0fd2017-11-21 18:13:31 +0100286 /* now update the dl_tbf */
287 dl_tbf->update();
288 dump_assignment(dl_tbf, "DL", verbose);
Max2ecf0fd2017-11-21 18:13:31 +0100289
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100290 check_tfi_usage(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100291
292 tbf_free(dl_tbf);
293 tbf_free(ul_tbf);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100294 talloc_free(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100295 return true;
296}
297
298static inline bool test_alloc_b_jolly(uint8_t ms_class)
299{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100300 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200301 GprsMs *ms;
Max2ecf0fd2017-11-21 18:13:31 +0100302 int tfi;
303 uint8_t trx_no;
304 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
305
306 printf("Testing jolly example\n");
307
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100308 the_pcu->alloc_algorithm = alloc_algorithm_b;
Max2ecf0fd2017-11-21 18:13:31 +0100309
310 enable_ts_on_bts(bts, false, true, true, true, true, false, false, false);
311
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100312 tfi = bts_tfi_find_free(bts, GPRS_RLCMAC_UL_TBF, &trx_no, -1);
Max2ecf0fd2017-11-21 18:13:31 +0100313 OSMO_ASSERT(tfi >= 0);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100314 ms = bts_alloc_ms(bts, ms_class, 0);
Pau Espin Pedrol1e009472021-01-11 20:40:19 +0100315 /* Avoid delaying free to avoid tons of to-be-freed ms objects queuing */
316 ms_set_timeout(ms, 0);
Pau Espin Pedrolbda7bb72022-10-31 14:33:09 +0100317 ul_tbf = ul_tbf_alloc(bts, ms, -1, false);
Max2ecf0fd2017-11-21 18:13:31 +0100318 if (!ul_tbf)
319 return false;
320
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200321 OSMO_ASSERT(ul_tbf->ms() == ms);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100322 OSMO_ASSERT(ms_current_trx(ul_tbf->ms()));
323 trx_no = ms_current_trx(ms)->trx_no;
Max2ecf0fd2017-11-21 18:13:31 +0100324 dump_assignment(ul_tbf, "UL", true);
325
326 /* assume final ack has not been sent */
Pau Espin Pedrol87573842022-10-26 20:23:45 +0200327 dl_tbf = dl_tbf_alloc(bts, ms, trx_no, false);
Max2ecf0fd2017-11-21 18:13:31 +0100328 if (!dl_tbf)
329 return false;
330
331 dump_assignment(dl_tbf, "DL", true);
332
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100333 check_tfi_usage(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100334
335 tbf_free(dl_tbf);
336 tbf_free(ul_tbf);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100337 talloc_free(bts);
Max2ecf0fd2017-11-21 18:13:31 +0100338 return true;
339}
340
341static void test_alloc_b_for_ms(uint8_t ms_class)
342{
343 bool rc;
344
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100345 printf("Going to test multislot assignment MS_CLASS=%d\n", ms_class);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100346 /*
347 * PDCH is on TS 6,7,8 and we start with a UL allocation and
348 * then follow two DL allocations (once single, once normal).
349 *
350 * Uplink assigned and still available..
351 */
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100352
Max2ecf0fd2017-11-21 18:13:31 +0100353 rc = test_alloc_b_ul_dl(false, false, false, false, false, true, true, true, ms_class, true);
354 if (!rc)
355 return;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100356
357 /**
358 * Test with the other order.. first DL and then UL
359 */
Max2ecf0fd2017-11-21 18:13:31 +0100360 rc = test_alloc_b_dl_ul(false, false, false, false, false, true, true, true, ms_class, true);
361 if (!rc)
362 return;
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100363
364 /* Andreas osmocom-pcu example */
Max2ecf0fd2017-11-21 18:13:31 +0100365 test_alloc_b_jolly(ms_class);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100366}
367
Max2ecf0fd2017-11-21 18:13:31 +0100368static 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 +0100369{
Max2ecf0fd2017-11-21 18:13:31 +0100370 bool rc;
371
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100372 /* we can test the allocation failures differently */
373 if (!ts0 && !ts1 && !ts2 && !ts3 && !ts4 && !ts5 && !ts6 && !ts7)
374 return;
375
376 printf("Mass test: TS0(%c%c%c%c%c%c%c%c)TS7 MS_Class=%d\n",
377 ts0 ? 'O' : 'x',
378 ts1 ? 'O' : 'x',
379 ts2 ? 'O' : 'x',
380 ts3 ? 'O' : 'x',
381 ts4 ? 'O' : 'x',
382 ts5 ? 'O' : 'x',
383 ts6 ? 'O' : 'x',
384 ts7 ? 'O' : 'x', ms_class);
385 fflush(stdout);
386
Max2ecf0fd2017-11-21 18:13:31 +0100387 rc = test_alloc_b_ul_dl(ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7, ms_class, false);
388 if (!rc)
389 return;
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100390
391 /**
392 * Test with the other order.. first DL and then UL
393 */
Max2ecf0fd2017-11-21 18:13:31 +0100394 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 +0100395}
396
397static void test_all_alloc_b()
398{
399 /* it is a bit crazy... */
400 for (uint8_t ts0 = 0; ts0 < 2; ++ts0)
401 for (uint8_t ts1 = 0; ts1 < 2; ++ts1)
402 for (uint8_t ts2 = 0; ts2 < 2; ++ts2)
403 for (uint8_t ts3 = 0; ts3 < 2; ++ts3)
404 for (uint8_t ts4 = 0; ts4 < 2; ++ts4)
405 for (uint8_t ts5 = 0; ts5 < 2; ++ts5)
406 for (uint8_t ts6 = 0; ts6 < 2; ++ts6)
407 for (uint8_t ts7 = 0; ts7 < 2; ++ts7)
Max9f460712018-01-23 20:57:08 +0100408 for (int ms_class = 0; ms_class < mslot_class_max(); ++ms_class)
Max2ecf0fd2017-11-21 18:13:31 +0100409 test_alloc_mass(ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7, ms_class);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100410}
411
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100412static void test_alloc_b()
413{
Max9f460712018-01-23 20:57:08 +0100414 for (int i = 0; i < mslot_class_max(); ++i)
Max2ecf0fd2017-11-21 18:13:31 +0100415 test_alloc_b_for_ms(i);
Holger Hans Peter Freytherf3eec042013-12-26 10:19:18 +0100416
417 test_all_alloc_b();
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100418}
419
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200420static char get_dir_char(uint8_t mask, uint8_t tx, uint8_t rx, uint8_t busy)
Jacob Erlbecke5655642015-06-29 12:19:52 +0200421{
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200422 int offs = busy ? 32 : 0;
423 return (mask & tx & rx) ? 'C' + offs :
424 (mask & tx) ? 'U' + offs :
425 (mask & rx) ? 'D' + offs :
Jacob Erlbecke5655642015-06-29 12:19:52 +0200426 '.';
427}
428
429enum test_mode {
430 TEST_MODE_UL_ONLY,
431 TEST_MODE_DL_ONLY,
432 TEST_MODE_UL_AND_DL,
433 TEST_MODE_DL_AND_UL,
434 TEST_MODE_DL_AFTER_UL,
435 TEST_MODE_UL_AFTER_DL,
436};
437
Maxc59ef122017-11-27 13:21:41 +0100438static inline char *test_mode_descr(enum test_mode t)
439{
440 switch (t) {
441 case TEST_MODE_UL_ONLY: return (char*)"UL only";
442 case TEST_MODE_DL_ONLY: return (char*)"DL only";
443 case TEST_MODE_UL_AND_DL: return (char*)"UL and DL";
444 case TEST_MODE_DL_AND_UL: return (char*)"DL and UL";
445 case TEST_MODE_DL_AFTER_UL: return (char*)"DL after UL";
446 case TEST_MODE_UL_AFTER_DL: return (char*)"UL after DL";
447 default: return NULL;
448 }
449}
450
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100451static GprsMs *alloc_tbfs(struct gprs_rlcmac_bts *bts, struct GprsMs *old_ms, enum test_mode mode)
Jacob Erlbecke5655642015-06-29 12:19:52 +0200452{
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100453 struct GprsMs *ms, *new_ms;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200454 uint8_t trx_no = -1;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200455
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100456 OSMO_ASSERT(old_ms != NULL);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200457
Jacob Erlbecke5655642015-06-29 12:19:52 +0200458 gprs_rlcmac_tbf *tbf = NULL;
459
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100460 if (ms_current_trx(old_ms))
461 trx_no = ms_current_trx(old_ms)->trx_no;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200462
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100463 ms_ref(old_ms);
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200464
Jacob Erlbecke5655642015-06-29 12:19:52 +0200465 /* Allocate what is needed first */
466 switch (mode) {
467 case TEST_MODE_UL_ONLY:
468 case TEST_MODE_DL_AFTER_UL:
469 case TEST_MODE_UL_AND_DL:
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100470 if (ms_ul_tbf(old_ms))
471 tbf_free(ms_ul_tbf(old_ms));
Pau Espin Pedrolbda7bb72022-10-31 14:33:09 +0100472 tbf = ul_tbf_alloc(bts, old_ms, trx_no, false);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100473 if (tbf == NULL) {
Pau Espin Pedrol0dcbc072021-11-10 19:09:10 +0100474 OSMO_ASSERT(trx_no != -1 || bts_all_pdch_allocated(bts));
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100475 ms_unref(old_ms);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200476 return NULL;
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100477 }
Jacob Erlbecke5655642015-06-29 12:19:52 +0200478 break;
479 case TEST_MODE_DL_ONLY:
480 case TEST_MODE_UL_AFTER_DL:
481 case TEST_MODE_DL_AND_UL:
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100482 if (ms_dl_tbf(old_ms))
483 tbf_free(ms_dl_tbf(old_ms));
Pau Espin Pedrol87573842022-10-26 20:23:45 +0200484 tbf = dl_tbf_alloc(bts, old_ms, trx_no, false);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100485 if (tbf == NULL) {
Pau Espin Pedrol0dcbc072021-11-10 19:09:10 +0100486 OSMO_ASSERT(trx_no != -1 || bts_all_pdch_allocated(bts));
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100487 ms_unref(old_ms);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200488 return NULL;
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100489 }
Jacob Erlbecke5655642015-06-29 12:19:52 +0200490 }
491
492 OSMO_ASSERT(tbf);
493 OSMO_ASSERT(tbf->ms());
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100494 OSMO_ASSERT(old_ms == tbf->ms());
Jacob Erlbecke5655642015-06-29 12:19:52 +0200495 ms = tbf->ms();
496
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100497 ms_ref(ms);
498 new_ms = ms;
Jacob Erlbeck14376a72015-07-07 11:31:28 +0200499 /* Continue with what is needed next */
500 switch (mode) {
501 case TEST_MODE_UL_ONLY:
502 case TEST_MODE_DL_ONLY:
503 /* We are done */
504 break;
505
506 case TEST_MODE_DL_AFTER_UL:
507 case TEST_MODE_UL_AND_DL:
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100508 new_ms = alloc_tbfs(bts, ms, TEST_MODE_DL_ONLY);
Jacob Erlbeck14376a72015-07-07 11:31:28 +0200509 break;
510
511 case TEST_MODE_UL_AFTER_DL:
512 case TEST_MODE_DL_AND_UL:
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100513 new_ms = alloc_tbfs(bts, ms, TEST_MODE_UL_ONLY);
Jacob Erlbeck14376a72015-07-07 11:31:28 +0200514 break;
515 }
516
Jacob Erlbecke5655642015-06-29 12:19:52 +0200517 /* Optionally delete the TBF */
518 switch (mode) {
519 case TEST_MODE_DL_AFTER_UL:
520 case TEST_MODE_UL_AFTER_DL:
521 tbf_free(tbf);
Jacob Erlbeck0f352a62015-07-16 18:23:33 +0200522 tbf = NULL;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200523 break;
524
525 default:
526 break;
527 }
528
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100529 if (!new_ms && tbf)
Jacob Erlbeck0f352a62015-07-16 18:23:33 +0200530 tbf_free(tbf);
531
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100532 ms_unref(old_ms);
533 ms_unref(ms);
534 return new_ms;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200535}
536
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100537static unsigned alloc_many_tbfs(struct gprs_rlcmac_bts *bts, unsigned min_class,
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200538 unsigned max_class, enum test_mode mode)
Jacob Erlbecke5655642015-06-29 12:19:52 +0200539{
Jacob Erlbecke5655642015-06-29 12:19:52 +0200540 unsigned counter;
541 unsigned ms_class = min_class;
542
Jacob Erlbecke5655642015-06-29 12:19:52 +0200543 for (counter = 0; 1; counter += 1) {
544 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
545 uint8_t ul_slots = 0;
546 uint8_t dl_slots = 0;
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200547 uint8_t busy_slots = 0;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200548 unsigned i;
549 int tfi = -1;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200550 int tfi2;
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200551 uint8_t trx_no2;
552 struct gprs_rlcmac_trx *trx;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200553 GprsMs *ms;
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200554 enum gprs_rlcmac_tbf_direction dir;
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200555 uint32_t tlli = counter + 0xc0000000;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200556
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100557 ms = bts_ms_by_tlli(bts, tlli, GSM_RESERVED_TMSI);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200558 if (!ms)
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100559 ms = bts_alloc_ms(bts, 0, 0);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100560 ms_set_ms_class(ms, ms_class);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100561 ms = alloc_tbfs(bts, ms, mode);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200562 if (!ms)
563 break;
564
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100565 ms_set_tlli(ms, tlli);
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200566
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100567 ul_tbf = ms_ul_tbf(ms);
568 dl_tbf = ms_dl_tbf(ms);
569 trx = ms_current_trx(ms);
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200570
571 OSMO_ASSERT(ul_tbf || dl_tbf);
Pau Espin Pedrol345d9ad2022-12-12 19:22:44 +0100572 OSMO_ASSERT(ms_first_common_ts(ms) != TBF_TS_UNSET);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200573 if (ul_tbf) {
Pau Espin Pedrol345d9ad2022-12-12 19:22:44 +0100574 ul_slots = 1 << (uint8_t)ms_first_common_ts(ms);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200575 tfi = ul_tbf->tfi();
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200576 dir = GPRS_RLCMAC_UL_TBF;
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200577 } else {
Pau Espin Pedrol345d9ad2022-12-12 19:22:44 +0100578 ul_slots = 1 << (uint8_t)ms_first_common_ts(ms);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200579 tfi = dl_tbf->tfi();
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200580 dir = GPRS_RLCMAC_DL_TBF;
Jacob Erlbecke5655642015-06-29 12:19:52 +0200581 }
582
583 for (i = 0; dl_tbf && i < ARRAY_SIZE(dl_tbf->pdch); i += 1)
584 if (dl_tbf->pdch[i])
585 dl_slots |= 1 << i;
586
Max5b0df1f2017-09-11 10:38:59 +0200587 for (i = 0; ul_tbf && i < ARRAY_SIZE(ul_tbf->pdch); i += 1)
588 if (ul_tbf->pdch[i])
589 ul_slots |= 1 << i;
590
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200591 for (i = 0; trx && i < ARRAY_SIZE(trx->pdch); i += 1) {
592 struct gprs_rlcmac_pdch *pdch = &trx->pdch[i];
593
594 if (ul_tbf && dl_tbf)
595 continue;
596
597 if (ul_tbf &&
Maxd000d802017-09-20 17:55:28 +0200598 pdch->assigned_tfi(GPRS_RLCMAC_DL_TBF) != NO_FREE_TFI)
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200599 continue;
600
601 if (dl_tbf &&
Maxd000d802017-09-20 17:55:28 +0200602 pdch->assigned_tfi(GPRS_RLCMAC_UL_TBF) != NO_FREE_TFI)
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200603 continue;
604
605 busy_slots |= 1 << i;
606 }
607
Maxd000d802017-09-20 17:55:28 +0200608 printf(" TBF[%d] class %d reserves " OSMO_BIT_SPEC "\n",
Jacob Erlbecke5655642015-06-29 12:19:52 +0200609 tfi, ms_class,
Jacob Erlbeckbf904222015-07-16 18:19:09 +0200610 get_dir_char(0x01, ul_slots, dl_slots, busy_slots),
611 get_dir_char(0x02, ul_slots, dl_slots, busy_slots),
612 get_dir_char(0x04, ul_slots, dl_slots, busy_slots),
613 get_dir_char(0x08, ul_slots, dl_slots, busy_slots),
614 get_dir_char(0x10, ul_slots, dl_slots, busy_slots),
615 get_dir_char(0x20, ul_slots, dl_slots, busy_slots),
616 get_dir_char(0x40, ul_slots, dl_slots, busy_slots),
617 get_dir_char(0x80, ul_slots, dl_slots, busy_slots));
Jacob Erlbecke5655642015-06-29 12:19:52 +0200618
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200619 if (tfi >= 0) {
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100620 OSMO_ASSERT(ms_current_trx(ms));
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100621 tfi2 = bts_tfi_find_free(bts, dir, &trx_no2,
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100622 ms_current_trx(ms)->trx_no);
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200623 OSMO_ASSERT(tfi != tfi2);
Jacob Erlbeck7b3675b2015-07-16 18:28:22 +0200624 OSMO_ASSERT(tfi2 < 0 ||
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100625 trx_no2 == ms_current_trx(ms)->trx_no);
Jacob Erlbeck61205a72015-07-09 11:35:50 +0200626 }
627
Jacob Erlbecke5655642015-06-29 12:19:52 +0200628 ms_class += 1;
629 if (ms_class > max_class)
630 ms_class = min_class;
631 }
632
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200633 return counter;
634}
635
Pau Espin Pedrolc85e0932021-02-25 18:08:10 +0100636static void test_successive_allocation(alloc_algorithm_func_t algo, unsigned min_class,
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200637 unsigned max_class, enum test_mode mode,
638 unsigned expect_num, const char *text)
639{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100640 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200641 struct gprs_rlcmac_trx *trx;
642 unsigned counter;
643
Maxc59ef122017-11-27 13:21:41 +0100644 printf("Going to test assignment with many TBF, algorithm %s class %u..%u (%s)\n",
645 text, min_class, max_class, test_mode_descr(mode));
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200646
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100647 the_pcu->alloc_algorithm = algo;
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200648
649 trx = &bts->trx[0];
650 trx->pdch[3].enable();
651 trx->pdch[4].enable();
652 trx->pdch[5].enable();
653 trx->pdch[6].enable();
654 trx->pdch[7].enable();
655
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100656 counter = alloc_many_tbfs(bts, min_class, max_class, mode);
Jacob Erlbeck69c9bfa2015-07-13 14:38:18 +0200657
Maxc59ef122017-11-27 13:21:41 +0100658 printf(" Successfully allocated %u UL TBFs, algorithm %s class %u..%u (%s)\n",
659 counter, text, min_class, max_class, test_mode_descr(mode));
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200660 if (counter != expect_num)
Maxc59ef122017-11-27 13:21:41 +0100661 fprintf(stderr, " Expected %u TBFs (got %u), algorithm %s class %u..%u (%s)\n",
662 expect_num, counter, text, min_class, max_class, test_mode_descr(mode));
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200663
Jacob Erlbeckec478752015-06-19 16:35:38 +0200664 OSMO_ASSERT(counter == expect_num);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200665
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100666 check_tfi_usage(bts);
667 talloc_free(bts);
Jacob Erlbecke5655642015-06-29 12:19:52 +0200668}
669
Pau Espin Pedrolc85e0932021-02-25 18:08:10 +0100670static void test_many_connections(alloc_algorithm_func_t algo, unsigned expect_num,
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200671 const char *text)
672{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100673 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200674 struct gprs_rlcmac_trx *trx;
675 int counter1, counter2 = -1;
676 unsigned i;
677 enum test_mode mode_seq[] = {
678 TEST_MODE_DL_AFTER_UL,
679 TEST_MODE_UL_ONLY,
680 TEST_MODE_DL_AFTER_UL,
681 TEST_MODE_DL_ONLY,
682 };
683
Maxc59ef122017-11-27 13:21:41 +0100684 printf("Going to test assignment with many connections, algorithm %s\n", text);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200685
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100686 the_pcu->alloc_algorithm = algo;
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200687
688 trx = &bts->trx[0];
689 trx->pdch[3].enable();
690 trx->pdch[4].enable();
691 trx->pdch[5].enable();
692 trx->pdch[6].enable();
693 trx->pdch[7].enable();
694
695 for (i = 0; i < ARRAY_SIZE(mode_seq); i += 1) {
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100696 counter1 = alloc_many_tbfs(bts, 1, mslot_class_max(), mode_seq[i]);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200697 fprintf(stderr, " Allocated %d TBFs (previously %d)\n",
698 counter1, counter2);
699
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100700 check_tfi_usage(bts);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200701
702 /* This will stop earlier due to USF shortage */
703 if (mode_seq[i] == TEST_MODE_UL_ONLY)
704 continue;
705
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200706 if (counter2 >= 0) {
707 if (counter1 < counter2)
708 fprintf(stderr, " Expected %d >= %d in %s\n",
709 counter1, counter2, text);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200710 OSMO_ASSERT(counter1 >= counter2);
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200711 }
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200712
713 counter2 = counter1;
714 }
715
716 printf(" Successfully allocated %d TBFs\n", counter1);
Jacob Erlbeck88fb6132015-07-16 15:01:38 +0200717 if (counter1 != (int)expect_num)
Maxc59ef122017-11-27 13:21:41 +0100718 fprintf(stderr, " Expected %d TBFs (got %d) for algorithm %s\n", expect_num, counter1, text);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200719
720 OSMO_ASSERT(expect_num == (unsigned)counter1);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100721 talloc_free(bts);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200722}
723
Maxc59ef122017-11-27 13:21:41 +0100724static 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 +0200725{
Maxc59ef122017-11-27 13:21:41 +0100726 test_successive_allocation(alloc_algorithm_a, 1, 1, mode, exp_A, "A");
727 test_successive_allocation(alloc_algorithm_b, 10, 10, mode, exp_B, "B");
728 test_successive_allocation(alloc_algorithm_dynamic, 10, 10, mode, exp_dyn, "dynamic");
Jacob Erlbecke5655642015-06-29 12:19:52 +0200729}
730
Maxc59ef122017-11-27 13:21:41 +0100731static void test_successive_allocations()
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200732{
Maxc59ef122017-11-27 13:21:41 +0100733 test_successive_allocation(alloc_algorithm_a, 1, 1, TEST_MODE_UL_AND_DL, 35, "A");
Pau Espin Pedroled2afa32021-02-22 17:20:15 +0100734 test_successive_allocation(alloc_algorithm_b, 10, 10, TEST_MODE_UL_AND_DL, 15, "B");
735 test_successive_allocation(alloc_algorithm_b, 12, 12, TEST_MODE_UL_AND_DL, 15, "B");
Maxc59ef122017-11-27 13:21:41 +0100736
Pau Espin Pedroled2afa32021-02-22 17:20:15 +0100737 test_successive_allocation(alloc_algorithm_b, 1, 12, TEST_MODE_UL_AND_DL, 23, "B");
738 test_successive_allocation(alloc_algorithm_b, 1, mslot_class_max(), TEST_MODE_UL_AND_DL, 17, "B");
739 test_successive_allocation(alloc_algorithm_dynamic, 1, mslot_class_max(), TEST_MODE_UL_AND_DL, 17, "dynamic");
Maxc59ef122017-11-27 13:21:41 +0100740
Pau Espin Pedroled2afa32021-02-22 17:20:15 +0100741 test_a_b_dyn(TEST_MODE_DL_AND_UL, 35, 15, 15);
742 test_a_b_dyn(TEST_MODE_DL_AFTER_UL, 160, 32, 101);
743 test_a_b_dyn(TEST_MODE_UL_AFTER_DL, 35, 15, 15);
744 test_a_b_dyn(TEST_MODE_UL_ONLY, 35, 15, 21);
Maxc59ef122017-11-27 13:21:41 +0100745 test_a_b_dyn(TEST_MODE_DL_ONLY, 160, 32, 101);
Jacob Erlbecka8c2aaf2015-07-13 14:50:08 +0200746}
747
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530748static void test_2_consecutive_dl_tbfs()
749{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100750 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +0200751 GprsMs *ms;
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530752 struct gprs_rlcmac_trx *trx;
753 uint8_t ms_class = 11;
754 uint8_t egprs_ms_class = 11;
755 gprs_rlcmac_tbf *dl_tbf1, *dl_tbf2;
756 uint8_t numTs1 = 0, numTs2 = 0;
757
758 printf("Testing DL TS allocation for Multi UEs\n");
759
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100760 the_pcu->alloc_algorithm = alloc_algorithm_b;
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530761
762 trx = &bts->trx[0];
763 trx->pdch[4].enable();
764 trx->pdch[5].enable();
765 trx->pdch[6].enable();
766 trx->pdch[7].enable();
767
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100768 ms = bts_alloc_ms(bts, ms_class, egprs_ms_class);
Pau Espin Pedrol87573842022-10-26 20:23:45 +0200769 dl_tbf1 = dl_tbf_alloc(bts, ms, 0, false);
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530770 OSMO_ASSERT(dl_tbf1);
771
772 for (int i = 0; i < 8; i++) {
773 if (dl_tbf1->pdch[i])
774 numTs1++;
775 }
776 OSMO_ASSERT(numTs1 == 4);
777 printf("TBF1: numTs(%d)\n", numTs1);
778
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100779 ms = bts_alloc_ms(bts, ms_class, egprs_ms_class);
Pau Espin Pedrol87573842022-10-26 20:23:45 +0200780 dl_tbf2 = dl_tbf_alloc(bts, ms, 0, false);
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530781 OSMO_ASSERT(dl_tbf2);
782
783 for (int i = 0; i < 8; i++) {
784 if (dl_tbf2->pdch[i])
785 numTs2++;
786 }
787
788 /*
789 * TODO: currently 2nd DL TBF gets 3 TS
790 * This behaviour will be fixed in subsequent patch
791 */
792 printf("TBF2: numTs(%d)\n", numTs2);
793 OSMO_ASSERT(numTs2 == 3);
794
795 tbf_free(dl_tbf1);
796 tbf_free(dl_tbf2);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100797 talloc_free(bts);
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530798}
799
Oliver Smith3f794702021-08-23 14:19:21 +0200800static void test_bts_pch_timer(void)
801{
802 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100803 struct osmo_mobile_identity mi_imsi1, mi_imsi2;
Pau Espin Pedrol19b33922021-11-08 17:49:52 +0100804 struct osmo_mobile_identity mi_tmsi1;
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100805 mi_imsi1.type = mi_imsi2.type = GSM_MI_TYPE_IMSI;
Pau Espin Pedrol19b33922021-11-08 17:49:52 +0100806 mi_tmsi1.type = GSM_MI_TYPE_TMSI;
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100807 OSMO_STRLCPY_ARRAY(mi_imsi1.imsi, "1234");
808 OSMO_STRLCPY_ARRAY(mi_imsi2.imsi, "5678");
Pau Espin Pedrol19b33922021-11-08 17:49:52 +0100809 mi_tmsi1.tmsi = 987654321;
Oliver Smith3f794702021-08-23 14:19:21 +0200810
811 fprintf(stderr, "Testing bts_pch_timer dealloc on bts dealloc\n");
812 log_set_category_filter(osmo_stderr_target, DPCU, 1, LOGL_DEBUG);
813
814 fprintf(stderr, "Starting PCH timer for 2 IMSI\n");
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100815 bts_pch_timer_start(bts, &mi_imsi1, mi_imsi1.imsi);
816 bts_pch_timer_start(bts, &mi_imsi2, mi_imsi2.imsi);
Pau Espin Pedrol19b33922021-11-08 17:49:52 +0100817 fprintf(stderr, "Starting PCH timer for 1 TMSI\n");
818 bts_pch_timer_start(bts, &mi_tmsi1, "6666");
Oliver Smith3f794702021-08-23 14:19:21 +0200819
820 fprintf(stderr, "Deallocating BTS, expecting the PCH timer to be stopped and deallocated\n");
821 talloc_free(bts);
822}
823
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200824int main(int argc, char **argv)
825{
826 tall_pcu_ctx = talloc_named_const(NULL, 1, "moiji-mobile AllocTest context");
827 if (!tall_pcu_ctx)
828 abort();
829
Neels Hofmeyr78ce5912017-02-08 17:07:31 +0100830 msgb_talloc_ctx_init(tall_pcu_ctx, 0);
Neels Hofmeyr42f2d612018-04-01 16:54:40 +0200831 osmo_init_logging2(tall_pcu_ctx, &gprs_log_info);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200832 log_set_use_color(osmo_stderr_target, 0);
Pau Espin Pedrol00f52cc2021-02-19 14:01:52 +0100833 log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE);
Pau Espin Pedrolb18d2a52021-02-19 14:00:48 +0100834 log_set_print_category(osmo_stderr_target, 0);
835 log_set_print_category_hex(osmo_stderr_target, 0);
Philipp Maierde0e5582020-03-25 12:23:52 +0100836 log_set_category_filter(osmo_stderr_target, DTBF, 1, LOGL_INFO);
Jacob Erlbeck9ec49e22015-06-29 13:00:20 +0200837 if (getenv("LOGL_DEBUG"))
838 log_set_log_level(osmo_stderr_target, LOGL_DEBUG);
Pau Espin Pedroldc2aaac2021-05-14 12:50:46 +0200839 osmo_fsm_log_addr(false);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200840
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100841 the_pcu = gprs_pcu_alloc(tall_pcu_ctx);
842
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200843 test_alloc_a();
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100844 test_alloc_b();
Maxc59ef122017-11-27 13:21:41 +0100845 test_successive_allocations();
846 test_many_connections(alloc_algorithm_a, 160, "A");
Max01bd0cc2018-01-23 20:58:49 +0100847 test_many_connections(alloc_algorithm_b, 32, "B");
Maxc59ef122017-11-27 13:21:41 +0100848 test_many_connections(alloc_algorithm_dynamic, 160, "dynamic");
Aravind Sirsikare26ee012016-09-06 18:15:45 +0530849 test_2_consecutive_dl_tbfs();
Oliver Smith3f794702021-08-23 14:19:21 +0200850 test_bts_pch_timer();
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100851
852 talloc_free(the_pcu);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200853 return EXIT_SUCCESS;
854}
855
856/*
857 * stubs that should not be reached
858 */
859extern "C" {
860void l1if_pdch_req() { abort(); }
861void l1if_connect_pdch() { abort(); }
862void l1if_close_pdch() { abort(); }
863void l1if_open_pdch() { abort(); }
864}