3 #include <netinet/in.h>
6 #include <sys/socket.h>
13 #include "utils/RWLock.h"
15 // Defined only in ifc_utils.c, in the kernel, and in the NDK.
17 #define SIOCKILLADDR 0x8939
24 #define KILL_INTERVAL_MS 10
25 #define CONNECT_THREADS 1
27 #define PERROR_EXIT(msg) { do { perror((msg)); exit(1); } while (0); };
30 // Ensures that sockets don't stay in TIME_WAIT state.
31 void setSoLinger(int s) {
32 const struct linger l = {
36 if (setsockopt(s, SOL_SOCKET, SO_LINGER, &l, sizeof(l)) == -1) {
37 PERROR_EXIT("SO_LINGER");
39 const int nolinger = -1;
40 if (setsockopt(s, SOL_TCP, TCP_LINGER2, &nolinger, sizeof(nolinger)) == -1) {
41 PERROR_EXIT("TCP_LINGER2");
46 // Binds to a random port on a random loopback address. We don't just use 127.0.0.1 because we don't
47 // want this test to kill unrelated connections on loopback.
48 int bindRandomAddr() {
50 sin.sin_family = AF_INET;
52 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
54 while (sin.sin_addr.s_addr == htonl(INADDR_LOOPBACK)) {
56 ((uint8_t *) &sin.sin_addr.s_addr) + 1,
57 sizeof(sin.sin_addr.s_addr) - 1);
61 if ((listensock = socket(AF_INET, SOCK_STREAM, 0)) == -1) PERROR_EXIT("listensock");
62 if (bind(listensock, (sockaddr *) &sin, sizeof(sin)) == -1) PERROR_EXIT("bind");
63 if (listen(listensock, 10) == -1) PERROR_EXIT("listen");
69 // Thread that calls SIOCKILLADDR in a loop.
70 void killSockets(sockaddr_in listenaddr, int intervalMs, android::RWLock *lock) {
72 memset(&ifr, 0, sizeof(ifr));
73 listenaddr.sin_port = 0;
74 strncpy(ifr.ifr_name, "lo", strlen("lo"));
75 memcpy(&ifr.ifr_addr, &listenaddr, sizeof(listenaddr));
77 int ioctlsock = socket(AF_INET, SOCK_DGRAM, 0);
78 if (ioctlsock == -1) PERROR_EXIT("ioctlsock");
81 if (ioctl(ioctlsock, SIOCKILLADDR, &ifr) != 0) {
82 PERROR_EXIT("SIOCKILLADDR failed, did you run 32-bit userspace on a 64-bit kernel?");
85 std::this_thread::sleep_for(std::chrono::milliseconds(intervalMs));
90 // Thread that calls connect() in a loop.
91 void connectLoop(sockaddr_in listenaddr, int listensock,
92 android::RWLock *lock, std::atomic<unsigned int> *attempts) {
94 int s = socket(AF_INET, SOCK_STREAM, 0);
97 // Don't call SIOCKILLADDR while connect() is running, or we'll end up with lots of
98 // connections in state FIN_WAITx or TIME_WAIT, which will then slow down future
99 // due to SYN retransmits.
101 if (connect(s, (sockaddr *) &listenaddr, sizeof(listenaddr)) == -1) PERROR_EXIT("connect");
104 send(s, "foo", 3, 0);
105 int acceptedsock = accept(listensock, NULL, 0);
106 if (close(acceptedsock) == -1) PERROR_EXIT("close");
107 if (close(s) == -1) PERROR_EXIT("close");
109 attempts->fetch_add(1);
114 // Thread that prints progress every second.
115 void progressThread(std::atomic<unsigned int> *attempts) {
116 uint32_t elapsed = 0;
117 uint32_t total, previous = 0;
119 std::this_thread::sleep_for(std::chrono::seconds(1));
121 total = attempts->load();
122 printf("%ds: %u cps, total %u\n", elapsed, total-previous, total);
130 int listensock = bindRandomAddr();
131 struct sockaddr_in sin;
132 socklen_t len = sizeof(sin);
133 if (getsockname(listensock, (sockaddr *) &sin, &len) == -1) PERROR_EXIT("getsockname");
135 printf("Using address %s:%d\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
137 android::RWLock lock;
138 std::atomic<unsigned int> attempts;
141 std::thread t0(killSockets, sin, KILL_INTERVAL_MS, &lock);
142 std::thread *connectThreads[CONNECT_THREADS];
143 for (size_t i = 0; i < CONNECT_THREADS; i++) {
144 connectThreads[i] = new std::thread(connectLoop, sin, listensock, &lock, &attempts);
146 std::thread t1(progressThread, &attempts);