xref: /dpdk/examples/qos_sched/main.c (revision ebee5594a35e2af743ff03cde7d4bac67ac772f2)
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 <unistd.h>
35 #include <stdint.h>
36 
37 #include <rte_log.h>
38 #include <rte_mbuf.h>
39 #include <rte_malloc.h>
40 #include <rte_cycles.h>
41 #include <rte_ethdev.h>
42 #include <rte_memcpy.h>
43 #include <rte_byteorder.h>
44 #include <rte_branch_prediction.h>
45 
46 #include <rte_sched.h>
47 
48 #include "main.h"
49 
50 #define APP_MODE_NONE 0
51 #define APP_RX_MODE   1
52 #define APP_WT_MODE   2
53 #define APP_TX_MODE   4
54 
55 uint8_t interactive = APP_INTERACTIVE_DEFAULT;
56 uint32_t qavg_period = APP_QAVG_PERIOD;
57 uint32_t qavg_ntimes = APP_QAVG_NTIMES;
58 
59 /* main processing loop */
60 static int
61 app_main_loop(__attribute__((unused))void *dummy)
62 {
63 	uint32_t lcore_id;
64 	uint32_t i, mode;
65 	uint32_t rx_idx = 0;
66 	uint32_t wt_idx = 0;
67 	uint32_t tx_idx = 0;
68 	struct thread_conf *rx_confs[MAX_DATA_STREAMS];
69 	struct thread_conf *wt_confs[MAX_DATA_STREAMS];
70 	struct thread_conf *tx_confs[MAX_DATA_STREAMS];
71 
72 	memset(rx_confs, 0, sizeof(rx_confs));
73 	memset(wt_confs, 0, sizeof(wt_confs));
74 	memset(tx_confs, 0, sizeof(tx_confs));
75 
76 
77 	mode = APP_MODE_NONE;
78 	lcore_id = rte_lcore_id();
79 
80 	for (i = 0; i < nb_pfc; i++) {
81 		struct flow_conf *flow = &qos_conf[i];
82 
83 		if (flow->rx_core == lcore_id) {
84 			flow->rx_thread.rx_port = flow->rx_port;
85 			flow->rx_thread.rx_ring =  flow->rx_ring;
86 			flow->rx_thread.rx_queue = flow->rx_queue;
87 
88 			rx_confs[rx_idx++] = &flow->rx_thread;
89 
90 			mode |= APP_RX_MODE;
91 		}
92 		if (flow->tx_core == lcore_id) {
93 			flow->tx_thread.tx_port = flow->tx_port;
94 			flow->tx_thread.tx_ring =  flow->tx_ring;
95 			flow->tx_thread.tx_queue = flow->tx_queue;
96 
97 			tx_confs[tx_idx++] = &flow->tx_thread;
98 
99 			mode |= APP_TX_MODE;
100 		}
101 		if (flow->wt_core == lcore_id) {
102 			flow->wt_thread.rx_ring =  flow->rx_ring;
103 			flow->wt_thread.tx_ring =  flow->tx_ring;
104 			flow->wt_thread.tx_port =  flow->tx_port;
105 			flow->wt_thread.sched_port =  flow->sched_port;
106 
107 			wt_confs[wt_idx++] = &flow->wt_thread;
108 
109 			mode |= APP_WT_MODE;
110 		}
111 	}
112 
113 	if (mode == APP_MODE_NONE) {
114 		RTE_LOG(INFO, APP, "lcore %u has nothing to do\n", lcore_id);
115 		return -1;
116 	}
117 
118 	if (mode == (APP_RX_MODE | APP_WT_MODE)) {
119 		RTE_LOG(INFO, APP, "lcore %u was configured for both RX and WT !!!\n",
120 				 lcore_id);
121 		return -1;
122 	}
123 
124 	RTE_LOG(INFO, APP, "entering main loop on lcore %u\n", lcore_id);
125 	/* initialize mbuf memory */
126 	if (mode == APP_RX_MODE) {
127 		for (i = 0; i < rx_idx; i++) {
128 			RTE_LOG(INFO, APP, "flow %u lcoreid %u "
129 					"reading port %"PRIu8"\n",
130 					i, lcore_id, rx_confs[i]->rx_port);
131 		}
132 
133 		app_rx_thread(rx_confs);
134 	}
135 	else if (mode == (APP_TX_MODE | APP_WT_MODE)) {
136 		for (i = 0; i < wt_idx; i++) {
137 			wt_confs[i]->m_table = rte_malloc("table_wt", sizeof(struct rte_mbuf *)
138 					* burst_conf.tx_burst, RTE_CACHE_LINE_SIZE);
139 
140 			if (wt_confs[i]->m_table == NULL)
141 				rte_panic("flow %u unable to allocate memory buffer\n", i);
142 
143 			RTE_LOG(INFO, APP, "flow %u lcoreid %u sched+write "
144 					"port %"PRIu8"\n",
145 					i, lcore_id, wt_confs[i]->tx_port);
146 		}
147 
148 		app_mixed_thread(wt_confs);
149 	}
150 	else if (mode == APP_TX_MODE) {
151 		for (i = 0; i < tx_idx; i++) {
152 			tx_confs[i]->m_table = rte_malloc("table_tx", sizeof(struct rte_mbuf *)
153 					* burst_conf.tx_burst, RTE_CACHE_LINE_SIZE);
154 
155 			if (tx_confs[i]->m_table == NULL)
156 				rte_panic("flow %u unable to allocate memory buffer\n", i);
157 
158 			RTE_LOG(INFO, APP, "flow %u lcoreid %u "
159 					"writing port %"PRIu8"\n",
160 					i, lcore_id, tx_confs[i]->tx_port);
161 		}
162 
163 		app_tx_thread(tx_confs);
164 	}
165 	else if (mode == APP_WT_MODE){
166 		for (i = 0; i < wt_idx; i++) {
167 			RTE_LOG(INFO, APP, "flow %u lcoreid %u scheduling \n", i, lcore_id);
168 		}
169 
170 		app_worker_thread(wt_confs);
171 	}
172 
173 	return 0;
174 }
175 
176 void
177 app_stat(void)
178 {
179 	uint32_t i;
180 	struct rte_eth_stats stats;
181 	static struct rte_eth_stats rx_stats[MAX_DATA_STREAMS];
182 	static struct rte_eth_stats tx_stats[MAX_DATA_STREAMS];
183 
184 	/* print statistics */
185 	for(i = 0; i < nb_pfc; i++) {
186 		struct flow_conf *flow = &qos_conf[i];
187 
188 		rte_eth_stats_get(flow->rx_port, &stats);
189 		printf("\nRX port %"PRIu8": rx: %"PRIu64 " err: %"PRIu64
190 				" no_mbuf: %"PRIu64 "\n",
191 				flow->rx_port,
192 				stats.ipackets - rx_stats[i].ipackets,
193 				stats.ierrors - rx_stats[i].ierrors,
194 				stats.rx_nombuf - rx_stats[i].rx_nombuf);
195 		memcpy(&rx_stats[i], &stats, sizeof(stats));
196 
197 		rte_eth_stats_get(flow->tx_port, &stats);
198 		printf("TX port %"PRIu8": tx: %" PRIu64 " err: %" PRIu64 "\n",
199 				flow->tx_port,
200 				stats.opackets - tx_stats[i].opackets,
201 				stats.oerrors - tx_stats[i].oerrors);
202 		memcpy(&tx_stats[i], &stats, sizeof(stats));
203 
204 #if APP_COLLECT_STAT
205 		printf("-------+------------+------------+\n");
206 		printf("       |  received  |   dropped  |\n");
207 		printf("-------+------------+------------+\n");
208 		printf("  RX   | %10" PRIu64 " | %10" PRIu64 " |\n",
209 			flow->rx_thread.stat.nb_rx,
210 			flow->rx_thread.stat.nb_drop);
211 		printf("QOS+TX | %10" PRIu64 " | %10" PRIu64 " |   pps: %"PRIu64 " \n",
212 			flow->wt_thread.stat.nb_rx,
213 			flow->wt_thread.stat.nb_drop,
214 			flow->wt_thread.stat.nb_rx - flow->wt_thread.stat.nb_drop);
215 		printf("-------+------------+------------+\n");
216 
217 		memset(&flow->rx_thread.stat, 0, sizeof(struct thread_stat));
218 		memset(&flow->wt_thread.stat, 0, sizeof(struct thread_stat));
219 #endif
220 	}
221 }
222 
223 int
224 main(int argc, char **argv)
225 {
226 	int ret;
227 
228 	ret = app_parse_args(argc, argv);
229 	if (ret < 0)
230 		return -1;
231 
232 	ret = app_init();
233 	if (ret < 0)
234 		return -1;
235 
236 	/* launch per-lcore init on every lcore */
237 	rte_eal_mp_remote_launch(app_main_loop, NULL, SKIP_MASTER);
238 
239 	if (interactive) {
240 		sleep(1);
241 		prompt();
242 	}
243 	else {
244 		/* print statistics every second */
245 		while(1) {
246 			sleep(1);
247 			app_stat();
248 		}
249 	}
250 
251 	return 0;
252 }
253