aboutsummaryrefslogtreecommitdiff
path: root/sys/netinet/tcp_ratelimit.h
blob: 8747708e8b5e857e92a7c83e03628ec6ca19e63b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*-
 *
 * SPDX-License-Identifier: BSD-3-Clause
 *
 * Copyright (c) 2018-2020
 *	Netflix Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * __FBSDID("$FreeBSD$");
 *
 */
/**
 * Author: Randall Stewart <rrs@netflix.com>
 */
#ifndef __tcp_ratelimit_h__
#define __tcp_ratelimit_h__

struct m_snd_tag;

/* Flags on an individual rate */
#define HDWRPACE_INITED 	0x0001
#define HDWRPACE_TAGPRESENT	0x0002
#define HDWRPACE_IFPDEPARTED	0x0004
struct tcp_hwrate_limit_table {
	const struct tcp_rate_set *ptbl;	/* Pointer to parent table */
	struct m_snd_tag *tag;	/* Send tag if needed (chelsio) */
	long	 rate;		/* Rate we get in Bytes per second (Bps) */
	long	 using;		/* How many flows are using this hdwr rate. */
	long	 rs_num_enobufs;
	uint32_t time_between;	/* Time-Gap between packets at this rate */
	uint32_t flags;
};

/* Rateset flags */
#define RS_IS_DEFF      0x0001	/* Its a lagg, do a double lookup */
#define RS_IS_INTF      0x0002	/* Its a plain interface */
#define RS_NO_PRE       0x0004	/* The interfacd has set rates */
#define RS_INT_TBL      0x0010	/*
				 * The table is the internal version
				 * which has special setup requirements.
				 */
#define RS_IS_DEAD      0x0020	/* The RS is dead list */
#define RS_FUNERAL_SCHD 0x0040  /* Is a epoch call scheduled to bury this guy?*/
#define RS_INTF_NO_SUP  0x0100 	/* The interface does not support the ratelimiting */

struct tcp_rate_set {
	struct sysctl_ctx_list sysctl_ctx;
	CK_LIST_ENTRY(tcp_rate_set) next;
	struct ifnet *rs_ifp;
	struct tcp_hwrate_limit_table *rs_rlt;
	uint64_t rs_flows_using;
	uint64_t rs_flow_limit;
	uint32_t rs_if_dunit;
	int rs_rate_cnt;
	int rs_min_seg;
	int rs_highest_valid;
	int rs_lowest_valid;
	int rs_disable;
	int rs_flags;
	struct epoch_context rs_epoch_ctx;
};

CK_LIST_HEAD(head_tcp_rate_set, tcp_rate_set);

/* Request flags */
#define RS_PACING_EXACT_MATCH	0x0001	/* Need an exact match for rate */
#define RS_PACING_GT		0x0002	/* Greater than requested */
#define RS_PACING_GEQ		0x0004	/* Greater than or equal too */
#define RS_PACING_LT		0x0008	/* Less than requested rate */
#define RS_PACING_SUB_OK	0x0010	/* If a rate can't be found get the
					 * next best rate (highest or lowest). */
#ifdef _KERNEL
#ifndef ETHERNET_SEGMENT_SIZE
#define ETHERNET_SEGMENT_SIZE 1514
#endif
#ifdef RATELIMIT
#define DETAILED_RATELIMIT_SYSCTL 1	/*
					 * Undefine this if you don't want
					 * detailed rates to appear in
					 * net.inet.tcp.rl.
					 * With the defintion each rate
					 * shows up in your sysctl tree
					 * this can be big.
					 */
uint64_t inline
tcp_hw_highest_rate(const struct tcp_hwrate_limit_table *rle)
{
	return (rle->ptbl->rs_rlt[rle->ptbl->rs_highest_valid].rate);
}

uint64_t
tcp_hw_highest_rate_ifp(struct ifnet *ifp, struct inpcb *inp);

const struct tcp_hwrate_limit_table *
tcp_set_pacing_rate(struct tcpcb *tp, struct ifnet *ifp,
    uint64_t bytes_per_sec, int flags, int *error, uint64_t *lower_rate);

const struct tcp_hwrate_limit_table *
tcp_chg_pacing_rate(const struct tcp_hwrate_limit_table *crte,
    struct tcpcb *tp, struct ifnet *ifp,
    uint64_t bytes_per_sec, int flags, int *error, uint64_t *lower_rate);
void
tcp_rel_pacing_rate(const struct tcp_hwrate_limit_table *crte,
    struct tcpcb *tp);
#else
static inline const struct tcp_hwrate_limit_table *
tcp_set_pacing_rate(struct tcpcb *tp, struct ifnet *ifp,
    uint64_t bytes_per_sec, int flags, int *error, uint64_t *lower_rate)
{
	if (error)
		*error = EOPNOTSUPP;
	return (NULL);
}

static inline const struct tcp_hwrate_limit_table *
tcp_chg_pacing_rate(const struct tcp_hwrate_limit_table *crte,
    struct tcpcb *tp, struct ifnet *ifp,
    uint64_t bytes_per_sec, int flags, int *error, uint64_t *lower_rate)
{
	if (error)
		*error = EOPNOTSUPP;
	return (NULL);
}

static inline void
tcp_rel_pacing_rate(const struct tcp_hwrate_limit_table *crte,
    struct tcpcb *tp)
{
	return;
}
#endif
/*
 * Given a b/w and a segsiz, and optional hardware
 * rate limit, return the ideal size to burst
 * out at once. Note the parameter can_use_1mss
 * dictates if the transport will tolerate a 1mss
 * limit, if not it will bottom out at 2mss (think
 * delayed ack).
 */
uint32_t
tcp_get_pacing_burst_size(struct tcpcb *tp, uint64_t bw, uint32_t segsiz, int can_use_1mss,
   const struct tcp_hwrate_limit_table *te, int *err);


void
tcp_rl_log_enobuf(const struct tcp_hwrate_limit_table *rte);

#endif
#endif