Simplify subscribing to events and fix events
[govpp.git] / adapter / vppapiclient / vppapiclient_adapter.go
1 // Copyright (c) 2017 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 // +build !windows,!darwin
16
17 // Package vppapiclient is the default VPP adapter being used for the connection with VPP via shared memory.
18 // It is based on the communication with the vppapiclient VPP library written in C via CGO.
19 package vppapiclient
20
21 /*
22 #cgo CFLAGS: -DPNG_DEBUG=1
23 #cgo LDFLAGS: -lvppapiclient
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <stdint.h>
28 #include <arpa/inet.h>
29 #include <vpp-api/client/vppapiclient.h>
30
31 extern void go_msg_callback(uint16_t msg_id, void* data, size_t size);
32
33 typedef struct __attribute__((__packed__)) _req_header {
34     uint16_t msg_id;
35     uint32_t client_index;
36     uint32_t context;
37 } req_header_t;
38
39 typedef struct __attribute__((__packed__)) _reply_header {
40     uint16_t msg_id;
41 } reply_header_t;
42
43 static void
44 govpp_msg_callback (unsigned char *data, int size)
45 {
46     reply_header_t *header = ((reply_header_t *)data);
47     go_msg_callback(ntohs(header->msg_id), data, size);
48 }
49
50 static int
51 govpp_send(uint32_t context, void *data, size_t size)
52 {
53         req_header_t *header = ((req_header_t *)data);
54         header->context = htonl(context);
55     return vac_write(data, size);
56 }
57
58 static int
59 govpp_connect (char *shm)
60 {
61     return vac_connect("govpp", shm, govpp_msg_callback, 32);
62 }
63
64 static int
65 govvp_disconnect()
66 {
67     return vac_disconnect();
68 }
69
70 static uint32_t
71 govpp_get_msg_index(char *name_and_crc)
72 {
73     return vac_get_msg_index(name_and_crc);
74 }
75 */
76 import "C"
77
78 import (
79         "fmt"
80         "os"
81         "path/filepath"
82         "reflect"
83         "unsafe"
84
85         "git.fd.io/govpp.git/adapter"
86         "github.com/fsnotify/fsnotify"
87 )
88
89 const (
90         // watchedFolder is a folder where vpp's shared memory is supposed to be created.
91         // File system events are monitored in this folder.
92         watchedFolder = "/dev/shm/"
93         // watchedFile is a default name of the file in the watchedFolder. Once the file is present,
94         // the vpp is ready to accept a new connection.
95         watchedFile = "vpe-api"
96 )
97
98 // vppAPIClientAdapter is the opaque context of the adapter.
99 type vppAPIClientAdapter struct {
100         shmPrefix string
101         callback  adapter.MsgCallback
102 }
103
104 var vppClient *vppAPIClientAdapter // global vpp API client adapter context
105
106 // NewVppAdapter returns a new vpp API client adapter.
107 func NewVppAdapter(shmPrefix string) adapter.VppAdapter {
108         return &vppAPIClientAdapter{
109                 shmPrefix: shmPrefix,
110         }
111 }
112
113 // Connect connects the process to VPP.
114 func (a *vppAPIClientAdapter) Connect() error {
115         vppClient = a
116         var rc _Ctype_int
117         if a.shmPrefix == "" {
118                 rc = C.govpp_connect(nil)
119         } else {
120                 shm := C.CString(a.shmPrefix)
121                 rc = C.govpp_connect(shm)
122         }
123         if rc != 0 {
124                 return fmt.Errorf("unable to connect to VPP (error=%d)", rc)
125         }
126         return nil
127 }
128
129 // Disconnect disconnects the process from VPP.
130 func (a *vppAPIClientAdapter) Disconnect() {
131         C.govvp_disconnect()
132 }
133
134 // GetMsgID returns a runtime message ID for the given message name and CRC.
135 func (a *vppAPIClientAdapter) GetMsgID(msgName string, msgCrc string) (uint16, error) {
136         nameAndCrc := C.CString(msgName + "_" + msgCrc)
137         defer C.free(unsafe.Pointer(nameAndCrc))
138
139         msgID := uint16(C.govpp_get_msg_index(nameAndCrc))
140         if msgID == ^uint16(0) {
141                 // VPP does not know this message
142                 return msgID, fmt.Errorf("unknown message: %v (crc: %v)", msgName, msgCrc)
143         }
144
145         return msgID, nil
146 }
147
148 // SendMsg sends a binary-encoded message to VPP.
149 func (a *vppAPIClientAdapter) SendMsg(context uint32, data []byte) error {
150         rc := C.govpp_send(C.uint32_t(context), unsafe.Pointer(&data[0]), C.size_t(len(data)))
151         if rc != 0 {
152                 return fmt.Errorf("unable to send the message (error=%d)", rc)
153         }
154         return nil
155 }
156
157 // SetMsgCallback sets a callback function that will be called by the adapter whenever a message comes from VPP.
158 func (a *vppAPIClientAdapter) SetMsgCallback(cb adapter.MsgCallback) {
159         a.callback = cb
160 }
161
162 // WaitReady blocks until shared memory for sending
163 // binary api calls is present on the file system.
164 func (a *vppAPIClientAdapter) WaitReady() error {
165         // Path to the shared memory segment
166         var path string
167         if a.shmPrefix == "" {
168                 path = filepath.Join(watchedFolder, watchedFile)
169         } else {
170                 path = filepath.Join(watchedFolder, a.shmPrefix+"-"+watchedFile)
171         }
172
173         // Watch folder if file does not exist yet
174         if !fileExists(path) {
175                 watcher, err := fsnotify.NewWatcher()
176                 if err != nil {
177                         return err
178                 }
179                 defer watcher.Close()
180
181                 if err := watcher.Add(watchedFolder); err != nil {
182                         return err
183                 }
184
185                 for {
186                         ev := <-watcher.Events
187                         if ev.Name == path && (ev.Op&fsnotify.Create) == fsnotify.Create {
188                                 break
189                         }
190                 }
191         }
192
193         return nil
194 }
195
196 func fileExists(name string) bool {
197         if _, err := os.Stat(name); err != nil {
198                 if os.IsNotExist(err) {
199                         return false
200                 }
201         }
202         return true
203 }
204
205 //export go_msg_callback
206 func go_msg_callback(msgID C.uint16_t, data unsafe.Pointer, size C.size_t) {
207         // convert unsafe.Pointer to byte slice
208         slice := &reflect.SliceHeader{Data: uintptr(data), Len: int(size), Cap: int(size)}
209         byteArr := *(*[]byte)(unsafe.Pointer(slice))
210
211         vppClient.callback(uint16(msgID), byteArr)
212 }