fixup race on ch.reqChan
[govpp.git] / core / channel.go
1 // Copyright (c) 2018 Cisco and/or its affiliates.
2 //
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:
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 package core
16
17 import (
18         "errors"
19         "fmt"
20         "reflect"
21         "strings"
22         "time"
23
24         "git.fd.io/govpp.git/api"
25         "github.com/sirupsen/logrus"
26 )
27
28 var (
29         ErrInvalidRequestCtx = errors.New("invalid request context")
30 )
31
32 // MessageCodec provides functionality for decoding binary data to generated API messages.
33 type MessageCodec interface {
34         // EncodeMsg encodes message into binary data.
35         EncodeMsg(msg api.Message, msgID uint16) ([]byte, error)
36         // DecodeMsg decodes binary-encoded data of a message into provided Message structure.
37         DecodeMsg(data []byte, msg api.Message) error
38 }
39
40 // MessageIdentifier provides identification of generated API messages.
41 type MessageIdentifier interface {
42         // GetMessageID returns message identifier of given API message.
43         GetMessageID(msg api.Message) (uint16, error)
44         // LookupByID looks up message name and crc by ID
45         LookupByID(msgID uint16) (api.Message, error)
46 }
47
48 // vppRequest is a request that will be sent to VPP.
49 type vppRequest struct {
50         seqNum uint16      // sequence number
51         msg    api.Message // binary API message to be send to VPP
52         multi  bool        // true if multipart response is expected
53 }
54
55 // vppReply is a reply received from VPP.
56 type vppReply struct {
57         seqNum       uint16 // sequence number
58         msgID        uint16 // ID of the message
59         data         []byte // encoded data with the message
60         lastReceived bool   // for multi request, true if the last reply has been already received
61         err          error  // in case of error, data is nil and this member contains error
62 }
63
64 // requestCtx is a context for request with single reply
65 type requestCtx struct {
66         ch     *Channel
67         seqNum uint16
68 }
69
70 // multiRequestCtx is a context for request with multiple responses
71 type multiRequestCtx struct {
72         ch     *Channel
73         seqNum uint16
74 }
75
76 // subscriptionCtx is a context of subscription for delivery of specific notification messages.
77 type subscriptionCtx struct {
78         ch         *Channel
79         notifChan  chan api.Message   // channel where notification messages will be delivered to
80         msgID      uint16             // message ID for the subscribed event message
81         event      api.Message        // event message that this subscription is for
82         msgFactory func() api.Message // function that returns a new instance of the specific message that is expected as a notification
83 }
84
85 // channel is the main communication interface with govpp core. It contains four Go channels, one for sending the requests
86 // to VPP, one for receiving the replies from it and the same set for notifications. The user can access the Go channels
87 // via methods provided by Channel interface in this package. Do not use the same channel from multiple goroutines
88 // concurrently, otherwise the responses could mix! Use multiple channels instead.
89 type Channel struct {
90         id   uint16
91         conn *Connection
92
93         reqChan   chan *vppRequest // channel for sending the requests to VPP
94         replyChan chan *vppReply   // channel where VPP replies are delivered to
95
96         msgCodec      MessageCodec      // used to decode binary data to generated API messages
97         msgIdentifier MessageIdentifier // used to retrieve message ID of a message
98
99         lastSeqNum uint16 // sequence number of the last sent request
100
101         delayedReply *vppReply     // reply already taken from ReplyChan, buffered for later delivery
102         replyTimeout time.Duration // maximum time that the API waits for a reply from VPP before returning an error, can be set with SetReplyTimeout
103 }
104
105 func newChannel(id uint16, conn *Connection, codec MessageCodec, identifier MessageIdentifier, reqSize, replySize int) *Channel {
106         return &Channel{
107                 id:            id,
108                 conn:          conn,
109                 msgCodec:      codec,
110                 msgIdentifier: identifier,
111                 reqChan:       make(chan *vppRequest, reqSize),
112                 replyChan:     make(chan *vppReply, replySize),
113                 replyTimeout:  DefaultReplyTimeout,
114         }
115 }
116
117 func (ch *Channel) GetID() uint16 {
118         return ch.id
119 }
120
121 func (ch *Channel) nextSeqNum() uint16 {
122         ch.lastSeqNum++
123         return ch.lastSeqNum
124 }
125
126 func (ch *Channel) SendRequest(msg api.Message) api.RequestCtx {
127         seqNum := ch.nextSeqNum()
128         ch.reqChan <- &vppRequest{
129                 msg:    msg,
130                 seqNum: seqNum,
131         }
132         return &requestCtx{ch: ch, seqNum: seqNum}
133 }
134
135 func (ch *Channel) SendMultiRequest(msg api.Message) api.MultiRequestCtx {
136         seqNum := ch.nextSeqNum()
137         ch.reqChan <- &vppRequest{
138                 msg:    msg,
139                 seqNum: seqNum,
140                 multi:  true,
141         }
142         return &multiRequestCtx{ch: ch, seqNum: seqNum}
143 }
144
145 func (ch *Channel) CheckCompatiblity(msgs ...api.Message) error {
146         for _, msg := range msgs {
147                 _, err := ch.msgIdentifier.GetMessageID(msg)
148                 if err != nil {
149                         return err
150                 }
151         }
152         return nil
153 }
154
155 func (ch *Channel) SubscribeNotification(notifChan chan api.Message, event api.Message) (api.SubscriptionCtx, error) {
156         msgID, err := ch.msgIdentifier.GetMessageID(event)
157         if err != nil {
158                 log.WithFields(logrus.Fields{
159                         "msg_name": event.GetMessageName(),
160                         "msg_crc":  event.GetCrcString(),
161                 }).Errorf("unable to retrieve message ID: %v", err)
162                 return nil, fmt.Errorf("unable to retrieve event message ID: %v", err)
163         }
164
165         sub := &subscriptionCtx{
166                 ch:         ch,
167                 notifChan:  notifChan,
168                 msgID:      msgID,
169                 event:      event,
170                 msgFactory: getMsgFactory(event),
171         }
172
173         // add the subscription into map
174         ch.conn.subscriptionsLock.Lock()
175         defer ch.conn.subscriptionsLock.Unlock()
176
177         ch.conn.subscriptions[msgID] = append(ch.conn.subscriptions[msgID], sub)
178
179         return sub, nil
180 }
181
182 func (ch *Channel) SetReplyTimeout(timeout time.Duration) {
183         ch.replyTimeout = timeout
184 }
185
186 func (ch *Channel) Close() {
187         close(ch.reqChan)
188 }
189
190 func (req *requestCtx) ReceiveReply(msg api.Message) error {
191         if req == nil || req.ch == nil {
192                 return ErrInvalidRequestCtx
193         }
194
195         lastReplyReceived, err := req.ch.receiveReplyInternal(msg, req.seqNum)
196         if err != nil {
197                 return err
198         } else if lastReplyReceived {
199                 return errors.New("multipart reply recieved while a single reply expected")
200         }
201
202         return nil
203 }
204
205 func (req *multiRequestCtx) ReceiveReply(msg api.Message) (lastReplyReceived bool, err error) {
206         if req == nil || req.ch == nil {
207                 return false, ErrInvalidRequestCtx
208         }
209
210         return req.ch.receiveReplyInternal(msg, req.seqNum)
211 }
212
213 func (sub *subscriptionCtx) Unsubscribe() error {
214         log.WithFields(logrus.Fields{
215                 "msg_name": sub.event.GetMessageName(),
216                 "msg_id":   sub.msgID,
217         }).Debug("Removing notification subscription.")
218
219         // remove the subscription from the map
220         sub.ch.conn.subscriptionsLock.Lock()
221         defer sub.ch.conn.subscriptionsLock.Unlock()
222
223         for i, item := range sub.ch.conn.subscriptions[sub.msgID] {
224                 if item == sub {
225                         // remove i-th item in the slice
226                         sub.ch.conn.subscriptions[sub.msgID] = append(sub.ch.conn.subscriptions[sub.msgID][:i], sub.ch.conn.subscriptions[sub.msgID][i+1:]...)
227                         return nil
228                 }
229         }
230
231         return fmt.Errorf("subscription for %q not found", sub.event.GetMessageName())
232 }
233
234 // receiveReplyInternal receives a reply from the reply channel into the provided msg structure.
235 func (ch *Channel) receiveReplyInternal(msg api.Message, expSeqNum uint16) (lastReplyReceived bool, err error) {
236         if msg == nil {
237                 return false, errors.New("nil message passed in")
238         }
239
240         var ignore bool
241
242         if vppReply := ch.delayedReply; vppReply != nil {
243                 // try the delayed reply
244                 ch.delayedReply = nil
245                 ignore, lastReplyReceived, err = ch.processReply(vppReply, expSeqNum, msg)
246                 if !ignore {
247                         return lastReplyReceived, err
248                 }
249         }
250
251         timer := time.NewTimer(ch.replyTimeout)
252         for {
253                 select {
254                 // blocks until a reply comes to ReplyChan or until timeout expires
255                 case vppReply := <-ch.replyChan:
256                         ignore, lastReplyReceived, err = ch.processReply(vppReply, expSeqNum, msg)
257                         if ignore {
258                                 logrus.Warnf("ignoring reply: %+v", vppReply)
259                                 continue
260                         }
261                         return lastReplyReceived, err
262
263                 case <-timer.C:
264                         err = fmt.Errorf("no reply received within the timeout period %s", ch.replyTimeout)
265                         return false, err
266                 }
267         }
268         return
269 }
270
271 func (ch *Channel) processReply(reply *vppReply, expSeqNum uint16, msg api.Message) (ignore bool, lastReplyReceived bool, err error) {
272         // check the sequence number
273         cmpSeqNums := compareSeqNumbers(reply.seqNum, expSeqNum)
274         if cmpSeqNums == -1 {
275                 // reply received too late, ignore the message
276                 logrus.WithField("seqNum", reply.seqNum).
277                         Warn("Received reply to an already closed binary API request")
278                 ignore = true
279                 return
280         }
281         if cmpSeqNums == 1 {
282                 ch.delayedReply = reply
283                 err = fmt.Errorf("missing binary API reply with sequence number: %d", expSeqNum)
284                 return
285         }
286
287         if reply.err != nil {
288                 err = reply.err
289                 return
290         }
291         if reply.lastReceived {
292                 lastReplyReceived = true
293                 return
294         }
295
296         // message checks
297         var expMsgID uint16
298         expMsgID, err = ch.msgIdentifier.GetMessageID(msg)
299         if err != nil {
300                 err = fmt.Errorf("message %s with CRC %s is not compatible with the VPP we are connected to",
301                         msg.GetMessageName(), msg.GetCrcString())
302                 return
303         }
304
305         if reply.msgID != expMsgID {
306                 var msgNameCrc string
307                 if replyMsg, err := ch.msgIdentifier.LookupByID(reply.msgID); err != nil {
308                         msgNameCrc = err.Error()
309                 } else {
310                         msgNameCrc = getMsgNameWithCrc(replyMsg)
311                 }
312
313                 err = fmt.Errorf("received invalid message ID (seqNum=%d), expected %d (%s), but got %d (%s) "+
314                         "(check if multiple goroutines are not sharing single GoVPP channel)",
315                         reply.seqNum, expMsgID, msg.GetMessageName(), reply.msgID, msgNameCrc)
316                 return
317         }
318
319         // decode the message
320         if err = ch.msgCodec.DecodeMsg(reply.data, msg); err != nil {
321                 return
322         }
323
324         // check Retval and convert it into VnetAPIError error
325         if strings.HasSuffix(msg.GetMessageName(), "_reply") {
326                 // TODO: use categories for messages to avoid checking message name
327                 if f := reflect.Indirect(reflect.ValueOf(msg)).FieldByName("Retval"); f.IsValid() {
328                         retval := int32(f.Int())
329                         err = api.RetvalToVPPApiError(retval)
330                 }
331         }
332
333         return
334 }