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_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 <net/if_private.h>
53 #include <netinet/in.h>
54 #include <netinet/in_pcb.h>
55 #define TCPSTATES /* for logging */
56 #include <netinet/tcp_var.h>
57 #include <netinet/tcp_hpts.h>
58 #include <netinet/tcp_log_buf.h>
59 #include <netinet/tcp_ratelimit.h>
60 #ifndef USECS_IN_SECOND
61 #define USECS_IN_SECOND 1000000
64 * For the purposes of each send, what is the size
65 * of an ethernet frame.
67 MALLOC_DEFINE(M_TCPPACE, "tcp_hwpace", "TCP Hardware pacing memory");
71 * The following preferred table will seem weird to
72 * the casual viewer. Why do we not have any rates below
73 * 1Mbps? Why do we have a rate at 1.44Mbps called common?
74 * Why do the rates cluster in the 1-100Mbps range more
75 * than others? Why does the table jump around at the beginnign
76 * and then be more consistently raising?
78 * Let me try to answer those questions. A lot of
79 * this is dependant on the hardware. We have three basic
80 * supporters of rate limiting
82 * Chelsio - Supporting 16 configurable rates.
83 * Mlx - c4 supporting 13 fixed rates.
84 * Mlx - c5 & c6 supporting 127 configurable rates.
86 * The c4 is why we have a common rate that is available
87 * in all rate tables. This is a selected rate from the
88 * c4 table and we assure its available in all ratelimit
89 * tables. This way the tcp_ratelimit code has an assured
90 * rate it should always be able to get. This answers a
91 * couple of the questions above.
93 * So what about the rest, well the table is built to
94 * try to get the most out of a joint hardware/software
95 * pacing system. The software pacer will always pick
96 * a rate higher than the b/w that it is estimating
98 * on the path. This is done for two reasons.
99 * a) So we can discover more b/w
101 * b) So we can send a block of MSS's down and then
102 * have the software timer go off after the previous
103 * send is completely out of the hardware.
105 * But when we do <b> we don't want to have the delay
106 * between the last packet sent by the hardware be
107 * excessively long (to reach our desired rate).
109 * So let me give an example for clarity.
111 * Lets assume that the tcp stack sees that 29,110,000 bps is
112 * what the bw of the path is. The stack would select the
113 * rate 31Mbps. 31Mbps means that each send that is done
114 * by the hardware will cause a 390 micro-second gap between
115 * the packets sent at that rate. For 29,110,000 bps we
116 * would need 416 micro-seconds gap between each send.
118 * Note that are calculating a complete time for pacing
119 * which includes the ethernet, IP and TCP overhead. So
120 * a full 1514 bytes is used for the above calculations.
121 * My testing has shown that both cards are also using this
122 * as their basis i.e. full payload size of the ethernet frame.
123 * The TCP stack caller needs to be aware of this and make the
124 * appropriate overhead calculations be included in its choices.
126 * Now, continuing our example, we pick a MSS size based on the
127 * delta between the two rates (416 - 390) divided into the rate
128 * we really wish to send at rounded up. That results in a MSS
129 * send of 17 mss's at once. The hardware then will
130 * run out of data in a single 17MSS send in 6,630 micro-seconds.
132 * On the other hand the software pacer will send more data
133 * in 7,072 micro-seconds. This means that we will refill
134 * the hardware 52 microseconds after it would have sent
135 * next if it had not ran out of data. This is a win since we are
136 * only sending every 7ms or so and yet all the packets are spaced on
137 * the wire with 94% of what they should be and only
138 * the last packet is delayed extra to make up for the
141 * Note that the above formula has two important caveat.
142 * If we are above (b/w wise) over 100Mbps we double the result
143 * of the MSS calculation. The second caveat is if we are 500Mbps
144 * or more we just send the maximum MSS at once i.e. 45MSS. At
145 * the higher b/w's even the cards have limits to what times (timer granularity)
146 * they can insert between packets and start to send more than one
147 * packet at a time on the wire.
150 #define COMMON_RATE 180500
151 const uint64_t desired_rates[] = {
152 122500, /* 1Mbps - rate 1 */
153 180500, /* 1.44Mpbs - rate 2 common rate */
154 375000, /* 3Mbps - rate 3 */
155 625000, /* 5Mbps - rate 4 */
156 1250000, /* 10Mbps - rate 5 */
157 1875000, /* 15Mbps - rate 6 */
158 2500000, /* 20Mbps - rate 7 */
159 3125000, /* 25Mbps - rate 8 */
160 3750000, /* 30Mbps - rate 9 */
161 4375000, /* 35Mbps - rate 10 */
162 5000000, /* 40Meg - rate 11 */
163 6250000, /* 50Mbps - rate 12 */
164 12500000, /* 100Mbps - rate 13 */
165 25000000, /* 200Mbps - rate 14 */
166 50000000, /* 400Mbps - rate 15 */
167 100000000, /* 800Mbps - rate 16 */
168 5625000, /* 45Mbps - rate 17 */
169 6875000, /* 55Mbps - rate 19 */
170 7500000, /* 60Mbps - rate 20 */
171 8125000, /* 65Mbps - rate 21 */
172 8750000, /* 70Mbps - rate 22 */
173 9375000, /* 75Mbps - rate 23 */
174 10000000, /* 80Mbps - rate 24 */
175 10625000, /* 85Mbps - rate 25 */
176 11250000, /* 90Mbps - rate 26 */
177 11875000, /* 95Mbps - rate 27 */
178 12500000, /* 100Mbps - rate 28 */
179 13750000, /* 110Mbps - rate 29 */
180 15000000, /* 120Mbps - rate 30 */
181 16250000, /* 130Mbps - rate 31 */
182 17500000, /* 140Mbps - rate 32 */
183 18750000, /* 150Mbps - rate 33 */
184 20000000, /* 160Mbps - rate 34 */
185 21250000, /* 170Mbps - rate 35 */
186 22500000, /* 180Mbps - rate 36 */
187 23750000, /* 190Mbps - rate 37 */
188 26250000, /* 210Mbps - rate 38 */
189 27500000, /* 220Mbps - rate 39 */
190 28750000, /* 230Mbps - rate 40 */
191 30000000, /* 240Mbps - rate 41 */
192 31250000, /* 250Mbps - rate 42 */
193 34375000, /* 275Mbps - rate 43 */
194 37500000, /* 300Mbps - rate 44 */
195 40625000, /* 325Mbps - rate 45 */
196 43750000, /* 350Mbps - rate 46 */
197 46875000, /* 375Mbps - rate 47 */
198 53125000, /* 425Mbps - rate 48 */
199 56250000, /* 450Mbps - rate 49 */
200 59375000, /* 475Mbps - rate 50 */
201 62500000, /* 500Mbps - rate 51 */
202 68750000, /* 550Mbps - rate 52 */
203 75000000, /* 600Mbps - rate 53 */
204 81250000, /* 650Mbps - rate 54 */
205 87500000, /* 700Mbps - rate 55 */
206 93750000, /* 750Mbps - rate 56 */
207 106250000, /* 850Mbps - rate 57 */
208 112500000, /* 900Mbps - rate 58 */
209 125000000, /* 1Gbps - rate 59 */
210 156250000, /* 1.25Gps - rate 60 */
211 187500000, /* 1.5Gps - rate 61 */
212 218750000, /* 1.75Gps - rate 62 */
213 250000000, /* 2Gbps - rate 63 */
214 281250000, /* 2.25Gps - rate 64 */
215 312500000, /* 2.5Gbps - rate 65 */
216 343750000, /* 2.75Gbps - rate 66 */
217 375000000, /* 3Gbps - rate 67 */
218 500000000, /* 4Gbps - rate 68 */
219 625000000, /* 5Gbps - rate 69 */
220 750000000, /* 6Gbps - rate 70 */
221 875000000, /* 7Gbps - rate 71 */
222 1000000000, /* 8Gbps - rate 72 */
223 1125000000, /* 9Gbps - rate 73 */
224 1250000000, /* 10Gbps - rate 74 */
225 1875000000, /* 15Gbps - rate 75 */
226 2500000000 /* 20Gbps - rate 76 */
229 #define MAX_HDWR_RATES (sizeof(desired_rates)/sizeof(uint64_t))
230 #define RS_ORDERED_COUNT 16 /*
231 * Number that are in order
232 * at the beginning of the table,
233 * over this a sort is required.
235 #define RS_NEXT_ORDER_GROUP 16 /*
236 * The point in our table where
237 * we come fill in a second ordered
238 * group (index wise means -1).
240 #define ALL_HARDWARE_RATES 1004 /*
241 * 1Meg - 1Gig in 1 Meg steps
242 * plus 100, 200k and 500k and
246 #define RS_ONE_MEGABIT_PERSEC 1000000
247 #define RS_ONE_GIGABIT_PERSEC 1000000000
248 #define RS_TEN_GIGABIT_PERSEC 10000000000
250 static struct head_tcp_rate_set int_rs;
251 static struct mtx rs_mtx;
252 uint32_t rs_number_alive;
253 uint32_t rs_number_dead;
254 static uint32_t rs_floor_mss = 0;
255 static uint32_t wait_time_floor = 8000; /* 8 ms */
256 static uint32_t rs_hw_floor_mss = 16;
257 static uint32_t num_of_waits_allowed = 1; /* How many time blocks are we willing to wait */
259 static uint32_t mss_divisor = RL_DEFAULT_DIVISOR;
260 static uint32_t even_num_segs = 1;
261 static uint32_t even_threshold = 4;
263 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, rl, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
264 "TCP Ratelimit stats");
265 SYSCTL_UINT(_net_inet_tcp_rl, OID_AUTO, alive, CTLFLAG_RW,
267 "Number of interfaces initialized for ratelimiting");
268 SYSCTL_UINT(_net_inet_tcp_rl, OID_AUTO, dead, CTLFLAG_RW,
270 "Number of interfaces departing from ratelimiting");
271 SYSCTL_UINT(_net_inet_tcp_rl, OID_AUTO, floor_mss, CTLFLAG_RW,
273 "Number of MSS that will override the normal minimums (0 means don't enforce)");
274 SYSCTL_UINT(_net_inet_tcp_rl, OID_AUTO, wait_floor, CTLFLAG_RW,
275 &wait_time_floor, 2000,
276 "Has b/w increases what is the wait floor we are willing to wait at the end?");
277 SYSCTL_UINT(_net_inet_tcp_rl, OID_AUTO, time_blocks, CTLFLAG_RW,
278 &num_of_waits_allowed, 1,
279 "How many time blocks on the end should software pacing be willing to wait?");
281 SYSCTL_UINT(_net_inet_tcp_rl, OID_AUTO, hw_floor_mss, CTLFLAG_RW,
282 &rs_hw_floor_mss, 16,
283 "Number of mss that are a minum for hardware pacing?");
285 SYSCTL_INT(_net_inet_tcp_rl, OID_AUTO, divisor, CTLFLAG_RW,
286 &mss_divisor, RL_DEFAULT_DIVISOR,
287 "The value divided into bytes per second to help establish mss size");
288 SYSCTL_INT(_net_inet_tcp_rl, OID_AUTO, even, CTLFLAG_RW,
290 "Do we round mss size up to an even number of segments for delayed ack");
291 SYSCTL_INT(_net_inet_tcp_rl, OID_AUTO, eventhresh, CTLFLAG_RW,
293 "At what number of mss do we start rounding up to an even number of mss?");
296 rl_add_syctl_entries(struct sysctl_oid *rl_sysctl_root, struct tcp_rate_set *rs)
299 * Add sysctl entries for thus interface.
301 if (rs->rs_flags & RS_INTF_NO_SUP) {
302 SYSCTL_ADD_S32(&rs->sysctl_ctx,
303 SYSCTL_CHILDREN(rl_sysctl_root),
304 OID_AUTO, "disable", CTLFLAG_RD,
306 "Disable this interface from new hdwr limiting?");
308 SYSCTL_ADD_S32(&rs->sysctl_ctx,
309 SYSCTL_CHILDREN(rl_sysctl_root),
310 OID_AUTO, "disable", CTLFLAG_RW,
312 "Disable this interface from new hdwr limiting?");
314 SYSCTL_ADD_S32(&rs->sysctl_ctx,
315 SYSCTL_CHILDREN(rl_sysctl_root),
316 OID_AUTO, "minseg", CTLFLAG_RW,
318 "What is the minimum we need to send on this interface?");
319 SYSCTL_ADD_U64(&rs->sysctl_ctx,
320 SYSCTL_CHILDREN(rl_sysctl_root),
321 OID_AUTO, "flow_limit", CTLFLAG_RW,
322 &rs->rs_flow_limit, 0,
323 "What is the limit for number of flows (0=unlimited)?");
324 SYSCTL_ADD_S32(&rs->sysctl_ctx,
325 SYSCTL_CHILDREN(rl_sysctl_root),
326 OID_AUTO, "highest", CTLFLAG_RD,
327 &rs->rs_highest_valid, 0,
328 "Highest valid rate");
329 SYSCTL_ADD_S32(&rs->sysctl_ctx,
330 SYSCTL_CHILDREN(rl_sysctl_root),
331 OID_AUTO, "lowest", CTLFLAG_RD,
332 &rs->rs_lowest_valid, 0,
333 "Lowest valid rate");
334 SYSCTL_ADD_S32(&rs->sysctl_ctx,
335 SYSCTL_CHILDREN(rl_sysctl_root),
336 OID_AUTO, "flags", CTLFLAG_RD,
338 "What lags are on the entry?");
339 SYSCTL_ADD_S32(&rs->sysctl_ctx,
340 SYSCTL_CHILDREN(rl_sysctl_root),
341 OID_AUTO, "numrates", CTLFLAG_RD,
343 "How many rates re there?");
344 SYSCTL_ADD_U64(&rs->sysctl_ctx,
345 SYSCTL_CHILDREN(rl_sysctl_root),
346 OID_AUTO, "flows_using", CTLFLAG_RD,
347 &rs->rs_flows_using, 0,
348 "How many flows are using this interface now?");
349 #ifdef DETAILED_RATELIMIT_SYSCTL
350 if (rs->rs_rlt && rs->rs_rate_cnt > 0) {
351 /* Lets display the rates */
353 struct sysctl_oid *rl_rates;
354 struct sysctl_oid *rl_rate_num;
356 rl_rates = SYSCTL_ADD_NODE(&rs->sysctl_ctx,
357 SYSCTL_CHILDREN(rl_sysctl_root),
360 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
362 for( i = 0; i < rs->rs_rate_cnt; i++) {
363 sprintf(rate_num, "%d", i);
364 rl_rate_num = SYSCTL_ADD_NODE(&rs->sysctl_ctx,
365 SYSCTL_CHILDREN(rl_rates),
368 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
370 SYSCTL_ADD_U32(&rs->sysctl_ctx,
371 SYSCTL_CHILDREN(rl_rate_num),
372 OID_AUTO, "flags", CTLFLAG_RD,
373 &rs->rs_rlt[i].flags, 0,
374 "Flags on this rate");
375 SYSCTL_ADD_U32(&rs->sysctl_ctx,
376 SYSCTL_CHILDREN(rl_rate_num),
377 OID_AUTO, "pacetime", CTLFLAG_RD,
378 &rs->rs_rlt[i].time_between, 0,
379 "Time hardware inserts between 1500 byte sends");
380 SYSCTL_ADD_LONG(&rs->sysctl_ctx,
381 SYSCTL_CHILDREN(rl_rate_num),
382 OID_AUTO, "rate", CTLFLAG_RD,
384 "Rate in bytes per second");
385 SYSCTL_ADD_LONG(&rs->sysctl_ctx,
386 SYSCTL_CHILDREN(rl_rate_num),
387 OID_AUTO, "using", CTLFLAG_RD,
388 &rs->rs_rlt[i].using,
389 "Number of flows using");
390 SYSCTL_ADD_LONG(&rs->sysctl_ctx,
391 SYSCTL_CHILDREN(rl_rate_num),
392 OID_AUTO, "enobufs", CTLFLAG_RD,
393 &rs->rs_rlt[i].rs_num_enobufs,
394 "Number of enobufs logged on this rate");
402 rs_destroy(epoch_context_t ctx)
404 struct tcp_rate_set *rs;
407 rs = __containerof(ctx, struct tcp_rate_set, rs_epoch_ctx);
410 rs->rs_flags &= ~RS_FUNERAL_SCHD;
412 * In theory its possible (but unlikely)
413 * that while the delete was occuring
414 * and we were applying the DEAD flag
415 * someone slipped in and found the
416 * interface in a lookup. While we
417 * decided rs_flows_using were 0 and
418 * scheduling the epoch_call, the other
419 * thread incremented rs_flow_using. This
420 * is because users have a pointer and
421 * we only use the rs_flows_using in an
422 * atomic fashion, i.e. the other entities
423 * are not protected. To assure this did
424 * not occur, we check rs_flows_using here
427 do_free_rs = (rs->rs_flows_using == 0);
432 sysctl_ctx_free(&rs->sysctl_ctx);
433 free(rs->rs_rlt, M_TCPPACE);
439 rs_defer_destroy(struct tcp_rate_set *rs)
442 mtx_assert(&rs_mtx, MA_OWNED);
444 /* Check if already pending. */
445 if (rs->rs_flags & RS_FUNERAL_SCHD)
450 /* Set flag to only defer once. */
451 rs->rs_flags |= RS_FUNERAL_SCHD;
452 NET_EPOCH_CALL(rs_destroy, &rs->rs_epoch_ctx);
456 extern counter_u64_t rate_limit_new;
457 extern counter_u64_t rate_limit_chg;
458 extern counter_u64_t rate_limit_set_ok;
459 extern counter_u64_t rate_limit_active;
460 extern counter_u64_t rate_limit_alloc_fail;
464 rl_attach_txrtlmt(struct ifnet *ifp,
468 struct m_snd_tag **tag)
471 union if_snd_tag_alloc_params params = {
472 .rate_limit.hdr.type = IF_SND_TAG_TYPE_RATE_LIMIT,
473 .rate_limit.hdr.flowid = flowid,
474 .rate_limit.hdr.flowtype = flowtype,
475 .rate_limit.max_rate = cfg_rate,
476 .rate_limit.flags = M_NOWAIT,
479 error = m_snd_tag_alloc(ifp, ¶ms, tag);
482 counter_u64_add(rate_limit_set_ok, 1);
483 counter_u64_add(rate_limit_active, 1);
484 } else if (error != EOPNOTSUPP)
485 counter_u64_add(rate_limit_alloc_fail, 1);
491 populate_canned_table(struct tcp_rate_set *rs, const uint64_t *rate_table_act)
494 * The internal table is "special", it
495 * is two seperate ordered tables that
496 * must be merged. We get here when the
497 * adapter specifies a number of rates that
498 * covers both ranges in the table in some
501 int i, at_low, at_high;
502 uint8_t low_disabled = 0, high_disabled = 0;
504 for(i = 0, at_low = 0, at_high = RS_NEXT_ORDER_GROUP; i < rs->rs_rate_cnt; i++) {
505 rs->rs_rlt[i].flags = 0;
506 rs->rs_rlt[i].time_between = 0;
507 if ((low_disabled == 0) &&
509 (rate_table_act[at_low] < rate_table_act[at_high]))) {
510 rs->rs_rlt[i].rate = rate_table_act[at_low];
512 if (at_low == RS_NEXT_ORDER_GROUP)
514 } else if (high_disabled == 0) {
515 rs->rs_rlt[i].rate = rate_table_act[at_high];
517 if (at_high == MAX_HDWR_RATES)
523 static struct tcp_rate_set *
524 rt_setup_new_rs(struct ifnet *ifp, int *error)
526 struct tcp_rate_set *rs;
527 const uint64_t *rate_table_act;
528 uint64_t lentim, res;
532 struct if_ratelimit_query_results rl;
533 struct sysctl_oid *rl_sysctl_root;
534 struct epoch_tracker et;
536 * We expect to enter with the
540 if (ifp->if_ratelimit_query == NULL) {
542 * We can do nothing if we cannot
543 * get a query back from the driver.
545 printf("Warning:No query functions for %s:%d-- failed\n",
546 ifp->if_dname, ifp->if_dunit);
549 rs = malloc(sizeof(struct tcp_rate_set), M_TCPPACE, M_NOWAIT | M_ZERO);
553 printf("Warning:No memory for malloc of tcp_rate_set\n");
556 memset(&rl, 0, sizeof(rl));
557 rl.flags = RT_NOSUPPORT;
558 ifp->if_ratelimit_query(ifp, &rl);
559 if (rl.flags & RT_IS_UNUSABLE) {
561 * The interface does not really support
564 memset(rs, 0, sizeof(struct tcp_rate_set));
566 rs->rs_if_dunit = ifp->if_dunit;
567 rs->rs_flags = RS_INTF_NO_SUP;
570 sysctl_ctx_init(&rs->sysctl_ctx);
571 rl_sysctl_root = SYSCTL_ADD_NODE(&rs->sysctl_ctx,
572 SYSCTL_STATIC_CHILDREN(_net_inet_tcp_rl),
574 rs->rs_ifp->if_xname,
575 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
577 rl_add_syctl_entries(rl_sysctl_root, rs);
580 CK_LIST_INSERT_HEAD(&int_rs, rs, next);
584 } else if ((rl.flags & RT_IS_INDIRECT) == RT_IS_INDIRECT) {
585 memset(rs, 0, sizeof(struct tcp_rate_set));
587 rs->rs_if_dunit = ifp->if_dunit;
588 rs->rs_flags = RS_IS_DEFF;
590 sysctl_ctx_init(&rs->sysctl_ctx);
591 rl_sysctl_root = SYSCTL_ADD_NODE(&rs->sysctl_ctx,
592 SYSCTL_STATIC_CHILDREN(_net_inet_tcp_rl),
594 rs->rs_ifp->if_xname,
595 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
597 rl_add_syctl_entries(rl_sysctl_root, rs);
600 CK_LIST_INSERT_HEAD(&int_rs, rs, next);
604 } else if ((rl.flags & RT_IS_FIXED_TABLE) == RT_IS_FIXED_TABLE) {
605 /* Mellanox C4 likely */
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;
610 rs->rs_highest_valid = 0;
611 rs->rs_flow_limit = rl.max_flows;
612 rs->rs_flags = RS_IS_INTF | RS_NO_PRE;
614 rate_table_act = rl.rate_table;
615 } else if ((rl.flags & RT_IS_SELECTABLE) == RT_IS_SELECTABLE) {
616 /* Chelsio, C5 and C6 of Mellanox? */
618 rs->rs_if_dunit = ifp->if_dunit;
619 rs->rs_rate_cnt = rl.number_of_rates;
620 rs->rs_min_seg = rl.min_segment_burst;
622 rs->rs_flow_limit = rl.max_flows;
623 rate_table_act = desired_rates;
624 if ((rs->rs_rate_cnt > MAX_HDWR_RATES) &&
625 (rs->rs_rate_cnt < ALL_HARDWARE_RATES)) {
627 * Our desired table is not big
628 * enough, do what we can.
630 rs->rs_rate_cnt = MAX_HDWR_RATES;
632 if (rs->rs_rate_cnt <= RS_ORDERED_COUNT)
633 rs->rs_flags = RS_IS_INTF;
635 rs->rs_flags = RS_IS_INTF | RS_INT_TBL;
636 if (rs->rs_rate_cnt >= ALL_HARDWARE_RATES)
637 rs->rs_rate_cnt = ALL_HARDWARE_RATES;
642 sz = sizeof(struct tcp_hwrate_limit_table) * rs->rs_rate_cnt;
643 rs->rs_rlt = malloc(sz, M_TCPPACE, M_NOWAIT);
644 if (rs->rs_rlt == NULL) {
651 if (rs->rs_rate_cnt >= ALL_HARDWARE_RATES) {
653 * The interface supports all
654 * the rates we could possibly want.
658 rs->rs_rlt[0].rate = 12500; /* 100k */
659 rs->rs_rlt[1].rate = 25000; /* 200k */
660 rs->rs_rlt[2].rate = 62500; /* 500k */
661 /* Note 125000 == 1Megabit
662 * populate 1Meg - 1000meg.
664 for(i = 3, rat = 125000; i< (ALL_HARDWARE_RATES-1); i++) {
665 rs->rs_rlt[i].rate = rat;
668 rs->rs_rlt[(ALL_HARDWARE_RATES-1)].rate = 1250000000;
669 } else if (rs->rs_flags & RS_INT_TBL) {
670 /* We populate this in a special way */
671 populate_canned_table(rs, rate_table_act);
674 * Just copy in the rates from
675 * the table, it is in order.
677 for (i=0; i<rs->rs_rate_cnt; i++) {
678 rs->rs_rlt[i].rate = rate_table_act[i];
679 rs->rs_rlt[i].time_between = 0;
680 rs->rs_rlt[i].flags = 0;
683 for (i = (rs->rs_rate_cnt - 1); i >= 0; i--) {
685 * We go backwards through the list so that if we can't get
686 * a rate and fail to init one, we have at least a chance of
687 * getting the highest one.
689 rs->rs_rlt[i].ptbl = rs;
690 rs->rs_rlt[i].tag = NULL;
691 rs->rs_rlt[i].using = 0;
692 rs->rs_rlt[i].rs_num_enobufs = 0;
694 * Calculate the time between.
696 lentim = ETHERNET_SEGMENT_SIZE * USECS_IN_SECOND;
697 res = lentim / rs->rs_rlt[i].rate;
699 rs->rs_rlt[i].time_between = res;
701 rs->rs_rlt[i].time_between = 1;
702 if (rs->rs_flags & RS_NO_PRE) {
703 rs->rs_rlt[i].flags = HDWRPACE_INITED;
704 rs->rs_lowest_valid = i;
708 if ((rl.flags & RT_IS_SETUP_REQ) &&
709 (ifp->if_ratelimit_query)) {
710 err = ifp->if_ratelimit_setup(ifp,
711 rs->rs_rlt[i].rate, i);
716 hash_type = M_HASHTYPE_RSS_TCP_IPV4;
718 hash_type = M_HASHTYPE_OPAQUE_HASH;
720 err = rl_attach_txrtlmt(ifp,
727 if (i == (rs->rs_rate_cnt - 1)) {
729 * Huh - first rate and we can't get
732 free(rs->rs_rlt, M_TCPPACE);
742 rs->rs_rlt[i].flags = HDWRPACE_INITED | HDWRPACE_TAGPRESENT;
743 rs->rs_lowest_valid = i;
747 /* Did we get at least 1 rate? */
748 if (rs->rs_rlt[(rs->rs_rate_cnt - 1)].flags & HDWRPACE_INITED)
749 rs->rs_highest_valid = rs->rs_rate_cnt - 1;
751 free(rs->rs_rlt, M_TCPPACE);
755 sysctl_ctx_init(&rs->sysctl_ctx);
756 rl_sysctl_root = SYSCTL_ADD_NODE(&rs->sysctl_ctx,
757 SYSCTL_STATIC_CHILDREN(_net_inet_tcp_rl),
759 rs->rs_ifp->if_xname,
760 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
762 rl_add_syctl_entries(rl_sysctl_root, rs);
765 CK_LIST_INSERT_HEAD(&int_rs, rs, next);
772 * For an explanation of why the argument is volatile please
773 * look at the comments around rt_setup_rate().
775 static const struct tcp_hwrate_limit_table *
776 tcp_int_find_suitable_rate(const volatile struct tcp_rate_set *rs,
777 uint64_t bytes_per_sec, uint32_t flags, uint64_t *lower_rate)
779 struct tcp_hwrate_limit_table *arte = NULL, *rte = NULL;
780 uint64_t mbits_per_sec, ind_calc, previous_rate = 0;
783 mbits_per_sec = (bytes_per_sec * 8);
784 if (flags & RS_PACING_LT) {
785 if ((mbits_per_sec < RS_ONE_MEGABIT_PERSEC) &&
786 (rs->rs_lowest_valid <= 2)){
788 * Smaller than 1Meg, only
789 * 3 entries can match it.
792 for(i = rs->rs_lowest_valid; i < 3; i++) {
793 if (bytes_per_sec <= rs->rs_rlt[i].rate) {
794 rte = &rs->rs_rlt[i];
796 } else if (rs->rs_rlt[i].flags & HDWRPACE_INITED) {
797 arte = &rs->rs_rlt[i];
799 previous_rate = rs->rs_rlt[i].rate;
802 } else if ((mbits_per_sec > RS_ONE_GIGABIT_PERSEC) &&
803 (rs->rs_rlt[(ALL_HARDWARE_RATES-1)].flags & HDWRPACE_INITED)){
805 * Larger than 1G (the majority of
808 if (mbits_per_sec < RS_TEN_GIGABIT_PERSEC)
809 rte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
811 arte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
812 previous_rate = rs->rs_rlt[(ALL_HARDWARE_RATES-2)].rate;
816 * If we reach here its in our table (between 1Meg - 1000Meg),
817 * just take the rounded down mbits per second, and add
818 * 1Megabit to it, from this we can calculate
819 * the index in the table.
821 ind_calc = mbits_per_sec/RS_ONE_MEGABIT_PERSEC;
822 if ((ind_calc * RS_ONE_MEGABIT_PERSEC) != mbits_per_sec)
824 /* our table is offset by 3, we add 2 */
826 if (ind_calc > (ALL_HARDWARE_RATES-1)) {
827 /* This should not happen */
828 ind_calc = ALL_HARDWARE_RATES-1;
830 if ((ind_calc >= rs->rs_lowest_valid) &&
831 (ind_calc <= rs->rs_highest_valid)) {
832 rte = &rs->rs_rlt[ind_calc];
834 previous_rate = rs->rs_rlt[(ind_calc-1)].rate;
836 } else if (flags & RS_PACING_EXACT_MATCH) {
837 if ((mbits_per_sec < RS_ONE_MEGABIT_PERSEC) &&
838 (rs->rs_lowest_valid <= 2)){
839 for(i = rs->rs_lowest_valid; i < 3; i++) {
840 if (bytes_per_sec == rs->rs_rlt[i].rate) {
841 rte = &rs->rs_rlt[i];
845 } else if ((mbits_per_sec > RS_ONE_GIGABIT_PERSEC) &&
846 (rs->rs_rlt[(ALL_HARDWARE_RATES-1)].flags & HDWRPACE_INITED)) {
847 /* > 1Gbps only one rate */
848 if (bytes_per_sec == rs->rs_rlt[(ALL_HARDWARE_RATES-1)].rate) {
850 rte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
853 /* Ok it must be a exact meg (its between 1G and 1Meg) */
854 ind_calc = mbits_per_sec/RS_ONE_MEGABIT_PERSEC;
855 if ((ind_calc * RS_ONE_MEGABIT_PERSEC) == mbits_per_sec) {
856 /* its an exact Mbps */
858 if (ind_calc > (ALL_HARDWARE_RATES-1)) {
859 /* This should not happen */
860 ind_calc = ALL_HARDWARE_RATES-1;
862 if (rs->rs_rlt[ind_calc].flags & HDWRPACE_INITED)
863 rte = &rs->rs_rlt[ind_calc];
867 /* we want greater than the requested rate */
868 if ((mbits_per_sec < RS_ONE_MEGABIT_PERSEC) &&
869 (rs->rs_lowest_valid <= 2)){
870 arte = &rs->rs_rlt[3]; /* set alternate to 1Meg */
871 for (i=2; i>=rs->rs_lowest_valid; i--) {
872 if (bytes_per_sec < rs->rs_rlt[i].rate) {
873 rte = &rs->rs_rlt[i];
875 previous_rate = rs->rs_rlt[(i-1)].rate;
878 } else if ((flags & RS_PACING_GEQ) &&
879 (bytes_per_sec == rs->rs_rlt[i].rate)) {
880 rte = &rs->rs_rlt[i];
882 previous_rate = rs->rs_rlt[(i-1)].rate;
886 arte = &rs->rs_rlt[i]; /* new alternate */
889 } else if (mbits_per_sec > RS_ONE_GIGABIT_PERSEC) {
890 if ((bytes_per_sec < rs->rs_rlt[(ALL_HARDWARE_RATES-1)].rate) &&
891 (rs->rs_rlt[(ALL_HARDWARE_RATES-1)].flags & HDWRPACE_INITED)){
892 /* Our top rate is larger than the request */
893 rte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
894 } else if ((flags & RS_PACING_GEQ) &&
895 (bytes_per_sec == rs->rs_rlt[(ALL_HARDWARE_RATES-1)].rate) &&
896 (rs->rs_rlt[(ALL_HARDWARE_RATES-1)].flags & HDWRPACE_INITED)) {
897 /* It matches our top rate */
898 rte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
899 } else if (rs->rs_rlt[(ALL_HARDWARE_RATES-1)].flags & HDWRPACE_INITED) {
900 /* The top rate is an alternative */
901 arte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
903 previous_rate = rs->rs_rlt[(ALL_HARDWARE_RATES-2)].rate;
905 /* Its in our range 1Meg - 1Gig */
906 if (flags & RS_PACING_GEQ) {
907 ind_calc = mbits_per_sec/RS_ONE_MEGABIT_PERSEC;
908 if ((ind_calc * RS_ONE_MEGABIT_PERSEC) == mbits_per_sec) {
909 if (ind_calc > (ALL_HARDWARE_RATES-1)) {
910 /* This should not happen */
911 ind_calc = (ALL_HARDWARE_RATES-1);
913 rte = &rs->rs_rlt[ind_calc];
915 previous_rate = rs->rs_rlt[(ind_calc-1)].rate;
919 ind_calc = (mbits_per_sec + (RS_ONE_MEGABIT_PERSEC-1))/RS_ONE_MEGABIT_PERSEC;
921 if (ind_calc > (ALL_HARDWARE_RATES-1)) {
922 /* This should not happen */
923 ind_calc = ALL_HARDWARE_RATES-1;
925 if (rs->rs_rlt[ind_calc].flags & HDWRPACE_INITED) {
926 rte = &rs->rs_rlt[ind_calc];
928 previous_rate = rs->rs_rlt[(ind_calc-1)].rate;
935 (flags & RS_PACING_SUB_OK)) {
936 /* We can use the substitute */
940 *lower_rate = previous_rate;
945 * For an explanation of why the argument is volatile please
946 * look at the comments around rt_setup_rate().
948 static const struct tcp_hwrate_limit_table *
949 tcp_find_suitable_rate(const volatile struct tcp_rate_set *rs, uint64_t bytes_per_sec, uint32_t flags, uint64_t *lower_rate)
952 * Hunt the rate table with the restrictions in flags and find a
953 * suitable rate if possible.
954 * RS_PACING_EXACT_MATCH - look for an exact match to rate.
955 * RS_PACING_GT - must be greater than.
956 * RS_PACING_GEQ - must be greater than or equal.
957 * RS_PACING_LT - must be less than.
958 * RS_PACING_SUB_OK - If we don't meet criteria a
962 struct tcp_hwrate_limit_table *rte = NULL;
963 uint64_t previous_rate = 0;
965 if ((rs->rs_flags & RS_INT_TBL) &&
966 (rs->rs_rate_cnt >= ALL_HARDWARE_RATES)) {
968 * Here we don't want to paw thru
969 * a big table, we have everything
970 * from 1Meg - 1000Meg in 1Meg increments.
971 * Use an alternate method to "lookup".
973 return (tcp_int_find_suitable_rate(rs, bytes_per_sec, flags, lower_rate));
975 if ((flags & RS_PACING_LT) ||
976 (flags & RS_PACING_EXACT_MATCH)) {
978 * For exact and less than we go forward through the table.
979 * This way when we find one larger we stop (exact was a
982 for (i = rs->rs_lowest_valid, matched = 0; i <= rs->rs_highest_valid; i++) {
983 if ((flags & RS_PACING_EXACT_MATCH) &&
984 (bytes_per_sec == rs->rs_rlt[i].rate)) {
985 rte = &rs->rs_rlt[i];
987 if (lower_rate != NULL)
988 *lower_rate = previous_rate;
990 } else if ((flags & RS_PACING_LT) &&
991 (bytes_per_sec <= rs->rs_rlt[i].rate)) {
992 rte = &rs->rs_rlt[i];
994 if (lower_rate != NULL)
995 *lower_rate = previous_rate;
998 previous_rate = rs->rs_rlt[i].rate;
999 if (bytes_per_sec > rs->rs_rlt[i].rate)
1002 if ((matched == 0) &&
1003 (flags & RS_PACING_LT) &&
1004 (flags & RS_PACING_SUB_OK)) {
1005 /* Kick in a substitute (the lowest) */
1006 rte = &rs->rs_rlt[rs->rs_lowest_valid];
1010 * Here we go backward through the table so that we can find
1011 * the one greater in theory faster (but its probably a
1014 for (i = rs->rs_highest_valid, matched = 0; i >= rs->rs_lowest_valid; i--) {
1015 if (rs->rs_rlt[i].rate > bytes_per_sec) {
1016 /* A possible candidate */
1017 rte = &rs->rs_rlt[i];
1019 if ((flags & RS_PACING_GEQ) &&
1020 (bytes_per_sec == rs->rs_rlt[i].rate)) {
1021 /* An exact match and we want equal */
1023 rte = &rs->rs_rlt[i];
1027 * Found one that is larger than but don't
1028 * stop, there may be a more closer match.
1032 if (rs->rs_rlt[i].rate < bytes_per_sec) {
1034 * We found a table entry that is smaller,
1035 * stop there will be none greater or equal.
1037 if (lower_rate != NULL)
1038 *lower_rate = rs->rs_rlt[i].rate;
1042 if ((matched == 0) &&
1043 (flags & RS_PACING_SUB_OK)) {
1044 /* Kick in a substitute (the highest) */
1045 rte = &rs->rs_rlt[rs->rs_highest_valid];
1051 static struct ifnet *
1052 rt_find_real_interface(struct ifnet *ifp, struct inpcb *inp, int *error)
1055 struct m_snd_tag *tag, *ntag;
1056 union if_snd_tag_alloc_params params = {
1057 .rate_limit.hdr.type = IF_SND_TAG_TYPE_RATE_LIMIT,
1058 .rate_limit.hdr.flowid = inp->inp_flowid,
1059 .rate_limit.hdr.numa_domain = inp->inp_numa_domain,
1060 .rate_limit.max_rate = COMMON_RATE,
1061 .rate_limit.flags = M_NOWAIT,
1065 params.rate_limit.hdr.flowtype = ((inp->inp_vflag & INP_IPV6) ?
1066 M_HASHTYPE_RSS_TCP_IPV6 : M_HASHTYPE_RSS_TCP_IPV4);
1068 params.rate_limit.hdr.flowtype = M_HASHTYPE_OPAQUE_HASH;
1070 err = m_snd_tag_alloc(ifp, ¶ms, &tag);
1072 /* Failed to setup a tag? */
1078 while (ntag->sw->next_snd_tag != NULL) {
1079 ntag = ntag->sw->next_snd_tag(ntag);
1082 m_snd_tag_rele(tag);
1087 rl_increment_using(const struct tcp_hwrate_limit_table *rte)
1089 struct tcp_hwrate_limit_table *decon_rte;
1091 decon_rte = __DECONST(struct tcp_hwrate_limit_table *, rte);
1092 atomic_add_long(&decon_rte->using, 1);
1096 rl_decrement_using(const struct tcp_hwrate_limit_table *rte)
1098 struct tcp_hwrate_limit_table *decon_rte;
1100 decon_rte = __DECONST(struct tcp_hwrate_limit_table *, rte);
1101 atomic_subtract_long(&decon_rte->using, 1);
1105 tcp_rl_log_enobuf(const struct tcp_hwrate_limit_table *rte)
1107 struct tcp_hwrate_limit_table *decon_rte;
1109 decon_rte = __DECONST(struct tcp_hwrate_limit_table *, rte);
1110 atomic_add_long(&decon_rte->rs_num_enobufs, 1);
1114 * Do NOT take the __noinline out of the
1115 * find_rs_for_ifp() function. If you do the inline
1116 * of it for the rt_setup_rate() will show you a
1117 * compiler bug. For some reason the compiler thinks
1118 * the list can never be empty. The consequence of
1119 * this will be a crash when we dereference NULL
1120 * if an ifp is removed just has a hw rate limit
1121 * is attempted. If you are working on the compiler
1122 * and want to "test" this go ahead and take the noinline
1123 * out otherwise let sleeping dogs ly until such time
1124 * as we get a compiler fix 10/2/20 -- RRS
1126 static __noinline struct tcp_rate_set *
1127 find_rs_for_ifp(struct ifnet *ifp)
1129 struct tcp_rate_set *rs;
1131 CK_LIST_FOREACH(rs, &int_rs, next) {
1132 if ((rs->rs_ifp == ifp) &&
1133 (rs->rs_if_dunit == ifp->if_dunit)) {
1134 /* Ok we found it */
1142 static const struct tcp_hwrate_limit_table *
1143 rt_setup_rate(struct inpcb *inp, struct ifnet *ifp, uint64_t bytes_per_sec,
1144 uint32_t flags, int *error, uint64_t *lower_rate)
1146 /* First lets find the interface if it exists */
1147 const struct tcp_hwrate_limit_table *rte;
1149 * So why is rs volatile? This is to defeat a
1150 * compiler bug where in the compiler is convinced
1151 * that rs can never be NULL (which is not true). Because
1152 * of its conviction it nicely optimizes out the if ((rs == NULL
1153 * below which means if you get a NULL back you dereference it.
1155 volatile struct tcp_rate_set *rs;
1156 struct epoch_tracker et;
1157 struct ifnet *oifp = ifp;
1160 NET_EPOCH_ENTER(et);
1162 rs = find_rs_for_ifp(ifp);
1164 (rs->rs_flags & RS_INTF_NO_SUP) ||
1165 (rs->rs_flags & RS_IS_DEAD)) {
1167 * This means we got a packet *before*
1168 * the IF-UP was processed below, <or>
1169 * while or after we already received an interface
1170 * departed event. In either case we really don't
1171 * want to do anything with pacing, in
1172 * the departing case the packet is not
1173 * going to go very far. The new case
1174 * might be arguable, but its impossible
1175 * to tell from the departing case.
1183 if ((rs == NULL) || (rs->rs_disable != 0)) {
1189 if (rs->rs_flags & RS_IS_DEFF) {
1190 /* We need to find the real interface */
1193 tifp = rt_find_real_interface(ifp, inp, error);
1195 if (rs->rs_disable && error)
1200 KASSERT((tifp != ifp),
1201 ("Lookup failure ifp:%p inp:%p rt_find_real_interface() returns the same interface tifp:%p?\n",
1204 goto use_real_interface;
1206 if (rs->rs_flow_limit &&
1207 ((rs->rs_flows_using + 1) > rs->rs_flow_limit)) {
1213 rte = tcp_find_suitable_rate(rs, bytes_per_sec, flags, lower_rate);
1215 err = in_pcbattach_txrtlmt(inp, oifp,
1221 /* Failed to attach */
1226 KASSERT((inp->inp_snd_tag != NULL) ,
1227 ("Setup rate has no snd_tag inp:%p rte:%p rate:%llu rs:%p",
1228 inp, rte, (unsigned long long)rte->rate, rs));
1230 counter_u64_add(rate_limit_new, 1);
1236 * We use an atomic here for accounting so we don't have to
1237 * use locks when freeing.
1239 atomic_add_64(&rs->rs_flows_using, 1);
1246 tcp_rl_ifnet_link(void *arg __unused, struct ifnet *ifp, int link_state)
1249 struct tcp_rate_set *rs;
1250 struct epoch_tracker et;
1252 if (((ifp->if_capenable & IFCAP_TXRTLMT) == 0) ||
1253 (link_state != LINK_STATE_UP)) {
1255 * We only care on an interface going up that is rate-limit
1260 NET_EPOCH_ENTER(et);
1262 rs = find_rs_for_ifp(ifp);
1264 /* We already have initialized this guy */
1265 mtx_unlock(&rs_mtx);
1269 mtx_unlock(&rs_mtx);
1271 rt_setup_new_rs(ifp, &error);
1275 tcp_rl_ifnet_departure(void *arg __unused, struct ifnet *ifp)
1277 struct tcp_rate_set *rs;
1278 struct epoch_tracker et;
1281 NET_EPOCH_ENTER(et);
1283 rs = find_rs_for_ifp(ifp);
1285 CK_LIST_REMOVE(rs, next);
1287 rs->rs_flags |= RS_IS_DEAD;
1288 for (i = 0; i < rs->rs_rate_cnt; i++) {
1289 if (rs->rs_rlt[i].flags & HDWRPACE_TAGPRESENT) {
1290 in_pcbdetach_tag(rs->rs_rlt[i].tag);
1291 rs->rs_rlt[i].tag = NULL;
1293 rs->rs_rlt[i].flags = HDWRPACE_IFPDEPARTED;
1295 if (rs->rs_flows_using == 0)
1296 rs_defer_destroy(rs);
1298 mtx_unlock(&rs_mtx);
1303 tcp_rl_shutdown(void *arg __unused, int howto __unused)
1305 struct tcp_rate_set *rs, *nrs;
1306 struct epoch_tracker et;
1309 NET_EPOCH_ENTER(et);
1311 CK_LIST_FOREACH_SAFE(rs, &int_rs, next, nrs) {
1312 CK_LIST_REMOVE(rs, next);
1314 rs->rs_flags |= RS_IS_DEAD;
1315 for (i = 0; i < rs->rs_rate_cnt; i++) {
1316 if (rs->rs_rlt[i].flags & HDWRPACE_TAGPRESENT) {
1317 in_pcbdetach_tag(rs->rs_rlt[i].tag);
1318 rs->rs_rlt[i].tag = NULL;
1320 rs->rs_rlt[i].flags = HDWRPACE_IFPDEPARTED;
1322 if (rs->rs_flows_using == 0)
1323 rs_defer_destroy(rs);
1325 mtx_unlock(&rs_mtx);
1329 const struct tcp_hwrate_limit_table *
1330 tcp_set_pacing_rate(struct tcpcb *tp, struct ifnet *ifp,
1331 uint64_t bytes_per_sec, int flags, int *error, uint64_t *lower_rate)
1333 struct inpcb *inp = tptoinpcb(tp);
1334 const struct tcp_hwrate_limit_table *rte;
1336 struct ktls_session *tls;
1339 INP_WLOCK_ASSERT(inp);
1341 if (inp->inp_snd_tag == NULL) {
1343 * We are setting up a rate for the first time.
1345 if ((ifp->if_capenable & IFCAP_TXRTLMT) == 0) {
1346 /* Not supported by the egress */
1353 if (tptosocket(tp)->so_snd.sb_flags & SB_TLS_IFNET) {
1354 tls = tptosocket(tp)->so_snd.sb_tls_info;
1356 if ((ifp->if_capenable & IFCAP_TXTLS_RTLMT) == 0 ||
1357 tls->mode != TCP_TLS_MODE_IFNET) {
1364 rte = rt_setup_rate(inp, ifp, bytes_per_sec, flags, error, lower_rate);
1366 rl_increment_using(rte);
1368 if (rte != NULL && tls != NULL && tls->snd_tag != NULL) {
1370 * Fake a route change error to reset the TLS
1371 * send tag. This will convert the existing
1372 * tag to a TLS ratelimit tag.
1374 MPASS(tls->snd_tag->sw->type == IF_SND_TAG_TYPE_TLS);
1375 ktls_output_eagain(inp, tls);
1380 * We are modifying a rate, wrong interface?
1387 tp->t_pacing_rate = rte->rate;
1393 const struct tcp_hwrate_limit_table *
1394 tcp_chg_pacing_rate(const struct tcp_hwrate_limit_table *crte,
1395 struct tcpcb *tp, struct ifnet *ifp,
1396 uint64_t bytes_per_sec, int flags, int *error, uint64_t *lower_rate)
1398 struct inpcb *inp = tptoinpcb(tp);
1399 const struct tcp_hwrate_limit_table *nrte;
1400 const struct tcp_rate_set *rs;
1402 struct ktls_session *tls = NULL;
1406 INP_WLOCK_ASSERT(inp);
1409 /* Wrong interface */
1416 if (tptosocket(tp)->so_snd.sb_flags & SB_TLS_IFNET) {
1417 tls = tptosocket(tp)->so_snd.sb_tls_info;
1418 if (tls->mode != TCP_TLS_MODE_IFNET)
1420 else if (tls->snd_tag != NULL &&
1421 tls->snd_tag->sw->type != IF_SND_TAG_TYPE_TLS_RATE_LIMIT) {
1422 if (!tls->reset_pending) {
1424 * NIC probably doesn't support
1425 * ratelimit TLS tags if it didn't
1426 * allocate one when an existing rate
1427 * was present, so ignore.
1429 tcp_rel_pacing_rate(crte, tp);
1431 *error = EOPNOTSUPP;
1436 * The send tag is being converted, so set the
1437 * rate limit on the inpcb tag. There is a
1438 * race that the new NIC send tag might use
1439 * the current rate instead of this one.
1445 if (inp->inp_snd_tag == NULL) {
1446 /* Wrong interface */
1447 tcp_rel_pacing_rate(crte, tp);
1453 if ((rs->rs_flags & RS_IS_DEAD) ||
1454 (crte->flags & HDWRPACE_IFPDEPARTED)) {
1455 /* Release the rate, and try anew */
1457 tcp_rel_pacing_rate(crte, tp);
1458 nrte = tcp_set_pacing_rate(tp, ifp,
1459 bytes_per_sec, flags, error, lower_rate);
1462 nrte = tcp_find_suitable_rate(rs, bytes_per_sec, flags, lower_rate);
1470 /* Release the old rate */
1473 tcp_rel_pacing_rate(crte, tp);
1476 rl_decrement_using(crte);
1477 rl_increment_using(nrte);
1478 /* Change rates to our new entry */
1481 err = ktls_modify_txrtlmt(tls, nrte->rate);
1484 err = in_pcbmodify_txrtlmt(inp, nrte->rate);
1486 struct tcp_rate_set *lrs;
1489 rl_decrement_using(nrte);
1490 lrs = __DECONST(struct tcp_rate_set *, rs);
1491 pre = atomic_fetchadd_64(&lrs->rs_flows_using, -1);
1492 /* Do we still have a snd-tag attached? */
1493 if (inp->inp_snd_tag)
1494 in_pcbdetach_txrtlmt(inp);
1497 struct epoch_tracker et;
1499 NET_EPOCH_ENTER(et);
1504 if (lrs->rs_flags & RS_IS_DEAD)
1505 rs_defer_destroy(lrs);
1506 mtx_unlock(&rs_mtx);
1514 counter_u64_add(rate_limit_chg, 1);
1519 tp->t_pacing_rate = nrte->rate;
1524 tcp_rel_pacing_rate(const struct tcp_hwrate_limit_table *crte, struct tcpcb *tp)
1526 struct inpcb *inp = tptoinpcb(tp);
1527 const struct tcp_rate_set *crs;
1528 struct tcp_rate_set *rs;
1531 INP_WLOCK_ASSERT(inp);
1533 tp->t_pacing_rate = -1;
1536 * Now we must break the const
1537 * in order to release our refcount.
1539 rs = __DECONST(struct tcp_rate_set *, crs);
1540 rl_decrement_using(crte);
1541 pre = atomic_fetchadd_64(&rs->rs_flows_using, -1);
1543 struct epoch_tracker et;
1545 NET_EPOCH_ENTER(et);
1550 if (rs->rs_flags & RS_IS_DEAD)
1551 rs_defer_destroy(rs);
1552 mtx_unlock(&rs_mtx);
1557 * XXX: If this connection is using ifnet TLS, should we
1558 * switch it to using an unlimited rate, or perhaps use
1559 * ktls_output_eagain() to reset the send tag to a plain
1562 in_pcbdetach_txrtlmt(inp);
1565 #define ONE_POINT_TWO_MEG 150000 /* 1.2 megabits in bytes */
1566 #define ONE_HUNDRED_MBPS 12500000 /* 100Mbps in bytes per second */
1567 #define FIVE_HUNDRED_MBPS 62500000 /* 500Mbps in bytes per second */
1568 #define MAX_MSS_SENT 43 /* 43 mss = 43 x 1500 = 64,500 bytes */
1571 tcp_log_pacing_size(struct tcpcb *tp, uint64_t bw, uint32_t segsiz, uint32_t new_tso,
1572 uint64_t hw_rate, uint32_t time_between, uint32_t calc_time_between,
1573 uint32_t segs, uint32_t res_div, uint16_t mult, uint8_t mod)
1575 if (tp->t_logstate != TCP_LOG_STATE_OFF) {
1576 union tcp_log_stackspecific log;
1579 memset(&log, 0, sizeof(log));
1580 log.u_bbr.flex1 = segsiz;
1581 log.u_bbr.flex2 = new_tso;
1582 log.u_bbr.flex3 = time_between;
1583 log.u_bbr.flex4 = calc_time_between;
1584 log.u_bbr.flex5 = segs;
1585 log.u_bbr.flex6 = res_div;
1586 log.u_bbr.flex7 = mult;
1587 log.u_bbr.flex8 = mod;
1588 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
1589 log.u_bbr.cur_del_rate = bw;
1590 log.u_bbr.delRate = hw_rate;
1591 TCP_LOG_EVENTP(tp, NULL,
1592 &tptosocket(tp)->so_rcv,
1593 &tptosocket(tp)->so_snd,
1594 TCP_HDWR_PACE_SIZE, 0,
1595 0, &log, false, &tv);
1600 tcp_get_pacing_burst_size_w_divisor(struct tcpcb *tp, uint64_t bw, uint32_t segsiz, int can_use_1mss,
1601 const struct tcp_hwrate_limit_table *te, int *err, int divisor)
1604 * We use the google formula to calculate the
1609 * tso = min(bw/(div=1000), 64k)
1611 * Note for these calculations we ignore the
1612 * packet overhead (enet hdr, ip hdr and tcp hdr).
1613 * We only get the google formula when we have
1614 * divisor = 1000, which is the default for now.
1616 uint64_t lentim, res, bytes;
1617 uint32_t new_tso, min_tso_segs;
1619 /* It can't be zero */
1620 if ((divisor == 0) ||
1621 (divisor < RL_MIN_DIVISOR)) {
1623 bytes = bw / mss_divisor;
1627 bytes = bw / divisor;
1628 /* We can't ever send more than 65k in a TSO */
1629 if (bytes > 0xffff) {
1633 new_tso = (bytes + segsiz - 1) / segsiz;
1634 /* Are we enforcing even boundaries? */
1635 if (even_num_segs && (new_tso & 1) && (new_tso > even_threshold))
1641 if (rs_floor_mss && (new_tso < rs_floor_mss))
1642 new_tso = rs_floor_mss;
1643 else if (new_tso < min_tso_segs)
1644 new_tso = min_tso_segs;
1645 if (new_tso > MAX_MSS_SENT)
1646 new_tso = MAX_MSS_SENT;
1648 tcp_log_pacing_size(tp, bw, segsiz, new_tso,
1649 0, 0, 0, 0, 0, 0, 1);
1651 * If we are not doing hardware pacing
1660 * For hardware pacing we look at the
1661 * rate you are sending at and compare
1662 * that to the rate you have in hardware.
1664 * If the hardware rate is slower than your
1665 * software rate then you are in error and
1666 * we will build a queue in our hardware whic
1667 * is probably not desired, in such a case
1668 * just return the non-hardware TSO size.
1670 * If the rate in hardware is faster (which
1671 * it should be) then look at how long it
1672 * takes to send one ethernet segment size at
1673 * your b/w and compare that to the time it
1674 * takes to send at the rate you had selected.
1676 * If your time is greater (which we hope it is)
1677 * we get the delta between the two, and then
1678 * divide that into your pacing time. This tells
1679 * us how many MSS you can send down at once (rounded up).
1681 * Note we also double this value if the b/w is over
1682 * 100Mbps. If its over 500meg we just set you to the
1683 * max (43 segments).
1685 if (te->rate > FIVE_HUNDRED_MBPS)
1687 if (te->rate == bw) {
1688 /* We are pacing at exactly the hdwr rate */
1690 tcp_log_pacing_size(tp, bw, segsiz, new_tso,
1691 te->rate, te->time_between, (uint32_t)0,
1692 (segsiz * MAX_MSS_SENT), 0, 0, 3);
1693 return (segsiz * MAX_MSS_SENT);
1695 lentim = ETHERNET_SEGMENT_SIZE * USECS_IN_SECOND;
1697 if (res > te->time_between) {
1698 uint32_t delta, segs, res_div;
1700 res_div = ((res * num_of_waits_allowed) + wait_time_floor);
1701 delta = res - te->time_between;
1702 segs = (res_div + delta - 1)/delta;
1703 if (segs < min_tso_segs)
1704 segs = min_tso_segs;
1705 if (segs < rs_hw_floor_mss)
1706 segs = rs_hw_floor_mss;
1707 if (segs > MAX_MSS_SENT)
1708 segs = MAX_MSS_SENT;
1710 tcp_log_pacing_size(tp, bw, segsiz, new_tso,
1711 te->rate, te->time_between, (uint32_t)res,
1712 segs, res_div, 1, 3);
1715 if (segs < new_tso) {
1723 * Your time is smaller which means
1724 * we will grow a queue on our
1725 * hardware. Send back the non-hardware
1728 tcp_log_pacing_size(tp, bw, segsiz, new_tso,
1729 te->rate, te->time_between, (uint32_t)res,
1738 tcp_hw_highest_rate_ifp(struct ifnet *ifp, struct inpcb *inp)
1740 struct epoch_tracker et;
1741 struct tcp_rate_set *rs;
1744 NET_EPOCH_ENTER(et);
1746 rs = find_rs_for_ifp(ifp);
1748 /* This interface does not do ratelimiting */
1750 } else if (rs->rs_flags & RS_IS_DEFF) {
1751 /* We need to find the real interface */
1754 tifp = rt_find_real_interface(ifp, inp, NULL);
1760 goto use_next_interface;
1762 /* Lets return the highest rate this guy has */
1763 rate_ret = rs->rs_rlt[rs->rs_highest_valid].rate;
1769 static eventhandler_tag rl_ifnet_departs;
1770 static eventhandler_tag rl_ifnet_arrives;
1771 static eventhandler_tag rl_shutdown_start;
1774 tcp_rs_init(void *st __unused)
1776 CK_LIST_INIT(&int_rs);
1777 rs_number_alive = 0;
1779 mtx_init(&rs_mtx, "tcp_rs_mtx", "rsmtx", MTX_DEF);
1780 rl_ifnet_departs = EVENTHANDLER_REGISTER(ifnet_departure_event,
1781 tcp_rl_ifnet_departure,
1782 NULL, EVENTHANDLER_PRI_ANY);
1783 rl_ifnet_arrives = EVENTHANDLER_REGISTER(ifnet_link_event,
1785 NULL, EVENTHANDLER_PRI_ANY);
1786 rl_shutdown_start = EVENTHANDLER_REGISTER(shutdown_pre_sync,
1787 tcp_rl_shutdown, NULL,
1788 SHUTDOWN_PRI_FIRST);
1789 printf("TCP_ratelimit: Is now initialized\n");
1792 SYSINIT(tcp_rl_init, SI_SUB_SMP + 1, SI_ORDER_ANY, tcp_rs_init, NULL);