• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * clatd_microbenchmark.c - micro-benchmark for clatd tun send path
17  *
18  * Run with:
19  *
20  * adb push {$ANDROID_PRODUCT_OUT,}/data/nativetest/clatd_microbenchmark/clatd_microbenchmark
21  * adb shell /data/nativetest/clatd_microbenchmark/clatd_microbenchmark
22  *
23  */
24 #include <arpa/inet.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <linux/if.h>
28 #include <linux/if_tun.h>
29 #include <netinet/in.h>
30 #include <netinet/ip.h>
31 #include <netinet/ip6.h>
32 #include <netinet/udp.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
38 #include <sys/stat.h>
39 #include <sys/types.h>
40 #include <sys/uio.h>
41 #include <time.h>
42 #include <unistd.h>
43 
44 #include "netutils/checksum.h"
45 #include "tun.h"
46 
47 #define DEVICENAME "clat4"
48 
49 #define PORT 51339
50 #define PAYLOADSIZE (1280 - sizeof(struct iphdr) - sizeof(struct udphdr))
51 #define NUMPACKETS 1000000
52 #define SEC_TO_NANOSEC (1000 * 1000 * 1000)
53 
init_sockaddr_in(struct sockaddr_in * sin,const char * addr)54 void init_sockaddr_in(struct sockaddr_in *sin, const char *addr) {
55   sin->sin_family      = AF_INET;
56   sin->sin_port        = 0;
57   sin->sin_addr.s_addr = inet_addr(addr);
58 }
59 
die(const char * str)60 void die(const char *str) {
61   perror(str);
62   exit(1);
63 }
64 
setup_tun()65 int setup_tun() {
66   int fd = tun_open();
67   if (fd == -1) die("tun_open");
68 
69   char dev[IFNAMSIZ] = DEVICENAME;
70   int ret            = tun_alloc(dev, fd, sizeof(dev));
71   if (ret == -1) die("tun_alloc");
72   struct ifreq ifr = {
73     .ifr_name = DEVICENAME,
74   };
75 
76   int s = socket(AF_INET, SOCK_DGRAM, 0);
77   init_sockaddr_in((struct sockaddr_in *)&ifr.ifr_addr, "192.0.0.4");
78   if (ioctl(s, SIOCSIFADDR, &ifr) < 0) die("SIOCSIFADDR");
79   init_sockaddr_in((struct sockaddr_in *)&ifr.ifr_addr, "255.255.255.248");
80   if (ioctl(s, SIOCSIFNETMASK, &ifr) < 0) die("SIOCSIFNETMASK");
81   if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0) die("SIOCGIFFLAGS");
82   ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
83   if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0) die("SIOCSIFFLAGS");
84   return fd;
85 }
86 
send_packet(int fd,uint8_t payload[],int len,uint32_t payload_checksum)87 int send_packet(int fd, uint8_t payload[], int len, uint32_t payload_checksum) {
88   struct tun_pi tun = { 0, htons(ETH_P_IP) };
89   struct udphdr udp = {
90     .source = htons(1234),
91     .dest   = htons(PORT),
92     .len    = htons(len + sizeof(udp)),
93     .check  = 0,
94   };
95   struct iphdr ip = {
96     .version  = 4,
97     .ihl      = 5,
98     .tot_len  = htons(len + sizeof(ip) + sizeof(udp)),
99     .frag_off = htons(IP_DF),
100     .ttl      = 55,
101     .protocol = IPPROTO_UDP,
102     .saddr    = htonl(0xc0000006),  // 192.0.0.6
103     .daddr    = htonl(0xc0000004),  // 192.0.0.4
104   };
105   clat_packet out = {
106     { &tun, sizeof(tun) },  // tun header
107     { &ip, sizeof(ip) },    // IP header
108     { NULL, 0 },            // Fragment header
109     { &udp, sizeof(udp) },  // Transport header
110     { NULL, 0 },            // ICMP error IP header
111     { NULL, 0 },            // ICMP error fragment header
112     { NULL, 0 },            // ICMP error transport header
113     { payload, len },       // Payload
114   };
115 
116   ip.check = ip_checksum(&ip, sizeof(ip));
117 
118   uint32_t sum;
119   sum = ipv4_pseudo_header_checksum(&ip, ntohs(udp.len));
120   sum = ip_checksum_add(sum, &udp, sizeof(udp));
121   sum += payload_checksum;
122   udp.check = ip_checksum_finish(sum);
123 
124   return send_tun(fd, out, sizeof(out) / sizeof(out[0]));
125 }
126 
timedelta(const struct timespec tv1,const struct timespec tv2)127 double timedelta(const struct timespec tv1, const struct timespec tv2) {
128   struct timespec end = tv2;
129   if (end.tv_nsec < tv1.tv_nsec) {
130     end.tv_sec -= 1;
131     end.tv_nsec += SEC_TO_NANOSEC;
132   }
133   double seconds = (end.tv_sec - tv1.tv_sec);
134   seconds += (((double)(end.tv_nsec - tv1.tv_nsec)) / SEC_TO_NANOSEC);
135   return seconds;
136 }
137 
benchmark(const char * name,int fd,int s,int num,int do_read,uint8_t payload[],int len,uint32_t payload_sum)138 void benchmark(const char *name, int fd, int s, int num, int do_read, uint8_t payload[], int len,
139                uint32_t payload_sum) {
140   int i;
141   char buf[4096];
142   struct timespec tv1, tv2;
143   int write_err = 0, read_err = 0;
144   clock_gettime(CLOCK_MONOTONIC, &tv1);
145   for (i = 0; i < num; i++) {
146     if (send_packet(fd, payload, len, payload_sum) == -1) write_err++;
147     if (do_read && recvfrom(s, buf, sizeof(buf), 0, NULL, NULL) == -1) {
148       read_err++;
149       if (errno == ETIMEDOUT) {
150         printf("Timed out after %d packets!\n", i);
151         break;
152       }
153     }
154   }
155   clock_gettime(CLOCK_MONOTONIC, &tv2);
156   double seconds = timedelta(tv1, tv2);
157   int pps        = (int)(i / seconds);
158   double mbps    = (i * PAYLOADSIZE / 1000000 * 8 / seconds);
159   printf("%s: %d packets in %.2fs (%d pps, %.2f Mbps), ", name, i, seconds, pps, mbps);
160   printf("read err %d (%.2f%%), write err %d (%.2f%%)\n", read_err, (float)read_err / i * 100,
161          write_err, (float)write_err / i * 100);
162 }
163 
open_socket()164 int open_socket() {
165   int sock = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, IPPROTO_UDP);
166 
167   int on = 1;
168   if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) die("SO_REUSEADDR");
169 
170   struct timeval tv = { 1, 0 };
171   if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) die("SO_RCVTIMEO");
172 
173   struct sockaddr_in addr = {
174     .sin_family = AF_INET,
175     .sin_port   = ntohs(PORT),
176     .sin_addr   = { INADDR_ANY },
177   };
178   if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) die("bind");
179 
180   return sock;
181 }
182 
main()183 int main() {
184   int fd   = setup_tun();
185   int sock = open_socket();
186 
187   int i;
188   uint8_t payload[PAYLOADSIZE];
189   for (i = 0; i < (int)sizeof(payload); i++) {
190     payload[i] = (uint8_t)i;
191   }
192   uint32_t payload_sum = ip_checksum_add(0, payload, sizeof(payload));
193 
194   // Check things are working.
195   char buf[4096];
196   if (send_packet(fd, payload, sizeof(payload), payload_sum) == -1) die("send_packet");
197   if (recvfrom(sock, buf, sizeof(buf), 0, NULL, NULL) == -1) die("recvfrom");
198 
199   benchmark("Blocking", fd, sock, NUMPACKETS, 1, payload, sizeof(payload), payload_sum);
200   close(fd);
201 
202   fd = setup_tun();
203   benchmark("No read", fd, sock, NUMPACKETS, 0, payload, sizeof(payload), payload_sum);
204   close(fd);
205 
206   fd = setup_tun();
207   benchmark("Nonblocking", fd, sock, NUMPACKETS, 1, payload, sizeof(payload), payload_sum);
208   close(fd);
209 
210   return 0;
211 }
212