From 28df7c855784e784fb0e614c1cca0e565a7ef75f Mon Sep 17 00:00:00 2001 From: Ondrej Fabry Date: Mon, 22 Oct 2018 14:45:21 +0200 Subject: Introduce StatsAPI and it's initial implementation - this implementation is basically Go wrapper around VPP's vppapiclient C library Change-Id: I6f53dc3e228868834bf3a8a00c686ad05e22f3dd Signed-off-by: Ondrej Fabry --- adapter/adapter.go | 46 ----- adapter/mock/mock_adapter.go | 6 +- adapter/stats_api.go | 86 +++++++++ adapter/vpp_api.go | 47 +++++ adapter/vppapiclient/doc.go | 18 ++ adapter/vppapiclient/empty_adapter.go | 56 ------ adapter/vppapiclient/stat_client.go | 279 +++++++++++++++++++++++++++ adapter/vppapiclient/stat_client_stub.go | 45 +++++ adapter/vppapiclient/vppapiclient.go | 223 +++++++++++++++++++++ adapter/vppapiclient/vppapiclient_adapter.go | 212 -------------------- adapter/vppapiclient/vppapiclient_stub.go | 53 +++++ 11 files changed, 754 insertions(+), 317 deletions(-) delete mode 100644 adapter/adapter.go create mode 100644 adapter/stats_api.go create mode 100644 adapter/vpp_api.go create mode 100644 adapter/vppapiclient/doc.go delete mode 100644 adapter/vppapiclient/empty_adapter.go create mode 100644 adapter/vppapiclient/stat_client.go create mode 100644 adapter/vppapiclient/stat_client_stub.go create mode 100644 adapter/vppapiclient/vppapiclient.go delete mode 100644 adapter/vppapiclient/vppapiclient_adapter.go create mode 100644 adapter/vppapiclient/vppapiclient_stub.go (limited to 'adapter') diff --git a/adapter/adapter.go b/adapter/adapter.go deleted file mode 100644 index aa34329..0000000 --- a/adapter/adapter.go +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright (c) 2017 Cisco and/or its affiliates. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at: -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package adapter - -import ( - "errors" -) - -// ErrNotImplemented is an error returned when missing implementation. -var ErrNotImplemented = errors.New("not implemented for this OS") - -// MsgCallback defines func signature for message callback. -type MsgCallback func(msgID uint16, data []byte) - -// VppAdapter provides connection to VPP. It is responsible for sending and receiving of binary-encoded messages to/from VPP. -type VppAdapter interface { - // Connect connects the process to VPP. - Connect() error - - // Disconnect disconnects the process from VPP. - Disconnect() - - // GetMsgID returns a runtime message ID for the given message name and CRC. - GetMsgID(msgName string, msgCrc string) (uint16, error) - - // SendMsg sends a binary-encoded message to VPP. - SendMsg(context uint32, data []byte) error - - // SetMsgCallback sets a callback function that will be called by the adapter whenever a message comes from VPP. - SetMsgCallback(cb MsgCallback) - - // WaitReady waits until adapter is ready. - WaitReady() error -} diff --git a/adapter/mock/mock_adapter.go b/adapter/mock/mock_adapter.go index cdf2081..9783651 100644 --- a/adapter/mock/mock_adapter.go +++ b/adapter/mock/mock_adapter.go @@ -37,7 +37,7 @@ const ( useReplyHandlers // use reply handler ) -// VppAdapter represents a mock VPP adapter that can be used for unit/integration testing instead of the vppapiclient adapter. +// VppAPI represents a mock VPP adapter that can be used for unit/integration testing instead of the vppapiclient adapter. type VppAdapter struct { callback adapter.MsgCallback @@ -107,8 +107,8 @@ func (a *VppAdapter) Connect() error { } // Disconnect emulates disconnecting the process from VPP. -func (a *VppAdapter) Disconnect() { - // no op +func (a *VppAdapter) Disconnect() error { + return nil } // GetMsgNameByID returns message name for specified message ID. diff --git a/adapter/stats_api.go b/adapter/stats_api.go new file mode 100644 index 0000000..8815aae --- /dev/null +++ b/adapter/stats_api.go @@ -0,0 +1,86 @@ +package adapter + +// StatsAPI provides connection to VPP stats API. +type StatsAPI interface { + // Connect establishes client connection to the stats API. + Connect() error + + // Disconnect terminates client connection. + Disconnect() error + + // ListStats lists names for all stats. + ListStats(patterns ...string) (statNames []string, err error) + + // DumpStats dumps all stat entries. + DumpStats(patterns ...string) ([]*StatEntry, error) +} + +// StatType represents type of stat directory and simply +// defines what type of stat data is stored in the stat entry. +type StatType int + +const ( + _ StatType = iota + ScalarIndex + SimpleCounterVector + CombinedCounterVector + ErrorIndex +) + +func (d StatType) String() string { + switch d { + case ScalarIndex: + return "ScalarIndex" + case SimpleCounterVector: + return "SimpleCounterVector" + case CombinedCounterVector: + return "CombinedCounterVector" + case ErrorIndex: + return "ErrorIndex" + } + return "UnknownStatType" +} + +// StatEntry represents single stat entry. The type of stat stored in Data +// is defined by Type. +type StatEntry struct { + Name string + Type StatType + Data Stat +} + +// Counter represents simple counter with single value. +type Counter uint64 + +// CombinedCounter represents counter with two values, for packet count and bytes count. +type CombinedCounter struct { + Packets Counter + Bytes Counter +} + +// ScalarStat represents stat for ScalarIndex. +type ScalarStat float64 + +// ErrorStat represents stat for ErrorIndex. +type ErrorStat uint64 + +// SimpleCounterStat represents stat for SimpleCounterVector. +// The outer array represents workers and the inner array represents sw_if_index. +// Values should be aggregated per interface for every worker. +type SimpleCounterStat [][]Counter + +// CombinedCounterStat represents stat for CombinedCounterVector. +// The outer array represents workers and the inner array represents sw_if_index. +// Values should be aggregated per interface for every worker. +type CombinedCounterStat [][]CombinedCounter + +// Data represents some type of stat which is usually defined by StatType. +type Stat interface { + // isStat is unexported to limit implementations of Data interface to this package, + isStat() +} + +func (ScalarStat) isStat() {} +func (ErrorStat) isStat() {} +func (SimpleCounterStat) isStat() {} +func (CombinedCounterStat) isStat() {} diff --git a/adapter/vpp_api.go b/adapter/vpp_api.go new file mode 100644 index 0000000..7d14633 --- /dev/null +++ b/adapter/vpp_api.go @@ -0,0 +1,47 @@ +// Copyright (c) 2017 Cisco and/or its affiliates. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package adapter + +import ( + "errors" +) + +// ErrNotImplemented is an error returned when missing implementation. +var ErrNotImplemented = errors.New("not implemented for this OS") + +// MsgCallback defines func signature for message callback. +type MsgCallback func(msgID uint16, data []byte) + +// VppAPI provides connection to VPP binary API. +// It is responsible for sending and receiving of binary-encoded messages to/from VPP. +type VppAPI interface { + // Connect connects the process to VPP. + Connect() error + + // Disconnect disconnects the process from VPP. + Disconnect() error + + // GetMsgID returns a runtime message ID for the given message name and CRC. + GetMsgID(msgName string, msgCrc string) (msgID uint16, err error) + + // SendMsg sends a binary-encoded message to VPP. + SendMsg(context uint32, data []byte) error + + // SetMsgCallback sets a callback function that will be called by the adapter whenever a message comes from VPP. + SetMsgCallback(cb MsgCallback) + + // WaitReady waits until adapter is ready. + WaitReady() error +} diff --git a/adapter/vppapiclient/doc.go b/adapter/vppapiclient/doc.go new file mode 100644 index 0000000..6505498 --- /dev/null +++ b/adapter/vppapiclient/doc.go @@ -0,0 +1,18 @@ +// Copyright (c) 2018 Cisco and/or its affiliates. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Package vppapiclient is the default VPP adapter being used for +// the connection to VPP binary & stats API via shared memory. +// It is essentially Go wrapper for the VPP vppapiclient library written in C. +package vppapiclient diff --git a/adapter/vppapiclient/empty_adapter.go b/adapter/vppapiclient/empty_adapter.go deleted file mode 100644 index 7514048..0000000 --- a/adapter/vppapiclient/empty_adapter.go +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright (c) 2017 Cisco and/or its affiliates. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at: -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// +build windows darwin - -/* - This is just an empty adapter that does nothing. It builds only on Windows and OSX, where the real - VPP API client adapter does not build. Its sole purpose is to make the compiler happy on Windows and OSX. -*/ - -package vppapiclient - -import ( - "git.fd.io/govpp.git/adapter" -) - -type vppAPIClientAdapter struct{} - -func NewVppAdapter(string) adapter.VppAdapter { - return &vppAPIClientAdapter{} -} - -func (a *vppAPIClientAdapter) Connect() error { - return adapter.ErrNotImplemented -} - -func (a *vppAPIClientAdapter) Disconnect() { - // no op -} - -func (a *vppAPIClientAdapter) GetMsgID(msgName string, msgCrc string) (uint16, error) { - return 0, nil -} - -func (a *vppAPIClientAdapter) SendMsg(clientID uint32, data []byte) error { - return nil -} - -func (a *vppAPIClientAdapter) SetMsgCallback(cb adapter.MsgCallback) { - // no op -} - -func (a *vppAPIClientAdapter) WaitReady() error { - return nil -} diff --git a/adapter/vppapiclient/stat_client.go b/adapter/vppapiclient/stat_client.go new file mode 100644 index 0000000..5f3b932 --- /dev/null +++ b/adapter/vppapiclient/stat_client.go @@ -0,0 +1,279 @@ +// Copyright (c) 2018 Cisco and/or its affiliates. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build !windows,!darwin + +package vppapiclient + +/* +#cgo CFLAGS: -DPNG_DEBUG=1 +#cgo LDFLAGS: -lvppapiclient + +#include +#include +#include +#include +#include +#include + +static int +govpp_stat_connect(char *socket_name) +{ + return stat_segment_connect(socket_name); +} + +static void +govpp_stat_disconnect() +{ + stat_segment_disconnect(); +} + +static uint32_t* +govpp_stat_segment_ls(uint8_t ** pattern) +{ + return stat_segment_ls(pattern); +} + +static int +govpp_stat_segment_vec_len(void *vec) +{ + return stat_segment_vec_len(vec); +} + +static void +govpp_stat_segment_vec_free(void *vec) +{ + stat_segment_vec_free(vec); +} + +static char* +govpp_stat_segment_dir_index_to_name(uint32_t *dir, uint32_t index) +{ + return stat_segment_index_to_name(dir[index]); +} + +static stat_segment_data_t* +govpp_stat_segment_dump(uint32_t *counter_vec) +{ + return stat_segment_dump(counter_vec); +} + +static stat_segment_data_t +govpp_stat_segment_dump_index(stat_segment_data_t *data, int index) +{ + return data[index]; +} + +static int +govpp_stat_segment_data_type(stat_segment_data_t *data) +{ + return data->type; +} + +static double +govpp_stat_segment_data_get_scalar_value(stat_segment_data_t *data) +{ + return data->scalar_value; +} + +static double +govpp_stat_segment_data_get_error_value(stat_segment_data_t *data) +{ + return data->error_value; +} + +static uint64_t** +govpp_stat_segment_data_get_simple_counter(stat_segment_data_t *data) +{ + return data->simple_counter_vec; +} + +static uint64_t* +govpp_stat_segment_data_get_simple_counter_index(stat_segment_data_t *data, int index) +{ + return data->simple_counter_vec[index]; +} + +static uint64_t +govpp_stat_segment_data_get_simple_counter_index_value(stat_segment_data_t *data, int index, int index2) +{ + return data->simple_counter_vec[index][index2]; +} + +static vlib_counter_t** +govpp_stat_segment_data_get_combined_counter(stat_segment_data_t *data) +{ + return data->combined_counter_vec; +} + +static vlib_counter_t* +govpp_stat_segment_data_get_combined_counter_index(stat_segment_data_t *data, int index) +{ + return data->combined_counter_vec[index]; +} + +static uint64_t +govpp_stat_segment_data_get_combined_counter_index_packets(stat_segment_data_t *data, int index, int index2) +{ + return data->combined_counter_vec[index][index2].packets; +} + +static uint64_t +govpp_stat_segment_data_get_combined_counter_index_bytes(stat_segment_data_t *data, int index, int index2) +{ + return data->combined_counter_vec[index][index2].bytes; +} + +static void +govpp_stat_segment_data_free(stat_segment_data_t *data) +{ + stat_segment_data_free(data); +} + +static uint8_t** +govpp_stat_segment_string_vector(uint8_t ** string_vector, char *string) +{ + return stat_segment_string_vector(string_vector, string); +} +*/ +import "C" +import ( + "fmt" + "os" + "unsafe" + + "git.fd.io/govpp.git/adapter" +) + +var ( + // DefaultStatSocket is the default path for the VPP stat socket file. + DefaultStatSocket = "/run/vpp/stats.sock" +) + +// StatClient is the default implementation of StatsAPI. +type StatClient struct { + socketName string +} + +// NewStatClient returns new VPP stats API client. +func NewStatClient(socketName string) *StatClient { + return &StatClient{ + socketName: socketName, + } +} + +func (c *StatClient) Connect() error { + var sockName string + + if c.socketName == "" { + sockName = DefaultStatSocket + } else { + sockName = c.socketName + } + + rc := C.govpp_stat_connect(C.CString(sockName)) + if rc != 0 { + return fmt.Errorf("connecting to VPP stats API failed (rc=%v)", rc) + } + + return nil +} + +func (c *StatClient) Disconnect() error { + C.govpp_stat_disconnect() + return nil +} + +func (c *StatClient) ListStats(patterns ...string) (stats []string, err error) { + dir := C.govpp_stat_segment_ls(convertStringSlice(patterns)) + defer C.govpp_stat_segment_vec_free(unsafe.Pointer(dir)) + + l := C.govpp_stat_segment_vec_len(unsafe.Pointer(dir)) + for i := 0; i < int(l); i++ { + nameChar := C.govpp_stat_segment_dir_index_to_name(dir, C.uint32_t(i)) + stats = append(stats, C.GoString(nameChar)) + C.free(unsafe.Pointer(nameChar)) + } + + return stats, nil +} + +func (c *StatClient) DumpStats(patterns ...string) (stats []*adapter.StatEntry, err error) { + dir := C.govpp_stat_segment_ls(convertStringSlice(patterns)) + defer C.govpp_stat_segment_vec_free(unsafe.Pointer(dir)) + + dump := C.govpp_stat_segment_dump(dir) + defer C.govpp_stat_segment_data_free(dump) + + l := C.govpp_stat_segment_vec_len(unsafe.Pointer(dump)) + for i := 0; i < int(l); i++ { + v := C.govpp_stat_segment_dump_index(dump, C.int(i)) + nameChar := v.name + name := C.GoString(nameChar) + typ := adapter.StatType(C.govpp_stat_segment_data_type(&v)) + + stat := &adapter.StatEntry{ + Name: name, + Type: typ, + } + + switch typ { + case adapter.ScalarIndex: + stat.Data = adapter.ScalarStat(C.govpp_stat_segment_data_get_scalar_value(&v)) + + case adapter.ErrorIndex: + stat.Data = adapter.ErrorStat(C.govpp_stat_segment_data_get_error_value(&v)) + + case adapter.SimpleCounterVector: + length := int(C.govpp_stat_segment_vec_len(unsafe.Pointer(C.govpp_stat_segment_data_get_simple_counter(&v)))) + vector := make([][]adapter.Counter, length) + for k := 0; k < length; k++ { + for j := 0; j < int(C.govpp_stat_segment_vec_len(unsafe.Pointer(C.govpp_stat_segment_data_get_simple_counter_index(&v, _Ctype_int(k))))); j++ { + vector[k] = append(vector[k], adapter.Counter(C.govpp_stat_segment_data_get_simple_counter_index_value(&v, _Ctype_int(k), _Ctype_int(j)))) + } + } + stat.Data = adapter.SimpleCounterStat(vector) + + case adapter.CombinedCounterVector: + length := int(C.govpp_stat_segment_vec_len(unsafe.Pointer(C.govpp_stat_segment_data_get_combined_counter(&v)))) + vector := make([][]adapter.CombinedCounter, length) + for k := 0; k < length; k++ { + for j := 0; j < int(C.govpp_stat_segment_vec_len(unsafe.Pointer(C.govpp_stat_segment_data_get_combined_counter_index(&v, _Ctype_int(k))))); j++ { + vector[k] = append(vector[k], adapter.CombinedCounter{ + Packets: adapter.Counter(C.govpp_stat_segment_data_get_combined_counter_index_packets(&v, _Ctype_int(k), _Ctype_int(j))), + Bytes: adapter.Counter(C.govpp_stat_segment_data_get_combined_counter_index_bytes(&v, _Ctype_int(k), _Ctype_int(j))), + }) + } + } + stat.Data = adapter.CombinedCounterStat(vector) + + default: + fmt.Fprintf(os.Stderr, "invalid stat type: %v (%d)", typ, typ) + continue + + } + + stats = append(stats, stat) + } + + return stats, nil +} + +func convertStringSlice(strs []string) **C.uint8_t { + var arr **C.uint8_t + for _, str := range strs { + arr = C.govpp_stat_segment_string_vector(arr, C.CString(str)) + } + return arr +} diff --git a/adapter/vppapiclient/stat_client_stub.go b/adapter/vppapiclient/stat_client_stub.go new file mode 100644 index 0000000..24f7e13 --- /dev/null +++ b/adapter/vppapiclient/stat_client_stub.go @@ -0,0 +1,45 @@ +// Copyright (c) 2018 Cisco and/or its affiliates. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build windows darwin + +package vppapiclient + +import ( + "git.fd.io/govpp.git/adapter" +) + +// StatClient is just an stub adapter that does nothing. It builds only on Windows and OSX, where the real +// VPP stats API client adapter does not build. Its sole purpose is to make the compiler happy on Windows and OSX. +type StatClient struct{} + +func NewStatClient(socketName string) *StatClient { + return new(StatClient) +} + +func (*StatClient) Connect() error { + return adapter.ErrNotImplemented +} + +func (*StatClient) Disconnect() error { + return nil +} + +func (*StatClient) ListStats(patterns ...string) (statNames []string, err error) { + return nil, adapter.ErrNotImplemented +} + +func (*StatClient) DumpStats(patterns ...string) ([]*adapter.StatEntry, error) { + return nil, adapter.ErrNotImplemented +} diff --git a/adapter/vppapiclient/vppapiclient.go b/adapter/vppapiclient/vppapiclient.go new file mode 100644 index 0000000..34ad199 --- /dev/null +++ b/adapter/vppapiclient/vppapiclient.go @@ -0,0 +1,223 @@ +// Copyright (c) 2017 Cisco and/or its affiliates. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build !windows,!darwin + +package vppapiclient + +/* +#cgo CFLAGS: -DPNG_DEBUG=1 +#cgo LDFLAGS: -lvppapiclient + +#include +#include +#include +#include +#include + +extern void go_msg_callback(uint16_t msg_id, void* data, size_t size); + +typedef struct __attribute__((__packed__)) _req_header { + uint16_t msg_id; + uint32_t client_index; + uint32_t context; +} req_header_t; + +typedef struct __attribute__((__packed__)) _reply_header { + uint16_t msg_id; +} reply_header_t; + +static void +govpp_msg_callback(unsigned char *data, int size) +{ + reply_header_t *header = ((reply_header_t *)data); + go_msg_callback(ntohs(header->msg_id), data, size); +} + +static int +govpp_send(uint32_t context, void *data, size_t size) +{ + req_header_t *header = ((req_header_t *)data); + header->context = htonl(context); + return vac_write(data, size); +} + +static int +govpp_connect(char *shm) +{ + return vac_connect("govpp", shm, govpp_msg_callback, 32); +} + +static int +govpp_disconnect() +{ + return vac_disconnect(); +} + +static uint32_t +govpp_get_msg_index(char *name_and_crc) +{ + return vac_get_msg_index(name_and_crc); +} +*/ +import "C" + +import ( + "fmt" + "os" + "path/filepath" + "reflect" + "unsafe" + + "git.fd.io/govpp.git/adapter" + "github.com/fsnotify/fsnotify" +) + +const ( + // shmDir is a directory where shared memory is supposed to be created. + shmDir = "/dev/shm/" + // vppShmFile is a default name of the file in the shmDir. + vppShmFile = "vpe-api" +) + +// global VPP binary API client adapter context +var client *VppClient + +// VppClient is the default implementation of the VppAPI. +type VppClient struct { + shmPrefix string + msgCallback adapter.MsgCallback +} + +// NewVppClient returns a new VPP binary API client. +func NewVppClient(shmPrefix string) *VppClient { + return &VppClient{ + shmPrefix: shmPrefix, + } +} + +// Connect connects the process to VPP. +func (a *VppClient) Connect() error { + if client != nil { + return fmt.Errorf("already connected to binary API, disconnect first") + } + + var rc _Ctype_int + if a.shmPrefix == "" { + rc = C.govpp_connect(nil) + } else { + shm := C.CString(a.shmPrefix) + rc = C.govpp_connect(shm) + } + if rc != 0 { + return fmt.Errorf("connecting to VPP binary API failed (rc=%v)", rc) + } + + client = a + return nil +} + +// Disconnect disconnects the process from VPP. +func (a *VppClient) Disconnect() error { + client = nil + + rc := C.govpp_disconnect() + if rc != 0 { + return fmt.Errorf("disconnecting from VPP binary API failed (rc=%v)", rc) + } + + return nil +} + +// GetMsgID returns a runtime message ID for the given message name and CRC. +func (a *VppClient) GetMsgID(msgName string, msgCrc string) (uint16, error) { + nameAndCrc := C.CString(msgName + "_" + msgCrc) + defer C.free(unsafe.Pointer(nameAndCrc)) + + msgID := uint16(C.govpp_get_msg_index(nameAndCrc)) + if msgID == ^uint16(0) { + // VPP does not know this message + return msgID, fmt.Errorf("unknown message: %v (crc: %v)", msgName, msgCrc) + } + + return msgID, nil +} + +// SendMsg sends a binary-encoded message to VPP. +func (a *VppClient) SendMsg(context uint32, data []byte) error { + rc := C.govpp_send(C.uint32_t(context), unsafe.Pointer(&data[0]), C.size_t(len(data))) + if rc != 0 { + return fmt.Errorf("unable to send the message (rc=%v)", rc) + } + return nil +} + +// SetMsgCallback sets a callback function that will be called by the adapter +// whenever a message comes from VPP. +func (a *VppClient) SetMsgCallback(cb adapter.MsgCallback) { + a.msgCallback = cb +} + +// WaitReady blocks until shared memory for sending +// binary api calls is present on the file system. +func (a *VppClient) WaitReady() error { + var path string + + // join the path to the shared memory segment + if a.shmPrefix == "" { + path = filepath.Join(shmDir, vppShmFile) + } else { + path = filepath.Join(shmDir, a.shmPrefix+"-"+vppShmFile) + } + + // check if file at the path exists + if _, err := os.Stat(path); err == nil { + // file exists, we are ready + return nil + } else if !os.IsNotExist(err) { + return err + } + + // file does not exist, start watching folder + watcher, err := fsnotify.NewWatcher() + if err != nil { + return err + } + defer watcher.Close() + + if err := watcher.Add(shmDir); err != nil { + return err + } + + for { + ev := <-watcher.Events + if ev.Name == path { + if (ev.Op & fsnotify.Create) == fsnotify.Create { + // file was created, we are ready + break + } + } + } + + return nil +} + +//export go_msg_callback +func go_msg_callback(msgID C.uint16_t, data unsafe.Pointer, size C.size_t) { + // convert unsafe.Pointer to byte slice + sliceHeader := &reflect.SliceHeader{Data: uintptr(data), Len: int(size), Cap: int(size)} + byteSlice := *(*[]byte)(unsafe.Pointer(sliceHeader)) + + client.msgCallback(uint16(msgID), byteSlice) +} diff --git a/adapter/vppapiclient/vppapiclient_adapter.go b/adapter/vppapiclient/vppapiclient_adapter.go deleted file mode 100644 index e62bccd..0000000 --- a/adapter/vppapiclient/vppapiclient_adapter.go +++ /dev/null @@ -1,212 +0,0 @@ -// Copyright (c) 2017 Cisco and/or its affiliates. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at: -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// +build !windows,!darwin - -// Package vppapiclient is the default VPP adapter being used for the connection with VPP via shared memory. -// It is based on the communication with the vppapiclient VPP library written in C via CGO. -package vppapiclient - -/* -#cgo CFLAGS: -DPNG_DEBUG=1 -#cgo LDFLAGS: -lvppapiclient - -#include -#include -#include -#include -#include - -extern void go_msg_callback(uint16_t msg_id, void* data, size_t size); - -typedef struct __attribute__((__packed__)) _req_header { - uint16_t msg_id; - uint32_t client_index; - uint32_t context; -} req_header_t; - -typedef struct __attribute__((__packed__)) _reply_header { - uint16_t msg_id; -} reply_header_t; - -static void -govpp_msg_callback (unsigned char *data, int size) -{ - reply_header_t *header = ((reply_header_t *)data); - go_msg_callback(ntohs(header->msg_id), data, size); -} - -static int -govpp_send(uint32_t context, void *data, size_t size) -{ - req_header_t *header = ((req_header_t *)data); - header->context = htonl(context); - return vac_write(data, size); -} - -static int -govpp_connect (char *shm) -{ - return vac_connect("govpp", shm, govpp_msg_callback, 32); -} - -static int -govvp_disconnect() -{ - return vac_disconnect(); -} - -static uint32_t -govpp_get_msg_index(char *name_and_crc) -{ - return vac_get_msg_index(name_and_crc); -} -*/ -import "C" - -import ( - "fmt" - "os" - "path/filepath" - "reflect" - "unsafe" - - "git.fd.io/govpp.git/adapter" - "github.com/fsnotify/fsnotify" -) - -const ( - // watchedFolder is a folder where vpp's shared memory is supposed to be created. - // File system events are monitored in this folder. - watchedFolder = "/dev/shm/" - // watchedFile is a default name of the file in the watchedFolder. Once the file is present, - // the vpp is ready to accept a new connection. - watchedFile = "vpe-api" -) - -// vppAPIClientAdapter is the opaque context of the adapter. -type vppAPIClientAdapter struct { - shmPrefix string - callback adapter.MsgCallback -} - -var vppClient *vppAPIClientAdapter // global vpp API client adapter context - -// NewVppAdapter returns a new vpp API client adapter. -func NewVppAdapter(shmPrefix string) adapter.VppAdapter { - return &vppAPIClientAdapter{ - shmPrefix: shmPrefix, - } -} - -// Connect connects the process to VPP. -func (a *vppAPIClientAdapter) Connect() error { - vppClient = a - var rc _Ctype_int - if a.shmPrefix == "" { - rc = C.govpp_connect(nil) - } else { - shm := C.CString(a.shmPrefix) - rc = C.govpp_connect(shm) - } - if rc != 0 { - return fmt.Errorf("unable to connect to VPP (error=%d)", rc) - } - return nil -} - -// Disconnect disconnects the process from VPP. -func (a *vppAPIClientAdapter) Disconnect() { - C.govvp_disconnect() -} - -// GetMsgID returns a runtime message ID for the given message name and CRC. -func (a *vppAPIClientAdapter) GetMsgID(msgName string, msgCrc string) (uint16, error) { - nameAndCrc := C.CString(msgName + "_" + msgCrc) - defer C.free(unsafe.Pointer(nameAndCrc)) - - msgID := uint16(C.govpp_get_msg_index(nameAndCrc)) - if msgID == ^uint16(0) { - // VPP does not know this message - return msgID, fmt.Errorf("unknown message: %v (crc: %v)", msgName, msgCrc) - } - - return msgID, nil -} - -// SendMsg sends a binary-encoded message to VPP. -func (a *vppAPIClientAdapter) SendMsg(context uint32, data []byte) error { - rc := C.govpp_send(C.uint32_t(context), unsafe.Pointer(&data[0]), C.size_t(len(data))) - if rc != 0 { - return fmt.Errorf("unable to send the message (error=%d)", rc) - } - return nil -} - -// SetMsgCallback sets a callback function that will be called by the adapter whenever a message comes from VPP. -func (a *vppAPIClientAdapter) SetMsgCallback(cb adapter.MsgCallback) { - a.callback = cb -} - -// WaitReady blocks until shared memory for sending -// binary api calls is present on the file system. -func (a *vppAPIClientAdapter) WaitReady() error { - // Path to the shared memory segment - var path string - if a.shmPrefix == "" { - path = filepath.Join(watchedFolder, watchedFile) - } else { - path = filepath.Join(watchedFolder, a.shmPrefix+"-"+watchedFile) - } - - // Watch folder if file does not exist yet - if !fileExists(path) { - watcher, err := fsnotify.NewWatcher() - if err != nil { - return err - } - defer watcher.Close() - - if err := watcher.Add(watchedFolder); err != nil { - return err - } - - for { - ev := <-watcher.Events - if ev.Name == path && (ev.Op&fsnotify.Create) == fsnotify.Create { - break - } - } - } - - return nil -} - -func fileExists(name string) bool { - if _, err := os.Stat(name); err != nil { - if os.IsNotExist(err) { - return false - } - } - return true -} - -//export go_msg_callback -func go_msg_callback(msgID C.uint16_t, data unsafe.Pointer, size C.size_t) { - // convert unsafe.Pointer to byte slice - slice := &reflect.SliceHeader{Data: uintptr(data), Len: int(size), Cap: int(size)} - byteArr := *(*[]byte)(unsafe.Pointer(slice)) - - vppClient.callback(uint16(msgID), byteArr) -} diff --git a/adapter/vppapiclient/vppapiclient_stub.go b/adapter/vppapiclient/vppapiclient_stub.go new file mode 100644 index 0000000..6de89a7 --- /dev/null +++ b/adapter/vppapiclient/vppapiclient_stub.go @@ -0,0 +1,53 @@ +// Copyright (c) 2017 Cisco and/or its affiliates. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build windows darwin + +package vppapiclient + +import ( + "git.fd.io/govpp.git/adapter" +) + +// VppClient is just an stub adapter that does nothing. It builds only on Windows and OSX, where the real +// VPP binary API client adapter does not build. Its sole purpose is to make the compiler happy on Windows and OSX. +type VppClient struct{} + +func NewVppAdapter(string) *VppClient { + return &VppClient{} +} + +func (a *VppClient) Connect() error { + return adapter.ErrNotImplemented +} + +func (a *VppClient) Disconnect() error { + return nil +} + +func (a *VppClient) GetMsgID(msgName string, msgCrc string) (uint16, error) { + return 0, nil +} + +func (a *VppClient) SendMsg(clientID uint32, data []byte) error { + return nil +} + +func (a *VppClient) SetMsgCallback(cb adapter.MsgCallback) { + // no op +} + +func (a *VppClient) WaitReady() error { + return adapter.ErrNotImplemented +} -- cgit 1.2.3-korg