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
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.
22 #cgo CFLAGS: -DPNG_DEBUG=1
23 #cgo LDFLAGS: -lvppapiclient
28 #include <arpa/inet.h>
29 #include <vpp-api/client/vppapiclient.h>
31 extern void go_msg_callback(uint16_t, uint32_t, void*, size_t);
33 typedef struct __attribute__((__packed__)) _req_header {
35 uint32_t client_index;
39 typedef struct __attribute__((__packed__)) _reply_header {
45 govpp_msg_callback (unsigned char *data, int size)
47 reply_header_t *header = ((reply_header_t *)data);
48 go_msg_callback(ntohs(header->msg_id), ntohl(header->context), data, size);
54 return vac_connect("govpp", NULL, govpp_msg_callback, 32);
60 return vac_disconnect();
64 govpp_send(uint32_t context, void *data, size_t size)
66 req_header_t *header = ((req_header_t *)data);
67 header->context = htonl(context);
68 return vac_write(data, size);
72 govpp_get_msg_index(char *name_and_crc)
74 return vac_get_msg_index(name_and_crc);
85 "git.fd.io/govpp.git/adapter"
88 // vppAPIClientAdapter is the opaque context of the adapter.
89 type vppAPIClientAdapter struct {
90 callback func(context uint32, msgId uint16, data []byte)
93 var vppClient *vppAPIClientAdapter // global vpp API client adapter context
95 // NewVppAdapter returns a new vpp API client adapter.
96 func NewVppAdapter() adapter.VppAdapter {
97 return &vppAPIClientAdapter{}
100 // Connect connects the process to VPP.
101 func (a *vppAPIClientAdapter) Connect() error {
103 rc := C.govpp_connect()
105 return fmt.Errorf("unable to connect to VPP (error=%d)", rc)
110 // Disconnect disconnects the process from VPP.
111 func (a *vppAPIClientAdapter) Disconnect() {
115 // GetMsgID returns a runtime message ID for the given message name and CRC.
116 func (a *vppAPIClientAdapter) GetMsgID(msgName string, msgCrc string) (uint16, error) {
117 nameAndCrc := C.CString(fmt.Sprintf("%s_%s", msgName, msgCrc))
118 defer C.free(unsafe.Pointer(nameAndCrc))
120 msgID := uint16(C.govpp_get_msg_index(nameAndCrc))
121 if msgID == ^uint16(0) {
122 return msgID, errors.New("unkonwn message")
128 // SendMsg sends a binary-encoded message to VPP.
129 func (a *vppAPIClientAdapter) SendMsg(clientID uint32, data []byte) error {
130 rc := C.govpp_send(C.uint32_t(clientID), unsafe.Pointer(&data[0]), C.size_t(len(data)))
132 return fmt.Errorf("unable to send the message (error=%d)", rc)
137 // SetMsgCallback sets a callback function that will be called by the adapter whenever a message comes from VPP.
138 func (a *vppAPIClientAdapter) SetMsgCallback(cb func(context uint32, msgID uint16, data []byte)) {
142 //export go_msg_callback
143 func go_msg_callback(msgID C.uint16_t, context C.uint32_t, data unsafe.Pointer, size C.size_t) {
144 // convert unsafe.Pointer to byte slice
145 slice := &reflect.SliceHeader{Data: uintptr(data), Len: int(size), Cap: int(size)}
146 byteArr := *(*[]byte)(unsafe.Pointer(slice))
148 vppClient.callback(uint32(context), uint16(msgID), byteArr)