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