This repository demonstrates how machine learning models, specifically decision trees, can be compiled into data plane rules for deployment on P4-programmable switches.
-
Mininet
A lightweight network emulator used to create virtual network topologies consisting of hosts, switches, and controllers. It allows fast prototyping and testing of P4 programs in a controlled environment. -
P4 (Programming Protocol-Independent Packet Processors)
A domain-specific language designed to describe how packets are processed on programmable switches. It enables full control over parsing, matching, and action execution on packet headers. -
P4Runtime (P4RT)
An API that provides a control plane interface to P4-programmable switches. It allows the controller to dynamically install, modify, and delete table entries on the switch at runtime.
- Generates synthetic packet flows using Scapy.
- Extracts flow-level features such as:
- Packet count
- Byte count
- Average packet size
- Flow duration
- Average inter-arrival time (IAT)
- Trains a Decision Tree (DT) classifier on the extracted flow features.
- Converts the DT logic into exact-match rules that can be understood by a P4 switch.
- Installs the rules via P4Runtime onto the switch, allowing in-network ML-based classification with explainable rules.
Start by cloning the official P4 tutorials repo:
$ git clone
Run the packet generator to create a .pcap
file with synthetic traffic consisting of multiple 5-tuple TCP flows. This file will also generate a decision tree for you in the output
directory.
$ python3 generate_pcap.py
Use the decision tree to generate rules for the controller.
$ python3 dt_rule_gen.py
Use make
to launch the Mininet + Stratum container in another terminal:
$ make mininet
Once started, you will see the mininet> prompt. This indicates that your virtual network is ready and running, and you can now issue commands through this prompt. Let's try listing the hosts and switches in this network and their connectivity. Enter ...
$ mininet> net
Output:
mininet> net
h1 h1-eth0:s1-eth1
h2 h2-eth0:s1-eth2
s1 lo: s1-eth1:h1-eth0 s1-eth2:h2-eth0
This shows three nodes in this network: h1
, h2
, and s1
. For h1
and h2
, their eth0
interface is connected to switch s1
eth1
and eth2
interfaces, respecitvely.
Note: Visit http://mininet.org/walkthrough/ to learn more about Mininet and the various commands you can run inside it.
Open another shell and run the starter code:
$ make controller name=decision-tree grpc_port=50001 topo=linear,2
This will:
-
Read topo/linear,2.json for decision tree rules
-
Connect to the P4 switch on gRPC port 50001
-
Insert table entries into MyIngress.classifier for flows that match the decision tree logic
-
Modify the P4 Program
- Use registers to track flow-level features such as:
pkt_count
byte_count
avg_pkt_size
duration
avg_iat
- Extract flow identifiers from 5-tuple (src IP, dst IP, src port, dst port, protocol).
- Use
register.read()
andregister.write()
to maintain per-flow state across packets.
- Use registers to track flow-level features such as:
-
Use Classifier Table
- Define a
classifier
table that matches on the flow features. - Implement actions like
write_result(result)
that store the classification output in metadata.
- Define a
-
Send Digest to Control Plane
- After classification (e.g., on FIN flag), trigger a digest message.
- Include in the digest: 5-tuple fields and the
result
.
-
Implement Python Receiver
- Write a Python script using
p4runtime-sh
orp4runtime API
to:- Listen for incoming digest messages.
- Extract and print the flow's 5-tuple and classification result.
- Write a Python script using
-
Run and Demo
- Generate PCAP using
generate_pcap.py
. - Extract features and rules with
dt_rule_gen.py
. - Install rules using
make controller name=decision-tree
. - Send packets using
send.py
. - Observe classification results in the Python receiver.
- Generate PCAP using
You now have an interpretable decision tree model compiled into P4 match-action rules, deployed directly on a P4-enabled switch using Mininet + P4Runtime.
This codebase uses scripts adapted from the Purdue CS 536 Public Repository.