2 * Copyright (c) 2016-2018 Netflix Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
30 #include "opt_inet6.h"
31 #include "opt_ipsec.h"
32 #include "opt_tcpdebug.h"
34 * Some notes about usage.
36 * The tcp_hpts system is designed to provide a high precision timer
37 * system for tcp. Its main purpose is to provide a mechanism for
38 * pacing packets out onto the wire. It can be used in two ways
39 * by a given TCP stack (and those two methods can be used simultaneously).
41 * First, and probably the main thing its used by Rack and BBR for, it can
42 * be used to call tcp_output() of a transport stack at some time in the future.
43 * The normal way this is done is that tcp_output() of the stack schedules
44 * itself to be called again by calling tcp_hpts_insert(tcpcb, slot). The
45 * slot is the time from now that the stack wants to be called but it
46 * must be converted to tcp_hpts's notion of slot. This is done with
47 * one of the macros HPTS_MS_TO_SLOTS or HPTS_USEC_TO_SLOTS. So a typical
48 * call from the tcp_output() routine might look like:
50 * tcp_hpts_insert(tp, HPTS_USEC_TO_SLOTS(550));
52 * The above would schedule tcp_ouput() to be called in 550 useconds.
53 * Note that if using this mechanism the stack will want to add near
54 * its top a check to prevent unwanted calls (from user land or the
55 * arrival of incoming ack's). So it would add something like:
57 * if (inp->inp_in_hpts)
60 * to prevent output processing until the time alotted has gone by.
61 * Of course this is a bare bones example and the stack will probably
62 * have more consideration then just the above.
64 * Now the tcp_hpts system will call tcp_output in one of two forms,
65 * it will first check to see if the stack as defined a
66 * tfb_tcp_output_wtime() function, if so that is the routine it
67 * will call, if that function is not defined then it will call the
68 * tfb_tcp_output() function. The only difference between these
69 * two calls is that the former passes the time in to the function
70 * so the function does not have to access the time (which tcp_hpts
71 * already has). What these functions do is of course totally up
72 * to the individual tcp stack.
74 * Now the second function (actually two functions I guess :D)
75 * the tcp_hpts system provides is the ability to either abort
76 * a connection (later) or process input on a connection.
77 * Why would you want to do this? To keep processor locality.
79 * So in order to use the input redirection function the
80 * stack changes its tcp_do_segment() routine to instead
81 * of process the data call the function:
83 * tcp_queue_pkt_to_input()
85 * You will note that the arguments to this function look
86 * a lot like tcp_do_segments's arguments. This function
87 * will assure that the tcp_hpts system will
88 * call the functions tfb_tcp_hpts_do_segment() from the
89 * correct CPU. Note that multiple calls can get pushed
90 * into the tcp_hpts system this will be indicated by
91 * the next to last argument to tfb_tcp_hpts_do_segment()
92 * (nxt_pkt). If nxt_pkt is a 1 then another packet is
93 * coming. If nxt_pkt is a 0 then this is the last call
94 * that the tcp_hpts system has available for the tcp stack.
96 * The other point of the input system is to be able to safely
97 * drop a tcp connection without worrying about the recursive
98 * locking that may be occuring on the INP_WLOCK. So if
99 * a stack wants to drop a connection it calls:
101 * tcp_set_inp_to_drop(tp, ETIMEDOUT)
103 * To schedule the tcp_hpts system to call
105 * tcp_drop(tp, drop_reason)
107 * at a future point. This is quite handy to prevent locking
108 * issues when dropping connections.
112 #include <sys/param.h>
114 #include <sys/interrupt.h>
115 #include <sys/module.h>
116 #include <sys/kernel.h>
117 #include <sys/hhook.h>
118 #include <sys/malloc.h>
119 #include <sys/mbuf.h>
120 #include <sys/proc.h> /* for proc0 declaration */
121 #include <sys/socket.h>
122 #include <sys/socketvar.h>
123 #include <sys/sysctl.h>
124 #include <sys/systm.h>
125 #include <sys/refcount.h>
126 #include <sys/sched.h>
127 #include <sys/queue.h>
129 #include <sys/counter.h>
130 #include <sys/time.h>
131 #include <sys/kthread.h>
132 #include <sys/kern_prefetch.h>
136 #include <net/route.h>
137 #include <net/vnet.h>
139 #define TCPSTATES /* for logging */
141 #include <netinet/in.h>
142 #include <netinet/in_kdtrace.h>
143 #include <netinet/in_pcb.h>
144 #include <netinet/ip.h>
145 #include <netinet/ip_icmp.h> /* required for icmp_var.h */
146 #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */
147 #include <netinet/ip_var.h>
148 #include <netinet/ip6.h>
149 #include <netinet6/in6_pcb.h>
150 #include <netinet6/ip6_var.h>
152 #include <netinet/tcp.h>
153 #include <netinet/tcp_fsm.h>
154 #include <netinet/tcp_seq.h>
155 #include <netinet/tcp_timer.h>
156 #include <netinet/tcp_var.h>
157 #include <netinet/tcpip.h>
158 #include <netinet/cc/cc.h>
159 #include <netinet/tcp_hpts.h>
162 #include <netinet/tcp_debug.h>
163 #endif /* tcpdebug */
165 #include <netinet/tcp_offload.h>
169 #include <netipsec/ipsec.h>
170 #include <netipsec/ipsec6.h>
174 MALLOC_DEFINE(M_TCPHPTS, "tcp_hpts", "TCP hpts");
176 static int tcp_bind_threads = 1;
178 static int tcp_bind_threads = 0;
180 TUNABLE_INT("net.inet.tcp.bind_hptss", &tcp_bind_threads);
182 static uint32_t tcp_hpts_logging_size = DEFAULT_HPTS_LOG;
184 TUNABLE_INT("net.inet.tcp.hpts_logging_sz", &tcp_hpts_logging_size);
186 static struct tcp_hptsi tcp_pace;
189 tcp_hptsi_lock_inpinfo(struct inpcb *inp,
191 static void tcp_wakehpts(struct tcp_hpts_entry *p);
192 static void tcp_wakeinput(struct tcp_hpts_entry *p);
193 static void tcp_input_data(struct tcp_hpts_entry *hpts, struct timeval *tv);
194 static void tcp_hptsi(struct tcp_hpts_entry *hpts, struct timeval *ctick);
195 static void tcp_hpts_thread(void *ctx);
196 static void tcp_init_hptsi(void *st);
198 int32_t tcp_min_hptsi_time = DEFAULT_MIN_SLEEP;
199 static int32_t tcp_hpts_callout_skip_swi = 0;
201 SYSCTL_DECL(_net_inet_tcp);
202 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, hpts, CTLFLAG_RW, 0, "TCP Hpts controls");
204 #define timersub(tvp, uvp, vvp) \
206 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
207 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
208 if ((vvp)->tv_usec < 0) { \
210 (vvp)->tv_usec += 1000000; \
214 static int32_t logging_on = 0;
215 static int32_t hpts_sleep_max = (NUM_OF_HPTSI_SLOTS - 2);
216 static int32_t tcp_hpts_precision = 120;
218 SYSCTL_INT(_net_inet_tcp_hpts, OID_AUTO, precision, CTLFLAG_RW,
219 &tcp_hpts_precision, 120,
220 "Value for PRE() precision of callout");
222 SYSCTL_INT(_net_inet_tcp_hpts, OID_AUTO, logging, CTLFLAG_RW,
224 "Turn on logging if compiled in");
226 counter_u64_t hpts_loops;
228 SYSCTL_COUNTER_U64(_net_inet_tcp_hpts, OID_AUTO, loops, CTLFLAG_RD,
229 &hpts_loops, "Number of times hpts had to loop to catch up");
231 counter_u64_t back_tosleep;
233 SYSCTL_COUNTER_U64(_net_inet_tcp_hpts, OID_AUTO, no_tcbsfound, CTLFLAG_RD,
234 &back_tosleep, "Number of times hpts found no tcbs");
236 static int32_t in_newts_every_tcb = 0;
238 SYSCTL_INT(_net_inet_tcp_hpts, OID_AUTO, in_tsperpcb, CTLFLAG_RW,
239 &in_newts_every_tcb, 0,
240 "Do we have a new cts every tcb we process for input");
241 static int32_t in_ts_percision = 0;
243 SYSCTL_INT(_net_inet_tcp_hpts, OID_AUTO, in_tspercision, CTLFLAG_RW,
245 "Do we use percise timestamp for clients on input");
246 static int32_t out_newts_every_tcb = 0;
248 SYSCTL_INT(_net_inet_tcp_hpts, OID_AUTO, out_tsperpcb, CTLFLAG_RW,
249 &out_newts_every_tcb, 0,
250 "Do we have a new cts every tcb we process for output");
251 static int32_t out_ts_percision = 0;
253 SYSCTL_INT(_net_inet_tcp_hpts, OID_AUTO, out_tspercision, CTLFLAG_RW,
254 &out_ts_percision, 0,
255 "Do we use a percise timestamp for every output cts");
257 SYSCTL_INT(_net_inet_tcp_hpts, OID_AUTO, maxsleep, CTLFLAG_RW,
259 "The maximum time the hpts will sleep <1 - 254>");
261 SYSCTL_INT(_net_inet_tcp_hpts, OID_AUTO, minsleep, CTLFLAG_RW,
262 &tcp_min_hptsi_time, 0,
263 "The minimum time the hpts must sleep before processing more slots");
265 SYSCTL_INT(_net_inet_tcp_hpts, OID_AUTO, skip_swi, CTLFLAG_RW,
266 &tcp_hpts_callout_skip_swi, 0,
267 "Do we have the callout call directly to the hpts?");
270 __tcp_hpts_log_it(struct tcp_hpts_entry *hpts, struct inpcb *inp, int event, uint32_t slot,
271 uint32_t ticknow, int32_t line)
275 HPTS_MTX_ASSERT(hpts);
276 if (hpts->p_log == NULL)
278 pl = &hpts->p_log[hpts->p_log_at];
280 if (hpts->p_log_at >= hpts->p_logsize) {
282 hpts->p_log_wrapped = 1;
286 pl->t_paceslot = inp->inp_hptsslot;
287 pl->t_hptsreq = inp->inp_hpts_request;
288 pl->p_onhpts = inp->inp_in_hpts;
289 pl->p_oninput = inp->inp_in_input;
299 pl->cts = tcp_get_usecs(NULL);
300 pl->p_curtick = hpts->p_curtick;
301 pl->p_prevtick = hpts->p_prevtick;
302 pl->p_on_queue_cnt = hpts->p_on_queue_cnt;
303 pl->ticknow = ticknow;
305 pl->p_nxt_slot = hpts->p_nxt_slot;
306 pl->p_cur_slot = hpts->p_cur_slot;
307 pl->p_hpts_sleep_time = hpts->p_hpts_sleep_time;
308 pl->p_flags = (hpts->p_cpu & 0x7f);
310 pl->p_flags |= (hpts->p_num & 0x7f);
312 if (hpts->p_hpts_active) {
313 pl->p_flags |= HPTS_HPTS_ACTIVE;
317 #define tcp_hpts_log_it(a, b, c, d, e) __tcp_hpts_log_it(a, b, c, d, e, __LINE__)
320 hpts_timeout_swi(void *arg)
322 struct tcp_hpts_entry *hpts;
324 hpts = (struct tcp_hpts_entry *)arg;
325 swi_sched(hpts->ie_cookie, 0);
329 hpts_timeout_dir(void *arg)
331 tcp_hpts_thread(arg);
335 hpts_sane_pace_remove(struct tcp_hpts_entry *hpts, struct inpcb *inp, struct hptsh *head, int clear)
338 if (mtx_owned(&hpts->p_mtx) == 0) {
339 /* We don't own the mutex? */
340 panic("%s: hpts:%p inp:%p no hpts mutex", __FUNCTION__, hpts, inp);
342 if (hpts->p_cpu != inp->inp_hpts_cpu) {
343 /* It is not the right cpu/mutex? */
344 panic("%s: hpts:%p inp:%p incorrect CPU", __FUNCTION__, hpts, inp);
346 if (inp->inp_in_hpts == 0) {
347 /* We are not on the hpts? */
348 panic("%s: hpts:%p inp:%p not on the hpts?", __FUNCTION__, hpts, inp);
350 if (TAILQ_EMPTY(head) &&
351 (hpts->p_on_queue_cnt != 0)) {
352 /* We should not be empty with a queue count */
353 panic("%s hpts:%p hpts bucket empty but cnt:%d",
354 __FUNCTION__, hpts, hpts->p_on_queue_cnt);
357 TAILQ_REMOVE(head, inp, inp_hpts);
358 hpts->p_on_queue_cnt--;
359 if (hpts->p_on_queue_cnt < 0) {
360 /* Count should not go negative .. */
362 panic("Hpts goes negative inp:%p hpts:%p",
365 hpts->p_on_queue_cnt = 0;
368 inp->inp_hpts_request = 0;
369 inp->inp_in_hpts = 0;
374 hpts_sane_pace_insert(struct tcp_hpts_entry *hpts, struct inpcb *inp, struct hptsh *head, int line, int noref)
377 if (mtx_owned(&hpts->p_mtx) == 0) {
378 /* We don't own the mutex? */
379 panic("%s: hpts:%p inp:%p no hpts mutex", __FUNCTION__, hpts, inp);
381 if (hpts->p_cpu != inp->inp_hpts_cpu) {
382 /* It is not the right cpu/mutex? */
383 panic("%s: hpts:%p inp:%p incorrect CPU", __FUNCTION__, hpts, inp);
385 if ((noref == 0) && (inp->inp_in_hpts == 1)) {
386 /* We are already on the hpts? */
387 panic("%s: hpts:%p inp:%p already on the hpts?", __FUNCTION__, hpts, inp);
390 TAILQ_INSERT_TAIL(head, inp, inp_hpts);
391 inp->inp_in_hpts = 1;
392 hpts->p_on_queue_cnt++;
399 hpts_sane_input_remove(struct tcp_hpts_entry *hpts, struct inpcb *inp, int clear)
402 if (mtx_owned(&hpts->p_mtx) == 0) {
403 /* We don't own the mutex? */
404 panic("%s: hpts:%p inp:%p no hpts mutex", __FUNCTION__, hpts, inp);
406 if (hpts->p_cpu != inp->inp_input_cpu) {
407 /* It is not the right cpu/mutex? */
408 panic("%s: hpts:%p inp:%p incorrect CPU", __FUNCTION__, hpts, inp);
410 if (inp->inp_in_input == 0) {
411 /* We are not on the input hpts? */
412 panic("%s: hpts:%p inp:%p not on the input hpts?", __FUNCTION__, hpts, inp);
415 TAILQ_REMOVE(&hpts->p_input, inp, inp_input);
416 hpts->p_on_inqueue_cnt--;
417 if (hpts->p_on_inqueue_cnt < 0) {
419 panic("Hpts in goes negative inp:%p hpts:%p",
422 hpts->p_on_inqueue_cnt = 0;
425 if (TAILQ_EMPTY(&hpts->p_input) &&
426 (hpts->p_on_inqueue_cnt != 0)) {
427 /* We should not be empty with a queue count */
428 panic("%s hpts:%p in_hpts input empty but cnt:%d",
429 __FUNCTION__, hpts, hpts->p_on_inqueue_cnt);
433 inp->inp_in_input = 0;
437 hpts_sane_input_insert(struct tcp_hpts_entry *hpts, struct inpcb *inp, int line)
440 if (mtx_owned(&hpts->p_mtx) == 0) {
441 /* We don't own the mutex? */
442 panic("%s: hpts:%p inp:%p no hpts mutex", __FUNCTION__, hpts, inp);
444 if (hpts->p_cpu != inp->inp_input_cpu) {
445 /* It is not the right cpu/mutex? */
446 panic("%s: hpts:%p inp:%p incorrect CPU", __FUNCTION__, hpts, inp);
448 if (inp->inp_in_input == 1) {
449 /* We are already on the input hpts? */
450 panic("%s: hpts:%p inp:%p already on the input hpts?", __FUNCTION__, hpts, inp);
453 TAILQ_INSERT_TAIL(&hpts->p_input, inp, inp_input);
454 inp->inp_in_input = 1;
455 hpts->p_on_inqueue_cnt++;
460 sysctl_tcp_hpts_log(SYSCTL_HANDLER_ARGS)
462 struct tcp_hpts_entry *hpts;
464 int32_t logging_was, i;
468 * HACK: Turn off logging so no locks are required this really needs
469 * a memory barrier :)
471 logging_was = logging_on;
476 for (i = 0; i < tcp_pace.rp_num_hptss; i++) {
477 hpts = tcp_pace.rp_ent[i];
478 if (hpts->p_log == NULL)
480 sz += (sizeof(struct hpts_log) * hpts->p_logsize);
482 error = SYSCTL_OUT(req, 0, sz);
484 for (i = 0; i < tcp_pace.rp_num_hptss; i++) {
485 hpts = tcp_pace.rp_ent[i];
486 if (hpts->p_log == NULL)
488 if (hpts->p_log_wrapped)
489 sz = (sizeof(struct hpts_log) * hpts->p_logsize);
491 sz = (sizeof(struct hpts_log) * hpts->p_log_at);
492 error = SYSCTL_OUT(req, hpts->p_log, sz);
495 logging_on = logging_was;
499 SYSCTL_PROC(_net_inet_tcp_hpts, OID_AUTO, log, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
500 0, 0, sysctl_tcp_hpts_log, "A", "tcp hptsi log");
504 * Try to get the INP_INFO lock.
506 * This function always succeeds in getting the lock. It will clear
507 * *tpp and return (1) if something critical changed while the inpcb
508 * was unlocked. Otherwise, it will leave *tpp unchanged and return (0).
510 * This function relies on the fact that the hpts always holds a
511 * reference on the inpcb while the segment is on the hptsi wheel and
512 * in the input queue.
516 tcp_hptsi_lock_inpinfo(struct inpcb *inp, struct tcpcb **tpp)
518 struct tcp_function_block *tfb;
522 /* Try the easy way. */
523 if (INP_INFO_TRY_RLOCK(&V_tcbinfo))
527 * OK, let's try the hard way. We'll save the function pointer block
528 * to make sure that doesn't change while we aren't holding the
535 INP_INFO_RLOCK(&V_tcbinfo);
537 /* If the session went away, return an error. */
538 if ((inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) ||
539 (inp->inp_flags2 & INP_FREED)) {
544 * If the function block or stack-specific data block changed,
548 if ((tp->t_fb != tfb) && (tp->t_fb_ptr != ptr)) {
557 tcp_wakehpts(struct tcp_hpts_entry *hpts)
559 HPTS_MTX_ASSERT(hpts);
560 swi_sched(hpts->ie_cookie, 0);
561 if (hpts->p_hpts_active == 2) {
562 /* Rare sleeping on a ENOBUF */
568 tcp_wakeinput(struct tcp_hpts_entry *hpts)
570 HPTS_MTX_ASSERT(hpts);
571 swi_sched(hpts->ie_cookie, 0);
572 if (hpts->p_hpts_active == 2) {
573 /* Rare sleeping on a ENOBUF */
578 struct tcp_hpts_entry *
579 tcp_cur_hpts(struct inpcb *inp)
582 struct tcp_hpts_entry *hpts;
584 hpts_num = inp->inp_hpts_cpu;
585 hpts = tcp_pace.rp_ent[hpts_num];
589 struct tcp_hpts_entry *
590 tcp_hpts_lock(struct inpcb *inp)
592 struct tcp_hpts_entry *hpts;
596 hpts_num = inp->inp_hpts_cpu;
597 hpts = tcp_pace.rp_ent[hpts_num];
599 if (mtx_owned(&hpts->p_mtx)) {
600 panic("Hpts:%p owns mtx prior-to lock line:%d",
604 mtx_lock(&hpts->p_mtx);
605 if (hpts_num != inp->inp_hpts_cpu) {
606 mtx_unlock(&hpts->p_mtx);
612 struct tcp_hpts_entry *
613 tcp_input_lock(struct inpcb *inp)
615 struct tcp_hpts_entry *hpts;
619 hpts_num = inp->inp_input_cpu;
620 hpts = tcp_pace.rp_ent[hpts_num];
622 if (mtx_owned(&hpts->p_mtx)) {
623 panic("Hpts:%p owns mtx prior-to lock line:%d",
627 mtx_lock(&hpts->p_mtx);
628 if (hpts_num != inp->inp_input_cpu) {
629 mtx_unlock(&hpts->p_mtx);
636 tcp_remove_hpts_ref(struct inpcb *inp, struct tcp_hpts_entry *hpts, int line)
640 if (inp->inp_flags2 & INP_FREED) {
642 * Need to play a special trick so that in_pcbrele_wlocked
643 * does not return 1 when it really should have returned 0.
646 inp->inp_flags2 &= ~INP_FREED;
650 #ifndef INP_REF_DEBUG
651 if (in_pcbrele_wlocked(inp)) {
653 * This should not happen. We have the inpcb referred to by
654 * the main socket (why we are called) and the hpts. It
655 * should always return 0.
657 panic("inpcb:%p release ret 1",
661 if (__in_pcbrele_wlocked(inp, line)) {
663 * This should not happen. We have the inpcb referred to by
664 * the main socket (why we are called) and the hpts. It
665 * should always return 0.
667 panic("inpcb:%p release ret 1",
672 inp->inp_flags2 |= INP_FREED;
677 tcp_hpts_remove_locked_output(struct tcp_hpts_entry *hpts, struct inpcb *inp, int32_t flags, int32_t line)
679 if (inp->inp_in_hpts) {
680 hpts_sane_pace_remove(hpts, inp, &hpts->p_hptss[inp->inp_hptsslot], 1);
681 tcp_remove_hpts_ref(inp, hpts, line);
686 tcp_hpts_remove_locked_input(struct tcp_hpts_entry *hpts, struct inpcb *inp, int32_t flags, int32_t line)
688 HPTS_MTX_ASSERT(hpts);
689 if (inp->inp_in_input) {
690 hpts_sane_input_remove(hpts, inp, 1);
691 tcp_remove_hpts_ref(inp, hpts, line);
696 * Called normally with the INP_LOCKED but it
697 * does not matter, the hpts lock is the key
698 * but the lock order allows us to hold the
699 * INP lock and then get the hpts lock.
701 * Valid values in the flags are
702 * HPTS_REMOVE_OUTPUT - remove from the output of the hpts.
703 * HPTS_REMOVE_INPUT - remove from the input of the hpts.
704 * Note that you can or both values together and get two
708 __tcp_hpts_remove(struct inpcb *inp, int32_t flags, int32_t line)
710 struct tcp_hpts_entry *hpts;
712 INP_WLOCK_ASSERT(inp);
713 if (flags & HPTS_REMOVE_OUTPUT) {
714 hpts = tcp_hpts_lock(inp);
715 tcp_hpts_remove_locked_output(hpts, inp, flags, line);
716 mtx_unlock(&hpts->p_mtx);
718 if (flags & HPTS_REMOVE_INPUT) {
719 hpts = tcp_input_lock(inp);
720 tcp_hpts_remove_locked_input(hpts, inp, flags, line);
721 mtx_unlock(&hpts->p_mtx);
726 hpts_tick(struct tcp_hpts_entry *hpts, int32_t plus)
728 return ((hpts->p_prevtick + plus) % NUM_OF_HPTSI_SLOTS);
732 tcp_queue_to_hpts_immediate_locked(struct inpcb *inp, struct tcp_hpts_entry *hpts, int32_t line, int32_t noref)
734 int32_t need_wake = 0;
735 uint32_t ticknow = 0;
737 HPTS_MTX_ASSERT(hpts);
738 if (inp->inp_in_hpts == 0) {
739 /* Ok we need to set it on the hpts in the current slot */
740 if (hpts->p_hpts_active == 0) {
741 /* A sleeping hpts we want in next slot to run */
743 tcp_hpts_log_it(hpts, inp, HPTSLOG_INSERT_SLEEPER, 0,
746 inp->inp_hptsslot = hpts_tick(hpts, 1);
747 inp->inp_hpts_request = 0;
749 tcp_hpts_log_it(hpts, inp, HPTSLOG_SLEEP_BEFORE, 1, ticknow);
752 } else if ((void *)inp == hpts->p_inp) {
754 * We can't allow you to go into the same slot we
755 * are in. We must put you out.
757 inp->inp_hptsslot = hpts->p_nxt_slot;
759 inp->inp_hptsslot = hpts->p_cur_slot;
760 hpts_sane_pace_insert(hpts, inp, &hpts->p_hptss[inp->inp_hptsslot], line, noref);
761 inp->inp_hpts_request = 0;
763 tcp_hpts_log_it(hpts, inp, HPTSLOG_IMMEDIATE, 0, 0);
767 * Activate the hpts if it is sleeping and its
771 tcp_hpts_log_it(hpts, inp, HPTSLOG_WAKEUP_HPTS, 0, ticknow);
773 hpts->p_direct_wake = 1;
781 __tcp_queue_to_hpts_immediate(struct inpcb *inp, int32_t line)
784 struct tcp_hpts_entry *hpts;
786 INP_WLOCK_ASSERT(inp);
787 hpts = tcp_hpts_lock(inp);
788 ret = tcp_queue_to_hpts_immediate_locked(inp, hpts, line, 0);
789 mtx_unlock(&hpts->p_mtx);
794 tcp_hpts_insert_locked(struct tcp_hpts_entry *hpts, struct inpcb *inp, uint32_t slot, uint32_t cts, int32_t line,
795 struct hpts_diag *diag, int32_t noref)
797 int32_t need_new_to = 0;
798 int32_t need_wakeup = 0;
799 uint32_t largest_slot;
800 uint32_t ticknow = 0;
803 HPTS_MTX_ASSERT(hpts);
805 memset(diag, 0, sizeof(struct hpts_diag));
806 diag->p_hpts_active = hpts->p_hpts_active;
807 diag->p_nxt_slot = hpts->p_nxt_slot;
808 diag->p_cur_slot = hpts->p_cur_slot;
809 diag->slot_req = slot;
811 if ((inp->inp_in_hpts == 0) || noref) {
812 inp->inp_hpts_request = slot;
815 tcp_queue_to_hpts_immediate_locked(inp, hpts, line, noref);
818 if (hpts->p_hpts_active) {
820 * Its slot - 1 since nxt_slot is the next tick that
821 * will go off since the hpts is awake
824 tcp_hpts_log_it(hpts, inp, HPTSLOG_INSERT_NORMAL, slot, 0);
827 * We want to make sure that we don't place a inp in
828 * the range of p_cur_slot <-> p_nxt_slot. If we
829 * take from p_nxt_slot to the end, plus p_cur_slot
830 * and then take away 2, we will know how many is
831 * the max slots we can use.
833 if (hpts->p_nxt_slot > hpts->p_cur_slot) {
835 * Non-wrap case nxt_slot <-> cur_slot we
836 * don't want to land in. So the diff gives
837 * us what is taken away from the number of
840 largest_slot = NUM_OF_HPTSI_SLOTS - (hpts->p_nxt_slot - hpts->p_cur_slot);
841 } else if (hpts->p_nxt_slot == hpts->p_cur_slot) {
842 largest_slot = NUM_OF_HPTSI_SLOTS - 2;
845 * Wrap case so the diff gives us the number
846 * of slots that we can land in.
848 largest_slot = hpts->p_cur_slot - hpts->p_nxt_slot;
851 * We take away two so we never have a problem (20
852 * usec's) out of 1024000 usecs
855 if (inp->inp_hpts_request > largest_slot) {
857 * Restrict max jump of slots and remember
861 inp->inp_hpts_request -= largest_slot;
863 /* This one will run when we hit it */
864 inp->inp_hpts_request = 0;
866 if (hpts->p_nxt_slot == hpts->p_cur_slot)
867 slot_calc = (hpts->p_nxt_slot + slot) % NUM_OF_HPTSI_SLOTS;
869 slot_calc = (hpts->p_nxt_slot + slot - 1) % NUM_OF_HPTSI_SLOTS;
870 if (slot_calc == hpts->p_cur_slot) {
873 panic("Hpts:%p impossible slot calculation slot_calc:%u slot:%u largest:%u\n",
874 hpts, slot_calc, slot, largest_slot);
879 slot_calc = NUM_OF_HPTSI_SLOTS - 1;
881 inp->inp_hptsslot = slot_calc;
883 diag->inp_hptsslot = inp->inp_hptsslot;
887 * The hpts is sleeping, we need to figure out where
888 * it will wake up at and if we need to reschedule
891 uint32_t have_slept, yet_to_sleep;
895 ticknow = tcp_gethptstick(&tv);
896 slot_now = ticknow % NUM_OF_HPTSI_SLOTS;
898 * The user wants to be inserted at (slot_now +
899 * slot) % NUM_OF_HPTSI_SLOTS, so lets set that up.
901 largest_slot = NUM_OF_HPTSI_SLOTS - 2;
902 if (inp->inp_hpts_request > largest_slot) {
903 /* Adjust the residual in inp_hpts_request */
905 inp->inp_hpts_request -= largest_slot;
907 /* No residual it all fits */
908 inp->inp_hpts_request = 0;
910 inp->inp_hptsslot = (slot_now + slot) % NUM_OF_HPTSI_SLOTS;
912 diag->slot_now = slot_now;
913 diag->inp_hptsslot = inp->inp_hptsslot;
914 diag->p_on_min_sleep = hpts->p_on_min_sleep;
917 tcp_hpts_log_it(hpts, inp, HPTSLOG_INSERT_SLEEPER, slot, ticknow);
919 /* Now do we need to restart the hpts's timer? */
920 if (TSTMP_GT(ticknow, hpts->p_curtick))
921 have_slept = ticknow - hpts->p_curtick;
924 if (have_slept < hpts->p_hpts_sleep_time) {
925 /* This should be what happens */
926 yet_to_sleep = hpts->p_hpts_sleep_time - have_slept;
928 /* We are over-due */
933 diag->have_slept = have_slept;
934 diag->yet_to_sleep = yet_to_sleep;
935 diag->hpts_sleep_time = hpts->p_hpts_sleep_time;
937 if ((hpts->p_on_min_sleep == 0) && (yet_to_sleep > slot)) {
939 * We need to reschedule the hptss time-out.
941 hpts->p_hpts_sleep_time = slot;
942 need_new_to = slot * HPTS_TICKS_PER_USEC;
945 hpts_sane_pace_insert(hpts, inp, &hpts->p_hptss[inp->inp_hptsslot], line, noref);
947 tcp_hpts_log_it(hpts, inp, HPTSLOG_INSERTED, slot, ticknow);
950 * Now how far is the hpts sleeping to? if active is 1, its
951 * up and ticking we do nothing, otherwise we may need to
952 * reschedule its callout if need_new_to is set from above.
956 tcp_hpts_log_it(hpts, inp, HPTSLOG_RESCHEDULE, 1, 0);
958 hpts->p_direct_wake = 1;
961 diag->need_new_to = 0;
962 diag->co_ret = 0xffff0000;
964 } else if (need_new_to) {
971 while (need_new_to > HPTS_USEC_IN_SEC) {
973 need_new_to -= HPTS_USEC_IN_SEC;
975 tv.tv_usec = need_new_to;
977 if (tcp_hpts_callout_skip_swi == 0) {
978 co_ret = callout_reset_sbt_on(&hpts->co, sb, 0,
979 hpts_timeout_swi, hpts, hpts->p_cpu,
980 (C_DIRECT_EXEC | C_PREL(tcp_hpts_precision)));
982 co_ret = callout_reset_sbt_on(&hpts->co, sb, 0,
983 hpts_timeout_dir, hpts,
985 C_PREL(tcp_hpts_precision));
988 diag->need_new_to = need_new_to;
989 diag->co_ret = co_ret;
994 panic("Hpts:%p tp:%p already on hpts and add?", hpts, inp);
1000 tcp_hpts_insert_diag(struct inpcb *inp, uint32_t slot, int32_t line, struct hpts_diag *diag){
1001 struct tcp_hpts_entry *hpts;
1002 uint32_t slot_on, cts;
1006 * We now return the next-slot the hpts will be on, beyond its
1007 * current run (if up) or where it was when it stopped if it is
1010 INP_WLOCK_ASSERT(inp);
1011 hpts = tcp_hpts_lock(inp);
1012 if (in_ts_percision)
1015 getmicrouptime(&tv);
1016 cts = tcp_tv_to_usectick(&tv);
1017 tcp_hpts_insert_locked(hpts, inp, slot, cts, line, diag, 0);
1018 slot_on = hpts->p_nxt_slot;
1019 mtx_unlock(&hpts->p_mtx);
1024 __tcp_hpts_insert(struct inpcb *inp, uint32_t slot, int32_t line){
1025 return (tcp_hpts_insert_diag(inp, slot, line, NULL));
1029 __tcp_queue_to_input_locked(struct inpcb *inp, struct tcp_hpts_entry *hpts, int32_t line)
1033 HPTS_MTX_ASSERT(hpts);
1034 if (inp->inp_in_input == 0) {
1035 /* Ok we need to set it on the hpts in the current slot */
1036 hpts_sane_input_insert(hpts, inp, line);
1038 if (hpts->p_hpts_active == 0) {
1040 * Activate the hpts if it is sleeping.
1043 tcp_hpts_log_it(hpts, inp, HPTSLOG_WAKEUP_INPUT, 0, 0);
1046 hpts->p_direct_wake = 1;
1047 tcp_wakeinput(hpts);
1049 } else if (hpts->p_hpts_active == 0) {
1051 hpts->p_direct_wake = 1;
1052 tcp_wakeinput(hpts);
1058 tcp_queue_pkt_to_input(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th,
1059 int32_t tlen, int32_t drop_hdrlen, uint8_t iptos, uint8_t ti_locked)
1061 /* Setup packet for input first */
1062 INP_WLOCK_ASSERT(tp->t_inpcb);
1063 m->m_pkthdr.pace_thoff = (uint16_t) ((caddr_t)th - mtod(m, caddr_t));
1064 m->m_pkthdr.pace_tlen = (uint16_t) tlen;
1065 m->m_pkthdr.pace_drphdrlen = drop_hdrlen;
1066 m->m_pkthdr.pace_tos = iptos;
1067 m->m_pkthdr.pace_lock = (uint8_t) ti_locked;
1068 if (tp->t_in_pkt == NULL) {
1072 tp->t_tail_pkt->m_nextpkt = m;
1079 __tcp_queue_to_input(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th,
1080 int32_t tlen, int32_t drop_hdrlen, uint8_t iptos, uint8_t ti_locked, int32_t line){
1081 struct tcp_hpts_entry *hpts;
1084 tcp_queue_pkt_to_input(tp, m, th, tlen, drop_hdrlen, iptos, ti_locked);
1085 hpts = tcp_input_lock(tp->t_inpcb);
1086 ret = __tcp_queue_to_input_locked(tp->t_inpcb, hpts, line);
1087 mtx_unlock(&hpts->p_mtx);
1092 __tcp_set_inp_to_drop(struct inpcb *inp, uint16_t reason, int32_t line)
1094 struct tcp_hpts_entry *hpts;
1097 tp = intotcpcb(inp);
1098 hpts = tcp_input_lock(tp->t_inpcb);
1099 if (inp->inp_in_input == 0) {
1100 /* Ok we need to set it on the hpts in the current slot */
1101 hpts_sane_input_insert(hpts, inp, line);
1102 if (hpts->p_hpts_active == 0) {
1104 * Activate the hpts if it is sleeping.
1106 hpts->p_direct_wake = 1;
1107 tcp_wakeinput(hpts);
1109 } else if (hpts->p_hpts_active == 0) {
1110 hpts->p_direct_wake = 1;
1111 tcp_wakeinput(hpts);
1113 inp->inp_hpts_drop_reas = reason;
1114 mtx_unlock(&hpts->p_mtx);
1118 hpts_random_cpu(struct inpcb *inp){
1120 * No flow type set distribute the load randomly.
1126 * If one has been set use it i.e. we want both in and out on the
1129 if (inp->inp_input_cpu_set) {
1130 return (inp->inp_input_cpu);
1131 } else if (inp->inp_hpts_cpu_set) {
1132 return (inp->inp_hpts_cpu);
1134 /* Nothing set use a random number */
1136 cpuid = (ran & 0xffff) % mp_ncpus;
1141 hpts_cpuid(struct inpcb *inp){
1146 * If one has been set use it i.e. we want both in and out on the
1149 if (inp->inp_input_cpu_set) {
1150 return (inp->inp_input_cpu);
1151 } else if (inp->inp_hpts_cpu_set) {
1152 return (inp->inp_hpts_cpu);
1154 /* If one is set the other must be the same */
1156 cpuid = rss_hash2cpuid(inp->inp_flowid, inp->inp_flowtype);
1157 if (cpuid == NETISR_CPUID_NONE)
1158 return (hpts_random_cpu(inp));
1163 * We don't have a flowid -> cpuid mapping, so cheat and just map
1164 * unknown cpuids to curcpu. Not the best, but apparently better
1165 * than defaulting to swi 0.
1167 if (inp->inp_flowtype != M_HASHTYPE_NONE) {
1168 cpuid = inp->inp_flowid % mp_ncpus;
1171 cpuid = hpts_random_cpu(inp);
1177 * Do NOT try to optimize the processing of inp's
1178 * by first pulling off all the inp's into a temporary
1179 * list (e.g. TAILQ_CONCAT). If you do that the subtle
1180 * interactions of switching CPU's will kill because of
1181 * problems in the linked list manipulation. Basically
1182 * you would switch cpu's with the hpts mutex locked
1183 * but then while you were processing one of the inp's
1184 * some other one that you switch will get a new
1185 * packet on the different CPU. It will insert it
1186 * on the new hptss input list. Creating a temporary
1187 * link in the inp will not fix it either, since
1188 * the other hpts will be doing the same thing and
1189 * you will both end up using the temporary link.
1191 * You will die in an ASSERT for tailq corruption if you
1192 * run INVARIANTS or you will die horribly without
1193 * INVARIANTS in some unknown way with a corrupt linked
1197 tcp_input_data(struct tcp_hpts_entry *hpts, struct timeval *tv)
1202 uint16_t drop_reason;
1204 uint32_t did_prefetch = 0;
1205 int32_t ti_locked = TI_UNLOCKED;
1207 HPTS_MTX_ASSERT(hpts);
1208 while ((inp = TAILQ_FIRST(&hpts->p_input)) != NULL) {
1209 HPTS_MTX_ASSERT(hpts);
1210 hpts_sane_input_remove(hpts, inp, 0);
1211 if (inp->inp_input_cpu_set == 0) {
1217 drop_reason = inp->inp_hpts_drop_reas;
1218 inp->inp_in_input = 0;
1219 mtx_unlock(&hpts->p_mtx);
1221 INP_INFO_RLOCK(&V_tcbinfo);
1222 ti_locked = TI_RLOCKED;
1224 ti_locked = TI_UNLOCKED;
1227 if ((inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) ||
1228 (inp->inp_flags2 & INP_FREED)) {
1231 if (ti_locked == TI_RLOCKED) {
1232 INP_INFO_RUNLOCK(&V_tcbinfo);
1234 if (in_pcbrele_wlocked(inp) == 0) {
1237 ti_locked = TI_UNLOCKED;
1238 mtx_lock(&hpts->p_mtx);
1241 tp = intotcpcb(inp);
1242 if ((tp == NULL) || (tp->t_inpcb == NULL)) {
1246 /* This tcb is being destroyed for drop_reason */
1252 tp->t_in_pkt = NULL;
1259 tp = tcp_drop(tp, drop_reason);
1260 INP_INFO_RUNLOCK(&V_tcbinfo);
1264 if (in_pcbrele_wlocked(inp) == 0)
1266 mtx_lock(&hpts->p_mtx);
1271 * Setup so the next time we will move to the right
1272 * CPU. This should be a rare event. It will
1273 * sometimes happens when we are the client side
1274 * (usually not the server). Somehow tcp_output()
1275 * gets called before the tcp_do_segment() sets the
1276 * intial state. This means the r_cpu and r_hpts_cpu
1277 * is 0. We get on the hpts, and then tcp_input()
1278 * gets called setting up the r_cpu to the correct
1279 * value. The hpts goes off and sees the mis-match.
1280 * We simply correct it here and the CPU will switch
1281 * to the new hpts nextime the tcb gets added to the
1282 * the hpts (not this time) :-)
1286 CURVNET_SET(tp->t_vnet);
1290 (m->m_pkthdr.pace_lock == TI_RLOCKED ||
1291 tp->t_state != TCPS_ESTABLISHED)) {
1292 ti_locked = TI_RLOCKED;
1293 if (tcp_hptsi_lock_inpinfo(inp, &tp)) {
1299 if (in_newts_every_tcb) {
1300 if (in_ts_percision)
1305 if (tp->t_fb_ptr != NULL) {
1306 kern_prefetch(tp->t_fb_ptr, &did_prefetch);
1309 /* Any input work to do, if so do it first */
1310 if ((m != NULL) && (m == tp->t_in_pkt)) {
1312 int32_t tlen, drop_hdrlen, nxt_pkt;
1316 tp->t_in_pkt = tp->t_tail_pkt = NULL;
1318 th = (struct tcphdr *)(mtod(m, caddr_t)+m->m_pkthdr.pace_thoff);
1319 tlen = m->m_pkthdr.pace_tlen;
1320 drop_hdrlen = m->m_pkthdr.pace_drphdrlen;
1321 iptos = m->m_pkthdr.pace_tos;
1322 m->m_nextpkt = NULL;
1327 inp->inp_input_calls = 1;
1328 if (tp->t_fb->tfb_tcp_hpts_do_segment) {
1329 /* Use the hpts specific do_segment */
1330 (*tp->t_fb->tfb_tcp_hpts_do_segment) (m, th, inp->inp_socket,
1332 tlen, iptos, ti_locked, nxt_pkt, tv);
1334 /* Use the default do_segment */
1335 (*tp->t_fb->tfb_tcp_do_segment) (m, th, inp->inp_socket,
1337 tlen, iptos, ti_locked);
1340 * Do segment returns unlocked we need the
1341 * lock again but we also need some kasserts
1344 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1345 INP_UNLOCK_ASSERT(inp);
1350 m->m_pkthdr.pace_lock == TI_RLOCKED) {
1351 INP_INFO_RLOCK(&V_tcbinfo);
1352 ti_locked = TI_RLOCKED;
1354 ti_locked = TI_UNLOCKED;
1357 * Since we have an opening here we must
1358 * re-check if the tcb went away while we
1359 * were getting the lock(s).
1361 if ((inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) ||
1362 (inp->inp_flags2 & INP_FREED)) {
1374 * Now that we hold the INP lock, check if
1375 * we need to upgrade our lock.
1377 if (ti_locked == TI_UNLOCKED &&
1378 (tp->t_state != TCPS_ESTABLISHED)) {
1379 ti_locked = TI_RLOCKED;
1380 if (tcp_hptsi_lock_inpinfo(inp, &tp))
1383 } /** end while(m) */
1384 } /** end if ((m != NULL) && (m == tp->t_in_pkt)) */
1385 if (in_pcbrele_wlocked(inp) == 0)
1387 if (ti_locked == TI_RLOCKED)
1388 INP_INFO_RUNLOCK(&V_tcbinfo);
1389 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1390 INP_UNLOCK_ASSERT(inp);
1391 ti_locked = TI_UNLOCKED;
1392 mtx_lock(&hpts->p_mtx);
1399 tcp_hpts_est_run(struct tcp_hpts_entry *hpts)
1401 int32_t ticks_to_run;
1403 if (hpts->p_prevtick && (SEQ_GT(hpts->p_curtick, hpts->p_prevtick))) {
1404 ticks_to_run = hpts->p_curtick - hpts->p_prevtick;
1405 if (ticks_to_run >= (NUM_OF_HPTSI_SLOTS - 1)) {
1406 ticks_to_run = NUM_OF_HPTSI_SLOTS - 2;
1409 if (hpts->p_prevtick == hpts->p_curtick) {
1410 /* This happens when we get woken up right away */
1415 /* Set in where we will be when we catch up */
1416 hpts->p_nxt_slot = (hpts->p_cur_slot + ticks_to_run) % NUM_OF_HPTSI_SLOTS;
1417 if (hpts->p_nxt_slot == hpts->p_cur_slot) {
1418 panic("Impossible math -- hpts:%p p_nxt_slot:%d p_cur_slot:%d ticks_to_run:%d",
1419 hpts, hpts->p_nxt_slot, hpts->p_cur_slot, ticks_to_run);
1421 return (ticks_to_run);
1425 tcp_hptsi(struct tcp_hpts_entry *hpts, struct timeval *ctick)
1428 struct inpcb *inp = NULL, *ninp;
1430 int32_t ticks_to_run, i, error, tick_now, interum_tick;
1431 int32_t paced_cnt = 0;
1432 int32_t did_prefetch = 0;
1433 int32_t prefetch_ninp = 0;
1434 int32_t prefetch_tp = 0;
1438 HPTS_MTX_ASSERT(hpts);
1439 hpts->p_curtick = tcp_tv_to_hptstick(ctick);
1440 cts = tcp_tv_to_usectick(ctick);
1441 memcpy(&tv, ctick, sizeof(struct timeval));
1442 hpts->p_cur_slot = hpts_tick(hpts, 1);
1444 /* Figure out if we had missed ticks */
1446 HPTS_MTX_ASSERT(hpts);
1447 ticks_to_run = tcp_hpts_est_run(hpts);
1448 if (!TAILQ_EMPTY(&hpts->p_input)) {
1449 tcp_input_data(hpts, &tv);
1452 if (TAILQ_EMPTY(&hpts->p_input) &&
1453 (hpts->p_on_inqueue_cnt != 0)) {
1454 panic("tp:%p in_hpts input empty but cnt:%d",
1455 hpts, hpts->p_on_inqueue_cnt);
1458 HPTS_MTX_ASSERT(hpts);
1459 /* Reset the ticks to run and time if we need too */
1460 interum_tick = tcp_gethptstick(&tv);
1461 if (interum_tick != hpts->p_curtick) {
1462 /* Save off the new time we execute to */
1464 hpts->p_curtick = interum_tick;
1465 cts = tcp_tv_to_usectick(&tv);
1466 hpts->p_cur_slot = hpts_tick(hpts, 1);
1467 ticks_to_run = tcp_hpts_est_run(hpts);
1469 if (ticks_to_run == -1) {
1473 tcp_hpts_log_it(hpts, inp, HPTSLOG_SETTORUN, ticks_to_run, 0);
1475 if (hpts->p_on_queue_cnt == 0) {
1478 HPTS_MTX_ASSERT(hpts);
1479 for (i = 0; i < ticks_to_run; i++) {
1481 * Calculate our delay, if there are no extra ticks there
1484 hpts->p_delayed_by = (ticks_to_run - (i + 1)) * HPTS_TICKS_PER_USEC;
1485 HPTS_MTX_ASSERT(hpts);
1486 while ((inp = TAILQ_FIRST(&hpts->p_hptss[hpts->p_cur_slot])) != NULL) {
1489 tcp_hpts_log_it(hpts, inp, HPTSLOG_HPTSI, ticks_to_run, i);
1493 if (hpts->p_cur_slot != inp->inp_hptsslot) {
1494 panic("Hpts:%p inp:%p slot mis-aligned %u vs %u",
1495 hpts, inp, hpts->p_cur_slot, inp->inp_hptsslot);
1498 if (inp->inp_hpts_cpu_set == 0) {
1503 hpts_sane_pace_remove(hpts, inp, &hpts->p_hptss[hpts->p_cur_slot], 0);
1504 if ((ninp = TAILQ_FIRST(&hpts->p_hptss[hpts->p_cur_slot])) != NULL) {
1505 /* We prefetch the next inp if possible */
1506 kern_prefetch(ninp, &prefetch_ninp);
1509 if (inp->inp_hpts_request) {
1511 * This guy is deferred out further in time
1512 * then our wheel had on it. Push him back
1515 int32_t remaining_slots;
1517 remaining_slots = ticks_to_run - (i + 1);
1518 if (inp->inp_hpts_request > remaining_slots) {
1520 * Keep INVARIANTS happy by clearing
1523 tcp_hpts_insert_locked(hpts, inp, inp->inp_hpts_request, cts, __LINE__, NULL, 1);
1527 inp->inp_hpts_request = 0;
1530 * We clear the hpts flag here after dealing with
1531 * remaining slots. This way anyone looking with the
1532 * TCB lock will see its on the hpts until just
1535 inp->inp_in_hpts = 0;
1536 mtx_unlock(&hpts->p_mtx);
1538 if (in_pcbrele_wlocked(inp)) {
1539 mtx_lock(&hpts->p_mtx);
1541 tcp_hpts_log_it(hpts, hpts->p_inp, HPTSLOG_INP_DONE, 0, 1);
1545 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1548 if (mtx_owned(&hpts->p_mtx)) {
1549 panic("Hpts:%p owns mtx prior-to lock line:%d",
1554 mtx_lock(&hpts->p_mtx);
1556 tcp_hpts_log_it(hpts, hpts->p_inp, HPTSLOG_INP_DONE, 0, 3);
1560 tp = intotcpcb(inp);
1561 if ((tp == NULL) || (tp->t_inpcb == NULL)) {
1566 * Setup so the next time we will move to
1567 * the right CPU. This should be a rare
1568 * event. It will sometimes happens when we
1569 * are the client side (usually not the
1570 * server). Somehow tcp_output() gets called
1571 * before the tcp_do_segment() sets the
1572 * intial state. This means the r_cpu and
1573 * r_hpts_cpu is 0. We get on the hpts, and
1574 * then tcp_input() gets called setting up
1575 * the r_cpu to the correct value. The hpts
1576 * goes off and sees the mis-match. We
1577 * simply correct it here and the CPU will
1578 * switch to the new hpts nextime the tcb
1579 * gets added to the the hpts (not this one)
1584 if (out_newts_every_tcb) {
1587 if (out_ts_percision)
1590 getmicrouptime(&sv);
1591 cts = tcp_tv_to_usectick(&sv);
1593 CURVNET_SET(tp->t_vnet);
1595 * There is a hole here, we get the refcnt on the
1596 * inp so it will still be preserved but to make
1597 * sure we can get the INP we need to hold the p_mtx
1598 * above while we pull out the tp/inp, as long as
1599 * fini gets the lock first we are assured of having
1600 * a sane INP we can lock and test.
1603 if (mtx_owned(&hpts->p_mtx)) {
1604 panic("Hpts:%p owns mtx before tcp-output:%d",
1608 if (tp->t_fb_ptr != NULL) {
1609 kern_prefetch(tp->t_fb_ptr, &did_prefetch);
1612 inp->inp_hpts_calls = 1;
1613 if (tp->t_fb->tfb_tcp_output_wtime != NULL) {
1614 error = (*tp->t_fb->tfb_tcp_output_wtime) (tp, &tv);
1616 error = tp->t_fb->tfb_tcp_output(tp);
1618 if (ninp && ninp->inp_ppcb) {
1620 * If we have a nxt inp, see if we can
1621 * prefetch its ppcb. Note this may seem
1622 * "risky" since we have no locks (other
1623 * than the previous inp) and there no
1624 * assurance that ninp was not pulled while
1625 * we were processing inp and freed. If this
1626 * occured it could mean that either:
1628 * a) Its NULL (which is fine we won't go
1629 * here) <or> b) Its valid (which is cool we
1630 * will prefetch it) <or> c) The inp got
1631 * freed back to the slab which was
1632 * reallocated. Then the piece of memory was
1633 * re-used and something else (not an
1634 * address) is in inp_ppcb. If that occurs
1635 * we don't crash, but take a TLB shootdown
1636 * performance hit (same as if it was NULL
1637 * and we tried to pre-fetch it).
1639 * Considering that the likelyhood of <c> is
1640 * quite rare we will take a risk on doing
1641 * this. If performance drops after testing
1642 * we can always take this out. NB: the
1643 * kern_prefetch on amd64 actually has
1644 * protection against a bad address now via
1645 * the DMAP_() tests. This will prevent the
1646 * TLB hit, and instead if <c> occurs just
1647 * cause us to load cache with a useless
1650 kern_prefetch(ninp->inp_ppcb, &prefetch_tp);
1654 INP_UNLOCK_ASSERT(inp);
1657 if (mtx_owned(&hpts->p_mtx)) {
1658 panic("Hpts:%p owns mtx prior-to lock line:%d",
1662 mtx_lock(&hpts->p_mtx);
1664 tcp_hpts_log_it(hpts, hpts->p_inp, HPTSLOG_INP_DONE, 0, 4);
1667 HPTS_MTX_ASSERT(hpts);
1670 if (hpts->p_cur_slot >= NUM_OF_HPTSI_SLOTS) {
1671 hpts->p_cur_slot = 0;
1675 HPTS_MTX_ASSERT(hpts);
1676 hpts->p_prevtick = hpts->p_curtick;
1677 hpts->p_delayed_by = 0;
1679 * Check to see if we took an excess amount of time and need to run
1680 * more ticks (if we did not hit eno-bufs).
1682 /* Re-run any input that may be there */
1683 (void)tcp_gethptstick(&tv);
1684 if (!TAILQ_EMPTY(&hpts->p_input)) {
1685 tcp_input_data(hpts, &tv);
1688 if (TAILQ_EMPTY(&hpts->p_input) &&
1689 (hpts->p_on_inqueue_cnt != 0)) {
1690 panic("tp:%p in_hpts input empty but cnt:%d",
1691 hpts, hpts->p_on_inqueue_cnt);
1694 tick_now = tcp_gethptstick(&tv);
1695 if (SEQ_GT(tick_now, hpts->p_prevtick)) {
1698 /* Did we really spend a full tick or more in here? */
1699 timersub(&tv, ctick, &res);
1700 if (res.tv_sec || (res.tv_usec >= HPTS_TICKS_PER_USEC)) {
1701 counter_u64_add(hpts_loops, 1);
1703 tcp_hpts_log_it(hpts, inp, HPTSLOG_TOLONG, (uint32_t) res.tv_usec, tick_now);
1706 hpts->p_curtick = tick_now;
1712 uint32_t t = 0, i, fnd = 0;
1714 if (hpts->p_on_queue_cnt) {
1718 * Find next slot that is occupied and use that to
1719 * be the sleep time.
1721 for (i = 1, t = hpts->p_nxt_slot; i < NUM_OF_HPTSI_SLOTS; i++) {
1722 if (TAILQ_EMPTY(&hpts->p_hptss[t]) == 0) {
1726 t = (t + 1) % NUM_OF_HPTSI_SLOTS;
1729 hpts->p_hpts_sleep_time = i;
1731 counter_u64_add(back_tosleep, 1);
1733 panic("Hpts:%p cnt:%d but non found", hpts, hpts->p_on_queue_cnt);
1735 hpts->p_on_queue_cnt = 0;
1740 /* No one on the wheel sleep for all but 2 slots */
1742 if (hpts_sleep_max == 0)
1744 hpts->p_hpts_sleep_time = min((NUM_OF_HPTSI_SLOTS - 2), hpts_sleep_max);
1748 tcp_hpts_log_it(hpts, inp, HPTSLOG_SLEEPSET, t, (hpts->p_hpts_sleep_time * HPTS_TICKS_PER_USEC));
1754 __tcp_set_hpts(struct inpcb *inp, int32_t line)
1756 struct tcp_hpts_entry *hpts;
1758 INP_WLOCK_ASSERT(inp);
1759 hpts = tcp_hpts_lock(inp);
1760 if ((inp->inp_in_hpts == 0) &&
1761 (inp->inp_hpts_cpu_set == 0)) {
1762 inp->inp_hpts_cpu = hpts_cpuid(inp);
1763 inp->inp_hpts_cpu_set = 1;
1765 mtx_unlock(&hpts->p_mtx);
1766 hpts = tcp_input_lock(inp);
1767 if ((inp->inp_input_cpu_set == 0) &&
1768 (inp->inp_in_input == 0)) {
1769 inp->inp_input_cpu = hpts_cpuid(inp);
1770 inp->inp_input_cpu_set = 1;
1772 mtx_unlock(&hpts->p_mtx);
1776 tcp_hpts_delayedby(struct inpcb *inp){
1777 return (tcp_pace.rp_ent[inp->inp_hpts_cpu]->p_delayed_by);
1781 tcp_hpts_thread(void *ctx)
1783 struct tcp_hpts_entry *hpts;
1787 hpts = (struct tcp_hpts_entry *)ctx;
1788 mtx_lock(&hpts->p_mtx);
1789 if (hpts->p_direct_wake) {
1790 /* Signaled by input */
1792 tcp_hpts_log_it(hpts, NULL, HPTSLOG_AWAKE, 1, 1);
1793 callout_stop(&hpts->co);
1796 if (callout_pending(&hpts->co) ||
1797 !callout_active(&hpts->co)) {
1799 tcp_hpts_log_it(hpts, NULL, HPTSLOG_AWAKE, 2, 2);
1800 mtx_unlock(&hpts->p_mtx);
1803 callout_deactivate(&hpts->co);
1805 tcp_hpts_log_it(hpts, NULL, HPTSLOG_AWAKE, 3, 3);
1807 hpts->p_hpts_active = 1;
1808 (void)tcp_gethptstick(&tv);
1809 tcp_hptsi(hpts, &tv);
1810 HPTS_MTX_ASSERT(hpts);
1812 tv.tv_usec = hpts->p_hpts_sleep_time * HPTS_TICKS_PER_USEC;
1813 if (tcp_min_hptsi_time && (tv.tv_usec < tcp_min_hptsi_time)) {
1814 tv.tv_usec = tcp_min_hptsi_time;
1815 hpts->p_on_min_sleep = 1;
1817 /* Clear the min sleep flag */
1818 hpts->p_on_min_sleep = 0;
1820 hpts->p_hpts_active = 0;
1822 if (tcp_hpts_callout_skip_swi == 0) {
1823 callout_reset_sbt_on(&hpts->co, sb, 0,
1824 hpts_timeout_swi, hpts, hpts->p_cpu,
1825 (C_DIRECT_EXEC | C_PREL(tcp_hpts_precision)));
1827 callout_reset_sbt_on(&hpts->co, sb, 0,
1828 hpts_timeout_dir, hpts,
1830 C_PREL(tcp_hpts_precision));
1832 hpts->p_direct_wake = 0;
1833 mtx_unlock(&hpts->p_mtx);
1839 tcp_init_hptsi(void *st)
1841 int32_t i, j, error, bound = 0, created = 0;
1845 struct tcp_hpts_entry *hpts;
1847 uint32_t ncpus = mp_ncpus ? mp_ncpus : MAXCPU;
1849 tcp_pace.rp_proc = NULL;
1850 tcp_pace.rp_num_hptss = ncpus;
1851 hpts_loops = counter_u64_alloc(M_WAITOK);
1852 back_tosleep = counter_u64_alloc(M_WAITOK);
1854 sz = (tcp_pace.rp_num_hptss * sizeof(struct tcp_hpts_entry *));
1855 tcp_pace.rp_ent = malloc(sz, M_TCPHPTS, M_WAITOK | M_ZERO);
1856 asz = sizeof(struct hptsh) * NUM_OF_HPTSI_SLOTS;
1857 for (i = 0; i < tcp_pace.rp_num_hptss; i++) {
1858 tcp_pace.rp_ent[i] = malloc(sizeof(struct tcp_hpts_entry),
1859 M_TCPHPTS, M_WAITOK | M_ZERO);
1860 tcp_pace.rp_ent[i]->p_hptss = malloc(asz,
1861 M_TCPHPTS, M_WAITOK);
1862 hpts = tcp_pace.rp_ent[i];
1864 * Init all the hpts structures that are not specifically
1865 * zero'd by the allocations. Also lets attach them to the
1866 * appropriate sysctl block as well.
1868 mtx_init(&hpts->p_mtx, "tcp_hpts_lck",
1869 "hpts", MTX_DEF | MTX_DUPOK);
1870 TAILQ_INIT(&hpts->p_input);
1871 for (j = 0; j < NUM_OF_HPTSI_SLOTS; j++) {
1872 TAILQ_INIT(&hpts->p_hptss[j]);
1874 sysctl_ctx_init(&hpts->hpts_ctx);
1875 sprintf(unit, "%d", i);
1876 hpts->hpts_root = SYSCTL_ADD_NODE(&hpts->hpts_ctx,
1877 SYSCTL_STATIC_CHILDREN(_net_inet_tcp_hpts),
1882 SYSCTL_ADD_INT(&hpts->hpts_ctx,
1883 SYSCTL_CHILDREN(hpts->hpts_root),
1884 OID_AUTO, "in_qcnt", CTLFLAG_RD,
1885 &hpts->p_on_inqueue_cnt, 0,
1886 "Count TCB's awaiting input processing");
1887 SYSCTL_ADD_INT(&hpts->hpts_ctx,
1888 SYSCTL_CHILDREN(hpts->hpts_root),
1889 OID_AUTO, "out_qcnt", CTLFLAG_RD,
1890 &hpts->p_on_queue_cnt, 0,
1891 "Count TCB's awaiting output processing");
1892 SYSCTL_ADD_UINT(&hpts->hpts_ctx,
1893 SYSCTL_CHILDREN(hpts->hpts_root),
1894 OID_AUTO, "active", CTLFLAG_RD,
1895 &hpts->p_hpts_active, 0,
1896 "Is the hpts active");
1897 SYSCTL_ADD_UINT(&hpts->hpts_ctx,
1898 SYSCTL_CHILDREN(hpts->hpts_root),
1899 OID_AUTO, "curslot", CTLFLAG_RD,
1900 &hpts->p_cur_slot, 0,
1901 "What the current slot is if active");
1902 SYSCTL_ADD_UINT(&hpts->hpts_ctx,
1903 SYSCTL_CHILDREN(hpts->hpts_root),
1904 OID_AUTO, "curtick", CTLFLAG_RD,
1905 &hpts->p_curtick, 0,
1906 "What the current tick on if active");
1907 SYSCTL_ADD_UINT(&hpts->hpts_ctx,
1908 SYSCTL_CHILDREN(hpts->hpts_root),
1909 OID_AUTO, "logsize", CTLFLAG_RD,
1910 &hpts->p_logsize, 0,
1911 "Hpts logging buffer size");
1912 hpts->p_hpts_sleep_time = NUM_OF_HPTSI_SLOTS - 2;
1914 hpts->p_prevtick = hpts->p_curtick = tcp_gethptstick(&tv);
1915 hpts->p_prevtick -= 1;
1916 hpts->p_prevtick %= NUM_OF_HPTSI_SLOTS;
1917 hpts->p_cpu = 0xffff;
1918 hpts->p_nxt_slot = 1;
1919 hpts->p_logsize = tcp_hpts_logging_size;
1920 if (hpts->p_logsize) {
1921 sz = (sizeof(struct hpts_log) * hpts->p_logsize);
1922 hpts->p_log = malloc(sz, M_TCPHPTS, M_WAITOK | M_ZERO);
1924 callout_init(&hpts->co, 1);
1927 * Now lets start ithreads to handle the hptss.
1930 hpts = tcp_pace.rp_ent[i];
1932 error = swi_add(&hpts->ie, "hpts",
1933 tcp_hpts_thread, (void *)hpts,
1934 SWI_NET, INTR_MPSAFE, &hpts->ie_cookie);
1936 panic("Can't add hpts:%p i:%d err:%d",
1940 if (tcp_bind_threads) {
1941 if (intr_event_bind(hpts->ie, i) == 0)
1945 tv.tv_usec = hpts->p_hpts_sleep_time * HPTS_TICKS_PER_USEC;
1947 if (tcp_hpts_callout_skip_swi == 0) {
1948 callout_reset_sbt_on(&hpts->co, sb, 0,
1949 hpts_timeout_swi, hpts, hpts->p_cpu,
1950 (C_DIRECT_EXEC | C_PREL(tcp_hpts_precision)));
1952 callout_reset_sbt_on(&hpts->co, sb, 0,
1953 hpts_timeout_dir, hpts,
1955 C_PREL(tcp_hpts_precision));
1958 printf("TCP Hpts created %d swi interrupt thread and bound %d\n",
1963 SYSINIT(tcphptsi, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, tcp_init_hptsi, NULL);