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