Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 1 | /* 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 Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 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 |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 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 |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 14 | * GNU Affero General Public License for more details. |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 15 | * |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 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/>. |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 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> |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 37 | #include <openbsc/gsm_subscriber.h> |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 38 | #include <openbsc/signal.h> |
| 39 | |
Pablo Neira Ayuso | dd5fff4 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 40 | #include <osmocom/core/talloc.h> |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 41 | |
Holger Hans Peter Freyther | 6ec8098 | 2010-12-25 14:08:00 +0100 | [diff] [blame] | 42 | #include <osmocom/vty/vty.h> |
| 43 | |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 44 | /* |
| 45 | * One pending SMS that we wait for. |
| 46 | */ |
| 47 | struct 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 Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 55 | |
| 56 | struct gsm_sms_queue { |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 57 | struct osmo_timer_list resend_pending; |
| 58 | struct osmo_timer_list push_queue; |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 59 | struct gsm_network *network; |
Holger Hans Peter Freyther | 4334e4e | 2010-12-25 14:46:54 +0100 | [diff] [blame] | 60 | int max_fail; |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 61 | int max_pending; |
| 62 | int pending; |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 63 | |
| 64 | struct llist_head pending_sms; |
| 65 | unsigned long long last_subscr_id; |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 66 | }; |
| 67 | |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 68 | static int sms_subscr_cb(unsigned int, unsigned int, void *, void *); |
| 69 | static int sms_sms_cb(unsigned int, unsigned int, void *, void *); |
| 70 | |
| 71 | static 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 | |
| 84 | static 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 | |
Holger Hans Peter Freyther | 735a280 | 2011-07-25 00:13:06 +0200 | [diff] [blame] | 89 | static struct gsm_sms_pending *sms_subscriber_find_pending( |
| 90 | struct gsm_sms_queue *smsq, |
| 91 | struct gsm_subscriber *subscr) |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 92 | { |
| 93 | struct gsm_sms_pending *pending; |
| 94 | |
| 95 | llist_for_each_entry(pending, &smsq->pending_sms, entry) { |
| 96 | if (pending->subscr == subscr) |
Holger Hans Peter Freyther | 735a280 | 2011-07-25 00:13:06 +0200 | [diff] [blame] | 97 | return pending; |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 98 | } |
| 99 | |
Holger Hans Peter Freyther | 735a280 | 2011-07-25 00:13:06 +0200 | [diff] [blame] | 100 | return NULL; |
| 101 | } |
| 102 | |
| 103 | static int sms_subscriber_is_pending(struct gsm_sms_queue *smsq, |
| 104 | struct gsm_subscriber *subscr) |
| 105 | { |
| 106 | return sms_subscriber_find_pending(smsq, subscr) != NULL; |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static struct gsm_sms_pending *sms_pending_from(struct gsm_sms_queue *smsq, |
| 110 | struct gsm_sms *sms) |
| 111 | { |
| 112 | struct gsm_sms_pending *pending; |
| 113 | |
| 114 | pending = talloc_zero(smsq, struct gsm_sms_pending); |
| 115 | if (!pending) |
| 116 | return NULL; |
| 117 | |
| 118 | pending->subscr = subscr_get(sms->receiver); |
| 119 | pending->sms_id = sms->id; |
| 120 | return pending; |
| 121 | } |
| 122 | |
| 123 | static void sms_pending_free(struct gsm_sms_pending *pending) |
| 124 | { |
| 125 | subscr_put(pending->subscr); |
| 126 | llist_del(&pending->entry); |
| 127 | talloc_free(pending); |
| 128 | } |
| 129 | |
| 130 | static void sms_pending_resend(struct gsm_sms_pending *pending) |
| 131 | { |
| 132 | struct gsm_sms_queue *smsq; |
Holger Hans Peter Freyther | d0a34ed | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 133 | LOGP(DLSMS, LOGL_DEBUG, |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 134 | "Scheduling resend of SMS %llu.\n", pending->sms_id); |
| 135 | |
| 136 | pending->resend = 1; |
| 137 | |
Jacob Erlbeck | da8770b | 2014-12-03 09:28:24 +0100 | [diff] [blame^] | 138 | smsq = pending->subscr->group->net->sms_queue; |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 139 | if (osmo_timer_pending(&smsq->resend_pending)) |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 140 | return; |
| 141 | |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 142 | osmo_timer_schedule(&smsq->resend_pending, 1, 0); |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static void sms_pending_failed(struct gsm_sms_pending *pending, int paging_error) |
| 146 | { |
| 147 | struct gsm_sms_queue *smsq; |
| 148 | |
Holger Hans Peter Freyther | d0a34ed | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 149 | LOGP(DLSMS, LOGL_NOTICE, "Sending SMS %llu failed %d times.\n", |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 150 | pending->sms_id, pending->failed_attempts); |
| 151 | |
Jacob Erlbeck | da8770b | 2014-12-03 09:28:24 +0100 | [diff] [blame^] | 152 | smsq = pending->subscr->group->net->sms_queue; |
Holger Hans Peter Freyther | 4334e4e | 2010-12-25 14:46:54 +0100 | [diff] [blame] | 153 | if (++pending->failed_attempts < smsq->max_fail) |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 154 | return sms_pending_resend(pending); |
| 155 | |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 156 | sms_pending_free(pending); |
| 157 | smsq->pending -= 1; |
| 158 | sms_queue_trigger(smsq); |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * Resend all SMS that are scheduled for a resend. This is done to |
| 163 | * avoid an immediate failure. |
| 164 | */ |
| 165 | static void sms_resend_pending(void *_data) |
| 166 | { |
| 167 | struct gsm_sms_pending *pending, *tmp; |
| 168 | struct gsm_sms_queue *smsq = _data; |
| 169 | |
| 170 | llist_for_each_entry_safe(pending, tmp, &smsq->pending_sms, entry) { |
| 171 | struct gsm_sms *sms; |
| 172 | if (!pending->resend) |
| 173 | continue; |
| 174 | |
| 175 | sms = db_sms_get(smsq->network, pending->sms_id); |
| 176 | |
| 177 | /* the sms is gone? Move to the next */ |
| 178 | if (!sms) { |
| 179 | sms_pending_free(pending); |
| 180 | smsq->pending -= 1; |
| 181 | sms_queue_trigger(smsq); |
| 182 | } else { |
| 183 | pending->resend = 0; |
| 184 | gsm411_send_sms_subscr(sms->receiver, sms); |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
Holger Hans Peter Freyther | e41db2a | 2010-12-25 17:45:23 +0100 | [diff] [blame] | 189 | static struct gsm_sms *take_next_sms(struct gsm_sms_queue *smsq) |
| 190 | { |
| 191 | struct gsm_sms *sms; |
| 192 | |
| 193 | sms = db_sms_get_unsent_by_subscr(smsq->network, smsq->last_subscr_id, 10); |
| 194 | if (sms) { |
| 195 | smsq->last_subscr_id = sms->receiver->id + 1; |
| 196 | return sms; |
| 197 | } |
| 198 | |
| 199 | /* need to wrap around */ |
| 200 | smsq->last_subscr_id = 0; |
| 201 | sms = db_sms_get_unsent_by_subscr(smsq->network, |
| 202 | smsq->last_subscr_id, 10); |
| 203 | if (sms) |
| 204 | smsq->last_subscr_id = sms->receiver->id + 1; |
| 205 | return sms; |
| 206 | } |
| 207 | |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 208 | /** |
| 209 | * I will submit up to max_pending - pending SMS to the |
| 210 | * subsystem. |
| 211 | */ |
| 212 | static void sms_submit_pending(void *_data) |
| 213 | { |
| 214 | struct gsm_sms_queue *smsq = _data; |
| 215 | int attempts = smsq->max_pending - smsq->pending; |
Holger Hans Peter Freyther | 3e479cc | 2010-12-25 19:28:44 +0100 | [diff] [blame] | 216 | int initialized = 0; |
| 217 | unsigned long long first_sub = 0; |
Holger Hans Peter Freyther | 8ba6f85 | 2010-12-25 19:32:12 +0100 | [diff] [blame] | 218 | int attempted = 0, rounds = 0; |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 219 | |
Holger Hans Peter Freyther | d0a34ed | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 220 | LOGP(DLSMS, LOGL_NOTICE, "Attempting to send %d SMS\n", attempts); |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 221 | |
Holger Hans Peter Freyther | 3e479cc | 2010-12-25 19:28:44 +0100 | [diff] [blame] | 222 | do { |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 223 | struct gsm_sms_pending *pending; |
| 224 | struct gsm_sms *sms; |
| 225 | |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 226 | |
Holger Hans Peter Freyther | e41db2a | 2010-12-25 17:45:23 +0100 | [diff] [blame] | 227 | sms = take_next_sms(smsq); |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 228 | if (!sms) |
| 229 | break; |
| 230 | |
Holger Hans Peter Freyther | 8ba6f85 | 2010-12-25 19:32:12 +0100 | [diff] [blame] | 231 | rounds += 1; |
| 232 | |
Holger Hans Peter Freyther | 3e479cc | 2010-12-25 19:28:44 +0100 | [diff] [blame] | 233 | /* |
| 234 | * This code needs to detect a loop. It assumes that no SMS |
| 235 | * will vanish during the time this is executed. We will remember |
| 236 | * the id of the first GSM subscriber we see and then will |
| 237 | * compare this. The Database code should make sure that we will |
| 238 | * see all other subscribers first before seeing this one again. |
| 239 | * |
| 240 | * It is always scary to have an infinite loop like this. |
| 241 | */ |
| 242 | if (!initialized) { |
| 243 | first_sub = sms->receiver->id; |
| 244 | initialized = 1; |
| 245 | } else if (first_sub == sms->receiver->id) { |
| 246 | sms_free(sms); |
| 247 | break; |
| 248 | } |
| 249 | |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 250 | /* no need to send a pending sms */ |
| 251 | if (sms_is_in_pending(smsq, sms)) { |
Holger Hans Peter Freyther | d0a34ed | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 252 | LOGP(DLSMS, LOGL_DEBUG, |
Holger Hans Peter Freyther | 487b9aa | 2010-12-27 20:12:25 +0100 | [diff] [blame] | 253 | "SMSqueue with pending sms: %llu. Skipping\n", sms->id); |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 254 | sms_free(sms); |
| 255 | continue; |
| 256 | } |
| 257 | |
| 258 | /* no need to send a SMS with the same receiver */ |
| 259 | if (sms_subscriber_is_pending(smsq, sms->receiver)) { |
Holger Hans Peter Freyther | d0a34ed | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 260 | LOGP(DLSMS, LOGL_DEBUG, |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 261 | "SMSqueue with pending sub: %llu. Skipping\n", sms->receiver->id); |
| 262 | sms_free(sms); |
| 263 | continue; |
| 264 | } |
| 265 | |
| 266 | pending = sms_pending_from(smsq, sms); |
| 267 | if (!pending) { |
Holger Hans Peter Freyther | d0a34ed | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 268 | LOGP(DLSMS, LOGL_ERROR, |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 269 | "Failed to create pending SMS entry.\n"); |
| 270 | sms_free(sms); |
| 271 | continue; |
| 272 | } |
| 273 | |
Holger Hans Peter Freyther | 3e479cc | 2010-12-25 19:28:44 +0100 | [diff] [blame] | 274 | attempted += 1; |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 275 | smsq->pending += 1; |
Holger Hans Peter Freyther | b01ca46 | 2010-12-28 14:25:25 +0100 | [diff] [blame] | 276 | llist_add_tail(&pending->entry, &smsq->pending_sms); |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 277 | gsm411_send_sms_subscr(sms->receiver, sms); |
Holger Hans Peter Freyther | 8ba6f85 | 2010-12-25 19:32:12 +0100 | [diff] [blame] | 278 | } while (attempted < attempts && rounds < 1000); |
Holger Hans Peter Freyther | 3e479cc | 2010-12-25 19:28:44 +0100 | [diff] [blame] | 279 | |
Holger Hans Peter Freyther | d0a34ed | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 280 | LOGP(DLSMS, LOGL_DEBUG, "SMSqueue added %d messages in %d rounds\n", attempted, rounds); |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 281 | } |
| 282 | |
Holger Hans Peter Freyther | e057698 | 2014-02-24 16:13:04 +0100 | [diff] [blame] | 283 | /** |
| 284 | * Send the next SMS or trigger the queue |
| 285 | */ |
| 286 | static void sms_send_next(struct gsm_subscriber *subscr) |
| 287 | { |
Jacob Erlbeck | da8770b | 2014-12-03 09:28:24 +0100 | [diff] [blame^] | 288 | struct gsm_sms_queue *smsq = subscr->group->net->sms_queue; |
Holger Hans Peter Freyther | e057698 | 2014-02-24 16:13:04 +0100 | [diff] [blame] | 289 | struct gsm_sms_pending *pending; |
| 290 | struct gsm_sms *sms; |
| 291 | |
| 292 | /* the subscriber should not be in the queue */ |
| 293 | OSMO_ASSERT(!sms_subscriber_is_pending(smsq, subscr)); |
| 294 | |
| 295 | /* check for more messages for this subscriber */ |
| 296 | sms = db_sms_get_unsent_for_subscr(subscr); |
| 297 | if (!sms) |
| 298 | goto no_pending_sms; |
| 299 | |
| 300 | /* No sms should be scheduled right now */ |
| 301 | OSMO_ASSERT(!sms_is_in_pending(smsq, sms)); |
| 302 | |
| 303 | /* Remember that we deliver this SMS and send it */ |
| 304 | pending = sms_pending_from(smsq, sms); |
| 305 | if (!pending) { |
| 306 | LOGP(DLSMS, LOGL_ERROR, |
| 307 | "Failed to create pending SMS entry.\n"); |
| 308 | sms_free(sms); |
| 309 | goto no_pending_sms; |
| 310 | } |
| 311 | |
| 312 | smsq->pending += 1; |
| 313 | llist_add_tail(&pending->entry, &smsq->pending_sms); |
| 314 | gsm411_send_sms_subscr(sms->receiver, sms); |
| 315 | return; |
| 316 | |
| 317 | no_pending_sms: |
| 318 | /* Try to send the SMS to avoid the queue being stuck */ |
Jacob Erlbeck | da8770b | 2014-12-03 09:28:24 +0100 | [diff] [blame^] | 319 | sms_submit_pending(subscr->group->net->sms_queue); |
Holger Hans Peter Freyther | e057698 | 2014-02-24 16:13:04 +0100 | [diff] [blame] | 320 | } |
| 321 | |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 322 | /* |
| 323 | * Kick off the queue again. |
| 324 | */ |
| 325 | int sms_queue_trigger(struct gsm_sms_queue *smsq) |
| 326 | { |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 327 | if (osmo_timer_pending(&smsq->push_queue)) |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 328 | return 0; |
| 329 | |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 330 | osmo_timer_schedule(&smsq->push_queue, 1, 0); |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 331 | return 0; |
| 332 | } |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 333 | |
| 334 | int sms_queue_start(struct gsm_network *network, int max_pending) |
| 335 | { |
| 336 | struct gsm_sms_queue *sms = talloc_zero(network, struct gsm_sms_queue); |
| 337 | if (!sms) { |
| 338 | LOGP(DMSC, LOGL_ERROR, "Failed to create the SMS queue.\n"); |
| 339 | return -1; |
| 340 | } |
| 341 | |
Pablo Neira Ayuso | ef717c6 | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 342 | osmo_signal_register_handler(SS_SUBSCR, sms_subscr_cb, network); |
| 343 | osmo_signal_register_handler(SS_SMS, sms_sms_cb, network); |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 344 | |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 345 | network->sms_queue = sms; |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 346 | INIT_LLIST_HEAD(&sms->pending_sms); |
Holger Hans Peter Freyther | 4871046 | 2010-12-25 17:43:03 +0100 | [diff] [blame] | 347 | sms->max_fail = 1; |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 348 | sms->network = network; |
| 349 | sms->max_pending = max_pending; |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 350 | sms->push_queue.data = sms; |
| 351 | sms->push_queue.cb = sms_submit_pending; |
| 352 | sms->resend_pending.data = sms; |
| 353 | sms->resend_pending.cb = sms_resend_pending; |
| 354 | |
| 355 | sms_submit_pending(sms); |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 356 | |
| 357 | return 0; |
| 358 | } |
| 359 | |
Holger Hans Peter Freyther | 735a280 | 2011-07-25 00:13:06 +0200 | [diff] [blame] | 360 | static int sub_ready_for_sm(struct gsm_network *net, struct gsm_subscriber *subscr) |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 361 | { |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 362 | struct gsm_sms *sms; |
Holger Hans Peter Freyther | 735a280 | 2011-07-25 00:13:06 +0200 | [diff] [blame] | 363 | struct gsm_sms_pending *pending; |
| 364 | struct gsm_subscriber_connection *conn; |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 365 | |
Holger Hans Peter Freyther | 735a280 | 2011-07-25 00:13:06 +0200 | [diff] [blame] | 366 | /* |
| 367 | * The code used to be very clever and tried to submit |
| 368 | * a SMS during the Location Updating Request. This has |
| 369 | * two issues: |
| 370 | * 1.) The Phone might not be ready yet, e.g. the C155 |
| 371 | * will not respond to the Submit when it is booting. |
| 372 | * 2.) The queue is already trying to submit SMS to the |
| 373 | * user and by not responding to the paging request |
| 374 | * we will set the LAC back to 0. We would have to |
| 375 | * stop the paging and move things over. |
| 376 | * |
| 377 | * We need to be careful in what we try here. |
| 378 | */ |
| 379 | |
| 380 | /* check if we have pending requests */ |
| 381 | pending = sms_subscriber_find_pending(net->sms_queue, subscr); |
| 382 | if (pending) { |
| 383 | LOGP(DMSC, LOGL_NOTICE, |
| 384 | "Pending paging while subscriber %llu attached.\n", |
| 385 | subscr->id); |
Holger Hans Peter Freyther | 735a280 | 2011-07-25 00:13:06 +0200 | [diff] [blame] | 386 | return 0; |
| 387 | } |
| 388 | |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 389 | conn = connection_for_subscr(subscr); |
| 390 | if (!conn) |
| 391 | return -1; |
Holger Hans Peter Freyther | 735a280 | 2011-07-25 00:13:06 +0200 | [diff] [blame] | 392 | |
| 393 | /* Now try to deliver any pending SMS to this sub */ |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 394 | sms = db_sms_get_unsent_for_subscr(subscr); |
| 395 | if (!sms) |
| 396 | return -1; |
| 397 | gsm411_send_sms(conn, sms); |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | static int sms_subscr_cb(unsigned int subsys, unsigned int signal, |
| 402 | void *handler_data, void *signal_data) |
| 403 | { |
| 404 | struct gsm_subscriber *subscr = signal_data; |
| 405 | |
| 406 | if (signal != S_SUBSCR_ATTACHED) |
| 407 | return 0; |
| 408 | |
| 409 | /* this is readyForSM */ |
Holger Hans Peter Freyther | 735a280 | 2011-07-25 00:13:06 +0200 | [diff] [blame] | 410 | return sub_ready_for_sm(handler_data, subscr); |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 411 | } |
| 412 | |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 413 | static int sms_sms_cb(unsigned int subsys, unsigned int signal, |
| 414 | void *handler_data, void *signal_data) |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 415 | { |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 416 | struct gsm_network *network = handler_data; |
| 417 | struct sms_signal_data *sig_sms = signal_data; |
| 418 | struct gsm_sms_pending *pending; |
Holger Hans Peter Freyther | e057698 | 2014-02-24 16:13:04 +0100 | [diff] [blame] | 419 | struct gsm_subscriber *subscr; |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 420 | |
| 421 | /* We got a new SMS and maybe should launch the queue again. */ |
| 422 | if (signal == S_SMS_SUBMITTED || signal == S_SMS_SMMA) { |
Holger Hans Peter Freyther | a0cfc32 | 2014-02-24 14:29:27 +0100 | [diff] [blame] | 423 | /* TODO: For SMMA we might want to re-use the radio connection. */ |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 424 | sms_queue_trigger(network->sms_queue); |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | if (!sig_sms->sms) |
| 429 | return -1; |
| 430 | |
| 431 | |
| 432 | /* |
| 433 | * Find the entry of our queue. The SMS subsystem will submit |
| 434 | * sms that are not in our control as we just have a channel |
| 435 | * open anyway. |
| 436 | */ |
| 437 | pending = sms_find_pending(network->sms_queue, sig_sms->sms); |
| 438 | if (!pending) |
| 439 | return 0; |
| 440 | |
| 441 | switch (signal) { |
| 442 | case S_SMS_DELIVERED: |
Holger Hans Peter Freyther | e057698 | 2014-02-24 16:13:04 +0100 | [diff] [blame] | 443 | /* Remember the subscriber and clear the pending entry */ |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 444 | network->sms_queue->pending -= 1; |
Holger Hans Peter Freyther | e057698 | 2014-02-24 16:13:04 +0100 | [diff] [blame] | 445 | subscr = subscr_get(pending->subscr); |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 446 | sms_pending_free(pending); |
Holger Hans Peter Freyther | e057698 | 2014-02-24 16:13:04 +0100 | [diff] [blame] | 447 | /* Attempt to send another SMS to this subscriber */ |
| 448 | sms_send_next(subscr); |
| 449 | subscr_put(subscr); |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 450 | break; |
| 451 | case S_SMS_MEM_EXCEEDED: |
| 452 | network->sms_queue->pending -= 1; |
| 453 | sms_pending_free(pending); |
| 454 | sms_queue_trigger(network->sms_queue); |
| 455 | break; |
| 456 | case S_SMS_UNKNOWN_ERROR: |
| 457 | /* |
| 458 | * There can be many reasons for this failure. E.g. the paging |
| 459 | * timed out, the subscriber was not paged at all, or there was |
| 460 | * a protocol error. The current strategy is to try sending the |
| 461 | * next SMS for busy/oom and to retransmit when we have paged. |
| 462 | * |
| 463 | * When the paging expires three times we will disable the |
| 464 | * subscriber. If we have some kind of other transmit error we |
| 465 | * should flag the SMS as bad. |
| 466 | */ |
| 467 | switch (sig_sms->paging_result) { |
| 468 | case 0: |
| 469 | /* BAD SMS? */ |
| 470 | db_sms_inc_deliver_attempts(sig_sms->sms); |
| 471 | sms_pending_failed(pending, 0); |
| 472 | break; |
| 473 | case GSM_PAGING_EXPIRED: |
| 474 | sms_pending_failed(pending, 1); |
| 475 | break; |
| 476 | |
| 477 | case GSM_PAGING_OOM: |
| 478 | case GSM_PAGING_BUSY: |
| 479 | network->sms_queue->pending -= 1; |
| 480 | sms_pending_free(pending); |
| 481 | sms_queue_trigger(network->sms_queue); |
| 482 | break; |
| 483 | default: |
Holger Hans Peter Freyther | d0a34ed | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 484 | LOGP(DLSMS, LOGL_ERROR, "Unhandled result: %d\n", |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 485 | sig_sms->paging_result); |
| 486 | } |
| 487 | break; |
| 488 | default: |
Holger Hans Peter Freyther | d0a34ed | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 489 | LOGP(DLSMS, LOGL_ERROR, "Unhandled result: %d\n", |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 490 | sig_sms->paging_result); |
| 491 | } |
| 492 | |
| 493 | return 0; |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 494 | } |
Holger Hans Peter Freyther | 6ec8098 | 2010-12-25 14:08:00 +0100 | [diff] [blame] | 495 | |
| 496 | /* VTY helper functions */ |
| 497 | int sms_queue_stats(struct gsm_sms_queue *smsq, struct vty *vty) |
| 498 | { |
| 499 | struct gsm_sms_pending *pending; |
| 500 | |
| 501 | vty_out(vty, "SMSqueue with max_pending: %d pending: %d%s", |
| 502 | smsq->max_pending, smsq->pending, VTY_NEWLINE); |
| 503 | |
| 504 | llist_for_each_entry(pending, &smsq->pending_sms, entry) |
Holger Hans Peter Freyther | eed400a | 2010-12-27 20:19:48 +0100 | [diff] [blame] | 505 | vty_out(vty, " SMS Pending for Subscriber: %llu SMS: %llu Failed: %d.%s", |
| 506 | pending->subscr->id, pending->sms_id, |
| 507 | pending->failed_attempts, VTY_NEWLINE); |
Holger Hans Peter Freyther | 6ec8098 | 2010-12-25 14:08:00 +0100 | [diff] [blame] | 508 | return 0; |
| 509 | } |
Holger Hans Peter Freyther | 96b4d35 | 2010-12-25 14:25:12 +0100 | [diff] [blame] | 510 | |
| 511 | int sms_queue_set_max_pending(struct gsm_sms_queue *smsq, int max_pending) |
| 512 | { |
Holger Hans Peter Freyther | d0a34ed | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 513 | LOGP(DLSMS, LOGL_NOTICE, "SMSqueue old max: %d new: %d\n", |
Holger Hans Peter Freyther | 96b4d35 | 2010-12-25 14:25:12 +0100 | [diff] [blame] | 514 | smsq->max_pending, max_pending); |
| 515 | smsq->max_pending = max_pending; |
| 516 | return 0; |
| 517 | } |
Holger Hans Peter Freyther | 416c7de | 2010-12-25 14:38:30 +0100 | [diff] [blame] | 518 | |
Holger Hans Peter Freyther | 70428d9 | 2010-12-25 14:50:50 +0100 | [diff] [blame] | 519 | int sms_queue_set_max_failure(struct gsm_sms_queue *smsq, int max_fail) |
| 520 | { |
Holger Hans Peter Freyther | d0a34ed | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 521 | LOGP(DLSMS, LOGL_NOTICE, "SMSqueue max failure old: %d new: %d\n", |
Holger Hans Peter Freyther | 70428d9 | 2010-12-25 14:50:50 +0100 | [diff] [blame] | 522 | smsq->max_fail, max_fail); |
| 523 | smsq->max_fail = max_fail; |
| 524 | return 0; |
| 525 | } |
| 526 | |
Holger Hans Peter Freyther | 416c7de | 2010-12-25 14:38:30 +0100 | [diff] [blame] | 527 | int sms_queue_clear(struct gsm_sms_queue *smsq) |
| 528 | { |
| 529 | struct gsm_sms_pending *pending, *tmp; |
| 530 | |
| 531 | llist_for_each_entry_safe(pending, tmp, &smsq->pending_sms, entry) { |
Holger Hans Peter Freyther | d0a34ed | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 532 | LOGP(DLSMS, LOGL_NOTICE, |
Holger Hans Peter Freyther | 416c7de | 2010-12-25 14:38:30 +0100 | [diff] [blame] | 533 | "SMSqueue clearing for sub %llu\n", pending->subscr->id); |
| 534 | sms_pending_free(pending); |
| 535 | } |
| 536 | |
Holger Hans Peter Freyther | 7142b28 | 2010-12-28 14:09:07 +0100 | [diff] [blame] | 537 | smsq->pending = 0; |
Holger Hans Peter Freyther | 416c7de | 2010-12-25 14:38:30 +0100 | [diff] [blame] | 538 | return 0; |
| 539 | } |