3 * SPDX-License-Identifier: BSD-3-Clause
5 * Copyright (c) 2018-2019
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Author: Randall Stewart <rrs@netflix.com>
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include "opt_inet6.h"
39 #include "opt_ipsec.h"
40 #include "opt_tcpdebug.h"
41 #include "opt_ratelimit.h"
42 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
49 #include <sys/sockbuf_tls.h>
51 #include <sys/sysctl.h>
52 #include <sys/eventhandler.h>
53 #include <sys/mutex.h>
55 #define TCPSTATES /* for logging */
56 #include <netinet/in.h>
57 #include <netinet/in_pcb.h>
58 #include <netinet/tcp_var.h>
60 #include <netinet6/tcp6_var.h>
62 #include <netinet/tcp_ratelimit.h>
63 #ifndef USECS_IN_SECOND
64 #define USECS_IN_SECOND 1000000
67 * For the purposes of each send, what is the size
68 * of an ethernet frame.
70 #ifndef ETHERNET_SEGMENT_SIZE
71 #define ETHERNET_SEGMENT_SIZE 1500
73 MALLOC_DEFINE(M_TCPPACE, "tcp_hwpace", "TCP Hardware pacing memory");
76 #define COMMON_RATE 180500
77 uint64_t desired_rates[] = {
79 180500, /* 1.44Mpbs */
90 12500000, /* 100Mbps */
91 25000000, /* 200Mbps */
92 50000000, /* 400Mbps */
93 100000000, /* 800Mbps */
100 8125000, /* 65Mbps */
101 10000000, /* 80Mbps */
102 18750000, /* 150Mbps */
103 20000000, /* 250Mbps */
104 37500000, /* 350Mbps */
105 62500000, /* 500Mbps */
106 78125000, /* 625Mbps */
107 125000000, /* 1Gbps */
109 #define MAX_HDWR_RATES (sizeof(desired_rates)/sizeof(uint64_t))
110 #define RS_ORDERED_COUNT 16 /*
111 * Number that are in order
112 * at the beginning of the table,
113 * over this a sort is required.
115 #define RS_NEXT_ORDER_GROUP 16 /*
116 * The point in our table where
117 * we come fill in a second ordered
118 * group (index wise means -1).
120 #define ALL_HARDWARE_RATES 1004 /*
121 * 1Meg - 1Gig in 1 Meg steps
122 * plus 100, 200k and 500k and
126 #define RS_ONE_MEGABIT_PERSEC 1000000
127 #define RS_ONE_GIGABIT_PERSEC 1000000000
128 #define RS_TEN_GIGABIT_PERSEC 10000000000
130 static struct head_tcp_rate_set int_rs;
131 static struct mtx rs_mtx;
132 uint32_t rs_number_alive;
133 uint32_t rs_number_dead;
135 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, rl, CTLFLAG_RW, 0,
136 "TCP Ratelimit stats");
137 SYSCTL_UINT(_net_inet_tcp_rl, OID_AUTO, alive, CTLFLAG_RW,
139 "Number of interfaces initialized for ratelimiting");
140 SYSCTL_UINT(_net_inet_tcp_rl, OID_AUTO, dead, CTLFLAG_RW,
142 "Number of interfaces departing from ratelimiting");
145 rl_add_syctl_entries(struct sysctl_oid *rl_sysctl_root, struct tcp_rate_set *rs)
148 * Add sysctl entries for thus interface.
150 if (rs->rs_flags & RS_INTF_NO_SUP) {
151 SYSCTL_ADD_S32(&rs->sysctl_ctx,
152 SYSCTL_CHILDREN(rl_sysctl_root),
153 OID_AUTO, "disable", CTLFLAG_RD,
155 "Disable this interface from new hdwr limiting?");
157 SYSCTL_ADD_S32(&rs->sysctl_ctx,
158 SYSCTL_CHILDREN(rl_sysctl_root),
159 OID_AUTO, "disable", CTLFLAG_RW,
161 "Disable this interface from new hdwr limiting?");
163 SYSCTL_ADD_S32(&rs->sysctl_ctx,
164 SYSCTL_CHILDREN(rl_sysctl_root),
165 OID_AUTO, "minseg", CTLFLAG_RW,
167 "What is the minimum we need to send on this interface?");
168 SYSCTL_ADD_U64(&rs->sysctl_ctx,
169 SYSCTL_CHILDREN(rl_sysctl_root),
170 OID_AUTO, "flow_limit", CTLFLAG_RW,
171 &rs->rs_flow_limit, 0,
172 "What is the limit for number of flows (0=unlimited)?");
173 SYSCTL_ADD_S32(&rs->sysctl_ctx,
174 SYSCTL_CHILDREN(rl_sysctl_root),
175 OID_AUTO, "highest", CTLFLAG_RD,
176 &rs->rs_highest_valid, 0,
177 "Highest valid rate");
178 SYSCTL_ADD_S32(&rs->sysctl_ctx,
179 SYSCTL_CHILDREN(rl_sysctl_root),
180 OID_AUTO, "lowest", CTLFLAG_RD,
181 &rs->rs_lowest_valid, 0,
182 "Lowest valid rate");
183 SYSCTL_ADD_S32(&rs->sysctl_ctx,
184 SYSCTL_CHILDREN(rl_sysctl_root),
185 OID_AUTO, "flags", CTLFLAG_RD,
187 "What lags are on the entry?");
188 SYSCTL_ADD_S32(&rs->sysctl_ctx,
189 SYSCTL_CHILDREN(rl_sysctl_root),
190 OID_AUTO, "numrates", CTLFLAG_RD,
192 "How many rates re there?");
193 SYSCTL_ADD_U64(&rs->sysctl_ctx,
194 SYSCTL_CHILDREN(rl_sysctl_root),
195 OID_AUTO, "flows_using", CTLFLAG_RD,
196 &rs->rs_flows_using, 0,
197 "How many flows are using this interface now?");
198 #ifdef DETAILED_RATELIMIT_SYSCTL
199 if (rs->rs_rlt && rs->rs_rate_cnt > 0) {
200 /* Lets display the rates */
202 struct sysctl_oid *rl_rates;
203 struct sysctl_oid *rl_rate_num;
205 rl_rates = SYSCTL_ADD_NODE(&rs->sysctl_ctx,
206 SYSCTL_CHILDREN(rl_sysctl_root),
211 for( i = 0; i < rs->rs_rate_cnt; i++) {
212 sprintf(rate_num, "%d", i);
213 rl_rate_num = SYSCTL_ADD_NODE(&rs->sysctl_ctx,
214 SYSCTL_CHILDREN(rl_rates),
219 SYSCTL_ADD_U32(&rs->sysctl_ctx,
220 SYSCTL_CHILDREN(rl_rate_num),
221 OID_AUTO, "flags", CTLFLAG_RD,
222 &rs->rs_rlt[i].flags, 0,
223 "Flags on this rate");
224 SYSCTL_ADD_U32(&rs->sysctl_ctx,
225 SYSCTL_CHILDREN(rl_rate_num),
226 OID_AUTO, "pacetime", CTLFLAG_RD,
227 &rs->rs_rlt[i].time_between, 0,
228 "Time hardware inserts between 1500 byte sends");
229 SYSCTL_ADD_U64(&rs->sysctl_ctx,
230 SYSCTL_CHILDREN(rl_rate_num),
231 OID_AUTO, "rate", CTLFLAG_RD,
232 &rs->rs_rlt[i].rate, 0,
233 "Rate in bytes per second");
240 rs_destroy(epoch_context_t ctx)
242 struct tcp_rate_set *rs;
244 rs = __containerof(ctx, struct tcp_rate_set, rs_epoch_ctx);
246 rs->rs_flags &= ~RS_FUNERAL_SCHD;
247 if (rs->rs_flows_using == 0) {
249 * In theory its possible (but unlikely)
250 * that while the delete was occuring
251 * and we were applying the DEAD flag
252 * someone slipped in and found the
253 * interface in a lookup. While we
254 * decided rs_flows_using were 0 and
255 * scheduling the epoch_call, the other
256 * thread incremented rs_flow_using. This
257 * is because users have a pointer and
258 * we only use the rs_flows_using in an
259 * atomic fashion, i.e. the other entities
260 * are not protected. To assure this did
261 * not occur, we check rs_flows_using here
264 sysctl_ctx_free(&rs->sysctl_ctx);
265 free(rs->rs_rlt, M_TCPPACE);
274 extern counter_u64_t rate_limit_set_ok;
275 extern counter_u64_t rate_limit_active;
276 extern counter_u64_t rate_limit_alloc_fail;
280 rl_attach_txrtlmt(struct ifnet *ifp,
284 struct m_snd_tag **tag)
287 union if_snd_tag_alloc_params params = {
288 .rate_limit.hdr.type = IF_SND_TAG_TYPE_RATE_LIMIT,
289 .rate_limit.hdr.flowid = flowid,
290 .rate_limit.hdr.flowtype = flowtype,
291 .rate_limit.max_rate = cfg_rate,
292 .rate_limit.flags = M_NOWAIT,
295 if (ifp->if_snd_tag_alloc == NULL) {
298 error = ifp->if_snd_tag_alloc(ifp, ¶ms, tag);
302 counter_u64_add(rate_limit_set_ok, 1);
303 counter_u64_add(rate_limit_active, 1);
305 counter_u64_add(rate_limit_alloc_fail, 1);
312 populate_canned_table(struct tcp_rate_set *rs, const uint64_t *rate_table_act)
315 * The internal table is "special", it
316 * is two seperate ordered tables that
317 * must be merged. We get here when the
318 * adapter specifies a number of rates that
319 * covers both ranges in the table in some
322 int i, at_low, at_high;
323 uint8_t low_disabled = 0, high_disabled = 0;
325 for(i = 0, at_low = 0, at_high = RS_NEXT_ORDER_GROUP; i < rs->rs_rate_cnt; i++) {
326 rs->rs_rlt[i].flags = 0;
327 rs->rs_rlt[i].time_between = 0;
328 if ((low_disabled == 0) &&
330 (rate_table_act[at_low] < rate_table_act[at_high]))) {
331 rs->rs_rlt[i].rate = rate_table_act[at_low];
333 if (at_low == RS_NEXT_ORDER_GROUP)
335 } else if (high_disabled == 0) {
336 rs->rs_rlt[i].rate = rate_table_act[at_high];
338 if (at_high == MAX_HDWR_RATES)
344 static struct tcp_rate_set *
345 rt_setup_new_rs(struct ifnet *ifp, int *error)
347 struct tcp_rate_set *rs;
348 const uint64_t *rate_table_act;
349 uint64_t lentim, res;
353 struct if_ratelimit_query_results rl;
354 struct sysctl_oid *rl_sysctl_root;
356 * We expect to enter with the
360 if (ifp->if_ratelimit_query == NULL) {
362 * We can do nothing if we cannot
363 * get a query back from the driver.
367 rs = malloc(sizeof(struct tcp_rate_set), M_TCPPACE, M_NOWAIT | M_ZERO);
373 rl.flags = RT_NOSUPPORT;
374 ifp->if_ratelimit_query(ifp, &rl);
375 if (rl.flags & RT_IS_UNUSABLE) {
377 * The interface does not really support
380 memset(rs, 0, sizeof(struct tcp_rate_set));
382 rs->rs_if_dunit = ifp->if_dunit;
383 rs->rs_flags = RS_INTF_NO_SUP;
386 sysctl_ctx_init(&rs->sysctl_ctx);
387 rl_sysctl_root = SYSCTL_ADD_NODE(&rs->sysctl_ctx,
388 SYSCTL_STATIC_CHILDREN(_net_inet_tcp_rl),
390 rs->rs_ifp->if_xname,
393 CK_LIST_INSERT_HEAD(&int_rs, rs, next);
394 /* Unlock to allow the sysctl stuff to allocate */
396 rl_add_syctl_entries(rl_sysctl_root, rs);
397 /* re-lock for our caller */
400 } else if ((rl.flags & RT_IS_INDIRECT) == RT_IS_INDIRECT) {
401 memset(rs, 0, sizeof(struct tcp_rate_set));
403 rs->rs_if_dunit = ifp->if_dunit;
404 rs->rs_flags = RS_IS_DEFF;
406 sysctl_ctx_init(&rs->sysctl_ctx);
407 rl_sysctl_root = SYSCTL_ADD_NODE(&rs->sysctl_ctx,
408 SYSCTL_STATIC_CHILDREN(_net_inet_tcp_rl),
410 rs->rs_ifp->if_xname,
413 CK_LIST_INSERT_HEAD(&int_rs, rs, next);
414 /* Unlock to allow the sysctl stuff to allocate */
416 rl_add_syctl_entries(rl_sysctl_root, rs);
417 /* re-lock for our caller */
420 } else if ((rl.flags & RT_IS_FIXED_TABLE) == RT_IS_FIXED_TABLE) {
421 /* Mellanox most likely */
423 rs->rs_if_dunit = ifp->if_dunit;
424 rs->rs_rate_cnt = rl.number_of_rates;
425 rs->rs_min_seg = rl.min_segment_burst;
426 rs->rs_highest_valid = 0;
427 rs->rs_flow_limit = rl.max_flows;
428 rs->rs_flags = RS_IS_INTF | RS_NO_PRE;
430 rate_table_act = rl.rate_table;
431 } else if ((rl.flags & RT_IS_SELECTABLE) == RT_IS_SELECTABLE) {
434 rs->rs_if_dunit = ifp->if_dunit;
435 rs->rs_rate_cnt = rl.number_of_rates;
436 rs->rs_min_seg = rl.min_segment_burst;
438 rs->rs_flow_limit = rl.max_flows;
439 rate_table_act = desired_rates;
440 if ((rs->rs_rate_cnt > MAX_HDWR_RATES) &&
441 (rs->rs_rate_cnt < ALL_HARDWARE_RATES)) {
443 * Our desired table is not big
444 * enough, do what we can.
446 rs->rs_rate_cnt = MAX_HDWR_RATES;
448 if (rs->rs_rate_cnt <= RS_ORDERED_COUNT)
449 rs->rs_flags = RS_IS_INTF;
451 rs->rs_flags = RS_IS_INTF | RS_INT_TBL;
452 if (rs->rs_rate_cnt >= ALL_HARDWARE_RATES)
453 rs->rs_rate_cnt = ALL_HARDWARE_RATES;
455 printf("Interface:%s unit:%d not one known to have rate-limits\n",
461 sz = sizeof(struct tcp_hwrate_limit_table) * rs->rs_rate_cnt;
462 rs->rs_rlt = malloc(sz, M_TCPPACE, M_NOWAIT);
463 if (rs->rs_rlt == NULL) {
470 if (rs->rs_rate_cnt >= ALL_HARDWARE_RATES) {
472 * The interface supports all
473 * the rates we could possibly want.
477 rs->rs_rlt[0].rate = 12500; /* 100k */
478 rs->rs_rlt[1].rate = 25000; /* 200k */
479 rs->rs_rlt[2].rate = 62500; /* 500k */
480 /* Note 125000 == 1Megabit
481 * populate 1Meg - 1000meg.
483 for(i = 3, rat = 125000; i< (ALL_HARDWARE_RATES-1); i++) {
484 rs->rs_rlt[i].rate = rat;
487 rs->rs_rlt[(ALL_HARDWARE_RATES-1)].rate = 1250000000;
488 } else if (rs->rs_flags & RS_INT_TBL) {
489 /* We populate this in a special way */
490 populate_canned_table(rs, rate_table_act);
493 * Just copy in the rates from
494 * the table, it is in order.
496 for (i=0; i<rs->rs_rate_cnt; i++) {
497 rs->rs_rlt[i].rate = rate_table_act[i];
498 rs->rs_rlt[i].time_between = 0;
499 rs->rs_rlt[i].flags = 0;
502 for (i = (rs->rs_rate_cnt - 1); i >= 0; i--) {
504 * We go backwards through the list so that if we can't get
505 * a rate and fail to init one, we have at least a chance of
506 * getting the highest one.
508 rs->rs_rlt[i].ptbl = rs;
509 rs->rs_rlt[i].tag = NULL;
511 * Calculate the time between.
513 lentim = ETHERNET_SEGMENT_SIZE * USECS_IN_SECOND;
514 res = lentim / rs->rs_rlt[i].rate;
516 rs->rs_rlt[i].time_between = res;
518 rs->rs_rlt[i].time_between = 1;
519 if (rs->rs_flags & RS_NO_PRE) {
520 rs->rs_rlt[i].flags = HDWRPACE_INITED;
521 rs->rs_lowest_valid = i;
525 hash_type = M_HASHTYPE_RSS_TCP_IPV4;
527 hash_type = M_HASHTYPE_OPAQUE_HASH;
529 err = rl_attach_txrtlmt(ifp,
535 if (i == (rs->rs_rate_cnt - 1)) {
537 * Huh - first rate and we can't get
540 free(rs->rs_rlt, M_TCPPACE);
550 rs->rs_rlt[i].flags = HDWRPACE_INITED | HDWRPACE_TAGPRESENT;
551 rs->rs_lowest_valid = i;
555 /* Did we get at least 1 rate? */
556 if (rs->rs_rlt[(rs->rs_rate_cnt - 1)].flags & HDWRPACE_INITED)
557 rs->rs_highest_valid = rs->rs_rate_cnt - 1;
559 free(rs->rs_rlt, M_TCPPACE);
563 CK_LIST_INSERT_HEAD(&int_rs, rs, next);
564 sysctl_ctx_init(&rs->sysctl_ctx);
565 rl_sysctl_root = SYSCTL_ADD_NODE(&rs->sysctl_ctx,
566 SYSCTL_STATIC_CHILDREN(_net_inet_tcp_rl),
568 rs->rs_ifp->if_xname,
571 /* Unlock to allow the sysctl stuff to allocate */
573 rl_add_syctl_entries(rl_sysctl_root, rs);
574 /* re-lock for our caller */
579 static const struct tcp_hwrate_limit_table *
580 tcp_int_find_suitable_rate(const struct tcp_rate_set *rs,
581 uint64_t bytes_per_sec, uint32_t flags)
583 struct tcp_hwrate_limit_table *arte = NULL, *rte = NULL;
584 uint64_t mbits_per_sec, ind_calc;
587 mbits_per_sec = (bytes_per_sec * 8);
588 if (flags & RS_PACING_LT) {
589 if ((mbits_per_sec < RS_ONE_MEGABIT_PERSEC) &&
590 (rs->rs_lowest_valid <= 2)){
592 * Smaller than 1Meg, only
593 * 3 entries can match it.
595 for(i = rs->rs_lowest_valid; i < 3; i++) {
596 if (bytes_per_sec <= rs->rs_rlt[i].rate) {
597 rte = &rs->rs_rlt[i];
599 } else if (rs->rs_rlt[i].flags & HDWRPACE_INITED) {
600 arte = &rs->rs_rlt[i];
604 } else if ((mbits_per_sec > RS_ONE_GIGABIT_PERSEC) &&
605 (rs->rs_rlt[(ALL_HARDWARE_RATES-1)].flags & HDWRPACE_INITED)){
607 * Larger than 1G (the majority of
610 if (mbits_per_sec < RS_TEN_GIGABIT_PERSEC)
611 rte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
613 arte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
617 * If we reach here its in our table (between 1Meg - 1000Meg),
618 * just take the rounded down mbits per second, and add
619 * 1Megabit to it, from this we can calculate
620 * the index in the table.
622 ind_calc = mbits_per_sec/RS_ONE_MEGABIT_PERSEC;
623 if ((ind_calc * RS_ONE_MEGABIT_PERSEC) != mbits_per_sec)
625 /* our table is offset by 3, we add 2 */
627 if (ind_calc > (ALL_HARDWARE_RATES-1)) {
628 /* This should not happen */
629 ind_calc = ALL_HARDWARE_RATES-1;
631 if ((ind_calc >= rs->rs_lowest_valid) &&
632 (ind_calc <= rs->rs_highest_valid))
633 rte = &rs->rs_rlt[ind_calc];
634 } else if (flags & RS_PACING_EXACT_MATCH) {
635 if ((mbits_per_sec < RS_ONE_MEGABIT_PERSEC) &&
636 (rs->rs_lowest_valid <= 2)){
637 for(i = rs->rs_lowest_valid; i < 3; i++) {
638 if (bytes_per_sec == rs->rs_rlt[i].rate) {
639 rte = &rs->rs_rlt[i];
643 } else if ((mbits_per_sec > RS_ONE_GIGABIT_PERSEC) &&
644 (rs->rs_rlt[(ALL_HARDWARE_RATES-1)].flags & HDWRPACE_INITED)) {
645 /* > 1Gbps only one rate */
646 if (bytes_per_sec == rs->rs_rlt[(ALL_HARDWARE_RATES-1)].rate) {
648 rte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
651 /* Ok it must be a exact meg (its between 1G and 1Meg) */
652 ind_calc = mbits_per_sec/RS_ONE_MEGABIT_PERSEC;
653 if ((ind_calc * RS_ONE_MEGABIT_PERSEC) == mbits_per_sec) {
654 /* its an exact Mbps */
656 if (ind_calc > (ALL_HARDWARE_RATES-1)) {
657 /* This should not happen */
658 ind_calc = ALL_HARDWARE_RATES-1;
660 if (rs->rs_rlt[ind_calc].flags & HDWRPACE_INITED)
661 rte = &rs->rs_rlt[ind_calc];
665 /* we want greater than the requested rate */
666 if ((mbits_per_sec < RS_ONE_MEGABIT_PERSEC) &&
667 (rs->rs_lowest_valid <= 2)){
668 arte = &rs->rs_rlt[3]; /* set alternate to 1Meg */
669 for (i=2; i>=rs->rs_lowest_valid; i--) {
670 if (bytes_per_sec < rs->rs_rlt[i].rate) {
671 rte = &rs->rs_rlt[i];
673 } else if ((flags & RS_PACING_GEQ) &&
674 (bytes_per_sec == rs->rs_rlt[i].rate)) {
675 rte = &rs->rs_rlt[i];
678 arte = &rs->rs_rlt[i]; /* new alternate */
681 } else if (mbits_per_sec > RS_ONE_GIGABIT_PERSEC) {
682 if ((bytes_per_sec < rs->rs_rlt[(ALL_HARDWARE_RATES-1)].rate) &&
683 (rs->rs_rlt[(ALL_HARDWARE_RATES-1)].flags & HDWRPACE_INITED)){
684 /* Our top rate is larger than the request */
685 rte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
686 } else if ((flags & RS_PACING_GEQ) &&
687 (bytes_per_sec == rs->rs_rlt[(ALL_HARDWARE_RATES-1)].rate) &&
688 (rs->rs_rlt[(ALL_HARDWARE_RATES-1)].flags & HDWRPACE_INITED)) {
689 /* It matches our top rate */
690 rte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
691 } else if (rs->rs_rlt[(ALL_HARDWARE_RATES-1)].flags & HDWRPACE_INITED) {
692 /* The top rate is an alternative */
693 arte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
696 /* Its in our range 1Meg - 1Gig */
697 if (flags & RS_PACING_GEQ) {
698 ind_calc = mbits_per_sec/RS_ONE_MEGABIT_PERSEC;
699 if ((ind_calc * RS_ONE_MEGABIT_PERSEC) == mbits_per_sec) {
700 if (ind_calc > (ALL_HARDWARE_RATES-1)) {
701 /* This should not happen */
702 ind_calc = (ALL_HARDWARE_RATES-1);
704 rte = &rs->rs_rlt[ind_calc];
708 ind_calc = (mbits_per_sec + (RS_ONE_MEGABIT_PERSEC-1))/RS_ONE_MEGABIT_PERSEC;
710 if (ind_calc > (ALL_HARDWARE_RATES-1)) {
711 /* This should not happen */
712 ind_calc = ALL_HARDWARE_RATES-1;
714 if (rs->rs_rlt[ind_calc].flags & HDWRPACE_INITED)
715 rte = &rs->rs_rlt[ind_calc];
721 (flags & RS_PACING_SUB_OK)) {
722 /* We can use the substitute */
728 static const struct tcp_hwrate_limit_table *
729 tcp_find_suitable_rate(const struct tcp_rate_set *rs, uint64_t bytes_per_sec, uint32_t flags)
732 * Hunt the rate table with the restrictions in flags and find a
733 * suitable rate if possible.
734 * RS_PACING_EXACT_MATCH - look for an exact match to rate.
735 * RS_PACING_GT - must be greater than.
736 * RS_PACING_GEQ - must be greater than or equal.
737 * RS_PACING_LT - must be less than.
738 * RS_PACING_SUB_OK - If we don't meet criteria a
742 struct tcp_hwrate_limit_table *rte = NULL;
745 if ((rs->rs_flags & RS_INT_TBL) &&
746 (rs->rs_rate_cnt >= ALL_HARDWARE_RATES)) {
748 * Here we don't want to paw thru
749 * a big table, we have everything
750 * from 1Meg - 1000Meg in 1Meg increments.
751 * Use an alternate method to "lookup".
753 return (tcp_int_find_suitable_rate(rs, bytes_per_sec, flags));
755 if ((flags & RS_PACING_LT) ||
756 (flags & RS_PACING_EXACT_MATCH)) {
758 * For exact and less than we go forward through the table.
759 * This way when we find one larger we stop (exact was a
762 for (i = rs->rs_lowest_valid, matched = 0; i <= rs->rs_highest_valid; i++) {
763 if ((flags & RS_PACING_EXACT_MATCH) &&
764 (bytes_per_sec == rs->rs_rlt[i].rate)) {
765 rte = &rs->rs_rlt[i];
768 } else if ((flags & RS_PACING_LT) &&
769 (bytes_per_sec <= rs->rs_rlt[i].rate)) {
770 rte = &rs->rs_rlt[i];
774 if (bytes_per_sec > rs->rs_rlt[i].rate)
777 if ((matched == 0) &&
778 (flags & RS_PACING_LT) &&
779 (flags & RS_PACING_SUB_OK)) {
780 /* Kick in a substitute (the lowest) */
781 rte = &rs->rs_rlt[rs->rs_lowest_valid];
785 * Here we go backward through the table so that we can find
786 * the one greater in theory faster (but its probably a
789 for (i = rs->rs_highest_valid, matched = 0; i >= rs->rs_lowest_valid; i--) {
790 if (rs->rs_rlt[i].rate > bytes_per_sec) {
791 /* A possible candidate */
792 rte = &rs->rs_rlt[i];
794 if ((flags & RS_PACING_GEQ) &&
795 (bytes_per_sec == rs->rs_rlt[i].rate)) {
796 /* An exact match and we want equal */
798 rte = &rs->rs_rlt[i];
802 * Found one that is larger than but don't
803 * stop, there may be a more closer match.
807 if (rs->rs_rlt[i].rate < bytes_per_sec) {
809 * We found a table entry that is smaller,
810 * stop there will be none greater or equal.
815 if ((matched == 0) &&
816 (flags & RS_PACING_SUB_OK)) {
817 /* Kick in a substitute (the highest) */
818 rte = &rs->rs_rlt[rs->rs_highest_valid];
824 static struct ifnet *
825 rt_find_real_interface(struct ifnet *ifp, struct inpcb *inp, int *error)
828 struct m_snd_tag *tag;
829 union if_snd_tag_alloc_params params = {
830 .rate_limit.hdr.type = IF_SND_TAG_TYPE_RATE_LIMIT,
831 .rate_limit.hdr.flowid = 1,
832 .rate_limit.max_rate = COMMON_RATE,
833 .rate_limit.flags = M_NOWAIT,
837 params.rate_limit.hdr.flowtype = ((inp->inp_vflag & INP_IPV6) ?
838 M_HASHTYPE_RSS_TCP_IPV6 : M_HASHTYPE_RSS_TCP_IPV4);
840 params.rate_limit.hdr.flowtype = M_HASHTYPE_OPAQUE_HASH;
843 if (ifp->if_snd_tag_alloc) {
848 err = ifp->if_snd_tag_alloc(ifp, ¶ms, &tag);
850 /* Failed to setup a tag? */
856 tifp->if_snd_tag_free(tag);
860 static const struct tcp_hwrate_limit_table *
861 rt_setup_rate(struct inpcb *inp, struct ifnet *ifp, uint64_t bytes_per_sec,
862 uint32_t flags, int *error)
864 /* First lets find the interface if it exists */
865 const struct tcp_hwrate_limit_table *rte;
866 struct tcp_rate_set *rs;
867 struct epoch_tracker et;
870 epoch_enter_preempt(net_epoch_preempt, &et);
872 CK_LIST_FOREACH(rs, &int_rs, next) {
874 * Note we don't look with the lock since we either see a
875 * new entry or will get one when we try to add it.
877 if (rs->rs_flags & RS_IS_DEAD) {
878 /* The dead are not looked at */
881 if ((rs->rs_ifp == ifp) &&
882 (rs->rs_if_dunit == ifp->if_dunit)) {
888 (rs->rs_flags & RS_INTF_NO_SUP) ||
889 (rs->rs_flags & RS_IS_DEAD)) {
891 * This means we got a packet *before*
892 * the IF-UP was processed below, <or>
893 * while or after we already received an interface
894 * departed event. In either case we really don't
895 * want to do anything with pacing, in
896 * the departing case the packet is not
897 * going to go very far. The new case
898 * might be arguable, but its impossible
899 * to tell from the departing case.
901 if (rs->rs_disable && error)
903 epoch_exit_preempt(net_epoch_preempt, &et);
907 if ((rs == NULL) || (rs->rs_disable != 0)) {
908 if (rs->rs_disable && error)
910 epoch_exit_preempt(net_epoch_preempt, &et);
913 if (rs->rs_flags & RS_IS_DEFF) {
914 /* We need to find the real interface */
917 tifp = rt_find_real_interface(ifp, inp, error);
919 if (rs->rs_disable && error)
921 epoch_exit_preempt(net_epoch_preempt, &et);
924 goto use_real_interface;
926 if (rs->rs_flow_limit &&
927 ((rs->rs_flows_using + 1) > rs->rs_flow_limit)) {
930 epoch_exit_preempt(net_epoch_preempt, &et);
933 rte = tcp_find_suitable_rate(rs, bytes_per_sec, flags);
935 err = in_pcbattach_txrtlmt(inp, rs->rs_ifp,
941 /* Failed to attach */
949 * We use an atomic here for accounting so we don't have to
950 * use locks when freeing.
952 atomic_add_64(&rs->rs_flows_using, 1);
954 epoch_exit_preempt(net_epoch_preempt, &et);
959 tcp_rl_ifnet_link(void *arg __unused, struct ifnet *ifp, int link_state)
962 struct tcp_rate_set *rs;
964 if (((ifp->if_capabilities & IFCAP_TXRTLMT) == 0) ||
965 (link_state != LINK_STATE_UP)) {
967 * We only care on an interface going up that is rate-limit
973 CK_LIST_FOREACH(rs, &int_rs, next) {
974 if ((rs->rs_ifp == ifp) &&
975 (rs->rs_if_dunit == ifp->if_dunit)) {
976 /* We already have initialized this guy */
981 rt_setup_new_rs(ifp, &error);
986 tcp_rl_ifnet_departure(void *arg __unused, struct ifnet *ifp)
988 struct tcp_rate_set *rs, *nrs;
993 CK_LIST_FOREACH_SAFE(rs, &int_rs, next, nrs) {
994 if ((rs->rs_ifp == ifp) &&
995 (rs->rs_if_dunit == ifp->if_dunit)) {
996 CK_LIST_REMOVE(rs, next);
999 rs->rs_flags |= RS_IS_DEAD;
1000 for (i = 0; i < rs->rs_rate_cnt; i++) {
1001 if (rs->rs_rlt[i].flags & HDWRPACE_TAGPRESENT) {
1002 tifp = rs->rs_rlt[i].tag->ifp;
1003 in_pcbdetach_tag(tifp, rs->rs_rlt[i].tag);
1004 rs->rs_rlt[i].tag = NULL;
1006 rs->rs_rlt[i].flags = HDWRPACE_IFPDEPARTED;
1008 if (rs->rs_flows_using == 0) {
1010 * No references left, so we can schedule the
1011 * destruction after the epoch (with a caveat).
1013 rs->rs_flags |= RS_FUNERAL_SCHD;
1014 epoch_call(net_epoch, &rs->rs_epoch_ctx, rs_destroy);
1019 mtx_unlock(&rs_mtx);
1023 tcp_rl_shutdown(void *arg __unused, int howto __unused)
1025 struct tcp_rate_set *rs, *nrs;
1030 CK_LIST_FOREACH_SAFE(rs, &int_rs, next, nrs) {
1031 CK_LIST_REMOVE(rs, next);
1034 rs->rs_flags |= RS_IS_DEAD;
1035 for (i = 0; i < rs->rs_rate_cnt; i++) {
1036 if (rs->rs_rlt[i].flags & HDWRPACE_TAGPRESENT) {
1037 tifp = rs->rs_rlt[i].tag->ifp;
1038 in_pcbdetach_tag(tifp, rs->rs_rlt[i].tag);
1039 rs->rs_rlt[i].tag = NULL;
1041 rs->rs_rlt[i].flags = HDWRPACE_IFPDEPARTED;
1043 if (rs->rs_flows_using != 0) {
1045 * We dont hold a reference
1046 * so we have nothing left to
1051 * No references left, so we can destroy it
1054 rs->rs_flags |= RS_FUNERAL_SCHD;
1055 epoch_call(net_epoch, &rs->rs_epoch_ctx, rs_destroy);
1058 mtx_unlock(&rs_mtx);
1061 const struct tcp_hwrate_limit_table *
1062 tcp_set_pacing_rate(struct tcpcb *tp, struct ifnet *ifp,
1063 uint64_t bytes_per_sec, int flags, int *error)
1065 const struct tcp_hwrate_limit_table *rte;
1067 if (tp->t_inpcb->inp_snd_tag == NULL) {
1069 * We are setting up a rate for the first time.
1071 if ((ifp->if_capabilities & IFCAP_TXRTLMT) == 0) {
1072 /* Not supported by the egress */
1078 if (tp->t_inpcb->inp_socket->so_snd.sb_tls_flags & SB_TLS_IFNET) {
1080 * We currently can't do both TLS and hardware
1088 rte = rt_setup_rate(tp->t_inpcb, ifp, bytes_per_sec, flags, error);
1091 * We are modifying a rate, wrong interface?
1100 const struct tcp_hwrate_limit_table *
1101 tcp_chg_pacing_rate(const struct tcp_hwrate_limit_table *crte,
1102 struct tcpcb *tp, struct ifnet *ifp,
1103 uint64_t bytes_per_sec, int flags, int *error)
1105 const struct tcp_hwrate_limit_table *nrte;
1106 const struct tcp_rate_set *rs;
1107 int is_indirect = 0;
1111 if ((tp->t_inpcb->inp_snd_tag == NULL) ||
1113 /* Wrong interface */
1119 if ((rs->rs_flags & RS_IS_DEAD) ||
1120 (crte->flags & HDWRPACE_IFPDEPARTED)) {
1121 /* Release the rate, and try anew */
1123 tcp_rel_pacing_rate(crte, tp);
1124 nrte = tcp_set_pacing_rate(tp, ifp,
1125 bytes_per_sec, flags, error);
1128 if ((rs->rs_flags & RT_IS_INDIRECT ) == RT_IS_INDIRECT)
1132 if ((is_indirect == 0) &&
1133 ((ifp != rs->rs_ifp) ||
1134 (ifp->if_dunit != rs->rs_if_dunit))) {
1136 * Something changed, the user is not pointing to the same
1137 * ifp? Maybe a route updated on this guy?
1140 } else if (is_indirect) {
1142 * For indirect we have to dig in and find the real interface.
1146 rifp = rt_find_real_interface(ifp, tp->t_inpcb, error);
1148 /* Can't find it? */
1151 if ((rifp != rs->rs_ifp) ||
1152 (ifp->if_dunit != rs->rs_if_dunit)) {
1156 nrte = tcp_find_suitable_rate(rs, bytes_per_sec, flags);
1164 /* Release the old rate */
1165 tcp_rel_pacing_rate(crte, tp);
1168 /* Change rates to our new entry */
1169 err = in_pcbmodify_txrtlmt(tp->t_inpcb, nrte->rate);
1181 tcp_rel_pacing_rate(const struct tcp_hwrate_limit_table *crte, struct tcpcb *tp)
1183 const struct tcp_rate_set *crs;
1184 struct tcp_rate_set *rs;
1189 * Now we must break the const
1190 * in order to release our refcount.
1192 rs = __DECONST(struct tcp_rate_set *, crs);
1193 pre = atomic_fetchadd_64(&rs->rs_flows_using, -1);
1199 if ((rs->rs_flags & RS_IS_DEAD) &&
1200 ((rs->rs_flags & RS_FUNERAL_SCHD) == 0)){
1203 * and a funeral is not pending, so
1204 * we must schedule it.
1206 rs->rs_flags |= RS_FUNERAL_SCHD;
1207 epoch_call(net_epoch, &rs->rs_epoch_ctx, rs_destroy);
1209 mtx_unlock(&rs_mtx);
1211 in_pcbdetach_txrtlmt(tp->t_inpcb);
1214 static eventhandler_tag rl_ifnet_departs;
1215 static eventhandler_tag rl_ifnet_arrives;
1216 static eventhandler_tag rl_shutdown_start;
1219 tcp_rs_init(void *st __unused)
1221 CK_LIST_INIT(&int_rs);
1222 rs_number_alive = 0;
1223 rs_number_dead = 0;;
1224 mtx_init(&rs_mtx, "tcp_rs_mtx", "rsmtx", MTX_DEF);
1225 rl_ifnet_departs = EVENTHANDLER_REGISTER(ifnet_departure_event,
1226 tcp_rl_ifnet_departure,
1227 NULL, EVENTHANDLER_PRI_ANY);
1228 rl_ifnet_arrives = EVENTHANDLER_REGISTER(ifnet_link_event,
1230 NULL, EVENTHANDLER_PRI_ANY);
1231 rl_shutdown_start = EVENTHANDLER_REGISTER(shutdown_pre_sync,
1232 tcp_rl_shutdown, NULL,
1233 SHUTDOWN_PRI_FIRST);
1234 printf("TCP_ratelimit: Is now initialized\n");
1237 SYSINIT(tcp_rl_init, SI_SUB_SMP + 1, SI_ORDER_ANY, tcp_rs_init, NULL);