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