-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrecv.go
215 lines (205 loc) · 6.44 KB
/
recv.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package kping
import (
"encoding/binary"
"fmt"
"io"
"net"
"os"
"runtime"
"sync"
"syscall"
"time"
"github.com/google/gopacket/afpacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"golang.org/x/net/bpf"
"golang.org/x/net/ipv4"
)
const (
// bits number of nanosecond
timeSliceLength = 8
// init value of ICMP packet id and seq
icmpIDSeqInitNum = 10000
)
func (p *kping) batchRecv(index int, wg *sync.WaitGroup) {
defer wg.Done()
recvBatch := p.batchRecvOpts.BatchSize
recvParallel := p.batchRecvOpts.Parallel
stime := time.Now()
rms := make([]message, 0, recvBatch)
for i := 0; i < int(recvBatch); i++ {
msg := message{
Buffers: [][]byte{make([]byte, 100)},
N: 0,
}
rms = append(rms, msg)
}
L:
for {
select {
case <-p.sendDone:
break L
default:
}
stime2 := time.Now()
num, err := p.rawConn.readBatch(rms, index, 0) // blocking read
durTime := time.Since(stime2)
if durTime > 400*time.Millisecond {
fmt.Fprintf(os.Stderr, "kping recv: %d(%d) readBatch %d(%d), usedTime: %s\n", index, recvParallel, recvBatch, num, durTime)
}
if err != nil {
if err2, ok := err.(*os.SyscallError); ok {
if err3, ok := err2.Err.(syscall.Errno); ok && err3.Temporary() {
//time.Sleep(20 * time.Millisecond)
}
} else {
fmt.Fprintf(os.Stderr, "kping recv: %d(%d) readBatch failed: %v\n", index, recvParallel, err)
}
continue
}
for _, msg := range rms[0:num] {
ip := net.IPv4(msg.Buffers[0][12], msg.Buffers[0][13], msg.Buffers[0][14], msg.Buffers[0][15]).String()
hdrlen := int(msg.Buffers[0][0]&0x0f) << 2
bytes := msg.Buffers[0][hdrlen:msg.N]
if len(bytes) < 16 {
fmt.Fprintf(os.Stderr, "kping recv: %d(%d) %s IMCP message length %d < 16 Bytes, ignored\n", index, recvParallel, ip, len(bytes))
continue
}
/*
bytes[0]: type
bytes[1]: code
bytes[2:4]: checkSum
bytes[4:6]: id
bytes[6:8]: seq
bytes[8:16]: payload: timestamp
*/
msgType := ipv4.ICMPType(bytes[0])
if msgType != ipv4.ICMPTypeEchoReply {
continue
}
id := int(binary.BigEndian.Uint16(bytes[4:6]))
seq := int(binary.BigEndian.Uint16(bytes[6:8]))
// ignore mismatch id or seq packet
if id < icmpIDSeqInitNum || seq < icmpIDSeqInitNum {
continue
}
// calculate RTT
var nsec int64
for i := uint8(0); i < timeSliceLength; i++ {
nsec += int64(bytes[8 : 8+timeSliceLength][i]) << ((7 - i) * timeSliceLength)
}
sendTime := time.Unix(nsec/1000000000, nsec%1000000000)
durTime := time.Since(stime2)
rtt := time.Since(sendTime.Add(durTime))
p.ipEventChan <- &ipEvent{
ip: ip,
seq: seq,
recvRTT: rtt,
}
}
}
fmt.Fprintf(os.Stderr, "kping recv: %d(%d) done, usedTime: %s\n", index, recvParallel, time.Since(stime))
}
func (p *kping) afpacketRecv(index int, wg *sync.WaitGroup) {
defer wg.Done()
recvParallel := p.afpacketRecvOpts.Parallel
options := []interface{}{
afpacket.OptFrameSize(1 << 11), // not used for v3.
afpacket.OptBlockSize(1 << 20),
afpacket.OptNumBlocks(p.afpacketRecvOpts.BlockMB),
afpacket.OptPollTimeout(p.afpacketRecvOpts.PollTimeout),
afpacket.OptInterface(p.afpacketRecvOpts.Iface),
afpacket.SocketRaw,
afpacket.TPacketVersion3,
}
tpacket, err := afpacket.NewTPacket(options...)
if err != nil {
fmt.Fprintf(os.Stderr, "kping recv: %d(%d) NewTPacket failed: %v\n", index, recvParallel, err)
return
}
defer tpacket.Close()
// bpf filter
filter := fmt.Sprintf("ip and dst %s and icmp[icmptype] = icmp-echoreply and icmp[4:2] >= %d and icmp[6:2] >= %d", p.sourceIP, icmpIDSeqInitNum, icmpIDSeqInitNum)
pcapBPF, err := pcap.CompileBPFFilter(layers.LinkTypeEthernet, int(p.size+60), filter)
if err != nil {
fmt.Fprintf(os.Stderr, "kping recv: %d(%d) CompileBPFFilter failed: %v\n", index, recvParallel, err)
return
}
bpfIns := []bpf.RawInstruction{}
for _, ins := range pcapBPF {
bpfIns2 := bpf.RawInstruction{
Op: ins.Code,
Jt: ins.Jt,
Jf: ins.Jf,
K: ins.K,
}
bpfIns = append(bpfIns, bpfIns2)
}
if tpacket.SetBPF(bpfIns); err != nil {
fmt.Fprintf(os.Stderr, "kping recv: %d(%d) SetBPF failed: %v\n", index, recvParallel, err)
return
}
// LoadBalance Fanout
runtime.LockOSThread()
tpacket.SetFanout(afpacket.FanoutLoadBalance, uint16(os.Getpid()))
ipCount := 0
stime := time.Now()
L:
for {
select {
case <-p.sendDone:
break L
default:
}
data, ci, err := tpacket.ZeroCopyReadPacketData()
if err == io.EOF {
fmt.Fprintf(os.Stderr, "kping recv: %d(%d) NextPacket: io.EOF, break for loop\n", index, recvParallel)
break
} else if err != nil {
if err == afpacket.ErrTimeout || err == afpacket.ErrPoll {
// os.Sleep(100*time.Millisecond)
} else {
fmt.Fprintf(os.Stderr, "kping recv: %d(%d) NextPacket: unknown error: %v, ignored\n", index, recvParallel, err)
}
continue
}
/*
fmt.Fprintf(os.Stderr, "%s\n", data)
OUTPUT:
PACKET: 52 bytes, wire length 52 cap length 52 @ 2017-07-04 23:43:39.708919 +0800 CST
- Layer 1 (14 bytes) = Ethernet {Contents=[..14..] Payload=[..38..] SrcMAC=ee:ff:ff:ff:ff:ff DstMAC=00:16:3e:04:79:3e EthernetType=IPv4 Length=0}
- Layer 2 (20 bytes) = IPv4 {Contents=[..20..] Payload=[..18..] Version=4 IHL=5 TOS=20 Length=38 Id=13672 Flags= FragOffset=0 TTL=51 Protocol=ICMPv4 Checksum=63803 SrcIP=222.37.36.141 DstIP=115.28.227.80 Options=[] Padding=[]}
- Layer 3 (08 bytes) = ICMPv4 {Contents=[..8..] Payload=[..10..] TypeCode=EchoReply Checksum=51683 Id=10003 Seq=10001}
- Layer 4 (10 bytes) = Payload 10 byte(s)
*/
if ci.Length < 50 { // 50: Ethernet(14)+IPv4(20)+ICMPv4(8)+Payload(>=8)
fmt.Fprintf(os.Stderr, "kping recv: %d(%d) packet length %d < 50 Bytes, ignored: \n%s\n", index, recvParallel, ci.Length, data)
continue
}
ip := net.IPv4(data[26], data[27], data[28], data[29]).String()
bytes := data[34:]
/*
bytes[0]: type
bytes[1]: code
bytes[2:4]: checkSum
bytes[4:6]: id
bytes[6:8]: seq
bytes[8:16]: payload: timestamp
*/
seq := int(binary.BigEndian.Uint16(bytes[6:8]))
// calculate RTT
var nsec int64
for i := uint8(0); i < timeSliceLength; i++ {
nsec += int64(bytes[8 : 8+timeSliceLength][i]) << ((7 - i) * timeSliceLength)
}
sendTime := time.Unix(nsec/1000000000, nsec%1000000000)
rtt := ci.Timestamp.Sub(sendTime)
p.ipEventChan <- &ipEvent{
ip: ip,
seq: seq,
recvRTT: rtt,
}
ipCount++
}
fmt.Fprintf(os.Stderr, "kping recv: %d(%d) done, ipCount: %d, usedTime: %s\n", index, recvParallel, ipCount, time.Since(stime))
}