1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2016-2018 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
28 #ifndef __tcp_log_buf_h__
29 #define __tcp_log_buf_h__
30
31 #define TCP_LOG_REASON_LEN 32
32 #define TCP_LOG_TAG_LEN 32
33 #define TCP_LOG_BUF_VER (9)
34
35 /*
36 * Because the (struct tcp_log_buffer) includes 8-byte uint64_t's, it requires
37 * 8-byte alignment to work properly on all platforms. Therefore, we will
38 * enforce 8-byte alignment for all the structures that may appear by
39 * themselves (instead of being embedded in another structure) in a data
40 * stream.
41 */
42 #define ALIGN_TCP_LOG __aligned(8)
43
44 /* Information about the socketbuffer state. */
45 struct tcp_log_sockbuf
46 {
47 uint32_t tls_sb_acc; /* available chars (sb->sb_acc) */
48 uint32_t tls_sb_ccc; /* claimed chars (sb->sb_ccc) */
49 uint32_t tls_sb_spare; /* spare */
50 };
51
52 /* Optional, verbose information that may be appended to an event log. */
53 struct tcp_log_verbose
54 {
55 #define TCP_FUNC_LEN 32
56 char tlv_snd_frm[TCP_FUNC_LEN]; /* tcp_output() caller */
57 char tlv_trace_func[TCP_FUNC_LEN]; /* Function that
58 generated trace */
59 uint32_t tlv_trace_line; /* Line number that generated trace */
60 uint8_t _pad[4];
61 } ALIGN_TCP_LOG;
62
63 /* Internal RACK state variables. */
64 struct tcp_log_rack
65 {
66 uint32_t tlr_rack_rtt; /* rc_rack_rtt */
67 uint8_t tlr_state; /* Internal RACK state */
68 uint8_t _pad[3]; /* Padding */
69 };
70
71 struct tcp_log_bbr {
72 uint64_t cur_del_rate;
73 uint64_t delRate;
74 uint64_t rttProp;
75 uint64_t bw_inuse;
76 uint32_t inflight;
77 uint32_t applimited;
78 uint32_t delivered;
79 uint32_t timeStamp;
80 uint32_t epoch;
81 uint32_t lt_epoch;
82 uint32_t pkts_out;
83 uint32_t flex1;
84 uint32_t flex2;
85 uint32_t flex3;
86 uint32_t flex4;
87 uint32_t flex5;
88 uint32_t flex6;
89 uint32_t lost;
90 uint16_t pacing_gain;
91 uint16_t cwnd_gain;
92 uint16_t flex7;
93 uint8_t bbr_state;
94 uint8_t bbr_substate;
95 uint8_t inhpts;
96 uint8_t ininput;
97 uint8_t use_lt_bw;
98 uint8_t flex8;
99 uint32_t pkt_epoch;
100 };
101
102 /* Per-stack stack-specific info. */
103 union tcp_log_stackspecific
104 {
105 struct tcp_log_rack u_rack;
106 struct tcp_log_bbr u_bbr;
107 };
108
109 struct tcp_log_buffer
110 {
111 /* Event basics */
112 struct timeval tlb_tv; /* Timestamp of trace */
113 uint32_t tlb_ticks; /* Timestamp of trace */
114 uint32_t tlb_sn; /* Serial number */
115 uint8_t tlb_stackid; /* Stack ID */
116 uint8_t tlb_eventid; /* Event ID */
117 uint16_t tlb_eventflags; /* Flags for the record */
118 #define TLB_FLAG_RXBUF 0x0001 /* Includes receive buffer info */
119 #define TLB_FLAG_TXBUF 0x0002 /* Includes send buffer info */
120 #define TLB_FLAG_HDR 0x0004 /* Includes a TCP header */
121 #define TLB_FLAG_VERBOSE 0x0008 /* Includes function/line numbers */
122 #define TLB_FLAG_STACKINFO 0x0010 /* Includes stack-specific info */
123 int tlb_errno; /* Event error (if any) */
124
125 /* Internal session state */
126 struct tcp_log_sockbuf tlb_rxbuf; /* Receive buffer */
127 struct tcp_log_sockbuf tlb_txbuf; /* Send buffer */
128
129 int tlb_state; /* TCPCB t_state */
130 uint32_t tlb_starttime; /* TCPCB t_starttime */
131 uint32_t tlb_iss; /* TCPCB iss */
132 uint32_t tlb_flags; /* TCPCB flags */
133 uint32_t tlb_snd_una; /* TCPCB snd_una */
134 uint32_t tlb_snd_max; /* TCPCB snd_max */
135 uint32_t tlb_snd_cwnd; /* TCPCB snd_cwnd */
136 uint32_t tlb_snd_nxt; /* TCPCB snd_nxt */
137 uint32_t tlb_snd_recover;/* TCPCB snd_recover */
138 uint32_t tlb_snd_wnd; /* TCPCB snd_wnd */
139 uint32_t tlb_snd_ssthresh; /* TCPCB snd_ssthresh */
140 uint32_t tlb_srtt; /* TCPCB t_srtt */
141 uint32_t tlb_rttvar; /* TCPCB t_rttvar */
142 uint32_t tlb_rcv_up; /* TCPCB rcv_up */
143 uint32_t tlb_rcv_adv; /* TCPCB rcv_adv */
144 uint32_t tlb_flags2; /* TCPCB t_flags2 */
145 uint32_t tlb_rcv_nxt; /* TCPCB rcv_nxt */
146 uint32_t tlb_rcv_wnd; /* TCPCB rcv_wnd */
147 uint32_t tlb_dupacks; /* TCPCB t_dupacks */
148 int tlb_segqlen; /* TCPCB segqlen */
149 int tlb_snd_numholes; /* TCPCB snd_numholes */
150 uint32_t tlb_flex1; /* Event specific information */
151 uint32_t tlb_flex2; /* Event specific information */
152 uint32_t tlb_fbyte_in; /* TCPCB first byte in time */
153 uint32_t tlb_fbyte_out; /* TCPCB first byte out time */
154 uint8_t tlb_snd_scale:4, /* TCPCB snd_scale */
155 tlb_rcv_scale:4; /* TCPCB rcv_scale */
156 uint8_t _pad[3]; /* Padding */
157
158 /* Per-stack info */
159 union tcp_log_stackspecific tlb_stackinfo;
160 #define tlb_rack tlb_stackinfo.u_rack
161
162 /* The packet */
163 uint32_t tlb_len; /* The packet's data length */
164 struct tcphdr tlb_th; /* The TCP header */
165 uint8_t tlb_opts[TCP_MAXOLEN]; /* The TCP options */
166
167 /* Verbose information (optional) */
168 struct tcp_log_verbose tlb_verbose[0];
169 } ALIGN_TCP_LOG;
170
171 enum tcp_log_events {
172 TCP_LOG_IN = 1, /* Incoming packet 1 */
173 TCP_LOG_OUT, /* Transmit (without other event) 2 */
174 TCP_LOG_RTO, /* Retransmit timeout 3 */
175 TCP_LOG_SB_WAKE, /* Awaken socket buffer 4 */
176 TCP_LOG_BAD_RETRAN, /* Detected bad retransmission 5 */
177 TCP_LOG_PRR, /* Doing PRR 6 */
178 TCP_LOG_REORDER, /* Detected reorder 7 */
179 TCP_LOG_HPTS, /* Hpts sending a packet 8 */
180 BBR_LOG_BBRUPD, /* We updated BBR info 9 */
181 BBR_LOG_BBRSND, /* We did a slot calculation and sending is done 10 */
182 BBR_LOG_ACKCLEAR, /* A ack clears all outstanding 11 */
183 BBR_LOG_INQUEUE, /* The tcb had a packet input to it 12 */
184 BBR_LOG_TIMERSTAR, /* Start a timer 13 */
185 BBR_LOG_TIMERCANC, /* Cancel a timer 14 */
186 BBR_LOG_ENTREC, /* Entered recovery 15 */
187 BBR_LOG_EXITREC, /* Exited recovery 16 */
188 BBR_LOG_CWND, /* Cwnd change 17 */
189 BBR_LOG_BWSAMP, /* LT B/W sample has been made 18 */
190 BBR_LOG_MSGSIZE, /* We received a EMSGSIZE error 19 */
191 BBR_LOG_BBRRTT, /* BBR RTT is updated 20 */
192 BBR_LOG_JUSTRET, /* We just returned out of output 21 */
193 BBR_LOG_STATE, /* A BBR state change occured 22 */
194 BBR_LOG_PKT_EPOCH, /* A BBR packet epoch occured 23 */
195 BBR_LOG_PERSIST, /* BBR changed to/from a persists 24 */
196 TCP_LOG_FLOWEND, /* End of a flow 25 */
197 BBR_LOG_RTO, /* BBR's timeout includes BBR info 26 */
198 BBR_LOG_DOSEG_DONE, /* hpts do_segment completes 27 */
199 BBR_LOG_EXIT_GAIN, /* hpts do_segment completes 28 */
200 BBR_LOG_THRESH_CALC, /* Doing threshold calculation 29 */
201 TCP_LOG_MAPCHG, /* Map Changes to the sendmap 30 */
202 TCP_LOG_USERSEND, /* User level sends data 31 */
203 BBR_RSM_CLEARED, /* RSM cleared of ACK flags 32 */
204 BBR_LOG_STATE_TARGET, /* Log of target at state 33 */
205 BBR_LOG_TIME_EPOCH, /* A timed based Epoch occured 34 */
206 BBR_LOG_TO_PROCESS, /* A to was processed 35 */
207 BBR_LOG_BBRTSO, /* TSO update 36 */
208 BBR_LOG_HPTSDIAG, /* Hpts diag insert 37 */
209 BBR_LOG_LOWGAIN, /* Low gain accounting 38 */
210 BBR_LOG_PROGRESS, /* Progress timer event 39 */
211 TCP_LOG_SOCKET_OPT, /* A socket option is set 40 */
212 BBR_LOG_TIMERPREP, /* A BBR var to debug out TLP issues 41 */
213 BBR_LOG_ENOBUF_JMP, /* We had a enobuf jump 42 */
214 BBR_LOG_HPTSI_CALC, /* calc the hptsi time 43 */
215 BBR_LOG_RTT_SHRINKS, /* We had a log reduction of rttProp 44 */
216 BBR_LOG_BW_RED_EV, /* B/W reduction events 45 */
217 BBR_LOG_REDUCE, /* old bbr log reduce for 4.1 and earlier 46*/
218 TCP_LOG_RTT, /* A rtt (in useconds) is being sampled and applied to the srtt algo 47 */
219 BBR_LOG_SETTINGS_CHG, /* Settings changed for loss response 48 */
220 BBR_LOG_SRTT_GAIN_EVENT, /* SRTT gaining -- now not used 49 */
221 TCP_LOG_REASS, /* Reassembly buffer logging 50 */
222 TCP_HDWR_PACE_SIZE, /* TCP pacing size set (rl and rack uses this) 51 */
223 BBR_LOG_HDWR_PACE, /* TCP Hardware pacing log 52 */
224 BBR_LOG_TSTMP_VAL, /* Temp debug timestamp validation 53 */
225 TCP_LOG_CONNEND, /* End of connection 54 */
226 TCP_LOG_LRO, /* LRO entry 55 */
227 TCP_SACK_FILTER_RES, /* Results of SACK Filter 56 */
228 TCP_SAD_DETECTION, /* Sack Attack Detection 57 */
229 TCP_TIMELY_WORK, /* Logs regarding Timely CC tweaks 58 */
230 TCP_LOG_USER_EVENT, /* User space event data 59 */
231 TCP_LOG_SENDFILE, /* sendfile() logging for TCP connections 60 */
232 TCP_LOG_HTTP_T, /* logging of http request tracking 61 */
233 TCP_LOG_ACCOUNTING, /* Log of TCP Accounting data 62 */
234 TCP_LOG_FSB, /* FSB information 63 */
235 TCP_LOG_END /* End (keep at end) 64 */
236 };
237
238 enum tcp_log_states {
239 TCP_LOG_STATE_CLEAR = -1, /* Deactivate and clear tracing */
240 TCP_LOG_STATE_OFF = 0, /* Pause */
241 TCP_LOG_STATE_TAIL=1, /* Keep the trailing events */
242 TCP_LOG_STATE_HEAD=2, /* Keep the leading events */
243 TCP_LOG_STATE_HEAD_AUTO=3, /* Keep the leading events, and
244 automatically dump them to the
245 device */
246 TCP_LOG_STATE_CONTINUAL=4, /* Continually dump the data when full */
247 TCP_LOG_STATE_TAIL_AUTO=5, /* Keep the trailing events, and
248 automatically dump them when the
249 session ends */
250 };
251
252 /* Use this if we don't know whether the operation succeeded. */
253 #define ERRNO_UNK (-1)
254
255 /*
256 * If the user included dev/tcp_log/tcp_log_dev.h, then include our private
257 * headers. Otherwise, there is no reason to pollute all the files with an
258 * additional include.
259 *
260 * This structure is aligned to an 8-byte boundary to match the alignment
261 * requirements of (struct tcp_log_buffer).
262 */
263 #ifdef __tcp_log_dev_h__
264 struct tcp_log_header {
265 struct tcp_log_common_header tlh_common;
266 #define tlh_version tlh_common.tlch_version
267 #define tlh_type tlh_common.tlch_type
268 #define tlh_length tlh_common.tlch_length
269 struct in_endpoints tlh_ie;
270 struct timeval tlh_offset; /* Uptime -> UTC offset */
271 char tlh_id[TCP_LOG_ID_LEN];
272 char tlh_reason[TCP_LOG_REASON_LEN];
273 char tlh_tag[TCP_LOG_TAG_LEN];
274 uint8_t tlh_af;
275 uint8_t _pad[7];
276 } ALIGN_TCP_LOG;
277
278 #ifdef _KERNEL
279 struct tcp_log_dev_log_queue {
280 struct tcp_log_dev_queue tldl_common;
281 char tldl_id[TCP_LOG_ID_LEN];
282 char tldl_reason[TCP_LOG_REASON_LEN];
283 char tldl_tag[TCP_LOG_TAG_LEN];
284 struct in_endpoints tldl_ie;
285 struct tcp_log_stailq tldl_entries;
286 int tldl_count;
287 uint8_t tldl_af;
288 };
289 #endif /* _KERNEL */
290 #endif /* __tcp_log_dev_h__ */
291
292 #ifdef _KERNEL
293
294 #define TCP_LOG_BUF_DEFAULT_SESSION_LIMIT 5000
295 #define TCP_LOG_BUF_DEFAULT_GLOBAL_LIMIT 5000000
296
297 /*
298 * TCP_LOG_EVENT_VERBOSE: The same as TCP_LOG_EVENT, except it always
299 * tries to record verbose information.
300 */
301 #define TCP_LOG_EVENT_VERBOSE(tp, th, rxbuf, txbuf, eventid, errornum, len, stackinfo, th_hostorder, tv) \
302 do { \
303 if (tp->t_logstate != TCP_LOG_STATE_OFF) \
304 tcp_log_event_(tp, th, rxbuf, txbuf, eventid, \
305 errornum, len, stackinfo, th_hostorder, \
306 tp->t_output_caller, __func__, __LINE__, tv);\
307 } while (0)
308
309 /*
310 * TCP_LOG_EVENT: This is a macro so we can capture function/line
311 * information when needed.
312 *
313 * Prototype:
314 * TCP_LOG_EVENT(struct tcpcb *tp, struct tcphdr *th, struct sockbuf *rxbuf,
315 * struct sockbuf *txbuf, uint8_t eventid, int errornum,
316 * union tcp_log_stackspecific *stackinfo)
317 *
318 * tp is mandatory and must be write locked.
319 * th is optional; if present, it will appear in the record.
320 * rxbuf and txbuf are optional; if present, they will appear in the record.
321 * eventid is mandatory.
322 * errornum is mandatory (it indicates the success or failure of the
323 * operation associated with the event).
324 * len indicates the length of the packet. If no packet, use 0.
325 * stackinfo is optional; if present, it will appear in the record.
326 */
327 #ifdef TCP_LOG_FORCEVERBOSE
328 #define TCP_LOG_EVENT TCP_LOG_EVENT_VERBOSE
329 #else
330 #define TCP_LOG_EVENT(tp, th, rxbuf, txbuf, eventid, errornum, len, stackinfo, th_hostorder) \
331 do { \
332 if (tcp_log_verbose) \
333 TCP_LOG_EVENT_VERBOSE(tp, th, rxbuf, txbuf, \
334 eventid, errornum, len, stackinfo, \
335 th_hostorder, NULL); \
336 else if (tp->t_logstate != TCP_LOG_STATE_OFF) \
337 tcp_log_event_(tp, th, rxbuf, txbuf, eventid, \
338 errornum, len, stackinfo, th_hostorder, \
339 NULL, NULL, 0, NULL); \
340 } while (0)
341 #endif /* TCP_LOG_FORCEVERBOSE */
342 #define TCP_LOG_EVENTP(tp, th, rxbuf, txbuf, eventid, errornum, len, stackinfo, th_hostorder, tv) \
343 do { \
344 if (tp->t_logstate != TCP_LOG_STATE_OFF) \
345 tcp_log_event_(tp, th, rxbuf, txbuf, eventid, \
346 errornum, len, stackinfo, th_hostorder, \
347 NULL, NULL, 0, tv); \
348 } while (0)
349
350 #ifdef TCP_BLACKBOX
351 extern bool tcp_log_verbose;
352 void tcp_log_drain(struct tcpcb *tp);
353 int tcp_log_dump_tp_logbuf(struct tcpcb *tp, char *reason, int how, bool force);
354 void tcp_log_dump_tp_bucket_logbufs(struct tcpcb *tp, char *reason);
355 struct tcp_log_buffer *tcp_log_event_(struct tcpcb *tp, struct tcphdr *th, struct sockbuf *rxbuf,
356 struct sockbuf *txbuf, uint8_t eventid, int errornum, uint32_t len,
357 union tcp_log_stackspecific *stackinfo, int th_hostorder,
358 const char *output_caller, const char *func, int line, const struct timeval *tv);
359 size_t tcp_log_get_id(struct tcpcb *tp, char *buf);
360 size_t tcp_log_get_tag(struct tcpcb *tp, char *buf);
361 u_int tcp_log_get_id_cnt(struct tcpcb *tp);
362 int tcp_log_getlogbuf(struct sockopt *sopt, struct tcpcb *tp);
363 void tcp_log_init(void);
364 int tcp_log_set_id(struct tcpcb *tp, char *id);
365 int tcp_log_set_tag(struct tcpcb *tp, char *tag);
366 int tcp_log_state_change(struct tcpcb *tp, int state);
367 void tcp_log_tcpcbinit(struct tcpcb *tp);
368 void tcp_log_tcpcbfini(struct tcpcb *tp);
369 void tcp_log_flowend(struct tcpcb *tp);
370 #else /* !TCP_BLACKBOX */
371 #define tcp_log_verbose (false)
372
373 static inline struct tcp_log_buffer *
tcp_log_event_(struct tcpcb * tp,struct tcphdr * th,struct sockbuf * rxbuf,struct sockbuf * txbuf,uint8_t eventid,int errornum,uint32_t len,union tcp_log_stackspecific * stackinfo,int th_hostorder,const char * output_caller,const char * func,int line,const struct timeval * tv)374 tcp_log_event_(struct tcpcb *tp, struct tcphdr *th, struct sockbuf *rxbuf,
375 struct sockbuf *txbuf, uint8_t eventid, int errornum, uint32_t len,
376 union tcp_log_stackspecific *stackinfo, int th_hostorder,
377 const char *output_caller, const char *func, int line,
378 const struct timeval *tv)
379 {
380
381 return (NULL);
382 }
383 #endif /* TCP_BLACKBOX */
384
385 #endif /* _KERNEL */
386 #endif /* __tcp_log_buf_h__ */
387