From bea91172a914b0b135360d2b73401c60f738e9c1 Mon Sep 17 00:00:00 2001 From: Laurent MAZET Date: Fri, 3 Oct 2025 17:40:21 +0200 Subject: [PATCH] add test for udp latency --- udp_lat.c | 189 +++++++++++++++++++++++++++++++++++++++++++++ udp.c => udp_rtt.c | 2 +- 2 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 udp_lat.c rename udp.c => udp_rtt.c (99%) diff --git a/udp_lat.c b/udp_lat.c new file mode 100644 index 0000000..2d7c744 --- /dev/null +++ b/udp_lat.c @@ -0,0 +1,189 @@ +/* depend: */ +/* cflags: */ +/* linker: mtime.o test.o stat.o -lm -lpthread -lrt */ + +#include +#include +#include + +#include +#include + +#include +#include +#include + +#include "mtime.h" +#include "test.h" + +/* global variables */ + +dts_t *deltas = NULL; +int nb_measurements = 0; + +int port = 1024; + +char *message = "UDP socket latency"; + +void _usage_ext (FILE *fd) +{ + fprintf (fd, "... port\n"); + fprintf (fd, " port: port number (%d)\n", port); +} +void (*usage_ext) (FILE *) = _usage_ext; + +int _parse_arg_ext (char *arg) +{ + static int narg = 0; + + int rc = 0; + switch (narg) { + case 0: port = atoi (arg); if (port < 0) rc = 1; break; + default: rc = 1; + } + narg++; + + return rc; +} +int (*parse_arg_ext) (char *) = _parse_arg_ext; + +#define MAXBUF 1024 +#define TIMER 1000 + +pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; +ts_t ts1, ts2; +int rc = 0; +int localhost_ip = 0x7f000001; + +void *ping (__attribute__((unused)) void *arg) +{ + + int sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP); + if (sock == -1) { + fprintf (stderr, "ping error: socket\n"); + rc = 1; + pthread_exit (NULL); + } +/* + struct sockaddr_in local = { 0 }; + local.sin_family = AF_INET; + local.sin_port = htons (port); + local.sin_addr.s_addr = htonl (INADDR_ANY); + + if (bind (sock, (struct sockaddr *)&local, sizeof (local)) == -1) { + fprintf (stderr, "ping error: bind\n"); + rc = 1; + pthread_exit (NULL); + } +*/ + struct sockaddr_in remote = { 0 }; + remote.sin_family = AF_INET; + remote.sin_port = htons (port); + remote.sin_addr.s_addr = htonl (localhost_ip); + + printf ("Sending ping...\n"); + + pthread_mutex_unlock (&mutex); + usleep (TIMER); + + for (int i = 0; i < nb_measurements; i++) { + + char buffer[MAXBUF] = { 0 }; + sprintf (buffer, "ping %d", i); + + pthread_mutex_lock (&mutex); + pthread_mutex_unlock (&mutex); + + sys_timestamp (&ts1); + if (sendto (sock, buffer, strlen (buffer) + 1, 0, (struct sockaddr *)&remote, sizeof (remote)) == -1) { + fprintf (stderr, "ping error: sendto (%d)\n", i); + rc = 1; + pthread_exit (NULL); + } + + usleep (TIMER); + } + + close (sock); + + pthread_exit (NULL); +} + +void *pong (__attribute__((unused)) void *arg) +{ + + int sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP); + if (sock == -1) { + fprintf (stderr, "pong error: socket\n"); + rc = 1; + pthread_exit (NULL); + } + + struct sockaddr_in local = { 0 }; + local.sin_family = AF_INET; + local.sin_port = htons (port); + local.sin_addr.s_addr = htonl (INADDR_ANY); + + if (bind (sock, (struct sockaddr *)&local, sizeof (local)) == -1) { + fprintf (stderr, "pong error: bind\n"); + rc = 1; + pthread_exit (NULL); + } + + printf ("Sending pong...\n"); + + for (int i = 0; i < nb_measurements; i++) { + + pthread_mutex_unlock (&mutex); + + char buffer[MAXBUF] = { 0 }; + if (recv (sock, buffer, MAXBUF, 0) == -1) { + fprintf (stderr, "pong error: recv_from (%d)\n", i); + rc = 1; + pthread_exit (NULL); + } + sys_timestamp (&ts2); + deltas[i] = diff_timestamp (&ts2, &ts1); + + pthread_mutex_lock (&mutex); + usleep (TIMER); + } + + close (sock); + + pthread_exit (NULL); +} + +int test (dts_t *buffer, int nb) +{ + + /* set global variables */ + + deltas = buffer; + nb_measurements = nb; + + /* udp socket test */ + + pthread_mutex_lock (&mutex); + + pthread_t tid1; + if (pthread_create (&tid1, NULL, ping, NULL) != 0) { + fprintf (stderr, "error on pthread_create\n"); + return 1; + } + + pthread_mutex_lock (&mutex); + + pthread_t tid2; + if (pthread_create (&tid2, NULL, pong, NULL) != 0) { + fprintf (stderr, "error on pthread_create\n"); + return 1; + } + + pthread_join (tid1, NULL); + pthread_join (tid2, NULL); + + pthread_mutex_destroy (&mutex); + + return rc; +} diff --git a/udp.c b/udp_rtt.c similarity index 99% rename from udp.c rename to udp_rtt.c index a66511a..de97b85 100644 --- a/udp.c +++ b/udp_rtt.c @@ -28,7 +28,7 @@ char *remote_host = "localhost"; int remote_port = 1025; int mode = 0; -char *message = "udp socket"; +char *message = "UDP socket RTT"; void _usage_ext (FILE *fd) { -- 2.30.2