Update libmemif
[govpp.git] / extras / libmemif / adapter.go
1 // Copyright (c) 2017 Cisco and/or its affiliates.
2 //
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:
6 //
7 //       http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 // +build !windows,!darwin
16
17 package libmemif
18
19 import (
20         "encoding/binary"
21         "os"
22         "sync"
23         "syscall"
24         "unsafe"
25
26         logger "github.com/sirupsen/logrus"
27 )
28
29 /*
30 #cgo LDFLAGS: -lmemif
31
32 #include <unistd.h>
33 #include <stdlib.h>
34 #include <stdint.h>
35 #include <string.h>
36 #include <sys/eventfd.h>
37 #include <libmemif.h>   // <-- VPP must be installed!
38
39 // Feature tests.
40 #ifndef MEMIF_HAVE_CANCEL_POLL_EVENT
41 // memif_cancel_poll_event that simply returns ErrUnsupported.
42 static int
43 memif_cancel_poll_event ()
44 {
45         return 102; // ErrUnsupported
46 }
47 #endif
48
49 // govpp_memif_conn_args_t replaces fixed sized arrays with C-strings which
50 // are much easier to work with in cgo.
51 typedef struct
52 {
53         memif_socket_handle_t socket;
54         char *secret;
55         uint8_t num_s2m_rings;
56         uint8_t num_m2s_rings;
57         uint16_t buffer_size;
58         uint8_t log2_ring_size;
59         uint8_t is_master;
60         uint32_t interface_id;
61         char *interface_name;
62         memif_interface_mode_t mode;
63 } govpp_memif_conn_args_t;
64
65 // govpp_memif_details_t replaces strings represented with (uint8_t *)
66 // to the standard and easy to work with in cgo: (char *)
67 typedef struct
68 {
69         char *if_name;
70         char *inst_name;
71         char *remote_if_name;
72         char *remote_inst_name;
73         uint32_t id;
74         char *secret;
75         uint8_t role;
76         uint8_t mode;
77         char *socket_filename;
78         uint8_t regions_num;
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;
84         uint8_t link_up_down;
85 } govpp_memif_details_t;
86
87 extern int go_on_connect_callback(void *privateCtx);
88 extern int go_on_disconnect_callback(void *privateCtx);
89
90 // Callbacks strip the connection handle away.
91
92 static int
93 govpp_on_connect_callback(memif_conn_handle_t conn, void *private_ctx)
94 {
95         return go_on_connect_callback(private_ctx);
96 }
97
98 static int
99 govpp_on_disconnect_callback(memif_conn_handle_t conn, void *private_ctx)
100 {
101         return go_on_disconnect_callback(private_ctx);
102 }
103
104 // govpp_memif_create uses govpp_memif_conn_args_t.
105 static int
106 govpp_memif_create (memif_conn_handle_t *conn, govpp_memif_conn_args_t *go_args,
107                     void *private_ctx)
108 {
109         memif_conn_args_t args;
110         memset (&args, 0, sizeof (args));
111         args.socket = (char *)go_args->socket;
112         if (go_args->secret != NULL)
113         {
114                 strncpy ((char *)args.secret, go_args->secret,
115                                  sizeof (args.secret) - 1);
116         }
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)
124         {
125                 strncpy ((char *)args.interface_name, go_args->interface_name,
126                                  sizeof(args.interface_name) - 1);
127         }
128         args.mode = go_args->mode;
129
130         return memif_create(conn, &args, govpp_on_connect_callback,
131                                                 govpp_on_disconnect_callback, NULL,
132                                                 private_ctx);
133 }
134
135 static int
136 govpp_memif_create_socket (memif_socket_handle_t *sock, char *filename)
137 {
138         return memif_create_socket(sock, filename, NULL);
139 }
140
141 // govpp_memif_get_details keeps reallocating buffer until it is large enough.
142 // The buffer is returned to be deallocated when it is no longer needed.
143 static int
144 govpp_memif_get_details (memif_conn_handle_t conn, govpp_memif_details_t *govpp_md,
145                          char **buf)
146 {
147         int rv = 0;
148         size_t buflen = 1 << 7;
149         char *buffer = NULL, *new_buffer = NULL;
150         memif_details_t md = {0};
151
152         do {
153                 // initial malloc (256 bytes) or realloc
154                 buflen <<= 1;
155                 new_buffer = realloc(buffer, buflen);
156                 if (new_buffer == NULL)
157                 {
158                         free(buffer);
159                         return MEMIF_ERR_NOMEM;
160                 }
161                 buffer = new_buffer;
162                 // try to get details
163                 rv = memif_get_details(conn, &md, buffer, buflen);
164         } while (rv == MEMIF_ERR_NOBUF_DET);
165
166         if (rv == 0)
167         {
168                 *buf = buffer;
169                 govpp_md->if_name = (char *)md.if_name;
170                 govpp_md->inst_name = (char *)md.inst_name;
171                 govpp_md->remote_if_name = (char *)md.remote_if_name;
172                 govpp_md->remote_inst_name = (char *)md.remote_inst_name;
173                 govpp_md->id = md.id;
174                 govpp_md->secret = (char *)md.secret;
175                 govpp_md->role = md.role;
176                 govpp_md->mode = md.mode;
177                 govpp_md->socket_filename = (char *)md.socket_filename;
178                 govpp_md->regions_num = md.regions_num;
179                 govpp_md->regions = md.regions;
180                 govpp_md->rx_queues_num = md.rx_queues_num;
181                 govpp_md->tx_queues_num = md.tx_queues_num;
182                 govpp_md->rx_queues = md.rx_queues;
183                 govpp_md->tx_queues = md.tx_queues;
184                 govpp_md->link_up_down = md.link_up_down;
185         }
186         else
187                 free(buffer);
188         return rv;
189 }
190
191 // Used to avoid cumbersome tricks that use unsafe.Pointer() + unsafe.Sizeof()
192 // or even cast C-array directly into Go-slice.
193 static memif_queue_details_t
194 govpp_get_rx_queue_details (govpp_memif_details_t *md, int index)
195 {
196         return md->rx_queues[index];
197 }
198
199 // Used to avoid cumbersome tricks that use unsafe.Pointer() + unsafe.Sizeof()
200 // or even cast C-array directly into Go-slice.
201 static memif_queue_details_t
202 govpp_get_tx_queue_details (govpp_memif_details_t *md, int index)
203 {
204         return md->tx_queues[index];
205 }
206
207 // Copy packet data into the selected buffer with splitting when necessary
208 static void
209 govpp_copy_packet_data(memif_buffer_t *buffers, uint16_t allocated, int bufIndex, void *packetData, uint16_t packetSize)
210 {
211         int dataOffset = 0;
212
213         do {
214                 buffers[bufIndex].len = (packetSize > buffers[bufIndex].len ? buffers[bufIndex].len : packetSize);
215                 void * curData = (packetData + dataOffset);
216                 memcpy(buffers[bufIndex].data, curData, (size_t)buffers[bufIndex].len);
217                 dataOffset += buffers[bufIndex].len;
218                 bufIndex += 1;
219                 packetSize -= buffers[bufIndex].len;
220         } while(packetSize > 0 && bufIndex < allocated && buffers[bufIndex].flags & MEMIF_BUFFER_FLAG_NEXT > 0);
221 }
222
223 // Get packet data from the selected buffer.
224 // Used to avoid an ugly unsafe.Pointer() + unsafe.Sizeof().
225 static void *
226 govpp_get_packet_data(memif_buffer_t *buffers, int index, int *size)
227 {
228         *size = (int)buffers[index].len;
229         return buffers[index].data;
230 }
231
232 // Checks if memif buffer is chained
233 static int
234 govpp_is_buffer_chained(memif_buffer_t *buffers, int index)
235 {
236     return buffers[index].flags & MEMIF_BUFFER_FLAG_NEXT;
237 }
238
239 // Allocate memif buffers and return pointer to next free buffer
240 static int
241 govpp_memif_buffer_alloc(memif_conn_handle_t conn, uint16_t qid,
242                         memif_buffer_t * bufs, uint16_t offset, memif_buffer_t ** nextFreeBuf,
243                         uint16_t count, uint16_t * count_out, uint16_t size)
244 {
245     memif_buffer_t * offsetBufs = (bufs + offset);
246     int err = memif_buffer_alloc(conn, qid, offsetBufs, count, count_out, size);
247     *count_out += offset;
248     *nextFreeBuf = offsetBufs;
249     return err;
250 }
251
252 */
253 import "C"
254
255 // IfMode represents the mode (layer/behaviour) in which the interface operates.
256 type IfMode int
257
258 const (
259         // IfModeEthernet tells memif to operate on the L2 layer.
260         IfModeEthernet IfMode = iota
261
262         // IfModeIP tells memif to operate on the L3 layer.
263         IfModeIP
264
265         // IfModePuntInject tells memif to behave as Inject/Punt interface.
266         IfModePuntInject
267 )
268
269 // RxMode is used to switch between polling and interrupt for RX.
270 type RxMode int
271
272 const (
273         // RxModeInterrupt tells libmemif to send interrupt signal when data are available.
274         RxModeInterrupt RxMode = iota
275
276         // RxModePolling means that the user needs to explicitly poll for data on RX
277         // queues.
278         RxModePolling
279 )
280
281 // RawPacketData represents raw packet data. libmemif doesn't care what the
282 // actual content is, it only manipulates with raw bytes.
283 type RawPacketData []byte
284
285 // MemifMeta is used to store a basic memif metadata needed for identification
286 // and connection establishment.
287 type MemifMeta struct {
288         // IfName is the interface name. Has to be unique across all created memifs.
289         // Interface name is truncated if needed to have no more than 32 characters.
290         IfName string
291
292         // InstanceName identifies the endpoint. If omitted, the application
293         // name passed to Init() will be used instead.
294         // Instance name is truncated if needed to have no more than 32 characters.
295         InstanceName string
296
297         // ConnID is a connection ID used to match opposite sides of the memif
298         // connection.
299         ConnID uint32
300
301         // SocketFilename is the filename of the AF_UNIX socket through which
302         // the connection is established.
303         // The string is truncated if neede to fit into sockaddr_un.sun_path
304         // (108 characters on Linux).
305         SocketFilename string
306
307         // Secret must be the same on both sides for the authentication to succeed.
308         // Empty string is allowed.
309         // The secret is truncated if needed to have no more than 24 characters.
310         Secret string
311
312         // IsMaster is set to true if memif operates in the Master mode.
313         IsMaster bool
314
315         // Mode is the mode (layer/behaviour) in which the memif operates.
316         Mode IfMode
317 }
318
319 // MemifShmSpecs is used to store the specification of the shared memory segment
320 // used by memif to send/receive packets.
321 type MemifShmSpecs struct {
322         // NumRxQueues is the number of Rx queues.
323         // Default is 1 (used if the value is 0).
324         NumRxQueues uint8
325
326         // NumTxQueues is the number of Tx queues.
327         // Default is 1 (used if the value is 0).
328         NumTxQueues uint8
329
330         // BufferSize is the size of the buffer to hold one packet, or a single
331         // fragment of a jumbo frame. Default is 2048 (used if the value is 0).
332         BufferSize uint16
333
334         // Log2RingSize is the number of items in the ring represented through
335         // the logarithm base 2.
336         // Default is 10 (used if the value is 0).
337         Log2RingSize uint8
338 }
339
340 // MemifConfig is the memif configuration.
341 // Used as the input argument to CreateInterface().
342 // It is the slave's config that mostly decides the parameters of the connection,
343 // but master may limit some of the quantities if needed (based on the memif
344 // protocol or master's configuration)
345 type MemifConfig struct {
346         MemifMeta
347         MemifShmSpecs
348 }
349
350 // ConnUpdateCallback is a callback type declaration used with callbacks
351 // related to connection status changes.
352 type ConnUpdateCallback func(memif *Memif) (err error)
353
354 // MemifCallbacks is a container for all callbacks provided by memif.
355 // Any callback can be nil, in which case it will be simply skipped.
356 // Important: Do not call CreateInterface() or Memif.Close() from within a callback
357 // or a deadlock will occur. Instead send signal through a channel to another
358 // go routine which will be able to create/remove memif interface(s).
359 type MemifCallbacks struct {
360         // OnConnect is triggered when a connection for a given memif was established.
361         OnConnect ConnUpdateCallback
362
363         // OnDisconnect is triggered when a connection for a given memif was lost.
364         OnDisconnect ConnUpdateCallback
365 }
366
367 // Memif represents a single memif interface. It provides methods to send/receive
368 // packets in bursts in either the polling mode or in the interrupt mode with
369 // the help of golang channels.
370 type Memif struct {
371         MemifMeta
372
373         // Per-library references
374         ifIndex int                     // index used in the Go-libmemif context (Context.memifs)
375         cHandle C.memif_conn_handle_t   // connection handle used in C-libmemif
376         sHandle C.memif_socket_handle_t // socket handle used in C-libmemif
377
378         // Callbacks
379         callbacks *MemifCallbacks
380
381         // Interrupt
382         intCh      chan uint8      // memif-global interrupt channel (value = queue ID)
383         queueIntCh []chan struct{} // per RX queue interrupt channel
384
385         // Rx/Tx queues
386         ringSize    int              // number of items in each ring
387         bufferSize  int              // max buffer size
388         stopQPollFd int              // event file descriptor used to stop pollRxQueue-s
389         wg          sync.WaitGroup   // wait group for all pollRxQueue-s
390         rxQueueBufs []CPacketBuffers // an array of C-libmemif packet buffers for each RX queue
391         txQueueBufs []CPacketBuffers // an array of C-libmemif packet buffers for each TX queue
392 }
393
394 // MemifDetails provides a detailed runtime information about a memif interface.
395 type MemifDetails struct {
396         MemifMeta
397         MemifConnDetails
398 }
399
400 // MemifConnDetails provides a detailed runtime information about a memif
401 // connection.
402 type MemifConnDetails struct {
403         // RemoteIfName is the name of the memif on the opposite side.
404         RemoteIfName string
405         // RemoteInstanceName is the name of the endpoint on the opposite side.
406         RemoteInstanceName string
407         // HasLink is true if the connection has link (= is established and functional).
408         HasLink bool
409         // RxQueues contains details for each Rx queue.
410         RxQueues []MemifQueueDetails
411         // TxQueues contains details for each Tx queue.
412         TxQueues []MemifQueueDetails
413 }
414
415 // MemifQueueDetails provides a detailed runtime information about a memif queue.
416 // Queue = Ring + the associated buffers (one directional).
417 type MemifQueueDetails struct {
418         // QueueID is the ID of the queue.
419         QueueID uint8
420         // RingSize is the number of slots in the ring (not logarithmic).
421         RingSize uint32
422         // BufferSize is the size of each buffer pointed to from the ring slots.
423         BufferSize uint16
424         /* Further ring information TO-BE-ADDED when C-libmemif supports them. */
425 }
426
427 // CPacketBuffers stores an array of memif buffers for use with TxBurst or RxBurst.
428 type CPacketBuffers struct {
429         buffers    *C.memif_buffer_t
430         count      int
431         rxChainBuf []RawPacketData
432 }
433
434 // Context is a global Go-libmemif runtime context.
435 type Context struct {
436         lock           sync.RWMutex
437         initialized    bool
438         memifs         map[int] /* ifIndex */ *Memif /* slice of all active memif interfaces */
439         nextMemifIndex int
440
441         wg sync.WaitGroup /* wait-group for pollEvents() */
442 }
443
444 type txPacketBuffer struct {
445         packets []RawPacketData
446         size    int
447 }
448
449 var (
450         // logger used by the adapter.
451         log *logger.Logger
452
453         // Global Go-libmemif context.
454         context = &Context{initialized: false}
455 )
456
457 // init initializes global logger, which logs debug level messages to stdout.
458 func init() {
459         log = logger.New()
460         log.Out = os.Stdout
461         log.Level = logger.DebugLevel
462 }
463
464 // SetLogger changes the logger for Go-libmemif to the provided one.
465 // The logger is not used for logging of C-libmemif.
466 func SetLogger(l *logger.Logger) {
467         log = l
468 }
469
470 // Init initializes the libmemif library. Must by called exactly once and before
471 // any libmemif functions. Do not forget to call Cleanup() before exiting
472 // your application.
473 // <appName> should be a human-readable string identifying your application.
474 // For example, VPP returns the version information ("show version" from VPP CLI).
475 func Init(appName string) error {
476         context.lock.Lock()
477         defer context.lock.Unlock()
478
479         if context.initialized {
480                 return ErrAlreadyInit
481         }
482
483         log.Debug("Initializing libmemif library")
484
485         // Initialize C-libmemif.
486         var errCode int
487         if appName == "" {
488                 errCode = int(C.memif_init(nil, nil, nil, nil, nil))
489         } else {
490                 appName := C.CString(appName)
491                 defer C.free(unsafe.Pointer(appName))
492                 errCode = int(C.memif_init(nil, appName, nil, nil, nil))
493         }
494         err := getMemifError(errCode)
495         if err != nil {
496                 return err
497         }
498
499         // Initialize the map of memory interfaces.
500         context.memifs = make(map[int]*Memif)
501
502         // Start event polling.
503         context.wg.Add(1)
504         go pollEvents()
505
506         context.initialized = true
507         log.Debug("libmemif library was initialized")
508         return err
509 }
510
511 // Cleanup cleans up all the resources allocated by libmemif.
512 func Cleanup() error {
513         context.lock.Lock()
514         defer context.lock.Unlock()
515
516         if !context.initialized {
517                 return ErrNotInit
518         }
519
520         log.Debug("Closing libmemif library")
521
522         // Delete all active interfaces.
523         for _, memif := range context.memifs {
524                 memif.Close()
525         }
526
527         // Stop the event loop (if supported by C-libmemif).
528         errCode := C.memif_cancel_poll_event()
529         err := getMemifError(int(errCode))
530         if err == nil {
531                 log.Debug("Waiting for pollEvents() to stop...")
532                 context.wg.Wait()
533                 log.Debug("pollEvents() has stopped...")
534         } else {
535                 log.WithField("err", err).Debug("NOT Waiting for pollEvents to stop...")
536         }
537
538         // Run cleanup for C-libmemif.
539         err = getMemifError(int(C.memif_cleanup()))
540         if err == nil {
541                 context.initialized = false
542                 log.Debug("libmemif library was closed")
543         }
544         return err
545 }
546
547 // CreateInterface creates a new memif interface with the given configuration.
548 // The same callbacks can be used with multiple memifs. The first callback input
549 // argument (*Memif) can be used to tell which memif the callback was triggered for.
550 // The method is thread-safe.
551 func CreateInterface(config *MemifConfig, callbacks *MemifCallbacks) (memif *Memif, err error) {
552         context.lock.Lock()
553         defer context.lock.Unlock()
554
555         if !context.initialized {
556                 return nil, ErrNotInit
557         }
558
559         log.WithField("ifName", config.IfName).Debug("Creating a new memif interface")
560
561         log2RingSize := config.Log2RingSize
562         if log2RingSize == 0 {
563                 log2RingSize = 10
564         }
565
566         bufferSize := config.BufferSize
567         if bufferSize <= 0 {
568                 bufferSize = 2048
569         }
570
571         // Create memif-wrapper for Go-libmemif.
572         memif = &Memif{
573                 MemifMeta:  config.MemifMeta,
574                 callbacks:  &MemifCallbacks{},
575                 ifIndex:    context.nextMemifIndex,
576                 ringSize:   1 << log2RingSize,
577                 bufferSize: int(bufferSize),
578         }
579
580         // Initialize memif callbacks.
581         if callbacks != nil {
582                 memif.callbacks.OnConnect = callbacks.OnConnect
583                 memif.callbacks.OnDisconnect = callbacks.OnDisconnect
584         }
585
586         // Initialize memif-global interrupt channel.
587         memif.intCh = make(chan uint8, 1<<6)
588
589         // Initialize event file descriptor for stopping Rx/Tx queue polling.
590         memif.stopQPollFd = int(C.eventfd(0, C.EFD_NONBLOCK))
591         if memif.stopQPollFd < 0 {
592                 return nil, ErrSyscall
593         }
594
595         // Initialize memif input arguments.
596         args := &C.govpp_memif_conn_args_t{}
597         // - socket file name
598         if config.SocketFilename != "" {
599                 log.WithField("name", config.SocketFilename).Debug("A new memif socket was created")
600                 errCode := C.govpp_memif_create_socket(&memif.sHandle, C.CString(config.SocketFilename))
601                 if getMemifError(int(errCode)) != nil {
602                         return nil, err
603                 }
604         }
605         args.socket = memif.sHandle
606         // - interface ID
607         args.interface_id = C.uint32_t(config.ConnID)
608         // - interface name
609         if config.IfName != "" {
610                 args.interface_name = C.CString(config.IfName)
611                 defer C.free(unsafe.Pointer(args.interface_name))
612         }
613         // - mode
614         switch config.Mode {
615         case IfModeEthernet:
616                 args.mode = C.MEMIF_INTERFACE_MODE_ETHERNET
617         case IfModeIP:
618                 args.mode = C.MEMIF_INTERFACE_MODE_IP
619         case IfModePuntInject:
620                 args.mode = C.MEMIF_INTERFACE_MODE_PUNT_INJECT
621         default:
622                 args.mode = C.MEMIF_INTERFACE_MODE_ETHERNET
623         }
624         // - secret
625         if config.Secret != "" {
626                 args.secret = C.CString(config.Secret)
627                 defer C.free(unsafe.Pointer(args.secret))
628         }
629         // - master/slave flag + number of Rx/Tx queues
630         if config.IsMaster {
631                 args.num_s2m_rings = C.uint8_t(config.NumRxQueues)
632                 args.num_m2s_rings = C.uint8_t(config.NumTxQueues)
633                 args.is_master = C.uint8_t(1)
634         } else {
635                 args.num_s2m_rings = C.uint8_t(config.NumTxQueues)
636                 args.num_m2s_rings = C.uint8_t(config.NumRxQueues)
637                 args.is_master = C.uint8_t(0)
638         }
639         // - buffer size
640         args.buffer_size = C.uint16_t(config.BufferSize)
641         // - log_2(ring size)
642         args.log2_ring_size = C.uint8_t(config.Log2RingSize)
643
644         // Create memif in C-libmemif.
645         errCode := C.govpp_memif_create(&memif.cHandle, args, unsafe.Pointer(uintptr(memif.ifIndex)))
646         if getMemifError(int(errCode)) != nil {
647                 return nil, err
648         }
649
650         // Register the new memif.
651         context.memifs[memif.ifIndex] = memif
652         context.nextMemifIndex++
653         log.WithField("ifName", config.IfName).Debug("A new memif interface was created")
654
655         return memif, nil
656 }
657
658 // GetInterruptChan returns a channel which is continuously being filled with
659 // IDs of queues with data ready to be received.
660 // Since there is only one interrupt signal sent for an entire burst of packets,
661 // an interrupt handling routine should repeatedly call RxBurst() until
662 // the function returns an empty slice of packets. This way it is ensured
663 // that there are no packets left on the queue unread when the interrupt signal
664 // is cleared.
665 // The method is thread-safe.
666 func (memif *Memif) GetInterruptChan() (ch <-chan uint8 /* queue ID */) {
667         return memif.intCh
668 }
669
670 // GetQueueInterruptChan returns an empty-data channel which fires every time
671 // there are data to read on a given queue.
672 // It is only valid to call this function if memif is in the connected state.
673 // Channel is automatically closed when the connection goes down (but after
674 // the user provided callback OnDisconnect has executed).
675 // Since there is only one interrupt signal sent for an entire burst of packets,
676 // an interrupt handling routine should repeatedly call RxBurst() until
677 // the function returns an empty slice of packets. This way it is ensured
678 // that there are no packets left on the queue unread when the interrupt signal
679 // is cleared.
680 // The method is thread-safe.
681 func (memif *Memif) GetQueueInterruptChan(queueID uint8) (ch <-chan struct{}, err error) {
682         if int(queueID) >= len(memif.queueIntCh) {
683                 return nil, ErrQueueID
684         }
685         return memif.queueIntCh[queueID], nil
686 }
687
688 // SetRxMode allows to switch between the interrupt and the polling mode for Rx.
689 // The method is thread-safe.
690 func (memif *Memif) SetRxMode(queueID uint8, rxMode RxMode) (err error) {
691         var cRxMode C.memif_rx_mode_t
692         switch rxMode {
693         case RxModeInterrupt:
694                 cRxMode = C.MEMIF_RX_MODE_INTERRUPT
695         case RxModePolling:
696                 cRxMode = C.MEMIF_RX_MODE_POLLING
697         default:
698                 cRxMode = C.MEMIF_RX_MODE_INTERRUPT
699         }
700         errCode := C.memif_set_rx_mode(memif.cHandle, cRxMode, C.uint16_t(queueID))
701         return getMemifError(int(errCode))
702 }
703
704 // GetDetails returns a detailed runtime information about this memif.
705 // The method is thread-safe.
706 func (memif *Memif) GetDetails() (details *MemifDetails, err error) {
707         cDetails := C.govpp_memif_details_t{}
708         var buf *C.char
709
710         // Get memif details from C-libmemif.
711         errCode := C.govpp_memif_get_details(memif.cHandle, &cDetails, &buf)
712         err = getMemifError(int(errCode))
713         if err != nil {
714                 return nil, err
715         }
716         defer C.free(unsafe.Pointer(buf))
717
718         // Convert details from C to Go.
719         details = &MemifDetails{}
720         // - metadata:
721         details.IfName = C.GoString(cDetails.if_name)
722         details.InstanceName = C.GoString(cDetails.inst_name)
723         details.ConnID = uint32(cDetails.id)
724         details.SocketFilename = C.GoString(cDetails.socket_filename)
725         if cDetails.secret != nil {
726                 details.Secret = C.GoString(cDetails.secret)
727         }
728         details.IsMaster = cDetails.role == C.uint8_t(0)
729         switch cDetails.mode {
730         case C.MEMIF_INTERFACE_MODE_ETHERNET:
731                 details.Mode = IfModeEthernet
732         case C.MEMIF_INTERFACE_MODE_IP:
733                 details.Mode = IfModeIP
734         case C.MEMIF_INTERFACE_MODE_PUNT_INJECT:
735                 details.Mode = IfModePuntInject
736         default:
737                 details.Mode = IfModeEthernet
738         }
739         // - connection details:
740         details.RemoteIfName = C.GoString(cDetails.remote_if_name)
741         details.RemoteInstanceName = C.GoString(cDetails.remote_inst_name)
742         details.HasLink = cDetails.link_up_down == C.uint8_t(1)
743         // - RX queues:
744         var i uint8
745         for i = 0; i < uint8(cDetails.rx_queues_num); i++ {
746                 cRxQueue := C.govpp_get_rx_queue_details(&cDetails, C.int(i))
747                 queueDetails := MemifQueueDetails{
748                         QueueID:    uint8(cRxQueue.qid),
749                         RingSize:   uint32(cRxQueue.ring_size),
750                         BufferSize: uint16(cRxQueue.buffer_size),
751                 }
752                 details.RxQueues = append(details.RxQueues, queueDetails)
753         }
754         // - TX queues:
755         for i = 0; i < uint8(cDetails.tx_queues_num); i++ {
756                 cTxQueue := C.govpp_get_tx_queue_details(&cDetails, C.int(i))
757                 queueDetails := MemifQueueDetails{
758                         QueueID:    uint8(cTxQueue.qid),
759                         RingSize:   uint32(cTxQueue.ring_size),
760                         BufferSize: uint16(cTxQueue.buffer_size),
761                 }
762                 details.TxQueues = append(details.TxQueues, queueDetails)
763         }
764
765         return details, nil
766 }
767
768 // TxBurst is used to send multiple packets in one call into a selected queue.
769 // The actual number of packets sent may be smaller and is returned as <count>.
770 // The method is non-blocking even if the ring is full and no packet can be sent.
771 // It is only valid to call this function if memif is in the connected state.
772 // Multiple TxBurst-s can run concurrently provided that each targets a different
773 // TX queue.
774 func (memif *Memif) TxBurst(queueID uint8, packets []RawPacketData) (count uint16, err error) {
775         if len(packets) == 0 {
776                 return 0, nil
777         }
778
779         if int(queueID) >= len(memif.txQueueBufs) {
780                 return 0, ErrQueueID
781         }
782
783         var bufCount int
784         buffers := make([]*txPacketBuffer, 0)
785         cQueueID := C.uint16_t(queueID)
786
787         for _, packet := range packets {
788                 packetLen := len(packet)
789                 log.Debugf("%v - preparing packet with len %v", cQueueID, packetLen)
790
791                 if packetLen > memif.bufferSize {
792                         // Create jumbo buffer
793                         buffer := &txPacketBuffer{
794                                 size:    packetLen,
795                                 packets: []RawPacketData{packet},
796                         }
797
798                         buffers = append(buffers, buffer)
799
800                         // Increment bufCount by number of splits in this jumbo
801                         bufCount += (buffer.size + memif.bufferSize - 1) / memif.bufferSize
802                 } else {
803                         buffersLen := len(buffers)
804
805                         // This is very first buffer so there is no data to append to, prepare empty one
806                         if buffersLen == 0 {
807                                 buffers = []*txPacketBuffer{{}}
808                                 buffersLen = 1
809                         }
810
811                         lastBuffer := buffers[buffersLen-1]
812
813                         // Last buffer is jumbo buffer, create new buffer
814                         if lastBuffer.size > memif.bufferSize {
815                                 lastBuffer = &txPacketBuffer{}
816                                 buffers = append(buffers, lastBuffer)
817                         }
818
819                         // Determine buffer size by max packet size in buffer
820                         if packetLen > lastBuffer.size {
821                                 lastBuffer.size = packetLen
822                         }
823
824                         lastBuffer.packets = append(lastBuffer.packets, packet)
825                         bufCount += 1
826                 }
827         }
828
829         // Reallocate Tx buffers if needed to fit the input packets.
830         log.Debugf("%v - total buffer to allocate count %v", cQueueID, bufCount)
831         pb := &memif.txQueueBufs[queueID]
832         if pb.count < bufCount {
833                 newBuffers := C.realloc(unsafe.Pointer(pb.buffers), C.size_t(bufCount*int(C.sizeof_memif_buffer_t)))
834                 if newBuffers == nil {
835                         // Realloc failed, <count> will be less than len(packets).
836                         bufCount = pb.count
837                 } else {
838                         pb.buffers = (*C.memif_buffer_t)(newBuffers)
839                         pb.count = bufCount
840                 }
841         }
842
843         // Allocate ring slots.
844         var allocated C.uint16_t
845         var subCount C.uint16_t
846         for _, buffer := range buffers {
847                 packetCount := C.uint16_t(len(buffer.packets))
848                 isJumbo := buffer.size > memif.bufferSize
849
850                 log.Debugf("%v - trying to send max buff size %v, packets len %v, buffer len %v, jumbo %v",
851                         cQueueID, buffer.size, len(buffer.packets), packetCount, isJumbo)
852
853                 var nextFreeBuff *C.memif_buffer_t
854                 startOffset := allocated
855                 errCode := C.govpp_memif_buffer_alloc(memif.cHandle, cQueueID, pb.buffers, startOffset, &nextFreeBuff,
856                         packetCount, &allocated, C.uint16_t(buffer.size))
857
858                 err = getMemifError(int(errCode))
859                 endEarly := err == ErrNoBufRing
860                 if endEarly {
861                         // Not enough ring slots, <count> will be less than packetCount.
862                         err = nil
863                 }
864                 if err != nil {
865                         return 0, err
866                 }
867
868                 // Copy packet data into the buffers.
869                 nowAllocated := allocated - startOffset
870                 toFill := nowAllocated
871                 if !isJumbo {
872                         // If this is not jumbo frame, only 1 packet needs to be copied each iteration
873                         toFill = 1
874                 }
875
876                 // Iterate over all packets and try to fill them into allocated buffers
877                 // If packet is jumbo frame, continue filling to allocated buffers until no buffer is left
878                 for i, packet := range buffer.packets {
879                         if i >= int(nowAllocated) {
880                                 // There was less allocated buffers than actual packet count so exit early
881                                 break
882                         }
883
884                         packetData := unsafe.Pointer(&packet[0])
885                         C.govpp_copy_packet_data(nextFreeBuff, toFill, C.int(i), packetData, C.uint16_t(len(packet)))
886                 }
887
888                 if isJumbo && nowAllocated > 0 {
889                         // If we successfully allocated required amount of buffers for entire jumbo to be sent
890                         // simply sub entire amount of jumbo frame packets and leave only 1 so sender will think
891                         // it only sent 1 packet so it does not need to know anything about jumbo frames
892                         subCount += nowAllocated - 1
893                 }
894
895                 // If we do not have enough buffers left to allocate, simply end here to avoid packet loss and try
896                 // to handle it next burst
897                 if endEarly {
898                         break
899                 }
900         }
901
902         var sentCount C.uint16_t
903         errCode := C.memif_tx_burst(memif.cHandle, cQueueID, pb.buffers, allocated, &sentCount)
904         err = getMemifError(int(errCode))
905         if err != nil {
906                 return 0, err
907         }
908
909         // Prevent negative values
910         realSent := uint16(sentCount) - uint16(subCount)
911         if subCount > sentCount {
912                 sentCount = 0
913         }
914
915         log.Debugf("%v - sent %v total allocated buffs %v", cQueueID, sentCount, allocated)
916         return realSent, nil
917 }
918
919 // RxBurst is used to receive multiple packets in one call from a selected queue.
920 // <count> is the number of packets to receive. The actual number of packets
921 // received may be smaller. <count> effectively limits the maximum number
922 // of packets to receive in one burst (for a flat, predictable memory usage).
923 // The method is non-blocking even if there are no packets to receive.
924 // It is only valid to call this function if memif is in the connected state.
925 // Multiple RxBurst-s can run concurrently provided that each targets a different
926 // Rx queue.
927 func (memif *Memif) RxBurst(queueID uint8, count uint16) (packets []RawPacketData, err error) {
928         var recvCount C.uint16_t
929         packets = make([]RawPacketData, 0)
930
931         if count == 0 {
932                 return packets, nil
933         }
934
935         if int(queueID) >= len(memif.rxQueueBufs) {
936                 return packets, ErrQueueID
937         }
938
939         // Reallocate Rx buffers if needed to fit the output packets.
940         pb := &memif.rxQueueBufs[queueID]
941         bufCount := int(count)
942         if pb.count < bufCount {
943                 newBuffers := C.realloc(unsafe.Pointer(pb.buffers), C.size_t(bufCount*int(C.sizeof_memif_buffer_t)))
944                 if newBuffers == nil {
945                         // Realloc failed, len(<packets>) will be certainly less than <count>.
946                         bufCount = pb.count
947                 } else {
948                         pb.buffers = (*C.memif_buffer_t)(newBuffers)
949                         pb.count = bufCount
950                 }
951         }
952
953         cQueueID := C.uint16_t(queueID)
954         errCode := C.memif_rx_burst(memif.cHandle, cQueueID, pb.buffers, C.uint16_t(bufCount), &recvCount)
955         err = getMemifError(int(errCode))
956         if err == ErrNoBuf {
957                 // More packets to read - the user is expected to run RxBurst() until there
958                 // are no more packets to receive.
959                 err = nil
960         }
961         if err != nil {
962                 return packets, err
963         }
964
965         chained := len(pb.rxChainBuf) > 0
966         if chained {
967                 // We had stored data from previous burst because last buffer in previous burst was chained
968                 // so we need to continue appending to this data
969                 packets = pb.rxChainBuf
970                 pb.rxChainBuf = nil
971         }
972
973         // Copy packet data into the instances of RawPacketData.
974         for i := 0; i < int(recvCount); i++ {
975                 var packetSize C.int
976                 packetData := C.govpp_get_packet_data(pb.buffers, C.int(i), &packetSize)
977                 packetBytes := C.GoBytes(packetData, packetSize)
978
979                 if chained {
980                         // We have chained buffers, so start merging packet data with last read packet
981                         prevPacket := packets[len(packets)-1]
982                         packets[len(packets)-1] = append(prevPacket, packetBytes...)
983                 } else {
984                         packets = append(packets, packetBytes)
985                 }
986
987                 // Mark last buffer as chained based on property on current buffer so next buffers
988                 // will try to append data to this one in case we got jumbo frame
989                 chained = C.govpp_is_buffer_chained(pb.buffers, C.int(i)) > 0
990         }
991
992         if recvCount > 0 {
993                 errCode = C.memif_refill_queue(memif.cHandle, cQueueID, recvCount, 0)
994         }
995         err = getMemifError(int(errCode))
996         if err != nil {
997                 // Throw away packets to avoid duplicities.
998                 packets = nil
999         }
1000
1001         if chained {
1002                 // We did not had enough space to process all chained buffers to the end so simply tell
1003                 // reader that it should not process any packets here and save them for next burst
1004                 // to finish reading the buffer chain
1005                 pb.rxChainBuf = packets
1006                 packets = nil
1007                 err = ErrNoBuf
1008         }
1009
1010         return packets, err
1011 }
1012
1013 // Close removes the memif interface. If the memif is in the connected state,
1014 // the connection is first properly closed.
1015 // Do not access memif after it is closed, let garbage collector to remove it.
1016 func (memif *Memif) Close() error {
1017         log.WithField("ifName", memif.IfName).Debug("Closing the memif interface")
1018
1019         // Delete memif from C-libmemif.
1020         err := getMemifError(int(C.memif_delete(&memif.cHandle)))
1021
1022         if err != nil {
1023                 // Close memif-global interrupt channel.
1024                 close(memif.intCh)
1025                 // Close file descriptor stopQPollFd.
1026                 C.close(C.int(memif.stopQPollFd))
1027         }
1028
1029         context.lock.Lock()
1030         defer context.lock.Unlock()
1031         // Unregister the interface from the context.
1032         delete(context.memifs, memif.ifIndex)
1033         log.WithField("ifName", memif.IfName).Debug("memif interface was closed")
1034
1035         return err
1036 }
1037
1038 // initQueues allocates resources associated with Rx/Tx queues.
1039 func (memif *Memif) initQueues() error {
1040         // Get Rx/Tx queues count.
1041         details, err := memif.GetDetails()
1042         if err != nil {
1043                 return err
1044         }
1045
1046         log.WithFields(logger.Fields{
1047                 "ifName":   memif.IfName,
1048                 "Rx-count": len(details.RxQueues),
1049                 "Tx-count": len(details.TxQueues),
1050         }).Debug("Initializing Rx/Tx queues.")
1051
1052         // Initialize interrupt channels.
1053         var i int
1054         for i = 0; i < len(details.RxQueues); i++ {
1055                 queueIntCh := make(chan struct{}, 1)
1056                 memif.queueIntCh = append(memif.queueIntCh, queueIntCh)
1057         }
1058
1059         // Initialize Rx/Tx packet buffers.
1060         for i = 0; i < len(details.RxQueues); i++ {
1061                 memif.rxQueueBufs = append(memif.rxQueueBufs, CPacketBuffers{})
1062                 if !memif.IsMaster {
1063                         errCode := C.memif_refill_queue(memif.cHandle, C.uint16_t(i), C.uint16_t(memif.ringSize-1), 0)
1064                         err = getMemifError(int(errCode))
1065                         if err != nil {
1066                                 log.Warn(err.Error())
1067                         }
1068                 }
1069         }
1070         for i = 0; i < len(details.TxQueues); i++ {
1071                 memif.txQueueBufs = append(memif.txQueueBufs, CPacketBuffers{})
1072         }
1073
1074         return nil
1075 }
1076
1077 // closeQueues deallocates all resources associated with Rx/Tx queues.
1078 func (memif *Memif) closeQueues() {
1079         log.WithFields(logger.Fields{
1080                 "ifName":   memif.IfName,
1081                 "Rx-count": len(memif.rxQueueBufs),
1082                 "Tx-count": len(memif.txQueueBufs),
1083         }).Debug("Closing Rx/Tx queues.")
1084
1085         // Close interrupt channels.
1086         for _, ch := range memif.queueIntCh {
1087                 close(ch)
1088         }
1089         memif.queueIntCh = nil
1090
1091         // Deallocate Rx/Tx packet buffers.
1092         for _, pb := range memif.rxQueueBufs {
1093                 C.free(unsafe.Pointer(pb.buffers))
1094         }
1095         memif.rxQueueBufs = nil
1096         for _, pb := range memif.txQueueBufs {
1097                 C.free(unsafe.Pointer(pb.buffers))
1098         }
1099         memif.txQueueBufs = nil
1100 }
1101
1102 // pollEvents repeatedly polls for a libmemif event.
1103 func pollEvents() {
1104         defer context.wg.Done()
1105         for {
1106                 errCode := C.memif_poll_event(C.int(-1))
1107                 err := getMemifError(int(errCode))
1108                 if err == ErrPollCanceled {
1109                         return
1110                 }
1111         }
1112 }
1113
1114 // pollRxQueue repeatedly polls an Rx queue for interrupts.
1115 func pollRxQueue(memif *Memif, queueID uint8) {
1116         defer memif.wg.Done()
1117
1118         log.WithFields(logger.Fields{
1119                 "ifName":   memif.IfName,
1120                 "queue-ID": queueID,
1121         }).Debug("Started queue interrupt polling.")
1122
1123         var qfd C.int
1124         errCode := C.memif_get_queue_efd(memif.cHandle, C.uint16_t(queueID), &qfd)
1125         err := getMemifError(int(errCode))
1126         if err != nil {
1127                 log.WithField("err", err).Error("memif_get_queue_efd() failed")
1128                 return
1129         }
1130
1131         // Create epoll file descriptor.
1132         var event [1]syscall.EpollEvent
1133         epFd, err := syscall.EpollCreate1(0)
1134         if err != nil {
1135                 log.WithField("err", err).Error("epoll_create1() failed")
1136                 return
1137         }
1138         defer syscall.Close(epFd)
1139
1140         // Add Rx queue interrupt file descriptor.
1141         event[0].Events = syscall.EPOLLIN
1142         event[0].Fd = int32(qfd)
1143         if err = syscall.EpollCtl(epFd, syscall.EPOLL_CTL_ADD, int(qfd), &event[0]); err != nil {
1144                 log.WithField("err", err).Error("epoll_ctl() failed")
1145                 return
1146         }
1147
1148         // Add file descriptor used to stop this go routine.
1149         event[0].Events = syscall.EPOLLIN
1150         event[0].Fd = int32(memif.stopQPollFd)
1151         if err = syscall.EpollCtl(epFd, syscall.EPOLL_CTL_ADD, memif.stopQPollFd, &event[0]); err != nil {
1152                 log.WithField("err", err).Error("epoll_ctl() failed")
1153                 return
1154         }
1155
1156         // Poll for interrupts.
1157         for {
1158                 _, err := syscall.EpollWait(epFd, event[:], -1)
1159                 if err != nil {
1160                         log.WithField("err", err).Error("epoll_wait() failed")
1161                         return
1162                 }
1163
1164                 // Handle Rx Interrupt.
1165                 if event[0].Fd == int32(qfd) {
1166                         // Consume the interrupt event.
1167                         buf := make([]byte, 8)
1168                         _, err = syscall.Read(int(qfd), buf[:])
1169                         if err != nil {
1170                                 log.WithField("err", err).Warn("read() failed")
1171                         }
1172
1173                         // Send signal to memif-global interrupt channel.
1174                         select {
1175                         case memif.intCh <- queueID:
1176                                 break
1177                         default:
1178                                 break
1179                         }
1180
1181                         // Send signal to queue-specific interrupt channel.
1182                         select {
1183                         case memif.queueIntCh[queueID] <- struct{}{}:
1184                                 break
1185                         default:
1186                                 break
1187                         }
1188                 }
1189
1190                 // Stop the go routine if requested.
1191                 if event[0].Fd == int32(memif.stopQPollFd) {
1192                         log.WithFields(logger.Fields{
1193                                 "ifName":   memif.IfName,
1194                                 "queue-ID": queueID,
1195                         }).Debug("Stopped queue interrupt polling.")
1196                         return
1197                 }
1198         }
1199 }
1200
1201 //export go_on_connect_callback
1202 func go_on_connect_callback(privateCtx unsafe.Pointer) C.int {
1203         log.Debug("go_on_connect_callback BEGIN")
1204         defer log.Debug("go_on_connect_callback END")
1205         context.lock.RLock()
1206         defer context.lock.RUnlock()
1207
1208         // Get memif reference.
1209         ifIndex := int(uintptr(privateCtx))
1210         memif, exists := context.memifs[ifIndex]
1211         if !exists {
1212                 return C.int(ErrNoConn.Code())
1213         }
1214
1215         // Initialize Rx/Tx queues.
1216         err := memif.initQueues()
1217         if err != nil {
1218                 if memifErr, ok := err.(*MemifError); ok {
1219                         return C.int(memifErr.Code())
1220                 }
1221                 return C.int(ErrUnknown.Code())
1222         }
1223
1224         // Call the user callback.
1225         if memif.callbacks.OnConnect != nil {
1226                 memif.callbacks.OnConnect(memif)
1227         }
1228
1229         // Start polling the RX queues for interrupts.
1230         for i := 0; i < len(memif.queueIntCh); i++ {
1231                 memif.wg.Add(1)
1232                 go pollRxQueue(memif, uint8(i))
1233         }
1234
1235         return C.int(0)
1236 }
1237
1238 //export go_on_disconnect_callback
1239 func go_on_disconnect_callback(privateCtx unsafe.Pointer) C.int {
1240         log.Debug("go_on_disconnect_callback BEGIN")
1241         defer log.Debug("go_on_disconnect_callback END")
1242         context.lock.RLock()
1243         defer context.lock.RUnlock()
1244
1245         // Get memif reference.
1246         ifIndex := int(uintptr(privateCtx))
1247         memif, exists := context.memifs[ifIndex]
1248         if !exists {
1249                 // Already closed.
1250                 return C.int(0)
1251         }
1252
1253         // Stop polling the RX queues for interrupts.
1254         buf := make([]byte, 8)
1255         binary.PutUvarint(buf, 1)
1256         // - add an event
1257         _, err := syscall.Write(memif.stopQPollFd, buf[:])
1258         if err != nil {
1259                 return C.int(ErrSyscall.Code())
1260         }
1261         // - wait
1262         memif.wg.Wait()
1263         // - remove the event
1264         _, err = syscall.Read(memif.stopQPollFd, buf[:])
1265         if err != nil {
1266                 return C.int(ErrSyscall.Code())
1267         }
1268
1269         // Call the user callback.
1270         if memif.callbacks.OnDisconnect != nil {
1271                 memif.callbacks.OnDisconnect(memif)
1272         }
1273
1274         // Close Rx/Tx queues.
1275         memif.closeQueues()
1276
1277         return C.int(0)
1278 }