4 * Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <rte_common.h>
39 #include <rte_mempool.h>
40 #include <rte_ethdev.h>
41 #include <rte_cycles.h>
43 #include <rte_meter.h>
46 * Traffic metering configuration
49 #define APP_MODE_FWD 0
50 #define APP_MODE_SRTCM_COLOR_BLIND 1
51 #define APP_MODE_SRTCM_COLOR_AWARE 2
52 #define APP_MODE_TRTCM_COLOR_BLIND 3
53 #define APP_MODE_TRTCM_COLOR_AWARE 4
55 #define APP_MODE APP_MODE_SRTCM_COLOR_BLIND
61 #define APP_PKT_FLOW_POS 33
62 #define APP_PKT_COLOR_POS 5
65 #if APP_PKT_FLOW_POS > 64 || APP_PKT_COLOR_POS > 64
66 #error Byte offset needs to be less than 64
70 * Buffer pool configuration
73 #define MBUF_SIZE (2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
75 #define MEMPOOL_CACHE_SIZE 256
77 static struct rte_mempool *pool = NULL;
83 static struct rte_eth_conf port_conf = {
85 .max_rx_pkt_len = ETHER_MAX_LEN,
96 .rss_hf = ETH_RSS_IPV4 | ETH_RSS_IPV6,
100 .mq_mode = ETH_DCB_NONE,
104 static const struct rte_eth_rxconf rx_conf = {
106 .pthresh = 8, /* RX prefetch threshold reg */
107 .hthresh = 8, /* RX host threshold reg */
108 .wthresh = 4, /* RX write-back threshold reg */
110 .rx_free_thresh = 32,
113 static const struct rte_eth_txconf tx_conf = {
115 .pthresh = 36, /* TX prefetch threshold reg */
116 .hthresh = 0, /* TX host threshold reg */
117 .wthresh = 0, /* TX write-back threshold reg */
124 #define NIC_RX_QUEUE_DESC 128
125 #define NIC_TX_QUEUE_DESC 512
127 #define NIC_RX_QUEUE 0
128 #define NIC_TX_QUEUE 0
134 #define PKT_RX_BURST_MAX 32
135 #define PKT_TX_BURST_MAX 32
136 #define TIME_TX_DRAIN 200000ULL
138 static uint8_t port_rx;
139 static uint8_t port_tx;
140 static struct rte_mbuf *pkts_rx[PKT_RX_BURST_MAX];
141 static struct rte_mbuf *pkts_tx[PKT_TX_BURST_MAX];
142 static uint16_t pkts_tx_len = 0;
145 struct rte_meter_srtcm_params app_srtcm_params[] = {
146 {.cir = 1000000 * 46, .cbs = 2048, .ebs = 2048},
149 struct rte_meter_trtcm_params app_trtcm_params[] = {
150 {.cir = 1000000 * 46, .pir = 1500000 * 46, .cbs = 2048, .pbs = 2048},
153 #define DIM(a) (sizeof (a) / sizeof ((a)[0]))
154 #define APP_FLOWS_MAX 256
156 FLOW_METER app_flows[APP_FLOWS_MAX];
159 app_configure_flow_table(void)
163 for (i = 0, j = 0; i < APP_FLOWS_MAX; i ++, j = (j + 1) % DIM(PARAMS)){
164 FUNC_CONFIG(&app_flows[i], &PARAMS[j]);
169 app_pkt_handle(struct rte_mbuf *pkt, uint64_t time)
173 uint8_t *pkt_data = rte_pktmbuf_mtod(pkt, uint8_t *);
174 uint32_t pkt_len = rte_pktmbuf_pkt_len(pkt) - sizeof(struct ether_hdr);
175 uint8_t flow_id = (uint8_t)(pkt_data[APP_PKT_FLOW_POS] & (APP_FLOWS_MAX - 1));
176 color = pkt_data[APP_PKT_COLOR_POS];
178 /* color input is not used for blind modes */
179 color = (uint8_t) FUNC_METER(&app_flows[flow_id], time, pkt_len,
180 (enum rte_meter_color) color);
181 pkt_data[APP_PKT_COLOR_POS] = color;
185 static __attribute__((noreturn)) int
186 main_loop(__attribute__((unused)) void *dummy)
188 uint64_t current_time, last_time = rte_rdtsc();
189 uint32_t lcore_id = rte_lcore_id();
191 printf("Core %u: port RX = %d, port TX = %d\n", lcore_id, port_rx, port_tx);
197 /* Mechanism to avoid stale packets in the output buffer */
198 current_time = rte_rdtsc();
199 time_diff = current_time - last_time;
200 if (unlikely(time_diff > TIME_TX_DRAIN)) {
203 if (pkts_tx_len == 0) {
204 last_time = current_time;
209 /* Write packet burst to NIC TX */
210 ret = rte_eth_tx_burst(port_tx, NIC_TX_QUEUE, pkts_tx, pkts_tx_len);
212 /* Free buffers for any packets not written successfully */
213 if (unlikely(ret < pkts_tx_len)) {
214 for ( ; ret < pkts_tx_len; ret ++) {
215 rte_pktmbuf_free(pkts_tx[ret]);
219 /* Empty the output buffer */
222 last_time = current_time;
225 /* Read packet burst from NIC RX */
226 nb_rx = rte_eth_rx_burst(port_rx, NIC_RX_QUEUE, pkts_rx, PKT_RX_BURST_MAX);
229 for (i = 0; i < nb_rx; i ++) {
230 struct rte_mbuf *pkt = pkts_rx[i];
232 /* Handle current packet*/
233 app_pkt_handle(pkt, current_time);
235 /* Write current packet in the output buffer */
236 pkts_tx[pkts_tx_len] = pkt;
239 /* Write packets from output buffer to NIC TX when full burst is available */
240 if (unlikely(pkts_tx_len == PKT_TX_BURST_MAX)) {
241 /* Write packet burst to NIC TX */
242 int ret = rte_eth_tx_burst(port_tx, NIC_TX_QUEUE, pkts_tx, PKT_TX_BURST_MAX);
244 /* Free buffers for any packets not written successfully */
245 if (unlikely(ret < PKT_TX_BURST_MAX)) {
246 for ( ; ret < PKT_TX_BURST_MAX; ret ++) {
247 rte_pktmbuf_free(pkts_tx[ret]);
251 /* Empty the output buffer */
259 print_usage(const char *prgname)
261 printf ("%s [EAL options] -- -p PORTMASK\n"
262 " -p PORTMASK: hexadecimal bitmask of ports to configure\n",
267 parse_portmask(const char *portmask)
272 /* parse hexadecimal string */
273 pm = strtoul(portmask, &end, 16);
274 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
283 /* Parse the argument given in the command line of the application */
285 parse_args(int argc, char **argv)
290 char *prgname = argv[0];
291 static struct option lgopts[] = {
294 uint64_t port_mask, i, mask;
298 while ((opt = getopt_long(argc, argvopt, "p:", lgopts, &option_index)) != EOF) {
301 port_mask = parse_portmask(optarg);
302 if (port_mask == 0) {
303 printf("invalid port mask (null port mask)\n");
304 print_usage(prgname);
308 for (i = 0, mask = 1; i < 64; i ++, mask <<= 1){
309 if (mask & port_mask){
316 for (i = 0, mask = 1; i < 64; i ++, mask <<= 1){
317 if (mask & port_mask){
324 if (port_mask != 0) {
325 printf("invalid port mask (more than 2 ports)\n");
326 print_usage(prgname);
332 print_usage(prgname);
338 print_usage(prgname);
342 argv[optind-1] = prgname;
344 optind = 0; /* reset getopt lib */
349 MAIN(int argc, char **argv)
355 ret = rte_eal_init(argc, argv);
357 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
360 if (rte_lcore_count() != 1) {
361 rte_exit(EXIT_FAILURE, "This application does not accept more than one core. "
362 "Please adjust the \"-c COREMASK\" parameter accordingly.\n");
365 /* Application non-EAL arguments parse */
366 ret = parse_args(argc, argv);
368 rte_exit(EXIT_FAILURE, "Invalid input arguments\n");
370 /* Buffer pool init */
371 pool = rte_mempool_create("pool", NB_MBUF, MBUF_SIZE, MEMPOOL_CACHE_SIZE,
372 sizeof(struct rte_pktmbuf_pool_private), rte_pktmbuf_pool_init, NULL,
373 rte_pktmbuf_init, NULL, rte_socket_id(), 0);
375 rte_exit(EXIT_FAILURE, "Buffer pool creation error\n");
378 if (rte_pmd_init_all() < 0)
379 rte_exit(EXIT_FAILURE, "PMD init error\n");
381 if (rte_eal_pci_probe() < 0)
382 rte_exit(EXIT_FAILURE, "PCI probe error\n");
385 ret = rte_eth_dev_configure(port_rx, 1, 1, &port_conf);
387 rte_exit(EXIT_FAILURE, "Port %d configuration error (%d)\n", port_rx, ret);
389 ret = rte_eth_rx_queue_setup(port_rx, NIC_RX_QUEUE, NIC_RX_QUEUE_DESC, rte_eth_dev_socket_id(port_rx), &rx_conf, pool);
391 rte_exit(EXIT_FAILURE, "Port %d RX queue setup error (%d)\n", port_rx, ret);
393 ret = rte_eth_tx_queue_setup(port_rx, NIC_TX_QUEUE, NIC_TX_QUEUE_DESC, rte_eth_dev_socket_id(port_rx), &tx_conf);
395 rte_exit(EXIT_FAILURE, "Port %d TX queue setup error (%d)\n", port_rx, ret);
397 ret = rte_eth_dev_configure(port_tx, 1, 1, &port_conf);
399 rte_exit(EXIT_FAILURE, "Port %d configuration error (%d)\n", port_tx, ret);
401 ret = rte_eth_rx_queue_setup(port_tx, NIC_RX_QUEUE, NIC_RX_QUEUE_DESC, rte_eth_dev_socket_id(port_tx), &rx_conf, pool);
403 rte_exit(EXIT_FAILURE, "Port %d RX queue setup error (%d)\n", port_tx, ret);
405 ret = rte_eth_tx_queue_setup(port_tx, NIC_TX_QUEUE, NIC_TX_QUEUE_DESC, rte_eth_dev_socket_id(port_tx), &tx_conf);
407 rte_exit(EXIT_FAILURE, "Port %d TX queue setup error (%d)\n", port_tx, ret);
409 ret = rte_eth_dev_start(port_rx);
411 rte_exit(EXIT_FAILURE, "Port %d start error (%d)\n", port_rx, ret);
413 ret = rte_eth_dev_start(port_tx);
415 rte_exit(EXIT_FAILURE, "Port %d start error (%d)\n", port_tx, ret);
417 rte_eth_promiscuous_enable(port_rx);
419 rte_eth_promiscuous_enable(port_tx);
421 /* App configuration */
422 app_configure_flow_table();
424 /* Launch per-lcore init on every lcore */
425 rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
426 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
427 if (rte_eal_wait_lcore(lcore_id) < 0)