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