blob: ff6e99346c7b999eb87d52262aa6c122121f906b [file] [log] [blame]
Harald Welte798418a2009-11-29 22:56:14 +01001/* Handover Logic for Inter-BTS (Intra-BSC) Handover. This does not
2 * actually implement the handover algorithm/decision, but executes a
3 * handover decision */
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 Welte798418a2009-11-29 22:56:14 +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 Welte798418a2009-11-29 22:56:14 +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 Welte798418a2009-11-29 22:56:14 +010021 *
22 */
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <errno.h>
28#include <time.h>
29#include <netinet/in.h>
30
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010031#include <osmocom/core/msgb.h>
Harald Welte798418a2009-11-29 22:56:14 +010032#include <openbsc/debug.h>
33#include <openbsc/gsm_data.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010034#include <osmocom/gsm/gsm_utils.h>
Harald Welte798418a2009-11-29 22:56:14 +010035#include <openbsc/gsm_subscriber.h>
36#include <openbsc/gsm_04_08.h>
37#include <openbsc/abis_rsl.h>
38#include <openbsc/chan_alloc.h>
39#include <openbsc/signal.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010040#include <osmocom/core/talloc.h>
Harald Welte798418a2009-11-29 22:56:14 +010041#include <openbsc/transaction.h>
Andreas Eversbergdcf38e12013-12-05 14:37:11 +010042#include <openbsc/trau_mux.h>
Harald Welte798418a2009-11-29 22:56:14 +010043
44struct bsc_handover {
45 struct llist_head list;
46
47 struct gsm_lchan *old_lchan;
48 struct gsm_lchan *new_lchan;
49
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +020050 struct osmo_timer_list T3103;
Harald Welte798418a2009-11-29 22:56:14 +010051
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020052 uint8_t ho_ref;
Harald Welte798418a2009-11-29 22:56:14 +010053};
54
55static LLIST_HEAD(bsc_handovers);
56
Holger Hans Peter Freytherd30ed6b2014-12-17 21:21:36 +010057static void handover_free(struct bsc_handover *ho)
58{
59 osmo_timer_del(&ho->T3103);
60 llist_del(&ho->list);
61 talloc_free(ho);
62}
63
Harald Welte798418a2009-11-29 22:56:14 +010064static struct bsc_handover *bsc_ho_by_new_lchan(struct gsm_lchan *new_lchan)
65{
66 struct bsc_handover *ho;
67
68 llist_for_each_entry(ho, &bsc_handovers, list) {
69 if (ho->new_lchan == new_lchan)
70 return ho;
71 }
72
73 return NULL;
74}
75
76static struct bsc_handover *bsc_ho_by_old_lchan(struct gsm_lchan *old_lchan)
77{
78 struct bsc_handover *ho;
79
80 llist_for_each_entry(ho, &bsc_handovers, list) {
81 if (ho->old_lchan == old_lchan)
82 return ho;
83 }
84
85 return NULL;
86}
87
88/* Hand over the specified logical channel to the specified new BTS.
89 * This is the main entry point for the actual handover algorithm,
90 * after it has decided it wants to initiate HO to a specific BTS */
91int bsc_handover_start(struct gsm_lchan *old_lchan, struct gsm_bts *bts)
92{
93 struct gsm_lchan *new_lchan;
94 struct bsc_handover *ho;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020095 static uint8_t ho_ref;
Harald Welte798418a2009-11-29 22:56:14 +010096 int rc;
97
Harald Welte66706812009-12-17 22:23:21 +010098 /* don't attempt multiple handovers for the same lchan at
99 * the same time */
100 if (bsc_ho_by_old_lchan(old_lchan))
101 return -EBUSY;
102
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100103 DEBUGP(DHO, "(old_lchan on BTS %u, new BTS %u)\n",
Harald Welte8d77b952009-12-17 00:31:10 +0100104 old_lchan->ts->trx->bts->nr, bts->nr);
105
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200106 osmo_counter_inc(bts->network->stats.handover.attempted);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100107
Holger Hans Peter Freythere071ab72010-06-30 12:40:10 +0800108 if (!old_lchan->conn) {
109 LOGP(DHO, LOGL_ERROR, "Old lchan lacks connection data.\n");
110 return -ENOSPC;
111 }
112
Holger Hans Peter Freyther457c2a82010-09-06 08:58:42 +0800113 new_lchan = lchan_alloc(bts, old_lchan->type, 0);
Harald Welte8d77b952009-12-17 00:31:10 +0100114 if (!new_lchan) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100115 LOGP(DHO, LOGL_NOTICE, "No free channel\n");
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200116 osmo_counter_inc(bts->network->stats.handover.no_channel);
Harald Welte798418a2009-11-29 22:56:14 +0100117 return -ENOSPC;
Harald Welte8d77b952009-12-17 00:31:10 +0100118 }
Harald Welte798418a2009-11-29 22:56:14 +0100119
Holger Hans Peter Freyther90cdd282010-12-21 13:29:14 +0100120 ho = talloc_zero(tall_bsc_ctx, struct bsc_handover);
Harald Welte798418a2009-11-29 22:56:14 +0100121 if (!ho) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100122 LOGP(DHO, LOGL_FATAL, "Out of Memory\n");
Harald Welte798418a2009-11-29 22:56:14 +0100123 lchan_free(new_lchan);
124 return -ENOMEM;
125 }
126 ho->old_lchan = old_lchan;
127 ho->new_lchan = new_lchan;
Harald Welte8d77b952009-12-17 00:31:10 +0100128 ho->ho_ref = ho_ref++;
129
130 /* copy some parameters from old lchan */
131 memcpy(&new_lchan->encr, &old_lchan->encr, sizeof(new_lchan->encr));
132 new_lchan->ms_power = old_lchan->ms_power;
133 new_lchan->bs_power = old_lchan->bs_power;
134 new_lchan->rsl_cmode = old_lchan->rsl_cmode;
135 new_lchan->tch_mode = old_lchan->tch_mode;
Holger Hans Peter Freyther22fd3cd2014-12-18 18:31:54 +0100136 new_lchan->mr_conf = old_lchan->mr_conf;
Holger Hans Peter Freythere071ab72010-06-30 12:40:10 +0800137
138 new_lchan->conn = old_lchan->conn;
139 new_lchan->conn->ho_lchan = new_lchan;
Harald Welte798418a2009-11-29 22:56:14 +0100140
141 /* FIXME: do we have a better idea of the timing advance? */
Andreas Eversberg723a7512013-10-11 12:55:35 +0200142 rc = rsl_chan_activate_lchan(new_lchan, RSL_ACT_INTER_ASYNC, ho->ho_ref);
Harald Welte798418a2009-11-29 22:56:14 +0100143 if (rc < 0) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100144 LOGP(DHO, LOGL_ERROR, "could not activate channel\n");
Holger Hans Peter Freythere071ab72010-06-30 12:40:10 +0800145 new_lchan->conn->ho_lchan = NULL;
Holger Hans Peter Freytherc8396672010-12-26 19:59:32 +0100146 new_lchan->conn = NULL;
Harald Welte798418a2009-11-29 22:56:14 +0100147 talloc_free(ho);
148 lchan_free(new_lchan);
149 return rc;
150 }
151
Holger Hans Peter Freyther5eec9d92010-04-10 00:16:04 +0200152 rsl_lchan_set_state(new_lchan, LCHAN_S_ACT_REQ);
Harald Welte798418a2009-11-29 22:56:14 +0100153 llist_add(&ho->list, &bsc_handovers);
154 /* we continue in the SS_LCHAN handler / ho_chan_activ_ack */
155
156 return 0;
157}
158
Holger Hans Peter Freytherebd50a62010-12-27 13:46:48 +0100159void bsc_clear_handover(struct gsm_subscriber_connection *conn, int free_lchan)
Holger Hans Peter Freytherf2553a62010-06-30 12:58:14 +0800160{
161 struct bsc_handover *ho;
162
163 ho = bsc_ho_by_new_lchan(conn->ho_lchan);
164
165
166 if (!ho && conn->ho_lchan)
167 LOGP(DHO, LOGL_ERROR, "BUG: We lost some state.\n");
168
169 if (!ho) {
170 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
171 return;
172 }
173
174 conn->ho_lchan->conn = NULL;
175 conn->ho_lchan = NULL;
Holger Hans Peter Freytherebd50a62010-12-27 13:46:48 +0100176
177 if (free_lchan)
Holger Hans Peter Freytherd66777f2012-12-06 12:20:56 +0100178 lchan_release(ho->new_lchan, 0, RSL_REL_LOCAL_END);
Holger Hans Peter Freytherf2553a62010-06-30 12:58:14 +0800179
Holger Hans Peter Freytherd30ed6b2014-12-17 21:21:36 +0100180 handover_free(ho);
Holger Hans Peter Freytherf2553a62010-06-30 12:58:14 +0800181}
182
Harald Welte798418a2009-11-29 22:56:14 +0100183/* T3103 expired: Handover has failed without HO COMPLETE or HO FAIL */
184static void ho_T3103_cb(void *_ho)
185{
186 struct bsc_handover *ho = _ho;
Harald Welteffa55a42009-12-22 19:07:32 +0100187 struct gsm_network *net = ho->new_lchan->ts->trx->bts->network;
Harald Welte798418a2009-11-29 22:56:14 +0100188
Harald Welte8d77b952009-12-17 00:31:10 +0100189 DEBUGP(DHO, "HO T3103 expired\n");
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200190 osmo_counter_inc(net->stats.handover.timeout);
Harald Welte8d77b952009-12-17 00:31:10 +0100191
Holger Hans Peter Freytherd9c9f072010-06-30 13:04:13 +0800192 ho->new_lchan->conn->ho_lchan = NULL;
193 ho->new_lchan->conn = NULL;
Holger Hans Peter Freytherd66777f2012-12-06 12:20:56 +0100194 lchan_release(ho->new_lchan, 0, RSL_REL_LOCAL_END);
Holger Hans Peter Freytherd30ed6b2014-12-17 21:21:36 +0100195 handover_free(ho);
Harald Welte798418a2009-11-29 22:56:14 +0100196}
197
198/* RSL has acknowledged activation of the new lchan */
199static int ho_chan_activ_ack(struct gsm_lchan *new_lchan)
200{
201 struct bsc_handover *ho;
Harald Welte798418a2009-11-29 22:56:14 +0100202
Harald Weltea9fa8dc2009-12-18 14:50:08 +0100203 /* we need to check if this channel activation is related to
204 * a handover at all (and if, which particular handover) */
Harald Welte798418a2009-11-29 22:56:14 +0100205 ho = bsc_ho_by_new_lchan(new_lchan);
Harald Weltea9fa8dc2009-12-18 14:50:08 +0100206 if (!ho)
Harald Welte798418a2009-11-29 22:56:14 +0100207 return -ENODEV;
Harald Weltea9fa8dc2009-12-18 14:50:08 +0100208
209 DEBUGP(DHO, "handover activate ack, send HO Command\n");
Harald Welte798418a2009-11-29 22:56:14 +0100210
211 /* we can now send the 04.08 HANDOVER COMMAND to the MS
212 * using the old lchan */
213
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200214 gsm48_send_ho_cmd(ho->old_lchan, new_lchan, 0, ho->ho_ref);
Harald Welte798418a2009-11-29 22:56:14 +0100215
216 /* start T3103. We can continue either with T3103 expiration,
217 * 04.08 HANDOVER COMPLETE or 04.08 HANDOVER FAIL */
218 ho->T3103.cb = ho_T3103_cb;
Harald Weltee47f96b2009-12-18 11:49:03 +0100219 ho->T3103.data = ho;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200220 osmo_timer_schedule(&ho->T3103, 10, 0);
Harald Welte798418a2009-11-29 22:56:14 +0100221
Harald Weltea0379022009-12-20 17:04:40 +0100222 /* create a RTP connection */
223 if (is_ipaccess_bts(new_lchan->ts->trx->bts))
224 rsl_ipacc_crcx(new_lchan);
225
Harald Welte798418a2009-11-29 22:56:14 +0100226 return 0;
227}
228
229/* RSL has not acknowledged activation of the new lchan */
230static int ho_chan_activ_nack(struct gsm_lchan *new_lchan)
231{
232 struct bsc_handover *ho;
233
234 ho = bsc_ho_by_new_lchan(new_lchan);
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100235 if (!ho) {
Harald Welte25cf8242012-07-06 14:21:55 +0200236 LOGP(DHO, LOGL_INFO, "ACT NACK: unable to find HO record\n");
Harald Welte798418a2009-11-29 22:56:14 +0100237 return -ENODEV;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100238 }
Harald Welte798418a2009-11-29 22:56:14 +0100239
Holger Hans Peter Freyther2391b4c2010-12-21 13:30:17 +0100240 new_lchan->conn->ho_lchan = NULL;
241 new_lchan->conn = NULL;
Holger Hans Peter Freytherd30ed6b2014-12-17 21:21:36 +0100242 handover_free(ho);
Harald Welte798418a2009-11-29 22:56:14 +0100243
244 /* FIXME: maybe we should try to allocate a new LCHAN here? */
245
246 return 0;
247}
248
249/* GSM 04.08 HANDOVER COMPLETE has been received on new channel */
250static int ho_gsm48_ho_compl(struct gsm_lchan *new_lchan)
251{
Holger Hans Peter Freyther9d3e2ec2010-12-26 20:34:26 +0100252 struct gsm_network *net;
Harald Welte798418a2009-11-29 22:56:14 +0100253 struct bsc_handover *ho;
254
255 ho = bsc_ho_by_new_lchan(new_lchan);
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100256 if (!ho) {
257 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
Harald Welte798418a2009-11-29 22:56:14 +0100258 return -ENODEV;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100259 }
Harald Welte798418a2009-11-29 22:56:14 +0100260
Holger Hans Peter Freyther9d3e2ec2010-12-26 20:34:26 +0100261 net = new_lchan->ts->trx->bts->network;
Harald Welte7c639a02009-12-25 23:02:50 +0100262 LOGP(DHO, LOGL_INFO, "Subscriber %s HO from BTS %u->%u on ARFCN "
Holger Hans Peter Freyther2412a072010-06-28 15:47:12 +0800263 "%u->%u\n", subscr_name(ho->old_lchan->conn->subscr),
Harald Welte7c639a02009-12-25 23:02:50 +0100264 ho->old_lchan->ts->trx->bts->nr, new_lchan->ts->trx->bts->nr,
265 ho->old_lchan->ts->trx->arfcn, new_lchan->ts->trx->arfcn);
266
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200267 osmo_counter_inc(net->stats.handover.completed);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100268
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200269 osmo_timer_del(&ho->T3103);
Harald Welte798418a2009-11-29 22:56:14 +0100270
Andreas Eversbergdcf38e12013-12-05 14:37:11 +0100271 /* switch TRAU muxer for E1 based BTS from one channel to another */
272 if (is_e1_bts(new_lchan->conn->bts))
273 switch_trau_mux(ho->old_lchan, new_lchan);
274
Holger Hans Peter Freythere071ab72010-06-30 12:40:10 +0800275 /* Replace the ho lchan with the primary one */
276 if (ho->old_lchan != new_lchan->conn->lchan)
277 LOGP(DHO, LOGL_ERROR, "Primary lchan changed during handover.\n");
278
279 if (new_lchan != new_lchan->conn->ho_lchan)
280 LOGP(DHO, LOGL_ERROR, "Handover channel changed during this handover.\n");
281
282 new_lchan->conn->ho_lchan = NULL;
283 new_lchan->conn->lchan = new_lchan;
284 ho->old_lchan->conn = NULL;
Harald Weltefe18d5c2009-12-17 17:14:43 +0100285
Holger Hans Peter Freyther74419492010-04-10 00:12:31 +0200286 rsl_lchan_set_state(ho->old_lchan, LCHAN_S_INACTIVE);
Holger Hans Peter Freytherd66777f2012-12-06 12:20:56 +0100287 lchan_release(ho->old_lchan, 0, RSL_REL_LOCAL_END);
Harald Welteade773f2009-12-21 13:29:19 +0100288
Holger Hans Peter Freytherd30ed6b2014-12-17 21:21:36 +0100289 handover_free(ho);
Harald Welte798418a2009-11-29 22:56:14 +0100290 return 0;
291}
292
293/* GSM 04.08 HANDOVER FAIL has been received */
294static int ho_gsm48_ho_fail(struct gsm_lchan *old_lchan)
295{
Harald Welteffa55a42009-12-22 19:07:32 +0100296 struct gsm_network *net = old_lchan->ts->trx->bts->network;
Harald Welte798418a2009-11-29 22:56:14 +0100297 struct bsc_handover *ho;
Holger Hans Peter Freytherd30ed6b2014-12-17 21:21:36 +0100298 struct gsm_lchan *new_lchan;
Harald Welte798418a2009-11-29 22:56:14 +0100299
300 ho = bsc_ho_by_old_lchan(old_lchan);
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100301 if (!ho) {
302 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
Harald Welte798418a2009-11-29 22:56:14 +0100303 return -ENODEV;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100304 }
Harald Welte798418a2009-11-29 22:56:14 +0100305
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200306 osmo_counter_inc(net->stats.handover.failed);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100307
Holger Hans Peter Freytherd30ed6b2014-12-17 21:21:36 +0100308 new_lchan = ho->new_lchan;
Holger Hans Peter Freythere071ab72010-06-30 12:40:10 +0800309
310 /* release the channel and forget about it */
311 ho->new_lchan->conn->ho_lchan = NULL;
312 ho->new_lchan->conn = NULL;
Holger Hans Peter Freytherd30ed6b2014-12-17 21:21:36 +0100313 handover_free(ho);
Holger Hans Peter Freythere071ab72010-06-30 12:40:10 +0800314
Holger Hans Peter Freytherd30ed6b2014-12-17 21:21:36 +0100315 lchan_release(new_lchan, 0, RSL_REL_LOCAL_END);
316
Harald Welte798418a2009-11-29 22:56:14 +0100317
318 return 0;
319}
320
321/* GSM 08.58 HANDOVER DETECT has been received */
322static int ho_rsl_detect(struct gsm_lchan *new_lchan)
323{
324 struct bsc_handover *ho;
325
Harald Welte6f7a5a72009-12-18 11:52:03 +0100326 ho = bsc_ho_by_new_lchan(new_lchan);
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100327 if (!ho) {
328 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
Harald Welte798418a2009-11-29 22:56:14 +0100329 return -ENODEV;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100330 }
Harald Welte798418a2009-11-29 22:56:14 +0100331
332 /* FIXME: do we actually want to do something here ? */
333
334 return 0;
335}
336
337static int ho_logic_sig_cb(unsigned int subsys, unsigned int signal,
338 void *handler_data, void *signal_data)
339{
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +0100340 struct lchan_signal_data *lchan_data;
Harald Welte798418a2009-11-29 22:56:14 +0100341 struct gsm_lchan *lchan;
342
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +0100343 lchan_data = signal_data;
Harald Welte798418a2009-11-29 22:56:14 +0100344 switch (subsys) {
345 case SS_LCHAN:
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +0100346 lchan = lchan_data->lchan;
Harald Welte798418a2009-11-29 22:56:14 +0100347 switch (signal) {
348 case S_LCHAN_ACTIVATE_ACK:
349 return ho_chan_activ_ack(lchan);
350 case S_LCHAN_ACTIVATE_NACK:
351 return ho_chan_activ_nack(lchan);
352 case S_LCHAN_HANDOVER_DETECT:
353 return ho_rsl_detect(lchan);
354 case S_LCHAN_HANDOVER_COMPL:
355 return ho_gsm48_ho_compl(lchan);
356 case S_LCHAN_HANDOVER_FAIL:
357 return ho_gsm48_ho_fail(lchan);
358 }
359 break;
360 default:
361 break;
362 }
363
364 return 0;
365}
366
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +0100367struct gsm_lchan *bsc_handover_pending(struct gsm_lchan *new_lchan)
368{
369 struct bsc_handover *ho;
370 ho = bsc_ho_by_new_lchan(new_lchan);
371 if (!ho)
372 return NULL;
373 return ho->old_lchan;
374}
375
Harald Welte798418a2009-11-29 22:56:14 +0100376static __attribute__((constructor)) void on_dso_load_ho_logic(void)
377{
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200378 osmo_signal_register_handler(SS_LCHAN, ho_logic_sig_cb, NULL);
Harald Welte798418a2009-11-29 22:56:14 +0100379}