1 ## Getting started {#libmemif_gettingstarted_doc}
3 #### Concept (Connecting to VPP)
5 For detailed information on api calls and structures please refer to @ref libmemif.h.
8 - Declare callback function handling file descriptor event polling.
11 control_fd_update (int fd, uint8_t events)
16 - Call memif initialization function. memif\_init
18 err = memif_init (control_fd_update, APP_NAME, NULL, NULL);
21 > If event occurres on any file descriptor returned by this callback, call memif\_control\_fd\_handler function. Since version 2.0, last two optional arguments are used to specify custom memory allocation.
23 memif_err = memif_control_fd_handler (evt.data.fd, events);
25 > If callback function parameter for memif\_init function is set to NULL, libmemif will handle file descriptor event polling.
26 Api call memif\_poll\_event will call epoll\_pwait with user defined timeout to poll event on file descriptors opened by libmemif.
31 if (memif_poll_event (-1) < 0)
33 DBG ("poll_event error!");
38 > Memif initialization function will initialize internal structures and create timer file descriptor, which will be used for sending periodic connection requests. Timer is disarmed if no memif interface is created.
41 - Declare memif connction handle.
43 memif_conn_handle_t c;
45 > example app uses struct that contains connection handle, rx/tx buffers and other connection specific information.
47 - Specify connection arguments.
49 memif_conn_args_t args;
50 memset (&args, 0, sizeof (args));
51 args.is_master = is_master;
52 args.log2_ring_size = 10;
53 args.buffer_size = 2048;
54 args.num_s2m_rings = 2;
55 args.num_m2s_rings = 2;
56 strncpy ((char *) args.interface_name, IF_NAME, strlen (IF_NAME));
58 args.interface_id = 0;
60 - Declare callback functions called on connected/disconnected/interrupted status changed.
63 on_connect (memif_conn_handle_t conn, void *private_ctx)
69 on_disconnect (memif_conn_handle_t conn, void *private_ctx)
71 INFO ("memif connected!");
75 - Call memif interface create function. memif\_create
77 err = memif_create (&c->conn,
78 &args, on_connect, on_disconnect, on_interrupt, &ctx[index]);
80 > If connection is in slave mode, arms timer file descriptor.
81 > If on interrupt callback is set to NULL, user will not be notified about interrupt. Use memif\_get\_queue\_efd call to get interrupt file descriptor for specific queue.
84 err = memif_get_queue_efd (c->conn, data->qid, &fd);
87 3. Connection establishment
88 - User application will poll events on all file descriptors returned in memif\_control\_fd\_update\_t callback.
89 - On event call memif\_control\_fd\_handler.
90 - Everything else regarding connection establishment will be done internally.
91 - Once connection has been established, a callback will inform the user about connection status change.
93 4. Interrupt packet receive
94 - If event is polled on interrupt file descriptor, libmemif will call memif\_interrupt\_t callback specified for every connection instance.
97 on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
104 - Packet data are stored in memif\_buffer\_t. Pointer _data_ points to shared memory buffer, and unsigned integer *_len* contains buffer length.
105 - flags: MEMIF\_BUFFER\_FLAG\_NEXT states that the buffer is not large enough to contain whole packet, so next buffer contains the rest of the packet. (chained buffers)
117 - Api call memif\_rx\_burst will set all required fields in memif buffers provided by user application and dequeue received buffers.
119 err = memif_rx_burst (c->conn, qid, c->bufs, MAX_MEMIF_BUFS, &rx);
121 - User application can then process packets.
122 - Api call memif\_refill\_queue will enqueue rx buffers.
124 err = memif_refill_queue (c->conn, qid, rx);
128 - Api call memif\_buffer\_alloc will find free tx buffers and set all required fields in memif buffers provided by user application.
130 err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, n, &r);
132 - User application can populate shared memory buffers with packets.
133 - Api call memif\_tx\_burst will enqueue tx buffers
135 err = memif_tx_burst (c->conn, qid, c->tx_bufs, c->tx_buf_num, &r);
141 uint16_t memif_ver = memif_get_version ();
144 - Api call memif\_get\_details will return details about connection.
146 err = memif_get_details (c->conn, &md, buf, buflen);
148 - Memif error messages
149 - Every api call returns error code (integer value) mapped to error string.
150 - Call memif\_strerror will return error message assigned to specific error code.
152 if (err != MEMIF_ERR_SUCCESS)
153 INFO ("memif_get_details: %s", memif_strerror (err));
155 - Not all syscall errors are translated to memif error codes. If error code 1 (MEMIF\_ERR\_SYSCALL) is returned then libmemif needs to be compiled with -DMEMIF_DBG flag to print error message. Use _make -B_ to rebuild libmemif in debug mode.
157 #### Example app (libmemif fd event polling):
159 - @ref extras/libmemif/examples/icmp_responder
161 > Optional argument: transmit queue id.
165 > Set transmit queue id to 1. Default is 0.
166 > Application will create memif interface in slave mode and try to connect to VPP. Exit using Ctrl+C. Application will handle SIGINT signal, free allocated memory and exit with EXIT_SUCCESS.
170 ICMP Responder custom fd event polling.
172 - @ref extras/libmemif/examples/icmp_responder-epoll
174 #### Example app (multi-thread queue polling)
176 ICMP Responder multi-thread.
177 - @ref extras/libmemif/examples/icmp_responder-mt
179 > Simple example of libmemif multi-thread usage. Connection establishment is handled by main thread. There are two rx/tx queues in this example. One in polling mode and second in interrupt mode.
183 # create memif id 0 master
184 # set int state memif0 up
185 # set int ip address memif0 192.168.1.1/24
188 For multiple rings (queues) support run VPP with worker threads:
189 example startup.conf:
203 # create memif id 0 master
204 # set int state memif0 up
205 # set int ip address memif0 192.168.1.1/24
208 > Master mode queue number is limited by worker threads. Slave mode interface needs to specify number of queues.
210 # create memif id 0 slave rx-queues 2 tx-queues 2
212 > Example applications use VPP default socket file for memif: /run/vpp/memif.sock
213 > For master mode, socket directory must exist prior to memif\_create call.
217 Unit tests use [Check](https://libcheck.github.io/check/index.html) framework. This framework must be installed in order to build *unit\_test* binary.
220 sudo apt-get install check
222 [More platforms](https://libcheck.github.io/check/web/install.html)