Harald Welte | 57e5b94 | 2012-09-07 10:23:44 +0200 | [diff] [blame] | 1 | /* test routines for BSSGP flow control implementation in libosmogb |
| 2 | * (C) 2012 by Harald Welte <laforge@gnumonks.org> |
| 3 | */ |
| 4 | |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <stdint.h> |
| 8 | #include <string.h> |
| 9 | #include <getopt.h> |
Holger Hans Peter Freyther | 5b6416a | 2013-09-16 14:13:25 +0200 | [diff] [blame] | 10 | #include <unistd.h> |
Harald Welte | 57e5b94 | 2012-09-07 10:23:44 +0200 | [diff] [blame] | 11 | |
Holger Hans Peter Freyther | c302eca | 2012-09-11 11:49:35 +0200 | [diff] [blame] | 12 | #include <osmocom/core/application.h> |
Harald Welte | 57e5b94 | 2012-09-07 10:23:44 +0200 | [diff] [blame] | 13 | #include <osmocom/core/utils.h> |
| 14 | #include <osmocom/core/logging.h> |
| 15 | #include <osmocom/core/talloc.h> |
| 16 | #include <osmocom/gprs/gprs_bssgp.h> |
| 17 | |
| 18 | static unsigned long in_ctr = 1; |
| 19 | static struct timeval tv_start; |
Neels Hofmeyr | a829b45 | 2018-04-05 03:02:35 +0200 | [diff] [blame] | 20 | void *ctx = NULL; |
Harald Welte | 57e5b94 | 2012-09-07 10:23:44 +0200 | [diff] [blame] | 21 | |
| 22 | int get_centisec_diff(void) |
| 23 | { |
| 24 | struct timeval tv; |
| 25 | struct timeval now; |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 26 | osmo_gettimeofday(&now, NULL); |
Harald Welte | 57e5b94 | 2012-09-07 10:23:44 +0200 | [diff] [blame] | 27 | |
| 28 | timersub(&now, &tv_start, &tv); |
| 29 | |
| 30 | return tv.tv_sec * 100 + tv.tv_usec/10000; |
| 31 | } |
| 32 | |
Harald Welte | 57e5b94 | 2012-09-07 10:23:44 +0200 | [diff] [blame] | 33 | static int fc_out_cb(struct bssgp_flow_control *fc, struct msgb *msg, |
| 34 | uint32_t llc_pdu_len, void *priv) |
| 35 | { |
| 36 | unsigned int csecs = get_centisec_diff(); |
Harald Welte | 57e5b94 | 2012-09-07 10:23:44 +0200 | [diff] [blame] | 37 | |
Holger Hans Peter Freyther | 10dd73c | 2014-10-10 17:24:34 +0200 | [diff] [blame] | 38 | printf("%u: FC OUT Nr %lu\n", csecs, (unsigned long) msg->cb[0]); |
| 39 | msgb_free(msg); |
Holger Hans Peter Freyther | 5b6416a | 2013-09-16 14:13:25 +0200 | [diff] [blame] | 40 | return 0; |
Harald Welte | 57e5b94 | 2012-09-07 10:23:44 +0200 | [diff] [blame] | 41 | } |
| 42 | |
Neels Hofmeyr | 9541a68 | 2017-11-16 22:55:02 +0100 | [diff] [blame] | 43 | static void fc_in(struct bssgp_flow_control *fc, unsigned int pdu_len) |
Harald Welte | 57e5b94 | 2012-09-07 10:23:44 +0200 | [diff] [blame] | 44 | { |
Holger Hans Peter Freyther | 10dd73c | 2014-10-10 17:24:34 +0200 | [diff] [blame] | 45 | struct msgb *msg; |
Harald Welte | 57e5b94 | 2012-09-07 10:23:44 +0200 | [diff] [blame] | 46 | unsigned int csecs = get_centisec_diff(); |
Neels Hofmeyr | 9541a68 | 2017-11-16 22:55:02 +0100 | [diff] [blame] | 47 | int rc; |
Harald Welte | 57e5b94 | 2012-09-07 10:23:44 +0200 | [diff] [blame] | 48 | |
Holger Hans Peter Freyther | 10dd73c | 2014-10-10 17:24:34 +0200 | [diff] [blame] | 49 | msg = msgb_alloc(1, "fc test"); |
| 50 | msg->cb[0] = in_ctr++; |
| 51 | |
| 52 | printf("%u: FC IN Nr %lu\n", csecs, msg->cb[0]); |
Neels Hofmeyr | 9541a68 | 2017-11-16 22:55:02 +0100 | [diff] [blame] | 53 | rc = bssgp_fc_in(fc, msg, pdu_len, NULL); |
| 54 | switch (rc) { |
| 55 | case 0: |
| 56 | printf(" -> %d: ok\n", rc); |
| 57 | break; |
| 58 | case -ENOSPC: |
| 59 | printf(" -> %d: queue full, msg dropped.\n", rc); |
| 60 | break; |
| 61 | case -EIO: |
| 62 | printf(" -> %d: PDU too large, msg dropped.\n", rc); |
| 63 | break; |
| 64 | default: |
| 65 | printf(" -> %d: error, msg dropped.\n", rc); |
| 66 | break; |
| 67 | } |
Harald Welte | 57e5b94 | 2012-09-07 10:23:44 +0200 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | |
| 71 | static void test_fc(uint32_t bucket_size_max, uint32_t bucket_leak_rate, |
| 72 | uint32_t max_queue_depth, uint32_t pdu_len, |
| 73 | uint32_t pdu_count) |
| 74 | { |
Neels Hofmeyr | a829b45 | 2018-04-05 03:02:35 +0200 | [diff] [blame] | 75 | struct bssgp_flow_control *fc = talloc_zero(ctx, struct bssgp_flow_control); |
Harald Welte | 57e5b94 | 2012-09-07 10:23:44 +0200 | [diff] [blame] | 76 | int i; |
| 77 | |
Neels Hofmeyr | 7132011 | 2017-02-06 14:39:53 +0100 | [diff] [blame] | 78 | osmo_gettimeofday_override_time = (struct timeval){ |
| 79 | .tv_sec = 1486385000, |
| 80 | .tv_usec = 423423, |
| 81 | }; |
| 82 | osmo_gettimeofday_override = true; |
| 83 | |
Harald Welte | 57e5b94 | 2012-09-07 10:23:44 +0200 | [diff] [blame] | 84 | bssgp_fc_init(fc, bucket_size_max, bucket_leak_rate, max_queue_depth, |
| 85 | fc_out_cb); |
| 86 | |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 87 | osmo_gettimeofday(&tv_start, NULL); |
Harald Welte | 57e5b94 | 2012-09-07 10:23:44 +0200 | [diff] [blame] | 88 | |
Neels Hofmeyr | 9541a68 | 2017-11-16 22:55:02 +0100 | [diff] [blame] | 89 | /* Fill the queue with PDUs, possibly beyond the queue being full. If it is full, additional PDUs |
| 90 | * are discarded. */ |
Harald Welte | 57e5b94 | 2012-09-07 10:23:44 +0200 | [diff] [blame] | 91 | for (i = 0; i < pdu_count; i++) { |
| 92 | fc_in(fc, pdu_len); |
| 93 | osmo_timers_check(); |
| 94 | osmo_timers_prepare(); |
| 95 | osmo_timers_update(); |
| 96 | } |
| 97 | |
| 98 | while (1) { |
Neels Hofmeyr | 7132011 | 2017-02-06 14:39:53 +0100 | [diff] [blame] | 99 | osmo_gettimeofday_override_add(0, 100000); |
| 100 | |
Harald Welte | 57e5b94 | 2012-09-07 10:23:44 +0200 | [diff] [blame] | 101 | osmo_timers_check(); |
| 102 | osmo_timers_prepare(); |
| 103 | osmo_timers_update(); |
| 104 | |
| 105 | if (llist_empty(&fc->queue)) |
| 106 | break; |
| 107 | } |
Neels Hofmeyr | 85f5a2c | 2017-11-16 22:31:57 +0100 | [diff] [blame] | 108 | |
| 109 | talloc_free(fc); |
Harald Welte | 57e5b94 | 2012-09-07 10:23:44 +0200 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static void help(void) |
| 113 | { |
| 114 | printf(" -h --help This help message\n"); |
| 115 | printf(" -s --bucket-size-max N Maximum size of bucket in octets\n"); |
| 116 | printf(" -r --bucket-leak-rate N Bucket leak rate in octets/sec\n"); |
| 117 | printf(" -d --max-queue-depth N Maximum length of pending PDU queue (msgs)\n"); |
| 118 | printf(" -l --pdu-length N Length of each PDU in octets\n"); |
| 119 | } |
| 120 | |
| 121 | int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx) |
| 122 | { |
| 123 | return -1; |
| 124 | } |
| 125 | |
| 126 | static struct log_info info = {}; |
| 127 | |
| 128 | int main(int argc, char **argv) |
| 129 | { |
| 130 | uint32_t bucket_size_max = 100; /* octets */ |
| 131 | uint32_t bucket_leak_rate = 100; /* octets / second */ |
| 132 | uint32_t max_queue_depth = 5; /* messages */ |
| 133 | uint32_t pdu_length = 10; /* octets */ |
| 134 | uint32_t pdu_count = 20; /* messages */ |
| 135 | int c; |
Neels Hofmeyr | 0128c78 | 2017-11-16 22:32:36 +0100 | [diff] [blame] | 136 | void *tall_msgb_ctx; |
Neels Hofmeyr | a829b45 | 2018-04-05 03:02:35 +0200 | [diff] [blame] | 137 | ctx = talloc_named_const(NULL, 0, "bssgp_fc_test"); |
Harald Welte | 57e5b94 | 2012-09-07 10:23:44 +0200 | [diff] [blame] | 138 | |
| 139 | static const struct option long_options[] = { |
| 140 | { "bucket-size-max", 1, 0, 's' }, |
| 141 | { "bucket-leak-rate", 1, 0, 'r' }, |
| 142 | { "max-queue-depth", 1, 0, 'd' }, |
| 143 | { "pdu-length", 1, 0, 'l' }, |
| 144 | { "pdu-count", 1, 0, 'c' }, |
| 145 | { "help", 0, 0, 'h' }, |
| 146 | { 0, 0, 0, 0 } |
| 147 | }; |
| 148 | |
Neels Hofmeyr | a829b45 | 2018-04-05 03:02:35 +0200 | [diff] [blame] | 149 | osmo_init_logging2(ctx, &info); |
Holger Hans Peter Freyther | c302eca | 2012-09-11 11:49:35 +0200 | [diff] [blame] | 150 | log_set_use_color(osmo_stderr_target, 0); |
| 151 | log_set_print_filename(osmo_stderr_target, 0); |
Harald Welte | 57e5b94 | 2012-09-07 10:23:44 +0200 | [diff] [blame] | 152 | |
Neels Hofmeyr | a829b45 | 2018-04-05 03:02:35 +0200 | [diff] [blame] | 153 | tall_msgb_ctx = msgb_talloc_ctx_init(ctx, 0); |
Neels Hofmeyr | 0128c78 | 2017-11-16 22:32:36 +0100 | [diff] [blame] | 154 | |
Harald Welte | 57e5b94 | 2012-09-07 10:23:44 +0200 | [diff] [blame] | 155 | while ((c = getopt_long(argc, argv, "s:r:d:l:c:", |
| 156 | long_options, NULL)) != -1) { |
| 157 | switch (c) { |
| 158 | case 's': |
| 159 | bucket_size_max = atoi(optarg); |
| 160 | break; |
| 161 | case 'r': |
| 162 | bucket_leak_rate = atoi(optarg); |
| 163 | break; |
| 164 | case 'd': |
| 165 | max_queue_depth = atoi(optarg); |
| 166 | break; |
| 167 | case 'l': |
| 168 | pdu_length = atoi(optarg); |
| 169 | break; |
| 170 | case 'c': |
| 171 | pdu_count = atoi(optarg); |
| 172 | break; |
| 173 | case 'h': |
| 174 | help(); |
| 175 | exit(EXIT_SUCCESS); |
| 176 | break; |
| 177 | default: |
| 178 | exit(EXIT_FAILURE); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /* bucket leak rate less than 100 not supported! */ |
| 183 | if (bucket_leak_rate < 100) { |
| 184 | fprintf(stderr, "Bucket leak rate < 100 not supported!\n"); |
| 185 | exit(EXIT_FAILURE); |
| 186 | } |
| 187 | |
| 188 | printf("===== BSSGP flow-control test START\n"); |
| 189 | printf("size-max=%u oct, leak-rate=%u oct/s, " |
| 190 | "queue-len=%u msgs, pdu_len=%u oct, pdu_cnt=%u\n\n", bucket_size_max, |
| 191 | bucket_leak_rate, max_queue_depth, pdu_length, pdu_count); |
| 192 | test_fc(bucket_size_max, bucket_leak_rate, max_queue_depth, |
| 193 | pdu_length, pdu_count); |
Neels Hofmeyr | 0128c78 | 2017-11-16 22:32:36 +0100 | [diff] [blame] | 194 | printf("msgb ctx: %zu b in %zu blocks (0 b in 1 block == just the context)\n", |
| 195 | talloc_total_size(tall_msgb_ctx), |
| 196 | talloc_total_blocks(tall_msgb_ctx)); |
Neels Hofmeyr | cd325ef | 2017-11-16 22:32:36 +0100 | [diff] [blame] | 197 | OSMO_ASSERT(talloc_total_size(tall_msgb_ctx) == 0); |
| 198 | OSMO_ASSERT(talloc_total_blocks(tall_msgb_ctx) == 1); |
Neels Hofmeyr | 0128c78 | 2017-11-16 22:32:36 +0100 | [diff] [blame] | 199 | talloc_free(tall_msgb_ctx); |
Harald Welte | 57e5b94 | 2012-09-07 10:23:44 +0200 | [diff] [blame] | 200 | printf("===== BSSGP flow-control test END\n\n"); |
| 201 | |
| 202 | exit(EXIT_SUCCESS); |
| 203 | } |