use*_*439 5 python networking scapy
我正在创建一个脚本,将来自 Tap0 的所有流量发送到 Eth0,并将来自 Eth0 的所有流量发送到 Tap0。在网上找到了很多例子后,我设法让它发挥作用。我遇到的问题是性能非常低。
在不使用脚本的情况下在 2 个虚拟机之间执行 Ping 操作所需时间不到 1 毫秒。使用脚本大约需要 15 毫秒。
当我使用 scp 将 10 MB 文件从虚拟机发送到另一个虚拟机时,平均。没有脚本时传输速率为 12 Mbps。使用该脚本,速度可降至不到 1 Mbps。
我知道Python实际上并不是处理网络流量最快的语言,但它有那么慢吗?
有没有办法优化这段代码?
我的虚拟机是 Ubuntu 10.04 32 位。
这是代码:
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
Run Code Online (Sandbox Code Playgroud)
老实说,我对它的表现感到惊讶。如果您能做得比现在更好,我会感到惊讶。
请记住数据包穿过用户域桥所必须遵循的路径:
通过 NIC 驱动程序进入一个接口,进入内核,然后它必须等待上下文切换到用户态,在用户态中它必须先获取 scapy 协议抽象,然后才能由您的代码进行评估。接下来,您的代码发送回 scapy 协议抽象(可能在 python 用户空间中重新组装数据包),写入套接字,等待上下文切换回内核空间,写入 NIC 驱动程序,最后发送出去界面...
现在,当您 ping 该链接时,您将测量两次完成整个过程所需的时间 - 一次去,一次返回。
考虑到你的上下文从内核到用户态切换了 4 次(每个方向 2 次)并且你能够在 0.015 秒内完成 - 这非常好。
归档时间: |
|
查看次数: |
6890 次 |
最近记录: |