Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1 | /* Paging helper and manager.... */ |
Holger Hans Peter Freyther | 81598ea | 2013-01-07 17:30:13 +0100 | [diff] [blame] | 2 | /* (C) 2009,2013 by Holger Hans Peter Freyther <zecke@selfish.org> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 3 | * All Rights Reserved |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 6 | * it under the terms of the GNU Affero General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 13 | * GNU Affero General Public License for more details. |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 14 | * |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 15 | * You should have received a copy of the GNU Affero General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 17 | * |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * Relevant specs: |
| 22 | * 12.21: |
| 23 | * - 9.4.12 for CCCH Local Threshold |
| 24 | * |
| 25 | * 05.58: |
| 26 | * - 8.5.2 CCCH Load indication |
| 27 | * - 9.3.15 Paging Load |
| 28 | * |
| 29 | * Approach: |
| 30 | * - Send paging command to subscriber |
| 31 | * - On Channel Request we will remember the reason |
| 32 | * - After the ACK we will request the identity |
| 33 | * - Then we will send assign the gsm_subscriber and |
| 34 | * - and call a callback |
| 35 | */ |
| 36 | |
| 37 | #include <stdio.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <assert.h> |
| 40 | |
Pablo Neira Ayuso | dd5fff4 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 41 | #include <osmocom/core/talloc.h> |
Harald Welte | f38b31c | 2011-06-26 14:21:43 +0200 | [diff] [blame] | 42 | #include <osmocom/gsm/gsm48.h> |
Harald Welte | ee3ce52 | 2011-06-26 14:41:58 +0200 | [diff] [blame] | 43 | #include <osmocom/gsm/gsm0502.h> |
Harald Welte | f38b31c | 2011-06-26 14:21:43 +0200 | [diff] [blame] | 44 | |
| 45 | #include <openbsc/paging.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 46 | #include <openbsc/debug.h> |
| 47 | #include <openbsc/signal.h> |
| 48 | #include <openbsc/abis_rsl.h> |
| 49 | #include <openbsc/gsm_data.h> |
Holger Hans Peter Freyther | 8ecec25 | 2010-04-10 00:06:17 +0200 | [diff] [blame] | 50 | #include <openbsc/chan_alloc.h> |
Holger Hans Peter Freyther | 710157a | 2010-06-17 16:43:24 +0800 | [diff] [blame] | 51 | #include <openbsc/bsc_api.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 52 | |
Harald Welte (local) | 8751ee9 | 2009-08-15 02:30:58 +0200 | [diff] [blame] | 53 | void *tall_paging_ctx; |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 54 | |
Holger Hans Peter Freyther | c814ae9 | 2010-09-06 10:05:23 +0800 | [diff] [blame] | 55 | #define PAGING_TIMER 0, 500000 |
| 56 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 57 | /* |
| 58 | * Kill one paging request update the internal list... |
| 59 | */ |
| 60 | static void paging_remove_request(struct gsm_bts_paging_state *paging_bts, |
| 61 | struct gsm_paging_request *to_be_deleted) |
| 62 | { |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 63 | osmo_timer_del(&to_be_deleted->T3113); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 64 | llist_del(&to_be_deleted->entry); |
| 65 | subscr_put(to_be_deleted->subscr); |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 66 | talloc_free(to_be_deleted); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static void page_ms(struct gsm_paging_request *request) |
| 70 | { |
Holger Hans Peter Freyther | 7eb8a9a | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 71 | uint8_t mi[128]; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 72 | unsigned int mi_len; |
| 73 | unsigned int page_group; |
Harald Welte | 1c23b8d | 2011-06-26 14:43:46 +0200 | [diff] [blame] | 74 | struct gsm_bts *bts = request->bts; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 75 | |
Holger Hans Peter Freyther | 308a441 | 2012-12-22 19:27:27 +0100 | [diff] [blame] | 76 | /* the bts is down.. we will just wait for the paging to expire */ |
| 77 | if (!bts->oml_link) |
| 78 | return; |
| 79 | |
Holger Hans Peter Freyther | 66f55e6 | 2014-12-28 15:00:36 +0100 | [diff] [blame] | 80 | log_set_context(BSC_CTX_SUBSCR, request->subscr); |
| 81 | |
Holger Hans Peter Freyther | b6a3394 | 2010-04-09 22:02:39 +0200 | [diff] [blame] | 82 | LOGP(DPAG, LOGL_INFO, "Going to send paging commands: imsi: '%s' tmsi: '0x%x'\n", |
Holger Hans Peter Freyther | 9b7911e | 2009-08-21 14:55:57 +0200 | [diff] [blame] | 83 | request->subscr->imsi, request->subscr->tmsi); |
| 84 | |
| 85 | if (request->subscr->tmsi == GSM_RESERVED_TMSI) |
| 86 | mi_len = gsm48_generate_mid_from_imsi(mi, request->subscr->imsi); |
| 87 | else |
| 88 | mi_len = gsm48_generate_mid_from_tmsi(mi, request->subscr->tmsi); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 89 | |
Harald Welte | 1c23b8d | 2011-06-26 14:43:46 +0200 | [diff] [blame] | 90 | page_group = gsm0502_calc_paging_group(&bts->si_common.chan_desc, |
| 91 | str_to_imsi(request->subscr->imsi)); |
| 92 | gsm0808_page(bts, page_group, mi_len, mi, request->chan_type); |
Holger Hans Peter Freyther | 66f55e6 | 2014-12-28 15:00:36 +0100 | [diff] [blame] | 93 | log_set_context(BSC_CTX_SUBSCR, NULL); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 94 | } |
| 95 | |
Holger Hans Peter Freyther | c814ae9 | 2010-09-06 10:05:23 +0800 | [diff] [blame] | 96 | static void paging_schedule_if_needed(struct gsm_bts_paging_state *paging_bts) |
| 97 | { |
| 98 | if (llist_empty(&paging_bts->pending_requests)) |
| 99 | return; |
| 100 | |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 101 | if (!osmo_timer_pending(&paging_bts->work_timer)) |
| 102 | osmo_timer_schedule(&paging_bts->work_timer, PAGING_TIMER); |
Holger Hans Peter Freyther | c814ae9 | 2010-09-06 10:05:23 +0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | |
Holger Hans Peter Freyther | e791d95 | 2010-04-20 10:59:33 +0800 | [diff] [blame] | 106 | static void paging_handle_pending_requests(struct gsm_bts_paging_state *paging_bts); |
Holger Hans Peter Freyther | e7be13c | 2010-04-19 19:41:26 +0800 | [diff] [blame] | 107 | static void paging_give_credit(void *data) |
| 108 | { |
| 109 | struct gsm_bts_paging_state *paging_bts = data; |
| 110 | |
| 111 | LOGP(DPAG, LOGL_NOTICE, "No slots available on bts nr %d\n", paging_bts->bts->nr); |
| 112 | paging_bts->available_slots = 20; |
Holger Hans Peter Freyther | e791d95 | 2010-04-20 10:59:33 +0800 | [diff] [blame] | 113 | paging_handle_pending_requests(paging_bts); |
Holger Hans Peter Freyther | e7be13c | 2010-04-19 19:41:26 +0800 | [diff] [blame] | 114 | } |
| 115 | |
Holger Hans Peter Freyther | 3112f67 | 2010-09-06 10:11:25 +0800 | [diff] [blame] | 116 | static int can_send_pag_req(struct gsm_bts *bts, int rsl_type) |
| 117 | { |
| 118 | struct pchan_load pl; |
| 119 | int count; |
| 120 | |
| 121 | memset(&pl, 0, sizeof(pl)); |
Alexander Couzens | 92866f0 | 2016-08-29 18:45:42 +0200 | [diff] [blame^] | 122 | bts_chan_load(&pl, bts, 0); |
Holger Hans Peter Freyther | 3112f67 | 2010-09-06 10:11:25 +0800 | [diff] [blame] | 123 | |
| 124 | switch (rsl_type) { |
| 125 | case RSL_CHANNEED_TCH_F: |
Holger Hans Peter Freyther | 3112f67 | 2010-09-06 10:11:25 +0800 | [diff] [blame] | 126 | case RSL_CHANNEED_TCH_ForH: |
Holger Hans Peter Freyther | 343b945 | 2010-04-20 17:15:21 +0800 | [diff] [blame] | 127 | goto count_tch; |
Holger Hans Peter Freyther | 3112f67 | 2010-09-06 10:11:25 +0800 | [diff] [blame] | 128 | break; |
| 129 | case RSL_CHANNEED_SDCCH: |
| 130 | goto count_sdcch; |
| 131 | break; |
| 132 | case RSL_CHANNEED_ANY: |
| 133 | default: |
| 134 | if (bts->network->pag_any_tch) |
Holger Hans Peter Freyther | 343b945 | 2010-04-20 17:15:21 +0800 | [diff] [blame] | 135 | goto count_tch; |
Holger Hans Peter Freyther | 3112f67 | 2010-09-06 10:11:25 +0800 | [diff] [blame] | 136 | else |
| 137 | goto count_sdcch; |
| 138 | break; |
| 139 | } |
| 140 | |
| 141 | return 0; |
| 142 | |
| 143 | /* could available SDCCH */ |
| 144 | count_sdcch: |
| 145 | count = 0; |
| 146 | count += pl.pchan[GSM_PCHAN_SDCCH8_SACCH8C].total |
| 147 | - pl.pchan[GSM_PCHAN_SDCCH8_SACCH8C].used; |
| 148 | count += pl.pchan[GSM_PCHAN_CCCH_SDCCH4].total |
| 149 | - pl.pchan[GSM_PCHAN_CCCH_SDCCH4].used; |
| 150 | return bts->paging.free_chans_need > count; |
Holger Hans Peter Freyther | 343b945 | 2010-04-20 17:15:21 +0800 | [diff] [blame] | 151 | |
| 152 | count_tch: |
| 153 | count = 0; |
| 154 | count += pl.pchan[GSM_PCHAN_TCH_F].total |
| 155 | - pl.pchan[GSM_PCHAN_TCH_F].used; |
| 156 | if (bts->network->neci) |
| 157 | count += pl.pchan[GSM_PCHAN_TCH_H].total |
| 158 | - pl.pchan[GSM_PCHAN_TCH_H].used; |
| 159 | return bts->paging.free_chans_need > count; |
Holger Hans Peter Freyther | 3112f67 | 2010-09-06 10:11:25 +0800 | [diff] [blame] | 160 | } |
| 161 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 162 | /* |
| 163 | * This is kicked by the periodic PAGING LOAD Indicator |
| 164 | * coming from abis_rsl.c |
| 165 | * |
| 166 | * We attempt to iterate once over the list of items but |
| 167 | * only upto available_slots. |
| 168 | */ |
| 169 | static void paging_handle_pending_requests(struct gsm_bts_paging_state *paging_bts) |
| 170 | { |
Holger Hans Peter Freyther | c814ae9 | 2010-09-06 10:05:23 +0800 | [diff] [blame] | 171 | struct gsm_paging_request *request = NULL; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 172 | |
| 173 | /* |
| 174 | * Determine if the pending_requests list is empty and |
| 175 | * return then. |
| 176 | */ |
| 177 | if (llist_empty(&paging_bts->pending_requests)) { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 178 | /* since the list is empty, no need to reschedule the timer */ |
| 179 | return; |
| 180 | } |
| 181 | |
Holger Hans Peter Freyther | 5fd943c | 2010-04-09 18:38:06 +0200 | [diff] [blame] | 182 | /* |
Holger Hans Peter Freyther | e7be13c | 2010-04-19 19:41:26 +0800 | [diff] [blame] | 183 | * In case the BTS does not provide us with load indication and we |
| 184 | * ran out of slots, call an autofill routine. It might be that the |
| 185 | * BTS did not like our paging messages and then we have counted down |
| 186 | * to zero and we do not get any messages. |
Holger Hans Peter Freyther | 5fd943c | 2010-04-09 18:38:06 +0200 | [diff] [blame] | 187 | */ |
| 188 | if (paging_bts->available_slots == 0) { |
Holger Hans Peter Freyther | e7be13c | 2010-04-19 19:41:26 +0800 | [diff] [blame] | 189 | paging_bts->credit_timer.cb = paging_give_credit; |
| 190 | paging_bts->credit_timer.data = paging_bts; |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 191 | osmo_timer_schedule(&paging_bts->credit_timer, 5, 0); |
Holger Hans Peter Freyther | e7be13c | 2010-04-19 19:41:26 +0800 | [diff] [blame] | 192 | return; |
Holger Hans Peter Freyther | 5fd943c | 2010-04-09 18:38:06 +0200 | [diff] [blame] | 193 | } |
| 194 | |
Holger Hans Peter Freyther | c814ae9 | 2010-09-06 10:05:23 +0800 | [diff] [blame] | 195 | request = llist_entry(paging_bts->pending_requests.next, |
| 196 | struct gsm_paging_request, entry); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 197 | |
Holger Hans Peter Freyther | 3112f67 | 2010-09-06 10:11:25 +0800 | [diff] [blame] | 198 | /* we need to determine the number of free channels */ |
| 199 | if (paging_bts->free_chans_need != -1) { |
| 200 | if (can_send_pag_req(request->bts, request->chan_type) != 0) |
| 201 | goto skip_paging; |
| 202 | } |
| 203 | |
Holger Hans Peter Freyther | c814ae9 | 2010-09-06 10:05:23 +0800 | [diff] [blame] | 204 | /* handle the paging request now */ |
| 205 | page_ms(request); |
| 206 | paging_bts->available_slots--; |
Holger Hans Peter Freyther | ab2a933 | 2010-12-23 18:19:17 +0100 | [diff] [blame] | 207 | request->attempts++; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 208 | |
Holger Hans Peter Freyther | c814ae9 | 2010-09-06 10:05:23 +0800 | [diff] [blame] | 209 | /* take the current and add it to the back */ |
| 210 | llist_del(&request->entry); |
| 211 | llist_add_tail(&request->entry, &paging_bts->pending_requests); |
Holger Hans Peter Freyther | 5ded191 | 2010-04-09 21:53:35 +0200 | [diff] [blame] | 212 | |
Holger Hans Peter Freyther | 3112f67 | 2010-09-06 10:11:25 +0800 | [diff] [blame] | 213 | skip_paging: |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 214 | osmo_timer_schedule(&paging_bts->work_timer, PAGING_TIMER); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | static void paging_worker(void *data) |
| 218 | { |
| 219 | struct gsm_bts_paging_state *paging_bts = data; |
| 220 | |
| 221 | paging_handle_pending_requests(paging_bts); |
| 222 | } |
| 223 | |
Holger Hans Peter Freyther | bd421f2 | 2011-04-26 15:44:57 +0200 | [diff] [blame] | 224 | static void paging_init_if_needed(struct gsm_bts *bts) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 225 | { |
Holger Hans Peter Freyther | bd421f2 | 2011-04-26 15:44:57 +0200 | [diff] [blame] | 226 | if (bts->paging.bts) |
| 227 | return; |
| 228 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 229 | bts->paging.bts = bts; |
| 230 | INIT_LLIST_HEAD(&bts->paging.pending_requests); |
| 231 | bts->paging.work_timer.cb = paging_worker; |
| 232 | bts->paging.work_timer.data = &bts->paging; |
| 233 | |
| 234 | /* Large number, until we get a proper message */ |
Holger Hans Peter Freyther | c13d32f | 2010-04-09 22:55:55 +0200 | [diff] [blame] | 235 | bts->paging.available_slots = 20; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | static int paging_pending_request(struct gsm_bts_paging_state *bts, |
| 239 | struct gsm_subscriber *subscr) { |
| 240 | struct gsm_paging_request *req; |
| 241 | |
| 242 | llist_for_each_entry(req, &bts->pending_requests, entry) { |
| 243 | if (subscr == req->subscr) |
| 244 | return 1; |
| 245 | } |
| 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | static void paging_T3113_expired(void *data) |
| 251 | { |
| 252 | struct gsm_paging_request *req = (struct gsm_paging_request *)data; |
Holger Hans Peter Freyther | 353e9b6 | 2009-11-17 10:16:46 +0100 | [diff] [blame] | 253 | void *cbfn_param; |
| 254 | gsm_cbfn *cbfn; |
Holger Hans Peter Freyther | ab2a933 | 2010-12-23 18:19:17 +0100 | [diff] [blame] | 255 | int msg; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 256 | |
Holger Hans Peter Freyther | 75cf52a | 2014-12-28 18:54:32 +0100 | [diff] [blame] | 257 | log_set_context(BSC_CTX_SUBSCR, req->subscr); |
Holger Hans Peter Freyther | 66f55e6 | 2014-12-28 15:00:36 +0100 | [diff] [blame] | 258 | |
Holger Hans Peter Freyther | b6a3394 | 2010-04-09 22:02:39 +0200 | [diff] [blame] | 259 | LOGP(DPAG, LOGL_INFO, "T3113 expired for request %p (%s)\n", |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 260 | req, req->subscr->imsi); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 261 | |
Holger Hans Peter Freyther | 353e9b6 | 2009-11-17 10:16:46 +0100 | [diff] [blame] | 262 | /* must be destroyed before calling cbfn, to prevent double free */ |
Alexander Couzens | d8aad3a | 2016-08-02 11:34:11 +0200 | [diff] [blame] | 263 | rate_ctr_inc(&req->bts->network->bsc_ctrs->ctr[BSC_CTR_PAGING_EXPIRED]); |
Holger Hans Peter Freyther | 353e9b6 | 2009-11-17 10:16:46 +0100 | [diff] [blame] | 264 | cbfn_param = req->cbfn_param; |
| 265 | cbfn = req->cbfn; |
Holger Hans Peter Freyther | ab2a933 | 2010-12-23 18:19:17 +0100 | [diff] [blame] | 266 | |
| 267 | /* did we ever manage to page the subscriber */ |
| 268 | msg = req->attempts > 0 ? GSM_PAGING_EXPIRED : GSM_PAGING_BUSY; |
| 269 | |
| 270 | /* destroy it now. Do not access req afterwards */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 271 | paging_remove_request(&req->bts->paging, req); |
Holger Hans Peter Freyther | 353e9b6 | 2009-11-17 10:16:46 +0100 | [diff] [blame] | 272 | |
Holger Hans Peter Freyther | 353e9b6 | 2009-11-17 10:16:46 +0100 | [diff] [blame] | 273 | if (cbfn) |
Holger Hans Peter Freyther | ab2a933 | 2010-12-23 18:19:17 +0100 | [diff] [blame] | 274 | cbfn(GSM_HOOK_RR_PAGING, msg, NULL, NULL, |
Holger Hans Peter Freyther | 353e9b6 | 2009-11-17 10:16:46 +0100 | [diff] [blame] | 275 | cbfn_param); |
Holger Hans Peter Freyther | 66f55e6 | 2014-12-28 15:00:36 +0100 | [diff] [blame] | 276 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 277 | } |
| 278 | |
Harald Welte | d363c95 | 2009-08-15 03:16:17 +0200 | [diff] [blame] | 279 | static int _paging_request(struct gsm_bts *bts, struct gsm_subscriber *subscr, |
Holger Hans Peter Freyther | 13d472d | 2009-06-10 02:45:42 +0200 | [diff] [blame] | 280 | int type, gsm_cbfn *cbfn, void *data) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 281 | { |
| 282 | struct gsm_bts_paging_state *bts_entry = &bts->paging; |
| 283 | struct gsm_paging_request *req; |
| 284 | |
| 285 | if (paging_pending_request(bts_entry, subscr)) { |
Holger Hans Peter Freyther | b6a3394 | 2010-04-09 22:02:39 +0200 | [diff] [blame] | 286 | LOGP(DPAG, LOGL_INFO, "Paging request already pending for %s\n", subscr->imsi); |
Harald Welte | d363c95 | 2009-08-15 03:16:17 +0200 | [diff] [blame] | 287 | return -EEXIST; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 288 | } |
| 289 | |
Holger Hans Peter Freyther | b6a3394 | 2010-04-09 22:02:39 +0200 | [diff] [blame] | 290 | LOGP(DPAG, LOGL_DEBUG, "Start paging of subscriber %llu on bts %d.\n", |
Harald Welte | cd97c21 | 2009-08-08 15:08:19 +0200 | [diff] [blame] | 291 | subscr->id, bts->nr); |
Harald Welte | 857e00d | 2009-06-26 20:25:23 +0200 | [diff] [blame] | 292 | req = talloc_zero(tall_paging_ctx, struct gsm_paging_request); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 293 | req->subscr = subscr_get(subscr); |
| 294 | req->bts = bts; |
| 295 | req->chan_type = type; |
| 296 | req->cbfn = cbfn; |
| 297 | req->cbfn_param = data; |
| 298 | req->T3113.cb = paging_T3113_expired; |
| 299 | req->T3113.data = req; |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 300 | osmo_timer_schedule(&req->T3113, bts->network->T3113, 0); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 301 | llist_add_tail(&req->entry, &bts_entry->pending_requests); |
Holger Hans Peter Freyther | c814ae9 | 2010-09-06 10:05:23 +0800 | [diff] [blame] | 302 | paging_schedule_if_needed(bts_entry); |
Harald Welte | d363c95 | 2009-08-15 03:16:17 +0200 | [diff] [blame] | 303 | |
| 304 | return 0; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 305 | } |
| 306 | |
Holger Hans Peter Freyther | 81598ea | 2013-01-07 17:30:13 +0100 | [diff] [blame] | 307 | int paging_request_bts(struct gsm_bts *bts, struct gsm_subscriber *subscr, |
| 308 | int type, gsm_cbfn *cbfn, void *data) |
| 309 | { |
| 310 | int rc; |
| 311 | |
| 312 | /* skip all currently inactive TRX */ |
| 313 | if (!trx_is_usable(bts->c0)) |
| 314 | return 0; |
| 315 | |
| 316 | /* maybe it is the first time we use it */ |
| 317 | paging_init_if_needed(bts); |
| 318 | |
| 319 | |
| 320 | /* Trigger paging, pass any error to the caller */ |
| 321 | rc = _paging_request(bts, subscr, type, cbfn, data); |
| 322 | if (rc < 0) |
| 323 | return rc; |
| 324 | return 1; |
| 325 | } |
| 326 | |
Harald Welte | d363c95 | 2009-08-15 03:16:17 +0200 | [diff] [blame] | 327 | int paging_request(struct gsm_network *network, struct gsm_subscriber *subscr, |
| 328 | int type, gsm_cbfn *cbfn, void *data) |
Holger Hans Peter Freyther | 13d472d | 2009-06-10 02:45:42 +0200 | [diff] [blame] | 329 | { |
| 330 | struct gsm_bts *bts = NULL; |
Harald Welte (local) | dec08ee | 2009-08-15 11:25:45 +0200 | [diff] [blame] | 331 | int num_pages = 0; |
Holger Hans Peter Freyther | 13d472d | 2009-06-10 02:45:42 +0200 | [diff] [blame] | 332 | |
Alexander Couzens | d8aad3a | 2016-08-02 11:34:11 +0200 | [diff] [blame] | 333 | rate_ctr_inc(&network->bsc_ctrs->ctr[BSC_CTR_PAGING_ATTEMPTED]); |
Harald Welte | 3edc5a9 | 2009-12-22 00:41:05 +0100 | [diff] [blame] | 334 | |
Harald Welte | 2a3a81b | 2009-08-01 19:31:47 +0200 | [diff] [blame] | 335 | /* start paging subscriber on all BTS within Location Area */ |
Holger Hans Peter Freyther | 13d472d | 2009-06-10 02:45:42 +0200 | [diff] [blame] | 336 | do { |
Harald Welte (local) | dec08ee | 2009-08-15 11:25:45 +0200 | [diff] [blame] | 337 | int rc; |
| 338 | |
Holger Freyther | 93795ac | 2009-03-31 04:35:19 +0200 | [diff] [blame] | 339 | bts = gsm_bts_by_lac(network, subscr->lac, bts); |
Holger Hans Peter Freyther | 13d472d | 2009-06-10 02:45:42 +0200 | [diff] [blame] | 340 | if (!bts) |
| 341 | break; |
Harald Welte (local) | 43db2bf | 2009-12-28 16:36:28 +0100 | [diff] [blame] | 342 | |
Holger Hans Peter Freyther | 81598ea | 2013-01-07 17:30:13 +0100 | [diff] [blame] | 343 | rc = paging_request_bts(bts, subscr, type, cbfn, data); |
Holger Hans Peter Freyther | e69e691 | 2015-08-03 11:19:35 +0200 | [diff] [blame] | 344 | if (rc < 0) { |
| 345 | paging_request_stop(NULL, subscr, NULL, NULL); |
Harald Welte | d363c95 | 2009-08-15 03:16:17 +0200 | [diff] [blame] | 346 | return rc; |
Holger Hans Peter Freyther | e69e691 | 2015-08-03 11:19:35 +0200 | [diff] [blame] | 347 | } |
Holger Hans Peter Freyther | 81598ea | 2013-01-07 17:30:13 +0100 | [diff] [blame] | 348 | num_pages += rc; |
Holger Hans Peter Freyther | 13d472d | 2009-06-10 02:45:42 +0200 | [diff] [blame] | 349 | } while (1); |
Harald Welte | d363c95 | 2009-08-15 03:16:17 +0200 | [diff] [blame] | 350 | |
Harald Welte | 3edc5a9 | 2009-12-22 00:41:05 +0100 | [diff] [blame] | 351 | if (num_pages == 0) |
Alexander Couzens | d8aad3a | 2016-08-02 11:34:11 +0200 | [diff] [blame] | 352 | rate_ctr_inc(&network->bsc_ctrs->ctr[BSC_CTR_PAGING_DETACHED]); |
Harald Welte | 3edc5a9 | 2009-12-22 00:41:05 +0100 | [diff] [blame] | 353 | |
Harald Welte (local) | dec08ee | 2009-08-15 11:25:45 +0200 | [diff] [blame] | 354 | return num_pages; |
Holger Hans Peter Freyther | 13d472d | 2009-06-10 02:45:42 +0200 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 358 | /* we consciously ignore the type of the request here */ |
Holger Hans Peter Freyther | 13d472d | 2009-06-10 02:45:42 +0200 | [diff] [blame] | 359 | static void _paging_request_stop(struct gsm_bts *bts, struct gsm_subscriber *subscr, |
Sylvain Munaut | b7b6e78 | 2010-12-01 21:41:35 +0100 | [diff] [blame] | 360 | struct gsm_subscriber_connection *conn, |
| 361 | struct msgb *msg) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 362 | { |
| 363 | struct gsm_bts_paging_state *bts_entry = &bts->paging; |
| 364 | struct gsm_paging_request *req, *req2; |
| 365 | |
Holger Hans Peter Freyther | bd421f2 | 2011-04-26 15:44:57 +0200 | [diff] [blame] | 366 | paging_init_if_needed(bts); |
| 367 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 368 | llist_for_each_entry_safe(req, req2, &bts_entry->pending_requests, |
| 369 | entry) { |
| 370 | if (req->subscr == subscr) { |
Holger Hans Peter Freyther | 551ce1f | 2015-04-06 16:54:55 +0200 | [diff] [blame] | 371 | gsm_cbfn *cbfn = req->cbfn; |
| 372 | void *param = req->cbfn_param; |
| 373 | |
| 374 | /* now give up the data structure */ |
| 375 | paging_remove_request(&bts->paging, req); |
| 376 | req = NULL; |
| 377 | |
| 378 | if (conn && cbfn) { |
Holger Hans Peter Freyther | b6a3394 | 2010-04-09 22:02:39 +0200 | [diff] [blame] | 379 | LOGP(DPAG, LOGL_DEBUG, "Stop paging on bts %d, calling cbfn.\n", bts->nr); |
Holger Hans Peter Freyther | 551ce1f | 2015-04-06 16:54:55 +0200 | [diff] [blame] | 380 | cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_SUCCEEDED, |
| 381 | msg, conn, param); |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 382 | } else |
Holger Hans Peter Freyther | b6a3394 | 2010-04-09 22:02:39 +0200 | [diff] [blame] | 383 | LOGP(DPAG, LOGL_DEBUG, "Stop paging on bts %d silently.\n", bts->nr); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 384 | break; |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
Holger Hans Peter Freyther | 13d472d | 2009-06-10 02:45:42 +0200 | [diff] [blame] | 389 | /* Stop paging on all other bts' */ |
| 390 | void paging_request_stop(struct gsm_bts *_bts, struct gsm_subscriber *subscr, |
Sylvain Munaut | b7b6e78 | 2010-12-01 21:41:35 +0100 | [diff] [blame] | 391 | struct gsm_subscriber_connection *conn, |
| 392 | struct msgb *msg) |
Holger Hans Peter Freyther | 13d472d | 2009-06-10 02:45:42 +0200 | [diff] [blame] | 393 | { |
Holger Hans Peter Freyther | dab5783 | 2015-08-03 11:20:31 +0200 | [diff] [blame] | 394 | struct gsm_bts *bts; |
Holger Hans Peter Freyther | 13d472d | 2009-06-10 02:45:42 +0200 | [diff] [blame] | 395 | |
Holger Hans Peter Freyther | 66f55e6 | 2014-12-28 15:00:36 +0100 | [diff] [blame] | 396 | log_set_context(BSC_CTX_SUBSCR, subscr); |
| 397 | |
Holger Hans Peter Freyther | dab5783 | 2015-08-03 11:20:31 +0200 | [diff] [blame] | 398 | /* Stop this first and dispatch the request */ |
Holger Hans Peter Freyther | b78507a | 2009-08-21 05:43:44 +0200 | [diff] [blame] | 399 | if (_bts) |
Sylvain Munaut | b7b6e78 | 2010-12-01 21:41:35 +0100 | [diff] [blame] | 400 | _paging_request_stop(_bts, subscr, conn, msg); |
Holger Hans Peter Freyther | c51e333 | 2009-06-10 11:46:58 +0200 | [diff] [blame] | 401 | |
Holger Hans Peter Freyther | dab5783 | 2015-08-03 11:20:31 +0200 | [diff] [blame] | 402 | /* Make sure to cancel this everywhere else */ |
| 403 | llist_for_each_entry(bts, &subscr->group->net->bts_list, list) { |
| 404 | /* Sort of an optimization. */ |
| 405 | if (bts == _bts) |
| 406 | continue; |
| 407 | _paging_request_stop(bts, subscr, NULL, NULL); |
| 408 | } |
Holger Hans Peter Freyther | 13d472d | 2009-06-10 02:45:42 +0200 | [diff] [blame] | 409 | } |
| 410 | |
Holger Hans Peter Freyther | 7eb8a9a | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 411 | void paging_update_buffer_space(struct gsm_bts *bts, uint16_t free_slots) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 412 | { |
Holger Hans Peter Freyther | bd421f2 | 2011-04-26 15:44:57 +0200 | [diff] [blame] | 413 | paging_init_if_needed(bts); |
| 414 | |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 415 | osmo_timer_del(&bts->paging.credit_timer); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 416 | bts->paging.available_slots = free_slots; |
Holger Hans Peter Freyther | c814ae9 | 2010-09-06 10:05:23 +0800 | [diff] [blame] | 417 | paging_schedule_if_needed(&bts->paging); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 418 | } |
Holger Hans Peter Freyther | 914ff19 | 2011-04-26 15:52:34 +0200 | [diff] [blame] | 419 | |
| 420 | unsigned int paging_pending_requests_nr(struct gsm_bts *bts) |
| 421 | { |
| 422 | unsigned int requests = 0; |
| 423 | struct gsm_paging_request *req; |
| 424 | |
| 425 | paging_init_if_needed(bts); |
| 426 | |
| 427 | llist_for_each_entry(req, &bts->paging.pending_requests, entry) |
| 428 | ++requests; |
| 429 | |
| 430 | return requests; |
| 431 | } |
Holger Hans Peter Freyther | ad39db6 | 2011-06-07 19:53:29 +0200 | [diff] [blame] | 432 | |
| 433 | /** |
| 434 | * Find any paging data for the given subscriber at the given BTS. |
| 435 | */ |
| 436 | void *paging_get_data(struct gsm_bts *bts, struct gsm_subscriber *subscr) |
| 437 | { |
| 438 | struct gsm_paging_request *req; |
| 439 | |
| 440 | llist_for_each_entry(req, &bts->paging.pending_requests, entry) |
| 441 | if (req->subscr == subscr) |
| 442 | return req->cbfn_param; |
| 443 | |
| 444 | return NULL; |
| 445 | } |