2012-09-07 08:23:44 +00:00
|
|
|
/* test routines for BSSGP flow control implementation in libosmogb
|
|
|
|
* (C) 2012 by Harald Welte <laforge@gnumonks.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <getopt.h>
|
2013-09-16 12:13:25 +00:00
|
|
|
#include <unistd.h>
|
2012-09-07 08:23:44 +00:00
|
|
|
|
2012-09-11 09:49:35 +00:00
|
|
|
#include <osmocom/core/application.h>
|
2012-09-07 08:23:44 +00:00
|
|
|
#include <osmocom/core/utils.h>
|
|
|
|
#include <osmocom/core/logging.h>
|
|
|
|
#include <osmocom/core/talloc.h>
|
|
|
|
#include <osmocom/gprs/gprs_bssgp.h>
|
|
|
|
|
|
|
|
static unsigned long in_ctr = 1;
|
|
|
|
static struct timeval tv_start;
|
|
|
|
|
|
|
|
int get_centisec_diff(void)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
struct timeval now;
|
2016-09-22 01:58:13 +00:00
|
|
|
osmo_gettimeofday(&now, NULL);
|
2012-09-07 08:23:44 +00:00
|
|
|
|
|
|
|
timersub(&now, &tv_start, &tv);
|
|
|
|
|
|
|
|
return tv.tv_sec * 100 + tv.tv_usec/10000;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fc_out_cb(struct bssgp_flow_control *fc, struct msgb *msg,
|
|
|
|
uint32_t llc_pdu_len, void *priv)
|
|
|
|
{
|
|
|
|
unsigned int csecs = get_centisec_diff();
|
|
|
|
|
2014-10-10 15:24:34 +00:00
|
|
|
printf("%u: FC OUT Nr %lu\n", csecs, (unsigned long) msg->cb[0]);
|
|
|
|
msgb_free(msg);
|
2013-09-16 12:13:25 +00:00
|
|
|
return 0;
|
2012-09-07 08:23:44 +00:00
|
|
|
}
|
|
|
|
|
2017-11-16 21:55:02 +00:00
|
|
|
static void fc_in(struct bssgp_flow_control *fc, unsigned int pdu_len)
|
2012-09-07 08:23:44 +00:00
|
|
|
{
|
2014-10-10 15:24:34 +00:00
|
|
|
struct msgb *msg;
|
2012-09-07 08:23:44 +00:00
|
|
|
unsigned int csecs = get_centisec_diff();
|
2017-11-16 21:55:02 +00:00
|
|
|
int rc;
|
2012-09-07 08:23:44 +00:00
|
|
|
|
2014-10-10 15:24:34 +00:00
|
|
|
msg = msgb_alloc(1, "fc test");
|
|
|
|
msg->cb[0] = in_ctr++;
|
|
|
|
|
|
|
|
printf("%u: FC IN Nr %lu\n", csecs, msg->cb[0]);
|
2017-11-16 21:55:02 +00:00
|
|
|
rc = bssgp_fc_in(fc, msg, pdu_len, NULL);
|
|
|
|
switch (rc) {
|
|
|
|
case 0:
|
|
|
|
printf(" -> %d: ok\n", rc);
|
|
|
|
break;
|
|
|
|
case -ENOSPC:
|
|
|
|
printf(" -> %d: queue full, msg dropped.\n", rc);
|
|
|
|
break;
|
|
|
|
case -EIO:
|
|
|
|
printf(" -> %d: PDU too large, msg dropped.\n", rc);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf(" -> %d: error, msg dropped.\n", rc);
|
|
|
|
break;
|
|
|
|
}
|
2012-09-07 08:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void test_fc(uint32_t bucket_size_max, uint32_t bucket_leak_rate,
|
|
|
|
uint32_t max_queue_depth, uint32_t pdu_len,
|
|
|
|
uint32_t pdu_count)
|
|
|
|
{
|
|
|
|
struct bssgp_flow_control *fc = talloc_zero(NULL, struct bssgp_flow_control);
|
|
|
|
int i;
|
|
|
|
|
2017-02-06 13:39:53 +00:00
|
|
|
osmo_gettimeofday_override_time = (struct timeval){
|
|
|
|
.tv_sec = 1486385000,
|
|
|
|
.tv_usec = 423423,
|
|
|
|
};
|
|
|
|
osmo_gettimeofday_override = true;
|
|
|
|
|
2012-09-07 08:23:44 +00:00
|
|
|
bssgp_fc_init(fc, bucket_size_max, bucket_leak_rate, max_queue_depth,
|
|
|
|
fc_out_cb);
|
|
|
|
|
2016-09-22 01:58:13 +00:00
|
|
|
osmo_gettimeofday(&tv_start, NULL);
|
2012-09-07 08:23:44 +00:00
|
|
|
|
2017-11-16 21:55:02 +00:00
|
|
|
/* Fill the queue with PDUs, possibly beyond the queue being full. If it is full, additional PDUs
|
|
|
|
* are discarded. */
|
2012-09-07 08:23:44 +00:00
|
|
|
for (i = 0; i < pdu_count; i++) {
|
|
|
|
fc_in(fc, pdu_len);
|
|
|
|
osmo_timers_check();
|
|
|
|
osmo_timers_prepare();
|
|
|
|
osmo_timers_update();
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
2017-02-06 13:39:53 +00:00
|
|
|
osmo_gettimeofday_override_add(0, 100000);
|
|
|
|
|
2012-09-07 08:23:44 +00:00
|
|
|
osmo_timers_check();
|
|
|
|
osmo_timers_prepare();
|
|
|
|
osmo_timers_update();
|
|
|
|
|
|
|
|
if (llist_empty(&fc->queue))
|
|
|
|
break;
|
|
|
|
}
|
2017-11-16 21:31:57 +00:00
|
|
|
|
|
|
|
talloc_free(fc);
|
2012-09-07 08:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void help(void)
|
|
|
|
{
|
|
|
|
printf(" -h --help This help message\n");
|
|
|
|
printf(" -s --bucket-size-max N Maximum size of bucket in octets\n");
|
|
|
|
printf(" -r --bucket-leak-rate N Bucket leak rate in octets/sec\n");
|
|
|
|
printf(" -d --max-queue-depth N Maximum length of pending PDU queue (msgs)\n");
|
|
|
|
printf(" -l --pdu-length N Length of each PDU in octets\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct log_info info = {};
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
uint32_t bucket_size_max = 100; /* octets */
|
|
|
|
uint32_t bucket_leak_rate = 100; /* octets / second */
|
|
|
|
uint32_t max_queue_depth = 5; /* messages */
|
|
|
|
uint32_t pdu_length = 10; /* octets */
|
|
|
|
uint32_t pdu_count = 20; /* messages */
|
|
|
|
int c;
|
2017-11-16 21:32:36 +00:00
|
|
|
void *tall_msgb_ctx;
|
2012-09-07 08:23:44 +00:00
|
|
|
|
|
|
|
static const struct option long_options[] = {
|
|
|
|
{ "bucket-size-max", 1, 0, 's' },
|
|
|
|
{ "bucket-leak-rate", 1, 0, 'r' },
|
|
|
|
{ "max-queue-depth", 1, 0, 'd' },
|
|
|
|
{ "pdu-length", 1, 0, 'l' },
|
|
|
|
{ "pdu-count", 1, 0, 'c' },
|
|
|
|
{ "help", 0, 0, 'h' },
|
|
|
|
{ 0, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
osmo_init_logging(&info);
|
2012-09-11 09:49:35 +00:00
|
|
|
log_set_use_color(osmo_stderr_target, 0);
|
|
|
|
log_set_print_filename(osmo_stderr_target, 0);
|
2012-09-07 08:23:44 +00:00
|
|
|
|
2017-11-16 21:32:36 +00:00
|
|
|
tall_msgb_ctx = msgb_talloc_ctx_init(NULL, 0);
|
|
|
|
|
2012-09-07 08:23:44 +00:00
|
|
|
while ((c = getopt_long(argc, argv, "s:r:d:l:c:",
|
|
|
|
long_options, NULL)) != -1) {
|
|
|
|
switch (c) {
|
|
|
|
case 's':
|
|
|
|
bucket_size_max = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
bucket_leak_rate = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
max_queue_depth = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
pdu_length = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
pdu_count = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
help();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* bucket leak rate less than 100 not supported! */
|
|
|
|
if (bucket_leak_rate < 100) {
|
|
|
|
fprintf(stderr, "Bucket leak rate < 100 not supported!\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("===== BSSGP flow-control test START\n");
|
|
|
|
printf("size-max=%u oct, leak-rate=%u oct/s, "
|
|
|
|
"queue-len=%u msgs, pdu_len=%u oct, pdu_cnt=%u\n\n", bucket_size_max,
|
|
|
|
bucket_leak_rate, max_queue_depth, pdu_length, pdu_count);
|
|
|
|
test_fc(bucket_size_max, bucket_leak_rate, max_queue_depth,
|
|
|
|
pdu_length, pdu_count);
|
2017-11-16 21:32:36 +00:00
|
|
|
printf("msgb ctx: %zu b in %zu blocks (0 b in 1 block == just the context)\n",
|
|
|
|
talloc_total_size(tall_msgb_ctx),
|
|
|
|
talloc_total_blocks(tall_msgb_ctx));
|
gprs_bssgp: bssgp_fc_in(): fix mem leak on queue overflow
All successful and all error code paths of bssgp_fc_in() free the msgb, except
the code path calling fc_enqueue() when the msg is dropped (due to queue being
full, or failure to allocate).
Callers could theoretically catch the -ENOSPC return value and discard the
msgb. However, in other code paths, a callback's return value is returned,
which is expected to free the msgb, so such callback would have to never return
-ENOSPC when it freed the msgb. Much simpler semantics would be to free the
msgb in every code path, no matter which kind of error occurred.
Who is currently calling bssgp_fc_in and how do they handle the return value?
- bssgp_fc_test.c ignores the return value (and hits a mem leak aka sanitizer
build failure if the queue is full).
- fc_timer_cb() ignores the return value.
- bssgp_tx_dl_ud() returns the bssgp_fc_in() rc.
- which is returned by a cascade of functions leading up to being returned,
for example, by gprs_llgmm_reset(), which is usually called with ignored
return code.
At this point it is already fairly clear that bssgp_fc_in() should always free
the msgb, since the callers don't seem to distinguish even between error or
success, let alone between -ENOSPC or other errors.
bssgp_fc_test: assert that no msgbs remain unfreed after the tests.
Adjust expected results.
Helps fix sanitizer build on debian 9.
Change-Id: I00c62a104baeaad6a85883c380259c469aebf0df
2017-11-16 21:32:36 +00:00
|
|
|
OSMO_ASSERT(talloc_total_size(tall_msgb_ctx) == 0);
|
|
|
|
OSMO_ASSERT(talloc_total_blocks(tall_msgb_ctx) == 1);
|
2017-11-16 21:32:36 +00:00
|
|
|
talloc_free(tall_msgb_ctx);
|
2012-09-07 08:23:44 +00:00
|
|
|
printf("===== BSSGP flow-control test END\n\n");
|
|
|
|
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|