blob: 393b0f2ad67cd94e6be5cff2bdd316223bc479e1 [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
32#include <openbsc/msgb.h>
33#include <openbsc/debug.h>
34#include <openbsc/gsm_data.h>
35#include <openbsc/gsm_utils.h>
36#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>
41#include <openbsc/talloc.h>
42#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 Welte24ff6ee2009-12-22 00:41:05 +0100100 bts->network->stats.handover.attempted++;
101
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 Welte24ff6ee2009-12-22 00:41:05 +0100105 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;
Harald Weltea9fa8dc2009-12-18 14:50:08 +0100125 new_lchan->subscr = subscr_get(old_lchan->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;
147
Harald Welte8d77b952009-12-17 00:31:10 +0100148 DEBUGP(DHO, "HO T3103 expired\n");
Harald Welte24ff6ee2009-12-22 00:41:05 +0100149 ho->new_lchan->ts->trx->bts->network->stats.handover.timeout++;
Harald Welte8d77b952009-12-17 00:31:10 +0100150
Harald Welte798418a2009-11-29 22:56:14 +0100151 lchan_free(ho->new_lchan);
152 llist_del(&ho->list);
153 talloc_free(ho);
154}
155
156/* RSL has acknowledged activation of the new lchan */
157static int ho_chan_activ_ack(struct gsm_lchan *new_lchan)
158{
159 struct bsc_handover *ho;
160 int rc;
161
Harald Weltea9fa8dc2009-12-18 14:50:08 +0100162 /* we need to check if this channel activation is related to
163 * a handover at all (and if, which particular handover) */
Harald Welte798418a2009-11-29 22:56:14 +0100164 ho = bsc_ho_by_new_lchan(new_lchan);
Harald Weltea9fa8dc2009-12-18 14:50:08 +0100165 if (!ho)
Harald Welte798418a2009-11-29 22:56:14 +0100166 return -ENODEV;
Harald Weltea9fa8dc2009-12-18 14:50:08 +0100167
168 DEBUGP(DHO, "handover activate ack, send HO Command\n");
Harald Welte798418a2009-11-29 22:56:14 +0100169
170 /* we can now send the 04.08 HANDOVER COMMAND to the MS
171 * using the old lchan */
172
Harald Welte8d77b952009-12-17 00:31:10 +0100173 rc = gsm48_send_ho_cmd(ho->old_lchan, new_lchan, 0, ho->ho_ref);
Harald Welte798418a2009-11-29 22:56:14 +0100174
175 /* start T3103. We can continue either with T3103 expiration,
176 * 04.08 HANDOVER COMPLETE or 04.08 HANDOVER FAIL */
177 ho->T3103.cb = ho_T3103_cb;
Harald Weltee47f96b2009-12-18 11:49:03 +0100178 ho->T3103.data = ho;
Harald Welte798418a2009-11-29 22:56:14 +0100179 bsc_schedule_timer(&ho->T3103, 10, 0);
180
Harald Weltea0379022009-12-20 17:04:40 +0100181 /* create a RTP connection */
182 if (is_ipaccess_bts(new_lchan->ts->trx->bts))
183 rsl_ipacc_crcx(new_lchan);
184
Harald Welte798418a2009-11-29 22:56:14 +0100185 return 0;
186}
187
188/* RSL has not acknowledged activation of the new lchan */
189static int ho_chan_activ_nack(struct gsm_lchan *new_lchan)
190{
191 struct bsc_handover *ho;
192
193 ho = bsc_ho_by_new_lchan(new_lchan);
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100194 if (!ho) {
195 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
Harald Welte798418a2009-11-29 22:56:14 +0100196 return -ENODEV;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100197 }
Harald Welte798418a2009-11-29 22:56:14 +0100198
199 llist_del(&ho->list);
200 talloc_free(ho);
201
202 /* FIXME: maybe we should try to allocate a new LCHAN here? */
203
204 return 0;
205}
206
207/* GSM 04.08 HANDOVER COMPLETE has been received on new channel */
208static int ho_gsm48_ho_compl(struct gsm_lchan *new_lchan)
209{
210 struct bsc_handover *ho;
211
212 ho = bsc_ho_by_new_lchan(new_lchan);
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100213 if (!ho) {
214 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
Harald Welte798418a2009-11-29 22:56:14 +0100215 return -ENODEV;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100216 }
Harald Welte798418a2009-11-29 22:56:14 +0100217
Harald Welte24ff6ee2009-12-22 00:41:05 +0100218 new_lchan->ts->trx->bts->network->stats.handover.completed++;
219
Harald Welte798418a2009-11-29 22:56:14 +0100220 bsc_del_timer(&ho->T3103);
Harald Welte798418a2009-11-29 22:56:14 +0100221
Harald Weltefe18d5c2009-12-17 17:14:43 +0100222 /* update lchan pointer of transaction */
223 trans_lchan_change(ho->old_lchan, new_lchan);
224
Harald Welteade773f2009-12-21 13:29:19 +0100225 ho->old_lchan->state = LCHAN_S_INACTIVE;
226 lchan_auto_release(ho->old_lchan);
227
Harald Welte798418a2009-11-29 22:56:14 +0100228 /* do something to re-route the actual speech frames ! */
Harald Welte798418a2009-11-29 22:56:14 +0100229
Harald Welteade773f2009-12-21 13:29:19 +0100230 llist_del(&ho->list);
Harald Welte798418a2009-11-29 22:56:14 +0100231 talloc_free(ho);
232
233 return 0;
234}
235
236/* GSM 04.08 HANDOVER FAIL has been received */
237static int ho_gsm48_ho_fail(struct gsm_lchan *old_lchan)
238{
239 struct bsc_handover *ho;
240
241 ho = bsc_ho_by_old_lchan(old_lchan);
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100242 if (!ho) {
243 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
Harald Welte798418a2009-11-29 22:56:14 +0100244 return -ENODEV;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100245 }
Harald Welte798418a2009-11-29 22:56:14 +0100246
Harald Welte24ff6ee2009-12-22 00:41:05 +0100247 old_lchan->ts->trx->bts->network->stats.handover.failed++;
248
Harald Welte798418a2009-11-29 22:56:14 +0100249 bsc_del_timer(&ho->T3103);
250 llist_del(&ho->list);
251 put_lchan(ho->new_lchan);
252 talloc_free(ho);
253
254 return 0;
255}
256
257/* GSM 08.58 HANDOVER DETECT has been received */
258static int ho_rsl_detect(struct gsm_lchan *new_lchan)
259{
260 struct bsc_handover *ho;
261
Harald Welte6f7a5a72009-12-18 11:52:03 +0100262 ho = bsc_ho_by_new_lchan(new_lchan);
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100263 if (!ho) {
264 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
Harald Welte798418a2009-11-29 22:56:14 +0100265 return -ENODEV;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100266 }
Harald Welte798418a2009-11-29 22:56:14 +0100267
268 /* FIXME: do we actually want to do something here ? */
269
270 return 0;
271}
272
Harald Weltea0379022009-12-20 17:04:40 +0100273static int ho_ipac_crcx_ack(struct gsm_lchan *new_lchan)
274{
275 struct bsc_handover *ho;
276 struct rtp_socket *old_rs, *new_rs, *other_rs;
277
278 ho = bsc_ho_by_new_lchan(new_lchan);
279 if (!ho) {
280 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
281 return -ENODEV;
282 }
283
284 if (ipacc_rtp_direct) {
285 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
286 return 0;
287 }
288
289 /* RTP Proxy mode */
290 new_rs = new_lchan->abis_ip.rtp_socket;
291 old_rs = ho->old_lchan->abis_ip.rtp_socket;
292
293 if (!new_rs) {
294 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
295 return -EIO;
296 }
297
298 rsl_ipacc_mdcx_to_rtpsock(new_lchan);
299
300 if (!old_rs) {
301 LOGP(DHO, LOGL_ERROR, "no RTP socekt for old_lchan\n");
302 return -EIO;
303 }
304
305 /* copy rx_action and reference to other sock */
306 new_rs->rx_action = old_rs->rx_action;
307 new_rs->tx_action = old_rs->tx_action;
308 new_rs->transmit = old_rs->transmit;
309
310 switch (ho->old_lchan->abis_ip.rtp_socket->rx_action) {
311 case RTP_PROXY:
312 other_rs = old_rs->proxy.other_sock;
313 rtp_socket_proxy(new_rs, other_rs);
314 /* delete reference to other end socket to prevent
315 * rtp_socket_free() from removing the inverse reference */
316 old_rs->proxy.other_sock = NULL;
317 break;
318 case RTP_RECV_UPSTREAM:
319 new_rs->receive = old_rs->receive;
320 break;
321 case RTP_NONE:
322 break;
323 }
324
325 return 0;
326}
327
Harald Welte798418a2009-11-29 22:56:14 +0100328static int ho_logic_sig_cb(unsigned int subsys, unsigned int signal,
329 void *handler_data, void *signal_data)
330{
331 struct gsm_lchan *lchan;
332
333 switch (subsys) {
334 case SS_LCHAN:
335 lchan = signal_data;
336 switch (signal) {
337 case S_LCHAN_ACTIVATE_ACK:
338 return ho_chan_activ_ack(lchan);
339 case S_LCHAN_ACTIVATE_NACK:
340 return ho_chan_activ_nack(lchan);
341 case S_LCHAN_HANDOVER_DETECT:
342 return ho_rsl_detect(lchan);
343 case S_LCHAN_HANDOVER_COMPL:
344 return ho_gsm48_ho_compl(lchan);
345 case S_LCHAN_HANDOVER_FAIL:
346 return ho_gsm48_ho_fail(lchan);
347 }
348 break;
Harald Weltea0379022009-12-20 17:04:40 +0100349 case SS_ABISIP:
350 lchan = signal_data;
351 switch (signal) {
352 case S_ABISIP_CRCX_ACK:
353 return ho_ipac_crcx_ack(lchan);
354 break;
355 }
356 break;
Harald Welte798418a2009-11-29 22:56:14 +0100357 default:
358 break;
359 }
360
361 return 0;
362}
363
364static __attribute__((constructor)) void on_dso_load_ho_logic(void)
365{
366 register_signal_handler(SS_LCHAN, ho_logic_sig_cb, NULL);
Harald Weltea0379022009-12-20 17:04:40 +0100367 register_signal_handler(SS_ABISIP, ho_logic_sig_cb, NULL);
Harald Welte798418a2009-11-29 22:56:14 +0100368}