-
Notifications
You must be signed in to change notification settings - Fork 1
/
smp.go
209 lines (176 loc) · 5.43 KB
/
smp.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
package smp
import (
"time"
"github.com/netsys-lab/scion-path-discovery/packets"
lookup "github.com/netsys-lab/scion-path-discovery/pathlookup"
"github.com/netsys-lab/scion-path-discovery/pathselection"
"github.com/netsys-lab/scion-path-discovery/socket"
"github.com/scionproto/scion/go/lib/snet"
log "github.com/sirupsen/logrus"
)
type PanSocketOptions struct {
Transport string // "QUIC" | "SCION"
}
var defaultSocketOptions = &PanSocketOptions{
Transport: "SCION",
}
type PanSocket struct {
Peer *snet.UDPAddr
Local string
UnderlaySocket socket.UnderlaySocket
PathQualityDB pathselection.PathQualityDatabase
Mode string
Options *PanSocketOptions
MetricsInterval time.Duration
metricsTicker *time.Ticker
OnNewConnReceived chan packets.UDPConn
}
//
// Instantiates a new Multipath Peer Socket
// peer argument may be omitted for a socket waiting for an incoming connections
//
func NewPanSock(local string, peer *snet.UDPAddr, options *PanSocketOptions) *PanSocket {
sock := &PanSocket{
Peer: peer,
Local: local,
PathQualityDB: pathselection.NewInMemoryPathQualityDatabase(),
Options: defaultSocketOptions,
MetricsInterval: 1000 * time.Millisecond,
OnNewConnReceived: make(chan packets.UDPConn, 16),
}
if options != nil {
sock.Options = options
}
switch sock.Options.Transport {
case "QUIC":
sock.UnderlaySocket = socket.NewQUICSocket(local)
break
case "SCION":
sock.UnderlaySocket = socket.NewSCIONSocket(local)
break
}
return sock
}
func (mp *PanSocket) GetMetrics() []*packets.PathMetrics {
return packets.GetMetricsDB().GetBySocket(mp.UnderlaySocket.Local())
}
func (mp *PanSocket) AverageReadBandwidth() int64 {
return mp.AggregateMetrics().AverageReadBandwidth()
}
func (mp *PanSocket) AverageWriteBandwidth() int64 {
return mp.AggregateMetrics().AverageWriteBandwidth()
}
func (mp *PanSocket) AggregateMetrics() *packets.PathMetrics {
return mp.UnderlaySocket.AggregateMetrics()
}
func (mp *PanSocket) GetCurrentPathset() pathselection.PathSet {
conns := mp.UnderlaySocket.GetConnections()
ps := make([]snet.Path, 0)
for _, c := range conns {
p := c.GetPath()
ps = append(ps, *p)
}
return pathselection.WrapPathset(ps)
}
// Listen on the provided local address
// This call does not wait for incoming connections
// and shout be called for both, waiting and dialing sockets
//
func (mp *PanSocket) Listen() error {
err := mp.UnderlaySocket.Listen()
if err != nil {
return err
}
conns := mp.UnderlaySocket.GetConnections()
mp.PathQualityDB.SetConnections(conns)
log.Debugf("Listening on %s", mp.Local)
return nil
}
//
// This method waits until a remote MPPeerSock calls connect to this
// socket's local address
// A pathselection may be passed, which lets the socket dialing back to its remote
// (e.g. for server-side path selection)
// Since the MPPeerSock waits for only one incoming connection to determine a new peer
// it starts waiting for other connections (if no selection passed) and fires the
// OnConnectionsChange event for each new incoming connection
//
func (mp *PanSocket) WaitForPeerConnect() (*snet.UDPAddr, error) {
log.Debugf("[PanSocket] Waiting for incoming connection")
remote, err := mp.UnderlaySocket.WaitForDialIn()
if err != nil {
return nil, err
}
log.Debugf("[PanSocket] Accepted connection from %s", remote.String())
mp.Peer = remote
mp.PathQualityDB.UpdatePathQualities(remote, 1*time.Second)
mp.collectMetrics()
conns := mp.UnderlaySocket.GetConnections()
mp.PathQualityDB.SetConnections(conns)
return remote, err
}
func (mp *PanSocket) collectMetrics() {
mp.metricsTicker = time.NewTicker(mp.MetricsInterval)
go func() {
for {
select {
case <-mp.metricsTicker.C:
mp.PathQualityDB.UpdateMetrics()
break
// case <-mp.metricsChan:
// return
}
}
}()
}
func (mp *PanSocket) GetAvailablePaths() ([]snet.Path, error) {
return lookup.PathLookup(mp.Peer.String())
}
//
// Set Peer after instantiating the socket
// This does not connect automatically after changing the peer
//
func (mp *PanSocket) SetPeer(peer *snet.UDPAddr) {
mp.Peer = peer
}
// Could call dialPath for all paths. However, not the connections over included
// should be idled or closed here
func (mp *PanSocket) DialAll(pathAlternatives *pathselection.PathSet, options *socket.ConnectOptions) error {
opts := socket.DialOptions{}
if options != nil {
opts.SendAddrPacket = options.SendAddrPacket
}
conns, err := mp.UnderlaySocket.DialAll(*mp.Peer, pathAlternatives.Paths, opts)
if err != nil {
return err
}
log.Debugf("[PanSocket] Dialed all to %s, got %d connections", mp.Peer.String(), len(conns))
mp.PathQualityDB.SetConnections(conns)
mp.PathQualityDB.UpdatePathQualities(&pathAlternatives.Address, 1*time.Second)
return nil
}
// A first approach could be to open connections over all
// Paths to later reduce time effort for switching paths
func (mp *PanSocket) Connect(pathAlternatives *pathselection.PathSet, options *socket.ConnectOptions) error {
// TODO: Rethink default values here...
opts := &socket.ConnectOptions{}
if options == nil {
opts.SendAddrPacket = true
} else {
opts = options
}
var err error
err = mp.DialAll(pathAlternatives, opts)
if err != nil {
return err
}
if !opts.NoMetricsCollection {
mp.collectMetrics()
}
return nil
}
func (mp *PanSocket) Disconnect() []error {
errs := mp.UnderlaySocket.CloseAll()
mp.metricsTicker.Stop()
return errs
}