blob: 0f07bcac61dba142b29dd7439cc44ae9bdba791d [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
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010027#include <osmocom/core/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>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010032#include <osmocom/core/talloc.h>
Harald Welte8d77b952009-12-17 00:31:10 +010033#include <openbsc/handover.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010034#include <osmocom/gsm/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,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020038 uint16_t arfcn, uint8_t bsic)
Harald Welte8d77b952009-12-17 00:31:10 +010039{
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,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020056 uint16_t arfcn, uint8_t bsic)
Harald Weltef7c28b02009-12-21 13:30:17 +010057{
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;
Holger Hans Peter Freyther80352e02011-04-18 17:16:56 +020096 struct neigh_meas_proc *nmp_worst = NULL;
Harald Weltef7c28b02009-12-21 13:30:17 +010097
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 Freyther80352e02011-04-18 17:16:56 +0200109 if (!nmp_worst || avg < worst) {
Harald Weltef7c28b02009-12-21 13:30:17 +0100110 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;
Max8a4d2e72016-05-17 15:56:49 +0200231 enum meas_rep_field dlev, dqual;
Harald Weltef7c28b02009-12-21 13:30:17 +0100232 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
Max8a4d2e72016-05-17 15:56:49 +0200243 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 Weltef7c28b02009-12-21 13:30:17 +0100251 /* parse actual neighbor cell info */
252 if (mr->num_cell > 0 && mr->num_cell < 7)
253 process_meas_neigh(mr);
Harald Welte8d77b952009-12-17 00:31:10 +0100254
Max8a4d2e72016-05-17 15:56:49 +0200255 av_rxlev = get_meas_rep_avg(mr->lchan, dlev,
Harald Weltef7c28b02009-12-21 13:30:17 +0100256 net->handover.win_rxlev_avg);
Harald Weltee786c322009-12-19 21:29:19 +0100257
Harald Weltef7c28b02009-12-21 13:30:17 +0100258 /* Interference HO */
259 if (rxlev2dbm(av_rxlev) > -85 &&
Max8a4d2e72016-05-17 15:56:49 +0200260 meas_rep_n_out_of_m_be(mr->lchan, dqual, 3, 4, 5))
Harald Weltef7c28b02009-12-21 13:30:17 +0100261 return attempt_handover(mr);
Harald Welte8d77b952009-12-17 00:31:10 +0100262
Harald Weltef7c28b02009-12-21 13:30:17 +0100263 /* Bad Quality */
Max8a4d2e72016-05-17 15:56:49 +0200264 if (meas_rep_n_out_of_m_be(mr->lchan, dqual, 3, 4, 5))
Harald Weltef7c28b02009-12-21 13:30:17 +0100265 return attempt_handover(mr);
Harald Welte8d77b952009-12-17 00:31:10 +0100266
Harald Weltef7c28b02009-12-21 13:30:17 +0100267 /* Low Level */
268 if (rxlev2dbm(av_rxlev) <= -110)
269 return attempt_handover(mr);
Harald Welte8d77b952009-12-17 00:31:10 +0100270
Harald Weltef7c28b02009-12-21 13:30:17 +0100271 /* Distance */
272 if (mr->ms_l1.ta > net->handover.max_distance)
273 return attempt_handover(mr);
Harald Weltebc814502009-12-19 21:41:52 +0100274
Harald Weltef7c28b02009-12-21 13:30:17 +0100275 /* 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 Welte8d77b952009-12-17 00:31:10 +0100281}
282
283static int ho_dec_sig_cb(unsigned int subsys, unsigned int signal,
284 void *handler_data, void *signal_data)
285{
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +0100286 struct lchan_signal_data *lchan_data;
Harald Welte8d77b952009-12-17 00:31:10 +0100287
288 if (subsys != SS_LCHAN)
289 return 0;
290
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +0100291 lchan_data = signal_data;
Harald Welte8d77b952009-12-17 00:31:10 +0100292 switch (signal) {
293 case S_LCHAN_MEAS_REP:
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +0100294 process_meas_rep(lchan_data->mr);
Harald Welte8d77b952009-12-17 00:31:10 +0100295 break;
296 }
297
298 return 0;
299}
300
301void on_dso_load_ho_dec(void)
302{
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200303 osmo_signal_register_handler(SS_LCHAN, ho_dec_sig_cb, NULL);
Harald Welte8d77b952009-12-17 00:31:10 +0100304}