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