From 0a5723267a49bdd07d075d2ade49cc3760dd48e1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Johan=20Hug=C3=A9?= <johan.huge@nexedi.com>
Date: Fri, 29 May 2020 10:14:34 +0200
Subject: [PATCH] Update wrapper scripts and improve bash options on client and
 server

---
 packet-exchange/src/client.c | 28 +++++++++++++++++++++++----
 packet-exchange/src/server.c | 37 +++++++++++++++++++++++++++++-------
 scripts/run_server           |  3 ++-
 3 files changed, 56 insertions(+), 12 deletions(-)

diff --git a/packet-exchange/src/client.c b/packet-exchange/src/client.c
index c64d8c7..d67a561 100644
--- a/packet-exchange/src/client.c
+++ b/packet-exchange/src/client.c
@@ -5,7 +5,7 @@
  *
  *  -a                    Run the real time thread on CPU1
  *  -b                    Measure RTT
- *  -d TX_BUFFER_LEN      Set the length of tx buffer
+ *  -d BUF_LEN      Set the length of tx buffer
  *  -e                    Set a txtime (to be used in an ETF qdisc)
  *  -f IF                 Set the network interface to be used
  *  -i USEC               Wake up the real time thread every USEC microseconds (Default: 10ms)
@@ -184,10 +184,25 @@ static void do_tsn_task(struct thread_param * param, uint64_t next_txtime) {
     }
 }
 
+static void help(char *argv[]) {
+  printf("Usage: %s [-abet] [-d BUF_LEN] [-f IF] [-i USEC] [-l N] [-p PRIO] [-r USEC]\n\n", argv[0]);
+  printf("       -a                    Run the real time thread on CPU1\n");
+  printf("       -b                    Measure RTT\n");
+  printf("       -d BUF_LEN      Set the length of tx buffer\n");
+  printf("       -e                    Set a txtime (to be used in an ETF qdisc)\n");
+  printf("       -f IF                 Set the network interface to be used\n");
+  printf("       -i USEC               Wake up the real time thread every USEC microseconds (Default: 10ms)\n");
+  printf("       -l N                  Wake up the real time thread N times (Default: 0)\n");
+  printf("       -p PRIO               Run the real time thread at priority PRIO\n");
+  printf("       -r USEC               Refresh the non real time main thread every USEC microseconds (Default: 50ms)\n");
+  printf("       -t                    Enable timestamps\n");
+  printf("\n");
+}
+
 static void process_options(int argc, char *argv[], thread_param_t *param,
                             main_param_t *main_param) {
   for (;;) {
-    int c = getopt(argc, argv, "abd:ef:i:l:p:q:r:t");
+    int c = getopt(argc, argv, "abd:efh:i:l:p:q:r:t");
 
     if (c == -1) break;
 
@@ -201,7 +216,7 @@ static void process_options(int argc, char *argv[], thread_param_t *param,
       case 'd':
         main_param->tx_buffer_len = atoi(optarg);
         if( main_param->tx_buffer_len < 1 ) {
-          fprintf(stderr, "TX_BUFFER_LEN should be greater than 1\n");
+          fprintf(stderr, "BUF_LEN should be greater than 1\n");
           exit(EXIT_FAILURE);
         }
         break;
@@ -211,6 +226,10 @@ static void process_options(int argc, char *argv[], thread_param_t *param,
       case 'f':
         strcpy(param->network_if, optarg);
         break;
+      case 'h':
+        help(argv);
+        exit(EXIT_SUCCESS);
+        break;
       case 'i':
         param->interval = atoi(optarg) * 1000;
         break;
@@ -230,13 +249,14 @@ static void process_options(int argc, char *argv[], thread_param_t *param,
         param->enable_timestamps = 1;
         break;
       default:
+        help(argv);
         exit(EXIT_FAILURE);
         break;
     }
   }
 
   if (argc != optind + 1) {
-    printf("Usage: %s server_ip\n", argv[0]);
+    help(argv);
     exit(EXIT_FAILURE);
   }
   param->ip_address = argv[optind];
diff --git a/packet-exchange/src/server.c b/packet-exchange/src/server.c
index 7b8a7e8..885a5eb 100644
--- a/packet-exchange/src/server.c
+++ b/packet-exchange/src/server.c
@@ -4,7 +4,7 @@
  * Bash options:
  *
  *  -a                    Run the real time thread on CPU1
- *  -b                    Server side RTT
+ *  -b CLIENT_IP          Server side RTT
  *  -d TX_BUFFER_LEN      Set the length of tx buffer
  *  -f IF                 Set the network interface to be used
  *  -p PRIO               Run the real time thread at priority PRIO
@@ -54,7 +54,7 @@ typedef struct thread_param {
   enum TSNTask tsn_task;
 
   int sockfd;
-  const char *ip_address;
+  char *ip_address;
   char network_if[256];
 
   thread_stat_t stats;
@@ -160,10 +160,28 @@ int main(int argc, char *argv[]) {
   exit(EXIT_SUCCESS);
 }
 
+static void help(char *argv[]) {
+  printf(
+      "Usage: %s [-a] [-b CLIENT_IP] [-d BUF_LEN] [-f IF] [-p PRIO] [-r "
+      "USEC]\n\n",
+      argv[0]);
+  printf("       -a                    Run the real time thread on CPU1\n");
+  printf("       -b CLIENT_IP          Server side RTT\n");
+  printf("       -d BUF_LEN            Set the length of tx buffer\n");
+  printf("       -f IF                 Set the network interface to be used\n");
+  printf(
+      "       -p PRIO               Run the real time thread at priority "
+      "PRIO\n");
+  printf(
+      "       -r USEC               Refresh the non real time main thread "
+      "every USEC microseconds\n");
+  printf("\n");
+}
+
 static void process_options(int argc, char *argv[], thread_param_t *param,
                             main_param_t *main_param) {
   for (;;) {
-    int c = getopt(argc, argv, "abd:f:p:r:");
+    int c = getopt(argc, argv, "ab:d:f:hp:r:");
 
     if (c == -1) break;
 
@@ -173,17 +191,22 @@ static void process_options(int argc, char *argv[], thread_param_t *param,
         break;
       case 'b':
         param->tsn_task = RTT_TASK;
+        strcpy(param->ip_address, optarg);
         break;
       case 'd':
         main_param->tx_buffer_len = atoi(optarg);
         if (main_param->tx_buffer_len < 1) {
-          fprintf(stderr, "TX_BUFFER_LEN should be greater than 1\n");
+          fprintf(stderr, "BUF_LEN should be greater than 1\n");
           exit(EXIT_FAILURE);
         }
         break;
       case 'f':
         strcpy(param->network_if, optarg);
         break;
+      case 'h':
+        help(argv);
+        exit(EXIT_SUCCESS);
+        break;
       case 'p':
         param->priority = atoi(optarg);
         break;
@@ -191,15 +214,15 @@ static void process_options(int argc, char *argv[], thread_param_t *param,
         main_param->refresh_rate = atoi(optarg);
         break;
       default:
+        help(argv);
         exit(EXIT_FAILURE);
         break;
     }
   }
 
-  if (argc != optind + 1) {
-    printf("Usage: %s client_ip\n", argv[0]);
+  if (argc != optind) {
+    help(argv);
     exit(EXIT_FAILURE);
   }
-  param->ip_address = argv[optind];
 }
 
diff --git a/scripts/run_server b/scripts/run_server
index 61a654d..9042376 100755
--- a/scripts/run_server
+++ b/scripts/run_server
@@ -7,12 +7,13 @@ usage() {
   exit 1;
 }
 
+server_options="-a -p 99"
 server_options="-a -p 99"
 
 while getopts "bt:" opt; do
   case "${opt}" in
     b )
-        server_options+=" -b"
+        server_options+=" -b 192.168.99.26"
         ;;
     t )
         use_tcpdump=1
-- 
2.30.9