aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorPablo Neira Ayuso <pablo@gnumonks.org>2011-10-14 20:57:10 +0200
committerPablo Neira Ayuso <pablo@netfilter.org>2011-10-14 21:03:52 +0200
commit6c3c279489da076366d1c2459debfc815df89f7b (patch)
tree4259bc694a307aba06e9ea929e26bae3ebc07779 /examples
parent370794a4a738b92d91407df8609c7c4e31c8be43 (diff)
examples: update LAPD over datagram tests
Now they can be used to generate a number of messages from the user and to measure the RTT of LAPD over datagram messages. Still, they need to be expanded to take the origin and remote IPs as argument from the command line, later.
Diffstat (limited to 'examples')
-rw-r--r--examples/lapd-over-datagram-network.c76
-rw-r--r--examples/lapd-over-datagram-user.c129
2 files changed, 117 insertions, 88 deletions
diff --git a/examples/lapd-over-datagram-network.c b/examples/lapd-over-datagram-network.c
index 3dbc210..58ee3fa 100644
--- a/examples/lapd-over-datagram-network.c
+++ b/examples/lapd-over-datagram-network.c
@@ -40,7 +40,7 @@ static int sapi = 63, tei = 0;
void sighandler(int foo)
{
lapd_instance_free(lapd);
- LOGP(DLINP, LOGL_NOTICE, "closing LAPD.\n");
+ LOGP(DLAPDTEST, LOGL_NOTICE, "closing LAPD.\n");
exit(EXIT_SUCCESS);
}
@@ -48,12 +48,13 @@ int read_cb(struct osmo_dgram_conn *conn, struct msgb *msg)
{
int error;
- LOGP(DLINP, LOGL_NOTICE, "received message from datagram\n");
+ LOGP(DLAPDTEST, LOGL_DEBUG, "received message from datagram\n");
if (lapd_receive(lapd, msg, &error) < 0) {
- LOGP(DLINP, LOGL_ERROR, "lapd_receive returned error!\n");
+ LOGP(DLAPDTEST, LOGL_ERROR, "lapd_receive returned error!\n");
return -1;
}
+
return 0;
}
@@ -61,7 +62,7 @@ void lapd_tx_cb(struct msgb *msg, void *cbdata)
{
struct osmo_dgram_conn *conn = cbdata;
- LOGP(DLINP, LOGL_NOTICE, "sending message over datagram\n");
+ LOGP(DLAPDTEST, LOGL_DEBUG, "sending message over datagram\n");
osmo_dgram_conn_send(conn, msg);
}
@@ -80,10 +81,30 @@ void lapd_rx_cb(struct osmo_dlsap_prim *dp, uint8_t tei, uint8_t sapi,
case PRIM_DL_DATA:
case PRIM_DL_UNIT_DATA:
if (dp->oph.operation == PRIM_OP_INDICATION) {
+ struct msgb *nmsg;
+ char *ptr;
+ int x;
+
msg->l2h = msg->l3h;
+
DEBUGP(DLAPDTEST, "RX: %s sapi=%d tei=%d\n",
osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)),
sapi, tei);
+
+ LOGP(DLAPDTEST, LOGL_DEBUG, "forwarding message\n");
+
+ nmsg = msgb_alloc(1024, "LAPD/test");
+ if (nmsg == NULL) {
+ LOGP(DLAPDTEST, LOGL_ERROR, "cannot alloc msg\n");
+ return;
+ }
+ ptr = (char *)msgb_put(nmsg, sizeof(int));
+
+ x = *((int *)msg->data);
+ memcpy(ptr, &x, sizeof(int));
+
+ /* send the message back to client over LAPD */
+ lapd_transmit(lapd, tei, sapi, msg);
return;
}
break;
@@ -96,40 +117,14 @@ void lapd_rx_cb(struct osmo_dlsap_prim *dp, uint8_t tei, uint8_t sapi,
}
}
-static int kbd_cb(struct osmo_fd *fd, unsigned int what)
-{
- char buf[1024];
- struct msgb *msg;
- uint8_t *ptr;
- int ret;
-
- ret = read(STDIN_FILENO, buf, sizeof(buf));
-
- LOGP(DLAPDTEST, LOGL_NOTICE, "read %d byte from keyboard\n", ret);
-
- msg = msgb_alloc_headroom(1024, 128, "lapd_test");
- if (msg == NULL) {
- LOGP(DLINP, LOGL_ERROR, "lapd: cannot allocate message\n");
- return 0;
- }
- ptr = msgb_put(msg, strlen(buf));
- memcpy(ptr, buf, strlen(buf));
- lapd_transmit(lapd, tei, sapi, msg);
-
- LOGP(DLAPDTEST, LOGL_NOTICE, "message of %d bytes sent\n", msg->len);
-
- return 0;
-}
-
-int main(void)
+int main(int argc, char *argv[])
{
- struct osmo_fd *kbd_ofd;
int teip;
tall_test = talloc_named_const(NULL, 1, "lapd_test");
osmo_init_logging(&lapd_test_log_info);
- log_set_log_level(osmo_stderr_target, 1);
+ log_set_log_level(osmo_stderr_target, LOGL_NOTICE);
/*
* initialize datagram server.
@@ -149,13 +144,13 @@ int main(void)
lapd = lapd_instance_alloc(1, lapd_tx_cb, conn, lapd_rx_cb, conn,
&lapd_profile_sat);
if (lapd == NULL) {
- LOGP(DLINP, LOGL_ERROR, "cannot allocate instance\n");
+ LOGP(DLAPDTEST, LOGL_ERROR, "cannot allocate instance\n");
exit(EXIT_FAILURE);
}
teip = lapd_tei_alloc(lapd, tei);
if (teip == 0) {
- LOGP(DLINP, LOGL_ERROR, "cannot assign TEI\n");
+ LOGP(DLAPDTEST, LOGL_ERROR, "cannot assign TEI\n");
exit(EXIT_FAILURE);
}
@@ -164,18 +159,7 @@ int main(void)
exit(EXIT_FAILURE);
}
- kbd_ofd = talloc_zero(tall_test, struct osmo_fd);
- if (!kbd_ofd) {
- LOGP(DLAPDTEST, LOGL_ERROR, "OOM\n");
- exit(EXIT_FAILURE);
- }
- kbd_ofd->fd = STDIN_FILENO;
- kbd_ofd->when = BSC_FD_READ;
- kbd_ofd->data = conn;
- kbd_ofd->cb = kbd_cb;
- osmo_fd_register(kbd_ofd);
-
- LOGP(DLINP, LOGL_NOTICE, "Entering main loop\n");
+ LOGP(DLAPDTEST, LOGL_NOTICE, "Entering main loop\n");
while(1) {
osmo_select_main(0);
diff --git a/examples/lapd-over-datagram-user.c b/examples/lapd-over-datagram-user.c
index 4c76fac..68fa424 100644
--- a/examples/lapd-over-datagram-user.c
+++ b/examples/lapd-over-datagram-user.c
@@ -2,8 +2,11 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <signal.h>
#include <unistd.h>
+#include <arpa/inet.h>
+#include <osmocom/core/linuxlist.h>
#include <osmocom/core/talloc.h>
#include <osmocom/core/msgb.h>
#include <osmocom/core/logging.h>
@@ -14,6 +17,15 @@
#include <osmocom/netif/datagram.h>
+static LLIST_HEAD(msg_sent_list);
+
+struct msg_sent {
+ struct llist_head head;
+ struct msgb *msg;
+ int num;
+ struct timeval tv;
+};
+
#define DLAPDTEST 0
struct log_info_cat lapd_test_cat[] = {
@@ -47,8 +59,6 @@ static int read_cb(struct osmo_dgram_conn *conn, struct msgb *msg)
{
int error;
- LOGP(DLINP, LOGL_NOTICE, "received message from datagram\n");
-
if (lapd_receive(lapd, msg, &error) < 0) {
LOGP(DLINP, LOGL_ERROR, "lapd_receive returned error!\n");
return -1;
@@ -60,7 +70,7 @@ static void *tall_test;
void lapd_tx_cb(struct msgb *msg, void *cbdata)
{
- LOGP(DLINP, LOGL_NOTICE, "sending message over datagram\n");
+ LOGP(DLINP, LOGL_DEBUG, "sending message over datagram\n");
osmo_dgram_conn_send(conn, msg);
}
@@ -68,10 +78,45 @@ void lapd_rx_cb(struct osmo_dlsap_prim *dp, uint8_t tei, uint8_t sapi,
void *rx_cbdata)
{
struct msgb *msg = dp->oph.msg;
+ int *__msgs = rx_cbdata;
+ int num_msgs = *__msgs;
switch (dp->oph.primitive) {
case PRIM_DL_EST:
DEBUGP(DLAPDTEST, "DL_EST: sapi(%d) tei(%d)\n", sapi, tei);
+
+ int i;
+ for (i=0; i<num_msgs; i++) {
+ struct msgb *msg;
+ struct msg_sent *msg_sent;
+ char *ptr;
+ int x;
+
+ msg = msgb_alloc(1024, "LAPD/test");
+ if (msg == NULL) {
+ LOGP(DLINP, LOGL_ERROR, "cannot alloc msg\n");
+ return;
+ }
+ ptr = (char *)msgb_put(msg, sizeof(int));
+
+ x = htonl(i);
+ memcpy(ptr, &x, sizeof(int));
+
+ msg_sent = talloc_zero(NULL, struct msg_sent);
+ if (msg_sent == NULL) {
+ LOGP(DLINP, LOGL_ERROR, "can't alloc struct\n");
+ return;
+ }
+ msg_sent->msg = msg;
+ gettimeofday(&msg_sent->tv, NULL);
+ msg_sent->num = i;
+ llist_add(&msg_sent->head, &msg_sent_list);
+
+ lapd_transmit(lapd, tei, sapi, msg);
+
+ LOGP(DLAPDTEST, LOGL_DEBUG, "enqueueing msg %d of "
+ "%d bytes to be sent over LAPD\n", i, msg->len);
+ }
break;
case PRIM_DL_REL:
DEBUGP(DLAPDTEST, "DL_REL: sapi(%d) tei(%d)\n", sapi, tei);
@@ -83,7 +128,36 @@ void lapd_rx_cb(struct osmo_dlsap_prim *dp, uint8_t tei, uint8_t sapi,
DEBUGP(DLAPDTEST, "RX: %s sapi=%d tei=%d\n",
osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)),
sapi, tei);
- return;
+
+ int num;
+ struct msg_sent *cur, *tmp, *found = NULL;
+
+ num = ntohl(*((int *)msg->data));
+ LOGP(DLINP, LOGL_DEBUG,
+ "received msg number %d\n", num);
+
+ llist_for_each_entry_safe(cur, tmp,
+ &msg_sent_list, head) {
+ if (cur->num == num) {
+ llist_del(&cur->head);
+ found = cur;
+ break;
+ }
+ }
+ if (found) {
+ struct timeval tv, diff;
+
+ gettimeofday(&tv, NULL);
+ timersub(&tv, &found->tv, &diff);
+
+ LOGP(DLINP, LOGL_NOTICE, "message %d replied "
+ "in %lu.%.6lu\n",
+ num, diff.tv_sec, diff.tv_usec);
+ talloc_free(found);
+ } else {
+ LOGP(DLINP, LOGL_ERROR,
+ "message %d not found!\n", num);
+ }
}
break;
case PRIM_MDL_ERROR:
@@ -95,45 +169,27 @@ void lapd_rx_cb(struct osmo_dlsap_prim *dp, uint8_t tei, uint8_t sapi,
}
}
-static int kbd_cb(struct osmo_fd *fd, unsigned int what)
+int main(int argc, char *argv[])
{
- char buf[1024];
- struct msgb *msg;
- uint8_t *ptr;
- int ret;
-
- ret = read(STDIN_FILENO, buf, sizeof(buf));
+ int num_msgs;
- LOGP(DLAPDTEST, LOGL_NOTICE, "read %d byte from keyboard\n", ret);
+ signal(SIGINT, sighandler);
- msg = msgb_alloc(1024, "LAPD/test");
- if (msg == NULL) {
- LOGP(DLINP, LOGL_ERROR, "lapd: cannot allocate message\n");
- return 0;
+ if (argc != 2) {
+ printf("Usage: %s [num_msgs]\n", argv[0]);
+ exit(EXIT_FAILURE);
}
- ptr = msgb_put(msg, strlen(buf));
- memcpy(ptr, buf, ret);
-
- lapd_transmit(lapd, tei, sapi, msg);
-
- LOGP(DLAPDTEST, LOGL_NOTICE, "message of %d bytes sent\n", msg->len);
-
- return 0;
-}
-
-int main(void)
-{
- struct osmo_fd *kbd_ofd;
+ num_msgs = atoi(argv[1]);
tall_test = talloc_named_const(NULL, 1, "lapd_test");
osmo_init_logging(&lapd_test_log_info);
- log_set_log_level(osmo_stderr_target, 1);
+ log_set_log_level(osmo_stderr_target, LOGL_NOTICE);
/*
* initialize LAPD stuff.
*/
- lapd = lapd_instance_alloc(0, lapd_tx_cb, NULL, lapd_rx_cb, NULL,
+ lapd = lapd_instance_alloc(0, lapd_tx_cb, NULL, lapd_rx_cb, &num_msgs,
&lapd_profile_sat);
if (lapd == NULL) {
LOGP(DLINP, LOGL_ERROR, "cannot allocate instance\n");
@@ -165,17 +221,6 @@ int main(void)
exit(EXIT_FAILURE);
}
- kbd_ofd = talloc_zero(tall_test, struct osmo_fd);
- if (!kbd_ofd) {
- LOGP(DLAPDTEST, LOGL_ERROR, "OOM\n");
- exit(EXIT_FAILURE);
- }
- kbd_ofd->fd = STDIN_FILENO;
- kbd_ofd->when = BSC_FD_READ;
- kbd_ofd->data = conn;
- kbd_ofd->cb = kbd_cb;
- osmo_fd_register(kbd_ofd);
-
LOGP(DLINP, LOGL_NOTICE, "Entering main loop\n");
while(1) {