Harald Welte | 96c0b08 | 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> |
| 43 | |
| 44 | struct 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 | |
| 55 | static LLIST_HEAD(bsc_handovers); |
| 56 | |
| 57 | static 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 | |
| 69 | static 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 */ |
| 84 | int 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 Welte | b90d7bd | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 88 | static u_int8_t ho_ref; |
Harald Welte | 96c0b08 | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 89 | int rc; |
| 90 | |
Harald Welte | aef376f | 2009-12-17 22:23:21 +0100 | [diff] [blame] | 91 | /* 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 Welte | cf2ec4a | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 96 | DEBUGP(DHO, "(old_lchan on BTS %u, new BTS %u)\n", |
Harald Welte | b90d7bd | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 97 | old_lchan->ts->trx->bts->nr, bts->nr); |
| 98 | |
Harald Welte | 96c0b08 | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 99 | new_lchan = lchan_alloc(bts, old_lchan->type); |
Harald Welte | b90d7bd | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 100 | if (!new_lchan) { |
Harald Welte | cf2ec4a | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 101 | LOGP(DHO, LOGL_NOTICE, "No free channel\n"); |
Harald Welte | 96c0b08 | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 102 | return -ENOSPC; |
Harald Welte | b90d7bd | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 103 | } |
Harald Welte | 96c0b08 | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 104 | |
| 105 | ho = talloc_zero(NULL, struct bsc_handover); |
| 106 | if (!ho) { |
Harald Welte | cf2ec4a | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 107 | LOGP(DHO, LOGL_FATAL, "Out of Memory\n"); |
Harald Welte | 96c0b08 | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 108 | lchan_free(new_lchan); |
| 109 | return -ENOMEM; |
| 110 | } |
| 111 | ho->old_lchan = old_lchan; |
| 112 | ho->new_lchan = new_lchan; |
Harald Welte | b90d7bd | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 113 | 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 Welte | 96c0b08 | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 121 | |
| 122 | /* FIXME: do we have a better idea of the timing advance? */ |
Harald Welte | b90d7bd | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 123 | rc = rsl_chan_activate_lchan(new_lchan, RSL_ACT_INTER_ASYNC, 0, |
| 124 | ho->ho_ref); |
Harald Welte | 96c0b08 | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 125 | if (rc < 0) { |
Harald Welte | cf2ec4a | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 126 | LOGP(DHO, LOGL_ERROR, "could not activate channel\n"); |
Harald Welte | 96c0b08 | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 127 | talloc_free(ho); |
| 128 | lchan_free(new_lchan); |
| 129 | return rc; |
| 130 | } |
| 131 | |
| 132 | llist_add(&ho->list, &bsc_handovers); |
| 133 | /* we continue in the SS_LCHAN handler / ho_chan_activ_ack */ |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | /* T3103 expired: Handover has failed without HO COMPLETE or HO FAIL */ |
| 139 | static void ho_T3103_cb(void *_ho) |
| 140 | { |
| 141 | struct bsc_handover *ho = _ho; |
| 142 | |
Harald Welte | b90d7bd | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 143 | DEBUGP(DHO, "HO T3103 expired\n"); |
| 144 | |
Harald Welte | 96c0b08 | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 145 | lchan_free(ho->new_lchan); |
| 146 | llist_del(&ho->list); |
| 147 | talloc_free(ho); |
| 148 | } |
| 149 | |
| 150 | /* RSL has acknowledged activation of the new lchan */ |
| 151 | static int ho_chan_activ_ack(struct gsm_lchan *new_lchan) |
| 152 | { |
| 153 | struct bsc_handover *ho; |
| 154 | int rc; |
| 155 | |
Harald Welte | b90d7bd | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 156 | DEBUGP(DHO, "handover activate ack, send HO Command\n"); |
| 157 | |
Harald Welte | 96c0b08 | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 158 | ho = bsc_ho_by_new_lchan(new_lchan); |
Harald Welte | cf2ec4a | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 159 | if (!ho) { |
| 160 | LOGP(DHO, LOGL_ERROR, "unable to find HO record\n"); |
Harald Welte | 96c0b08 | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 161 | return -ENODEV; |
Harald Welte | cf2ec4a | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 162 | } |
Harald Welte | 96c0b08 | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 163 | |
| 164 | /* we can now send the 04.08 HANDOVER COMMAND to the MS |
| 165 | * using the old lchan */ |
| 166 | |
Harald Welte | b90d7bd | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 167 | rc = gsm48_send_ho_cmd(ho->old_lchan, new_lchan, 0, ho->ho_ref); |
Harald Welte | 96c0b08 | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 168 | |
| 169 | /* start T3103. We can continue either with T3103 expiration, |
| 170 | * 04.08 HANDOVER COMPLETE or 04.08 HANDOVER FAIL */ |
| 171 | ho->T3103.cb = ho_T3103_cb; |
| 172 | bsc_schedule_timer(&ho->T3103, 10, 0); |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | /* RSL has not acknowledged activation of the new lchan */ |
| 178 | static int ho_chan_activ_nack(struct gsm_lchan *new_lchan) |
| 179 | { |
| 180 | struct bsc_handover *ho; |
| 181 | |
| 182 | ho = bsc_ho_by_new_lchan(new_lchan); |
Harald Welte | cf2ec4a | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 183 | if (!ho) { |
| 184 | LOGP(DHO, LOGL_ERROR, "unable to find HO record\n"); |
Harald Welte | 96c0b08 | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 185 | return -ENODEV; |
Harald Welte | cf2ec4a | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 186 | } |
Harald Welte | 96c0b08 | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 187 | |
| 188 | llist_del(&ho->list); |
| 189 | talloc_free(ho); |
| 190 | |
| 191 | /* FIXME: maybe we should try to allocate a new LCHAN here? */ |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | /* GSM 04.08 HANDOVER COMPLETE has been received on new channel */ |
| 197 | static int ho_gsm48_ho_compl(struct gsm_lchan *new_lchan) |
| 198 | { |
| 199 | struct bsc_handover *ho; |
| 200 | |
| 201 | ho = bsc_ho_by_new_lchan(new_lchan); |
Harald Welte | cf2ec4a | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 202 | if (!ho) { |
| 203 | LOGP(DHO, LOGL_ERROR, "unable to find HO record\n"); |
Harald Welte | 96c0b08 | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 204 | return -ENODEV; |
Harald Welte | cf2ec4a | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 205 | } |
Harald Welte | 96c0b08 | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 206 | |
| 207 | bsc_del_timer(&ho->T3103); |
| 208 | llist_del(&ho->list); |
| 209 | |
Harald Welte | f8c5070 | 2009-12-17 17:14:43 +0100 | [diff] [blame] | 210 | /* update lchan pointer of transaction */ |
| 211 | trans_lchan_change(ho->old_lchan, new_lchan); |
| 212 | |
Harald Welte | 96c0b08 | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 213 | /* do something to re-route the actual speech frames ! */ |
| 214 | //tch_remap(ho->old_lchan, ho->new_lchan); |
| 215 | |
| 216 | /* release old lchan */ |
| 217 | put_lchan(ho->old_lchan); |
| 218 | |
| 219 | talloc_free(ho); |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | /* GSM 04.08 HANDOVER FAIL has been received */ |
| 225 | static int ho_gsm48_ho_fail(struct gsm_lchan *old_lchan) |
| 226 | { |
| 227 | struct bsc_handover *ho; |
| 228 | |
| 229 | ho = bsc_ho_by_old_lchan(old_lchan); |
Harald Welte | cf2ec4a | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 230 | if (!ho) { |
| 231 | LOGP(DHO, LOGL_ERROR, "unable to find HO record\n"); |
Harald Welte | 96c0b08 | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 232 | return -ENODEV; |
Harald Welte | cf2ec4a | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 233 | } |
Harald Welte | 96c0b08 | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 234 | |
| 235 | bsc_del_timer(&ho->T3103); |
| 236 | llist_del(&ho->list); |
| 237 | put_lchan(ho->new_lchan); |
| 238 | talloc_free(ho); |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | /* GSM 08.58 HANDOVER DETECT has been received */ |
| 244 | static int ho_rsl_detect(struct gsm_lchan *new_lchan) |
| 245 | { |
| 246 | struct bsc_handover *ho; |
| 247 | |
| 248 | ho = bsc_ho_by_old_lchan(new_lchan); |
Harald Welte | cf2ec4a | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 249 | if (!ho) { |
| 250 | LOGP(DHO, LOGL_ERROR, "unable to find HO record\n"); |
Harald Welte | 96c0b08 | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 251 | return -ENODEV; |
Harald Welte | cf2ec4a | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 252 | } |
Harald Welte | 96c0b08 | 2009-11-29 22:56:14 +0100 | [diff] [blame] | 253 | |
| 254 | /* FIXME: do we actually want to do something here ? */ |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | static int ho_logic_sig_cb(unsigned int subsys, unsigned int signal, |
| 260 | void *handler_data, void *signal_data) |
| 261 | { |
| 262 | struct gsm_lchan *lchan; |
| 263 | |
| 264 | switch (subsys) { |
| 265 | case SS_LCHAN: |
| 266 | lchan = signal_data; |
| 267 | switch (signal) { |
| 268 | case S_LCHAN_ACTIVATE_ACK: |
| 269 | return ho_chan_activ_ack(lchan); |
| 270 | case S_LCHAN_ACTIVATE_NACK: |
| 271 | return ho_chan_activ_nack(lchan); |
| 272 | case S_LCHAN_HANDOVER_DETECT: |
| 273 | return ho_rsl_detect(lchan); |
| 274 | case S_LCHAN_HANDOVER_COMPL: |
| 275 | return ho_gsm48_ho_compl(lchan); |
| 276 | case S_LCHAN_HANDOVER_FAIL: |
| 277 | return ho_gsm48_ho_fail(lchan); |
| 278 | } |
| 279 | break; |
| 280 | default: |
| 281 | break; |
| 282 | } |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | static __attribute__((constructor)) void on_dso_load_ho_logic(void) |
| 288 | { |
| 289 | register_signal_handler(SS_LCHAN, ho_logic_sig_cb, NULL); |
| 290 | } |