나는 Tap0에서 모든 트래픽을 Eth0으로 보내고 Eth0에서 모든 트래픽을 Tap0으로 보냅니다. 온라인으로 많은 예제를 찾은 후에, 나는 그것을 잘 만들 수있었습니다. 내가 가진 문제는 성능이 매우 낮다는 것입니다.Scapy로 낮은 성능
스크립트를 사용하지 않고 VM 2 개를 핑하는 데 1 밀리 초가 걸리지 않습니다. 스크립트로 ~ 15ms가 걸립니다.
scp를 사용하여 VM에서 다른 것으로 10MB 파일을 보낼 때 평균. 전송 속도는 스크립트없이 12Mbps입니다. 스크립트로 1Mbps 미만으로 내려갑니다.
저는 파이썬이 실제로 네트워크 트래픽을 처리하는 가장 빠른 언어가 아니라는 것을 알고 있습니다. 그러나 느린가?
이 코드를 최적화 할 수있는 방법이 있습니까?
내 VM은 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
REPL 외부에서 와일드 카드 가져 오기 ('*')를 사용하지 마십시오. – jfs
프로파일 러는 무엇을 말합니까? 병목은 어디에 있습니까? – jfs
어떻게 결국 문제를 해결 했습니까? –