-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathlp.go
226 lines (191 loc) · 5.34 KB
/
lp.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
package ndn
import (
"bytes"
"encoding/binary"
"math/rand"
"strconv"
"github.com/usnistgov/ndn-dpdk/ndn/an"
"github.com/usnistgov/ndn-dpdk/ndn/tlv"
"github.com/zyedidia/generic/cache"
)
func lpIsCritical(typ uint32) bool {
return typ < 800 || typ > 959 || typ&0x03 != 0
}
const fragmentOverhead = 0 +
1 + 3 + // LpPacket TL
1 + 1 + 8 + // LpSeqNum
1 + 1 + 2 + // FragIndex
1 + 1 + 2 + // FragCount
1 + 3 + // LpPayload TL
0
// LpL3 contains layer 3 fields in NDNLPv2 header.
type LpL3 struct {
PitToken []byte
NackReason uint8
CongMark uint8
}
// Empty returns true if LpL3 has zero fields.
func (lph LpL3) Empty() bool {
return len(lph.PitToken) == 0 && lph.NackReason == an.NackNone && lph.CongMark == 0
}
func (lph LpL3) encode() (fields []tlv.Field) {
if len(lph.PitToken) > 0 {
fields = append(fields, tlv.TLVBytes(an.TtPitToken, lph.PitToken))
}
switch lph.NackReason {
case an.NackNone:
case an.NackUnspecified:
fields = append(fields, tlv.TLV(an.TtNack))
default:
fields = append(fields, tlv.TLV(an.TtNack, tlv.TLVNNI(an.TtNackReason, lph.NackReason)))
}
if lph.CongMark != 0 {
fields = append(fields, tlv.TLVNNI(an.TtCongestionMark, lph.CongMark))
}
return fields
}
func (lph *LpL3) inheritFrom(src LpL3) {
lph.PitToken = bytes.Clone(src.PitToken)
lph.CongMark = src.CongMark
}
// LpFragment represents an NDNLPv2 fragmented frame.
type LpFragment struct {
SeqNum uint64
FragIndex int
FragCount int
Header []byte // encoded NDNLPv2 L3 header fields
Payload []byte // LpPayload TLV-VALUE
}
var _ tlv.Fielder = LpFragment{}
func (frag LpFragment) String() string {
return strconv.FormatUint(frag.SeqNum, 16) + ":" + strconv.Itoa(frag.FragIndex) + ":" + strconv.Itoa(frag.FragCount)
}
// Field implements tlv.Fielder interface.
func (frag LpFragment) Field() tlv.Field {
if frag.FragIndex < 0 || frag.FragIndex >= frag.FragCount {
return tlv.FieldError(ErrFragment)
}
var seqNum [8]byte
binary.BigEndian.PutUint64(seqNum[:], frag.SeqNum)
return tlv.TLV(an.TtLpPacket,
tlv.TLVBytes(an.TtLpSeqNum, seqNum[:]),
tlv.TLVNNI(an.TtFragIndex, frag.FragIndex),
tlv.TLVNNI(an.TtFragCount, frag.FragCount),
tlv.Bytes(frag.Header),
tlv.TLVBytes(an.TtLpPayload, frag.Payload))
}
// LpFragmenter splits Packet into fragments.
type LpFragmenter struct {
nextSeqNum uint64
room int
}
// Fragment fragments a packet.
func (fragmenter *LpFragmenter) Fragment(full *Packet) (frags []*Packet, e error) {
header, payload, e := full.encodeL3()
if e != nil {
return nil, e
}
sizeofFirstFragment := fragmenter.room - len(header)
if sizeofPayload := len(payload); sizeofFirstFragment > sizeofPayload { // no fragmentation necessary
return []*Packet{full}, nil
}
if sizeofFirstFragment <= 0 { // MTU is too small to fit this packet
return nil, ErrFragment
}
var first Packet
first.Lp = full.Lp
first.Fragment = &LpFragment{
Header: header,
Payload: payload[:sizeofFirstFragment],
}
frags = append(frags, &first)
for offset, nextOffset := sizeofFirstFragment, 0; offset < len(payload); offset = nextOffset {
nextOffset = offset + fragmenter.room
if nextOffset > len(payload) {
nextOffset = len(payload)
}
var frag Packet
frag.Fragment = &LpFragment{
Payload: payload[offset:nextOffset],
}
frags = append(frags, &frag)
}
for i, frag := range frags {
frag.Fragment.SeqNum = fragmenter.nextSeqNum
fragmenter.nextSeqNum++
frag.Fragment.FragIndex = i
frag.Fragment.FragCount = len(frags)
}
return frags, nil
}
// NewLpFragmenter creates a LpFragmenter.
func NewLpFragmenter(mtu int) *LpFragmenter {
var fragmenter LpFragmenter
fragmenter.nextSeqNum = rand.Uint64()
fragmenter.room = mtu - fragmentOverhead
return &fragmenter
}
// LpReassembler reassembles fragments.
type LpReassembler struct {
pms *cache.Cache[uint64, *lpPartialPacket]
}
// Accept processes a fragment.
// pkt.Fragment must not be nil.
func (reass *LpReassembler) Accept(pkt *Packet) (full *Packet, e error) {
seq0 := pkt.Fragment.SeqNum - uint64(pkt.Fragment.FragIndex)
pp, _ := reass.pms.Get(seq0)
if pp == nil {
pp = &lpPartialPacket{}
reass.pms.Put(seq0, pp)
}
full, discard, e := pp.Accept(pkt)
if discard {
reass.pms.Remove(seq0)
}
return full, e
}
// NewLpReassembler creates a LpReassembler.
func NewLpReassembler(capacity int) *LpReassembler {
return &LpReassembler{
pms: cache.New[uint64, *lpPartialPacket](capacity),
}
}
type lpPartialPacket struct {
lpl3 LpL3
buffer [][]byte
accepted int
}
func (pp *lpPartialPacket) Accept(pkt *Packet) (full *Packet, discard bool, e error) {
if pp.accepted == 0 {
pp.buffer = make([][]byte, pkt.Fragment.FragCount)
pp.acceptOne(pkt)
return nil, false, nil
}
if pkt.Fragment.FragCount != len(pp.buffer) {
return nil, true, ErrFragment
}
if pp.buffer[pkt.Fragment.FragIndex] != nil {
return nil, false, ErrFragment
}
pp.acceptOne(pkt)
if pp.accepted == len(pp.buffer) {
full, e = pp.reassemble()
return full, true, e
}
return nil, false, nil
}
func (pp *lpPartialPacket) acceptOne(pkt *Packet) {
if pkt.Fragment.FragIndex == 0 {
pp.lpl3 = pkt.Lp
}
pp.buffer[pkt.Fragment.FragIndex] = pkt.Fragment.Payload
pp.accepted++
}
func (pp *lpPartialPacket) reassemble() (full *Packet, e error) {
full = &Packet{Lp: pp.lpl3}
payload := bytes.Join(pp.buffer, nil)
if e = full.decodePayload(payload); e != nil {
return nil, e
}
return full, nil
}