Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 1 | /* Handover Decision making for Inter-BTS (Intra-BSC) Handover. This |
| 2 | * only implements the handover algorithm/decision, but not execution |
| 3 | * of it */ |
| 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 | 8d77b95 | 2009-12-17 00:31:10 +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 | 8d77b95 | 2009-12-17 00:31:10 +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 | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <stdlib.h> |
| 25 | #include <errno.h> |
| 26 | |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 27 | #include <osmocom/core/msgb.h> |
Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 28 | #include <openbsc/debug.h> |
| 29 | #include <openbsc/gsm_data.h> |
| 30 | #include <openbsc/meas_rep.h> |
| 31 | #include <openbsc/signal.h> |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 32 | #include <osmocom/core/talloc.h> |
Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 33 | #include <openbsc/handover.h> |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 34 | #include <osmocom/gsm/gsm_utils.h> |
Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 35 | |
Harald Welte | f7c28b0 | 2009-12-21 13:30:17 +0100 | [diff] [blame] | 36 | /* issue handover to a cell identified by ARFCN and BSIC */ |
Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 37 | static int handover_to_arfcn_bsic(struct gsm_lchan *lchan, |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 38 | uint16_t arfcn, uint8_t bsic) |
Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 39 | { |
| 40 | struct gsm_bts *new_bts; |
| 41 | |
| 42 | /* resolve the gsm_bts structure for the best neighbor */ |
| 43 | new_bts = gsm_bts_neighbor(lchan->ts->trx->bts, arfcn, bsic); |
| 44 | if (!new_bts) { |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 45 | LOGP(DHO, LOGL_NOTICE, "unable to determine neighbor BTS " |
| 46 | "for ARFCN %u BSIC %u ?!?\n", arfcn, bsic); |
Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 47 | return -EINVAL; |
| 48 | } |
| 49 | |
| 50 | /* and actually try to handover to that cell */ |
| 51 | return bsc_handover_start(lchan, new_bts); |
| 52 | } |
| 53 | |
Harald Welte | f7c28b0 | 2009-12-21 13:30:17 +0100 | [diff] [blame] | 54 | /* did we get a RXLEV for a given cell in the given report? */ |
| 55 | static int rxlev_for_cell_in_rep(struct gsm_meas_rep *mr, |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 56 | uint16_t arfcn, uint8_t bsic) |
Harald Welte | f7c28b0 | 2009-12-21 13:30:17 +0100 | [diff] [blame] | 57 | { |
| 58 | int i; |
Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 59 | |
Harald Welte | f7c28b0 | 2009-12-21 13:30:17 +0100 | [diff] [blame] | 60 | for (i = 0; i < mr->num_cell; i++) { |
| 61 | struct gsm_meas_rep_cell *mrc = &mr->cell[i]; |
| 62 | |
| 63 | /* search for matching report */ |
| 64 | if (!(mrc->arfcn == arfcn && mrc->bsic == bsic)) |
| 65 | continue; |
| 66 | |
| 67 | mrc->flags |= MRC_F_PROCESSED; |
| 68 | return mrc->rxlev; |
| 69 | } |
| 70 | return -ENODEV; |
| 71 | } |
| 72 | |
| 73 | /* obtain averaged rxlev for given neighbor */ |
| 74 | static int neigh_meas_avg(struct neigh_meas_proc *nmp, int window) |
| 75 | { |
| 76 | unsigned int i, idx; |
| 77 | int avg = 0; |
| 78 | |
| 79 | idx = calc_initial_idx(ARRAY_SIZE(nmp->rxlev), |
| 80 | nmp->rxlev_cnt % ARRAY_SIZE(nmp->rxlev), |
| 81 | window); |
| 82 | |
| 83 | for (i = 0; i < window; i++) { |
| 84 | int j = (idx+i) % ARRAY_SIZE(nmp->rxlev); |
| 85 | |
| 86 | avg += nmp->rxlev[j]; |
| 87 | } |
| 88 | |
| 89 | return avg / window; |
| 90 | } |
| 91 | |
| 92 | /* find empty or evict bad neighbor */ |
| 93 | static struct neigh_meas_proc *find_evict_neigh(struct gsm_lchan *lchan) |
| 94 | { |
| 95 | int j, worst = 999999; |
Holger Hans Peter Freyther | 80352e0 | 2011-04-18 17:16:56 +0200 | [diff] [blame] | 96 | struct neigh_meas_proc *nmp_worst = NULL; |
Harald Welte | f7c28b0 | 2009-12-21 13:30:17 +0100 | [diff] [blame] | 97 | |
| 98 | /* first try to find an empty/unused slot */ |
| 99 | for (j = 0; j < ARRAY_SIZE(lchan->neigh_meas); j++) { |
| 100 | struct neigh_meas_proc *nmp = &lchan->neigh_meas[j]; |
| 101 | if (!nmp->arfcn) |
| 102 | return nmp; |
| 103 | } |
| 104 | |
| 105 | /* no empty slot found. evict worst neighbor from list */ |
| 106 | for (j = 0; j < ARRAY_SIZE(lchan->neigh_meas); j++) { |
| 107 | struct neigh_meas_proc *nmp = &lchan->neigh_meas[j]; |
| 108 | int avg = neigh_meas_avg(nmp, MAX_WIN_NEIGH_AVG); |
Holger Hans Peter Freyther | 80352e0 | 2011-04-18 17:16:56 +0200 | [diff] [blame] | 109 | if (!nmp_worst || avg < worst) { |
Harald Welte | f7c28b0 | 2009-12-21 13:30:17 +0100 | [diff] [blame] | 110 | worst = avg; |
| 111 | nmp_worst = nmp; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return nmp_worst; |
| 116 | } |
| 117 | |
| 118 | /* process neighbor cell measurement reports */ |
| 119 | static void process_meas_neigh(struct gsm_meas_rep *mr) |
| 120 | { |
| 121 | int i, j, idx; |
| 122 | |
| 123 | /* for each reported cell, try to update global state */ |
| 124 | for (j = 0; j < ARRAY_SIZE(mr->lchan->neigh_meas); j++) { |
| 125 | struct neigh_meas_proc *nmp = &mr->lchan->neigh_meas[j]; |
| 126 | unsigned int idx; |
| 127 | int rxlev; |
| 128 | |
| 129 | /* skip unused entries */ |
| 130 | if (!nmp->arfcn) |
| 131 | continue; |
| 132 | |
| 133 | rxlev = rxlev_for_cell_in_rep(mr, nmp->arfcn, nmp->bsic); |
| 134 | idx = nmp->rxlev_cnt % ARRAY_SIZE(nmp->rxlev); |
| 135 | if (rxlev >= 0) { |
| 136 | nmp->rxlev[idx] = rxlev; |
| 137 | nmp->last_seen_nr = mr->nr; |
| 138 | } else |
| 139 | nmp->rxlev[idx] = 0; |
| 140 | nmp->rxlev_cnt++; |
| 141 | } |
| 142 | |
| 143 | /* iterate over list of reported cells, check if we did not |
| 144 | * process all of them */ |
| 145 | for (i = 0; i < mr->num_cell; i++) { |
| 146 | struct gsm_meas_rep_cell *mrc = &mr->cell[i]; |
| 147 | struct neigh_meas_proc *nmp; |
| 148 | |
| 149 | if (mrc->flags & MRC_F_PROCESSED) |
| 150 | continue; |
| 151 | |
| 152 | nmp = find_evict_neigh(mr->lchan); |
| 153 | |
| 154 | nmp->arfcn = mrc->arfcn; |
| 155 | nmp->bsic = mrc->bsic; |
| 156 | |
| 157 | idx = nmp->rxlev_cnt % ARRAY_SIZE(nmp->rxlev); |
| 158 | nmp->rxlev[idx] = mrc->rxlev; |
| 159 | nmp->rxlev_cnt++; |
| 160 | nmp->last_seen_nr = mr->nr; |
| 161 | |
| 162 | mrc->flags |= MRC_F_PROCESSED; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /* attempt to do a handover */ |
| 167 | static int attempt_handover(struct gsm_meas_rep *mr) |
| 168 | { |
| 169 | struct gsm_network *net = mr->lchan->ts->trx->bts->network; |
| 170 | struct neigh_meas_proc *best_cell = NULL; |
| 171 | unsigned int best_better_db = 0; |
| 172 | int i, rc; |
| 173 | |
| 174 | /* find the best cell in this report that is at least RXLEV_HYST |
| 175 | * better than the current serving cell */ |
| 176 | |
| 177 | for (i = 0; i < ARRAY_SIZE(mr->lchan->neigh_meas); i++) { |
| 178 | struct neigh_meas_proc *nmp = &mr->lchan->neigh_meas[i]; |
| 179 | int avg, better; |
| 180 | |
| 181 | /* skip empty slots */ |
| 182 | if (nmp->arfcn == 0) |
| 183 | continue; |
| 184 | |
| 185 | /* caculate average rxlev for this cell over the window */ |
| 186 | avg = neigh_meas_avg(nmp, net->handover.win_rxlev_avg_neigh); |
| 187 | |
| 188 | /* check if hysteresis is fulfilled */ |
| 189 | if (avg < mr->dl.full.rx_lev + net->handover.pwr_hysteresis) |
| 190 | continue; |
| 191 | |
| 192 | better = avg - mr->dl.full.rx_lev; |
| 193 | if (better > best_better_db) { |
| 194 | best_cell = nmp; |
| 195 | best_better_db = better; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | if (!best_cell) |
| 200 | return 0; |
| 201 | |
| 202 | LOGP(DHO, LOGL_INFO, "%s: Cell on ARFCN %u is better: ", |
| 203 | gsm_ts_name(mr->lchan->ts), best_cell->arfcn); |
| 204 | if (!net->handover.active) { |
| 205 | LOGPC(DHO, LOGL_INFO, "Skipping, Handover disabled\n"); |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | rc = handover_to_arfcn_bsic(mr->lchan, best_cell->arfcn, best_cell->bsic); |
| 210 | switch (rc) { |
| 211 | case 0: |
| 212 | LOGPC(DHO, LOGL_INFO, "Starting handover\n"); |
| 213 | break; |
| 214 | case -ENOSPC: |
| 215 | LOGPC(DHO, LOGL_INFO, "No channel available\n"); |
| 216 | break; |
| 217 | case -EBUSY: |
| 218 | LOGPC(DHO, LOGL_INFO, "Handover already active\n"); |
| 219 | break; |
| 220 | default: |
| 221 | LOGPC(DHO, LOGL_ERROR, "Unknown error\n"); |
| 222 | } |
| 223 | return rc; |
| 224 | } |
| 225 | |
| 226 | /* process an already parsed measurement report and decide if we want to |
| 227 | * attempt a handover */ |
Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 228 | static int process_meas_rep(struct gsm_meas_rep *mr) |
| 229 | { |
Harald Welte | f7c28b0 | 2009-12-21 13:30:17 +0100 | [diff] [blame] | 230 | struct gsm_network *net = mr->lchan->ts->trx->bts->network; |
Max | 8a4d2e7 | 2016-05-17 15:56:49 +0200 | [diff] [blame] | 231 | enum meas_rep_field dlev, dqual; |
Harald Welte | f7c28b0 | 2009-12-21 13:30:17 +0100 | [diff] [blame] | 232 | int av_rxlev; |
Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 233 | |
Harald Welte | 386cd2b | 2009-12-18 11:49:20 +0100 | [diff] [blame] | 234 | /* we currently only do handover for TCH channels */ |
| 235 | switch (mr->lchan->type) { |
| 236 | case GSM_LCHAN_TCH_F: |
| 237 | case GSM_LCHAN_TCH_H: |
| 238 | break; |
| 239 | default: |
| 240 | return 0; |
| 241 | } |
| 242 | |
Max | 8a4d2e7 | 2016-05-17 15:56:49 +0200 | [diff] [blame] | 243 | if (mr->flags & MEAS_REP_F_DL_DTX) { |
| 244 | dlev = MEAS_REP_DL_RXLEV_SUB; |
| 245 | dqual = MEAS_REP_DL_RXQUAL_SUB; |
| 246 | } else { |
| 247 | dlev = MEAS_REP_DL_RXLEV_FULL; |
| 248 | dqual = MEAS_REP_DL_RXQUAL_FULL; |
| 249 | } |
| 250 | |
Harald Welte | f7c28b0 | 2009-12-21 13:30:17 +0100 | [diff] [blame] | 251 | /* parse actual neighbor cell info */ |
| 252 | if (mr->num_cell > 0 && mr->num_cell < 7) |
| 253 | process_meas_neigh(mr); |
Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 254 | |
Max | 8a4d2e7 | 2016-05-17 15:56:49 +0200 | [diff] [blame] | 255 | av_rxlev = get_meas_rep_avg(mr->lchan, dlev, |
Harald Welte | f7c28b0 | 2009-12-21 13:30:17 +0100 | [diff] [blame] | 256 | net->handover.win_rxlev_avg); |
Harald Welte | e786c32 | 2009-12-19 21:29:19 +0100 | [diff] [blame] | 257 | |
Harald Welte | f7c28b0 | 2009-12-21 13:30:17 +0100 | [diff] [blame] | 258 | /* Interference HO */ |
| 259 | if (rxlev2dbm(av_rxlev) > -85 && |
Max | 8a4d2e7 | 2016-05-17 15:56:49 +0200 | [diff] [blame] | 260 | meas_rep_n_out_of_m_be(mr->lchan, dqual, 3, 4, 5)) |
Harald Welte | f7c28b0 | 2009-12-21 13:30:17 +0100 | [diff] [blame] | 261 | return attempt_handover(mr); |
Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 262 | |
Harald Welte | f7c28b0 | 2009-12-21 13:30:17 +0100 | [diff] [blame] | 263 | /* Bad Quality */ |
Max | 8a4d2e7 | 2016-05-17 15:56:49 +0200 | [diff] [blame] | 264 | if (meas_rep_n_out_of_m_be(mr->lchan, dqual, 3, 4, 5)) |
Harald Welte | f7c28b0 | 2009-12-21 13:30:17 +0100 | [diff] [blame] | 265 | return attempt_handover(mr); |
Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 266 | |
Harald Welte | f7c28b0 | 2009-12-21 13:30:17 +0100 | [diff] [blame] | 267 | /* Low Level */ |
| 268 | if (rxlev2dbm(av_rxlev) <= -110) |
| 269 | return attempt_handover(mr); |
Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 270 | |
Harald Welte | f7c28b0 | 2009-12-21 13:30:17 +0100 | [diff] [blame] | 271 | /* Distance */ |
| 272 | if (mr->ms_l1.ta > net->handover.max_distance) |
| 273 | return attempt_handover(mr); |
Harald Welte | bc81450 | 2009-12-19 21:41:52 +0100 | [diff] [blame] | 274 | |
Harald Welte | f7c28b0 | 2009-12-21 13:30:17 +0100 | [diff] [blame] | 275 | /* Power Budget AKA Better Cell */ |
| 276 | if ((mr->nr % net->handover.pwr_interval) == 0) |
| 277 | return attempt_handover(mr); |
| 278 | |
| 279 | return 0; |
| 280 | |
Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | static int ho_dec_sig_cb(unsigned int subsys, unsigned int signal, |
| 284 | void *handler_data, void *signal_data) |
| 285 | { |
Holger Hans Peter Freyther | 08eebd5 | 2010-12-27 13:28:20 +0100 | [diff] [blame] | 286 | struct lchan_signal_data *lchan_data; |
Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 287 | |
| 288 | if (subsys != SS_LCHAN) |
| 289 | return 0; |
| 290 | |
Holger Hans Peter Freyther | 08eebd5 | 2010-12-27 13:28:20 +0100 | [diff] [blame] | 291 | lchan_data = signal_data; |
Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 292 | switch (signal) { |
| 293 | case S_LCHAN_MEAS_REP: |
Holger Hans Peter Freyther | 08eebd5 | 2010-12-27 13:28:20 +0100 | [diff] [blame] | 294 | process_meas_rep(lchan_data->mr); |
Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 295 | break; |
| 296 | } |
| 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | void on_dso_load_ho_dec(void) |
| 302 | { |
Pablo Neira Ayuso | bbc5b99 | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 303 | osmo_signal_register_handler(SS_LCHAN, ho_dec_sig_cb, NULL); |
Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 304 | } |