libmemif for Golang is build on the top of the original, C-written
libmemif library using `cgo`. It is therefore necessary to have C-libmemif
-header files and the library itself installed in locations known
+header files, and the library itself installed in locations known
to the compiler.
For example, to install C-libmemif system-wide into the standard
```
$ git clone https://gerrit.fd.io/r/vpp
$ cd vpp/extras/libmemif
-$ ./bootstrap
-$ ./configure
-$ make install
+$ mkdir build
+$ cd build
+$ cmake ..
+$ sudo make install
```
### Build
**libmemif** was designed for a maximum possible performance. Packets
are sent and received in bulks, rather than one-by-one, using
`Memif.TxBurst(queueID, packets)` and `Memif.RxBurst(queueID, count)`,
-respectively. Memif connection can consists of multiple queues in both
+respectively. Memif connection can consist of multiple queues in both
directions. A queue is one-directional wait-free ring buffer.
It is the unit of parallelism for data transmission. The maximum possible
lock-free granularity is therefore one go routine for one queue.
*raw-data* is a basic example showing how to create a memif interface,
handle events through callbacks and perform Rx/Tx of raw data. Before
-handling an actual packets it is important to understand the skeleton
+handling an actual packet it is important to understand the skeleton
of libmemif-based applications.
Since VPP expects proper packet data, it is not very useful to connect
// are much easier to work with in cgo.
typedef struct
{
- char *socket_filename;
+ memif_socket_handle_t socket;
char *secret;
uint8_t num_s2m_rings;
uint8_t num_m2s_rings;
{
memif_conn_args_t args;
memset (&args, 0, sizeof (args));
- args.socket_filename = (char *)go_args->socket_filename;
+ args.socket = (char *)go_args->socket;
if (go_args->secret != NULL)
{
strncpy ((char *)args.secret, go_args->secret,
private_ctx);
}
+static int
+govpp_memif_create_socket (memif_socket_handle_t *sock, char *filename)
+{
+ return memif_create_socket(sock, filename, NULL);
+}
+
// govpp_memif_get_details keeps reallocating buffer until it is large enough.
// The buffer is returned to be deallocated when it is no longer needed.
static int
MemifMeta
// Per-library references
- ifIndex int // index used in the Go-libmemif context (Context.memifs)
- cHandle C.memif_conn_handle_t // handle used in C-libmemif
+ ifIndex int // index used in the Go-libmemif context (Context.memifs)
+ cHandle C.memif_conn_handle_t // connection handle used in C-libmemif
+ sHandle C.memif_socket_handle_t // socket handle used in C-libmemif
// Callbacks
callbacks *MemifCallbacks
args := &C.govpp_memif_conn_args_t{}
// - socket file name
if config.SocketFilename != "" {
- args.socket_filename = C.CString(config.SocketFilename)
- defer C.free(unsafe.Pointer(args.socket_filename))
+ log.WithField("name", config.SocketFilename).Debug("A new memif socket was created")
+ errCode := C.govpp_memif_create_socket(&memif.sHandle, C.CString(config.SocketFilename))
+ if getMemifError(int(errCode)) != nil {
+ return nil, err
+ }
}
+ args.socket = memif.sHandle
// - interface ID
args.interface_id = C.uint32_t(config.ConnID)
// - interface name
// Create memif in C-libmemif.
errCode := C.govpp_memif_create(&memif.cHandle, args, unsafe.Pointer(uintptr(memif.ifIndex)))
- err = getMemifError(int(errCode))
- if err != nil {
+ if getMemifError(int(errCode)) != nil {
return nil, err
}
}
var bufCount int
- var buffers []*txPacketBuffer
+ buffers := make([]*txPacketBuffer, 0)
cQueueID := C.uint16_t(queueID)
for _, packet := range packets {
// Rx queue.
func (memif *Memif) RxBurst(queueID uint8, count uint16) (packets []RawPacketData, err error) {
var recvCount C.uint16_t
+ packets = make([]RawPacketData, 0)
if count == 0 {
return packets, nil
import (
"errors"
"fmt"
+ "git.fd.io/govpp.git/extras/libmemif"
+ "github.com/google/gopacket"
+ "github.com/google/gopacket/layers"
"io"
"net"
"os"
"os/signal"
- "github.com/google/gopacket"
- "github.com/google/gopacket/layers"
- "git.fd.io/govpp.git/extras/libmemif"
)
var (
break
}
// Generate response for each supported request.
- responses := []libmemif.RawPacketData{}
+ var responses []libmemif.RawPacketData
for _, packet := range packets {
fmt.Println("Received new packet:")
DumpPacket(packet)
hwAddr, err = net.ParseMAC(MAC)
if err != nil {
- fmt.Println("Failed to parse the MAC address: %v", err)
+ fmt.Printf("Failed to parse the MAC address: %v", err)
return
}
ipAddr = ip.To4()
}
if ipAddr == nil {
- fmt.Println("Failed to parse the IP address: %v", err)
+ fmt.Printf("Failed to parse the IP address: %v", err)
return
}
import (
"github.com/google/gopacket"
- "time"
- "sync"
"io"
+ "sync"
+ "time"
)
type memoizedPacket struct {
}
}
-// Reads packet data from memif in bursts, based on previously configured rxCount parameterer. Then caches the
+// Reads packet data from memif in bursts, based on previously configured rxCount parameter. Then caches the
// resulting packets and returns them 1 by 1 from this method until queue is empty then tries to call new rx burst
// to read more data. If no data is returned, io.EOF error is thrown and caller should stop reading.
func (handle *MemifPacketHandle) ReadPacketData() (data []byte, ci gopacket.CaptureInfo, err error) {