1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2017 Mellanox Technologies, Ltd
10 #include <sys/types.h>
11 #include <sys/queue.h>
21 #include <rte_common.h>
22 #include <rte_malloc.h>
23 #include <rte_ether.h>
24 #include <rte_ethdev.h>
25 #include <rte_mempool.h>
29 #include <rte_cycles.h>
31 static volatile bool force_quit;
33 static uint16_t port_id;
34 static uint16_t nr_queues = 5;
35 static uint8_t selected_queue = 1;
36 struct rte_mempool *mbuf_pool;
37 struct rte_flow *flow;
39 #define SRC_IP ((0<<24) + (0<<16) + (0<<8) + 0) /* src ip = 0.0.0.0 */
40 #define DEST_IP ((192<<24) + (168<<16) + (1<<8) + 1) /* dest ip = 192.168.1.1 */
41 #define FULL_MASK 0xffffffff /* full mask */
42 #define EMPTY_MASK 0x0 /* empty mask */
44 #include "flow_blocks.c"
47 print_ether_addr(const char *what, struct rte_ether_addr *eth_addr)
49 char buf[RTE_ETHER_ADDR_FMT_SIZE];
50 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
51 printf("%s%s", what, buf);
54 /* Main_loop for flow filtering. 8< */
58 struct rte_mbuf *mbufs[32];
59 struct rte_ether_hdr *eth_hdr;
60 struct rte_flow_error error;
66 /* Reading the packets from all queues. 8< */
68 for (i = 0; i < nr_queues; i++) {
69 nb_rx = rte_eth_rx_burst(port_id,
72 for (j = 0; j < nb_rx; j++) {
73 struct rte_mbuf *m = mbufs[j];
75 eth_hdr = rte_pktmbuf_mtod(m,
76 struct rte_ether_hdr *);
77 print_ether_addr("src=",
79 print_ether_addr(" - dst=",
81 printf(" - queue=0x%x",
90 /* >8 End of reading the packets from all queues. */
92 /* closing and releasing resources */
93 rte_flow_flush(port_id, &error);
94 ret = rte_eth_dev_stop(port_id);
96 printf("Failed to stop port %u: %s",
97 port_id, rte_strerror(-ret));
98 rte_eth_dev_close(port_id);
101 /* >8 End of main_loop for flow filtering. */
103 #define CHECK_INTERVAL 1000 /* 100ms */
104 #define MAX_REPEAT_TIMES 90 /* 9s (90 * 100ms) in total */
107 assert_link_status(void)
109 struct rte_eth_link link;
110 uint8_t rep_cnt = MAX_REPEAT_TIMES;
111 int link_get_err = -EINVAL;
113 memset(&link, 0, sizeof(link));
115 link_get_err = rte_eth_link_get(port_id, &link);
116 if (link_get_err == 0 && link.link_status == RTE_ETH_LINK_UP)
118 rte_delay_ms(CHECK_INTERVAL);
121 if (link_get_err < 0)
122 rte_exit(EXIT_FAILURE, ":: error: link get is failing: %s\n",
123 rte_strerror(-link_get_err));
124 if (link.link_status == RTE_ETH_LINK_DOWN)
125 rte_exit(EXIT_FAILURE, ":: error: link is still down\n");
128 /* Port initialization used in flow filtering. 8< */
134 /* Ethernet port configured with default settings. 8< */
135 struct rte_eth_conf port_conf = {
141 RTE_ETH_TX_OFFLOAD_VLAN_INSERT |
142 RTE_ETH_TX_OFFLOAD_IPV4_CKSUM |
143 RTE_ETH_TX_OFFLOAD_UDP_CKSUM |
144 RTE_ETH_TX_OFFLOAD_TCP_CKSUM |
145 RTE_ETH_TX_OFFLOAD_SCTP_CKSUM |
146 RTE_ETH_TX_OFFLOAD_TCP_TSO,
149 struct rte_eth_txconf txq_conf;
150 struct rte_eth_rxconf rxq_conf;
151 struct rte_eth_dev_info dev_info;
153 ret = rte_eth_dev_info_get(port_id, &dev_info);
155 rte_exit(EXIT_FAILURE,
156 "Error during getting device (port %u) info: %s\n",
157 port_id, strerror(-ret));
159 port_conf.txmode.offloads &= dev_info.tx_offload_capa;
160 printf(":: initializing port: %d\n", port_id);
161 ret = rte_eth_dev_configure(port_id,
162 nr_queues, nr_queues, &port_conf);
164 rte_exit(EXIT_FAILURE,
165 ":: cannot configure device: err=%d, port=%u\n",
169 rxq_conf = dev_info.default_rxconf;
170 rxq_conf.offloads = port_conf.rxmode.offloads;
171 /* >8 End of ethernet port configured with default settings. */
173 /* Configuring number of RX and TX queues connected to single port. 8< */
174 for (i = 0; i < nr_queues; i++) {
175 ret = rte_eth_rx_queue_setup(port_id, i, 512,
176 rte_eth_dev_socket_id(port_id),
180 rte_exit(EXIT_FAILURE,
181 ":: Rx queue setup failed: err=%d, port=%u\n",
186 txq_conf = dev_info.default_txconf;
187 txq_conf.offloads = port_conf.txmode.offloads;
189 for (i = 0; i < nr_queues; i++) {
190 ret = rte_eth_tx_queue_setup(port_id, i, 512,
191 rte_eth_dev_socket_id(port_id),
194 rte_exit(EXIT_FAILURE,
195 ":: Tx queue setup failed: err=%d, port=%u\n",
199 /* >8 End of Configuring RX and TX queues connected to single port. */
201 /* Setting the RX port to promiscuous mode. 8< */
202 ret = rte_eth_promiscuous_enable(port_id);
204 rte_exit(EXIT_FAILURE,
205 ":: promiscuous mode enable failed: err=%s, port=%u\n",
206 rte_strerror(-ret), port_id);
207 /* >8 End of setting the RX port to promiscuous mode. */
209 /* Starting the port. 8< */
210 ret = rte_eth_dev_start(port_id);
212 rte_exit(EXIT_FAILURE,
213 "rte_eth_dev_start:err=%d, port=%u\n",
216 /* >8 End of starting the port. */
218 assert_link_status();
220 printf(":: initializing port: %d done\n", port_id);
222 /* >8 End of Port initialization used in flow filtering. */
225 signal_handler(int signum)
227 if (signum == SIGINT || signum == SIGTERM) {
228 printf("\n\nSignal %d received, preparing to exit...\n",
235 main(int argc, char **argv)
239 struct rte_flow_error error;
241 /* Initialize EAL. 8< */
242 ret = rte_eal_init(argc, argv);
244 rte_exit(EXIT_FAILURE, ":: invalid EAL arguments\n");
245 /* >8 End of Initialization of EAL. */
248 signal(SIGINT, signal_handler);
249 signal(SIGTERM, signal_handler);
251 nr_ports = rte_eth_dev_count_avail();
253 rte_exit(EXIT_FAILURE, ":: no Ethernet ports found\n");
256 printf(":: warn: %d ports detected, but we use only one: port %u\n",
259 /* Allocates a mempool to hold the mbufs. 8< */
260 mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", 4096, 128, 0,
261 RTE_MBUF_DEFAULT_BUF_SIZE,
263 /* >8 End of allocating a mempool to hold the mbufs. */
264 if (mbuf_pool == NULL)
265 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool\n");
267 /* Initializes all the ports using the user defined init_port(). 8< */
269 /* >8 End of Initializing the ports using user defined init_port(). */
271 /* Create flow for send packet with. 8< */
272 flow = generate_ipv4_flow(port_id, selected_queue,
274 DEST_IP, FULL_MASK, &error);
275 /* >8 End of create flow and the flow rule. */
277 printf("Flow can't be created %d message: %s\n",
279 error.message ? error.message : "(no stated reason)");
280 rte_exit(EXIT_FAILURE, "error in creating flow");
282 /* >8 End of creating flow for send packet with. */
284 /* Launching main_loop(). 8< */
286 /* >8 End of launching main_loop(). */
288 /* clean up the EAL */