xref: /freebsd/sys/netinet/tcp_ratelimit.c (revision e9d419a05357036ea2fd37218d853d2c713d55cc)
1 /*-
2  *
3  * SPDX-License-Identifier: BSD-3-Clause
4  *
5  * Copyright (c) 2018-2020
6  *	Netflix Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  */
30 /**
31  * Author: Randall Stewart <rrs@netflix.com>
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 #include "opt_inet.h"
37 #include "opt_inet6.h"
38 #include "opt_ipsec.h"
39 #include "opt_tcpdebug.h"
40 #include "opt_ratelimit.h"
41 #include <sys/param.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/sysctl.h>
48 #include <sys/eventhandler.h>
49 #include <sys/mutex.h>
50 #include <sys/ck.h>
51 #include <net/if.h>
52 #include <net/if_var.h>
53 #include <netinet/in.h>
54 #include <netinet/in_pcb.h>
55 #define TCPSTATES		/* for logging */
56 #include <netinet/tcp_var.h>
57 #ifdef INET6
58 #include <netinet6/tcp6_var.h>
59 #endif
60 #include <netinet/tcp_hpts.h>
61 #include <netinet/tcp_log_buf.h>
62 #include <netinet/tcp_ratelimit.h>
63 #ifndef USECS_IN_SECOND
64 #define USECS_IN_SECOND 1000000
65 #endif
66 /*
67  * For the purposes of each send, what is the size
68  * of an ethernet frame.
69  */
70 MALLOC_DEFINE(M_TCPPACE, "tcp_hwpace", "TCP Hardware pacing memory");
71 #ifdef RATELIMIT
72 
73 /*
74  * The following preferred table will seem weird to
75  * the casual viewer. Why do we not have any rates below
76  * 1Mbps? Why do we have a rate at 1.44Mbps called common?
77  * Why do the rates cluster in the 1-100Mbps range more
78  * than others? Why does the table jump around at the beginnign
79  * and then be more consistently raising?
80  *
81  * Let me try to answer those questions. A lot of
82  * this is dependant on the hardware. We have three basic
83  * supporters of rate limiting
84  *
85  * Chelsio - Supporting 16 configurable rates.
86  * Mlx  - c4 supporting 13 fixed rates.
87  * Mlx  - c5 & c6 supporting 127 configurable rates.
88  *
89  * The c4 is why we have a common rate that is available
90  * in all rate tables. This is a selected rate from the
91  * c4 table and we assure its available in all ratelimit
92  * tables. This way the tcp_ratelimit code has an assured
93  * rate it should always be able to get. This answers a
94  * couple of the questions above.
95  *
96  * So what about the rest, well the table is built to
97  * try to get the most out of a joint hardware/software
98  * pacing system.  The software pacer will always pick
99  * a rate higher than the b/w that it is estimating
100  *
101  * on the path. This is done for two reasons.
102  * a) So we can discover more b/w
103  * and
104  * b) So we can send a block of MSS's down and then
105  *    have the software timer go off after the previous
106  *    send is completely out of the hardware.
107  *
108  * But when we do <b> we don't want to have the delay
109  * between the last packet sent by the hardware be
110  * excessively long (to reach our desired rate).
111  *
112  * So let me give an example for clarity.
113  *
114  * Lets assume that the tcp stack sees that 29,110,000 bps is
115  * what the bw of the path is. The stack would select the
116  * rate 31Mbps. 31Mbps means that each send that is done
117  * by the hardware will cause a 390 micro-second gap between
118  * the packets sent at that rate. For 29,110,000 bps we
119  * would need 416 micro-seconds gap between each send.
120  *
121  * Note that are calculating a complete time for pacing
122  * which includes the ethernet, IP and TCP overhead. So
123  * a full 1514 bytes is used for the above calculations.
124  * My testing has shown that both cards are also using this
125  * as their basis i.e. full payload size of the ethernet frame.
126  * The TCP stack caller needs to be aware of this and make the
127  * appropriate overhead calculations be included in its choices.
128  *
129  * Now, continuing our example, we pick a MSS size based on the
130  * delta between the two rates (416 - 390) divided into the rate
131  * we really wish to send at rounded up.  That results in a MSS
132  * send of 17 mss's at once. The hardware then will
133  * run out of data in a single 17MSS send in 6,630 micro-seconds.
134  *
135  * On the other hand the software pacer will send more data
136  * in 7,072 micro-seconds. This means that we will refill
137  * the hardware 52 microseconds after it would have sent
138  * next if it had not ran out of data. This is a win since we are
139  * only sending every 7ms or so and yet all the packets are spaced on
140  * the wire with 94% of what they should be and only
141  * the last packet is delayed extra to make up for the
142  * difference.
143  *
144  * Note that the above formula has two important caveat.
145  * If we are above (b/w wise) over 100Mbps we double the result
146  * of the MSS calculation. The second caveat is if we are 500Mbps
147  * or more we just send the maximum MSS at once i.e. 45MSS. At
148  * the higher b/w's even the cards have limits to what times (timer granularity)
149  * they can insert between packets and start to send more than one
150  * packet at a time on the wire.
151  *
152  */
153 #define COMMON_RATE 180500
154 const uint64_t desired_rates[] = {
155 	122500,			/* 1Mbps  - rate 1 */
156 	180500,			/* 1.44Mpbs - rate 2  common rate */
157 	375000,			/* 3Mbps    - rate 3 */
158 	625000,			/* 5Mbps    - rate 4 */
159 	1250000,		/* 10Mbps   - rate 5 */
160 	1875000,		/* 15Mbps   - rate 6 */
161 	2500000,		/* 20Mbps   - rate 7 */
162 	3125000,	       	/* 25Mbps   - rate 8 */
163 	3750000,		/* 30Mbps   - rate 9 */
164 	4375000,		/* 35Mbps   - rate 10 */
165 	5000000,		/* 40Meg    - rate 11 */
166 	6250000,		/* 50Mbps   - rate 12 */
167 	12500000,		/* 100Mbps  - rate 13 */
168 	25000000,		/* 200Mbps  - rate 14 */
169 	50000000,		/* 400Mbps  - rate 15 */
170 	100000000,		/* 800Mbps  - rate 16 */
171 	5625000,		/* 45Mbps   - rate 17 */
172 	6875000,		/* 55Mbps   - rate 19 */
173 	7500000,		/* 60Mbps   - rate 20 */
174 	8125000,		/* 65Mbps   - rate 21 */
175 	8750000,		/* 70Mbps   - rate 22 */
176 	9375000,		/* 75Mbps   - rate 23 */
177 	10000000,		/* 80Mbps   - rate 24 */
178 	10625000,		/* 85Mbps   - rate 25 */
179 	11250000,		/* 90Mbps   - rate 26 */
180 	11875000,		/* 95Mbps   - rate 27 */
181 	12500000,		/* 100Mbps  - rate 28 */
182 	13750000,		/* 110Mbps  - rate 29 */
183 	15000000,		/* 120Mbps  - rate 30 */
184 	16250000,		/* 130Mbps  - rate 31 */
185 	17500000,		/* 140Mbps  - rate 32 */
186 	18750000,		/* 150Mbps  - rate 33 */
187 	20000000,		/* 160Mbps  - rate 34 */
188 	21250000,		/* 170Mbps  - rate 35 */
189 	22500000,		/* 180Mbps  - rate 36 */
190 	23750000,		/* 190Mbps  - rate 37 */
191 	26250000,		/* 210Mbps  - rate 38 */
192 	27500000,		/* 220Mbps  - rate 39 */
193 	28750000,		/* 230Mbps  - rate 40 */
194 	30000000,	       	/* 240Mbps  - rate 41 */
195 	31250000,		/* 250Mbps  - rate 42 */
196 	34375000,		/* 275Mbps  - rate 43 */
197 	37500000,		/* 300Mbps  - rate 44 */
198 	40625000,		/* 325Mbps  - rate 45 */
199 	43750000,		/* 350Mbps  - rate 46 */
200 	46875000,		/* 375Mbps  - rate 47 */
201 	53125000,		/* 425Mbps  - rate 48 */
202 	56250000,		/* 450Mbps  - rate 49 */
203 	59375000,		/* 475Mbps  - rate 50 */
204 	62500000,		/* 500Mbps  - rate 51 */
205 	68750000,		/* 550Mbps  - rate 52 */
206 	75000000,		/* 600Mbps  - rate 53 */
207 	81250000,		/* 650Mbps  - rate 54 */
208 	87500000,		/* 700Mbps  - rate 55 */
209 	93750000,		/* 750Mbps  - rate 56 */
210 	106250000,		/* 850Mbps  - rate 57 */
211 	112500000,		/* 900Mbps  - rate 58 */
212 	125000000,		/* 1Gbps    - rate 59 */
213 	156250000,		/* 1.25Gps  - rate 60 */
214 	187500000,		/* 1.5Gps   - rate 61 */
215 	218750000,		/* 1.75Gps  - rate 62 */
216 	250000000,		/* 2Gbps    - rate 63 */
217 	281250000,		/* 2.25Gps  - rate 64 */
218 	312500000,		/* 2.5Gbps  - rate 65 */
219 	343750000,		/* 2.75Gbps - rate 66 */
220 	375000000,		/* 3Gbps    - rate 67 */
221 	500000000,		/* 4Gbps    - rate 68 */
222 	625000000,		/* 5Gbps    - rate 69 */
223 	750000000,		/* 6Gbps    - rate 70 */
224 	875000000,		/* 7Gbps    - rate 71 */
225 	1000000000,		/* 8Gbps    - rate 72 */
226 	1125000000,		/* 9Gbps    - rate 73 */
227 	1250000000,		/* 10Gbps   - rate 74 */
228 	1875000000,		/* 15Gbps   - rate 75 */
229 	2500000000		/* 20Gbps   - rate 76 */
230 };
231 
232 #define MAX_HDWR_RATES (sizeof(desired_rates)/sizeof(uint64_t))
233 #define RS_ORDERED_COUNT 16	/*
234 				 * Number that are in order
235 				 * at the beginning of the table,
236 				 * over this a sort is required.
237 				 */
238 #define RS_NEXT_ORDER_GROUP 16	/*
239 				 * The point in our table where
240 				 * we come fill in a second ordered
241 				 * group (index wise means -1).
242 				 */
243 #define ALL_HARDWARE_RATES 1004 /*
244 				 * 1Meg - 1Gig in 1 Meg steps
245 				 * plus 100, 200k  and 500k and
246 				 * 10Gig
247 				 */
248 
249 #define RS_ONE_MEGABIT_PERSEC 1000000
250 #define RS_ONE_GIGABIT_PERSEC 1000000000
251 #define RS_TEN_GIGABIT_PERSEC 10000000000
252 
253 static struct head_tcp_rate_set int_rs;
254 static struct mtx rs_mtx;
255 uint32_t rs_number_alive;
256 uint32_t rs_number_dead;
257 static uint32_t rs_floor_mss = 0;
258 static uint32_t wait_time_floor = 8000;	/* 8 ms */
259 static uint32_t rs_hw_floor_mss = 16;
260 static uint32_t num_of_waits_allowed = 1; /* How many time blocks are we willing to wait */
261 
262 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, rl, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
263     "TCP Ratelimit stats");
264 SYSCTL_UINT(_net_inet_tcp_rl, OID_AUTO, alive, CTLFLAG_RW,
265     &rs_number_alive, 0,
266     "Number of interfaces initialized for ratelimiting");
267 SYSCTL_UINT(_net_inet_tcp_rl, OID_AUTO, dead, CTLFLAG_RW,
268     &rs_number_dead, 0,
269     "Number of interfaces departing from ratelimiting");
270 SYSCTL_UINT(_net_inet_tcp_rl, OID_AUTO, floor_mss, CTLFLAG_RW,
271     &rs_floor_mss, 0,
272     "Number of MSS that will override the normal minimums (0 means don't enforce)");
273 SYSCTL_UINT(_net_inet_tcp_rl, OID_AUTO, wait_floor, CTLFLAG_RW,
274     &wait_time_floor, 2000,
275     "Has b/w increases what is the wait floor we are willing to wait at the end?");
276 SYSCTL_UINT(_net_inet_tcp_rl, OID_AUTO, time_blocks, CTLFLAG_RW,
277     &num_of_waits_allowed, 1,
278     "How many time blocks on the end should software pacing be willing to wait?");
279 
280 SYSCTL_UINT(_net_inet_tcp_rl, OID_AUTO, hw_floor_mss, CTLFLAG_RW,
281     &rs_hw_floor_mss, 16,
282     "Number of mss that are a minum for hardware pacing?");
283 
284 
285 static void
286 rl_add_syctl_entries(struct sysctl_oid *rl_sysctl_root, struct tcp_rate_set *rs)
287 {
288 	/*
289 	 * Add sysctl entries for thus interface.
290 	 */
291 	if (rs->rs_flags & RS_INTF_NO_SUP) {
292 		SYSCTL_ADD_S32(&rs->sysctl_ctx,
293 		   SYSCTL_CHILDREN(rl_sysctl_root),
294 		   OID_AUTO, "disable", CTLFLAG_RD,
295 		   &rs->rs_disable, 0,
296 		   "Disable this interface from new hdwr limiting?");
297 	} else {
298 		SYSCTL_ADD_S32(&rs->sysctl_ctx,
299 		   SYSCTL_CHILDREN(rl_sysctl_root),
300 		   OID_AUTO, "disable", CTLFLAG_RW,
301 		   &rs->rs_disable, 0,
302 		   "Disable this interface from new hdwr limiting?");
303 	}
304 	SYSCTL_ADD_S32(&rs->sysctl_ctx,
305 	    SYSCTL_CHILDREN(rl_sysctl_root),
306 	    OID_AUTO, "minseg", CTLFLAG_RW,
307 	    &rs->rs_min_seg, 0,
308 	    "What is the minimum we need to send on this interface?");
309 	SYSCTL_ADD_U64(&rs->sysctl_ctx,
310 	    SYSCTL_CHILDREN(rl_sysctl_root),
311 	    OID_AUTO, "flow_limit", CTLFLAG_RW,
312 	    &rs->rs_flow_limit, 0,
313 	    "What is the limit for number of flows (0=unlimited)?");
314 	SYSCTL_ADD_S32(&rs->sysctl_ctx,
315 	    SYSCTL_CHILDREN(rl_sysctl_root),
316 	    OID_AUTO, "highest", CTLFLAG_RD,
317 	    &rs->rs_highest_valid, 0,
318 	    "Highest valid rate");
319 	SYSCTL_ADD_S32(&rs->sysctl_ctx,
320 	    SYSCTL_CHILDREN(rl_sysctl_root),
321 	    OID_AUTO, "lowest", CTLFLAG_RD,
322 	    &rs->rs_lowest_valid, 0,
323 	    "Lowest valid rate");
324 	SYSCTL_ADD_S32(&rs->sysctl_ctx,
325 	    SYSCTL_CHILDREN(rl_sysctl_root),
326 	    OID_AUTO, "flags", CTLFLAG_RD,
327 	    &rs->rs_flags, 0,
328 	    "What lags are on the entry?");
329 	SYSCTL_ADD_S32(&rs->sysctl_ctx,
330 	    SYSCTL_CHILDREN(rl_sysctl_root),
331 	    OID_AUTO, "numrates", CTLFLAG_RD,
332 	    &rs->rs_rate_cnt, 0,
333 	    "How many rates re there?");
334 	SYSCTL_ADD_U64(&rs->sysctl_ctx,
335 	    SYSCTL_CHILDREN(rl_sysctl_root),
336 	    OID_AUTO, "flows_using", CTLFLAG_RD,
337 	    &rs->rs_flows_using, 0,
338 	    "How many flows are using this interface now?");
339 #ifdef DETAILED_RATELIMIT_SYSCTL
340 	if (rs->rs_rlt && rs->rs_rate_cnt > 0) {
341 		/*  Lets display the rates */
342 		int i;
343 		struct sysctl_oid *rl_rates;
344 		struct sysctl_oid *rl_rate_num;
345 		char rate_num[16];
346 		rl_rates = SYSCTL_ADD_NODE(&rs->sysctl_ctx,
347 					    SYSCTL_CHILDREN(rl_sysctl_root),
348 					    OID_AUTO,
349 					    "rate",
350 					    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
351 					    "Ratelist");
352 		for( i = 0; i < rs->rs_rate_cnt; i++) {
353 			sprintf(rate_num, "%d", i);
354 			rl_rate_num = SYSCTL_ADD_NODE(&rs->sysctl_ctx,
355 					    SYSCTL_CHILDREN(rl_rates),
356 					    OID_AUTO,
357 					    rate_num,
358 					    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
359 					    "Individual Rate");
360 			SYSCTL_ADD_U32(&rs->sysctl_ctx,
361 				       SYSCTL_CHILDREN(rl_rate_num),
362 				       OID_AUTO, "flags", CTLFLAG_RD,
363 				       &rs->rs_rlt[i].flags, 0,
364 				       "Flags on this rate");
365 			SYSCTL_ADD_U32(&rs->sysctl_ctx,
366 				       SYSCTL_CHILDREN(rl_rate_num),
367 				       OID_AUTO, "pacetime", CTLFLAG_RD,
368 				       &rs->rs_rlt[i].time_between, 0,
369 				       "Time hardware inserts between 1500 byte sends");
370 			SYSCTL_ADD_U64(&rs->sysctl_ctx,
371 				       SYSCTL_CHILDREN(rl_rate_num),
372 				       OID_AUTO, "rate", CTLFLAG_RD,
373 				       &rs->rs_rlt[i].rate, 0,
374 				       "Rate in bytes per second");
375 		}
376 	}
377 #endif
378 }
379 
380 static void
381 rs_destroy(epoch_context_t ctx)
382 {
383 	struct tcp_rate_set *rs;
384 	bool do_free_rs;
385 
386 	rs = __containerof(ctx, struct tcp_rate_set, rs_epoch_ctx);
387 
388 	mtx_lock(&rs_mtx);
389 	rs->rs_flags &= ~RS_FUNERAL_SCHD;
390 	/*
391 	 * In theory its possible (but unlikely)
392 	 * that while the delete was occuring
393 	 * and we were applying the DEAD flag
394 	 * someone slipped in and found the
395 	 * interface in a lookup. While we
396 	 * decided rs_flows_using were 0 and
397 	 * scheduling the epoch_call, the other
398 	 * thread incremented rs_flow_using. This
399 	 * is because users have a pointer and
400 	 * we only use the rs_flows_using in an
401 	 * atomic fashion, i.e. the other entities
402 	 * are not protected. To assure this did
403 	 * not occur, we check rs_flows_using here
404 	 * before deleting.
405 	 */
406 	do_free_rs = (rs->rs_flows_using == 0);
407 	rs_number_dead--;
408 	mtx_unlock(&rs_mtx);
409 
410 	if (do_free_rs) {
411 		sysctl_ctx_free(&rs->sysctl_ctx);
412 		free(rs->rs_rlt, M_TCPPACE);
413 		free(rs, M_TCPPACE);
414 	}
415 }
416 
417 static void
418 rs_defer_destroy(struct tcp_rate_set *rs)
419 {
420 
421 	mtx_assert(&rs_mtx, MA_OWNED);
422 
423 	/* Check if already pending. */
424 	if (rs->rs_flags & RS_FUNERAL_SCHD)
425 		return;
426 
427 	rs_number_dead++;
428 
429 	/* Set flag to only defer once. */
430 	rs->rs_flags |= RS_FUNERAL_SCHD;
431 	NET_EPOCH_CALL(rs_destroy, &rs->rs_epoch_ctx);
432 }
433 
434 #ifdef INET
435 extern counter_u64_t rate_limit_new;
436 extern counter_u64_t rate_limit_chg;
437 extern counter_u64_t rate_limit_set_ok;
438 extern counter_u64_t rate_limit_active;
439 extern counter_u64_t rate_limit_alloc_fail;
440 #endif
441 
442 static int
443 rl_attach_txrtlmt(struct ifnet *ifp,
444     uint32_t flowtype,
445     int flowid,
446     uint64_t cfg_rate,
447     struct m_snd_tag **tag)
448 {
449 	int error;
450 	union if_snd_tag_alloc_params params = {
451 		.rate_limit.hdr.type = IF_SND_TAG_TYPE_RATE_LIMIT,
452 		.rate_limit.hdr.flowid = flowid,
453 		.rate_limit.hdr.flowtype = flowtype,
454 		.rate_limit.max_rate = cfg_rate,
455 		.rate_limit.flags = M_NOWAIT,
456 	};
457 
458 	error = m_snd_tag_alloc(ifp, &params, tag);
459 #ifdef INET
460 	if (error == 0) {
461 		counter_u64_add(rate_limit_set_ok, 1);
462 		counter_u64_add(rate_limit_active, 1);
463 	} else if (error != EOPNOTSUPP)
464 		counter_u64_add(rate_limit_alloc_fail, 1);
465 #endif
466 	return (error);
467 }
468 
469 static void
470 populate_canned_table(struct tcp_rate_set *rs, const uint64_t *rate_table_act)
471 {
472 	/*
473 	 * The internal table is "special", it
474 	 * is two seperate ordered tables that
475 	 * must be merged. We get here when the
476 	 * adapter specifies a number of rates that
477 	 * covers both ranges in the table in some
478 	 * form.
479 	 */
480 	int i, at_low, at_high;
481 	uint8_t low_disabled = 0, high_disabled = 0;
482 
483 	for(i = 0, at_low = 0, at_high = RS_NEXT_ORDER_GROUP; i < rs->rs_rate_cnt; i++) {
484 		rs->rs_rlt[i].flags = 0;
485 		rs->rs_rlt[i].time_between = 0;
486 		if ((low_disabled == 0) &&
487 		    (high_disabled ||
488 		     (rate_table_act[at_low] < rate_table_act[at_high]))) {
489 			rs->rs_rlt[i].rate = rate_table_act[at_low];
490 			at_low++;
491 			if (at_low == RS_NEXT_ORDER_GROUP)
492 				low_disabled = 1;
493 		} else if (high_disabled == 0) {
494 			rs->rs_rlt[i].rate = rate_table_act[at_high];
495 			at_high++;
496 			if (at_high == MAX_HDWR_RATES)
497 				high_disabled = 1;
498 		}
499 	}
500 }
501 
502 static struct tcp_rate_set *
503 rt_setup_new_rs(struct ifnet *ifp, int *error)
504 {
505 	struct tcp_rate_set *rs;
506 	const uint64_t *rate_table_act;
507 	uint64_t lentim, res;
508 	size_t sz;
509 	uint32_t hash_type;
510 	int i;
511 	struct if_ratelimit_query_results rl;
512 	struct sysctl_oid *rl_sysctl_root;
513 	struct epoch_tracker et;
514 	/*
515 	 * We expect to enter with the
516 	 * mutex locked.
517 	 */
518 
519 	if (ifp->if_ratelimit_query == NULL) {
520 		/*
521 		 * We can do nothing if we cannot
522 		 * get a query back from the driver.
523 		 */
524 		printf("Warning:No query functions for %s:%d-- failed\n",
525 		       ifp->if_dname, ifp->if_dunit);
526 		return (NULL);
527 	}
528 	rs = malloc(sizeof(struct tcp_rate_set), M_TCPPACE, M_NOWAIT | M_ZERO);
529 	if (rs == NULL) {
530 		if (error)
531 			*error = ENOMEM;
532 		printf("Warning:No memory for malloc of tcp_rate_set\n");
533 		return (NULL);
534 	}
535 	memset(&rl, 0, sizeof(rl));
536 	rl.flags = RT_NOSUPPORT;
537 	ifp->if_ratelimit_query(ifp, &rl);
538 	if (rl.flags & RT_IS_UNUSABLE) {
539 		/*
540 		 * The interface does not really support
541 		 * the rate-limiting.
542 		 */
543 		memset(rs, 0, sizeof(struct tcp_rate_set));
544 		rs->rs_ifp = ifp;
545 		rs->rs_if_dunit = ifp->if_dunit;
546 		rs->rs_flags = RS_INTF_NO_SUP;
547 		rs->rs_disable = 1;
548 		rs_number_alive++;
549 		sysctl_ctx_init(&rs->sysctl_ctx);
550 		rl_sysctl_root = SYSCTL_ADD_NODE(&rs->sysctl_ctx,
551 		    SYSCTL_STATIC_CHILDREN(_net_inet_tcp_rl),
552 		    OID_AUTO,
553 		    rs->rs_ifp->if_xname,
554 		    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
555 		    "");
556 		rl_add_syctl_entries(rl_sysctl_root, rs);
557 		NET_EPOCH_ENTER(et);
558 		mtx_lock(&rs_mtx);
559 		CK_LIST_INSERT_HEAD(&int_rs, rs, next);
560 		mtx_unlock(&rs_mtx);
561 		NET_EPOCH_EXIT(et);
562 		return (rs);
563 	} else if ((rl.flags & RT_IS_INDIRECT) == RT_IS_INDIRECT) {
564 		memset(rs, 0, sizeof(struct tcp_rate_set));
565 		rs->rs_ifp = ifp;
566 		rs->rs_if_dunit = ifp->if_dunit;
567 		rs->rs_flags = RS_IS_DEFF;
568 		rs_number_alive++;
569 		sysctl_ctx_init(&rs->sysctl_ctx);
570 		rl_sysctl_root = SYSCTL_ADD_NODE(&rs->sysctl_ctx,
571 		    SYSCTL_STATIC_CHILDREN(_net_inet_tcp_rl),
572 		    OID_AUTO,
573 		    rs->rs_ifp->if_xname,
574 		    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
575 		    "");
576 		rl_add_syctl_entries(rl_sysctl_root, rs);
577 		NET_EPOCH_ENTER(et);
578 		mtx_lock(&rs_mtx);
579 		CK_LIST_INSERT_HEAD(&int_rs, rs, next);
580 		mtx_unlock(&rs_mtx);
581 		NET_EPOCH_EXIT(et);
582 		return (rs);
583 	} else if ((rl.flags & RT_IS_FIXED_TABLE) == RT_IS_FIXED_TABLE) {
584 		/* Mellanox C4 likely */
585 		rs->rs_ifp = ifp;
586 		rs->rs_if_dunit = ifp->if_dunit;
587 		rs->rs_rate_cnt = rl.number_of_rates;
588 		rs->rs_min_seg = rl.min_segment_burst;
589 		rs->rs_highest_valid = 0;
590 		rs->rs_flow_limit = rl.max_flows;
591 		rs->rs_flags = RS_IS_INTF | RS_NO_PRE;
592 		rs->rs_disable = 0;
593 		rate_table_act = rl.rate_table;
594 	} else if ((rl.flags & RT_IS_SELECTABLE) == RT_IS_SELECTABLE) {
595 		/* Chelsio, C5 and C6 of Mellanox? */
596 		rs->rs_ifp = ifp;
597 		rs->rs_if_dunit = ifp->if_dunit;
598 		rs->rs_rate_cnt = rl.number_of_rates;
599 		rs->rs_min_seg = rl.min_segment_burst;
600 		rs->rs_disable = 0;
601 		rs->rs_flow_limit = rl.max_flows;
602 		rate_table_act = desired_rates;
603 		if ((rs->rs_rate_cnt > MAX_HDWR_RATES) &&
604 		    (rs->rs_rate_cnt < ALL_HARDWARE_RATES)) {
605 			/*
606 			 * Our desired table is not big
607 			 * enough, do what we can.
608 			 */
609 			rs->rs_rate_cnt = MAX_HDWR_RATES;
610 		 }
611 		if (rs->rs_rate_cnt <= RS_ORDERED_COUNT)
612 			rs->rs_flags = RS_IS_INTF;
613 		else
614 			rs->rs_flags = RS_IS_INTF | RS_INT_TBL;
615 		if (rs->rs_rate_cnt >= ALL_HARDWARE_RATES)
616 			rs->rs_rate_cnt = ALL_HARDWARE_RATES;
617 	} else {
618 		free(rs, M_TCPPACE);
619 		return (NULL);
620 	}
621 	sz = sizeof(struct tcp_hwrate_limit_table) * rs->rs_rate_cnt;
622 	rs->rs_rlt = malloc(sz, M_TCPPACE, M_NOWAIT);
623 	if (rs->rs_rlt == NULL) {
624 		if (error)
625 			*error = ENOMEM;
626 bail:
627 		free(rs, M_TCPPACE);
628 		return (NULL);
629 	}
630 	if (rs->rs_rate_cnt >= ALL_HARDWARE_RATES) {
631 		/*
632 		 * The interface supports all
633 		 * the rates we could possibly want.
634 		 */
635 		uint64_t rat;
636 
637 		rs->rs_rlt[0].rate = 12500;	/* 100k */
638 		rs->rs_rlt[1].rate = 25000;	/* 200k */
639 		rs->rs_rlt[2].rate = 62500;	/* 500k */
640 		/* Note 125000 == 1Megabit
641 		 * populate 1Meg - 1000meg.
642 		 */
643 		for(i = 3, rat = 125000; i< (ALL_HARDWARE_RATES-1); i++) {
644 			rs->rs_rlt[i].rate = rat;
645 			rat += 125000;
646 		}
647 		rs->rs_rlt[(ALL_HARDWARE_RATES-1)].rate = 1250000000;
648 	} else if (rs->rs_flags & RS_INT_TBL) {
649 		/* We populate this in a special way */
650 		populate_canned_table(rs, rate_table_act);
651 	} else {
652 		/*
653 		 * Just copy in the rates from
654 		 * the table, it is in order.
655 		 */
656 		for (i=0; i<rs->rs_rate_cnt; i++) {
657 			rs->rs_rlt[i].rate = rate_table_act[i];
658 			rs->rs_rlt[i].time_between = 0;
659 			rs->rs_rlt[i].flags = 0;
660 		}
661 	}
662 	for (i = (rs->rs_rate_cnt - 1); i >= 0; i--) {
663 		/*
664 		 * We go backwards through the list so that if we can't get
665 		 * a rate and fail to init one, we have at least a chance of
666 		 * getting the highest one.
667 		 */
668 		rs->rs_rlt[i].ptbl = rs;
669 		rs->rs_rlt[i].tag = NULL;
670 		/*
671 		 * Calculate the time between.
672 		 */
673 		lentim = ETHERNET_SEGMENT_SIZE * USECS_IN_SECOND;
674 		res = lentim / rs->rs_rlt[i].rate;
675 		if (res > 0)
676 			rs->rs_rlt[i].time_between = res;
677 		else
678 			rs->rs_rlt[i].time_between = 1;
679 		if (rs->rs_flags & RS_NO_PRE) {
680 			rs->rs_rlt[i].flags = HDWRPACE_INITED;
681 			rs->rs_lowest_valid = i;
682 		} else {
683 			int err;
684 
685 			if ((rl.flags & RT_IS_SETUP_REQ)  &&
686 			    (ifp->if_ratelimit_query)) {
687 				err = ifp->if_ratelimit_setup(ifp,
688   				         rs->rs_rlt[i].rate, i);
689 				if (err)
690 					goto handle_err;
691 			}
692 #ifdef RSS
693 			hash_type = M_HASHTYPE_RSS_TCP_IPV4;
694 #else
695 			hash_type = M_HASHTYPE_OPAQUE_HASH;
696 #endif
697 			err = rl_attach_txrtlmt(ifp,
698 			    hash_type,
699 			    (i + 1),
700 			    rs->rs_rlt[i].rate,
701 			    &rs->rs_rlt[i].tag);
702 			if (err) {
703 handle_err:
704 				if (i == (rs->rs_rate_cnt - 1)) {
705 					/*
706 					 * Huh - first rate and we can't get
707 					 * it?
708 					 */
709 					free(rs->rs_rlt, M_TCPPACE);
710 					if (error)
711 						*error = err;
712 					goto bail;
713 				} else {
714 					if (error)
715 						*error = err;
716 				}
717 				break;
718 			} else {
719 				rs->rs_rlt[i].flags = HDWRPACE_INITED | HDWRPACE_TAGPRESENT;
720 				rs->rs_lowest_valid = i;
721 			}
722 		}
723 	}
724 	/* Did we get at least 1 rate? */
725 	if (rs->rs_rlt[(rs->rs_rate_cnt - 1)].flags & HDWRPACE_INITED)
726 		rs->rs_highest_valid = rs->rs_rate_cnt - 1;
727 	else {
728 		free(rs->rs_rlt, M_TCPPACE);
729 		goto bail;
730 	}
731 	rs_number_alive++;
732 	sysctl_ctx_init(&rs->sysctl_ctx);
733 	rl_sysctl_root = SYSCTL_ADD_NODE(&rs->sysctl_ctx,
734 	    SYSCTL_STATIC_CHILDREN(_net_inet_tcp_rl),
735 	    OID_AUTO,
736 	    rs->rs_ifp->if_xname,
737 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
738 	    "");
739 	rl_add_syctl_entries(rl_sysctl_root, rs);
740 	NET_EPOCH_ENTER(et);
741 	mtx_lock(&rs_mtx);
742 	CK_LIST_INSERT_HEAD(&int_rs, rs, next);
743 	mtx_unlock(&rs_mtx);
744 	NET_EPOCH_EXIT(et);
745 	return (rs);
746 }
747 
748 /*
749  * For an explanation of why the argument is volatile please
750  * look at the comments around rt_setup_rate().
751  */
752 static const struct tcp_hwrate_limit_table *
753 tcp_int_find_suitable_rate(const volatile struct tcp_rate_set *rs,
754     uint64_t bytes_per_sec, uint32_t flags, uint64_t *lower_rate)
755 {
756 	struct tcp_hwrate_limit_table *arte = NULL, *rte = NULL;
757 	uint64_t mbits_per_sec, ind_calc, previous_rate = 0;
758 	int i;
759 
760 	mbits_per_sec = (bytes_per_sec * 8);
761 	if (flags & RS_PACING_LT) {
762 		if ((mbits_per_sec < RS_ONE_MEGABIT_PERSEC) &&
763 		    (rs->rs_lowest_valid <= 2)){
764 			/*
765 			 * Smaller than 1Meg, only
766 			 * 3 entries can match it.
767 			 */
768 			previous_rate = 0;
769 			for(i = rs->rs_lowest_valid; i < 3; i++) {
770 				if (bytes_per_sec <= rs->rs_rlt[i].rate) {
771 					rte = &rs->rs_rlt[i];
772 					break;
773 				} else if (rs->rs_rlt[i].flags & HDWRPACE_INITED) {
774 					arte = &rs->rs_rlt[i];
775 				}
776 				previous_rate = rs->rs_rlt[i].rate;
777 			}
778 			goto done;
779 		} else if ((mbits_per_sec > RS_ONE_GIGABIT_PERSEC) &&
780 			   (rs->rs_rlt[(ALL_HARDWARE_RATES-1)].flags & HDWRPACE_INITED)){
781 			/*
782 			 * Larger than 1G (the majority of
783 			 * our table.
784 			 */
785 			if (mbits_per_sec < RS_TEN_GIGABIT_PERSEC)
786 				rte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
787 			else
788 				arte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
789 			previous_rate = rs->rs_rlt[(ALL_HARDWARE_RATES-2)].rate;
790 			goto done;
791 		}
792 		/*
793 		 * If we reach here its in our table (between 1Meg - 1000Meg),
794 		 * just take the rounded down mbits per second, and add
795 		 * 1Megabit to it, from this we can calculate
796 		 * the index in the table.
797 		 */
798 		ind_calc = mbits_per_sec/RS_ONE_MEGABIT_PERSEC;
799 		if ((ind_calc * RS_ONE_MEGABIT_PERSEC) != mbits_per_sec)
800 			ind_calc++;
801 		/* our table is offset by 3, we add 2 */
802 		ind_calc += 2;
803 		if (ind_calc > (ALL_HARDWARE_RATES-1)) {
804 			/* This should not happen */
805 			ind_calc = ALL_HARDWARE_RATES-1;
806 		}
807 		if ((ind_calc >= rs->rs_lowest_valid) &&
808 		    (ind_calc <= rs->rs_highest_valid)) {
809 			rte = &rs->rs_rlt[ind_calc];
810 			if (ind_calc >= 1)
811 				previous_rate = rs->rs_rlt[(ind_calc-1)].rate;
812 		}
813 	} else if (flags & RS_PACING_EXACT_MATCH) {
814 		if ((mbits_per_sec < RS_ONE_MEGABIT_PERSEC) &&
815 		    (rs->rs_lowest_valid <= 2)){
816 			for(i = rs->rs_lowest_valid; i < 3; i++) {
817 				if (bytes_per_sec == rs->rs_rlt[i].rate) {
818 					rte = &rs->rs_rlt[i];
819 					break;
820 				}
821 			}
822 		} else if ((mbits_per_sec > RS_ONE_GIGABIT_PERSEC) &&
823 			   (rs->rs_rlt[(ALL_HARDWARE_RATES-1)].flags & HDWRPACE_INITED)) {
824 			/* > 1Gbps only one rate */
825 			if (bytes_per_sec == rs->rs_rlt[(ALL_HARDWARE_RATES-1)].rate) {
826 				/* Its 10G wow */
827 				rte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
828 			}
829 		} else {
830 			/* Ok it must be a exact meg (its between 1G and 1Meg) */
831 			ind_calc = mbits_per_sec/RS_ONE_MEGABIT_PERSEC;
832 			if ((ind_calc * RS_ONE_MEGABIT_PERSEC) == mbits_per_sec) {
833 				/* its an exact Mbps */
834 				ind_calc += 2;
835 				if (ind_calc > (ALL_HARDWARE_RATES-1)) {
836 					/* This should not happen */
837 					ind_calc = ALL_HARDWARE_RATES-1;
838 				}
839 				if (rs->rs_rlt[ind_calc].flags & HDWRPACE_INITED)
840 					rte = &rs->rs_rlt[ind_calc];
841 			}
842 		}
843 	} else {
844 		/* we want greater than the requested rate */
845 		if ((mbits_per_sec < RS_ONE_MEGABIT_PERSEC) &&
846 		    (rs->rs_lowest_valid <= 2)){
847 			arte = &rs->rs_rlt[3]; /* set alternate to 1Meg */
848 			for (i=2; i>=rs->rs_lowest_valid; i--) {
849 				if (bytes_per_sec < rs->rs_rlt[i].rate) {
850 					rte = &rs->rs_rlt[i];
851 					if (i >= 1) {
852 						previous_rate = rs->rs_rlt[(i-1)].rate;
853 					}
854 					break;
855 				} else if ((flags & RS_PACING_GEQ) &&
856 					   (bytes_per_sec == rs->rs_rlt[i].rate)) {
857 					rte = &rs->rs_rlt[i];
858 					if (i >= 1) {
859 						previous_rate = rs->rs_rlt[(i-1)].rate;
860 					}
861 					break;
862 				} else {
863 					arte = &rs->rs_rlt[i]; /* new alternate */
864 				}
865 			}
866 		} else if (mbits_per_sec > RS_ONE_GIGABIT_PERSEC) {
867 			if ((bytes_per_sec < rs->rs_rlt[(ALL_HARDWARE_RATES-1)].rate) &&
868 			    (rs->rs_rlt[(ALL_HARDWARE_RATES-1)].flags & HDWRPACE_INITED)){
869 				/* Our top rate is larger than the request */
870 				rte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
871 			} else if ((flags & RS_PACING_GEQ) &&
872 				   (bytes_per_sec == rs->rs_rlt[(ALL_HARDWARE_RATES-1)].rate) &&
873 				   (rs->rs_rlt[(ALL_HARDWARE_RATES-1)].flags & HDWRPACE_INITED)) {
874 				/* It matches our top rate */
875 				rte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
876 			} else if (rs->rs_rlt[(ALL_HARDWARE_RATES-1)].flags & HDWRPACE_INITED) {
877 				/* The top rate is an alternative */
878 				arte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
879 			}
880 			previous_rate = rs->rs_rlt[(ALL_HARDWARE_RATES-2)].rate;
881 		} else {
882 			/* Its in our range 1Meg - 1Gig */
883 			if (flags & RS_PACING_GEQ) {
884 				ind_calc = mbits_per_sec/RS_ONE_MEGABIT_PERSEC;
885 				if ((ind_calc * RS_ONE_MEGABIT_PERSEC) == mbits_per_sec) {
886 					if (ind_calc > (ALL_HARDWARE_RATES-1)) {
887 						/* This should not happen */
888 						ind_calc = (ALL_HARDWARE_RATES-1);
889 					}
890 					rte = &rs->rs_rlt[ind_calc];
891 					if (ind_calc >= 1)
892 						previous_rate = rs->rs_rlt[(ind_calc-1)].rate;
893 				}
894 				goto done;
895 			}
896 			ind_calc = (mbits_per_sec + (RS_ONE_MEGABIT_PERSEC-1))/RS_ONE_MEGABIT_PERSEC;
897 			ind_calc += 2;
898 			if (ind_calc > (ALL_HARDWARE_RATES-1)) {
899 				/* This should not happen */
900 				ind_calc = ALL_HARDWARE_RATES-1;
901 			}
902 			if (rs->rs_rlt[ind_calc].flags & HDWRPACE_INITED) {
903 				rte = &rs->rs_rlt[ind_calc];
904 				if (ind_calc >= 1)
905 					previous_rate = rs->rs_rlt[(ind_calc-1)].rate;
906 			}
907 		}
908 	}
909 done:
910 	if ((rte == NULL) &&
911 	    (arte != NULL) &&
912 	    (flags & RS_PACING_SUB_OK)) {
913 		/* We can use the substitute */
914 		rte = arte;
915 	}
916 	if (lower_rate)
917 		*lower_rate = previous_rate;
918 	return (rte);
919 }
920 
921 /*
922  * For an explanation of why the argument is volatile please
923  * look at the comments around rt_setup_rate().
924  */
925 static const struct tcp_hwrate_limit_table *
926 tcp_find_suitable_rate(const volatile struct tcp_rate_set *rs, uint64_t bytes_per_sec, uint32_t flags, uint64_t *lower_rate)
927 {
928 	/**
929 	 * Hunt the rate table with the restrictions in flags and find a
930 	 * suitable rate if possible.
931 	 * RS_PACING_EXACT_MATCH - look for an exact match to rate.
932 	 * RS_PACING_GT     - must be greater than.
933 	 * RS_PACING_GEQ    - must be greater than or equal.
934 	 * RS_PACING_LT     - must be less than.
935 	 * RS_PACING_SUB_OK - If we don't meet criteria a
936 	 *                    substitute is ok.
937 	 */
938 	int i, matched;
939 	struct tcp_hwrate_limit_table *rte = NULL;
940 	uint64_t previous_rate = 0;
941 
942 	if ((rs->rs_flags & RS_INT_TBL) &&
943 	    (rs->rs_rate_cnt >= ALL_HARDWARE_RATES)) {
944 		/*
945 		 * Here we don't want to paw thru
946 		 * a big table, we have everything
947 		 * from 1Meg - 1000Meg in 1Meg increments.
948 		 * Use an alternate method to "lookup".
949 		 */
950 		return (tcp_int_find_suitable_rate(rs, bytes_per_sec, flags, lower_rate));
951 	}
952 	if ((flags & RS_PACING_LT) ||
953 	    (flags & RS_PACING_EXACT_MATCH)) {
954 		/*
955 		 * For exact and less than we go forward through the table.
956 		 * This way when we find one larger we stop (exact was a
957 		 * toss up).
958 		 */
959 		for (i = rs->rs_lowest_valid, matched = 0; i <= rs->rs_highest_valid; i++) {
960 			if ((flags & RS_PACING_EXACT_MATCH) &&
961 			    (bytes_per_sec == rs->rs_rlt[i].rate)) {
962 				rte = &rs->rs_rlt[i];
963 				matched = 1;
964 				if (lower_rate != NULL)
965 					*lower_rate = previous_rate;
966 				break;
967 			} else if ((flags & RS_PACING_LT) &&
968 			    (bytes_per_sec <= rs->rs_rlt[i].rate)) {
969 				rte = &rs->rs_rlt[i];
970 				matched = 1;
971 				if (lower_rate != NULL)
972 					*lower_rate = previous_rate;
973 				break;
974 			}
975 			previous_rate = rs->rs_rlt[i].rate;
976 			if (bytes_per_sec > rs->rs_rlt[i].rate)
977 				break;
978 		}
979 		if ((matched == 0) &&
980 		    (flags & RS_PACING_LT) &&
981 		    (flags & RS_PACING_SUB_OK)) {
982 			/* Kick in a substitute (the lowest) */
983 			rte = &rs->rs_rlt[rs->rs_lowest_valid];
984 		}
985 	} else {
986 		/*
987 		 * Here we go backward through the table so that we can find
988 		 * the one greater in theory faster (but its probably a
989 		 * wash).
990 		 */
991 		for (i = rs->rs_highest_valid, matched = 0; i >= rs->rs_lowest_valid; i--) {
992 			if (rs->rs_rlt[i].rate > bytes_per_sec) {
993 				/* A possible candidate */
994 				rte = &rs->rs_rlt[i];
995 			}
996 			if ((flags & RS_PACING_GEQ) &&
997 			    (bytes_per_sec == rs->rs_rlt[i].rate)) {
998 				/* An exact match and we want equal */
999 				matched = 1;
1000 				rte = &rs->rs_rlt[i];
1001 				break;
1002 			} else if (rte) {
1003 				/*
1004 				 * Found one that is larger than but don't
1005 				 * stop, there may be a more closer match.
1006 				 */
1007 				matched = 1;
1008 			}
1009 			if (rs->rs_rlt[i].rate < bytes_per_sec) {
1010 				/*
1011 				 * We found a table entry that is smaller,
1012 				 * stop there will be none greater or equal.
1013 				 */
1014 				if (lower_rate != NULL)
1015 					*lower_rate = rs->rs_rlt[i].rate;
1016 				break;
1017 			}
1018 		}
1019 		if ((matched == 0) &&
1020 		    (flags & RS_PACING_SUB_OK)) {
1021 			/* Kick in a substitute (the highest) */
1022 			rte = &rs->rs_rlt[rs->rs_highest_valid];
1023 		}
1024 	}
1025 	return (rte);
1026 }
1027 
1028 static struct ifnet *
1029 rt_find_real_interface(struct ifnet *ifp, struct inpcb *inp, int *error)
1030 {
1031 	struct ifnet *tifp;
1032 	struct m_snd_tag *tag, *ntag;
1033 	union if_snd_tag_alloc_params params = {
1034 		.rate_limit.hdr.type = IF_SND_TAG_TYPE_RATE_LIMIT,
1035 		.rate_limit.hdr.flowid = inp->inp_flowid,
1036 		.rate_limit.hdr.numa_domain = inp->inp_numa_domain,
1037 		.rate_limit.max_rate = COMMON_RATE,
1038 		.rate_limit.flags = M_NOWAIT,
1039 	};
1040 	int err;
1041 #ifdef RSS
1042 	params.rate_limit.hdr.flowtype = ((inp->inp_vflag & INP_IPV6) ?
1043 	    M_HASHTYPE_RSS_TCP_IPV6 : M_HASHTYPE_RSS_TCP_IPV4);
1044 #else
1045 	params.rate_limit.hdr.flowtype = M_HASHTYPE_OPAQUE_HASH;
1046 #endif
1047 	err = m_snd_tag_alloc(ifp, &params, &tag);
1048 	if (err) {
1049 		/* Failed to setup a tag? */
1050 		if (error)
1051 			*error = err;
1052 		return (NULL);
1053 	}
1054 	ntag = tag;
1055 	while(ntag->ifp->if_next_snd_tag != NULL) {
1056 		ntag = ntag->ifp->if_next_snd_tag(ntag);
1057 	}
1058 	tifp = ntag->ifp;
1059 	m_snd_tag_rele(tag);
1060 	return (tifp);
1061 }
1062 
1063 static void
1064 rl_increment_using(const struct tcp_hwrate_limit_table *rte)
1065 {
1066 }
1067 
1068 static void
1069 rl_decrement_using(const struct tcp_hwrate_limit_table *rte)
1070 {
1071 }
1072 
1073 void
1074 tcp_rl_log_enobuf(const struct tcp_hwrate_limit_table *rte)
1075 {
1076 }
1077 
1078 /*
1079  * Do NOT take the __noinline out of the
1080  * find_rs_for_ifp() function. If you do the inline
1081  * of it for the rt_setup_rate() will show you a
1082  * compiler bug. For some reason the compiler thinks
1083  * the list can never be empty. The consequence of
1084  * this will be a crash when we dereference NULL
1085  * if an ifp is removed just has a hw rate limit
1086  * is attempted. If you are working on the compiler
1087  * and want to "test" this go ahead and take the noinline
1088  * out otherwise let sleeping dogs ly until such time
1089  * as we get a compiler fix 10/2/20 -- RRS
1090  */
1091 static __noinline struct tcp_rate_set *
1092 find_rs_for_ifp(struct ifnet *ifp)
1093 {
1094 	struct tcp_rate_set *rs;
1095 
1096 	CK_LIST_FOREACH(rs, &int_rs, next) {
1097 		if ((rs->rs_ifp == ifp) &&
1098 		    (rs->rs_if_dunit == ifp->if_dunit)) {
1099 			/* Ok we found it */
1100 			return (rs);
1101 		}
1102 	}
1103 	return (NULL);
1104 }
1105 
1106 
1107 static const struct tcp_hwrate_limit_table *
1108 rt_setup_rate(struct inpcb *inp, struct ifnet *ifp, uint64_t bytes_per_sec,
1109     uint32_t flags, int *error, uint64_t *lower_rate)
1110 {
1111 	/* First lets find the interface if it exists */
1112 	const struct tcp_hwrate_limit_table *rte;
1113 	/*
1114 	 * So why is rs volatile? This is to defeat a
1115 	 * compiler bug where in the compiler is convinced
1116 	 * that rs can never be NULL (which is not true). Because
1117 	 * of its conviction it nicely optimizes out the if ((rs == NULL
1118 	 * below which means if you get a NULL back you dereference it.
1119 	 */
1120 	volatile struct tcp_rate_set *rs;
1121 	struct epoch_tracker et;
1122 	struct ifnet *oifp = ifp;
1123 	int err;
1124 
1125 	NET_EPOCH_ENTER(et);
1126 use_real_interface:
1127 	rs = find_rs_for_ifp(ifp);
1128 	if ((rs == NULL) ||
1129 	    (rs->rs_flags & RS_INTF_NO_SUP) ||
1130 	    (rs->rs_flags & RS_IS_DEAD)) {
1131 		/*
1132 		 * This means we got a packet *before*
1133 		 * the IF-UP was processed below, <or>
1134 		 * while or after we already received an interface
1135 		 * departed event. In either case we really don't
1136 		 * want to do anything with pacing, in
1137 		 * the departing case the packet is not
1138 		 * going to go very far. The new case
1139 		 * might be arguable, but its impossible
1140 		 * to tell from the departing case.
1141 		 */
1142 		if (error)
1143 			*error = ENODEV;
1144 		NET_EPOCH_EXIT(et);
1145 		return (NULL);
1146 	}
1147 
1148 	if ((rs == NULL) || (rs->rs_disable != 0)) {
1149 		if (error)
1150 			*error = ENOSPC;
1151 		NET_EPOCH_EXIT(et);
1152 		return (NULL);
1153 	}
1154 	if (rs->rs_flags & RS_IS_DEFF) {
1155 		/* We need to find the real interface */
1156 		struct ifnet *tifp;
1157 
1158 		tifp = rt_find_real_interface(ifp, inp, error);
1159 		if (tifp == NULL) {
1160 			if (rs->rs_disable && error)
1161 				*error = ENOTSUP;
1162 			NET_EPOCH_EXIT(et);
1163 			return (NULL);
1164 		}
1165 		KASSERT((tifp != ifp),
1166 			("Lookup failure ifp:%p inp:%p rt_find_real_interface() returns the same interface tifp:%p?\n",
1167 			 ifp, inp, tifp));
1168 		ifp = tifp;
1169 		goto use_real_interface;
1170 	}
1171 	if (rs->rs_flow_limit &&
1172 	    ((rs->rs_flows_using + 1) > rs->rs_flow_limit)) {
1173 		if (error)
1174 			*error = ENOSPC;
1175 		NET_EPOCH_EXIT(et);
1176 		return (NULL);
1177 	}
1178 	rte = tcp_find_suitable_rate(rs, bytes_per_sec, flags, lower_rate);
1179 	if (rte) {
1180 		err = in_pcbattach_txrtlmt(inp, oifp,
1181 		    inp->inp_flowtype,
1182 		    inp->inp_flowid,
1183 		    rte->rate,
1184 		    &inp->inp_snd_tag);
1185 		if (err) {
1186 			/* Failed to attach */
1187 			if (error)
1188 				*error = err;
1189 			rte = NULL;
1190 		} else {
1191 			KASSERT((inp->inp_snd_tag != NULL) ,
1192 				("Setup rate has no snd_tag inp:%p rte:%p rate:%llu rs:%p",
1193 				 inp, rte, (unsigned long long)rte->rate, rs));
1194 #ifdef INET
1195 			counter_u64_add(rate_limit_new, 1);
1196 #endif
1197 		}
1198 	}
1199 	if (rte) {
1200 		/*
1201 		 * We use an atomic here for accounting so we don't have to
1202 		 * use locks when freeing.
1203 		 */
1204 		atomic_add_64(&rs->rs_flows_using, 1);
1205 	}
1206 	NET_EPOCH_EXIT(et);
1207 	return (rte);
1208 }
1209 
1210 static void
1211 tcp_rl_ifnet_link(void *arg __unused, struct ifnet *ifp, int link_state)
1212 {
1213 	int error;
1214 	struct tcp_rate_set *rs;
1215 	struct epoch_tracker et;
1216 
1217 	if (((ifp->if_capenable & IFCAP_TXRTLMT) == 0) ||
1218 	    (link_state != LINK_STATE_UP)) {
1219 		/*
1220 		 * We only care on an interface going up that is rate-limit
1221 		 * capable.
1222 		 */
1223 		return;
1224 	}
1225 	NET_EPOCH_ENTER(et);
1226 	mtx_lock(&rs_mtx);
1227 	rs = find_rs_for_ifp(ifp);
1228 	if (rs) {
1229 		/* We already have initialized this guy */
1230 		mtx_unlock(&rs_mtx);
1231 		NET_EPOCH_EXIT(et);
1232 		return;
1233 	}
1234 	mtx_unlock(&rs_mtx);
1235 	NET_EPOCH_EXIT(et);
1236 	rt_setup_new_rs(ifp, &error);
1237 }
1238 
1239 static void
1240 tcp_rl_ifnet_departure(void *arg __unused, struct ifnet *ifp)
1241 {
1242 	struct tcp_rate_set *rs;
1243 	struct epoch_tracker et;
1244 	int i;
1245 
1246 	NET_EPOCH_ENTER(et);
1247 	mtx_lock(&rs_mtx);
1248 	rs = find_rs_for_ifp(ifp);
1249 	if (rs) {
1250 		CK_LIST_REMOVE(rs, next);
1251 		rs_number_alive--;
1252 		rs->rs_flags |= RS_IS_DEAD;
1253 		for (i = 0; i < rs->rs_rate_cnt; i++) {
1254 			if (rs->rs_rlt[i].flags & HDWRPACE_TAGPRESENT) {
1255 				in_pcbdetach_tag(rs->rs_rlt[i].tag);
1256 				rs->rs_rlt[i].tag = NULL;
1257 			}
1258 			rs->rs_rlt[i].flags = HDWRPACE_IFPDEPARTED;
1259 		}
1260 		if (rs->rs_flows_using == 0)
1261 			rs_defer_destroy(rs);
1262 	}
1263 	mtx_unlock(&rs_mtx);
1264 	NET_EPOCH_EXIT(et);
1265 }
1266 
1267 static void
1268 tcp_rl_shutdown(void *arg __unused, int howto __unused)
1269 {
1270 	struct tcp_rate_set *rs, *nrs;
1271 	struct epoch_tracker et;
1272 	int i;
1273 
1274 	NET_EPOCH_ENTER(et);
1275 	mtx_lock(&rs_mtx);
1276 	CK_LIST_FOREACH_SAFE(rs, &int_rs, next, nrs) {
1277 		CK_LIST_REMOVE(rs, next);
1278 		rs_number_alive--;
1279 		rs->rs_flags |= RS_IS_DEAD;
1280 		for (i = 0; i < rs->rs_rate_cnt; i++) {
1281 			if (rs->rs_rlt[i].flags & HDWRPACE_TAGPRESENT) {
1282 				in_pcbdetach_tag(rs->rs_rlt[i].tag);
1283 				rs->rs_rlt[i].tag = NULL;
1284 			}
1285 			rs->rs_rlt[i].flags = HDWRPACE_IFPDEPARTED;
1286 		}
1287 		if (rs->rs_flows_using == 0)
1288 			rs_defer_destroy(rs);
1289 	}
1290 	mtx_unlock(&rs_mtx);
1291 	NET_EPOCH_EXIT(et);
1292 }
1293 
1294 const struct tcp_hwrate_limit_table *
1295 tcp_set_pacing_rate(struct tcpcb *tp, struct ifnet *ifp,
1296     uint64_t bytes_per_sec, int flags, int *error, uint64_t *lower_rate)
1297 {
1298 	const struct tcp_hwrate_limit_table *rte;
1299 #ifdef KERN_TLS
1300 	struct ktls_session *tls;
1301 #endif
1302 
1303 	INP_WLOCK_ASSERT(tp->t_inpcb);
1304 
1305 	if (tp->t_inpcb->inp_snd_tag == NULL) {
1306 		/*
1307 		 * We are setting up a rate for the first time.
1308 		 */
1309 		if ((ifp->if_capenable & IFCAP_TXRTLMT) == 0) {
1310 			/* Not supported by the egress */
1311 			if (error)
1312 				*error = ENODEV;
1313 			return (NULL);
1314 		}
1315 #ifdef KERN_TLS
1316 		tls = NULL;
1317 		if (tp->t_inpcb->inp_socket->so_snd.sb_flags & SB_TLS_IFNET) {
1318 			tls = tp->t_inpcb->inp_socket->so_snd.sb_tls_info;
1319 
1320 			if ((ifp->if_capenable & IFCAP_TXTLS_RTLMT) == 0 ||
1321 			    tls->mode != TCP_TLS_MODE_IFNET) {
1322 				if (error)
1323 					*error = ENODEV;
1324 				return (NULL);
1325 			}
1326 		}
1327 #endif
1328 		rte = rt_setup_rate(tp->t_inpcb, ifp, bytes_per_sec, flags, error, lower_rate);
1329 		if (rte)
1330 			rl_increment_using(rte);
1331 #ifdef KERN_TLS
1332 		if (rte != NULL && tls != NULL && tls->snd_tag != NULL) {
1333 			/*
1334 			 * Fake a route change error to reset the TLS
1335 			 * send tag.  This will convert the existing
1336 			 * tag to a TLS ratelimit tag.
1337 			 */
1338 			MPASS(tls->snd_tag->type == IF_SND_TAG_TYPE_TLS);
1339 			ktls_output_eagain(tp->t_inpcb, tls);
1340 		}
1341 #endif
1342 	} else {
1343 		/*
1344 		 * We are modifying a rate, wrong interface?
1345 		 */
1346 		if (error)
1347 			*error = EINVAL;
1348 		rte = NULL;
1349 	}
1350 	if (rte != NULL) {
1351 		tp->t_pacing_rate = rte->rate;
1352 		*error = 0;
1353 	}
1354 	return (rte);
1355 }
1356 
1357 const struct tcp_hwrate_limit_table *
1358 tcp_chg_pacing_rate(const struct tcp_hwrate_limit_table *crte,
1359     struct tcpcb *tp, struct ifnet *ifp,
1360     uint64_t bytes_per_sec, int flags, int *error, uint64_t *lower_rate)
1361 {
1362 	const struct tcp_hwrate_limit_table *nrte;
1363 	const struct tcp_rate_set *rs;
1364 #ifdef KERN_TLS
1365 	struct ktls_session *tls = NULL;
1366 #endif
1367 	int err;
1368 
1369 	INP_WLOCK_ASSERT(tp->t_inpcb);
1370 
1371 	if (crte == NULL) {
1372 		/* Wrong interface */
1373 		if (error)
1374 			*error = EINVAL;
1375 		return (NULL);
1376 	}
1377 
1378 #ifdef KERN_TLS
1379 	if (tp->t_inpcb->inp_socket->so_snd.sb_flags & SB_TLS_IFNET) {
1380 		tls = tp->t_inpcb->inp_socket->so_snd.sb_tls_info;
1381 		MPASS(tls->mode == TCP_TLS_MODE_IFNET);
1382 		if (tls->snd_tag != NULL &&
1383 		    tls->snd_tag->type != IF_SND_TAG_TYPE_TLS_RATE_LIMIT) {
1384 			/*
1385 			 * NIC probably doesn't support ratelimit TLS
1386 			 * tags if it didn't allocate one when an
1387 			 * existing rate was present, so ignore.
1388 			 */
1389 			if (error)
1390 				*error = EOPNOTSUPP;
1391 			return (NULL);
1392 		}
1393 	}
1394 #endif
1395 	if (tp->t_inpcb->inp_snd_tag == NULL) {
1396 		/* Wrong interface */
1397 		if (error)
1398 			*error = EINVAL;
1399 		return (NULL);
1400 	}
1401 	rs = crte->ptbl;
1402 	if ((rs->rs_flags & RS_IS_DEAD) ||
1403 	    (crte->flags & HDWRPACE_IFPDEPARTED)) {
1404 		/* Release the rate, and try anew */
1405 
1406 		tcp_rel_pacing_rate(crte, tp);
1407 		nrte = tcp_set_pacing_rate(tp, ifp,
1408 		    bytes_per_sec, flags, error, lower_rate);
1409 		return (nrte);
1410 	}
1411 	nrte = tcp_find_suitable_rate(rs, bytes_per_sec, flags, lower_rate);
1412 	if (nrte == crte) {
1413 		/* No change */
1414 		if (error)
1415 			*error = 0;
1416 		return (crte);
1417 	}
1418 	if (nrte == NULL) {
1419 		/* Release the old rate */
1420 		if (error)
1421 			*error = ENOENT;
1422 		tcp_rel_pacing_rate(crte, tp);
1423 		return (NULL);
1424 	}
1425 	rl_decrement_using(crte);
1426 	rl_increment_using(nrte);
1427 	/* Change rates to our new entry */
1428 #ifdef KERN_TLS
1429 	if (tls != NULL)
1430 		err = ktls_modify_txrtlmt(tls, nrte->rate);
1431 	else
1432 #endif
1433 		err = in_pcbmodify_txrtlmt(tp->t_inpcb, nrte->rate);
1434 	if (err) {
1435 		rl_decrement_using(nrte);
1436 		/* Do we still have a snd-tag attached? */
1437 		if (tp->t_inpcb->inp_snd_tag)
1438 			in_pcbdetach_txrtlmt(tp->t_inpcb);
1439 		if (error)
1440 			*error = err;
1441 		return (NULL);
1442 	} else {
1443 #ifdef INET
1444 		counter_u64_add(rate_limit_chg, 1);
1445 #endif
1446 	}
1447 	if (error)
1448 		*error = 0;
1449 	tp->t_pacing_rate = nrte->rate;
1450 	return (nrte);
1451 }
1452 
1453 void
1454 tcp_rel_pacing_rate(const struct tcp_hwrate_limit_table *crte, struct tcpcb *tp)
1455 {
1456 	const struct tcp_rate_set *crs;
1457 	struct tcp_rate_set *rs;
1458 	uint64_t pre;
1459 
1460 	INP_WLOCK_ASSERT(tp->t_inpcb);
1461 
1462 	tp->t_pacing_rate = -1;
1463 	crs = crte->ptbl;
1464 	/*
1465 	 * Now we must break the const
1466 	 * in order to release our refcount.
1467 	 */
1468 	rs = __DECONST(struct tcp_rate_set *, crs);
1469 	rl_decrement_using(crte);
1470 	pre = atomic_fetchadd_64(&rs->rs_flows_using, -1);
1471 	if (pre == 1) {
1472 		struct epoch_tracker et;
1473 
1474 		NET_EPOCH_ENTER(et);
1475 		mtx_lock(&rs_mtx);
1476 		/*
1477 		 * Is it dead?
1478 		 */
1479 		if (rs->rs_flags & RS_IS_DEAD)
1480 			rs_defer_destroy(rs);
1481 		mtx_unlock(&rs_mtx);
1482 		NET_EPOCH_EXIT(et);
1483 	}
1484 
1485 	/*
1486 	 * XXX: If this connection is using ifnet TLS, should we
1487 	 * switch it to using an unlimited rate, or perhaps use
1488 	 * ktls_output_eagain() to reset the send tag to a plain
1489 	 * TLS tag?
1490 	 */
1491 	in_pcbdetach_txrtlmt(tp->t_inpcb);
1492 }
1493 
1494 #define ONE_POINT_TWO_MEG 150000 /* 1.2 megabits in bytes */
1495 #define ONE_HUNDRED_MBPS 12500000	/* 100Mbps in bytes per second */
1496 #define FIVE_HUNDRED_MBPS 62500000	/* 500Mbps in bytes per second */
1497 #define MAX_MSS_SENT 43	/* 43 mss = 43 x 1500 = 64,500 bytes */
1498 
1499 static void
1500 tcp_log_pacing_size(struct tcpcb *tp, uint64_t bw, uint32_t segsiz, uint32_t new_tso,
1501 		    uint64_t hw_rate, uint32_t time_between, uint32_t calc_time_between,
1502 		    uint32_t segs, uint32_t res_div, uint16_t mult, uint8_t mod)
1503 {
1504 	if (tp->t_logstate != TCP_LOG_STATE_OFF) {
1505 		union tcp_log_stackspecific log;
1506 		struct timeval tv;
1507 		uint32_t cts;
1508 
1509 		memset(&log, 0, sizeof(log));
1510 		cts = tcp_get_usecs(&tv);
1511 		log.u_bbr.flex1 = segsiz;
1512 		log.u_bbr.flex2 = new_tso;
1513 		log.u_bbr.flex3 = time_between;
1514 		log.u_bbr.flex4 = calc_time_between;
1515 		log.u_bbr.flex5 = segs;
1516 		log.u_bbr.flex6 = res_div;
1517 		log.u_bbr.flex7 = mult;
1518 		log.u_bbr.flex8 = mod;
1519 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
1520 		log.u_bbr.cur_del_rate = bw;
1521 		log.u_bbr.delRate = hw_rate;
1522 		TCP_LOG_EVENTP(tp, NULL,
1523 		    &tp->t_inpcb->inp_socket->so_rcv,
1524 		    &tp->t_inpcb->inp_socket->so_snd,
1525 		    TCP_HDWR_PACE_SIZE, 0,
1526 		    0, &log, false, &tv);
1527 	}
1528 }
1529 
1530 uint32_t
1531 tcp_get_pacing_burst_size (struct tcpcb *tp, uint64_t bw, uint32_t segsiz, int can_use_1mss,
1532    const struct tcp_hwrate_limit_table *te, int *err)
1533 {
1534 	/*
1535 	 * We use the google formula to calculate the
1536 	 * TSO size. I.E.
1537 	 * bw < 24Meg
1538 	 *   tso = 2mss
1539 	 * else
1540 	 *   tso = min(bw/1000, 64k)
1541 	 *
1542 	 * Note for these calculations we ignore the
1543 	 * packet overhead (enet hdr, ip hdr and tcp hdr).
1544 	 */
1545 	uint64_t lentim, res, bytes;
1546 	uint32_t new_tso, min_tso_segs;
1547 
1548 	bytes = bw / 1000;
1549 	if (bytes > (64 * 1000))
1550 		bytes = 64 * 1000;
1551 	/* Round up */
1552 	new_tso = (bytes + segsiz - 1) / segsiz;
1553 	if (can_use_1mss && (bw < ONE_POINT_TWO_MEG))
1554 		min_tso_segs = 1;
1555 	else
1556 		min_tso_segs = 2;
1557 	if (rs_floor_mss && (new_tso < rs_floor_mss))
1558 		new_tso = rs_floor_mss;
1559 	else if (new_tso < min_tso_segs)
1560 		new_tso = min_tso_segs;
1561 	if (new_tso > MAX_MSS_SENT)
1562 		new_tso = MAX_MSS_SENT;
1563 	new_tso *= segsiz;
1564  	tcp_log_pacing_size(tp, bw, segsiz, new_tso,
1565 			    0, 0, 0, 0, 0, 0, 1);
1566 	/*
1567 	 * If we are not doing hardware pacing
1568 	 * then we are done.
1569 	 */
1570 	if (te == NULL) {
1571 		if (err)
1572 			*err = 0;
1573 		return(new_tso);
1574 	}
1575 	/*
1576 	 * For hardware pacing we look at the
1577 	 * rate you are sending at and compare
1578 	 * that to the rate you have in hardware.
1579 	 *
1580 	 * If the hardware rate is slower than your
1581 	 * software rate then you are in error and
1582 	 * we will build a queue in our hardware whic
1583 	 * is probably not desired, in such a case
1584 	 * just return the non-hardware TSO size.
1585 	 *
1586 	 * If the rate in hardware is faster (which
1587 	 * it should be) then look at how long it
1588 	 * takes to send one ethernet segment size at
1589 	 * your b/w and compare that to the time it
1590 	 * takes to send at the rate you had selected.
1591 	 *
1592 	 * If your time is greater (which we hope it is)
1593 	 * we get the delta between the two, and then
1594 	 * divide that into your pacing time. This tells
1595 	 * us how many MSS you can send down at once (rounded up).
1596 	 *
1597 	 * Note we also double this value if the b/w is over
1598 	 * 100Mbps. If its over 500meg we just set you to the
1599 	 * max (43 segments).
1600 	 */
1601 	if (te->rate > FIVE_HUNDRED_MBPS)
1602 		goto max;
1603 	if (te->rate == bw) {
1604 		/* We are pacing at exactly the hdwr rate */
1605 max:
1606 		tcp_log_pacing_size(tp, bw, segsiz, new_tso,
1607 				    te->rate, te->time_between, (uint32_t)0,
1608 				    (segsiz * MAX_MSS_SENT), 0, 0, 3);
1609 		return (segsiz * MAX_MSS_SENT);
1610 	}
1611 	lentim = ETHERNET_SEGMENT_SIZE * USECS_IN_SECOND;
1612 	res = lentim / bw;
1613 	if (res > te->time_between) {
1614 		uint32_t delta, segs, res_div;
1615 
1616 		res_div = ((res * num_of_waits_allowed) + wait_time_floor);
1617 		delta = res - te->time_between;
1618 		segs = (res_div + delta - 1)/delta;
1619 		if (segs < min_tso_segs)
1620 			segs = min_tso_segs;
1621 		if (segs < rs_hw_floor_mss)
1622 			segs = rs_hw_floor_mss;
1623 		if (segs > MAX_MSS_SENT)
1624 			segs = MAX_MSS_SENT;
1625 		segs *= segsiz;
1626 		tcp_log_pacing_size(tp, bw, segsiz, new_tso,
1627 				    te->rate, te->time_between, (uint32_t)res,
1628 				    segs, res_div, 1, 3);
1629 		if (err)
1630 			*err = 0;
1631 		if (segs < new_tso) {
1632 			/* unexpected ? */
1633 			return(new_tso);
1634 		} else {
1635 			return (segs);
1636 		}
1637 	} else {
1638 		/*
1639 		 * Your time is smaller which means
1640 		 * we will grow a queue on our
1641 		 * hardware. Send back the non-hardware
1642 		 * rate.
1643 		 */
1644 		tcp_log_pacing_size(tp, bw, segsiz, new_tso,
1645 				    te->rate, te->time_between, (uint32_t)res,
1646 				    0, 0, 0, 4);
1647 		if (err)
1648 			*err = -1;
1649 		return (new_tso);
1650 	}
1651 }
1652 
1653 uint64_t
1654 tcp_hw_highest_rate_ifp(struct ifnet *ifp, struct inpcb *inp)
1655 {
1656 	struct epoch_tracker et;
1657 	struct tcp_rate_set *rs;
1658 	uint64_t rate_ret;
1659 
1660 	NET_EPOCH_ENTER(et);
1661 use_next_interface:
1662 	rs = find_rs_for_ifp(ifp);
1663 	if (rs == NULL) {
1664 		/* This interface does not do ratelimiting */
1665 		rate_ret = 0;
1666 	} else if (rs->rs_flags & RS_IS_DEFF) {
1667 		/* We need to find the real interface */
1668 		struct ifnet *tifp;
1669 
1670 		tifp = rt_find_real_interface(ifp, inp, NULL);
1671 		if (tifp == NULL) {
1672 			NET_EPOCH_EXIT(et);
1673 			return (0);
1674 		}
1675 		ifp = tifp;
1676 		goto use_next_interface;
1677 	} else {
1678 		/* Lets return the highest rate this guy has */
1679 		rate_ret = rs->rs_rlt[rs->rs_highest_valid].rate;
1680 	}
1681 	NET_EPOCH_EXIT(et);
1682 	return(rate_ret);
1683 }
1684 
1685 static eventhandler_tag rl_ifnet_departs;
1686 static eventhandler_tag rl_ifnet_arrives;
1687 static eventhandler_tag rl_shutdown_start;
1688 
1689 static void
1690 tcp_rs_init(void *st __unused)
1691 {
1692 	CK_LIST_INIT(&int_rs);
1693 	rs_number_alive = 0;
1694 	rs_number_dead = 0;
1695 	mtx_init(&rs_mtx, "tcp_rs_mtx", "rsmtx", MTX_DEF);
1696 	rl_ifnet_departs = EVENTHANDLER_REGISTER(ifnet_departure_event,
1697 	    tcp_rl_ifnet_departure,
1698 	    NULL, EVENTHANDLER_PRI_ANY);
1699 	rl_ifnet_arrives = EVENTHANDLER_REGISTER(ifnet_link_event,
1700 	    tcp_rl_ifnet_link,
1701 	    NULL, EVENTHANDLER_PRI_ANY);
1702 	rl_shutdown_start = EVENTHANDLER_REGISTER(shutdown_pre_sync,
1703 	    tcp_rl_shutdown, NULL,
1704 	    SHUTDOWN_PRI_FIRST);
1705 	printf("TCP_ratelimit: Is now initialized\n");
1706 }
1707 
1708 SYSINIT(tcp_rl_init, SI_SUB_SMP + 1, SI_ORDER_ANY, tcp_rs_init, NULL);
1709 #endif
1710