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.

Packets enter router R1 on Gi0/0 and Gi0/1. Inside R1, a flow cache holds one entry per key with packet and byte counters. Expired entries leave as flow records in a NetFlow v9 or IPFIX message over UDP to the collector 198.51.100.10, which shows a record list, top talkers and alerts.
Packets update the cache; expired entries travel as records to the collector.

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.

ReasonRule on the pageflowEndReason
end of flowa TCP FIN or RST is seen: export at once3
inactive (idle) timeoutno packet for 15 s1
active timeoutthe entry is 60 s old, even if still busy2
cache fullno free entry: the one idle longest goes5

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

A timeline from t = 0 to 120 s. The backup flow sends a burst every 12 s from t = 0 to 96. At t = 60 the active timeout exports record 1: 600 packets, 900,000 bytes. A new entry starts at t = 72; after the last burst at 96 there is no packet for 15 s, so the inactive timeout exports record 2 at t = 111: 300 packets, 450,000 bytes.
The active timeout cuts a long flow into records; the inactive timeout ends it.

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.

FieldNetFlow v9 name (type)IPFIX name (element ID)
source addressIPV4_SRC_ADDR (8)sourceIPv4Address (8)
destination portL4_DST_PORT (11)destinationTransportPort (11)
protocolPROTOCOL (4)protocolIdentifier (4)
ToSSRC_TOS (5)ipClassOfService (5)
input interfaceINPUT_SNMP (10)ingressInterface (10)
packetsIN_PKTS (2)packetDeltaCount (2)
bytesIN_BYTES (1)octetDeltaCount (1)
TCP flagsTCP_FLAGS (6)tcpControlBits (6)
start, endFIRST_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:

SourcePackets keptEstimate (× 10)ExactError
192.0.2.10, cloud sync49490 pkts, 612,500 B480 pkts, 600,000 B+2%
192.0.2.20, web upload21210 pkts, 210,000 B215 pkts, 215,000 B−2%
192.0.2.30, video call12120 pkts, 24,000 B150 pkts, 30,000 B−20%
192.0.2.40, DNS0no record at all3 pkts, 222 Bgone

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.
Left, a normal web connection: 2 flow records, 4 packets each, 584 and 3,112 bytes, flags .AP.SF. Right, a port scan: 8 flow records from 203.0.113.66 to ports 21, 22, 23, 25, 80, 443, 445 and 3389 of 192.0.2.10, 1 packet and 60 bytes each, flags ....S., no reply flows.
Same records, different shape: a scan is many one-packet SYN flows from one source.

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.

On the mind map

Open the Networking mind mapNetworkingLayers & links: frames on one local networkLayers & linksIP & routing: packets hop between networksIP & routingCore services: address, name and timeCore servicesTCP, UDP & sockets: end to end between two portsTCP, UDP & socketsHTTP & TLS: what a browser does per requestHTTP & TLSAPIs & push: how apps talk to serversAPIs & pushProxies & edge: boxes in the middle of the pathProxies & edgeContainers: virtual networks from Linux partsContainersReal-time media: voice and video over UDPReal-time mediaSecurity at the edge: filter traffic at the borderSecurity at the edgeDHCPDHCPDISCOVER, OFFER, REQUEST, ACK; renew at T1SNMP MIB and trapsSNMP MIB and trapsPoll to ask, trap to be told at onceNetFlow / IPFIXFlow = key; export on FIN or timeoutDNS resolutionDNS resolutionroot, TLD, authoritative; cached for TTLDNS over HTTPS / TLSDNS over HTTPS / TLSHides names from the path, not the resolverSMTP, IMAP and POP3SMTP, IMAP and POP3SMTP pushes to the MX; IMAP/POP3 pullDNSSEC chain of trustDNSSEC chain of trustparent's DS vouches for the child's KSKNTPNTP4 timestamps give clock offset and delayGPS TrilaterationGPS TrilaterationClock error is one more unknownOpen the Networking mind mapNetworkingaddress, name and timeCore servicesDHCPDHCPDISCOVER, OFFER, REQUEST, ACK; renew at T1SNMP MIB and trapsSNMP MIB and trapsPoll to ask, trap to be told at onceNetFlow / IPFIXFlow = key; export on FIN or timeoutDNS resolutionDNS resolutionroot, TLD, authoritative; cached for TTLDNS over HTTPS / TLSDNS over HTTPS / TLSHides names from the path, not the resolverSMTP, IMAP and POP3SMTP, IMAP and POP3SMTP pushes to the MX; IMAP/POP3 pullDNSSEC chain of trustDNSSEC chain of trustparent's DS vouches for the child's KSKNTPNTP4 timestamps give clock offset and delayGPS TrilaterationGPS TrilaterationClock error is one more unknown
This page on the Networking mind map · 83 pages in 10 branches · open it to see the whole map and test yourself in recall mode.