Merge branch 'userns-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ebiederm...
[linux-2.6-block.git] / samples / sockmap / sockmap_user.c
1 /* Copyright (c) 2017 Covalent IO, Inc. http://covalent.io
2  *
3  * This program is free software; you can redistribute it and/or
4  * modify it under the terms of version 2 of the GNU General Public
5  * License as published by the Free Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful, but
8  * WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * General Public License for more details.
11  */
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <sys/socket.h>
15 #include <sys/ioctl.h>
16 #include <sys/select.h>
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
19 #include <unistd.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <sys/ioctl.h>
23 #include <stdbool.h>
24 #include <signal.h>
25 #include <fcntl.h>
26 #include <sys/wait.h>
27 #include <time.h>
28
29 #include <sys/time.h>
30 #include <sys/resource.h>
31 #include <sys/types.h>
32 #include <sys/sendfile.h>
33
34 #include <linux/netlink.h>
35 #include <linux/socket.h>
36 #include <linux/sock_diag.h>
37 #include <linux/bpf.h>
38 #include <linux/if_link.h>
39 #include <assert.h>
40 #include <libgen.h>
41
42 #include <getopt.h>
43
44 #include "../bpf/bpf_load.h"
45 #include "../bpf/bpf_util.h"
46 #include "../bpf/libbpf.h"
47
48 int running;
49 void running_handler(int a);
50
51 /* randomly selected ports for testing on lo */
52 #define S1_PORT 10000
53 #define S2_PORT 10001
54
55 /* global sockets */
56 int s1, s2, c1, c2, p1, p2;
57
58 int txmsg_pass;
59 int txmsg_noisy;
60 int txmsg_redir;
61 int txmsg_redir_noisy;
62 int txmsg_drop;
63 int txmsg_apply;
64 int txmsg_cork;
65 int txmsg_start;
66 int txmsg_end;
67 int txmsg_ingress;
68 int txmsg_skb;
69
70 static const struct option long_options[] = {
71         {"help",        no_argument,            NULL, 'h' },
72         {"cgroup",      required_argument,      NULL, 'c' },
73         {"rate",        required_argument,      NULL, 'r' },
74         {"verbose",     no_argument,            NULL, 'v' },
75         {"iov_count",   required_argument,      NULL, 'i' },
76         {"length",      required_argument,      NULL, 'l' },
77         {"test",        required_argument,      NULL, 't' },
78         {"data_test",   no_argument,            NULL, 'd' },
79         {"txmsg",               no_argument,    &txmsg_pass,  1  },
80         {"txmsg_noisy",         no_argument,    &txmsg_noisy, 1  },
81         {"txmsg_redir",         no_argument,    &txmsg_redir, 1  },
82         {"txmsg_redir_noisy",   no_argument,    &txmsg_redir_noisy, 1},
83         {"txmsg_drop",          no_argument,    &txmsg_drop, 1 },
84         {"txmsg_apply", required_argument,      NULL, 'a'},
85         {"txmsg_cork",  required_argument,      NULL, 'k'},
86         {"txmsg_start", required_argument,      NULL, 's'},
87         {"txmsg_end",   required_argument,      NULL, 'e'},
88         {"txmsg_ingress", no_argument,          &txmsg_ingress, 1 },
89         {"txmsg_skb", no_argument,              &txmsg_skb, 1 },
90         {0, 0, NULL, 0 }
91 };
92
93 static void usage(char *argv[])
94 {
95         int i;
96
97         printf(" Usage: %s --cgroup <cgroup_path>\n", argv[0]);
98         printf(" options:\n");
99         for (i = 0; long_options[i].name != 0; i++) {
100                 printf(" --%-12s", long_options[i].name);
101                 if (long_options[i].flag != NULL)
102                         printf(" flag (internal value:%d)\n",
103                                 *long_options[i].flag);
104                 else
105                         printf(" -%c\n", long_options[i].val);
106         }
107         printf("\n");
108 }
109
110 static int sockmap_init_sockets(void)
111 {
112         int i, err, one = 1;
113         struct sockaddr_in addr;
114         int *fds[4] = {&s1, &s2, &c1, &c2};
115
116         s1 = s2 = p1 = p2 = c1 = c2 = 0;
117
118         /* Init sockets */
119         for (i = 0; i < 4; i++) {
120                 *fds[i] = socket(AF_INET, SOCK_STREAM, 0);
121                 if (*fds[i] < 0) {
122                         perror("socket s1 failed()");
123                         return errno;
124                 }
125         }
126
127         /* Allow reuse */
128         for (i = 0; i < 2; i++) {
129                 err = setsockopt(*fds[i], SOL_SOCKET, SO_REUSEADDR,
130                                  (char *)&one, sizeof(one));
131                 if (err) {
132                         perror("setsockopt failed()");
133                         return errno;
134                 }
135         }
136
137         /* Non-blocking sockets */
138         for (i = 0; i < 2; i++) {
139                 err = ioctl(*fds[i], FIONBIO, (char *)&one);
140                 if (err < 0) {
141                         perror("ioctl s1 failed()");
142                         return errno;
143                 }
144         }
145
146         /* Bind server sockets */
147         memset(&addr, 0, sizeof(struct sockaddr_in));
148         addr.sin_family = AF_INET;
149         addr.sin_addr.s_addr = inet_addr("127.0.0.1");
150
151         addr.sin_port = htons(S1_PORT);
152         err = bind(s1, (struct sockaddr *)&addr, sizeof(addr));
153         if (err < 0) {
154                 perror("bind s1 failed()\n");
155                 return errno;
156         }
157
158         addr.sin_port = htons(S2_PORT);
159         err = bind(s2, (struct sockaddr *)&addr, sizeof(addr));
160         if (err < 0) {
161                 perror("bind s2 failed()\n");
162                 return errno;
163         }
164
165         /* Listen server sockets */
166         addr.sin_port = htons(S1_PORT);
167         err = listen(s1, 32);
168         if (err < 0) {
169                 perror("listen s1 failed()\n");
170                 return errno;
171         }
172
173         addr.sin_port = htons(S2_PORT);
174         err = listen(s2, 32);
175         if (err < 0) {
176                 perror("listen s1 failed()\n");
177                 return errno;
178         }
179
180         /* Initiate Connect */
181         addr.sin_port = htons(S1_PORT);
182         err = connect(c1, (struct sockaddr *)&addr, sizeof(addr));
183         if (err < 0 && errno != EINPROGRESS) {
184                 perror("connect c1 failed()\n");
185                 return errno;
186         }
187
188         addr.sin_port = htons(S2_PORT);
189         err = connect(c2, (struct sockaddr *)&addr, sizeof(addr));
190         if (err < 0 && errno != EINPROGRESS) {
191                 perror("connect c2 failed()\n");
192                 return errno;
193         } else if (err < 0) {
194                 err = 0;
195         }
196
197         /* Accept Connecrtions */
198         p1 = accept(s1, NULL, NULL);
199         if (p1 < 0) {
200                 perror("accept s1 failed()\n");
201                 return errno;
202         }
203
204         p2 = accept(s2, NULL, NULL);
205         if (p2 < 0) {
206                 perror("accept s1 failed()\n");
207                 return errno;
208         }
209
210         printf("connected sockets: c1 <-> p1, c2 <-> p2\n");
211         printf("cgroups binding: c1(%i) <-> s1(%i) - - - c2(%i) <-> s2(%i)\n",
212                 c1, s1, c2, s2);
213         return 0;
214 }
215
216 struct msg_stats {
217         size_t bytes_sent;
218         size_t bytes_recvd;
219         struct timespec start;
220         struct timespec end;
221 };
222
223 struct sockmap_options {
224         int verbose;
225         bool base;
226         bool sendpage;
227         bool data_test;
228         bool drop_expected;
229 };
230
231 static int msg_loop_sendpage(int fd, int iov_length, int cnt,
232                              struct msg_stats *s,
233                              struct sockmap_options *opt)
234 {
235         bool drop = opt->drop_expected;
236         unsigned char k = 0;
237         FILE *file;
238         int i, fp;
239
240         file = fopen(".sendpage_tst.tmp", "w+");
241         for (i = 0; i < iov_length * cnt; i++, k++)
242                 fwrite(&k, sizeof(char), 1, file);
243         fflush(file);
244         fseek(file, 0, SEEK_SET);
245         fclose(file);
246
247         fp = open(".sendpage_tst.tmp", O_RDONLY);
248         clock_gettime(CLOCK_MONOTONIC, &s->start);
249         for (i = 0; i < cnt; i++) {
250                 int sent = sendfile(fd, fp, NULL, iov_length);
251
252                 if (!drop && sent < 0) {
253                         perror("send loop error:");
254                         close(fp);
255                         return sent;
256                 } else if (drop && sent >= 0) {
257                         printf("sendpage loop error expected: %i\n", sent);
258                         close(fp);
259                         return -EIO;
260                 }
261
262                 if (sent > 0)
263                         s->bytes_sent += sent;
264         }
265         clock_gettime(CLOCK_MONOTONIC, &s->end);
266         close(fp);
267         return 0;
268 }
269
270 static int msg_loop(int fd, int iov_count, int iov_length, int cnt,
271                     struct msg_stats *s, bool tx,
272                     struct sockmap_options *opt)
273 {
274         struct msghdr msg = {0};
275         int err, i, flags = MSG_NOSIGNAL;
276         struct iovec *iov;
277         unsigned char k;
278         bool data_test = opt->data_test;
279         bool drop = opt->drop_expected;
280
281         iov = calloc(iov_count, sizeof(struct iovec));
282         if (!iov)
283                 return errno;
284
285         k = 0;
286         for (i = 0; i < iov_count; i++) {
287                 unsigned char *d = calloc(iov_length, sizeof(char));
288
289                 if (!d) {
290                         fprintf(stderr, "iov_count %i/%i OOM\n", i, iov_count);
291                         goto out_errno;
292                 }
293                 iov[i].iov_base = d;
294                 iov[i].iov_len = iov_length;
295
296                 if (data_test && tx) {
297                         int j;
298
299                         for (j = 0; j < iov_length; j++)
300                                 d[j] = k++;
301                 }
302         }
303
304         msg.msg_iov = iov;
305         msg.msg_iovlen = iov_count;
306         k = 0;
307
308         if (tx) {
309                 clock_gettime(CLOCK_MONOTONIC, &s->start);
310                 for (i = 0; i < cnt; i++) {
311                         int sent = sendmsg(fd, &msg, flags);
312
313                         if (!drop && sent < 0) {
314                                 perror("send loop error:");
315                                 goto out_errno;
316                         } else if (drop && sent >= 0) {
317                                 printf("send loop error expected: %i\n", sent);
318                                 errno = -EIO;
319                                 goto out_errno;
320                         }
321                         if (sent > 0)
322                                 s->bytes_sent += sent;
323                 }
324                 clock_gettime(CLOCK_MONOTONIC, &s->end);
325         } else {
326                 int slct, recv, max_fd = fd;
327                 struct timeval timeout;
328                 float total_bytes;
329                 fd_set w;
330
331                 total_bytes = (float)iov_count * (float)iov_length * (float)cnt;
332                 err = clock_gettime(CLOCK_MONOTONIC, &s->start);
333                 if (err < 0)
334                         perror("recv start time: ");
335                 while (s->bytes_recvd < total_bytes) {
336                         timeout.tv_sec = 1;
337                         timeout.tv_usec = 0;
338
339                         /* FD sets */
340                         FD_ZERO(&w);
341                         FD_SET(fd, &w);
342
343                         slct = select(max_fd + 1, &w, NULL, NULL, &timeout);
344                         if (slct == -1) {
345                                 perror("select()");
346                                 clock_gettime(CLOCK_MONOTONIC, &s->end);
347                                 goto out_errno;
348                         } else if (!slct) {
349                                 fprintf(stderr, "unexpected timeout\n");
350                                 errno = -EIO;
351                                 clock_gettime(CLOCK_MONOTONIC, &s->end);
352                                 goto out_errno;
353                         }
354
355                         recv = recvmsg(fd, &msg, flags);
356                         if (recv < 0) {
357                                 if (errno != EWOULDBLOCK) {
358                                         clock_gettime(CLOCK_MONOTONIC, &s->end);
359                                         perror("recv failed()\n");
360                                         goto out_errno;
361                                 }
362                         }
363
364                         s->bytes_recvd += recv;
365
366                         if (data_test) {
367                                 int j;
368
369                                 for (i = 0; i < msg.msg_iovlen; i++) {
370                                         unsigned char *d = iov[i].iov_base;
371
372                                         for (j = 0;
373                                              j < iov[i].iov_len && recv; j++) {
374                                                 if (d[j] != k++) {
375                                                         errno = -EIO;
376                                                         fprintf(stderr,
377                                                                 "detected data corruption @iov[%i]:%i %02x != %02x, %02x ?= %02x\n",
378                                                                 i, j, d[j], k - 1, d[j+1], k + 1);
379                                                         goto out_errno;
380                                                 }
381                                                 recv--;
382                                         }
383                                 }
384                         }
385                 }
386                 clock_gettime(CLOCK_MONOTONIC, &s->end);
387         }
388
389         for (i = 0; i < iov_count; i++)
390                 free(iov[i].iov_base);
391         free(iov);
392         return 0;
393 out_errno:
394         for (i = 0; i < iov_count; i++)
395                 free(iov[i].iov_base);
396         free(iov);
397         return errno;
398 }
399
400 static float giga = 1000000000;
401
402 static inline float sentBps(struct msg_stats s)
403 {
404         return s.bytes_sent / (s.end.tv_sec - s.start.tv_sec);
405 }
406
407 static inline float recvdBps(struct msg_stats s)
408 {
409         return s.bytes_recvd / (s.end.tv_sec - s.start.tv_sec);
410 }
411
412 static int sendmsg_test(int iov_count, int iov_buf, int cnt,
413                         struct sockmap_options *opt)
414 {
415         float sent_Bps = 0, recvd_Bps = 0;
416         int rx_fd, txpid, rxpid, err = 0;
417         struct msg_stats s = {0};
418         int status;
419
420         errno = 0;
421
422         if (opt->base)
423                 rx_fd = p1;
424         else
425                 rx_fd = p2;
426
427         rxpid = fork();
428         if (rxpid == 0) {
429                 if (opt->drop_expected)
430                         exit(1);
431
432                 if (opt->sendpage)
433                         iov_count = 1;
434                 err = msg_loop(rx_fd, iov_count, iov_buf,
435                                cnt, &s, false, opt);
436                 if (err)
437                         fprintf(stderr,
438                                 "msg_loop_rx: iov_count %i iov_buf %i cnt %i err %i\n",
439                                 iov_count, iov_buf, cnt, err);
440                 shutdown(p2, SHUT_RDWR);
441                 shutdown(p1, SHUT_RDWR);
442                 if (s.end.tv_sec - s.start.tv_sec) {
443                         sent_Bps = sentBps(s);
444                         recvd_Bps = recvdBps(s);
445                 }
446                 fprintf(stdout,
447                         "rx_sendmsg: TX: %zuB %fB/s %fGB/s RX: %zuB %fB/s %fGB/s\n",
448                         s.bytes_sent, sent_Bps, sent_Bps/giga,
449                         s.bytes_recvd, recvd_Bps, recvd_Bps/giga);
450                 exit(1);
451         } else if (rxpid == -1) {
452                 perror("msg_loop_rx: ");
453                 return errno;
454         }
455
456         txpid = fork();
457         if (txpid == 0) {
458                 if (opt->sendpage)
459                         err = msg_loop_sendpage(c1, iov_buf, cnt, &s, opt);
460                 else
461                         err = msg_loop(c1, iov_count, iov_buf,
462                                        cnt, &s, true, opt);
463
464                 if (err)
465                         fprintf(stderr,
466                                 "msg_loop_tx: iov_count %i iov_buf %i cnt %i err %i\n",
467                                 iov_count, iov_buf, cnt, err);
468                 shutdown(c1, SHUT_RDWR);
469                 if (s.end.tv_sec - s.start.tv_sec) {
470                         sent_Bps = sentBps(s);
471                         recvd_Bps = recvdBps(s);
472                 }
473                 fprintf(stdout,
474                         "tx_sendmsg: TX: %zuB %fB/s %f GB/s RX: %zuB %fB/s %fGB/s\n",
475                         s.bytes_sent, sent_Bps, sent_Bps/giga,
476                         s.bytes_recvd, recvd_Bps, recvd_Bps/giga);
477                 exit(1);
478         } else if (txpid == -1) {
479                 perror("msg_loop_tx: ");
480                 return errno;
481         }
482
483         assert(waitpid(rxpid, &status, 0) == rxpid);
484         assert(waitpid(txpid, &status, 0) == txpid);
485         return err;
486 }
487
488 static int forever_ping_pong(int rate, struct sockmap_options *opt)
489 {
490         struct timeval timeout;
491         char buf[1024] = {0};
492         int sc;
493
494         timeout.tv_sec = 10;
495         timeout.tv_usec = 0;
496
497         /* Ping/Pong data from client to server */
498         sc = send(c1, buf, sizeof(buf), 0);
499         if (sc < 0) {
500                 perror("send failed()\n");
501                 return sc;
502         }
503
504         do {
505                 int s, rc, i, max_fd = p2;
506                 fd_set w;
507
508                 /* FD sets */
509                 FD_ZERO(&w);
510                 FD_SET(c1, &w);
511                 FD_SET(c2, &w);
512                 FD_SET(p1, &w);
513                 FD_SET(p2, &w);
514
515                 s = select(max_fd + 1, &w, NULL, NULL, &timeout);
516                 if (s == -1) {
517                         perror("select()");
518                         break;
519                 } else if (!s) {
520                         fprintf(stderr, "unexpected timeout\n");
521                         break;
522                 }
523
524                 for (i = 0; i <= max_fd && s > 0; ++i) {
525                         if (!FD_ISSET(i, &w))
526                                 continue;
527
528                         s--;
529
530                         rc = recv(i, buf, sizeof(buf), 0);
531                         if (rc < 0) {
532                                 if (errno != EWOULDBLOCK) {
533                                         perror("recv failed()\n");
534                                         return rc;
535                                 }
536                         }
537
538                         if (rc == 0) {
539                                 close(i);
540                                 break;
541                         }
542
543                         sc = send(i, buf, rc, 0);
544                         if (sc < 0) {
545                                 perror("send failed()\n");
546                                 return sc;
547                         }
548                 }
549
550                 if (rate)
551                         sleep(rate);
552
553                 if (opt->verbose) {
554                         printf(".");
555                         fflush(stdout);
556
557                 }
558         } while (running);
559
560         return 0;
561 }
562
563 enum {
564         PING_PONG,
565         SENDMSG,
566         BASE,
567         BASE_SENDPAGE,
568         SENDPAGE,
569 };
570
571 int main(int argc, char **argv)
572 {
573         int iov_count = 1, length = 1024, rate = 1, tx_prog_fd;
574         struct rlimit r = {10 * 1024 * 1024, RLIM_INFINITY};
575         int opt, longindex, err, cg_fd = 0;
576         struct sockmap_options options = {0};
577         int test = PING_PONG;
578         char filename[256];
579
580         while ((opt = getopt_long(argc, argv, ":dhvc:r:i:l:t:",
581                                   long_options, &longindex)) != -1) {
582                 switch (opt) {
583                 case 's':
584                         txmsg_start = atoi(optarg);
585                         break;
586                 case 'e':
587                         txmsg_end = atoi(optarg);
588                         break;
589                 case 'a':
590                         txmsg_apply = atoi(optarg);
591                         break;
592                 case 'k':
593                         txmsg_cork = atoi(optarg);
594                         break;
595                 case 'c':
596                         cg_fd = open(optarg, O_DIRECTORY, O_RDONLY);
597                         if (cg_fd < 0) {
598                                 fprintf(stderr,
599                                         "ERROR: (%i) open cg path failed: %s\n",
600                                         cg_fd, optarg);
601                                 return cg_fd;
602                         }
603                         break;
604                 case 'r':
605                         rate = atoi(optarg);
606                         break;
607                 case 'v':
608                         options.verbose = 1;
609                         break;
610                 case 'i':
611                         iov_count = atoi(optarg);
612                         break;
613                 case 'l':
614                         length = atoi(optarg);
615                         break;
616                 case 'd':
617                         options.data_test = true;
618                         break;
619                 case 't':
620                         if (strcmp(optarg, "ping") == 0) {
621                                 test = PING_PONG;
622                         } else if (strcmp(optarg, "sendmsg") == 0) {
623                                 test = SENDMSG;
624                         } else if (strcmp(optarg, "base") == 0) {
625                                 test = BASE;
626                         } else if (strcmp(optarg, "base_sendpage") == 0) {
627                                 test = BASE_SENDPAGE;
628                         } else if (strcmp(optarg, "sendpage") == 0) {
629                                 test = SENDPAGE;
630                         } else {
631                                 usage(argv);
632                                 return -1;
633                         }
634                         break;
635                 case 0:
636                         break;
637                 case 'h':
638                 default:
639                         usage(argv);
640                         return -1;
641                 }
642         }
643
644         if (!cg_fd) {
645                 fprintf(stderr, "%s requires cgroup option: --cgroup <path>\n",
646                         argv[0]);
647                 return -1;
648         }
649
650         if (setrlimit(RLIMIT_MEMLOCK, &r)) {
651                 perror("setrlimit(RLIMIT_MEMLOCK)");
652                 return 1;
653         }
654
655         snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
656
657         running = 1;
658
659         /* catch SIGINT */
660         signal(SIGINT, running_handler);
661
662         if (load_bpf_file(filename)) {
663                 fprintf(stderr, "load_bpf_file: (%s) %s\n",
664                         filename, strerror(errno));
665                 return 1;
666         }
667
668         /* If base test skip BPF setup */
669         if (test == BASE || test == BASE_SENDPAGE)
670                 goto run;
671
672         /* Attach programs to sockmap */
673         err = bpf_prog_attach(prog_fd[0], map_fd[0],
674                                 BPF_SK_SKB_STREAM_PARSER, 0);
675         if (err) {
676                 fprintf(stderr, "ERROR: bpf_prog_attach (sockmap): %d (%s)\n",
677                         err, strerror(errno));
678                 return err;
679         }
680
681         err = bpf_prog_attach(prog_fd[1], map_fd[0],
682                                 BPF_SK_SKB_STREAM_VERDICT, 0);
683         if (err) {
684                 fprintf(stderr, "ERROR: bpf_prog_attach (sockmap): %d (%s)\n",
685                         err, strerror(errno));
686                 return err;
687         }
688
689         /* Attach to cgroups */
690         err = bpf_prog_attach(prog_fd[2], cg_fd, BPF_CGROUP_SOCK_OPS, 0);
691         if (err) {
692                 fprintf(stderr, "ERROR: bpf_prog_attach (groups): %d (%s)\n",
693                         err, strerror(errno));
694                 return err;
695         }
696
697 run:
698         err = sockmap_init_sockets();
699         if (err) {
700                 fprintf(stderr, "ERROR: test socket failed: %d\n", err);
701                 goto out;
702         }
703
704         /* Attach txmsg program to sockmap */
705         if (txmsg_pass)
706                 tx_prog_fd = prog_fd[3];
707         else if (txmsg_noisy)
708                 tx_prog_fd = prog_fd[4];
709         else if (txmsg_redir)
710                 tx_prog_fd = prog_fd[5];
711         else if (txmsg_redir_noisy)
712                 tx_prog_fd = prog_fd[6];
713         else if (txmsg_drop)
714                 tx_prog_fd = prog_fd[9];
715         /* apply and cork must be last */
716         else if (txmsg_apply)
717                 tx_prog_fd = prog_fd[7];
718         else if (txmsg_cork)
719                 tx_prog_fd = prog_fd[8];
720         else
721                 tx_prog_fd = 0;
722
723         if (tx_prog_fd) {
724                 int redir_fd, i = 0;
725
726                 err = bpf_prog_attach(tx_prog_fd,
727                                       map_fd[1], BPF_SK_MSG_VERDICT, 0);
728                 if (err) {
729                         fprintf(stderr,
730                                 "ERROR: bpf_prog_attach (txmsg): %d (%s)\n",
731                                 err, strerror(errno));
732                         return err;
733                 }
734
735                 err = bpf_map_update_elem(map_fd[1], &i, &c1, BPF_ANY);
736                 if (err) {
737                         fprintf(stderr,
738                                 "ERROR: bpf_map_update_elem (txmsg):  %d (%s\n",
739                                 err, strerror(errno));
740                         return err;
741                 }
742
743                 if (txmsg_redir || txmsg_redir_noisy)
744                         redir_fd = c2;
745                 else
746                         redir_fd = c1;
747
748                 err = bpf_map_update_elem(map_fd[2], &i, &redir_fd, BPF_ANY);
749                 if (err) {
750                         fprintf(stderr,
751                                 "ERROR: bpf_map_update_elem (txmsg):  %d (%s\n",
752                                 err, strerror(errno));
753                         return err;
754                 }
755
756                 if (txmsg_apply) {
757                         err = bpf_map_update_elem(map_fd[3],
758                                                   &i, &txmsg_apply, BPF_ANY);
759                         if (err) {
760                                 fprintf(stderr,
761                                         "ERROR: bpf_map_update_elem (apply_bytes):  %d (%s\n",
762                                         err, strerror(errno));
763                                 return err;
764                         }
765                 }
766
767                 if (txmsg_cork) {
768                         err = bpf_map_update_elem(map_fd[4],
769                                                   &i, &txmsg_cork, BPF_ANY);
770                         if (err) {
771                                 fprintf(stderr,
772                                         "ERROR: bpf_map_update_elem (cork_bytes):  %d (%s\n",
773                                         err, strerror(errno));
774                                 return err;
775                         }
776                 }
777
778                 if (txmsg_start) {
779                         err = bpf_map_update_elem(map_fd[5],
780                                                   &i, &txmsg_start, BPF_ANY);
781                         if (err) {
782                                 fprintf(stderr,
783                                         "ERROR: bpf_map_update_elem (txmsg_start):  %d (%s)\n",
784                                         err, strerror(errno));
785                                 return err;
786                         }
787                 }
788
789                 if (txmsg_end) {
790                         i = 1;
791                         err = bpf_map_update_elem(map_fd[5],
792                                                   &i, &txmsg_end, BPF_ANY);
793                         if (err) {
794                                 fprintf(stderr,
795                                         "ERROR: bpf_map_update_elem (txmsg_end):  %d (%s)\n",
796                                         err, strerror(errno));
797                                 return err;
798                         }
799                 }
800
801                 if (txmsg_ingress) {
802                         int in = BPF_F_INGRESS;
803
804                         i = 0;
805                         err = bpf_map_update_elem(map_fd[6], &i, &in, BPF_ANY);
806                         if (err) {
807                                 fprintf(stderr,
808                                         "ERROR: bpf_map_update_elem (txmsg_ingress): %d (%s)\n",
809                                         err, strerror(errno));
810                         }
811                         i = 1;
812                         err = bpf_map_update_elem(map_fd[1], &i, &p1, BPF_ANY);
813                         if (err) {
814                                 fprintf(stderr,
815                                         "ERROR: bpf_map_update_elem (p1 txmsg): %d (%s)\n",
816                                         err, strerror(errno));
817                         }
818                         err = bpf_map_update_elem(map_fd[2], &i, &p1, BPF_ANY);
819                         if (err) {
820                                 fprintf(stderr,
821                                         "ERROR: bpf_map_update_elem (p1 redir): %d (%s)\n",
822                                         err, strerror(errno));
823                         }
824
825                         i = 2;
826                         err = bpf_map_update_elem(map_fd[2], &i, &p2, BPF_ANY);
827                         if (err) {
828                                 fprintf(stderr,
829                                         "ERROR: bpf_map_update_elem (p2 txmsg): %d (%s)\n",
830                                         err, strerror(errno));
831                         }
832                 }
833
834                 if (txmsg_skb) {
835                         int skb_fd = (test == SENDMSG || test == SENDPAGE) ? p2 : p1;
836                         int ingress = BPF_F_INGRESS;
837
838                         i = 0;
839                         err = bpf_map_update_elem(map_fd[7], &i, &ingress, BPF_ANY);
840                         if (err) {
841                                 fprintf(stderr,
842                                         "ERROR: bpf_map_update_elem (txmsg_ingress): %d (%s)\n",
843                                         err, strerror(errno));
844                         }
845
846                         i = 3;
847                         err = bpf_map_update_elem(map_fd[0], &i, &skb_fd, BPF_ANY);
848                         if (err) {
849                                 fprintf(stderr,
850                                         "ERROR: bpf_map_update_elem (c1 sockmap): %d (%s)\n",
851                                         err, strerror(errno));
852                         }
853                 }
854         }
855
856         if (txmsg_drop)
857                 options.drop_expected = true;
858
859         if (test == PING_PONG)
860                 err = forever_ping_pong(rate, &options);
861         else if (test == SENDMSG) {
862                 options.base = false;
863                 options.sendpage = false;
864                 err = sendmsg_test(iov_count, length, rate, &options);
865         } else if (test == SENDPAGE) {
866                 options.base = false;
867                 options.sendpage = true;
868                 err = sendmsg_test(iov_count, length, rate, &options);
869         } else if (test == BASE) {
870                 options.base = true;
871                 options.sendpage = false;
872                 err = sendmsg_test(iov_count, length, rate, &options);
873         } else if (test == BASE_SENDPAGE) {
874                 options.base = true;
875                 options.sendpage = true;
876                 err = sendmsg_test(iov_count, length, rate, &options);
877         } else
878                 fprintf(stderr, "unknown test\n");
879 out:
880         bpf_prog_detach2(prog_fd[2], cg_fd, BPF_CGROUP_SOCK_OPS);
881         close(s1);
882         close(s2);
883         close(p1);
884         close(p2);
885         close(c1);
886         close(c2);
887         close(cg_fd);
888         return err;
889 }
890
891 void running_handler(int a)
892 {
893         running = 0;
894 }