blob: ea58a8a6f13714f4373f4597da86448dfab76b76 [file] [log] [blame]
Lorenzo Colittib20719e2014-12-08 10:51:32 +09001/*
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 */
junyulaic4e591a2018-11-26 22:36:10 +090024#include <arpa/inet.h>
Lorenzo Colittib20719e2014-12-08 10:51:32 +090025#include <errno.h>
26#include <fcntl.h>
junyulaic4e591a2018-11-26 22:36:10 +090027#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>
Lorenzo Colittib20719e2014-12-08 10:51:32 +090033#include <stdio.h>
34#include <stdlib.h>
junyulaic4e591a2018-11-26 22:36:10 +090035#include <string.h>
Lorenzo Colittib20719e2014-12-08 10:51:32 +090036#include <sys/ioctl.h>
37#include <sys/socket.h>
38#include <sys/stat.h>
39#include <sys/types.h>
40#include <sys/uio.h>
junyulaic4e591a2018-11-26 22:36:10 +090041#include <time.h>
42#include <unistd.h>
Lorenzo Colittib20719e2014-12-08 10:51:32 +090043
Lorenzo Colitti98de5952019-01-20 11:45:03 +090044#include "netutils/checksum.h"
Lorenzo Colittib20719e2014-12-08 10:51:32 +090045#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
54void init_sockaddr_in(struct sockaddr_in *sin, const char *addr) {
junyulaic4e591a2018-11-26 22:36:10 +090055 sin->sin_family = AF_INET;
56 sin->sin_port = 0;
57 sin->sin_addr.s_addr = inet_addr(addr);
Lorenzo Colittib20719e2014-12-08 10:51:32 +090058}
59
60void die(const char *str) {
junyulaic4e591a2018-11-26 22:36:10 +090061 perror(str);
62 exit(1);
Lorenzo Colittib20719e2014-12-08 10:51:32 +090063}
64
65int setup_tun() {
junyulaic4e591a2018-11-26 22:36:10 +090066 int fd = tun_open();
67 if (fd == -1) die("tun_open");
Lorenzo Colittib20719e2014-12-08 10:51:32 +090068
junyulaic4e591a2018-11-26 22:36:10 +090069 char dev[IFNAMSIZ] = DEVICENAME;
Lorenzo Colitti6a095df2019-04-10 23:22:30 +090070 int ret = tun_alloc(dev, fd, sizeof(dev));
junyulaic4e591a2018-11-26 22:36:10 +090071 if (ret == -1) die("tun_alloc");
72 struct ifreq ifr = {
73 .ifr_name = DEVICENAME,
74 };
Lorenzo Colittib20719e2014-12-08 10:51:32 +090075
junyulaic4e591a2018-11-26 22:36:10 +090076 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;
Lorenzo Colittib20719e2014-12-08 10:51:32 +090085}
86
87int send_packet(int fd, uint8_t payload[], int len, uint32_t payload_checksum) {
junyulaic4e591a2018-11-26 22:36:10 +090088 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 };
Lorenzo Colittib20719e2014-12-08 10:51:32 +0900115
junyulaic4e591a2018-11-26 22:36:10 +0900116 ip.check = ip_checksum(&ip, sizeof(ip));
Lorenzo Colittib20719e2014-12-08 10:51:32 +0900117
junyulaic4e591a2018-11-26 22:36:10 +0900118 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);
Lorenzo Colittib20719e2014-12-08 10:51:32 +0900123
junyulaic4e591a2018-11-26 22:36:10 +0900124 return send_tun(fd, out, sizeof(out) / sizeof(out[0]));
Lorenzo Colittib20719e2014-12-08 10:51:32 +0900125}
126
127double timedelta(const struct timespec tv1, const struct timespec tv2) {
junyulaic4e591a2018-11-26 22:36:10 +0900128 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;
Lorenzo Colittib20719e2014-12-08 10:51:32 +0900136}
137
junyulaic4e591a2018-11-26 22:36:10 +0900138void 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 }
Lorenzo Colittib20719e2014-12-08 10:51:32 +0900153 }
junyulaic4e591a2018-11-26 22:36:10 +0900154 }
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);
Lorenzo Colittib20719e2014-12-08 10:51:32 +0900162}
163
164int open_socket() {
junyulaic4e591a2018-11-26 22:36:10 +0900165 int sock = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, IPPROTO_UDP);
Lorenzo Colittib20719e2014-12-08 10:51:32 +0900166
junyulaic4e591a2018-11-26 22:36:10 +0900167 int on = 1;
168 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) die("SO_REUSEADDR");
Lorenzo Colittib20719e2014-12-08 10:51:32 +0900169
junyulaic4e591a2018-11-26 22:36:10 +0900170 struct timeval tv = { 1, 0 };
171 if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) die("SO_RCVTIMEO");
Lorenzo Colittib20719e2014-12-08 10:51:32 +0900172
junyulaic4e591a2018-11-26 22:36:10 +0900173 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");
Lorenzo Colittib20719e2014-12-08 10:51:32 +0900179
junyulaic4e591a2018-11-26 22:36:10 +0900180 return sock;
Lorenzo Colittib20719e2014-12-08 10:51:32 +0900181}
182
183int main() {
junyulaic4e591a2018-11-26 22:36:10 +0900184 int fd = setup_tun();
185 int sock = open_socket();
Lorenzo Colittib20719e2014-12-08 10:51:32 +0900186
junyulaic4e591a2018-11-26 22:36:10 +0900187 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));
Lorenzo Colittib20719e2014-12-08 10:51:32 +0900193
junyulaic4e591a2018-11-26 22:36:10 +0900194 // 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");
Lorenzo Colittib20719e2014-12-08 10:51:32 +0900198
junyulaic4e591a2018-11-26 22:36:10 +0900199 benchmark("Blocking", fd, sock, NUMPACKETS, 1, payload, sizeof(payload), payload_sum);
200 close(fd);
Lorenzo Colittib20719e2014-12-08 10:51:32 +0900201
junyulaic4e591a2018-11-26 22:36:10 +0900202 fd = setup_tun();
junyulaic4e591a2018-11-26 22:36:10 +0900203 benchmark("No read", fd, sock, NUMPACKETS, 0, payload, sizeof(payload), payload_sum);
204 close(fd);
Lorenzo Colittib20719e2014-12-08 10:51:32 +0900205
junyulaic4e591a2018-11-26 22:36:10 +0900206 fd = setup_tun();
junyulaic4e591a2018-11-26 22:36:10 +0900207 benchmark("Nonblocking", fd, sock, NUMPACKETS, 1, payload, sizeof(payload), payload_sum);
208 close(fd);
Lorenzo Colittib20719e2014-12-08 10:51:32 +0900209
junyulaic4e591a2018-11-26 22:36:10 +0900210 return 0;
Lorenzo Colittib20719e2014-12-08 10:51:32 +0900211}