blob: ece71ca7fa795b9c711ef02a2198b40436d6228c [file] [log] [blame]
Harald Welte59b04682009-06-10 05:40:52 +08001/* Paging helper and manager.... */
2/* (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
3 * All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
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
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 */
20
21/*
22 * Relevant specs:
23 * 12.21:
24 * - 9.4.12 for CCCH Local Threshold
25 *
26 * 05.58:
27 * - 8.5.2 CCCH Load indication
28 * - 9.3.15 Paging Load
29 *
30 * Approach:
31 * - Send paging command to subscriber
32 * - On Channel Request we will remember the reason
33 * - After the ACK we will request the identity
34 * - Then we will send assign the gsm_subscriber and
35 * - and call a callback
36 */
37
38#include <stdio.h>
39#include <stdlib.h>
40#include <assert.h>
41
42#include <openbsc/paging.h>
Harald Weltef4625b12010-02-20 16:24:02 +010043#include <osmocore/talloc.h>
Harald Welte59b04682009-06-10 05:40:52 +080044#include <openbsc/debug.h>
45#include <openbsc/signal.h>
46#include <openbsc/abis_rsl.h>
47#include <openbsc/gsm_data.h>
48
Harald Welte (local)8751ee92009-08-15 02:30:58 +020049void *tall_paging_ctx;
Harald Weltea8379772009-06-20 22:36:41 +020050
Harald Welte59b04682009-06-10 05:40:52 +080051static unsigned int calculate_group(struct gsm_bts *bts, struct gsm_subscriber *subscr)
52{
53 int ccch_conf;
54 int bs_cc_chans;
55 int blocks;
56 unsigned int group;
57
Harald Weltea54a2bb2009-12-01 18:04:30 +053058 ccch_conf = bts->si_common.chan_desc.ccch_conf;
Harald Welte59b04682009-06-10 05:40:52 +080059 bs_cc_chans = rsl_ccch_conf_to_bs_cc_chans(ccch_conf);
60 /* code word + 2, as 2 channels equals 0x0 */
61 blocks = rsl_number_of_paging_subchannels(bts);
62 group = get_paging_group(str_to_imsi(subscr->imsi),
63 bs_cc_chans, blocks);
64 return group;
65}
66
67/*
68 * Kill one paging request update the internal list...
69 */
70static void paging_remove_request(struct gsm_bts_paging_state *paging_bts,
71 struct gsm_paging_request *to_be_deleted)
72{
Harald Welte59b04682009-06-10 05:40:52 +080073 bsc_del_timer(&to_be_deleted->T3113);
74 llist_del(&to_be_deleted->entry);
75 subscr_put(to_be_deleted->subscr);
Harald Weltea8379772009-06-20 22:36:41 +020076 talloc_free(to_be_deleted);
Harald Welte59b04682009-06-10 05:40:52 +080077}
78
79static void page_ms(struct gsm_paging_request *request)
80{
81 u_int8_t mi[128];
Harald Welte59b04682009-06-10 05:40:52 +080082 unsigned int mi_len;
83 unsigned int page_group;
84
Holger Hans Peter Freyther9b7911e2009-08-21 14:55:57 +020085 DEBUGP(DPAG, "Going to send paging commands: imsi: '%s' tmsi: '0x%x'\n",
86 request->subscr->imsi, request->subscr->tmsi);
87
88 if (request->subscr->tmsi == GSM_RESERVED_TMSI)
89 mi_len = gsm48_generate_mid_from_imsi(mi, request->subscr->imsi);
90 else
91 mi_len = gsm48_generate_mid_from_tmsi(mi, request->subscr->tmsi);
Harald Welte59b04682009-06-10 05:40:52 +080092
93 page_group = calculate_group(request->bts, request->subscr);
Harald Welte59b04682009-06-10 05:40:52 +080094 rsl_paging_cmd(request->bts, page_group, mi_len, mi,
95 request->chan_type);
96}
97
Harald Welte59b04682009-06-10 05:40:52 +080098/*
99 * This is kicked by the periodic PAGING LOAD Indicator
100 * coming from abis_rsl.c
101 *
102 * We attempt to iterate once over the list of items but
103 * only upto available_slots.
104 */
105static void paging_handle_pending_requests(struct gsm_bts_paging_state *paging_bts)
106{
107 struct gsm_paging_request *initial_request = NULL;
108 struct gsm_paging_request *current_request = NULL;
109
110 /*
111 * Determine if the pending_requests list is empty and
112 * return then.
113 */
114 if (llist_empty(&paging_bts->pending_requests)) {
Harald Welte59b04682009-06-10 05:40:52 +0800115 /* since the list is empty, no need to reschedule the timer */
116 return;
117 }
118
Holger Hans Peter Freyther5fd943c2010-04-09 18:38:06 +0200119 /*
120 * In case the BTS does not provide us with load indication just fill
121 * up our slots for this round. We should be able to page 20 subscribers
122 * every two seconds. So we will just give the BTS some extra credit.
123 * We will have to see how often we run out of this credit, so we might
124 * need a low watermark and then add credit or give 20 every run when
125 * the bts sets an option for that.
126 */
127 if (paging_bts->available_slots == 0) {
128 LOGP(DPAG, LOGL_NOTICE, "No slots available on bts nr %d\n",
129 paging_bts->bts->nr);
130 paging_bts->available_slots = 20;
131 }
132
Holger Hans Peter Freyther5ded1912010-04-09 21:53:35 +0200133 initial_request = llist_entry(paging_bts->pending_requests.next,
134 struct gsm_paging_request, entry);
Harald Welte59b04682009-06-10 05:40:52 +0800135 current_request = initial_request;
136
137 do {
138 /* handle the paging request now */
139 page_ms(current_request);
140 paging_bts->available_slots--;
141
Holger Hans Peter Freyther5ded1912010-04-09 21:53:35 +0200142 /* take the current and add it to the back */
143 llist_del(&current_request->entry);
144 llist_add_tail(&current_request->entry, &paging_bts->pending_requests);
145
146 /* take the next request */
147 current_request = llist_entry(paging_bts->pending_requests.next,
148 struct gsm_paging_request, entry);
Harald Welte59b04682009-06-10 05:40:52 +0800149 } while (paging_bts->available_slots > 0
150 && initial_request != current_request);
151
Holger Hans Peter Freytherc6b5f0e2010-01-27 09:10:05 +0100152 bsc_schedule_timer(&paging_bts->work_timer, 2, 0);
Harald Welte59b04682009-06-10 05:40:52 +0800153}
154
155static void paging_worker(void *data)
156{
157 struct gsm_bts_paging_state *paging_bts = data;
158
159 paging_handle_pending_requests(paging_bts);
160}
161
162void paging_init(struct gsm_bts *bts)
163{
164 bts->paging.bts = bts;
165 INIT_LLIST_HEAD(&bts->paging.pending_requests);
166 bts->paging.work_timer.cb = paging_worker;
167 bts->paging.work_timer.data = &bts->paging;
168
169 /* Large number, until we get a proper message */
170 bts->paging.available_slots = 100;
171}
172
173static int paging_pending_request(struct gsm_bts_paging_state *bts,
174 struct gsm_subscriber *subscr) {
175 struct gsm_paging_request *req;
176
177 llist_for_each_entry(req, &bts->pending_requests, entry) {
178 if (subscr == req->subscr)
179 return 1;
180 }
181
182 return 0;
183}
184
185static void paging_T3113_expired(void *data)
186{
187 struct gsm_paging_request *req = (struct gsm_paging_request *)data;
188 struct paging_signal_data sig_data;
Holger Hans Peter Freyther353e9b62009-11-17 10:16:46 +0100189 void *cbfn_param;
190 gsm_cbfn *cbfn;
Harald Welte59b04682009-06-10 05:40:52 +0800191
192 DEBUGP(DPAG, "T3113 expired for request %p (%s)\n",
193 req, req->subscr->imsi);
194
Holger Hans Peter Freythera3a16662009-08-20 06:15:21 +0200195 sig_data.subscr = req->subscr;
196 sig_data.bts = req->bts;
197 sig_data.lchan = NULL;
Harald Welte59b04682009-06-10 05:40:52 +0800198
Holger Hans Peter Freyther353e9b62009-11-17 10:16:46 +0100199 /* must be destroyed before calling cbfn, to prevent double free */
Holger Hans Peter Freytherfad51da2010-04-08 16:48:46 +0200200 counter_inc(req->bts->network->stats.paging.expired);
Holger Hans Peter Freyther353e9b62009-11-17 10:16:46 +0100201 cbfn_param = req->cbfn_param;
202 cbfn = req->cbfn;
Harald Welte59b04682009-06-10 05:40:52 +0800203 paging_remove_request(&req->bts->paging, req);
Holger Hans Peter Freyther353e9b62009-11-17 10:16:46 +0100204
Harald Welte3edc5a92009-12-22 00:41:05 +0100205
Sylvain Munaut4ff936f2009-12-19 12:38:10 +0100206 dispatch_signal(SS_PAGING, S_PAGING_EXPIRED, &sig_data);
Holger Hans Peter Freyther353e9b62009-11-17 10:16:46 +0100207 if (cbfn)
208 cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_EXPIRED, NULL, NULL,
209 cbfn_param);
Harald Welte59b04682009-06-10 05:40:52 +0800210}
211
Harald Welted363c952009-08-15 03:16:17 +0200212static int _paging_request(struct gsm_bts *bts, struct gsm_subscriber *subscr,
Holger Hans Peter Freyther13d472d2009-06-10 02:45:42 +0200213 int type, gsm_cbfn *cbfn, void *data)
Harald Welte59b04682009-06-10 05:40:52 +0800214{
215 struct gsm_bts_paging_state *bts_entry = &bts->paging;
216 struct gsm_paging_request *req;
217
218 if (paging_pending_request(bts_entry, subscr)) {
219 DEBUGP(DPAG, "Paging request already pending\n");
Harald Welted363c952009-08-15 03:16:17 +0200220 return -EEXIST;
Harald Welte59b04682009-06-10 05:40:52 +0800221 }
222
Harald Weltecd97c212009-08-08 15:08:19 +0200223 DEBUGP(DPAG, "Start paging of subscriber %llu on bts %d.\n",
224 subscr->id, bts->nr);
Harald Welte857e00d2009-06-26 20:25:23 +0200225 req = talloc_zero(tall_paging_ctx, struct gsm_paging_request);
Harald Welte59b04682009-06-10 05:40:52 +0800226 req->subscr = subscr_get(subscr);
227 req->bts = bts;
228 req->chan_type = type;
229 req->cbfn = cbfn;
230 req->cbfn_param = data;
231 req->T3113.cb = paging_T3113_expired;
232 req->T3113.data = req;
Holger Hans Peter Freyther4b9042a2009-11-21 21:48:53 +0100233 bsc_schedule_timer(&req->T3113, bts->network->T3113, 0);
Harald Welte59b04682009-06-10 05:40:52 +0800234 llist_add_tail(&req->entry, &bts_entry->pending_requests);
235
236 if (!bsc_timer_pending(&bts_entry->work_timer))
Holger Hans Peter Freytherc6b5f0e2010-01-27 09:10:05 +0100237 bsc_schedule_timer(&bts_entry->work_timer, 2, 0);
Harald Welted363c952009-08-15 03:16:17 +0200238
239 return 0;
Harald Welte59b04682009-06-10 05:40:52 +0800240}
241
Harald Welted363c952009-08-15 03:16:17 +0200242int paging_request(struct gsm_network *network, struct gsm_subscriber *subscr,
243 int type, gsm_cbfn *cbfn, void *data)
Holger Hans Peter Freyther13d472d2009-06-10 02:45:42 +0200244{
245 struct gsm_bts *bts = NULL;
Harald Welte (local)dec08ee2009-08-15 11:25:45 +0200246 int num_pages = 0;
Holger Hans Peter Freyther13d472d2009-06-10 02:45:42 +0200247
Harald Weltebdbb7442009-12-22 19:07:32 +0100248 counter_inc(network->stats.paging.attempted);
Harald Welte3edc5a92009-12-22 00:41:05 +0100249
Harald Welte2a3a81b2009-08-01 19:31:47 +0200250 /* start paging subscriber on all BTS within Location Area */
Holger Hans Peter Freyther13d472d2009-06-10 02:45:42 +0200251 do {
Harald Welte (local)dec08ee2009-08-15 11:25:45 +0200252 int rc;
253
Holger Freyther93795ac2009-03-31 04:35:19 +0200254 bts = gsm_bts_by_lac(network, subscr->lac, bts);
Holger Hans Peter Freyther13d472d2009-06-10 02:45:42 +0200255 if (!bts)
256 break;
Harald Welte (local)43db2bf2009-12-28 16:36:28 +0100257
258 /* skip all currently inactive TRX */
259 if (!trx_is_usable(bts->c0))
260 continue;
261
Harald Welte (local)dec08ee2009-08-15 11:25:45 +0200262 num_pages++;
Holger Hans Peter Freyther13d472d2009-06-10 02:45:42 +0200263
Harald Welted363c952009-08-15 03:16:17 +0200264 /* Trigger paging, pass any error to caller */
265 rc = _paging_request(bts, subscr, type, cbfn, data);
266 if (rc < 0)
267 return rc;
Holger Hans Peter Freyther13d472d2009-06-10 02:45:42 +0200268 } while (1);
Harald Welted363c952009-08-15 03:16:17 +0200269
Harald Welte3edc5a92009-12-22 00:41:05 +0100270 if (num_pages == 0)
Harald Weltebdbb7442009-12-22 19:07:32 +0100271 counter_inc(network->stats.paging.detached);
Harald Welte3edc5a92009-12-22 00:41:05 +0100272
Harald Welte (local)dec08ee2009-08-15 11:25:45 +0200273 return num_pages;
Holger Hans Peter Freyther13d472d2009-06-10 02:45:42 +0200274}
275
276
Harald Welte59b04682009-06-10 05:40:52 +0800277/* we consciously ignore the type of the request here */
Holger Hans Peter Freyther13d472d2009-06-10 02:45:42 +0200278static void _paging_request_stop(struct gsm_bts *bts, struct gsm_subscriber *subscr,
279 struct gsm_lchan *lchan)
Harald Welte59b04682009-06-10 05:40:52 +0800280{
281 struct gsm_bts_paging_state *bts_entry = &bts->paging;
282 struct gsm_paging_request *req, *req2;
283
284 llist_for_each_entry_safe(req, req2, &bts_entry->pending_requests,
285 entry) {
286 if (req->subscr == subscr) {
Harald Welte03740842009-06-10 23:11:52 +0800287 if (lchan && req->cbfn) {
288 DEBUGP(DPAG, "Stop paging on bts %d, calling cbfn.\n", bts->nr);
Harald Welte59b04682009-06-10 05:40:52 +0800289 req->cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_SUCCEEDED,
290 NULL, lchan, req->cbfn_param);
Harald Welte03740842009-06-10 23:11:52 +0800291 } else
292 DEBUGP(DPAG, "Stop paging on bts %d silently.\n", bts->nr);
Harald Welte59b04682009-06-10 05:40:52 +0800293 paging_remove_request(&bts->paging, req);
294 break;
295 }
296 }
297}
298
Holger Hans Peter Freyther13d472d2009-06-10 02:45:42 +0200299/* Stop paging on all other bts' */
300void paging_request_stop(struct gsm_bts *_bts, struct gsm_subscriber *subscr,
301 struct gsm_lchan *lchan)
302{
303 struct gsm_bts *bts = NULL;
304
Holger Hans Peter Freytherb78507a2009-08-21 05:43:44 +0200305 if (_bts)
306 _paging_request_stop(_bts, subscr, lchan);
Holger Hans Peter Freytherc51e3332009-06-10 11:46:58 +0200307
Holger Hans Peter Freyther13d472d2009-06-10 02:45:42 +0200308 do {
309 /*
310 * FIXME: Don't use the lac of the subscriber...
311 * as it might have magically changed the lac.. use the
312 * location area of the _bts as reconfiguration of the
313 * network is probably happening less often.
314 */
Holger Hans Peter Freytherb78507a2009-08-21 05:43:44 +0200315 bts = gsm_bts_by_lac(subscr->net, subscr->lac, bts);
Holger Hans Peter Freyther13d472d2009-06-10 02:45:42 +0200316 if (!bts)
317 break;
318
319 /* Stop paging */
Holger Hans Peter Freytherc51e3332009-06-10 11:46:58 +0200320 if (bts != _bts)
321 _paging_request_stop(bts, subscr, NULL);
Holger Hans Peter Freyther13d472d2009-06-10 02:45:42 +0200322 } while (1);
323}
324
Harald Welte59b04682009-06-10 05:40:52 +0800325void paging_update_buffer_space(struct gsm_bts *bts, u_int16_t free_slots)
326{
327 bts->paging.available_slots = free_slots;
328}