Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cli command visor ping and test #1405

Merged
merged 31 commits into from
Dec 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
1820d44
Fix Latency in network stats
ersonp Nov 2, 2022
af45c90
Add visor ping subcommand
ersonp Nov 2, 2022
d942ec8
Fix summarie in proc after the Latency change
ersonp Nov 2, 2022
c644369
test
ersonp Nov 7, 2022
d442f4b
Add ping app module
ersonp Dec 2, 2022
03ca1bf
Add ping method to networker interface
ersonp Dec 2, 2022
3a9fae0
Add PingRoute method to router
ersonp Dec 2, 2022
4f0d064
Use Ping instead of Dial in API
ersonp Dec 2, 2022
3105f8f
Fix RulesMap in setup node
ersonp Dec 5, 2022
eeaa8a1
Fix noise message authentication error
ersonp Dec 9, 2022
ed8274b
Remove test logs
ersonp Dec 9, 2022
5bb48a7
Revert test changes
ersonp Dec 9, 2022
697ce71
Update Ping usage in dmsg network
ersonp Dec 9, 2022
9115937
Check if pk to be ping is in the route hops
ersonp Dec 9, 2022
41555a6
Split TestRouting into DialPing, Ping and StopPing
ersonp Dec 12, 2022
6c3612b
Linting
ersonp Dec 12, 2022
164354d
Add ping latency logic
ersonp Dec 12, 2022
80635d0
Add pk check
ersonp Dec 12, 2022
95c5701
Fix DialPing by waiting for atleast one tpm to initilize
ersonp Dec 12, 2022
2966e0b
Fix handlePingConn
ersonp Dec 12, 2022
13a32a7
Update ping subcommand
ersonp Dec 12, 2022
c13a0b4
Fix listening on close conn error on shutdown
ersonp Dec 12, 2022
3e36ede
Minor initilization fix
ersonp Dec 12, 2022
9df4573
change ping command for get arbitrary data size
mrpalide Dec 20, 2022
4325b56
Merge branch 'develop' into feat/visor-ping
mrpalide Dec 20, 2022
99fc314
fix rpcClient errors in new way
mrpalide Dec 20, 2022
3c70520
add `skywire-cli visor test` command
mrpalide Dec 20, 2022
2376c2b
improve logic
mrpalide Dec 20, 2022
02c8268
set limit on packet size in ping to 2KB
mrpalide Dec 27, 2022
fe53441
Add PingSizeMsg
ersonp Dec 27, 2022
e62c1b1
Fix ping data limit
ersonp Dec 27, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions cmd/skywire-cli/commands/visor/ping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Package clivisor cmd/skywire-cli/commands/visor/ping.go
package clivisor

import (
"fmt"
"os"

"github.com/spf13/cobra"

clirpc "github.com/skycoin/skywire/cmd/skywire-cli/commands/rpc"
"github.com/skycoin/skywire/cmd/skywire-cli/internal"
"github.com/skycoin/skywire/pkg/visor"
)

var tries int
var pcktSize int

func init() {
RootCmd.AddCommand(pingCmd)
pingCmd.Flags().IntVarP(&tries, "tries", "t", 1, "Number of pings")
pingCmd.Flags().IntVarP(&pcktSize, "size", "s", 32, "Size of packet, in KB, default is 32KB")
RootCmd.AddCommand(testCmd)
testCmd.Flags().IntVarP(&tries, "tries", "t", 1, "Number of tests per public visors")
testCmd.Flags().IntVarP(&pcktSize, "size", "s", 32, "Size of packet, in KB, default is 32KB")
}

var pingCmd = &cobra.Command{
Use: "ping <pk>",
Short: "Ping the visor with given pk",
Long: "\n Creates a route with the provided pk as a hop and returns latency on the conn",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
pk := internal.ParsePK(cmd.Flags(), "pk", args[0])
pingConfig := visor.PingConfig{PK: pk, Tries: tries, PcktSize: pcktSize}
rpcClient, err := clirpc.Client(cmd.Flags())
if err != nil {
os.Exit(1)
}
err = rpcClient.DialPing(pingConfig)
internal.Catch(cmd.Flags(), err)

latencies, err := rpcClient.Ping(pingConfig)
internal.Catch(cmd.Flags(), err)

for _, latency := range latencies {
internal.PrintOutput(cmd.Flags(), fmt.Sprint(latency), fmt.Sprintf(fmt.Sprint(latency)+"\n"))
}
err = rpcClient.StopPing(pk)
internal.Catch(cmd.Flags(), err)
},
}

var testCmd = &cobra.Command{
Use: "test",
Short: "Test the visor with public visors on network",
Long: "\n Creates a route with public visors as a hop and returns latency on the conn",
Run: func(cmd *cobra.Command, args []string) {
pingConfig := visor.PingConfig{Tries: tries, PcktSize: pcktSize}
rpcClient, err := clirpc.Client(cmd.Flags())
if err != nil {
os.Exit(1)
}
results, err := rpcClient.TestVisor(pingConfig)
internal.Catch(cmd.Flags(), err)
for i, result := range results {
internal.PrintOutput(cmd.Flags(), result, fmt.Sprintf("Test No. %d\nPK: %s\nMax: %s\nMin: %s\nMean: %s\n\n", i+1, result.PK, result.Max, result.Min, result.Mean))
}
},
}
8 changes: 8 additions & 0 deletions pkg/app/appnet/dmsg_networker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package appnet

import (
"context"
"fmt"
"net"

"github.com/skycoin/dmsg/pkg/dmsg"

"github.com/skycoin/skywire-utilities/pkg/cipher"
)

// DmsgNetworker implements `Networker` for dmsg network.
Expand All @@ -25,6 +28,11 @@ func (n *DmsgNetworker) Dial(addr Addr) (net.Conn, error) {
return n.DialContext(context.Background(), addr)
}

// Ping dials remote `addr` via dmsg network.
func (n *DmsgNetworker) Ping(pk cipher.PubKey, addr Addr) (net.Conn, error) {
return nil, fmt.Errorf("Ping not available on dmsg network")
}

// DialContext dials remote `addr` via dmsg network with context.
func (n *DmsgNetworker) DialContext(ctx context.Context, addr Addr) (net.Conn, error) {
remote := dmsg.Addr{
Expand Down
45 changes: 43 additions & 2 deletions pkg/app/appnet/mock_networker.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion pkg/app/appnet/networker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
"errors"
"net"
"sync"

"github.com/skycoin/skywire-utilities/pkg/cipher"
)

//go:generate mockery -name Networker -case underscore -inpkg
//go:generate mockery --name Networker --case underscore --inpackage

var (
// ErrNoSuchNetworker is being returned when there's no suitable networker.
Expand Down Expand Up @@ -63,6 +65,7 @@ func ClearNetworkers() {
// Networker defines basic network operations, such as Dial/Listen.
type Networker interface {
Dial(addr Addr) (net.Conn, error)
Ping(pk cipher.PubKey, addr Addr) (net.Conn, error)
DialContext(ctx context.Context, addr Addr) (net.Conn, error)
Listen(addr Addr) (net.Listener, error)
ListenContext(ctx context.Context, addr Addr) (net.Listener, error)
Expand All @@ -73,6 +76,15 @@ func Dial(addr Addr) (net.Conn, error) {
return DialContext(context.Background(), addr)
}

// Ping dials the remote `addr`.
func Ping(pk cipher.PubKey, addr Addr) (net.Conn, error) {
n, err := ResolveNetworker(addr.Net)
if err != nil {
return nil, err
}
return n.Ping(pk, addr)
}

// DialContext dials the remote `addr` with the context.
func DialContext(ctx context.Context, addr Addr) (net.Conn, error) {
n, err := ResolveNetworker(addr.Net)
Expand Down
31 changes: 30 additions & 1 deletion pkg/app/appnet/skywire_networker.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/sirupsen/logrus"

"github.com/skycoin/skywire-utilities/pkg/cipher"
"github.com/skycoin/skywire-utilities/pkg/netutil"
"github.com/skycoin/skywire/pkg/router"
"github.com/skycoin/skywire/pkg/routing"
Expand All @@ -20,6 +21,8 @@ import (
var (
// ErrPortAlreadyBound is being returned when the desired port is already bound to.
ErrPortAlreadyBound = errors.New("port already bound")
// ErrConnClosed is being returned when we listen on a closed conn.
ErrConnClosed = errors.New("listening on closed connection")
)

// SkywireNetworker implements `Networker` for skynet.
Expand Down Expand Up @@ -70,6 +73,32 @@ func (r *SkywireNetworker) DialContext(ctx context.Context, addr Addr) (conn net
}, nil
}

// Ping dials remote `addr` via `skynet` with context.
func (r *SkywireNetworker) Ping(pk cipher.PubKey, addr Addr) (net.Conn, error) {
ctx := context.Background()
localPort, freePort, err := r.porter.ReserveEphemeral(ctx, nil)
if err != nil {
return nil, err
}

// ensure ports are freed on error.
defer func() {
if err != nil {
freePort()
}
}()
conn, err := r.r.PingRoute(ctx, pk, routing.Port(localPort), addr.Port, router.DefaultDialOptions())
if err != nil {
return nil, err
}

return &SkywireConn{
Conn: conn,
nrg: conn.(*router.NoiseRouteGroup),
freePort: freePort,
}, nil
}

// Listen starts listening on local `addr` in the skynet.
func (r *SkywireNetworker) Listen(addr Addr) (net.Listener, error) {
return r.ListenContext(context.Background(), addr)
Expand Down Expand Up @@ -181,7 +210,7 @@ type skywireListener struct {
func (l *skywireListener) Accept() (net.Conn, error) {
conn, ok := <-l.connsCh
if !ok {
return nil, errors.New("listening on closed connection")
return nil, ErrConnClosed
}

return &SkywireConn{
Expand Down
6 changes: 3 additions & 3 deletions pkg/app/appserver/proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,10 +445,10 @@ func (p *Proc) ConnectionsSummary() []ConnectionSummary {
})
return true
}

summaries = append(summaries, ConnectionSummary{
IsAlive: skywireConn.IsAlive(),
Latency: skywireConn.Latency(),
IsAlive: skywireConn.IsAlive(),
// Latency in summary is expected to be in ms and not ns so we change the base to ms
Latency: time.Duration(skywireConn.Latency().Milliseconds()),
UploadSpeed: skywireConn.UploadSpeed(),
DownloadSpeed: skywireConn.DownloadSpeed(),
BandwidthSent: skywireConn.BandwidthSent(),
Expand Down
46 changes: 43 additions & 3 deletions pkg/router/mock_router.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/router/network_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func (s *networkStats) SetLatency(latency uint32) {

func (s *networkStats) Latency() time.Duration {
latencyMs := atomic.LoadUint32(&s.latency)

return time.Duration(latencyMs)
// the latency is store in uint32 of millisecond but time.Duration takes nanosecond
return time.Duration(latencyMs * uint32(time.Millisecond.Nanoseconds()))
}

func (s *networkStats) SetUploadSpeed(speed uint32) {
Expand Down
Loading