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