aboutsummaryrefslogtreecommitdiffstats
path: root/tests/osmo-pcap-test/osmux_test.c
blob: 91637537dcf54169fe731c2013746d4786579016 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 * (C) 2012 by Pablo Neira Ayuso <pablo@gnumonks.org>
 * (C) 2012 by On Waves ehf <http://www.on-waves.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <stdio.h>
#include <pcap.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <string.h>
#include <dlfcn.h>
#include <unistd.h>

#include <osmocom/core/msgb.h>
#include <osmocom/core/timer.h>
#include <osmocom/core/select.h>
#include <osmocom/core/logging.h>
#include <osmocom/core/application.h>
#include <osmocom/core/talloc.h>

#include <osmocom/netif/rtp.h>
#include <osmocom/netif/osmux.h>

#include "osmo_pcap.h"

#define DOSMUXTEST 0

/*
 * This is the output handle for osmux, it stores last RTP sequence and
 * timestamp that has been used. There should be one per circuit ID.
 */
static struct osmux_out_handle h_output;

static void tx_cb(struct msgb *msg, void *data)
{
	printf("now sending message scheduled [emulated], msg=%p\n", msg);
	/*
	 * Here we should call the real function that sends the message
	 * instead of releasing it.
	 */
	msgb_free(msg);
}

static void deliver(struct msgb *batch_msg)
{
	struct osmux_hdr *osmuxh;

	printf("sending batch (len=%d) [emulated]\n", batch_msg->len);

	/* This code below belongs to the osmux receiver */
	while((osmuxh = osmux_xfrm_output_pull(batch_msg)) != NULL)
		osmux_xfrm_output_sched(&h_output, osmuxh);
	msgb_free(batch_msg);
}

/*
 * This is the input handle for osmux. It stores the last osmux sequence that
 * has been used and the deliver function that sends the osmux batch.
 */
struct osmux_in_handle h_input = {
	.osmux_seq	= 0, /* sequence number to start OSmux message from */
	.batch_factor = 4, /* batch up to 4 RTP messages */
	.deliver	= deliver,
};

#define MAX_CONCURRENT_CALLS	8

static int ccid[MAX_CONCURRENT_CALLS] = { -1, -1, -1, -1, -1, -1, -1, -1 };

static void register_ccid(uint32_t ssrc)
{
       int i, found = 0;

       for (i=0; i<MAX_CONCURRENT_CALLS; i++) {
               if (ccid[i] == ssrc)
                       continue;
               if (ccid[i] < 0) {
                       found = 1;
                       break;
               }
       }

       if (found) {
               ccid[i] = ssrc;
               LOGP(DOSMUXTEST, LOGL_DEBUG, "mapping ssrc=%u to ccid=%d\n",
                       ntohl(ssrc), i);
       } else {
               LOGP(DOSMUXTEST, LOGL_ERROR, "cannot map ssrc to ccid!\n");
       }
}

static int get_ccid(uint32_t ssrc)
{
       int i, found = 0;

       for (i=0; i<MAX_CONCURRENT_CALLS; i++) {
               if (ccid[i] == ssrc) {
                       found = 1;
                       break;
               }
       }

       return found ? i : -1;
}

static int pcap_test_run(struct msgb *msg)
{
	int ret, ccid;
	struct rtp_hdr *rtph;

	rtph = osmo_rtp_get_hdr(msg);
	if (rtph == NULL)
		return 0;

	ccid = get_ccid(rtph->ssrc);
	if (ccid < 0)
		register_ccid(rtph->ssrc);

	while ((ret = osmux_xfrm_input(&h_input, msg, ccid)) > 1) {
		/* batch full, deliver it */
		osmux_xfrm_input_deliver(&h_input);
	}
	if (ret == -1)
		printf("something is wrong\n");

	return 0;
}

static struct osmo_pcap osmo_pcap;

static void osmo_pcap_pkt_timer_cb(void *data)
{
	if (osmo_pcap_test_run(&osmo_pcap, IPPROTO_UDP, pcap_test_run) < 0) {
		osmo_pcap_stats_printf();
		printf("\e[1;34mDone.\e[0m\n");
		osmo_pcap_test_close(osmo_pcap.h);
		exit(EXIT_SUCCESS);
	}
}

struct log_info_cat osmux_test_cat[] = {
	[DOSMUXTEST] = {
		.name = "DOSMUXTEST",
		.description = "osmux test",
		.color = "\033[1;35m",
		.enabled = 1, .loglevel = LOGL_NOTICE,
	},
};

const struct log_info osmux_log_info = {
	.filter_fn = NULL,
	.cat = osmux_test_cat,
	.num_cat = ARRAY_SIZE(osmux_test_cat),
};

static void *tall_test;

int main(int argc, char *argv[])
{
	int ret;

	if (argc != 2) {
		fprintf(stderr, "Wrong usage:\n");
		fprintf(stderr, "%s <pcap_file>\n", argv[0]);
		fprintf(stderr, "example: %s file.pcap\n", argv[0]);
		exit(EXIT_FAILURE);
	}

	tall_test = talloc_named_const(NULL, 1, "osmux_pcap_test");
	osmo_init_logging(&osmux_log_info);
	log_set_log_level(osmo_stderr_target, LOGL_DEBUG);

	osmo_pcap_init();

	printf("\e[1;34mStarting test...\e[0m\n");

	osmo_pcap.h = osmo_pcap_test_open(argv[1]);
	if (osmo_pcap.h == NULL)
		exit(EXIT_FAILURE);

	osmo_pcap.timer.cb = osmo_pcap_pkt_timer_cb;

	osmux_xfrm_input_init(&h_input);
	osmux_xfrm_output_init2(&h_output, 0, 98);
	osmux_xfrm_output_set_tx_cb(&h_output, tx_cb, NULL);

	/* first run */
	osmo_pcap_pkt_timer_cb(NULL);

	while(1) {
		osmo_select_main(0);
	}

	return ret;
}