# bfd — Sub-Second RFC 5880 Link Liveness on UDP 3784 (`bfd`)

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

- **Full Protocol Name:** Bidirectional Forwarding Detection Control Packet
- **Keyword:** `bfd`
- **OSI Layer & Transport:** Layer 3 · UDP 3784 (Multihop 4784, TTL 255)
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** RFC 5880, RFC 5881, RFC 5883
- **Wireshark Display Filter:** `bfd`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> BFD (UDP 3784 (Multihop 4784, TTL 255))`

## Overview

Waiting 180 seconds for BGP Hold Time to notice a dead fiber is painful — `bfd` fires `ttl 255` UDP `3784` control packets with microsecond `mintx`/`minrx` timers and discriminator handshakes.

Bidirectional Forwarding Detection (`bfd`, RFC 5880) provides sub-50ms forwarding-plane failure detection for BGP, OSPF, IS-IS, VRRP, and MPLS/VXLAN tunnels. Single-hop BFD (RFC 5881) runs over **UDP `dstport 3784`** with `srcport >= 49152` and mandatory **TTL / Hop Limit `255`**, while multihop BFD (RFC 5883) uses `dstport 4784` and Seamless BFD uses `dstport 7784`.

FrameAssembly constructs the exact 24-byte BFD v1 Control header—packing Version `1`, 5-bit Diagnostic (`diag`), 2-bit State (`state`), Detect Multiplier (`mult`), 32-bit `mydisc` / `yourdisc` session discriminators, and microsecond `mintx` / `minrx` intervals.

## Minimal Quickstart (`bfd_quickstart.frasm`)

```frameassembly
host rtr_leaf  { ip 172.16.0.1 }
host rtr_spine { ip 172.16.0.2 }

compile {
    rtr_leaf -> rtr_spine bfd state 3 mydisc 101 yourdisc 202 mintx 50000 minrx 50000 mult 3 wait 50ms
    rtr_leaf <- rtr_spine bfd state 3 mydisc 202 yourdisc 101 mintx 50000 minrx 50000 mult 3
}
```

### Automatic Wire Calculations

- Defaults UDP `srcport` to RFC 5881 range (`49152+`), `dstport` to `3784`, and IP `ttl` / Hop Limit to `255`.
- Bit-packs Version (`1`), `diag` (`0..31`), `state` (`0..3`), `mult` (default `3`), `Length = 24`, `mydisc`, `yourdisc`, `mintx` (`100000` us), `minrx` (`100000` us), and Echo RX interval.
- Validates `state` (`0..=3`) and `diag` (`0..=31`) bounds at compile time.

## Practical Lab Example: BFD 3-Way Session Bring-Up (Down -> Init -> Up) & Detection Timeout (`bfd_state_machine.frasm`)

Traces two routers negotiating a 50ms (`50000` us * `mult 3` = `150ms` detection) BFD session from `Down` (`1`) through `Init` (`2`) to `Up` (`3`), followed by a `diag 1` timeout.

```frameassembly
host pe_north { ip 10.255.1.1 }
host pe_south { ip 10.255.1.2 }

compile {
    // 1. Initial Down packet (state 1, yourdisc 0 = peer unknown, slow 1s timer)
    pe_north -> pe_south bfd state 1 diag 0 mydisc 0x1001 yourdisc 0 mintx 1000000 minrx 1000000 mult 3 dscp 48 wait 2ms
    // 2. Peer sees 0x1001 and transitions to Init (state 2)
    pe_north <- pe_south bfd state 2 diag 0 mydisc 0x2002 yourdisc 0x1001 mintx 50000 minrx 50000 mult 3 dscp 48 wait 2ms
    // 3. Both peers reach Up (state 3) at 50ms (50,000 us) intervals
    pe_north -> pe_south bfd state 3 diag 0 mydisc 0x1001 yourdisc 0x2002 mintx 50000 minrx 50000 mult 3 dscp 48 wait 50ms
    pe_north <- pe_south bfd state 3 diag 0 mydisc 0x2002 yourdisc 0x1001 mintx 50000 minrx 50000 mult 3 dscp 48 wait 150ms
    // 4. Link failure detected: state 1 (Down), diag 1 (Control Detection Time Expired)
    pe_north -> pe_south bfd state 1 diag 1 mydisc 0x1001 yourdisc 0x2002 mintx 1000000 minrx 1000000 mult 3 dscp 48
}
```

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

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

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

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

## All `bfd` Keywords & Header Options

Packet line pattern: `src_host -> dst_host bfd [state <0..3>] [diag <0..31>] [mydisc <number>] [yourdisc <number>] [mintx <number>] [minrx <number>] [mult <0..255>]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `state <0..3>` | 2-bit BFD session state (0..3) | `3 (Up)` | Sets session state: `0` = AdminDown, `1` = Down, `2` = Init, `3` = Up. |
| `diag <0..31>` | 5-bit RFC 5880 Diagnostic code (0..31) | `0 (No Diagnostic)` | Sets failure reason (`0` None, `1` Control Detection Time Expired, `3` Neighbor Signaled Session Down, `7` Administratively Down). |
| `mydisc <number> \| yourdisc <number>` | 32-bit non-zero / peer session discriminators | `mydisc 1, yourdisc 0` | Sets My Discriminator and Your Discriminator (`0` until learned from peer). |
| `mintx <number> \| minrx <number>` | Whole number (microseconds) | `100000 (100 ms)` | Sets Desired Min TX Interval and Required Min RX Interval in **microseconds** (`50000` = `50ms`, `1000000` = `1s`). |
| `mult <1..255>` | 8-bit Detect Multiplier | `3` | Sets the missed-packet multiplier (`Detection Time = mult * max(minrx, peer_mintx)`). |

## Cross-Layer Options Valid with `bfd`

| 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 255, 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), UDP 3784 (Multihop 4784, TTL 255)` | Overrides source or destination port and remembers the port pair automatically on `<-` replies. |

## Common Mistakes & Quick Fixes

### mintx and minrx Are Expressed in Microseconds (Not Milliseconds)

- **Avoid:** `r1 -> r2 bfd mintx 50 minrx 50   // 50 microseconds!`
- **Valid v3.2.2 Syntax:** `r1 -> r2 bfd mintx 50000 minrx 50000   // 50 milliseconds`

Per RFC 5880 §4.1, BFD timer fields are encoded in microseconds (`50000` = `50 ms`, `300000` = `300 ms`).

### Use dstport 4784 for Multihop BFD (RFC 5883)

- **Valid v3.2.2 Syntax:** `r1 -> r2 bfd dstport 4784 ttl 64 state 3 mydisc 10 yourdisc 20`

Single-hop BFD uses UDP `3784` with `ttl 255`; for multihop eBGP loopback BFD sessions, override `dstport 4784` and set an appropriate `ttl`.
