-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathnat44_global_address.go
126 lines (111 loc) · 4.44 KB
/
nat44_global_address.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
// 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 (
"errors"
"net"
"go.ligato.io/cn-infra/v2/logging"
"google.golang.org/protobuf/proto"
kvs "go.ligato.io/vpp-agent/v3/plugins/kvscheduler/api"
"go.ligato.io/vpp-agent/v3/plugins/vpp/natplugin/descriptor/adapter"
"go.ligato.io/vpp-agent/v3/plugins/vpp/natplugin/vppcalls"
l3 "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/l3"
nat "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/nat"
)
const (
// NAT44GlobalAddressDescriptorName is the name of the descriptor for IP addresses
// from the VPP NAT44 address pool.
NAT44GlobalAddressDescriptorName = "vpp-nat44-global-address"
// dependency labels
addressVrfDep = "vrf-table-exists"
addressEpModeDep = "nat44-is-in-endpoint-dependent-mode"
)
// A list of non-retriable errors:
var (
// ErrInvalidNATAddress is returned when IP address from VPP NAT address pool
// cannot be parsed.
ErrInvalidNATAddress = errors.New("Invalid VPP NAT address")
)
// NAT44GlobalAddressDescriptor teaches KVScheduler how to add/remove IP addresses
// to/from the VPP NAT44 address pool.
// Deprecated. Functionality moved to NAT44AddressPoolDescriptor. Kept for backward compatibility.
type NAT44GlobalAddressDescriptor struct {
log logging.Logger
natHandler vppcalls.NatVppAPI
}
// NewNAT44GlobalAddressDescriptor creates a new instance of the NAT44Address descriptor.
// Deprecated. Functionality moved to NAT44AddressPoolDescriptor. Kept for backward compatibility.
func NewNAT44GlobalAddressDescriptor(natHandler vppcalls.NatVppAPI, log logging.PluginLogger) *kvs.KVDescriptor {
ctx := &NAT44GlobalAddressDescriptor{
natHandler: natHandler,
log: log.NewLogger("nat44-global-address-descriptor"),
}
typedDescr := &adapter.NAT44GlobalAddressDescriptor{
Name: NAT44GlobalAddressDescriptorName,
KeySelector: ctx.IsNat44DerivedAddressKey,
ValueTypeName: string(proto.MessageName(&nat.Nat44Global_Address{})),
Create: ctx.Create,
Delete: ctx.Delete,
Dependencies: ctx.Dependencies,
}
return adapter.NewNAT44GlobalAddressDescriptor(typedDescr)
}
// IsNat44DerivedAddressKey returns true if the key is identifying configuration
// for a single address from the NAT44 address pool.
func (d *NAT44GlobalAddressDescriptor) IsNat44DerivedAddressKey(key string) bool {
_, _, isNATAddrKey := nat.ParseDerivedAddressNAT44Key(key)
return isNATAddrKey
}
// Validate validates configuration for NAT44 address.
func (d *NAT44GlobalAddressDescriptor) Validate(key string, natAddr *nat.Nat44Global_Address) error {
ipAddr := net.ParseIP(natAddr.Address)
if ipAddr == nil {
return kvs.NewInvalidValueError(ErrInvalidNATAddress, "address")
}
return nil
}
// Create adds IP address into the NAT44 address pool.
func (d *NAT44GlobalAddressDescriptor) Create(key string, natAddr *nat.Nat44Global_Address) (metadata interface{}, err error) {
err = d.natHandler.AddNat44AddressPool(natAddr.VrfId, natAddr.Address, "", natAddr.TwiceNat)
if err != nil {
d.log.Error(err)
return nil, err
}
return nil, nil
}
// Delete removes IP address from the NAT44 address pool.
func (d *NAT44GlobalAddressDescriptor) Delete(key string, natAddr *nat.Nat44Global_Address, metadata interface{}) error {
err := d.natHandler.DelNat44AddressPool(natAddr.VrfId, natAddr.Address, "", natAddr.TwiceNat)
if err != nil {
d.log.Error(err)
return err
}
return nil
}
// Dependencies lists endpoint-dependent mode and non-zero VRF as dependencies.
func (d *NAT44GlobalAddressDescriptor) Dependencies(key string, natAddr *nat.Nat44Global_Address) (deps []kvs.Dependency) {
if natAddr.VrfId != 0 && natAddr.VrfId != ^uint32(0) {
deps = append(deps, kvs.Dependency{
Label: addressVrfDep,
Key: l3.VrfTableKey(natAddr.VrfId, l3.VrfTable_IPV4),
})
}
if !d.natHandler.WithLegacyStartupConf() {
deps = append(deps, kvs.Dependency{
Label: addressEpModeDep,
Key: nat.Nat44EndpointDepKey,
})
}
return deps
}