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