blob: 825d31e35b19167ae832b5c39e08cc8deea966c5 [file] [log] [blame]
Harald Welteb68899d2009-01-06 21:47:18 +00001/* Paging helper and manager.... */
Holger Hans Peter Freytherdc030962013-01-07 17:30:13 +01002/* (C) 2009,2013 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welteb68899d2009-01-06 21:47:18 +00003 * All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01006 * 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 Welteb68899d2009-01-06 21:47:18 +00008 * (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 Welte9af6ddf2011-01-01 15:25:50 +010013 * GNU Affero General Public License for more details.
Harald Welteb68899d2009-01-06 21:47:18 +000014 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010015 * 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 Welteb68899d2009-01-06 21:47:18 +000017 *
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
Harald Welte2f1311b2009-01-06 23:50:39 +000037#include <stdio.h>
38#include <stdlib.h>
Holger Freythere36e15e2009-02-14 18:05:03 +000039#include <assert.h>
Harald Welte2f1311b2009-01-06 23:50:39 +000040
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010041#include <osmocom/core/talloc.h>
Harald Welte457a0812011-06-26 14:21:43 +020042#include <osmocom/gsm/gsm48.h>
Harald Weltebc82f922011-06-26 14:41:58 +020043#include <osmocom/gsm/gsm0502.h>
Harald Welte457a0812011-06-26 14:21:43 +020044
Neels Hofmeyrc0164792017-09-04 15:15:32 +020045#include <osmocom/bsc/bsc_subscriber.h>
46#include <osmocom/bsc/paging.h>
47#include <osmocom/bsc/debug.h>
48#include <osmocom/bsc/signal.h>
49#include <osmocom/bsc/abis_rsl.h>
50#include <osmocom/bsc/gsm_data.h>
51#include <osmocom/bsc/chan_alloc.h>
52#include <osmocom/bsc/bsc_api.h>
Harald Welteb68899d2009-01-06 21:47:18 +000053
Neels Hofmeyrcc6240a2018-02-13 21:21:42 +010054void *tall_paging_ctx = NULL;
Harald Welte2cf161b2009-06-20 22:36:41 +020055
Holger Hans Peter Freythereb241aa2010-09-06 10:05:23 +080056#define PAGING_TIMER 0, 500000
57
Harald Welte38c2f132009-01-06 23:10:57 +000058/*
Neels Hofmeyrc29505e2016-05-20 21:59:55 +020059 * TODO MSCSPLIT: the paging in libbsc is closely tied to MSC land in that the
60 * MSC realm callback functions used to be invoked from the BSC/BTS level. So
61 * this entire file needs to be rewired for use with an A interface.
62 */
63
64/*
Harald Welte38c2f132009-01-06 23:10:57 +000065 * Kill one paging request update the internal list...
66 */
Holger Freyther1fd34142009-02-09 23:42:03 +000067static void paging_remove_request(struct gsm_bts_paging_state *paging_bts,
Neels Hofmeyr6d804b12017-02-18 22:20:46 +010068 struct gsm_paging_request *to_be_deleted)
Holger Freyther3d949242009-02-06 18:08:18 +000069{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +020070 osmo_timer_del(&to_be_deleted->T3113);
Harald Welte38c2f132009-01-06 23:10:57 +000071 llist_del(&to_be_deleted->entry);
Neels Hofmeyr6d804b12017-02-18 22:20:46 +010072 bsc_subscr_put(to_be_deleted->bsub);
Harald Welte2cf161b2009-06-20 22:36:41 +020073 talloc_free(to_be_deleted);
Harald Welte38c2f132009-01-06 23:10:57 +000074}
75
Holger Freyther33ba7522009-02-06 22:33:25 +000076static void page_ms(struct gsm_paging_request *request)
77{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020078 uint8_t mi[128];
Harald Welte2f1311b2009-01-06 23:50:39 +000079 unsigned int mi_len;
Holger Freyther33ba7522009-02-06 22:33:25 +000080 unsigned int page_group;
Harald Welte1cf43ec2011-06-26 14:43:46 +020081 struct gsm_bts *bts = request->bts;
Holger Freyther33ba7522009-02-06 22:33:25 +000082
Holger Hans Peter Freyther10049bc2012-12-22 19:27:27 +010083 /* the bts is down.. we will just wait for the paging to expire */
84 if (!bts->oml_link)
85 return;
86
Neels Hofmeyr6d804b12017-02-18 22:20:46 +010087 log_set_context(LOG_CTX_BSC_SUBSCR, request->bsub);
Holger Hans Peter Freytherb7ccac42014-12-28 15:00:36 +010088
Stefan Sperlingd2474ca2018-01-08 16:43:32 +010089 LOGP(DPAG, LOGL_INFO, "(bts=%d) Going to send paging commands: imsi: %s tmsi: "
90 "0x%08x for ch. type %d (attempt %d)\n", bts->nr, request->bsub->imsi,
Neels Hofmeyr6d804b12017-02-18 22:20:46 +010091 request->bsub->tmsi, request->chan_type, request->attempts);
Holger Hans Peter Freytherb28285d2009-08-21 14:55:57 +020092
Neels Hofmeyr6d804b12017-02-18 22:20:46 +010093 if (request->bsub->tmsi == GSM_RESERVED_TMSI)
94 mi_len = gsm48_generate_mid_from_imsi(mi, request->bsub->imsi);
Holger Hans Peter Freytherb28285d2009-08-21 14:55:57 +020095 else
Neels Hofmeyr6d804b12017-02-18 22:20:46 +010096 mi_len = gsm48_generate_mid_from_tmsi(mi, request->bsub->tmsi);
Holger Freyther33ba7522009-02-06 22:33:25 +000097
Harald Welte1cf43ec2011-06-26 14:43:46 +020098 page_group = gsm0502_calc_paging_group(&bts->si_common.chan_desc,
Neels Hofmeyr6d804b12017-02-18 22:20:46 +010099 str_to_imsi(request->bsub->imsi));
Harald Welte1cf43ec2011-06-26 14:43:46 +0200100 gsm0808_page(bts, page_group, mi_len, mi, request->chan_type);
Neels Hofmeyr6d804b12017-02-18 22:20:46 +0100101 log_set_context(LOG_CTX_BSC_SUBSCR, NULL);
Holger Freyther33ba7522009-02-06 22:33:25 +0000102}
103
Holger Hans Peter Freythereb241aa2010-09-06 10:05:23 +0800104static void paging_schedule_if_needed(struct gsm_bts_paging_state *paging_bts)
105{
106 if (llist_empty(&paging_bts->pending_requests))
107 return;
108
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200109 if (!osmo_timer_pending(&paging_bts->work_timer))
110 osmo_timer_schedule(&paging_bts->work_timer, PAGING_TIMER);
Holger Hans Peter Freythereb241aa2010-09-06 10:05:23 +0800111}
112
113
Holger Hans Peter Freyther78fab292010-04-20 10:59:33 +0800114static void paging_handle_pending_requests(struct gsm_bts_paging_state *paging_bts);
Holger Hans Peter Freyther6410d932010-04-19 19:41:26 +0800115static void paging_give_credit(void *data)
116{
117 struct gsm_bts_paging_state *paging_bts = data;
118
119 LOGP(DPAG, LOGL_NOTICE, "No slots available on bts nr %d\n", paging_bts->bts->nr);
120 paging_bts->available_slots = 20;
Holger Hans Peter Freyther78fab292010-04-20 10:59:33 +0800121 paging_handle_pending_requests(paging_bts);
Holger Hans Peter Freyther6410d932010-04-19 19:41:26 +0800122}
123
Harald Welte958cccf2017-12-11 14:25:08 +0100124/*! count the number of free channels for given RSL channel type required
125 * \param[in] BTS on which we shall count
126 * \param[in] rsl_type the RSL channel needed type
127 * \returns number of free channels matching \a rsl_type in \a bts */
Holger Hans Peter Freythere66bf1f2010-09-06 10:11:25 +0800128static int can_send_pag_req(struct gsm_bts *bts, int rsl_type)
129{
130 struct pchan_load pl;
131 int count;
132
133 memset(&pl, 0, sizeof(pl));
Neels Hofmeyr2afffd52016-09-25 17:01:20 +0200134 bts_chan_load(&pl, bts);
Holger Hans Peter Freythere66bf1f2010-09-06 10:11:25 +0800135
136 switch (rsl_type) {
137 case RSL_CHANNEED_TCH_F:
Holger Hans Peter Freythere66bf1f2010-09-06 10:11:25 +0800138 case RSL_CHANNEED_TCH_ForH:
Holger Hans Peter Freyther2e4447b2010-04-20 17:15:21 +0800139 goto count_tch;
Holger Hans Peter Freythere66bf1f2010-09-06 10:11:25 +0800140 break;
141 case RSL_CHANNEED_SDCCH:
142 goto count_sdcch;
143 break;
144 case RSL_CHANNEED_ANY:
145 default:
146 if (bts->network->pag_any_tch)
Holger Hans Peter Freyther2e4447b2010-04-20 17:15:21 +0800147 goto count_tch;
Holger Hans Peter Freythere66bf1f2010-09-06 10:11:25 +0800148 else
149 goto count_sdcch;
150 break;
151 }
152
153 return 0;
154
155 /* could available SDCCH */
156count_sdcch:
157 count = 0;
158 count += pl.pchan[GSM_PCHAN_SDCCH8_SACCH8C].total
159 - pl.pchan[GSM_PCHAN_SDCCH8_SACCH8C].used;
160 count += pl.pchan[GSM_PCHAN_CCCH_SDCCH4].total
161 - pl.pchan[GSM_PCHAN_CCCH_SDCCH4].used;
162 return bts->paging.free_chans_need > count;
Holger Hans Peter Freyther2e4447b2010-04-20 17:15:21 +0800163
164count_tch:
165 count = 0;
166 count += pl.pchan[GSM_PCHAN_TCH_F].total
167 - pl.pchan[GSM_PCHAN_TCH_F].used;
168 if (bts->network->neci)
169 count += pl.pchan[GSM_PCHAN_TCH_H].total
170 - pl.pchan[GSM_PCHAN_TCH_H].used;
171 return bts->paging.free_chans_need > count;
Holger Hans Peter Freythere66bf1f2010-09-06 10:11:25 +0800172}
173
Holger Freythere36e15e2009-02-14 18:05:03 +0000174/*
175 * This is kicked by the periodic PAGING LOAD Indicator
176 * coming from abis_rsl.c
177 *
178 * We attempt to iterate once over the list of items but
179 * only upto available_slots.
180 */
Holger Freytherb1ffbb62009-02-11 01:49:15 +0000181static void paging_handle_pending_requests(struct gsm_bts_paging_state *paging_bts)
182{
Holger Hans Peter Freythereb241aa2010-09-06 10:05:23 +0800183 struct gsm_paging_request *request = NULL;
Harald Welte38c2f132009-01-06 23:10:57 +0000184
Holger Freythere36e15e2009-02-14 18:05:03 +0000185 /*
186 * Determine if the pending_requests list is empty and
187 * return then.
188 */
Harald Welte75a1fa82009-02-17 01:39:41 +0000189 if (llist_empty(&paging_bts->pending_requests)) {
Harald Welte75a1fa82009-02-17 01:39:41 +0000190 /* since the list is empty, no need to reschedule the timer */
Harald Welte38c2f132009-01-06 23:10:57 +0000191 return;
192 }
193
Holger Hans Peter Freyther19722d42010-04-09 18:38:06 +0200194 /*
Holger Hans Peter Freyther6410d932010-04-19 19:41:26 +0800195 * In case the BTS does not provide us with load indication and we
196 * ran out of slots, call an autofill routine. It might be that the
197 * BTS did not like our paging messages and then we have counted down
198 * to zero and we do not get any messages.
Holger Hans Peter Freyther19722d42010-04-09 18:38:06 +0200199 */
200 if (paging_bts->available_slots == 0) {
Pablo Neira Ayuso51215762017-05-08 20:57:52 +0200201 osmo_timer_setup(&paging_bts->credit_timer, paging_give_credit,
202 paging_bts);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200203 osmo_timer_schedule(&paging_bts->credit_timer, 5, 0);
Holger Hans Peter Freyther6410d932010-04-19 19:41:26 +0800204 return;
Holger Hans Peter Freyther19722d42010-04-09 18:38:06 +0200205 }
206
Holger Hans Peter Freythereb241aa2010-09-06 10:05:23 +0800207 request = llist_entry(paging_bts->pending_requests.next,
208 struct gsm_paging_request, entry);
Holger Freythere36e15e2009-02-14 18:05:03 +0000209
Holger Hans Peter Freythere66bf1f2010-09-06 10:11:25 +0800210 /* we need to determine the number of free channels */
211 if (paging_bts->free_chans_need != -1) {
212 if (can_send_pag_req(request->bts, request->chan_type) != 0)
213 goto skip_paging;
214 }
215
Holger Hans Peter Freythereb241aa2010-09-06 10:05:23 +0800216 /* handle the paging request now */
217 page_ms(request);
218 paging_bts->available_slots--;
Holger Hans Peter Freytherd3baf412010-12-23 18:19:17 +0100219 request->attempts++;
Holger Freythere36e15e2009-02-14 18:05:03 +0000220
Holger Hans Peter Freythereb241aa2010-09-06 10:05:23 +0800221 /* take the current and add it to the back */
222 llist_del(&request->entry);
223 llist_add_tail(&request->entry, &paging_bts->pending_requests);
Holger Hans Peter Freyther754e8012010-04-09 21:53:35 +0200224
Holger Hans Peter Freythere66bf1f2010-09-06 10:11:25 +0800225skip_paging:
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200226 osmo_timer_schedule(&paging_bts->work_timer, PAGING_TIMER);
Harald Welte75a1fa82009-02-17 01:39:41 +0000227}
228
229static void paging_worker(void *data)
230{
231 struct gsm_bts_paging_state *paging_bts = data;
232
233 paging_handle_pending_requests(paging_bts);
Harald Welte38c2f132009-01-06 23:10:57 +0000234}
235
Harald Welte958cccf2017-12-11 14:25:08 +0100236/*! initialize the bts paging state, if it hasn't been initialized yet */
Holger Hans Peter Freyther5a081bb2011-04-26 15:44:57 +0200237static void paging_init_if_needed(struct gsm_bts *bts)
Holger Freytherceb59b72009-02-06 18:54:00 +0000238{
Holger Hans Peter Freyther5a081bb2011-04-26 15:44:57 +0200239 if (bts->paging.bts)
240 return;
241
Holger Freytherceb59b72009-02-06 18:54:00 +0000242 bts->paging.bts = bts;
Neels Hofmeyr71932262017-12-13 19:05:36 +0100243
244 /* This should be initialized only once. There is currently no code that sets bts->paging.bts
245 * back to NULL, so let's just assert this one instead of graceful handling. */
246 OSMO_ASSERT(llist_empty(&bts->paging.pending_requests));
247
Pablo Neira Ayuso51215762017-05-08 20:57:52 +0200248 osmo_timer_setup(&bts->paging.work_timer, paging_worker,
249 &bts->paging);
Holger Freyther392209c2009-02-10 00:06:19 +0000250
251 /* Large number, until we get a proper message */
Holger Hans Peter Freyther14696002010-04-09 22:55:55 +0200252 bts->paging.available_slots = 20;
Holger Freytherceb59b72009-02-06 18:54:00 +0000253}
254
Harald Welte958cccf2017-12-11 14:25:08 +0100255/*! do we have any pending paging requests for given subscriber? */
Holger Freyther1fd34142009-02-09 23:42:03 +0000256static int paging_pending_request(struct gsm_bts_paging_state *bts,
Neels Hofmeyr6d804b12017-02-18 22:20:46 +0100257 struct bsc_subscr *bsub)
258{
Holger Freytherceb59b72009-02-06 18:54:00 +0000259 struct gsm_paging_request *req;
Harald Welteb68899d2009-01-06 21:47:18 +0000260
261 llist_for_each_entry(req, &bts->pending_requests, entry) {
Neels Hofmeyr6d804b12017-02-18 22:20:46 +0100262 if (bsub == req->bsub)
Harald Welteb68899d2009-01-06 21:47:18 +0000263 return 1;
264 }
265
266 return 0;
267}
268
Harald Welte958cccf2017-12-11 14:25:08 +0100269/*! Call-back once T3113 (paging timeout) expires for given paging_request */
Harald Welte51cce1e2009-02-10 17:35:04 +0000270static void paging_T3113_expired(void *data)
271{
Holger Freyther83e44b02009-02-14 18:04:59 +0000272 struct gsm_paging_request *req = (struct gsm_paging_request *)data;
Harald Welte51cce1e2009-02-10 17:35:04 +0000273
Neels Hofmeyr6d804b12017-02-18 22:20:46 +0100274 log_set_context(LOG_CTX_BSC_SUBSCR, req->bsub);
Holger Hans Peter Freytherb7ccac42014-12-28 15:00:36 +0100275
Holger Hans Peter Freytherc50b8362010-04-09 22:02:39 +0200276 LOGP(DPAG, LOGL_INFO, "T3113 expired for request %p (%s)\n",
Neels Hofmeyr6d804b12017-02-18 22:20:46 +0100277 req, bsc_subscr_name(req->bsub));
Holger Freyther4af2b482009-02-14 22:51:03 +0000278
Holger Hans Peter Freytherfe9da822009-11-17 10:16:46 +0100279 /* must be destroyed before calling cbfn, to prevent double free */
Harald Weltecf9d4312017-12-13 23:17:16 +0100280 rate_ctr_inc(&req->bts->bts_ctrs->ctr[BTS_CTR_PAGING_EXPIRED]);
Holger Hans Peter Freytherd3baf412010-12-23 18:19:17 +0100281
282 /* destroy it now. Do not access req afterwards */
Harald Welte51cce1e2009-02-10 17:35:04 +0000283 paging_remove_request(&req->bts->paging, req);
284}
285
Harald Welte958cccf2017-12-11 14:25:08 +0100286/*! Start paging + paging timer for given subscriber on given BTS
287 * \param bts BTS on which to page
288 * \param[in] bsub subscriber we want to page
289 * \param[in] type type of radio channel we're requirign
Harald Weltef4b66fb2017-12-11 15:21:48 +0100290 * \param[in] msc MSC which has issue this paging
Harald Welte958cccf2017-12-11 14:25:08 +0100291 * \returns 0 on success, negative on error */
Harald Weltef4b66fb2017-12-11 15:21:48 +0100292static int _paging_request(struct gsm_bts *bts, struct bsc_subscr *bsub, int type,
293 struct bsc_msc_data *msc)
Harald Welte09e38af2009-02-16 22:52:23 +0000294{
Holger Freytherceb59b72009-02-06 18:54:00 +0000295 struct gsm_bts_paging_state *bts_entry = &bts->paging;
296 struct gsm_paging_request *req;
Harald Welteb68899d2009-01-06 21:47:18 +0000297
Harald Weltecf9d4312017-12-13 23:17:16 +0100298 rate_ctr_inc(&bts->bts_ctrs->ctr[BTS_CTR_PAGING_ATTEMPTED]);
299
Neels Hofmeyr6d804b12017-02-18 22:20:46 +0100300 if (paging_pending_request(bts_entry, bsub)) {
301 LOGP(DPAG, LOGL_INFO, "Paging request already pending for %s\n",
302 bsc_subscr_name(bsub));
Harald Weltecf9d4312017-12-13 23:17:16 +0100303 rate_ctr_inc(&bts->bts_ctrs->ctr[BTS_CTR_PAGING_ALREADY]);
Harald Weltee903edf2009-08-15 03:16:17 +0200304 return -EEXIST;
Holger Freyther49314f62009-02-14 23:53:20 +0000305 }
306
Neels Hofmeyr6d804b12017-02-18 22:20:46 +0100307 LOGP(DPAG, LOGL_DEBUG, "Start paging of subscriber %s on bts %d.\n",
308 bsc_subscr_name(bsub), bts->nr);
Harald Welte470ec292009-06-26 20:25:23 +0200309 req = talloc_zero(tall_paging_ctx, struct gsm_paging_request);
Harald Welte958cccf2017-12-11 14:25:08 +0100310 OSMO_ASSERT(req);
Neels Hofmeyr6d804b12017-02-18 22:20:46 +0100311 req->bsub = bsc_subscr_get(bsub);
Harald Welteb68899d2009-01-06 21:47:18 +0000312 req->bts = bts;
Harald Welte38c2f132009-01-06 23:10:57 +0000313 req->chan_type = type;
Harald Weltef4b66fb2017-12-11 15:21:48 +0100314 req->msc = msc;
Pablo Neira Ayuso51215762017-05-08 20:57:52 +0200315 osmo_timer_setup(&req->T3113, paging_T3113_expired, req);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200316 osmo_timer_schedule(&req->T3113, bts->network->T3113, 0);
Holger Freyther49314f62009-02-14 23:53:20 +0000317 llist_add_tail(&req->entry, &bts_entry->pending_requests);
Holger Hans Peter Freythereb241aa2010-09-06 10:05:23 +0800318 paging_schedule_if_needed(bts_entry);
Harald Weltee903edf2009-08-15 03:16:17 +0200319
320 return 0;
Harald Welteb68899d2009-01-06 21:47:18 +0000321}
Harald Welte763da002009-02-06 12:52:14 +0000322
Harald Welte958cccf2017-12-11 14:25:08 +0100323/*! Handle PAGING request from MSC for one (matching) BTS
324 * \param bts BTS on which to page
325 * \param[in] bsub subscriber we want to page
326 * \param[in] type type of radio channel we're requirign
Harald Weltef4b66fb2017-12-11 15:21:48 +0100327 * \param[in] msc MSC which has issue this paging
Harald Welte958cccf2017-12-11 14:25:08 +0100328 * returns 1 on success; 0 in case of error (e.g. TRX down) */
Harald Weltef4b66fb2017-12-11 15:21:48 +0100329int paging_request_bts(struct gsm_bts *bts, struct bsc_subscr *bsub, int type,
330 struct bsc_msc_data *msc)
Holger Hans Peter Freytherdc030962013-01-07 17:30:13 +0100331{
332 int rc;
333
334 /* skip all currently inactive TRX */
335 if (!trx_is_usable(bts->c0))
336 return 0;
337
338 /* maybe it is the first time we use it */
339 paging_init_if_needed(bts);
340
Holger Hans Peter Freytherdc030962013-01-07 17:30:13 +0100341 /* Trigger paging, pass any error to the caller */
Harald Weltef4b66fb2017-12-11 15:21:48 +0100342 rc = _paging_request(bts, bsub, type, msc);
Holger Hans Peter Freytherdc030962013-01-07 17:30:13 +0100343 if (rc < 0)
344 return rc;
345 return 1;
346}
347
Harald Welte958cccf2017-12-11 14:25:08 +0100348/*! Receive a new PAGING request from the MSC
349 * \param network gsm_network we operate in
350 * \param[in] bsub subscriber we want to page
351 * \param[in] type type of radio channel we're requirign
Harald Weltef4b66fb2017-12-11 15:21:48 +0100352 * \param[in] msc MSC which has issue this paging
Harald Welte958cccf2017-12-11 14:25:08 +0100353 * \returns number of BTSs on which we issued the paging */
Harald Weltef4b66fb2017-12-11 15:21:48 +0100354int paging_request(struct gsm_network *network, struct bsc_subscr *bsub, int type,
355 struct bsc_msc_data *msc)
Holger Hans Peter Freyther2c451232009-06-10 02:45:42 +0200356{
357 struct gsm_bts *bts = NULL;
Harald Welte (local)0abaf332009-08-15 11:25:45 +0200358 int num_pages = 0;
Holger Hans Peter Freyther2c451232009-06-10 02:45:42 +0200359
Alexander Couzensb847a212016-08-02 11:34:11 +0200360 rate_ctr_inc(&network->bsc_ctrs->ctr[BSC_CTR_PAGING_ATTEMPTED]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100361
Harald Weltea1b28582009-08-01 19:31:47 +0200362 /* start paging subscriber on all BTS within Location Area */
Holger Hans Peter Freyther2c451232009-06-10 02:45:42 +0200363 do {
Harald Welte (local)0abaf332009-08-15 11:25:45 +0200364 int rc;
365
Neels Hofmeyr6d804b12017-02-18 22:20:46 +0100366 bts = gsm_bts_by_lac(network, bsub->lac, bts);
Holger Hans Peter Freyther2c451232009-06-10 02:45:42 +0200367 if (!bts)
368 break;
Harald Welte (local)82ff3972009-12-28 16:36:28 +0100369
Harald Weltef4b66fb2017-12-11 15:21:48 +0100370 rc = paging_request_bts(bts, bsub, type, msc);
Holger Hans Peter Freyther469f9922015-08-03 11:19:35 +0200371 if (rc < 0) {
Neels Hofmeyr6d804b12017-02-18 22:20:46 +0100372 paging_request_stop(&network->bts_list, NULL, bsub,
373 NULL, NULL);
Harald Weltee903edf2009-08-15 03:16:17 +0200374 return rc;
Holger Hans Peter Freyther469f9922015-08-03 11:19:35 +0200375 }
Holger Hans Peter Freytherdc030962013-01-07 17:30:13 +0100376 num_pages += rc;
Holger Hans Peter Freyther2c451232009-06-10 02:45:42 +0200377 } while (1);
Harald Weltee903edf2009-08-15 03:16:17 +0200378
Harald Welte24ff6ee2009-12-22 00:41:05 +0100379 if (num_pages == 0)
Alexander Couzensb847a212016-08-02 11:34:11 +0200380 rate_ctr_inc(&network->bsc_ctrs->ctr[BSC_CTR_PAGING_DETACHED]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100381
Harald Welte (local)0abaf332009-08-15 11:25:45 +0200382 return num_pages;
Holger Hans Peter Freyther2c451232009-06-10 02:45:42 +0200383}
384
385
Harald Welte958cccf2017-12-11 14:25:08 +0100386/*! Stop paging a given subscriber on a given BTS.
387 * If \a conn is non-NULL, we also call the paging call-back function
388 * to notify the paging originator that paging has completed.
389 * \param[in] bts BTS on which we shall stop paging
390 * \param[in] bsub subscriber which we shall stop paging
391 * \param[in] conn connection to the subscriber (if any)
392 * \param[in] msg message received from subscrbier (if any) */
Harald Welte763da002009-02-06 12:52:14 +0000393/* we consciously ignore the type of the request here */
Neels Hofmeyr6d804b12017-02-18 22:20:46 +0100394static void _paging_request_stop(struct gsm_bts *bts, struct bsc_subscr *bsub,
Sylvain Munautc7ff2572010-12-01 21:41:35 +0100395 struct gsm_subscriber_connection *conn,
396 struct msgb *msg)
Harald Welte763da002009-02-06 12:52:14 +0000397{
Holger Freytherceb59b72009-02-06 18:54:00 +0000398 struct gsm_bts_paging_state *bts_entry = &bts->paging;
399 struct gsm_paging_request *req, *req2;
Harald Welte763da002009-02-06 12:52:14 +0000400
Holger Hans Peter Freyther5a081bb2011-04-26 15:44:57 +0200401 paging_init_if_needed(bts);
402
Harald Welte763da002009-02-06 12:52:14 +0000403 llist_for_each_entry_safe(req, req2, &bts_entry->pending_requests,
Neels Hofmeyr6d804b12017-02-18 22:20:46 +0100404 entry) {
405 if (req->bsub == bsub) {
Holger Hans Peter Freyther0ae564d2015-04-06 16:54:55 +0200406 /* now give up the data structure */
407 paging_remove_request(&bts->paging, req);
Harald Weltef4b66fb2017-12-11 15:21:48 +0100408 LOGP(DPAG, LOGL_DEBUG, "Stop paging %s on bts %d\n", bsub->imsi, bts->nr);
Holger Freyther3d949242009-02-06 18:08:18 +0000409 break;
410 }
Harald Welte763da002009-02-06 12:52:14 +0000411 }
412}
Holger Freyther392209c2009-02-10 00:06:19 +0000413
Harald Welte958cccf2017-12-11 14:25:08 +0100414/*! Stop paging on all other bts'
415 * \param[in] bts_list list of BTSs to iterate
416 * \param[in] _bts BTS which has received a paging response
417 * \param[in] bsub subscriber
418 * \param[in] msgb L3 message that we have received from \a bsub on \a _bts */
Neels Hofmeyr6d804b12017-02-18 22:20:46 +0100419void paging_request_stop(struct llist_head *bts_list,
420 struct gsm_bts *_bts, struct bsc_subscr *bsub,
Sylvain Munautc7ff2572010-12-01 21:41:35 +0100421 struct gsm_subscriber_connection *conn,
422 struct msgb *msg)
Holger Hans Peter Freyther2c451232009-06-10 02:45:42 +0200423{
Holger Hans Peter Freytherba433652015-08-03 11:20:31 +0200424 struct gsm_bts *bts;
Holger Hans Peter Freyther2c451232009-06-10 02:45:42 +0200425
Neels Hofmeyr6d804b12017-02-18 22:20:46 +0100426 log_set_context(LOG_CTX_BSC_SUBSCR, bsub);
Holger Hans Peter Freytherb7ccac42014-12-28 15:00:36 +0100427
Holger Hans Peter Freytherba433652015-08-03 11:20:31 +0200428 /* Stop this first and dispatch the request */
Harald Weltecf9d4312017-12-13 23:17:16 +0100429 if (_bts) {
Neels Hofmeyr6d804b12017-02-18 22:20:46 +0100430 _paging_request_stop(_bts, bsub, conn, msg);
Harald Weltecf9d4312017-12-13 23:17:16 +0100431 rate_ctr_inc(&_bts->bts_ctrs->ctr[BTS_CTR_PAGING_RESPONDED]);
432 rate_ctr_inc(&_bts->network->bsc_ctrs->ctr[BSC_CTR_PAGING_RESPONDED]);
433 }
Holger Hans Peter Freytherfdac4cc2009-06-10 11:46:58 +0200434
Holger Hans Peter Freytherba433652015-08-03 11:20:31 +0200435 /* Make sure to cancel this everywhere else */
Neels Hofmeyr6d804b12017-02-18 22:20:46 +0100436 llist_for_each_entry(bts, bts_list, list) {
Holger Hans Peter Freytherba433652015-08-03 11:20:31 +0200437 /* Sort of an optimization. */
438 if (bts == _bts)
439 continue;
Neels Hofmeyr6d804b12017-02-18 22:20:46 +0100440 _paging_request_stop(bts, bsub, NULL, NULL);
Holger Hans Peter Freytherba433652015-08-03 11:20:31 +0200441 }
Holger Hans Peter Freyther2c451232009-06-10 02:45:42 +0200442}
443
Harald Welte958cccf2017-12-11 14:25:08 +0100444
445/*! Update the BTS paging buffer slots on given BTS */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200446void paging_update_buffer_space(struct gsm_bts *bts, uint16_t free_slots)
Holger Freyther392209c2009-02-10 00:06:19 +0000447{
Holger Hans Peter Freyther5a081bb2011-04-26 15:44:57 +0200448 paging_init_if_needed(bts);
449
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200450 osmo_timer_del(&bts->paging.credit_timer);
Holger Freyther392209c2009-02-10 00:06:19 +0000451 bts->paging.available_slots = free_slots;
Holger Hans Peter Freythereb241aa2010-09-06 10:05:23 +0800452 paging_schedule_if_needed(&bts->paging);
Holger Freyther392209c2009-02-10 00:06:19 +0000453}
Holger Hans Peter Freyther66e14cd2011-04-26 15:52:34 +0200454
Harald Welte958cccf2017-12-11 14:25:08 +0100455/*! Count the number of pending paging requests on given BTS */
Holger Hans Peter Freyther66e14cd2011-04-26 15:52:34 +0200456unsigned int paging_pending_requests_nr(struct gsm_bts *bts)
457{
458 unsigned int requests = 0;
459 struct gsm_paging_request *req;
460
461 paging_init_if_needed(bts);
462
463 llist_for_each_entry(req, &bts->paging.pending_requests, entry)
464 ++requests;
465
466 return requests;
467}
Holger Hans Peter Freyther31f5f712011-06-07 19:53:29 +0200468
Harald Welte958cccf2017-12-11 14:25:08 +0100469/*! Find any paging data for the given subscriber at the given BTS. */
Harald Weltef4b66fb2017-12-11 15:21:48 +0100470struct bsc_msc_data *paging_get_msc(struct gsm_bts *bts, struct bsc_subscr *bsub)
Holger Hans Peter Freyther31f5f712011-06-07 19:53:29 +0200471{
472 struct gsm_paging_request *req;
473
474 llist_for_each_entry(req, &bts->paging.pending_requests, entry)
Neels Hofmeyr6d804b12017-02-18 22:20:46 +0100475 if (req->bsub == bsub)
Harald Weltef4b66fb2017-12-11 15:21:48 +0100476 return req->msc;
Holger Hans Peter Freyther31f5f712011-06-07 19:53:29 +0200477
478 return NULL;
479}
Harald Welted382bf62017-12-11 15:33:35 +0100480
481/*! Flush all paging requests at a given BTS for a given MSC*/
482void paging_flush_bts(struct gsm_bts *bts, struct bsc_msc_data *msc)
483{
484 struct gsm_paging_request *req, *req2;
485
Philipp Maier256d09d2017-12-13 15:33:25 +0100486 paging_init_if_needed(bts);
487
Harald Welted382bf62017-12-11 15:33:35 +0100488 llist_for_each_entry_safe(req, req2, &bts->paging.pending_requests, entry) {
489 if (msc && req->msc != msc)
490 continue;
491 /* now give up the data structure */
492 LOGP(DPAG, LOGL_DEBUG, "Stop paging %s on bts %d (flush).\n", req->bsub->imsi, bts->nr);
493 paging_remove_request(&bts->paging, req);
494 }
495}
496
497/*! Flush all paging requests issued by \a msc on any BTS in \a net */
498void paging_flush_network(struct gsm_network *net, struct bsc_msc_data *msc)
499{
500 struct gsm_bts *bts;
501
502 llist_for_each_entry(bts, &net->bts_list, list)
503 paging_flush_bts(bts, msc);
504}