Remove ethvlink block from fblock namespace, too
[ana-net.git] / opt / pflana-pps.c
blobc2e012194f16556ca8b62862420132722aed0bd2
1 /* Compile with -lrt */
3 #include <stdio.h>
4 #include <stdint.h>
5 #include <unistd.h>
6 #include <signal.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <ctype.h>
10 #include <assert.h>
11 #include <sys/socket.h>
12 #include <sys/time.h>
13 #include <time.h>
15 #define AF_LANA 27
17 static sig_atomic_t sigint = 0;
19 static inline double timespec_to_double(const struct timespec *time)
21 return time->tv_nsec * 1E-9 + (double) time->tv_sec;
24 static void sig_handler(int sig)
26 if (sig == SIGINT)
27 sigint = 1;
30 int main(int argc, char **argv)
32 int sock, ret;
33 char buff[1600];
34 struct timespec before, after;
35 unsigned long long pkts = 0, byte = 0, max;
36 double x1, x2, elapsed;
38 if (geteuid() != 0) {
39 fprintf(stderr, "Not root?!\n");
40 exit(EXIT_FAILURE);
42 if (argc != 2) {
43 fprintf(stderr, "No pkt number given!\n");
44 exit(EXIT_FAILURE);
46 max = (unsigned long long) atol(argv[argc - 1]);
48 // signal(SIGINT, sig_handler);
50 sock = socket(AF_LANA, SOCK_RAW, 0);
51 if (sock < 0) {
52 perror("socket");
53 return 0;
56 printf("Hit key to start!\n");
57 getchar();
58 printf("Abort with ^C\n");
60 memset(&before, 0, sizeof(before));
61 memset(&after, 0, sizeof(after));
63 clock_gettime(CLOCK_REALTIME, &before);
64 while (!sigint || pkts > max) {
65 ret = recv(sock, buff, sizeof(buff), 0);
66 if (ret < 0) {
67 perror("recvmsg");
68 continue;
69 } else {
70 pkts++;
71 byte += ret;
72 printf("got\n");
75 clock_gettime(CLOCK_REALTIME, &after);
77 x1 = timespec_to_double(&after);
78 x2 = timespec_to_double(&before);
79 elapsed = x1 - x2;
81 printf("\n\n");
82 fflush(stdout);
83 printf("time: %lf, pkts/s: %.2lf, bytes/s: %.2lf\n",
84 elapsed, 1.0 * pkts / elapsed, 1.0 * byte / elapsed);
86 close(sock);
87 return 0;