# sflow — XDR-Encoded Switch Packet Sampling on UDP 6343 (`sflow`)

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

- **Full Protocol Name:** sFlow Version 5 Switched & Routed Packet Sampling
- **Keyword:** `sflow`
- **OSI Layer & Transport:** Layer 7 · UDP 6343
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** sFlow v5 Specification
- **Wireshark Display Filter:** `sflow`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> UDP 6343 -> sFlow v5`

## Overview

Hardware ASICs love sFlow v5 on UDP `6343` because sampling 1-in-4096 frames takes zero stateful flow memory. `sflow` encodes both IPv4 (`1`) and IPv6 (`2`) XDR agent headers cleanly.

sFlow v5 (`sflow`) is the wire-speed packet sampling telemetry protocol built into Arista, Juniper, Cisco Nexus, HPE/Aruba, and SONiC data-center switches on **UDP port `6343`**. Instead of maintaining stateful flow caches on the switch ASIC, sFlow exports XDR-encoded packet header samples directly to collectors such as sFlow-RT, pmacct, Akvorado, and FastNetMon.

FrameAssembly constructs a valid XDR-aligned sFlow v5 datagram containing the Agent IP (`agentip`, automatically tagged as IPv4 type `1` or IPv6 type `2`), `subagentid`, datagram `seq`, switch `uptime`, and sampled Ethernet frame header records.

## Minimal Quickstart (`sflow_quickstart.frasm`)

```frameassembly
host tor_sw01  { ip 10.40.0.11 }
host sflow_col { ip 10.40.0.200 }

compile {
    tor_sw01 -> sflow_col sflow agentip 10.40.0.11 subagentid 1 seq 1 uptime 360000 samples 1
}
```

### Automatic Wire Calculations

- Defaults UDP destination port to `6343` and aligns all fields to 4-byte XDR boundaries per the sFlow v5 specification.
- Sets Agent Address Type to `1` (4-byte IPv4) or `2` (16-byte IPv6) automatically from `agentip`.
- Includes an Enterprise `0`, Format `1` Flow Sample containing a sampled Raw Packet Header record so Wireshark dissects the full sFlow tree.

## Practical Lab Example: Multi-Switch Leaf-Spine sFlow v5 Telemetry Stream (`sflow_leaf_spine.frasm`)

Simulates two leaf switches exporting sequential sFlow v5 datagrams (including an IPv6 loopback agent IP) to a central collector.

```frameassembly
host leaf_01   { ip 172.18.1.11 }
host leaf_02   { ip 172.18.1.12 }
host collector { ip 172.18.100.50 }

compile {
    leaf_01 -> collector sflow agentip 172.18.1.11 subagentid 0 seq 101 uptime 450000 samples 1 wait 2ms
    leaf_01 -> collector sflow agentip 172.18.1.11 subagentid 0 seq 102 uptime 451000 samples 1 wait 2ms
    leaf_02 -> collector sflow agentip 2001:db8:18::12 subagentid 1 seq 55 uptime 820000 samples 1
}
```

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

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

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

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

## All `sflow` Keywords & Header Options

Packet line pattern: `src_host -> dst_host sflow [agentip <ip>] [subagentid <number>] [seq <number>] [uptime <number>] [samples <number>]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `agentip <ip>` | IPv4 or IPv6 switch management/loopback IP | `Source host IP` | Sets the sFlow Agent Address (XDR Type `1` IPv4 or Type `2` IPv6) in the datagram header. |
| `subagentid <number>` | 32-bit Sub-Agent ID | `0` | Disambiguates multiple linecards or virtual switches sharing one `agentip`. |
| `seq <number>` | 32-bit datagram sequence number | `1` | Sets the sFlow v5 datagram sequence counter (collectors track gaps in `seq` to detect dropped UDP telemetry). |
| `uptime <number>` | 32-bit milliseconds since boot | `1000` | Sets `sysUpTime` in milliseconds inside the sFlow v5 header. |
| `samples <number>` | Sample count | `1` | Sets the number of sample records carried in the sFlow datagram. |

## Cross-Layer Options Valid with `sflow`

| 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), UDP 6343` | Overrides source or destination port and remembers the port pair automatically on `<-` replies. |

## Common Mistakes & Quick Fixes

### seq on sflow Sets the sFlow Datagram Counter (UDP Has No TCP seq)

- **Valid v3.2.2 Syntax:** `tor_sw01 -> sflow_col sflow agentip 10.40.0.11 seq 42`

Because sFlow runs over UDP `6343`, `seq` sets the 32-bit sFlow datagram sequence number rather than a TCP sequence number.

### uptime Is Measured in Milliseconds

- **Valid v3.2.2 Syntax:** `tor_sw01 -> sflow_col sflow uptime 600000`

In sFlow v5, `uptime 600000` represents 600 seconds (10 minutes) since the switch agent initialized.
