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