This repository has been archived by the owner on Aug 3, 2020. It is now read-only.
forked from ligato/vpp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
srv6.go
644 lines (584 loc) · 25.6 KB
/
srv6.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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
// Copyright (c) 2019 Bell Canada, Pantheon Technologies and/or its affiliates.
//
// 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 vpp1904 contains wrappers over VPP (version 19.01) binary APIs to simplify their usage
package vpp1904
import (
"bytes"
"fmt"
"net"
"regexp"
"strconv"
"strings"
"github.com/ligato/cn-infra/logging"
nbint "github.com/ligato/vpp-agent/api/models/vpp/interfaces"
srv6 "github.com/ligato/vpp-agent/api/models/vpp/srv6"
"github.com/ligato/vpp-agent/plugins/vpp/binapi/vpp1904/interfaces"
"github.com/ligato/vpp-agent/plugins/vpp/binapi/vpp1904/sr"
)
// Constants for behavior function hardcoded into VPP (there can be also custom behavior functions implemented as VPP plugins)
// Constants are taken from VPP's vnet/srv6/sr.h (names are modified to Golang from original C form in VPP code)
const (
BehaviorEnd uint8 = iota + 1 // Behavior of simple endpoint
BehaviorX // Behavior of endpoint with Layer-3 cross-connect
BehaviorT // Behavior of endpoint with specific IPv6 table lookup
BehaviorDfirst // Unused. Separator in between regular and D
BehaviorDX2 // Behavior of endpoint with decapulation and Layer-2 cross-connect (or DX2 with egress VLAN rewrite when VLAN notzero - not supported this variant yet)
BehaviorDX6 // Behavior of endpoint with decapsulation and IPv6 cross-connect
BehaviorDX4 // Behavior of endpoint with decapsulation and IPv4 cross-connect
BehaviorDT6 // Behavior of endpoint with decapsulation and specific IPv6 table lookup
BehaviorDT4 // Behavior of endpoint with decapsulation and specific IPv4 table lookup
BehaviorLast // seems unused, note in VPP: "Must always be the last one"
)
// Constants for steering type
// Constants are taken from VPP's vnet/srv6/sr.h (names are modified to Golang from original C form in VPP code)
const (
SteerTypeL2 uint8 = 2
SteerTypeIPv4 uint8 = 4
SteerTypeIPv6 uint8 = 6
)
// Constants for operation of SR policy modify binary API method
const (
AddSRList uint8 = iota + 1 // Add SR List to an existing SR policy
DeleteSRList // Delete SR List from an existing SR policy
ModifyWeightOfSRList // Modify the weight of an existing SR List
)
// AddLocalSid adds local sid <localSID> into VPP
func (h *SRv6VppHandler) AddLocalSid(localSID *srv6.LocalSID) error {
sidAddr, err := parseIPv6(localSID.GetSid())
if err != nil {
return fmt.Errorf("sid address %s is not IPv6 address: %v", localSID.GetSid(), err) // calls from descriptor are already validated
}
return h.addDelLocalSid(false, sidAddr, localSID)
}
// DeleteLocalSid delets local sid given by <sidAddr> in VPP
func (h *SRv6VppHandler) DeleteLocalSid(sidAddr net.IP) error {
return h.addDelLocalSid(true, sidAddr, nil)
}
func (h *SRv6VppHandler) addDelLocalSid(deletion bool, sidAddr net.IP, localSID *srv6.LocalSID) error {
h.log.WithFields(logging.Fields{"localSID": sidAddr, "delete": deletion, "installationVrfID": h.installationVrfID(localSID), "end function": h.endFunction(localSID)}).
Debug("Adding/deleting Local SID", sidAddr)
if !deletion && localSID.GetEndFunction_AD() != nil {
return h.addSRProxy(sidAddr, localSID)
}
req := &sr.SrLocalsidAddDel{
IsDel: boolToUint(deletion),
Localsid: sr.Srv6Sid{Addr: []byte(sidAddr)},
}
if !deletion {
req.FibTable = localSID.InstallationVrfId // where to install localsid entry
if err := h.writeEndFunction(req, sidAddr, localSID); err != nil {
return err
}
}
reply := &sr.SrLocalsidAddDelReply{}
if err := h.callsChannel.SendRequest(req).ReceiveReply(reply); err != nil {
return err
}
if reply.Retval != 0 {
return fmt.Errorf("vpp call %q returned: %d", reply.GetMessageName(), reply.Retval)
}
h.log.WithFields(logging.Fields{"localSID": sidAddr, "delete": deletion, "installationVrfID": h.installationVrfID(localSID), "end function": h.endFunction(localSID)}).
Debug("Added/deleted Local SID ", sidAddr)
return nil
}
// addSRProxy adds local sid with SR-proxy end function (End.AD). This functionality has no binary API in VPP, therefore
// CLI commands are used (VPE binary API that calls VPP's CLI).
func (h *SRv6VppHandler) addSRProxy(sidAddr net.IP, localSID *srv6.LocalSID) error {
// get VPP-internal names of IN and OUT interfaces
names, err := h.interfaceNameMapping()
if err != nil {
return fmt.Errorf("can't convert interface names from etcd to VPP-internal interface names:%v", err)
}
outInterface, found := names[localSID.GetEndFunction_AD().OutgoingInterface]
if !found {
return fmt.Errorf("can't find VPP-internal name for interface %v (name in etcd)", localSID.GetEndFunction_AD().OutgoingInterface)
}
inInterface, found := names[localSID.GetEndFunction_AD().IncomingInterface]
if !found {
return fmt.Errorf("can't find VPP-internal name for interface %v (name in etcd)", localSID.GetEndFunction_AD().IncomingInterface)
}
// add SR-proxy using VPP CLI
var cmd string
if strings.TrimSpace(localSID.GetEndFunction_AD().L3ServiceAddress) == "" { // L2 service
cmd = fmt.Sprintf("sr localsid address %v behavior end.ad oif %v iif %v", sidAddr, outInterface, inInterface)
} else { // L3 service
cmd = fmt.Sprintf("sr localsid address %v behavior end.ad nh %v oif %v iif %v", sidAddr, localSID.GetEndFunction_AD().L3ServiceAddress, outInterface, inInterface)
}
data, err := h.RunCli(cmd)
if err != nil {
return err
}
if len(strings.TrimSpace(string(data))) > 0 {
return fmt.Errorf("addition of dynamic segment routing proxy failed by returning nonblank space text in CLI: %v", string(data))
}
return nil
}
// interfaceNameMapping dumps from VPP internal names of interfaces and uses them to produce mapping from ligato interface names to vpp internal names.
func (h *SRv6VppHandler) interfaceNameMapping() (map[string]string, error) {
mapping := make(map[string]string)
reqCtx := h.callsChannel.SendMultiRequest(&interfaces.SwInterfaceDump{})
for {
// get next interface info
ifDetails := &interfaces.SwInterfaceDetails{}
stop, err := reqCtx.ReceiveReply(ifDetails)
if stop {
break // Break from the loop.
}
if err != nil {
return nil, fmt.Errorf("failed to dump interface: %v", err)
}
// extract and compute names
ligatoName := string(bytes.SplitN(ifDetails.Tag, []byte{0x00}, 2)[0])
vppInternalName := string(bytes.SplitN(ifDetails.InterfaceName, []byte{0x00}, 2)[0])
if ifDetails.SupSwIfIndex == ifDetails.SwIfIndex && // no subinterface (subinterface are not DPDK)
guessInterfaceType(string(ifDetails.InterfaceName)) == nbint.Interface_DPDK { // fill name for physical interfaces (they are mostly without tag)
ligatoName = vppInternalName
}
mapping[ligatoName] = vppInternalName
}
return mapping, nil
}
func (h *SRv6VppHandler) installationVrfID(localSID *srv6.LocalSID) string {
if localSID != nil {
return string(localSID.InstallationVrfId)
}
return "<nil>"
}
func (h *SRv6VppHandler) endFunction(localSID *srv6.LocalSID) string {
switch ef := localSID.GetEndFunction().(type) {
case *srv6.LocalSID_BaseEndFunction:
return fmt.Sprintf("End{psp: %v}", ef.BaseEndFunction.Psp)
case *srv6.LocalSID_EndFunction_X:
return fmt.Sprintf("X{psp: %v, OutgoingInterface: %v, NextHop: %v}", ef.EndFunction_X.Psp, ef.EndFunction_X.OutgoingInterface, ef.EndFunction_X.NextHop)
case *srv6.LocalSID_EndFunction_T:
return fmt.Sprintf("T{psp: %v, vrf: %v}", ef.EndFunction_T.Psp, ef.EndFunction_T.VrfId)
case *srv6.LocalSID_EndFunction_DX2:
return fmt.Sprintf("DX2{VlanTag: %v, OutgoingInterface: %v}", ef.EndFunction_DX2.VlanTag, ef.EndFunction_DX2.OutgoingInterface)
case *srv6.LocalSID_EndFunction_DX4:
return fmt.Sprintf("DX4{OutgoingInterface: %v, NextHop: %v}", ef.EndFunction_DX4.OutgoingInterface, ef.EndFunction_DX4.NextHop)
case *srv6.LocalSID_EndFunction_DX6:
return fmt.Sprintf("DX6{OutgoingInterface: %v, NextHop: %v}", ef.EndFunction_DX6.OutgoingInterface, ef.EndFunction_DX6.NextHop)
case *srv6.LocalSID_EndFunction_DT4:
return fmt.Sprintf("DT4{vrf: %v}", ef.EndFunction_DT4.VrfId)
case *srv6.LocalSID_EndFunction_DT6:
return fmt.Sprintf("DT6{vrf: %v}", ef.EndFunction_DT6.VrfId)
case *srv6.LocalSID_EndFunction_AD:
return fmt.Sprintf("AD{L3ServiceAddress: %v, OutgoingInterface: %v, IncomingInterface: %v}", ef.EndFunction_AD.L3ServiceAddress, ef.EndFunction_AD.OutgoingInterface, ef.EndFunction_AD.IncomingInterface)
case nil:
return "<nil>"
default:
return "unknown end function"
}
}
func (h *SRv6VppHandler) writeEndFunction(req *sr.SrLocalsidAddDel, sidAddr net.IP, localSID *srv6.LocalSID) error {
switch ef := localSID.EndFunction.(type) {
case *srv6.LocalSID_BaseEndFunction:
req.Behavior = BehaviorEnd
req.EndPsp = boolToUint(ef.BaseEndFunction.Psp)
case *srv6.LocalSID_EndFunction_X:
req.Behavior = BehaviorX
req.EndPsp = boolToUint(ef.EndFunction_X.Psp)
ifMeta, exists := h.ifIndexes.LookupByName(ef.EndFunction_X.OutgoingInterface)
if !exists {
return fmt.Errorf("for interface %v doesn't exist sw index", ef.EndFunction_X.OutgoingInterface)
}
req.SwIfIndex = ifMeta.SwIfIndex
nhAddr, err := parseIPv6(ef.EndFunction_X.NextHop) // parses also ipv4 addresses but into ipv6 address form
if err != nil {
return err
}
if nhAddr4 := nhAddr.To4(); nhAddr4 != nil { // ipv4 address in ipv6 address form?
req.NhAddr4 = nhAddr4
} else {
req.NhAddr6 = []byte(nhAddr)
}
case *srv6.LocalSID_EndFunction_T:
req.Behavior = BehaviorT
req.EndPsp = boolToUint(ef.EndFunction_T.Psp)
req.SwIfIndex = ef.EndFunction_T.VrfId
case *srv6.LocalSID_EndFunction_DX2:
req.Behavior = BehaviorDX2
req.VlanIndex = ef.EndFunction_DX2.VlanTag
ifMeta, exists := h.ifIndexes.LookupByName(ef.EndFunction_DX2.OutgoingInterface)
if !exists {
return fmt.Errorf("for interface %v doesn't exist sw index", ef.EndFunction_DX2.OutgoingInterface)
}
req.SwIfIndex = ifMeta.SwIfIndex
case *srv6.LocalSID_EndFunction_DX4:
req.Behavior = BehaviorDX4
ifMeta, exists := h.ifIndexes.LookupByName(ef.EndFunction_DX4.OutgoingInterface)
if !exists {
return fmt.Errorf("for interface %v doesn't exist sw index", ef.EndFunction_DX4.OutgoingInterface)
}
req.SwIfIndex = ifMeta.SwIfIndex
nhAddr, err := parseIPv6(ef.EndFunction_DX4.NextHop) // parses also IPv4
if err != nil {
return err
}
nhAddr4 := nhAddr.To4()
if nhAddr4 == nil {
return fmt.Errorf("next hop of DX4 end function (%v) is not valid IPv4 address", ef.EndFunction_DX4.NextHop)
}
req.NhAddr4 = []byte(nhAddr4)
case *srv6.LocalSID_EndFunction_DX6:
req.Behavior = BehaviorDX6
ifMeta, exists := h.ifIndexes.LookupByName(ef.EndFunction_DX6.OutgoingInterface)
if !exists {
return fmt.Errorf("for interface %v doesn't exist sw index", ef.EndFunction_DX6.OutgoingInterface)
}
req.SwIfIndex = ifMeta.SwIfIndex
nhAddr6, err := parseIPv6(ef.EndFunction_DX6.NextHop)
if err != nil {
return err
}
req.NhAddr6 = []byte(nhAddr6)
case *srv6.LocalSID_EndFunction_DT4:
req.Behavior = BehaviorDT4
req.SwIfIndex = ef.EndFunction_DT4.VrfId
case *srv6.LocalSID_EndFunction_DT6:
req.Behavior = BehaviorDT6
req.SwIfIndex = ef.EndFunction_DT6.VrfId
case nil:
return fmt.Errorf("End function not set. Please configure end function for local SID %v ", sidAddr)
default:
return fmt.Errorf("unknown end function (model link type %T)", ef) // EndFunction_AD is handled elsewhere
}
return nil
}
// SetEncapsSourceAddress sets for SRv6 in VPP the source address used for encapsulated packet
func (h *SRv6VppHandler) SetEncapsSourceAddress(address string) error {
h.log.Debugf("Configuring encapsulation source address to address %v", address)
ipAddress, err := parseIPv6(address)
if err != nil {
return err
}
req := &sr.SrSetEncapSource{
EncapsSource: []byte(ipAddress),
}
reply := &sr.SrSetEncapSourceReply{}
if err := h.callsChannel.SendRequest(req).ReceiveReply(reply); err != nil {
return err
}
if reply.Retval != 0 {
return fmt.Errorf("vpp call %q returned: %d", reply.GetMessageName(), reply.Retval)
}
h.log.WithFields(logging.Fields{"Encapsulation source address": address}).
Debug("Encapsulation source address configured.")
return nil
}
// AddPolicy adds SRv6 policy <policy> into VPP (including all policy's segment lists).
func (h *SRv6VppHandler) AddPolicy(policy *srv6.Policy) error {
if err := h.addBasePolicyWithFirstSegmentList(policy); err != nil {
return fmt.Errorf("can't create Policy with first segment list (Policy: %+v): %v", policy, err)
}
if err := h.addOtherSegmentLists(policy); err != nil {
return fmt.Errorf("can't add all segment lists to created policy %+v: %v", policy, err)
}
return nil
}
func (h *SRv6VppHandler) addBasePolicyWithFirstSegmentList(policy *srv6.Policy) error {
h.log.Debugf("Adding SR policy %+v", policy)
bindingSid, err := parseIPv6(policy.GetBsid()) // already validated
if err != nil {
return fmt.Errorf("binding sid address %s is not IPv6 address: %v", policy.GetBsid(), err) // calls from descriptor are already validated
}
if len(policy.SegmentLists) == 0 {
return fmt.Errorf("policy must have defined at least one segment list (Policy: %+v)", policy) // calls from descriptor are already validated
}
sids, err := h.convertPolicySegment(policy.SegmentLists[0])
if err != nil {
return err
}
// Note: Weight in sr.SrPolicyAdd is leftover from API changes that moved weight into sr.Srv6SidList (it is weight of sid list not of the whole policy)
req := &sr.SrPolicyAdd{
BsidAddr: []byte(bindingSid),
Sids: *sids,
IsEncap: boolToUint(policy.SrhEncapsulation),
Type: boolToUint(policy.SprayBehaviour),
FibTable: policy.InstallationVrfId,
}
reply := &sr.SrPolicyAddReply{}
if err := h.callsChannel.SendRequest(req).ReceiveReply(reply); err != nil {
return err
}
if reply.Retval != 0 {
return fmt.Errorf("vpp call %q returned: %d", reply.GetMessageName(), reply.Retval)
}
h.log.WithFields(logging.Fields{"binding SID": bindingSid, "list of next SIDs": policy.SegmentLists[0].Segments}).
Debug("base SR policy (policy with just one segment list) added")
return nil
}
func (h *SRv6VppHandler) addOtherSegmentLists(policy *srv6.Policy) error {
for _, sl := range policy.SegmentLists[1:] {
if err := h.AddPolicySegmentList(sl, policy); err != nil {
return fmt.Errorf("failed to add policy segment %+v: %v", sl, err)
}
}
return nil
}
// DeletePolicy deletes SRv6 policy given by binding SID <bindingSid>
func (h *SRv6VppHandler) DeletePolicy(bindingSid net.IP) error {
h.log.Debugf("Deleting SR policy with binding SID %v ", bindingSid)
req := &sr.SrPolicyDel{
BsidAddr: sr.Srv6Sid{Addr: []byte(bindingSid)}, // TODO add ability to define policy also by index (SrPolicyIndex)
}
reply := &sr.SrPolicyDelReply{}
if err := h.callsChannel.SendRequest(req).ReceiveReply(reply); err != nil {
return err
}
if reply.Retval != 0 {
return fmt.Errorf("vpp call %q returned: %d", reply.GetMessageName(), reply.Retval)
}
h.log.WithFields(logging.Fields{"binding SID": bindingSid}).Debug("SR policy deleted")
return nil
}
// AddPolicySegmentList adds segment list <segmentList> to SRv6 policy <policy> in VPP
func (h *SRv6VppHandler) AddPolicySegmentList(segmentList *srv6.Policy_SegmentList, policy *srv6.Policy) error {
h.log.Debugf("Adding segment %+v to SR policy %+v", segmentList, policy)
err := h.modPolicy(AddSRList, policy, segmentList, 0)
if err == nil {
h.log.WithFields(logging.Fields{"binding SID": policy.Bsid, "list of next SIDs": segmentList.Segments}).
Debug("SR policy modified(added another segment list)")
}
return err
}
// DeletePolicySegmentList removes segment list <segmentList> (with VPP-internal index <segmentVPPIndex>) from SRv6 policy <policy> in VPP
func (h *SRv6VppHandler) DeletePolicySegmentList(segmentList *srv6.Policy_SegmentList, segmentVPPIndex uint32, policy *srv6.Policy) error {
h.log.Debugf("Removing segment %+v (vpp-internal index %v) from SR policy %+v", segmentList, segmentVPPIndex, policy)
err := h.modPolicy(DeleteSRList, policy, segmentList, segmentVPPIndex)
if err == nil {
h.log.WithFields(logging.Fields{"binding SID": policy.Bsid, "list of next SIDs": segmentList.Segments, "segmentListIndex": segmentVPPIndex}).
Debug("SR policy modified(removed segment list)")
}
return err
}
func (h *SRv6VppHandler) modPolicy(operation uint8, policy *srv6.Policy, segmentList *srv6.Policy_SegmentList, segmentListIndex uint32) error {
bindingSid, err := parseIPv6(policy.GetBsid())
if err != nil {
return fmt.Errorf("binding sid address %s is not IPv6 address: %v", policy.GetBsid(), err) // calls from descriptor are already validated
}
sids, err := h.convertPolicySegment(segmentList)
if err != nil {
return err
}
// Note: Weight in sr.SrPolicyMod is leftover from API changes that moved weight into sr.Srv6SidList (it is weight of sid list not of the whole policy)
req := &sr.SrPolicyMod{
BsidAddr: []byte(bindingSid), // TODO add ability to define policy also by index (SrPolicyIndex)
Operation: operation,
Sids: *sids,
FibTable: policy.InstallationVrfId,
}
if operation == DeleteSRList || operation == ModifyWeightOfSRList {
req.SlIndex = segmentListIndex
}
reply := &sr.SrPolicyModReply{}
if err := h.callsChannel.SendRequest(req).ReceiveReply(reply); err != nil {
return err
}
if reply.Retval != 0 {
return fmt.Errorf("vpp call %q returned: %d", reply.GetMessageName(), reply.Retval)
}
return nil
}
func (h *SRv6VppHandler) convertPolicySegment(segmentList *srv6.Policy_SegmentList) (*sr.Srv6SidList, error) {
var segments []sr.Srv6Sid
for _, sid := range segmentList.Segments {
// parse to IPv6 address
parserSid, err := parseIPv6(sid)
if err != nil {
return nil, err
}
// add sid to segment list
ipv6Segment := sr.Srv6Sid{
Addr: make([]byte, 16), // sr.Srv6Sid.Addr = [16]byte
}
copy(ipv6Segment.Addr, parserSid)
segments = append(segments, ipv6Segment)
}
return &sr.Srv6SidList{
NumSids: uint8(len(segments)),
Sids: segments,
Weight: segmentList.Weight,
}, nil
}
// RetrievePolicyIndexInfo retrieves index of policy <policy> and its segment lists
func (h *SRv6VppHandler) RetrievePolicyIndexInfo(policy *srv6.Policy) (policyIndex uint32, segmentListIndexes map[*srv6.Policy_SegmentList]uint32, err error) {
// dump sr policies using VPP CLI
data, err := h.RunCli("sh sr policies")
if err != nil {
return ^uint32(0), nil, fmt.Errorf("can't dump index data from VPP: %v", err)
}
// do necessary parsing to extract index of segment list
dumpStr := strings.ToLower(string(data))
segmentListIndexes = make(map[*srv6.Policy_SegmentList]uint32)
for _, policyStr := range strings.Split(dumpStr, "-----------") {
policyHeader := regexp.MustCompile(fmt.Sprintf("\\[(\\d+)\\]\\.-\\s+bsid:\\s*%s", strings.ToLower(strings.TrimSpace(policy.GetBsid()))))
if policyMatch := policyHeader.FindStringSubmatch(policyStr); policyMatch != nil {
parsed, err := strconv.ParseUint(policyMatch[1], 10, 32)
if err != nil {
return ^uint32(0), nil, fmt.Errorf("can't parse policy index %q (dump: %s)", policyMatch[1], dumpStr)
}
policyIndex = uint32(parsed)
for _, sl := range policy.SegmentLists {
slRE := regexp.MustCompile(fmt.Sprintf("\\[(\\d+)\\].- < %s,[^:>]*> weight: %d", strings.ToLower(strings.Join(sl.Segments, ", ")), sl.Weight))
if slMatch := slRE.FindStringSubmatch(policyStr); slMatch != nil {
parsed, err := strconv.ParseUint(slMatch[1], 10, 32)
if err != nil {
return ^uint32(0), nil, fmt.Errorf("can't parse segment policy index %q (dump: %s)", slMatch[1], dumpStr)
}
segmentListIndexes[sl] = uint32(parsed)
continue
}
return ^uint32(0), nil, fmt.Errorf("can't find index for segment list %+v (policy bsid %v) in dump %q", sl, policy.GetBsid(), dumpStr)
}
return policyIndex, segmentListIndexes, nil
}
}
return ^uint32(0), nil, fmt.Errorf("can't find index for policy with bsid %v in dump %q", policy.GetBsid(), dumpStr)
}
// AddSteering sets in VPP steering into SRv6 policy.
func (h *SRv6VppHandler) AddSteering(steering *srv6.Steering) error {
return h.addDelSteering(false, steering)
}
// RemoveSteering removes in VPP steering into SRv6 policy.
func (h *SRv6VppHandler) RemoveSteering(steering *srv6.Steering) error {
return h.addDelSteering(true, steering)
}
func (h *SRv6VppHandler) addDelSteering(delete bool, steering *srv6.Steering) error {
// defining operation strings for logging
operationProgressing, operationFinished := "Adding", "Added"
if delete {
operationProgressing, operationFinished = "Removing", "Removed"
}
// logging info about operation with steering
switch t := steering.Traffic.(type) {
case *srv6.Steering_L3Traffic_:
h.log.Debugf("%v steering for l3 traffic with destination %v to SR policy (binding SID %v, policy index %v)",
operationProgressing, t.L3Traffic.PrefixAddress, steering.GetPolicyBsid(), steering.GetPolicyIndex())
case *srv6.Steering_L2Traffic_:
h.log.Debugf("%v steering for l2 traffic from interface %v to SR policy (binding SID %v, policy index %v)",
operationProgressing, t.L2Traffic.InterfaceName, steering.GetPolicyBsid(), steering.GetPolicyIndex())
}
// converting policy reference
var policyBSIDAddr []byte // undefined reference
var policyIndex = uint32(0) // undefined reference
switch ref := steering.PolicyRef.(type) {
case *srv6.Steering_PolicyBsid:
bsid, err := parseIPv6(ref.PolicyBsid)
if err != nil {
return fmt.Errorf("can't parse binding SID %q to IP address: %v ", ref.PolicyBsid, err)
}
policyBSIDAddr = []byte(bsid)
case *srv6.Steering_PolicyIndex:
policyIndex = ref.PolicyIndex
case nil:
return fmt.Errorf("policy reference must be provided")
default:
return fmt.Errorf("unknown policy reference type (link type %+v)", ref)
}
// converting target traffic info
var prefixAddr []byte
steerType := SteerTypeIPv6
tableID := uint32(0)
maskWidth := uint32(0)
intIndex := uint32(0)
switch t := steering.Traffic.(type) {
case *srv6.Steering_L3Traffic_:
ip, ipnet, err := net.ParseCIDR(t.L3Traffic.PrefixAddress)
if err != nil {
return fmt.Errorf("can't parse ip prefix %q: %v", t.L3Traffic.PrefixAddress, err)
}
if ip.To4() != nil { // IPv4 address
steerType = SteerTypeIPv4
}
tableID = t.L3Traffic.InstallationVrfId
prefixAddr = []byte(ip.To16())
ms, _ := ipnet.Mask.Size()
maskWidth = uint32(ms)
case *srv6.Steering_L2Traffic_:
steerType = SteerTypeL2
ifMeta, exists := h.ifIndexes.LookupByName(t.L2Traffic.InterfaceName)
if !exists {
return fmt.Errorf("for interface %v doesn't exist sw index", t.L2Traffic.InterfaceName)
}
intIndex = ifMeta.SwIfIndex
case nil:
return fmt.Errorf("traffic type must be provided")
default:
return fmt.Errorf("unknown traffic type (link type %+v)", t)
}
req := &sr.SrSteeringAddDel{
IsDel: boolToUint(delete),
TableID: tableID,
BsidAddr: policyBSIDAddr, // policy (to which we want to steer routing into) identified by policy binding sid (alternativelly it can be used policy index)
SrPolicyIndex: policyIndex, // policy (to which we want to steer routing into) identified by policy index (alternativelly it can be used policy binding sid)
TrafficType: steerType, // type of traffic to steer
PrefixAddr: prefixAddr, // destination prefix address (L3 traffic type only)
MaskWidth: maskWidth, // destination ip prefix mask (L3 traffic type only)
SwIfIndex: intIndex, // incoming interface (L2 traffic type only)
}
reply := &sr.SrSteeringAddDelReply{}
if err := h.callsChannel.SendRequest(req).ReceiveReply(reply); err != nil {
return err
}
if reply.Retval != 0 {
return fmt.Errorf("vpp call %q returned: %d", reply.GetMessageName(), reply.Retval)
}
h.log.WithFields(logging.Fields{"steer type": steerType, "L3 prefix address bytes": prefixAddr,
"L2 interface index": intIndex, "policy binding SID": policyBSIDAddr, "policy index": policyIndex}).
Debugf("%v steering to SR policy ", operationFinished)
return nil
}
func boolToUint(input bool) uint8 {
if input {
return uint8(1)
}
return uint8(0)
}
// parseIPv6 parses string <str> to IPv6 address (including IPv4 address converted to IPv6 address)
func parseIPv6(str string) (net.IP, error) {
ip := net.ParseIP(str)
if ip == nil {
return nil, fmt.Errorf(" %q is not ip address", str)
}
ipv6 := ip.To16()
if ipv6 == nil {
return nil, fmt.Errorf(" %q is not ipv6 address", str)
}
return ipv6, nil
}
// guessInterfaceType attempts to guess the correct interface type from its internal name (as given by VPP).
// This is required mainly for those interface types, that do not provide dump binary API,
// such as loopback of af_packet.
func guessInterfaceType(ifName string) nbint.Interface_Type {
switch {
case strings.HasPrefix(ifName, "loop"),
strings.HasPrefix(ifName, "local"):
return nbint.Interface_SOFTWARE_LOOPBACK
case strings.HasPrefix(ifName, "memif"):
return nbint.Interface_MEMIF
case strings.HasPrefix(ifName, "tap"):
return nbint.Interface_TAP
case strings.HasPrefix(ifName, "host"):
return nbint.Interface_AF_PACKET
case strings.HasPrefix(ifName, "vxlan"):
return nbint.Interface_VXLAN_TUNNEL
case strings.HasPrefix(ifName, "ipsec"):
return nbint.Interface_IPSEC_TUNNEL
case strings.HasPrefix(ifName, "vmxnet3"):
return nbint.Interface_VMXNET3_INTERFACE
default:
return nbint.Interface_DPDK
}
}