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.
15 // +build !windows,!darwin
20 #cgo CFLAGS: -DPNG_DEBUG=1
21 #cgo LDFLAGS: -lvppapiclient
26 #include <arpa/inet.h>
27 #include <vpp-api/client/vppapiclient.h>
29 extern void go_msg_callback(uint16_t msg_id, void* data, size_t size);
31 typedef struct __attribute__((__packed__)) _req_header {
33 uint32_t client_index;
37 typedef struct __attribute__((__packed__)) _reply_header {
42 govpp_msg_callback(unsigned char *data, int size)
44 reply_header_t *header = ((reply_header_t *)data);
45 go_msg_callback(ntohs(header->msg_id), data, size);
49 govpp_send(uint32_t context, void *data, size_t size)
51 req_header_t *header = ((req_header_t *)data);
52 header->context = htonl(context);
53 return vac_write(data, size);
57 govpp_connect(char *shm)
59 return vac_connect("govpp", shm, govpp_msg_callback, 32);
65 return vac_disconnect();
69 govpp_get_msg_index(char *name_and_crc)
71 return vac_get_msg_index(name_and_crc);
84 "git.fd.io/govpp.git/adapter"
85 "github.com/fsnotify/fsnotify"
89 // MaxWaitReady defines maximum duration before waiting for shared memory
91 MaxWaitReady = time.Second * 15
95 // shmDir is a directory where shared memory is supposed to be created.
97 // vppShmFile is a default name of the file in the shmDir.
98 vppShmFile = "vpe-api"
101 // global VPP binary API client, library vppapiclient only supports
102 // single connection at a time
103 var globalVppClient *vppClient
105 // stubVppClient is the default implementation of the VppAPI.
106 type vppClient struct {
108 msgCallback adapter.MsgCallback
111 // NewVppClient returns a new VPP binary API client.
112 func NewVppClient(shmPrefix string) adapter.VppAPI {
114 shmPrefix: shmPrefix,
118 // Connect connects the process to VPP.
119 func (a *vppClient) Connect() error {
120 if globalVppClient != nil {
121 return fmt.Errorf("already connected to binary API, disconnect first")
125 if a.shmPrefix == "" {
126 rc = C.govpp_connect(nil)
128 shm := C.CString(a.shmPrefix)
129 rc = C.govpp_connect(shm)
132 return fmt.Errorf("connecting to VPP binary API failed (rc=%v)", rc)
139 // Disconnect disconnects the process from VPP.
140 func (a *vppClient) Disconnect() error {
141 globalVppClient = nil
143 rc := C.govpp_disconnect()
145 return fmt.Errorf("disconnecting from VPP binary API failed (rc=%v)", rc)
151 // GetMsgID returns a runtime message ID for the given message name and CRC.
152 func (a *vppClient) GetMsgID(msgName string, msgCrc string) (uint16, error) {
153 nameAndCrc := C.CString(msgName + "_" + msgCrc)
154 defer C.free(unsafe.Pointer(nameAndCrc))
156 msgID := uint16(C.govpp_get_msg_index(nameAndCrc))
157 if msgID == ^uint16(0) {
158 // VPP does not know this message
159 return msgID, fmt.Errorf("unknown message: %v (crc: %v)", msgName, msgCrc)
165 // SendMsg sends a binary-encoded message to VPP.
166 func (a *vppClient) SendMsg(context uint32, data []byte) error {
167 rc := C.govpp_send(C.uint32_t(context), unsafe.Pointer(&data[0]), C.size_t(len(data)))
169 return fmt.Errorf("unable to send the message (rc=%v)", rc)
174 // SetMsgCallback sets a callback function that will be called by the adapter
175 // whenever a message comes from VPP.
176 func (a *vppClient) SetMsgCallback(cb adapter.MsgCallback) {
180 // WaitReady blocks until shared memory for sending
181 // binary api calls is present on the file system.
182 func (a *vppClient) WaitReady() error {
183 // join the path to the shared memory segment
185 if a.shmPrefix == "" {
186 path = filepath.Join(shmDir, vppShmFile)
188 path = filepath.Join(shmDir, a.shmPrefix+"-"+vppShmFile)
191 // check if file at the path already exists
192 if _, err := os.Stat(path); err == nil {
193 // file exists, we are ready
195 } else if !os.IsNotExist(err) {
199 // file does not exist, start watching folder
200 watcher, err := fsnotify.NewWatcher()
204 defer watcher.Close()
206 // start watching directory
207 if err := watcher.Add(shmDir); err != nil {
213 case <-time.After(MaxWaitReady):
214 return fmt.Errorf("waiting for shared memory segment timed out (%s)", MaxWaitReady)
215 case e := <-watcher.Errors:
217 case ev := <-watcher.Events:
219 if (ev.Op & fsnotify.Create) == fsnotify.Create {
220 // file was created, we are ready
228 //export go_msg_callback
229 func go_msg_callback(msgID C.uint16_t, data unsafe.Pointer, size C.size_t) {
230 // convert unsafe.Pointer to byte slice
231 sliceHeader := &reflect.SliceHeader{Data: uintptr(data), Len: int(size), Cap: int(size)}
232 byteSlice := *(*[]byte)(unsafe.Pointer(sliceHeader))
234 globalVppClient.msgCallback(uint16(msgID), byteSlice)