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