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> |
| 27 | |
Holger Hans Peter Freyther | 3547045 | 2010-12-22 18:13:00 +0100 | [diff] [blame] | 28 | #include <openbsc/gsm_subscriber.h> |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 29 | #include <openbsc/chan_alloc.h> |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 30 | #include <openbsc/abis_nm.h> |
Holger Freyther | 67b4b9a | 2009-01-01 03:46:11 +0000 | [diff] [blame] | 31 | #include <openbsc/abis_rsl.h> |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 32 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | f95f273 | 2012-01-17 15:27:33 +0100 | [diff] [blame] | 33 | #include <openbsc/rtp_proxy.h> |
Holger Freyther | 7c19f74 | 2009-06-06 13:54:35 +0000 | [diff] [blame] | 34 | #include <openbsc/signal.h> |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 35 | |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 36 | #include <osmocom/core/talloc.h> |
Holger Hans Peter Freyther | 5ba05f4 | 2010-06-22 12:11:59 +0800 | [diff] [blame] | 37 | |
Harald Welte | 4c70454 | 2009-12-24 10:10:16 +0100 | [diff] [blame] | 38 | static int ts_is_usable(struct gsm_bts_trx_ts *ts) |
| 39 | { |
| 40 | /* FIXME: How does this behave for BS-11 ? */ |
| 41 | if (is_ipaccess_bts(ts->trx->bts)) { |
Harald Welte | d64c0bc | 2011-05-30 12:07:53 +0200 | [diff] [blame] | 42 | if (!nm_is_running(&ts->mo.nm_state)) |
Harald Welte | 4c70454 | 2009-12-24 10:10:16 +0100 | [diff] [blame] | 43 | return 0; |
| 44 | } |
| 45 | |
Neels Hofmeyr | c6926d0 | 2016-07-14 02:51:13 +0200 | [diff] [blame] | 46 | /* If a TCH/F_PDCH TS is busy changing, it is already taken or not |
Neels Hofmeyr | 832afa3 | 2016-06-14 13:12:00 +0200 | [diff] [blame] | 47 | * yet available. */ |
| 48 | if (ts->pchan == GSM_PCHAN_TCH_F_PDCH) { |
| 49 | if (ts->flags & TS_F_PDCH_PENDING_MASK) |
| 50 | return 0; |
| 51 | } |
| 52 | |
Neels Hofmeyr | fdd9ad7 | 2016-07-14 03:01:24 +0200 | [diff] [blame] | 53 | /* If a dynamic channel is busy changing, it is already taken or not |
| 54 | * yet available. */ |
| 55 | if (ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH) { |
| 56 | if (ts->dyn.pchan_is != ts->dyn.pchan_want) |
| 57 | return 0; |
| 58 | } |
| 59 | |
Harald Welte | 4c70454 | 2009-12-24 10:10:16 +0100 | [diff] [blame] | 60 | return 1; |
| 61 | } |
| 62 | |
Harald Welte (local) | 82ff397 | 2009-12-28 16:36:28 +0100 | [diff] [blame] | 63 | int trx_is_usable(struct gsm_bts_trx *trx) |
Harald Welte | 4c70454 | 2009-12-24 10:10:16 +0100 | [diff] [blame] | 64 | { |
| 65 | /* FIXME: How does this behave for BS-11 ? */ |
| 66 | if (is_ipaccess_bts(trx->bts)) { |
Harald Welte | d64c0bc | 2011-05-30 12:07:53 +0200 | [diff] [blame] | 67 | if (!nm_is_running(&trx->mo.nm_state) || |
| 68 | !nm_is_running(&trx->bb_transc.mo.nm_state)) |
Harald Welte | 4c70454 | 2009-12-24 10:10:16 +0100 | [diff] [blame] | 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | return 1; |
| 73 | } |
| 74 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 75 | static struct gsm_lchan * |
Neels Hofmeyr | fdd9ad7 | 2016-07-14 03:01:24 +0200 | [diff] [blame] | 76 | _lc_find_trx(struct gsm_bts_trx *trx, enum gsm_phys_chan_config pchan, |
| 77 | enum gsm_phys_chan_config dyn_as_pchan) |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 78 | { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 79 | struct gsm_bts_trx_ts *ts; |
Andreas Eversberg | 0434efa | 2013-10-11 13:05:16 +0200 | [diff] [blame] | 80 | int j, start, stop, dir, ss; |
Neels Hofmeyr | fdd9ad7 | 2016-07-14 03:01:24 +0200 | [diff] [blame] | 81 | int check_subslots; |
Harald Welte | 8836726 | 2009-08-10 13:25:55 +0200 | [diff] [blame] | 82 | |
Harald Welte | 4c70454 | 2009-12-24 10:10:16 +0100 | [diff] [blame] | 83 | if (!trx_is_usable(trx)) |
| 84 | return NULL; |
| 85 | |
Andreas Eversberg | 0434efa | 2013-10-11 13:05:16 +0200 | [diff] [blame] | 86 | if (trx->bts->chan_alloc_reverse) { |
| 87 | /* check TS 7..0 */ |
| 88 | start = 7; |
| 89 | stop = -1; |
| 90 | dir = -1; |
| 91 | } else { |
| 92 | /* check TS 0..7 */ |
| 93 | start = 0; |
| 94 | stop = 8; |
| 95 | dir = 1; |
| 96 | } |
| 97 | |
| 98 | for (j = start; j != stop; j += dir) { |
Harald Welte | fc0d952 | 2009-08-10 13:46:55 +0200 | [diff] [blame] | 99 | ts = &trx->ts[j]; |
Harald Welte | 4c70454 | 2009-12-24 10:10:16 +0100 | [diff] [blame] | 100 | if (!ts_is_usable(ts)) |
| 101 | continue; |
Neels Hofmeyr | a668525 | 2016-06-06 12:53:25 +0200 | [diff] [blame] | 102 | if (ts->pchan != pchan) |
Harald Welte | fc0d952 | 2009-08-10 13:46:55 +0200 | [diff] [blame] | 103 | continue; |
Neels Hofmeyr | fdd9ad7 | 2016-07-14 03:01:24 +0200 | [diff] [blame] | 104 | |
| 105 | /* |
| 106 | * Allocation for fully dynamic timeslots |
| 107 | * (does not apply for ip.access style GSM_PCHAN_TCH_F_PDCH) |
| 108 | * |
| 109 | * Note the special nature of a dynamic timeslot in PDCH mode: |
| 110 | * in PDCH mode, typically, lchan->type is GSM_LCHAN_NONE and |
| 111 | * lchan->state is LCHAN_S_NONE -- an otherwise unused slot |
| 112 | * becomes PDCH implicitly. In the same sense, this channel |
| 113 | * allocator will never be asked to find an available PDCH |
| 114 | * slot; only TCH/F or TCH/H will be requested, and PDCH mode |
| 115 | * means that it is available for switchover. |
| 116 | * |
| 117 | * A dynamic timeslot in PDCH mode may be switched to TCH/F or |
| 118 | * TCH/H. If a dyn TS is already in TCH/F or TCH/H mode, it |
| 119 | * means that it is in use and its mode can't be switched. |
| 120 | * |
| 121 | * The logic concerning channels for TCH/F is trivial: there is |
| 122 | * only one channel, so a dynamic TS in TCH/F mode is already |
| 123 | * taken and not available for allocation. For TCH/H, we need |
| 124 | * to check whether a dynamic timeslot is already in TCH/H mode |
| 125 | * and whether one of the two channels is still available. |
| 126 | */ |
Neels Hofmeyr | e14f4b9 | 2016-12-05 16:48:36 +0100 | [diff] [blame] | 127 | switch (pchan) { |
| 128 | case GSM_PCHAN_TCH_F_TCH_H_PDCH: |
Neels Hofmeyr | fdd9ad7 | 2016-07-14 03:01:24 +0200 | [diff] [blame] | 129 | if (ts->dyn.pchan_is != ts->dyn.pchan_want) { |
| 130 | /* The TS's mode is being switched. Not |
| 131 | * available anymore/yet. */ |
| 132 | DEBUGP(DRLL, "%s already in switchover\n", |
| 133 | gsm_ts_and_pchan_name(ts)); |
| 134 | continue; |
| 135 | } |
| 136 | if (ts->dyn.pchan_is == GSM_PCHAN_PDCH) { |
| 137 | /* This slot is available. Still check for |
| 138 | * error states to be sure; in all cases the |
| 139 | * first lchan will be used. */ |
| 140 | if (ts->lchan->state != LCHAN_S_NONE |
| 141 | && ts->lchan->state != LCHAN_S_ACTIVE) |
| 142 | continue; |
| 143 | return ts->lchan; |
| 144 | } |
Neels Hofmeyr | e14f4b9 | 2016-12-05 16:48:36 +0100 | [diff] [blame] | 145 | if (ts->dyn.pchan_is != dyn_as_pchan) |
| 146 | /* not applicable. */ |
Neels Hofmeyr | fdd9ad7 | 2016-07-14 03:01:24 +0200 | [diff] [blame] | 147 | continue; |
Neels Hofmeyr | e14f4b9 | 2016-12-05 16:48:36 +0100 | [diff] [blame] | 148 | /* The requested type matches the dynamic timeslot's |
| 149 | * current mode. A channel may still be available |
| 150 | * (think TCH/H). */ |
| 151 | check_subslots = ts_subslots(ts); |
| 152 | break; |
| 153 | |
Neels Hofmeyr | 8825c69 | 2016-12-05 16:50:47 +0100 | [diff] [blame] | 154 | case GSM_PCHAN_TCH_F_PDCH: |
| 155 | /* Available for voice when in PDCH mode */ |
| 156 | if (ts_pchan(ts) != GSM_PCHAN_PDCH) |
| 157 | continue; |
| 158 | /* Subslots of a PDCH ts don't need to be checked. */ |
| 159 | return ts->lchan; |
| 160 | |
Neels Hofmeyr | e14f4b9 | 2016-12-05 16:48:36 +0100 | [diff] [blame] | 161 | default: |
Neels Hofmeyr | fdd9ad7 | 2016-07-14 03:01:24 +0200 | [diff] [blame] | 162 | /* Not a dynamic channel, there is only one pchan kind: */ |
Neels Hofmeyr | 5486025 | 2016-07-29 18:13:38 +0200 | [diff] [blame] | 163 | check_subslots = ts_subslots(ts); |
Neels Hofmeyr | e14f4b9 | 2016-12-05 16:48:36 +0100 | [diff] [blame] | 164 | break; |
Neels Hofmeyr | fdd9ad7 | 2016-07-14 03:01:24 +0200 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /* Is a sub-slot still available? */ |
| 168 | for (ss = 0; ss < check_subslots; ss++) { |
Harald Welte | fc0d952 | 2009-08-10 13:46:55 +0200 | [diff] [blame] | 169 | struct gsm_lchan *lc = &ts->lchan[ss]; |
Harald Welte (local) | 3e46031 | 2009-12-27 18:12:29 +0100 | [diff] [blame] | 170 | if (lc->type == GSM_LCHAN_NONE && |
| 171 | lc->state == LCHAN_S_NONE) |
Harald Welte | fc0d952 | 2009-08-10 13:46:55 +0200 | [diff] [blame] | 172 | return lc; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 173 | } |
| 174 | } |
Harald Welte | c0d83b0 | 2010-03-28 15:58:03 +0800 | [diff] [blame] | 175 | |
Harald Welte | fc0d952 | 2009-08-10 13:46:55 +0200 | [diff] [blame] | 176 | return NULL; |
| 177 | } |
| 178 | |
| 179 | static struct gsm_lchan * |
Neels Hofmeyr | fdd9ad7 | 2016-07-14 03:01:24 +0200 | [diff] [blame] | 180 | _lc_dyn_find_bts(struct gsm_bts *bts, enum gsm_phys_chan_config pchan, |
| 181 | enum gsm_phys_chan_config dyn_as_pchan) |
Harald Welte | fc0d952 | 2009-08-10 13:46:55 +0200 | [diff] [blame] | 182 | { |
| 183 | struct gsm_bts_trx *trx; |
Harald Welte | fc0d952 | 2009-08-10 13:46:55 +0200 | [diff] [blame] | 184 | struct gsm_lchan *lc; |
| 185 | |
| 186 | if (bts->chan_alloc_reverse) { |
| 187 | llist_for_each_entry_reverse(trx, &bts->trx_list, list) { |
Neels Hofmeyr | fdd9ad7 | 2016-07-14 03:01:24 +0200 | [diff] [blame] | 188 | lc = _lc_find_trx(trx, pchan, dyn_as_pchan); |
Harald Welte | fc0d952 | 2009-08-10 13:46:55 +0200 | [diff] [blame] | 189 | if (lc) |
| 190 | return lc; |
| 191 | } |
| 192 | } else { |
| 193 | llist_for_each_entry(trx, &bts->trx_list, list) { |
Neels Hofmeyr | fdd9ad7 | 2016-07-14 03:01:24 +0200 | [diff] [blame] | 194 | lc = _lc_find_trx(trx, pchan, dyn_as_pchan); |
Harald Welte | fc0d952 | 2009-08-10 13:46:55 +0200 | [diff] [blame] | 195 | if (lc) |
| 196 | return lc; |
| 197 | } |
| 198 | } |
| 199 | |
Harald Welte | f86852c | 2015-01-01 12:46:26 +0100 | [diff] [blame] | 200 | return NULL; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Neels Hofmeyr | fdd9ad7 | 2016-07-14 03:01:24 +0200 | [diff] [blame] | 203 | static struct gsm_lchan * |
| 204 | _lc_find_bts(struct gsm_bts *bts, enum gsm_phys_chan_config pchan) |
| 205 | { |
| 206 | return _lc_dyn_find_bts(bts, pchan, GSM_PCHAN_NONE); |
| 207 | } |
| 208 | |
Neels Hofmeyr | a668525 | 2016-06-06 12:53:25 +0200 | [diff] [blame] | 209 | /* Allocate a logical channel. |
| 210 | * |
Neels Hofmeyr | c6926d0 | 2016-07-14 02:51:13 +0200 | [diff] [blame] | 211 | * Dynamic channel types: we always prefer a dedicated TS, and only pick + |
| 212 | * switch a dynamic TS if no pure TS of the requested PCHAN is available. |
| 213 | * |
| 214 | * TCH_F/PDCH: if we pick a PDCH ACT style dynamic TS as TCH/F channel, PDCH |
| 215 | * will be disabled in rsl_chan_activate_lchan(); there is no need to check |
| 216 | * whether PDCH mode is currently active, here. |
Neels Hofmeyr | a668525 | 2016-06-06 12:53:25 +0200 | [diff] [blame] | 217 | */ |
Holger Hans Peter Freyther | 457c2a8 | 2010-09-06 08:58:42 +0800 | [diff] [blame] | 218 | struct gsm_lchan *lchan_alloc(struct gsm_bts *bts, enum gsm_chan_t type, |
| 219 | int allow_bigger) |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 220 | { |
| 221 | struct gsm_lchan *lchan = NULL; |
Harald Welte | 30f1f37 | 2014-12-28 15:00:45 +0100 | [diff] [blame] | 222 | enum gsm_phys_chan_config first, first_cbch, second, second_cbch; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 223 | |
| 224 | switch (type) { |
| 225 | case GSM_LCHAN_SDCCH: |
Harald Welte | 65676fe | 2009-08-10 14:44:24 +0200 | [diff] [blame] | 226 | if (bts->chan_alloc_reverse) { |
| 227 | first = GSM_PCHAN_SDCCH8_SACCH8C; |
Harald Welte | 30f1f37 | 2014-12-28 15:00:45 +0100 | [diff] [blame] | 228 | first_cbch = GSM_PCHAN_SDCCH8_SACCH8C_CBCH; |
Harald Welte | 65676fe | 2009-08-10 14:44:24 +0200 | [diff] [blame] | 229 | second = GSM_PCHAN_CCCH_SDCCH4; |
Harald Welte | 30f1f37 | 2014-12-28 15:00:45 +0100 | [diff] [blame] | 230 | second_cbch = GSM_PCHAN_CCCH_SDCCH4_CBCH; |
Harald Welte | 65676fe | 2009-08-10 14:44:24 +0200 | [diff] [blame] | 231 | } else { |
| 232 | first = GSM_PCHAN_CCCH_SDCCH4; |
Harald Welte | 30f1f37 | 2014-12-28 15:00:45 +0100 | [diff] [blame] | 233 | first_cbch = GSM_PCHAN_CCCH_SDCCH4_CBCH; |
Harald Welte | 65676fe | 2009-08-10 14:44:24 +0200 | [diff] [blame] | 234 | second = GSM_PCHAN_SDCCH8_SACCH8C; |
Harald Welte | 30f1f37 | 2014-12-28 15:00:45 +0100 | [diff] [blame] | 235 | second_cbch = GSM_PCHAN_SDCCH8_SACCH8C_CBCH; |
Harald Welte | 65676fe | 2009-08-10 14:44:24 +0200 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | lchan = _lc_find_bts(bts, first); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 239 | if (lchan == NULL) |
Harald Welte | 30f1f37 | 2014-12-28 15:00:45 +0100 | [diff] [blame] | 240 | lchan = _lc_find_bts(bts, first_cbch); |
| 241 | if (lchan == NULL) |
Harald Welte | 65676fe | 2009-08-10 14:44:24 +0200 | [diff] [blame] | 242 | lchan = _lc_find_bts(bts, second); |
Harald Welte | 30f1f37 | 2014-12-28 15:00:45 +0100 | [diff] [blame] | 243 | if (lchan == NULL) |
| 244 | lchan = _lc_find_bts(bts, second_cbch); |
Holger Hans Peter Freyther | 457c2a8 | 2010-09-06 08:58:42 +0800 | [diff] [blame] | 245 | |
| 246 | /* allow to assign bigger channels */ |
| 247 | if (allow_bigger) { |
| 248 | if (lchan == NULL) { |
| 249 | lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_H); |
Neels Hofmeyr | f5713a5 | 2016-06-06 12:57:22 +0200 | [diff] [blame] | 250 | if (lchan) |
| 251 | type = GSM_LCHAN_TCH_H; |
Holger Hans Peter Freyther | 457c2a8 | 2010-09-06 08:58:42 +0800 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | if (lchan == NULL) { |
| 255 | lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_F); |
Neels Hofmeyr | f5713a5 | 2016-06-06 12:57:22 +0200 | [diff] [blame] | 256 | if (lchan) |
| 257 | type = GSM_LCHAN_TCH_F; |
Holger Hans Peter Freyther | 457c2a8 | 2010-09-06 08:58:42 +0800 | [diff] [blame] | 258 | } |
Neels Hofmeyr | a668525 | 2016-06-06 12:53:25 +0200 | [diff] [blame] | 259 | |
| 260 | /* try dynamic TCH/F_PDCH */ |
| 261 | if (lchan == NULL) { |
| 262 | lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_F_PDCH); |
| 263 | /* TCH/F_PDCH will be used as TCH/F */ |
| 264 | if (lchan) |
| 265 | type = GSM_LCHAN_TCH_F; |
| 266 | } |
Neels Hofmeyr | fdd9ad7 | 2016-07-14 03:01:24 +0200 | [diff] [blame] | 267 | |
| 268 | /* try fully dynamic TCH/F_TCH/H_PDCH */ |
| 269 | if (lchan == NULL) { |
| 270 | lchan = _lc_dyn_find_bts(bts, GSM_PCHAN_TCH_F_TCH_H_PDCH, |
| 271 | GSM_PCHAN_TCH_H); |
| 272 | if (lchan) |
| 273 | type = GSM_LCHAN_TCH_H; |
| 274 | } |
| 275 | /* |
| 276 | * No need to check fully dynamic channels for TCH/F: |
| 277 | * if no TCH/H was available, neither will be TCH/F. |
| 278 | */ |
Holger Hans Peter Freyther | 457c2a8 | 2010-09-06 08:58:42 +0800 | [diff] [blame] | 279 | } |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 280 | break; |
| 281 | case GSM_LCHAN_TCH_F: |
Harald Welte | fc0d952 | 2009-08-10 13:46:55 +0200 | [diff] [blame] | 282 | lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_F); |
Harald Welte | a4c63b0 | 2014-05-18 22:23:26 +0200 | [diff] [blame] | 283 | /* If we don't have TCH/F available, fall-back to TCH/H */ |
| 284 | if (!lchan) { |
| 285 | lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_H); |
Neels Hofmeyr | f5713a5 | 2016-06-06 12:57:22 +0200 | [diff] [blame] | 286 | if (lchan) |
| 287 | type = GSM_LCHAN_TCH_H; |
Harald Welte | a4c63b0 | 2014-05-18 22:23:26 +0200 | [diff] [blame] | 288 | } |
Neels Hofmeyr | a668525 | 2016-06-06 12:53:25 +0200 | [diff] [blame] | 289 | /* If we don't have TCH/H either, try dynamic TCH/F_PDCH */ |
| 290 | if (!lchan) { |
| 291 | lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_F_PDCH); |
| 292 | /* TCH/F_PDCH used as TCH/F -- here, type is already |
| 293 | * set to GSM_LCHAN_TCH_F, but for clarity's sake... */ |
| 294 | if (lchan) |
| 295 | type = GSM_LCHAN_TCH_F; |
| 296 | } |
Neels Hofmeyr | 5f0c71b | 2016-07-23 20:15:28 +0200 | [diff] [blame] | 297 | |
Neels Hofmeyr | fdd9ad7 | 2016-07-14 03:01:24 +0200 | [diff] [blame] | 298 | /* Try fully dynamic TCH/F_TCH/H_PDCH as TCH/F... */ |
Neels Hofmeyr | 5f0c71b | 2016-07-23 20:15:28 +0200 | [diff] [blame] | 299 | if (!lchan && bts->network->dyn_ts_allow_tch_f) { |
Neels Hofmeyr | fdd9ad7 | 2016-07-14 03:01:24 +0200 | [diff] [blame] | 300 | lchan = _lc_dyn_find_bts(bts, |
| 301 | GSM_PCHAN_TCH_F_TCH_H_PDCH, |
| 302 | GSM_PCHAN_TCH_F); |
| 303 | if (lchan) |
| 304 | type = GSM_LCHAN_TCH_F; |
| 305 | } |
| 306 | /* ...and as TCH/H. */ |
| 307 | if (!lchan) { |
| 308 | lchan = _lc_dyn_find_bts(bts, |
| 309 | GSM_PCHAN_TCH_F_TCH_H_PDCH, |
| 310 | GSM_PCHAN_TCH_H); |
| 311 | if (lchan) |
| 312 | type = GSM_LCHAN_TCH_H; |
| 313 | } |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 314 | break; |
| 315 | case GSM_LCHAN_TCH_H: |
Alexander Couzens | fbd96f5 | 2016-08-29 18:40:02 +0200 | [diff] [blame] | 316 | lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_H); |
Harald Welte | 210c850 | 2009-12-12 20:58:20 +0100 | [diff] [blame] | 317 | /* If we don't have TCH/H available, fall-back to TCH/F */ |
Harald Welte | 487e6be | 2009-12-12 21:16:38 +0100 | [diff] [blame] | 318 | if (!lchan) { |
Harald Welte | 210c850 | 2009-12-12 20:58:20 +0100 | [diff] [blame] | 319 | lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_F); |
Neels Hofmeyr | f5713a5 | 2016-06-06 12:57:22 +0200 | [diff] [blame] | 320 | if (lchan) |
| 321 | type = GSM_LCHAN_TCH_F; |
Harald Welte | 487e6be | 2009-12-12 21:16:38 +0100 | [diff] [blame] | 322 | } |
Neels Hofmeyr | fdd9ad7 | 2016-07-14 03:01:24 +0200 | [diff] [blame] | 323 | /* No dedicated TCH/x available -- try fully dynamic |
| 324 | * TCH/F_TCH/H_PDCH */ |
| 325 | if (!lchan) { |
| 326 | lchan = _lc_dyn_find_bts(bts, |
| 327 | GSM_PCHAN_TCH_F_TCH_H_PDCH, |
| 328 | GSM_PCHAN_TCH_H); |
| 329 | if (lchan) |
| 330 | type = GSM_LCHAN_TCH_H; |
| 331 | } |
| 332 | /* |
| 333 | * No need to check TCH/F_TCH/H_PDCH channels for TCH/F: |
| 334 | * if no TCH/H was available, neither will be TCH/F. |
| 335 | */ |
Neels Hofmeyr | a668525 | 2016-06-06 12:53:25 +0200 | [diff] [blame] | 336 | /* If we don't have TCH/F either, try dynamic TCH/F_PDCH */ |
| 337 | if (!lchan) { |
| 338 | lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_F_PDCH); |
| 339 | if (lchan) |
| 340 | type = GSM_LCHAN_TCH_F; |
| 341 | } |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 342 | break; |
| 343 | default: |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 344 | LOGP(DRLL, LOGL_ERROR, "Unknown gsm_chan_t %u\n", type); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 347 | if (lchan) { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 348 | lchan->type = type; |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 349 | |
Neels Hofmeyr | d1c0e37 | 2016-07-25 12:33:02 +0200 | [diff] [blame] | 350 | LOGP(DRLL, LOGL_INFO, "%s Allocating lchan=%u as %s\n", |
| 351 | gsm_ts_and_pchan_name(lchan->ts), |
| 352 | lchan->nr, gsm_lchant_name(lchan->type)); |
| 353 | |
Holger Hans Peter Freyther | 5ba6f48 | 2009-10-28 14:23:39 +0100 | [diff] [blame] | 354 | /* clear sapis */ |
Holger Hans Peter Freyther | 45b0243 | 2009-11-18 22:59:51 +0100 | [diff] [blame] | 355 | memset(lchan->sapis, 0, ARRAY_SIZE(lchan->sapis)); |
Holger Hans Peter Freyther | 5ba6f48 | 2009-10-28 14:23:39 +0100 | [diff] [blame] | 356 | |
Holger Hans Peter Freyther | ea52802 | 2009-11-18 22:57:02 +0100 | [diff] [blame] | 357 | /* clear multi rate config */ |
Andreas Eversberg | 7326652 | 2014-01-19 11:47:44 +0100 | [diff] [blame] | 358 | memset(&lchan->mr_ms_lv, 0, sizeof(lchan->mr_ms_lv)); |
| 359 | memset(&lchan->mr_bts_lv, 0, sizeof(lchan->mr_bts_lv)); |
Holger Hans Peter Freyther | 454140e | 2014-12-28 12:08:28 +0100 | [diff] [blame] | 360 | lchan->broken_reason = ""; |
Harald Welte | c0d83b0 | 2010-03-28 15:58:03 +0800 | [diff] [blame] | 361 | } else { |
| 362 | struct challoc_signal_data sig; |
Neels Hofmeyr | d1c0e37 | 2016-07-25 12:33:02 +0200 | [diff] [blame] | 363 | |
| 364 | LOGP(DRLL, LOGL_ERROR, "Failed to allocate %s channel\n", |
| 365 | gsm_lchant_name(type)); |
| 366 | |
Harald Welte | c0d83b0 | 2010-03-28 15:58:03 +0800 | [diff] [blame] | 367 | sig.bts = bts; |
| 368 | sig.type = type; |
Pablo Neira Ayuso | bbc5b99 | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 369 | osmo_signal_dispatch(SS_CHALLOC, S_CHALLOC_ALLOC_FAIL, &sig); |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 370 | } |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 371 | |
| 372 | return lchan; |
| 373 | } |
| 374 | |
| 375 | /* Free a logical channel */ |
| 376 | void lchan_free(struct gsm_lchan *lchan) |
| 377 | { |
Harald Welte | c0d83b0 | 2010-03-28 15:58:03 +0800 | [diff] [blame] | 378 | struct challoc_signal_data sig; |
Harald Welte | d12b0fd | 2009-12-15 21:36:05 +0100 | [diff] [blame] | 379 | int i; |
| 380 | |
Harald Welte | c0d83b0 | 2010-03-28 15:58:03 +0800 | [diff] [blame] | 381 | sig.type = lchan->type; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 382 | lchan->type = GSM_LCHAN_NONE; |
Holger Hans Peter Freyther | 2412a07 | 2010-06-28 15:47:12 +0800 | [diff] [blame] | 383 | |
| 384 | |
| 385 | if (lchan->conn) { |
Holger Hans Peter Freyther | 08eebd5 | 2010-12-27 13:28:20 +0100 | [diff] [blame] | 386 | struct lchan_signal_data sig; |
| 387 | |
Holger Hans Peter Freyther | 2412a07 | 2010-06-28 15:47:12 +0800 | [diff] [blame] | 388 | /* We might kill an active channel... */ |
Holger Hans Peter Freyther | 08eebd5 | 2010-12-27 13:28:20 +0100 | [diff] [blame] | 389 | sig.lchan = lchan; |
| 390 | sig.mr = NULL; |
Pablo Neira Ayuso | bbc5b99 | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 391 | osmo_signal_dispatch(SS_LCHAN, S_LCHAN_UNEXPECTED_RELEASE, &sig); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 392 | } |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 393 | |
Holger Hans Peter Freyther | f95f273 | 2012-01-17 15:27:33 +0100 | [diff] [blame] | 394 | if (lchan->abis_ip.rtp_socket) { |
| 395 | LOGP(DRLL, LOGL_ERROR, "%s RTP Proxy Socket remained open.\n", |
| 396 | gsm_lchan_name(lchan)); |
| 397 | rtp_socket_free(lchan->abis_ip.rtp_socket); |
| 398 | lchan->abis_ip.rtp_socket = NULL; |
| 399 | } |
Harald Welte | c627afc | 2009-01-09 21:39:17 +0000 | [diff] [blame] | 400 | |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 401 | /* stop the timer */ |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 402 | osmo_timer_del(&lchan->T3101); |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 403 | |
Harald Welte | d12b0fd | 2009-12-15 21:36:05 +0100 | [diff] [blame] | 404 | /* clear cached measuement reports */ |
| 405 | lchan->meas_rep_idx = 0; |
| 406 | for (i = 0; i < ARRAY_SIZE(lchan->meas_rep); i++) { |
| 407 | lchan->meas_rep[i].flags = 0; |
| 408 | lchan->meas_rep[i].nr = 0; |
| 409 | } |
Harald Welte | f7c28b0 | 2009-12-21 13:30:17 +0100 | [diff] [blame] | 410 | for (i = 0; i < ARRAY_SIZE(lchan->neigh_meas); i++) |
| 411 | lchan->neigh_meas[i].arfcn = 0; |
Harald Welte | d12b0fd | 2009-12-15 21:36:05 +0100 | [diff] [blame] | 412 | |
Holger Hans Peter Freyther | 5ba05f4 | 2010-06-22 12:11:59 +0800 | [diff] [blame] | 413 | if (lchan->rqd_ref) { |
| 414 | talloc_free(lchan->rqd_ref); |
| 415 | lchan->rqd_ref = NULL; |
| 416 | lchan->rqd_ta = 0; |
| 417 | } |
| 418 | |
Harald Welte | c0d83b0 | 2010-03-28 15:58:03 +0800 | [diff] [blame] | 419 | sig.lchan = lchan; |
| 420 | sig.bts = lchan->ts->trx->bts; |
Pablo Neira Ayuso | bbc5b99 | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 421 | osmo_signal_dispatch(SS_CHALLOC, S_CHALLOC_FREED, &sig); |
Harald Welte | c0d83b0 | 2010-03-28 15:58:03 +0800 | [diff] [blame] | 422 | |
Holger Hans Peter Freyther | 2412a07 | 2010-06-28 15:47:12 +0800 | [diff] [blame] | 423 | if (lchan->conn) { |
Holger Hans Peter Freyther | 4049455 | 2010-06-28 17:09:29 +0800 | [diff] [blame] | 424 | LOGP(DRLL, LOGL_ERROR, "the subscriber connection should be gone.\n"); |
Holger Hans Peter Freyther | 2412a07 | 2010-06-28 15:47:12 +0800 | [diff] [blame] | 425 | lchan->conn = NULL; |
| 426 | } |
| 427 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 428 | /* FIXME: ts_free() the timeslot, if we're the last logical |
| 429 | * channel using it */ |
| 430 | } |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 431 | |
Holger Hans Peter Freyther | 135f797 | 2010-04-15 11:21:02 +0200 | [diff] [blame] | 432 | /* |
| 433 | * There was an error with the TRX and we need to forget |
| 434 | * any state so that a lchan can be allocated again after |
| 435 | * the trx is fully usable. |
Holger Hans Peter Freyther | f7a1c23 | 2010-06-22 12:19:06 +0800 | [diff] [blame] | 436 | * |
| 437 | * This should be called after lchan_free to force a channel |
| 438 | * be available for allocation again. This means that this |
| 439 | * method will stop the "delay after error"-timer and set the |
| 440 | * state to LCHAN_S_NONE. |
Holger Hans Peter Freyther | 135f797 | 2010-04-15 11:21:02 +0200 | [diff] [blame] | 441 | */ |
| 442 | void lchan_reset(struct gsm_lchan *lchan) |
| 443 | { |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 444 | osmo_timer_del(&lchan->T3101); |
Holger Hans Peter Freyther | b348939 | 2011-12-28 16:21:05 +0100 | [diff] [blame] | 445 | osmo_timer_del(&lchan->T3109); |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 446 | osmo_timer_del(&lchan->T3111); |
| 447 | osmo_timer_del(&lchan->error_timer); |
Holger Hans Peter Freyther | 135f797 | 2010-04-15 11:21:02 +0200 | [diff] [blame] | 448 | |
| 449 | lchan->type = GSM_LCHAN_NONE; |
| 450 | lchan->state = LCHAN_S_NONE; |
Holger Hans Peter Freyther | f95f273 | 2012-01-17 15:27:33 +0100 | [diff] [blame] | 451 | |
| 452 | if (lchan->abis_ip.rtp_socket) { |
| 453 | rtp_socket_free(lchan->abis_ip.rtp_socket); |
| 454 | lchan->abis_ip.rtp_socket = NULL; |
| 455 | } |
Holger Hans Peter Freyther | 135f797 | 2010-04-15 11:21:02 +0200 | [diff] [blame] | 456 | } |
| 457 | |
Holger Hans Peter Freyther | 4b85a32 | 2010-07-29 17:09:36 +0800 | [diff] [blame] | 458 | /* Drive the release process of the lchan */ |
Holger Hans Peter Freyther | 8582535 | 2011-12-27 22:24:17 +0100 | [diff] [blame] | 459 | static void _lchan_handle_release(struct gsm_lchan *lchan, |
Holger Hans Peter Freyther | b348939 | 2011-12-28 16:21:05 +0100 | [diff] [blame] | 460 | int sacch_deact, int mode) |
Holger Hans Peter Freyther | 4b85a32 | 2010-07-29 17:09:36 +0800 | [diff] [blame] | 461 | { |
Holger Hans Peter Freyther | 8582535 | 2011-12-27 22:24:17 +0100 | [diff] [blame] | 462 | /* Release all SAPIs on the local end and continue */ |
| 463 | rsl_release_sapis_from(lchan, 1, RSL_REL_LOCAL_END); |
Holger Hans Peter Freyther | 4b85a32 | 2010-07-29 17:09:36 +0800 | [diff] [blame] | 464 | |
Holger Hans Peter Freyther | 8582535 | 2011-12-27 22:24:17 +0100 | [diff] [blame] | 465 | /* |
| 466 | * Shall we send a RR Release, start T3109 and wait for the |
| 467 | * release indication from the BTS or just take it down (e.g. |
| 468 | * on assignment requests) |
| 469 | */ |
Holger Hans Peter Freyther | b348939 | 2011-12-28 16:21:05 +0100 | [diff] [blame] | 470 | if (sacch_deact) { |
Holger Hans Peter Freyther | 4b85a32 | 2010-07-29 17:09:36 +0800 | [diff] [blame] | 471 | gsm48_send_rr_release(lchan); |
Holger Hans Peter Freyther | b348939 | 2011-12-28 16:21:05 +0100 | [diff] [blame] | 472 | |
| 473 | /* Deactivate the SACCH on the BTS side */ |
| 474 | rsl_deact_sacch(lchan); |
| 475 | rsl_start_t3109(lchan); |
Holger Hans Peter Freyther | 006e3d8 | 2012-12-25 23:45:14 +0100 | [diff] [blame] | 476 | } else if (lchan->sapis[0] == LCHAN_SAPI_UNUSED) { |
| 477 | rsl_direct_rf_release(lchan); |
Holger Hans Peter Freyther | b348939 | 2011-12-28 16:21:05 +0100 | [diff] [blame] | 478 | } else { |
Holger Hans Peter Freyther | 8582535 | 2011-12-27 22:24:17 +0100 | [diff] [blame] | 479 | rsl_release_request(lchan, 0, mode); |
Holger Hans Peter Freyther | b348939 | 2011-12-28 16:21:05 +0100 | [diff] [blame] | 480 | } |
Holger Hans Peter Freyther | 4b85a32 | 2010-07-29 17:09:36 +0800 | [diff] [blame] | 481 | } |
Holger Hans Peter Freyther | 135f797 | 2010-04-15 11:21:02 +0200 | [diff] [blame] | 482 | |
Holger Freyther | 67b4b9a | 2009-01-01 03:46:11 +0000 | [diff] [blame] | 483 | /* Consider releasing the channel now */ |
Holger Hans Peter Freyther | 5ca825e | 2012-12-06 12:01:38 +0100 | [diff] [blame] | 484 | int lchan_release(struct gsm_lchan *lchan, int sacch_deact, enum rsl_rel_mode mode) |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 485 | { |
Holger Hans Peter Freyther | 4b85a32 | 2010-07-29 17:09:36 +0800 | [diff] [blame] | 486 | DEBUGP(DRLL, "%s starting release sequence\n", gsm_lchan_name(lchan)); |
Holger Hans Peter Freyther | 63d18b5 | 2010-04-10 00:14:55 +0200 | [diff] [blame] | 487 | rsl_lchan_set_state(lchan, LCHAN_S_REL_REQ); |
Holger Hans Peter Freyther | 4b85a32 | 2010-07-29 17:09:36 +0800 | [diff] [blame] | 488 | |
Holger Hans Peter Freyther | abf962b | 2010-11-14 16:04:46 +0100 | [diff] [blame] | 489 | lchan->conn = NULL; |
Holger Hans Peter Freyther | 8582535 | 2011-12-27 22:24:17 +0100 | [diff] [blame] | 490 | _lchan_handle_release(lchan, sacch_deact, mode); |
Holger Freyther | 67b4b9a | 2009-01-01 03:46:11 +0000 | [diff] [blame] | 491 | return 1; |
| 492 | } |
| 493 | |
Neels Hofmeyr | 2afffd5 | 2016-09-25 17:01:20 +0200 | [diff] [blame] | 494 | 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] | 495 | { |
| 496 | struct gsm_bts_trx *trx; |
| 497 | |
| 498 | llist_for_each_entry(trx, &bts->trx_list, list) { |
| 499 | int i; |
| 500 | |
| 501 | /* skip administratively deactivated tranxsceivers */ |
Harald Welte | d64c0bc | 2011-05-30 12:07:53 +0200 | [diff] [blame] | 502 | if (!nm_is_running(&trx->mo.nm_state) || |
| 503 | !nm_is_running(&trx->bb_transc.mo.nm_state)) |
Harald Welte | b908cb7 | 2009-12-22 13:09:29 +0100 | [diff] [blame] | 504 | continue; |
| 505 | |
| 506 | for (i = 0; i < ARRAY_SIZE(trx->ts); i++) { |
| 507 | struct gsm_bts_trx_ts *ts = &trx->ts[i]; |
| 508 | struct load_counter *pl = &cl->pchan[ts->pchan]; |
| 509 | int j; |
Neels Hofmeyr | 3673380 | 2016-07-29 18:10:59 +0200 | [diff] [blame] | 510 | int subslots; |
Harald Welte | b908cb7 | 2009-12-22 13:09:29 +0100 | [diff] [blame] | 511 | |
| 512 | /* skip administratively deactivated timeslots */ |
Harald Welte | d64c0bc | 2011-05-30 12:07:53 +0200 | [diff] [blame] | 513 | if (!nm_is_running(&ts->mo.nm_state)) |
Harald Welte | b908cb7 | 2009-12-22 13:09:29 +0100 | [diff] [blame] | 514 | continue; |
| 515 | |
Neels Hofmeyr | 3673380 | 2016-07-29 18:10:59 +0200 | [diff] [blame] | 516 | subslots = ts_subslots(ts); |
| 517 | for (j = 0; j < subslots; j++) { |
Harald Welte | b908cb7 | 2009-12-22 13:09:29 +0100 | [diff] [blame] | 518 | struct gsm_lchan *lchan = &ts->lchan[j]; |
| 519 | |
| 520 | pl->total++; |
| 521 | |
| 522 | switch (lchan->state) { |
| 523 | case LCHAN_S_NONE: |
| 524 | break; |
| 525 | default: |
| 526 | pl->used++; |
| 527 | break; |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | void network_chan_load(struct pchan_load *pl, struct gsm_network *net) |
| 535 | { |
| 536 | struct gsm_bts *bts; |
| 537 | |
| 538 | memset(pl, 0, sizeof(*pl)); |
| 539 | |
| 540 | llist_for_each_entry(bts, &net->bts_list, list) |
Neels Hofmeyr | 2afffd5 | 2016-09-25 17:01:20 +0200 | [diff] [blame] | 541 | bts_chan_load(pl, bts); |
Harald Welte | b908cb7 | 2009-12-22 13:09:29 +0100 | [diff] [blame] | 542 | } |
Neels Hofmeyr | 2afffd5 | 2016-09-25 17:01:20 +0200 | [diff] [blame] | 543 | |