bpf: sockmap sample, use fork() for send and recv
authorJohn Fastabend <john.fastabend@gmail.com>
Mon, 22 Jan 2018 18:36:02 +0000 (10:36 -0800)
committerDaniel Borkmann <daniel@iogearbox.net>
Wed, 24 Jan 2018 09:46:58 +0000 (10:46 +0100)
Currently for SENDMSG tests first send completes then recv runs. This
does not work well for large data sizes and/or many iterations. So
fork the recv and send handler so that we run both send and recv. In
the future we can add a parameter to do more than a single fork of
tx/rx.

With this we can get many GBps of data which helps exercise the
sockmap code.

Signed-off-by: John Fastabend <john.fastabend@gmail.com>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
samples/sockmap/sockmap_user.c

index ccc7173..6ab3ec2 100644 (file)
@@ -23,6 +23,7 @@
 #include <stdbool.h>
 #include <signal.h>
 #include <fcntl.h>
+#include <sys/wait.h>
 
 #include <sys/time.h>
 #include <sys/types.h>
@@ -195,7 +196,7 @@ static int msg_loop(int fd, int iov_count, int iov_length, int cnt,
 {
        struct msghdr msg = {0};
        struct iovec *iov;
-       int i, flags = 0;
+       int i, flags = MSG_NOSIGNAL;
 
        iov = calloc(iov_count, sizeof(struct iovec));
        if (!iov)
@@ -275,25 +276,47 @@ out_errno:
 
 static int sendmsg_test(int iov_count, int iov_buf, int cnt, int verbose)
 {
+       int txpid, rxpid, err = 0;
        struct msg_stats s = {0};
-       int err;
-
-       err = msg_loop(c1, iov_count, iov_buf, cnt, &s, true);
-       if (err) {
-               fprintf(stderr,
-                       "msg_loop_tx: iov_count %i iov_buf %i cnt %i err %i\n",
-                       iov_count, iov_buf, cnt, err);
-               return err;
+       int status;
+
+       errno = 0;
+
+       rxpid = fork();
+       if (rxpid == 0) {
+               err = msg_loop(p2, iov_count, iov_buf, cnt, &s, false);
+               if (err)
+                       fprintf(stderr,
+                               "msg_loop_rx: iov_count %i iov_buf %i cnt %i err %i\n",
+                               iov_count, iov_buf, cnt, err);
+               fprintf(stdout, "rx_sendmsg: TX_bytes %zu RX_bytes %zu\n",
+                       s.bytes_sent, s.bytes_recvd);
+               shutdown(p2, SHUT_RDWR);
+               shutdown(p1, SHUT_RDWR);
+               exit(1);
+       } else if (rxpid == -1) {
+               perror("msg_loop_rx: ");
+               return errno;
        }
 
-       err = msg_loop(p2, iov_count, iov_buf, cnt, &s, false);
-       if (err)
-               fprintf(stderr,
-                       "msg_loop_rx: iov_count %i iov_buf %i cnt %i err %i\n",
-                       iov_count, iov_buf, cnt, err);
+       txpid = fork();
+       if (txpid == 0) {
+               err = msg_loop(c1, iov_count, iov_buf, cnt, &s, true);
+               if (err)
+                       fprintf(stderr,
+                               "msg_loop_tx: iov_count %i iov_buf %i cnt %i err %i\n",
+                               iov_count, iov_buf, cnt, err);
+               fprintf(stdout, "tx_sendmsg: TX_bytes %zu RX_bytes %zu\n",
+                       s.bytes_sent, s.bytes_recvd);
+               shutdown(c1, SHUT_RDWR);
+               exit(1);
+       } else if (txpid == -1) {
+               perror("msg_loop_tx: ");
+               return errno;
+       }
 
-       fprintf(stdout, "sendmsg: TX_bytes %zu RX_bytes %zu\n",
-               s.bytes_sent, s.bytes_recvd);
+       assert(waitpid(rxpid, &status, 0) == rxpid);
+       assert(waitpid(txpid, &status, 0) == txpid);
        return err;
 }