1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
8 #include <rte_common.h>
10 #include <rte_malloc.h>
11 #include <rte_mempool.h>
12 #include <rte_ethdev.h>
13 #include <rte_cycles.h>
15 #include <rte_meter.h>
18 * Traffic metering configuration
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
27 #define APP_MODE APP_MODE_SRTCM_COLOR_BLIND
33 #define APP_PKT_FLOW_POS 33
34 #define APP_PKT_COLOR_POS 5
37 #if APP_PKT_FLOW_POS > 64 || APP_PKT_COLOR_POS > 64
38 #error Byte offset needs to be less than 64
42 * Buffer pool configuration
46 #define MEMPOOL_CACHE_SIZE 256
48 static struct rte_mempool *pool = NULL;
54 static struct rte_eth_conf port_conf = {
56 .mq_mode = ETH_MQ_RX_RSS,
57 .max_rx_pkt_len = ETHER_MAX_LEN,
59 .ignore_offload_bitfield = 1,
60 .offloads = (DEV_RX_OFFLOAD_CHECKSUM |
61 DEV_RX_OFFLOAD_CRC_STRIP),
70 .mq_mode = ETH_DCB_NONE,
74 #define NIC_RX_QUEUE_DESC 128
75 #define NIC_TX_QUEUE_DESC 512
77 #define NIC_RX_QUEUE 0
78 #define NIC_TX_QUEUE 0
84 #define PKT_RX_BURST_MAX 32
85 #define PKT_TX_BURST_MAX 32
86 #define TIME_TX_DRAIN 200000ULL
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;
93 struct rte_meter_srtcm_params app_srtcm_params[] = {
94 {.cir = 1000000 * 46, .cbs = 2048, .ebs = 2048},
97 struct rte_meter_trtcm_params app_trtcm_params[] = {
98 {.cir = 1000000 * 46, .pir = 1500000 * 46, .cbs = 2048, .pbs = 2048},
101 #define APP_FLOWS_MAX 256
103 FLOW_METER app_flows[APP_FLOWS_MAX];
106 app_configure_flow_table(void)
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]);
122 app_set_pkt_color(uint8_t *pkt_data, enum policer_action color)
124 pkt_data[APP_PKT_COLOR_POS] = (uint8_t)color;
128 app_pkt_handle(struct rte_mbuf *pkt, uint64_t time)
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;
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);
141 /* Apply policing and set the output color */
142 action = policer_table[input_color][output_color];
143 app_set_pkt_color(pkt_data, action);
149 static __attribute__((noreturn)) int
150 main_loop(__attribute__((unused)) void *dummy)
152 uint64_t current_time, last_time = rte_rdtsc();
153 uint32_t lcore_id = rte_lcore_id();
155 printf("Core %u: port RX = %d, port TX = %d\n", lcore_id, port_rx, port_tx);
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;
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);
174 for (i = 0; i < nb_rx; i ++) {
175 struct rte_mbuf *pkt = pkts_rx[i];
177 /* Handle current packet */
178 if (app_pkt_handle(pkt, current_time) == DROP)
179 rte_pktmbuf_free(pkt);
181 rte_eth_tx_buffer(port_tx, NIC_TX_QUEUE, tx_buffer, pkt);
187 print_usage(const char *prgname)
189 printf ("%s [EAL options] -- -p PORTMASK\n"
190 " -p PORTMASK: hexadecimal bitmask of ports to configure\n",
195 parse_portmask(const char *portmask)
200 /* parse hexadecimal string */
201 pm = strtoul(portmask, &end, 16);
202 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
211 /* Parse the argument given in the command line of the application */
213 parse_args(int argc, char **argv)
218 char *prgname = argv[0];
219 static struct option lgopts[] = {
222 uint64_t port_mask, i, mask;
226 while ((opt = getopt_long(argc, argvopt, "p:", lgopts, &option_index)) != EOF) {
229 port_mask = parse_portmask(optarg);
230 if (port_mask == 0) {
231 printf("invalid port mask (null port mask)\n");
232 print_usage(prgname);
236 for (i = 0, mask = 1; i < 64; i ++, mask <<= 1){
237 if (mask & port_mask){
244 for (i = 0, mask = 1; i < 64; i ++, mask <<= 1){
245 if (mask & port_mask){
252 if (port_mask != 0) {
253 printf("invalid port mask (more than 2 ports)\n");
254 print_usage(prgname);
260 print_usage(prgname);
266 print_usage(prgname);
270 argv[optind-1] = prgname;
272 optind = 1; /* reset getopt lib */
277 main(int argc, char **argv)
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;
289 ret = rte_eal_init(argc, argv);
291 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
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");
299 /* Application non-EAL arguments parse */
300 ret = parse_args(argc, argv);
302 rte_exit(EXIT_FAILURE, "Invalid input arguments\n");
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());
308 rte_exit(EXIT_FAILURE, "Buffer pool creation error\n");
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);
317 rte_exit(EXIT_FAILURE, "Port %d configuration error (%d)\n", port_rx, ret);
319 ret = rte_eth_dev_adjust_nb_rx_tx_desc(port_rx, &nb_rxd, &nb_txd);
321 rte_exit(EXIT_FAILURE, "Port %d adjust number of descriptors error (%d)\n",
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),
330 rte_exit(EXIT_FAILURE, "Port %d RX queue setup error (%d)\n", port_rx, ret);
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),
339 rte_exit(EXIT_FAILURE, "Port %d TX queue setup error (%d)\n", port_rx, ret);
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);
347 rte_exit(EXIT_FAILURE, "Port %d configuration error (%d)\n", port_tx, ret);
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);
353 rte_exit(EXIT_FAILURE, "Port %d adjust number of descriptors error (%d)\n",
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),
362 rte_exit(EXIT_FAILURE, "Port %d RX queue setup error (%d)\n", port_tx, ret);
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),
371 rte_exit(EXIT_FAILURE, "Port %d TX queue setup error (%d)\n", port_tx, ret);
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",
380 rte_eth_tx_buffer_init(tx_buffer, PKT_TX_BURST_MAX);
382 ret = rte_eth_dev_start(port_rx);
384 rte_exit(EXIT_FAILURE, "Port %d start error (%d)\n", port_rx, ret);
386 ret = rte_eth_dev_start(port_tx);
388 rte_exit(EXIT_FAILURE, "Port %d start error (%d)\n", port_tx, ret);
390 rte_eth_promiscuous_enable(port_rx);
392 rte_eth_promiscuous_enable(port_tx);
394 /* App configuration */
395 ret = app_configure_flow_table();
397 rte_exit(EXIT_FAILURE, "Invalid configure flow table\n");
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)