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>
48 #include <sys/sysctl.h>
49 #include <sys/eventhandler.h>
50 #include <sys/mutex.h>
52 #define TCPSTATES /* for logging */
53 #include <netinet/in.h>
54 #include <netinet/in_pcb.h>
55 #include <netinet/tcp_var.h>
57 #include <netinet6/tcp6_var.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 #ifndef ETHERNET_SEGMENT_SIZE
68 #define ETHERNET_SEGMENT_SIZE 1500
70 MALLOC_DEFINE(M_TCPPACE, "tcp_hwpace", "TCP Hardware pacing memory");
73 #define COMMON_RATE 180500
74 uint64_t desired_rates[] = {
76 180500, /* 1.44Mpbs */
87 12500000, /* 100Mbps */
88 25000000, /* 200Mbps */
89 50000000, /* 400Mbps */
90 100000000, /* 800Mbps */
98 10000000, /* 80Mbps */
99 18750000, /* 150Mbps */
100 20000000, /* 250Mbps */
101 37500000, /* 350Mbps */
102 62500000, /* 500Mbps */
103 78125000, /* 625Mbps */
104 125000000, /* 1Gbps */
106 #define MAX_HDWR_RATES (sizeof(desired_rates)/sizeof(uint64_t))
107 #define RS_ORDERED_COUNT 16 /*
108 * Number that are in order
109 * at the beginning of the table,
110 * over this a sort is required.
112 #define RS_NEXT_ORDER_GROUP 16 /*
113 * The point in our table where
114 * we come fill in a second ordered
115 * group (index wise means -1).
117 #define ALL_HARDWARE_RATES 1004 /*
118 * 1Meg - 1Gig in 1 Meg steps
119 * plus 100, 200k and 500k and
123 #define RS_ONE_MEGABIT_PERSEC 1000000
124 #define RS_ONE_GIGABIT_PERSEC 1000000000
125 #define RS_TEN_GIGABIT_PERSEC 10000000000
127 static struct head_tcp_rate_set int_rs;
128 static struct mtx rs_mtx;
129 uint32_t rs_number_alive;
130 uint32_t rs_number_dead;
132 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, rl, CTLFLAG_RW, 0,
133 "TCP Ratelimit stats");
134 SYSCTL_UINT(_net_inet_tcp_rl, OID_AUTO, alive, CTLFLAG_RW,
136 "Number of interfaces initialized for ratelimiting");
137 SYSCTL_UINT(_net_inet_tcp_rl, OID_AUTO, dead, CTLFLAG_RW,
139 "Number of interfaces departing from ratelimiting");
142 rl_add_syctl_entries(struct sysctl_oid *rl_sysctl_root, struct tcp_rate_set *rs)
145 * Add sysctl entries for thus interface.
147 if (rs->rs_flags & RS_INTF_NO_SUP) {
148 SYSCTL_ADD_S32(&rs->sysctl_ctx,
149 SYSCTL_CHILDREN(rl_sysctl_root),
150 OID_AUTO, "disable", CTLFLAG_RD,
152 "Disable this interface from new hdwr limiting?");
154 SYSCTL_ADD_S32(&rs->sysctl_ctx,
155 SYSCTL_CHILDREN(rl_sysctl_root),
156 OID_AUTO, "disable", CTLFLAG_RW,
158 "Disable this interface from new hdwr limiting?");
160 SYSCTL_ADD_S32(&rs->sysctl_ctx,
161 SYSCTL_CHILDREN(rl_sysctl_root),
162 OID_AUTO, "minseg", CTLFLAG_RW,
164 "What is the minimum we need to send on this interface?");
165 SYSCTL_ADD_U64(&rs->sysctl_ctx,
166 SYSCTL_CHILDREN(rl_sysctl_root),
167 OID_AUTO, "flow_limit", CTLFLAG_RW,
168 &rs->rs_flow_limit, 0,
169 "What is the limit for number of flows (0=unlimited)?");
170 SYSCTL_ADD_S32(&rs->sysctl_ctx,
171 SYSCTL_CHILDREN(rl_sysctl_root),
172 OID_AUTO, "highest", CTLFLAG_RD,
173 &rs->rs_highest_valid, 0,
174 "Highest valid rate");
175 SYSCTL_ADD_S32(&rs->sysctl_ctx,
176 SYSCTL_CHILDREN(rl_sysctl_root),
177 OID_AUTO, "lowest", CTLFLAG_RD,
178 &rs->rs_lowest_valid, 0,
179 "Lowest valid rate");
180 SYSCTL_ADD_S32(&rs->sysctl_ctx,
181 SYSCTL_CHILDREN(rl_sysctl_root),
182 OID_AUTO, "flags", CTLFLAG_RD,
184 "What lags are on the entry?");
185 SYSCTL_ADD_S32(&rs->sysctl_ctx,
186 SYSCTL_CHILDREN(rl_sysctl_root),
187 OID_AUTO, "numrates", CTLFLAG_RD,
189 "How many rates re there?");
190 SYSCTL_ADD_U64(&rs->sysctl_ctx,
191 SYSCTL_CHILDREN(rl_sysctl_root),
192 OID_AUTO, "flows_using", CTLFLAG_RD,
193 &rs->rs_flows_using, 0,
194 "How many flows are using this interface now?");
195 #ifdef DETAILED_RATELIMIT_SYSCTL
196 if (rs->rs_rlt && rs->rs_rate_cnt > 0) {
197 /* Lets display the rates */
199 struct sysctl_oid *rl_rates;
200 struct sysctl_oid *rl_rate_num;
202 rl_rates = SYSCTL_ADD_NODE(&rs->sysctl_ctx,
203 SYSCTL_CHILDREN(rl_sysctl_root),
208 for( i = 0; i < rs->rs_rate_cnt; i++) {
209 sprintf(rate_num, "%d", i);
210 rl_rate_num = SYSCTL_ADD_NODE(&rs->sysctl_ctx,
211 SYSCTL_CHILDREN(rl_rates),
216 SYSCTL_ADD_U32(&rs->sysctl_ctx,
217 SYSCTL_CHILDREN(rl_rate_num),
218 OID_AUTO, "flags", CTLFLAG_RD,
219 &rs->rs_rlt[i].flags, 0,
220 "Flags on this rate");
221 SYSCTL_ADD_U32(&rs->sysctl_ctx,
222 SYSCTL_CHILDREN(rl_rate_num),
223 OID_AUTO, "pacetime", CTLFLAG_RD,
224 &rs->rs_rlt[i].time_between, 0,
225 "Time hardware inserts between 1500 byte sends");
226 SYSCTL_ADD_U64(&rs->sysctl_ctx,
227 SYSCTL_CHILDREN(rl_rate_num),
228 OID_AUTO, "rate", CTLFLAG_RD,
229 &rs->rs_rlt[i].rate, 0,
230 "Rate in bytes per second");
237 rs_destroy(epoch_context_t ctx)
239 struct tcp_rate_set *rs;
242 rs = __containerof(ctx, struct tcp_rate_set, rs_epoch_ctx);
245 rs->rs_flags &= ~RS_FUNERAL_SCHD;
247 * In theory its possible (but unlikely)
248 * that while the delete was occuring
249 * and we were applying the DEAD flag
250 * someone slipped in and found the
251 * interface in a lookup. While we
252 * decided rs_flows_using were 0 and
253 * scheduling the epoch_call, the other
254 * thread incremented rs_flow_using. This
255 * is because users have a pointer and
256 * we only use the rs_flows_using in an
257 * atomic fashion, i.e. the other entities
258 * are not protected. To assure this did
259 * not occur, we check rs_flows_using here
262 do_free_rs = (rs->rs_flows_using == 0);
267 sysctl_ctx_free(&rs->sysctl_ctx);
268 free(rs->rs_rlt, M_TCPPACE);
274 rs_defer_destroy(struct tcp_rate_set *rs)
277 mtx_assert(&rs_mtx, MA_OWNED);
279 /* Check if already pending. */
280 if (rs->rs_flags & RS_FUNERAL_SCHD)
285 /* Set flag to only defer once. */
286 rs->rs_flags |= RS_FUNERAL_SCHD;
287 epoch_call(net_epoch, &rs->rs_epoch_ctx, rs_destroy);
291 extern counter_u64_t rate_limit_set_ok;
292 extern counter_u64_t rate_limit_active;
293 extern counter_u64_t rate_limit_alloc_fail;
297 rl_attach_txrtlmt(struct ifnet *ifp,
301 struct m_snd_tag **tag)
304 union if_snd_tag_alloc_params params = {
305 .rate_limit.hdr.type = IF_SND_TAG_TYPE_RATE_LIMIT,
306 .rate_limit.hdr.flowid = flowid,
307 .rate_limit.hdr.flowtype = flowtype,
308 .rate_limit.max_rate = cfg_rate,
309 .rate_limit.flags = M_NOWAIT,
312 if (ifp->if_snd_tag_alloc == NULL) {
315 error = ifp->if_snd_tag_alloc(ifp, ¶ms, tag);
319 counter_u64_add(rate_limit_set_ok, 1);
320 counter_u64_add(rate_limit_active, 1);
322 counter_u64_add(rate_limit_alloc_fail, 1);
329 populate_canned_table(struct tcp_rate_set *rs, const uint64_t *rate_table_act)
332 * The internal table is "special", it
333 * is two seperate ordered tables that
334 * must be merged. We get here when the
335 * adapter specifies a number of rates that
336 * covers both ranges in the table in some
339 int i, at_low, at_high;
340 uint8_t low_disabled = 0, high_disabled = 0;
342 for(i = 0, at_low = 0, at_high = RS_NEXT_ORDER_GROUP; i < rs->rs_rate_cnt; i++) {
343 rs->rs_rlt[i].flags = 0;
344 rs->rs_rlt[i].time_between = 0;
345 if ((low_disabled == 0) &&
347 (rate_table_act[at_low] < rate_table_act[at_high]))) {
348 rs->rs_rlt[i].rate = rate_table_act[at_low];
350 if (at_low == RS_NEXT_ORDER_GROUP)
352 } else if (high_disabled == 0) {
353 rs->rs_rlt[i].rate = rate_table_act[at_high];
355 if (at_high == MAX_HDWR_RATES)
361 static struct tcp_rate_set *
362 rt_setup_new_rs(struct ifnet *ifp, int *error)
364 struct tcp_rate_set *rs;
365 const uint64_t *rate_table_act;
366 uint64_t lentim, res;
370 struct if_ratelimit_query_results rl;
371 struct sysctl_oid *rl_sysctl_root;
373 * We expect to enter with the
377 if (ifp->if_ratelimit_query == NULL) {
379 * We can do nothing if we cannot
380 * get a query back from the driver.
384 rs = malloc(sizeof(struct tcp_rate_set), M_TCPPACE, M_NOWAIT | M_ZERO);
390 rl.flags = RT_NOSUPPORT;
391 ifp->if_ratelimit_query(ifp, &rl);
392 if (rl.flags & RT_IS_UNUSABLE) {
394 * The interface does not really support
397 memset(rs, 0, sizeof(struct tcp_rate_set));
399 rs->rs_if_dunit = ifp->if_dunit;
400 rs->rs_flags = RS_INTF_NO_SUP;
403 sysctl_ctx_init(&rs->sysctl_ctx);
404 rl_sysctl_root = SYSCTL_ADD_NODE(&rs->sysctl_ctx,
405 SYSCTL_STATIC_CHILDREN(_net_inet_tcp_rl),
407 rs->rs_ifp->if_xname,
410 rl_add_syctl_entries(rl_sysctl_root, rs);
412 CK_LIST_INSERT_HEAD(&int_rs, rs, next);
415 } else if ((rl.flags & RT_IS_INDIRECT) == RT_IS_INDIRECT) {
416 memset(rs, 0, sizeof(struct tcp_rate_set));
418 rs->rs_if_dunit = ifp->if_dunit;
419 rs->rs_flags = RS_IS_DEFF;
421 sysctl_ctx_init(&rs->sysctl_ctx);
422 rl_sysctl_root = SYSCTL_ADD_NODE(&rs->sysctl_ctx,
423 SYSCTL_STATIC_CHILDREN(_net_inet_tcp_rl),
425 rs->rs_ifp->if_xname,
428 rl_add_syctl_entries(rl_sysctl_root, rs);
430 CK_LIST_INSERT_HEAD(&int_rs, rs, next);
433 } else if ((rl.flags & RT_IS_FIXED_TABLE) == RT_IS_FIXED_TABLE) {
434 /* Mellanox most likely */
436 rs->rs_if_dunit = ifp->if_dunit;
437 rs->rs_rate_cnt = rl.number_of_rates;
438 rs->rs_min_seg = rl.min_segment_burst;
439 rs->rs_highest_valid = 0;
440 rs->rs_flow_limit = rl.max_flows;
441 rs->rs_flags = RS_IS_INTF | RS_NO_PRE;
443 rate_table_act = rl.rate_table;
444 } else if ((rl.flags & RT_IS_SELECTABLE) == RT_IS_SELECTABLE) {
447 rs->rs_if_dunit = ifp->if_dunit;
448 rs->rs_rate_cnt = rl.number_of_rates;
449 rs->rs_min_seg = rl.min_segment_burst;
451 rs->rs_flow_limit = rl.max_flows;
452 rate_table_act = desired_rates;
453 if ((rs->rs_rate_cnt > MAX_HDWR_RATES) &&
454 (rs->rs_rate_cnt < ALL_HARDWARE_RATES)) {
456 * Our desired table is not big
457 * enough, do what we can.
459 rs->rs_rate_cnt = MAX_HDWR_RATES;
461 if (rs->rs_rate_cnt <= RS_ORDERED_COUNT)
462 rs->rs_flags = RS_IS_INTF;
464 rs->rs_flags = RS_IS_INTF | RS_INT_TBL;
465 if (rs->rs_rate_cnt >= ALL_HARDWARE_RATES)
466 rs->rs_rate_cnt = ALL_HARDWARE_RATES;
468 printf("Interface:%s unit:%d not one known to have rate-limits\n",
474 sz = sizeof(struct tcp_hwrate_limit_table) * rs->rs_rate_cnt;
475 rs->rs_rlt = malloc(sz, M_TCPPACE, M_NOWAIT);
476 if (rs->rs_rlt == NULL) {
483 if (rs->rs_rate_cnt >= ALL_HARDWARE_RATES) {
485 * The interface supports all
486 * the rates we could possibly want.
490 rs->rs_rlt[0].rate = 12500; /* 100k */
491 rs->rs_rlt[1].rate = 25000; /* 200k */
492 rs->rs_rlt[2].rate = 62500; /* 500k */
493 /* Note 125000 == 1Megabit
494 * populate 1Meg - 1000meg.
496 for(i = 3, rat = 125000; i< (ALL_HARDWARE_RATES-1); i++) {
497 rs->rs_rlt[i].rate = rat;
500 rs->rs_rlt[(ALL_HARDWARE_RATES-1)].rate = 1250000000;
501 } else if (rs->rs_flags & RS_INT_TBL) {
502 /* We populate this in a special way */
503 populate_canned_table(rs, rate_table_act);
506 * Just copy in the rates from
507 * the table, it is in order.
509 for (i=0; i<rs->rs_rate_cnt; i++) {
510 rs->rs_rlt[i].rate = rate_table_act[i];
511 rs->rs_rlt[i].time_between = 0;
512 rs->rs_rlt[i].flags = 0;
515 for (i = (rs->rs_rate_cnt - 1); i >= 0; i--) {
517 * We go backwards through the list so that if we can't get
518 * a rate and fail to init one, we have at least a chance of
519 * getting the highest one.
521 rs->rs_rlt[i].ptbl = rs;
522 rs->rs_rlt[i].tag = NULL;
524 * Calculate the time between.
526 lentim = ETHERNET_SEGMENT_SIZE * USECS_IN_SECOND;
527 res = lentim / rs->rs_rlt[i].rate;
529 rs->rs_rlt[i].time_between = res;
531 rs->rs_rlt[i].time_between = 1;
532 if (rs->rs_flags & RS_NO_PRE) {
533 rs->rs_rlt[i].flags = HDWRPACE_INITED;
534 rs->rs_lowest_valid = i;
538 hash_type = M_HASHTYPE_RSS_TCP_IPV4;
540 hash_type = M_HASHTYPE_OPAQUE_HASH;
542 err = rl_attach_txrtlmt(ifp,
548 if (i == (rs->rs_rate_cnt - 1)) {
550 * Huh - first rate and we can't get
553 free(rs->rs_rlt, M_TCPPACE);
563 rs->rs_rlt[i].flags = HDWRPACE_INITED | HDWRPACE_TAGPRESENT;
564 rs->rs_lowest_valid = i;
568 /* Did we get at least 1 rate? */
569 if (rs->rs_rlt[(rs->rs_rate_cnt - 1)].flags & HDWRPACE_INITED)
570 rs->rs_highest_valid = rs->rs_rate_cnt - 1;
572 free(rs->rs_rlt, M_TCPPACE);
576 sysctl_ctx_init(&rs->sysctl_ctx);
577 rl_sysctl_root = SYSCTL_ADD_NODE(&rs->sysctl_ctx,
578 SYSCTL_STATIC_CHILDREN(_net_inet_tcp_rl),
580 rs->rs_ifp->if_xname,
583 rl_add_syctl_entries(rl_sysctl_root, rs);
585 CK_LIST_INSERT_HEAD(&int_rs, rs, next);
590 static const struct tcp_hwrate_limit_table *
591 tcp_int_find_suitable_rate(const struct tcp_rate_set *rs,
592 uint64_t bytes_per_sec, uint32_t flags)
594 struct tcp_hwrate_limit_table *arte = NULL, *rte = NULL;
595 uint64_t mbits_per_sec, ind_calc;
598 mbits_per_sec = (bytes_per_sec * 8);
599 if (flags & RS_PACING_LT) {
600 if ((mbits_per_sec < RS_ONE_MEGABIT_PERSEC) &&
601 (rs->rs_lowest_valid <= 2)){
603 * Smaller than 1Meg, only
604 * 3 entries can match it.
606 for(i = rs->rs_lowest_valid; i < 3; i++) {
607 if (bytes_per_sec <= rs->rs_rlt[i].rate) {
608 rte = &rs->rs_rlt[i];
610 } else if (rs->rs_rlt[i].flags & HDWRPACE_INITED) {
611 arte = &rs->rs_rlt[i];
615 } else if ((mbits_per_sec > RS_ONE_GIGABIT_PERSEC) &&
616 (rs->rs_rlt[(ALL_HARDWARE_RATES-1)].flags & HDWRPACE_INITED)){
618 * Larger than 1G (the majority of
621 if (mbits_per_sec < RS_TEN_GIGABIT_PERSEC)
622 rte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
624 arte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
628 * If we reach here its in our table (between 1Meg - 1000Meg),
629 * just take the rounded down mbits per second, and add
630 * 1Megabit to it, from this we can calculate
631 * the index in the table.
633 ind_calc = mbits_per_sec/RS_ONE_MEGABIT_PERSEC;
634 if ((ind_calc * RS_ONE_MEGABIT_PERSEC) != mbits_per_sec)
636 /* our table is offset by 3, we add 2 */
638 if (ind_calc > (ALL_HARDWARE_RATES-1)) {
639 /* This should not happen */
640 ind_calc = ALL_HARDWARE_RATES-1;
642 if ((ind_calc >= rs->rs_lowest_valid) &&
643 (ind_calc <= rs->rs_highest_valid))
644 rte = &rs->rs_rlt[ind_calc];
645 } else if (flags & RS_PACING_EXACT_MATCH) {
646 if ((mbits_per_sec < RS_ONE_MEGABIT_PERSEC) &&
647 (rs->rs_lowest_valid <= 2)){
648 for(i = rs->rs_lowest_valid; i < 3; i++) {
649 if (bytes_per_sec == rs->rs_rlt[i].rate) {
650 rte = &rs->rs_rlt[i];
654 } else if ((mbits_per_sec > RS_ONE_GIGABIT_PERSEC) &&
655 (rs->rs_rlt[(ALL_HARDWARE_RATES-1)].flags & HDWRPACE_INITED)) {
656 /* > 1Gbps only one rate */
657 if (bytes_per_sec == rs->rs_rlt[(ALL_HARDWARE_RATES-1)].rate) {
659 rte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
662 /* Ok it must be a exact meg (its between 1G and 1Meg) */
663 ind_calc = mbits_per_sec/RS_ONE_MEGABIT_PERSEC;
664 if ((ind_calc * RS_ONE_MEGABIT_PERSEC) == mbits_per_sec) {
665 /* its an exact Mbps */
667 if (ind_calc > (ALL_HARDWARE_RATES-1)) {
668 /* This should not happen */
669 ind_calc = ALL_HARDWARE_RATES-1;
671 if (rs->rs_rlt[ind_calc].flags & HDWRPACE_INITED)
672 rte = &rs->rs_rlt[ind_calc];
676 /* we want greater than the requested rate */
677 if ((mbits_per_sec < RS_ONE_MEGABIT_PERSEC) &&
678 (rs->rs_lowest_valid <= 2)){
679 arte = &rs->rs_rlt[3]; /* set alternate to 1Meg */
680 for (i=2; i>=rs->rs_lowest_valid; i--) {
681 if (bytes_per_sec < rs->rs_rlt[i].rate) {
682 rte = &rs->rs_rlt[i];
684 } else if ((flags & RS_PACING_GEQ) &&
685 (bytes_per_sec == rs->rs_rlt[i].rate)) {
686 rte = &rs->rs_rlt[i];
689 arte = &rs->rs_rlt[i]; /* new alternate */
692 } else if (mbits_per_sec > RS_ONE_GIGABIT_PERSEC) {
693 if ((bytes_per_sec < rs->rs_rlt[(ALL_HARDWARE_RATES-1)].rate) &&
694 (rs->rs_rlt[(ALL_HARDWARE_RATES-1)].flags & HDWRPACE_INITED)){
695 /* Our top rate is larger than the request */
696 rte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
697 } else if ((flags & RS_PACING_GEQ) &&
698 (bytes_per_sec == rs->rs_rlt[(ALL_HARDWARE_RATES-1)].rate) &&
699 (rs->rs_rlt[(ALL_HARDWARE_RATES-1)].flags & HDWRPACE_INITED)) {
700 /* It matches our top rate */
701 rte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
702 } else if (rs->rs_rlt[(ALL_HARDWARE_RATES-1)].flags & HDWRPACE_INITED) {
703 /* The top rate is an alternative */
704 arte = &rs->rs_rlt[(ALL_HARDWARE_RATES-1)];
707 /* Its in our range 1Meg - 1Gig */
708 if (flags & RS_PACING_GEQ) {
709 ind_calc = mbits_per_sec/RS_ONE_MEGABIT_PERSEC;
710 if ((ind_calc * RS_ONE_MEGABIT_PERSEC) == mbits_per_sec) {
711 if (ind_calc > (ALL_HARDWARE_RATES-1)) {
712 /* This should not happen */
713 ind_calc = (ALL_HARDWARE_RATES-1);
715 rte = &rs->rs_rlt[ind_calc];
719 ind_calc = (mbits_per_sec + (RS_ONE_MEGABIT_PERSEC-1))/RS_ONE_MEGABIT_PERSEC;
721 if (ind_calc > (ALL_HARDWARE_RATES-1)) {
722 /* This should not happen */
723 ind_calc = ALL_HARDWARE_RATES-1;
725 if (rs->rs_rlt[ind_calc].flags & HDWRPACE_INITED)
726 rte = &rs->rs_rlt[ind_calc];
732 (flags & RS_PACING_SUB_OK)) {
733 /* We can use the substitute */
739 static const struct tcp_hwrate_limit_table *
740 tcp_find_suitable_rate(const struct tcp_rate_set *rs, uint64_t bytes_per_sec, uint32_t flags)
743 * Hunt the rate table with the restrictions in flags and find a
744 * suitable rate if possible.
745 * RS_PACING_EXACT_MATCH - look for an exact match to rate.
746 * RS_PACING_GT - must be greater than.
747 * RS_PACING_GEQ - must be greater than or equal.
748 * RS_PACING_LT - must be less than.
749 * RS_PACING_SUB_OK - If we don't meet criteria a
753 struct tcp_hwrate_limit_table *rte = NULL;
756 if ((rs->rs_flags & RS_INT_TBL) &&
757 (rs->rs_rate_cnt >= ALL_HARDWARE_RATES)) {
759 * Here we don't want to paw thru
760 * a big table, we have everything
761 * from 1Meg - 1000Meg in 1Meg increments.
762 * Use an alternate method to "lookup".
764 return (tcp_int_find_suitable_rate(rs, bytes_per_sec, flags));
766 if ((flags & RS_PACING_LT) ||
767 (flags & RS_PACING_EXACT_MATCH)) {
769 * For exact and less than we go forward through the table.
770 * This way when we find one larger we stop (exact was a
773 for (i = rs->rs_lowest_valid, matched = 0; i <= rs->rs_highest_valid; i++) {
774 if ((flags & RS_PACING_EXACT_MATCH) &&
775 (bytes_per_sec == rs->rs_rlt[i].rate)) {
776 rte = &rs->rs_rlt[i];
779 } else if ((flags & RS_PACING_LT) &&
780 (bytes_per_sec <= rs->rs_rlt[i].rate)) {
781 rte = &rs->rs_rlt[i];
785 if (bytes_per_sec > rs->rs_rlt[i].rate)
788 if ((matched == 0) &&
789 (flags & RS_PACING_LT) &&
790 (flags & RS_PACING_SUB_OK)) {
791 /* Kick in a substitute (the lowest) */
792 rte = &rs->rs_rlt[rs->rs_lowest_valid];
796 * Here we go backward through the table so that we can find
797 * the one greater in theory faster (but its probably a
800 for (i = rs->rs_highest_valid, matched = 0; i >= rs->rs_lowest_valid; i--) {
801 if (rs->rs_rlt[i].rate > bytes_per_sec) {
802 /* A possible candidate */
803 rte = &rs->rs_rlt[i];
805 if ((flags & RS_PACING_GEQ) &&
806 (bytes_per_sec == rs->rs_rlt[i].rate)) {
807 /* An exact match and we want equal */
809 rte = &rs->rs_rlt[i];
813 * Found one that is larger than but don't
814 * stop, there may be a more closer match.
818 if (rs->rs_rlt[i].rate < bytes_per_sec) {
820 * We found a table entry that is smaller,
821 * stop there will be none greater or equal.
826 if ((matched == 0) &&
827 (flags & RS_PACING_SUB_OK)) {
828 /* Kick in a substitute (the highest) */
829 rte = &rs->rs_rlt[rs->rs_highest_valid];
835 static struct ifnet *
836 rt_find_real_interface(struct ifnet *ifp, struct inpcb *inp, int *error)
839 struct m_snd_tag *tag;
840 union if_snd_tag_alloc_params params = {
841 .rate_limit.hdr.type = IF_SND_TAG_TYPE_RATE_LIMIT,
842 .rate_limit.hdr.flowid = 1,
843 .rate_limit.max_rate = COMMON_RATE,
844 .rate_limit.flags = M_NOWAIT,
848 params.rate_limit.hdr.flowtype = ((inp->inp_vflag & INP_IPV6) ?
849 M_HASHTYPE_RSS_TCP_IPV6 : M_HASHTYPE_RSS_TCP_IPV4);
851 params.rate_limit.hdr.flowtype = M_HASHTYPE_OPAQUE_HASH;
854 if (ifp->if_snd_tag_alloc) {
859 err = ifp->if_snd_tag_alloc(ifp, ¶ms, &tag);
861 /* Failed to setup a tag? */
867 tifp->if_snd_tag_free(tag);
871 static const struct tcp_hwrate_limit_table *
872 rt_setup_rate(struct inpcb *inp, struct ifnet *ifp, uint64_t bytes_per_sec,
873 uint32_t flags, int *error)
875 /* First lets find the interface if it exists */
876 const struct tcp_hwrate_limit_table *rte;
877 struct tcp_rate_set *rs;
878 struct epoch_tracker et;
881 epoch_enter_preempt(net_epoch_preempt, &et);
883 CK_LIST_FOREACH(rs, &int_rs, next) {
885 * Note we don't look with the lock since we either see a
886 * new entry or will get one when we try to add it.
888 if (rs->rs_flags & RS_IS_DEAD) {
889 /* The dead are not looked at */
892 if ((rs->rs_ifp == ifp) &&
893 (rs->rs_if_dunit == ifp->if_dunit)) {
899 (rs->rs_flags & RS_INTF_NO_SUP) ||
900 (rs->rs_flags & RS_IS_DEAD)) {
902 * This means we got a packet *before*
903 * the IF-UP was processed below, <or>
904 * while or after we already received an interface
905 * departed event. In either case we really don't
906 * want to do anything with pacing, in
907 * the departing case the packet is not
908 * going to go very far. The new case
909 * might be arguable, but its impossible
910 * to tell from the departing case.
912 if (rs->rs_disable && error)
914 epoch_exit_preempt(net_epoch_preempt, &et);
918 if ((rs == NULL) || (rs->rs_disable != 0)) {
919 if (rs->rs_disable && error)
921 epoch_exit_preempt(net_epoch_preempt, &et);
924 if (rs->rs_flags & RS_IS_DEFF) {
925 /* We need to find the real interface */
928 tifp = rt_find_real_interface(ifp, inp, error);
930 if (rs->rs_disable && error)
932 epoch_exit_preempt(net_epoch_preempt, &et);
935 goto use_real_interface;
937 if (rs->rs_flow_limit &&
938 ((rs->rs_flows_using + 1) > rs->rs_flow_limit)) {
941 epoch_exit_preempt(net_epoch_preempt, &et);
944 rte = tcp_find_suitable_rate(rs, bytes_per_sec, flags);
946 err = in_pcbattach_txrtlmt(inp, rs->rs_ifp,
952 /* Failed to attach */
960 * We use an atomic here for accounting so we don't have to
961 * use locks when freeing.
963 atomic_add_64(&rs->rs_flows_using, 1);
965 epoch_exit_preempt(net_epoch_preempt, &et);
970 tcp_rl_ifnet_link(void *arg __unused, struct ifnet *ifp, int link_state)
973 struct tcp_rate_set *rs;
975 if (((ifp->if_capabilities & IFCAP_TXRTLMT) == 0) ||
976 (link_state != LINK_STATE_UP)) {
978 * We only care on an interface going up that is rate-limit
984 CK_LIST_FOREACH(rs, &int_rs, next) {
985 if ((rs->rs_ifp == ifp) &&
986 (rs->rs_if_dunit == ifp->if_dunit)) {
987 /* We already have initialized this guy */
993 rt_setup_new_rs(ifp, &error);
997 tcp_rl_ifnet_departure(void *arg __unused, struct ifnet *ifp)
999 struct tcp_rate_set *rs, *nrs;
1004 CK_LIST_FOREACH_SAFE(rs, &int_rs, next, nrs) {
1005 if ((rs->rs_ifp == ifp) &&
1006 (rs->rs_if_dunit == ifp->if_dunit)) {
1007 CK_LIST_REMOVE(rs, next);
1009 rs->rs_flags |= RS_IS_DEAD;
1010 for (i = 0; i < rs->rs_rate_cnt; i++) {
1011 if (rs->rs_rlt[i].flags & HDWRPACE_TAGPRESENT) {
1012 tifp = rs->rs_rlt[i].tag->ifp;
1013 in_pcbdetach_tag(tifp, rs->rs_rlt[i].tag);
1014 rs->rs_rlt[i].tag = NULL;
1016 rs->rs_rlt[i].flags = HDWRPACE_IFPDEPARTED;
1018 if (rs->rs_flows_using == 0)
1019 rs_defer_destroy(rs);
1023 mtx_unlock(&rs_mtx);
1027 tcp_rl_shutdown(void *arg __unused, int howto __unused)
1029 struct tcp_rate_set *rs, *nrs;
1034 CK_LIST_FOREACH_SAFE(rs, &int_rs, next, nrs) {
1035 CK_LIST_REMOVE(rs, next);
1037 rs->rs_flags |= RS_IS_DEAD;
1038 for (i = 0; i < rs->rs_rate_cnt; i++) {
1039 if (rs->rs_rlt[i].flags & HDWRPACE_TAGPRESENT) {
1040 tifp = rs->rs_rlt[i].tag->ifp;
1041 in_pcbdetach_tag(tifp, rs->rs_rlt[i].tag);
1042 rs->rs_rlt[i].tag = NULL;
1044 rs->rs_rlt[i].flags = HDWRPACE_IFPDEPARTED;
1046 if (rs->rs_flows_using == 0)
1047 rs_defer_destroy(rs);
1049 mtx_unlock(&rs_mtx);
1052 const struct tcp_hwrate_limit_table *
1053 tcp_set_pacing_rate(struct tcpcb *tp, struct ifnet *ifp,
1054 uint64_t bytes_per_sec, int flags, int *error)
1056 const struct tcp_hwrate_limit_table *rte;
1058 if (tp->t_inpcb->inp_snd_tag == NULL) {
1060 * We are setting up a rate for the first time.
1062 if ((ifp->if_capabilities & IFCAP_TXRTLMT) == 0) {
1063 /* Not supported by the egress */
1069 if (tp->t_inpcb->inp_socket->so_snd.sb_flags & SB_TLS_IFNET) {
1071 * We currently can't do both TLS and hardware
1079 rte = rt_setup_rate(tp->t_inpcb, ifp, bytes_per_sec, flags, error);
1082 * We are modifying a rate, wrong interface?
1091 const struct tcp_hwrate_limit_table *
1092 tcp_chg_pacing_rate(const struct tcp_hwrate_limit_table *crte,
1093 struct tcpcb *tp, struct ifnet *ifp,
1094 uint64_t bytes_per_sec, int flags, int *error)
1096 const struct tcp_hwrate_limit_table *nrte;
1097 const struct tcp_rate_set *rs;
1098 int is_indirect = 0;
1102 if ((tp->t_inpcb->inp_snd_tag == NULL) ||
1104 /* Wrong interface */
1110 if ((rs->rs_flags & RS_IS_DEAD) ||
1111 (crte->flags & HDWRPACE_IFPDEPARTED)) {
1112 /* Release the rate, and try anew */
1114 tcp_rel_pacing_rate(crte, tp);
1115 nrte = tcp_set_pacing_rate(tp, ifp,
1116 bytes_per_sec, flags, error);
1119 if ((rs->rs_flags & RT_IS_INDIRECT ) == RT_IS_INDIRECT)
1123 if ((is_indirect == 0) &&
1124 ((ifp != rs->rs_ifp) ||
1125 (ifp->if_dunit != rs->rs_if_dunit))) {
1127 * Something changed, the user is not pointing to the same
1128 * ifp? Maybe a route updated on this guy?
1131 } else if (is_indirect) {
1133 * For indirect we have to dig in and find the real interface.
1137 rifp = rt_find_real_interface(ifp, tp->t_inpcb, error);
1139 /* Can't find it? */
1142 if ((rifp != rs->rs_ifp) ||
1143 (ifp->if_dunit != rs->rs_if_dunit)) {
1147 nrte = tcp_find_suitable_rate(rs, bytes_per_sec, flags);
1155 /* Release the old rate */
1156 tcp_rel_pacing_rate(crte, tp);
1159 /* Change rates to our new entry */
1160 err = in_pcbmodify_txrtlmt(tp->t_inpcb, nrte->rate);
1172 tcp_rel_pacing_rate(const struct tcp_hwrate_limit_table *crte, struct tcpcb *tp)
1174 const struct tcp_rate_set *crs;
1175 struct tcp_rate_set *rs;
1180 * Now we must break the const
1181 * in order to release our refcount.
1183 rs = __DECONST(struct tcp_rate_set *, crs);
1184 pre = atomic_fetchadd_64(&rs->rs_flows_using, -1);
1190 if (rs->rs_flags & RS_IS_DEAD)
1191 rs_defer_destroy(rs);
1192 mtx_unlock(&rs_mtx);
1194 in_pcbdetach_txrtlmt(tp->t_inpcb);
1197 static eventhandler_tag rl_ifnet_departs;
1198 static eventhandler_tag rl_ifnet_arrives;
1199 static eventhandler_tag rl_shutdown_start;
1202 tcp_rs_init(void *st __unused)
1204 CK_LIST_INIT(&int_rs);
1205 rs_number_alive = 0;
1206 rs_number_dead = 0;;
1207 mtx_init(&rs_mtx, "tcp_rs_mtx", "rsmtx", MTX_DEF);
1208 rl_ifnet_departs = EVENTHANDLER_REGISTER(ifnet_departure_event,
1209 tcp_rl_ifnet_departure,
1210 NULL, EVENTHANDLER_PRI_ANY);
1211 rl_ifnet_arrives = EVENTHANDLER_REGISTER(ifnet_link_event,
1213 NULL, EVENTHANDLER_PRI_ANY);
1214 rl_shutdown_start = EVENTHANDLER_REGISTER(shutdown_pre_sync,
1215 tcp_rl_shutdown, NULL,
1216 SHUTDOWN_PRI_FIRST);
1217 printf("TCP_ratelimit: Is now initialized\n");
1220 SYSINIT(tcp_rl_init, SI_SUB_SMP + 1, SI_ORDER_ANY, tcp_rs_init, NULL);