blob: 38bb32ea238a55ee8ac39816f30c21cadd19d72a [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
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (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
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <errno.h>
29#include <time.h>
30#include <netinet/in.h>
31
Harald Weltedfe6c7d2010-02-20 16:24:02 +010032#include <osmocore/msgb.h>
Harald Welte798418a2009-11-29 22:56:14 +010033#include <openbsc/debug.h>
34#include <openbsc/gsm_data.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010035#include <osmocore/gsm_utils.h>
Harald Welte798418a2009-11-29 22:56:14 +010036#include <openbsc/gsm_subscriber.h>
37#include <openbsc/gsm_04_08.h>
38#include <openbsc/abis_rsl.h>
39#include <openbsc/chan_alloc.h>
40#include <openbsc/signal.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010041#include <osmocore/talloc.h>
Harald Welte798418a2009-11-29 22:56:14 +010042#include <openbsc/transaction.h>
Harald Weltea0379022009-12-20 17:04:40 +010043#include <openbsc/rtp_proxy.h>
Harald Welte798418a2009-11-29 22:56:14 +010044
45struct bsc_handover {
46 struct llist_head list;
47
48 struct gsm_lchan *old_lchan;
49 struct gsm_lchan *new_lchan;
50
51 struct timer_list T3103;
52
53 u_int8_t ho_ref;
54};
55
56static LLIST_HEAD(bsc_handovers);
57
58static struct bsc_handover *bsc_ho_by_new_lchan(struct gsm_lchan *new_lchan)
59{
60 struct bsc_handover *ho;
61
62 llist_for_each_entry(ho, &bsc_handovers, list) {
63 if (ho->new_lchan == new_lchan)
64 return ho;
65 }
66
67 return NULL;
68}
69
70static struct bsc_handover *bsc_ho_by_old_lchan(struct gsm_lchan *old_lchan)
71{
72 struct bsc_handover *ho;
73
74 llist_for_each_entry(ho, &bsc_handovers, list) {
75 if (ho->old_lchan == old_lchan)
76 return ho;
77 }
78
79 return NULL;
80}
81
82/* Hand over the specified logical channel to the specified new BTS.
83 * This is the main entry point for the actual handover algorithm,
84 * after it has decided it wants to initiate HO to a specific BTS */
85int bsc_handover_start(struct gsm_lchan *old_lchan, struct gsm_bts *bts)
86{
87 struct gsm_lchan *new_lchan;
88 struct bsc_handover *ho;
Harald Welte8d77b952009-12-17 00:31:10 +010089 static u_int8_t ho_ref;
Harald Welte798418a2009-11-29 22:56:14 +010090 int rc;
91
Harald Welte66706812009-12-17 22:23:21 +010092 /* don't attempt multiple handovers for the same lchan at
93 * the same time */
94 if (bsc_ho_by_old_lchan(old_lchan))
95 return -EBUSY;
96
Harald Welteb1d4c8e2009-12-17 23:10:46 +010097 DEBUGP(DHO, "(old_lchan on BTS %u, new BTS %u)\n",
Harald Welte8d77b952009-12-17 00:31:10 +010098 old_lchan->ts->trx->bts->nr, bts->nr);
99
Harald Welteffa55a42009-12-22 19:07:32 +0100100 counter_inc(bts->network->stats.handover.attempted);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100101
Holger Hans Peter Freythere071ab72010-06-30 12:40:10 +0800102 if (!old_lchan->conn) {
103 LOGP(DHO, LOGL_ERROR, "Old lchan lacks connection data.\n");
104 return -ENOSPC;
105 }
106
Holger Hans Peter Freyther457c2a82010-09-06 08:58:42 +0800107 new_lchan = lchan_alloc(bts, old_lchan->type, 0);
Harald Welte8d77b952009-12-17 00:31:10 +0100108 if (!new_lchan) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100109 LOGP(DHO, LOGL_NOTICE, "No free channel\n");
Harald Welteffa55a42009-12-22 19:07:32 +0100110 counter_inc(bts->network->stats.handover.no_channel);
Harald Welte798418a2009-11-29 22:56:14 +0100111 return -ENOSPC;
Harald Welte8d77b952009-12-17 00:31:10 +0100112 }
Harald Welte798418a2009-11-29 22:56:14 +0100113
Holger Hans Peter Freyther90cdd282010-12-21 13:29:14 +0100114 ho = talloc_zero(tall_bsc_ctx, struct bsc_handover);
Harald Welte798418a2009-11-29 22:56:14 +0100115 if (!ho) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100116 LOGP(DHO, LOGL_FATAL, "Out of Memory\n");
Harald Welte798418a2009-11-29 22:56:14 +0100117 lchan_free(new_lchan);
118 return -ENOMEM;
119 }
120 ho->old_lchan = old_lchan;
121 ho->new_lchan = new_lchan;
Harald Welte8d77b952009-12-17 00:31:10 +0100122 ho->ho_ref = ho_ref++;
123
124 /* copy some parameters from old lchan */
125 memcpy(&new_lchan->encr, &old_lchan->encr, sizeof(new_lchan->encr));
126 new_lchan->ms_power = old_lchan->ms_power;
127 new_lchan->bs_power = old_lchan->bs_power;
128 new_lchan->rsl_cmode = old_lchan->rsl_cmode;
129 new_lchan->tch_mode = old_lchan->tch_mode;
Holger Hans Peter Freythere071ab72010-06-30 12:40:10 +0800130
131 new_lchan->conn = old_lchan->conn;
132 new_lchan->conn->ho_lchan = new_lchan;
Harald Welte798418a2009-11-29 22:56:14 +0100133
134 /* FIXME: do we have a better idea of the timing advance? */
Harald Welte8d77b952009-12-17 00:31:10 +0100135 rc = rsl_chan_activate_lchan(new_lchan, RSL_ACT_INTER_ASYNC, 0,
136 ho->ho_ref);
Harald Welte798418a2009-11-29 22:56:14 +0100137 if (rc < 0) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100138 LOGP(DHO, LOGL_ERROR, "could not activate channel\n");
Holger Hans Peter Freythere071ab72010-06-30 12:40:10 +0800139 new_lchan->conn->ho_lchan = NULL;
Holger Hans Peter Freytherc8396672010-12-26 19:59:32 +0100140 new_lchan->conn = NULL;
Harald Welte798418a2009-11-29 22:56:14 +0100141 talloc_free(ho);
142 lchan_free(new_lchan);
143 return rc;
144 }
145
Holger Hans Peter Freyther5eec9d92010-04-10 00:16:04 +0200146 rsl_lchan_set_state(new_lchan, LCHAN_S_ACT_REQ);
Harald Welte798418a2009-11-29 22:56:14 +0100147 llist_add(&ho->list, &bsc_handovers);
148 /* we continue in the SS_LCHAN handler / ho_chan_activ_ack */
149
150 return 0;
151}
152
Holger Hans Peter Freytherf2553a62010-06-30 12:58:14 +0800153void bsc_clear_handover(struct gsm_subscriber_connection *conn)
154{
155 struct bsc_handover *ho;
156
157 ho = bsc_ho_by_new_lchan(conn->ho_lchan);
158
159
160 if (!ho && conn->ho_lchan)
161 LOGP(DHO, LOGL_ERROR, "BUG: We lost some state.\n");
162
163 if (!ho) {
164 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
165 return;
166 }
167
168 conn->ho_lchan->conn = NULL;
169 conn->ho_lchan = NULL;
170 lchan_release(ho->new_lchan, 0, 1);
171
172 bsc_del_timer(&ho->T3103);
173 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");
Harald Welteffa55a42009-12-22 19:07:32 +0100184 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;
188 lchan_release(ho->new_lchan, 0, 1);
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;
197 int rc;
198
Harald Weltea9fa8dc2009-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 Welte798418a2009-11-29 22:56:14 +0100201 ho = bsc_ho_by_new_lchan(new_lchan);
Harald Weltea9fa8dc2009-12-18 14:50:08 +0100202 if (!ho)
Harald Welte798418a2009-11-29 22:56:14 +0100203 return -ENODEV;
Harald Weltea9fa8dc2009-12-18 14:50:08 +0100204
205 DEBUGP(DHO, "handover activate ack, send HO Command\n");
Harald Welte798418a2009-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
Harald Welte8d77b952009-12-17 00:31:10 +0100210 rc = gsm48_send_ho_cmd(ho->old_lchan, new_lchan, 0, ho->ho_ref);
Harald Welte798418a2009-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 Weltee47f96b2009-12-18 11:49:03 +0100215 ho->T3103.data = ho;
Harald Welte798418a2009-11-29 22:56:14 +0100216 bsc_schedule_timer(&ho->T3103, 10, 0);
217
Harald Weltea0379022009-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 Welte798418a2009-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 Welteb1d4c8e2009-12-17 23:10:46 +0100231 if (!ho) {
232 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
Harald Welte798418a2009-11-29 22:56:14 +0100233 return -ENODEV;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100234 }
Harald Welte798418a2009-11-29 22:56:14 +0100235
Holger Hans Peter Freyther2391b4c2010-12-21 13:30:17 +0100236 new_lchan->conn->ho_lchan = NULL;
237 new_lchan->conn = NULL;
Harald Welte798418a2009-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 Freyther9d3e2ec2010-12-26 20:34:26 +0100249 struct gsm_network *net;
Harald Welte798418a2009-11-29 22:56:14 +0100250 struct bsc_handover *ho;
251
252 ho = bsc_ho_by_new_lchan(new_lchan);
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100253 if (!ho) {
254 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
Harald Welte798418a2009-11-29 22:56:14 +0100255 return -ENODEV;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100256 }
Harald Welte798418a2009-11-29 22:56:14 +0100257
Holger Hans Peter Freyther9d3e2ec2010-12-26 20:34:26 +0100258 net = new_lchan->ts->trx->bts->network;
Harald Welte7c639a02009-12-25 23:02:50 +0100259 LOGP(DHO, LOGL_INFO, "Subscriber %s HO from BTS %u->%u on ARFCN "
Holger Hans Peter Freyther2412a072010-06-28 15:47:12 +0800260 "%u->%u\n", subscr_name(ho->old_lchan->conn->subscr),
Harald Welte7c639a02009-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
Harald Welteffa55a42009-12-22 19:07:32 +0100264 counter_inc(net->stats.handover.completed);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100265
Harald Welte798418a2009-11-29 22:56:14 +0100266 bsc_del_timer(&ho->T3103);
Harald Welte798418a2009-11-29 22:56:14 +0100267
Holger Hans Peter Freythere071ab72010-06-30 12:40:10 +0800268 /* Replace the ho lchan with the primary one */
269 if (ho->old_lchan != new_lchan->conn->lchan)
270 LOGP(DHO, LOGL_ERROR, "Primary lchan changed during handover.\n");
271
272 if (new_lchan != new_lchan->conn->ho_lchan)
273 LOGP(DHO, LOGL_ERROR, "Handover channel changed during this handover.\n");
274
275 new_lchan->conn->ho_lchan = NULL;
276 new_lchan->conn->lchan = new_lchan;
277 ho->old_lchan->conn = NULL;
Harald Weltefe18d5c2009-12-17 17:14:43 +0100278
Holger Hans Peter Freyther74419492010-04-10 00:12:31 +0200279 rsl_lchan_set_state(ho->old_lchan, LCHAN_S_INACTIVE);
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800280 lchan_release(ho->old_lchan, 0, 1);
Harald Welteade773f2009-12-21 13:29:19 +0100281
Harald Welte798418a2009-11-29 22:56:14 +0100282 /* do something to re-route the actual speech frames ! */
Harald Welte798418a2009-11-29 22:56:14 +0100283
Harald Welteade773f2009-12-21 13:29:19 +0100284 llist_del(&ho->list);
Harald Welte798418a2009-11-29 22:56:14 +0100285 talloc_free(ho);
286
287 return 0;
288}
289
290/* GSM 04.08 HANDOVER FAIL has been received */
291static int ho_gsm48_ho_fail(struct gsm_lchan *old_lchan)
292{
Harald Welteffa55a42009-12-22 19:07:32 +0100293 struct gsm_network *net = old_lchan->ts->trx->bts->network;
Harald Welte798418a2009-11-29 22:56:14 +0100294 struct bsc_handover *ho;
295
296 ho = bsc_ho_by_old_lchan(old_lchan);
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100297 if (!ho) {
298 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
Harald Welte798418a2009-11-29 22:56:14 +0100299 return -ENODEV;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100300 }
Harald Welte798418a2009-11-29 22:56:14 +0100301
Harald Welteffa55a42009-12-22 19:07:32 +0100302 counter_inc(net->stats.handover.failed);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100303
Harald Welte798418a2009-11-29 22:56:14 +0100304 bsc_del_timer(&ho->T3103);
305 llist_del(&ho->list);
Holger Hans Peter Freythere071ab72010-06-30 12:40:10 +0800306
307 /* release the channel and forget about it */
308 ho->new_lchan->conn->ho_lchan = NULL;
309 ho->new_lchan->conn = NULL;
310 lchan_release(ho->new_lchan, 0, 1);
311
Harald Welte798418a2009-11-29 22:56:14 +0100312 talloc_free(ho);
313
314 return 0;
315}
316
317/* GSM 08.58 HANDOVER DETECT has been received */
318static int ho_rsl_detect(struct gsm_lchan *new_lchan)
319{
320 struct bsc_handover *ho;
321
Harald Welte6f7a5a72009-12-18 11:52:03 +0100322 ho = bsc_ho_by_new_lchan(new_lchan);
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100323 if (!ho) {
324 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
Harald Welte798418a2009-11-29 22:56:14 +0100325 return -ENODEV;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100326 }
Harald Welte798418a2009-11-29 22:56:14 +0100327
328 /* FIXME: do we actually want to do something here ? */
329
330 return 0;
331}
332
Harald Weltea0379022009-12-20 17:04:40 +0100333static int ho_ipac_crcx_ack(struct gsm_lchan *new_lchan)
334{
335 struct bsc_handover *ho;
336 struct rtp_socket *old_rs, *new_rs, *other_rs;
337
338 ho = bsc_ho_by_new_lchan(new_lchan);
339 if (!ho) {
Harald Weltee98d4272009-12-24 13:27:02 +0100340 /* it is perfectly normal, we have CRCX even in non-HO cases */
341 return 0;
Harald Weltea0379022009-12-20 17:04:40 +0100342 }
343
344 if (ipacc_rtp_direct) {
345 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
346 return 0;
347 }
348
349 /* RTP Proxy mode */
350 new_rs = new_lchan->abis_ip.rtp_socket;
351 old_rs = ho->old_lchan->abis_ip.rtp_socket;
352
353 if (!new_rs) {
354 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
355 return -EIO;
356 }
357
358 rsl_ipacc_mdcx_to_rtpsock(new_lchan);
359
360 if (!old_rs) {
361 LOGP(DHO, LOGL_ERROR, "no RTP socekt for old_lchan\n");
362 return -EIO;
363 }
364
365 /* copy rx_action and reference to other sock */
366 new_rs->rx_action = old_rs->rx_action;
367 new_rs->tx_action = old_rs->tx_action;
368 new_rs->transmit = old_rs->transmit;
369
370 switch (ho->old_lchan->abis_ip.rtp_socket->rx_action) {
371 case RTP_PROXY:
372 other_rs = old_rs->proxy.other_sock;
373 rtp_socket_proxy(new_rs, other_rs);
374 /* delete reference to other end socket to prevent
375 * rtp_socket_free() from removing the inverse reference */
376 old_rs->proxy.other_sock = NULL;
377 break;
378 case RTP_RECV_UPSTREAM:
379 new_rs->receive = old_rs->receive;
380 break;
381 case RTP_NONE:
382 break;
383 }
384
385 return 0;
386}
387
Harald Welte798418a2009-11-29 22:56:14 +0100388static int ho_logic_sig_cb(unsigned int subsys, unsigned int signal,
389 void *handler_data, void *signal_data)
390{
391 struct gsm_lchan *lchan;
392
393 switch (subsys) {
394 case SS_LCHAN:
395 lchan = signal_data;
396 switch (signal) {
397 case S_LCHAN_ACTIVATE_ACK:
398 return ho_chan_activ_ack(lchan);
399 case S_LCHAN_ACTIVATE_NACK:
400 return ho_chan_activ_nack(lchan);
401 case S_LCHAN_HANDOVER_DETECT:
402 return ho_rsl_detect(lchan);
403 case S_LCHAN_HANDOVER_COMPL:
404 return ho_gsm48_ho_compl(lchan);
405 case S_LCHAN_HANDOVER_FAIL:
406 return ho_gsm48_ho_fail(lchan);
407 }
408 break;
Harald Weltea0379022009-12-20 17:04:40 +0100409 case SS_ABISIP:
410 lchan = signal_data;
411 switch (signal) {
412 case S_ABISIP_CRCX_ACK:
413 return ho_ipac_crcx_ack(lchan);
414 break;
415 }
416 break;
Harald Welte798418a2009-11-29 22:56:14 +0100417 default:
418 break;
419 }
420
421 return 0;
422}
423
424static __attribute__((constructor)) void on_dso_load_ho_logic(void)
425{
426 register_signal_handler(SS_LCHAN, ho_logic_sig_cb, NULL);
Harald Weltea0379022009-12-20 17:04:40 +0100427 register_signal_handler(SS_ABISIP, ho_logic_sig_cb, NULL);
Harald Welte798418a2009-11-29 22:56:14 +0100428}