1 .. SPDX-License-Identifier: BSD-3-Clause
2 Copyright 2017 Mellanox Technologies, Ltd
4 Basic RTE Flow Filtering Sample Application
5 ===========================================
7 The Basic RTE flow filtering sample application is a simple example of a
8 creating a RTE flow rule.
10 It is intended as a demonstration of the basic components RTE flow rules.
13 Compiling the Application
14 -------------------------
16 To compile the sample application see :doc:`compiling`.
19 Running the Application
20 -----------------------
22 To run the example in a ``linux`` environment:
24 .. code-block:: console
26 ./<build_dir>/examples/dpdk-flow_filtering -l 1 -n 1
28 Refer to *DPDK Getting Started Guide* for general information on running
29 applications and the Environment Abstraction Layer (EAL) options.
35 The example is built from 2 files,
36 ``main.c`` which holds the example logic and ``flow_blocks.c`` that holds the
37 implementation for building the flow rule.
39 The following sections provide an explanation of the main components of the
42 All DPDK library functions used in the sample code are prefixed with ``rte_``
43 and are explained in detail in the *DPDK API Documentation*.
49 The ``main()`` function located in ``main.c`` file performs the initialization
50 and runs the main loop function.
52 The first task is to initialize the Environment Abstraction Layer (EAL). The
53 ``argc`` and ``argv`` arguments are provided to the ``rte_eal_init()``
54 function. The value returned is the number of parsed arguments:
56 .. literalinclude:: ../../../examples/flow_filtering/main.c
58 :start-after: Initialize EAL. 8<
59 :end-before: >8 End of Initialization of EAL.
63 The ``main()`` also allocates a mempool to hold the mbufs (Message Buffers)
64 used by the application:
66 .. literalinclude:: ../../../examples/flow_filtering/main.c
68 :start-after: Allocates a mempool to hold the mbufs. 8<
69 :end-before: >8 End of allocating a mempool to hold the mbufs.
72 Mbufs are the packet buffer structure used by DPDK. They are explained in
73 detail in the "Mbuf Library" section of the *DPDK Programmer's Guide*.
75 The ``main()`` function also initializes all the ports using the user defined
76 ``init_port()`` function which is explained in the next section:
78 .. literalinclude:: ../../../examples/flow_filtering/main.c
80 :start-after: Initializes all the ports using the user defined init_port(). 8<
81 :end-before: >8 End of Initializing the ports using user defined init_port().
84 Once the initialization is complete, we set the flow rule using the
87 .. literalinclude:: ../../../examples/flow_filtering/main.c
89 :start-after: Create flow for send packet with. 8<
90 :end-before: >8 End of creating flow for send packet with.
93 In the last part the application is ready to launch the
94 ``main_loop()`` function. Which is explained below.
97 .. literalinclude:: ../../../examples/flow_filtering/main.c
99 :start-after: Launching main_loop(). 8<
100 :end-before: >8 End of launching main_loop().
103 The Port Initialization Function
104 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
106 The main functional part of the port initialization used in the flow filtering
107 application is shown below:
109 .. literalinclude:: ../../../examples/flow_filtering/main.c
111 :start-after: Port initialization used in flow filtering. 8<
112 :end-before: >8 End of Port initialization used in flow filtering.
114 The Ethernet port is configured with default settings using the
115 ``rte_eth_dev_configure()`` function and the ``port_conf_default`` struct:
117 .. literalinclude:: ../../../examples/flow_filtering/main.c
119 :start-after: Ethernet port configured with default settings. 8<
120 :end-before: >8 End of ethernet port configured with default settings.
123 For this example we are configuring number of rx and tx queues that are connected
126 .. literalinclude:: ../../../examples/flow_filtering/main.c
128 :start-after: Configuring number of RX and TX queues connected to single port. 8<
129 :end-before: >8 End of Configuring RX and TX queues connected to single port.
132 In the next step we create and apply the flow rule. which is to send packets
133 with destination ip equals to 192.168.1.1 to queue number 1. The detail
134 explanation of the ``generate_ipv4_flow()`` appears later in this document:
136 .. literalinclude:: ../../../examples/flow_filtering/main.c
138 :start-after: Create flow for send packet with. 8<
139 :end-before: >8 End of create flow and the flow rule.
142 We are setting the RX port to promiscuous mode:
144 .. literalinclude:: ../../../examples/flow_filtering/main.c
146 :start-after: Setting the RX port to promiscuous mode. 8<
147 :end-before: >8 End of setting the RX port to promiscuous mode.
150 The last step is to start the port.
152 .. literalinclude:: ../../../examples/flow_filtering/main.c
154 :start-after: Starting the port. 8<
155 :end-before: >8 End of starting the port.
159 The main_loop function
160 ~~~~~~~~~~~~~~~~~~~~~~
162 As we saw above the ``main()`` function calls an application function to handle
163 the main loop. For the flow filtering application the main_loop function
164 looks like the following:
166 .. literalinclude:: ../../../examples/flow_filtering/main.c
168 :start-after: Main_loop for flow filtering. 8<
169 :end-before: >8 End of reading the packets from all queues.
171 The main work of the application is reading the packets from all
172 queues and printing for each packet the destination queue:
174 .. literalinclude:: ../../../examples/flow_filtering/main.c
176 :start-after: Reading the packets from all queues. 8<
177 :end-before: >8 End of main_loop for flow filtering.
180 The forwarding loop can be interrupted and the application closed using
181 ``Ctrl-C``. Which results in closing the port and the device using
182 ``rte_eth_dev_stop`` and ``rte_eth_dev_close``
184 The generate_ipv4_flow function
185 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
187 The generate_ipv4_flow function is responsible for creating the flow rule.
188 This function is located in the ``flow_blocks.c`` file.
190 .. literalinclude:: ../../../examples/flow_filtering/flow_blocks.c
192 :start-after: Function responsible for creating the flow rule. 8<
193 :end-before: >8 End of function responsible for creating the flow rule.
195 The first part of the function is declaring the structures that will be used.
197 .. literalinclude:: ../../../examples/flow_filtering/flow_blocks.c
199 :start-after: Declaring structs being used. 8<
200 :end-before: >8 End of declaring structs being used.
203 The following part create the flow attributes, in our case ingress.
205 .. literalinclude:: ../../../examples/flow_filtering/flow_blocks.c
207 :start-after: Set the rule attribute, only ingress packets will be checked. 8<
208 :end-before: >8 End of setting the rule attribute.
211 The third part defines the action to be taken when a packet matches
212 the rule. In this case send the packet to queue.
214 .. literalinclude:: ../../../examples/flow_filtering/flow_blocks.c
216 :start-after: Function responsible for creating the flow rule. 8<
217 :end-before: >8 End of setting the rule attribute.
219 The fourth part is responsible for creating the pattern and is built from
220 number of steps. In each step we build one level of the pattern starting with
223 Setting the first level of the pattern ETH:
225 .. literalinclude:: ../../../examples/flow_filtering/flow_blocks.c
227 :start-after: Set this level to allow all. 8<
228 :end-before: >8 End of setting the first level of the pattern.
231 Setting the second level of the pattern IP:
233 .. literalinclude:: ../../../examples/flow_filtering/flow_blocks.c
235 :start-after: Setting the second level of the pattern. 8<
236 :end-before: >8 End of setting the second level of the pattern.
239 Closing the pattern part.
241 .. literalinclude:: ../../../examples/flow_filtering/flow_blocks.c
243 :start-after: The final level must be always type end. 8<
244 :end-before: >8 End of final level must be always type end.
247 The last part of the function is to validate the rule and create it.
249 .. literalinclude:: ../../../examples/flow_filtering/flow_blocks.c
251 :start-after: Validate the rule and create it. 8<
252 :end-before: >8 End of validation the rule and create it.