blob: e8e65846f95e629d07edb13396c0fb2a4842b720 [file] [log] [blame]
Holger Hans Peter Freyther575d2e12010-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 Freyther61318332010-12-24 21:39:55 +010038#include <openbsc/gsm_subscriber.h>
Holger Hans Peter Freyther575d2e12010-12-24 13:48:27 +010039#include <openbsc/signal.h>
40
41#include <osmocore/talloc.h>
42
Holger Hans Peter Freyther6ec80982010-12-25 14:08:00 +010043#include <osmocom/vty/vty.h>
44
Holger Hans Peter Freyther61318332010-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 Freyther575d2e12010-12-24 13:48:27 +010056
57struct gsm_sms_queue {
Holger Hans Peter Freyther61318332010-12-24 21:39:55 +010058 struct timer_list resend_pending;
59 struct timer_list push_queue;
Holger Hans Peter Freyther575d2e12010-12-24 13:48:27 +010060 struct gsm_network *network;
Holger Hans Peter Freyther4334e4e2010-12-25 14:46:54 +010061 int max_fail;
Holger Hans Peter Freyther575d2e12010-12-24 13:48:27 +010062 int max_pending;
63 int pending;
Holger Hans Peter Freyther61318332010-12-24 21:39:55 +010064
65 struct llist_head pending_sms;
66 unsigned long long last_subscr_id;
Holger Hans Peter Freyther575d2e12010-12-24 13:48:27 +010067};
68
Holger Hans Peter Freyther61318332010-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 Freyther4334e4e2010-12-25 14:46:54 +0100147 if (++pending->failed_attempts < smsq->max_fail)
Holger Hans Peter Freyther61318332010-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);
Holger Hans Peter Freyther1ae97512010-12-25 16:53:24 +0100155
156 /* Workaround a failing sync */
157 db_subscriber_update(pending->subscr);
Holger Hans Peter Freyther61318332010-12-24 21:39:55 +0100158 }
159
160 sms_pending_free(pending);
161 smsq->pending -= 1;
162 sms_queue_trigger(smsq);
163}
164
165/*
166 * Resend all SMS that are scheduled for a resend. This is done to
167 * avoid an immediate failure.
168 */
169static void sms_resend_pending(void *_data)
170{
171 struct gsm_sms_pending *pending, *tmp;
172 struct gsm_sms_queue *smsq = _data;
173
174 llist_for_each_entry_safe(pending, tmp, &smsq->pending_sms, entry) {
175 struct gsm_sms *sms;
176 if (!pending->resend)
177 continue;
178
179 sms = db_sms_get(smsq->network, pending->sms_id);
180
181 /* the sms is gone? Move to the next */
182 if (!sms) {
183 sms_pending_free(pending);
184 smsq->pending -= 1;
185 sms_queue_trigger(smsq);
186 } else {
187 pending->resend = 0;
188 gsm411_send_sms_subscr(sms->receiver, sms);
189 }
190 }
191}
192
Holger Hans Peter Freythere41db2a2010-12-25 17:45:23 +0100193static struct gsm_sms *take_next_sms(struct gsm_sms_queue *smsq)
194{
195 struct gsm_sms *sms;
196
197 sms = db_sms_get_unsent_by_subscr(smsq->network, smsq->last_subscr_id, 10);
198 if (sms) {
199 smsq->last_subscr_id = sms->receiver->id + 1;
200 return sms;
201 }
202
203 /* need to wrap around */
204 smsq->last_subscr_id = 0;
205 sms = db_sms_get_unsent_by_subscr(smsq->network,
206 smsq->last_subscr_id, 10);
207 if (sms)
208 smsq->last_subscr_id = sms->receiver->id + 1;
209 return sms;
210}
211
Holger Hans Peter Freyther61318332010-12-24 21:39:55 +0100212/**
213 * I will submit up to max_pending - pending SMS to the
214 * subsystem.
215 */
216static void sms_submit_pending(void *_data)
217{
218 struct gsm_sms_queue *smsq = _data;
219 int attempts = smsq->max_pending - smsq->pending;
220 int i;
221
222 LOGP(DSMS, LOGL_NOTICE, "Attempting to send %d SMS\n", attempts);
223
224 for (i = 0; i < attempts; ++i) {
225 struct gsm_sms_pending *pending;
226 struct gsm_sms *sms;
227
Holger Hans Peter Freyther61318332010-12-24 21:39:55 +0100228
Holger Hans Peter Freythere41db2a2010-12-25 17:45:23 +0100229 sms = take_next_sms(smsq);
Holger Hans Peter Freyther61318332010-12-24 21:39:55 +0100230 if (!sms)
231 break;
232
233 /* no need to send a pending sms */
234 if (sms_is_in_pending(smsq, sms)) {
235 LOGP(DSMS, LOGL_DEBUG,
236 "SMSqueue with pending sms: %llu\n. Skipping", sms->id);
237 sms_free(sms);
238 continue;
239 }
240
241 /* no need to send a SMS with the same receiver */
242 if (sms_subscriber_is_pending(smsq, sms->receiver)) {
243 LOGP(DSMS, LOGL_DEBUG,
244 "SMSqueue with pending sub: %llu. Skipping\n", sms->receiver->id);
245 sms_free(sms);
246 continue;
247 }
248
249 pending = sms_pending_from(smsq, sms);
250 if (!pending) {
251 LOGP(DSMS, LOGL_ERROR,
252 "Failed to create pending SMS entry.\n");
253 sms_free(sms);
254 continue;
255 }
256
Holger Hans Peter Freyther61318332010-12-24 21:39:55 +0100257 smsq->pending += 1;
258 llist_add(&pending->entry, &smsq->pending_sms);
259 gsm411_send_sms_subscr(sms->receiver, sms);
260 }
261}
262
263/*
264 * Kick off the queue again.
265 */
266int sms_queue_trigger(struct gsm_sms_queue *smsq)
267{
268 if (bsc_timer_pending(&smsq->push_queue))
269 return 0;
270
271 bsc_schedule_timer(&smsq->push_queue, 1, 0);
272 return 0;
273}
Holger Hans Peter Freyther575d2e12010-12-24 13:48:27 +0100274
275int sms_queue_start(struct gsm_network *network, int max_pending)
276{
277 struct gsm_sms_queue *sms = talloc_zero(network, struct gsm_sms_queue);
278 if (!sms) {
279 LOGP(DMSC, LOGL_ERROR, "Failed to create the SMS queue.\n");
280 return -1;
281 }
282
Holger Hans Peter Freyther61318332010-12-24 21:39:55 +0100283 register_signal_handler(SS_SUBSCR, sms_subscr_cb, network);
284 register_signal_handler(SS_SMS, sms_sms_cb, network);
285
Holger Hans Peter Freyther575d2e12010-12-24 13:48:27 +0100286 network->sms_queue = sms;
Holger Hans Peter Freyther61318332010-12-24 21:39:55 +0100287 INIT_LLIST_HEAD(&sms->pending_sms);
Holger Hans Peter Freyther48710462010-12-25 17:43:03 +0100288 sms->max_fail = 1;
Holger Hans Peter Freyther575d2e12010-12-24 13:48:27 +0100289 sms->network = network;
290 sms->max_pending = max_pending;
Holger Hans Peter Freyther61318332010-12-24 21:39:55 +0100291 sms->push_queue.data = sms;
292 sms->push_queue.cb = sms_submit_pending;
293 sms->resend_pending.data = sms;
294 sms->resend_pending.cb = sms_resend_pending;
295
296 sms_submit_pending(sms);
Holger Hans Peter Freyther575d2e12010-12-24 13:48:27 +0100297
298 return 0;
299}
300
301static int sub_ready_for_sm(struct gsm_subscriber *subscr)
302{
303 struct gsm_subscriber_connection *conn;
304 struct gsm_sms *sms;
305
306 /* A subscriber has attached. Check if there are
307 * any pending SMS for him to be delivered */
308 conn = connection_for_subscr(subscr);
309 if (!conn)
310 return -1;
311 sms = db_sms_get_unsent_for_subscr(subscr);
312 if (!sms)
313 return -1;
314 gsm411_send_sms(conn, sms);
315 return 0;
316}
317
318static int sms_subscr_cb(unsigned int subsys, unsigned int signal,
319 void *handler_data, void *signal_data)
320{
321 struct gsm_subscriber *subscr = signal_data;
322
323 if (signal != S_SUBSCR_ATTACHED)
324 return 0;
325
326 /* this is readyForSM */
327 return sub_ready_for_sm(subscr);
328}
329
Holger Hans Peter Freyther61318332010-12-24 21:39:55 +0100330static int sms_sms_cb(unsigned int subsys, unsigned int signal,
331 void *handler_data, void *signal_data)
Holger Hans Peter Freyther575d2e12010-12-24 13:48:27 +0100332{
Holger Hans Peter Freyther61318332010-12-24 21:39:55 +0100333 struct gsm_network *network = handler_data;
334 struct sms_signal_data *sig_sms = signal_data;
335 struct gsm_sms_pending *pending;
336
337 /* We got a new SMS and maybe should launch the queue again. */
338 if (signal == S_SMS_SUBMITTED || signal == S_SMS_SMMA) {
339 sms_queue_trigger(network->sms_queue);
340 return 0;
341 }
342
343 if (!sig_sms->sms)
344 return -1;
345
346
347 /*
348 * Find the entry of our queue. The SMS subsystem will submit
349 * sms that are not in our control as we just have a channel
350 * open anyway.
351 */
352 pending = sms_find_pending(network->sms_queue, sig_sms->sms);
353 if (!pending)
354 return 0;
355
356 switch (signal) {
357 case S_SMS_DELIVERED:
358 /*
359 * Create place for a new SMS but keep the pending data
360 * so we will not attempt to send the SMS for this subscriber
361 * as we still have an open channel and will attempt to submit
362 * SMS to it anyway.
363 */
364 network->sms_queue->pending -= 1;
365 sms_submit_pending(network->sms_queue);
366 sms_pending_free(pending);
367 break;
368 case S_SMS_MEM_EXCEEDED:
369 network->sms_queue->pending -= 1;
370 sms_pending_free(pending);
371 sms_queue_trigger(network->sms_queue);
372 break;
373 case S_SMS_UNKNOWN_ERROR:
374 /*
375 * There can be many reasons for this failure. E.g. the paging
376 * timed out, the subscriber was not paged at all, or there was
377 * a protocol error. The current strategy is to try sending the
378 * next SMS for busy/oom and to retransmit when we have paged.
379 *
380 * When the paging expires three times we will disable the
381 * subscriber. If we have some kind of other transmit error we
382 * should flag the SMS as bad.
383 */
384 switch (sig_sms->paging_result) {
385 case 0:
386 /* BAD SMS? */
387 db_sms_inc_deliver_attempts(sig_sms->sms);
388 sms_pending_failed(pending, 0);
389 break;
390 case GSM_PAGING_EXPIRED:
391 sms_pending_failed(pending, 1);
392 break;
393
394 case GSM_PAGING_OOM:
395 case GSM_PAGING_BUSY:
396 network->sms_queue->pending -= 1;
397 sms_pending_free(pending);
398 sms_queue_trigger(network->sms_queue);
399 break;
400 default:
401 LOGP(DSMS, LOGL_ERROR, "Unhandled result: %d\n",
402 sig_sms->paging_result);
403 }
404 break;
405 default:
406 LOGP(DSMS, LOGL_ERROR, "Unhandled result: %d\n",
407 sig_sms->paging_result);
408 }
409
410 return 0;
Holger Hans Peter Freyther575d2e12010-12-24 13:48:27 +0100411}
Holger Hans Peter Freyther6ec80982010-12-25 14:08:00 +0100412
413/* VTY helper functions */
414int sms_queue_stats(struct gsm_sms_queue *smsq, struct vty *vty)
415{
416 struct gsm_sms_pending *pending;
417
418 vty_out(vty, "SMSqueue with max_pending: %d pending: %d%s",
419 smsq->max_pending, smsq->pending, VTY_NEWLINE);
420
421 llist_for_each_entry(pending, &smsq->pending_sms, entry)
Holger Hans Peter Freyther58fe0282010-12-25 14:15:32 +0100422 vty_out(vty, " SMS Pending for Subscriber: %llu%s",
Holger Hans Peter Freyther6ec80982010-12-25 14:08:00 +0100423 pending->subscr->id, VTY_NEWLINE);
424 return 0;
425}
Holger Hans Peter Freyther96b4d352010-12-25 14:25:12 +0100426
427int sms_queue_set_max_pending(struct gsm_sms_queue *smsq, int max_pending)
428{
429 LOGP(DSMS, LOGL_NOTICE, "SMSqueue old max: %d new: %d\n",
430 smsq->max_pending, max_pending);
431 smsq->max_pending = max_pending;
432 return 0;
433}
Holger Hans Peter Freyther416c7de2010-12-25 14:38:30 +0100434
Holger Hans Peter Freyther70428d92010-12-25 14:50:50 +0100435int sms_queue_set_max_failure(struct gsm_sms_queue *smsq, int max_fail)
436{
437 LOGP(DSMS, LOGL_NOTICE, "SMSqueue max failure old: %d new: %d\n",
438 smsq->max_fail, max_fail);
439 smsq->max_fail = max_fail;
440 return 0;
441}
442
Holger Hans Peter Freyther416c7de2010-12-25 14:38:30 +0100443int sms_queue_clear(struct gsm_sms_queue *smsq)
444{
445 struct gsm_sms_pending *pending, *tmp;
446
447 llist_for_each_entry_safe(pending, tmp, &smsq->pending_sms, entry) {
448 LOGP(DSMS, LOGL_NOTICE,
449 "SMSqueue clearing for sub %llu\n", pending->subscr->id);
450 sms_pending_free(pending);
451 }
452
453 return 0;
454}