# rocev2 — UDP 4791 InfiniBand BTH/RETH/AETH & Variant ICRC-32 (`rocev2`)

> **FrameAssembly v3.3.0** · Protocol 97 of 81 · Canonical URL: https://frameassembly-lang.dev/docs/protocols/rocev2

- **Full Protocol Name:** RDMA over Converged Ethernet v2 (InfiniBand Annex A17)
- **Keyword:** `rocev2`
- **OSI Layer & Transport:** Layer 7 · UDP 4791
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** IBTA RoCEv2 Annex A17, InfiniBand Arch Vol 1
- **Wireshark Display Filter:** `infiniband`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> UDP 4791 -> RoCEv2 (InfiniBand BTH)`

## Overview

AI GPU cluster fabrics live on UDP `4791` paired with `dscp 26` + `ecn 2` and `pfc`: `rocev2` packs the 12-byte InfiniBand BTH (`destqp`, `seqnum`), 16-byte RETH (`addr`, `key`, `count`), and 4-byte Invariant CRC (`crc`) automatically!

RDMA over Converged Ethernet v2 (`rocev2`, IBTA Annex A17) powers **AI/LLM GPU training fabrics (NCCL collective all-reduce)**, **NVMe-oF/RDMA storage**, and ultra-low-latency HPC clusters by encapsulating **InfiniBand Base Transport Headers (BTH)** inside **IPv4/IPv6 + UDP port `4791`**. Every RoCEv2 packet carries a 12-byte **BTH** (`OpCode`, `Solicited/Migrate/Pad`, `Partition Key pkey`, 24-bit `Destination Queue Pair destqp`, `Acknowledge Request ackreq`, and 24-bit `Packet Sequence Number seqnum`), optional **RETH** (`64-bit Virtual Address addr`, `32-bit R_Key key`, `32-bit DMA Length count`) or **AETH** (`Syndrome/Status`, `24-bit Message SeqNum msgid`), payload, and a trailing **4-byte Invariant CRC (`ICRC`)**.

FrameAssembly computes the InfiniBand Annex A17 **RoCEv2 ICRC** (masking mutable IP TTL/DSCP/ECN, UDP checksum, and BTH Resv8a bits to `1`s per spec!) automatically and lets you pair `rocev2` with `dscp 26 ecn 3` (CE) and Layer 2 `pfc` pause frames for end-to-end DCQCN lossless fabric labs.

## Minimal Quickstart (`rocev2_quickstart.frasm`)

```frameassembly
host gpu_node_01 { ip 10.90.3.11 }
host gpu_node_02 { ip 10.90.3.12 }

compile {
    gpu_node_01 -> gpu_node_02 rocev2 write pkey 0xffff destqp 0x000104 ackreq seqnum 1000 addr 0x7f8000100000 key 0xa1b2c3d4 count 4096 dscp 26 ecn 2 payload "NCCL-ALLREDUCE-SHARD" wait 50us
    gpu_node_01 <- gpu_node_02 rocev2 opcode "ack" pkey 0xffff destqp 0x000103 seqnum 1000 status 0x00 msgid 1 dscp 26 ecn 2
}
```

### Automatic Wire Calculations

- Defaults to UDP destination port `4791` (`srcport` entropy for ECMP hashing) and writes the 12-byte InfiniBand Base Transport Header (`BTH`).
- Attaches the 16-byte RDMA Extended Transport Header (`RETH`: `va`, `rkey`, `dma_len`) on RC RDMA `write` (`0x0a`) / `read` (`0x0c`), or the 4-byte `AETH` (`syndrome`, `msn`) on `ack` (`0x11`) / Read Responses.
- Computes the genuine IEEE 802.3 CRC-32 **IBTA Annex A17 Invariant CRC (`ICRC`)** over the masked pseudo-IP + UDP + BTH + payload automatically (unless `crc` is overridden).

## Practical Lab Example: AI Training Cluster DCQCN Loop: ECN-Marked RDMA Write, RoCEv2 CNP (0x81) & RC ACK (`rocev2_dcqcn_gpu_fabric.frasm`)

Captures a GPU node sending an RDMA Write (`ECN = CE 3` marked by a congested spine switch), the receiver returning a RoCEv2 Congestion Notification Packet (`opcode 0x81` on QP `1` / data QP) plus RC `ACK`, followed by a paced RDMA Read.

```frameassembly
host h100_rank0 { ip 10.192.0.10 mac 0c:42:a1:00:00:10 }
host h100_rank1 { ip 10.192.0.11 mac 0c:42:a1:00:00:11 }

compile {
    // 1. RC RDMA Write Only (0x0a) on CoS 3 / DSCP 26 experiencing switch ECN Congestion Experienced (ecn 3)
    h100_rank0 -> h100_rank1 rocev2 write srcport 58100 pkey 0xffff destqp 0x000200 ackreq seqnum 5001 addr 0x00007f0040000000 key 0x11223344 count 65536 vlan 100 pcp 3 dscp 26 ecn 3 payload "FP16-GRADIENT-TENSOR" wait 20us
    // 2. Receiver immediately fires DCQCN Congestion Notification Packet (BTH OpCode 0x81 CNP, DSCP 48 CS6)
    h100_rank0 <- h100_rank1 rocev2 opcode 0x81 pkey 0xffff destqp 0x0001ff seqnum 0 vlan 100 pcp 6 dscp 48 wait 15us
    // 3. Receiver returns RC Acknowledge (OpCode 0x11, AETH ACK credit syndrome 0x00)
    h100_rank0 <- h100_rank1 rocev2 opcode "ack" pkey 0xffff destqp 0x0001ff seqnum 5001 status 0x00 msgid 42 vlan 100 pcp 3 dscp 26 ecn 2 wait 80us
    // 4. Rate-paced RC RDMA Read Request (OpCode 0x0c, ECN ECT(0) = 2)
    h100_rank0 -> h100_rank1 rocev2 read srcport 58100 pkey 0xffff destqp 0x000200 ackreq seqnum 5002 addr 0x00007f0040010000 key 0x11223344 count 4096 vlan 100 pcp 3 dscp 26 ecn 2
}
```

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

# 2. Compile .frasm -> classic .pcap, nanosecond .pcapng, or pipe via '-'
frameassembly rocev2_dcqcn_gpu_fabric.frasm rocev2_dcqcn_gpu_fabric.pcap
frameassembly rocev2_dcqcn_gpu_fabric.frasm rocev2_dcqcn_gpu_fabric.pcapng

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

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

## All `rocev2` Keywords & Header Options

Packet line pattern: `src_host -> dst_host rocev2 [read | write | opcode "<text_or_num>"] [pkey <0..65535>] [destqp <0..16777215>] [ackreq] [seqnum <0..16777215>] [addr <number>] [key <number>] [count <number>] [status <0..255>] [msgid <0..16777215>] [crc <number>]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `read \| write \| opcode "<text_or_num>"` | RC RDMA OpCode keyword ("send","write","read","ack","cnp") or 8-bit BTH OpCode (`0x04` Send, `0x0a` Write, `0x0c` Read, `0x11` ACK, `0x81` CNP) | `send (0x04 RC Send Only)` | Sets byte 0 of the 12-byte InfiniBand Base Transport Header (`BTH`) and selects optional `RETH` / `AETH` subheaders. |
| `pkey <0..65535>` | 16-bit Partition Key | `0xffff (Default full-member partition)` | Writes bytes 2..3 of the BTH. |
| `destqp <0..16777215>` | 24-bit Destination Queue Pair number | `0x000100` | Writes bytes 5..7 of the BTH identifying the remote HCA Queue Pair. |
| `ackreq \| seqnum <0..16777215>` | Acknowledge Request bit (`0x80`) & 24-bit Packet Sequence Number (`PSN`) | `ackreq off, seqnum 1` | Populates BTH bytes 8..11 (`A`-bit in MSB of byte 8 + 24-bit `PSN` in bytes 9..11). |
| `addr <number> \| key <number> \| count <number>` | 64-bit Virtual Address, 32-bit R_Key, and 32-bit DMA Length (`RETH`) | `addr 0, key 0, count 0` | Writes the 16-byte RDMA Extended Transport Header (`RETH`) on `write` and `read` requests. |
| `status <0..255> \| msgid <0..16777215>` | 8-bit AETH Syndrome (`0x00` ACK, `0x60` RNR NAK) & 24-bit Message Sequence Number | `status 0, msgid 1` | Writes the 4-byte ACK Extended Transport Header (`AETH`) on `ack` and RDMA Read Response packets. |
| `crc <number>` | Optional 32-bit Invariant CRC override | `Auto-computed IBTA Annex A17 ICRC` | Overrides the trailing 4-byte little-endian `ICRC` for negative NIC drop testing. |

## Cross-Layer Options Valid with `rocev2`

| 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). |
| `comment "<text>"` | Quoted UTF-8 annotation | `Omitted` | Attaches an Enhanced Packet Block `opt_comment` annotation when compiling to `.pcapng` (Wireshark `pkt_comment` filter). |
| `payload "<text>" \| hexpayload "<hex>"` | Quoted text or hex bytes (choose one) | `Omitted` | Appends or overrides raw payload bytes (`"deadbeef"`, `"de:ad:be:ef"`, or `"\x01\x02"`) and updates packet lengths and checksums. |
| `srcmac <mac> \| dstmac <mac> \| ethertype <0..65535>` | Colon/hyphen/Cisco MAC · EtherType 0..65535 (e.g. 0x0800) | `Host MAC / RFC multicast MAC / auto EtherType` | Overrides the 14-byte Ethernet II source MAC, destination MAC, or standalone Layer 2 16-bit EtherType. |
| `fcs \| badfcs` | Ethernet CRC-32 trailer flags (choose one) | `Omitted` | `fcs` appends a valid IEEE 802.3 4-byte CRC-32 Frame Check Sequence trailer; `badfcs` XOR-corrupts the CRC-32 for NIC/tap negative tests. |
| `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>]` | Repeatable label stack (0..1048575, exp/tc 0..7, bos 0..1, ttl 0..255) | `Unlabeled (auto bos=0 on outer labels, bos=1 on bottom)` | Prepends one or more 32-bit MPLS label shims (`0x8847`, e.g. `mpls 16000 mpls 24010`) ahead of Layer 2 EoMPLS or Layer 3 payloads. |
| `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>] [routeralert] [badipchecksum]` | IPv4 ID, DF/MF flags, offset 0..8191, Router Alert & checksum fault | `ipid 1, flags 0, fragoffset 0, valid RFC 1071 checksum` | Sets IPv4 Identification/fragmentation, injects RFC 2113 Router Alert (`20 04 00 00`), or corrupts the 16-bit IPv4 header checksum (`badipchecksum`). |
| `flowlabel <0..1048575>` | 20-bit IPv6 Flow Label (0..1048575) | `0` | Sets the 20-bit IPv6 Flow Label for ECMP routing tests (valid on IPv6 endpoint pairs only). |
| `srcport <0..65535> \| dstport <0..65535>` | Port number 0..65535 | `srcport 49152 (remembered), UDP 4791` | Overrides source or destination port and remembers the port pair automatically on `<-` replies. |
| `transport sctp` | Optional SCTP association transport (IP Proto 132) | `UDP (UDP 4791)` | Carries this Layer 7 PDU inside a stateful RFC 9260 SCTP `DATA` chunk with automatic Verification Tag, TSN tracking, and CRC-32c. |
| `badchecksum \| nochecksum` | L4 checksum fault or UDP zero-checksum flag | `Valid RFC checksum / CRC-32c` | `badchecksum` corrupts the L4 checksum for negative firewall/NIC offload tests; `nochecksum` writes `0x0000` in UDP headers. |

## Common Mistakes & Quick Fixes

### destqp and seqnum (PSN) Are 24-Bit Fields (0..16777215)

- **Avoid:** `n1 -> n2 rocev2 write destqp 0x01000000 seqnum 1`
- **Valid v3.3.0 Syntax:** `n1 -> n2 rocev2 write destqp 0x000200 seqnum 1`

InfiniBand BTH reserves the upper byte before both `Destination QP` and `PSN` (`A`-bit + reserved bits), capping `destqp` and `seqnum` at `0xffffff` (`16,777,215`).

### Pair RoCEv2 with ECN-Capable Transport Markings (ecn 2 or ecn 3)

- **Valid v3.3.0 Syntax:** `n1 -> n2 rocev2 write destqp 256 seqnum 1 dscp 26 ecn 2`

Production RoCEv2 lossless fabrics rely on IP header ECN bits (`ecn 2` `ECT(0)` in normal operation, `ecn 3` `CE` under congestion) to trigger DCQCN rate control before PFC buffer exhaustion.
