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
|
package bgp
import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"github.com/golang/glog"
)
type PrefixSIDTLVInterface interface {
Len() int
DecodeFromBytes([]byte) error
Serialize() ([]byte, error)
String() string
MarshalJSON() ([]byte, error)
}
type PathAttributePrefixSID struct {
PathAttribute
TLVs []PrefixSIDTLVInterface
}
type PrefixSIDTLVType uint16
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 {
TLVs map[PrefixSIDTLVType]PrefixSIDTLVInterface
}
func (p *PathAttributePrefixSID) Extract() *PrefixSIDAttribute {
s := &PrefixSIDAttribute{}
// for _, tlv := range p.TLVs {
// switch v := tlv.(type) {
// case *LsTLVNodeFlagBits:
// l.Node.Flags = v.Extract()
// case *LsTLVOpaqueNodeAttr:
// l.Node.Opaque = &v.Attr
// case *LsTLVNodeName:
// l.Node.Name = &v.Name
// case *LsTLVIsisArea:
// l.Node.IsisArea = &v.Area
// case *LsTLVLocalIPv4RouterID:
// l.Node.LocalRouterID = &v.IP
// l.Link.LocalRouterID = &v.IP
// case *LsTLVLocalIPv6RouterID:
// l.Node.LocalRouterIDv6 = &v.IP
// l.Link.LocalRouterIDv6 = &v.IP
// case *LsTLVSrCapabilities:
// l.Node.SrCapabilties = v.Extract()
// case *LsTLVSrAlgorithm:
// l.Node.SrAlgorithms = &v.Algorithm
// case *LsTLVSrLocalBlock:
// l.Node.SrLocalBlock = v.Extract()
// case *LsTLVRemoteIPv4RouterID:
// l.Link.RemoteRouterID = &v.IP
// case *LsTLVRemoteIPv6RouterID:
// l.Link.RemoteRouterIDv6 = &v.IP
// case *LsTLVAdminGroup:
// l.Link.AdminGroup = &v.AdminGroup
// case *LsTLVMaxLinkBw:
// l.Link.Bandwidth = &v.Bandwidth
// case *LsTLVMaxReservableLinkBw:
// l.Link.ReservableBandwidth = &v.Bandwidth
// case *LsTLVUnreservedBw:
// l.Link.UnreservedBandwidth = &v.Bandwidth
// case *LsTLVSrlg:
// l.Link.Srlgs = &v.Srlgs
// case *LsTLVTEDefaultMetric:
// l.Link.DefaultTEMetric = &v.Metric
// case *LsTLVIGPMetric:
// l.Link.IGPMetric = &v.Metric
// case *LsTLVOpaqueLinkAttr:
// l.Link.Opaque = &v.Attr
// case *LsTLVLinkName:
// l.Link.Name = &v.Name
// case *LsTLVAdjacencySID:
// l.Link.SrAdjacencySID = &v.SID
// case *LsTLVIGPFlags:
// l.Prefix.IGPFlags = v.Extract()
// case *LsTLVOpaquePrefixAttr:
// l.Prefix.Opaque = &v.Attr
// case *LsTLVPrefixSID:
// l.Prefix.SrPrefixSID = &v.SID
// }
// }
return s
}
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{}
// // Node NLRI-related TLVs (https://tools.ietf.org/html/rfc7752#section-3.3.1)
// case LS_TLV_NODE_FLAG_BITS:
// tlv = &LsTLVNodeFlagBits{}
// case LS_TLV_OPAQUE_NODE_ATTR:
// tlv = &LsTLVOpaqueNodeAttr{}
// case LS_TLV_NODE_NAME:
// tlv = &LsTLVNodeName{}
// case LS_TLV_ISIS_AREA:
// tlv = &LsTLVIsisArea{}
// // Used by Link NLRI as well.
// case LS_TLV_IPV4_LOCAL_ROUTER_ID:
// tlv = &LsTLVLocalIPv4RouterID{}
// // Used by Link NLRI as well.
// case LS_TLV_IPV6_LOCAL_ROUTER_ID:
// tlv = &LsTLVLocalIPv6RouterID{}
// // SR-related TLVs (draft-ietf-idr-bgp-ls-segment-routing-ext-08) for Node NLRI
// case LS_TLV_SR_CAPABILITIES:
// tlv = &LsTLVSrCapabilities{}
// case LS_TLV_SR_ALGORITHM:
// tlv = &LsTLVSrAlgorithm{}
// case LS_TLV_SR_LOCAL_BLOCK:
// tlv = &LsTLVSrLocalBlock{}
// // Link NLRI-related TLVs (https://tools.ietf.org/html/rfc7752#section-3.3.2)
// case LS_TLV_IPV4_REMOTE_ROUTER_ID:
// tlv = &LsTLVRemoteIPv4RouterID{}
// case LS_TLV_IPV6_REMOTE_ROUTER_ID:
// tlv = &LsTLVRemoteIPv6RouterID{}
// case LS_TLV_ADMIN_GROUP:
// tlv = &LsTLVAdminGroup{}
// case LS_TLV_MAX_LINK_BANDWIDTH:
// tlv = &LsTLVMaxLinkBw{}
// case LS_TLV_MAX_RESERVABLE_BANDWIDTH:
// tlv = &LsTLVMaxReservableLinkBw{}
// case LS_TLV_UNRESERVED_BANDWIDTH:
// tlv = &LsTLVUnreservedBw{}
// case LS_TLV_SRLG:
// tlv = &LsTLVSrlg{}
// case LS_TLV_TE_DEFAULT_METRIC:
// tlv = &LsTLVTEDefaultMetric{}
// case LS_TLV_IGP_METRIC:
// tlv = &LsTLVIGPMetric{}
// case LS_TLV_OPAQUE_LINK_ATTR:
// tlv = &LsTLVOpaqueLinkAttr{}
// case LS_TLV_LINK_NAME:
// tlv = &LsTLVLinkName{}
// // SR-related TLVs (draft-ietf-idr-bgp-ls-segment-routing-ext-08) for Link NLRI
// case LS_TLV_ADJACENCY_SID:
// tlv = &LsTLVAdjacencySID{}
// // Prefix NLRI-related TLVs (https://tools.ietf.org/html/rfc7752#section-3.3.3)
// case LS_TLV_IGP_FLAGS:
// tlv = &LsTLVIGPFlags{}
// case LS_TLV_OPAQUE_PREFIX_ATTR:
// tlv = &LsTLVOpaquePrefixAttr{}
// // SR-related TLVs (draft-ietf-idr-bgp-ls-segment-routing-ext-08) for Prefix NLRI
// case LS_TLV_PREFIX_SID:
// tlv = &LsTLVPrefixSID{}
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(),
*p.Extract(),
})
}
type PrefixSIDType5 struct {
Type PrefixSIDTLVType
Length uint16
}
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")
}
return nil
}
func (s *PrefixSIDType5) MarshalJSON() ([]byte, error) {
return nil, nil
}
func (s *PrefixSIDType5) String() string {
return ""
}
|