12529f56eSJonathan T. Looney /*- 22529f56eSJonathan T. Looney * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 32529f56eSJonathan T. Looney * 42529f56eSJonathan T. Looney * Copyright (c) 2016-2018 52529f56eSJonathan T. Looney * Netflix Inc. All rights reserved. 62529f56eSJonathan T. Looney * 72529f56eSJonathan T. Looney * Redistribution and use in source and binary forms, with or without 82529f56eSJonathan T. Looney * modification, are permitted provided that the following conditions 92529f56eSJonathan T. Looney * are met: 102529f56eSJonathan T. Looney * 1. Redistributions of source code must retain the above copyright 112529f56eSJonathan T. Looney * notice, this list of conditions and the following disclaimer. 122529f56eSJonathan T. Looney * 2. Redistributions in binary form must reproduce the above copyright 132529f56eSJonathan T. Looney * notice, this list of conditions and the following disclaimer in the 142529f56eSJonathan T. Looney * documentation and/or other materials provided with the distribution. 152529f56eSJonathan T. Looney * 162529f56eSJonathan T. Looney * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 172529f56eSJonathan T. Looney * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 182529f56eSJonathan T. Looney * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 192529f56eSJonathan T. Looney * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 202529f56eSJonathan T. Looney * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 212529f56eSJonathan T. Looney * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 222529f56eSJonathan T. Looney * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 232529f56eSJonathan T. Looney * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 242529f56eSJonathan T. Looney * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 252529f56eSJonathan T. Looney * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 262529f56eSJonathan T. Looney * SUCH DAMAGE. 272529f56eSJonathan T. Looney * 282529f56eSJonathan T. Looney */ 292529f56eSJonathan T. Looney 302529f56eSJonathan T. Looney #include <sys/cdefs.h> 312529f56eSJonathan T. Looney __FBSDID("$FreeBSD$"); 322529f56eSJonathan T. Looney 332529f56eSJonathan T. Looney #include <sys/param.h> 342529f56eSJonathan T. Looney #include <sys/kernel.h> 352529f56eSJonathan T. Looney #include <sys/lock.h> 362529f56eSJonathan T. Looney #include <sys/malloc.h> 372529f56eSJonathan T. Looney #include <sys/mutex.h> 382529f56eSJonathan T. Looney #include <sys/queue.h> 392529f56eSJonathan T. Looney #include <sys/refcount.h> 402529f56eSJonathan T. Looney #include <sys/rwlock.h> 412529f56eSJonathan T. Looney #include <sys/socket.h> 422529f56eSJonathan T. Looney #include <sys/socketvar.h> 432529f56eSJonathan T. Looney #include <sys/sysctl.h> 442529f56eSJonathan T. Looney #include <sys/tree.h> 452529f56eSJonathan T. Looney #include <sys/counter.h> 462529f56eSJonathan T. Looney 472529f56eSJonathan T. Looney #include <dev/tcp_log/tcp_log_dev.h> 482529f56eSJonathan T. Looney 492529f56eSJonathan T. Looney #include <net/if.h> 502529f56eSJonathan T. Looney #include <net/if_var.h> 512529f56eSJonathan T. Looney #include <net/vnet.h> 522529f56eSJonathan T. Looney 532529f56eSJonathan T. Looney #include <netinet/in.h> 542529f56eSJonathan T. Looney #include <netinet/in_pcb.h> 552529f56eSJonathan T. Looney #include <netinet/in_var.h> 562529f56eSJonathan T. Looney #include <netinet/tcp_var.h> 572529f56eSJonathan T. Looney #include <netinet/tcp_log_buf.h> 582529f56eSJonathan T. Looney 592529f56eSJonathan T. Looney /* Default expiry time */ 602529f56eSJonathan T. Looney #define TCP_LOG_EXPIRE_TIME ((sbintime_t)60 * SBT_1S) 612529f56eSJonathan T. Looney 622529f56eSJonathan T. Looney /* Max interval at which to run the expiry timer */ 632529f56eSJonathan T. Looney #define TCP_LOG_EXPIRE_INTVL ((sbintime_t)5 * SBT_1S) 642529f56eSJonathan T. Looney 652529f56eSJonathan T. Looney bool tcp_log_verbose; 662529f56eSJonathan T. Looney static uma_zone_t tcp_log_bucket_zone, tcp_log_node_zone, tcp_log_zone; 672529f56eSJonathan T. Looney static int tcp_log_session_limit = TCP_LOG_BUF_DEFAULT_SESSION_LIMIT; 682529f56eSJonathan T. Looney static uint32_t tcp_log_version = TCP_LOG_BUF_VER; 692529f56eSJonathan T. Looney RB_HEAD(tcp_log_id_tree, tcp_log_id_bucket); 702529f56eSJonathan T. Looney static struct tcp_log_id_tree tcp_log_id_head; 712529f56eSJonathan T. Looney static STAILQ_HEAD(, tcp_log_id_node) tcp_log_expireq_head = 722529f56eSJonathan T. Looney STAILQ_HEAD_INITIALIZER(tcp_log_expireq_head); 732529f56eSJonathan T. Looney static struct mtx tcp_log_expireq_mtx; 742529f56eSJonathan T. Looney static struct callout tcp_log_expireq_callout; 75*9959c8b9SJonathan T. Looney static u_long tcp_log_auto_ratio = 0; 76*9959c8b9SJonathan T. Looney static volatile u_long tcp_log_auto_ratio_cur = 0; 772529f56eSJonathan T. Looney static uint32_t tcp_log_auto_mode = TCP_LOG_STATE_TAIL; 782529f56eSJonathan T. Looney static bool tcp_log_auto_all = false; 792529f56eSJonathan T. Looney 802529f56eSJonathan T. Looney RB_PROTOTYPE_STATIC(tcp_log_id_tree, tcp_log_id_bucket, tlb_rb, tcp_log_id_cmp) 812529f56eSJonathan T. Looney 822529f56eSJonathan T. Looney SYSCTL_NODE(_net_inet_tcp, OID_AUTO, bb, CTLFLAG_RW, 0, "TCP Black Box controls"); 832529f56eSJonathan T. Looney 842529f56eSJonathan T. Looney SYSCTL_BOOL(_net_inet_tcp_bb, OID_AUTO, log_verbose, CTLFLAG_RW, &tcp_log_verbose, 852529f56eSJonathan T. Looney 0, "Force verbose logging for TCP traces"); 862529f56eSJonathan T. Looney 872529f56eSJonathan T. Looney SYSCTL_INT(_net_inet_tcp_bb, OID_AUTO, log_session_limit, 882529f56eSJonathan T. Looney CTLFLAG_RW, &tcp_log_session_limit, 0, 892529f56eSJonathan T. Looney "Maximum number of events maintained for each TCP session"); 902529f56eSJonathan T. Looney 912529f56eSJonathan T. Looney SYSCTL_UMA_MAX(_net_inet_tcp_bb, OID_AUTO, log_global_limit, CTLFLAG_RW, 922529f56eSJonathan T. Looney &tcp_log_zone, "Maximum number of events maintained for all TCP sessions"); 932529f56eSJonathan T. Looney 942529f56eSJonathan T. Looney SYSCTL_UMA_CUR(_net_inet_tcp_bb, OID_AUTO, log_global_entries, CTLFLAG_RD, 952529f56eSJonathan T. Looney &tcp_log_zone, "Current number of events maintained for all TCP sessions"); 962529f56eSJonathan T. Looney 972529f56eSJonathan T. Looney SYSCTL_UMA_MAX(_net_inet_tcp_bb, OID_AUTO, log_id_limit, CTLFLAG_RW, 982529f56eSJonathan T. Looney &tcp_log_bucket_zone, "Maximum number of log IDs"); 992529f56eSJonathan T. Looney 1002529f56eSJonathan T. Looney SYSCTL_UMA_CUR(_net_inet_tcp_bb, OID_AUTO, log_id_entries, CTLFLAG_RD, 1012529f56eSJonathan T. Looney &tcp_log_bucket_zone, "Current number of log IDs"); 1022529f56eSJonathan T. Looney 1032529f56eSJonathan T. Looney SYSCTL_UMA_MAX(_net_inet_tcp_bb, OID_AUTO, log_id_tcpcb_limit, CTLFLAG_RW, 1042529f56eSJonathan T. Looney &tcp_log_node_zone, "Maximum number of tcpcbs with log IDs"); 1052529f56eSJonathan T. Looney 1062529f56eSJonathan T. Looney SYSCTL_UMA_CUR(_net_inet_tcp_bb, OID_AUTO, log_id_tcpcb_entries, CTLFLAG_RD, 1072529f56eSJonathan T. Looney &tcp_log_node_zone, "Current number of tcpcbs with log IDs"); 1082529f56eSJonathan T. Looney 1092529f56eSJonathan T. Looney SYSCTL_U32(_net_inet_tcp_bb, OID_AUTO, log_version, CTLFLAG_RD, &tcp_log_version, 1102529f56eSJonathan T. Looney 0, "Version of log formats exported"); 1112529f56eSJonathan T. Looney 112*9959c8b9SJonathan T. Looney SYSCTL_ULONG(_net_inet_tcp_bb, OID_AUTO, log_auto_ratio, CTLFLAG_RW, 1132529f56eSJonathan T. Looney &tcp_log_auto_ratio, 0, "Do auto capturing for 1 out of N sessions"); 1142529f56eSJonathan T. Looney 1152529f56eSJonathan T. Looney SYSCTL_U32(_net_inet_tcp_bb, OID_AUTO, log_auto_mode, CTLFLAG_RW, 1162529f56eSJonathan T. Looney &tcp_log_auto_mode, TCP_LOG_STATE_HEAD_AUTO, 1172529f56eSJonathan T. Looney "Logging mode for auto-selected sessions (default is TCP_LOG_STATE_HEAD_AUTO)"); 1182529f56eSJonathan T. Looney 1192529f56eSJonathan T. Looney SYSCTL_BOOL(_net_inet_tcp_bb, OID_AUTO, log_auto_all, CTLFLAG_RW, 1202529f56eSJonathan T. Looney &tcp_log_auto_all, false, 1212529f56eSJonathan T. Looney "Auto-select from all sessions (rather than just those with IDs)"); 1222529f56eSJonathan T. Looney 1232529f56eSJonathan T. Looney #ifdef TCPLOG_DEBUG_COUNTERS 1242529f56eSJonathan T. Looney counter_u64_t tcp_log_queued; 1252529f56eSJonathan T. Looney counter_u64_t tcp_log_que_fail1; 1262529f56eSJonathan T. Looney counter_u64_t tcp_log_que_fail2; 1272529f56eSJonathan T. Looney counter_u64_t tcp_log_que_fail3; 1282529f56eSJonathan T. Looney counter_u64_t tcp_log_que_fail4; 1292529f56eSJonathan T. Looney counter_u64_t tcp_log_que_fail5; 1302529f56eSJonathan T. Looney counter_u64_t tcp_log_que_copyout; 1312529f56eSJonathan T. Looney counter_u64_t tcp_log_que_read; 1322529f56eSJonathan T. Looney counter_u64_t tcp_log_que_freed; 1332529f56eSJonathan T. Looney 1342529f56eSJonathan T. Looney SYSCTL_COUNTER_U64(_net_inet_tcp_bb, OID_AUTO, queued, CTLFLAG_RD, 1352529f56eSJonathan T. Looney &tcp_log_queued, "Number of entries queued"); 1362529f56eSJonathan T. Looney SYSCTL_COUNTER_U64(_net_inet_tcp_bb, OID_AUTO, fail1, CTLFLAG_RD, 1372529f56eSJonathan T. Looney &tcp_log_que_fail1, "Number of entries queued but fail 1"); 1382529f56eSJonathan T. Looney SYSCTL_COUNTER_U64(_net_inet_tcp_bb, OID_AUTO, fail2, CTLFLAG_RD, 1392529f56eSJonathan T. Looney &tcp_log_que_fail2, "Number of entries queued but fail 2"); 1402529f56eSJonathan T. Looney SYSCTL_COUNTER_U64(_net_inet_tcp_bb, OID_AUTO, fail3, CTLFLAG_RD, 1412529f56eSJonathan T. Looney &tcp_log_que_fail3, "Number of entries queued but fail 3"); 1422529f56eSJonathan T. Looney SYSCTL_COUNTER_U64(_net_inet_tcp_bb, OID_AUTO, fail4, CTLFLAG_RD, 1432529f56eSJonathan T. Looney &tcp_log_que_fail4, "Number of entries queued but fail 4"); 1442529f56eSJonathan T. Looney SYSCTL_COUNTER_U64(_net_inet_tcp_bb, OID_AUTO, fail5, CTLFLAG_RD, 1452529f56eSJonathan T. Looney &tcp_log_que_fail5, "Number of entries queued but fail 4"); 1462529f56eSJonathan T. Looney SYSCTL_COUNTER_U64(_net_inet_tcp_bb, OID_AUTO, copyout, CTLFLAG_RD, 1472529f56eSJonathan T. Looney &tcp_log_que_copyout, "Number of entries copied out"); 1482529f56eSJonathan T. Looney SYSCTL_COUNTER_U64(_net_inet_tcp_bb, OID_AUTO, read, CTLFLAG_RD, 1492529f56eSJonathan T. Looney &tcp_log_que_read, "Number of entries read from the queue"); 1502529f56eSJonathan T. Looney SYSCTL_COUNTER_U64(_net_inet_tcp_bb, OID_AUTO, freed, CTLFLAG_RD, 1512529f56eSJonathan T. Looney &tcp_log_que_freed, "Number of entries freed after reading"); 1522529f56eSJonathan T. Looney #endif 1532529f56eSJonathan T. Looney 1542529f56eSJonathan T. Looney #ifdef INVARIANTS 1552529f56eSJonathan T. Looney #define TCPLOG_DEBUG_RINGBUF 1562529f56eSJonathan T. Looney #endif 1572529f56eSJonathan T. Looney 1582529f56eSJonathan T. Looney struct tcp_log_mem 1592529f56eSJonathan T. Looney { 1602529f56eSJonathan T. Looney STAILQ_ENTRY(tcp_log_mem) tlm_queue; 1612529f56eSJonathan T. Looney struct tcp_log_buffer tlm_buf; 1622529f56eSJonathan T. Looney struct tcp_log_verbose tlm_v; 1632529f56eSJonathan T. Looney #ifdef TCPLOG_DEBUG_RINGBUF 1642529f56eSJonathan T. Looney volatile int tlm_refcnt; 1652529f56eSJonathan T. Looney #endif 1662529f56eSJonathan T. Looney }; 1672529f56eSJonathan T. Looney 1682529f56eSJonathan T. Looney /* 60 bytes for the header, + 16 bytes for padding */ 1692529f56eSJonathan T. Looney static uint8_t zerobuf[76]; 1702529f56eSJonathan T. Looney 1712529f56eSJonathan T. Looney /* 1722529f56eSJonathan T. Looney * Lock order: 1732529f56eSJonathan T. Looney * 1. TCPID_TREE 1742529f56eSJonathan T. Looney * 2. TCPID_BUCKET 1752529f56eSJonathan T. Looney * 3. INP 1762529f56eSJonathan T. Looney * 1772529f56eSJonathan T. Looney * Rules: 1782529f56eSJonathan T. Looney * A. You need a lock on the Tree to add/remove buckets. 1792529f56eSJonathan T. Looney * B. You need a lock on the bucket to add/remove nodes from the bucket. 1802529f56eSJonathan T. Looney * C. To change information in a node, you need the INP lock if the tln_closed 1812529f56eSJonathan T. Looney * field is false. Otherwise, you need the bucket lock. (Note that the 1822529f56eSJonathan T. Looney * tln_closed field can change at any point, so you need to recheck the 1832529f56eSJonathan T. Looney * entry after acquiring the INP lock.) 1842529f56eSJonathan T. Looney * D. To remove a node from the bucket, you must have that entry locked, 1852529f56eSJonathan T. Looney * according to the criteria of Rule C. Also, the node must not be on 1862529f56eSJonathan T. Looney * the expiry queue. 1872529f56eSJonathan T. Looney * E. The exception to C is the expiry queue fields, which are locked by 1882529f56eSJonathan T. Looney * the TCPLOG_EXPIREQ lock. 1892529f56eSJonathan T. Looney * 1902529f56eSJonathan T. Looney * Buckets have a reference count. Each node is a reference. Further, 1912529f56eSJonathan T. Looney * other callers may add reference counts to keep a bucket from disappearing. 1922529f56eSJonathan T. Looney * You can add a reference as long as you own a lock sufficient to keep the 1932529f56eSJonathan T. Looney * bucket from disappearing. For example, a common use is: 1942529f56eSJonathan T. Looney * a. Have a locked INP, but need to lock the TCPID_BUCKET. 1952529f56eSJonathan T. Looney * b. Add a refcount on the bucket. (Safe because the INP lock prevents 1962529f56eSJonathan T. Looney * the TCPID_BUCKET from going away.) 1972529f56eSJonathan T. Looney * c. Drop the INP lock. 1982529f56eSJonathan T. Looney * d. Acquire a lock on the TCPID_BUCKET. 1992529f56eSJonathan T. Looney * e. Acquire a lock on the INP. 2002529f56eSJonathan T. Looney * f. Drop the refcount on the bucket. 2012529f56eSJonathan T. Looney * (At this point, the bucket may disappear.) 2022529f56eSJonathan T. Looney * 2032529f56eSJonathan T. Looney * Expire queue lock: 2042529f56eSJonathan T. Looney * You can acquire this with either the bucket or INP lock. Don't reverse it. 2052529f56eSJonathan T. Looney * When the expire code has committed to freeing a node, it resets the expiry 2062529f56eSJonathan T. Looney * time to SBT_MAX. That is the signal to everyone else that they should 2072529f56eSJonathan T. Looney * leave that node alone. 2082529f56eSJonathan T. Looney */ 2092529f56eSJonathan T. Looney static struct rwlock tcp_id_tree_lock; 2102529f56eSJonathan T. Looney #define TCPID_TREE_WLOCK() rw_wlock(&tcp_id_tree_lock) 2112529f56eSJonathan T. Looney #define TCPID_TREE_RLOCK() rw_rlock(&tcp_id_tree_lock) 2122529f56eSJonathan T. Looney #define TCPID_TREE_UPGRADE() rw_try_upgrade(&tcp_id_tree_lock) 2132529f56eSJonathan T. Looney #define TCPID_TREE_WUNLOCK() rw_wunlock(&tcp_id_tree_lock) 2142529f56eSJonathan T. Looney #define TCPID_TREE_RUNLOCK() rw_runlock(&tcp_id_tree_lock) 2152529f56eSJonathan T. Looney #define TCPID_TREE_WLOCK_ASSERT() rw_assert(&tcp_id_tree_lock, RA_WLOCKED) 2162529f56eSJonathan T. Looney #define TCPID_TREE_RLOCK_ASSERT() rw_assert(&tcp_id_tree_lock, RA_RLOCKED) 2172529f56eSJonathan T. Looney #define TCPID_TREE_UNLOCK_ASSERT() rw_assert(&tcp_id_tree_lock, RA_UNLOCKED) 2182529f56eSJonathan T. Looney 2192529f56eSJonathan T. Looney #define TCPID_BUCKET_LOCK_INIT(tlb) mtx_init(&((tlb)->tlb_mtx), "tcp log id bucket", NULL, MTX_DEF) 2202529f56eSJonathan T. Looney #define TCPID_BUCKET_LOCK_DESTROY(tlb) mtx_destroy(&((tlb)->tlb_mtx)) 2212529f56eSJonathan T. Looney #define TCPID_BUCKET_LOCK(tlb) mtx_lock(&((tlb)->tlb_mtx)) 2222529f56eSJonathan T. Looney #define TCPID_BUCKET_UNLOCK(tlb) mtx_unlock(&((tlb)->tlb_mtx)) 2232529f56eSJonathan T. Looney #define TCPID_BUCKET_LOCK_ASSERT(tlb) mtx_assert(&((tlb)->tlb_mtx), MA_OWNED) 2242529f56eSJonathan T. Looney #define TCPID_BUCKET_UNLOCK_ASSERT(tlb) mtx_assert(&((tlb)->tlb_mtx), MA_NOTOWNED) 2252529f56eSJonathan T. Looney 2262529f56eSJonathan T. Looney #define TCPID_BUCKET_REF(tlb) refcount_acquire(&((tlb)->tlb_refcnt)) 2272529f56eSJonathan T. Looney #define TCPID_BUCKET_UNREF(tlb) refcount_release(&((tlb)->tlb_refcnt)) 2282529f56eSJonathan T. Looney 2292529f56eSJonathan T. Looney #define TCPLOG_EXPIREQ_LOCK() mtx_lock(&tcp_log_expireq_mtx) 2302529f56eSJonathan T. Looney #define TCPLOG_EXPIREQ_UNLOCK() mtx_unlock(&tcp_log_expireq_mtx) 2312529f56eSJonathan T. Looney 2322529f56eSJonathan T. Looney SLIST_HEAD(tcp_log_id_head, tcp_log_id_node); 2332529f56eSJonathan T. Looney 2342529f56eSJonathan T. Looney struct tcp_log_id_bucket 2352529f56eSJonathan T. Looney { 2362529f56eSJonathan T. Looney /* 2372529f56eSJonathan T. Looney * tlb_id must be first. This lets us use strcmp on 2382529f56eSJonathan T. Looney * (struct tcp_log_id_bucket *) and (char *) interchangeably. 2392529f56eSJonathan T. Looney */ 2402529f56eSJonathan T. Looney char tlb_id[TCP_LOG_ID_LEN]; 2412529f56eSJonathan T. Looney RB_ENTRY(tcp_log_id_bucket) tlb_rb; 2422529f56eSJonathan T. Looney struct tcp_log_id_head tlb_head; 2432529f56eSJonathan T. Looney struct mtx tlb_mtx; 2442529f56eSJonathan T. Looney volatile u_int tlb_refcnt; 2452529f56eSJonathan T. Looney }; 2462529f56eSJonathan T. Looney 2472529f56eSJonathan T. Looney struct tcp_log_id_node 2482529f56eSJonathan T. Looney { 2492529f56eSJonathan T. Looney SLIST_ENTRY(tcp_log_id_node) tln_list; 2502529f56eSJonathan T. Looney STAILQ_ENTRY(tcp_log_id_node) tln_expireq; /* Locked by the expireq lock */ 2512529f56eSJonathan T. Looney sbintime_t tln_expiretime; /* Locked by the expireq lock */ 2522529f56eSJonathan T. Looney 2532529f56eSJonathan T. Looney /* 2542529f56eSJonathan T. Looney * If INP is NULL, that means the connection has closed. We've 2552529f56eSJonathan T. Looney * saved the connection endpoint information and the log entries 2562529f56eSJonathan T. Looney * in the tln_ie and tln_entries members. We've also saved a pointer 2572529f56eSJonathan T. Looney * to the enclosing bucket here. If INP is not NULL, the information is 2582529f56eSJonathan T. Looney * in the PCB and not here. 2592529f56eSJonathan T. Looney */ 2602529f56eSJonathan T. Looney struct inpcb *tln_inp; 2612529f56eSJonathan T. Looney struct tcpcb *tln_tp; 2622529f56eSJonathan T. Looney struct tcp_log_id_bucket *tln_bucket; 2632529f56eSJonathan T. Looney struct in_endpoints tln_ie; 2642529f56eSJonathan T. Looney struct tcp_log_stailq tln_entries; 2652529f56eSJonathan T. Looney int tln_count; 2662529f56eSJonathan T. Looney volatile int tln_closed; 2672529f56eSJonathan T. Looney uint8_t tln_af; 2682529f56eSJonathan T. Looney }; 2692529f56eSJonathan T. Looney 2702529f56eSJonathan T. Looney enum tree_lock_state { 2712529f56eSJonathan T. Looney TREE_UNLOCKED = 0, 2722529f56eSJonathan T. Looney TREE_RLOCKED, 2732529f56eSJonathan T. Looney TREE_WLOCKED, 2742529f56eSJonathan T. Looney }; 2752529f56eSJonathan T. Looney 2762529f56eSJonathan T. Looney /* Do we want to select this session for auto-logging? */ 2772529f56eSJonathan T. Looney static __inline bool 2782529f56eSJonathan T. Looney tcp_log_selectauto(void) 2792529f56eSJonathan T. Looney { 2802529f56eSJonathan T. Looney 2812529f56eSJonathan T. Looney /* 2822529f56eSJonathan T. Looney * If we are doing auto-capturing, figure out whether we will capture 2832529f56eSJonathan T. Looney * this session. 2842529f56eSJonathan T. Looney */ 2852529f56eSJonathan T. Looney if (tcp_log_auto_ratio && 286*9959c8b9SJonathan T. Looney (atomic_fetchadd_long(&tcp_log_auto_ratio_cur, 1) % 2872529f56eSJonathan T. Looney tcp_log_auto_ratio) == 0) 2882529f56eSJonathan T. Looney return (true); 2892529f56eSJonathan T. Looney return (false); 2902529f56eSJonathan T. Looney } 2912529f56eSJonathan T. Looney 2922529f56eSJonathan T. Looney static __inline int 2932529f56eSJonathan T. Looney tcp_log_id_cmp(struct tcp_log_id_bucket *a, struct tcp_log_id_bucket *b) 2942529f56eSJonathan T. Looney { 2952529f56eSJonathan T. Looney KASSERT(a != NULL, ("tcp_log_id_cmp: argument a is unexpectedly NULL")); 2962529f56eSJonathan T. Looney KASSERT(b != NULL, ("tcp_log_id_cmp: argument b is unexpectedly NULL")); 2972529f56eSJonathan T. Looney return strncmp(a->tlb_id, b->tlb_id, TCP_LOG_ID_LEN); 2982529f56eSJonathan T. Looney } 2992529f56eSJonathan T. Looney 3002529f56eSJonathan T. Looney RB_GENERATE_STATIC(tcp_log_id_tree, tcp_log_id_bucket, tlb_rb, tcp_log_id_cmp) 3012529f56eSJonathan T. Looney 3022529f56eSJonathan T. Looney static __inline void 3032529f56eSJonathan T. Looney tcp_log_id_validate_tree_lock(int tree_locked) 3042529f56eSJonathan T. Looney { 3052529f56eSJonathan T. Looney 3062529f56eSJonathan T. Looney #ifdef INVARIANTS 3072529f56eSJonathan T. Looney switch (tree_locked) { 3082529f56eSJonathan T. Looney case TREE_WLOCKED: 3092529f56eSJonathan T. Looney TCPID_TREE_WLOCK_ASSERT(); 3102529f56eSJonathan T. Looney break; 3112529f56eSJonathan T. Looney case TREE_RLOCKED: 3122529f56eSJonathan T. Looney TCPID_TREE_RLOCK_ASSERT(); 3132529f56eSJonathan T. Looney break; 3142529f56eSJonathan T. Looney case TREE_UNLOCKED: 3152529f56eSJonathan T. Looney TCPID_TREE_UNLOCK_ASSERT(); 3162529f56eSJonathan T. Looney break; 3172529f56eSJonathan T. Looney default: 3182529f56eSJonathan T. Looney kassert_panic("%s:%d: unknown tree lock state", __func__, 3192529f56eSJonathan T. Looney __LINE__); 3202529f56eSJonathan T. Looney } 3212529f56eSJonathan T. Looney #endif 3222529f56eSJonathan T. Looney } 3232529f56eSJonathan T. Looney 3242529f56eSJonathan T. Looney static __inline void 3252529f56eSJonathan T. Looney tcp_log_remove_bucket(struct tcp_log_id_bucket *tlb) 3262529f56eSJonathan T. Looney { 3272529f56eSJonathan T. Looney 3282529f56eSJonathan T. Looney TCPID_TREE_WLOCK_ASSERT(); 3292529f56eSJonathan T. Looney KASSERT(SLIST_EMPTY(&tlb->tlb_head), 3302529f56eSJonathan T. Looney ("%s: Attempt to remove non-empty bucket", __func__)); 3312529f56eSJonathan T. Looney if (RB_REMOVE(tcp_log_id_tree, &tcp_log_id_head, tlb) == NULL) { 3322529f56eSJonathan T. Looney #ifdef INVARIANTS 3332529f56eSJonathan T. Looney kassert_panic("%s:%d: error removing element from tree", 3342529f56eSJonathan T. Looney __func__, __LINE__); 3352529f56eSJonathan T. Looney #endif 3362529f56eSJonathan T. Looney } 3372529f56eSJonathan T. Looney TCPID_BUCKET_LOCK_DESTROY(tlb); 3382529f56eSJonathan T. Looney uma_zfree(tcp_log_bucket_zone, tlb); 3392529f56eSJonathan T. Looney } 3402529f56eSJonathan T. Looney 3412529f56eSJonathan T. Looney /* 3422529f56eSJonathan T. Looney * Call with a referenced and locked bucket. 3432529f56eSJonathan T. Looney * Will return true if the bucket was freed; otherwise, false. 3442529f56eSJonathan T. Looney * tlb: The bucket to unreference. 3452529f56eSJonathan T. Looney * tree_locked: A pointer to the state of the tree lock. If the tree lock 3462529f56eSJonathan T. Looney * state changes, the function will update it. 3472529f56eSJonathan T. Looney * inp: If not NULL and the function needs to drop the inp lock to relock the 3482529f56eSJonathan T. Looney * tree, it will do so. (The caller must ensure inp will not become invalid, 3492529f56eSJonathan T. Looney * probably by holding a reference to it.) 3502529f56eSJonathan T. Looney */ 3512529f56eSJonathan T. Looney static bool 3522529f56eSJonathan T. Looney tcp_log_unref_bucket(struct tcp_log_id_bucket *tlb, int *tree_locked, 3532529f56eSJonathan T. Looney struct inpcb *inp) 3542529f56eSJonathan T. Looney { 3552529f56eSJonathan T. Looney 3562529f56eSJonathan T. Looney KASSERT(tlb != NULL, ("%s: called with NULL tlb", __func__)); 3572529f56eSJonathan T. Looney KASSERT(tree_locked != NULL, ("%s: called with NULL tree_locked", 3582529f56eSJonathan T. Looney __func__)); 3592529f56eSJonathan T. Looney 3602529f56eSJonathan T. Looney tcp_log_id_validate_tree_lock(*tree_locked); 3612529f56eSJonathan T. Looney 3622529f56eSJonathan T. Looney /* 3632529f56eSJonathan T. Looney * Did we hold the last reference on the tlb? If so, we may need 3642529f56eSJonathan T. Looney * to free it. (Note that we can realistically only execute the 3652529f56eSJonathan T. Looney * loop twice: once without a write lock and once with a write 3662529f56eSJonathan T. Looney * lock.) 3672529f56eSJonathan T. Looney */ 3682529f56eSJonathan T. Looney while (TCPID_BUCKET_UNREF(tlb)) { 3692529f56eSJonathan T. Looney /* 3702529f56eSJonathan T. Looney * We need a write lock on the tree to free this. 3712529f56eSJonathan T. Looney * If we can upgrade the tree lock, this is "easy". If we 3722529f56eSJonathan T. Looney * can't upgrade the tree lock, we need to do this the 3732529f56eSJonathan T. Looney * "hard" way: unwind all our locks and relock everything. 3742529f56eSJonathan T. Looney * In the meantime, anything could have changed. We even 3752529f56eSJonathan T. Looney * need to validate that we still need to free the bucket. 3762529f56eSJonathan T. Looney */ 3772529f56eSJonathan T. Looney if (*tree_locked == TREE_RLOCKED && TCPID_TREE_UPGRADE()) 3782529f56eSJonathan T. Looney *tree_locked = TREE_WLOCKED; 3792529f56eSJonathan T. Looney else if (*tree_locked != TREE_WLOCKED) { 3802529f56eSJonathan T. Looney TCPID_BUCKET_REF(tlb); 3812529f56eSJonathan T. Looney if (inp != NULL) 3822529f56eSJonathan T. Looney INP_WUNLOCK(inp); 3832529f56eSJonathan T. Looney TCPID_BUCKET_UNLOCK(tlb); 3842529f56eSJonathan T. Looney if (*tree_locked == TREE_RLOCKED) 3852529f56eSJonathan T. Looney TCPID_TREE_RUNLOCK(); 3862529f56eSJonathan T. Looney TCPID_TREE_WLOCK(); 3872529f56eSJonathan T. Looney *tree_locked = TREE_WLOCKED; 3882529f56eSJonathan T. Looney TCPID_BUCKET_LOCK(tlb); 3892529f56eSJonathan T. Looney if (inp != NULL) 3902529f56eSJonathan T. Looney INP_WLOCK(inp); 3912529f56eSJonathan T. Looney continue; 3922529f56eSJonathan T. Looney } 3932529f56eSJonathan T. Looney 3942529f56eSJonathan T. Looney /* 3952529f56eSJonathan T. Looney * We have an empty bucket and a write lock on the tree. 3962529f56eSJonathan T. Looney * Remove the empty bucket. 3972529f56eSJonathan T. Looney */ 3982529f56eSJonathan T. Looney tcp_log_remove_bucket(tlb); 3992529f56eSJonathan T. Looney return (true); 4002529f56eSJonathan T. Looney } 4012529f56eSJonathan T. Looney return (false); 4022529f56eSJonathan T. Looney } 4032529f56eSJonathan T. Looney 4042529f56eSJonathan T. Looney /* 4052529f56eSJonathan T. Looney * Call with a locked bucket. This function will release the lock on the 4062529f56eSJonathan T. Looney * bucket before returning. 4072529f56eSJonathan T. Looney * 4082529f56eSJonathan T. Looney * The caller is responsible for freeing the tp->t_lin/tln node! 4092529f56eSJonathan T. Looney * 4102529f56eSJonathan T. Looney * Note: one of tp or both tlb and tln must be supplied. 4112529f56eSJonathan T. Looney * 4122529f56eSJonathan T. Looney * inp: A pointer to the inp. If the function needs to drop the inp lock to 4132529f56eSJonathan T. Looney * acquire the tree write lock, it will do so. (The caller must ensure inp 4142529f56eSJonathan T. Looney * will not become invalid, probably by holding a reference to it.) 4152529f56eSJonathan T. Looney * tp: A pointer to the tcpcb. (optional; if specified, tlb and tln are ignored) 4162529f56eSJonathan T. Looney * tlb: A pointer to the bucket. (optional; ignored if tp is specified) 4172529f56eSJonathan T. Looney * tln: A pointer to the node. (optional; ignored if tp is specified) 4182529f56eSJonathan T. Looney * tree_locked: A pointer to the state of the tree lock. If the tree lock 4192529f56eSJonathan T. Looney * state changes, the function will update it. 4202529f56eSJonathan T. Looney * 4212529f56eSJonathan T. Looney * Will return true if the INP lock was reacquired; otherwise, false. 4222529f56eSJonathan T. Looney */ 4232529f56eSJonathan T. Looney static bool 4242529f56eSJonathan T. Looney tcp_log_remove_id_node(struct inpcb *inp, struct tcpcb *tp, 4252529f56eSJonathan T. Looney struct tcp_log_id_bucket *tlb, struct tcp_log_id_node *tln, 4262529f56eSJonathan T. Looney int *tree_locked) 4272529f56eSJonathan T. Looney { 4282529f56eSJonathan T. Looney int orig_tree_locked; 4292529f56eSJonathan T. Looney 4302529f56eSJonathan T. Looney KASSERT(tp != NULL || (tlb != NULL && tln != NULL), 4312529f56eSJonathan T. Looney ("%s: called with tp=%p, tlb=%p, tln=%p", __func__, 4322529f56eSJonathan T. Looney tp, tlb, tln)); 4332529f56eSJonathan T. Looney KASSERT(tree_locked != NULL, ("%s: called with NULL tree_locked", 4342529f56eSJonathan T. Looney __func__)); 4352529f56eSJonathan T. Looney 4362529f56eSJonathan T. Looney if (tp != NULL) { 4372529f56eSJonathan T. Looney tlb = tp->t_lib; 4382529f56eSJonathan T. Looney tln = tp->t_lin; 4392529f56eSJonathan T. Looney KASSERT(tlb != NULL, ("%s: unexpectedly NULL tlb", __func__)); 4402529f56eSJonathan T. Looney KASSERT(tln != NULL, ("%s: unexpectedly NULL tln", __func__)); 4412529f56eSJonathan T. Looney } 4422529f56eSJonathan T. Looney 4432529f56eSJonathan T. Looney tcp_log_id_validate_tree_lock(*tree_locked); 4442529f56eSJonathan T. Looney TCPID_BUCKET_LOCK_ASSERT(tlb); 4452529f56eSJonathan T. Looney 4462529f56eSJonathan T. Looney /* 4472529f56eSJonathan T. Looney * Remove the node, clear the log bucket and node from the TCPCB, and 4482529f56eSJonathan T. Looney * decrement the bucket refcount. In the process, if this is the 4492529f56eSJonathan T. Looney * last reference, the bucket will be freed. 4502529f56eSJonathan T. Looney */ 4512529f56eSJonathan T. Looney SLIST_REMOVE(&tlb->tlb_head, tln, tcp_log_id_node, tln_list); 4522529f56eSJonathan T. Looney if (tp != NULL) { 4532529f56eSJonathan T. Looney tp->t_lib = NULL; 4542529f56eSJonathan T. Looney tp->t_lin = NULL; 4552529f56eSJonathan T. Looney } 4562529f56eSJonathan T. Looney orig_tree_locked = *tree_locked; 4572529f56eSJonathan T. Looney if (!tcp_log_unref_bucket(tlb, tree_locked, inp)) 4582529f56eSJonathan T. Looney TCPID_BUCKET_UNLOCK(tlb); 4592529f56eSJonathan T. Looney return (*tree_locked != orig_tree_locked); 4602529f56eSJonathan T. Looney } 4612529f56eSJonathan T. Looney 4622529f56eSJonathan T. Looney #define RECHECK_INP_CLEAN(cleanup) do { \ 4632529f56eSJonathan T. Looney if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \ 4642529f56eSJonathan T. Looney rv = ECONNRESET; \ 4652529f56eSJonathan T. Looney cleanup; \ 4662529f56eSJonathan T. Looney goto done; \ 4672529f56eSJonathan T. Looney } \ 4682529f56eSJonathan T. Looney tp = intotcpcb(inp); \ 4692529f56eSJonathan T. Looney } while (0) 4702529f56eSJonathan T. Looney 4712529f56eSJonathan T. Looney #define RECHECK_INP() RECHECK_INP_CLEAN(/* noop */) 4722529f56eSJonathan T. Looney 4732529f56eSJonathan T. Looney static void 4742529f56eSJonathan T. Looney tcp_log_grow_tlb(char *tlb_id, struct tcpcb *tp) 4752529f56eSJonathan T. Looney { 4762529f56eSJonathan T. Looney 4772529f56eSJonathan T. Looney INP_WLOCK_ASSERT(tp->t_inpcb); 4782529f56eSJonathan T. Looney 4792529f56eSJonathan T. Looney #ifdef NETFLIX 4802529f56eSJonathan T. Looney if (V_tcp_perconn_stats_enable == 2 && tp->t_stats == NULL) 4812529f56eSJonathan T. Looney (void)tcp_stats_sample_rollthedice(tp, tlb_id, strlen(tlb_id)); 4822529f56eSJonathan T. Looney #endif 4832529f56eSJonathan T. Looney } 4842529f56eSJonathan T. Looney 4852529f56eSJonathan T. Looney /* 4862529f56eSJonathan T. Looney * Set the TCP log ID for a TCPCB. 4872529f56eSJonathan T. Looney * Called with INPCB locked. Returns with it unlocked. 4882529f56eSJonathan T. Looney */ 4892529f56eSJonathan T. Looney int 4902529f56eSJonathan T. Looney tcp_log_set_id(struct tcpcb *tp, char *id) 4912529f56eSJonathan T. Looney { 4922529f56eSJonathan T. Looney struct tcp_log_id_bucket *tlb, *tmp_tlb; 4932529f56eSJonathan T. Looney struct tcp_log_id_node *tln; 4942529f56eSJonathan T. Looney struct inpcb *inp; 4952529f56eSJonathan T. Looney int tree_locked, rv; 4962529f56eSJonathan T. Looney bool bucket_locked; 4972529f56eSJonathan T. Looney 4982529f56eSJonathan T. Looney tlb = NULL; 4992529f56eSJonathan T. Looney tln = NULL; 5002529f56eSJonathan T. Looney inp = tp->t_inpcb; 5012529f56eSJonathan T. Looney tree_locked = TREE_UNLOCKED; 5022529f56eSJonathan T. Looney bucket_locked = false; 5032529f56eSJonathan T. Looney 5042529f56eSJonathan T. Looney restart: 5052529f56eSJonathan T. Looney INP_WLOCK_ASSERT(inp); 5062529f56eSJonathan T. Looney 5072529f56eSJonathan T. Looney /* See if the ID is unchanged. */ 5082529f56eSJonathan T. Looney if ((tp->t_lib != NULL && !strcmp(tp->t_lib->tlb_id, id)) || 5092529f56eSJonathan T. Looney (tp->t_lib == NULL && *id == 0)) { 5102529f56eSJonathan T. Looney rv = 0; 5112529f56eSJonathan T. Looney goto done; 5122529f56eSJonathan T. Looney } 5132529f56eSJonathan T. Looney 5142529f56eSJonathan T. Looney /* 5152529f56eSJonathan T. Looney * If the TCPCB had a previous ID, we need to extricate it from 5162529f56eSJonathan T. Looney * the previous list. 5172529f56eSJonathan T. Looney * 5182529f56eSJonathan T. Looney * Drop the TCPCB lock and lock the tree and the bucket. 5192529f56eSJonathan T. Looney * Because this is called in the socket context, we (theoretically) 5202529f56eSJonathan T. Looney * don't need to worry about the INPCB completely going away 5212529f56eSJonathan T. Looney * while we are gone. 5222529f56eSJonathan T. Looney */ 5232529f56eSJonathan T. Looney if (tp->t_lib != NULL) { 5242529f56eSJonathan T. Looney tlb = tp->t_lib; 5252529f56eSJonathan T. Looney TCPID_BUCKET_REF(tlb); 5262529f56eSJonathan T. Looney INP_WUNLOCK(inp); 5272529f56eSJonathan T. Looney 5282529f56eSJonathan T. Looney if (tree_locked == TREE_UNLOCKED) { 5292529f56eSJonathan T. Looney TCPID_TREE_RLOCK(); 5302529f56eSJonathan T. Looney tree_locked = TREE_RLOCKED; 5312529f56eSJonathan T. Looney } 5322529f56eSJonathan T. Looney TCPID_BUCKET_LOCK(tlb); 5332529f56eSJonathan T. Looney bucket_locked = true; 5342529f56eSJonathan T. Looney INP_WLOCK(inp); 5352529f56eSJonathan T. Looney 5362529f56eSJonathan T. Looney /* 5372529f56eSJonathan T. Looney * Unreference the bucket. If our bucket went away, it is no 5382529f56eSJonathan T. Looney * longer locked or valid. 5392529f56eSJonathan T. Looney */ 5402529f56eSJonathan T. Looney if (tcp_log_unref_bucket(tlb, &tree_locked, inp)) { 5412529f56eSJonathan T. Looney bucket_locked = false; 5422529f56eSJonathan T. Looney tlb = NULL; 5432529f56eSJonathan T. Looney } 5442529f56eSJonathan T. Looney 5452529f56eSJonathan T. Looney /* Validate the INP. */ 5462529f56eSJonathan T. Looney RECHECK_INP(); 5472529f56eSJonathan T. Looney 5482529f56eSJonathan T. Looney /* 5492529f56eSJonathan T. Looney * Evaluate whether the bucket changed while we were unlocked. 5502529f56eSJonathan T. Looney * 5512529f56eSJonathan T. Looney * Possible scenarios here: 5522529f56eSJonathan T. Looney * 1. Bucket is unchanged and the same one we started with. 5532529f56eSJonathan T. Looney * 2. The TCPCB no longer has a bucket and our bucket was 5542529f56eSJonathan T. Looney * freed. 5552529f56eSJonathan T. Looney * 3. The TCPCB has a new bucket, whether ours was freed. 5562529f56eSJonathan T. Looney * 4. The TCPCB no longer has a bucket and our bucket was 5572529f56eSJonathan T. Looney * not freed. 5582529f56eSJonathan T. Looney * 5592529f56eSJonathan T. Looney * In cases 2-4, we will start over. In case 1, we will 5602529f56eSJonathan T. Looney * proceed here to remove the bucket. 5612529f56eSJonathan T. Looney */ 5622529f56eSJonathan T. Looney if (tlb == NULL || tp->t_lib != tlb) { 5632529f56eSJonathan T. Looney KASSERT(bucket_locked || tlb == NULL, 5642529f56eSJonathan T. Looney ("%s: bucket_locked (%d) and tlb (%p) are " 5652529f56eSJonathan T. Looney "inconsistent", __func__, bucket_locked, tlb)); 5662529f56eSJonathan T. Looney 5672529f56eSJonathan T. Looney if (bucket_locked) { 5682529f56eSJonathan T. Looney TCPID_BUCKET_UNLOCK(tlb); 5692529f56eSJonathan T. Looney bucket_locked = false; 5702529f56eSJonathan T. Looney tlb = NULL; 5712529f56eSJonathan T. Looney } 5722529f56eSJonathan T. Looney goto restart; 5732529f56eSJonathan T. Looney } 5742529f56eSJonathan T. Looney 5752529f56eSJonathan T. Looney /* 5762529f56eSJonathan T. Looney * Store the (struct tcp_log_id_node) for reuse. Then, remove 5772529f56eSJonathan T. Looney * it from the bucket. In the process, we may end up relocking. 5782529f56eSJonathan T. Looney * If so, we need to validate that the INP is still valid, and 5792529f56eSJonathan T. Looney * the TCPCB entries match we expect. 5802529f56eSJonathan T. Looney * 5812529f56eSJonathan T. Looney * We will clear tlb and change the bucket_locked state just 5822529f56eSJonathan T. Looney * before calling tcp_log_remove_id_node(), since that function 5832529f56eSJonathan T. Looney * will unlock the bucket. 5842529f56eSJonathan T. Looney */ 5852529f56eSJonathan T. Looney if (tln != NULL) 5862529f56eSJonathan T. Looney uma_zfree(tcp_log_node_zone, tln); 5872529f56eSJonathan T. Looney tln = tp->t_lin; 5882529f56eSJonathan T. Looney tlb = NULL; 5892529f56eSJonathan T. Looney bucket_locked = false; 5902529f56eSJonathan T. Looney if (tcp_log_remove_id_node(inp, tp, NULL, NULL, &tree_locked)) { 5912529f56eSJonathan T. Looney RECHECK_INP(); 5922529f56eSJonathan T. Looney 5932529f56eSJonathan T. Looney /* 5942529f56eSJonathan T. Looney * If the TCPCB moved to a new bucket while we had 5952529f56eSJonathan T. Looney * dropped the lock, restart. 5962529f56eSJonathan T. Looney */ 5972529f56eSJonathan T. Looney if (tp->t_lib != NULL || tp->t_lin != NULL) 5982529f56eSJonathan T. Looney goto restart; 5992529f56eSJonathan T. Looney } 6002529f56eSJonathan T. Looney 6012529f56eSJonathan T. Looney /* 6022529f56eSJonathan T. Looney * Yay! We successfully removed the TCPCB from its old 6032529f56eSJonathan T. Looney * bucket. Phew! 6042529f56eSJonathan T. Looney * 6052529f56eSJonathan T. Looney * On to bigger and better things... 6062529f56eSJonathan T. Looney */ 6072529f56eSJonathan T. Looney } 6082529f56eSJonathan T. Looney 6092529f56eSJonathan T. Looney /* At this point, the TCPCB should not be in any bucket. */ 6102529f56eSJonathan T. Looney KASSERT(tp->t_lib == NULL, ("%s: tp->t_lib is not NULL", __func__)); 6112529f56eSJonathan T. Looney 6122529f56eSJonathan T. Looney /* 6132529f56eSJonathan T. Looney * If the new ID is not empty, we need to now assign this TCPCB to a 6142529f56eSJonathan T. Looney * new bucket. 6152529f56eSJonathan T. Looney */ 6162529f56eSJonathan T. Looney if (*id) { 6172529f56eSJonathan T. Looney /* Get a new tln, if we don't already have one to reuse. */ 6182529f56eSJonathan T. Looney if (tln == NULL) { 6192529f56eSJonathan T. Looney tln = uma_zalloc(tcp_log_node_zone, M_NOWAIT | M_ZERO); 6202529f56eSJonathan T. Looney if (tln == NULL) { 6212529f56eSJonathan T. Looney rv = ENOBUFS; 6222529f56eSJonathan T. Looney goto done; 6232529f56eSJonathan T. Looney } 6242529f56eSJonathan T. Looney tln->tln_inp = inp; 6252529f56eSJonathan T. Looney tln->tln_tp = tp; 6262529f56eSJonathan T. Looney } 6272529f56eSJonathan T. Looney 6282529f56eSJonathan T. Looney /* 6292529f56eSJonathan T. Looney * Drop the INP lock for a bit. We don't need it, and dropping 6302529f56eSJonathan T. Looney * it prevents lock order reversals. 6312529f56eSJonathan T. Looney */ 6322529f56eSJonathan T. Looney INP_WUNLOCK(inp); 6332529f56eSJonathan T. Looney 6342529f56eSJonathan T. Looney /* Make sure we have at least a read lock on the tree. */ 6352529f56eSJonathan T. Looney tcp_log_id_validate_tree_lock(tree_locked); 6362529f56eSJonathan T. Looney if (tree_locked == TREE_UNLOCKED) { 6372529f56eSJonathan T. Looney TCPID_TREE_RLOCK(); 6382529f56eSJonathan T. Looney tree_locked = TREE_RLOCKED; 6392529f56eSJonathan T. Looney } 6402529f56eSJonathan T. Looney 6412529f56eSJonathan T. Looney refind: 6422529f56eSJonathan T. Looney /* 6432529f56eSJonathan T. Looney * Remember that we constructed (struct tcp_log_id_node) so 6442529f56eSJonathan T. Looney * we can safely cast the id to it for the purposes of finding. 6452529f56eSJonathan T. Looney */ 6462529f56eSJonathan T. Looney KASSERT(tlb == NULL, ("%s:%d tlb unexpectedly non-NULL", 6472529f56eSJonathan T. Looney __func__, __LINE__)); 6482529f56eSJonathan T. Looney tmp_tlb = RB_FIND(tcp_log_id_tree, &tcp_log_id_head, 6492529f56eSJonathan T. Looney (struct tcp_log_id_bucket *) id); 6502529f56eSJonathan T. Looney 6512529f56eSJonathan T. Looney /* 6522529f56eSJonathan T. Looney * If we didn't find a matching bucket, we need to add a new 6532529f56eSJonathan T. Looney * one. This requires a write lock. But, of course, we will 6542529f56eSJonathan T. Looney * need to recheck some things when we re-acquire the lock. 6552529f56eSJonathan T. Looney */ 6562529f56eSJonathan T. Looney if (tmp_tlb == NULL && tree_locked != TREE_WLOCKED) { 6572529f56eSJonathan T. Looney tree_locked = TREE_WLOCKED; 6582529f56eSJonathan T. Looney if (!TCPID_TREE_UPGRADE()) { 6592529f56eSJonathan T. Looney TCPID_TREE_RUNLOCK(); 6602529f56eSJonathan T. Looney TCPID_TREE_WLOCK(); 6612529f56eSJonathan T. Looney 6622529f56eSJonathan T. Looney /* 6632529f56eSJonathan T. Looney * The tree may have changed while we were 6642529f56eSJonathan T. Looney * unlocked. 6652529f56eSJonathan T. Looney */ 6662529f56eSJonathan T. Looney goto refind; 6672529f56eSJonathan T. Looney } 6682529f56eSJonathan T. Looney } 6692529f56eSJonathan T. Looney 6702529f56eSJonathan T. Looney /* If we need to add a new bucket, do it now. */ 6712529f56eSJonathan T. Looney if (tmp_tlb == NULL) { 6722529f56eSJonathan T. Looney /* Allocate new bucket. */ 6732529f56eSJonathan T. Looney tlb = uma_zalloc(tcp_log_bucket_zone, M_NOWAIT); 6742529f56eSJonathan T. Looney if (tlb == NULL) { 6752529f56eSJonathan T. Looney rv = ENOBUFS; 6762529f56eSJonathan T. Looney goto done_noinp; 6772529f56eSJonathan T. Looney } 6782529f56eSJonathan T. Looney 6792529f56eSJonathan T. Looney /* 6802529f56eSJonathan T. Looney * Copy the ID to the bucket. 6812529f56eSJonathan T. Looney * NB: Don't use strlcpy() unless you are sure 6822529f56eSJonathan T. Looney * we've always validated NULL termination. 6832529f56eSJonathan T. Looney * 6842529f56eSJonathan T. Looney * TODO: When I'm done writing this, see if we 6852529f56eSJonathan T. Looney * we have correctly validated NULL termination and 6862529f56eSJonathan T. Looney * can use strlcpy(). :-) 6872529f56eSJonathan T. Looney */ 6882529f56eSJonathan T. Looney strncpy(tlb->tlb_id, id, TCP_LOG_ID_LEN - 1); 6892529f56eSJonathan T. Looney tlb->tlb_id[TCP_LOG_ID_LEN - 1] = '\0'; 6902529f56eSJonathan T. Looney 6912529f56eSJonathan T. Looney /* 6922529f56eSJonathan T. Looney * Take the refcount for the first node and go ahead 6932529f56eSJonathan T. Looney * and lock this. Note that we zero the tlb_mtx 6942529f56eSJonathan T. Looney * structure, since 0xdeadc0de flips the right bits 6952529f56eSJonathan T. Looney * for the code to think that this mutex has already 6962529f56eSJonathan T. Looney * been initialized. :-( 6972529f56eSJonathan T. Looney */ 6982529f56eSJonathan T. Looney SLIST_INIT(&tlb->tlb_head); 6992529f56eSJonathan T. Looney refcount_init(&tlb->tlb_refcnt, 1); 7002529f56eSJonathan T. Looney memset(&tlb->tlb_mtx, 0, sizeof(struct mtx)); 7012529f56eSJonathan T. Looney TCPID_BUCKET_LOCK_INIT(tlb); 7022529f56eSJonathan T. Looney TCPID_BUCKET_LOCK(tlb); 7032529f56eSJonathan T. Looney bucket_locked = true; 7042529f56eSJonathan T. Looney 7052529f56eSJonathan T. Looney #define FREE_NEW_TLB() do { \ 7062529f56eSJonathan T. Looney TCPID_BUCKET_LOCK_DESTROY(tlb); \ 7072529f56eSJonathan T. Looney uma_zfree(tcp_log_bucket_zone, tlb); \ 7082529f56eSJonathan T. Looney bucket_locked = false; \ 7092529f56eSJonathan T. Looney tlb = NULL; \ 7102529f56eSJonathan T. Looney } while (0) 7112529f56eSJonathan T. Looney /* 7122529f56eSJonathan T. Looney * Relock the INP and make sure we are still 7132529f56eSJonathan T. Looney * unassigned. 7142529f56eSJonathan T. Looney */ 7152529f56eSJonathan T. Looney INP_WLOCK(inp); 7162529f56eSJonathan T. Looney RECHECK_INP_CLEAN(FREE_NEW_TLB()); 7172529f56eSJonathan T. Looney if (tp->t_lib != NULL) { 7182529f56eSJonathan T. Looney FREE_NEW_TLB(); 7192529f56eSJonathan T. Looney goto restart; 7202529f56eSJonathan T. Looney } 7212529f56eSJonathan T. Looney 7222529f56eSJonathan T. Looney /* Add the new bucket to the tree. */ 7232529f56eSJonathan T. Looney tmp_tlb = RB_INSERT(tcp_log_id_tree, &tcp_log_id_head, 7242529f56eSJonathan T. Looney tlb); 7252529f56eSJonathan T. Looney KASSERT(tmp_tlb == NULL, 7262529f56eSJonathan T. Looney ("%s: Unexpected conflicting bucket (%p) while " 7272529f56eSJonathan T. Looney "adding new bucket (%p)", __func__, tmp_tlb, tlb)); 7282529f56eSJonathan T. Looney 7292529f56eSJonathan T. Looney /* 7302529f56eSJonathan T. Looney * If we found a conflicting bucket, free the new 7312529f56eSJonathan T. Looney * one we made and fall through to use the existing 7322529f56eSJonathan T. Looney * bucket. 7332529f56eSJonathan T. Looney */ 7342529f56eSJonathan T. Looney if (tmp_tlb != NULL) { 7352529f56eSJonathan T. Looney FREE_NEW_TLB(); 7362529f56eSJonathan T. Looney INP_WUNLOCK(inp); 7372529f56eSJonathan T. Looney } 7382529f56eSJonathan T. Looney #undef FREE_NEW_TLB 7392529f56eSJonathan T. Looney } 7402529f56eSJonathan T. Looney 7412529f56eSJonathan T. Looney /* If we found an existing bucket, use it. */ 7422529f56eSJonathan T. Looney if (tmp_tlb != NULL) { 7432529f56eSJonathan T. Looney tlb = tmp_tlb; 7442529f56eSJonathan T. Looney TCPID_BUCKET_LOCK(tlb); 7452529f56eSJonathan T. Looney bucket_locked = true; 7462529f56eSJonathan T. Looney 7472529f56eSJonathan T. Looney /* 7482529f56eSJonathan T. Looney * Relock the INP and make sure we are still 7492529f56eSJonathan T. Looney * unassigned. 7502529f56eSJonathan T. Looney */ 7512529f56eSJonathan T. Looney INP_UNLOCK_ASSERT(inp); 7522529f56eSJonathan T. Looney INP_WLOCK(inp); 7532529f56eSJonathan T. Looney RECHECK_INP(); 7542529f56eSJonathan T. Looney if (tp->t_lib != NULL) { 7552529f56eSJonathan T. Looney TCPID_BUCKET_UNLOCK(tlb); 7562529f56eSJonathan T. Looney tlb = NULL; 7572529f56eSJonathan T. Looney goto restart; 7582529f56eSJonathan T. Looney } 7592529f56eSJonathan T. Looney 7602529f56eSJonathan T. Looney /* Take a reference on the bucket. */ 7612529f56eSJonathan T. Looney TCPID_BUCKET_REF(tlb); 7622529f56eSJonathan T. Looney } 7632529f56eSJonathan T. Looney 7642529f56eSJonathan T. Looney tcp_log_grow_tlb(tlb->tlb_id, tp); 7652529f56eSJonathan T. Looney 7662529f56eSJonathan T. Looney /* Add the new node to the list. */ 7672529f56eSJonathan T. Looney SLIST_INSERT_HEAD(&tlb->tlb_head, tln, tln_list); 7682529f56eSJonathan T. Looney tp->t_lib = tlb; 7692529f56eSJonathan T. Looney tp->t_lin = tln; 7702529f56eSJonathan T. Looney tln = NULL; 7712529f56eSJonathan T. Looney } 7722529f56eSJonathan T. Looney 7732529f56eSJonathan T. Looney rv = 0; 7742529f56eSJonathan T. Looney 7752529f56eSJonathan T. Looney done: 7762529f56eSJonathan T. Looney /* Unlock things, as needed, and return. */ 7772529f56eSJonathan T. Looney INP_WUNLOCK(inp); 7782529f56eSJonathan T. Looney done_noinp: 7792529f56eSJonathan T. Looney INP_UNLOCK_ASSERT(inp); 7802529f56eSJonathan T. Looney if (bucket_locked) { 7812529f56eSJonathan T. Looney TCPID_BUCKET_LOCK_ASSERT(tlb); 7822529f56eSJonathan T. Looney TCPID_BUCKET_UNLOCK(tlb); 7832529f56eSJonathan T. Looney } else if (tlb != NULL) 7842529f56eSJonathan T. Looney TCPID_BUCKET_UNLOCK_ASSERT(tlb); 7852529f56eSJonathan T. Looney if (tree_locked == TREE_WLOCKED) { 7862529f56eSJonathan T. Looney TCPID_TREE_WLOCK_ASSERT(); 7872529f56eSJonathan T. Looney TCPID_TREE_WUNLOCK(); 7882529f56eSJonathan T. Looney } else if (tree_locked == TREE_RLOCKED) { 7892529f56eSJonathan T. Looney TCPID_TREE_RLOCK_ASSERT(); 7902529f56eSJonathan T. Looney TCPID_TREE_RUNLOCK(); 7912529f56eSJonathan T. Looney } else 7922529f56eSJonathan T. Looney TCPID_TREE_UNLOCK_ASSERT(); 7932529f56eSJonathan T. Looney if (tln != NULL) 7942529f56eSJonathan T. Looney uma_zfree(tcp_log_node_zone, tln); 7952529f56eSJonathan T. Looney return (rv); 7962529f56eSJonathan T. Looney } 7972529f56eSJonathan T. Looney 7982529f56eSJonathan T. Looney /* 7992529f56eSJonathan T. Looney * Get the TCP log ID for a TCPCB. 8002529f56eSJonathan T. Looney * Called with INPCB locked. 8012529f56eSJonathan T. Looney * 'buf' must point to a buffer that is at least TCP_LOG_ID_LEN bytes long. 8022529f56eSJonathan T. Looney * Returns number of bytes copied. 8032529f56eSJonathan T. Looney */ 8042529f56eSJonathan T. Looney size_t 8052529f56eSJonathan T. Looney tcp_log_get_id(struct tcpcb *tp, char *buf) 8062529f56eSJonathan T. Looney { 8072529f56eSJonathan T. Looney size_t len; 8082529f56eSJonathan T. Looney 8092529f56eSJonathan T. Looney INP_LOCK_ASSERT(tp->t_inpcb); 8102529f56eSJonathan T. Looney if (tp->t_lib != NULL) { 8112529f56eSJonathan T. Looney len = strlcpy(buf, tp->t_lib->tlb_id, TCP_LOG_ID_LEN); 8122529f56eSJonathan T. Looney KASSERT(len < TCP_LOG_ID_LEN, 8132529f56eSJonathan T. Looney ("%s:%d: tp->t_lib->tlb_id too long (%zu)", 8142529f56eSJonathan T. Looney __func__, __LINE__, len)); 8152529f56eSJonathan T. Looney } else { 8162529f56eSJonathan T. Looney *buf = '\0'; 8172529f56eSJonathan T. Looney len = 0; 8182529f56eSJonathan T. Looney } 8192529f56eSJonathan T. Looney return (len); 8202529f56eSJonathan T. Looney } 8212529f56eSJonathan T. Looney 8222529f56eSJonathan T. Looney /* 8232529f56eSJonathan T. Looney * Get number of connections with the same log ID. 8242529f56eSJonathan T. Looney * Log ID is taken from given TCPCB. 8252529f56eSJonathan T. Looney * Called with INPCB locked. 8262529f56eSJonathan T. Looney */ 8272529f56eSJonathan T. Looney u_int 8282529f56eSJonathan T. Looney tcp_log_get_id_cnt(struct tcpcb *tp) 8292529f56eSJonathan T. Looney { 8302529f56eSJonathan T. Looney 8312529f56eSJonathan T. Looney INP_WLOCK_ASSERT(tp->t_inpcb); 8322529f56eSJonathan T. Looney return ((tp->t_lib == NULL) ? 0 : tp->t_lib->tlb_refcnt); 8332529f56eSJonathan T. Looney } 8342529f56eSJonathan T. Looney 8352529f56eSJonathan T. Looney #ifdef TCPLOG_DEBUG_RINGBUF 8362529f56eSJonathan T. Looney /* 8372529f56eSJonathan T. Looney * Functions/macros to increment/decrement reference count for a log 8382529f56eSJonathan T. Looney * entry. This should catch when we do a double-free/double-remove or 8392529f56eSJonathan T. Looney * a double-add. 8402529f56eSJonathan T. Looney */ 8412529f56eSJonathan T. Looney static inline void 8422529f56eSJonathan T. Looney _tcp_log_entry_refcnt_add(struct tcp_log_mem *log_entry, const char *func, 8432529f56eSJonathan T. Looney int line) 8442529f56eSJonathan T. Looney { 8452529f56eSJonathan T. Looney int refcnt; 8462529f56eSJonathan T. Looney 8472529f56eSJonathan T. Looney refcnt = atomic_fetchadd_int(&log_entry->tlm_refcnt, 1); 8482529f56eSJonathan T. Looney if (refcnt != 0) 8492529f56eSJonathan T. Looney panic("%s:%d: log_entry(%p)->tlm_refcnt is %d (expected 0)", 8502529f56eSJonathan T. Looney func, line, log_entry, refcnt); 8512529f56eSJonathan T. Looney } 8522529f56eSJonathan T. Looney #define tcp_log_entry_refcnt_add(l) \ 8532529f56eSJonathan T. Looney _tcp_log_entry_refcnt_add((l), __func__, __LINE__) 8542529f56eSJonathan T. Looney 8552529f56eSJonathan T. Looney static inline void 8562529f56eSJonathan T. Looney _tcp_log_entry_refcnt_rem(struct tcp_log_mem *log_entry, const char *func, 8572529f56eSJonathan T. Looney int line) 8582529f56eSJonathan T. Looney { 8592529f56eSJonathan T. Looney int refcnt; 8602529f56eSJonathan T. Looney 8612529f56eSJonathan T. Looney refcnt = atomic_fetchadd_int(&log_entry->tlm_refcnt, -1); 8622529f56eSJonathan T. Looney if (refcnt != 1) 8632529f56eSJonathan T. Looney panic("%s:%d: log_entry(%p)->tlm_refcnt is %d (expected 1)", 8642529f56eSJonathan T. Looney func, line, log_entry, refcnt); 8652529f56eSJonathan T. Looney } 8662529f56eSJonathan T. Looney #define tcp_log_entry_refcnt_rem(l) \ 8672529f56eSJonathan T. Looney _tcp_log_entry_refcnt_rem((l), __func__, __LINE__) 8682529f56eSJonathan T. Looney 8692529f56eSJonathan T. Looney #else /* !TCPLOG_DEBUG_RINGBUF */ 8702529f56eSJonathan T. Looney 8712529f56eSJonathan T. Looney #define tcp_log_entry_refcnt_add(l) 8722529f56eSJonathan T. Looney #define tcp_log_entry_refcnt_rem(l) 8732529f56eSJonathan T. Looney 8742529f56eSJonathan T. Looney #endif 8752529f56eSJonathan T. Looney 8762529f56eSJonathan T. Looney /* 8772529f56eSJonathan T. Looney * Cleanup after removing a log entry, but only decrement the count if we 8782529f56eSJonathan T. Looney * are running INVARIANTS. 8792529f56eSJonathan T. Looney */ 8802529f56eSJonathan T. Looney static inline void 8812529f56eSJonathan T. Looney tcp_log_free_log_common(struct tcp_log_mem *log_entry, int *count __unused) 8822529f56eSJonathan T. Looney { 8832529f56eSJonathan T. Looney 8842529f56eSJonathan T. Looney uma_zfree(tcp_log_zone, log_entry); 8852529f56eSJonathan T. Looney #ifdef INVARIANTS 8862529f56eSJonathan T. Looney (*count)--; 8872529f56eSJonathan T. Looney KASSERT(*count >= 0, 8882529f56eSJonathan T. Looney ("%s: count unexpectedly negative", __func__)); 8892529f56eSJonathan T. Looney #endif 8902529f56eSJonathan T. Looney } 8912529f56eSJonathan T. Looney 8922529f56eSJonathan T. Looney static void 8932529f56eSJonathan T. Looney tcp_log_free_entries(struct tcp_log_stailq *head, int *count) 8942529f56eSJonathan T. Looney { 8952529f56eSJonathan T. Looney struct tcp_log_mem *log_entry; 8962529f56eSJonathan T. Looney 8972529f56eSJonathan T. Looney /* Free the entries. */ 8982529f56eSJonathan T. Looney while ((log_entry = STAILQ_FIRST(head)) != NULL) { 8992529f56eSJonathan T. Looney STAILQ_REMOVE_HEAD(head, tlm_queue); 9002529f56eSJonathan T. Looney tcp_log_entry_refcnt_rem(log_entry); 9012529f56eSJonathan T. Looney tcp_log_free_log_common(log_entry, count); 9022529f56eSJonathan T. Looney } 9032529f56eSJonathan T. Looney } 9042529f56eSJonathan T. Looney 9052529f56eSJonathan T. Looney /* Cleanup after removing a log entry. */ 9062529f56eSJonathan T. Looney static inline void 9072529f56eSJonathan T. Looney tcp_log_remove_log_cleanup(struct tcpcb *tp, struct tcp_log_mem *log_entry) 9082529f56eSJonathan T. Looney { 9092529f56eSJonathan T. Looney uma_zfree(tcp_log_zone, log_entry); 9102529f56eSJonathan T. Looney tp->t_lognum--; 9112529f56eSJonathan T. Looney KASSERT(tp->t_lognum >= 0, 9122529f56eSJonathan T. Looney ("%s: tp->t_lognum unexpectedly negative", __func__)); 9132529f56eSJonathan T. Looney } 9142529f56eSJonathan T. Looney 9152529f56eSJonathan T. Looney /* Remove a log entry from the head of a list. */ 9162529f56eSJonathan T. Looney static inline void 9172529f56eSJonathan T. Looney tcp_log_remove_log_head(struct tcpcb *tp, struct tcp_log_mem *log_entry) 9182529f56eSJonathan T. Looney { 9192529f56eSJonathan T. Looney 9202529f56eSJonathan T. Looney KASSERT(log_entry == STAILQ_FIRST(&tp->t_logs), 9212529f56eSJonathan T. Looney ("%s: attempt to remove non-HEAD log entry", __func__)); 9222529f56eSJonathan T. Looney STAILQ_REMOVE_HEAD(&tp->t_logs, tlm_queue); 9232529f56eSJonathan T. Looney tcp_log_entry_refcnt_rem(log_entry); 9242529f56eSJonathan T. Looney tcp_log_remove_log_cleanup(tp, log_entry); 9252529f56eSJonathan T. Looney } 9262529f56eSJonathan T. Looney 9272529f56eSJonathan T. Looney #ifdef TCPLOG_DEBUG_RINGBUF 9282529f56eSJonathan T. Looney /* 9292529f56eSJonathan T. Looney * Initialize the log entry's reference count, which we want to 9302529f56eSJonathan T. Looney * survive allocations. 9312529f56eSJonathan T. Looney */ 9322529f56eSJonathan T. Looney static int 9332529f56eSJonathan T. Looney tcp_log_zone_init(void *mem, int size, int flags __unused) 9342529f56eSJonathan T. Looney { 9352529f56eSJonathan T. Looney struct tcp_log_mem *tlm; 9362529f56eSJonathan T. Looney 9372529f56eSJonathan T. Looney KASSERT(size >= sizeof(struct tcp_log_mem), 9382529f56eSJonathan T. Looney ("%s: unexpectedly short (%d) allocation", __func__, size)); 9392529f56eSJonathan T. Looney tlm = (struct tcp_log_mem *)mem; 9402529f56eSJonathan T. Looney tlm->tlm_refcnt = 0; 9412529f56eSJonathan T. Looney return (0); 9422529f56eSJonathan T. Looney } 9432529f56eSJonathan T. Looney 9442529f56eSJonathan T. Looney /* 9452529f56eSJonathan T. Looney * Double check that the refcnt is zero on allocation and return. 9462529f56eSJonathan T. Looney */ 9472529f56eSJonathan T. Looney static int 9482529f56eSJonathan T. Looney tcp_log_zone_ctor(void *mem, int size, void *args __unused, int flags __unused) 9492529f56eSJonathan T. Looney { 9502529f56eSJonathan T. Looney struct tcp_log_mem *tlm; 9512529f56eSJonathan T. Looney 9522529f56eSJonathan T. Looney KASSERT(size >= sizeof(struct tcp_log_mem), 9532529f56eSJonathan T. Looney ("%s: unexpectedly short (%d) allocation", __func__, size)); 9542529f56eSJonathan T. Looney tlm = (struct tcp_log_mem *)mem; 9552529f56eSJonathan T. Looney if (tlm->tlm_refcnt != 0) 9562529f56eSJonathan T. Looney panic("%s:%d: tlm(%p)->tlm_refcnt is %d (expected 0)", 9572529f56eSJonathan T. Looney __func__, __LINE__, tlm, tlm->tlm_refcnt); 9582529f56eSJonathan T. Looney return (0); 9592529f56eSJonathan T. Looney } 9602529f56eSJonathan T. Looney 9612529f56eSJonathan T. Looney static void 9622529f56eSJonathan T. Looney tcp_log_zone_dtor(void *mem, int size, void *args __unused) 9632529f56eSJonathan T. Looney { 9642529f56eSJonathan T. Looney struct tcp_log_mem *tlm; 9652529f56eSJonathan T. Looney 9662529f56eSJonathan T. Looney KASSERT(size >= sizeof(struct tcp_log_mem), 9672529f56eSJonathan T. Looney ("%s: unexpectedly short (%d) allocation", __func__, size)); 9682529f56eSJonathan T. Looney tlm = (struct tcp_log_mem *)mem; 9692529f56eSJonathan T. Looney if (tlm->tlm_refcnt != 0) 9702529f56eSJonathan T. Looney panic("%s:%d: tlm(%p)->tlm_refcnt is %d (expected 0)", 9712529f56eSJonathan T. Looney __func__, __LINE__, tlm, tlm->tlm_refcnt); 9722529f56eSJonathan T. Looney } 9732529f56eSJonathan T. Looney #endif /* TCPLOG_DEBUG_RINGBUF */ 9742529f56eSJonathan T. Looney 9752529f56eSJonathan T. Looney /* Do global initialization. */ 9762529f56eSJonathan T. Looney void 9772529f56eSJonathan T. Looney tcp_log_init(void) 9782529f56eSJonathan T. Looney { 9792529f56eSJonathan T. Looney 9802529f56eSJonathan T. Looney tcp_log_zone = uma_zcreate("tcp_log", sizeof(struct tcp_log_mem), 9812529f56eSJonathan T. Looney #ifdef TCPLOG_DEBUG_RINGBUF 9822529f56eSJonathan T. Looney tcp_log_zone_ctor, tcp_log_zone_dtor, tcp_log_zone_init, 9832529f56eSJonathan T. Looney #else 9842529f56eSJonathan T. Looney NULL, NULL, NULL, 9852529f56eSJonathan T. Looney #endif 9862529f56eSJonathan T. Looney NULL, UMA_ALIGN_PTR, 0); 9872529f56eSJonathan T. Looney (void)uma_zone_set_max(tcp_log_zone, TCP_LOG_BUF_DEFAULT_GLOBAL_LIMIT); 9882529f56eSJonathan T. Looney tcp_log_bucket_zone = uma_zcreate("tcp_log_bucket", 9892529f56eSJonathan T. Looney sizeof(struct tcp_log_id_bucket), NULL, NULL, NULL, NULL, 9902529f56eSJonathan T. Looney UMA_ALIGN_PTR, 0); 9912529f56eSJonathan T. Looney tcp_log_node_zone = uma_zcreate("tcp_log_node", 9922529f56eSJonathan T. Looney sizeof(struct tcp_log_id_node), NULL, NULL, NULL, NULL, 9932529f56eSJonathan T. Looney UMA_ALIGN_PTR, 0); 9942529f56eSJonathan T. Looney #ifdef TCPLOG_DEBUG_COUNTERS 9952529f56eSJonathan T. Looney tcp_log_queued = counter_u64_alloc(M_WAITOK); 9962529f56eSJonathan T. Looney tcp_log_que_fail1 = counter_u64_alloc(M_WAITOK); 9972529f56eSJonathan T. Looney tcp_log_que_fail2 = counter_u64_alloc(M_WAITOK); 9982529f56eSJonathan T. Looney tcp_log_que_fail3 = counter_u64_alloc(M_WAITOK); 9992529f56eSJonathan T. Looney tcp_log_que_fail4 = counter_u64_alloc(M_WAITOK); 10002529f56eSJonathan T. Looney tcp_log_que_fail5 = counter_u64_alloc(M_WAITOK); 10012529f56eSJonathan T. Looney tcp_log_que_copyout = counter_u64_alloc(M_WAITOK); 10022529f56eSJonathan T. Looney tcp_log_que_read = counter_u64_alloc(M_WAITOK); 10032529f56eSJonathan T. Looney tcp_log_que_freed = counter_u64_alloc(M_WAITOK); 10042529f56eSJonathan T. Looney #endif 10052529f56eSJonathan T. Looney 10062529f56eSJonathan T. Looney rw_init_flags(&tcp_id_tree_lock, "TCP ID tree", RW_NEW); 10072529f56eSJonathan T. Looney mtx_init(&tcp_log_expireq_mtx, "TCP log expireq", NULL, MTX_DEF); 10082529f56eSJonathan T. Looney callout_init(&tcp_log_expireq_callout, 1); 10092529f56eSJonathan T. Looney } 10102529f56eSJonathan T. Looney 10112529f56eSJonathan T. Looney /* Do per-TCPCB initialization. */ 10122529f56eSJonathan T. Looney void 10132529f56eSJonathan T. Looney tcp_log_tcpcbinit(struct tcpcb *tp) 10142529f56eSJonathan T. Looney { 10152529f56eSJonathan T. Looney 10162529f56eSJonathan T. Looney /* A new TCPCB should start out zero-initialized. */ 10172529f56eSJonathan T. Looney STAILQ_INIT(&tp->t_logs); 10182529f56eSJonathan T. Looney 10192529f56eSJonathan T. Looney /* 10202529f56eSJonathan T. Looney * If we are doing auto-capturing, figure out whether we will capture 10212529f56eSJonathan T. Looney * this session. 10222529f56eSJonathan T. Looney */ 10232529f56eSJonathan T. Looney if (tcp_log_selectauto()) { 10242529f56eSJonathan T. Looney tp->t_logstate = tcp_log_auto_mode; 10252529f56eSJonathan T. Looney tp->t_flags2 |= TF2_LOG_AUTO; 10262529f56eSJonathan T. Looney } 10272529f56eSJonathan T. Looney } 10282529f56eSJonathan T. Looney 10292529f56eSJonathan T. Looney 10302529f56eSJonathan T. Looney /* Remove entries */ 10312529f56eSJonathan T. Looney static void 10322529f56eSJonathan T. Looney tcp_log_expire(void *unused __unused) 10332529f56eSJonathan T. Looney { 10342529f56eSJonathan T. Looney struct tcp_log_id_bucket *tlb; 10352529f56eSJonathan T. Looney struct tcp_log_id_node *tln; 10362529f56eSJonathan T. Looney sbintime_t expiry_limit; 10372529f56eSJonathan T. Looney int tree_locked; 10382529f56eSJonathan T. Looney 10392529f56eSJonathan T. Looney TCPLOG_EXPIREQ_LOCK(); 10402529f56eSJonathan T. Looney if (callout_pending(&tcp_log_expireq_callout)) { 10412529f56eSJonathan T. Looney /* Callout was reset. */ 10422529f56eSJonathan T. Looney TCPLOG_EXPIREQ_UNLOCK(); 10432529f56eSJonathan T. Looney return; 10442529f56eSJonathan T. Looney } 10452529f56eSJonathan T. Looney 10462529f56eSJonathan T. Looney /* 10472529f56eSJonathan T. Looney * Process entries until we reach one that expires too far in the 10482529f56eSJonathan T. Looney * future. Look one second in the future. 10492529f56eSJonathan T. Looney */ 10502529f56eSJonathan T. Looney expiry_limit = getsbinuptime() + SBT_1S; 10512529f56eSJonathan T. Looney tree_locked = TREE_UNLOCKED; 10522529f56eSJonathan T. Looney 10532529f56eSJonathan T. Looney while ((tln = STAILQ_FIRST(&tcp_log_expireq_head)) != NULL && 10542529f56eSJonathan T. Looney tln->tln_expiretime <= expiry_limit) { 10552529f56eSJonathan T. Looney if (!callout_active(&tcp_log_expireq_callout)) { 10562529f56eSJonathan T. Looney /* 10572529f56eSJonathan T. Looney * Callout was stopped. I guess we should 10582529f56eSJonathan T. Looney * just quit at this point. 10592529f56eSJonathan T. Looney */ 10602529f56eSJonathan T. Looney TCPLOG_EXPIREQ_UNLOCK(); 10612529f56eSJonathan T. Looney return; 10622529f56eSJonathan T. Looney } 10632529f56eSJonathan T. Looney 10642529f56eSJonathan T. Looney /* 10652529f56eSJonathan T. Looney * Remove the node from the head of the list and unlock 10662529f56eSJonathan T. Looney * the list. Change the expiry time to SBT_MAX as a signal 10672529f56eSJonathan T. Looney * to other threads that we now own this. 10682529f56eSJonathan T. Looney */ 10692529f56eSJonathan T. Looney STAILQ_REMOVE_HEAD(&tcp_log_expireq_head, tln_expireq); 10702529f56eSJonathan T. Looney tln->tln_expiretime = SBT_MAX; 10712529f56eSJonathan T. Looney TCPLOG_EXPIREQ_UNLOCK(); 10722529f56eSJonathan T. Looney 10732529f56eSJonathan T. Looney /* 10742529f56eSJonathan T. Looney * Remove the node from the bucket. 10752529f56eSJonathan T. Looney */ 10762529f56eSJonathan T. Looney tlb = tln->tln_bucket; 10772529f56eSJonathan T. Looney TCPID_BUCKET_LOCK(tlb); 10782529f56eSJonathan T. Looney if (tcp_log_remove_id_node(NULL, NULL, tlb, tln, &tree_locked)) { 10792529f56eSJonathan T. Looney tcp_log_id_validate_tree_lock(tree_locked); 10802529f56eSJonathan T. Looney if (tree_locked == TREE_WLOCKED) 10812529f56eSJonathan T. Looney TCPID_TREE_WUNLOCK(); 10822529f56eSJonathan T. Looney else 10832529f56eSJonathan T. Looney TCPID_TREE_RUNLOCK(); 10842529f56eSJonathan T. Looney tree_locked = TREE_UNLOCKED; 10852529f56eSJonathan T. Looney } 10862529f56eSJonathan T. Looney 10872529f56eSJonathan T. Looney /* Drop the INP reference. */ 10882529f56eSJonathan T. Looney INP_WLOCK(tln->tln_inp); 10892529f56eSJonathan T. Looney if (!in_pcbrele_wlocked(tln->tln_inp)) 10902529f56eSJonathan T. Looney INP_WUNLOCK(tln->tln_inp); 10912529f56eSJonathan T. Looney 10922529f56eSJonathan T. Looney /* Free the log records. */ 10932529f56eSJonathan T. Looney tcp_log_free_entries(&tln->tln_entries, &tln->tln_count); 10942529f56eSJonathan T. Looney 10952529f56eSJonathan T. Looney /* Free the node. */ 10962529f56eSJonathan T. Looney uma_zfree(tcp_log_node_zone, tln); 10972529f56eSJonathan T. Looney 10982529f56eSJonathan T. Looney /* Relock the expiry queue. */ 10992529f56eSJonathan T. Looney TCPLOG_EXPIREQ_LOCK(); 11002529f56eSJonathan T. Looney } 11012529f56eSJonathan T. Looney 11022529f56eSJonathan T. Looney /* 11032529f56eSJonathan T. Looney * We've expired all the entries we can. Do we need to reschedule 11042529f56eSJonathan T. Looney * ourselves? 11052529f56eSJonathan T. Looney */ 11062529f56eSJonathan T. Looney callout_deactivate(&tcp_log_expireq_callout); 11072529f56eSJonathan T. Looney if (tln != NULL) { 11082529f56eSJonathan T. Looney /* 11092529f56eSJonathan T. Looney * Get max(now + TCP_LOG_EXPIRE_INTVL, tln->tln_expiretime) and 11102529f56eSJonathan T. Looney * set the next callout to that. (This helps ensure we generally 11112529f56eSJonathan T. Looney * run the callout no more often than desired.) 11122529f56eSJonathan T. Looney */ 11132529f56eSJonathan T. Looney expiry_limit = getsbinuptime() + TCP_LOG_EXPIRE_INTVL; 11142529f56eSJonathan T. Looney if (expiry_limit < tln->tln_expiretime) 11152529f56eSJonathan T. Looney expiry_limit = tln->tln_expiretime; 11162529f56eSJonathan T. Looney callout_reset_sbt(&tcp_log_expireq_callout, expiry_limit, 11172529f56eSJonathan T. Looney SBT_1S, tcp_log_expire, NULL, C_ABSOLUTE); 11182529f56eSJonathan T. Looney } 11192529f56eSJonathan T. Looney 11202529f56eSJonathan T. Looney /* We're done. */ 11212529f56eSJonathan T. Looney TCPLOG_EXPIREQ_UNLOCK(); 11222529f56eSJonathan T. Looney return; 11232529f56eSJonathan T. Looney } 11242529f56eSJonathan T. Looney 11252529f56eSJonathan T. Looney /* 11262529f56eSJonathan T. Looney * Move log data from the TCPCB to a new node. This will reset the TCPCB log 11272529f56eSJonathan T. Looney * entries and log count; however, it will not touch other things from the 11282529f56eSJonathan T. Looney * TCPCB (e.g. t_lin, t_lib). 11292529f56eSJonathan T. Looney * 11302529f56eSJonathan T. Looney * NOTE: Must hold a lock on the INP. 11312529f56eSJonathan T. Looney */ 11322529f56eSJonathan T. Looney static void 11332529f56eSJonathan T. Looney tcp_log_move_tp_to_node(struct tcpcb *tp, struct tcp_log_id_node *tln) 11342529f56eSJonathan T. Looney { 11352529f56eSJonathan T. Looney 11362529f56eSJonathan T. Looney INP_WLOCK_ASSERT(tp->t_inpcb); 11372529f56eSJonathan T. Looney 11382529f56eSJonathan T. Looney tln->tln_ie = tp->t_inpcb->inp_inc.inc_ie; 11392529f56eSJonathan T. Looney if (tp->t_inpcb->inp_inc.inc_flags & INC_ISIPV6) 11402529f56eSJonathan T. Looney tln->tln_af = AF_INET6; 11412529f56eSJonathan T. Looney else 11422529f56eSJonathan T. Looney tln->tln_af = AF_INET; 11432529f56eSJonathan T. Looney tln->tln_entries = tp->t_logs; 11442529f56eSJonathan T. Looney tln->tln_count = tp->t_lognum; 11452529f56eSJonathan T. Looney tln->tln_bucket = tp->t_lib; 11462529f56eSJonathan T. Looney 11472529f56eSJonathan T. Looney /* Clear information from the PCB. */ 11482529f56eSJonathan T. Looney STAILQ_INIT(&tp->t_logs); 11492529f56eSJonathan T. Looney tp->t_lognum = 0; 11502529f56eSJonathan T. Looney } 11512529f56eSJonathan T. Looney 11522529f56eSJonathan T. Looney /* Do per-TCPCB cleanup */ 11532529f56eSJonathan T. Looney void 11542529f56eSJonathan T. Looney tcp_log_tcpcbfini(struct tcpcb *tp) 11552529f56eSJonathan T. Looney { 11562529f56eSJonathan T. Looney struct tcp_log_id_node *tln, *tln_first; 11572529f56eSJonathan T. Looney struct tcp_log_mem *log_entry; 11582529f56eSJonathan T. Looney sbintime_t callouttime; 11592529f56eSJonathan T. Looney 11602529f56eSJonathan T. Looney INP_WLOCK_ASSERT(tp->t_inpcb); 11612529f56eSJonathan T. Looney 11622529f56eSJonathan T. Looney /* 11632529f56eSJonathan T. Looney * If we were gathering packets to be automatically dumped, try to do 11642529f56eSJonathan T. Looney * it now. If this succeeds, the log information in the TCPCB will be 11652529f56eSJonathan T. Looney * cleared. Otherwise, we'll handle the log information as we do 11662529f56eSJonathan T. Looney * for other states. 11672529f56eSJonathan T. Looney */ 11682529f56eSJonathan T. Looney switch(tp->t_logstate) { 11692529f56eSJonathan T. Looney case TCP_LOG_STATE_HEAD_AUTO: 11702529f56eSJonathan T. Looney (void)tcp_log_dump_tp_logbuf(tp, "auto-dumped from head", 11712529f56eSJonathan T. Looney M_NOWAIT, false); 11722529f56eSJonathan T. Looney break; 11732529f56eSJonathan T. Looney case TCP_LOG_STATE_TAIL_AUTO: 11742529f56eSJonathan T. Looney (void)tcp_log_dump_tp_logbuf(tp, "auto-dumped from tail", 11752529f56eSJonathan T. Looney M_NOWAIT, false); 11762529f56eSJonathan T. Looney break; 11772529f56eSJonathan T. Looney case TCP_LOG_STATE_CONTINUAL: 11782529f56eSJonathan T. Looney (void)tcp_log_dump_tp_logbuf(tp, "auto-dumped from continual", 11792529f56eSJonathan T. Looney M_NOWAIT, false); 11802529f56eSJonathan T. Looney break; 11812529f56eSJonathan T. Looney } 11822529f56eSJonathan T. Looney 11832529f56eSJonathan T. Looney /* 11842529f56eSJonathan T. Looney * There are two ways we could keep logs: per-socket or per-ID. If 11852529f56eSJonathan T. Looney * we are tracking logs with an ID, then the logs survive the 11862529f56eSJonathan T. Looney * destruction of the TCPCB. 11872529f56eSJonathan T. Looney * 11882529f56eSJonathan T. Looney * If the TCPCB is associated with an ID node, move the logs from the 11892529f56eSJonathan T. Looney * TCPCB to the ID node. In theory, this is safe, for reasons which I 11902529f56eSJonathan T. Looney * will now explain for my own benefit when I next need to figure out 11912529f56eSJonathan T. Looney * this code. :-) 11922529f56eSJonathan T. Looney * 11932529f56eSJonathan T. Looney * We own the INP lock. Therefore, no one else can change the contents 11942529f56eSJonathan T. Looney * of this node (Rule C). Further, no one can remove this node from 11952529f56eSJonathan T. Looney * the bucket while we hold the lock (Rule D). Basically, no one can 11962529f56eSJonathan T. Looney * mess with this node. That leaves two states in which we could be: 11972529f56eSJonathan T. Looney * 11982529f56eSJonathan T. Looney * 1. Another thread is currently waiting to acquire the INP lock, with 11992529f56eSJonathan T. Looney * plans to do something with this node. When we drop the INP lock, 12002529f56eSJonathan T. Looney * they will have a chance to do that. They will recheck the 12012529f56eSJonathan T. Looney * tln_closed field (see note to Rule C) and then acquire the 12022529f56eSJonathan T. Looney * bucket lock before proceeding further. 12032529f56eSJonathan T. Looney * 12042529f56eSJonathan T. Looney * 2. Another thread will try to acquire a lock at some point in the 12052529f56eSJonathan T. Looney * future. If they try to acquire a lock before we set the 12062529f56eSJonathan T. Looney * tln_closed field, they will follow state #1. If they try to 12072529f56eSJonathan T. Looney * acquire a lock after we set the tln_closed field, they will be 12082529f56eSJonathan T. Looney * able to make changes to the node, at will, following Rule C. 12092529f56eSJonathan T. Looney * 12102529f56eSJonathan T. Looney * Therefore, we currently own this node and can make any changes 12112529f56eSJonathan T. Looney * we want. But, as soon as we set the tln_closed field to true, we 12122529f56eSJonathan T. Looney * have effectively dropped our lock on the node. (For this reason, we 12132529f56eSJonathan T. Looney * also need to make sure our writes are ordered correctly. An atomic 12142529f56eSJonathan T. Looney * operation with "release" semantics should be sufficient.) 12152529f56eSJonathan T. Looney */ 12162529f56eSJonathan T. Looney 12172529f56eSJonathan T. Looney if (tp->t_lin != NULL) { 12182529f56eSJonathan T. Looney /* Copy the relevant information to the log entry. */ 12192529f56eSJonathan T. Looney tln = tp->t_lin; 12202529f56eSJonathan T. Looney KASSERT(tln->tln_inp == tp->t_inpcb, 12212529f56eSJonathan T. Looney ("%s: Mismatched inp (tln->tln_inp=%p, tp->t_inpcb=%p)", 12222529f56eSJonathan T. Looney __func__, tln->tln_inp, tp->t_inpcb)); 12232529f56eSJonathan T. Looney tcp_log_move_tp_to_node(tp, tln); 12242529f56eSJonathan T. Looney 12252529f56eSJonathan T. Looney /* Clear information from the PCB. */ 12262529f56eSJonathan T. Looney tp->t_lin = NULL; 12272529f56eSJonathan T. Looney tp->t_lib = NULL; 12282529f56eSJonathan T. Looney 12292529f56eSJonathan T. Looney /* 12302529f56eSJonathan T. Looney * Take a reference on the INP. This ensures that the INP 12312529f56eSJonathan T. Looney * remains valid while the node is on the expiry queue. This 12322529f56eSJonathan T. Looney * ensures the INP is valid for other threads that may be 12332529f56eSJonathan T. Looney * racing to lock this node when we move it to the expire 12342529f56eSJonathan T. Looney * queue. 12352529f56eSJonathan T. Looney */ 12362529f56eSJonathan T. Looney in_pcbref(tp->t_inpcb); 12372529f56eSJonathan T. Looney 12382529f56eSJonathan T. Looney /* 12392529f56eSJonathan T. Looney * Store the entry on the expiry list. The exact behavior 12402529f56eSJonathan T. Looney * depends on whether we have entries to keep. If so, we 12412529f56eSJonathan T. Looney * put the entry at the tail of the list and expire in 12422529f56eSJonathan T. Looney * TCP_LOG_EXPIRE_TIME. Otherwise, we expire "now" and put 12432529f56eSJonathan T. Looney * the entry at the head of the list. (Handling the cleanup 12442529f56eSJonathan T. Looney * via the expiry timer lets us avoid locking messy-ness here.) 12452529f56eSJonathan T. Looney */ 12462529f56eSJonathan T. Looney tln->tln_expiretime = getsbinuptime(); 12472529f56eSJonathan T. Looney TCPLOG_EXPIREQ_LOCK(); 12482529f56eSJonathan T. Looney if (tln->tln_count) { 12492529f56eSJonathan T. Looney tln->tln_expiretime += TCP_LOG_EXPIRE_TIME; 12502529f56eSJonathan T. Looney if (STAILQ_EMPTY(&tcp_log_expireq_head) && 12512529f56eSJonathan T. Looney !callout_active(&tcp_log_expireq_callout)) { 12522529f56eSJonathan T. Looney /* 12532529f56eSJonathan T. Looney * We are adding the first entry and a callout 12542529f56eSJonathan T. Looney * is not currently scheduled; therefore, we 12552529f56eSJonathan T. Looney * need to schedule one. 12562529f56eSJonathan T. Looney */ 12572529f56eSJonathan T. Looney callout_reset_sbt(&tcp_log_expireq_callout, 12582529f56eSJonathan T. Looney tln->tln_expiretime, SBT_1S, tcp_log_expire, 12592529f56eSJonathan T. Looney NULL, C_ABSOLUTE); 12602529f56eSJonathan T. Looney } 12612529f56eSJonathan T. Looney STAILQ_INSERT_TAIL(&tcp_log_expireq_head, tln, 12622529f56eSJonathan T. Looney tln_expireq); 12632529f56eSJonathan T. Looney } else { 12642529f56eSJonathan T. Looney callouttime = tln->tln_expiretime + 12652529f56eSJonathan T. Looney TCP_LOG_EXPIRE_INTVL; 12662529f56eSJonathan T. Looney tln_first = STAILQ_FIRST(&tcp_log_expireq_head); 12672529f56eSJonathan T. Looney 12682529f56eSJonathan T. Looney if ((tln_first == NULL || 12692529f56eSJonathan T. Looney callouttime < tln_first->tln_expiretime) && 12702529f56eSJonathan T. Looney (callout_pending(&tcp_log_expireq_callout) || 12712529f56eSJonathan T. Looney !callout_active(&tcp_log_expireq_callout))) { 12722529f56eSJonathan T. Looney /* 12732529f56eSJonathan T. Looney * The list is empty, or we want to run the 12742529f56eSJonathan T. Looney * expire code before the first entry's timer 12752529f56eSJonathan T. Looney * fires. Also, we are in a case where a callout 12762529f56eSJonathan T. Looney * is not actively running. We want to reset 12772529f56eSJonathan T. Looney * the callout to occur sooner. 12782529f56eSJonathan T. Looney */ 12792529f56eSJonathan T. Looney callout_reset_sbt(&tcp_log_expireq_callout, 12802529f56eSJonathan T. Looney callouttime, SBT_1S, tcp_log_expire, NULL, 12812529f56eSJonathan T. Looney C_ABSOLUTE); 12822529f56eSJonathan T. Looney } 12832529f56eSJonathan T. Looney 12842529f56eSJonathan T. Looney /* 12852529f56eSJonathan T. Looney * Insert to the head, or just after the head, as 12862529f56eSJonathan T. Looney * appropriate. (This might result in small 12872529f56eSJonathan T. Looney * mis-orderings as a bunch of "expire now" entries 12882529f56eSJonathan T. Looney * gather at the start of the list, but that should 12892529f56eSJonathan T. Looney * not produce big problems, since the expire timer 12902529f56eSJonathan T. Looney * will walk through all of them.) 12912529f56eSJonathan T. Looney */ 12922529f56eSJonathan T. Looney if (tln_first == NULL || 12932529f56eSJonathan T. Looney tln->tln_expiretime < tln_first->tln_expiretime) 12942529f56eSJonathan T. Looney STAILQ_INSERT_HEAD(&tcp_log_expireq_head, tln, 12952529f56eSJonathan T. Looney tln_expireq); 12962529f56eSJonathan T. Looney else 12972529f56eSJonathan T. Looney STAILQ_INSERT_AFTER(&tcp_log_expireq_head, 12982529f56eSJonathan T. Looney tln_first, tln, tln_expireq); 12992529f56eSJonathan T. Looney } 13002529f56eSJonathan T. Looney TCPLOG_EXPIREQ_UNLOCK(); 13012529f56eSJonathan T. Looney 13022529f56eSJonathan T. Looney /* 13032529f56eSJonathan T. Looney * We are done messing with the tln. After this point, we 13042529f56eSJonathan T. Looney * can't touch it. (Note that the "release" semantics should 13052529f56eSJonathan T. Looney * be included with the TCPLOG_EXPIREQ_UNLOCK() call above. 13062529f56eSJonathan T. Looney * Therefore, they should be unnecessary here. However, it 13072529f56eSJonathan T. Looney * seems like a good idea to include them anyway, since we 13082529f56eSJonathan T. Looney * really are releasing a lock here.) 13092529f56eSJonathan T. Looney */ 13102529f56eSJonathan T. Looney atomic_store_rel_int(&tln->tln_closed, 1); 13112529f56eSJonathan T. Looney } else { 13122529f56eSJonathan T. Looney /* Remove log entries. */ 13132529f56eSJonathan T. Looney while ((log_entry = STAILQ_FIRST(&tp->t_logs)) != NULL) 13142529f56eSJonathan T. Looney tcp_log_remove_log_head(tp, log_entry); 13152529f56eSJonathan T. Looney KASSERT(tp->t_lognum == 0, 13162529f56eSJonathan T. Looney ("%s: After freeing entries, tp->t_lognum=%d (expected 0)", 13172529f56eSJonathan T. Looney __func__, tp->t_lognum)); 13182529f56eSJonathan T. Looney } 13192529f56eSJonathan T. Looney 13202529f56eSJonathan T. Looney /* 13212529f56eSJonathan T. Looney * Change the log state to off (just in case anything tries to sneak 13222529f56eSJonathan T. Looney * in a last-minute log). 13232529f56eSJonathan T. Looney */ 13242529f56eSJonathan T. Looney tp->t_logstate = TCP_LOG_STATE_OFF; 13252529f56eSJonathan T. Looney } 13262529f56eSJonathan T. Looney 13272529f56eSJonathan T. Looney /* 13282529f56eSJonathan T. Looney * This logs an event for a TCP socket. Normally, this is called via 13292529f56eSJonathan T. Looney * TCP_LOG_EVENT or TCP_LOG_EVENT_VERBOSE. See the documentation for 13302529f56eSJonathan T. Looney * TCP_LOG_EVENT(). 13312529f56eSJonathan T. Looney */ 13322529f56eSJonathan T. Looney 13332529f56eSJonathan T. Looney struct tcp_log_buffer * 13342529f56eSJonathan T. Looney tcp_log_event_(struct tcpcb *tp, struct tcphdr *th, struct sockbuf *rxbuf, 13352529f56eSJonathan T. Looney struct sockbuf *txbuf, uint8_t eventid, int errornum, uint32_t len, 13362529f56eSJonathan T. Looney union tcp_log_stackspecific *stackinfo, int th_hostorder, 13372529f56eSJonathan T. Looney const char *output_caller, const char *func, int line, const struct timeval *itv) 13382529f56eSJonathan T. Looney { 13392529f56eSJonathan T. Looney struct tcp_log_mem *log_entry; 13402529f56eSJonathan T. Looney struct tcp_log_buffer *log_buf; 13412529f56eSJonathan T. Looney int attempt_count = 0; 13422529f56eSJonathan T. Looney struct tcp_log_verbose *log_verbose; 13432529f56eSJonathan T. Looney uint32_t logsn; 13442529f56eSJonathan T. Looney 13452529f56eSJonathan T. Looney KASSERT((func == NULL && line == 0) || (func != NULL && line > 0), 13462529f56eSJonathan T. Looney ("%s called with inconsistent func (%p) and line (%d) arguments", 13472529f56eSJonathan T. Looney __func__, func, line)); 13482529f56eSJonathan T. Looney 13492529f56eSJonathan T. Looney INP_WLOCK_ASSERT(tp->t_inpcb); 13502529f56eSJonathan T. Looney 13512529f56eSJonathan T. Looney KASSERT(tp->t_logstate == TCP_LOG_STATE_HEAD || 13522529f56eSJonathan T. Looney tp->t_logstate == TCP_LOG_STATE_TAIL || 13532529f56eSJonathan T. Looney tp->t_logstate == TCP_LOG_STATE_CONTINUAL || 13542529f56eSJonathan T. Looney tp->t_logstate == TCP_LOG_STATE_HEAD_AUTO || 13552529f56eSJonathan T. Looney tp->t_logstate == TCP_LOG_STATE_TAIL_AUTO, 13562529f56eSJonathan T. Looney ("%s called with unexpected tp->t_logstate (%d)", __func__, 13572529f56eSJonathan T. Looney tp->t_logstate)); 13582529f56eSJonathan T. Looney 13592529f56eSJonathan T. Looney /* 13602529f56eSJonathan T. Looney * Get the serial number. We do this early so it will 13612529f56eSJonathan T. Looney * increment even if we end up skipping the log entry for some 13622529f56eSJonathan T. Looney * reason. 13632529f56eSJonathan T. Looney */ 13642529f56eSJonathan T. Looney logsn = tp->t_logsn++; 13652529f56eSJonathan T. Looney 13662529f56eSJonathan T. Looney /* 13672529f56eSJonathan T. Looney * Can we get a new log entry? If so, increment the lognum counter 13682529f56eSJonathan T. Looney * here. 13692529f56eSJonathan T. Looney */ 13702529f56eSJonathan T. Looney retry: 13712529f56eSJonathan T. Looney if (tp->t_lognum < tcp_log_session_limit) { 13722529f56eSJonathan T. Looney if ((log_entry = uma_zalloc(tcp_log_zone, M_NOWAIT)) != NULL) 13732529f56eSJonathan T. Looney tp->t_lognum++; 13742529f56eSJonathan T. Looney } else 13752529f56eSJonathan T. Looney log_entry = NULL; 13762529f56eSJonathan T. Looney 13772529f56eSJonathan T. Looney /* Do we need to try to reuse? */ 13782529f56eSJonathan T. Looney if (log_entry == NULL) { 13792529f56eSJonathan T. Looney /* 13802529f56eSJonathan T. Looney * Sacrifice auto-logged sessions without a log ID if 13812529f56eSJonathan T. Looney * tcp_log_auto_all is false. (If they don't have a log 13822529f56eSJonathan T. Looney * ID by now, it is probable that either they won't get one 13832529f56eSJonathan T. Looney * or we are resource-constrained.) 13842529f56eSJonathan T. Looney */ 13852529f56eSJonathan T. Looney if (tp->t_lib == NULL && (tp->t_flags2 & TF2_LOG_AUTO) && 13862529f56eSJonathan T. Looney !tcp_log_auto_all) { 13872529f56eSJonathan T. Looney if (tcp_log_state_change(tp, TCP_LOG_STATE_CLEAR)) { 13882529f56eSJonathan T. Looney #ifdef INVARIANTS 13892529f56eSJonathan T. Looney panic("%s:%d: tcp_log_state_change() failed " 13902529f56eSJonathan T. Looney "to set tp %p to TCP_LOG_STATE_CLEAR", 13912529f56eSJonathan T. Looney __func__, __LINE__, tp); 13922529f56eSJonathan T. Looney #endif 13932529f56eSJonathan T. Looney tp->t_logstate = TCP_LOG_STATE_OFF; 13942529f56eSJonathan T. Looney } 13952529f56eSJonathan T. Looney return (NULL); 13962529f56eSJonathan T. Looney } 13972529f56eSJonathan T. Looney /* 13982529f56eSJonathan T. Looney * If we are in TCP_LOG_STATE_HEAD_AUTO state, try to dump 13992529f56eSJonathan T. Looney * the buffers. If successful, deactivate tracing. Otherwise, 14002529f56eSJonathan T. Looney * leave it active so we will retry. 14012529f56eSJonathan T. Looney */ 14022529f56eSJonathan T. Looney if (tp->t_logstate == TCP_LOG_STATE_HEAD_AUTO && 14032529f56eSJonathan T. Looney !tcp_log_dump_tp_logbuf(tp, "auto-dumped from head", 14042529f56eSJonathan T. Looney M_NOWAIT, false)) { 14052529f56eSJonathan T. Looney tp->t_logstate = TCP_LOG_STATE_OFF; 14062529f56eSJonathan T. Looney return(NULL); 14072529f56eSJonathan T. Looney } else if ((tp->t_logstate == TCP_LOG_STATE_CONTINUAL) && 14082529f56eSJonathan T. Looney !tcp_log_dump_tp_logbuf(tp, "auto-dumped from continual", 14092529f56eSJonathan T. Looney M_NOWAIT, false)) { 14102529f56eSJonathan T. Looney if (attempt_count == 0) { 14112529f56eSJonathan T. Looney attempt_count++; 14122529f56eSJonathan T. Looney goto retry; 14132529f56eSJonathan T. Looney } 14142529f56eSJonathan T. Looney #ifdef TCPLOG_DEBUG_COUNTERS 14152529f56eSJonathan T. Looney counter_u64_add(tcp_log_que_fail4, 1); 14162529f56eSJonathan T. Looney #endif 14172529f56eSJonathan T. Looney return(NULL); 14182529f56eSJonathan T. Looney } else if (tp->t_logstate == TCP_LOG_STATE_HEAD_AUTO) 14192529f56eSJonathan T. Looney return(NULL); 14202529f56eSJonathan T. Looney 14212529f56eSJonathan T. Looney /* If in HEAD state, just deactivate the tracing and return. */ 14222529f56eSJonathan T. Looney if (tp->t_logstate == TCP_LOG_STATE_HEAD) { 14232529f56eSJonathan T. Looney tp->t_logstate = TCP_LOG_STATE_OFF; 14242529f56eSJonathan T. Looney return(NULL); 14252529f56eSJonathan T. Looney } 14262529f56eSJonathan T. Looney 14272529f56eSJonathan T. Looney /* 14282529f56eSJonathan T. Looney * Get a buffer to reuse. If that fails, just give up. 14292529f56eSJonathan T. Looney * (We can't log anything without a buffer in which to 14302529f56eSJonathan T. Looney * put it.) 14312529f56eSJonathan T. Looney * 14322529f56eSJonathan T. Looney * Note that we don't change the t_lognum counter 14332529f56eSJonathan T. Looney * here. Because we are re-using the buffer, the total 14342529f56eSJonathan T. Looney * number won't change. 14352529f56eSJonathan T. Looney */ 14362529f56eSJonathan T. Looney if ((log_entry = STAILQ_FIRST(&tp->t_logs)) == NULL) 14372529f56eSJonathan T. Looney return(NULL); 14382529f56eSJonathan T. Looney STAILQ_REMOVE_HEAD(&tp->t_logs, tlm_queue); 14392529f56eSJonathan T. Looney tcp_log_entry_refcnt_rem(log_entry); 14402529f56eSJonathan T. Looney } 14412529f56eSJonathan T. Looney 14422529f56eSJonathan T. Looney KASSERT(log_entry != NULL, 14432529f56eSJonathan T. Looney ("%s: log_entry unexpectedly NULL", __func__)); 14442529f56eSJonathan T. Looney 14452529f56eSJonathan T. Looney /* Extract the log buffer and verbose buffer pointers. */ 14462529f56eSJonathan T. Looney log_buf = &log_entry->tlm_buf; 14472529f56eSJonathan T. Looney log_verbose = &log_entry->tlm_v; 14482529f56eSJonathan T. Looney 14492529f56eSJonathan T. Looney /* Basic entries. */ 14502529f56eSJonathan T. Looney if (itv == NULL) 14512529f56eSJonathan T. Looney getmicrouptime(&log_buf->tlb_tv); 14522529f56eSJonathan T. Looney else 14532529f56eSJonathan T. Looney memcpy(&log_buf->tlb_tv, itv, sizeof(struct timeval)); 14542529f56eSJonathan T. Looney log_buf->tlb_ticks = ticks; 14552529f56eSJonathan T. Looney log_buf->tlb_sn = logsn; 14562529f56eSJonathan T. Looney log_buf->tlb_stackid = tp->t_fb->tfb_id; 14572529f56eSJonathan T. Looney log_buf->tlb_eventid = eventid; 14582529f56eSJonathan T. Looney log_buf->tlb_eventflags = 0; 14592529f56eSJonathan T. Looney log_buf->tlb_errno = errornum; 14602529f56eSJonathan T. Looney 14612529f56eSJonathan T. Looney /* Socket buffers */ 14622529f56eSJonathan T. Looney if (rxbuf != NULL) { 14632529f56eSJonathan T. Looney log_buf->tlb_eventflags |= TLB_FLAG_RXBUF; 14642529f56eSJonathan T. Looney log_buf->tlb_rxbuf.tls_sb_acc = rxbuf->sb_acc; 14652529f56eSJonathan T. Looney log_buf->tlb_rxbuf.tls_sb_ccc = rxbuf->sb_ccc; 14662529f56eSJonathan T. Looney log_buf->tlb_rxbuf.tls_sb_spare = 0; 14672529f56eSJonathan T. Looney } 14682529f56eSJonathan T. Looney if (txbuf != NULL) { 14692529f56eSJonathan T. Looney log_buf->tlb_eventflags |= TLB_FLAG_TXBUF; 14702529f56eSJonathan T. Looney log_buf->tlb_txbuf.tls_sb_acc = txbuf->sb_acc; 14712529f56eSJonathan T. Looney log_buf->tlb_txbuf.tls_sb_ccc = txbuf->sb_ccc; 14722529f56eSJonathan T. Looney log_buf->tlb_txbuf.tls_sb_spare = 0; 14732529f56eSJonathan T. Looney } 14742529f56eSJonathan T. Looney /* Copy values from tp to the log entry. */ 14752529f56eSJonathan T. Looney #define COPY_STAT(f) log_buf->tlb_ ## f = tp->f 14762529f56eSJonathan T. Looney #define COPY_STAT_T(f) log_buf->tlb_ ## f = tp->t_ ## f 14772529f56eSJonathan T. Looney COPY_STAT_T(state); 14782529f56eSJonathan T. Looney COPY_STAT_T(starttime); 14792529f56eSJonathan T. Looney COPY_STAT(iss); 14802529f56eSJonathan T. Looney COPY_STAT_T(flags); 14812529f56eSJonathan T. Looney COPY_STAT(snd_una); 14822529f56eSJonathan T. Looney COPY_STAT(snd_max); 14832529f56eSJonathan T. Looney COPY_STAT(snd_cwnd); 14842529f56eSJonathan T. Looney COPY_STAT(snd_nxt); 14852529f56eSJonathan T. Looney COPY_STAT(snd_recover); 14862529f56eSJonathan T. Looney COPY_STAT(snd_wnd); 14872529f56eSJonathan T. Looney COPY_STAT(snd_ssthresh); 14882529f56eSJonathan T. Looney COPY_STAT_T(srtt); 14892529f56eSJonathan T. Looney COPY_STAT_T(rttvar); 14902529f56eSJonathan T. Looney COPY_STAT(rcv_up); 14912529f56eSJonathan T. Looney COPY_STAT(rcv_adv); 14922529f56eSJonathan T. Looney COPY_STAT(rcv_nxt); 14932529f56eSJonathan T. Looney COPY_STAT(sack_newdata); 14942529f56eSJonathan T. Looney COPY_STAT(rcv_wnd); 14952529f56eSJonathan T. Looney COPY_STAT_T(dupacks); 14962529f56eSJonathan T. Looney COPY_STAT_T(segqlen); 14972529f56eSJonathan T. Looney COPY_STAT(snd_numholes); 14982529f56eSJonathan T. Looney COPY_STAT(snd_scale); 14992529f56eSJonathan T. Looney COPY_STAT(rcv_scale); 15002529f56eSJonathan T. Looney #undef COPY_STAT 15012529f56eSJonathan T. Looney #undef COPY_STAT_T 15022529f56eSJonathan T. Looney log_buf->tlb_flex1 = 0; 15032529f56eSJonathan T. Looney log_buf->tlb_flex2 = 0; 15042529f56eSJonathan T. Looney /* Copy stack-specific info. */ 15052529f56eSJonathan T. Looney if (stackinfo != NULL) { 15062529f56eSJonathan T. Looney memcpy(&log_buf->tlb_stackinfo, stackinfo, 15072529f56eSJonathan T. Looney sizeof(log_buf->tlb_stackinfo)); 15082529f56eSJonathan T. Looney log_buf->tlb_eventflags |= TLB_FLAG_STACKINFO; 15092529f56eSJonathan T. Looney } 15102529f56eSJonathan T. Looney 15112529f56eSJonathan T. Looney /* The packet */ 15122529f56eSJonathan T. Looney log_buf->tlb_len = len; 15132529f56eSJonathan T. Looney if (th) { 15142529f56eSJonathan T. Looney int optlen; 15152529f56eSJonathan T. Looney 15162529f56eSJonathan T. Looney log_buf->tlb_eventflags |= TLB_FLAG_HDR; 15172529f56eSJonathan T. Looney log_buf->tlb_th = *th; 15182529f56eSJonathan T. Looney if (th_hostorder) 15192529f56eSJonathan T. Looney tcp_fields_to_net(&log_buf->tlb_th); 15202529f56eSJonathan T. Looney optlen = (th->th_off << 2) - sizeof (struct tcphdr); 15212529f56eSJonathan T. Looney if (optlen > 0) 15222529f56eSJonathan T. Looney memcpy(log_buf->tlb_opts, th + 1, optlen); 15232529f56eSJonathan T. Looney } 15242529f56eSJonathan T. Looney 15252529f56eSJonathan T. Looney /* Verbose information */ 15262529f56eSJonathan T. Looney if (func != NULL) { 15272529f56eSJonathan T. Looney log_buf->tlb_eventflags |= TLB_FLAG_VERBOSE; 15282529f56eSJonathan T. Looney if (output_caller != NULL) 15292529f56eSJonathan T. Looney strlcpy(log_verbose->tlv_snd_frm, output_caller, 15302529f56eSJonathan T. Looney TCP_FUNC_LEN); 15312529f56eSJonathan T. Looney else 15322529f56eSJonathan T. Looney *log_verbose->tlv_snd_frm = 0; 15332529f56eSJonathan T. Looney strlcpy(log_verbose->tlv_trace_func, func, TCP_FUNC_LEN); 15342529f56eSJonathan T. Looney log_verbose->tlv_trace_line = line; 15352529f56eSJonathan T. Looney } 15362529f56eSJonathan T. Looney 15372529f56eSJonathan T. Looney /* Insert the new log at the tail. */ 15382529f56eSJonathan T. Looney STAILQ_INSERT_TAIL(&tp->t_logs, log_entry, tlm_queue); 15392529f56eSJonathan T. Looney tcp_log_entry_refcnt_add(log_entry); 15402529f56eSJonathan T. Looney return (log_buf); 15412529f56eSJonathan T. Looney } 15422529f56eSJonathan T. Looney 15432529f56eSJonathan T. Looney /* 15442529f56eSJonathan T. Looney * Change the logging state for a TCPCB. Returns 0 on success or an 15452529f56eSJonathan T. Looney * error code on failure. 15462529f56eSJonathan T. Looney */ 15472529f56eSJonathan T. Looney int 15482529f56eSJonathan T. Looney tcp_log_state_change(struct tcpcb *tp, int state) 15492529f56eSJonathan T. Looney { 15502529f56eSJonathan T. Looney struct tcp_log_mem *log_entry; 15512529f56eSJonathan T. Looney 15522529f56eSJonathan T. Looney INP_WLOCK_ASSERT(tp->t_inpcb); 15532529f56eSJonathan T. Looney switch(state) { 15542529f56eSJonathan T. Looney case TCP_LOG_STATE_CLEAR: 15552529f56eSJonathan T. Looney while ((log_entry = STAILQ_FIRST(&tp->t_logs)) != NULL) 15562529f56eSJonathan T. Looney tcp_log_remove_log_head(tp, log_entry); 15572529f56eSJonathan T. Looney /* Fall through */ 15582529f56eSJonathan T. Looney 15592529f56eSJonathan T. Looney case TCP_LOG_STATE_OFF: 15602529f56eSJonathan T. Looney tp->t_logstate = TCP_LOG_STATE_OFF; 15612529f56eSJonathan T. Looney break; 15622529f56eSJonathan T. Looney 15632529f56eSJonathan T. Looney case TCP_LOG_STATE_TAIL: 15642529f56eSJonathan T. Looney case TCP_LOG_STATE_HEAD: 15652529f56eSJonathan T. Looney case TCP_LOG_STATE_CONTINUAL: 15662529f56eSJonathan T. Looney case TCP_LOG_STATE_HEAD_AUTO: 15672529f56eSJonathan T. Looney case TCP_LOG_STATE_TAIL_AUTO: 15682529f56eSJonathan T. Looney tp->t_logstate = state; 15692529f56eSJonathan T. Looney break; 15702529f56eSJonathan T. Looney 15712529f56eSJonathan T. Looney default: 15722529f56eSJonathan T. Looney return (EINVAL); 15732529f56eSJonathan T. Looney } 15742529f56eSJonathan T. Looney 15752529f56eSJonathan T. Looney tp->t_flags2 &= ~(TF2_LOG_AUTO); 15762529f56eSJonathan T. Looney 15772529f56eSJonathan T. Looney return (0); 15782529f56eSJonathan T. Looney } 15792529f56eSJonathan T. Looney 15802529f56eSJonathan T. Looney /* If tcp_drain() is called, flush half the log entries. */ 15812529f56eSJonathan T. Looney void 15822529f56eSJonathan T. Looney tcp_log_drain(struct tcpcb *tp) 15832529f56eSJonathan T. Looney { 15842529f56eSJonathan T. Looney struct tcp_log_mem *log_entry, *next; 15852529f56eSJonathan T. Looney int target, skip; 15862529f56eSJonathan T. Looney 15872529f56eSJonathan T. Looney INP_WLOCK_ASSERT(tp->t_inpcb); 15882529f56eSJonathan T. Looney if ((target = tp->t_lognum / 2) == 0) 15892529f56eSJonathan T. Looney return; 15902529f56eSJonathan T. Looney 15912529f56eSJonathan T. Looney /* 15922529f56eSJonathan T. Looney * If we are logging the "head" packets, we want to discard 15932529f56eSJonathan T. Looney * from the tail of the queue. Otherwise, we want to discard 15942529f56eSJonathan T. Looney * from the head. 15952529f56eSJonathan T. Looney */ 15962529f56eSJonathan T. Looney if (tp->t_logstate == TCP_LOG_STATE_HEAD || 15972529f56eSJonathan T. Looney tp->t_logstate == TCP_LOG_STATE_HEAD_AUTO) { 15982529f56eSJonathan T. Looney skip = tp->t_lognum - target; 15992529f56eSJonathan T. Looney STAILQ_FOREACH(log_entry, &tp->t_logs, tlm_queue) 16002529f56eSJonathan T. Looney if (!--skip) 16012529f56eSJonathan T. Looney break; 16022529f56eSJonathan T. Looney KASSERT(log_entry != NULL, 16032529f56eSJonathan T. Looney ("%s: skipped through all entries!", __func__)); 16042529f56eSJonathan T. Looney if (log_entry == NULL) 16052529f56eSJonathan T. Looney return; 16062529f56eSJonathan T. Looney while ((next = STAILQ_NEXT(log_entry, tlm_queue)) != NULL) { 16072529f56eSJonathan T. Looney STAILQ_REMOVE_AFTER(&tp->t_logs, log_entry, tlm_queue); 16082529f56eSJonathan T. Looney tcp_log_entry_refcnt_rem(next); 16092529f56eSJonathan T. Looney tcp_log_remove_log_cleanup(tp, next); 16102529f56eSJonathan T. Looney #ifdef INVARIANTS 16112529f56eSJonathan T. Looney target--; 16122529f56eSJonathan T. Looney #endif 16132529f56eSJonathan T. Looney } 16142529f56eSJonathan T. Looney KASSERT(target == 0, 16152529f56eSJonathan T. Looney ("%s: After removing from tail, target was %d", __func__, 16162529f56eSJonathan T. Looney target)); 16172529f56eSJonathan T. Looney } else if (tp->t_logstate == TCP_LOG_STATE_CONTINUAL) { 16182529f56eSJonathan T. Looney (void)tcp_log_dump_tp_logbuf(tp, "auto-dumped from continual", 16192529f56eSJonathan T. Looney M_NOWAIT, false); 16202529f56eSJonathan T. Looney } else { 16212529f56eSJonathan T. Looney while ((log_entry = STAILQ_FIRST(&tp->t_logs)) != NULL && 16222529f56eSJonathan T. Looney target--) 16232529f56eSJonathan T. Looney tcp_log_remove_log_head(tp, log_entry); 16242529f56eSJonathan T. Looney KASSERT(target <= 0, 16252529f56eSJonathan T. Looney ("%s: After removing from head, target was %d", __func__, 16262529f56eSJonathan T. Looney target)); 16272529f56eSJonathan T. Looney KASSERT(tp->t_lognum > 0, 16282529f56eSJonathan T. Looney ("%s: After removing from head, tp->t_lognum was %d", 16292529f56eSJonathan T. Looney __func__, target)); 16302529f56eSJonathan T. Looney KASSERT(log_entry != NULL, 16312529f56eSJonathan T. Looney ("%s: After removing from head, the tailq was empty", 16322529f56eSJonathan T. Looney __func__)); 16332529f56eSJonathan T. Looney } 16342529f56eSJonathan T. Looney } 16352529f56eSJonathan T. Looney 16362529f56eSJonathan T. Looney static inline int 16372529f56eSJonathan T. Looney tcp_log_copyout(struct sockopt *sopt, void *src, void *dst, size_t len) 16382529f56eSJonathan T. Looney { 16392529f56eSJonathan T. Looney 16402529f56eSJonathan T. Looney if (sopt->sopt_td != NULL) 16412529f56eSJonathan T. Looney return (copyout(src, dst, len)); 16422529f56eSJonathan T. Looney bcopy(src, dst, len); 16432529f56eSJonathan T. Looney return (0); 16442529f56eSJonathan T. Looney } 16452529f56eSJonathan T. Looney 16462529f56eSJonathan T. Looney static int 16472529f56eSJonathan T. Looney tcp_log_logs_to_buf(struct sockopt *sopt, struct tcp_log_stailq *log_tailqp, 16482529f56eSJonathan T. Looney struct tcp_log_buffer **end, int count) 16492529f56eSJonathan T. Looney { 16502529f56eSJonathan T. Looney struct tcp_log_buffer *out_entry; 16512529f56eSJonathan T. Looney struct tcp_log_mem *log_entry; 16522529f56eSJonathan T. Looney size_t entrysize; 16532529f56eSJonathan T. Looney int error; 16542529f56eSJonathan T. Looney #ifdef INVARIANTS 16552529f56eSJonathan T. Looney int orig_count = count; 16562529f56eSJonathan T. Looney #endif 16572529f56eSJonathan T. Looney 16582529f56eSJonathan T. Looney /* Copy the data out. */ 16592529f56eSJonathan T. Looney error = 0; 16602529f56eSJonathan T. Looney out_entry = (struct tcp_log_buffer *) sopt->sopt_val; 16612529f56eSJonathan T. Looney STAILQ_FOREACH(log_entry, log_tailqp, tlm_queue) { 16622529f56eSJonathan T. Looney count--; 16632529f56eSJonathan T. Looney KASSERT(count >= 0, 16642529f56eSJonathan T. Looney ("%s:%d: Exceeded expected count (%d) processing list %p", 16652529f56eSJonathan T. Looney __func__, __LINE__, orig_count, log_tailqp)); 16662529f56eSJonathan T. Looney 16672529f56eSJonathan T. Looney #ifdef TCPLOG_DEBUG_COUNTERS 16682529f56eSJonathan T. Looney counter_u64_add(tcp_log_que_copyout, 1); 16692529f56eSJonathan T. Looney #endif 16702529f56eSJonathan T. Looney #if 0 16712529f56eSJonathan T. Looney struct tcp_log_buffer *lb = &log_entry->tlm_buf; 16722529f56eSJonathan T. Looney int i; 16732529f56eSJonathan T. Looney 16742529f56eSJonathan T. Looney printf("lb = %p:\n", lb); 16752529f56eSJonathan T. Looney #define PRINT(f) printf(#f " = %u\n", (unsigned int)lb->f) 16762529f56eSJonathan T. Looney printf("tlb_tv = {%lu, %lu}\n", lb->tlb_tv.tv_sec, lb->tlb_tv.tv_usec); 16772529f56eSJonathan T. Looney PRINT(tlb_ticks); 16782529f56eSJonathan T. Looney PRINT(tlb_sn); 16792529f56eSJonathan T. Looney PRINT(tlb_stackid); 16802529f56eSJonathan T. Looney PRINT(tlb_eventid); 16812529f56eSJonathan T. Looney PRINT(tlb_eventflags); 16822529f56eSJonathan T. Looney PRINT(tlb_errno); 16832529f56eSJonathan T. Looney PRINT(tlb_rxbuf.tls_sb_acc); 16842529f56eSJonathan T. Looney PRINT(tlb_rxbuf.tls_sb_ccc); 16852529f56eSJonathan T. Looney PRINT(tlb_rxbuf.tls_sb_spare); 16862529f56eSJonathan T. Looney PRINT(tlb_txbuf.tls_sb_acc); 16872529f56eSJonathan T. Looney PRINT(tlb_txbuf.tls_sb_ccc); 16882529f56eSJonathan T. Looney PRINT(tlb_txbuf.tls_sb_spare); 16892529f56eSJonathan T. Looney PRINT(tlb_state); 16902529f56eSJonathan T. Looney PRINT(tlb_flags); 16912529f56eSJonathan T. Looney PRINT(tlb_snd_una); 16922529f56eSJonathan T. Looney PRINT(tlb_snd_max); 16932529f56eSJonathan T. Looney PRINT(tlb_snd_cwnd); 16942529f56eSJonathan T. Looney PRINT(tlb_snd_nxt); 16952529f56eSJonathan T. Looney PRINT(tlb_snd_recover); 16962529f56eSJonathan T. Looney PRINT(tlb_snd_wnd); 16972529f56eSJonathan T. Looney PRINT(tlb_snd_ssthresh); 16982529f56eSJonathan T. Looney PRINT(tlb_srtt); 16992529f56eSJonathan T. Looney PRINT(tlb_rttvar); 17002529f56eSJonathan T. Looney PRINT(tlb_rcv_up); 17012529f56eSJonathan T. Looney PRINT(tlb_rcv_adv); 17022529f56eSJonathan T. Looney PRINT(tlb_rcv_nxt); 17032529f56eSJonathan T. Looney PRINT(tlb_sack_newdata); 17042529f56eSJonathan T. Looney PRINT(tlb_rcv_wnd); 17052529f56eSJonathan T. Looney PRINT(tlb_dupacks); 17062529f56eSJonathan T. Looney PRINT(tlb_segqlen); 17072529f56eSJonathan T. Looney PRINT(tlb_snd_numholes); 17082529f56eSJonathan T. Looney PRINT(tlb_snd_scale); 17092529f56eSJonathan T. Looney PRINT(tlb_rcv_scale); 17102529f56eSJonathan T. Looney PRINT(tlb_len); 17112529f56eSJonathan T. Looney printf("hex dump: "); 17122529f56eSJonathan T. Looney for (i = 0; i < sizeof(struct tcp_log_buffer); i++) 17132529f56eSJonathan T. Looney printf("%02x", *(((uint8_t *)lb) + i)); 17142529f56eSJonathan T. Looney #undef PRINT 17152529f56eSJonathan T. Looney #endif 17162529f56eSJonathan T. Looney /* 17172529f56eSJonathan T. Looney * Skip copying out the header if it isn't present. 17182529f56eSJonathan T. Looney * Instead, copy out zeros (to ensure we don't leak info). 17192529f56eSJonathan T. Looney * TODO: Make sure we truly do zero everything we don't 17202529f56eSJonathan T. Looney * explicitly set. 17212529f56eSJonathan T. Looney */ 17222529f56eSJonathan T. Looney if (log_entry->tlm_buf.tlb_eventflags & TLB_FLAG_HDR) 17232529f56eSJonathan T. Looney entrysize = sizeof(struct tcp_log_buffer); 17242529f56eSJonathan T. Looney else 17252529f56eSJonathan T. Looney entrysize = offsetof(struct tcp_log_buffer, tlb_th); 17262529f56eSJonathan T. Looney error = tcp_log_copyout(sopt, &log_entry->tlm_buf, out_entry, 17272529f56eSJonathan T. Looney entrysize); 17282529f56eSJonathan T. Looney if (error) 17292529f56eSJonathan T. Looney break; 17302529f56eSJonathan T. Looney if (!(log_entry->tlm_buf.tlb_eventflags & TLB_FLAG_HDR)) { 17312529f56eSJonathan T. Looney error = tcp_log_copyout(sopt, zerobuf, 17322529f56eSJonathan T. Looney ((uint8_t *)out_entry) + entrysize, 17332529f56eSJonathan T. Looney sizeof(struct tcp_log_buffer) - entrysize); 17342529f56eSJonathan T. Looney } 17352529f56eSJonathan T. Looney 17362529f56eSJonathan T. Looney /* 17372529f56eSJonathan T. Looney * Copy out the verbose bit, if needed. Either way, 17382529f56eSJonathan T. Looney * increment the output pointer the correct amount. 17392529f56eSJonathan T. Looney */ 17402529f56eSJonathan T. Looney if (log_entry->tlm_buf.tlb_eventflags & TLB_FLAG_VERBOSE) { 17412529f56eSJonathan T. Looney error = tcp_log_copyout(sopt, &log_entry->tlm_v, 17422529f56eSJonathan T. Looney out_entry->tlb_verbose, 17432529f56eSJonathan T. Looney sizeof(struct tcp_log_verbose)); 17442529f56eSJonathan T. Looney if (error) 17452529f56eSJonathan T. Looney break; 17462529f56eSJonathan T. Looney out_entry = (struct tcp_log_buffer *) 17472529f56eSJonathan T. Looney (((uint8_t *) (out_entry + 1)) + 17482529f56eSJonathan T. Looney sizeof(struct tcp_log_verbose)); 17492529f56eSJonathan T. Looney } else 17502529f56eSJonathan T. Looney out_entry++; 17512529f56eSJonathan T. Looney } 17522529f56eSJonathan T. Looney *end = out_entry; 17532529f56eSJonathan T. Looney KASSERT(error || count == 0, 17542529f56eSJonathan T. Looney ("%s:%d: Less than expected count (%d) processing list %p" 17552529f56eSJonathan T. Looney " (%d remain)", __func__, __LINE__, orig_count, 17562529f56eSJonathan T. Looney log_tailqp, count)); 17572529f56eSJonathan T. Looney 17582529f56eSJonathan T. Looney return (error); 17592529f56eSJonathan T. Looney } 17602529f56eSJonathan T. Looney 17612529f56eSJonathan T. Looney /* 17622529f56eSJonathan T. Looney * Copy out the buffer. Note that we do incremental copying, so 17632529f56eSJonathan T. Looney * sooptcopyout() won't work. However, the goal is to produce the same 17642529f56eSJonathan T. Looney * end result as if we copied in the entire user buffer, updated it, 17652529f56eSJonathan T. Looney * and then used sooptcopyout() to copy it out. 17662529f56eSJonathan T. Looney * 17672529f56eSJonathan T. Looney * NOTE: This should be called with a write lock on the PCB; however, 17682529f56eSJonathan T. Looney * the function will drop it after it extracts the data from the TCPCB. 17692529f56eSJonathan T. Looney */ 17702529f56eSJonathan T. Looney int 17712529f56eSJonathan T. Looney tcp_log_getlogbuf(struct sockopt *sopt, struct tcpcb *tp) 17722529f56eSJonathan T. Looney { 17732529f56eSJonathan T. Looney struct tcp_log_stailq log_tailq; 17742529f56eSJonathan T. Looney struct tcp_log_mem *log_entry, *log_next; 17752529f56eSJonathan T. Looney struct tcp_log_buffer *out_entry; 17762529f56eSJonathan T. Looney struct inpcb *inp; 17772529f56eSJonathan T. Looney size_t outsize, entrysize; 17782529f56eSJonathan T. Looney int error, outnum; 17792529f56eSJonathan T. Looney 17802529f56eSJonathan T. Looney INP_WLOCK_ASSERT(tp->t_inpcb); 17812529f56eSJonathan T. Looney inp = tp->t_inpcb; 17822529f56eSJonathan T. Looney 17832529f56eSJonathan T. Looney /* 17842529f56eSJonathan T. Looney * Determine which log entries will fit in the buffer. As an 17852529f56eSJonathan T. Looney * optimization, skip this if all the entries will clearly fit 17862529f56eSJonathan T. Looney * in the buffer. (However, get an exact size if we are using 17872529f56eSJonathan T. Looney * INVARIANTS.) 17882529f56eSJonathan T. Looney */ 17892529f56eSJonathan T. Looney #ifndef INVARIANTS 17902529f56eSJonathan T. Looney if (sopt->sopt_valsize / (sizeof(struct tcp_log_buffer) + 17912529f56eSJonathan T. Looney sizeof(struct tcp_log_verbose)) >= tp->t_lognum) { 17922529f56eSJonathan T. Looney log_entry = STAILQ_LAST(&tp->t_logs, tcp_log_mem, tlm_queue); 17932529f56eSJonathan T. Looney log_next = NULL; 17942529f56eSJonathan T. Looney outsize = 0; 17952529f56eSJonathan T. Looney outnum = tp->t_lognum; 17962529f56eSJonathan T. Looney } else { 17972529f56eSJonathan T. Looney #endif 17982529f56eSJonathan T. Looney outsize = outnum = 0; 17992529f56eSJonathan T. Looney log_entry = NULL; 18002529f56eSJonathan T. Looney STAILQ_FOREACH(log_next, &tp->t_logs, tlm_queue) { 18012529f56eSJonathan T. Looney entrysize = sizeof(struct tcp_log_buffer); 18022529f56eSJonathan T. Looney if (log_next->tlm_buf.tlb_eventflags & 18032529f56eSJonathan T. Looney TLB_FLAG_VERBOSE) 18042529f56eSJonathan T. Looney entrysize += sizeof(struct tcp_log_verbose); 18052529f56eSJonathan T. Looney if ((sopt->sopt_valsize - outsize) < entrysize) 18062529f56eSJonathan T. Looney break; 18072529f56eSJonathan T. Looney outsize += entrysize; 18082529f56eSJonathan T. Looney outnum++; 18092529f56eSJonathan T. Looney log_entry = log_next; 18102529f56eSJonathan T. Looney } 18112529f56eSJonathan T. Looney KASSERT(outsize <= sopt->sopt_valsize, 18122529f56eSJonathan T. Looney ("%s: calculated output size (%zu) greater than available" 18132529f56eSJonathan T. Looney "space (%zu)", __func__, outsize, sopt->sopt_valsize)); 18142529f56eSJonathan T. Looney #ifndef INVARIANTS 18152529f56eSJonathan T. Looney } 18162529f56eSJonathan T. Looney #endif 18172529f56eSJonathan T. Looney 18182529f56eSJonathan T. Looney /* 18192529f56eSJonathan T. Looney * Copy traditional sooptcopyout() behavior: if sopt->sopt_val 18202529f56eSJonathan T. Looney * is NULL, silently skip the copy. However, in this case, we 18212529f56eSJonathan T. Looney * will leave the list alone and return. Functionally, this 18222529f56eSJonathan T. Looney * gives userspace a way to poll for an approximate buffer 18232529f56eSJonathan T. Looney * size they will need to get the log entries. 18242529f56eSJonathan T. Looney */ 18252529f56eSJonathan T. Looney if (sopt->sopt_val == NULL) { 18262529f56eSJonathan T. Looney INP_WUNLOCK(inp); 18272529f56eSJonathan T. Looney if (outsize == 0) { 18282529f56eSJonathan T. Looney outsize = outnum * (sizeof(struct tcp_log_buffer) + 18292529f56eSJonathan T. Looney sizeof(struct tcp_log_verbose)); 18302529f56eSJonathan T. Looney } 18312529f56eSJonathan T. Looney if (sopt->sopt_valsize > outsize) 18322529f56eSJonathan T. Looney sopt->sopt_valsize = outsize; 18332529f56eSJonathan T. Looney return (0); 18342529f56eSJonathan T. Looney } 18352529f56eSJonathan T. Looney 18362529f56eSJonathan T. Looney /* 18372529f56eSJonathan T. Looney * Break apart the list. We'll save the ones we want to copy 18382529f56eSJonathan T. Looney * out locally and remove them from the TCPCB list. We can 18392529f56eSJonathan T. Looney * then drop the INPCB lock while we do the copyout. 18402529f56eSJonathan T. Looney * 18412529f56eSJonathan T. Looney * There are roughly three cases: 18422529f56eSJonathan T. Looney * 1. There was nothing to copy out. That's easy: drop the 18432529f56eSJonathan T. Looney * lock and return. 18442529f56eSJonathan T. Looney * 2. We are copying out the entire list. Again, that's easy: 18452529f56eSJonathan T. Looney * move the whole list. 18462529f56eSJonathan T. Looney * 3. We are copying out a partial list. That's harder. We 18472529f56eSJonathan T. Looney * need to update the list book-keeping entries. 18482529f56eSJonathan T. Looney */ 18492529f56eSJonathan T. Looney if (log_entry != NULL && log_next == NULL) { 18502529f56eSJonathan T. Looney /* Move entire list. */ 18512529f56eSJonathan T. Looney KASSERT(outnum == tp->t_lognum, 18522529f56eSJonathan T. Looney ("%s:%d: outnum (%d) should match tp->t_lognum (%d)", 18532529f56eSJonathan T. Looney __func__, __LINE__, outnum, tp->t_lognum)); 18542529f56eSJonathan T. Looney log_tailq = tp->t_logs; 18552529f56eSJonathan T. Looney tp->t_lognum = 0; 18562529f56eSJonathan T. Looney STAILQ_INIT(&tp->t_logs); 18572529f56eSJonathan T. Looney } else if (log_entry != NULL) { 18582529f56eSJonathan T. Looney /* Move partial list. */ 18592529f56eSJonathan T. Looney KASSERT(outnum < tp->t_lognum, 18602529f56eSJonathan T. Looney ("%s:%d: outnum (%d) not less than tp->t_lognum (%d)", 18612529f56eSJonathan T. Looney __func__, __LINE__, outnum, tp->t_lognum)); 18622529f56eSJonathan T. Looney STAILQ_FIRST(&log_tailq) = STAILQ_FIRST(&tp->t_logs); 18632529f56eSJonathan T. Looney STAILQ_FIRST(&tp->t_logs) = STAILQ_NEXT(log_entry, tlm_queue); 18642529f56eSJonathan T. Looney KASSERT(STAILQ_NEXT(log_entry, tlm_queue) != NULL, 18652529f56eSJonathan T. Looney ("%s:%d: tp->t_logs is unexpectedly shorter than expected" 18662529f56eSJonathan T. Looney "(tp: %p, log_tailq: %p, outnum: %d, tp->t_lognum: %d)", 18672529f56eSJonathan T. Looney __func__, __LINE__, tp, &log_tailq, outnum, tp->t_lognum)); 18682529f56eSJonathan T. Looney STAILQ_NEXT(log_entry, tlm_queue) = NULL; 18692529f56eSJonathan T. Looney log_tailq.stqh_last = &STAILQ_NEXT(log_entry, tlm_queue); 18702529f56eSJonathan T. Looney tp->t_lognum -= outnum; 18712529f56eSJonathan T. Looney } else 18722529f56eSJonathan T. Looney STAILQ_INIT(&log_tailq); 18732529f56eSJonathan T. Looney 18742529f56eSJonathan T. Looney /* Drop the PCB lock. */ 18752529f56eSJonathan T. Looney INP_WUNLOCK(inp); 18762529f56eSJonathan T. Looney 18772529f56eSJonathan T. Looney /* Copy the data out. */ 18782529f56eSJonathan T. Looney error = tcp_log_logs_to_buf(sopt, &log_tailq, &out_entry, outnum); 18792529f56eSJonathan T. Looney 18802529f56eSJonathan T. Looney if (error) { 18812529f56eSJonathan T. Looney /* Restore list */ 18822529f56eSJonathan T. Looney INP_WLOCK(inp); 18832529f56eSJonathan T. Looney if ((inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) == 0) { 18842529f56eSJonathan T. Looney tp = intotcpcb(inp); 18852529f56eSJonathan T. Looney 18862529f56eSJonathan T. Looney /* Merge the two lists. */ 18872529f56eSJonathan T. Looney STAILQ_CONCAT(&log_tailq, &tp->t_logs); 18882529f56eSJonathan T. Looney tp->t_logs = log_tailq; 18892529f56eSJonathan T. Looney tp->t_lognum += outnum; 18902529f56eSJonathan T. Looney } 18912529f56eSJonathan T. Looney INP_WUNLOCK(inp); 18922529f56eSJonathan T. Looney } else { 18932529f56eSJonathan T. Looney /* Sanity check entries */ 18942529f56eSJonathan T. Looney KASSERT(((caddr_t)out_entry - (caddr_t)sopt->sopt_val) == 18952529f56eSJonathan T. Looney outsize, ("%s: Actual output size (%zu) != " 18962529f56eSJonathan T. Looney "calculated output size (%zu)", __func__, 18972529f56eSJonathan T. Looney (size_t)((caddr_t)out_entry - (caddr_t)sopt->sopt_val), 18982529f56eSJonathan T. Looney outsize)); 18992529f56eSJonathan T. Looney 19002529f56eSJonathan T. Looney /* Free the entries we just copied out. */ 19012529f56eSJonathan T. Looney STAILQ_FOREACH_SAFE(log_entry, &log_tailq, tlm_queue, log_next) { 19022529f56eSJonathan T. Looney tcp_log_entry_refcnt_rem(log_entry); 19032529f56eSJonathan T. Looney uma_zfree(tcp_log_zone, log_entry); 19042529f56eSJonathan T. Looney } 19052529f56eSJonathan T. Looney } 19062529f56eSJonathan T. Looney 19072529f56eSJonathan T. Looney sopt->sopt_valsize = (size_t)((caddr_t)out_entry - 19082529f56eSJonathan T. Looney (caddr_t)sopt->sopt_val); 19092529f56eSJonathan T. Looney return (error); 19102529f56eSJonathan T. Looney } 19112529f56eSJonathan T. Looney 19122529f56eSJonathan T. Looney static void 19132529f56eSJonathan T. Looney tcp_log_free_queue(struct tcp_log_dev_queue *param) 19142529f56eSJonathan T. Looney { 19152529f56eSJonathan T. Looney struct tcp_log_dev_log_queue *entry; 19162529f56eSJonathan T. Looney 19172529f56eSJonathan T. Looney KASSERT(param != NULL, ("%s: called with NULL param", __func__)); 19182529f56eSJonathan T. Looney if (param == NULL) 19192529f56eSJonathan T. Looney return; 19202529f56eSJonathan T. Looney 19212529f56eSJonathan T. Looney entry = (struct tcp_log_dev_log_queue *)param; 19222529f56eSJonathan T. Looney 19232529f56eSJonathan T. Looney /* Free the entries. */ 19242529f56eSJonathan T. Looney tcp_log_free_entries(&entry->tldl_entries, &entry->tldl_count); 19252529f56eSJonathan T. Looney 19262529f56eSJonathan T. Looney /* Free the buffer, if it is allocated. */ 19272529f56eSJonathan T. Looney if (entry->tldl_common.tldq_buf != NULL) 19282529f56eSJonathan T. Looney free(entry->tldl_common.tldq_buf, M_TCPLOGDEV); 19292529f56eSJonathan T. Looney 19302529f56eSJonathan T. Looney /* Free the queue entry. */ 19312529f56eSJonathan T. Looney free(entry, M_TCPLOGDEV); 19322529f56eSJonathan T. Looney } 19332529f56eSJonathan T. Looney 19342529f56eSJonathan T. Looney static struct tcp_log_common_header * 19352529f56eSJonathan T. Looney tcp_log_expandlogbuf(struct tcp_log_dev_queue *param) 19362529f56eSJonathan T. Looney { 19372529f56eSJonathan T. Looney struct tcp_log_dev_log_queue *entry; 19382529f56eSJonathan T. Looney struct tcp_log_header *hdr; 19392529f56eSJonathan T. Looney uint8_t *end; 19402529f56eSJonathan T. Looney struct sockopt sopt; 19412529f56eSJonathan T. Looney int error; 19422529f56eSJonathan T. Looney 19432529f56eSJonathan T. Looney entry = (struct tcp_log_dev_log_queue *)param; 19442529f56eSJonathan T. Looney 19452529f56eSJonathan T. Looney /* Take a worst-case guess at space needs. */ 19462529f56eSJonathan T. Looney sopt.sopt_valsize = sizeof(struct tcp_log_header) + 19472529f56eSJonathan T. Looney entry->tldl_count * (sizeof(struct tcp_log_buffer) + 19482529f56eSJonathan T. Looney sizeof(struct tcp_log_verbose)); 19492529f56eSJonathan T. Looney hdr = malloc(sopt.sopt_valsize, M_TCPLOGDEV, M_NOWAIT); 19502529f56eSJonathan T. Looney if (hdr == NULL) { 19512529f56eSJonathan T. Looney #ifdef TCPLOG_DEBUG_COUNTERS 19522529f56eSJonathan T. Looney counter_u64_add(tcp_log_que_fail5, entry->tldl_count); 19532529f56eSJonathan T. Looney #endif 19542529f56eSJonathan T. Looney return (NULL); 19552529f56eSJonathan T. Looney } 19562529f56eSJonathan T. Looney sopt.sopt_val = hdr + 1; 19572529f56eSJonathan T. Looney sopt.sopt_valsize -= sizeof(struct tcp_log_header); 19582529f56eSJonathan T. Looney sopt.sopt_td = NULL; 19592529f56eSJonathan T. Looney 19602529f56eSJonathan T. Looney error = tcp_log_logs_to_buf(&sopt, &entry->tldl_entries, 19612529f56eSJonathan T. Looney (struct tcp_log_buffer **)&end, entry->tldl_count); 19622529f56eSJonathan T. Looney if (error) { 19632529f56eSJonathan T. Looney free(hdr, M_TCPLOGDEV); 19642529f56eSJonathan T. Looney return (NULL); 19652529f56eSJonathan T. Looney } 19662529f56eSJonathan T. Looney 19672529f56eSJonathan T. Looney /* Free the entries. */ 19682529f56eSJonathan T. Looney tcp_log_free_entries(&entry->tldl_entries, &entry->tldl_count); 19692529f56eSJonathan T. Looney entry->tldl_count = 0; 19702529f56eSJonathan T. Looney 19712529f56eSJonathan T. Looney memset(hdr, 0, sizeof(struct tcp_log_header)); 19722529f56eSJonathan T. Looney hdr->tlh_version = TCP_LOG_BUF_VER; 19732529f56eSJonathan T. Looney hdr->tlh_type = TCP_LOG_DEV_TYPE_BBR; 19742529f56eSJonathan T. Looney hdr->tlh_length = end - (uint8_t *)hdr; 19752529f56eSJonathan T. Looney hdr->tlh_ie = entry->tldl_ie; 19762529f56eSJonathan T. Looney hdr->tlh_af = entry->tldl_af; 19772529f56eSJonathan T. Looney getboottime(&hdr->tlh_offset); 19782529f56eSJonathan T. Looney strlcpy(hdr->tlh_id, entry->tldl_id, TCP_LOG_ID_LEN); 19792529f56eSJonathan T. Looney strlcpy(hdr->tlh_reason, entry->tldl_reason, TCP_LOG_REASON_LEN); 19802529f56eSJonathan T. Looney return ((struct tcp_log_common_header *)hdr); 19812529f56eSJonathan T. Looney } 19822529f56eSJonathan T. Looney 19832529f56eSJonathan T. Looney /* 19842529f56eSJonathan T. Looney * Queue the tcpcb's log buffer for transmission via the log buffer facility. 19852529f56eSJonathan T. Looney * 19862529f56eSJonathan T. Looney * NOTE: This should be called with a write lock on the PCB. 19872529f56eSJonathan T. Looney * 19882529f56eSJonathan T. Looney * how should be M_WAITOK or M_NOWAIT. If M_WAITOK, the function will drop 19892529f56eSJonathan T. Looney * and reacquire the INP lock if it needs to do so. 19902529f56eSJonathan T. Looney * 19912529f56eSJonathan T. Looney * If force is false, this will only dump auto-logged sessions if 19922529f56eSJonathan T. Looney * tcp_log_auto_all is true or if there is a log ID defined for the session. 19932529f56eSJonathan T. Looney */ 19942529f56eSJonathan T. Looney int 19952529f56eSJonathan T. Looney tcp_log_dump_tp_logbuf(struct tcpcb *tp, char *reason, int how, bool force) 19962529f56eSJonathan T. Looney { 19972529f56eSJonathan T. Looney struct tcp_log_dev_log_queue *entry; 19982529f56eSJonathan T. Looney struct inpcb *inp; 19992529f56eSJonathan T. Looney #ifdef TCPLOG_DEBUG_COUNTERS 20002529f56eSJonathan T. Looney int num_entries; 20012529f56eSJonathan T. Looney #endif 20022529f56eSJonathan T. Looney 20032529f56eSJonathan T. Looney inp = tp->t_inpcb; 20042529f56eSJonathan T. Looney INP_WLOCK_ASSERT(inp); 20052529f56eSJonathan T. Looney 20062529f56eSJonathan T. Looney /* If there are no log entries, there is nothing to do. */ 20072529f56eSJonathan T. Looney if (tp->t_lognum == 0) 20082529f56eSJonathan T. Looney return (0); 20092529f56eSJonathan T. Looney 20102529f56eSJonathan T. Looney /* Check for a log ID. */ 20112529f56eSJonathan T. Looney if (tp->t_lib == NULL && (tp->t_flags2 & TF2_LOG_AUTO) && 20122529f56eSJonathan T. Looney !tcp_log_auto_all && !force) { 20132529f56eSJonathan T. Looney struct tcp_log_mem *log_entry; 20142529f56eSJonathan T. Looney 20152529f56eSJonathan T. Looney /* 20162529f56eSJonathan T. Looney * We needed a log ID and none was found. Free the log entries 20172529f56eSJonathan T. Looney * and return success. Also, cancel further logging. If the 20182529f56eSJonathan T. Looney * session doesn't have a log ID by now, we'll assume it isn't 20192529f56eSJonathan T. Looney * going to get one. 20202529f56eSJonathan T. Looney */ 20212529f56eSJonathan T. Looney while ((log_entry = STAILQ_FIRST(&tp->t_logs)) != NULL) 20222529f56eSJonathan T. Looney tcp_log_remove_log_head(tp, log_entry); 20232529f56eSJonathan T. Looney KASSERT(tp->t_lognum == 0, 20242529f56eSJonathan T. Looney ("%s: After freeing entries, tp->t_lognum=%d (expected 0)", 20252529f56eSJonathan T. Looney __func__, tp->t_lognum)); 20262529f56eSJonathan T. Looney tp->t_logstate = TCP_LOG_STATE_OFF; 20272529f56eSJonathan T. Looney return (0); 20282529f56eSJonathan T. Looney } 20292529f56eSJonathan T. Looney 20302529f56eSJonathan T. Looney /* 20312529f56eSJonathan T. Looney * Allocate memory. If we must wait, we'll need to drop the locks 20322529f56eSJonathan T. Looney * and reacquire them (and do all the related business that goes 20332529f56eSJonathan T. Looney * along with that). 20342529f56eSJonathan T. Looney */ 20352529f56eSJonathan T. Looney entry = malloc(sizeof(struct tcp_log_dev_log_queue), M_TCPLOGDEV, 20362529f56eSJonathan T. Looney M_NOWAIT); 20372529f56eSJonathan T. Looney if (entry == NULL && (how & M_NOWAIT)) { 20382529f56eSJonathan T. Looney #ifdef TCPLOG_DEBUG_COUNTERS 20392529f56eSJonathan T. Looney counter_u64_add(tcp_log_que_fail3, 1); 20402529f56eSJonathan T. Looney #endif 20412529f56eSJonathan T. Looney return (ENOBUFS); 20422529f56eSJonathan T. Looney } 20432529f56eSJonathan T. Looney if (entry == NULL) { 20442529f56eSJonathan T. Looney INP_WUNLOCK(inp); 20452529f56eSJonathan T. Looney entry = malloc(sizeof(struct tcp_log_dev_log_queue), 20462529f56eSJonathan T. Looney M_TCPLOGDEV, M_WAITOK); 20472529f56eSJonathan T. Looney INP_WLOCK(inp); 20482529f56eSJonathan T. Looney /* 20492529f56eSJonathan T. Looney * Note that this check is slightly overly-restrictive in 20502529f56eSJonathan T. Looney * that the TCB can survive either of these events. 20512529f56eSJonathan T. Looney * However, there is currently not a good way to ensure 20522529f56eSJonathan T. Looney * that is the case. So, if we hit this M_WAIT path, we 20532529f56eSJonathan T. Looney * may end up dropping some entries. That seems like a 20542529f56eSJonathan T. Looney * small price to pay for safety. 20552529f56eSJonathan T. Looney */ 20562529f56eSJonathan T. Looney if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 20572529f56eSJonathan T. Looney free(entry, M_TCPLOGDEV); 20582529f56eSJonathan T. Looney #ifdef TCPLOG_DEBUG_COUNTERS 20592529f56eSJonathan T. Looney counter_u64_add(tcp_log_que_fail2, 1); 20602529f56eSJonathan T. Looney #endif 20612529f56eSJonathan T. Looney return (ECONNRESET); 20622529f56eSJonathan T. Looney } 20632529f56eSJonathan T. Looney tp = intotcpcb(inp); 20642529f56eSJonathan T. Looney if (tp->t_lognum == 0) { 20652529f56eSJonathan T. Looney free(entry, M_TCPLOGDEV); 20662529f56eSJonathan T. Looney return (0); 20672529f56eSJonathan T. Looney } 20682529f56eSJonathan T. Looney } 20692529f56eSJonathan T. Looney 20702529f56eSJonathan T. Looney /* Fill in the unique parts of the queue entry. */ 20712529f56eSJonathan T. Looney if (tp->t_lib != NULL) 20722529f56eSJonathan T. Looney strlcpy(entry->tldl_id, tp->t_lib->tlb_id, TCP_LOG_ID_LEN); 20732529f56eSJonathan T. Looney else 20742529f56eSJonathan T. Looney strlcpy(entry->tldl_id, "UNKNOWN", TCP_LOG_ID_LEN); 20752529f56eSJonathan T. Looney if (reason != NULL) 20762529f56eSJonathan T. Looney strlcpy(entry->tldl_reason, reason, TCP_LOG_REASON_LEN); 20772529f56eSJonathan T. Looney else 20782529f56eSJonathan T. Looney strlcpy(entry->tldl_reason, "UNKNOWN", TCP_LOG_ID_LEN); 20792529f56eSJonathan T. Looney entry->tldl_ie = inp->inp_inc.inc_ie; 20802529f56eSJonathan T. Looney if (inp->inp_inc.inc_flags & INC_ISIPV6) 20812529f56eSJonathan T. Looney entry->tldl_af = AF_INET6; 20822529f56eSJonathan T. Looney else 20832529f56eSJonathan T. Looney entry->tldl_af = AF_INET; 20842529f56eSJonathan T. Looney entry->tldl_entries = tp->t_logs; 20852529f56eSJonathan T. Looney entry->tldl_count = tp->t_lognum; 20862529f56eSJonathan T. Looney 20872529f56eSJonathan T. Looney /* Fill in the common parts of the queue entry. */ 20882529f56eSJonathan T. Looney entry->tldl_common.tldq_buf = NULL; 20892529f56eSJonathan T. Looney entry->tldl_common.tldq_xform = tcp_log_expandlogbuf; 20902529f56eSJonathan T. Looney entry->tldl_common.tldq_dtor = tcp_log_free_queue; 20912529f56eSJonathan T. Looney 20922529f56eSJonathan T. Looney /* Clear the log data from the TCPCB. */ 20932529f56eSJonathan T. Looney #ifdef TCPLOG_DEBUG_COUNTERS 20942529f56eSJonathan T. Looney num_entries = tp->t_lognum; 20952529f56eSJonathan T. Looney #endif 20962529f56eSJonathan T. Looney tp->t_lognum = 0; 20972529f56eSJonathan T. Looney STAILQ_INIT(&tp->t_logs); 20982529f56eSJonathan T. Looney 20992529f56eSJonathan T. Looney /* Add the entry. If no one is listening, free the entry. */ 21002529f56eSJonathan T. Looney if (tcp_log_dev_add_log((struct tcp_log_dev_queue *)entry)) { 21012529f56eSJonathan T. Looney tcp_log_free_queue((struct tcp_log_dev_queue *)entry); 21022529f56eSJonathan T. Looney #ifdef TCPLOG_DEBUG_COUNTERS 21032529f56eSJonathan T. Looney counter_u64_add(tcp_log_que_fail1, num_entries); 21042529f56eSJonathan T. Looney } else { 21052529f56eSJonathan T. Looney counter_u64_add(tcp_log_queued, num_entries); 21062529f56eSJonathan T. Looney #endif 21072529f56eSJonathan T. Looney } 21082529f56eSJonathan T. Looney return (0); 21092529f56eSJonathan T. Looney } 21102529f56eSJonathan T. Looney 21112529f56eSJonathan T. Looney /* 21122529f56eSJonathan T. Looney * Queue the log_id_node's log buffers for transmission via the log buffer 21132529f56eSJonathan T. Looney * facility. 21142529f56eSJonathan T. Looney * 21152529f56eSJonathan T. Looney * NOTE: This should be called with the bucket locked and referenced. 21162529f56eSJonathan T. Looney * 21172529f56eSJonathan T. Looney * how should be M_WAITOK or M_NOWAIT. If M_WAITOK, the function will drop 21182529f56eSJonathan T. Looney * and reacquire the bucket lock if it needs to do so. (The caller must 21192529f56eSJonathan T. Looney * ensure that the tln is no longer on any lists so no one else will mess 21202529f56eSJonathan T. Looney * with this while the lock is dropped!) 21212529f56eSJonathan T. Looney */ 21222529f56eSJonathan T. Looney static int 21232529f56eSJonathan T. Looney tcp_log_dump_node_logbuf(struct tcp_log_id_node *tln, char *reason, int how) 21242529f56eSJonathan T. Looney { 21252529f56eSJonathan T. Looney struct tcp_log_dev_log_queue *entry; 21262529f56eSJonathan T. Looney struct tcp_log_id_bucket *tlb; 21272529f56eSJonathan T. Looney 21282529f56eSJonathan T. Looney tlb = tln->tln_bucket; 21292529f56eSJonathan T. Looney TCPID_BUCKET_LOCK_ASSERT(tlb); 21302529f56eSJonathan T. Looney KASSERT(tlb->tlb_refcnt > 0, 21312529f56eSJonathan T. Looney ("%s:%d: Called with unreferenced bucket (tln=%p, tlb=%p)", 21322529f56eSJonathan T. Looney __func__, __LINE__, tln, tlb)); 21332529f56eSJonathan T. Looney KASSERT(tln->tln_closed, 21342529f56eSJonathan T. Looney ("%s:%d: Called for node with tln_closed==false (tln=%p)", 21352529f56eSJonathan T. Looney __func__, __LINE__, tln)); 21362529f56eSJonathan T. Looney 21372529f56eSJonathan T. Looney /* If there are no log entries, there is nothing to do. */ 21382529f56eSJonathan T. Looney if (tln->tln_count == 0) 21392529f56eSJonathan T. Looney return (0); 21402529f56eSJonathan T. Looney 21412529f56eSJonathan T. Looney /* 21422529f56eSJonathan T. Looney * Allocate memory. If we must wait, we'll need to drop the locks 21432529f56eSJonathan T. Looney * and reacquire them (and do all the related business that goes 21442529f56eSJonathan T. Looney * along with that). 21452529f56eSJonathan T. Looney */ 21462529f56eSJonathan T. Looney entry = malloc(sizeof(struct tcp_log_dev_log_queue), M_TCPLOGDEV, 21472529f56eSJonathan T. Looney M_NOWAIT); 21482529f56eSJonathan T. Looney if (entry == NULL && (how & M_NOWAIT)) 21492529f56eSJonathan T. Looney return (ENOBUFS); 21502529f56eSJonathan T. Looney if (entry == NULL) { 21512529f56eSJonathan T. Looney TCPID_BUCKET_UNLOCK(tlb); 21522529f56eSJonathan T. Looney entry = malloc(sizeof(struct tcp_log_dev_log_queue), 21532529f56eSJonathan T. Looney M_TCPLOGDEV, M_WAITOK); 21542529f56eSJonathan T. Looney TCPID_BUCKET_LOCK(tlb); 21552529f56eSJonathan T. Looney } 21562529f56eSJonathan T. Looney 21572529f56eSJonathan T. Looney /* Fill in the common parts of the queue entry.. */ 21582529f56eSJonathan T. Looney entry->tldl_common.tldq_buf = NULL; 21592529f56eSJonathan T. Looney entry->tldl_common.tldq_xform = tcp_log_expandlogbuf; 21602529f56eSJonathan T. Looney entry->tldl_common.tldq_dtor = tcp_log_free_queue; 21612529f56eSJonathan T. Looney 21622529f56eSJonathan T. Looney /* Fill in the unique parts of the queue entry. */ 21632529f56eSJonathan T. Looney strlcpy(entry->tldl_id, tlb->tlb_id, TCP_LOG_ID_LEN); 21642529f56eSJonathan T. Looney if (reason != NULL) 21652529f56eSJonathan T. Looney strlcpy(entry->tldl_reason, reason, TCP_LOG_REASON_LEN); 21662529f56eSJonathan T. Looney else 21672529f56eSJonathan T. Looney strlcpy(entry->tldl_reason, "UNKNOWN", TCP_LOG_ID_LEN); 21682529f56eSJonathan T. Looney entry->tldl_ie = tln->tln_ie; 21692529f56eSJonathan T. Looney entry->tldl_entries = tln->tln_entries; 21702529f56eSJonathan T. Looney entry->tldl_count = tln->tln_count; 21712529f56eSJonathan T. Looney entry->tldl_af = tln->tln_af; 21722529f56eSJonathan T. Looney 21732529f56eSJonathan T. Looney /* Add the entry. If no one is listening, free the entry. */ 21742529f56eSJonathan T. Looney if (tcp_log_dev_add_log((struct tcp_log_dev_queue *)entry)) 21752529f56eSJonathan T. Looney tcp_log_free_queue((struct tcp_log_dev_queue *)entry); 21762529f56eSJonathan T. Looney 21772529f56eSJonathan T. Looney return (0); 21782529f56eSJonathan T. Looney } 21792529f56eSJonathan T. Looney 21802529f56eSJonathan T. Looney 21812529f56eSJonathan T. Looney /* 21822529f56eSJonathan T. Looney * Queue the log buffers for all sessions in a bucket for transmissions via 21832529f56eSJonathan T. Looney * the log buffer facility. 21842529f56eSJonathan T. Looney * 21852529f56eSJonathan T. Looney * NOTE: This should be called with a locked bucket; however, the function 21862529f56eSJonathan T. Looney * will drop the lock. 21872529f56eSJonathan T. Looney */ 21882529f56eSJonathan T. Looney #define LOCAL_SAVE 10 21892529f56eSJonathan T. Looney static void 21902529f56eSJonathan T. Looney tcp_log_dumpbucketlogs(struct tcp_log_id_bucket *tlb, char *reason) 21912529f56eSJonathan T. Looney { 21922529f56eSJonathan T. Looney struct tcp_log_id_node local_entries[LOCAL_SAVE]; 21932529f56eSJonathan T. Looney struct inpcb *inp; 21942529f56eSJonathan T. Looney struct tcpcb *tp; 21952529f56eSJonathan T. Looney struct tcp_log_id_node *cur_tln, *prev_tln, *tmp_tln; 21962529f56eSJonathan T. Looney int i, num_local_entries, tree_locked; 21972529f56eSJonathan T. Looney bool expireq_locked; 21982529f56eSJonathan T. Looney 21992529f56eSJonathan T. Looney TCPID_BUCKET_LOCK_ASSERT(tlb); 22002529f56eSJonathan T. Looney 22012529f56eSJonathan T. Looney /* 22022529f56eSJonathan T. Looney * Take a reference on the bucket to keep it from disappearing until 22032529f56eSJonathan T. Looney * we are done. 22042529f56eSJonathan T. Looney */ 22052529f56eSJonathan T. Looney TCPID_BUCKET_REF(tlb); 22062529f56eSJonathan T. Looney 22072529f56eSJonathan T. Looney /* 22082529f56eSJonathan T. Looney * We'll try to create these without dropping locks. However, we 22092529f56eSJonathan T. Looney * might very well need to drop locks to get memory. If that's the 22102529f56eSJonathan T. Looney * case, we'll save up to 10 on the stack, and sacrifice the rest. 22112529f56eSJonathan T. Looney * (Otherwise, we need to worry about finding our place again in a 22122529f56eSJonathan T. Looney * potentially changed list. It just doesn't seem worth the trouble 22132529f56eSJonathan T. Looney * to do that. 22142529f56eSJonathan T. Looney */ 22152529f56eSJonathan T. Looney expireq_locked = false; 22162529f56eSJonathan T. Looney num_local_entries = 0; 22172529f56eSJonathan T. Looney prev_tln = NULL; 22182529f56eSJonathan T. Looney tree_locked = TREE_UNLOCKED; 22192529f56eSJonathan T. Looney SLIST_FOREACH_SAFE(cur_tln, &tlb->tlb_head, tln_list, tmp_tln) { 22202529f56eSJonathan T. Looney /* 22212529f56eSJonathan T. Looney * If this isn't associated with a TCPCB, we can pull it off 22222529f56eSJonathan T. Looney * the list now. We need to be careful that the expire timer 22232529f56eSJonathan T. Looney * hasn't already taken ownership (tln_expiretime == SBT_MAX). 22242529f56eSJonathan T. Looney * If so, we let the expire timer code free the data. 22252529f56eSJonathan T. Looney */ 22262529f56eSJonathan T. Looney if (cur_tln->tln_closed) { 22272529f56eSJonathan T. Looney no_inp: 22282529f56eSJonathan T. Looney /* 22292529f56eSJonathan T. Looney * Get the expireq lock so we can get a consistent 22302529f56eSJonathan T. Looney * read of tln_expiretime and so we can remove this 22312529f56eSJonathan T. Looney * from the expireq. 22322529f56eSJonathan T. Looney */ 22332529f56eSJonathan T. Looney if (!expireq_locked) { 22342529f56eSJonathan T. Looney TCPLOG_EXPIREQ_LOCK(); 22352529f56eSJonathan T. Looney expireq_locked = true; 22362529f56eSJonathan T. Looney } 22372529f56eSJonathan T. Looney 22382529f56eSJonathan T. Looney /* 22392529f56eSJonathan T. Looney * We ignore entries with tln_expiretime == SBT_MAX. 22402529f56eSJonathan T. Looney * The expire timer code already owns those. 22412529f56eSJonathan T. Looney */ 22422529f56eSJonathan T. Looney KASSERT(cur_tln->tln_expiretime > (sbintime_t) 0, 22432529f56eSJonathan T. Looney ("%s:%d: node on the expire queue without positive " 22442529f56eSJonathan T. Looney "expire time", __func__, __LINE__)); 22452529f56eSJonathan T. Looney if (cur_tln->tln_expiretime == SBT_MAX) { 22462529f56eSJonathan T. Looney prev_tln = cur_tln; 22472529f56eSJonathan T. Looney continue; 22482529f56eSJonathan T. Looney } 22492529f56eSJonathan T. Looney 22502529f56eSJonathan T. Looney /* Remove the entry from the expireq. */ 22512529f56eSJonathan T. Looney STAILQ_REMOVE(&tcp_log_expireq_head, cur_tln, 22522529f56eSJonathan T. Looney tcp_log_id_node, tln_expireq); 22532529f56eSJonathan T. Looney 22542529f56eSJonathan T. Looney /* Remove the entry from the bucket. */ 22552529f56eSJonathan T. Looney if (prev_tln != NULL) 22562529f56eSJonathan T. Looney SLIST_REMOVE_AFTER(prev_tln, tln_list); 22572529f56eSJonathan T. Looney else 22582529f56eSJonathan T. Looney SLIST_REMOVE_HEAD(&tlb->tlb_head, tln_list); 22592529f56eSJonathan T. Looney 22602529f56eSJonathan T. Looney /* 22612529f56eSJonathan T. Looney * Drop the INP and bucket reference counts. Due to 22622529f56eSJonathan T. Looney * lock-ordering rules, we need to drop the expire 22632529f56eSJonathan T. Looney * queue lock. 22642529f56eSJonathan T. Looney */ 22652529f56eSJonathan T. Looney TCPLOG_EXPIREQ_UNLOCK(); 22662529f56eSJonathan T. Looney expireq_locked = false; 22672529f56eSJonathan T. Looney 22682529f56eSJonathan T. Looney /* Drop the INP reference. */ 22692529f56eSJonathan T. Looney INP_WLOCK(cur_tln->tln_inp); 22702529f56eSJonathan T. Looney if (!in_pcbrele_wlocked(cur_tln->tln_inp)) 22712529f56eSJonathan T. Looney INP_WUNLOCK(cur_tln->tln_inp); 22722529f56eSJonathan T. Looney 22732529f56eSJonathan T. Looney if (tcp_log_unref_bucket(tlb, &tree_locked, NULL)) { 22742529f56eSJonathan T. Looney #ifdef INVARIANTS 22752529f56eSJonathan T. Looney panic("%s: Bucket refcount unexpectedly 0.", 22762529f56eSJonathan T. Looney __func__); 22772529f56eSJonathan T. Looney #endif 22782529f56eSJonathan T. Looney /* 22792529f56eSJonathan T. Looney * Recover as best we can: free the entry we 22802529f56eSJonathan T. Looney * own. 22812529f56eSJonathan T. Looney */ 22822529f56eSJonathan T. Looney tcp_log_free_entries(&cur_tln->tln_entries, 22832529f56eSJonathan T. Looney &cur_tln->tln_count); 22842529f56eSJonathan T. Looney uma_zfree(tcp_log_node_zone, cur_tln); 22852529f56eSJonathan T. Looney goto done; 22862529f56eSJonathan T. Looney } 22872529f56eSJonathan T. Looney 22882529f56eSJonathan T. Looney if (tcp_log_dump_node_logbuf(cur_tln, reason, 22892529f56eSJonathan T. Looney M_NOWAIT)) { 22902529f56eSJonathan T. Looney /* 22912529f56eSJonathan T. Looney * If we have sapce, save the entries locally. 22922529f56eSJonathan T. Looney * Otherwise, free them. 22932529f56eSJonathan T. Looney */ 22942529f56eSJonathan T. Looney if (num_local_entries < LOCAL_SAVE) { 22952529f56eSJonathan T. Looney local_entries[num_local_entries] = 22962529f56eSJonathan T. Looney *cur_tln; 22972529f56eSJonathan T. Looney num_local_entries++; 22982529f56eSJonathan T. Looney } else { 22992529f56eSJonathan T. Looney tcp_log_free_entries( 23002529f56eSJonathan T. Looney &cur_tln->tln_entries, 23012529f56eSJonathan T. Looney &cur_tln->tln_count); 23022529f56eSJonathan T. Looney } 23032529f56eSJonathan T. Looney } 23042529f56eSJonathan T. Looney 23052529f56eSJonathan T. Looney /* No matter what, we are done with the node now. */ 23062529f56eSJonathan T. Looney uma_zfree(tcp_log_node_zone, cur_tln); 23072529f56eSJonathan T. Looney 23082529f56eSJonathan T. Looney /* 23092529f56eSJonathan T. Looney * Because we removed this entry from the list, prev_tln 23102529f56eSJonathan T. Looney * (which tracks the previous entry still on the tlb 23112529f56eSJonathan T. Looney * list) remains unchanged. 23122529f56eSJonathan T. Looney */ 23132529f56eSJonathan T. Looney continue; 23142529f56eSJonathan T. Looney } 23152529f56eSJonathan T. Looney 23162529f56eSJonathan T. Looney /* 23172529f56eSJonathan T. Looney * If we get to this point, the session data is still held in 23182529f56eSJonathan T. Looney * the TCPCB. So, we need to pull the data out of that. 23192529f56eSJonathan T. Looney * 23202529f56eSJonathan T. Looney * We will need to drop the expireq lock so we can lock the INP. 23212529f56eSJonathan T. Looney * We can then try to extract the data the "easy" way. If that 23222529f56eSJonathan T. Looney * fails, we'll save the log entries for later. 23232529f56eSJonathan T. Looney */ 23242529f56eSJonathan T. Looney if (expireq_locked) { 23252529f56eSJonathan T. Looney TCPLOG_EXPIREQ_UNLOCK(); 23262529f56eSJonathan T. Looney expireq_locked = false; 23272529f56eSJonathan T. Looney } 23282529f56eSJonathan T. Looney 23292529f56eSJonathan T. Looney /* Lock the INP and then re-check the state. */ 23302529f56eSJonathan T. Looney inp = cur_tln->tln_inp; 23312529f56eSJonathan T. Looney INP_WLOCK(inp); 23322529f56eSJonathan T. Looney /* 23332529f56eSJonathan T. Looney * If we caught this while it was transitioning, the data 23342529f56eSJonathan T. Looney * might have moved from the TCPCB to the tln (signified by 23352529f56eSJonathan T. Looney * setting tln_closed to true. If so, treat this like an 23362529f56eSJonathan T. Looney * inactive connection. 23372529f56eSJonathan T. Looney */ 23382529f56eSJonathan T. Looney if (cur_tln->tln_closed) { 23392529f56eSJonathan T. Looney /* 23402529f56eSJonathan T. Looney * It looks like we may have caught this connection 23412529f56eSJonathan T. Looney * while it was transitioning from active to inactive. 23422529f56eSJonathan T. Looney * Treat this like an inactive connection. 23432529f56eSJonathan T. Looney */ 23442529f56eSJonathan T. Looney INP_WUNLOCK(inp); 23452529f56eSJonathan T. Looney goto no_inp; 23462529f56eSJonathan T. Looney } 23472529f56eSJonathan T. Looney 23482529f56eSJonathan T. Looney /* 23492529f56eSJonathan T. Looney * Try to dump the data from the tp without dropping the lock. 23502529f56eSJonathan T. Looney * If this fails, try to save off the data locally. 23512529f56eSJonathan T. Looney */ 23522529f56eSJonathan T. Looney tp = cur_tln->tln_tp; 23532529f56eSJonathan T. Looney if (tcp_log_dump_tp_logbuf(tp, reason, M_NOWAIT, true) && 23542529f56eSJonathan T. Looney num_local_entries < LOCAL_SAVE) { 23552529f56eSJonathan T. Looney tcp_log_move_tp_to_node(tp, 23562529f56eSJonathan T. Looney &local_entries[num_local_entries]); 23572529f56eSJonathan T. Looney local_entries[num_local_entries].tln_closed = 1; 23582529f56eSJonathan T. Looney KASSERT(local_entries[num_local_entries].tln_bucket == 23592529f56eSJonathan T. Looney tlb, ("%s: %d: bucket mismatch for node %p", 23602529f56eSJonathan T. Looney __func__, __LINE__, cur_tln)); 23612529f56eSJonathan T. Looney num_local_entries++; 23622529f56eSJonathan T. Looney } 23632529f56eSJonathan T. Looney 23642529f56eSJonathan T. Looney INP_WUNLOCK(inp); 23652529f56eSJonathan T. Looney 23662529f56eSJonathan T. Looney /* 23672529f56eSJonathan T. Looney * We are goint to leave the current tln on the list. It will 23682529f56eSJonathan T. Looney * become the previous tln. 23692529f56eSJonathan T. Looney */ 23702529f56eSJonathan T. Looney prev_tln = cur_tln; 23712529f56eSJonathan T. Looney } 23722529f56eSJonathan T. Looney 23732529f56eSJonathan T. Looney /* Drop our locks, if any. */ 23742529f56eSJonathan T. Looney KASSERT(tree_locked == TREE_UNLOCKED, 23752529f56eSJonathan T. Looney ("%s: %d: tree unexpectedly locked", __func__, __LINE__)); 23762529f56eSJonathan T. Looney switch (tree_locked) { 23772529f56eSJonathan T. Looney case TREE_WLOCKED: 23782529f56eSJonathan T. Looney TCPID_TREE_WUNLOCK(); 23792529f56eSJonathan T. Looney tree_locked = TREE_UNLOCKED; 23802529f56eSJonathan T. Looney break; 23812529f56eSJonathan T. Looney case TREE_RLOCKED: 23822529f56eSJonathan T. Looney TCPID_TREE_RUNLOCK(); 23832529f56eSJonathan T. Looney tree_locked = TREE_UNLOCKED; 23842529f56eSJonathan T. Looney break; 23852529f56eSJonathan T. Looney } 23862529f56eSJonathan T. Looney if (expireq_locked) { 23872529f56eSJonathan T. Looney TCPLOG_EXPIREQ_UNLOCK(); 23882529f56eSJonathan T. Looney expireq_locked = false; 23892529f56eSJonathan T. Looney } 23902529f56eSJonathan T. Looney 23912529f56eSJonathan T. Looney /* 23922529f56eSJonathan T. Looney * Try again for any saved entries. tcp_log_dump_node_logbuf() is 23932529f56eSJonathan T. Looney * guaranteed to free the log entries within the node. And, since 23942529f56eSJonathan T. Looney * the node itself is on our stack, we don't need to free it. 23952529f56eSJonathan T. Looney */ 23962529f56eSJonathan T. Looney for (i = 0; i < num_local_entries; i++) 23972529f56eSJonathan T. Looney tcp_log_dump_node_logbuf(&local_entries[i], reason, M_WAITOK); 23982529f56eSJonathan T. Looney 23992529f56eSJonathan T. Looney /* Drop our reference. */ 24002529f56eSJonathan T. Looney if (!tcp_log_unref_bucket(tlb, &tree_locked, NULL)) 24012529f56eSJonathan T. Looney TCPID_BUCKET_UNLOCK(tlb); 24022529f56eSJonathan T. Looney 24032529f56eSJonathan T. Looney done: 24042529f56eSJonathan T. Looney /* Drop our locks, if any. */ 24052529f56eSJonathan T. Looney switch (tree_locked) { 24062529f56eSJonathan T. Looney case TREE_WLOCKED: 24072529f56eSJonathan T. Looney TCPID_TREE_WUNLOCK(); 24082529f56eSJonathan T. Looney break; 24092529f56eSJonathan T. Looney case TREE_RLOCKED: 24102529f56eSJonathan T. Looney TCPID_TREE_RUNLOCK(); 24112529f56eSJonathan T. Looney break; 24122529f56eSJonathan T. Looney } 24132529f56eSJonathan T. Looney if (expireq_locked) 24142529f56eSJonathan T. Looney TCPLOG_EXPIREQ_UNLOCK(); 24152529f56eSJonathan T. Looney } 24162529f56eSJonathan T. Looney #undef LOCAL_SAVE 24172529f56eSJonathan T. Looney 24182529f56eSJonathan T. Looney 24192529f56eSJonathan T. Looney /* 24202529f56eSJonathan T. Looney * Queue the log buffers for all sessions in a bucket for transmissions via 24212529f56eSJonathan T. Looney * the log buffer facility. 24222529f56eSJonathan T. Looney * 24232529f56eSJonathan T. Looney * NOTE: This should be called with a locked INP; however, the function 24242529f56eSJonathan T. Looney * will drop the lock. 24252529f56eSJonathan T. Looney */ 24262529f56eSJonathan T. Looney void 24272529f56eSJonathan T. Looney tcp_log_dump_tp_bucket_logbufs(struct tcpcb *tp, char *reason) 24282529f56eSJonathan T. Looney { 24292529f56eSJonathan T. Looney struct tcp_log_id_bucket *tlb; 24302529f56eSJonathan T. Looney int tree_locked; 24312529f56eSJonathan T. Looney 24322529f56eSJonathan T. Looney /* Figure out our bucket and lock it. */ 24332529f56eSJonathan T. Looney INP_WLOCK_ASSERT(tp->t_inpcb); 24342529f56eSJonathan T. Looney tlb = tp->t_lib; 24352529f56eSJonathan T. Looney if (tlb == NULL) { 24362529f56eSJonathan T. Looney /* 24372529f56eSJonathan T. Looney * No bucket; treat this like a request to dump a single 24382529f56eSJonathan T. Looney * session's traces. 24392529f56eSJonathan T. Looney */ 24402529f56eSJonathan T. Looney (void)tcp_log_dump_tp_logbuf(tp, reason, M_WAITOK, true); 24412529f56eSJonathan T. Looney INP_WUNLOCK(tp->t_inpcb); 24422529f56eSJonathan T. Looney return; 24432529f56eSJonathan T. Looney } 24442529f56eSJonathan T. Looney TCPID_BUCKET_REF(tlb); 24452529f56eSJonathan T. Looney INP_WUNLOCK(tp->t_inpcb); 24462529f56eSJonathan T. Looney TCPID_BUCKET_LOCK(tlb); 24472529f56eSJonathan T. Looney 24482529f56eSJonathan T. Looney /* If we are the last reference, we have nothing more to do here. */ 24492529f56eSJonathan T. Looney tree_locked = TREE_UNLOCKED; 24502529f56eSJonathan T. Looney if (tcp_log_unref_bucket(tlb, &tree_locked, NULL)) { 24512529f56eSJonathan T. Looney switch (tree_locked) { 24522529f56eSJonathan T. Looney case TREE_WLOCKED: 24532529f56eSJonathan T. Looney TCPID_TREE_WUNLOCK(); 24542529f56eSJonathan T. Looney break; 24552529f56eSJonathan T. Looney case TREE_RLOCKED: 24562529f56eSJonathan T. Looney TCPID_TREE_RUNLOCK(); 24572529f56eSJonathan T. Looney break; 24582529f56eSJonathan T. Looney } 24592529f56eSJonathan T. Looney return; 24602529f56eSJonathan T. Looney } 24612529f56eSJonathan T. Looney 24622529f56eSJonathan T. Looney /* Turn this over to tcp_log_dumpbucketlogs() to finish the work. */ 24632529f56eSJonathan T. Looney tcp_log_dumpbucketlogs(tlb, reason); 24642529f56eSJonathan T. Looney } 24652529f56eSJonathan T. Looney 24662529f56eSJonathan T. Looney /* 24672529f56eSJonathan T. Looney * Mark the end of a flow with the current stack. A stack can add 24682529f56eSJonathan T. Looney * stack-specific info to this trace event by overriding this 24692529f56eSJonathan T. Looney * function (see bbr_log_flowend() for example). 24702529f56eSJonathan T. Looney */ 24712529f56eSJonathan T. Looney void 24722529f56eSJonathan T. Looney tcp_log_flowend(struct tcpcb *tp) 24732529f56eSJonathan T. Looney { 24742529f56eSJonathan T. Looney if (tp->t_logstate != TCP_LOG_STATE_OFF) { 24752529f56eSJonathan T. Looney struct socket *so = tp->t_inpcb->inp_socket; 24762529f56eSJonathan T. Looney TCP_LOG_EVENT(tp, NULL, &so->so_rcv, &so->so_snd, 24772529f56eSJonathan T. Looney TCP_LOG_FLOWEND, 0, 0, NULL, false); 24782529f56eSJonathan T. Looney } 24792529f56eSJonathan T. Looney } 24802529f56eSJonathan T. Looney 2481