-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
187 lines (149 loc) · 4.04 KB
/
main.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
package main
import (
"log"
"net"
"os"
"strconv"
"syscall"
"time"
"unsafe"
)
// #include <sys/syscall.h>
import "C"
const (
payload_sz = 32
chunk_sz = 1024
local_port = 5674
)
func main() {
if len(os.Args) < 4 {
usage()
}
addr, err := net.ResolveUDPAddr("", os.Args[3])
chk(err)
iter, err := strconv.Atoi(os.Args[2])
chk(err)
iter *= 1E6
switch os.Args[1] {
case "write":
write(addr, iter)
case "writeToUDP":
writeToUDP(addr, iter)
case "sendTo":
sendTo(addr, iter)
case "sendMsg":
sendMsg(addr, iter)
case "sendMMsg":
sendMMsg(addr, iter)
case "All":
write(addr, iter)
time.Sleep(time.Second * 2)
writeToUDP(addr, iter)
time.Sleep(time.Second * 2)
sendTo(addr, iter)
time.Sleep(time.Second * 2)
sendMsg(addr, iter)
time.Sleep(time.Second * 2)
sendMMsg(addr, iter)
default:
usage()
}
}
func usage() {
log.Fatal("Usage: %s write|writeToUDP|sendTo|sendMsg|sendMMsg|All iteration_count*1M target_ip:port", os.Args[0])
}
func chk(err error) {
if err != nil {
panic(err)
}
}
func write(addr *net.UDPAddr, i int) {
log.Printf("Start `write` test with %d iteration\n", i)
conn, err := net.DialUDP(addr.Network(), nil, addr)
chk(err)
payload := make([]byte, payload_sz)
for ; i > 0; i-- {
_, err := conn.Write(payload)
chk(err)
}
}
func writeToUDP(addr *net.UDPAddr, i int) {
log.Printf("Start `writeToUDP` test with %d iteration\n", i)
conn, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.IPv4zero, Port: 0})
chk(err)
payload := make([]byte, payload_sz)
for ; i > 0; i-- {
_, err := conn.WriteToUDP(payload, addr)
chk(err)
}
}
func sendTo(addr *net.UDPAddr, i int) {
log.Printf("Start `sendTo` test with %d iteration\n", i)
laddr := UDPAddrToSockaddr(&net.UDPAddr{Port: local_port, IP: net.IPv4zero})
raddr := UDPAddrToSockaddr(addr)
fd := connectUDP(laddr, raddr)
payload := make([]byte, payload_sz)
for ; i > 0; i-- {
err := syscall.Sendto(fd, payload, syscall.MSG_DONTWAIT, raddr)
chk(err)
}
}
func sendMsg(addr *net.UDPAddr, i int) {
log.Printf("Start `sendMsg` test with %d iteration\n", i)
laddr := UDPAddrToSockaddr(&net.UDPAddr{Port: local_port, IP: net.IPv4zero})
raddr := UDPAddrToSockaddr(addr)
fd := connectUDP(laddr, raddr)
payload := make([]byte, payload_sz)
for ; i > 0; i-- {
err := syscall.Sendmsg(fd, payload, nil, raddr, syscall.MSG_DONTWAIT)
chk(err)
}
}
type MMsghdr struct {
Msg syscall.Msghdr
cnt int
}
func sendMMsg(addr *net.UDPAddr, i int) {
i = i / chunk_sz
log.Printf("Start `sendMMsg` test with %d iteration\n", i)
laddr := UDPAddrToSockaddr(&net.UDPAddr{Port: local_port, IP: net.IPv4zero})
raddr := UDPAddrToSockaddr(addr)
fd := connectUDP(laddr, raddr)
msgcnt := chunk_sz
var msgArr [chunk_sz]MMsghdr
for j := 0; j < msgcnt; j++ {
p := make([]byte, payload_sz)
for k := 0; k < payload_sz; k++ {
p[k] = byte(k)
}
var iov syscall.Iovec
iov.Base = (*byte)(unsafe.Pointer(&p[0]))
iov.SetLen(len(p))
var msg syscall.Msghdr
msg.Iov = &iov
msg.Iovlen = 1
msgArr[j] = MMsghdr{msg, 0}
}
for ; i > 0; i-- {
//_, _, e1 := syscall.Syscall6(C.SYS_sendmmsg, uintptr(fd), uintptr(unsafe.Pointer(&msgArr[0])), uintptr(msgcnt), uintptr(syscall.MSG_DONTWAIT), 0, 0)
_, _, e1 := syscall.Syscall6(C.SYS_sendmmsg, uintptr(fd), uintptr(unsafe.Pointer(&msgArr[0])), uintptr(msgcnt), 0, 0, 0)
if e1 != 0 {
panic("error on sendmmsg")
}
}
}
func UDPAddrToSockaddr(addr *net.UDPAddr) *syscall.SockaddrInet4 {
raddr := &syscall.SockaddrInet4{Port: addr.Port, Addr: [4]byte{addr.IP[12], addr.IP[13], addr.IP[14], addr.IP[15]}}
return raddr
}
func connectUDP(laddr, raddr syscall.Sockaddr) int {
fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, 0)
chk(err)
err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
chk(err)
err = syscall.Bind(fd, laddr)
chk(err)
err = syscall.Connect(fd, raddr)
chk(err)
return fd
}