The T-Rex Open Traffic Generator Python Package
Project description
Snappi-Trex
Snappi-Trex is a Snappi plugin that allows executing scripts written using Snappi with Cisco's T-Rex Traffic Generator
Installing Snappi-Trex
First, make sure that Snappi is installed.
pip3 install snappi
Now, install the snappi_trex extension.
pip3 install snappi_trex
Installing and Running T-Rex
For a quick tutorial on T-Rex installation, running, and basic usage, check out my T-Rex Tutorial
Start Scripting
import snappi
def hello_snappi():
"""
This script does following:
- Send 1000 packets back and forth between the two ports at a rate of
1000 packets per second.
"""
# create a new API instance where host points to controller
api = snappi.api(host='https://localhost', ext='trex')
# and an empty traffic configuration to be pushed to controller later on
cfg = api.config()
# add two ports where location points to traffic-engine (aka ports)
p1, p2 = (
cfg.ports
.port(name='p1', location='localhost:5555')
.port(name='p2', location='localhost:5556')
)
# add layer 1 property to configure same speed on both ports
ly = cfg.layer1.layer1(name='ly')[-1]
ly.port_names = [p1.name, p2.name]
ly.speed = ly.SPEED_1_GBPS
# enable packet capture on both ports
cp = cfg.captures.capture(name='cp')[-1]
cp.port_names = [p1.name, p2.name]
# add two traffic flows
f1, f2 = cfg.flows.flow(name='flow p1->p2').flow(name='flow p2->p1')
# and assign source and destination ports for each
f1.tx_rx.port.tx_name, f1.tx_rx.port.rx_name = p1.name, p2.name
f2.tx_rx.port.tx_name, f2.tx_rx.port.rx_name = p2.name, p1.name
# configure packet size, rate and duration for both flows
f1.size.fixed = 128
f2.size.fixed = 256
for f in cfg.flows:
# send 1000 packets and stop
f.duration.fixed_packets.packets = 1000
# send 1000 packets per second
f.rate.pps = 1000
# configure packet with Ethernet, IPv4 and UDP headers for both flows
eth1, ip1, udp1 = f1.packet.ethernet().ipv4().udp()
eth2, ip2, udp2 = f2.packet.ethernet().ipv4().udp()
# set source and destination MAC addresses
eth2.src.value, eth2.dst.value = '00:AA:00:00:00:AA', '00:AA:00:00:04:00'
eth1.src.increment.start = '10:AA:00:00:04:00'
eth1.src.increment.step = 2
eth1.src.increment.count = 1000
eth2.src.values = ['33:33:33:11:11:11', '22:22:22:22:22:22']
eth1.dst.decrement.start = '10:AA:00:00:04:00'
eth1.dst.decrement.step = 4
eth1.dst.decrement.count = 1000
# set source and destination IPv4 addresses
ip2.src.value, ip2.dst.value = '10.0.0.2', '10.0.0.1'
ip1.src.increment.start = '11.0.0.1'
ip1.src.increment.step = 2
ip1.src.increment.count = 1000
ip1.dst.decrement.start = '12.0.0.0'
ip1.dst.decrement.step = 4
ip1.dst.decrement.count = 1000
# set incrementing port numbers as source UDP ports
udp1.src_port.value = 5000
udp2.src_port.decrement.start = 5000
udp2.src_port.decrement.step = 1
udp2.src_port.decrement.count = 1000
# assign list of port numbers as destination UDP ports
udp1.dst_port.values = [8000, 8004, 8049, 9001]
udp2.dst_port.decrement.start = 5000
udp2.dst_port.decrement.step = 1
udp2.dst_port.decrement.count = 1000
print('Pushing traffic configuration ...')
api.set_config(cfg)
print('Starting transmit on all configured flows ...')
ts = api.transmit_state()
ts.state = ts.START
api.set_transmit_state(ts)
if __name__ == '__main__':
hello_snappi()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
snappi_trex-0.0.71.tar.gz
(11.2 kB
view hashes)
Built Distribution
Close
Hashes for snappi_trex-0.0.71-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cf0140673a6b6112a04f575fa76dea56236ec86b2f038ef505e70df59d3b3000 |
|
MD5 | ad2f58a06a91732e9791427e5d016192 |
|
BLAKE2b-256 | 0c2380cbd56983c6450bd7f824276f63d996e0e9f7fdd30ff0e794130cf0ef5e |