Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 1 | /* 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 Welte | a037902 | 2009-12-20 17:04:40 +0100 | [diff] [blame] | 43 | #include <openbsc/rtp_proxy.h> |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 44 | |
| 45 | struct 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 | |
| 56 | static LLIST_HEAD(bsc_handovers); |
| 57 | |
| 58 | static 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 | |
| 70 | static 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 */ |
| 85 | int 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 Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 89 | static u_int8_t ho_ref; |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 90 | int rc; |
| 91 | |
Harald Welte | 6670681 | 2009-12-17 22:23:21 +0100 | [diff] [blame] | 92 | /* 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 Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 97 | DEBUGP(DHO, "(old_lchan on BTS %u, new BTS %u)\n", |
Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 98 | old_lchan->ts->trx->bts->nr, bts->nr); |
| 99 | |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 100 | new_lchan = lchan_alloc(bts, old_lchan->type); |
Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 101 | if (!new_lchan) { |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 102 | LOGP(DHO, LOGL_NOTICE, "No free channel\n"); |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 103 | return -ENOSPC; |
Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 104 | } |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 105 | |
| 106 | ho = talloc_zero(NULL, struct bsc_handover); |
| 107 | if (!ho) { |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 108 | LOGP(DHO, LOGL_FATAL, "Out of Memory\n"); |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 109 | lchan_free(new_lchan); |
| 110 | return -ENOMEM; |
| 111 | } |
| 112 | ho->old_lchan = old_lchan; |
| 113 | ho->new_lchan = new_lchan; |
Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 114 | ho->ho_ref = ho_ref++; |
| 115 | |
| 116 | /* copy some parameters from old lchan */ |
| 117 | memcpy(&new_lchan->encr, &old_lchan->encr, sizeof(new_lchan->encr)); |
| 118 | new_lchan->ms_power = old_lchan->ms_power; |
| 119 | new_lchan->bs_power = old_lchan->bs_power; |
| 120 | new_lchan->rsl_cmode = old_lchan->rsl_cmode; |
| 121 | new_lchan->tch_mode = old_lchan->tch_mode; |
Harald Welte | a9fa8dc | 2009-12-18 14:50:08 +0100 | [diff] [blame] | 122 | new_lchan->subscr = subscr_get(old_lchan->subscr); |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 123 | |
| 124 | /* FIXME: do we have a better idea of the timing advance? */ |
Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 125 | rc = rsl_chan_activate_lchan(new_lchan, RSL_ACT_INTER_ASYNC, 0, |
| 126 | ho->ho_ref); |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 127 | if (rc < 0) { |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 128 | LOGP(DHO, LOGL_ERROR, "could not activate channel\n"); |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 129 | talloc_free(ho); |
| 130 | lchan_free(new_lchan); |
| 131 | return rc; |
| 132 | } |
| 133 | |
| 134 | llist_add(&ho->list, &bsc_handovers); |
| 135 | /* we continue in the SS_LCHAN handler / ho_chan_activ_ack */ |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | /* T3103 expired: Handover has failed without HO COMPLETE or HO FAIL */ |
| 141 | static void ho_T3103_cb(void *_ho) |
| 142 | { |
| 143 | struct bsc_handover *ho = _ho; |
| 144 | |
Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 145 | DEBUGP(DHO, "HO T3103 expired\n"); |
| 146 | |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 147 | lchan_free(ho->new_lchan); |
| 148 | llist_del(&ho->list); |
| 149 | talloc_free(ho); |
| 150 | } |
| 151 | |
| 152 | /* RSL has acknowledged activation of the new lchan */ |
| 153 | static int ho_chan_activ_ack(struct gsm_lchan *new_lchan) |
| 154 | { |
| 155 | struct bsc_handover *ho; |
| 156 | int rc; |
| 157 | |
Harald Welte | a9fa8dc | 2009-12-18 14:50:08 +0100 | [diff] [blame] | 158 | /* we need to check if this channel activation is related to |
| 159 | * a handover at all (and if, which particular handover) */ |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 160 | ho = bsc_ho_by_new_lchan(new_lchan); |
Harald Welte | a9fa8dc | 2009-12-18 14:50:08 +0100 | [diff] [blame] | 161 | if (!ho) |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 162 | return -ENODEV; |
Harald Welte | a9fa8dc | 2009-12-18 14:50:08 +0100 | [diff] [blame] | 163 | |
| 164 | DEBUGP(DHO, "handover activate ack, send HO Command\n"); |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 165 | |
| 166 | /* we can now send the 04.08 HANDOVER COMMAND to the MS |
| 167 | * using the old lchan */ |
| 168 | |
Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 169 | rc = gsm48_send_ho_cmd(ho->old_lchan, new_lchan, 0, ho->ho_ref); |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 170 | |
| 171 | /* start T3103. We can continue either with T3103 expiration, |
| 172 | * 04.08 HANDOVER COMPLETE or 04.08 HANDOVER FAIL */ |
| 173 | ho->T3103.cb = ho_T3103_cb; |
Harald Welte | e47f96b | 2009-12-18 11:49:03 +0100 | [diff] [blame] | 174 | ho->T3103.data = ho; |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 175 | bsc_schedule_timer(&ho->T3103, 10, 0); |
| 176 | |
Harald Welte | a037902 | 2009-12-20 17:04:40 +0100 | [diff] [blame] | 177 | /* create a RTP connection */ |
| 178 | if (is_ipaccess_bts(new_lchan->ts->trx->bts)) |
| 179 | rsl_ipacc_crcx(new_lchan); |
| 180 | |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | /* RSL has not acknowledged activation of the new lchan */ |
| 185 | static int ho_chan_activ_nack(struct gsm_lchan *new_lchan) |
| 186 | { |
| 187 | struct bsc_handover *ho; |
| 188 | |
| 189 | ho = bsc_ho_by_new_lchan(new_lchan); |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 190 | if (!ho) { |
| 191 | LOGP(DHO, LOGL_ERROR, "unable to find HO record\n"); |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 192 | return -ENODEV; |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 193 | } |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 194 | |
| 195 | llist_del(&ho->list); |
| 196 | talloc_free(ho); |
| 197 | |
| 198 | /* FIXME: maybe we should try to allocate a new LCHAN here? */ |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | /* GSM 04.08 HANDOVER COMPLETE has been received on new channel */ |
| 204 | static int ho_gsm48_ho_compl(struct gsm_lchan *new_lchan) |
| 205 | { |
| 206 | struct bsc_handover *ho; |
| 207 | |
| 208 | ho = bsc_ho_by_new_lchan(new_lchan); |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 209 | if (!ho) { |
| 210 | LOGP(DHO, LOGL_ERROR, "unable to find HO record\n"); |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 211 | return -ENODEV; |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 212 | } |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 213 | |
| 214 | bsc_del_timer(&ho->T3103); |
| 215 | llist_del(&ho->list); |
| 216 | |
Harald Welte | fe18d5c | 2009-12-17 17:14:43 +0100 | [diff] [blame] | 217 | /* update lchan pointer of transaction */ |
| 218 | trans_lchan_change(ho->old_lchan, new_lchan); |
| 219 | |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 220 | /* do something to re-route the actual speech frames ! */ |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 221 | |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 222 | talloc_free(ho); |
| 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | /* GSM 04.08 HANDOVER FAIL has been received */ |
| 228 | static int ho_gsm48_ho_fail(struct gsm_lchan *old_lchan) |
| 229 | { |
| 230 | struct bsc_handover *ho; |
| 231 | |
| 232 | ho = bsc_ho_by_old_lchan(old_lchan); |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 233 | if (!ho) { |
| 234 | LOGP(DHO, LOGL_ERROR, "unable to find HO record\n"); |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 235 | return -ENODEV; |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 236 | } |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 237 | |
| 238 | bsc_del_timer(&ho->T3103); |
| 239 | llist_del(&ho->list); |
| 240 | put_lchan(ho->new_lchan); |
| 241 | talloc_free(ho); |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | /* GSM 08.58 HANDOVER DETECT has been received */ |
| 247 | static int ho_rsl_detect(struct gsm_lchan *new_lchan) |
| 248 | { |
| 249 | struct bsc_handover *ho; |
| 250 | |
Harald Welte | 6f7a5a7 | 2009-12-18 11:52:03 +0100 | [diff] [blame] | 251 | ho = bsc_ho_by_new_lchan(new_lchan); |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 252 | if (!ho) { |
| 253 | LOGP(DHO, LOGL_ERROR, "unable to find HO record\n"); |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 254 | return -ENODEV; |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 255 | } |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 256 | |
| 257 | /* FIXME: do we actually want to do something here ? */ |
| 258 | |
| 259 | return 0; |
| 260 | } |
| 261 | |
Harald Welte | a037902 | 2009-12-20 17:04:40 +0100 | [diff] [blame] | 262 | static int ho_ipac_crcx_ack(struct gsm_lchan *new_lchan) |
| 263 | { |
| 264 | struct bsc_handover *ho; |
| 265 | struct rtp_socket *old_rs, *new_rs, *other_rs; |
| 266 | |
| 267 | ho = bsc_ho_by_new_lchan(new_lchan); |
| 268 | if (!ho) { |
| 269 | LOGP(DHO, LOGL_ERROR, "unable to find HO record\n"); |
| 270 | return -ENODEV; |
| 271 | } |
| 272 | |
| 273 | if (ipacc_rtp_direct) { |
| 274 | LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n"); |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | /* RTP Proxy mode */ |
| 279 | new_rs = new_lchan->abis_ip.rtp_socket; |
| 280 | old_rs = ho->old_lchan->abis_ip.rtp_socket; |
| 281 | |
| 282 | if (!new_rs) { |
| 283 | LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n"); |
| 284 | return -EIO; |
| 285 | } |
| 286 | |
| 287 | rsl_ipacc_mdcx_to_rtpsock(new_lchan); |
| 288 | |
| 289 | if (!old_rs) { |
| 290 | LOGP(DHO, LOGL_ERROR, "no RTP socekt for old_lchan\n"); |
| 291 | return -EIO; |
| 292 | } |
| 293 | |
| 294 | /* copy rx_action and reference to other sock */ |
| 295 | new_rs->rx_action = old_rs->rx_action; |
| 296 | new_rs->tx_action = old_rs->tx_action; |
| 297 | new_rs->transmit = old_rs->transmit; |
| 298 | |
| 299 | switch (ho->old_lchan->abis_ip.rtp_socket->rx_action) { |
| 300 | case RTP_PROXY: |
| 301 | other_rs = old_rs->proxy.other_sock; |
| 302 | rtp_socket_proxy(new_rs, other_rs); |
| 303 | /* delete reference to other end socket to prevent |
| 304 | * rtp_socket_free() from removing the inverse reference */ |
| 305 | old_rs->proxy.other_sock = NULL; |
| 306 | break; |
| 307 | case RTP_RECV_UPSTREAM: |
| 308 | new_rs->receive = old_rs->receive; |
| 309 | break; |
| 310 | case RTP_NONE: |
| 311 | break; |
| 312 | } |
| 313 | |
| 314 | return 0; |
| 315 | } |
| 316 | |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 317 | static int ho_logic_sig_cb(unsigned int subsys, unsigned int signal, |
| 318 | void *handler_data, void *signal_data) |
| 319 | { |
| 320 | struct gsm_lchan *lchan; |
| 321 | |
| 322 | switch (subsys) { |
| 323 | case SS_LCHAN: |
| 324 | lchan = signal_data; |
| 325 | switch (signal) { |
| 326 | case S_LCHAN_ACTIVATE_ACK: |
| 327 | return ho_chan_activ_ack(lchan); |
| 328 | case S_LCHAN_ACTIVATE_NACK: |
| 329 | return ho_chan_activ_nack(lchan); |
| 330 | case S_LCHAN_HANDOVER_DETECT: |
| 331 | return ho_rsl_detect(lchan); |
| 332 | case S_LCHAN_HANDOVER_COMPL: |
| 333 | return ho_gsm48_ho_compl(lchan); |
| 334 | case S_LCHAN_HANDOVER_FAIL: |
| 335 | return ho_gsm48_ho_fail(lchan); |
| 336 | } |
| 337 | break; |
Harald Welte | a037902 | 2009-12-20 17:04:40 +0100 | [diff] [blame] | 338 | case SS_ABISIP: |
| 339 | lchan = signal_data; |
| 340 | switch (signal) { |
| 341 | case S_ABISIP_CRCX_ACK: |
| 342 | return ho_ipac_crcx_ack(lchan); |
| 343 | break; |
| 344 | } |
| 345 | break; |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 346 | default: |
| 347 | break; |
| 348 | } |
| 349 | |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | static __attribute__((constructor)) void on_dso_load_ho_logic(void) |
| 354 | { |
| 355 | register_signal_handler(SS_LCHAN, ho_logic_sig_cb, NULL); |
Harald Welte | a037902 | 2009-12-20 17:04:40 +0100 | [diff] [blame] | 356 | register_signal_handler(SS_ABISIP, ho_logic_sig_cb, NULL); |
Harald Welte | 798418a | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 357 | } |