3 * SPDX-License-Identifier: BSD-3-Clause
5 * Copyright (c) 2018-2020
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
31 * Author: Randall Stewart <rrs@netflix.com>
34 #include <sys/cdefs.h>
36 #include "opt_inet6.h"
37 #include "opt_ipsec.h"
38 #include "opt_tcpdebug.h"
39 #include "opt_ratelimit.h"
40 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/sysctl.h>
47 #include <sys/eventhandler.h>
48 #include <sys/mutex.h>
51 #include <net/if_var.h>
52 #include <netinet/in.h>
53 #include <netinet/in_pcb.h>
54 #define TCPSTATES /* for logging */
55 #include <netinet/tcp_var.h>
57 #include <netinet6/tcp6_var.h>
59 #include <netinet/tcp_hpts.h>
60 #include <netinet/tcp_log_buf.h>
61 #include <netinet/tcp_ratelimit.h>
62 #ifndef USECS_IN_SECOND
63 #define USECS_IN_SECOND 1000000
66 * For the purposes of each send, what is the size
67 * of an ethernet frame.
69 MALLOC_DEFINE(M_TCPPACE, "tcp_hwpace", "TCP Hardware pacing memory");
73 * The following preferred table will seem weird to
74 * the casual viewer. Why do we not have any rates below
75 * 1Mbps? Why do we have a rate at 1.44Mbps called common?
76 * Why do the rates cluster in the 1-100Mbps range more
77 * than others? Why does the table jump around at the beginnign
78 * and then be more consistently raising?
80 * Let me try to answer those questions. A lot of
81 * this is dependant on the hardware. We have three basic
82 * supporters of rate limiting
84 * Chelsio - Supporting 16 configurable rates.
85 * Mlx - c4 supporting 13 fixed rates.
86 * Mlx - c5 & c6 supporting 127 configurable rates.
88 * The c4 is why we have a common rate that is available
89 * in all rate tables. This is a selected rate from the
90 * c4 table and we assure its available in all ratelimit
91 * tables. This way the tcp_ratelimit code has an assured
92 * rate it should always be able to get. This answers a
93 * couple of the questions above.
95 * So what about the rest, well the table is built to
96 * try to get the most out of a joint hardware/software
97 * pacing system. The software pacer will always pick
98 * a rate higher than the b/w that it is estimating
100 * on the path. This is done for two reasons.
101 * a) So we can discover more b/w
103 * b) So we can send a block of MSS's down and then
104 * have the software timer go off after the previous
105 * send is completely out of the hardware.
107 * But when we do <b> we don't want to have the delay
108 * between the last packet sent by the hardware be
109 * excessively long (to reach our desired rate).
111 * So let me give an example for clarity.
113 * Lets assume that the tcp stack sees that 29,110,000 bps is
114 * what the bw of the path is. The stack would select the
115 * rate 31Mbps. 31Mbps means that each send that is done
116 * by the hardware will cause a 390 micro-second gap between
117 * the packets sent at that rate. For 29,110,000 bps we
118 * would need 416 micro-seconds gap between each send.
120 * Note that are calculating a complete time for pacing
121 * which includes the ethernet, IP and TCP overhead. So
122 * a full 1514 bytes is used for the above calculations.
123 * My testing has shown that both cards are also using this
124 * as their basis i.e. full payload size of the ethernet frame.
125 * The TCP stack caller needs to be aware of this and make the
126 * appropriate overhead calculations be included in its choices.
128 * Now, continuing our example, we pick a MSS size based on the
129 * delta between the two rates (416 - 390) divided into the rate
130 * we really wish to send at rounded up. That results in a MSS
131 * send of 17 mss's at once. The hardware then will
132 * run out of data in a single 17MSS send in 6,630 micro-seconds.
134 * On the other hand the software pacer will send more data
135 * in 7,072 micro-seconds. This means that we will refill
136 * the hardware 52 microseconds after it would have sent
137 * next if it had not ran out of data. This is a win since we are
138 * only sending every 7ms or so and yet all the packets are spaced on
139 * the wire with 94% of what they should be and only
140 * the last packet is delayed extra to make up for the
143 * Note that the above formula has two important caveat.
144 * If we are above (b/w wise) over 100Mbps we double the result
145 * of the MSS calculation. The second caveat is if we are 500Mbps
146 * or more we just send the maximum MSS at once i.e. 45MSS. At
147 * the higher b/w's even the cards have limits to what times (timer granularity)
148 * they can insert between packets and start to send more than one
149 * packet at a time on the wire.
152 #define COMMON_RATE 180500
153 const uint64_t desired_rates[] = {
154 122500, /* 1Mbps - rate 1 */
155 180500, /* 1.44Mpbs - rate 2 common rate */
156 375000, /* 3Mbps - rate 3 */
157 625000, /* 5Mbps - rate 4 */
158 1250000, /* 10Mbps - rate 5 */
159 1875000, /* 15Mbps - rate 6 */
160 2500000, /* 20Mbps - rate 7 */
161 3125000, /* 25Mbps - rate 8 */
162 3750000, /* 30Mbps - rate 9 */
163 4375000, /* 35Mbps - rate 10 */
164 5000000, /* 40Meg - rate 11 */
165 6250000, /* 50Mbps - rate 12 */
166 12500000, /* 100Mbps - rate 13 */
167 25000000, /* 200Mbps - rate 14 */
168 50000000, /* 400Mbps - rate 15 */
169 100000000, /* 800Mbps - rate 16 */
170 5625000, /* 45Mbps - rate 17 */
171 6875000, /* 55Mbps - rate 19 */
172 7500000, /* 60Mbps - rate 20 */
173 8125000, /* 65Mbps - rate 21 */
174 8750000, /* 70Mbps - rate 22 */
175 9375000, /* 75Mbps - rate 23 */
176 10000000, /* 80Mbps - rate 24 */
177 10625000, /* 85Mbps - rate 25 */
178 11250000, /* 90Mbps - rate 26 */
179 11875000, /* 95Mbps - rate 27 */
180 12500000, /* 100Mbps - rate 28 */
181 13750000, /* 110Mbps - rate 29 */
182 15000000, /* 120Mbps - rate 30 */
183 16250000, /* 130Mbps - rate 31 */
184 17500000, /* 140Mbps - rate 32 */
185 18750000, /* 150Mbps - rate 33 */
186 20000000, /* 160Mbps - rate 34 */
187 21250000, /* 170Mbps - rate 35 */
188 22500000, /* 180Mbps - rate 36 */
189 23750000, /* 190Mbps - rate 37 */
190 26250000, /* 210Mbps - rate 38 */
191 27500000, /* 220Mbps - rate 39 */
192 28750000, /* 230Mbps - rate 40 */
193 30000000, /* 240Mbps - rate 41 */
194 31250000, /* 250Mbps - rate 42 */
195 34375000, /* 275Mbps - rate 43 */
196 37500000, /* 300Mbps - rate 44 */
197 40625000, /* 325Mbps - rate 45 */
198 43750000, /* 350Mbps - rate 46 */
199 46875000, /* 375Mbps - rate 47 */
200 53125000, /* 425Mbps - rate 48 */
201 56250000, /* 450Mbps - rate 49 */
202 59375000, /* 475Mbps - rate 50 */
203 62500000, /* 500Mbps - rate 51 */
204 68750000, /* 550Mbps - rate 52 */
205 75000000, /* 600Mbps - rate 53 */
206 81250000, /* 650Mbps - rate 54 */
207 87500000, /* 700Mbps - rate 55 */
208 93750000, /* 750Mbps - rate 56 */
209 106250000, /* 850Mbps - rate 57 */
210 112500000, /* 900Mbps - rate 58 */
211 125000000, /* 1Gbps - rate 59 */
212 156250000, /* 1.25Gps - rate 60 */
213 187500000, /* 1.5Gps - rate 61 */
214 218750000, /* 1.75Gps - rate 62 */
215 250000000, /* 2Gbps - rate 63 */
216 281250000, /* 2.25Gps - rate 64 */
217 312500000, /* 2.5Gbps - rate 65 */
218 343750000, /* 2.75Gbps - rate 66 */
219 375000000, /* 3Gbps - rate 67 */
220 500000000, /* 4Gbps - rate 68 */
221 625000000, /* 5Gbps - rate 69 */
222 750000000, /* 6Gbps - rate 70 */
223 875000000, /* 7Gbps - rate 71 */
224 1000000000, /* 8Gbps - rate 72 */
225 1125000000, /* 9Gbps - rate 73 */
226 1250000000, /* 10Gbps - rate 74 */
227 1875000000, /* 15Gbps - rate 75 */
228 2500000000 /* 20Gbps - rate 76 */
231 #define MAX_HDWR_RATES (sizeof(desired_rates)/sizeof(uint64_t))
232 #define RS_ORDERED_COUNT 16 /*
233 * Number that are in order
234 * at the beginning of the table,
235 * over this a sort is required.
237 #define RS_NEXT_ORDER_GROUP 16 /*
238 * The point in our table where
239 * we come fill in a second ordered
240 * group (index wise means -1).
242 #define ALL_HARDWARE_RATES 1004 /*
243 * 1Meg - 1Gig in 1 Meg steps
244 * plus 100, 200k and 500k and
248 #define RS_ONE_MEGABIT_PERSEC 1000000
249 #define RS_ONE_GIGABIT_PERSEC 1000000000
250 #define RS_TEN_GIGABIT_PERSEC 10000000000
252 static struct head_tcp_rate_set int_rs;
253 static struct mtx rs_mtx;
254 uint32_t rs_number_alive;
255 uint32_t rs_number_dead;
256 static uint32_t rs_floor_mss = 0;
257 static uint32_t wait_time_floor = 8000; /* 8 ms */
258 static uint32_t rs_hw_floor_mss = 16;
259 static uint32_t num_of_waits_allowed = 1; /* How many time blocks are we willing to wait */
261 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, rl, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
262 "TCP Ratelimit stats");
263 SYSCTL_UINT(_net_inet_tcp_rl, OID_AUTO, alive, CTLFLAG_RW,
265 "Number of interfaces initialized for ratelimiting");
266 SYSCTL_UINT(_net_inet_tcp_rl, OID_AUTO, dead, CTLFLAG_RW,
268 "Number of interfaces departing from ratelimiting");
269 SYSCTL_UINT(_net_inet_tcp_rl, OID_AUTO, floor_mss, CTLFLAG_RW,
271 "Number of MSS that will override the normal minimums (0 means don't enforce)");
272 SYSCTL_UINT(_net_inet_tcp_rl, OID_AUTO, wait_floor, CTLFLAG_RW,
273 &wait_time_floor, 2000,
274 "Has b/w increases what is the wait floor we are willing to wait at the end?");
275 SYSCTL_UINT(_net_inet_tcp_rl, OID_AUTO, time_blocks, CTLFLAG_RW,
276 &num_of_waits_allowed, 1,
277 "How many time blocks on the end should software pacing be willing to wait?");
279 SYSCTL_UINT(_net_inet_tcp_rl, OID_AUTO, hw_floor_mss, CTLFLAG_RW,
280 &rs_hw_floor_mss, 16,
281 "Number of mss that are a minum for hardware pacing?");
285 rl_add_syctl_entries(struct sysctl_oid *rl_sysctl_root, struct tcp_rate_set *rs)
288 * Add sysctl entries for thus interface.
290 if (rs->rs_flags & RS_INTF_NO_SUP) {
291 SYSCTL_ADD_S32(&rs->sysctl_ctx,
292 SYSCTL_CHILDREN(rl_sysctl_root),
293 OID_AUTO, "disable", CTLFLAG_RD,
295 "Disable this interface from new hdwr limiting?");
297 SYSCTL_ADD_S32(&rs->sysctl_ctx,
298 SYSCTL_CHILDREN(rl_sysctl_root),
299 OID_AUTO, "disable", CTLFLAG_RW,
301 "Disable this interface from new hdwr limiting?");
303 SYSCTL_ADD_S32(&rs->sysctl_ctx,
304 SYSCTL_CHILDREN(rl_sysctl_root),
305 OID_AUTO, "minseg", CTLFLAG_RW,
307 "What is the minimum we need to send on this interface?");
308 SYSCTL_ADD_U64(&rs->sysctl_ctx,
309 SYSCTL_CHILDREN(rl_sysctl_root),
310 OID_AUTO, "flow_limit", CTLFLAG_RW,
311 &rs->rs_flow_limit, 0,
312 "What is the limit for number of flows (0=unlimited)?");
313 SYSCTL_ADD_S32(&rs->sysctl_ctx,
314 SYSCTL_CHILDREN(rl_sysctl_root),
315 OID_AUTO, "highest", CTLFLAG_RD,
316 &rs->rs_highest_valid, 0,
317 "Highest valid rate");
318 SYSCTL_ADD_S32(&rs->sysctl_ctx,
319 SYSCTL_CHILDREN(rl_sysctl_root),
320 OID_AUTO, "lowest", CTLFLAG_RD,
321 &rs->rs_lowest_valid, 0,
322 "Lowest valid rate");
323 SYSCTL_ADD_S32(&rs->sysctl_ctx,
324 SYSCTL_CHILDREN(rl_sysctl_root),
325 OID_AUTO, "flags", CTLFLAG_RD,
327 "What lags are on the entry?");
328 SYSCTL_ADD_S32(&rs->sysctl_ctx,
329 SYSCTL_CHILDREN(rl_sysctl_root),
330 OID_AUTO, "numrates", CTLFLAG_RD,
332 "How many rates re there?");
333 SYSCTL_ADD_U64(&rs->sysctl_ctx,
334 SYSCTL_CHILDREN(rl_sysctl_root),
335 OID_AUTO, "flows_using", CTLFLAG_RD,
336 &rs->rs_flows_using, 0,
337 "How many flows are using this interface now?");
338 #ifdef DETAILED_RATELIMIT_SYSCTL
339 if (rs->rs_rlt && rs->rs_rate_cnt > 0) {
340 /* Lets display the rates */
342 struct sysctl_oid *rl_rates;
343 struct sysctl_oid *rl_rate_num;
345 rl_rates = SYSCTL_ADD_NODE(&rs->sysctl_ctx,
346 SYSCTL_CHILDREN(rl_sysctl_root),
349 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
351 for( i = 0; i < rs->rs_rate_cnt; i++) {
352 sprintf(rate_num, "%d", i);
353 rl_rate_num = SYSCTL_ADD_NODE(&rs->sysctl_ctx,
354 SYSCTL_CHILDREN(rl_rates),
357 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
359 SYSCTL_ADD_U32(&rs->sysctl_ctx,
360 SYSCTL_CHILDREN(rl_rate_num),
361 OID_AUTO, "flags", CTLFLAG_RD,
362 &rs->rs_rlt[i].flags, 0,
363 "Flags on this rate");
364 SYSCTL_ADD_U32(&rs->sysctl_ctx,
365 SYSCTL_CHILDREN(rl_rate_num),
366 OID_AUTO, "pacetime", CTLFLAG_RD,
367 &rs->rs_rlt[i].time_between, 0,
368 "Time hardware inserts between 1500 byte sends");
369 SYSCTL_ADD_LONG(&rs->sysctl_ctx,
370 SYSCTL_CHILDREN(rl_rate_num),
371 OID_AUTO, "rate", CTLFLAG_RD,
373 "Rate in bytes per second");
374 SYSCTL_ADD_LONG(&rs->sysctl_ctx,
375 SYSCTL_CHILDREN(rl_rate_num),
376 OID_AUTO, "using", CTLFLAG_RD,
377 &rs->rs_rlt[i].using,
378 "Number of flows using");
379 SYSCTL_ADD_LONG(&rs->sysctl_ctx,
380 SYSCTL_CHILDREN(rl_rate_num),
381 OID_AUTO, "enobufs", CTLFLAG_RD,
382 &rs->rs_rlt[i].rs_num_enobufs,
383 "Number of enobufs logged on this rate");
391 rs_destroy(epoch_context_t ctx)
393 struct tcp_rate_set *rs;
396 rs = __containerof(ctx, struct tcp_rate_set, rs_epoch_ctx);
399 rs->rs_flags &= ~RS_FUNERAL_SCHD;
401 * In theory its possible (but unlikely)
402 * that while the delete was occuring
403 * and we were applying the DEAD flag
404 * someone slipped in and found the
405 * interface in a lookup. While we
406 * decided rs_flows_using were 0 and
407 * scheduling the epoch_call, the other
408 * thread incremented rs_flow_using. This
409 * is because users have a pointer and
410 * we only use the rs_flows_using in an
411 * atomic fashion, i.e. the other entities
412 * are not protected. To assure this did
413 * not occur, we check rs_flows_using here
416 do_free_rs = (rs->rs_flows_using == 0);
421 sysctl_ctx_free(&rs->sysctl_ctx);
422 free(rs->rs_rlt, M_TCPPACE);
428 rs_defer_destroy(struct tcp_rate_set *rs)
431 mtx_assert(&rs_mtx, MA_OWNED);
433 /* Check if already pending. */
434 if (rs->rs_flags & RS_FUNERAL_SCHD)
439 /* Set flag to only defer once. */
440 rs->rs_flags |= RS_FUNERAL_SCHD;
441 NET_EPOCH_CALL(rs_destroy, &rs->rs_epoch_ctx);
445 extern counter_u64_t rate_limit_new;
446 extern counter_u64_t rate_limit_chg;
447 extern counter_u64_t rate_limit_set_ok;
448 extern counter_u64_t rate_limit_active;
449 extern counter_u64_t rate_limit_alloc_fail;
453 rl_attach_txrtlmt(struct ifnet *ifp,
457 struct m_snd_tag **tag)
460 union if_snd_tag_alloc_params params = {
461 .rate_limit.hdr.type = IF_SND_TAG_TYPE_RATE_LIMIT,
462 .rate_limit.hdr.flowid = flowid,
463 .rate_limit.hdr.flowtype = flowtype,
464 .rate_limit.max_rate = cfg_rate,
465 .rate_limit.flags = M_NOWAIT,
468 error = m_snd_tag_alloc(ifp, ¶ms, tag);
471 counter_u64_add(rate_limit_set_ok, 1);
472 counter_u64_add(rate_limit_active, 1);
473 } else if (error != EOPNOTSUPP)
474 counter_u64_add(rate_limit_alloc_fail, 1);
480 populate_canned_table(struct tcp_rate_set *rs, const uint64_t *rate_table_act)
483 * The internal table is "special", it
484 * is two seperate ordered tables that
485 * must be merged. We get here when the
486 * adapter specifies a number of rates that
487 * covers both ranges in the table in some
490 int i, at_low, at_high;
491 uint8_t low_disabled = 0, high_disabled = 0;
493 for(i = 0, at_low = 0, at_high = RS_NEXT_ORDER_GROUP; i < rs->rs_rate_cnt; i++) {
494 rs->rs_rlt[i].flags = 0;
495 rs->rs_rlt[i].time_between = 0;
496 if ((low_disabled == 0) &&
498 (rate_table_act[at_low] < rate_table_act[at_high]))) {
499 rs->rs_rlt[i].rate = rate_table_act[at_low];
501 if (at_low == RS_NEXT_ORDER_GROUP)
503 } else if (high_disabled == 0) {
504 rs->rs_rlt[i].rate = rate_table_act[at_high];
506 if (at_high == MAX_HDWR_RATES)
512 static struct tcp_rate_set *
513 rt_setup_new_rs(struct ifnet *ifp, int *error)
515 struct tcp_rate_set *rs;
516 const uint64_t *rate_table_act;
517 uint64_t lentim, res;
521 struct if_ratelimit_query_results rl;
522 struct sysctl_oid *rl_sysctl_root;
523 struct epoch_tracker et;
525 * We expect to enter with the
529 if (ifp->if_ratelimit_query == NULL) {
531 * We can do nothing if we cannot
532 * get a query back from the driver.
534 printf("Warning:No query functions for %s:%d-- failed\n",
535 ifp->if_dname, ifp->if_dunit);
538 rs = malloc(sizeof(struct tcp_rate_set), M_TCPPACE, M_NOWAIT | M_ZERO);
542 printf("Warning:No memory for malloc of tcp_rate_set\n");
545 memset(&rl, 0, sizeof(rl));
546 rl.flags = RT_NOSUPPORT;
547 ifp->if_ratelimit_query(ifp, &rl);
548 if (rl.flags & RT_IS_UNUSABLE) {
550 * The interface does not really support
553 memset(rs, 0, sizeof(struct tcp_rate_set));
555 rs->rs_if_dunit = ifp->if_dunit;
556 rs->rs_flags = RS_INTF_NO_SUP;
559 sysctl_ctx_init(&rs->sysctl_ctx);
560 rl_sysctl_root = SYSCTL_ADD_NODE(&rs->sysctl_ctx,
561 SYSCTL_STATIC_CHILDREN(_net_inet_tcp_rl),
563 rs->rs_ifp->if_xname,
564 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
566 rl_add_syctl_entries(rl_sysctl_root, rs);
569 CK_LIST_INSERT_HEAD(&int_rs, rs, next);
573 } else if ((rl.flags & RT_IS_INDIRECT) == RT_IS_INDIRECT) {
574 memset(rs, 0, sizeof(struct tcp_rate_set));
576 rs->rs_if_dunit = ifp->if_dunit;
577 rs->rs_flags = RS_IS_DEFF;
579 sysctl_ctx_init(&rs->sysctl_ctx);
580 rl_sysctl_root = SYSCTL_ADD_NODE(&rs->sysctl_ctx,
581 SYSCTL_STATIC_CHILDREN(_net_inet_tcp_rl),
583 rs->rs_ifp->if_xname,
584 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
586 rl_add_syctl_entries(rl_sysctl_root, rs);
589 CK_LIST_INSERT_HEAD(&int_rs, rs, next);
593 } else if ((rl.flags & RT_IS_FIXED_TABLE) == RT_IS_FIXED_TABLE) {
594 /* Mellanox C4 likely */
596 rs->rs_if_dunit = ifp->if_dunit;
597 rs->rs_rate_cnt = rl.number_of_rates;
598 rs->rs_min_seg = rl.min_segment_burst;
599 rs->rs_highest_valid = 0;
600 rs->rs_flow_limit = rl.max_flows;
601 rs->rs_flags = RS_IS_INTF | RS_NO_PRE;
603 rate_table_act = rl.rate_table;
604 } else if ((rl.flags & RT_IS_SELECTABLE) == RT_IS_SELECTABLE) {
605 /* Chelsio, C5 and C6 of Mellanox? */
607 rs->rs_if_dunit = ifp->if_dunit;
608 rs->rs_rate_cnt = rl.number_of_rates;
609 rs->rs_min_seg = rl.min_segment_burst;
611 rs->rs_flow_limit = rl.max_flows;
612 rate_table_act = desired_rates;
613 if ((rs->rs_rate_cnt > MAX_HDWR_RATES) &&
614 (rs->rs_rate_cnt < ALL_HARDWARE_RATES)) {
616 * Our desired table is not big
617 * enough, do what we can.
619 rs->rs_rate_cnt = MAX_HDWR_RATES;
621 if (rs->rs_rate_cnt <= RS_ORDERED_COUNT)
622 rs->rs_flags = RS_IS_INTF;
624 rs->rs_flags = RS_IS_INTF | RS_INT_TBL;
625 if (rs->rs_rate_cnt >= ALL_HARDWARE_RATES)
626 rs->rs_rate_cnt = ALL_HARDWARE_RATES;
631 sz = sizeof(struct tcp_hwrate_limit_table) * rs->rs_rate_cnt;
632 rs->rs_rlt = malloc(sz, M_TCPPACE, M_NOWAIT);
633 if (rs->rs_rlt == NULL) {
640 if (rs->rs_rate_cnt >= ALL_HARDWARE_RATES) {
642 * The interface supports all
643 * the rates we could possibly want.
647 rs->rs_rlt[0].rate = 12500; /* 100k */
648 rs->rs_rlt[1].rate = 25000; /* 200k */
649 rs->rs_rlt[2].rate = 62500; /* 500k */
650 /* Note 125000 == 1Megabit
651 * populate 1Meg - 1000meg.
653 for(i = 3, rat = 125000; i< (ALL_HARDWARE_RATES-1); i++) {
654 rs->rs_rlt[i].rate = rat;
657 rs->rs_rlt[(ALL_HARDWARE_RATES-1)].rate = 1250000000;
658 } else if (rs->rs_flags & RS_INT_TBL) {
659 /* We populate this in a special way */
660 populate_canned_table(rs, rate_table_act);
663 * Just copy in the rates from
664 * the table, it is in order.
666 for (i=0; i<rs->rs_rate_cnt; i++) {
667 rs->rs_rlt[i].rate = rate_table_act[i];
668 rs->rs_rlt[i].time_between = 0;
669 rs->rs_rlt[i].flags = 0;
672 for (i = (rs->rs_rate_cnt - 1); i >= 0; i--) {
674 * We go backwards through the list so that if we can't get
675 * a rate and fail to init one, we have at least a chance of
676 * getting the highest one.
678 rs->rs_rlt[i].ptbl = rs;
679 rs->rs_rlt[i].tag = NULL;
680 rs->rs_rlt[i].using = 0;
681 rs->rs_rlt[i].rs_num_enobufs = 0;
683 * Calculate the time between.
685 lentim = ETHERNET_SEGMENT_SIZE * USECS_IN_SECOND;
686 res = lentim / rs->rs_rlt[i].rate;
688 rs->rs_rlt[i].time_between = res;
690 rs->rs_rlt[i].time_between = 1;
691 if (rs->rs_flags & RS_NO_PRE) {
692 rs->rs_rlt[i].flags = HDWRPACE_INITED;
693 rs->rs_lowest_valid = i;
697 if ((rl.flags & RT_IS_SETUP_REQ) &&
698 (ifp->if_ratelimit_query)) {
699 err = ifp->if_ratelimit_setup(ifp,
700 rs->rs_rlt[i].rate, i);
705 hash_type = M_HASHTYPE_RSS_TCP_IPV4;
707 hash_type = M_HASHTYPE_OPAQUE_HASH;
709 err = rl_attach_txrtlmt(ifp,
716 if (i == (rs->rs_rate_cnt - 1)) {
718 * Huh - first rate and we can't get
721 free(rs->rs_rlt, M_TCPPACE);
731 rs->rs_rlt[i].flags = HDWRPACE_INITED | HDWRPACE_TAGPRESENT;
732 rs->rs_lowest_valid = i;
736 /* Did we get at least 1 rate? */
737 if (rs->rs_rlt[(rs->rs_rate_cnt - 1)].flags & HDWRPACE_INITED)
738 rs->rs_highest_valid = rs->rs_rate_cnt - 1;
740 free(rs->rs_rlt, M_TCPPACE);
744 sysctl_ctx_init(&rs->sysctl_ctx);
745 rl_sysctl_root = SYSCTL_ADD_NODE(&rs->sysctl_ctx,
746 SYSCTL_STATIC_CHILDREN(_net_inet_tcp_rl),
748 rs->rs_ifp->if_xname,
749 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
751 rl_add_syctl_entries(rl_sysctl_root, rs);
754 CK_LIST_INSERT_HEAD(&int_rs, rs, next);
761 * For an explanation of why the argument is volatile please
762 * look at the comments around rt_setup_rate().
764 static const struct tcp_hwrate_limit_table *
765 tcp_int_find_suitable_rate(const volatile struct tcp_rate_set *rs,
766 uint64_t bytes_per_sec, uint32_t flags, uint64_t *lower_rate)
768 struct tcp_hwrate_limit_table *arte = NULL, *rte = NULL;
769 uint64_t mbits_per_sec, ind_calc, previous_rate = 0;
772 mbits_per_sec = (bytes_per_sec * 8);
773 if (flags & RS_PACING_LT) {
774 if ((mbits_per_sec < RS_ONE_MEGABIT_PERSEC) &&
775 (rs->rs_lowest_valid <= 2)){
777 * Smaller than 1Meg, only
778 * 3 entries can match it.
781 for(i = rs->rs_lowest_valid; i < 3; i++) {
782 if (bytes_per_sec <= rs->rs_rlt[i].rate) {
783 rte = &rs->rs_rlt[i];
785 } else if (rs->rs_rlt[i].flags & HDWRPACE_INITED) {
786 arte = &rs->rs_rlt[i];
788 previous_rate = rs->rs_rlt[i].rate;
791 } else if ((mbits_per_sec > RS_ONE_GIGABIT_PERSEC) &&
792 (rs->rs_rlt[(ALL_HARDWARE_RATES-1)].flags & HDWRPACE_INITED)){
794 * Larger than 1G (the majority of
797 if (mbits_per_sec < RS_TEN_GIGABIT_PERSEC)
798 rte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
800 arte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
801 previous_rate = rs->rs_rlt[(ALL_HARDWARE_RATES-2)].rate;
805 * If we reach here its in our table (between 1Meg - 1000Meg),
806 * just take the rounded down mbits per second, and add
807 * 1Megabit to it, from this we can calculate
808 * the index in the table.
810 ind_calc = mbits_per_sec/RS_ONE_MEGABIT_PERSEC;
811 if ((ind_calc * RS_ONE_MEGABIT_PERSEC) != mbits_per_sec)
813 /* our table is offset by 3, we add 2 */
815 if (ind_calc > (ALL_HARDWARE_RATES-1)) {
816 /* This should not happen */
817 ind_calc = ALL_HARDWARE_RATES-1;
819 if ((ind_calc >= rs->rs_lowest_valid) &&
820 (ind_calc <= rs->rs_highest_valid)) {
821 rte = &rs->rs_rlt[ind_calc];
823 previous_rate = rs->rs_rlt[(ind_calc-1)].rate;
825 } else if (flags & RS_PACING_EXACT_MATCH) {
826 if ((mbits_per_sec < RS_ONE_MEGABIT_PERSEC) &&
827 (rs->rs_lowest_valid <= 2)){
828 for(i = rs->rs_lowest_valid; i < 3; i++) {
829 if (bytes_per_sec == rs->rs_rlt[i].rate) {
830 rte = &rs->rs_rlt[i];
834 } else if ((mbits_per_sec > RS_ONE_GIGABIT_PERSEC) &&
835 (rs->rs_rlt[(ALL_HARDWARE_RATES-1)].flags & HDWRPACE_INITED)) {
836 /* > 1Gbps only one rate */
837 if (bytes_per_sec == rs->rs_rlt[(ALL_HARDWARE_RATES-1)].rate) {
839 rte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
842 /* Ok it must be a exact meg (its between 1G and 1Meg) */
843 ind_calc = mbits_per_sec/RS_ONE_MEGABIT_PERSEC;
844 if ((ind_calc * RS_ONE_MEGABIT_PERSEC) == mbits_per_sec) {
845 /* its an exact Mbps */
847 if (ind_calc > (ALL_HARDWARE_RATES-1)) {
848 /* This should not happen */
849 ind_calc = ALL_HARDWARE_RATES-1;
851 if (rs->rs_rlt[ind_calc].flags & HDWRPACE_INITED)
852 rte = &rs->rs_rlt[ind_calc];
856 /* we want greater than the requested rate */
857 if ((mbits_per_sec < RS_ONE_MEGABIT_PERSEC) &&
858 (rs->rs_lowest_valid <= 2)){
859 arte = &rs->rs_rlt[3]; /* set alternate to 1Meg */
860 for (i=2; i>=rs->rs_lowest_valid; i--) {
861 if (bytes_per_sec < rs->rs_rlt[i].rate) {
862 rte = &rs->rs_rlt[i];
864 previous_rate = rs->rs_rlt[(i-1)].rate;
867 } else if ((flags & RS_PACING_GEQ) &&
868 (bytes_per_sec == rs->rs_rlt[i].rate)) {
869 rte = &rs->rs_rlt[i];
871 previous_rate = rs->rs_rlt[(i-1)].rate;
875 arte = &rs->rs_rlt[i]; /* new alternate */
878 } else if (mbits_per_sec > RS_ONE_GIGABIT_PERSEC) {
879 if ((bytes_per_sec < rs->rs_rlt[(ALL_HARDWARE_RATES-1)].rate) &&
880 (rs->rs_rlt[(ALL_HARDWARE_RATES-1)].flags & HDWRPACE_INITED)){
881 /* Our top rate is larger than the request */
882 rte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
883 } else if ((flags & RS_PACING_GEQ) &&
884 (bytes_per_sec == rs->rs_rlt[(ALL_HARDWARE_RATES-1)].rate) &&
885 (rs->rs_rlt[(ALL_HARDWARE_RATES-1)].flags & HDWRPACE_INITED)) {
886 /* It matches our top rate */
887 rte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
888 } else if (rs->rs_rlt[(ALL_HARDWARE_RATES-1)].flags & HDWRPACE_INITED) {
889 /* The top rate is an alternative */
890 arte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
892 previous_rate = rs->rs_rlt[(ALL_HARDWARE_RATES-2)].rate;
894 /* Its in our range 1Meg - 1Gig */
895 if (flags & RS_PACING_GEQ) {
896 ind_calc = mbits_per_sec/RS_ONE_MEGABIT_PERSEC;
897 if ((ind_calc * RS_ONE_MEGABIT_PERSEC) == mbits_per_sec) {
898 if (ind_calc > (ALL_HARDWARE_RATES-1)) {
899 /* This should not happen */
900 ind_calc = (ALL_HARDWARE_RATES-1);
902 rte = &rs->rs_rlt[ind_calc];
904 previous_rate = rs->rs_rlt[(ind_calc-1)].rate;
908 ind_calc = (mbits_per_sec + (RS_ONE_MEGABIT_PERSEC-1))/RS_ONE_MEGABIT_PERSEC;
910 if (ind_calc > (ALL_HARDWARE_RATES-1)) {
911 /* This should not happen */
912 ind_calc = ALL_HARDWARE_RATES-1;
914 if (rs->rs_rlt[ind_calc].flags & HDWRPACE_INITED) {
915 rte = &rs->rs_rlt[ind_calc];
917 previous_rate = rs->rs_rlt[(ind_calc-1)].rate;
924 (flags & RS_PACING_SUB_OK)) {
925 /* We can use the substitute */
929 *lower_rate = previous_rate;
934 * For an explanation of why the argument is volatile please
935 * look at the comments around rt_setup_rate().
937 static const struct tcp_hwrate_limit_table *
938 tcp_find_suitable_rate(const volatile struct tcp_rate_set *rs, uint64_t bytes_per_sec, uint32_t flags, uint64_t *lower_rate)
941 * Hunt the rate table with the restrictions in flags and find a
942 * suitable rate if possible.
943 * RS_PACING_EXACT_MATCH - look for an exact match to rate.
944 * RS_PACING_GT - must be greater than.
945 * RS_PACING_GEQ - must be greater than or equal.
946 * RS_PACING_LT - must be less than.
947 * RS_PACING_SUB_OK - If we don't meet criteria a
951 struct tcp_hwrate_limit_table *rte = NULL;
952 uint64_t previous_rate = 0;
954 if ((rs->rs_flags & RS_INT_TBL) &&
955 (rs->rs_rate_cnt >= ALL_HARDWARE_RATES)) {
957 * Here we don't want to paw thru
958 * a big table, we have everything
959 * from 1Meg - 1000Meg in 1Meg increments.
960 * Use an alternate method to "lookup".
962 return (tcp_int_find_suitable_rate(rs, bytes_per_sec, flags, lower_rate));
964 if ((flags & RS_PACING_LT) ||
965 (flags & RS_PACING_EXACT_MATCH)) {
967 * For exact and less than we go forward through the table.
968 * This way when we find one larger we stop (exact was a
971 for (i = rs->rs_lowest_valid, matched = 0; i <= rs->rs_highest_valid; i++) {
972 if ((flags & RS_PACING_EXACT_MATCH) &&
973 (bytes_per_sec == rs->rs_rlt[i].rate)) {
974 rte = &rs->rs_rlt[i];
976 if (lower_rate != NULL)
977 *lower_rate = previous_rate;
979 } else if ((flags & RS_PACING_LT) &&
980 (bytes_per_sec <= rs->rs_rlt[i].rate)) {
981 rte = &rs->rs_rlt[i];
983 if (lower_rate != NULL)
984 *lower_rate = previous_rate;
987 previous_rate = rs->rs_rlt[i].rate;
988 if (bytes_per_sec > rs->rs_rlt[i].rate)
991 if ((matched == 0) &&
992 (flags & RS_PACING_LT) &&
993 (flags & RS_PACING_SUB_OK)) {
994 /* Kick in a substitute (the lowest) */
995 rte = &rs->rs_rlt[rs->rs_lowest_valid];
999 * Here we go backward through the table so that we can find
1000 * the one greater in theory faster (but its probably a
1003 for (i = rs->rs_highest_valid, matched = 0; i >= rs->rs_lowest_valid; i--) {
1004 if (rs->rs_rlt[i].rate > bytes_per_sec) {
1005 /* A possible candidate */
1006 rte = &rs->rs_rlt[i];
1008 if ((flags & RS_PACING_GEQ) &&
1009 (bytes_per_sec == rs->rs_rlt[i].rate)) {
1010 /* An exact match and we want equal */
1012 rte = &rs->rs_rlt[i];
1016 * Found one that is larger than but don't
1017 * stop, there may be a more closer match.
1021 if (rs->rs_rlt[i].rate < bytes_per_sec) {
1023 * We found a table entry that is smaller,
1024 * stop there will be none greater or equal.
1026 if (lower_rate != NULL)
1027 *lower_rate = rs->rs_rlt[i].rate;
1031 if ((matched == 0) &&
1032 (flags & RS_PACING_SUB_OK)) {
1033 /* Kick in a substitute (the highest) */
1034 rte = &rs->rs_rlt[rs->rs_highest_valid];
1040 static struct ifnet *
1041 rt_find_real_interface(struct ifnet *ifp, struct inpcb *inp, int *error)
1044 struct m_snd_tag *tag, *ntag;
1045 union if_snd_tag_alloc_params params = {
1046 .rate_limit.hdr.type = IF_SND_TAG_TYPE_RATE_LIMIT,
1047 .rate_limit.hdr.flowid = inp->inp_flowid,
1048 .rate_limit.hdr.numa_domain = inp->inp_numa_domain,
1049 .rate_limit.max_rate = COMMON_RATE,
1050 .rate_limit.flags = M_NOWAIT,
1054 params.rate_limit.hdr.flowtype = ((inp->inp_vflag & INP_IPV6) ?
1055 M_HASHTYPE_RSS_TCP_IPV6 : M_HASHTYPE_RSS_TCP_IPV4);
1057 params.rate_limit.hdr.flowtype = M_HASHTYPE_OPAQUE_HASH;
1059 err = m_snd_tag_alloc(ifp, ¶ms, &tag);
1061 /* Failed to setup a tag? */
1067 while(ntag->ifp->if_next_snd_tag != NULL) {
1068 ntag = ntag->ifp->if_next_snd_tag(ntag);
1071 m_snd_tag_rele(tag);
1076 rl_increment_using(const struct tcp_hwrate_limit_table *rte)
1078 struct tcp_hwrate_limit_table *decon_rte;
1080 decon_rte = __DECONST(struct tcp_hwrate_limit_table *, rte);
1081 atomic_add_long(&decon_rte->using, 1);
1085 rl_decrement_using(const struct tcp_hwrate_limit_table *rte)
1087 struct tcp_hwrate_limit_table *decon_rte;
1089 decon_rte = __DECONST(struct tcp_hwrate_limit_table *, rte);
1090 atomic_subtract_long(&decon_rte->using, 1);
1094 tcp_rl_log_enobuf(const struct tcp_hwrate_limit_table *rte)
1096 struct tcp_hwrate_limit_table *decon_rte;
1098 decon_rte = __DECONST(struct tcp_hwrate_limit_table *, rte);
1099 atomic_add_long(&decon_rte->rs_num_enobufs, 1);
1103 * Do NOT take the __noinline out of the
1104 * find_rs_for_ifp() function. If you do the inline
1105 * of it for the rt_setup_rate() will show you a
1106 * compiler bug. For some reason the compiler thinks
1107 * the list can never be empty. The consequence of
1108 * this will be a crash when we dereference NULL
1109 * if an ifp is removed just has a hw rate limit
1110 * is attempted. If you are working on the compiler
1111 * and want to "test" this go ahead and take the noinline
1112 * out otherwise let sleeping dogs ly until such time
1113 * as we get a compiler fix 10/2/20 -- RRS
1115 static __noinline struct tcp_rate_set *
1116 find_rs_for_ifp(struct ifnet *ifp)
1118 struct tcp_rate_set *rs;
1120 CK_LIST_FOREACH(rs, &int_rs, next) {
1121 if ((rs->rs_ifp == ifp) &&
1122 (rs->rs_if_dunit == ifp->if_dunit)) {
1123 /* Ok we found it */
1131 static const struct tcp_hwrate_limit_table *
1132 rt_setup_rate(struct inpcb *inp, struct ifnet *ifp, uint64_t bytes_per_sec,
1133 uint32_t flags, int *error, uint64_t *lower_rate)
1135 /* First lets find the interface if it exists */
1136 const struct tcp_hwrate_limit_table *rte;
1138 * So why is rs volatile? This is to defeat a
1139 * compiler bug where in the compiler is convinced
1140 * that rs can never be NULL (which is not true). Because
1141 * of its conviction it nicely optimizes out the if ((rs == NULL
1142 * below which means if you get a NULL back you dereference it.
1144 volatile struct tcp_rate_set *rs;
1145 struct epoch_tracker et;
1146 struct ifnet *oifp = ifp;
1149 NET_EPOCH_ENTER(et);
1151 rs = find_rs_for_ifp(ifp);
1153 (rs->rs_flags & RS_INTF_NO_SUP) ||
1154 (rs->rs_flags & RS_IS_DEAD)) {
1156 * This means we got a packet *before*
1157 * the IF-UP was processed below, <or>
1158 * while or after we already received an interface
1159 * departed event. In either case we really don't
1160 * want to do anything with pacing, in
1161 * the departing case the packet is not
1162 * going to go very far. The new case
1163 * might be arguable, but its impossible
1164 * to tell from the departing case.
1172 if ((rs == NULL) || (rs->rs_disable != 0)) {
1178 if (rs->rs_flags & RS_IS_DEFF) {
1179 /* We need to find the real interface */
1182 tifp = rt_find_real_interface(ifp, inp, error);
1184 if (rs->rs_disable && error)
1189 KASSERT((tifp != ifp),
1190 ("Lookup failure ifp:%p inp:%p rt_find_real_interface() returns the same interface tifp:%p?\n",
1193 goto use_real_interface;
1195 if (rs->rs_flow_limit &&
1196 ((rs->rs_flows_using + 1) > rs->rs_flow_limit)) {
1202 rte = tcp_find_suitable_rate(rs, bytes_per_sec, flags, lower_rate);
1204 err = in_pcbattach_txrtlmt(inp, oifp,
1210 /* Failed to attach */
1215 KASSERT((inp->inp_snd_tag != NULL) ,
1216 ("Setup rate has no snd_tag inp:%p rte:%p rate:%llu rs:%p",
1217 inp, rte, (unsigned long long)rte->rate, rs));
1219 counter_u64_add(rate_limit_new, 1);
1225 * We use an atomic here for accounting so we don't have to
1226 * use locks when freeing.
1228 atomic_add_64(&rs->rs_flows_using, 1);
1235 tcp_rl_ifnet_link(void *arg __unused, struct ifnet *ifp, int link_state)
1238 struct tcp_rate_set *rs;
1239 struct epoch_tracker et;
1241 if (((ifp->if_capenable & IFCAP_TXRTLMT) == 0) ||
1242 (link_state != LINK_STATE_UP)) {
1244 * We only care on an interface going up that is rate-limit
1249 NET_EPOCH_ENTER(et);
1251 rs = find_rs_for_ifp(ifp);
1253 /* We already have initialized this guy */
1254 mtx_unlock(&rs_mtx);
1258 mtx_unlock(&rs_mtx);
1260 rt_setup_new_rs(ifp, &error);
1264 tcp_rl_ifnet_departure(void *arg __unused, struct ifnet *ifp)
1266 struct tcp_rate_set *rs;
1267 struct epoch_tracker et;
1270 NET_EPOCH_ENTER(et);
1272 rs = find_rs_for_ifp(ifp);
1274 CK_LIST_REMOVE(rs, next);
1276 rs->rs_flags |= RS_IS_DEAD;
1277 for (i = 0; i < rs->rs_rate_cnt; i++) {
1278 if (rs->rs_rlt[i].flags & HDWRPACE_TAGPRESENT) {
1279 in_pcbdetach_tag(rs->rs_rlt[i].tag);
1280 rs->rs_rlt[i].tag = NULL;
1282 rs->rs_rlt[i].flags = HDWRPACE_IFPDEPARTED;
1284 if (rs->rs_flows_using == 0)
1285 rs_defer_destroy(rs);
1287 mtx_unlock(&rs_mtx);
1292 tcp_rl_shutdown(void *arg __unused, int howto __unused)
1294 struct tcp_rate_set *rs, *nrs;
1295 struct epoch_tracker et;
1298 NET_EPOCH_ENTER(et);
1300 CK_LIST_FOREACH_SAFE(rs, &int_rs, next, nrs) {
1301 CK_LIST_REMOVE(rs, next);
1303 rs->rs_flags |= RS_IS_DEAD;
1304 for (i = 0; i < rs->rs_rate_cnt; i++) {
1305 if (rs->rs_rlt[i].flags & HDWRPACE_TAGPRESENT) {
1306 in_pcbdetach_tag(rs->rs_rlt[i].tag);
1307 rs->rs_rlt[i].tag = NULL;
1309 rs->rs_rlt[i].flags = HDWRPACE_IFPDEPARTED;
1311 if (rs->rs_flows_using == 0)
1312 rs_defer_destroy(rs);
1314 mtx_unlock(&rs_mtx);
1318 const struct tcp_hwrate_limit_table *
1319 tcp_set_pacing_rate(struct tcpcb *tp, struct ifnet *ifp,
1320 uint64_t bytes_per_sec, int flags, int *error, uint64_t *lower_rate)
1322 const struct tcp_hwrate_limit_table *rte;
1324 struct ktls_session *tls;
1327 INP_WLOCK_ASSERT(tp->t_inpcb);
1329 if (tp->t_inpcb->inp_snd_tag == NULL) {
1331 * We are setting up a rate for the first time.
1333 if ((ifp->if_capenable & IFCAP_TXRTLMT) == 0) {
1334 /* Not supported by the egress */
1341 if (tp->t_inpcb->inp_socket->so_snd.sb_flags & SB_TLS_IFNET) {
1342 tls = tp->t_inpcb->inp_socket->so_snd.sb_tls_info;
1344 if ((ifp->if_capenable & IFCAP_TXTLS_RTLMT) == 0 ||
1345 tls->mode != TCP_TLS_MODE_IFNET) {
1352 rte = rt_setup_rate(tp->t_inpcb, ifp, bytes_per_sec, flags, error, lower_rate);
1354 rl_increment_using(rte);
1356 if (rte != NULL && tls != NULL && tls->snd_tag != NULL) {
1358 * Fake a route change error to reset the TLS
1359 * send tag. This will convert the existing
1360 * tag to a TLS ratelimit tag.
1362 MPASS(tls->snd_tag->type == IF_SND_TAG_TYPE_TLS);
1363 ktls_output_eagain(tp->t_inpcb, tls);
1368 * We are modifying a rate, wrong interface?
1375 tp->t_pacing_rate = rte->rate;
1381 const struct tcp_hwrate_limit_table *
1382 tcp_chg_pacing_rate(const struct tcp_hwrate_limit_table *crte,
1383 struct tcpcb *tp, struct ifnet *ifp,
1384 uint64_t bytes_per_sec, int flags, int *error, uint64_t *lower_rate)
1386 const struct tcp_hwrate_limit_table *nrte;
1387 const struct tcp_rate_set *rs;
1389 struct ktls_session *tls = NULL;
1393 INP_WLOCK_ASSERT(tp->t_inpcb);
1396 /* Wrong interface */
1403 if (tp->t_inpcb->inp_socket->so_snd.sb_flags & SB_TLS_IFNET) {
1404 tls = tp->t_inpcb->inp_socket->so_snd.sb_tls_info;
1405 MPASS(tls->mode == TCP_TLS_MODE_IFNET);
1406 if (tls->snd_tag != NULL &&
1407 tls->snd_tag->type != IF_SND_TAG_TYPE_TLS_RATE_LIMIT) {
1409 * NIC probably doesn't support ratelimit TLS
1410 * tags if it didn't allocate one when an
1411 * existing rate was present, so ignore.
1414 *error = EOPNOTSUPP;
1419 if (tp->t_inpcb->inp_snd_tag == NULL) {
1420 /* Wrong interface */
1426 if ((rs->rs_flags & RS_IS_DEAD) ||
1427 (crte->flags & HDWRPACE_IFPDEPARTED)) {
1428 /* Release the rate, and try anew */
1430 tcp_rel_pacing_rate(crte, tp);
1431 nrte = tcp_set_pacing_rate(tp, ifp,
1432 bytes_per_sec, flags, error, lower_rate);
1435 nrte = tcp_find_suitable_rate(rs, bytes_per_sec, flags, lower_rate);
1443 /* Release the old rate */
1446 tcp_rel_pacing_rate(crte, tp);
1449 rl_decrement_using(crte);
1450 rl_increment_using(nrte);
1451 /* Change rates to our new entry */
1454 err = ktls_modify_txrtlmt(tls, nrte->rate);
1457 err = in_pcbmodify_txrtlmt(tp->t_inpcb, nrte->rate);
1459 rl_decrement_using(nrte);
1460 /* Do we still have a snd-tag attached? */
1461 if (tp->t_inpcb->inp_snd_tag)
1462 in_pcbdetach_txrtlmt(tp->t_inpcb);
1468 counter_u64_add(rate_limit_chg, 1);
1473 tp->t_pacing_rate = nrte->rate;
1478 tcp_rel_pacing_rate(const struct tcp_hwrate_limit_table *crte, struct tcpcb *tp)
1480 const struct tcp_rate_set *crs;
1481 struct tcp_rate_set *rs;
1484 INP_WLOCK_ASSERT(tp->t_inpcb);
1486 tp->t_pacing_rate = -1;
1489 * Now we must break the const
1490 * in order to release our refcount.
1492 rs = __DECONST(struct tcp_rate_set *, crs);
1493 rl_decrement_using(crte);
1494 pre = atomic_fetchadd_64(&rs->rs_flows_using, -1);
1496 struct epoch_tracker et;
1498 NET_EPOCH_ENTER(et);
1503 if (rs->rs_flags & RS_IS_DEAD)
1504 rs_defer_destroy(rs);
1505 mtx_unlock(&rs_mtx);
1510 * XXX: If this connection is using ifnet TLS, should we
1511 * switch it to using an unlimited rate, or perhaps use
1512 * ktls_output_eagain() to reset the send tag to a plain
1515 in_pcbdetach_txrtlmt(tp->t_inpcb);
1518 #define ONE_POINT_TWO_MEG 150000 /* 1.2 megabits in bytes */
1519 #define ONE_HUNDRED_MBPS 12500000 /* 100Mbps in bytes per second */
1520 #define FIVE_HUNDRED_MBPS 62500000 /* 500Mbps in bytes per second */
1521 #define MAX_MSS_SENT 43 /* 43 mss = 43 x 1500 = 64,500 bytes */
1524 tcp_log_pacing_size(struct tcpcb *tp, uint64_t bw, uint32_t segsiz, uint32_t new_tso,
1525 uint64_t hw_rate, uint32_t time_between, uint32_t calc_time_between,
1526 uint32_t segs, uint32_t res_div, uint16_t mult, uint8_t mod)
1528 if (tp->t_logstate != TCP_LOG_STATE_OFF) {
1529 union tcp_log_stackspecific log;
1533 memset(&log, 0, sizeof(log));
1534 cts = tcp_get_usecs(&tv);
1535 log.u_bbr.flex1 = segsiz;
1536 log.u_bbr.flex2 = new_tso;
1537 log.u_bbr.flex3 = time_between;
1538 log.u_bbr.flex4 = calc_time_between;
1539 log.u_bbr.flex5 = segs;
1540 log.u_bbr.flex6 = res_div;
1541 log.u_bbr.flex7 = mult;
1542 log.u_bbr.flex8 = mod;
1543 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
1544 log.u_bbr.cur_del_rate = bw;
1545 log.u_bbr.delRate = hw_rate;
1546 TCP_LOG_EVENTP(tp, NULL,
1547 &tp->t_inpcb->inp_socket->so_rcv,
1548 &tp->t_inpcb->inp_socket->so_snd,
1549 TCP_HDWR_PACE_SIZE, 0,
1550 0, &log, false, &tv);
1555 tcp_get_pacing_burst_size (struct tcpcb *tp, uint64_t bw, uint32_t segsiz, int can_use_1mss,
1556 const struct tcp_hwrate_limit_table *te, int *err)
1559 * We use the google formula to calculate the
1564 * tso = min(bw/1000, 64k)
1566 * Note for these calculations we ignore the
1567 * packet overhead (enet hdr, ip hdr and tcp hdr).
1569 uint64_t lentim, res, bytes;
1570 uint32_t new_tso, min_tso_segs;
1573 if (bytes > (64 * 1000))
1576 new_tso = (bytes + segsiz - 1) / segsiz;
1577 if (can_use_1mss && (bw < ONE_POINT_TWO_MEG))
1581 if (rs_floor_mss && (new_tso < rs_floor_mss))
1582 new_tso = rs_floor_mss;
1583 else if (new_tso < min_tso_segs)
1584 new_tso = min_tso_segs;
1585 if (new_tso > MAX_MSS_SENT)
1586 new_tso = MAX_MSS_SENT;
1588 tcp_log_pacing_size(tp, bw, segsiz, new_tso,
1589 0, 0, 0, 0, 0, 0, 1);
1591 * If we are not doing hardware pacing
1600 * For hardware pacing we look at the
1601 * rate you are sending at and compare
1602 * that to the rate you have in hardware.
1604 * If the hardware rate is slower than your
1605 * software rate then you are in error and
1606 * we will build a queue in our hardware whic
1607 * is probably not desired, in such a case
1608 * just return the non-hardware TSO size.
1610 * If the rate in hardware is faster (which
1611 * it should be) then look at how long it
1612 * takes to send one ethernet segment size at
1613 * your b/w and compare that to the time it
1614 * takes to send at the rate you had selected.
1616 * If your time is greater (which we hope it is)
1617 * we get the delta between the two, and then
1618 * divide that into your pacing time. This tells
1619 * us how many MSS you can send down at once (rounded up).
1621 * Note we also double this value if the b/w is over
1622 * 100Mbps. If its over 500meg we just set you to the
1623 * max (43 segments).
1625 if (te->rate > FIVE_HUNDRED_MBPS)
1627 if (te->rate == bw) {
1628 /* We are pacing at exactly the hdwr rate */
1630 tcp_log_pacing_size(tp, bw, segsiz, new_tso,
1631 te->rate, te->time_between, (uint32_t)0,
1632 (segsiz * MAX_MSS_SENT), 0, 0, 3);
1633 return (segsiz * MAX_MSS_SENT);
1635 lentim = ETHERNET_SEGMENT_SIZE * USECS_IN_SECOND;
1637 if (res > te->time_between) {
1638 uint32_t delta, segs, res_div;
1640 res_div = ((res * num_of_waits_allowed) + wait_time_floor);
1641 delta = res - te->time_between;
1642 segs = (res_div + delta - 1)/delta;
1643 if (segs < min_tso_segs)
1644 segs = min_tso_segs;
1645 if (segs < rs_hw_floor_mss)
1646 segs = rs_hw_floor_mss;
1647 if (segs > MAX_MSS_SENT)
1648 segs = MAX_MSS_SENT;
1650 tcp_log_pacing_size(tp, bw, segsiz, new_tso,
1651 te->rate, te->time_between, (uint32_t)res,
1652 segs, res_div, 1, 3);
1655 if (segs < new_tso) {
1663 * Your time is smaller which means
1664 * we will grow a queue on our
1665 * hardware. Send back the non-hardware
1668 tcp_log_pacing_size(tp, bw, segsiz, new_tso,
1669 te->rate, te->time_between, (uint32_t)res,
1678 tcp_hw_highest_rate_ifp(struct ifnet *ifp, struct inpcb *inp)
1680 struct epoch_tracker et;
1681 struct tcp_rate_set *rs;
1684 NET_EPOCH_ENTER(et);
1686 rs = find_rs_for_ifp(ifp);
1688 /* This interface does not do ratelimiting */
1690 } else if (rs->rs_flags & RS_IS_DEFF) {
1691 /* We need to find the real interface */
1694 tifp = rt_find_real_interface(ifp, inp, NULL);
1700 goto use_next_interface;
1702 /* Lets return the highest rate this guy has */
1703 rate_ret = rs->rs_rlt[rs->rs_highest_valid].rate;
1709 static eventhandler_tag rl_ifnet_departs;
1710 static eventhandler_tag rl_ifnet_arrives;
1711 static eventhandler_tag rl_shutdown_start;
1714 tcp_rs_init(void *st __unused)
1716 CK_LIST_INIT(&int_rs);
1717 rs_number_alive = 0;
1719 mtx_init(&rs_mtx, "tcp_rs_mtx", "rsmtx", MTX_DEF);
1720 rl_ifnet_departs = EVENTHANDLER_REGISTER(ifnet_departure_event,
1721 tcp_rl_ifnet_departure,
1722 NULL, EVENTHANDLER_PRI_ANY);
1723 rl_ifnet_arrives = EVENTHANDLER_REGISTER(ifnet_link_event,
1725 NULL, EVENTHANDLER_PRI_ANY);
1726 rl_shutdown_start = EVENTHANDLER_REGISTER(shutdown_pre_sync,
1727 tcp_rl_shutdown, NULL,
1728 SHUTDOWN_PRI_FIRST);
1729 printf("TCP_ratelimit: Is now initialized\n");
1732 SYSINIT(tcp_rl_init, SI_SUB_SMP + 1, SI_ORDER_ANY, tcp_rs_init, NULL);