1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2017 Mellanox Technologies, Ltd
10 #include <sys/types.h>
11 #include <sys/queue.h>
12 #include <netinet/in.h>
22 #include <rte_common.h>
23 #include <rte_malloc.h>
24 #include <rte_ether.h>
25 #include <rte_ethdev.h>
26 #include <rte_mempool.h>
30 #include <rte_cycles.h>
32 static volatile bool force_quit;
34 static uint16_t port_id;
35 static uint16_t nr_queues = 5;
36 static uint8_t selected_queue = 1;
37 struct rte_mempool *mbuf_pool;
38 struct rte_flow *flow;
40 #define SRC_IP ((0<<24) + (0<<16) + (0<<8) + 0) /* src ip = 0.0.0.0 */
41 #define DEST_IP ((192<<24) + (168<<16) + (1<<8) + 1) /* dest ip = 192.168.1.1 */
42 #define FULL_MASK 0xffffffff /* full mask */
43 #define EMPTY_MASK 0x0 /* empty mask */
45 #include "flow_blocks.c"
48 print_ether_addr(const char *what, struct ether_addr *eth_addr)
50 char buf[ETHER_ADDR_FMT_SIZE];
51 ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
52 printf("%s%s", what, buf);
58 struct rte_mbuf *mbufs[32];
59 struct ether_hdr *eth_hdr;
60 struct rte_flow_error error;
66 for (i = 0; i < nr_queues; i++) {
67 nb_rx = rte_eth_rx_burst(port_id,
70 for (j = 0; j < nb_rx; j++) {
71 struct rte_mbuf *m = mbufs[j];
73 eth_hdr = rte_pktmbuf_mtod(m,
75 print_ether_addr("src=",
77 print_ether_addr(" - dst=",
79 printf(" - queue=0x%x",
89 /* closing and releasing resources */
90 rte_flow_flush(port_id, &error);
91 rte_eth_dev_stop(port_id);
92 rte_eth_dev_close(port_id);
95 #define CHECK_INTERVAL 1000 /* 100ms */
96 #define MAX_REPEAT_TIMES 90 /* 9s (90 * 100ms) in total */
99 assert_link_status(void)
101 struct rte_eth_link link;
102 uint8_t rep_cnt = MAX_REPEAT_TIMES;
104 memset(&link, 0, sizeof(link));
106 rte_eth_link_get(port_id, &link);
107 if (link.link_status == ETH_LINK_UP)
109 rte_delay_ms(CHECK_INTERVAL);
112 if (link.link_status == ETH_LINK_DOWN)
113 rte_exit(EXIT_FAILURE, ":: error: link is still down\n");
121 struct rte_eth_conf port_conf = {
124 .offloads = DEV_RX_OFFLOAD_CRC_STRIP,
128 DEV_TX_OFFLOAD_VLAN_INSERT |
129 DEV_TX_OFFLOAD_IPV4_CKSUM |
130 DEV_TX_OFFLOAD_UDP_CKSUM |
131 DEV_TX_OFFLOAD_TCP_CKSUM |
132 DEV_TX_OFFLOAD_SCTP_CKSUM |
133 DEV_TX_OFFLOAD_TCP_TSO,
136 * Initialize fdir_conf of rte_eth_conf.
137 * Fdir is used in flow filtering for I40e,
138 * so rte_flow rules involve some fdir
139 * configurations. In long term it's better
140 * that drivers don't require any fdir
141 * configuration for rte_flow, but we need to
142 * get this workaround so that sample app can
146 .mode = RTE_FDIR_MODE_PERFECT,
147 .pballoc = RTE_FDIR_PBALLOC_64K,
148 .status = RTE_FDIR_REPORT_STATUS,
152 struct rte_eth_txconf txq_conf;
153 struct rte_eth_rxconf rxq_conf;
154 struct rte_eth_dev_info dev_info;
156 printf(":: initializing port: %d\n", port_id);
157 ret = rte_eth_dev_configure(port_id,
158 nr_queues, nr_queues, &port_conf);
160 rte_exit(EXIT_FAILURE,
161 ":: cannot configure device: err=%d, port=%u\n",
165 rte_eth_dev_info_get(port_id, &dev_info);
166 rxq_conf = dev_info.default_rxconf;
167 rxq_conf.offloads = port_conf.rxmode.offloads;
168 /* only set Rx queues: something we care only so far */
169 for (i = 0; i < nr_queues; i++) {
170 ret = rte_eth_rx_queue_setup(port_id, i, 512,
171 rte_eth_dev_socket_id(port_id),
175 rte_exit(EXIT_FAILURE,
176 ":: Rx queue setup failed: err=%d, port=%u\n",
181 txq_conf = dev_info.default_txconf;
182 txq_conf.offloads = port_conf.txmode.offloads;
184 for (i = 0; i < nr_queues; i++) {
185 ret = rte_eth_tx_queue_setup(port_id, i, 512,
186 rte_eth_dev_socket_id(port_id),
189 rte_exit(EXIT_FAILURE,
190 ":: Tx queue setup failed: err=%d, port=%u\n",
195 rte_eth_promiscuous_enable(port_id);
196 ret = rte_eth_dev_start(port_id);
198 rte_exit(EXIT_FAILURE,
199 "rte_eth_dev_start:err=%d, port=%u\n",
203 assert_link_status();
205 printf(":: initializing port: %d done\n", port_id);
209 signal_handler(int signum)
211 if (signum == SIGINT || signum == SIGTERM) {
212 printf("\n\nSignal %d received, preparing to exit...\n",
219 main(int argc, char **argv)
223 struct rte_flow_error error;
225 ret = rte_eal_init(argc, argv);
227 rte_exit(EXIT_FAILURE, ":: invalid EAL arguments\n");
230 signal(SIGINT, signal_handler);
231 signal(SIGTERM, signal_handler);
233 nr_ports = rte_eth_dev_count_avail();
235 rte_exit(EXIT_FAILURE, ":: no Ethernet ports found\n");
238 printf(":: warn: %d ports detected, but we use only one: port %u\n",
241 mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", 4096, 128, 0,
242 RTE_MBUF_DEFAULT_BUF_SIZE,
244 if (mbuf_pool == NULL)
245 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool\n");
249 /* create flow for send packet with */
250 flow = generate_ipv4_flow(port_id, selected_queue,
252 DEST_IP, FULL_MASK, &error);
254 printf("Flow can't be created %d message: %s\n",
256 error.message ? error.message : "(no stated reason)");
257 rte_exit(EXIT_FAILURE, "error in creating flow");