blob: efafca6e2bc827285b6cfe50587178d7aba6e61d [file] [log] [blame]
Harald Welte8d77b952009-12-17 00:31:10 +01001/* 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 Weltedfe6c7d2010-02-20 16:24:02 +010028#include <osmocore/msgb.h>
Harald Welte8d77b952009-12-17 00:31:10 +010029#include <openbsc/debug.h>
30#include <openbsc/gsm_data.h>
31#include <openbsc/meas_rep.h>
32#include <openbsc/signal.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010033#include <osmocore/talloc.h>
Harald Welte8d77b952009-12-17 00:31:10 +010034#include <openbsc/handover.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010035#include <osmocore/gsm_utils.h>
Harald Welte8d77b952009-12-17 00:31:10 +010036
Harald Weltef7c28b02009-12-21 13:30:17 +010037/* issue handover to a cell identified by ARFCN and BSIC */
Harald Welte8d77b952009-12-17 00:31:10 +010038static 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 Welteb1d4c8e2009-12-17 23:10:46 +010046 LOGP(DHO, LOGL_NOTICE, "unable to determine neighbor BTS "
47 "for ARFCN %u BSIC %u ?!?\n", arfcn, bsic);
Harald Welte8d77b952009-12-17 00:31:10 +010048 return -EINVAL;
49 }
50
51 /* and actually try to handover to that cell */
52 return bsc_handover_start(lchan, new_bts);
53}
54
Harald Weltef7c28b02009-12-21 13:30:17 +010055/* did we get a RXLEV for a given cell in the given report? */
56static 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 Welte8d77b952009-12-17 00:31:10 +010060
Harald Weltef7c28b02009-12-21 13:30:17 +010061 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 */
75static 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 */
94static 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 */
120static 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 */
168static 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 Welte8d77b952009-12-17 00:31:10 +0100229static int process_meas_rep(struct gsm_meas_rep *mr)
230{
Harald Weltef7c28b02009-12-21 13:30:17 +0100231 struct gsm_network *net = mr->lchan->ts->trx->bts->network;
232 int av_rxlev;
Harald Welte8d77b952009-12-17 00:31:10 +0100233
Harald Welte386cd2b2009-12-18 11:49:20 +0100234 /* 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 Weltef7c28b02009-12-21 13:30:17 +0100243 /* parse actual neighbor cell info */
244 if (mr->num_cell > 0 && mr->num_cell < 7)
245 process_meas_neigh(mr);
Harald Welte8d77b952009-12-17 00:31:10 +0100246
Harald Weltef7c28b02009-12-21 13:30:17 +0100247 av_rxlev = get_meas_rep_avg(mr->lchan, MEAS_REP_DL_RXLEV_FULL,
248 net->handover.win_rxlev_avg);
Harald Weltee786c322009-12-19 21:29:19 +0100249
Harald Weltef7c28b02009-12-21 13:30:17 +0100250 /* 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 Welte8d77b952009-12-17 00:31:10 +0100255
Harald Weltef7c28b02009-12-21 13:30:17 +0100256 /* 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 Welte8d77b952009-12-17 00:31:10 +0100260
Harald Weltef7c28b02009-12-21 13:30:17 +0100261 /* Low Level */
262 if (rxlev2dbm(av_rxlev) <= -110)
263 return attempt_handover(mr);
Harald Welte8d77b952009-12-17 00:31:10 +0100264
Harald Weltef7c28b02009-12-21 13:30:17 +0100265 /* Distance */
266 if (mr->ms_l1.ta > net->handover.max_distance)
267 return attempt_handover(mr);
Harald Weltebc814502009-12-19 21:41:52 +0100268
Harald Weltef7c28b02009-12-21 13:30:17 +0100269 /* 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 Welte8d77b952009-12-17 00:31:10 +0100275}
276
277static 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
295void on_dso_load_ho_dec(void)
296{
297 register_signal_handler(SS_LCHAN, ho_dec_sig_cb, NULL);
298}