-
Notifications
You must be signed in to change notification settings - Fork 100
feat(egress): dynamic IP insertion from DNS answers #197
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
77cb8b2
feat(egress): dynamic IP insertion from DNS answers
Pangjiping 0aaf9bb
chore(egress): add benchmark test
Pangjiping 671567d
chore(egress): prefer first non-loopback nameserver
Pangjiping 92c8ba2
chore(egress): optimize benchmark test
Pangjiping a20e03a
chore(egress): update TODOs and optimize dnsproxy type convert
Pangjiping bf4567b
chore(egress): optimize smoke test target domain
Pangjiping 5d7d2dd
fix(egress): using the same lock in ApplyStatic avoids this race.
Pangjiping File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| # Egress Benchmark | ||
|
|
||
| This document describes the **Egress Sidecar** end-to-end benchmark: it compares **dns** and **dns+nft** modes under real conditions for latency and throughput. | ||
|
|
||
| ## Purpose | ||
|
|
||
| - **dns**: DNS proxy only (pass-through), no nftables writes; used as the baseline. | ||
| - **dns+nft**: DNS proxy plus synchronous `AddResolvedIPs` before each DNS reply, writing resolved IPs into nftables for | ||
| L2 egress enforcement. | ||
|
|
||
| The benchmark runs the same workload in both modes and reports end-to-end latency (P50, P99) and throughput (Req/s) to | ||
| measure the overhead of the synchronous nft write path. | ||
|
|
||
| ## Environment and Flow | ||
|
|
||
| - **Environment**: The Egress sidecar runs in a Docker container on the host. The container includes the sidecar (DNS | ||
| proxy and optional nft), iptables redirect of port 53 to the proxy, and the policy server on port 18080. The workload | ||
| runs **inside the same container**: DNS and HTTPS traffic go through the proxy. | ||
| - **Flow** (per phase): | ||
| 1. Start the sidecar with the chosen mode (`dns` or `dns+nft`). | ||
| 2. Wait for health checks, then push the allow list to `/policy` (see domain list below). | ||
| 3. Write the domain list into the container as `/tmp/bench-domains.txt` (one `https://<domain>` per line). | ||
| 4. **Warm-up**: One request to each of the first 10 domains (10 concurrent), 1 round. | ||
| 5. **Timed run**: One request per domain for all domains (N concurrent per round), for 10 rounds; each request | ||
| records `time_namelookup` and `time_total`. | ||
| 6. Copy results from the container and compute P50, P99, average latency, and Req/s. | ||
| - **Execution order**: **dns+nft** runs first, then **dns**; the comparison table is printed at the end. | ||
|
|
||
| ## Workload | ||
|
|
||
| - **Domain list**: Read from `components/egress/tests/hostname.txt`, one domain per line (lines starting with `#` and | ||
| empty lines are ignored). Default is about 100 resolvable domains. | ||
| - **Rounds and concurrency**: The script uses `ROUNDS=10`. Each round issues one HTTPS request per domain in | ||
| `hostname.txt`, with all requests in that round concurrent; 10 rounds total. | ||
| - **Total requests**: `TOTAL_REQUESTS = ROUNDS × NUM_DOMAINS` (e.g. 10 × 100 = 1000). | ||
| - **Per request**: Inside the container, `curl -o /dev/null -s -w "%{time_namelookup}\t%{time_total}\n"` is used against | ||
| `https://<domain>`, with a 10s timeout per request; the whole benchmark run has a 300s wall-clock timeout. | ||
|
|
||
| ## Policy | ||
|
|
||
| - Policy is default-deny with explicit allow rules: one `{"action":"allow","target":"<domain>"}` per domain in | ||
| `hostname.txt` is sent via `POST /policy`, so every domain used in the benchmark is allowed. | ||
|
|
||
| ## How to Run | ||
|
|
||
| **Script**: `components/egress/tests/bench-e2e-dns-nft.sh` | ||
|
|
||
| **Requirements**: Docker and `curl` on the host (for pushing policy); the Egress image includes `curl` for the workload. | ||
|
|
||
| **Commands** (from repo root or from `components/egress`): | ||
|
|
||
| ```bash | ||
| ./tests/bench-dns-nft.sh | ||
| ``` | ||
|
|
||
| The script resolves `tests/hostname.txt` relative to its own path, so the working directory does not need to be changed. | ||
|
|
||
| ## Configuration | ||
|
|
||
| | Item | Location / variable | Default / notes | | ||
| |---------------------|----------------------------------------|------------------------------------------------| | ||
| | Domain list | `components/egress/tests/hostname.txt` | One domain per line; `#` comments allowed | | ||
| | Rounds | `ROUNDS` in script | 10 | | ||
| | Per-request timeout | `CURL_TIMEOUT` in script | 10 seconds | | ||
| | Benchmark timeout | `BENCH_EXEC_TIMEOUT` in script | 300 seconds (max wall time for the timed run) | | ||
| | Image | `IMG` in script | See script; override for a locally built image | | ||
|
|
||
| Changing the number of domains or rounds updates the total request count; the report shows “N rounds × M domains” for | ||
| the current config. | ||
|
|
||
| ## Output and Metrics | ||
|
|
||
| - **Terminal**: A table with **Req/s**, **Avg(s)**, **P50(s)**, **P99(s)** for both modes, plus short notes (dns vs | ||
| dns+nft, warm-up, first-resolution cost). | ||
| - **Artifacts** (on the host under `/tmp`): `bench-e2e-dns-total.txt`, `bench-e2e-dns+nft-total.txt` (one | ||
| `time_total` per line), and `-namelookup.txt`, `-wall.txt`, etc., for further analysis or plotting. | ||
|
|
||
| ## Notes | ||
|
|
||
| - The first resolution of a domain in dns+nft triggers a DNS lookup and an nft write, so cost is higher; later requests | ||
| for the same domain hit the set and are cheaper. The multi-round, multi-domain design mixes cold and warm resolution. | ||
| - In CI (e.g. GitHub Actions), the script wraps the timed-run `docker exec` with `timeout` inside the shell function so | ||
| `timeout` runs a real command, not a function name, avoiding “No such file or directory” errors. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| // Copyright 2026 Alibaba Group Holding Ltd. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "log" | ||
| "net/netip" | ||
| "os" | ||
| "strconv" | ||
|
|
||
| "github.com/alibaba/opensandbox/egress/pkg/constants" | ||
| "github.com/alibaba/opensandbox/egress/pkg/dnsproxy" | ||
| ) | ||
|
|
||
| // AllowIPsForNft returns the list of IPs to merge into the nft allow set for DNS in dns+nft mode: | ||
| // 127.0.0.1 (proxy listen / iptables redirect target) plus validated, capped nameserver IPs from resolvPath. | ||
| // Validation: skips unspecified (0.0.0.0, ::) and loopback (127.x, ::1). | ||
| // Cap: at most max nameservers (default 3; set EGRESS_MAX_NAMESERVERS=0 for no cap, or 1–10). | ||
| func AllowIPsForNft(resolvPath string) []netip.Addr { | ||
| raw, _ := dnsproxy.ResolvNameserverIPs(resolvPath) | ||
| maxNsCount := maxNameserversFromEnv() | ||
|
|
||
| var validated []netip.Addr | ||
| for _, ip := range raw { | ||
| if maxNsCount > 0 && len(validated) >= maxNsCount { | ||
| break | ||
| } | ||
| if !isValidNameserverIP(ip) { | ||
| continue | ||
| } | ||
| validated = append(validated, ip) | ||
| } | ||
|
|
||
| // 127.0.0.1 first so packets redirected to proxy are accepted by nft. | ||
| out := make([]netip.Addr, 0, 1+len(validated)) | ||
| out = append(out, netip.MustParseAddr("127.0.0.1")) | ||
| out = append(out, validated...) | ||
|
|
||
| if len(out) > 1 { | ||
| log.Printf("[dns] whitelisting proxy listen + %d nameserver(s) for nft: %v", len(validated), formatIPs(out)) | ||
| } else { | ||
| log.Printf("[dns] whitelisting proxy listen (127.0.0.1); no valid nameserver IPs from %s", resolvPath) | ||
| } | ||
| return out | ||
| } | ||
|
|
||
| func maxNameserversFromEnv() int { | ||
| s := os.Getenv(constants.EnvMaxNameservers) | ||
| if s == "" { | ||
| return constants.DefaultMaxNameservers | ||
| } | ||
| n, err := strconv.Atoi(s) | ||
| if err != nil || n < 0 { | ||
| return constants.DefaultMaxNameservers | ||
| } | ||
| if n > 10 { | ||
| return 10 | ||
| } | ||
| // 0 = no cap | ||
| return n | ||
| } | ||
|
|
||
| func isValidNameserverIP(ip netip.Addr) bool { | ||
| if ip.IsUnspecified() { | ||
| return false | ||
| } | ||
| if ip.IsLoopback() { | ||
| return false | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| func formatIPs(ips []netip.Addr) []string { | ||
| out := make([]string, len(ips)) | ||
| for i, ip := range ips { | ||
| out[i] = ip.String() | ||
| } | ||
| return out | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.