blob: f74be3009b1c53de19b9ea0b5fa1a36c561389ec [file] [log] [blame]
Harald Welte57e5b942012-09-07 10:23:44 +02001/* 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>
10
Holger Hans Peter Freytherc302eca2012-09-11 11:49:35 +020011#include <osmocom/core/application.h>
Harald Welte57e5b942012-09-07 10:23:44 +020012#include <osmocom/core/utils.h>
13#include <osmocom/core/logging.h>
14#include <osmocom/core/talloc.h>
15#include <osmocom/gprs/gprs_bssgp.h>
16
17static unsigned long in_ctr = 1;
18static struct timeval tv_start;
19
20int get_centisec_diff(void)
21{
22 struct timeval tv;
23 struct timeval now;
24 gettimeofday(&now, NULL);
25
26 timersub(&now, &tv_start, &tv);
27
28 return tv.tv_sec * 100 + tv.tv_usec/10000;
29}
30
31/* round to deciseconds to make sure test output is always consistent */
32int round_decisec(int csec_in)
33{
34 int tmp = csec_in / 10;
35
36 return tmp * 10;
37}
38
39static int fc_out_cb(struct bssgp_flow_control *fc, struct msgb *msg,
40 uint32_t llc_pdu_len, void *priv)
41{
42 unsigned int csecs = get_centisec_diff();
43 csecs = round_decisec(csecs);
44
45 printf("%u: FC OUT Nr %lu\n", csecs, (unsigned long) msg);
46}
47
48static int fc_in(struct bssgp_flow_control *fc, unsigned int pdu_len)
49{
50 unsigned int csecs = get_centisec_diff();
51 csecs = round_decisec(csecs);
52
53 printf("%u: FC IN Nr %lu\n", csecs, in_ctr);
54 bssgp_fc_in(fc, (struct msgb *) in_ctr, pdu_len, NULL);
55 in_ctr++;
56}
57
58
59static void test_fc(uint32_t bucket_size_max, uint32_t bucket_leak_rate,
60 uint32_t max_queue_depth, uint32_t pdu_len,
61 uint32_t pdu_count)
62{
63 struct bssgp_flow_control *fc = talloc_zero(NULL, struct bssgp_flow_control);
64 int i;
65
66 bssgp_fc_init(fc, bucket_size_max, bucket_leak_rate, max_queue_depth,
67 fc_out_cb);
68
69 gettimeofday(&tv_start, NULL);
70
71 for (i = 0; i < pdu_count; i++) {
72 fc_in(fc, pdu_len);
73 osmo_timers_check();
74 osmo_timers_prepare();
75 osmo_timers_update();
76 }
77
78 while (1) {
79 usleep(100000);
80 osmo_timers_check();
81 osmo_timers_prepare();
82 osmo_timers_update();
83
84 if (llist_empty(&fc->queue))
85 break;
86 }
87}
88
89static void help(void)
90{
91 printf(" -h --help This help message\n");
92 printf(" -s --bucket-size-max N Maximum size of bucket in octets\n");
93 printf(" -r --bucket-leak-rate N Bucket leak rate in octets/sec\n");
94 printf(" -d --max-queue-depth N Maximum length of pending PDU queue (msgs)\n");
95 printf(" -l --pdu-length N Length of each PDU in octets\n");
96}
97
98int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
99{
100 return -1;
101}
102
103static struct log_info info = {};
104
105int main(int argc, char **argv)
106{
107 uint32_t bucket_size_max = 100; /* octets */
108 uint32_t bucket_leak_rate = 100; /* octets / second */
109 uint32_t max_queue_depth = 5; /* messages */
110 uint32_t pdu_length = 10; /* octets */
111 uint32_t pdu_count = 20; /* messages */
112 int c;
113
114 static const struct option long_options[] = {
115 { "bucket-size-max", 1, 0, 's' },
116 { "bucket-leak-rate", 1, 0, 'r' },
117 { "max-queue-depth", 1, 0, 'd' },
118 { "pdu-length", 1, 0, 'l' },
119 { "pdu-count", 1, 0, 'c' },
120 { "help", 0, 0, 'h' },
121 { 0, 0, 0, 0 }
122 };
123
124 osmo_init_logging(&info);
Holger Hans Peter Freytherc302eca2012-09-11 11:49:35 +0200125 log_set_use_color(osmo_stderr_target, 0);
126 log_set_print_filename(osmo_stderr_target, 0);
Harald Welte57e5b942012-09-07 10:23:44 +0200127
128 while ((c = getopt_long(argc, argv, "s:r:d:l:c:",
129 long_options, NULL)) != -1) {
130 switch (c) {
131 case 's':
132 bucket_size_max = atoi(optarg);
133 break;
134 case 'r':
135 bucket_leak_rate = atoi(optarg);
136 break;
137 case 'd':
138 max_queue_depth = atoi(optarg);
139 break;
140 case 'l':
141 pdu_length = atoi(optarg);
142 break;
143 case 'c':
144 pdu_count = atoi(optarg);
145 break;
146 case 'h':
147 help();
148 exit(EXIT_SUCCESS);
149 break;
150 default:
151 exit(EXIT_FAILURE);
152 }
153 }
154
155 /* bucket leak rate less than 100 not supported! */
156 if (bucket_leak_rate < 100) {
157 fprintf(stderr, "Bucket leak rate < 100 not supported!\n");
158 exit(EXIT_FAILURE);
159 }
160
161 printf("===== BSSGP flow-control test START\n");
162 printf("size-max=%u oct, leak-rate=%u oct/s, "
163 "queue-len=%u msgs, pdu_len=%u oct, pdu_cnt=%u\n\n", bucket_size_max,
164 bucket_leak_rate, max_queue_depth, pdu_length, pdu_count);
165 test_fc(bucket_size_max, bucket_leak_rate, max_queue_depth,
166 pdu_length, pdu_count);
167 printf("===== BSSGP flow-control test END\n\n");
168
169 exit(EXIT_SUCCESS);
170}