forked from ligato/vpp-agent
/
plugin_impl_govppmux.go
305 lines (253 loc) · 8.18 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
// 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"
"fmt"
"os"
"strings"
"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/measure"
"github.com/ligato/cn-infra/rpc/rest"
"github.com/pkg/errors"
"github.com/ligato/vpp-agent/plugins/govppmux/vppcalls"
_ "github.com/ligato/vpp-agent/plugins/govppmux/vppcalls/vpp1904"
_ "github.com/ligato/vpp-agent/plugins/govppmux/vppcalls/vpp1908"
_ "github.com/ligato/vpp-agent/plugins/govppmux/vppcalls/vpp2001_324"
_ "github.com/ligato/vpp-agent/plugins/govppmux/vppcalls/vpp2001_379"
)
var (
disabledSocketClient = os.Getenv("GOVPPMUX_NOSOCK") != ""
)
// Plugin implements the govppmux plugin interface.
type Plugin struct {
Deps
vppConn *govpp.Connection
vppAdapter adapter.VppAPI
statsConn govppapi.StatsProvider
statsAdapter adapter.StatsAPI
vppConChan chan govpp.ConnectionEvent
lastConnErr error
// mu protects fields below (vppInfo, lastEvent)
infoMu sync.Mutex
vppInfo VPPInfo
lastEvent govpp.ConnectionEvent
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
HTTPHandlers rest.HTTPHandlers
StatusCheck statuscheck.PluginStatusWriter
Resync *resync.Plugin
}
// Init is the entry point called by Agent Core. A single binary-API connection to VPP is established.
func (p *Plugin) Init() (err error) {
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")
}
// register REST API handlers
p.registerHandlers(p.HTTPHandlers)
if p.vppAdapter == nil {
var address string
useShm := disabledSocketClient || p.config.ConnectViaShm || p.config.ShmPrefix != ""
if useShm {
address = p.config.ShmPrefix
} else {
address = p.config.BinAPISocketPath
}
p.vppAdapter = NewVppAdapter(address, useShm)
} else {
// this is used for testing purposes
p.Log.Info("Reusing existing vppAdapter")
}
// TODO: Async connect & automatic reconnect support is not yet implemented in the agent,
// so synchronously wait until connected to VPP.
startTime := time.Now()
p.Log.Debugf("connecting to VPP..")
p.vppConn, p.vppConChan, err = govpp.AsyncConnect(p.vppAdapter, p.config.RetryConnectCount, p.config.RetryConnectTimeout)
if err != nil {
return err
}
// wait for connection event
for {
event := <-p.vppConChan
if event.State == govpp.Connected {
break
} else if event.State == govpp.Failed || event.State == govpp.Disconnected {
return errors.Errorf("unable to establish connection to VPP (%v)", event.Error)
} else {
p.Log.Debugf("VPP connection state: %+v", event)
}
}
connectDur := time.Since(startTime)
p.Log.Debugf("connection to VPP established (took %s)", connectDur.Round(time.Millisecond))
if err := p.updateVPPInfo(); err != nil {
return errors.WithMessage(err, "retrieving VPP info failed")
}
// Connect to VPP status socket
var statsSocket string
if p.config.StatsSocketPath != "" {
statsSocket = p.config.StatsSocketPath
} else {
statsSocket = adapter.DefaultStatsSocket
}
statsAdapter := NewStatsAdapter(statsSocket)
if statsAdapter == nil {
p.Log.Warnf("Unable to connect to the VPP statistics socket, nil stats adapter", err)
} else if p.statsConn, err = govpp.ConnectStats(statsAdapter); err != nil {
p.Log.Warnf("Unable to connect to the VPP statistics socket, %v", err)
p.statsAdapter = nil
}
return nil
}
// AfterInit reports status check.
func (p *Plugin) AfterInit() error {
// 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())
p.wg.Add(1)
go p.handleVPPConnectionEvents(ctx)
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
}
// VPPInfo returns information about VPP session.
func (p *Plugin) VPPInfo() (VPPInfo, error) {
p.infoMu.Lock()
defer p.infoMu.Unlock()
return p.vppInfo, nil
}
func (p *Plugin) updateVPPInfo() error {
if p.vppConn == nil {
return fmt.Errorf("VPP connection is nil")
}
vppAPIChan, err := p.vppConn.NewAPIChannel()
if err != nil {
return err
}
defer vppAPIChan.Close()
vpeHandler := vppcalls.CompatibleVpeHandler(vppAPIChan)
version, err := vpeHandler.RunCli("show version verbose")
if err != nil {
p.Log.Warnf("RunCli error: %v", err)
} else {
p.Log.Debugf("vpp# show version verbose\n%s", version)
}
cmdline, err := vpeHandler.RunCli("show version cmdline")
if err != nil {
p.Log.Warnf("RunCli error: %v", err)
} else {
out := strings.Replace(cmdline, "\n", "", -1)
p.Log.Debugf("vpp# show version cmdline:\n%s", out)
}
ver, err := vpeHandler.GetVersionInfo()
if err != nil {
return err
}
p.Log.Infof("VPP version: %v", ver.Version)
vpe, err := vpeHandler.GetVpeInfo()
if err != nil {
return err
}
p.Log.WithFields(logging.Fields{
"PID": vpe.PID,
"ClientID": vpe.ClientIdx,
}).Debugf("loaded %d VPP modules: %v", len(vpe.ModuleVersions), vpe.ModuleVersions)
p.infoMu.Lock()
p.vppInfo = VPPInfo{
Connected: true,
VersionInfo: *ver,
VpeInfo: *vpe,
}
p.infoMu.Unlock()
return nil
}
// handleVPPConnectionEvents handles VPP connection events.
func (p *Plugin) handleVPPConnectionEvents(ctx context.Context) {
defer p.wg.Done()
for {
select {
case event := <-p.vppConChan:
if event.State == govpp.Connected {
if err := p.updateVPPInfo(); err != nil {
p.Log.Errorf("updating VPP info failed: %v", err)
}
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 if event.State == govpp.Failed || event.State == govpp.Disconnected {
p.infoMu.Lock()
p.vppInfo.Connected = false
p.infoMu.Unlock()
p.lastConnErr = errors.Errorf("VPP connection lost (event: %+v)", event)
p.StatusCheck.ReportStateChange(p.PluginName, statuscheck.Error, p.lastConnErr)
} else {
p.Log.Debugf("VPP connection state: %+v", event)
}
p.infoMu.Lock()
p.lastEvent = event
p.infoMu.Unlock()
case <-ctx.Done():
return
}
}
}