blob: 89daf1f0fcdc6cb1daa690be43a046691badc636 [file] [log] [blame]
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +01001/* SMS queue to continously attempt to deliver SMS */
2/*
3 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 */
21
22/**
23 * The difficulty of such a queue is to send a lot of SMS without
24 * overloading the paging subsystem and the database and other users
25 * of the MSC. To make the best use we would need to know the number
26 * of pending paging requests, then throttle the number of SMS we
27 * want to send and such.
28 * We will start with a very simple SMS Queue and then try to speed
29 * things up by collecting data from other parts of the system.
30 */
31
32#include <openbsc/sms_queue.h>
33#include <openbsc/chan_alloc.h>
34#include <openbsc/db.h>
35#include <openbsc/debug.h>
36#include <openbsc/gsm_data.h>
37#include <openbsc/gsm_04_11.h>
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +010038#include <openbsc/gsm_subscriber.h>
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +010039#include <openbsc/signal.h>
40
41#include <osmocore/talloc.h>
42
Holger Hans Peter Freyther81c0e252010-12-25 14:08:00 +010043#include <osmocom/vty/vty.h>
44
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +010045/*
46 * One pending SMS that we wait for.
47 */
48struct gsm_sms_pending {
49 struct llist_head entry;
50
51 struct gsm_subscriber *subscr;
52 unsigned long long sms_id;
53 int failed_attempts;
54 int resend;
55};
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +010056
57struct gsm_sms_queue {
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +010058 struct timer_list resend_pending;
59 struct timer_list push_queue;
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +010060 struct gsm_network *network;
Holger Hans Peter Freyther7e59c832010-12-25 14:46:54 +010061 int max_fail;
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +010062 int max_pending;
63 int pending;
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +010064
65 struct llist_head pending_sms;
66 unsigned long long last_subscr_id;
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +010067};
68
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +010069static int sms_subscr_cb(unsigned int, unsigned int, void *, void *);
70static int sms_sms_cb(unsigned int, unsigned int, void *, void *);
71
72static struct gsm_sms_pending *sms_find_pending(struct gsm_sms_queue *smsq,
73 struct gsm_sms *sms)
74{
75 struct gsm_sms_pending *pending;
76
77 llist_for_each_entry(pending, &smsq->pending_sms, entry) {
78 if (pending->sms_id == sms->id)
79 return pending;
80 }
81
82 return NULL;
83}
84
85static int sms_is_in_pending(struct gsm_sms_queue *smsq, struct gsm_sms *sms)
86{
87 return sms_find_pending(smsq, sms) != NULL;
88}
89
90static int sms_subscriber_is_pending(struct gsm_sms_queue *smsq,
91 struct gsm_subscriber *subscr)
92{
93 struct gsm_sms_pending *pending;
94
95 llist_for_each_entry(pending, &smsq->pending_sms, entry) {
96 if (pending->subscr == subscr)
97 return 1;
98 }
99
100 return 0;
101}
102
103static struct gsm_sms_pending *sms_pending_from(struct gsm_sms_queue *smsq,
104 struct gsm_sms *sms)
105{
106 struct gsm_sms_pending *pending;
107
108 pending = talloc_zero(smsq, struct gsm_sms_pending);
109 if (!pending)
110 return NULL;
111
112 pending->subscr = subscr_get(sms->receiver);
113 pending->sms_id = sms->id;
114 return pending;
115}
116
117static void sms_pending_free(struct gsm_sms_pending *pending)
118{
119 subscr_put(pending->subscr);
120 llist_del(&pending->entry);
121 talloc_free(pending);
122}
123
124static void sms_pending_resend(struct gsm_sms_pending *pending)
125{
126 struct gsm_sms_queue *smsq;
127 LOGP(DSMS, LOGL_DEBUG,
128 "Scheduling resend of SMS %llu.\n", pending->sms_id);
129
130 pending->resend = 1;
131
132 smsq = pending->subscr->net->sms_queue;
133 if (bsc_timer_pending(&smsq->resend_pending))
134 return;
135
136 bsc_schedule_timer(&smsq->resend_pending, 1, 0);
137}
138
139static void sms_pending_failed(struct gsm_sms_pending *pending, int paging_error)
140{
141 struct gsm_sms_queue *smsq;
142
143 LOGP(DSMS, LOGL_NOTICE, "Sending SMS %llu failed %d times.\n",
144 pending->sms_id, pending->failed_attempts);
145
146 smsq = pending->subscr->net->sms_queue;
Holger Hans Peter Freyther7e59c832010-12-25 14:46:54 +0100147 if (++pending->failed_attempts < smsq->max_fail)
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100148 return sms_pending_resend(pending);
149
150 if (paging_error) {
151 LOGP(DSMS, LOGL_NOTICE,
152 "Subscriber %llu is not reachable. Setting LAC=0.\n", pending->subscr->id);
153 pending->subscr->lac = GSM_LAC_RESERVED_DETACHED;
154 db_sync_subscriber(pending->subscr);
155 }
156
157 sms_pending_free(pending);
158 smsq->pending -= 1;
159 sms_queue_trigger(smsq);
160}
161
162/*
163 * Resend all SMS that are scheduled for a resend. This is done to
164 * avoid an immediate failure.
165 */
166static void sms_resend_pending(void *_data)
167{
168 struct gsm_sms_pending *pending, *tmp;
169 struct gsm_sms_queue *smsq = _data;
170
171 llist_for_each_entry_safe(pending, tmp, &smsq->pending_sms, entry) {
172 struct gsm_sms *sms;
173 if (!pending->resend)
174 continue;
175
176 sms = db_sms_get(smsq->network, pending->sms_id);
177
178 /* the sms is gone? Move to the next */
179 if (!sms) {
180 sms_pending_free(pending);
181 smsq->pending -= 1;
182 sms_queue_trigger(smsq);
183 } else {
184 pending->resend = 0;
185 gsm411_send_sms_subscr(sms->receiver, sms);
186 }
187 }
188}
189
190/**
191 * I will submit up to max_pending - pending SMS to the
192 * subsystem.
193 */
194static void sms_submit_pending(void *_data)
195{
196 struct gsm_sms_queue *smsq = _data;
197 int attempts = smsq->max_pending - smsq->pending;
198 int i;
199
200 LOGP(DSMS, LOGL_NOTICE, "Attempting to send %d SMS\n", attempts);
201
202 for (i = 0; i < attempts; ++i) {
203 struct gsm_sms_pending *pending;
204 struct gsm_sms *sms;
205
206 sms = db_sms_get_unsent_by_subscr(smsq->network, smsq->last_subscr_id, 10);
207
208 /* handle wrapping around */
209 if (!sms) {
210 smsq->last_subscr_id = 0;
211 sms = db_sms_get_unsent_by_subscr(smsq->network,
212 smsq->last_subscr_id, 10);
213 }
214
215 if (!sms)
216 break;
217
218 /* no need to send a pending sms */
219 if (sms_is_in_pending(smsq, sms)) {
220 LOGP(DSMS, LOGL_DEBUG,
221 "SMSqueue with pending sms: %llu\n. Skipping", sms->id);
222 sms_free(sms);
223 continue;
224 }
225
226 /* no need to send a SMS with the same receiver */
227 if (sms_subscriber_is_pending(smsq, sms->receiver)) {
228 LOGP(DSMS, LOGL_DEBUG,
229 "SMSqueue with pending sub: %llu. Skipping\n", sms->receiver->id);
230 sms_free(sms);
231 continue;
232 }
233
234 pending = sms_pending_from(smsq, sms);
235 if (!pending) {
236 LOGP(DSMS, LOGL_ERROR,
237 "Failed to create pending SMS entry.\n");
238 sms_free(sms);
239 continue;
240 }
241
242 smsq->last_subscr_id = sms->receiver->id + 1;
243 smsq->pending += 1;
244 llist_add(&pending->entry, &smsq->pending_sms);
245 gsm411_send_sms_subscr(sms->receiver, sms);
246 }
247}
248
249/*
250 * Kick off the queue again.
251 */
252int sms_queue_trigger(struct gsm_sms_queue *smsq)
253{
254 if (bsc_timer_pending(&smsq->push_queue))
255 return 0;
256
257 bsc_schedule_timer(&smsq->push_queue, 1, 0);
258 return 0;
259}
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100260
261int sms_queue_start(struct gsm_network *network, int max_pending)
262{
263 struct gsm_sms_queue *sms = talloc_zero(network, struct gsm_sms_queue);
264 if (!sms) {
265 LOGP(DMSC, LOGL_ERROR, "Failed to create the SMS queue.\n");
266 return -1;
267 }
268
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100269 register_signal_handler(SS_SUBSCR, sms_subscr_cb, network);
270 register_signal_handler(SS_SMS, sms_sms_cb, network);
271
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100272 network->sms_queue = sms;
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100273 INIT_LLIST_HEAD(&sms->pending_sms);
Holger Hans Peter Freyther7e59c832010-12-25 14:46:54 +0100274 sms->max_fail = 3;
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100275 sms->network = network;
276 sms->max_pending = max_pending;
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100277 sms->push_queue.data = sms;
278 sms->push_queue.cb = sms_submit_pending;
279 sms->resend_pending.data = sms;
280 sms->resend_pending.cb = sms_resend_pending;
281
282 sms_submit_pending(sms);
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100283
284 return 0;
285}
286
287static int sub_ready_for_sm(struct gsm_subscriber *subscr)
288{
289 struct gsm_subscriber_connection *conn;
290 struct gsm_sms *sms;
291
292 /* A subscriber has attached. Check if there are
293 * any pending SMS for him to be delivered */
294 conn = connection_for_subscr(subscr);
295 if (!conn)
296 return -1;
297 sms = db_sms_get_unsent_for_subscr(subscr);
298 if (!sms)
299 return -1;
300 gsm411_send_sms(conn, sms);
301 return 0;
302}
303
304static int sms_subscr_cb(unsigned int subsys, unsigned int signal,
305 void *handler_data, void *signal_data)
306{
307 struct gsm_subscriber *subscr = signal_data;
308
309 if (signal != S_SUBSCR_ATTACHED)
310 return 0;
311
312 /* this is readyForSM */
313 return sub_ready_for_sm(subscr);
314}
315
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100316static int sms_sms_cb(unsigned int subsys, unsigned int signal,
317 void *handler_data, void *signal_data)
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100318{
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100319 struct gsm_network *network = handler_data;
320 struct sms_signal_data *sig_sms = signal_data;
321 struct gsm_sms_pending *pending;
322
323 /* We got a new SMS and maybe should launch the queue again. */
324 if (signal == S_SMS_SUBMITTED || signal == S_SMS_SMMA) {
325 sms_queue_trigger(network->sms_queue);
326 return 0;
327 }
328
329 if (!sig_sms->sms)
330 return -1;
331
332
333 /*
334 * Find the entry of our queue. The SMS subsystem will submit
335 * sms that are not in our control as we just have a channel
336 * open anyway.
337 */
338 pending = sms_find_pending(network->sms_queue, sig_sms->sms);
339 if (!pending)
340 return 0;
341
342 switch (signal) {
343 case S_SMS_DELIVERED:
344 /*
345 * Create place for a new SMS but keep the pending data
346 * so we will not attempt to send the SMS for this subscriber
347 * as we still have an open channel and will attempt to submit
348 * SMS to it anyway.
349 */
350 network->sms_queue->pending -= 1;
351 sms_submit_pending(network->sms_queue);
352 sms_pending_free(pending);
353 break;
354 case S_SMS_MEM_EXCEEDED:
355 network->sms_queue->pending -= 1;
356 sms_pending_free(pending);
357 sms_queue_trigger(network->sms_queue);
358 break;
359 case S_SMS_UNKNOWN_ERROR:
360 /*
361 * There can be many reasons for this failure. E.g. the paging
362 * timed out, the subscriber was not paged at all, or there was
363 * a protocol error. The current strategy is to try sending the
364 * next SMS for busy/oom and to retransmit when we have paged.
365 *
366 * When the paging expires three times we will disable the
367 * subscriber. If we have some kind of other transmit error we
368 * should flag the SMS as bad.
369 */
370 switch (sig_sms->paging_result) {
371 case 0:
372 /* BAD SMS? */
373 db_sms_inc_deliver_attempts(sig_sms->sms);
374 sms_pending_failed(pending, 0);
375 break;
376 case GSM_PAGING_EXPIRED:
377 sms_pending_failed(pending, 1);
378 break;
379
380 case GSM_PAGING_OOM:
381 case GSM_PAGING_BUSY:
382 network->sms_queue->pending -= 1;
383 sms_pending_free(pending);
384 sms_queue_trigger(network->sms_queue);
385 break;
386 default:
387 LOGP(DSMS, LOGL_ERROR, "Unhandled result: %d\n",
388 sig_sms->paging_result);
389 }
390 break;
391 default:
392 LOGP(DSMS, LOGL_ERROR, "Unhandled result: %d\n",
393 sig_sms->paging_result);
394 }
395
396 return 0;
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100397}
Holger Hans Peter Freyther81c0e252010-12-25 14:08:00 +0100398
399/* VTY helper functions */
400int sms_queue_stats(struct gsm_sms_queue *smsq, struct vty *vty)
401{
402 struct gsm_sms_pending *pending;
403
404 vty_out(vty, "SMSqueue with max_pending: %d pending: %d%s",
405 smsq->max_pending, smsq->pending, VTY_NEWLINE);
406
407 llist_for_each_entry(pending, &smsq->pending_sms, entry)
Holger Hans Peter Freyther7a0e1662010-12-25 14:15:32 +0100408 vty_out(vty, " SMS Pending for Subscriber: %llu%s",
Holger Hans Peter Freyther81c0e252010-12-25 14:08:00 +0100409 pending->subscr->id, VTY_NEWLINE);
410 return 0;
411}
Holger Hans Peter Freyther3c6f6c22010-12-25 14:25:12 +0100412
413int sms_queue_set_max_pending(struct gsm_sms_queue *smsq, int max_pending)
414{
415 LOGP(DSMS, LOGL_NOTICE, "SMSqueue old max: %d new: %d\n",
416 smsq->max_pending, max_pending);
417 smsq->max_pending = max_pending;
418 return 0;
419}
Holger Hans Peter Freyther4dcc5e52010-12-25 14:38:30 +0100420
Holger Hans Peter Freyther994dcbb2010-12-25 14:50:50 +0100421int sms_queue_set_max_failure(struct gsm_sms_queue *smsq, int max_fail)
422{
423 LOGP(DSMS, LOGL_NOTICE, "SMSqueue max failure old: %d new: %d\n",
424 smsq->max_fail, max_fail);
425 smsq->max_fail = max_fail;
426 return 0;
427}
428
Holger Hans Peter Freyther4dcc5e52010-12-25 14:38:30 +0100429int sms_queue_clear(struct gsm_sms_queue *smsq)
430{
431 struct gsm_sms_pending *pending, *tmp;
432
433 llist_for_each_entry_safe(pending, tmp, &smsq->pending_sms, entry) {
434 LOGP(DSMS, LOGL_NOTICE,
435 "SMSqueue clearing for sub %llu\n", pending->subscr->id);
436 sms_pending_free(pending);
437 }
438
439 return 0;
440}