blob: 1d14ff02f0be510cf49c31170fa0e8125664afc1 [file] [log] [blame]
Martin Hauke3f07dac2019-11-14 17:49:08 +01001/* SMS queue to continuously attempt to deliver SMS */
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +01002/*
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
Harald Welte2483f1b2016-06-19 18:06:02 +020031#include <limits.h>
32
Neels Hofmeyr90843962017-09-04 15:04:35 +020033#include <osmocom/msc/sms_queue.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020034#include <osmocom/msc/db.h>
35#include <osmocom/msc/debug.h>
36#include <osmocom/msc/gsm_data.h>
37#include <osmocom/msc/gsm_04_11.h>
38#include <osmocom/msc/gsm_subscriber.h>
39#include <osmocom/msc/signal.h>
40#include <osmocom/msc/vlr.h>
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +010041
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010042#include <osmocom/core/talloc.h>
Harald Welte63494a62022-05-15 13:03:57 +020043#include <osmocom/core/utils.h>
44#include <osmocom/core/rate_ctr.h>
45#include <osmocom/core/stat_item.h>
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +010046
Holger Hans Peter Freyther81c0e252010-12-25 14:08:00 +010047#include <osmocom/vty/vty.h>
48
Harald Welte63494a62022-05-15 13:03:57 +020049enum smsq_stat_item_idx {
50 SMSQ_STAT_SMS_RAM_PENDING,
51};
52
53static const struct osmo_stat_item_desc smsq_stat_item_desc[] = {
54 [SMSQ_STAT_SMS_RAM_PENDING] = { "ram:pending",
55 "Number of SMSs in the in-RAM pending delivery queue" },
56};
57
58static const struct osmo_stat_item_group_desc smsq_statg_desc = {
59 "sms_queue",
60 "SMS queue",
61 OSMO_STATS_CLASS_GLOBAL,
62 ARRAY_SIZE(smsq_stat_item_desc),
63 smsq_stat_item_desc,
64};
65
66enum smsq_rate_ctr_idx {
67 SMSQ_CTR_SMS_DELIVERY_ATTEMPTS,
68 SMSQ_CTR_SMS_DELIVERY_ACK,
69 SMSQ_CTR_SMS_DELIVERY_ERR,
70 SMSQ_CTR_SMS_DELIVERY_NOMEM,
71 SMSQ_CTR_SMS_DELIVERY_TIMEOUT,
72};
73
74static const struct rate_ctr_desc smsq_ctr_desc[] = {
75 [SMSQ_CTR_SMS_DELIVERY_ATTEMPTS] = { "delivery:attempts",
76 "Attempted MT SMS deliveries to subscriber" },
77 [SMSQ_CTR_SMS_DELIVERY_ACK] = { "deliver:ack",
78 "Successful MT SMS delivery to subscriber" },
79 [SMSQ_CTR_SMS_DELIVERY_ERR] = { "deliver:error",
80 "Erroneous MT SMS delivery" },
81 [SMSQ_CTR_SMS_DELIVERY_NOMEM] = { "deliver:no_memory",
82 "Failed MT SMS delivery due to no memory on MS" },
83 [SMSQ_CTR_SMS_DELIVERY_TIMEOUT] = { "deliver:paging_timeout",
84 "Failed MT SMS delivery due to paging timeout (MS gone?)" },
85};
86
87static const struct rate_ctr_group_desc smsq_ctrg_desc = {
88 "sms_queue",
89 "SMS queue",
90 OSMO_STATS_CLASS_GLOBAL,
91 ARRAY_SIZE(smsq_ctr_desc),
92 smsq_ctr_desc,
93};
94
95#define smsq_rate_ctr_inc(smsq, idx) \
96 rate_ctr_inc(rate_ctr_group_get_ctr((smsq)->ctrg, idx))
97#define smsq_rate_ctr_add(smsq, idx, val) \
98 rate_ctr_add(rate_ctr_group_get_ctr((smsq)->ctrg, idx), val)
99
100#define smsq_stat_item_inc(smsq, idx) \
101 osmo_stat_item_inc(osmo_stat_item_group_get_item((smsq)->statg, idx), 1)
102#define smsq_stat_item_dec(smsq, idx) \
103 osmo_stat_item_dec(osmo_stat_item_group_get_item((smsq)->statg, idx), 1)
104#define smsq_stat_item_set(smsq, idx, val) \
105 osmo_stat_item_set(osmo_stat_item_group_get_item((smsq)->statg, idx), val)
106
107
Harald Weltedc7d8412022-05-14 09:54:15 +0200108/* One in-RAM record of a "pending SMS". This is not the SMS itself, but merely
109 * a pointer to the database record. It holds a reference on the vlr_subscriber
110 * and some counters. While this object exists in RAM, we are regularly attempting
111 * to deliver the related SMS. */
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100112struct gsm_sms_pending {
Harald Weltedc7d8412022-05-14 09:54:15 +0200113 struct llist_head entry; /* gsm_sms_queue.pending_sms */
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100114
Harald Weltedc7d8412022-05-14 09:54:15 +0200115 struct vlr_subscr *vsub; /* destination subscriber for this SMS */
116 struct msc_a *msc_a; /* MSC_A associated with this SMS */
117 unsigned long long sms_id; /* unique ID (in SQL database) of this SMS */
118 int failed_attempts; /* count of failed deliver attempts so far */
119 int resend; /* should we try re-sending it (now) ? */
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100120};
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100121
Harald Weltedc7d8412022-05-14 09:54:15 +0200122/* (global) state of the SMS queue. */
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100123struct gsm_sms_queue {
Harald Weltedc7d8412022-05-14 09:54:15 +0200124 struct osmo_timer_list resend_pending; /* timer triggering sms_resend_pending() */
125 struct osmo_timer_list push_queue; /* timer triggering sms_submit_pending() */
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100126 struct gsm_network *network;
Harald Weltedc7d8412022-05-14 09:54:15 +0200127 int max_fail; /* maximum number of delivery failures */
128 int max_pending; /* maximum number of gsm_sms_pending in RAM */
129 int pending; /* current number of gsm_sms_pending in RAM */
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100130
Harald Weltedc7d8412022-05-14 09:54:15 +0200131 struct llist_head pending_sms; /* list of gsm_sms_pending */
Harald Welte2483f1b2016-06-19 18:06:02 +0200132
Harald Weltedc7d8412022-05-14 09:54:15 +0200133 /* last MSISDN for which we read SMS from the database and created gsm_sms_pending records */
Vadim Yanitskiy8b0737f2019-05-25 19:27:17 +0700134 char last_msisdn[GSM23003_MSISDN_MAX_DIGITS+1];
Harald Welte63494a62022-05-15 13:03:57 +0200135
136 /* statistics / counters */
137 struct osmo_stat_item_group *statg;
138 struct rate_ctr_group *ctrg;
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100139};
140
Harald Welte63494a62022-05-15 13:03:57 +0200141/* private wrapper function to make sure we count all SMS delivery attempts */
142static void _gsm411_send_sms(struct gsm_network *net, struct vlr_subscr *vsub, struct gsm_sms *sms)
143{
144 smsq_rate_ctr_inc(net->sms_queue, SMSQ_CTR_SMS_DELIVERY_ATTEMPTS);
145 gsm411_send_sms(net, vsub, sms);
146}
147
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100148static int sms_subscr_cb(unsigned int, unsigned int, void *, void *);
149static int sms_sms_cb(unsigned int, unsigned int, void *, void *);
150
Harald Weltedc7d8412022-05-14 09:54:15 +0200151/* look-up a 'gsm_sms_pending' for the given sms_id; return NULL if none */
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100152static struct gsm_sms_pending *sms_find_pending(struct gsm_sms_queue *smsq,
Stefan Sperling87cba1f2018-01-22 17:05:37 +0100153 unsigned long long sms_id)
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100154{
155 struct gsm_sms_pending *pending;
156
157 llist_for_each_entry(pending, &smsq->pending_sms, entry) {
Stefan Sperling87cba1f2018-01-22 17:05:37 +0100158 if (pending->sms_id == sms_id)
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100159 return pending;
160 }
161
162 return NULL;
163}
164
Harald Weltedc7d8412022-05-14 09:54:15 +0200165/* do we currently have a gsm_sms_pending object for the given SMS id? */
Stefan Sperling87cba1f2018-01-22 17:05:37 +0100166int sms_queue_sms_is_pending(struct gsm_sms_queue *smsq, unsigned long long sms_id)
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100167{
Stefan Sperling87cba1f2018-01-22 17:05:37 +0100168 return sms_find_pending(smsq, sms_id) != NULL;
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100169}
170
Harald Weltedc7d8412022-05-14 09:54:15 +0200171/* find the first pending SMS (in RAM) for the given subscriber */
Holger Hans Peter Freyther074b2b22011-07-25 00:13:06 +0200172static struct gsm_sms_pending *sms_subscriber_find_pending(
173 struct gsm_sms_queue *smsq,
Harald Welte2483f1b2016-06-19 18:06:02 +0200174 struct vlr_subscr *vsub)
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100175{
176 struct gsm_sms_pending *pending;
177
178 llist_for_each_entry(pending, &smsq->pending_sms, entry) {
Harald Welte2483f1b2016-06-19 18:06:02 +0200179 if (pending->vsub == vsub)
Holger Hans Peter Freyther074b2b22011-07-25 00:13:06 +0200180 return pending;
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100181 }
182
Holger Hans Peter Freyther074b2b22011-07-25 00:13:06 +0200183 return NULL;
184}
185
Harald Weltedc7d8412022-05-14 09:54:15 +0200186/* do we have any pending SMS (in RAM) for the given subscriber? */
Holger Hans Peter Freyther074b2b22011-07-25 00:13:06 +0200187static int sms_subscriber_is_pending(struct gsm_sms_queue *smsq,
Harald Welte2483f1b2016-06-19 18:06:02 +0200188 struct vlr_subscr *vsub)
Holger Hans Peter Freyther074b2b22011-07-25 00:13:06 +0200189{
Harald Welte2483f1b2016-06-19 18:06:02 +0200190 return sms_subscriber_find_pending(smsq, vsub) != NULL;
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100191}
192
Harald Weltedc7d8412022-05-14 09:54:15 +0200193/* allocate a new gsm_sms_pending record and fill it with information from 'sms' */
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100194static struct gsm_sms_pending *sms_pending_from(struct gsm_sms_queue *smsq,
195 struct gsm_sms *sms)
196{
197 struct gsm_sms_pending *pending;
198
199 pending = talloc_zero(smsq, struct gsm_sms_pending);
200 if (!pending)
201 return NULL;
202
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100203 vlr_subscr_get(sms->receiver, VSUB_USE_SMS_PENDING);
204 pending->vsub = sms->receiver;
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100205 pending->sms_id = sms->id;
206 return pending;
207}
208
Harald Weltedc7d8412022-05-14 09:54:15 +0200209/* release a gsm_sms_pending object */
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100210static void sms_pending_free(struct gsm_sms_pending *pending)
211{
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100212 vlr_subscr_put(pending->vsub, VSUB_USE_SMS_PENDING);
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100213 llist_del(&pending->entry);
214 talloc_free(pending);
215}
216
Harald Weltedc7d8412022-05-14 09:54:15 +0200217/* this sets the 'resend' flag of the gsm_sms_pending and schedules
218 * the timer for re-sending */
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100219static void sms_pending_resend(struct gsm_sms_pending *pending)
220{
Harald Welte2483f1b2016-06-19 18:06:02 +0200221 struct gsm_network *net = pending->vsub->vlr->user_ctx;
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100222 struct gsm_sms_queue *smsq;
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100223 LOGP(DLSMS, LOGL_DEBUG,
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100224 "Scheduling resend of SMS %llu.\n", pending->sms_id);
225
226 pending->resend = 1;
227
Harald Welte2483f1b2016-06-19 18:06:02 +0200228 smsq = net->sms_queue;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200229 if (osmo_timer_pending(&smsq->resend_pending))
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100230 return;
231
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200232 osmo_timer_schedule(&smsq->resend_pending, 1, 0);
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100233}
234
Harald Weltedc7d8412022-05-14 09:54:15 +0200235/* call-back when a pending SMS has failed; try another re-send if number of
236 * attempts is < smsq->max_fail */
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100237static void sms_pending_failed(struct gsm_sms_pending *pending, int paging_error)
238{
Harald Welte2483f1b2016-06-19 18:06:02 +0200239 struct gsm_network *net = pending->vsub->vlr->user_ctx;
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100240 struct gsm_sms_queue *smsq;
241
Neels Hofmeyr34a36da2019-03-06 16:00:41 +0100242 pending->failed_attempts++;
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100243 LOGP(DLSMS, LOGL_NOTICE, "Sending SMS %llu failed %d times.\n",
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100244 pending->sms_id, pending->failed_attempts);
245
Harald Welte2483f1b2016-06-19 18:06:02 +0200246 smsq = net->sms_queue;
Neels Hofmeyr34a36da2019-03-06 16:00:41 +0100247 if (pending->failed_attempts < smsq->max_fail)
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100248 return sms_pending_resend(pending);
249
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100250 sms_pending_free(pending);
251 smsq->pending -= 1;
Harald Welte63494a62022-05-15 13:03:57 +0200252 smsq_stat_item_dec(smsq, SMSQ_STAT_SMS_RAM_PENDING);
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100253}
254
Harald Weltedc7d8412022-05-14 09:54:15 +0200255/* Resend all SMS that are scheduled for a resend. This is done to
256 * avoid an immediate failure. This iterates over all the (in RAM)
257 * pending_sms records, checks for resend == true, reads them from the
Harald Welte63494a62022-05-15 13:03:57 +0200258 * DB and attempts to send them via _gsm411_send_sms() */
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100259static void sms_resend_pending(void *_data)
260{
261 struct gsm_sms_pending *pending, *tmp;
262 struct gsm_sms_queue *smsq = _data;
263
264 llist_for_each_entry_safe(pending, tmp, &smsq->pending_sms, entry) {
265 struct gsm_sms *sms;
266 if (!pending->resend)
267 continue;
268
269 sms = db_sms_get(smsq->network, pending->sms_id);
270
271 /* the sms is gone? Move to the next */
272 if (!sms) {
273 sms_pending_free(pending);
274 smsq->pending -= 1;
Harald Welte63494a62022-05-15 13:03:57 +0200275 smsq_stat_item_dec(smsq, SMSQ_STAT_SMS_RAM_PENDING);
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100276 sms_queue_trigger(smsq);
277 } else {
278 pending->resend = 0;
Harald Welte63494a62022-05-15 13:03:57 +0200279 _gsm411_send_sms(smsq->network, sms->receiver, sms);
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100280 }
281 }
282}
283
Harald Welte2483f1b2016-06-19 18:06:02 +0200284/* Find the next pending SMS by cycling through the recipients. We could also
285 * cycle through the pending SMS, but that might cause us to keep trying to
286 * send SMS to the same few subscribers repeatedly while not servicing other
287 * subscribers for a long time. By walking the list of recipient MSISDNs, we
288 * ensure that all subscribers get their fair time to receive SMS. */
289struct gsm_sms *smsq_take_next_sms(struct gsm_network *net,
290 char *last_msisdn,
291 size_t last_msisdn_buflen)
Holger Hans Peter Freytherf7e23892010-12-25 17:45:23 +0100292{
293 struct gsm_sms *sms;
Harald Welte2483f1b2016-06-19 18:06:02 +0200294 int wrapped = 0;
295 int sanity = 100;
296 char started_with_msisdn[last_msisdn_buflen];
Holger Hans Peter Freytherf7e23892010-12-25 17:45:23 +0100297
Max98f74672018-02-05 12:57:06 +0100298 OSMO_STRLCPY_ARRAY(started_with_msisdn, last_msisdn);
Harald Welte2483f1b2016-06-19 18:06:02 +0200299
300 while (wrapped < 2 && (--sanity)) {
301 /* If we wrapped around and passed the first msisdn, we're
302 * through the entire SMS DB; end it. */
303 if (wrapped && strcmp(last_msisdn, started_with_msisdn) >= 0)
304 break;
305
306 sms = db_sms_get_next_unsent_rr_msisdn(net, last_msisdn, 9);
307 if (!sms) {
308 last_msisdn[0] = '\0';
Max5e2e9bd2018-02-06 19:31:08 +0100309 wrapped++;
Harald Welte2483f1b2016-06-19 18:06:02 +0200310 continue;
311 }
312
313 /* Whatever happens, next time around service another recipient
314 */
315 osmo_strlcpy(last_msisdn, sms->dst.addr, last_msisdn_buflen);
316
317 /* Is the subscriber attached? If not, go to next SMS */
Vadim Yanitskiy96262a72019-03-28 21:25:14 +0700318 if (!sms->receiver || !sms->receiver->lu_complete) {
319 LOGP(DLSMS, LOGL_DEBUG,
Pau Espin Pedrolf8af7762019-04-09 12:44:35 +0200320 "Subscriber %s%s is not attached, skipping SMS %llu\n",
321 sms->receiver ? "" : "MSISDN-",
322 sms->receiver ? vlr_subscr_msisdn_or_name(sms->receiver)
323 : sms->dst.addr, sms->id);
Vadim Yanitskiy96262a72019-03-28 21:25:14 +0700324 sms_free(sms);
Harald Welte2483f1b2016-06-19 18:06:02 +0200325 continue;
Vadim Yanitskiy96262a72019-03-28 21:25:14 +0700326 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200327
Holger Hans Peter Freytherf7e23892010-12-25 17:45:23 +0100328 return sms;
329 }
330
Harald Welte2483f1b2016-06-19 18:06:02 +0200331 DEBUGP(DLSMS, "SMS queue: no SMS to be sent\n");
332 return NULL;
Holger Hans Peter Freytherf7e23892010-12-25 17:45:23 +0100333}
334
Harald Weltedc7d8412022-05-14 09:54:15 +0200335/* read up to 'max_pending' pending SMS from the database and add them to the in-memory
336 * sms_queue; trigger the first delivery attempt. 'submit' in this context means
337 * "read from the database and add to the in-memory gsm_sms_queue" and is not to be
338 * confused with the SMS SUBMIT operation a MS performs when sending a MO-SMS. */
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100339static void sms_submit_pending(void *_data)
340{
341 struct gsm_sms_queue *smsq = _data;
342 int attempts = smsq->max_pending - smsq->pending;
Holger Hans Peter Freyther20384572010-12-25 19:28:44 +0100343 int initialized = 0;
344 unsigned long long first_sub = 0;
Holger Hans Peter Freyther5479fc82010-12-25 19:32:12 +0100345 int attempted = 0, rounds = 0;
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100346
Pau Espin Pedrolfe5b7042019-06-20 10:45:35 +0200347 LOGP(DLSMS, LOGL_DEBUG, "Attempting to send up to %d SMS\n", attempts);
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100348
Holger Hans Peter Freyther20384572010-12-25 19:28:44 +0100349 do {
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100350 struct gsm_sms_pending *pending;
351 struct gsm_sms *sms;
352
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100353
Harald Welte2483f1b2016-06-19 18:06:02 +0200354 sms = smsq_take_next_sms(smsq->network, smsq->last_msisdn,
355 sizeof(smsq->last_msisdn));
Neels Hofmeyr1e918c32016-05-09 21:48:53 +0200356 if (!sms) {
357 LOGP(DLSMS, LOGL_DEBUG, "Sending SMS done (%d attempted)\n",
358 attempted);
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100359 break;
Neels Hofmeyr1e918c32016-05-09 21:48:53 +0200360 }
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100361
Holger Hans Peter Freyther5479fc82010-12-25 19:32:12 +0100362 rounds += 1;
Neels Hofmeyr3d6a8be2019-11-18 07:15:56 +0100363 LOGP(DLSMS, LOGL_DEBUG, "Checking whether to send SMS %llu\n", sms->id);
Holger Hans Peter Freyther5479fc82010-12-25 19:32:12 +0100364
Holger Hans Peter Freyther20384572010-12-25 19:28:44 +0100365 /*
366 * This code needs to detect a loop. It assumes that no SMS
367 * will vanish during the time this is executed. We will remember
368 * the id of the first GSM subscriber we see and then will
369 * compare this. The Database code should make sure that we will
370 * see all other subscribers first before seeing this one again.
371 *
372 * It is always scary to have an infinite loop like this.
373 */
374 if (!initialized) {
375 first_sub = sms->receiver->id;
376 initialized = 1;
377 } else if (first_sub == sms->receiver->id) {
Neels Hofmeyr1e918c32016-05-09 21:48:53 +0200378 LOGP(DLSMS, LOGL_DEBUG, "Sending SMS done (loop) (%d attempted)\n",
379 attempted);
Holger Hans Peter Freyther20384572010-12-25 19:28:44 +0100380 sms_free(sms);
381 break;
382 }
383
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100384 /* no need to send a pending sms */
Stefan Sperling87cba1f2018-01-22 17:05:37 +0100385 if (sms_queue_sms_is_pending(smsq, sms->id)) {
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100386 LOGP(DLSMS, LOGL_DEBUG,
Holger Hans Peter Freytherdc53af62010-12-27 20:12:25 +0100387 "SMSqueue with pending sms: %llu. Skipping\n", sms->id);
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100388 sms_free(sms);
389 continue;
390 }
391
392 /* no need to send a SMS with the same receiver */
393 if (sms_subscriber_is_pending(smsq, sms->receiver)) {
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100394 LOGP(DLSMS, LOGL_DEBUG,
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100395 "SMSqueue with pending sub: %llu. Skipping\n", sms->receiver->id);
396 sms_free(sms);
397 continue;
398 }
399
Harald Weltedc7d8412022-05-14 09:54:15 +0200400 /* allocate a new gsm_sms_pending object in RAM */
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100401 pending = sms_pending_from(smsq, sms);
402 if (!pending) {
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100403 LOGP(DLSMS, LOGL_ERROR,
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100404 "Failed to create pending SMS entry.\n");
405 sms_free(sms);
406 continue;
407 }
408
Holger Hans Peter Freyther20384572010-12-25 19:28:44 +0100409 attempted += 1;
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100410 smsq->pending += 1;
Harald Welte63494a62022-05-15 13:03:57 +0200411 smsq_stat_item_inc(smsq, SMSQ_STAT_SMS_RAM_PENDING);
Holger Hans Peter Freyther701076e2010-12-28 14:25:25 +0100412 llist_add_tail(&pending->entry, &smsq->pending_sms);
Harald Welte63494a62022-05-15 13:03:57 +0200413 _gsm411_send_sms(smsq->network, sms->receiver, sms);
Holger Hans Peter Freyther5479fc82010-12-25 19:32:12 +0100414 } while (attempted < attempts && rounds < 1000);
Holger Hans Peter Freyther20384572010-12-25 19:28:44 +0100415
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100416 LOGP(DLSMS, LOGL_DEBUG, "SMSqueue added %d messages in %d rounds\n", attempted, rounds);
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100417}
418
Harald Weltedc7d8412022-05-14 09:54:15 +0200419/* obtain the next pending SMS for given subscriber from database,
420 * create gsm_sms_pending object and attempt first delivery. If there
421 * are no SMS pending for the given subscriber, call sms_submit_pending()
422 * to read more SMS (for any subscriber) into the in-RAM pending queue */
Harald Welte2483f1b2016-06-19 18:06:02 +0200423static void sms_send_next(struct vlr_subscr *vsub)
Holger Hans Peter Freyther93de8b22014-02-24 16:13:04 +0100424{
Harald Welte2483f1b2016-06-19 18:06:02 +0200425 struct gsm_network *net = vsub->vlr->user_ctx;
426 struct gsm_sms_queue *smsq = net->sms_queue;
Holger Hans Peter Freyther93de8b22014-02-24 16:13:04 +0100427 struct gsm_sms_pending *pending;
428 struct gsm_sms *sms;
429
430 /* the subscriber should not be in the queue */
Harald Welte2483f1b2016-06-19 18:06:02 +0200431 OSMO_ASSERT(!sms_subscriber_is_pending(smsq, vsub));
Holger Hans Peter Freyther93de8b22014-02-24 16:13:04 +0100432
433 /* check for more messages for this subscriber */
Harald Welte2483f1b2016-06-19 18:06:02 +0200434 sms = db_sms_get_unsent_for_subscr(vsub, UINT_MAX);
Holger Hans Peter Freyther93de8b22014-02-24 16:13:04 +0100435 if (!sms)
436 goto no_pending_sms;
437
Harald Welte2483f1b2016-06-19 18:06:02 +0200438 /* The sms should not be scheduled right now */
Stefan Sperling87cba1f2018-01-22 17:05:37 +0100439 OSMO_ASSERT(!sms_queue_sms_is_pending(smsq, sms->id));
Holger Hans Peter Freyther93de8b22014-02-24 16:13:04 +0100440
441 /* Remember that we deliver this SMS and send it */
442 pending = sms_pending_from(smsq, sms);
443 if (!pending) {
444 LOGP(DLSMS, LOGL_ERROR,
445 "Failed to create pending SMS entry.\n");
446 sms_free(sms);
447 goto no_pending_sms;
448 }
449
450 smsq->pending += 1;
Harald Welte63494a62022-05-15 13:03:57 +0200451 smsq_stat_item_inc(smsq, SMSQ_STAT_SMS_RAM_PENDING);
Holger Hans Peter Freyther93de8b22014-02-24 16:13:04 +0100452 llist_add_tail(&pending->entry, &smsq->pending_sms);
Harald Welte63494a62022-05-15 13:03:57 +0200453 _gsm411_send_sms(smsq->network, sms->receiver, sms);
Holger Hans Peter Freyther93de8b22014-02-24 16:13:04 +0100454 return;
455
456no_pending_sms:
457 /* Try to send the SMS to avoid the queue being stuck */
Harald Welte2483f1b2016-06-19 18:06:02 +0200458 sms_submit_pending(net->sms_queue);
Holger Hans Peter Freyther93de8b22014-02-24 16:13:04 +0100459}
460
Harald Weltedc7d8412022-05-14 09:54:15 +0200461/* Trigger a call to sms_submit_pending() in one second */
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100462int sms_queue_trigger(struct gsm_sms_queue *smsq)
463{
Neels Hofmeyr1e918c32016-05-09 21:48:53 +0200464 LOGP(DLSMS, LOGL_DEBUG, "Triggering SMS queue\n");
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200465 if (osmo_timer_pending(&smsq->push_queue))
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100466 return 0;
467
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200468 osmo_timer_schedule(&smsq->push_queue, 1, 0);
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100469 return 0;
470}
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100471
Harald Weltedc7d8412022-05-14 09:54:15 +0200472/* initialize the sms_queue subsystem and read the first batch of SMS from
473 * the database for delivery */
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100474int sms_queue_start(struct gsm_network *network, int max_pending)
475{
476 struct gsm_sms_queue *sms = talloc_zero(network, struct gsm_sms_queue);
477 if (!sms) {
478 LOGP(DMSC, LOGL_ERROR, "Failed to create the SMS queue.\n");
479 return -1;
480 }
481
Harald Welte63494a62022-05-15 13:03:57 +0200482 sms->statg = osmo_stat_item_group_alloc(sms, &smsq_statg_desc, 0);
483 if (!sms->statg)
484 goto err_free;
485
486 sms->ctrg = rate_ctr_group_alloc(sms, &smsq_ctrg_desc, 0);
487 if (!sms->ctrg)
488 goto err_statg;
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100489
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100490 network->sms_queue = sms;
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100491 INIT_LLIST_HEAD(&sms->pending_sms);
Holger Hans Peter Freythera37e3bc2010-12-25 17:43:03 +0100492 sms->max_fail = 1;
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100493 sms->network = network;
494 sms->max_pending = max_pending;
Pablo Neira Ayuso51215762017-05-08 20:57:52 +0200495 osmo_timer_setup(&sms->push_queue, sms_submit_pending, sms);
496 osmo_timer_setup(&sms->resend_pending, sms_resend_pending, sms);
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100497
Harald Welte63494a62022-05-15 13:03:57 +0200498 osmo_signal_register_handler(SS_SUBSCR, sms_subscr_cb, network);
499 osmo_signal_register_handler(SS_SMS, sms_sms_cb, network);
500
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100501 sms_submit_pending(sms);
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100502
503 return 0;
Harald Welte63494a62022-05-15 13:03:57 +0200504
505err_statg:
506 osmo_stat_item_group_free(sms->statg);
507err_free:
508 talloc_free(sms);
509
510 return -ENOMEM;
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100511}
512
Harald Weltedc7d8412022-05-14 09:54:15 +0200513/* call-back: Given subscriber is now ready for short messages. */
Harald Welte2483f1b2016-06-19 18:06:02 +0200514static int sub_ready_for_sm(struct gsm_network *net, struct vlr_subscr *vsub)
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100515{
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100516 struct gsm_sms *sms;
Holger Hans Peter Freyther074b2b22011-07-25 00:13:06 +0200517 struct gsm_sms_pending *pending;
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100518
Holger Hans Peter Freyther074b2b22011-07-25 00:13:06 +0200519 /*
520 * The code used to be very clever and tried to submit
521 * a SMS during the Location Updating Request. This has
522 * two issues:
523 * 1.) The Phone might not be ready yet, e.g. the C155
524 * will not respond to the Submit when it is booting.
525 * 2.) The queue is already trying to submit SMS to the
526 * user and by not responding to the paging request
527 * we will set the LAC back to 0. We would have to
528 * stop the paging and move things over.
529 *
530 * We need to be careful in what we try here.
531 */
532
533 /* check if we have pending requests */
Harald Welte2483f1b2016-06-19 18:06:02 +0200534 pending = sms_subscriber_find_pending(net->sms_queue, vsub);
Holger Hans Peter Freyther074b2b22011-07-25 00:13:06 +0200535 if (pending) {
536 LOGP(DMSC, LOGL_NOTICE,
537 "Pending paging while subscriber %llu attached.\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200538 vsub->id);
Holger Hans Peter Freyther074b2b22011-07-25 00:13:06 +0200539 return 0;
540 }
541
Holger Hans Peter Freyther074b2b22011-07-25 00:13:06 +0200542 /* Now try to deliver any pending SMS to this sub */
Harald Welte2483f1b2016-06-19 18:06:02 +0200543 sms = db_sms_get_unsent_for_subscr(vsub, UINT_MAX);
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100544 if (!sms)
545 return -1;
Vadim Yanitskiy24e025e2018-11-22 15:42:39 +0700546
Harald Welte63494a62022-05-15 13:03:57 +0200547 _gsm411_send_sms(net, vsub, sms);
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100548 return 0;
549}
550
Harald Weltedc7d8412022-05-14 09:54:15 +0200551/* call-back for SS_SUBSCR signals */
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100552static int sms_subscr_cb(unsigned int subsys, unsigned int signal,
553 void *handler_data, void *signal_data)
554{
Harald Welte2483f1b2016-06-19 18:06:02 +0200555 struct vlr_subscr *vsub = signal_data;
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100556
557 if (signal != S_SUBSCR_ATTACHED)
558 return 0;
559
560 /* this is readyForSM */
Harald Welte2483f1b2016-06-19 18:06:02 +0200561 return sub_ready_for_sm(handler_data, vsub);
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100562}
563
Harald Weltedc7d8412022-05-14 09:54:15 +0200564/* call-back for SS_SMS signals */
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100565static int sms_sms_cb(unsigned int subsys, unsigned int signal,
566 void *handler_data, void *signal_data)
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100567{
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100568 struct gsm_network *network = handler_data;
569 struct sms_signal_data *sig_sms = signal_data;
570 struct gsm_sms_pending *pending;
Harald Welte2483f1b2016-06-19 18:06:02 +0200571 struct vlr_subscr *vsub;
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100572
573 /* We got a new SMS and maybe should launch the queue again. */
574 if (signal == S_SMS_SUBMITTED || signal == S_SMS_SMMA) {
Holger Hans Peter Freyther024dc772014-02-24 14:29:27 +0100575 /* TODO: For SMMA we might want to re-use the radio connection. */
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100576 sms_queue_trigger(network->sms_queue);
577 return 0;
578 }
579
580 if (!sig_sms->sms)
581 return -1;
582
583
584 /*
585 * Find the entry of our queue. The SMS subsystem will submit
586 * sms that are not in our control as we just have a channel
587 * open anyway.
588 */
Stefan Sperling87cba1f2018-01-22 17:05:37 +0100589 pending = sms_find_pending(network->sms_queue, sig_sms->sms->id);
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100590 if (!pending)
591 return 0;
592
593 switch (signal) {
594 case S_SMS_DELIVERED:
Harald Welte63494a62022-05-15 13:03:57 +0200595 smsq_rate_ctr_inc(network->sms_queue, SMSQ_CTR_SMS_DELIVERY_ACK);
Holger Hans Peter Freyther93de8b22014-02-24 16:13:04 +0100596 /* Remember the subscriber and clear the pending entry */
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100597 network->sms_queue->pending -= 1;
Harald Welte63494a62022-05-15 13:03:57 +0200598 smsq_stat_item_dec(network->sms_queue, SMSQ_STAT_SMS_RAM_PENDING);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100599 vsub = pending->vsub;
600 vlr_subscr_get(vsub, __func__);
Stefan Sperling6ba2d5a2018-01-18 18:55:26 +0100601 db_sms_delete_sent_message_by_id(pending->sms_id);
Harald Welte37a3ae82018-04-14 20:53:36 +0200602 sms_pending_free(pending);
Holger Hans Peter Freyther93de8b22014-02-24 16:13:04 +0100603 /* Attempt to send another SMS to this subscriber */
Harald Welte2483f1b2016-06-19 18:06:02 +0200604 sms_send_next(vsub);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100605 vlr_subscr_put(vsub, __func__);
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100606 break;
607 case S_SMS_MEM_EXCEEDED:
Harald Welte63494a62022-05-15 13:03:57 +0200608 smsq_rate_ctr_inc(network->sms_queue, SMSQ_CTR_SMS_DELIVERY_NOMEM);
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100609 network->sms_queue->pending -= 1;
Harald Welte63494a62022-05-15 13:03:57 +0200610 smsq_stat_item_dec(network->sms_queue, SMSQ_STAT_SMS_RAM_PENDING);
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100611 sms_pending_free(pending);
612 sms_queue_trigger(network->sms_queue);
613 break;
614 case S_SMS_UNKNOWN_ERROR:
615 /*
616 * There can be many reasons for this failure. E.g. the paging
617 * timed out, the subscriber was not paged at all, or there was
618 * a protocol error. The current strategy is to try sending the
619 * next SMS for busy/oom and to retransmit when we have paged.
620 *
621 * When the paging expires three times we will disable the
622 * subscriber. If we have some kind of other transmit error we
623 * should flag the SMS as bad.
624 */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100625 if (sig_sms->paging_result) {
Harald Welte63494a62022-05-15 13:03:57 +0200626 smsq_rate_ctr_inc(network->sms_queue, SMSQ_CTR_SMS_DELIVERY_ERR);
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100627 /* BAD SMS? */
628 db_sms_inc_deliver_attempts(sig_sms->sms);
629 sms_pending_failed(pending, 0);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100630 } else {
Harald Welte63494a62022-05-15 13:03:57 +0200631 smsq_rate_ctr_inc(network->sms_queue, SMSQ_CTR_SMS_DELIVERY_TIMEOUT);
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100632 sms_pending_failed(pending, 1);
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100633 }
634 break;
635 default:
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100636 LOGP(DLSMS, LOGL_ERROR, "Unhandled result: %d\n",
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100637 sig_sms->paging_result);
638 }
639
Stefan Sperling14e05172018-01-22 17:31:20 +0100640 /* While here, attempt to remove an expired SMS from the DB. */
641 db_sms_delete_oldest_expired_message();
642
Holger Hans Peter Freyther17164062010-12-24 21:39:55 +0100643 return 0;
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100644}
Holger Hans Peter Freyther81c0e252010-12-25 14:08:00 +0100645
646/* VTY helper functions */
647int sms_queue_stats(struct gsm_sms_queue *smsq, struct vty *vty)
648{
649 struct gsm_sms_pending *pending;
650
651 vty_out(vty, "SMSqueue with max_pending: %d pending: %d%s",
652 smsq->max_pending, smsq->pending, VTY_NEWLINE);
653
654 llist_for_each_entry(pending, &smsq->pending_sms, entry)
Holger Hans Peter Freyther583e9ae2010-12-27 20:19:48 +0100655 vty_out(vty, " SMS Pending for Subscriber: %llu SMS: %llu Failed: %d.%s",
Harald Welte2483f1b2016-06-19 18:06:02 +0200656 pending->vsub->id, pending->sms_id,
Holger Hans Peter Freyther583e9ae2010-12-27 20:19:48 +0100657 pending->failed_attempts, VTY_NEWLINE);
Holger Hans Peter Freyther81c0e252010-12-25 14:08:00 +0100658 return 0;
659}
Holger Hans Peter Freyther3c6f6c22010-12-25 14:25:12 +0100660
661int sms_queue_set_max_pending(struct gsm_sms_queue *smsq, int max_pending)
662{
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100663 LOGP(DLSMS, LOGL_NOTICE, "SMSqueue old max: %d new: %d\n",
Holger Hans Peter Freyther3c6f6c22010-12-25 14:25:12 +0100664 smsq->max_pending, max_pending);
665 smsq->max_pending = max_pending;
666 return 0;
667}
Holger Hans Peter Freyther4dcc5e52010-12-25 14:38:30 +0100668
Holger Hans Peter Freyther994dcbb2010-12-25 14:50:50 +0100669int sms_queue_set_max_failure(struct gsm_sms_queue *smsq, int max_fail)
670{
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100671 LOGP(DLSMS, LOGL_NOTICE, "SMSqueue max failure old: %d new: %d\n",
Holger Hans Peter Freyther994dcbb2010-12-25 14:50:50 +0100672 smsq->max_fail, max_fail);
673 smsq->max_fail = max_fail;
674 return 0;
675}
676
Holger Hans Peter Freyther4dcc5e52010-12-25 14:38:30 +0100677int sms_queue_clear(struct gsm_sms_queue *smsq)
678{
679 struct gsm_sms_pending *pending, *tmp;
680
681 llist_for_each_entry_safe(pending, tmp, &smsq->pending_sms, entry) {
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100682 LOGP(DLSMS, LOGL_NOTICE,
Harald Welte2483f1b2016-06-19 18:06:02 +0200683 "SMSqueue clearing for sub %llu\n", pending->vsub->id);
Holger Hans Peter Freyther4dcc5e52010-12-25 14:38:30 +0100684 sms_pending_free(pending);
685 }
686
Holger Hans Peter Freyther96e9f082010-12-28 14:09:07 +0100687 smsq->pending = 0;
Harald Welte63494a62022-05-15 13:03:57 +0200688 smsq_stat_item_set(smsq, SMSQ_STAT_SMS_RAM_PENDING, 0);
Holger Hans Peter Freyther4dcc5e52010-12-25 14:38:30 +0100689 return 0;
690}