1 /*- 2 * Copyright (c) 2016-2018 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 26 #ifndef __tcp_hpts_h__ 27 #define __tcp_hpts_h__ 28 29 /* Number of useconds represented by an hpts slot */ 30 #define HPTS_USECS_PER_SLOT 10 31 #define HPTS_USEC_IN_SEC 1000000 32 #define HPTS_MSEC_IN_SEC 1000 33 #define HPTS_USEC_IN_MSEC 1000 34 35 /* 36 * The following functions should also be available 37 * to userspace as well. 38 */ 39 static inline uint32_t 40 tcp_tv_to_usec(const struct timeval *sv) 41 { 42 return ((uint32_t) ((sv->tv_sec * HPTS_USEC_IN_SEC) + sv->tv_usec)); 43 } 44 45 static inline uint32_t 46 tcp_tv_to_msec(const struct timeval *sv) 47 { 48 return ((uint32_t) ((sv->tv_sec * HPTS_MSEC_IN_SEC) + (sv->tv_usec/HPTS_USEC_IN_MSEC))); 49 } 50 51 static inline uint64_t 52 tcp_tv_to_lusec(const struct timeval *sv) 53 { 54 return ((uint64_t)((sv->tv_sec * HPTS_USEC_IN_SEC) + sv->tv_usec)); 55 } 56 57 static inline uint64_t 58 tcp_tv_to_lusectick(const struct timeval *sv) 59 { 60 return ((uint64_t)((sv->tv_sec * HPTS_USEC_IN_SEC) + sv->tv_usec)); 61 } 62 63 64 struct hpts_diag { 65 uint32_t p_hpts_active; /* bbr->flex7 x */ 66 uint32_t p_nxt_slot; /* bbr->flex1 x */ 67 uint32_t p_cur_slot; /* bbr->flex2 x */ 68 uint32_t p_prev_slot; /* bbr->delivered */ 69 uint32_t p_runningslot; /* bbr->inflight */ 70 uint32_t slot_req; /* bbr->flex3 x */ 71 uint32_t inp_hptsslot; /* bbr->flex4 x */ 72 uint32_t time_remaining; /* bbr->flex5 x */ 73 uint32_t have_slept; /* bbr->epoch x */ 74 uint32_t hpts_sleep_time; /* bbr->applimited x */ 75 uint32_t yet_to_sleep; /* bbr->lt_epoch x */ 76 uint32_t need_new_to; /* bbr->flex6 x */ 77 uint32_t wheel_slot; /* bbr->bw_inuse x */ 78 uint32_t maxslots; /* bbr->delRate x */ 79 uint32_t wheel_cts; /* bbr->rttProp x */ 80 int32_t co_ret; /* bbr->pkts_out x */ 81 uint8_t p_on_min_sleep; /* bbr->flex8 x */ 82 }; 83 84 /* Magic flags to tell whats cooking on the pacing wheel */ 85 #define PACE_TMR_DELACK 0x01 /* Delayed ack timer running */ 86 #define PACE_TMR_RACK 0x02 /* RACK timer running */ 87 #define PACE_TMR_TLP 0x04 /* TLP timer running */ 88 #define PACE_TMR_RXT 0x08 /* Retransmit timer running */ 89 #define PACE_TMR_PERSIT 0x10 /* Persists timer running */ 90 #define PACE_TMR_KEEP 0x20 /* Keep alive timer running */ 91 #define PACE_PKT_OUTPUT 0x40 /* Output Packets being paced */ 92 #define PACE_TMR_MASK (PACE_TMR_KEEP|PACE_TMR_PERSIT|PACE_TMR_RXT|PACE_TMR_TLP|PACE_TMR_RACK|PACE_TMR_DELACK) 93 94 #ifdef _KERNEL 95 96 extern struct tcp_hptsi *tcp_hptsi_pace; 97 98 /* 99 * The following are the definitions for the kernel HPTS interface for managing 100 * the HPTS ring and the TCBs on it. 101 */ 102 103 void __tcp_hpts_init(struct tcp_hptsi *pace, struct tcpcb *); 104 #define tcp_hpts_init(tp) __tcp_hpts_init(tcp_hptsi_pace, tp) 105 106 void __tcp_hpts_remove(struct tcp_hptsi *pace, struct tcpcb *); 107 #define tcp_hpts_remove(tp) __tcp_hpts_remove(tcp_hptsi_pace, tp) 108 109 static inline bool 110 tcp_in_hpts(struct tcpcb *tp) 111 { 112 return ((tp->t_in_hpts == IHPTS_ONQUEUE) || 113 ((tp->t_in_hpts == IHPTS_MOVING) && 114 (tp->t_hpts_slot != -1))); 115 } 116 117 /* 118 * To insert a TCB on the hpts you *must* be holding the 119 * INP_WLOCK(). The hpts insert code will then acqurire 120 * the hpts's lock and insert the TCB on the requested 121 * slot possibly waking up the hpts if you are requesting 122 * a time earlier than what the hpts is sleeping to (if 123 * the hpts is sleeping). You may check the inp->inp_in_hpts 124 * flag without the hpts lock. The hpts is the only one 125 * that will clear this flag holding only the hpts lock. This 126 * means that in your tcp_output() routine when you test for 127 * it to be 1 (so you wont call output) it may be transitioning 128 * to 0 (by the hpts). That will be fine since that will just 129 * mean an extra call to tcp_output that most likely will find 130 * the call you executed (when the mis-match occurred) will have 131 * put the TCB back on the hpts and it will return. If your 132 * call did not add it back to the hpts then you will either 133 * over-send or the cwnd will block you from sending more. 134 * 135 * Note you should also be holding the INP_WLOCK() when you 136 * call the remove from the hpts as well. Thoug usually 137 * you are either doing this from a timer, where you need 138 * that INP_WLOCK() or from destroying your TCB where again 139 * you should already have the INP_WLOCK(). 140 */ 141 void __tcp_hpts_insert(struct tcp_hptsi *pace, struct tcpcb *tp, uint32_t usecs, 142 struct hpts_diag *diag); 143 #define tcp_hpts_insert(tp, usecs, diag) \ 144 __tcp_hpts_insert(tcp_hptsi_pace, (tp), (usecs), (diag)) 145 146 void __tcp_set_hpts(struct tcp_hptsi *pace, struct tcpcb *tp); 147 #define tcp_set_hpts(tp) __tcp_set_hpts(tcp_hptsi_pace, tp) 148 149 extern int32_t tcp_min_hptsi_time; 150 151 static inline int32_t 152 get_hpts_min_sleep_time(void) 153 { 154 return (tcp_min_hptsi_time + HPTS_USECS_PER_SLOT); 155 } 156 157 static inline uint64_t 158 tcp_get_u64_usecs(struct timeval *tv) 159 { 160 struct timeval tvd; 161 162 if (tv == NULL) 163 tv = &tvd; 164 microuptime(tv); 165 return (tcp_tv_to_lusec(tv)); 166 } 167 168 static inline uint32_t 169 tcp_get_usecs(struct timeval *tv) 170 { 171 struct timeval tvd; 172 173 if (tv == NULL) 174 tv = &tvd; 175 microuptime(tv); 176 return (tcp_tv_to_usec(tv)); 177 } 178 179 #endif /* _KERNEL */ 180 #endif /* __tcp_hpts_h__ */ 181