1 .. SPDX-License-Identifier: BSD-3-Clause
2 Copyright(c) 2010-2014 Intel Corporation.
4 Exception Path Sample Application
5 =================================
7 The Exception Path sample application is a simple example that demonstrates the use of the DPDK
8 to set up an exception path for packets to go through the Linux* kernel.
9 This is done by using virtual TAP network interfaces.
10 These can be read from and written to by the DPDK application and
11 appear to the kernel as a standard network interface.
16 The application creates two threads for each NIC port being used.
17 One thread reads from the port and writes the data unmodified to a thread-specific TAP interface.
18 The second thread reads from a TAP interface and writes the data unmodified to the NIC port.
20 The packet flow through the exception path application is as shown in the following figure.
22 .. _figure_exception_path_example:
24 .. figure:: img/exception_path_example.*
29 To make throughput measurements, kernel bridges must be setup to forward data between the bridges appropriately.
31 Compiling the Application
32 -------------------------
34 To compile the sample application see :doc:`compiling`.
36 The application is located in the ``exception_path`` sub-directory.
38 Running the Application
39 -----------------------
41 The application requires a number of command line options:
43 .. code-block:: console
45 .build/exception_path [EAL options] -- -p PORTMASK -i IN_CORES -o OUT_CORES
49 * -p PORTMASK: A hex bitmask of ports to use
51 * -i IN_CORES: A hex bitmask of cores which read from NIC
53 * -o OUT_CORES: A hex bitmask of cores which write to NIC
55 Refer to the *DPDK Getting Started Guide* for general information on running applications
56 and the Environment Abstraction Layer (EAL) options.
58 The number of bits set in each bitmask must be the same.
59 The coremask -c or the corelist -l parameter of the EAL options should include IN_CORES and OUT_CORES.
60 The same bit must not be set in IN_CORES and OUT_CORES.
61 The affinities between ports and cores are set beginning with the least significant bit of each mask, that is,
62 the port represented by the lowest bit in PORTMASK is read from by the core represented by the lowest bit in IN_CORES,
63 and written to by the core represented by the lowest bit in OUT_CORES.
65 For example to run the application with two ports and four cores:
67 .. code-block:: console
69 ./build/exception_path -l 0-3 -n 4 -- -p 3 -i 3 -o c
74 While the application is running, statistics on packets sent and
75 received can be displayed by sending the SIGUSR1 signal to the application from another terminal:
77 .. code-block:: console
79 killall -USR1 exception_path
81 The statistics can be reset by sending a SIGUSR2 signal in a similar way.
86 The following sections provide some explanation of the code.
91 Setup of the mbuf pool, driver and queues is similar to the setup done in the :ref:`l2_fwd_app_real_and_virtual`.
92 In addition, the TAP interfaces must also be created.
93 A TAP interface is created for each lcore that is being used.
94 The code for creating the TAP interface is as follows:
99 * Create a tap network interface, or use existing one with same name.
100 * If name[0]='\0' then a name is automatically assigned and returned in name.
103 static int tap_create(char *name)
108 fd = open("/dev/net/tun", O_RDWR);
112 memset(&ifr, 0, sizeof(ifr));
114 /* TAP device without packet information */
116 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
118 rte_snprinf(ifr.ifr_name, IFNAMSIZ, name);
120 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
129 snprintf(name, IFNAMSIZ, ifr.ifr_name);
134 The other step in the initialization process that is unique to this sample application
135 is the association of each port with two cores:
137 * One core to read from the port and write to a TAP interface
139 * A second core to read from a TAP interface and write to the port
141 This is done using an array called port_ids[], which is indexed by the lcore IDs.
142 The population of this array is shown below:
149 RTE_LCORE_FOREACH(i) {
150 if (input_cores_mask & (1ULL << i)) {
151 /* Skip ports that are not enabled */
152 while ((ports_mask & (1 << rx_port)) == 0) {
154 if (rx_port > (sizeof(ports_mask) * 8))
155 goto fail; /* not enough ports */
157 port_ids[i] = rx_port++;
158 } else if (output_cores_mask & (1ULL << i)) {
159 /* Skip ports that are not enabled */
160 while ((ports_mask & (1 << tx_port)) == 0) {
162 if (tx_port > (sizeof(ports_mask) * 8))
163 goto fail; /* not enough ports */
165 port_ids[i] = tx_port++;
172 After the initialization steps are complete, the main_loop() function is run on each lcore.
173 This function first checks the lcore_id against the user provided input_cores_mask and output_cores_mask to see
174 if this core is reading from or writing to a TAP interface.
176 For the case that reads from a NIC port, the packet reception is the same as in the L2 Forwarding sample application
177 (see :ref:`l2_fwd_app_rx_tx_packets`).
178 The packet transmission is done by calling write() with the file descriptor of the appropriate TAP interface
179 and then explicitly freeing the mbuf back to the pool.
183 /* Loop forever reading from NIC and writing to tap */
186 struct rte_mbuf *pkts_burst[PKT_BURST_SZ];
189 const unsigned nb_rx = rte_eth_rx_burst(port_ids[lcore_id], 0, pkts_burst, PKT_BURST_SZ);
191 lcore_stats[lcore_id].rx += nb_rx;
193 for (i = 0; likely(i < nb_rx); i++) {
194 struct rte_mbuf *m = pkts_burst[i];
195 int ret = write(tap_fd, rte_pktmbuf_mtod(m, void*),
197 rte_pktmbuf_data_len(m));
200 lcore_stats[lcore_id].dropped++;
202 lcore_stats[lcore_id].tx++;
206 For the other case that reads from a TAP interface and writes to a NIC port,
207 packets are retrieved by doing a read() from the file descriptor of the appropriate TAP interface.
208 This fills in the data into the mbuf, then other fields are set manually.
209 The packet can then be transmitted as normal.
213 /* Loop forever reading from tap and writing to NIC */
217 struct rte_mbuf *m = rte_pktmbuf_alloc(pktmbuf_pool);
222 ret = read(tap_fd, m->pkt.data, MAX_PACKET_SZ); lcore_stats[lcore_id].rx++;
223 if (unlikely(ret < 0)) {
224 FATAL_ERROR("Reading from %s interface failed", tap_name);
229 m->pkt.data_len = (uint16_t)ret;
231 ret = rte_eth_tx_burst(port_ids[lcore_id], 0, &m, 1);
232 if (unlikely(ret < 1)) {
234 lcore_stats[lcore_id].dropped++;
237 lcore_stats[lcore_id].tx++;
241 To set up loops for measuring throughput, TAP interfaces can be connected using bridging.
242 The steps to do this are described in the section that follows.
244 Managing TAP Interfaces and Bridges
245 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
247 The Exception Path sample application creates TAP interfaces with names of the format tap_dpdk_nn,
248 where nn is the lcore ID. These TAP interfaces need to be configured for use:
250 .. code-block:: console
252 ifconfig tap_dpdk_00 up
254 To set up a bridge between two interfaces so that packets sent to one interface can be read from another,
257 .. code-block:: console
260 brctl addif br0 tap_dpdk_00
261 brctl addif br0 tap_dpdk_03
264 The TAP interfaces created by this application exist only when the application is running,
265 so the steps above need to be repeated each time the application is run.
266 To avoid this, persistent TAP interfaces can be created using openvpn:
268 .. code-block:: console
270 openvpn --mktun --dev tap_dpdk_00
272 If this method is used, then the steps above have to be done only once and
273 the same TAP interfaces can be reused each time the application is run.
274 To remove bridges and persistent TAP interfaces, the following commands are used:
276 .. code-block:: console
280 openvpn --rmtun --dev tap_dpdk_00