xref: /dpdk/examples/flow_filtering/main.c (revision fd5f9fb95ff5fb2543ad1cd8b4206f531d443347)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2017 Mellanox Technologies, Ltd
3  */
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <stdint.h>
9 #include <inttypes.h>
10 #include <sys/types.h>
11 #include <sys/queue.h>
12 #include <netinet/in.h>
13 #include <setjmp.h>
14 #include <stdarg.h>
15 #include <ctype.h>
16 #include <errno.h>
17 #include <getopt.h>
18 #include <signal.h>
19 #include <stdbool.h>
20 
21 #include <rte_eal.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>
27 #include <rte_mbuf.h>
28 #include <rte_net.h>
29 #include <rte_flow.h>
30 #include <rte_cycles.h>
31 
32 static volatile bool force_quit;
33 
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;
39 
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 */
44 
45 #include "flow_blocks.c"
46 
47 static inline void
48 print_ether_addr(const char *what, struct rte_ether_addr *eth_addr)
49 {
50 	char buf[RTE_ETHER_ADDR_FMT_SIZE];
51 	rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
52 	printf("%s%s", what, buf);
53 }
54 
55 static int
56 main_loop(void)
57 {
58 	struct rte_mbuf *mbufs[32];
59 	struct rte_ether_hdr *eth_hdr;
60 	struct rte_flow_error error;
61 	uint16_t nb_rx;
62 	uint16_t i;
63 	uint16_t j;
64 	int ret;
65 
66 	while (!force_quit) {
67 		for (i = 0; i < nr_queues; i++) {
68 			nb_rx = rte_eth_rx_burst(port_id,
69 						i, mbufs, 32);
70 			if (nb_rx) {
71 				for (j = 0; j < nb_rx; j++) {
72 					struct rte_mbuf *m = mbufs[j];
73 
74 					eth_hdr = rte_pktmbuf_mtod(m,
75 							struct rte_ether_hdr *);
76 					print_ether_addr("src=",
77 							&eth_hdr->s_addr);
78 					print_ether_addr(" - dst=",
79 							&eth_hdr->d_addr);
80 					printf(" - queue=0x%x",
81 							(unsigned int)i);
82 					printf("\n");
83 
84 					rte_pktmbuf_free(m);
85 				}
86 			}
87 		}
88 	}
89 
90 	/* closing and releasing resources */
91 	rte_flow_flush(port_id, &error);
92 	ret = rte_eth_dev_stop(port_id);
93 	if (ret < 0)
94 		printf("Failed to stop port %u: %s",
95 		       port_id, rte_strerror(-ret));
96 	rte_eth_dev_close(port_id);
97 	return ret;
98 }
99 
100 #define CHECK_INTERVAL 1000  /* 100ms */
101 #define MAX_REPEAT_TIMES 90  /* 9s (90 * 100ms) in total */
102 
103 static void
104 assert_link_status(void)
105 {
106 	struct rte_eth_link link;
107 	uint8_t rep_cnt = MAX_REPEAT_TIMES;
108 	int link_get_err = -EINVAL;
109 
110 	memset(&link, 0, sizeof(link));
111 	do {
112 		link_get_err = rte_eth_link_get(port_id, &link);
113 		if (link_get_err == 0 && link.link_status == ETH_LINK_UP)
114 			break;
115 		rte_delay_ms(CHECK_INTERVAL);
116 	} while (--rep_cnt);
117 
118 	if (link_get_err < 0)
119 		rte_exit(EXIT_FAILURE, ":: error: link get is failing: %s\n",
120 			 rte_strerror(-link_get_err));
121 	if (link.link_status == ETH_LINK_DOWN)
122 		rte_exit(EXIT_FAILURE, ":: error: link is still down\n");
123 }
124 
125 static void
126 init_port(void)
127 {
128 	int ret;
129 	uint16_t i;
130 	struct rte_eth_conf port_conf = {
131 		.rxmode = {
132 			.split_hdr_size = 0,
133 		},
134 		.txmode = {
135 			.offloads =
136 				DEV_TX_OFFLOAD_VLAN_INSERT |
137 				DEV_TX_OFFLOAD_IPV4_CKSUM  |
138 				DEV_TX_OFFLOAD_UDP_CKSUM   |
139 				DEV_TX_OFFLOAD_TCP_CKSUM   |
140 				DEV_TX_OFFLOAD_SCTP_CKSUM  |
141 				DEV_TX_OFFLOAD_TCP_TSO,
142 		},
143 	};
144 	struct rte_eth_txconf txq_conf;
145 	struct rte_eth_rxconf rxq_conf;
146 	struct rte_eth_dev_info dev_info;
147 
148 	ret = rte_eth_dev_info_get(port_id, &dev_info);
149 	if (ret != 0)
150 		rte_exit(EXIT_FAILURE,
151 			"Error during getting device (port %u) info: %s\n",
152 			port_id, strerror(-ret));
153 
154 	port_conf.txmode.offloads &= dev_info.tx_offload_capa;
155 	printf(":: initializing port: %d\n", port_id);
156 	ret = rte_eth_dev_configure(port_id,
157 				nr_queues, nr_queues, &port_conf);
158 	if (ret < 0) {
159 		rte_exit(EXIT_FAILURE,
160 			":: cannot configure device: err=%d, port=%u\n",
161 			ret, port_id);
162 	}
163 
164 	rxq_conf = dev_info.default_rxconf;
165 	rxq_conf.offloads = port_conf.rxmode.offloads;
166 	for (i = 0; i < nr_queues; i++) {
167 		ret = rte_eth_rx_queue_setup(port_id, i, 512,
168 				     rte_eth_dev_socket_id(port_id),
169 				     &rxq_conf,
170 				     mbuf_pool);
171 		if (ret < 0) {
172 			rte_exit(EXIT_FAILURE,
173 				":: Rx queue setup failed: err=%d, port=%u\n",
174 				ret, port_id);
175 		}
176 	}
177 
178 	txq_conf = dev_info.default_txconf;
179 	txq_conf.offloads = port_conf.txmode.offloads;
180 
181 	for (i = 0; i < nr_queues; i++) {
182 		ret = rte_eth_tx_queue_setup(port_id, i, 512,
183 				rte_eth_dev_socket_id(port_id),
184 				&txq_conf);
185 		if (ret < 0) {
186 			rte_exit(EXIT_FAILURE,
187 				":: Tx queue setup failed: err=%d, port=%u\n",
188 				ret, port_id);
189 		}
190 	}
191 
192 	ret = rte_eth_promiscuous_enable(port_id);
193 	if (ret != 0)
194 		rte_exit(EXIT_FAILURE,
195 			":: promiscuous mode enable failed: err=%s, port=%u\n",
196 			rte_strerror(-ret), port_id);
197 
198 	ret = rte_eth_dev_start(port_id);
199 	if (ret < 0) {
200 		rte_exit(EXIT_FAILURE,
201 			"rte_eth_dev_start:err=%d, port=%u\n",
202 			ret, port_id);
203 	}
204 
205 	assert_link_status();
206 
207 	printf(":: initializing port: %d done\n", port_id);
208 }
209 
210 static void
211 signal_handler(int signum)
212 {
213 	if (signum == SIGINT || signum == SIGTERM) {
214 		printf("\n\nSignal %d received, preparing to exit...\n",
215 				signum);
216 		force_quit = true;
217 	}
218 }
219 
220 int
221 main(int argc, char **argv)
222 {
223 	int ret;
224 	uint16_t nr_ports;
225 	struct rte_flow_error error;
226 
227 	ret = rte_eal_init(argc, argv);
228 	if (ret < 0)
229 		rte_exit(EXIT_FAILURE, ":: invalid EAL arguments\n");
230 
231 	force_quit = false;
232 	signal(SIGINT, signal_handler);
233 	signal(SIGTERM, signal_handler);
234 
235 	nr_ports = rte_eth_dev_count_avail();
236 	if (nr_ports == 0)
237 		rte_exit(EXIT_FAILURE, ":: no Ethernet ports found\n");
238 	port_id = 0;
239 	if (nr_ports != 1) {
240 		printf(":: warn: %d ports detected, but we use only one: port %u\n",
241 			nr_ports, port_id);
242 	}
243 	mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", 4096, 128, 0,
244 					    RTE_MBUF_DEFAULT_BUF_SIZE,
245 					    rte_socket_id());
246 	if (mbuf_pool == NULL)
247 		rte_exit(EXIT_FAILURE, "Cannot init mbuf pool\n");
248 
249 	init_port();
250 
251 	/* create flow for send packet with */
252 	flow = generate_ipv4_flow(port_id, selected_queue,
253 				SRC_IP, EMPTY_MASK,
254 				DEST_IP, FULL_MASK, &error);
255 	if (!flow) {
256 		printf("Flow can't be created %d message: %s\n",
257 			error.type,
258 			error.message ? error.message : "(no stated reason)");
259 		rte_exit(EXIT_FAILURE, "error in creating flow");
260 	}
261 
262 	return main_loop();
263 }
264