1 /*- 2 * Copyright (c) 2004 Gleb Smirnoff <glebius@FreeBSD.org> 3 * Copyright (c) 2001-2003 Roman V. Palagin <romanp@unshadow.net> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * $SourceForge: netflow.c,v 1.41 2004/09/05 11:41:10 glebius Exp $ 28 */ 29 30 static const char rcs_id[] = 31 "@(#) $FreeBSD$"; 32 33 #include <sys/param.h> 34 #include <sys/kernel.h> 35 #include <sys/limits.h> 36 #include <sys/mbuf.h> 37 #include <sys/syslog.h> 38 #include <sys/systm.h> 39 #include <sys/socket.h> 40 41 #include <net/if.h> 42 #include <net/if_var.h> 43 #include <net/if_dl.h> 44 #include <net/route.h> 45 #include <netinet/in.h> 46 #include <netinet/in_systm.h> 47 #include <netinet/ip.h> 48 #include <netinet/tcp.h> 49 #include <netinet/udp.h> 50 51 #include <netgraph/ng_message.h> 52 #include <netgraph/netgraph.h> 53 54 #include <netgraph/netflow/netflow.h> 55 #include <netgraph/netflow/ng_netflow.h> 56 57 #define NBUCKETS (4096) /* must be power of 2 */ 58 59 /* This hash is for TCP or UDP packets */ 60 #define FULL_HASH(addr1,addr2,port1,port2)\ 61 (((addr1 >> 16) ^ \ 62 (addr2 & 0x00FF) ^ \ 63 ((port1 ^ port2) << 8) )& \ 64 (NBUCKETS - 1)) 65 66 /* This hash for all other IP packets */ 67 #define ADDR_HASH(addr1,addr2)\ 68 (((addr1 >> 16) ^ \ 69 (addr2 & 0x00FF) )& \ 70 (NBUCKETS - 1)) 71 72 /* Macros to shorten logical constructions */ 73 /* XXX: priv must exist in namespace */ 74 #define INACTIVE(fle) (time_uptime - fle->f.last > priv->info.nfinfo_inact_t) 75 #define AGED(fle) (time_uptime - fle->f.first > priv->info.nfinfo_act_t) 76 #define ISFREE(fle) (fle->f.packets == 0) 77 78 /* 79 * 4 is a magical number: statistically number of 4-packet flows is 80 * bigger than 5,6,7...-packet flows by an order of magnitude. Most UDP/ICMP 81 * scans are 1 packet (~ 90% of flow cache). TCP scans are 2-packet in case 82 * of reachable host and 4-packet otherwise. 83 */ 84 #define SMALL(fle) (fle->f.packets <= 4) 85 86 /* 87 * Cisco uses milliseconds for uptime. Bad idea, since it overflows 88 * every 48+ days. But we will do same to keep compatibility. This macro 89 * does overflowable multiplication to 1000. 90 */ 91 #define MILLIUPTIME(t) (((t) << 9) + /* 512 */ \ 92 ((t) << 8) + /* 256 */ \ 93 ((t) << 7) + /* 128 */ \ 94 ((t) << 6) + /* 64 */ \ 95 ((t) << 5) + /* 32 */ \ 96 ((t) << 3)) /* 8 */ 97 98 MALLOC_DECLARE(M_NETFLOW); 99 MALLOC_DEFINE(M_NETFLOW, "NetFlow", "flow cache"); 100 101 static int export_add(priv_p , struct flow_entry *); 102 static int export_send(priv_p ); 103 104 /* Generate hash for a given flow record */ 105 static __inline uint32_t 106 ip_hash(struct flow_rec *r) 107 { 108 switch (r->r_ip_p) { 109 case IPPROTO_TCP: 110 case IPPROTO_UDP: 111 return FULL_HASH(r->r_src.s_addr, r->r_dst.s_addr, 112 r->r_sport, r->r_dport); 113 default: 114 return ADDR_HASH(r->r_src.s_addr, r->r_dst.s_addr); 115 } 116 } 117 118 /* Lookup for record in given slot */ 119 static __inline struct flow_entry * 120 hash_lookup(struct flow_hash_entry *h, int slot, struct flow_rec *r) 121 { 122 struct flow_entry *fle; 123 124 LIST_FOREACH(fle, &(h[slot].head), fle_hash) 125 if (bcmp(r, &fle->f.r, sizeof(struct flow_rec)) == 0) 126 return (fle); 127 128 return (NULL); 129 } 130 131 /* Get a flow entry from free list */ 132 static __inline struct flow_entry * 133 alloc_flow(priv_p priv, int *flows) 134 { 135 register struct flow_entry *fle; 136 137 mtx_lock(&priv->free_mtx); 138 139 if (SLIST_EMPTY(&priv->free_list)) { 140 mtx_unlock(&priv->free_mtx); 141 return(NULL); 142 } 143 144 fle = SLIST_FIRST(&priv->free_list); 145 SLIST_REMOVE_HEAD(&priv->free_list, fle_free); 146 147 priv->info.nfinfo_used++; 148 priv->info.nfinfo_free--; 149 150 if (flows != NULL) 151 *flows = priv->info.nfinfo_used; 152 153 mtx_unlock(&priv->free_mtx); 154 155 return (fle); 156 } 157 158 /* Insert flow entry into a free list. */ 159 static __inline int 160 free_flow(priv_p priv, struct flow_entry *fle) 161 { 162 int flows; 163 164 mtx_lock(&priv->free_mtx); 165 fle->f.packets = 0; 166 SLIST_INSERT_HEAD(&priv->free_list, fle, fle_free); 167 flows = priv->info.nfinfo_used--; 168 priv->info.nfinfo_free++; 169 mtx_unlock(&priv->free_mtx); 170 171 return flows; 172 } 173 174 #define NGNF_GETUSED(priv, rval) do { \ 175 mtx_lock(&priv->free_mtx); \ 176 rval = priv->info.nfinfo_used; \ 177 mtx_unlock(&priv->free_mtx); \ 178 } while (0) 179 180 /* Insert flow entry into expire list. */ 181 /* XXX: Flow must be detached from work queue, but not from cache */ 182 static __inline void 183 expire_flow(priv_p priv, struct flow_entry *fle) 184 { 185 mtx_assert(&priv->work_mtx, MA_OWNED); 186 LIST_REMOVE(fle, fle_hash); 187 188 mtx_lock(&priv->expire_mtx); 189 SLIST_INSERT_HEAD(&priv->expire_list, fle, fle_free); 190 mtx_unlock(&priv->expire_mtx); 191 } 192 193 /* Get a snapshot of node statistics */ 194 void 195 ng_netflow_copyinfo(priv_p priv, struct ng_netflow_info *i) 196 { 197 mtx_lock(&priv->free_mtx); 198 memcpy((void *)i, (void *)&priv->info, sizeof(priv->info)); 199 mtx_unlock(&priv->free_mtx); 200 } 201 202 /* Calculate number of bits in netmask */ 203 #define g21 0x55555555ul /* = 0101_0101_0101_0101_0101_0101_0101_0101 */ 204 #define g22 0x33333333ul /* = 0011_0011_0011_0011_0011_0011_0011_0011 */ 205 #define g23 0x0f0f0f0ful /* = 0000_1111_0000_1111_0000_1111_0000_1111 */ 206 static __inline u_char 207 bit_count(uint32_t v) 208 { 209 v = (v & g21) + ((v >> 1) & g21); 210 v = (v & g22) + ((v >> 2) & g22); 211 v = (v + (v >> 4)) & g23; 212 return (v + (v >> 8) + (v >> 16) + (v >> 24)) & 0x3f; 213 } 214 215 /* 216 * Insert a record into defined slot. 217 * 218 * First we get for us a free flow entry, then fill in all 219 * possible fields in it. Then obtain lock on flow cache 220 * and insert flow entry. 221 */ 222 static __inline int 223 hash_insert(priv_p priv, int slot, struct flow_rec *r, int plen) 224 { 225 struct flow_hash_entry *h = priv->hash; 226 struct flow_entry *fle; 227 struct route ro; 228 struct sockaddr_in *sin; 229 230 fle = alloc_flow(priv, NULL); 231 if (fle == NULL) 232 return (ENOMEM); 233 234 /* 235 * Now fle is totally ours. It is detached from all lists, 236 * we can safely edit it. 237 */ 238 239 bcopy(r, &fle->f.r, sizeof(struct flow_rec)); 240 fle->f.bytes = plen; 241 fle->f.packets = 1; 242 243 priv->info.nfinfo_bytes += plen; 244 245 fle->f.first = fle->f.last = time_uptime; 246 247 /* 248 * First we do route table lookup on destination address. So we can 249 * fill in out_ifx, dst_mask, nexthop, and dst_as in future releases. 250 */ 251 bzero((caddr_t)&ro, sizeof(ro)); 252 sin = (struct sockaddr_in *)&ro.ro_dst; 253 sin->sin_len = sizeof(*sin); 254 sin->sin_family = AF_INET; 255 sin->sin_addr = fle->f.r.r_dst; 256 rtalloc_ign(&ro, RTF_CLONING); 257 if (ro.ro_rt != NULL) { 258 struct rtentry *rt = ro.ro_rt; 259 260 fle->f.fle_o_ifx = rt->rt_ifp->if_index; 261 262 if (rt->rt_flags & RTF_GATEWAY && 263 rt->rt_gateway->sa_family == AF_INET) 264 fle->f.next_hop = 265 ((struct sockaddr_in *)(rt->rt_gateway))->sin_addr; 266 267 if (rt_mask(rt)) 268 fle->f.dst_mask = 269 bit_count(((struct sockaddr_in *)rt_mask(rt))->sin_addr.s_addr); 270 else if (rt->rt_flags & RTF_HOST) 271 /* Give up. We can't determine mask :( */ 272 fle->f.dst_mask = 32; 273 274 RTFREE(ro.ro_rt); 275 } 276 277 /* Do route lookup on source address, to fill in src_mask. */ 278 279 bzero((caddr_t)&ro, sizeof(ro)); 280 sin = (struct sockaddr_in *)&ro.ro_dst; 281 sin->sin_len = sizeof(*sin); 282 sin->sin_family = AF_INET; 283 sin->sin_addr = fle->f.r.r_src; 284 rtalloc_ign(&ro, RTF_CLONING); 285 if (ro.ro_rt != NULL) { 286 struct rtentry *rt = ro.ro_rt; 287 288 if (rt_mask(rt)) 289 fle->f.src_mask = 290 bit_count(((struct sockaddr_in *)rt_mask(rt))->sin_addr.s_addr); 291 else if (rt->rt_flags & RTF_HOST) 292 /* Give up. We can't determine mask :( */ 293 fle->f.src_mask = 32; 294 295 RTFREE(ro.ro_rt); 296 } 297 298 /* Push new flow entry into flow cache */ 299 mtx_lock(&priv->work_mtx); 300 LIST_INSERT_HEAD(&(h[slot].head), fle, fle_hash); 301 TAILQ_INSERT_TAIL(&priv->work_queue, fle, fle_work); 302 mtx_unlock(&priv->work_mtx); 303 304 return (0); 305 } 306 307 static __inline int 308 make_flow_rec(struct mbuf **m, int *plen, struct flow_rec *r, 309 uint8_t *tcp_flags, u_int16_t i_ifx) 310 { 311 register struct ip *ip; 312 int hlen; 313 int error = 0; 314 315 ip = mtod(*m, struct ip*); 316 317 /* check version */ 318 if (ip->ip_v != IPVERSION) 319 return (EINVAL); 320 321 /* verify min header length */ 322 hlen = ip->ip_hl << 2; 323 324 if (hlen < sizeof(struct ip)) 325 return (EINVAL); 326 327 r->r_src = ip->ip_src; 328 r->r_dst = ip->ip_dst; 329 330 /* save packet length */ 331 *plen = ntohs(ip->ip_len); 332 333 r->r_ip_p = ip->ip_p; 334 r->r_tos = ip->ip_tos; 335 336 /* Configured in_ifx overrides mbuf's */ 337 if (i_ifx == 0) { 338 if ((*m)->m_pkthdr.rcvif) 339 r->r_i_ifx = (*m)->m_pkthdr.rcvif->if_index; 340 } else 341 r->r_i_ifx = i_ifx; 342 343 /* 344 * XXX NOTE: only first fragment of fragmented TCP, UDP and 345 * ICMP packet will be recorded with proper s_port and d_port. 346 * Following fragments will be recorded simply as IP packet with 347 * ip_proto = ip->ip_p and s_port, d_port set to zero. 348 * I know, it looks like bug. But I don't want to re-implement 349 * ip packet assebmling here. Anyway, (in)famous trafd works this way - 350 * and nobody complains yet :) 351 */ 352 if(ip->ip_off & htons(IP_OFFMASK)) 353 return (0); 354 355 /* skip IP header */ 356 m_adj(*m, hlen); 357 358 switch(r->r_ip_p) { 359 case IPPROTO_TCP: 360 { 361 register struct tcphdr *tcp; 362 363 /* verify that packet is not truncated */ 364 if (CHECK_MLEN(*m, sizeof(struct tcphdr))) 365 ERROUT(EINVAL); 366 367 if (CHECK_PULLUP(*m, sizeof(struct tcphdr))) 368 ERROUT(ENOBUFS); 369 370 tcp = mtod(*m, struct tcphdr*); 371 r->r_sport = tcp->th_sport; 372 r->r_dport = tcp->th_dport; 373 *tcp_flags = tcp->th_flags; 374 break; 375 } 376 case IPPROTO_UDP: 377 /* verify that packet is not truncated */ 378 if (CHECK_MLEN(*m, sizeof(struct udphdr))) 379 ERROUT(EINVAL); 380 381 if (CHECK_PULLUP(*m, sizeof(struct udphdr))) 382 ERROUT(ENOBUFS); 383 384 r->r_ports = *(mtod(*m, uint32_t *)); 385 break; 386 } 387 388 done: 389 return (error); 390 } 391 392 /* 393 * Non-static functions called from ng_netflow.c 394 */ 395 396 /* Allocate memory and set up flow cache */ 397 int 398 ng_netflow_cache_init(priv_p priv) 399 { 400 struct flow_entry *fle; 401 int i; 402 403 /* allocate cache */ 404 MALLOC(priv->cache, struct flow_entry *, 405 CACHESIZE * sizeof(struct flow_entry), 406 M_NETFLOW, M_WAITOK | M_ZERO); 407 408 if (priv->cache == NULL) 409 return (ENOMEM); 410 411 /* allocate hash */ 412 MALLOC(priv->hash, struct flow_hash_entry *, 413 NBUCKETS * sizeof(struct flow_hash_entry), 414 M_NETFLOW, M_WAITOK | M_ZERO); 415 416 if (priv->hash == NULL) { 417 FREE(priv->cache, M_NETFLOW); 418 return (ENOMEM); 419 } 420 421 TAILQ_INIT(&priv->work_queue); 422 SLIST_INIT(&priv->free_list); 423 SLIST_INIT(&priv->expire_list); 424 425 mtx_init(&priv->work_mtx, "ng_netflow cache mutex", NULL, MTX_DEF); 426 mtx_init(&priv->free_mtx, "ng_netflow free mutex", NULL, MTX_DEF); 427 mtx_init(&priv->expire_mtx, "ng_netflow expire mutex", NULL, MTX_DEF); 428 429 /* build free list */ 430 for (i = 0, fle = priv->cache; i < CACHESIZE; i++, fle++) 431 SLIST_INSERT_HEAD(&priv->free_list, fle, fle_free); 432 433 priv->info.nfinfo_free = CACHESIZE; 434 435 return (0); 436 } 437 438 /* Free all flow cache memory. Called from node close method. */ 439 void 440 ng_netflow_cache_flush(priv_p priv) 441 { 442 register struct flow_entry *fle; 443 int i; 444 445 /* 446 * We are going to free probably billable data. 447 * Expire everything before freeing it. 448 * No locking is required since callout is already drained. 449 */ 450 451 for (i = 0, fle = priv->cache; i < CACHESIZE; i++, fle++) 452 if (!ISFREE(fle)) 453 /* ignore errors now */ 454 (void )export_add(priv, fle); 455 456 mtx_destroy(&priv->work_mtx); 457 mtx_destroy(&priv->free_mtx); 458 mtx_destroy(&priv->expire_mtx); 459 460 /* free hash memory */ 461 if (priv->hash) 462 FREE(priv->hash, M_NETFLOW); 463 464 /* free flow cache */ 465 if (priv->cache) 466 FREE(priv->cache, M_NETFLOW); 467 468 } 469 470 /* Insert packet from &m into flow cache. */ 471 int 472 ng_netflow_flow_add(priv_p priv, struct mbuf **m, iface_p iface) 473 { 474 struct flow_hash_entry *h = priv->hash; 475 register struct flow_entry *fle; 476 struct flow_rec r; 477 int plen; 478 int error = 0; 479 uint32_t slot; 480 uint8_t tcp_flags = 0; 481 482 /* Try to fill *rec */ 483 bzero(&r, sizeof(r)); 484 if ((error = make_flow_rec(m, &plen, &r, &tcp_flags, 485 iface->info.ifinfo_index))) 486 return (error); 487 488 slot = ip_hash(&r); 489 490 mtx_lock(&priv->work_mtx); 491 492 /* Update node statistics. */ 493 priv->info.nfinfo_packets ++; 494 priv->info.nfinfo_bytes += plen; 495 496 fle = hash_lookup(h, slot, &r); /* New flow entry or existent? */ 497 498 if (fle) { /* an existent entry */ 499 500 TAILQ_REMOVE(&priv->work_queue, fle, fle_work); 501 502 fle->f.bytes += plen; 503 fle->f.packets ++; 504 fle->f.tcp_flags |= tcp_flags; 505 fle->f.last = time_uptime; 506 507 /* 508 * We have the following reasons to expire flow in active way: 509 * - it hit active timeout 510 * - a TCP connection closed 511 * - it is going to overflow counter 512 */ 513 if (tcp_flags & TH_FIN || tcp_flags & TH_RST || AGED(fle) || 514 (fle->f.bytes >= (UINT_MAX - IF_MAXMTU)) ) 515 expire_flow(priv, fle); 516 else 517 TAILQ_INSERT_TAIL(&priv->work_queue, fle, fle_work); 518 519 mtx_unlock(&priv->work_mtx); 520 521 } else { /* a new flow entry */ 522 523 mtx_unlock(&priv->work_mtx); 524 return hash_insert(priv, slot, &r, plen); 525 526 } 527 528 mtx_assert(&priv->work_mtx, MA_NOTOWNED); 529 mtx_assert(&priv->expire_mtx, MA_NOTOWNED); 530 mtx_assert(&priv->free_mtx, MA_NOTOWNED); 531 532 return (0); 533 } 534 535 /* 536 * Return records from cache. netgraph(4) guarantees us that we 537 * are locked against ng_netflow_rcvdata(). However we can 538 * work with ng_netflow_expire() in parrallel. XXX: Is it dangerous? 539 * 540 * TODO: matching particular IP should be done in kernel, here. 541 */ 542 int 543 ng_netflow_flow_show(priv_p priv, uint32_t last, struct ng_mesg *resp) 544 { 545 struct flow_entry *fle; 546 struct ngnf_flows *data; 547 548 data = (struct ngnf_flows *)resp->data; 549 data->last = 0; 550 data->nentries = 0; 551 552 /* Check if this is a first run */ 553 if (last == 0) 554 fle = priv->cache; 555 else { 556 if (last > CACHESIZE-1) 557 return (EINVAL); 558 fle = priv->cache + last; 559 } 560 561 /* 562 * We will transfer not more than NREC_AT_ONCE. More data 563 * will come in next message. 564 * We send current stop point to userland, and userland should return 565 * it back to us. 566 */ 567 for (; last < CACHESIZE; fle++, last++) { 568 if (ISFREE(fle)) 569 continue; 570 bcopy(&fle->f, &(data->entries[data->nentries]), 571 sizeof(fle->f)); 572 data->nentries ++; 573 if (data->nentries == NREC_AT_ONCE) { 574 if (++last < CACHESIZE) 575 data->last = (++fle - priv->cache); 576 return (0); 577 } 578 } 579 580 return (0); 581 } 582 583 /* We have full datagram in privdata. Send it to export hook. */ 584 static int 585 export_send(priv_p priv) 586 { 587 struct netflow_v5_header *header = &priv->dgram.header; 588 struct timespec ts; 589 struct mbuf *m; 590 int error = 0; 591 int mlen; 592 593 header->sys_uptime = htonl(MILLIUPTIME(time_uptime)); 594 595 getnanotime(&ts); 596 header->unix_secs = htonl(ts.tv_sec); 597 header->unix_nsecs = htonl(ts.tv_nsec); 598 599 /* Flow sequence contains number of first record */ 600 header->flow_seq = htonl(priv->flow_seq - header->count); 601 602 mlen = sizeof(struct netflow_v5_header) + 603 sizeof(struct netflow_v5_record) * header->count; 604 605 header->count = htons(header->count); 606 if ((m = m_devget((caddr_t)header, mlen, 0, NULL, NULL)) == NULL) { 607 log(LOG_CRIT, "ng_netflow: m_devget() failed, losing export " 608 "dgram\n"); 609 header->count = 0; 610 return(ENOBUFS); 611 } 612 613 header->count = 0; 614 615 /* Giant is required in sosend() at this moment. */ 616 NET_LOCK_GIANT(); 617 NG_SEND_DATA_ONLY(error, priv->export, m); 618 NET_UNLOCK_GIANT(); 619 620 if (error) 621 NG_FREE_M(m); 622 623 return (error); 624 } 625 626 627 /* Create export datagram. */ 628 static int 629 export_add(priv_p priv, struct flow_entry *fle) 630 { 631 struct netflow_v5_header *header = &priv->dgram.header; 632 struct netflow_v5_record *rec; 633 634 if (header->count == 0 ) { /* first record */ 635 rec = &priv->dgram.r[0]; 636 header->count = 1; 637 } else { /* continue filling datagram */ 638 rec = &priv->dgram.r[header->count]; 639 header->count ++; 640 } 641 642 /* Fill in export record */ 643 rec->src_addr = fle->f.r.r_src.s_addr; 644 rec->dst_addr = fle->f.r.r_dst.s_addr; 645 rec->next_hop = fle->f.next_hop.s_addr; 646 rec->i_ifx = htons(fle->f.fle_i_ifx); 647 rec->o_ifx = htons(fle->f.fle_o_ifx); 648 rec->packets = htonl(fle->f.packets); 649 rec->octets = htonl(fle->f.bytes); 650 rec->first = htonl(MILLIUPTIME(fle->f.first)); 651 rec->last = htonl(MILLIUPTIME(fle->f.last)); 652 rec->s_port = fle->f.r.r_sport; 653 rec->d_port = fle->f.r.r_dport; 654 rec->flags = fle->f.tcp_flags; 655 rec->prot = fle->f.r.r_ip_p; 656 rec->tos = fle->f.r.r_tos; 657 rec->dst_mask = fle->f.dst_mask; 658 rec->src_mask = fle->f.src_mask; 659 660 priv->flow_seq++; 661 662 if (header->count == NETFLOW_V5_MAX_RECORDS) /* end of datagram */ 663 return export_send(priv); 664 665 return (0); 666 } 667 668 /* Periodic flow expiry run. */ 669 void 670 ng_netflow_expire(void *arg) 671 { 672 register struct flow_entry *fle, *fle1; 673 priv_p priv = (priv_p )arg; 674 uint32_t used; 675 int error = 0; 676 677 /* First pack actively expired entries */ 678 mtx_lock(&priv->expire_mtx); 679 while (!SLIST_EMPTY(&(priv->expire_list))) { 680 fle = SLIST_FIRST(&(priv->expire_list)); 681 SLIST_REMOVE_HEAD(&(priv->expire_list), fle_free); 682 mtx_unlock(&priv->expire_mtx); 683 684 /* 685 * While we have dropped the lock, expire_flow() may 686 * insert another flow into top of the list. 687 * This is not harmful for us, since we have already 688 * detached our own. 689 */ 690 691 if ((error = export_add(priv, fle)) != 0) 692 log(LOG_CRIT, "ng_netflow: export_add() failed: %u\n", 693 error); 694 (void )free_flow(priv, fle); 695 696 mtx_lock(&priv->expire_mtx); 697 } 698 mtx_unlock(&priv->expire_mtx); 699 700 NGNF_GETUSED(priv, used); 701 mtx_lock(&priv->work_mtx); 702 TAILQ_FOREACH_SAFE(fle, &(priv->work_queue), fle_work, fle1) { 703 /* 704 * When cache size has not reached CACHELOWAT yet, we keep 705 * both inactive and active flows in cache. Doing this, we 706 * reduce number of exports, since many inactive flows may 707 * wake up and continue their life. However, we make an 708 * exclusion for scans. It is very rare situation that 709 * inactive 1-packet flow will wake up. 710 * When cache has reached CACHELOWAT, we expire all inactive 711 * flows, until cache gets to a sane size. 712 */ 713 if (used <= CACHELOWAT && !INACTIVE(fle)) 714 goto finish; 715 716 if ((INACTIVE(fle) && (SMALL(fle) || (used > CACHELOWAT))) || 717 AGED(fle)) { 718 719 /* Detach flow entry from cache */ 720 LIST_REMOVE(fle, fle_hash); 721 TAILQ_REMOVE(&priv->work_queue, fle, fle_work); 722 723 /* 724 * While we are sending to collector, unlock cache. 725 * XXX: it can happen, however with a small probability, 726 * that item, we are holding now, can be moved to the 727 * top of flow cache by node thread. In this case our 728 * expire thread stops checking. Since this is not 729 * fatal we will just ignore it now. 730 */ 731 mtx_unlock(&priv->work_mtx); 732 733 if ((error = export_add(priv, fle)) != 0) 734 log(LOG_CRIT, "ng_netflow: export_add() " 735 "failed: %u\n", error); 736 737 used = free_flow(priv, fle); 738 739 mtx_lock(&priv->work_mtx); 740 } 741 } 742 743 finish: 744 mtx_unlock(&priv->work_mtx); 745 746 mtx_assert(&priv->expire_mtx, MA_NOTOWNED); 747 mtx_assert(&priv->free_mtx, MA_NOTOWNED); 748 749 /* schedule next expire */ 750 callout_reset(&priv->exp_callout, (1*hz), &ng_netflow_expire, 751 (void *)priv); 752 753 } 754