blob: dd221500560eab662e9d7198a49fa7f44d3bc2d4 [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 Weltea8379772009-06-20 22:36:41 +020043#include <openbsc/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
49#define PAGING_TIMEOUT 1, 75000
50#define MAX_PAGING_REQUEST 750
51
Harald Weltea8379772009-06-20 22:36:41 +020052static void *tall_paging_ctx;
53
Harald Welte59b04682009-06-10 05:40:52 +080054static unsigned int calculate_group(struct gsm_bts *bts, struct gsm_subscriber *subscr)
55{
56 int ccch_conf;
57 int bs_cc_chans;
58 int blocks;
59 unsigned int group;
60
61 ccch_conf = bts->chan_desc.ccch_conf;
62 bs_cc_chans = rsl_ccch_conf_to_bs_cc_chans(ccch_conf);
63 /* code word + 2, as 2 channels equals 0x0 */
64 blocks = rsl_number_of_paging_subchannels(bts);
65 group = get_paging_group(str_to_imsi(subscr->imsi),
66 bs_cc_chans, blocks);
67 return group;
68}
69
70/*
71 * Kill one paging request update the internal list...
72 */
73static void paging_remove_request(struct gsm_bts_paging_state *paging_bts,
74 struct gsm_paging_request *to_be_deleted)
75{
76 /* Update the last_request if that is necessary */
77 if (to_be_deleted == paging_bts->last_request) {
78 paging_bts->last_request =
79 (struct gsm_paging_request *)paging_bts->last_request->entry.next;
80 if (&to_be_deleted->entry == &paging_bts->pending_requests)
81 paging_bts->last_request = NULL;
82 }
83
84 bsc_del_timer(&to_be_deleted->T3113);
85 llist_del(&to_be_deleted->entry);
86 subscr_put(to_be_deleted->subscr);
Harald Weltea8379772009-06-20 22:36:41 +020087 talloc_free(to_be_deleted);
Harald Welte59b04682009-06-10 05:40:52 +080088}
89
90static void page_ms(struct gsm_paging_request *request)
91{
92 u_int8_t mi[128];
93 unsigned long int tmsi;
94 unsigned int mi_len;
95 unsigned int page_group;
96
97 DEBUGP(DPAG, "Going to send paging commands: '%s'\n",
98 request->subscr->imsi);
99
100 page_group = calculate_group(request->bts, request->subscr);
101 tmsi = strtoul(request->subscr->tmsi, NULL, 10);
102 mi_len = generate_mid_from_tmsi(mi, tmsi);
103 rsl_paging_cmd(request->bts, page_group, mi_len, mi,
104 request->chan_type);
105}
106
107static void paging_move_to_next(struct gsm_bts_paging_state *paging_bts)
108{
109 paging_bts->last_request =
110 (struct gsm_paging_request *)paging_bts->last_request->entry.next;
111 if (&paging_bts->last_request->entry == &paging_bts->pending_requests)
112 paging_bts->last_request = NULL;
113}
114
115/*
116 * This is kicked by the periodic PAGING LOAD Indicator
117 * coming from abis_rsl.c
118 *
119 * We attempt to iterate once over the list of items but
120 * only upto available_slots.
121 */
122static void paging_handle_pending_requests(struct gsm_bts_paging_state *paging_bts)
123{
124 struct gsm_paging_request *initial_request = NULL;
125 struct gsm_paging_request *current_request = NULL;
126
127 /*
128 * Determine if the pending_requests list is empty and
129 * return then.
130 */
131 if (llist_empty(&paging_bts->pending_requests)) {
132 paging_bts->last_request = NULL;
133 /* since the list is empty, no need to reschedule the timer */
134 return;
135 }
136
137 if (!paging_bts->last_request)
138 paging_bts->last_request =
139 (struct gsm_paging_request *)paging_bts->pending_requests.next;
140
141 assert(paging_bts->last_request);
142 initial_request = paging_bts->last_request;
143 current_request = initial_request;
144
145 do {
146 /* handle the paging request now */
147 page_ms(current_request);
148 paging_bts->available_slots--;
149
150 /*
151 * move to the next item. We might wrap around
152 * this means last_request will be NULL and we just
153 * call paging_page_to_next again. It it guranteed
154 * that the list is not empty.
155 */
156 paging_move_to_next(paging_bts);
157 if (!paging_bts->last_request)
158 paging_bts->last_request =
159 (struct gsm_paging_request *)paging_bts->pending_requests.next;
160 current_request = paging_bts->last_request;
161 } while (paging_bts->available_slots > 0
162 && initial_request != current_request);
163
164 bsc_schedule_timer(&paging_bts->work_timer, 1, 0);
165}
166
167static void paging_worker(void *data)
168{
169 struct gsm_bts_paging_state *paging_bts = data;
170
171 paging_handle_pending_requests(paging_bts);
172}
173
174void paging_init(struct gsm_bts *bts)
175{
176 bts->paging.bts = bts;
177 INIT_LLIST_HEAD(&bts->paging.pending_requests);
178 bts->paging.work_timer.cb = paging_worker;
179 bts->paging.work_timer.data = &bts->paging;
180
181 /* Large number, until we get a proper message */
182 bts->paging.available_slots = 100;
183}
184
185static int paging_pending_request(struct gsm_bts_paging_state *bts,
186 struct gsm_subscriber *subscr) {
187 struct gsm_paging_request *req;
188
189 llist_for_each_entry(req, &bts->pending_requests, entry) {
190 if (subscr == req->subscr)
191 return 1;
192 }
193
194 return 0;
195}
196
197static void paging_T3113_expired(void *data)
198{
199 struct gsm_paging_request *req = (struct gsm_paging_request *)data;
200 struct paging_signal_data sig_data;
201
202 DEBUGP(DPAG, "T3113 expired for request %p (%s)\n",
203 req, req->subscr->imsi);
204
205 sig_data.subscr = req->subscr,
206 sig_data.bts = req->bts,
207 sig_data.lchan = NULL,
208
209 dispatch_signal(SS_PAGING, S_PAGING_COMPLETED, &sig_data);
210 if (req->cbfn)
211 req->cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_EXPIRED, NULL, NULL,
212 req->cbfn_param);
213 paging_remove_request(&req->bts->paging, req);
214}
215
Holger Hans Peter Freyther13d472d2009-06-10 02:45:42 +0200216static void _paging_request(struct gsm_bts *bts, struct gsm_subscriber *subscr,
217 int type, gsm_cbfn *cbfn, void *data)
Harald Welte59b04682009-06-10 05:40:52 +0800218{
219 struct gsm_bts_paging_state *bts_entry = &bts->paging;
220 struct gsm_paging_request *req;
221
222 if (paging_pending_request(bts_entry, subscr)) {
223 DEBUGP(DPAG, "Paging request already pending\n");
224 return;
225 }
226
Harald Weltecd97c212009-08-08 15:08:19 +0200227 DEBUGP(DPAG, "Start paging of subscriber %llu on bts %d.\n",
228 subscr->id, bts->nr);
Harald Welte857e00d2009-06-26 20:25:23 +0200229 req = talloc_zero(tall_paging_ctx, struct gsm_paging_request);
Harald Welte59b04682009-06-10 05:40:52 +0800230 req->subscr = subscr_get(subscr);
231 req->bts = bts;
232 req->chan_type = type;
233 req->cbfn = cbfn;
234 req->cbfn_param = data;
235 req->T3113.cb = paging_T3113_expired;
236 req->T3113.data = req;
237 bsc_schedule_timer(&req->T3113, T3113_VALUE);
238 llist_add_tail(&req->entry, &bts_entry->pending_requests);
239
240 if (!bsc_timer_pending(&bts_entry->work_timer))
241 bsc_schedule_timer(&bts_entry->work_timer, 1, 0);
242}
243
Holger Freyther93795ac2009-03-31 04:35:19 +0200244void paging_request(struct gsm_network *network, struct gsm_subscriber *subscr,
Holger Hans Peter Freyther13d472d2009-06-10 02:45:42 +0200245 int type, gsm_cbfn *cbfn, void *data)
246{
247 struct gsm_bts *bts = NULL;
248
Harald Welte2a3a81b2009-08-01 19:31:47 +0200249 /* start paging subscriber on all BTS within Location Area */
Holger Hans Peter Freyther13d472d2009-06-10 02:45:42 +0200250 do {
Holger Freyther93795ac2009-03-31 04:35:19 +0200251 bts = gsm_bts_by_lac(network, subscr->lac, bts);
Holger Hans Peter Freyther13d472d2009-06-10 02:45:42 +0200252 if (!bts)
253 break;
254
255 /* Trigger paging */
Harald Weltecd97c212009-08-08 15:08:19 +0200256 _paging_request(bts, subscr, type, cbfn, data);
Holger Hans Peter Freyther13d472d2009-06-10 02:45:42 +0200257 } while (1);
258}
259
260
Harald Welte59b04682009-06-10 05:40:52 +0800261/* we consciously ignore the type of the request here */
Holger Hans Peter Freyther13d472d2009-06-10 02:45:42 +0200262static void _paging_request_stop(struct gsm_bts *bts, struct gsm_subscriber *subscr,
263 struct gsm_lchan *lchan)
Harald Welte59b04682009-06-10 05:40:52 +0800264{
265 struct gsm_bts_paging_state *bts_entry = &bts->paging;
266 struct gsm_paging_request *req, *req2;
267
268 llist_for_each_entry_safe(req, req2, &bts_entry->pending_requests,
269 entry) {
270 if (req->subscr == subscr) {
Harald Welte03740842009-06-10 23:11:52 +0800271 if (lchan && req->cbfn) {
272 DEBUGP(DPAG, "Stop paging on bts %d, calling cbfn.\n", bts->nr);
Harald Welte59b04682009-06-10 05:40:52 +0800273 req->cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_SUCCEEDED,
274 NULL, lchan, req->cbfn_param);
Harald Welte03740842009-06-10 23:11:52 +0800275 } else
276 DEBUGP(DPAG, "Stop paging on bts %d silently.\n", bts->nr);
Harald Welte59b04682009-06-10 05:40:52 +0800277 paging_remove_request(&bts->paging, req);
278 break;
279 }
280 }
281}
282
Holger Hans Peter Freyther13d472d2009-06-10 02:45:42 +0200283/* Stop paging on all other bts' */
284void paging_request_stop(struct gsm_bts *_bts, struct gsm_subscriber *subscr,
285 struct gsm_lchan *lchan)
286{
287 struct gsm_bts *bts = NULL;
288
Holger Hans Peter Freytherc51e3332009-06-10 11:46:58 +0200289 _paging_request_stop(_bts, subscr, lchan);
290
Holger Hans Peter Freyther13d472d2009-06-10 02:45:42 +0200291 do {
292 /*
293 * FIXME: Don't use the lac of the subscriber...
294 * as it might have magically changed the lac.. use the
295 * location area of the _bts as reconfiguration of the
296 * network is probably happening less often.
297 */
298 bts = gsm_bts_by_lac(_bts->network, subscr->lac, bts);
299 if (!bts)
300 break;
301
302 /* Stop paging */
Holger Hans Peter Freytherc51e3332009-06-10 11:46:58 +0200303 if (bts != _bts)
304 _paging_request_stop(bts, subscr, NULL);
Holger Hans Peter Freyther13d472d2009-06-10 02:45:42 +0200305 } while (1);
306}
307
Harald Welte59b04682009-06-10 05:40:52 +0800308void paging_update_buffer_space(struct gsm_bts *bts, u_int16_t free_slots)
309{
310 bts->paging.available_slots = free_slots;
311}
Harald Welte932e20d2009-07-28 00:41:45 +0200312
313static __attribute__((constructor)) void on_dso_load_paging(void)
314{
315 tall_paging_ctx = talloc_named_const(NULL, 1, "paging_request");
316}