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