xref: /dpdk/examples/flow_filtering/main.c (revision 295968d1)
1ea9382dcSThomas Monjalon /* SPDX-License-Identifier: BSD-3-Clause
2ea9382dcSThomas Monjalon  * Copyright 2017 Mellanox Technologies, Ltd
34a3ef59aSOri Kam  */
44a3ef59aSOri Kam 
54a3ef59aSOri Kam #include <stdio.h>
64a3ef59aSOri Kam #include <stdlib.h>
74a3ef59aSOri Kam #include <string.h>
84a3ef59aSOri Kam #include <stdint.h>
94a3ef59aSOri Kam #include <inttypes.h>
104a3ef59aSOri Kam #include <sys/types.h>
114a3ef59aSOri Kam #include <sys/queue.h>
124a3ef59aSOri Kam #include <setjmp.h>
134a3ef59aSOri Kam #include <stdarg.h>
144a3ef59aSOri Kam #include <ctype.h>
154a3ef59aSOri Kam #include <errno.h>
164a3ef59aSOri Kam #include <getopt.h>
174a3ef59aSOri Kam #include <signal.h>
184a3ef59aSOri Kam #include <stdbool.h>
194a3ef59aSOri Kam 
204a3ef59aSOri Kam #include <rte_eal.h>
214a3ef59aSOri Kam #include <rte_common.h>
224a3ef59aSOri Kam #include <rte_malloc.h>
234a3ef59aSOri Kam #include <rte_ether.h>
244a3ef59aSOri Kam #include <rte_ethdev.h>
254a3ef59aSOri Kam #include <rte_mempool.h>
264a3ef59aSOri Kam #include <rte_mbuf.h>
274a3ef59aSOri Kam #include <rte_net.h>
284a3ef59aSOri Kam #include <rte_flow.h>
29429cede1SBeilei Xing #include <rte_cycles.h>
304a3ef59aSOri Kam 
314a3ef59aSOri Kam static volatile bool force_quit;
324a3ef59aSOri Kam 
336014215aSZhiyong Yang static uint16_t port_id;
344a3ef59aSOri Kam static uint16_t nr_queues = 5;
354a3ef59aSOri Kam static uint8_t selected_queue = 1;
364a3ef59aSOri Kam struct rte_mempool *mbuf_pool;
374a3ef59aSOri Kam struct rte_flow *flow;
384a3ef59aSOri Kam 
394a3ef59aSOri Kam #define SRC_IP ((0<<24) + (0<<16) + (0<<8) + 0) /* src ip = 0.0.0.0 */
404a3ef59aSOri Kam #define DEST_IP ((192<<24) + (168<<16) + (1<<8) + 1) /* dest ip = 192.168.1.1 */
414a3ef59aSOri Kam #define FULL_MASK 0xffffffff /* full mask */
424a3ef59aSOri Kam #define EMPTY_MASK 0x0 /* empty mask */
434a3ef59aSOri Kam 
444a3ef59aSOri Kam #include "flow_blocks.c"
454a3ef59aSOri Kam 
464a3ef59aSOri Kam static inline void
print_ether_addr(const char * what,struct rte_ether_addr * eth_addr)476d13ea8eSOlivier Matz print_ether_addr(const char *what, struct rte_ether_addr *eth_addr)
484a3ef59aSOri Kam {
4935b2d13fSOlivier Matz 	char buf[RTE_ETHER_ADDR_FMT_SIZE];
5035b2d13fSOlivier Matz 	rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
514a3ef59aSOri Kam 	printf("%s%s", what, buf);
524a3ef59aSOri Kam }
534a3ef59aSOri Kam 
549a212dc0SConor Fogarty /* Main_loop for flow filtering. 8< */
55b55efbabSIvan Ilchenko static int
main_loop(void)564a3ef59aSOri Kam main_loop(void)
574a3ef59aSOri Kam {
584a3ef59aSOri Kam 	struct rte_mbuf *mbufs[32];
596d13ea8eSOlivier Matz 	struct rte_ether_hdr *eth_hdr;
604a3ef59aSOri Kam 	struct rte_flow_error error;
614a3ef59aSOri Kam 	uint16_t nb_rx;
624a3ef59aSOri Kam 	uint16_t i;
634a3ef59aSOri Kam 	uint16_t j;
64b55efbabSIvan Ilchenko 	int ret;
654a3ef59aSOri Kam 
669a212dc0SConor Fogarty 	/* Reading the packets from all queues. 8< */
674a3ef59aSOri Kam 	while (!force_quit) {
684a3ef59aSOri Kam 		for (i = 0; i < nr_queues; i++) {
694a3ef59aSOri Kam 			nb_rx = rte_eth_rx_burst(port_id,
704a3ef59aSOri Kam 						i, mbufs, 32);
714a3ef59aSOri Kam 			if (nb_rx) {
724a3ef59aSOri Kam 				for (j = 0; j < nb_rx; j++) {
734a3ef59aSOri Kam 					struct rte_mbuf *m = mbufs[j];
744a3ef59aSOri Kam 
754a3ef59aSOri Kam 					eth_hdr = rte_pktmbuf_mtod(m,
766d13ea8eSOlivier Matz 							struct rte_ether_hdr *);
774a3ef59aSOri Kam 					print_ether_addr("src=",
7804d43857SDmitry Kozlyuk 							&eth_hdr->src_addr);
794a3ef59aSOri Kam 					print_ether_addr(" - dst=",
8004d43857SDmitry Kozlyuk 							&eth_hdr->dst_addr);
814a3ef59aSOri Kam 					printf(" - queue=0x%x",
824a3ef59aSOri Kam 							(unsigned int)i);
834a3ef59aSOri Kam 					printf("\n");
844a3ef59aSOri Kam 
854a3ef59aSOri Kam 					rte_pktmbuf_free(m);
864a3ef59aSOri Kam 				}
874a3ef59aSOri Kam 			}
884a3ef59aSOri Kam 		}
894a3ef59aSOri Kam 	}
909a212dc0SConor Fogarty 	/* >8 End of reading the packets from all queues. */
914a3ef59aSOri Kam 
924a3ef59aSOri Kam 	/* closing and releasing resources */
934a3ef59aSOri Kam 	rte_flow_flush(port_id, &error);
94b55efbabSIvan Ilchenko 	ret = rte_eth_dev_stop(port_id);
95b55efbabSIvan Ilchenko 	if (ret < 0)
96b55efbabSIvan Ilchenko 		printf("Failed to stop port %u: %s",
97b55efbabSIvan Ilchenko 		       port_id, rte_strerror(-ret));
984a3ef59aSOri Kam 	rte_eth_dev_close(port_id);
99b55efbabSIvan Ilchenko 	return ret;
1004a3ef59aSOri Kam }
1019a212dc0SConor Fogarty /* >8 End of main_loop for flow filtering. */
1024a3ef59aSOri Kam 
103429cede1SBeilei Xing #define CHECK_INTERVAL 1000  /* 100ms */
104429cede1SBeilei Xing #define MAX_REPEAT_TIMES 90  /* 9s (90 * 100ms) in total */
105429cede1SBeilei Xing 
1064a3ef59aSOri Kam static void
assert_link_status(void)1074a3ef59aSOri Kam assert_link_status(void)
1084a3ef59aSOri Kam {
1094a3ef59aSOri Kam 	struct rte_eth_link link;
110429cede1SBeilei Xing 	uint8_t rep_cnt = MAX_REPEAT_TIMES;
11122e5c73bSIgor Romanov 	int link_get_err = -EINVAL;
1124a3ef59aSOri Kam 
1134a3ef59aSOri Kam 	memset(&link, 0, sizeof(link));
114429cede1SBeilei Xing 	do {
11522e5c73bSIgor Romanov 		link_get_err = rte_eth_link_get(port_id, &link);
116*295968d1SFerruh Yigit 		if (link_get_err == 0 && link.link_status == RTE_ETH_LINK_UP)
117429cede1SBeilei Xing 			break;
118429cede1SBeilei Xing 		rte_delay_ms(CHECK_INTERVAL);
119429cede1SBeilei Xing 	} while (--rep_cnt);
120429cede1SBeilei Xing 
12122e5c73bSIgor Romanov 	if (link_get_err < 0)
12222e5c73bSIgor Romanov 		rte_exit(EXIT_FAILURE, ":: error: link get is failing: %s\n",
12322e5c73bSIgor Romanov 			 rte_strerror(-link_get_err));
124*295968d1SFerruh Yigit 	if (link.link_status == RTE_ETH_LINK_DOWN)
1254a3ef59aSOri Kam 		rte_exit(EXIT_FAILURE, ":: error: link is still down\n");
1264a3ef59aSOri Kam }
1274a3ef59aSOri Kam 
1289a212dc0SConor Fogarty /* Port initialization used in flow filtering. 8< */
1294a3ef59aSOri Kam static void
init_port(void)1304a3ef59aSOri Kam init_port(void)
1314a3ef59aSOri Kam {
1324a3ef59aSOri Kam 	int ret;
1334a3ef59aSOri Kam 	uint16_t i;
1349a212dc0SConor Fogarty 	/* Ethernet port configured with default settings. 8< */
1354a3ef59aSOri Kam 	struct rte_eth_conf port_conf = {
1364a3ef59aSOri Kam 		.rxmode = {
1374a3ef59aSOri Kam 			.split_hdr_size = 0,
1384a3ef59aSOri Kam 		},
139feca6c42SWei Zhao 		.txmode = {
140feca6c42SWei Zhao 			.offloads =
141*295968d1SFerruh Yigit 				RTE_ETH_TX_OFFLOAD_VLAN_INSERT |
142*295968d1SFerruh Yigit 				RTE_ETH_TX_OFFLOAD_IPV4_CKSUM  |
143*295968d1SFerruh Yigit 				RTE_ETH_TX_OFFLOAD_UDP_CKSUM   |
144*295968d1SFerruh Yigit 				RTE_ETH_TX_OFFLOAD_TCP_CKSUM   |
145*295968d1SFerruh Yigit 				RTE_ETH_TX_OFFLOAD_SCTP_CKSUM  |
146*295968d1SFerruh Yigit 				RTE_ETH_TX_OFFLOAD_TCP_TSO,
147feca6c42SWei Zhao 		},
1484a3ef59aSOri Kam 	};
149feca6c42SWei Zhao 	struct rte_eth_txconf txq_conf;
150e6bb3077SShahaf Shuler 	struct rte_eth_rxconf rxq_conf;
151feca6c42SWei Zhao 	struct rte_eth_dev_info dev_info;
1524a3ef59aSOri Kam 
153d9a66c56SIvan Ilchenko 	ret = rte_eth_dev_info_get(port_id, &dev_info);
154d9a66c56SIvan Ilchenko 	if (ret != 0)
155d9a66c56SIvan Ilchenko 		rte_exit(EXIT_FAILURE,
156d9a66c56SIvan Ilchenko 			"Error during getting device (port %u) info: %s\n",
157d9a66c56SIvan Ilchenko 			port_id, strerror(-ret));
158d9a66c56SIvan Ilchenko 
159a5e7c52dSOri Kam 	port_conf.txmode.offloads &= dev_info.tx_offload_capa;
1604a3ef59aSOri Kam 	printf(":: initializing port: %d\n", port_id);
1614a3ef59aSOri Kam 	ret = rte_eth_dev_configure(port_id,
1624a3ef59aSOri Kam 				nr_queues, nr_queues, &port_conf);
1634a3ef59aSOri Kam 	if (ret < 0) {
1644a3ef59aSOri Kam 		rte_exit(EXIT_FAILURE,
1654a3ef59aSOri Kam 			":: cannot configure device: err=%d, port=%u\n",
1664a3ef59aSOri Kam 			ret, port_id);
1674a3ef59aSOri Kam 	}
1684a3ef59aSOri Kam 
169e6bb3077SShahaf Shuler 	rxq_conf = dev_info.default_rxconf;
170e6bb3077SShahaf Shuler 	rxq_conf.offloads = port_conf.rxmode.offloads;
1719a212dc0SConor Fogarty 	/* >8 End of ethernet port configured with default settings. */
1729a212dc0SConor Fogarty 
1739a212dc0SConor Fogarty 	/* Configuring number of RX and TX queues connected to single port. 8< */
1744a3ef59aSOri Kam 	for (i = 0; i < nr_queues; i++) {
1754a3ef59aSOri Kam 		ret = rte_eth_rx_queue_setup(port_id, i, 512,
1764a3ef59aSOri Kam 				     rte_eth_dev_socket_id(port_id),
177e6bb3077SShahaf Shuler 				     &rxq_conf,
1784a3ef59aSOri Kam 				     mbuf_pool);
1794a3ef59aSOri Kam 		if (ret < 0) {
1804a3ef59aSOri Kam 			rte_exit(EXIT_FAILURE,
1814a3ef59aSOri Kam 				":: Rx queue setup failed: err=%d, port=%u\n",
1824a3ef59aSOri Kam 				ret, port_id);
1834a3ef59aSOri Kam 		}
1844a3ef59aSOri Kam 	}
1854a3ef59aSOri Kam 
186feca6c42SWei Zhao 	txq_conf = dev_info.default_txconf;
187feca6c42SWei Zhao 	txq_conf.offloads = port_conf.txmode.offloads;
188feca6c42SWei Zhao 
189feca6c42SWei Zhao 	for (i = 0; i < nr_queues; i++) {
190feca6c42SWei Zhao 		ret = rte_eth_tx_queue_setup(port_id, i, 512,
191feca6c42SWei Zhao 				rte_eth_dev_socket_id(port_id),
192feca6c42SWei Zhao 				&txq_conf);
193feca6c42SWei Zhao 		if (ret < 0) {
194feca6c42SWei Zhao 			rte_exit(EXIT_FAILURE,
195feca6c42SWei Zhao 				":: Tx queue setup failed: err=%d, port=%u\n",
196feca6c42SWei Zhao 				ret, port_id);
197feca6c42SWei Zhao 		}
198feca6c42SWei Zhao 	}
1999a212dc0SConor Fogarty 	/* >8 End of Configuring RX and TX queues connected to single port. */
200feca6c42SWei Zhao 
2019a212dc0SConor Fogarty 	/* Setting the RX port to promiscuous mode. 8< */
202f430bbceSIvan Ilchenko 	ret = rte_eth_promiscuous_enable(port_id);
203f430bbceSIvan Ilchenko 	if (ret != 0)
204f430bbceSIvan Ilchenko 		rte_exit(EXIT_FAILURE,
205f430bbceSIvan Ilchenko 			":: promiscuous mode enable failed: err=%s, port=%u\n",
206f430bbceSIvan Ilchenko 			rte_strerror(-ret), port_id);
2079a212dc0SConor Fogarty 	/* >8 End of setting the RX port to promiscuous mode. */
208f430bbceSIvan Ilchenko 
2099a212dc0SConor Fogarty 	/* Starting the port. 8< */
2104a3ef59aSOri Kam 	ret = rte_eth_dev_start(port_id);
2114a3ef59aSOri Kam 	if (ret < 0) {
2124a3ef59aSOri Kam 		rte_exit(EXIT_FAILURE,
2134a3ef59aSOri Kam 			"rte_eth_dev_start:err=%d, port=%u\n",
2144a3ef59aSOri Kam 			ret, port_id);
2154a3ef59aSOri Kam 	}
2169a212dc0SConor Fogarty 	/* >8 End of starting the port. */
2174a3ef59aSOri Kam 
2184a3ef59aSOri Kam 	assert_link_status();
2194a3ef59aSOri Kam 
2204a3ef59aSOri Kam 	printf(":: initializing port: %d done\n", port_id);
2214a3ef59aSOri Kam }
2229a212dc0SConor Fogarty /* >8 End of Port initialization used in flow filtering. */
2234a3ef59aSOri Kam 
2244a3ef59aSOri Kam static void
signal_handler(int signum)2254a3ef59aSOri Kam signal_handler(int signum)
2264a3ef59aSOri Kam {
2274a3ef59aSOri Kam 	if (signum == SIGINT || signum == SIGTERM) {
2284a3ef59aSOri Kam 		printf("\n\nSignal %d received, preparing to exit...\n",
2294a3ef59aSOri Kam 				signum);
2304a3ef59aSOri Kam 		force_quit = true;
2314a3ef59aSOri Kam 	}
2324a3ef59aSOri Kam }
2334a3ef59aSOri Kam 
2344a3ef59aSOri Kam int
main(int argc,char ** argv)2354a3ef59aSOri Kam main(int argc, char **argv)
2364a3ef59aSOri Kam {
2374a3ef59aSOri Kam 	int ret;
238d9a42a69SThomas Monjalon 	uint16_t nr_ports;
2394a3ef59aSOri Kam 	struct rte_flow_error error;
2404a3ef59aSOri Kam 
2419a212dc0SConor Fogarty 	/* Initialize EAL. 8< */
2424a3ef59aSOri Kam 	ret = rte_eal_init(argc, argv);
2434a3ef59aSOri Kam 	if (ret < 0)
2444a3ef59aSOri Kam 		rte_exit(EXIT_FAILURE, ":: invalid EAL arguments\n");
2459a212dc0SConor Fogarty 	/* >8 End of Initialization of EAL. */
2464a3ef59aSOri Kam 
2474a3ef59aSOri Kam 	force_quit = false;
2484a3ef59aSOri Kam 	signal(SIGINT, signal_handler);
2494a3ef59aSOri Kam 	signal(SIGTERM, signal_handler);
2504a3ef59aSOri Kam 
251d9a42a69SThomas Monjalon 	nr_ports = rte_eth_dev_count_avail();
2524a3ef59aSOri Kam 	if (nr_ports == 0)
2534a3ef59aSOri Kam 		rte_exit(EXIT_FAILURE, ":: no Ethernet ports found\n");
2544a3ef59aSOri Kam 	port_id = 0;
2554a3ef59aSOri Kam 	if (nr_ports != 1) {
2564a3ef59aSOri Kam 		printf(":: warn: %d ports detected, but we use only one: port %u\n",
2574a3ef59aSOri Kam 			nr_ports, port_id);
2584a3ef59aSOri Kam 	}
2599a212dc0SConor Fogarty 	/* Allocates a mempool to hold the mbufs. 8< */
2604a3ef59aSOri Kam 	mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", 4096, 128, 0,
2614a3ef59aSOri Kam 					    RTE_MBUF_DEFAULT_BUF_SIZE,
2624a3ef59aSOri Kam 					    rte_socket_id());
2639a212dc0SConor Fogarty 	/* >8 End of allocating a mempool to hold the mbufs. */
2644a3ef59aSOri Kam 	if (mbuf_pool == NULL)
2654a3ef59aSOri Kam 		rte_exit(EXIT_FAILURE, "Cannot init mbuf pool\n");
2664a3ef59aSOri Kam 
2679a212dc0SConor Fogarty 	/* Initializes all the ports using the user defined init_port(). 8< */
2684a3ef59aSOri Kam 	init_port();
2699a212dc0SConor Fogarty 	/* >8 End of Initializing the ports using user defined init_port(). */
2704a3ef59aSOri Kam 
2719a212dc0SConor Fogarty 	/* Create flow for send packet with. 8< */
2724a3ef59aSOri Kam 	flow = generate_ipv4_flow(port_id, selected_queue,
2734a3ef59aSOri Kam 				SRC_IP, EMPTY_MASK,
2744a3ef59aSOri Kam 				DEST_IP, FULL_MASK, &error);
2759a212dc0SConor Fogarty 	/* >8 End of create flow and the flow rule. */
2764a3ef59aSOri Kam 	if (!flow) {
2774a3ef59aSOri Kam 		printf("Flow can't be created %d message: %s\n",
2784a3ef59aSOri Kam 			error.type,
2794a3ef59aSOri Kam 			error.message ? error.message : "(no stated reason)");
2804a3ef59aSOri Kam 		rte_exit(EXIT_FAILURE, "error in creating flow");
2814a3ef59aSOri Kam 	}
2829a212dc0SConor Fogarty 	/* >8 End of creating flow for send packet with. */
2834a3ef59aSOri Kam 
2849a212dc0SConor Fogarty 	/* Launching main_loop(). 8< */
28510aa3757SChengchang Tang 	ret = main_loop();
2869a212dc0SConor Fogarty 	/* >8 End of launching main_loop(). */
28710aa3757SChengchang Tang 
28810aa3757SChengchang Tang 	/* clean up the EAL */
28910aa3757SChengchang Tang 	rte_eal_cleanup();
29010aa3757SChengchang Tang 
29110aa3757SChengchang Tang 	return ret;
2924a3ef59aSOri Kam }
293