xref: /dflybsd-src/sys/netgraph7/netflow/netflow.c (revision 023c80aedb3e2e509d65a897a1cbd48d14bcb206)
1b06ebda0SMatthew Dillon /*-
2b06ebda0SMatthew Dillon  * Copyright (c) 2004-2005 Gleb Smirnoff <glebius@FreeBSD.org>
3b06ebda0SMatthew Dillon  * Copyright (c) 2001-2003 Roman V. Palagin <romanp@unshadow.net>
4b06ebda0SMatthew Dillon  * All rights reserved.
5b06ebda0SMatthew Dillon  *
6b06ebda0SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
7b06ebda0SMatthew Dillon  * modification, are permitted provided that the following conditions
8b06ebda0SMatthew Dillon  * are met:
9b06ebda0SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
10b06ebda0SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
11b06ebda0SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
12b06ebda0SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in the
13b06ebda0SMatthew Dillon  *    documentation and/or other materials provided with the distribution.
14b06ebda0SMatthew Dillon  *
15b06ebda0SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16b06ebda0SMatthew Dillon  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17b06ebda0SMatthew Dillon  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18b06ebda0SMatthew Dillon  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19b06ebda0SMatthew Dillon  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20b06ebda0SMatthew Dillon  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21b06ebda0SMatthew Dillon  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22b06ebda0SMatthew Dillon  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23b06ebda0SMatthew Dillon  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24b06ebda0SMatthew Dillon  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25b06ebda0SMatthew Dillon  * SUCH DAMAGE.
26b06ebda0SMatthew Dillon  *
27b06ebda0SMatthew Dillon  * $SourceForge: netflow.c,v 1.41 2004/09/05 11:41:10 glebius Exp $
285a975a3dSMatthew Dillon  * $FreeBSD: src/sys/netgraph/netflow/netflow.c,v 1.29 2008/05/09 23:02:57 julian Exp $
29b06ebda0SMatthew Dillon  */
30b06ebda0SMatthew Dillon 
31b06ebda0SMatthew Dillon #include <sys/param.h>
32b06ebda0SMatthew Dillon #include <sys/kernel.h>
33b06ebda0SMatthew Dillon #include <sys/limits.h>
34b06ebda0SMatthew Dillon #include <sys/mbuf.h>
35b06ebda0SMatthew Dillon #include <sys/syslog.h>
36b06ebda0SMatthew Dillon #include <sys/systm.h>
37b06ebda0SMatthew Dillon #include <sys/socket.h>
38b06ebda0SMatthew Dillon 
39b06ebda0SMatthew Dillon #include <machine/atomic.h>
40b06ebda0SMatthew Dillon 
41b06ebda0SMatthew Dillon #include <net/if.h>
42b06ebda0SMatthew Dillon #include <net/route.h>
43b06ebda0SMatthew Dillon #include <netinet/in.h>
44b06ebda0SMatthew Dillon #include <netinet/in_systm.h>
45b06ebda0SMatthew Dillon #include <netinet/ip.h>
46b06ebda0SMatthew Dillon #include <netinet/tcp.h>
47b06ebda0SMatthew Dillon #include <netinet/udp.h>
48b06ebda0SMatthew Dillon 
495a975a3dSMatthew Dillon #include "ng_message.h"
505a975a3dSMatthew Dillon #include "netgraph.h"
51b06ebda0SMatthew Dillon 
525a975a3dSMatthew Dillon #include "netflow/netflow.h"
535a975a3dSMatthew Dillon #include "netflow/ng_netflow.h"
54b06ebda0SMatthew Dillon 
55b06ebda0SMatthew Dillon #define	NBUCKETS	(65536)		/* must be power of 2 */
56b06ebda0SMatthew Dillon 
57b06ebda0SMatthew Dillon /* This hash is for TCP or UDP packets. */
58b06ebda0SMatthew Dillon #define FULL_HASH(addr1, addr2, port1, port2)	\
59b06ebda0SMatthew Dillon 	(((addr1 ^ (addr1 >> 16) ^ 		\
60b06ebda0SMatthew Dillon 	htons(addr2 ^ (addr2 >> 16))) ^ 	\
61b06ebda0SMatthew Dillon 	port1 ^ htons(port2)) &			\
62b06ebda0SMatthew Dillon 	(NBUCKETS - 1))
63b06ebda0SMatthew Dillon 
64b06ebda0SMatthew Dillon /* This hash is for all other IP packets. */
65b06ebda0SMatthew Dillon #define ADDR_HASH(addr1, addr2)			\
66b06ebda0SMatthew Dillon 	((addr1 ^ (addr1 >> 16) ^ 		\
67b06ebda0SMatthew Dillon 	htons(addr2 ^ (addr2 >> 16))) &		\
68b06ebda0SMatthew Dillon 	(NBUCKETS - 1))
69b06ebda0SMatthew Dillon 
70b06ebda0SMatthew Dillon /* Macros to shorten logical constructions */
71b06ebda0SMatthew Dillon /* XXX: priv must exist in namespace */
72b06ebda0SMatthew Dillon #define	INACTIVE(fle)	(time_uptime - fle->f.last > priv->info.nfinfo_inact_t)
73b06ebda0SMatthew Dillon #define	AGED(fle)	(time_uptime - fle->f.first > priv->info.nfinfo_act_t)
74b06ebda0SMatthew Dillon #define	ISFREE(fle)	(fle->f.packets == 0)
75b06ebda0SMatthew Dillon 
76b06ebda0SMatthew Dillon /*
77b06ebda0SMatthew Dillon  * 4 is a magical number: statistically number of 4-packet flows is
78b06ebda0SMatthew Dillon  * bigger than 5,6,7...-packet flows by an order of magnitude. Most UDP/ICMP
79b06ebda0SMatthew Dillon  * scans are 1 packet (~ 90% of flow cache). TCP scans are 2-packet in case
80b06ebda0SMatthew Dillon  * of reachable host and 4-packet otherwise.
81b06ebda0SMatthew Dillon  */
82b06ebda0SMatthew Dillon #define	SMALL(fle)	(fle->f.packets <= 4)
83b06ebda0SMatthew Dillon 
84b06ebda0SMatthew Dillon /*
85b06ebda0SMatthew Dillon  * Cisco uses milliseconds for uptime. Bad idea, since it overflows
86b06ebda0SMatthew Dillon  * every 48+ days. But we will do same to keep compatibility. This macro
87b06ebda0SMatthew Dillon  * does overflowable multiplication to 1000.
88b06ebda0SMatthew Dillon  */
89b06ebda0SMatthew Dillon #define	MILLIUPTIME(t)	(((t) << 9) +	/* 512 */	\
90b06ebda0SMatthew Dillon 			 ((t) << 8) +	/* 256 */	\
91b06ebda0SMatthew Dillon 			 ((t) << 7) +	/* 128 */	\
92b06ebda0SMatthew Dillon 			 ((t) << 6) +	/* 64  */	\
93b06ebda0SMatthew Dillon 			 ((t) << 5) +	/* 32  */	\
94b06ebda0SMatthew Dillon 			 ((t) << 3))	/* 8   */
95b06ebda0SMatthew Dillon 
96b06ebda0SMatthew Dillon MALLOC_DECLARE(M_NETFLOW_HASH);
97b06ebda0SMatthew Dillon MALLOC_DEFINE(M_NETFLOW_HASH, "netflow_hash", "NetFlow hash");
98b06ebda0SMatthew Dillon 
99b06ebda0SMatthew Dillon static int export_add(item_p, struct flow_entry *);
100b06ebda0SMatthew Dillon static int export_send(priv_p, item_p, int flags);
101b06ebda0SMatthew Dillon 
102b06ebda0SMatthew Dillon /* Generate hash for a given flow record. */
103b06ebda0SMatthew Dillon static __inline uint32_t
104b06ebda0SMatthew Dillon ip_hash(struct flow_rec *r)
105b06ebda0SMatthew Dillon {
106b06ebda0SMatthew Dillon 	switch (r->r_ip_p) {
107b06ebda0SMatthew Dillon 	case IPPROTO_TCP:
108b06ebda0SMatthew Dillon 	case IPPROTO_UDP:
109b06ebda0SMatthew Dillon 		return FULL_HASH(r->r_src.s_addr, r->r_dst.s_addr,
110b06ebda0SMatthew Dillon 		    r->r_sport, r->r_dport);
111b06ebda0SMatthew Dillon 	default:
112b06ebda0SMatthew Dillon 		return ADDR_HASH(r->r_src.s_addr, r->r_dst.s_addr);
113b06ebda0SMatthew Dillon 	}
114b06ebda0SMatthew Dillon }
115b06ebda0SMatthew Dillon 
116b06ebda0SMatthew Dillon /* This is callback from uma(9), called on alloc. */
117b06ebda0SMatthew Dillon static int
118b06ebda0SMatthew Dillon uma_ctor_flow(void *mem, int size, void *arg, int how)
119b06ebda0SMatthew Dillon {
120b06ebda0SMatthew Dillon 	priv_p priv = (priv_p )arg;
121b06ebda0SMatthew Dillon 
122b06ebda0SMatthew Dillon 	if (atomic_load_acq_32(&priv->info.nfinfo_used) >= CACHESIZE)
123b06ebda0SMatthew Dillon 		return (ENOMEM);
124b06ebda0SMatthew Dillon 
125b06ebda0SMatthew Dillon 	atomic_add_32(&priv->info.nfinfo_used, 1);
126b06ebda0SMatthew Dillon 
127b06ebda0SMatthew Dillon 	return (0);
128b06ebda0SMatthew Dillon }
129b06ebda0SMatthew Dillon 
130b06ebda0SMatthew Dillon /* This is callback from uma(9), called on free. */
131b06ebda0SMatthew Dillon static void
132b06ebda0SMatthew Dillon uma_dtor_flow(void *mem, int size, void *arg)
133b06ebda0SMatthew Dillon {
134b06ebda0SMatthew Dillon 	priv_p priv = (priv_p )arg;
135b06ebda0SMatthew Dillon 
136b06ebda0SMatthew Dillon 	atomic_subtract_32(&priv->info.nfinfo_used, 1);
137b06ebda0SMatthew Dillon }
138b06ebda0SMatthew Dillon 
139b06ebda0SMatthew Dillon /*
140b06ebda0SMatthew Dillon  * Detach export datagram from priv, if there is any.
141b06ebda0SMatthew Dillon  * If there is no, allocate a new one.
142b06ebda0SMatthew Dillon  */
143b06ebda0SMatthew Dillon static item_p
144b06ebda0SMatthew Dillon get_export_dgram(priv_p priv)
145b06ebda0SMatthew Dillon {
146b06ebda0SMatthew Dillon 	item_p	item = NULL;
147b06ebda0SMatthew Dillon 
148b06ebda0SMatthew Dillon 	mtx_lock(&priv->export_mtx);
149b06ebda0SMatthew Dillon 	if (priv->export_item != NULL) {
150b06ebda0SMatthew Dillon 		item = priv->export_item;
151b06ebda0SMatthew Dillon 		priv->export_item = NULL;
152b06ebda0SMatthew Dillon 	}
153b06ebda0SMatthew Dillon 	mtx_unlock(&priv->export_mtx);
154b06ebda0SMatthew Dillon 
155b06ebda0SMatthew Dillon 	if (item == NULL) {
156b06ebda0SMatthew Dillon 		struct netflow_v5_export_dgram *dgram;
157b06ebda0SMatthew Dillon 		struct mbuf *m;
158b06ebda0SMatthew Dillon 
1595a975a3dSMatthew Dillon 		m = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
160b06ebda0SMatthew Dillon 		if (m == NULL)
161b06ebda0SMatthew Dillon 			return (NULL);
162b06ebda0SMatthew Dillon 		item = ng_package_data(m, NG_NOFLAGS);
163b06ebda0SMatthew Dillon 		if (item == NULL)
164b06ebda0SMatthew Dillon 			return (NULL);
165b06ebda0SMatthew Dillon 		dgram = mtod(m, struct netflow_v5_export_dgram *);
166b06ebda0SMatthew Dillon 		dgram->header.count = 0;
167b06ebda0SMatthew Dillon 		dgram->header.version = htons(NETFLOW_V5);
168b06ebda0SMatthew Dillon 
169b06ebda0SMatthew Dillon 	}
170b06ebda0SMatthew Dillon 
171b06ebda0SMatthew Dillon 	return (item);
172b06ebda0SMatthew Dillon }
173b06ebda0SMatthew Dillon 
174b06ebda0SMatthew Dillon /*
175b06ebda0SMatthew Dillon  * Re-attach incomplete datagram back to priv.
176b06ebda0SMatthew Dillon  * If there is already another one, then send incomplete. */
177b06ebda0SMatthew Dillon static void
178b06ebda0SMatthew Dillon return_export_dgram(priv_p priv, item_p item, int flags)
179b06ebda0SMatthew Dillon {
180b06ebda0SMatthew Dillon 	/*
181b06ebda0SMatthew Dillon 	 * It may happen on SMP, that some thread has already
182b06ebda0SMatthew Dillon 	 * put its item there, in this case we bail out and
183b06ebda0SMatthew Dillon 	 * send what we have to collector.
184b06ebda0SMatthew Dillon 	 */
185b06ebda0SMatthew Dillon 	mtx_lock(&priv->export_mtx);
186b06ebda0SMatthew Dillon 	if (priv->export_item == NULL) {
187b06ebda0SMatthew Dillon 		priv->export_item = item;
188b06ebda0SMatthew Dillon 		mtx_unlock(&priv->export_mtx);
189b06ebda0SMatthew Dillon 	} else {
190b06ebda0SMatthew Dillon 		mtx_unlock(&priv->export_mtx);
191b06ebda0SMatthew Dillon 		export_send(priv, item, flags);
192b06ebda0SMatthew Dillon 	}
193b06ebda0SMatthew Dillon }
194b06ebda0SMatthew Dillon 
195b06ebda0SMatthew Dillon /*
196b06ebda0SMatthew Dillon  * The flow is over. Call export_add() and free it. If datagram is
197b06ebda0SMatthew Dillon  * full, then call export_send().
198b06ebda0SMatthew Dillon  */
199b06ebda0SMatthew Dillon static __inline void
200b06ebda0SMatthew Dillon expire_flow(priv_p priv, item_p *item, struct flow_entry *fle, int flags)
201b06ebda0SMatthew Dillon {
202b06ebda0SMatthew Dillon 	if (*item == NULL)
203b06ebda0SMatthew Dillon 		*item = get_export_dgram(priv);
204b06ebda0SMatthew Dillon 	if (*item == NULL) {
205b06ebda0SMatthew Dillon 		atomic_add_32(&priv->info.nfinfo_export_failed, 1);
206b06ebda0SMatthew Dillon 		uma_zfree_arg(priv->zone, fle, priv);
207b06ebda0SMatthew Dillon 		return;
208b06ebda0SMatthew Dillon 	}
209b06ebda0SMatthew Dillon 	if (export_add(*item, fle) > 0) {
210b06ebda0SMatthew Dillon 		export_send(priv, *item, flags);
211b06ebda0SMatthew Dillon 		*item = NULL;
212b06ebda0SMatthew Dillon 	}
213b06ebda0SMatthew Dillon 	uma_zfree_arg(priv->zone, fle, priv);
214b06ebda0SMatthew Dillon }
215b06ebda0SMatthew Dillon 
216b06ebda0SMatthew Dillon /* Get a snapshot of node statistics */
217b06ebda0SMatthew Dillon void
218b06ebda0SMatthew Dillon ng_netflow_copyinfo(priv_p priv, struct ng_netflow_info *i)
219b06ebda0SMatthew Dillon {
220b06ebda0SMatthew Dillon 	/* XXX: atomic */
221b06ebda0SMatthew Dillon 	memcpy((void *)i, (void *)&priv->info, sizeof(priv->info));
222b06ebda0SMatthew Dillon }
223b06ebda0SMatthew Dillon 
224b06ebda0SMatthew Dillon /*
225b06ebda0SMatthew Dillon  * Insert a record into defined slot.
226b06ebda0SMatthew Dillon  *
227b06ebda0SMatthew Dillon  * First we get for us a free flow entry, then fill in all
228b06ebda0SMatthew Dillon  * possible fields in it.
229b06ebda0SMatthew Dillon  *
230b06ebda0SMatthew Dillon  * TODO: consider dropping hash mutex while filling in datagram,
231b06ebda0SMatthew Dillon  * as this was done in previous version. Need to test & profile
232b06ebda0SMatthew Dillon  * to be sure.
233b06ebda0SMatthew Dillon  */
234b06ebda0SMatthew Dillon static __inline int
235b06ebda0SMatthew Dillon hash_insert(priv_p priv, struct flow_hash_entry  *hsh, struct flow_rec *r,
236b06ebda0SMatthew Dillon 	int plen, uint8_t tcp_flags)
237b06ebda0SMatthew Dillon {
238b06ebda0SMatthew Dillon 	struct flow_entry *fle;
239b06ebda0SMatthew Dillon 	struct sockaddr_in sin;
240b06ebda0SMatthew Dillon 	struct rtentry *rt;
241b06ebda0SMatthew Dillon 
242a6c72860SNuno Antunes 	KKASSERT(mtx_owned(&hsh->mtx));
243b06ebda0SMatthew Dillon 
2445a975a3dSMatthew Dillon 	fle = uma_zalloc_arg(priv->zone, priv, M_WAITOK | M_NULLOK);
245b06ebda0SMatthew Dillon 	if (fle == NULL) {
246b06ebda0SMatthew Dillon 		atomic_add_32(&priv->info.nfinfo_alloc_failed, 1);
247b06ebda0SMatthew Dillon 		return (ENOMEM);
248b06ebda0SMatthew Dillon 	}
249b06ebda0SMatthew Dillon 
250b06ebda0SMatthew Dillon 	/*
251b06ebda0SMatthew Dillon 	 * Now fle is totally ours. It is detached from all lists,
252b06ebda0SMatthew Dillon 	 * we can safely edit it.
253b06ebda0SMatthew Dillon 	 */
254b06ebda0SMatthew Dillon 
255b06ebda0SMatthew Dillon 	bcopy(r, &fle->f.r, sizeof(struct flow_rec));
256b06ebda0SMatthew Dillon 	fle->f.bytes = plen;
257b06ebda0SMatthew Dillon 	fle->f.packets = 1;
258b06ebda0SMatthew Dillon 	fle->f.tcp_flags = tcp_flags;
259b06ebda0SMatthew Dillon 
260b06ebda0SMatthew Dillon 	fle->f.first = fle->f.last = time_uptime;
261b06ebda0SMatthew Dillon 
262b06ebda0SMatthew Dillon 	/*
263b06ebda0SMatthew Dillon 	 * First we do route table lookup on destination address. So we can
264b06ebda0SMatthew Dillon 	 * fill in out_ifx, dst_mask, nexthop, and dst_as in future releases.
265b06ebda0SMatthew Dillon 	 */
266b06ebda0SMatthew Dillon 	bzero(&sin, sizeof(sin));
267b06ebda0SMatthew Dillon 	sin.sin_len = sizeof(struct sockaddr_in);
268b06ebda0SMatthew Dillon 	sin.sin_family = AF_INET;
269b06ebda0SMatthew Dillon 	sin.sin_addr = fle->f.r.r_dst;
270b06ebda0SMatthew Dillon 	/* XXX MRT 0 as a default.. need the m here to get fib */
271b06ebda0SMatthew Dillon 	rt = rtalloc1_fib((struct sockaddr *)&sin, 0, RTF_CLONING, 0);
272b06ebda0SMatthew Dillon 	if (rt != NULL) {
273b06ebda0SMatthew Dillon 		fle->f.fle_o_ifx = rt->rt_ifp->if_index;
274b06ebda0SMatthew Dillon 
275b06ebda0SMatthew Dillon 		if (rt->rt_flags & RTF_GATEWAY &&
276b06ebda0SMatthew Dillon 		    rt->rt_gateway->sa_family == AF_INET)
277b06ebda0SMatthew Dillon 			fle->f.next_hop =
278b06ebda0SMatthew Dillon 			    ((struct sockaddr_in *)(rt->rt_gateway))->sin_addr;
279b06ebda0SMatthew Dillon 
280b06ebda0SMatthew Dillon 		if (rt_mask(rt))
281b06ebda0SMatthew Dillon 			fle->f.dst_mask = bitcount32(((struct sockaddr_in *)
282b06ebda0SMatthew Dillon 			    rt_mask(rt))->sin_addr.s_addr);
283b06ebda0SMatthew Dillon 		else if (rt->rt_flags & RTF_HOST)
284b06ebda0SMatthew Dillon 			/* Give up. We can't determine mask :( */
285b06ebda0SMatthew Dillon 			fle->f.dst_mask = 32;
286b06ebda0SMatthew Dillon 
287b06ebda0SMatthew Dillon 		RTFREE_LOCKED(rt);
288b06ebda0SMatthew Dillon 	}
289b06ebda0SMatthew Dillon 
290b06ebda0SMatthew Dillon 	/* Do route lookup on source address, to fill in src_mask. */
291b06ebda0SMatthew Dillon 	bzero(&sin, sizeof(sin));
292b06ebda0SMatthew Dillon 	sin.sin_len = sizeof(struct sockaddr_in);
293b06ebda0SMatthew Dillon 	sin.sin_family = AF_INET;
294b06ebda0SMatthew Dillon 	sin.sin_addr = fle->f.r.r_src;
295b06ebda0SMatthew Dillon 	/* XXX MRT 0 as a default  revisit.  need the mbuf for fib*/
296b06ebda0SMatthew Dillon 	rt = rtalloc1_fib((struct sockaddr *)&sin, 0, RTF_CLONING, 0);
297b06ebda0SMatthew Dillon 	if (rt != NULL) {
298b06ebda0SMatthew Dillon 		if (rt_mask(rt))
299b06ebda0SMatthew Dillon 			fle->f.src_mask = bitcount32(((struct sockaddr_in *)
300b06ebda0SMatthew Dillon 			    rt_mask(rt))->sin_addr.s_addr);
301b06ebda0SMatthew Dillon 		else if (rt->rt_flags & RTF_HOST)
302b06ebda0SMatthew Dillon 			/* Give up. We can't determine mask :( */
303b06ebda0SMatthew Dillon 			fle->f.src_mask = 32;
304b06ebda0SMatthew Dillon 
305b06ebda0SMatthew Dillon 		RTFREE_LOCKED(rt);
306b06ebda0SMatthew Dillon 	}
307b06ebda0SMatthew Dillon 
308b06ebda0SMatthew Dillon 	/* Push new flow at the and of hash. */
309b06ebda0SMatthew Dillon 	TAILQ_INSERT_TAIL(&hsh->head, fle, fle_hash);
310b06ebda0SMatthew Dillon 
311b06ebda0SMatthew Dillon 	return (0);
312b06ebda0SMatthew Dillon }
313b06ebda0SMatthew Dillon 
314b06ebda0SMatthew Dillon 
315b06ebda0SMatthew Dillon /*
316b06ebda0SMatthew Dillon  * Non-static functions called from ng_netflow.c
317b06ebda0SMatthew Dillon  */
318b06ebda0SMatthew Dillon 
319b06ebda0SMatthew Dillon /* Allocate memory and set up flow cache */
320b06ebda0SMatthew Dillon int
321b06ebda0SMatthew Dillon ng_netflow_cache_init(priv_p priv)
322b06ebda0SMatthew Dillon {
323b06ebda0SMatthew Dillon 	struct flow_hash_entry	*hsh;
324b06ebda0SMatthew Dillon 	int i;
325b06ebda0SMatthew Dillon 
326b06ebda0SMatthew Dillon 	/* Initialize cache UMA zone. */
327b06ebda0SMatthew Dillon 	priv->zone = uma_zcreate("NetFlow cache", sizeof(struct flow_entry),
328b06ebda0SMatthew Dillon 	    uma_ctor_flow, uma_dtor_flow, NULL, NULL, UMA_ALIGN_CACHE, 0);
329b06ebda0SMatthew Dillon 	uma_zone_set_max(priv->zone, CACHESIZE);
330b06ebda0SMatthew Dillon 
331b06ebda0SMatthew Dillon 	/* Allocate hash. */
332fc025606SSascha Wildner 	priv->hash = kmalloc(NBUCKETS * sizeof(struct flow_hash_entry),
333b06ebda0SMatthew Dillon 			     M_NETFLOW_HASH, M_WAITOK | M_ZERO);
334b06ebda0SMatthew Dillon 
335b06ebda0SMatthew Dillon 	/* Initialize hash. */
336b06ebda0SMatthew Dillon 	for (i = 0, hsh = priv->hash; i < NBUCKETS; i++, hsh++) {
337b06ebda0SMatthew Dillon 		mtx_init(&hsh->mtx, "hash mutex", NULL, MTX_DEF);
338b06ebda0SMatthew Dillon 		TAILQ_INIT(&hsh->head);
339b06ebda0SMatthew Dillon 	}
340b06ebda0SMatthew Dillon 
341b06ebda0SMatthew Dillon 	mtx_init(&priv->export_mtx, "export dgram lock", NULL, MTX_DEF);
342b06ebda0SMatthew Dillon 
343b06ebda0SMatthew Dillon 	return (0);
344b06ebda0SMatthew Dillon }
345b06ebda0SMatthew Dillon 
346b06ebda0SMatthew Dillon /* Free all flow cache memory. Called from node close method. */
347b06ebda0SMatthew Dillon void
348b06ebda0SMatthew Dillon ng_netflow_cache_flush(priv_p priv)
349b06ebda0SMatthew Dillon {
350b06ebda0SMatthew Dillon 	struct flow_entry	*fle, *fle1;
351b06ebda0SMatthew Dillon 	struct flow_hash_entry	*hsh;
352b06ebda0SMatthew Dillon 	item_p			item = NULL;
353b06ebda0SMatthew Dillon 	int i;
354b06ebda0SMatthew Dillon 
355b06ebda0SMatthew Dillon 	/*
356b06ebda0SMatthew Dillon 	 * We are going to free probably billable data.
357b06ebda0SMatthew Dillon 	 * Expire everything before freeing it.
358b06ebda0SMatthew Dillon 	 * No locking is required since callout is already drained.
359b06ebda0SMatthew Dillon 	 */
360b06ebda0SMatthew Dillon 	for (hsh = priv->hash, i = 0; i < NBUCKETS; hsh++, i++)
361b06ebda0SMatthew Dillon 		TAILQ_FOREACH_SAFE(fle, &hsh->head, fle_hash, fle1) {
362b06ebda0SMatthew Dillon 			TAILQ_REMOVE(&hsh->head, fle, fle_hash);
363b06ebda0SMatthew Dillon 			expire_flow(priv, &item, fle, NG_QUEUE);
364b06ebda0SMatthew Dillon 		}
365b06ebda0SMatthew Dillon 
366b06ebda0SMatthew Dillon 	if (item != NULL)
367b06ebda0SMatthew Dillon 		export_send(priv, item, NG_QUEUE);
368b06ebda0SMatthew Dillon 
369b06ebda0SMatthew Dillon 	uma_zdestroy(priv->zone);
370b06ebda0SMatthew Dillon 
371b06ebda0SMatthew Dillon 	/* Destroy hash mutexes. */
372b06ebda0SMatthew Dillon 	for (i = 0, hsh = priv->hash; i < NBUCKETS; i++, hsh++)
373b06ebda0SMatthew Dillon 		mtx_destroy(&hsh->mtx);
374b06ebda0SMatthew Dillon 
375b06ebda0SMatthew Dillon 	/* Free hash memory. */
376b06ebda0SMatthew Dillon 	if (priv->hash)
377fc025606SSascha Wildner 		kfree(priv->hash, M_NETFLOW_HASH);
378b06ebda0SMatthew Dillon 
379b06ebda0SMatthew Dillon 	mtx_destroy(&priv->export_mtx);
380b06ebda0SMatthew Dillon }
381b06ebda0SMatthew Dillon 
382b06ebda0SMatthew Dillon /* Insert packet from into flow cache. */
383b06ebda0SMatthew Dillon int
384b06ebda0SMatthew Dillon ng_netflow_flow_add(priv_p priv, struct ip *ip, iface_p iface,
385b06ebda0SMatthew Dillon 	struct ifnet *ifp)
386b06ebda0SMatthew Dillon {
387b06ebda0SMatthew Dillon 	register struct flow_entry	*fle, *fle1;
388b06ebda0SMatthew Dillon 	struct flow_hash_entry		*hsh;
389b06ebda0SMatthew Dillon 	struct flow_rec		r;
390b06ebda0SMatthew Dillon 	item_p			item = NULL;
391b06ebda0SMatthew Dillon 	int			hlen, plen;
392b06ebda0SMatthew Dillon 	int			error = 0;
393b06ebda0SMatthew Dillon 	uint8_t			tcp_flags = 0;
394b06ebda0SMatthew Dillon 
395b06ebda0SMatthew Dillon 	/* Try to fill flow_rec r */
396b06ebda0SMatthew Dillon 	bzero(&r, sizeof(r));
397b06ebda0SMatthew Dillon 	/* check version */
398b06ebda0SMatthew Dillon 	if (ip->ip_v != IPVERSION)
399b06ebda0SMatthew Dillon 		return (EINVAL);
400b06ebda0SMatthew Dillon 
401b06ebda0SMatthew Dillon 	/* verify min header length */
402b06ebda0SMatthew Dillon 	hlen = ip->ip_hl << 2;
403b06ebda0SMatthew Dillon 
404b06ebda0SMatthew Dillon 	if (hlen < sizeof(struct ip))
405b06ebda0SMatthew Dillon 		return (EINVAL);
406b06ebda0SMatthew Dillon 
407b06ebda0SMatthew Dillon 	r.r_src = ip->ip_src;
408b06ebda0SMatthew Dillon 	r.r_dst = ip->ip_dst;
409b06ebda0SMatthew Dillon 
410b06ebda0SMatthew Dillon 	/* save packet length */
411b06ebda0SMatthew Dillon 	plen = ntohs(ip->ip_len);
412b06ebda0SMatthew Dillon 
413b06ebda0SMatthew Dillon 	r.r_ip_p = ip->ip_p;
414b06ebda0SMatthew Dillon 	r.r_tos = ip->ip_tos;
415b06ebda0SMatthew Dillon 
416b06ebda0SMatthew Dillon 	/* Configured in_ifx overrides mbuf's */
417b06ebda0SMatthew Dillon 	if (iface->info.ifinfo_index == 0) {
418b06ebda0SMatthew Dillon 		if (ifp != NULL)
419b06ebda0SMatthew Dillon 			r.r_i_ifx = ifp->if_index;
420b06ebda0SMatthew Dillon 	} else
421b06ebda0SMatthew Dillon 		r.r_i_ifx = iface->info.ifinfo_index;
422b06ebda0SMatthew Dillon 
423b06ebda0SMatthew Dillon 	/*
424b06ebda0SMatthew Dillon 	 * XXX NOTE: only first fragment of fragmented TCP, UDP and
425b06ebda0SMatthew Dillon 	 * ICMP packet will be recorded with proper s_port and d_port.
426b06ebda0SMatthew Dillon 	 * Following fragments will be recorded simply as IP packet with
427b06ebda0SMatthew Dillon 	 * ip_proto = ip->ip_p and s_port, d_port set to zero.
428b06ebda0SMatthew Dillon 	 * I know, it looks like bug. But I don't want to re-implement
429b06ebda0SMatthew Dillon 	 * ip packet assebmling here. Anyway, (in)famous trafd works this way -
430b06ebda0SMatthew Dillon 	 * and nobody complains yet :)
431b06ebda0SMatthew Dillon 	 */
432b06ebda0SMatthew Dillon 	if ((ip->ip_off & htons(IP_OFFMASK)) == 0)
433b06ebda0SMatthew Dillon 		switch(r.r_ip_p) {
434b06ebda0SMatthew Dillon 		case IPPROTO_TCP:
435b06ebda0SMatthew Dillon 		{
436b06ebda0SMatthew Dillon 			register struct tcphdr *tcp;
437b06ebda0SMatthew Dillon 
438b06ebda0SMatthew Dillon 			tcp = (struct tcphdr *)((caddr_t )ip + hlen);
439b06ebda0SMatthew Dillon 			r.r_sport = tcp->th_sport;
440b06ebda0SMatthew Dillon 			r.r_dport = tcp->th_dport;
441b06ebda0SMatthew Dillon 			tcp_flags = tcp->th_flags;
442b06ebda0SMatthew Dillon 			break;
443b06ebda0SMatthew Dillon 		}
444b06ebda0SMatthew Dillon 			case IPPROTO_UDP:
445b06ebda0SMatthew Dillon 			r.r_ports = *(uint32_t *)((caddr_t )ip + hlen);
446b06ebda0SMatthew Dillon 			break;
447b06ebda0SMatthew Dillon 		}
448b06ebda0SMatthew Dillon 
449b06ebda0SMatthew Dillon 	/* Update node statistics. XXX: race... */
450b06ebda0SMatthew Dillon 	priv->info.nfinfo_packets ++;
451b06ebda0SMatthew Dillon 	priv->info.nfinfo_bytes += plen;
452b06ebda0SMatthew Dillon 
453b06ebda0SMatthew Dillon 	/* Find hash slot. */
454b06ebda0SMatthew Dillon 	hsh = &priv->hash[ip_hash(&r)];
455b06ebda0SMatthew Dillon 
456b06ebda0SMatthew Dillon 	mtx_lock(&hsh->mtx);
457b06ebda0SMatthew Dillon 
458b06ebda0SMatthew Dillon 	/*
459b06ebda0SMatthew Dillon 	 * Go through hash and find our entry. If we encounter an
460b06ebda0SMatthew Dillon 	 * entry, that should be expired, purge it. We do a reverse
461b06ebda0SMatthew Dillon 	 * search since most active entries are first, and most
462b06ebda0SMatthew Dillon 	 * searches are done on most active entries.
463b06ebda0SMatthew Dillon 	 */
464b06ebda0SMatthew Dillon 	TAILQ_FOREACH_REVERSE_SAFE(fle, &hsh->head, fhead, fle_hash, fle1) {
465b06ebda0SMatthew Dillon 		if (bcmp(&r, &fle->f.r, sizeof(struct flow_rec)) == 0)
466b06ebda0SMatthew Dillon 			break;
467b06ebda0SMatthew Dillon 		if ((INACTIVE(fle) && SMALL(fle)) || AGED(fle)) {
468b06ebda0SMatthew Dillon 			TAILQ_REMOVE(&hsh->head, fle, fle_hash);
469b06ebda0SMatthew Dillon 			expire_flow(priv, &item, fle, NG_QUEUE);
470b06ebda0SMatthew Dillon 			atomic_add_32(&priv->info.nfinfo_act_exp, 1);
471b06ebda0SMatthew Dillon 		}
472b06ebda0SMatthew Dillon 	}
473b06ebda0SMatthew Dillon 
474b06ebda0SMatthew Dillon 	if (fle) {			/* An existent entry. */
475b06ebda0SMatthew Dillon 
476b06ebda0SMatthew Dillon 		fle->f.bytes += plen;
477b06ebda0SMatthew Dillon 		fle->f.packets ++;
478b06ebda0SMatthew Dillon 		fle->f.tcp_flags |= tcp_flags;
479b06ebda0SMatthew Dillon 		fle->f.last = time_uptime;
480b06ebda0SMatthew Dillon 
481b06ebda0SMatthew Dillon 		/*
482b06ebda0SMatthew Dillon 		 * We have the following reasons to expire flow in active way:
483b06ebda0SMatthew Dillon 		 * - it hit active timeout
484b06ebda0SMatthew Dillon 		 * - a TCP connection closed
485b06ebda0SMatthew Dillon 		 * - it is going to overflow counter
486b06ebda0SMatthew Dillon 		 */
487b06ebda0SMatthew Dillon 		if (tcp_flags & TH_FIN || tcp_flags & TH_RST || AGED(fle) ||
488b06ebda0SMatthew Dillon 		    (fle->f.bytes >= (UINT_MAX - IF_MAXMTU)) ) {
489b06ebda0SMatthew Dillon 			TAILQ_REMOVE(&hsh->head, fle, fle_hash);
490b06ebda0SMatthew Dillon 			expire_flow(priv, &item, fle, NG_QUEUE);
491b06ebda0SMatthew Dillon 			atomic_add_32(&priv->info.nfinfo_act_exp, 1);
492b06ebda0SMatthew Dillon 		} else {
493b06ebda0SMatthew Dillon 			/*
494b06ebda0SMatthew Dillon 			 * It is the newest, move it to the tail,
495b06ebda0SMatthew Dillon 			 * if it isn't there already. Next search will
496b06ebda0SMatthew Dillon 			 * locate it quicker.
497b06ebda0SMatthew Dillon 			 */
498b06ebda0SMatthew Dillon 			if (fle != TAILQ_LAST(&hsh->head, fhead)) {
499b06ebda0SMatthew Dillon 				TAILQ_REMOVE(&hsh->head, fle, fle_hash);
500b06ebda0SMatthew Dillon 				TAILQ_INSERT_TAIL(&hsh->head, fle, fle_hash);
501b06ebda0SMatthew Dillon 			}
502b06ebda0SMatthew Dillon 		}
503b06ebda0SMatthew Dillon 	} else				/* A new flow entry. */
504b06ebda0SMatthew Dillon 		error = hash_insert(priv, hsh, &r, plen, tcp_flags);
505b06ebda0SMatthew Dillon 
506b06ebda0SMatthew Dillon 	mtx_unlock(&hsh->mtx);
507b06ebda0SMatthew Dillon 
508b06ebda0SMatthew Dillon 	if (item != NULL)
509b06ebda0SMatthew Dillon 		return_export_dgram(priv, item, NG_QUEUE);
510b06ebda0SMatthew Dillon 
511b06ebda0SMatthew Dillon 	return (error);
512b06ebda0SMatthew Dillon }
513b06ebda0SMatthew Dillon 
514b06ebda0SMatthew Dillon /*
515b06ebda0SMatthew Dillon  * Return records from cache to userland.
516b06ebda0SMatthew Dillon  *
517b06ebda0SMatthew Dillon  * TODO: matching particular IP should be done in kernel, here.
518b06ebda0SMatthew Dillon  */
519b06ebda0SMatthew Dillon int
520b06ebda0SMatthew Dillon ng_netflow_flow_show(priv_p priv, uint32_t last, struct ng_mesg *resp)
521b06ebda0SMatthew Dillon {
522b06ebda0SMatthew Dillon 	struct flow_hash_entry *hsh;
523b06ebda0SMatthew Dillon 	struct flow_entry *fle;
524b06ebda0SMatthew Dillon 	struct ngnf_flows *data;
525b06ebda0SMatthew Dillon 	int i;
526b06ebda0SMatthew Dillon 
527b06ebda0SMatthew Dillon 	data = (struct ngnf_flows *)resp->data;
528b06ebda0SMatthew Dillon 	data->last = 0;
529b06ebda0SMatthew Dillon 	data->nentries = 0;
530b06ebda0SMatthew Dillon 
531b06ebda0SMatthew Dillon 	/* Check if this is a first run */
532b06ebda0SMatthew Dillon 	if (last == 0) {
533b06ebda0SMatthew Dillon 		hsh = priv->hash;
534b06ebda0SMatthew Dillon 		i = 0;
535b06ebda0SMatthew Dillon 	} else {
536b06ebda0SMatthew Dillon 		if (last > NBUCKETS-1)
537b06ebda0SMatthew Dillon 			return (EINVAL);
538b06ebda0SMatthew Dillon 		hsh = priv->hash + last;
539b06ebda0SMatthew Dillon 		i = last;
540b06ebda0SMatthew Dillon 	}
541b06ebda0SMatthew Dillon 
542b06ebda0SMatthew Dillon 	/*
543b06ebda0SMatthew Dillon 	 * We will transfer not more than NREC_AT_ONCE. More data
544b06ebda0SMatthew Dillon 	 * will come in next message.
545b06ebda0SMatthew Dillon 	 * We send current hash index to userland, and userland should
546b06ebda0SMatthew Dillon 	 * return it back to us. Then, we will restart with new entry.
547b06ebda0SMatthew Dillon 	 *
548b06ebda0SMatthew Dillon 	 * The resulting cache snapshot is inaccurate for the
549b06ebda0SMatthew Dillon 	 * following reasons:
550b06ebda0SMatthew Dillon 	 *  - we skip locked hash entries
551b06ebda0SMatthew Dillon 	 *  - we bail out, if someone wants our entry
552b06ebda0SMatthew Dillon 	 *  - we skip rest of entry, when hit NREC_AT_ONCE
553b06ebda0SMatthew Dillon 	 */
554b06ebda0SMatthew Dillon 	for (; i < NBUCKETS; hsh++, i++) {
555b06ebda0SMatthew Dillon 		if (mtx_trylock(&hsh->mtx) == 0)
556b06ebda0SMatthew Dillon 			continue;
557b06ebda0SMatthew Dillon 
558b06ebda0SMatthew Dillon 		TAILQ_FOREACH(fle, &hsh->head, fle_hash) {
559*023c80aeSSascha Wildner 			if (mtx_contested(&hsh->mtx))
560b06ebda0SMatthew Dillon 				break;
561b06ebda0SMatthew Dillon 
562b06ebda0SMatthew Dillon 			bcopy(&fle->f, &(data->entries[data->nentries]),
563b06ebda0SMatthew Dillon 			    sizeof(fle->f));
564b06ebda0SMatthew Dillon 			data->nentries++;
565b06ebda0SMatthew Dillon 			if (data->nentries == NREC_AT_ONCE) {
566b06ebda0SMatthew Dillon 				mtx_unlock(&hsh->mtx);
567b06ebda0SMatthew Dillon 				if (++i < NBUCKETS)
568b06ebda0SMatthew Dillon 					data->last = i;
569b06ebda0SMatthew Dillon 				return (0);
570b06ebda0SMatthew Dillon 			}
571b06ebda0SMatthew Dillon 		}
572b06ebda0SMatthew Dillon 		mtx_unlock(&hsh->mtx);
573b06ebda0SMatthew Dillon 	}
574b06ebda0SMatthew Dillon 
575b06ebda0SMatthew Dillon 	return (0);
576b06ebda0SMatthew Dillon }
577b06ebda0SMatthew Dillon 
578b06ebda0SMatthew Dillon /* We have full datagram in privdata. Send it to export hook. */
579b06ebda0SMatthew Dillon static int
580b06ebda0SMatthew Dillon export_send(priv_p priv, item_p item, int flags)
581b06ebda0SMatthew Dillon {
582b06ebda0SMatthew Dillon 	struct mbuf *m = NGI_M(item);
583b06ebda0SMatthew Dillon 	struct netflow_v5_export_dgram *dgram = mtod(m,
584b06ebda0SMatthew Dillon 					struct netflow_v5_export_dgram *);
585b06ebda0SMatthew Dillon 	struct netflow_v5_header *header = &dgram->header;
586b06ebda0SMatthew Dillon 	struct timespec ts;
587b06ebda0SMatthew Dillon 	int error = 0;
588b06ebda0SMatthew Dillon 
589b06ebda0SMatthew Dillon 	/* Fill mbuf header. */
590b06ebda0SMatthew Dillon 	m->m_len = m->m_pkthdr.len = sizeof(struct netflow_v5_record) *
591b06ebda0SMatthew Dillon 	   header->count + sizeof(struct netflow_v5_header);
592b06ebda0SMatthew Dillon 
593b06ebda0SMatthew Dillon 	/* Fill export header. */
594b06ebda0SMatthew Dillon 	header->sys_uptime = htonl(MILLIUPTIME(time_uptime));
595b06ebda0SMatthew Dillon 	getnanotime(&ts);
596b06ebda0SMatthew Dillon 	header->unix_secs  = htonl(ts.tv_sec);
597b06ebda0SMatthew Dillon 	header->unix_nsecs = htonl(ts.tv_nsec);
598b06ebda0SMatthew Dillon 	header->engine_type = 0;
599b06ebda0SMatthew Dillon 	header->engine_id = 0;
600b06ebda0SMatthew Dillon 	header->pad = 0;
601b06ebda0SMatthew Dillon 	header->flow_seq = htonl(atomic_fetchadd_32(&priv->flow_seq,
602b06ebda0SMatthew Dillon 	    header->count));
603b06ebda0SMatthew Dillon 	header->count = htons(header->count);
604b06ebda0SMatthew Dillon 
605b06ebda0SMatthew Dillon 	if (priv->export != NULL)
606b06ebda0SMatthew Dillon 		NG_FWD_ITEM_HOOK_FLAGS(error, item, priv->export, flags);
607b06ebda0SMatthew Dillon 	else
608b06ebda0SMatthew Dillon 		NG_FREE_ITEM(item);
609b06ebda0SMatthew Dillon 
610b06ebda0SMatthew Dillon 	return (error);
611b06ebda0SMatthew Dillon }
612b06ebda0SMatthew Dillon 
613b06ebda0SMatthew Dillon 
614b06ebda0SMatthew Dillon /* Add export record to dgram. */
615b06ebda0SMatthew Dillon static int
616b06ebda0SMatthew Dillon export_add(item_p item, struct flow_entry *fle)
617b06ebda0SMatthew Dillon {
618b06ebda0SMatthew Dillon 	struct netflow_v5_export_dgram *dgram = mtod(NGI_M(item),
619b06ebda0SMatthew Dillon 					struct netflow_v5_export_dgram *);
620b06ebda0SMatthew Dillon 	struct netflow_v5_header *header = &dgram->header;
621b06ebda0SMatthew Dillon 	struct netflow_v5_record *rec;
622b06ebda0SMatthew Dillon 
623b06ebda0SMatthew Dillon 	rec = &dgram->r[header->count];
624b06ebda0SMatthew Dillon 	header->count ++;
625b06ebda0SMatthew Dillon 
626b06ebda0SMatthew Dillon 	KASSERT(header->count <= NETFLOW_V5_MAX_RECORDS,
627b06ebda0SMatthew Dillon 	    ("ng_netflow: export too big"));
628b06ebda0SMatthew Dillon 
629b06ebda0SMatthew Dillon 	/* Fill in export record. */
630b06ebda0SMatthew Dillon 	rec->src_addr = fle->f.r.r_src.s_addr;
631b06ebda0SMatthew Dillon 	rec->dst_addr = fle->f.r.r_dst.s_addr;
632b06ebda0SMatthew Dillon 	rec->next_hop = fle->f.next_hop.s_addr;
633b06ebda0SMatthew Dillon 	rec->i_ifx    = htons(fle->f.fle_i_ifx);
634b06ebda0SMatthew Dillon 	rec->o_ifx    = htons(fle->f.fle_o_ifx);
635b06ebda0SMatthew Dillon 	rec->packets  = htonl(fle->f.packets);
636b06ebda0SMatthew Dillon 	rec->octets   = htonl(fle->f.bytes);
637b06ebda0SMatthew Dillon 	rec->first    = htonl(MILLIUPTIME(fle->f.first));
638b06ebda0SMatthew Dillon 	rec->last     = htonl(MILLIUPTIME(fle->f.last));
639b06ebda0SMatthew Dillon 	rec->s_port   = fle->f.r.r_sport;
640b06ebda0SMatthew Dillon 	rec->d_port   = fle->f.r.r_dport;
641b06ebda0SMatthew Dillon 	rec->flags    = fle->f.tcp_flags;
642b06ebda0SMatthew Dillon 	rec->prot     = fle->f.r.r_ip_p;
643b06ebda0SMatthew Dillon 	rec->tos      = fle->f.r.r_tos;
644b06ebda0SMatthew Dillon 	rec->dst_mask = fle->f.dst_mask;
645b06ebda0SMatthew Dillon 	rec->src_mask = fle->f.src_mask;
646b06ebda0SMatthew Dillon 
647b06ebda0SMatthew Dillon 	/* Not supported fields. */
648b06ebda0SMatthew Dillon 	rec->src_as = rec->dst_as = 0;
649b06ebda0SMatthew Dillon 
650b06ebda0SMatthew Dillon 	if (header->count == NETFLOW_V5_MAX_RECORDS)
651b06ebda0SMatthew Dillon 		return (1); /* end of datagram */
652b06ebda0SMatthew Dillon 	else
653b06ebda0SMatthew Dillon 		return (0);
654b06ebda0SMatthew Dillon }
655b06ebda0SMatthew Dillon 
656b06ebda0SMatthew Dillon /* Periodic flow expiry run. */
657b06ebda0SMatthew Dillon void
658b06ebda0SMatthew Dillon ng_netflow_expire(void *arg)
659b06ebda0SMatthew Dillon {
660b06ebda0SMatthew Dillon 	struct flow_entry	*fle, *fle1;
661b06ebda0SMatthew Dillon 	struct flow_hash_entry	*hsh;
662b06ebda0SMatthew Dillon 	priv_p			priv = (priv_p )arg;
663b06ebda0SMatthew Dillon 	item_p			item = NULL;
664b06ebda0SMatthew Dillon 	uint32_t		used;
665b06ebda0SMatthew Dillon 	int			i;
666b06ebda0SMatthew Dillon 
667b06ebda0SMatthew Dillon 	/*
668b06ebda0SMatthew Dillon 	 * Going through all the cache.
669b06ebda0SMatthew Dillon 	 */
670b06ebda0SMatthew Dillon 	for (hsh = priv->hash, i = 0; i < NBUCKETS; hsh++, i++) {
671b06ebda0SMatthew Dillon 		/*
672b06ebda0SMatthew Dillon 		 * Skip entries, that are already being worked on.
673b06ebda0SMatthew Dillon 		 */
674b06ebda0SMatthew Dillon 		if (mtx_trylock(&hsh->mtx) == 0)
675b06ebda0SMatthew Dillon 			continue;
676b06ebda0SMatthew Dillon 
677b06ebda0SMatthew Dillon 		used = atomic_load_acq_32(&priv->info.nfinfo_used);
678b06ebda0SMatthew Dillon 		TAILQ_FOREACH_SAFE(fle, &hsh->head, fle_hash, fle1) {
679b06ebda0SMatthew Dillon 			/*
680b06ebda0SMatthew Dillon 			 * Interrupt thread wants this entry!
681b06ebda0SMatthew Dillon 			 * Quick! Quick! Bail out!
682b06ebda0SMatthew Dillon 			 */
6835a975a3dSMatthew Dillon 			if (mtx_contested(&hsh->mtx))
684b06ebda0SMatthew Dillon 				break;
685b06ebda0SMatthew Dillon 
686b06ebda0SMatthew Dillon 			/*
687b06ebda0SMatthew Dillon 			 * Don't expire aggressively while hash collision
688b06ebda0SMatthew Dillon 			 * ratio is predicted small.
689b06ebda0SMatthew Dillon 			 */
690b06ebda0SMatthew Dillon 			if (used <= (NBUCKETS*2) && !INACTIVE(fle))
691b06ebda0SMatthew Dillon 				break;
692b06ebda0SMatthew Dillon 
693b06ebda0SMatthew Dillon 			if ((INACTIVE(fle) && (SMALL(fle) ||
694b06ebda0SMatthew Dillon 			    (used > (NBUCKETS*2)))) || AGED(fle)) {
695b06ebda0SMatthew Dillon 				TAILQ_REMOVE(&hsh->head, fle, fle_hash);
696b06ebda0SMatthew Dillon 				expire_flow(priv, &item, fle, NG_NOFLAGS);
697b06ebda0SMatthew Dillon 				used--;
698b06ebda0SMatthew Dillon 				atomic_add_32(&priv->info.nfinfo_inact_exp, 1);
699b06ebda0SMatthew Dillon 			}
700b06ebda0SMatthew Dillon 		}
701b06ebda0SMatthew Dillon 		mtx_unlock(&hsh->mtx);
702b06ebda0SMatthew Dillon 	}
703b06ebda0SMatthew Dillon 
704b06ebda0SMatthew Dillon 	if (item != NULL)
705b06ebda0SMatthew Dillon 		return_export_dgram(priv, item, NG_NOFLAGS);
706b06ebda0SMatthew Dillon 
707b06ebda0SMatthew Dillon 	/* Schedule next expire. */
708b06ebda0SMatthew Dillon 	callout_reset(&priv->exp_callout, (1*hz), &ng_netflow_expire,
709b06ebda0SMatthew Dillon 	    (void *)priv);
710b06ebda0SMatthew Dillon }
711