xref: /freebsd-src/sys/netinet/tcp_hpts.h (revision 638b5ae1c7858373344bc7b9dcb5a1e7fab80bd9)
13ee9c3c4SRandall Stewart /*-
252467047SWarner Losh  * Copyright (c) 2016-2018 Netflix, Inc.
33ee9c3c4SRandall Stewart  *
43ee9c3c4SRandall Stewart  * Redistribution and use in source and binary forms, with or without
53ee9c3c4SRandall Stewart  * modification, are permitted provided that the following conditions
63ee9c3c4SRandall Stewart  * are met:
73ee9c3c4SRandall Stewart  * 1. Redistributions of source code must retain the above copyright
83ee9c3c4SRandall Stewart  *    notice, this list of conditions and the following disclaimer.
93ee9c3c4SRandall Stewart  * 2. Redistributions in binary form must reproduce the above copyright
103ee9c3c4SRandall Stewart  *    notice, this list of conditions and the following disclaimer in the
113ee9c3c4SRandall Stewart  *    documentation and/or other materials provided with the distribution.
123ee9c3c4SRandall Stewart  *
133ee9c3c4SRandall Stewart  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
143ee9c3c4SRandall Stewart  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
153ee9c3c4SRandall Stewart  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
163ee9c3c4SRandall Stewart  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
173ee9c3c4SRandall Stewart  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
183ee9c3c4SRandall Stewart  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
193ee9c3c4SRandall Stewart  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
203ee9c3c4SRandall Stewart  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
213ee9c3c4SRandall Stewart  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
223ee9c3c4SRandall Stewart  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
233ee9c3c4SRandall Stewart  * SUCH DAMAGE.
243ee9c3c4SRandall Stewart  */
253ee9c3c4SRandall Stewart 
26603bbd06SWarner Losh #ifndef __tcp_hpts_h__
27603bbd06SWarner Losh #define __tcp_hpts_h__
28603bbd06SWarner Losh 
293ee9c3c4SRandall Stewart /* Number of useconds in a hpts tick */
30d7955cc0SRandall Stewart #define HPTS_TICKS_PER_SLOT 10
313b0b41e6SRandall Stewart #define HPTS_MS_TO_SLOTS(x) ((x * 100) + 1)
323ee9c3c4SRandall Stewart #define HPTS_USEC_TO_SLOTS(x) ((x+9) /10)
333ee9c3c4SRandall Stewart #define HPTS_USEC_IN_SEC 1000000
343ee9c3c4SRandall Stewart #define HPTS_MSEC_IN_SEC 1000
353ee9c3c4SRandall Stewart #define HPTS_USEC_IN_MSEC 1000
363ee9c3c4SRandall Stewart 
373ee9c3c4SRandall Stewart struct hpts_diag {
383b0b41e6SRandall Stewart 	uint32_t p_hpts_active; 	/* bbr->flex7 x */
393b0b41e6SRandall Stewart 	uint32_t p_nxt_slot;		/* bbr->flex1 x */
403b0b41e6SRandall Stewart 	uint32_t p_cur_slot;		/* bbr->flex2 x */
413b0b41e6SRandall Stewart 	uint32_t p_prev_slot;		/* bbr->delivered */
42d7955cc0SRandall Stewart 	uint32_t p_runningslot;		/* bbr->inflight */
433b0b41e6SRandall Stewart 	uint32_t slot_req;		/* bbr->flex3 x */
443b0b41e6SRandall Stewart 	uint32_t inp_hptsslot;		/* bbr->flex4 x */
453b0b41e6SRandall Stewart 	uint32_t slot_remaining;	/* bbr->flex5 x */
463b0b41e6SRandall Stewart 	uint32_t have_slept;		/* bbr->epoch x */
473b0b41e6SRandall Stewart 	uint32_t hpts_sleep_time;	/* bbr->applimited x */
483b0b41e6SRandall Stewart 	uint32_t yet_to_sleep;		/* bbr->lt_epoch x */
493b0b41e6SRandall Stewart 	uint32_t need_new_to;		/* bbr->flex6 x  */
50d7955cc0SRandall Stewart 	uint32_t wheel_slot;		/* bbr->bw_inuse x */
51d7955cc0SRandall Stewart 	uint32_t maxslots;		/* bbr->delRate x */
523b0b41e6SRandall Stewart 	uint32_t wheel_cts;		/* bbr->rttProp x */
533b0b41e6SRandall Stewart 	int32_t co_ret; 		/* bbr->pkts_out x */
543b0b41e6SRandall Stewart 	uint32_t p_curtick;		/* upper bbr->cur_del_rate */
553b0b41e6SRandall Stewart 	uint32_t p_lasttick;		/* lower bbr->cur_del_rate */
563b0b41e6SRandall Stewart 	uint8_t p_on_min_sleep; 	/* bbr->flex8 x */
573ee9c3c4SRandall Stewart };
583ee9c3c4SRandall Stewart 
593b0b41e6SRandall Stewart /* Magic flags to tell whats cooking on the pacing wheel */
603b0b41e6SRandall Stewart #define PACE_TMR_DELACK 0x01	/* Delayed ack timer running */
613b0b41e6SRandall Stewart #define PACE_TMR_RACK   0x02	/* RACK timer running */
623b0b41e6SRandall Stewart #define PACE_TMR_TLP    0x04	/* TLP timer running */
633b0b41e6SRandall Stewart #define PACE_TMR_RXT    0x08	/* Retransmit timer running */
643b0b41e6SRandall Stewart #define PACE_TMR_PERSIT 0x10	/* Persists timer running */
653b0b41e6SRandall Stewart #define PACE_TMR_KEEP   0x20	/* Keep alive timer running */
663b0b41e6SRandall Stewart #define PACE_PKT_OUTPUT 0x40	/* Output Packets being paced */
673b0b41e6SRandall Stewart #define PACE_TMR_MASK   (PACE_TMR_KEEP|PACE_TMR_PERSIT|PACE_TMR_RXT|PACE_TMR_TLP|PACE_TMR_RACK|PACE_TMR_DELACK)
683b0b41e6SRandall Stewart 
69d7955cc0SRandall Stewart #define DEFAULT_CONNECTION_THESHOLD 100
70d7955cc0SRandall Stewart 
713ee9c3c4SRandall Stewart /*
723ee9c3c4SRandall Stewart  * When using the hpts, a TCP stack must make sure
733ee9c3c4SRandall Stewart  * that once a INP_DROPPED flag is applied to a INP
743ee9c3c4SRandall Stewart  * that it does not expect tcp_output() to ever be
753ee9c3c4SRandall Stewart  * called by the hpts. The hpts will *not* call
763ee9c3c4SRandall Stewart  * any output (or input) functions on a TCB that
773ee9c3c4SRandall Stewart  * is in the DROPPED state.
783ee9c3c4SRandall Stewart  *
793ee9c3c4SRandall Stewart  * This implies final ACK's and RST's that might
803ee9c3c4SRandall Stewart  * be sent when a TCB is still around must be
813ee9c3c4SRandall Stewart  * sent from a routine like tcp_respond().
823ee9c3c4SRandall Stewart  */
83d7955cc0SRandall Stewart #define LOWEST_SLEEP_ALLOWED 50
843ee9c3c4SRandall Stewart #define DEFAULT_MIN_SLEEP 250	/* How many usec's is default for hpts sleep
853ee9c3c4SRandall Stewart 				 * this determines min granularity of the
86d7955cc0SRandall Stewart 				 * hpts. If 1, granularity is 10useconds at
87d7955cc0SRandall Stewart 				 * the cost of more CPU (context switching).
88d7955cc0SRandall Stewart 				 * Note do not set this to 0.
89d7955cc0SRandall Stewart 				 */
90d7955cc0SRandall Stewart #define DYNAMIC_MIN_SLEEP DEFAULT_MIN_SLEEP
916e6439b2SRandall Stewart #define DYNAMIC_MAX_SLEEP 5000	/* 5ms */
926e6439b2SRandall Stewart 
93d7955cc0SRandall Stewart /* Thresholds for raising/lowering sleep */
94d7955cc0SRandall Stewart #define TICKS_INDICATE_MORE_SLEEP 100		/* This would be 1ms */
95d7955cc0SRandall Stewart #define TICKS_INDICATE_LESS_SLEEP 1000		/* This would indicate 10ms */
96d7955cc0SRandall Stewart /**
97d7955cc0SRandall Stewart  *
98d7955cc0SRandall Stewart  * Dynamic adjustment of sleeping times is done in "new" mode
99d7955cc0SRandall Stewart  * where we are depending on syscall returns and lro returns
100d7955cc0SRandall Stewart  * to push hpts forward mainly and the timer is only a backstop.
101d7955cc0SRandall Stewart  *
102d7955cc0SRandall Stewart  * When we are in the "new" mode i.e. conn_cnt > conn_cnt_thresh
103d7955cc0SRandall Stewart  * then we do a dynamic adjustment on the time we sleep.
104d7955cc0SRandall Stewart  * Our threshold is if the lateness of the first client served (in ticks) is
105d7955cc0SRandall Stewart  * greater than or equal too ticks_indicate_more_sleep (10ms
106d7955cc0SRandall Stewart  * or 10000 ticks). If we were that late, the actual sleep time
107d7955cc0SRandall Stewart  * is adjusted down by 50%. If the ticks_ran is less than
108d7955cc0SRandall Stewart  * ticks_indicate_more_sleep (100 ticks or 1000usecs).
109d7955cc0SRandall Stewart  *
110d7955cc0SRandall Stewart  */
111d7955cc0SRandall Stewart 
1123ee9c3c4SRandall Stewart #ifdef _KERNEL
113c2a69e84SGleb Smirnoff void tcp_hpts_init(struct tcpcb *);
114c2a69e84SGleb Smirnoff void tcp_hpts_remove(struct tcpcb *);
115f2e75b96SGleb Smirnoff static inline bool
tcp_in_hpts(struct tcpcb * tp)116c2a69e84SGleb Smirnoff tcp_in_hpts(struct tcpcb *tp)
117c2a69e84SGleb Smirnoff {
118*638b5ae1SRandall Stewart 	return ((tp->t_in_hpts == IHPTS_ONQUEUE) ||
119*638b5ae1SRandall Stewart 		((tp->t_in_hpts == IHPTS_MOVING) &&
120*638b5ae1SRandall Stewart 		 (tp->t_hpts_slot != -1)));
121c2a69e84SGleb Smirnoff }
12250f081ecSGleb Smirnoff 
1233ee9c3c4SRandall Stewart /*
1243ee9c3c4SRandall Stewart  * To insert a TCB on the hpts you *must* be holding the
1253ee9c3c4SRandall Stewart  * INP_WLOCK(). The hpts insert code will then acqurire
1263ee9c3c4SRandall Stewart  * the hpts's lock and insert the TCB on the requested
1273ee9c3c4SRandall Stewart  * slot possibly waking up the hpts if you are requesting
1283ee9c3c4SRandall Stewart  * a time earlier than what the hpts is sleeping to (if
1293ee9c3c4SRandall Stewart  * the hpts is sleeping). You may check the inp->inp_in_hpts
1303ee9c3c4SRandall Stewart  * flag without the hpts lock. The hpts is the only one
1313ee9c3c4SRandall Stewart  * that will clear this flag holding only the hpts lock. This
1323ee9c3c4SRandall Stewart  * means that in your tcp_output() routine when you test for
1333ee9c3c4SRandall Stewart  * it to be 1 (so you wont call output) it may be transitioning
1343ee9c3c4SRandall Stewart  * to 0 (by the hpts). That will be fine since that will just
1353ee9c3c4SRandall Stewart  * mean an extra call to tcp_output that most likely will find
136d07a5018SGordon Bergling  * the call you executed (when the mis-match occurred) will have
1373ee9c3c4SRandall Stewart  * put the TCB back on the hpts and it will return. If your
1383ee9c3c4SRandall Stewart  * call did not add it back to the hpts then you will either
1393ee9c3c4SRandall Stewart  * over-send or the cwnd will block you from sending more.
1403ee9c3c4SRandall Stewart  *
1413ee9c3c4SRandall Stewart  * Note you should also be holding the INP_WLOCK() when you
1423ee9c3c4SRandall Stewart  * call the remove from the hpts as well. Thoug usually
1433ee9c3c4SRandall Stewart  * you are either doing this from a timer, where you need
1443ee9c3c4SRandall Stewart  * that INP_WLOCK() or from destroying your TCB where again
1453ee9c3c4SRandall Stewart  * you should already have the INP_WLOCK().
1463ee9c3c4SRandall Stewart  */
147c2a69e84SGleb Smirnoff uint32_t tcp_hpts_insert_diag(struct tcpcb *tp, uint32_t slot, int32_t line,
148db0ac6deSCy Schubert     struct hpts_diag *diag);
149db0ac6deSCy Schubert #define	tcp_hpts_insert(inp, slot)	\
150db0ac6deSCy Schubert 	tcp_hpts_insert_diag((inp), (slot), __LINE__, NULL)
1513ee9c3c4SRandall Stewart 
152c2a69e84SGleb Smirnoff void __tcp_set_hpts(struct tcpcb *tp, int32_t line);
1533ee9c3c4SRandall Stewart #define tcp_set_hpts(a) __tcp_set_hpts(a, __LINE__)
1543ee9c3c4SRandall Stewart 
155db0ac6deSCy Schubert void tcp_set_inp_to_drop(struct inpcb *inp, uint16_t reason);
1563ee9c3c4SRandall Stewart 
1572c6fc36aSGleb Smirnoff void tcp_lro_hpts_init(void);
15848b55a7cSGleb Smirnoff void tcp_lro_hpts_uninit(void);
159d7955cc0SRandall Stewart 
1603ee9c3c4SRandall Stewart extern int32_t tcp_min_hptsi_time;
1613ee9c3c4SRandall Stewart 
162d7955cc0SRandall Stewart #endif /* _KERNEL */
163d7955cc0SRandall Stewart 
164d7955cc0SRandall Stewart /*
165d7955cc0SRandall Stewart  * The following functions should also be available
166d7955cc0SRandall Stewart  * to userspace as well.
167d7955cc0SRandall Stewart  */
1683ee9c3c4SRandall Stewart static __inline uint32_t
tcp_tv_to_hptstick(const struct timeval * sv)169d7955cc0SRandall Stewart tcp_tv_to_hptstick(const struct timeval *sv)
1703ee9c3c4SRandall Stewart {
171d7955cc0SRandall Stewart 	return ((sv->tv_sec * 100000) + (sv->tv_usec / HPTS_TICKS_PER_SLOT));
172d7955cc0SRandall Stewart }
173d7955cc0SRandall Stewart 
174d7955cc0SRandall Stewart static __inline uint32_t
tcp_tv_to_usectick(const struct timeval * sv)175d7955cc0SRandall Stewart tcp_tv_to_usectick(const struct timeval *sv)
176d7955cc0SRandall Stewart {
177d7955cc0SRandall Stewart 	return ((uint32_t) ((sv->tv_sec * HPTS_USEC_IN_SEC) + sv->tv_usec));
178d7955cc0SRandall Stewart }
179d7955cc0SRandall Stewart 
180d7955cc0SRandall Stewart static __inline uint32_t
tcp_tv_to_mssectick(const struct timeval * sv)181d7955cc0SRandall Stewart tcp_tv_to_mssectick(const struct timeval *sv)
182d7955cc0SRandall Stewart {
183d7955cc0SRandall Stewart 	return ((uint32_t) ((sv->tv_sec * HPTS_MSEC_IN_SEC) + (sv->tv_usec/HPTS_USEC_IN_MSEC)));
184d7955cc0SRandall Stewart }
185d7955cc0SRandall Stewart 
186d7955cc0SRandall Stewart static __inline uint64_t
tcp_tv_to_lusectick(const struct timeval * sv)187d7955cc0SRandall Stewart tcp_tv_to_lusectick(const struct timeval *sv)
188d7955cc0SRandall Stewart {
189d7955cc0SRandall Stewart 	return ((uint64_t)((sv->tv_sec * HPTS_USEC_IN_SEC) + sv->tv_usec));
190d7955cc0SRandall Stewart }
191d7955cc0SRandall Stewart 
192d7955cc0SRandall Stewart #ifdef _KERNEL
19373ee5756SRandall Stewart 
19473ee5756SRandall Stewart extern int32_t tcp_min_hptsi_time;
19573ee5756SRandall Stewart 
1966eb2dbfaSGleb Smirnoff static inline int32_t
get_hpts_min_sleep_time(void)197c67eb393SMateusz Guzik get_hpts_min_sleep_time(void)
19873ee5756SRandall Stewart {
19973ee5756SRandall Stewart 	return (tcp_min_hptsi_time + HPTS_TICKS_PER_SLOT);
20073ee5756SRandall Stewart }
20173ee5756SRandall Stewart 
2023ee9c3c4SRandall Stewart static __inline uint32_t
tcp_gethptstick(struct timeval * sv)2033ee9c3c4SRandall Stewart tcp_gethptstick(struct timeval *sv)
2043ee9c3c4SRandall Stewart {
2053ee9c3c4SRandall Stewart 	struct timeval tv;
2063ee9c3c4SRandall Stewart 
2073ee9c3c4SRandall Stewart 	if (sv == NULL)
2083ee9c3c4SRandall Stewart 		sv = &tv;
2093ee9c3c4SRandall Stewart 	microuptime(sv);
2103ee9c3c4SRandall Stewart 	return (tcp_tv_to_hptstick(sv));
2113ee9c3c4SRandall Stewart }
2123ee9c3c4SRandall Stewart 
213*638b5ae1SRandall Stewart static __inline uint64_t
tcp_get_u64_usecs(struct timeval * tv)214*638b5ae1SRandall Stewart tcp_get_u64_usecs(struct timeval *tv)
215*638b5ae1SRandall Stewart {
216*638b5ae1SRandall Stewart 	struct timeval tvd;
217*638b5ae1SRandall Stewart 
218*638b5ae1SRandall Stewart 	if (tv == NULL)
219*638b5ae1SRandall Stewart 		tv = &tvd;
220*638b5ae1SRandall Stewart 	microuptime(tv);
221*638b5ae1SRandall Stewart 	return (tcp_tv_to_lusectick(tv));
222*638b5ae1SRandall Stewart }
223*638b5ae1SRandall Stewart 
2243ee9c3c4SRandall Stewart static __inline uint32_t
tcp_get_usecs(struct timeval * tv)2253ee9c3c4SRandall Stewart tcp_get_usecs(struct timeval *tv)
2263ee9c3c4SRandall Stewart {
2273ee9c3c4SRandall Stewart 	struct timeval tvd;
2283ee9c3c4SRandall Stewart 
2293ee9c3c4SRandall Stewart 	if (tv == NULL)
2303ee9c3c4SRandall Stewart 		tv = &tvd;
2313ee9c3c4SRandall Stewart 	microuptime(tv);
2323ee9c3c4SRandall Stewart 	return (tcp_tv_to_usectick(tv));
2333ee9c3c4SRandall Stewart }
2343ee9c3c4SRandall Stewart 
235603bbd06SWarner Losh #endif /* _KERNEL */
236603bbd06SWarner Losh #endif /* __tcp_hpts_h__ */
237