blob: 44aef343c3fdc4b5c9c3167ab18d436b51949e4c [file] [log] [blame]
Pau Espin Pedrol582a15e2021-03-12 15:40:48 +01001/* PDCH UL Controller test
2 *
3 * Copyright (C) 2021 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
4 * Author: Pau Espin Pedrol <pespin@sysmocom.de>
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <string.h>
22#include <stdio.h>
Pau Espin Pedrol95f8fa12021-03-12 17:50:10 +010023#include <stdint.h>
24#include <unistd.h>
25#include <inttypes.h>
Pau Espin Pedrol582a15e2021-03-12 15:40:48 +010026
27extern "C" {
28#include <osmocom/core/application.h>
29#include <osmocom/gsm/gsm_utils.h>
30#include <osmocom/core/talloc.h>
31#include <osmocom/core/utils.h>
32}
33
Pau Espin Pedrol95f8fa12021-03-12 17:50:10 +010034#include "gprs_ms.h"
Pau Espin Pedrol582a15e2021-03-12 15:40:48 +010035#include "bts.h"
36#include "sba.h"
37#include "pdch_ul_controller.h"
38
39/* globals used by the code */
40void *tall_pcu_ctx;
41
Pau Espin Pedrol95f8fa12021-03-12 17:50:10 +010042static void print_ulc_nodes(struct pdch_ulc *ulc)
43{
44 struct rb_node *node;
45 for (node = rb_first(&ulc->tree_root); node; node = rb_next(node)) {
46 struct pdch_ulc_node *it = container_of(node, struct pdch_ulc_node, node);
47 printf("FN=%" PRIu32 " type=%s\n",
48 it->fn, get_value_string(pdch_ul_node_names, it->type));
49 }
50}
51
Pau Espin Pedrol4f67a9b2021-06-30 16:03:06 +020052static struct gprs_rlcmac_bts *setup_new_bts(void)
53{
54 struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0);
55 struct gprs_rlcmac_pdch *pdch = &bts->trx[0].pdch[0];
56 pdch->enable();
57 return bts;
58}
59
Pau Espin Pedrol582a15e2021-03-12 15:40:48 +010060static void test_reserve_multiple()
61{
62 printf("=== start: %s ===\n", __FUNCTION__);
63 const uint32_t fn = 20;
Pau Espin Pedrol4f67a9b2021-06-30 16:03:06 +020064 struct gprs_rlcmac_bts *bts = setup_new_bts();
Pau Espin Pedrol582a15e2021-03-12 15:40:48 +010065 struct gprs_rlcmac_pdch *pdch = &bts->trx[0].pdch[0];
66 struct gprs_rlcmac_tbf *tbf1 = (struct gprs_rlcmac_tbf*)0x1234; /*Dummy pointer */
67 struct gprs_rlcmac_tbf *tbf2 = (struct gprs_rlcmac_tbf*)0x5678; /*Dummy pointer */
68 struct gprs_rlcmac_sba *sba1, *sba2;
69 pdch->last_rts_fn = fn; /* This is used by sba_alloc to set + reserve FN */
70 sba1 = sba_alloc(bts, pdch, 0);
71 pdch->last_rts_fn = fn_next_block(pdch->last_rts_fn);
72 sba2 = sba_alloc(bts, pdch, 0);
73 uint32_t tbf1_poll_fn1 = fn_next_block(sba2->fn);
74 uint32_t tbf2_poll_fn1 = fn_next_block(tbf1_poll_fn1);
75 uint32_t tbf1_poll_fn2 = fn_next_block(tbf2_poll_fn1);
76 int rc;
77 struct pdch_ulc_node *node;
78
79 /* SBAs are reserved directly during allocation: */
80 OSMO_ASSERT(pdch_ulc_fn_is_free(pdch->ulc, sba1->fn) == false);
81 OSMO_ASSERT(pdch_ulc_get_sba(pdch->ulc, sba1->fn) == sba1);
82 OSMO_ASSERT(pdch_ulc_fn_is_free(pdch->ulc, sba2->fn) == false);
83 OSMO_ASSERT(pdch_ulc_get_sba(pdch->ulc, sba2->fn) == sba2);
84
85 rc = pdch_ulc_reserve_sba(pdch->ulc, sba1);
86 OSMO_ASSERT(rc == -EEXIST);
87 OSMO_ASSERT(pdch_ulc_fn_is_free(pdch->ulc, sba1->fn) == false);
88 node = pdch_ulc_get_node(pdch->ulc, sba1->fn);
89 OSMO_ASSERT(node->type == PDCH_ULC_NODE_SBA && node->sba.sba == sba1);
90 OSMO_ASSERT(pdch_ulc_fn_is_free(pdch->ulc, sba1->fn) == false);
91
92 rc = pdch_ulc_reserve_sba(pdch->ulc, sba2);
93 OSMO_ASSERT(rc == -EEXIST);
94 OSMO_ASSERT(pdch_ulc_get_sba(pdch->ulc, sba1->fn) == sba1);
95 OSMO_ASSERT(pdch_ulc_get_sba(pdch->ulc, sba2->fn) == sba2);
96 node = pdch_ulc_get_node(pdch->ulc, sba2->fn);
97 OSMO_ASSERT(node->type == PDCH_ULC_NODE_SBA && node->sba.sba == sba2);
98
Pau Espin Pedrol86580e12021-03-29 18:15:30 +020099 rc = pdch_ulc_reserve_tbf_poll(pdch->ulc, sba1->fn, tbf1, PDCH_ULC_POLL_UL_ASS);
Pau Espin Pedrol582a15e2021-03-12 15:40:48 +0100100 OSMO_ASSERT(rc == -EEXIST);
101 OSMO_ASSERT(pdch_ulc_get_tbf_poll(pdch->ulc, sba1->fn) == NULL);
Pau Espin Pedrol86580e12021-03-29 18:15:30 +0200102 rc = pdch_ulc_reserve_tbf_poll(pdch->ulc, sba2->fn, tbf1, PDCH_ULC_POLL_UL_ASS);
Pau Espin Pedrol582a15e2021-03-12 15:40:48 +0100103 OSMO_ASSERT(rc == -EEXIST);
104 OSMO_ASSERT(pdch_ulc_get_tbf_poll(pdch->ulc, sba2->fn) == NULL);
105
106 /* Now Reserve correctly TBF1 */
107 OSMO_ASSERT(pdch_ulc_fn_is_free(pdch->ulc, tbf1_poll_fn1) == true);
Pau Espin Pedrol86580e12021-03-29 18:15:30 +0200108 rc = pdch_ulc_reserve_tbf_poll(pdch->ulc, tbf1_poll_fn1, tbf1, PDCH_ULC_POLL_UL_ASS);
Pau Espin Pedrol582a15e2021-03-12 15:40:48 +0100109 OSMO_ASSERT(rc == 0);
110 OSMO_ASSERT(pdch_ulc_get_tbf_poll(pdch->ulc, tbf1_poll_fn1) == tbf1);
111 OSMO_ASSERT(pdch_ulc_fn_is_free(pdch->ulc, tbf1_poll_fn1) == false);
112 node = pdch_ulc_get_node(pdch->ulc, tbf1_poll_fn1);
113 OSMO_ASSERT(node->type == PDCH_ULC_NODE_TBF_POLL && node->tbf_poll.poll_tbf == tbf1);
114 OSMO_ASSERT(pdch_ulc_fn_is_free(pdch->ulc, tbf1_poll_fn1) == false);
115
116 /* Now reserve correctly TBF2 */
117 OSMO_ASSERT(pdch_ulc_fn_is_free(pdch->ulc, tbf2_poll_fn1) == true);
Pau Espin Pedrol86580e12021-03-29 18:15:30 +0200118 rc = pdch_ulc_reserve_tbf_poll(pdch->ulc, tbf2_poll_fn1, tbf2, PDCH_ULC_POLL_UL_ASS);
Pau Espin Pedrol582a15e2021-03-12 15:40:48 +0100119 OSMO_ASSERT(rc == 0);
120 OSMO_ASSERT(pdch_ulc_get_tbf_poll(pdch->ulc, tbf2_poll_fn1) == tbf2);
121 OSMO_ASSERT(pdch_ulc_fn_is_free(pdch->ulc, tbf2_poll_fn1) == false);
122 node = pdch_ulc_get_node(pdch->ulc, tbf2_poll_fn1);
123 OSMO_ASSERT(node->type == PDCH_ULC_NODE_TBF_POLL && node->tbf_poll.poll_tbf == tbf2);
124 OSMO_ASSERT(pdch_ulc_fn_is_free(pdch->ulc, tbf2_poll_fn1) == false);
125
126 /* Now Reserve TBF1 for POLL again on a later FN, which is totally expected: */
127 OSMO_ASSERT(pdch_ulc_fn_is_free(pdch->ulc, tbf1_poll_fn2) == true);
Pau Espin Pedrol86580e12021-03-29 18:15:30 +0200128 rc = pdch_ulc_reserve_tbf_poll(pdch->ulc, tbf1_poll_fn2, tbf1, PDCH_ULC_POLL_UL_ASS);
Pau Espin Pedrol582a15e2021-03-12 15:40:48 +0100129 OSMO_ASSERT(rc == 0);
130 OSMO_ASSERT(pdch_ulc_get_tbf_poll(pdch->ulc, tbf1_poll_fn2) == tbf1);
131 OSMO_ASSERT(pdch_ulc_fn_is_free(pdch->ulc, tbf1_poll_fn2) == false);
132 node = pdch_ulc_get_node(pdch->ulc, tbf1_poll_fn2);
133 OSMO_ASSERT(node->type == PDCH_ULC_NODE_TBF_POLL && node->tbf_poll.poll_tbf == tbf1);
134 OSMO_ASSERT(pdch_ulc_fn_is_free(pdch->ulc, tbf1_poll_fn2) == false);
135
136 /* Now release them in different ways: */
137 node = pdch_ulc_pop_node(pdch->ulc, sba2->fn);
138 OSMO_ASSERT(node->type == PDCH_ULC_NODE_SBA && node->sba.sba == sba2);
139 OSMO_ASSERT(pdch_ulc_get_sba(pdch->ulc, sba2->fn) == NULL);
140 OSMO_ASSERT(pdch_ulc_fn_is_free(pdch->ulc, sba2->fn) == true);
141 /* This will probably print a warning since in general SBAs are expected
142 * to be released from ULC during sba_free() time: */
143 sba_free(sba2);
144
145 pdch_ulc_expire_fn(pdch->ulc, sba1->fn);
146
147 /* here the 2 tbf1 entries should be removed, so Ul Controller should
148 only have 1 entry for tbf2 after the call: */
149 pdch_ulc_release_tbf(pdch->ulc, tbf1);
150 OSMO_ASSERT(pdch_ulc_get_tbf_poll(pdch->ulc, tbf1_poll_fn1) == NULL);
151 OSMO_ASSERT(pdch_ulc_get_tbf_poll(pdch->ulc, tbf1_poll_fn2) == NULL);
152 OSMO_ASSERT(pdch_ulc_get_tbf_poll(pdch->ulc, tbf2_poll_fn1) == tbf2);
153
154 rc = pdch_ulc_release_fn(pdch->ulc, tbf1_poll_fn1);
155 OSMO_ASSERT(rc == -ENOKEY);
156 rc = pdch_ulc_release_fn(pdch->ulc, tbf1_poll_fn2);
157 OSMO_ASSERT(rc == -ENOKEY);
158 rc = pdch_ulc_release_fn(pdch->ulc, tbf2_poll_fn1);
159 OSMO_ASSERT(rc == 0);
160
161 /* Make sure the store is empty now: */
162 OSMO_ASSERT(!rb_first(&pdch->ulc->tree_root));
163
164 talloc_free(bts);
165 printf("=== end: %s ===\n", __FUNCTION__);
166}
167
Pau Espin Pedrol95f8fa12021-03-12 17:50:10 +0100168int _alloc_algorithm_dummy(struct gprs_rlcmac_bts *bts, struct gprs_rlcmac_tbf *tbf,
169 bool single, int8_t use_tbf)
170{
Pau Espin Pedrol1a1557a2021-05-13 18:39:36 +0200171 tbf->trx = &bts->trx[0];
Pau Espin Pedrol9935d0d2022-12-13 18:29:25 +0100172 ms_set_first_common_ts(tbf_ms(tbf), &tbf->trx->pdch[0]);
Pau Espin Pedrol95f8fa12021-03-12 17:50:10 +0100173 return 0;
174}
175
176
177static void test_fn_wrap_around()
178{
179 printf("=== start: %s ===\n", __FUNCTION__);
180 const uint32_t start_fn = GSM_MAX_FN - 40;
181
182 the_pcu->alloc_algorithm = _alloc_algorithm_dummy;
183
Pau Espin Pedrol4f67a9b2021-06-30 16:03:06 +0200184 struct gprs_rlcmac_bts *bts = setup_new_bts();
Pau Espin Pedrol95f8fa12021-03-12 17:50:10 +0100185 struct GprsMs *ms = ms_alloc(bts, 0x12345678);
Pau Espin Pedrol87573842022-10-26 20:23:45 +0200186 struct gprs_rlcmac_tbf *tbf1 = dl_tbf_alloc(bts, ms, 0, true);
Pau Espin Pedrol95f8fa12021-03-12 17:50:10 +0100187 struct gprs_rlcmac_pdch *pdch = &tbf1->trx->pdch[0];
188 int rc;
189 uint32_t fn, last_fn;
190
191 fn = start_fn;
192 while (fn < 40 || fn >= start_fn) {
193 printf("*** RESERVE FN=%" PRIu32 ":\n", fn);
Pau Espin Pedrol86580e12021-03-29 18:15:30 +0200194 rc = pdch_ulc_reserve_tbf_poll(pdch->ulc, fn, tbf1, PDCH_ULC_POLL_UL_ASS);
Pau Espin Pedrol95f8fa12021-03-12 17:50:10 +0100195 OSMO_ASSERT(rc == 0);
196 print_ulc_nodes(pdch->ulc);
197 fn = fn_next_block(fn);
198 }
199 last_fn = fn;
200
201 /* Expiring fn_next_block(start_fn) should only expire first 2 entries here: */
202 fn = fn_next_block(start_fn);
203 printf("*** EXPIRE FN=%" PRIu32 ":\n", fn);
204 pdch_ulc_expire_fn(pdch->ulc, fn);
205 print_ulc_nodes(pdch->ulc);
206
207 /* We should still be able to release FN=0 here, since it came later: */
208 printf("*** RELEASE fn=%" PRIu32 ":\n", 0);
209 rc = pdch_ulc_release_fn(pdch->ulc, 0);
210 print_ulc_nodes(pdch->ulc);
Pau Espin Pedrolc7cc4162021-03-12 18:24:57 +0100211 OSMO_ASSERT(rc == 0);
Pau Espin Pedrol95f8fa12021-03-12 17:50:10 +0100212
213 /* Expiring last FN should expire all entries */
214 printf("*** EXPIRE FN=%" PRIu32 ":\n", last_fn);
215 pdch_ulc_expire_fn(pdch->ulc, last_fn);
216 print_ulc_nodes(pdch->ulc);
217 /* Make sure the store is empty now: */
Pau Espin Pedrolc7cc4162021-03-12 18:24:57 +0100218 OSMO_ASSERT(!rb_first(&pdch->ulc->tree_root));
Pau Espin Pedrol95f8fa12021-03-12 17:50:10 +0100219
220 talloc_free(bts);
221 printf("=== end: %s ===\n", __FUNCTION__);
222}
223
Pau Espin Pedrolce3bd252021-03-29 12:13:13 +0200224static void test_next_free_fn_sba()
225{
226 printf("=== start: %s ===\n", __FUNCTION__);
Pau Espin Pedrol4f67a9b2021-06-30 16:03:06 +0200227 struct gprs_rlcmac_bts *bts = setup_new_bts();
Pau Espin Pedrolce3bd252021-03-29 12:13:13 +0200228 struct gprs_rlcmac_pdch *pdch = &bts->trx[0].pdch[0];
229 struct gprs_rlcmac_sba *sba1, *sba2, *sba3, *sba4;
230
231 pdch->last_rts_fn = 52;
232 printf("*** ALLOC 1 SBA FN=%" PRIu32 ":\n", pdch->last_rts_fn);
233 sba1 = sba_alloc(bts, pdch, 0);
234 print_ulc_nodes(pdch->ulc);
235
236 pdch->last_rts_fn = 65;
237 printf("*** ALLOC 3 SBA FN=%" PRIu32 ":\n", pdch->last_rts_fn);
238 sba2 = sba_alloc(bts, pdch, 0);
239 sba3 = sba_alloc(bts, pdch, 0);
240 sba4 = sba_alloc(bts, pdch, 0);
241 print_ulc_nodes(pdch->ulc);
242 (void)sba1; (void)sba2; (void)sba3; (void)sba4;
243
244 talloc_free(bts);
245 printf("=== end: %s ===\n", __FUNCTION__);
246}
247
Pau Espin Pedrol50a1ede2021-03-29 13:49:43 +0200248static void test_next_free_fn_rrbp()
249{
250 printf("=== start: %s ===\n", __FUNCTION__);
Pau Espin Pedrol4f67a9b2021-06-30 16:03:06 +0200251 struct gprs_rlcmac_bts *bts = setup_new_bts();
Pau Espin Pedrol50a1ede2021-03-29 13:49:43 +0200252 struct gprs_rlcmac_pdch *pdch = &bts->trx[0].pdch[0];
253 struct gprs_rlcmac_sba *sba1;
254 uint32_t poll_fn, curr_fn;
255 unsigned int rrbp;
256 int rc;
257
258 rc = pdch_ulc_get_next_free_rrbp_fn(pdch->ulc, 26, &poll_fn, &rrbp);
259 OSMO_ASSERT(rc == 0);
260 OSMO_ASSERT(poll_fn == 26+13);
261 OSMO_ASSERT(rrbp == RRBP_N_plus_13);
262
263
264 pdch->last_rts_fn = 52;
265 printf("*** ALLOC 1 SBA FN=%" PRIu32 ":\n", pdch->last_rts_fn);
266 sba1 = sba_alloc(bts, pdch, 0); (void)sba1;
267 print_ulc_nodes(pdch->ulc);
268 curr_fn = sba1->fn - 13;
269 rc = pdch_ulc_get_next_free_rrbp_fn(pdch->ulc, curr_fn, &poll_fn, &rrbp);
270 OSMO_ASSERT(rc == 0);
271 printf("***NEXT FREE RRBP FN=%" PRIu32 ":\n", poll_fn);
272 OSMO_ASSERT(poll_fn == (curr_fn+17) || poll_fn == (curr_fn+18));
273 OSMO_ASSERT(rrbp == RRBP_N_plus_17_18);
274
275 pdch->last_rts_fn = fn_next_block(pdch->last_rts_fn);
276 printf("*** ALLOC 1 SBA FN=%" PRIu32 ":\n", pdch->last_rts_fn);
277 sba1 = sba_alloc(bts, pdch, 0); (void)sba1;
278 print_ulc_nodes(pdch->ulc);
279 rc = pdch_ulc_get_next_free_rrbp_fn(pdch->ulc, curr_fn, &poll_fn, &rrbp);
280 OSMO_ASSERT(rc == 0);
281 printf("***NEXT FREE RRBP FN=%" PRIu32 ":\n", poll_fn);
282 OSMO_ASSERT(poll_fn == (curr_fn+21) || poll_fn == (curr_fn+22));
283 OSMO_ASSERT(rrbp == RRBP_N_plus_21_22);
284
285 pdch->last_rts_fn = fn_next_block(pdch->last_rts_fn);
286 printf("*** ALLOC 1 SBA FN=%" PRIu32 ":\n", pdch->last_rts_fn);
287 sba1 = sba_alloc(bts, pdch, 0); (void)sba1;
288 print_ulc_nodes(pdch->ulc);
289 rc = pdch_ulc_get_next_free_rrbp_fn(pdch->ulc, curr_fn, &poll_fn, &rrbp);
290 OSMO_ASSERT(rc == 0);
291 printf("***NEXT FREE RRBP FN=%" PRIu32 ":\n", poll_fn);
292 OSMO_ASSERT(poll_fn == (curr_fn+26));
293 OSMO_ASSERT(rrbp == RRBP_N_plus_26);
294
295 pdch->last_rts_fn = fn_next_block(pdch->last_rts_fn);
296 printf("*** ALLOC 1 SBA FN=%" PRIu32 ":\n", pdch->last_rts_fn);
297 sba1 = sba_alloc(bts, pdch, 0); (void)sba1;
298 print_ulc_nodes(pdch->ulc);
299 rc = pdch_ulc_get_next_free_rrbp_fn(pdch->ulc, curr_fn, &poll_fn, &rrbp);
300 OSMO_ASSERT(rc == -EBUSY);
301
302 talloc_free(bts);
303 printf("=== end: %s ===\n", __FUNCTION__);
304}
305
Pau Espin Pedrol582a15e2021-03-12 15:40:48 +0100306int main(int argc, char **argv)
307{
308 tall_pcu_ctx = talloc_named_const(NULL, 1, "pdch_ulc test context");
309 if (!tall_pcu_ctx)
310 abort();
311
312 msgb_talloc_ctx_init(tall_pcu_ctx, 0);
313 osmo_init_logging2(tall_pcu_ctx, &gprs_log_info);
314 log_set_use_color(osmo_stderr_target, 0);
315 log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE);
316 log_set_log_level(osmo_stderr_target, LOGL_DEBUG);
317 log_set_print_category_hex(osmo_stderr_target, 0);
318 log_set_print_category(osmo_stderr_target, 0);
319 log_parse_category_mask(osmo_stderr_target, "DPCU,1:DRLCMAC,1:DRLCMACUL,1");
320
321 the_pcu = gprs_pcu_alloc(tall_pcu_ctx);
322
323 test_reserve_multiple();
Pau Espin Pedrol95f8fa12021-03-12 17:50:10 +0100324 test_fn_wrap_around();
Pau Espin Pedrolce3bd252021-03-29 12:13:13 +0200325 test_next_free_fn_sba();
Pau Espin Pedrol50a1ede2021-03-29 13:49:43 +0200326 test_next_free_fn_rrbp();
Pau Espin Pedrol582a15e2021-03-12 15:40:48 +0100327 talloc_free(the_pcu);
328 return EXIT_SUCCESS;
329}
330
331/*
332 * stubs that should not be reached
333 */
334int16_t spoof_mnc = 0, spoof_mcc = 0;
335bool spoof_mnc_3_digits = false;
336extern "C" {
337 void l1if_pdch_req() {
338 abort();
339 } void l1if_connect_pdch() {
340 abort();
Philipp Maier72ed3332023-02-27 15:32:00 +0100341 } void l1if_disconnect_pdch() {
342 abort();
Pau Espin Pedrol582a15e2021-03-12 15:40:48 +0100343 }
344 void l1if_close_pdch() {
345 abort();
346 }
347 void l1if_open_pdch() {
348 abort();
349 }
350}