LISP: support for neighbor discovery
[vpp.git] / src / uri / uri_socket_server.c
index 64d3b49..4f4c5f3 100644 (file)
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
+#include <arpa/inet.h>
 #include <netdb.h>
 #include <vppinfra/format.h>
 #include <signal.h>
 #include <sys/ucontext.h>
+#include <sys/time.h>
 
 volatile int signal_received;
 
@@ -72,32 +74,63 @@ setup_signal_handler (void)
 int
 main (int argc, char *argv[])
 {
-  int sockfd, portno, n, sent, accfd;
+  int sockfd, portno, n, sent, accfd, reuse;
+  socklen_t client_addr_len;
   struct sockaddr_in serv_addr;
+  struct sockaddr_in client;
   struct hostent *server;
-  u8 *rx_buffer = 0;
+  u8 *rx_buffer = 0, no_echo = 0;
+  struct timeval start, end;
+  long rcvd = 0;
+  double deltat;
 
-  if (0 && argc < 3)
+  if (argc > 1 && argc < 3)
     {
-      fformat (stderr, "usage %s hostname port\n", argv[0]);
+      fformat (stderr, "usage %s host port\n", argv[0]);
       exit (0);
     }
 
+  if (argc >= 4)
+    {
+      no_echo = atoi (argv[3]);
+      portno = atoi (argv[2]);
+      server = gethostbyname (argv[1]);
+      if (server == NULL)
+       {
+         clib_unix_warning ("gethostbyname");
+         exit (1);
+       }
+    }
+  else
+    {
+      /* Defaults */
+      portno = 1234;
+      server = gethostbyname ("6.0.1.1");
+      if (server == NULL)
+       {
+         clib_unix_warning ("gethostbyname");
+         exit (1);
+       }
+    }
+
+
   setup_signal_handler ();
 
-  portno = 1234;               // atoi(argv[2]);
   sockfd = socket (AF_INET, SOCK_STREAM, 0);
   if (sockfd < 0)
     {
       clib_unix_error ("socket");
       exit (1);
     }
-  server = gethostbyname ("6.0.1.1");
-  if (server == NULL)
+
+  reuse = 1;
+  if (setsockopt (sockfd, SOL_SOCKET, SO_REUSEADDR, (const char *) &reuse,
+                 sizeof (reuse)) < 0)
     {
-      clib_unix_warning ("gethostbyname");
+      clib_unix_error ("setsockopt(SO_REUSEADDR) failed");
       exit (1);
     }
+
   bzero ((char *) &serv_addr, sizeof (serv_addr));
   serv_addr.sin_family = AF_INET;
   bcopy ((char *) server->h_addr,
@@ -109,7 +142,7 @@ main (int argc, char *argv[])
       exit (1);
     }
 
-  vec_validate (rx_buffer, 8999 /* jumbo mtu */ );
+  vec_validate (rx_buffer, 128 << 10);
 
   if (listen (sockfd, 5 /* backlog */ ) < 0)
     {
@@ -123,12 +156,17 @@ main (int argc, char *argv[])
       if (signal_received)
        break;
 
-      accfd = accept (sockfd, 0 /* don't care */ , 0);
+      client_addr_len = sizeof (struct sockaddr);
+      accfd = accept (sockfd, (struct sockaddr *) &client, &client_addr_len);
       if (accfd < 0)
        {
          clib_unix_warning ("accept");
          continue;
        }
+      fformat (stderr, "Accepted connection from: %s : %d\n",
+              inet_ntoa (client.sin_addr), client.sin_port);
+      gettimeofday (&start, NULL);
+
       while (1)
        {
          n = recv (accfd, rx_buffer, vec_len (rx_buffer), 0 /* flags */ );
@@ -136,6 +174,14 @@ main (int argc, char *argv[])
            {
              /* Graceful exit */
              close (accfd);
+             gettimeofday (&end, NULL);
+             deltat = (end.tv_sec - start.tv_sec);
+             deltat += (end.tv_usec - start.tv_usec) / 1000000.0;
+             clib_warning ("Finished in %.6f", deltat);
+             clib_warning ("%.4f Gbit/second %s",
+                           (((f64) rcvd * 8.0) / deltat / 1e9),
+                           no_echo ? "half" : "full");
+             rcvd = 0;
              break;
            }
          if (n < 0)
@@ -148,6 +194,10 @@ main (int argc, char *argv[])
          if (signal_received)
            break;
 
+         rcvd += n;
+         if (no_echo)
+           continue;
+
          sent = send (accfd, rx_buffer, n, 0 /* flags */ );
          if (n < 0)
            {