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/api"
29 var ReplyChannelTimeout = time.Millisecond * 100
32 ErrNotConnected = errors.New("not connected to VPP, ignoring the request")
33 ErrProbeTimeout = errors.New("probe reply not received within timeout period")
36 // watchRequests watches for requests on the request API channel and forwards them as messages to VPP.
37 func (c *Connection) watchRequests(ch *Channel) {
40 case req, ok := <-ch.reqChan:
41 // new request on the request channel
43 // after closing the request channel, release API channel and return
44 c.releaseAPIChannel(ch)
47 if err := c.processRequest(ch, req); err != nil {
48 sendReply(ch, &vppReply{
50 err: fmt.Errorf("unable to process request: %w", err),
57 // processRequest processes a single request received on the request channel.
58 func (c *Connection) sendMessage(context uint32, msg api.Message) error {
59 // check whether we are connected to VPP
60 if atomic.LoadUint32(&c.vppConnected) == 0 {
61 return ErrNotConnected
64 /*log := log.WithFields(logger.Fields{
66 "msg_name": msg.GetMessageName(),
67 "msg_crc": msg.GetCrcString(),
70 // retrieve message ID
71 msgID, err := c.GetMessageID(msg)
73 //log.WithError(err).Debugf("unable to retrieve message ID: %#v", msg)
77 //log = log.WithField("msg_id", msgID)
80 data, err := c.codec.EncodeMsg(msg, msgID)
82 log.WithError(err).Debugf("unable to encode message: %#v", msg)
86 //log = log.WithField("msg_length", len(data))
88 if log.Level >= logger.DebugLevel {
89 log.Debugf("--> SEND: MSG %T %+v", msg, msg)
92 // send message to VPP
93 err = c.vppClient.SendMsg(context, data)
95 log.WithError(err).Debugf("unable to send message: %#v", msg)
102 // processRequest processes a single request received on the request channel.
103 func (c *Connection) processRequest(ch *Channel, req *vppRequest) error {
104 // check whether we are connected to VPP
105 if atomic.LoadUint32(&c.vppConnected) == 0 {
106 err := ErrNotConnected
107 log.WithFields(logger.Fields{
109 "seq_num": req.seqNum,
110 "msg_name": req.msg.GetMessageName(),
111 "msg_crc": req.msg.GetCrcString(),
113 }).Warnf("Unable to process request")
117 // retrieve message ID
118 msgID, err := c.GetMessageID(req.msg)
120 log.WithFields(logger.Fields{
122 "msg_name": req.msg.GetMessageName(),
123 "msg_crc": req.msg.GetCrcString(),
124 "seq_num": req.seqNum,
126 }).Warnf("Unable to retrieve message ID")
130 // encode the message into binary
131 data, err := c.codec.EncodeMsg(req.msg, msgID)
133 log.WithFields(logger.Fields{
136 "msg_name": req.msg.GetMessageName(),
137 "msg_crc": req.msg.GetCrcString(),
138 "seq_num": req.seqNum,
140 }).Warnf("Unable to encode message: %T %+v", req.msg, req.msg)
144 context := packRequestContext(ch.id, req.multi, req.seqNum)
146 if log.Level >= logger.DebugLevel { // for performance reasons - logrus does some processing even if debugs are disabled
147 log.WithFields(logger.Fields{
150 "msg_name": req.msg.GetMessageName(),
151 "msg_crc": req.msg.GetCrcString(),
152 "seq_num": req.seqNum,
153 "is_multi": req.multi,
155 "data_len": len(data),
156 }).Debugf("--> SEND MSG: %T %+v", req.msg, req.msg)
159 // send the request to VPP
160 err = c.vppClient.SendMsg(context, data)
162 log.WithFields(logger.Fields{
165 "msg_name": req.msg.GetMessageName(),
166 "msg_crc": req.msg.GetCrcString(),
167 "seq_num": req.seqNum,
168 "is_multi": req.multi,
170 "data_len": len(data),
172 }).Warnf("Unable to send message")
177 // send a control ping to determine end of the multipart response
178 pingData, _ := c.codec.EncodeMsg(c.msgControlPing, c.pingReqID)
180 if log.Level >= logger.DebugLevel {
181 log.WithFields(logger.Fields{
183 "msg_id": c.pingReqID,
184 "msg_name": c.msgControlPing.GetMessageName(),
185 "msg_crc": c.msgControlPing.GetCrcString(),
186 "seq_num": req.seqNum,
188 "data_len": len(pingData),
189 }).Debugf(" -> SEND MSG: %T", c.msgControlPing)
192 if err := c.vppClient.SendMsg(context, pingData); err != nil {
193 log.WithFields(logger.Fields{
195 "seq_num": req.seqNum,
197 }).Warnf("unable to send control ping")
204 // msgCallback is called whenever any binary API message comes from VPP.
205 func (c *Connection) msgCallback(msgID uint16, data []byte) {
209 ).Warn("Connection already disconnected, ignoring the message.")
213 msg, ok := c.msgMap[msgID]
215 log.Warnf("Unknown message received, ID: %d", msgID)
219 // decode message context to fix for special cases of messages,
221 // - replies that don't have context as first field (comes as zero)
222 // - events that don't have context at all (comes as non zero)
224 context, err := c.codec.DecodeMsgContext(data, msg)
226 log.WithField("msg_id", msgID).Warnf("Unable to decode message context: %v", err)
230 chanID, isMulti, seqNum := unpackRequestContext(context)
232 if log.Level == logger.DebugLevel { // for performance reasons - logrus does some processing even if debugs are disabled
233 msg = reflect.New(reflect.TypeOf(msg).Elem()).Interface().(api.Message)
235 // decode the message
236 if err = c.codec.DecodeMsg(data, msg); err != nil {
237 err = fmt.Errorf("decoding message failed: %w", err)
241 log.WithFields(logger.Fields{
244 "msg_size": len(data),
248 "msg_crc": msg.GetCrcString(),
249 }).Debugf("<-- govpp RECEIVE: %s %+v", msg.GetMessageName(), msg)
252 if context == 0 || c.isNotificationMessage(msgID) {
253 // process the message as a notification
254 c.sendNotifications(msgID, data)
258 // match ch according to the context
259 c.channelsLock.RLock()
260 ch, ok := c.channels[chanID]
261 c.channelsLock.RUnlock()
263 log.WithFields(logger.Fields{
266 }).Error("Channel ID not known, ignoring the message.")
270 // if this is a control ping reply to a multipart request,
271 // treat this as a last part of the reply
272 lastReplyReceived := isMulti && msgID == c.pingReplyID
274 // send the data to the channel, it needs to be copied,
275 // because it will be freed after this function returns
276 sendReply(ch, &vppReply{
279 data: append([]byte(nil), data...),
280 lastReceived: lastReplyReceived,
283 // store actual time of this reply
284 c.lastReplyLock.Lock()
285 c.lastReply = time.Now()
286 c.lastReplyLock.Unlock()
289 // sendReply sends the reply into the go channel, if it cannot be completed without blocking, otherwise
290 // it logs the error and do not send the message.
291 func sendReply(ch *Channel, reply *vppReply) {
292 // first try to avoid creating timer
294 case ch.replyChan <- reply:
295 return // reply sent ok
297 // reply channel full
299 if ch.receiveReplyTimeout == 0 {
300 log.WithFields(logger.Fields{
302 "msg_id": reply.msgID,
303 "seq_num": reply.seqNum,
305 }).Warn("Reply channel full, dropping reply.")
309 case ch.replyChan <- reply:
310 return // reply sent ok
311 case <-time.After(ch.receiveReplyTimeout):
312 // receiver still not ready
313 log.WithFields(logger.Fields{
315 "msg_id": reply.msgID,
316 "seq_num": reply.seqNum,
318 }).Warnf("Unable to send reply (reciever end not ready in %v).", ch.receiveReplyTimeout)
322 // isNotificationMessage returns true if someone has subscribed to provided message ID.
323 func (c *Connection) isNotificationMessage(msgID uint16) bool {
324 c.subscriptionsLock.RLock()
325 defer c.subscriptionsLock.RUnlock()
327 _, exists := c.subscriptions[msgID]
331 // sendNotifications send a notification message to all subscribers subscribed for that message.
332 func (c *Connection) sendNotifications(msgID uint16, data []byte) {
333 c.subscriptionsLock.RLock()
334 defer c.subscriptionsLock.RUnlock()
338 // send to notification to each subscriber
339 for _, sub := range c.subscriptions[msgID] {
340 log.WithFields(logger.Fields{
341 "msg_name": sub.event.GetMessageName(),
343 "msg_size": len(data),
344 }).Debug("Sending a notification to the subscription channel.")
346 event := sub.msgFactory()
347 if err := c.codec.DecodeMsg(data, event); err != nil {
348 log.WithFields(logger.Fields{
349 "msg_name": sub.event.GetMessageName(),
351 "msg_size": len(data),
353 }).Warnf("Unable to decode the notification message")
357 // send the message into the go channel of the subscription
359 case sub.notifChan <- event:
360 // message sent successfully
362 // unable to write into the channel without blocking
363 log.WithFields(logger.Fields{
364 "msg_name": sub.event.GetMessageName(),
366 "msg_size": len(data),
367 }).Warn("Unable to deliver the notification, reciever end not ready.")
374 log.WithFields(logger.Fields{
376 "msg_size": len(data),
377 }).Info("No subscription found for the notification message.")
381 // +------------------+-------------------+-----------------------+
382 // | 15b = channel ID | 1b = is multipart | 16b = sequence number |
383 // +------------------+-------------------+-----------------------+
384 func packRequestContext(chanID uint16, isMultipart bool, seqNum uint16) uint32 {
385 context := uint32(chanID) << 17
389 context |= uint32(seqNum)
393 func unpackRequestContext(context uint32) (chanID uint16, isMulipart bool, seqNum uint16) {
394 chanID = uint16(context >> 17)
395 if ((context >> 16) & 0x1) != 0 {
398 seqNum = uint16(context & 0xffff)
402 // compareSeqNumbers returns -1, 0, 1 if sequence number <seqNum1> precedes, equals to,
403 // or succeeds seq. number <seqNum2>.
404 // Since sequence numbers cycle in the finite set of size 2^16, the function
405 // must assume that the distance between compared sequence numbers is less than
406 // (2^16)/2 to determine the order.
407 func compareSeqNumbers(seqNum1, seqNum2 uint16) int {
408 // calculate distance from seqNum1 to seqNum2
410 if seqNum1 <= seqNum2 {
411 dist = seqNum2 - seqNum1
413 dist = 0xffff - (seqNum1 - seqNum2 - 1)
417 } else if dist <= 0x8000 {