Flexible traffic generation with scapy

I know that such questions have been asked many times before, but I think this is a little different.

I am trying to write a flexible traffic generator in Python using scapy. Creating a packet is fine, but when it comes to sending traffic at a fairly high speed (for my needs, somewhere in the range of 500-700 packets per second), I seem to hit a wall of about 20-30 pps.

I believe there may be some need for streaming, or am I missing something simpler?

+4
source share
2 answers

On my system, I get much better performance by sending ethernet packets with sendp compared to sending IP packets using send.

# this gives appox 500pps on my system pe=Ether()/IP(dst="10.13.37.218")/ICMP() sendp(pe, loop=True) # this gives approx 100pps on my system pi=IP(dst="10.13.37.218")/ICMP() send(pi, loop=True) 

But sending a (pre-processed) packet on the socket is faster:

 s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW) s.bind(("eth0", 0)) pe=Ether()/IP(dst="10.13.37.218")/ICMP() data = pe.build() while True: s.send(data) 

But moving pe.build () into the loop drastically slows down, hinting that this actual package building takes time.

+7
source

FTR, although the answer above is correct, it can also be implemented at level 2 using a Scapy socket:

 from scapy.all import * sock = conf.L2socket() pe=Ether()/IP(dst="10.13.37.218")/ICMP() data = pe.build() while True: pe.send(data) 

Although, if sending packets in a loop is your goal:

 send(Ether()/IP(dst="10.13.37.218")/ICMP(), loop=1) 

I will do :-)

0
source

All Articles