3b692b1631987e7e517259d14813f6644e8b27bc
[govpp.git] / extras / libmemif / README.md
1 ## Go-libmemif
2
3 Package **libmemif** is a Golang adapter for the **libmemif library**
4 (`extras/libmemif` in the [VPP](https://wiki.fd.io/view/VPP) repository).
5 To differentiate between the adapter and the underlying C-written library,
6 labels `Go-libmemif` and `C-libmemif` are used in the documentation.
7
8 ### Requirements
9
10 libmemif for Golang is build on the top of the original, C-written
11 libmemif library using `cgo`. It is therefore necessary to have C-libmemif
12 header files and the library itself installed in locations known
13 to the compiler.
14
15 For example, to install C-libmemif system-wide into the standard
16 locations, execute:
17 ```
18 $ git clone https://gerrit.fd.io/r/vpp
19 $ cd vpp/extras/libmemif
20 $ ./bootstrap
21 $ ./configure
22 $ make install
23 ```
24
25 ### Build
26
27 Package **libmemif** is not part of the **GoVPP** core and as such it is
28 not included in the [make build](../../Makefile) target.
29 Instead, it has its own target in the [top-level Makefile](../../Makefile)
30 used to build the attached examples with the adapter:
31 ```
32 $ make extras
33 ```
34
35 ### APIs
36
37 All **Go-libmemif** public APIs can be found in [adapter.go](adapter.go).
38 Please see the comments for a more detailed description.
39 Additionally, a list of all errors thrown by libmemif can be found
40 in [error.go](error.go).
41
42 ### Usage
43
44 **libmemif** needs to be first initialized with `Init(appName)`.
45 This has to be done only once in the context of the entire process.
46 Make sure to call `Cleanup()` to release all the resources allocated
47 by **libmemif** before exiting your application. Consider calling
48 `Init()` followed by `Cleanup()` scheduled with `defer` in the `main()`
49 function.
50
51 Log messages are by default printed to stdout. Use `SetLogger()` to use
52 your own customized logger (can be changed before `Init()`).
53
54 Once **libmemif** is initialized, new memif interfaces can be created
55 with `CreateInterface(config, callbacks)`. See `MemifConfig` structure
56 definition to learn about possible memif configuration options.
57 If successful, `CreateInterface()` returns an instance of `Memif`
58 structure representing the underlying memif interface.
59
60 Callbacks are optional and can be shared across multiple memif instances.
61 Available callbacks are:
62 1. **OnConnect**: called when the connection is established.
63    By the time the callback is called, the Rx/Tx queues are initialized
64    and ready for data transmission. Interrupt channels are also
65    created and ready to be read from.
66    The user is expected to start polling for input packets via repeated
67    calls to `Memif.RxBurst(queueID, count)` or to initiate select
68    on the interrupt channels obtained with `Get*InterruptChan()`,
69    depending on the Rx mode. By default, all memif Rx queues are created
70    in the interrupt mode, but this can be changed per-queue with
71    `Memif.SetRxMode(queueID, mode)`.
72 2. **OnDisconnect**: called after the connection was closed. Immediately
73    after the user callback returns, Rx/Tx queues and interrupt channels
74    are also deallocated. The user defined callback should therefore ensure
75    that all the Rx/Tx operations are stopped before it returns.
76
77 **libmemif** was designed for a maximum possible performance. Packets
78 are sent and received in bulks, rather than one-by-one, using
79 `Memif.TxBurst(queueID, packets)` and `Memif.RxBurst(queueID, count)`,
80 respectively. Memif connection can consists of multiple queues in both
81 directions. A queue is one-directional wait-free ring buffer.
82 It is the unit of parallelism for data transmission. The maximum possible
83 lock-free granularity is therefore one go routine for one queue.
84
85 Interrupt channel for one specific Rx queue can be obtained with
86 `GetQueueInterruptChan(queueID)` as opposed to `GetInterruptChan()`
87 for all the Rx queues. There is only one interrupt signal sent for
88 an entire burst of packets, therefore an interrupt handling routine
89 should repeatedly call RxBurst() until an empty slice of packets
90 is returned. This way it is ensured that there are no packets left
91 on the queue unread when the interrupt signal is cleared.
92 Study the `ReadAndPrintPackets()` function in [raw-data example](examples/raw-data/raw-data.go).
93
94 For **libmemif** the packet is just an array of bytes. It does not care
95 what the actual content is. It is not required for a packet to follow
96 any network protocol in order to get transported from one end to another.
97 See the type declaration for `RawPacketData` and its use in `Memif.TxBurst()`
98 and `Memif.RxBurst()`.
99
100 In order to remove a memif interface, call `Memif.Close()`. If the memif
101 is in the connected state, the connection is first properly closed.
102 Do not touch memif after it was closed, let garbage collector to remove
103 the `Memif` instance. In the end, `Cleanup()` will also ensure that all
104 active memif interfaces are closed before the cleanup finalizes.
105
106 ### Examples
107
108 **Go-libmemif** ships with two simple examples demonstrating the usage
109 of the package with a detailed commentary.
110 The examples can be found in the subdirectory [examples](./examples).
111
112 #### Raw data (libmemif <-> libmemif)
113
114 *raw-data* is a basic example showing how to create a memif interface,
115 handle events through callbacks and perform Rx/Tx of raw data. Before
116 handling an actual packets it is important to understand the skeleton
117 of libmemif-based applications.
118
119 Since VPP expects proper packet data, it is not very useful to connect
120 *raw-data* example with VPP, even though it will work, since all
121 the received data will get dropped on the VPP side.
122
123 To create a connection of two raw-data instances, start two processes
124 concurrently in an arbitrary order:
125  - *master* memif:
126    ```
127    $ cd extras/libmemif/examples/raw-data
128    $ ./raw-data
129    ```
130  - *slave* memif:
131    ```
132    $ cd extras/libmemif/examples/raw-data
133    $ ./raw-data --slave
134    ```
135
136 Every 3 seconds both sides send 3 raw-data packets to the opposite end
137 through each of the 3 queues. The received packets are printed to stdout.
138
139 Stop an instance of *raw-data* with an interrupt signal (^C).
140
141 #### ICMP Responder
142
143 *icmp-responder* is a simple example showing how to answer APR and ICMP
144 echo requests through a memif interface. Package `google/gopacket` is
145 used to decode and construct packets.
146
147 The appropriate VPP configuration for the opposite memif is:
148 ```
149 vpp$ create memif socket id 1 filename /tmp/icmp-responder-example
150 vpp$ create interface memif id 1 socket-id 1 slave secret secret no-zero-copy
151 vpp$ set int state memif1/1 up
152 vpp$ set int ip address memif1/1 192.168.1.2/24
153 ```
154
155 To start the example, simply type:
156 ```
157 root$ ./icmp-responder
158 ```
159
160 *icmp-responder* needs to be run as root so that it can access the socket
161 created by VPP.
162
163 Normally, the memif interface is in the master mode. Pass CLI flag `--slave`
164 to create memif in the slave mode:
165 ```
166 root$ ./icmp-responder --slave
167 ```
168
169 Don't forget to put the opposite memif into the master mode in that case.
170
171 To verify the connection, run:
172 ```
173 vpp$ ping 192.168.1.1
174 64 bytes from 192.168.1.1: icmp_seq=2 ttl=255 time=.6974 ms
175 64 bytes from 192.168.1.1: icmp_seq=3 ttl=255 time=.6310 ms
176 64 bytes from 192.168.1.1: icmp_seq=4 ttl=255 time=1.0350 ms
177 64 bytes from 192.168.1.1: icmp_seq=5 ttl=255 time=.5359 ms
178
179 Statistics: 5 sent, 4 received, 20% packet loss
180 vpp$ sh ip arp
181     Time           IP4       Flags      Ethernet              Interface
182     68.5648   192.168.1.1     D    aa:aa:aa:aa:aa:aa memif0/1
183 ```
184 *Note*: it is expected that the first ping is shown as lost.
185         It was actually converted to an ARP request. This is a VPP
186         specific feature common to all interface types.
187
188 Stop the example with an interrupt signal (^C).