xref: /freebsd/sys/netinet/tcp_var.h (revision 46023d54c7c2c00d273b7499421315f216ff4989)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1993, 1994, 1995
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef _NETINET_TCP_VAR_H_
33 #define _NETINET_TCP_VAR_H_
34 
35 #include <netinet/tcp.h>
36 #include <netinet/tcp_fsm.h>
37 
38 #ifdef _KERNEL
39 #include <net/vnet.h>
40 #include <sys/mbuf.h>
41 #include <sys/ktls.h>
42 #endif
43 
44 #define TCP_END_BYTE_INFO 8	/* Bytes that makeup the "end information array" */
45 /* Types of ending byte info */
46 #define TCP_EI_EMPTY_SLOT	0
47 #define TCP_EI_STATUS_CLIENT_FIN	0x1
48 #define TCP_EI_STATUS_CLIENT_RST	0x2
49 #define TCP_EI_STATUS_SERVER_FIN	0x3
50 #define TCP_EI_STATUS_SERVER_RST	0x4
51 #define TCP_EI_STATUS_RETRAN		0x5
52 #define TCP_EI_STATUS_PROGRESS		0x6
53 #define TCP_EI_STATUS_PERSIST_MAX	0x7
54 #define TCP_EI_STATUS_KEEP_MAX		0x8
55 #define TCP_EI_STATUS_DATA_A_CLOSE	0x9
56 #define TCP_EI_STATUS_RST_IN_FRONT	0xa
57 #define TCP_EI_STATUS_2MSL		0xb
58 #define TCP_EI_STATUS_MAX_VALUE		0xb
59 
60 #define TCP_TRK_REQ_LOG_NEW		0x01
61 #define TCP_TRK_REQ_LOG_COMPLETE	0x02
62 #define TCP_TRK_REQ_LOG_FREED		0x03
63 #define TCP_TRK_REQ_LOG_ALLOCFAIL	0x04
64 #define TCP_TRK_REQ_LOG_MOREYET	0x05
65 #define TCP_TRK_REQ_LOG_FORCEFREE	0x06
66 #define TCP_TRK_REQ_LOG_STALE		0x07
67 #define TCP_TRK_REQ_LOG_SEARCH		0x08
68 
69 /************************************************/
70 /* Status bits we track to assure no duplicates,
71  * the bits here are not used by the code but
72  * for human representation. To check a bit we
73  * take and shift over by 1 minus the value (1-8).
74  */
75 /************************************************/
76 #define TCP_EI_BITS_CLIENT_FIN	0x001
77 #define TCP_EI_BITS_CLIENT_RST	0x002
78 #define TCP_EI_BITS_SERVER_FIN	0x004
79 #define TCP_EI_BITS_SERVER_RST	0x008
80 #define TCP_EI_BITS_RETRAN	0x010
81 #define TCP_EI_BITS_PROGRESS	0x020
82 #define TCP_EI_BITS_PRESIST_MAX	0x040
83 #define TCP_EI_BITS_KEEP_MAX	0x080
84 #define TCP_EI_BITS_DATA_A_CLO  0x100
85 #define TCP_EI_BITS_RST_IN_FR	0x200	/* a front state reset */
86 #define TCP_EI_BITS_2MS_TIMER	0x400	/* 2 MSL timer expired */
87 
88 #if defined(_KERNEL) || defined(_WANT_TCPCB)
89 #include <sys/_callout.h>
90 #include <sys/osd.h>
91 
92 #include <netinet/cc/cc.h>
93 
94 /* TCP segment queue entry */
95 struct tseg_qent {
96 	TAILQ_ENTRY(tseg_qent) tqe_q;
97 	struct	mbuf   *tqe_m;		/* mbuf contains packet */
98 	struct  mbuf   *tqe_last;	/* last mbuf in chain */
99 	tcp_seq tqe_start;		/* TCP Sequence number start */
100 	int	tqe_len;		/* TCP segment data length */
101 	uint32_t tqe_flags;		/* The flags from tcp_get_flags() */
102 	uint32_t tqe_mbuf_cnt;		/* Count of mbuf overhead */
103 };
104 TAILQ_HEAD(tsegqe_head, tseg_qent);
105 
106 struct sackblk {
107 	tcp_seq start;		/* start seq no. of sack block */
108 	tcp_seq end;		/* end seq no. */
109 };
110 
111 struct sackhole {
112 	tcp_seq start;		/* start seq no. of hole */
113 	tcp_seq end;		/* end seq no. */
114 	tcp_seq rxmit;		/* next seq. no in hole to be retransmitted */
115 	TAILQ_ENTRY(sackhole) scblink;	/* scoreboard linkage */
116 };
117 
118 struct sackhint {
119 	struct sackhole	*nexthole;
120 	int32_t		sack_bytes_rexmit;
121 	tcp_seq		last_sack_ack;	/* Most recent/largest sacked ack */
122 
123 	int32_t		delivered_data; /* Newly acked data from last SACK */
124 
125 	int32_t		sacked_bytes;	/* Total sacked bytes reported by the
126 					 * receiver via sack option
127 					 */
128 	uint32_t	recover_fs;	/* Flight Size at the start of Loss recovery */
129 	uint32_t	prr_delivered;	/* Total bytes delivered using PRR */
130 	uint32_t	prr_out;	/* Bytes sent during IN_RECOVERY */
131 	int32_t		hole_bytes;	/* current number of bytes in scoreboard holes */
132 	int32_t		lost_bytes;	/* number of rfc6675 IsLost() bytes */
133 };
134 
135 #define SEGQ_EMPTY(tp) TAILQ_EMPTY(&(tp)->t_segq)
136 
137 STAILQ_HEAD(tcp_log_stailq, tcp_log_mem);
138 
139 #define TCP_TRK_TRACK_FLG_EMPTY 0x00	/* Available */
140 #define TCP_TRK_TRACK_FLG_USED  0x01	/* In use */
141 #define TCP_TRK_TRACK_FLG_OPEN  0x02	/* End is not valid (open range request) */
142 #define TCP_TRK_TRACK_FLG_SEQV  0x04	/* We had a sendfile that touched it  */
143 #define TCP_TRK_TRACK_FLG_COMP  0x08	/* Sendfile as placed the last bits (range req only) */
144 #define TCP_TRK_TRACK_FLG_FSND	0x10	/* First send has been done into the seq space */
145 #define TCP_TRK_TRACK_FLG_LSND	0x20	/* We were able to set the Last Sent */
146 #define MAX_TCP_TRK_REQ 5		/* Max we will have at once */
147 
148 struct tcp_sendfile_track {
149 	uint64_t timestamp;	/* User sent timestamp */
150 	uint64_t start;		/* Start of sendfile offset */
151 	uint64_t end;		/* End if not open-range req */
152 	uint64_t localtime;	/* Time we actually got the req */
153 	uint64_t deadline;	/* If in CU mode, deadline to delivery */
154 	uint64_t first_send;	/* Time of first send in the range */
155 	uint64_t cspr;		/* Client suggested pace rate */
156 	uint64_t sent_at_fs;	/* What was t_sndbytes as we begun sending */
157 	uint64_t rxt_at_fs;	/* What was t_snd_rxt_bytes as we begun sending */
158 	uint64_t sent_at_ls;	/* Sent value at the last send */
159 	uint64_t rxt_at_ls;	/* Retransmit value at the last send */
160 	tcp_seq start_seq;	/* First TCP Seq assigned */
161 	tcp_seq end_seq;	/* If range req last seq */
162 	uint32_t flags;		/* Type of request open etc */
163 	uint32_t sbcc_at_s;	/* When we allocate what is the sb_cc */
164 	uint32_t hint_maxseg;	/* Client hinted maxseg */
165 	uint32_t playout_ms;	/* Client playout ms */
166 	uint32_t hybrid_flags;	/* Hybrid flags on this request */
167 };
168 
169 
170 /*
171  * Change Query responses for a stack switch we create a structure
172  * that allows query response from the new stack to the old, if
173  * supported.
174  *
175  * There are three queries currently defined.
176  *  - sendmap
177  *  - timers
178  *  - rack_times
179  *
180  * For the sendmap query the caller fills in the
181  * req and the req_param as the first seq (usually
182  * snd_una). When the response comes back indicating
183  * that there was data (return value 1), then the caller
184  * can build a sendmap entry based on the range and the
185  * times. The next query would then be done at the
186  * newly created sendmap_end. Repeated until sendmap_end == snd_max.
187  *
188  * Flags in sendmap_flags are defined below as well.
189  *
190  * For timers the standard PACE_TMR_XXXX flags are returned indicating
191  * a pacing timer (possibly) and one other timer. If pacing timer then
192  * the expiration timeout time in microseconds is in timer_pacing_to.
193  * And the value used with whatever timer (if a flag is set) is in
194  * timer_rxt. If no timers are running a 0 is returned and of
195  * course no flags are set in timer_hpts_flags.
196  *
197  * The rack_times are a misc collection of information that
198  * the old stack might possibly fill in. Of course its possible
199  * that an old stack may not have a piece of information. If so
200  * then setting that value to zero is advised. Setting any
201  * timestamp passed should only place a zero in it when it
202  * is unfilled. This may mean that a time is off by a micro-second
203  * but this is ok in the grand scheme of things.
204  *
205  * When switching stacks it is desireable to get as much information
206  * from the old stack to the new stack as possible. Though not always
207  * will the stack be compatible in the types of information. The
208  * init() function needs to take care when it begins changing
209  * things such as inp_flags2 and the timer units to position these
210  * changes at a point where it is unlikely they will fail after
211  * making such changes. A stack optionally can have an "undo"
212  * function
213  *
214  * To transfer information to the old stack from the new in
215  * respect to LRO and the inp_flags2, the new stack should set
216  * the inp_flags2 to what it supports. The old stack in its
217  * fini() function should call the tcp_handle_orphaned_packets()
218  * to clean up any packets. Note that a new stack should attempt
219  */
220 
221 /* Query types */
222 #define TCP_QUERY_SENDMAP	1
223 #define TCP_QUERY_TIMERS_UP	2
224 #define TCP_QUERY_RACK_TIMES	3
225 
226 /* Flags returned in sendmap_flags */
227 #define SNDMAP_ACKED		0x000001/* The remote endpoint acked this */
228 #define SNDMAP_OVERMAX		0x000008/* We have more retran's then we can fit */
229 #define SNDMAP_SACK_PASSED	0x000010/* A sack was done above this block */
230 #define SNDMAP_HAS_FIN		0x000040/* segment is sent with fin */
231 #define SNDMAP_TLP		0x000080/* segment sent as tail-loss-probe */
232 #define SNDMAP_HAS_SYN		0x000800/* SYN is on this guy */
233 #define SNDMAP_HAD_PUSH		0x008000/* Push was sent on original send */
234 #define SNDMAP_MASK  (SNDMAP_ACKED|SNDMAP_OVERMAX|SNDMAP_SACK_PASSED|SNDMAP_HAS_FIN\
235 		      |SNDMAP_TLP|SNDMAP_HAS_SYN|SNDMAP_HAD_PUSH)
236 #define SNDMAP_NRTX 3
237 
238 struct tcp_query_resp {
239 	int req;
240 	uint32_t req_param;
241 	union {
242 		struct {
243 			tcp_seq sendmap_start;
244 			tcp_seq sendmap_end;
245 			int sendmap_send_cnt;
246 			uint64_t sendmap_time[SNDMAP_NRTX];
247 			uint64_t sendmap_ack_arrival;
248 			int sendmap_flags;
249 			uint32_t sendmap_r_rtr_bytes;
250 			/* If FAS is available if not 0 */
251 			uint32_t sendmap_fas;
252 			uint8_t sendmap_dupacks;
253 		};
254 		struct {
255 			uint32_t timer_hpts_flags;
256 			uint32_t timer_pacing_to;
257 			uint32_t timer_timer_exp;
258 		};
259 		struct {
260 			/* Timestamps and rtt's */
261 			uint32_t rack_reorder_ts;	/* Last uscts that reordering was seen */
262 			uint32_t rack_num_dsacks;	/* Num of dsacks seen */
263 			uint32_t rack_rxt_last_time; 	/* Last time a RXT/TLP or rack tmr  went off */
264 			uint32_t rack_min_rtt;		/* never 0 smallest rtt seen */
265 			uint32_t rack_rtt;		/* Last rtt used by rack */
266 			uint32_t rack_tmit_time;	/* The time the rtt seg was tmited */
267 			uint32_t rack_time_went_idle;	/* If in persist the time we went idle */
268 			/* Prr data  */
269 			uint32_t rack_sacked;
270 			uint32_t rack_holes_rxt;
271 			uint32_t rack_prr_delivered;
272 			uint32_t rack_prr_recovery_fs;
273 			uint32_t rack_prr_out;
274 			uint32_t rack_prr_sndcnt;
275 			/* TLP data */
276 			uint16_t rack_tlp_cnt_out;	/* How many tlp's have been sent */
277 			/* Various bits */
278 			uint8_t  rack_tlp_out;		/* Is a TLP outstanding */
279 			uint8_t  rack_srtt_measured;	/* The previous stack has measured srtt */
280 			uint8_t  rack_in_persist;	/* Is the old stack in persists? */
281 			uint8_t	 rack_wanted_output;	/* Did the prevous stack have a want output set */
282 		};
283 	};
284 };
285 
286 #define TCP_TMR_GRANULARITY_TICKS	1	/* TCP timers are in ticks (msec if hz=1000)  */
287 #define TCP_TMR_GRANULARITY_USEC	2	/* TCP timers are in microseconds */
288 
289 typedef enum {
290 	TT_REXMT = 0,
291 	TT_PERSIST,
292 	TT_KEEP,
293 	TT_2MSL,
294 	TT_DELACK,
295 	TT_N,
296 } tt_which;
297 
298 typedef enum {
299 	TT_PROCESSING = 0,
300 	TT_PROCESSED,
301 	TT_STARTING,
302 	TT_STOPPING,
303 } tt_what;
304 
305 /*
306  * Tcp control block, one per tcp connection.
307  */
308 struct tcpcb {
309 	struct inpcb t_inpcb;		/* embedded protocol independent cb */
310 #define	t_start_zero	t_fb
311 #define	t_zero_size	(sizeof(struct tcpcb) - \
312 			    offsetof(struct tcpcb, t_start_zero))
313 	struct tcp_function_block *t_fb;/* TCP function call block */
314 	void	*t_fb_ptr;		/* Pointer to t_fb specific data */
315 
316 	struct callout t_callout;
317 	sbintime_t t_timers[TT_N];
318 	sbintime_t t_precisions[TT_N];
319 
320 	/* HPTS. Used by BBR and Rack stacks. See tcp_hpts.c for more info. */
321 	TAILQ_ENTRY(tcpcb)	t_hpts;		/* linkage to HPTS ring */
322 	STAILQ_HEAD(, mbuf)	t_inqueue;	/* HPTS input packets queue */
323 	uint32_t t_hpts_request;	/* Current hpts request, zero if
324 					 * fits in the pacing window. */
325 	uint32_t t_hpts_slot;		/* HPTS wheel slot this tcb is. */
326 	uint32_t t_hpts_drop_reas;	/* Reason we are dropping the pcb. */
327 	uint32_t t_hpts_gencnt;
328 	uint16_t t_hpts_cpu;		/* CPU chosen by hpts_cpuid(). */
329 	uint16_t t_lro_cpu;		/* CPU derived from LRO. */
330 #define	HPTS_CPU_NONE	((uint16_t)-1)
331 	enum {
332 		IHPTS_NONE = 0,
333 		IHPTS_ONQUEUE,
334 		IHPTS_MOVING,
335 	} t_in_hpts;			/* Is it linked into HPTS? */
336 
337 	uint32_t t_maxseg:24,		/* maximum segment size */
338 		_t_logstate:8;		/* State of "black box" logging */
339 	uint32_t t_port:16,		/* Tunneling (over udp) port */
340 		t_state:4,		/* state of this connection */
341 		t_idle_reduce : 1,
342 		t_delayed_ack: 7,	/* Delayed ack variable */
343 		t_fin_is_rst: 1,	/* Are fin's treated as resets */
344 		t_log_state_set: 1,
345 		bits_spare : 2;
346 	u_int	t_flags;
347 	tcp_seq	snd_una;		/* sent but unacknowledged */
348 	tcp_seq	snd_max;		/* highest sequence number sent;
349 					 * used to recognize retransmits
350 					 */
351 	tcp_seq snd_nxt;		/* send next */
352 	tcp_seq snd_up;			/* send urgent pointer */
353 	uint32_t snd_wnd;		/* send window */
354 	uint32_t snd_cwnd;		/* congestion-controlled window */
355 	uint32_t ts_offset;		/* our timestamp offset */
356 	uint32_t rfbuf_ts;		/* recv buffer autoscaling timestamp */
357 	int	rcv_numsacks;		/* # distinct sack blks present */
358 	u_int	t_tsomax;		/* TSO total burst length limit */
359 	u_int	t_tsomaxsegcount;	/* TSO maximum segment count */
360 	u_int	t_tsomaxsegsize;	/* TSO maximum segment size in bytes */
361 	tcp_seq	rcv_nxt;		/* receive next */
362 	tcp_seq	rcv_adv;		/* advertised window */
363 	uint32_t rcv_wnd;		/* receive window */
364 	u_int	t_flags2;		/* More tcpcb flags storage */
365 	int	t_srtt;			/* smoothed round-trip time */
366 	int	t_rttvar;		/* variance in round-trip time */
367 	uint32_t ts_recent;		/* timestamp echo data */
368 	u_char	snd_scale;		/* window scaling for send window */
369 	u_char	rcv_scale;		/* window scaling for recv window */
370 	u_char	snd_limited;		/* segments limited transmitted */
371 	u_char	request_r_scale;	/* pending window scaling */
372 	tcp_seq	last_ack_sent;
373 	u_int	t_rcvtime;		/* inactivity time */
374 	tcp_seq	rcv_up;			/* receive urgent pointer */
375 	int	t_segqlen;		/* segment reassembly queue length */
376 	uint32_t t_segqmbuflen;		/* total reassembly queue byte length */
377 	struct	tsegqe_head t_segq;	/* segment reassembly queue */
378 	uint32_t snd_ssthresh;		/* snd_cwnd size threshold for
379 					 * for slow start exponential to
380 					 * linear switch
381 					 */
382 	tcp_seq	snd_wl1;		/* window update seg seq number */
383 	tcp_seq	snd_wl2;		/* window update seg ack number */
384 
385 	tcp_seq	irs;			/* initial receive sequence number */
386 	tcp_seq	iss;			/* initial send sequence number */
387 	u_int	t_acktime;		/* RACK and BBR incoming new data was acked */
388 	u_int	t_sndtime;		/* time last data was sent */
389 	u_int	ts_recent_age;		/* when last updated */
390 	tcp_seq	snd_recover;		/* for use in NewReno Fast Recovery */
391 	char	t_oobflags;		/* have some */
392 	char	t_iobc;			/* input character */
393 	uint8_t t_nic_ktls_xmit:1,	/* active nic ktls xmit sessions */
394 		t_nic_ktls_xmit_dis:1,	/* disabled nic xmit ktls? */
395 		t_nic_ktls_spare:6;	/* spare nic ktls */
396 	int	t_rxtcur;		/* current retransmit value (ticks) */
397 
398 	int	t_rxtshift;		/* log(2) of rexmt exp. backoff */
399 	u_int	t_rtttime;		/* RTT measurement start time */
400 
401 	tcp_seq	t_rtseq;		/* sequence number being timed */
402 	u_int	t_starttime;		/* time connection was established */
403 	u_int	t_fbyte_in;		/* ticks time first byte queued in */
404 	u_int	t_fbyte_out;		/* ticks time first byte queued out */
405 
406 	u_int	t_pmtud_saved_maxseg;	/* pre-blackhole MSS */
407 	int	t_blackhole_enter;	/* when to enter blackhole detection */
408 	int	t_blackhole_exit;	/* when to exit blackhole detection */
409 	u_int	t_rttmin;		/* minimum rtt allowed */
410 
411 	int	t_softerror;		/* possible error not yet reported */
412 	uint32_t max_sndwnd;		/* largest window peer has offered */
413 	uint32_t snd_cwnd_prev;		/* cwnd prior to retransmit */
414 	uint32_t snd_ssthresh_prev;	/* ssthresh prior to retransmit */
415 	tcp_seq	snd_recover_prev;	/* snd_recover prior to retransmit */
416 	int	t_sndzerowin;		/* zero-window updates sent */
417 	int	snd_numholes;		/* number of holes seen by sender */
418 	u_int	t_badrxtwin;		/* window for retransmit recovery */
419 	TAILQ_HEAD(sackhole_head, sackhole) snd_holes;
420 					/* SACK scoreboard (sorted) */
421 	tcp_seq	snd_fack;		/* last seq number(+1) sack'd by rcv'r*/
422 	struct sackblk sackblks[MAX_SACK_BLKS]; /* seq nos. of sack blocks */
423 	struct sackhint	sackhint;	/* SACK scoreboard hint */
424 	int	t_rttlow;		/* smallest observerved RTT */
425 	int	rfbuf_cnt;		/* recv buffer autoscaling byte count */
426 	struct toedev	*tod;		/* toedev handling this connection */
427 	int	t_sndrexmitpack;	/* retransmit packets sent */
428 	int	t_rcvoopack;		/* out-of-order packets received */
429 	void	*t_toe;			/* TOE pcb pointer */
430 	struct cc_algo	*t_cc;		/* congestion control algorithm */
431 	struct cc_var	t_ccv;		/* congestion control specific vars */
432 	int	t_bytes_acked;		/* # bytes acked during current RTT */
433 	u_int	t_maxunacktime;
434 	u_int	t_keepinit;		/* time to establish connection */
435 	u_int	t_keepidle;		/* time before keepalive probes begin */
436 	u_int	t_keepintvl;		/* interval between keepalives */
437 	u_int	t_keepcnt;		/* number of keepalives before close */
438 	int	t_dupacks;		/* consecutive dup acks recd */
439 	int	t_lognum;		/* Number of log entries */
440 	int	t_loglimit;		/* Maximum number of log entries */
441 	uint32_t t_rcep;		/* Number of received CE marked pkts */
442 	uint32_t t_scep;		/* Synced number of delivered CE pkts */
443 	int64_t	t_pacing_rate;		/* bytes / sec, -1 => unlimited */
444 	struct tcp_log_stailq t_logs;	/* Log buffer */
445 	struct tcp_log_id_node *t_lin;
446 	struct tcp_log_id_bucket *t_lib;
447 	const char *t_output_caller;	/* Function that called tcp_output */
448 	struct statsblob *t_stats;	/* Per-connection stats */
449 	/* Should these be a pointer to the arrays or an array? */
450 	uint32_t t_logsn;		/* Log "serial number" */
451 	uint32_t gput_ts;		/* Time goodput measurement started */
452 	tcp_seq gput_seq;		/* Outbound measurement seq */
453 	tcp_seq gput_ack;		/* Inbound measurement ack */
454 	int32_t t_stats_gput_prev;	/* XXXLAS: Prev gput measurement */
455 	uint32_t t_sndtlppack;		/* tail loss probe packets sent */
456 	uint64_t t_sndtlpbyte;		/* total tail loss probe bytes sent */
457 	uint64_t t_sndbytes;		/* total bytes sent */
458 	uint64_t t_snd_rxt_bytes;	/* total bytes retransmitted */
459 	uint32_t t_dsack_bytes;		/* dsack bytes received */
460 	uint32_t t_dsack_tlp_bytes;	/* dsack bytes received for TLPs sent */
461 	uint32_t t_dsack_pack;		/* dsack packets we have eceived */
462 	uint8_t t_tmr_granularity;	/* Granularity of all timers srtt etc */
463 	uint8_t t_rttupdated;		/* number of times rtt sampled */
464 	/* TCP Fast Open */
465 	uint8_t t_tfo_client_cookie_len; /* TFO client cookie length */
466 	uint32_t t_end_info_status;	/* Status flag of end info */
467 	sbintime_t t_challenge_ack_end;	/* End of the challenge ack epoch */
468 	uint32_t t_challenge_ack_cnt;	/* Number of challenge ACKs sent in
469 					 * current epoch
470 					 */
471 
472 	unsigned int *t_tfo_pending;	/* TFO server pending counter */
473 	union {
474 		uint8_t client[TCP_FASTOPEN_MAX_COOKIE_LEN];
475 		uint64_t server;
476 	} t_tfo_cookie;			/* TCP Fast Open cookie to send */
477 	union {
478 		uint8_t t_end_info_bytes[TCP_END_BYTE_INFO];
479 		uint64_t t_end_info;
480 	};
481 	struct osd	t_osd;		/* storage for Khelp module data */
482 	uint8_t _t_logpoint;	/* Used when a BB log points is enabled */
483 	/*
484 	 * Keep all #ifdef'ed components at the end of the structure!
485 	 * This is important to minimize problems when compiling modules
486 	 * using this structure from within the modules' directory.
487 	 */
488 #ifdef TCP_REQUEST_TRK
489 	/* Response tracking addons. */
490 	uint8_t t_tcpreq_req;	/* Request count */
491 	uint8_t t_tcpreq_open;	/* Number of open range requests */
492 	uint8_t t_tcpreq_closed;	/* Number of closed range requests */
493 	uint32_t tcp_hybrid_start;	/* Num of times we started hybrid pacing */
494 	uint32_t tcp_hybrid_stop;	/* Num of times we stopped hybrid pacing */
495 	uint32_t tcp_hybrid_error;	/* Num of times we failed to start hybrid pacing */
496 	struct tcp_sendfile_track t_tcpreq_info[MAX_TCP_TRK_REQ];
497 #endif
498 #ifdef TCP_ACCOUNTING
499 	uint64_t tcp_cnt_counters[TCP_NUM_CNT_COUNTERS];
500 	uint64_t tcp_proc_time[TCP_NUM_CNT_COUNTERS];
501 #endif
502 };
503 #endif	/* _KERNEL || _WANT_TCPCB */
504 
505 #ifdef _KERNEL
506 struct tcptemp {
507 	u_char	tt_ipgen[40]; /* the size must be of max ip header, now IPv6 */
508 	struct	tcphdr tt_t;
509 };
510 
511 /* SACK scoreboard update status */
512 typedef enum {
513 	SACK_NOCHANGE = 0,
514 	SACK_CHANGE,
515 	SACK_NEWLOSS
516 } sackstatus_t;
517 
518 /* Enable TCP/UDP tunneling port */
519 #define TCP_TUNNELING_PORT_MIN		0
520 #define TCP_TUNNELING_PORT_MAX		65535
521 #define TCP_TUNNELING_PORT_DEFAULT	0
522 
523 /* Enable TCP/UDP tunneling port */
524 #define TCP_TUNNELING_OVERHEAD_MIN	sizeof(struct udphdr)
525 #define TCP_TUNNELING_OVERHEAD_MAX	1024
526 #define TCP_TUNNELING_OVERHEAD_DEFAULT	TCP_TUNNELING_OVERHEAD_MIN
527 
528 /* Minimum map entries limit value, if set */
529 #define TCP_MIN_MAP_ENTRIES_LIMIT	128
530 
531 /* Flags for tcp functions */
532 #define	TCP_FUNC_BEING_REMOVED	0x01   	/* Can no longer be referenced */
533 #define	TCP_FUNC_OUTPUT_CANDROP	0x02   	/* tfb_tcp_output may ask tcp_drop */
534 #define	TCP_FUNC_DEFAULT_OK	0x04   	/* Can be used as default */
535 
536 /**
537  * tfb_tcp_handoff_ok is a mandatory function allowing
538  * to query a stack, if it can take over a tcpcb.
539  * You return 0 to say you can take over and run your stack,
540  * you return non-zero (an error number) to say no you can't.
541  *
542  * tfb_tcp_fb_init is used to allow the new stack to
543  * setup its control block. Among the things it must
544  * do is:
545  * a) Make sure that the inp_flags2 is setup correctly
546  *    for LRO. There are two flags that the previous
547  *    stack may have set INP_MBUF_ACKCMP and
548  *    INP_SUPPORTS_MBUFQ. If the new stack does not
549  *    support these it *should* clear the flags.
550  * b) Make sure that the timers are in the proper
551  *    granularity that the stack wants. The stack
552  *    should check the t_tmr_granularity field. Currently
553  *    there are two values that it may hold
554  *    TCP_TMR_GRANULARITY_TICKS and TCP_TMR_GRANULARITY_USEC.
555  *    Use the functions tcp_timer_convert(tp, granularity);
556  *    to move the timers to the correct format for your stack.
557  *
558  * The new stack may also optionally query the tfb_chg_query
559  * function if the old stack has one. The new stack may ask
560  * for one of three entries and can also state to the old
561  * stack its support for the INP_MBUF_ACKCMP and
562  * INP_SUPPORTS_MBUFQ. This is important since if there are
563  * queued ack's without that statement the old stack will
564  * be forced to discard the queued acks. The requests that
565  * can be made for information by the new stacks are:
566  *
567  * Note also that the tfb_tcp_fb_init() when called can
568  * determine if a query is needed by looking at the
569  * value passed in the ptr. The ptr is designed to be
570  * set in with any allocated memory, but the address
571  * of the condtion (ptr == &tp->t_fb_ptr) will be
572  * true if this is not a stack switch but the initial
573  * setup of a tcb (which means no query would be needed).
574  * If, however, the value is not t_fb_ptr, then the caller
575  * is in the middle of a stack switch and is the new stack.
576  * A query would be appropriate (if the new stack support
577  * the query mechanism).
578  *
579  * TCP_QUERY_SENDMAP - Query of outstanding data.
580  * TCP_QUERY_TIMERS_UP	- Query about running timers.
581  * TCP_SUPPORTED_LRO - Declaration in req_param of
582  *                     the inp_flags2 supported by
583  *                     the new stack.
584  * TCP_QUERY_RACK_TIMES	- Enquire about various timestamps
585  *                        and states the old stack may be in.
586  *
587  * tfb_tcp_fb_fini is changed to add a flag to tell
588  * the old stack if the tcb is being destroyed or
589  * not. A one in the flag means the TCB is being
590  * destroyed, a zero indicates its transitioning to
591  * another stack (via socket option). The
592  * tfb_tcp_fb_fini() function itself should not change timers
593  * or inp_flags2 (the tfb_tcp_fb_init() must do that). However
594  * if the old stack supports the LRO mbuf queuing, and the new
595  * stack does not communicate via chg messages that it too does,
596  * it must assume it does not and free any queued mbufs.
597  *
598  */
599 struct tcp_function_block {
600 	char tfb_tcp_block_name[TCP_FUNCTION_NAME_LEN_MAX];
601 	int	(*tfb_tcp_output)(struct tcpcb *);
602 	void	(*tfb_tcp_do_segment)(struct tcpcb *, struct mbuf *,
603 		    struct tcphdr *, int, int, uint8_t);
604 	int      (*tfb_do_segment_nounlock)(struct tcpcb *, struct mbuf *,
605 		    struct tcphdr *, int, int, uint8_t, int, struct timeval *);
606 	int     (*tfb_do_queued_segments)(struct tcpcb *, int);
607 	int     (*tfb_tcp_ctloutput)(struct tcpcb *, struct sockopt *);
608 	/* Optional memory allocation/free routine */
609 	int	(*tfb_tcp_fb_init)(struct tcpcb *, void **);
610 	void	(*tfb_tcp_fb_fini)(struct tcpcb *, int);
611 	/* Optional timers, must define all if you define one */
612 	int	(*tfb_tcp_timer_stop_all)(struct tcpcb *);
613 	void	(*tfb_tcp_rexmit_tmr)(struct tcpcb *);
614 	int	(*tfb_tcp_handoff_ok)(struct tcpcb *);
615 	void	(*tfb_tcp_mtu_chg)(struct tcpcb *tp);
616 	int	(*tfb_pru_options)(struct tcpcb *, int);
617 	void	(*tfb_hwtls_change)(struct tcpcb *, int);
618 	int	(*tfb_chg_query)(struct tcpcb *, struct tcp_query_resp *);
619 	void	(*tfb_switch_failed)(struct tcpcb *);
620 	bool	(*tfb_early_wake_check)(struct tcpcb *);
621 	int     (*tfb_compute_pipe)(struct tcpcb *tp);
622 	int     (*tfb_stack_info)(struct tcpcb *tp, struct stack_specific_info *);
623 	void	(*tfb_inherit)(struct tcpcb *tp, struct inpcb *h_inp);
624 	volatile uint32_t tfb_refcnt;
625 	uint32_t  tfb_flags;
626 	uint8_t	tfb_id;
627 };
628 
629 /* Maximum number of names each TCP function block can be registered with. */
630 #define	TCP_FUNCTION_NAME_NUM_MAX	8
631 
632 struct tcp_function {
633 	TAILQ_ENTRY(tcp_function)	tf_next;
634 	char				tf_name[TCP_FUNCTION_NAME_LEN_MAX];
635 	struct tcp_function_block	*tf_fb;
636 };
637 
638 TAILQ_HEAD(tcp_funchead, tcp_function);
639 
640 struct tcpcb * tcp_drop(struct tcpcb *, int);
641 
642 #ifdef _NETINET_IN_PCB_H_
643 #define	intotcpcb(inp)	__containerof((inp), struct tcpcb, t_inpcb)
644 #define	sototcpcb(so)	intotcpcb(sotoinpcb(so))
645 #define	tptoinpcb(tp)	(&(tp)->t_inpcb)
646 #define	tptosocket(tp)	(tp)->t_inpcb.inp_socket
647 
648 /*
649  * tcp_output()
650  * Handles tcp_drop request from advanced stacks and reports that inpcb is
651  * gone with negative return code.
652  * Drop in replacement for the default stack.
653  */
654 static inline int
tcp_output(struct tcpcb * tp)655 tcp_output(struct tcpcb *tp)
656 {
657 	struct inpcb *inp = tptoinpcb(tp);
658 	int rv;
659 
660 	INP_WLOCK_ASSERT(inp);
661 
662 	rv = tp->t_fb->tfb_tcp_output(tp);
663 	if (rv < 0) {
664 		KASSERT(tp->t_fb->tfb_flags & TCP_FUNC_OUTPUT_CANDROP,
665 		    ("TCP stack %s requested tcp_drop(%p)",
666 		    tp->t_fb->tfb_tcp_block_name, tp));
667 		tp = tcp_drop(tp, -rv);
668 		if (tp)
669 			INP_WUNLOCK(inp);
670 	}
671 
672 	return (rv);
673 }
674 
675 /*
676  * tcp_output_unlock()
677  * Always returns unlocked, handles drop request from advanced stacks.
678  * Always returns positive error code.
679  */
680 static inline int
tcp_output_unlock(struct tcpcb * tp)681 tcp_output_unlock(struct tcpcb *tp)
682 {
683 	struct inpcb *inp = tptoinpcb(tp);
684 	int rv;
685 
686 	INP_WLOCK_ASSERT(inp);
687 
688 	rv = tp->t_fb->tfb_tcp_output(tp);
689 	if (rv < 0) {
690 		KASSERT(tp->t_fb->tfb_flags & TCP_FUNC_OUTPUT_CANDROP,
691 		    ("TCP stack %s requested tcp_drop(%p)",
692 		    tp->t_fb->tfb_tcp_block_name, tp));
693 		rv = -rv;
694 		tp = tcp_drop(tp, rv);
695 		if (tp)
696 			INP_WUNLOCK(inp);
697 	} else
698 		INP_WUNLOCK(inp);
699 
700 	return (rv);
701 }
702 
703 /*
704  * tcp_output_nodrop()
705  * Always returns locked.  It is caller's responsibility to run tcp_drop()!
706  * Useful in syscall implementations, when we want to perform some logging
707  * and/or tracing with tcpcb before calling tcp_drop().  To be used with
708  * tcp_unlock_or_drop() later.
709  *
710  * XXXGL: maybe don't allow stacks to return a drop request at certain
711  * TCP states? Why would it do in connect(2)? In recv(2)?
712  */
713 static inline int
tcp_output_nodrop(struct tcpcb * tp)714 tcp_output_nodrop(struct tcpcb *tp)
715 {
716 	int rv;
717 
718 	INP_WLOCK_ASSERT(tptoinpcb(tp));
719 
720 	rv = tp->t_fb->tfb_tcp_output(tp);
721 	KASSERT(rv >= 0 || tp->t_fb->tfb_flags & TCP_FUNC_OUTPUT_CANDROP,
722 	    ("TCP stack %s requested tcp_drop(%p)",
723 	    tp->t_fb->tfb_tcp_block_name, tp));
724 	return (rv);
725 }
726 
727 /*
728  * tcp_unlock_or_drop()
729  * Handle return code from tfb_tcp_output() after we have logged/traced,
730  * to be used with tcp_output_nodrop().
731  */
732 static inline int
tcp_unlock_or_drop(struct tcpcb * tp,int tcp_output_retval)733 tcp_unlock_or_drop(struct tcpcb *tp, int tcp_output_retval)
734 {
735 	struct inpcb *inp = tptoinpcb(tp);
736 
737 	INP_WLOCK_ASSERT(inp);
738 
739         if (tcp_output_retval < 0) {
740                 tcp_output_retval = -tcp_output_retval;
741                 if (tcp_drop(tp, tcp_output_retval) != NULL)
742                         INP_WUNLOCK(inp);
743         } else
744 		INP_WUNLOCK(inp);
745 
746 	return (tcp_output_retval);
747 }
748 #endif	/* _NETINET_IN_PCB_H_ */
749 
750 static int inline
tcp_packets_this_ack(struct tcpcb * tp,tcp_seq ack)751 tcp_packets_this_ack(struct tcpcb *tp, tcp_seq ack)
752 {
753 	return ((ack - tp->snd_una) / tp->t_maxseg +
754 		((((ack - tp->snd_una) % tp->t_maxseg) != 0) ? 1 : 0));
755 }
756 #endif	/* _KERNEL */
757 
758 /*
759  * Flags and utility macros for the t_flags field.
760  */
761 #define	TF_ACKNOW	0x00000001	/* ack peer immediately */
762 #define	TF_DELACK	0x00000002	/* ack, but try to delay it */
763 #define	TF_NODELAY	0x00000004	/* don't delay packets to coalesce */
764 #define	TF_NOOPT	0x00000008	/* don't use tcp options */
765 #define	TF_SENTFIN	0x00000010	/* have sent FIN */
766 #define	TF_REQ_SCALE	0x00000020	/* have/will request window scaling */
767 #define	TF_RCVD_SCALE	0x00000040	/* other side has requested scaling */
768 #define	TF_REQ_TSTMP	0x00000080	/* have/will request timestamps */
769 #define	TF_RCVD_TSTMP	0x00000100	/* a timestamp was received in SYN */
770 #define	TF_SACK_PERMIT	0x00000200	/* other side said I could SACK */
771 #define	TF_NEEDSYN	0x00000400	/* send SYN (implicit state) */
772 #define	TF_NEEDFIN	0x00000800	/* send FIN (implicit state) */
773 #define	TF_NOPUSH	0x00001000	/* don't push */
774 #define	TF_PREVVALID	0x00002000	/* saved values for bad rxmit valid
775 					 * Note: accessing and restoring from
776 					 * these may only be done in the 1st
777 					 * RTO recovery round (t_rxtshift == 1)
778 					 */
779 #define	TF_WAKESOR	0x00004000	/* wake up receive socket */
780 #define	TF_GPUTINPROG	0x00008000	/* Goodput measurement in progress */
781 #define	TF_MORETOCOME	0x00010000	/* More data to be appended to sock */
782 #define	TF_SONOTCONN	0x00020000	/* needs soisconnected() on ESTAB */
783 #define	TF_LASTIDLE	0x00040000	/* connection was previously idle */
784 #define	TF_RXWIN0SENT	0x00080000	/* sent a receiver win 0 in response */
785 #define	TF_FASTRECOVERY	0x00100000	/* in NewReno Fast Recovery */
786 #define	TF_WASFRECOVERY	0x00200000	/* was in NewReno Fast Recovery */
787 #define	TF_SIGNATURE	0x00400000	/* require MD5 digests (RFC2385) */
788 #define	TF_FORCEDATA	0x00800000	/* force out a byte */
789 #define	TF_TSO		0x01000000	/* TSO enabled on this connection */
790 #define	TF_TOE		0x02000000	/* this connection is offloaded */
791 #define	TF_CLOSED	0x04000000	/* close(2) called on socket */
792 #define TF_SENTSYN      0x08000000      /* At least one syn has been sent */
793 #define	TF_LRD		0x10000000	/* Lost Retransmission Detection */
794 #define	TF_CONGRECOVERY	0x20000000	/* congestion recovery mode */
795 #define	TF_WASCRECOVERY	0x40000000	/* was in congestion recovery */
796 #define	TF_FASTOPEN	0x80000000	/* TCP Fast Open indication */
797 
798 #define	IN_FASTRECOVERY(t_flags)	(t_flags & TF_FASTRECOVERY)
799 #define	ENTER_FASTRECOVERY(t_flags)	t_flags |= TF_FASTRECOVERY
800 #define	EXIT_FASTRECOVERY(t_flags)	t_flags &= ~TF_FASTRECOVERY
801 
802 #define	IN_CONGRECOVERY(t_flags)	(t_flags & TF_CONGRECOVERY)
803 #define	ENTER_CONGRECOVERY(t_flags)	t_flags |= TF_CONGRECOVERY
804 #define	EXIT_CONGRECOVERY(t_flags)	t_flags &= ~TF_CONGRECOVERY
805 
806 #define	IN_RECOVERY(t_flags) (t_flags & (TF_CONGRECOVERY | TF_FASTRECOVERY))
807 #define	ENTER_RECOVERY(t_flags) t_flags |= (TF_CONGRECOVERY | TF_FASTRECOVERY)
808 #define	EXIT_RECOVERY(t_flags) t_flags &= ~(TF_CONGRECOVERY | TF_FASTRECOVERY)
809 
810 #define	BYTES_THIS_ACK(tp, th)	(th->th_ack - tp->snd_una)
811 
812 /*
813  * Flags for the t_oobflags field.
814  */
815 #define	TCPOOB_HAVEDATA	0x01
816 #define	TCPOOB_HADDATA	0x02
817 
818 /*
819  * Flags for the extended TCP flags field, t_flags2
820  */
821 #define	TF2_PLPMTU_BLACKHOLE	0x00000001 /* Possible PLPMTUD Black Hole. */
822 #define	TF2_PLPMTU_PMTUD	0x00000002 /* Allowed to attempt PLPMTUD. */
823 #define	TF2_PLPMTU_MAXSEGSNT	0x00000004 /* Last seg sent was full seg. */
824 #define	TF2_LOG_AUTO		0x00000008 /* Session is auto-logging. */
825 #define	TF2_DROP_AF_DATA	0x00000010 /* Drop after all data ack'd */
826 #define	TF2_ECN_PERMIT		0x00000020 /* connection ECN-ready */
827 #define	TF2_ECN_SND_CWR		0x00000040 /* ECN CWR in queue */
828 #define	TF2_ECN_SND_ECE		0x00000080 /* ECN ECE in queue */
829 #define	TF2_ACE_PERMIT		0x00000100 /* Accurate ECN mode */
830 #define	TF2_HPTS_CPU_SET	0x00000200 /* t_hpts_cpu is not random */
831 #define	TF2_FBYTES_COMPLETE	0x00000400 /* We have first bytes in and out */
832 #define	TF2_ECN_USE_ECT1	0x00000800 /* Use ECT(1) marking on session */
833 #define TF2_TCP_ACCOUNTING	0x00001000 /* Do TCP accounting */
834 #define	TF2_HPTS_CALLS		0x00002000 /* tcp_output() called via HPTS */
835 #define	TF2_MBUF_L_ACKS		0x00004000 /* large mbufs for ack compression */
836 #define	TF2_MBUF_ACKCMP		0x00008000 /* mbuf ack compression ok */
837 #define	TF2_SUPPORTS_MBUFQ	0x00010000 /* Supports the mbuf queue method */
838 #define	TF2_MBUF_QUEUE_READY	0x00020000 /* Inputs can be queued */
839 #define	TF2_DONT_SACK_QUEUE	0x00040000 /* Don't wake on sack */
840 #define	TF2_CANNOT_DO_ECN	0x00080000 /* The stack does not do ECN */
841 #define	TF2_PROC_SACK_PROHIBIT	0x00100000 /* Due to small MSS size do not process sack's */
842 #define	TF2_IPSEC_TSO		0x00200000 /* IPSEC + TSO supported */
843 #define	TF2_NO_ISS_CHECK	0x00400000 /* Don't check SEG.ACK against ISS */
844 
845 /*
846  * Structure to hold TCP options that are only used during segment
847  * processing (in tcp_input), but not held in the tcpcb.
848  * It's basically used to reduce the number of parameters
849  * to tcp_dooptions and tcp_addoptions.
850  * The binary order of the to_flags is relevant for packing of the
851  * options in tcp_addoptions.
852  */
853 struct tcpopt {
854 	u_int32_t	to_flags;	/* which options are present */
855 #define	TOF_MSS		0x0001		/* maximum segment size */
856 #define	TOF_SCALE	0x0002		/* window scaling */
857 #define	TOF_SACKPERM	0x0004		/* SACK permitted */
858 #define	TOF_TS		0x0010		/* timestamp */
859 #define	TOF_SIGNATURE	0x0040		/* TCP-MD5 signature option (RFC2385) */
860 #define	TOF_SACK	0x0080		/* Peer sent SACK option */
861 #define	TOF_FASTOPEN	0x0100		/* TCP Fast Open (TFO) cookie */
862 #define	TOF_MAXOPT	0x0200
863 	u_int32_t	to_tsval;	/* new timestamp */
864 	u_int32_t	to_tsecr;	/* reflected timestamp */
865 	u_char		*to_sacks;	/* pointer to the first SACK blocks */
866 	u_char		*to_signature;	/* pointer to the TCP-MD5 signature */
867 	u_int8_t	*to_tfo_cookie; /* pointer to the TFO cookie */
868 	u_int16_t	to_mss;		/* maximum segment size */
869 	u_int8_t	to_wscale;	/* window scaling */
870 	u_int8_t	to_nsacks;	/* number of SACK blocks */
871 	u_int8_t	to_tfo_len;	/* TFO cookie length */
872 	u_int32_t	to_spare;	/* UTO */
873 };
874 
875 /*
876  * Flags for tcp_dooptions.
877  */
878 #define	TO_SYN		0x01		/* parse SYN-only options */
879 
880 struct hc_metrics_lite {	/* must stay in sync with hc_metrics */
881 	uint32_t	hc_mtu;		/* MTU for this path */
882 	uint32_t	hc_ssthresh;	/* outbound gateway buffer limit */
883 	uint32_t	hc_rtt;		/* estimated round trip time */
884 	uint32_t	hc_rttvar;	/* estimated rtt variance */
885 	uint32_t	hc_cwnd;	/* congestion window */
886 	uint32_t	hc_sendpipe;	/* outbound delay-bandwidth product */
887 	uint32_t	hc_recvpipe;	/* inbound delay-bandwidth product */
888 };
889 
890 #ifndef _NETINET_IN_PCB_H_
891 struct in_conninfo;
892 #endif /* _NETINET_IN_PCB_H_ */
893 
894 /*
895  * The smoothed round-trip time and estimated variance
896  * are stored as fixed point numbers scaled by the values below.
897  * For convenience, these scales are also used in smoothing the average
898  * (smoothed = (1/scale)sample + ((scale-1)/scale)smoothed).
899  * With these scales, srtt has 3 bits to the right of the binary point,
900  * and thus an "ALPHA" of 0.875.  rttvar has 2 bits to the right of the
901  * binary point, and is smoothed with an ALPHA of 0.75.
902  */
903 #define	TCP_RTT_SCALE		32	/* multiplier for srtt; 5 bits frac. */
904 #define	TCP_RTT_SHIFT		5	/* shift for srtt; 5 bits frac. */
905 #define	TCP_RTTVAR_SCALE	16	/* multiplier for rttvar; 4 bits */
906 #define	TCP_RTTVAR_SHIFT	4	/* shift for rttvar; 4 bits */
907 #define	TCP_DELTA_SHIFT		2	/* see tcp_input.c */
908 
909 /*
910  * The initial retransmission should happen at rtt + 4 * rttvar.
911  * Because of the way we do the smoothing, srtt and rttvar
912  * will each average +1/2 tick of bias.  When we compute
913  * the retransmit timer, we want 1/2 tick of rounding and
914  * 1 extra tick because of +-1/2 tick uncertainty in the
915  * firing of the timer.  The bias will give us exactly the
916  * 1.5 tick we need.  But, because the bias is
917  * statistical, we have to test that we don't drop below
918  * the minimum feasible timer (which is 2 ticks).
919  * This version of the macro adapted from a paper by Lawrence
920  * Brakmo and Larry Peterson which outlines a problem caused
921  * by insufficient precision in the original implementation,
922  * which results in inappropriately large RTO values for very
923  * fast networks.
924  */
925 #define	TCP_REXMTVAL(tp) \
926 	max((tp)->t_rttmin, (((tp)->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT))  \
927 	  + (tp)->t_rttvar) >> TCP_DELTA_SHIFT)
928 
929 /*
930  * Global (per-VNET) TCP statistics.  The below structure represents what we
931  * export to the userland, but in the kernel we have an array of counter_u64_t
932  * with as many elements as there are members in the structure.  The counters
933  * shall be increased by TCPSTAT_INC() or KMOD_TCPSTAT_INC().  Adding a new
934  * counter also requires adding corresponding SDT probes into in_kdtrace.h and
935  * into in_kdtrace.c.
936  */
937 struct	tcpstat {
938 	uint64_t tcps_connattempt;	/* connections initiated */
939 	uint64_t tcps_accepts;		/* connections accepted */
940 	uint64_t tcps_connects;		/* connections established */
941 	uint64_t tcps_drops;		/* connections dropped */
942 	uint64_t tcps_conndrops;	/* embryonic connections dropped */
943 	uint64_t tcps_minmssdrops;	/* average minmss too low drops */
944 	uint64_t tcps_closed;		/* conn. closed (includes drops) */
945 	uint64_t tcps_segstimed;	/* segs where we tried to get rtt */
946 	uint64_t tcps_rttupdated;	/* times we succeeded */
947 	uint64_t tcps_delack;		/* delayed acks sent */
948 	uint64_t tcps_timeoutdrop;	/* conn. dropped in rxmt timeout */
949 	uint64_t tcps_rexmttimeo;	/* retransmit timeouts */
950 	uint64_t tcps_persisttimeo;	/* persist timeouts */
951 	uint64_t tcps_keeptimeo;	/* keepalive timeouts */
952 	uint64_t tcps_keepprobe;	/* keepalive probes sent */
953 	uint64_t tcps_keepdrops;	/* connections dropped in keepalive */
954 	uint64_t tcps_progdrops;	/* drops due to no progress */
955 
956 	uint64_t tcps_sndtotal;		/* total packets sent */
957 	uint64_t tcps_sndpack;		/* data packets sent */
958 	uint64_t tcps_sndbyte;		/* data bytes sent */
959 	uint64_t tcps_sndrexmitpack;	/* data packets retransmitted */
960 	uint64_t tcps_sndrexmitbyte;	/* data bytes retransmitted */
961 	uint64_t tcps_sndrexmitbad;	/* unnecessary packet retransmissions */
962 	uint64_t tcps_sndacks;		/* ack-only packets sent */
963 	uint64_t tcps_sndprobe;		/* window probes sent */
964 	uint64_t tcps_sndurg;		/* packets sent with URG only */
965 	uint64_t tcps_sndwinup;		/* window update-only packets sent */
966 	uint64_t tcps_sndctrl;		/* control (SYN|FIN|RST) packets sent */
967 
968 	uint64_t tcps_rcvtotal;		/* total packets received */
969 	uint64_t tcps_rcvpack;		/* packets received in sequence */
970 	uint64_t tcps_rcvbyte;		/* bytes received in sequence */
971 	uint64_t tcps_rcvbadsum;	/* packets received with ccksum errs */
972 	uint64_t tcps_rcvbadoff;	/* packets received with bad offset */
973 	uint64_t tcps_rcvreassfull;	/* packets dropped for no reass space */
974 	uint64_t tcps_rcvshort;		/* packets received too short */
975 	uint64_t tcps_rcvduppack;	/* duplicate-only packets received */
976 	uint64_t tcps_rcvdupbyte;	/* duplicate-only bytes received */
977 	uint64_t tcps_rcvpartduppack;	/* packets with some duplicate data */
978 	uint64_t tcps_rcvpartdupbyte;	/* dup. bytes in part-dup. packets */
979 	uint64_t tcps_rcvoopack;	/* out-of-order packets received */
980 	uint64_t tcps_rcvoobyte;	/* out-of-order bytes received */
981 	uint64_t tcps_rcvpackafterwin;	/* packets with data after window */
982 	uint64_t tcps_rcvbyteafterwin;	/* bytes rcvd after window */
983 	uint64_t tcps_rcvafterclose;	/* packets rcvd after "close" */
984 	uint64_t tcps_rcvwinprobe;	/* rcvd window probe packets */
985 	uint64_t tcps_rcvdupack;	/* rcvd duplicate acks */
986 	uint64_t tcps_rcvacktoomuch;	/* rcvd acks for unsent data */
987 	uint64_t tcps_rcvackpack;	/* rcvd ack packets */
988 	uint64_t tcps_rcvackbyte;	/* bytes acked by rcvd acks */
989 	uint64_t tcps_rcvwinupd;	/* rcvd window update packets */
990 	uint64_t tcps_pawsdrop;		/* segments dropped due to PAWS */
991 	uint64_t tcps_predack;		/* times hdr predict ok for acks */
992 	uint64_t tcps_preddat;		/* times hdr predict ok for data pkts */
993 	uint64_t tcps_pcbcachemiss;
994 	uint64_t tcps_cachedrtt;	/* times cached RTT in route updated */
995 	uint64_t tcps_cachedrttvar;	/* times cached rttvar updated */
996 	uint64_t tcps_cachedssthresh;	/* times cached ssthresh updated */
997 	uint64_t tcps_usedrtt;		/* times RTT initialized from route */
998 	uint64_t tcps_usedrttvar;	/* times RTTVAR initialized from rt */
999 	uint64_t tcps_usedssthresh;	/* times ssthresh initialized from rt*/
1000 	uint64_t tcps_persistdrop;	/* timeout in persist state */
1001 	uint64_t tcps_badsyn;		/* bogus SYN, e.g. premature ACK */
1002 	uint64_t tcps_mturesent;	/* resends due to MTU discovery */
1003 	uint64_t tcps_listendrop;	/* listen queue overflows */
1004 	uint64_t tcps_badrst;		/* ignored RSTs in the window */
1005 
1006 	uint64_t tcps_sc_added;		/* entry added to syncache */
1007 	uint64_t tcps_sc_retransmitted;	/* syncache entry was retransmitted */
1008 	uint64_t tcps_sc_dupsyn;	/* duplicate SYN packet */
1009 	uint64_t tcps_sc_dropped;	/* could not reply to packet */
1010 	uint64_t tcps_sc_completed;	/* successful extraction of entry */
1011 	uint64_t tcps_sc_bucketoverflow;/* syncache per-bucket limit hit */
1012 	uint64_t tcps_sc_cacheoverflow;	/* syncache cache limit hit */
1013 	uint64_t tcps_sc_reset;		/* RST removed entry from syncache */
1014 	uint64_t tcps_sc_stale;		/* timed out or listen socket gone */
1015 	uint64_t tcps_sc_aborted;	/* syncache entry aborted */
1016 	uint64_t tcps_sc_badack;	/* removed due to bad ACK */
1017 	uint64_t tcps_sc_unreach;	/* ICMP unreachable received */
1018 	uint64_t tcps_sc_zonefail;	/* zalloc() failed */
1019 	uint64_t tcps_sc_sendcookie;	/* SYN cookie sent */
1020 	uint64_t tcps_sc_recvcookie;	/* SYN cookie received */
1021 	uint64_t tcps_sc_spurcookie;	/* SYN cookie spurious, rejected */
1022 	uint64_t tcps_sc_failcookie;	/* SYN cookie failed, rejected */
1023 
1024 	uint64_t tcps_hc_added;		/* entry added to hostcache */
1025 	uint64_t tcps_hc_bucketoverflow;/* hostcache per bucket limit hit */
1026 
1027 	uint64_t tcps_finwait2_drops;    /* Drop FIN_WAIT_2 connection after time limit */
1028 
1029 	/* SACK related stats */
1030 	uint64_t tcps_sack_recovery_episode; /* SACK recovery episodes */
1031 	uint64_t tcps_sack_rexmits;	    /* SACK rexmit segments   */
1032 	uint64_t tcps_sack_rexmits_tso;	    /* SACK rexmit TSO chunks */
1033 	uint64_t tcps_sack_rexmit_bytes;    /* SACK rexmit bytes      */
1034 	uint64_t tcps_sack_rcv_blocks;	    /* SACK blocks (options) received */
1035 	uint64_t tcps_sack_send_blocks;	    /* SACK blocks (options) sent     */
1036 	uint64_t tcps_sack_lostrexmt;	    /* SACK lost retransmission recovered */
1037 	uint64_t tcps_sack_sboverflow;	    /* times scoreboard overflowed */
1038 
1039 	/* ECN related stats */
1040 	uint64_t tcps_ecn_rcvce;		/* ECN Congestion Experienced */
1041 	uint64_t tcps_ecn_rcvect0;		/* ECN Capable Transport */
1042 	uint64_t tcps_ecn_rcvect1;		/* ECN Capable Transport */
1043 	uint64_t tcps_ecn_shs;		/* ECN successful handshakes */
1044 	uint64_t tcps_ecn_rcwnd;	/* # times ECN reduced the cwnd */
1045 
1046 	/* TCP_SIGNATURE related stats */
1047 	uint64_t tcps_sig_rcvgoodsig;	/* Total matching signature received */
1048 	uint64_t tcps_sig_rcvbadsig;	/* Total bad signature received */
1049 	uint64_t tcps_sig_err_buildsig;	/* Failed to make signature */
1050 	uint64_t tcps_sig_err_sigopt;	/* No signature expected by socket */
1051 	uint64_t tcps_sig_err_nosigopt;	/* No signature provided by segment */
1052 
1053 	/* Path MTU Discovery Black Hole Detection related stats */
1054 	uint64_t tcps_pmtud_blackhole_activated;	 /* Black Hole Count */
1055 	uint64_t tcps_pmtud_blackhole_activated_min_mss; /* BH at min MSS Count */
1056 	uint64_t tcps_pmtud_blackhole_failed;		 /* Black Hole Failure Count */
1057 
1058 	uint64_t tcps_tunneled_pkts;	/* Packets encap's in UDP received */
1059 	uint64_t tcps_tunneled_errs;	/* Packets that had errors that were UDP encaped */
1060 
1061 	/* Dsack related stats */
1062 	uint64_t tcps_dsack_count;	/* Number of ACKs arriving with DSACKs */
1063 	uint64_t tcps_dsack_bytes;	/* Number of bytes DSACK'ed no TLP */
1064 	uint64_t tcps_dsack_tlp_bytes;	/* Number of bytes DSACK'ed due to TLPs */
1065 
1066 	/* TCPS_TIME_WAIT usage stats */
1067 	uint64_t tcps_tw_recycles;	/* Times time-wait was recycled. */
1068 	uint64_t tcps_tw_resets;	/* Times time-wait sent a reset. */
1069 	uint64_t tcps_tw_responds;	/* Times time-wait sent a valid ack. */
1070 
1071 	/* Accurate ECN Handshake stats */
1072 	uint64_t tcps_ace_nect;		/* ACE SYN packet with Non-ECT */
1073 	uint64_t tcps_ace_ect1;		/* ACE SYN packet with ECT1 */
1074 	uint64_t tcps_ace_ect0;		/* ACE SYN packet with ECT0 */
1075 	uint64_t tcps_ace_ce;		/* ACE SYN packet with CE */
1076 
1077 	/* ECN related stats */
1078 	uint64_t tcps_ecn_sndect0;		/* ECN Capable Transport */
1079 	uint64_t tcps_ecn_sndect1;		/* ECN Capable Transport */
1080 
1081 	/*
1082 	 * BBR and Rack implement TLP's these values count TLP bytes in
1083 	 * two catagories, bytes that were retransmitted and bytes that
1084 	 * were newly transmited. Both types can serve as TLP's but they
1085 	 * are accounted differently.
1086 	 */
1087 	uint64_t tcps_tlpresends;	/* number of tlp resends */
1088 	uint64_t tcps_tlpresend_bytes;	/* number of bytes resent by tlp */
1089 
1090 	/* SEG.ACK validation failures */
1091 	uint64_t tcps_rcvghostack;	/* received ACK for data never sent */
1092 	uint64_t tcps_rcvacktooold;	/* received ACK for data too long ago */
1093 
1094 
1095 	uint64_t _pad[1];		/* 1 TBD placeholder for STABLE */
1096 };
1097 
1098 #define	tcps_rcvmemdrop	tcps_rcvreassfull	/* compat */
1099 
1100 #ifdef _KERNEL
1101 #include <sys/counter.h>
1102 #include <netinet/in_kdtrace.h>
1103 
1104 VNET_PCPUSTAT_DECLARE(struct tcpstat, tcpstat);	/* tcp statistics */
1105 /*
1106  * In-kernel consumers can use these accessor macros directly to update
1107  * stats.
1108  */
1109 #define TCPSTAT_ADD(name, val)                                           \
1110 	do {                                                             \
1111 		MIB_SDT_PROBE1(tcp, count, name, (val));                 \
1112 		VNET_PCPUSTAT_ADD(struct tcpstat, tcpstat, name, (val)); \
1113 	} while (0)
1114 #define	TCPSTAT_INC(name)	TCPSTAT_ADD(name, 1)
1115 
1116 /*
1117  * Kernel module consumers must use this accessor macro.
1118  */
1119 void	kmod_tcpstat_add(int statnum, int val);
1120 #define KMOD_TCPSTAT_ADD(name, val)                               \
1121 	do {                                                      \
1122 		MIB_SDT_PROBE1(tcp, count, name, (val));          \
1123 		kmod_tcpstat_add(offsetof(struct tcpstat, name) / \
1124 			sizeof(uint64_t),                         \
1125 		    val);                                         \
1126 	} while (0)
1127 #define	KMOD_TCPSTAT_INC(name)	KMOD_TCPSTAT_ADD(name, 1)
1128 
1129 /*
1130  * Running TCP connection count by state.
1131  */
1132 VNET_DECLARE(counter_u64_t, tcps_states[TCP_NSTATES]);
1133 #define	V_tcps_states	VNET(tcps_states)
1134 #define	TCPSTATES_INC(state)	counter_u64_add(V_tcps_states[state], 1)
1135 #define	TCPSTATES_DEC(state)	counter_u64_add(V_tcps_states[state], -1)
1136 
1137 /*
1138  * TCP specific helper hook point identifiers.
1139  */
1140 #define	HHOOK_TCP_EST_IN		0
1141 #define	HHOOK_TCP_EST_OUT		1
1142 #define	HHOOK_TCP_LAST			HHOOK_TCP_EST_OUT
1143 
1144 struct tcp_hhook_data {
1145 	struct tcpcb	*tp;
1146 	struct tcphdr	*th;
1147 	struct tcpopt	*to;
1148 	uint32_t	len;
1149 	int		tso;
1150 	tcp_seq		curack;
1151 };
1152 #ifdef TCP_HHOOK
1153 void hhook_run_tcp_est_out(struct tcpcb *tp,
1154 	struct tcphdr *th, struct tcpopt *to,
1155 	uint32_t len, int tso);
1156 #endif
1157 #endif
1158 
1159 /*
1160  * TCB structure exported to user-land via sysctl(3).
1161  *
1162  * Fields prefixed with "xt_" are unique to the export structure, and fields
1163  * with "t_" or other prefixes match corresponding fields of 'struct tcpcb'.
1164  *
1165  * Legend:
1166  * (s) - used by userland utilities in src
1167  * (p) - used by utilities in ports
1168  * (3) - is known to be used by third party software not in ports
1169  * (n) - no known usage
1170  *
1171  * Evil hack: declare only if in_pcb.h and sys/socketvar.h have been
1172  * included.  Not all of our clients do.
1173  */
1174 #if defined(_NETINET_IN_PCB_H_) && defined(_SYS_SOCKETVAR_H_)
1175 struct xtcpcb {
1176 	ksize_t	xt_len;		/* length of this structure */
1177 	struct xinpcb	xt_inp;
1178 	char		xt_stack[TCP_FUNCTION_NAME_LEN_MAX];	/* (s) */
1179 	char		xt_logid[TCP_LOG_ID_LEN];	/* (s) */
1180 	char		xt_cc[TCP_CA_NAME_MAX];	/* (s) */
1181 	int64_t		spare64[6];
1182 	int32_t		t_state;		/* (s,p) */
1183 	uint32_t	t_flags;		/* (s,p) */
1184 	int32_t		t_sndzerowin;		/* (s) */
1185 	int32_t		t_sndrexmitpack;	/* (s) */
1186 	int32_t		t_rcvoopack;		/* (s) */
1187 	int32_t		t_rcvtime;		/* (s) */
1188 	int32_t		tt_rexmt;		/* (s) */
1189 	int32_t		tt_persist;		/* (s) */
1190 	int32_t		tt_keep;		/* (s) */
1191 	int32_t		tt_2msl;		/* (s) */
1192 	int32_t		tt_delack;		/* (s) */
1193 	int32_t		t_logstate;		/* (3) */
1194 	uint32_t	t_snd_cwnd;		/* (s) */
1195 	uint32_t	t_snd_ssthresh;		/* (s) */
1196 	uint32_t	t_maxseg;		/* (s) */
1197 	uint32_t	t_rcv_wnd;		/* (s) */
1198 	uint32_t	t_snd_wnd;		/* (s) */
1199 	uint32_t	xt_ecn;			/* (s) */
1200 	uint32_t	t_dsack_bytes;		/* (n) */
1201 	uint32_t	t_dsack_tlp_bytes;	/* (n) */
1202 	uint32_t	t_dsack_pack;		/* (n) */
1203 	uint16_t	xt_encaps_port;		/* (s) */
1204 	int16_t		spare16;
1205 	int32_t		spare32[22];
1206 } __aligned(8);
1207 
1208 #ifdef _KERNEL
1209 void	tcp_inptoxtp(const struct inpcb *, struct xtcpcb *);
1210 #endif
1211 #endif
1212 
1213 /*
1214  * TCP function information (name-to-id mapping, aliases, and refcnt)
1215  * exported to user-land via sysctl(3).
1216  */
1217 struct tcp_function_info {
1218 	uint32_t	tfi_refcnt;
1219 	uint8_t		tfi_id;
1220 	char		tfi_name[TCP_FUNCTION_NAME_LEN_MAX];
1221 	char		tfi_alias[TCP_FUNCTION_NAME_LEN_MAX];
1222 };
1223 
1224 /*
1225  * Identifiers for TCP sysctl nodes
1226  */
1227 #define	TCPCTL_DO_RFC1323	1	/* use RFC-1323 extensions */
1228 #define	TCPCTL_MSSDFLT		3	/* MSS default */
1229 #define TCPCTL_STATS		4	/* statistics */
1230 #define	TCPCTL_RTTDFLT		5	/* default RTT estimate */
1231 #define	TCPCTL_KEEPIDLE		6	/* keepalive idle timer */
1232 #define	TCPCTL_KEEPINTVL	7	/* interval to send keepalives */
1233 #define	TCPCTL_SENDSPACE	8	/* send buffer space */
1234 #define	TCPCTL_RECVSPACE	9	/* receive buffer space */
1235 #define	TCPCTL_KEEPINIT		10	/* timeout for establishing syn */
1236 #define	TCPCTL_PCBLIST		11	/* list of all outstanding PCBs */
1237 #define	TCPCTL_DELACKTIME	12	/* time before sending delayed ACK */
1238 #define	TCPCTL_V6MSSDFLT	13	/* MSS default for IPv6 */
1239 #define	TCPCTL_SACK		14	/* Selective Acknowledgement,rfc 2018 */
1240 #define	TCPCTL_DROP		15	/* drop tcp connection */
1241 #define	TCPCTL_STATES		16	/* connection counts by TCP state */
1242 #define	TCPCTL_KTLSLIST		17	/* connections with active ktls
1243 					   session */
1244 #define	TCPCTL_KTLSLIST_WKEYS	18	/* KTLSLIST with key data exported */
1245 
1246 #ifdef _KERNEL
1247 #ifdef SYSCTL_DECL
1248 SYSCTL_DECL(_net_inet_tcp);
1249 SYSCTL_DECL(_net_inet_tcp_sack);
1250 MALLOC_DECLARE(M_TCPLOG);
1251 #endif
1252 
1253 VNET_DECLARE(int, tcp_log_in_vain);
1254 #define	V_tcp_log_in_vain		VNET(tcp_log_in_vain)
1255 
1256 /*
1257  * Global TCP tunables shared between different stacks.
1258  * Please keep the list sorted.
1259  */
1260 VNET_DECLARE(int, drop_synfin);
1261 VNET_DECLARE(int, path_mtu_discovery);
1262 VNET_DECLARE(int, tcp_abc_l_var);
1263 VNET_DECLARE(uint32_t, tcp_ack_war_cnt);
1264 VNET_DECLARE(uint32_t, tcp_ack_war_time_window);
1265 VNET_DECLARE(int, tcp_autorcvbuf_max);
1266 VNET_DECLARE(int, tcp_autosndbuf_inc);
1267 VNET_DECLARE(int, tcp_autosndbuf_max);
1268 VNET_DECLARE(int, tcp_bind_all_fibs);
1269 VNET_DECLARE(int, tcp_delack_enabled);
1270 VNET_DECLARE(int, tcp_do_autorcvbuf);
1271 VNET_DECLARE(int, tcp_do_autosndbuf);
1272 VNET_DECLARE(int, tcp_do_ecn);
1273 VNET_DECLARE(int, tcp_do_lrd);
1274 VNET_DECLARE(int, tcp_do_prr);
1275 VNET_DECLARE(int, tcp_do_prr_conservative);
1276 VNET_DECLARE(int, tcp_do_newcwv);
1277 VNET_DECLARE(int, tcp_do_rfc1323);
1278 VNET_DECLARE(int, tcp_tolerate_missing_ts);
1279 VNET_DECLARE(int, tcp_do_rfc3042);
1280 VNET_DECLARE(int, tcp_do_rfc3390);
1281 VNET_DECLARE(int, tcp_do_rfc3465);
1282 VNET_DECLARE(int, tcp_do_newsack);
1283 VNET_DECLARE(int, tcp_do_sack);
1284 VNET_DECLARE(int, tcp_do_tso);
1285 VNET_DECLARE(int, tcp_ecn_maxretries);
1286 VNET_DECLARE(int, tcp_initcwnd_segments);
1287 VNET_DECLARE(int, tcp_insecure_rst);
1288 VNET_DECLARE(int, tcp_insecure_syn);
1289 VNET_DECLARE(int, tcp_insecure_ack);
1290 VNET_DECLARE(uint32_t, tcp_map_entries_limit);
1291 VNET_DECLARE(uint32_t, tcp_map_split_limit);
1292 VNET_DECLARE(int, tcp_minmss);
1293 VNET_DECLARE(int, tcp_mssdflt);
1294 #ifdef STATS
1295 VNET_DECLARE(int, tcp_perconn_stats_dflt_tpl);
1296 VNET_DECLARE(int, tcp_perconn_stats_enable);
1297 #endif /* STATS */
1298 VNET_DECLARE(int, tcp_recvspace);
1299 VNET_DECLARE(int, tcp_retries);
1300 VNET_DECLARE(int, tcp_sack_globalholes);
1301 VNET_DECLARE(int, tcp_sack_globalmaxholes);
1302 VNET_DECLARE(int, tcp_sack_maxholes);
1303 VNET_DECLARE(int, tcp_sack_tso);
1304 VNET_DECLARE(int, tcp_sc_rst_sock_fail);
1305 VNET_DECLARE(int, tcp_sendspace);
1306 VNET_DECLARE(int, tcp_udp_tunneling_overhead);
1307 VNET_DECLARE(int, tcp_udp_tunneling_port);
1308 VNET_DECLARE(struct inpcbinfo, tcbinfo);
1309 
1310 #define	V_tcp_do_lrd			VNET(tcp_do_lrd)
1311 #define	V_tcp_do_prr			VNET(tcp_do_prr)
1312 #define	V_tcp_do_newcwv			VNET(tcp_do_newcwv)
1313 #define	V_drop_synfin			VNET(drop_synfin)
1314 #define	V_path_mtu_discovery		VNET(path_mtu_discovery)
1315 #define	V_tcbinfo			VNET(tcbinfo)
1316 #define	V_tcp_abc_l_var			VNET(tcp_abc_l_var)
1317 #define	V_tcp_ack_war_cnt		VNET(tcp_ack_war_cnt)
1318 #define	V_tcp_ack_war_time_window	VNET(tcp_ack_war_time_window)
1319 #define	V_tcp_autorcvbuf_max		VNET(tcp_autorcvbuf_max)
1320 #define	V_tcp_autosndbuf_inc		VNET(tcp_autosndbuf_inc)
1321 #define	V_tcp_autosndbuf_max		VNET(tcp_autosndbuf_max)
1322 #define	V_tcp_bind_all_fibs		VNET(tcp_bind_all_fibs)
1323 #define	V_tcp_delack_enabled		VNET(tcp_delack_enabled)
1324 #define	V_tcp_do_autorcvbuf		VNET(tcp_do_autorcvbuf)
1325 #define	V_tcp_do_autosndbuf		VNET(tcp_do_autosndbuf)
1326 #define	V_tcp_do_ecn			VNET(tcp_do_ecn)
1327 #define	V_tcp_do_rfc1323		VNET(tcp_do_rfc1323)
1328 #define	V_tcp_tolerate_missing_ts	VNET(tcp_tolerate_missing_ts)
1329 #define V_tcp_ts_offset_per_conn	VNET(tcp_ts_offset_per_conn)
1330 #define	V_tcp_do_rfc3042		VNET(tcp_do_rfc3042)
1331 #define	V_tcp_do_rfc3390		VNET(tcp_do_rfc3390)
1332 #define	V_tcp_do_rfc3465		VNET(tcp_do_rfc3465)
1333 #define	V_tcp_do_newsack		VNET(tcp_do_newsack)
1334 #define	V_tcp_do_sack			VNET(tcp_do_sack)
1335 #define	V_tcp_do_tso			VNET(tcp_do_tso)
1336 #define	V_tcp_ecn_maxretries		VNET(tcp_ecn_maxretries)
1337 #define	V_tcp_initcwnd_segments		VNET(tcp_initcwnd_segments)
1338 #define	V_tcp_insecure_rst		VNET(tcp_insecure_rst)
1339 #define	V_tcp_insecure_syn		VNET(tcp_insecure_syn)
1340 #define	V_tcp_insecure_ack		VNET(tcp_insecure_ack)
1341 #define	V_tcp_map_entries_limit		VNET(tcp_map_entries_limit)
1342 #define	V_tcp_map_split_limit		VNET(tcp_map_split_limit)
1343 #define	V_tcp_minmss			VNET(tcp_minmss)
1344 #define	V_tcp_mssdflt			VNET(tcp_mssdflt)
1345 #ifdef STATS
1346 #define	V_tcp_perconn_stats_dflt_tpl	VNET(tcp_perconn_stats_dflt_tpl)
1347 #define	V_tcp_perconn_stats_enable	VNET(tcp_perconn_stats_enable)
1348 #endif /* STATS */
1349 #define	V_tcp_recvspace			VNET(tcp_recvspace)
1350 #define	V_tcp_retries			VNET(tcp_retries)
1351 #define	V_tcp_sack_globalholes		VNET(tcp_sack_globalholes)
1352 #define	V_tcp_sack_globalmaxholes	VNET(tcp_sack_globalmaxholes)
1353 #define	V_tcp_sack_maxholes		VNET(tcp_sack_maxholes)
1354 #define	V_tcp_sack_tso			VNET(tcp_sack_tso)
1355 #define	V_tcp_sc_rst_sock_fail		VNET(tcp_sc_rst_sock_fail)
1356 #define	V_tcp_sendspace			VNET(tcp_sendspace)
1357 #define	V_tcp_udp_tunneling_overhead	VNET(tcp_udp_tunneling_overhead)
1358 #define	V_tcp_udp_tunneling_port	VNET(tcp_udp_tunneling_port)
1359 
1360 #ifdef TCP_HHOOK
1361 VNET_DECLARE(struct hhook_head *, tcp_hhh[HHOOK_TCP_LAST + 1]);
1362 #define	V_tcp_hhh		VNET(tcp_hhh)
1363 #endif
1364 
1365 void	tcp_account_for_send(struct tcpcb *, uint32_t, uint8_t, uint8_t, bool);
1366 int	 tcp_addoptions(struct tcpopt *, u_char *);
1367 struct tcpcb *
1368 	 tcp_close(struct tcpcb *);
1369 void	 tcp_discardcb(struct tcpcb *);
1370 void	 tcp_twstart(struct tcpcb *);
1371 int	 tcp_ctloutput(struct socket *, struct sockopt *);
1372 void	 tcp_fini(void *);
1373 char	*tcp_log_addrs(struct in_conninfo *, struct tcphdr *, const void *,
1374 	    const void *);
1375 char	*tcp_log_vain(struct in_conninfo *, struct tcphdr *, const void *,
1376 	    const void *);
1377 int	 tcp_reass(struct tcpcb *, struct tcphdr *, tcp_seq *, int *,
1378 	    struct mbuf *);
1379 void	 tcp_reass_global_init(void);
1380 void	 tcp_reass_flush(struct tcpcb *);
1381 void	 tcp_dooptions(struct tcpopt *, u_char *, int, int);
1382 void	tcp_dropwithreset(struct mbuf *, struct tcphdr *,
1383 		     struct tcpcb *, int, int);
1384 void	tcp_pulloutofband(struct socket *,
1385 		     struct tcphdr *, struct mbuf *, int);
1386 void	tcp_xmit_timer(struct tcpcb *, int);
1387 void	tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *);
1388 void	cc_ack_received(struct tcpcb *tp, struct tcphdr *th,
1389 			    uint16_t nsegs, uint16_t type);
1390 void 	cc_conn_init(struct tcpcb *tp);
1391 void 	cc_post_recovery(struct tcpcb *tp, struct tcphdr *th);
1392 void    cc_ecnpkt_handler(struct tcpcb *tp, struct tcphdr *th, uint8_t iptos);
1393 void	cc_ecnpkt_handler_flags(struct tcpcb *tp, uint16_t flags, uint8_t iptos);
1394 void	cc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type);
1395 #ifdef TCP_HHOOK
1396 void	hhook_run_tcp_est_in(struct tcpcb *tp,
1397 			    struct tcphdr *th, struct tcpopt *to);
1398 #endif
1399 
1400 int	 tcp_input(struct mbuf **, int *, int);
1401 int	 tcp_autorcvbuf(struct mbuf *, struct tcphdr *, struct socket *,
1402 	    struct tcpcb *, int);
1403 int	 tcp_input_with_port(struct mbuf **, int *, int, uint16_t);
1404 void	tcp_do_segment(struct tcpcb *, struct mbuf *, struct tcphdr *, int,
1405     int, uint8_t);
1406 
1407 int register_tcp_functions(struct tcp_function_block *blk, int wait);
1408 int register_tcp_functions_as_names(struct tcp_function_block *blk,
1409     int wait, const char *names[], int *num_names);
1410 int register_tcp_functions_as_name(struct tcp_function_block *blk,
1411     const char *name, int wait);
1412 int deregister_tcp_functions(struct tcp_function_block *blk, bool quiesce,
1413     bool force);
1414 struct tcp_function_block *find_and_ref_tcp_functions(struct tcp_function_set *fs);
1415 int find_tcp_function_alias(struct tcp_function_block *blk, struct tcp_function_set *fs);
1416 uint32_t tcp_get_srtt(struct tcpcb *tp, int granularity);
1417 void tcp_switch_back_to_default(struct tcpcb *tp);
1418 struct tcp_function_block *
1419 find_and_ref_tcp_fb(struct tcp_function_block *fs);
1420 int tcp_default_ctloutput(struct tcpcb *tp, struct sockopt *sopt);
1421 int tcp_ctloutput_set(struct inpcb *inp, struct sockopt *sopt);
1422 void tcp_log_socket_option(struct tcpcb *tp, uint32_t option_num,
1423     uint32_t option_val, int err);
1424 
1425 
1426 extern counter_u64_t tcp_inp_lro_direct_queue;
1427 extern counter_u64_t tcp_inp_lro_wokeup_queue;
1428 extern counter_u64_t tcp_inp_lro_compressed;
1429 extern counter_u64_t tcp_inp_lro_locks_taken;
1430 extern counter_u64_t tcp_extra_mbuf;
1431 extern counter_u64_t tcp_would_have_but;
1432 extern counter_u64_t tcp_comp_total;
1433 extern counter_u64_t tcp_uncomp_total;
1434 extern counter_u64_t tcp_bad_csums;
1435 
1436 extern uint32_t tcp_ack_war_time_window;
1437 extern uint32_t tcp_ack_war_cnt;
1438 
1439 /*
1440  * Used by tcp_maxmtu() to communicate interface specific features
1441  * and limits at the time of connection setup.
1442  */
1443 struct tcp_ifcap {
1444 	int	ifcap;
1445 	u_int	tsomax;
1446 	u_int	tsomaxsegcount;
1447 	u_int	tsomaxsegsize;
1448 	bool	ipsec_tso;
1449 };
1450 uint32_t tcp_maxmtu(struct in_conninfo *, struct tcp_ifcap *);
1451 uint32_t tcp_maxmtu6(struct in_conninfo *, struct tcp_ifcap *);
1452 
1453 void	 tcp6_use_min_mtu(struct tcpcb *);
1454 u_int	 tcp_maxseg(const struct tcpcb *);
1455 u_int	 tcp_fixed_maxseg(const struct tcpcb *);
1456 void	 tcp_mss_update(struct tcpcb *, int, int, struct hc_metrics_lite *,
1457 	    struct tcp_ifcap *);
1458 void	 tcp_mss(struct tcpcb *, int);
1459 int	 tcp_mssopt(struct in_conninfo *);
1460 struct tcpcb *
1461 	 tcp_newtcpcb(struct inpcb *, struct tcpcb *);
1462 int	 tcp_default_output(struct tcpcb *);
1463 void	 tcp_state_change(struct tcpcb *, int);
1464 void	 tcp_respond(struct tcpcb *, void *,
1465 	    struct tcphdr *, struct mbuf *, tcp_seq, tcp_seq, uint16_t);
1466 void	 tcp_send_challenge_ack(struct tcpcb *, struct tcphdr *, struct mbuf *);
1467 bool	 tcp_twcheck(struct inpcb *, struct tcpopt *, struct tcphdr *,
1468 	    struct mbuf *, int);
1469 void	 tcp_setpersist(struct tcpcb *);
1470 void	 tcp_record_dsack(struct tcpcb *tp, tcp_seq start, tcp_seq end, int tlp);
1471 struct tcptemp *
1472 	 tcpip_maketemplate(struct inpcb *);
1473 void	 tcpip_fillheaders(struct inpcb *, uint16_t, void *, void *);
1474 void	 tcp_timer_activate(struct tcpcb *, tt_which, u_int);
1475 bool	 tcp_timer_active(struct tcpcb *, tt_which);
1476 void	 tcp_timer_stop(struct tcpcb *);
1477 int	 inp_to_cpuid(struct inpcb *inp);
1478 /*
1479  * All tcp_hc_* functions are IPv4 and IPv6 (via in_conninfo)
1480  */
1481 void	 tcp_hc_init(void);
1482 #ifdef VIMAGE
1483 void	 tcp_hc_destroy(void);
1484 #endif
1485 void	 tcp_hc_get(const struct in_conninfo *, struct hc_metrics_lite *);
1486 uint32_t tcp_hc_getmtu(const struct in_conninfo *);
1487 void	 tcp_hc_updatemtu(const struct in_conninfo *, uint32_t);
1488 void	 tcp_hc_update(const struct in_conninfo *, struct hc_metrics_lite *);
1489 void 	 cc_after_idle(struct tcpcb *tp);
1490 
1491 extern	struct protosw tcp_protosw;		/* shared for TOE */
1492 extern	struct protosw tcp6_protosw;		/* shared for TOE */
1493 
1494 uint32_t tcp_new_ts_offset(struct in_conninfo *);
1495 tcp_seq	 tcp_new_isn(struct in_conninfo *);
1496 
1497 sackstatus_t
1498 	 tcp_sack_doack(struct tcpcb *, struct tcpopt *, tcp_seq);
1499 int	 tcp_dsack_block_exists(struct tcpcb *);
1500 void	 tcp_update_dsack_list(struct tcpcb *, tcp_seq, tcp_seq);
1501 void	 tcp_update_sack_list(struct tcpcb *tp, tcp_seq rcv_laststart,
1502 	    tcp_seq rcv_lastend);
1503 void	 tcp_clean_dsack_blocks(struct tcpcb *tp);
1504 void	 tcp_clean_sackreport(struct tcpcb *tp);
1505 int	 tcp_sack_adjust(struct tcpcb *tp);
1506 struct sackhole *tcp_sack_output(struct tcpcb *tp, int *sack_bytes_rexmt);
1507 void	 tcp_do_prr_ack(struct tcpcb *, struct tcphdr *, struct tcpopt *,
1508 	    sackstatus_t, u_int *);
1509 void	 tcp_lost_retransmission(struct tcpcb *, struct tcphdr *);
1510 void	 tcp_sack_partialack(struct tcpcb *, struct tcphdr *, u_int *);
1511 void	 tcp_resend_sackholes(struct tcpcb *tp);
1512 void	 tcp_free_sackholes(struct tcpcb *tp);
1513 void	 tcp_sack_lost_retransmission(struct tcpcb *, struct tcphdr *);
1514 int	 tcp_newreno(struct tcpcb *, struct tcphdr *);
1515 int	 tcp_compute_pipe(struct tcpcb *);
1516 uint32_t tcp_compute_initwnd(uint32_t);
1517 void	 tcp_sndbuf_autoscale(struct tcpcb *, struct socket *, uint32_t);
1518 int	 tcp_stats_sample_rollthedice(struct tcpcb *tp, void *seed_bytes,
1519     size_t seed_len);
1520 int tcp_can_enable_pacing(void);
1521 int tcp_incr_dgp_pacing_cnt(void);
1522 void tcp_dec_dgp_pacing_cnt(void);
1523 void tcp_decrement_paced_conn(void);
1524 void tcp_change_time_units(struct tcpcb *, int);
1525 void tcp_handle_orphaned_packets(struct tcpcb *);
1526 
1527 struct mbuf *
1528 	 tcp_m_copym(struct mbuf *m, int32_t off0, int32_t *plen,
1529 	   int32_t seglimit, int32_t segsize, struct sockbuf *sb, bool hw_tls);
1530 
1531 int	tcp_stats_init(void);
1532 void tcp_log_end_status(struct tcpcb *tp, uint8_t status);
1533 #ifdef TCP_REQUEST_TRK
1534 void tcp_req_free_a_slot(struct tcpcb *tp, struct tcp_sendfile_track *ent);
1535 struct tcp_sendfile_track *
1536 tcp_req_find_a_req_that_is_completed_by(struct tcpcb *tp, tcp_seq th_ack, int *ip);
1537 int tcp_req_check_for_comp(struct tcpcb *tp, tcp_seq ack_point);
1538 int
1539 tcp_req_is_entry_comp(struct tcpcb *tp, struct tcp_sendfile_track *ent, tcp_seq ack_point);
1540 struct tcp_sendfile_track *
1541 tcp_req_find_req_for_seq(struct tcpcb *tp, tcp_seq seq);
1542 void
1543 tcp_req_log_req_info(struct tcpcb *tp,
1544     struct tcp_sendfile_track *req, uint16_t slot,
1545     uint8_t val, uint64_t offset, uint64_t nbytes);
1546 
1547 uint32_t
1548 tcp_estimate_tls_overhead(struct socket *so, uint64_t tls_usr_bytes);
1549 void
1550 tcp_req_alloc_req(struct tcpcb *tp, union tcp_log_userdata *user,
1551     uint64_t ts);
1552 
1553 struct tcp_sendfile_track *
1554 tcp_req_alloc_req_full(struct tcpcb *tp, struct tcp_snd_req *req, uint64_t ts, int rec_dups);
1555 
1556 
1557 #endif
1558 #ifdef TCP_ACCOUNTING
1559 int tcp_do_ack_accounting(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to, uint32_t tiwin, int mss);
1560 #endif
1561 
1562 static inline void
tcp_lro_features_off(struct tcpcb * tp)1563 tcp_lro_features_off(struct tcpcb *tp)
1564 {
1565 	tp->t_flags2 &= ~(TF2_SUPPORTS_MBUFQ|
1566 	    TF2_MBUF_QUEUE_READY|
1567 	    TF2_DONT_SACK_QUEUE|
1568 	    TF2_MBUF_ACKCMP|
1569 	    TF2_MBUF_L_ACKS);
1570 }
1571 
1572 static inline void
tcp_fields_to_host(struct tcphdr * th)1573 tcp_fields_to_host(struct tcphdr *th)
1574 {
1575 
1576 	th->th_seq = ntohl(th->th_seq);
1577 	th->th_ack = ntohl(th->th_ack);
1578 	th->th_win = ntohs(th->th_win);
1579 	th->th_urp = ntohs(th->th_urp);
1580 }
1581 
1582 static inline void
tcp_fields_to_net(struct tcphdr * th)1583 tcp_fields_to_net(struct tcphdr *th)
1584 {
1585 
1586 	th->th_seq = htonl(th->th_seq);
1587 	th->th_ack = htonl(th->th_ack);
1588 	th->th_win = htons(th->th_win);
1589 	th->th_urp = htons(th->th_urp);
1590 }
1591 #endif /* _KERNEL */
1592 
1593 #endif /* _NETINET_TCP_VAR_H_ */
1594