doc: fix spellings and typos
[dpdk.git] / doc / guides / prog_guide / kernel_nic_interface.rst
1 ..  BSD LICENSE
2     Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
3     All rights reserved.
4
5     Redistribution and use in source and binary forms, with or without
6     modification, are permitted provided that the following conditions
7     are met:
8
9     * Redistributions of source code must retain the above copyright
10     notice, this list of conditions and the following disclaimer.
11     * Redistributions in binary form must reproduce the above copyright
12     notice, this list of conditions and the following disclaimer in
13     the documentation and/or other materials provided with the
14     distribution.
15     * Neither the name of Intel Corporation nor the names of its
16     contributors may be used to endorse or promote products derived
17     from this software without specific prior written permission.
18
19     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20     "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21     LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22     A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23     OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25     LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 Kernel NIC Interface
32 ====================
33
34 The DPDK Kernel NIC Interface (KNI) allows userspace applications access to the Linux* control plane.
35
36 The benefits of using the DPDK KNI are:
37
38 *   Faster than existing Linux TUN/TAP interfaces
39     (by eliminating system calls and copy_to_user()/copy_from_user() operations.
40
41 *   Allows management of DPDK ports using standard Linux net tools such as ethtool, ifconfig and tcpdump.
42
43 *   Allows an interface with the kernel network stack.
44
45 The components of an application using the DPDK Kernel NIC Interface are shown in Figure 17.
46
47 .. _pg_figure_17:
48
49 **Figure 17. Components of a DPDK KNI Application**
50
51 .. image43_png has been renamed
52
53 |kernel_nic_intf|
54
55 The DPDK KNI Kernel Module
56 --------------------------
57
58 The KNI kernel loadable module provides support for two types of devices:
59
60 *   A Miscellaneous device (/dev/kni) that:
61
62     *   Creates net devices (via ioctl  calls).
63
64     *   Maintains a kernel thread context shared by all KNI instances
65         (simulating the RX side of the net driver).
66
67     *   For single kernel thread mode, maintains a kernel thread context shared by all KNI instances
68         (simulating the RX side of the net driver).
69
70     *   For multiple kernel thread mode, maintains a kernel thread context for each KNI instance
71         (simulating the RX side of the new driver).
72
73 *   Net device:
74
75     *   Net functionality provided by implementing several operations such as netdev_ops,
76         header_ops, ethtool_ops that are defined by struct net_device,
77         including support for DPDK mbufs and FIFOs.
78
79     *   The interface name is provided from userspace.
80
81     *   The MAC address can be the real NIC MAC address or random.
82
83 KNI Creation and Deletion
84 -------------------------
85
86 The KNI interfaces are created by a DPDK application dynamically.
87 The interface name and FIFO details are provided by the application through an ioctl call
88 using the rte_kni_device_info struct which contains:
89
90 *   The interface name.
91
92 *   Physical addresses of the corresponding memzones for the relevant FIFOs.
93
94 *   Mbuf mempool details, both physical and virtual (to calculate the offset for mbuf pointers).
95
96 *   PCI information.
97
98 *   Core affinity.
99
100 Refer to rte_kni_common.h in the DPDK source code for more details.
101
102 The physical addresses will be re-mapped into the kernel address space and stored in separate KNI contexts.
103
104 Once KNI interfaces are created, the KNI context information can be queried by calling the rte_kni_info_get() function.
105
106 The KNI interfaces can be deleted by a DPDK application dynamically after being created.
107 Furthermore, all those KNI interfaces not deleted will be deleted on the release operation
108 of the miscellaneous device (when the DPDK application is closed).
109
110 DPDK mbuf Flow
111 --------------
112
113 To minimize the amount of DPDK code running in kernel space, the mbuf mempool is managed in userspace only.
114 The kernel module will be aware of mbufs,
115 but all mbuf allocation and free operations will be handled by the DPDK application only.
116
117 Figure 18 shows a typical scenario with packets sent in both directions.
118
119 .. _pg_figure_18:
120
121 **Figure 18. Packet Flow via mbufs in the DPDK KNI**
122
123 .. image44_png has been renamed
124
125 |pkt_flow_kni|
126
127 Use Case: Ingress
128 -----------------
129
130 On the DPDK RX side, the mbuf is allocated by the PMD in the RX thread context.
131 This thread will enqueue the mbuf in the rx_q FIFO.
132 The KNI thread will poll all KNI active devices for the rx_q.
133 If an mbuf is dequeued, it will be converted to a sk_buff and sent to the net stack via netif_rx().
134 The dequeued mbuf must be freed, so the same pointer is sent back in the free_q FIFO.
135
136 The RX thread, in the same main loop, polls this FIFO and frees the mbuf after dequeuing it.
137
138 Use Case: Egress
139 ----------------
140
141 For packet egress the DPDK application must first enqueue several mbufs to create an mbuf cache on the kernel side.
142
143 The packet is received from the Linux net stack, by calling the kni_net_tx() callback.
144 The mbuf is dequeued (without waiting due the cache) and filled with data from sk_buff.
145 The sk_buff is then freed and the mbuf sent in the tx_q FIFO.
146
147 The DPDK TX thread dequeues the mbuf and sends it to the PMD (via rte_eth_tx_burst()).
148 It then puts the mbuf back in the cache.
149
150 Ethtool
151 -------
152
153 Ethtool is a Linux-specific tool with corresponding support in the kernel
154 where each net device must register its own callbacks for the supported operations.
155 The current implementation uses the igb/ixgbe modified Linux drivers for ethtool support.
156 Ethtool is not supported in i40e and VMs (VF or EM devices).
157
158 Link state and MTU change
159 -------------------------
160
161 Link state and MTU change are network interface specific operations usually done via ifconfig.
162 The request is initiated from the kernel side (in the context of the ifconfig process)
163 and handled by the user space DPDK application.
164 The application polls the request, calls the application handler and returns the response back into the kernel space.
165
166 The application handlers can be registered upon interface creation or explicitly registered/unregistered in runtime.
167 This provides flexibility in multiprocess scenarios
168 (where the KNI is created in the primary process but the callbacks are handled in the secondary one).
169 The constraint is that a single process can register and handle the requests.
170
171 KNI Working as a Kernel vHost Backend
172 -------------------------------------
173
174 vHost is a kernel module usually working as the backend of virtio (a para- virtualization driver framework)
175 to accelerate the traffic from the guest to the host.
176 The DPDK Kernel NIC interface provides the ability to hookup vHost traffic into userspace DPDK application.
177 Together with the DPDK PMD virtio, it significantly improves the throughput between guest and host.
178 In the scenario where DPDK is running as fast path in the host, kni-vhost is an efficient path for the traffic.
179
180 Overview
181 ~~~~~~~~
182
183 vHost-net has three kinds of real backend implementations. They are: 1) tap, 2) macvtap and 3) RAW socket.
184 The main idea behind kni-vhost is making the KNI work as a RAW socket, attaching it as the backend instance of vHost-net.
185 It is using the existing interface with vHost-net, so it does not require any kernel hacking,
186 and is fully-compatible with the kernel vhost module.
187 As vHost is still taking responsibility for communicating with the front-end virtio,
188 it naturally supports both legacy virtio -net and the DPDK PMD virtio.
189 There is a little penalty that comes from the non-polling mode of vhost.
190 However, it scales throughput well when using KNI in multi-thread mode.
191
192 .. _pg_figure_19:
193
194 **Figure 19. vHost-net Architecture Overview**
195
196 .. image45_png has been renamed
197
198 |vhost_net_arch|
199
200 Packet Flow
201 ~~~~~~~~~~~
202
203 There is only a minor difference from the original KNI traffic flows.
204 On transmit side, vhost kthread calls the RAW socket's ops sendmsg and it puts the packets into the KNI transmit FIFO.
205 On the receive side, the kni kthread gets packets from the KNI receive FIFO, puts them into the queue of the raw socket,
206 and wakes up the task in vhost kthread to begin receiving.
207 All the packet copying, irrespective of whether it is on the transmit or receive side,
208 happens in the context of vhost kthread.
209 Every vhost-net device is exposed to a front end virtio device in the guest.
210
211 .. _pg_figure_20:
212
213 **Figure 20. KNI Traffic Flow**
214
215 .. image46_png  has been renamed
216
217 |kni_traffic_flow|
218
219 Sample Usage
220 ~~~~~~~~~~~~
221
222 Before starting to use KNI as the backend of vhost, the CONFIG_RTE_KNI_VHOST configuration option must be turned on.
223 Otherwise, by default, KNI will not enable its backend support capability.
224
225 Of course, as a prerequisite, the vhost/vhost-net kernel CONFIG should be chosen before compiling the kernel.
226
227 #.  Compile the DPDK and insert uio_pci_generic/igb_uio kernel modules as normal.
228
229 #.  Insert the KNI kernel module:
230
231     .. code-block:: console
232
233         insmod ./rte_kni.ko
234
235     If using KNI in multi-thread mode, use the following command line:
236
237     .. code-block:: console
238
239         insmod ./rte_kni.ko kthread_mode=multiple
240
241 #.  Running the KNI sample application:
242
243     .. code-block:: console
244
245         ./kni -c -0xf0 -n 4 -- -p 0x3 -P -config="(0,4,6),(1,5,7)"
246
247     This command runs the kni sample application with two physical ports.
248     Each port pins two forwarding cores (ingress/egress) in user space.
249
250 #.  Assign a raw socket to vhost-net during qemu-kvm startup.
251     The DPDK does not provide a script to do this since it is easy for the user to customize.
252     The following shows the key steps to launch qemu-kvm with kni-vhost:
253
254     .. code-block:: bash
255
256         #!/bin/bash
257         echo 1 > /sys/class/net/vEth0/sock_en
258         fd=`cat /sys/class/net/vEth0/sock_fd`
259         qemu-kvm \
260         -name vm1 -cpu host -m 2048 -smp 1 -hda /opt/vm-fc16.img \
261         -netdev tap,fd=$fd,id=hostnet1,vhost=on \
262         -device virti-net-pci,netdev=hostnet1,id=net1,bus=pci.0,addr=0x4
263
264 It is simple to enable raw socket using sysfs sock_en and get raw socket fd using sock_fd under the KNI device node.
265
266 Then, using the qemu-kvm command with the -netdev option to assign such raw socket fd as vhost's backend.
267
268 .. note::
269
270     The key word tap must exist as qemu-kvm now only supports vhost with a tap backend, so here we cheat qemu-kvm by an existing fd.
271
272 Compatibility Configure Option
273 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
274
275 There is a CONFIG_RTE_KNI_VHOST_VNET_HDR_EN configuration option in DPDK configuration file.
276 By default, it set to n, which means do not turn on the virtio net header,
277 which is used to support additional features (such as, csum offload, vlan offload, generic-segmentation and so on),
278 since the kni-vhost does not yet support those features.
279
280 Even if the option is turned on, kni-vhost will ignore the information that the header contains.
281 When working with legacy virtio on the guest, it is better to turn off unsupported offload features using ethtool -K.
282 Otherwise, there may be problems such as an incorrect L4 checksum error.
283
284 .. |kni_traffic_flow| image:: img/kni_traffic_flow.*
285
286 .. |vhost_net_arch| image:: img/vhost_net_arch.*
287
288 .. |pkt_flow_kni| image:: img/pkt_flow_kni.*
289
290 .. |kernel_nic_intf| image:: img/kernel_nic_intf.*