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