Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 1 | /* Handover Logic for Inter-BTS (Intra-BSC) Handover. This does not |
| 2 | * actually implement the handover algorithm/decision, but executes a |
| 3 | * handover decision */ |
| 4 | |
| 5 | /* (C) 2009 by Harald Welte <laforge@gnumonks.org> |
| 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 10 | * it under the terms of the GNU Affero General Public License as published by |
| 11 | * the Free Software Foundation; either version 3 of the License, or |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 17 | * GNU Affero General Public License for more details. |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 18 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <errno.h> |
| 28 | #include <time.h> |
| 29 | #include <netinet/in.h> |
| 30 | |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 31 | #include <osmocom/core/msgb.h> |
Neels Hofmeyr | c016479 | 2017-09-04 15:15:32 +0200 | [diff] [blame] | 32 | #include <osmocom/bsc/debug.h> |
| 33 | #include <osmocom/bsc/gsm_data.h> |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 34 | #include <osmocom/gsm/gsm_utils.h> |
Neels Hofmeyr | c016479 | 2017-09-04 15:15:32 +0200 | [diff] [blame] | 35 | #include <osmocom/bsc/abis_rsl.h> |
| 36 | #include <osmocom/bsc/chan_alloc.h> |
| 37 | #include <osmocom/bsc/signal.h> |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 38 | #include <osmocom/core/talloc.h> |
Neels Hofmeyr | c016479 | 2017-09-04 15:15:32 +0200 | [diff] [blame] | 39 | #include <osmocom/bsc/bsc_subscriber.h> |
| 40 | #include <osmocom/bsc/gsm_04_08_utils.h> |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 41 | |
| 42 | struct bsc_handover { |
| 43 | struct llist_head list; |
| 44 | |
| 45 | struct gsm_lchan *old_lchan; |
| 46 | struct gsm_lchan *new_lchan; |
| 47 | |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 48 | struct osmo_timer_list T3103; |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 49 | |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 50 | uint8_t ho_ref; |
Neels Hofmeyr | 5eaa4fb | 2017-11-27 17:57:15 +0100 | [diff] [blame] | 51 | |
| 52 | bool inter_cell; |
| 53 | bool async; |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | static LLIST_HEAD(bsc_handovers); |
| 57 | |
Holger Hans Peter Freyther | d30ed6b | 2014-12-17 21:21:36 +0100 | [diff] [blame] | 58 | static void handover_free(struct bsc_handover *ho) |
| 59 | { |
| 60 | osmo_timer_del(&ho->T3103); |
| 61 | llist_del(&ho->list); |
| 62 | talloc_free(ho); |
| 63 | } |
| 64 | |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 65 | static struct bsc_handover *bsc_ho_by_new_lchan(struct gsm_lchan *new_lchan) |
| 66 | { |
| 67 | struct bsc_handover *ho; |
| 68 | |
| 69 | llist_for_each_entry(ho, &bsc_handovers, list) { |
| 70 | if (ho->new_lchan == new_lchan) |
| 71 | return ho; |
| 72 | } |
| 73 | |
| 74 | return NULL; |
| 75 | } |
| 76 | |
| 77 | static struct bsc_handover *bsc_ho_by_old_lchan(struct gsm_lchan *old_lchan) |
| 78 | { |
| 79 | struct bsc_handover *ho; |
| 80 | |
| 81 | llist_for_each_entry(ho, &bsc_handovers, list) { |
| 82 | if (ho->old_lchan == old_lchan) |
| 83 | return ho; |
| 84 | } |
| 85 | |
| 86 | return NULL; |
| 87 | } |
| 88 | |
Neels Hofmeyr | 3e62d41 | 2016-05-23 17:56:57 +0200 | [diff] [blame] | 89 | /*! \brief Hand over the specified logical channel to the specified new BTS. |
| 90 | * This is the main entry point for the actual handover algorithm, after the |
| 91 | * decision whether to initiate HO to a specific BTS. */ |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 92 | int bsc_handover_start(struct gsm_lchan *old_lchan, struct gsm_bts *bts) |
| 93 | { |
| 94 | struct gsm_lchan *new_lchan; |
| 95 | struct bsc_handover *ho; |
Neels Hofmeyr | c766c9e | 2017-11-27 17:58:04 +0100 | [diff] [blame] | 96 | static uint8_t ho_ref = 0; |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 97 | int rc; |
| 98 | |
Harald Welte | 6670681 | 2009-12-17 22:23:21 +0100 | [diff] [blame] | 99 | /* don't attempt multiple handovers for the same lchan at |
| 100 | * the same time */ |
| 101 | if (bsc_ho_by_old_lchan(old_lchan)) |
| 102 | return -EBUSY; |
| 103 | |
Philipp Maier | 39f62bb | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 104 | DEBUGP(DHO, "Beginning with handover operation" |
| 105 | "(old_lchan on BTS %u, new BTS %u) ...\n", |
Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 106 | old_lchan->ts->trx->bts->nr, bts->nr); |
| 107 | |
Alexander Couzens | b847a21 | 2016-08-02 11:34:11 +0200 | [diff] [blame] | 108 | rate_ctr_inc(&bts->network->bsc_ctrs->ctr[BSC_CTR_HANDOVER_ATTEMPTED]); |
Harald Welte | 24ff6ee | 2009-12-22 00:41:05 +0100 | [diff] [blame] | 109 | |
Holger Hans Peter Freyther | e071ab7 | 2010-06-30 12:40:10 +0800 | [diff] [blame] | 110 | if (!old_lchan->conn) { |
| 111 | LOGP(DHO, LOGL_ERROR, "Old lchan lacks connection data.\n"); |
| 112 | return -ENOSPC; |
| 113 | } |
| 114 | |
Holger Hans Peter Freyther | 457c2a8 | 2010-09-06 08:58:42 +0800 | [diff] [blame] | 115 | new_lchan = lchan_alloc(bts, old_lchan->type, 0); |
Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 116 | if (!new_lchan) { |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 117 | LOGP(DHO, LOGL_NOTICE, "No free channel\n"); |
Alexander Couzens | b847a21 | 2016-08-02 11:34:11 +0200 | [diff] [blame] | 118 | rate_ctr_inc(&bts->network->bsc_ctrs->ctr[BSC_CTR_HANDOVER_NO_CHANNEL]); |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 119 | return -ENOSPC; |
Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 120 | } |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 121 | |
Holger Hans Peter Freyther | 90cdd28 | 2010-12-21 13:29:14 +0100 | [diff] [blame] | 122 | ho = talloc_zero(tall_bsc_ctx, struct bsc_handover); |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 123 | if (!ho) { |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 124 | LOGP(DHO, LOGL_FATAL, "Out of Memory\n"); |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 125 | lchan_free(new_lchan); |
| 126 | return -ENOMEM; |
| 127 | } |
| 128 | ho->old_lchan = old_lchan; |
| 129 | ho->new_lchan = new_lchan; |
Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 130 | ho->ho_ref = ho_ref++; |
Neels Hofmeyr | 5eaa4fb | 2017-11-27 17:57:15 +0100 | [diff] [blame] | 131 | if (old_lchan->ts->trx->bts != bts) { |
| 132 | ho->inter_cell = true; |
| 133 | ho->async = true; |
| 134 | } |
Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 135 | |
| 136 | /* copy some parameters from old lchan */ |
| 137 | memcpy(&new_lchan->encr, &old_lchan->encr, sizeof(new_lchan->encr)); |
| 138 | new_lchan->ms_power = old_lchan->ms_power; |
| 139 | new_lchan->bs_power = old_lchan->bs_power; |
| 140 | new_lchan->rsl_cmode = old_lchan->rsl_cmode; |
| 141 | new_lchan->tch_mode = old_lchan->tch_mode; |
Andreas Eversberg | 7326652 | 2014-01-19 11:47:44 +0100 | [diff] [blame] | 142 | memcpy(&new_lchan->mr_ms_lv, &old_lchan->mr_ms_lv, ARRAY_SIZE(new_lchan->mr_ms_lv)); |
| 143 | memcpy(&new_lchan->mr_bts_lv, &old_lchan->mr_bts_lv, ARRAY_SIZE(new_lchan->mr_bts_lv)); |
Holger Hans Peter Freyther | e071ab7 | 2010-06-30 12:40:10 +0800 | [diff] [blame] | 144 | |
| 145 | new_lchan->conn = old_lchan->conn; |
| 146 | new_lchan->conn->ho_lchan = new_lchan; |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 147 | |
| 148 | /* FIXME: do we have a better idea of the timing advance? */ |
Neels Hofmeyr | 5eaa4fb | 2017-11-27 17:57:15 +0100 | [diff] [blame] | 149 | rc = rsl_chan_activate_lchan(new_lchan, |
| 150 | ho->inter_cell |
| 151 | ? (ho->async ? RSL_ACT_INTER_ASYNC : RSL_ACT_INTER_SYNC) |
| 152 | : RSL_ACT_INTRA_IMM_ASS, |
| 153 | ho->ho_ref); |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 154 | if (rc < 0) { |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 155 | LOGP(DHO, LOGL_ERROR, "could not activate channel\n"); |
Holger Hans Peter Freyther | e071ab7 | 2010-06-30 12:40:10 +0800 | [diff] [blame] | 156 | new_lchan->conn->ho_lchan = NULL; |
Holger Hans Peter Freyther | c839667 | 2010-12-26 19:59:32 +0100 | [diff] [blame] | 157 | new_lchan->conn = NULL; |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 158 | talloc_free(ho); |
| 159 | lchan_free(new_lchan); |
| 160 | return rc; |
| 161 | } |
| 162 | |
Holger Hans Peter Freyther | 5eec9d9 | 2010-04-10 00:16:04 +0200 | [diff] [blame] | 163 | rsl_lchan_set_state(new_lchan, LCHAN_S_ACT_REQ); |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 164 | llist_add(&ho->list, &bsc_handovers); |
| 165 | /* we continue in the SS_LCHAN handler / ho_chan_activ_ack */ |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
Neels Hofmeyr | a2733ae | 2017-11-28 00:29:25 +0100 | [diff] [blame] | 170 | /* clear any operation for this connection */ |
Holger Hans Peter Freyther | ebd50a6 | 2010-12-27 13:46:48 +0100 | [diff] [blame] | 171 | void bsc_clear_handover(struct gsm_subscriber_connection *conn, int free_lchan) |
Holger Hans Peter Freyther | f2553a6 | 2010-06-30 12:58:14 +0800 | [diff] [blame] | 172 | { |
| 173 | struct bsc_handover *ho; |
| 174 | |
| 175 | ho = bsc_ho_by_new_lchan(conn->ho_lchan); |
| 176 | |
| 177 | |
| 178 | if (!ho && conn->ho_lchan) |
| 179 | LOGP(DHO, LOGL_ERROR, "BUG: We lost some state.\n"); |
| 180 | |
| 181 | if (!ho) { |
| 182 | LOGP(DHO, LOGL_ERROR, "unable to find HO record\n"); |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | conn->ho_lchan->conn = NULL; |
| 187 | conn->ho_lchan = NULL; |
Holger Hans Peter Freyther | ebd50a6 | 2010-12-27 13:46:48 +0100 | [diff] [blame] | 188 | |
| 189 | if (free_lchan) |
Holger Hans Peter Freyther | d66777f | 2012-12-06 12:20:56 +0100 | [diff] [blame] | 190 | lchan_release(ho->new_lchan, 0, RSL_REL_LOCAL_END); |
Holger Hans Peter Freyther | f2553a6 | 2010-06-30 12:58:14 +0800 | [diff] [blame] | 191 | |
Holger Hans Peter Freyther | d30ed6b | 2014-12-17 21:21:36 +0100 | [diff] [blame] | 192 | handover_free(ho); |
Holger Hans Peter Freyther | f2553a6 | 2010-06-30 12:58:14 +0800 | [diff] [blame] | 193 | } |
| 194 | |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 195 | /* T3103 expired: Handover has failed without HO COMPLETE or HO FAIL */ |
| 196 | static void ho_T3103_cb(void *_ho) |
| 197 | { |
| 198 | struct bsc_handover *ho = _ho; |
Harald Welte | ffa55a4 | 2009-12-22 19:07:32 +0100 | [diff] [blame] | 199 | struct gsm_network *net = ho->new_lchan->ts->trx->bts->network; |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 200 | |
Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 201 | DEBUGP(DHO, "HO T3103 expired\n"); |
Alexander Couzens | b847a21 | 2016-08-02 11:34:11 +0200 | [diff] [blame] | 202 | rate_ctr_inc(&net->bsc_ctrs->ctr[BSC_CTR_HANDOVER_TIMEOUT]); |
Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 203 | |
Holger Hans Peter Freyther | d9c9f07 | 2010-06-30 13:04:13 +0800 | [diff] [blame] | 204 | ho->new_lchan->conn->ho_lchan = NULL; |
| 205 | ho->new_lchan->conn = NULL; |
Holger Hans Peter Freyther | d66777f | 2012-12-06 12:20:56 +0100 | [diff] [blame] | 206 | lchan_release(ho->new_lchan, 0, RSL_REL_LOCAL_END); |
Holger Hans Peter Freyther | d30ed6b | 2014-12-17 21:21:36 +0100 | [diff] [blame] | 207 | handover_free(ho); |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /* RSL has acknowledged activation of the new lchan */ |
| 211 | static int ho_chan_activ_ack(struct gsm_lchan *new_lchan) |
| 212 | { |
| 213 | struct bsc_handover *ho; |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 214 | |
Harald Welte | a9fa8dc | 2009-12-18 14:50:08 +0100 | [diff] [blame] | 215 | /* we need to check if this channel activation is related to |
| 216 | * a handover at all (and if, which particular handover) */ |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 217 | ho = bsc_ho_by_new_lchan(new_lchan); |
Harald Welte | a9fa8dc | 2009-12-18 14:50:08 +0100 | [diff] [blame] | 218 | if (!ho) |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 219 | return -ENODEV; |
Harald Welte | a9fa8dc | 2009-12-18 14:50:08 +0100 | [diff] [blame] | 220 | |
| 221 | DEBUGP(DHO, "handover activate ack, send HO Command\n"); |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 222 | |
| 223 | /* we can now send the 04.08 HANDOVER COMMAND to the MS |
| 224 | * using the old lchan */ |
| 225 | |
Holger Hans Peter Freyther | a5050b1 | 2012-09-11 11:55:03 +0200 | [diff] [blame] | 226 | gsm48_send_ho_cmd(ho->old_lchan, new_lchan, 0, ho->ho_ref); |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 227 | |
| 228 | /* start T3103. We can continue either with T3103 expiration, |
| 229 | * 04.08 HANDOVER COMPLETE or 04.08 HANDOVER FAIL */ |
Pablo Neira Ayuso | 5121576 | 2017-05-08 20:57:52 +0200 | [diff] [blame] | 230 | osmo_timer_setup(&ho->T3103, ho_T3103_cb, ho); |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 231 | osmo_timer_schedule(&ho->T3103, 10, 0); |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 232 | |
Harald Welte | a037902 | 2009-12-20 17:04:40 +0100 | [diff] [blame] | 233 | /* create a RTP connection */ |
| 234 | if (is_ipaccess_bts(new_lchan->ts->trx->bts)) |
| 235 | rsl_ipacc_crcx(new_lchan); |
| 236 | |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | /* RSL has not acknowledged activation of the new lchan */ |
| 241 | static int ho_chan_activ_nack(struct gsm_lchan *new_lchan) |
| 242 | { |
| 243 | struct bsc_handover *ho; |
| 244 | |
| 245 | ho = bsc_ho_by_new_lchan(new_lchan); |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 246 | if (!ho) { |
Harald Welte | 25cf824 | 2012-07-06 14:21:55 +0200 | [diff] [blame] | 247 | LOGP(DHO, LOGL_INFO, "ACT NACK: unable to find HO record\n"); |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 248 | return -ENODEV; |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 249 | } |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 250 | |
Holger Hans Peter Freyther | 2391b4c | 2010-12-21 13:30:17 +0100 | [diff] [blame] | 251 | new_lchan->conn->ho_lchan = NULL; |
| 252 | new_lchan->conn = NULL; |
Holger Hans Peter Freyther | d30ed6b | 2014-12-17 21:21:36 +0100 | [diff] [blame] | 253 | handover_free(ho); |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 254 | |
| 255 | /* FIXME: maybe we should try to allocate a new LCHAN here? */ |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | /* GSM 04.08 HANDOVER COMPLETE has been received on new channel */ |
| 261 | static int ho_gsm48_ho_compl(struct gsm_lchan *new_lchan) |
| 262 | { |
Holger Hans Peter Freyther | 9d3e2ec | 2010-12-26 20:34:26 +0100 | [diff] [blame] | 263 | struct gsm_network *net; |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 264 | struct bsc_handover *ho; |
| 265 | |
| 266 | ho = bsc_ho_by_new_lchan(new_lchan); |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 267 | if (!ho) { |
| 268 | LOGP(DHO, LOGL_ERROR, "unable to find HO record\n"); |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 269 | return -ENODEV; |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 270 | } |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 271 | |
Holger Hans Peter Freyther | 9d3e2ec | 2010-12-26 20:34:26 +0100 | [diff] [blame] | 272 | net = new_lchan->ts->trx->bts->network; |
Harald Welte | 7c639a0 | 2009-12-25 23:02:50 +0100 | [diff] [blame] | 273 | LOGP(DHO, LOGL_INFO, "Subscriber %s HO from BTS %u->%u on ARFCN " |
Neels Hofmeyr | 7b65688 | 2017-07-09 22:09:18 +0200 | [diff] [blame] | 274 | "%u->%u\n", bsc_subscr_name(ho->old_lchan->conn->bsub), |
Harald Welte | 7c639a0 | 2009-12-25 23:02:50 +0100 | [diff] [blame] | 275 | ho->old_lchan->ts->trx->bts->nr, new_lchan->ts->trx->bts->nr, |
| 276 | ho->old_lchan->ts->trx->arfcn, new_lchan->ts->trx->arfcn); |
| 277 | |
Alexander Couzens | b847a21 | 2016-08-02 11:34:11 +0200 | [diff] [blame] | 278 | rate_ctr_inc(&net->bsc_ctrs->ctr[BSC_CTR_HANDOVER_COMPLETED]); |
Harald Welte | 24ff6ee | 2009-12-22 00:41:05 +0100 | [diff] [blame] | 279 | |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 280 | osmo_timer_del(&ho->T3103); |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 281 | |
Holger Hans Peter Freyther | e071ab7 | 2010-06-30 12:40:10 +0800 | [diff] [blame] | 282 | /* Replace the ho lchan with the primary one */ |
| 283 | if (ho->old_lchan != new_lchan->conn->lchan) |
| 284 | LOGP(DHO, LOGL_ERROR, "Primary lchan changed during handover.\n"); |
| 285 | |
| 286 | if (new_lchan != new_lchan->conn->ho_lchan) |
| 287 | LOGP(DHO, LOGL_ERROR, "Handover channel changed during this handover.\n"); |
| 288 | |
| 289 | new_lchan->conn->ho_lchan = NULL; |
| 290 | new_lchan->conn->lchan = new_lchan; |
| 291 | ho->old_lchan->conn = NULL; |
Harald Welte | fe18d5c | 2009-12-17 17:14:43 +0100 | [diff] [blame] | 292 | |
Holger Hans Peter Freyther | d66777f | 2012-12-06 12:20:56 +0100 | [diff] [blame] | 293 | lchan_release(ho->old_lchan, 0, RSL_REL_LOCAL_END); |
Harald Welte | ade773f | 2009-12-21 13:29:19 +0100 | [diff] [blame] | 294 | |
Holger Hans Peter Freyther | d30ed6b | 2014-12-17 21:21:36 +0100 | [diff] [blame] | 295 | handover_free(ho); |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | /* GSM 04.08 HANDOVER FAIL has been received */ |
| 300 | static int ho_gsm48_ho_fail(struct gsm_lchan *old_lchan) |
| 301 | { |
Harald Welte | ffa55a4 | 2009-12-22 19:07:32 +0100 | [diff] [blame] | 302 | struct gsm_network *net = old_lchan->ts->trx->bts->network; |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 303 | struct bsc_handover *ho; |
Holger Hans Peter Freyther | d30ed6b | 2014-12-17 21:21:36 +0100 | [diff] [blame] | 304 | struct gsm_lchan *new_lchan; |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 305 | |
| 306 | ho = bsc_ho_by_old_lchan(old_lchan); |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 307 | if (!ho) { |
| 308 | LOGP(DHO, LOGL_ERROR, "unable to find HO record\n"); |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 309 | return -ENODEV; |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 310 | } |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 311 | |
Alexander Couzens | b847a21 | 2016-08-02 11:34:11 +0200 | [diff] [blame] | 312 | rate_ctr_inc(&net->bsc_ctrs->ctr[BSC_CTR_HANDOVER_FAILED]); |
Harald Welte | 24ff6ee | 2009-12-22 00:41:05 +0100 | [diff] [blame] | 313 | |
Holger Hans Peter Freyther | d30ed6b | 2014-12-17 21:21:36 +0100 | [diff] [blame] | 314 | new_lchan = ho->new_lchan; |
Holger Hans Peter Freyther | e071ab7 | 2010-06-30 12:40:10 +0800 | [diff] [blame] | 315 | |
| 316 | /* release the channel and forget about it */ |
| 317 | ho->new_lchan->conn->ho_lchan = NULL; |
| 318 | ho->new_lchan->conn = NULL; |
Holger Hans Peter Freyther | d30ed6b | 2014-12-17 21:21:36 +0100 | [diff] [blame] | 319 | handover_free(ho); |
Holger Hans Peter Freyther | e071ab7 | 2010-06-30 12:40:10 +0800 | [diff] [blame] | 320 | |
Holger Hans Peter Freyther | d30ed6b | 2014-12-17 21:21:36 +0100 | [diff] [blame] | 321 | lchan_release(new_lchan, 0, RSL_REL_LOCAL_END); |
| 322 | |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 323 | |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | /* GSM 08.58 HANDOVER DETECT has been received */ |
| 328 | static int ho_rsl_detect(struct gsm_lchan *new_lchan) |
| 329 | { |
| 330 | struct bsc_handover *ho; |
| 331 | |
Harald Welte | 6f7a5a7 | 2009-12-18 11:52:03 +0100 | [diff] [blame] | 332 | ho = bsc_ho_by_new_lchan(new_lchan); |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 333 | if (!ho) { |
| 334 | LOGP(DHO, LOGL_ERROR, "unable to find HO record\n"); |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 335 | return -ENODEV; |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 336 | } |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 337 | |
Neels Hofmeyr | be1131d | 2018-01-19 01:23:35 +0100 | [diff] [blame] | 338 | LOGP(DHO, LOGL_DEBUG, "%s Handover RACH detected\n", gsm_lchan_name(new_lchan)); |
| 339 | |
| 340 | /* This is just for logging on the DHO category. The actual MGCP switchover happens in |
| 341 | * osmo_bsc_mgcp.c by receiving the same S_LCHAN_HANDOVER_DETECT signal. |
| 342 | * (Calling mgcp_handover() directly currently breaks linking in utils/...) */ |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 343 | |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | static int ho_logic_sig_cb(unsigned int subsys, unsigned int signal, |
| 348 | void *handler_data, void *signal_data) |
| 349 | { |
Holger Hans Peter Freyther | 08eebd5 | 2010-12-27 13:28:20 +0100 | [diff] [blame] | 350 | struct lchan_signal_data *lchan_data; |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 351 | struct gsm_lchan *lchan; |
| 352 | |
Holger Hans Peter Freyther | 08eebd5 | 2010-12-27 13:28:20 +0100 | [diff] [blame] | 353 | lchan_data = signal_data; |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 354 | switch (subsys) { |
| 355 | case SS_LCHAN: |
Holger Hans Peter Freyther | 08eebd5 | 2010-12-27 13:28:20 +0100 | [diff] [blame] | 356 | lchan = lchan_data->lchan; |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 357 | switch (signal) { |
| 358 | case S_LCHAN_ACTIVATE_ACK: |
| 359 | return ho_chan_activ_ack(lchan); |
| 360 | case S_LCHAN_ACTIVATE_NACK: |
| 361 | return ho_chan_activ_nack(lchan); |
| 362 | case S_LCHAN_HANDOVER_DETECT: |
| 363 | return ho_rsl_detect(lchan); |
| 364 | case S_LCHAN_HANDOVER_COMPL: |
| 365 | return ho_gsm48_ho_compl(lchan); |
| 366 | case S_LCHAN_HANDOVER_FAIL: |
| 367 | return ho_gsm48_ho_fail(lchan); |
| 368 | } |
| 369 | break; |
| 370 | default: |
| 371 | break; |
| 372 | } |
| 373 | |
| 374 | return 0; |
| 375 | } |
| 376 | |
Neels Hofmeyr | a2733ae | 2017-11-28 00:29:25 +0100 | [diff] [blame] | 377 | /* Return the old lchan or NULL. This is meant for audio handling */ |
Holger Hans Peter Freyther | c121bb3 | 2012-12-26 10:17:42 +0100 | [diff] [blame] | 378 | struct gsm_lchan *bsc_handover_pending(struct gsm_lchan *new_lchan) |
| 379 | { |
| 380 | struct bsc_handover *ho; |
| 381 | ho = bsc_ho_by_new_lchan(new_lchan); |
| 382 | if (!ho) |
| 383 | return NULL; |
| 384 | return ho->old_lchan; |
| 385 | } |
| 386 | |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 387 | static __attribute__((constructor)) void on_dso_load_ho_logic(void) |
| 388 | { |
Pablo Neira Ayuso | bbc5b99 | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 389 | osmo_signal_register_handler(SS_LCHAN, ho_logic_sig_cb, NULL); |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 390 | } |
Andreas Eversberg | a918000 | 2013-05-30 11:14:31 +0200 | [diff] [blame] | 391 | |
| 392 | /* Count number of currently ongoing handovers |
| 393 | * inter_cell: if true, count only handovers between two cells. If false, count only handovers within one |
| 394 | * cell. */ |
| 395 | int bsc_ho_count(struct gsm_bts *bts, bool inter_cell) |
| 396 | { |
| 397 | struct bsc_handover *ho; |
| 398 | int count = 0; |
| 399 | |
| 400 | llist_for_each_entry(ho, &bsc_handovers, list) { |
| 401 | if (ho->inter_cell != inter_cell) |
| 402 | continue; |
| 403 | if (ho->new_lchan->ts->trx->bts == bts) |
| 404 | count++; |
| 405 | } |
| 406 | |
| 407 | return count; |
| 408 | } |