blob: f0d3d375c4d3dc65f10277c11c96af6efd7a9e73 [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
Harald Welte9af6ddf2011-01-01 15:25:50 +01007 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +01009 * (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
Harald Welte9af6ddf2011-01-01 15:25:50 +010014 * GNU Affero General Public License for more details.
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +010015 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010016 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +010018 *
19 */
20
21/**
22 * The difficulty of such a queue is to send a lot of SMS without
23 * overloading the paging subsystem and the database and other users
24 * of the MSC. To make the best use we would need to know the number
25 * of pending paging requests, then throttle the number of SMS we
26 * want to send and such.
27 * We will start with a very simple SMS Queue and then try to speed
28 * things up by collecting data from other parts of the system.
29 */
30
31#include <openbsc/sms_queue.h>
32#include <openbsc/chan_alloc.h>
33#include <openbsc/db.h>
34#include <openbsc/debug.h>
35#include <openbsc/gsm_data.h>
36#include <openbsc/gsm_04_11.h>
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +010037#include <openbsc/gsm_subscriber.h>
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +010038#include <openbsc/signal.h>
39
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010040#include <osmocom/core/talloc.h>
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +010041
Holger Hans Peter Freyther81c0e252010-12-25 14:08:00 +010042#include <osmocom/vty/vty.h>
43
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +010044/*
45 * One pending SMS that we wait for.
46 */
47struct gsm_sms_pending {
48 struct llist_head entry;
49
50 struct gsm_subscriber *subscr;
51 unsigned long long sms_id;
52 int failed_attempts;
53 int resend;
54};
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +010055
56struct gsm_sms_queue {
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +010057 struct timer_list resend_pending;
58 struct timer_list push_queue;
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +010059 struct gsm_network *network;
Holger Hans Peter Freyther7e59c832010-12-25 14:46:54 +010060 int max_fail;
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +010061 int max_pending;
62 int pending;
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +010063
64 struct llist_head pending_sms;
65 unsigned long long last_subscr_id;
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +010066};
67
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +010068static int sms_subscr_cb(unsigned int, unsigned int, void *, void *);
69static int sms_sms_cb(unsigned int, unsigned int, void *, void *);
70
71static struct gsm_sms_pending *sms_find_pending(struct gsm_sms_queue *smsq,
72 struct gsm_sms *sms)
73{
74 struct gsm_sms_pending *pending;
75
76 llist_for_each_entry(pending, &smsq->pending_sms, entry) {
77 if (pending->sms_id == sms->id)
78 return pending;
79 }
80
81 return NULL;
82}
83
84static int sms_is_in_pending(struct gsm_sms_queue *smsq, struct gsm_sms *sms)
85{
86 return sms_find_pending(smsq, sms) != NULL;
87}
88
89static int sms_subscriber_is_pending(struct gsm_sms_queue *smsq,
90 struct gsm_subscriber *subscr)
91{
92 struct gsm_sms_pending *pending;
93
94 llist_for_each_entry(pending, &smsq->pending_sms, entry) {
95 if (pending->subscr == subscr)
96 return 1;
97 }
98
99 return 0;
100}
101
102static struct gsm_sms_pending *sms_pending_from(struct gsm_sms_queue *smsq,
103 struct gsm_sms *sms)
104{
105 struct gsm_sms_pending *pending;
106
107 pending = talloc_zero(smsq, struct gsm_sms_pending);
108 if (!pending)
109 return NULL;
110
111 pending->subscr = subscr_get(sms->receiver);
112 pending->sms_id = sms->id;
113 return pending;
114}
115
116static void sms_pending_free(struct gsm_sms_pending *pending)
117{
118 subscr_put(pending->subscr);
119 llist_del(&pending->entry);
120 talloc_free(pending);
121}
122
123static void sms_pending_resend(struct gsm_sms_pending *pending)
124{
125 struct gsm_sms_queue *smsq;
126 LOGP(DSMS, LOGL_DEBUG,
127 "Scheduling resend of SMS %llu.\n", pending->sms_id);
128
129 pending->resend = 1;
130
131 smsq = pending->subscr->net->sms_queue;
132 if (bsc_timer_pending(&smsq->resend_pending))
133 return;
134
135 bsc_schedule_timer(&smsq->resend_pending, 1, 0);
136}
137
138static void sms_pending_failed(struct gsm_sms_pending *pending, int paging_error)
139{
140 struct gsm_sms_queue *smsq;
141
142 LOGP(DSMS, LOGL_NOTICE, "Sending SMS %llu failed %d times.\n",
143 pending->sms_id, pending->failed_attempts);
144
145 smsq = pending->subscr->net->sms_queue;
Holger Hans Peter Freyther7e59c832010-12-25 14:46:54 +0100146 if (++pending->failed_attempts < smsq->max_fail)
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100147 return sms_pending_resend(pending);
148
149 if (paging_error) {
150 LOGP(DSMS, LOGL_NOTICE,
151 "Subscriber %llu is not reachable. Setting LAC=0.\n", pending->subscr->id);
152 pending->subscr->lac = GSM_LAC_RESERVED_DETACHED;
153 db_sync_subscriber(pending->subscr);
Holger Hans Peter Freythera3a659b2010-12-25 16:53:24 +0100154
155 /* Workaround a failing sync */
156 db_subscriber_update(pending->subscr);
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100157 }
158
159 sms_pending_free(pending);
160 smsq->pending -= 1;
161 sms_queue_trigger(smsq);
162}
163
164/*
165 * Resend all SMS that are scheduled for a resend. This is done to
166 * avoid an immediate failure.
167 */
168static void sms_resend_pending(void *_data)
169{
170 struct gsm_sms_pending *pending, *tmp;
171 struct gsm_sms_queue *smsq = _data;
172
173 llist_for_each_entry_safe(pending, tmp, &smsq->pending_sms, entry) {
174 struct gsm_sms *sms;
175 if (!pending->resend)
176 continue;
177
178 sms = db_sms_get(smsq->network, pending->sms_id);
179
180 /* the sms is gone? Move to the next */
181 if (!sms) {
182 sms_pending_free(pending);
183 smsq->pending -= 1;
184 sms_queue_trigger(smsq);
185 } else {
186 pending->resend = 0;
187 gsm411_send_sms_subscr(sms->receiver, sms);
188 }
189 }
190}
191
Holger Hans Peter Freytherf7e23892010-12-25 17:45:23 +0100192static struct gsm_sms *take_next_sms(struct gsm_sms_queue *smsq)
193{
194 struct gsm_sms *sms;
195
196 sms = db_sms_get_unsent_by_subscr(smsq->network, smsq->last_subscr_id, 10);
197 if (sms) {
198 smsq->last_subscr_id = sms->receiver->id + 1;
199 return sms;
200 }
201
202 /* need to wrap around */
203 smsq->last_subscr_id = 0;
204 sms = db_sms_get_unsent_by_subscr(smsq->network,
205 smsq->last_subscr_id, 10);
206 if (sms)
207 smsq->last_subscr_id = sms->receiver->id + 1;
208 return sms;
209}
210
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100211/**
212 * I will submit up to max_pending - pending SMS to the
213 * subsystem.
214 */
215static void sms_submit_pending(void *_data)
216{
217 struct gsm_sms_queue *smsq = _data;
218 int attempts = smsq->max_pending - smsq->pending;
Holger Hans Peter Freyther20384572010-12-25 19:28:44 +0100219 int initialized = 0;
220 unsigned long long first_sub = 0;
Holger Hans Peter Freyther5479fc82010-12-25 19:32:12 +0100221 int attempted = 0, rounds = 0;
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100222
223 LOGP(DSMS, LOGL_NOTICE, "Attempting to send %d SMS\n", attempts);
224
Holger Hans Peter Freyther20384572010-12-25 19:28:44 +0100225 do {
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100226 struct gsm_sms_pending *pending;
227 struct gsm_sms *sms;
228
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100229
Holger Hans Peter Freytherf7e23892010-12-25 17:45:23 +0100230 sms = take_next_sms(smsq);
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100231 if (!sms)
232 break;
233
Holger Hans Peter Freyther5479fc82010-12-25 19:32:12 +0100234 rounds += 1;
235
Holger Hans Peter Freyther20384572010-12-25 19:28:44 +0100236 /*
237 * This code needs to detect a loop. It assumes that no SMS
238 * will vanish during the time this is executed. We will remember
239 * the id of the first GSM subscriber we see and then will
240 * compare this. The Database code should make sure that we will
241 * see all other subscribers first before seeing this one again.
242 *
243 * It is always scary to have an infinite loop like this.
244 */
245 if (!initialized) {
246 first_sub = sms->receiver->id;
247 initialized = 1;
248 } else if (first_sub == sms->receiver->id) {
249 sms_free(sms);
250 break;
251 }
252
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100253 /* no need to send a pending sms */
254 if (sms_is_in_pending(smsq, sms)) {
255 LOGP(DSMS, LOGL_DEBUG,
Holger Hans Peter Freytherdc53af62010-12-27 20:12:25 +0100256 "SMSqueue with pending sms: %llu. Skipping\n", sms->id);
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100257 sms_free(sms);
258 continue;
259 }
260
261 /* no need to send a SMS with the same receiver */
262 if (sms_subscriber_is_pending(smsq, sms->receiver)) {
263 LOGP(DSMS, LOGL_DEBUG,
264 "SMSqueue with pending sub: %llu. Skipping\n", sms->receiver->id);
265 sms_free(sms);
266 continue;
267 }
268
269 pending = sms_pending_from(smsq, sms);
270 if (!pending) {
271 LOGP(DSMS, LOGL_ERROR,
272 "Failed to create pending SMS entry.\n");
273 sms_free(sms);
274 continue;
275 }
276
Holger Hans Peter Freyther20384572010-12-25 19:28:44 +0100277 attempted += 1;
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100278 smsq->pending += 1;
Holger Hans Peter Freyther701076e2010-12-28 14:25:25 +0100279 llist_add_tail(&pending->entry, &smsq->pending_sms);
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100280 gsm411_send_sms_subscr(sms->receiver, sms);
Holger Hans Peter Freyther5479fc82010-12-25 19:32:12 +0100281 } while (attempted < attempts && rounds < 1000);
Holger Hans Peter Freyther20384572010-12-25 19:28:44 +0100282
Holger Hans Peter Freyther5479fc82010-12-25 19:32:12 +0100283 LOGP(DSMS, LOGL_DEBUG, "SMSqueue added %d messages in %d rounds\n", attempted, rounds);
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100284}
285
286/*
287 * Kick off the queue again.
288 */
289int sms_queue_trigger(struct gsm_sms_queue *smsq)
290{
291 if (bsc_timer_pending(&smsq->push_queue))
292 return 0;
293
294 bsc_schedule_timer(&smsq->push_queue, 1, 0);
295 return 0;
296}
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100297
298int sms_queue_start(struct gsm_network *network, int max_pending)
299{
300 struct gsm_sms_queue *sms = talloc_zero(network, struct gsm_sms_queue);
301 if (!sms) {
302 LOGP(DMSC, LOGL_ERROR, "Failed to create the SMS queue.\n");
303 return -1;
304 }
305
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100306 register_signal_handler(SS_SUBSCR, sms_subscr_cb, network);
307 register_signal_handler(SS_SMS, sms_sms_cb, network);
308
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100309 network->sms_queue = sms;
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100310 INIT_LLIST_HEAD(&sms->pending_sms);
Holger Hans Peter Freythera37e3bc2010-12-25 17:43:03 +0100311 sms->max_fail = 1;
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100312 sms->network = network;
313 sms->max_pending = max_pending;
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100314 sms->push_queue.data = sms;
315 sms->push_queue.cb = sms_submit_pending;
316 sms->resend_pending.data = sms;
317 sms->resend_pending.cb = sms_resend_pending;
318
319 sms_submit_pending(sms);
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100320
321 return 0;
322}
323
324static int sub_ready_for_sm(struct gsm_subscriber *subscr)
325{
326 struct gsm_subscriber_connection *conn;
327 struct gsm_sms *sms;
328
329 /* A subscriber has attached. Check if there are
330 * any pending SMS for him to be delivered */
331 conn = connection_for_subscr(subscr);
332 if (!conn)
333 return -1;
334 sms = db_sms_get_unsent_for_subscr(subscr);
335 if (!sms)
336 return -1;
337 gsm411_send_sms(conn, sms);
338 return 0;
339}
340
341static int sms_subscr_cb(unsigned int subsys, unsigned int signal,
342 void *handler_data, void *signal_data)
343{
344 struct gsm_subscriber *subscr = signal_data;
345
346 if (signal != S_SUBSCR_ATTACHED)
347 return 0;
348
349 /* this is readyForSM */
350 return sub_ready_for_sm(subscr);
351}
352
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100353static int sms_sms_cb(unsigned int subsys, unsigned int signal,
354 void *handler_data, void *signal_data)
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100355{
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100356 struct gsm_network *network = handler_data;
357 struct sms_signal_data *sig_sms = signal_data;
358 struct gsm_sms_pending *pending;
359
360 /* We got a new SMS and maybe should launch the queue again. */
361 if (signal == S_SMS_SUBMITTED || signal == S_SMS_SMMA) {
362 sms_queue_trigger(network->sms_queue);
363 return 0;
364 }
365
366 if (!sig_sms->sms)
367 return -1;
368
369
370 /*
371 * Find the entry of our queue. The SMS subsystem will submit
372 * sms that are not in our control as we just have a channel
373 * open anyway.
374 */
375 pending = sms_find_pending(network->sms_queue, sig_sms->sms);
376 if (!pending)
377 return 0;
378
379 switch (signal) {
380 case S_SMS_DELIVERED:
381 /*
382 * Create place for a new SMS but keep the pending data
383 * so we will not attempt to send the SMS for this subscriber
384 * as we still have an open channel and will attempt to submit
385 * SMS to it anyway.
386 */
387 network->sms_queue->pending -= 1;
388 sms_submit_pending(network->sms_queue);
389 sms_pending_free(pending);
390 break;
391 case S_SMS_MEM_EXCEEDED:
392 network->sms_queue->pending -= 1;
393 sms_pending_free(pending);
394 sms_queue_trigger(network->sms_queue);
395 break;
396 case S_SMS_UNKNOWN_ERROR:
397 /*
398 * There can be many reasons for this failure. E.g. the paging
399 * timed out, the subscriber was not paged at all, or there was
400 * a protocol error. The current strategy is to try sending the
401 * next SMS for busy/oom and to retransmit when we have paged.
402 *
403 * When the paging expires three times we will disable the
404 * subscriber. If we have some kind of other transmit error we
405 * should flag the SMS as bad.
406 */
407 switch (sig_sms->paging_result) {
408 case 0:
409 /* BAD SMS? */
410 db_sms_inc_deliver_attempts(sig_sms->sms);
411 sms_pending_failed(pending, 0);
412 break;
413 case GSM_PAGING_EXPIRED:
414 sms_pending_failed(pending, 1);
415 break;
416
417 case GSM_PAGING_OOM:
418 case GSM_PAGING_BUSY:
419 network->sms_queue->pending -= 1;
420 sms_pending_free(pending);
421 sms_queue_trigger(network->sms_queue);
422 break;
423 default:
424 LOGP(DSMS, LOGL_ERROR, "Unhandled result: %d\n",
425 sig_sms->paging_result);
426 }
427 break;
428 default:
429 LOGP(DSMS, LOGL_ERROR, "Unhandled result: %d\n",
430 sig_sms->paging_result);
431 }
432
433 return 0;
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100434}
Holger Hans Peter Freyther81c0e252010-12-25 14:08:00 +0100435
436/* VTY helper functions */
437int sms_queue_stats(struct gsm_sms_queue *smsq, struct vty *vty)
438{
439 struct gsm_sms_pending *pending;
440
441 vty_out(vty, "SMSqueue with max_pending: %d pending: %d%s",
442 smsq->max_pending, smsq->pending, VTY_NEWLINE);
443
444 llist_for_each_entry(pending, &smsq->pending_sms, entry)
Holger Hans Peter Freyther583e9ae2010-12-27 20:19:48 +0100445 vty_out(vty, " SMS Pending for Subscriber: %llu SMS: %llu Failed: %d.%s",
446 pending->subscr->id, pending->sms_id,
447 pending->failed_attempts, VTY_NEWLINE);
Holger Hans Peter Freyther81c0e252010-12-25 14:08:00 +0100448 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
Holger Hans Peter Freyther96e9f082010-12-28 14:09:07 +0100477 smsq->pending = 0;
Holger Hans Peter Freyther4dcc5e52010-12-25 14:38:30 +0100478 return 0;
479}