forked from ligato/vpp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_resync.go
251 lines (212 loc) · 8.12 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
// 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 ifplugin
import (
"github.com/ligato/cn-infra/core"
"github.com/ligato/cn-infra/logging"
"github.com/ligato/cn-infra/logging/logroot"
"github.com/ligato/vpp-agent/idxvpp/nametoidx"
"github.com/ligato/vpp-agent/idxvpp/persist"
"github.com/ligato/vpp-agent/plugins/defaultplugins/ifplugin/model/bfd"
intf "github.com/ligato/vpp-agent/plugins/defaultplugins/ifplugin/model/interfaces"
"github.com/ligato/vpp-agent/plugins/defaultplugins/ifplugin/model/stn"
"github.com/ligato/vpp-agent/plugins/defaultplugins/ifplugin/vppdump"
)
// Resync writes interfaces to the empty VPP
//
// - resyncs the VPP
// - temporary: (checks wether sw_if_indexes are not obsolate - this will be swapped with master ID)
// - deletes obsolate status data
func (plugin *InterfaceConfigurator) Resync(nbIfaces []*intf.Interfaces_Interface) error {
plugin.Log.WithField("cfg", plugin).Debug("RESYNC Interface begin.")
// Calculate and log interface resync
defer func() {
if plugin.Stopwatch != nil {
plugin.Stopwatch.PrintLog()
}
}()
// Step 0: Dump current state of the VPP
vppIfaces, err := vppdump.DumpInterfaces(plugin.Log, plugin.vppCh, plugin.Stopwatch)
if err != nil {
return err
}
plugin.Log.Debug("VPP contains len(vppIfaces)=", len(vppIfaces))
// Step 1: Correlate vppIfaces with northbound interfaces
// it means to find out names for vpp swIndexes
// (temporary: correlate using persisted sw_if_indexes)
corr := nametoidx.NewNameToIdx(logroot.StandardLogger(), core.PluginName("defaultvppplugins-ifplugin"), "iface resync corr", nil)
if !plugin.resyncDoneOnce { //probably shortly after startup
// we temporary load the last state from the file (in case the agent crashed)
// later we use the VPP Master ID to correlate
tmpCorr := nametoidx.NewNameToIdx(logroot.StandardLogger(), core.PluginName("defaultvppplugins-ifplugin"), "iface resync corr", nil)
err = persist.Marshalling(plugin.ServiceLabel.GetAgentLabel(), plugin.swIfIndexes.GetMapping(), tmpCorr)
if err != nil {
return err
}
plugin.resyncDoneOnce = true
// check if all loaded indexes are still in VPP (remove all sw_if_idx not contained in the VPP dump)
for _, nbIface := range nbIfaces {
if vppSwIfIdx, meta, found := tmpCorr.LookupIdx(nbIface.Name); found {
corr.RegisterName(nbIface.Name, vppSwIfIdx, meta)
plugin.Log.WithField("swIfIndex", vppSwIfIdx).Debug("Correlation ", nbIface.Name)
}
}
}
var wasError error
// Step 2: delete obsolete vpp configuration
for vppSwIfIdx, vppIface := range vppIfaces {
_, _, found := corr.LookupName(vppSwIfIdx)
if vppSwIfIdx == 0 {
// local0 - default loopback interface
plugin.swIfIndexes.RegisterName(vppIface.VPPInternalName, vppSwIfIdx, &vppIface.Interfaces_Interface)
} else if vppIface.Type == intf.InterfaceType_ETHERNET_CSMACD {
// physical interface (PCI device)
plugin.swIfIndexes.RegisterName(vppIface.VPPInternalName, vppSwIfIdx, &vppIface.Interfaces_Interface)
} else if !found {
err := plugin.deleteVPPInterface(&vppIface.Interfaces_Interface, vppSwIfIdx)
plugin.Log.WithFields(logging.Fields{"swIfIndex": vppSwIfIdx, "vppIface": vppIface}).
Info("Interface deletion ", err)
if err != nil {
wasError = err
}
}
}
toBeConfigured := []*intf.Interfaces_Interface{}
// Step 3: modify existing vpp configuration
for _, nbIface := range nbIfaces {
swIfIdx, _, found := corr.LookupIdx(nbIface.Name)
vppIface, foundDump := vppIfaces[swIfIdx]
if found && foundDump {
err := plugin.modifyVPPInterface(nbIface, &vppIface.Interfaces_Interface, swIfIdx, vppIface.Type)
if err != nil {
wasError = err
}
} else {
toBeConfigured = append(toBeConfigured, nbIface)
}
}
// Step 4: create missing vpp configuration
for _, nbIface := range toBeConfigured {
err := plugin.ConfigureVPPInterface(nbIface)
if err != nil {
wasError = err
}
}
plugin.Log.WithField("cfg", plugin).Debug("RESYNC Interface end. ", wasError)
return wasError
}
// VerifyVPPConfigPresence dumps VPP interface configuration on the vpp. If there are any interfaces configured (except
// the local0), it returns false (do not interrupt the resto of the resync), otherwise returns true
func (plugin *InterfaceConfigurator) VerifyVPPConfigPresence(nbIfaces []*intf.Interfaces_Interface) bool {
plugin.Log.WithField("cfg", plugin).Debug("RESYNC Interface begin.")
// notify that the resync should be stopped
var stop bool
// Step 0: Dump actual state of the VPP
vppIfaces, err := vppdump.DumpInterfaces(plugin.Log, plugin.vppCh, plugin.Stopwatch)
if err != nil {
return stop
}
// The strategy is optimize-cold-start, so look over all dumped VPP interfaces and check for the configured ones
// (leave out the local0). If there are any other interfaces, return true (resync will continue).
// If not, return a false flag which cancels the VPP resync operation.
plugin.Log.Info("optimize-cold-start VPP resync strategy chosen, resolving...")
if len(vppIfaces) == 0 {
stop = true
plugin.Log.Infof("...VPP resync interrupted assuming there is no configuration on the VPP (no interface was found)")
return stop
}
// in interface exists, try to find local0 interface (index 0)
_, ok := vppIfaces[0]
// in case local0 is the only interface on the vpp, stop the resync
if len(vppIfaces) == 1 && ok {
stop = true
plugin.Log.Infof("...VPP resync interrupted assuming there is no configuration on the VPP (only local0 was found)")
return stop
}
// otherwise continue normally
plugin.Log.Infof("... VPP configuration found, continue with VPP resync")
return stop
}
// ResyncSession writes BFD sessions to the empty VPP
func (plugin *BFDConfigurator) ResyncSession(bfds []*bfd.SingleHopBFD_Session) error {
plugin.Log.WithField("cfg", plugin).Debug("RESYNC BFD Session begin.")
// Calculate and log bfd resync
defer func() {
if plugin.Stopwatch != nil {
plugin.Stopwatch.PrintLog()
}
}()
// lookup BFD sessions
err := plugin.LookupBfdSessions()
if err != nil {
return err
}
var wasError error
// create BFD sessions
for _, bfdSession := range bfds {
err = plugin.ConfigureBfdSession(bfdSession)
if err != nil {
wasError = err
}
}
plugin.Log.WithField("cfg", plugin).Debug("RESYNC BFD Session end. ", wasError)
return wasError
}
// ResyncAuthKey writes BFD keys to the empty VPP
func (plugin *BFDConfigurator) ResyncAuthKey(bfds []*bfd.SingleHopBFD_Key) error {
plugin.Log.WithField("cfg", plugin).Debug("RESYNC BFD Keys begin.")
// Calculate and log bfd resync
defer func() {
if plugin.Stopwatch != nil {
plugin.Stopwatch.PrintLog()
}
}()
// lookup BFD auth keys
err := plugin.LookupBfdKeys()
if err != nil {
return err
}
var wasError error
// create BFD auth keys
for _, bfdKey := range bfds {
err = plugin.ConfigureBfdAuthKey(bfdKey)
if err != nil {
wasError = err
}
}
plugin.Log.WithField("cfg", plugin).Debug("RESYNC BFD Keys end. ", wasError)
return wasError
}
// ResyncEchoFunction writes BFD echo function to the empty VPP
func (plugin *BFDConfigurator) ResyncEchoFunction(bfds []*bfd.SingleHopBFD_EchoFunction) error {
return nil
}
// Resync writes stn rule to the the empty VPP
func (plugin *StnConfigurator) Resync(stnRules []*stn.StnRule) error {
plugin.Log.WithField("cfg", plugin).Debug("RESYNC stn rules begin. ")
// Calculate and log stn rules resync
defer func() {
if plugin.Stopwatch != nil {
plugin.Stopwatch.PrintLog()
}
}()
var wasError error
if len(stnRules) > 0 {
for _, rule := range stnRules {
wasError = plugin.Add(rule)
}
}
plugin.Log.WithField("cfg", plugin).Debug("RESYNC stn rules end. ", wasError)
return wasError
}