blob: 3de08c4c848929674a9be91cb1ec2602b07299d5 [file] [log] [blame]
Harald Welte8470bf22008-12-25 23:28:35 +00001/* GSM Channel allocation routines
2 *
3 * (C) 2008 by Harald Welte <laforge@gnumonks.org>
Holger Freythere64a7a32009-02-06 21:55:37 +00004 * (C) 2008, 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte8470bf22008-12-25 23:28:35 +00005 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01009 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
Harald Welte8470bf22008-12-25 23:28:35 +000011 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010016 * GNU Affero General Public License for more details.
Harald Welte8470bf22008-12-25 23:28:35 +000017 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010018 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte8470bf22008-12-25 23:28:35 +000020 *
21 */
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <errno.h>
Stefan Sperling5b6aa652018-04-09 13:31:51 +020027#include <inttypes.h>
Harald Welte8470bf22008-12-25 23:28:35 +000028
Neels Hofmeyrc0164792017-09-04 15:15:32 +020029#include <osmocom/bsc/chan_alloc.h>
30#include <osmocom/bsc/abis_nm.h>
31#include <osmocom/bsc/abis_rsl.h>
32#include <osmocom/bsc/debug.h>
Neels Hofmeyrc0164792017-09-04 15:15:32 +020033#include <osmocom/bsc/signal.h>
Neels Hofmeyr31f525e2018-05-14 18:14:15 +020034#include <osmocom/bsc/timeslot_fsm.h>
35#include <osmocom/bsc/lchan_fsm.h>
Neels Hofmeyr62427422018-07-24 16:18:15 +020036#include <osmocom/bsc/gsm_04_08_rr.h>
Pau Espin Pedrol388ed582020-07-15 20:53:16 +020037#include <osmocom/bsc/bts.h>
Holger Freytherc6ea9db2008-12-30 19:18:21 +000038
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010039#include <osmocom/core/talloc.h>
Holger Hans Peter Freyther5ba05f42010-06-22 12:11:59 +080040
Alexander Chemeris6d4ccc12020-05-07 15:32:52 +030041/* Update channel load calculation for the given BTS */
Neels Hofmeyr2afffd52016-09-25 17:01:20 +020042void bts_chan_load(struct pchan_load *cl, const struct gsm_bts *bts)
Harald Welteb908cb72009-12-22 13:09:29 +010043{
44 struct gsm_bts_trx *trx;
45
46 llist_for_each_entry(trx, &bts->trx_list, list) {
47 int i;
48
Alexander Chemeris29993a92020-06-06 00:40:56 +030049 /* skip administratively deactivated transceivers */
Sylvain Munaut54385542020-05-08 09:57:16 +020050 if (!trx_is_usable(trx))
Harald Welteb908cb72009-12-22 13:09:29 +010051 continue;
52
53 for (i = 0; i < ARRAY_SIZE(trx->ts); i++) {
54 struct gsm_bts_trx_ts *ts = &trx->ts[i];
Neels Hofmeyr31f525e2018-05-14 18:14:15 +020055 struct load_counter *pl = &cl->pchan[ts->pchan_on_init];
56 struct gsm_lchan *lchan;
Harald Welteb908cb72009-12-22 13:09:29 +010057
58 /* skip administratively deactivated timeslots */
Keithcae79e92021-04-02 09:27:21 +020059 if (!is_ericsson_bts(trx->bts) && !nm_is_running(&ts->mo.nm_state))
Harald Welteb908cb72009-12-22 13:09:29 +010060 continue;
61
Alexander Chemeris241f0582020-05-06 02:25:21 +030062 /* Dynamic timeslots have to be counted separately
63 * when not in TCH/F or TCH/H mode because they don't
64 * have an lchan's allocated to them. At the same time,
65 * dynamic timeslots in NONE and PDCH modes are same
66 * as in UNUSED mode from the CS channel load perspective
67 * beause they can be switched to TCH mode at any moment.
68 * I.e. they are "available" for TCH. */
69 if ((ts->pchan_on_init == GSM_PCHAN_TCH_F_TCH_H_PDCH ||
70 ts->pchan_on_init == GSM_PCHAN_TCH_F_PDCH) &&
71 (ts->pchan_is == GSM_PCHAN_NONE ||
72 ts->pchan_is == GSM_PCHAN_PDCH)) {
73 pl->total++;
74 }
75
76 /* Count allocated logical channels.
77 * Note: A GSM_PCHAN_TCH_F_TCH_H_PDCH can be switched
78 * to a single TCH/F or to two TCH/H. So when it's in
79 * the TCH/H mode, total number of available channels
80 * is 1 more than when it's in the TCH/F mode.
81 * I.e. "total" count will fluctuate depending on
82 * whether GSM_PCHAN_TCH_F_TCH_H_PDCH timeslot is
83 * in TCH/F or TCH/H (or in NONE/PDCH) mode. */
Neels Hofmeyr31f525e2018-05-14 18:14:15 +020084 ts_for_each_lchan(lchan, ts) {
Harald Welte744886a2019-05-23 21:58:21 +020085 /* don't even count CBCH slots in total */
86 if (lchan->type == GSM_LCHAN_CBCH)
87 continue;
88
Harald Welteb908cb72009-12-22 13:09:29 +010089 pl->total++;
90
Alexander Chemerisa3af93a2020-05-17 00:55:01 +030091 /* lchans under a BORKEN TS should be counted
92 * as used just as BORKEN lchans under a normal TS */
93 if (ts->fi->state == TS_ST_BORKEN) {
94 pl->used++;
95 continue;
96 }
97
Neels Hofmeyr31f525e2018-05-14 18:14:15 +020098 switch (lchan->fi->state) {
99 case LCHAN_ST_UNUSED:
Harald Welteb908cb72009-12-22 13:09:29 +0100100 break;
101 default:
102 pl->used++;
103 break;
104 }
105 }
106 }
107 }
108}
109
Alexander Chemeris6d4ccc12020-05-07 15:32:52 +0300110/* Update channel load calculation for all BTS in the BSC */
Harald Welteb908cb72009-12-22 13:09:29 +0100111void network_chan_load(struct pchan_load *pl, struct gsm_network *net)
112{
113 struct gsm_bts *bts;
114
115 memset(pl, 0, sizeof(*pl));
116
117 llist_for_each_entry(bts, &net->bts_list, list)
Neels Hofmeyr2afffd52016-09-25 17:01:20 +0200118 bts_chan_load(pl, bts);
Harald Welteb908cb72009-12-22 13:09:29 +0100119}
Neels Hofmeyr2afffd52016-09-25 17:01:20 +0200120
Alexander Chemerisb091def2020-05-06 23:17:49 +0300121static void chan_load_stat_set(enum gsm_phys_chan_config pchan,
122 struct gsm_bts *bts,
123 struct load_counter *lc)
124{
125 switch (pchan) {
126 case GSM_PCHAN_NONE:
127 case GSM_PCHAN_CCCH:
128 case GSM_PCHAN_PDCH:
129 case GSM_PCHAN_UNKNOWN:
130 break;
131 case GSM_PCHAN_CCCH_SDCCH4:
132 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_CHAN_CCCH_SDCCH4_USED], lc->used);
133 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_CHAN_CCCH_SDCCH4_TOTAL], lc->total);
134 break;
135 case GSM_PCHAN_TCH_F:
136 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_CHAN_TCH_F_USED], lc->used);
137 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_CHAN_TCH_F_TOTAL], lc->total);
138 break;
139 case GSM_PCHAN_TCH_H:
140 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_CHAN_TCH_H_USED], lc->used);
141 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_CHAN_TCH_H_TOTAL], lc->total);
142 break;
143 case GSM_PCHAN_SDCCH8_SACCH8C:
144 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_CHAN_SDCCH8_USED], lc->used);
145 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_CHAN_SDCCH8_TOTAL], lc->total);
146 break;
147 case GSM_PCHAN_TCH_F_PDCH:
148 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_CHAN_TCH_F_PDCH_USED], lc->used);
149 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_CHAN_TCH_F_PDCH_TOTAL], lc->total);
150 break;
151 case GSM_PCHAN_CCCH_SDCCH4_CBCH:
152 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_CHAN_CCCH_SDCCH4_CBCH_USED], lc->used);
153 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_CHAN_CCCH_SDCCH4_CBCH_TOTAL], lc->total);
154 break;
155 case GSM_PCHAN_SDCCH8_SACCH8C_CBCH:
156 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_CHAN_SDCCH8_CBCH_USED], lc->used);
157 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_CHAN_SDCCH8_CBCH_TOTAL], lc->total);
158 break;
159 case GSM_PCHAN_TCH_F_TCH_H_PDCH:
160 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_CHAN_TCH_F_TCH_H_PDCH_USED], lc->used);
161 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_CHAN_TCH_F_TCH_H_PDCH_TOTAL], lc->total);
162 break;
163 default:
164 LOG_BTS(bts, DRLL, LOGL_NOTICE, "Unknown channel type %d\n", pchan);
165 }
166}
167
Stefan Sperling6cee8932018-01-30 18:14:22 +0100168/* Update T3122 wait indicator based on samples of BTS channel load. */
169void
170bts_update_t3122_chan_load(struct gsm_bts *bts)
171{
172 struct pchan_load pl;
173 uint64_t used = 0;
174 uint32_t total = 0;
175 uint64_t load;
176 uint64_t wait_ind;
177 static const uint8_t min_wait_ind = GSM_T3122_DEFAULT;
178 static const uint8_t max_wait_ind = 128; /* max wait ~2 minutes */
179 int i;
180
Neels Hofmeyrf802f7f2018-02-15 18:46:39 +0100181 /* Ignore BTS that are not in operation, in order to not flood the log with "bogus channel load"
182 * messages */
183 if (!trx_is_usable(bts->c0))
184 return;
185
Stefan Sperling6cee8932018-01-30 18:14:22 +0100186 /* Sum up current load across all channels. */
187 memset(&pl, 0, sizeof(pl));
188 bts_chan_load(&pl, bts);
189 for (i = 0; i < ARRAY_SIZE(pl.pchan); i++) {
190 struct load_counter *lc = &pl.pchan[i];
191
Alexander Chemerisb091def2020-05-06 23:17:49 +0300192 /* Export channel load to stats gauges */
193 chan_load_stat_set(i, bts, lc);
194
Stefan Sperling6cee8932018-01-30 18:14:22 +0100195 /* Ignore samples too large for fixed-point calculations (shouldn't happen). */
196 if (lc->used > UINT16_MAX || lc->total > UINT16_MAX) {
Harald Welteafe987f2019-06-13 16:37:24 +0200197 LOG_BTS(bts, DRLL, LOGL_NOTICE, "numbers in channel load sample "
198 "too large (used=%u / total=%u)\n", lc->used, lc->total);
Stefan Sperling6cee8932018-01-30 18:14:22 +0100199 continue;
200 }
201
202 used += lc->used;
203 total += lc->total;
204 }
205
206 /* Check for invalid samples (shouldn't happen). */
Harald Weltec66fd3d2020-07-04 11:35:16 +0200207 if (used > total) {
Harald Welteafe987f2019-06-13 16:37:24 +0200208 LOG_BTS(bts, DRLL, LOGL_NOTICE, "bogus channel load sample (used=%"PRIu64" / total=%"PRIu32")\n",
209 used, total);
Harald Weltec66fd3d2020-07-04 11:35:16 +0200210 }
211 if (total == 0 || used > total) {
Stefan Sperling6cee8932018-01-30 18:14:22 +0100212 bts->T3122 = 0; /* disable override of network-wide default value */
213 bts->chan_load_samples_idx = 0; /* invalidate other samples collected so far */
214 return;
215 }
216
217 /* If we haven't got enough samples yet, store measurement for later use. */
218 if (bts->chan_load_samples_idx < ARRAY_SIZE(bts->chan_load_samples)) {
219 struct load_counter *sample = &bts->chan_load_samples[bts->chan_load_samples_idx++];
220 sample->total = (unsigned int)total;
221 sample->used = (unsigned int)used;
222 return;
223 }
224
225 /* We have enough samples and will overwrite our current samples later. */
226 bts->chan_load_samples_idx = 0;
227
228 /* Add all previous samples to the current sample. */
229 for (i = 0; i < ARRAY_SIZE(bts->chan_load_samples); i++) {
230 struct load_counter *sample = &bts->chan_load_samples[i];
231 total += sample->total;
232 used += sample->used;
233 }
234
235 used <<= 8; /* convert to fixed-point */
236
237 /* Log channel load average. */
238 load = ((used / total) * 100);
Harald Welteafe987f2019-06-13 16:37:24 +0200239 LOG_BTS(bts, DRLL, LOGL_DEBUG, "channel load average is %"PRIu64".%.2"PRIu64"%%\n",
240 (load & 0xffffff00) >> 8, (load & 0xff) / 10);
Stefan Sperling6442e432018-02-06 14:44:54 +0100241 bts->chan_load_avg = ((load & 0xffffff00) >> 8);
242 OSMO_ASSERT(bts->chan_load_avg <= 100);
243 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_CHAN_LOAD_AVERAGE], bts->chan_load_avg);
Stefan Sperling6cee8932018-01-30 18:14:22 +0100244
245 /* Calculate new T3122 wait indicator. */
246 wait_ind = ((used / total) * max_wait_ind);
247 wait_ind >>= 8; /* convert from fixed-point to integer */
248 if (wait_ind < min_wait_ind)
249 wait_ind = min_wait_ind;
250 else if (wait_ind > max_wait_ind)
251 wait_ind = max_wait_ind;
252
Harald Welteafe987f2019-06-13 16:37:24 +0200253 LOG_BTS(bts, DRLL, LOGL_DEBUG, "T3122 wait indicator set to %"PRIu64" seconds\n", wait_ind);
Stefan Sperling6cee8932018-01-30 18:14:22 +0100254 bts->T3122 = (uint8_t)wait_ind;
Stefan Sperling81dc9e72018-02-05 17:34:36 +0100255 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_T3122], wait_ind);
Stefan Sperling6cee8932018-01-30 18:14:22 +0100256}