1..  SPDX-License-Identifier: BSD-3-Clause
2    Copyright(c) 2010-2015 Intel Corporation.
3
4.. _kni:
5
6Kernel NIC Interface
7====================
8
9The DPDK Kernel NIC Interface (KNI) allows userspace applications access to the Linux* control plane.
10
11The benefits of using the DPDK KNI are:
12
13*   Faster than existing Linux TUN/TAP interfaces
14    (by eliminating system calls and copy_to_user()/copy_from_user() operations.
15
16*   Allows management of DPDK ports using standard Linux net tools such as ethtool, ifconfig and tcpdump.
17
18*   Allows an interface with the kernel network stack.
19
20The components of an application using the DPDK Kernel NIC Interface are shown in :numref:`figure_kernel_nic_intf`.
21
22.. _figure_kernel_nic_intf:
23
24.. figure:: img/kernel_nic_intf.*
25
26   Components of a DPDK KNI Application
27
28
29The DPDK KNI Kernel Module
30--------------------------
31
32The KNI kernel loadable module ``rte_kni`` provides the kernel interface
33for DPDK applications.
34
35When the ``rte_kni`` module is loaded, it will create a device ``/dev/kni``
36that is used by the DPDK KNI API functions to control and communicate with
37the kernel module.
38
39The ``rte_kni`` kernel module contains several optional parameters which
40can be specified when the module is loaded to control its behavior:
41
42.. code-block:: console
43
44    # modinfo rte_kni.ko
45    <snip>
46    parm:           lo_mode: KNI loopback mode (default=lo_mode_none):
47                    lo_mode_none        Kernel loopback disabled
48                    lo_mode_fifo        Enable kernel loopback with fifo
49                    lo_mode_fifo_skb    Enable kernel loopback with fifo and skb buffer
50                     (charp)
51    parm:           kthread_mode: Kernel thread mode (default=single):
52                    single    Single kernel thread mode enabled.
53                    multiple  Multiple kernel thread mode enabled.
54                     (charp)
55    parm:           carrier: Default carrier state for KNI interface (default=off):
56                    off   Interfaces will be created with carrier state set to off.
57                    on    Interfaces will be created with carrier state set to on.
58                     (charp)
59
60Loading the ``rte_kni`` kernel module without any optional parameters is
61the typical way a DPDK application gets packets into and out of the kernel
62network stack.  Without any parameters, only one kernel thread is created
63for all KNI devices for packet receiving in kernel side, loopback mode is
64disabled, and the default carrier state of KNI interfaces is set to *off*.
65
66.. code-block:: console
67
68    # insmod kmod/rte_kni.ko
69
70.. _kni_loopback_mode:
71
72Loopback Mode
73~~~~~~~~~~~~~
74
75For testing, the ``rte_kni`` kernel module can be loaded in loopback mode
76by specifying the ``lo_mode`` parameter:
77
78.. code-block:: console
79
80    # insmod kmod/rte_kni.ko lo_mode=lo_mode_fifo
81
82The ``lo_mode_fifo`` loopback option will loop back ring enqueue/dequeue
83operations in kernel space.
84
85.. code-block:: console
86
87    # insmod kmod/rte_kni.ko lo_mode=lo_mode_fifo_skb
88
89The ``lo_mode_fifo_skb`` loopback option will loop back ring enqueue/dequeue
90operations and sk buffer copies in kernel space.
91
92If the ``lo_mode`` parameter is not specified, loopback mode is disabled.
93
94.. _kni_kernel_thread_mode:
95
96Kernel Thread Mode
97~~~~~~~~~~~~~~~~~~
98
99To provide flexibility of performance, the ``rte_kni`` KNI kernel module
100can be loaded with the ``kthread_mode`` parameter.  The ``rte_kni`` kernel
101module supports two options: "single kernel thread" mode and "multiple
102kernel thread" mode.
103
104Single kernel thread mode is enabled as follows:
105
106.. code-block:: console
107
108    # insmod kmod/rte_kni.ko kthread_mode=single
109
110This mode will create only one kernel thread for all KNI interfaces to
111receive data on the kernel side.  By default, this kernel thread is not
112bound to any particular core, but the user can set the core affinity for
113this kernel thread by setting the ``core_id`` and ``force_bind`` parameters
114in ``struct rte_kni_conf`` when the first KNI interface is created:
115
116For optimum performance, the kernel thread should be bound to a core in
117on the same socket as the DPDK lcores used in the application.
118
119The KNI kernel module can also be configured to start a separate kernel
120thread for each KNI interface created by the DPDK application.  Multiple
121kernel thread mode is enabled as follows:
122
123.. code-block:: console
124
125    # insmod kmod/rte_kni.ko kthread_mode=multiple
126
127This mode will create a separate kernel thread for each KNI interface to
128receive data on the kernel side.  The core affinity of each ``kni_thread``
129kernel thread can be specified by setting the ``core_id`` and ``force_bind``
130parameters in ``struct rte_kni_conf`` when each KNI interface is created.
131
132Multiple kernel thread mode can provide scalable higher performance if
133sufficient unused cores are available on the host system.
134
135If the ``kthread_mode`` parameter is not specified, the "single kernel
136thread" mode is used.
137
138.. _kni_default_carrier_state:
139
140Default Carrier State
141~~~~~~~~~~~~~~~~~~~~~
142
143The default carrier state of KNI interfaces created by the ``rte_kni``
144kernel module is controlled via the ``carrier`` option when the module
145is loaded.
146
147If ``carrier=off`` is specified, the kernel module will leave the carrier
148state of the interface *down* when the interface is management enabled.
149The DPDK application can set the carrier state of the KNI interface using the
150``rte_kni_update_link()`` function.  This is useful for DPDK applications
151which require that the carrier state of the KNI interface reflect the
152actual link state of the corresponding physical NIC port.
153
154If ``carrier=on`` is specified, the kernel module will automatically set
155the carrier state of the interface to *up* when the interface is management
156enabled.  This is useful for DPDK applications which use the KNI interface as
157a purely virtual interface that does not correspond to any physical hardware
158and do not wish to explicitly set the carrier state of the interface with
159``rte_kni_update_link()``.  It is also useful for testing in loopback mode
160where the NIC port may not be physically connected to anything.
161
162To set the default carrier state to *on*:
163
164.. code-block:: console
165
166    # insmod kmod/rte_kni.ko carrier=on
167
168To set the default carrier state to *off*:
169
170.. code-block:: console
171
172    # insmod kmod/rte_kni.ko carrier=off
173
174If the ``carrier`` parameter is not specified, the default carrier state
175of KNI interfaces will be set to *off*.
176
177KNI Creation and Deletion
178-------------------------
179
180Before any KNI interfaces can be created, the ``rte_kni`` kernel module must
181be loaded into the kernel and configured withe ``rte_kni_init()`` function.
182
183The KNI interfaces are created by a DPDK application dynamically via the
184``rte_kni_alloc()`` function.
185
186The ``struct rte_kni_conf`` structure contains fields which allow the
187user to specify the interface name, set the MTU size, set an explicit or
188random MAC address and control the affinity of the kernel Rx thread(s)
189(both single and multi-threaded modes).
190
191The ``struct rte_kni_ops`` structure contains pointers to functions to
192handle requests from the ``rte_kni`` kernel module.  These functions
193allow DPDK applications to perform actions when the KNI interfaces are
194manipulated by control commands or functions external to the application.
195
196For example, the DPDK application may wish to enabled/disable a physical
197NIC port when a user enabled/disables a KNI interface with ``ip link set
198[up|down] dev <ifaceX>``.  The DPDK application can register a callback for
199``config_network_if`` which will be called when the interface management
200state changes.
201
202There are currently four callbacks for which the user can register
203application functions:
204
205``config_network_if``:
206
207    Called when the management state of the KNI interface changes.
208    For example, when the user runs ``ip link set [up|down] dev <ifaceX>``.
209
210``change_mtu``:
211
212    Called when the user changes the MTU size of the KNI
213    interface.  For example, when the user runs ``ip link set mtu <size>
214    dev <ifaceX>``.
215
216``config_mac_address``:
217
218    Called when the user changes the MAC address of the KNI interface.
219    For example, when the user runs ``ip link set address <MAC>
220    dev <ifaceX>``.  If the user sets this callback function to NULL,
221    but sets the ``port_id`` field to a value other than -1, a default
222    callback handler in the rte_kni library ``kni_config_mac_address()``
223    will be called which calls ``rte_eth_dev_default_mac_addr_set()``
224    on the specified ``port_id``.
225
226``config_promiscusity``:
227
228    Called when the user changes the promiscuity state of the KNI
229    interface.  For example, when the user runs ``ip link set promisc
230    [on|off] dev <ifaceX>``. If the user sets this callback function to
231    NULL, but sets the ``port_id`` field to a value other than -1, a default
232    callback handler in the rte_kni library ``kni_config_promiscusity()``
233    will be called which calls ``rte_eth_promiscuous_<enable|disable>()``
234    on the specified ``port_id``.
235
236In order to run these callbacks, the application must periodically call
237the ``rte_kni_handle_request()`` function.  Any user callback function
238registered will be called directly from ``rte_kni_handle_request()`` so
239care must be taken to prevent deadlock and to not block any DPDK fastpath
240tasks.  Typically DPDK applications which use these callbacks will need
241to create a separate thread or secondary process to periodically call
242``rte_kni_handle_request()``.
243
244The KNI interfaces can be deleted by a DPDK application with
245``rte_kni_release()``.  All KNI interfaces not explicitly deleted will be
246deleted when the the ``/dev/kni`` device is closed, either explicitly with
247``rte_kni_close()`` or when the DPDK application is closed.
248
249DPDK mbuf Flow
250--------------
251
252To minimize the amount of DPDK code running in kernel space, the mbuf mempool is managed in userspace only.
253The kernel module will be aware of mbufs,
254but all mbuf allocation and free operations will be handled by the DPDK application only.
255
256:numref:`figure_pkt_flow_kni` shows a typical scenario with packets sent in both directions.
257
258.. _figure_pkt_flow_kni:
259
260.. figure:: img/pkt_flow_kni.*
261
262   Packet Flow via mbufs in the DPDK KNI
263
264
265Use Case: Ingress
266-----------------
267
268On the DPDK RX side, the mbuf is allocated by the PMD in the RX thread context.
269This thread will enqueue the mbuf in the rx_q FIFO.
270The KNI thread will poll all KNI active devices for the rx_q.
271If an mbuf is dequeued, it will be converted to a sk_buff and sent to the net stack via netif_rx().
272The dequeued mbuf must be freed, so the same pointer is sent back in the free_q FIFO.
273
274The RX thread, in the same main loop, polls this FIFO and frees the mbuf after dequeuing it.
275
276Use Case: Egress
277----------------
278
279For packet egress the DPDK application must first enqueue several mbufs to create an mbuf cache on the kernel side.
280
281The packet is received from the Linux net stack, by calling the kni_net_tx() callback.
282The mbuf is dequeued (without waiting due the cache) and filled with data from sk_buff.
283The sk_buff is then freed and the mbuf sent in the tx_q FIFO.
284
285The DPDK TX thread dequeues the mbuf and sends it to the PMD via ``rte_eth_tx_burst()``.
286It then puts the mbuf back in the cache.
287
288Ethtool
289-------
290
291Ethtool is a Linux-specific tool with corresponding support in the kernel
292where each net device must register its own callbacks for the supported operations.
293The current implementation uses the igb/ixgbe modified Linux drivers for ethtool support.
294Ethtool is not supported in i40e and VMs (VF or EM devices).
295