forked from ligato/vpp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_resync.go
313 lines (281 loc) · 9.45 KB
/
data_resync.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
// 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.
package l2plugin
import (
"strings"
"github.com/ligato/vpp-agent/plugins/vpp/l2plugin/l2idx"
"github.com/ligato/vpp-agent/plugins/vpp/model/l2"
)
// Resync writes missing BDs to the VPP and removes obsolete ones.
func (plugin *BDConfigurator) Resync(nbBDs []*l2.BridgeDomains_BridgeDomain) error {
plugin.log.WithField("cfg", plugin).Debug("RESYNC BDs begin.")
// Calculate and log bd resync.
defer func() {
if plugin.stopwatch != nil {
plugin.stopwatch.PrintLog()
}
}()
// Re-initialize cache
plugin.clearMapping()
// Dump current state of the VPP bridge domains
vppBDs, err := plugin.bdHandler.DumpBridgeDomains()
if err != nil {
return err
}
// Correlate with NB config
var wasErr error
for vppBDIdx, vppBD := range vppBDs {
// tag is bridge domain name (unique identifier)
tag := vppBD.Bd.Name
// Find NB bridge domain with the same name
var nbBD *l2.BridgeDomains_BridgeDomain
for _, nbBDConfig := range nbBDs {
if tag == nbBDConfig.Name {
nbBD = nbBDConfig
break
}
}
// NB config does not exist, VPP bridge domain is obsolete
if nbBD == nil {
if err := plugin.deleteBridgeDomain(&l2.BridgeDomains_BridgeDomain{
Name: tag,
}, vppBDIdx); err != nil {
plugin.log.Error(err)
continue
}
plugin.log.Debugf("RESYNC bridge domain: obsolete config %v (ID %v) removed", tag, vppBDIdx)
} else {
// Bridge domain exists, validate
valid, recreate := plugin.vppValidateBridgeDomainBVI(nbBD, &l2.BridgeDomains_BridgeDomain{
Name: tag,
Learn: vppBD.Bd.Learn,
Flood: vppBD.Bd.Flood,
Forward: vppBD.Bd.Forward,
UnknownUnicastFlood: vppBD.Bd.UnknownUnicastFlood,
ArpTermination: vppBD.Bd.ArpTermination,
MacAge: vppBD.Bd.MacAge,
})
if !valid {
plugin.log.Errorf("RESYNC bridge domain: new config %v is invalid", nbBD.Name)
continue
}
if recreate {
// Internal bridge domain parameters changed, cannot be modified and will be recreated
if err := plugin.deleteBridgeDomain(&l2.BridgeDomains_BridgeDomain{
Name: tag,
}, vppBDIdx); err != nil {
plugin.log.Error(err)
continue
}
if err := plugin.ConfigureBridgeDomain(nbBD); err != nil {
plugin.log.Error(err)
continue
}
plugin.log.Debugf("RESYNC bridge domains: config %v recreated", nbBD.Name)
continue
}
// todo currently it is not possible to dump interfaces. In order to prevent BD removal, unset all available interfaces
// Dump all interfaces
interfaceMap, err := plugin.ifHandler.DumpInterfaces()
if err != nil {
plugin.log.Error(err)
wasErr = err
continue
}
// Prepare a list of interface objects with proper name
var interfacesToUnset []*l2.BridgeDomains_BridgeDomain_Interfaces
for _, iface := range interfaceMap {
interfacesToUnset = append(interfacesToUnset, &l2.BridgeDomains_BridgeDomain_Interfaces{
Name: iface.Interface.Name,
})
}
// Remove interfaces from bridge domain. Attempt to unset interface which does not belong to the bridge domain
// does not cause an error
if _, err := plugin.bdHandler.UnsetInterfacesFromBridgeDomain(nbBD.Name, vppBDIdx, interfacesToUnset, plugin.ifIndexes); err != nil {
return err
}
// Set all new interfaces to the bridge domain
// todo there is no need to calculate diff from configured interfaces, because currently all available interfaces are set here
configuredIfs, err := plugin.bdHandler.SetInterfacesToBridgeDomain(nbBD.Name, vppBDIdx, nbBD.Interfaces, plugin.ifIndexes)
if err != nil {
return err
}
// todo VPP does not support ARP dump, they can be only added at this time
// Resolve new ARP entries
for _, arpEntry := range nbBD.ArpTerminationTable {
if err := plugin.bdHandler.VppAddArpTerminationTableEntry(vppBDIdx, arpEntry.PhysAddress, arpEntry.IpAddress); err != nil {
plugin.log.Error(err)
wasErr = err
}
}
// Register bridge domain
plugin.bdIndexes.RegisterName(nbBD.Name, plugin.bdIDSeq, l2idx.NewBDMetadata(nbBD, configuredIfs))
plugin.bdIDSeq++
plugin.log.Debugf("RESYNC Bridge domain: config %v (ID %v) modified", tag, vppBDIdx)
}
}
// Configure new bridge domains
for _, newBD := range nbBDs {
_, _, found := plugin.bdIndexes.LookupIdx(newBD.Name)
if !found {
if err := plugin.ConfigureBridgeDomain(newBD); err != nil {
plugin.log.Error(err)
continue
}
plugin.log.Debugf("RESYNC bridge domains: new config %v added", newBD.Name)
}
}
return wasErr
}
// Resync writes missing FIBs to the VPP and removes obsolete ones.
func (plugin *FIBConfigurator) Resync(nbFIBs []*l2.FibTable_FibEntry) error {
plugin.log.WithField("cfg", plugin).Debug("RESYNC FIBs begin.")
// Calculate and log fib resync.
defer func() {
if plugin.stopwatch != nil {
plugin.stopwatch.PrintLog()
}
}()
// Re-initialize cache
plugin.clearMapping()
// Get all FIB entries configured on the VPP
vppFIBs, err := plugin.fibHandler.DumpFIBTableEntries()
if err != nil {
return err
}
// Correlate existing config with the NB
var wasErr error
for vppFIBmac, vppFIBdata := range vppFIBs {
exists, meta := func(nbFIBs []*l2.FibTable_FibEntry) (bool, *l2.FibTable_FibEntry) {
for _, nbFIB := range nbFIBs {
// Physical address
if strings.ToUpper(vppFIBmac) != strings.ToUpper(nbFIB.PhysAddress) {
continue
}
// Bridge domain
bdIdx, _, found := plugin.bdIndexes.LookupIdx(nbFIB.BridgeDomain)
if !found || vppFIBdata.Meta.BdID != bdIdx {
continue
}
// BVI
if vppFIBdata.Fib.BridgedVirtualInterface != nbFIB.BridgedVirtualInterface {
continue
}
// Interface
swIdx, _, found := plugin.ifIndexes.LookupIdx(nbFIB.OutgoingInterface)
if !found || vppFIBdata.Meta.IfIdx != swIdx {
continue
}
// Is static
if vppFIBdata.Fib.StaticConfig != nbFIB.StaticConfig {
continue
}
return true, nbFIB
}
return false, nil
}(nbFIBs)
// Register existing entries, Remove entries missing in NB config (except non-static)
if exists {
plugin.fibIndexes.RegisterName(vppFIBmac, plugin.fibIndexSeq, meta)
plugin.fibIndexSeq++
} else if vppFIBdata.Fib.StaticConfig {
// Get appropriate interface/bridge domain names
ifIdx, _, ifFound := plugin.ifIndexes.LookupName(vppFIBdata.Meta.IfIdx)
bdIdx, _, bdFound := plugin.bdIndexes.LookupName(vppFIBdata.Meta.BdID)
if !ifFound || !bdFound {
// FIB entry cannot be removed without these informations and
// it should be removed by the VPP
continue
}
plugin.Delete(&l2.FibTable_FibEntry{
PhysAddress: vppFIBmac,
OutgoingInterface: ifIdx,
BridgeDomain: bdIdx,
}, func(callbackErr error) {
if callbackErr != nil {
wasErr = callbackErr
}
})
}
}
// Configure all unregistered FIB entries from NB config
for _, nbFIB := range nbFIBs {
_, _, found := plugin.fibIndexes.LookupIdx(nbFIB.PhysAddress)
if !found {
plugin.Add(nbFIB, func(callbackErr error) {
if callbackErr != nil {
wasErr = callbackErr
}
})
}
}
plugin.log.WithField("cfg", plugin).Debug("RESYNC FIBs end.")
return wasErr
}
// Resync writes missing XCons to the VPP and removes obsolete ones.
func (plugin *XConnectConfigurator) Resync(nbXConns []*l2.XConnectPairs_XConnectPair) error {
plugin.log.WithField("cfg", plugin).Debug("RESYNC XConnect begin.")
// Calculate and log xConnect resync.
defer func() {
if plugin.stopwatch != nil {
plugin.stopwatch.PrintLog()
}
}()
// Re-initialize cache
plugin.clearMapping()
// Read cross connects from the VPP
vppXConns, err := plugin.xcHandler.DumpXConnectPairs()
if err != nil {
return err
}
// Correlate with NB config
var wasErr error
for _, vppXConn := range vppXConns {
var existsInNB bool
var rxIfName, txIfName string
for _, nbXConn := range nbXConns {
// Find receive and transmit interface
rxIfName, _, rxIfFound := plugin.ifIndexes.LookupName(vppXConn.Meta.ReceiveInterfaceSwIfIdx)
txIfName, _, txIfFound := plugin.ifIndexes.LookupName(vppXConn.Meta.TransmitInterfaceSwIfIdx)
if !rxIfFound || !txIfFound {
continue
}
if rxIfName == nbXConn.ReceiveInterface && txIfName == nbXConn.TransmitInterface {
// NB XConnect correlated with VPP
plugin.xcIndexes.RegisterName(nbXConn.ReceiveInterface, plugin.xcIndexSeq, nbXConn)
plugin.xcIndexSeq++
existsInNB = true
}
}
if !existsInNB {
if err := plugin.DeleteXConnectPair(&l2.XConnectPairs_XConnectPair{
ReceiveInterface: rxIfName,
TransmitInterface: txIfName,
}); err != nil {
wasErr = err
}
}
}
// Configure new XConnect pairs
for _, nbXConn := range nbXConns {
_, _, found := plugin.xcIndexes.LookupIdx(nbXConn.ReceiveInterface)
if !found {
if err := plugin.ConfigureXConnectPair(nbXConn); err != nil {
wasErr = err
}
}
}
plugin.log.WithField("cfg", plugin).Debug("RESYNC XConnect end. ", wasErr)
return wasErr
}