Imported Upstream version 16.04
[deb_dpdk.git] / examples / qos_meter / main.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 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_malloc.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 NB_MBUF             8192
75 #define MEMPOOL_CACHE_SIZE  256
76
77 static struct rte_mempool *pool = NULL;
78
79 /*
80  * NIC configuration
81  *
82  ***/
83 static struct rte_eth_conf port_conf = {
84         .rxmode = {
85                 .mq_mode        = ETH_MQ_RX_RSS,
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_IP,
98                 },
99         },
100         .txmode = {
101                 .mq_mode = ETH_DCB_NONE,
102         },
103 };
104
105 #define NIC_RX_QUEUE_DESC               128
106 #define NIC_TX_QUEUE_DESC               512
107
108 #define NIC_RX_QUEUE                    0
109 #define NIC_TX_QUEUE                    0
110
111 /*
112  * Packet RX/TX
113  *
114  ***/
115 #define PKT_RX_BURST_MAX                32
116 #define PKT_TX_BURST_MAX                32
117 #define TIME_TX_DRAIN                   200000ULL
118
119 static uint8_t port_rx;
120 static uint8_t port_tx;
121 static struct rte_mbuf *pkts_rx[PKT_RX_BURST_MAX];
122 struct rte_eth_dev_tx_buffer *tx_buffer;
123
124 struct rte_meter_srtcm_params app_srtcm_params[] = {
125         {.cir = 1000000 * 46,  .cbs = 2048, .ebs = 2048},
126 };
127
128 struct rte_meter_trtcm_params app_trtcm_params[] = {
129         {.cir = 1000000 * 46,  .pir = 1500000 * 46,  .cbs = 2048, .pbs = 2048},
130 };
131
132 #define APP_FLOWS_MAX  256
133
134 FLOW_METER app_flows[APP_FLOWS_MAX];
135
136 static void
137 app_configure_flow_table(void)
138 {
139         uint32_t i, j;
140
141         for (i = 0, j = 0; i < APP_FLOWS_MAX; i ++, j = (j + 1) % RTE_DIM(PARAMS)){
142                 FUNC_CONFIG(&app_flows[i], &PARAMS[j]);
143         }
144 }
145
146 static inline void
147 app_set_pkt_color(uint8_t *pkt_data, enum policer_action color)
148 {
149         pkt_data[APP_PKT_COLOR_POS] = (uint8_t)color;
150 }
151
152 static inline int
153 app_pkt_handle(struct rte_mbuf *pkt, uint64_t time)
154 {
155         uint8_t input_color, output_color;
156         uint8_t *pkt_data = rte_pktmbuf_mtod(pkt, uint8_t *);
157         uint32_t pkt_len = rte_pktmbuf_pkt_len(pkt) - sizeof(struct ether_hdr);
158         uint8_t flow_id = (uint8_t)(pkt_data[APP_PKT_FLOW_POS] & (APP_FLOWS_MAX - 1));
159         input_color = pkt_data[APP_PKT_COLOR_POS];
160         enum policer_action action;
161
162         /* color input is not used for blind modes */
163         output_color = (uint8_t) FUNC_METER(&app_flows[flow_id], time, pkt_len,
164                 (enum rte_meter_color) input_color);
165
166         /* Apply policing and set the output color */
167         action = policer_table[input_color][output_color];
168         app_set_pkt_color(pkt_data, action);
169
170         return action;
171 }
172
173
174 static __attribute__((noreturn)) int
175 main_loop(__attribute__((unused)) void *dummy)
176 {
177         uint64_t current_time, last_time = rte_rdtsc();
178         uint32_t lcore_id = rte_lcore_id();
179
180         printf("Core %u: port RX = %d, port TX = %d\n", lcore_id, port_rx, port_tx);
181
182         while (1) {
183                 uint64_t time_diff;
184                 int i, nb_rx;
185
186                 /* Mechanism to avoid stale packets in the output buffer */
187                 current_time = rte_rdtsc();
188                 time_diff = current_time - last_time;
189                 if (unlikely(time_diff > TIME_TX_DRAIN)) {
190                         /* Flush tx buffer */
191                         rte_eth_tx_buffer_flush(port_tx, NIC_TX_QUEUE, tx_buffer);
192                         last_time = current_time;
193                 }
194
195                 /* Read packet burst from NIC RX */
196                 nb_rx = rte_eth_rx_burst(port_rx, NIC_RX_QUEUE, pkts_rx, PKT_RX_BURST_MAX);
197
198                 /* Handle packets */
199                 for (i = 0; i < nb_rx; i ++) {
200                         struct rte_mbuf *pkt = pkts_rx[i];
201
202                         /* Handle current packet */
203                         if (app_pkt_handle(pkt, current_time) == DROP)
204                                 rte_pktmbuf_free(pkt);
205                         else
206                                 rte_eth_tx_buffer(port_tx, NIC_TX_QUEUE, tx_buffer, pkt);
207                 }
208         }
209 }
210
211 static void
212 print_usage(const char *prgname)
213 {
214         printf ("%s [EAL options] -- -p PORTMASK\n"
215                 "  -p PORTMASK: hexadecimal bitmask of ports to configure\n",
216                 prgname);
217 }
218
219 static int
220 parse_portmask(const char *portmask)
221 {
222         char *end = NULL;
223         unsigned long pm;
224
225         /* parse hexadecimal string */
226         pm = strtoul(portmask, &end, 16);
227         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
228                 return -1;
229
230         if (pm == 0)
231                 return -1;
232
233         return pm;
234 }
235
236 /* Parse the argument given in the command line of the application */
237 static int
238 parse_args(int argc, char **argv)
239 {
240         int opt;
241         char **argvopt;
242         int option_index;
243         char *prgname = argv[0];
244         static struct option lgopts[] = {
245                 {NULL, 0, 0, 0}
246         };
247         uint64_t port_mask, i, mask;
248
249         argvopt = argv;
250
251         while ((opt = getopt_long(argc, argvopt, "p:", lgopts, &option_index)) != EOF) {
252                 switch (opt) {
253                 case 'p':
254                         port_mask = parse_portmask(optarg);
255                         if (port_mask == 0) {
256                                 printf("invalid port mask (null port mask)\n");
257                                 print_usage(prgname);
258                                 return -1;
259                         }
260
261                         for (i = 0, mask = 1; i < 64; i ++, mask <<= 1){
262                                 if (mask & port_mask){
263                                         port_rx = i;
264                                         port_mask &= ~ mask;
265                                         break;
266                                 }
267                         }
268
269                         for (i = 0, mask = 1; i < 64; i ++, mask <<= 1){
270                                 if (mask & port_mask){
271                                         port_tx = i;
272                                         port_mask &= ~ mask;
273                                         break;
274                                 }
275                         }
276
277                         if (port_mask != 0) {
278                                 printf("invalid port mask (more than 2 ports)\n");
279                                 print_usage(prgname);
280                                 return -1;
281                         }
282                         break;
283
284                 default:
285                         print_usage(prgname);
286                         return -1;
287                 }
288         }
289
290         if (optind <= 1) {
291                 print_usage(prgname);
292                 return -1;
293         }
294
295         argv[optind-1] = prgname;
296
297         optind = 0; /* reset getopt lib */
298         return 0;
299 }
300
301 int
302 main(int argc, char **argv)
303 {
304         uint32_t lcore_id;
305         int ret;
306
307         /* EAL init */
308         ret = rte_eal_init(argc, argv);
309         if (ret < 0)
310                 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
311         argc -= ret;
312         argv += ret;
313         if (rte_lcore_count() != 1) {
314                 rte_exit(EXIT_FAILURE, "This application does not accept more than one core. "
315                 "Please adjust the \"-c COREMASK\" parameter accordingly.\n");
316         }
317
318         /* Application non-EAL arguments parse */
319         ret = parse_args(argc, argv);
320         if (ret < 0)
321                 rte_exit(EXIT_FAILURE, "Invalid input arguments\n");
322
323         /* Buffer pool init */
324         pool = rte_pktmbuf_pool_create("pool", NB_MBUF, MEMPOOL_CACHE_SIZE,
325                 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
326         if (pool == NULL)
327                 rte_exit(EXIT_FAILURE, "Buffer pool creation error\n");
328
329         /* NIC init */
330         ret = rte_eth_dev_configure(port_rx, 1, 1, &port_conf);
331         if (ret < 0)
332                 rte_exit(EXIT_FAILURE, "Port %d configuration error (%d)\n", port_rx, ret);
333
334         ret = rte_eth_rx_queue_setup(port_rx, NIC_RX_QUEUE, NIC_RX_QUEUE_DESC,
335                                 rte_eth_dev_socket_id(port_rx),
336                                 NULL, pool);
337         if (ret < 0)
338                 rte_exit(EXIT_FAILURE, "Port %d RX queue setup error (%d)\n", port_rx, ret);
339
340         ret = rte_eth_tx_queue_setup(port_rx, NIC_TX_QUEUE, NIC_TX_QUEUE_DESC,
341                                 rte_eth_dev_socket_id(port_rx),
342                                 NULL);
343         if (ret < 0)
344         rte_exit(EXIT_FAILURE, "Port %d TX queue setup error (%d)\n", port_rx, ret);
345
346         ret = rte_eth_dev_configure(port_tx, 1, 1, &port_conf);
347         if (ret < 0)
348                 rte_exit(EXIT_FAILURE, "Port %d configuration error (%d)\n", port_tx, ret);
349
350         ret = rte_eth_rx_queue_setup(port_tx, NIC_RX_QUEUE, NIC_RX_QUEUE_DESC,
351                                 rte_eth_dev_socket_id(port_tx),
352                                 NULL, pool);
353         if (ret < 0)
354                 rte_exit(EXIT_FAILURE, "Port %d RX queue setup error (%d)\n", port_tx, ret);
355
356         ret = rte_eth_tx_queue_setup(port_tx, NIC_TX_QUEUE, NIC_TX_QUEUE_DESC,
357                                 rte_eth_dev_socket_id(port_tx),
358                                 NULL);
359         if (ret < 0)
360                 rte_exit(EXIT_FAILURE, "Port %d TX queue setup error (%d)\n", port_tx, ret);
361
362         tx_buffer = rte_zmalloc_socket("tx_buffer",
363                         RTE_ETH_TX_BUFFER_SIZE(PKT_TX_BURST_MAX), 0,
364                         rte_eth_dev_socket_id(port_tx));
365         if (tx_buffer == NULL)
366                 rte_exit(EXIT_FAILURE, "Port %d TX buffer allocation error\n",
367                                 port_tx);
368
369         rte_eth_tx_buffer_init(tx_buffer, PKT_TX_BURST_MAX);
370
371         ret = rte_eth_dev_start(port_rx);
372         if (ret < 0)
373                 rte_exit(EXIT_FAILURE, "Port %d start error (%d)\n", port_rx, ret);
374
375         ret = rte_eth_dev_start(port_tx);
376         if (ret < 0)
377                 rte_exit(EXIT_FAILURE, "Port %d start error (%d)\n", port_tx, ret);
378
379         rte_eth_promiscuous_enable(port_rx);
380
381         rte_eth_promiscuous_enable(port_tx);
382
383         /* App configuration */
384         app_configure_flow_table();
385
386         /* Launch per-lcore init on every lcore */
387         rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
388         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
389                 if (rte_eal_wait_lcore(lcore_id) < 0)
390                         return -1;
391         }
392
393         return 0;
394 }