我正在尝试编写程序来并行测试各种大小的数据包的数据传输速度。我注意到一些奇怪的事情,根据我的程序,数据包的大小似乎对传输时间没有影响,而Unix ping
二进制文件会在我使用的某些数据包大小上超时。我发送了4个包含字符串' testquest'并且只有2000字节设置为0.但是,当我打印结果时,他们所有包含' testquest' (并且远远短于2000字节)。我唯一可以得出的结论是,这些套接字在某种程度上都接收到相同的数据包,这可以解释它们如何具有相同的rtt。
我做了这个MCVE来说明这个问题(你可以忽略“校验和”功能,它包含在内,但是我从经验中知道它有效):
#!/usr/bin/env python3
import socket
import struct
import time
from multiprocessing.pool import ThreadPool as Pool
from sys import argv, byteorder
def calculate_checksum(pkt):
"""
Implementation of the "Internet Checksum" specified in RFC 1071 (https://tools.ieft.org/html/rfc1071)
Ideally this would act on the string as a series of 16-bit ints (host
packed), but this works.
Network data is big-endian, hosts are typically little-endian,
which makes this much more tedious than it needs to be.
"""
countTo = len(pkt) // 2 * 2
total, count = 0, 0
# Handle bytes in pairs (decoding as short ints)
loByte, hiByte = 0, 0
while count < countTo:
if (byteorder == "little"):
loByte = pkt[count]
hiByte = pkt[count + 1]
else:
loByte = pkt[count + 1]
hiByte = pkt[count]
total += hiByte * 256 + loByte
count += 2
# Handle last byte if applicable (odd-number of bytes)
# Endianness should be irrelevant in this case
if countTo < len(pkt): # Check for odd length
total += pkt[len(pkt) - 1]
total &= 0xffffffff # Truncate sum to 32 bits (a variance from ping.c, which
# uses signed ints, but overflow is unlikely in ping)
total = (total >> 16) + (total & 0xffff) # Add high 16 bits to low 16 bits
total += (total >> 16) # Add carry from above (if any)
return socket.htons((~total) & 0xffff)
def ping(args):
sock, payload = args[0], args[1]
header = struct.pack("!BBH", 8, 0, 0)
checksum = calculate_checksum(header+payload)
header = struct.pack("!BBH", 8, 0, checksum)
timestamp = time.time()
sock.send(header+payload)
try:
response = sock.recv(20+len(payload))
except socket.timeout:
return 0
return (len(response), (time.time() - timestamp) * 1000)
host = argv[1] # A host that doesn't respond to ping packets > 1500B
# 1 is ICMP protocol number
sockets = [socket.socket(socket.AF_INET, socket.SOCK_RAW, proto=1) for i in range(12)]
for i, sock in enumerate(sockets):
sock.settimeout(0.1)
sock.bind(("0.0.0.0", i))
sock.connect((host, 1)) # Port number should never matter for ICMP
args = [(sockets[i], bytes(2**i)) for i in range(12)]
for arg in args:
print(ping(arg))
arg[0].close()
这实际上向我展示了一些更令人不安的事情 - 似乎rtt实际上随着数据包大小的增加而正在减少!调用此程序(以root身份获取套接字权限)输出:
0
0
(24, 15.784025192260742)
(28, 0.04601478576660156)
(28, 0.025033950805664062)
(28, 0.033855438232421875)
(28, 0.03528594970703125)
(28, 0.04887580871582031)
(28, 0.05316734313964844)
(28, 0.03790855407714844)
(28, 0.0209808349609375)
(28, 0.024080276489257812)
但现在注意当我尝试使用ping
发送大小为2048的数据包时会发生什么:
user@mycomputer ~/src/connvitals $ time ping -c1 -s2048 $box
PING <hostname redacted> (<IP address redacted>): 2048 data bytes
--- <hostname redacted> ping statistics ---
1 packets transmitted, 0 packets received, 100.0% packet loss
real 0m11.018s
user 0m0.005s
sys 0m0.008s
不仅数据包丢失,而且需要11秒才能完成!那么为什么 - 如果我的超时设置为100毫秒 - 这个数据包是否成功了#34;来自我的python脚本的响应只有~0.04ms ??
提前感谢您提供的任何帮助。
我刚刚再次检查过,似乎它的多个套接字是问题所在,并且线程似乎与它无关。当我对每个套接字执行ping操作时,我会遇到同样的问题 - 然后立即关闭它 - 顺序执行。
答案 0 :(得分:3)
所有套接字都是相同的,并且都绑定到同一主机。数据包中没有任何信息可供内核知道要转到哪个套接字,而raw(7)似乎暗示所有套接字都会接收它们。
您可能会在所有主题中获得所有回复,这意味着您获得的每个帖子的响应次数是您预期的12倍。