Flexible traffic generation with scapy

DiscoTroy picture DiscoTroy · Sep 20, 2011 · Viewed 10.7k times · Source

I know questions like this have been asked plenty of times before, but I think this is subtley different.

I am attempting to write a flexible traffic generator in Python using scapy. Producing the packet is fine, but when it comes to sending traffic at a sufficiently fast rate (for my needs, somewhere in the range of 500-700 packets per second), I seem to have hit a wall at around 20-30 pps.

I believe that there may be some need for threading, or am I missing something easier?

Answer

Anders Waldenborg picture Anders Waldenborg · Sep 23, 2011

On my system I get much better performance sending ethernet frames 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 (precreated) packet on the socket manually is way 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 the pe.build() into the loop drastically reduces speed, hinting that it is the actual packet building that takes time.