1 // Copyright (c) 2017 Cisco and/or its affiliates.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at:
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
25 logger "github.com/sirupsen/logrus"
27 "git.fd.io/govpp.git/adapter"
28 "git.fd.io/govpp.git/api"
29 "git.fd.io/govpp.git/codec"
33 DefaultReconnectInterval = time.Second / 2 // default interval between reconnect attempts
34 DefaultMaxReconnectAttempts = 3 // default maximum number of reconnect attempts
38 RequestChanBufSize = 100 // default size of the request channel buffer
39 ReplyChanBufSize = 100 // default size of the reply channel buffer
40 NotificationChanBufSize = 100 // default size of the notification channel buffer
44 HealthCheckProbeInterval = time.Second // default health check probe interval
45 HealthCheckReplyTimeout = time.Millisecond * 100 // timeout for reply to a health check probe
46 HealthCheckThreshold = 1 // number of failed health checks until the error is reported
47 DefaultReplyTimeout = time.Second // default timeout for replies from VPP
50 // ConnectionState represents the current state of the connection to VPP.
51 type ConnectionState int
54 // Connected represents state in which the connection has been successfully established.
55 Connected ConnectionState = iota
57 // Disconnected represents state in which the connection has been dropped.
60 // Failed represents state in which the reconnecting failed after exceeding maximum number of attempts.
64 func (s ConnectionState) String() string {
73 return fmt.Sprintf("UnknownState(%d)", s)
77 // ConnectionEvent is a notification about change in the VPP connection state.
78 type ConnectionEvent struct {
79 // Timestamp holds the time when the event has been created.
82 // State holds the new state of the connection at the time when the event has been created.
85 // Error holds error if any encountered.
89 // Connection represents a shared memory connection to VPP via vppAdapter.
90 type Connection struct {
91 vppClient adapter.VppAPI // VPP binary API client
93 maxAttempts int // interval for reconnect attempts
94 recInterval time.Duration // maximum number of reconnect attempts
96 vppConnected uint32 // non-zero if the adapter is connected to VPP
98 codec *codec.MsgCodec // message codec
99 msgIDs map[string]uint16 // map of message IDs indexed by message name + CRC
100 msgMap map[uint16]api.Message // map of messages indexed by message ID
102 maxChannelID uint32 // maximum used channel ID (the real limit is 2^15, 32-bit is used for atomic operations)
103 channelsLock sync.RWMutex // lock for the channels map
104 channels map[uint16]*Channel // map of all API channels indexed by the channel ID
106 subscriptionsLock sync.RWMutex // lock for the subscriptions map
107 subscriptions map[uint16][]*subscriptionCtx // map od all notification subscriptions indexed by message ID
109 pingReqID uint16 // ID if the ControlPing message
110 pingReplyID uint16 // ID of the ControlPingReply message
112 lastReplyLock sync.Mutex // lock for the last reply
113 lastReply time.Time // time of the last received reply from VPP
116 func newConnection(binapi adapter.VppAPI, attempts int, interval time.Duration) *Connection {
118 attempts = DefaultMaxReconnectAttempts
121 interval = DefaultReconnectInterval
126 maxAttempts: attempts,
127 recInterval: interval,
128 codec: &codec.MsgCodec{},
129 msgIDs: make(map[string]uint16),
130 msgMap: make(map[uint16]api.Message),
131 channels: make(map[uint16]*Channel),
132 subscriptions: make(map[uint16][]*subscriptionCtx),
134 binapi.SetMsgCallback(c.msgCallback)
138 // Connect connects to VPP API using specified adapter and returns a connection handle.
139 // This call blocks until it is either connected, or an error occurs.
140 // Only one connection attempt will be performed.
141 func Connect(binapi adapter.VppAPI) (*Connection, error) {
142 // create new connection handle
143 c := newConnection(binapi, DefaultMaxReconnectAttempts, DefaultReconnectInterval)
145 // blocking attempt to connect to VPP
146 if err := c.connectVPP(); err != nil {
153 // AsyncConnect asynchronously connects to VPP using specified VPP adapter and returns the connection handle
154 // and ConnectionState channel. This call does not block until connection is established, it
155 // returns immediately. The caller is supposed to watch the returned ConnectionState channel for
156 // Connected/Disconnected events. In case of disconnect, the library will asynchronously try to reconnect.
157 func AsyncConnect(binapi adapter.VppAPI, attempts int, interval time.Duration) (*Connection, chan ConnectionEvent, error) {
158 // create new connection handle
159 c := newConnection(binapi, attempts, interval)
161 // asynchronously attempt to connect to VPP
162 connChan := make(chan ConnectionEvent, NotificationChanBufSize)
163 go c.connectLoop(connChan)
165 return c, connChan, nil
168 // connectVPP performs blocking attempt to connect to VPP.
169 func (c *Connection) connectVPP() error {
170 log.Debug("Connecting to VPP..")
173 if err := c.vppClient.Connect(); err != nil {
176 log.Debugf("Connected to VPP")
178 if err := c.retrieveMessageIDs(); err != nil {
179 if err := c.vppClient.Disconnect(); err != nil {
180 log.Debugf("disconnecting vpp client failed: %v", err)
182 return fmt.Errorf("VPP is incompatible: %v", err)
185 // store connected state
186 atomic.StoreUint32(&c.vppConnected, 1)
191 // Disconnect disconnects from VPP API and releases all connection-related resources.
192 func (c *Connection) Disconnect() {
196 if c.vppClient != nil {
201 // disconnectVPP disconnects from VPP in case it is connected.
202 func (c *Connection) disconnectVPP() {
203 if atomic.CompareAndSwapUint32(&c.vppConnected, 1, 0) {
204 log.Debug("Disconnecting from VPP..")
206 if err := c.vppClient.Disconnect(); err != nil {
207 log.Debugf("Disconnect from VPP failed: %v", err)
209 log.Debug("Disconnected from VPP")
213 func (c *Connection) NewAPIChannel() (api.Channel, error) {
214 return c.newAPIChannel(RequestChanBufSize, ReplyChanBufSize)
217 func (c *Connection) NewAPIChannelBuffered(reqChanBufSize, replyChanBufSize int) (api.Channel, error) {
218 return c.newAPIChannel(reqChanBufSize, replyChanBufSize)
221 // NewAPIChannelBuffered returns a new API channel for communication with VPP via govpp core.
222 // It allows to specify custom buffer sizes for the request and reply Go channels.
223 func (c *Connection) newAPIChannel(reqChanBufSize, replyChanBufSize int) (*Channel, error) {
225 return nil, errors.New("nil connection passed in")
228 // create new channel
229 chID := uint16(atomic.AddUint32(&c.maxChannelID, 1) & 0x7fff)
230 channel := newChannel(chID, c, c.codec, c, reqChanBufSize, replyChanBufSize)
232 // store API channel within the client
233 c.channelsLock.Lock()
234 c.channels[chID] = channel
235 c.channelsLock.Unlock()
237 // start watching on the request channel
238 go c.watchRequests(channel)
243 // releaseAPIChannel releases API channel that needs to be closed.
244 func (c *Connection) releaseAPIChannel(ch *Channel) {
245 log.WithFields(logger.Fields{
247 }).Debug("API channel released")
249 // delete the channel from channels map
250 c.channelsLock.Lock()
251 delete(c.channels, ch.id)
252 c.channelsLock.Unlock()
255 // connectLoop attempts to connect to VPP until it succeeds.
256 // Then it continues with healthCheckLoop.
257 func (c *Connection) connectLoop(connChan chan ConnectionEvent) {
258 var reconnectAttempts int
260 // loop until connected
262 if err := c.vppClient.WaitReady(); err != nil {
263 log.Debugf("wait ready failed: %v", err)
265 if err := c.connectVPP(); err == nil {
266 // signal connected event
267 connChan <- ConnectionEvent{Timestamp: time.Now(), State: Connected}
269 } else if reconnectAttempts < c.maxAttempts {
271 log.Warnf("connecting failed (attempt %d/%d): %v", reconnectAttempts, c.maxAttempts, err)
272 time.Sleep(c.recInterval)
274 connChan <- ConnectionEvent{Timestamp: time.Now(), State: Failed, Error: err}
279 // we are now connected, continue with health check loop
280 c.healthCheckLoop(connChan)
283 // healthCheckLoop checks whether connection to VPP is alive. In case of disconnect,
284 // it continues with connectLoop and tries to reconnect.
285 func (c *Connection) healthCheckLoop(connChan chan ConnectionEvent) {
286 // create a separate API channel for health check probes
287 ch, err := c.newAPIChannel(1, 1)
289 log.Error("Failed to create health check API channel, health check will be disabled:", err)
294 sinceLastReply time.Duration
298 // send health check probes until an error or timeout occurs
300 // sleep until next health check probe period
301 time.Sleep(HealthCheckProbeInterval)
303 if atomic.LoadUint32(&c.vppConnected) == 0 {
304 // Disconnect has been called in the meantime, return the healthcheck - reconnect loop
305 log.Debug("Disconnected on request, exiting health check loop.")
309 // try draining probe replies from previous request before sending next one
312 log.Debug("drained old probe reply from reply channel")
316 // send the control ping request
317 ch.reqChan <- &vppRequest{msg: msgControlPing}
320 // expect response within timeout period
322 case vppReply := <-ch.replyChan:
325 case <-time.After(HealthCheckReplyTimeout):
326 err = ErrProbeTimeout
328 // check if time since last reply from any other
329 // channel is less than health check reply timeout
330 c.lastReplyLock.Lock()
331 sinceLastReply = time.Since(c.lastReply)
332 c.lastReplyLock.Unlock()
334 if sinceLastReply < HealthCheckReplyTimeout {
335 log.Warnf("VPP health check probe timing out, but some request on other channel was received %v ago, continue waiting!", sinceLastReply)
342 if err == ErrProbeTimeout {
344 log.Warnf("VPP health check probe timed out after %v (%d. timeout)", HealthCheckReplyTimeout, failedChecks)
345 if failedChecks > HealthCheckThreshold {
346 // in case of exceeded failed check treshold, assume VPP disconnected
347 log.Errorf("VPP health check exceeded treshold for timeouts (>%d), assuming disconnect", HealthCheckThreshold)
348 connChan <- ConnectionEvent{Timestamp: time.Now(), State: Disconnected}
351 } else if err != nil {
352 // in case of error, assume VPP disconnected
353 log.Errorf("VPP health check probe failed: %v", err)
354 connChan <- ConnectionEvent{Timestamp: time.Now(), State: Disconnected, Error: err}
356 } else if failedChecks > 0 {
357 // in case of success after failed checks, clear failed check counter
359 log.Infof("VPP health check probe OK")
367 // we are now disconnected, start connect loop
368 c.connectLoop(connChan)
371 func getMsgNameWithCrc(x api.Message) string {
372 return x.GetMessageName() + "_" + x.GetCrcString()
375 func getMsgFactory(msg api.Message) func() api.Message {
376 return func() api.Message {
377 return reflect.New(reflect.TypeOf(msg).Elem()).Interface().(api.Message)
381 // GetMessageID returns message identifier of given API message.
382 func (c *Connection) GetMessageID(msg api.Message) (uint16, error) {
384 return 0, errors.New("nil connection passed in")
387 if msgID, ok := c.msgIDs[getMsgNameWithCrc(msg)]; ok {
391 msgID, err := c.vppClient.GetMsgID(msg.GetMessageName(), msg.GetCrcString())
396 c.msgIDs[getMsgNameWithCrc(msg)] = msgID
397 c.msgMap[msgID] = msg
402 // LookupByID looks up message name and crc by ID.
403 func (c *Connection) LookupByID(msgID uint16) (api.Message, error) {
405 return nil, errors.New("nil connection passed in")
408 if msg, ok := c.msgMap[msgID]; ok {
412 return nil, fmt.Errorf("unknown message ID: %d", msgID)
415 // retrieveMessageIDs retrieves IDs for all registered messages and stores them in map
416 func (c *Connection) retrieveMessageIDs() (err error) {
419 msgs := api.GetRegisteredMessages()
422 for name, msg := range msgs {
423 msgID, err := c.GetMessageID(msg)
425 log.Debugf("retrieving msgID for %s failed: %v", name, err)
430 if c.pingReqID == 0 && msg.GetMessageName() == msgControlPing.GetMessageName() {
432 msgControlPing = reflect.New(reflect.TypeOf(msg).Elem()).Interface().(api.Message)
433 } else if c.pingReplyID == 0 && msg.GetMessageName() == msgControlPingReply.GetMessageName() {
434 c.pingReplyID = msgID
435 msgControlPingReply = reflect.New(reflect.TypeOf(msg).Elem()).Interface().(api.Message)
439 log.Debugf("message %q (%s) has ID: %d", name, getMsgNameWithCrc(msg), msgID)
442 log.Debugf("retrieved %d/%d msgIDs (took %s)", n, len(msgs), time.Since(t))