SDN languages and tools
Practical study notes on SDN languages (P4, OpenFlow, Frenetic/Pyretic, eBPF) and tools (Mininet, bmv2, p4c, ovs-ofctl, P4Runtime, Ryu/Opendaylight) explaining what each is, how they differ, and step-by-step examples for packet-processing and flow installation.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
SDN languages and tools let engineers program how network devices process packets and how controllers manage flows. An engineer uses these languages (e.g., P4, OpenFlow) and tools (e.g., Mininet, Open vSwitch) to prototype, test and deploy custom packet processing in data centres and research labs without redesigning hardware.
Key ideas
P4 (Programming Protocol-Independent Packet Processors) — a high-level DSL for describing how the data plane parses packet headers, matches on header fields and applies actions. Exact definition: "P4 is a language to define packet header formats, parsers, match-action tables, and control flow so that packet processing behavior is programmer-defined and protocol-independent." Assumptions: target is a programmable data plane (software switch such as bmv2 or hardware that supports P4); the compiler and target support required externs/actions.
- What to draw: draw a vertical pipeline box labelled 'Parser' at top, inside it a small state diagram showing Ethernet -> IPv4 (arrow). Below, draw a sequence of table boxes: table1 (L2) -> table2 (L3) -> action unit -> deparser. Label input packets at top left and output packets at bottom right.
Match-action table — exact statement: "A match-action table is a finite set of entries; each entry maps a match key (set of header fields, possibly with masks or ternary wildcards) to an action, and may include a priority for ternary matches; tables also have a default action when no entry matches." Assumptions: entries are finite and installed either by control plane or statically; matches evaluated under the table's match type (exact, ternary, LPM).
OpenFlow — protocol and model where the controller installs flow entries (match + actions) into switches using a standard message set. Definition: "OpenFlow is an interface for controllers to query a switch's flow tables and statistics and to add, modify, and delete flow/group/meter entries." Assumptions: switch implements OpenFlow agent and has flow tables compatible with protocol version.
eBPF (extended Berkeley Packet Filter) — a safe programmable bytecode executed in the Linux kernel for packet processing, tracing, and more. Definition: "eBPF is a restricted, verifiable bytecode run by the kernel with helper functions; it can be attached to hooks (tc, XDP, socket) to modify and inspect packets." Assumptions: kernel supports eBPF verifier and required helpers; user compiles C to eBPF bytecode.
Mininet — a lightweight network emulator that runs many host and switch processes on one Linux machine using network namespaces and virtual Ethernet links. Draw: one host namespace box per host, veth links between hosts and a software switch (Open vSwitch) process in the middle.
Behavioral Model v2 (bmv2) — a software switch implementation that acts as a reference P4 target. It executes P4 programs in software for testing.
P4Runtime — a control-plane API (gRPC + protobuf) for controllers to install table entries into P4 targets at runtime. Use when controller needs to program P4 switches.
Open vSwitch (OVS) — a software switch widely used with SDN controllers; supports OpenFlow and integration with kernel datapath.
Controller frameworks — Ryu, ONOS, OpenDaylight, Floodlight: these are separate topics but important as they run the control logic and speak OpenFlow; some also support P4Runtime (e.g., ONOS).
Notes on trade-offs (exam style): P4 programs change parsing and actions and give fine-grained data-plane control; OpenFlow gives controller-driven table updates but depends on the switch's fixed pipeline model. eBPF is tied to Linux kernel hooks and is very practical for loadable dataplane features on servers.
Where I am unsure: exact list of hardware vendors that support specific P4 externs and which P4 language version (v1model vs PSA) each vendor uses; list appears later when recommending targets.
Formulas
(For this programming topic, key syntax and commands to remember.)
header ethernet_t { bit<48> dst; bit<48> src; bit<16> ethertype; } — defines an Ethernet header type in P4_16; use inside a P4 program's header declarations.
parser parse_ethernet(packet_in pkt, out headers hdr) { state start { pkt.extract(hdr.ethernet); transition select(hdr.ethernet.ethertype) { 0x0800: parse_ipv4; default: accept; } } } — P4 parser state syntax: pkt.extract reads bytes into header struct; select chooses next state.
table ipv4_lpm { key = { hdr.ipv4.dstAddr: lpm; } actions = { forward; drop; } size = 1024; } — P4 table declaration: match type lpm for longest-prefix match; ensure all listed actions (e.g., drop) are defined in your program.
action forward(bit<48> dst_mac, bit<9> port) { hdr.ethernet.dst = dst_mac; standard_metadata.egress_spec = port; } action drop() { mark_to_drop(); } — P4 actions: forward sets header fields and egress port; drop marks the packet to be dropped on v1model/PSA targets.
p4c --target bmv2 --arch v1model -o build/program.json program.p4 — compile P4 program for the bmv2 v1model target, producing a JSON for simple_switch_grpc.
simple_switch_grpc --device-id 0 build/program.json — start behavioral model with P4 JSON and gRPC control plane endpoint.
ovs-ofctl add-flow s1 'in_port=1,dl_type=0x0800,nw_dst=10.0.0.2,actions=output:2' — OpenFlow flow-add command via ovs-ofctl: match on in_port and IPv4 dst and forward to port 2.
sudo mn --topo single,3 --switch ovsk --controller remote — start Mininet with 3 hosts, OVS kernel switch, and a remote controller.
clang -O2 -target bpf -c xdp_prog.c -o xdp_prog.o — compile eBPF/XDP program from C to object file (requires LLVM/clang).
Worked examples
P4 packet-processing trace (step-by-step) Problem: A P4 program parses Ethernet and IPv4 and has a table
ipv4_lpmthat maps dstAddr 10.0.0.2/32 -> action forward(dst_mac=00:11:22:33:44:55, port=2). Trace an input Ethernet frame with dst MAC 00:aa:bb:cc:dd:ee, ethertype 0x0800, IPv4 dst 10.0.0.2.Steps:
- Parser state
start:pkt.extract(hdr.ethernet)reads Layer2 fields. Nowhdr.ethernet.ethertype == 0x0800so transition toparse_ipv4. parse_ipv4:pkt.extract(hdr.ipv4)reads IP header;hdr.ipv4.dstAddr == 10.0.0.2.- Control flow:
apply(ipv4_lpm)looks up keyhdr.ipv4.dstAddrwith LPM. Entry for 10.0.0.2/32 matches. - Action
forwardexecuted: setshdr.ethernet.dst = 00:11:22:33:44:55andstandard_metadata.egress_spec = 2. - Deparser emits headers: Ethernet (with new dst MAC) then IPv4. Packet forwarded out port 2.
Final: packet forwarded out port 2 with dst MAC 00:11:22:33:44:55.
- Parser state
Installing an OpenFlow proactive flow with ovs-ofctl (command trace) Problem: In Mininet switch
s1(OVS bridges1) you must make any IPv4 packet with dst 10.0.0.2 go out port 2.Steps and expected shell outputs (typical):
- Add flow:
sudo ovs-ofctl add-flow s1 "ip,nw_dst=10.0.0.2,actions=output:2" - Verify:
sudo ovs-ofctl dump-flows s1prints a line likeNXST_FLOW reply (x bytes) cookie=0x0, duration=..., table=0, n_packets=0, n_bytes=0, ip,nw_dst=10.0.0.2 actions=output:2. - Send test packet from host h1 to 10.0.0.2:
h1 ping -c1 10.0.0.2— packet observed leaving s1 on port 2.
Final: Flow installed and verified by dump-flows and packet forwarding.
- Add flow:
Short exam-style compare: "Compare P4 and OpenFlow for data-plane programmability" (model answer)
- P4 is a language to describe packet parsing, match-action tables and control flow. It programs the data plane itself and requires a P4-capable target or software switch (bmv2). It gives protocol-independent parsing — you can define new headers.
- OpenFlow is a control-plane protocol for installing flow rules into an existing switch pipeline. It does not change how the switch parses packets: you are limited by the switch's fixed header extraction and match fields.
- Use P4 when you need new protocols or custom parsing. Use OpenFlow when you want centralised flow control on standard header fields and when using many commodity switches that already implement OpenFlow.
Common mistakes
- Using
ovs-ofctlflow syntax as if it accepts P4 runtime table names — OpenFlow matches fields supported by OVS only. - Expecting Mininet to emulate hardware performance — Mininet emulates topology and control, not forwarding-rate behaviour of ASICs.
- Forgetting to set egress port metadata in P4 (standard_metadata.egress_spec) — packet will be dropped or misforwarded.
- Not running privileged commands (Mininet, ovs-ofctl) with sudo; leads to confusing errors.
For your exam
Kinds of questions:
- Definitions: define P4, match-action table, eBPF, Mininet.
- Short notes: advantages/disadvantages of P4 vs OpenFlow; role of bmv2; what P4Runtime does.
- Derivations/diagrams: draw a P4 pipeline and label parser, tables, actions, deparser; trace packet through parser and tables.
- Programs: small P4 snippets (header, parser, table, action) and explain what they do.
- Commands: write the ovs-ofctl command to add a flow for IPv4 dst.
What full-marks answers include:
- Precise definitions with bolded key terms, assumptions and limitations.
- Correct diagram labels (parser, tables, action, metadata, controller interface).
- Step-by-step trace for program or packet with header field values at each step.
- For commands/programs: correct syntax and explanation of each field.
Quick check
- What does P4 allow you to define that OpenFlow does not?
- Name two tools used to test P4 programs in software.
- Where does eBPF run and why is it restricted?
Answers: 1) Packet parser/header formats and the pipeline itself (protocol-independence). 2) bmv2 (behavioral model) and Mininet — also simple_switch_grpc. 3) eBPF runs inside the Linux kernel; it is restricted and verified by the kernel verifier to ensure safety (no unbounded loops, safe memory access).
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?