blob: d3f843afb85bbe36e0754ef3d06f11a83bbc1a5c [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
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * 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 Welte8d77b952009-12-17 00:31:10 +010012 * (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 Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Harald Welte8d77b952009-12-17 00:31:10 +010018 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010019 * 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 Welte8d77b952009-12-17 00:31:10 +010021 *
22 */
23
24#include <stdlib.h>
25#include <errno.h>
26
Harald Weltedfe6c7d2010-02-20 16:24:02 +010027#include <osmocore/msgb.h>
Harald Welte8d77b952009-12-17 00:31:10 +010028#include <openbsc/debug.h>
29#include <openbsc/gsm_data.h>
30#include <openbsc/meas_rep.h>
31#include <openbsc/signal.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010032#include <osmocore/talloc.h>
Harald Welte8d77b952009-12-17 00:31:10 +010033#include <openbsc/handover.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010034#include <osmocore/gsm_utils.h>
Harald Welte8d77b952009-12-17 00:31:10 +010035
Harald Weltef7c28b02009-12-21 13:30:17 +010036/* issue handover to a cell identified by ARFCN and BSIC */
Harald Welte8d77b952009-12-17 00:31:10 +010037static int handover_to_arfcn_bsic(struct gsm_lchan *lchan,
38 u_int16_t arfcn, u_int8_t bsic)
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 Welteb1d4c8e2009-12-17 23:10:46 +010045 LOGP(DHO, LOGL_NOTICE, "unable to determine neighbor BTS "
46 "for ARFCN %u BSIC %u ?!?\n", arfcn, bsic);
Harald Welte8d77b952009-12-17 00:31:10 +010047 return -EINVAL;
48 }
49
50 /* and actually try to handover to that cell */
51 return bsc_handover_start(lchan, new_bts);
52}
53
Harald Weltef7c28b02009-12-21 13:30:17 +010054/* did we get a RXLEV for a given cell in the given report? */
55static int rxlev_for_cell_in_rep(struct gsm_meas_rep *mr,
56 u_int16_t arfcn, u_int8_t bsic)
57{
58 int i;
Harald Welte8d77b952009-12-17 00:31:10 +010059
Harald Weltef7c28b02009-12-21 13:30:17 +010060 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 */
74static 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 */
93static struct neigh_meas_proc *find_evict_neigh(struct gsm_lchan *lchan)
94{
95 int j, worst = 999999;
96 struct neigh_meas_proc *nmp_worst;
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);
109 if (avg < worst) {
110 worst = avg;
111 nmp_worst = nmp;
112 }
113 }
114
115 return nmp_worst;
116}
117
118/* process neighbor cell measurement reports */
119static 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 */
167static 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 Welte8d77b952009-12-17 00:31:10 +0100228static int process_meas_rep(struct gsm_meas_rep *mr)
229{
Harald Weltef7c28b02009-12-21 13:30:17 +0100230 struct gsm_network *net = mr->lchan->ts->trx->bts->network;
231 int av_rxlev;
Harald Welte8d77b952009-12-17 00:31:10 +0100232
Harald Welte386cd2b2009-12-18 11:49:20 +0100233 /* we currently only do handover for TCH channels */
234 switch (mr->lchan->type) {
235 case GSM_LCHAN_TCH_F:
236 case GSM_LCHAN_TCH_H:
237 break;
238 default:
239 return 0;
240 }
241
Harald Weltef7c28b02009-12-21 13:30:17 +0100242 /* parse actual neighbor cell info */
243 if (mr->num_cell > 0 && mr->num_cell < 7)
244 process_meas_neigh(mr);
Harald Welte8d77b952009-12-17 00:31:10 +0100245
Harald Weltef7c28b02009-12-21 13:30:17 +0100246 av_rxlev = get_meas_rep_avg(mr->lchan, MEAS_REP_DL_RXLEV_FULL,
247 net->handover.win_rxlev_avg);
Harald Weltee786c322009-12-19 21:29:19 +0100248
Harald Weltef7c28b02009-12-21 13:30:17 +0100249 /* Interference HO */
250 if (rxlev2dbm(av_rxlev) > -85 &&
251 meas_rep_n_out_of_m_be(mr->lchan, MEAS_REP_DL_RXQUAL_FULL,
252 3, 4, 5))
253 return attempt_handover(mr);
Harald Welte8d77b952009-12-17 00:31:10 +0100254
Harald Weltef7c28b02009-12-21 13:30:17 +0100255 /* Bad Quality */
256 if (meas_rep_n_out_of_m_be(mr->lchan, MEAS_REP_DL_RXQUAL_FULL,
257 3, 4, 5))
258 return attempt_handover(mr);
Harald Welte8d77b952009-12-17 00:31:10 +0100259
Harald Weltef7c28b02009-12-21 13:30:17 +0100260 /* Low Level */
261 if (rxlev2dbm(av_rxlev) <= -110)
262 return attempt_handover(mr);
Harald Welte8d77b952009-12-17 00:31:10 +0100263
Harald Weltef7c28b02009-12-21 13:30:17 +0100264 /* Distance */
265 if (mr->ms_l1.ta > net->handover.max_distance)
266 return attempt_handover(mr);
Harald Weltebc814502009-12-19 21:41:52 +0100267
Harald Weltef7c28b02009-12-21 13:30:17 +0100268 /* Power Budget AKA Better Cell */
269 if ((mr->nr % net->handover.pwr_interval) == 0)
270 return attempt_handover(mr);
271
272 return 0;
273
Harald Welte8d77b952009-12-17 00:31:10 +0100274}
275
276static int ho_dec_sig_cb(unsigned int subsys, unsigned int signal,
277 void *handler_data, void *signal_data)
278{
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +0100279 struct lchan_signal_data *lchan_data;
Harald Welte8d77b952009-12-17 00:31:10 +0100280
281 if (subsys != SS_LCHAN)
282 return 0;
283
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +0100284 lchan_data = signal_data;
Harald Welte8d77b952009-12-17 00:31:10 +0100285 switch (signal) {
286 case S_LCHAN_MEAS_REP:
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +0100287 process_meas_rep(lchan_data->mr);
Harald Welte8d77b952009-12-17 00:31:10 +0100288 break;
289 }
290
291 return 0;
292}
293
294void on_dso_load_ho_dec(void)
295{
296 register_signal_handler(SS_LCHAN, ho_dec_sig_cb, NULL);
297}