forked from ligato/vpp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface_state.go
490 lines (420 loc) · 15.4 KB
/
interface_state.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// 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 (
"bytes"
"context"
"net"
"os"
"sync"
"time"
govppapi "git.fd.io/govpp.git/api"
"github.com/go-errors/errors"
"github.com/ligato/cn-infra/logging"
"github.com/ligato/cn-infra/utils/safeclose"
"github.com/ligato/vpp-agent/plugins/govppmux"
"github.com/ligato/vpp-agent/plugins/vpp/binapi/interfaces"
"github.com/ligato/vpp-agent/plugins/vpp/binapi/stats"
"github.com/ligato/vpp-agent/plugins/vpp/ifplugin/ifaceidx"
intf "github.com/ligato/vpp-agent/plugins/vpp/model/interfaces"
)
// counterType is the basic counter type - contains only packet statistics.
type counterType int
// constants as defined in the vnet_interface_counter_type_t enum in 'vnet/interface.h'
const (
Drop counterType = 0
Punt = 1
IPv4 = 2
IPv6 = 3
RxNoBuf = 4
RxMiss = 5
RxError = 6
TxError = 7
MPLS = 8
)
// combinedCounterType is the extended counter type - contains both packet and byte statistics.
type combinedCounterType int
// constants as defined in the vnet_interface_counter_type_t enum in 'vnet/interface.h'
const (
Rx combinedCounterType = 0
Tx = 1
)
const (
megabit = 1000000 // One megabit in bytes
)
// InterfaceStateUpdater holds state data of all VPP interfaces.
type InterfaceStateUpdater struct {
log logging.Logger
swIfIndexes ifaceidx.SwIfIndex
publishIfState func(notification *intf.InterfaceNotification)
ifState map[uint32]*intf.InterfacesState_Interface // swIfIndex to state data map
access sync.Mutex // lock for the state data map
vppCh govppapi.Channel
vppNotifSubs govppapi.SubscriptionCtx
vppCountersSubs govppapi.SubscriptionCtx
vppCombinedCountersSubs govppapi.SubscriptionCtx
notifChan chan govppapi.Message
swIdxChan chan ifaceidx.SwIfIdxDto
cancel context.CancelFunc // cancel can be used to cancel all goroutines and their jobs inside of the plugin
wg sync.WaitGroup // wait group that allows to wait until all goroutines of the plugin have finished
}
// Init members (channels, maps...) and start go routines
func (c *InterfaceStateUpdater) Init(ctx context.Context, logger logging.PluginLogger, goVppMux govppmux.API,
swIfIndexes ifaceidx.SwIfIndex, notifChan chan govppapi.Message,
publishIfState func(notification *intf.InterfaceNotification)) (err error) {
// Logger
c.log = logger.NewLogger("if-state")
// Mappings
c.swIfIndexes = swIfIndexes
c.publishIfState = publishIfState
c.ifState = make(map[uint32]*intf.InterfacesState_Interface)
// VPP channel
c.vppCh, err = goVppMux.NewAPIChannel()
if err != nil {
return errors.Errorf("failed to create API channel: %v", err)
}
c.swIdxChan = make(chan ifaceidx.SwIfIdxDto, 100)
swIfIndexes.WatchNameToIdx("ifplugin_ifstate", c.swIdxChan)
c.notifChan = notifChan
// Create child context
var childCtx context.Context
childCtx, c.cancel = context.WithCancel(ctx)
// Watch for incoming notifications
go c.watchVPPNotifications(childCtx)
c.log.Info("Interface state updater initialized")
return nil
}
// AfterInit subscribes for watching VPP notifications on previously initialized channel
func (c *InterfaceStateUpdater) AfterInit() error {
err := c.subscribeVPPNotifications()
if err != nil {
return err
}
return nil
}
// subscribeVPPNotifications subscribes for interface state notifications from VPP.
func (c *InterfaceStateUpdater) subscribeVPPNotifications() error {
var err error
// subscribe for receiving SwInterfaceEvents notifications
if c.vppNotifSubs, err = c.vppCh.SubscribeNotification(c.notifChan, &interfaces.SwInterfaceEvent{}); err != nil {
return errors.Errorf("failed to subscribe VPP notification (sw_interface_event): %v", err)
}
// subscribe for receiving VnetInterfaceSimpleCounters notifications
if c.vppCountersSubs, err = c.vppCh.SubscribeNotification(c.notifChan, &stats.VnetInterfaceSimpleCounters{}); err != nil {
return errors.Errorf("failed to subscribe VPP notification (vnet_interface_simple_counters): %v", err)
}
// subscribe for receiving VnetInterfaceCombinedCounters notifications
if c.vppCombinedCountersSubs, err = c.vppCh.SubscribeNotification(c.notifChan, &stats.VnetInterfaceCombinedCounters{}); err != nil {
return errors.Errorf("failed to subscribe VPP notification (vnet_interface_combined_counters): %v", err)
}
wantIfEventsReply := &interfaces.WantInterfaceEventsReply{}
// enable interface state notifications from VPP
err = c.vppCh.SendRequest(&interfaces.WantInterfaceEvents{
PID: uint32(os.Getpid()),
EnableDisable: 1,
}).ReceiveReply(wantIfEventsReply)
if err != nil {
return errors.Errorf("failed to get interface events: %v", err)
}
if wantIfEventsReply.Retval != 0 {
return errors.Errorf("%s returned %d", wantIfEventsReply.GetMessageName(), wantIfEventsReply.Retval)
}
wantStatsReply := &stats.WantStatsReply{}
// enable interface counters notifications from VPP
err = c.vppCh.SendRequest(&stats.WantStats{
PID: uint32(os.Getpid()),
EnableDisable: 1,
}).ReceiveReply(wantStatsReply)
if err != nil {
return errors.Errorf("failed to get interface events: %v", err)
}
if wantStatsReply.Retval != 0 {
return errors.Errorf("%s returned %d", wantStatsReply.GetMessageName(), wantStatsReply.Retval)
}
return nil
}
// Close unsubscribes from interface state notifications from VPP & GOVPP channel
func (c *InterfaceStateUpdater) Close() error {
c.cancel()
c.wg.Wait()
if c.vppNotifSubs != nil {
if err := c.vppNotifSubs.Unsubscribe(); err != nil {
return c.LogError(errors.Errorf("failed to unsubscribe interface state notification on close: %v", err))
}
}
if c.vppCountersSubs != nil {
if err := c.vppCountersSubs.Unsubscribe(); err != nil {
return c.LogError(errors.Errorf("failed to unsubscribe interface state counters on close: %v", err))
}
}
if c.vppCombinedCountersSubs != nil {
if err := c.vppCombinedCountersSubs.Unsubscribe(); err != nil {
return c.LogError(errors.Errorf("failed to unsubscribe interface state combined counters on close: %v", err))
}
}
if err := safeclose.Close(c.vppCh); err != nil {
return c.LogError(errors.Errorf("failed to safe close interface state: %v", err))
}
return nil
}
// watchVPPNotifications watches for delivery of notifications from VPP.
func (c *InterfaceStateUpdater) watchVPPNotifications(ctx context.Context) {
c.wg.Add(1)
defer c.wg.Done()
if c.notifChan != nil {
c.log.Debug("Interface state VPP notification watcher started")
} else {
c.log.Warn("Interface state VPP notification does not start: the channel c nil")
return
}
for {
select {
case msg := <-c.notifChan:
switch notif := msg.(type) {
case *interfaces.SwInterfaceEvent:
c.processIfStateNotification(notif)
case *stats.VnetInterfaceSimpleCounters:
c.processIfCounterNotification(notif)
case *stats.VnetInterfaceCombinedCounters:
c.processIfCombinedCounterNotification(notif)
case *interfaces.SwInterfaceDetails:
c.updateIfStateDetails(notif)
default:
c.log.Debugf("Ignoring unknown VPP notification: %s, %v",
msg.GetMessageName(), msg)
}
case swIdxDto := <-c.swIdxChan:
if swIdxDto.Del {
c.setIfStateDeleted(swIdxDto.Idx, swIdxDto.Name)
}
swIdxDto.Done()
case <-ctx.Done():
// stop watching for notifications
c.log.Debug("Interface state VPP notification watcher stopped")
return
}
}
}
// processIfStateNotification process a VPP state notification.
func (c *InterfaceStateUpdater) processIfStateNotification(notif *interfaces.SwInterfaceEvent) {
// update and return if state data
ifState, found := c.updateIfStateFlags(notif)
if !found {
return
}
c.log.Debugf("Interface state notification for %s (Idx %d)", ifState.Name, ifState.IfIndex)
// store data in ETCD
c.publishIfState(&intf.InterfaceNotification{
Type: intf.InterfaceNotification_UPDOWN, State: ifState})
}
// getIfStateData returns interface state data structure for the specified interface index and interface name.
// NOTE: plugin.ifStateData needs to be locked when calling this function!
func (c *InterfaceStateUpdater) getIfStateData(swIfIndex uint32, ifName string) (*intf.InterfacesState_Interface, bool) {
ifState, ok := c.ifState[swIfIndex]
// check also if the provided logical name c the same as the one associated
// with swIfIndex, because swIfIndexes might be reused
if ok && ifState.Name == ifName {
return ifState, true
}
return nil, false
}
// getIfStateDataWLookup returns interface state data structure for the specified interface index (creates it if it does not exist).
// NOTE: plugin.ifStateData needs to be locked when calling this function!
func (c *InterfaceStateUpdater) getIfStateDataWLookup(ifIdx uint32) (
*intf.InterfacesState_Interface, bool) {
ifName, _, found := c.swIfIndexes.LookupName(ifIdx)
if !found {
c.log.Debugf("Interface state data structure lookup for %d interrupted, not registered yet", ifIdx)
return nil, found
}
ifState, found := c.getIfStateData(ifIdx, ifName)
if !found {
ifState = &intf.InterfacesState_Interface{
IfIndex: ifIdx,
Name: ifName,
Statistics: &intf.InterfacesState_Interface_Statistics{},
}
c.ifState[ifIdx] = ifState
found = true
}
return ifState, found
}
// updateIfStateFlags updates the interface state data in memory from provided VPP flags message and returns updated state data.
// NOTE: plugin.ifStateData needs to be locked when calling this function!
func (c *InterfaceStateUpdater) updateIfStateFlags(vppMsg *interfaces.SwInterfaceEvent) (
iface *intf.InterfacesState_Interface, found bool) {
ifState, found := c.getIfStateDataWLookup(vppMsg.SwIfIndex)
if !found {
return nil, false
}
ifState.LastChange = time.Now().Unix()
if vppMsg.Deleted == 1 {
ifState.AdminStatus = intf.InterfacesState_Interface_DELETED
ifState.OperStatus = intf.InterfacesState_Interface_DELETED
} else {
if vppMsg.AdminUpDown == 1 {
ifState.AdminStatus = intf.InterfacesState_Interface_UP
} else {
ifState.AdminStatus = intf.InterfacesState_Interface_DOWN
}
if vppMsg.LinkUpDown == 1 {
ifState.OperStatus = intf.InterfacesState_Interface_UP
} else {
ifState.OperStatus = intf.InterfacesState_Interface_DOWN
}
}
return ifState, true
}
// processIfCounterNotification processes a VPP (simple) counter message.
func (c *InterfaceStateUpdater) processIfCounterNotification(counter *stats.VnetInterfaceSimpleCounters) {
c.access.Lock()
defer c.access.Unlock()
for i := uint32(0); i < counter.Count; i++ {
swIfIndex := counter.FirstSwIfIndex + i
ifState, found := c.getIfStateDataWLookup(swIfIndex)
if !found {
continue
}
ifStats := ifState.Statistics
packets := counter.Data[i]
switch counterType(counter.VnetCounterType) {
case Drop:
ifStats.DropPackets = packets
case Punt:
ifStats.PuntPackets = packets
case IPv4:
ifStats.Ipv4Packets = packets
case IPv6:
ifStats.Ipv6Packets = packets
case RxNoBuf:
ifStats.InNobufPackets = packets
case RxMiss:
ifStats.InMissPackets = packets
case RxError:
ifStats.InErrorPackets = packets
case TxError:
ifStats.OutErrorPackets = packets
}
}
}
// processIfCombinedCounterNotification processes a VPP message with combined counters.
func (c *InterfaceStateUpdater) processIfCombinedCounterNotification(counter *stats.VnetInterfaceCombinedCounters) {
c.access.Lock()
defer c.access.Unlock()
if counter.VnetCounterType > Tx {
// TODO: process other types of combined counters (RX/TX for unicast/multicast/broadcast)
return
}
var save bool
for i := uint32(0); i < counter.Count; i++ {
swIfIndex := counter.FirstSwIfIndex + i
ifState, found := c.getIfStateDataWLookup(swIfIndex)
if !found {
continue
}
ifStats := ifState.Statistics
if combinedCounterType(counter.VnetCounterType) == Rx {
ifStats.InPackets = counter.Data[i].Packets
ifStats.InBytes = counter.Data[i].Bytes
} else if combinedCounterType(counter.VnetCounterType) == Tx {
ifStats.OutPackets = counter.Data[i].Packets
ifStats.OutBytes = counter.Data[i].Bytes
save = true
}
}
if save {
// store counters of all interfaces into ETCD
for _, counter := range c.ifState {
c.publishIfState(&intf.InterfaceNotification{
Type: intf.InterfaceNotification_UPDOWN, State: counter})
}
}
}
// updateIfStateDetails updates the interface state data in memory from provided VPP details message.
func (c *InterfaceStateUpdater) updateIfStateDetails(ifDetails *interfaces.SwInterfaceDetails) {
c.access.Lock()
defer c.access.Unlock()
ifState, found := c.getIfStateDataWLookup(ifDetails.SwIfIndex)
if !found {
return
}
ifState.InternalName = string(bytes.SplitN(ifDetails.InterfaceName, []byte{0x00}, 2)[0])
if ifDetails.AdminUpDown == 1 {
ifState.AdminStatus = intf.InterfacesState_Interface_UP
} else {
ifState.AdminStatus = intf.InterfacesState_Interface_DOWN
}
if ifDetails.LinkUpDown == 1 {
ifState.OperStatus = intf.InterfacesState_Interface_UP
} else {
ifState.OperStatus = intf.InterfacesState_Interface_DOWN
}
hwAddr := net.HardwareAddr(ifDetails.L2Address[:ifDetails.L2AddressLength])
ifState.PhysAddress = hwAddr.String()
ifState.Mtu = uint32(ifDetails.LinkMtu)
switch ifDetails.LinkSpeed {
case 1:
ifState.Speed = 10 * megabit // 10M
case 2:
ifState.Speed = 100 * megabit // 100M
case 4:
ifState.Speed = 1000 * megabit // 1G
case 8:
ifState.Speed = 10000 * megabit // 10G
case 16:
ifState.Speed = 40000 * megabit // 40G
case 32:
ifState.Speed = 100000 * megabit // 100G
default:
ifState.Speed = 0
}
switch ifDetails.LinkSpeed {
case 1:
ifState.Duplex = intf.InterfacesState_Interface_HALF
case 2:
ifState.Duplex = intf.InterfacesState_Interface_FULL
default:
ifState.Duplex = intf.InterfacesState_Interface_UNKNOWN_DUPLEX
}
c.publishIfState(&intf.InterfaceNotification{
Type: intf.InterfaceNotification_UNKNOWN, State: ifState})
}
// setIfStateDeleted marks the interface as deleted in the state data structure in memory.
func (c *InterfaceStateUpdater) setIfStateDeleted(swIfIndex uint32, ifName string) {
c.access.Lock()
defer c.access.Unlock()
ifState, found := c.getIfStateData(swIfIndex, ifName)
if !found {
return
}
ifState.AdminStatus = intf.InterfacesState_Interface_DELETED
ifState.OperStatus = intf.InterfacesState_Interface_DELETED
ifState.LastChange = time.Now().Unix()
// this can be post-processed by multiple plugins
c.publishIfState(&intf.InterfaceNotification{
Type: intf.InterfaceNotification_UNKNOWN, State: ifState})
}
// LogError prints error if not nil, including stack trace. The same value is also returned, so it can be easily propagated further
func (c *InterfaceStateUpdater) LogError(err error) error {
if err == nil {
return nil
}
switch err.(type) {
case *errors.Error:
c.log.WithField("logger", c.log).Errorf(string(err.Error() + "\n" + string(err.(*errors.Error).Stack())))
default:
c.log.Error(err)
}
return err
}