Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ads: support envoy filter local ratelimit. #859

Merged
merged 1 commit into from
Oct 31, 2024

Conversation

yuanqijing
Copy link
Contributor

What type of PR is this?
support local rate limit.

What this PR does / why we need it:

Which issue(s) this PR fixes:
Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?:


Copy link

codecov bot commented Sep 15, 2024

Codecov Report

Attention: Patch coverage is 88.57143% with 4 lines in your changes missing coverage. Please review.

Project coverage is 52.17%. Comparing base (1f2940b) to head (6876096).
Report is 130 commits behind head on main.

Files with missing lines Patch % Lines
pkg/controller/ads/cache.go 0.00% 4 Missing ⚠️
Files with missing lines Coverage Δ
pkg/controller/ads/extensions/local_ratelimit.go 100.00% <100.00%> (ø)
pkg/utils/test/bpf_map.go 34.04% <100.00%> (-3.11%) ⬇️
pkg/controller/ads/cache.go 47.94% <0.00%> (+0.39%) ⬆️

... and 69 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e5b8028...6876096. Read the comment docs.

@yuanqijing yuanqijing changed the title [WIP] ads: support envoy filter local ratelimit. ads: support envoy filter local ratelimit. Sep 15, 2024
}
*/
message LocalRateLimit {
reserved 1 to 2;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why reserve?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reserve for
{
"stat_prefix": ...,
"status": {...},
}
should i start token_bucket with number 1, or keep the number tag ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can start from 1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.

"enable_x_ratelimit_headers": ...,
"vh_rate_limits": ...,
"always_consume_default_token_bucket": {...},
"rate_limited_as_resource_exhausted": ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if other fields not supported, please remove them. Add once we support it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just remove the comments? i copy the definition from envoy api.

Copy link
Member

@hzxuzhonghu hzxuzhonghu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First we need a proposal to better understand this implement

};

struct ratelimit_value {
__u64 last_topup;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a comment about what is this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.

@yuanqijing
Copy link
Contributor Author

First we need a proposal to better understand this implement

hi i have created a proposal pr in #873

@yuanqijing yuanqijing force-pushed the ads/ratelimit branch 2 times, most recently from 917ece0 to 007baba Compare September 19, 2024 14:49
@yuanqijing
Copy link
Contributor Author

@hzxuzhonghu hi, ratelimit use the same way to cut tcp connection as circuit breaker, it can be merged after #570.

@yuanqijing yuanqijing force-pushed the ads/ratelimit branch 2 times, most recently from cdf2d67 to 8b988d1 Compare September 19, 2024 15:16
__type(value, struct ratelimit_value);
__uint(max_entries, 1000);
__uint(map_flags, BPF_F_NO_PREALLOC);
} ratelimit_map SEC(".maps");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please keep consistent with other map name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.

@hzxuzhonghu
Copy link
Member

In kmesh, we are a little different, we can only ratelimit on the client side now. So take the example here, though it is http.

https://istio.io/latest/docs/tasks/policy-enforcement/rate-limit/#local-rate-limit

apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
  name: filter-local-ratelimit-svc
  namespace: istio-system
spec:
  workloadSelector:
    labels:
      app: productpage
  configPatches:
    - applyTo: NETWORK_FILTER
      match:
        context: SIDECAR_OUTBOUND

It should be applied on each client instance, not on all the instances of a node.

@yuanqijing
Copy link
Contributor Author

yuanqijing commented Oct 28, 2024

In kmesh, we are a little different, we can only ratelimit on the client side now. So take the example here, though it is http.

https://istio.io/latest/docs/tasks/policy-enforcement/rate-limit/#local-rate-limit

apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
  name: filter-local-ratelimit-svc
  namespace: istio-system
spec:
  workloadSelector:
    labels:
      app: productpage
  configPatches:
    - applyTo: NETWORK_FILTER
      match:
        context: SIDECAR_OUTBOUND

It should be applied on each client instance, not on all the instances of a node.

Understood, so the key point here is whether intances in a node share a token bucket.
option 1: client_A->Service_C: 5 token, client_B->Service_C: 6 token
option 2: Service_C: 11 token.

Envoy using option 2, but in kmesh we use option1 ? Is my understanding correct?

@hzxuzhonghu
Copy link
Member

Envoy using option 2, but in kmesh we use option1 ? Is my understanding correct?

Envoy can use both, kmesh we can only use option 1

@yuanqijing yuanqijing force-pushed the ads/ratelimit branch 3 times, most recently from 362207e to 6876096 Compare October 28, 2024 08:51
@yuanqijing
Copy link
Contributor Author

Envoy using option 2, but in kmesh we use option1 ? Is my understanding correct?

Envoy can use both, kmesh we can only use option 1

I have added a new field to distinguish different pods:

struct ratelimit_key {
    union {
        struct {
            __u64 netns;  /* Network namespace. */
            __u32 ipv4;   /* Destination IPv4 address. */
            __u32 port;   /* Destination port. */
            __u32 family; /* Address family (e.g., AF_INET) */
        } sk_skb;
    } key;
};

@yuanqijing
Copy link
Contributor Author

@hzxuzhonghu Hi I think there are 2 pending Issues:

  1. When cleaning up the listener, also clean up the ratelimit configuration. Can I create another PR to fix this?
  2. The concurrent issue here may not have been fully discussed yet.

@hzxuzhonghu
Copy link
Member

  1. When cleaning up the listener, also clean up the ratelimit configuration. Can I create another PR to fix this?

Yes, i think its ok

  1. The concurrent issue here may not have been fully discussed yet.

@nlgwcy suggested using atomic update

@yuanqijing
Copy link
Contributor Author

  1. When cleaning up the listener, also clean up the ratelimit configuration. Can I create another PR to fix this?

Yes, i think its ok

  1. The concurrent issue here may not have been fully discussed yet.

@nlgwcy suggested using atomic update

suggested atomic updates all addressed. #859 (comment) #859 (comment)

if (topup > 0) {
value->tokens += topup * settings->tokens_per_fill;
if (value->tokens > settings->max_tokens) {
value->tokens = settings->max_tokens;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not concurrent safe

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this ensue concurent safe ? cause only one progress can add the value.

  topup = (now - value->last_topup) / settings->fill_interval;
  if (topup > 0) {
      topup_time = value->last_topup + topup * settings->fill_interval;
      if (__sync_bool_compare_and_swap(&value->last_topup, value->last_topup, topup_time)) {
          delta = topup * settings->tokens_per_fill;
          if (value->tokens + delta > settings->max_tokens) {
              delta = settings->max_tokens - value->tokens;
          }
          __sync_fetch_and_add(&value->tokens, delta);
      }
  }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nlgwcy is an expert on this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nlgwcy hi, could this approach be used to avoid concurrent issues?

#include "listener/listener.pb-c.h"

struct ratelimit_key {
union {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why use union here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a union in the ratelimit_key structure improves scalability. If we need to limit HTTP connections in the future, we can reuse the same key structure. For example, we can add a structure for HTTP connections like this:


struct ratelimit_key {
    union {
        struct {
            __u64 netns;  /* Network namespace. */
            __u32 ipv4;   /* Destination IPv4 address. */
            __u32 port;   /* Destination port. */
            __u32 family; /* Address family (e.g., AF_INET) */
        } sk_skb;

        struct {
            __u32 http_method;   /* HTTP method (e.g., GET, POST) */
            __u32 http_version;    /* HTTP version (e.g., 1.1, 2) */
            __u32 http_port;         /* HTTP port */
        } http; // New HTTP connection related fields
    } key;
};

@nlgwcy
Copy link
Contributor

nlgwcy commented Oct 31, 2024

/lgtm

if (rate_limit__check_and_take(&key, &settings)) {
BPF_LOG(INFO, FILTERCHAIN, "rate limit exceeded\n");
// TODO: remove this after #570 merged.
#ifndef MARK_REJECTED
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not needed anymore.

@kmesh-bot
Copy link
Collaborator

New changes are detected. LGTM label has been removed.

@lec-bit
Copy link
Contributor

lec-bit commented Oct 31, 2024

/retest

@yuanqijing yuanqijing force-pushed the ads/ratelimit branch 2 times, most recently from 0c4da26 to 6bc5c4e Compare October 31, 2024 12:50
@nlgwcy
Copy link
Contributor

nlgwcy commented Oct 31, 2024

/approve

@kmesh-bot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: nlgwcy

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@nlgwcy nlgwcy merged commit 55d4e65 into kmesh-net:main Oct 31, 2024
6 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants