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.
23 logger "github.com/sirupsen/logrus"
27 ErrNotConnected = errors.New("not connected to VPP, ignoring the request")
28 ErrProbeTimeout = errors.New("probe reply not received within timeout period")
31 // watchRequests watches for requests on the request API channel and forwards them as messages to VPP.
32 func (c *Connection) watchRequests(ch *Channel) {
35 case req, ok := <-ch.reqChan:
36 // new request on the request channel
38 // after closing the request channel, release API channel and return
39 c.releaseAPIChannel(ch)
42 if err := c.processRequest(ch, req); err != nil {
43 sendReplyError(ch, req, err)
49 // processRequest processes a single request received on the request channel.
50 func (c *Connection) processRequest(ch *Channel, req *vppRequest) error {
51 // check whether we are connected to VPP
52 if atomic.LoadUint32(&c.vppConnected) == 0 {
53 err := ErrNotConnected
54 log.Errorf("processing request failed: %v", err)
58 // retrieve message ID
59 msgID, err := c.GetMessageID(req.msg)
61 log.WithFields(logger.Fields{
62 "msg_name": req.msg.GetMessageName(),
63 "msg_crc": req.msg.GetCrcString(),
64 "seq_num": req.seqNum,
66 }).Errorf("failed to retrieve message ID")
67 return fmt.Errorf("unable to retrieve message ID: %v", err)
70 // encode the message into binary
71 data, err := c.codec.EncodeMsg(req.msg, msgID)
73 log.WithFields(logger.Fields{
76 "msg_name": req.msg.GetMessageName(),
77 "seq_num": req.seqNum,
79 }).Errorf("failed to encode message: %#v", req.msg)
80 return fmt.Errorf("unable to encode the message: %v", err)
83 context := packRequestContext(ch.id, req.multi, req.seqNum)
85 if log.Level == logger.DebugLevel { // for performance reasons - logrus does some processing even if debugs are disabled
86 log.WithFields(logger.Fields{
89 "is_multi": req.multi,
91 "msg_name": req.msg.GetMessageName(),
92 "msg_size": len(data),
93 "seq_num": req.seqNum,
94 }).Debug(" -> Sending a message to VPP.")
97 // send the request to VPP
98 err = c.vppClient.SendMsg(context, data)
100 err = fmt.Errorf("unable to send the message: %v", err)
101 log.WithFields(logger.Fields{
104 "seq_num": req.seqNum,
110 // send a control ping to determine end of the multipart response
111 pingData, _ := c.codec.EncodeMsg(msgControlPing, c.pingReqID)
113 log.WithFields(logger.Fields{
116 "msg_id": c.pingReqID,
117 "msg_size": len(pingData),
118 "seq_num": req.seqNum,
119 }).Debug(" -> Sending a control ping to VPP.")
121 if err := c.vppClient.SendMsg(context, pingData); err != nil {
122 log.WithFields(logger.Fields{
125 "seq_num": req.seqNum,
126 }).Warnf("unable to send control ping: %v", err)
133 // msgCallback is called whenever any binary API message comes from VPP.
134 func (c *Connection) msgCallback(msgID uint16, data []byte) {
136 defer connLock.RUnlock()
139 log.Warn("Already disconnected, ignoring the message.")
143 msg, ok := c.msgMap[msgID]
145 log.Warnf("Unknown message received, ID: %d", msgID)
149 // decode message context to fix for special cases of messages,
151 // - replies that don't have context as first field (comes as zero)
152 // - events that don't have context at all (comes as non zero)
154 context, err := c.codec.DecodeMsgContext(data, msg)
156 log.Errorf("decoding context failed: %v", err)
159 chanID, isMulti, seqNum := unpackRequestContext(context)
160 if log.Level == logger.DebugLevel { // for performance reasons - logrus does some processing even if debugs are disabled
161 log.WithFields(logger.Fields{
164 "msg_name": msg.GetMessageName(),
165 "msg_size": len(data),
169 }).Debug(" <- Received a message from VPP.")
172 if context == 0 || c.isNotificationMessage(msgID) {
173 // process the message as a notification
174 c.sendNotifications(msgID, data)
178 // match ch according to the context
179 c.channelsLock.RLock()
180 ch, ok := c.channels[chanID]
181 c.channelsLock.RUnlock()
183 log.WithFields(logger.Fields{
186 }).Error("Channel ID not known, ignoring the message.")
190 // if this is a control ping reply to a multipart request,
191 // treat this as a last part of the reply
192 lastReplyReceived := isMulti && msgID == c.pingReplyID
194 // send the data to the channel, it needs to be copied,
195 // because it will be freed after this function returns
196 sendReply(ch, &vppReply{
199 data: append([]byte(nil), data...),
200 lastReceived: lastReplyReceived,
203 // store actual time of this reply
204 c.lastReplyLock.Lock()
205 c.lastReply = time.Now()
206 c.lastReplyLock.Unlock()
209 // sendReply sends the reply into the go channel, if it cannot be completed without blocking, otherwise
210 // it logs the error and do not send the message.
211 func sendReply(ch *Channel, reply *vppReply) {
213 case ch.replyChan <- reply:
214 // reply sent successfully
215 case <-time.After(time.Millisecond * 100):
216 // receiver still not ready
217 log.WithFields(logger.Fields{
219 "msg_id": reply.msgID,
220 "seq_num": reply.seqNum,
221 }).Warn("Unable to send the reply, reciever end not ready.")
225 func sendReplyError(ch *Channel, req *vppRequest, err error) {
226 sendReply(ch, &vppReply{seqNum: req.seqNum, err: err})
229 // isNotificationMessage returns true if someone has subscribed to provided message ID.
230 func (c *Connection) isNotificationMessage(msgID uint16) bool {
231 c.subscriptionsLock.RLock()
232 defer c.subscriptionsLock.RUnlock()
234 _, exists := c.subscriptions[msgID]
238 // sendNotifications send a notification message to all subscribers subscribed for that message.
239 func (c *Connection) sendNotifications(msgID uint16, data []byte) {
240 c.subscriptionsLock.RLock()
241 defer c.subscriptionsLock.RUnlock()
245 // send to notification to each subscriber
246 for _, sub := range c.subscriptions[msgID] {
247 log.WithFields(logger.Fields{
248 "msg_name": sub.event.GetMessageName(),
250 "msg_size": len(data),
251 }).Debug("Sending a notification to the subscription channel.")
253 event := sub.msgFactory()
254 if err := c.codec.DecodeMsg(data, event); err != nil {
255 log.WithFields(logger.Fields{
256 "msg_name": sub.event.GetMessageName(),
258 "msg_size": len(data),
259 }).Errorf("Unable to decode the notification message: %v", err)
263 // send the message into the go channel of the subscription
265 case sub.notifChan <- event:
266 // message sent successfully
268 // unable to write into the channel without blocking
269 log.WithFields(logger.Fields{
270 "msg_name": sub.event.GetMessageName(),
272 "msg_size": len(data),
273 }).Warn("Unable to deliver the notification, reciever end not ready.")
280 log.WithFields(logger.Fields{
282 "msg_size": len(data),
283 }).Info("No subscription found for the notification message.")
287 // +------------------+-------------------+-----------------------+
288 // | 15b = channel ID | 1b = is multipart | 16b = sequence number |
289 // +------------------+-------------------+-----------------------+
290 func packRequestContext(chanID uint16, isMultipart bool, seqNum uint16) uint32 {
291 context := uint32(chanID) << 17
295 context |= uint32(seqNum)
299 func unpackRequestContext(context uint32) (chanID uint16, isMulipart bool, seqNum uint16) {
300 chanID = uint16(context >> 17)
301 if ((context >> 16) & 0x1) != 0 {
304 seqNum = uint16(context & 0xffff)
308 // compareSeqNumbers returns -1, 0, 1 if sequence number <seqNum1> precedes, equals to,
309 // or succeeds seq. number <seqNum2>.
310 // Since sequence numbers cycle in the finite set of size 2^16, the function
311 // must assume that the distance between compared sequence numbers is less than
312 // (2^16)/2 to determine the order.
313 func compareSeqNumbers(seqNum1, seqNum2 uint16) int {
314 // calculate distance from seqNum1 to seqNum2
316 if seqNum1 <= seqNum2 {
317 dist = seqNum2 - seqNum1
319 dist = 0xffff - (seqNum1 - seqNum2 - 1)
323 } else if dist <= 0x8000 {