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
26 logger "github.com/sirupsen/logrus"
36 #include <sys/eventfd.h>
37 #include <libmemif.h> // <-- VPP must be installed!
40 #ifndef MEMIF_HAVE_CANCEL_POLL_EVENT
41 // memif_cancel_poll_event that simply returns ErrUnsupported.
43 memif_cancel_poll_event ()
45 return 102; // ErrUnsupported
49 // govpp_memif_conn_args_t replaces fixed sized arrays with C-strings which
50 // are much easier to work with in cgo.
53 char *socket_filename;
55 uint8_t num_s2m_rings;
56 uint8_t num_m2s_rings;
58 uint8_t log2_ring_size;
60 uint32_t interface_id;
62 memif_interface_mode_t mode;
63 } govpp_memif_conn_args_t;
65 // govpp_memif_details_t replaces strings represented with (uint8_t *)
66 // to the standard and easy to work with in cgo: (char *)
72 char *remote_inst_name;
77 char *socket_filename;
79 memif_region_details_t *regions;
80 uint8_t rx_queues_num;
81 uint8_t tx_queues_num;
82 memif_queue_details_t *rx_queues;
83 memif_queue_details_t *tx_queues;
85 } govpp_memif_details_t;
87 extern int go_on_connect_callback(void *privateCtx);
88 extern int go_on_disconnect_callback(void *privateCtx);
90 // Callbacks strip the connection handle away.
93 govpp_on_connect_callback(memif_conn_handle_t conn, void *private_ctx)
95 return go_on_connect_callback(private_ctx);
99 govpp_on_disconnect_callback(memif_conn_handle_t conn, void *private_ctx)
101 return go_on_disconnect_callback(private_ctx);
104 // govpp_memif_create uses govpp_memif_conn_args_t.
106 govpp_memif_create (memif_conn_handle_t *conn, govpp_memif_conn_args_t *go_args,
109 memif_conn_args_t args;
110 memset (&args, 0, sizeof (args));
111 args.socket_filename = (char *)go_args->socket_filename;
112 if (go_args->secret != NULL)
114 strncpy ((char *)args.secret, go_args->secret,
115 sizeof (args.secret) - 1);
117 args.num_s2m_rings = go_args->num_s2m_rings;
118 args.num_m2s_rings = go_args->num_m2s_rings;
119 args.buffer_size = go_args->buffer_size;
120 args.log2_ring_size = go_args->log2_ring_size;
121 args.is_master = go_args->is_master;
122 args.interface_id = go_args->interface_id;
123 if (go_args->interface_name != NULL)
125 strncpy ((char *)args.interface_name, go_args->interface_name,
126 sizeof(args.interface_name) - 1);
128 args.mode = go_args->mode;
130 return memif_create(conn, &args, govpp_on_connect_callback,
131 govpp_on_disconnect_callback, NULL,
135 // govpp_memif_get_details keeps reallocating buffer until it is large enough.
136 // The buffer is returned to be deallocated when it is no longer needed.
138 govpp_memif_get_details (memif_conn_handle_t conn, govpp_memif_details_t *govpp_md,
142 size_t buflen = 1 << 7;
143 char *buffer = NULL, *new_buffer = NULL;
144 memif_details_t md = {0};
147 // initial malloc (256 bytes) or realloc
149 new_buffer = realloc(buffer, buflen);
150 if (new_buffer == NULL)
153 return MEMIF_ERR_NOMEM;
156 // try to get details
157 rv = memif_get_details(conn, &md, buffer, buflen);
158 } while (rv == MEMIF_ERR_NOBUF_DET);
163 govpp_md->if_name = (char *)md.if_name;
164 govpp_md->inst_name = (char *)md.inst_name;
165 govpp_md->remote_if_name = (char *)md.remote_if_name;
166 govpp_md->remote_inst_name = (char *)md.remote_inst_name;
167 govpp_md->id = md.id;
168 govpp_md->secret = (char *)md.secret;
169 govpp_md->role = md.role;
170 govpp_md->mode = md.mode;
171 govpp_md->socket_filename = (char *)md.socket_filename;
172 govpp_md->regions_num = md.regions_num;
173 govpp_md->regions = md.regions;
174 govpp_md->rx_queues_num = md.rx_queues_num;
175 govpp_md->tx_queues_num = md.tx_queues_num;
176 govpp_md->rx_queues = md.rx_queues;
177 govpp_md->tx_queues = md.tx_queues;
178 govpp_md->link_up_down = md.link_up_down;
185 // Used to avoid cumbersome tricks that use unsafe.Pointer() + unsafe.Sizeof()
186 // or even cast C-array directly into Go-slice.
187 static memif_queue_details_t
188 govpp_get_rx_queue_details (govpp_memif_details_t *md, int index)
190 return md->rx_queues[index];
193 // Used to avoid cumbersome tricks that use unsafe.Pointer() + unsafe.Sizeof()
194 // or even cast C-array directly into Go-slice.
195 static memif_queue_details_t
196 govpp_get_tx_queue_details (govpp_memif_details_t *md, int index)
198 return md->tx_queues[index];
201 // Copy packet data into the selected buffer with splitting when necessary
203 govpp_copy_packet_data(memif_buffer_t *buffers, uint16_t allocated, int bufIndex, void *packetData, uint16_t packetSize)
208 buffers[bufIndex].len = (packetSize > buffers[bufIndex].len ? buffers[bufIndex].len : packetSize);
209 void * curData = (packetData + dataOffset);
210 memcpy(buffers[bufIndex].data, curData, (size_t)buffers[bufIndex].len);
211 dataOffset += buffers[bufIndex].len;
213 packetSize -= buffers[bufIndex].len;
214 } while(packetSize > 0 && bufIndex < allocated && buffers[bufIndex].flags & MEMIF_BUFFER_FLAG_NEXT > 0);
217 // Get packet data from the selected buffer.
218 // Used to avoid an ugly unsafe.Pointer() + unsafe.Sizeof().
220 govpp_get_packet_data(memif_buffer_t *buffers, int index, int *size)
222 *size = (int)buffers[index].len;
223 return buffers[index].data;
226 // Checks if memif buffer is chained
228 govpp_is_buffer_chained(memif_buffer_t *buffers, int index)
230 return buffers[index].flags & MEMIF_BUFFER_FLAG_NEXT;
233 // Allocate memif buffers and return pointer to next free buffer
235 govpp_memif_buffer_alloc(memif_conn_handle_t conn, uint16_t qid,
236 memif_buffer_t * bufs, uint16_t offset, memif_buffer_t ** nextFreeBuf,
237 uint16_t count, uint16_t * count_out, uint16_t size)
239 memif_buffer_t * offsetBufs = (bufs + offset);
240 int err = memif_buffer_alloc(conn, qid, offsetBufs, count, count_out, size);
241 *count_out += offset;
242 *nextFreeBuf = offsetBufs;
249 // IfMode represents the mode (layer/behaviour) in which the interface operates.
253 // IfModeEthernet tells memif to operate on the L2 layer.
254 IfModeEthernet IfMode = iota
256 // IfModeIP tells memif to operate on the L3 layer.
259 // IfModePuntInject tells memif to behave as Inject/Punt interface.
263 // RxMode is used to switch between polling and interrupt for RX.
267 // RxModeInterrupt tells libmemif to send interrupt signal when data are available.
268 RxModeInterrupt RxMode = iota
270 // RxModePolling means that the user needs to explicitly poll for data on RX
275 // RawPacketData represents raw packet data. libmemif doesn't care what the
276 // actual content is, it only manipulates with raw bytes.
277 type RawPacketData []byte
279 // MemifMeta is used to store a basic memif metadata needed for identification
280 // and connection establishment.
281 type MemifMeta struct {
282 // IfName is the interface name. Has to be unique across all created memifs.
283 // Interface name is truncated if needed to have no more than 32 characters.
286 // InstanceName identifies the endpoint. If omitted, the application
287 // name passed to Init() will be used instead.
288 // Instance name is truncated if needed to have no more than 32 characters.
291 // ConnID is a connection ID used to match opposite sides of the memif
295 // SocketFilename is the filename of the AF_UNIX socket through which
296 // the connection is established.
297 // The string is truncated if neede to fit into sockaddr_un.sun_path
298 // (108 characters on Linux).
299 SocketFilename string
301 // Secret must be the same on both sides for the authentication to succeed.
302 // Empty string is allowed.
303 // The secret is truncated if needed to have no more than 24 characters.
306 // IsMaster is set to true if memif operates in the Master mode.
309 // Mode is the mode (layer/behaviour) in which the memif operates.
313 // MemifShmSpecs is used to store the specification of the shared memory segment
314 // used by memif to send/receive packets.
315 type MemifShmSpecs struct {
316 // NumRxQueues is the number of Rx queues.
317 // Default is 1 (used if the value is 0).
320 // NumTxQueues is the number of Tx queues.
321 // Default is 1 (used if the value is 0).
324 // BufferSize is the size of the buffer to hold one packet, or a single
325 // fragment of a jumbo frame. Default is 2048 (used if the value is 0).
328 // Log2RingSize is the number of items in the ring represented through
329 // the logarithm base 2.
330 // Default is 10 (used if the value is 0).
334 // MemifConfig is the memif configuration.
335 // Used as the input argument to CreateInterface().
336 // It is the slave's config that mostly decides the parameters of the connection,
337 // but master may limit some of the quantities if needed (based on the memif
338 // protocol or master's configuration)
339 type MemifConfig struct {
344 // ConnUpdateCallback is a callback type declaration used with callbacks
345 // related to connection status changes.
346 type ConnUpdateCallback func(memif *Memif) (err error)
348 // MemifCallbacks is a container for all callbacks provided by memif.
349 // Any callback can be nil, in which case it will be simply skipped.
350 // Important: Do not call CreateInterface() or Memif.Close() from within a callback
351 // or a deadlock will occur. Instead send signal through a channel to another
352 // go routine which will be able to create/remove memif interface(s).
353 type MemifCallbacks struct {
354 // OnConnect is triggered when a connection for a given memif was established.
355 OnConnect ConnUpdateCallback
357 // OnDisconnect is triggered when a connection for a given memif was lost.
358 OnDisconnect ConnUpdateCallback
361 // Memif represents a single memif interface. It provides methods to send/receive
362 // packets in bursts in either the polling mode or in the interrupt mode with
363 // the help of golang channels.
367 // Per-library references
368 ifIndex int // index used in the Go-libmemif context (Context.memifs)
369 cHandle C.memif_conn_handle_t // handle used in C-libmemif
372 callbacks *MemifCallbacks
375 intCh chan uint8 // memif-global interrupt channel (value = queue ID)
376 queueIntCh []chan struct{} // per RX queue interrupt channel
379 ringSize int // number of items in each ring
380 bufferSize int // max buffer size
381 stopQPollFd int // event file descriptor used to stop pollRxQueue-s
382 wg sync.WaitGroup // wait group for all pollRxQueue-s
383 rxQueueBufs []CPacketBuffers // an array of C-libmemif packet buffers for each RX queue
384 txQueueBufs []CPacketBuffers // an array of C-libmemif packet buffers for each TX queue
387 // MemifDetails provides a detailed runtime information about a memif interface.
388 type MemifDetails struct {
393 // MemifConnDetails provides a detailed runtime information about a memif
395 type MemifConnDetails struct {
396 // RemoteIfName is the name of the memif on the opposite side.
398 // RemoteInstanceName is the name of the endpoint on the opposite side.
399 RemoteInstanceName string
400 // HasLink is true if the connection has link (= is established and functional).
402 // RxQueues contains details for each Rx queue.
403 RxQueues []MemifQueueDetails
404 // TxQueues contains details for each Tx queue.
405 TxQueues []MemifQueueDetails
408 // MemifQueueDetails provides a detailed runtime information about a memif queue.
409 // Queue = Ring + the associated buffers (one directional).
410 type MemifQueueDetails struct {
411 // QueueID is the ID of the queue.
413 // RingSize is the number of slots in the ring (not logarithmic).
415 // BufferSize is the size of each buffer pointed to from the ring slots.
417 /* Further ring information TO-BE-ADDED when C-libmemif supports them. */
420 // CPacketBuffers stores an array of memif buffers for use with TxBurst or RxBurst.
421 type CPacketBuffers struct {
422 buffers *C.memif_buffer_t
424 rxChainBuf []RawPacketData
427 // Context is a global Go-libmemif runtime context.
428 type Context struct {
431 memifs map[int] /* ifIndex */ *Memif /* slice of all active memif interfaces */
434 wg sync.WaitGroup /* wait-group for pollEvents() */
437 type txPacketBuffer struct {
438 packets []RawPacketData
443 // logger used by the adapter.
446 // Global Go-libmemif context.
447 context = &Context{initialized: false}
450 // init initializes global logger, which logs debug level messages to stdout.
454 log.Level = logger.DebugLevel
457 // SetLogger changes the logger for Go-libmemif to the provided one.
458 // The logger is not used for logging of C-libmemif.
459 func SetLogger(l *logger.Logger) {
463 // Init initializes the libmemif library. Must by called exactly once and before
464 // any libmemif functions. Do not forget to call Cleanup() before exiting
466 // <appName> should be a human-readable string identifying your application.
467 // For example, VPP returns the version information ("show version" from VPP CLI).
468 func Init(appName string) error {
470 defer context.lock.Unlock()
472 if context.initialized {
473 return ErrAlreadyInit
476 log.Debug("Initializing libmemif library")
478 // Initialize C-libmemif.
481 errCode = int(C.memif_init(nil, nil, nil, nil, nil))
483 appName := C.CString(appName)
484 defer C.free(unsafe.Pointer(appName))
485 errCode = int(C.memif_init(nil, appName, nil, nil, nil))
487 err := getMemifError(errCode)
492 // Initialize the map of memory interfaces.
493 context.memifs = make(map[int]*Memif)
495 // Start event polling.
499 context.initialized = true
500 log.Debug("libmemif library was initialized")
504 // Cleanup cleans up all the resources allocated by libmemif.
505 func Cleanup() error {
507 defer context.lock.Unlock()
509 if !context.initialized {
513 log.Debug("Closing libmemif library")
515 // Delete all active interfaces.
516 for _, memif := range context.memifs {
520 // Stop the event loop (if supported by C-libmemif).
521 errCode := C.memif_cancel_poll_event()
522 err := getMemifError(int(errCode))
524 log.Debug("Waiting for pollEvents() to stop...")
526 log.Debug("pollEvents() has stopped...")
528 log.WithField("err", err).Debug("NOT Waiting for pollEvents to stop...")
531 // Run cleanup for C-libmemif.
532 err = getMemifError(int(C.memif_cleanup()))
534 context.initialized = false
535 log.Debug("libmemif library was closed")
540 // CreateInterface creates a new memif interface with the given configuration.
541 // The same callbacks can be used with multiple memifs. The first callback input
542 // argument (*Memif) can be used to tell which memif the callback was triggered for.
543 // The method is thread-safe.
544 func CreateInterface(config *MemifConfig, callbacks *MemifCallbacks) (memif *Memif, err error) {
546 defer context.lock.Unlock()
548 if !context.initialized {
549 return nil, ErrNotInit
552 log.WithField("ifName", config.IfName).Debug("Creating a new memif interface")
554 log2RingSize := config.Log2RingSize
555 if log2RingSize == 0 {
559 bufferSize := config.BufferSize
564 // Create memif-wrapper for Go-libmemif.
566 MemifMeta: config.MemifMeta,
567 callbacks: &MemifCallbacks{},
568 ifIndex: context.nextMemifIndex,
569 ringSize: 1 << log2RingSize,
570 bufferSize: int(bufferSize),
573 // Initialize memif callbacks.
574 if callbacks != nil {
575 memif.callbacks.OnConnect = callbacks.OnConnect
576 memif.callbacks.OnDisconnect = callbacks.OnDisconnect
579 // Initialize memif-global interrupt channel.
580 memif.intCh = make(chan uint8, 1<<6)
582 // Initialize event file descriptor for stopping Rx/Tx queue polling.
583 memif.stopQPollFd = int(C.eventfd(0, C.EFD_NONBLOCK))
584 if memif.stopQPollFd < 0 {
585 return nil, ErrSyscall
588 // Initialize memif input arguments.
589 args := &C.govpp_memif_conn_args_t{}
590 // - socket file name
591 if config.SocketFilename != "" {
592 args.socket_filename = C.CString(config.SocketFilename)
593 defer C.free(unsafe.Pointer(args.socket_filename))
596 args.interface_id = C.uint32_t(config.ConnID)
598 if config.IfName != "" {
599 args.interface_name = C.CString(config.IfName)
600 defer C.free(unsafe.Pointer(args.interface_name))
605 args.mode = C.MEMIF_INTERFACE_MODE_ETHERNET
607 args.mode = C.MEMIF_INTERFACE_MODE_IP
608 case IfModePuntInject:
609 args.mode = C.MEMIF_INTERFACE_MODE_PUNT_INJECT
611 args.mode = C.MEMIF_INTERFACE_MODE_ETHERNET
614 if config.Secret != "" {
615 args.secret = C.CString(config.Secret)
616 defer C.free(unsafe.Pointer(args.secret))
618 // - master/slave flag + number of Rx/Tx queues
620 args.num_s2m_rings = C.uint8_t(config.NumRxQueues)
621 args.num_m2s_rings = C.uint8_t(config.NumTxQueues)
622 args.is_master = C.uint8_t(1)
624 args.num_s2m_rings = C.uint8_t(config.NumTxQueues)
625 args.num_m2s_rings = C.uint8_t(config.NumRxQueues)
626 args.is_master = C.uint8_t(0)
629 args.buffer_size = C.uint16_t(config.BufferSize)
630 // - log_2(ring size)
631 args.log2_ring_size = C.uint8_t(config.Log2RingSize)
633 // Create memif in C-libmemif.
634 errCode := C.govpp_memif_create(&memif.cHandle, args, unsafe.Pointer(uintptr(memif.ifIndex)))
635 err = getMemifError(int(errCode))
640 // Register the new memif.
641 context.memifs[memif.ifIndex] = memif
642 context.nextMemifIndex++
643 log.WithField("ifName", config.IfName).Debug("A new memif interface was created")
648 // GetInterruptChan returns a channel which is continuously being filled with
649 // IDs of queues with data ready to be received.
650 // Since there is only one interrupt signal sent for an entire burst of packets,
651 // an interrupt handling routine should repeatedly call RxBurst() until
652 // the function returns an empty slice of packets. This way it is ensured
653 // that there are no packets left on the queue unread when the interrupt signal
655 // The method is thread-safe.
656 func (memif *Memif) GetInterruptChan() (ch <-chan uint8 /* queue ID */) {
660 // GetQueueInterruptChan returns an empty-data channel which fires every time
661 // there are data to read on a given queue.
662 // It is only valid to call this function if memif is in the connected state.
663 // Channel is automatically closed when the connection goes down (but after
664 // the user provided callback OnDisconnect has executed).
665 // Since there is only one interrupt signal sent for an entire burst of packets,
666 // an interrupt handling routine should repeatedly call RxBurst() until
667 // the function returns an empty slice of packets. This way it is ensured
668 // that there are no packets left on the queue unread when the interrupt signal
670 // The method is thread-safe.
671 func (memif *Memif) GetQueueInterruptChan(queueID uint8) (ch <-chan struct{}, err error) {
672 if int(queueID) >= len(memif.queueIntCh) {
673 return nil, ErrQueueID
675 return memif.queueIntCh[queueID], nil
678 // SetRxMode allows to switch between the interrupt and the polling mode for Rx.
679 // The method is thread-safe.
680 func (memif *Memif) SetRxMode(queueID uint8, rxMode RxMode) (err error) {
681 var cRxMode C.memif_rx_mode_t
683 case RxModeInterrupt:
684 cRxMode = C.MEMIF_RX_MODE_INTERRUPT
686 cRxMode = C.MEMIF_RX_MODE_POLLING
688 cRxMode = C.MEMIF_RX_MODE_INTERRUPT
690 errCode := C.memif_set_rx_mode(memif.cHandle, cRxMode, C.uint16_t(queueID))
691 return getMemifError(int(errCode))
694 // GetDetails returns a detailed runtime information about this memif.
695 // The method is thread-safe.
696 func (memif *Memif) GetDetails() (details *MemifDetails, err error) {
697 cDetails := C.govpp_memif_details_t{}
700 // Get memif details from C-libmemif.
701 errCode := C.govpp_memif_get_details(memif.cHandle, &cDetails, &buf)
702 err = getMemifError(int(errCode))
706 defer C.free(unsafe.Pointer(buf))
708 // Convert details from C to Go.
709 details = &MemifDetails{}
711 details.IfName = C.GoString(cDetails.if_name)
712 details.InstanceName = C.GoString(cDetails.inst_name)
713 details.ConnID = uint32(cDetails.id)
714 details.SocketFilename = C.GoString(cDetails.socket_filename)
715 if cDetails.secret != nil {
716 details.Secret = C.GoString(cDetails.secret)
718 details.IsMaster = cDetails.role == C.uint8_t(0)
719 switch cDetails.mode {
720 case C.MEMIF_INTERFACE_MODE_ETHERNET:
721 details.Mode = IfModeEthernet
722 case C.MEMIF_INTERFACE_MODE_IP:
723 details.Mode = IfModeIP
724 case C.MEMIF_INTERFACE_MODE_PUNT_INJECT:
725 details.Mode = IfModePuntInject
727 details.Mode = IfModeEthernet
729 // - connection details:
730 details.RemoteIfName = C.GoString(cDetails.remote_if_name)
731 details.RemoteInstanceName = C.GoString(cDetails.remote_inst_name)
732 details.HasLink = cDetails.link_up_down == C.uint8_t(1)
735 for i = 0; i < uint8(cDetails.rx_queues_num); i++ {
736 cRxQueue := C.govpp_get_rx_queue_details(&cDetails, C.int(i))
737 queueDetails := MemifQueueDetails{
738 QueueID: uint8(cRxQueue.qid),
739 RingSize: uint32(cRxQueue.ring_size),
740 BufferSize: uint16(cRxQueue.buffer_size),
742 details.RxQueues = append(details.RxQueues, queueDetails)
745 for i = 0; i < uint8(cDetails.tx_queues_num); i++ {
746 cTxQueue := C.govpp_get_tx_queue_details(&cDetails, C.int(i))
747 queueDetails := MemifQueueDetails{
748 QueueID: uint8(cTxQueue.qid),
749 RingSize: uint32(cTxQueue.ring_size),
750 BufferSize: uint16(cTxQueue.buffer_size),
752 details.TxQueues = append(details.TxQueues, queueDetails)
758 // TxBurst is used to send multiple packets in one call into a selected queue.
759 // The actual number of packets sent may be smaller and is returned as <count>.
760 // The method is non-blocking even if the ring is full and no packet can be sent.
761 // It is only valid to call this function if memif is in the connected state.
762 // Multiple TxBurst-s can run concurrently provided that each targets a different
764 func (memif *Memif) TxBurst(queueID uint8, packets []RawPacketData) (count uint16, err error) {
765 if len(packets) == 0 {
769 if int(queueID) >= len(memif.txQueueBufs) {
774 var buffers []*txPacketBuffer
775 cQueueID := C.uint16_t(queueID)
777 for _, packet := range packets {
778 packetLen := len(packet)
779 log.Debugf("%v - preparing packet with len %v", cQueueID, packetLen)
781 if packetLen > memif.bufferSize {
782 // Create jumbo buffer
783 buffer := &txPacketBuffer{
785 packets: []RawPacketData{packet},
788 buffers = append(buffers, buffer)
790 // Increment bufCount by number of splits in this jumbo
791 bufCount += (buffer.size + memif.bufferSize - 1) / memif.bufferSize
793 buffersLen := len(buffers)
795 // This is very first buffer so there is no data to append to, prepare empty one
797 buffers = []*txPacketBuffer{{}}
801 lastBuffer := buffers[buffersLen-1]
803 // Last buffer is jumbo buffer, create new buffer
804 if lastBuffer.size > memif.bufferSize {
805 lastBuffer = &txPacketBuffer{}
806 buffers = append(buffers, lastBuffer)
809 // Determine buffer size by max packet size in buffer
810 if packetLen > lastBuffer.size {
811 lastBuffer.size = packetLen
814 lastBuffer.packets = append(lastBuffer.packets, packet)
819 // Reallocate Tx buffers if needed to fit the input packets.
820 log.Debugf("%v - total buffer to allocate count %v", cQueueID, bufCount)
821 pb := &memif.txQueueBufs[queueID]
822 if pb.count < bufCount {
823 newBuffers := C.realloc(unsafe.Pointer(pb.buffers), C.size_t(bufCount*int(C.sizeof_memif_buffer_t)))
824 if newBuffers == nil {
825 // Realloc failed, <count> will be less than len(packets).
828 pb.buffers = (*C.memif_buffer_t)(newBuffers)
833 // Allocate ring slots.
834 var allocated C.uint16_t
835 var subCount C.uint16_t
836 for _, buffer := range buffers {
837 packetCount := C.uint16_t(len(buffer.packets))
838 isJumbo := buffer.size > memif.bufferSize
840 log.Debugf("%v - trying to send max buff size %v, packets len %v, buffer len %v, jumbo %v",
841 cQueueID, buffer.size, len(buffer.packets), packetCount, isJumbo)
843 var nextFreeBuff *C.memif_buffer_t
844 startOffset := allocated
845 errCode := C.govpp_memif_buffer_alloc(memif.cHandle, cQueueID, pb.buffers, startOffset, &nextFreeBuff,
846 packetCount, &allocated, C.uint16_t(buffer.size))
848 err = getMemifError(int(errCode))
849 endEarly := err == ErrNoBufRing
851 // Not enough ring slots, <count> will be less than packetCount.
858 // Copy packet data into the buffers.
859 nowAllocated := allocated - startOffset
860 toFill := nowAllocated
862 // If this is not jumbo frame, only 1 packet needs to be copied each iteration
866 // Iterate over all packets and try to fill them into allocated buffers
867 // If packet is jumbo frame, continue filling to allocated buffers until no buffer is left
868 for i, packet := range buffer.packets {
869 if i >= int(nowAllocated) {
870 // There was less allocated buffers than actual packet count so exit early
874 packetData := unsafe.Pointer(&packet[0])
875 C.govpp_copy_packet_data(nextFreeBuff, toFill, C.int(i), packetData, C.uint16_t(len(packet)))
878 if isJumbo && nowAllocated > 0 {
879 // If we successfully allocated required amount of buffers for entire jumbo to be sent
880 // simply sub entire amount of jumbo frame packets and leave only 1 so sender will think
881 // it only sent 1 packet so it does not need to know anything about jumbo frames
882 subCount += nowAllocated - 1
885 // If we do not have enough buffers left to allocate, simply end here to avoid packet loss and try
886 // to handle it next burst
892 var sentCount C.uint16_t
893 errCode := C.memif_tx_burst(memif.cHandle, cQueueID, pb.buffers, allocated, &sentCount)
894 err = getMemifError(int(errCode))
899 // Prevent negative values
900 realSent := uint16(sentCount) - uint16(subCount)
901 if subCount > sentCount {
905 log.Debugf("%v - sent %v total allocated buffs %v", cQueueID, sentCount, allocated)
909 // RxBurst is used to receive multiple packets in one call from a selected queue.
910 // <count> is the number of packets to receive. The actual number of packets
911 // received may be smaller. <count> effectively limits the maximum number
912 // of packets to receive in one burst (for a flat, predictable memory usage).
913 // The method is non-blocking even if there are no packets to receive.
914 // It is only valid to call this function if memif is in the connected state.
915 // Multiple RxBurst-s can run concurrently provided that each targets a different
917 func (memif *Memif) RxBurst(queueID uint8, count uint16) (packets []RawPacketData, err error) {
918 var recvCount C.uint16_t
924 if int(queueID) >= len(memif.rxQueueBufs) {
925 return packets, ErrQueueID
928 // Reallocate Rx buffers if needed to fit the output packets.
929 pb := &memif.rxQueueBufs[queueID]
930 bufCount := int(count)
931 if pb.count < bufCount {
932 newBuffers := C.realloc(unsafe.Pointer(pb.buffers), C.size_t(bufCount*int(C.sizeof_memif_buffer_t)))
933 if newBuffers == nil {
934 // Realloc failed, len(<packets>) will be certainly less than <count>.
937 pb.buffers = (*C.memif_buffer_t)(newBuffers)
942 cQueueID := C.uint16_t(queueID)
943 errCode := C.memif_rx_burst(memif.cHandle, cQueueID, pb.buffers, C.uint16_t(bufCount), &recvCount)
944 err = getMemifError(int(errCode))
946 // More packets to read - the user is expected to run RxBurst() until there
947 // are no more packets to receive.
954 chained := len(pb.rxChainBuf) > 0
956 // We had stored data from previous burst because last buffer in previous burst was chained
957 // so we need to continue appending to this data
958 packets = pb.rxChainBuf
962 // Copy packet data into the instances of RawPacketData.
963 for i := 0; i < int(recvCount); i++ {
965 packetData := C.govpp_get_packet_data(pb.buffers, C.int(i), &packetSize)
966 packetBytes := C.GoBytes(packetData, packetSize)
969 // We have chained buffers, so start merging packet data with last read packet
970 prevPacket := packets[len(packets)-1]
971 packets[len(packets)-1] = append(prevPacket, packetBytes...)
973 packets = append(packets, packetBytes)
976 // Mark last buffer as chained based on property on current buffer so next buffers
977 // will try to append data to this one in case we got jumbo frame
978 chained = C.govpp_is_buffer_chained(pb.buffers, C.int(i)) > 0
982 errCode = C.memif_refill_queue(memif.cHandle, cQueueID, recvCount, 0)
984 err = getMemifError(int(errCode))
986 // Throw away packets to avoid duplicities.
991 // We did not had enough space to process all chained buffers to the end so simply tell
992 // reader that it should not process any packets here and save them for next burst
993 // to finish reading the buffer chain
994 pb.rxChainBuf = packets
1002 // Close removes the memif interface. If the memif is in the connected state,
1003 // the connection is first properly closed.
1004 // Do not access memif after it is closed, let garbage collector to remove it.
1005 func (memif *Memif) Close() error {
1006 log.WithField("ifName", memif.IfName).Debug("Closing the memif interface")
1008 // Delete memif from C-libmemif.
1009 err := getMemifError(int(C.memif_delete(&memif.cHandle)))
1012 // Close memif-global interrupt channel.
1014 // Close file descriptor stopQPollFd.
1015 C.close(C.int(memif.stopQPollFd))
1019 defer context.lock.Unlock()
1020 // Unregister the interface from the context.
1021 delete(context.memifs, memif.ifIndex)
1022 log.WithField("ifName", memif.IfName).Debug("memif interface was closed")
1027 // initQueues allocates resources associated with Rx/Tx queues.
1028 func (memif *Memif) initQueues() error {
1029 // Get Rx/Tx queues count.
1030 details, err := memif.GetDetails()
1035 log.WithFields(logger.Fields{
1036 "ifName": memif.IfName,
1037 "Rx-count": len(details.RxQueues),
1038 "Tx-count": len(details.TxQueues),
1039 }).Debug("Initializing Rx/Tx queues.")
1041 // Initialize interrupt channels.
1043 for i = 0; i < len(details.RxQueues); i++ {
1044 queueIntCh := make(chan struct{}, 1)
1045 memif.queueIntCh = append(memif.queueIntCh, queueIntCh)
1048 // Initialize Rx/Tx packet buffers.
1049 for i = 0; i < len(details.RxQueues); i++ {
1050 memif.rxQueueBufs = append(memif.rxQueueBufs, CPacketBuffers{})
1051 if !memif.IsMaster {
1052 errCode := C.memif_refill_queue(memif.cHandle, C.uint16_t(i), C.uint16_t(memif.ringSize-1), 0)
1053 err = getMemifError(int(errCode))
1055 log.Warn(err.Error())
1059 for i = 0; i < len(details.TxQueues); i++ {
1060 memif.txQueueBufs = append(memif.txQueueBufs, CPacketBuffers{})
1066 // closeQueues deallocates all resources associated with Rx/Tx queues.
1067 func (memif *Memif) closeQueues() {
1068 log.WithFields(logger.Fields{
1069 "ifName": memif.IfName,
1070 "Rx-count": len(memif.rxQueueBufs),
1071 "Tx-count": len(memif.txQueueBufs),
1072 }).Debug("Closing Rx/Tx queues.")
1074 // Close interrupt channels.
1075 for _, ch := range memif.queueIntCh {
1078 memif.queueIntCh = nil
1080 // Deallocate Rx/Tx packet buffers.
1081 for _, pb := range memif.rxQueueBufs {
1082 C.free(unsafe.Pointer(pb.buffers))
1084 memif.rxQueueBufs = nil
1085 for _, pb := range memif.txQueueBufs {
1086 C.free(unsafe.Pointer(pb.buffers))
1088 memif.txQueueBufs = nil
1091 // pollEvents repeatedly polls for a libmemif event.
1093 defer context.wg.Done()
1095 errCode := C.memif_poll_event(C.int(-1))
1096 err := getMemifError(int(errCode))
1097 if err == ErrPollCanceled {
1103 // pollRxQueue repeatedly polls an Rx queue for interrupts.
1104 func pollRxQueue(memif *Memif, queueID uint8) {
1105 defer memif.wg.Done()
1107 log.WithFields(logger.Fields{
1108 "ifName": memif.IfName,
1109 "queue-ID": queueID,
1110 }).Debug("Started queue interrupt polling.")
1113 errCode := C.memif_get_queue_efd(memif.cHandle, C.uint16_t(queueID), &qfd)
1114 err := getMemifError(int(errCode))
1116 log.WithField("err", err).Error("memif_get_queue_efd() failed")
1120 // Create epoll file descriptor.
1121 var event [1]syscall.EpollEvent
1122 epFd, err := syscall.EpollCreate1(0)
1124 log.WithField("err", err).Error("epoll_create1() failed")
1127 defer syscall.Close(epFd)
1129 // Add Rx queue interrupt file descriptor.
1130 event[0].Events = syscall.EPOLLIN
1131 event[0].Fd = int32(qfd)
1132 if err = syscall.EpollCtl(epFd, syscall.EPOLL_CTL_ADD, int(qfd), &event[0]); err != nil {
1133 log.WithField("err", err).Error("epoll_ctl() failed")
1137 // Add file descriptor used to stop this go routine.
1138 event[0].Events = syscall.EPOLLIN
1139 event[0].Fd = int32(memif.stopQPollFd)
1140 if err = syscall.EpollCtl(epFd, syscall.EPOLL_CTL_ADD, memif.stopQPollFd, &event[0]); err != nil {
1141 log.WithField("err", err).Error("epoll_ctl() failed")
1145 // Poll for interrupts.
1147 _, err := syscall.EpollWait(epFd, event[:], -1)
1149 log.WithField("err", err).Error("epoll_wait() failed")
1153 // Handle Rx Interrupt.
1154 if event[0].Fd == int32(qfd) {
1155 // Consume the interrupt event.
1156 buf := make([]byte, 8)
1157 _, err = syscall.Read(int(qfd), buf[:])
1159 log.WithField("err", err).Warn("read() failed")
1162 // Send signal to memif-global interrupt channel.
1164 case memif.intCh <- queueID:
1170 // Send signal to queue-specific interrupt channel.
1172 case memif.queueIntCh[queueID] <- struct{}{}:
1179 // Stop the go routine if requested.
1180 if event[0].Fd == int32(memif.stopQPollFd) {
1181 log.WithFields(logger.Fields{
1182 "ifName": memif.IfName,
1183 "queue-ID": queueID,
1184 }).Debug("Stopped queue interrupt polling.")
1190 //export go_on_connect_callback
1191 func go_on_connect_callback(privateCtx unsafe.Pointer) C.int {
1192 log.Debug("go_on_connect_callback BEGIN")
1193 defer log.Debug("go_on_connect_callback END")
1194 context.lock.RLock()
1195 defer context.lock.RUnlock()
1197 // Get memif reference.
1198 ifIndex := int(uintptr(privateCtx))
1199 memif, exists := context.memifs[ifIndex]
1201 return C.int(ErrNoConn.Code())
1204 // Initialize Rx/Tx queues.
1205 err := memif.initQueues()
1207 if memifErr, ok := err.(*MemifError); ok {
1208 return C.int(memifErr.Code())
1210 return C.int(ErrUnknown.Code())
1213 // Call the user callback.
1214 if memif.callbacks.OnConnect != nil {
1215 memif.callbacks.OnConnect(memif)
1218 // Start polling the RX queues for interrupts.
1219 for i := 0; i < len(memif.queueIntCh); i++ {
1221 go pollRxQueue(memif, uint8(i))
1227 //export go_on_disconnect_callback
1228 func go_on_disconnect_callback(privateCtx unsafe.Pointer) C.int {
1229 log.Debug("go_on_disconnect_callback BEGIN")
1230 defer log.Debug("go_on_disconnect_callback END")
1231 context.lock.RLock()
1232 defer context.lock.RUnlock()
1234 // Get memif reference.
1235 ifIndex := int(uintptr(privateCtx))
1236 memif, exists := context.memifs[ifIndex]
1242 // Stop polling the RX queues for interrupts.
1243 buf := make([]byte, 8)
1244 binary.PutUvarint(buf, 1)
1246 _, err := syscall.Write(memif.stopQPollFd, buf[:])
1248 return C.int(ErrSyscall.Code())
1252 // - remove the event
1253 _, err = syscall.Read(memif.stopQPollFd, buf[:])
1255 return C.int(ErrSyscall.Code())
1258 // Call the user callback.
1259 if memif.callbacks.OnDisconnect != nil {
1260 memif.callbacks.OnDisconnect(memif)
1263 // Close Rx/Tx queues.