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