forked from ekr/minq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
measurement.go
227 lines (189 loc) · 6.25 KB
/
measurement.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
Package minq is a minimal implementation of QUIC, as documented at
https://quicwg.github.io/. Minq partly implements draft-04.
*/
package minq
import (
// "fmt"
"time"
)
const (
latencySpinShift = 6
latencySpinMask = ((1 << (latencySpinShift + 1)) | (1 << latencySpinShift))
latencySpinMod = 4
latencyValidShift = 5
latencyValidMask = (1 << latencyValidShift)
blockingShift = 4
blockingMask = (1 << blockingShift)
latencyStatusShift = 2
latencyStatusMask = ((1 << (latencyStatusShift + 1)) | (1 << latencyStatusShift))
lossShift = 1
lossMask = (1 << lossShift)
latencyValidEdgeShift = 0
latencyValidEdgeMask = (1 << latencyValidEdgeShift)
)
const (
latencyRxTxDelayMax = 1 * time.Millisecond
)
const (
statusInvalid = 0x00
statusHandshake0 = 0x01
statusHandshake1 = 0x02
statusValid = 0x03
)
type MeasurementField uint8
/* Measurement data that will pass over the wire */
type MeasurementHeaderData struct{
latencySpin uint8 /* Two bit spin value */
latencyValid bool /* Single bit valid, old sematics */
blocking bool /* blocking bit */
latencyStatus uint8 /* Two bit valid signal */
loss bool /* Loss bit */
latencyValidEdge bool /* Single bit valid, only set on edge */
}
/* Store all (meta)data related to the measurement header field */
type MeasurementData struct {
hdrData MeasurementHeaderData
maxPacketNumber uint64
role uint8
latencyRxEdgeTime time.Time
lastRxLatencySpin uint8
generatingEdge bool
incommingLatencyStatus uint8
}
/* Encode the measurement header for transmission */
func (m *MeasurementHeaderData) encode() MeasurementField {
var field MeasurementField = 0x00
field |= MeasurementField(m.latencySpin << latencySpinShift)
if m.latencyValid {
field |= MeasurementField(1 << latencyValidShift)
}
if m.blocking {
field |= MeasurementField(1 << blockingShift)
}
field |= MeasurementField(m.latencyStatus << latencyStatusShift)
if m.latencyValidEdge{
field |= MeasurementField(1 << latencyValidEdgeShift)
}
if m.loss{
field |= MeasurementField(1 << lossShift)
}
return field
}
/* Decode a received measurement header */
func (m MeasurementField) decode() MeasurementHeaderData {
var measurementHeaderData MeasurementHeaderData
latencySpin := (uint8(m) & latencySpinMask) >> latencySpinShift
latencyValid := (uint8(m) & latencyValidMask) == latencyValidMask
blocking := (uint8(m) & blockingMask) == blockingMask
latencyStatus := (uint8(m) & latencyStatusMask) >> latencyStatusShift
loss := (uint8(m) & lossMask) == lossMask
latencyValidEdge := (uint8(m) & latencyValidEdgeMask) == latencyValidEdgeMask
measurementHeaderData = MeasurementHeaderData{
latencySpin,
latencyValid,
blocking,
latencyStatus,
loss,
latencyValidEdge,
}
return measurementHeaderData
}
/* Create a new (empty) measurement struct */
func newMeasurementData(role uint8) MeasurementData {
return MeasurementData{
MeasurementHeaderData{
0, // latencySpin
true, // latencyValid
false, // blocking
statusInvalid, // latencyStatus
false, // loss
true, // latencyValidEdge
},
0, // maxPacketNumber
role, // role
time.Now(), // latencyRxEdgeTime
0xff, // lastRxLatencySpin
false, // generatingEdge
statusInvalid, // incommingLatencyStatus
}
}
/* Perform measurement tasks to be executed on packet reception */
func (m *MeasurementData) incommingMeasurementTasks(hdr *packetHeader){
m.setOutgoingLatencySpin(hdr)
}
/* Perform measurement tasks to be executed when packet loss is experienced */
func (m *MeasurementData) lossMeasurementTasks(){
m.hdrData.loss = true
}
func (m *MeasurementData) outgoingMeasurementTasks(c *Connection) {
/* We are generating an edge on the outgoing spin signal
* so we have to see if it can be considered "valid" */
if m.generatingEdge {
rxTxDelta := time.Since(m.latencyRxEdgeTime)
/* If we are to late for sending a valid edge */
if rxTxDelta > latencyRxTxDelayMax {
m.hdrData.latencyValid = false
m.hdrData.latencyValidEdge = false
m.hdrData.latencyStatus = statusInvalid
/* If we can send a valid edge */
} else {
m.hdrData.latencyValid = true
m.hdrData.latencyValidEdge = true
m.hdrData.latencyStatus = m.incommingLatencyStatus + 1
if m.hdrData.latencyStatus > 3 {
m.hdrData.latencyStatus = 3
}
}
/* Set latencyvalid to true ONLY for the packet with
the spin edge */
} else {
m.hdrData.latencyStatus = statusInvalid
m.hdrData.latencyValidEdge = false
}
m.generatingEdge = false
/* We check if the outoing queues have frames needing transmit or not */
blocking := true
queues := [...][]frame{c.outputClearQ, c.outputProtectedQ}
for _, queue := range queues {
if blocking == false {
break
}
for _, frame := range queue {
if frame.needsTransmit {
blocking = false
break
}
}
}
m.hdrData.blocking = blocking
}
/* Look at the incomming LatencySpin, and determine what
* the outgoing one should be */
func (m *MeasurementData) setOutgoingLatencySpin(hdr *packetHeader){
/* Check if packet was received out of order. If so, ignore it */
if hdr.PacketNumber <= m.maxPacketNumber {
return
} else {
m.maxPacketNumber = hdr.PacketNumber
}
var receivedMeasurement MeasurementHeaderData
receivedMeasurement = hdr.Measurement.decode()
m.incommingLatencyStatus = receivedMeasurement.latencyStatus
/* This means we are about the generate an edge on our outgoing spinbit,
* so we have need to store the time this has happened, so we can later decide
* if the outgoing edge we create is "valid" */
if receivedMeasurement.latencySpin != m.lastRxLatencySpin {
m.latencyRxEdgeTime = time.Now()
m.generatingEdge = true
/* reset the loss bit */
m.hdrData.loss = false
}
/* Server echos back the latest LatencySpinBit seen */
if m.role == RoleServer{
m.hdrData.latencySpin = receivedMeasurement.latencySpin
} else {
m.hdrData.latencySpin = (receivedMeasurement.latencySpin + 1) % latencySpinMod
}
m.lastRxLatencySpin = receivedMeasurement.latencySpin
}