NG_NETFLOW(4)
π¦ NG_NETFLOW(4) β the netflow node, explained by your local Unix bestie
okay so this is a manpage from the Kernel Interfaces Manual and it's about ng_netflow, which is genuinely one of those things that sounds scary and then turns out to be iconic once someone explains it. that someone is me. you're welcome π
β¨ NAME β¨
ng_netflow β Cisco's NetFlow implementation π
β¨ SYNOPSIS β¨
#include <sys/types.h>
#include <netinet/in.h>
#include <netgraph/netflow/ng_netflow.h>
β¨ DESCRIPTION β¨
Bestie. Listen. The ng_netflow node implements Cisco's NetFlow export protocol on a router that's running FreeBSD. It literally just sits there listening to incoming traffic and figuring out which flows are unique in it. That's the whole job. Main character energy, honestly π
So how does it tell flows apart? A flow is distinguished by:
- endpoint IP addresses
- TCP/UDP port numbers
- ToS
- input interface
Expired flows get exported out of the node in NetFlow version 5/9 UDP datagrams. And why does a flow expire? Three reasons, and here's the list:
- An RST or FIN TCP segment. bye boo π
- Active timeout. Flows literally cannot live longer than the specified period of time. The default is
1800seconds (that's 30 minutes). - Inactive timeout. The flow was inactive for the specified period of time. The default is
15seconds.
The node also supports IPv6 accounting (NetFlow v9 only, no cap) and it's aware of multiple fibs. Different fibs are mapped to different domain_id in NetFlow V9, and different engine_id in NetFlow V5. it's giving organized β¨
π«Ά HOOKS π«Ά
This node type supports up to NG_NETFLOW_MAXIFACES hooks β and that default is 65536, yes really β named iface0, iface1, etc., plus the same number of hooks named out0, out1, etc. Oh and also two export hooks: export (for NetFlow version 5) and export9 (for NetFlow version 9). Export can happen simultaneously for all supported export hooks, because we contain multitudes.
By default (that's with ingress NetFlow enabled), the node does NetFlow accounting of data received on iface* hooks. If the corresponding out hook is connected, the unmodified data is bypassed to it β otherwise the data is just freed. π
If data is received on an out hook, it's bypassed to the corresponding iface hook without any processing (that's egress NetFlow, which is disabled by default).
When a full export datagram for an export protocol is built, it gets sent to the export or export9 hook. In normal operation, one (or more) export hook is connected to the inet/dgram/udp hook of the ng_ksocket(4) node. slay.
π CONTROL MESSAGES π
This node type supports the generic control messages, plus the following:
NGM_NETFLOW_INFO(info) β returns some node statistics and the current timeout values in astruct ng_netflow_info.NGM_NETFLOW_IFINFO(ifinfo) β returns information about theifaceNhook. The hook number is passed as an argument.NGM_NETFLOW_SETDLT(setdlt) β sets data link type on theifaceNhook. Currently the supported types areDLT_RAW(raw IP datagrams) andDLT_EN10MB(Ethernet).DLT_definitions can be found in the<net/bpf.h>header. Currently used values are1forDLT_EN10MBand12forDLT_RAW. This message type usesstruct ng_netflow_setdltas an argument:
struct ng_netflow_setdlt {
uint16_t iface; /* which iface dlt change */
uint8_t dlt; /* DLT_XXX from bpf.h */
};
heads up: the requested ifaceN hook must already be connected, otherwise the message send operation will return an error. no hook, no service π
NGM_NETFLOW_SETIFINDEX(setifindex) β okay so sometimesng_netflowmay be unable to determine the input interface index of a packet. This can happen if traffic enters theng_netflownode before it comes to the system interface's input queue. An example of such a setup is capturing traffic between a synchronous data line andng_iface(4). In this case, the input index should be associated with a given hook. The interface's index can be determined viaif_nametoindex(3)from userland. This message requiresstruct ng_netflow_setifindexas an argument:
struct ng_netflow_setifindex {
uint16_t iface; /* which iface index change */
uint16_t index; /* new index */
};
same rule bestie: the requested ifaceN hook must already be connected, otherwise the message send operation will return an error.
NGM_NETFLOW_SETTIMEOUTS(settimeouts) β sets values in seconds for the NetFlow active/inactive timeouts. This message requiresstruct ng_netflow_settimeoutsas an argument:
struct ng_netflow_settimeouts {
uint32_t inactive_timeout; /* flow inactive timeout */
uint32_t active_timeout; /* flow active timeout */
};
NGM_NETFLOW_SETCONFIG(setconfig) β sets configuration for the specified interface. This message requiresstruct ng_netflow_setconfigas an argument:
struct ng_netflow_setconfig {
uint16_t iface; /* which iface config change */
uint32_t conf; /* new config */
#define NG_NETFLOW_CONF_INGRESS 1
#define NG_NETFLOW_CONF_EGRESS 2
#define NG_NETFLOW_CONF_ONCE 4
#define NG_NETFLOW_CONF_THISONCE 8
#define NG_NETFLOW_CONF_NOSRCLOOKUP 16
#define NG_NETFLOW_CONF_NODSTLOOKUP 32
};
Configuration is a bitmask of several options. Let's go through them one by one, because this is the part everyone gets confused about and you're not gonna be everyone:
- Option
NG_NETFLOW_CONF_INGRESSis enabled by default and enables ingress NetFlow generation (for data coming from theifaceXhook). - Option
NG_NETFLOW_CONF_EGRESSenables egress NetFlow (for data coming from theoutXhook). - Option
NG_NETFLOW_CONF_ONCEdefines that a packet should be accounted only once if it several times passes via the netflow node. - Option
NG_NETFLOW_CONF_THISONCEdefines that a packet should be accounted only once if it several times passes via exactly this netflow node. - These two options are important to avoid duplicate accounting when both ingress and egress NetFlow are enabled. fr fr, double counting is not the vibe.
- Option
NG_NETFLOW_CONF_NOSRCLOOKUPskips radix lookup on flow source address, which is used to fill in network mask. - Option
NG_NETFLOW_CONF_NODSTLOOKUPskips radix lookup on destination (which fills egress interface id, destination mask and gateway).
If one doesn't need the data provided by lookups, he/she can disable them, to reduce load on routers. efficiency queen πΈ
NGM_NETFLOW_SETTEMPLATE(settemplate) β sets various timeouts to announce data flow templates (NetFlow v9-specific). This message requiresstruct ng_netflow_settemplateas an argument:
struct ng_netflow_settemplate {
uint16_t time; /* max time between announce */
uint16_t packets; /* max packets between announce */
};
The value of the time field represents time in seconds to re-announce data templates. The value of the packets field represents the maximum packets count between re-announcing data templates.
NGM_NETFLOW_SETMTU(setmtu) β sets export interface MTU to build packets of specified size (NetFlow v9-specific). This message requiresstruct ng_netflow_setmtuas an argument:
struct ng_netflow_setemtu {
uint16_t mtu; /* MTU for packet */
};
The default is 1500 bytes. (yes the struct in the header is spelled ng_netflow_setemtu β that's not me, that's canon, iykyk π«Ά)
NGM_NETFLOW_SHOWβ this control message asks a node to dump the entire contents of the flow cache. Important: it is called fromflowctl(8), not directly fromngctl(8). respect the chain of command bestie.NGM_NETFLOW_V9INFO(v9info) β returns some NetFlow v9 related values in a:
struct ng_netflow_v9info {
uint16_t templ_packets; /* v9 template packets */
uint16_t templ_time; /* v9 template time */
uint16_t mtu; /* v9 MTU */
};
π SHUTDOWN π
This node shuts down upon receipt of a NGM_SHUTDOWN control message, or when all hooks have been disconnected. that's it. that's the whole shutdown policy. β¨
π EXAMPLES π
The simplest possible configuration is one Ethernet interface, where flow collecting is enabled. Look at her go:
/usr/sbin/ngctl -f- <<-SEQ
mkpeer fxp0: netflow lower iface0
name fxp0:lower netflow
connect fxp0: netflow: upper out0
mkpeer netflow: ksocket export inet/dgram/udp
msg netflow:export connect inet/10.0.0.1:4444
SEQ
And this is a more complicated example of a router with 2 NetFlow-enabled interfaces fxp0 and ng0. Note that the ng0: node in this example is connected to ng_tee(4) β the latter sends us a copy of IP packets, which we analyze and free. On fxp0: we do not use tee, but send packets back to either node. love that for us π
/usr/sbin/ngctl -f- <<-SEQ
# connect ng0's tee to iface0 hook
mkpeer ng0:inet netflow right2left iface0
name ng0:inet.right2left netflow
# set DLT to raw mode
msg netflow: setdlt { iface=0 dlt=12 }
# set interface index (5 in this example)
msg netflow: setifindex { iface=0 index=5 }
# Connect fxp0: to iface1 and out1 hook
connect fxp0: netflow: lower iface1
connect fxp0: netflow: upper out1
# Create ksocket node on export hook, and configure it
# to send exports to proper destination
mkpeer netflow: ksocket export inet/dgram/udp
msg netflow:export connect inet/10.0.0.1:4444
SEQ
π SEE ALSO π
setfib(2), netgraph(4), ng_ether(4), ng_iface(4), ng_ksocket(4), ng_tee(4), flowctl(8), ngctl(8)
B. Claise, Ed, Cisco Systems NetFlow Services Export Version 9, RFC 3954.
http://www.cisco.com/en/US/docs/ios/solutions_docs/netflow/nfwhite.html
π§ AUTHORS π§
The ng_netflow node type was written by Gleb Smirnoff <glebius@FreeBSD.org>, Alexander Motin <mav@FreeBSD.org>, and Alexander Chernikov <melifaro@ipfw.ru>. The initial code was based on ng_ipacct, written by Roman V. Palagin <romanp@unshadow.net>. absolute legends, all of them π
π BUGS π
Cache snapshot obtained via the NGM_NETFLOW_SHOW command may lack some percentage of entries under severe load. π
The ng_netflow node type does not fill in AS numbers. This is due to the lack of necessary information in the kernel routing table. However β plot twist β this information can be injected into the kernel from a routing daemon such as GNU Zebra. This functionality may become available in future releases. π
and that's the whole page bestie β¨ this beauty came out of the Debian corner of the world, dated December 10, 2012 β yes it's vintage, yes it still ate. go configure some flows, you've got this π¦
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- nixpkgs unstable: source
- Copyright (c) 2004-2005 Gleb Smirnoff <glebius@FreeBSD.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms