The idea: count conversations, not packets
An operator wants to know who uses a link, how much, and whether anything looks wrong. Keeping every packet is far too much data. Flow monitoring keeps a summary instead. The router groups packets into flows, counts the packets and bytes of each flow, and when a flow is over it sends one small flow record to a collector. Cisco called this NetFlow; the IETF standard version is IPFIX.
The page has one router, R1 (192.0.2.1), with a flow monitor on both interfaces: Gi0/0
(ifIndex 1, the LAN 192.0.2.0/24) and Gi0/1 (ifIndex 2, the uplink). The collector is
198.51.100.10. All addresses are documentation ranges. The clock is in seconds and moves with the
traffic or with Advance 15 s.
The flow key
Two packets belong to the same flow when they have the same key. Classic NetFlow uses 7 key fields: the 5-tuple (source IP, destination IP, source port, destination port, protocol), the ToS byte, and the input interface. Change any one of them and it is a different flow. So:
- A flow is one-way. A TCP connection is two flows: client to server, and server to client. They also enter on different interfaces.
- A video call marked ToS
0xB8(the voice and video class) and the same 5-tuple with ToS 0 would be two flows. That lets the operator see how much traffic each class carries.
The flow cache
For each packet the router looks the key up in its flow cache. If there is an entry, it adds 1
to the packet counter and the packet's length to the byte counter, sets last to now, and ORs the packet's
TCP flags into the entry. If there is none, it creates one with first = now. The flags column uses the
nfdump style UAPRSF, with a dot for each flag never seen.
Demo 1: Web connection. The client 192.0.2.10:49152 talks to
203.0.113.80:443 for 3 seconds. The client-to-server entry ends with 4 packets, 584 bytes; the
server-to-client entry with 4 packets, 3,112 bytes. Both show flags .AP.SF (0x1b): this
connection had a SYN, data, ACKs and a FIN, which is what a normal TCP flow looks like.
When a record leaves the cache
A record is only useful once it reaches the collector, and the cache is small, so entries must leave. There are
four reasons. IPFIX writes the reason into each record as flowEndReason.
| Reason | Rule on the page | flowEndReason |
|---|---|---|
| end of flow | a TCP FIN or RST is seen: export at once | 3 |
| inactive (idle) timeout | no packet for 15 s | 1 |
| active timeout | the entry is 60 s old, even if still busy | 2 |
| cache full | no free entry: the one idle longest goes | 5 |
UDP has no FIN, so a DNS lookup only ends by the inactive timeout (Demo 2: two records, 74 and 138 bytes, exported at t = 15 s). A long transfer would stay in the cache for hours and the collector would see nothing until it ended. The active timeout cuts it into pieces so the collector sees the traffic while it flows (Demo 3: Long backup).
On Cisco routers these are cache timeout inactive 15 and cache timeout active, whose
default is 1800 s (30 minutes). The page uses 60 s so you can watch it.
NetFlow v9 and IPFIX: templates
Records are sent in UDP messages, usually to port 2055 for NetFlow v9 and 4739 for IPFIX. Each message can hold many records. Both formats are template based: the router first sends a template (template ID 256 here) that lists the fields of a record and their order. A data record is then just numbers in that order. The collector can't decode data until it has the template, so the router sends it in the first message and repeats it from time to time.
| Field | NetFlow v9 name (type) | IPFIX name (element ID) |
|---|---|---|
| source address | IPV4_SRC_ADDR (8) | sourceIPv4Address (8) |
| destination port | L4_DST_PORT (11) | destinationTransportPort (11) |
| protocol | PROTOCOL (4) | protocolIdentifier (4) |
| ToS | SRC_TOS (5) | ipClassOfService (5) |
| input interface | INPUT_SNMP (10) | ingressInterface (10) |
| packets | IN_PKTS (2) | packetDeltaCount (2) |
| bytes | IN_BYTES (1) | octetDeltaCount (1) |
| TCP flags | TCP_FLAGS (6) | tcpControlBits (6) |
| start, end | FIRST_SWITCHED (22), LAST_SWITCHED (21) | flowStartSeconds (150), flowEndSeconds (151) |
| why it ended | (none) | flowEndReason (136) |
IPFIX (RFC 7011) grew out of v9 (RFC 3954): the header says version 10 instead of 9, the template set has ID 2
instead of 0, and the sequence number counts data records instead of messages. In Demo 7 the same DNS
records go out as IPFIX. The interface numbers are the same ifIndex values that
SNMP reports, so a collector can name the interfaces.
Sampling: count some packets, multiply
A fast link carries millions of packets per second, too many to look up one by one. With 1-in-N
sampling the router looks at only some packets: here each packet is kept with chance 1/10 (random
sampling). The cache counts only the kept packets. The router tells the collector the rate in an options record
(SAMPLING_INTERVAL in v9, samplingPacketInterval in IPFIX), and the collector multiplies
the counters by N.
Demo 5 sends the same office traffic as Demo 4, with the sampler on. The sampler keeps 82 of the 848 packets, and the collector multiplies each record by 10:
| Source | Packets kept | Estimate (× 10) | Exact | Error |
|---|---|---|---|---|
| 192.0.2.10, cloud sync | 49 | 490 pkts, 612,500 B | 480 pkts, 600,000 B | +2% |
| 192.0.2.20, web upload | 21 | 210 pkts, 210,000 B | 215 pkts, 215,000 B | −2% |
| 192.0.2.30, video call | 12 | 120 pkts, 24,000 B | 150 pkts, 30,000 B | −20% |
| 192.0.2.40, DNS | 0 | no record at all | 3 pkts, 222 B | gone |
The ranking of the top talkers is still right, but the DNS lookup has vanished.
The rule of thumb: a flow with k sampled packets has an error of about 1/√k. Big flows are measured well; flows of a few packets are a coin toss. That is fine for capacity planning, but it hides exactly the small flows a scan is made of. The same trade-off, few counters and a bounded error, is behind the Count-Min Sketch.
Using the records: planning and defence
Records carry no payload, only who talked to whom, how much, when and with which flags. That is enough for two everyday jobs.
- Capacity planning. Add up bytes by source, destination or port to find the top talkers. In Demo 4, 192.0.2.10 sent 600,000 of the 845,222 bytes (71%). If the uplink is often full, this tells you whether to buy bandwidth or move one backup job.
- Spotting a scan. Normal TCP flows have several packets and flags like
.AP.SF. A port scan looks different: one source, many flows to different ports, each of one packet with only....S., and nothing back. In Demo 6 203.0.113.66 sends one SYN to 8 ports of 192.0.2.10; the collector's rule (5 or more such flows from one source) raises an alert. - A flood. A denial-of-service flood shows the opposite shape: many sources, one destination, and that destination's bytes jump far above its normal share. The same top-talkers view, grouped by destination, shows it.
Flow records tell you that something looks odd; they don't say what was inside. Deciding whether to block it is the job of a stateful firewall, and judging packets against signatures or a baseline is the job of an intrusion detection system.
See also the TCP handshake for the SYN, SYN-ACK and FIN packets the flows are made of, and TCP vs UDP for why a UDP flow has no end marker.
See also Link Aggregation with LACP for how a switch spreads flows over the member links of a port-channel.
What the page leaves out
Egress monitoring and double counting, IPv6 and MPLS keys, Flexible NetFlow's user-defined keys and non-key fields, NetFlow v5's fixed record format, template refresh timers and lost templates, export over SCTP or TCP, sequence-number gaps that reveal lost export packets, sFlow (which samples packet headers and sends them as they are), deterministic and hash-based sampling, flow aggregation at the collector, the time spent in the cache, and real detection systems, which learn baselines per host and per hour instead of one fixed rule.