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