-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsend.go
More file actions
92 lines (89 loc) · 2.3 KB
/
send.go
File metadata and controls
92 lines (89 loc) · 2.3 KB
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
package kping
import (
"fmt"
"os"
"time"
"golang.org/x/net/icmp"
"golang.org/x/net/ipv4"
)
// batch send ICMP packets
func (p *kping) send(index int, addrBatchChan chan addrBatch) {
stime := time.Now()
// create ICMP Echo packet
t := make([]byte, p.size)
b := icmp.Echo{ID: icmpIDSeqInitNum + index, Data: t}
m := icmp.Message{
Type: ipv4.ICMPTypeEcho,
Code: 0,
Body: &b,
}
// message cache
wms := make([]message, 0, p.sendOpts.BatchSize)
L:
for {
var ab addrBatch
var ok bool
select {
case <-p.context.Done():
break L // send timeout
case ab, ok = <-addrBatchChan:
if !ok {
break L // send done
}
}
// get lock, at most one sent goroutine working
p.sendLock.Lock()
stime2 := time.Now()
b.Seq = icmpIDSeqInitNum + ab.seq
// fill icmp payload with current timestamp
nsec := time.Now().UnixNano()
for i := uint64(0); i < uint64(p.size); i++ {
if i < timeSliceLength {
t[i] = byte((nsec >> ((7 - i) * timeSliceLength)) & 0xff)
} else {
t[i] = 1
}
}
bytes, _ := (&m).Marshal(nil)
// reuse message cache
wms2 := wms[0:0:len(ab.addrs)]
for _, addr := range ab.addrs {
msg := message{
Buffers: [][]byte{bytes},
Addr: addr,
}
wms2 = append(wms2, msg)
}
var num int
var err error
for {
// blocking write multi messages
num, err = p.rawConn.writeBatch(wms2, 0)
if err != nil {
fmt.Fprintf(os.Stderr, "kping send: %d(%d), seq: %d, writeBatch failed: %v\n", index, p.sendOpts.Parallel, ab.seq, err)
continue
}
break
}
if num != len(wms2) {
fmt.Fprintf(os.Stderr, "kping send: %d(%d), seq: %d, writeBatch parted: %d(%d)\n", index, p.sendOpts.Parallel, ab.seq, len(wms2), num)
}
durTime := time.Since(stime2)
if durTime > 50*time.Millisecond {
fmt.Fprintf(os.Stderr, "kping send: %d(%d), seq: %d, writeBatch %d(%d), usedTime: %s\n", index, p.sendOpts.Parallel, ab.seq, len(wms2), num, durTime)
}
for _, msg := range wms2[0:num] {
addr := msg.Addr.String()
durTime := time.Since(stime2)
p.ipEventChan <- &ipEvent{
ip: addr,
seq: b.Seq,
sendDuration: durTime,
}
}
// wait a little time
time.Sleep(p.sendOpts.WaitTimeout)
p.sendLock.Unlock()
}
fmt.Fprintf(os.Stderr, "kping send: %d(%d) done, usedTime: %s\n", index, p.sendOpts.Parallel, time.Since(stime))
}