forked from ligato/vpp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ifplugin.go
379 lines (324 loc) · 12.8 KB
/
ifplugin.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// Copyright (c) 2018 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 descriptor-adapter --descriptor-name Interface --value-type *vpp_interfaces.Interface --meta-type *ifaceidx.IfaceMetadata --import "ifaceidx" --import "github.com/ligato/vpp-agent/api/models/vpp/interfaces" --output-dir "descriptor"
//go:generate descriptor-adapter --descriptor-name Unnumbered --value-type *vpp_interfaces.Interface_Unnumbered --import "github.com/ligato/vpp-agent/api/models/vpp/interfaces" --output-dir "descriptor"
//go:generate descriptor-adapter --descriptor-name RxMode --value-type *vpp_interfaces.Interface --import "github.com/ligato/vpp-agent/api/models/vpp/interfaces" --output-dir "descriptor"
//go:generate descriptor-adapter --descriptor-name RxPlacement --value-type *vpp_interfaces.Interface_RxPlacement --import "github.com/ligato/vpp-agent/api/models/vpp/interfaces" --output-dir "descriptor"
//go:generate descriptor-adapter --descriptor-name BondedInterface --value-type *vpp_interfaces.BondLink_BondedInterface --import "github.com/ligato/vpp-agent/api/models/vpp/interfaces" --output-dir "descriptor"
//go:generate descriptor-adapter --descriptor-name Span --value-type *vpp_interfaces.Span --import "github.com/ligato/vpp-agent/api/models/vpp/interfaces" --output-dir "descriptor"
package ifplugin
import (
"context"
"sync"
"time"
"github.com/ligato/cn-infra/servicelabel"
govppapi "git.fd.io/govpp.git/api"
"github.com/ligato/cn-infra/datasync"
"github.com/ligato/cn-infra/health/statuscheck"
"github.com/ligato/cn-infra/idxmap"
"github.com/ligato/cn-infra/infra"
"github.com/ligato/cn-infra/utils/safeclose"
"github.com/pkg/errors"
"github.com/ligato/vpp-agent/api/models/vpp"
interfaces "github.com/ligato/vpp-agent/api/models/vpp/interfaces"
"github.com/ligato/vpp-agent/plugins/govppmux"
kvs "github.com/ligato/vpp-agent/plugins/kvscheduler/api"
linux_ifcalls "github.com/ligato/vpp-agent/plugins/linux/ifplugin/linuxcalls"
"github.com/ligato/vpp-agent/plugins/linux/nsplugin"
"github.com/ligato/vpp-agent/plugins/netalloc"
"github.com/ligato/vpp-agent/plugins/vpp/ifplugin/descriptor"
"github.com/ligato/vpp-agent/plugins/vpp/ifplugin/ifaceidx"
"github.com/ligato/vpp-agent/plugins/vpp/ifplugin/vppcalls"
_ "github.com/ligato/vpp-agent/plugins/vpp/ifplugin/vppcalls/vpp1904"
_ "github.com/ligato/vpp-agent/plugins/vpp/ifplugin/vppcalls/vpp1908"
_ "github.com/ligato/vpp-agent/plugins/vpp/ifplugin/vppcalls/vpp2001_324"
_ "github.com/ligato/vpp-agent/plugins/vpp/ifplugin/vppcalls/vpp2001_379"
)
// Default Go routine count used while retrieving linux configuration
const goRoutineCount = 10
// IfPlugin configures VPP interfaces using GoVPP.
type IfPlugin struct {
Deps
// GoVPP
vppCh govppapi.Channel
// handlers
ifHandler vppcalls.InterfaceVppAPI
linuxIfHandler linux_ifcalls.NetlinkAPIRead
// index maps
intfIndex ifaceidx.IfaceMetadataIndex
dhcpIndex idxmap.NamedMapping
// descriptors
linkStateDescriptor *descriptor.LinkStateDescriptor
dhcpDescriptor *descriptor.DHCPDescriptor
spanDescriptor *descriptor.SpanDescriptor
// from config file
defaultMtu uint32
// state data
publishStats bool
publishLock sync.Mutex
statusCheckReg bool
watchStatusReg datasync.WatchRegistration
resyncStatusChan chan datasync.ResyncEvent
ifStateChan chan *interfaces.InterfaceNotification
ifStateUpdater *InterfaceStateUpdater
// go routine management
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
}
// Deps lists dependencies of the interface plugin.
type Deps struct {
infra.PluginDeps
KVScheduler kvs.KVScheduler
GoVppmux govppmux.StatsAPI
ServiceLabel servicelabel.ReaderAPI
AddrAlloc netalloc.AddressAllocator
/* LinuxIfPlugin and NsPlugin deps are optional,
but they are required if AFPacket or TAP+TAP_TO_VPP interfaces are used. */
LinuxIfPlugin descriptor.LinuxPluginAPI
NsPlugin nsplugin.API
// state publishing
StatusCheck statuscheck.PluginStatusWriter
PublishErrors datasync.KeyProtoValWriter // TODO: to be used with a generic plugin for publishing errors (not just interfaces and BDs)
Watcher datasync.KeyValProtoWatcher /* for resync of interface state data (PublishStatistics) */
NotifyStates datasync.KeyProtoValWriter /* e.g. Kafka (up/down events only)*/
PublishStatistics datasync.KeyProtoValWriter /* e.g. ETCD (with resync) */
DataSyncs map[string]datasync.KeyProtoValWriter /* available DBs for PublishStatistics */
PushNotification func(notification *vpp.Notification)
}
// Init loads configuration file and registers interface-related descriptors.
func (p *IfPlugin) Init() (err error) {
// Create plugin context, save cancel function into the plugin handle.
p.ctx, p.cancel = context.WithCancel(context.Background())
// Read config file and set all related fields
if err := p.fromConfigFile(); err != nil {
return err
}
// Fills nil dependencies with default values
p.publishStats = p.PublishStatistics != nil || p.NotifyStates != nil
p.fixNilPointers()
// VPP channel
if p.vppCh, err = p.GoVppmux.NewAPIChannel(); err != nil {
return errors.Errorf("failed to create GoVPP API channel: %v", err)
}
// init handlers
p.ifHandler = vppcalls.CompatibleInterfaceVppHandler(p.vppCh, p.Log)
if p.LinuxIfPlugin != nil {
p.linuxIfHandler = linux_ifcalls.NewNetLinkHandler(p.NsPlugin, p.LinuxIfPlugin.GetInterfaceIndex(),
p.ServiceLabel.GetAgentPrefix(), goRoutineCount, p.Log)
}
// init & register descriptors
// -> base interface descriptor
ifaceDescriptor, ifaceDescrCtx := descriptor.NewInterfaceDescriptor(p.ifHandler,
p.AddrAlloc, p.defaultMtu, p.linuxIfHandler, p.LinuxIfPlugin, p.NsPlugin, p.Log)
err = p.KVScheduler.RegisterKVDescriptor(ifaceDescriptor)
if err != nil {
return err
}
var withIndex bool
metadataMap := p.KVScheduler.GetMetadataMap(ifaceDescriptor.Name)
p.intfIndex, withIndex = metadataMap.(ifaceidx.IfaceMetadataIndex)
if !withIndex {
return errors.New("missing index with interface metadata")
}
ifaceDescrCtx.SetInterfaceIndex(p.intfIndex)
// -> descriptors for derived values / notifications
var (
linkStateDescriptor *kvs.KVDescriptor
dhcpDescriptor *kvs.KVDescriptor
)
dhcpDescriptor, p.dhcpDescriptor = descriptor.NewDHCPDescriptor(p.KVScheduler,
p.ifHandler, p.intfIndex, p.Log)
linkStateDescriptor, p.linkStateDescriptor = descriptor.NewLinkStateDescriptor(
p.KVScheduler, p.ifHandler, p.intfIndex, p.Log)
rxModeDescriptor := descriptor.NewRxModeDescriptor(p.ifHandler, p.intfIndex, p.Log)
rxPlacementDescriptor := descriptor.NewRxPlacementDescriptor(p.ifHandler, p.intfIndex, p.Log)
addrDescriptor := descriptor.NewInterfaceAddressDescriptor(p.ifHandler, p.AddrAlloc, p.intfIndex, p.Log)
unIfDescriptor := descriptor.NewUnnumberedIfDescriptor(p.ifHandler, p.intfIndex, p.Log)
bondIfDescriptor, _ := descriptor.NewBondedInterfaceDescriptor(p.ifHandler, p.intfIndex, p.Log)
vrfDescriptor := descriptor.NewInterfaceVrfDescriptor(p.ifHandler, p.intfIndex, p.Log)
withAddrDescriptor := descriptor.NewInterfaceWithAddrDescriptor(p.Log)
spanDescriptor, spanDescriptorCtx := descriptor.NewSpanDescriptor(p.ifHandler, p.Log)
spanDescriptorCtx.SetInterfaceIndex(p.intfIndex)
err = p.KVScheduler.RegisterKVDescriptor(
dhcpDescriptor,
linkStateDescriptor,
rxModeDescriptor,
rxPlacementDescriptor,
addrDescriptor,
unIfDescriptor,
bondIfDescriptor,
vrfDescriptor,
withAddrDescriptor,
spanDescriptor,
)
if err != nil {
return err
}
// start watching for DHCP notifications
p.dhcpIndex = p.KVScheduler.GetMetadataMap(dhcpDescriptor.Name)
if p.dhcpIndex == nil {
return errors.New("missing index with DHCP metadata")
}
p.dhcpDescriptor.WatchDHCPNotifications(p.ctx)
// interface state data
if p.publishStats {
// subscribe & watch for resync of interface state data
p.resyncStatusChan = make(chan datasync.ResyncEvent)
p.wg.Add(1)
go p.watchStatusEvents()
}
// start interface state updater
p.ifStateChan = make(chan *interfaces.InterfaceNotification, 1000)
// start interface state publishing
p.wg.Add(1)
go p.publishIfStateEvents()
// Interface state updater
p.ifStateUpdater = &InterfaceStateUpdater{}
var n int
var t time.Time
ifNotifHandler := func(state *interfaces.InterfaceNotification) {
select {
case p.ifStateChan <- state:
// OK
default:
// full
if time.Since(t) > time.Second {
p.Log.Debugf("ifStateChan channel is full (%d)", n)
n = 0
} else {
n++
}
t = time.Now()
}
}
err = p.ifStateUpdater.Init(p.ctx, p.Log, p.KVScheduler, p.GoVppmux, p.intfIndex,
ifNotifHandler, p.publishStats)
if err != nil {
return err
}
if p.publishStats {
if err = p.subscribeWatcher(); err != nil {
return err
}
}
return nil
}
func (p *IfPlugin) subscribeWatcher() (err error) {
keyPrefixes := []string{interfaces.StatePrefix}
p.Log.Debugf("subscribe to %d status prefixes: %v", len(keyPrefixes), keyPrefixes)
p.watchStatusReg, err = p.Watcher.Watch("vpp-if-state",
nil, p.resyncStatusChan, keyPrefixes...)
if err != nil {
return err
}
return nil
}
// AfterInit delegates the call to ifStateUpdater.
func (p *IfPlugin) AfterInit() error {
err := p.ifStateUpdater.AfterInit()
if err != nil {
return err
}
if p.StatusCheck != nil {
// Register the plugin to status check. Periodical probe is not needed,
// data change will be reported when changed
p.StatusCheck.Register(p.PluginName, nil)
// Notify that status check for the plugins was registered. It will
// prevent status report errors in case resync is executed before AfterInit.
p.statusCheckReg = true
}
return nil
}
// Close stops all go routines.
func (p *IfPlugin) Close() error {
// stop publishing of state data
p.cancel()
p.wg.Wait()
// close all resources
return safeclose.Close(
// DHCP descriptor (DHCP notification watcher)
p.dhcpDescriptor,
// state updater
p.ifStateUpdater,
// registrations
p.watchStatusReg)
}
// GetInterfaceIndex gives read-only access to map with metadata of all configured
// VPP interfaces.
func (p *IfPlugin) GetInterfaceIndex() ifaceidx.IfaceMetadataIndex {
return p.intfIndex
}
// GetDHCPIndex gives read-only access to (untyped) map with DHCP leases.
// Cast metadata to "github.com/ligato/vpp-agent/api/models/vpp/interfaces".DHCPLease
func (p *IfPlugin) GetDHCPIndex() idxmap.NamedMapping {
return p.dhcpIndex
}
// SetNotifyService sets notification callback for processing VPP notifications.
func (p *IfPlugin) SetNotifyService(notify func(notification *vpp.Notification)) {
p.PushNotification = notify
}
// fromConfigFile loads plugin attributes from the configuration file.
func (p *IfPlugin) fromConfigFile() error {
config, err := p.loadConfig()
if err != nil {
p.Log.Errorf("Error reading %v config file: %v", p.PluginName, err)
return err
}
if config != nil {
publishers := datasync.KVProtoWriters{}
for _, pub := range config.StatusPublishers {
db, found := p.Deps.DataSyncs[pub]
if !found {
p.Log.Warnf("Unknown status publisher %q from config", pub)
continue
}
publishers = append(publishers, db)
p.Log.Infof("Added status publisher %q from config", pub)
}
p.Deps.PublishStatistics = publishers
if config.MTU != 0 {
p.defaultMtu = config.MTU
p.Log.Infof("Default MTU set to %v", p.defaultMtu)
}
}
return nil
}
var (
// noopWriter (no operation writer) helps avoiding NIL pointer based segmentation fault.
// It is used as default if some dependency was not injected.
noopWriter = datasync.KVProtoWriters{}
// noopWatcher (no operation watcher) helps avoiding NIL pointer based segmentation fault.
// It is used as default if some dependency was not injected.
noopWatcher = datasync.KVProtoWatchers{}
)
// fixNilPointers sets noopWriter & nooWatcher for nil dependencies.
func (p *IfPlugin) fixNilPointers() {
if p.Deps.PublishErrors == nil {
p.Deps.PublishErrors = noopWriter
p.Log.Debug("setting default noop writer for PublishErrors dependency")
}
if p.Deps.PublishStatistics == nil {
p.Deps.PublishStatistics = noopWriter
p.Log.Debug("setting default noop writer for PublishStatistics dependency")
}
if p.Deps.NotifyStates == nil {
p.Deps.NotifyStates = noopWriter
p.Log.Debug("setting default noop writer for NotifyStatistics dependency")
}
if p.Deps.Watcher == nil {
p.Deps.Watcher = noopWatcher
p.Log.Debug("setting default noop watcher for Watcher dependency")
}
}