blob: 68fcb2a447ab9faf09dd5f6310cde5e0b7c7e5d5 [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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20#include "gprs_rlcmac.h"
21#include "gprs_debug.h"
22#include "tbf.h"
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +020023#include "bts.h"
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020024
25#include <string.h>
26#include <stdio.h>
27
28extern "C" {
29#include <osmocom/core/application.h>
30#include <osmocom/core/msgb.h>
31#include <osmocom/core/talloc.h>
32#include <osmocom/core/utils.h>
33}
34
35/* globals used by the code */
36void *tall_pcu_ctx;
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020037int16_t spoof_mnc = 0, spoof_mcc = 0;
38
39static void test_alloc_a(gprs_rlcmac_tbf_direction dir, const int count)
40{
41 int tfi;
42 uint8_t used_trx;
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +020043 BTS the_bts;
44 struct gprs_rlcmac_bts *bts;
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020045 struct gprs_rlcmac_tbf *tbfs[33] = { 0, };
46
47 printf("Testing alloc_a direction(%d)\n", dir);
48
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +020049 bts = the_bts.bts_data();
50 bts->alloc_algorithm = alloc_algorithm_a;
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020051
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +020052 struct gprs_rlcmac_trx *trx = &bts->trx[0];
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +020053 trx->pdch[2].enable();
54 trx->pdch[3].enable();
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020055
56 /**
57 * Currently alloc_a will only allocate from the first
58 * PDCH and all possible usf's. We run out of usf's before
59 * we are out of tfi's. Observe this and make sure that at
60 * least this part is working okay.
61 */
62 for (int i = 0; i < count; ++i) {
63 struct gprs_rlcmac_tbf *tbf;
64
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +020065 tfi = the_bts.tfi_find_free(dir, &used_trx, 0);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020066 OSMO_ASSERT(tfi >= 0);
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +020067 tbfs[i] = tbf_alloc(bts, NULL, dir, tfi, used_trx, 0, 0);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020068 }
69
70 /* Now check that there are still some TFIs */
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +020071 tfi = the_bts.tfi_find_free(dir, &used_trx, 0);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020072 switch (dir) {
73 case GPRS_RLCMAC_UL_TBF:
74 OSMO_ASSERT(tfi >= 0);
75 break;
76 case GPRS_RLCMAC_DL_TBF:
77 OSMO_ASSERT(tfi < 0);
78 break;
79 }
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +020080 OSMO_ASSERT(!tbf_alloc(bts, NULL, dir, tfi, used_trx, 0, 0));
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020081
82 for (int i = 0; i < ARRAY_SIZE(tbfs); ++i)
83 if (tbfs[i])
84 tbf_free(tbfs[i]);
85
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +020086 tfi = the_bts.tfi_find_free(dir, &used_trx, 0);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020087 OSMO_ASSERT(tfi >= 0);
88
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +020089 tbfs[tfi] = tbf_alloc(bts, NULL, dir, tfi, used_trx, 0, 0);
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +020090 OSMO_ASSERT(tbfs[tfi]);
91 tbf_free(tbfs[tfi]);
92}
93
94static void test_alloc_a()
95{
96 test_alloc_a(GPRS_RLCMAC_DL_TBF, 32);
97 test_alloc_a(GPRS_RLCMAC_UL_TBF, 7);
98}
99
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100100static void dump_assignment(struct gprs_rlcmac_tbf *tbf, const char *dir)
101{
102 for (int i = 0; i < ARRAY_SIZE(tbf->pdch); ++i)
103 if (tbf->pdch[i])
104 printf("PDCH[%d] is used for %s\n", i, dir);
105 printf("PDCH[%d] is control_ts for %s\n", tbf->control_ts, dir);
106 printf("PDCH[%d] is first common for %s\n", tbf->first_common_ts, dir);
107}
108
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100109static void test_alloc_b(int ms_class)
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100110{
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100111 printf("Going to test multislot assignment MS_CLASS=%d\n", ms_class);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100112 /*
113 * PDCH is on TS 6,7,8 and we start with a UL allocation and
114 * then follow two DL allocations (once single, once normal).
115 *
116 * Uplink assigned and still available..
117 */
118 {
119 BTS the_bts;
120 struct gprs_rlcmac_bts *bts;
121 struct gprs_rlcmac_trx *trx;
122 int tfi;
123 uint8_t ts_no, trx_no;
124
125 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
126
127 printf("Testing UL then DL assignment.\n");
128
129 bts = the_bts.bts_data();
130 bts->alloc_algorithm = alloc_algorithm_b;
131
132 trx = &bts->trx[0];
133 trx->pdch[5].enable();
134 trx->pdch[6].enable();
135 trx->pdch[7].enable();
136
137 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
138 OSMO_ASSERT(tfi >= 0);
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100139 ul_tbf = tbf_alloc(bts, NULL, GPRS_RLCMAC_UL_TBF, tfi, trx_no, ms_class, 1);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100140 OSMO_ASSERT(ul_tbf);
141 dump_assignment(ul_tbf, "UL");
142
143 /* assume final ack has not been sent */
144 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
145 OSMO_ASSERT(tfi >= 0);
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100146 dl_tbf = tbf_alloc(bts, ul_tbf, GPRS_RLCMAC_DL_TBF, tfi, trx_no, ms_class, 0);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100147 OSMO_ASSERT(dl_tbf);
148 dump_assignment(dl_tbf, "DL");
149
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100150 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
151
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100152 tbf_free(dl_tbf);
153 tbf_free(ul_tbf);
154 }
155
156 /**
157 * Test with the other order.. first DL and then UL
158 */
159 {
160 BTS the_bts;
161 struct gprs_rlcmac_bts *bts;
162 struct gprs_rlcmac_trx *trx;
163 int tfi;
164 uint8_t ts_no, trx_no;
165
166 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
167
168 printf("Testing DL then UL assignment followed by update\n");
169
170 bts = the_bts.bts_data();
171 bts->alloc_algorithm = alloc_algorithm_b;
172
173 trx = &bts->trx[0];
174 trx->pdch[5].enable();
175 trx->pdch[6].enable();
176 trx->pdch[7].enable();
177
178 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
179 OSMO_ASSERT(tfi >= 0);
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100180 dl_tbf = tbf_alloc(bts, NULL, GPRS_RLCMAC_DL_TBF, tfi, trx_no, ms_class, 1);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100181 dl_tbf->m_tlli = 0x23;
182 dl_tbf->m_tlli_valid = true;
183 OSMO_ASSERT(dl_tbf);
184 dump_assignment(dl_tbf, "DL");
185
186 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
187 OSMO_ASSERT(tfi >= 0);
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100188 ul_tbf = tbf_alloc(bts, dl_tbf, GPRS_RLCMAC_UL_TBF, tfi, trx_no, ms_class, 0);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100189 ul_tbf->m_tlli = 0x23;
190 ul_tbf->m_tlli_valid = true;
191 ul_tbf->dir.ul.contention_resolution_done = 1;
192 OSMO_ASSERT(ul_tbf);
193 dump_assignment(ul_tbf, "UL");
194
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100195 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
196
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100197 /* now update the dl_tbf */
198 dl_tbf->update();
199 dump_assignment(dl_tbf, "DL");
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100200 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100201
202 tbf_free(dl_tbf);
203 tbf_free(ul_tbf);
204 }
205
206 /* Andreas osmocom-pcu example */
207 {
208 BTS the_bts;
209 struct gprs_rlcmac_bts *bts;
210 struct gprs_rlcmac_trx *trx;
211 int tfi;
212 uint8_t ts_no, trx_no;
213
214 gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
215
216 printf("Testing jolly example\n");
217
218 bts = the_bts.bts_data();
219 bts->alloc_algorithm = alloc_algorithm_b;
220
221 trx = &bts->trx[0];
222 trx->pdch[1].enable();
223 trx->pdch[2].enable();
224 trx->pdch[3].enable();
225 trx->pdch[4].enable();
226
227 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
228 OSMO_ASSERT(tfi >= 0);
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100229 ul_tbf = tbf_alloc(bts, NULL, GPRS_RLCMAC_UL_TBF, tfi, trx_no, ms_class, 0);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100230 OSMO_ASSERT(ul_tbf);
231 dump_assignment(ul_tbf, "UL");
232
233 /* assume final ack has not been sent */
234 tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
235 OSMO_ASSERT(tfi >= 0);
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100236 dl_tbf = tbf_alloc(bts, ul_tbf, GPRS_RLCMAC_DL_TBF, tfi, trx_no, ms_class, 0);
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100237 OSMO_ASSERT(dl_tbf);
238 dump_assignment(dl_tbf, "DL");
239
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100240 OSMO_ASSERT(dl_tbf->first_common_ts == ul_tbf->first_common_ts);
241
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100242 tbf_free(dl_tbf);
243 tbf_free(ul_tbf);
244 }
245}
246
Holger Hans Peter Freytherc7b998c2013-12-25 19:25:10 +0100247static void test_alloc_b()
248{
249 for (int i = 0; i < 30; ++i)
250 test_alloc_b(i);
251}
252
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200253int main(int argc, char **argv)
254{
255 tall_pcu_ctx = talloc_named_const(NULL, 1, "moiji-mobile AllocTest context");
256 if (!tall_pcu_ctx)
257 abort();
258
259 msgb_set_talloc_ctx(tall_pcu_ctx);
260 osmo_init_logging(&gprs_log_info);
261 log_set_use_color(osmo_stderr_target, 0);
262 log_set_print_filename(osmo_stderr_target, 0);
263
264 test_alloc_a();
Holger Hans Peter Freyther4af30532013-12-25 19:16:55 +0100265 test_alloc_b();
Holger Hans Peter Freytherbfdd5f22013-10-16 17:29:31 +0200266 return EXIT_SUCCESS;
267}
268
269/*
270 * stubs that should not be reached
271 */
272extern "C" {
273void l1if_pdch_req() { abort(); }
274void l1if_connect_pdch() { abort(); }
275void l1if_close_pdch() { abort(); }
276void l1if_open_pdch() { abort(); }
277}