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