diff options
Diffstat (limited to 'openbsc/src/libmsc')
-rw-r--r-- | openbsc/src/libmsc/db.c | 61 | ||||
-rw-r--r-- | openbsc/src/libmsc/sms_queue.c | 68 | ||||
-rw-r--r-- | openbsc/src/libmsc/vty_interface_layer3.c | 11 |
3 files changed, 84 insertions, 56 deletions
diff --git a/openbsc/src/libmsc/db.c b/openbsc/src/libmsc/db.c index 035d5488f..28e978213 100644 --- a/openbsc/src/libmsc/db.c +++ b/openbsc/src/libmsc/db.c @@ -643,20 +643,21 @@ struct gsm_sms *db_sms_get(struct gsm_network *net, unsigned long long id) return sms; } -/* retrieve the next unsent SMS with ID >= min_id */ -struct gsm_sms *db_sms_get_unsent(struct gsm_network *net, unsigned long long min_id) +struct gsm_sms *db_sms_get_next_unsent(struct gsm_network *net, + unsigned long long min_sms_id, + unsigned int max_failed) { dbi_result result; struct gsm_sms *sms; result = dbi_conn_queryf(conn, - "SELECT SMS.* " - "FROM SMS JOIN Subscriber ON " - "SMS.dest_addr = Subscriber.extension " - "WHERE SMS.id >= %llu AND SMS.sent IS NULL " - "AND Subscriber.lac > 0 " - "ORDER BY SMS.id LIMIT 1", - min_id); + "SELECT * FROM SMS" + " WHERE sent IS NULL" + " AND id >= %llu" + " AND deliver_attempts <= %u" + " ORDER BY id LIMIT 1", + min_sms_id, max_failed); + if (!result) return NULL; @@ -672,21 +673,24 @@ struct gsm_sms *db_sms_get_unsent(struct gsm_network *net, unsigned long long mi return sms; } -struct gsm_sms *db_sms_get_unsent_by_subscr(struct gsm_network *net, - unsigned long long min_subscr_id, - unsigned int failed) +/* retrieve the next unsent SMS for a given subscriber */ +struct gsm_sms *db_sms_get_unsent_for_subscr(struct vlr_subscr *vsub, + unsigned int max_failed) { + struct gsm_network *net = vsub->vlr->user_ctx; dbi_result result; struct gsm_sms *sms; + if (!vsub->lu_complete) + return NULL; + result = dbi_conn_queryf(conn, - "SELECT SMS.* " - "FROM SMS JOIN Subscriber ON " - "SMS.dest_addr = Subscriber.extension " - "WHERE Subscriber.id >= %llu AND SMS.sent IS NULL " - "AND Subscriber.lac > 0 AND SMS.deliver_attempts < %u " - "ORDER BY Subscriber.id, SMS.id LIMIT 1", - min_subscr_id, failed); + "SELECT * FROM SMS" + " WHERE sent IS NULL" + " AND dest_addr=%s" + " AND deliver_attempts <= %u" + " ORDER BY id LIMIT 1", + vsub->msisdn, max_failed); if (!result) return NULL; @@ -702,21 +706,20 @@ struct gsm_sms *db_sms_get_unsent_by_subscr(struct gsm_network *net, return sms; } -/* retrieve the next unsent SMS for a given subscriber */ -struct gsm_sms *db_sms_get_unsent_for_subscr(struct vlr_subscr *vsub) +struct gsm_sms *db_sms_get_next_unsent_rr_msisdn(struct gsm_network *net, + const char *last_msisdn, + unsigned int max_failed) { - struct gsm_network *net = vsub->vlr->user_ctx; dbi_result result; struct gsm_sms *sms; result = dbi_conn_queryf(conn, - "SELECT SMS.* " - "FROM SMS JOIN Subscriber ON " - "SMS.dest_addr = Subscriber.extension " - "WHERE Subscriber.id = %llu AND SMS.sent IS NULL " - "AND Subscriber.lac > 0 " - "ORDER BY SMS.id LIMIT 1", - vsub->id); + "SELECT * FROM SMS" + " WHERE sent IS NULL" + " AND dest_addr > '%s'" + " AND deliver_attempts <= %u" + " ORDER BY dest_addr, id LIMIT 1", + last_msisdn, max_failed); if (!result) return NULL; diff --git a/openbsc/src/libmsc/sms_queue.c b/openbsc/src/libmsc/sms_queue.c index e3b554d88..a01177dac 100644 --- a/openbsc/src/libmsc/sms_queue.c +++ b/openbsc/src/libmsc/sms_queue.c @@ -28,6 +28,8 @@ * things up by collecting data from other parts of the system. */ +#include <limits.h> + #include <openbsc/sms_queue.h> #include <openbsc/chan_alloc.h> #include <openbsc/db.h> @@ -63,7 +65,8 @@ struct gsm_sms_queue { int pending; struct llist_head pending_sms; - unsigned long long last_subscr_id; + + char last_msisdn[GSM_EXTENSION_LENGTH+1]; }; static int sms_subscr_cb(unsigned int, unsigned int, void *, void *); @@ -189,29 +192,49 @@ static void sms_resend_pending(void *_data) } } -static struct gsm_sms *take_next_sms(struct gsm_sms_queue *smsq) +/* Find the next pending SMS by cycling through the recipients. We could also + * cycle through the pending SMS, but that might cause us to keep trying to + * send SMS to the same few subscribers repeatedly while not servicing other + * subscribers for a long time. By walking the list of recipient MSISDNs, we + * ensure that all subscribers get their fair time to receive SMS. */ +struct gsm_sms *smsq_take_next_sms(struct gsm_network *net, + char *last_msisdn, + size_t last_msisdn_buflen) { struct gsm_sms *sms; + int wrapped = 0; + int sanity = 100; + char started_with_msisdn[last_msisdn_buflen]; + + osmo_strlcpy(started_with_msisdn, last_msisdn, + sizeof(started_with_msisdn)); + + while (wrapped < 2 && (--sanity)) { + /* If we wrapped around and passed the first msisdn, we're + * through the entire SMS DB; end it. */ + if (wrapped && strcmp(last_msisdn, started_with_msisdn) >= 0) + break; + + sms = db_sms_get_next_unsent_rr_msisdn(net, last_msisdn, 9); + if (!sms) { + last_msisdn[0] = '\0'; + wrapped ++; + continue; + } + + /* Whatever happens, next time around service another recipient + */ + osmo_strlcpy(last_msisdn, sms->dst.addr, last_msisdn_buflen); + + /* Is the subscriber attached? If not, go to next SMS */ + if (!sms->receiver || !sms->receiver->lu_complete) + continue; - sms = db_sms_get_unsent_by_subscr(smsq->network, smsq->last_subscr_id, 10); - DEBUGP(DLSMS, "db_sms_get_unsent_by_subscr(id = %llu) returned %p\n", - smsq->last_subscr_id, sms); - if (sms) { - smsq->last_subscr_id = sms->receiver->id + 1; - DEBUGP(DLSMS, "take_next_sms() returns %p\n", sms); return sms; } - /* need to wrap around */ - smsq->last_subscr_id = 0; - sms = db_sms_get_unsent_by_subscr(smsq->network, - smsq->last_subscr_id, 10); - DEBUGP(DLSMS, "db_sms_get_unsent_by_subscr(id = %llu) returned %p\n", - smsq->last_subscr_id, sms); - if (sms) - smsq->last_subscr_id = sms->receiver->id + 1; - DEBUGP(DLSMS, "take_next_sms() returns %p\n", sms); - return sms; + DEBUGP(DLSMS, "SMS queue: no SMS to be sent\n"); + return NULL; } /** @@ -233,7 +256,8 @@ static void sms_submit_pending(void *_data) struct gsm_sms *sms; - sms = take_next_sms(smsq); + sms = smsq_take_next_sms(smsq->network, smsq->last_msisdn, + sizeof(smsq->last_msisdn)); if (!sms) { LOGP(DLSMS, LOGL_DEBUG, "Sending SMS done (%d attempted)\n", attempted); @@ -309,11 +333,11 @@ static void sms_send_next(struct vlr_subscr *vsub) OSMO_ASSERT(!sms_subscriber_is_pending(smsq, vsub)); /* check for more messages for this subscriber */ - sms = db_sms_get_unsent_for_subscr(vsub); + sms = db_sms_get_unsent_for_subscr(vsub, UINT_MAX); if (!sms) goto no_pending_sms; - /* No sms should be scheduled right now */ + /* The sms should not be scheduled right now */ OSMO_ASSERT(!sms_is_in_pending(smsq, sms)); /* Remember that we deliver this SMS and send it */ @@ -408,7 +432,7 @@ static int sub_ready_for_sm(struct gsm_network *net, struct vlr_subscr *vsub) return -1; /* Now try to deliver any pending SMS to this sub */ - sms = db_sms_get_unsent_for_subscr(vsub); + sms = db_sms_get_unsent_for_subscr(vsub, UINT_MAX); if (!sms) return -1; gsm411_send_sms(conn, sms); diff --git a/openbsc/src/libmsc/vty_interface_layer3.c b/openbsc/src/libmsc/vty_interface_layer3.c index 5ed0b0fe1..7fa66d3e4 100644 --- a/openbsc/src/libmsc/vty_interface_layer3.c +++ b/openbsc/src/libmsc/vty_interface_layer3.c @@ -158,16 +158,17 @@ DEFUN(sms_send_pend, { struct gsm_network *gsmnet = gsmnet_from_vty(vty); struct gsm_sms *sms; - int id = 0; + unsigned long long sms_id = 0; while (1) { - sms = db_sms_get_unsent_by_subscr(gsmnet, id, UINT_MAX); + sms = db_sms_get_next_unsent(gsmnet, sms_id, UINT_MAX); if (!sms) break; - gsm411_send_sms_subscr(sms->receiver, sms); + if (sms->receiver) + gsm411_send_sms_subscr(sms->receiver, sms); - id = sms->receiver->id + 1; + sms_id = sms->id + 1; } return CMD_SUCCESS; @@ -268,7 +269,7 @@ DEFUN(subscriber_send_pending_sms, return CMD_WARNING; } - sms = db_sms_get_unsent_by_subscr(gsmnet, vsub->id, UINT_MAX); + sms = db_sms_get_unsent_for_subscr(vsub, UINT_MAX); if (sms) gsm411_send_sms_subscr(sms->receiver, sms); |