Skip to content

Commit

Permalink
[add] plugin traffic tool add readme.md
Browse files Browse the repository at this point in the history
  • Loading branch information
jiumos committed Dec 28, 2023
1 parent 3fe6c1d commit 4ef2685
Show file tree
Hide file tree
Showing 13 changed files with 255 additions and 206 deletions.
4 changes: 2 additions & 2 deletions go-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func main() {
http.ListenAndServe(":8080", nil)
}()

// TLS
// TLS
certPath := "/app/certificate/certificate.pem"
keyPath := "/app/certificate/private-key.pem"

Expand All @@ -221,5 +221,5 @@ func main() {
fmt.Println("HTTPS server error:", err)
}
}()
select{}
select {}
}
4 changes: 2 additions & 2 deletions traffic-generators/1-high-connection/tcpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"os"
"strconv"
"strings"
"sync/atomic"
"syscall"
"time"
"sync/atomic"
)

const (
Expand Down Expand Up @@ -139,7 +139,7 @@ func main() {
}

for serverPort := MIN_SERVER_PORT; ; serverPort++ {
if atomic.LoadInt64(&liveConnection) + 1 > int64(maxConcurrent){
if atomic.LoadInt64(&liveConnection)+1 > int64(maxConcurrent) {
time.Sleep(time.Second)
timeStart = time.Now()
sleepCounter = 0
Expand Down
2 changes: 1 addition & 1 deletion traffic-generators/new-flow/tcpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,4 @@ func main() {
sleepCounter = 0
}
}
}
}
78 changes: 78 additions & 0 deletions traffic-tools-plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# 打流工具介绍

## 1. DNS 打流工具

**协议:** UDP (L4) 和 DNS (L7)

通过 UDP 协议和 DNS 协议的流量触发 DNS 插件。该工具可以模拟 DNS 流量,用于测试和评估 DNS 插件的性能和响应。

**使用方法:**

```
-c int
Number of concurrent connections (default 1)
-r int
Number of requests per connection (default 10)
```

## 2. HTTP 打流工具

**协议:** HTTP (L7)

通过 8080 端口和 URL 为 "/user_info" 的流量触发 HTTP 插件。该工具可以模拟 HTTP 流量,特定的请求和响应头部字段用于触发 HTTP 插件的计算。

**请求 (Req):**
- Header 添加 "Custom-Trace-Info" 用于 HTTP 插件计算

**响应 (Resp):**
- Data 添加 "code" 和 "data" 字段用于 HTTP 插件计算
-
**使用方法:**

```
-r int
request per second (default 100)
```

## 3. HTTP Status Rewrite 打流工具

**协议:** HTTP (L7)

通过监听 HTTP 响应,修改 Data 字段的 "OPT_STATUS" 为 "SUCCESS",同时填充其他数据,以触发 TCP 分片(TCP Fragment)。

**响应 (Resp):**
- Data 添加 "OPT_STATUS": "SUCCESS",其他填充数据用于触发 TCP 分片

**使用方法:**

```
-r int
request per second (default 100)
```

## 4. GO_HTTP2_Uprobe 打流工具

**协议:** gRPC

通过 gRPC 调用触发,指定 "Trace" 和 "Trace" 字段。该工具用于测试和评估http2_uprobe插件性能。

**使用方法:**

```
-r int
request per second (default 100)
```

## 5. KRPC 打流工具

**使用方法:**

```
-h string
krpc pod clusterIP
-r int
request per second (default 100)
```



32 changes: 13 additions & 19 deletions traffic-tools-plugin/dns/client.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package main

import (
"flag"
"fmt"
"golang.org/x/net/dns/dnsmessage"
"net"
"os"
"strconv"
"time"
)

var (
newFlow = 1
request_per_flow = 1
request_per_conn = flag.Int("r", 10, "Number of requests per connection")
concurrentConn = flag.Int("c", 1, "Number of concurrent connections")
request_interval = time.Second / 10

)

func newConnection(queryData []byte) {
flag.Parse()
serverAddr := "127.0.0.1:19053"

// 创建 UDP 连接到 DNS 服务器
Expand All @@ -27,7 +28,7 @@ func newConnection(queryData []byte) {
fmt.Printf("Error creating UDP connection: %v\n", err)
}
defer conn.Close()
for i:=0; i < request_per_flow; i++{
for i := 0; i < *request_per_conn; i++ {
conn.Write(queryData)
responseData := make([]byte, 512)
conn.Read(responseData)
Expand All @@ -39,7 +40,7 @@ func main() {

message := dnsmessage.Message{
Header: dnsmessage.Header{
ID: 12345,
ID: 12345,
Response: false,
OpCode: 0,
RecursionDesired: true,
Expand All @@ -57,25 +58,18 @@ func main() {
if err != nil {
fmt.Printf("Failed to encode DNS query: %v\n", err)
}
if len(os.Args) == 2{
newFlow, _ = strconv.Atoi(os.Args[1])
}
if len(os.Args) == 3{
newFlow, _ = strconv.Atoi(os.Args[1])
request_per_flow, _ = strconv.Atoi(os.Args[2])
}
sleepCounter := 0
timeStart := time.Now()

for {
for {
go newConnection(queryData)
time.Sleep(time.Second / time.Duration(newFlow) / 6)
time.Sleep(time.Second / time.Duration(*concurrentConn) / 6)

sleepCounter += 1
if sleepCounter >= newFlow {
if sleepCounter >= *concurrentConn {
timeElapsed := time.Since(timeStart)
if timeElapsed < time.Second {
fmt.Printf("Create %d connections\n",sleepCounter)
fmt.Printf("Create %d connections\n", sleepCounter)
time.Sleep(time.Second - timeElapsed)
}

Expand All @@ -84,4 +78,4 @@ func main() {
}
}

}
}
20 changes: 10 additions & 10 deletions traffic-tools-plugin/dns/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package main

import (
"fmt"
"golang.org/x/net/dns/dnsmessage"
"net"
"sync/atomic"
"golang.org/x/net/dns/dnsmessage"
"time"
)

Expand All @@ -26,7 +26,7 @@ func main() {
defer conn.Close()
fmt.Printf("DNS server is listening on %s\n", serverAddr)

go func (){
go func() {
for {
buffer := make([]byte, 512)
_, addr, err := conn.ReadFromUDP(buffer)
Expand All @@ -38,21 +38,21 @@ func main() {
// 解析 DNS 查询请求
var request dnsmessage.Message
request.Unpack(buffer)

// 构建 DNS 响应消息
response := dnsmessage.Message{
Header: dnsmessage.Header{
ID: request.Header.ID, // 使用相同的事务 ID
Response: true, // 设置为响应
ID: request.Header.ID, // 使用相同的事务 ID
Response: true, // 设置为响应
},
Questions: request.Questions, // 复制查询问题
Answers: []dnsmessage.Resource{
{
Header: dnsmessage.ResourceHeader{
Name: request.Questions[0].Name, // 使用相同的查询域名
Type: dnsmessage.TypeA, // 响应类型,A表示 IPv4 地址
Class: dnsmessage.ClassINET, // 查询类别,通常是 INET
TTL: 300, // TTL (Time to Live) 设置为 300 秒
Type: dnsmessage.TypeA, // 响应类型,A表示 IPv4 地址
Class: dnsmessage.ClassINET, // 查询类别,通常是 INET
TTL: 300, // TTL (Time to Live) 设置为 300 秒
},
Body: &dnsmessage.AResource{
A: [4]byte{192, 168, 1, 100}, // 设置响应的 IP 地址
Expand All @@ -64,13 +64,13 @@ func main() {
conn.WriteToUDP(responseData, addr)
}
}()

ticker := time.NewTicker(1 * time.Second)
i := 0
for range ticker.C {
i += 1
fmt.Printf("%s %d totalPacket %d,\n",
time.Now().Format(time.RFC3339),i ,atomic.LoadUint64(&totalPacket))
time.Now().Format(time.RFC3339), i, atomic.LoadUint64(&totalPacket))
}

}
55 changes: 26 additions & 29 deletions traffic-tools-plugin/go_http2_uprobe/client.go
Original file line number Diff line number Diff line change
@@ -1,48 +1,45 @@
package main

import (
"context"
"demo/go_http2_uprobe/pb"
"fmt"
"google.golang.org/grpc"
"time"
"os"
"strconv"
"context"
"demo/go_http2_uprobe/pb"
"flag"
"fmt"
"google.golang.org/grpc"
"time"
)

var (
newFlow = 80
requestPerSecond = flag.Int("r", 100, "request per second")
)

func newRequest(cli pb.DemoClient) {
var i uint32 = 1
cli.Call(context.TODO(), &pb.Req{
Msg: &pb.Msg{
ID: i,
Payload: []byte{1, 2, 3, 4},
},
Trace: &pb.Trace{
TraceId: 999,
Start: 999,
Span: 123,
},
})
var i uint32 = 1
cli.Call(context.TODO(), &pb.Req{
Msg: &pb.Msg{
ID: i,
Payload: []byte{1, 2, 3, 4},
},
Trace: &pb.Trace{
TraceId: 999,
Start: 999,
Span: 123,
},
})
}

func main() {
if len(os.Args) == 2{
newFlow, _ = strconv.Atoi(os.Args[1])
}
flag.Parse()
sleepCounter := 0
timeStart := time.Now()
c, _ := grpc.Dial("127.0.0.1:1234", grpc.WithInsecure())
cli := pb.NewDemoClient(c)
for {
c, _ := grpc.Dial("127.0.0.1:1234", grpc.WithInsecure())
cli := pb.NewDemoClient(c)
for {
go newRequest(cli)
time.Sleep(time.Second / time.Duration(newFlow) / 6)
time.Sleep(time.Second / time.Duration(*requestPerSecond) / 6)

sleepCounter += 1
if sleepCounter >= newFlow {
if sleepCounter >= *requestPerSecond {
timeElapsed := time.Since(timeStart)
fmt.Printf("Create %d requests, cost time %v\n", sleepCounter, timeElapsed)
if timeElapsed < time.Second {
Expand All @@ -53,4 +50,4 @@ func main() {
sleepCounter = 0
}
}
}
}
Loading

0 comments on commit 4ef2685

Please sign in to comment.