forked from ligato/vpp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin_impl_govppmux.go
343 lines (296 loc) · 10.1 KB
/
plugin_impl_govppmux.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
// 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 govppmux
import (
"context"
"sync"
"time"
"git.fd.io/govpp.git/adapter"
govppapi "git.fd.io/govpp.git/api"
govpp "git.fd.io/govpp.git/core"
"github.com/ligato/cn-infra/datasync/resync"
"github.com/ligato/cn-infra/health/statuscheck"
"github.com/ligato/cn-infra/infra"
"github.com/ligato/cn-infra/logging"
"github.com/ligato/cn-infra/logging/logrus"
"github.com/ligato/cn-infra/logging/measure"
"github.com/ligato/cn-infra/logging/measure/model/apitrace"
"github.com/pkg/errors"
"github.com/ligato/vpp-agent/plugins/govppmux/vppcalls"
)
// Default path to socket for VPP stats
const defaultStatsSocket = "/run/vpp/stats.sock"
// Plugin implements the govppmux plugin interface.
type Plugin struct {
Deps
vppConn *govpp.Connection
vppAdapter adapter.VppAPI
statsAdapter adapter.StatsAPI
vppConChan chan govpp.ConnectionEvent
lastConnErr error
config *Config
// Cancel can be used to cancel all goroutines and their jobs inside of the plugin.
cancel context.CancelFunc
// Plugin-wide tracer instance used to trace and time-measure binary API calls. Can be nil if not set.
tracer measure.Tracer
// Wait group allows to wait until all goroutines of the plugin have finished.
wg sync.WaitGroup
}
// Deps groups injected dependencies of plugin
// so that they do not mix with other plugin fields.
type Deps struct {
infra.PluginDeps
StatusCheck statuscheck.PluginStatusWriter
Resync *resync.Plugin
}
// Config groups the configurable parameter of GoVpp.
type Config struct {
TraceEnabled bool `json:"trace-enabled"`
ReconnectResync bool `json:"resync-after-reconnect"`
HealthCheckProbeInterval time.Duration `json:"health-check-probe-interval"`
HealthCheckReplyTimeout time.Duration `json:"health-check-reply-timeout"`
HealthCheckThreshold int `json:"health-check-threshold"`
ReplyTimeout time.Duration `json:"reply-timeout"`
// The prefix prepended to the name used for shared memory (SHM) segments. If not set,
// shared memory segments are created directly in the SHM directory /dev/shm.
ShmPrefix string `json:"shm-prefix"`
StatsSocketName string `json:"stats-socket-name"`
// How many times can be request resent in case vpp is suddenly disconnected.
RetryRequestCount int `json:"retry-request-count"`
// Time between request resend attempts. Default is 500ms.
RetryRequestTimeout time.Duration `json:"retry-request-timeout"`
}
func defaultConfig() *Config {
return &Config{
HealthCheckProbeInterval: time.Second,
HealthCheckReplyTimeout: 250 * time.Millisecond,
HealthCheckThreshold: 1,
ReplyTimeout: time.Second,
RetryRequestTimeout: 500 * time.Millisecond,
}
}
func (p *Plugin) loadConfig() (*Config, error) {
cfg := defaultConfig()
found, err := p.Cfg.LoadValue(cfg)
if err != nil {
return nil, err
} else if found {
p.Log.Debugf("config loaded from file %q", p.Cfg.GetConfigName())
} else {
p.Log.Debugf("config file %q not found, using default config", p.Cfg.GetConfigName())
}
return cfg, nil
}
// Init is the entry point called by Agent Core. A single binary-API connection to VPP is established.
func (p *Plugin) Init() error {
var err error
govppLogger := p.Deps.Log.NewLogger("govpp")
if govppLogger, ok := govppLogger.(*logrus.Logger); ok {
govppLogger.SetLevel(logging.InfoLevel)
govpp.SetLogger(govppLogger.StandardLogger())
}
if p.config, err = p.loadConfig(); err != nil {
return err
}
p.Log.Debugf("config: %+v", p.config)
govpp.HealthCheckProbeInterval = p.config.HealthCheckProbeInterval
govpp.HealthCheckReplyTimeout = p.config.HealthCheckReplyTimeout
govpp.HealthCheckThreshold = p.config.HealthCheckThreshold
govpp.DefaultReplyTimeout = p.config.ReplyTimeout
if p.config.TraceEnabled {
p.tracer = measure.NewTracer("govpp-mux")
p.Log.Info("VPP API trace enabled")
}
if p.vppAdapter == nil {
p.vppAdapter = NewVppAdapter(p.config.ShmPrefix)
} else {
// this is used for testing purposes
p.Log.Info("Reusing existing vppAdapter")
}
startTime := time.Now()
p.vppConn, p.vppConChan, err = govpp.AsyncConnect(p.vppAdapter)
if err != nil {
return err
}
// TODO: Async connect & automatic reconnect support is not yet implemented in the agent,
// so synchronously wait until connected to VPP.
status := <-p.vppConChan
if status.State != govpp.Connected {
return errors.New("unable to connect to VPP")
}
vppConnectTime := time.Since(startTime)
info, err := p.retrieveVpeInfo()
if err != nil {
p.Log.Errorf("retrieving vpe info failed: %v", err)
return err
}
p.Log.Infof("Connected to VPP [PID:%d] (took %s)",
info.PID, vppConnectTime.Truncate(time.Millisecond))
p.retrieveVersion()
// Register providing status reports (push mode)
p.StatusCheck.Register(p.PluginName, nil)
p.StatusCheck.ReportStateChange(p.PluginName, statuscheck.OK, nil)
var ctx context.Context
ctx, p.cancel = context.WithCancel(context.Background())
go p.handleVPPConnectionEvents(ctx)
// Connect to VPP status socket
if p.config.StatsSocketName != "" {
p.statsAdapter = NewStatsAdapter(p.config.StatsSocketName)
} else {
p.statsAdapter = NewStatsAdapter(defaultStatsSocket)
}
if err := p.statsAdapter.Connect(); err != nil {
p.Log.Warnf("Unable to connect to VPP statistics socket, %v", err)
p.statsAdapter = nil
}
return nil
}
// Close cleans up the resources allocated by the govppmux plugin.
func (p *Plugin) Close() error {
p.cancel()
p.wg.Wait()
defer func() {
if p.vppConn != nil {
p.vppConn.Disconnect()
}
if p.statsAdapter != nil {
if err := p.statsAdapter.Disconnect(); err != nil {
p.Log.Errorf("VPP statistics socket adapter disconnect error: %v", err)
}
}
}()
return nil
}
// NewAPIChannel returns a new API channel for communication with VPP via govpp core.
// It uses default buffer sizes for the request and reply Go channels.
//
// Example of binary API call from some plugin using GOVPP:
// ch, _ := govpp_mux.NewAPIChannel()
// ch.SendRequest(req).ReceiveReply
func (p *Plugin) NewAPIChannel() (govppapi.Channel, error) {
ch, err := p.vppConn.NewAPIChannel()
if err != nil {
return nil, err
}
retryCfg := retryConfig{
p.config.RetryRequestCount,
p.config.RetryRequestTimeout,
}
return &goVppChan{ch, retryCfg, p.tracer}, nil
}
// NewAPIChannelBuffered returns a new API channel for communication with VPP via govpp core.
// It allows to specify custom buffer sizes for the request and reply Go channels.
//
// Example of binary API call from some plugin using GOVPP:
// ch, _ := govpp_mux.NewAPIChannelBuffered(100, 100)
// ch.SendRequest(req).ReceiveReply
func (p *Plugin) NewAPIChannelBuffered(reqChanBufSize, replyChanBufSize int) (govppapi.Channel, error) {
ch, err := p.vppConn.NewAPIChannelBuffered(reqChanBufSize, replyChanBufSize)
if err != nil {
return nil, err
}
retryCfg := retryConfig{
p.config.RetryRequestCount,
p.config.RetryRequestTimeout,
}
return &goVppChan{ch, retryCfg, p.tracer}, nil
}
// GetTrace returns all trace entries measured so far
func (p *Plugin) GetTrace() *apitrace.Trace {
if !p.config.TraceEnabled {
p.Log.Warnf("VPP API trace is disabled")
return nil
}
return p.tracer.Get()
}
// ListStats returns all stats names
func (p *Plugin) ListStats(prefixes ...string) ([]string, error) {
if p.statsAdapter == nil {
return nil, nil
}
return p.statsAdapter.ListStats(prefixes...)
}
// DumpStats returns all stats with name, type and value
func (p *Plugin) DumpStats(prefixes ...string) ([]*adapter.StatEntry, error) {
if p.statsAdapter == nil {
return nil, nil
}
return p.statsAdapter.DumpStats(prefixes...)
}
// handleVPPConnectionEvents handles VPP connection events.
func (p *Plugin) handleVPPConnectionEvents(ctx context.Context) {
p.wg.Add(1)
defer p.wg.Done()
for {
select {
case status := <-p.vppConChan:
if status.State == govpp.Connected {
p.retrieveVpeInfo()
p.retrieveVersion()
if p.config.ReconnectResync && p.lastConnErr != nil {
p.Log.Info("Starting resync after VPP reconnect")
if p.Resync != nil {
p.Resync.DoResync()
p.lastConnErr = nil
} else {
p.Log.Warn("Expected resync after VPP reconnect could not start because of missing Resync plugin")
}
}
p.StatusCheck.ReportStateChange(p.PluginName, statuscheck.OK, nil)
} else {
p.lastConnErr = errors.New("VPP disconnected")
p.StatusCheck.ReportStateChange(p.PluginName, statuscheck.Error, p.lastConnErr)
}
case <-ctx.Done():
return
}
}
}
func (p *Plugin) retrieveVpeInfo() (*vppcalls.VpeInfo, error) {
vppAPIChan, err := p.vppConn.NewAPIChannel()
if err != nil {
p.Log.Error("getting new api channel failed:", err)
return nil, err
}
defer vppAPIChan.Close()
info, err := vppcalls.GetVpeInfo(vppAPIChan)
if err != nil {
p.Log.Warn("getting version info failed:", err)
return nil, err
}
p.Log.Debugf("connection info: %+v", info)
return info, nil
}
func (p *Plugin) retrieveVersion() {
vppAPIChan, err := p.vppConn.NewAPIChannel()
if err != nil {
p.Log.Error("getting new api channel failed:", err)
return
}
defer vppAPIChan.Close()
version, err := vppcalls.GetVersionInfo(vppAPIChan)
if err != nil {
p.Log.Warn("getting version info failed:", err)
return
}
p.Log.Debugf("version info: %+v", version)
p.Log.Infof("VPP version: %q (%v)", version.Version, version.BuildDate)
// Get VPP ACL plugin version
var aclVersion string
if aclVersion, err = vppcalls.GetACLPluginVersion(vppAPIChan); err != nil {
p.Log.Warn("getting acl version info failed:", err)
return
}
p.Log.Infof("VPP ACL plugin version: %q", aclVersion)
}