forked from ligato/vpp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xconnect_config.go
347 lines (299 loc) · 12.1 KB
/
xconnect_config.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
// 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 (
"fmt"
"time"
govppapi "git.fd.io/govpp.git/api"
"git.fd.io/govpp.git/core/bin_api/vpe"
"github.com/ligato/cn-infra/logging"
"github.com/ligato/cn-infra/logging/measure"
"github.com/ligato/cn-infra/utils/safeclose"
"github.com/ligato/vpp-agent/idxvpp"
"github.com/ligato/vpp-agent/idxvpp/nametoidx"
"github.com/ligato/vpp-agent/plugins/defaultplugins/ifplugin/ifaceidx"
l2ba "github.com/ligato/vpp-agent/plugins/defaultplugins/l2plugin/bin_api/l2"
"github.com/ligato/vpp-agent/plugins/defaultplugins/l2plugin/model/l2"
"github.com/ligato/vpp-agent/plugins/defaultplugins/l2plugin/vppcalls"
"github.com/ligato/vpp-agent/plugins/govppmux"
)
const (
transmitInterfaceKey = "TransmitInterface"
)
// XConnectConfigurator implements PluginHandlerVPP.
type XConnectConfigurator struct {
Log logging.Logger
GoVppmux govppmux.API
SwIfIndexes ifaceidx.SwIfIndex
XcIndexes idxvpp.NameToIdxRW
XcIndexSeq uint32
vppChan *govppapi.Channel
Stopwatch *measure.Stopwatch // timer used to measure and store time
}
// XConnectMeta meta holds info about transmit interface.
type XConnectMeta struct {
TransmitInterface string
configured bool // true if already configured on VPP, false if still waiting for creation of the rx or tx interface
}
// Init members (channels...) and start go routines.
func (plugin *XConnectConfigurator) Init() (err error) {
plugin.Log.Debug("Initializing L2 xConnect")
// Init VPP API channel.
plugin.vppChan, err = plugin.GoVppmux.NewAPIChannel()
if err != nil {
return err
}
// Check bin api message compatibility.
err = vppcalls.CheckMsgCompatibilityForL2XConnect(plugin.Log, plugin.vppChan)
if err != nil {
return err
}
plugin.XcIndexes = nametoidx.NewNameToIdx(plugin.Log, "l2plugin", "xconnect", func(meta interface{}) map[string][]string {
res := map[string][]string{}
if xc, ok := meta.(*XConnectMeta); ok {
res[transmitInterfaceKey] = []string{xc.TransmitInterface}
}
return res
})
return nil
}
// Close GOVPP channel.
func (plugin *XConnectConfigurator) Close() error {
return safeclose.Close(plugin.vppChan)
}
// ConfigureXConnectPair processes the NB config and propagates it to bin api calls.
func (plugin *XConnectConfigurator) ConfigureXConnectPair(xConnectPairInput *l2.XConnectPairs_XConnectPair) error {
plugin.Log.Infof("Configuring L2 xConnect pair %v", xConnectPairInput.ReceiveInterface)
// Find interfaces.
receiveInterfaceIndex, _, rxFound := plugin.SwIfIndexes.LookupIdx(xConnectPairInput.ReceiveInterface)
if !rxFound {
plugin.Log.WithField("Interface", xConnectPairInput.ReceiveInterface).Warn("Receive interface not found.")
}
transmitInterfaceIndex, _, txFound := plugin.SwIfIndexes.LookupIdx(xConnectPairInput.TransmitInterface)
if !txFound {
plugin.Log.WithField("Interface", xConnectPairInput.TransmitInterface).Warn("Transmit interface not found.")
}
if rxFound && txFound {
// can be configured now
err := vppcalls.VppSetL2XConnect(receiveInterfaceIndex, transmitInterfaceIndex, plugin.Log, plugin.vppChan,
measure.GetTimeLog(vpe.SwInterfaceSetL2Xconnect{}, plugin.Stopwatch))
if err != nil {
plugin.Log.WithField("Error", err).Error("Failed to create l2xConnect")
return err
}
} else {
plugin.Log.Warn("rx or tx interface not found, l2xconnect postponed")
}
// Prepare meta.
meta := XConnectMeta{
TransmitInterface: xConnectPairInput.TransmitInterface,
configured: rxFound && txFound,
}
// Register.
plugin.XcIndexes.RegisterName(xConnectPairInput.ReceiveInterface, plugin.XcIndexSeq, &meta)
plugin.XcIndexSeq++
return nil
}
// ModifyXConnectPair processes the NB config and propagates it to bin api calls.
func (plugin *XConnectConfigurator) ModifyXConnectPair(newConfig *l2.XConnectPairs_XConnectPair, oldConfig *l2.XConnectPairs_XConnectPair) error {
plugin.Log.Infof("Modifying L2 xConnect pair %v %v", oldConfig)
// interfaces
receiveInterfaceIndex, _, found := plugin.SwIfIndexes.LookupIdx(newConfig.ReceiveInterface)
if !found {
plugin.Log.WithField("Interface", newConfig.ReceiveInterface).Error("Receive interface not found.")
return nil
}
newTransmitInterfaceIndex, _, found := plugin.SwIfIndexes.LookupIdx(newConfig.TransmitInterface)
if !found {
plugin.Log.WithField("Interface", newConfig.TransmitInterface).Error("New transmit interface not found.")
return nil
}
oldTransmitInterfaceIndex, _, found := plugin.SwIfIndexes.LookupIdx(oldConfig.TransmitInterface)
if !found {
plugin.Log.WithField("Interface", newConfig.TransmitInterface).Debug("Old transmit interface not found.")
oldTransmitInterfaceIndex = 0
// do not return, not an error
}
if oldTransmitInterfaceIndex == newTransmitInterfaceIndex {
// nothing to update
return nil
} else if oldTransmitInterfaceIndex == 0 {
// Create new xConnect only.
err := vppcalls.VppSetL2XConnect(receiveInterfaceIndex, newTransmitInterfaceIndex, plugin.Log,
plugin.vppChan, measure.GetTimeLog(vpe.SwInterfaceSetL2Xconnect{}, plugin.Stopwatch))
if err != nil {
plugin.Log.WithField("Error", err).Error("Failed to set l2xConnect")
return err
}
} else {
errDel := vppcalls.VppUnsetL2XConnect(receiveInterfaceIndex, oldTransmitInterfaceIndex, plugin.Log,
plugin.vppChan, measure.GetTimeLog(vpe.SwInterfaceSetL2Xconnect{}, plugin.Stopwatch))
if errDel != nil {
plugin.Log.WithField("Error", errDel).Error("Failed to remove l2xConnect")
return errDel
}
errCreate := vppcalls.VppSetL2XConnect(receiveInterfaceIndex, newTransmitInterfaceIndex, plugin.Log,
plugin.vppChan, measure.GetTimeLog(vpe.SwInterfaceSetL2Xconnect{}, plugin.Stopwatch))
if errCreate != nil {
plugin.Log.WithField("Error", errCreate).Error("Failed to set l2xConnect")
return errCreate
}
}
meta := XConnectMeta{
TransmitInterface: newConfig.TransmitInterface,
}
plugin.XcIndexes.RegisterName(newConfig.ReceiveInterface, receiveInterfaceIndex, &meta)
plugin.XcIndexSeq++
return nil
}
// DeleteXConnectPair processes the NB config and propagates it to bin api calls.
func (plugin *XConnectConfigurator) DeleteXConnectPair(xConnectPairInput *l2.XConnectPairs_XConnectPair) error {
return plugin.deleteL2XConnectPair(xConnectPairInput.ReceiveInterface, xConnectPairInput.TransmitInterface)
}
// LookupXConnectPairs registers missing l2 xConnect pairs.
func (plugin *XConnectConfigurator) LookupXConnectPairs() error {
// L2XconnectDump time measurement
start := time.Now()
defer func() {
if plugin.Stopwatch != nil {
timeLog := measure.GetTimeLog(l2ba.L2XconnectDump{}, plugin.Stopwatch)
if timeLog != nil {
timeLog.LogTimeEntry(time.Since(start))
}
}
}()
req := &l2ba.L2XconnectDump{}
reqContext := plugin.vppChan.SendMultiRequest(req)
var index uint32 = 1
for {
msg := &l2ba.L2XconnectDetails{}
stop, err := reqContext.ReceiveReply(msg)
if err != nil {
plugin.Log.Error(err)
return err
}
if stop {
break
}
// Store name if missing.
_, _, found := plugin.XcIndexes.LookupName(index)
xcIdentifier := string(msg.RxSwIfIndex)
if !found {
plugin.Log.WithFields(logging.Fields{"Name": xcIdentifier, "Index": index}).Debug("L2 xConnect pair registered.")
plugin.XcIndexes.RegisterName(xcIdentifier, index, nil)
} else {
plugin.Log.WithFields(logging.Fields{"Name": xcIdentifier, "Index": index}).Debug("L2 xConnect pair already registered.")
}
index++
}
return nil
}
// ResolveCreatedInterface configures xconnect pairs that use the interface as rx or tx and have not been configured yet.
func (plugin *XConnectConfigurator) ResolveCreatedInterface(interfaceName string, interfaceIndex uint32) error {
plugin.Log.Infof("Resolving L2 xConnect pairs for created interface %v", interfaceName)
var err error
// Lookup for the interface in rx interfaces.
err = plugin.resolveRxInterface(interfaceName, true)
// Lookup for the interface in tx interfaces.
rxIfs := plugin.XcIndexes.LookupNameByMetadata(transmitInterfaceKey, interfaceName)
for _, rxIf := range rxIfs {
err = plugin.resolveRxInterface(rxIf, true)
}
return err
}
// ResolveDeletedInterface deletes xconnect pairs that have not been deleted yet and use the interface as rx or tx.
func (plugin *XConnectConfigurator) ResolveDeletedInterface(interfaceName string) error {
plugin.Log.Infof("Resolving L2 xConnect pairs for deleted interface %v", interfaceName)
var err error
// Lookup for the interface in rx interfaces.
err = plugin.resolveRxInterface(interfaceName, false)
// Lookup for the interface in tx interfaces.
rxIfs := plugin.XcIndexes.LookupNameByMetadata(transmitInterfaceKey, interfaceName)
for _, rxIf := range rxIfs {
err = plugin.resolveRxInterface(rxIf, false)
}
return err
}
// resolveRxInterface creates/deletes 2sxconnect in XcIndexes that has not been created yet/need to be deleted
// and have the rx interface name matching with the provided argument.
func (plugin *XConnectConfigurator) resolveRxInterface(rxIfName string, create bool) error {
var err error
idx, meta, exists := plugin.XcIndexes.LookupIdx(rxIfName)
if exists {
meta := meta.(*XConnectMeta)
if create {
// The l2xconn needs to be created.
if !meta.configured {
// not yet configured, try to configure now
err = plugin.configureL2XConnectPair(rxIfName, meta.TransmitInterface)
if err == nil {
// mark as configured and save
meta.configured = true
plugin.XcIndexes.RegisterName(rxIfName, idx, meta)
}
}
} else {
// The l2xconn needs to be deleted.
err = plugin.deleteL2XConnectPair(rxIfName, meta.TransmitInterface)
// meta deleted in deleteL2XConnectPair
}
}
return err
}
func (plugin *XConnectConfigurator) configureL2XConnectPair(rxIf, txIf string) error {
plugin.Log.Infof("Configuring L2 xConnect pair %v %v", rxIf, txIf)
// Find interface idx-es.
receiveInterfaceIndex, _, found := plugin.SwIfIndexes.LookupIdx(rxIf)
if !found {
plugin.Log.WithField("Interface", rxIf).Warn("Receive interface not found.")
return fmt.Errorf("receive interface '%s' not found", rxIf)
}
transmitInterfaceIndex, _, found := plugin.SwIfIndexes.LookupIdx(txIf)
if !found {
plugin.Log.WithField("Interface", txIf).Warn("Transmit interface not found.")
return fmt.Errorf("transmit interface '%s' not found", txIf)
}
// Configure l2xconnect.
err := vppcalls.VppSetL2XConnect(receiveInterfaceIndex, transmitInterfaceIndex, plugin.Log, plugin.vppChan,
measure.GetTimeLog(vpe.SwInterfaceSetL2Xconnect{}, plugin.Stopwatch))
if err != nil {
plugin.Log.WithField("Error", err).Error("Failed to create l2xConnect")
return err
}
return nil
}
func (plugin *XConnectConfigurator) deleteL2XConnectPair(rxIf, txIf string) error {
plugin.Log.Infof("Deleting L2 xConnect pair %v %v", rxIf, txIf)
// Find interface idx-es.
receiveInterfaceIndex, _, found := plugin.SwIfIndexes.LookupIdx(rxIf)
if !found {
plugin.Log.WithField("Interface", rxIf).Warn("Receive interface not found.")
return fmt.Errorf("receive interface '%s' not found", rxIf)
}
transmitInterfaceIndex, _, found := plugin.SwIfIndexes.LookupIdx(txIf)
if !found {
plugin.Log.WithField("Interface", txIf).Warn("Transmit interface not found.")
return fmt.Errorf("transmit interface '%s' not found", txIf)
}
err := vppcalls.VppUnsetL2XConnect(receiveInterfaceIndex, transmitInterfaceIndex, plugin.Log, plugin.vppChan,
measure.GetTimeLog(vpe.SwInterfaceSetL2Xconnect{}, plugin.Stopwatch))
if err != nil {
plugin.Log.WithField("Error", err).Error("Failed to remove l2xConnect")
return err
}
// Unregister.
plugin.XcIndexes.UnregisterName(rxIf)
plugin.Log.WithFields(logging.Fields{"RecIface": rxIf, "Idex": receiveInterfaceIndex}).Debug("XConnect pair unregistered.")
return nil
}