blob: f6ac660905b93038c48985dad8b1c9f7ee304a13 [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
Holger Hans Peter Freytherf7e23892010-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 Freyther17164062010-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;
Holger Hans Peter Freyther20384572010-12-25 19:28:44 +0100220 int initialized = 0;
221 unsigned long long first_sub = 0;
222 int attempted = 0;
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100223
224 LOGP(DSMS, LOGL_NOTICE, "Attempting to send %d SMS\n", attempts);
225
Holger Hans Peter Freyther20384572010-12-25 19:28:44 +0100226 do {
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100227 struct gsm_sms_pending *pending;
228 struct gsm_sms *sms;
229
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100230
Holger Hans Peter Freytherf7e23892010-12-25 17:45:23 +0100231 sms = take_next_sms(smsq);
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100232 if (!sms)
233 break;
234
Holger Hans Peter Freyther20384572010-12-25 19:28:44 +0100235 /*
236 * This code needs to detect a loop. It assumes that no SMS
237 * will vanish during the time this is executed. We will remember
238 * the id of the first GSM subscriber we see and then will
239 * compare this. The Database code should make sure that we will
240 * see all other subscribers first before seeing this one again.
241 *
242 * It is always scary to have an infinite loop like this.
243 */
244 if (!initialized) {
245 first_sub = sms->receiver->id;
246 initialized = 1;
247 } else if (first_sub == sms->receiver->id) {
248 sms_free(sms);
249 break;
250 }
251
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100252 /* no need to send a pending sms */
253 if (sms_is_in_pending(smsq, sms)) {
254 LOGP(DSMS, LOGL_DEBUG,
255 "SMSqueue with pending sms: %llu\n. Skipping", sms->id);
256 sms_free(sms);
257 continue;
258 }
259
260 /* no need to send a SMS with the same receiver */
261 if (sms_subscriber_is_pending(smsq, sms->receiver)) {
262 LOGP(DSMS, LOGL_DEBUG,
263 "SMSqueue with pending sub: %llu. Skipping\n", sms->receiver->id);
264 sms_free(sms);
265 continue;
266 }
267
268 pending = sms_pending_from(smsq, sms);
269 if (!pending) {
270 LOGP(DSMS, LOGL_ERROR,
271 "Failed to create pending SMS entry.\n");
272 sms_free(sms);
273 continue;
274 }
275
Holger Hans Peter Freyther20384572010-12-25 19:28:44 +0100276 attempted += 1;
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100277 smsq->pending += 1;
278 llist_add(&pending->entry, &smsq->pending_sms);
279 gsm411_send_sms_subscr(sms->receiver, sms);
Holger Hans Peter Freyther20384572010-12-25 19:28:44 +0100280 } while (attempted < attempts);
281
282 LOGP(DSMS, LOGL_DEBUG, "SMSqueue added %d messages\n", attempted);
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100283}
284
285/*
286 * Kick off the queue again.
287 */
288int sms_queue_trigger(struct gsm_sms_queue *smsq)
289{
290 if (bsc_timer_pending(&smsq->push_queue))
291 return 0;
292
293 bsc_schedule_timer(&smsq->push_queue, 1, 0);
294 return 0;
295}
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100296
297int sms_queue_start(struct gsm_network *network, int max_pending)
298{
299 struct gsm_sms_queue *sms = talloc_zero(network, struct gsm_sms_queue);
300 if (!sms) {
301 LOGP(DMSC, LOGL_ERROR, "Failed to create the SMS queue.\n");
302 return -1;
303 }
304
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100305 register_signal_handler(SS_SUBSCR, sms_subscr_cb, network);
306 register_signal_handler(SS_SMS, sms_sms_cb, network);
307
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100308 network->sms_queue = sms;
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100309 INIT_LLIST_HEAD(&sms->pending_sms);
Holger Hans Peter Freythera37e3bc2010-12-25 17:43:03 +0100310 sms->max_fail = 1;
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100311 sms->network = network;
312 sms->max_pending = max_pending;
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100313 sms->push_queue.data = sms;
314 sms->push_queue.cb = sms_submit_pending;
315 sms->resend_pending.data = sms;
316 sms->resend_pending.cb = sms_resend_pending;
317
318 sms_submit_pending(sms);
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100319
320 return 0;
321}
322
323static int sub_ready_for_sm(struct gsm_subscriber *subscr)
324{
325 struct gsm_subscriber_connection *conn;
326 struct gsm_sms *sms;
327
328 /* A subscriber has attached. Check if there are
329 * any pending SMS for him to be delivered */
330 conn = connection_for_subscr(subscr);
331 if (!conn)
332 return -1;
333 sms = db_sms_get_unsent_for_subscr(subscr);
334 if (!sms)
335 return -1;
336 gsm411_send_sms(conn, sms);
337 return 0;
338}
339
340static int sms_subscr_cb(unsigned int subsys, unsigned int signal,
341 void *handler_data, void *signal_data)
342{
343 struct gsm_subscriber *subscr = signal_data;
344
345 if (signal != S_SUBSCR_ATTACHED)
346 return 0;
347
348 /* this is readyForSM */
349 return sub_ready_for_sm(subscr);
350}
351
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100352static int sms_sms_cb(unsigned int subsys, unsigned int signal,
353 void *handler_data, void *signal_data)
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100354{
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100355 struct gsm_network *network = handler_data;
356 struct sms_signal_data *sig_sms = signal_data;
357 struct gsm_sms_pending *pending;
358
359 /* We got a new SMS and maybe should launch the queue again. */
360 if (signal == S_SMS_SUBMITTED || signal == S_SMS_SMMA) {
361 sms_queue_trigger(network->sms_queue);
362 return 0;
363 }
364
365 if (!sig_sms->sms)
366 return -1;
367
368
369 /*
370 * Find the entry of our queue. The SMS subsystem will submit
371 * sms that are not in our control as we just have a channel
372 * open anyway.
373 */
374 pending = sms_find_pending(network->sms_queue, sig_sms->sms);
375 if (!pending)
376 return 0;
377
378 switch (signal) {
379 case S_SMS_DELIVERED:
380 /*
381 * Create place for a new SMS but keep the pending data
382 * so we will not attempt to send the SMS for this subscriber
383 * as we still have an open channel and will attempt to submit
384 * SMS to it anyway.
385 */
386 network->sms_queue->pending -= 1;
387 sms_submit_pending(network->sms_queue);
388 sms_pending_free(pending);
389 break;
390 case S_SMS_MEM_EXCEEDED:
391 network->sms_queue->pending -= 1;
392 sms_pending_free(pending);
393 sms_queue_trigger(network->sms_queue);
394 break;
395 case S_SMS_UNKNOWN_ERROR:
396 /*
397 * There can be many reasons for this failure. E.g. the paging
398 * timed out, the subscriber was not paged at all, or there was
399 * a protocol error. The current strategy is to try sending the
400 * next SMS for busy/oom and to retransmit when we have paged.
401 *
402 * When the paging expires three times we will disable the
403 * subscriber. If we have some kind of other transmit error we
404 * should flag the SMS as bad.
405 */
406 switch (sig_sms->paging_result) {
407 case 0:
408 /* BAD SMS? */
409 db_sms_inc_deliver_attempts(sig_sms->sms);
410 sms_pending_failed(pending, 0);
411 break;
412 case GSM_PAGING_EXPIRED:
413 sms_pending_failed(pending, 1);
414 break;
415
416 case GSM_PAGING_OOM:
417 case GSM_PAGING_BUSY:
418 network->sms_queue->pending -= 1;
419 sms_pending_free(pending);
420 sms_queue_trigger(network->sms_queue);
421 break;
422 default:
423 LOGP(DSMS, LOGL_ERROR, "Unhandled result: %d\n",
424 sig_sms->paging_result);
425 }
426 break;
427 default:
428 LOGP(DSMS, LOGL_ERROR, "Unhandled result: %d\n",
429 sig_sms->paging_result);
430 }
431
432 return 0;
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100433}
Holger Hans Peter Freyther81c0e252010-12-25 14:08:00 +0100434
435/* VTY helper functions */
436int sms_queue_stats(struct gsm_sms_queue *smsq, struct vty *vty)
437{
438 struct gsm_sms_pending *pending;
439
440 vty_out(vty, "SMSqueue with max_pending: %d pending: %d%s",
441 smsq->max_pending, smsq->pending, VTY_NEWLINE);
442
443 llist_for_each_entry(pending, &smsq->pending_sms, entry)
Holger Hans Peter Freyther7a0e1662010-12-25 14:15:32 +0100444 vty_out(vty, " SMS Pending for Subscriber: %llu%s",
Holger Hans Peter Freyther81c0e252010-12-25 14:08:00 +0100445 pending->subscr->id, VTY_NEWLINE);
446 return 0;
447}
Holger Hans Peter Freyther3c6f6c22010-12-25 14:25:12 +0100448
449int sms_queue_set_max_pending(struct gsm_sms_queue *smsq, int max_pending)
450{
451 LOGP(DSMS, LOGL_NOTICE, "SMSqueue old max: %d new: %d\n",
452 smsq->max_pending, max_pending);
453 smsq->max_pending = max_pending;
454 return 0;
455}
Holger Hans Peter Freyther4dcc5e52010-12-25 14:38:30 +0100456
Holger Hans Peter Freyther994dcbb2010-12-25 14:50:50 +0100457int sms_queue_set_max_failure(struct gsm_sms_queue *smsq, int max_fail)
458{
459 LOGP(DSMS, LOGL_NOTICE, "SMSqueue max failure old: %d new: %d\n",
460 smsq->max_fail, max_fail);
461 smsq->max_fail = max_fail;
462 return 0;
463}
464
Holger Hans Peter Freyther4dcc5e52010-12-25 14:38:30 +0100465int sms_queue_clear(struct gsm_sms_queue *smsq)
466{
467 struct gsm_sms_pending *pending, *tmp;
468
469 llist_for_each_entry_safe(pending, tmp, &smsq->pending_sms, entry) {
470 LOGP(DSMS, LOGL_NOTICE,
471 "SMSqueue clearing for sub %llu\n", pending->subscr->id);
472 sms_pending_free(pending);
473 }
474
475 return 0;
476}