我正在创建一个脚本,从Tap0向Eth0发送所有流量,并从Eth0向Tap0发送所有流量。在网上找到了许多例子后,我设法使它发挥作用。我的问题是性能很低。在
不使用脚本在两个虚拟机之间进行ping,所需时间小于1ms。使用脚本则需要~15ms
当从一个没有scp的脚本发送到另一个.10 MB的文件时,使用的是一个10 MB的平均速率。有了这个脚本,它会降到低于1Mbps的速度。在
我知道Python实际上不是处理网络流量最快的语言,但是它有那么慢吗?在
有没有方法可以优化这段代码?在
我的虚拟机是ubuntu10.0432位。在
代码如下:
import os,sys,getopt,struct,re,string,logging
from socket import *
from fcntl import ioctl
from select import select
from scapy.all import *
TUNSETIFF = 0x400454ca
IFF_TAP = 0x0002
TUNMODE = IFF_TAP
ETH_IFACE = "eth0"
TAP_IFACE = "tap0"
conf.iface = ETH_IFACE
# Here we capture frames on ETH0
s = conf.L2listen(iface = ETH_IFACE)
# Open /dev/net/tun in TAP (ether) mode (create TAP0)
f = os.open("/dev/net/tun", os.O_RDWR)
ifs = ioctl(f, TUNSETIFF, struct.pack("16sH", "tap%d", TUNMODE))
# Speed optimization so Scapy does not have to parse payloads
Ether.payload_guess=[]
os.system("ifconfig eth0 0.0.0.0")
os.system("ifconfig tap0 192.168.40.107")
os.system("ifconfig tap0 down")
os.system("ifconfig tap0 hw ether 00:0c:29:7a:52:c4")
os.system("ifconfig tap0 up")
eth_hwaddr = get_if_hwaddr('eth0')
while 1:
r = select([f,s],[],[])[0] #Monitor f(TAP0) and s(ETH0) at the same time to see if a frame came in.
#Frames from TAP0
if f in r: #If TAP0 received a frame
# tuntap frame max. size is 1522 (ethernet, see RFC3580) + 4
tap_frame = os.read(f,1526)
tap_rcvd_frame = Ether(tap_frame[4:])
sendp(tap_rcvd_frame,verbose=0) #Send frame to ETH0
#Frames from ETH0
if s in r: #If ETH0 received a frame
eth_frame = s.recv(1522)
if eth_frame.src != eth_hwaddr:
# Add Tun/Tap header to frame, convert to string and send. "\x00\x00\x00\x00" is a requirement when writing to tap interfaces. It is an identifier for the Kernel.
eth_sent_frame = "\x00\x00\x00\x00" + str(eth_frame)
os.write(f, eth_sent_frame) #Send frame to TAP0
老实说,我很惊讶它的表现。如果你能比现在做得更好,我会很惊讶的。在
请记住数据包通过用户陆桥时必须遵循的路径:
进入一个接口,通过NIC驱动程序进入内核,然后它必须等待上下文切换到user land,在那里它必须clime scapy协议抽象,然后才能由代码对其求值。然后你的代码发送回scapy协议抽象(可能在python用户空间重新组装包),被写入套接字,等待上下文切换回内核,写入NIC驱动程序,最后被发送到接口。。。在
现在,当你ping过这个链接时,你要计算两次完成整个过程所需的时间——一次是去,一次是返回。在
考虑到你的上下文从内核切换到用户登陆4次(每个方向2次),并且你能在0.015秒内完成这个任务——这很好。在
我也有类似的问题:从一个似乎有 disected scapy's source code的链接
也是一个similar analysis here (link2)。因此,您可以根据link2中的示例代码进行优化。在
相关问题 更多 >
编程相关推荐