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