blob: 7352d6464224183fca77a9b2a5ce2e0f6782b90d [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
Harald Welte798418a2009-11-29 22:56:14 +0100102 new_lchan = lchan_alloc(bts, old_lchan->type);
Harald Welte8d77b952009-12-17 00:31:10 +0100103 if (!new_lchan) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100104 LOGP(DHO, LOGL_NOTICE, "No free channel\n");
Harald Welteffa55a42009-12-22 19:07:32 +0100105 counter_inc(bts->network->stats.handover.no_channel);
Harald Welte798418a2009-11-29 22:56:14 +0100106 return -ENOSPC;
Harald Welte8d77b952009-12-17 00:31:10 +0100107 }
Harald Welte798418a2009-11-29 22:56:14 +0100108
109 ho = talloc_zero(NULL, struct bsc_handover);
110 if (!ho) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100111 LOGP(DHO, LOGL_FATAL, "Out of Memory\n");
Harald Welte798418a2009-11-29 22:56:14 +0100112 lchan_free(new_lchan);
113 return -ENOMEM;
114 }
115 ho->old_lchan = old_lchan;
116 ho->new_lchan = new_lchan;
Harald Welte8d77b952009-12-17 00:31:10 +0100117 ho->ho_ref = ho_ref++;
118
119 /* copy some parameters from old lchan */
120 memcpy(&new_lchan->encr, &old_lchan->encr, sizeof(new_lchan->encr));
121 new_lchan->ms_power = old_lchan->ms_power;
122 new_lchan->bs_power = old_lchan->bs_power;
123 new_lchan->rsl_cmode = old_lchan->rsl_cmode;
124 new_lchan->tch_mode = old_lchan->tch_mode;
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100125 new_lchan->conn.subscr = subscr_get(old_lchan->conn.subscr);
Harald Welte798418a2009-11-29 22:56:14 +0100126
127 /* FIXME: do we have a better idea of the timing advance? */
Harald Welte8d77b952009-12-17 00:31:10 +0100128 rc = rsl_chan_activate_lchan(new_lchan, RSL_ACT_INTER_ASYNC, 0,
129 ho->ho_ref);
Harald Welte798418a2009-11-29 22:56:14 +0100130 if (rc < 0) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100131 LOGP(DHO, LOGL_ERROR, "could not activate channel\n");
Harald Welte798418a2009-11-29 22:56:14 +0100132 talloc_free(ho);
133 lchan_free(new_lchan);
134 return rc;
135 }
136
137 llist_add(&ho->list, &bsc_handovers);
138 /* we continue in the SS_LCHAN handler / ho_chan_activ_ack */
139
140 return 0;
141}
142
143/* T3103 expired: Handover has failed without HO COMPLETE or HO FAIL */
144static void ho_T3103_cb(void *_ho)
145{
146 struct bsc_handover *ho = _ho;
Harald Welteffa55a42009-12-22 19:07:32 +0100147 struct gsm_network *net = ho->new_lchan->ts->trx->bts->network;
Harald Welte798418a2009-11-29 22:56:14 +0100148
Harald Welte8d77b952009-12-17 00:31:10 +0100149 DEBUGP(DHO, "HO T3103 expired\n");
Harald Welteffa55a42009-12-22 19:07:32 +0100150 counter_inc(net->stats.handover.timeout);
Harald Welte8d77b952009-12-17 00:31:10 +0100151
Harald Welte798418a2009-11-29 22:56:14 +0100152 lchan_free(ho->new_lchan);
153 llist_del(&ho->list);
154 talloc_free(ho);
155}
156
157/* RSL has acknowledged activation of the new lchan */
158static int ho_chan_activ_ack(struct gsm_lchan *new_lchan)
159{
160 struct bsc_handover *ho;
161 int rc;
162
Harald Weltea9fa8dc2009-12-18 14:50:08 +0100163 /* we need to check if this channel activation is related to
164 * a handover at all (and if, which particular handover) */
Harald Welte798418a2009-11-29 22:56:14 +0100165 ho = bsc_ho_by_new_lchan(new_lchan);
Harald Weltea9fa8dc2009-12-18 14:50:08 +0100166 if (!ho)
Harald Welte798418a2009-11-29 22:56:14 +0100167 return -ENODEV;
Harald Weltea9fa8dc2009-12-18 14:50:08 +0100168
169 DEBUGP(DHO, "handover activate ack, send HO Command\n");
Harald Welte798418a2009-11-29 22:56:14 +0100170
171 /* we can now send the 04.08 HANDOVER COMMAND to the MS
172 * using the old lchan */
173
Harald Welte8d77b952009-12-17 00:31:10 +0100174 rc = gsm48_send_ho_cmd(ho->old_lchan, new_lchan, 0, ho->ho_ref);
Harald Welte798418a2009-11-29 22:56:14 +0100175
176 /* start T3103. We can continue either with T3103 expiration,
177 * 04.08 HANDOVER COMPLETE or 04.08 HANDOVER FAIL */
178 ho->T3103.cb = ho_T3103_cb;
Harald Weltee47f96b2009-12-18 11:49:03 +0100179 ho->T3103.data = ho;
Harald Welte798418a2009-11-29 22:56:14 +0100180 bsc_schedule_timer(&ho->T3103, 10, 0);
181
Harald Weltea0379022009-12-20 17:04:40 +0100182 /* create a RTP connection */
183 if (is_ipaccess_bts(new_lchan->ts->trx->bts))
184 rsl_ipacc_crcx(new_lchan);
185
Harald Welte798418a2009-11-29 22:56:14 +0100186 return 0;
187}
188
189/* RSL has not acknowledged activation of the new lchan */
190static int ho_chan_activ_nack(struct gsm_lchan *new_lchan)
191{
192 struct bsc_handover *ho;
193
194 ho = bsc_ho_by_new_lchan(new_lchan);
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100195 if (!ho) {
196 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
Harald Welte798418a2009-11-29 22:56:14 +0100197 return -ENODEV;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100198 }
Harald Welte798418a2009-11-29 22:56:14 +0100199
200 llist_del(&ho->list);
201 talloc_free(ho);
202
203 /* FIXME: maybe we should try to allocate a new LCHAN here? */
204
205 return 0;
206}
207
208/* GSM 04.08 HANDOVER COMPLETE has been received on new channel */
209static int ho_gsm48_ho_compl(struct gsm_lchan *new_lchan)
210{
Harald Welteffa55a42009-12-22 19:07:32 +0100211 struct gsm_network *net = new_lchan->ts->trx->bts->network;
Harald Welte798418a2009-11-29 22:56:14 +0100212 struct bsc_handover *ho;
213
214 ho = bsc_ho_by_new_lchan(new_lchan);
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100215 if (!ho) {
216 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
Harald Welte798418a2009-11-29 22:56:14 +0100217 return -ENODEV;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100218 }
Harald Welte798418a2009-11-29 22:56:14 +0100219
Harald Welte7c639a02009-12-25 23:02:50 +0100220 LOGP(DHO, LOGL_INFO, "Subscriber %s HO from BTS %u->%u on ARFCN "
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100221 "%u->%u\n", subscr_name(ho->old_lchan->conn.subscr),
Harald Welte7c639a02009-12-25 23:02:50 +0100222 ho->old_lchan->ts->trx->bts->nr, new_lchan->ts->trx->bts->nr,
223 ho->old_lchan->ts->trx->arfcn, new_lchan->ts->trx->arfcn);
224
Harald Welteffa55a42009-12-22 19:07:32 +0100225 counter_inc(net->stats.handover.completed);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100226
Harald Welte798418a2009-11-29 22:56:14 +0100227 bsc_del_timer(&ho->T3103);
Harald Welte798418a2009-11-29 22:56:14 +0100228
Harald Weltefe18d5c2009-12-17 17:14:43 +0100229 /* update lchan pointer of transaction */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +0100230 trans_lchan_change(&ho->old_lchan->conn, &new_lchan->conn);
Harald Weltefe18d5c2009-12-17 17:14:43 +0100231
Holger Hans Peter Freyther74419492010-04-10 00:12:31 +0200232 rsl_lchan_set_state(ho->old_lchan, LCHAN_S_INACTIVE);
Harald Welteade773f2009-12-21 13:29:19 +0100233 lchan_auto_release(ho->old_lchan);
234
Harald Welte798418a2009-11-29 22:56:14 +0100235 /* do something to re-route the actual speech frames ! */
Harald Welte798418a2009-11-29 22:56:14 +0100236
Harald Welteade773f2009-12-21 13:29:19 +0100237 llist_del(&ho->list);
Harald Welte798418a2009-11-29 22:56:14 +0100238 talloc_free(ho);
239
240 return 0;
241}
242
243/* GSM 04.08 HANDOVER FAIL has been received */
244static int ho_gsm48_ho_fail(struct gsm_lchan *old_lchan)
245{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100246 struct gsm_subscriber_connection *conn;
Harald Welteffa55a42009-12-22 19:07:32 +0100247 struct gsm_network *net = old_lchan->ts->trx->bts->network;
Harald Welte798418a2009-11-29 22:56:14 +0100248 struct bsc_handover *ho;
249
250 ho = bsc_ho_by_old_lchan(old_lchan);
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100251 if (!ho) {
252 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
Harald Welte798418a2009-11-29 22:56:14 +0100253 return -ENODEV;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100254 }
Harald Welte798418a2009-11-29 22:56:14 +0100255
Harald Welteffa55a42009-12-22 19:07:32 +0100256 counter_inc(net->stats.handover.failed);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100257
Harald Welte798418a2009-11-29 22:56:14 +0100258 bsc_del_timer(&ho->T3103);
259 llist_del(&ho->list);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100260 conn = &ho->new_lchan->conn;
261 put_subscr_con(conn);
Harald Welte798418a2009-11-29 22:56:14 +0100262 talloc_free(ho);
263
264 return 0;
265}
266
267/* GSM 08.58 HANDOVER DETECT has been received */
268static int ho_rsl_detect(struct gsm_lchan *new_lchan)
269{
270 struct bsc_handover *ho;
271
Harald Welte6f7a5a72009-12-18 11:52:03 +0100272 ho = bsc_ho_by_new_lchan(new_lchan);
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100273 if (!ho) {
274 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
Harald Welte798418a2009-11-29 22:56:14 +0100275 return -ENODEV;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100276 }
Harald Welte798418a2009-11-29 22:56:14 +0100277
278 /* FIXME: do we actually want to do something here ? */
279
280 return 0;
281}
282
Harald Weltea0379022009-12-20 17:04:40 +0100283static int ho_ipac_crcx_ack(struct gsm_lchan *new_lchan)
284{
285 struct bsc_handover *ho;
286 struct rtp_socket *old_rs, *new_rs, *other_rs;
287
288 ho = bsc_ho_by_new_lchan(new_lchan);
289 if (!ho) {
Harald Weltee98d4272009-12-24 13:27:02 +0100290 /* it is perfectly normal, we have CRCX even in non-HO cases */
291 return 0;
Harald Weltea0379022009-12-20 17:04:40 +0100292 }
293
294 if (ipacc_rtp_direct) {
295 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
296 return 0;
297 }
298
299 /* RTP Proxy mode */
300 new_rs = new_lchan->abis_ip.rtp_socket;
301 old_rs = ho->old_lchan->abis_ip.rtp_socket;
302
303 if (!new_rs) {
304 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
305 return -EIO;
306 }
307
308 rsl_ipacc_mdcx_to_rtpsock(new_lchan);
309
310 if (!old_rs) {
311 LOGP(DHO, LOGL_ERROR, "no RTP socekt for old_lchan\n");
312 return -EIO;
313 }
314
315 /* copy rx_action and reference to other sock */
316 new_rs->rx_action = old_rs->rx_action;
317 new_rs->tx_action = old_rs->tx_action;
318 new_rs->transmit = old_rs->transmit;
319
320 switch (ho->old_lchan->abis_ip.rtp_socket->rx_action) {
321 case RTP_PROXY:
322 other_rs = old_rs->proxy.other_sock;
323 rtp_socket_proxy(new_rs, other_rs);
324 /* delete reference to other end socket to prevent
325 * rtp_socket_free() from removing the inverse reference */
326 old_rs->proxy.other_sock = NULL;
327 break;
328 case RTP_RECV_UPSTREAM:
329 new_rs->receive = old_rs->receive;
330 break;
331 case RTP_NONE:
332 break;
333 }
334
335 return 0;
336}
337
Harald Welte798418a2009-11-29 22:56:14 +0100338static int ho_logic_sig_cb(unsigned int subsys, unsigned int signal,
339 void *handler_data, void *signal_data)
340{
341 struct gsm_lchan *lchan;
342
343 switch (subsys) {
344 case SS_LCHAN:
345 lchan = signal_data;
346 switch (signal) {
347 case S_LCHAN_ACTIVATE_ACK:
348 return ho_chan_activ_ack(lchan);
349 case S_LCHAN_ACTIVATE_NACK:
350 return ho_chan_activ_nack(lchan);
351 case S_LCHAN_HANDOVER_DETECT:
352 return ho_rsl_detect(lchan);
353 case S_LCHAN_HANDOVER_COMPL:
354 return ho_gsm48_ho_compl(lchan);
355 case S_LCHAN_HANDOVER_FAIL:
356 return ho_gsm48_ho_fail(lchan);
357 }
358 break;
Harald Weltea0379022009-12-20 17:04:40 +0100359 case SS_ABISIP:
360 lchan = signal_data;
361 switch (signal) {
362 case S_ABISIP_CRCX_ACK:
363 return ho_ipac_crcx_ack(lchan);
364 break;
365 }
366 break;
Harald Welte798418a2009-11-29 22:56:14 +0100367 default:
368 break;
369 }
370
371 return 0;
372}
373
374static __attribute__((constructor)) void on_dso_load_ho_logic(void)
375{
376 register_signal_handler(SS_LCHAN, ho_logic_sig_cb, NULL);
Harald Weltea0379022009-12-20 17:04:40 +0100377 register_signal_handler(SS_ABISIP, ho_logic_sig_cb, NULL);
Harald Welte798418a2009-11-29 22:56:14 +0100378}