forked from ligato/vpp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
l4_configurator.go
270 lines (223 loc) · 9.99 KB
/
l4_configurator.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
// Copyright (c) 2017 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.
//go:generate protoc --proto_path=model/l4 --gogo_out=model/l4 model/l4/l4.proto
//go:generate binapi-generator --input-file=/usr/share/vpp/api/session.api.json --output-dir=bin_api
package l4plugin
import (
govppapi "git.fd.io/govpp.git/api"
"github.com/ligato/cn-infra/logging"
"github.com/ligato/cn-infra/logging/measure"
"github.com/ligato/cn-infra/servicelabel"
"github.com/ligato/vpp-agent/plugins/defaultplugins/ifplugin/ifaceidx"
"github.com/ligato/vpp-agent/plugins/defaultplugins/l4plugin/model/l4"
"github.com/ligato/vpp-agent/plugins/defaultplugins/l4plugin/nsidx"
"github.com/ligato/vpp-agent/plugins/defaultplugins/l4plugin/vppcalls"
"github.com/ligato/vpp-agent/plugins/govppmux"
)
// L4Configurator runs in the background in its own goroutine where it watches for any changes
// in the configuration of interfaces as modelled by the proto file "../model/l4/l4.proto"
// and stored in ETCD under the keys "/vnf-agent/{vnf-agent}/vpp/config/v1/l4/l4ftEnabled"
// and "/vnf-agent/{vnf-agent}/vpp/config/v1/l4/namespaces/{namespace_id}".
// Updates received from the northbound API are compared with the VPP run-time configuration and differences
// are applied through the VPP binary API.
type L4Configurator struct {
Log logging.Logger
ServiceLabel servicelabel.ReaderAPI
GoVppmux govppmux.API
// Indexes
SwIfIndexes ifaceidx.SwIfIndex
AppNsIndexes nsidx.AppNsIndexRW
NotConfiguredAppNs nsidx.AppNsIndexRW // the mapping stores not-configurable app namespaces with metadata
AppNsIdxSeq uint32 // common for both mappings, should be incremented after every registration
// timer used to measure and store time
Stopwatch *measure.Stopwatch
// channel to communicate with the vpp
vppCh *govppapi.Channel
// Features flag - internal state whether the L4 l4ftEnabled are enabled or disabled
l4ftEnabled bool
}
// Init members (channels...) and start go routines
func (plugin *L4Configurator) Init() error {
plugin.Log.Debugf("Init L4Configurator plugin")
var err error
// init vpp channel
plugin.vppCh, err = plugin.GoVppmux.NewAPIChannel()
if err != nil {
return err
}
return nil
}
// Close members, channels
func (plugin *L4Configurator) Close() error {
return nil
}
// ConfigureL4FeatureFlag process the NB Features config and propagates it to bin api calls
func (plugin *L4Configurator) ConfigureL4FeatureFlag(features *l4.L4Features) error {
plugin.Log.Info("Configuring L4 l4ftEnabled")
if features.Enabled {
if err := vppcalls.EnableL4Features(plugin.Log, plugin.vppCh); err != nil {
return err
}
plugin.l4ftEnabled = true
plugin.Log.Infof("L4 l4ftEnabled were enabled")
plugin.resolveCachedNamespaces()
} else {
if err := vppcalls.DisableL4Features(plugin.Log, plugin.vppCh); err != nil {
return err
}
plugin.l4ftEnabled = false
plugin.Log.Infof("L4 l4ftEnabled were disabled")
}
return nil
}
// DeleteL4FeatureFlag process the NB Features config and propagates it to bin api calls
func (plugin *L4Configurator) DeleteL4FeatureFlag() error {
plugin.Log.Info("Deleting L4 l4ftEnabled")
if err := vppcalls.DisableL4Features(plugin.Log, plugin.vppCh); err != nil {
return err
}
plugin.l4ftEnabled = false
plugin.Log.Infof("L4 l4ftEnabled were disabled")
return nil
}
// ConfigureAppNamespace process the NB AppNamespace config and propagates it to bin api calls
func (plugin *L4Configurator) ConfigureAppNamespace(ns *l4.AppNamespaces_AppNamespace) error {
plugin.Log.Infof("Configuring new AppNamespace with ID %v", ns.NamespaceId)
// Check whether L4 l4ftEnabled are enabled. If not, all namespaces created earlier are added to cache
if !plugin.l4ftEnabled {
plugin.NotConfiguredAppNs.RegisterName(ns.NamespaceId, plugin.AppNsIdxSeq, ns)
plugin.Log.Infof("Unable to configure application namespace %v due to disabled L4 features, moving to cache", ns.NamespaceId)
plugin.AppNsIdxSeq++
return nil
}
// Find interface. If not found, add to cache for not configured namespaces
ifIdx, _, found := plugin.SwIfIndexes.LookupIdx(ns.Interface)
if !found {
plugin.NotConfiguredAppNs.RegisterName(ns.NamespaceId, plugin.AppNsIdxSeq, ns)
plugin.Log.Infof("Unable to configure application namespace %v due to missing interface, moving to cache", ns.NamespaceId)
plugin.AppNsIdxSeq++
return nil
}
return plugin.configureAppNamespace(ns, ifIdx)
}
// ModifyAppNamespace process the NB AppNamespace config and propagates it to bin api calls
func (plugin *L4Configurator) ModifyAppNamespace(newNs *l4.AppNamespaces_AppNamespace, oldNs *l4.AppNamespaces_AppNamespace) error {
plugin.Log.Infof("Modifying AppNamespace with ID %v", newNs.NamespaceId)
// At first, unregister the old configuration from both mappings (if exists)
plugin.AppNsIndexes.UnregisterName(oldNs.NamespaceId)
plugin.NotConfiguredAppNs.UnregisterName(oldNs.NamespaceId)
// Check whether L4 l4ftEnabled are enabled. If not, all namespaces created earlier are added to cache
if !plugin.l4ftEnabled {
plugin.NotConfiguredAppNs.RegisterName(newNs.NamespaceId, plugin.AppNsIdxSeq, newNs)
plugin.Log.Infof("Unable to modify application namespace %v due to disabled L4 features, moving to cache", newNs.NamespaceId)
plugin.AppNsIdxSeq++
return nil
}
// Check interface
ifIdx, _, found := plugin.SwIfIndexes.LookupIdx(newNs.Interface)
if !found {
plugin.NotConfiguredAppNs.RegisterName(newNs.NamespaceId, plugin.AppNsIdxSeq, newNs)
plugin.Log.Infof("Unable to modify application namespace %v due to missing interface, moving to cache", newNs.NamespaceId)
plugin.AppNsIdxSeq++
return nil
}
return plugin.configureAppNamespace(newNs, ifIdx)
}
// DeleteAppNamespace process the NB AppNamespace config and propagates it to bin api calls. This case is not currently
// supported by VPP
func (plugin *L4Configurator) DeleteAppNamespace(ns *l4.AppNamespaces_AppNamespace) error {
// todo implement
plugin.Log.Warn("AppNamespace removal not supported by VPP")
return nil
}
// ResolveCreatedInterface looks for application namespace this interface is assigned to and configures them
func (plugin *L4Configurator) ResolveCreatedInterface(interfaceName string, interfaceIndex uint32) error {
plugin.Log.Infof("L4Plugin: Resolving new interface %v", interfaceName)
// If L4 features are not enabled, skip (and keep all in cache)
if !plugin.l4ftEnabled {
return nil
}
// Search mapping for unregistered application namespaces using the new interface
appNamespaces := plugin.NotConfiguredAppNs.LookupNamesByInterface(interfaceName)
if len(appNamespaces) > 0 {
plugin.Log.Debugf("Found %v app namespaces for interface %v", len(appNamespaces), interfaceName)
for _, appNamespace := range appNamespaces {
if err := plugin.configureAppNamespace(appNamespace, interfaceIndex); err != nil {
return err
}
// Remove from cache
plugin.NotConfiguredAppNs.UnregisterName(appNamespace.NamespaceId)
}
}
return nil
}
// ResolveDeletedInterface looks for application namespace this interface is assigned to and removes
func (plugin *L4Configurator) ResolveDeletedInterface(interfaceName string, interfaceIndex uint32) error {
plugin.Log.Infof("L4Plugin: Resolving removed interface %v", interfaceName)
// Search mapping for configured application namespaces using the new interface
confAppNs := plugin.NotConfiguredAppNs.LookupNamesByInterface(interfaceName)
if len(confAppNs) > 0 {
plugin.Log.Debugf("Found %v app namespaces belonging to removed interface %v", len(confAppNs), interfaceName)
for _, appNamespace := range confAppNs {
// todo remove namespace. Also check whether it can be done while L4Features are disabled
// Unregister from configured namespaces mapping
plugin.AppNsIndexes.UnregisterName(appNamespace.NamespaceId)
// Add to un-configured. If the interface will be recreated, all namespaces are configured back
plugin.NotConfiguredAppNs.RegisterName(appNamespace.NamespaceId, plugin.AppNsIdxSeq, appNamespace)
plugin.AppNsIdxSeq++
}
}
return nil
}
func (plugin *L4Configurator) configureAppNamespace(ns *l4.AppNamespaces_AppNamespace, ifIdx uint32) error {
// Namespace ID
nsID := []byte(ns.NamespaceId)
err := vppcalls.AddAppNamespace(ns.Secret, ifIdx, ns.Ipv4FibId, ns.Ipv6FibId, nsID, plugin.Log, plugin.vppCh)
if err != nil {
return err
}
// register namespace
plugin.AppNsIndexes.RegisterName(ns.NamespaceId, plugin.AppNsIdxSeq, ns)
plugin.AppNsIdxSeq++
plugin.Log.Debugf("Application namespace %v registered")
return nil
}
// An application namespace can be cached from two reasons:
// - the required interface was missing
// - the L4 features were disabled
// Namespaces skipped due to the second case are configured here
func (plugin *L4Configurator) resolveCachedNamespaces() {
plugin.Log.Info("Configuring cached namespaces after L4 features were enabled")
index := plugin.AppNsIdxSeq
// Scan all registered indexes in mapping for un-configured application namespaces
for {
if index == 0 {
break
}
index--
_, ns, found := plugin.NotConfiguredAppNs.LookupName(index)
if !found {
continue
}
// Check interface. If still missing, continue (keep namespace in cache)
ifIdx, _, found := plugin.SwIfIndexes.LookupIdx(ns.Interface)
if !found {
plugin.Log.Infof("Unable to configure application namespace %v due to missing interface, keeping in cache", ns.NamespaceId)
continue
}
plugin.configureAppNamespace(ns, ifIdx)
// AppNamespace was configured, remove drom cache
plugin.NotConfiguredAppNs.UnregisterName(ns.NamespaceId)
}
}