Skip to content

Commit 5964c91

Browse files
committed
ads: support envoy filter local ratelimit.
Signed-off-by: yuan <yuanqijing00x@gmail.com>
1 parent 53caaa8 commit 5964c91

File tree

10 files changed

+389
-30
lines changed

10 files changed

+389
-30
lines changed

api/filter/ratelimit.proto

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
syntax = "proto3";
2+
3+
package filter;
4+
option go_package = "kmesh.net/kmesh/api/filter;filter";
5+
6+
/*
7+
TokenBucket defines parameters for a token bucket rate limiter.
8+
https://www.envoyproxy.io/docs/envoy/latest/api-v3/type/v3/token_bucket.proto#envoy-v3-api-msg-type-v3-tokenbucket
9+
{
10+
"max_tokens": ...,
11+
"tokens_per_fill": {...},
12+
"fill_interval": {...}
13+
}
14+
*/
15+
message TokenBucket {
16+
// The maximum number of tokens in the bucket.
17+
int64 max_tokens = 1;
18+
19+
// The number of tokens added to the bucket during each fill interval.
20+
int64 tokens_per_fill = 2;
21+
22+
// The interval at which the bucket is refilled in nanoseconds.
23+
int64 fill_interval = 3;
24+
}
25+
26+
27+
/*
28+
LocalRateLimit defines parameters for local rate limiting.
29+
https://www.envoyproxy.io/docs/envoy/latest/api-v3/extensions/filters/http/local_ratelimit/v3/local_rate_limit.proto#envoy-v3-api-msg-extensions-filters-http-local-ratelimit-v3-localratelimit
30+
{
31+
"stat_prefix": ...,
32+
"status": {...},
33+
"token_bucket": {...},
34+
"filter_enabled": {...},
35+
"filter_enforced": {...},
36+
"request_headers_to_add_when_not_enforced": [],
37+
"response_headers_to_add": [],
38+
"descriptors": [],
39+
"stage": ...,
40+
"local_rate_limit_per_downstream_connection": ...,
41+
"local_cluster_rate_limit": {...},
42+
"enable_x_ratelimit_headers": ...,
43+
"vh_rate_limits": ...,
44+
"always_consume_default_token_bucket": {...},
45+
"rate_limited_as_resource_exhausted": ...
46+
}
47+
*/
48+
message LocalRateLimit {
49+
reserved 1 to 2;
50+
51+
// The token bucket configuration for the rate limiter.
52+
TokenBucket token_bucket = 3;
53+
54+
// Reserved field numbers for future use
55+
reserved 4 to 15;
56+
}
57+

api/listener/listener_components.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ option go_package = "kmesh.net/kmesh/api/listener;listener";
66
import "api/core/address.proto";
77
import "api/filter/tcp_proxy.proto";
88
import "api/filter/http_connection_manager.proto";
9+
import "api/filter/ratelimit.proto";
910

1011
message Filter {
1112
string name = 1;
1213
oneof config_type {
1314
filter.TcpProxy tcp_proxy = 2;
1415
filter.HttpConnectionManager http_connection_manager = 3;
16+
filter.LocalRateLimit local_rate_limit = 4;
1517
}
1618
}
1719

api/v2-c/listener/listener_components.pb-c.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ void listener__filter_chain__free_unpacked
142142
assert(message->base.descriptor == &listener__filter_chain__descriptor);
143143
protobuf_c_message_free_unpacked ((ProtobufCMessage*)message, allocator);
144144
}
145-
static const ProtobufCFieldDescriptor listener__filter__field_descriptors[3] =
145+
static const ProtobufCFieldDescriptor listener__filter__field_descriptors[4] =
146146
{
147147
{
148148
"name",
@@ -180,16 +180,29 @@ static const ProtobufCFieldDescriptor listener__filter__field_descriptors[3] =
180180
0 | PROTOBUF_C_FIELD_FLAG_ONEOF, /* flags */
181181
0,NULL,NULL /* reserved1,reserved2, etc */
182182
},
183+
{
184+
"local_rate_limit",
185+
4,
186+
PROTOBUF_C_LABEL_NONE,
187+
PROTOBUF_C_TYPE_MESSAGE,
188+
offsetof(Listener__Filter, config_type_case),
189+
offsetof(Listener__Filter, local_rate_limit),
190+
&filter__local_rate_limit__descriptor,
191+
NULL,
192+
0 | PROTOBUF_C_FIELD_FLAG_ONEOF, /* flags */
193+
0,NULL,NULL /* reserved1,reserved2, etc */
194+
},
183195
};
184196
static const unsigned listener__filter__field_indices_by_name[] = {
185197
2, /* field[2] = http_connection_manager */
198+
3, /* field[3] = local_rate_limit */
186199
0, /* field[0] = name */
187200
1, /* field[1] = tcp_proxy */
188201
};
189202
static const ProtobufCIntRange listener__filter__number_ranges[1 + 1] =
190203
{
191204
{ 1, 0 },
192-
{ 0, 3 }
205+
{ 0, 4 }
193206
};
194207
const ProtobufCMessageDescriptor listener__filter__descriptor =
195208
{
@@ -199,7 +212,7 @@ const ProtobufCMessageDescriptor listener__filter__descriptor =
199212
"Listener__Filter",
200213
"listener",
201214
sizeof(Listener__Filter),
202-
3,
215+
4,
203216
listener__filter__field_descriptors,
204217
listener__filter__field_indices_by_name,
205218
1, listener__filter__number_ranges,

api/v2-c/listener/listener_components.pb-c.h

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v2/listener/listener_components.pb.go

Lines changed: 48 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bpf/kmesh/ads/include/filter.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#ifndef __KMESH_FILTER_H__
55
#define __KMESH_FILTER_H__
66

7+
#include "local_ratelimit.h"
78
#include "tcp_proxy.h"
89
#include "tail_call.h"
910
#include "bpf_log.h"
@@ -181,6 +182,10 @@ int filter_chain_manager(ctx_buff_t *ctx)
181182
if (filter_chain == NULL) {
182183
return KMESH_TAIL_CALL_RET(-1);
183184
}
185+
186+
/* ratelimit check */
187+
Local_rate_limit__check_and_take(filter_chain, &addr, ctx);
188+
184189
/* filter match */
185190
ret = filter_chain_filter_match(filter_chain, &addr, ctx, &filter, &filter_idx);
186191
if (ret != 0) {

0 commit comments

Comments
 (0)