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