blob: 2501f609381e978aa91cc37621de97e5a90b3ba7 [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 Pedrol95f8fa12021-03-12 17:50:10 +0100172 return 0;
173}
174
175
176static void test_fn_wrap_around()
177{
178 printf("=== start: %s ===\n", __FUNCTION__);
179 const uint32_t start_fn = GSM_MAX_FN - 40;
180
181 the_pcu->alloc_algorithm = _alloc_algorithm_dummy;
182
Pau Espin Pedrol4f67a9b2021-06-30 16:03:06 +0200183 struct gprs_rlcmac_bts *bts = setup_new_bts();
Pau Espin Pedrol95f8fa12021-03-12 17:50:10 +0100184 struct GprsMs *ms = ms_alloc(bts, 0x12345678);
Pau Espin Pedrol87573842022-10-26 20:23:45 +0200185 struct gprs_rlcmac_tbf *tbf1 = dl_tbf_alloc(bts, ms, 0, true);
Pau Espin Pedrol95f8fa12021-03-12 17:50:10 +0100186 struct gprs_rlcmac_pdch *pdch = &tbf1->trx->pdch[0];
187 int rc;
188 uint32_t fn, last_fn;
189
190 fn = start_fn;
191 while (fn < 40 || fn >= start_fn) {
192 printf("*** RESERVE FN=%" PRIu32 ":\n", fn);
Pau Espin Pedrol86580e12021-03-29 18:15:30 +0200193 rc = pdch_ulc_reserve_tbf_poll(pdch->ulc, fn, tbf1, PDCH_ULC_POLL_UL_ASS);
Pau Espin Pedrol95f8fa12021-03-12 17:50:10 +0100194 OSMO_ASSERT(rc == 0);
195 print_ulc_nodes(pdch->ulc);
196 fn = fn_next_block(fn);
197 }
198 last_fn = fn;
199
200 /* Expiring fn_next_block(start_fn) should only expire first 2 entries here: */
201 fn = fn_next_block(start_fn);
202 printf("*** EXPIRE FN=%" PRIu32 ":\n", fn);
203 pdch_ulc_expire_fn(pdch->ulc, fn);
204 print_ulc_nodes(pdch->ulc);
205
206 /* We should still be able to release FN=0 here, since it came later: */
207 printf("*** RELEASE fn=%" PRIu32 ":\n", 0);
208 rc = pdch_ulc_release_fn(pdch->ulc, 0);
209 print_ulc_nodes(pdch->ulc);
Pau Espin Pedrolc7cc4162021-03-12 18:24:57 +0100210 OSMO_ASSERT(rc == 0);
Pau Espin Pedrol95f8fa12021-03-12 17:50:10 +0100211
212 /* Expiring last FN should expire all entries */
213 printf("*** EXPIRE FN=%" PRIu32 ":\n", last_fn);
214 pdch_ulc_expire_fn(pdch->ulc, last_fn);
215 print_ulc_nodes(pdch->ulc);
216 /* Make sure the store is empty now: */
Pau Espin Pedrolc7cc4162021-03-12 18:24:57 +0100217 OSMO_ASSERT(!rb_first(&pdch->ulc->tree_root));
Pau Espin Pedrol95f8fa12021-03-12 17:50:10 +0100218
219 talloc_free(bts);
220 printf("=== end: %s ===\n", __FUNCTION__);
221}
222
Pau Espin Pedrolce3bd252021-03-29 12:13:13 +0200223static void test_next_free_fn_sba()
224{
225 printf("=== start: %s ===\n", __FUNCTION__);
Pau Espin Pedrol4f67a9b2021-06-30 16:03:06 +0200226 struct gprs_rlcmac_bts *bts = setup_new_bts();
Pau Espin Pedrolce3bd252021-03-29 12:13:13 +0200227 struct gprs_rlcmac_pdch *pdch = &bts->trx[0].pdch[0];
228 struct gprs_rlcmac_sba *sba1, *sba2, *sba3, *sba4;
229
230 pdch->last_rts_fn = 52;
231 printf("*** ALLOC 1 SBA FN=%" PRIu32 ":\n", pdch->last_rts_fn);
232 sba1 = sba_alloc(bts, pdch, 0);
233 print_ulc_nodes(pdch->ulc);
234
235 pdch->last_rts_fn = 65;
236 printf("*** ALLOC 3 SBA FN=%" PRIu32 ":\n", pdch->last_rts_fn);
237 sba2 = sba_alloc(bts, pdch, 0);
238 sba3 = sba_alloc(bts, pdch, 0);
239 sba4 = sba_alloc(bts, pdch, 0);
240 print_ulc_nodes(pdch->ulc);
241 (void)sba1; (void)sba2; (void)sba3; (void)sba4;
242
243 talloc_free(bts);
244 printf("=== end: %s ===\n", __FUNCTION__);
245}
246
Pau Espin Pedrol50a1ede2021-03-29 13:49:43 +0200247static void test_next_free_fn_rrbp()
248{
249 printf("=== start: %s ===\n", __FUNCTION__);
Pau Espin Pedrol4f67a9b2021-06-30 16:03:06 +0200250 struct gprs_rlcmac_bts *bts = setup_new_bts();
Pau Espin Pedrol50a1ede2021-03-29 13:49:43 +0200251 struct gprs_rlcmac_pdch *pdch = &bts->trx[0].pdch[0];
252 struct gprs_rlcmac_sba *sba1;
253 uint32_t poll_fn, curr_fn;
254 unsigned int rrbp;
255 int rc;
256
257 rc = pdch_ulc_get_next_free_rrbp_fn(pdch->ulc, 26, &poll_fn, &rrbp);
258 OSMO_ASSERT(rc == 0);
259 OSMO_ASSERT(poll_fn == 26+13);
260 OSMO_ASSERT(rrbp == RRBP_N_plus_13);
261
262
263 pdch->last_rts_fn = 52;
264 printf("*** ALLOC 1 SBA FN=%" PRIu32 ":\n", pdch->last_rts_fn);
265 sba1 = sba_alloc(bts, pdch, 0); (void)sba1;
266 print_ulc_nodes(pdch->ulc);
267 curr_fn = sba1->fn - 13;
268 rc = pdch_ulc_get_next_free_rrbp_fn(pdch->ulc, curr_fn, &poll_fn, &rrbp);
269 OSMO_ASSERT(rc == 0);
270 printf("***NEXT FREE RRBP FN=%" PRIu32 ":\n", poll_fn);
271 OSMO_ASSERT(poll_fn == (curr_fn+17) || poll_fn == (curr_fn+18));
272 OSMO_ASSERT(rrbp == RRBP_N_plus_17_18);
273
274 pdch->last_rts_fn = fn_next_block(pdch->last_rts_fn);
275 printf("*** ALLOC 1 SBA FN=%" PRIu32 ":\n", pdch->last_rts_fn);
276 sba1 = sba_alloc(bts, pdch, 0); (void)sba1;
277 print_ulc_nodes(pdch->ulc);
278 rc = pdch_ulc_get_next_free_rrbp_fn(pdch->ulc, curr_fn, &poll_fn, &rrbp);
279 OSMO_ASSERT(rc == 0);
280 printf("***NEXT FREE RRBP FN=%" PRIu32 ":\n", poll_fn);
281 OSMO_ASSERT(poll_fn == (curr_fn+21) || poll_fn == (curr_fn+22));
282 OSMO_ASSERT(rrbp == RRBP_N_plus_21_22);
283
284 pdch->last_rts_fn = fn_next_block(pdch->last_rts_fn);
285 printf("*** ALLOC 1 SBA FN=%" PRIu32 ":\n", pdch->last_rts_fn);
286 sba1 = sba_alloc(bts, pdch, 0); (void)sba1;
287 print_ulc_nodes(pdch->ulc);
288 rc = pdch_ulc_get_next_free_rrbp_fn(pdch->ulc, curr_fn, &poll_fn, &rrbp);
289 OSMO_ASSERT(rc == 0);
290 printf("***NEXT FREE RRBP FN=%" PRIu32 ":\n", poll_fn);
291 OSMO_ASSERT(poll_fn == (curr_fn+26));
292 OSMO_ASSERT(rrbp == RRBP_N_plus_26);
293
294 pdch->last_rts_fn = fn_next_block(pdch->last_rts_fn);
295 printf("*** ALLOC 1 SBA FN=%" PRIu32 ":\n", pdch->last_rts_fn);
296 sba1 = sba_alloc(bts, pdch, 0); (void)sba1;
297 print_ulc_nodes(pdch->ulc);
298 rc = pdch_ulc_get_next_free_rrbp_fn(pdch->ulc, curr_fn, &poll_fn, &rrbp);
299 OSMO_ASSERT(rc == -EBUSY);
300
301 talloc_free(bts);
302 printf("=== end: %s ===\n", __FUNCTION__);
303}
304
Pau Espin Pedrol582a15e2021-03-12 15:40:48 +0100305int main(int argc, char **argv)
306{
307 tall_pcu_ctx = talloc_named_const(NULL, 1, "pdch_ulc test context");
308 if (!tall_pcu_ctx)
309 abort();
310
311 msgb_talloc_ctx_init(tall_pcu_ctx, 0);
312 osmo_init_logging2(tall_pcu_ctx, &gprs_log_info);
313 log_set_use_color(osmo_stderr_target, 0);
314 log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE);
315 log_set_log_level(osmo_stderr_target, LOGL_DEBUG);
316 log_set_print_category_hex(osmo_stderr_target, 0);
317 log_set_print_category(osmo_stderr_target, 0);
318 log_parse_category_mask(osmo_stderr_target, "DPCU,1:DRLCMAC,1:DRLCMACUL,1");
319
320 the_pcu = gprs_pcu_alloc(tall_pcu_ctx);
321
322 test_reserve_multiple();
Pau Espin Pedrol95f8fa12021-03-12 17:50:10 +0100323 test_fn_wrap_around();
Pau Espin Pedrolce3bd252021-03-29 12:13:13 +0200324 test_next_free_fn_sba();
Pau Espin Pedrol50a1ede2021-03-29 13:49:43 +0200325 test_next_free_fn_rrbp();
Pau Espin Pedrol582a15e2021-03-12 15:40:48 +0100326 talloc_free(the_pcu);
327 return EXIT_SUCCESS;
328}
329
330/*
331 * stubs that should not be reached
332 */
333int16_t spoof_mnc = 0, spoof_mcc = 0;
334bool spoof_mnc_3_digits = false;
335extern "C" {
336 void l1if_pdch_req() {
337 abort();
338 } void l1if_connect_pdch() {
339 abort();
340 }
341 void l1if_close_pdch() {
342 abort();
343 }
344 void l1if_open_pdch() {
345 abort();
346 }
347}