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

Feature/upload metrics #7

Merged
merged 4 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
107 changes: 107 additions & 0 deletions Utils/addr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package util

import (
"context"
"fmt"
"net"
"os"
"time"

"github.com/phayes/freeport"
"github.com/scionproto/scion/go/lib/addr"
"github.com/scionproto/scion/go/lib/sciond"
"github.com/scionproto/scion/go/lib/snet"
"github.com/scionproto/scion/go/lib/sock/reliable"
)

// findAnyHostInLocalAS returns the IP address of some (infrastructure) host in the local AS.
func findAnyHostInLocalAS(ctx context.Context, sciondConn sciond.Connector) (*net.UDPAddr, error) {
addr, err := sciond.TopoQuerier{Connector: sciondConn}.UnderlayAnycast(ctx, addr.SvcCS)
if err != nil {
return nil, err
}
return addr, nil
}

func findSciond(ctx context.Context) (sciond.Connector, error) {
address, ok := os.LookupEnv("SCION_DAEMON_ADDRESS")
if !ok {
address = sciond.DefaultAPIAddress
}
sciondConn, err := sciond.NewService(address).Connect(ctx)
if err != nil {
return nil, fmt.Errorf("unable to connect to SCIOND at %s (override with SCION_DAEMON_ADDRESS): %w", address, err)
}
return sciondConn, nil
}

func findDispatcher() (reliable.Dispatcher, error) {
path, err := findDispatcherSocket()
if err != nil {
return nil, err
}
dispatcher := reliable.NewDispatcher(path)
return dispatcher, nil
}

func findDispatcherSocket() (string, error) {
path, ok := os.LookupEnv("SCION_DISPATCHER_SOCKET")
if !ok {
path = reliable.DefaultDispPath
}

if err := statSocket(path); err != nil {
return "", fmt.Errorf("error looking for SCION dispatcher socket at %s (override with SCION_DISPATCHER_SOCKET): %w", path, err)
}
return path, nil
}

func statSocket(path string) error {
fileinfo, err := os.Stat(path)
if err != nil {
return err
}
if !isSocket(fileinfo.Mode()) {
return fmt.Errorf("%s is not a socket (mode: %s)", path, fileinfo.Mode())
}
return nil
}

func isSocket(mode os.FileMode) bool {
return mode&os.ModeSocket != 0
}

func GetLocalHost() (*net.UDPAddr, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
sciondConn, err := findSciond(ctx)
if err != nil {
return nil, err
}
hostInLocalAS, err := findAnyHostInLocalAS(ctx, sciondConn)
if err != nil {
return nil, err
}
return hostInLocalAS, nil
}

func GetDefaultLocalAddr() (*snet.UDPAddr, error) {
netAddr, err := GetLocalHost()
if err != nil {
return nil, err
}
netAddr.Port, _ = freeport.GetFreePort()
sciondConn, err := findSciond(context.Background())
if err != nil {
return nil, err
}
localIA, err := sciondConn.LocalIA(context.Background())
if err != nil {
return nil, err
}
sAddr := &snet.UDPAddr{
IA: localIA,
Host: netAddr,
}
return sAddr, nil
}
14 changes: 13 additions & 1 deletion Utils/numbers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package util

import "math/rand"
import (
"encoding/base32"
"math/rand"
)

func EnsureBetweenRandom(val, min, max int) int {
if val > max || val < min {
Expand All @@ -10,3 +13,12 @@ func EnsureBetweenRandom(val, min, max int) int {
return val

}

func RandStringBytes(length int) string {
randomBytes := make([]byte, 32)
_, err := rand.Read(randomBytes)
if err != nil {
panic(err)
}
return base32.StdEncoding.EncodeToString(randomBytes)[:length]
}
18 changes: 15 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"time"

util "github.com/netsys-lab/bittorrent-over-scion/Utils"
"github.com/netsys-lab/bittorrent-over-scion/bitfield"
"github.com/netsys-lab/bittorrent-over-scion/config"
"github.com/netsys-lab/bittorrent-over-scion/dht_node"
Expand Down Expand Up @@ -136,16 +137,27 @@ func (mp *MPClient) DialAndWaitForConnectBack(
infoHash [20]byte,
discoveryConfig *config.PeerDiscoveryConfig,
node *dht_node.DhtNode) ([]*Client, error) {
var err error

address, err := snet.ParseUDPAddr(peer.Addr)
if err != nil {
return nil, err
}

localSocketAddr, err := snet.ParseUDPAddr(local)
if err != nil {
return nil, err
var localSocketAddr *snet.UDPAddr

if local == "" {
localSocketAddr, err = util.GetDefaultLocalAddr()
if err != nil {
return nil, err
}
} else {
localSocketAddr, err = snet.ParseUDPAddr(local)
if err != nil {
return nil, err
}
}

localSocketAddr.Host.Port, _ = freeport.GetFreePort()
localSocketAddrStr := localSocketAddr.String()

Expand Down
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ require (
github.com/lucas-clemente/quic-go v0.21.1
github.com/netsec-ethz/scion-apps v0.3.1-0.20210924130723-be84cbd98c1f
github.com/netsys-lab/dht v0.1.18
github.com/netsys-lab/scion-path-discovery v0.1.0
github.com/netsys-lab/scion-path-discovery v1.0.1-0.20220308120244-5d3dfbea5e05
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2
github.com/scionproto/scion v0.6.0
github.com/sirupsen/logrus v1.6.0
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.7.0
)

Expand Down Expand Up @@ -51,15 +51,14 @@ require (
github.com/grpc-ecosystem/grpc-opentracing v0.0.0-20180507213350-8e809c8a8645 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/inconshreveable/log15 v0.0.0-20180818164646-67afb5ed74ec // indirect
github.com/johannwagner/scion-optimized-connection v0.3.0 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect
github.com/marten-seemann/qtls-go1-15 v0.1.4 // indirect
github.com/marten-seemann/qtls-go1-16 v0.1.3 // indirect
github.com/marten-seemann/qtls-go1-17 v0.1.0-beta.1.2 // indirect
github.com/mattn/go-colorable v0.1.6 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/netsec-ethz/rains v0.2.0 // indirect
github.com/netsys-lab/scion-optimized-connection v0.4.2-0.20220107124242-cc4b4825db7f // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pelletier/go-toml v1.9.3 // indirect
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,6 @@ github.com/jackpal/bencode-go v1.0.0/go.mod h1:5FSBQ74yhCl5oQ+QxRPYzWMONFnxbL68/
github.com/jellevandenhooff/dkim v0.0.0-20150330215556-f50fe3d243e1/go.mod h1:E0B/fFc00Y+Rasa88328GlI/XbtyysCtTHZS8h7IrBU=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/johannwagner/scion-optimized-connection v0.3.0 h1:jApU/wPmoFxWY03lb4w8zqD1LxyqicZevdWz2AwUyeM=
github.com/johannwagner/scion-optimized-connection v0.3.0/go.mod h1:7lcniBD5FKl7FcE3iBtPrs9KuJp3CBhs5fO22eDI2Jg=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
Expand All @@ -491,7 +489,6 @@ github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kormat/fmt15 v0.0.0-20181112140556-ee69fecb2656/go.mod h1:8fpYQL5jskFnAq4zE2UpspqEVHuTjurptCxHPpdoBgM=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
Expand Down Expand Up @@ -593,8 +590,10 @@ github.com/netsec-ethz/scion-apps v0.3.1-0.20210924130723-be84cbd98c1f h1:TElJhH
github.com/netsec-ethz/scion-apps v0.3.1-0.20210924130723-be84cbd98c1f/go.mod h1:38N/jVYtBJsjCyztk+vqAcXpzEgJMirmSGzB7w4oCGQ=
github.com/netsys-lab/dht v0.1.18 h1:15IIZwb0Is0z3McM9MOCuRqeMEOp0DKHdQxrUO1glkI=
github.com/netsys-lab/dht v0.1.18/go.mod h1:6QHwOc+4cUK+PO9spTWjSUHT5eYHKgVAmCbDjPYinzU=
github.com/netsys-lab/scion-path-discovery v0.1.0 h1:fT+xx29RBDF+1sayw0CFiMx74nZ2tXC6gZwzDLu0Kdc=
github.com/netsys-lab/scion-path-discovery v0.1.0/go.mod h1:w/hhxTXo77tGESkZtC7U5axwRF13LoSFLHw5ueug1wY=
github.com/netsys-lab/scion-optimized-connection v0.4.2-0.20220107124242-cc4b4825db7f h1:r1foN6U7eOsD+73zBHFNUZO+1BEWu7GkcRZuu1L/gw0=
github.com/netsys-lab/scion-optimized-connection v0.4.2-0.20220107124242-cc4b4825db7f/go.mod h1:yL9AhbhrOEcoQyg7xJSLEP9TvHw4nrY1mdG0V60MF9M=
github.com/netsys-lab/scion-path-discovery v1.0.1-0.20220308120244-5d3dfbea5e05 h1:OPS87tk7F+D/0w9/CSf02d38hqGiz/4bkcKM1y0A5kk=
github.com/netsys-lab/scion-path-discovery v1.0.1-0.20220308120244-5d3dfbea5e05/go.mod h1:MLcbOPImZSXSWOdUAZuyTa4IU7e3edk6CvDHVWH4cRI=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
Expand Down Expand Up @@ -790,8 +789,9 @@ github.com/shurcooL/users v0.0.0-20180125191416-49c67e49c537/go.mod h1:QJTqeLYED
github.com/shurcooL/webdavfs v0.0.0-20170829043945-18c3829fa133/go.mod h1:hKmq5kWdCj2z2KEozexVbfEZIWiTjhE0+UjmZgPqehw=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/assertions v0.0.0-20190215210624-980c5ac6f3ac h1:wbW+Bybf9pXxnCFAOWZTqkRjAc7rAIwo2e1ArUhiHxg=
github.com/smartystreets/assertions v0.0.0-20190215210624-980c5ac6f3ac/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
Expand Down
31 changes: 31 additions & 0 deletions server/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package server

import (
"fmt"
"time"

"github.com/netsys-lab/scion-path-discovery/packets"
)

type UploadConnMetrics struct {
ConnId string
Remote string
SessionId string
Metrics packets.PathMetrics
StartDate time.Time
EndDate time.Time
Closed bool
Path string
Duration time.Duration
}

func (m *UploadConnMetrics) GetCsv() string {
secs := int64((m.Duration * time.Second) - 3*time.Second)
bw := (m.Metrics.WrittenBytes * 8 / 1024 / 1024) / secs
// id;remote;sessionId;uploadBw;startDate;endDate;closed;path;duration;
return fmt.Sprintf("%s;%s;%s;%d;%s;%s;%t;%s;%d", m.ConnId, m.Remote, m.SessionId, bw, m.StartDate, m.EndDate, m.Closed, m.Path, m.Duration)
}

func (m *UploadConnMetrics) GetCsvHeader() string {
return "id;remote;sessionId;uploadBw;startDate;endDate;closed;path;duration;"
}
Loading