xref: /dpdk/app/test-pipeline/runtime.c (revision fc1f2750a3ec6da919e3c86e59d56f34ec97154b)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdint.h>
37 #include <inttypes.h>
38 #include <sys/types.h>
39 #include <string.h>
40 #include <sys/queue.h>
41 #include <stdarg.h>
42 #include <errno.h>
43 #include <getopt.h>
44 
45 #include <rte_common.h>
46 #include <rte_byteorder.h>
47 #include <rte_log.h>
48 #include <rte_memory.h>
49 #include <rte_memcpy.h>
50 #include <rte_memzone.h>
51 #include <rte_tailq.h>
52 #include <rte_eal.h>
53 #include <rte_per_lcore.h>
54 #include <rte_launch.h>
55 #include <rte_atomic.h>
56 #include <rte_cycles.h>
57 #include <rte_prefetch.h>
58 #include <rte_lcore.h>
59 #include <rte_per_lcore.h>
60 #include <rte_branch_prediction.h>
61 #include <rte_interrupts.h>
62 #include <rte_pci.h>
63 #include <rte_random.h>
64 #include <rte_debug.h>
65 #include <rte_ether.h>
66 #include <rte_ethdev.h>
67 #include <rte_ring.h>
68 #include <rte_mempool.h>
69 #include <rte_mbuf.h>
70 #include <rte_ip.h>
71 #include <rte_tcp.h>
72 #include <rte_lpm.h>
73 #include <rte_lpm6.h>
74 #include <rte_malloc.h>
75 
76 #include "main.h"
77 
78 void
79 app_main_loop_rx(void) {
80 	uint32_t i;
81 	int ret;
82 
83 	RTE_LOG(INFO, USER1, "Core %u is doing RX\n", rte_lcore_id());
84 
85 	for (i = 0; ; i = ((i + 1) & (app.n_ports - 1))) {
86 		uint16_t n_mbufs;
87 
88 		n_mbufs = rte_eth_rx_burst(
89 			app.ports[i],
90 			0,
91 			app.mbuf_rx.array,
92 			app.burst_size_rx_read);
93 
94 		if (n_mbufs == 0)
95 			continue;
96 
97 		do {
98 			ret = rte_ring_sp_enqueue_bulk(
99 				app.rings_rx[i],
100 				(void **) app.mbuf_rx.array,
101 				n_mbufs);
102 		} while (ret < 0);
103 	}
104 }
105 
106 void
107 app_main_loop_worker(void) {
108 	struct app_mbuf_array *worker_mbuf;
109 	uint32_t i;
110 
111 	RTE_LOG(INFO, USER1, "Core %u is doing work (no pipeline)\n",
112 		rte_lcore_id());
113 
114 	worker_mbuf = rte_malloc_socket(NULL, sizeof(struct app_mbuf_array),
115 			CACHE_LINE_SIZE, rte_socket_id());
116 	if (worker_mbuf == NULL)
117 		rte_panic("Worker thread: cannot allocate buffer space\n");
118 
119 	for (i = 0; ; i = ((i + 1) & (app.n_ports - 1))) {
120 		int ret;
121 
122 		ret = rte_ring_sc_dequeue_bulk(
123 			app.rings_rx[i],
124 			(void **) worker_mbuf->array,
125 			app.burst_size_worker_read);
126 
127 		if (ret == -ENOENT)
128 			continue;
129 
130 		do {
131 			ret = rte_ring_sp_enqueue_bulk(
132 				app.rings_tx[i ^ 1],
133 				(void **) worker_mbuf->array,
134 				app.burst_size_worker_write);
135 		} while (ret < 0);
136 	}
137 }
138 
139 void
140 app_main_loop_tx(void) {
141 	uint32_t i;
142 
143 	RTE_LOG(INFO, USER1, "Core %u is doing TX\n", rte_lcore_id());
144 
145 	for (i = 0; ; i = ((i + 1) & (app.n_ports - 1))) {
146 		uint16_t n_mbufs, n_pkts;
147 		int ret;
148 
149 		n_mbufs = app.mbuf_tx[i].n_mbufs;
150 
151 		ret = rte_ring_sc_dequeue_bulk(
152 			app.rings_tx[i],
153 			(void **) &app.mbuf_tx[i].array[n_mbufs],
154 			app.burst_size_tx_read);
155 
156 		if (ret == -ENOENT)
157 			continue;
158 
159 		n_mbufs += app.burst_size_tx_read;
160 
161 		if (n_mbufs < app.burst_size_tx_write) {
162 			app.mbuf_tx[i].n_mbufs = n_mbufs;
163 			continue;
164 		}
165 
166 		n_pkts = rte_eth_tx_burst(
167 			app.ports[i],
168 			0,
169 			app.mbuf_tx[i].array,
170 			n_mbufs);
171 
172 		if (n_pkts < n_mbufs) {
173 			uint16_t k;
174 
175 			for (k = n_pkts; k < n_mbufs; k++) {
176 				struct rte_mbuf *pkt_to_free;
177 
178 				pkt_to_free = app.mbuf_tx[i].array[k];
179 				rte_pktmbuf_free(pkt_to_free);
180 			}
181 		}
182 
183 		app.mbuf_tx[i].n_mbufs = 0;
184 	}
185 }
186