-
Notifications
You must be signed in to change notification settings - Fork 0
/
dhcplt.go
192 lines (168 loc) · 4.71 KB
/
dhcplt.go
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// dhcplt
package main
import (
"context"
"os/signal"
"sync"
"syscall"
// "encoding/json"
"fmt"
"log"
"net/http"
_ "net/http/pprof"
"os"
"runtime"
// "runtime/debug"
"time"
"github.com/hujun-open/dhcplt/common"
"github.com/hujun-open/shouchan"
mv "github.com/RobinUS2/golang-moving-average"
"github.com/hujun-open/etherconn"
)
const (
BBFEnterpriseNumber = 3561
EthernetTypeIPv4 uint16 = 0x0800
EthernetTypeIPv6 uint16 = 0x86DD
)
type execResult int
const (
resultSuccess execResult = iota
resultFailure
)
func (er execResult) String() string {
switch er {
case resultSuccess:
return "success"
case resultFailure:
return "failed"
default:
return "unknow result"
}
}
type resultSummary struct {
Total int
Success int
Failed int
Released int
Renewed int
Rebinded int
LessThanSecond int
Shortest time.Duration
Longest time.Duration
TotalTime time.Duration
AvgSuccessTime *mv.MovingAverage
setup *testSetup
}
func newResultSummary(s *testSetup) *resultSummary {
return &resultSummary{
AvgSuccessTime: mv.New(5),
setup: s,
Shortest: maxDuration,
Longest: time.Duration(0),
}
}
func (rs resultSummary) String() string {
r := "Result Summary\n"
r += fmt.Sprintf("total trans: %d\n", rs.Total)
r += fmt.Sprintf("Success dial:%d\n", rs.Success)
r += fmt.Sprintf("Success release:%d\n", rs.Released)
r += fmt.Sprintf("Success renew:%d\n", rs.Renewed)
r += fmt.Sprintf("Success rebind:%d\n", rs.Rebinded)
r += fmt.Sprintf("Failed trans:%d\n", rs.Failed)
r += fmt.Sprintf("Duration:%v\n", rs.TotalTime)
r += fmt.Sprintf("Interval:%v\n", rs.setup.Interval)
avgSuccess := time.Duration(rs.AvgSuccessTime.Avg())
r += fmt.Sprintf("Setup rate:%v\n", float64(rs.Success)/(float64(rs.TotalTime)/float64(time.Second)))
r += fmt.Sprintf("Fastest dial success:%v\n", rs.Shortest)
r += fmt.Sprintf("dial Success within a second:%v\n", rs.LessThanSecond)
r += fmt.Sprintf("Slowest dial success:%v\n", rs.Longest)
r += fmt.Sprintf("Avg dial success time:%v\n", avgSuccess)
return r
}
const maxDuration = time.Duration(int64(^uint64(0) >> 1))
func createPktRelay(setup *testSetup) (etherconn.PacketRelay, error) {
switch setup.Driver {
case ENG_AFPKT:
relay, err := etherconn.NewRawSocketRelay(context.Background(),
setup.Ifname, etherconn.WithBPFFilter(bpfFilter),
etherconn.WithDebug(setup.Debug),
etherconn.WithDefaultReceival(false),
etherconn.WithSendChanDepth(10240),
)
if err != nil {
return nil, fmt.Errorf("failed to create afpkt relay for if %v, %v", setup.Ifname, err)
}
return relay, nil
case ENG_XDP:
relay, err := etherconn.NewXDPRelay(context.Background(),
setup.Ifname, etherconn.WithXDPDebug(setup.Debug),
etherconn.WithXDPDefaultReceival(false),
etherconn.WithXDPSendChanDepth(10240),
etherconn.WithXDPUMEMNumOfTrunk(65536),
etherconn.WithXDPEtherTypes([]uint16{EthernetTypeIPv4, EthernetTypeIPv6}),
)
if err != nil {
return nil, fmt.Errorf("failed to create xdp relay for if %v, %v", setup.Ifname, err)
}
return relay, nil
}
return nil, fmt.Errorf("unknown eng type %v", setup.Driver)
}
const (
//NOTE: without (xxx and vlan), then double vlan case won't work
// bpfFilter = "(udp or (udp and vlan)) or (icmp6 or (icmp6 and vlan))"
//bpfFilter = "(ip6 or (ip6 and vlan)) or (ip or (ip and vlan))"
// bpfFilter = "ip or ip6"
bpfFilter = ""
ENG_AFPKT = etherconn.RelayTypeAFP
ENG_XDP = etherconn.RelayTypeXDP
)
var VERSION string = "unversioned"
func handleCtrlC(c chan os.Signal, cf context.CancelFunc) {
<-c
fmt.Println("\n\rstopping...")
cf()
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
cnf, err := shouchan.NewSConf(newDefaultConf(), "dhcplt",
fmt.Sprintf("a DHCP load tester, %v", VERSION), shouchan.WithDefaultConfigFilePath[*testSetup]("dhcplt.conf"))
if err != nil {
panic(err)
}
cnf.ReadwithCMDLine()
setup := cnf.GetConf()
// fmt.Printf("%+v\n", setup)
err = setup.init()
if err != nil {
log.Fatalf("invalid setup, %v", err)
}
if setup.Profiling {
runtime.SetBlockProfileRate(1000000000)
go func() {
log.Println(http.ListenAndServe("0.0.0.0:6060", nil))
}()
}
if setup.Debug {
common.Logger = log.New(os.Stderr, "", log.Ldate|log.Ltime)
}
sch, err := NewSched(setup)
if err != nil {
common.MyLog("failed to create sched, %v", err)
return
}
ctx, cancelf := context.WithCancel(context.Background())
wg := new(sync.WaitGroup)
wg.Add(1)
go sch.run(ctx, wg)
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go handleCtrlC(c, cancelf)
wg.Wait()
if setup.Profiling {
ch := make(chan bool)
<-ch
}
fmt.Println("done.")
}