# nsh — RFC 8300 Service Function Chaining (SPI, SI & Context) (`nsh`)

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

- **Full Protocol Name:** Network Service Header (Service Function Chaining)
- **Keyword:** `nsh`
- **OSI Layer & Transport:** Layer 2 · EtherType 0x894f
- **Host Endpoint Rule:** MAC-Only or IP
- **Standards:** RFC 8300, RFC 7665
- **Wireshark Display Filter:** `nsh`
- **Wire Encapsulation:** `Ethernet II -> NSH (RFC 8300) (EtherType 0x894f)`

## Overview

Steer packets through NFV firewall and scrubber chains on `0x894f`: `nsh` packs the 24-bit `spi`, hop-decrementing `si`, 16-byte `mdtype 1` context, and enforces the 6-bit `ttl <= 63` rule at compile time.

Network Service Header (`nsh`, RFC 8300, `EtherType 0x894f`) is the IETF data-plane encapsulation for **Service Function Chaining (SFC)** across NFV firewalls, DPI scrubbers, carrier NATs, and SD-WAN service appliances. An NSH header combines a 4-byte Base Header (`version`, `oam` bit, 6-bit `ttl`, `length`, `mdtype`, `nextproto`), a 4-byte Service Path Header (24-bit Service Path Identifier `spi` + 8-bit Service Index `si`), and fixed 16-byte (`mdtype 1`) or variable TLV (`mdtype 2`) Context Headers.

FrameAssembly validates the 6-bit NSH `ttl` bound (`0..63`) at compile time, auto-computes the 6-bit NSH Length field in 4-byte words (`0x06` for MD Type 1, `0x02+` for MD Type 2), and maps symbolic `nextproto` names (`"ipv4"`, `"ipv6"`, `"ethernet"`, `"nsh"`, `"mpls"`).

## Minimal Quickstart (`nsh_quickstart.frasm`)

```frameassembly
host sff_ingress { mac 00:1c:73:50:00:01 }
host sf_firewall { mac 00:1c:73:50:00:10 }

compile {
    sff_ingress -> sf_firewall nsh spi 100 si 255 mdtype 1 nextproto "ipv4" context "00000001000000020000000300000004"
}
```

### Automatic Wire Calculations

- Sets Ethernet II EtherType to `0x894f` and bit-packs the 2-bit `version`, `oam` flag, 6-bit `ttl` (`default 63`), and 6-bit 32-bit-word `Length` field.
- Packs the 24-bit Service Path Identifier (`spi`) and 8-bit Service Index (`si`) into the 4-byte Service Path Header.
- Pads or sizes the Context Header to 16 bytes for `mdtype 1` (`Length = 6`) or 4-byte-aligned variable octets for `mdtype 2`.

## Practical Lab Example: Multi-Hop NFV Service Chain: Classifier -> Firewall SF -> IPS SF (SI 255 -> 253) (`nsh_sfc_chain.frasm`)

Traces a packet classified onto Service Path `spi 420` stepping through two Service Functions as `si` decrements from `255` to `254` to `253`, followed by an `oam` health check.

```frameassembly
host classifier { mac 00:1c:73:01:00:01 }
host sff_core   { mac 00:1c:73:01:00:02 }
host sf_ngfw    { mac 00:1c:73:01:00:11 }
host sf_ips     { mac 00:1c:73:01:00:12 }

compile {
    // 1. Classifier pushes NSH SPI 420, SI 255 with tenant ID in MD Type 1 context
    classifier -> sff_core nsh spi 420 si 255 ttl 63 mdtype 1 nextproto "ethernet" context "000001a4000000100000000000000000" wait 1ms
    // 2. SFF steers packet through NGFW (decrements SI -> 254) and IPS (SI -> 253)
    sff_core -> sf_ngfw nsh spi 420 si 255 ttl 62 mdtype 1 nextproto "ethernet" context "000001a4000000100000000000000000" wait 1ms
    sf_ngfw -> sff_core nsh spi 420 si 254 ttl 62 mdtype 1 nextproto "ethernet" context "000001a4000000100000000100000000" wait 1ms
    sff_core -> sf_ips nsh spi 420 si 254 ttl 61 mdtype 1 nextproto "ethernet" context "000001a4000000100000000100000000" wait 1ms
    sf_ips -> sff_core nsh spi 420 si 253 ttl 61 mdtype 1 nextproto "ethernet" context "000001a4000000100000000300000000" wait 2ms
    // 3. SFC OAM probe on MD Type 2 (variable-length context omitted, Length = 2)
    classifier -> sff_core nsh oam spi 420 si 255 ttl 63 mdtype 2 nextproto "nsh"
}
```

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

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

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

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

## All `nsh` Keywords & Header Options

Packet line pattern: `src_host -> dst_host nsh [version <0..3>] [oam] [ttl <0..63>] [mdtype <1|2>] [nextproto <0..255 | "ipv4" | "ipv6" | "ethernet" | "nsh" | "mpls">] [spi <0..16777215>] [si <0..255>] [context "<hex>"]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `version <0..3>` | 2-bit NSH version (0..3) | `0` | Sets the 2-bit RFC 8300 NSH Base Header Version (`0` standard). |
| `oam` | Boolean flag | `Unset (0)` | Sets the `O` (Operations, Administration, and Maintenance) bit in the NSH Base Header. |
| `ttl <0..63>` | 6-bit NSH Service Plane TTL (0..63) | `63` | Sets the 6-bit loop-prevention TTL inside the NSH Base Header (validated `<= 63` at compile time). |
| `mdtype <1\|2>` | Metadata Type (1 = Fixed 16B, 2 = Variable) | `1` | Selects Fixed 4x32-bit Context Header (`1`, `Length=6`) or Variable Context (`2`). |
| `nextproto <"ipv4"\|"ipv6"\|"ethernet"\|"nsh"\|"mpls"\|0..255>` | Protocol name or 8-bit code | `"ipv4" (0x01)` | Sets Next Protocol (`1`=IPv4, `2`=IPv6, `3`=Ethernet, `4`=NSH, `5`=MPLS). |
| `spi <0..16777215>` | 24-bit Service Path Identifier | `1` | Writes the big-endian 24-bit Service Path ID in bytes 4..6 of the NSH header. |
| `si <0..255>` | 8-bit Service Index | `255` | Writes the 8-bit Service Index hop counter in byte 7 of the NSH header. |
| `context "<hex>"` | Quoted hex bytes (16B on mdtype 1) | `16 zero bytes on mdtype 1` | Populates the NSH Mandatory Context Header (`mdtype 1`) or variable metadata (`mdtype 2`). |

## Cross-Layer Options Valid with `nsh`

| 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. |

## Common Mistakes & Quick Fixes

### NSH ttl Is Strictly 6 Bits (0..63, Never 64 or 255)

- **Avoid:** `sff1 -> sff2 nsh spi 100 ttl 64`
- **Valid v3.3.0 Syntax:** `sff1 -> sff2 nsh spi 100 ttl 63`

RFC 8300 allocates a 6-bit TTL field in the NSH Base Header (`max 63`), so `ttl 64` or `ttl 255` fails compile-time semantic validation on `nsh` statements.

### Use nextproto (Never nextheader) on NSH Frames

- **Avoid:** `sff1 -> sff2 nsh spi 100 nextheader 1`
- **Valid v3.3.0 Syntax:** `sff1 -> sff2 nsh spi 100 nextproto "ipv4"`

`nextproto` sets the NSH Base Header Next Protocol field (`"ipv4"`, `"ipv6"`, `"ethernet"`, `"nsh"`, `"mpls"`).
