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>
35 __FBSDID("$FreeBSD$");
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>
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>
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>
58 #include <netinet6/tcp6_var.h>
60 #include <netinet/tcp_ratelimit.h>
61 #ifndef USECS_IN_SECOND
62 #define USECS_IN_SECOND 1000000
65 * For the purposes of each send, what is the size
66 * of an ethernet frame.
68 MALLOC_DEFINE(M_TCPPACE, "tcp_hwpace", "TCP Hardware pacing memory");
72 * The following preferred table will seem weird to
73 * the casual viewer. Why do we not have any rates below
74 * 1Mbps? Why do we have a rate at 1.44Mbps called common?
75 * Why do the rates cluster in the 1-100Mbps range more
76 * than others? Why does the table jump around at the beginnign
77 * and then be more consistently raising?
79 * Let me try to answer those questions. A lot of
80 * this is dependant on the hardware. We have three basic
81 * supporters of rate limiting
83 * Chelsio - Supporting 16 configurable rates.
84 * Mlx - c4 supporting 13 fixed rates.
85 * Mlx - c5 & c6 supporting 127 configurable rates.
87 * The c4 is why we have a common rate that is available
88 * in all rate tables. This is a selected rate from the
89 * c4 table and we assure its available in all ratelimit
90 * tables. This way the tcp_ratelimit code has an assured
91 * rate it should always be able to get. This answers a
92 * couple of the questions above.
94 * So what about the rest, well the table is built to
95 * try to get the most out of a joint hardware/software
96 * pacing system. The software pacer will always pick
97 * a rate higher than the b/w that it is estimating
99 * on the path. This is done for two reasons.
100 * a) So we can discover more b/w
102 * b) So we can send a block of MSS's down and then
103 * have the software timer go off after the previous
104 * send is completely out of the hardware.
106 * But when we do <b> we don't want to have the delay
107 * between the last packet sent by the hardware be
108 * excessively long (to reach our desired rate).
110 * So let me give an example for clarity.
112 * Lets assume that the tcp stack sees that 29,110,000 bps is
113 * what the bw of the path is. The stack would select the
114 * rate 31Mbps. 31Mbps means that each send that is done
115 * by the hardware will cause a 390 micro-second gap between
116 * the packets sent at that rate. For 29,110,000 bps we
117 * would need 416 micro-seconds gap between each send.
119 * Note that are calculating a complete time for pacing
120 * which includes the ethernet, IP and TCP overhead. So
121 * a full 1514 bytes is used for the above calculations.
122 * My testing has shown that both cards are also using this
123 * as their basis i.e. full payload size of the ethernet frame.
124 * The TCP stack caller needs to be aware of this and make the
125 * appropriate overhead calculations be included in its choices.
127 * Now, continuing our example, we pick a MSS size based on the
128 * delta between the two rates (416 - 390) divided into the rate
129 * we really wish to send at rounded up. That results in a MSS
130 * send of 17 mss's at once. The hardware then will
131 * run out of data in a single 17MSS send in 6,630 micro-seconds.
133 * On the other hand the software pacer will send more data
134 * in 7,072 micro-seconds. This means that we will refill
135 * the hardware 52 microseconds after it would have sent
136 * next if it had not ran out of data. This is a win since we are
137 * only sending every 7ms or so and yet all the packets are spaced on
138 * the wire with 94% of what they should be and only
139 * the last packet is delayed extra to make up for the
142 * Note that the above formula has two important caveat.
143 * If we are above (b/w wise) over 100Mbps we double the result
144 * of the MSS calculation. The second caveat is if we are 500Mbps
145 * or more we just send the maximum MSS at once i.e. 45MSS. At
146 * the higher b/w's even the cards have limits to what times (timer granularity)
147 * they can insert between packets and start to send more than one
148 * packet at a time on the wire.
151 #define COMMON_RATE 180500
152 const uint64_t desired_rates[] = {
153 122500, /* 1Mbps - rate 1 */
154 180500, /* 1.44Mpbs - rate 2 common rate */
155 375000, /* 3Mbps - rate 3 */
156 625000, /* 5Mbps - rate 4 */
157 875000, /* 7Mbps - rate 5 */
158 1125000, /* 9Mbps - rate 6 */
159 1375000, /* 11Mbps - rate 7 */
160 1625000, /* 13Mbps - rate 8 */
161 2625000, /* 21Mbps - rate 9 */
162 3875000, /* 31Mbps - rate 10 */
163 5125000, /* 41Meg - rate 11 */
164 12500000, /* 100Mbps - rate 12 */
165 25000000, /* 200Mbps - rate 13 */
166 50000000, /* 400Mbps - rate 14 */
167 63750000, /* 51Mbps - rate 15 */
168 100000000, /* 800Mbps - rate 16 */
169 1875000, /* 15Mbps - rate 17 */
170 2125000, /* 17Mbps - rate 18 */
171 2375000, /* 19Mbps - rate 19 */
172 2875000, /* 23Mbps - rate 20 */
173 3125000, /* 25Mbps - rate 21 */
174 3375000, /* 27Mbps - rate 22 */
175 3625000, /* 29Mbps - rate 23 */
176 4125000, /* 33Mbps - rate 24 */
177 4375000, /* 35Mbps - rate 25 */
178 4625000, /* 37Mbps - rate 26 */
179 4875000, /* 39Mbps - rate 27 */
180 5375000, /* 43Mbps - rate 28 */
181 5625000, /* 45Mbps - rate 29 */
182 5875000, /* 47Mbps - rate 30 */
183 6125000, /* 49Mbps - rate 31 */
184 6625000, /* 53Mbps - rate 32 */
185 6875000, /* 55Mbps - rate 33 */
186 7125000, /* 57Mbps - rate 34 */
187 7375000, /* 59Mbps - rate 35 */
188 7625000, /* 61Mbps - rate 36 */
189 7875000, /* 63Mbps - rate 37 */
190 8125000, /* 65Mbps - rate 38 */
191 8375000, /* 67Mbps - rate 39 */
192 8625000, /* 69Mbps - rate 40 */
193 8875000, /* 71Mbps - rate 41 */
194 9125000, /* 73Mbps - rate 42 */
195 9375000, /* 75Mbps - rate 43 */
196 9625000, /* 77Mbps - rate 44 */
197 9875000, /* 79Mbps - rate 45 */
198 10125000, /* 81Mbps - rate 46 */
199 10375000, /* 83Mbps - rate 47 */
200 10625000, /* 85Mbps - rate 48 */
201 10875000, /* 87Mbps - rate 49 */
202 11125000, /* 89Mbps - rate 50 */
203 11375000, /* 91Mbps - rate 51 */
204 11625000, /* 93Mbps - rate 52 */
205 11875000, /* 95Mbps - rate 53 */
206 13125000, /* 105Mbps - rate 54 */
207 13750000, /* 110Mbps - rate 55 */
208 14375000, /* 115Mbps - rate 56 */
209 15000000, /* 120Mbps - rate 57 */
210 15625000, /* 125Mbps - rate 58 */
211 16250000, /* 130Mbps - rate 59 */
212 16875000, /* 135Mbps - rate 60 */
213 17500000, /* 140Mbps - rate 61 */
214 18125000, /* 145Mbps - rate 62 */
215 18750000, /* 150Mbps - rate 64 */
216 20000000, /* 160Mbps - rate 65 */
217 21250000, /* 170Mbps - rate 66 */
218 22500000, /* 180Mbps - rate 67 */
219 23750000, /* 190Mbps - rate 68 */
220 26250000, /* 210Mbps - rate 69 */
221 27500000, /* 220Mbps - rate 70 */
222 28750000, /* 230Mbps - rate 71 */
223 30000000, /* 240Mbps - rate 72 */
224 31250000, /* 250Mbps - rate 73 */
225 34375000, /* 275Mbps - rate 74 */
226 37500000, /* 300Mbps - rate 75 */
227 40625000, /* 325Mbps - rate 76 */
228 43750000, /* 350Mbps - rate 77 */
229 46875000, /* 375Mbps - rate 78 */
230 53125000, /* 425Mbps - rate 79 */
231 56250000, /* 450Mbps - rate 80 */
232 59375000, /* 475Mbps - rate 81 */
233 62500000, /* 500Mbps - rate 82 */
234 68750000, /* 550Mbps - rate 83 */
235 75000000, /* 600Mbps - rate 84 */
236 81250000, /* 650Mbps - rate 85 */
237 87500000, /* 700Mbps - rate 86 */
238 93750000, /* 750Mbps - rate 87 */
239 106250000, /* 850Mbps - rate 88 */
240 112500000, /* 900Mbps - rate 89 */
241 125000000, /* 1Gbps - rate 90 */
242 156250000, /* 1.25Gps - rate 91 */
243 187500000, /* 1.5Gps - rate 92 */
244 218750000, /* 1.75Gps - rate 93 */
245 250000000, /* 2Gbps - rate 94 */
246 281250000, /* 2.25Gps - rate 95 */
247 312500000, /* 2.5Gbps - rate 96 */
248 343750000, /* 2.75Gbps - rate 97 */
249 375000000, /* 3Gbps - rate 98 */
250 500000000, /* 4Gbps - rate 99 */
251 625000000, /* 5Gbps - rate 100 */
252 750000000, /* 6Gbps - rate 101 */
253 875000000, /* 7Gbps - rate 102 */
254 1000000000, /* 8Gbps - rate 103 */
255 1125000000, /* 9Gbps - rate 104 */
256 1250000000, /* 10Gbps - rate 105 */
257 1875000000, /* 15Gbps - rate 106 */
258 2500000000 /* 20Gbps - rate 107 */
261 #define MAX_HDWR_RATES (sizeof(desired_rates)/sizeof(uint64_t))
262 #define RS_ORDERED_COUNT 16 /*
263 * Number that are in order
264 * at the beginning of the table,
265 * over this a sort is required.
267 #define RS_NEXT_ORDER_GROUP 16 /*
268 * The point in our table where
269 * we come fill in a second ordered
270 * group (index wise means -1).
272 #define ALL_HARDWARE_RATES 1004 /*
273 * 1Meg - 1Gig in 1 Meg steps
274 * plus 100, 200k and 500k and
278 #define RS_ONE_MEGABIT_PERSEC 1000000
279 #define RS_ONE_GIGABIT_PERSEC 1000000000
280 #define RS_TEN_GIGABIT_PERSEC 10000000000
282 static struct head_tcp_rate_set int_rs;
283 static struct mtx rs_mtx;
284 uint32_t rs_number_alive;
285 uint32_t rs_number_dead;
287 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, rl, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
288 "TCP Ratelimit stats");
289 SYSCTL_UINT(_net_inet_tcp_rl, OID_AUTO, alive, CTLFLAG_RW,
291 "Number of interfaces initialized for ratelimiting");
292 SYSCTL_UINT(_net_inet_tcp_rl, OID_AUTO, dead, CTLFLAG_RW,
294 "Number of interfaces departing from ratelimiting");
297 rl_add_syctl_entries(struct sysctl_oid *rl_sysctl_root, struct tcp_rate_set *rs)
300 * Add sysctl entries for thus interface.
302 if (rs->rs_flags & RS_INTF_NO_SUP) {
303 SYSCTL_ADD_S32(&rs->sysctl_ctx,
304 SYSCTL_CHILDREN(rl_sysctl_root),
305 OID_AUTO, "disable", CTLFLAG_RD,
307 "Disable this interface from new hdwr limiting?");
309 SYSCTL_ADD_S32(&rs->sysctl_ctx,
310 SYSCTL_CHILDREN(rl_sysctl_root),
311 OID_AUTO, "disable", CTLFLAG_RW,
313 "Disable this interface from new hdwr limiting?");
315 SYSCTL_ADD_S32(&rs->sysctl_ctx,
316 SYSCTL_CHILDREN(rl_sysctl_root),
317 OID_AUTO, "minseg", CTLFLAG_RW,
319 "What is the minimum we need to send on this interface?");
320 SYSCTL_ADD_U64(&rs->sysctl_ctx,
321 SYSCTL_CHILDREN(rl_sysctl_root),
322 OID_AUTO, "flow_limit", CTLFLAG_RW,
323 &rs->rs_flow_limit, 0,
324 "What is the limit for number of flows (0=unlimited)?");
325 SYSCTL_ADD_S32(&rs->sysctl_ctx,
326 SYSCTL_CHILDREN(rl_sysctl_root),
327 OID_AUTO, "highest", CTLFLAG_RD,
328 &rs->rs_highest_valid, 0,
329 "Highest valid rate");
330 SYSCTL_ADD_S32(&rs->sysctl_ctx,
331 SYSCTL_CHILDREN(rl_sysctl_root),
332 OID_AUTO, "lowest", CTLFLAG_RD,
333 &rs->rs_lowest_valid, 0,
334 "Lowest valid rate");
335 SYSCTL_ADD_S32(&rs->sysctl_ctx,
336 SYSCTL_CHILDREN(rl_sysctl_root),
337 OID_AUTO, "flags", CTLFLAG_RD,
339 "What lags are on the entry?");
340 SYSCTL_ADD_S32(&rs->sysctl_ctx,
341 SYSCTL_CHILDREN(rl_sysctl_root),
342 OID_AUTO, "numrates", CTLFLAG_RD,
344 "How many rates re there?");
345 SYSCTL_ADD_U64(&rs->sysctl_ctx,
346 SYSCTL_CHILDREN(rl_sysctl_root),
347 OID_AUTO, "flows_using", CTLFLAG_RD,
348 &rs->rs_flows_using, 0,
349 "How many flows are using this interface now?");
350 #ifdef DETAILED_RATELIMIT_SYSCTL
351 if (rs->rs_rlt && rs->rs_rate_cnt > 0) {
352 /* Lets display the rates */
354 struct sysctl_oid *rl_rates;
355 struct sysctl_oid *rl_rate_num;
357 rl_rates = SYSCTL_ADD_NODE(&rs->sysctl_ctx,
358 SYSCTL_CHILDREN(rl_sysctl_root),
361 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
363 for( i = 0; i < rs->rs_rate_cnt; i++) {
364 sprintf(rate_num, "%d", i);
365 rl_rate_num = SYSCTL_ADD_NODE(&rs->sysctl_ctx,
366 SYSCTL_CHILDREN(rl_rates),
369 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
371 SYSCTL_ADD_U32(&rs->sysctl_ctx,
372 SYSCTL_CHILDREN(rl_rate_num),
373 OID_AUTO, "flags", CTLFLAG_RD,
374 &rs->rs_rlt[i].flags, 0,
375 "Flags on this rate");
376 SYSCTL_ADD_U32(&rs->sysctl_ctx,
377 SYSCTL_CHILDREN(rl_rate_num),
378 OID_AUTO, "pacetime", CTLFLAG_RD,
379 &rs->rs_rlt[i].time_between, 0,
380 "Time hardware inserts between 1500 byte sends");
381 SYSCTL_ADD_U64(&rs->sysctl_ctx,
382 SYSCTL_CHILDREN(rl_rate_num),
383 OID_AUTO, "rate", CTLFLAG_RD,
384 &rs->rs_rlt[i].rate, 0,
385 "Rate in bytes per second");
392 rs_destroy(epoch_context_t ctx)
394 struct tcp_rate_set *rs;
397 rs = __containerof(ctx, struct tcp_rate_set, rs_epoch_ctx);
400 rs->rs_flags &= ~RS_FUNERAL_SCHD;
402 * In theory its possible (but unlikely)
403 * that while the delete was occuring
404 * and we were applying the DEAD flag
405 * someone slipped in and found the
406 * interface in a lookup. While we
407 * decided rs_flows_using were 0 and
408 * scheduling the epoch_call, the other
409 * thread incremented rs_flow_using. This
410 * is because users have a pointer and
411 * we only use the rs_flows_using in an
412 * atomic fashion, i.e. the other entities
413 * are not protected. To assure this did
414 * not occur, we check rs_flows_using here
417 do_free_rs = (rs->rs_flows_using == 0);
422 sysctl_ctx_free(&rs->sysctl_ctx);
423 free(rs->rs_rlt, M_TCPPACE);
429 rs_defer_destroy(struct tcp_rate_set *rs)
432 mtx_assert(&rs_mtx, MA_OWNED);
434 /* Check if already pending. */
435 if (rs->rs_flags & RS_FUNERAL_SCHD)
440 /* Set flag to only defer once. */
441 rs->rs_flags |= RS_FUNERAL_SCHD;
442 NET_EPOCH_CALL(rs_destroy, &rs->rs_epoch_ctx);
446 extern counter_u64_t rate_limit_set_ok;
447 extern counter_u64_t rate_limit_active;
448 extern counter_u64_t rate_limit_alloc_fail;
452 rl_attach_txrtlmt(struct ifnet *ifp,
456 struct m_snd_tag **tag)
459 union if_snd_tag_alloc_params params = {
460 .rate_limit.hdr.type = IF_SND_TAG_TYPE_RATE_LIMIT,
461 .rate_limit.hdr.flowid = flowid,
462 .rate_limit.hdr.flowtype = flowtype,
463 .rate_limit.max_rate = cfg_rate,
464 .rate_limit.flags = M_NOWAIT,
467 error = m_snd_tag_alloc(ifp, ¶ms, tag);
470 counter_u64_add(rate_limit_set_ok, 1);
471 counter_u64_add(rate_limit_active, 1);
472 } else if (error != EOPNOTSUPP)
473 counter_u64_add(rate_limit_alloc_fail, 1);
479 populate_canned_table(struct tcp_rate_set *rs, const uint64_t *rate_table_act)
482 * The internal table is "special", it
483 * is two seperate ordered tables that
484 * must be merged. We get here when the
485 * adapter specifies a number of rates that
486 * covers both ranges in the table in some
489 int i, at_low, at_high;
490 uint8_t low_disabled = 0, high_disabled = 0;
492 for(i = 0, at_low = 0, at_high = RS_NEXT_ORDER_GROUP; i < rs->rs_rate_cnt; i++) {
493 rs->rs_rlt[i].flags = 0;
494 rs->rs_rlt[i].time_between = 0;
495 if ((low_disabled == 0) &&
497 (rate_table_act[at_low] < rate_table_act[at_high]))) {
498 rs->rs_rlt[i].rate = rate_table_act[at_low];
500 if (at_low == RS_NEXT_ORDER_GROUP)
502 } else if (high_disabled == 0) {
503 rs->rs_rlt[i].rate = rate_table_act[at_high];
505 if (at_high == MAX_HDWR_RATES)
511 static struct tcp_rate_set *
512 rt_setup_new_rs(struct ifnet *ifp, int *error)
514 struct tcp_rate_set *rs;
515 const uint64_t *rate_table_act;
516 uint64_t lentim, res;
520 struct if_ratelimit_query_results rl;
521 struct sysctl_oid *rl_sysctl_root;
523 * We expect to enter with the
527 if (ifp->if_ratelimit_query == NULL) {
529 * We can do nothing if we cannot
530 * get a query back from the driver.
532 printf("Warning:No query functions for %s:%d-- failed\n",
533 ifp->if_dname, ifp->if_dunit);
536 rs = malloc(sizeof(struct tcp_rate_set), M_TCPPACE, M_NOWAIT | M_ZERO);
540 printf("Warning:No memory for malloc of tcp_rate_set\n");
543 memset(&rl, 0, sizeof(rl));
544 rl.flags = RT_NOSUPPORT;
545 ifp->if_ratelimit_query(ifp, &rl);
546 if (rl.flags & RT_IS_UNUSABLE) {
548 * The interface does not really support
551 memset(rs, 0, sizeof(struct tcp_rate_set));
553 rs->rs_if_dunit = ifp->if_dunit;
554 rs->rs_flags = RS_INTF_NO_SUP;
557 sysctl_ctx_init(&rs->sysctl_ctx);
558 rl_sysctl_root = SYSCTL_ADD_NODE(&rs->sysctl_ctx,
559 SYSCTL_STATIC_CHILDREN(_net_inet_tcp_rl),
561 rs->rs_ifp->if_xname,
562 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
564 rl_add_syctl_entries(rl_sysctl_root, rs);
566 CK_LIST_INSERT_HEAD(&int_rs, rs, next);
569 } else if ((rl.flags & RT_IS_INDIRECT) == RT_IS_INDIRECT) {
570 memset(rs, 0, sizeof(struct tcp_rate_set));
572 rs->rs_if_dunit = ifp->if_dunit;
573 rs->rs_flags = RS_IS_DEFF;
575 sysctl_ctx_init(&rs->sysctl_ctx);
576 rl_sysctl_root = SYSCTL_ADD_NODE(&rs->sysctl_ctx,
577 SYSCTL_STATIC_CHILDREN(_net_inet_tcp_rl),
579 rs->rs_ifp->if_xname,
580 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
582 rl_add_syctl_entries(rl_sysctl_root, rs);
584 CK_LIST_INSERT_HEAD(&int_rs, rs, next);
587 } else if ((rl.flags & RT_IS_FIXED_TABLE) == RT_IS_FIXED_TABLE) {
588 /* Mellanox C4 likely */
590 rs->rs_if_dunit = ifp->if_dunit;
591 rs->rs_rate_cnt = rl.number_of_rates;
592 rs->rs_min_seg = rl.min_segment_burst;
593 rs->rs_highest_valid = 0;
594 rs->rs_flow_limit = rl.max_flows;
595 rs->rs_flags = RS_IS_INTF | RS_NO_PRE;
597 rate_table_act = rl.rate_table;
598 } else if ((rl.flags & RT_IS_SELECTABLE) == RT_IS_SELECTABLE) {
599 /* Chelsio, C5 and C6 of Mellanox? */
601 rs->rs_if_dunit = ifp->if_dunit;
602 rs->rs_rate_cnt = rl.number_of_rates;
603 rs->rs_min_seg = rl.min_segment_burst;
605 rs->rs_flow_limit = rl.max_flows;
606 rate_table_act = desired_rates;
607 if ((rs->rs_rate_cnt > MAX_HDWR_RATES) &&
608 (rs->rs_rate_cnt < ALL_HARDWARE_RATES)) {
610 * Our desired table is not big
611 * enough, do what we can.
613 rs->rs_rate_cnt = MAX_HDWR_RATES;
615 if (rs->rs_rate_cnt <= RS_ORDERED_COUNT)
616 rs->rs_flags = RS_IS_INTF;
618 rs->rs_flags = RS_IS_INTF | RS_INT_TBL;
619 if (rs->rs_rate_cnt >= ALL_HARDWARE_RATES)
620 rs->rs_rate_cnt = ALL_HARDWARE_RATES;
625 sz = sizeof(struct tcp_hwrate_limit_table) * rs->rs_rate_cnt;
626 rs->rs_rlt = malloc(sz, M_TCPPACE, M_NOWAIT);
627 if (rs->rs_rlt == NULL) {
634 if (rs->rs_rate_cnt >= ALL_HARDWARE_RATES) {
636 * The interface supports all
637 * the rates we could possibly want.
641 rs->rs_rlt[0].rate = 12500; /* 100k */
642 rs->rs_rlt[1].rate = 25000; /* 200k */
643 rs->rs_rlt[2].rate = 62500; /* 500k */
644 /* Note 125000 == 1Megabit
645 * populate 1Meg - 1000meg.
647 for(i = 3, rat = 125000; i< (ALL_HARDWARE_RATES-1); i++) {
648 rs->rs_rlt[i].rate = rat;
651 rs->rs_rlt[(ALL_HARDWARE_RATES-1)].rate = 1250000000;
652 } else if (rs->rs_flags & RS_INT_TBL) {
653 /* We populate this in a special way */
654 populate_canned_table(rs, rate_table_act);
657 * Just copy in the rates from
658 * the table, it is in order.
660 for (i=0; i<rs->rs_rate_cnt; i++) {
661 rs->rs_rlt[i].rate = rate_table_act[i];
662 rs->rs_rlt[i].time_between = 0;
663 rs->rs_rlt[i].flags = 0;
666 for (i = (rs->rs_rate_cnt - 1); i >= 0; i--) {
668 * We go backwards through the list so that if we can't get
669 * a rate and fail to init one, we have at least a chance of
670 * getting the highest one.
672 rs->rs_rlt[i].ptbl = rs;
673 rs->rs_rlt[i].tag = NULL;
675 * Calculate the time between.
677 lentim = ETHERNET_SEGMENT_SIZE * USECS_IN_SECOND;
678 res = lentim / rs->rs_rlt[i].rate;
680 rs->rs_rlt[i].time_between = res;
682 rs->rs_rlt[i].time_between = 1;
683 if (rs->rs_flags & RS_NO_PRE) {
684 rs->rs_rlt[i].flags = HDWRPACE_INITED;
685 rs->rs_lowest_valid = i;
689 if ((rl.flags & RT_IS_SETUP_REQ) &&
690 (ifp->if_ratelimit_query)) {
691 err = ifp->if_ratelimit_setup(ifp,
692 rs->rs_rlt[i].rate, i);
697 hash_type = M_HASHTYPE_RSS_TCP_IPV4;
699 hash_type = M_HASHTYPE_OPAQUE_HASH;
701 err = rl_attach_txrtlmt(ifp,
708 if (i == (rs->rs_rate_cnt - 1)) {
710 * Huh - first rate and we can't get
713 free(rs->rs_rlt, M_TCPPACE);
723 rs->rs_rlt[i].flags = HDWRPACE_INITED | HDWRPACE_TAGPRESENT;
724 rs->rs_lowest_valid = i;
728 /* Did we get at least 1 rate? */
729 if (rs->rs_rlt[(rs->rs_rate_cnt - 1)].flags & HDWRPACE_INITED)
730 rs->rs_highest_valid = rs->rs_rate_cnt - 1;
732 free(rs->rs_rlt, M_TCPPACE);
736 sysctl_ctx_init(&rs->sysctl_ctx);
737 rl_sysctl_root = SYSCTL_ADD_NODE(&rs->sysctl_ctx,
738 SYSCTL_STATIC_CHILDREN(_net_inet_tcp_rl),
740 rs->rs_ifp->if_xname,
741 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
743 rl_add_syctl_entries(rl_sysctl_root, rs);
745 CK_LIST_INSERT_HEAD(&int_rs, rs, next);
750 static const struct tcp_hwrate_limit_table *
751 tcp_int_find_suitable_rate(const struct tcp_rate_set *rs,
752 uint64_t bytes_per_sec, uint32_t flags)
754 struct tcp_hwrate_limit_table *arte = NULL, *rte = NULL;
755 uint64_t mbits_per_sec, ind_calc;
758 mbits_per_sec = (bytes_per_sec * 8);
759 if (flags & RS_PACING_LT) {
760 if ((mbits_per_sec < RS_ONE_MEGABIT_PERSEC) &&
761 (rs->rs_lowest_valid <= 2)){
763 * Smaller than 1Meg, only
764 * 3 entries can match it.
766 for(i = rs->rs_lowest_valid; i < 3; i++) {
767 if (bytes_per_sec <= rs->rs_rlt[i].rate) {
768 rte = &rs->rs_rlt[i];
770 } else if (rs->rs_rlt[i].flags & HDWRPACE_INITED) {
771 arte = &rs->rs_rlt[i];
775 } else if ((mbits_per_sec > RS_ONE_GIGABIT_PERSEC) &&
776 (rs->rs_rlt[(ALL_HARDWARE_RATES-1)].flags & HDWRPACE_INITED)){
778 * Larger than 1G (the majority of
781 if (mbits_per_sec < RS_TEN_GIGABIT_PERSEC)
782 rte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
784 arte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
788 * If we reach here its in our table (between 1Meg - 1000Meg),
789 * just take the rounded down mbits per second, and add
790 * 1Megabit to it, from this we can calculate
791 * the index in the table.
793 ind_calc = mbits_per_sec/RS_ONE_MEGABIT_PERSEC;
794 if ((ind_calc * RS_ONE_MEGABIT_PERSEC) != mbits_per_sec)
796 /* our table is offset by 3, we add 2 */
798 if (ind_calc > (ALL_HARDWARE_RATES-1)) {
799 /* This should not happen */
800 ind_calc = ALL_HARDWARE_RATES-1;
802 if ((ind_calc >= rs->rs_lowest_valid) &&
803 (ind_calc <= rs->rs_highest_valid))
804 rte = &rs->rs_rlt[ind_calc];
805 } else if (flags & RS_PACING_EXACT_MATCH) {
806 if ((mbits_per_sec < RS_ONE_MEGABIT_PERSEC) &&
807 (rs->rs_lowest_valid <= 2)){
808 for(i = rs->rs_lowest_valid; i < 3; i++) {
809 if (bytes_per_sec == rs->rs_rlt[i].rate) {
810 rte = &rs->rs_rlt[i];
814 } else if ((mbits_per_sec > RS_ONE_GIGABIT_PERSEC) &&
815 (rs->rs_rlt[(ALL_HARDWARE_RATES-1)].flags & HDWRPACE_INITED)) {
816 /* > 1Gbps only one rate */
817 if (bytes_per_sec == rs->rs_rlt[(ALL_HARDWARE_RATES-1)].rate) {
819 rte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
822 /* Ok it must be a exact meg (its between 1G and 1Meg) */
823 ind_calc = mbits_per_sec/RS_ONE_MEGABIT_PERSEC;
824 if ((ind_calc * RS_ONE_MEGABIT_PERSEC) == mbits_per_sec) {
825 /* its an exact Mbps */
827 if (ind_calc > (ALL_HARDWARE_RATES-1)) {
828 /* This should not happen */
829 ind_calc = ALL_HARDWARE_RATES-1;
831 if (rs->rs_rlt[ind_calc].flags & HDWRPACE_INITED)
832 rte = &rs->rs_rlt[ind_calc];
836 /* we want greater than the requested rate */
837 if ((mbits_per_sec < RS_ONE_MEGABIT_PERSEC) &&
838 (rs->rs_lowest_valid <= 2)){
839 arte = &rs->rs_rlt[3]; /* set alternate to 1Meg */
840 for (i=2; i>=rs->rs_lowest_valid; i--) {
841 if (bytes_per_sec < rs->rs_rlt[i].rate) {
842 rte = &rs->rs_rlt[i];
844 } else if ((flags & RS_PACING_GEQ) &&
845 (bytes_per_sec == rs->rs_rlt[i].rate)) {
846 rte = &rs->rs_rlt[i];
849 arte = &rs->rs_rlt[i]; /* new alternate */
852 } else if (mbits_per_sec > RS_ONE_GIGABIT_PERSEC) {
853 if ((bytes_per_sec < rs->rs_rlt[(ALL_HARDWARE_RATES-1)].rate) &&
854 (rs->rs_rlt[(ALL_HARDWARE_RATES-1)].flags & HDWRPACE_INITED)){
855 /* Our top rate is larger than the request */
856 rte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
857 } else if ((flags & RS_PACING_GEQ) &&
858 (bytes_per_sec == rs->rs_rlt[(ALL_HARDWARE_RATES-1)].rate) &&
859 (rs->rs_rlt[(ALL_HARDWARE_RATES-1)].flags & HDWRPACE_INITED)) {
860 /* It matches our top rate */
861 rte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
862 } else if (rs->rs_rlt[(ALL_HARDWARE_RATES-1)].flags & HDWRPACE_INITED) {
863 /* The top rate is an alternative */
864 arte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
867 /* Its in our range 1Meg - 1Gig */
868 if (flags & RS_PACING_GEQ) {
869 ind_calc = mbits_per_sec/RS_ONE_MEGABIT_PERSEC;
870 if ((ind_calc * RS_ONE_MEGABIT_PERSEC) == mbits_per_sec) {
871 if (ind_calc > (ALL_HARDWARE_RATES-1)) {
872 /* This should not happen */
873 ind_calc = (ALL_HARDWARE_RATES-1);
875 rte = &rs->rs_rlt[ind_calc];
879 ind_calc = (mbits_per_sec + (RS_ONE_MEGABIT_PERSEC-1))/RS_ONE_MEGABIT_PERSEC;
881 if (ind_calc > (ALL_HARDWARE_RATES-1)) {
882 /* This should not happen */
883 ind_calc = ALL_HARDWARE_RATES-1;
885 if (rs->rs_rlt[ind_calc].flags & HDWRPACE_INITED)
886 rte = &rs->rs_rlt[ind_calc];
892 (flags & RS_PACING_SUB_OK)) {
893 /* We can use the substitute */
899 static const struct tcp_hwrate_limit_table *
900 tcp_find_suitable_rate(const struct tcp_rate_set *rs, uint64_t bytes_per_sec, uint32_t flags)
903 * Hunt the rate table with the restrictions in flags and find a
904 * suitable rate if possible.
905 * RS_PACING_EXACT_MATCH - look for an exact match to rate.
906 * RS_PACING_GT - must be greater than.
907 * RS_PACING_GEQ - must be greater than or equal.
908 * RS_PACING_LT - must be less than.
909 * RS_PACING_SUB_OK - If we don't meet criteria a
913 struct tcp_hwrate_limit_table *rte = NULL;
915 if ((rs->rs_flags & RS_INT_TBL) &&
916 (rs->rs_rate_cnt >= ALL_HARDWARE_RATES)) {
918 * Here we don't want to paw thru
919 * a big table, we have everything
920 * from 1Meg - 1000Meg in 1Meg increments.
921 * Use an alternate method to "lookup".
923 return (tcp_int_find_suitable_rate(rs, bytes_per_sec, flags));
925 if ((flags & RS_PACING_LT) ||
926 (flags & RS_PACING_EXACT_MATCH)) {
928 * For exact and less than we go forward through the table.
929 * This way when we find one larger we stop (exact was a
932 for (i = rs->rs_lowest_valid, matched = 0; i <= rs->rs_highest_valid; i++) {
933 if ((flags & RS_PACING_EXACT_MATCH) &&
934 (bytes_per_sec == rs->rs_rlt[i].rate)) {
935 rte = &rs->rs_rlt[i];
938 } else if ((flags & RS_PACING_LT) &&
939 (bytes_per_sec <= rs->rs_rlt[i].rate)) {
940 rte = &rs->rs_rlt[i];
944 if (bytes_per_sec > rs->rs_rlt[i].rate)
947 if ((matched == 0) &&
948 (flags & RS_PACING_LT) &&
949 (flags & RS_PACING_SUB_OK)) {
950 /* Kick in a substitute (the lowest) */
951 rte = &rs->rs_rlt[rs->rs_lowest_valid];
955 * Here we go backward through the table so that we can find
956 * the one greater in theory faster (but its probably a
959 for (i = rs->rs_highest_valid, matched = 0; i >= rs->rs_lowest_valid; i--) {
960 if (rs->rs_rlt[i].rate > bytes_per_sec) {
961 /* A possible candidate */
962 rte = &rs->rs_rlt[i];
964 if ((flags & RS_PACING_GEQ) &&
965 (bytes_per_sec == rs->rs_rlt[i].rate)) {
966 /* An exact match and we want equal */
968 rte = &rs->rs_rlt[i];
972 * Found one that is larger than but don't
973 * stop, there may be a more closer match.
977 if (rs->rs_rlt[i].rate < bytes_per_sec) {
979 * We found a table entry that is smaller,
980 * stop there will be none greater or equal.
985 if ((matched == 0) &&
986 (flags & RS_PACING_SUB_OK)) {
987 /* Kick in a substitute (the highest) */
988 rte = &rs->rs_rlt[rs->rs_highest_valid];
994 static struct ifnet *
995 rt_find_real_interface(struct ifnet *ifp, struct inpcb *inp, int *error)
998 struct m_snd_tag *tag;
999 union if_snd_tag_alloc_params params = {
1000 .rate_limit.hdr.type = IF_SND_TAG_TYPE_RATE_LIMIT,
1001 .rate_limit.hdr.flowid = 1,
1002 .rate_limit.hdr.numa_domain = inp->inp_numa_domain,
1003 .rate_limit.max_rate = COMMON_RATE,
1004 .rate_limit.flags = M_NOWAIT,
1008 params.rate_limit.hdr.flowtype = ((inp->inp_vflag & INP_IPV6) ?
1009 M_HASHTYPE_RSS_TCP_IPV6 : M_HASHTYPE_RSS_TCP_IPV4);
1011 params.rate_limit.hdr.flowtype = M_HASHTYPE_OPAQUE_HASH;
1013 err = m_snd_tag_alloc(ifp, ¶ms, &tag);
1015 /* Failed to setup a tag? */
1021 m_snd_tag_rele(tag);
1025 static const struct tcp_hwrate_limit_table *
1026 rt_setup_rate(struct inpcb *inp, struct ifnet *ifp, uint64_t bytes_per_sec,
1027 uint32_t flags, int *error)
1029 /* First lets find the interface if it exists */
1030 const struct tcp_hwrate_limit_table *rte;
1031 struct tcp_rate_set *rs;
1032 struct epoch_tracker et;
1035 NET_EPOCH_ENTER(et);
1037 CK_LIST_FOREACH(rs, &int_rs, next) {
1039 * Note we don't look with the lock since we either see a
1040 * new entry or will get one when we try to add it.
1042 if (rs->rs_flags & RS_IS_DEAD) {
1043 /* The dead are not looked at */
1046 if ((rs->rs_ifp == ifp) &&
1047 (rs->rs_if_dunit == ifp->if_dunit)) {
1048 /* Ok we found it */
1053 (rs->rs_flags & RS_INTF_NO_SUP) ||
1054 (rs->rs_flags & RS_IS_DEAD)) {
1056 * This means we got a packet *before*
1057 * the IF-UP was processed below, <or>
1058 * while or after we already received an interface
1059 * departed event. In either case we really don't
1060 * want to do anything with pacing, in
1061 * the departing case the packet is not
1062 * going to go very far. The new case
1063 * might be arguable, but its impossible
1064 * to tell from the departing case.
1066 if (rs->rs_disable && error)
1072 if ((rs == NULL) || (rs->rs_disable != 0)) {
1073 if (rs->rs_disable && error)
1078 if (rs->rs_flags & RS_IS_DEFF) {
1079 /* We need to find the real interface */
1082 tifp = rt_find_real_interface(ifp, inp, error);
1084 if (rs->rs_disable && error)
1089 goto use_real_interface;
1091 if (rs->rs_flow_limit &&
1092 ((rs->rs_flows_using + 1) > rs->rs_flow_limit)) {
1098 rte = tcp_find_suitable_rate(rs, bytes_per_sec, flags);
1100 err = in_pcbattach_txrtlmt(inp, rs->rs_ifp,
1106 /* Failed to attach */
1114 * We use an atomic here for accounting so we don't have to
1115 * use locks when freeing.
1117 atomic_add_64(&rs->rs_flows_using, 1);
1124 tcp_rl_ifnet_link(void *arg __unused, struct ifnet *ifp, int link_state)
1127 struct tcp_rate_set *rs;
1129 if (((ifp->if_capenable & IFCAP_TXRTLMT) == 0) ||
1130 (link_state != LINK_STATE_UP)) {
1132 * We only care on an interface going up that is rate-limit
1138 CK_LIST_FOREACH(rs, &int_rs, next) {
1139 if ((rs->rs_ifp == ifp) &&
1140 (rs->rs_if_dunit == ifp->if_dunit)) {
1141 /* We already have initialized this guy */
1142 mtx_unlock(&rs_mtx);
1146 mtx_unlock(&rs_mtx);
1147 rt_setup_new_rs(ifp, &error);
1151 tcp_rl_ifnet_departure(void *arg __unused, struct ifnet *ifp)
1153 struct tcp_rate_set *rs, *nrs;
1157 CK_LIST_FOREACH_SAFE(rs, &int_rs, next, nrs) {
1158 if ((rs->rs_ifp == ifp) &&
1159 (rs->rs_if_dunit == ifp->if_dunit)) {
1160 CK_LIST_REMOVE(rs, next);
1162 rs->rs_flags |= RS_IS_DEAD;
1163 for (i = 0; i < rs->rs_rate_cnt; i++) {
1164 if (rs->rs_rlt[i].flags & HDWRPACE_TAGPRESENT) {
1165 in_pcbdetach_tag(rs->rs_rlt[i].tag);
1166 rs->rs_rlt[i].tag = NULL;
1168 rs->rs_rlt[i].flags = HDWRPACE_IFPDEPARTED;
1170 if (rs->rs_flows_using == 0)
1171 rs_defer_destroy(rs);
1175 mtx_unlock(&rs_mtx);
1179 tcp_rl_shutdown(void *arg __unused, int howto __unused)
1181 struct tcp_rate_set *rs, *nrs;
1185 CK_LIST_FOREACH_SAFE(rs, &int_rs, next, nrs) {
1186 CK_LIST_REMOVE(rs, next);
1188 rs->rs_flags |= RS_IS_DEAD;
1189 for (i = 0; i < rs->rs_rate_cnt; i++) {
1190 if (rs->rs_rlt[i].flags & HDWRPACE_TAGPRESENT) {
1191 in_pcbdetach_tag(rs->rs_rlt[i].tag);
1192 rs->rs_rlt[i].tag = NULL;
1194 rs->rs_rlt[i].flags = HDWRPACE_IFPDEPARTED;
1196 if (rs->rs_flows_using == 0)
1197 rs_defer_destroy(rs);
1199 mtx_unlock(&rs_mtx);
1202 const struct tcp_hwrate_limit_table *
1203 tcp_set_pacing_rate(struct tcpcb *tp, struct ifnet *ifp,
1204 uint64_t bytes_per_sec, int flags, int *error)
1206 const struct tcp_hwrate_limit_table *rte;
1208 struct ktls_session *tls;
1211 INP_WLOCK_ASSERT(tp->t_inpcb);
1213 if (tp->t_inpcb->inp_snd_tag == NULL) {
1215 * We are setting up a rate for the first time.
1217 if ((ifp->if_capenable & IFCAP_TXRTLMT) == 0) {
1218 /* Not supported by the egress */
1225 if (tp->t_inpcb->inp_socket->so_snd.sb_flags & SB_TLS_IFNET) {
1226 tls = tp->t_inpcb->inp_socket->so_snd.sb_tls_info;
1228 if ((ifp->if_capenable & IFCAP_TXTLS_RTLMT) == 0 ||
1229 tls->mode != TCP_TLS_MODE_IFNET) {
1236 rte = rt_setup_rate(tp->t_inpcb, ifp, bytes_per_sec, flags, error);
1238 if (rte != NULL && tls != NULL && tls->snd_tag != NULL) {
1240 * Fake a route change error to reset the TLS
1241 * send tag. This will convert the existing
1242 * tag to a TLS ratelimit tag.
1244 MPASS(tls->snd_tag->type == IF_SND_TAG_TYPE_TLS);
1245 ktls_output_eagain(tp->t_inpcb, tls);
1250 * We are modifying a rate, wrong interface?
1256 tp->t_pacing_rate = rte->rate;
1261 const struct tcp_hwrate_limit_table *
1262 tcp_chg_pacing_rate(const struct tcp_hwrate_limit_table *crte,
1263 struct tcpcb *tp, struct ifnet *ifp,
1264 uint64_t bytes_per_sec, int flags, int *error)
1266 const struct tcp_hwrate_limit_table *nrte;
1267 const struct tcp_rate_set *rs;
1269 struct ktls_session *tls = NULL;
1271 int is_indirect = 0;
1274 INP_WLOCK_ASSERT(tp->t_inpcb);
1277 /* Wrong interface */
1284 if (tp->t_inpcb->inp_socket->so_snd.sb_flags & SB_TLS_IFNET) {
1285 tls = tp->t_inpcb->inp_socket->so_snd.sb_tls_info;
1286 MPASS(tls->mode == TCP_TLS_MODE_IFNET);
1287 if (tls->snd_tag != NULL &&
1288 tls->snd_tag->type != IF_SND_TAG_TYPE_TLS_RATE_LIMIT) {
1290 * NIC probably doesn't support ratelimit TLS
1291 * tags if it didn't allocate one when an
1292 * existing rate was present, so ignore.
1295 *error = EOPNOTSUPP;
1300 if (tp->t_inpcb->inp_snd_tag == NULL) {
1301 /* Wrong interface */
1307 if ((rs->rs_flags & RS_IS_DEAD) ||
1308 (crte->flags & HDWRPACE_IFPDEPARTED)) {
1309 /* Release the rate, and try anew */
1311 tcp_rel_pacing_rate(crte, tp);
1312 nrte = tcp_set_pacing_rate(tp, ifp,
1313 bytes_per_sec, flags, error);
1316 if ((rs->rs_flags & RT_IS_INDIRECT ) == RT_IS_INDIRECT)
1320 if ((is_indirect == 0) &&
1321 ((ifp != rs->rs_ifp) ||
1322 (ifp->if_dunit != rs->rs_if_dunit))) {
1324 * Something changed, the user is not pointing to the same
1325 * ifp? Maybe a route updated on this guy?
1328 } else if (is_indirect) {
1330 * For indirect we have to dig in and find the real interface.
1334 rifp = rt_find_real_interface(ifp, tp->t_inpcb, error);
1336 /* Can't find it? */
1339 if ((rifp != rs->rs_ifp) ||
1340 (ifp->if_dunit != rs->rs_if_dunit)) {
1344 nrte = tcp_find_suitable_rate(rs, bytes_per_sec, flags);
1352 /* Release the old rate */
1353 tcp_rel_pacing_rate(crte, tp);
1356 /* Change rates to our new entry */
1359 err = ktls_modify_txrtlmt(tls, nrte->rate);
1362 err = in_pcbmodify_txrtlmt(tp->t_inpcb, nrte->rate);
1370 tp->t_pacing_rate = nrte->rate;
1375 tcp_rel_pacing_rate(const struct tcp_hwrate_limit_table *crte, struct tcpcb *tp)
1377 const struct tcp_rate_set *crs;
1378 struct tcp_rate_set *rs;
1381 INP_WLOCK_ASSERT(tp->t_inpcb);
1383 tp->t_pacing_rate = -1;
1386 * Now we must break the const
1387 * in order to release our refcount.
1389 rs = __DECONST(struct tcp_rate_set *, crs);
1390 pre = atomic_fetchadd_64(&rs->rs_flows_using, -1);
1396 if (rs->rs_flags & RS_IS_DEAD)
1397 rs_defer_destroy(rs);
1398 mtx_unlock(&rs_mtx);
1402 * XXX: If this connection is using ifnet TLS, should we
1403 * switch it to using an unlimited rate, or perhaps use
1404 * ktls_output_eagain() to reset the send tag to a plain
1407 in_pcbdetach_txrtlmt(tp->t_inpcb);
1410 #define ONE_POINT_TWO_MEG 150000 /* 1.2 megabits in bytes */
1411 #define ONE_HUNDRED_MBPS 12500000 /* 100Mbps in bytes per second */
1412 #define FIVE_HUNDRED_MBPS 62500000 /* 500Mbps in bytes per second */
1413 #define MAX_MSS_SENT 43 /* 43 mss = 43 x 1500 = 64,500 bytes */
1416 tcp_get_pacing_burst_size (uint64_t bw, uint32_t segsiz, int can_use_1mss,
1417 const struct tcp_hwrate_limit_table *te, int *err)
1420 * We use the google formula to calculate the
1425 * tso = min(bw/1000, 64k)
1427 * Note for these calculations we ignore the
1428 * packet overhead (enet hdr, ip hdr and tcp hdr).
1430 uint64_t lentim, res, bytes;
1431 uint32_t new_tso, min_tso_segs;
1434 if (bytes > (64 * 1000))
1437 new_tso = (bytes + segsiz - 1) / segsiz;
1438 if (can_use_1mss && (bw < ONE_POINT_TWO_MEG))
1442 if (new_tso < min_tso_segs)
1443 new_tso = min_tso_segs;
1444 if (new_tso > MAX_MSS_SENT)
1445 new_tso = MAX_MSS_SENT;
1448 * If we are not doing hardware pacing
1457 * For hardware pacing we look at the
1458 * rate you are sending at and compare
1459 * that to the rate you have in hardware.
1461 * If the hardware rate is slower than your
1462 * software rate then you are in error and
1463 * we will build a queue in our hardware whic
1464 * is probably not desired, in such a case
1465 * just return the non-hardware TSO size.
1467 * If the rate in hardware is faster (which
1468 * it should be) then look at how long it
1469 * takes to send one ethernet segment size at
1470 * your b/w and compare that to the time it
1471 * takes to send at the rate you had selected.
1473 * If your time is greater (which we hope it is)
1474 * we get the delta between the two, and then
1475 * divide that into your pacing time. This tells
1476 * us how many MSS you can send down at once (rounded up).
1478 * Note we also double this value if the b/w is over
1479 * 100Mbps. If its over 500meg we just set you to the
1480 * max (43 segments).
1482 if (te->rate > FIVE_HUNDRED_MBPS)
1483 return (segsiz * MAX_MSS_SENT);
1484 if (te->rate == bw) {
1485 /* We are pacing at exactly the hdwr rate */
1486 return (segsiz * MAX_MSS_SENT);
1488 lentim = ETHERNET_SEGMENT_SIZE * USECS_IN_SECOND;
1490 if (res > te->time_between) {
1491 uint32_t delta, segs;
1493 delta = res - te->time_between;
1494 segs = (res + delta - 1)/delta;
1495 if (te->rate > ONE_HUNDRED_MBPS)
1497 if (segs < min_tso_segs)
1498 segs = min_tso_segs;
1499 if (segs > MAX_MSS_SENT)
1500 segs = MAX_MSS_SENT;
1504 if (segs < new_tso) {
1512 * Your time is smaller which means
1513 * we will grow a queue on our
1514 * hardware. Send back the non-hardware
1523 static eventhandler_tag rl_ifnet_departs;
1524 static eventhandler_tag rl_ifnet_arrives;
1525 static eventhandler_tag rl_shutdown_start;
1528 tcp_rs_init(void *st __unused)
1530 CK_LIST_INIT(&int_rs);
1531 rs_number_alive = 0;
1533 mtx_init(&rs_mtx, "tcp_rs_mtx", "rsmtx", MTX_DEF);
1534 rl_ifnet_departs = EVENTHANDLER_REGISTER(ifnet_departure_event,
1535 tcp_rl_ifnet_departure,
1536 NULL, EVENTHANDLER_PRI_ANY);
1537 rl_ifnet_arrives = EVENTHANDLER_REGISTER(ifnet_link_event,
1539 NULL, EVENTHANDLER_PRI_ANY);
1540 rl_shutdown_start = EVENTHANDLER_REGISTER(shutdown_pre_sync,
1541 tcp_rl_shutdown, NULL,
1542 SHUTDOWN_PRI_FIRST);
1543 printf("TCP_ratelimit: Is now initialized\n");
1546 SYSINIT(tcp_rl_init, SI_SUB_SMP + 1, SI_ORDER_ANY, tcp_rs_init, NULL);