examples: fix port mask parsing failure handling
[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 = RTE_ETHER_MAX_LEN,
58                 .split_hdr_size = 0,
59                 .offloads = DEV_RX_OFFLOAD_CHECKSUM,
60         },
61         .rx_adv_conf = {
62                 .rss_conf = {
63                         .rss_key = NULL,
64                         .rss_hf = ETH_RSS_IP,
65                 },
66         },
67         .txmode = {
68                 .mq_mode = ETH_DCB_NONE,
69         },
70 };
71
72 #define NIC_RX_QUEUE_DESC               1024
73 #define NIC_TX_QUEUE_DESC               1024
74
75 #define NIC_RX_QUEUE                    0
76 #define NIC_TX_QUEUE                    0
77
78 /*
79  * Packet RX/TX
80  *
81  ***/
82 #define PKT_RX_BURST_MAX                32
83 #define PKT_TX_BURST_MAX                32
84 #define TIME_TX_DRAIN                   200000ULL
85
86 static uint16_t port_rx;
87 static uint16_t port_tx;
88 static struct rte_mbuf *pkts_rx[PKT_RX_BURST_MAX];
89 struct rte_eth_dev_tx_buffer *tx_buffer;
90
91 struct rte_meter_srtcm_params app_srtcm_params = {
92         .cir = 1000000 * 46,
93         .cbs = 2048,
94         .ebs = 2048
95 };
96
97 struct rte_meter_srtcm_profile app_srtcm_profile;
98
99 struct rte_meter_trtcm_params app_trtcm_params = {
100         .cir = 1000000 * 46,
101         .pir = 1500000 * 46,
102         .cbs = 2048,
103         .pbs = 2048
104 };
105
106 struct rte_meter_trtcm_profile app_trtcm_profile;
107
108 #define APP_FLOWS_MAX  256
109
110 FLOW_METER app_flows[APP_FLOWS_MAX];
111
112 static int
113 app_configure_flow_table(void)
114 {
115         uint32_t i;
116         int ret;
117
118         ret = rte_meter_srtcm_profile_config(&app_srtcm_profile,
119                 &app_srtcm_params);
120         if (ret)
121                 return ret;
122
123         ret = rte_meter_trtcm_profile_config(&app_trtcm_profile,
124                 &app_trtcm_params);
125         if (ret)
126                 return ret;
127
128         for (i = 0; i < APP_FLOWS_MAX; i++) {
129                 ret = FUNC_CONFIG(&app_flows[i], &PROFILE);
130                 if (ret)
131                         return ret;
132         }
133
134         return 0;
135 }
136
137 static inline void
138 app_set_pkt_color(uint8_t *pkt_data, enum policer_action color)
139 {
140         pkt_data[APP_PKT_COLOR_POS] = (uint8_t)color;
141 }
142
143 static inline int
144 app_pkt_handle(struct rte_mbuf *pkt, uint64_t time)
145 {
146         uint8_t input_color, output_color;
147         uint8_t *pkt_data = rte_pktmbuf_mtod(pkt, uint8_t *);
148         uint32_t pkt_len = rte_pktmbuf_pkt_len(pkt) -
149                 sizeof(struct rte_ether_hdr);
150         uint8_t flow_id = (uint8_t)(pkt_data[APP_PKT_FLOW_POS] & (APP_FLOWS_MAX - 1));
151         input_color = pkt_data[APP_PKT_COLOR_POS];
152         enum policer_action action;
153
154         /* color input is not used for blind modes */
155         output_color = (uint8_t) FUNC_METER(&app_flows[flow_id],
156                 &PROFILE,
157                 time,
158                 pkt_len,
159                 (enum rte_color) input_color);
160
161         /* Apply policing and set the output color */
162         action = policer_table[input_color][output_color];
163         app_set_pkt_color(pkt_data, action);
164
165         return action;
166 }
167
168
169 static __rte_noreturn int
170 main_loop(__rte_unused void *dummy)
171 {
172         uint64_t current_time, last_time = rte_rdtsc();
173         uint32_t lcore_id = rte_lcore_id();
174
175         printf("Core %u: port RX = %d, port TX = %d\n", lcore_id, port_rx, port_tx);
176
177         while (1) {
178                 uint64_t time_diff;
179                 int i, nb_rx;
180
181                 /* Mechanism to avoid stale packets in the output buffer */
182                 current_time = rte_rdtsc();
183                 time_diff = current_time - last_time;
184                 if (unlikely(time_diff > TIME_TX_DRAIN)) {
185                         /* Flush tx buffer */
186                         rte_eth_tx_buffer_flush(port_tx, NIC_TX_QUEUE, tx_buffer);
187                         last_time = current_time;
188                 }
189
190                 /* Read packet burst from NIC RX */
191                 nb_rx = rte_eth_rx_burst(port_rx, NIC_RX_QUEUE, pkts_rx, PKT_RX_BURST_MAX);
192
193                 /* Handle packets */
194                 for (i = 0; i < nb_rx; i ++) {
195                         struct rte_mbuf *pkt = pkts_rx[i];
196
197                         /* Handle current packet */
198                         if (app_pkt_handle(pkt, current_time) == DROP)
199                                 rte_pktmbuf_free(pkt);
200                         else
201                                 rte_eth_tx_buffer(port_tx, NIC_TX_QUEUE, tx_buffer, pkt);
202                 }
203         }
204 }
205
206 static void
207 print_usage(const char *prgname)
208 {
209         printf ("%s [EAL options] -- -p PORTMASK\n"
210                 "  -p PORTMASK: hexadecimal bitmask of ports to configure\n",
211                 prgname);
212 }
213
214 static int
215 parse_portmask(const char *portmask)
216 {
217         char *end = NULL;
218         unsigned long pm;
219
220         /* parse hexadecimal string */
221         pm = strtoul(portmask, &end, 16);
222         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
223                 return 0;
224
225         return pm;
226 }
227
228 /* Parse the argument given in the command line of the application */
229 static int
230 parse_args(int argc, char **argv)
231 {
232         int opt;
233         char **argvopt;
234         int option_index;
235         char *prgname = argv[0];
236         static struct option lgopts[] = {
237                 {NULL, 0, 0, 0}
238         };
239         uint64_t port_mask, i, mask;
240
241         argvopt = argv;
242
243         while ((opt = getopt_long(argc, argvopt, "p:", lgopts, &option_index)) != EOF) {
244                 switch (opt) {
245                 case 'p':
246                         port_mask = parse_portmask(optarg);
247                         if (port_mask == 0) {
248                                 printf("invalid port mask (null port mask)\n");
249                                 print_usage(prgname);
250                                 return -1;
251                         }
252
253                         for (i = 0, mask = 1; i < 64; i ++, mask <<= 1){
254                                 if (mask & port_mask){
255                                         port_rx = i;
256                                         port_mask &= ~ mask;
257                                         break;
258                                 }
259                         }
260
261                         for (i = 0, mask = 1; i < 64; i ++, mask <<= 1){
262                                 if (mask & port_mask){
263                                         port_tx = i;
264                                         port_mask &= ~ mask;
265                                         break;
266                                 }
267                         }
268
269                         if (port_mask != 0) {
270                                 printf("invalid port mask (more than 2 ports)\n");
271                                 print_usage(prgname);
272                                 return -1;
273                         }
274                         break;
275
276                 default:
277                         print_usage(prgname);
278                         return -1;
279                 }
280         }
281
282         if (optind <= 1) {
283                 print_usage(prgname);
284                 return -1;
285         }
286
287         argv[optind-1] = prgname;
288
289         optind = 1; /* reset getopt lib */
290         return 0;
291 }
292
293 int
294 main(int argc, char **argv)
295 {
296         uint32_t lcore_id;
297         uint16_t nb_rxd = NIC_RX_QUEUE_DESC;
298         uint16_t nb_txd = NIC_TX_QUEUE_DESC;
299         struct rte_eth_conf conf;
300         struct rte_eth_rxconf rxq_conf;
301         struct rte_eth_txconf txq_conf;
302         struct rte_eth_dev_info dev_info;
303         int ret;
304
305         /* EAL init */
306         ret = rte_eal_init(argc, argv);
307         if (ret < 0)
308                 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
309         argc -= ret;
310         argv += ret;
311         if (rte_lcore_count() != 1) {
312                 rte_exit(EXIT_FAILURE, "This application does not accept more than one core. "
313                 "Please adjust the \"-c COREMASK\" parameter accordingly.\n");
314         }
315
316         /* Application non-EAL arguments parse */
317         ret = parse_args(argc, argv);
318         if (ret < 0)
319                 rte_exit(EXIT_FAILURE, "Invalid input arguments\n");
320
321         /* Buffer pool init */
322         pool = rte_pktmbuf_pool_create("pool", NB_MBUF, MEMPOOL_CACHE_SIZE,
323                 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
324         if (pool == NULL)
325                 rte_exit(EXIT_FAILURE, "Buffer pool creation error\n");
326
327         /* NIC init */
328         conf = port_conf;
329
330         ret = rte_eth_dev_info_get(port_rx, &dev_info);
331         if (ret != 0)
332                 rte_exit(EXIT_FAILURE,
333                         "Error during getting device (port %u) info: %s\n",
334                         port_rx, strerror(-ret));
335
336         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
337                 conf.txmode.offloads |= DEV_TX_OFFLOAD_MBUF_FAST_FREE;
338
339         conf.rx_adv_conf.rss_conf.rss_hf &= dev_info.flow_type_rss_offloads;
340         if (conf.rx_adv_conf.rss_conf.rss_hf !=
341                         port_conf.rx_adv_conf.rss_conf.rss_hf) {
342                 printf("Port %u modified RSS hash function based on hardware support,"
343                         "requested:%#"PRIx64" configured:%#"PRIx64"\n",
344                         port_rx,
345                         port_conf.rx_adv_conf.rss_conf.rss_hf,
346                         conf.rx_adv_conf.rss_conf.rss_hf);
347         }
348
349         ret = rte_eth_dev_configure(port_rx, 1, 1, &conf);
350         if (ret < 0)
351                 rte_exit(EXIT_FAILURE, "Port %d configuration error (%d)\n", port_rx, ret);
352
353         ret = rte_eth_dev_adjust_nb_rx_tx_desc(port_rx, &nb_rxd, &nb_txd);
354         if (ret < 0)
355                 rte_exit(EXIT_FAILURE, "Port %d adjust number of descriptors error (%d)\n",
356                                 port_rx, ret);
357
358         rxq_conf = dev_info.default_rxconf;
359         rxq_conf.offloads = conf.rxmode.offloads;
360         ret = rte_eth_rx_queue_setup(port_rx, NIC_RX_QUEUE, nb_rxd,
361                                 rte_eth_dev_socket_id(port_rx),
362                                 &rxq_conf, pool);
363         if (ret < 0)
364                 rte_exit(EXIT_FAILURE, "Port %d RX queue setup error (%d)\n", port_rx, ret);
365
366         txq_conf = dev_info.default_txconf;
367         txq_conf.offloads = conf.txmode.offloads;
368         ret = rte_eth_tx_queue_setup(port_rx, NIC_TX_QUEUE, nb_txd,
369                                 rte_eth_dev_socket_id(port_rx),
370                                 &txq_conf);
371         if (ret < 0)
372         rte_exit(EXIT_FAILURE, "Port %d TX queue setup error (%d)\n", port_rx, ret);
373
374         conf = port_conf;
375
376         ret = rte_eth_dev_info_get(port_tx, &dev_info);
377         if (ret != 0)
378                 rte_exit(EXIT_FAILURE,
379                         "Error during getting device (port %u) info: %s\n",
380                         port_tx, strerror(-ret));
381
382         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
383                 conf.txmode.offloads |= DEV_TX_OFFLOAD_MBUF_FAST_FREE;
384
385         conf.rx_adv_conf.rss_conf.rss_hf &= dev_info.flow_type_rss_offloads;
386         if (conf.rx_adv_conf.rss_conf.rss_hf !=
387                         port_conf.rx_adv_conf.rss_conf.rss_hf) {
388                 printf("Port %u modified RSS hash function based on hardware support,"
389                         "requested:%#"PRIx64" configured:%#"PRIx64"\n",
390                         port_tx,
391                         port_conf.rx_adv_conf.rss_conf.rss_hf,
392                         conf.rx_adv_conf.rss_conf.rss_hf);
393         }
394
395         ret = rte_eth_dev_configure(port_tx, 1, 1, &conf);
396         if (ret < 0)
397                 rte_exit(EXIT_FAILURE, "Port %d configuration error (%d)\n", port_tx, ret);
398
399         nb_rxd = NIC_RX_QUEUE_DESC;
400         nb_txd = NIC_TX_QUEUE_DESC;
401         ret = rte_eth_dev_adjust_nb_rx_tx_desc(port_tx, &nb_rxd, &nb_txd);
402         if (ret < 0)
403                 rte_exit(EXIT_FAILURE, "Port %d adjust number of descriptors error (%d)\n",
404                                 port_tx, ret);
405
406         rxq_conf = dev_info.default_rxconf;
407         rxq_conf.offloads = conf.rxmode.offloads;
408         ret = rte_eth_rx_queue_setup(port_tx, NIC_RX_QUEUE, nb_rxd,
409                                 rte_eth_dev_socket_id(port_tx),
410                                 NULL, pool);
411         if (ret < 0)
412                 rte_exit(EXIT_FAILURE, "Port %d RX queue setup error (%d)\n", port_tx, ret);
413
414         txq_conf = dev_info.default_txconf;
415         txq_conf.offloads = conf.txmode.offloads;
416         ret = rte_eth_tx_queue_setup(port_tx, NIC_TX_QUEUE, nb_txd,
417                                 rte_eth_dev_socket_id(port_tx),
418                                 NULL);
419         if (ret < 0)
420                 rte_exit(EXIT_FAILURE, "Port %d TX queue setup error (%d)\n", port_tx, ret);
421
422         tx_buffer = rte_zmalloc_socket("tx_buffer",
423                         RTE_ETH_TX_BUFFER_SIZE(PKT_TX_BURST_MAX), 0,
424                         rte_eth_dev_socket_id(port_tx));
425         if (tx_buffer == NULL)
426                 rte_exit(EXIT_FAILURE, "Port %d TX buffer allocation error\n",
427                                 port_tx);
428
429         rte_eth_tx_buffer_init(tx_buffer, PKT_TX_BURST_MAX);
430
431         ret = rte_eth_dev_start(port_rx);
432         if (ret < 0)
433                 rte_exit(EXIT_FAILURE, "Port %d start error (%d)\n", port_rx, ret);
434
435         ret = rte_eth_dev_start(port_tx);
436         if (ret < 0)
437                 rte_exit(EXIT_FAILURE, "Port %d start error (%d)\n", port_tx, ret);
438
439         ret = rte_eth_promiscuous_enable(port_rx);
440         if (ret != 0)
441                 rte_exit(EXIT_FAILURE,
442                         "Port %d promiscuous mode enable error (%s)\n",
443                         port_rx, rte_strerror(-ret));
444
445         ret = rte_eth_promiscuous_enable(port_tx);
446         if (ret != 0)
447                 rte_exit(EXIT_FAILURE,
448                         "Port %d promiscuous mode enable error (%s)\n",
449                         port_rx, rte_strerror(-ret));
450
451         /* App configuration */
452         ret = app_configure_flow_table();
453         if (ret < 0)
454                 rte_exit(EXIT_FAILURE, "Invalid configure flow table\n");
455
456         /* Launch per-lcore init on every lcore */
457         rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
458         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
459                 if (rte_eal_wait_lcore(lcore_id) < 0)
460                         return -1;
461         }
462
463         return 0;
464 }