xref: /freebsd/sys/netinet/tcp_stacks/tcp_rack.h (revision b9f654b163bce26de79705e77b872427c9f2afa1)
1 /*-
2  * Copyright (c) 2016 Netflix, Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD$
26  */
27 
28 #ifndef _NETINET_TCP_RACK_H_
29 #define _NETINET_TCP_RACK_H_
30 
31 #define RACK_ACKED	  0x0001/* The remote endpoint acked this */
32 #define RACK_TO_MIXED	  0x0002/* A timeout occured that mixed the send order */
33 #define RACK_DEFERRED	  0x0004/* We can't use this for RTT calc */
34 #define RACK_OVERMAX	  0x0008/* We have more retran's then we can fit */
35 #define RACK_SACK_PASSED  0x0010/* A sack was done above this block */
36 #define RACK_WAS_SACKPASS 0x0020/* We retransmitted due to SACK pass */
37 #define RACK_HAS_FIN	  0x0040/* segment is sent with fin */
38 #define RACK_TLP	  0x0080/* segment sent as tail-loss-probe */
39 
40 #define RACK_NUM_OF_RETRANS 3
41 
42 #define RACK_INITIAL_RTO 1000 /* 1 second in milli seconds */
43 
44 struct rack_sendmap {
45 	TAILQ_ENTRY(rack_sendmap) r_next;	/* seq number arrayed next */
46 	TAILQ_ENTRY(rack_sendmap) r_tnext;	/* Time of transmit based next */
47 	uint32_t r_tim_lastsent[RACK_NUM_OF_RETRANS];
48 	uint32_t r_start;	/* Sequence number of the segment */
49 	uint32_t r_end;		/* End seq, this is 1 beyond actually */
50 	uint32_t r_rtr_bytes;	/* How many bytes have been retransmitted */
51 	uint16_t r_rtr_cnt;	/* Retran count, index this -1 to get time
52 				 * sent */
53 	uint8_t r_flags;	/* Flags as defined above */
54 	uint8_t r_sndcnt;	/* Retran count, not limited by
55 				 * RACK_NUM_OF_RETRANS */
56 	uint8_t r_in_tmap;	/* Flag to see if its in the r_tnext array */
57 	uint8_t r_resv[3];
58 };
59 
60 TAILQ_HEAD(rack_head, rack_sendmap);
61 
62 
63 /*
64  * We use the rate sample structure to
65  * assist in single sack/ack rate and rtt
66  * calculation. In the future we will expand
67  * this in BBR to do forward rate sample
68  * b/w estimation.
69  */
70 #define RACK_RTT_EMPTY 0x00000001	/* Nothing yet stored in RTT's */
71 #define RACK_RTT_VALID 0x00000002	/* We have at least one valid RTT */
72 struct rack_rtt_sample {
73 	uint32_t rs_flags;
74 	uint32_t rs_rtt_lowest;
75 	uint32_t rs_rtt_highest;
76 	uint32_t rs_rtt_cnt;
77 	uint64_t rs_rtt_tot;
78 };
79 
80 #define RACK_LOG_TYPE_ACK	0x01
81 #define RACK_LOG_TYPE_OUT	0x02
82 #define RACK_LOG_TYPE_TO	0x03
83 #define RACK_LOG_TYPE_ALLOC     0x04
84 #define RACK_LOG_TYPE_FREE      0x05
85 
86 
87 struct rack_log {
88 	union {
89 		struct rack_sendmap *rsm;	/* For alloc/free */
90 		uint64_t sb_acc;/* For out/ack or t-o */
91 	};
92 	uint32_t th_seq;
93 	uint32_t th_ack;
94 	uint32_t snd_una;
95 	uint32_t snd_nxt;	/* th_win for TYPE_ACK */
96 	uint32_t snd_max;
97 	uint32_t blk_start[4];
98 	uint32_t blk_end[4];
99 	uint8_t type;
100 	uint8_t n_sackblks;
101 	uint16_t len;		/* Timeout T3=1, TLP=2, RACK=3 */
102 };
103 
104 /*
105  * Magic numbers for logging timeout events if the
106  * logging is enabled.
107  */
108 #define RACK_TO_FRM_TMR  1
109 #define RACK_TO_FRM_TLP  2
110 #define RACK_TO_FRM_RACK 3
111 #define RACK_TO_FRM_KEEP 4
112 #define RACK_TO_FRM_PERSIST 5
113 #define RACK_TO_FRM_DELACK 6
114 
115 struct rack_opts_stats {
116 	uint64_t tcp_rack_prop_rate;
117  	uint64_t tcp_rack_prop;
118 	uint64_t tcp_rack_tlp_reduce;
119 	uint64_t tcp_rack_early_recov;
120 	uint64_t tcp_rack_pace_always;
121 	uint64_t tcp_rack_pace_reduce;
122 	uint64_t tcp_rack_max_seg;
123 	uint64_t tcp_rack_prr_sendalot;
124 	uint64_t tcp_rack_min_to;
125 	uint64_t tcp_rack_early_seg;
126 	uint64_t tcp_rack_reord_thresh;
127 	uint64_t tcp_rack_reord_fade;
128 	uint64_t tcp_rack_tlp_thresh;
129 	uint64_t tcp_rack_pkt_delay;
130 	uint64_t tcp_rack_tlp_inc_var;
131 	uint64_t tcp_tlp_use;
132 	uint64_t tcp_rack_idle_reduce;
133 	uint64_t tcp_rack_idle_reduce_high;
134 	uint64_t rack_no_timer_in_hpts;
135 	uint64_t tcp_rack_min_pace_seg;
136 	uint64_t tcp_rack_min_pace;
137 };
138 
139 #define TLP_USE_ID	1	/* Internet draft behavior */
140 #define TLP_USE_TWO_ONE 2	/* Use 2.1 behavior */
141 #define TLP_USE_TWO_TWO 3	/* Use 2.2 behavior */
142 
143 #ifdef _KERNEL
144 #define RACK_OPTS_SIZE (sizeof(struct rack_opts_stats)/sizeof(uint64_t))
145 extern counter_u64_t rack_opts_arry[RACK_OPTS_SIZE];
146 #define RACK_OPTS_ADD(name, amm) counter_u64_add(rack_opts_arry[(offsetof(struct rack_opts_stats, name)/sizeof(uint64_t))], (amm))
147 #define RACK_OPTS_INC(name) RACK_OPTS_ADD(name, 1)
148 #endif
149 /*
150  * As we get each SACK we wade through the
151  * rc_map and mark off what is acked.
152  * We also increment rc_sacked as well.
153  *
154  * We also pay attention to missing entries
155  * based on the time and possibly mark them
156  * for retransmit. If we do and we are not already
157  * in recovery we enter recovery. In doing
158  * so we claer prr_delivered/holes_rxt and prr_sent_dur_rec.
159  * We also setup rc_next/rc_snd_nxt/rc_send_end so
160  * we will know where to send from. When not in
161  * recovery rc_next will be NULL and rc_snd_nxt should
162  * equal snd_max.
163  *
164  * Whenever we retransmit from recovery we increment
165  * rc_holes_rxt as we retran a block and mark it as retransmitted
166  * with the time it was sent. During non-recovery sending we
167  * add to our map and note the time down of any send expanding
168  * the rc_map at the tail and moving rc_snd_nxt up with snd_max.
169  *
170  * In recovery during SACK/ACK processing if a chunk has
171  * been retransmitted and it is now acked, we decrement rc_holes_rxt.
172  * When we retransmit from the scoreboard we use
173  * rc_next and rc_snd_nxt/rc_send_end to help us
174  * find what needs to be retran.
175  *
176  * To calculate pipe we simply take (snd_max - snd_una) + rc_holes_rxt
177  * This gets us the effect of RFC6675 pipe, counting twice for
178  * bytes retransmitted.
179  */
180 
181 #define TT_RACK_FR_TMR	0x2000
182 
183 /*
184  * Locking for the rack control block.
185  * a) Locked by INP_WLOCK
186  * b) Locked by the hpts-mutex
187  *
188  */
189 
190 struct rack_control {
191 	/* Second cache line 0x40 from tcp_rack */
192 	struct rack_head rc_map;/* List of all segments Lock(a) */
193 	struct rack_head rc_tmap;	/* List in transmit order Lock(a) */
194 	struct rack_sendmap *rc_tlpsend;	/* Remembered place for
195 						 * tlp_sending Lock(a) */
196 	struct rack_sendmap *rc_resend;	/* something we have been asked to
197 					 * resend */
198 	uint32_t rc_hpts_flags;
199 	uint32_t rc_timer_exp;	/* If a timer ticks of expiry */
200 	uint32_t rc_rack_min_rtt;	/* lowest RTT seen Lock(a) */
201 	uint32_t rc_rack_largest_cwnd;	/* Largest CWND we have seen Lock(a) */
202 
203 	/* Third Cache line 0x80 */
204 	struct rack_head rc_free;	/* Allocation array */
205 	uint32_t rc_time_last_sent;	/* Time we last sent some data and
206 					 * logged it Lock(a). */
207 	uint32_t rc_reorder_ts;	/* Last time we saw reordering Lock(a) */
208 
209 	uint32_t rc_tlp_new_data;	/* we need to send new-data on a TLP
210 					 * Lock(a) */
211 	uint32_t rc_prr_out;	/* bytes sent during recovery Lock(a) */
212 
213 	uint32_t rc_prr_recovery_fs;	/* recovery fs point Lock(a) */
214 
215 	uint32_t rc_prr_sndcnt;	/* Prr sndcnt Lock(a) */
216 
217 	uint32_t rc_sacked;	/* Tot sacked on scoreboard Lock(a) */
218 	uint32_t rc_last_tlp_seq;	/* Last tlp sequence Lock(a) */
219 
220 	uint32_t rc_prr_delivered;	/* during recovery prr var Lock(a) */
221 	uint16_t rc_tlp_send_cnt;	/* Number of TLP sends we have done
222 					 * since peer spoke to us Lock(a) */
223 	uint16_t rc_tlp_seg_send_cnt;	/* Number of times we have TLP sent
224 					 * rc_last_tlp_seq Lock(a) */
225 
226 	uint32_t rc_loss_count;	/* During recovery how many segments were lost
227 				 * Lock(a) */
228 	uint32_t rc_reorder_fade;	/* Socket option value Lock(a) */
229 
230 	/* Forth cache line 0xc0  */
231 	/* Times */
232 
233 	uint32_t rc_rack_tmit_time;	/* Rack transmit time Lock(a) */
234 	uint32_t rc_holes_rxt;	/* Tot retraned from scoreboard Lock(a) */
235 
236 	/* Variables to track bad retransmits and recover */
237 	uint32_t rc_rsm_start;	/* RSM seq number we retransmitted Lock(a) */
238 	uint32_t rc_cwnd_at;	/* cwnd at the retransmit Lock(a) */
239 
240 	uint32_t rc_ssthresh_at;/* ssthresh at the retransmit Lock(a) */
241 	uint32_t rc_num_maps_alloced;	/* Number of map blocks (sacks) we
242 					 * have allocated */
243 	uint32_t rc_rcvtime;	/* When we last received data */
244 	uint32_t rc_notused;
245 	uint32_t rc_last_output_to;
246 	uint32_t rc_went_idle_time;
247 
248 	struct rack_sendmap *rc_sacklast;	/* sack remembered place
249 						 * Lock(a) */
250 
251 	struct rack_sendmap *rc_next;	/* remembered place where we next
252 					 * retransmit at Lock(a) */
253 	struct rack_sendmap *rc_rsm_at_retran;	/* Debug variable kept for
254 						 * cache line alignment
255 						 * Lock(a) */
256 	/* Cache line split 0x100 */
257 	struct sack_filter rack_sf;
258 	/* Cache line split 0x140 */
259 	/* Flags for various things */
260 	struct rack_rtt_sample rack_rs;
261 	uint32_t rc_tlp_threshold;	/* Socket option value Lock(a) */
262 	uint16_t rc_early_recovery_segs;	/* Socket option value Lock(a) */
263 	uint16_t rc_reorder_shift;	/* Socket option value Lock(a) */
264 	uint16_t rc_pkt_delay;	/* Socket option value Lock(a) */
265 	uint8_t rc_prop_rate;	/* Socket option value Lock(a) */
266 	uint8_t rc_prop_reduce;	/* Socket option value Lock(a) */
267 	uint8_t rc_tlp_cwnd_reduce;	/* Socket option value Lock(a) */
268 	uint8_t rc_early_recovery;	/* Socket option value Lock(a) */
269 	uint8_t rc_prr_sendalot;/* Socket option value Lock(a) */
270 	uint8_t rc_min_to;	/* Socket option value Lock(a) */
271 	uint8_t rc_prr_inc_var;	/* Socket option value Lock(a) */
272 	uint8_t rc_tlp_rtx_out;	/* This is TLPRtxOut in the draft */
273 	uint8_t rc_rate_sample_method;
274 };
275 
276 #ifdef _KERNEL
277 
278 struct tcp_rack {
279 	/* First cache line 0x00 */
280 	TAILQ_ENTRY(tcp_rack) r_hpts;	/* hptsi queue next Lock(b) */
281 	int32_t(*r_substate) (struct mbuf *, struct tcphdr *,
282 	    struct socket *, struct tcpcb *, struct tcpopt *,
283 	    int32_t, int32_t, uint32_t, int, int);	/* Lock(a) */
284 	struct tcpcb *rc_tp;	/* The tcpcb Lock(a) */
285 	struct inpcb *rc_inp;	/* The inpcb Lock(a) */
286 	uint32_t rc_free_cnt;	/* Number of free entries on the rc_free list
287 				 * Lock(a) */
288 	uint32_t rc_rack_rtt;	/* RACK-RTT Lock(a) */
289 	uint16_t r_wanted_output;	/* Output routine wanted to be called */
290 	uint16_t r_cpu;		/* CPU that the INP is running on Lock(a) */
291 	uint16_t rc_pace_max_segs;	/* Socket option value Lock(a) */
292 	uint16_t rc_pace_reduce;/* Socket option value Lock(a) */
293 
294 	uint8_t r_state;	/* Current rack state Lock(a) */
295 	uint8_t rc_tmr_stopped : 7,
296 		t_timers_stopped : 1;
297 	uint8_t rc_enobuf;	/* count of enobufs on connection provides
298 				 * backoff Lock(a) */
299 	uint8_t r_timer_override : 1,	/* hpts override Lock(a) */
300 		r_tlp_running : 1, 	/* Running from a TLP timeout Lock(a) */
301 		r_is_v6 : 1,	/* V6 pcb Lock(a)  */
302 		rc_in_persist : 1,
303 		rc_last_pto_set : 1, /* XXX not used */
304 		rc_tlp_in_progress : 1,
305 		rc_always_pace : 1,	/* Socket option value Lock(a) */
306 		rc_timer_up : 1;	/* The rack timer is up flag  Lock(a) */
307 	uint8_t r_idle_reduce_largest : 1,
308 		r_enforce_min_pace : 2,
309 		r_min_pace_seg_thresh : 5;
310 	uint8_t rack_tlp_threshold_use;
311 	uint8_t rc_allow_data_af_clo: 1,
312 		delayed_ack : 1,
313 		rc_avail : 6;
314 	uint8_t r_resv[2];	/* Fill to cache line boundary */
315 	/* Cache line 2 0x40 */
316 	struct rack_control r_ctl;
317 }        __aligned(CACHE_LINE_SIZE);
318 
319 #endif
320 #endif
321