Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 64 additions & 39 deletions python-ecosys/iperf3/iperf3.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,33 @@ def make_cookie():
return cookie


def _transfer(udp, reverse, addr, s_data, buf, udp_last_send, udp_packet_id, udp_interval, stats):
if udp:
if reverse:
recvninto(s_data, buf)
udp_in_sec, udp_in_usec, udp_in_id = struct.unpack_from(">III", buf, 0)
if udp_in_id != udp_packet_id + 1:
stats.add_lost_packets(udp_in_id - (udp_packet_id + 1))
udp_packet_id = udp_in_id
stats.add_bytes(len(buf))
else:
t = ticks_us()
if t - udp_last_send > udp_interval:
udp_last_send += udp_interval
udp_packet_id += 1
struct.pack_into(">III", buf, 0, t // 1000000, t % 1000000, udp_packet_id)
n = s_data.sendto(buf, addr)
stats.add_bytes(n)
else:
if reverse:
recvninto(s_data, buf)
n = len(buf)
else:
n = s_data.send(buf)
stats.add_bytes(n)
return udp_last_send, udp_packet_id


def server_once():
# Listen for a connection
ai = socket.getaddrinfo("0.0.0.0", 5201)
Expand Down Expand Up @@ -233,13 +260,21 @@ def server_once():
s_data, addr = s_listen.accept()
print("Accepted connection:", addr)
recvn(s_data, COOKIE_SIZE)
udp = False
udp_packet_id = 0
udp_interval = None
udp_last_send = None
elif param.get("udp", False):
# Close TCP connection and open UDP "connection"
s_listen.close()
s_data = socket.socket(ai[0], socket.SOCK_DGRAM)
s_data.bind(ai[-1])
data, addr = s_data.recvfrom(4)
s_data.sendto(b"\x12\x34\x56\x78", addr)
s_data.sendto(struct.pack("<I", 987654321), addr)
udp_interval = 1000000 * 8 * param["len"] // param["bandwidth"]
udp = True
udp_packet_id = 0
udp_last_send = ticks_us() - udp_interval
else:
assert False

Expand Down Expand Up @@ -269,16 +304,21 @@ def server_once():
if cmd == TEST_END:
running = False
elif pollable_is_sock(pollable, s_data):
if reverse:
n = s_data.send(data_buf)
stats.add_bytes(n)
else:
recvninto(s_data, data_buf)
stats.add_bytes(len(data_buf))
udp_last_send, udp_packet_id = _transfer(
udp,
not reverse,
addr,
s_data,
data_buf,
udp_last_send,
udp_packet_id,
udp_interval,
stats,
)
stats.update()

# Need to continue writing so other side doesn't get blocked waiting for data
if reverse:
if reverse and not udp:
while True:
for pollable in poll.poll(0):
if pollable_is_sock(pollable, s_data):
Expand Down Expand Up @@ -360,6 +400,7 @@ def client(host, udp=False, reverse=False, bandwidth=10 * 1024 * 1024):
else:
param["tcp"] = True
param["len"] = 3000
udp_interval = None

if reverse:
param["reverse"] = True
Expand Down Expand Up @@ -403,33 +444,17 @@ def client(host, udp=False, reverse=False, bandwidth=10 * 1024 * 1024):
stats.stop()
else:
# Send/receiver data
if udp:
if reverse:
recvninto(s_data, buf)
udp_in_sec, udp_in_usec, udp_in_id = struct.unpack_from(">III", buf, 0)
# print(udp_in_sec, udp_in_usec, udp_in_id)
if udp_in_id != udp_packet_id + 1:
stats.add_lost_packets(udp_in_id - (udp_packet_id + 1))
udp_packet_id = udp_in_id
stats.add_bytes(len(buf))
else:
# print('UDP send', udp_last_send, t, udp_interval)
if t - udp_last_send > udp_interval:
udp_last_send += udp_interval
udp_packet_id += 1
struct.pack_into(
">III", buf, 0, t // 1000000, t % 1000000, udp_packet_id
)
n = s_data.sendto(buf, ai[-1])
stats.add_bytes(n)
else:
if reverse:
recvninto(s_data, buf)
n = len(buf)
else:
# print('TCP send', len(buf))
n = s_data.send(buf)
stats.add_bytes(n)
udp_last_send, udp_packet_id = _transfer(
udp,
reverse,
ai[-1],
s_data,
buf,
udp_last_send,
udp_packet_id,
udp_interval,
stats,
)

elif pollable_is_sock(pollable, s_ctrl):
# Receive command
Expand Down Expand Up @@ -534,7 +559,7 @@ def main():
client(opt_host, opt_udp, opt_reverse)


if sys.platform == "linux":
if sys.implementation.name != "micropython":

def pollable_is_sock(pollable, sock):
return sock is not None and pollable[0] == sock.fileno()
Expand All @@ -544,12 +569,12 @@ def ticks_us():

def ticks_diff(a, b):
return a - b

if __name__ == "__main__":
main()
else:

def pollable_is_sock(pollable, sock):
return pollable[0] == sock

from time import ticks_us, ticks_diff

if sys.platform == "linux" and __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion python-ecosys/iperf3/manifest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
metadata(version="0.1.4", pypi="iperf3", pypi_publish="uiperf3")
metadata(version="0.1.5", pypi="iperf3", pypi_publish="uiperf3")

module("iperf3.py")
Loading