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
/
abf.go
240 lines (214 loc) · 7.48 KB
/
abf.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
// 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 (
"github.com/ligato/vpp-agent/plugins/vpp/aclplugin/descriptor"
"github.com/go-errors/errors"
"github.com/gogo/protobuf/proto"
prototypes "github.com/gogo/protobuf/types"
"github.com/ligato/cn-infra/idxmap"
"github.com/ligato/cn-infra/logging"
abf "github.com/ligato/vpp-agent/api/models/vpp/abf"
acl "github.com/ligato/vpp-agent/api/models/vpp/acl"
"github.com/ligato/vpp-agent/api/models/vpp/interfaces"
"github.com/ligato/vpp-agent/plugins/kvscheduler/api"
"github.com/ligato/vpp-agent/plugins/vpp/abfplugin/abfidx"
"github.com/ligato/vpp-agent/plugins/vpp/abfplugin/descriptor/adapter"
"github.com/ligato/vpp-agent/plugins/vpp/abfplugin/vppcalls"
"github.com/ligato/vpp-agent/plugins/vpp/aclplugin/aclidx"
ifdescriptor "github.com/ligato/vpp-agent/plugins/vpp/ifplugin/descriptor"
)
const (
// ABFDescriptorName is descriptor name
ABFDescriptorName = "vpp-abf"
// dependency labels
aclDep = "acl-exists"
)
// A list of non-retriable errors:
var (
// ErrABFWithoutACL is returned when ABF configuration does not contain associated access list.
ErrABFWithoutACL = errors.New("ABF configuration defined without ACL")
)
// ABFDescriptor is descriptor for ABF
type ABFDescriptor struct {
// dependencies
log logging.Logger
abfHandler vppcalls.ABFVppAPI
// runtime
aclIndex aclidx.ACLMetadataIndex
}
// NewABFDescriptor is constructor for ABF descriptor and returns descriptor
// suitable for registration (via adapter) with the KVScheduler.
func NewABFDescriptor(abfHandler vppcalls.ABFVppAPI, aclIndex aclidx.ACLMetadataIndex,
logger logging.PluginLogger) *api.KVDescriptor {
ctx := &ABFDescriptor{
log: logger.NewLogger("abf-descriptor"),
aclIndex: aclIndex,
abfHandler: abfHandler,
}
typedDescr := &adapter.ABFDescriptor{
Name: ABFDescriptorName,
NBKeyPrefix: abf.ModelABF.KeyPrefix(),
ValueTypeName: abf.ModelABF.ProtoName(),
KeySelector: abf.ModelABF.IsKeyValid,
KeyLabel: abf.ModelABF.StripKeyPrefix,
WithMetadata: true,
MetadataMapFactory: func() idxmap.NamedMappingRW {
return abfidx.NewABFIndex(ctx.log, "vpp-abf-index")
},
ValueComparator: ctx.EquivalentABFs,
Validate: ctx.Validate,
Create: ctx.Create,
Delete: ctx.Delete,
Retrieve: ctx.Retrieve,
DerivedValues: ctx.DerivedValues,
Dependencies: ctx.Dependencies,
RetrieveDependencies: []string{ifdescriptor.InterfaceDescriptorName, descriptor.ACLDescriptorName},
}
return adapter.NewABFDescriptor(typedDescr)
}
// EquivalentABFs compares related ACL name, list of attached interfaces and forwarding paths to
// specify ABS equality.
func (d *ABFDescriptor) EquivalentABFs(key string, oldABF, newABF *abf.ABF) bool {
// check index and associated ACL
if oldABF.AclName != newABF.AclName {
return false
}
// compare attached interfaces
if len(oldABF.AttachedInterfaces) != len(newABF.AttachedInterfaces) {
return false
}
if !equivalentABFAttachedInterfaces(oldABF.AttachedInterfaces, newABF.AttachedInterfaces) {
return false
}
// compare forwarding paths
if len(oldABF.ForwardingPaths) != len(newABF.ForwardingPaths) {
return false
}
return equivalentABFForwardingPaths(oldABF.ForwardingPaths, newABF.ForwardingPaths)
}
// Validate validates VPP ABF configuration.
func (d *ABFDescriptor) Validate(key string, abfData *abf.ABF) error {
if abfData.AclName == "" {
return api.NewInvalidValueError(ErrABFWithoutACL, "acl_name")
}
return nil
}
// Create validates ABF (mainly index), verifies ACL existence and configures ABF policy. Attached interfaces
// are put to metadata together with the ABF index to make it available for other ABF descriptors.
func (d *ABFDescriptor) Create(key string, abfData *abf.ABF) (*abfidx.ABFMetadata, error) {
// get ACL index
aclData, exists := d.aclIndex.LookupByName(abfData.AclName)
if !exists {
err := errors.Errorf("failed to obtain metadata for ACL %s", abfData.AclName)
d.log.Error(err)
return nil, err
}
// add new ABF policy
if err := d.abfHandler.AddAbfPolicy(abfData.Index, aclData.Index, abfData.ForwardingPaths); err != nil {
d.log.Error(err)
return nil, err
}
// fill the metadata
metadata := &abfidx.ABFMetadata{
Index: abfData.Index,
Attached: abfData.AttachedInterfaces,
}
return metadata, nil
}
// Delete removes ABF policy
func (d *ABFDescriptor) Delete(key string, abfData *abf.ABF, metadata *abfidx.ABFMetadata) error {
// ACL ID is not required
return d.abfHandler.DeleteAbfPolicy(metadata.Index, abfData.ForwardingPaths)
}
// Retrieve returns ABF policies from the VPP.
func (d *ABFDescriptor) Retrieve(correlate []adapter.ABFKVWithMetadata) (abfs []adapter.ABFKVWithMetadata, err error) {
// Retrieve VPP configuration.
abfPolicies, err := d.abfHandler.DumpABFPolicy()
if err != nil {
return nil, errors.Errorf("failed to dump ABF policy: %v", err)
}
for _, abfPolicy := range abfPolicies {
abfs = append(abfs, adapter.ABFKVWithMetadata{
Key: abf.Key(abfPolicy.ABF.Index),
Value: abfPolicy.ABF,
Metadata: &abfidx.ABFMetadata{
Index: abfPolicy.Meta.PolicyID,
Attached: abfPolicy.ABF.AttachedInterfaces,
},
Origin: api.FromNB,
})
}
return abfs, nil
}
// DerivedValues returns list of derived values for ABF.
func (d *ABFDescriptor) DerivedValues(key string, value *abf.ABF) (derived []api.KeyValuePair) {
for _, attachedIf := range value.GetAttachedInterfaces() {
derived = append(derived, api.KeyValuePair{
Key: abf.ToInterfaceKey(value.Index, attachedIf.InputInterface),
Value: &prototypes.Empty{},
})
}
return derived
}
// A list of ABF dependencies (ACL + forwarding path interfaces).
func (d *ABFDescriptor) Dependencies(key string, abfData *abf.ABF) (dependencies []api.Dependency) {
// forwarding path interfaces
for _, abfDataLabel := range abfData.ForwardingPaths {
if abfDataLabel.InterfaceName != "" {
dependencies = append(dependencies, api.Dependency{
Label: interfaceDep,
Key: vpp_interfaces.InterfaceKey(abfDataLabel.InterfaceName),
})
}
}
// access list
dependencies = append(dependencies, api.Dependency{
Label: aclDep,
Key: acl.Key(abfData.AclName),
})
return dependencies
}
func equivalentABFAttachedInterfaces(oldIfs, newIfs []*abf.ABF_AttachedInterface) bool {
for _, oldIf := range oldIfs {
var found bool
for _, newIf := range newIfs {
if proto.Equal(oldIf, newIf) {
found = true
}
}
if !found {
return false
}
}
return true
}
func equivalentABFForwardingPaths(oldPaths, newPaths []*abf.ABF_ForwardingPath) bool {
for _, oldPath := range oldPaths {
var found bool
for _, newPath := range newPaths {
if oldPath.InterfaceName == newPath.InterfaceName &&
oldPath.NextHopIp == newPath.NextHopIp &&
oldPath.Weight == newPath.Weight &&
oldPath.Preference == newPath.Preference &&
oldPath.Dvr == newPath.Dvr {
found = true
}
}
if !found {
return false
}
}
return true
}