# openflow — OpenFlow 1.3 SDN Controller <-> Switch Messages (`openflow`)

> **FrameAssembly v3.2.2** · Protocol 28 of 81 · Canonical URL: https://frameassembly-lang.dev/docs/protocols/openflow

- **Full Protocol Name:** OpenFlow SDN Control Channel (OpenFlow 1.3 / 1.4)
- **Keyword:** `openflow`
- **OSI Layer & Transport:** Layer 3 · TCP 6653
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** ONF OpenFlow 1.3 (TS-023), OpenFlow 1.4
- **Wireshark Display Filter:** `openflow_v4 || openflow`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> OpenFlow SDN (TCP 6653)`

## Overview

Speak directly to virtual switches and SDN controllers over TCP `6653`: `openflow` builds `hello`, `featuresreply` (`dpid`), `packetin`, and `flowmod` headers without booting an entire controller stack.

OpenFlow (`openflow`) is the Software-Defined Networking (SDN) southbound control protocol used between controllers (ONOS, Ryu, OpenDaylight, OVN `ovs-vswitchd`) and OpenFlow switches over **TCP `dstport 6653`** (legacy `6633`). Every OpenFlow message starts with an 8-byte header (`version`, `type`, 16-bit `length`, and 32-bit Transaction ID `xid`).

FrameAssembly defaults to **OpenFlow 1.3 (`version 4`)**, shares 4-tuple TCP sequence tracking with `tcp`, and encodes structured payloads for `hello`, `echoreq`/`echoreply`, `featuresreq`/`featuresreply` (with 64-bit Datapath ID `dpid`), `packetin`, `packetout`, and `flowmod` (`tableid` + `priority`).

## Minimal Quickstart (`openflow_quickstart.frasm`)

```frameassembly
host sdn_ctrl { ip 10.90.0.10 }
host ovs_br0  { ip 10.90.0.21 }

compile {
    sdn_ctrl -> ovs_br0 openflow hello xid 1 wait 1ms
    sdn_ctrl <- ovs_br0 openflow hello xid 1 wait 1ms
    sdn_ctrl -> ovs_br0 openflow featuresreq xid 2 wait 1ms
    sdn_ctrl <- ovs_br0 openflow featuresreply xid 2 dpid 0x0000001c73000001
}
```

### Automatic Wire Calculations

- Defaults to IANA OpenFlow port TCP `6653`, Version `4` (`0x04` = OpenFlow 1.3), and shares stateful 4-tuple TCP `seq`/`acknum` progression.
- Computes the exact 16-bit big-endian OpenFlow `length` field across both 8-byte header-only messages (`hello`, `featuresreq`, `echoreq`) and structured bodies (`32B` `featuresreply`, `56B` `flowmod`).
- Encodes the 64-bit switch Datapath ID (`dpid`) inside `OFPT_FEATURES_REPLY` (`type 6`) and `tableid` + `priority` inside `OFPT_FLOW_MOD` (`type 14`).

## Practical Lab Example: Complete OpenFlow 1.3 Switch Onboarding, Packet-In Miss & FlowMod Programming (`openflow_sdn_pipeline.frasm`)

Walks through TCP 6653 connection establishment, OpenFlow 1.3 `HELLO` + `FEATURES_REPLY`, a switch table-miss `PACKET_IN`, controller `FLOW_MOD` installation in Table 0, and `ECHO` heartbeat.

```frameassembly
host controller { ip 10.90.0.10 }
host leaf_ovs   { ip 10.90.0.51 }

compile {
    // 1. TCP handshake on port 6653
    leaf_ovs -> controller tcp syn srcport 54653 dstport 6653 win 65535 mss 1460 wait 1ms
    leaf_ovs <- controller tcp syn ack win 65535 mss 1460 wait 1ms
    leaf_ovs -> controller tcp ack wait 1ms
    // 2. OpenFlow 1.3 (version 4) Hello & Features negotiation
    leaf_ovs -> controller openflow hello version 4 xid 10 wait 1ms
    leaf_ovs <- controller openflow hello version 4 xid 10 wait 1ms
    leaf_ovs <- controller openflow featuresreq version 4 xid 11 wait 1ms
    leaf_ovs -> controller openflow featuresreply version 4 xid 11 dpid 0x0000001c73aa0051 wait 3ms
    // 3. Table-miss PacketIn triggers FlowMod rule installation + PacketOut
    leaf_ovs -> controller openflow packetin version 4 xid 0 tableid 0 wait 2ms
    leaf_ovs <- controller openflow flowmod version 4 xid 12 tableid 0 priority 1000 wait 1ms
    leaf_ovs <- controller openflow packetout version 4 xid 13 wait 5ms
    // 4. Liveness Echo Request / Reply
    leaf_ovs <- controller openflow echoreq version 4 xid 99 wait 1ms
    leaf_ovs -> controller openflow echoreply version 4 xid 99
}
```

```bash
# 1. Eyeball relative timestamps, ports & wire lengths in your terminal
frameassembly show openflow_sdn_pipeline.frasm

# 2. Compile .frasm -> classic .pcap (tcpdump/tcpreplay/Suricata) or nanosecond .pcapng
frameassembly openflow_sdn_pipeline.frasm openflow_sdn_pipeline.pcap
frameassembly openflow_sdn_pipeline.frasm openflow_sdn_pipeline.pcapng

# 3. Round-trip .pcap <-> .frasm whenever you want to scrub or tweak an existing capture
frameassembly reverse openflow_sdn_pipeline.pcap openflow_sdn_pipeline_edited.frasm

# 4. Verify dissected protocol fields in tshark
tshark -r openflow_sdn_pipeline.pcap
```

## All `openflow` Keywords & Header Options

Packet line pattern: `src_host -> dst_host openflow [hello | echoreq | echorequest | echoreply | featuresreq | featuresreply | packetin | packetout | flowmod | type <0..255>] [version <0..255>] [xid <number>] [dpid <number>] [tableid <0..255>] [priority <0..65535>]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `hello \| echoreq \| echorequest \| echoreply \| featuresreq \| featuresreply \| packetin \| packetout \| flowmod \| type <0..255>` | OpenFlow message type keyword or u8 | `hello (Type 0)` | Maps to OpenFlow 1.3 types: `hello` (`0`), `echoreq`/`echorequest` (`2`), `echoreply` (`3`), `featuresreq` (`5`), `featuresreply` (`6`), `packetin` (`10`), `packetout` (`13`), `flowmod` (`14`). |
| `version <0..255>` | 8-bit wire version code | `4 (OpenFlow 1.3)` | Sets wire protocol version (`1` = OF 1.0, `4` = OF 1.3, `5` = OF 1.4, `6` = OF 1.5). |
| `xid <number>` | 32-bit Transaction ID | `1` | Correlates controller requests (`featuresreq`, `echoreq`) with switch replies. |
| `dpid <number>` | Whole number (0x hex recommended) | `1` | Sets the 8-byte Datapath ID in `featuresreply` (`upper 16 bits` vendor/ID + `lower 48 bits` switch MAC). |
| `tableid <0..255> \| priority <0..65535>` | 8-bit Table ID, 16-bit Flow Priority | `tableid 0, priority 100` | Populates pipeline Table ID (`flowmod` / `packetin`) and rule match Priority (`flowmod`). |

## Cross-Layer Options Valid with `openflow`

| Option | Range / Format | Default | Effect |
| --- | --- | --- | --- |
| `wait <number><ns\|us\|ms\|s\|m>` | Time gap (ns, us, ms, s, m) | `10ms` | Advances the packet timestamp clock after sending this packet and before the next packet (`wait 0ns` sends back-to-back). |
| `payload "<text>" \| hexpayload "<hex>"` | Quoted text or hex bytes (choose one) | `Omitted` | Appends or overrides raw payload bytes (`"deadbeef"` or `"de:ad:be:ef"`) and updates packet lengths and checksums. |
| `srcmac <mac> \| dstmac <mac> \| ethertype <0..65535>` | Colon/hyphen/Cisco MAC · EtherType u16 (e.g. 0x0800) | `Host MAC / RFC multicast MAC / auto EtherType` | Overrides the 14-byte Ethernet II source MAC, destination MAC, or 16-bit EtherType for this single packet. |
| `vlan <0..4095> [pcp <0..7>] [dei <0\|1>]` | VLAN ID 0..4095, PCP 0..7, DEI 0..1 | `Untagged` | Wraps the packet in an IEEE 802.1Q VLAN tag (`0x8100`) with optional 802.1p QoS priority. |
| `qinq <0..4095> [innervlan <0..4095>]` | Outer S-VLAN 0..4095, Inner C-VLAN 0..4095 | `Untagged (innervlan 1 when qinq is set)` | Double-tags the packet with outer IEEE 802.1ad S-Tag (`0x88a8`) plus inner IEEE 802.1Q C-Tag (`0x8100`). |
| `mpls <0..1048575> [label <0..1048575>] [exp\|tc <0..7>] [bos <0\|1>] [mplsttl <0..255>]` | Label 0..1048575, exp/tc 0..7, bos 0..1, ttl 0..255 | `Unlabeled (at most 1 mpls tag per packet)` | Inserts a 32-bit MPLS label header (`0x8847`) ahead of the Layer 3 header. |
| `ttl <0..255> [dscp <0..63>] [ecn <0..3>]` | TTL/Hop Limit 0..255, DSCP 0..63, ECN 0..3 | `ttl 64, dscp 0, ecn 0` | Sets IPv4 TTL or IPv6 Hop Limit plus DiffServ (DSCP) and ECN QoS markings. |
| `ipid <0..65535> [df] [mf] [fragoffset <0..8191>]` | IPv4 ID, DF/MF flags, fragment offset 0..8191 | `ipid 1, flags 0, fragoffset 0` | Sets IPv4 Identification, Do-Not-Fragment (`df`), More-Fragments (`mf`), and fragment offset (ignored over IPv6). |
| `flowlabel <0..1048575>` | 20-bit IPv6 Flow Label (0..1048575) | `0` | Sets the 20-bit IPv6 Flow Label for ECMP routing tests (ignored when running over IPv4 hosts). |
| `srcport <0..65535> \| dstport <0..65535>` | Port number 0..65535 | `srcport 49152 (remembered), TCP 6653` | Overrides source or destination port and remembers the port pair automatically on `<-` replies. |
| `seq <number> \| acknum <number> \| win\|window <0..65535> \| urgent <0..65535> \| syn \| ack \| psh \| fin \| rst \| urg \| ece \| cwr` | TCP sequence/ack numbers, window size, flags & RFC 7323 options | `psh ack, win 65535, automatic TCP seq/acknum` | Overrides automatic TCP sequence tracking, flags, or RFC 7323 options (`mss`, `wscale`, `sackperm`, `tsval`, `tsecr`). |

## Common Mistakes & Quick Fixes

### Wire Version Numbers Differ From Marketing Version Strings

- **Avoid:** `ctrl -> sw openflow hello version "1.3"`
- **Valid v3.2.2 Syntax:** `ctrl -> sw openflow hello version 4   // 0x04 = OpenFlow 1.3`

On the wire, OpenFlow 1.0 is `version 1`, OpenFlow 1.3 is `version 4`, and OpenFlow 1.4 is `version 5`.

### Legacy Port 6633 vs IANA Standardized Port 6653

- **Valid v3.2.2 Syntax:** `ctrl -> sw openflow hello dstport 6633`

FrameAssembly defaults to IANA port `6653`; add `dstport 6633` when testing older OpenFlow 1.0 controllers.
