blob: 5297ab6cab2e0510e0ea02642980ac088f8d60a1 [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 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;
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;
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 Welteffa55a42009-12-22 19:07:32 +0100220 counter_inc(net->stats.handover.completed);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100221
Harald Welte798418a2009-11-29 22:56:14 +0100222 bsc_del_timer(&ho->T3103);
Harald Welte798418a2009-11-29 22:56:14 +0100223
Harald Weltefe18d5c2009-12-17 17:14:43 +0100224 /* update lchan pointer of transaction */
225 trans_lchan_change(ho->old_lchan, new_lchan);
226
Harald Welteade773f2009-12-21 13:29:19 +0100227 ho->old_lchan->state = LCHAN_S_INACTIVE;
228 lchan_auto_release(ho->old_lchan);
229
Harald Welte798418a2009-11-29 22:56:14 +0100230 /* do something to re-route the actual speech frames ! */
Harald Welte798418a2009-11-29 22:56:14 +0100231
Harald Welteade773f2009-12-21 13:29:19 +0100232 llist_del(&ho->list);
Harald Welte798418a2009-11-29 22:56:14 +0100233 talloc_free(ho);
234
235 return 0;
236}
237
238/* GSM 04.08 HANDOVER FAIL has been received */
239static int ho_gsm48_ho_fail(struct gsm_lchan *old_lchan)
240{
Harald Welteffa55a42009-12-22 19:07:32 +0100241 struct gsm_network *net = old_lchan->ts->trx->bts->network;
Harald Welte798418a2009-11-29 22:56:14 +0100242 struct bsc_handover *ho;
243
244 ho = bsc_ho_by_old_lchan(old_lchan);
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100245 if (!ho) {
246 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
Harald Welte798418a2009-11-29 22:56:14 +0100247 return -ENODEV;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100248 }
Harald Welte798418a2009-11-29 22:56:14 +0100249
Harald Welteffa55a42009-12-22 19:07:32 +0100250 counter_inc(net->stats.handover.failed);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100251
Harald Welte798418a2009-11-29 22:56:14 +0100252 bsc_del_timer(&ho->T3103);
253 llist_del(&ho->list);
254 put_lchan(ho->new_lchan);
255 talloc_free(ho);
256
257 return 0;
258}
259
260/* GSM 08.58 HANDOVER DETECT has been received */
261static int ho_rsl_detect(struct gsm_lchan *new_lchan)
262{
263 struct bsc_handover *ho;
264
Harald Welte6f7a5a72009-12-18 11:52:03 +0100265 ho = bsc_ho_by_new_lchan(new_lchan);
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100266 if (!ho) {
267 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
Harald Welte798418a2009-11-29 22:56:14 +0100268 return -ENODEV;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100269 }
Harald Welte798418a2009-11-29 22:56:14 +0100270
271 /* FIXME: do we actually want to do something here ? */
272
273 return 0;
274}
275
Harald Weltea0379022009-12-20 17:04:40 +0100276static int ho_ipac_crcx_ack(struct gsm_lchan *new_lchan)
277{
278 struct bsc_handover *ho;
279 struct rtp_socket *old_rs, *new_rs, *other_rs;
280
281 ho = bsc_ho_by_new_lchan(new_lchan);
282 if (!ho) {
283 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
284 return -ENODEV;
285 }
286
287 if (ipacc_rtp_direct) {
288 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
289 return 0;
290 }
291
292 /* RTP Proxy mode */
293 new_rs = new_lchan->abis_ip.rtp_socket;
294 old_rs = ho->old_lchan->abis_ip.rtp_socket;
295
296 if (!new_rs) {
297 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
298 return -EIO;
299 }
300
301 rsl_ipacc_mdcx_to_rtpsock(new_lchan);
302
303 if (!old_rs) {
304 LOGP(DHO, LOGL_ERROR, "no RTP socekt for old_lchan\n");
305 return -EIO;
306 }
307
308 /* copy rx_action and reference to other sock */
309 new_rs->rx_action = old_rs->rx_action;
310 new_rs->tx_action = old_rs->tx_action;
311 new_rs->transmit = old_rs->transmit;
312
313 switch (ho->old_lchan->abis_ip.rtp_socket->rx_action) {
314 case RTP_PROXY:
315 other_rs = old_rs->proxy.other_sock;
316 rtp_socket_proxy(new_rs, other_rs);
317 /* delete reference to other end socket to prevent
318 * rtp_socket_free() from removing the inverse reference */
319 old_rs->proxy.other_sock = NULL;
320 break;
321 case RTP_RECV_UPSTREAM:
322 new_rs->receive = old_rs->receive;
323 break;
324 case RTP_NONE:
325 break;
326 }
327
328 return 0;
329}
330
Harald Welte798418a2009-11-29 22:56:14 +0100331static int ho_logic_sig_cb(unsigned int subsys, unsigned int signal,
332 void *handler_data, void *signal_data)
333{
334 struct gsm_lchan *lchan;
335
336 switch (subsys) {
337 case SS_LCHAN:
338 lchan = signal_data;
339 switch (signal) {
340 case S_LCHAN_ACTIVATE_ACK:
341 return ho_chan_activ_ack(lchan);
342 case S_LCHAN_ACTIVATE_NACK:
343 return ho_chan_activ_nack(lchan);
344 case S_LCHAN_HANDOVER_DETECT:
345 return ho_rsl_detect(lchan);
346 case S_LCHAN_HANDOVER_COMPL:
347 return ho_gsm48_ho_compl(lchan);
348 case S_LCHAN_HANDOVER_FAIL:
349 return ho_gsm48_ho_fail(lchan);
350 }
351 break;
Harald Weltea0379022009-12-20 17:04:40 +0100352 case SS_ABISIP:
353 lchan = signal_data;
354 switch (signal) {
355 case S_ABISIP_CRCX_ACK:
356 return ho_ipac_crcx_ack(lchan);
357 break;
358 }
359 break;
Harald Welte798418a2009-11-29 22:56:14 +0100360 default:
361 break;
362 }
363
364 return 0;
365}
366
367static __attribute__((constructor)) void on_dso_load_ho_logic(void)
368{
369 register_signal_handler(SS_LCHAN, ho_logic_sig_cb, NULL);
Harald Weltea0379022009-12-20 17:04:40 +0100370 register_signal_handler(SS_ABISIP, ho_logic_sig_cb, NULL);
Harald Welte798418a2009-11-29 22:56:14 +0100371}