xref: /freebsd/sys/netinet/tcp_ratelimit.h (revision a3557ef0)
1 /*-
2  *
3  * SPDX-License-Identifier: BSD-3-Clause
4  *
5  * Copyright (c) 2018-2020
6  *	Netflix Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  * __FBSDID("$FreeBSD$");
29  *
30  */
31 /**
32  * Author: Randall Stewart <rrs@netflix.com>
33  */
34 #ifndef __tcp_ratelimit_h__
35 #define __tcp_ratelimit_h__
36 
37 struct m_snd_tag;
38 
39 /* Flags on an individual rate */
40 #define HDWRPACE_INITED 	0x0001
41 #define HDWRPACE_TAGPRESENT	0x0002
42 #define HDWRPACE_IFPDEPARTED	0x0004
43 struct tcp_hwrate_limit_table {
44 	const struct tcp_rate_set *ptbl;	/* Pointer to parent table */
45 	struct m_snd_tag *tag;	/* Send tag if needed (chelsio) */
46 	uint64_t rate;		/* Rate we get in Bytes per second (Bps) */
47 	uint32_t time_between;	/* Time-Gap between packets at this rate */
48 	uint32_t flags;
49 };
50 
51 /* Rateset flags */
52 #define RS_IS_DEFF      0x0001	/* Its a lagg, do a double lookup */
53 #define RS_IS_INTF      0x0002	/* Its a plain interface */
54 #define RS_NO_PRE       0x0004	/* The interfacd has set rates */
55 #define RS_INT_TBL      0x0010	/*
56 				 * The table is the internal version
57 				 * which has special setup requirements.
58 				 */
59 #define RS_IS_DEAD      0x0020	/* The RS is dead list */
60 #define RS_FUNERAL_SCHD 0x0040  /* Is a epoch call scheduled to bury this guy?*/
61 #define RS_INTF_NO_SUP  0x0100 	/* The interface does not support the ratelimiting */
62 
63 struct tcp_rate_set {
64 	struct sysctl_ctx_list sysctl_ctx;
65 	CK_LIST_ENTRY(tcp_rate_set) next;
66 	struct ifnet *rs_ifp;
67 	struct tcp_hwrate_limit_table *rs_rlt;
68 	uint64_t rs_flows_using;
69 	uint64_t rs_flow_limit;
70 	uint32_t rs_if_dunit;
71 	int rs_rate_cnt;
72 	int rs_min_seg;
73 	int rs_highest_valid;
74 	int rs_lowest_valid;
75 	int rs_disable;
76 	int rs_flags;
77 	struct epoch_context rs_epoch_ctx;
78 };
79 
80 CK_LIST_HEAD(head_tcp_rate_set, tcp_rate_set);
81 
82 /* Request flags */
83 #define RS_PACING_EXACT_MATCH	0x0001	/* Need an exact match for rate */
84 #define RS_PACING_GT		0x0002	/* Greater than requested */
85 #define RS_PACING_GEQ		0x0004	/* Greater than or equal too */
86 #define RS_PACING_LT		0x0008	/* Less than requested rate */
87 #define RS_PACING_SUB_OK	0x0010	/* If a rate can't be found get the
88 					 * next best rate (highest or lowest). */
89 #ifdef _KERNEL
90 #ifndef ETHERNET_SEGMENT_SIZE
91 #define ETHERNET_SEGMENT_SIZE 1514
92 #endif
93 #ifdef RATELIMIT
94 #define DETAILED_RATELIMIT_SYSCTL 1	/*
95 					 * Undefine this if you don't want
96 					 * detailed rates to appear in
97 					 * net.inet.tcp.rl.
98 					 * With the defintion each rate
99 					 * shows up in your sysctl tree
100 					 * this can be big.
101 					 */
102 
103 const struct tcp_hwrate_limit_table *
104 tcp_set_pacing_rate(struct tcpcb *tp, struct ifnet *ifp,
105     uint64_t bytes_per_sec, int flags, int *error);
106 
107 const struct tcp_hwrate_limit_table *
108 tcp_chg_pacing_rate(const struct tcp_hwrate_limit_table *crte,
109     struct tcpcb *tp, struct ifnet *ifp,
110     uint64_t bytes_per_sec, int flags, int *error);
111 void
112 tcp_rel_pacing_rate(const struct tcp_hwrate_limit_table *crte,
113     struct tcpcb *tp);
114 #else
115 static inline const struct tcp_hwrate_limit_table *
116 tcp_set_pacing_rate(struct tcpcb *tp, struct ifnet *ifp,
117     uint64_t bytes_per_sec, int flags, int *error)
118 {
119 	if (error)
120 		*error = EOPNOTSUPP;
121 	return (NULL);
122 }
123 
124 static inline const struct tcp_hwrate_limit_table *
125 tcp_chg_pacing_rate(const struct tcp_hwrate_limit_table *crte,
126     struct tcpcb *tp, struct ifnet *ifp,
127     uint64_t bytes_per_sec, int flags, int *error)
128 {
129 	if (error)
130 		*error = EOPNOTSUPP;
131 	return (NULL);
132 }
133 
134 static inline void
135 tcp_rel_pacing_rate(const struct tcp_hwrate_limit_table *crte,
136     struct tcpcb *tp)
137 {
138 	return;
139 }
140 #endif
141 /*
142  * Given a b/w and a segsiz, and optional hardware
143  * rate limit, return the ideal size to burst
144  * out at once. Note the parameter can_use_1mss
145  * dictates if the transport will tolerate a 1mss
146  * limit, if not it will bottom out at 2mss (think
147  * delayed ack).
148  */
149 uint32_t
150 tcp_get_pacing_burst_size(uint64_t bw, uint32_t segsiz, int can_use_1mss,
151    const struct tcp_hwrate_limit_table *te, int *err);
152 
153 #endif
154 #endif
155