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
173from socket import SOCK_STREAM, AF_INET, SOL_SOCKET, SO_REUSEADDR, SOL_TCP, TCP_MAXSEG, SO_RCVBUF, SO_SNDBUF, IPPROTO_IP, TCP_NODELAY, TCP_QUICKACK
import ctypes
import os
import socket
import struct
import sys
import time
import threading
import select
rcv_buf = int(sys.argv[1])
fill_mtd = sys.argv[2]
drain_mtd = sys.argv[3]
packet_size = int(sys.argv[4])
IP_BIND_ADDRESS_NO_PORT = 24
IP_MTU = 14
sd = socket.socket(AF_INET, SOCK_STREAM, 0)
sd.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
sd.setsockopt(SOL_TCP, TCP_MAXSEG, 1024)
sd.setsockopt(SOL_TCP, TCP_NODELAY, 1)
if rcv_buf:
sd.setsockopt(SOL_SOCKET, SO_RCVBUF, rcv_buf)
sd.bind(('127.0.0.3', 1234))
sd.listen(32)
cd = socket.socket(AF_INET, SOCK_STREAM, 0)
cd.setsockopt(SOL_TCP, TCP_MAXSEG, packet_size)
cd.setsockopt(SOL_SOCKET, SO_SNDBUF, 1024*1024*4)
cd.setsockopt(SOL_TCP, TCP_NODELAY, 1)
cd.setsockopt(IPPROTO_IP, IP_BIND_ADDRESS_NO_PORT, 1)
cd.bind(('127.0.0.2', 1235))
cd.connect(('127.0.0.3', 1234))
# move to ESTAB state ASAP, since tcp_rcv_established is for ESTAB indeed
ssd, _ = sd.accept()
time.sleep(1)
done = False
waitinsec = 0.05
waitforfiller=threading.Semaphore()
waitforfiller.acquire()
def thread_fill(fill_mtd):
(fill_mtd, _, payload_sz) = fill_mtd.partition('+')
if fill_mtd == 'chunk':
chunk_max_inflight = 10
elif fill_mtd == 'sync':
chunk_max_inflight = 0
fill_mtd = 'chunk'
else:
chunk_max_inflight = None
if not payload_sz:
payload_sz = 1024
else:
if '+' in payload_sz:
(payload_sz, _, chunk_max_inflight) = payload_sz.partition('+')
chunk_max_inflight = int(chunk_max_inflight)
payload_sz = int(payload_sz)
burst_payload = b'a'* (16*1024*1024)
chunk_payload = b'a' * payload_sz
print("[+] fill=%s chunk_payload/inflight=%s/%s" % (fill_mtd, payload_sz, chunk_max_inflight))
cd.setblocking(False)
wff = waitforfiller
# if fill_mtd == 'chunk':
# cd.send(b'a'*1024*80)
while not done:
try:
if fill_mtd == 'burst':
cd.send(burst_payload)
#ssd.setsockopt(SOL_TCP, TCP_QUICKACK, 1)
if wff:
wff.release()
wff = None
# ti = cd.getsockopt(socket.SOL_TCP, socket.TCP_INFO, 512)
# (_, inflight) = struct.unpack_from("24sh", ti)
# print("inflight=%d\n", inflight)
select.select([], [cd], [], waitinsec / 1000)
continue
elif fill_mtd == 'chunk':
# cd.send(b'a'*1024*8)
# cd.send(chunk_payload)
# cd.send(chunk_payload)
# cd.send(chunk_payload)
cd.send(chunk_payload)
ssd.setsockopt(SOL_TCP, TCP_QUICKACK, 1)
inflight = sys.maxsize
t0 = time.time()
while inflight > chunk_max_inflight:
ti = cd.getsockopt(socket.SOL_TCP, socket.TCP_INFO, 512)
(_, inflight) = struct.unpack_from("24sh", ti)
if time.time() - t0 > 8:
print("[!] failed to sync inflight")
raise BlockingIOError
else:
raise "XXX"
if wff:
wff.release()
wff = None
except BlockingIOError:
time.sleep(waitinsec)
def thread_drain(drain_mtd):
waitforfiller.acquire()
(drain_mtd, _, bytespersec) = drain_mtd.partition('+')
if not bytespersec:
bytespersec = 1024
else:
if bytespersec.endswith('Kbit'):
bytespersec = int(bytespersec[:-4])*1000//8
elif bytespersec.endswith('Mbit'):
bytespersec = int(bytespersec[:-4])*1000*1000//8
else:
bytespersec = int(bytespersec)
print("[+] drain=%s bytespersec=%s" % (drain_mtd, bytespersec))
bytesperiter = int(bytespersec * waitinsec)
ssd.setblocking(False)
offset = 0.
# time.sleep(1)
# ssd.recv(bytespersec)
i = 0
while not done:
wanted = waitinsec - offset
t0 = time.time()
if drain_mtd == 'none':
pass
elif drain_mtd == 'cont':
try:
ssd.recv(bytesperiter)
except BlockingIOError:
pass
else:
raise "XXX"
time.sleep(wanted)
td = time.time()-t0
offset = td - wanted
if False: # staircase rbuf
i += 1
print(i)
ssd.setsockopt(SOL_SOCKET, SO_RCVBUF, i * 64*1024)
try:
ssd.setsockopt(SOL_TCP, socket.TCP_WINDOW_CLAMP, b'\xff\xff\xff\x7f')
except:
pass
fill_thrd = threading.Thread(target=thread_fill, args=(fill_mtd,))
fill_thrd.start()
drain_thrd = threading.Thread(target=thread_drain, args=(drain_mtd,))
drain_thrd.start()
try:
time.sleep(5)
except KeyboardInterrupt:
print("Ctrl+C pressed...")
done = True
fill_thrd.join()
drain_thrd.join()
print("[+] done")
os.system('ss -memoi sport = :1234 or dport = :1234|cat')
os.system('nstat -az TcpExtTCPRcvCollapsed TcpExtTCPRcvCoalesce TcpExtTCPRcvQDrop TcpExtTCPOFOQueue TcpExtTCPBacklogCoalesce')