blob: 41b9ecc2584480d2d8d455f2fc11f3fcc0ee19dc [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);
Holger Hans Peter Freythera3a659b2010-12-25 16:53:24 +0100155
156 /* Workaround a failing sync */
157 db_subscriber_update(pending->subscr);
Holger Hans Peter Freyther17164062010-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
193/**
194 * I will submit up to max_pending - pending SMS to the
195 * subsystem.
196 */
197static void sms_submit_pending(void *_data)
198{
199 struct gsm_sms_queue *smsq = _data;
200 int attempts = smsq->max_pending - smsq->pending;
201 int i;
202
203 LOGP(DSMS, LOGL_NOTICE, "Attempting to send %d SMS\n", attempts);
204
205 for (i = 0; i < attempts; ++i) {
206 struct gsm_sms_pending *pending;
207 struct gsm_sms *sms;
208
209 sms = db_sms_get_unsent_by_subscr(smsq->network, smsq->last_subscr_id, 10);
210
211 /* handle wrapping around */
212 if (!sms) {
213 smsq->last_subscr_id = 0;
214 sms = db_sms_get_unsent_by_subscr(smsq->network,
215 smsq->last_subscr_id, 10);
216 }
217
218 if (!sms)
219 break;
220
221 /* no need to send a pending sms */
222 if (sms_is_in_pending(smsq, sms)) {
223 LOGP(DSMS, LOGL_DEBUG,
224 "SMSqueue with pending sms: %llu\n. Skipping", sms->id);
225 sms_free(sms);
226 continue;
227 }
228
229 /* no need to send a SMS with the same receiver */
230 if (sms_subscriber_is_pending(smsq, sms->receiver)) {
231 LOGP(DSMS, LOGL_DEBUG,
232 "SMSqueue with pending sub: %llu. Skipping\n", sms->receiver->id);
233 sms_free(sms);
234 continue;
235 }
236
237 pending = sms_pending_from(smsq, sms);
238 if (!pending) {
239 LOGP(DSMS, LOGL_ERROR,
240 "Failed to create pending SMS entry.\n");
241 sms_free(sms);
242 continue;
243 }
244
245 smsq->last_subscr_id = sms->receiver->id + 1;
246 smsq->pending += 1;
247 llist_add(&pending->entry, &smsq->pending_sms);
248 gsm411_send_sms_subscr(sms->receiver, sms);
249 }
250}
251
252/*
253 * Kick off the queue again.
254 */
255int sms_queue_trigger(struct gsm_sms_queue *smsq)
256{
257 if (bsc_timer_pending(&smsq->push_queue))
258 return 0;
259
260 bsc_schedule_timer(&smsq->push_queue, 1, 0);
261 return 0;
262}
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100263
264int sms_queue_start(struct gsm_network *network, int max_pending)
265{
266 struct gsm_sms_queue *sms = talloc_zero(network, struct gsm_sms_queue);
267 if (!sms) {
268 LOGP(DMSC, LOGL_ERROR, "Failed to create the SMS queue.\n");
269 return -1;
270 }
271
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100272 register_signal_handler(SS_SUBSCR, sms_subscr_cb, network);
273 register_signal_handler(SS_SMS, sms_sms_cb, network);
274
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100275 network->sms_queue = sms;
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100276 INIT_LLIST_HEAD(&sms->pending_sms);
Holger Hans Peter Freythera37e3bc2010-12-25 17:43:03 +0100277 sms->max_fail = 1;
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100278 sms->network = network;
279 sms->max_pending = max_pending;
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100280 sms->push_queue.data = sms;
281 sms->push_queue.cb = sms_submit_pending;
282 sms->resend_pending.data = sms;
283 sms->resend_pending.cb = sms_resend_pending;
284
285 sms_submit_pending(sms);
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100286
287 return 0;
288}
289
290static int sub_ready_for_sm(struct gsm_subscriber *subscr)
291{
292 struct gsm_subscriber_connection *conn;
293 struct gsm_sms *sms;
294
295 /* A subscriber has attached. Check if there are
296 * any pending SMS for him to be delivered */
297 conn = connection_for_subscr(subscr);
298 if (!conn)
299 return -1;
300 sms = db_sms_get_unsent_for_subscr(subscr);
301 if (!sms)
302 return -1;
303 gsm411_send_sms(conn, sms);
304 return 0;
305}
306
307static int sms_subscr_cb(unsigned int subsys, unsigned int signal,
308 void *handler_data, void *signal_data)
309{
310 struct gsm_subscriber *subscr = signal_data;
311
312 if (signal != S_SUBSCR_ATTACHED)
313 return 0;
314
315 /* this is readyForSM */
316 return sub_ready_for_sm(subscr);
317}
318
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100319static int sms_sms_cb(unsigned int subsys, unsigned int signal,
320 void *handler_data, void *signal_data)
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100321{
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100322 struct gsm_network *network = handler_data;
323 struct sms_signal_data *sig_sms = signal_data;
324 struct gsm_sms_pending *pending;
325
326 /* We got a new SMS and maybe should launch the queue again. */
327 if (signal == S_SMS_SUBMITTED || signal == S_SMS_SMMA) {
328 sms_queue_trigger(network->sms_queue);
329 return 0;
330 }
331
332 if (!sig_sms->sms)
333 return -1;
334
335
336 /*
337 * Find the entry of our queue. The SMS subsystem will submit
338 * sms that are not in our control as we just have a channel
339 * open anyway.
340 */
341 pending = sms_find_pending(network->sms_queue, sig_sms->sms);
342 if (!pending)
343 return 0;
344
345 switch (signal) {
346 case S_SMS_DELIVERED:
347 /*
348 * Create place for a new SMS but keep the pending data
349 * so we will not attempt to send the SMS for this subscriber
350 * as we still have an open channel and will attempt to submit
351 * SMS to it anyway.
352 */
353 network->sms_queue->pending -= 1;
354 sms_submit_pending(network->sms_queue);
355 sms_pending_free(pending);
356 break;
357 case S_SMS_MEM_EXCEEDED:
358 network->sms_queue->pending -= 1;
359 sms_pending_free(pending);
360 sms_queue_trigger(network->sms_queue);
361 break;
362 case S_SMS_UNKNOWN_ERROR:
363 /*
364 * There can be many reasons for this failure. E.g. the paging
365 * timed out, the subscriber was not paged at all, or there was
366 * a protocol error. The current strategy is to try sending the
367 * next SMS for busy/oom and to retransmit when we have paged.
368 *
369 * When the paging expires three times we will disable the
370 * subscriber. If we have some kind of other transmit error we
371 * should flag the SMS as bad.
372 */
373 switch (sig_sms->paging_result) {
374 case 0:
375 /* BAD SMS? */
376 db_sms_inc_deliver_attempts(sig_sms->sms);
377 sms_pending_failed(pending, 0);
378 break;
379 case GSM_PAGING_EXPIRED:
380 sms_pending_failed(pending, 1);
381 break;
382
383 case GSM_PAGING_OOM:
384 case GSM_PAGING_BUSY:
385 network->sms_queue->pending -= 1;
386 sms_pending_free(pending);
387 sms_queue_trigger(network->sms_queue);
388 break;
389 default:
390 LOGP(DSMS, LOGL_ERROR, "Unhandled result: %d\n",
391 sig_sms->paging_result);
392 }
393 break;
394 default:
395 LOGP(DSMS, LOGL_ERROR, "Unhandled result: %d\n",
396 sig_sms->paging_result);
397 }
398
399 return 0;
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100400}
Holger Hans Peter Freyther81c0e252010-12-25 14:08:00 +0100401
402/* VTY helper functions */
403int sms_queue_stats(struct gsm_sms_queue *smsq, struct vty *vty)
404{
405 struct gsm_sms_pending *pending;
406
407 vty_out(vty, "SMSqueue with max_pending: %d pending: %d%s",
408 smsq->max_pending, smsq->pending, VTY_NEWLINE);
409
410 llist_for_each_entry(pending, &smsq->pending_sms, entry)
Holger Hans Peter Freyther7a0e1662010-12-25 14:15:32 +0100411 vty_out(vty, " SMS Pending for Subscriber: %llu%s",
Holger Hans Peter Freyther81c0e252010-12-25 14:08:00 +0100412 pending->subscr->id, VTY_NEWLINE);
413 return 0;
414}
Holger Hans Peter Freyther3c6f6c22010-12-25 14:25:12 +0100415
416int sms_queue_set_max_pending(struct gsm_sms_queue *smsq, int max_pending)
417{
418 LOGP(DSMS, LOGL_NOTICE, "SMSqueue old max: %d new: %d\n",
419 smsq->max_pending, max_pending);
420 smsq->max_pending = max_pending;
421 return 0;
422}
Holger Hans Peter Freyther4dcc5e52010-12-25 14:38:30 +0100423
Holger Hans Peter Freyther994dcbb2010-12-25 14:50:50 +0100424int sms_queue_set_max_failure(struct gsm_sms_queue *smsq, int max_fail)
425{
426 LOGP(DSMS, LOGL_NOTICE, "SMSqueue max failure old: %d new: %d\n",
427 smsq->max_fail, max_fail);
428 smsq->max_fail = max_fail;
429 return 0;
430}
431
Holger Hans Peter Freyther4dcc5e52010-12-25 14:38:30 +0100432int sms_queue_clear(struct gsm_sms_queue *smsq)
433{
434 struct gsm_sms_pending *pending, *tmp;
435
436 llist_for_each_entry_safe(pending, tmp, &smsq->pending_sms, entry) {
437 LOGP(DSMS, LOGL_NOTICE,
438 "SMSqueue clearing for sub %llu\n", pending->subscr->id);
439 sms_pending_free(pending);
440 }
441
442 return 0;
443}