blob: 63abf8b3632d0c5708700dd8a3ec98714fbcec48 [file] [log] [blame]
Jacob Erlbeckcdebf742014-10-14 14:49:37 +02001/* Test routines for the BSSGP implementation in libosmogb
2 *
3 * (C) 2014 by sysmocom s.f.m.c. GmbH
4 * Author: Jacob Erlbeck <jerlbeck@sysmocom.de>
5 *
6 * Skeleton based on bssgp_fc_test.c
7 * (C) 2012 by Harald Welte <laforge@gnumonks.org>
8 */
9
10#undef _GNU_SOURCE
11#define _GNU_SOURCE
12
13#include <osmocom/core/application.h>
14#include <osmocom/core/utils.h>
15#include <osmocom/core/logging.h>
16#include <osmocom/core/talloc.h>
17#include <osmocom/core/prim.h>
18#include <osmocom/gprs/gprs_bssgp.h>
19#include <osmocom/gprs/gprs_ns.h>
Jacob Erlbeck9385d1e2015-05-06 09:29:32 +020020#include <osmocom/gprs/gprs_bssgp_bss.h>
Jacob Erlbeckcdebf742014-10-14 14:49:37 +020021
22#include <stdio.h>
23#include <stdlib.h>
24#include <stdint.h>
25#include <string.h>
26#include <getopt.h>
27#include <unistd.h>
28#include <netinet/ip.h>
Holger Hans Peter Freythere0a1a802014-10-25 12:19:56 +020029#include <sys/socket.h>
Jacob Erlbeckcdebf742014-10-14 14:49:37 +020030#include <dlfcn.h>
31
32#define BSS_NSEI 0x0b55
33
34static struct osmo_prim_hdr last_oph = {0};
35
36/* override */
37ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
38 const struct sockaddr *dest_addr, socklen_t addrlen)
39{
40 typedef ssize_t (*sendto_t)(int, const void *, size_t, int,
41 const struct sockaddr *, socklen_t);
42 static sendto_t real_sendto = NULL;
43 uint32_t dest_host = htonl(((struct sockaddr_in *)dest_addr)->sin_addr.s_addr);
44
45 if (!real_sendto)
46 real_sendto = dlsym(RTLD_NEXT, "sendto");
47
48 fprintf(stderr, "MESSAGE to 0x%08x, msg length %d\n%s\n",
49 dest_host, len, osmo_hexdump(buf, len));
50
51 return len;
52}
53
54/* override */
55int gprs_ns_callback(enum gprs_ns_evt event, struct gprs_nsvc *nsvc,
56 struct msgb *msg, uint16_t bvci)
57{
58 fprintf(stderr, "CALLBACK, event %d, msg length %d, bvci 0x%04x\n%s\n\n",
59 event, msgb_bssgp_len(msg), bvci,
60 osmo_hexdump(msgb_bssgph(msg), msgb_bssgp_len(msg)));
61 return 0;
62}
63
Jacob Erlbeck9385d1e2015-05-06 09:29:32 +020064struct msgb *last_ns_tx_msg = NULL;
65
66/* override */
67int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg)
68{
69 msgb_free(last_ns_tx_msg);
70 last_ns_tx_msg = msg;
71
72 return msgb_length(msg);
73}
74
Jacob Erlbeckcdebf742014-10-14 14:49:37 +020075int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
76{
77 printf("BSSGP primitive, SAP %d, prim = %d, op = %d, msg = %s\n",
78 oph->sap, oph->primitive, oph->operation, msgb_hexdump(oph->msg));
79
80 last_oph.sap = oph->sap;
81 last_oph.primitive = oph->primitive;
82 last_oph.operation = oph->operation;
83 last_oph.msg = NULL;
84 return -1;
85}
86
87static void msgb_bssgp_send_and_free(struct msgb *msg)
88{
89 msgb_nsei(msg) = BSS_NSEI;
90
91 bssgp_rcvmsg(msg);
92
93 msgb_free(msg);
94}
95
96static void send_bssgp_supend(enum bssgp_pdu_type pdu_type, uint32_t tlli)
97{
98 struct msgb *msg = bssgp_msgb_alloc();
99 uint32_t tlli_be = htonl(tlli);
100 uint8_t rai[] = {0x0f, 0xf1, 0x80, 0x20, 0x37, 0x00};
101
102 msgb_v_put(msg, pdu_type);
103 msgb_tvlv_put(msg, BSSGP_IE_TLLI, sizeof(tlli_be), (uint8_t *)&tlli_be);
104 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, sizeof(rai), &rai[0]);
105
106 msgb_bssgp_send_and_free(msg);
107}
108
109static void send_bssgp_resume(enum bssgp_pdu_type pdu_type, uint32_t tlli)
110{
111 struct msgb *msg = bssgp_msgb_alloc();
112 uint32_t tlli_be = htonl(tlli);
113 uint8_t rai[] = {0x0f, 0xf1, 0x80, 0x20, 0x37, 0x00};
114 uint8_t suspend_ref = 1;
115
116 msgb_v_put(msg, pdu_type);
117 msgb_tvlv_put(msg, BSSGP_IE_TLLI, sizeof(tlli_be), (uint8_t *)&tlli_be);
118 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, sizeof(rai), &rai[0]);
119 msgb_tvlv_put(msg, BSSGP_IE_SUSPEND_REF_NR, 1, &suspend_ref);
120
121 msgb_bssgp_send_and_free(msg);
122}
123
124static void test_bssgp_suspend_resume(void)
125{
126 const uint32_t tlli = 0xf0123456;
127
128 printf("----- %s START\n", __func__);
129 memset(&last_oph, 0, sizeof(last_oph));
130
131 send_bssgp_supend(BSSGP_PDUT_SUSPEND, tlli);
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200132 OSMO_ASSERT(last_oph.primitive == PRIM_BSSGP_GMM_SUSPEND);
Jacob Erlbeckcdebf742014-10-14 14:49:37 +0200133
134 send_bssgp_resume(BSSGP_PDUT_RESUME, tlli);
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200135 OSMO_ASSERT(last_oph.primitive == PRIM_BSSGP_GMM_RESUME);
Jacob Erlbeckcdebf742014-10-14 14:49:37 +0200136
137 printf("----- %s END\n", __func__);
138}
139
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100140static void send_bssgp_status(enum gprs_bssgp_cause cause, uint16_t *bvci)
141{
142 struct msgb *msg = bssgp_msgb_alloc();
143 uint8_t cause_ = cause;
144
145 msgb_v_put(msg, BSSGP_PDUT_STATUS);
146 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, &cause_);
147 if (bvci) {
148 uint16_t bvci_ = htons(*bvci);
149 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &bvci_);
150 }
151
152 msgb_bssgp_send_and_free(msg);
153}
154
155static void test_bssgp_status(void)
156{
157 uint16_t bvci;
158
159 printf("----- %s START\n", __func__);
160
161 send_bssgp_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL);
162 OSMO_ASSERT(last_oph.primitive == PRIM_NM_STATUS);
163
164 /* Enforce prim != PRIM_NM_STATUS */
165 last_oph.primitive = PRIM_NM_LLC_DISCARDED;
166
167 bvci = 1234;
168 send_bssgp_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci);
169 OSMO_ASSERT(last_oph.primitive == PRIM_NM_STATUS);
170
171 printf("----- %s END\n", __func__);
172}
173
Jacob Erlbeckb535e392015-04-07 17:52:44 +0200174static void test_bssgp_bad_reset()
175{
176 struct msgb *msg = bssgp_msgb_alloc();
177 uint16_t bvci_be = htons(2);
178 uint8_t cause = BSSGP_CAUSE_OML_INTERV;
179
180 msgb_v_put(msg, BSSGP_PDUT_BVC_RESET);
181 msgb_tvlv_put(msg, BSSGP_IE_BVCI, sizeof(bvci_be), (uint8_t *)&bvci_be);
182 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, sizeof(cause), &cause);
183
184 msgb_bvci(msg) = 0xbad;
185
186 msgb_bssgp_send_and_free(msg);
187}
188
Jacob Erlbeck9385d1e2015-05-06 09:29:32 +0200189static void test_bssgp_flow_control_bvc(void)
190{
191 struct bssgp_bvc_ctx bctx = {
192 .nsei = 0x1234,
193 .bvci = 0x5678,
194 };
195 const uint8_t tag = 42;
196 const uint32_t bmax = 0x1022 * 100;
197 const uint32_t rate = 0xc040 / 8 * 100;
198 const uint32_t bmax_ms = bmax / 2;
199 const uint32_t rate_ms = rate / 2;
200 uint8_t ratio = 0x78;
201 uint32_t qdelay = 0x1144 * 10;
202 int rc;
203
204 static uint8_t expected_simple_msg[] = {
205 0x26,
206 0x1e, 0x81, 0x2a, /* tag */
207 0x05, 0x82, 0x10, 0x22, /* Bmax */
208 0x03, 0x82, 0xc0, 0x40, /* R */
209 0x01, 0x82, 0x08, 0x11, /* Bmax_MS */
210 0x1c, 0x82, 0x60, 0x20, /* R_MS */
211 };
212
213 static uint8_t expected_ext_msg[] = {
214 0x26,
215 0x1e, 0x81, 0x2a, /* tag */
216 0x05, 0x82, 0x10, 0x22, /* Bmax */
217 0x03, 0x82, 0xc0, 0x40, /* R */
218 0x01, 0x82, 0x08, 0x11, /* Bmax_MS */
219 0x1c, 0x82, 0x60, 0x20, /* R_MS */
220 0x3c, 0x81, 0x78, /* ratio */
221 0x06, 0x82, 0x11, 0x44, /* Qdelay */
222 };
223
224 printf("----- %s START\n", __func__);
225
226 rc = bssgp_tx_fc_bvc(&bctx, tag, bmax, rate, bmax_ms, rate_ms,
227 NULL, NULL);
228
229 OSMO_ASSERT(rc >= 0);
230 OSMO_ASSERT(last_ns_tx_msg != NULL);
231 printf("Got message: %s\n", msgb_hexdump(last_ns_tx_msg));
232 OSMO_ASSERT(msgb_length(last_ns_tx_msg) == sizeof(expected_simple_msg));
233 OSMO_ASSERT(0 == memcmp(msgb_data(last_ns_tx_msg),
234 expected_simple_msg, sizeof(expected_simple_msg)));
235
236 rc = bssgp_tx_fc_bvc(&bctx, tag, bmax, rate, bmax_ms, rate_ms,
237 &ratio, &qdelay);
238
239 OSMO_ASSERT(rc >= 0);
240 OSMO_ASSERT(last_ns_tx_msg != NULL);
241 printf("Got message: %s\n", msgb_hexdump(last_ns_tx_msg));
242 OSMO_ASSERT(msgb_length(last_ns_tx_msg) == sizeof(expected_ext_msg));
243 OSMO_ASSERT(0 == memcmp(msgb_data(last_ns_tx_msg),
244 expected_ext_msg, sizeof(expected_ext_msg)));
245
246 msgb_free(last_ns_tx_msg);
247 last_ns_tx_msg = NULL;
248
249 printf("----- %s END\n", __func__);
250}
Jacob Erlbeckb535e392015-04-07 17:52:44 +0200251
Jacob Erlbeckcdebf742014-10-14 14:49:37 +0200252static struct log_info info = {};
253
254int main(int argc, char **argv)
255{
256 struct sockaddr_in bss_peer= {0};
257
258 osmo_init_logging(&info);
259 log_set_use_color(osmo_stderr_target, 0);
260 log_set_print_filename(osmo_stderr_target, 0);
261
262 bssgp_nsi = gprs_ns_instantiate(gprs_ns_callback, NULL);
263
264 bss_peer.sin_family = AF_INET;
265 bss_peer.sin_port = htons(32000);
266 bss_peer.sin_addr.s_addr = htonl(0x7f0000ff);
267
268 gprs_ns_nsip_connect(bssgp_nsi, &bss_peer, BSS_NSEI, BSS_NSEI+1);
269
270
271 printf("===== BSSGP test START\n");
272 test_bssgp_suspend_resume();
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100273 test_bssgp_status();
Jacob Erlbeckb535e392015-04-07 17:52:44 +0200274 test_bssgp_bad_reset();
Jacob Erlbeck9385d1e2015-05-06 09:29:32 +0200275 test_bssgp_flow_control_bvc();
Jacob Erlbeckcdebf742014-10-14 14:49:37 +0200276 printf("===== BSSGP test END\n\n");
277
278 exit(EXIT_SUCCESS);
279}