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 $ 28*5a975a3dSMatthew Dillon * $FreeBSD: src/sys/netgraph/netflow/netflow.c,v 1.29 2008/05/09 23:02:57 julian Exp $ 29*5a975a3dSMatthew Dillon * $DragonFly: src/sys/netgraph7/netflow/netflow.c,v 1.2 2008/06/26 23:05:40 dillon Exp $ 30b06ebda0SMatthew Dillon */ 31b06ebda0SMatthew Dillon 32b06ebda0SMatthew Dillon #include <sys/param.h> 33b06ebda0SMatthew Dillon #include <sys/kernel.h> 34b06ebda0SMatthew Dillon #include <sys/limits.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 50*5a975a3dSMatthew Dillon #include "ng_message.h" 51*5a975a3dSMatthew Dillon #include "netgraph.h" 52b06ebda0SMatthew Dillon 53*5a975a3dSMatthew Dillon #include "netflow/netflow.h" 54*5a975a3dSMatthew 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 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 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 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 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 160*5a975a3dSMatthew Dillon m = m_getcl(MB_DONTWAIT, 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 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 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 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 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 243b06ebda0SMatthew Dillon mtx_assert(&hsh->mtx, MA_OWNED); 244b06ebda0SMatthew Dillon 245*5a975a3dSMatthew 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 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. */ 333b06ebda0SMatthew Dillon MALLOC(priv->hash, struct flow_hash_entry *, 334b06ebda0SMatthew Dillon NBUCKETS * sizeof(struct flow_hash_entry), 335b06ebda0SMatthew Dillon M_NETFLOW_HASH, M_WAITOK | M_ZERO); 336b06ebda0SMatthew Dillon 337b06ebda0SMatthew Dillon if (priv->hash == NULL) { 338b06ebda0SMatthew Dillon uma_zdestroy(priv->zone); 339b06ebda0SMatthew Dillon return (ENOMEM); 340b06ebda0SMatthew Dillon } 341b06ebda0SMatthew Dillon 342b06ebda0SMatthew Dillon /* Initialize hash. */ 343b06ebda0SMatthew Dillon for (i = 0, hsh = priv->hash; i < NBUCKETS; i++, hsh++) { 344b06ebda0SMatthew Dillon mtx_init(&hsh->mtx, "hash mutex", NULL, MTX_DEF); 345b06ebda0SMatthew Dillon TAILQ_INIT(&hsh->head); 346b06ebda0SMatthew Dillon } 347b06ebda0SMatthew Dillon 348b06ebda0SMatthew Dillon mtx_init(&priv->export_mtx, "export dgram lock", NULL, MTX_DEF); 349b06ebda0SMatthew Dillon 350b06ebda0SMatthew Dillon return (0); 351b06ebda0SMatthew Dillon } 352b06ebda0SMatthew Dillon 353b06ebda0SMatthew Dillon /* Free all flow cache memory. Called from node close method. */ 354b06ebda0SMatthew Dillon void 355b06ebda0SMatthew Dillon ng_netflow_cache_flush(priv_p priv) 356b06ebda0SMatthew Dillon { 357b06ebda0SMatthew Dillon struct flow_entry *fle, *fle1; 358b06ebda0SMatthew Dillon struct flow_hash_entry *hsh; 359b06ebda0SMatthew Dillon item_p item = NULL; 360b06ebda0SMatthew Dillon int i; 361b06ebda0SMatthew Dillon 362b06ebda0SMatthew Dillon /* 363b06ebda0SMatthew Dillon * We are going to free probably billable data. 364b06ebda0SMatthew Dillon * Expire everything before freeing it. 365b06ebda0SMatthew Dillon * No locking is required since callout is already drained. 366b06ebda0SMatthew Dillon */ 367b06ebda0SMatthew Dillon for (hsh = priv->hash, i = 0; i < NBUCKETS; hsh++, i++) 368b06ebda0SMatthew Dillon TAILQ_FOREACH_SAFE(fle, &hsh->head, fle_hash, fle1) { 369b06ebda0SMatthew Dillon TAILQ_REMOVE(&hsh->head, fle, fle_hash); 370b06ebda0SMatthew Dillon expire_flow(priv, &item, fle, NG_QUEUE); 371b06ebda0SMatthew Dillon } 372b06ebda0SMatthew Dillon 373b06ebda0SMatthew Dillon if (item != NULL) 374b06ebda0SMatthew Dillon export_send(priv, item, NG_QUEUE); 375b06ebda0SMatthew Dillon 376b06ebda0SMatthew Dillon uma_zdestroy(priv->zone); 377b06ebda0SMatthew Dillon 378b06ebda0SMatthew Dillon /* Destroy hash mutexes. */ 379b06ebda0SMatthew Dillon for (i = 0, hsh = priv->hash; i < NBUCKETS; i++, hsh++) 380b06ebda0SMatthew Dillon mtx_destroy(&hsh->mtx); 381b06ebda0SMatthew Dillon 382b06ebda0SMatthew Dillon /* Free hash memory. */ 383b06ebda0SMatthew Dillon if (priv->hash) 384b06ebda0SMatthew Dillon FREE(priv->hash, M_NETFLOW_HASH); 385b06ebda0SMatthew Dillon 386b06ebda0SMatthew Dillon mtx_destroy(&priv->export_mtx); 387b06ebda0SMatthew Dillon } 388b06ebda0SMatthew Dillon 389b06ebda0SMatthew Dillon /* Insert packet from into flow cache. */ 390b06ebda0SMatthew Dillon int 391b06ebda0SMatthew Dillon ng_netflow_flow_add(priv_p priv, struct ip *ip, iface_p iface, 392b06ebda0SMatthew Dillon struct ifnet *ifp) 393b06ebda0SMatthew Dillon { 394b06ebda0SMatthew Dillon register struct flow_entry *fle, *fle1; 395b06ebda0SMatthew Dillon struct flow_hash_entry *hsh; 396b06ebda0SMatthew Dillon struct flow_rec r; 397b06ebda0SMatthew Dillon item_p item = NULL; 398b06ebda0SMatthew Dillon int hlen, plen; 399b06ebda0SMatthew Dillon int error = 0; 400b06ebda0SMatthew Dillon uint8_t tcp_flags = 0; 401b06ebda0SMatthew Dillon 402b06ebda0SMatthew Dillon /* Try to fill flow_rec r */ 403b06ebda0SMatthew Dillon bzero(&r, sizeof(r)); 404b06ebda0SMatthew Dillon /* check version */ 405b06ebda0SMatthew Dillon if (ip->ip_v != IPVERSION) 406b06ebda0SMatthew Dillon return (EINVAL); 407b06ebda0SMatthew Dillon 408b06ebda0SMatthew Dillon /* verify min header length */ 409b06ebda0SMatthew Dillon hlen = ip->ip_hl << 2; 410b06ebda0SMatthew Dillon 411b06ebda0SMatthew Dillon if (hlen < sizeof(struct ip)) 412b06ebda0SMatthew Dillon return (EINVAL); 413b06ebda0SMatthew Dillon 414b06ebda0SMatthew Dillon r.r_src = ip->ip_src; 415b06ebda0SMatthew Dillon r.r_dst = ip->ip_dst; 416b06ebda0SMatthew Dillon 417b06ebda0SMatthew Dillon /* save packet length */ 418b06ebda0SMatthew Dillon plen = ntohs(ip->ip_len); 419b06ebda0SMatthew Dillon 420b06ebda0SMatthew Dillon r.r_ip_p = ip->ip_p; 421b06ebda0SMatthew Dillon r.r_tos = ip->ip_tos; 422b06ebda0SMatthew Dillon 423b06ebda0SMatthew Dillon /* Configured in_ifx overrides mbuf's */ 424b06ebda0SMatthew Dillon if (iface->info.ifinfo_index == 0) { 425b06ebda0SMatthew Dillon if (ifp != NULL) 426b06ebda0SMatthew Dillon r.r_i_ifx = ifp->if_index; 427b06ebda0SMatthew Dillon } else 428b06ebda0SMatthew Dillon r.r_i_ifx = iface->info.ifinfo_index; 429b06ebda0SMatthew Dillon 430b06ebda0SMatthew Dillon /* 431b06ebda0SMatthew Dillon * XXX NOTE: only first fragment of fragmented TCP, UDP and 432b06ebda0SMatthew Dillon * ICMP packet will be recorded with proper s_port and d_port. 433b06ebda0SMatthew Dillon * Following fragments will be recorded simply as IP packet with 434b06ebda0SMatthew Dillon * ip_proto = ip->ip_p and s_port, d_port set to zero. 435b06ebda0SMatthew Dillon * I know, it looks like bug. But I don't want to re-implement 436b06ebda0SMatthew Dillon * ip packet assebmling here. Anyway, (in)famous trafd works this way - 437b06ebda0SMatthew Dillon * and nobody complains yet :) 438b06ebda0SMatthew Dillon */ 439b06ebda0SMatthew Dillon if ((ip->ip_off & htons(IP_OFFMASK)) == 0) 440b06ebda0SMatthew Dillon switch(r.r_ip_p) { 441b06ebda0SMatthew Dillon case IPPROTO_TCP: 442b06ebda0SMatthew Dillon { 443b06ebda0SMatthew Dillon register struct tcphdr *tcp; 444b06ebda0SMatthew Dillon 445b06ebda0SMatthew Dillon tcp = (struct tcphdr *)((caddr_t )ip + hlen); 446b06ebda0SMatthew Dillon r.r_sport = tcp->th_sport; 447b06ebda0SMatthew Dillon r.r_dport = tcp->th_dport; 448b06ebda0SMatthew Dillon tcp_flags = tcp->th_flags; 449b06ebda0SMatthew Dillon break; 450b06ebda0SMatthew Dillon } 451b06ebda0SMatthew Dillon case IPPROTO_UDP: 452b06ebda0SMatthew Dillon r.r_ports = *(uint32_t *)((caddr_t )ip + hlen); 453b06ebda0SMatthew Dillon break; 454b06ebda0SMatthew Dillon } 455b06ebda0SMatthew Dillon 456b06ebda0SMatthew Dillon /* Update node statistics. XXX: race... */ 457b06ebda0SMatthew Dillon priv->info.nfinfo_packets ++; 458b06ebda0SMatthew Dillon priv->info.nfinfo_bytes += plen; 459b06ebda0SMatthew Dillon 460b06ebda0SMatthew Dillon /* Find hash slot. */ 461b06ebda0SMatthew Dillon hsh = &priv->hash[ip_hash(&r)]; 462b06ebda0SMatthew Dillon 463b06ebda0SMatthew Dillon mtx_lock(&hsh->mtx); 464b06ebda0SMatthew Dillon 465b06ebda0SMatthew Dillon /* 466b06ebda0SMatthew Dillon * Go through hash and find our entry. If we encounter an 467b06ebda0SMatthew Dillon * entry, that should be expired, purge it. We do a reverse 468b06ebda0SMatthew Dillon * search since most active entries are first, and most 469b06ebda0SMatthew Dillon * searches are done on most active entries. 470b06ebda0SMatthew Dillon */ 471b06ebda0SMatthew Dillon TAILQ_FOREACH_REVERSE_SAFE(fle, &hsh->head, fhead, fle_hash, fle1) { 472b06ebda0SMatthew Dillon if (bcmp(&r, &fle->f.r, sizeof(struct flow_rec)) == 0) 473b06ebda0SMatthew Dillon break; 474b06ebda0SMatthew Dillon if ((INACTIVE(fle) && SMALL(fle)) || AGED(fle)) { 475b06ebda0SMatthew Dillon TAILQ_REMOVE(&hsh->head, fle, fle_hash); 476b06ebda0SMatthew Dillon expire_flow(priv, &item, fle, NG_QUEUE); 477b06ebda0SMatthew Dillon atomic_add_32(&priv->info.nfinfo_act_exp, 1); 478b06ebda0SMatthew Dillon } 479b06ebda0SMatthew Dillon } 480b06ebda0SMatthew Dillon 481b06ebda0SMatthew Dillon if (fle) { /* An existent entry. */ 482b06ebda0SMatthew Dillon 483b06ebda0SMatthew Dillon fle->f.bytes += plen; 484b06ebda0SMatthew Dillon fle->f.packets ++; 485b06ebda0SMatthew Dillon fle->f.tcp_flags |= tcp_flags; 486b06ebda0SMatthew Dillon fle->f.last = time_uptime; 487b06ebda0SMatthew Dillon 488b06ebda0SMatthew Dillon /* 489b06ebda0SMatthew Dillon * We have the following reasons to expire flow in active way: 490b06ebda0SMatthew Dillon * - it hit active timeout 491b06ebda0SMatthew Dillon * - a TCP connection closed 492b06ebda0SMatthew Dillon * - it is going to overflow counter 493b06ebda0SMatthew Dillon */ 494b06ebda0SMatthew Dillon if (tcp_flags & TH_FIN || tcp_flags & TH_RST || AGED(fle) || 495b06ebda0SMatthew Dillon (fle->f.bytes >= (UINT_MAX - IF_MAXMTU)) ) { 496b06ebda0SMatthew Dillon TAILQ_REMOVE(&hsh->head, fle, fle_hash); 497b06ebda0SMatthew Dillon expire_flow(priv, &item, fle, NG_QUEUE); 498b06ebda0SMatthew Dillon atomic_add_32(&priv->info.nfinfo_act_exp, 1); 499b06ebda0SMatthew Dillon } else { 500b06ebda0SMatthew Dillon /* 501b06ebda0SMatthew Dillon * It is the newest, move it to the tail, 502b06ebda0SMatthew Dillon * if it isn't there already. Next search will 503b06ebda0SMatthew Dillon * locate it quicker. 504b06ebda0SMatthew Dillon */ 505b06ebda0SMatthew Dillon if (fle != TAILQ_LAST(&hsh->head, fhead)) { 506b06ebda0SMatthew Dillon TAILQ_REMOVE(&hsh->head, fle, fle_hash); 507b06ebda0SMatthew Dillon TAILQ_INSERT_TAIL(&hsh->head, fle, fle_hash); 508b06ebda0SMatthew Dillon } 509b06ebda0SMatthew Dillon } 510b06ebda0SMatthew Dillon } else /* A new flow entry. */ 511b06ebda0SMatthew Dillon error = hash_insert(priv, hsh, &r, plen, tcp_flags); 512b06ebda0SMatthew Dillon 513b06ebda0SMatthew Dillon mtx_unlock(&hsh->mtx); 514b06ebda0SMatthew Dillon 515b06ebda0SMatthew Dillon if (item != NULL) 516b06ebda0SMatthew Dillon return_export_dgram(priv, item, NG_QUEUE); 517b06ebda0SMatthew Dillon 518b06ebda0SMatthew Dillon return (error); 519b06ebda0SMatthew Dillon } 520b06ebda0SMatthew Dillon 521b06ebda0SMatthew Dillon /* 522b06ebda0SMatthew Dillon * Return records from cache to userland. 523b06ebda0SMatthew Dillon * 524b06ebda0SMatthew Dillon * TODO: matching particular IP should be done in kernel, here. 525b06ebda0SMatthew Dillon */ 526b06ebda0SMatthew Dillon int 527b06ebda0SMatthew Dillon ng_netflow_flow_show(priv_p priv, uint32_t last, struct ng_mesg *resp) 528b06ebda0SMatthew Dillon { 529b06ebda0SMatthew Dillon struct flow_hash_entry *hsh; 530b06ebda0SMatthew Dillon struct flow_entry *fle; 531b06ebda0SMatthew Dillon struct ngnf_flows *data; 532b06ebda0SMatthew Dillon int i; 533b06ebda0SMatthew Dillon 534b06ebda0SMatthew Dillon data = (struct ngnf_flows *)resp->data; 535b06ebda0SMatthew Dillon data->last = 0; 536b06ebda0SMatthew Dillon data->nentries = 0; 537b06ebda0SMatthew Dillon 538b06ebda0SMatthew Dillon /* Check if this is a first run */ 539b06ebda0SMatthew Dillon if (last == 0) { 540b06ebda0SMatthew Dillon hsh = priv->hash; 541b06ebda0SMatthew Dillon i = 0; 542b06ebda0SMatthew Dillon } else { 543b06ebda0SMatthew Dillon if (last > NBUCKETS-1) 544b06ebda0SMatthew Dillon return (EINVAL); 545b06ebda0SMatthew Dillon hsh = priv->hash + last; 546b06ebda0SMatthew Dillon i = last; 547b06ebda0SMatthew Dillon } 548b06ebda0SMatthew Dillon 549b06ebda0SMatthew Dillon /* 550b06ebda0SMatthew Dillon * We will transfer not more than NREC_AT_ONCE. More data 551b06ebda0SMatthew Dillon * will come in next message. 552b06ebda0SMatthew Dillon * We send current hash index to userland, and userland should 553b06ebda0SMatthew Dillon * return it back to us. Then, we will restart with new entry. 554b06ebda0SMatthew Dillon * 555b06ebda0SMatthew Dillon * The resulting cache snapshot is inaccurate for the 556b06ebda0SMatthew Dillon * following reasons: 557b06ebda0SMatthew Dillon * - we skip locked hash entries 558b06ebda0SMatthew Dillon * - we bail out, if someone wants our entry 559b06ebda0SMatthew Dillon * - we skip rest of entry, when hit NREC_AT_ONCE 560b06ebda0SMatthew Dillon */ 561b06ebda0SMatthew Dillon for (; i < NBUCKETS; hsh++, i++) { 562b06ebda0SMatthew Dillon if (mtx_trylock(&hsh->mtx) == 0) 563b06ebda0SMatthew Dillon continue; 564b06ebda0SMatthew Dillon 565b06ebda0SMatthew Dillon TAILQ_FOREACH(fle, &hsh->head, fle_hash) { 566*5a975a3dSMatthew Dillon if (mtx_contested(&hsh->mtx) 567b06ebda0SMatthew Dillon break; 568b06ebda0SMatthew Dillon 569b06ebda0SMatthew Dillon bcopy(&fle->f, &(data->entries[data->nentries]), 570b06ebda0SMatthew Dillon sizeof(fle->f)); 571b06ebda0SMatthew Dillon data->nentries++; 572b06ebda0SMatthew Dillon if (data->nentries == NREC_AT_ONCE) { 573b06ebda0SMatthew Dillon mtx_unlock(&hsh->mtx); 574b06ebda0SMatthew Dillon if (++i < NBUCKETS) 575b06ebda0SMatthew Dillon data->last = i; 576b06ebda0SMatthew Dillon return (0); 577b06ebda0SMatthew Dillon } 578b06ebda0SMatthew Dillon } 579b06ebda0SMatthew Dillon mtx_unlock(&hsh->mtx); 580b06ebda0SMatthew Dillon } 581b06ebda0SMatthew Dillon 582b06ebda0SMatthew Dillon return (0); 583b06ebda0SMatthew Dillon } 584b06ebda0SMatthew Dillon 585b06ebda0SMatthew Dillon /* We have full datagram in privdata. Send it to export hook. */ 586b06ebda0SMatthew Dillon static int 587b06ebda0SMatthew Dillon export_send(priv_p priv, item_p item, int flags) 588b06ebda0SMatthew Dillon { 589b06ebda0SMatthew Dillon struct mbuf *m = NGI_M(item); 590b06ebda0SMatthew Dillon struct netflow_v5_export_dgram *dgram = mtod(m, 591b06ebda0SMatthew Dillon struct netflow_v5_export_dgram *); 592b06ebda0SMatthew Dillon struct netflow_v5_header *header = &dgram->header; 593b06ebda0SMatthew Dillon struct timespec ts; 594b06ebda0SMatthew Dillon int error = 0; 595b06ebda0SMatthew Dillon 596b06ebda0SMatthew Dillon /* Fill mbuf header. */ 597b06ebda0SMatthew Dillon m->m_len = m->m_pkthdr.len = sizeof(struct netflow_v5_record) * 598b06ebda0SMatthew Dillon header->count + sizeof(struct netflow_v5_header); 599b06ebda0SMatthew Dillon 600b06ebda0SMatthew Dillon /* Fill export header. */ 601b06ebda0SMatthew Dillon header->sys_uptime = htonl(MILLIUPTIME(time_uptime)); 602b06ebda0SMatthew Dillon getnanotime(&ts); 603b06ebda0SMatthew Dillon header->unix_secs = htonl(ts.tv_sec); 604b06ebda0SMatthew Dillon header->unix_nsecs = htonl(ts.tv_nsec); 605b06ebda0SMatthew Dillon header->engine_type = 0; 606b06ebda0SMatthew Dillon header->engine_id = 0; 607b06ebda0SMatthew Dillon header->pad = 0; 608b06ebda0SMatthew Dillon header->flow_seq = htonl(atomic_fetchadd_32(&priv->flow_seq, 609b06ebda0SMatthew Dillon header->count)); 610b06ebda0SMatthew Dillon header->count = htons(header->count); 611b06ebda0SMatthew Dillon 612b06ebda0SMatthew Dillon if (priv->export != NULL) 613b06ebda0SMatthew Dillon NG_FWD_ITEM_HOOK_FLAGS(error, item, priv->export, flags); 614b06ebda0SMatthew Dillon else 615b06ebda0SMatthew Dillon NG_FREE_ITEM(item); 616b06ebda0SMatthew Dillon 617b06ebda0SMatthew Dillon return (error); 618b06ebda0SMatthew Dillon } 619b06ebda0SMatthew Dillon 620b06ebda0SMatthew Dillon 621b06ebda0SMatthew Dillon /* Add export record to dgram. */ 622b06ebda0SMatthew Dillon static int 623b06ebda0SMatthew Dillon export_add(item_p item, struct flow_entry *fle) 624b06ebda0SMatthew Dillon { 625b06ebda0SMatthew Dillon struct netflow_v5_export_dgram *dgram = mtod(NGI_M(item), 626b06ebda0SMatthew Dillon struct netflow_v5_export_dgram *); 627b06ebda0SMatthew Dillon struct netflow_v5_header *header = &dgram->header; 628b06ebda0SMatthew Dillon struct netflow_v5_record *rec; 629b06ebda0SMatthew Dillon 630b06ebda0SMatthew Dillon rec = &dgram->r[header->count]; 631b06ebda0SMatthew Dillon header->count ++; 632b06ebda0SMatthew Dillon 633b06ebda0SMatthew Dillon KASSERT(header->count <= NETFLOW_V5_MAX_RECORDS, 634b06ebda0SMatthew Dillon ("ng_netflow: export too big")); 635b06ebda0SMatthew Dillon 636b06ebda0SMatthew Dillon /* Fill in export record. */ 637b06ebda0SMatthew Dillon rec->src_addr = fle->f.r.r_src.s_addr; 638b06ebda0SMatthew Dillon rec->dst_addr = fle->f.r.r_dst.s_addr; 639b06ebda0SMatthew Dillon rec->next_hop = fle->f.next_hop.s_addr; 640b06ebda0SMatthew Dillon rec->i_ifx = htons(fle->f.fle_i_ifx); 641b06ebda0SMatthew Dillon rec->o_ifx = htons(fle->f.fle_o_ifx); 642b06ebda0SMatthew Dillon rec->packets = htonl(fle->f.packets); 643b06ebda0SMatthew Dillon rec->octets = htonl(fle->f.bytes); 644b06ebda0SMatthew Dillon rec->first = htonl(MILLIUPTIME(fle->f.first)); 645b06ebda0SMatthew Dillon rec->last = htonl(MILLIUPTIME(fle->f.last)); 646b06ebda0SMatthew Dillon rec->s_port = fle->f.r.r_sport; 647b06ebda0SMatthew Dillon rec->d_port = fle->f.r.r_dport; 648b06ebda0SMatthew Dillon rec->flags = fle->f.tcp_flags; 649b06ebda0SMatthew Dillon rec->prot = fle->f.r.r_ip_p; 650b06ebda0SMatthew Dillon rec->tos = fle->f.r.r_tos; 651b06ebda0SMatthew Dillon rec->dst_mask = fle->f.dst_mask; 652b06ebda0SMatthew Dillon rec->src_mask = fle->f.src_mask; 653b06ebda0SMatthew Dillon 654b06ebda0SMatthew Dillon /* Not supported fields. */ 655b06ebda0SMatthew Dillon rec->src_as = rec->dst_as = 0; 656b06ebda0SMatthew Dillon 657b06ebda0SMatthew Dillon if (header->count == NETFLOW_V5_MAX_RECORDS) 658b06ebda0SMatthew Dillon return (1); /* end of datagram */ 659b06ebda0SMatthew Dillon else 660b06ebda0SMatthew Dillon return (0); 661b06ebda0SMatthew Dillon } 662b06ebda0SMatthew Dillon 663b06ebda0SMatthew Dillon /* Periodic flow expiry run. */ 664b06ebda0SMatthew Dillon void 665b06ebda0SMatthew Dillon ng_netflow_expire(void *arg) 666b06ebda0SMatthew Dillon { 667b06ebda0SMatthew Dillon struct flow_entry *fle, *fle1; 668b06ebda0SMatthew Dillon struct flow_hash_entry *hsh; 669b06ebda0SMatthew Dillon priv_p priv = (priv_p )arg; 670b06ebda0SMatthew Dillon item_p item = NULL; 671b06ebda0SMatthew Dillon uint32_t used; 672b06ebda0SMatthew Dillon int i; 673b06ebda0SMatthew Dillon 674b06ebda0SMatthew Dillon /* 675b06ebda0SMatthew Dillon * Going through all the cache. 676b06ebda0SMatthew Dillon */ 677b06ebda0SMatthew Dillon for (hsh = priv->hash, i = 0; i < NBUCKETS; hsh++, i++) { 678b06ebda0SMatthew Dillon /* 679b06ebda0SMatthew Dillon * Skip entries, that are already being worked on. 680b06ebda0SMatthew Dillon */ 681b06ebda0SMatthew Dillon if (mtx_trylock(&hsh->mtx) == 0) 682b06ebda0SMatthew Dillon continue; 683b06ebda0SMatthew Dillon 684b06ebda0SMatthew Dillon used = atomic_load_acq_32(&priv->info.nfinfo_used); 685b06ebda0SMatthew Dillon TAILQ_FOREACH_SAFE(fle, &hsh->head, fle_hash, fle1) { 686b06ebda0SMatthew Dillon /* 687b06ebda0SMatthew Dillon * Interrupt thread wants this entry! 688b06ebda0SMatthew Dillon * Quick! Quick! Bail out! 689b06ebda0SMatthew Dillon */ 690*5a975a3dSMatthew Dillon if (mtx_contested(&hsh->mtx)) 691b06ebda0SMatthew Dillon break; 692b06ebda0SMatthew Dillon 693b06ebda0SMatthew Dillon /* 694b06ebda0SMatthew Dillon * Don't expire aggressively while hash collision 695b06ebda0SMatthew Dillon * ratio is predicted small. 696b06ebda0SMatthew Dillon */ 697b06ebda0SMatthew Dillon if (used <= (NBUCKETS*2) && !INACTIVE(fle)) 698b06ebda0SMatthew Dillon break; 699b06ebda0SMatthew Dillon 700b06ebda0SMatthew Dillon if ((INACTIVE(fle) && (SMALL(fle) || 701b06ebda0SMatthew Dillon (used > (NBUCKETS*2)))) || AGED(fle)) { 702b06ebda0SMatthew Dillon TAILQ_REMOVE(&hsh->head, fle, fle_hash); 703b06ebda0SMatthew Dillon expire_flow(priv, &item, fle, NG_NOFLAGS); 704b06ebda0SMatthew Dillon used--; 705b06ebda0SMatthew Dillon atomic_add_32(&priv->info.nfinfo_inact_exp, 1); 706b06ebda0SMatthew Dillon } 707b06ebda0SMatthew Dillon } 708b06ebda0SMatthew Dillon mtx_unlock(&hsh->mtx); 709b06ebda0SMatthew Dillon } 710b06ebda0SMatthew Dillon 711b06ebda0SMatthew Dillon if (item != NULL) 712b06ebda0SMatthew Dillon return_export_dgram(priv, item, NG_NOFLAGS); 713b06ebda0SMatthew Dillon 714b06ebda0SMatthew Dillon /* Schedule next expire. */ 715b06ebda0SMatthew Dillon callout_reset(&priv->exp_callout, (1*hz), &ng_netflow_expire, 716b06ebda0SMatthew Dillon (void *)priv); 717b06ebda0SMatthew Dillon } 718