#ifndef _NG_NETFLOW_H_
#define _NG_NETFLOW_H_
#define NG_NETFLOW_NODE_TYPE "netflow"
#define NGM_NETFLOW_COOKIE 1365756954
#define NGM_NETFLOW_V9_COOKIE 1349865386
#define NG_NETFLOW_MAXIFACES USHRT_MAX
#define NG_NETFLOW_HOOK_DATA "iface"
#define NG_NETFLOW_HOOK_OUT "out"
#define NG_NETFLOW_HOOK_EXPORT "export"
#define NG_NETFLOW_HOOK_EXPORT9 "export9"
enum {
NGM_NETFLOW_INFO = 1|NGM_READONLY|NGM_HASREPLY,
NGM_NETFLOW_IFINFO = 2|NGM_READONLY|NGM_HASREPLY,
NGM_NETFLOW_SHOW = 3|NGM_READONLY|NGM_HASREPLY,
NGM_NETFLOW_SETDLT = 4,
NGM_NETFLOW_SETIFINDEX = 5,
NGM_NETFLOW_SETTIMEOUTS = 6,
NGM_NETFLOW_SETCONFIG = 7,
NGM_NETFLOW_SETTEMPLATE = 8,
NGM_NETFLOW_SETMTU = 9,
NGM_NETFLOW_V9INFO = 10|NGM_READONLY|NGM_HASREPLY,
};
struct ng_netflow_info {
uint64_t nfinfo_bytes;
uint64_t nfinfo_packets;
uint64_t nfinfo_bytes6;
uint64_t nfinfo_packets6;
uint64_t nfinfo_sbytes;
uint64_t nfinfo_spackets;
uint64_t nfinfo_sbytes6;
uint64_t nfinfo_spackets6;
uint64_t nfinfo_act_exp;
uint64_t nfinfo_inact_exp;
uint32_t nfinfo_used;
uint32_t nfinfo_used6;
uint32_t nfinfo_alloc_failed;
uint32_t nfinfo_export_failed;
uint32_t nfinfo_export9_failed;
uint32_t nfinfo_realloc_mbuf;
uint32_t nfinfo_alloc_fibs;
uint32_t nfinfo_inact_t;
uint32_t nfinfo_act_t;
};
#define NG_NETFLOW_INFO_TYPE { \
{ "IPv4 bytes", &ng_parse_uint64_type },\
{ "IPv4 packets", &ng_parse_uint64_type },\
{ "IPv6 bytes", &ng_parse_uint64_type },\
{ "IPv6 packets", &ng_parse_uint64_type },\
{ "IPv4 skipped bytes", &ng_parse_uint64_type },\
{ "IPv4 skipped packets", &ng_parse_uint64_type },\
{ "IPv6 skipped bytes", &ng_parse_uint64_type },\
{ "IPv6 skipped packets", &ng_parse_uint64_type },\
{ "Active expiries", &ng_parse_uint64_type },\
{ "Inactive expiries", &ng_parse_uint64_type },\
{ "IPv4 records used", &ng_parse_uint32_type },\
{ "IPv6 records used", &ng_parse_uint32_type },\
{ "Failed allocations", &ng_parse_uint32_type },\
{ "V5 failed exports", &ng_parse_uint32_type },\
{ "V9 failed exports", &ng_parse_uint32_type },\
{ "mbuf reallocations", &ng_parse_uint32_type },\
{ "fibs allocated", &ng_parse_uint32_type },\
{ "Inactive timeout", &ng_parse_uint32_type },\
{ "Active timeout", &ng_parse_uint32_type },\
{ NULL } \
}
struct ng_netflow_ifinfo {
uint32_t ifinfo_packets;
uint8_t ifinfo_dlt;
#define MAXDLTNAMELEN 20
uint16_t ifinfo_index;
uint32_t conf;
};
struct ng_netflow_setdlt {
uint16_t iface;
uint8_t dlt;
};
struct ng_netflow_setifindex {
uint16_t iface;
uint16_t index;
};
struct ng_netflow_settimeouts {
uint32_t inactive_timeout;
uint32_t active_timeout;
};
#define NG_NETFLOW_CONF_INGRESS 0x01
#define NG_NETFLOW_CONF_EGRESS 0x02
#define NG_NETFLOW_CONF_ONCE 0x04
#define NG_NETFLOW_CONF_THISONCE 0x08
#define NG_NETFLOW_CONF_NOSRCLOOKUP 0x10
#define NG_NETFLOW_CONF_NODSTLOOKUP 0x20
#define NG_NETFLOW_IS_FRAG 0x01
#define NG_NETFLOW_FLOW_FLAGS (NG_NETFLOW_CONF_NOSRCLOOKUP|\
NG_NETFLOW_CONF_NODSTLOOKUP)
struct ng_netflow_setconfig {
uint16_t iface;
uint32_t conf;
};
struct ng_netflow_settemplate {
uint16_t time;
uint16_t packets;
};
struct ng_netflow_setmtu {
uint16_t mtu;
};
struct ngnf_show_header {
u_char version;
uint32_t hash_id;
uint32_t list_id;
uint32_t nentries;
};
struct ng_netflow_v9info {
uint16_t templ_packets;
uint16_t templ_time;
uint16_t mtu;
};
struct flow_rec {
uint16_t flow_type;
uint16_t fib;
struct in_addr r_src;
struct in_addr r_dst;
union {
struct {
uint16_t s_port;
uint16_t d_port;
} dir;
uint32_t both;
} ports;
union {
struct {
u_char prot;
u_char tos;
uint16_t i_ifx;
} i;
uint32_t all;
} misc;
};
struct flow6_rec {
uint16_t flow_type;
uint16_t fib;
union {
struct in_addr r_src;
struct in6_addr r_src6;
} src;
union {
struct in_addr r_dst;
struct in6_addr r_dst6;
} dst;
union {
struct {
uint16_t s_port;
uint16_t d_port;
} dir;
uint32_t both;
} ports;
union {
struct {
u_char prot;
u_char tos;
uint16_t i_ifx;
} i;
uint32_t all;
} misc;
};
#define r_ip_p misc.i.prot
#define r_tos misc.i.tos
#define r_i_ifx misc.i.i_ifx
#define r_misc misc.all
#define r_ports ports.both
#define r_sport ports.dir.s_port
#define r_dport ports.dir.d_port
struct flow_entry_data {
uint16_t version;
struct flow_rec r;
struct in_addr next_hop;
uint16_t fle_o_ifx;
#define fle_i_ifx r.misc.i.i_ifx
uint8_t dst_mask;
uint8_t src_mask;
u_long packets;
u_long bytes;
long first;
long last;
uint16_t tcp_flags;
};
struct flow6_entry_data {
uint16_t version;
struct flow6_rec r;
union {
struct in_addr next_hop;
struct in6_addr next_hop6;
} n;
uint16_t fle_o_ifx;
#define fle_i_ifx r.misc.i.i_ifx
uint8_t dst_mask;
uint8_t src_mask;
u_long packets;
u_long bytes;
long first;
long last;
uint16_t tcp_flags;
};
#define NREC_AT_ONCE 1000
#define NREC6_AT_ONCE (NREC_AT_ONCE * sizeof(struct flow_entry_data) / \
sizeof(struct flow6_entry_data))
#define NGRESP_SIZE (sizeof(struct ngnf_show_header) + (NREC_AT_ONCE * \
sizeof(struct flow_entry_data)))
#define SORCVBUF_SIZE (NGRESP_SIZE + 2 * sizeof(struct ng_mesg))
#ifdef _KERNEL
struct flow_entry {
TAILQ_ENTRY(flow_entry) fle_hash;
struct flow_entry_data f;
};
struct flow6_entry {
TAILQ_ENTRY(flow_entry) fle_hash;
struct flow6_entry_data f;
};
#define NG_NETFLOW_IFINFO_TYPE { \
{ "packets", &ng_parse_uint32_type },\
{ "data link type", &ng_parse_uint8_type }, \
{ "index", &ng_parse_uint16_type },\
{ "conf", &ng_parse_uint32_type },\
{ NULL } \
}
#define NG_NETFLOW_SETDLT_TYPE { \
{ "iface", &ng_parse_uint16_type }, \
{ "dlt", &ng_parse_uint8_type }, \
{ NULL } \
}
#define NG_NETFLOW_SETIFINDEX_TYPE { \
{ "iface", &ng_parse_uint16_type }, \
{ "index", &ng_parse_uint16_type }, \
{ NULL } \
}
#define NG_NETFLOW_SETTIMEOUTS_TYPE { \
{ "inactive", &ng_parse_uint32_type }, \
{ "active", &ng_parse_uint32_type }, \
{ NULL } \
}
#define NG_NETFLOW_SETCONFIG_TYPE { \
{ "iface", &ng_parse_uint16_type }, \
{ "conf", &ng_parse_uint32_type }, \
{ NULL } \
}
#define NG_NETFLOW_SETTEMPLATE_TYPE { \
{ "time", &ng_parse_uint16_type }, \
{ "packets", &ng_parse_uint16_type }, \
{ NULL } \
}
#define NG_NETFLOW_SETMTU_TYPE { \
{ "mtu", &ng_parse_uint16_type }, \
{ NULL } \
}
#define NG_NETFLOW_V9INFO_TYPE { \
{ "v9 template packets", &ng_parse_uint16_type },\
{ "v9 template time", &ng_parse_uint16_type },\
{ "v9 MTU", &ng_parse_uint16_type },\
{ NULL } \
}
struct ng_netflow_iface {
hook_p hook;
hook_p out;
struct ng_netflow_ifinfo info;
};
typedef struct ng_netflow_iface *iface_p;
typedef struct ng_netflow_ifinfo *ifinfo_p;
struct netflow_export_item {
item_p item;
item_p item9;
struct netflow_v9_packet_opt *item9_opt;
};
struct fib_export {
uint32_t fib;
struct netflow_export_item exp;
struct mtx export_mtx;
struct mtx export9_mtx;
uint32_t flow_seq;
uint32_t flow9_seq;
uint32_t domain_id;
uint32_t templ_last_ts;
uint32_t templ_last_pkt;
uint32_t sent_packets;
struct netflow_v9_packet_opt *export9_opt;
};
typedef struct fib_export *fib_export_p;
struct netflow {
node_p node;
hook_p export;
hook_p export9;
struct callout exp_callout;
#define CACHESIZE (65536*16)
#define CACHELOWAT (CACHESIZE * 3/4)
#define CACHEHIGHWAT (CACHESIZE * 9/10)
uma_zone_t zone;
struct flow_hash_entry *hash;
#ifdef INET6
uma_zone_t zone6;
struct flow_hash_entry *hash6;
#endif
counter_u64_t nfinfo_bytes;
counter_u64_t nfinfo_packets;
counter_u64_t nfinfo_bytes6;
counter_u64_t nfinfo_packets6;
counter_u64_t nfinfo_sbytes;
counter_u64_t nfinfo_spackets;
counter_u64_t nfinfo_sbytes6;
counter_u64_t nfinfo_spackets6;
counter_u64_t nfinfo_act_exp;
counter_u64_t nfinfo_inact_exp;
uint32_t nfinfo_alloc_failed;
uint32_t nfinfo_export_failed;
uint32_t nfinfo_export9_failed;
uint32_t nfinfo_realloc_mbuf;
uint32_t nfinfo_alloc_fibs;
uint32_t nfinfo_inact_t;
uint32_t nfinfo_act_t;
fib_export_p *fib_data;
uint16_t maxfibs;
uint16_t templ_time;
uint16_t templ_packets;
#define NETFLOW_V9_MAX_FLOWSETS 2
u_char flowsets_count;
u_char flowset_records[NETFLOW_V9_MAX_FLOWSETS - 1];
uint16_t mtu;
struct netflow_v9_flowset_header
*v9_flowsets[NETFLOW_V9_MAX_FLOWSETS - 1];
struct ng_netflow_iface ifaces[NG_NETFLOW_MAXIFACES];
};
typedef struct netflow *priv_p;
struct flow_hash_entry {
struct mtx mtx;
TAILQ_HEAD(fhead, flow_entry) head;
};
#define ERROUT(x) { error = (x); goto done; }
#define MTAG_NETFLOW 1221656444
#define MTAG_NETFLOW_CALLED 0
#define m_pktlen(m) ((m)->m_pkthdr.len)
#define IP6VERSION 6
#define priv_to_fib(priv, fib) (priv)->fib_data[(fib)]
#define MILLIUPTIME(t) (((t) << 9) + \
((t) << 8) + \
((t) << 7) + \
((t) << 6) + \
((t) << 5) + \
((t) << 3))
void ng_netflow_cache_init(priv_p);
void ng_netflow_cache_flush(priv_p);
int ng_netflow_fib_init(priv_p priv, int fib);
void ng_netflow_copyinfo(priv_p, struct ng_netflow_info *);
void ng_netflow_copyv9info(priv_p, struct ng_netflow_v9info *);
callout_func_t ng_netflow_expire;
int ng_netflow_flow_add(priv_p, fib_export_p, struct ip *, caddr_t,
uint8_t, uint8_t, unsigned int);
int ng_netflow_flow6_add(priv_p, fib_export_p, struct ip6_hdr *, caddr_t,
uint8_t, uint8_t, unsigned int);
int ng_netflow_flow_show(priv_p, struct ngnf_show_header *req,
struct ngnf_show_header *resp);
void ng_netflow_v9_cache_init(priv_p);
void ng_netflow_v9_cache_flush(priv_p);
item_p get_export9_dgram(priv_p, fib_export_p,
struct netflow_v9_packet_opt **);
void return_export9_dgram(priv_p, fib_export_p, item_p,
struct netflow_v9_packet_opt *, int);
int export9_add(item_p, struct netflow_v9_packet_opt *,
struct flow_entry *);
int export9_send(priv_p, fib_export_p, item_p,
struct netflow_v9_packet_opt *, int);
#endif
#endif