blob: 9b80df3f588bda42c2dafa0552d8b7a9a0832b61 [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>
Holger Freytherc6ea9db2008-12-30 19:18:21 +000037
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010038#include <osmocom/core/talloc.h>
Holger Hans Peter Freyther5ba05f42010-06-22 12:11:59 +080039
Alexander Chemeris6d4ccc12020-05-07 15:32:52 +030040/* Update channel load calculation for the given BTS */
Neels Hofmeyr2afffd52016-09-25 17:01:20 +020041void bts_chan_load(struct pchan_load *cl, const struct gsm_bts *bts)
Harald Welteb908cb72009-12-22 13:09:29 +010042{
43 struct gsm_bts_trx *trx;
44
45 llist_for_each_entry(trx, &bts->trx_list, list) {
46 int i;
47
48 /* skip administratively deactivated tranxsceivers */
Sylvain Munaut54385542020-05-08 09:57:16 +020049 if (!trx_is_usable(trx))
Harald Welteb908cb72009-12-22 13:09:29 +010050 continue;
51
52 for (i = 0; i < ARRAY_SIZE(trx->ts); i++) {
53 struct gsm_bts_trx_ts *ts = &trx->ts[i];
Neels Hofmeyr31f525e2018-05-14 18:14:15 +020054 struct load_counter *pl = &cl->pchan[ts->pchan_on_init];
55 struct gsm_lchan *lchan;
Harald Welteb908cb72009-12-22 13:09:29 +010056
57 /* skip administratively deactivated timeslots */
Harald Welted64c0bc2011-05-30 12:07:53 +020058 if (!nm_is_running(&ts->mo.nm_state))
Harald Welteb908cb72009-12-22 13:09:29 +010059 continue;
60
Alexander Chemeris241f0582020-05-06 02:25:21 +030061 /* Dynamic timeslots have to be counted separately
62 * when not in TCH/F or TCH/H mode because they don't
63 * have an lchan's allocated to them. At the same time,
64 * dynamic timeslots in NONE and PDCH modes are same
65 * as in UNUSED mode from the CS channel load perspective
66 * beause they can be switched to TCH mode at any moment.
67 * I.e. they are "available" for TCH. */
68 if ((ts->pchan_on_init == GSM_PCHAN_TCH_F_TCH_H_PDCH ||
69 ts->pchan_on_init == GSM_PCHAN_TCH_F_PDCH) &&
70 (ts->pchan_is == GSM_PCHAN_NONE ||
71 ts->pchan_is == GSM_PCHAN_PDCH)) {
72 pl->total++;
73 }
74
75 /* Count allocated logical channels.
76 * Note: A GSM_PCHAN_TCH_F_TCH_H_PDCH can be switched
77 * to a single TCH/F or to two TCH/H. So when it's in
78 * the TCH/H mode, total number of available channels
79 * is 1 more than when it's in the TCH/F mode.
80 * I.e. "total" count will fluctuate depending on
81 * whether GSM_PCHAN_TCH_F_TCH_H_PDCH timeslot is
82 * in TCH/F or TCH/H (or in NONE/PDCH) mode. */
Neels Hofmeyr31f525e2018-05-14 18:14:15 +020083 ts_for_each_lchan(lchan, ts) {
Harald Welte744886a2019-05-23 21:58:21 +020084 /* don't even count CBCH slots in total */
85 if (lchan->type == GSM_LCHAN_CBCH)
86 continue;
87
Harald Welteb908cb72009-12-22 13:09:29 +010088 pl->total++;
89
Alexander Chemerisa3af93a2020-05-17 00:55:01 +030090 /* lchans under a BORKEN TS should be counted
91 * as used just as BORKEN lchans under a normal TS */
92 if (ts->fi->state == TS_ST_BORKEN) {
93 pl->used++;
94 continue;
95 }
96
Neels Hofmeyr31f525e2018-05-14 18:14:15 +020097 switch (lchan->fi->state) {
98 case LCHAN_ST_UNUSED:
Harald Welteb908cb72009-12-22 13:09:29 +010099 break;
100 default:
101 pl->used++;
102 break;
103 }
104 }
105 }
106 }
107}
108
Alexander Chemeris6d4ccc12020-05-07 15:32:52 +0300109/* Update channel load calculation for all BTS in the BSC */
Harald Welteb908cb72009-12-22 13:09:29 +0100110void network_chan_load(struct pchan_load *pl, struct gsm_network *net)
111{
112 struct gsm_bts *bts;
113
114 memset(pl, 0, sizeof(*pl));
115
116 llist_for_each_entry(bts, &net->bts_list, list)
Neels Hofmeyr2afffd52016-09-25 17:01:20 +0200117 bts_chan_load(pl, bts);
Harald Welteb908cb72009-12-22 13:09:29 +0100118}
Neels Hofmeyr2afffd52016-09-25 17:01:20 +0200119
Alexander Chemerisb091def2020-05-06 23:17:49 +0300120static void chan_load_stat_set(enum gsm_phys_chan_config pchan,
121 struct gsm_bts *bts,
122 struct load_counter *lc)
123{
124 switch (pchan) {
125 case GSM_PCHAN_NONE:
126 case GSM_PCHAN_CCCH:
127 case GSM_PCHAN_PDCH:
128 case GSM_PCHAN_UNKNOWN:
129 break;
130 case GSM_PCHAN_CCCH_SDCCH4:
131 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_CHAN_CCCH_SDCCH4_USED], lc->used);
132 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_CHAN_CCCH_SDCCH4_TOTAL], lc->total);
133 break;
134 case GSM_PCHAN_TCH_F:
135 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_CHAN_TCH_F_USED], lc->used);
136 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_CHAN_TCH_F_TOTAL], lc->total);
137 break;
138 case GSM_PCHAN_TCH_H:
139 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_CHAN_TCH_H_USED], lc->used);
140 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_CHAN_TCH_H_TOTAL], lc->total);
141 break;
142 case GSM_PCHAN_SDCCH8_SACCH8C:
143 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_CHAN_SDCCH8_USED], lc->used);
144 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_CHAN_SDCCH8_TOTAL], lc->total);
145 break;
146 case GSM_PCHAN_TCH_F_PDCH:
147 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_CHAN_TCH_F_PDCH_USED], lc->used);
148 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_CHAN_TCH_F_PDCH_TOTAL], lc->total);
149 break;
150 case GSM_PCHAN_CCCH_SDCCH4_CBCH:
151 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_CHAN_CCCH_SDCCH4_CBCH_USED], lc->used);
152 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_CHAN_CCCH_SDCCH4_CBCH_TOTAL], lc->total);
153 break;
154 case GSM_PCHAN_SDCCH8_SACCH8C_CBCH:
155 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_CHAN_SDCCH8_CBCH_USED], lc->used);
156 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_CHAN_SDCCH8_CBCH_TOTAL], lc->total);
157 break;
158 case GSM_PCHAN_TCH_F_TCH_H_PDCH:
159 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_CHAN_TCH_F_TCH_H_PDCH_USED], lc->used);
160 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_CHAN_TCH_F_TCH_H_PDCH_TOTAL], lc->total);
161 break;
162 default:
163 LOG_BTS(bts, DRLL, LOGL_NOTICE, "Unknown channel type %d\n", pchan);
164 }
165}
166
Stefan Sperling6cee8932018-01-30 18:14:22 +0100167/* Update T3122 wait indicator based on samples of BTS channel load. */
168void
169bts_update_t3122_chan_load(struct gsm_bts *bts)
170{
171 struct pchan_load pl;
172 uint64_t used = 0;
173 uint32_t total = 0;
174 uint64_t load;
175 uint64_t wait_ind;
176 static const uint8_t min_wait_ind = GSM_T3122_DEFAULT;
177 static const uint8_t max_wait_ind = 128; /* max wait ~2 minutes */
178 int i;
179
Neels Hofmeyrf802f7f2018-02-15 18:46:39 +0100180 /* Ignore BTS that are not in operation, in order to not flood the log with "bogus channel load"
181 * messages */
182 if (!trx_is_usable(bts->c0))
183 return;
184
Stefan Sperling6cee8932018-01-30 18:14:22 +0100185 /* Sum up current load across all channels. */
186 memset(&pl, 0, sizeof(pl));
187 bts_chan_load(&pl, bts);
188 for (i = 0; i < ARRAY_SIZE(pl.pchan); i++) {
189 struct load_counter *lc = &pl.pchan[i];
190
Alexander Chemerisb091def2020-05-06 23:17:49 +0300191 /* Export channel load to stats gauges */
192 chan_load_stat_set(i, bts, lc);
193
Stefan Sperling6cee8932018-01-30 18:14:22 +0100194 /* Ignore samples too large for fixed-point calculations (shouldn't happen). */
195 if (lc->used > UINT16_MAX || lc->total > UINT16_MAX) {
Harald Welteafe987f2019-06-13 16:37:24 +0200196 LOG_BTS(bts, DRLL, LOGL_NOTICE, "numbers in channel load sample "
197 "too large (used=%u / total=%u)\n", lc->used, lc->total);
Stefan Sperling6cee8932018-01-30 18:14:22 +0100198 continue;
199 }
200
201 used += lc->used;
202 total += lc->total;
203 }
204
205 /* Check for invalid samples (shouldn't happen). */
Harald Weltec66fd3d2020-07-04 11:35:16 +0200206 if (used > total) {
Harald Welteafe987f2019-06-13 16:37:24 +0200207 LOG_BTS(bts, DRLL, LOGL_NOTICE, "bogus channel load sample (used=%"PRIu64" / total=%"PRIu32")\n",
208 used, total);
Harald Weltec66fd3d2020-07-04 11:35:16 +0200209 }
210 if (total == 0 || used > total) {
Stefan Sperling6cee8932018-01-30 18:14:22 +0100211 bts->T3122 = 0; /* disable override of network-wide default value */
212 bts->chan_load_samples_idx = 0; /* invalidate other samples collected so far */
213 return;
214 }
215
216 /* If we haven't got enough samples yet, store measurement for later use. */
217 if (bts->chan_load_samples_idx < ARRAY_SIZE(bts->chan_load_samples)) {
218 struct load_counter *sample = &bts->chan_load_samples[bts->chan_load_samples_idx++];
219 sample->total = (unsigned int)total;
220 sample->used = (unsigned int)used;
221 return;
222 }
223
224 /* We have enough samples and will overwrite our current samples later. */
225 bts->chan_load_samples_idx = 0;
226
227 /* Add all previous samples to the current sample. */
228 for (i = 0; i < ARRAY_SIZE(bts->chan_load_samples); i++) {
229 struct load_counter *sample = &bts->chan_load_samples[i];
230 total += sample->total;
231 used += sample->used;
232 }
233
234 used <<= 8; /* convert to fixed-point */
235
236 /* Log channel load average. */
237 load = ((used / total) * 100);
Harald Welteafe987f2019-06-13 16:37:24 +0200238 LOG_BTS(bts, DRLL, LOGL_DEBUG, "channel load average is %"PRIu64".%.2"PRIu64"%%\n",
239 (load & 0xffffff00) >> 8, (load & 0xff) / 10);
Stefan Sperling6442e432018-02-06 14:44:54 +0100240 bts->chan_load_avg = ((load & 0xffffff00) >> 8);
241 OSMO_ASSERT(bts->chan_load_avg <= 100);
242 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_CHAN_LOAD_AVERAGE], bts->chan_load_avg);
Stefan Sperling6cee8932018-01-30 18:14:22 +0100243
244 /* Calculate new T3122 wait indicator. */
245 wait_ind = ((used / total) * max_wait_ind);
246 wait_ind >>= 8; /* convert from fixed-point to integer */
247 if (wait_ind < min_wait_ind)
248 wait_ind = min_wait_ind;
249 else if (wait_ind > max_wait_ind)
250 wait_ind = max_wait_ind;
251
Harald Welteafe987f2019-06-13 16:37:24 +0200252 LOG_BTS(bts, DRLL, LOGL_DEBUG, "T3122 wait indicator set to %"PRIu64" seconds\n", wait_ind);
Stefan Sperling6cee8932018-01-30 18:14:22 +0100253 bts->T3122 = (uint8_t)wait_ind;
Stefan Sperling81dc9e72018-02-05 17:34:36 +0100254 osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_T3122], wait_ind);
Stefan Sperling6cee8932018-01-30 18:14:22 +0100255}