forked from ligato/vpp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
abf_to_interface.go
145 lines (126 loc) · 4.62 KB
/
abf_to_interface.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
// Copyright (c) 2019 Cisco 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 descriptor
import (
"fmt"
vpp_abf "github.com/ligato/vpp-agent/api/models/vpp/abf"
"github.com/ligato/vpp-agent/plugins/vpp/abfplugin/abfidx"
"github.com/ligato/vpp-agent/api/models/vpp/interfaces"
"github.com/go-errors/errors"
"github.com/gogo/protobuf/proto"
"github.com/ligato/cn-infra/logging"
"github.com/ligato/vpp-agent/plugins/kvscheduler/api"
"github.com/ligato/vpp-agent/plugins/vpp/abfplugin/vppcalls"
"github.com/ligato/vpp-agent/plugins/vpp/ifplugin"
)
const (
// ABFToInterfaceDescriptorName is name for descriptor
ABFToInterfaceDescriptorName = "vpp-abf-to-interface"
// dependency labels
interfaceDep = "interface-exists"
)
// ABFToInterfaceDescriptor represents assignment of interface to ABF policy.
type ABFToInterfaceDescriptor struct {
log logging.Logger
abfHandler vppcalls.ABFVppAPI
abfIndex abfidx.ABFMetadataIndex
ifPlugin ifplugin.API
}
// NewABFToInterfaceDescriptor returns new ABFInterface descriptor
func NewABFToInterfaceDescriptor(abfIndex abfidx.ABFMetadataIndex, abfHandler vppcalls.ABFVppAPI, ifPlugin ifplugin.API, log logging.PluginLogger) *api.KVDescriptor {
ctx := &ABFToInterfaceDescriptor{
log: log,
abfHandler: abfHandler,
abfIndex: abfIndex,
ifPlugin: ifPlugin,
}
return &api.KVDescriptor{
Name: ABFToInterfaceDescriptorName,
KeySelector: ctx.IsABFInterfaceKey,
Create: ctx.Create,
Delete: ctx.Delete,
Dependencies: ctx.Dependencies,
}
}
// IsABFInterfaceKey returns true if the key is identifying ABF policy interface (derived value)
func (d *ABFToInterfaceDescriptor) IsABFInterfaceKey(key string) bool {
_, _, isABFToInterfaceKey := vpp_abf.ParseToInterfaceKey(key)
return isABFToInterfaceKey
}
// Create binds interface to ABF.
func (d *ABFToInterfaceDescriptor) Create(key string, emptyVal proto.Message) (metadata api.Metadata, err error) {
// validate and get all required values
isIPv6, abfIdx, ifIdx, priority, err := d.process(key)
if err != nil {
d.log.Error(err)
return nil, err
}
// attach interface to ABF policy
if isIPv6 {
return nil, d.abfHandler.AbfAttachInterfaceIPv6(abfIdx, ifIdx, priority)
}
return nil, d.abfHandler.AbfAttachInterfaceIPv4(abfIdx, ifIdx, priority)
}
// Delete unbinds interface from ABF.
func (d *ABFToInterfaceDescriptor) Delete(key string, emptyVal proto.Message, metadata api.Metadata) (err error) {
// validate and get all required values
isIPv6, abfIdx, ifIdx, priority, err := d.process(key)
if err != nil {
d.log.Error(err)
return err
}
// detach interface to ABF policy
if isIPv6 {
return d.abfHandler.AbfDetachInterfaceIPv6(abfIdx, ifIdx, priority)
}
return d.abfHandler.AbfDetachInterfaceIPv4(abfIdx, ifIdx, priority)
}
// Dependencies lists the interface as the only dependency for the binding.
func (d *ABFToInterfaceDescriptor) Dependencies(key string, emptyVal proto.Message) []api.Dependency {
_, ifName, _ := vpp_abf.ParseToInterfaceKey(key)
return []api.Dependency{
{
Label: interfaceDep,
Key: vpp_interfaces.InterfaceKey(ifName),
},
}
}
// returns a bunch of values needed to attach/detach interface to/from ABF
func (d *ABFToInterfaceDescriptor) process(key string) (isIPv6 bool, abfIdx, ifIdx, priority uint32, err error) {
// parse ABF and interface name
abfIndex, ifName, isValid := vpp_abf.ParseToInterfaceKey(key)
if !isValid {
err = fmt.Errorf("ABF to interface key %s is not valid", key)
return
}
// obtain ABF index
abfData, exists := d.abfIndex.LookupByName(abfIndex)
if !exists {
err = errors.Errorf("failed to obtain metadata for ABF %s", abfIndex)
return
}
// obtain interface index
ifData, exists := d.ifPlugin.GetInterfaceIndex().LookupByName(ifName)
if !exists {
err = errors.Errorf("failed to obtain metadata for interface %s", ifName)
return
}
// find other interface parameters from metadata
for _, attachedIf := range abfData.Attached {
if attachedIf.InputInterface == ifName {
isIPv6, priority = attachedIf.IsIpv6, attachedIf.Priority
}
}
return isIPv6, abfData.Index, ifData.SwIfIndex, priority, nil
}