This repository has been archived by the owner on Aug 3, 2020. It is now read-only.
forked from ligato/vpp-agent
/
nat44_dnat.go
356 lines (319 loc) · 11.3 KB
/
nat44_dnat.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
// Copyright (c) 2018 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/gogo/protobuf/proto"
"github.com/ligato/cn-infra/logging"
"github.com/pkg/errors"
interfaces "github.com/ligato/vpp-agent/api/models/vpp/interfaces"
l3 "github.com/ligato/vpp-agent/api/models/vpp/l3"
nat "github.com/ligato/vpp-agent/api/models/vpp/nat"
kvs "github.com/ligato/vpp-agent/plugins/kvscheduler/api"
vpp_ifdescriptor "github.com/ligato/vpp-agent/plugins/vpp/ifplugin/descriptor"
"github.com/ligato/vpp-agent/plugins/vpp/natplugin/descriptor/adapter"
"github.com/ligato/vpp-agent/plugins/vpp/natplugin/vppcalls"
"strconv"
)
const (
// DNAT44DescriptorName is the name of the descriptor for VPP NAT44
// Destination-NAT configurations.
DNAT44DescriptorName = "vpp-nat44-dnat"
// untaggedDNAT is used as a label for DNAT grouping all untagged static
// and identity mappings.
untaggedDNAT = "UNTAGGED-DNAT"
// dependency labels
mappingInterfaceDep = "interface-exists"
mappingVrfDep = "vrf-table-exists"
)
// A list of non-retriable errors:
var (
// ErrDNAT44WithEmptyLabel is returned when NAT44 DNAT configuration is defined
// with empty label
ErrDNAT44WithEmptyLabel = errors.New("NAT44 DNAT configuration defined with empty label")
)
// DNAT44Descriptor teaches KVScheduler how to configure Destination NAT44 in VPP.
type DNAT44Descriptor struct {
log logging.Logger
natHandler vppcalls.NatVppAPI
}
// NewDNAT44Descriptor creates a new instance of the DNAT44 descriptor.
func NewDNAT44Descriptor(natHandler vppcalls.NatVppAPI, log logging.PluginLogger) *kvs.KVDescriptor {
ctx := &DNAT44Descriptor{
natHandler: natHandler,
log: log.NewLogger("nat44-dnat-descriptor"),
}
typedDescr := &adapter.DNAT44Descriptor{
Name: DNAT44DescriptorName,
NBKeyPrefix: nat.ModelDNat44.KeyPrefix(),
ValueTypeName: nat.ModelDNat44.ProtoName(),
KeySelector: nat.ModelDNat44.IsKeyValid,
KeyLabel: nat.ModelDNat44.StripKeyPrefix,
ValueComparator: ctx.EquivalentDNAT44,
Validate: ctx.Validate,
Create: ctx.Create,
Delete: ctx.Delete,
Update: ctx.Update,
Retrieve: ctx.Retrieve,
Dependencies: ctx.Dependencies,
// retrieve interfaces and allocated IP addresses first
RetrieveDependencies: []string{vpp_ifdescriptor.InterfaceDescriptorName, vpp_ifdescriptor.DHCPDescriptorName},
}
return adapter.NewDNAT44Descriptor(typedDescr)
}
// EquivalentDNAT44 compares two instances of DNAT44 for equality.
func (d *DNAT44Descriptor) EquivalentDNAT44(key string, oldDNAT, newDNAT *nat.DNat44) bool {
// compare identity mappings
obsoleteIDMappings, newIDMappings := diffIdentityMappings(oldDNAT.IdMappings, newDNAT.IdMappings)
if len(obsoleteIDMappings) != 0 || len(newIDMappings) != 0 {
return false
}
// compare static mappings
obsoleteStMappings, newStMappings := diffStaticMappings(oldDNAT.StMappings, newDNAT.StMappings)
return len(obsoleteStMappings) == 0 && len(newStMappings) == 0
}
// IsRetriableFailure returns <false> for errors related to invalid configuration.
func (d *DNAT44Descriptor) IsRetriableFailure(err error) bool {
return err != ErrDNAT44WithEmptyLabel
}
// Validate validates VPP destination-NAT44 configuration.
func (d *DNAT44Descriptor) Validate(key string, dnat *nat.DNat44) error {
if dnat.Label == "" {
return kvs.NewInvalidValueError(ErrDNAT44WithEmptyLabel, "label")
}
return nil
}
// Create adds new destination-NAT44 configuration.
func (d *DNAT44Descriptor) Create(key string, dnat *nat.DNat44) (metadata interface{}, err error) {
// Add = Modify from empty DNAT
return d.Update(key, &nat.DNat44{Label: dnat.Label}, dnat, nil)
}
// Delete removes existing destination-NAT44 configuration.
func (d *DNAT44Descriptor) Delete(key string, dnat *nat.DNat44, metadata interface{}) error {
// Delete = Modify into empty DNAT
_, err := d.Update(key, dnat, &nat.DNat44{Label: dnat.Label}, metadata)
return err
}
// Update updates destination-NAT44 configuration.
func (d *DNAT44Descriptor) Update(key string, oldDNAT, newDNAT *nat.DNat44, oldMetadata interface{}) (newMetadata interface{}, err error) {
obsoleteIDMappings, newIDMappings := diffIdentityMappings(oldDNAT.IdMappings, newDNAT.IdMappings)
obsoleteStMappings, newStMappings := diffStaticMappings(oldDNAT.StMappings, newDNAT.StMappings)
// remove obsolete identity mappings
for _, oldMapping := range obsoleteIDMappings {
if err = d.natHandler.DelNat44IdentityMapping(oldMapping, oldDNAT.Label); err != nil {
err = errors.Errorf("failed to remove identity mapping from DNAT %s: %v", oldDNAT.Label, err)
d.log.Error(err)
return nil, err
}
}
// remove obsolete static mappings
for _, oldMapping := range obsoleteStMappings {
if err = d.natHandler.DelNat44StaticMapping(oldMapping, oldDNAT.Label); err != nil {
err = errors.Errorf("failed to remove static mapping from DNAT %s: %v", oldDNAT.Label, err)
d.log.Error(err)
return nil, err
}
}
// add new identity mappings
for _, newMapping := range newIDMappings {
if err = d.natHandler.AddNat44IdentityMapping(newMapping, newDNAT.Label); err != nil {
err = errors.Errorf("failed to add identity mapping for DNAT %s: %v", newDNAT.Label, err)
d.log.Error(err)
return nil, err
}
}
// add new static mappings
for _, newMapping := range newStMappings {
if err = d.natHandler.AddNat44StaticMapping(newMapping, newDNAT.Label); err != nil {
err = errors.Errorf("failed to add static mapping for DNAT %s: %v", newDNAT.Label, err)
d.log.Error(err)
return nil, err
}
}
return nil, nil
}
// Retrieve returns the current NAT44 global configuration.
func (d *DNAT44Descriptor) Retrieve(correlate []adapter.DNAT44KVWithMetadata) (
retrieved []adapter.DNAT44KVWithMetadata, err error,
) {
// collect DNATs which are expected to be empty
corrEmptyDNATs := make(map[string]*nat.DNat44)
for _, kv := range correlate {
if len(kv.Value.IdMappings) == 0 && len(kv.Value.StMappings) == 0 {
corrEmptyDNATs[kv.Value.Label] = kv.Value
}
}
// dump (non-empty) DNATs
dnatDump, err := d.natHandler.DNat44Dump()
if err != nil {
d.log.Error(err)
return retrieved, err
}
// process DNAT dump
for _, dnat := range dnatDump {
if dnat.Label == "" {
// all untagged mappings are grouped under one DNAT with label <untaggedDNAT>
// - they will get removed by resync (not configured by agent, or tagging has failed)
dnat.Label = untaggedDNAT
}
if _, expectedToBeEmpty := corrEmptyDNATs[dnat.Label]; expectedToBeEmpty {
// a DNAT mapping which is expected to be empty, but actually is not
delete(corrEmptyDNATs, dnat.Label)
}
retrieved = append(retrieved, adapter.DNAT44KVWithMetadata{
Key: nat.DNAT44Key(dnat.Label),
Value: dnat,
Origin: kvs.FromNB,
})
}
// add empty DNATs (nothing from them is dumped)
for dnatLabel, dnat := range corrEmptyDNATs {
retrieved = append(retrieved, adapter.DNAT44KVWithMetadata{
Key: nat.DNAT44Key(dnatLabel),
Value: dnat,
Origin: kvs.FromNB,
})
}
return retrieved, nil
}
// Dependencies lists external interfaces and non-zero VRFs from mappings as dependencies.
func (d *DNAT44Descriptor) Dependencies(key string, dnat *nat.DNat44) (dependencies []kvs.Dependency) {
// collect referenced external interfaces and VRFs
externalIfaces := make(map[string]struct{})
vrfs := make(map[uint32]struct{})
for _, mapping := range dnat.StMappings {
if mapping.ExternalInterface != "" {
externalIfaces[mapping.ExternalInterface] = struct{}{}
}
for _, localIP := range mapping.LocalIps {
vrfs[localIP.VrfId] = struct{}{}
}
}
for _, mapping := range dnat.IdMappings {
if mapping.Interface != "" {
externalIfaces[mapping.Interface] = struct{}{}
}
vrfs[mapping.VrfId] = struct{}{}
}
// for every external interface add one dependency
for externalIface := range externalIfaces {
dependencies = append(dependencies, kvs.Dependency{
Label: mappingInterfaceDep + "-" + externalIface,
Key: interfaces.InterfaceKey(externalIface),
})
}
// for every non-zero VRF add one dependency
for vrf := range vrfs {
if vrf == 0 {
continue
}
dependencies = append(dependencies, kvs.Dependency{
Label: mappingVrfDep + "-" + strconv.Itoa(int(vrf)),
Key: l3.VrfTableKey(vrf, l3.VrfTable_IPV4),
})
}
return dependencies
}
// diffIdentityMappings compares two *sets* of identity mappings.
func diffIdentityMappings(
oldIDMappings, newIDMappings []*nat.DNat44_IdentityMapping) (obsoleteMappings, newMappings []*nat.DNat44_IdentityMapping) {
for _, oldMapping := range oldIDMappings {
found := false
for _, newMapping := range newIDMappings {
if proto.Equal(oldMapping, newMapping) {
found = true
break
}
}
if !found {
obsoleteMappings = append(obsoleteMappings, oldMapping)
}
}
for _, newMapping := range newIDMappings {
found := false
for _, oldMapping := range oldIDMappings {
if proto.Equal(oldMapping, newMapping) {
found = true
break
}
}
if !found {
newMappings = append(newMappings, newMapping)
}
}
return obsoleteMappings, newMappings
}
// diffStaticMappings compares two *sets* of static mappings.
func diffStaticMappings(
oldStMappings, newStMappings []*nat.DNat44_StaticMapping) (obsoleteMappings, newMappings []*nat.DNat44_StaticMapping) {
for _, oldMapping := range oldStMappings {
found := false
for _, newMapping := range newStMappings {
if equivalentStaticMappings(oldMapping, newMapping) {
found = true
break
}
}
if !found {
obsoleteMappings = append(obsoleteMappings, oldMapping)
}
}
for _, newMapping := range newStMappings {
found := false
for _, oldMapping := range oldStMappings {
if equivalentStaticMappings(oldMapping, newMapping) {
found = true
break
}
}
if !found {
newMappings = append(newMappings, newMapping)
}
}
return obsoleteMappings, newMappings
}
// equivalentStaticMappings compares two static mappings for equality.
func equivalentStaticMappings(stMapping1, stMapping2 *nat.DNat44_StaticMapping) bool {
// attributes compared as usually
if stMapping1.Protocol != stMapping2.Protocol || stMapping1.ExternalPort != stMapping2.ExternalPort ||
stMapping1.ExternalIp != stMapping2.ExternalIp || stMapping1.ExternalInterface != stMapping2.ExternalInterface ||
stMapping1.TwiceNat != stMapping2.TwiceNat || stMapping1.SessionAffinity != stMapping1.SessionAffinity {
return false
}
// compare locals ignoring their order
for _, localIP1 := range stMapping1.LocalIps {
found := false
for _, localIP2 := range stMapping2.LocalIps {
if proto.Equal(localIP1, localIP2) {
found = true
break
}
}
if !found {
return false
}
}
for _, localIP2 := range stMapping2.LocalIps {
found := false
for _, localIP1 := range stMapping1.LocalIps {
if proto.Equal(localIP1, localIP2) {
found = true
break
}
}
if !found {
return false
}
}
return true
}