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.
24 logger "github.com/Sirupsen/logrus"
26 "git.fd.io/govpp.git/adapter"
27 "git.fd.io/govpp.git/api"
28 "git.fd.io/govpp.git/core/bin_api/vpe"
32 requestChannelBufSize = 100 // default size of the request channel buffers
33 replyChannelBufSize = 100 // default size of the reply channel buffers
34 notificationChannelBufSize = 100 // default size of the notification channel buffers
38 healthCheckProbeInterval = time.Second * 1 // default health check probe interval
39 healthCheckReplyTimeout = time.Millisecond * 100 // timeout for reply to a health check probe
42 // ConnectionState holds the current state of the connection to VPP.
43 type ConnectionState int
46 // Connected connection state means that the connection to VPP has been successfully established.
47 Connected ConnectionState = iota
49 // Disconnected connection state means that the connection to VPP has been lost.
53 // ConnectionEvent is a notification about change in the VPP connection state.
54 type ConnectionEvent struct {
55 // Timestamp holds the time when the event has been generated.
58 // State holds the new state of the connection to VPP at the time when the event has been generated.
62 // Connection represents a shared memory connection to VPP via vppAdapter.
63 type Connection struct {
64 vpp adapter.VppAdapter // VPP adapter
65 connected uint32 // non-zero if the adapter is connected to VPP
66 codec *MsgCodec // message codec
68 msgIDs map[string]uint16 // map of message IDs indexed by message name + CRC
69 msgIDsLock sync.RWMutex // lock for the message IDs map
71 channels map[uint32]*api.Channel // map of all API channels indexed by the channel ID
72 channelsLock sync.RWMutex // lock for the channels map
74 notifSubscriptions map[uint16][]*api.NotifSubscription // map od all notification subscriptions indexed by message ID
75 notifSubscriptionsLock sync.RWMutex // lock for the subscriptions map
77 maxChannelID uint32 // maximum used client ID
78 pingReqID uint16 // ID if the ControlPing message
79 pingReplyID uint16 // ID of the ControlPingReply message
82 // channelMetadata contains core-local metadata of an API channel.
83 type channelMetadata struct {
84 id uint32 // channel ID
85 multipart uint32 // 1 if multipart request is being processed, 0 otherwise
89 log *logger.Logger // global logger
90 conn *Connection // global handle to the Connection (used in the message receive callback)
91 connLock sync.RWMutex // lock for the global connection
94 // init initializes global logger, which logs debug level messages to stdout.
98 log.Level = logger.DebugLevel
101 // SetLogger sets global logger to provided one.
102 func SetLogger(l *logger.Logger) {
106 // Connect connects to VPP using specified VPP adapter and returns the connection handle.
107 // This call blocks until VPP is connected, or an error occurs. Only one connection attempt will be performed.
108 func Connect(vppAdapter adapter.VppAdapter) (*Connection, error) {
109 // create new connection handle
110 c, err := newConnection(vppAdapter)
115 // blocking attempt to connect to VPP
124 // AsyncConnect asynchronously connects to VPP using specified VPP adapter and returns the connection handle
125 // and ConnectionState channel. This call does not block until connection is established, it
126 // returns immediately. The caller is supposed to watch the returned ConnectionState channel for
127 // Connected/Disconnected events. In case of disconnect, the library will asynchronously try to reconnect.
128 func AsyncConnect(vppAdapter adapter.VppAdapter) (*Connection, chan ConnectionEvent, error) {
129 // create new connection handle
130 c, err := newConnection(vppAdapter)
135 // asynchronously attempt to connect to VPP
136 connChan := make(chan ConnectionEvent, notificationChannelBufSize)
137 go c.connectLoop(connChan)
139 return conn, connChan, nil
142 // Disconnect disconnects from VPP and releases all connection-related resources.
143 func (c *Connection) Disconnect() {
148 defer connLock.Unlock()
150 if c != nil && c.vpp != nil {
156 // newConnection returns new connection handle.
157 func newConnection(vppAdapter adapter.VppAdapter) (*Connection, error) {
159 defer connLock.Unlock()
162 return nil, errors.New("only one connection per process is supported")
165 conn = &Connection{vpp: vppAdapter, codec: &MsgCodec{}}
166 conn.channels = make(map[uint32]*api.Channel)
167 conn.msgIDs = make(map[string]uint16)
168 conn.notifSubscriptions = make(map[uint16][]*api.NotifSubscription)
170 conn.vpp.SetMsgCallback(msgCallback)
174 // connectVPP performs one blocking attempt to connect to VPP.
175 func (c *Connection) connectVPP() error {
176 log.Debug("Connecting to VPP...")
179 err := c.vpp.Connect()
185 // store connected state
186 atomic.StoreUint32(&c.connected, 1)
188 // store control ping IDs
189 c.pingReqID, _ = c.GetMessageID(&vpe.ControlPing{})
190 c.pingReplyID, _ = c.GetMessageID(&vpe.ControlPingReply{})
192 log.Info("Connected to VPP.")
196 // disconnectVPP disconnects from VPP in case it is connected.
197 func (c *Connection) disconnectVPP() {
198 if atomic.CompareAndSwapUint32(&c.connected, 1, 0) {
203 // connectLoop attempts to connect to VPP until it succeeds.
204 // Then it continues with healthCheckLoop.
205 func (c *Connection) connectLoop(connChan chan ConnectionEvent) {
206 // loop until connected
208 err := c.connectVPP()
210 // signal connected event
211 connChan <- ConnectionEvent{Timestamp: time.Now(), State: Connected}
216 // we are now connected, continue with health check loop
217 c.healthCheckLoop(connChan)
220 // healthCheckLoop checks whether connection to VPP is alive. In case of disconnect,
221 // it continues with connectLoop and tries to reconnect.
222 func (c *Connection) healthCheckLoop(connChan chan ConnectionEvent) {
223 // create a separate API channel for health check probes
224 ch, err := conn.NewAPIChannel()
226 log.Error("Error by creating health check API channel, health check will be disabled:", err)
230 // send health check probes until an error occurs
232 // wait for healthCheckProbeInterval
233 <-time.After(healthCheckProbeInterval)
235 if atomic.LoadUint32(&c.connected) == 0 {
236 // Disconnect has been called in the meantime, return the healthcheck - reconnect loop
237 log.Debug("Disconnected on request, exiting health check loop.")
241 // send the control ping
242 ch.ReqChan <- &api.VppRequest{Message: &vpe.ControlPing{}}
244 // expect response within timeout period
246 case vppReply := <-ch.ReplyChan:
248 case <-time.After(healthCheckReplyTimeout):
249 err = errors.New("probe reply not received within the timeout period")
252 // in case of error, break & disconnect
254 log.Errorf("VPP health check failed: %v", err)
255 // signal disconnected event via channel
256 connChan <- ConnectionEvent{Timestamp: time.Now(), State: Disconnected}
265 // we are now disconnected, start connect loop
266 c.connectLoop(connChan)
269 // NewAPIChannel returns a new API channel for communication with VPP via govpp core.
270 // It uses default buffer sizes for the request and reply Go channels.
271 func (c *Connection) NewAPIChannel() (*api.Channel, error) {
273 return nil, errors.New("nil connection passed in")
275 return c.NewAPIChannelBuffered(requestChannelBufSize, replyChannelBufSize)
278 // NewAPIChannelBuffered returns a new API channel for communication with VPP via govpp core.
279 // It allows to specify custom buffer sizes for the request and reply Go channels.
280 func (c *Connection) NewAPIChannelBuffered(reqChanBufSize, replyChanBufSize int) (*api.Channel, error) {
282 return nil, errors.New("nil connection passed in")
284 chID := atomic.AddUint32(&c.maxChannelID, 1)
285 chMeta := &channelMetadata{id: chID}
287 ch := api.NewChannelInternal(chMeta)
288 ch.MsgDecoder = c.codec
291 // create the communication channels
292 ch.ReqChan = make(chan *api.VppRequest, reqChanBufSize)
293 ch.ReplyChan = make(chan *api.VppReply, replyChanBufSize)
294 ch.NotifSubsChan = make(chan *api.NotifSubscribeRequest, reqChanBufSize)
295 ch.NotifSubsReplyChan = make(chan error, replyChanBufSize)
297 // store API channel within the client
298 c.channelsLock.Lock()
299 c.channels[chID] = ch
300 c.channelsLock.Unlock()
302 // start watching on the request channel
303 go c.watchRequests(ch, chMeta)
308 // releaseAPIChannel releases API channel that needs to be closed.
309 func (c *Connection) releaseAPIChannel(ch *api.Channel, chMeta *channelMetadata) {
310 log.WithFields(logger.Fields{
311 "context": chMeta.id,
312 }).Debug("API channel closed.")
314 // delete the channel from channels map
315 c.channelsLock.Lock()
316 delete(c.channels, chMeta.id)
317 c.channelsLock.Unlock()