blob: f3f5d6c210202000ea27648cf1e87bc49fe5362d [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>
43
44struct bsc_handover {
45 struct llist_head list;
46
47 struct gsm_lchan *old_lchan;
48 struct gsm_lchan *new_lchan;
49
50 struct timer_list T3103;
51
52 u_int8_t ho_ref;
53};
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;
Harald Welte8d77b952009-12-17 00:31:10 +010088 static u_int8_t ho_ref;
Harald Welte798418a2009-11-29 22:56:14 +010089 int rc;
90
Harald Welte66706812009-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 Welteb1d4c8e2009-12-17 23:10:46 +010096 DEBUGP(DHO, "(old_lchan on BTS %u, new BTS %u)\n",
Harald Welte8d77b952009-12-17 00:31:10 +010097 old_lchan->ts->trx->bts->nr, bts->nr);
98
Harald Welte798418a2009-11-29 22:56:14 +010099 new_lchan = lchan_alloc(bts, old_lchan->type);
Harald Welte8d77b952009-12-17 00:31:10 +0100100 if (!new_lchan) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100101 LOGP(DHO, LOGL_NOTICE, "No free channel\n");
Harald Welte798418a2009-11-29 22:56:14 +0100102 return -ENOSPC;
Harald Welte8d77b952009-12-17 00:31:10 +0100103 }
Harald Welte798418a2009-11-29 22:56:14 +0100104
105 ho = talloc_zero(NULL, struct bsc_handover);
106 if (!ho) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100107 LOGP(DHO, LOGL_FATAL, "Out of Memory\n");
Harald Welte798418a2009-11-29 22:56:14 +0100108 lchan_free(new_lchan);
109 return -ENOMEM;
110 }
111 ho->old_lchan = old_lchan;
112 ho->new_lchan = new_lchan;
Harald Welte8d77b952009-12-17 00:31:10 +0100113 ho->ho_ref = ho_ref++;
114
115 /* copy some parameters from old lchan */
116 memcpy(&new_lchan->encr, &old_lchan->encr, sizeof(new_lchan->encr));
117 new_lchan->ms_power = old_lchan->ms_power;
118 new_lchan->bs_power = old_lchan->bs_power;
119 new_lchan->rsl_cmode = old_lchan->rsl_cmode;
120 new_lchan->tch_mode = old_lchan->tch_mode;
Harald Weltea9fa8dc2009-12-18 14:50:08 +0100121 new_lchan->subscr = subscr_get(old_lchan->subscr);
Harald Welte798418a2009-11-29 22:56:14 +0100122
123 /* FIXME: do we have a better idea of the timing advance? */
Harald Welte8d77b952009-12-17 00:31:10 +0100124 rc = rsl_chan_activate_lchan(new_lchan, RSL_ACT_INTER_ASYNC, 0,
125 ho->ho_ref);
Harald Welte798418a2009-11-29 22:56:14 +0100126 if (rc < 0) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100127 LOGP(DHO, LOGL_ERROR, "could not activate channel\n");
Harald Welte798418a2009-11-29 22:56:14 +0100128 talloc_free(ho);
129 lchan_free(new_lchan);
130 return rc;
131 }
132
133 llist_add(&ho->list, &bsc_handovers);
134 /* we continue in the SS_LCHAN handler / ho_chan_activ_ack */
135
136 return 0;
137}
138
139/* T3103 expired: Handover has failed without HO COMPLETE or HO FAIL */
140static void ho_T3103_cb(void *_ho)
141{
142 struct bsc_handover *ho = _ho;
143
Harald Welte8d77b952009-12-17 00:31:10 +0100144 DEBUGP(DHO, "HO T3103 expired\n");
145
Harald Welte798418a2009-11-29 22:56:14 +0100146 lchan_free(ho->new_lchan);
147 llist_del(&ho->list);
148 talloc_free(ho);
149}
150
151/* RSL has acknowledged activation of the new lchan */
152static int ho_chan_activ_ack(struct gsm_lchan *new_lchan)
153{
154 struct bsc_handover *ho;
155 int rc;
156
Harald Weltea9fa8dc2009-12-18 14:50:08 +0100157 /* we need to check if this channel activation is related to
158 * a handover at all (and if, which particular handover) */
Harald Welte798418a2009-11-29 22:56:14 +0100159 ho = bsc_ho_by_new_lchan(new_lchan);
Harald Weltea9fa8dc2009-12-18 14:50:08 +0100160 if (!ho)
Harald Welte798418a2009-11-29 22:56:14 +0100161 return -ENODEV;
Harald Weltea9fa8dc2009-12-18 14:50:08 +0100162
163 DEBUGP(DHO, "handover activate ack, send HO Command\n");
Harald Welte798418a2009-11-29 22:56:14 +0100164
165 /* we can now send the 04.08 HANDOVER COMMAND to the MS
166 * using the old lchan */
167
Harald Welte8d77b952009-12-17 00:31:10 +0100168 rc = gsm48_send_ho_cmd(ho->old_lchan, new_lchan, 0, ho->ho_ref);
Harald Welte798418a2009-11-29 22:56:14 +0100169
170 /* start T3103. We can continue either with T3103 expiration,
171 * 04.08 HANDOVER COMPLETE or 04.08 HANDOVER FAIL */
172 ho->T3103.cb = ho_T3103_cb;
Harald Weltee47f96b2009-12-18 11:49:03 +0100173 ho->T3103.data = ho;
Harald Welte798418a2009-11-29 22:56:14 +0100174 bsc_schedule_timer(&ho->T3103, 10, 0);
175
176 return 0;
177}
178
179/* RSL has not acknowledged activation of the new lchan */
180static int ho_chan_activ_nack(struct gsm_lchan *new_lchan)
181{
182 struct bsc_handover *ho;
183
184 ho = bsc_ho_by_new_lchan(new_lchan);
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100185 if (!ho) {
186 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
Harald Welte798418a2009-11-29 22:56:14 +0100187 return -ENODEV;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100188 }
Harald Welte798418a2009-11-29 22:56:14 +0100189
190 llist_del(&ho->list);
191 talloc_free(ho);
192
193 /* FIXME: maybe we should try to allocate a new LCHAN here? */
194
195 return 0;
196}
197
198/* GSM 04.08 HANDOVER COMPLETE has been received on new channel */
199static int ho_gsm48_ho_compl(struct gsm_lchan *new_lchan)
200{
201 struct bsc_handover *ho;
202
203 ho = bsc_ho_by_new_lchan(new_lchan);
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100204 if (!ho) {
205 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
Harald Welte798418a2009-11-29 22:56:14 +0100206 return -ENODEV;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100207 }
Harald Welte798418a2009-11-29 22:56:14 +0100208
209 bsc_del_timer(&ho->T3103);
210 llist_del(&ho->list);
211
Harald Weltefe18d5c2009-12-17 17:14:43 +0100212 /* update lchan pointer of transaction */
213 trans_lchan_change(ho->old_lchan, new_lchan);
214
Harald Welte798418a2009-11-29 22:56:14 +0100215 /* do something to re-route the actual speech frames ! */
216 //tch_remap(ho->old_lchan, ho->new_lchan);
217
218 /* release old lchan */
219 put_lchan(ho->old_lchan);
220
221 talloc_free(ho);
222
223 return 0;
224}
225
226/* GSM 04.08 HANDOVER FAIL has been received */
227static int ho_gsm48_ho_fail(struct gsm_lchan *old_lchan)
228{
229 struct bsc_handover *ho;
230
231 ho = bsc_ho_by_old_lchan(old_lchan);
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100232 if (!ho) {
233 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
Harald Welte798418a2009-11-29 22:56:14 +0100234 return -ENODEV;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100235 }
Harald Welte798418a2009-11-29 22:56:14 +0100236
237 bsc_del_timer(&ho->T3103);
238 llist_del(&ho->list);
239 put_lchan(ho->new_lchan);
240 talloc_free(ho);
241
242 return 0;
243}
244
245/* GSM 08.58 HANDOVER DETECT has been received */
246static int ho_rsl_detect(struct gsm_lchan *new_lchan)
247{
248 struct bsc_handover *ho;
249
Harald Welte6f7a5a72009-12-18 11:52:03 +0100250 ho = bsc_ho_by_new_lchan(new_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
256 /* FIXME: do we actually want to do something here ? */
257
258 return 0;
259}
260
261static int ho_logic_sig_cb(unsigned int subsys, unsigned int signal,
262 void *handler_data, void *signal_data)
263{
264 struct gsm_lchan *lchan;
265
266 switch (subsys) {
267 case SS_LCHAN:
268 lchan = signal_data;
269 switch (signal) {
270 case S_LCHAN_ACTIVATE_ACK:
271 return ho_chan_activ_ack(lchan);
272 case S_LCHAN_ACTIVATE_NACK:
273 return ho_chan_activ_nack(lchan);
274 case S_LCHAN_HANDOVER_DETECT:
275 return ho_rsl_detect(lchan);
276 case S_LCHAN_HANDOVER_COMPL:
277 return ho_gsm48_ho_compl(lchan);
278 case S_LCHAN_HANDOVER_FAIL:
279 return ho_gsm48_ho_fail(lchan);
280 }
281 break;
282 default:
283 break;
284 }
285
286 return 0;
287}
288
289static __attribute__((constructor)) void on_dso_load_ho_logic(void)
290{
291 register_signal_handler(SS_LCHAN, ho_logic_sig_cb, NULL);
292}