add stats
authorMazet Laurent <laurent.mazet@thalesgroup.com>
Fri, 11 Jul 2025 07:56:43 +0000 (09:56 +0200)
committerMazet Laurent <laurent.mazet@thalesgroup.com>
Fri, 11 Jul 2025 07:56:43 +0000 (09:56 +0200)
test.sh
ulvpn.c

diff --git a/test.sh b/test.sh
index 53e5f9146103e8f6704d72dae8dea54ff65b124a..b71bf59044b58f2440ef46e81fea89039cc81a70 100755 (executable)
--- a/test.sh
+++ b/test.sh
@@ -75,10 +75,15 @@ $@ \
   -t udp://10.1.0.1:1234 -u udp://10.1.0.1:1234 \
   -x $DEV://00:00:00:00:00:00/0809 $DEV://00:00:00:00:00:00/0819 \
   -y $DEV://00:00:00:00:00:00/0829 $DEV://00:00:00:00:00:00/0839 &
-pids="$pids $!"
+vpn=$!
+pids="$pids $vpn"
 
 sleep 4
 
+kill -USR1 $vpn
+
+sleep 1
+
 for p in $pids; do
     echo killing $p
     pkill -TERM -P $p 2>/dev/null
diff --git a/ulvpn.c b/ulvpn.c
index 889fb24101e9d46c5bf9075aa620cc525400757a..71a4c3eaff5fc5911ca752ec286489acfac60557 100644 (file)
--- a/ulvpn.c
+++ b/ulvpn.c
@@ -57,6 +57,17 @@ uint8_t aad[4] = { 0, 0, 0, 0 };
 
 DECLARE_VERBOSE_LEVEL (ulvpn, INFO);
 
+typedef struct {
+    unsigned int rx;
+    unsigned int tx;
+} stat_fd_t;
+
+stat_fd_t stat_tun_fd = { 0, 0 };
+stat_fd_t stat_udp_fd = { 0, 0 };
+stat_fd_t stat_blk_fd = { 0, 0 };
+stat_fd_t stat_red_fd = { 0, 0 };
+int raise_stat_sig = 0;
+
 void sig_handler (int sig)
 {
     switch (sig) {
@@ -66,6 +77,14 @@ void sig_handler (int sig)
     case SIGTERM:
         stop = 1;
         break;
+    case SIGUSR1:
+        printf ("tun: RX %9d TX %9d\nudp: RX %9d TX %9d\nblk: RX %9d TX %9d\nred: RX %9d TX %9d\n",
+                stat_tun_fd.rx, stat_tun_fd.tx, stat_udp_fd.rx,
+                stat_udp_fd.tx, stat_blk_fd.rx, stat_blk_fd.tx,
+                stat_red_fd.rx, stat_red_fd.tx);
+        fflush (stdout);
+        raise_stat_sig = 1;
+        break;
     }
 }
 
@@ -381,6 +400,7 @@ int main (int argc, char **argv)
     /* signals */
     signal(SIGINT, sig_handler);
     signal(SIGTERM, sig_handler);
+    signal(SIGUSR1, sig_handler);
 
     /* main loop */
     int rc = 0;
@@ -416,7 +436,9 @@ int main (int argc, char **argv)
         }
 
         if (-1 == select (max_fd + 1, &readset, NULL, NULL, NULL)) {
-            if (!stop) {
+            if (raise_stat_sig) {
+               raise_stat_sig = 0;
+            } else if (!stop) {
                 VERBOSE (ulvpn, ERROR, PRINTF ("select error\n"));
                 rc = 2;
             }
@@ -426,6 +448,7 @@ int main (int argc, char **argv)
         if (FD_ISSET (tun_fd, &readset)) {
             uint8_t buffer[MAXPAYLOAD] = {0};
             int rxlen = MAPEC_Receive (tun_fd, buffer, sizeof (buffer));
+            stat_tun_fd.rx++;
             if (rxlen < 0) {
                 VERBOSE (ulvpn, WARNING, PRINTF ("error when receiving from tun\n"));
            } else {
@@ -438,6 +461,7 @@ int main (int argc, char **argv)
                     } else {
                         VERBOSE (ulvpn, DEBUG, PRINTF ("packing %d bytes\n", txlen));
                         seqtx = MOREP_Send (txred_fd, 0x00, buffer, txlen);
+                        stat_red_fd.tx++;
                         if (seqtx < 0) {
                             VERBOSE (ulvpn, WARNING, PRINTF ("error when sending to CM red\n"));
                         } else {
@@ -446,6 +470,7 @@ int main (int argc, char **argv)
                     }
                 } else if (udp_fd >= 0) {
                     int txlen = MAPEC_Send (udp_fd, buffer, rxlen);
+                    stat_udp_fd.tx++;
                     if (txlen <= 0) {
                         VERBOSE (ulvpn, WARNING, PRINTF ("error when sending to udp\n"));
                     } else {
@@ -459,6 +484,7 @@ int main (int argc, char **argv)
         } else if (FD_ISSET (udp_fd, &readset)) {
             uint8_t buffer[MAXPAYLOAD] = {0};
             int rxlen = MAPEC_Receive (udp_fd, buffer, sizeof (buffer));
+            stat_udp_fd.rx++;
             if (rxlen < 0) {
                 VERBOSE (ulvpn, WARNING, PRINTF ("error when receiving from udp\n"));
             } else {
@@ -471,6 +497,7 @@ int main (int argc, char **argv)
                     } else {
                         VERBOSE (ulvpn, DEBUG, PRINTF ("packing %d bytes\n", txlen));
                         seqtx = MOREP_Send (txblack_fd, 0x02, buffer, txlen);
+                        stat_blk_fd.tx++;
                         if (seqtx < 0) {
                             VERBOSE (ulvpn, WARNING, PRINTF ("error when sending to CM black\n"));
                         } else {
@@ -479,6 +506,7 @@ int main (int argc, char **argv)
                     }
                 } else if (tun_fd >= 0) {
                     int txlen = MAPEC_Send (tun_fd, buffer, rxlen);
+                    stat_tun_fd.tx++;
                     if (txlen <= 0) {
                         VERBOSE (ulvpn, WARNING, PRINTF ("error when sending to tun\n"));
                     } else {
@@ -494,6 +522,7 @@ int main (int argc, char **argv)
             uint8_t msgtype = -1;
             int rxlen = 0;
             seqrx = MOREP_Receive (rxblack_fd, &msgtype, buffer, &rxlen);
+            stat_blk_fd.rx++;
             if (seqrx == -2) {
                 continue;
             }
@@ -516,6 +545,7 @@ int main (int argc, char **argv)
                         } else {
                             VERBOSE (ulvpn, DEBUG, PRINTF ("depacking %d bytes\n", txlen));
                             txlen = MAPEC_Send (udp_fd, buffer, txlen);
+                            stat_udp_fd.tx++;
                             if (txlen <= 0) {
                                 VERBOSE (ulvpn, WARNING, PRINTF ("error when sending to udp\n"));
                             } else {
@@ -537,6 +567,7 @@ int main (int argc, char **argv)
             uint8_t msgtype = -1;
             int rxlen = 0;
             seqrx = MOREP_Receive (rxred_fd, &msgtype, buffer, &rxlen);
+            stat_red_fd.rx++;
             if (seqrx == -2) {
                 continue;
             }
@@ -559,6 +590,7 @@ int main (int argc, char **argv)
                             } else {
                             VERBOSE (ulvpn, DEBUG, PRINTF ("depacking %d bytes\n", txlen));
                             txlen = MAPEC_Send (tun_fd, buffer, txlen);
+                            stat_tun_fd.tx++;
                             if (txlen <= 0) {
                                 VERBOSE (ulvpn, WARNING, PRINTF ("error when sending to tun\n"));
                             } else {