blob: 36a758be6b4e77c686c511335b37eb25ead8e982 [file] [log] [blame]
Harald Welte96c0b082009-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 Welte0e3e88e2011-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 Welte96c0b082009-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 Welte0e3e88e2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Harald Welte96c0b082009-11-29 22:56:14 +010018 *
Harald Welte0e3e88e2011-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 Welte96c0b082009-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 Ayusodd5fff42011-03-22 16:47:59 +010031#include <osmocom/core/msgb.h>
Harald Welte96c0b082009-11-29 22:56:14 +010032#include <openbsc/debug.h>
33#include <openbsc/gsm_data.h>
Pablo Neira Ayusodd5fff42011-03-22 16:47:59 +010034#include <osmocom/gsm/gsm_utils.h>
Harald Welte96c0b082009-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 Ayusodd5fff42011-03-22 16:47:59 +010040#include <osmocom/core/talloc.h>
Harald Welte96c0b082009-11-29 22:56:14 +010041#include <openbsc/transaction.h>
Andreas Eversberg02063672013-12-05 14:37:11 +010042#include <openbsc/trau_mux.h>
Harald Welte96c0b082009-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 Ayuso840ccf62011-05-06 12:11:06 +020050 struct osmo_timer_list T3103;
Harald Welte96c0b082009-11-29 22:56:14 +010051
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +020052 uint8_t ho_ref;
Harald Welte96c0b082009-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 Freyther7eb8a9a2011-04-18 17:04:00 +020088 static uint8_t ho_ref;
Harald Welte96c0b082009-11-29 22:56:14 +010089 int rc;
90
Harald Welteaef376f2009-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 Weltecf2ec4a2009-12-17 23:10:46 +010096 DEBUGP(DHO, "(old_lchan on BTS %u, new BTS %u)\n",
Harald Welteb90d7bd2009-12-17 00:31:10 +010097 old_lchan->ts->trx->bts->nr, bts->nr);
98
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +020099 osmo_counter_inc(bts->network->stats.handover.attempted);
Harald Welte3edc5a92009-12-22 00:41:05 +0100100
Holger Hans Peter Freytherbac7ff72010-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 Freytherdb392032010-09-06 08:58:42 +0800106 new_lchan = lchan_alloc(bts, old_lchan->type, 0);
Harald Welteb90d7bd2009-12-17 00:31:10 +0100107 if (!new_lchan) {
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100108 LOGP(DHO, LOGL_NOTICE, "No free channel\n");
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200109 osmo_counter_inc(bts->network->stats.handover.no_channel);
Harald Welte96c0b082009-11-29 22:56:14 +0100110 return -ENOSPC;
Harald Welteb90d7bd2009-12-17 00:31:10 +0100111 }
Harald Welte96c0b082009-11-29 22:56:14 +0100112
Holger Hans Peter Freyther394ba232010-12-21 13:29:14 +0100113 ho = talloc_zero(tall_bsc_ctx, struct bsc_handover);
Harald Welte96c0b082009-11-29 22:56:14 +0100114 if (!ho) {
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100115 LOGP(DHO, LOGL_FATAL, "Out of Memory\n");
Harald Welte96c0b082009-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 Welteb90d7bd2009-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 Freytherbac7ff72010-06-30 12:40:10 +0800129
130 new_lchan->conn = old_lchan->conn;
131 new_lchan->conn->ho_lchan = new_lchan;
Harald Welte96c0b082009-11-29 22:56:14 +0100132
133 /* FIXME: do we have a better idea of the timing advance? */
Harald Welteb90d7bd2009-12-17 00:31:10 +0100134 rc = rsl_chan_activate_lchan(new_lchan, RSL_ACT_INTER_ASYNC, 0,
135 ho->ho_ref);
Harald Welte96c0b082009-11-29 22:56:14 +0100136 if (rc < 0) {
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100137 LOGP(DHO, LOGL_ERROR, "could not activate channel\n");
Holger Hans Peter Freytherbac7ff72010-06-30 12:40:10 +0800138 new_lchan->conn->ho_lchan = NULL;
Holger Hans Peter Freyther36cd62c2010-12-26 19:59:32 +0100139 new_lchan->conn = NULL;
Harald Welte96c0b082009-11-29 22:56:14 +0100140 talloc_free(ho);
141 lchan_free(new_lchan);
142 return rc;
143 }
144
Holger Hans Peter Freyther599056d2010-04-10 00:16:04 +0200145 rsl_lchan_set_state(new_lchan, LCHAN_S_ACT_REQ);
Harald Welte96c0b082009-11-29 22:56:14 +0100146 llist_add(&ho->list, &bsc_handovers);
147 /* we continue in the SS_LCHAN handler / ho_chan_activ_ack */
148
149 return 0;
150}
151
Holger Hans Peter Freytherfc0db5c2010-12-27 13:46:48 +0100152void bsc_clear_handover(struct gsm_subscriber_connection *conn, int free_lchan)
Holger Hans Peter Freytherdf5f7242010-06-30 12:58:14 +0800153{
154 struct bsc_handover *ho;
155
156 ho = bsc_ho_by_new_lchan(conn->ho_lchan);
157
158
159 if (!ho && conn->ho_lchan)
160 LOGP(DHO, LOGL_ERROR, "BUG: We lost some state.\n");
161
162 if (!ho) {
163 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
164 return;
165 }
166
167 conn->ho_lchan->conn = NULL;
168 conn->ho_lchan = NULL;
Holger Hans Peter Freytherfc0db5c2010-12-27 13:46:48 +0100169
170 if (free_lchan)
Holger Hans Peter Freytherc260adb2012-12-06 12:20:56 +0100171 lchan_release(ho->new_lchan, 0, RSL_REL_LOCAL_END);
Holger Hans Peter Freytherdf5f7242010-06-30 12:58:14 +0800172
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +0200173 osmo_timer_del(&ho->T3103);
Holger Hans Peter Freytherdf5f7242010-06-30 12:58:14 +0800174 llist_del(&ho->list);
175 talloc_free(ho);
176}
177
Harald Welte96c0b082009-11-29 22:56:14 +0100178/* T3103 expired: Handover has failed without HO COMPLETE or HO FAIL */
179static void ho_T3103_cb(void *_ho)
180{
181 struct bsc_handover *ho = _ho;
Harald Weltebdbb7442009-12-22 19:07:32 +0100182 struct gsm_network *net = ho->new_lchan->ts->trx->bts->network;
Harald Welte96c0b082009-11-29 22:56:14 +0100183
Harald Welteb90d7bd2009-12-17 00:31:10 +0100184 DEBUGP(DHO, "HO T3103 expired\n");
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200185 osmo_counter_inc(net->stats.handover.timeout);
Harald Welteb90d7bd2009-12-17 00:31:10 +0100186
Holger Hans Peter Freytherb93d08c2010-06-30 13:04:13 +0800187 ho->new_lchan->conn->ho_lchan = NULL;
188 ho->new_lchan->conn = NULL;
Holger Hans Peter Freytherc260adb2012-12-06 12:20:56 +0100189 lchan_release(ho->new_lchan, 0, RSL_REL_LOCAL_END);
Harald Welte96c0b082009-11-29 22:56:14 +0100190 llist_del(&ho->list);
191 talloc_free(ho);
192}
193
194/* RSL has acknowledged activation of the new lchan */
195static int ho_chan_activ_ack(struct gsm_lchan *new_lchan)
196{
197 struct bsc_handover *ho;
Harald Welte96c0b082009-11-29 22:56:14 +0100198
Harald Weltefedc19e2009-12-18 14:50:08 +0100199 /* we need to check if this channel activation is related to
200 * a handover at all (and if, which particular handover) */
Harald Welte96c0b082009-11-29 22:56:14 +0100201 ho = bsc_ho_by_new_lchan(new_lchan);
Harald Weltefedc19e2009-12-18 14:50:08 +0100202 if (!ho)
Harald Welte96c0b082009-11-29 22:56:14 +0100203 return -ENODEV;
Harald Weltefedc19e2009-12-18 14:50:08 +0100204
205 DEBUGP(DHO, "handover activate ack, send HO Command\n");
Harald Welte96c0b082009-11-29 22:56:14 +0100206
207 /* we can now send the 04.08 HANDOVER COMMAND to the MS
208 * using the old lchan */
209
Holger Hans Peter Freyther1a9f3ee2012-09-11 11:55:03 +0200210 gsm48_send_ho_cmd(ho->old_lchan, new_lchan, 0, ho->ho_ref);
Harald Welte96c0b082009-11-29 22:56:14 +0100211
212 /* start T3103. We can continue either with T3103 expiration,
213 * 04.08 HANDOVER COMPLETE or 04.08 HANDOVER FAIL */
214 ho->T3103.cb = ho_T3103_cb;
Harald Welte6d8220c2009-12-18 11:49:03 +0100215 ho->T3103.data = ho;
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +0200216 osmo_timer_schedule(&ho->T3103, 10, 0);
Harald Welte96c0b082009-11-29 22:56:14 +0100217
Harald Weltef8189fe2009-12-20 17:04:40 +0100218 /* create a RTP connection */
219 if (is_ipaccess_bts(new_lchan->ts->trx->bts))
220 rsl_ipacc_crcx(new_lchan);
221
Harald Welte96c0b082009-11-29 22:56:14 +0100222 return 0;
223}
224
225/* RSL has not acknowledged activation of the new lchan */
226static int ho_chan_activ_nack(struct gsm_lchan *new_lchan)
227{
228 struct bsc_handover *ho;
229
230 ho = bsc_ho_by_new_lchan(new_lchan);
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100231 if (!ho) {
Harald Welte07ccb9c2012-07-06 14:21:55 +0200232 LOGP(DHO, LOGL_INFO, "ACT NACK: unable to find HO record\n");
Harald Welte96c0b082009-11-29 22:56:14 +0100233 return -ENODEV;
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100234 }
Harald Welte96c0b082009-11-29 22:56:14 +0100235
Holger Hans Peter Freythere0a492a2010-12-21 13:30:17 +0100236 new_lchan->conn->ho_lchan = NULL;
237 new_lchan->conn = NULL;
Harald Welte96c0b082009-11-29 22:56:14 +0100238 llist_del(&ho->list);
239 talloc_free(ho);
240
241 /* FIXME: maybe we should try to allocate a new LCHAN here? */
242
243 return 0;
244}
245
246/* GSM 04.08 HANDOVER COMPLETE has been received on new channel */
247static int ho_gsm48_ho_compl(struct gsm_lchan *new_lchan)
248{
Holger Hans Peter Freytherd7087812010-12-26 20:34:26 +0100249 struct gsm_network *net;
Harald Welte96c0b082009-11-29 22:56:14 +0100250 struct bsc_handover *ho;
251
252 ho = bsc_ho_by_new_lchan(new_lchan);
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100253 if (!ho) {
254 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
Harald Welte96c0b082009-11-29 22:56:14 +0100255 return -ENODEV;
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100256 }
Harald Welte96c0b082009-11-29 22:56:14 +0100257
Holger Hans Peter Freytherd7087812010-12-26 20:34:26 +0100258 net = new_lchan->ts->trx->bts->network;
Harald Welte58eeaba2009-12-25 23:02:50 +0100259 LOGP(DHO, LOGL_INFO, "Subscriber %s HO from BTS %u->%u on ARFCN "
Holger Hans Peter Freyther1a95fa82010-06-28 15:47:12 +0800260 "%u->%u\n", subscr_name(ho->old_lchan->conn->subscr),
Harald Welte58eeaba2009-12-25 23:02:50 +0100261 ho->old_lchan->ts->trx->bts->nr, new_lchan->ts->trx->bts->nr,
262 ho->old_lchan->ts->trx->arfcn, new_lchan->ts->trx->arfcn);
263
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200264 osmo_counter_inc(net->stats.handover.completed);
Harald Welte3edc5a92009-12-22 00:41:05 +0100265
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +0200266 osmo_timer_del(&ho->T3103);
Harald Welte96c0b082009-11-29 22:56:14 +0100267
Andreas Eversberg02063672013-12-05 14:37:11 +0100268 /* switch TRAU muxer for E1 based BTS from one channel to another */
269 if (is_e1_bts(new_lchan->conn->bts))
270 switch_trau_mux(ho->old_lchan, new_lchan);
271
Holger Hans Peter Freytherbac7ff72010-06-30 12:40:10 +0800272 /* Replace the ho lchan with the primary one */
273 if (ho->old_lchan != new_lchan->conn->lchan)
274 LOGP(DHO, LOGL_ERROR, "Primary lchan changed during handover.\n");
275
276 if (new_lchan != new_lchan->conn->ho_lchan)
277 LOGP(DHO, LOGL_ERROR, "Handover channel changed during this handover.\n");
278
279 new_lchan->conn->ho_lchan = NULL;
280 new_lchan->conn->lchan = new_lchan;
281 ho->old_lchan->conn = NULL;
Harald Weltef8c50702009-12-17 17:14:43 +0100282
Holger Hans Peter Freyther68914a02010-04-10 00:12:31 +0200283 rsl_lchan_set_state(ho->old_lchan, LCHAN_S_INACTIVE);
Holger Hans Peter Freytherc260adb2012-12-06 12:20:56 +0100284 lchan_release(ho->old_lchan, 0, RSL_REL_LOCAL_END);
Harald Welte83c59812009-12-21 13:29:19 +0100285
Harald Welte83c59812009-12-21 13:29:19 +0100286 llist_del(&ho->list);
Harald Welte96c0b082009-11-29 22:56:14 +0100287 talloc_free(ho);
288
289 return 0;
290}
291
292/* GSM 04.08 HANDOVER FAIL has been received */
293static int ho_gsm48_ho_fail(struct gsm_lchan *old_lchan)
294{
Harald Weltebdbb7442009-12-22 19:07:32 +0100295 struct gsm_network *net = old_lchan->ts->trx->bts->network;
Harald Welte96c0b082009-11-29 22:56:14 +0100296 struct bsc_handover *ho;
297
298 ho = bsc_ho_by_old_lchan(old_lchan);
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100299 if (!ho) {
300 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
Harald Welte96c0b082009-11-29 22:56:14 +0100301 return -ENODEV;
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100302 }
Harald Welte96c0b082009-11-29 22:56:14 +0100303
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200304 osmo_counter_inc(net->stats.handover.failed);
Harald Welte3edc5a92009-12-22 00:41:05 +0100305
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +0200306 osmo_timer_del(&ho->T3103);
Harald Welte96c0b082009-11-29 22:56:14 +0100307 llist_del(&ho->list);
Holger Hans Peter Freytherbac7ff72010-06-30 12:40:10 +0800308
309 /* release the channel and forget about it */
310 ho->new_lchan->conn->ho_lchan = NULL;
311 ho->new_lchan->conn = NULL;
Holger Hans Peter Freytherc260adb2012-12-06 12:20:56 +0100312 lchan_release(ho->new_lchan, 0, RSL_REL_LOCAL_END);
Holger Hans Peter Freytherbac7ff72010-06-30 12:40:10 +0800313
Harald Welte96c0b082009-11-29 22:56:14 +0100314 talloc_free(ho);
315
316 return 0;
317}
318
319/* GSM 08.58 HANDOVER DETECT has been received */
320static int ho_rsl_detect(struct gsm_lchan *new_lchan)
321{
322 struct bsc_handover *ho;
323
Harald Weltebc2e5392009-12-18 11:52:03 +0100324 ho = bsc_ho_by_new_lchan(new_lchan);
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100325 if (!ho) {
326 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
Harald Welte96c0b082009-11-29 22:56:14 +0100327 return -ENODEV;
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100328 }
Harald Welte96c0b082009-11-29 22:56:14 +0100329
330 /* FIXME: do we actually want to do something here ? */
331
332 return 0;
333}
334
335static int ho_logic_sig_cb(unsigned int subsys, unsigned int signal,
336 void *handler_data, void *signal_data)
337{
Holger Hans Peter Freyther645b3832010-12-27 13:28:20 +0100338 struct lchan_signal_data *lchan_data;
Harald Welte96c0b082009-11-29 22:56:14 +0100339 struct gsm_lchan *lchan;
340
Holger Hans Peter Freyther645b3832010-12-27 13:28:20 +0100341 lchan_data = signal_data;
Harald Welte96c0b082009-11-29 22:56:14 +0100342 switch (subsys) {
343 case SS_LCHAN:
Holger Hans Peter Freyther645b3832010-12-27 13:28:20 +0100344 lchan = lchan_data->lchan;
Harald Welte96c0b082009-11-29 22:56:14 +0100345 switch (signal) {
346 case S_LCHAN_ACTIVATE_ACK:
347 return ho_chan_activ_ack(lchan);
348 case S_LCHAN_ACTIVATE_NACK:
349 return ho_chan_activ_nack(lchan);
350 case S_LCHAN_HANDOVER_DETECT:
351 return ho_rsl_detect(lchan);
352 case S_LCHAN_HANDOVER_COMPL:
353 return ho_gsm48_ho_compl(lchan);
354 case S_LCHAN_HANDOVER_FAIL:
355 return ho_gsm48_ho_fail(lchan);
356 }
357 break;
358 default:
359 break;
360 }
361
362 return 0;
363}
364
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +0100365struct gsm_lchan *bsc_handover_pending(struct gsm_lchan *new_lchan)
366{
367 struct bsc_handover *ho;
368 ho = bsc_ho_by_new_lchan(new_lchan);
369 if (!ho)
370 return NULL;
371 return ho->old_lchan;
372}
373
Harald Welte96c0b082009-11-29 22:56:14 +0100374static __attribute__((constructor)) void on_dso_load_ho_logic(void)
375{
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200376 osmo_signal_register_handler(SS_LCHAN, ho_logic_sig_cb, NULL);
Harald Welte96c0b082009-11-29 22:56:14 +0100377}