ca1d4ac6ce2190fd4556df063c8ed40eec14cecf
[dpdk.git] / examples / qos_meter / 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 <stdio.h>
36 #include <getopt.h>
37
38 #include <rte_common.h>
39 #include <rte_eal.h>
40 #include <rte_mempool.h>
41 #include <rte_ethdev.h>
42 #include <rte_cycles.h>
43 #include <rte_mbuf.h>
44 #include <rte_meter.h>
45
46 /*
47  * Traffic metering configuration
48  *
49  */
50 #define APP_MODE_FWD                    0
51 #define APP_MODE_SRTCM_COLOR_BLIND      1
52 #define APP_MODE_SRTCM_COLOR_AWARE      2
53 #define APP_MODE_TRTCM_COLOR_BLIND      3
54 #define APP_MODE_TRTCM_COLOR_AWARE      4
55
56 #define APP_MODE        APP_MODE_SRTCM_COLOR_BLIND
57
58
59 #include "main.h"
60
61
62 #define APP_PKT_FLOW_POS                33
63 #define APP_PKT_COLOR_POS               5
64
65
66 #if APP_PKT_FLOW_POS > 64 || APP_PKT_COLOR_POS > 64
67 #error Byte offset needs to be less than 64
68 #endif
69
70 /*
71  * Buffer pool configuration 
72  *
73  ***/
74 #define MBUF_SIZE           (2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
75 #define NB_MBUF             8192
76 #define MEMPOOL_CACHE_SIZE  256
77
78 static struct rte_mempool *pool = NULL;
79
80 /*
81  * NIC configuration
82  *
83  ***/
84 static struct rte_eth_conf port_conf = {
85         .rxmode = {
86                 .max_rx_pkt_len = ETHER_MAX_LEN,
87                 .split_hdr_size = 0,
88                 .header_split   = 0,
89                 .hw_ip_checksum = 1,
90                 .hw_vlan_filter = 0,
91                 .jumbo_frame    = 0,
92                 .hw_strip_crc   = 0,
93         },
94         .rx_adv_conf = {
95                 .rss_conf = {
96                         .rss_key = NULL,
97                         .rss_hf = ETH_RSS_IPV4 | ETH_RSS_IPV6,
98                 },
99         },
100         .txmode = {
101                 .mq_mode = ETH_DCB_NONE,
102         },
103 };
104
105 static const struct rte_eth_rxconf rx_conf = {
106         .rx_thresh = {
107                 .pthresh = 8, /* RX prefetch threshold reg */
108                 .hthresh = 8, /* RX host threshold reg */
109                 .wthresh = 4, /* RX write-back threshold reg */
110         },
111         .rx_free_thresh = 32,
112 };
113
114 static const struct rte_eth_txconf tx_conf = {
115         .tx_thresh = {
116                 .pthresh = 36, /* TX prefetch threshold reg */
117                 .hthresh = 0,  /* TX host threshold reg */
118                 .wthresh = 0,  /* TX write-back threshold reg */
119         },
120         .tx_free_thresh = 0,
121         .tx_rs_thresh = 0,
122         .txq_flags = 0x0,
123 };
124
125 #define NIC_RX_QUEUE_DESC               128
126 #define NIC_TX_QUEUE_DESC               512
127
128 #define NIC_RX_QUEUE                    0
129 #define NIC_TX_QUEUE                    0
130
131 /*
132  * Packet RX/TX
133  *
134  ***/
135 #define PKT_RX_BURST_MAX                32
136 #define PKT_TX_BURST_MAX                32
137 #define TIME_TX_DRAIN                   200000ULL
138
139 static uint8_t port_rx;
140 static uint8_t port_tx;
141 static struct rte_mbuf *pkts_rx[PKT_RX_BURST_MAX];
142 static struct rte_mbuf *pkts_tx[PKT_TX_BURST_MAX];
143 static uint16_t pkts_tx_len = 0;
144
145
146 struct rte_meter_srtcm_params app_srtcm_params[] = {
147         {.cir = 1000000 * 46,  .cbs = 2048, .ebs = 2048},
148 };
149
150 struct rte_meter_trtcm_params app_trtcm_params[] = {
151         {.cir = 1000000 * 46,  .pir = 1500000 * 46,  .cbs = 2048, .pbs = 2048},
152 };
153
154 #define DIM(a)   (sizeof (a) / sizeof ((a)[0]))
155 #define APP_FLOWS_MAX  256
156
157 FLOW_METER app_flows[APP_FLOWS_MAX];
158
159 static void 
160 app_configure_flow_table(void)
161 {
162         uint32_t i, j;
163
164         for (i = 0, j = 0; i < APP_FLOWS_MAX; i ++, j = (j + 1) % DIM(PARAMS)){
165                 FUNC_CONFIG(&app_flows[i], &PARAMS[j]);
166         }
167 }
168
169 static inline void
170 app_pkt_handle(struct rte_mbuf *pkt, uint64_t time)
171 {
172         uint8_t color;
173
174         uint8_t *pkt_data = rte_pktmbuf_mtod(pkt, uint8_t *);
175         uint32_t pkt_len = rte_pktmbuf_pkt_len(pkt) - sizeof(struct ether_hdr);
176         uint8_t flow_id = (uint8_t)(pkt_data[APP_PKT_FLOW_POS] & (APP_FLOWS_MAX - 1));
177         color = pkt_data[APP_PKT_COLOR_POS];
178
179         /* color input is not used for blind modes */
180         color = (uint8_t) FUNC_METER(&app_flows[flow_id], time, pkt_len,
181                         (enum rte_meter_color) color);
182         pkt_data[APP_PKT_COLOR_POS] = color;
183 }
184
185
186 static __attribute__((noreturn)) int
187 main_loop(__attribute__((unused)) void *dummy)
188 {
189         uint64_t current_time, last_time = rte_rdtsc();
190         uint32_t lcore_id = rte_lcore_id();
191
192         printf("Core %u: port RX = %d, port TX = %d\n", lcore_id, port_rx, port_tx);
193
194         while (1) {
195                 uint64_t time_diff;
196                 int i, nb_rx;
197                 
198                 /* Mechanism to avoid stale packets in the output buffer */
199                 current_time = rte_rdtsc();
200                 time_diff = current_time - last_time;
201                 if (unlikely(time_diff > TIME_TX_DRAIN)) {
202                         int ret;
203                         
204                         if (pkts_tx_len == 0) {
205                                 last_time = current_time;
206                                 
207                                 continue;
208                         }
209
210                         /* Write packet burst to NIC TX */
211                         ret = rte_eth_tx_burst(port_tx, NIC_TX_QUEUE, pkts_tx, pkts_tx_len);
212                         
213                         /* Free buffers for any packets not written successfully */
214                         if (unlikely(ret < pkts_tx_len)) {
215                                 for ( ; ret < pkts_tx_len; ret ++) {
216                                         rte_pktmbuf_free(pkts_tx[ret]);
217                                 }
218                         }
219
220                         /* Empty the output buffer */
221                         pkts_tx_len = 0;
222                         
223                         last_time = current_time;
224                 }
225                 
226                 /* Read packet burst from NIC RX */
227                 nb_rx = rte_eth_rx_burst(port_rx, NIC_RX_QUEUE, pkts_rx, PKT_RX_BURST_MAX);
228                 
229                 /* Handle packets */
230                 for (i = 0; i < nb_rx; i ++) {
231                         struct rte_mbuf *pkt = pkts_rx[i];
232                         
233                         /* Handle current packet*/
234                         app_pkt_handle(pkt, current_time);
235                         
236                         /* Write current packet in the output buffer */
237                         pkts_tx[pkts_tx_len] = pkt;
238                         pkts_tx_len ++;
239                         
240                         /* Write packets from output buffer to NIC TX when full burst is available */
241                         if (unlikely(pkts_tx_len == PKT_TX_BURST_MAX)) {
242                                 /* Write packet burst to NIC TX */
243                                 int ret = rte_eth_tx_burst(port_tx, NIC_TX_QUEUE, pkts_tx, PKT_TX_BURST_MAX);
244                                 
245                                 /* Free buffers for any packets not written successfully */
246                                 if (unlikely(ret < PKT_TX_BURST_MAX)) {
247                                         for ( ; ret < PKT_TX_BURST_MAX; ret ++) {
248                                                 rte_pktmbuf_free(pkts_tx[ret]);
249                                         }
250                                 }
251                                 
252                                 /* Empty the output buffer */
253                                 pkts_tx_len = 0;
254                         }
255                 }
256         }
257 }
258
259 static void
260 print_usage(const char *prgname)
261 {
262         printf ("%s [EAL options] -- -p PORTMASK\n"
263                 "  -p PORTMASK: hexadecimal bitmask of ports to configure\n",
264                 prgname);
265 }
266
267 static int
268 parse_portmask(const char *portmask)
269 {
270         char *end = NULL;
271         unsigned long pm;
272
273         /* parse hexadecimal string */
274         pm = strtoul(portmask, &end, 16);
275         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
276                 return -1;
277
278         if (pm == 0)
279                 return -1;
280
281         return pm;
282 }
283
284 /* Parse the argument given in the command line of the application */
285 static int
286 parse_args(int argc, char **argv)
287 {
288         int opt;
289         char **argvopt;
290         int option_index;
291         char *prgname = argv[0];
292         static struct option lgopts[] = {
293                 {NULL, 0, 0, 0}
294         };
295         uint64_t port_mask, i, mask;            
296
297         argvopt = argv;
298
299         while ((opt = getopt_long(argc, argvopt, "p:", lgopts, &option_index)) != EOF) {
300                 switch (opt) {
301                 case 'p':
302                         port_mask = parse_portmask(optarg);
303                         if (port_mask == 0) {
304                                 printf("invalid port mask (null port mask)\n");
305                                 print_usage(prgname);
306                                 return -1;
307                         }
308                         
309                         for (i = 0, mask = 1; i < 64; i ++, mask <<= 1){
310                                 if (mask & port_mask){
311                                         port_rx = i;
312                                         port_mask &= ~ mask;
313                                         break;
314                                 }
315                         }
316
317                         for (i = 0, mask = 1; i < 64; i ++, mask <<= 1){
318                                 if (mask & port_mask){
319                                         port_tx = i;
320                                         port_mask &= ~ mask;
321                                         break;
322                                 }
323                         }
324                         
325                         if (port_mask != 0) {
326                                 printf("invalid port mask (more than 2 ports)\n");
327                                 print_usage(prgname);
328                                 return -1;
329                         }
330                         break;
331                         
332                 default:
333                         print_usage(prgname);
334                         return -1;
335                 }
336         }
337
338         if (optind <= 1) {
339                 print_usage(prgname);
340                 return -1;
341         }
342
343         argv[optind-1] = prgname;
344
345         optind = 0; /* reset getopt lib */
346         return 0;
347 }
348
349 int
350 MAIN(int argc, char **argv)
351 {
352         uint32_t lcore_id;
353         int ret;
354
355         /* EAL init */
356         ret = rte_eal_init(argc, argv);
357         if (ret < 0)
358                 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
359         argc -= ret;
360         argv += ret;
361         if (rte_lcore_count() != 1) {
362                 rte_exit(EXIT_FAILURE, "This application does not accept more than one core. " 
363                 "Please adjust the \"-c COREMASK\" parameter accordingly.\n");
364         }
365         
366         /* Application non-EAL arguments parse */
367         ret = parse_args(argc, argv);
368         if (ret < 0)
369                 rte_exit(EXIT_FAILURE, "Invalid input arguments\n");
370
371         /* Buffer pool init */
372         pool = rte_mempool_create("pool", NB_MBUF, MBUF_SIZE, MEMPOOL_CACHE_SIZE, 
373                 sizeof(struct rte_pktmbuf_pool_private), rte_pktmbuf_pool_init, NULL, 
374                 rte_pktmbuf_init, NULL, rte_socket_id(), 0);
375         if (pool == NULL)
376                 rte_exit(EXIT_FAILURE, "Buffer pool creation error\n");
377
378         /* PMD init */
379         if (rte_pmd_init_all() < 0)
380                 rte_exit(EXIT_FAILURE, "PMD init error\n");
381
382         if (rte_eal_pci_probe() < 0)
383                 rte_exit(EXIT_FAILURE, "PCI probe error\n");
384
385         /* NIC init */
386         ret = rte_eth_dev_configure(port_rx, 1, 1, &port_conf);
387         if (ret < 0)
388                 rte_exit(EXIT_FAILURE, "Port %d configuration error (%d)\n", port_rx, ret);
389
390         ret = rte_eth_rx_queue_setup(port_rx, NIC_RX_QUEUE, NIC_RX_QUEUE_DESC, rte_eth_dev_socket_id(port_rx), &rx_conf, pool);
391         if (ret < 0)
392                 rte_exit(EXIT_FAILURE, "Port %d RX queue setup error (%d)\n", port_rx, ret);
393         
394         ret = rte_eth_tx_queue_setup(port_rx, NIC_TX_QUEUE, NIC_TX_QUEUE_DESC, rte_eth_dev_socket_id(port_rx), &tx_conf);
395         if (ret < 0)
396         rte_exit(EXIT_FAILURE, "Port %d TX queue setup error (%d)\n", port_rx, ret);
397
398         ret = rte_eth_dev_configure(port_tx, 1, 1, &port_conf);
399         if (ret < 0)
400                 rte_exit(EXIT_FAILURE, "Port %d configuration error (%d)\n", port_tx, ret);
401
402         ret = rte_eth_rx_queue_setup(port_tx, NIC_RX_QUEUE, NIC_RX_QUEUE_DESC, rte_eth_dev_socket_id(port_tx), &rx_conf, pool);
403         if (ret < 0)
404                 rte_exit(EXIT_FAILURE, "Port %d RX queue setup error (%d)\n", port_tx, ret);
405
406         ret = rte_eth_tx_queue_setup(port_tx, NIC_TX_QUEUE, NIC_TX_QUEUE_DESC, rte_eth_dev_socket_id(port_tx), &tx_conf);
407         if (ret < 0)
408                 rte_exit(EXIT_FAILURE, "Port %d TX queue setup error (%d)\n", port_tx, ret);
409
410         ret = rte_eth_dev_start(port_rx);
411         if (ret < 0)
412                 rte_exit(EXIT_FAILURE, "Port %d start error (%d)\n", port_rx, ret);
413                 
414         ret = rte_eth_dev_start(port_tx);
415         if (ret < 0)
416                 rte_exit(EXIT_FAILURE, "Port %d start error (%d)\n", port_tx, ret);
417
418         rte_eth_promiscuous_enable(port_rx);
419
420         rte_eth_promiscuous_enable(port_tx);
421         
422         /* App configuration */
423         app_configure_flow_table();
424
425         /* Launch per-lcore init on every lcore */
426         rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
427         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
428                 if (rte_eal_wait_lcore(lcore_id) < 0)
429                         return -1;
430         }
431
432         return 0;
433 }