Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 1 | /* GSM Channel allocation routines |
| 2 | * |
| 3 | * (C) 2008 by Harald Welte <laforge@gnumonks.org> |
Holger Freyther | e64a7a3 | 2009-02-06 21:55:37 +0000 | [diff] [blame] | 4 | * (C) 2008, 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 9 | * 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 Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 11 | * (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 Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 16 | * GNU Affero General Public License for more details. |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 17 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 18 | * 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 Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <errno.h> |
Stefan Sperling | 5b6aa65 | 2018-04-09 13:31:51 +0200 | [diff] [blame] | 27 | #include <inttypes.h> |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 28 | |
Neels Hofmeyr | c016479 | 2017-09-04 15:15:32 +0200 | [diff] [blame] | 29 | #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 Hofmeyr | c016479 | 2017-09-04 15:15:32 +0200 | [diff] [blame] | 33 | #include <osmocom/bsc/signal.h> |
Neels Hofmeyr | 31f525e | 2018-05-14 18:14:15 +0200 | [diff] [blame] | 34 | #include <osmocom/bsc/timeslot_fsm.h> |
| 35 | #include <osmocom/bsc/lchan_fsm.h> |
Neels Hofmeyr | 6242742 | 2018-07-24 16:18:15 +0200 | [diff] [blame] | 36 | #include <osmocom/bsc/gsm_04_08_rr.h> |
Pau Espin Pedrol | 388ed58 | 2020-07-15 20:53:16 +0200 | [diff] [blame] | 37 | #include <osmocom/bsc/bts.h> |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 38 | |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 39 | #include <osmocom/core/talloc.h> |
Holger Hans Peter Freyther | 5ba05f4 | 2010-06-22 12:11:59 +0800 | [diff] [blame] | 40 | |
Alexander Chemeris | 6d4ccc1 | 2020-05-07 15:32:52 +0300 | [diff] [blame] | 41 | /* Update channel load calculation for the given BTS */ |
Neels Hofmeyr | 2afffd5 | 2016-09-25 17:01:20 +0200 | [diff] [blame] | 42 | void bts_chan_load(struct pchan_load *cl, const struct gsm_bts *bts) |
Harald Welte | b908cb7 | 2009-12-22 13:09:29 +0100 | [diff] [blame] | 43 | { |
| 44 | struct gsm_bts_trx *trx; |
| 45 | |
| 46 | llist_for_each_entry(trx, &bts->trx_list, list) { |
| 47 | int i; |
| 48 | |
Alexander Chemeris | 29993a9 | 2020-06-06 00:40:56 +0300 | [diff] [blame] | 49 | /* skip administratively deactivated transceivers */ |
Sylvain Munaut | 5438554 | 2020-05-08 09:57:16 +0200 | [diff] [blame] | 50 | if (!trx_is_usable(trx)) |
Harald Welte | b908cb7 | 2009-12-22 13:09:29 +0100 | [diff] [blame] | 51 | continue; |
| 52 | |
| 53 | for (i = 0; i < ARRAY_SIZE(trx->ts); i++) { |
| 54 | struct gsm_bts_trx_ts *ts = &trx->ts[i]; |
Neels Hofmeyr | 31f525e | 2018-05-14 18:14:15 +0200 | [diff] [blame] | 55 | struct load_counter *pl = &cl->pchan[ts->pchan_on_init]; |
| 56 | struct gsm_lchan *lchan; |
Harald Welte | b908cb7 | 2009-12-22 13:09:29 +0100 | [diff] [blame] | 57 | |
| 58 | /* skip administratively deactivated timeslots */ |
Keith | cae79e9 | 2021-04-02 09:27:21 +0200 | [diff] [blame] | 59 | if (!is_ericsson_bts(trx->bts) && !nm_is_running(&ts->mo.nm_state)) |
Harald Welte | b908cb7 | 2009-12-22 13:09:29 +0100 | [diff] [blame] | 60 | continue; |
| 61 | |
Alexander Chemeris | 241f058 | 2020-05-06 02:25:21 +0300 | [diff] [blame] | 62 | /* 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 Hofmeyr | 31f525e | 2018-05-14 18:14:15 +0200 | [diff] [blame] | 84 | ts_for_each_lchan(lchan, ts) { |
Harald Welte | 744886a | 2019-05-23 21:58:21 +0200 | [diff] [blame] | 85 | /* don't even count CBCH slots in total */ |
| 86 | if (lchan->type == GSM_LCHAN_CBCH) |
| 87 | continue; |
| 88 | |
Harald Welte | b908cb7 | 2009-12-22 13:09:29 +0100 | [diff] [blame] | 89 | pl->total++; |
| 90 | |
Alexander Chemeris | a3af93a | 2020-05-17 00:55:01 +0300 | [diff] [blame] | 91 | /* 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 Hofmeyr | 31f525e | 2018-05-14 18:14:15 +0200 | [diff] [blame] | 98 | switch (lchan->fi->state) { |
| 99 | case LCHAN_ST_UNUSED: |
Harald Welte | b908cb7 | 2009-12-22 13:09:29 +0100 | [diff] [blame] | 100 | break; |
| 101 | default: |
| 102 | pl->used++; |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
Alexander Chemeris | 6d4ccc1 | 2020-05-07 15:32:52 +0300 | [diff] [blame] | 110 | /* Update channel load calculation for all BTS in the BSC */ |
Harald Welte | b908cb7 | 2009-12-22 13:09:29 +0100 | [diff] [blame] | 111 | void 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 Hofmeyr | 2afffd5 | 2016-09-25 17:01:20 +0200 | [diff] [blame] | 118 | bts_chan_load(pl, bts); |
Harald Welte | b908cb7 | 2009-12-22 13:09:29 +0100 | [diff] [blame] | 119 | } |
Neels Hofmeyr | 2afffd5 | 2016-09-25 17:01:20 +0200 | [diff] [blame] | 120 | |
Alexander Chemeris | b091def | 2020-05-06 23:17:49 +0300 | [diff] [blame] | 121 | static 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 Sperling | 6cee893 | 2018-01-30 18:14:22 +0100 | [diff] [blame] | 168 | /* Update T3122 wait indicator based on samples of BTS channel load. */ |
| 169 | void |
| 170 | bts_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 Hofmeyr | f802f7f | 2018-02-15 18:46:39 +0100 | [diff] [blame] | 181 | /* 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 Sperling | 6cee893 | 2018-01-30 18:14:22 +0100 | [diff] [blame] | 186 | /* 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 Chemeris | b091def | 2020-05-06 23:17:49 +0300 | [diff] [blame] | 192 | /* Export channel load to stats gauges */ |
| 193 | chan_load_stat_set(i, bts, lc); |
| 194 | |
Stefan Sperling | 6cee893 | 2018-01-30 18:14:22 +0100 | [diff] [blame] | 195 | /* Ignore samples too large for fixed-point calculations (shouldn't happen). */ |
| 196 | if (lc->used > UINT16_MAX || lc->total > UINT16_MAX) { |
Harald Welte | afe987f | 2019-06-13 16:37:24 +0200 | [diff] [blame] | 197 | LOG_BTS(bts, DRLL, LOGL_NOTICE, "numbers in channel load sample " |
| 198 | "too large (used=%u / total=%u)\n", lc->used, lc->total); |
Stefan Sperling | 6cee893 | 2018-01-30 18:14:22 +0100 | [diff] [blame] | 199 | continue; |
| 200 | } |
| 201 | |
| 202 | used += lc->used; |
| 203 | total += lc->total; |
| 204 | } |
| 205 | |
| 206 | /* Check for invalid samples (shouldn't happen). */ |
Harald Welte | c66fd3d | 2020-07-04 11:35:16 +0200 | [diff] [blame] | 207 | if (used > total) { |
Harald Welte | afe987f | 2019-06-13 16:37:24 +0200 | [diff] [blame] | 208 | LOG_BTS(bts, DRLL, LOGL_NOTICE, "bogus channel load sample (used=%"PRIu64" / total=%"PRIu32")\n", |
| 209 | used, total); |
Harald Welte | c66fd3d | 2020-07-04 11:35:16 +0200 | [diff] [blame] | 210 | } |
| 211 | if (total == 0 || used > total) { |
Stefan Sperling | 6cee893 | 2018-01-30 18:14:22 +0100 | [diff] [blame] | 212 | 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 Welte | afe987f | 2019-06-13 16:37:24 +0200 | [diff] [blame] | 239 | LOG_BTS(bts, DRLL, LOGL_DEBUG, "channel load average is %"PRIu64".%.2"PRIu64"%%\n", |
| 240 | (load & 0xffffff00) >> 8, (load & 0xff) / 10); |
Stefan Sperling | 6442e43 | 2018-02-06 14:44:54 +0100 | [diff] [blame] | 241 | 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 Sperling | 6cee893 | 2018-01-30 18:14:22 +0100 | [diff] [blame] | 244 | |
| 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 Welte | afe987f | 2019-06-13 16:37:24 +0200 | [diff] [blame] | 253 | LOG_BTS(bts, DRLL, LOGL_DEBUG, "T3122 wait indicator set to %"PRIu64" seconds\n", wait_ind); |
Stefan Sperling | 6cee893 | 2018-01-30 18:14:22 +0100 | [diff] [blame] | 254 | bts->T3122 = (uint8_t)wait_ind; |
Stefan Sperling | 81dc9e7 | 2018-02-05 17:34:36 +0100 | [diff] [blame] | 255 | osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_T3122], wait_ind); |
Stefan Sperling | 6cee893 | 2018-01-30 18:14:22 +0100 | [diff] [blame] | 256 | } |