This repo provides a simple example of how to use Tofino's packet generator to send packets from one the internal dev_port to the physical port.
This repo is motivated by tofino-pktgen. The description of Tofino's packet generator is in Sec. 9 of the Tofino Native Architecture. You can also get some description of the parameters in this slide.
Tofino will generate packet to the internal dev_port, which is not visible to the host. Check Table 8 at page 77 of Tofino Native Architecture.
You can configure the packet generator to send packets to a physical port or process it inside the switch. This repo uses a simple forwarding program that forwards packets from one port to another with a simple match-action table.
action send(PortId_t port){
ig_tm_md.ucast_egress_port = port;
}
table forward{
key = {ig_intr_md.ingress_port : exact;}
actions = {
send;
@defaultonly NoAction;
}
const default_action = NoAction();
size = 20;
}
Build the P4 program and load it to the Tofino switch.
sudo p4-build simple_forwarding.p4
tfm -p simple_forwarding
# in another terminal
p4 -p simple_forwarding
Here we send generated packets from 196 to port 1/0 (dev_port=0). Check ucli.pm
for the dev_port numbers.
At bfshell
, run
ucli
pm
# enable port
port-enb 1/0
# check if it's enabled
show -p 1/0
exit
# add table eentry
bfrt_python
bfrt
simple_forwarding.pipe.Ingress.forward.add_with_send(ingress_port=196,port=0)
Run the following control plane code to configure and start the packet generator. Check the comments pktgen_setup.py
for more details. It uses the Periodic timer trigger
mode.
python3 pktgen_setup.py --rate 0.001 --pktsize 256 --genport 196
This command will generate packets with a size of 256 bytes at a rate of 0.001 Gbps (1 Mbps) from the generator port 196.
You can then using tcpdump
to check the packets on port 1/0:
# veth0 corresponds to port 1/0
sudo tcpdump -i veth0 -nn -tt
Note that the generated packets will have a PktGen header after other metadata headers and before the Ethernet header. You can use scapy
to parse the packets and remove the PktGen header.
To stop the packet generator, you can run the following command:
python3 pktgen_stop.py
This repo is now a minimal runable example. I will keep updating this repo if I find any other useful features or configurations. Let me know if you have any questions or suggestions.