summaryrefslogtreecommitdiffstats
path: root/clatd_microbenchmark.c
blob: fed310066a5df7a6be8b69ad211fbbdffecfec3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * Copyright 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * clatd_microbenchmark.c - micro-benchmark for clatd tun send path
 *
 * Run with:
 *
 * adb push {$ANDROID_PRODUCT_OUT,}/data/nativetest/clatd_microbenchmark/clatd_microbenchmark
 * adb shell /data/nativetest/clatd_microbenchmark/clatd_microbenchmark
 *
 */
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/udp.h>
#include <arpa/inet.h>
#include <linux/if.h>
#include <linux/if_tun.h>

#include "checksum.h"
#include "tun.h"

#define DEVICENAME "clat4"

#define PORT 51339
#define PAYLOADSIZE (1280 - sizeof(struct iphdr) - sizeof(struct udphdr))
#define NUMPACKETS 1000000
#define SEC_TO_NANOSEC (1000 * 1000 * 1000)

void init_sockaddr_in(struct sockaddr_in *sin, const char *addr) {
    sin->sin_family = AF_INET;
    sin->sin_port = 0;
    sin->sin_addr.s_addr = inet_addr(addr);
}

void die(const char *str) {
    perror(str);
    exit(1);
}

int setup_tun() {
    int fd = tun_open();
    if (fd == -1) die("tun_open");

    char dev[IFNAMSIZ] = DEVICENAME;
    int ret = tun_alloc(dev, fd);
    if (ret == -1) die("tun_alloc");
    struct ifreq ifr = {
        .ifr_name = DEVICENAME,
    };

    int s = socket(AF_INET, SOCK_DGRAM, 0);
    init_sockaddr_in((struct sockaddr_in *) &ifr.ifr_addr, "192.0.0.4");
    if (ioctl(s, SIOCSIFADDR, &ifr) < 0) die("SIOCSIFADDR");
    init_sockaddr_in((struct sockaddr_in *) &ifr.ifr_addr, "255.255.255.248");
    if (ioctl(s, SIOCSIFNETMASK, &ifr) < 0) die("SIOCSIFNETMASK");
    if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0) die("SIOCGIFFLAGS");
    ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
    if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0) die("SIOCSIFFLAGS");
    return fd;
}

int send_packet(int fd, uint8_t payload[], int len, uint32_t payload_checksum) {
    struct tun_pi tun = { 0, htons(ETH_P_IP) };
    struct udphdr udp = {
        .source = htons(1234),
        .dest = htons(PORT),
        .len = htons(len + sizeof(udp)),
        .check = 0,
    };
    struct iphdr ip = {
        .version = 4,
        .ihl = 5,
        .tot_len = htons(len + sizeof(ip) + sizeof(udp)),
        .frag_off = htons(IP_DF),
        .ttl = 55,
        .protocol = IPPROTO_UDP,
        .saddr = htonl(0xc0000006),  // 192.0.0.6
        .daddr = htonl(0xc0000004),  // 192.0.0.4
    };
    clat_packet out = {
        { &tun, sizeof(tun) },  // tun header
        { &ip, sizeof(ip) },    // IP header
        { NULL, 0 },            // Fragment header
        { &udp, sizeof(udp) },  // Transport header
        { NULL, 0 },            // ICMP error IP header
        { NULL, 0 },            // ICMP error fragment header
        { NULL, 0 },            // ICMP error transport header
        { payload, len },       // Payload
    };

    ip.check = ip_checksum(&ip, sizeof(ip));

    uint32_t sum;
    sum = ipv4_pseudo_header_checksum(&ip, ntohs(udp.len));
    sum = ip_checksum_add(sum, &udp, sizeof(udp));
    sum += payload_checksum;
    udp.check = ip_checksum_finish(sum);

    return send_tun(fd, out, sizeof(out) / sizeof(out[0]));
}

double timedelta(const struct timespec tv1, const struct timespec tv2) {
    struct timespec end = tv2;
    if (end.tv_nsec < tv1.tv_nsec) {
        end.tv_sec -= 1;
        end.tv_nsec += SEC_TO_NANOSEC;
    }
    double seconds = (end.tv_sec - tv1.tv_sec);
    seconds += (((double) (end.tv_nsec - tv1.tv_nsec)) / SEC_TO_NANOSEC);
    return seconds;
}

void benchmark(const char *name, int fd, int s, int num, int do_read,
               uint8_t payload[], int len, uint32_t payload_sum) {
    int i;
    char buf[4096];
    struct timespec tv1, tv2;
    int write_err = 0, read_err = 0;
    clock_gettime(CLOCK_MONOTONIC, &tv1);
    for (i = 0; i < num; i++) {
        if (send_packet(fd, payload, len, payload_sum) == -1) write_err++;
        if (do_read && recvfrom(s, buf, sizeof(buf), 0, NULL, NULL) == -1) {
            read_err++;
            if (errno == ETIMEDOUT) {
                printf("Timed out after %d packets!\n", i);
                break;
            }
        }
    }
    clock_gettime(CLOCK_MONOTONIC, &tv2);
    double seconds = timedelta(tv1, tv2);
    int pps = (int) (i / seconds);
    double mbps = (i * PAYLOADSIZE / 1000000 * 8 / seconds);
    printf("%s: %d packets in %.2fs (%d pps, %.2f Mbps), ", name, i, seconds, pps, mbps);
    printf("read err %d (%.2f%%), write err %d (%.2f%%)\n",
           read_err, (float) read_err / i * 100,
           write_err, (float) write_err / i * 100);
}

int open_socket() {
    int sock = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, IPPROTO_UDP);

    int on = 1;
    if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) die("SO_REUSEADDR");

    struct timeval tv = { 1, 0 };
    if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) die("SO_RCVTIMEO");

    struct sockaddr_in addr = {
        .sin_family = AF_INET,
        .sin_port = ntohs(PORT),
        .sin_addr = { INADDR_ANY }
    };
    if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)) == -1) die ("bind");

   return sock;
}

int main() {
    int fd = setup_tun();
    int sock = open_socket();

    int i;
    uint8_t payload[PAYLOADSIZE];
    for (i = 0; i < (int) sizeof(payload); i++) {
        payload[i] = (uint8_t) i;
    }
    uint32_t payload_sum = ip_checksum_add(0, payload, sizeof(payload));

    // Check things are working.
    char buf[4096];
    if (send_packet(fd, payload, sizeof(payload), payload_sum) == -1) die("send_packet");
    if (recvfrom(sock, buf, sizeof(buf), 0, NULL, NULL) == -1) die("recvfrom");

    benchmark("Blocking", fd, sock, NUMPACKETS, 1, payload, sizeof(payload), payload_sum);
    close(fd);

    fd = setup_tun();
    set_nonblocking(fd);
    benchmark("No read", fd, sock, NUMPACKETS, 0, payload, sizeof(payload), payload_sum);
    close(fd);

    fd = setup_tun();
    set_nonblocking(fd);
    benchmark("Nonblocking", fd, sock, NUMPACKETS, 1, payload, sizeof(payload), payload_sum);
    close(fd);

    return 0;
}