forked from usnistgov/ndn-dpdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathping.go
145 lines (137 loc) · 3.58 KB
/
ping.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
package main
import (
"context"
"fmt"
"log"
"math/rand"
"sync/atomic"
"time"
"github.com/urfave/cli/v2"
"github.com/usnistgov/ndn-dpdk/ndn"
"github.com/usnistgov/ndn-dpdk/ndn/endpoint"
)
func init() {
var name string
var wantAdvertise, wantSign bool
var payloadLen int
defineCommand(&cli.Command{
Name: "pingserver",
Usage: "Reachability test server: reply to every Interest under a prefix.",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Usage: "producer name `prefix`",
Destination: &name,
Required: true,
},
&cli.BoolFlag{
Name: "advertise",
Usage: "whether to advertise/register prefix",
Value: true,
Destination: &wantAdvertise,
},
&cli.IntFlag{
Name: "payload",
Usage: "payload length",
Destination: &payloadLen,
},
&cli.BoolFlag{
Name: "signed",
Usage: "enable packet signing (SigSha256)",
Destination: &wantSign,
},
},
Before: openUplink,
Action: func(c *cli.Context) error {
payload := make([]byte, payloadLen)
rand.New(rand.NewSource(rand.Int63())).Read(payload)
var signer ndn.Signer
if wantSign {
signer = ndn.DigestSigning
}
_, e := endpoint.Produce(c.Context, endpoint.ProducerOptions{
Prefix: ndn.ParseName(name),
NoAdvertise: !wantAdvertise,
Handler: func(ctx context.Context, interest ndn.Interest) (ndn.Data, error) {
log.Print(interest)
return ndn.MakeData(interest, payload), nil
},
DataSigner: signer,
})
if e != nil {
return e
}
<-c.Context.Done()
return nil
},
})
}
func init() {
var name string
var interval, lifetime time.Duration
var wantVerify bool
defineCommand(&cli.Command{
Name: "pingclient",
Usage: "Reachability test client: periodically send Interest under a prefix.",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Usage: "consumer name `prefix`",
Destination: &name,
Required: true,
},
&cli.DurationFlag{
Name: "interval",
Usage: "the `interval` between Interests",
Value: 100 * time.Millisecond,
Destination: &interval,
},
&cli.DurationFlag{
Name: "lifetime",
Usage: "Interest `lifetime`",
Value: 1000 * time.Millisecond,
Destination: &lifetime,
},
&cli.BoolFlag{
Name: "verified",
Usage: "enable packet verification (SigSha256)",
Destination: &wantVerify,
},
},
Before: openUplink,
Action: func(c *cli.Context) error {
var verifier ndn.Verifier
if wantVerify {
verifier = ndn.DigestSigning
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ticker := time.NewTicker(interval)
defer ticker.Stop()
seqNum := rand.Uint64()
var nData, nErrors atomic.Int64
for {
select {
case <-c.Context.Done():
return nil
case timestamp := <-ticker.C:
go func(t0 time.Time, s uint64) {
interest := ndn.MakeInterest(fmt.Sprintf("%s/%016X", name, s), ndn.MustBeFreshFlag, lifetime)
_, e := endpoint.Consume(ctx, interest, endpoint.ConsumerOptions{
Verifier: verifier,
})
rtt := time.Since(t0)
if e == nil {
nDataL, nErrorsL := nData.Add(1), nErrors.Load()
log.Printf("%6.2f%% D %016X %6dus", 100*float64(nDataL)/float64(nDataL+nErrorsL), s, rtt.Microseconds())
} else {
nDataL, nErrorsL := nData.Load(), nErrors.Add(1)
log.Printf("%6.2f%% E %016X %v", 100*float64(nDataL)/float64(nDataL+nErrorsL), s, e)
}
}(timestamp, seqNum)
seqNum++
}
}
},
})
}