xref: /freebsd/sys/netinet/tcp_log_buf.h (revision ccb59683b98360afaf5b5bb641a68fea22c68d0b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2016-2020 Netflix, Inc.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29 
30 #ifndef __tcp_log_buf_h__
31 #define __tcp_log_buf_h__
32 
33 #define	TCP_LOG_REASON_LEN	32
34 #define	TCP_LOG_TAG_LEN		32
35 #define	TCP_LOG_BUF_VER		(9)
36 
37 /*
38  * Because the (struct tcp_log_buffer) includes 8-byte uint64_t's, it requires
39  * 8-byte alignment to work properly on all platforms. Therefore, we will
40  * enforce 8-byte alignment for all the structures that may appear by
41  * themselves (instead of being embedded in another structure) in a data
42  * stream.
43  */
44 #define	ALIGN_TCP_LOG		__aligned(8)
45 
46 /* Information about the socketbuffer state. */
47 struct tcp_log_sockbuf
48 {
49 	uint32_t	tls_sb_acc;	/* available chars (sb->sb_acc) */
50 	uint32_t	tls_sb_ccc;	/* claimed chars (sb->sb_ccc) */
51 	uint32_t	tls_sb_spare;	/* spare */
52 };
53 
54 /* Optional, verbose information that may be appended to an event log. */
55 struct tcp_log_verbose
56 {
57 #define	TCP_FUNC_LEN	32
58 	char		tlv_snd_frm[TCP_FUNC_LEN]; /* tcp_output() caller */
59 	char		tlv_trace_func[TCP_FUNC_LEN]; /* Function that
60 							 generated trace */
61 	uint32_t	tlv_trace_line;	/* Line number that generated trace */
62 	uint8_t		_pad[4];
63 } ALIGN_TCP_LOG;
64 
65 /* Internal RACK state variables. */
66 struct tcp_log_rack
67 {
68 	uint32_t	tlr_rack_rtt;		/* rc_rack_rtt */
69 	uint8_t		tlr_state;		/* Internal RACK state */
70 	uint8_t		_pad[3];		/* Padding */
71 };
72 
73 struct tcp_log_bbr {
74 	uint64_t cur_del_rate;
75 	uint64_t delRate;
76 	uint64_t rttProp;
77 	uint64_t bw_inuse;
78 	uint32_t inflight;
79 	uint32_t applimited;
80 	uint32_t delivered;
81 	uint32_t timeStamp;
82 	uint32_t epoch;
83 	uint32_t lt_epoch;
84 	uint32_t pkts_out;
85 	uint32_t flex1;
86 	uint32_t flex2;
87 	uint32_t flex3;
88 	uint32_t flex4;
89 	uint32_t flex5;
90 	uint32_t flex6;
91 	uint32_t lost;
92 	uint16_t pacing_gain;
93 	uint16_t cwnd_gain;
94 	uint16_t flex7;
95 	uint8_t bbr_state;
96 	uint8_t bbr_substate;
97 	uint8_t inhpts;
98 	uint8_t __spare;
99 	uint8_t use_lt_bw;
100 	uint8_t flex8;
101 	uint32_t pkt_epoch;
102 };
103 
104 /* shadows tcp_log_bbr struct element sizes */
105 struct tcp_log_raw {
106 	uint64_t u64_flex[4];
107 	uint32_t u32_flex[14];
108 	uint16_t u16_flex[3];
109 	uint8_t u8_flex[6];
110 	uint32_t u32_flex2[1];
111 };
112 
113 struct tcp_log_uint64 {
114 	uint64_t u64_flex[13];
115 };
116 
117 struct tcp_log_sendfile {
118 	uint64_t offset;
119 	uint64_t length;
120 	uint32_t flags;
121 };
122 
123 /*
124  * tcp_log_stackspecific is currently being used as "event specific" log
125  * info by all stacks (i.e. struct tcp_log_bbr is used for generic event
126  * logging). Until this is cleaned up more generically and throughout,
127  * allow events to use the same space in the union.
128  */
129 union tcp_log_stackspecific
130 {
131 	struct tcp_log_rack u_rack;
132 	struct tcp_log_bbr u_bbr;
133 	struct tcp_log_sendfile u_sf;
134 	struct tcp_log_raw u_raw;	/* "raw" log access */
135 	struct tcp_log_uint64 u64_raw;	/* just u64's - used by process info */
136 };
137 
138 typedef union tcp_log_stackspecific tcp_log_eventspecific_t;
139 
140 struct tcp_log_buffer
141 {
142 	/* Event basics */
143 	struct timeval	tlb_tv;		/* Timestamp of trace */
144 	uint32_t	tlb_ticks;	/* Timestamp of trace */
145 	uint32_t	tlb_sn;		/* Serial number */
146 	uint8_t		tlb_stackid;	/* Stack ID */
147 	uint8_t		tlb_eventid;	/* Event ID */
148 	uint16_t	tlb_eventflags;	/* Flags for the record */
149 #define	TLB_FLAG_RXBUF		0x0001	/* Includes receive buffer info */
150 #define	TLB_FLAG_TXBUF		0x0002	/* Includes send buffer info */
151 #define	TLB_FLAG_HDR		0x0004	/* Includes a TCP header */
152 #define	TLB_FLAG_VERBOSE	0x0008	/* Includes function/line numbers */
153 #define	TLB_FLAG_STACKINFO	0x0010	/* Includes stack-specific info */
154 	int		tlb_errno;	/* Event error (if any) */
155 
156 	/* Internal session state */
157 	struct tcp_log_sockbuf tlb_rxbuf; /* Receive buffer */
158 	struct tcp_log_sockbuf tlb_txbuf; /* Send buffer */
159 
160 	int		tlb_state;	/* TCPCB t_state */
161 	uint32_t	tlb_starttime;	/* TCPCB t_starttime */
162 	uint32_t	tlb_iss;	/* TCPCB iss */
163 	uint32_t	tlb_flags;	/* TCPCB flags */
164 	uint32_t	tlb_snd_una;	/* TCPCB snd_una */
165 	uint32_t	tlb_snd_max;	/* TCPCB snd_max */
166 	uint32_t	tlb_snd_cwnd;	/* TCPCB snd_cwnd */
167 	uint32_t	tlb_snd_nxt;	/* TCPCB snd_nxt */
168 	uint32_t	tlb_snd_recover;/* TCPCB snd_recover */
169 	uint32_t	tlb_snd_wnd;	/* TCPCB snd_wnd */
170 	uint32_t	tlb_snd_ssthresh; /* TCPCB snd_ssthresh */
171 	uint32_t	tlb_srtt;	/* TCPCB t_srtt */
172 	uint32_t	tlb_rttvar;	/* TCPCB t_rttvar */
173 	uint32_t	tlb_rcv_up;	/* TCPCB rcv_up */
174 	uint32_t	tlb_rcv_adv;	/* TCPCB rcv_adv */
175 	uint32_t	tlb_flags2;	/* TCPCB t_flags2 */
176 	uint32_t	tlb_rcv_nxt;	/* TCPCB rcv_nxt */
177 	uint32_t	tlb_rcv_wnd;	/* TCPCB rcv_wnd */
178 	uint32_t	tlb_dupacks;	/* TCPCB t_dupacks */
179 	int		tlb_segqlen;	/* TCPCB segqlen */
180 	int		tlb_snd_numholes; /* TCPCB snd_numholes */
181 	uint32_t	tlb_flex1;	/* Event specific information */
182 	uint32_t	tlb_flex2;	/* Event specific information */
183 	uint32_t	tlb_fbyte_in;	/* TCPCB first byte in time */
184 	uint32_t	tlb_fbyte_out;	/* TCPCB first byte out time */
185 	uint8_t		tlb_snd_scale:4, /* TCPCB snd_scale */
186 			tlb_rcv_scale:4; /* TCPCB rcv_scale */
187 	uint8_t		_pad[3];	/* Padding */
188 	/* Per-stack info */
189 	union tcp_log_stackspecific tlb_stackinfo;
190 #define	tlb_rack	tlb_stackinfo.u_rack
191 
192 	/* The packet */
193 	uint32_t	tlb_len;	/* The packet's data length */
194 	struct tcphdr	tlb_th;		/* The TCP header */
195 	uint8_t		tlb_opts[TCP_MAXOLEN]; /* The TCP options */
196 
197 	/* Verbose information (optional) */
198 	struct tcp_log_verbose tlb_verbose[0];
199 } ALIGN_TCP_LOG;
200 
201 enum tcp_log_events {
202 	TCP_LOG_IN = 1,		/* Incoming packet                   1 */
203 	TCP_LOG_OUT,		/* Transmit (without other event)    2 */
204 	TCP_LOG_RTO,		/* Retransmit timeout                3 */
205 	TCP_LOG_SB_WAKE,	/* Awaken socket buffer              4 */
206 	TCP_LOG_BAD_RETRAN,	/* Detected bad retransmission       5 */
207 	TCP_LOG_PRR,		/* Doing PRR                         6 */
208 	TCP_LOG_REORDER,	/* Detected reorder                  7 */
209 	TCP_LOG_HPTS,		/* Hpts sending a packet             8 */
210 	BBR_LOG_BBRUPD,		/* We updated BBR info               9 */
211 	BBR_LOG_BBRSND,		/* We did a slot calculation and sending is done 10 */
212 	BBR_LOG_ACKCLEAR,	/* A ack clears all outstanding     11 */
213 	BBR_LOG_INQUEUE,	/* The tcb had a packet input to it 12 */
214 	BBR_LOG_TIMERSTAR,	/* Start a timer                    13 */
215 	BBR_LOG_TIMERCANC,	/* Cancel a timer                   14 */
216 	BBR_LOG_ENTREC,		/* Entered recovery                 15 */
217 	BBR_LOG_EXITREC,	/* Exited recovery                  16 */
218 	BBR_LOG_CWND,		/* Cwnd change                      17 */
219 	BBR_LOG_BWSAMP,		/* LT B/W sample has been made      18 */
220 	BBR_LOG_MSGSIZE,	/* We received a EMSGSIZE error     19 */
221 	BBR_LOG_BBRRTT,		/* BBR RTT is updated               20 */
222 	BBR_LOG_JUSTRET,	/* We just returned out of output   21 */
223 	BBR_LOG_STATE,		/* A BBR state change occurred      22 */
224 	BBR_LOG_PKT_EPOCH,      /* A BBR packet epoch occurred      23 */
225 	BBR_LOG_PERSIST,        /* BBR changed to/from a persists   24 */
226 	TCP_LOG_FLOWEND,        /* End of a flow                    25 */
227 	BBR_LOG_RTO,            /* BBR's timeout includes BBR info  26 */
228 	BBR_LOG_DOSEG_DONE,     /* hpts do_segment completes        27 */
229 	BBR_LOG_EXIT_GAIN,      /* hpts do_segment completes        28 */
230 	BBR_LOG_THRESH_CALC,    /* Doing threshold calculation      29 */
231 	TCP_LOG_MAPCHG,		/* Map Changes to the sendmap       30 */
232 	TCP_LOG_USERSEND, 	/* User level sends data            31 */
233 	BBR_RSM_CLEARED,	/* RSM cleared of ACK flags         32 */
234 	BBR_LOG_STATE_TARGET, 	/* Log of target at state           33 */
235 	BBR_LOG_TIME_EPOCH, 	/* A timed based Epoch occurred     34 */
236 	BBR_LOG_TO_PROCESS,	/* A to was processed               35 */
237 	BBR_LOG_BBRTSO,		/* TSO update                       36 */
238 	BBR_LOG_HPTSDIAG,	/* Hpts diag insert                 37 */
239 	BBR_LOG_LOWGAIN,	/* Low gain accounting              38 */
240 	BBR_LOG_PROGRESS,	/* Progress timer event             39 */
241 	TCP_LOG_SOCKET_OPT,	/* A socket option is set           40 */
242 	BBR_LOG_TIMERPREP,	/* A BBR var to debug out TLP issues  41 */
243 	BBR_LOG_ENOBUF_JMP,	/* We had a enobuf jump             42 */
244 	BBR_LOG_HPTSI_CALC,	/* calc the hptsi time              43 */
245 	BBR_LOG_RTT_SHRINKS,	/* We had a log reduction of rttProp 44 */
246 	BBR_LOG_BW_RED_EV,	/* B/W reduction events             45 */
247 	BBR_LOG_REDUCE,		/* old bbr log reduce for 4.1 and earlier 46*/
248 	TCP_LOG_RTT,		/* A rtt (in useconds) is being sampled and applied to the srtt algo 47 */
249 	BBR_LOG_SETTINGS_CHG,	/* Settings changed for loss response 48 */
250 	BBR_LOG_SRTT_GAIN_EVENT, /* SRTT gaining -- now not used    49 */
251 	TCP_LOG_REASS,		/* Reassembly buffer logging        50 */
252 	TCP_HDWR_PACE_SIZE,	/*  TCP pacing size set (rl and rack uses this)  51 */
253 	BBR_LOG_HDWR_PACE,	/* TCP Hardware pacing log          52 */
254 	BBR_LOG_TSTMP_VAL,	/* Temp debug timestamp validation  53 */
255 	TCP_LOG_CONNEND,	/* End of connection                54 */
256 	TCP_LOG_LRO,		/* LRO entry                        55 */
257 	TCP_SACK_FILTER_RES,	/* Results of SACK Filter           56 */
258 	TCP_SAD_DETECT,		/* Sack Attack Detection            57 */
259 	TCP_TIMELY_WORK,	/* Logs regarding Timely CC tweaks  58 */
260 	TCP_LOG_USER_EVENT,	/* User space event data            59 */
261 	TCP_LOG_SENDFILE,	/* sendfile() logging for TCP connections 60 */
262 	TCP_LOG_HTTP_T,		/* logging of http request tracking 61 */
263 	TCP_LOG_ACCOUNTING,	/* Log of TCP Accounting data 62 */
264 	TCP_LOG_FSB,		/* FSB information 63 */
265 	RACK_DSACK_HANDLING,	/* Handling of DSACK in rack for reordering window 64 */
266 	TCP_HYSTART,		/* TCP Hystart logging 65 */
267 	TCP_CHG_QUERY,		/* Change query during fnc_init() 66 */
268 	TCP_RACK_LOG_COLLAPSE,	/* Window collapse by peer 67 */
269 	TCP_RACK_TP_TRIGGERED,	/* A rack tracepoint is triggered 68 */
270 	TCP_HYBRID_PACING_LOG,	/* Hybrid pacing log 69 */
271 	TCP_LOG_PRU,		/* TCP protocol user request        70 */
272 	TCP_LOG_END		/* End (keep at end)                71 */
273 };
274 
275 enum tcp_log_states {
276 	TCP_LOG_STATE_RATIO_OFF = -2,	/* Log ratio evaluation yielded an OFF
277 					   result. Only used for tlb_logstate */
278 	TCP_LOG_STATE_CLEAR = -1,	/* Deactivate and clear tracing. Passed
279 					   to tcp_log_state_change() but never
280 					   stored in any logstate variable */
281 	TCP_LOG_STATE_OFF = 0,		/* Pause */
282 
283 	/* Positively numbered states represent active logging modes */
284 	TCP_LOG_STATE_TAIL=1,		/* Keep the trailing events */
285 	TCP_LOG_STATE_HEAD=2,		/* Keep the leading events */
286 	TCP_LOG_STATE_HEAD_AUTO=3,	/* Keep the leading events, and
287 					   automatically dump them to the
288 					   device  */
289 	TCP_LOG_STATE_CONTINUAL=4,	/* Continually dump the data when full */
290 	TCP_LOG_STATE_TAIL_AUTO=5,	/* Keep the trailing events, and
291 					   automatically dump them when the
292 					   session ends */
293 	TCP_LOG_VIA_BBPOINTS=6		/* Log only if the BB point has been configured */
294 };
295 
296 /* Use this if we don't know whether the operation succeeded. */
297 #define	ERRNO_UNK	(-1)
298 
299 /*
300  * If the user included dev/tcp_log/tcp_log_dev.h, then include our private
301  * headers. Otherwise, there is no reason to pollute all the files with an
302  * additional include.
303  *
304  * This structure is aligned to an 8-byte boundary to match the alignment
305  * requirements of (struct tcp_log_buffer).
306  */
307 #ifdef __tcp_log_dev_h__
308 struct tcp_log_header {
309 	struct tcp_log_common_header tlh_common;
310 #define	tlh_version	tlh_common.tlch_version
311 #define	tlh_type	tlh_common.tlch_type
312 #define	tlh_length	tlh_common.tlch_length
313 	struct in_endpoints	tlh_ie;
314 	struct timeval		tlh_offset;	/* Uptime -> UTC offset */
315 	char			tlh_id[TCP_LOG_ID_LEN];
316 	char			tlh_reason[TCP_LOG_REASON_LEN];
317 	char			tlh_tag[TCP_LOG_TAG_LEN];
318 	uint8_t		tlh_af;
319 	uint8_t		_pad[7];
320 } ALIGN_TCP_LOG;
321 
322 #ifdef _KERNEL
323 struct tcp_log_dev_log_queue {
324 	struct tcp_log_dev_queue tldl_common;
325 	char			tldl_id[TCP_LOG_ID_LEN];
326 	char			tldl_reason[TCP_LOG_REASON_LEN];
327 	char			tldl_tag[TCP_LOG_TAG_LEN];
328 	struct in_endpoints	tldl_ie;
329 	struct tcp_log_stailq	tldl_entries;
330 	int			tldl_count;
331 	uint8_t			tldl_af;
332 };
333 #endif /* _KERNEL */
334 #endif /* __tcp_log_dev_h__ */
335 
336 /*
337  * Defined BBPOINTS that can be used
338  * with TCP_LOG_VIA_BBPOINTS.
339  */
340 #define TCP_BBPOINT_NONE		0
341 #define TCP_BBPOINT_REQ_LEVEL_LOGGING	1
342 
343 /*********************/
344 /* TCP Trace points */
345 /*********************/
346 /*
347  * TCP trace points are interesting points within
348  * the TCP code that the author/debugger may want
349  * to have BB logging enabled if we hit that point.
350  * In order to enable a trace point you set the
351  * sysctl var net.inet.tcp.bb.tp.number to
352  * one of the numbers listed below. You also
353  * must make sure net.inet.tcp.bb.tp.bbmode is
354  * non-zero, the default is 4 for continuous tracing.
355  * You also set in the number of connections you want
356  * have get BB logs in net.inet.tcp.bb.tp.count.
357  *
358  * Count will decrement every time BB logging is assigned
359  * to a connection that hit your tracepoint.
360  *
361  * You can enable all trace points by setting the number
362  * to 0xffffffff. You can disable all trace points by
363  * setting number to zero (or count to 0).
364  *
365  * Below are the enumerated list of tracepoints that
366  * have currently been defined in the code. Add more
367  * as you add a call to rack_trace_point(rack, <name>);
368  * where <name> is defined below.
369  */
370 #define TCP_TP_HWENOBUF		0x00000001	/* When we are doing hardware pacing and hit enobufs */
371 #define TCP_TP_ENOBUF		0x00000002	/* When we hit enobufs with software pacing */
372 #define TCP_TP_COLLAPSED_WND	0x00000003	/* When a peer to collapses its rwnd on us */
373 #define TCP_TP_COLLAPSED_RXT	0x00000004	/* When we actually retransmit a collapsed window rsm */
374 #define TCP_TP_HTTP_LOG_FAIL	0x00000005	/* We tried to allocate a HTTP log but had no space */
375 #define TCP_TP_RESET_RCV	0x00000006	/* Triggers when we receive a RST */
376 #define TCP_TP_EXCESS_RXT	0x00000007	/* When we get excess RXT's clamping the cwnd */
377 #define TCP_TP_SAD_TRIGGERED	0x00000008	/* Sack Attack Detection triggers */
378 
379 #define TCP_TP_SAD_SUSPECT	0x0000000a	/* A sack has supicious information in it */
380 
381 #ifdef _KERNEL
382 
383 extern uint32_t tcp_trace_point_config;
384 extern uint32_t tcp_trace_point_bb_mode;
385 extern int32_t tcp_trace_point_count;
386 
387 /*
388  * Returns true if any sort of BB logging is enabled,
389  * commonly used throughout the codebase.
390  */
391 static inline int
392 tcp_bblogging_on(struct tcpcb *tp)
393 {
394 	if (tp->_t_logstate <= TCP_LOG_STATE_OFF)
395 		return (0);
396 	if (tp->_t_logstate == TCP_LOG_VIA_BBPOINTS)
397 		return (0);
398 	return (1);
399 }
400 
401 /*
402  * Returns true if we match a specific bbpoint when
403  * in TCP_LOG_VIA_BBPOINTS, but also returns true
404  * for all the other logging states.
405  */
406 static inline int
407 tcp_bblogging_point_on(struct tcpcb *tp, uint8_t bbpoint)
408 {
409 	if (tp->_t_logstate <= TCP_LOG_STATE_OFF)
410 		return (0);
411 	if ((tp->_t_logstate == TCP_LOG_VIA_BBPOINTS) &&
412 	    (tp->_t_logpoint == bbpoint))
413 		return (1);
414 	else if (tp->_t_logstate == TCP_LOG_VIA_BBPOINTS)
415 		return (0);
416 	return (1);
417 }
418 
419 static inline void
420 tcp_set_bblog_state(struct tcpcb *tp, uint8_t ls, uint8_t bbpoint)
421 {
422 	if ((ls == TCP_LOG_VIA_BBPOINTS) &&
423 	    (tp->_t_logstate <= TCP_LOG_STATE_OFF)){
424 		/*
425 		 * We don't allow a BBPOINTS set to override
426 		 * other types of BB logging set by other means such
427 		 * as the bb_ratio/bb_state URL parameters. In other
428 		 * words BBlogging must be *off* in order to turn on
429 		 * a BBpoint.
430 		 */
431 		tp->_t_logpoint = bbpoint;
432 		tp->_t_logstate = ls;
433 	} else if (ls != TCP_LOG_VIA_BBPOINTS) {
434 		tp->_t_logpoint = 0;
435 		if ((ls >= TCP_LOG_STATE_OFF) &&
436 		    (ls < TCP_LOG_VIA_BBPOINTS))
437 			tp->_t_logstate = ls;
438 	}
439 }
440 
441 static inline uint32_t
442 tcp_get_bblog_state(struct tcpcb *tp)
443 {
444 	return (tp->_t_logstate);
445 }
446 
447 static inline void
448 tcp_trace_point(struct tcpcb *tp, int num)
449 {
450 #ifdef TCP_BLACKBOX
451 	if (((tcp_trace_point_config == num)  ||
452 	     (tcp_trace_point_config == 0xffffffff)) &&
453 	    (tcp_trace_point_bb_mode != 0) &&
454 	    (tcp_trace_point_count > 0) &&
455 	    (tcp_bblogging_on(tp) == 0)) {
456 		int res;
457 		res = atomic_fetchadd_int(&tcp_trace_point_count, -1);
458 		if (res > 0) {
459 			tcp_set_bblog_state(tp, tcp_trace_point_bb_mode, TCP_BBPOINT_NONE);
460 		} else {
461 			/* Loss a race assure its zero now */
462 			tcp_trace_point_count = 0;
463 		}
464 	}
465 #endif
466 }
467 
468 #define	TCP_LOG_BUF_DEFAULT_SESSION_LIMIT	5000
469 #define	TCP_LOG_BUF_DEFAULT_GLOBAL_LIMIT	5000000
470 
471 /*
472  * TCP_LOG_EVENT_VERBOSE: The same as TCP_LOG_EVENT, except it always
473  * tries to record verbose information.
474  */
475 #define	TCP_LOG_EVENT_VERBOSE(tp, th, rxbuf, txbuf, eventid, errornum, len, stackinfo, th_hostorder, tv) \
476 	do {								\
477 		if (tcp_bblogging_on(tp)) \
478 			tcp_log_event(tp, th, rxbuf, txbuf, eventid,	\
479 			    errornum, len, stackinfo, th_hostorder,	\
480 			    tp->t_output_caller, __func__, __LINE__, tv);\
481 	} while (0)
482 
483 /*
484  * TCP_LOG_EVENT: This is a macro so we can capture function/line
485  * information when needed. You can use the macro when you are not
486  * doing a lot of prep in the stack specific information i.e. you
487  * don't add extras (stackinfo). If you are adding extras which
488  * means filling out a stack variable instead use the tcp_log_event()
489  * function but enclose the call to the log (and all the setup) in a
490  * if (tcp_bblogging_on(tp)) {
491  *   ... setup and logging call ...
492  * }
493  *
494  * Always use the macro tcp_bblogging_on() since sometimes the defintions
495  * do change.
496  *
497  * BBlogging also supports the concept of a BBpoint. The idea behind this
498  * is that when you set a specific BBpoint on and turn the logging into
499  * the BBpoint mode (TCP_LOG_VIA_BBPOINTS) you will be defining very very
500  * few of these points to come out. The point is specific to a code you
501  * want tied to that one BB logging. This allows you to turn on a much broader
502  * scale set of limited logging on more connections without overwhelming the
503  * I/O system with too much BBlogs. This of course means you need to be quite
504  * careful on how many BBlogs go with each point, but you can have multiple points
505  * only one of which is active at a time.
506  *
507  * To define a point you add it above under the define for TCP_BBPOINT_NONE (which
508  * is the default i.e. no point is defined. You then, for your point use the
509  * tcp_bblogging_point_on(struct tcpcb *tp, uint8_t bbpoint) inline to enclose
510  * your call to tcp_log_event.  Do not use one of the TCP_LOGGING macros else
511  * your point will never come out. You specify your defined point in the bbpoint
512  * side of the inline. An example of this you can find in rack where the
513  * TCP_BBPOINT_REQ_LEVEL_LOGGING is used. There a specific set of logs are generated
514  * for each http request that rack is tracking.
515  *
516  * When turning on BB logging use the inline:
517  * tcp_set_bblog_state(struct tcpcb *tp, uint8_t ls, uint8_t bbpoint)
518  * the ls field is the logging state TCP_LOG_STATE_CONTINUAL etc. The
519  * bbpoint field is ignored unless the ls field is set to TCP_LOG_VIA_BBPOINTS.
520  * Currently there is only a socket option that turns on the non-BBPOINT
521  * logging.
522  *
523  * Prototype:
524  * TCP_LOG_EVENT(struct tcpcb *tp, struct tcphdr *th, struct sockbuf *rxbuf,
525  *     struct sockbuf *txbuf, uint8_t eventid, int errornum,
526  *     union tcp_log_stackspecific *stackinfo)
527  *
528  * tp is mandatory and must be write locked.
529  * th is optional; if present, it will appear in the record.
530  * rxbuf and txbuf are optional; if present, they will appear in the record.
531  * eventid is mandatory.
532  * errornum is mandatory (it indicates the success or failure of the
533  *     operation associated with the event).
534  * len indicates the length of the packet. If no packet, use 0.
535  * stackinfo is optional; if present, it will appear in the record.
536  */
537 struct tcpcb;
538 #ifdef TCP_LOG_FORCEVERBOSE
539 #define	TCP_LOG_EVENT	TCP_LOG_EVENT_VERBOSE
540 #else
541 #define	TCP_LOG_EVENT(tp, th, rxbuf, txbuf, eventid, errornum, len, stackinfo, th_hostorder) \
542 	do {								\
543 		if (tcp_log_verbose)					\
544 			TCP_LOG_EVENT_VERBOSE(tp, th, rxbuf, txbuf,	\
545 			    eventid, errornum, len, stackinfo,		\
546 			    th_hostorder, NULL);			\
547 		else if (tcp_bblogging_on(tp))				\
548 			tcp_log_event(tp, th, rxbuf, txbuf, eventid,	\
549 			    errornum, len, stackinfo, th_hostorder,	\
550 			    NULL, NULL, 0, NULL);			\
551 	} while (0)
552 #endif /* TCP_LOG_FORCEVERBOSE */
553 #define	TCP_LOG_EVENTP(tp, th, rxbuf, txbuf, eventid, errornum, len, stackinfo, th_hostorder, tv) \
554 	do {								\
555 		if (tcp_bblogging_on(tp))				\
556 			tcp_log_event(tp, th, rxbuf, txbuf, eventid,	\
557 			    errornum, len, stackinfo, th_hostorder,	\
558 			    NULL, NULL, 0, tv);				\
559 	} while (0)
560 
561 #ifdef TCP_BLACKBOX
562 extern bool tcp_log_verbose;
563 void tcp_log_drain(struct tcpcb *tp);
564 int tcp_log_dump_tp_logbuf(struct tcpcb *tp, char *reason, int how, bool force);
565 void tcp_log_dump_tp_bucket_logbufs(struct tcpcb *tp, char *reason);
566 struct tcp_log_buffer *tcp_log_event(struct tcpcb *tp, struct tcphdr *th, struct sockbuf *rxbuf,
567     struct sockbuf *txbuf, uint8_t eventid, int errornum, uint32_t len,
568     union tcp_log_stackspecific *stackinfo, int th_hostorder,
569     const char *output_caller, const char *func, int line, const struct timeval *tv);
570 size_t tcp_log_get_id(struct tcpcb *tp, char *buf);
571 size_t tcp_log_get_tag(struct tcpcb *tp, char *buf);
572 u_int tcp_log_get_id_cnt(struct tcpcb *tp);
573 int tcp_log_getlogbuf(struct sockopt *sopt, struct tcpcb *tp);
574 void tcp_log_init(void);
575 int tcp_log_set_id(struct tcpcb *tp, char *id);
576 int tcp_log_set_tag(struct tcpcb *tp, char *tag);
577 int tcp_log_state_change(struct tcpcb *tp, int state);
578 void tcp_log_tcpcbinit(struct tcpcb *tp);
579 void tcp_log_tcpcbfini(struct tcpcb *tp);
580 void tcp_log_flowend(struct tcpcb *tp);
581 void tcp_log_sendfile(struct socket *so, off_t offset, size_t nbytes,
582     int flags);
583 int tcp_log_apply_ratio(struct tcpcb *tp, int ratio);
584 #else /* !TCP_BLACKBOX */
585 #define tcp_log_verbose	(false)
586 
587 static inline struct tcp_log_buffer *
588 tcp_log_event(struct tcpcb *tp, struct tcphdr *th, struct sockbuf *rxbuf,
589     struct sockbuf *txbuf, uint8_t eventid, int errornum, uint32_t len,
590     union tcp_log_stackspecific *stackinfo, int th_hostorder,
591     const char *output_caller, const char *func, int line,
592     const struct timeval *tv)
593 {
594 
595 	return (NULL);
596 }
597 #endif /* TCP_BLACKBOX */
598 
599 #endif	/* _KERNEL */
600 #endif	/* __tcp_log_buf_h__ */
601