blob: 30fa5f0980a23eb8fc8d8ec1bc30b83dfda08b85 [file] [log] [blame]
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001/* 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 Affero General Public License as published by
8 * the Free Software Foundation; either version 3 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 Affero General Public License for more details.
15 *
16 * 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/>.
18 *
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>
37#include <openbsc/gsm_subscriber.h>
38#include <openbsc/signal.h>
39
Jonathan Santos5a45b152011-08-17 15:33:57 -040040#include <osmocom/core/talloc.h>
Jonathan Santos03fd8d02011-05-25 13:54:02 -040041
42#include <osmocom/vty/vty.h>
43
44/*
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};
55
56struct gsm_sms_queue {
Jonathan Santos5a45b152011-08-17 15:33:57 -040057 struct osmo_timer_list resend_pending;
58 struct osmo_timer_list push_queue;
Jonathan Santos03fd8d02011-05-25 13:54:02 -040059 struct gsm_network *network;
60 int max_fail;
61 int max_pending;
62 int pending;
63
64 struct llist_head pending_sms;
65 unsigned long long last_subscr_id;
66};
67
68static 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;
Jonathan Santos5a45b152011-08-17 15:33:57 -0400132 if (osmo_timer_pending(&smsq->resend_pending))
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400133 return;
134
Jonathan Santos5a45b152011-08-17 15:33:57 -0400135 osmo_timer_schedule(&smsq->resend_pending, 1, 0);
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400136}
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;
146 if (++pending->failed_attempts < smsq->max_fail)
147 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);
154
155 /* Workaround a failing sync */
156 db_subscriber_update(pending->subscr);
157 }
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
192static 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
211/**
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;
219 int initialized = 0;
220 unsigned long long first_sub = 0;
221 int attempted = 0, rounds = 0;
222
223 LOGP(DSMS, LOGL_NOTICE, "Attempting to send %d SMS\n", attempts);
224
225 do {
226 struct gsm_sms_pending *pending;
227 struct gsm_sms *sms;
228
229
230 sms = take_next_sms(smsq);
231 if (!sms)
232 break;
233
234 rounds += 1;
235
236 /*
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
253 /* no need to send a pending sms */
254 if (sms_is_in_pending(smsq, sms)) {
255 LOGP(DSMS, LOGL_DEBUG,
256 "SMSqueue with pending sms: %llu. Skipping\n", sms->id);
257 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
277 attempted += 1;
278 smsq->pending += 1;
279 llist_add_tail(&pending->entry, &smsq->pending_sms);
280 gsm411_send_sms_subscr(sms->receiver, sms);
281 } while (attempted < attempts && rounds < 1000);
282
283 LOGP(DSMS, LOGL_DEBUG, "SMSqueue added %d messages in %d rounds\n", attempted, rounds);
284}
285
286/*
287 * Kick off the queue again.
288 */
289int sms_queue_trigger(struct gsm_sms_queue *smsq)
290{
Jonathan Santos5a45b152011-08-17 15:33:57 -0400291 if (osmo_timer_pending(&smsq->push_queue))
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400292 return 0;
293
Jonathan Santos5a45b152011-08-17 15:33:57 -0400294 osmo_timer_schedule(&smsq->push_queue, 1, 0);
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400295 return 0;
296}
297
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
Jonathan Santos5a45b152011-08-17 15:33:57 -0400306 osmo_signal_register_handler(SS_SUBSCR, sms_subscr_cb, network);
307 osmo_signal_register_handler(SS_SMS, sms_sms_cb, network);
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400308
309 network->sms_queue = sms;
310 INIT_LLIST_HEAD(&sms->pending_sms);
311 sms->max_fail = 1;
312 sms->network = network;
313 sms->max_pending = max_pending;
314 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);
320
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
353static int sms_sms_cb(unsigned int subsys, unsigned int signal,
354 void *handler_data, void *signal_data)
355{
356 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;
434}
435
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)
445 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);
448 return 0;
449}
450
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}
458
459int 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
467int 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 smsq->pending = 0;
478 return 0;
479}