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