-
Notifications
You must be signed in to change notification settings - Fork 162
/
request.go
182 lines (157 loc) · 5.33 KB
/
request.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
// Copyright 2020 ETH Zurich, Anapaya Systems
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package e2e
import (
"time"
base "github.com/scionproto/scion/go/cs/reservation"
"github.com/scionproto/scion/go/lib/colibri/reservation"
"github.com/scionproto/scion/go/lib/serrors"
"github.com/scionproto/scion/go/lib/spath"
)
// Request is the base struct for any type of COLIBRI e2e request.
type Request struct {
base.RequestMetadata // information about the request (forwarding path)
ID reservation.E2EID // the ID this request refers to
Index reservation.IndexNumber // the index this request refers to
Timestamp time.Time // the mandatory timestamp
}
// NewRequest constructs the e2e Request type.
func NewRequest(ts time.Time, id *reservation.E2EID, idx reservation.IndexNumber,
path *spath.Path) (*Request, error) {
metadata, err := base.NewRequestMetadata(path)
if err != nil {
return nil, serrors.WrapStr("new segment request", err)
}
if id == nil {
return nil, serrors.New("new e2e request with nil ID")
}
return &Request{
RequestMetadata: *metadata,
ID: *id,
Index: idx,
Timestamp: ts,
}, nil
}
// SetupRequest represents all possible e2e setup requests.
type SetupRequest interface {
IsSuccessful() bool
GetCommonSetupReq() *SetupReq // return the underlying basic SetupReq (common for all)
}
// SetupReq is an e2e setup/renewal request, that has been so far accepted.
type SetupReq struct {
Request
SegmentRsvs []reservation.SegmentID
SegmentRsvASCount []uint8 // how many ASes per segment reservation
RequestedBW reservation.BWCls
AllocationTrail []reservation.BWCls
totalASCount int
currentASSegmentRsvIndex int // the index in SegmentRsv for the current AS
isTransfer bool
}
// NewSetupRequest creates and initializes an e2e setup request common for both success and failure.
func NewSetupRequest(r *Request, segRsvs []reservation.SegmentID, segRsvCount []uint8,
requestedBW reservation.BWCls, allocTrail []reservation.BWCls) (*SetupReq, error) {
if len(segRsvs) != len(segRsvCount) || len(segRsvs) == 0 {
return nil, serrors.New("e2e setup request invalid", "seg_rsv_len", len(segRsvs),
"seg_rsv_count_len", len(segRsvCount))
}
totalASCount := 0
currASindex := -1
isTransfer := false
n := len(allocTrail) - 1
for i, c := range segRsvCount {
totalASCount += int(c)
n -= int(c) - 1
if i == len(segRsvCount)-1 {
n-- // the last segment spans 1 more AS
}
if n < 0 && currASindex < 0 {
currASindex = i
isTransfer = i < len(segRsvCount)-1 && n == -1 // dst AS is no transfer
}
}
totalASCount -= len(segRsvCount) - 1
if currASindex < 0 {
return nil, serrors.New("error initializing e2e request",
"alloc_trail_len", len(allocTrail), "seg_rsv_count", segRsvCount)
}
return &SetupReq{
Request: *r,
SegmentRsvs: segRsvs,
SegmentRsvASCount: segRsvCount,
RequestedBW: requestedBW,
AllocationTrail: allocTrail,
totalASCount: totalASCount,
currentASSegmentRsvIndex: currASindex,
isTransfer: isTransfer,
}, nil
}
// GetCommonSetupReq returns the pointer to the data structure.
func (r *SetupReq) GetCommonSetupReq() *SetupReq {
return r
}
func (r *SetupReq) Transfer() bool {
return r.isTransfer
}
type PathLocation int
const (
Source PathLocation = iota
Transit
Destination
)
func (r *SetupReq) Location() PathLocation {
switch len(r.AllocationTrail) {
case 0:
return Source
case r.totalASCount:
return Destination
default:
return Transit
}
}
// SegmentRsvIDsForThisAS returns the segment reservation ID this AS belongs to. Iff this
// AS is a transfer AS (stitching point), there will be two reservation IDs returned, in the
// order of traversal.
func (r *SetupReq) SegmentRsvIDsForThisAS() []reservation.SegmentID {
indices := make([]reservation.SegmentID, 1, 2)
indices[0] = r.SegmentRsvs[r.currentASSegmentRsvIndex]
if r.isTransfer {
indices = append(indices, r.SegmentRsvs[r.currentASSegmentRsvIndex+1])
}
return indices
}
// SetupReqSuccess is a successful e2e setup request traveling along the reservation path.
type SetupReqSuccess struct {
SetupReq
Token reservation.Token
}
var _ SetupRequest = (*SetupReqSuccess)(nil)
// IsSuccessful returns true.
func (s *SetupReqSuccess) IsSuccessful() bool {
return true
}
// SetupReqFailure is a failed e2e setup request also traveling along the reservation path.
type SetupReqFailure struct {
SetupReq
ErrorCode uint8
}
var _ SetupRequest = (*SetupReqFailure)(nil)
// IsSuccessful returns false, as this is a failed setup.
func (s *SetupReqFailure) IsSuccessful() bool {
return false
}
// CleanupReq is a cleaup request for an e2e index.
type CleanupReq struct {
Request
}