blob: 22f9883fdddef05437ce0c2aab4bc9be4d2dc224 [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
57static struct bsc_handover *bsc_ho_by_new_lchan(struct gsm_lchan *new_lchan)
58{
59 struct bsc_handover *ho;
60
61 llist_for_each_entry(ho, &bsc_handovers, list) {
62 if (ho->new_lchan == new_lchan)
63 return ho;
64 }
65
66 return NULL;
67}
68
69static struct bsc_handover *bsc_ho_by_old_lchan(struct gsm_lchan *old_lchan)
70{
71 struct bsc_handover *ho;
72
73 llist_for_each_entry(ho, &bsc_handovers, list) {
74 if (ho->old_lchan == old_lchan)
75 return ho;
76 }
77
78 return NULL;
79}
80
81/* Hand over the specified logical channel to the specified new BTS.
82 * This is the main entry point for the actual handover algorithm,
83 * after it has decided it wants to initiate HO to a specific BTS */
84int bsc_handover_start(struct gsm_lchan *old_lchan, struct gsm_bts *bts)
85{
86 struct gsm_lchan *new_lchan;
87 struct bsc_handover *ho;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020088 static uint8_t ho_ref;
Harald Welte798418a2009-11-29 22:56:14 +010089 int rc;
90
Harald Welte66706812009-12-17 22:23:21 +010091 /* don't attempt multiple handovers for the same lchan at
92 * the same time */
93 if (bsc_ho_by_old_lchan(old_lchan))
94 return -EBUSY;
95
Harald Welteb1d4c8e2009-12-17 23:10:46 +010096 DEBUGP(DHO, "(old_lchan on BTS %u, new BTS %u)\n",
Harald Welte8d77b952009-12-17 00:31:10 +010097 old_lchan->ts->trx->bts->nr, bts->nr);
98
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +020099 osmo_counter_inc(bts->network->stats.handover.attempted);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100100
Holger Hans Peter Freythere071ab72010-06-30 12:40:10 +0800101 if (!old_lchan->conn) {
102 LOGP(DHO, LOGL_ERROR, "Old lchan lacks connection data.\n");
103 return -ENOSPC;
104 }
105
Holger Hans Peter Freyther457c2a82010-09-06 08:58:42 +0800106 new_lchan = lchan_alloc(bts, old_lchan->type, 0);
Harald Welte8d77b952009-12-17 00:31:10 +0100107 if (!new_lchan) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100108 LOGP(DHO, LOGL_NOTICE, "No free channel\n");
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200109 osmo_counter_inc(bts->network->stats.handover.no_channel);
Harald Welte798418a2009-11-29 22:56:14 +0100110 return -ENOSPC;
Harald Welte8d77b952009-12-17 00:31:10 +0100111 }
Harald Welte798418a2009-11-29 22:56:14 +0100112
Holger Hans Peter Freyther90cdd282010-12-21 13:29:14 +0100113 ho = talloc_zero(tall_bsc_ctx, struct bsc_handover);
Harald Welte798418a2009-11-29 22:56:14 +0100114 if (!ho) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100115 LOGP(DHO, LOGL_FATAL, "Out of Memory\n");
Harald Welte798418a2009-11-29 22:56:14 +0100116 lchan_free(new_lchan);
117 return -ENOMEM;
118 }
119 ho->old_lchan = old_lchan;
120 ho->new_lchan = new_lchan;
Harald Welte8d77b952009-12-17 00:31:10 +0100121 ho->ho_ref = ho_ref++;
122
123 /* copy some parameters from old lchan */
124 memcpy(&new_lchan->encr, &old_lchan->encr, sizeof(new_lchan->encr));
125 new_lchan->ms_power = old_lchan->ms_power;
126 new_lchan->bs_power = old_lchan->bs_power;
127 new_lchan->rsl_cmode = old_lchan->rsl_cmode;
128 new_lchan->tch_mode = old_lchan->tch_mode;
Holger Hans Peter Freythere071ab72010-06-30 12:40:10 +0800129
130 new_lchan->conn = old_lchan->conn;
131 new_lchan->conn->ho_lchan = new_lchan;
Harald Welte798418a2009-11-29 22:56:14 +0100132
133 /* FIXME: do we have a better idea of the timing advance? */
Andreas Eversberg723a7512013-10-11 12:55:35 +0200134 rc = rsl_chan_activate_lchan(new_lchan, RSL_ACT_INTER_ASYNC, ho->ho_ref);
Harald Welte798418a2009-11-29 22:56:14 +0100135 if (rc < 0) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100136 LOGP(DHO, LOGL_ERROR, "could not activate channel\n");
Holger Hans Peter Freythere071ab72010-06-30 12:40:10 +0800137 new_lchan->conn->ho_lchan = NULL;
Holger Hans Peter Freytherc8396672010-12-26 19:59:32 +0100138 new_lchan->conn = NULL;
Harald Welte798418a2009-11-29 22:56:14 +0100139 talloc_free(ho);
140 lchan_free(new_lchan);
141 return rc;
142 }
143
Holger Hans Peter Freyther5eec9d92010-04-10 00:16:04 +0200144 rsl_lchan_set_state(new_lchan, LCHAN_S_ACT_REQ);
Harald Welte798418a2009-11-29 22:56:14 +0100145 llist_add(&ho->list, &bsc_handovers);
146 /* we continue in the SS_LCHAN handler / ho_chan_activ_ack */
147
148 return 0;
149}
150
Holger Hans Peter Freytherebd50a62010-12-27 13:46:48 +0100151void bsc_clear_handover(struct gsm_subscriber_connection *conn, int free_lchan)
Holger Hans Peter Freytherf2553a62010-06-30 12:58:14 +0800152{
153 struct bsc_handover *ho;
154
155 ho = bsc_ho_by_new_lchan(conn->ho_lchan);
156
157
158 if (!ho && conn->ho_lchan)
159 LOGP(DHO, LOGL_ERROR, "BUG: We lost some state.\n");
160
161 if (!ho) {
162 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
163 return;
164 }
165
166 conn->ho_lchan->conn = NULL;
167 conn->ho_lchan = NULL;
Holger Hans Peter Freytherebd50a62010-12-27 13:46:48 +0100168
169 if (free_lchan)
Holger Hans Peter Freytherd66777f2012-12-06 12:20:56 +0100170 lchan_release(ho->new_lchan, 0, RSL_REL_LOCAL_END);
Holger Hans Peter Freytherf2553a62010-06-30 12:58:14 +0800171
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200172 osmo_timer_del(&ho->T3103);
Holger Hans Peter Freytherf2553a62010-06-30 12:58:14 +0800173 llist_del(&ho->list);
174 talloc_free(ho);
175}
176
Harald Welte798418a2009-11-29 22:56:14 +0100177/* T3103 expired: Handover has failed without HO COMPLETE or HO FAIL */
178static void ho_T3103_cb(void *_ho)
179{
180 struct bsc_handover *ho = _ho;
Harald Welteffa55a42009-12-22 19:07:32 +0100181 struct gsm_network *net = ho->new_lchan->ts->trx->bts->network;
Harald Welte798418a2009-11-29 22:56:14 +0100182
Harald Welte8d77b952009-12-17 00:31:10 +0100183 DEBUGP(DHO, "HO T3103 expired\n");
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200184 osmo_counter_inc(net->stats.handover.timeout);
Harald Welte8d77b952009-12-17 00:31:10 +0100185
Holger Hans Peter Freytherd9c9f072010-06-30 13:04:13 +0800186 ho->new_lchan->conn->ho_lchan = NULL;
187 ho->new_lchan->conn = NULL;
Holger Hans Peter Freytherd66777f2012-12-06 12:20:56 +0100188 lchan_release(ho->new_lchan, 0, RSL_REL_LOCAL_END);
Harald Welte798418a2009-11-29 22:56:14 +0100189 llist_del(&ho->list);
190 talloc_free(ho);
191}
192
193/* RSL has acknowledged activation of the new lchan */
194static int ho_chan_activ_ack(struct gsm_lchan *new_lchan)
195{
196 struct bsc_handover *ho;
Harald Welte798418a2009-11-29 22:56:14 +0100197
Harald Weltea9fa8dc2009-12-18 14:50:08 +0100198 /* we need to check if this channel activation is related to
199 * a handover at all (and if, which particular handover) */
Harald Welte798418a2009-11-29 22:56:14 +0100200 ho = bsc_ho_by_new_lchan(new_lchan);
Harald Weltea9fa8dc2009-12-18 14:50:08 +0100201 if (!ho)
Harald Welte798418a2009-11-29 22:56:14 +0100202 return -ENODEV;
Harald Weltea9fa8dc2009-12-18 14:50:08 +0100203
204 DEBUGP(DHO, "handover activate ack, send HO Command\n");
Harald Welte798418a2009-11-29 22:56:14 +0100205
206 /* we can now send the 04.08 HANDOVER COMMAND to the MS
207 * using the old lchan */
208
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200209 gsm48_send_ho_cmd(ho->old_lchan, new_lchan, 0, ho->ho_ref);
Harald Welte798418a2009-11-29 22:56:14 +0100210
211 /* start T3103. We can continue either with T3103 expiration,
212 * 04.08 HANDOVER COMPLETE or 04.08 HANDOVER FAIL */
213 ho->T3103.cb = ho_T3103_cb;
Harald Weltee47f96b2009-12-18 11:49:03 +0100214 ho->T3103.data = ho;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200215 osmo_timer_schedule(&ho->T3103, 10, 0);
Harald Welte798418a2009-11-29 22:56:14 +0100216
Harald Weltea0379022009-12-20 17:04:40 +0100217 /* create a RTP connection */
218 if (is_ipaccess_bts(new_lchan->ts->trx->bts))
219 rsl_ipacc_crcx(new_lchan);
220
Harald Welte798418a2009-11-29 22:56:14 +0100221 return 0;
222}
223
224/* RSL has not acknowledged activation of the new lchan */
225static int ho_chan_activ_nack(struct gsm_lchan *new_lchan)
226{
227 struct bsc_handover *ho;
228
229 ho = bsc_ho_by_new_lchan(new_lchan);
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100230 if (!ho) {
Harald Welte25cf8242012-07-06 14:21:55 +0200231 LOGP(DHO, LOGL_INFO, "ACT NACK: unable to find HO record\n");
Harald Welte798418a2009-11-29 22:56:14 +0100232 return -ENODEV;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100233 }
Harald Welte798418a2009-11-29 22:56:14 +0100234
Holger Hans Peter Freyther2391b4c2010-12-21 13:30:17 +0100235 new_lchan->conn->ho_lchan = NULL;
236 new_lchan->conn = NULL;
Harald Welte798418a2009-11-29 22:56:14 +0100237 llist_del(&ho->list);
238 talloc_free(ho);
239
240 /* FIXME: maybe we should try to allocate a new LCHAN here? */
241
242 return 0;
243}
244
245/* GSM 04.08 HANDOVER COMPLETE has been received on new channel */
246static int ho_gsm48_ho_compl(struct gsm_lchan *new_lchan)
247{
Holger Hans Peter Freyther9d3e2ec2010-12-26 20:34:26 +0100248 struct gsm_network *net;
Harald Welte798418a2009-11-29 22:56:14 +0100249 struct bsc_handover *ho;
250
251 ho = bsc_ho_by_new_lchan(new_lchan);
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100252 if (!ho) {
253 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
Harald Welte798418a2009-11-29 22:56:14 +0100254 return -ENODEV;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100255 }
Harald Welte798418a2009-11-29 22:56:14 +0100256
Holger Hans Peter Freyther9d3e2ec2010-12-26 20:34:26 +0100257 net = new_lchan->ts->trx->bts->network;
Harald Welte7c639a02009-12-25 23:02:50 +0100258 LOGP(DHO, LOGL_INFO, "Subscriber %s HO from BTS %u->%u on ARFCN "
Holger Hans Peter Freyther2412a072010-06-28 15:47:12 +0800259 "%u->%u\n", subscr_name(ho->old_lchan->conn->subscr),
Harald Welte7c639a02009-12-25 23:02:50 +0100260 ho->old_lchan->ts->trx->bts->nr, new_lchan->ts->trx->bts->nr,
261 ho->old_lchan->ts->trx->arfcn, new_lchan->ts->trx->arfcn);
262
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200263 osmo_counter_inc(net->stats.handover.completed);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100264
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200265 osmo_timer_del(&ho->T3103);
Harald Welte798418a2009-11-29 22:56:14 +0100266
Andreas Eversbergdcf38e12013-12-05 14:37:11 +0100267 /* switch TRAU muxer for E1 based BTS from one channel to another */
268 if (is_e1_bts(new_lchan->conn->bts))
269 switch_trau_mux(ho->old_lchan, new_lchan);
270
Holger Hans Peter Freythere071ab72010-06-30 12:40:10 +0800271 /* Replace the ho lchan with the primary one */
272 if (ho->old_lchan != new_lchan->conn->lchan)
273 LOGP(DHO, LOGL_ERROR, "Primary lchan changed during handover.\n");
274
275 if (new_lchan != new_lchan->conn->ho_lchan)
276 LOGP(DHO, LOGL_ERROR, "Handover channel changed during this handover.\n");
277
278 new_lchan->conn->ho_lchan = NULL;
279 new_lchan->conn->lchan = new_lchan;
280 ho->old_lchan->conn = NULL;
Harald Weltefe18d5c2009-12-17 17:14:43 +0100281
Holger Hans Peter Freyther74419492010-04-10 00:12:31 +0200282 rsl_lchan_set_state(ho->old_lchan, LCHAN_S_INACTIVE);
Holger Hans Peter Freytherd66777f2012-12-06 12:20:56 +0100283 lchan_release(ho->old_lchan, 0, RSL_REL_LOCAL_END);
Harald Welteade773f2009-12-21 13:29:19 +0100284
Harald Welteade773f2009-12-21 13:29:19 +0100285 llist_del(&ho->list);
Harald Welte798418a2009-11-29 22:56:14 +0100286 talloc_free(ho);
287
288 return 0;
289}
290
291/* GSM 04.08 HANDOVER FAIL has been received */
292static int ho_gsm48_ho_fail(struct gsm_lchan *old_lchan)
293{
Harald Welteffa55a42009-12-22 19:07:32 +0100294 struct gsm_network *net = old_lchan->ts->trx->bts->network;
Harald Welte798418a2009-11-29 22:56:14 +0100295 struct bsc_handover *ho;
296
297 ho = bsc_ho_by_old_lchan(old_lchan);
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100298 if (!ho) {
299 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
Harald Welte798418a2009-11-29 22:56:14 +0100300 return -ENODEV;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100301 }
Harald Welte798418a2009-11-29 22:56:14 +0100302
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200303 osmo_counter_inc(net->stats.handover.failed);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100304
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200305 osmo_timer_del(&ho->T3103);
Harald Welte798418a2009-11-29 22:56:14 +0100306 llist_del(&ho->list);
Holger Hans Peter Freythere071ab72010-06-30 12:40:10 +0800307
308 /* release the channel and forget about it */
309 ho->new_lchan->conn->ho_lchan = NULL;
310 ho->new_lchan->conn = NULL;
Holger Hans Peter Freytherd66777f2012-12-06 12:20:56 +0100311 lchan_release(ho->new_lchan, 0, RSL_REL_LOCAL_END);
Holger Hans Peter Freythere071ab72010-06-30 12:40:10 +0800312
Harald Welte798418a2009-11-29 22:56:14 +0100313 talloc_free(ho);
314
315 return 0;
316}
317
318/* GSM 08.58 HANDOVER DETECT has been received */
319static int ho_rsl_detect(struct gsm_lchan *new_lchan)
320{
321 struct bsc_handover *ho;
322
Harald Welte6f7a5a72009-12-18 11:52:03 +0100323 ho = bsc_ho_by_new_lchan(new_lchan);
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100324 if (!ho) {
325 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
Harald Welte798418a2009-11-29 22:56:14 +0100326 return -ENODEV;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100327 }
Harald Welte798418a2009-11-29 22:56:14 +0100328
329 /* FIXME: do we actually want to do something here ? */
330
331 return 0;
332}
333
334static int ho_logic_sig_cb(unsigned int subsys, unsigned int signal,
335 void *handler_data, void *signal_data)
336{
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +0100337 struct lchan_signal_data *lchan_data;
Harald Welte798418a2009-11-29 22:56:14 +0100338 struct gsm_lchan *lchan;
339
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +0100340 lchan_data = signal_data;
Harald Welte798418a2009-11-29 22:56:14 +0100341 switch (subsys) {
342 case SS_LCHAN:
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +0100343 lchan = lchan_data->lchan;
Harald Welte798418a2009-11-29 22:56:14 +0100344 switch (signal) {
345 case S_LCHAN_ACTIVATE_ACK:
346 return ho_chan_activ_ack(lchan);
347 case S_LCHAN_ACTIVATE_NACK:
348 return ho_chan_activ_nack(lchan);
349 case S_LCHAN_HANDOVER_DETECT:
350 return ho_rsl_detect(lchan);
351 case S_LCHAN_HANDOVER_COMPL:
352 return ho_gsm48_ho_compl(lchan);
353 case S_LCHAN_HANDOVER_FAIL:
354 return ho_gsm48_ho_fail(lchan);
355 }
356 break;
357 default:
358 break;
359 }
360
361 return 0;
362}
363
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +0100364struct gsm_lchan *bsc_handover_pending(struct gsm_lchan *new_lchan)
365{
366 struct bsc_handover *ho;
367 ho = bsc_ho_by_new_lchan(new_lchan);
368 if (!ho)
369 return NULL;
370 return ho->old_lchan;
371}
372
Harald Welte798418a2009-11-29 22:56:14 +0100373static __attribute__((constructor)) void on_dso_load_ho_logic(void)
374{
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200375 osmo_signal_register_handler(SS_LCHAN, ho_logic_sig_cb, NULL);
Harald Welte798418a2009-11-29 22:56:14 +0100376}