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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
package bgp
import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"github.com/golang/glog"
)
// PrefixSIDTLVInterface defines standard set of methods to handle Prefix SID attribute's TLVs
type PrefixSIDTLVInterface interface {
Len() int
DecodeFromBytes([]byte) error
Serialize() ([]byte, error)
String() string
MarshalJSON() ([]byte, error)
}
type PathAttributePrefixSID struct {
PathAttribute
TLVs []PrefixSIDTLVInterface
}
type PrefixSIDTLVType uint8
type PrefixSIDTLV struct {
Type PrefixSIDTLVType
Length uint16
}
func (s *PrefixSIDTLV) Len() int {
return int(s.Length) + tlvHdrLen
}
func (s *PrefixSIDTLV) Serialize(value []byte) ([]byte, error) {
if len(value) != int(s.Length) {
return nil, malformedAttrListErr("serialization failed: Prefix SID TLV malformed")
}
buf := make([]byte, tlvHdrLen+len(value))
binary.BigEndian.PutUint16(buf[:2], uint16(s.Type))
binary.BigEndian.PutUint16(buf[2:4], uint16(s.Length))
copy(buf[4:], value)
return buf, nil
}
func (s *PrefixSIDTLV) DecodeFromBytes(data []byte) ([]byte, error) {
if len(data) < tlvHdrLen {
return nil, malformedAttrListErr("decoding failed: Prefix SID TLV malformed")
}
s.Type = PrefixSIDTLVType(binary.BigEndian.Uint16(data[:2]))
s.Length = binary.BigEndian.Uint16(data[2:4])
if len(data) < s.Len() {
return nil, malformedAttrListErr("decoding failed: Prefix SID TLV malformed")
}
return data[tlvHdrLen:s.Len()], nil
}
type PrefixSIDAttribute struct {
PrefixSIDTLV
TLVs []PrefixSIDTLVInterface
}
func (p *PathAttributePrefixSID) DecodeFromBytes(data []byte, options ...*MarshallingOption) error {
tlvs, err := p.PathAttribute.DecodeFromBytes(data)
if err != nil {
return err
}
glog.Infof("><SB> tlvs: %+v", tlvs)
for len(tlvs) >= tlvHdrLen {
t := &PrefixSIDTLV{}
_, err := t.DecodeFromBytes(tlvs)
if err != nil {
return err
}
var tlv PrefixSIDTLVInterface
switch t.Type {
case 5:
tlv = &PrefixSIDType5{}
default:
tlvs = tlvs[t.Len():]
continue
}
if err := tlv.DecodeFromBytes(tlvs); err != nil {
return err
}
tlvs = tlvs[t.Len():]
p.TLVs = append(p.TLVs, tlv)
}
return nil
}
func (p *PathAttributePrefixSID) Serialize(options ...*MarshallingOption) ([]byte, error) {
buf := []byte{}
for _, tlv := range p.TLVs {
s, err := tlv.Serialize()
if err != nil {
return nil, err
}
buf = append(buf, s...)
}
return p.PathAttribute.Serialize(buf)
}
func (p *PathAttributePrefixSID) String() string {
var buf bytes.Buffer
for _, tlv := range p.TLVs {
buf.WriteString(fmt.Sprintf("%s ", tlv.String()))
}
return fmt.Sprintf("{LsAttributes: %s}", buf.String())
}
func (p *PathAttributePrefixSID) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Type BGPAttrType `json:"type"`
Flags BGPAttrFlag `json:"flags"`
PrefixSIDAttribute
}{
p.GetType(),
p.GetFlags(),
PrefixSIDAttribute{},
})
}
// PrefixSIDType5 defines the structure of
type PrefixSIDType5 struct {
PrefixSIDTLV
ServiceTLVs []PrefixSIDTLVInterface
}
func (s *PrefixSIDType5) Len() int {
return int(s.Length) + tlvHdrLen
}
func (s *PrefixSIDType5) Serialize() ([]byte, error) {
// if len(value) != int(s.Length) {
// return nil, malformedAttrListErr("serialization failed: Prefix SID TLV malformed")
// }
// buf := make([]byte, tlvHdrLen+len(value))
// binary.BigEndian.PutUint16(buf[:2], uint16(s.Type))
// binary.BigEndian.PutUint16(buf[2:4], uint16(s.Length))
// copy(buf[4:], value)
// return buf, nil
return nil, nil
}
func (s *PrefixSIDType5) DecodeFromBytes(data []byte) error {
if len(data) < tlvHdrLen {
return malformedAttrListErr("decoding failed: Prefix SID TLV malformed")
}
s.Type = PrefixSIDTLVType(binary.BigEndian.Uint16(data[:2]))
s.Length = binary.BigEndian.Uint16(data[2:4])
if len(data) < s.Len() {
return malformedAttrListErr("decoding failed: Prefix SID TLV malformed")
}
glog.Infof("><SB> PrefixSIDType5 %+v", data)
return nil
}
func (s *PrefixSIDType5) MarshalJSON() ([]byte, error) {
return nil, nil
}
func (s *PrefixSIDType5) String() string {
return ""
}
func (s *PrefixSIDType5) Extract() string {
return ""
}
type SRv6ServiceSubTLV struct {
PrefixSIDTLV
TLV []PrefixSIDTLVInterface
}
func (s *SRv6ServiceSubTLV) Len() int {
return int(s.Length) + tlvHdrLen
}
func (s *SRv6ServiceSubTLV) Serialize() ([]byte, error) {
// if len(value) != int(s.Length) {
// return nil, malformedAttrListErr("serialization failed: Prefix SID TLV malformed")
// }
// buf := make([]byte, tlvHdrLen+len(value))
// binary.BigEndian.PutUint16(buf[:2], uint16(s.Type))
// binary.BigEndian.PutUint16(buf[2:4], uint16(s.Length))
// copy(buf[4:], value)
// return buf, nil
return nil, nil
}
func (s *SRv6ServiceSubTLV) DecodeFromBytes(data []byte) error {
if len(data) < tlvHdrLen {
return malformedAttrListErr("decoding failed: Prefix SID TLV malformed")
}
s.Type = PrefixSIDTLVType(binary.BigEndian.Uint16(data[:2]))
s.Length = binary.BigEndian.Uint16(data[2:4])
if len(data) < s.Len() {
return malformedAttrListErr("decoding failed: Prefix SID TLV malformed")
}
return nil
}
func (s *SRv6ServiceSubTLV) MarshalJSON() ([]byte, error) {
return nil, nil
}
func (s *SRv6ServiceSubTLV) String() string {
return ""
}
// SRv6InformationSubTLV defines a structure of SRv6 Information Sub TLV (type 1) object
// https://tools.ietf.org/html/draft-dawra-bess-srv6-services-02#section-2.1.1
type SRv6InformationSubTLV struct {
PrefixSIDTLV
SID []byte
Flags uint8
EndpointBehavior uint16
SubSubTLV []PrefixSIDTLVInterface
}
type SRv6SubSubTLV struct {
PrefixSIDTLV
TLV []PrefixSIDTLVInterface
}
func (s *SRv6SubSubTLV) Len() int {
return int(s.Length) + tlvHdrLen
}
func (s *SRv6SubSubTLV) Serialize() ([]byte, error) {
s.
if len(value) != int(s.Length) {
return nil, malformedAttrListErr("serialization failed: Prefix SID TLV malformed")
}
buf := make([]byte, tlvHdrLen+len(value))
binary.BigEndian.PutUint16(buf[:2], uint16(s.Type))
binary.BigEndian.PutUint16(buf[2:4], uint16(s.Length))
copy(buf[4:], value)
return buf, nil
return nil, nil
}
func (s *SRv6SubSubTLV) DecodeFromBytes(data []byte) error {
if len(data) < tlvHdrLen {
return malformedAttrListErr("decoding failed: Prefix SID TLV malformed")
}
s.Type = PrefixSIDTLVType(binary.BigEndian.Uint16(data[:2]))
s.Length = binary.BigEndian.Uint16(data[2:4])
if len(data) < s.Len() {
return malformedAttrListErr("decoding failed: Prefix SID TLV malformed")
}
return nil
}
func (s *SRv6SubSubTLV) MarshalJSON() ([]byte, error) {
return nil, nil
}
func (s *SRv6SubSubTLV) String() string {
return ""
}
// SRv6SIDStructureSubSubTLV defines a structure of SRv6 SID Structure Sub Sub TLV (type 1) object
// https://tools.ietf.org/html/draft-dawra-bess-srv6-services-02#section-2.1.2.1
type SRv6SIDStructureSubSubTLV struct {
PrefixSIDTLV
LocalBlockLength uint8 `json:"local_block_length,omitempty"`
LocatorNodeLength uint8 `json:"locator_node_length,omitempty"`
FunctionLength uint8 `json:"function_length,omitempty"`
ArgumentLength uint8 `json:"argument_length,omitempty"`
TranspositionLength uint8 `json:"transposition_length,omitempty"`
TranspositionOffset uint8 `json:"transposition_offset,omitempty"`
}
/////////////////////////////
// type LsTLVAdjacencySID struct {
// LsTLV
// Flags uint8
// Weight uint8
// SID uint32
// }
// func (l *LsTLVAdjacencySID) DecodeFromBytes(data []byte) error {
// value, err := l.LsTLV.DecodeFromBytes(data)
// if err != nil {
// return err
// }
// if l.Type != LS_TLV_ADJACENCY_SID {
// return malformedAttrListErr("Unexpected TLV type")
// }
// // https://tools.ietf.org/html/draft-ietf-idr-bgp-ls-segment-routing-ext-08#section-2.2.1
// if len(value) != 7 && len(value) != 8 {
// return malformedAttrListErr("Incorrect Adjacency SID length")
// }
// l.Flags = value[0]
// l.Weight = value[1]
// v := value[4:]
// if len(v) == 4 {
// l.SID = binary.BigEndian.Uint32(v)
// } else {
// buf := []byte{0, 0, 0, 0}
// for i := 1; i < len(buf); i++ {
// buf[i] = v[i-1]
// }
// // Label is represented by 20 rightmost bits.
// l.SID = binary.BigEndian.Uint32(buf) & 0xfffff
// }
// return nil
// }
// func (l *LsTLVAdjacencySID) Serialize() ([]byte, error) {
// buf := make([]byte, 0)
// buf = append(buf, l.Flags)
// buf = append(buf, l.Weight)
// // Reserved
// buf = append(buf, []byte{0, 0}...)
// var b [4]byte
// binary.BigEndian.PutUint32(b[:4], l.SID)
// if l.Length == 7 {
// return l.LsTLV.Serialize(append(buf, b[1:]...))
// }
// return l.LsTLV.Serialize(append(buf, b[:]...))
// }
// func (l *LsTLVAdjacencySID) String() string {
// return fmt.Sprintf("{Adjacency SID: %v}", l.SID)
// }
// func (l *LsTLVAdjacencySID) MarshalJSON() ([]byte, error) {
// return json.Marshal(struct {
// Type LsTLVType `json:"type"`
// SID uint32 `json:"adjacency_sid"`
// }{
// Type: l.Type,
// SID: l.SID,
// })
// }
//////////////////////////////////////
|