1e6541fdeSIntel /*- 2e6541fdeSIntel * BSD LICENSE 3e6541fdeSIntel * 4*e2366e74STomasz Kulasek * Copyright(c) 2010-2016 Intel Corporation. All rights reserved. 5e6541fdeSIntel * All rights reserved. 6e6541fdeSIntel * 7e6541fdeSIntel * Redistribution and use in source and binary forms, with or without 8e6541fdeSIntel * modification, are permitted provided that the following conditions 9e6541fdeSIntel * are met: 10e6541fdeSIntel * 11e6541fdeSIntel * * Redistributions of source code must retain the above copyright 12e6541fdeSIntel * notice, this list of conditions and the following disclaimer. 13e6541fdeSIntel * * Redistributions in binary form must reproduce the above copyright 14e6541fdeSIntel * notice, this list of conditions and the following disclaimer in 15e6541fdeSIntel * the documentation and/or other materials provided with the 16e6541fdeSIntel * distribution. 17e6541fdeSIntel * * Neither the name of Intel Corporation nor the names of its 18e6541fdeSIntel * contributors may be used to endorse or promote products derived 19e6541fdeSIntel * from this software without specific prior written permission. 20e6541fdeSIntel * 21e6541fdeSIntel * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22e6541fdeSIntel * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23e6541fdeSIntel * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24e6541fdeSIntel * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25e6541fdeSIntel * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26e6541fdeSIntel * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27e6541fdeSIntel * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28e6541fdeSIntel * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29e6541fdeSIntel * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30e6541fdeSIntel * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31e6541fdeSIntel * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32e6541fdeSIntel */ 33e6541fdeSIntel 34e6541fdeSIntel #include <stdio.h> 35e6541fdeSIntel #include <getopt.h> 36e6541fdeSIntel 37e6541fdeSIntel #include <rte_common.h> 38e6541fdeSIntel #include <rte_eal.h> 39*e2366e74STomasz Kulasek #include <rte_malloc.h> 40e6541fdeSIntel #include <rte_mempool.h> 41e6541fdeSIntel #include <rte_ethdev.h> 42e6541fdeSIntel #include <rte_cycles.h> 43e6541fdeSIntel #include <rte_mbuf.h> 44e6541fdeSIntel #include <rte_meter.h> 45e6541fdeSIntel 46e6541fdeSIntel /* 47e6541fdeSIntel * Traffic metering configuration 48e6541fdeSIntel * 49e6541fdeSIntel */ 50e6541fdeSIntel #define APP_MODE_FWD 0 51e6541fdeSIntel #define APP_MODE_SRTCM_COLOR_BLIND 1 52e6541fdeSIntel #define APP_MODE_SRTCM_COLOR_AWARE 2 53e6541fdeSIntel #define APP_MODE_TRTCM_COLOR_BLIND 3 54e6541fdeSIntel #define APP_MODE_TRTCM_COLOR_AWARE 4 55e6541fdeSIntel 56e6541fdeSIntel #define APP_MODE APP_MODE_SRTCM_COLOR_BLIND 57e6541fdeSIntel 58e6541fdeSIntel 59e6541fdeSIntel #include "main.h" 60e6541fdeSIntel 61e6541fdeSIntel 62e6541fdeSIntel #define APP_PKT_FLOW_POS 33 63e6541fdeSIntel #define APP_PKT_COLOR_POS 5 64e6541fdeSIntel 65e6541fdeSIntel 66e6541fdeSIntel #if APP_PKT_FLOW_POS > 64 || APP_PKT_COLOR_POS > 64 67e6541fdeSIntel #error Byte offset needs to be less than 64 68e6541fdeSIntel #endif 69e6541fdeSIntel 70e6541fdeSIntel /* 71e6541fdeSIntel * Buffer pool configuration 72e6541fdeSIntel * 73e6541fdeSIntel ***/ 74e6541fdeSIntel #define NB_MBUF 8192 75e6541fdeSIntel #define MEMPOOL_CACHE_SIZE 256 76e6541fdeSIntel 77e6541fdeSIntel static struct rte_mempool *pool = NULL; 78e6541fdeSIntel 79e6541fdeSIntel /* 80e6541fdeSIntel * NIC configuration 81e6541fdeSIntel * 82e6541fdeSIntel ***/ 83e6541fdeSIntel static struct rte_eth_conf port_conf = { 84e6541fdeSIntel .rxmode = { 8513c4ebd6SBruce Richardson .mq_mode = ETH_MQ_RX_RSS, 86e6541fdeSIntel .max_rx_pkt_len = ETHER_MAX_LEN, 87e6541fdeSIntel .split_hdr_size = 0, 88e6541fdeSIntel .header_split = 0, 89e6541fdeSIntel .hw_ip_checksum = 1, 90e6541fdeSIntel .hw_vlan_filter = 0, 91e6541fdeSIntel .jumbo_frame = 0, 92e6541fdeSIntel .hw_strip_crc = 0, 93e6541fdeSIntel }, 94e6541fdeSIntel .rx_adv_conf = { 95e6541fdeSIntel .rss_conf = { 96e6541fdeSIntel .rss_key = NULL, 978a387fa8SHelin Zhang .rss_hf = ETH_RSS_IP, 98e6541fdeSIntel }, 99e6541fdeSIntel }, 100e6541fdeSIntel .txmode = { 101e6541fdeSIntel .mq_mode = ETH_DCB_NONE, 102e6541fdeSIntel }, 103e6541fdeSIntel }; 104e6541fdeSIntel 105e6541fdeSIntel #define NIC_RX_QUEUE_DESC 128 106e6541fdeSIntel #define NIC_TX_QUEUE_DESC 512 107e6541fdeSIntel 108e6541fdeSIntel #define NIC_RX_QUEUE 0 109e6541fdeSIntel #define NIC_TX_QUEUE 0 110e6541fdeSIntel 111e6541fdeSIntel /* 112e6541fdeSIntel * Packet RX/TX 113e6541fdeSIntel * 114e6541fdeSIntel ***/ 115e6541fdeSIntel #define PKT_RX_BURST_MAX 32 116e6541fdeSIntel #define PKT_TX_BURST_MAX 32 117e6541fdeSIntel #define TIME_TX_DRAIN 200000ULL 118e6541fdeSIntel 119e6541fdeSIntel static uint8_t port_rx; 120e6541fdeSIntel static uint8_t port_tx; 121e6541fdeSIntel static struct rte_mbuf *pkts_rx[PKT_RX_BURST_MAX]; 122*e2366e74STomasz Kulasek struct rte_eth_dev_tx_buffer *tx_buffer; 123e6541fdeSIntel 124e6541fdeSIntel struct rte_meter_srtcm_params app_srtcm_params[] = { 125e6541fdeSIntel {.cir = 1000000 * 46, .cbs = 2048, .ebs = 2048}, 126e6541fdeSIntel }; 127e6541fdeSIntel 128e6541fdeSIntel struct rte_meter_trtcm_params app_trtcm_params[] = { 129e6541fdeSIntel {.cir = 1000000 * 46, .pir = 1500000 * 46, .cbs = 2048, .pbs = 2048}, 130e6541fdeSIntel }; 131e6541fdeSIntel 132e6541fdeSIntel #define APP_FLOWS_MAX 256 133e6541fdeSIntel 134e6541fdeSIntel FLOW_METER app_flows[APP_FLOWS_MAX]; 135e6541fdeSIntel 136e6541fdeSIntel static void 137e6541fdeSIntel app_configure_flow_table(void) 138e6541fdeSIntel { 139e6541fdeSIntel uint32_t i, j; 140e6541fdeSIntel 14113c4ebd6SBruce Richardson for (i = 0, j = 0; i < APP_FLOWS_MAX; i ++, j = (j + 1) % RTE_DIM(PARAMS)){ 142e6541fdeSIntel FUNC_CONFIG(&app_flows[i], &PARAMS[j]); 143e6541fdeSIntel } 144e6541fdeSIntel } 145e6541fdeSIntel 146e6541fdeSIntel static inline void 147fc8a10d8SIntel app_set_pkt_color(uint8_t *pkt_data, enum policer_action color) 148fc8a10d8SIntel { 149fc8a10d8SIntel pkt_data[APP_PKT_COLOR_POS] = (uint8_t)color; 150fc8a10d8SIntel } 151fc8a10d8SIntel 152fc8a10d8SIntel static inline int 153e6541fdeSIntel app_pkt_handle(struct rte_mbuf *pkt, uint64_t time) 154e6541fdeSIntel { 155fc8a10d8SIntel uint8_t input_color, output_color; 156e6541fdeSIntel uint8_t *pkt_data = rte_pktmbuf_mtod(pkt, uint8_t *); 157e6541fdeSIntel uint32_t pkt_len = rte_pktmbuf_pkt_len(pkt) - sizeof(struct ether_hdr); 158e6541fdeSIntel uint8_t flow_id = (uint8_t)(pkt_data[APP_PKT_FLOW_POS] & (APP_FLOWS_MAX - 1)); 159fc8a10d8SIntel input_color = pkt_data[APP_PKT_COLOR_POS]; 160fc8a10d8SIntel enum policer_action action; 161e6541fdeSIntel 162e6541fdeSIntel /* color input is not used for blind modes */ 163fc8a10d8SIntel output_color = (uint8_t) FUNC_METER(&app_flows[flow_id], time, pkt_len, 164fc8a10d8SIntel (enum rte_meter_color) input_color); 165fc8a10d8SIntel 166fc8a10d8SIntel /* Apply policing and set the output color */ 167fc8a10d8SIntel action = policer_table[input_color][output_color]; 168fc8a10d8SIntel app_set_pkt_color(pkt_data, action); 169fc8a10d8SIntel 170fc8a10d8SIntel return action; 171e6541fdeSIntel } 172e6541fdeSIntel 173e6541fdeSIntel 174e6541fdeSIntel static __attribute__((noreturn)) int 175e6541fdeSIntel main_loop(__attribute__((unused)) void *dummy) 176e6541fdeSIntel { 177e6541fdeSIntel uint64_t current_time, last_time = rte_rdtsc(); 178e6541fdeSIntel uint32_t lcore_id = rte_lcore_id(); 179e6541fdeSIntel 180e6541fdeSIntel printf("Core %u: port RX = %d, port TX = %d\n", lcore_id, port_rx, port_tx); 181e6541fdeSIntel 182e6541fdeSIntel while (1) { 183e6541fdeSIntel uint64_t time_diff; 184e6541fdeSIntel int i, nb_rx; 185e6541fdeSIntel 186e6541fdeSIntel /* Mechanism to avoid stale packets in the output buffer */ 187e6541fdeSIntel current_time = rte_rdtsc(); 188e6541fdeSIntel time_diff = current_time - last_time; 189e6541fdeSIntel if (unlikely(time_diff > TIME_TX_DRAIN)) { 190*e2366e74STomasz Kulasek /* Flush tx buffer */ 191*e2366e74STomasz Kulasek rte_eth_tx_buffer_flush(port_tx, NIC_TX_QUEUE, tx_buffer); 192e6541fdeSIntel last_time = current_time; 193e6541fdeSIntel } 194e6541fdeSIntel 195e6541fdeSIntel /* Read packet burst from NIC RX */ 196e6541fdeSIntel nb_rx = rte_eth_rx_burst(port_rx, NIC_RX_QUEUE, pkts_rx, PKT_RX_BURST_MAX); 197e6541fdeSIntel 198e6541fdeSIntel /* Handle packets */ 199e6541fdeSIntel for (i = 0; i < nb_rx; i ++) { 200e6541fdeSIntel struct rte_mbuf *pkt = pkts_rx[i]; 201e6541fdeSIntel 202e6541fdeSIntel /* Handle current packet */ 203fc8a10d8SIntel if (app_pkt_handle(pkt, current_time) == DROP) 204fc8a10d8SIntel rte_pktmbuf_free(pkt); 205*e2366e74STomasz Kulasek else 206*e2366e74STomasz Kulasek rte_eth_tx_buffer(port_tx, NIC_TX_QUEUE, tx_buffer, pkt); 207e6541fdeSIntel } 208e6541fdeSIntel } 209e6541fdeSIntel } 210e6541fdeSIntel 211e6541fdeSIntel static void 212e6541fdeSIntel print_usage(const char *prgname) 213e6541fdeSIntel { 214e6541fdeSIntel printf ("%s [EAL options] -- -p PORTMASK\n" 215e6541fdeSIntel " -p PORTMASK: hexadecimal bitmask of ports to configure\n", 216e6541fdeSIntel prgname); 217e6541fdeSIntel } 218e6541fdeSIntel 219e6541fdeSIntel static int 220e6541fdeSIntel parse_portmask(const char *portmask) 221e6541fdeSIntel { 222e6541fdeSIntel char *end = NULL; 223e6541fdeSIntel unsigned long pm; 224e6541fdeSIntel 225e6541fdeSIntel /* parse hexadecimal string */ 226e6541fdeSIntel pm = strtoul(portmask, &end, 16); 227e6541fdeSIntel if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0')) 228e6541fdeSIntel return -1; 229e6541fdeSIntel 230e6541fdeSIntel if (pm == 0) 231e6541fdeSIntel return -1; 232e6541fdeSIntel 233e6541fdeSIntel return pm; 234e6541fdeSIntel } 235e6541fdeSIntel 236e6541fdeSIntel /* Parse the argument given in the command line of the application */ 237e6541fdeSIntel static int 238e6541fdeSIntel parse_args(int argc, char **argv) 239e6541fdeSIntel { 240e6541fdeSIntel int opt; 241e6541fdeSIntel char **argvopt; 242e6541fdeSIntel int option_index; 243e6541fdeSIntel char *prgname = argv[0]; 244e6541fdeSIntel static struct option lgopts[] = { 245e6541fdeSIntel {NULL, 0, 0, 0} 246e6541fdeSIntel }; 247e6541fdeSIntel uint64_t port_mask, i, mask; 248e6541fdeSIntel 249e6541fdeSIntel argvopt = argv; 250e6541fdeSIntel 251e6541fdeSIntel while ((opt = getopt_long(argc, argvopt, "p:", lgopts, &option_index)) != EOF) { 252e6541fdeSIntel switch (opt) { 253e6541fdeSIntel case 'p': 254e6541fdeSIntel port_mask = parse_portmask(optarg); 255e6541fdeSIntel if (port_mask == 0) { 256e6541fdeSIntel printf("invalid port mask (null port mask)\n"); 257e6541fdeSIntel print_usage(prgname); 258e6541fdeSIntel return -1; 259e6541fdeSIntel } 260e6541fdeSIntel 261e6541fdeSIntel for (i = 0, mask = 1; i < 64; i ++, mask <<= 1){ 262e6541fdeSIntel if (mask & port_mask){ 263e6541fdeSIntel port_rx = i; 264e6541fdeSIntel port_mask &= ~ mask; 265e6541fdeSIntel break; 266e6541fdeSIntel } 267e6541fdeSIntel } 268e6541fdeSIntel 269e6541fdeSIntel for (i = 0, mask = 1; i < 64; i ++, mask <<= 1){ 270e6541fdeSIntel if (mask & port_mask){ 271e6541fdeSIntel port_tx = i; 272e6541fdeSIntel port_mask &= ~ mask; 273e6541fdeSIntel break; 274e6541fdeSIntel } 275e6541fdeSIntel } 276e6541fdeSIntel 277e6541fdeSIntel if (port_mask != 0) { 278e6541fdeSIntel printf("invalid port mask (more than 2 ports)\n"); 279e6541fdeSIntel print_usage(prgname); 280e6541fdeSIntel return -1; 281e6541fdeSIntel } 282e6541fdeSIntel break; 283e6541fdeSIntel 284e6541fdeSIntel default: 285e6541fdeSIntel print_usage(prgname); 286e6541fdeSIntel return -1; 287e6541fdeSIntel } 288e6541fdeSIntel } 289e6541fdeSIntel 290e6541fdeSIntel if (optind <= 1) { 291e6541fdeSIntel print_usage(prgname); 292e6541fdeSIntel return -1; 293e6541fdeSIntel } 294e6541fdeSIntel 295e6541fdeSIntel argv[optind-1] = prgname; 296e6541fdeSIntel 297e6541fdeSIntel optind = 0; /* reset getopt lib */ 298e6541fdeSIntel return 0; 299e6541fdeSIntel } 300e6541fdeSIntel 301e6541fdeSIntel int 30298a16481SDavid Marchand main(int argc, char **argv) 303e6541fdeSIntel { 304e6541fdeSIntel uint32_t lcore_id; 305e6541fdeSIntel int ret; 306e6541fdeSIntel 307e6541fdeSIntel /* EAL init */ 308e6541fdeSIntel ret = rte_eal_init(argc, argv); 309e6541fdeSIntel if (ret < 0) 310e6541fdeSIntel rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n"); 311e6541fdeSIntel argc -= ret; 312e6541fdeSIntel argv += ret; 313e6541fdeSIntel if (rte_lcore_count() != 1) { 314e6541fdeSIntel rte_exit(EXIT_FAILURE, "This application does not accept more than one core. " 315e6541fdeSIntel "Please adjust the \"-c COREMASK\" parameter accordingly.\n"); 316e6541fdeSIntel } 317e6541fdeSIntel 318e6541fdeSIntel /* Application non-EAL arguments parse */ 319e6541fdeSIntel ret = parse_args(argc, argv); 320e6541fdeSIntel if (ret < 0) 321e6541fdeSIntel rte_exit(EXIT_FAILURE, "Invalid input arguments\n"); 322e6541fdeSIntel 323e6541fdeSIntel /* Buffer pool init */ 324ea0c20eaSOlivier Matz pool = rte_pktmbuf_pool_create("pool", NB_MBUF, MEMPOOL_CACHE_SIZE, 325824cb29cSKonstantin Ananyev 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id()); 326e6541fdeSIntel if (pool == NULL) 327e6541fdeSIntel rte_exit(EXIT_FAILURE, "Buffer pool creation error\n"); 328e6541fdeSIntel 329e6541fdeSIntel /* NIC init */ 330e6541fdeSIntel ret = rte_eth_dev_configure(port_rx, 1, 1, &port_conf); 331e6541fdeSIntel if (ret < 0) 332e6541fdeSIntel rte_exit(EXIT_FAILURE, "Port %d configuration error (%d)\n", port_rx, ret); 333e6541fdeSIntel 33481f7ecd9SPablo de Lara ret = rte_eth_rx_queue_setup(port_rx, NIC_RX_QUEUE, NIC_RX_QUEUE_DESC, 33581f7ecd9SPablo de Lara rte_eth_dev_socket_id(port_rx), 33681f7ecd9SPablo de Lara NULL, pool); 337e6541fdeSIntel if (ret < 0) 338e6541fdeSIntel rte_exit(EXIT_FAILURE, "Port %d RX queue setup error (%d)\n", port_rx, ret); 339e6541fdeSIntel 34081f7ecd9SPablo de Lara ret = rte_eth_tx_queue_setup(port_rx, NIC_TX_QUEUE, NIC_TX_QUEUE_DESC, 34181f7ecd9SPablo de Lara rte_eth_dev_socket_id(port_rx), 34281f7ecd9SPablo de Lara NULL); 343e6541fdeSIntel if (ret < 0) 344e6541fdeSIntel rte_exit(EXIT_FAILURE, "Port %d TX queue setup error (%d)\n", port_rx, ret); 345e6541fdeSIntel 346e6541fdeSIntel ret = rte_eth_dev_configure(port_tx, 1, 1, &port_conf); 347e6541fdeSIntel if (ret < 0) 348e6541fdeSIntel rte_exit(EXIT_FAILURE, "Port %d configuration error (%d)\n", port_tx, ret); 349e6541fdeSIntel 35081f7ecd9SPablo de Lara ret = rte_eth_rx_queue_setup(port_tx, NIC_RX_QUEUE, NIC_RX_QUEUE_DESC, 35181f7ecd9SPablo de Lara rte_eth_dev_socket_id(port_tx), 35281f7ecd9SPablo de Lara NULL, pool); 353e6541fdeSIntel if (ret < 0) 354e6541fdeSIntel rte_exit(EXIT_FAILURE, "Port %d RX queue setup error (%d)\n", port_tx, ret); 355e6541fdeSIntel 35681f7ecd9SPablo de Lara ret = rte_eth_tx_queue_setup(port_tx, NIC_TX_QUEUE, NIC_TX_QUEUE_DESC, 35781f7ecd9SPablo de Lara rte_eth_dev_socket_id(port_tx), 35881f7ecd9SPablo de Lara NULL); 359e6541fdeSIntel if (ret < 0) 360e6541fdeSIntel rte_exit(EXIT_FAILURE, "Port %d TX queue setup error (%d)\n", port_tx, ret); 361e6541fdeSIntel 362*e2366e74STomasz Kulasek tx_buffer = rte_zmalloc_socket("tx_buffer", 363*e2366e74STomasz Kulasek RTE_ETH_TX_BUFFER_SIZE(PKT_TX_BURST_MAX), 0, 364*e2366e74STomasz Kulasek rte_eth_dev_socket_id(port_tx)); 365*e2366e74STomasz Kulasek if (tx_buffer == NULL) 366*e2366e74STomasz Kulasek rte_exit(EXIT_FAILURE, "Port %d TX buffer allocation error\n", 367*e2366e74STomasz Kulasek port_tx); 368*e2366e74STomasz Kulasek 369*e2366e74STomasz Kulasek rte_eth_tx_buffer_init(tx_buffer, PKT_TX_BURST_MAX); 370*e2366e74STomasz Kulasek 371e6541fdeSIntel ret = rte_eth_dev_start(port_rx); 372e6541fdeSIntel if (ret < 0) 373e6541fdeSIntel rte_exit(EXIT_FAILURE, "Port %d start error (%d)\n", port_rx, ret); 374e6541fdeSIntel 375e6541fdeSIntel ret = rte_eth_dev_start(port_tx); 376e6541fdeSIntel if (ret < 0) 377e6541fdeSIntel rte_exit(EXIT_FAILURE, "Port %d start error (%d)\n", port_tx, ret); 378e6541fdeSIntel 379e6541fdeSIntel rte_eth_promiscuous_enable(port_rx); 380e6541fdeSIntel 381e6541fdeSIntel rte_eth_promiscuous_enable(port_tx); 382e6541fdeSIntel 383e6541fdeSIntel /* App configuration */ 384e6541fdeSIntel app_configure_flow_table(); 385e6541fdeSIntel 386e6541fdeSIntel /* Launch per-lcore init on every lcore */ 387e6541fdeSIntel rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER); 388e6541fdeSIntel RTE_LCORE_FOREACH_SLAVE(lcore_id) { 389e6541fdeSIntel if (rte_eal_wait_lcore(lcore_id) < 0) 390e6541fdeSIntel return -1; 391e6541fdeSIntel } 392e6541fdeSIntel 393e6541fdeSIntel return 0; 394e6541fdeSIntel } 395